[InstCombine] Add a new formula for SMIN.

Summary:
After this change `MatchSelectPattern` recognizes the following form
of SMIN:

  Y >s C ? ~Y : ~C == ~Y <s ~C ? ~Y : ~C = SMIN(~Y, ~C)

Reviewers: majnemer

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D9352

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236202 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjoy Das 2015-04-30 04:56:00 +00:00
parent deedba2a36
commit c0730628a4
2 changed files with 23 additions and 0 deletions

View File

@ -86,6 +86,17 @@ MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS) {
return (CmpLHS == FalseVal) ? SPF_ABS : SPF_NABS;
}
}
// Y >s C ? ~Y : ~C == ~Y <s ~C ? ~Y : ~C = SMIN(~Y, ~C)
if (const auto *C2 = dyn_cast<ConstantInt>(FalseVal)) {
if (C1->getType() == C2->getType() && ~C1->getValue() == C2->getValue() &&
(match(TrueVal, m_Not(m_Specific(CmpLHS))) ||
match(CmpLHS, m_Not(m_Specific(TrueVal))))) {
LHS = TrueVal;
RHS = FalseVal;
return SPF_SMIN;
}
}
}
// TODO: (X > 4) ? X : 5 --> (X >= 5) ? X : 5 --> MAX(X, 5)

View File

@ -1520,3 +1520,15 @@ define i32 @test_select_select1(i32 %a, i32 %r0, i32 %r1, i32 %v1, i32 %v2) {
%s1 = select i1 %c1, i32 %r0, i32 %s0
ret i32 %s1
}
define i32 @test_max_of_min(i32 %a) {
; MAX(MIN(%a, -1), -1) == -1
; CHECK-LABEL: @test_max_of_min(
; CHECK: ret i32 -1
%not_a = xor i32 %a, -1
%c0 = icmp sgt i32 %a, 0
%s0 = select i1 %c0, i32 %not_a, i32 -1
%c1 = icmp sgt i32 %s0, -1
%s1 = select i1 %c1, i32 %s0, i32 -1
ret i32 %s1
}