FunctionLoweringInfo.h 10 KB

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