Skip to content

[Reg2Mem] Handle CallBr instructions #90953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
assert(BB && "Unable to split critical edge.");
(void)BB;
}
} else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
for (int i = 0; i < CBI->getNumSuccessors(); i++) {
auto *Succ = CBI->getSuccessor(i);
if (!Succ->getSinglePredecessor()) {
assert(isCriticalEdge(II, i) && "Expected a critical edge!");
BasicBlock *BB = SplitCriticalEdge(II, i);
assert(BB && "Unable to split critical edge.");
}
}
}

// Change all of the users of the instruction to read from the stack slot.
Expand Down Expand Up @@ -102,9 +111,14 @@ AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
new StoreInst(&I, Slot, Handler->getFirstInsertionPt());
return Slot;
}
} else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
InsertPt = II->getNormalDest()->getFirstInsertionPt();
} else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
for (BasicBlock *Succ : successors(CBI))
new StoreInst(CBI, Slot, Succ->getFirstInsertionPt());
return Slot;
} else {
InvokeInst &II = cast<InvokeInst>(I);
InsertPt = II.getNormalDest()->getFirstInsertionPt();
llvm_unreachable("Unsupported terminator for Reg2Mem");
}

new StoreInst(&I, Slot, InsertPt);
Expand Down
32 changes: 32 additions & 0 deletions llvm/test/Transforms/Reg2Mem/callbr-crash.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=reg2mem -S < %s | FileCheck %s

define void @crash() {
; CHECK-LABEL: @crash(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A_REG2MEM:%.*]] = alloca i64, align 8
; CHECK-NEXT: %"reg2mem alloca point" = bitcast i32 0 to i32
; CHECK-NEXT: [[A:%.*]] = callbr i64 asm "", "=r,r,!i"(i64 0)
; CHECK-NEXT: to label [[THEN:%.*]] [label %entry.else_crit_edge]
; CHECK: entry.else_crit_edge:
; CHECK-NEXT: store i64 [[A]], ptr [[A_REG2MEM]], align 4
; CHECK-NEXT: br label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: store i64 [[A]], ptr [[A_REG2MEM]], align 4
; CHECK-NEXT: [[A_RELOAD:%.*]] = load i64, ptr [[A_REG2MEM]], align 4
; CHECK-NEXT: [[B:%.*]] = inttoptr i64 [[A_RELOAD]] to ptr
; CHECK-NEXT: br label [[ELSE]]
; CHECK: else:
; CHECK-NEXT: ret void
;
entry:
%a = callbr i64 asm "", "=r,r,!i"(i64 0)
to label %then [label %else]

then:
%b = inttoptr i64 %a to ptr
br label %else

else:
ret void
}