From c0730628a4956986bda54f99da21590cb6ad33ce Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Thu, 30 Apr 2015 04:56:00 +0000 Subject: [PATCH] [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 == ~Y (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) diff --git a/test/Transforms/InstCombine/select.ll b/test/Transforms/InstCombine/select.ll index ef122c92092..e4bc96cff17 100644 --- a/test/Transforms/InstCombine/select.ll +++ b/test/Transforms/InstCombine/select.ll @@ -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 +} -- 2.34.1