1 //===- InstSimplifyPass.cpp -----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Transforms/Scalar/InstSimplifyPass.h" 10 #include "llvm/ADT/DepthFirstIterator.h" 11 #include "llvm/ADT/SmallPtrSet.h" 12 #include "llvm/ADT/Statistic.h" 13 #include "llvm/Analysis/AssumptionCache.h" 14 #include "llvm/Analysis/InstructionSimplify.h" 15 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 16 #include "llvm/Analysis/TargetLibraryInfo.h" 17 #include "llvm/IR/DataLayout.h" 18 #include "llvm/IR/Dominators.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/Type.h" 21 #include "llvm/InitializePasses.h" 22 #include "llvm/Pass.h" 23 #include "llvm/Transforms/Scalar.h" 24 #include "llvm/Transforms/Utils.h" 25 #include "llvm/Transforms/Utils/Local.h" 26 using namespace llvm; 27 28 #define DEBUG_TYPE "instsimplify" 29 30 STATISTIC(NumSimplified, "Number of redundant instructions removed"); 31 32 static bool runImpl(Function &F, const SimplifyQuery &SQ, 33 OptimizationRemarkEmitter *ORE) { 34 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; 35 bool Changed = false; 36 37 do { 38 for (BasicBlock &BB : F) { 39 // Unreachable code can take on strange forms that we are not prepared to 40 // handle. For example, an instruction may have itself as an operand. 41 if (!SQ.DT->isReachableFromEntry(&BB)) 42 continue; 43 44 SmallVector<WeakTrackingVH, 8> DeadInstsInBB; 45 for (Instruction &I : BB) { 46 // The first time through the loop, ToSimplify is empty and we try to 47 // simplify all instructions. On later iterations, ToSimplify is not 48 // empty and we only bother simplifying instructions that are in it. 49 if (!ToSimplify->empty() && !ToSimplify->count(&I)) 50 continue; 51 52 // Don't waste time simplifying dead/unused instructions. 53 if (isInstructionTriviallyDead(&I)) { 54 DeadInstsInBB.push_back(&I); 55 Changed = true; 56 } else 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); 62 ++NumSimplified; 63 Changed = true; 64 // A call can get simplified, but it may not be trivially dead. 65 if (isInstructionTriviallyDead(&I)) 66 DeadInstsInBB.push_back(&I); 67 } 68 } 69 } 70 RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI); 71 } 72 73 // Place the list of instructions to simplify on the next loop iteration 74 // into ToSimplify. 75 std::swap(ToSimplify, Next); 76 Next->clear(); 77 } while (!ToSimplify->empty()); 78 79 return Changed; 80 } 81 82 namespace { 83 struct InstSimplifyLegacyPass : public FunctionPass { 84 static char ID; // Pass identification, replacement for typeid 85 InstSimplifyLegacyPass() : FunctionPass(ID) { 86 initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry()); 87 } 88 89 void getAnalysisUsage(AnalysisUsage &AU) const override { 90 AU.setPreservesCFG(); 91 AU.addRequired<DominatorTreeWrapperPass>(); 92 AU.addRequired<AssumptionCacheTracker>(); 93 AU.addRequired<TargetLibraryInfoWrapperPass>(); 94 AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); 95 } 96 97 /// Remove instructions that simplify. 98 bool runOnFunction(Function &F) override { 99 if (skipFunction(F)) 100 return false; 101 102 const DominatorTree *DT = 103 &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 104 const TargetLibraryInfo *TLI = 105 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 106 AssumptionCache *AC = 107 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 108 OptimizationRemarkEmitter *ORE = 109 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); 110 const DataLayout &DL = F.getParent()->getDataLayout(); 111 const SimplifyQuery SQ(DL, TLI, DT, AC); 112 return runImpl(F, SQ, ORE); 113 } 114 }; 115 } // namespace 116 117 char InstSimplifyLegacyPass::ID = 0; 118 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify", 119 "Remove redundant instructions", false, false) 120 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 121 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 122 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 123 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) 124 INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify", 125 "Remove redundant instructions", false, false) 126 127 // Public interface to the simplify instructions pass. 128 FunctionPass *llvm::createInstSimplifyLegacyPass() { 129 return new InstSimplifyLegacyPass(); 130 } 131 132 PreservedAnalyses InstSimplifyPass::run(Function &F, 133 FunctionAnalysisManager &AM) { 134 auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 135 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); 136 auto &AC = AM.getResult<AssumptionAnalysis>(F); 137 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F); 138 const DataLayout &DL = F.getParent()->getDataLayout(); 139 const SimplifyQuery SQ(DL, &TLI, &DT, &AC); 140 bool Changed = runImpl(F, SQ, &ORE); 141 if (!Changed) 142 return PreservedAnalyses::all(); 143 144 PreservedAnalyses PA; 145 PA.preserveSet<CFGAnalyses>(); 146 return PA; 147 } 148