From 2e920bd04f4cea5efc2b1944d5c4d9ec9278fda7 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Tue, 18 Aug 2015 20:48:29 +0000 Subject: [PATCH] DAGCombiner: Optimize SELECTs first before turning them into SELECT_CC This is part of http://reviews.llvm.org/D11616 - I just decided to split this up into a separate commit. llvm-svn: 245349 --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 64 +++++++++---------- .../CodeGen/AArch64/dag-combine-select.ll | 17 +++++ 2 files changed, 49 insertions(+), 32 deletions(-) create mode 100644 llvm/test/CodeGen/AArch64/dag-combine-select.ll diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 585cff0de0b5..faa9d351e469 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -4955,38 +4955,6 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) { if (SimplifySelectOps(N, N1, N2)) return SDValue(N, 0); // Don't revisit N. - // fold selects based on a setcc into other things, such as min/max/abs - if (N0.getOpcode() == ISD::SETCC) { - // select x, y (fcmp lt x, y) -> fminnum x, y - // select x, y (fcmp gt x, y) -> fmaxnum x, y - // - // This is OK if we don't care about what happens if either operand is a - // NaN. - // - - // FIXME: Instead of testing for UnsafeFPMath, this should be checking for - // no signed zeros as well as no nans. - const TargetOptions &Options = DAG.getTarget().Options; - if (Options.UnsafeFPMath && - VT.isFloatingPoint() && N0.hasOneUse() && - DAG.isKnownNeverNaN(N1) && DAG.isKnownNeverNaN(N2)) { - ISD::CondCode CC = cast(N0.getOperand(2))->get(); - - if (SDValue FMinMax = combineMinNumMaxNum(SDLoc(N), VT, N0.getOperand(0), - N0.getOperand(1), N1, N2, CC, - TLI, DAG)) - return FMinMax; - } - - if ((!LegalOperations && - TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) || - TLI.isOperationLegal(ISD::SELECT_CC, VT)) - return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, - N0.getOperand(0), N0.getOperand(1), - N1, N2, N0.getOperand(2)); - return SimplifySelect(SDLoc(N), N0, N1, N2); - } - if (VT0 == MVT::i1) { if (TLI.shouldNormalizeToSelectSequence(*DAG.getContext(), VT)) { // select (and Cond0, Cond1), X, Y @@ -5050,6 +5018,38 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) { } } + // fold selects based on a setcc into other things, such as min/max/abs + if (N0.getOpcode() == ISD::SETCC) { + // select x, y (fcmp lt x, y) -> fminnum x, y + // select x, y (fcmp gt x, y) -> fmaxnum x, y + // + // This is OK if we don't care about what happens if either operand is a + // NaN. + // + + // FIXME: Instead of testing for UnsafeFPMath, this should be checking for + // no signed zeros as well as no nans. + const TargetOptions &Options = DAG.getTarget().Options; + if (Options.UnsafeFPMath && + VT.isFloatingPoint() && N0.hasOneUse() && + DAG.isKnownNeverNaN(N1) && DAG.isKnownNeverNaN(N2)) { + ISD::CondCode CC = cast(N0.getOperand(2))->get(); + + if (SDValue FMinMax = combineMinNumMaxNum(SDLoc(N), VT, N0.getOperand(0), + N0.getOperand(1), N1, N2, CC, + TLI, DAG)) + return FMinMax; + } + + if ((!LegalOperations && + TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) || + TLI.isOperationLegal(ISD::SELECT_CC, VT)) + return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, + N0.getOperand(0), N0.getOperand(1), + N1, N2, N0.getOperand(2)); + return SimplifySelect(SDLoc(N), N0, N1, N2); + } + return SDValue(); } diff --git a/llvm/test/CodeGen/AArch64/dag-combine-select.ll b/llvm/test/CodeGen/AArch64/dag-combine-select.ll new file mode 100644 index 000000000000..01934e91a71f --- /dev/null +++ b/llvm/test/CodeGen/AArch64/dag-combine-select.ll @@ -0,0 +1,17 @@ +; RUN: llc -o - %s | FileCheck %s +target triple = "arm64--" + +; Ensure that we transform select(C0, x, select(C1, x, y)) towards +; select(C0 | C1, x, y) so we can use CMP;CCMP for the implementation. +; CHECK-LABEL: test0: +; CHECK: cmp w0, #7 +; CHECK: ccmp w1, #0, #0, ne +; CHECK: csel w0, w1, w2, gt +; CHECK: ret +define i32 @test0(i32 %v0, i32 %v1, i32 %v2) { + %cmp1 = icmp eq i32 %v0, 7 + %cmp2 = icmp sgt i32 %v1, 0 + %sel0 = select i1 %cmp1, i32 %v1, i32 %v2 + %sel1 = select i1 %cmp2, i32 %v1, i32 %sel0 + ret i32 %sel1 +}