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/OptimizationDiagnosticInfo.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/Utils/Local.h"
31 #include "llvm/Transforms/Scalar.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()) {
58 SimplifyInstruction(I, SQ.getWithInstruction(I), ORE)) {
59 // Mark all uses for resimplification next time round the loop.
60 for (User *U : I->users())
61 Next->insert(cast<Instruction>(U));
62 I->replaceAllUsesWith(V);
67 if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
68 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
69 // instruction, so simply incrementing the iterator does not work.
70 // When instructions get deleted re-iterate instead.
78 // Place the list of instructions to simplify on the next loop iteration
80 std::swap(ToSimplify, Next);
82 } while (!ToSimplify->empty());
88 struct InstSimplifier : public FunctionPass {
89 static char ID; // Pass identification, replacement for typeid
90 InstSimplifier() : FunctionPass(ID) {
91 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
94 void getAnalysisUsage(AnalysisUsage &AU) const override {
96 AU.addRequired<DominatorTreeWrapperPass>();
97 AU.addRequired<AssumptionCacheTracker>();
98 AU.addRequired<TargetLibraryInfoWrapperPass>();
99 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
102 /// runOnFunction - Remove instructions that simplify.
103 bool runOnFunction(Function &F) override {
107 const DominatorTree *DT =
108 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
109 const TargetLibraryInfo *TLI =
110 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
111 AssumptionCache *AC =
112 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
113 OptimizationRemarkEmitter *ORE =
114 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
115 const DataLayout &DL = F.getParent()->getDataLayout();
116 const SimplifyQuery SQ(DL, TLI, DT, AC);
117 return runImpl(F, SQ, ORE);
122 char InstSimplifier::ID = 0;
123 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
124 "Remove redundant instructions", false, false)
125 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
126 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
127 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
128 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
129 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
130 "Remove redundant instructions", false, false)
131 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
133 // Public interface to the simplify instructions pass.
134 FunctionPass *llvm::createInstructionSimplifierPass() {
135 return new InstSimplifier();
138 PreservedAnalyses InstSimplifierPass::run(Function &F,
139 FunctionAnalysisManager &AM) {
140 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
141 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
142 auto &AC = AM.getResult<AssumptionAnalysis>(F);
143 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
144 const DataLayout &DL = F.getParent()->getDataLayout();
145 const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
146 bool Changed = runImpl(F, SQ, &ORE);
148 return PreservedAnalyses::all();
150 PreservedAnalyses PA;
151 PA.preserveSet<CFGAnalyses>();