From 632bf62924fc53527a8ca89b1322e6354134adb2 Mon Sep 17 00:00:00 2001 From: Milos Poletanovic Date: Mon, 15 Dec 2025 17:33:25 +0100 Subject: [PATCH 1/5] [InstCombine] Fix unsafe PHINode cast and simplify logic in PointerReplacer --- .../InstCombine/InstCombineLoadStoreAlloca.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index 9491610190c10..ea5587f4c9412 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -296,12 +296,12 @@ 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(V)) - return IsReplaceable = false; - return isAvailable(cast(V)); - })) { + bool IsReplaceable = all_of(PHI->incoming_values(), [](Value *V) { + return isa(V); + }); + if (IsReplaceable && all_of(PHI->incoming_values(), [&](Value *V) { + return isAvailable(cast(V)); + })) { UsersToReplace.insert(PHI); PushUsersToWorklist(PHI); continue; From 6621ac8e9553c327d6fc2becadcf60ce75f7dc02 Mon Sep 17 00:00:00 2001 From: Milos Poletanovic Date: Tue, 16 Dec 2025 16:18:53 +0100 Subject: [PATCH 2/5] Added a test. --- .../InstCombine/alloca-phi-non-inst.ll | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll 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..701394e41ccae --- /dev/null +++ b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll @@ -0,0 +1,29 @@ +; RUN: opt < %s -O1 -S | FileCheck %s + +@__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: entry: +; CHECK-NEXT: br label %BS_LABEL_3 +; CHECK: BS_LABEL_3: +; CHECK-NEXT: br label %BS_LABEL_3 +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 ], [ undef, %entry ], [ undef, %entry ] + %cmp = icmp ugt ptr addrspace(5) %l_631.0, null + br label %BS_LABEL_7 +} \ No newline at end of file From ba60d8331ad91ac11e614a050bdcea5fc5fc5480 Mon Sep 17 00:00:00 2001 From: Milos Poletanovic Date: Wed, 17 Dec 2025 01:22:32 +0100 Subject: [PATCH 3/5] Added a new line at the end. --- llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll index 701394e41ccae..b8b8c29221f7b 100644 --- a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll +++ b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll @@ -26,4 +26,4 @@ BS_LABEL_3: ; preds = %BS_LABEL_7, %sw.epi %l_631.0 = phi ptr addrspace(5) [ %l_631.1, %BS_LABEL_7 ], [ %arrayidx, %sw.epilog ], [ undef, %entry ], [ undef, %entry ] %cmp = icmp ugt ptr addrspace(5) %l_631.0, null br label %BS_LABEL_7 -} \ No newline at end of file +} From a2eba6ab780cbcef4d3102e836b11380a0a5f57e Mon Sep 17 00:00:00 2001 From: Milos Poletanovic Date: Wed, 17 Dec 2025 11:10:03 +0100 Subject: [PATCH 4/5] Addressed comments regarding test. --- .../InstCombine/alloca-phi-non-inst.ll | 69 +++++++++++-------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll index b8b8c29221f7b..74767929588f5 100644 --- a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll +++ b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll @@ -1,29 +1,40 @@ -; RUN: opt < %s -O1 -S | FileCheck %s - -@__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: entry: -; CHECK-NEXT: br label %BS_LABEL_3 -; CHECK: BS_LABEL_3: -; CHECK-NEXT: br label %BS_LABEL_3 -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 ], [ undef, %entry ], [ undef, %entry ] - %cmp = icmp ugt ptr addrspace(5) %l_631.0, null - br label %BS_LABEL_7 -} +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +@__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 ], [ undef, %entry ], [ undef, %entry ] + %cmp = icmp ugt ptr addrspace(5) %l_631.0, null + br label %BS_LABEL_7 +} From 00a72ca7232e8fe7fd9f19cfcc271af4f70fdc88 Mon Sep 17 00:00:00 2001 From: Milos Poletanovic Date: Wed, 17 Dec 2025 11:41:50 +0100 Subject: [PATCH 5/5] Use poison instead of undef (deprecated). --- llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll index 74767929588f5..6891d862b2468 100644 --- a/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll +++ b/llvm/test/Transforms/InstCombine/alloca-phi-non-inst.ll @@ -34,7 +34,7 @@ BS_LABEL_7: ; preds = %BS_LABEL_3, %sw.epi 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 ], [ undef, %entry ], [ undef, %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 }