Skip to content

Commit abd4882

Browse files
committed
[PowerPC] $carry should be added to successors liveins if still alive
Fix bug reported in #116984. The fix is similar to 647e861.
1 parent c17df0a commit abd4882

File tree

5 files changed

+115
-16
lines changed

5 files changed

+115
-16
lines changed

llvm/include/llvm/CodeGen/LivePhysRegs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs);
195195
void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
196196
MachineBasicBlock &MBB);
197197

198+
/// Check if physical register \p Reg is alive after \p MBI.
199+
bool isPhysRegLiveAfter(Register Reg, MachineBasicBlock::iterator MBI);
200+
198201
/// Convenience function for recomputing live-in's for a MBB. Returns true if
199202
/// any changes were made.
200203
static inline bool recomputeLiveIns(MachineBasicBlock &MBB) {

llvm/lib/CodeGen/LivePhysRegs.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,25 @@ void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs,
338338
computeLiveIns(LiveRegs, MBB);
339339
addLiveIns(MBB, LiveRegs);
340340
}
341+
342+
bool llvm::isPhysRegLiveAfter(Register Reg, MachineBasicBlock::iterator MBI) {
343+
assert(Reg.isPhysical() && "Apply to physical register only");
344+
345+
MachineBasicBlock *MBB = MBI->getParent();
346+
// Scan forward through BB for a use/def of Reg .
347+
for (const MachineInstr &MI : llvm::make_range(std::next(MBI), MBB->end())) {
348+
if (MI.readsRegister(Reg, /*TRI=*/nullptr))
349+
return true;
350+
// If we found a def, we can stop searching.
351+
if (MI.definesRegister(Reg, /*TRI=*/nullptr))
352+
return false;
353+
}
354+
355+
// If we hit the end of the block, check whether Reg is live into a
356+
// successor.
357+
for (MachineBasicBlock *Succ : MBB->successors())
358+
if (Succ->isLiveIn(Reg))
359+
return true;
360+
361+
return false;
362+
}

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/ADT/StringRef.h"
3737
#include "llvm/CodeGen/CallingConvLower.h"
3838
#include "llvm/CodeGen/ISDOpcodes.h"
39+
#include "llvm/CodeGen/LivePhysRegs.h"
3940
#include "llvm/CodeGen/MachineBasicBlock.h"
4041
#include "llvm/CodeGen/MachineFrameInfo.h"
4142
#include "llvm/CodeGen/MachineFunction.h"
@@ -13296,6 +13297,10 @@ PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1329613297
DebugLoc dl = MI.getDebugLoc();
1329713298
F->insert(It, copy0MBB);
1329813299
F->insert(It, sinkMBB);
13300+
if (isPhysRegLiveAfter(PPC::CARRY , MI.getIterator())) {
13301+
copy0MBB->addLiveIn(PPC::CARRY);
13302+
sinkMBB->addLiveIn(PPC::CARRY);
13303+
}
1329913304

1330013305
// Set the call frame size on entry to the new basic blocks.
1330113306
// See https://reviews.llvm.org/D156113.

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/Analysis/ProfileSummaryInfo.h"
2929
#include "llvm/Analysis/VectorUtils.h"
3030
#include "llvm/CodeGen/IntrinsicLowering.h"
31+
#include "llvm/CodeGen/LivePhysRegs.h"
3132
#include "llvm/CodeGen/MachineFrameInfo.h"
3233
#include "llvm/CodeGen/MachineFunction.h"
3334
#include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -35349,22 +35350,7 @@ MVT X86TargetLowering::getPreferredSwitchConditionType(LLVMContext &Context,
3534935350
// basic block or any successors of the basic block.
3535035351
static bool isEFLAGSLiveAfter(MachineBasicBlock::iterator Itr,
3535135352
MachineBasicBlock *BB) {
35352-
// Scan forward through BB for a use/def of EFLAGS.
35353-
for (const MachineInstr &mi : llvm::make_range(std::next(Itr), BB->end())) {
35354-
if (mi.readsRegister(X86::EFLAGS, /*TRI=*/nullptr))
35355-
return true;
35356-
// If we found a def, we can stop searching.
35357-
if (mi.definesRegister(X86::EFLAGS, /*TRI=*/nullptr))
35358-
return false;
35359-
}
35360-
35361-
// If we hit the end of the block, check whether EFLAGS is live into a
35362-
// successor.
35363-
for (MachineBasicBlock *Succ : BB->successors())
35364-
if (Succ->isLiveIn(X86::EFLAGS))
35365-
return true;
35366-
35367-
return false;
35353+
return isPhysRegLiveAfter(X86::EFLAGS, Itr);
3536835354
}
3536935355

