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