[llvm] 44a52ea - [InstCombine] Fix unsafe PHINode cast and simplify logic in PointerReplacer (#172332)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 17 04:07:44 PST 2025
Author: Miloš Poletanović
Date: 2025-12-17T12:07:40Z
New Revision: 44a52ea8beed75eeaca831d1af3590dac9fa832e
URL: https://github.com/llvm/llvm-project/commit/44a52ea8beed75eeaca831d1af3590dac9fa832e
DIFF: https://github.com/llvm/llvm-project/commit/44a52ea8beed75eeaca831d1af3590dac9fa832e.diff
LOG: [InstCombine] Fix unsafe PHINode cast and simplify logic in PointerReplacer (#172332)
Fixes #171883.
Basically, if the operand of the phi is an Instruction but it's not
available, the [condition
](https://github.com/llvm/llvm-project/blame/1847a4efae6b0c0c985cecead1f3ef381a1962de/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp#L300)would
just break, and when we reach the[ deferral
check](https://github.com/llvm/llvm-project/blame/1847a4efae6b0c0c985cecead1f3ef381a1962de/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp#L313),
execution would continue even though there is a non-Instruction operand,
leading to a crash in the [subsequent processing
loop](https://github.com/llvm/llvm-project/blame/1847a4efae6b0c0c985cecead1f3ef381a1962de/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp#L320).
Added:
llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll
Modified:
llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 9491610190c10..5bca0bb8846c0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -296,10 +296,9 @@ bool PointerReplacer::collectUsers() {
/// TODO: Handle poison and null pointers for PHI and select.
// If all incoming values are available, mark this PHI as
// replacable and push it's users into the worklist.
- bool IsReplaceable = true;
- if (all_of(PHI->incoming_values(), [&](Value *V) {
- if (!isa<Instruction>(V))
- return IsReplaceable = false;
+ bool IsReplaceable = all_of(PHI->incoming_values(),
+ [](Value *V) { return isa<Instruction>(V); });
+ if (IsReplaceable && all_of(PHI->incoming_values(), [&](Value *V) {
return isAvailable(cast<Instruction>(V));
})) {
UsersToReplace.insert(PHI);
diff --git a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll
new file mode 100644
index 0000000000000..6891d862b2468
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll
@@ -0,0 +1,40 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+ at __const.var1 = addrspace(4) constant <{ ptr addrspace(5), ptr addrspace(5), ptr addrspace(5), ptr addrspace(5) }> <{ ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5)), ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5)), ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5)), ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5)) }>
+define void @test(i32 %0, i1 %tobool, ptr addrspace(4) %const_src) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: i32 [[TMP0:%.*]], i1 [[TOBOOL:%.*]], ptr addrspace(4) [[CONST_SRC:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: switch i32 [[TMP0]], label %[[SW_EPILOG:.*]] [
+; CHECK-NEXT: i32 1, label %[[BS_LABEL_3:.*]]
+; CHECK-NEXT: i32 0, label %[[BS_LABEL_3]]
+; CHECK-NEXT: ]
+; CHECK: [[SW_EPILOG]]:
+; CHECK-NEXT: br i1 [[TOBOOL]], label %[[BS_LABEL_7:.*]], label %[[BS_LABEL_3]]
+; CHECK: [[BS_LABEL_7]]:
+; CHECK-NEXT: br label %[[BS_LABEL_3]]
+; CHECK: [[BS_LABEL_3]]:
+; CHECK-NEXT: br label %[[BS_LABEL_7]]
+;
+entry:
+ %l_632 = alloca [4 x ptr addrspace(5)], align 4, addrspace(5)
+ switch i32 %0, label %sw.epilog [
+ i32 1, label %BS_LABEL_3
+ i32 0, label %BS_LABEL_3
+ ]
+
+sw.epilog: ; preds = %entry
+ call void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) %l_632, ptr addrspace(4) @__const.var1, i64 16, i1 false)
+ %arrayidx = getelementptr inbounds [4 x ptr addrspace(5)], ptr addrspace(5) %l_632, i64 0, i64 3
+ br i1 %tobool, label %BS_LABEL_7, label %BS_LABEL_3
+
+BS_LABEL_7: ; preds = %BS_LABEL_3, %sw.epilog
+ %l_631.1 = phi ptr addrspace(5) [ %arrayidx, %sw.epilog ], [ %l_631.0, %BS_LABEL_3 ]
+ br label %BS_LABEL_3
+
+BS_LABEL_3: ; preds = %BS_LABEL_7, %sw.epilog, %entry, %entry
+ %l_631.0 = phi ptr addrspace(5) [ %l_631.1, %BS_LABEL_7 ], [ %arrayidx, %sw.epilog ], [ poison, %entry ], [ poison, %entry ]
+ %cmp = icmp ugt ptr addrspace(5) %l_631.0, null
+ br label %BS_LABEL_7
+}
More information about the llvm-commits
mailing list