[llvm] [LVI] Use m_AddLike instead of m_Add when matching simple condition (PR #86058)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 21 11:53:39 PDT 2024
https://github.com/goldsteinn updated https://github.com/llvm/llvm-project/pull/86058
>From 2fe55be1d69da77288931d25f5f4ee4d82fd74be Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Wed, 20 Mar 2024 19:40:06 -0500
Subject: [PATCH 1/2] [LVI] Add tests for tracking `or disjoint` like add; NFC
---
.../CorrelatedValuePropagation/basic.ll | 58 +++++++++++++++++++
.../CorrelatedValuePropagation/icmp.ll | 21 +++++++
2 files changed, 79 insertions(+)
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
index 8dce9ef9fa4350..94de0128a09ac1 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
@@ -870,6 +870,34 @@ out:
ret i1 false
}
+define i1 @clamp_high1_or(i32 noundef %a) {
+; CHECK-LABEL: @clamp_high1_or(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
+; CHECK-NEXT: br i1 [[CMP]], label [[A_GUARD:%.*]], label [[OUT:%.*]]
+; CHECK: a_guard:
+; CHECK-NEXT: [[SEL_CMP:%.*]] = icmp eq i32 [[A]], 5
+; CHECK-NEXT: [[ADD:%.*]] = or disjoint i32 [[A]], 1
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[SEL_CMP]], i32 5, i32 [[ADD]]
+; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[SEL]], 6
+; CHECK-NEXT: ret i1 [[RES]]
+; CHECK: out:
+; CHECK-NEXT: ret i1 false
+;
+entry:
+ %cmp = icmp sle i32 %a, 5
+ br i1 %cmp, label %a_guard, label %out
+
+a_guard:
+ %sel_cmp = icmp eq i32 %a, 5
+ %add = or disjoint i32 %a, 1
+ %sel = select i1 %sel_cmp, i32 5, i32 %add
+ %res = icmp eq i32 %sel, 6
+ ret i1 %res
+out:
+ ret i1 false
+}
+
define i1 @clamp_high2(i32 noundef %a) {
; CHECK-LABEL: @clamp_high2(
; CHECK-NEXT: entry:
@@ -897,6 +925,36 @@ out:
ret i1 false
}
+
+define i1 @clamp_high2_or_disjoint(i32 noundef %a) {
+; CHECK-LABEL: @clamp_high2_or_disjoint(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
+; CHECK-NEXT: br i1 [[CMP]], label [[A_GUARD:%.*]], label [[OUT:%.*]]
+; CHECK: a_guard:
+; CHECK-NEXT: [[SEL_CMP:%.*]] = icmp ne i32 [[A]], 5
+; CHECK-NEXT: [[ADD:%.*]] = or disjoint i32 [[A]], 1
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[SEL_CMP]], i32 [[ADD]], i32 5
+; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[SEL]], 6
+; CHECK-NEXT: ret i1 [[RES]]
+; CHECK: out:
+; CHECK-NEXT: ret i1 false
+;
+entry:
+ %cmp = icmp sle i32 %a, 5
+ br i1 %cmp, label %a_guard, label %out
+
+a_guard:
+ %sel_cmp = icmp ne i32 %a, 5
+ %add = or disjoint i32 %a, 1
+ %sel = select i1 %sel_cmp, i32 %add, i32 5
+ %res = icmp eq i32 %sel, 6
+ ret i1 %res
+out:
+ ret i1 false
+}
+
+
define i1 @clamp_high3(i32 noundef %a) {
; CHECK-LABEL: @clamp_high3(
; CHECK-NEXT: entry:
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index 101820a4c65f23..b534e3fd2137c0 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -587,6 +587,27 @@ define i1 @test_assume_cmp_with_offset(i64 %idx) {
ret i1 %cmp2
}
+define i1 @test_assume_cmp_with_offset_or(i64 %idx, i1 %other) {
+; CHECK-LABEL: @test_assume_cmp_with_offset_or(
+; CHECK-NEXT: [[IDX_OFF1:%.*]] = or disjoint i64 [[IDX:%.*]], 5
+; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i64 [[IDX_OFF1]], 10
+; CHECK-NEXT: br i1 [[CMP1]], label [[T:%.*]], label [[F:%.*]]
+; CHECK: T:
+; CHECK-NEXT: [[CMP3:%.*]] = icmp ugt i64 [[IDX]], 2
+; CHECK-NEXT: ret i1 [[CMP3]]
+; CHECK: F:
+; CHECK-NEXT: ret i1 [[CMP2:%.*]]
+;
+ %idx.off1 = or disjoint i64 %idx, 5
+ %cmp1 = icmp ugt i64 %idx.off1, 10
+ br i1 %cmp1, label %T, label %F
+T:
+ %cmp2 = icmp ugt i64 %idx, 2
+ ret i1 %cmp2
+F:
+ ret i1 %other
+}
+
define void @test_cmp_phi(i8 %a) {
; CHECK-LABEL: @test_cmp_phi(
; CHECK-NEXT: entry:
>From 23bbdef8ea9b9854229d21cf1ddef279d86f5fb4 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Wed, 20 Mar 2024 18:13:18 -0500
Subject: [PATCH 2/2] [LVI] Use m_AddLike instead of m_Add when matching simple
condition
We have more complete logic for handling `Add`, so try to use that
logic for `or disjoint` (which can definitionally be treated as
`add`).
---
llvm/lib/Analysis/LazyValueInfo.cpp | 4 ++--
llvm/test/Transforms/CorrelatedValuePropagation/basic.ll | 6 ++----
llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll | 3 +--
3 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 9ae31d165235ca..b8bc81197c95c0 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1069,14 +1069,14 @@ static bool matchICmpOperand(APInt &Offset, Value *LHS, Value *Val,
// Handle range checking idiom produced by InstCombine. We will subtract the
// offset from the allowed range for RHS in this case.
const APInt *C;
- if (match(LHS, m_Add(m_Specific(Val), m_APInt(C)))) {
+ if (match(LHS, m_AddLike(m_Specific(Val), m_APInt(C)))) {
Offset = *C;
return true;
}
// Handle the symmetric case. This appears in saturation patterns like
// (x == 16) ? 16 : (x + 1).
- if (match(Val, m_Add(m_Specific(LHS), m_APInt(C)))) {
+ if (match(Val, m_AddLike(m_Specific(LHS), m_APInt(C)))) {
Offset = -*C;
return true;
}
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
index 94de0128a09ac1..701d867416a13e 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
@@ -879,8 +879,7 @@ define i1 @clamp_high1_or(i32 noundef %a) {
; CHECK-NEXT: [[SEL_CMP:%.*]] = icmp eq i32 [[A]], 5
; CHECK-NEXT: [[ADD:%.*]] = or disjoint i32 [[A]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[SEL_CMP]], i32 5, i32 [[ADD]]
-; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[SEL]], 6
-; CHECK-NEXT: ret i1 [[RES]]
+; CHECK-NEXT: ret i1 false
; CHECK: out:
; CHECK-NEXT: ret i1 false
;
@@ -935,8 +934,7 @@ define i1 @clamp_high2_or_disjoint(i32 noundef %a) {
; CHECK-NEXT: [[SEL_CMP:%.*]] = icmp ne i32 [[A]], 5
; CHECK-NEXT: [[ADD:%.*]] = or disjoint i32 [[A]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[SEL_CMP]], i32 [[ADD]], i32 5
-; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[SEL]], 6
-; CHECK-NEXT: ret i1 [[RES]]
+; CHECK-NEXT: ret i1 false
; CHECK: out:
; CHECK-NEXT: ret i1 false
;
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index b534e3fd2137c0..b5337b9ddc2487 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -593,8 +593,7 @@ define i1 @test_assume_cmp_with_offset_or(i64 %idx, i1 %other) {
; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i64 [[IDX_OFF1]], 10
; CHECK-NEXT: br i1 [[CMP1]], label [[T:%.*]], label [[F:%.*]]
; CHECK: T:
-; CHECK-NEXT: [[CMP3:%.*]] = icmp ugt i64 [[IDX]], 2
-; CHECK-NEXT: ret i1 [[CMP3]]
+; CHECK-NEXT: ret i1 true
; CHECK: F:
; CHECK-NEXT: ret i1 [[CMP2:%.*]]
;
More information about the llvm-commits
mailing list