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 // Unreachable code can take on strange forms that we are not prepared to
38 // handle. For example, an instruction may have itself as an operand.
39 if (!SQ
.DT
->isReachableFromEntry(&BB
))
42 SmallVector
<Instruction
*, 8> DeadInstsInBB
;
43 for (Instruction
&I
: BB
) {
44 // The first time through the loop, ToSimplify is empty and we try to
45 // simplify all instructions. On later iterations, ToSimplify is not
46 // empty and we only bother simplifying instructions that are in it.
47 if (!ToSimplify
->empty() && !ToSimplify
->count(&I
))
50 // Don't waste time simplifying dead/unused instructions.
51 if (isInstructionTriviallyDead(&I
)) {
52 DeadInstsInBB
.push_back(&I
);
54 } else if (!I
.use_empty()) {
55 if (Value
*V
= SimplifyInstruction(&I
, SQ
, ORE
)) {
56 // Mark all uses for resimplification next time round the loop.
57 for (User
*U
: I
.users())
58 Next
->insert(cast
<Instruction
>(U
));
59 I
.replaceAllUsesWith(V
);
62 // A call can get simplified, but it may not be trivially dead.
63 if (isInstructionTriviallyDead(&I
))
64 DeadInstsInBB
.push_back(&I
);
68 RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB
, SQ
.TLI
);
71 // Place the list of instructions to simplify on the next loop iteration
73 std::swap(ToSimplify
, Next
);
75 } while (!ToSimplify
->empty());
81 struct InstSimplifyLegacyPass
: public FunctionPass
{
82 static char ID
; // Pass identification, replacement for typeid
83 InstSimplifyLegacyPass() : FunctionPass(ID
) {
84 initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
87 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
89 AU
.addRequired
<DominatorTreeWrapperPass
>();
90 AU
.addRequired
<AssumptionCacheTracker
>();
91 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
92 AU
.addRequired
<OptimizationRemarkEmitterWrapperPass
>();
95 /// Remove instructions that simplify.
96 bool runOnFunction(Function
&F
) override
{
100 const DominatorTree
*DT
=
101 &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
102 const TargetLibraryInfo
*TLI
=
103 &getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI();
104 AssumptionCache
*AC
=
105 &getAnalysis
<AssumptionCacheTracker
>().getAssumptionCache(F
);
106 OptimizationRemarkEmitter
*ORE
=
107 &getAnalysis
<OptimizationRemarkEmitterWrapperPass
>().getORE();
108 const DataLayout
&DL
= F
.getParent()->getDataLayout();
109 const SimplifyQuery
SQ(DL
, TLI
, DT
, AC
);
110 return runImpl(F
, SQ
, ORE
);
115 char InstSimplifyLegacyPass::ID
= 0;
116 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass
, "instsimplify",
117 "Remove redundant instructions", false, false)
118 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker
)
119 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
120 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
121 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass
)
122 INITIALIZE_PASS_END(InstSimplifyLegacyPass
, "instsimplify",
123 "Remove redundant instructions", false, false)
125 // Public interface to the simplify instructions pass.
126 FunctionPass
*llvm::createInstSimplifyLegacyPass() {
127 return new InstSimplifyLegacyPass();
130 PreservedAnalyses
InstSimplifyPass::run(Function
&F
,
131 FunctionAnalysisManager
&AM
) {
132 auto &DT
= AM
.getResult
<DominatorTreeAnalysis
>(F
);
133 auto &TLI
= AM
.getResult
<TargetLibraryAnalysis
>(F
);
134 auto &AC
= AM
.getResult
<AssumptionAnalysis
>(F
);
135 auto &ORE
= AM
.getResult
<OptimizationRemarkEmitterAnalysis
>(F
);
136 const DataLayout
&DL
= F
.getParent()->getDataLayout();
137 const SimplifyQuery
SQ(DL
, &TLI
, &DT
, &AC
);
138 bool Changed
= runImpl(F
, SQ
, &ORE
);
140 return PreservedAnalyses::all();
142 PreservedAnalyses PA
;
143 PA
.preserveSet
<CFGAnalyses
>();