Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
17 KiB

  1. //===-- FastISel.h - Definition of the FastISel class ---------------------===//
  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 file defines the FastISel class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_FASTISEL_H
  14. #define LLVM_CODEGEN_FASTISEL_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/ValueTypes.h"
  18. namespace llvm {
  19. class AllocaInst;
  20. class Constant;
  21. class ConstantFP;
  22. class FunctionLoweringInfo;
  23. class Instruction;
  24. class LoadInst;
  25. class MachineBasicBlock;
  26. class MachineConstantPool;
  27. class MachineFunction;
  28. class MachineInstr;
  29. class MachineFrameInfo;
  30. class MachineRegisterInfo;
  31. class DataLayout;
  32. class TargetInstrInfo;
  33. class TargetLibraryInfo;
  34. class TargetLowering;
  35. class TargetMachine;
  36. class TargetRegisterClass;
  37. class TargetRegisterInfo;
  38. class User;
  39. class Value;
  40. /// FastISel - This is a fast-path instruction selection class that
  41. /// generates poor code and doesn't support illegal types or non-trivial
  42. /// lowering, but runs quickly.
  43. class FastISel {
  44. protected:
  45. DenseMap<const Value *, unsigned> LocalValueMap;
  46. FunctionLoweringInfo &FuncInfo;
  47. MachineRegisterInfo &MRI;
  48. MachineFrameInfo &MFI;
  49. MachineConstantPool &MCP;
  50. DebugLoc DL;
  51. const TargetMachine &TM;
  52. const DataLayout &TD;
  53. const TargetInstrInfo &TII;
  54. const TargetLowering &TLI;
  55. const TargetRegisterInfo &TRI;
  56. const TargetLibraryInfo *LibInfo;
  57. /// The position of the last instruction for materializing constants
  58. /// for use in the current block. It resets to EmitStartPt when it
  59. /// makes sense (for example, it's usually profitable to avoid function
  60. /// calls between the definition and the use)
  61. MachineInstr *LastLocalValue;
  62. /// The top most instruction in the current block that is allowed for
  63. /// emitting local variables. LastLocalValue resets to EmitStartPt when
  64. /// it makes sense (for example, on function calls)
  65. MachineInstr *EmitStartPt;
  66. public:
  67. /// getLastLocalValue - Return the position of the last instruction
  68. /// emitted for materializing constants for use in the current block.
  69. MachineInstr *getLastLocalValue() { return LastLocalValue; }
  70. /// setLastLocalValue - Update the position of the last instruction
  71. /// emitted for materializing constants for use in the current block.
  72. void setLastLocalValue(MachineInstr *I) {
  73. EmitStartPt = I;
  74. LastLocalValue = I;
  75. }
  76. /// startNewBlock - Set the current block to which generated machine
  77. /// instructions will be appended, and clear the local CSE map.
  78. ///
  79. void startNewBlock();
  80. /// getCurDebugLoc() - Return current debug location information.
  81. DebugLoc getCurDebugLoc() const { return DL; }
  82. /// LowerArguments - Do "fast" instruction selection for function arguments
  83. /// and append machine instructions to the current block. Return true if
  84. /// it is successful.
  85. bool LowerArguments();
  86. /// SelectInstruction - Do "fast" instruction selection for the given
  87. /// LLVM IR instruction, and append generated machine instructions to
  88. /// the current block. Return true if selection was successful.
  89. ///
  90. bool SelectInstruction(const Instruction *I);
  91. /// SelectOperator - Do "fast" instruction selection for the given
  92. /// LLVM IR operator (Instruction or ConstantExpr), and append
  93. /// generated machine instructions to the current block. Return true
  94. /// if selection was successful.
  95. ///
  96. bool SelectOperator(const User *I, unsigned Opcode);
  97. /// getRegForValue - Create a virtual register and arrange for it to
  98. /// be assigned the value for the given LLVM value.
  99. unsigned getRegForValue(const Value *V);
  100. /// lookUpRegForValue - Look up the value to see if its value is already
  101. /// cached in a register. It may be defined by instructions across blocks or
  102. /// defined locally.
  103. unsigned lookUpRegForValue(const Value *V);
  104. /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
  105. /// takes care of truncating or sign-extending the given getelementptr
  106. /// index value.
  107. std::pair<unsigned, bool> getRegForGEPIndex(const Value *V);
  108. /// \brief We're checking to see if we can fold \p LI into \p FoldInst.
  109. /// Note that we could have a sequence where multiple LLVM IR instructions
  110. /// are folded into the same machineinstr. For example we could have:
  111. /// A: x = load i32 *P
  112. /// B: y = icmp A, 42
  113. /// C: br y, ...
  114. ///
  115. /// In this scenario, \p LI is "A", and \p FoldInst is "C". We know
  116. /// about "B" (and any other folded instructions) because it is between
  117. /// A and C.
  118. ///
  119. /// If we succeed folding, return true.
  120. ///
  121. bool tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst);
  122. /// \brief The specified machine instr operand is a vreg, and that
  123. /// vreg is being provided by the specified load instruction. If possible,
  124. /// try to fold the load as an operand to the instruction, returning true if
  125. /// possible.
  126. /// This method should be implemented by targets.
  127. virtual bool tryToFoldLoadIntoMI(MachineInstr * /*MI*/, unsigned /*OpNo*/,
  128. const LoadInst * /*LI*/) {
  129. return false;
  130. }
  131. /// recomputeInsertPt - Reset InsertPt to prepare for inserting instructions
  132. /// into the current block.
  133. void recomputeInsertPt();
  134. /// removeDeadCode - Remove all dead instructions between the I and E.
  135. void removeDeadCode(MachineBasicBlock::iterator I,
  136. MachineBasicBlock::iterator E);
  137. struct SavePoint {
  138. MachineBasicBlock::iterator InsertPt;
  139. DebugLoc DL;
  140. };
  141. /// enterLocalValueArea - Prepare InsertPt to begin inserting instructions
  142. /// into the local value area and return the old insert position.
  143. SavePoint enterLocalValueArea();
  144. /// leaveLocalValueArea - Reset InsertPt to the given old insert position.
  145. void leaveLocalValueArea(SavePoint Old);
  146. virtual ~FastISel();
  147. protected:
  148. explicit FastISel(FunctionLoweringInfo &funcInfo,
  149. const TargetLibraryInfo *libInfo);
  150. /// TargetSelectInstruction - This method is called by target-independent
  151. /// code when the normal FastISel process fails to select an instruction.
  152. /// This gives targets a chance to emit code for anything that doesn't
  153. /// fit into FastISel's framework. It returns true if it was successful.
  154. ///
  155. virtual bool
  156. TargetSelectInstruction(const Instruction *I) = 0;
  157. /// FastLowerArguments - This method is called by target-independent code to
  158. /// do target specific argument lowering. It returns true if it was
  159. /// successful.
  160. virtual bool FastLowerArguments();
  161. /// FastEmit_r - This method is called by target-independent code
  162. /// to request that an instruction with the given type and opcode
  163. /// be emitted.
  164. virtual unsigned FastEmit_(MVT VT,
  165. MVT RetVT,
  166. unsigned Opcode);
  167. /// FastEmit_r - This method is called by target-independent code
  168. /// to request that an instruction with the given type, opcode, and
  169. /// register operand be emitted.
  170. ///
  171. virtual unsigned FastEmit_r(MVT VT,
  172. MVT RetVT,
  173. unsigned Opcode,
  174. unsigned Op0, bool Op0IsKill);
  175. /// FastEmit_rr - This method is called by target-independent code
  176. /// to request that an instruction with the given type, opcode, and
  177. /// register operands be emitted.
  178. ///
  179. virtual unsigned FastEmit_rr(MVT VT,
  180. MVT RetVT,
  181. unsigned Opcode,
  182. unsigned Op0, bool Op0IsKill,
  183. unsigned Op1, bool Op1IsKill);
  184. /// FastEmit_ri - This method is called by target-independent code
  185. /// to request that an instruction with the given type, opcode, and
  186. /// register and immediate operands be emitted.
  187. ///
  188. virtual unsigned FastEmit_ri(MVT VT,
  189. MVT RetVT,
  190. unsigned Opcode,
  191. unsigned Op0, bool Op0IsKill,
  192. uint64_t Imm);
  193. /// FastEmit_rf - This method is called by target-independent code
  194. /// to request that an instruction with the given type, opcode, and
  195. /// register and floating-point immediate operands be emitted.
  196. ///
  197. virtual unsigned FastEmit_rf(MVT VT,
  198. MVT RetVT,
  199. unsigned Opcode,
  200. unsigned Op0, bool Op0IsKill,
  201. const ConstantFP *FPImm);
  202. /// FastEmit_rri - This method is called by target-independent code
  203. /// to request that an instruction with the given type, opcode, and
  204. /// register and immediate operands be emitted.
  205. ///
  206. virtual unsigned FastEmit_rri(MVT VT,
  207. MVT RetVT,
  208. unsigned Opcode,
  209. unsigned Op0, bool Op0IsKill,
  210. unsigned Op1, bool Op1IsKill,
  211. uint64_t Imm);
  212. /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
  213. /// to emit an instruction with an immediate operand using FastEmit_ri.
  214. /// If that fails, it materializes the immediate into a register and try
  215. /// FastEmit_rr instead.
  216. unsigned FastEmit_ri_(MVT VT,
  217. unsigned Opcode,
  218. unsigned Op0, bool Op0IsKill,
  219. uint64_t Imm, MVT ImmType);
  220. /// FastEmit_i - This method is called by target-independent code
  221. /// to request that an instruction with the given type, opcode, and
  222. /// immediate operand be emitted.
  223. virtual unsigned FastEmit_i(MVT VT,
  224. MVT RetVT,
  225. unsigned Opcode,
  226. uint64_t Imm);
  227. /// FastEmit_f - This method is called by target-independent code
  228. /// to request that an instruction with the given type, opcode, and
  229. /// floating-point immediate operand be emitted.
  230. virtual unsigned FastEmit_f(MVT VT,
  231. MVT RetVT,
  232. unsigned Opcode,
  233. const ConstantFP *FPImm);
  234. /// FastEmitInst_ - Emit a MachineInstr with no operands and a
  235. /// result register in the given register class.
  236. ///
  237. unsigned FastEmitInst_(unsigned MachineInstOpcode,
  238. const TargetRegisterClass *RC);
  239. /// FastEmitInst_r - Emit a MachineInstr with one register operand
  240. /// and a result register in the given register class.
  241. ///
  242. unsigned FastEmitInst_r(unsigned MachineInstOpcode,
  243. const TargetRegisterClass *RC,
  244. unsigned Op0, bool Op0IsKill);
  245. /// FastEmitInst_rr - Emit a MachineInstr with two register operands
  246. /// and a result register in the given register class.
  247. ///
  248. unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
  249. const TargetRegisterClass *RC,
  250. unsigned Op0, bool Op0IsKill,
  251. unsigned Op1, bool Op1IsKill);
  252. /// FastEmitInst_rrr - Emit a MachineInstr with three register operands
  253. /// and a result register in the given register class.
  254. ///
  255. unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
  256. const TargetRegisterClass *RC,
  257. unsigned Op0, bool Op0IsKill,
  258. unsigned Op1, bool Op1IsKill,
  259. unsigned Op2, bool Op2IsKill);
  260. /// FastEmitInst_ri - Emit a MachineInstr with a register operand,
  261. /// an immediate, and a result register in the given register class.
  262. ///
  263. unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
  264. const TargetRegisterClass *RC,
  265. unsigned Op0, bool Op0IsKill,
  266. uint64_t Imm);
  267. /// FastEmitInst_rii - Emit a MachineInstr with one register operand
  268. /// and two immediate operands.
  269. ///
  270. unsigned FastEmitInst_rii(unsigned MachineInstOpcode,
  271. const TargetRegisterClass *RC,
  272. unsigned Op0, bool Op0IsKill,
  273. uint64_t Imm1, uint64_t Imm2);
  274. /// FastEmitInst_rf - Emit a MachineInstr with two register operands
  275. /// and a result register in the given register class.
  276. ///
  277. unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
  278. const TargetRegisterClass *RC,
  279. unsigned Op0, bool Op0IsKill,
  280. const ConstantFP *FPImm);
  281. /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
  282. /// an immediate, and a result register in the given register class.
  283. ///
  284. unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
  285. const TargetRegisterClass *RC,
  286. unsigned Op0, bool Op0IsKill,
  287. unsigned Op1, bool Op1IsKill,
  288. uint64_t Imm);
  289. /// FastEmitInst_rrii - Emit a MachineInstr with two register operands,
  290. /// two immediates operands, and a result register in the given register
  291. /// class.
  292. unsigned FastEmitInst_rrii(unsigned MachineInstOpcode,
  293. const TargetRegisterClass *RC,
  294. unsigned Op0, bool Op0IsKill,
  295. unsigned Op1, bool Op1IsKill,
  296. uint64_t Imm1, uint64_t Imm2);
  297. /// FastEmitInst_i - Emit a MachineInstr with a single immediate
  298. /// operand, and a result register in the given register class.
  299. unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
  300. const TargetRegisterClass *RC,
  301. uint64_t Imm);
  302. /// FastEmitInst_ii - Emit a MachineInstr with a two immediate operands.
  303. unsigned FastEmitInst_ii(unsigned MachineInstrOpcode,
  304. const TargetRegisterClass *RC,
  305. uint64_t Imm1, uint64_t Imm2);
  306. /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
  307. /// from a specified index of a superregister to a specified type.
  308. unsigned FastEmitInst_extractsubreg(MVT RetVT,
  309. unsigned Op0, bool Op0IsKill,
  310. uint32_t Idx);
  311. /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
  312. /// with all but the least significant bit set to zero.
  313. unsigned FastEmitZExtFromI1(MVT VT,
  314. unsigned Op0, bool Op0IsKill);
  315. /// FastEmitBranch - Emit an unconditional branch to the given block,
  316. /// unless it is the immediate (fall-through) successor, and update
  317. /// the CFG.
  318. void FastEmitBranch(MachineBasicBlock *MBB, DebugLoc DL);
  319. void UpdateValueMap(const Value* I, unsigned Reg, unsigned NumRegs = 1);
  320. unsigned createResultReg(const TargetRegisterClass *RC);
  321. /// TargetMaterializeConstant - Emit a constant in a register using
  322. /// target-specific logic, such as constant pool loads.
  323. virtual unsigned TargetMaterializeConstant(const Constant* C) {
  324. return 0;
  325. }
  326. /// TargetMaterializeAlloca - Emit an alloca address in a register using
  327. /// target-specific logic.
  328. virtual unsigned TargetMaterializeAlloca(const AllocaInst* C) {
  329. return 0;
  330. }
  331. virtual unsigned TargetMaterializeFloatZero(const ConstantFP* CF) {
  332. return 0;
  333. }
  334. private:
  335. bool SelectBinaryOp(const User *I, unsigned ISDOpcode);
  336. bool SelectFNeg(const User *I);
  337. bool SelectGetElementPtr(const User *I);
  338. bool SelectCall(const User *I);
  339. bool SelectBitCast(const User *I);
  340. bool SelectCast(const User *I, unsigned Opcode);
  341. bool SelectExtractValue(const User *I);
  342. bool SelectInsertValue(const User *I);
  343. /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
  344. /// Emit code to ensure constants are copied into registers when needed.
  345. /// Remember the virtual registers that need to be added to the Machine PHI
  346. /// nodes as input. We cannot just directly add them, because expansion
  347. /// might result in multiple MBB's for one BB. As such, the start of the
  348. /// BB might correspond to a different MBB than the end.
  349. bool HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
  350. /// materializeRegForValue - Helper for getRegForVale. This function is
  351. /// called when the value isn't already available in a register and must
  352. /// be materialized with new instructions.
  353. unsigned materializeRegForValue(const Value *V, MVT VT);
  354. /// flushLocalValueMap - clears LocalValueMap and moves the area for the
  355. /// new local variables to the beginning of the block. It helps to avoid
  356. /// spilling cached variables across heavy instructions like calls.
  357. void flushLocalValueMap();
  358. /// hasTrivialKill - Test whether the given value has exactly one use.
  359. bool hasTrivialKill(const Value *V) const;
  360. };
  361. }
  362. #endif