Skip to content

Commit b952dd6

Browse files
committed
Do cross-block analysis with post-order traversal, add assert + test for unreachable blocks.
1 parent c88520f commit b952dd6

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "RISCV.h"
2020
#include "RISCVSubtarget.h"
21-
#include "llvm/ADT/SetVector.h"
21+
#include "llvm/ADT/PostOrderIterator.h"
2222
#include "llvm/CodeGen/MachineDominators.h"
2323
#include "llvm/CodeGen/MachineFunctionPass.h"
2424
#include "llvm/InitializePasses.h"
@@ -1350,21 +1350,22 @@ bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
13501350

13511351
TII = ST.getInstrInfo();
13521352

1353-
bool MadeChange = false;
1354-
for (MachineBasicBlock &MBB : MF) {
1355-
// Avoid unreachable blocks as they have degenerate dominance
1356-
if (!MDT->isReachableFromEntry(&MBB))
1357-
continue;
13581353

1359-
// For each instruction that defines a vector, compute what VL its
1360-
// downstream users demand.
1361-
for (MachineInstr &MI : reverse(MBB)) {
1354+
// For each instruction that defines a vector, compute what VL its
1355+
// downstream users demand.
1356+
for (MachineBasicBlock *MBB : post_order(&MF)) {
1357+
// Avoid unreachable blocks as they have degenerate dominance
1358+
assert(MDT->isReachableFromEntry(MBB));
1359+
for (MachineInstr &MI : reverse(*MBB)) {
13621360
if (!isCandidate(MI))
13631361
continue;
13641362
if (auto DemandedVL = checkUsers(MI))
13651363
DemandedVLs.insert({&MI, *DemandedVL});
13661364
}
1365+
}
13671366

1367+
bool MadeChange = false;
1368+
for (MachineBasicBlock &MBB : MF) {
13681369
// Then go through and see if we can reduce the VL of any instructions to
13691370
// only what's demanded.
13701371
for (auto &MI : MBB) {
@@ -1374,8 +1375,6 @@ bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
13741375
continue;
13751376
MadeChange = true;
13761377
}
1377-
1378-
DemandedVLs.clear();
13791378
}
13801379

13811380
return MadeChange;

llvm/test/CodeGen/RISCV/rvv/vl-opt.mir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,23 @@ body: |
223223
%c:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 3 /* e8 */, 0 /* tu, mu */
224224
BEQ $x1, $x0, %bb.1
225225
PseudoBR %bb.2
226+
...
227+
---
228+
name: unreachable
229+
body: |
230+
; CHECK-LABEL: name: unreachable
231+
; CHECK: bb.0:
232+
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 3 /* e8 */, 0 /* tu, mu */
233+
; CHECK-NEXT: $v8 = COPY %x
234+
; CHECK-NEXT: PseudoRET
235+
; CHECK-NEXT: {{ $}}
236+
; CHECK-NEXT: bb.1:
237+
; CHECK-NEXT: %y:vr = PseudoVADD_VV_M1 $noreg, %x, $noreg, 1, 3 /* e8 */, 0 /* tu, mu */
238+
; CHECK-NEXT: PseudoRET
239+
bb.0:
240+
%x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 3 /* e8 */, 0 /* tu, mu */
241+
$v8 = COPY %x
242+
PseudoRET
243+
bb.1:
244+
%y:vr = PseudoVADD_VV_M1 $noreg, %x, $noreg, 1, 3 /* e8 */, 0 /* tu, mu */
245+
PseudoRET

0 commit comments

Comments
 (0)