Skip to content

Commit 9bc4d05

Browse files
committed
[Reg2Mem] Handle CallBr instructions
1 parent 37eb9c9 commit 9bc4d05

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

llvm/lib/Transforms/Utils/DemoteRegToStack.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
5050
assert(BB && "Unable to split critical edge.");
5151
(void)BB;
5252
}
53+
} else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
54+
for (int i = 0; i < CBI->getNumSuccessors(); i++) {
55+
auto *Succ = CBI->getSuccessor(i);
56+
if (!Succ->getSinglePredecessor()) {
57+
assert(isCriticalEdge(II, i) && "Expected a critical edge!");
58+
BasicBlock *BB = SplitCriticalEdge(II, i);
59+
assert(BB && "Unable to split critical edge.");
60+
}
61+
}
5362
}
5463

5564
// Change all of the users of the instruction to read from the stack slot.
@@ -102,9 +111,14 @@ AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
102111
new StoreInst(&I, Slot, Handler->getFirstInsertionPt());
103112
return Slot;
104113
}
114+
} else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
115+
InsertPt = II->getNormalDest()->getFirstInsertionPt();
116+
} else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
117+
for (BasicBlock *Succ : successors(CBI))
118+
new StoreInst(CBI, Slot, Succ->getFirstInsertionPt());
119+
return Slot;
105120
} else {
106-
InvokeInst &II = cast<InvokeInst>(I);
107-
InsertPt = II.getNormalDest()->getFirstInsertionPt();
121+
llvm_unreachable("Unsupported terminator for Reg2Mem");
108122
}
109123

110124
new StoreInst(&I, Slot, InsertPt);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes=reg2mem -S < %s | FileCheck %s
3+
4+
define void @crash() {
5+
; CHECK-LABEL: @crash(
6+
; CHECK-NEXT: entry:
7+
; CHECK-NEXT: [[A_REG2MEM:%.*]] = alloca i64, align 8
8+
; CHECK-NEXT: %"reg2mem alloca point" = bitcast i32 0 to i32
9+
; CHECK-NEXT: [[A:%.*]] = callbr i64 asm "", "=r,r,!i"(i64 0)
10+
; CHECK-NEXT: to label [[THEN:%.*]] [label %entry.else_crit_edge]
11+
; CHECK: entry.else_crit_edge:
12+
; CHECK-NEXT: store i64 [[A]], ptr [[A_REG2MEM]], align 4
13+
; CHECK-NEXT: br label [[ELSE:%.*]]
14+
; CHECK: then:
15+
; CHECK-NEXT: store i64 [[A]], ptr [[A_REG2MEM]], align 4
16+
; CHECK-NEXT: [[A_RELOAD:%.*]] = load i64, ptr [[A_REG2MEM]], align 4
17+
; CHECK-NEXT: [[B:%.*]] = inttoptr i64 [[A_RELOAD]] to ptr
18+
; CHECK-NEXT: br label [[ELSE]]
19+
; CHECK: else:
20+
; CHECK-NEXT: ret void
21+
;
22+
entry:
23+
%a = callbr i64 asm "", "=r,r,!i"(i64 0)
24+
to label %then [label %else]
25+
26+
then:
27+
%b = inttoptr i64 %a to ptr
28+
br label %else
29+
30+
else:
31+
ret void
32+
}

0 commit comments

Comments
 (0)