Skip to content

Commit b7f4b17

Browse files
committed
[CodeGenPrepare] Handle address sinking obtained from invoke
1 parent 610d057 commit b7f4b17

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5790,7 +5790,8 @@ static BasicBlock::iterator findInsertPos(Value *Addr, Instruction *MemoryInst,
57905790
// instruction after it.
57915791
if (SunkAddr) {
57925792
if (Instruction *AddrInst = dyn_cast<Instruction>(SunkAddr))
5793-
return std::next(AddrInst->getIterator());
5793+
return AddrInst->isTerminator() ? MemoryInst->getIterator()
5794+
: std::next(AddrInst->getIterator());
57945795
}
57955796

57965797
// Find the first user of Addr in current BB.

llvm/test/Transforms/CodeGenPrepare/X86/sink-addr-reuse.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,38 @@ bb3:
4242
bb7:
4343
ret void
4444
}
45+
46+
; Test a scenario when the address is the last instruction in the basic block.
47+
48+
define void @adress_terminator() personality ptr null {
49+
; CHECK-LABEL: define void @adress_terminator() personality ptr null {
50+
; CHECK-NEXT: [[ENTRY:.*:]]
51+
; CHECK-NEXT: [[PTR:%.*]] = invoke ptr null(i64 0)
52+
; CHECK-NEXT: to label %[[BODY_1:.*]] unwind label %[[EHCLEANUP:.*]]
53+
; CHECK: [[EHCLEANUP]]:
54+
; CHECK-NEXT: [[TMP0:%.*]] = cleanuppad within none []
55+
; CHECK-NEXT: cleanupret from [[TMP0]] unwind to caller
56+
; CHECK: [[BODY_1]]:
57+
; CHECK-NEXT: [[TMP1:%.*]] = bitcast ptr [[PTR]] to ptr
58+
; CHECK-NEXT: store <4 x i32> zeroinitializer, ptr [[TMP1]], align 4
59+
; CHECK-NEXT: [[V0:%.*]] = load <4 x i32>, ptr [[PTR]], align 4
60+
; CHECK-NEXT: store <4 x i32> zeroinitializer, ptr [[PTR]], align 4
61+
; CHECK-NEXT: ret void
62+
;
63+
entry:
64+
%ptr = invoke ptr null(i64 0) to label %body.1 unwind label %ehcleanup
65+
66+
body.2:
67+
%v0 = load <4 x i32>, ptr %2, align 4
68+
store <4 x i32> zeroinitializer, ptr %2, align 4
69+
ret void
70+
71+
ehcleanup:
72+
%1 = cleanuppad within none []
73+
cleanupret from %1 unwind to caller
74+
75+
body.1:
76+
%2 = getelementptr { i32 }, ptr %ptr, i64 0, i32 0
77+
store <4 x i32> zeroinitializer, ptr %2, align 4
78+
br label %body.2
79+
}

0 commit comments

Comments
 (0)