Skip to content

Commit b861ddf

Browse files
committed
[RISCV] Move the creation of VLMaxSentinel to isel. Use X0 during lowering.
The VLMaxSentinel is represented as TargetConstant, but that's included in isa<ConstantSDNode>. To keep constant VLs and VLMax separate as long as possible, use the X0 register during lowering and only convert to VLMaxSentinel during isel. Reviewed By: frasercrmck Differential Revision: https://reviews.llvm.org/D118845
1 parent 8baa076 commit b861ddf

3 files changed

Lines changed: 34 additions & 33 deletions

File tree

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ void RISCVDAGToDAGISel::PreprocessISelDAG() {
5555
unsigned Opc =
5656
VT.isInteger() ? RISCVISD::VMV_V_X_VL : RISCVISD::VFMV_V_F_VL;
5757
SDLoc DL(N);
58-
SDValue VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, DL,
59-
Subtarget->getXLenVT());
58+
SDValue VL = CurDAG->getRegister(RISCV::X0, Subtarget->getXLenVT());
6059
SDValue Result = CurDAG->getNode(Opc, DL, VT, N->getOperand(0), VL);
6160

6261
--I;
@@ -1902,14 +1901,24 @@ bool RISCVDAGToDAGISel::hasAllNBitUsers(SDNode *Node, unsigned Bits) const {
19021901
// allows us to choose betwen VSETIVLI or VSETVLI later.
19031902
bool RISCVDAGToDAGISel::selectVLOp(SDValue N, SDValue &VL) {
19041903
auto *C = dyn_cast<ConstantSDNode>(N);
1905-
if (C && isUInt<5>(C->getZExtValue()))
1904+
if (C && isUInt<5>(C->getZExtValue())) {
19061905
VL = CurDAG->getTargetConstant(C->getZExtValue(), SDLoc(N),
19071906
N->getValueType(0));
1908-
else if (C && C->isAllOnesValue() && C->getOpcode() != ISD::TargetConstant)
1907+
} else if (C && C->isAllOnesValue()) {
1908+
// Treat all ones as VLMax.
19091909
VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
19101910
N->getValueType(0));
1911-
else
1911+
} else if (isa<RegisterSDNode>(N) &&
1912+
cast<RegisterSDNode>(N)->getReg() == RISCV::X0) {
1913+
// All our VL operands use an operand that allows GPRNoX0 or an immediate
1914+
// as the register class. Convert X0 to a special immediate to pass the
1915+
// MachineVerifier. This is recognized specially by the vsetvli insertion
1916+
// pass.
1917+
VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
1918+
N->getValueType(0));
1919+
} else {
19121920
VL = N;
1921+
}
19131922

19141923
return true;
19151924
}

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ getDefaultVLOps(MVT VecVT, MVT ContainerVT, SDLoc DL, SelectionDAG &DAG,
17231723
MVT XLenVT = Subtarget.getXLenVT();
17241724
SDValue VL = VecVT.isFixedLengthVector()
17251725
? DAG.getConstant(VecVT.getVectorNumElements(), DL, XLenVT)
1726-
: DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, XLenVT);
1726+
: DAG.getRegister(RISCV::X0, XLenVT);
17271727
MVT MaskVT = MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
17281728
SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
17291729
return {Mask, VL};
@@ -2370,14 +2370,12 @@ static SDValue splatPartsI64WithVL(const SDLoc &DL, MVT VT, SDValue Lo,
23702370
// If vl is equal to XLEN_MAX and Hi constant is equal to Lo, we could use
23712371
// vmv.v.x whose EEW = 32 to lower it.
23722372
auto *Const = dyn_cast<ConstantSDNode>(VL);
2373-
if (LoC == HiC && Const && Const->isAllOnesValue() &&
2374-
Const->getOpcode() != ISD::TargetConstant) {
2373+
if (LoC == HiC && Const && Const->isAllOnesValue()) {
23752374
MVT InterVT = MVT::getVectorVT(MVT::i32, VT.getVectorElementCount() * 2);
23762375
// TODO: if vl <= min(VLMAX), we can also do this. But we could not
23772376
// access the subtarget here now.
2378-
auto InterVec = DAG.getNode(
2379-
RISCVISD::VMV_V_X_VL, DL, InterVT, Lo,
2380-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, MVT::i32));
2377+
auto InterVec = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, InterVT, Lo,
2378+
DAG.getRegister(RISCV::X0, MVT::i32));
23812379
return DAG.getNode(ISD::BITCAST, DL, VT, InterVec);
23822380
}
23832381
}
@@ -4159,22 +4157,20 @@ SDValue RISCVTargetLowering::lowerSPLAT_VECTOR_PARTS(SDValue Op,
41594157
// If Hi constant is all the same sign bit as Lo, lower this as a custom
41604158
// node in order to try and match RVV vector/scalar instructions.
41614159
if ((LoC >> 31) == HiC)
4162-
return DAG.getNode(
4163-
RISCVISD::VMV_V_X_VL, DL, VecVT, Lo,
4164-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, MVT::i32));
4160+
return DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT, Lo,
4161+
DAG.getRegister(RISCV::X0, MVT::i32));
41654162
}
41664163

