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/Pass.h"
22 #include "llvm/Transforms/Utils.h"
23 #include "llvm/Transforms/Utils/Local.h"
26 #define DEBUG_TYPE "instsimplify"
28 STATISTIC(NumSimplified
, "Number of redundant instructions removed");
30 static bool runImpl(Function
&F
, const SimplifyQuery
&SQ
,
31 OptimizationRemarkEmitter
*ORE
) {
32 SmallPtrSet
<const Instruction
*, 8> S1
, S2
, *ToSimplify
= &S1
, *Next
= &S2
;
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
))
45 // Don't waste time simplifying dead/unused instructions.
46 if (isInstructionTriviallyDead(&I
)) {
47 DeadInstsInBB
.push_back(&I
);
49 } else if (!I
.use_empty()) {
50 if (Value
*V
= SimplifyInstruction(&I
, SQ
, ORE
)) {
51 // Mark all uses for resimplification next time round the loop.
52 for (User
*U
: I
.users())
53 Next
->insert(cast
<Instruction
>(U
));
54 I
.replaceAllUsesWith(V
);
57 // A call can get simplified, but it may not be trivially dead.
58 if (isInstructionTriviallyDead(&I
))
59 DeadInstsInBB
.push_back(&I
);
63 RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB
, SQ
.TLI
);
66 // Place the list of instructions to simplify on the next loop iteration
68 std::swap(ToSimplify
, Next
);
70 } while (!ToSimplify
->empty());
76 struct InstSimplifyLegacyPass
: public FunctionPass
{
77 static char ID
; // Pass identification, replacement for typeid
78 InstSimplifyLegacyPass() : FunctionPass(ID
) {
79 initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
82 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
84 AU
.addRequired
<DominatorTreeWrapperPass
>();
85 AU
.addRequired
<AssumptionCacheTracker
>();
86 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
87 AU
.addRequired
<OptimizationRemarkEmitterWrapperPass
>();
90 /// runOnFunction - Remove instructions that simplify.
91 bool runOnFunction(Function
&F
) override
{
95 const DominatorTree
*DT
=
96 &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
97 const TargetLibraryInfo
*TLI
=
98 &getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI();
100 &getAnalysis
<AssumptionCacheTracker
>().getAssumptionCache(F
);
101 OptimizationRemarkEmitter
*ORE
=
102 &getAnalysis
<OptimizationRemarkEmitterWrapperPass
>().getORE();
103 const DataLayout
&DL
= F
.getParent()->getDataLayout();
104 const SimplifyQuery
SQ(DL
, TLI
, DT
, AC
);
105 return runImpl(F
, SQ
, ORE
);
110 char InstSimplifyLegacyPass::ID
= 0;
111 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass
, "instsimplify",
112 "Remove redundant instructions", false, false)
113 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker
)
114 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
115 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
116 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass
)
117 INITIALIZE_PASS_END(InstSimplifyLegacyPass
, "instsimplify",
118 "Remove redundant instructions", false, false)
120 // Public interface to the simplify instructions pass.
121 FunctionPass
*llvm::createInstSimplifyLegacyPass() {
122 return new InstSimplifyLegacyPass();
125 PreservedAnalyses
InstSimplifyPass::run(Function
&F
,
126 FunctionAnalysisManager
&AM
) {
127 auto &DT
= AM
.getResult
<DominatorTreeAnalysis
>(F
);
128 auto &TLI
= AM
.getResult
<TargetLibraryAnalysis
>(F
);
129 auto &AC
= AM
.getResult
<AssumptionAnalysis
>(F
);
130 auto &ORE
= AM
.getResult
<OptimizationRemarkEmitterAnalysis
>(F
);
131 const DataLayout
&DL
= F
.getParent()->getDataLayout();
132 const SimplifyQuery
SQ(DL
, &TLI
, &DT
, &AC
);
133 bool Changed
= runImpl(F
, SQ
, &ORE
);
135 return PreservedAnalyses::all();
137 PreservedAnalyses PA
;
138 PA
.preserveSet
<CFGAnalyses
>();