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