[llvm] [ValueTracking] Try to infer range of select from true and false values. (PR #68256)

Mikhail Gudim via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 4 14:43:13 PDT 2023


https://github.com/mgudim updated https://github.com/llvm/llvm-project/pull/68256

>From 67257c0dadd6a5f2b2a7746b00e0e7588598433c Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Wed, 4 Oct 2023 15:58:29 -0400
Subject: [PATCH 1/2] [ValueTracking] Try to infer range of select from true
 and false values.

When computing range of `select` instruction, first compute the union of
ranges of "True" and "False" operands of the `select` instruction.
---
 llvm/lib/Analysis/ValueTracking.cpp               |  9 +++++++--
 .../ValueTracking/constant-range-select.ll        | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/Analysis/ValueTracking/constant-range-select.ll

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 0736ef65b306519..4078376124b1750 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8854,11 +8854,16 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
   } else if (auto *II = dyn_cast<IntrinsicInst>(V))
     CR = getRangeForIntrinsic(*II);
   else if (auto *SI = dyn_cast<SelectInst>(V)) {
+    ConstantRange CRTrue = computeConstantRange(
+        SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
+    ConstantRange CRFalse = computeConstantRange(
+        SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
+    CR = CRTrue.unionWith(CRFalse);
+    // TODO: Return ConstantRange.
     APInt Lower = APInt(BitWidth, 0);
     APInt Upper = APInt(BitWidth, 0);
-    // TODO: Return ConstantRange.
     setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
-    CR = ConstantRange::getNonEmpty(Lower, Upper);
+    CR = CR.intersectWith(ConstantRange::getNonEmpty(Lower, Upper));
   } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
     APInt Lower = APInt(BitWidth, 0);
     APInt Upper = APInt(BitWidth, 0);
diff --git a/llvm/test/Analysis/ValueTracking/constant-range-select.ll b/llvm/test/Analysis/ValueTracking/constant-range-select.ll
new file mode 100644
index 000000000000000..353fa7e1cf5dd33
--- /dev/null
+++ b/llvm/test/Analysis/ValueTracking/constant-range-select.ll
@@ -0,0 +1,15 @@
+; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
+
+ at a = dso_local local_unnamed_addr global [10 x i32] zeroinitializer, align 4
+
+; CHECK-LABEL: Function: select_in_gep
+; CHECK: NoAlias: i32* %arrayidx, i32* getelementptr inbounds ([10 x i32], ptr @a, i64 0, i64 3)
+define i32 @select_in_gep(i1 %c)  {
+entry:
+  %cond = select i1 %c, i64 2, i64 1
+  %0 = load i32, ptr getelementptr inbounds ([10 x i32], ptr @a, i64 0, i64 3), align 4
+  %arrayidx = getelementptr inbounds [10 x i32], ptr @a, i64 0, i64 %cond
+  store i32 %0, ptr %arrayidx, align 4
+  %1 = load i32, ptr getelementptr inbounds ([10 x i32], ptr @a, i64 0, i64 3), align 4
+  ret i32 %1
+}

>From 6663d200f4d5511793fa287ced3eb5c70bf29597 Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Wed, 4 Oct 2023 17:41:03 -0400
Subject: [PATCH 2/2] Fixed failing test.

---
 llvm/test/Transforms/InstCombine/binop-select.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/binop-select.ll b/llvm/test/Transforms/InstCombine/binop-select.ll
index a59e19897f061d1..6cd4132eadd77b9 100644
--- a/llvm/test/Transforms/InstCombine/binop-select.ll
+++ b/llvm/test/Transforms/InstCombine/binop-select.ll
@@ -324,12 +324,12 @@ define i32 @sub_sel_op1_use(i1 %b) {
 ; CHECK-LABEL: @sub_sel_op1_use(
 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[B:%.*]], i32 42, i32 41
 ; CHECK-NEXT:    call void @use(i32 [[S]])
-; CHECK-NEXT:    [[R:%.*]] = sub nsw i32 42, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw nsw i32 42, [[S]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %s = select i1 %b, i32 42, i32 41
   call void @use(i32 %s)
-  %r = sub nsw i32 42, %s
+  %r = sub nuw nsw i32 42, %s
   ret i32 %r
 }
 



More information about the llvm-commits mailing list