SelectionDAGISel.h 14 KB

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