FunctionLoweringInfo.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //===-- FunctionLoweringInfo.h - Lower functions from LLVM IR to CodeGen --===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This implements routines for translating functions from LLVM IR into
  11. // Machine IR.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
  15. #define LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
  16. #include "llvm/ADT/APInt.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/IndexedMap.h"
  19. #include "llvm/ADT/Optional.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/IR/InlineAsm.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/Target/TargetRegisterInfo.h"
  27. #include <vector>
  28. namespace llvm {
  29. class AllocaInst;
  30. class BasicBlock;
  31. class BranchProbabilityInfo;
  32. class CallInst;
  33. class Function;
  34. class GlobalVariable;
  35. class Instruction;
  36. class MachineInstr;
  37. class MachineBasicBlock;
  38. class MachineFunction;
  39. class MachineModuleInfo;
  40. class MachineRegisterInfo;
  41. class SelectionDAG;
  42. class MVT;
  43. class TargetLowering;
  44. class Value;
  45. //===--------------------------------------------------------------------===//
  46. /// FunctionLoweringInfo - This contains information that is global to a
  47. /// function that is used when lowering a region of the function.
  48. ///
  49. class FunctionLoweringInfo {
  50. public:
  51. const Function *Fn;
  52. MachineFunction *MF;
  53. const TargetLowering *TLI;
  54. MachineRegisterInfo *RegInfo;
  55. BranchProbabilityInfo *BPI;
  56. /// CanLowerReturn - true iff the function's return value can be lowered to
  57. /// registers.
  58. bool CanLowerReturn;
  59. /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg
  60. /// allocated to hold a pointer to the hidden sret parameter.
  61. unsigned DemoteRegister;
  62. /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
  63. DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap;
  64. /// ValueMap - Since we emit code for the function a basic block at a time,
  65. /// we must remember which virtual registers hold the values for
  66. /// cross-basic-block values.
  67. DenseMap<const Value*, unsigned> ValueMap;
  68. // Keep track of frame indices allocated for statepoints as they could be used
  69. // across basic block boundaries.
  70. // Key of the map is statepoint instruction, value is a map from spilled
  71. // llvm Value to the optional stack stack slot index.
  72. // If optional is unspecified it means that we have visited this value
  73. // but didn't spill it.
  74. typedef DenseMap<const Value*, Optional<int>> StatepointSpilledValueMapTy;
  75. DenseMap<const Instruction*, StatepointSpilledValueMapTy>
  76. StatepointRelocatedValues;
  77. /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
  78. /// the entry block. This allows the allocas to be efficiently referenced
  79. /// anywhere in the function.
  80. DenseMap<const AllocaInst*, int> StaticAllocaMap;
  81. /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
  82. DenseMap<const Argument*, int> ByValArgFrameIndexMap;
  83. /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
  84. /// function arguments that are inserted after scheduling is completed.
  85. SmallVector<MachineInstr*, 8> ArgDbgValues;
  86. /// RegFixups - Registers which need to be replaced after isel is done.
  87. DenseMap<unsigned, unsigned> RegFixups;
  88. /// StatepointStackSlots - A list of temporary stack slots (frame indices)
  89. /// used to spill values at a statepoint. We store them here to enable
  90. /// reuse of the same stack slots across different statepoints in different
  91. /// basic blocks.
  92. SmallVector<unsigned, 50> StatepointStackSlots;
  93. /// MBB - The current block.
  94. MachineBasicBlock *MBB;
  95. /// MBB - The current insert position inside the current block.
  96. MachineBasicBlock::iterator InsertPt;
  97. #ifndef NDEBUG
  98. SmallPtrSet<const Instruction *, 8> CatchInfoLost;
  99. SmallPtrSet<const Instruction *, 8> CatchInfoFound;
  100. #endif
  101. struct LiveOutInfo {
  102. unsigned NumSignBits : 31;
  103. bool IsValid : 1;
  104. APInt KnownOne, KnownZero;
  105. LiveOutInfo() : NumSignBits(0), IsValid(true), KnownOne(1, 0),
  106. KnownZero(1, 0) {}
  107. };
  108. /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
  109. /// for a value.
  110. DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
  111. /// VisitedBBs - The set of basic blocks visited thus far by instruction
  112. /// selection.
  113. SmallPtrSet<const BasicBlock*, 4> VisitedBBs;
  114. /// PHINodesToUpdate - A list of phi instructions whose operand list will
  115. /// be updated after processing the current basic block.
  116. /// TODO: This isn't per-function state, it's per-basic-block state. But
  117. /// there's no other convenient place for it to live right now.
  118. std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
  119. unsigned OrigNumPHINodesToUpdate;
  120. /// If the current MBB is a landing pad, the exception pointer and exception
  121. /// selector registers are copied into these virtual registers by
  122. /// SelectionDAGISel::PrepareEHLandingPad().
  123. unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg;
  124. /// set - Initialize this FunctionLoweringInfo with the given Function
  125. /// and its associated MachineFunction.
  126. ///
  127. void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
  128. /// clear - Clear out all the function-specific state. This returns this
  129. /// FunctionLoweringInfo to an empty state, ready to be used for a
  130. /// different function.
  131. void clear();
  132. /// isExportedInst - Return true if the specified value is an instruction
  133. /// exported from its block.
  134. bool isExportedInst(const Value *V) {
  135. return ValueMap.count(V);
  136. }
  137. unsigned CreateReg(MVT VT);
  138. unsigned CreateRegs(Type *Ty);
  139. unsigned InitializeRegForValue(const Value *V) {
  140. unsigned &R = ValueMap[V];
  141. assert(R == 0 && "Already initialized this value register!");
  142. return R = CreateRegs(V->getType());
  143. }
  144. /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
  145. /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
  146. const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg) {
  147. if (!LiveOutRegInfo.inBounds(Reg))
  148. return nullptr;
  149. const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
  150. if (!LOI->IsValid)
  151. return nullptr;
  152. return LOI;
  153. }
  154. /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
  155. /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
  156. /// the register's LiveOutInfo is for a smaller bit width, it is extended to
  157. /// the larger bit width by zero extension. The bit width must be no smaller
  158. /// than the LiveOutInfo's existing bit width.
  159. const LiveOutInfo *GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth);
  160. /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
  161. void AddLiveOutRegInfo(unsigned Reg, unsigned NumSignBits,
  162. const APInt &KnownZero, const APInt &KnownOne) {
  163. // Only install this information if it tells us something.
  164. if (NumSignBits == 1 && KnownZero == 0 && KnownOne == 0)
  165. return;
  166. LiveOutRegInfo.grow(Reg);
  167. LiveOutInfo &LOI = LiveOutRegInfo[Reg];
  168. LOI.NumSignBits = NumSignBits;
  169. LOI.KnownOne = KnownOne;
  170. LOI.KnownZero = KnownZero;
  171. }
  172. /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
  173. /// register based on the LiveOutInfo of its operands.
  174. void ComputePHILiveOutRegInfo(const PHINode*);
  175. /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
  176. /// called when a block is visited before all of its predecessors.
  177. void InvalidatePHILiveOutRegInfo(const PHINode *PN) {
  178. // PHIs with no uses have no ValueMap entry.
  179. DenseMap<const Value*, unsigned>::const_iterator It = ValueMap.find(PN);
  180. if (It == ValueMap.end())
  181. return;
  182. unsigned Reg = It->second;
  183. if (Reg == 0)
  184. return;
  185. LiveOutRegInfo.grow(Reg);
  186. LiveOutRegInfo[Reg].IsValid = false;
  187. }
  188. /// setArgumentFrameIndex - Record frame index for the byval
  189. /// argument.
  190. void setArgumentFrameIndex(const Argument *A, int FI);
  191. /// getArgumentFrameIndex - Get frame index for the byval argument.
  192. int getArgumentFrameIndex(const Argument *A);
  193. private:
  194. void addSEHHandlersForLPads(ArrayRef<const LandingPadInst *> LPads);
  195. /// LiveOutRegInfo - Information about live out vregs.
  196. IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
  197. };
  198. /// ComputeUsesVAFloatArgument - Determine if any floating-point values are
  199. /// being passed to this variadic function, and set the MachineModuleInfo's
  200. /// usesVAFloatArgument flag if so. This flag is used to emit an undefined
  201. /// reference to _fltused on Windows, which will link in MSVCRT's
  202. /// floating-point support.
  203. void ComputeUsesVAFloatArgument(const CallInst &I, MachineModuleInfo *MMI);
  204. /// AddLandingPadInfo - Extract the exception handling information from the
  205. /// landingpad instruction and add them to the specified machine module info.
  206. void AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
  207. MachineBasicBlock *MBB);
  208. } // end namespace llvm
  209. #endif