1 //===- GVN.h - Eliminate redundant values and loads -------------*- C++ -*-===//
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 /// This file provides the interface for LLVM's Global Value Numbering pass
10 /// which eliminates fully redundant instructions. It also does somewhat Ad-Hoc
11 /// PRE and dead load elimination.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_SCALAR_GVN_H
16 #define LLVM_TRANSFORMS_SCALAR_GVN_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/PostOrderIterator.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/InstructionPrecedenceTracking.h"
25 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/PassManager.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/Allocator.h"
31 #include "llvm/Support/Compiler.h"
38 class AssumptionCache
;
43 class ExtractValueInst
;
49 class OptimizationRemarkEmitter
;
51 class TargetLibraryInfo
;
54 /// A private "module" namespace for types and utilities used by GVN. These
55 /// are implementation details and should not be used by clients.
56 namespace gvn LLVM_LIBRARY_VISIBILITY
{
58 struct AvailableValue
;
59 struct AvailableValueInBlock
;
62 } // end namespace gvn
64 /// The core GVN pass object.
66 /// FIXME: We should have a good summary of the GVN algorithm implemented by
67 /// this particular pass here.
68 class GVN
: public PassInfoMixin
<GVN
> {
72 /// Run the pass over the function.
73 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
75 /// This removes the specified instruction from
76 /// our various maps and marks it for deletion.
77 void markInstructionForDeletion(Instruction
*I
) {
79 InstrsToErase
.push_back(I
);
82 DominatorTree
&getDominatorTree() const { return *DT
; }
83 AliasAnalysis
*getAliasAnalysis() const { return VN
.getAliasAnalysis(); }
84 MemoryDependenceResults
&getMemDep() const { return *MD
; }
86 /// This class holds the mapping between values and value numbers. It is used
87 /// as an efficient mechanism to determine the expression-wise equivalence of
90 DenseMap
<Value
*, uint32_t> valueNumbering
;
91 DenseMap
<Expression
, uint32_t> expressionNumbering
;
93 // Expressions is the vector of Expression. ExprIdx is the mapping from
94 // value number to the index of Expression in Expressions. We use it
95 // instead of a DenseMap because filling such mapping is faster than
96 // filling a DenseMap and the compile time is a little better.
97 uint32_t nextExprNumber
;
99 std::vector
<Expression
> Expressions
;
100 std::vector
<uint32_t> ExprIdx
;
102 // Value number to PHINode mapping. Used for phi-translate in scalarpre.
103 DenseMap
<uint32_t, PHINode
*> NumberingPhi
;
105 // Cache for phi-translate in scalarpre.
106 using PhiTranslateMap
=
107 DenseMap
<std::pair
<uint32_t, const BasicBlock
*>, uint32_t>;
108 PhiTranslateMap PhiTranslateTable
;
111 MemoryDependenceResults
*MD
;
114 uint32_t nextValueNumber
= 1;
116 Expression
createExpr(Instruction
*I
);
117 Expression
createCmpExpr(unsigned Opcode
, CmpInst::Predicate Predicate
,
118 Value
*LHS
, Value
*RHS
);
119 Expression
createExtractvalueExpr(ExtractValueInst
*EI
);
120 uint32_t lookupOrAddCall(CallInst
*C
);
121 uint32_t phiTranslateImpl(const BasicBlock
*BB
, const BasicBlock
*PhiBlock
,
122 uint32_t Num
, GVN
&Gvn
);
123 std::pair
<uint32_t, bool> assignExpNewValueNum(Expression
&exp
);
124 bool areAllValsInBB(uint32_t num
, const BasicBlock
*BB
, GVN
&Gvn
);
128 ValueTable(const ValueTable
&Arg
);
129 ValueTable(ValueTable
&&Arg
);
132 uint32_t lookupOrAdd(Value
*V
);
133 uint32_t lookup(Value
*V
, bool Verify
= true) const;
134 uint32_t lookupOrAddCmp(unsigned Opcode
, CmpInst::Predicate Pred
,
135 Value
*LHS
, Value
*RHS
);
136 uint32_t phiTranslate(const BasicBlock
*BB
, const BasicBlock
*PhiBlock
,
137 uint32_t Num
, GVN
&Gvn
);
138 void eraseTranslateCacheEntry(uint32_t Num
, const BasicBlock
&CurrBlock
);
139 bool exists(Value
*V
) const;
140 void add(Value
*V
, uint32_t num
);
142 void erase(Value
*v
);
143 void setAliasAnalysis(AliasAnalysis
*A
) { AA
= A
; }
144 AliasAnalysis
*getAliasAnalysis() const { return AA
; }
145 void setMemDep(MemoryDependenceResults
*M
) { MD
= M
; }
146 void setDomTree(DominatorTree
*D
) { DT
= D
; }
147 uint32_t getNextUnusedValueNumber() { return nextValueNumber
; }
148 void verifyRemoved(const Value
*) const;
152 friend class gvn::GVNLegacyPass
;
153 friend struct DenseMapInfo
<Expression
>;
155 MemoryDependenceResults
*MD
;
157 const TargetLibraryInfo
*TLI
;
159 SetVector
<BasicBlock
*> DeadBlocks
;
160 OptimizationRemarkEmitter
*ORE
;
161 ImplicitControlFlowTracking
*ICF
;
165 /// A mapping from value numbers to lists of Value*'s that
166 /// have that value number. Use findLeader to query it.
167 struct LeaderTableEntry
{
169 const BasicBlock
*BB
;
170 LeaderTableEntry
*Next
;
172 DenseMap
<uint32_t, LeaderTableEntry
> LeaderTable
;
173 BumpPtrAllocator TableAllocator
;
175 // Block-local map of equivalent values to their leader, does not
176 // propagate to any successors. Entries added mid-block are applied
177 // to the remaining instructions in the block.
178 SmallMapVector
<Value
*, Constant
*, 4> ReplaceWithConstMap
;
179 SmallVector
<Instruction
*, 8> InstrsToErase
;
181 // Map the block to reversed postorder traversal number. It is used to
182 // find back edge easily.
183 DenseMap
<AssertingVH
<BasicBlock
>, uint32_t> BlockRPONumber
;
185 // This is set 'true' initially and also when new blocks have been added to
186 // the function being analyzed. This boolean is used to control the updating
187 // of BlockRPONumber prior to accessing the contents of BlockRPONumber.
188 bool InvalidBlockRPONumbers
= true;
190 using LoadDepVect
= SmallVector
<NonLocalDepResult
, 64>;
191 using AvailValInBlkVect
= SmallVector
<gvn::AvailableValueInBlock
, 64>;
192 using UnavailBlkVect
= SmallVector
<BasicBlock
*, 64>;
194 bool runImpl(Function
&F
, AssumptionCache
&RunAC
, DominatorTree
&RunDT
,
195 const TargetLibraryInfo
&RunTLI
, AAResults
&RunAA
,
196 MemoryDependenceResults
*RunMD
, LoopInfo
*LI
,
197 OptimizationRemarkEmitter
*ORE
);
199 /// Push a new Value to the LeaderTable onto the list for its value number.
200 void addToLeaderTable(uint32_t N
, Value
*V
, const BasicBlock
*BB
) {
201 LeaderTableEntry
&Curr
= LeaderTable
[N
];
208 LeaderTableEntry
*Node
= TableAllocator
.Allocate
<LeaderTableEntry
>();
211 Node
->Next
= Curr
.Next
;
215 /// Scan the list of values corresponding to a given
216 /// value number, and remove the given instruction if encountered.
217 void removeFromLeaderTable(uint32_t N
, Instruction
*I
, BasicBlock
*BB
) {
218 LeaderTableEntry
*Prev
= nullptr;
219 LeaderTableEntry
*Curr
= &LeaderTable
[N
];
221 while (Curr
&& (Curr
->Val
!= I
|| Curr
->BB
!= BB
)) {
230 Prev
->Next
= Curr
->Next
;
236 LeaderTableEntry
*Next
= Curr
->Next
;
237 Curr
->Val
= Next
->Val
;
239 Curr
->Next
= Next
->Next
;
244 // List of critical edges to be split between iterations.
245 SmallVector
<std::pair
<Instruction
*, unsigned>, 4> toSplit
;
247 // Helper functions of redundant load elimination
248 bool processLoad(LoadInst
*L
);
249 bool processNonLocalLoad(LoadInst
*L
);
250 bool processAssumeIntrinsic(IntrinsicInst
*II
);
252 /// Given a local dependency (Def or Clobber) determine if a value is
253 /// available for the load. Returns true if an value is known to be
254 /// available and populates Res. Returns false otherwise.
255 bool AnalyzeLoadAvailability(LoadInst
*LI
, MemDepResult DepInfo
,
256 Value
*Address
, gvn::AvailableValue
&Res
);
258 /// Given a list of non-local dependencies, determine if a value is
259 /// available for the load in each specified block. If it is, add it to
260 /// ValuesPerBlock. If not, add it to UnavailableBlocks.
261 void AnalyzeLoadAvailability(LoadInst
*LI
, LoadDepVect
&Deps
,
262 AvailValInBlkVect
&ValuesPerBlock
,
263 UnavailBlkVect
&UnavailableBlocks
);
265 bool PerformLoadPRE(LoadInst
*LI
, AvailValInBlkVect
&ValuesPerBlock
,
266 UnavailBlkVect
&UnavailableBlocks
);
268 // Other helper routines
269 bool processInstruction(Instruction
*I
);
270 bool processBlock(BasicBlock
*BB
);
271 void dump(DenseMap
<uint32_t, Value
*> &d
) const;
272 bool iterateOnFunction(Function
&F
);
273 bool performPRE(Function
&F
);
274 bool performScalarPRE(Instruction
*I
);
275 bool performScalarPREInsertion(Instruction
*Instr
, BasicBlock
*Pred
,
276 BasicBlock
*Curr
, unsigned int ValNo
);
277 Value
*findLeader(const BasicBlock
*BB
, uint32_t num
);
278 void cleanupGlobalSets();
279 void fillImplicitControlFlowInfo(BasicBlock
*BB
);
280 void verifyRemoved(const Instruction
*I
) const;
281 bool splitCriticalEdges();
282 BasicBlock
*splitCriticalEdges(BasicBlock
*Pred
, BasicBlock
*Succ
);
283 bool replaceOperandsWithConsts(Instruction
*I
) const;
284 bool propagateEquality(Value
*LHS
, Value
*RHS
, const BasicBlockEdge
&Root
,
285 bool DominatesByEdge
);
286 bool processFoldableCondBr(BranchInst
*BI
);
287 void addDeadBlock(BasicBlock
*BB
);
288 void assignValNumForDeadCode();
289 void assignBlockRPONumber(Function
&F
);
292 /// Create a legacy GVN pass. This also allows parameterizing whether or not
293 /// loads are eliminated by the pass.
294 FunctionPass
*createGVNPass(bool NoLoads
= false);
296 /// A simple and fast domtree-based GVN pass to hoist common expressions
297 /// from sibling branches.
298 struct GVNHoistPass
: PassInfoMixin
<GVNHoistPass
> {
299 /// Run the pass over the function.
300 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
303 /// Uses an "inverted" value numbering to decide the similarity of
304 /// expressions and sinks similar expressions into successors.
305 struct GVNSinkPass
: PassInfoMixin
<GVNSinkPass
> {
306 /// Run the pass over the function.
307 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
310 } // end namespace llvm
312 #endif // LLVM_TRANSFORMS_SCALAR_GVN_H