3537035356
/// Utility function to emit xbegin specifying the start of an RTM region.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -stop-after=finalize-isel -verify-machineinstrs < %s | FileCheck %s
3+
4+
target datalayout = "E-m:e-p:32:32-Fn32-i64:64-n32"
5+
target triple = "powerpc-unknown-linux-gnu"
6+
7+
@md_seq_show___trans_tmp_57 = external global i8
8+
9+
define i32 @md_seq_show(i64 %0, i32 %1) #0 {
10+
; CHECK-LABEL: name: md_seq_show
11+
; CHECK: bb.0.entry:
12+
; CHECK-NEXT: successors: %bb.3(0x40000000), %bb.4(0x40000000)
13+
; CHECK-NEXT: liveins: $r3, $r4, $r5
14+
; CHECK-NEXT: {{ $}}
15+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r5
16+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY $r4
17+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gprc = COPY $r3
18+
; CHECK-NEXT: [[COPY3:%[0-9]+]]:gprc = COPY [[COPY1]]
19+
; CHECK-NEXT: [[COPY4:%[0-9]+]]:gprc = COPY [[COPY2]]
20+
; CHECK-NEXT: [[ADDIC:%[0-9]+]]:gprc = ADDIC [[COPY1]], 1, implicit-def $carry
21+
; CHECK-NEXT: [[CMPLWI:%[0-9]+]]:crrc = CMPLWI killed [[ADDIC]], 1
22+
; CHECK-NEXT: [[LI:%[0-9]+]]:gprc_and_gprc_nor0 = LI 0
23+
; CHECK-NEXT: [[LI1:%[0-9]+]]:gprc_and_gprc_nor0 = LI 1
24+
; CHECK-NEXT: BCC 44, [[CMPLWI]], %bb.4
25+
; CHECK-NEXT: {{ $}}
26+
; CHECK-NEXT: bb.3.entry:
27+
; CHECK-NEXT: successors: %bb.4(0x80000000)
28+
; CHECK-NEXT: liveins: $carry
29+
; CHECK-NEXT: {{ $}}
30+
; CHECK-NEXT: bb.4.entry:
31+
; CHECK-NEXT: successors: %bb.5(0x40000000), %bb.6(0x40000000)
32+
; CHECK-NEXT: liveins: $carry
33+
; CHECK-NEXT: {{ $}}
34+
; CHECK-NEXT: [[PHI:%[0-9]+]]:gprc_and_gprc_nor0 = PHI [[LI]], %bb.3, [[LI1]], %bb.0
35+
; CHECK-NEXT: [[ADDZE:%[0-9]+]]:gprc = ADDZE [[COPY2]], implicit-def dead $carry, implicit $carry
36+
; CHECK-NEXT: [[ADDIC1:%[0-9]+]]:gprc = ADDIC [[ADDZE]], -1, implicit-def $carry
37+
; CHECK-NEXT: [[SUBFE:%[0-9]+]]:gprc_and_gprc_nor0 = SUBFE killed [[ADDIC1]], [[ADDZE]], implicit-def dead $carry, implicit $carry
38+
; CHECK-NEXT: [[CMPLWI1:%[0-9]+]]:crrc = CMPLWI [[ADDZE]], 0
39+
; CHECK-NEXT: BCC 76, [[CMPLWI1]], %bb.6
40+
; CHECK-NEXT: {{ $}}
41+
; CHECK-NEXT: bb.5.entry:
42+
; CHECK-NEXT: successors: %bb.6(0x80000000)
43+
; CHECK-NEXT: {{ $}}
44+
; CHECK-NEXT: bb.6.entry:
45+
; CHECK-NEXT: successors: %bb.1(0x55555556), %bb.2(0x2aaaaaaa)
46+
; CHECK-NEXT: {{ $}}
47+
; CHECK-NEXT: [[PHI1:%[0-9]+]]:gprc = PHI [[SUBFE]], %bb.5, [[PHI]], %bb.4
48+
; CHECK-NEXT: [[CMPLWI2:%[0-9]+]]:crrc = CMPLWI killed [[PHI1]], 0
49+
; CHECK-NEXT: BCC 68, killed [[CMPLWI2]], %bb.2
50+
; CHECK-NEXT: B %bb.1
51+
; CHECK-NEXT: {{ $}}
52+
; CHECK-NEXT: bb.1.for.cond.i.preheader:
53+
; CHECK-NEXT: [[LI2:%[0-9]+]]:gprc = LI 0
54+
; CHECK-NEXT: $r3 = COPY [[LI2]]
55+
; CHECK-NEXT: BLR implicit $lr, implicit $rm, implicit $r3
56+
; CHECK-NEXT: {{ $}}
57+
; CHECK-NEXT: bb.2.status_resync.exit:
58+
; CHECK-NEXT: [[ADDIC2:%[0-9]+]]:gprc = ADDIC [[COPY]], -1, implicit-def $carry
59+
; CHECK-NEXT: [[SUBFE1:%[0-9]+]]:gprc = SUBFE killed [[ADDIC2]], [[COPY]], implicit-def dead $carry, implicit $carry
60+
; CHECK-NEXT: [[LIS:%[0-9]+]]:gprc_and_gprc_nor0 = LIS target-flags(ppc-ha) @md_seq_show___trans_tmp_57
61+
; CHECK-NEXT: STB killed [[SUBFE1]], target-flags(ppc-lo) @md_seq_show___trans_tmp_57, killed [[LIS]] :: (store (s8) into @md_seq_show___trans_tmp_57)
62+
; CHECK-NEXT: [[LI3:%[0-9]+]]:gprc = LI 0
63+
; CHECK-NEXT: $r3 = COPY [[LI3]]
64+
; CHECK-NEXT: BLR implicit $lr, implicit $rm, implicit $r3
65+
entry:
66+
switch i64 %0, label %status_resync.exit [
67+
i64 -1, label %for.cond.i.preheader
68+
i64 0, label %for.cond.i.preheader
69+
]
70+
71+
for.cond.i.preheader: ; preds = %entry, %entry
72+
ret i32 0
73+
74+
status_resync.exit: ; preds = %entry
75+
%tobool = icmp ne i32 %1, 0
76+
%storedv = zext i1 %tobool to i8
77+
store i8 %storedv, ptr @md_seq_show___trans_tmp_57, align 1
78+
ret i32 0
79+
}
80+
81+
attributes #0 = { "target-features"="-aix-shared-lib-tls-model-opt,-aix-small-local-dynamic-tls,-aix-small-local-exec-tls,-altivec,-bpermd,-crbits,-crypto,-direct-move,-extdiv,-htm,-isa-v206-instructions,-isa-v207-instructions,-isa-v30-instructions,-power8-vector,-power9-vector,-privileged,-quadword-atomics,-rop-protect,-spe,-vsx" }
82+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
83+
; CHECK: {{.*}}

0 commit comments

Comments
 (0)