SelectionDAGISel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- 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 file implements the SelectionDAGISel class, which is used as the common
  15. // base class for SelectionDAG-based instruction selectors.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_SELECTIONDAGISEL_H
  19. #define LLVM_CODEGEN_SELECTIONDAGISEL_H
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/SelectionDAG.h"
  22. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  23. #include "llvm/IR/BasicBlock.h"
  24. #include <memory>
  25. namespace llvm {
  26. class AAResults;
  27. class SelectionDAGBuilder;
  28. class SDValue;
  29. class MachineRegisterInfo;
  30. class MachineFunction;
  31. class OptimizationRemarkEmitter;
  32. class TargetLowering;
  33. class TargetLibraryInfo;
  34. class FunctionLoweringInfo;
  35. class SwiftErrorValueTracking;
  36. class GCFunctionInfo;
  37. class ScheduleDAGSDNodes;
  38. /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
  39. /// pattern-matching instruction selectors.
  40. class SelectionDAGISel : public MachineFunctionPass {
  41. public:
  42. TargetMachine &TM;
  43. const TargetLibraryInfo *LibInfo;
  44. std::unique_ptr<FunctionLoweringInfo> FuncInfo;
  45. SwiftErrorValueTracking *SwiftError;
  46. MachineFunction *MF;
  47. MachineRegisterInfo *RegInfo;
  48. SelectionDAG *CurDAG;
  49. std::unique_ptr<SelectionDAGBuilder> SDB;
  50. AAResults *AA = nullptr;
  51. GCFunctionInfo *GFI = nullptr;
  52. CodeGenOpt::Level OptLevel;
  53. const TargetInstrInfo *TII;
  54. const TargetLowering *TLI;
  55. bool FastISelFailed;
  56. SmallPtrSet<const Instruction *, 4> ElidedArgCopyInstrs;
  57. bool UseInstrRefDebugInfo = false;
  58. /// Current optimization remark emitter.
  59. /// Used to report things like combines and FastISel failures.
  60. std::unique_ptr<OptimizationRemarkEmitter> ORE;
  61. static char ID;
  62. explicit SelectionDAGISel(TargetMachine &tm,
  63. CodeGenOpt::Level OL = CodeGenOpt::Default);
  64. ~SelectionDAGISel() override;
  65. const TargetLowering *getTargetLowering() const { return TLI; }
  66. void getAnalysisUsage(AnalysisUsage &AU) const override;
  67. bool runOnMachineFunction(MachineFunction &MF) override;
  68. virtual void emitFunctionEntryCode() {}
  69. /// PreprocessISelDAG - This hook allows targets to hack on the graph before
  70. /// instruction selection starts.
  71. virtual void PreprocessISelDAG() {}
  72. /// PostprocessISelDAG() - This hook allows the target to hack on the graph
  73. /// right after selection.
  74. virtual void PostprocessISelDAG() {}
  75. /// Main hook for targets to transform nodes into machine nodes.
  76. virtual void Select(SDNode *N) = 0;
  77. /// SelectInlineAsmMemoryOperand - Select the specified address as a target
  78. /// addressing mode, according to the specified constraint. If this does
  79. /// not match or is not implemented, return true. The resultant operands
  80. /// (which will appear in the machine instruction) should be added to the
  81. /// OutOps vector.
  82. virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
  83. unsigned ConstraintID,
  84. std::vector<SDValue> &OutOps) {
  85. return true;
  86. }
  87. /// IsProfitableToFold - Returns true if it's profitable to fold the specific
  88. /// operand node N of U during instruction selection that starts at Root.
  89. virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
  90. /// IsLegalToFold - Returns true if the specific operand node N of
  91. /// U can be folded during instruction selection that starts at Root.
  92. /// FIXME: This is a static member function because the MSP430/X86
  93. /// targets, which uses it during isel. This could become a proper member.
  94. static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
  95. CodeGenOpt::Level OptLevel,
  96. bool IgnoreChains = false);
  97. static void InvalidateNodeId(SDNode *N);
  98. static int getUninvalidatedNodeId(SDNode *N);
  99. static void EnforceNodeIdInvariant(SDNode *N);
  100. // Opcodes used by the DAG state machine:
  101. enum BuiltinOpcodes {
  102. OPC_Scope,
  103. OPC_RecordNode,
  104. OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3,
  105. OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
  106. OPC_RecordMemRef,
  107. OPC_CaptureGlueInput,
  108. OPC_MoveChild,
  109. OPC_MoveChild0, OPC_MoveChild1, OPC_MoveChild2, OPC_MoveChild3,
  110. OPC_MoveChild4, OPC_MoveChild5, OPC_MoveChild6, OPC_MoveChild7,
  111. OPC_MoveParent,
  112. OPC_CheckSame,
  113. OPC_CheckChild0Same, OPC_CheckChild1Same,
  114. OPC_CheckChild2Same, OPC_CheckChild3Same,
  115. OPC_CheckPatternPredicate,
  116. OPC_CheckPredicate,
  117. OPC_CheckPredicateWithOperands,
  118. OPC_CheckOpcode,
  119. OPC_SwitchOpcode,
  120. OPC_CheckType,
  121. OPC_CheckTypeRes,
  122. OPC_SwitchType,
  123. OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
  124. OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
  125. OPC_CheckChild6Type, OPC_CheckChild7Type,
  126. OPC_CheckInteger,
  127. OPC_CheckChild0Integer, OPC_CheckChild1Integer, OPC_CheckChild2Integer,
  128. OPC_CheckChild3Integer, OPC_CheckChild4Integer,
  129. OPC_CheckCondCode, OPC_CheckChild2CondCode,
  130. OPC_CheckValueType,
  131. OPC_CheckComplexPat,
  132. OPC_CheckAndImm, OPC_CheckOrImm,
  133. OPC_CheckImmAllOnesV,
  134. OPC_CheckImmAllZerosV,
  135. OPC_CheckFoldableChainNode,
  136. OPC_EmitInteger,
  137. OPC_EmitStringInteger,
  138. OPC_EmitRegister,
  139. OPC_EmitRegister2,
  140. OPC_EmitConvertToTarget,
  141. OPC_EmitMergeInputChains,
  142. OPC_EmitMergeInputChains1_0,
  143. OPC_EmitMergeInputChains1_1,
  144. OPC_EmitMergeInputChains1_2,
  145. OPC_EmitCopyToReg,
  146. OPC_EmitCopyToReg2,
  147. OPC_EmitNodeXForm,
  148. OPC_EmitNode,
  149. // Space-optimized forms that implicitly encode number of result VTs.
  150. OPC_EmitNode0, OPC_EmitNode1, OPC_EmitNode2,
  151. OPC_MorphNodeTo,
  152. // Space-optimized forms that implicitly encode number of result VTs.
  153. OPC_MorphNodeTo0, OPC_MorphNodeTo1, OPC_MorphNodeTo2,
  154. OPC_CompleteMatch,
  155. // Contains offset in table for pattern being selected
  156. OPC_Coverage
  157. };
  158. enum {
  159. OPFL_None = 0, // Node has no chain or glue input and isn't variadic.
  160. OPFL_Chain = 1, // Node has a chain input.
  161. OPFL_GlueInput = 2, // Node has a glue input.
  162. OPFL_GlueOutput = 4, // Node has a glue output.
  163. OPFL_MemRefs = 8, // Node gets accumulated MemRefs.
  164. OPFL_Variadic0 = 1<<4, // Node is variadic, root has 0 fixed inputs.
  165. OPFL_Variadic1 = 2<<4, // Node is variadic, root has 1 fixed inputs.
  166. OPFL_Variadic2 = 3<<4, // Node is variadic, root has 2 fixed inputs.
  167. OPFL_Variadic3 = 4<<4, // Node is variadic, root has 3 fixed inputs.
  168. OPFL_Variadic4 = 5<<4, // Node is variadic, root has 4 fixed inputs.
  169. OPFL_Variadic5 = 6<<4, // Node is variadic, root has 5 fixed inputs.
  170. OPFL_Variadic6 = 7<<4, // Node is variadic, root has 6 fixed inputs.
  171. OPFL_VariadicInfo = OPFL_Variadic6
  172. };
  173. /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
  174. /// number of fixed arity values that should be skipped when copying from the
  175. /// root.
  176. static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
  177. return ((Flags&OPFL_VariadicInfo) >> 4)-1;
  178. }
  179. protected:
  180. /// DAGSize - Size of DAG being instruction selected.
  181. ///
  182. unsigned DAGSize = 0;
  183. /// ReplaceUses - replace all uses of the old node F with the use
  184. /// of the new node T.
  185. void ReplaceUses(SDValue F, SDValue T) {
  186. CurDAG->ReplaceAllUsesOfValueWith(F, T);
  187. EnforceNodeIdInvariant(T.getNode());
  188. }
  189. /// ReplaceUses - replace all uses of the old nodes F with the use
  190. /// of the new nodes T.
  191. void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
  192. CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num);
  193. for (unsigned i = 0; i < Num; ++i)
  194. EnforceNodeIdInvariant(T[i].getNode());
  195. }
  196. /// ReplaceUses - replace all uses of the old node F with the use
  197. /// of the new node T.
  198. void ReplaceUses(SDNode *F, SDNode *T) {
  199. CurDAG->ReplaceAllUsesWith(F, T);
  200. EnforceNodeIdInvariant(T);
  201. }
  202. /// Replace all uses of \c F with \c T, then remove \c F from the DAG.
  203. void ReplaceNode(SDNode *F, SDNode *T) {
  204. CurDAG->ReplaceAllUsesWith(F, T);
  205. EnforceNodeIdInvariant(T);
  206. CurDAG->RemoveDeadNode(F);
  207. }
  208. /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
  209. /// by tblgen. Others should not call it.
  210. void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops,
  211. const SDLoc &DL);
  212. /// getPatternForIndex - Patterns selected by tablegen during ISEL
  213. virtual StringRef getPatternForIndex(unsigned index) {
  214. llvm_unreachable("Tblgen should generate the implementation of this!");
  215. }
  216. /// getIncludePathForIndex - get the td source location of pattern instantiation
  217. virtual StringRef getIncludePathForIndex(unsigned index) {
  218. llvm_unreachable("Tblgen should generate the implementation of this!");
  219. }
  220. bool shouldOptForSize(const MachineFunction *MF) const {
  221. return CurDAG->shouldOptForSize();
  222. }
  223. public:
  224. // Calls to these predicates are generated by tblgen.
  225. bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
  226. int64_t DesiredMaskS) const;
  227. bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
  228. int64_t DesiredMaskS) const;
  229. /// CheckPatternPredicate - This function is generated by tblgen in the
  230. /// target. It runs the specified pattern predicate and returns true if it
  231. /// succeeds or false if it fails. The number is a private implementation
  232. /// detail to the code tblgen produces.
  233. virtual bool CheckPatternPredicate(unsigned PredNo) const {
  234. llvm_unreachable("Tblgen should generate the implementation of this!");
  235. }
  236. /// CheckNodePredicate - This function is generated by tblgen in the target.
  237. /// It runs node predicate number PredNo and returns true if it succeeds or
  238. /// false if it fails. The number is a private implementation
  239. /// detail to the code tblgen produces.
  240. virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
  241. llvm_unreachable("Tblgen should generate the implementation of this!");
  242. }
  243. /// CheckNodePredicateWithOperands - This function is generated by tblgen in
  244. /// the target.
  245. /// It runs node predicate number PredNo and returns true if it succeeds or
  246. /// false if it fails. The number is a private implementation detail to the
  247. /// code tblgen produces.
  248. virtual bool CheckNodePredicateWithOperands(
  249. SDNode *N, unsigned PredNo,
  250. const SmallVectorImpl<SDValue> &Operands) const {
  251. llvm_unreachable("Tblgen should generate the implementation of this!");
  252. }
  253. virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,
  254. unsigned PatternNo,
  255. SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {
  256. llvm_unreachable("Tblgen should generate the implementation of this!");
  257. }
  258. virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
  259. llvm_unreachable("Tblgen should generate this!");
  260. }
  261. void SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable,
  262. unsigned TableSize);
  263. /// Return true if complex patterns for this target can mutate the
  264. /// DAG.
  265. virtual bool ComplexPatternFuncMutatesDAG() const {
  266. return false;
  267. }
  268. /// Return whether the node may raise an FP exception.
  269. bool mayRaiseFPException(SDNode *Node) const;
  270. bool isOrEquivalentToAdd(const SDNode *N) const;
  271. private:
  272. // Calls to these functions are generated by tblgen.
  273. void Select_INLINEASM(SDNode *N);
  274. void Select_READ_REGISTER(SDNode *Op);
  275. void Select_WRITE_REGISTER(SDNode *Op);
  276. void Select_UNDEF(SDNode *N);
  277. void CannotYetSelect(SDNode *N);
  278. void Select_FREEZE(SDNode *N);
  279. void Select_ARITH_FENCE(SDNode *N);
  280. private:
  281. void DoInstructionSelection();
  282. SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
  283. ArrayRef<SDValue> Ops, unsigned EmitNodeInfo);
  284. /// Prepares the landing pad to take incoming values or do other EH
  285. /// personality specific tasks. Returns true if the block should be
  286. /// instruction selected, false if no code should be emitted for it.
  287. bool PrepareEHLandingPad();
  288. /// Perform instruction selection on all basic blocks in the function.
  289. void SelectAllBasicBlocks(const Function &Fn);
  290. /// Perform instruction selection on a single basic block, for
  291. /// instructions between \p Begin and \p End. \p HadTailCall will be set
  292. /// to true if a call in the block was translated as a tail call.
  293. void SelectBasicBlock(BasicBlock::const_iterator Begin,
  294. BasicBlock::const_iterator End,
  295. bool &HadTailCall);
  296. void FinishBasicBlock();
  297. void CodeGenAndEmitDAG();
  298. /// Generate instructions for lowering the incoming arguments of the
  299. /// given function.
  300. void LowerArguments(const Function &F);
  301. void ComputeLiveOutVRegInfo();
  302. /// Create the scheduler. If a specific scheduler was specified
  303. /// via the SchedulerRegistry, use it, otherwise select the
  304. /// one preferred by the target.
  305. ///
  306. ScheduleDAGSDNodes *CreateScheduler();
  307. /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
  308. /// state machines that start with a OPC_SwitchOpcode node.
  309. std::vector<unsigned> OpcodeOffset;
  310. void UpdateChains(SDNode *NodeToMatch, SDValue InputChain,
  311. SmallVectorImpl<SDNode *> &ChainNodesMatched,
  312. bool isMorphNodeTo);
  313. };
  314. }
  315. #endif /* LLVM_CODEGEN_SELECTIONDAGISEL_H */
  316. #ifdef __GNUC__
  317. #pragma GCC diagnostic pop
  318. #endif