FunctionLoweringInfo.h 11 KB

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