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