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/Scalar.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/IR/DataLayout.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/Transforms/Utils/Local.h"
32 #define DEBUG_TYPE "instsimplify"
34 STATISTIC(NumSimplified, "Number of redundant instructions removed");
37 struct InstSimplifier : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 InstSimplifier() : FunctionPass(ID) {
40 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.addRequired<AssumptionCacheTracker>();
46 AU.addRequired<TargetLibraryInfoWrapperPass>();
49 /// runOnFunction - Remove instructions that simplify.
50 bool runOnFunction(Function &F) override {
51 const DominatorTreeWrapperPass *DTWP =
52 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
53 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
54 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
55 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
56 const TargetLibraryInfo *TLI =
57 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
59 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
60 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
64 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
65 // Here be subtlety: the iterator must be incremented before the loop
66 // body (not sure why), so a range-for loop won't work here.
67 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
68 Instruction *I = BI++;
69 // The first time through the loop ToSimplify is empty and we try to
70 // simplify all instructions. On later iterations ToSimplify is not
71 // empty and we only bother simplifying instructions that are in it.
72 if (!ToSimplify->empty() && !ToSimplify->count(I))
74 // Don't waste time simplifying unused instructions.
76 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
77 // Mark all uses for resimplification next time round the loop.
78 for (User *U : I->users())
79 Next->insert(cast<Instruction>(U));
80 I->replaceAllUsesWith(V);
84 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
86 // RecursivelyDeleteTriviallyDeadInstruction can remove
87 // more than one instruction, so simply incrementing the
88 // iterator does not work. When instructions get deleted
89 // re-iterate instead.
90 BI = BB->begin(); BE = BB->end();
95 // Place the list of instructions to simplify on the next loop iteration
97 std::swap(ToSimplify, Next);
99 } while (!ToSimplify->empty());
106 char InstSimplifier::ID = 0;
107 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
108 "Remove redundant instructions", false, false)
109 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
110 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
111 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
112 "Remove redundant instructions", false, false)
113 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
115 // Public interface to the simplify instructions pass.
116 FunctionPass *llvm::createInstructionSimplifierPass() {
117 return new InstSimplifier();