LLVM  14.0.0git
ConstantHoisting.h
Go to the documentation of this file.
1 //==- ConstantHoisting.h - Prepare code for expensive constants --*- C++ -*-==//
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 // This pass identifies expensive constants to hoist and coalesces them to
10 // better prepare it for SelectionDAG-based code generation. This works around
11 // the limitations of the basic-block-at-a-time approach.
12 //
13 // First it scans all instructions for integer constants and calculates its
14 // cost. If the constant can be folded into the instruction (the cost is
15 // TCC_Free) or the cost is just a simple operation (TCC_BASIC), then we don't
16 // consider it expensive and leave it alone. This is the default behavior and
17 // the default implementation of getIntImmCostInst will always return TCC_Free.
18 //
19 // If the cost is more than TCC_BASIC, then the integer constant can't be folded
20 // into the instruction and it might be beneficial to hoist the constant.
21 // Similar constants are coalesced to reduce register pressure and
22 // materialization code.
23 //
24 // When a constant is hoisted, it is also hidden behind a bitcast to force it to
25 // be live-out of the basic block. Otherwise the constant would be just
26 // duplicated and each basic block would have its own copy in the SelectionDAG.
27 // The SelectionDAG recognizes such constants as opaque and doesn't perform
28 // certain transformations on them, which would create a new expensive constant.
29 //
30 // This optimization is only applied to integer constants in instructions and
31 // simple (this means not nested) constant cast expressions. For example:
32 // %0 = load i64* inttoptr (i64 big_constant to i64*)
33 //
34 //===----------------------------------------------------------------------===//
35 
36 #ifndef LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
37 #define LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
38 
39 #include "llvm/ADT/DenseMap.h"
40 #include "llvm/ADT/MapVector.h"
41 #include "llvm/ADT/PointerUnion.h"
42 #include "llvm/ADT/SetVector.h"
43 #include "llvm/ADT/SmallPtrSet.h"
44 #include "llvm/ADT/SmallVector.h"
45 #include "llvm/IR/PassManager.h"
46 #include <algorithm>
47 #include <vector>
48 
49 namespace llvm {
50 
51 class BasicBlock;
52 class BlockFrequencyInfo;
53 class Constant;
54 class ConstantInt;
55 class ConstantExpr;
56 class DominatorTree;
57 class Function;
58 class GlobalVariable;
59 class Instruction;
60 class ProfileSummaryInfo;
61 class TargetTransformInfo;
62 
63 /// A private "module" namespace for types and utilities used by
64 /// ConstantHoisting. These are implementation details and should not be used by
65 /// clients.
66 namespace consthoist {
67 
68 /// Keeps track of the user of a constant and the operand index where the
69 /// constant is used.
70 struct ConstantUser {
72  unsigned OpndIdx;
73 
74  ConstantUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) {}
75 };
76 
78 
79 /// Keeps track of a constant candidate and its uses.
82  // If the candidate is a ConstantExpr (currely only constant GEP expressions
83  // whose base pointers are GlobalVariables are supported), ConstInt records
84  // its offset from the base GV, ConstExpr tracks the candidate GEP expr.
87  unsigned CumulativeCost = 0;
88 
91 
92  /// Add the user to the use list and update the cost.
93  void addUser(Instruction *Inst, unsigned Idx, unsigned Cost) {
94  CumulativeCost += Cost;
95  Uses.push_back(ConstantUser(Inst, Idx));
96  }
97 };
98 
99 /// This represents a constant that has been rebased with respect to a
100 /// base constant. The difference to the base constant is recorded in Offset.
105 
107  Type *Ty=nullptr) : Uses(std::move(Uses)), Offset(Offset), Ty(Ty) {}
108 };
109 
111 
112 /// A base constant and all its rebased constants.
113 struct ConstantInfo {
114  // If the candidate is a ConstantExpr (currely only constant GEP expressions
115  // whose base pointers are GlobalVariables are supported), ConstInt records
116  // its offset from the base GV, ConstExpr tracks the candidate GEP expr.
120 };
121 
122 } // end namespace consthoist
123 
124 class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
125 public:
127 
128  // Glue for old PM.
130  BlockFrequencyInfo *BFI, BasicBlock &Entry,
131  ProfileSummaryInfo *PSI);
132 
133  void cleanup() {
134  ClonedCastMap.clear();
135  ConstIntCandVec.clear();
136  for (auto MapEntry : ConstGEPCandMap)
137  MapEntry.second.clear();
138  ConstGEPCandMap.clear();
139  ConstIntInfoVec.clear();
140  for (auto MapEntry : ConstGEPInfoMap)
141  MapEntry.second.clear();
142  ConstGEPInfoMap.clear();
143  }
144 
145 private:
146  using ConstPtrUnionType = PointerUnion<ConstantInt *, ConstantExpr *>;
147  using ConstCandMapType = DenseMap<ConstPtrUnionType, unsigned>;
148 
149  const TargetTransformInfo *TTI;
150  DominatorTree *DT;
151  BlockFrequencyInfo *BFI;
152  LLVMContext *Ctx;
153  const DataLayout *DL;
154  BasicBlock *Entry;
155  ProfileSummaryInfo *PSI;
156 
157  /// Keeps track of constant candidates found in the function.
158  using ConstCandVecType = std::vector<consthoist::ConstantCandidate>;
159  using GVCandVecMapType = MapVector<GlobalVariable *, ConstCandVecType>;
160  ConstCandVecType ConstIntCandVec;
161  GVCandVecMapType ConstGEPCandMap;
162 
163  /// These are the final constants we decided to hoist.
164  using ConstInfoVecType = SmallVector<consthoist::ConstantInfo, 8>;
165  using GVInfoVecMapType = MapVector<GlobalVariable *, ConstInfoVecType>;
166  ConstInfoVecType ConstIntInfoVec;
167  GVInfoVecMapType ConstGEPInfoMap;
168 
169  /// Keep track of cast instructions we already cloned.
171 
172  Instruction *findMatInsertPt(Instruction *Inst, unsigned Idx = ~0U) const;
174  findConstantInsertionPoint(const consthoist::ConstantInfo &ConstInfo) const;
175  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
176  Instruction *Inst, unsigned Idx,
177  ConstantInt *ConstInt);
178  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
179  Instruction *Inst, unsigned Idx,
180  ConstantExpr *ConstExpr);
181  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
182  Instruction *Inst, unsigned Idx);
183  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
184  Instruction *Inst);
185  void collectConstantCandidates(Function &Fn);
186  void findAndMakeBaseConstant(ConstCandVecType::iterator S,
187  ConstCandVecType::iterator E,
189  unsigned maximizeConstantsInRange(ConstCandVecType::iterator S,
190  ConstCandVecType::iterator E,
191  ConstCandVecType::iterator &MaxCostItr);
192  // If BaseGV is nullptr, find base among Constant Integer candidates;
193  // otherwise find base among constant GEPs sharing BaseGV as base pointer.
194  void findBaseConstants(GlobalVariable *BaseGV);
195  void emitBaseConstants(Instruction *Base, Constant *Offset, Type *Ty,
196  const consthoist::ConstantUser &ConstUser);
197  // If BaseGV is nullptr, emit Constant Integer base; otherwise emit
198  // constant GEP base.
199  bool emitBaseConstants(GlobalVariable *BaseGV);
200  void deleteDeadCastInst() const;
201 };
202 
203 } // end namespace llvm
204 
205 #endif // LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
llvm::consthoist::ConstantUser::ConstantUser
ConstantUser(Instruction *Inst, unsigned Idx)
Definition: ConstantHoisting.h:74
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::consthoist::ConstantCandidate
Keeps track of a constant candidate and its uses.
Definition: ConstantHoisting.h:80
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:61
llvm::consthoist::ConstantInfo::RebasedConstants
RebasedConstantListType RebasedConstants
Definition: ConstantHoisting.h:119
llvm::consthoist::RebasedConstantInfo::Offset
Constant * Offset
Definition: ConstantHoisting.h:103
llvm::SmallVector< ConstantUser, 8 >
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:168
MapVector.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DenseMap.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::MapVector< GlobalVariable *, ConstCandVecType >
consthoist
consthoist
Definition: ConstantHoisting.cpp:135
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::ConstantHoistingPass::runImpl
bool runImpl(Function &F, TargetTransformInfo &TTI, DominatorTree &DT, BlockFrequencyInfo *BFI, BasicBlock &Entry, ProfileSummaryInfo *PSI)
Optimize expensive integer constants in the given function.
Definition: ConstantHoisting.cpp:944
llvm::consthoist::ConstantUser::OpndIdx
unsigned OpndIdx
Definition: ConstantHoisting.h:72
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ConstantHoistingPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: ConstantHoisting.cpp:983
llvm::consthoist::ConstantCandidate::CumulativeCost
unsigned CumulativeCost
Definition: ConstantHoisting.h:87
llvm::Instruction
Definition: Instruction.h:45
llvm::consthoist::ConstantCandidate::ConstExpr
ConstantExpr * ConstExpr
Definition: ConstantHoisting.h:86
SmallPtrSet.h
llvm::consthoist::ConstantCandidate::addUser
void addUser(Instruction *Inst, unsigned Idx, unsigned Cost)
Add the user to the use list and update the cost.
Definition: ConstantHoisting.h:93
llvm::consthoist::ConstantInfo::BaseExpr
ConstantExpr * BaseExpr
Definition: ConstantHoisting.h:118
llvm::consthoist::ConstantUser
Keeps track of the user of a constant and the operand index where the constant is used.
Definition: ConstantHoisting.h:70
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
llvm::consthoist::ConstantInfo::BaseInt
ConstantInt * BaseInt
Definition: ConstantHoisting.h:117
llvm::consthoist::RebasedConstantInfo::RebasedConstantInfo
RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset, Type *Ty=nullptr)
Definition: ConstantHoisting.h:106
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DenseMap
Definition: DenseMap.h:714
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1609
llvm::ConstantHoistingPass::cleanup
void cleanup()
Definition: ConstantHoisting.h:133
PointerUnion.h
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::consthoist::ConstantCandidate::Uses
ConstantUseListType Uses
Definition: ConstantHoisting.h:81
llvm::consthoist::RebasedConstantInfo::Ty
Type * Ty
Definition: ConstantHoisting.h:104
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::consthoist::ConstantInfo
A base constant and all its rebased constants.
Definition: ConstantHoisting.h:113
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:119
llvm::consthoist::RebasedConstantInfo::Uses
ConstantUseListType Uses
Definition: ConstantHoisting.h:102
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:936
std
Definition: BitVector.h:838
llvm::consthoist::ConstantCandidate::ConstInt
ConstantInt * ConstInt
Definition: ConstantHoisting.h:85
PassManager.h
llvm::consthoist::ConstantCandidate::ConstantCandidate
ConstantCandidate(ConstantInt *ConstInt, ConstantExpr *ConstExpr=nullptr)
Definition: ConstantHoisting.h:89
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:585
llvm::consthoist::ConstantUser::Inst
Instruction * Inst
Definition: ConstantHoisting.h:71
SmallVector.h
llvm::consthoist::RebasedConstantInfo
This represents a constant that has been rebased with respect to a base constant.
Definition: ConstantHoisting.h:101
llvm::SmallVectorImpl< consthoist::ConstantInfo >
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::SetVector
A vector that has set insertion semantics.
Definition: SetVector.h:40
llvm::ConstantHoistingPass
Definition: ConstantHoisting.h:124
llvm::codeview::PublicSymFlags::Function
@ Function
SetVector.h
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58