Skip to content

Commit 2986a9c

Browse files
committed
[InstCombine] canonicalize 'not' op after min/max intrinsic
This is another step towards parity between existing select transforms and min/max intrinsics (D98152).. The existing 'not' folds around select are complicated, so it's likely that we will need to enhance this, but this should be a safe step.
1 parent ef19f6c commit 2986a9c

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,11 +883,20 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
883883
}
884884
}
885885

886-
if (match(I0, m_Not(m_Value(X))) && match(I1, m_Not(m_Value(Y))) &&
887-
(I0->hasOneUse() || I1->hasOneUse())) {
888-
Value *InvMaxMin =
889-
Builder.CreateBinaryIntrinsic(getInverseMinMaxIntrinsic(IID), X, Y);
890-
return BinaryOperator::CreateNot(InvMaxMin);
886+
if (match(I0, m_Not(m_Value(X)))) {
887+
// max (not X), (not Y) --> not (min X, Y)
888+
Intrinsic::ID InvID = getInverseMinMaxIntrinsic(IID);
889+
if (match(I1, m_Not(m_Value(Y))) &&
890+
(I0->hasOneUse() || I1->hasOneUse())) {
891+
Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, Y);
892+
return BinaryOperator::CreateNot(InvMaxMin);
893+
}
894+
// max (not X), C --> not(min X, ~C)
895+
if (match(I1, m_Constant(C)) && I0->hasOneUse()) {
896+
Constant *NotC = ConstantExpr::getNot(C);
897+
Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotC);
898+
return BinaryOperator::CreateNot(InvMaxMin);
899+
}
891900
}
892901

893902
break;

llvm/test/Transforms/InstCombine/minmax-intrinsics.ll

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,21 +457,25 @@ define i8 @umin_of_nots_uses(i8 %x, i8 %y) {
457457
ret i8 %m
458458
}
459459

460+
; Canonicalize 'not' after min/max.
461+
460462
define i8 @smax_of_not_and_const(i8 %x) {
461463
; CHECK-LABEL: @smax_of_not_and_const(
462-
; CHECK-NEXT: [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
463-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[NOTX]], i8 42)
464+
; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 -43)
465+
; CHECK-NEXT: [[M:%.*]] = xor i8 [[TMP1]], -1
464466
; CHECK-NEXT: ret i8 [[M]]
465467
;
466468
%notx = xor i8 %x, -1
467469
%m = call i8 @llvm.smax.i8(i8 %notx, i8 42)
468470
ret i8 %m
469471
}
470472

473+
; Vectors are ok (including undef lanes of not ops and min/max constant operand)
474+
471475
define <3 x i8> @smin_of_not_and_const(<3 x i8> %x) {
472476
; CHECK-LABEL: @smin_of_not_and_const(
473-
; CHECK-NEXT: [[NOTX:%.*]] = xor <3 x i8> [[X:%.*]], <i8 -1, i8 -1, i8 undef>
474-
; CHECK-NEXT: [[M:%.*]] = call <3 x i8> @llvm.smin.v3i8(<3 x i8> [[NOTX]], <3 x i8> <i8 42, i8 undef, i8 43>)
477+
; CHECK-NEXT: [[TMP1:%.*]] = call <3 x i8> @llvm.smax.v3i8(<3 x i8> [[X:%.*]], <3 x i8> <i8 -43, i8 undef, i8 -44>)
478+
; CHECK-NEXT: [[M:%.*]] = xor <3 x i8> [[TMP1]], <i8 -1, i8 -1, i8 -1>
475479
; CHECK-NEXT: ret <3 x i8> [[M]]
476480
;
477481
%notx = xor <3 x i8> %x, <i8 -1, i8 -1, i8 undef>
@@ -481,8 +485,8 @@ define <3 x i8> @smin_of_not_and_const(<3 x i8> %x) {
481485

482486
define i8 @umax_of_not_and_const(i8 %x) {
483487
; CHECK-LABEL: @umax_of_not_and_const(
484-
; CHECK-NEXT: [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
485-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[NOTX]], i8 44)
488+
; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 -45)
489+
; CHECK-NEXT: [[M:%.*]] = xor i8 [[TMP1]], -1
486490
; CHECK-NEXT: ret i8 [[M]]
487491
;
488492
%notx = xor i8 %x, -1
@@ -492,15 +496,17 @@ define i8 @umax_of_not_and_const(i8 %x) {
492496

493497
define i8 @umin_of_not_and_const(i8 %x) {
494498
; CHECK-LABEL: @umin_of_not_and_const(
495-
; CHECK-NEXT: [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
496-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[NOTX]], i8 -45)
499+
; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 44)
500+
; CHECK-NEXT: [[M:%.*]] = xor i8 [[TMP1]], -1
497501
; CHECK-NEXT: ret i8 [[M]]
498502
;
499503
%notx = xor i8 %x, -1
500504
%m = call i8 @llvm.umin.i8(i8 -45, i8 %notx)
501505
ret i8 %m
502506
}
503507

508+
; Negative test - too many uses
509+
504510
define i8 @umin_of_not_and_const_uses(i8 %x) {
505511
; CHECK-LABEL: @umin_of_not_and_const_uses(
506512
; CHECK-NEXT: [[NOTX:%.*]] = xor i8 [[X:%.*]], -1

0 commit comments

Comments
 (0)