1 //===- InstSimplifyPass.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
11 #include "llvm/ADT/DepthFirstIterator.h"
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/Analysis/AssumptionCache.h"
15 #include "llvm/Analysis/InstructionSimplify.h"
16 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Transforms/Utils.h"
24 #include "llvm/Transforms/Utils/Local.h"
27 #define DEBUG_TYPE "instsimplify"
29 STATISTIC(NumSimplified, "Number of redundant instructions removed");
31 static bool runImpl(Function &F, const SimplifyQuery &SQ,
32 OptimizationRemarkEmitter *ORE) {
33 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
37 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
38 // Here be subtlety: the iterator must be incremented before the loop
39 // body (not sure why), so a range-for loop won't work here.
40 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
41 Instruction *I = &*BI++;
42 // The first time through the loop ToSimplify is empty and we try to
43 // simplify all instructions. On later iterations ToSimplify is not
44 // empty and we only bother simplifying instructions that are in it.
45 if (!ToSimplify->empty() && !ToSimplify->count(I))
48 // Don't waste time simplifying unused instructions.
49 if (!I->use_empty()) {
50 if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
51 // Mark all uses for resimplification next time round the loop.
52 for (User *U : I->users())
53 Next->insert(cast<Instruction>(U));
54 I->replaceAllUsesWith(V);
59 if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
60 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
61 // instruction, so simply incrementing the iterator does not work.
62 // When instructions get deleted re-iterate instead.
70 // Place the list of instructions to simplify on the next loop iteration
72 std::swap(ToSimplify, Next);
74 } while (!ToSimplify->empty());
80 struct InstSimplifyLegacyPass : public FunctionPass {
81 static char ID; // Pass identification, replacement for typeid
82 InstSimplifyLegacyPass() : FunctionPass(ID) {
83 initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
86 void getAnalysisUsage(AnalysisUsage &AU) const override {
88 AU.addRequired<DominatorTreeWrapperPass>();
89 AU.addRequired<AssumptionCacheTracker>();
90 AU.addRequired<TargetLibraryInfoWrapperPass>();
91 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
94 /// runOnFunction - Remove instructions that simplify.
95 bool runOnFunction(Function &F) override {
99 const DominatorTree *DT =
100 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
101 const TargetLibraryInfo *TLI =
102 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
103 AssumptionCache *AC =
104 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
105 OptimizationRemarkEmitter *ORE =
106 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
107 const DataLayout &DL = F.getParent()->getDataLayout();
108 const SimplifyQuery SQ(DL, TLI, DT, AC);
109 return runImpl(F, SQ, ORE);
114 char InstSimplifyLegacyPass::ID = 0;
115 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
116 "Remove redundant instructions", false, false)
117 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
118 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
119 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
120 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
121 INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
122 "Remove redundant instructions", false, false)
124 // Public interface to the simplify instructions pass.
125 FunctionPass *llvm::createInstSimplifyLegacyPass() {
126 return new InstSimplifyLegacyPass();
129 PreservedAnalyses InstSimplifyPass::run(Function &F,
130 FunctionAnalysisManager &AM) {
131 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
132 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
133 auto &AC = AM.getResult<AssumptionAnalysis>(F);
134 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
135 const DataLayout &DL = F.getParent()->getDataLayout();
136 const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
137 bool Changed = runImpl(F, SQ, &ORE);
139 return PreservedAnalyses::all();
141 PreservedAnalyses PA;
142 PA.preserveSet<CFGAnalyses>();