Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1 | //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===// |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Nate Begeman and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run |
| 11 | // both before and after the DAG is legalized. |
| 12 | // |
| 13 | // FIXME: Missing folds |
| 14 | // sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into |
| 15 | // a sequence of multiplies, shifts, and adds. This should be controlled by |
| 16 | // some kind of hint from the target that int div is expensive. |
| 17 | // various folds of mulh[s,u] by constants such as -1, powers of 2, etc. |
| 18 | // |
| 19 | // FIXME: Should add a corresponding version of fold AND with |
| 20 | // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which |
| 21 | // we don't have yet. |
| 22 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 23 | // FIXME: select C, pow2, pow2 -> something smart |
| 24 | // FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z) |
| 25 | // FIXME: (select C, load A, load B) -> load (select C, A, B) |
| 26 | // FIXME: store -> load -> forward substitute |
| 27 | // FIXME: Dead stores -> nuke |
| 28 | // FIXME: shr X, (and Y,31) -> shr X, Y |
| 29 | // FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 30 | // FIXME: mul (x, const) -> shifts + adds |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 31 | // FIXME: undef values |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 32 | // FIXME: make truncate see through SIGN_EXTEND and AND |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 33 | // FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 34 | // FIXME: verify that getNode can't return extends with an operand whose type |
| 35 | // is >= to that of the extend. |
| 36 | // FIXME: divide by zero is currently left unfolded. do we want to turn this |
| 37 | // into an undef? |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 38 | // FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 39 | // |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | #define DEBUG_TYPE "dagcombine" |
| 43 | #include "llvm/ADT/Statistic.h" |
| 44 | #include "llvm/CodeGen/SelectionDAG.h" |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 46 | #include "llvm/Support/MathExtras.h" |
| 47 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 49 | #include <cmath> |
| 50 | using namespace llvm; |
| 51 | |
| 52 | namespace { |
| 53 | Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined"); |
| 54 | |
| 55 | class DAGCombiner { |
| 56 | SelectionDAG &DAG; |
| 57 | TargetLowering &TLI; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 58 | bool AfterLegalize; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 59 | |
| 60 | // Worklist of all of the nodes that need to be simplified. |
| 61 | std::vector<SDNode*> WorkList; |
| 62 | |
| 63 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 64 | /// the instruction to the work lists because they might get more simplified |
| 65 | /// now. |
| 66 | /// |
| 67 | void AddUsersToWorkList(SDNode *N) { |
| 68 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 69 | UI != UE; ++UI) |
| 70 | WorkList.push_back(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 74 | void removeFromWorkList(SDNode *N) { |
| 75 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N), |
| 76 | WorkList.end()); |
| 77 | } |
| 78 | |
| 79 | /// visit - call the node-specific routine that knows how to fold each |
| 80 | /// particular type of node. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 81 | SDOperand visit(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 82 | |
| 83 | // Visitation implementation - Implement dag node combining for different |
| 84 | // node types. The semantics are as follows: |
| 85 | // Return Value: |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 86 | // SDOperand.Val == 0 - No change was made |
| 87 | // otherwise - N should be replaced by the returned Operand. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 88 | // |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 89 | SDOperand visitTokenFactor(SDNode *N); |
| 90 | SDOperand visitADD(SDNode *N); |
| 91 | SDOperand visitSUB(SDNode *N); |
| 92 | SDOperand visitMUL(SDNode *N); |
| 93 | SDOperand visitSDIV(SDNode *N); |
| 94 | SDOperand visitUDIV(SDNode *N); |
| 95 | SDOperand visitSREM(SDNode *N); |
| 96 | SDOperand visitUREM(SDNode *N); |
| 97 | SDOperand visitMULHU(SDNode *N); |
| 98 | SDOperand visitMULHS(SDNode *N); |
| 99 | SDOperand visitAND(SDNode *N); |
| 100 | SDOperand visitOR(SDNode *N); |
| 101 | SDOperand visitXOR(SDNode *N); |
| 102 | SDOperand visitSHL(SDNode *N); |
| 103 | SDOperand visitSRA(SDNode *N); |
| 104 | SDOperand visitSRL(SDNode *N); |
| 105 | SDOperand visitCTLZ(SDNode *N); |
| 106 | SDOperand visitCTTZ(SDNode *N); |
| 107 | SDOperand visitCTPOP(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 108 | SDOperand visitSELECT(SDNode *N); |
| 109 | SDOperand visitSELECT_CC(SDNode *N); |
| 110 | SDOperand visitSETCC(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 111 | SDOperand visitSIGN_EXTEND(SDNode *N); |
| 112 | SDOperand visitZERO_EXTEND(SDNode *N); |
| 113 | SDOperand visitSIGN_EXTEND_INREG(SDNode *N); |
| 114 | SDOperand visitTRUNCATE(SDNode *N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 115 | |
| 116 | SDOperand visitFADD(SDNode *N); |
| 117 | SDOperand visitFSUB(SDNode *N); |
| 118 | SDOperand visitFMUL(SDNode *N); |
| 119 | SDOperand visitFDIV(SDNode *N); |
| 120 | SDOperand visitFREM(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 121 | SDOperand visitSINT_TO_FP(SDNode *N); |
| 122 | SDOperand visitUINT_TO_FP(SDNode *N); |
| 123 | SDOperand visitFP_TO_SINT(SDNode *N); |
| 124 | SDOperand visitFP_TO_UINT(SDNode *N); |
| 125 | SDOperand visitFP_ROUND(SDNode *N); |
| 126 | SDOperand visitFP_ROUND_INREG(SDNode *N); |
| 127 | SDOperand visitFP_EXTEND(SDNode *N); |
| 128 | SDOperand visitFNEG(SDNode *N); |
| 129 | SDOperand visitFABS(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 130 | SDOperand visitBRCOND(SDNode *N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 131 | SDOperand visitBRCONDTWOWAY(SDNode *N); |
| 132 | SDOperand visitBR_CC(SDNode *N); |
| 133 | SDOperand visitBRTWOWAY_CC(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 134 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 135 | SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2); |
| 136 | SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2, |
| 137 | SDOperand N3, ISD::CondCode CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 138 | SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 139 | ISD::CondCode Cond, bool foldBooleans = true); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 140 | public: |
| 141 | DAGCombiner(SelectionDAG &D) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 142 | : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {} |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 143 | |
| 144 | /// Run - runs the dag combiner on all nodes in the work list |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 145 | void Run(bool RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 146 | }; |
| 147 | } |
| 148 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 149 | /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use |
| 150 | /// this predicate to simplify operations downstream. Op and Mask are known to |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 151 | /// be the same type. |
| 152 | static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask, |
| 153 | const TargetLowering &TLI) { |
| 154 | unsigned SrcBits; |
| 155 | if (Mask == 0) return true; |
| 156 | |
| 157 | // If we know the result of a setcc has the top bits zero, use this info. |
| 158 | switch (Op.getOpcode()) { |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 159 | case ISD::Constant: |
| 160 | return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0; |
| 161 | case ISD::SETCC: |
| 162 | return ((Mask & 1) == 0) && |
| 163 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult; |
| 164 | case ISD::ZEXTLOAD: |
| 165 | SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT()); |
| 166 | return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. |
| 167 | case ISD::ZERO_EXTEND: |
| 168 | SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType()); |
| 169 | return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI); |
| 170 | case ISD::AssertZext: |
| 171 | SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); |
| 172 | return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. |
| 173 | case ISD::AND: |
Chris Lattner | ee899e6 | 2005-10-09 22:12:36 +0000 | [diff] [blame] | 174 | // If either of the operands has zero bits, the result will too. |
| 175 | if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) || |
| 176 | MaskedValueIsZero(Op.getOperand(0), Mask, TLI)) |
| 177 | return true; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 178 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
| 179 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) |
| 180 | return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI); |
Chris Lattner | ee899e6 | 2005-10-09 22:12:36 +0000 | [diff] [blame] | 181 | return false; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 182 | case ISD::OR: |
| 183 | case ISD::XOR: |
| 184 | return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) && |
| 185 | MaskedValueIsZero(Op.getOperand(1), Mask, TLI); |
| 186 | case ISD::SELECT: |
| 187 | return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) && |
| 188 | MaskedValueIsZero(Op.getOperand(2), Mask, TLI); |
| 189 | case ISD::SELECT_CC: |
| 190 | return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) && |
| 191 | MaskedValueIsZero(Op.getOperand(3), Mask, TLI); |
| 192 | case ISD::SRL: |
| 193 | // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0 |
| 194 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 195 | uint64_t NewVal = Mask << ShAmt->getValue(); |
| 196 | SrcBits = MVT::getSizeInBits(Op.getValueType()); |
| 197 | if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1; |
| 198 | return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); |
| 199 | } |
| 200 | return false; |
| 201 | case ISD::SHL: |
| 202 | // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0 |
| 203 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 204 | uint64_t NewVal = Mask >> ShAmt->getValue(); |
| 205 | return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); |
| 206 | } |
| 207 | return false; |
Chris Lattner | bba9aa3 | 2005-10-10 16:51:40 +0000 | [diff] [blame] | 208 | case ISD::ADD: |
Chris Lattner | d739075 | 2005-10-10 16:52:03 +0000 | [diff] [blame] | 209 | // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits. |
Chris Lattner | bba9aa3 | 2005-10-10 16:51:40 +0000 | [diff] [blame] | 210 | if ((Mask&(Mask+1)) == 0) { // All low bits |
| 211 | if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) && |
| 212 | MaskedValueIsZero(Op.getOperand(1), Mask, TLI)) |
| 213 | return true; |
| 214 | } |
| 215 | break; |
Chris Lattner | c4ced26 | 2005-10-07 15:30:32 +0000 | [diff] [blame] | 216 | case ISD::SUB: |
| 217 | if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) { |
| 218 | // We know that the top bits of C-X are clear if X contains less bits |
| 219 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 220 | // positive if we can prove that X is >= 0 and < 16. |
| 221 | unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0)); |
| 222 | if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear |
| 223 | unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1); |
| 224 | uint64_t MaskV = (1ULL << (63-NLZ))-1; |
| 225 | if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) { |
| 226 | // High bits are clear this value is known to be >= C. |
| 227 | unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue()); |
| 228 | if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0) |
| 229 | return true; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | break; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 234 | case ISD::CTTZ: |
| 235 | case ISD::CTLZ: |
| 236 | case ISD::CTPOP: |
| 237 | // Bit counting instructions can not set the high bits of the result |
| 238 | // register. The max number of bits sets depends on the input. |
| 239 | return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0; |
| 240 | |
| 241 | // TODO we could handle some SRA cases here. |
| 242 | default: break; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 243 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 247 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 248 | // that selects between the values 1 and 0, making it equivalent to a setcc. |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 249 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
| 250 | // nodes based on the type of node we are checking. This simplifies life a |
| 251 | // bit for the callers. |
| 252 | static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS, |
| 253 | SDOperand &CC) { |
| 254 | if (N.getOpcode() == ISD::SETCC) { |
| 255 | LHS = N.getOperand(0); |
| 256 | RHS = N.getOperand(1); |
| 257 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 258 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 259 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 260 | if (N.getOpcode() == ISD::SELECT_CC && |
| 261 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 262 | N.getOperand(3).getOpcode() == ISD::Constant && |
| 263 | cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 264 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 265 | LHS = N.getOperand(0); |
| 266 | RHS = N.getOperand(1); |
| 267 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 268 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 269 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 273 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 274 | // one use. If this is true, it allows the users to invert the operation for |
| 275 | // free when it is profitable to do so. |
| 276 | static bool isOneUseSetCC(SDOperand N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 277 | SDOperand N0, N1, N2; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 278 | if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 279 | return true; |
| 280 | return false; |
| 281 | } |
| 282 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 283 | // FIXME: This should probably go in the ISD class rather than being duplicated |
| 284 | // in several files. |
| 285 | static bool isCommutativeBinOp(unsigned Opcode) { |
| 286 | switch (Opcode) { |
| 287 | case ISD::ADD: |
| 288 | case ISD::MUL: |
| 289 | case ISD::AND: |
| 290 | case ISD::OR: |
| 291 | case ISD::XOR: return true; |
| 292 | default: return false; // FIXME: Need commutative info for user ops! |
| 293 | } |
| 294 | } |
| 295 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 296 | void DAGCombiner::Run(bool RunningAfterLegalize) { |
| 297 | // set the instance variable, so that the various visit routines may use it. |
| 298 | AfterLegalize = RunningAfterLegalize; |
| 299 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 300 | // Add all the dag nodes to the worklist. |
| 301 | WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end()); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 303 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 304 | // to the root node, preventing it from being deleted, and tracking any |
| 305 | // changes of the root. |
| 306 | HandleSDNode Dummy(DAG.getRoot()); |
| 307 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 308 | // while the worklist isn't empty, inspect the node on the end of it and |
| 309 | // try and combine it. |
| 310 | while (!WorkList.empty()) { |
| 311 | SDNode *N = WorkList.back(); |
| 312 | WorkList.pop_back(); |
| 313 | |
| 314 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 315 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 316 | // reduced number of uses, allowing other xforms. |
| 317 | if (N->use_empty() && N != &Dummy) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 318 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 319 | WorkList.push_back(N->getOperand(i).Val); |
| 320 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 321 | removeFromWorkList(N); |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 322 | DAG.DeleteNode(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 323 | continue; |
| 324 | } |
| 325 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 326 | SDOperand RV = visit(N); |
| 327 | if (RV.Val) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 328 | ++NodesCombined; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 329 | // If we get back the same node we passed in, rather than a new node or |
| 330 | // zero, we know that the node must have defined multiple values and |
| 331 | // CombineTo was used. Since CombineTo takes care of the worklist |
| 332 | // mechanics for us, we have no work to do in this case. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 333 | if (RV.Val != N) { |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 334 | DEBUG(std::cerr << "\nReplacing "; N->dump(); |
| 335 | std::cerr << "\nWith: "; RV.Val->dump(); |
| 336 | std::cerr << '\n'); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 337 | DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV)); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 338 | |
| 339 | // Push the new node and any users onto the worklist |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 340 | WorkList.push_back(RV.Val); |
| 341 | AddUsersToWorkList(RV.Val); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 342 | |
| 343 | // Nodes can end up on the worklist more than once. Make sure we do |
| 344 | // not process a node that has been replaced. |
| 345 | removeFromWorkList(N); |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 346 | |
| 347 | // Finally, since the node is now dead, remove it from the graph. |
| 348 | DAG.DeleteNode(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 349 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 352 | |
| 353 | // If the root changed (e.g. it was a dead load, update the root). |
| 354 | DAG.setRoot(Dummy.getValue()); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 357 | SDOperand DAGCombiner::visit(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 358 | switch(N->getOpcode()) { |
| 359 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 360 | case ISD::TokenFactor: return visitTokenFactor(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 361 | case ISD::ADD: return visitADD(N); |
| 362 | case ISD::SUB: return visitSUB(N); |
| 363 | case ISD::MUL: return visitMUL(N); |
| 364 | case ISD::SDIV: return visitSDIV(N); |
| 365 | case ISD::UDIV: return visitUDIV(N); |
| 366 | case ISD::SREM: return visitSREM(N); |
| 367 | case ISD::UREM: return visitUREM(N); |
| 368 | case ISD::MULHU: return visitMULHU(N); |
| 369 | case ISD::MULHS: return visitMULHS(N); |
| 370 | case ISD::AND: return visitAND(N); |
| 371 | case ISD::OR: return visitOR(N); |
| 372 | case ISD::XOR: return visitXOR(N); |
| 373 | case ISD::SHL: return visitSHL(N); |
| 374 | case ISD::SRA: return visitSRA(N); |
| 375 | case ISD::SRL: return visitSRL(N); |
| 376 | case ISD::CTLZ: return visitCTLZ(N); |
| 377 | case ISD::CTTZ: return visitCTTZ(N); |
| 378 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 379 | case ISD::SELECT: return visitSELECT(N); |
| 380 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 381 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 382 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 383 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
| 384 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 385 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 386 | case ISD::FADD: return visitFADD(N); |
| 387 | case ISD::FSUB: return visitFSUB(N); |
| 388 | case ISD::FMUL: return visitFMUL(N); |
| 389 | case ISD::FDIV: return visitFDIV(N); |
| 390 | case ISD::FREM: return visitFREM(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 391 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 392 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 393 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 394 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 395 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 396 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 397 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 398 | case ISD::FNEG: return visitFNEG(N); |
| 399 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 400 | case ISD::BRCOND: return visitBRCOND(N); |
| 401 | case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N); |
| 402 | case ISD::BR_CC: return visitBR_CC(N); |
| 403 | case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 404 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 405 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 408 | SDOperand DAGCombiner::visitTokenFactor(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 409 | // If the token factor has two operands and one is the entry token, replace |
| 410 | // the token factor with the other operand. |
| 411 | if (N->getNumOperands() == 2) { |
| 412 | if (N->getOperand(0).getOpcode() == ISD::EntryToken) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 413 | return N->getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 414 | if (N->getOperand(1).getOpcode() == ISD::EntryToken) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 415 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 416 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 417 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 420 | SDOperand DAGCombiner::visitADD(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 421 | SDOperand N0 = N->getOperand(0); |
| 422 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 423 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 424 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 425 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 426 | |
| 427 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 428 | if (N0C && N1C) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 429 | return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 430 | // canonicalize constant to RHS |
| 431 | if (N0C && !N1C) { |
| 432 | std::swap(N0, N1); |
| 433 | std::swap(N0C, N1C); |
| 434 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 435 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 436 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 437 | return N0; |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 438 | // fold (add (add x, c1), c2) -> (add x, c1+c2) |
| 439 | if (N1C && N0.getOpcode() == ISD::ADD) { |
| 440 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 441 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 442 | if (N00C) |
| 443 | return DAG.getNode(ISD::ADD, VT, N0.getOperand(1), |
| 444 | DAG.getConstant(N1C->getValue()+N00C->getValue(), VT)); |
| 445 | if (N01C) |
| 446 | return DAG.getNode(ISD::ADD, VT, N0.getOperand(0), |
| 447 | DAG.getConstant(N1C->getValue()+N01C->getValue(), VT)); |
| 448 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 449 | // fold ((0-A) + B) -> B-A |
| 450 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 451 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 452 | return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 453 | // fold (A + (0-B)) -> A-B |
| 454 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 455 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 456 | return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 457 | // fold (A+(B-A)) -> B |
| 458 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 459 | return N1.getOperand(0); |
| 460 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 463 | SDOperand DAGCombiner::visitSUB(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 464 | SDOperand N0 = N->getOperand(0); |
| 465 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 466 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 467 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 468 | |
| 469 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 470 | if (N0C && N1C) |
| 471 | return DAG.getConstant(N0C->getValue() - N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 472 | N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 473 | // fold (sub x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 474 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 475 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 476 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 477 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 478 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 479 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 480 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 481 | return N0.getOperand(0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 482 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 485 | SDOperand DAGCombiner::visitMUL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 486 | SDOperand N0 = N->getOperand(0); |
| 487 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 488 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 489 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 490 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 491 | |
| 492 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 493 | if (N0C && N1C) |
| 494 | return DAG.getConstant(N0C->getValue() * N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 495 | N->getValueType(0)); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 496 | // canonicalize constant to RHS |
| 497 | if (N0C && !N1C) { |
| 498 | std::swap(N0, N1); |
| 499 | std::swap(N0C, N1C); |
| 500 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 501 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 502 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 503 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 504 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 505 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 506 | return DAG.getNode(ISD::SUB, N->getValueType(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 507 | DAG.getConstant(0, N->getValueType(0)), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 508 | // fold (mul x, (1 << c)) -> x << c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 509 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 510 | return DAG.getNode(ISD::SHL, N->getValueType(0), N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 511 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 512 | TLI.getShiftAmountTy())); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 513 | // fold (mul (mul x, c1), c2) -> (mul x, c1*c2) |
| 514 | if (N1C && N0.getOpcode() == ISD::MUL) { |
| 515 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 516 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 517 | if (N00C) |
| 518 | return DAG.getNode(ISD::MUL, VT, N0.getOperand(1), |
| 519 | DAG.getConstant(N1C->getValue()*N00C->getValue(), VT)); |
| 520 | if (N01C) |
| 521 | return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), |
| 522 | DAG.getConstant(N1C->getValue()*N01C->getValue(), VT)); |
| 523 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 524 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 527 | SDOperand DAGCombiner::visitSDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 528 | SDOperand N0 = N->getOperand(0); |
| 529 | SDOperand N1 = N->getOperand(1); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 530 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 531 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 532 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 533 | |
| 534 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 535 | if (N0C && N1C && !N1C->isNullValue()) |
| 536 | return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 537 | N->getValueType(0)); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 538 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 539 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
| 540 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
| 541 | if (MaskedValueIsZero(N1, SignBit, TLI) && |
| 542 | MaskedValueIsZero(N0, SignBit, TLI)) |
| 543 | return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 544 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 547 | SDOperand DAGCombiner::visitUDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 548 | SDOperand N0 = N->getOperand(0); |
| 549 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 550 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 551 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 552 | |
| 553 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 554 | if (N0C && N1C && !N1C->isNullValue()) |
| 555 | return DAG.getConstant(N0C->getValue() / N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 556 | N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 557 | // fold (udiv x, (1 << c)) -> x >>u c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 558 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 559 | return DAG.getNode(ISD::SRL, N->getValueType(0), N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 560 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 561 | TLI.getShiftAmountTy())); |
| 562 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 565 | SDOperand DAGCombiner::visitSREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 566 | SDOperand N0 = N->getOperand(0); |
| 567 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 568 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 569 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 570 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 571 | |
| 572 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 573 | if (N0C && N1C && !N1C->isNullValue()) |
| 574 | return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 575 | N->getValueType(0)); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 576 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 577 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
| 578 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
| 579 | if (MaskedValueIsZero(N1, SignBit, TLI) && |
| 580 | MaskedValueIsZero(N0, SignBit, TLI)) |
| 581 | return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 582 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 585 | SDOperand DAGCombiner::visitUREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 586 | SDOperand N0 = N->getOperand(0); |
| 587 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 588 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 589 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 590 | |
| 591 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 592 | if (N0C && N1C && !N1C->isNullValue()) |
| 593 | return DAG.getConstant(N0C->getValue() % N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 594 | N->getValueType(0)); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 595 | // fold (urem x, pow2) -> (and x, pow2-1) |
| 596 | if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue())) |
| 597 | return DAG.getNode(ISD::AND, N0.getValueType(), N0, |
| 598 | DAG.getConstant(N1C->getValue()-1, N1.getValueType())); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 599 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 602 | SDOperand DAGCombiner::visitMULHS(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 603 | SDOperand N0 = N->getOperand(0); |
| 604 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 605 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 606 | |
| 607 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 608 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 609 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 610 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 611 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 612 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0, |
| 613 | DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 614 | TLI.getShiftAmountTy())); |
| 615 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 618 | SDOperand DAGCombiner::visitMULHU(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 619 | SDOperand N0 = N->getOperand(0); |
| 620 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 621 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 622 | |
| 623 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 624 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 625 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 626 | // fold (mulhu x, 1) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 627 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 628 | return DAG.getConstant(0, N0.getValueType()); |
| 629 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 632 | SDOperand DAGCombiner::visitAND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 633 | SDOperand N0 = N->getOperand(0); |
| 634 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 635 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 636 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 637 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 638 | MVT::ValueType VT = N1.getValueType(); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 639 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 640 | |
| 641 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 642 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 643 | return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 644 | // canonicalize constant to RHS |
| 645 | if (N0C && !N1C) { |
| 646 | std::swap(N0, N1); |
| 647 | std::swap(N0C, N1C); |
| 648 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 649 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 650 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 651 | return N0; |
| 652 | // if (and x, c) is known to be zero, return 0 |
| 653 | if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
| 654 | return DAG.getConstant(0, VT); |
| 655 | // fold (and x, c) -> x iff (x & ~c) == 0 |
| 656 | if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)), |
| 657 | TLI)) |
| 658 | return N0; |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 659 | // fold (and (and x, c1), c2) -> (and x, c1^c2) |
| 660 | if (N1C && N0.getOpcode() == ISD::AND) { |
| 661 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 662 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 663 | if (N00C) |
| 664 | return DAG.getNode(ISD::AND, VT, N0.getOperand(1), |
| 665 | DAG.getConstant(N1C->getValue()&N00C->getValue(), VT)); |
| 666 | if (N01C) |
| 667 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 668 | DAG.getConstant(N1C->getValue()&N01C->getValue(), VT)); |
| 669 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 670 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 671 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) { |
| 672 | unsigned ExtendBits = |
| 673 | MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT()); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 674 | if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 675 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 676 | } |
| 677 | // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF |
| 678 | if (N0.getOpcode() == ISD::OR) |
| 679 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 680 | if ((ORI->getValue() & N1C->getValue()) == N1C->getValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 681 | return N1; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 682 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 683 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 684 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 685 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 686 | |
| 687 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 688 | MVT::isInteger(LL.getValueType())) { |
| 689 | // fold (X == 0) & (Y == 0) -> (X|Y == 0) |
| 690 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) { |
| 691 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
| 692 | WorkList.push_back(ORNode.Val); |
| 693 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 694 | } |
| 695 | // fold (X == -1) & (Y == -1) -> (X&Y == -1) |
| 696 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
| 697 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
| 698 | WorkList.push_back(ANDNode.Val); |
| 699 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 700 | } |
| 701 | // fold (X > -1) & (Y > -1) -> (X|Y > -1) |
| 702 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
| 703 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
| 704 | WorkList.push_back(ORNode.Val); |
| 705 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 706 | } |
| 707 | } |
| 708 | // canonicalize equivalent to ll == rl |
| 709 | if (LL == RR && LR == RL) { |
| 710 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 711 | std::swap(RL, RR); |
| 712 | } |
| 713 | if (LL == RL && LR == RR) { |
| 714 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 715 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
| 716 | if (Result != ISD::SETCC_INVALID) |
| 717 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 718 | } |
| 719 | } |
| 720 | // fold (and (zext x), (zext y)) -> (zext (and x, y)) |
| 721 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 722 | N1.getOpcode() == ISD::ZERO_EXTEND && |
| 723 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 724 | SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(), |
| 725 | N0.getOperand(0), N1.getOperand(0)); |
| 726 | WorkList.push_back(ANDNode.Val); |
| 727 | return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode); |
| 728 | } |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 729 | // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y)) |
| 730 | if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) || |
| 731 | (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) && |
| 732 | N0.getOperand(1) == N1.getOperand(1)) { |
| 733 | SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(), |
| 734 | N0.getOperand(0), N1.getOperand(0)); |
| 735 | WorkList.push_back(ANDNode.Val); |
| 736 | return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1)); |
| 737 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 738 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 741 | SDOperand DAGCombiner::visitOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 742 | SDOperand N0 = N->getOperand(0); |
| 743 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 744 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 745 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 746 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 747 | MVT::ValueType VT = N1.getValueType(); |
| 748 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 749 | |
| 750 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 751 | if (N0C && N1C) |
| 752 | return DAG.getConstant(N0C->getValue() | N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 753 | N->getValueType(0)); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 754 | // canonicalize constant to RHS |
| 755 | if (N0C && !N1C) { |
| 756 | std::swap(N0, N1); |
| 757 | std::swap(N0C, N1C); |
| 758 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 759 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 760 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 761 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 762 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 763 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 764 | return N1; |
| 765 | // fold (or x, c) -> c iff (x & ~c) == 0 |
| 766 | if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)), |
| 767 | TLI)) |
| 768 | return N1; |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 769 | // fold (or (or x, c1), c2) -> (or x, c1|c2) |
| 770 | if (N1C && N0.getOpcode() == ISD::OR) { |
| 771 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 772 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 773 | if (N00C) |
| 774 | return DAG.getNode(ISD::OR, VT, N0.getOperand(1), |
| 775 | DAG.getConstant(N1C->getValue()|N00C->getValue(), VT)); |
| 776 | if (N01C) |
| 777 | return DAG.getNode(ISD::OR, VT, N0.getOperand(0), |
| 778 | DAG.getConstant(N1C->getValue()|N01C->getValue(), VT)); |
| 779 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 780 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 781 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 782 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 783 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 784 | |
| 785 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 786 | MVT::isInteger(LL.getValueType())) { |
| 787 | // fold (X != 0) | (Y != 0) -> (X|Y != 0) |
| 788 | // fold (X < 0) | (Y < 0) -> (X|Y < 0) |
| 789 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && |
| 790 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
| 791 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
| 792 | WorkList.push_back(ORNode.Val); |
| 793 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 794 | } |
| 795 | // fold (X != -1) | (Y != -1) -> (X&Y != -1) |
| 796 | // fold (X > -1) | (Y > -1) -> (X&Y > -1) |
| 797 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
| 798 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
| 799 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
| 800 | WorkList.push_back(ANDNode.Val); |
| 801 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 802 | } |
| 803 | } |
| 804 | // canonicalize equivalent to ll == rl |
| 805 | if (LL == RR && LR == RL) { |
| 806 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 807 | std::swap(RL, RR); |
| 808 | } |
| 809 | if (LL == RL && LR == RR) { |
| 810 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 811 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
| 812 | if (Result != ISD::SETCC_INVALID) |
| 813 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 814 | } |
| 815 | } |
| 816 | // fold (or (zext x), (zext y)) -> (zext (or x, y)) |
| 817 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 818 | N1.getOpcode() == ISD::ZERO_EXTEND && |
| 819 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 820 | SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(), |
| 821 | N0.getOperand(0), N1.getOperand(0)); |
| 822 | WorkList.push_back(ORNode.Val); |
| 823 | return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode); |
| 824 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 825 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 828 | SDOperand DAGCombiner::visitXOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 829 | SDOperand N0 = N->getOperand(0); |
| 830 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 831 | SDOperand LHS, RHS, CC; |
| 832 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 833 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 834 | MVT::ValueType VT = N0.getValueType(); |
| 835 | |
| 836 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 837 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 838 | return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 839 | // canonicalize constant to RHS |
| 840 | if (N0C && !N1C) { |
| 841 | std::swap(N0, N1); |
| 842 | std::swap(N0C, N1C); |
| 843 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 844 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 845 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 846 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 847 | // fold !(x cc y) -> (x !cc y) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 848 | if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
| 849 | bool isInt = MVT::isInteger(LHS.getValueType()); |
| 850 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 851 | isInt); |
| 852 | if (N0.getOpcode() == ISD::SETCC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 853 | return DAG.getSetCC(VT, LHS, RHS, NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 854 | if (N0.getOpcode() == ISD::SELECT_CC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 855 | return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 856 | assert(0 && "Unhandled SetCC Equivalent!"); |
| 857 | abort(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 858 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 859 | // fold !(x or y) -> (!x and !y) iff x or y are setcc |
| 860 | if (N1C && N1C->getValue() == 1 && |
| 861 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 862 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 863 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 864 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 865 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 866 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 867 | WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val); |
| 868 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 871 | // fold !(x or y) -> (!x and !y) iff x or y are constants |
| 872 | if (N1C && N1C->isAllOnesValue() && |
| 873 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 874 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 875 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 876 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 877 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 878 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 879 | WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val); |
| 880 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 883 | // fold (xor (xor x, c1), c2) -> (xor x, c1^c2) |
| 884 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 885 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 886 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 887 | if (N00C) |
| 888 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(1), |
| 889 | DAG.getConstant(N1C->getValue()^N00C->getValue(), VT)); |
| 890 | if (N01C) |
| 891 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(0), |
| 892 | DAG.getConstant(N1C->getValue()^N01C->getValue(), VT)); |
| 893 | } |
| 894 | // fold (xor x, x) -> 0 |
| 895 | if (N0 == N1) |
| 896 | return DAG.getConstant(0, VT); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 897 | // fold (xor (zext x), (zext y)) -> (zext (xor x, y)) |
| 898 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 899 | N1.getOpcode() == ISD::ZERO_EXTEND && |
| 900 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 901 | SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(), |
| 902 | N0.getOperand(0), N1.getOperand(0)); |
| 903 | WorkList.push_back(XORNode.Val); |
| 904 | return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode); |
| 905 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 906 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 909 | SDOperand DAGCombiner::visitSHL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 910 | SDOperand N0 = N->getOperand(0); |
| 911 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 912 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 913 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 914 | MVT::ValueType VT = N0.getValueType(); |
| 915 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 916 | |
| 917 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 918 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 919 | return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 920 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 921 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 922 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 923 | // fold (shl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 924 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 925 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 926 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 927 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 928 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 929 | // if (shl x, c) is known to be zero, return 0 |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 930 | if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
| 931 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 932 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 933 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 934 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 935 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 936 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 937 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 938 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 939 | return DAG.getNode(ISD::SHL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 940 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 941 | } |
| 942 | // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or |
| 943 | // (srl (and x, -1 << c1), c1-c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 944 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 945 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 946 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 947 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 948 | SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 949 | DAG.getConstant(~0ULL << c1, VT)); |
| 950 | if (c2 > c1) |
| 951 | return DAG.getNode(ISD::SHL, VT, Mask, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 952 | DAG.getConstant(c2-c1, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 953 | else |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 954 | return DAG.getNode(ISD::SRL, VT, Mask, |
| 955 | DAG.getConstant(c1-c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 956 | } |
| 957 | // fold (shl (sra x, c1), c1) -> (and x, -1 << c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 958 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 959 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 960 | DAG.getConstant(~0ULL << N1C->getValue(), VT)); |
| 961 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 964 | SDOperand DAGCombiner::visitSRA(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 965 | SDOperand N0 = N->getOperand(0); |
| 966 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 967 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 968 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 969 | MVT::ValueType VT = N0.getValueType(); |
| 970 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 971 | |
| 972 | // fold (sra c1, c2) -> c1>>c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 973 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 974 | return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 975 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 976 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 977 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 978 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 979 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 980 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 981 | // fold (sra x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 982 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 983 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 984 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 985 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 986 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 987 | // If the sign bit is known to be zero, switch this to a SRL. |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 988 | if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 989 | return DAG.getNode(ISD::SRL, VT, N0, N1); |
| 990 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 993 | SDOperand DAGCombiner::visitSRL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 994 | SDOperand N0 = N->getOperand(0); |
| 995 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 996 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 997 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 998 | MVT::ValueType VT = N0.getValueType(); |
| 999 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1000 | |
| 1001 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1002 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1003 | return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1004 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1005 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1006 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1007 | // fold (srl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1008 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1009 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1010 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1011 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1012 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1013 | // if (srl x, c) is known to be zero, return 0 |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1014 | if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
| 1015 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1016 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1017 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1018 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1019 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1020 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1021 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1022 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1023 | return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1024 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1025 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1026 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1029 | SDOperand DAGCombiner::visitCTLZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1030 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1031 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1032 | |
| 1033 | // fold (ctlz c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1034 | if (N0C) |
| 1035 | return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1036 | N0.getValueType()); |
| 1037 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1040 | SDOperand DAGCombiner::visitCTTZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1041 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1042 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1043 | |
| 1044 | // fold (cttz c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1045 | if (N0C) |
| 1046 | return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1047 | N0.getValueType()); |
| 1048 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1051 | SDOperand DAGCombiner::visitCTPOP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1052 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1053 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1054 | |
| 1055 | // fold (ctpop c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1056 | if (N0C) |
| 1057 | return DAG.getConstant(CountPopulation_64(N0C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1058 | N0.getValueType()); |
| 1059 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1062 | SDOperand DAGCombiner::visitSELECT(SDNode *N) { |
| 1063 | SDOperand N0 = N->getOperand(0); |
| 1064 | SDOperand N1 = N->getOperand(1); |
| 1065 | SDOperand N2 = N->getOperand(2); |
| 1066 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1067 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1068 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1069 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1070 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1071 | // fold select C, X, X -> X |
| 1072 | if (N1 == N2) |
| 1073 | return N1; |
| 1074 | // fold select true, X, Y -> X |
| 1075 | if (N0C && !N0C->isNullValue()) |
| 1076 | return N1; |
| 1077 | // fold select false, X, Y -> Y |
| 1078 | if (N0C && N0C->isNullValue()) |
| 1079 | return N2; |
| 1080 | // fold select C, 1, X -> C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1081 | if (MVT::i1 == VT && N1C && N1C->getValue() == 1) |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1082 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1083 | // fold select C, 0, X -> ~C & X |
| 1084 | // FIXME: this should check for C type == X type, not i1? |
| 1085 | if (MVT::i1 == VT && N1C && N1C->isNullValue()) { |
| 1086 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
| 1087 | WorkList.push_back(XORNode.Val); |
| 1088 | return DAG.getNode(ISD::AND, VT, XORNode, N2); |
| 1089 | } |
| 1090 | // fold select C, X, 1 -> ~C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1091 | if (MVT::i1 == VT && N2C && N2C->getValue() == 1) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1092 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
| 1093 | WorkList.push_back(XORNode.Val); |
| 1094 | return DAG.getNode(ISD::OR, VT, XORNode, N1); |
| 1095 | } |
| 1096 | // fold select C, X, 0 -> C & X |
| 1097 | // FIXME: this should check for C type == X type, not i1? |
| 1098 | if (MVT::i1 == VT && N2C && N2C->isNullValue()) |
| 1099 | return DAG.getNode(ISD::AND, VT, N0, N1); |
| 1100 | // fold X ? X : Y --> X ? 1 : Y --> X | Y |
| 1101 | if (MVT::i1 == VT && N0 == N1) |
| 1102 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1103 | // fold X ? Y : X --> X ? Y : 0 --> X & Y |
| 1104 | if (MVT::i1 == VT && N0 == N2) |
| 1105 | return DAG.getNode(ISD::AND, VT, N0, N1); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1106 | // fold selects based on a setcc into other things, such as min/max/abs |
| 1107 | if (N0.getOpcode() == ISD::SETCC) |
| 1108 | return SimplifySelect(N0, N1, N2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1109 | return SDOperand(); |
| 1110 | } |
| 1111 | |
| 1112 | SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) { |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1113 | SDOperand N0 = N->getOperand(0); |
| 1114 | SDOperand N1 = N->getOperand(1); |
| 1115 | SDOperand N2 = N->getOperand(2); |
| 1116 | SDOperand N3 = N->getOperand(3); |
| 1117 | SDOperand N4 = N->getOperand(4); |
| 1118 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1119 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1120 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1121 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
| 1122 | |
| 1123 | // Determine if the condition we're dealing with is constant |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1124 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
Chris Lattner | 9155902 | 2005-10-05 04:45:43 +0000 | [diff] [blame] | 1125 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 1126 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1127 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 1128 | if (N2 == N3) |
| 1129 | return N2; |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1130 | // fold select_cc into other things, such as min/max/abs |
| 1131 | return SimplifySelectCC(N0, N1, N2, N3, CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | SDOperand DAGCombiner::visitSETCC(SDNode *N) { |
| 1135 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
| 1136 | cast<CondCodeSDNode>(N->getOperand(2))->get()); |
| 1137 | } |
| 1138 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1139 | SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1140 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1141 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1142 | MVT::ValueType VT = N->getValueType(0); |
| 1143 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1144 | // fold (sext c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1145 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1146 | return DAG.getConstant(N0C->getSignExtended(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1147 | // fold (sext (sext x)) -> (sext x) |
| 1148 | if (N0.getOpcode() == ISD::SIGN_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1149 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0)); |
| 1150 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1153 | SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1154 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1155 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1156 | MVT::ValueType VT = N->getValueType(0); |
| 1157 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1158 | // fold (zext c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1159 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1160 | return DAG.getConstant(N0C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1161 | // fold (zext (zext x)) -> (zext x) |
| 1162 | if (N0.getOpcode() == ISD::ZERO_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1163 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0)); |
| 1164 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1167 | SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1168 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1169 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1170 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1171 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1172 | MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT(); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 1173 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1174 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1175 | // fold (sext_in_reg c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1176 | if (N0C) { |
| 1177 | SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1178 | return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1179 | } |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1180 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1 |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1181 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1182 | cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1183 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1184 | } |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1185 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 1186 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1187 | EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1188 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1189 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1190 | // fold (sext_in_reg (assert_sext x)) -> (assert_sext x) |
| 1191 | if (N0.getOpcode() == ISD::AssertSext && |
| 1192 | cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1193 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1194 | } |
| 1195 | // fold (sext_in_reg (sextload x)) -> (sextload x) |
| 1196 | if (N0.getOpcode() == ISD::SEXTLOAD && |
| 1197 | cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1198 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1199 | } |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1200 | // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1 |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1201 | if (N0.getOpcode() == ISD::SETCC && |
| 1202 | TLI.getSetCCResultContents() == |
| 1203 | TargetLowering::ZeroOrNegativeOneSetCCResult) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1204 | return N0; |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 1205 | // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero |
| 1206 | if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI)) |
| 1207 | return DAG.getNode(ISD::AND, N0.getValueType(), N0, |
| 1208 | DAG.getConstant(~0ULL >> (64-EVTBits), VT)); |
| 1209 | // fold (sext_in_reg (srl x)) -> sra x |
| 1210 | if (N0.getOpcode() == ISD::SRL && |
| 1211 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 1212 | cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) { |
| 1213 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0), |
| 1214 | N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1215 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1216 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1219 | SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1220 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1221 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1222 | MVT::ValueType VT = N->getValueType(0); |
| 1223 | |
| 1224 | // noop truncate |
| 1225 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1226 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1227 | // fold (truncate c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1228 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1229 | return DAG.getConstant(N0C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1230 | // fold (truncate (truncate x)) -> (truncate x) |
| 1231 | if (N0.getOpcode() == ISD::TRUNCATE) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1232 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1233 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
| 1234 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){ |
| 1235 | if (N0.getValueType() < VT) |
| 1236 | // if the source is smaller than the dest, we still need an extend |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1237 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1238 | else if (N0.getValueType() > VT) |
| 1239 | // if the source is larger than the dest, than we just need the truncate |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1240 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1241 | else |
| 1242 | // if the source and dest are the same type, we can drop both the extend |
| 1243 | // and the truncate |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1244 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1245 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1246 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1249 | SDOperand DAGCombiner::visitFADD(SDNode *N) { |
| 1250 | SDOperand N0 = N->getOperand(0); |
| 1251 | SDOperand N1 = N->getOperand(1); |
| 1252 | MVT::ValueType VT = N->getValueType(0); |
| 1253 | |
| 1254 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1255 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1256 | // fold floating point (fadd c1, c2) |
| 1257 | return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), |
| 1258 | N->getValueType(0)); |
| 1259 | } |
| 1260 | // fold (A + (-B)) -> A-B |
| 1261 | if (N1.getOpcode() == ISD::FNEG) |
| 1262 | return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0)); |
| 1263 | |
| 1264 | // fold ((-A) + B) -> B-A |
| 1265 | if (N0.getOpcode() == ISD::FNEG) |
| 1266 | return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0)); |
| 1267 | |
| 1268 | return SDOperand(); |
| 1269 | } |
| 1270 | |
| 1271 | SDOperand DAGCombiner::visitFSUB(SDNode *N) { |
| 1272 | SDOperand N0 = N->getOperand(0); |
| 1273 | SDOperand N1 = N->getOperand(1); |
| 1274 | MVT::ValueType VT = N->getValueType(0); |
| 1275 | |
| 1276 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1277 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1278 | // fold floating point (fsub c1, c2) |
| 1279 | return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(), |
| 1280 | N->getValueType(0)); |
| 1281 | } |
| 1282 | // fold (A-(-B)) -> A+B |
| 1283 | if (N1.getOpcode() == ISD::FNEG) |
| 1284 | return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0)); |
| 1285 | |
| 1286 | return SDOperand(); |
| 1287 | } |
| 1288 | |
| 1289 | SDOperand DAGCombiner::visitFMUL(SDNode *N) { |
| 1290 | SDOperand N0 = N->getOperand(0); |
| 1291 | SDOperand N1 = N->getOperand(1); |
| 1292 | MVT::ValueType VT = N->getValueType(0); |
| 1293 | |
| 1294 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1295 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1296 | // fold floating point (fmul c1, c2) |
| 1297 | return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(), |
| 1298 | N->getValueType(0)); |
| 1299 | } |
| 1300 | return SDOperand(); |
| 1301 | } |
| 1302 | |
| 1303 | SDOperand DAGCombiner::visitFDIV(SDNode *N) { |
| 1304 | SDOperand N0 = N->getOperand(0); |
| 1305 | SDOperand N1 = N->getOperand(1); |
| 1306 | MVT::ValueType VT = N->getValueType(0); |
| 1307 | |
| 1308 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1309 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1310 | // fold floating point (fdiv c1, c2) |
| 1311 | return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(), |
| 1312 | N->getValueType(0)); |
| 1313 | } |
| 1314 | return SDOperand(); |
| 1315 | } |
| 1316 | |
| 1317 | SDOperand DAGCombiner::visitFREM(SDNode *N) { |
| 1318 | SDOperand N0 = N->getOperand(0); |
| 1319 | SDOperand N1 = N->getOperand(1); |
| 1320 | MVT::ValueType VT = N->getValueType(0); |
| 1321 | |
| 1322 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1323 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1324 | // fold floating point (frem c1, c2) -> fmod(c1, c2) |
| 1325 | return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()), |
| 1326 | N->getValueType(0)); |
| 1327 | } |
| 1328 | return SDOperand(); |
| 1329 | } |
| 1330 | |
| 1331 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1332 | SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1333 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1334 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1335 | |
| 1336 | // fold (sint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1337 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1338 | return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0)); |
| 1339 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1342 | SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1343 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1344 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1345 | |
| 1346 | // fold (uint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1347 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1348 | return DAG.getConstantFP(N0C->getValue(), N->getValueType(0)); |
| 1349 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1352 | SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1353 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1354 | |
| 1355 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1356 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1357 | return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0)); |
| 1358 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1361 | SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1362 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1363 | |
| 1364 | // fold (fp_to_uint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1365 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1366 | return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0)); |
| 1367 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1370 | SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1371 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1372 | |
| 1373 | // fold (fp_round c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1374 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1375 | return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0)); |
| 1376 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1379 | SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1380 | SDOperand N0 = N->getOperand(0); |
| 1381 | MVT::ValueType VT = N->getValueType(0); |
| 1382 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1383 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1384 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1385 | // fold (fp_round_inreg c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1386 | if (N0CFP) { |
| 1387 | SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1388 | return DAG.getNode(ISD::FP_EXTEND, VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1389 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1390 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1393 | SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1394 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1395 | |
| 1396 | // fold (fp_extend c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1397 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1398 | return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0)); |
| 1399 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1402 | SDOperand DAGCombiner::visitFNEG(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1403 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1404 | // fold (neg c1) -> -c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1405 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1406 | return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1407 | // fold (neg (sub x, y)) -> (sub y, x) |
| 1408 | if (N->getOperand(0).getOpcode() == ISD::SUB) |
| 1409 | return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1410 | N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1411 | // fold (neg (neg x)) -> x |
| 1412 | if (N->getOperand(0).getOpcode() == ISD::FNEG) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1413 | return N->getOperand(0).getOperand(0); |
| 1414 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1417 | SDOperand DAGCombiner::visitFABS(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1418 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1419 | // fold (fabs c1) -> fabs(c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1420 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1421 | return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1422 | // fold (fabs (fabs x)) -> (fabs x) |
| 1423 | if (N->getOperand(0).getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1424 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1425 | // fold (fabs (fneg x)) -> (fabs x) |
| 1426 | if (N->getOperand(0).getOpcode() == ISD::FNEG) |
| 1427 | return DAG.getNode(ISD::FABS, N->getValueType(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1428 | N->getOperand(0).getOperand(0)); |
| 1429 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1432 | SDOperand DAGCombiner::visitBRCOND(SDNode *N) { |
| 1433 | SDOperand Chain = N->getOperand(0); |
| 1434 | SDOperand N1 = N->getOperand(1); |
| 1435 | SDOperand N2 = N->getOperand(2); |
| 1436 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1437 | |
| 1438 | // never taken branch, fold to chain |
| 1439 | if (N1C && N1C->isNullValue()) |
| 1440 | return Chain; |
| 1441 | // unconditional branch |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1442 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1443 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N2); |
| 1444 | return SDOperand(); |
| 1445 | } |
| 1446 | |
| 1447 | SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) { |
| 1448 | SDOperand Chain = N->getOperand(0); |
| 1449 | SDOperand N1 = N->getOperand(1); |
| 1450 | SDOperand N2 = N->getOperand(2); |
| 1451 | SDOperand N3 = N->getOperand(3); |
| 1452 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1453 | |
| 1454 | // unconditional branch to true mbb |
| 1455 | if (N1C && N1C->getValue() == 1) |
| 1456 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N2); |
| 1457 | // unconditional branch to false mbb |
| 1458 | if (N1C && N1C->isNullValue()) |
| 1459 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N3); |
| 1460 | return SDOperand(); |
| 1461 | } |
| 1462 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 1463 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 1464 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1465 | SDOperand DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 1466 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
| 1467 | SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
| 1468 | |
| 1469 | // Use SimplifySetCC to simplify SETCC's. |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1470 | SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false); |
| 1471 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val); |
| 1472 | |
| 1473 | // fold br_cc true, dest -> br dest (unconditional branch) |
| 1474 | if (SCCC && SCCC->getValue()) |
| 1475 | return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0), |
| 1476 | N->getOperand(4)); |
| 1477 | // fold br_cc false, dest -> unconditional fall through |
| 1478 | if (SCCC && SCCC->isNullValue()) |
| 1479 | return N->getOperand(0); |
| 1480 | // fold to a simpler setcc |
| 1481 | if (Simp.Val && Simp.getOpcode() == ISD::SETCC) |
| 1482 | return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0), |
| 1483 | Simp.getOperand(2), Simp.getOperand(0), |
| 1484 | Simp.getOperand(1), N->getOperand(4)); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1485 | return SDOperand(); |
| 1486 | } |
| 1487 | |
| 1488 | SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) { |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1489 | SDOperand Chain = N->getOperand(0); |
| 1490 | SDOperand CCN = N->getOperand(1); |
| 1491 | SDOperand LHS = N->getOperand(2); |
| 1492 | SDOperand RHS = N->getOperand(3); |
| 1493 | SDOperand N4 = N->getOperand(4); |
| 1494 | SDOperand N5 = N->getOperand(5); |
| 1495 | |
| 1496 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS, |
| 1497 | cast<CondCodeSDNode>(CCN)->get(), false); |
| 1498 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 1499 | |
| 1500 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 1501 | if (N4 == N5) |
| 1502 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N4); |
| 1503 | // fold select_cc true, x, y -> x |
| 1504 | if (SCCC && SCCC->getValue()) |
| 1505 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N4); |
| 1506 | // fold select_cc false, x, y -> y |
| 1507 | if (SCCC && SCCC->isNullValue()) |
| 1508 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N5); |
| 1509 | // fold to a simpler setcc |
| 1510 | if (SCC.Val && SCC.getOpcode() == ISD::SETCC) |
| 1511 | return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0), |
| 1512 | SCC.getOperand(1), N4, N5); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1513 | return SDOperand(); |
| 1514 | } |
| 1515 | |
| 1516 | SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 1517 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
| 1518 | |
| 1519 | SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2, |
| 1520 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 1521 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 1522 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 1523 | // the SELECT node, since we were called with a SELECT node. |
| 1524 | if (SCC.Val) { |
| 1525 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 1526 | // Otherwise, just return whatever node we got back, like fabs. |
| 1527 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
| 1528 | SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(), |
| 1529 | SCC.getOperand(0), SCC.getOperand(1), |
| 1530 | SCC.getOperand(4)); |
| 1531 | WorkList.push_back(SETCC.Val); |
| 1532 | return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2), |
| 1533 | SCC.getOperand(3), SETCC); |
| 1534 | } |
| 1535 | return SCC; |
| 1536 | } |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1537 | return SDOperand(); |
| 1538 | } |
| 1539 | |
| 1540 | SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1, |
| 1541 | SDOperand N2, SDOperand N3, |
| 1542 | ISD::CondCode CC) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 1543 | |
| 1544 | MVT::ValueType VT = N2.getValueType(); |
| 1545 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 1546 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 1547 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 1548 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 1549 | |
| 1550 | // Determine if the condition we're dealing with is constant |
| 1551 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
| 1552 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 1553 | |
| 1554 | // fold select_cc true, x, y -> x |
| 1555 | if (SCCC && SCCC->getValue()) |
| 1556 | return N2; |
| 1557 | // fold select_cc false, x, y -> y |
| 1558 | if (SCCC && SCCC->getValue() == 0) |
| 1559 | return N3; |
| 1560 | |
| 1561 | // Check to see if we can simplify the select into an fabs node |
| 1562 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1563 | // Allow either -0.0 or 0.0 |
| 1564 | if (CFP->getValue() == 0.0) { |
| 1565 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 1566 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 1567 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 1568 | N2 == N3.getOperand(0)) |
| 1569 | return DAG.getNode(ISD::FABS, VT, N0); |
| 1570 | |
| 1571 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 1572 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 1573 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 1574 | N2.getOperand(0) == N3) |
| 1575 | return DAG.getNode(ISD::FABS, VT, N3); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | // Check to see if we can perform the "gzip trick", transforming |
| 1580 | // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A |
| 1581 | if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() && |
| 1582 | MVT::isInteger(N0.getValueType()) && |
| 1583 | MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) { |
| 1584 | MVT::ValueType XType = N0.getValueType(); |
| 1585 | MVT::ValueType AType = N2.getValueType(); |
| 1586 | if (XType >= AType) { |
| 1587 | // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 1588 | // single-bit constant. |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 1589 | if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) { |
| 1590 | unsigned ShCtV = Log2_64(N2C->getValue()); |
| 1591 | ShCtV = MVT::getSizeInBits(XType)-ShCtV-1; |
| 1592 | SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy()); |
| 1593 | SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt); |
| 1594 | WorkList.push_back(Shift.Val); |
| 1595 | if (XType > AType) { |
| 1596 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
| 1597 | WorkList.push_back(Shift.Val); |
| 1598 | } |
| 1599 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 1600 | } |
| 1601 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 1602 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 1603 | TLI.getShiftAmountTy())); |
| 1604 | WorkList.push_back(Shift.Val); |
| 1605 | if (XType > AType) { |
| 1606 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
| 1607 | WorkList.push_back(Shift.Val); |
| 1608 | } |
| 1609 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 1610 | } |
| 1611 | } |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame^] | 1612 | |
| 1613 | // fold select C, 16, 0 -> shl C, 4 |
| 1614 | if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) && |
| 1615 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) { |
| 1616 | // Get a SetCC of the condition |
| 1617 | // FIXME: Should probably make sure that setcc is legal if we ever have a |
| 1618 | // target where it isn't. |
| 1619 | SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
| 1620 | WorkList.push_back(SCC.Val); |
| 1621 | // cast from setcc result type to select result type |
| 1622 | if (AfterLegalize) |
| 1623 | Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType()); |
| 1624 | else |
| 1625 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC); |
| 1626 | WorkList.push_back(Temp.Val); |
| 1627 | // shl setcc result by log2 n2c |
| 1628 | return DAG.getNode(ISD::SHL, N2.getValueType(), Temp, |
| 1629 | DAG.getConstant(Log2_64(N2C->getValue()), |
| 1630 | TLI.getShiftAmountTy())); |
| 1631 | } |
| 1632 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 1633 | // Check to see if this is the equivalent of setcc |
| 1634 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 1635 | // otherwise, go ahead with the folds. |
| 1636 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) { |
| 1637 | MVT::ValueType XType = N0.getValueType(); |
| 1638 | if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) { |
| 1639 | SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
| 1640 | if (Res.getValueType() != VT) |
| 1641 | Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res); |
| 1642 | return Res; |
| 1643 | } |
| 1644 | |
| 1645 | // seteq X, 0 -> srl (ctlz X, log2(size(X))) |
| 1646 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
| 1647 | TLI.isOperationLegal(ISD::CTLZ, XType)) { |
| 1648 | SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0); |
| 1649 | return DAG.getNode(ISD::SRL, XType, Ctlz, |
| 1650 | DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)), |
| 1651 | TLI.getShiftAmountTy())); |
| 1652 | } |
| 1653 | // setgt X, 0 -> srl (and (-X, ~X), size(X)-1) |
| 1654 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
| 1655 | SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType), |
| 1656 | N0); |
| 1657 | SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0, |
| 1658 | DAG.getConstant(~0ULL, XType)); |
| 1659 | return DAG.getNode(ISD::SRL, XType, |
| 1660 | DAG.getNode(ISD::AND, XType, NegN0, NotN0), |
| 1661 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 1662 | TLI.getShiftAmountTy())); |
| 1663 | } |
| 1664 | // setgt X, -1 -> xor (srl (X, size(X)-1), 1) |
| 1665 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
| 1666 | SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0, |
| 1667 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 1668 | TLI.getShiftAmountTy())); |
| 1669 | return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType)); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X -> |
| 1674 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| 1675 | if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) && |
| 1676 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) { |
| 1677 | if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) { |
| 1678 | MVT::ValueType XType = N0.getValueType(); |
| 1679 | if (SubC->isNullValue() && MVT::isInteger(XType)) { |
| 1680 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 1681 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 1682 | TLI.getShiftAmountTy())); |
| 1683 | SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift); |
| 1684 | WorkList.push_back(Shift.Val); |
| 1685 | WorkList.push_back(Add.Val); |
| 1686 | return DAG.getNode(ISD::XOR, XType, Add, Shift); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1691 | return SDOperand(); |
| 1692 | } |
| 1693 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1694 | SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1695 | SDOperand N1, ISD::CondCode Cond, |
| 1696 | bool foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1697 | // These setcc operations always fold. |
| 1698 | switch (Cond) { |
| 1699 | default: break; |
| 1700 | case ISD::SETFALSE: |
| 1701 | case ISD::SETFALSE2: return DAG.getConstant(0, VT); |
| 1702 | case ISD::SETTRUE: |
| 1703 | case ISD::SETTRUE2: return DAG.getConstant(1, VT); |
| 1704 | } |
| 1705 | |
| 1706 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 1707 | uint64_t C1 = N1C->getValue(); |
| 1708 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) { |
| 1709 | uint64_t C0 = N0C->getValue(); |
| 1710 | |
| 1711 | // Sign extend the operands if required |
| 1712 | if (ISD::isSignedIntSetCC(Cond)) { |
| 1713 | C0 = N0C->getSignExtended(); |
| 1714 | C1 = N1C->getSignExtended(); |
| 1715 | } |
| 1716 | |
| 1717 | switch (Cond) { |
| 1718 | default: assert(0 && "Unknown integer setcc!"); |
| 1719 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 1720 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 1721 | case ISD::SETULT: return DAG.getConstant(C0 < C1, VT); |
| 1722 | case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT); |
| 1723 | case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT); |
| 1724 | case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT); |
| 1725 | case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT); |
| 1726 | case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT); |
| 1727 | case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT); |
| 1728 | case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT); |
| 1729 | } |
| 1730 | } else { |
| 1731 | // If the LHS is a ZERO_EXTEND, perform the comparison on the input. |
| 1732 | if (N0.getOpcode() == ISD::ZERO_EXTEND) { |
| 1733 | unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType()); |
| 1734 | |
| 1735 | // If the comparison constant has bits in the upper part, the |
| 1736 | // zero-extended value could never match. |
| 1737 | if (C1 & (~0ULL << InSize)) { |
| 1738 | unsigned VSize = MVT::getSizeInBits(N0.getValueType()); |
| 1739 | switch (Cond) { |
| 1740 | case ISD::SETUGT: |
| 1741 | case ISD::SETUGE: |
| 1742 | case ISD::SETEQ: return DAG.getConstant(0, VT); |
| 1743 | case ISD::SETULT: |
| 1744 | case ISD::SETULE: |
| 1745 | case ISD::SETNE: return DAG.getConstant(1, VT); |
| 1746 | case ISD::SETGT: |
| 1747 | case ISD::SETGE: |
| 1748 | // True if the sign bit of C1 is set. |
| 1749 | return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT); |
| 1750 | case ISD::SETLT: |
| 1751 | case ISD::SETLE: |
| 1752 | // True if the sign bit of C1 isn't set. |
| 1753 | return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT); |
| 1754 | default: |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | // Otherwise, we can perform the comparison with the low bits. |
| 1760 | switch (Cond) { |
| 1761 | case ISD::SETEQ: |
| 1762 | case ISD::SETNE: |
| 1763 | case ISD::SETUGT: |
| 1764 | case ISD::SETUGE: |
| 1765 | case ISD::SETULT: |
| 1766 | case ISD::SETULE: |
| 1767 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1768 | DAG.getConstant(C1, N0.getOperand(0).getValueType()), |
| 1769 | Cond); |
| 1770 | default: |
| 1771 | break; // todo, be more careful with signed comparisons |
| 1772 | } |
| 1773 | } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1774 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 1775 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); |
| 1776 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 1777 | MVT::ValueType ExtDstTy = N0.getValueType(); |
| 1778 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
| 1779 | |
| 1780 | // If the extended part has any inconsistent bits, it cannot ever |
| 1781 | // compare equal. In other words, they have to be all ones or all |
| 1782 | // zeros. |
| 1783 | uint64_t ExtBits = |
| 1784 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
| 1785 | if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits) |
| 1786 | return DAG.getConstant(Cond == ISD::SETNE, VT); |
| 1787 | |
| 1788 | SDOperand ZextOp; |
| 1789 | MVT::ValueType Op0Ty = N0.getOperand(0).getValueType(); |
| 1790 | if (Op0Ty == ExtSrcTy) { |
| 1791 | ZextOp = N0.getOperand(0); |
| 1792 | } else { |
| 1793 | int64_t Imm = ~0ULL >> (64-ExtSrcTyBits); |
| 1794 | ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0), |
| 1795 | DAG.getConstant(Imm, Op0Ty)); |
| 1796 | } |
| 1797 | WorkList.push_back(ZextOp.Val); |
| 1798 | // Otherwise, make this a use of a zext. |
| 1799 | return DAG.getSetCC(VT, ZextOp, |
| 1800 | DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)), |
| 1801 | ExtDstTy), |
| 1802 | Cond); |
| 1803 | } |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 1804 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1805 | uint64_t MinVal, MaxVal; |
| 1806 | unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0)); |
| 1807 | if (ISD::isSignedIntSetCC(Cond)) { |
| 1808 | MinVal = 1ULL << (OperandBitSize-1); |
| 1809 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 1810 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 1811 | else |
| 1812 | MaxVal = 0; |
| 1813 | } else { |
| 1814 | MinVal = 0; |
| 1815 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 1816 | } |
| 1817 | |
| 1818 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 1819 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 1820 | if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true |
| 1821 | --C1; // X >= C0 --> X > (C0-1) |
| 1822 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 1823 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
| 1824 | } |
| 1825 | |
| 1826 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 1827 | if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true |
| 1828 | ++C1; // X <= C0 --> X < (C0+1) |
| 1829 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 1830 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
| 1831 | } |
| 1832 | |
| 1833 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) |
| 1834 | return DAG.getConstant(0, VT); // X < MIN --> false |
| 1835 | |
| 1836 | // Canonicalize setgt X, Min --> setne X, Min |
| 1837 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) |
| 1838 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
| 1839 | |
| 1840 | // If we have setult X, 1, turn it into seteq X, 0 |
| 1841 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) |
| 1842 | return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()), |
| 1843 | ISD::SETEQ); |
| 1844 | // If we have setugt X, Max-1, turn it into seteq X, Max |
| 1845 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) |
| 1846 | return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()), |
| 1847 | ISD::SETEQ); |
| 1848 | |
| 1849 | // If we have "setcc X, C0", check to see if we can shrink the immediate |
| 1850 | // by changing cc. |
| 1851 | |
| 1852 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 1853 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 1854 | C1 == (~0ULL >> (65-OperandBitSize))) |
| 1855 | return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()), |
| 1856 | ISD::SETLT); |
| 1857 | |
| 1858 | // FIXME: Implement the rest of these. |
| 1859 | |
| 1860 | // Fold bit comparisons when we can. |
| 1861 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 1862 | VT == N0.getValueType() && N0.getOpcode() == ISD::AND) |
| 1863 | if (ConstantSDNode *AndRHS = |
| 1864 | dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 1865 | if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 1866 | // Perform the xform if the AND RHS is a single bit. |
| 1867 | if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { |
| 1868 | return DAG.getNode(ISD::SRL, VT, N0, |
| 1869 | DAG.getConstant(Log2_64(AndRHS->getValue()), |
| 1870 | TLI.getShiftAmountTy())); |
| 1871 | } |
| 1872 | } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) { |
| 1873 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 1874 | // Perform the xform if C1 is a single bit. |
| 1875 | if ((C1 & (C1-1)) == 0) { |
| 1876 | return DAG.getNode(ISD::SRL, VT, N0, |
| 1877 | DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy())); |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | } else if (isa<ConstantSDNode>(N0.Val)) { |
| 1883 | // Ensure that the constant occurs on the RHS. |
| 1884 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 1885 | } |
| 1886 | |
| 1887 | if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val)) |
| 1888 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) { |
| 1889 | double C0 = N0C->getValue(), C1 = N1C->getValue(); |
| 1890 | |
| 1891 | switch (Cond) { |
| 1892 | default: break; // FIXME: Implement the rest of these! |
| 1893 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 1894 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 1895 | case ISD::SETLT: return DAG.getConstant(C0 < C1, VT); |
| 1896 | case ISD::SETGT: return DAG.getConstant(C0 > C1, VT); |
| 1897 | case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT); |
| 1898 | case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT); |
| 1899 | } |
| 1900 | } else { |
| 1901 | // Ensure that the constant occurs on the RHS. |
| 1902 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 1903 | } |
| 1904 | |
| 1905 | if (N0 == N1) { |
| 1906 | // We can always fold X == Y for integer setcc's. |
| 1907 | if (MVT::isInteger(N0.getValueType())) |
| 1908 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 1909 | unsigned UOF = ISD::getUnorderedFlavor(Cond); |
| 1910 | if (UOF == 2) // FP operators that are undefined on NaNs. |
| 1911 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 1912 | if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) |
| 1913 | return DAG.getConstant(UOF, VT); |
| 1914 | // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO |
| 1915 | // if it is not already. |
| 1916 | ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO; |
| 1917 | if (NewCond != Cond) |
| 1918 | return DAG.getSetCC(VT, N0, N1, NewCond); |
| 1919 | } |
| 1920 | |
| 1921 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 1922 | MVT::isInteger(N0.getValueType())) { |
| 1923 | if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || |
| 1924 | N0.getOpcode() == ISD::XOR) { |
| 1925 | // Simplify (X+Y) == (X+Z) --> Y == Z |
| 1926 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1927 | if (N0.getOperand(0) == N1.getOperand(0)) |
| 1928 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond); |
| 1929 | if (N0.getOperand(1) == N1.getOperand(1)) |
| 1930 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond); |
| 1931 | if (isCommutativeBinOp(N0.getOpcode())) { |
| 1932 | // If X op Y == Y op X, try other combinations. |
| 1933 | if (N0.getOperand(0) == N1.getOperand(1)) |
| 1934 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond); |
| 1935 | if (N0.getOperand(1) == N1.getOperand(0)) |
| 1936 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond); |
| 1937 | } |
| 1938 | } |
| 1939 | |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 1940 | // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes. |
| 1941 | if (N0.getOpcode() == ISD::XOR) |
| 1942 | if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
| 1943 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) { |
| 1944 | // If we know that all of the inverted bits are zero, don't bother |
| 1945 | // performing the inversion. |
| 1946 | if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI)) |
| 1947 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1948 | DAG.getConstant(XORC->getValue()^RHSC->getValue(), |
| 1949 | N0.getValueType()), Cond); |
| 1950 | } |
| 1951 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1952 | // Simplify (X+Z) == X --> Z == 0 |
| 1953 | if (N0.getOperand(0) == N1) |
| 1954 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 1955 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 1956 | if (N0.getOperand(1) == N1) { |
| 1957 | if (isCommutativeBinOp(N0.getOpcode())) |
| 1958 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1959 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 1960 | else { |
| 1961 | assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 1962 | // (Z-X) == X --> Z == X<<1 |
| 1963 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), |
| 1964 | N1, |
| 1965 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
| 1966 | WorkList.push_back(SH.Val); |
| 1967 | return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond); |
| 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || |
| 1973 | N1.getOpcode() == ISD::XOR) { |
| 1974 | // Simplify X == (X+Z) --> Z == 0 |
| 1975 | if (N1.getOperand(0) == N0) { |
| 1976 | return DAG.getSetCC(VT, N1.getOperand(1), |
| 1977 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 1978 | } else if (N1.getOperand(1) == N0) { |
| 1979 | if (isCommutativeBinOp(N1.getOpcode())) { |
| 1980 | return DAG.getSetCC(VT, N1.getOperand(0), |
| 1981 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 1982 | } else { |
| 1983 | assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 1984 | // X == (Z-X) --> X<<1 == Z |
| 1985 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0, |
| 1986 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
| 1987 | WorkList.push_back(SH.Val); |
| 1988 | return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond); |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | // Fold away ALL boolean setcc's. |
| 1995 | SDOperand Temp; |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1996 | if (N0.getValueType() == MVT::i1 && foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1997 | switch (Cond) { |
| 1998 | default: assert(0 && "Unknown integer setcc!"); |
| 1999 | case ISD::SETEQ: // X == Y -> (X^Y)^1 |
| 2000 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 2001 | N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1)); |
| 2002 | WorkList.push_back(Temp.Val); |
| 2003 | break; |
| 2004 | case ISD::SETNE: // X != Y --> (X^Y) |
| 2005 | N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 2006 | break; |
| 2007 | case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y |
| 2008 | case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y |
| 2009 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 2010 | N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp); |
| 2011 | WorkList.push_back(Temp.Val); |
| 2012 | break; |
| 2013 | case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X |
| 2014 | case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X |
| 2015 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 2016 | N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp); |
| 2017 | WorkList.push_back(Temp.Val); |
| 2018 | break; |
| 2019 | case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y |
| 2020 | case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y |
| 2021 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 2022 | N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp); |
| 2023 | WorkList.push_back(Temp.Val); |
| 2024 | break; |
| 2025 | case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X |
| 2026 | case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X |
| 2027 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 2028 | N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp); |
| 2029 | break; |
| 2030 | } |
| 2031 | if (VT != MVT::i1) { |
| 2032 | WorkList.push_back(N0.Val); |
| 2033 | // FIXME: If running after legalize, we probably can't do this. |
| 2034 | N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
| 2035 | } |
| 2036 | return N0; |
| 2037 | } |
| 2038 | |
| 2039 | // Could not fold it. |
| 2040 | return SDOperand(); |
| 2041 | } |
| 2042 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2043 | // SelectionDAG::Combine - This is the entry point for the file. |
| 2044 | // |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 2045 | void SelectionDAG::Combine(bool RunningAfterLegalize) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2046 | /// run - This is the main entry point to this class. |
| 2047 | /// |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 2048 | DAGCombiner(*this).Run(RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2049 | } |