41674164
// Detect cases where Hi is (SRA Lo, 31) which means Hi is Lo sign extended.
41684165
if (Hi.getOpcode() == ISD::SRA && Hi.getOperand(0) == Lo &&
41694166
isa<ConstantSDNode>(Hi.getOperand(1)) &&
41704167
Hi.getConstantOperandVal(1) == 31)
4171-
return DAG.getNode(
4172-
RISCVISD::VMV_V_X_VL, DL, VecVT, Lo,
4173-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, MVT::i32));
4168+
return DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT, Lo,
4169+
DAG.getRegister(RISCV::X0, MVT::i32));
41744170

41754171
// Fall back to use a stack store and stride x0 vector load. Use X0 as VL.
41764172
return DAG.getNode(RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL, DL, VecVT, Lo, Hi,
4177-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, MVT::i32));
4173+
DAG.getRegister(RISCV::X0, MVT::i32));
41784174
}
41794175

41804176
// Custom-lower extensions from mask vectors by using a vselect either with 1
@@ -4206,12 +4202,10 @@ SDValue RISCVTargetLowering::lowerVectorMaskExt(SDValue Op, SelectionDAG &DAG,
42064202
SplatZero = DAG.getSplatVector(VecVT, DL, SplatZero);
42074203
SplatTrueVal = DAG.getSplatVector(VecVT, DL, SplatTrueVal);
42084204
} else {
4209-
SplatZero =
4210-
DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT, SplatZero,
4211-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, XLenVT));
4212-
SplatTrueVal =
4213-
DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT, SplatTrueVal,
4214-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, XLenVT));
4205+
SplatZero = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT, SplatZero,
4206+
DAG.getRegister(RISCV::X0, XLenVT));
4207+
SplatTrueVal = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT, SplatTrueVal,
4208+
DAG.getRegister(RISCV::X0, XLenVT));
42154209
}
42164210

42174211
return DAG.getNode(ISD::VSELECT, DL, VecVT, Src, SplatTrueVal, SplatZero);
@@ -5528,9 +5522,8 @@ SDValue RISCVTargetLowering::lowerVECTOR_REVERSE(SDValue Op,
55285522
if (!IsRV32E64)
55295523
SplatVL = DAG.getSplatVector(IntVT, DL, VLMinus1);
55305524
else
5531-
SplatVL =
5532-
DAG.getNode(RISCVISD::VMV_V_X_VL, DL, IntVT, VLMinus1,
5533-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL, XLenVT));
5525+
SplatVL = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, IntVT, VLMinus1,
5526+
DAG.getRegister(RISCV::X0, XLenVT));
55345527

55355528
SDValue VID = DAG.getNode(RISCVISD::VID_VL, DL, IntVT, Mask, VL);
55365529
SDValue Indices =
@@ -8255,8 +8248,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
82558248
SDLoc DL(N);
82568249
EVT VT = N->getValueType(0);
82578250
ShAmt = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VT, ShAmt.getOperand(0),
8258-
DAG.getTargetConstant(RISCV::VLMaxSentinel, DL,
8259-
Subtarget.getXLenVT()));
8251+
DAG.getRegister(RISCV::X0, Subtarget.getXLenVT()));
82608252
return DAG.getNode(N->getOpcode(), DL, VT, N->getOperand(0), ShAmt);
82618253
}
82628254
break;

llvm/lib/Target/RISCV/RISCVInstrInfoVVLPatterns.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,8 @@ multiclass VPatBinarySDNodeExt_V_WV<SDNode op, PatFrags extop, string instructio
649649
(riscv_trunc_vector_vl
650650
(op (vti.Wti.Vector vti.Wti.RegClass:$rs2),
651651
(vti.Wti.Vector (extop (vti.Vti.Vector vti.Vti.RegClass:$rs1)))),
652-
(riscv_vmset_vl VLMax),
653-
VLMax)),
652+
(riscv_vmset_vl X0),
653+
X0)),
654654
(!cast<Instruction>(instruction_name#"_WV_"#vti.Vti.LMul.MX)
655655
vti.Wti.RegClass:$rs2, vti.Vti.RegClass:$rs1,
656656
vti.Vti.AVL, vti.Vti.Log2SEW)>;
@@ -664,8 +664,8 @@ multiclass VPatBinarySDNodeExt_V_WX<SDNode op, PatFrags extop, string instructio
664664
(riscv_trunc_vector_vl
665665
(op (vti.Wti.Vector vti.Wti.RegClass:$rs2),
666666
(vti.Wti.Vector (extop (vti.Vti.Vector (SplatPat GPR:$rs1))))),
667-
(riscv_vmset_vl VLMax),
668-
VLMax)),
667+
(riscv_vmset_vl X0),
668+
X0)),
669669
(!cast<Instruction>(instruction_name#"_WX_"#vti.Vti.LMul.MX)
670670
vti.Wti.RegClass:$rs2, GPR:$rs1,
671671
vti.Vti.AVL, vti.Vti.Log2SEW)>;

0 commit comments

Comments
 (0)