[llvm] cd87fe0 - [Instsimplfy] X == Y ? 0 : X ^ Y --> X ^ Y
via llvm-commits
llvm-commits at lists.llvm.org
Wed May 17 03:38:20 PDT 2023
Author: Zhongyunde
Date: 2023-05-17T18:36:39+08:00
New Revision: cd87fe0c8be3c462f5c0bac745d740d0b5185562
URL: https://github.com/llvm/llvm-project/commit/cd87fe0c8be3c462f5c0bac745d740d0b5185562
DIFF: https://github.com/llvm/llvm-project/commit/cd87fe0c8be3c462f5c0bac745d740d0b5185562.diff
LOG: [Instsimplfy] X == Y ? 0 : X ^ Y --> X ^ Y
Alive2: https://alive2.llvm.org/ce/z/cykffE
Fixes: https://github.com/llvm/llvm-project/issues/62753
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D150750
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/select.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 89ac18bbddf5..3b951b9c8076 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4259,9 +4259,11 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
NewOps[0] == NewOps[1])
return NewOps[0];
- // x - x -> 0. This is non-refining, because x is non-poison by assumption
- // and this case never wraps, so nowrap flags can be ignored.
- if (Opcode == Instruction::Sub && NewOps[0] == NewOps[1]) {
+ // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
+ // by assumption and this case never wraps, so nowrap flags can be
+ // ignored.
+ if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
+ NewOps[0] == NewOps[1]) {
assert(NewOps[0] == RepOp && "Precondition for non-poison assumption");
return Constant::getNullValue(I->getType());
}
diff --git a/llvm/test/Transforms/InstSimplify/select.ll b/llvm/test/Transforms/InstSimplify/select.ll
index a77e8df1b8b4..384da54ed2b1 100644
--- a/llvm/test/Transforms/InstSimplify/select.ll
+++ b/llvm/test/Transforms/InstSimplify/select.ll
@@ -1496,3 +1496,62 @@ define i8 @select_sub_cmp_nonzero(i8 %0, i8 %1) {
%5 = select i1 %3, i8 42, i8 %4
ret i8 %5
}
+
+; X == Y ? 0 : X ^ Y --> X ^ Y, https://alive2.llvm.org/ce/z/cykffE
+define i8 @select_xor_cmp(i8 %0, i8 %1) {
+; CHECK-LABEL: @select_xor_cmp(
+; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[TMP1:%.*]], [[TMP0:%.*]]
+; CHECK-NEXT: ret i8 [[TMP3]]
+;
+ %3 = icmp eq i8 %1, %0
+ %4 = xor i8 %1, %0
+ %5 = select i1 %3, i8 0, i8 %4
+ ret i8 %5
+}
+
+define <2 x i8> @select_xor_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
+; CHECK-LABEL: @select_xor_cmp_vec(
+; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
+; CHECK-NEXT: ret <2 x i8> [[TMP3]]
+;
+ %3 = icmp eq <2 x i8> %1, %0
+ %4 = xor <2 x i8> %1, %0
+ %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
+ ret <2 x i8> %5
+}
+
+define i8 @select_xor_cmp_swap(i8 %0, i8 %1) {
+; CHECK-LABEL: @select_xor_cmp_swap(
+; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT: ret i8 [[TMP3]]
+;
+ %3 = icmp eq i8 %1, %0
+ %4 = xor i8 %0, %1
+ %5 = select i1 %3, i8 0, i8 %4
+ ret i8 %5
+}
+
+define <2 x i8> @select_xor_cmp_vec_swap(<2 x i8> %0, <2 x i8> %1) {
+; CHECK-LABEL: @select_xor_cmp_vec_swap(
+; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i8> [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT: ret <2 x i8> [[TMP3]]
+;
+ %3 = icmp eq <2 x i8> %1, %0
+ %4 = xor <2 x i8> %0, %1
+ %5 = select <2 x i1> %3, <2 x i8> <i8 0, i8 0>, <2 x i8> %4
+ ret <2 x i8> %5
+}
+
+; Negative test: the xor operands are not %0 and %1
+define i8 @select_xor_cmp_unmatched_operands(i8 %0, i8 %1, i8 %c) {
+; CHECK-LABEL: @select_xor_cmp_unmatched_operands(
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
+; CHECK-NEXT: [[TMP4:%.*]] = xor i8 [[TMP1]], [[C:%.*]]
+; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i8 0, i8 [[TMP4]]
+; CHECK-NEXT: ret i8 [[TMP5]]
+;
+ %3 = icmp eq i8 %1, %0
+ %4 = xor i8 %1, %c
+ %5 = select i1 %3, i8 0, i8 %4
+ ret i8 %5
+}
More information about the llvm-commits
mailing list