Skip to content

Commit be5ecc3

Browse files
authored
[RISCV] Don't move source if passthru already dominates in vmv.v.v peephole (llvm#105792)
Currently we move the source down to where vmv.v.v to make sure that the new passthru dominates, but we do this even if it already does. This adds a simple local dominance check (taken from X86FastPreTileConfig.cpp) and avoids doing the move if it can. It also modifies the move to only move it to just past the passthru definition, and not all the way down to the vmv.v.v. This allows folding to succeed in some edge cases, which prevents regressions in an upcoming patch.
1 parent 001e423 commit be5ecc3

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,22 @@ static bool isSafeToMove(const MachineInstr &From, const MachineInstr &To) {
437437
return From.isSafeToMove(SawStore);
438438
}
439439

440+
/// Given A and B are in the same MBB, returns true if A comes before B.
441+
static bool dominates(MachineBasicBlock::const_iterator A,
442+
MachineBasicBlock::const_iterator B) {
443+
assert(A->getParent() == B->getParent());
444+
const MachineBasicBlock *MBB = A->getParent();
445+
auto MBBEnd = MBB->end();
446+
if (B == MBBEnd)
447+
return true;
448+
449+
MachineBasicBlock::const_iterator I = MBB->begin();
450+
for (; &*I != A && &*I != B; ++I)
451+
;
452+
453+
return &*I == A;
454+
}
455+
440456
/// If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
441457
/// into it.
442458
///
@@ -481,12 +497,15 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
481497
if (!isVLKnownLE(SrcVL, MI.getOperand(3)))
482498
return false;
483499

484-
// If Src ends up using MI's passthru/VL, move it so it can access it.
485-
// TODO: We don't need to do this if they already dominate Src.
486-
if (!SrcPassthru.isIdenticalTo(Passthru)) {
487-
if (!isSafeToMove(*Src, MI))
488-
return false;
489-
Src->moveBefore(&MI);
500+
// If the new passthru doesn't dominate Src, try to move Src so it does.
501+
if (Passthru.getReg() != RISCV::NoRegister) {
502+
MachineInstr *PassthruDef = MRI->getVRegDef(Passthru.getReg());
503+
if (PassthruDef->getParent() == Src->getParent() &&
504+
!dominates(PassthruDef, Src)) {
505+
if (!isSafeToMove(*Src, *PassthruDef->getNextNode()))
506+
return false;
507+
Src->moveBefore(PassthruDef->getNextNode());
508+
}
490509
}
491510

492511
if (SrcPassthru.getReg() != Passthru.getReg()) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
2+
# RUN: llc %s -o - -mtriple=riscv64 -mattr=+v -run-pass=riscv-vector-peephole \
3+
# RUN: -verify-machineinstrs | FileCheck %s
4+
5+
---
6+
name: move_src
7+
body: |
8+
bb.0:
9+
liveins: $v8
10+
; CHECK-LABEL: name: move_src
11+
; CHECK: liveins: $v8
12+
; CHECK-NEXT: {{ $}}
13+
; CHECK-NEXT: %passthru:vr = COPY $v8
14+
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
15+
; CHECK-NEXT: %y:gpr = ADDI $x0, 1
16+
%x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
17+
%passthru:vr = COPY $v8
18+
%y:gpr = ADDI $x0, 1
19+
%z:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5 /* e32 */, 0 /* tu, mu */
20+
...

0 commit comments

Comments
 (0)