Skip to content

Commit aefcd59

Browse files
committed
[RISCV] Teach RISCVInsertVSETVLI::needVSETVLI to handle mask register instructions better.
If the VL operand of a mask register instruction comes from an explicit vsetvli with a different VTYPE, we can still avoid needing a vsetvli as long as the SEW/LMUL ratio is the same and policy bits match. Differential Revision: https://reviews.llvm.org/D112762
1 parent 1deccd0 commit aefcd59

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ class VSETVLIInfo {
178178
return getSEWLMULRatio() == Other.getSEWLMULRatio();
179179
}
180180

181+
bool hasCompatibleVTYPE(const VSETVLIInfo &InstrInfo, bool Strict) const {
182+
// Simple case, see if full VTYPE matches.
183+
if (hasSameVTYPE(InstrInfo))
184+
return true;
185+
186+
if (Strict)
187+
return false;
188+
189+
// If this is a mask reg operation, it only cares about VLMAX.
190+
// FIXME: Mask reg operations are probably ok if "this" VLMAX is larger
191+
// than "InstrInfo".
192+
// FIXME: The policy bits can probably be ignored for mask reg operations.
193+
if (InstrInfo.MaskRegOp && hasSameVLMAX(InstrInfo) &&
194+
TailAgnostic == InstrInfo.TailAgnostic &&
195+
MaskAgnostic == InstrInfo.MaskAgnostic)
196+
return true;
197+
198+
return false;
199+
}
200+
181201
// Determine whether the vector instructions requirements represented by
182202
// InstrInfo are compatible with the previous vsetvli instruction represented
183203
// by this.
@@ -206,23 +226,15 @@ class VSETVLIInfo {
206226
if (!hasSameAVL(InstrInfo))
207227
return false;
208228

209-
// Simple case, see if full VTYPE matches.
210-
if (hasSameVTYPE(InstrInfo))
229+
if (hasCompatibleVTYPE(InstrInfo, Strict))
211230
return true;
212231

213232
// Strict matches must ensure a full VTYPE match.
214233
if (Strict)
215234
return false;
216235

217-
// If this is a mask reg operation, it only cares about VLMAX.
218-
// FIXME: Mask reg operations are probably ok if "this" VLMAX is larger
219-
// than "InstrInfo".
220-
if (InstrInfo.MaskRegOp && hasSameVLMAX(InstrInfo) &&
221-
TailAgnostic == InstrInfo.TailAgnostic &&
222-
MaskAgnostic == InstrInfo.MaskAgnostic)
223-
return true;
224-
225236
// Store instructions don't use the policy fields.
237+
// TODO: Move into hasCompatibleVTYPE?
226238
if (InstrInfo.StoreOp && VLMul == InstrInfo.VLMul && SEW == InstrInfo.SEW)
227239
return true;
228240

@@ -564,7 +576,7 @@ bool RISCVInsertVSETVLI::needVSETVLI(const VSETVLIInfo &Require,
564576
// VSETVLI here.
565577
if (!CurInfo.isUnknown() && Require.hasAVLReg() &&
566578
Require.getAVLReg().isVirtual() && !CurInfo.hasSEWLMULRatioOnly() &&
567-
Require.hasSameVTYPE(CurInfo)) {
579+
CurInfo.hasCompatibleVTYPE(Require, /*Strict*/ false)) {
568580
if (MachineInstr *DefMI = MRI->getVRegDef(Require.getAVLReg())) {
569581
if (DefMI->getOpcode() == RISCV::PseudoVSETVLI ||
570582
DefMI->getOpcode() == RISCV::PseudoVSETVLIX0 ||

llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ entry:
7777
ret <vscale x 1 x i64> %1
7878
}
7979

80-
; FIXME the second vsetvli is unnecessary.
80+
; Make sure we don't insert a vsetvli for the vmand instruction.
8181
define <vscale x 1 x i1> @test5(<vscale x 1 x i64> %0, <vscale x 1 x i64> %1, <vscale x 1 x i1> %2, i64 %avl) nounwind {
8282
; CHECK-LABEL: test5:
8383
; CHECK: # %bb.0: # %entry
8484
; CHECK-NEXT: vsetvli a0, a0, e64, m1, ta, mu
8585
; CHECK-NEXT: vmseq.vv v8, v8, v9
86-
; CHECK-NEXT: vsetvli zero, a0, e8, mf8, ta, mu
8786
; CHECK-NEXT: vmand.mm v0, v8, v0
8887
; CHECK-NEXT: ret
8988
entry:

0 commit comments

Comments
 (0)