FunctionLoweringInfo.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. } type = NoRelocate;
  97. // Payload contains either frame index of the stack slot in which the value
  98. // was spilled, or virtual register which contains the re-definition.
  99. union payload_t {
  100. payload_t() : FI(-1) {}
  101. int FI;
  102. Register Reg;
  103. } payload;
  104. };
  105. /// Keep track of each value which was relocated and the strategy used to
  106. /// relocate that value. This information is required when visiting
  107. /// gc.relocates which may appear in following blocks.
  108. using StatepointSpillMapTy =
  109. DenseMap<const Value *, StatepointRelocationRecord>;
  110. DenseMap<const Instruction *, StatepointSpillMapTy> StatepointRelocationMaps;
  111. /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
  112. /// the entry block. This allows the allocas to be efficiently referenced
  113. /// anywhere in the function.
  114. DenseMap<const AllocaInst*, int> StaticAllocaMap;
  115. /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
  116. DenseMap<const Argument*, int> ByValArgFrameIndexMap;
  117. /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
  118. /// function arguments that are inserted after scheduling is completed.
  119. SmallVector<MachineInstr*, 8> ArgDbgValues;
  120. /// Bitvector with a bit set if corresponding argument is described in
  121. /// ArgDbgValues. Using arg numbers according to Argument numbering.
  122. BitVector DescribedArgs;
  123. /// RegFixups - Registers which need to be replaced after isel is done.
  124. DenseMap<Register, Register> RegFixups;
  125. DenseSet<Register> RegsWithFixups;
  126. /// StatepointStackSlots - A list of temporary stack slots (frame indices)
  127. /// used to spill values at a statepoint. We store them here to enable
  128. /// reuse of the same stack slots across different statepoints in different
  129. /// basic blocks.
  130. SmallVector<unsigned, 50> StatepointStackSlots;
  131. /// MBB - The current block.
  132. MachineBasicBlock *MBB;
  133. /// MBB - The current insert position inside the current block.
  134. MachineBasicBlock::iterator InsertPt;
  135. struct LiveOutInfo {
  136. unsigned NumSignBits : 31;
  137. unsigned IsValid : 1;
  138. KnownBits Known = 1;
  139. LiveOutInfo() : NumSignBits(0), IsValid(true) {}
  140. };
  141. /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
  142. /// for a value.
  143. DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
  144. /// VisitedBBs - The set of basic blocks visited thus far by instruction
  145. /// selection.
  146. SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
  147. /// PHINodesToUpdate - A list of phi instructions whose operand list will
  148. /// be updated after processing the current basic block.
  149. /// TODO: This isn't per-function state, it's per-basic-block state. But
  150. /// there's no other convenient place for it to live right now.
  151. std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
  152. unsigned OrigNumPHINodesToUpdate;
  153. /// If the current MBB is a landing pad, the exception pointer and exception
  154. /// selector registers are copied into these virtual registers by
  155. /// SelectionDAGISel::PrepareEHLandingPad().
  156. unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg;
  157. /// set - Initialize this FunctionLoweringInfo with the given Function
  158. /// and its associated MachineFunction.
  159. ///
  160. void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
  161. /// clear - Clear out all the function-specific state. This returns this
  162. /// FunctionLoweringInfo to an empty state, ready to be used for a
  163. /// different function.
  164. void clear();
  165. /// isExportedInst - Return true if the specified value is an instruction
  166. /// exported from its block.
  167. bool isExportedInst(const Value *V) const {
  168. return ValueMap.count(V);
  169. }
  170. Register CreateReg(MVT VT, bool isDivergent = false);
  171. Register CreateRegs(const Value *V);
  172. Register CreateRegs(Type *Ty, bool isDivergent = false);
  173. Register InitializeRegForValue(const Value *V) {
  174. // Tokens never live in vregs.
  175. if (V->getType()->isTokenTy())
  176. return 0;
  177. Register &R = ValueMap[V];
  178. assert(R == 0 && "Already initialized this value register!");
  179. assert(VirtReg2Value.empty());
  180. return R = CreateRegs(V);
  181. }
  182. /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
  183. /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
  184. const LiveOutInfo *GetLiveOutRegInfo(Register Reg) {
  185. if (!LiveOutRegInfo.inBounds(Reg))
  186. return nullptr;
  187. const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
  188. if (!LOI->IsValid)
  189. return nullptr;
  190. return LOI;
  191. }
  192. /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
  193. /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
  194. /// the register's LiveOutInfo is for a smaller bit width, it is extended to
  195. /// the larger bit width by zero extension. The bit width must be no smaller
  196. /// than the LiveOutInfo's existing bit width.
  197. const LiveOutInfo *GetLiveOutRegInfo(Register Reg, unsigned BitWidth);
  198. /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
  199. void AddLiveOutRegInfo(Register Reg, unsigned NumSignBits,
  200. const KnownBits &Known) {
  201. // Only install this information if it tells us something.
  202. if (NumSignBits == 1 && Known.isUnknown())
  203. return;
  204. LiveOutRegInfo.grow(Reg);
  205. LiveOutInfo &LOI = LiveOutRegInfo[Reg];
  206. LOI.NumSignBits = NumSignBits;
  207. LOI.Known.One = Known.One;
  208. LOI.Known.Zero = Known.Zero;
  209. }
  210. /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
  211. /// register based on the LiveOutInfo of its operands.
  212. void ComputePHILiveOutRegInfo(const PHINode*);
  213. /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
  214. /// called when a block is visited before all of its predecessors.
  215. void InvalidatePHILiveOutRegInfo(const PHINode *PN) {
  216. // PHIs with no uses have no ValueMap entry.
  217. DenseMap<const Value*, Register>::const_iterator It = ValueMap.find(PN);
  218. if (It == ValueMap.end())
  219. return;
  220. Register Reg = It->second;
  221. if (Reg == 0)
  222. return;
  223. LiveOutRegInfo.grow(Reg);
  224. LiveOutRegInfo[Reg].IsValid = false;
  225. }
  226. /// setArgumentFrameIndex - Record frame index for the byval
  227. /// argument.
  228. void setArgumentFrameIndex(const Argument *A, int FI);
  229. /// getArgumentFrameIndex - Get frame index for the byval argument.
  230. int getArgumentFrameIndex(const Argument *A);
  231. Register getCatchPadExceptionPointerVReg(const Value *CPI,
  232. const TargetRegisterClass *RC);
  233. private:
  234. /// LiveOutRegInfo - Information about live out vregs.
  235. IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
  236. };
  237. } // end namespace llvm
  238. #endif // LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
  239. #ifdef __GNUC__
  240. #pragma GCC diagnostic pop
  241. #endif