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