1 //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
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/InstructionSimplify.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Target/TargetLibraryInfo.h"
28 #include "llvm/Transforms/Utils/Local.h"
29 using namespace llvm;
30
31 #define DEBUG_TYPE "instsimplify"
32
33 STATISTIC(NumSimplified, "Number of redundant instructions removed");
34
35 namespace {
36 struct InstSimplifier : public FunctionPass {
37 static char ID; // Pass identification, replacement for typeid
InstSimplifier__anonea7169760111::InstSimplifier38 InstSimplifier() : FunctionPass(ID) {
39 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
40 }
41
getAnalysisUsage__anonea7169760111::InstSimplifier42 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.setPreservesCFG();
44 AU.addRequired<TargetLibraryInfo>();
45 }
46
47 /// runOnFunction - Remove instructions that simplify.
runOnFunction__anonea7169760111::InstSimplifier48 bool runOnFunction(Function &F) override {
49 const DominatorTreeWrapperPass *DTWP =
50 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
51 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
52 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
53 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
54 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
55 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
56 bool Changed = false;
57
58 do {
59 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
60 // Here be subtlety: the iterator must be incremented before the loop
61 // body (not sure why), so a range-for loop won't work here.
62 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
63 Instruction *I = BI++;
64 // The first time through the loop ToSimplify is empty and we try to
65 // simplify all instructions. On later iterations ToSimplify is not
66 // empty and we only bother simplifying instructions that are in it.
67 if (!ToSimplify->empty() && !ToSimplify->count(I))
68 continue;
69 // Don't waste time simplifying unused instructions.
70 if (!I->use_empty())
71 if (Value *V = SimplifyInstruction(I, DL, TLI, DT)) {
72 // Mark all uses for resimplification next time round the loop.
73 for (User *U : I->users())
74 Next->insert(cast<Instruction>(U));
75 I->replaceAllUsesWith(V);
76 ++NumSimplified;
77 Changed = true;
78 }
79 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
80 if (res) {
81 // RecursivelyDeleteTriviallyDeadInstruction can remove
82 // more than one instruction, so simply incrementing the
83 // iterator does not work. When instructions get deleted
84 // re-iterate instead.
85 BI = BB->begin(); BE = BB->end();
86 Changed |= res;
87 }
88 }
89
90 // Place the list of instructions to simplify on the next loop iteration
91 // into ToSimplify.
92 std::swap(ToSimplify, Next);
93 Next->clear();
94 } while (!ToSimplify->empty());
95
96 return Changed;
97 }
98 };
99 }
100
101 char InstSimplifier::ID = 0;
102 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
103 "Remove redundant instructions", false, false)
104 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
105 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
106 "Remove redundant instructions", false, false)
107 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
108
109 // Public interface to the simplify instructions pass.
createInstructionSimplifierPass()110 FunctionPass *llvm::createInstructionSimplifierPass() {
111 return new InstSimplifier();
112 }
113