Skip to content

Commit 26a6fcd

Browse files
committedJan 6, 2018
[InstCombine] relax use constraint for min/max (~a, ~b) --> ~min/max(a, b)
In the minimal case, this won't remove instructions, but it still improves uses of existing values. In the motivating example from PR35834, it does remove instructions, and sets that case up to be optimized by something like D41603: https://reviews.llvm.org/D41603 llvm-svn: 321936
1 parent f7e7752 commit 26a6fcd

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1555,8 +1555,8 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
15551555
// MAX(~a, ~b) -> ~MIN(a, b)
15561556
// MIN(~a, ~b) -> ~MAX(a, b)
15571557
Value *A, *B;
1558-
if (match(LHS, m_Not(m_Value(A))) && LHS->getNumUses() <= 2 &&
1559-
match(RHS, m_Not(m_Value(B))) && RHS->getNumUses() <= 2) {
1558+
if (match(LHS, m_Not(m_Value(A))) && match(RHS, m_Not(m_Value(B))) &&
1559+
(LHS->getNumUses() <= 2 || RHS->getNumUses() <= 2)) {
15601560
CmpInst::Predicate InvertedPred =
15611561
getCmpPredicateForMinMax(getInverseMinMaxSelectPattern(SPF));
15621562
Value *InvertedCmp = Builder.CreateICmp(InvertedPred, A, B);

‎llvm/test/Transforms/InstCombine/max-of-nots.ll

+9-11
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ declare void @extra_use(i8)
4747
define i8 @umin_not_1_extra_use(i8 %x, i8 %y) {
4848
; CHECK-LABEL: @umin_not_1_extra_use(
4949
; CHECK-NEXT: [[NX:%.*]] = xor i8 %x, -1
50-
; CHECK-NEXT: [[NY:%.*]] = xor i8 %y, -1
51-
; CHECK-NEXT: [[CMPXY:%.*]] = icmp ult i8 [[NX]], [[NY]]
52-
; CHECK-NEXT: [[MINXY:%.*]] = select i1 [[CMPXY]], i8 [[NX]], i8 [[NY]]
50+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i8 %x, %y
51+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i8 %x, i8 %y
52+
; CHECK-NEXT: [[MINXY:%.*]] = xor i8 [[TMP2]], -1
5353
; CHECK-NEXT: call void @extra_use(i8 [[NX]])
5454
; CHECK-NEXT: ret i8 [[MINXY]]
5555
;
@@ -84,15 +84,13 @@ define i8 @umin_not_2_extra_use(i8 %x, i8 %y) {
8484

8585
define i8 @umin3_not(i8 %x, i8 %y, i8 %z) {
8686
; CHECK-LABEL: @umin3_not(
87-
; CHECK-NEXT: [[NX:%.*]] = xor i8 %x, -1
88-
; CHECK-NEXT: [[NY:%.*]] = xor i8 %y, -1
89-
; CHECK-NEXT: [[NZ:%.*]] = xor i8 %z, -1
9087
; CHECK-NEXT: [[CMPYX:%.*]] = icmp ult i8 %y, %x
91-
; CHECK-NEXT: [[CMPXZ:%.*]] = icmp ult i8 [[NX]], [[NZ]]
92-
; CHECK-NEXT: [[MINXZ:%.*]] = select i1 [[CMPXZ]], i8 [[NX]], i8 [[NZ]]
93-
; CHECK-NEXT: [[CMPYZ:%.*]] = icmp ult i8 [[NY]], [[NZ]]
94-
; CHECK-NEXT: [[MINYZ:%.*]] = select i1 [[CMPYZ]], i8 [[NY]], i8 [[NZ]]
95-
; CHECK-NEXT: [[R:%.*]] = select i1 [[CMPYX]], i8 [[MINXZ]], i8 [[MINYZ]]
88+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i8 %x, %z
89+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i8 %x, i8 %z
90+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ugt i8 %y, %z
91+
; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP3]], i8 %y, i8 %z
92+
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[CMPYX]], i8 [[TMP2]], i8 [[TMP4]]
93+
; CHECK-NEXT: [[R:%.*]] = xor i8 [[R:%.*]].v, -1
9694
; CHECK-NEXT: ret i8 [[R]]
9795
;
9896
%nx = xor i8 %x, -1

0 commit comments

Comments
 (0)
Please sign in to comment.