[PATCH] D138700: [InstSimplify] Fold (X || Y) ? false && X --> false
chenglin.bi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 28 11:55:31 PST 2022
bcl5980 updated this revision to Diff 478310.
bcl5980 edited the summary of this revision.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D138700/new/
https://reviews.llvm.org/D138700
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/select-logical.ll
Index: llvm/test/Transforms/InstSimplify/select-logical.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select-logical.ll
+++ llvm/test/Transforms/InstSimplify/select-logical.ll
@@ -202,9 +202,7 @@
define i1 @or_select_false_x_case1(i1 %x, i1 %y) {
; CHECK-LABEL: @or_select_false_x_case1(
-; CHECK-NEXT: [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 false, i1 [[X]]
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%or = or i1 %x, %y
%r = select i1 %or, i1 false, i1 %x
@@ -215,9 +213,7 @@
define i1 @or_select_false_x_case2(i1 %x, i1 %y) {
; CHECK-LABEL: @or_select_false_x_case2(
-; CHECK-NEXT: [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 false, i1 [[Y]]
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%or = or i1 %x, %y
%r = select i1 %or, i1 false, i1 %y
@@ -228,9 +224,7 @@
define <2 x i1> @or_select_false_x_vector(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @or_select_false_x_vector(
-; CHECK-NEXT: [[OR:%.*]] = or <2 x i1> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> zeroinitializer, <2 x i1> [[X]]
-; CHECK-NEXT: ret <2 x i1> [[R]]
+; CHECK-NEXT: ret <2 x i1> zeroinitializer
;
%or = or <2 x i1> %x, %y
%r = select <2 x i1> %or, <2 x i1> <i1 false, i1 false>, <2 x i1> %x
@@ -254,9 +248,7 @@
define i1 @logical_or_select_false_x_case1(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_or_select_false_x_case1(
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 false, i1 [[X]]
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%or = select i1 %x, i1 true, i1 %y
%r = select i1 %or, i1 false, i1 %x
@@ -267,9 +259,7 @@
define i1 @logical_or_select_false_x_case2(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_or_select_false_x_case2(
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[Y:%.*]], i1 true, i1 [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 false, i1 [[X]]
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%or = select i1 %y, i1 true, i1 %x
%r = select i1 %or, i1 false, i1 %x
@@ -280,9 +270,7 @@
define <2 x i1> @logical_or_select_false_x_vector(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @logical_or_select_false_x_vector(
-; CHECK-NEXT: [[OR:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> zeroinitializer, <2 x i1> [[X]]
-; CHECK-NEXT: ret <2 x i1> [[R]]
+; CHECK-NEXT: ret <2 x i1> zeroinitializer
;
%or = select <2 x i1> %y, <2 x i1> <i1 true, i1 true>, <2 x i1> %x
%r = select <2 x i1> %or, <2 x i1> <i1 false, i1 false>, <2 x i1> %x
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4555,9 +4555,16 @@
if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
return Cond;
- // (X || Y) && (X || !Y) --> X (commuted 8 ways)
Value *X, *Y;
+ if (match(Cond, m_LogicalOr(m_Value(X), m_Value(Y)))) {
+ auto *C = dyn_cast<Constant>(TrueVal);
+ // (X || Y) ? false : X --> false (commuted 2 ways)
+ if (C && C->isNullValue() && (X == FalseVal || Y == FalseVal))
+ return TrueVal;
+ }
+
if (match(FalseVal, m_ZeroInt())) {
+ // (X || Y) && (X || !Y) --> X (commuted 8 ways)
if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
return X;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138700.478310.patch
Type: text/x-patch
Size: 3790 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221128/038cf600/attachment.bin>
More information about the llvm-commits
mailing list