Skip to content

Commit 6bdabcf

Browse files
author
Eli Friedman
committed
[CodeGen] Fix forward scan in MachineBasicBlock::computeRegisterLiveness.
The scan was incorrectly skipping the first instruction, so a register could appear to be dead when it was actually live. This eventually leads to a machine verifier failure and miscompile in arm-ldst-opt. Differential Revision: https://reviews.llvm.org/D54491 llvm-svn: 346821
1 parent 2d2e23e commit 6bdabcf

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,24 +1380,21 @@ MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
13801380

13811381
// Try searching forwards from Before, looking for reads or defs.
13821382
const_iterator I(Before);
1383-
// If this is the last insn in the block, don't search forwards.
1384-
if (I != end()) {
1385-
for (++I; I != end() && N > 0; ++I) {
1386-
if (I->isDebugInstr())
1387-
continue;
1383+
for (; I != end() && N > 0; ++I) {
1384+
if (I->isDebugInstr())
1385+
continue;
13881386

1389-
--N;
1387+
--N;
13901388

1391-
MachineOperandIteratorBase::PhysRegInfo Info =
1392-
ConstMIOperands(*I).analyzePhysReg(Reg, TRI);
1389+
MachineOperandIteratorBase::PhysRegInfo Info =
1390+
ConstMIOperands(*I).analyzePhysReg(Reg, TRI);
13931391

1394-
// Register is live when we read it here.
1395-
if (Info.Read)
1396-
return LQR_Live;
1397-
// Register is dead if we can fully overwrite or clobber it here.
1398-
if (Info.FullyDefined || Info.Clobbered)
1399-
return LQR_Dead;
1400-
}
1392+
// Register is live when we read it here.
1393+
if (Info.Read)
1394+
return LQR_Live;
1395+
// Register is dead if we can fully overwrite or clobber it here.
1396+
if (Info.FullyDefined || Info.Clobbered)
1397+
return LQR_Dead;
14011398
}
14021399

14031400
// If we reached the end, it is safe to clobber Reg at the end of a block of
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# RUN: llc -mtriple=thumbv6m--eabi -verify-machineinstrs -run-pass=arm-ldst-opt %s -o - | FileCheck %s
2+
3+
# Make sure bb.0 isn't transformed: it would incorrectly clobber CPSR.
4+
#
5+
# Make sure bb.1 is transformed, so the test doesn't accidentally break.
6+
7+
# CHECK-LABEL: bb.0:
8+
# CHECK: renamable $r0 = tLDRi renamable $r4, 0, 14, $noreg :: (load 4)
9+
# CHECK: renamable $r1 = tLDRi renamable $r4, 1, 14, $noreg :: (load 4)
10+
11+
# CHECK-LABEL: bb.1:
12+
# CHECK: $r4 = tLDMIA_UPD $r4, 14, $noreg, def $r0, def $r1
13+
# CHECK: $r4, dead $cpsr = tSUBi8 $r4, 8, 14, $noreg
14+
15+
name: foo
16+
tracksRegLiveness: true
17+
body: |
18+
bb.0:
19+
liveins: $r2, $r4
20+
renamable $r0 = tLDRi renamable $r2, 4, 14, $noreg :: (load 4)
21+
dead renamable $r0, $cpsr = tADDi3 killed renamable $r0, 1, 14, $noreg
22+
renamable $r0 = tLDRi renamable $r4, 0, 14, $noreg :: (load 4)
23+
renamable $r1 = tLDRi renamable $r4, 1, 14, $noreg :: (load 4)
24+
tBcc %bb.1, 0, killed $cpsr
25+
bb.1:
26+
liveins: $r2, $r4
27+
renamable $r0 = tLDRi renamable $r2, 4, 14, $noreg :: (load 4)
28+
dead renamable $r0, $cpsr = tADDi3 killed renamable $r0, 1, 14, $noreg
29+
renamable $r0 = tLDRi renamable $r4, 0, 14, $noreg :: (load 4)
30+
renamable $r1 = tLDRi renamable $r4, 1, 14, $noreg :: (load 4)
31+
bb.2:
32+
liveins: $r4
33+
TRAP

0 commit comments

Comments
 (0)