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