From 82209fd96e456d22c10905fd3d5f6946be3e1b94 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Amaury=20S=C3=A9chet?= Date: Sat, 5 Nov 2022 16:44:42 +0000 Subject: [PATCH] [NFC] Refactor DAGCombiner::foldSelectOfConstants to reduce nesting 2.0 --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 71 ++++++++++++++------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index ef32cfa..0112a40 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -10436,49 +10436,50 @@ SDValue DAGCombiner::foldSelectOfConstants(SDNode *N) { // Use a target hook because some targets may prefer to transform in the // other direction. - if (shouldConvertSelectOfConstantsToMath(Cond, VT, TLI)) { - // For any constants that differ by 1, we can transform the select into - // an extend and add. - const APInt &C1Val = C1->getAPIntValue(); - const APInt &C2Val = C2->getAPIntValue(); + if (!shouldConvertSelectOfConstantsToMath(Cond, VT, TLI)) + return SDValue(); - // select Cond, C1, C1-1 --> add (zext Cond), C1-1 - if (C1Val - 1 == C2Val) { - Cond = DAG.getZExtOrTrunc(Cond, DL, VT); - return DAG.getNode(ISD::ADD, DL, VT, Cond, N2); - } + // For any constants that differ by 1, we can transform the select into + // an extend and add. + const APInt &C1Val = C1->getAPIntValue(); + const APInt &C2Val = C2->getAPIntValue(); - // select Cond, C1, C1+1 --> add (sext Cond), C1+1 - if (C1Val + 1 == C2Val) { - Cond = DAG.getSExtOrTrunc(Cond, DL, VT); - return DAG.getNode(ISD::ADD, DL, VT, Cond, N2); - } + // select Cond, C1, C1-1 --> add (zext Cond), C1-1 + if (C1Val - 1 == C2Val) { + Cond = DAG.getZExtOrTrunc(Cond, DL, VT); + return DAG.getNode(ISD::ADD, DL, VT, Cond, N2); + } - // select Cond, Pow2, 0 --> (zext Cond) << log2(Pow2) - if (C1Val.isPowerOf2() && C2Val.isZero()) { - Cond = DAG.getZExtOrTrunc(Cond, DL, VT); - SDValue ShAmtC = - DAG.getShiftAmountConstant(C1Val.exactLogBase2(), VT, DL); - return DAG.getNode(ISD::SHL, DL, VT, Cond, ShAmtC); - } + // select Cond, C1, C1+1 --> add (sext Cond), C1+1 + if (C1Val + 1 == C2Val) { + Cond = DAG.getSExtOrTrunc(Cond, DL, VT); + return DAG.getNode(ISD::ADD, DL, VT, Cond, N2); + } - // select Cond, -1, C --> or (sext Cond), C - if (C1->isAllOnes()) { - Cond = DAG.getSExtOrTrunc(Cond, DL, VT); - return DAG.getNode(ISD::OR, DL, VT, Cond, N2); - } + // select Cond, Pow2, 0 --> (zext Cond) << log2(Pow2) + if (C1Val.isPowerOf2() && C2Val.isZero()) { + Cond = DAG.getZExtOrTrunc(Cond, DL, VT); + SDValue ShAmtC = + DAG.getShiftAmountConstant(C1Val.exactLogBase2(), VT, DL); + return DAG.getNode(ISD::SHL, DL, VT, Cond, ShAmtC); + } - // select Cond, C, -1 --> or (sext (not Cond)), C - if (C2->isAllOnes()) { - SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1); - NotCond = DAG.getSExtOrTrunc(NotCond, DL, VT); - return DAG.getNode(ISD::OR, DL, VT, NotCond, N1); - } + // select Cond, -1, C --> or (sext Cond), C + if (C1->isAllOnes()) { + Cond = DAG.getSExtOrTrunc(Cond, DL, VT); + return DAG.getNode(ISD::OR, DL, VT, Cond, N2); + } - if (SDValue V = foldSelectOfConstantsUsingSra(N, DAG)) - return V; + // select Cond, C, -1 --> or (sext (not Cond)), C + if (C2->isAllOnes()) { + SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1); + NotCond = DAG.getSExtOrTrunc(NotCond, DL, VT); + return DAG.getNode(ISD::OR, DL, VT, NotCond, N1); } + if (SDValue V = foldSelectOfConstantsUsingSra(N, DAG)) + return V; + return SDValue(); } -- 2.7.4