1 //===- FunctionLoweringInfo.h - Lower functions from LLVM IR to CodeGen ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements routines for translating functions from LLVM IR into
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
16 #define LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/IndexedMap.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/CodeGen/ISDOpcodes.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/TargetRegisterInfo.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/Support/KnownBits.h"
39 class BranchProbabilityInfo;
42 class MachineFunction;
44 class MachineRegisterInfo;
49 //===--------------------------------------------------------------------===//
50 /// FunctionLoweringInfo - This contains information that is global to a
51 /// function that is used when lowering a region of the function.
53 class FunctionLoweringInfo {
57 const TargetLowering *TLI;
58 MachineRegisterInfo *RegInfo;
59 BranchProbabilityInfo *BPI;
60 /// CanLowerReturn - true iff the function's return value can be lowered to
64 /// True if part of the CSRs will be handled via explicit copies.
67 /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg
68 /// allocated to hold a pointer to the hidden sret parameter.
69 unsigned DemoteRegister;
71 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
72 DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap;
74 /// A map from swifterror value in a basic block to the virtual register it is
75 /// currently represented by.
76 DenseMap<std::pair<const MachineBasicBlock *, const Value *>, unsigned>
79 /// A list of upward exposed vreg uses that need to be satisfied by either a
80 /// copy def or a phi node at the beginning of the basic block representing
81 /// the predecessor(s) swifterror value.
82 DenseMap<std::pair<const MachineBasicBlock *, const Value *>, unsigned>
83 SwiftErrorVRegUpwardsUse;
85 /// A map from instructions that define/use a swifterror value to the virtual
86 /// register that represents that def/use.
87 llvm::DenseMap<PointerIntPair<const Instruction *, 1, bool>, unsigned>
88 SwiftErrorVRegDefUses;
90 /// The swifterror argument of the current function.
91 const Value *SwiftErrorArg;
93 using SwiftErrorValues = SmallVector<const Value*, 1>;
94 /// A function can only have a single swifterror argument. And if it does
95 /// have a swifterror argument, it must be the first entry in
97 SwiftErrorValues SwiftErrorVals;
99 /// Get or create the swifterror value virtual register in
100 /// SwiftErrorVRegDefMap for this basic block.
101 unsigned getOrCreateSwiftErrorVReg(const MachineBasicBlock *,
104 /// Set the swifterror virtual register in the SwiftErrorVRegDefMap for this
106 void setCurrentSwiftErrorVReg(const MachineBasicBlock *MBB, const Value *,
109 /// Get or create the swifterror value virtual register for a def of a
110 /// swifterror by an instruction.
111 std::pair<unsigned, bool> getOrCreateSwiftErrorVRegDefAt(const Instruction *);
112 std::pair<unsigned, bool>
113 getOrCreateSwiftErrorVRegUseAt(const Instruction *, const MachineBasicBlock *,
116 /// ValueMap - Since we emit code for the function a basic block at a time,
117 /// we must remember which virtual registers hold the values for
118 /// cross-basic-block values.
119 DenseMap<const Value *, unsigned> ValueMap;
121 /// Track virtual registers created for exception pointers.
122 DenseMap<const Value *, unsigned> CatchPadExceptionPointers;
124 /// Keep track of frame indices allocated for statepoints as they could be
125 /// used across basic block boundaries. This struct is more complex than a
126 /// simple map because the stateopint lowering code de-duplicates gc pointers
127 /// based on their SDValue (so %p and (bitcast %p to T) will get the same
128 /// slot), and we track that here.
130 struct StatepointSpillMap {
131 using SlotMapTy = DenseMap<const Value *, Optional<int>>;
133 /// Maps uniqued llvm IR values to the slots they were spilled in. If a
134 /// value is mapped to None it means we visited the value but didn't spill
135 /// it (because it was a constant, for instance).
138 /// Maps llvm IR values to the values they were de-duplicated to.
139 DenseMap<const Value *, const Value *> DuplicateMap;
141 SlotMapTy::const_iterator find(const Value *V) const {
142 auto DuplIt = DuplicateMap.find(V);
143 if (DuplIt != DuplicateMap.end())
145 return SlotMap.find(V);
148 SlotMapTy::const_iterator end() const { return SlotMap.end(); }
151 /// Maps gc.statepoint instructions to their corresponding StatepointSpillMap
153 DenseMap<const Instruction *, StatepointSpillMap> StatepointSpillMaps;
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.
158 DenseMap<const AllocaInst*, int> StaticAllocaMap;
160 /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
161 DenseMap<const Argument*, int> ByValArgFrameIndexMap;
163 /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
164 /// function arguments that are inserted after scheduling is completed.
165 SmallVector<MachineInstr*, 8> ArgDbgValues;
167 /// RegFixups - Registers which need to be replaced after isel is done.
168 DenseMap<unsigned, unsigned> RegFixups;
170 /// StatepointStackSlots - A list of temporary stack slots (frame indices)
171 /// used to spill values at a statepoint. We store them here to enable
172 /// reuse of the same stack slots across different statepoints in different
174 SmallVector<unsigned, 50> StatepointStackSlots;
176 /// MBB - The current block.
177 MachineBasicBlock *MBB;
179 /// MBB - The current insert position inside the current block.
180 MachineBasicBlock::iterator InsertPt;
183 unsigned NumSignBits : 31;
184 unsigned IsValid : 1;
187 LiveOutInfo() : NumSignBits(0), IsValid(true) {}
190 /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
192 DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
194 /// VisitedBBs - The set of basic blocks visited thus far by instruction
196 SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
198 /// PHINodesToUpdate - A list of phi instructions whose operand list will
199 /// be updated after processing the current basic block.
200 /// TODO: This isn't per-function state, it's per-basic-block state. But
201 /// there's no other convenient place for it to live right now.
202 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
203 unsigned OrigNumPHINodesToUpdate;
205 /// If the current MBB is a landing pad, the exception pointer and exception
206 /// selector registers are copied into these virtual registers by
207 /// SelectionDAGISel::PrepareEHLandingPad().
208 unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg;
210 /// set - Initialize this FunctionLoweringInfo with the given Function
211 /// and its associated MachineFunction.
213 void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
215 /// clear - Clear out all the function-specific state. This returns this
216 /// FunctionLoweringInfo to an empty state, ready to be used for a
217 /// different function.
220 /// isExportedInst - Return true if the specified value is an instruction
221 /// exported from its block.
222 bool isExportedInst(const Value *V) {
223 return ValueMap.count(V);
226 unsigned CreateReg(MVT VT);
228 unsigned CreateRegs(Type *Ty);
230 unsigned InitializeRegForValue(const Value *V) {
231 // Tokens never live in vregs.
232 if (V->getType()->isTokenTy())
234 unsigned &R = ValueMap[V];
235 assert(R == 0 && "Already initialized this value register!");
236 return R = CreateRegs(V->getType());
239 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
240 /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
241 const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg) {
242 if (!LiveOutRegInfo.inBounds(Reg))
245 const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
252 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
253 /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
254 /// the register's LiveOutInfo is for a smaller bit width, it is extended to
255 /// the larger bit width by zero extension. The bit width must be no smaller
256 /// than the LiveOutInfo's existing bit width.
257 const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth);
259 /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
260 void AddLiveOutRegInfo(unsigned Reg, unsigned NumSignBits,
261 const KnownBits &Known) {
262 // Only install this information if it tells us something.
263 if (NumSignBits == 1 && Known.isUnknown())
266 LiveOutRegInfo.grow(Reg);
267 LiveOutInfo &LOI = LiveOutRegInfo[Reg];
268 LOI.NumSignBits = NumSignBits;
269 LOI.Known.One = Known.One;
270 LOI.Known.Zero = Known.Zero;
273 /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
274 /// register based on the LiveOutInfo of its operands.
275 void ComputePHILiveOutRegInfo(const PHINode*);
277 /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
278 /// called when a block is visited before all of its predecessors.
279 void InvalidatePHILiveOutRegInfo(const PHINode *PN) {
280 // PHIs with no uses have no ValueMap entry.
281 DenseMap<const Value*, unsigned>::const_iterator It = ValueMap.find(PN);
282 if (It == ValueMap.end())
285 unsigned Reg = It->second;
289 LiveOutRegInfo.grow(Reg);
290 LiveOutRegInfo[Reg].IsValid = false;
293 /// setArgumentFrameIndex - Record frame index for the byval
295 void setArgumentFrameIndex(const Argument *A, int FI);
297 /// getArgumentFrameIndex - Get frame index for the byval argument.
298 int getArgumentFrameIndex(const Argument *A);
300 unsigned getCatchPadExceptionPointerVReg(const Value *CPI,
301 const TargetRegisterClass *RC);
304 void addSEHHandlersForLPads(ArrayRef<const LandingPadInst *> LPads);
306 /// LiveOutRegInfo - Information about live out vregs.
307 IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
310 } // end namespace llvm
312 #endif // LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H