LLVM 19.0.0git
Go to the documentation of this file.
1//==- ConstantHoisting.h - Prepare code for expensive constants --*- 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
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.
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.
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.
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.
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*)
39#include "llvm/ADT/ArrayRef.h"
40#include "llvm/ADT/DenseMap.h"
41#include "llvm/ADT/MapVector.h"
43#include "llvm/ADT/SetVector.h"
45#include "llvm/IR/PassManager.h"
46#include <algorithm>
47#include <vector>
49namespace llvm {
51class BasicBlock;
52class BlockFrequencyInfo;
53class Constant;
54class ConstantInt;
55class ConstantExpr;
56class DominatorTree;
57class Function;
58class GlobalVariable;
59class Instruction;
60class ProfileSummaryInfo;
61class TargetTransformInfo;
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.
66namespace consthoist {
68/// Keeps track of the user of a constant and the operand index where the
69/// constant is used.
72 unsigned OpndIdx;
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;
92 /// Add the user to the use list and update the cost.
93 void addUser(Instruction *Inst, unsigned Idx, unsigned Cost) {
96 }
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.
107 Type *Ty=nullptr) : Uses(std::move(Uses)), Offset(Offset), Ty(Ty) {}
112/// A base constant and all its rebased constants.
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.
122} // end namespace consthoist
124class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
128 // Glue for old PM.
130 BlockFrequencyInfo *BFI, BasicBlock &Entry,
131 ProfileSummaryInfo *PSI);
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 }
146 using ConstPtrUnionType = PointerUnion<ConstantInt *, ConstantExpr *>;
147 using ConstCandMapType = DenseMap<ConstPtrUnionType, unsigned>;
150 DominatorTree *DT;
152 LLVMContext *Ctx;
153 const DataLayout *DL;
154 BasicBlock *Entry;
156 bool OptForSize;
158 /// Keeps track of constant candidates found in the function.
159 using ConstCandVecType = std::vector<consthoist::ConstantCandidate>;
160 using GVCandVecMapType = MapVector<GlobalVariable *, ConstCandVecType>;
161 ConstCandVecType ConstIntCandVec;
162 GVCandVecMapType ConstGEPCandMap;
164 /// These are the final constants we decided to hoist.
165 using ConstInfoVecType = SmallVector<consthoist::ConstantInfo, 8>;
166 using GVInfoVecMapType = MapVector<GlobalVariable *, ConstInfoVecType>;
167 ConstInfoVecType ConstIntInfoVec;
168 GVInfoVecMapType ConstGEPInfoMap;
170 /// Keep track of cast instructions we already cloned.
173 void collectMatInsertPts(
174 const consthoist::RebasedConstantListType &RebasedConstants,
175 SmallVectorImpl<BasicBlock::iterator> &MatInsertPts) const;
176 BasicBlock::iterator findMatInsertPt(Instruction *Inst,
177 unsigned Idx = ~0U) const;
178 SetVector<BasicBlock::iterator> findConstantInsertionPoint(
179 const consthoist::ConstantInfo &ConstInfo,
180 const ArrayRef<BasicBlock::iterator> MatInsertPts) const;
181 void collectConstantCandidates(ConstCandMapType &ConstCandMap,
182 Instruction *Inst, unsigned Idx,
183 ConstantInt *ConstInt);
184 void collectConstantCandidates(ConstCandMapType &ConstCandMap,
185 Instruction *Inst, unsigned Idx,
186 ConstantExpr *ConstExpr);
187 void collectConstantCandidates(ConstCandMapType &ConstCandMap,
188 Instruction *Inst, unsigned Idx);
189 void collectConstantCandidates(ConstCandMapType &ConstCandMap,
190 Instruction *Inst);
191 void collectConstantCandidates(Function &Fn);
192 void findAndMakeBaseConstant(ConstCandVecType::iterator S,
193 ConstCandVecType::iterator E,
195 unsigned maximizeConstantsInRange(ConstCandVecType::iterator S,
196 ConstCandVecType::iterator E,
197 ConstCandVecType::iterator &MaxCostItr);
198 // If BaseGV is nullptr, find base among Constant Integer candidates;
199 // otherwise find base among constant GEPs sharing BaseGV as base pointer.
200 void findBaseConstants(GlobalVariable *BaseGV);
202 /// A ConstantUser grouped with the Type and Constant adjustment. The user
203 /// will be adjusted by Offset.
204 struct UserAdjustment {
205 Constant *Offset;
206 Type *Ty;
207 BasicBlock::iterator MatInsertPt;
209 UserAdjustment(Constant *O, Type *T, BasicBlock::iterator I,
211 : Offset(O), Ty(T), MatInsertPt(I), User(U) {}
212 };
213 void emitBaseConstants(Instruction *Base, UserAdjustment *Adj);
214 // If BaseGV is nullptr, emit Constant Integer base; otherwise emit
215 // constant GEP base.
216 bool emitBaseConstants(GlobalVariable *BaseGV);
217 void deleteDeadCastInst() const;
220} // end namespace llvm
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
This header defines various interfaces for pass management in LLVM.
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:165
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1084
bool runImpl(Function &F, TargetTransformInfo &TTI, DominatorTree &DT, BlockFrequencyInfo *BFI, BasicBlock &Entry, ProfileSummaryInfo *PSI)
Optimize expensive integer constants in the given function.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
Analysis providing profile information.
A vector that has set insertion semantics.
Definition: SetVector.h:57
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1849
InstructionCost Cost
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:74
Keeps track of a constant candidate and its uses.
void addUser(Instruction *Inst, unsigned Idx, unsigned Cost)
Add the user to the use list and update the cost.
ConstantCandidate(ConstantInt *ConstInt, ConstantExpr *ConstExpr=nullptr)
A base constant and all its rebased constants.
RebasedConstantListType RebasedConstants
Keeps track of the user of a constant and the operand index where the constant is used.
ConstantUser(Instruction *Inst, unsigned Idx)
This represents a constant that has been rebased with respect to a base constant.
RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset, Type *Ty=nullptr)