1 //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
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 // This is a utility pass used for testing the InstructionSimplify analysis.
11 // The analysis is applied to every instruction, and if it simplifies then the
12 // instruction is replaced by the simplification. If you are looking for a pass
13 // that performs serious instruction folding, use the instcombine pass instead.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/SimplifyInstructions.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Utils/Local.h"
34 #define DEBUG_TYPE "instsimplify"
36 STATISTIC(NumSimplified, "Number of redundant instructions removed");
38 static bool runImpl(Function &F, const SimplifyQuery &SQ,
39 OptimizationRemarkEmitter *ORE) {
40 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
44 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
45 // Here be subtlety: the iterator must be incremented before the loop
46 // body (not sure why), so a range-for loop won't work here.
47 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
48 Instruction *I = &*BI++;
49 // The first time through the loop ToSimplify is empty and we try to
50 // simplify all instructions. On later iterations ToSimplify is not
51 // empty and we only bother simplifying instructions that are in it.
52 if (!ToSimplify->empty() && !ToSimplify->count(I))
55 // Don't waste time simplifying unused instructions.
56 if (!I->use_empty()) {
57 if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
58 // Mark all uses for resimplification next time round the loop.
59 for (User *U : I->users())
60 Next->insert(cast<Instruction>(U));
61 I->replaceAllUsesWith(V);
66 if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
67 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
68 // instruction, so simply incrementing the iterator does not work.
69 // When instructions get deleted re-iterate instead.
77 // Place the list of instructions to simplify on the next loop iteration
79 std::swap(ToSimplify, Next);
81 } while (!ToSimplify->empty());
87 struct InstSimplifier : public FunctionPass {
88 static char ID; // Pass identification, replacement for typeid
89 InstSimplifier() : FunctionPass(ID) {
90 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
93 void getAnalysisUsage(AnalysisUsage &AU) const override {
95 AU.addRequired<DominatorTreeWrapperPass>();
96 AU.addRequired<AssumptionCacheTracker>();
97 AU.addRequired<TargetLibraryInfoWrapperPass>();
98 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
101 /// runOnFunction - Remove instructions that simplify.
102 bool runOnFunction(Function &F) override {
106 const DominatorTree *DT =
107 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
108 const TargetLibraryInfo *TLI =
109 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
110 AssumptionCache *AC =
111 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
112 OptimizationRemarkEmitter *ORE =
113 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
114 const DataLayout &DL = F.getParent()->getDataLayout();
115 const SimplifyQuery SQ(DL, TLI, DT, AC);
116 return runImpl(F, SQ, ORE);
121 char InstSimplifier::ID = 0;
122 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
123 "Remove redundant instructions", false, false)
124 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
125 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
126 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
127 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
128 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
129 "Remove redundant instructions", false, false)
130 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
132 // Public interface to the simplify instructions pass.
133 FunctionPass *llvm::createInstructionSimplifierPass() {
134 return new InstSimplifier();
137 PreservedAnalyses InstSimplifierPass::run(Function &F,
138 FunctionAnalysisManager &AM) {
139 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
140 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
141 auto &AC = AM.getResult<AssumptionAnalysis>(F);
142 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
143 const DataLayout &DL = F.getParent()->getDataLayout();
144 const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
145 bool Changed = runImpl(F, SQ, &ORE);
147 return PreservedAnalyses::all();
149 PreservedAnalyses PA;
150 PA.preserveSet<CFGAnalyses>();