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