[llvm] [InstCombine] Simplify nonnull phi nodes (PR #128466)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 25 07:45:14 PST 2025
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/128466
>From cd2d4fc4360b028587117fd4f91b8627976e960c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 24 Feb 2025 13:35:19 +0800
Subject: [PATCH 1/3] [InstCombine] Add pre-commit tests. NFC.
---
llvm/test/Transforms/InstCombine/load.ll | 29 ++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/load.ll b/llvm/test/Transforms/InstCombine/load.ll
index d10aabff6709a..c6e28653e7fab 100644
--- a/llvm/test/Transforms/InstCombine/load.ll
+++ b/llvm/test/Transforms/InstCombine/load.ll
@@ -481,3 +481,32 @@ define i16 @load_select_with_null_gep3(i1 %cond, ptr %p, i64 %x, i64 %y) {
%res = load i16, ptr %gep2, align 2
ret i16 %res
}
+
+define i32 @test_load_phi_with_select(ptr %p, i1 %cond1) {
+; CHECK-LABEL: @test_load_phi_with_select(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[LOOP_BODY:%.*]]
+; CHECK: loop.body:
+; CHECK-NEXT: [[BASE:%.*]] = phi ptr [ [[P1:%.*]], [[ENTRY:%.*]] ], [ [[P:%.*]], [[LOOP_BODY]] ]
+; CHECK-NEXT: [[TARGET:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE]], i64 24
+; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[TARGET]], align 4
+; CHECK-NEXT: [[P]] = select i1 [[COND1:%.*]], ptr null, ptr [[P1]]
+; CHECK-NEXT: [[COND21:%.*]] = icmp eq i32 [[LOAD]], 0
+; CHECK-NEXT: br i1 [[COND21]], label [[LOOP_BODY]], label [[EXIT:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: ret i32 [[LOAD]]
+;
+entry:
+ br label %loop.body
+
+loop.body:
+ %base = phi ptr [ %p, %entry ], [ %sel, %loop.body ]
+ %target = getelementptr inbounds i8, ptr %base, i64 24
+ %load = load i32, ptr %target, align 4
+ %sel = select i1 %cond1, ptr null, ptr %p
+ %cond2 = icmp eq i32 %load, 0
+ br i1 %cond2, label %loop.body, label %exit
+
+exit:
+ ret i32 %load
+}
>From 3b13bda80ca0ad349105c59ed6087201b4c76899 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 24 Feb 2025 13:55:53 +0800
Subject: [PATCH 2/3] [InstCombine] Simplify nonnull phi nodes
---
.../InstCombine/InstCombineLoadStoreAlloca.cpp | 14 ++++++++++++++
llvm/test/Transforms/InstCombine/load.ll | 4 +---
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 221511e9aba0b..52245a80cd4ef 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -1011,6 +1011,20 @@ Value *InstCombinerImpl::simplifyNonNullOperand(Value *V,
}
}
+ if (auto *PHI = dyn_cast<PHINode>(V)) {
+ bool Changed = false;
+ for (Use &U : PHI->incoming_values()) {
+ if (auto *Res =
+ simplifyNonNullOperand(U.get(), HasDereferenceable, Depth + 1)) {
+ replaceUse(U, Res);
+ Changed = true;
+ }
+ }
+ if (Changed)
+ addToWorklist(PHI);
+ return nullptr;
+ }
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/load.ll b/llvm/test/Transforms/InstCombine/load.ll
index c6e28653e7fab..736a31fc6ae0a 100644
--- a/llvm/test/Transforms/InstCombine/load.ll
+++ b/llvm/test/Transforms/InstCombine/load.ll
@@ -487,10 +487,8 @@ define i32 @test_load_phi_with_select(ptr %p, i1 %cond1) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP_BODY:%.*]]
; CHECK: loop.body:
-; CHECK-NEXT: [[BASE:%.*]] = phi ptr [ [[P1:%.*]], [[ENTRY:%.*]] ], [ [[P:%.*]], [[LOOP_BODY]] ]
-; CHECK-NEXT: [[TARGET:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE]], i64 24
+; CHECK-NEXT: [[TARGET:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE:%.*]], i64 24
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[TARGET]], align 4
-; CHECK-NEXT: [[P]] = select i1 [[COND1:%.*]], ptr null, ptr [[P1]]
; CHECK-NEXT: [[COND21:%.*]] = icmp eq i32 [[LOAD]], 0
; CHECK-NEXT: br i1 [[COND21]], label [[LOOP_BODY]], label [[EXIT:%.*]]
; CHECK: exit:
>From 3a27268e264826ef9cf493f645507e490f05e7f3 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 25 Feb 2025 23:44:36 +0800
Subject: [PATCH 3/3] [InstCombine] Address review comments.
---
.../Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 52245a80cd4ef..c1f5e286ab3ed 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -1014,8 +1014,9 @@ Value *InstCombinerImpl::simplifyNonNullOperand(Value *V,
if (auto *PHI = dyn_cast<PHINode>(V)) {
bool Changed = false;
for (Use &U : PHI->incoming_values()) {
- if (auto *Res =
- simplifyNonNullOperand(U.get(), HasDereferenceable, Depth + 1)) {
+ // We set Depth to RecursionLimit to avoid expensive recursion.
+ if (auto *Res = simplifyNonNullOperand(U.get(), HasDereferenceable,
+ RecursionLimit)) {
replaceUse(U, Res);
Changed = true;
}
More information about the llvm-commits
mailing list