• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Utils.h"
24 #include "llvm/Transforms/Utils/Local.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "instsimplify"
28 
29 STATISTIC(NumSimplified, "Number of redundant instructions removed");
30 
runImpl(Function & F,const SimplifyQuery & SQ,OptimizationRemarkEmitter * ORE)31 static bool runImpl(Function &F, const SimplifyQuery &SQ,
32                     OptimizationRemarkEmitter *ORE) {
33   SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
34   bool Changed = false;
35 
36   do {
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))
41         continue;
42 
43       SmallVector<Instruction *, 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))
49           continue;
50 
51         // Don't waste time simplifying dead/unused instructions.
52         if (isInstructionTriviallyDead(&I)) {
53           DeadInstsInBB.push_back(&I);
54           Changed = true;
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);
61             ++NumSimplified;
62             Changed = true;
63             // A call can get simplified, but it may not be trivially dead.
64             if (isInstructionTriviallyDead(&I))
65               DeadInstsInBB.push_back(&I);
66           }
67         }
68       }
69       RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI);
70     }
71 
72     // Place the list of instructions to simplify on the next loop iteration
73     // into ToSimplify.
74     std::swap(ToSimplify, Next);
75     Next->clear();
76   } while (!ToSimplify->empty());
77 
78   return Changed;
79 }
80 
81 namespace {
82 struct InstSimplifyLegacyPass : public FunctionPass {
83   static char ID; // Pass identification, replacement for typeid
InstSimplifyLegacyPass__anonbe63a0e00111::InstSimplifyLegacyPass84   InstSimplifyLegacyPass() : FunctionPass(ID) {
85     initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
86   }
87 
getAnalysisUsage__anonbe63a0e00111::InstSimplifyLegacyPass88   void getAnalysisUsage(AnalysisUsage &AU) const override {
89     AU.setPreservesCFG();
90     AU.addRequired<DominatorTreeWrapperPass>();
91     AU.addRequired<AssumptionCacheTracker>();
92     AU.addRequired<TargetLibraryInfoWrapperPass>();
93     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
94   }
95 
96   /// Remove instructions that simplify.
runOnFunction__anonbe63a0e00111::InstSimplifyLegacyPass97   bool runOnFunction(Function &F) override {
98     if (skipFunction(F))
99       return false;
100 
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);
112   }
113 };
114 } // namespace
115 
116 char InstSimplifyLegacyPass::ID = 0;
117 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
118                       "Remove redundant instructions", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)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)
125 
126 // Public interface to the simplify instructions pass.
127 FunctionPass *llvm::createInstSimplifyLegacyPass() {
128   return new InstSimplifyLegacyPass();
129 }
130 
run(Function & F,FunctionAnalysisManager & AM)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);
140   if (!Changed)
141     return PreservedAnalyses::all();
142 
143   PreservedAnalyses PA;
144   PA.preserveSet<CFGAnalyses>();
145   return PA;
146 }
147