LLVM API Documentation
00001 //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This is a utility pass used for testing the InstructionSimplify analysis. 00011 // The analysis is applied to every instruction, and if it simplifies then the 00012 // instruction is replaced by the simplification. If you are looking for a pass 00013 // that performs serious instruction folding, use the instcombine pass instead. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/Transforms/Scalar.h" 00018 #include "llvm/ADT/DepthFirstIterator.h" 00019 #include "llvm/ADT/SmallPtrSet.h" 00020 #include "llvm/ADT/Statistic.h" 00021 #include "llvm/Analysis/AssumptionTracker.h" 00022 #include "llvm/Analysis/InstructionSimplify.h" 00023 #include "llvm/IR/DataLayout.h" 00024 #include "llvm/IR/Dominators.h" 00025 #include "llvm/IR/Function.h" 00026 #include "llvm/IR/Type.h" 00027 #include "llvm/Pass.h" 00028 #include "llvm/Target/TargetLibraryInfo.h" 00029 #include "llvm/Transforms/Utils/Local.h" 00030 using namespace llvm; 00031 00032 #define DEBUG_TYPE "instsimplify" 00033 00034 STATISTIC(NumSimplified, "Number of redundant instructions removed"); 00035 00036 namespace { 00037 struct InstSimplifier : public FunctionPass { 00038 static char ID; // Pass identification, replacement for typeid 00039 InstSimplifier() : FunctionPass(ID) { 00040 initializeInstSimplifierPass(*PassRegistry::getPassRegistry()); 00041 } 00042 00043 void getAnalysisUsage(AnalysisUsage &AU) const override { 00044 AU.setPreservesCFG(); 00045 AU.addRequired<AssumptionTracker>(); 00046 AU.addRequired<TargetLibraryInfo>(); 00047 } 00048 00049 /// runOnFunction - Remove instructions that simplify. 00050 bool runOnFunction(Function &F) override { 00051 const DominatorTreeWrapperPass *DTWP = 00052 getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 00053 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; 00054 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 00055 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr; 00056 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>(); 00057 AssumptionTracker *AT = &getAnalysis<AssumptionTracker>(); 00058 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; 00059 bool Changed = false; 00060 00061 do { 00062 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) 00063 // Here be subtlety: the iterator must be incremented before the loop 00064 // body (not sure why), so a range-for loop won't work here. 00065 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { 00066 Instruction *I = BI++; 00067 // The first time through the loop ToSimplify is empty and we try to 00068 // simplify all instructions. On later iterations ToSimplify is not 00069 // empty and we only bother simplifying instructions that are in it. 00070 if (!ToSimplify->empty() && !ToSimplify->count(I)) 00071 continue; 00072 // Don't waste time simplifying unused instructions. 00073 if (!I->use_empty()) 00074 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AT)) { 00075 // Mark all uses for resimplification next time round the loop. 00076 for (User *U : I->users()) 00077 Next->insert(cast<Instruction>(U)); 00078 I->replaceAllUsesWith(V); 00079 ++NumSimplified; 00080 Changed = true; 00081 } 00082 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI); 00083 if (res) { 00084 // RecursivelyDeleteTriviallyDeadInstruction can remove 00085 // more than one instruction, so simply incrementing the 00086 // iterator does not work. When instructions get deleted 00087 // re-iterate instead. 00088 BI = BB->begin(); BE = BB->end(); 00089 Changed |= res; 00090 } 00091 } 00092 00093 // Place the list of instructions to simplify on the next loop iteration 00094 // into ToSimplify. 00095 std::swap(ToSimplify, Next); 00096 Next->clear(); 00097 } while (!ToSimplify->empty()); 00098 00099 return Changed; 00100 } 00101 }; 00102 } 00103 00104 char InstSimplifier::ID = 0; 00105 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify", 00106 "Remove redundant instructions", false, false) 00107 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) 00108 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) 00109 INITIALIZE_PASS_END(InstSimplifier, "instsimplify", 00110 "Remove redundant instructions", false, false) 00111 char &llvm::InstructionSimplifierID = InstSimplifier::ID; 00112 00113 // Public interface to the simplify instructions pass. 00114 FunctionPass *llvm::createInstructionSimplifierPass() { 00115 return new InstSimplifier(); 00116 }