SelectionDAGISel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===//
  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 implements the SelectionDAGISel class, which is used as the common
  11. // base class for SelectionDAG-based instruction selectors.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_SELECTIONDAGISEL_H
  15. #define LLVM_CODEGEN_SELECTIONDAGISEL_H
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/SelectionDAG.h"
  18. #include "llvm/IR/BasicBlock.h"
  19. #include "llvm/Pass.h"
  20. #include "llvm/Target/TargetSubtargetInfo.h"
  21. namespace llvm {
  22. class FastISel;
  23. class SelectionDAGBuilder;
  24. class SDValue;
  25. class MachineRegisterInfo;
  26. class MachineBasicBlock;
  27. class MachineFunction;
  28. class MachineInstr;
  29. class TargetLowering;
  30. class TargetLibraryInfo;
  31. class FunctionLoweringInfo;
  32. class ScheduleHazardRecognizer;
  33. class GCFunctionInfo;
  34. class ScheduleDAGSDNodes;
  35. class LoadInst;
  36. /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
  37. /// pattern-matching instruction selectors.
  38. class SelectionDAGISel : public MachineFunctionPass {
  39. public:
  40. TargetMachine &TM;
  41. const TargetLibraryInfo *LibInfo;
  42. FunctionLoweringInfo *FuncInfo;
  43. MachineFunction *MF;
  44. MachineRegisterInfo *RegInfo;
  45. SelectionDAG *CurDAG;
  46. SelectionDAGBuilder *SDB;
  47. AliasAnalysis *AA;
  48. GCFunctionInfo *GFI;
  49. CodeGenOpt::Level OptLevel;
  50. const TargetInstrInfo *TII;
  51. const TargetLowering *TLI;
  52. static char ID;
  53. explicit SelectionDAGISel(TargetMachine &tm,
  54. CodeGenOpt::Level OL = CodeGenOpt::Default);
  55. ~SelectionDAGISel() override;
  56. const TargetLowering *getTargetLowering() const { return TLI; }
  57. void getAnalysisUsage(AnalysisUsage &AU) const override;
  58. bool runOnMachineFunction(MachineFunction &MF) override;
  59. virtual void EmitFunctionEntryCode() {}
  60. /// PreprocessISelDAG - This hook allows targets to hack on the graph before
  61. /// instruction selection starts.
  62. virtual void PreprocessISelDAG() {}
  63. /// PostprocessISelDAG() - This hook allows the target to hack on the graph
  64. /// right after selection.
  65. virtual void PostprocessISelDAG() {}
  66. /// Select - Main hook targets implement to select a node.
  67. virtual SDNode *Select(SDNode *N) = 0;
  68. /// SelectInlineAsmMemoryOperand - Select the specified address as a target
  69. /// addressing mode, according to the specified constraint. If this does
  70. /// not match or is not implemented, return true. The resultant operands
  71. /// (which will appear in the machine instruction) should be added to the
  72. /// OutOps vector.
  73. virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
  74. unsigned ConstraintID,
  75. std::vector<SDValue> &OutOps) {
  76. return true;
  77. }
  78. /// IsProfitableToFold - Returns true if it's profitable to fold the specific
  79. /// operand node N of U during instruction selection that starts at Root.
  80. virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
  81. /// IsLegalToFold - Returns true if the specific operand node N of
  82. /// U can be folded during instruction selection that starts at Root.
  83. /// FIXME: This is a static member function because the MSP430/X86
  84. /// targets, which uses it during isel. This could become a proper member.
  85. static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
  86. CodeGenOpt::Level OptLevel,
  87. bool IgnoreChains = false);
  88. // Opcodes used by the DAG state machine:
  89. enum BuiltinOpcodes {
  90. OPC_Scope,
  91. OPC_RecordNode,
  92. OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3,
  93. OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
  94. OPC_RecordMemRef,
  95. OPC_CaptureGlueInput,
  96. OPC_MoveChild,
  97. OPC_MoveParent,
  98. OPC_CheckSame,
  99. OPC_CheckChild0Same, OPC_CheckChild1Same,
  100. OPC_CheckChild2Same, OPC_CheckChild3Same,
  101. OPC_CheckPatternPredicate,
  102. OPC_CheckPredicate,
  103. OPC_CheckOpcode,
  104. OPC_SwitchOpcode,
  105. OPC_CheckType,
  106. OPC_SwitchType,
  107. OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
  108. OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
  109. OPC_CheckChild6Type, OPC_CheckChild7Type,
  110. OPC_CheckInteger,
  111. OPC_CheckChild0Integer, OPC_CheckChild1Integer, OPC_CheckChild2Integer,
  112. OPC_CheckChild3Integer, OPC_CheckChild4Integer,
  113. OPC_CheckCondCode,
  114. OPC_CheckValueType,
  115. OPC_CheckComplexPat,
  116. OPC_CheckAndImm, OPC_CheckOrImm,
  117. OPC_CheckFoldableChainNode,
  118. OPC_EmitInteger,
  119. OPC_EmitRegister,
  120. OPC_EmitRegister2,
  121. OPC_EmitConvertToTarget,
  122. OPC_EmitMergeInputChains,
  123. OPC_EmitMergeInputChains1_0,
  124. OPC_EmitMergeInputChains1_1,
  125. OPC_EmitCopyToReg,
  126. OPC_EmitNodeXForm,
  127. OPC_EmitNode,
  128. OPC_MorphNodeTo,
  129. OPC_MarkGlueResults,
  130. OPC_CompleteMatch
  131. };
  132. enum {
  133. OPFL_None = 0, // Node has no chain or glue input and isn't variadic.
  134. OPFL_Chain = 1, // Node has a chain input.
  135. OPFL_GlueInput = 2, // Node has a glue input.
  136. OPFL_GlueOutput = 4, // Node has a glue output.
  137. OPFL_MemRefs = 8, // Node gets accumulated MemRefs.
  138. OPFL_Variadic0 = 1<<4, // Node is variadic, root has 0 fixed inputs.
  139. OPFL_Variadic1 = 2<<4, // Node is variadic, root has 1 fixed inputs.
  140. OPFL_Variadic2 = 3<<4, // Node is variadic, root has 2 fixed inputs.
  141. OPFL_Variadic3 = 4<<4, // Node is variadic, root has 3 fixed inputs.
  142. OPFL_Variadic4 = 5<<4, // Node is variadic, root has 4 fixed inputs.
  143. OPFL_Variadic5 = 6<<4, // Node is variadic, root has 5 fixed inputs.
  144. OPFL_Variadic6 = 7<<4, // Node is variadic, root has 6 fixed inputs.
  145. OPFL_VariadicInfo = OPFL_Variadic6
  146. };
  147. /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
  148. /// number of fixed arity values that should be skipped when copying from the
  149. /// root.
  150. static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
  151. return ((Flags&OPFL_VariadicInfo) >> 4)-1;
  152. }
  153. protected:
  154. /// DAGSize - Size of DAG being instruction selected.
  155. ///
  156. unsigned DAGSize;
  157. /// ReplaceUses - replace all uses of the old node F with the use
  158. /// of the new node T.
  159. void ReplaceUses(SDValue F, SDValue T) {
  160. CurDAG->ReplaceAllUsesOfValueWith(F, T);
  161. }
  162. /// ReplaceUses - replace all uses of the old nodes F with the use
  163. /// of the new nodes T.
  164. void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
  165. CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num);
  166. }
  167. /// ReplaceUses - replace all uses of the old node F with the use
  168. /// of the new node T.
  169. void ReplaceUses(SDNode *F, SDNode *T) {
  170. CurDAG->ReplaceAllUsesWith(F, T);
  171. }
  172. /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
  173. /// by tblgen. Others should not call it.
  174. void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops, SDLoc DL);
  175. public:
  176. // Calls to these predicates are generated by tblgen.
  177. bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
  178. int64_t DesiredMaskS) const;
  179. bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
  180. int64_t DesiredMaskS) const;
  181. /// CheckPatternPredicate - This function is generated by tblgen in the
  182. /// target. It runs the specified pattern predicate and returns true if it
  183. /// succeeds or false if it fails. The number is a private implementation
  184. /// detail to the code tblgen produces.
  185. virtual bool CheckPatternPredicate(unsigned PredNo) const {
  186. llvm_unreachable("Tblgen should generate the implementation of this!");
  187. }
  188. /// CheckNodePredicate - This function is generated by tblgen in the target.
  189. /// It runs node predicate number PredNo and returns true if it succeeds or
  190. /// false if it fails. The number is a private implementation
  191. /// detail to the code tblgen produces.
  192. virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
  193. llvm_unreachable("Tblgen should generate the implementation of this!");
  194. }
  195. virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,
  196. unsigned PatternNo,
  197. SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {
  198. llvm_unreachable("Tblgen should generate the implementation of this!");
  199. }
  200. virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
  201. llvm_unreachable("Tblgen should generate this!");
  202. }
  203. SDNode *SelectCodeCommon(SDNode *NodeToMatch,
  204. const unsigned char *MatcherTable,
  205. unsigned TableSize);
  206. /// \brief Return true if complex patterns for this target can mutate the
  207. /// DAG.
  208. virtual bool ComplexPatternFuncMutatesDAG() const {
  209. return false;
  210. }
  211. private:
  212. // Calls to these functions are generated by tblgen.
  213. SDNode *Select_INLINEASM(SDNode *N);
  214. SDNode *Select_READ_REGISTER(SDNode *N);
  215. SDNode *Select_WRITE_REGISTER(SDNode *N);
  216. SDNode *Select_UNDEF(SDNode *N);
  217. void CannotYetSelect(SDNode *N);
  218. private:
  219. void DoInstructionSelection();
  220. SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
  221. ArrayRef<SDValue> Ops, unsigned EmitNodeInfo);
  222. /// Prepares the landing pad to take incoming values or do other EH
  223. /// personality specific tasks. Returns true if the block should be
  224. /// instruction selected, false if no code should be emitted for it.
  225. bool PrepareEHLandingPad();
  226. /// \brief Perform instruction selection on all basic blocks in the function.
  227. void SelectAllBasicBlocks(const Function &Fn);
  228. /// \brief Perform instruction selection on a single basic block, for
  229. /// instructions between \p Begin and \p End. \p HadTailCall will be set
  230. /// to true if a call in the block was translated as a tail call.
  231. void SelectBasicBlock(BasicBlock::const_iterator Begin,
  232. BasicBlock::const_iterator End,
  233. bool &HadTailCall);
  234. void FinishBasicBlock();
  235. void CodeGenAndEmitDAG();
  236. /// \brief Generate instructions for lowering the incoming arguments of the
  237. /// given function.
  238. void LowerArguments(const Function &F);
  239. void ComputeLiveOutVRegInfo();
  240. /// Create the scheduler. If a specific scheduler was specified
  241. /// via the SchedulerRegistry, use it, otherwise select the
  242. /// one preferred by the target.
  243. ///
  244. ScheduleDAGSDNodes *CreateScheduler();
  245. /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
  246. /// state machines that start with a OPC_SwitchOpcode node.
  247. std::vector<unsigned> OpcodeOffset;
  248. void UpdateChainsAndGlue(SDNode *NodeToMatch, SDValue InputChain,
  249. const SmallVectorImpl<SDNode*> &ChainNodesMatched,
  250. SDValue InputGlue, const SmallVectorImpl<SDNode*> &F,
  251. bool isMorphNodeTo);
  252. };
  253. }
  254. #endif /* LLVM_CODEGEN_SELECTIONDAGISEL_H */