LLVM 24.0.0git
FunctionLoweringInfo.h
Go to the documentation of this file.
1//===- FunctionLoweringInfo.h - Lower functions from LLVM IR ---*- 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 implements routines for translating functions from LLVM IR into
10// Machine IR.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
15#define LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
16
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/BitVector.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/IndexedMap.h"
27#include "llvm/IR/Type.h"
28#include "llvm/IR/Value.h"
31#include <cassert>
32#include <optional>
33#include <utility>
34#include <vector>
35
36namespace llvm {
37
38class Argument;
39class BasicBlock;
41class Function;
42class Instruction;
43class MachineFunction;
44class MachineInstr;
46class MVT;
47class SDLoc;
48class SDValue;
49class SelectionDAG;
50class TargetLowering;
51struct EVT;
52
53template <typename T> class GenericSSAContext;
55template <typename T> class GenericUniformityInfo;
57
58//===--------------------------------------------------------------------===//
59/// FunctionLoweringInfo - This contains information that is global to a
60/// function that is used when lowering a region of the function.
61///
63public:
64 const Function *Fn;
70 /// CanLowerReturn - true iff the function's return value can be lowered to
71 /// registers.
73
74 /// True if part of the CSRs will be handled via explicit copies.
76
77 /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg
78 /// allocated to hold a pointer to the hidden sret parameter.
80
81 /// A mapping from LLVM basic block number to their machine block.
83
84 /// ValueMap - Since we emit code for the function a basic block at a time,
85 /// we must remember which virtual registers hold the values for
86 /// cross-basic-block values.
88
89 /// VirtReg2Value map is needed by the Divergence Analysis driven
90 /// instruction selection. It is reverted ValueMap. It is computed
91 /// in lazy style - on demand. It is used to get the Value corresponding
92 /// to the live in virtual register and is called from the
93 /// TargetLowerinInfo::isSDNodeSourceOfDivergence.
95
96 /// This method is called from TargetLowerinInfo::isSDNodeSourceOfDivergence
97 /// to get the Value corresponding to the live-in virtual register.
99
100 /// Track virtual registers created for exception pointers.
102
103 /// A directly-lowered statepoint value (see willLowerDirectly): a leaf that
104 /// can be rebuilt at a gc.relocate in another block.
108 APInt IntValue; // Constant: the integer value.
109 int FrameIndexValue; // FrameIndex: the frame index.
110
111 /// Capture the leaf \p V, which must be a directly-lowered value.
113
114 /// Rebuild the captured leaf as a fresh SDValue of type \p VT.
116 EVT VT) const;
117 };
118
119 /// Helper object to track which of three possible relocation mechanisms are
120 /// used for a particular value being relocated over a statepoint.
123 // Value did not need to be relocated and can be used directly.
125 // Value was spilled to stack and needs filled at the gc.relocate.
127 // Value was lowered to tied def and gc.relocate should be replaced with
128 // copy from vreg.
130 // Value was lowered to tied def and gc.relocate should be replaced with
131 // SDValue kept in StatepointLoweringInfo structure. This valid for local
132 // relocates only.
135 // Payload contains either frame index of the stack slot in which the value
136 // was spilled, or virtual register which contains the re-definition.
142
143 // Set for a NoRelocate value whose gc.relocate is in another block; the
144 // directly-lowered leaf is rebuilt at the gc.relocate.
145 std::optional<StatepointDirectLeaf> RematLeaf;
146 };
147
148 /// Keep track of each value which was relocated and the strategy used to
149 /// relocate that value. This information is required when visiting
150 /// gc.relocates which may appear in following blocks.
154
155 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
156 /// the entry block. This allows the allocas to be efficiently referenced
157 /// anywhere in the function.
159
160 /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
162
163 /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
164 /// function arguments that are inserted after scheduling is completed.
166
167 /// Bitvector with a bit set if corresponding argument is described in
168 /// ArgDbgValues. Using arg numbers according to Argument numbering.
170
171 /// RegFixups - Registers which need to be replaced after isel is done.
173
175
176 /// StatepointStackSlots - A list of temporary stack slots (frame indices)
177 /// used to spill values at a statepoint. We store them here to enable
178 /// reuse of the same stack slots across different statepoints in different
179 /// basic blocks.
181
182 /// MBB - The current block.
184
185 /// MBB - The current insert position inside the current block.
187
188 struct LiveOutInfo {
189 unsigned NumSignBits : 31;
190 unsigned IsValid : 1;
192
194 };
195
196 /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
197 /// for a value.
199
200 /// The set of basic blocks visited thus far by instruction selection. Indexed
201 /// by basic block number.
203
204 /// PHINodesToUpdate - A list of phi instructions whose operand list will
205 /// be updated after processing the current basic block.
206 /// TODO: This isn't per-function state, it's per-basic-block state. But
207 /// there's no other convenient place for it to live right now.
208 std::vector<std::pair<MachineInstr*, Register>> PHINodesToUpdate;
210
211 /// If the current MBB is a landing pad, the exception pointer and exception
212 /// selector registers are copied into these virtual registers by
213 /// SelectionDAGISel::PrepareEHLandingPad().
215
216 /// The exception model in effect, resolved once per function.
218
219 /// The current call site index being processed, if any. 0 if none.
220 unsigned CurCallSite = 0;
221
222 /// Collection of dbg_declare instructions handled after argument
223 /// lowering and before ISel proper.
225
226 /// set - Initialize this FunctionLoweringInfo with the given Function
227 /// and its associated MachineFunction.
228 ///
230
231 /// clear - Clear out all the function-specific state. This returns this
232 /// FunctionLoweringInfo to an empty state, ready to be used for a
233 /// different function.
234 LLVM_ABI void clear();
235
236 /// isExportedInst - Return true if the specified value is an instruction
237 /// exported from its block.
238 bool isExportedInst(const Value *V) const {
239 return ValueMap.count(V);
240 }
241
243 assert(BB->getNumber() < MBBMap.size() && "uninitialized MBBMap?");
244 return MBBMap[BB->getNumber()];
245 }
246
247 LLVM_ABI Register CreateReg(MVT VT, bool isDivergent = false);
248
250
251 LLVM_ABI Register CreateRegs(Type *Ty, bool isDivergent = false);
252
254
255 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
256 /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
258 if (!LiveOutRegInfo.inBounds(Reg))
259 return nullptr;
260
261 const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
262 if (!LOI->IsValid)
263 return nullptr;
264
265 return LOI;
266 }
267
268 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
269 /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
270 /// the register's LiveOutInfo is for a smaller bit width, it is extended to
271 /// the larger bit width by zero extension. The bit width must be no smaller
272 /// than the LiveOutInfo's existing bit width.
273 LLVM_ABI const LiveOutInfo *GetLiveOutRegInfo(Register Reg,
274 unsigned BitWidth);
275
276 /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
277 void AddLiveOutRegInfo(Register Reg, unsigned NumSignBits,
278 const KnownBits &Known) {
279 // Only install this information if it tells us something.
280 if (NumSignBits == 1 && Known.isUnknown())
281 return;
282
283 LiveOutRegInfo.grow(Reg);
284 LiveOutInfo &LOI = LiveOutRegInfo[Reg];
285 LOI.NumSignBits = NumSignBits;
286 LOI.Known.One = Known.One;
287 LOI.Known.Zero = Known.Zero;
288 }
289
290 /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
291 /// register based on the LiveOutInfo of its operands.
293
294 /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
295 /// called when a block is visited before all of its predecessors.
297 // PHIs with no uses have no ValueMap entry.
298 auto It = ValueMap.find(PN);
299 if (It == ValueMap.end())
300 return;
301
302 Register Reg = It->second;
303 if (Reg == 0)
304 return;
305
306 LiveOutRegInfo.grow(Reg);
307 LiveOutRegInfo[Reg].IsValid = false;
308 }
309
310 /// setArgumentFrameIndex - Record frame index for the byval
311 /// argument.
312 LLVM_ABI void setArgumentFrameIndex(const Argument *A, int FI);
313
314 /// getArgumentFrameIndex - Get frame index for the byval argument.
316
318 const Value *CPI, const TargetRegisterClass *RC);
319
320 /// Set the call site currently being processed.
321 void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
322
323 /// Get the call site currently being processed, if any. Return zero if none.
324 unsigned getCurrentCallSite() { return CurCallSite; }
325
326private:
327 /// LiveOutRegInfo - Information about live out vregs.
329};
330
331} // end namespace llvm
332
333#endif // LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
This file implements an indexed map.
Register Reg
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Class for arbitrary precision integers.
Definition APInt.h:78
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM Basic Block Representation.
Definition BasicBlock.h:62
unsigned getNumber() const
Definition BasicBlock.h:95
Analysis providing branch probability information.
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
BranchProbabilityInfo * BPI
LLVM_ABI Register CreateRegs(const Value *V)
DenseMap< const Value *, StatepointRelocationRecord > StatepointSpillMapTy
Keep track of each value which was relocated and the strategy used to relocate that value.
SmallPtrSet< const DbgVariableRecord *, 8 > PreprocessedDVRDeclares
Collection of dbg_declare instructions handled after argument lowering and before ISel proper.
LLVM_ABI void clear()
clear - Clear out all the function-specific state.
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseSet< Register > RegsWithFixups
Register DemoteRegister
DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg allocated to hold a pointer to ...
LLVM_ABI void setArgumentFrameIndex(const Argument *A, int FI)
setArgumentFrameIndex - Record frame index for the byval argument.
SmallVector< bool > VisitedBBs
The set of basic blocks visited thus far by instruction selection.
void InvalidatePHILiveOutRegInfo(const PHINode *PN)
InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be called when a block is visited b...
BitVector DescribedArgs
Bitvector with a bit set if corresponding argument is described in ArgDbgValues.
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
DenseMap< const Instruction *, StatepointSpillMapTy > StatepointRelocationMaps
unsigned CurCallSite
The current call site index being processed, if any. 0 if none.
LLVM_ABI int getArgumentFrameIndex(const Argument *A)
getArgumentFrameIndex - Get frame index for the byval argument.
bool isExportedInst(const Value *V) const
isExportedInst - Return true if the specified value is an instruction exported from its block.
void AddLiveOutRegInfo(Register Reg, unsigned NumSignBits, const KnownBits &Known)
AddLiveOutRegInfo - Adds LiveOutInfo for a register.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
LLVM_ABI Register InitializeRegForValue(const Value *V)
bool SplitCSR
True if part of the CSRs will be handled via explicit copies.
DenseMap< const Value *, Register > ValueMap
ValueMap - Since we emit code for the function a basic block at a time, we must remember which virtua...
Register ExceptionPointerVirtReg
If the current MBB is a landing pad, the exception pointer and exception selector registers are copie...
MachineBasicBlock::iterator InsertPt
MBB - The current insert position inside the current block.
SmallVector< unsigned, 50 > StatepointStackSlots
StatepointStackSlots - A list of temporary stack slots (frame indices) used to spill values at a stat...
LLVM_ABI void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG)
set - Initialize this FunctionLoweringInfo with the given Function and its associated MachineFunction...
MachineBasicBlock * MBB
MBB - The current block.
ExceptionHandling ExceptionModel
The exception model in effect, resolved once per function.
DenseMap< const Argument *, int > ByValArgFrameIndexMap
ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
DenseMap< Register, Register > RegFixups
RegFixups - Registers which need to be replaced after isel is done.
SmallVector< MachineInstr *, 8 > ArgDbgValues
ArgDbgValues - A list of DBG_VALUE instructions created during isel for function arguments that are i...
std::vector< std::pair< MachineInstr *, Register > > PHINodesToUpdate
PHINodesToUpdate - A list of phi instructions whose operand list will be updated after processing the...
unsigned getCurrentCallSite()
Get the call site currently being processed, if any. Return zero if none.
LLVM_ABI void ComputePHILiveOutRegInfo(const PHINode *)
ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination register based on the LiveOutI...
SmallVector< MachineBasicBlock * > MBBMap
A mapping from LLVM basic block number to their machine block.
LLVM_ABI const Value * getValueFromVirtualReg(Register Vreg)
This method is called from TargetLowerinInfo::isSDNodeSourceOfDivergence to get the Value correspondi...
void setCurrentCallSite(unsigned Site)
Set the call site currently being processed.
DenseMap< Register, const Value * > VirtReg2Value
VirtReg2Value map is needed by the Divergence Analysis driven instruction selection.
MachineRegisterInfo * RegInfo
LLVM_ABI Register CreateReg(MVT VT, bool isDivergent=false)
CreateReg - Allocate a single virtual register for the given type.
bool CanLowerReturn
CanLowerReturn - true iff the function's return value can be lowered to registers.
DenseMap< const Value *, ISD::NodeType > PreferredExtendType
Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND) for a value.
LLVM_ABI Register getCatchPadExceptionPointerVReg(const Value *CPI, const TargetRegisterClass *RC)
DenseMap< const Value *, Register > CatchPadExceptionPointers
Track virtual registers created for exception pointers.
Machine Value Type.
MachineInstrBundleIterator< MachineInstr > iterator
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
GenericSSAContext< Function > SSAContext
@ Known
Known to have no common set bits.
constexpr unsigned BitWidth
ExceptionHandling
Definition CodeGen.h:54
@ Default
Not specified; resolve to the target's default model.
Definition CodeGen.h:55
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
Extended Value Type.
Definition ValueTypes.h:35
LLVM_ABI SDValue rematerialize(SelectionDAG &DAG, const SDLoc &DL, EVT VT) const
Rebuild the captured leaf as a fresh SDValue of type VT.
LLVM_ABI StatepointDirectLeaf(SDValue V)
Capture the leaf V, which must be a directly-lowered value.
Helper object to track which of three possible relocation mechanisms are used for a particular value ...
union llvm::FunctionLoweringInfo::StatepointRelocationRecord::payload_t payload
enum llvm::FunctionLoweringInfo::StatepointRelocationRecord::RelocType type