[llvm] f32b9eb - [ConstantHoisting] Skip PHI edges from unreachable blocks (#203892)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 08:12:42 PDT 2026


Author: Björn Pettersson
Date: 2026-06-15T17:12:37+02:00
New Revision: f32b9eb416beaa13cceac849905a9cc0a993620d

URL: https://github.com/llvm/llvm-project/commit/f32b9eb416beaa13cceac849905a9cc0a993620d
DIFF: https://github.com/llvm/llvm-project/commit/f32b9eb416beaa13cceac849905a9cc0a993620d.diff

LOG: [ConstantHoisting] Skip PHI edges from unreachable blocks (#203892)

When collecting constant candidates, skip incoming PHI edges from blocks
that are unreachable from entry. This avoids assertion failures when the
pass later tries to find insertion points or update users for constants
that only appear on unreachable edges.

While touching this part of the code, also remove an older XFAIL from
test/Transforms/ConstantHoisting/X86/pr52689-not-all-uses-rebased.ll
That test case also triggered the same assert once upon a time, but it
has been set to XFAIL for some time since the reproducer no longer
triggered the bug. This patch turns it into a normal test case instead
of an XFAIL test. Afaict the original problem may have been the same. We
have PHI nodes with edges from unreachable blocks. One difference
compared to the new aarch64 test is that here the involved constants are
GEPs and not simple scalars.

Closes https://github.com/llvm/llvm-project/issues/52689

Added: 
    llvm/test/Transforms/ConstantHoisting/AArch64/consthoist-unreachable-phi-edge.ll

Modified: 
    llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
    llvm/test/Transforms/ConstantHoisting/X86/pr52689-not-all-uses-rebased.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
index 77542b3097d1a..2609ed1d820be 100644
--- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
@@ -505,6 +505,12 @@ void ConstantHoistingPass::collectConstantCandidates(
 
   // Scan all operands.
   for (unsigned Idx = 0, E = Inst->getNumOperands(); Idx != E; ++Idx) {
+    // Skip analyzing incoming PHI edges from unreachable blocks.
+    if (auto PHI = dyn_cast<PHINode>(Inst)) {
+      BasicBlock *IncomingBB = PHI->getIncomingBlock(Idx);
+      if (!DT->isReachableFromEntry(IncomingBB))
+        continue;
+    }
     // The cost of materializing the constants (defined in
     // `TargetTransformInfo::getIntImmCostInst`) for instructions which only
     // take constant variables is lower than `TargetTransformInfo::TCC_Basic`.

diff  --git a/llvm/test/Transforms/ConstantHoisting/AArch64/consthoist-unreachable-phi-edge.ll b/llvm/test/Transforms/ConstantHoisting/AArch64/consthoist-unreachable-phi-edge.ll
new file mode 100644
index 0000000000000..65bce13bf022c
--- /dev/null
+++ b/llvm/test/Transforms/ConstantHoisting/AArch64/consthoist-unreachable-phi-edge.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -mtriple aarch64-- -passes=consthoist -S | FileCheck %s
+
+; The PHI node has some inbound edges with constants that originate from
+; basic blocks that aren't reachable from entry. Those could be tricky
+; to update when trying to find insertion points etc for a common base
+; constant.
+
+; The test case was added as a regression test to show that that we no longer
+; hit assertion failures about number of users updated/rebased. The workaround
+; is to ignore phi edges from basic blocks that aren't reachable from entry
+; when looking for candidate constants.
+
+define i64 @func(i1 %b0, i1 %b1) {
+; CHECK-LABEL: define i64 @func(
+; CHECK-SAME: i1 [[B0:%.*]], i1 [[B1:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[B0]], label %[[BLOCK1:.*]], label %[[BLOCK2:.*]]
+; CHECK:       [[BLOCK1]]:
+; CHECK-NEXT:    [[CONST1:%.*]] = bitcast i64 4886718345 to i64
+; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK:       [[BLOCK2]]:
+; CHECK-NEXT:    br i1 [[B1]], label %[[BLOCK3:.*]], label %[[MERGE]]
+; CHECK:       [[BLOCK3]]:
+; CHECK-NEXT:    [[CONST:%.*]] = bitcast i64 4886718345 to i64
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[UNREACHABLE1:.*]]:
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[UNREACHABLE2:.*]]:
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[V:%.*]] = phi i64 [ 4886718345, %[[UNREACHABLE1]] ], [ 4886718345, %[[UNREACHABLE2]] ], [ [[CONST1]], %[[BLOCK1]] ], [ [[CONST]], %[[BLOCK3]] ], [ 0, %[[BLOCK2]] ]
+; CHECK-NEXT:    ret i64 [[V]]
+;
+entry:
+  br i1 %b0, label %block1, label %block2
+
+block1:
+  br label %merge
+
+block2:
+  br i1 %b1, label %block3, label %merge
+
+block3:
+  br label %merge
+
+unreachable1:
+  br label %merge
+
+unreachable2:
+  br label %merge
+
+merge:
+  %v = phi i64 [ 4886718345, %unreachable1 ], [ 4886718345, %unreachable2 ], [ 4886718345, %block1 ], [ 4886718345, %block3 ], [ 0, %block2 ]
+  ret i64 %v
+}
+
+

diff  --git a/llvm/test/Transforms/ConstantHoisting/X86/pr52689-not-all-uses-rebased.ll b/llvm/test/Transforms/ConstantHoisting/X86/pr52689-not-all-uses-rebased.ll
index e4352e4d98b77..7818cbcb040b5 100644
--- a/llvm/test/Transforms/ConstantHoisting/X86/pr52689-not-all-uses-rebased.ll
+++ b/llvm/test/Transforms/ConstantHoisting/X86/pr52689-not-all-uses-rebased.ll
@@ -1,25 +1,35 @@
-; RUN: not --crash opt -passes='consthoist' -S -o - -consthoist-gep=1 -mtriple=x86_64-unknown-linux-gnu < %s 2>&1 | FileCheck %s
-
-; REQUIRES: asserts
-
-; My changes fixed this likely by accident, please update as necessary when
-; you work on this:
-; XFAIL: *
-
-; Matching assertion strings is not easy as they might 
diff er on 
diff erent
-; platforms. So limit this to x86_64-linux.
-; REQUIRES: x86_64-linux
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes='consthoist' -S -o - -consthoist-gep=1 -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
 
 ; This is a reproducer for https://github.com/llvm/llvm-project/issues/52689
 ;
 ; opt: ../lib/Transforms/Scalar/ConstantHoisting.cpp:919: bool llvm::ConstantHoistingPass::emitBaseConstants(llvm::GlobalVariable *): Assertion `UsesNum == (ReBasesNum + NotRebasedNum) && "Not all uses are rebased"' failed.
 
-; CHECK: UsesNum == (ReBasesNum + NotRebasedNum)
-; CHECK-SAME: Not all uses are rebased
-
 @g_77 = external global [5 x i32]
 
 define internal ptr @func_29(i1 %p1, i1 %p2, ptr %p3) {
+; CHECK-LABEL: define internal ptr @func_29(
+; CHECK-SAME: i1 [[P1:%.*]], i1 [[P2:%.*]], ptr [[P3:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CONST:%.*]] = bitcast ptr getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 1) to ptr
+; CHECK-NEXT:    br i1 [[P1]], label %[[CRIT_EDGE:.*]], label %[[IF_ELSE3089:.*]]
+; CHECK:       [[CRIT_EDGE]]:
+; CHECK-NEXT:    br label %[[FOR_COND1063:.*]]
+; CHECK:       [[FOR_COND1063]]:
+; CHECK-NEXT:    [[L_323_SROA_0_0:%.*]] = phi ptr [ getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 3), %[[CLEANUP1660:.*]] ], [ null, %[[CRIT_EDGE]] ]
+; CHECK-NEXT:    [[L_323_SROA_2_0:%.*]] = phi ptr [ getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 3), %[[CLEANUP1660]] ], [ null, %[[CRIT_EDGE]] ]
+; CHECK-NEXT:    br i1 [[P2]], label %[[CLEANUP1660_THREAD:.*]], label %[[CLEANUP1674:.*]]
+; CHECK:       [[CLEANUP1660_THREAD]]:
+; CHECK-NEXT:    br label %[[CLEANUP1674]]
+; CHECK:       [[CLEANUP1660]]:
+; CHECK-NEXT:    br label %[[FOR_COND1063]]
+; CHECK:       [[CLEANUP1674]]:
+; CHECK-NEXT:    store ptr [[CONST]], ptr [[P3]], align 1
+; CHECK-NEXT:    ret ptr null
+; CHECK:       [[IF_ELSE3089]]:
+; CHECK-NEXT:    store ptr [[CONST]], ptr [[P3]], align 1
+; CHECK-NEXT:    ret ptr null
+;
 entry:
   br i1 %p1, label %crit_edge, label %if.else3089
 


        


More information about the llvm-commits mailing list