[llvm] [InstCombine] Fold `trunc nuw (x xor y) to i1` to `x != y` (PR #90408)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 28 11:05:55 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Monad (YanWQ-monad)
<details>
<summary>Changes</summary>
Fold:
``` llvm
define i1 @<!-- -->src(i64 %x, i64 %y) {
entry:
%xor = xor i64 %x, %y
%r = trunc nuw i64 %xor to i1
ret i1 %r
}
define i1 @<!-- -->tgt(i64 %x, i64 %y) {
%r = icmp ne i64 %x, %y
ret i1 %r
}
```
Proof: https://alive2.llvm.org/ce/z/RSWpbS
---
Full diff: https://github.com/llvm/llvm-project/pull/90408.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (+7)
- (modified) llvm/test/Transforms/InstCombine/trunc.ll (+31)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 567b27b4630439..c769c28c90c1a9 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -770,6 +770,13 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
return new ICmpInst(ICmpInst::Predicate::ICMP_EQ, X, Zero);
}
}
+
+ if (Trunc.hasNoUnsignedWrap()) {
+ Value *X, *Y;
+ if (match(Src, m_Xor(m_Value(X), m_Value(Y)))) {
+ return new ICmpInst(ICmpInst::ICMP_NE, X, Y);
+ }
+ }
}
Value *A, *B;
diff --git a/llvm/test/Transforms/InstCombine/trunc.ll b/llvm/test/Transforms/InstCombine/trunc.ll
index e59b2bea6684c0..d4e4b1866349c0 100644
--- a/llvm/test/Transforms/InstCombine/trunc.ll
+++ b/llvm/test/Transforms/InstCombine/trunc.ll
@@ -1054,3 +1054,34 @@ define i8 @drop_both_trunc(i16 %x, i16 %y) {
%res = trunc nuw nsw i16 %and2 to i8
ret i8 %res
}
+
+define i1 @trunc_xor(i8 %x, i8 %y) {
+; CHECK-LABEL: @trunc_xor(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = trunc i8 [[XOR]] to i1
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %xor = xor i8 %x, %y
+ %r = trunc i8 %xor to i1
+ ret i1 %r
+}
+
+define i1 @trunc_nuw_xor(i8 %x, i8 %y) {
+; CHECK-LABEL: @trunc_nuw_xor(
+; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %xor = xor i8 %x, %y
+ %r = trunc nuw i8 %xor to i1
+ ret i1 %r
+}
+
+define <2 x i1> @trunc_nuw_xor_vector(<2 x i8> %x, <2 x i8> %y) {
+; CHECK-LABEL: @trunc_nuw_xor_vector(
+; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret <2 x i1> [[R]]
+;
+ %xor = xor <2 x i8> %x, %y
+ %r = trunc nuw <2 x i8> %xor to <2 x i1>
+ ret <2 x i1> %r
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/90408
More information about the llvm-commits
mailing list