Skip to content

Commit f297332

Browse files
committed
[InstSimplify] Fold (X || Y) ? X : Y --> X
(X || Y) ? X : Y --> X https://alive2.llvm.org/ce/z/oRQJee Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D138815
1 parent 9e0f9f1 commit f297332

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,6 +4555,10 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
45554555
if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
45564556
return Cond;
45574557

4558+
// (X || Y) ? X : Y --> X (commuted 2 ways)
4559+
if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
4560+
return TrueVal;
4561+
45584562
// (X || Y) ? false : X --> false (commuted 2 ways)
45594563
if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
45604564
match(TrueVal, m_ZeroInt()))

llvm/test/Transforms/InstSimplify/select-logical.ll

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,7 @@ define i1 @or_select_false_x_negative(i1 %x, i1 %y, i1 %z) {
382382

383383
define i1 @select_or_same_op(i1 %x, i1 %y) {
384384
; CHECK-LABEL: @select_or_same_op(
385-
; CHECK-NEXT: [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
386-
; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]]
387-
; CHECK-NEXT: ret i1 [[R]]
385+
; CHECK-NEXT: ret i1 [[X:%.*]]
388386
;
389387
%or = or i1 %x, %y
390388
%r = select i1 %or, i1 %x, i1 %y
@@ -394,9 +392,7 @@ define i1 @select_or_same_op(i1 %x, i1 %y) {
394392

395393
define i1 @select_or_same_op_commute(i1 %x, i1 %y) {
396394
; CHECK-LABEL: @select_or_same_op_commute(
397-
; CHECK-NEXT: [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
398-
; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[Y]], i1 [[X]]
399-
; CHECK-NEXT: ret i1 [[R]]
395+
; CHECK-NEXT: ret i1 [[Y:%.*]]
400396
;
401397
%or = or i1 %x, %y
402398
%r = select i1 %or, i1 %y, i1 %x
@@ -406,9 +402,7 @@ define i1 @select_or_same_op_commute(i1 %x, i1 %y) {
406402

407403
define <2 x i1> @select_or_same_op_vector1(<2 x i1> %x, <2 x i1> %y) {
408404
; CHECK-LABEL: @select_or_same_op_vector1(
409-
; CHECK-NEXT: [[OR:%.*]] = or <2 x i1> [[X:%.*]], [[Y:%.*]]
410-
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> [[X]], <2 x i1> [[Y]]
411-
; CHECK-NEXT: ret <2 x i1> [[R]]
405+
; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
412406
;
413407
%or = or <2 x i1> %x, %y
414408
%r = select <2 x i1> %or, <2 x i1> %x, <2 x i1> %y
@@ -418,9 +412,7 @@ define <2 x i1> @select_or_same_op_vector1(<2 x i1> %x, <2 x i1> %y) {
418412

419413
define i1 @select_logic_or1_same_op(i1 %x, i1 %y) {
420414
; CHECK-LABEL: @select_logic_or1_same_op(
421-
; CHECK-NEXT: [[OR:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]]
422-
; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]]
423-
; CHECK-NEXT: ret i1 [[R]]
415+
; CHECK-NEXT: ret i1 [[X:%.*]]
424416
;
425417
%or = select i1 %x, i1 true, i1 %y
426418
%r = select i1 %or, i1 %x, i1 %y
@@ -430,9 +422,7 @@ define i1 @select_logic_or1_same_op(i1 %x, i1 %y) {
430422

431423
define i1 @select_logic_or2_same_op(i1 %x, i1 %y) {
432424
; CHECK-LABEL: @select_logic_or2_same_op(
433-
; CHECK-NEXT: [[OR:%.*]] = select i1 [[Y:%.*]], i1 true, i1 [[X:%.*]]
434-
; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]]
435-
; CHECK-NEXT: ret i1 [[R]]
425+
; CHECK-NEXT: ret i1 [[X:%.*]]
436426
;
437427
%or = select i1 %y, i1 true, i1 %x
438428
%r = select i1 %or, i1 %x, i1 %y
@@ -442,15 +432,15 @@ define i1 @select_logic_or2_same_op(i1 %x, i1 %y) {
442432

443433
define <2 x i1> @select_or_same_op_vector2(<2 x i1> %x, <2 x i1> %y) {
444434
; CHECK-LABEL: @select_or_same_op_vector2(
445-
; CHECK-NEXT: [[OR:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[Y:%.*]]
446-
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> [[X]], <2 x i1> [[Y]]
447-
; CHECK-NEXT: ret <2 x i1> [[R]]
435+
; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
448436
;
449437
%or = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %y
450438
%r = select <2 x i1> %or, <2 x i1> %x, <2 x i1> %y
451439
ret <2 x i1> %r
452440
}
453441

442+
; TODO: this could transform to X
443+
; (X || Y) ? X : Y --> X
454444

455445
define <2 x i1> @select_or_same_op_vector2_poison(<2 x i1> %x, <2 x i1> %y) {
456446
; CHECK-LABEL: @select_or_same_op_vector2_poison(

0 commit comments

Comments
 (0)