[llvm] [ValueTracking] Handle trunc to i1 as condition in dominating condition. (PR #126414)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 9 03:13:38 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Andreas Jonson (andjo403)
<details>
<summary>Changes</summary>
proof: https://alive2.llvm.org/ce/z/j5fZgV
---
Full diff: https://github.com/llvm/llvm-project/pull/126414.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/ValueTracking.cpp (+19)
- (modified) llvm/test/Transforms/InstCombine/known-bits.ll (+10-20)
``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 8a9ad55366ee703..40fae7ca95d774b 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -801,6 +801,22 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond,
if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
+
+ if (match(Cond, m_Not(m_Value(Cond))))
+ Invert = !Invert;
+
+ if (match(Cond, m_Trunc(m_Specific(V)))) {
+ KnownBits DstKnown(1);
+ if (Invert) {
+ DstKnown.setAllZero();
+ } else {
+ DstKnown.setAllOnes();
+ }
+ if (cast<TruncInst>(Cond)->hasNoUnsignedWrap())
+ Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
+ else
+ Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
+ }
}
void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
@@ -10272,6 +10288,9 @@ void llvm::findValuesAffectedByCondition(
m_Value()))) {
// Handle patterns that computeKnownFPClass() support.
AddAffected(A);
+ } else if (!IsAssume && match(V, m_CombineOr(m_Trunc(m_Value(X)),
+ m_Not(m_Trunc(m_Value(X)))))) {
+ AddAffected(X);
}
}
}
diff --git a/llvm/test/Transforms/InstCombine/known-bits.ll b/llvm/test/Transforms/InstCombine/known-bits.ll
index a3872fefecf3b31..b47c3de21f9feaf 100644
--- a/llvm/test/Transforms/InstCombine/known-bits.ll
+++ b/llvm/test/Transforms/InstCombine/known-bits.ll
@@ -2167,11 +2167,9 @@ define i8 @test_trunc_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
-; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[B]]
+; CHECK-NEXT: ret i8 1
; CHECK: if.else:
-; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[C]]
+; CHECK-NEXT: ret i8 0
;
entry:
%cast = trunc i8 %a to i1
@@ -2192,11 +2190,9 @@ define i8 @test_not_trunc_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
-; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[B]]
+; CHECK-NEXT: ret i8 0
; CHECK: if.else:
-; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[C]]
+; CHECK-NEXT: ret i8 1
;
entry:
%cast = trunc i8 %a to i1
@@ -2243,11 +2239,9 @@ define i8 @test_trunc_nuw_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc nuw i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
-; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[B]]
+; CHECK-NEXT: ret i8 0
; CHECK: if.else:
-; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[C]]
+; CHECK-NEXT: ret i8 1
;
entry:
%cast = trunc nuw i8 %a to i1
@@ -2268,11 +2262,9 @@ define i8 @test_trunc_nuw_or_2(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc nuw i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
-; CHECK-NEXT: [[B:%.*]] = or i8 [[A]], 2
-; CHECK-NEXT: ret i8 [[B]]
+; CHECK-NEXT: ret i8 2
; CHECK: if.else:
-; CHECK-NEXT: [[C:%.*]] = or i8 [[A]], 2
-; CHECK-NEXT: ret i8 [[C]]
+; CHECK-NEXT: ret i8 3
;
entry:
%cast = trunc nuw i8 %a to i1
@@ -2293,11 +2285,9 @@ define i8 @test_not_trunc_nuw_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc nuw i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
-; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[B]]
+; CHECK-NEXT: ret i8 0
; CHECK: if.else:
-; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
-; CHECK-NEXT: ret i8 [[C]]
+; CHECK-NEXT: ret i8 1
;
entry:
%cast = trunc nuw i8 %a to i1
``````````
</details>
https://github.com/llvm/llvm-project/pull/126414
More information about the llvm-commits
mailing list