| 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/BitVector.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/IndexedMap.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/CodeGen/ISDOpcodes.h" |
| 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 24 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/Type.h" |
| 27 | #include "llvm/IR/Value.h" |
| 28 | #include "llvm/Support/KnownBits.h" |
| 29 | #include <cassert> |
| 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
| 33 | namespace llvm { |
| 34 | |
| 35 | class Argument; |
| 36 | class BasicBlock; |
| 37 | class BranchProbabilityInfo; |
| 38 | class LegacyDivergenceAnalysis; |
| 39 | class Function; |
| 40 | class Instruction; |
| 41 | class MachineFunction; |
| 42 | class MachineInstr; |
| 43 | class MachineRegisterInfo; |
| 44 | class MVT; |
| 45 | class SelectionDAG; |
| 46 | class TargetLowering; |
| 47 | |
| 48 | //===--------------------------------------------------------------------===// |
| 49 | /// FunctionLoweringInfo - This contains information that is global to a |
| 50 | /// function that is used when lowering a region of the function. |
| 51 | /// |
| 52 | class FunctionLoweringInfo { |
| 53 | public: |
| 54 | const Function *Fn; |
| 55 | MachineFunction *MF; |
| 56 | const TargetLowering *TLI; |
| 57 | MachineRegisterInfo *RegInfo; |
| 58 | BranchProbabilityInfo *BPI; |
| 59 | const LegacyDivergenceAnalysis *DA; |
| 60 | /// CanLowerReturn - true iff the function's return value can be lowered to |
| 61 | /// registers. |
| 62 | bool CanLowerReturn; |
| 63 | |
| 64 | /// True if part of the CSRs will be handled via explicit copies. |
| 65 | bool SplitCSR; |
| 66 | |
| 67 | /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg |
| 68 | /// allocated to hold a pointer to the hidden sret parameter. |
| 69 | Register DemoteRegister; |
| 70 | |
| 71 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 72 | DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 73 | |
| 74 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 75 | /// we must remember which virtual registers hold the values for |
| 76 | /// cross-basic-block values. |
| 77 | DenseMap<const Value *, Register> ValueMap; |
| 78 | |
| 79 | /// VirtReg2Value map is needed by the Divergence Analysis driven |
| 80 | /// instruction selection. It is reverted ValueMap. It is computed |
| 81 | /// in lazy style - on demand. It is used to get the Value corresponding |
| 82 | /// to the live in virtual register and is called from the |
| 83 | /// TargetLowerinInfo::isSDNodeSourceOfDivergence. |
| 84 | DenseMap<Register, const Value*> VirtReg2Value; |
| 85 | |
| 86 | /// This method is called from TargetLowerinInfo::isSDNodeSourceOfDivergence |
| 87 | /// to get the Value corresponding to the live-in virtual register. |
| 88 | const Value *getValueFromVirtualReg(Register Vreg); |
| 89 | |
| 90 | /// Track virtual registers created for exception pointers. |
| 91 | DenseMap<const Value *, Register> CatchPadExceptionPointers; |
| 92 | |
| 93 | /// Helper object to track which of three possible relocation mechanisms are |
| 94 | /// used for a particular value being relocated over a statepoint. |
| 95 | struct StatepointRelocationRecord { |
| 96 | enum RelocType { |
| 97 | // Value did not need to be relocated and can be used directly. |
| 98 | NoRelocate, |
| 99 | // Value was spilled to stack and needs filled at the gc.relocate. |
| 100 | Spill, |
| 101 | // Value was lowered to tied def and gc.relocate should be replaced with |
| 102 | // copy from vreg. |
| 103 | VReg, |
| 104 | } type = NoRelocate; |
| 105 | // Payload contains either frame index of the stack slot in which the value |
| 106 | // was spilled, or virtual register which contains the re-definition. |
| 107 | union payload_t { |
| 108 | payload_t() : FI(-1) {} |
| 109 | int FI; |
| 110 | Register Reg; |
| 111 | } payload; |
| 112 | }; |
| 113 | |
| 114 | /// Keep track of each value which was relocated and the strategy used to |
| 115 | /// relocate that value. This information is required when visiting |
| 116 | /// gc.relocates which may appear in following blocks. |
| 117 | using StatepointSpillMapTy = |
| 118 | DenseMap<const Value *, StatepointRelocationRecord>; |
| 119 | DenseMap<const Instruction *, StatepointSpillMapTy> StatepointRelocationMaps; |
| 120 | |
| 121 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 122 | /// the entry block. This allows the allocas to be efficiently referenced |
| 123 | /// anywhere in the function. |
| 124 | DenseMap<const AllocaInst*, int> StaticAllocaMap; |
| 125 | |
| 126 | /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments. |
| 127 | DenseMap<const Argument*, int> ByValArgFrameIndexMap; |
| 128 | |
| 129 | /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for |
| 130 | /// function arguments that are inserted after scheduling is completed. |
| 131 | SmallVector<MachineInstr*, 8> ArgDbgValues; |
| 132 | |
| 133 | /// Bitvector with a bit set if corresponding argument is described in |
| 134 | /// ArgDbgValues. Using arg numbers according to Argument numbering. |
| 135 | BitVector DescribedArgs; |
| 136 | |
| 137 | /// RegFixups - Registers which need to be replaced after isel is done. |
| 138 | DenseMap<Register, Register> RegFixups; |
| 139 | |
| 140 | DenseSet<Register> RegsWithFixups; |
| 141 | |
| 142 | /// StatepointStackSlots - A list of temporary stack slots (frame indices) |
| 143 | /// used to spill values at a statepoint. We store them here to enable |
| 144 | /// reuse of the same stack slots across different statepoints in different |
| 145 | /// basic blocks. |
| 146 | SmallVector<unsigned, 50> StatepointStackSlots; |
| 147 | |
| 148 | /// MBB - The current block. |
| 149 | MachineBasicBlock *MBB; |
| 150 | |
| 151 | /// MBB - The current insert position inside the current block. |
| 152 | MachineBasicBlock::iterator InsertPt; |
| 153 | |
| 154 | struct LiveOutInfo { |
| 155 | unsigned NumSignBits : 31; |
| 156 | unsigned IsValid : 1; |
| 157 | KnownBits Known = 1; |
| 158 | |
| 159 | LiveOutInfo() : NumSignBits(0), IsValid(true) {} |
| 160 | }; |
| 161 | |
| 162 | /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND) |
| 163 | /// for a value. |
| 164 | DenseMap<const Value *, ISD::NodeType> PreferredExtendType; |
| 165 | |
| 166 | /// VisitedBBs - The set of basic blocks visited thus far by instruction |
| 167 | /// selection. |
| 168 | SmallPtrSet<const BasicBlock*, 4> VisitedBBs; |
| 169 | |
| 170 | /// PHINodesToUpdate - A list of phi instructions whose operand list will |
| 171 | /// be updated after processing the current basic block. |
| 172 | /// TODO: This isn't per-function state, it's per-basic-block state. But |
| 173 | /// there's no other convenient place for it to live right now. |
| 174 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 175 | unsigned OrigNumPHINodesToUpdate; |
| 176 | |
| 177 | /// If the current MBB is a landing pad, the exception pointer and exception |
| 178 | /// selector registers are copied into these virtual registers by |
| 179 | /// SelectionDAGISel::PrepareEHLandingPad(). |
| 180 | unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg; |
| 181 | |
| 182 | /// set - Initialize this FunctionLoweringInfo with the given Function |
| 183 | /// and its associated MachineFunction. |
| 184 | /// |
| 185 | void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG); |
| 186 | |
| 187 | /// clear - Clear out all the function-specific state. This returns this |
| 188 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 189 | /// different function. |
| 190 | void clear(); |
| 191 | |
| 192 | /// isExportedInst - Return true if the specified value is an instruction |
| 193 | /// exported from its block. |
| 194 | bool isExportedInst(const Value *V) const { |
| 195 | return ValueMap.count(V); |
| 196 | } |
| 197 | |
| 198 | Register CreateReg(MVT VT, bool isDivergent = false); |
| 199 | |
| 200 | Register CreateRegs(const Value *V); |
| 201 | |
| 202 | Register CreateRegs(Type *Ty, bool isDivergent = false); |
| 203 | |
| 204 | Register InitializeRegForValue(const Value *V) { |
| 205 | // Tokens never live in vregs. |
| 206 | if (V->getType()->isTokenTy()) |
| 207 | return 0; |
| 208 | Register &R = ValueMap[V]; |
| 209 | assert(R == 0 && "Already initialized this value register!"); |
| 210 | assert(VirtReg2Value.empty()); |
| 211 | return R = CreateRegs(V); |
| 212 | } |
| 213 | |
| 214 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 215 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. |
| 216 | const LiveOutInfo *GetLiveOutRegInfo(Register Reg) { |
| 217 | if (!LiveOutRegInfo.inBounds(Reg)) |
| 218 | return nullptr; |
| 219 | |
| 220 | const LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; |
| 221 | if (!LOI->IsValid) |
| 222 | return nullptr; |
| 223 | |
| 224 | return LOI; |
| 225 | } |
| 226 | |
| 227 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 228 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If |
| 229 | /// the register's LiveOutInfo is for a smaller bit width, it is extended to |
| 230 | /// the larger bit width by zero extension. The bit width must be no smaller |
| 231 | /// than the LiveOutInfo's existing bit width. |
| 232 | const LiveOutInfo *GetLiveOutRegInfo(Register Reg, unsigned BitWidth); |
| 233 | |
| 234 | /// AddLiveOutRegInfo - Adds LiveOutInfo for a register. |
| 235 | void AddLiveOutRegInfo(Register Reg, unsigned NumSignBits, |
| 236 | const KnownBits &Known) { |
| 237 | // Only install this information if it tells us something. |
| 238 | if (NumSignBits == 1 && Known.isUnknown()) |
| 239 | return; |
| 240 | |
| 241 | LiveOutRegInfo.grow(Reg); |
| 242 | LiveOutInfo &LOI = LiveOutRegInfo[Reg]; |
| 243 | LOI.NumSignBits = NumSignBits; |
| 244 | LOI.Known.One = Known.One; |
| 245 | LOI.Known.Zero = Known.Zero; |
| 246 | } |
| 247 | |
| 248 | /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination |
| 249 | /// register based on the LiveOutInfo of its operands. |
| 250 | void ComputePHILiveOutRegInfo(const PHINode*); |
| 251 | |
| 252 | /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be |
| 253 | /// called when a block is visited before all of its predecessors. |
| 254 | void InvalidatePHILiveOutRegInfo(const PHINode *PN) { |
| 255 | // PHIs with no uses have no ValueMap entry. |
| 256 | DenseMap<const Value*, Register>::const_iterator It = ValueMap.find(PN); |
| 257 | if (It == ValueMap.end()) |
| 258 | return; |
| 259 | |
| 260 | Register Reg = It->second; |
| 261 | if (Reg == 0) |
| 262 | return; |
| 263 | |
| 264 | LiveOutRegInfo.grow(Reg); |
| 265 | LiveOutRegInfo[Reg].IsValid = false; |
| 266 | } |
| 267 | |
| 268 | /// setArgumentFrameIndex - Record frame index for the byval |
| 269 | /// argument. |
| 270 | void setArgumentFrameIndex(const Argument *A, int FI); |
| 271 | |
| 272 | /// getArgumentFrameIndex - Get frame index for the byval argument. |
| 273 | int getArgumentFrameIndex(const Argument *A); |
| 274 | |
| 275 | Register getCatchPadExceptionPointerVReg(const Value *CPI, |
| 276 | const TargetRegisterClass *RC); |
| 277 | |
| 278 | private: |
| 279 | /// LiveOutRegInfo - Information about live out vregs. |
| 280 | IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo; |
| 281 | }; |
| 282 | |
| 283 | } // end namespace llvm |
| 284 | |
| 285 | #endif // LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H |