[llvm] [ValueTracking] Skip incoming values that are the same as the phi in `isGuaranteedNotToBeUndefOrPoison` (PR #130111)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 6 07:15:44 PST 2025


https://github.com/DianQK updated https://github.com/llvm/llvm-project/pull/130111

>From e10b2baed2baad5d77473234d7d158f204ca2132 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 6 Mar 2025 21:44:20 +0800
Subject: [PATCH 1/5] Add test cases

---
 llvm/test/Analysis/ValueTracking/phi-self.ll | 146 +++++++++++++++++++
 1 file changed, 146 insertions(+)
 create mode 100644 llvm/test/Analysis/ValueTracking/phi-self.ll

diff --git a/llvm/test/Analysis/ValueTracking/phi-self.ll b/llvm/test/Analysis/ValueTracking/phi-self.ll
new file mode 100644
index 0000000000000..e9e61c27827f8
--- /dev/null
+++ b/llvm/test/Analysis/ValueTracking/phi-self.ll
@@ -0,0 +1,146 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
+
+; Test `%r` can be replaced by `%nonpoison`.
+
+define i64 @other_noundef() {
+; CHECK-LABEL: define i64 @other_noundef() {
+; CHECK-NEXT:  [[START:.*]]:
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[NONPOISON:%.*]] = phi i64 [ [[NONPOISON]], %[[BB0:.*]] ], [ [[NONPOISON]], %[[BB1:.*]] ], [ [[NONPOISON]], %[[BB2:.*]] ], [ [[NONPOISON]], %[[BB:.*]] ], [ [[I:%.*]], %[[BACK_TO_LOOP:.*]] ], [ 0, %[[START]] ]
+; CHECK-NEXT:    [[I]] = call i64 @opaque()
+; CHECK-NEXT:    switch i64 [[I]], label %[[EXIT0:.*]] [
+; CHECK-NEXT:      i64 -1, label %[[EXIT1:.*]]
+; CHECK-NEXT:      i64 2, label %[[BACK_TO_LOOP]]
+; CHECK-NEXT:      i64 0, label %[[BB]]
+; CHECK-NEXT:    ]
+; CHECK:       [[EXIT0]]:
+; CHECK-NEXT:    br label %[[EXIT1]]
+; CHECK:       [[EXIT1]]:
+; CHECK-NEXT:    [[R:%.*]] = phi i64 [ [[NONPOISON]], %[[LOOP]] ], [ undef, %[[EXIT0]] ]
+; CHECK-NEXT:    ret i64 [[R]]
+; CHECK:       [[BACK_TO_LOOP]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+; CHECK:       [[BB]]:
+; CHECK-NEXT:    switch i64 [[NONPOISON]], label %[[LOOP]] [
+; CHECK-NEXT:      i64 0, label %[[BB0]]
+; CHECK-NEXT:      i64 1, label %[[BB1]]
+; CHECK-NEXT:      i64 2, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB0]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+;
+start:
+  br label %loop
+
+loop:                                             ; preds = %bb2, %bb1, %bb0, %bb, %back_to_loop, %start
+  %nonpoison = phi i64 [ %nonpoison, %bb0 ], [ %nonpoison, %bb1 ], [ %nonpoison, %bb2 ], [ %nonpoison, %bb ], [ %i, %back_to_loop ], [ 0, %start ]
+  %i = call i64 @opaque()
+  switch i64 %i, label %exit0 [
+  i64 -1, label %exit1
+  i64 2, label %back_to_loop
+  i64 0, label %bb
+  ]
+
+exit0:                                            ; preds = %loop
+  br label %exit1
+
+exit1:                                            ; preds = %exit0, %loop
+  %r = phi i64 [ %nonpoison, %loop ], [ undef, %exit0 ]
+  ret i64 %r
+
+back_to_loop:                                     ; preds = %loop
+  br label %loop
+
+bb:                                               ; preds = %loop
+  switch i64 %nonpoison, label %loop [
+  i64 0, label %bb0
+  i64 1, label %bb1
+  i64 2, label %bb2
+  ]
+
+bb0:                                              ; preds = %bb
+  br label %loop
+
+bb1:                                              ; preds = %bb
+  br label %loop
+
+bb2:                                              ; preds = %bb
+  br label %loop
+}
+
+define i64 @other_poison() {
+; CHECK-LABEL: define i64 @other_poison() {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[I:%.*]] = call i64 @opaque()
+; CHECK-NEXT:    switch i64 [[I]], label %[[EXIT0:.*]] [
+; CHECK-NEXT:      i64 -1, label %[[EXIT1:.*]]
+; CHECK-NEXT:      i64 2, label %[[BACK_TO_LOOP:.*]]
+; CHECK-NEXT:      i64 0, label %[[BB:.*]]
+; CHECK-NEXT:    ]
+; CHECK:       [[EXIT0]]:
+; CHECK-NEXT:    br label %[[EXIT1]]
+; CHECK:       [[EXIT1]]:
+; CHECK-NEXT:    ret i64 0
+; CHECK:       [[BACK_TO_LOOP]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+; CHECK:       [[BB]]:
+; CHECK-NEXT:    switch i64 0, label %[[LOOP]] [
+; CHECK-NEXT:      i64 0, label %[[BB0:.*]]
+; CHECK-NEXT:      i64 1, label %[[BB1:.*]]
+; CHECK-NEXT:      i64 2, label %[[BB2:.*]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB0]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    br label %[[LOOP]]
+;
+start:
+  br label %loop
+
+loop:                                             ; preds = %bb2, %bb1, %bb0, %bb, %back_to_loop, %start
+  %maypoison = phi i64 [ %maypoison, %bb0 ], [ %maypoison, %bb1 ], [ %maypoison, %bb2 ], [ %maypoison, %bb ], [ poison, %back_to_loop ], [ 0, %start ]
+  %i = call i64 @opaque()
+  switch i64 %i, label %exit0 [
+  i64 -1, label %exit1
+  i64 2, label %back_to_loop
+  i64 0, label %bb
+  ]
+
+exit0:                                            ; preds = %loop
+  br label %exit1
+
+exit1:                                            ; preds = %exit0, %loop
+  %r = phi i64 [ %maypoison, %loop ], [ undef, %exit0 ]
+  ret i64 %r
+
+back_to_loop:                                     ; preds = %loop
+  br label %loop
+
+bb:                                               ; preds = %loop
+  switch i64 %maypoison, label %loop [
+  i64 0, label %bb0
+  i64 1, label %bb1
+  i64 2, label %bb2
+  ]
+
+bb0:                                              ; preds = %bb
+  br label %loop
+
+bb1:                                              ; preds = %bb
+  br label %loop
+
+bb2:                                              ; preds = %bb
+  br label %loop
+}
+
+declare i64 @opaque()

>From d998eed8aae7cd8d5d28266c9c21f903a100e532 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 4 Mar 2025 22:43:34 +0800
Subject: [PATCH 2/5] [ValueTracking] Skip incoming values that are the same as
 the phi

---
 llvm/lib/Analysis/ValueTracking.cpp          | 2 ++
 llvm/test/Analysis/ValueTracking/phi-self.ll | 5 ++---
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e3e026f7979da..9b098bcb28bcc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -7824,6 +7824,8 @@ static bool isGuaranteedNotToBeUndefOrPoison(
       unsigned Num = PN->getNumIncomingValues();
       bool IsWellDefined = true;
       for (unsigned i = 0; i < Num; ++i) {
+        if (PN == PN->getIncomingValue(i))
+          continue;
         auto *TI = PN->getIncomingBlock(i)->getTerminator();
         if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
                                               DT, Depth + 1, Kind)) {
diff --git a/llvm/test/Analysis/ValueTracking/phi-self.ll b/llvm/test/Analysis/ValueTracking/phi-self.ll
index e9e61c27827f8..095a16983ce21 100644
--- a/llvm/test/Analysis/ValueTracking/phi-self.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-self.ll
@@ -8,7 +8,7 @@ define i64 @other_noundef() {
 ; CHECK-NEXT:  [[START:.*]]:
 ; CHECK-NEXT:    br label %[[LOOP:.*]]
 ; CHECK:       [[LOOP]]:
-; CHECK-NEXT:    [[NONPOISON:%.*]] = phi i64 [ [[NONPOISON]], %[[BB0:.*]] ], [ [[NONPOISON]], %[[BB1:.*]] ], [ [[NONPOISON]], %[[BB2:.*]] ], [ [[NONPOISON]], %[[BB:.*]] ], [ [[I:%.*]], %[[BACK_TO_LOOP:.*]] ], [ 0, %[[START]] ]
+; CHECK-NEXT:    [[R:%.*]] = phi i64 [ [[R]], %[[BB0:.*]] ], [ [[R]], %[[BB1:.*]] ], [ [[R]], %[[BB2:.*]] ], [ [[R]], %[[BB:.*]] ], [ [[I:%.*]], %[[BACK_TO_LOOP:.*]] ], [ 0, %[[START]] ]
 ; CHECK-NEXT:    [[I]] = call i64 @opaque()
 ; CHECK-NEXT:    switch i64 [[I]], label %[[EXIT0:.*]] [
 ; CHECK-NEXT:      i64 -1, label %[[EXIT1:.*]]
@@ -18,12 +18,11 @@ define i64 @other_noundef() {
 ; CHECK:       [[EXIT0]]:
 ; CHECK-NEXT:    br label %[[EXIT1]]
 ; CHECK:       [[EXIT1]]:
-; CHECK-NEXT:    [[R:%.*]] = phi i64 [ [[NONPOISON]], %[[LOOP]] ], [ undef, %[[EXIT0]] ]
 ; CHECK-NEXT:    ret i64 [[R]]
 ; CHECK:       [[BACK_TO_LOOP]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
 ; CHECK:       [[BB]]:
-; CHECK-NEXT:    switch i64 [[NONPOISON]], label %[[LOOP]] [
+; CHECK-NEXT:    switch i64 [[R]], label %[[LOOP]] [
 ; CHECK-NEXT:      i64 0, label %[[BB0]]
 ; CHECK-NEXT:      i64 1, label %[[BB1]]
 ; CHECK-NEXT:      i64 2, label %[[BB2]]

>From f4d12ee9d3f0630759d714b8f703ce1496af8fa7 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 6 Mar 2025 22:54:19 +0800
Subject: [PATCH 3/5] Simplify test cases

---
 llvm/test/Analysis/ValueTracking/phi-self.ll | 126 ++++++-------------
 1 file changed, 40 insertions(+), 86 deletions(-)

diff --git a/llvm/test/Analysis/ValueTracking/phi-self.ll b/llvm/test/Analysis/ValueTracking/phi-self.ll
index 095a16983ce21..264d26657ccac 100644
--- a/llvm/test/Analysis/ValueTracking/phi-self.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-self.ll
@@ -3,143 +3,97 @@
 
 ; Test `%r` can be replaced by `%nonpoison`.
 
-define i64 @other_noundef() {
-; CHECK-LABEL: define i64 @other_noundef() {
+define i32 @other_noundef() {
+; CHECK-LABEL: define i32 @other_noundef() {
 ; CHECK-NEXT:  [[START:.*]]:
 ; CHECK-NEXT:    br label %[[LOOP:.*]]
 ; CHECK:       [[LOOP]]:
-; CHECK-NEXT:    [[R:%.*]] = phi i64 [ [[R]], %[[BB0:.*]] ], [ [[R]], %[[BB1:.*]] ], [ [[R]], %[[BB2:.*]] ], [ [[R]], %[[BB:.*]] ], [ [[I:%.*]], %[[BACK_TO_LOOP:.*]] ], [ 0, %[[START]] ]
-; CHECK-NEXT:    [[I]] = call i64 @opaque()
-; CHECK-NEXT:    switch i64 [[I]], label %[[EXIT0:.*]] [
-; CHECK-NEXT:      i64 -1, label %[[EXIT1:.*]]
-; CHECK-NEXT:      i64 2, label %[[BACK_TO_LOOP]]
-; CHECK-NEXT:      i64 0, label %[[BB]]
+; CHECK-NEXT:    [[NONPOISON:%.*]] = phi i32 [ 0, %[[START]] ], [ [[NONPOISON]], %[[BB0:.*]] ], [ 1, %[[BB1:.*]] ]
+; CHECK-NEXT:    [[I:%.*]] = call i32 @opaque()
+; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT0:.*]] [
+; CHECK-NEXT:      i32 0, label %[[BB0]]
+; CHECK-NEXT:      i32 1, label %[[BB1]]
+; CHECK-NEXT:      i32 2, label %[[EXIT1:.*]]
 ; CHECK-NEXT:    ]
 ; CHECK:       [[EXIT0]]:
 ; CHECK-NEXT:    br label %[[EXIT1]]
 ; CHECK:       [[EXIT1]]:
-; CHECK-NEXT:    ret i64 [[R]]
-; CHECK:       [[BACK_TO_LOOP]]:
-; CHECK-NEXT:    br label %[[LOOP]]
-; CHECK:       [[BB]]:
-; CHECK-NEXT:    switch i64 [[R]], label %[[LOOP]] [
-; CHECK-NEXT:      i64 0, label %[[BB0]]
-; CHECK-NEXT:      i64 1, label %[[BB1]]
-; CHECK-NEXT:      i64 2, label %[[BB2]]
-; CHECK-NEXT:    ]
+; CHECK-NEXT:    ret i32 [[NONPOISON]]
 ; CHECK:       [[BB0]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
 ; CHECK:       [[BB1]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
-; CHECK:       [[BB2]]:
-; CHECK-NEXT:    br label %[[LOOP]]
 ;
 start:
   br label %loop
 
-loop:                                             ; preds = %bb2, %bb1, %bb0, %bb, %back_to_loop, %start
-  %nonpoison = phi i64 [ %nonpoison, %bb0 ], [ %nonpoison, %bb1 ], [ %nonpoison, %bb2 ], [ %nonpoison, %bb ], [ %i, %back_to_loop ], [ 0, %start ]
-  %i = call i64 @opaque()
-  switch i64 %i, label %exit0 [
-  i64 -1, label %exit1
-  i64 2, label %back_to_loop
-  i64 0, label %bb
+loop:                                             ; preds = %bb1, %bb0, %start
+  %nonpoison = phi i32 [ 0, %start ], [ %nonpoison, %bb0 ], [ 1, %bb1 ]
+  %i = call i32 @opaque()
+  switch i32 %i, label %exit0 [
+  i32 0, label %bb0
+  i32 1, label %bb1
+  i32 2, label %exit1
   ]
 
 exit0:                                            ; preds = %loop
   br label %exit1
 
 exit1:                                            ; preds = %exit0, %loop
-  %r = phi i64 [ %nonpoison, %loop ], [ undef, %exit0 ]
-  ret i64 %r
-
-back_to_loop:                                     ; preds = %loop
-  br label %loop
-
-bb:                                               ; preds = %loop
-  switch i64 %nonpoison, label %loop [
-  i64 0, label %bb0
-  i64 1, label %bb1
-  i64 2, label %bb2
-  ]
-
-bb0:                                              ; preds = %bb
-  br label %loop
+  %r = phi i32 [ %nonpoison, %loop ], [ undef, %exit0 ]
+  ret i32 %r
 
-bb1:                                              ; preds = %bb
+bb0:                                              ; preds = %loop
   br label %loop
 
-bb2:                                              ; preds = %bb
+bb1:                                              ; preds = %loop
   br label %loop
 }
 
-define i64 @other_poison() {
-; CHECK-LABEL: define i64 @other_poison() {
+define i32 @other_poison() {
+; CHECK-LABEL: define i32 @other_poison() {
 ; CHECK-NEXT:  [[START:.*:]]
 ; CHECK-NEXT:    br label %[[LOOP:.*]]
 ; CHECK:       [[LOOP]]:
-; CHECK-NEXT:    [[I:%.*]] = call i64 @opaque()
-; CHECK-NEXT:    switch i64 [[I]], label %[[EXIT0:.*]] [
-; CHECK-NEXT:      i64 -1, label %[[EXIT1:.*]]
-; CHECK-NEXT:      i64 2, label %[[BACK_TO_LOOP:.*]]
-; CHECK-NEXT:      i64 0, label %[[BB:.*]]
+; CHECK-NEXT:    [[I:%.*]] = call i32 @opaque()
+; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT0:.*]] [
+; CHECK-NEXT:      i32 0, label %[[BB0:.*]]
+; CHECK-NEXT:      i32 1, label %[[BB1:.*]]
+; CHECK-NEXT:      i32 2, label %[[EXIT1:.*]]
 ; CHECK-NEXT:    ]
 ; CHECK:       [[EXIT0]]:
 ; CHECK-NEXT:    br label %[[EXIT1]]
 ; CHECK:       [[EXIT1]]:
-; CHECK-NEXT:    ret i64 0
-; CHECK:       [[BACK_TO_LOOP]]:
-; CHECK-NEXT:    br label %[[LOOP]]
-; CHECK:       [[BB]]:
-; CHECK-NEXT:    switch i64 0, label %[[LOOP]] [
-; CHECK-NEXT:      i64 0, label %[[BB0:.*]]
-; CHECK-NEXT:      i64 1, label %[[BB1:.*]]
-; CHECK-NEXT:      i64 2, label %[[BB2:.*]]
-; CHECK-NEXT:    ]
+; CHECK-NEXT:    ret i32 0
 ; CHECK:       [[BB0]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
 ; CHECK:       [[BB1]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
-; CHECK:       [[BB2]]:
-; CHECK-NEXT:    br label %[[LOOP]]
 ;
 start:
   br label %loop
 
-loop:                                             ; preds = %bb2, %bb1, %bb0, %bb, %back_to_loop, %start
-  %maypoison = phi i64 [ %maypoison, %bb0 ], [ %maypoison, %bb1 ], [ %maypoison, %bb2 ], [ %maypoison, %bb ], [ poison, %back_to_loop ], [ 0, %start ]
-  %i = call i64 @opaque()
-  switch i64 %i, label %exit0 [
-  i64 -1, label %exit1
-  i64 2, label %back_to_loop
-  i64 0, label %bb
+loop:                                             ; preds = %bb1, %bb0, %start
+  %maypoison = phi i32 [ 0, %start ], [ %maypoison, %bb0 ], [ poison, %bb1 ]
+  %i = call i32 @opaque()
+  switch i32 %i, label %exit0 [
+  i32 0, label %bb0
+  i32 1, label %bb1
+  i32 2, label %exit1
   ]
 
 exit0:                                            ; preds = %loop
   br label %exit1
 
 exit1:                                            ; preds = %exit0, %loop
-  %r = phi i64 [ %maypoison, %loop ], [ undef, %exit0 ]
-  ret i64 %r
-
-back_to_loop:                                     ; preds = %loop
-  br label %loop
-
-bb:                                               ; preds = %loop
-  switch i64 %maypoison, label %loop [
-  i64 0, label %bb0
-  i64 1, label %bb1
-  i64 2, label %bb2
-  ]
-
-bb0:                                              ; preds = %bb
-  br label %loop
+  %r = phi i32 [ %maypoison, %loop ], [ undef, %exit0 ]
+  ret i32 %r
 
-bb1:                                              ; preds = %bb
+bb0:                                              ; preds = %loop
   br label %loop
 
-bb2:                                              ; preds = %bb
+bb1:                                              ; preds = %loop
   br label %loop
 }
 
-declare i64 @opaque()
+declare i32 @opaque()

>From e1a8bfb0998458336a9a254e60edf1d7c8dc5e1a Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 6 Mar 2025 23:08:46 +0800
Subject: [PATCH 4/5] Address feedback

---
 llvm/test/Analysis/ValueTracking/phi-self.ll | 46 +++++++-------------
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/llvm/test/Analysis/ValueTracking/phi-self.ll b/llvm/test/Analysis/ValueTracking/phi-self.ll
index 264d26657ccac..46bba4aac96a8 100644
--- a/llvm/test/Analysis/ValueTracking/phi-self.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-self.ll
@@ -10,14 +10,11 @@ define i32 @other_noundef() {
 ; CHECK:       [[LOOP]]:
 ; CHECK-NEXT:    [[NONPOISON:%.*]] = phi i32 [ 0, %[[START]] ], [ [[NONPOISON]], %[[BB0:.*]] ], [ 1, %[[BB1:.*]] ]
 ; CHECK-NEXT:    [[I:%.*]] = call i32 @opaque()
-; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT0:.*]] [
+; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT:.*]] [
 ; CHECK-NEXT:      i32 0, label %[[BB0]]
 ; CHECK-NEXT:      i32 1, label %[[BB1]]
-; CHECK-NEXT:      i32 2, label %[[EXIT1:.*]]
 ; CHECK-NEXT:    ]
-; CHECK:       [[EXIT0]]:
-; CHECK-NEXT:    br label %[[EXIT1]]
-; CHECK:       [[EXIT1]]:
+; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret i32 [[NONPOISON]]
 ; CHECK:       [[BB0]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
@@ -27,26 +24,22 @@ define i32 @other_noundef() {
 start:
   br label %loop
 
-loop:                                             ; preds = %bb1, %bb0, %start
+loop:
   %nonpoison = phi i32 [ 0, %start ], [ %nonpoison, %bb0 ], [ 1, %bb1 ]
   %i = call i32 @opaque()
-  switch i32 %i, label %exit0 [
+  switch i32 %i, label %exit [
   i32 0, label %bb0
   i32 1, label %bb1
-  i32 2, label %exit1
   ]
 
-exit0:                                            ; preds = %loop
-  br label %exit1
-
-exit1:                                            ; preds = %exit0, %loop
-  %r = phi i32 [ %nonpoison, %loop ], [ undef, %exit0 ]
+exit:
+  %r = freeze i32 %nonpoison
   ret i32 %r
 
-bb0:                                              ; preds = %loop
+bb0:
   br label %loop
 
-bb1:                                              ; preds = %loop
+bb1:
   br label %loop
 }
 
@@ -56,14 +49,11 @@ define i32 @other_poison() {
 ; CHECK-NEXT:    br label %[[LOOP:.*]]
 ; CHECK:       [[LOOP]]:
 ; CHECK-NEXT:    [[I:%.*]] = call i32 @opaque()
-; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT0:.*]] [
+; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT:.*]] [
 ; CHECK-NEXT:      i32 0, label %[[BB0:.*]]
 ; CHECK-NEXT:      i32 1, label %[[BB1:.*]]
-; CHECK-NEXT:      i32 2, label %[[EXIT1:.*]]
 ; CHECK-NEXT:    ]
-; CHECK:       [[EXIT0]]:
-; CHECK-NEXT:    br label %[[EXIT1]]
-; CHECK:       [[EXIT1]]:
+; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret i32 0
 ; CHECK:       [[BB0]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
@@ -73,26 +63,22 @@ define i32 @other_poison() {
 start:
   br label %loop
 
-loop:                                             ; preds = %bb1, %bb0, %start
+loop:
   %maypoison = phi i32 [ 0, %start ], [ %maypoison, %bb0 ], [ poison, %bb1 ]
   %i = call i32 @opaque()
-  switch i32 %i, label %exit0 [
+  switch i32 %i, label %exit [
   i32 0, label %bb0
   i32 1, label %bb1
-  i32 2, label %exit1
   ]
 
-exit0:                                            ; preds = %loop
-  br label %exit1
-
-exit1:                                            ; preds = %exit0, %loop
-  %r = phi i32 [ %maypoison, %loop ], [ undef, %exit0 ]
+exit:
+  %r = freeze i32 %maypoison
   ret i32 %r
 
-bb0:                                              ; preds = %loop
+bb0:
   br label %loop
 
-bb1:                                              ; preds = %loop
+bb1:
   br label %loop
 }
 

>From 692930753b342a4e12c281af82a6be01cb938772 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 6 Mar 2025 23:15:28 +0800
Subject: [PATCH 5/5] Make testing more robust

---
 llvm/test/Analysis/ValueTracking/phi-self.ll | 26 +++++++++++---------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/llvm/test/Analysis/ValueTracking/phi-self.ll b/llvm/test/Analysis/ValueTracking/phi-self.ll
index 46bba4aac96a8..17afd872cab03 100644
--- a/llvm/test/Analysis/ValueTracking/phi-self.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-self.ll
@@ -3,12 +3,13 @@
 
 ; Test `%r` can be replaced by `%nonpoison`.
 
-define i32 @other_noundef() {
-; CHECK-LABEL: define i32 @other_noundef() {
+define i32 @other_noundef(i32 noundef %arg) {
+; CHECK-LABEL: define i32 @other_noundef(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
 ; CHECK-NEXT:  [[START:.*]]:
 ; CHECK-NEXT:    br label %[[LOOP:.*]]
 ; CHECK:       [[LOOP]]:
-; CHECK-NEXT:    [[NONPOISON:%.*]] = phi i32 [ 0, %[[START]] ], [ [[NONPOISON]], %[[BB0:.*]] ], [ 1, %[[BB1:.*]] ]
+; CHECK-NEXT:    [[NONPOISON:%.*]] = phi i32 [ 0, %[[START]] ], [ [[NONPOISON]], %[[BB0:.*]] ], [ [[ARG]], %[[BB1:.*]] ]
 ; CHECK-NEXT:    [[I:%.*]] = call i32 @opaque()
 ; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT:.*]] [
 ; CHECK-NEXT:      i32 0, label %[[BB0]]
@@ -25,7 +26,7 @@ start:
   br label %loop
 
 loop:
-  %nonpoison = phi i32 [ 0, %start ], [ %nonpoison, %bb0 ], [ 1, %bb1 ]
+  %nonpoison = phi i32 [ 0, %start ], [ %nonpoison, %bb0 ], [ %arg, %bb1 ]
   %i = call i32 @opaque()
   switch i32 %i, label %exit [
   i32 0, label %bb0
@@ -43,18 +44,21 @@ bb1:
   br label %loop
 }
 
-define i32 @other_poison() {
-; CHECK-LABEL: define i32 @other_poison() {
-; CHECK-NEXT:  [[START:.*:]]
+define i32 @other_poison(i32 %arg) {
+; CHECK-LABEL: define i32 @other_poison(
+; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-NEXT:  [[START:.*]]:
 ; CHECK-NEXT:    br label %[[LOOP:.*]]
 ; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[MAYPOISON:%.*]] = phi i32 [ 0, %[[START]] ], [ [[MAYPOISON]], %[[BB0:.*]] ], [ [[ARG]], %[[BB1:.*]] ]
 ; CHECK-NEXT:    [[I:%.*]] = call i32 @opaque()
 ; CHECK-NEXT:    switch i32 [[I]], label %[[EXIT:.*]] [
-; CHECK-NEXT:      i32 0, label %[[BB0:.*]]
-; CHECK-NEXT:      i32 1, label %[[BB1:.*]]
+; CHECK-NEXT:      i32 0, label %[[BB0]]
+; CHECK-NEXT:      i32 1, label %[[BB1]]
 ; CHECK-NEXT:    ]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    [[R:%.*]] = freeze i32 [[MAYPOISON]]
+; CHECK-NEXT:    ret i32 [[R]]
 ; CHECK:       [[BB0]]:
 ; CHECK-NEXT:    br label %[[LOOP]]
 ; CHECK:       [[BB1]]:
@@ -64,7 +68,7 @@ start:
   br label %loop
 
 loop:
-  %maypoison = phi i32 [ 0, %start ], [ %maypoison, %bb0 ], [ poison, %bb1 ]
+  %maypoison = phi i32 [ 0, %start ], [ %maypoison, %bb0 ], [ %arg, %bb1 ]
   %i = call i32 @opaque()
   switch i32 %i, label %exit [
   i32 0, label %bb0



More information about the llvm-commits mailing list