Skip to content

[Bitcode] Fix constexpr expansion creating invalid PHIs #141560

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 27, 2025
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
8 changes: 6 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6095,14 +6095,18 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
// seen value here, to avoid expanding a constant expression multiple
// times.
auto It = Args.find(BB);
BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB});
if (It != Args.end()) {
PN->addIncoming(It->second, BB);
// If this predecessor was also replaced with a constexpr basic
// block, it must be de-duplicated.
if (!EdgeBB) {
PN->addIncoming(It->second, BB);
}
continue;
}

// If there already is a block for this edge (from a different phi),
// use it.
BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB});
if (!EdgeBB) {
// Otherwise, use a temporary block (that we will discard if it
// turns out to be unnecessary).
Expand Down
30 changes: 30 additions & 0 deletions llvm/test/Bitcode/constexpr-to-instr-dups.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; RUN: opt -expand-constant-exprs %s.bc -S | FileCheck %s
@foo = external constant i32

define i32 @test(i32 %arg) {
entry:
switch i32 %arg, label %cont [
i32 1, label %cont
i32 2, label %nonconst
]

nonconst:
%cmp = icmp ne i32 %arg, 2
br i1 %cmp, label %cont, label %cont

; CHECK-LABEL: phi.constexpr:
; CHECK-NEXT: %constexpr = ptrtoint ptr @foo to i32
; CHECK-NEXT: %constexpr1 = or i32 %constexpr, 5
; CHECK-NEXT: br label %cont


; CHECK-LABEL: cont:
; CHECK-NEXT: %res = phi i32 [ %constexpr1, %phi.constexpr ], [ 1, %nonconst ], [ 1, %nonconst ]
; CHECK-NEXT: ret i32 %res
cont:
%res = phi i32 [or (i32 5, i32 ptrtoint (ptr @foo to i32)), %entry],
[or (i32 5, i32 ptrtoint (ptr @foo to i32)), %entry],
[1, %nonconst],
[1, %nonconst]
ret i32 %res
}
Binary file added llvm/test/Bitcode/constexpr-to-instr-dups.ll.bc
Binary file not shown.
4 changes: 2 additions & 2 deletions llvm/test/Bitcode/constexpr-to-instr.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llvm-dis -expand-constant-exprs < %s.bc | FileCheck %s
; RUN: opt -expand-constant-exprs -S %s.bc | FileCheck %s

@g = extern_weak global i32
@g2 = extern_weak global i32
Expand Down Expand Up @@ -225,7 +225,7 @@ define i64 @test_phi_multiple_identical_predecessors(i32 %x) {
; CHECK-NEXT: %constexpr = ptrtoint ptr @g to i64
; CHECK-NEXT: br label %join
; CHECK: join:
; CHECK-NEXT: %phi = phi i64 [ %constexpr, %phi.constexpr ], [ %constexpr, %phi.constexpr ], [ 0, %default ]
; CHECK-NEXT: %phi = phi i64 [ %constexpr, %phi.constexpr ], [ 0, %default ]
; CHECK-NEXT: ret i64 %phi
entry:
switch i32 %x, label %default [
Expand Down