LLVM  9.0.0svn
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 getIntImmCost 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/PointerUnion.h"
41 #include "llvm/ADT/SmallPtrSet.h"
42 #include "llvm/ADT/SmallVector.h"
43 #include "llvm/IR/PassManager.h"
44 #include <algorithm>
45 #include <vector>
46 
47 namespace llvm {
48 
49 class BasicBlock;
50 class BlockFrequencyInfo;
51 class Constant;
52 class ConstantInt;
53 class ConstantExpr;
54 class DominatorTree;
55 class Function;
56 class GlobalVariable;
57 class Instruction;
58 class TargetTransformInfo;
59 
60 /// A private "module" namespace for types and utilities used by
61 /// ConstantHoisting. These are implementation details and should not be used by
62 /// clients.
63 namespace consthoist {
64 
65 /// Keeps track of the user of a constant and the operand index where the
66 /// constant is used.
67 struct ConstantUser {
69  unsigned OpndIdx;
70 
71  ConstantUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) {}
72 };
73 
75 
76 /// Keeps track of a constant candidate and its uses.
79  // If the candidate is a ConstantExpr (currely only constant GEP expressions
80  // whose base pointers are GlobalVariables are supported), ConstInt records
81  // its offset from the base GV, ConstExpr tracks the candidate GEP expr.
84  unsigned CumulativeCost = 0;
85 
86  ConstantCandidate(ConstantInt *ConstInt, ConstantExpr *ConstExpr=nullptr) :
87  ConstInt(ConstInt), ConstExpr(ConstExpr) {}
88 
89  /// Add the user to the use list and update the cost.
90  void addUser(Instruction *Inst, unsigned Idx, unsigned Cost) {
91  CumulativeCost += Cost;
92  Uses.push_back(ConstantUser(Inst, Idx));
93  }
94 };
95 
96 /// This represents a constant that has been rebased with respect to a
97 /// base constant. The difference to the base constant is recorded in Offset.
102 
104  Type *Ty=nullptr) : Uses(std::move(Uses)), Offset(Offset), Ty(Ty) {}
105 };
106 
108 
109 /// A base constant and all its rebased constants.
110 struct ConstantInfo {
111  // If the candidate is a ConstantExpr (currely only constant GEP expressions
112  // whose base pointers are GlobalVariables are supported), ConstInt records
113  // its offset from the base GV, ConstExpr tracks the candidate GEP expr.
117 };
118 
119 } // end namespace consthoist
120 
121 class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
122 public:
124 
125  // Glue for old PM.
128 
129  void releaseMemory() {
130  ClonedCastMap.clear();
131  ConstIntCandVec.clear();
132  for (auto MapEntry : ConstGEPCandMap)
133  MapEntry.second.clear();
134  ConstGEPCandMap.clear();
135  ConstIntInfoVec.clear();
136  for (auto MapEntry : ConstGEPInfoMap)
137  MapEntry.second.clear();
138  ConstGEPInfoMap.clear();
139  }
140 
141 private:
144 
145  const TargetTransformInfo *TTI;
146  DominatorTree *DT;
148  LLVMContext *Ctx;
149  const DataLayout *DL;
150  BasicBlock *Entry;
151 
152  /// Keeps track of constant candidates found in the function.
153  using ConstCandVecType = std::vector<consthoist::ConstantCandidate>;
155  ConstCandVecType ConstIntCandVec;
156  GVCandVecMapType ConstGEPCandMap;
157 
158  /// These are the final constants we decided to hoist.
161  ConstInfoVecType ConstIntInfoVec;
162  GVInfoVecMapType ConstGEPInfoMap;
163 
164  /// Keep track of cast instructions we already cloned.
166 
167  Instruction *findMatInsertPt(Instruction *Inst, unsigned Idx = ~0U) const;
169  findConstantInsertionPoint(const consthoist::ConstantInfo &ConstInfo) const;
170  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
171  Instruction *Inst, unsigned Idx,
172  ConstantInt *ConstInt);
173  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
174  Instruction *Inst, unsigned Idx,
175  ConstantExpr *ConstExpr);
176  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
177  Instruction *Inst, unsigned Idx);
178  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
179  Instruction *Inst);
180  void collectConstantCandidates(Function &Fn);
181  void findAndMakeBaseConstant(ConstCandVecType::iterator S,
182  ConstCandVecType::iterator E,
184  unsigned maximizeConstantsInRange(ConstCandVecType::iterator S,
185  ConstCandVecType::iterator E,
186  ConstCandVecType::iterator &MaxCostItr);
187  // If BaseGV is nullptr, find base among Constant Integer candidates;
188  // otherwise find base among constant GEPs sharing BaseGV as base pointer.
189  void findBaseConstants(GlobalVariable *BaseGV);
190  void emitBaseConstants(Instruction *Base, Constant *Offset, Type *Ty,
191  const consthoist::ConstantUser &ConstUser);
192  // If BaseGV is nullptr, emit Constant Integer base; otherwise emit
193  // constant GEP base.
194  bool emitBaseConstants(GlobalVariable *BaseGV);
195  void deleteDeadCastInst() const;
196  bool optimizeConstants(Function &Fn);
197 };
198 
199 } // end namespace llvm
200 
201 #endif // LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT)
This is the entry point for all transforms.
ConstantCandidate(ConstantInt *ConstInt, ConstantExpr *ConstExpr=nullptr)
A base constant and all its rebased constants.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
void push_back(const T &Elt)
Definition: SmallVector.h:211
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.
F(f)
This represents a constant that has been rebased with respect to a base constant. ...
Definition: BitVector.h:937
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:888
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
Keeps track of the user of a constant and the operand index where the constant is used...
RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset, Type *Ty=nullptr)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
ConstantUser(Instruction *Inst, unsigned Idx)
A container for analyses that lazily runs them and caches their results.
RebasedConstantListType RebasedConstants
This header defines various interfaces for pass management in LLVM.
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:86