RISCVISelDAGToDAG.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //===---- RISCVISelDAGToDAG.h - A dag to dag inst selector for RISCV ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines an instruction selector for the RISCV target.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TARGET_RISCV_RISCVISELDAGTODAG_H
  13. #define LLVM_LIB_TARGET_RISCV_RISCVISELDAGTODAG_H
  14. #include "RISCV.h"
  15. #include "RISCVTargetMachine.h"
  16. #include "llvm/CodeGen/SelectionDAGISel.h"
  17. #include "llvm/Support/KnownBits.h"
  18. // RISCV-specific code to select RISCV machine instructions for
  19. // SelectionDAG operations.
  20. namespace llvm {
  21. class RISCVDAGToDAGISel : public SelectionDAGISel {
  22. const RISCVSubtarget *Subtarget = nullptr;
  23. public:
  24. static char ID;
  25. RISCVDAGToDAGISel() = delete;
  26. explicit RISCVDAGToDAGISel(RISCVTargetMachine &TargetMachine,
  27. CodeGenOpt::Level OptLevel)
  28. : SelectionDAGISel(ID, TargetMachine, OptLevel) {}
  29. bool runOnMachineFunction(MachineFunction &MF) override {
  30. Subtarget = &MF.getSubtarget<RISCVSubtarget>();
  31. return SelectionDAGISel::runOnMachineFunction(MF);
  32. }
  33. void PreprocessISelDAG() override;
  34. void PostprocessISelDAG() override;
  35. void Select(SDNode *Node) override;
  36. bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
  37. std::vector<SDValue> &OutOps) override;
  38. bool SelectAddrFrameIndex(SDValue Addr, SDValue &Base, SDValue &Offset);
  39. bool SelectFrameAddrRegImm(SDValue Addr, SDValue &Base, SDValue &Offset);
  40. bool SelectAddrRegImm(SDValue Addr, SDValue &Base, SDValue &Offset);
  41. bool tryShrinkShlLogicImm(SDNode *Node);
  42. bool selectShiftMask(SDValue N, unsigned ShiftWidth, SDValue &ShAmt);
  43. bool selectShiftMaskXLen(SDValue N, SDValue &ShAmt) {
  44. return selectShiftMask(N, Subtarget->getXLen(), ShAmt);
  45. }
  46. bool selectShiftMask32(SDValue N, SDValue &ShAmt) {
  47. return selectShiftMask(N, 32, ShAmt);
  48. }
  49. bool selectSExti32(SDValue N, SDValue &Val);
  50. bool selectZExtBits(SDValue N, unsigned Bits, SDValue &Val);
  51. template <unsigned Bits> bool selectZExtBits(SDValue N, SDValue &Val) {
  52. return selectZExtBits(N, Bits, Val);
  53. }
  54. bool selectSHXADDOp(SDValue N, unsigned ShAmt, SDValue &Val);
  55. template <unsigned ShAmt> bool selectSHXADDOp(SDValue N, SDValue &Val) {
  56. return selectSHXADDOp(N, ShAmt, Val);
  57. }
  58. bool selectSHXADD_UWOp(SDValue N, unsigned ShAmt, SDValue &Val);
  59. template <unsigned ShAmt> bool selectSHXADD_UWOp(SDValue N, SDValue &Val) {
  60. return selectSHXADD_UWOp(N, ShAmt, Val);
  61. }
  62. bool hasAllNBitUsers(SDNode *Node, unsigned Bits,
  63. const unsigned Depth = 0) const;
  64. bool hasAllHUsers(SDNode *Node) const { return hasAllNBitUsers(Node, 16); }
  65. bool hasAllWUsers(SDNode *Node) const { return hasAllNBitUsers(Node, 32); }
  66. bool selectVLOp(SDValue N, SDValue &VL);
  67. bool selectVSplat(SDValue N, SDValue &SplatVal);
  68. bool selectVSplatSimm5(SDValue N, SDValue &SplatVal);
  69. bool selectVSplatUimm5(SDValue N, SDValue &SplatVal);
  70. bool selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal);
  71. bool selectVSplatSimm5Plus1NonZero(SDValue N, SDValue &SplatVal);
  72. bool selectRVVSimm5(SDValue N, unsigned Width, SDValue &Imm);
  73. template <unsigned Width> bool selectRVVSimm5(SDValue N, SDValue &Imm) {
  74. return selectRVVSimm5(N, Width, Imm);
  75. }
  76. void addVectorLoadStoreOperands(SDNode *Node, unsigned SEWImm,
  77. const SDLoc &DL, unsigned CurOp,
  78. bool IsMasked, bool IsStridedOrIndexed,
  79. SmallVectorImpl<SDValue> &Operands,
  80. bool IsLoad = false, MVT *IndexVT = nullptr);
  81. void selectVLSEG(SDNode *Node, bool IsMasked, bool IsStrided);
  82. void selectVLSEGFF(SDNode *Node, bool IsMasked);
  83. void selectVLXSEG(SDNode *Node, bool IsMasked, bool IsOrdered);
  84. void selectVSSEG(SDNode *Node, bool IsMasked, bool IsStrided);
  85. void selectVSXSEG(SDNode *Node, bool IsMasked, bool IsOrdered);
  86. void selectVSETVLI(SDNode *Node);
  87. // Return the RISC-V condition code that matches the given DAG integer
  88. // condition code. The CondCode must be one of those supported by the RISC-V
  89. // ISA (see translateSetCCForBranch).
  90. static RISCVCC::CondCode getRISCVCCForIntCC(ISD::CondCode CC) {
  91. switch (CC) {
  92. default:
  93. llvm_unreachable("Unsupported CondCode");
  94. case ISD::SETEQ:
  95. return RISCVCC::COND_EQ;
  96. case ISD::SETNE:
  97. return RISCVCC::COND_NE;
  98. case ISD::SETLT:
  99. return RISCVCC::COND_LT;
  100. case ISD::SETGE:
  101. return RISCVCC::COND_GE;
  102. case ISD::SETULT:
  103. return RISCVCC::COND_LTU;
  104. case ISD::SETUGE:
  105. return RISCVCC::COND_GEU;
  106. }
  107. }
  108. // Include the pieces autogenerated from the target description.
  109. #include "RISCVGenDAGISel.inc"
  110. private:
  111. bool doPeepholeSExtW(SDNode *Node);
  112. bool doPeepholeMaskedRVV(SDNode *Node);
  113. bool doPeepholeMergeVVMFold();
  114. bool performVMergeToVAdd(SDNode *N);
  115. bool performCombineVMergeAndVOps(SDNode *N, bool IsTA);
  116. };
  117. namespace RISCV {
  118. struct VLSEGPseudo {
  119. uint16_t NF : 4;
  120. uint16_t Masked : 1;
  121. uint16_t IsTU : 1;
  122. uint16_t Strided : 1;
  123. uint16_t FF : 1;
  124. uint16_t Log2SEW : 3;
  125. uint16_t LMUL : 3;
  126. uint16_t Pseudo;
  127. };
  128. struct VLXSEGPseudo {
  129. uint16_t NF : 4;
  130. uint16_t Masked : 1;
  131. uint16_t IsTU : 1;
  132. uint16_t Ordered : 1;
  133. uint16_t Log2SEW : 3;
  134. uint16_t LMUL : 3;
  135. uint16_t IndexLMUL : 3;
  136. uint16_t Pseudo;
  137. };
  138. struct VSSEGPseudo {
  139. uint16_t NF : 4;
  140. uint16_t Masked : 1;
  141. uint16_t Strided : 1;
  142. uint16_t Log2SEW : 3;
  143. uint16_t LMUL : 3;
  144. uint16_t Pseudo;
  145. };
  146. struct VSXSEGPseudo {
  147. uint16_t NF : 4;
  148. uint16_t Masked : 1;
  149. uint16_t Ordered : 1;
  150. uint16_t Log2SEW : 3;
  151. uint16_t LMUL : 3;
  152. uint16_t IndexLMUL : 3;
  153. uint16_t Pseudo;
  154. };
  155. struct VLEPseudo {
  156. uint16_t Masked : 1;
  157. uint16_t IsTU : 1;
  158. uint16_t Strided : 1;
  159. uint16_t FF : 1;
  160. uint16_t Log2SEW : 3;
  161. uint16_t LMUL : 3;
  162. uint16_t Pseudo;
  163. };
  164. struct VSEPseudo {
  165. uint16_t Masked :1;
  166. uint16_t Strided : 1;
  167. uint16_t Log2SEW : 3;
  168. uint16_t LMUL : 3;
  169. uint16_t Pseudo;
  170. };
  171. struct VLX_VSXPseudo {
  172. uint16_t Masked : 1;
  173. uint16_t IsTU : 1;
  174. uint16_t Ordered : 1;
  175. uint16_t Log2SEW : 3;
  176. uint16_t LMUL : 3;
  177. uint16_t IndexLMUL : 3;
  178. uint16_t Pseudo;
  179. };
  180. struct RISCVMaskedPseudoInfo {
  181. uint16_t MaskedPseudo;
  182. uint16_t UnmaskedPseudo;
  183. uint16_t UnmaskedTUPseudo;
  184. uint8_t MaskOpIdx;
  185. };
  186. #define GET_RISCVVSSEGTable_DECL
  187. #define GET_RISCVVLSEGTable_DECL
  188. #define GET_RISCVVLXSEGTable_DECL
  189. #define GET_RISCVVSXSEGTable_DECL
  190. #define GET_RISCVVLETable_DECL
  191. #define GET_RISCVVSETable_DECL
  192. #define GET_RISCVVLXTable_DECL
  193. #define GET_RISCVVSXTable_DECL
  194. #define GET_RISCVMaskedPseudosTable_DECL
  195. #include "RISCVGenSearchableTables.inc"
  196. } // namespace RISCV
  197. } // namespace llvm
  198. #endif