1//===---- RISCVISelDAGToDAG.h - A dag to dag inst selector for RISC-V -----===//
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 RISC-V target.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_RISCV_RISCVISELDAGTODAG_H
14#define LLVM_LIB_TARGET_RISCV_RISCVISELDAGTODAG_H
15
16#include "RISCV.h"
17#include "RISCVTargetMachine.h"
18#include "llvm/CodeGen/SelectionDAGISel.h"
19#include "llvm/Support/KnownBits.h"
20
21// RISC-V specific code to select RISC-V machine instructions for
22// SelectionDAG operations.
23namespace llvm {
24class RISCVDAGToDAGISel : public SelectionDAGISel {
25 const RISCVSubtarget *Subtarget = nullptr;
26
27public:
28 RISCVDAGToDAGISel() = delete;
29
30 explicit RISCVDAGToDAGISel(RISCVTargetMachine &TargetMachine,
31 CodeGenOptLevel OptLevel)
32 : SelectionDAGISel(TargetMachine, OptLevel) {}
33
34 bool runOnMachineFunction(MachineFunction &MF) override {
35 Subtarget = &MF.getSubtarget<RISCVSubtarget>();
36 return SelectionDAGISel::runOnMachineFunction(mf&: MF);
37 }
38
39 void PreprocessISelDAG() override;
40 void PostprocessISelDAG() override;
41
42 void Select(SDNode *Node) override;
43
44 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
45 InlineAsm::ConstraintCode ConstraintID,
46 std::vector<SDValue> &OutOps) override;
47
48 bool areOffsetsWithinAlignment(SDValue Addr, Align Alignment);
49
50 bool SelectAddrFrameIndex(SDValue Addr, SDValue &Base, SDValue &Offset);
51 bool SelectAddrRegImm(SDValue Addr, SDValue &Base, SDValue &Offset);
52 bool SelectAddrRegImm9(SDValue Addr, SDValue &Base, SDValue &Offset);
53 bool SelectAddrRegImmLsb00000(SDValue Addr, SDValue &Base, SDValue &Offset);
54
55 bool SelectAddrRegRegScale(SDValue Addr, unsigned MaxShiftAmount,
56 SDValue &Base, SDValue &Index, SDValue &Scale);
57
58 template <unsigned MaxShift>
59 bool SelectAddrRegRegScale(SDValue Addr, SDValue &Base, SDValue &Index,
60 SDValue &Scale) {
61 return SelectAddrRegRegScale(Addr, MaxShiftAmount: MaxShift, Base, Index, Scale);
62 }
63
64 bool SelectAddrRegZextRegScale(SDValue Addr, unsigned MaxShiftAmount,
65 unsigned Bits, SDValue &Base, SDValue &Index,
66 SDValue &Scale);
67
68 template <unsigned MaxShift, unsigned Bits>
69 bool SelectAddrRegZextRegScale(SDValue Addr, SDValue &Base, SDValue &Index,
70 SDValue &Scale) {
71 return SelectAddrRegZextRegScale(Addr, MaxShiftAmount: MaxShift, Bits, Base, Index, Scale);
72 }
73
74 bool SelectAddrRegReg(SDValue Addr, SDValue &Base, SDValue &Offset);
75
76 bool tryShrinkShlLogicImm(SDNode *Node);
77 bool trySignedBitfieldExtract(SDNode *Node);
78 bool trySignedBitfieldInsertInSign(SDNode *Node);
79 bool tryUnsignedBitfieldExtract(SDNode *Node, const SDLoc &DL, MVT VT,
80 SDValue X, unsigned Msb, unsigned Lsb);
81 bool tryUnsignedBitfieldInsertInZero(SDNode *Node, const SDLoc &DL, MVT VT,
82 SDValue X, unsigned Msb, unsigned Lsb);
83 bool tryIndexedLoad(SDNode *Node);
84 bool tryWideningMulAcc(SDNode *Node, const SDLoc &DL);
85
86 bool selectShiftMask(SDValue N, unsigned ShiftWidth, SDValue &ShAmt);
87 bool selectShiftMaskXLen(SDValue N, SDValue &ShAmt) {
88 return selectShiftMask(N, ShiftWidth: Subtarget->getXLen(), ShAmt);
89 }
90 template <unsigned Size> bool selectShiftMask(SDValue N, SDValue &ShAmt) {
91 return selectShiftMask(N, ShiftWidth: Size, ShAmt);
92 }
93
94 bool selectSETCC(SDValue N, ISD::CondCode ExpectedCCVal, SDValue &Val);
95 bool selectSETNE(SDValue N, SDValue &Val) {
96 return selectSETCC(N, ExpectedCCVal: ISD::SETNE, Val);
97 }
98 bool selectSETEQ(SDValue N, SDValue &Val) {
99 return selectSETCC(N, ExpectedCCVal: ISD::SETEQ, Val);
100 }
101
102 bool selectSExtBits(SDValue N, unsigned Bits, SDValue &Val);
103 template <unsigned Bits> bool selectSExtBits(SDValue N, SDValue &Val) {
104 return selectSExtBits(N, Bits, Val);
105 }
106 bool selectZExtBits(SDValue N, unsigned Bits, SDValue &Val);
107 template <unsigned Bits> bool selectZExtBits(SDValue N, SDValue &Val) {
108 return selectZExtBits(N, Bits, Val);
109 }
110
111 bool selectSHXADDOp(SDValue N, unsigned ShAmt, SDValue &Val);
112 template <unsigned ShAmt> bool selectSHXADDOp(SDValue N, SDValue &Val) {
113 return selectSHXADDOp(N, ShAmt, Val);
114 }
115
116 bool selectSHXADD_UWOp(SDValue N, unsigned ShAmt, SDValue &Val);
117 template <unsigned ShAmt> bool selectSHXADD_UWOp(SDValue N, SDValue &Val) {
118 return selectSHXADD_UWOp(N, ShAmt, Val);
119 }
120
121 bool selectZExtImm32(SDValue N, SDValue &Val);
122 bool selectNegImm(SDValue N, SDValue &Val);
123 bool selectInvLogicImm(SDValue N, SDValue &Val);
124
125 bool orDisjoint(const SDNode *Node) const;
126 bool hasAllNBitUsers(SDNode *Node, unsigned Bits,
127 const unsigned Depth = 0) const;
128 bool hasAllBUsers(SDNode *Node) const { return hasAllNBitUsers(Node, Bits: 8); }
129 bool hasAllHUsers(SDNode *Node) const { return hasAllNBitUsers(Node, Bits: 16); }
130 bool hasAllWUsers(SDNode *Node) const { return hasAllNBitUsers(Node, Bits: 32); }
131
132 bool selectSimm5Shl2(SDValue N, SDValue &Simm5, SDValue &Shl2);
133
134 bool selectVLOp(SDValue N, SDValue &VL);
135
136 bool selectVSplat(SDValue N, SDValue &SplatVal);
137 bool selectVSplatSimm5(SDValue N, SDValue &SplatVal);
138 bool selectVSplatUimm(SDValue N, unsigned Bits, SDValue &SplatVal);
139 template <unsigned Bits> bool selectVSplatUimmBits(SDValue N, SDValue &Val) {
140 return selectVSplatUimm(N, Bits, SplatVal&: Val);
141 }
142 bool selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal);
143 bool selectVSplatSimm5Plus1NoDec(SDValue N, SDValue &SplatVal);
144 bool selectVSplatSimm5Plus1NonZero(SDValue N, SDValue &SplatVal);
145 bool selectVSplatImm64Neg(SDValue N, SDValue &SplatVal);
146 // Matches the splat of a value which can be extended or truncated, such that
147 // only the bottom 8 bits are preserved.
148 bool selectLow8BitsVSplat(SDValue N, SDValue &SplatVal);
149 bool selectScalarFPAsInt(SDValue N, SDValue &Imm);
150
151 bool selectRVVSimm5(SDValue N, unsigned Width, SDValue &Imm);
152 template <unsigned Width> bool selectRVVSimm5(SDValue N, SDValue &Imm) {
153 return selectRVVSimm5(N, Width, Imm);
154 }
155
156 bool selectVMNOTOp(SDValue N, SDValue &Res);
157 bool selectVMNOT_VLOp(SDNode *Parent, SDValue N, SDValue &Res);
158
159 void addVectorLoadStoreOperands(SDNode *Node, unsigned SEWImm,
160 const SDLoc &DL, unsigned CurOp,
161 bool IsMasked, bool IsStridedOrIndexed,
162 SmallVectorImpl<SDValue> &Operands,
163 bool IsLoad = false, MVT *IndexVT = nullptr);
164
165 void selectVLSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsStrided);
166 void selectVLSEGFF(SDNode *Node, unsigned NF, bool IsMasked);
167 void selectVLXSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsOrdered);
168 void selectVSSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsStrided);
169 void selectVSXSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsOrdered);
170
171 void selectVSETVLI(SDNode *Node);
172 void selectXSfmmVSET(SDNode *Node);
173
174 void selectSF_VC_X_SE(SDNode *Node);
175
176 // Return the RISC-V condition code that matches the given DAG integer
177 // condition code. The CondCode must be one of those supported by the RISC-V
178 // ISA (see translateSetCCForBranch).
179 static RISCVCC::CondCode getRISCVCCForIntCC(ISD::CondCode CC) {
180 switch (CC) {
181 default:
182 llvm_unreachable("Unsupported CondCode");
183 case ISD::SETEQ:
184 return RISCVCC::COND_EQ;
185 case ISD::SETNE:
186 return RISCVCC::COND_NE;
187 case ISD::SETLT:
188 return RISCVCC::COND_LT;
189 case ISD::SETGE:
190 return RISCVCC::COND_GE;
191 case ISD::SETULT:
192 return RISCVCC::COND_LTU;
193 case ISD::SETUGE:
194 return RISCVCC::COND_GEU;
195 }
196 }
197
198// Include the pieces autogenerated from the target description.
199#define GET_DAGISEL_DECL
200#include "RISCVGenDAGISel.inc"
201
202private:
203 bool doPeepholeSExtW(SDNode *Node);
204 bool doPeepholeMaskedRVV(MachineSDNode *Node);
205 bool doPeepholeNoRegPassThru();
206 bool performCombineVMergeAndVOps(SDNode *N);
207 bool selectImm64IfCheaper(int64_t Imm, int64_t OrigImm, SDValue N,
208 SDValue &Val);
209};
210
211class RISCVDAGToDAGISelLegacy : public SelectionDAGISelLegacy {
212public:
213 static char ID;
214 explicit RISCVDAGToDAGISelLegacy(RISCVTargetMachine &TargetMachine,
215 CodeGenOptLevel OptLevel);
216};
217
218} // namespace llvm
219
220#endif
221