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