From 306cd28e214a7913e5f0fec865e0857609e164b3 Mon Sep 17 00:00:00 2001 From: Dinesh Dwivedi Date: Mon, 19 May 2014 07:08:32 +0000 Subject: [PATCH] Added inst-combine for 'MIN(MIN(A, 97), 23)' and 'MAX(MAX(A, 23), 97)' This removes TODO added in r208849 [http://reviews.llvm.org/D3629] MIN(MIN(A, 97), 23) -> MIN(A, 23) MAX(MAX(A, 23), 97) -> MAX(A, 97) Differential Revision: http://reviews.llvm.org/D3785 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209110 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineSelect.cpp | 18 +++++-- test/Transforms/InstCombine/select.ll | 52 +++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineSelect.cpp b/lib/Transforms/InstCombine/InstCombineSelect.cpp index 98260b05992..9a41e4b9406 100644 --- a/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -651,24 +651,32 @@ Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner, return ReplaceInstUsesWith(Outer, C); } - // MIN(MIN(A, 23), 97) -> MIN(A, 23) - // MAX(MAX(A, 97), 23) -> MAX(A, 97) if (SPF1 == SPF2) { if (ConstantInt *CB = dyn_cast(B)) { if (ConstantInt *CC = dyn_cast(C)) { APInt ACB = CB->getValue(); APInt ACC = CC->getValue(); + + // MIN(MIN(A, 23), 97) -> MIN(A, 23) + // MAX(MAX(A, 97), 23) -> MAX(A, 97) if ((SPF1 == SPF_UMIN && ACB.ule(ACC)) || (SPF1 == SPF_SMIN && ACB.sle(ACC)) || (SPF1 == SPF_UMAX && ACB.uge(ACC)) || (SPF1 == SPF_SMAX && ACB.sge(ACC))) return ReplaceInstUsesWith(Outer, Inner); + + // MIN(MIN(A, 97), 23) -> MIN(A, 23) + // MAX(MAX(A, 23), 97) -> MAX(A, 97) + if ((SPF1 == SPF_UMIN && ACB.ugt(ACC)) || + (SPF1 == SPF_SMIN && ACB.sgt(ACC)) || + (SPF1 == SPF_UMAX && ACB.ult(ACC)) || + (SPF1 == SPF_SMAX && ACB.slt(ACC))) { + Outer.replaceUsesOfWith(Inner, A); + return &Outer; + } } } } - - // TODO: MIN(MIN(A, 97), 23) -> MIN(A, 23) - // TODO: MAX(MAX(A, 23), 97) -> MAX(A, 97) return nullptr; } diff --git a/test/Transforms/InstCombine/select.ll b/test/Transforms/InstCombine/select.ll index bbb2792eef9..2213be1f511 100644 --- a/test/Transforms/InstCombine/select.ll +++ b/test/Transforms/InstCombine/select.ll @@ -1078,4 +1078,56 @@ entry: ret i32 %retval ; CHECK-LABEL: @test71( ; CHECK: ret i32 %cond +} + +; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11) +define i32 @test72(i32 %x) { + %cmp = icmp sgt i32 %x, 92 + %cond = select i1 %cmp, i32 92, i32 %x + %cmp3 = icmp sgt i32 %cond, 11 + %retval = select i1 %cmp3, i32 11, i32 %cond + ret i32 %retval +; CHECK-LABEL: @test72( +; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp sgt i32 %x, 11 +; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 11, i32 %x +; CHECK-NEXT: ret i32 [[SEL]] +} + +; MIN(MIN(X, 83), 24) -> MIN(X, 24) +define i32 @test73(i32 %x) { + %cmp = icmp ugt i32 %x, 83 + %cond = select i1 %cmp, i32 83, i32 %x + %cmp3 = icmp ugt i32 %cond, 24 + %retval = select i1 %cmp3, i32 24, i32 %cond + ret i32 %retval +; CHECK-LABEL: @test73( +; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %x, 24 +; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 24, i32 %x +; CHECK-NEXT: ret i32 [[SEL]] +} + +; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75) +define i32 @test74(i32 %x) { + %cmp = icmp slt i32 %x, 36 + %cond = select i1 %cmp, i32 36, i32 %x + %cmp3 = icmp slt i32 %cond, 75 + %retval = select i1 %cmp3, i32 75, i32 %cond + ret i32 %retval +; CHECK-LABEL: @test74( +; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp slt i32 %x, 75 +; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 75, i32 %x +; CHECK-NEXT: ret i32 [[SEL]] +} + +; MAX(MAX(X, 47), 68) -> MAX(X, 68) +define i32 @test75(i32 %x) { + %cmp = icmp ult i32 %x, 47 + %cond = select i1 %cmp, i32 47, i32 %x + %cmp3 = icmp ult i32 %cond, 68 + %retval = select i1 %cmp3, i32 68, i32 %cond + ret i32 %retval +; CHECK-LABEL: @test75( +; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %x, 68 +; CHECK-NEXT: [[SEL:%[a-z0-9]+]] = select i1 [[CMP]], i32 68, i32 %x +; CHECK-NEXT: ret i32 [[SEL]] } \ No newline at end of file -- 2.34.1