[llvm] [LVI][CVP] propagates `undef` to range and `abs` (PR #68190)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 9 18:16:59 PDT 2023


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

>From c6573e3d05c5f1ea6030da66934c695b8c69cd9f Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 10 Oct 2023 09:07:20 +0800
Subject: [PATCH 1/2] [LVI][CVP] Pre-commit for `getConstantRangeOrFull`

---
 .../merge-range-and-undef.ll                  | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll b/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll
index 2aba1f0a9919067..01cf05ba21a6e28 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll
@@ -382,3 +382,39 @@ exit: ; CVP only simplifies based on ranges for non-local conditions.
   call void @use(i1 %t.1)
   ret i64 %res
 }
+
+; Test case for PR68381.
+; Because of `undef`, we can only delete the second `and` instruction.
+define i32 @constant_range_and_undef_and(i1 %c0, i1 %c1, i8 %v1, i8 %v2) {
+; CHECK-LABEL: @constant_range_and_undef_and(
+; CHECK-NEXT:  start:
+; CHECK-NEXT:    br i1 [[C0:%.*]], label [[BB0:%.*]], label [[BB1:%.*]]
+; CHECK:       bb0:
+; CHECK-NEXT:    [[V1_I32:%.*]] = zext i8 [[V1:%.*]] to i32
+; CHECK-NEXT:    br label [[BB1]]
+; CHECK:       bb1:
+; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[V1_I32]], [[BB0]] ], [ undef, [[START:%.*]] ]
+; CHECK-NEXT:    br i1 [[C1:%.*]], label [[BB0]], label [[BB2:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    [[V2_I32:%.*]] = zext i8 [[V2:%.*]] to i32
+; CHECK-NEXT:    [[Y:%.*]] = or i32 [[X]], [[V2_I32]]
+; CHECK-NEXT:    ret i32 [[Y]]
+;
+start:
+  br i1 %c0, label %bb0, label %bb1
+
+bb0:
+  %v1_i32 = zext i8 %v1 to i32
+  br label %bb1
+
+bb1:
+  %x = phi i32 [ %v1_i32, %bb0 ], [ undef, %start ]
+  br i1 %c1, label %bb0, label %bb2
+
+bb2:
+  %v2_i32 = zext i8 %v2 to i32
+  %y = or i32 %x, %v2_i32
+  %z = and i32 %y, 255
+  %z1 = and i32 %z, 255
+  ret i32 %z1
+}

>From d0cd71ca01d8e49b2e2310ec8f6fc52d50efb676 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 10 Oct 2023 09:14:08 +0800
Subject: [PATCH 2/2] [LVI][CVP] Treat undef like a full range

---
 llvm/lib/Analysis/LazyValueInfo.cpp                            | 2 +-
 .../CorrelatedValuePropagation/merge-range-and-undef.ll        | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 0892aa9d75fb417..789a02ed329c909 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -801,7 +801,7 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
 
 static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val,
                                             Type *Ty, const DataLayout &DL) {
-  if (Val.isConstantRange())
+  if (Val.isConstantRange(/*UndefAllowed*/ false))
     return Val.getConstantRange();
   return ConstantRange::getFull(DL.getTypeSizeInBits(Ty));
 }
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll b/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll
index 01cf05ba21a6e28..1d70932ee857b71 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll
@@ -398,7 +398,8 @@ define i32 @constant_range_and_undef_and(i1 %c0, i1 %c1, i8 %v1, i8 %v2) {
 ; CHECK:       bb2:
 ; CHECK-NEXT:    [[V2_I32:%.*]] = zext i8 [[V2:%.*]] to i32
 ; CHECK-NEXT:    [[Y:%.*]] = or i32 [[X]], [[V2_I32]]
-; CHECK-NEXT:    ret i32 [[Y]]
+; CHECK-NEXT:    [[Z:%.*]] = and i32 [[Y]], 255
+; CHECK-NEXT:    ret i32 [[Z]]
 ;
 start:
   br i1 %c0, label %bb0, label %bb1



More information about the llvm-commits mailing list