xref: /aosp_15_r20/external/llvm/lib/Transforms/Utils/SimplifyInstructions.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This is a utility pass used for testing the InstructionSimplify analysis.
11*9880d681SAndroid Build Coastguard Worker // The analysis is applied to every instruction, and if it simplifies then the
12*9880d681SAndroid Build Coastguard Worker // instruction is replaced by the simplification.  If you are looking for a pass
13*9880d681SAndroid Build Coastguard Worker // that performs serious instruction folding, use the instcombine pass instead.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/SimplifyInstructions.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DepthFirstIterator.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InstructionSimplify.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "instsimplify"
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSimplified, "Number of redundant instructions removed");
36*9880d681SAndroid Build Coastguard Worker 
runImpl(Function & F,const DominatorTree * DT,const TargetLibraryInfo * TLI,AssumptionCache * AC)37*9880d681SAndroid Build Coastguard Worker static bool runImpl(Function &F, const DominatorTree *DT, const TargetLibraryInfo *TLI,
38*9880d681SAndroid Build Coastguard Worker                     AssumptionCache *AC) {
39*9880d681SAndroid Build Coastguard Worker   const DataLayout &DL = F.getParent()->getDataLayout();
40*9880d681SAndroid Build Coastguard Worker   SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
41*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker   do {
44*9880d681SAndroid Build Coastguard Worker     for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
45*9880d681SAndroid Build Coastguard Worker       // Here be subtlety: the iterator must be incremented before the loop
46*9880d681SAndroid Build Coastguard Worker       // body (not sure why), so a range-for loop won't work here.
47*9880d681SAndroid Build Coastguard Worker       for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
48*9880d681SAndroid Build Coastguard Worker         Instruction *I = &*BI++;
49*9880d681SAndroid Build Coastguard Worker         // The first time through the loop ToSimplify is empty and we try to
50*9880d681SAndroid Build Coastguard Worker         // simplify all instructions.  On later iterations ToSimplify is not
51*9880d681SAndroid Build Coastguard Worker         // empty and we only bother simplifying instructions that are in it.
52*9880d681SAndroid Build Coastguard Worker         if (!ToSimplify->empty() && !ToSimplify->count(I))
53*9880d681SAndroid Build Coastguard Worker           continue;
54*9880d681SAndroid Build Coastguard Worker         // Don't waste time simplifying unused instructions.
55*9880d681SAndroid Build Coastguard Worker         if (!I->use_empty())
56*9880d681SAndroid Build Coastguard Worker           if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
57*9880d681SAndroid Build Coastguard Worker             // Mark all uses for resimplification next time round the loop.
58*9880d681SAndroid Build Coastguard Worker             for (User *U : I->users())
59*9880d681SAndroid Build Coastguard Worker               Next->insert(cast<Instruction>(U));
60*9880d681SAndroid Build Coastguard Worker             I->replaceAllUsesWith(V);
61*9880d681SAndroid Build Coastguard Worker             ++NumSimplified;
62*9880d681SAndroid Build Coastguard Worker             Changed = true;
63*9880d681SAndroid Build Coastguard Worker           }
64*9880d681SAndroid Build Coastguard Worker         bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
65*9880d681SAndroid Build Coastguard Worker         if (res)  {
66*9880d681SAndroid Build Coastguard Worker           // RecursivelyDeleteTriviallyDeadInstruction can remove
67*9880d681SAndroid Build Coastguard Worker           // more than one instruction, so simply incrementing the
68*9880d681SAndroid Build Coastguard Worker           // iterator does not work. When instructions get deleted
69*9880d681SAndroid Build Coastguard Worker           // re-iterate instead.
70*9880d681SAndroid Build Coastguard Worker           BI = BB->begin(); BE = BB->end();
71*9880d681SAndroid Build Coastguard Worker           Changed |= res;
72*9880d681SAndroid Build Coastguard Worker         }
73*9880d681SAndroid Build Coastguard Worker       }
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker     // Place the list of instructions to simplify on the next loop iteration
76*9880d681SAndroid Build Coastguard Worker     // into ToSimplify.
77*9880d681SAndroid Build Coastguard Worker     std::swap(ToSimplify, Next);
78*9880d681SAndroid Build Coastguard Worker     Next->clear();
79*9880d681SAndroid Build Coastguard Worker   } while (!ToSimplify->empty());
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker   return Changed;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker namespace {
85*9880d681SAndroid Build Coastguard Worker   struct InstSimplifier : public FunctionPass {
86*9880d681SAndroid Build Coastguard Worker     static char ID; // Pass identification, replacement for typeid
InstSimplifier__anona94573a80111::InstSimplifier87*9880d681SAndroid Build Coastguard Worker     InstSimplifier() : FunctionPass(ID) {
88*9880d681SAndroid Build Coastguard Worker       initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
89*9880d681SAndroid Build Coastguard Worker     }
90*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage__anona94573a80111::InstSimplifier91*9880d681SAndroid Build Coastguard Worker     void getAnalysisUsage(AnalysisUsage &AU) const override {
92*9880d681SAndroid Build Coastguard Worker       AU.setPreservesCFG();
93*9880d681SAndroid Build Coastguard Worker       AU.addRequired<AssumptionCacheTracker>();
94*9880d681SAndroid Build Coastguard Worker       AU.addRequired<TargetLibraryInfoWrapperPass>();
95*9880d681SAndroid Build Coastguard Worker     }
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker     /// runOnFunction - Remove instructions that simplify.
runOnFunction__anona94573a80111::InstSimplifier98*9880d681SAndroid Build Coastguard Worker     bool runOnFunction(Function &F) override {
99*9880d681SAndroid Build Coastguard Worker       if (skipFunction(F))
100*9880d681SAndroid Build Coastguard Worker         return false;
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker       const DominatorTreeWrapperPass *DTWP =
103*9880d681SAndroid Build Coastguard Worker           getAnalysisIfAvailable<DominatorTreeWrapperPass>();
104*9880d681SAndroid Build Coastguard Worker       const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
105*9880d681SAndroid Build Coastguard Worker       const TargetLibraryInfo *TLI =
106*9880d681SAndroid Build Coastguard Worker           &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
107*9880d681SAndroid Build Coastguard Worker       AssumptionCache *AC =
108*9880d681SAndroid Build Coastguard Worker           &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
109*9880d681SAndroid Build Coastguard Worker       return runImpl(F, DT, TLI, AC);
110*9880d681SAndroid Build Coastguard Worker     }
111*9880d681SAndroid Build Coastguard Worker   };
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker char InstSimplifier::ID = 0;
115*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
116*9880d681SAndroid Build Coastguard Worker                       "Remove redundant instructions", false, false)
117*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
118*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
119*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
120*9880d681SAndroid Build Coastguard Worker                     "Remove redundant instructions", false, false)
121*9880d681SAndroid Build Coastguard Worker char &llvm::InstructionSimplifierID = InstSimplifier::ID;
122*9880d681SAndroid Build Coastguard Worker 
123*9880d681SAndroid Build Coastguard Worker // Public interface to the simplify instructions pass.
createInstructionSimplifierPass()124*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createInstructionSimplifierPass() {
125*9880d681SAndroid Build Coastguard Worker   return new InstSimplifier();
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker 
run(Function & F,AnalysisManager<Function> & AM)128*9880d681SAndroid Build Coastguard Worker PreservedAnalyses InstSimplifierPass::run(Function &F,
129*9880d681SAndroid Build Coastguard Worker                                       AnalysisManager<Function> &AM) {
130*9880d681SAndroid Build Coastguard Worker   auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
131*9880d681SAndroid Build Coastguard Worker   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
132*9880d681SAndroid Build Coastguard Worker   auto &AC = AM.getResult<AssumptionAnalysis>(F);
133*9880d681SAndroid Build Coastguard Worker   bool Changed = runImpl(F, DT, &TLI, &AC);
134*9880d681SAndroid Build Coastguard Worker   if (!Changed)
135*9880d681SAndroid Build Coastguard Worker     return PreservedAnalyses::all();
136*9880d681SAndroid Build Coastguard Worker   // FIXME: This should also 'preserve the CFG'.
137*9880d681SAndroid Build Coastguard Worker   return PreservedAnalyses::none();
138*9880d681SAndroid Build Coastguard Worker }
139