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 | // |
| 23 | // FIXME: mul (x, const) -> shifts + adds |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 24 | // FIXME: undef values |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 25 | // FIXME: zero extend when top bits are 0 -> drop it ? |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 26 | // FIXME: make truncate see through SIGN_EXTEND and AND |
| 27 | // FIXME: sext_in_reg(setcc) on targets that return zero or one, and where |
| 28 | // EVT != MVT::i1 can drop the sext. |
| 29 | // FIXME: (or x, c) -> c iff maskedValueIsZero(x, ~c) |
| 30 | // FIXME: MaskedValueIsZero can see through SRL, so it should be sufficient to: |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 31 | //if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 32 | // return DAG.getConstant(0, VT).Val; |
| 33 | // FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 34 | // FIXME: verify that getNode can't return extends with an operand whose type |
| 35 | // is >= to that of the extend. |
| 36 | // FIXME: divide by zero is currently left unfolded. do we want to turn this |
| 37 | // into an undef? |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 38 | // |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | #define DEBUG_TYPE "dagcombine" |
| 42 | #include "llvm/ADT/Statistic.h" |
| 43 | #include "llvm/CodeGen/SelectionDAG.h" |
| 44 | #include "llvm/Support/MathExtras.h" |
| 45 | #include "llvm/Target/TargetLowering.h" |
| 46 | #include <cmath> |
| 47 | using namespace llvm; |
| 48 | |
| 49 | namespace { |
| 50 | Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined"); |
| 51 | |
| 52 | class DAGCombiner { |
| 53 | SelectionDAG &DAG; |
| 54 | TargetLowering &TLI; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 55 | bool AfterLegalize; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 56 | |
| 57 | // Worklist of all of the nodes that need to be simplified. |
| 58 | std::vector<SDNode*> WorkList; |
| 59 | |
| 60 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 61 | /// the instruction to the work lists because they might get more simplified |
| 62 | /// now. |
| 63 | /// |
| 64 | void AddUsersToWorkList(SDNode *N) { |
| 65 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 66 | UI != UE; ++UI) |
| 67 | WorkList.push_back(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 71 | void removeFromWorkList(SDNode *N) { |
| 72 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N), |
| 73 | WorkList.end()); |
| 74 | } |
| 75 | |
| 76 | /// visit - call the node-specific routine that knows how to fold each |
| 77 | /// particular type of node. |
| 78 | SDNode *visit(SDNode *N); |
| 79 | |
| 80 | // Visitation implementation - Implement dag node combining for different |
| 81 | // node types. The semantics are as follows: |
| 82 | // Return Value: |
| 83 | // null - No change was made |
| 84 | // otherwise - Node N should be replaced by the returned node. |
| 85 | // |
| 86 | SDNode *visitTokenFactor(SDNode *N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 87 | SDNode *visitADD(SDNode *N); |
| 88 | SDNode *visitSUB(SDNode *N); |
| 89 | SDNode *visitMUL(SDNode *N); |
| 90 | SDNode *visitSDIV(SDNode *N); |
| 91 | SDNode *visitUDIV(SDNode *N); |
| 92 | SDNode *visitSREM(SDNode *N); |
| 93 | SDNode *visitUREM(SDNode *N); |
| 94 | SDNode *visitMULHU(SDNode *N); |
| 95 | SDNode *visitMULHS(SDNode *N); |
| 96 | SDNode *visitAND(SDNode *N); |
| 97 | SDNode *visitOR(SDNode *N); |
| 98 | SDNode *visitXOR(SDNode *N); |
| 99 | SDNode *visitSHL(SDNode *N); |
| 100 | SDNode *visitSRA(SDNode *N); |
| 101 | SDNode *visitSRL(SDNode *N); |
| 102 | SDNode *visitCTLZ(SDNode *N); |
| 103 | SDNode *visitCTTZ(SDNode *N); |
| 104 | SDNode *visitCTPOP(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 105 | // select |
| 106 | // select_cc |
| 107 | // setcc |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 108 | SDNode *visitSIGN_EXTEND(SDNode *N); |
| 109 | SDNode *visitZERO_EXTEND(SDNode *N); |
| 110 | SDNode *visitSIGN_EXTEND_INREG(SDNode *N); |
| 111 | SDNode *visitTRUNCATE(SDNode *N); |
| 112 | SDNode *visitSINT_TO_FP(SDNode *N); |
| 113 | SDNode *visitUINT_TO_FP(SDNode *N); |
| 114 | SDNode *visitFP_TO_SINT(SDNode *N); |
| 115 | SDNode *visitFP_TO_UINT(SDNode *N); |
| 116 | SDNode *visitFP_ROUND(SDNode *N); |
| 117 | SDNode *visitFP_ROUND_INREG(SDNode *N); |
| 118 | SDNode *visitFP_EXTEND(SDNode *N); |
| 119 | SDNode *visitFNEG(SDNode *N); |
| 120 | SDNode *visitFABS(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 121 | // brcond |
| 122 | // brcondtwoway |
| 123 | // br_cc |
| 124 | // brtwoway_cc |
| 125 | public: |
| 126 | DAGCombiner(SelectionDAG &D) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 127 | : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {} |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 128 | |
| 129 | /// Run - runs the dag combiner on all nodes in the work list |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 130 | void Run(bool RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 131 | }; |
| 132 | } |
| 133 | |
| 134 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 135 | /// this predicate to simplify operations downstream. V and Mask are known to |
| 136 | /// be the same type. |
| 137 | static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask, |
| 138 | const TargetLowering &TLI) { |
| 139 | unsigned SrcBits; |
| 140 | if (Mask == 0) return true; |
| 141 | |
| 142 | // If we know the result of a setcc has the top bits zero, use this info. |
| 143 | switch (Op.getOpcode()) { |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 144 | case ISD::Constant: |
| 145 | return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0; |
| 146 | case ISD::SETCC: |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 147 | // FIXME: teach this about non ZeroOrOne values, such as 0 or -1 |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 148 | return ((Mask & 1) == 0) && |
| 149 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult; |
| 150 | case ISD::ZEXTLOAD: |
| 151 | SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT()); |
| 152 | return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. |
| 153 | case ISD::ZERO_EXTEND: |
| 154 | SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType()); |
| 155 | return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI); |
| 156 | case ISD::AssertZext: |
| 157 | SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); |
| 158 | return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. |
| 159 | case ISD::AND: |
| 160 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
| 161 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) |
| 162 | return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI); |
| 163 | // FALL THROUGH |
| 164 | case ISD::OR: |
| 165 | case ISD::XOR: |
| 166 | return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) && |
| 167 | MaskedValueIsZero(Op.getOperand(1), Mask, TLI); |
| 168 | case ISD::SELECT: |
| 169 | return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) && |
| 170 | MaskedValueIsZero(Op.getOperand(2), Mask, TLI); |
| 171 | case ISD::SELECT_CC: |
| 172 | return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) && |
| 173 | MaskedValueIsZero(Op.getOperand(3), Mask, TLI); |
| 174 | case ISD::SRL: |
| 175 | // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0 |
| 176 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 177 | uint64_t NewVal = Mask << ShAmt->getValue(); |
| 178 | SrcBits = MVT::getSizeInBits(Op.getValueType()); |
| 179 | if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1; |
| 180 | return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); |
| 181 | } |
| 182 | return false; |
| 183 | case ISD::SHL: |
| 184 | // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0 |
| 185 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 186 | uint64_t NewVal = Mask >> ShAmt->getValue(); |
| 187 | return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); |
| 188 | } |
| 189 | return false; |
| 190 | case ISD::CTTZ: |
| 191 | case ISD::CTLZ: |
| 192 | case ISD::CTPOP: |
| 193 | // Bit counting instructions can not set the high bits of the result |
| 194 | // register. The max number of bits sets depends on the input. |
| 195 | return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0; |
| 196 | |
| 197 | // TODO we could handle some SRA cases here. |
| 198 | default: break; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 199 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 203 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 204 | // 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^] | 205 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
| 206 | // nodes based on the type of node we are checking. This simplifies life a |
| 207 | // bit for the callers. |
| 208 | static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS, |
| 209 | SDOperand &CC) { |
| 210 | if (N.getOpcode() == ISD::SETCC) { |
| 211 | LHS = N.getOperand(0); |
| 212 | RHS = N.getOperand(1); |
| 213 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 214 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 215 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 216 | if (N.getOpcode() == ISD::SELECT_CC && |
| 217 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 218 | N.getOperand(3).getOpcode() == ISD::Constant && |
| 219 | cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 220 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 221 | LHS = N.getOperand(0); |
| 222 | RHS = N.getOperand(1); |
| 223 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 224 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 225 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 229 | // isInvertibleForFree - Return true if there is no cost to emitting the logical |
| 230 | // inverse of this node. |
| 231 | static bool isInvertibleForFree(SDOperand N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 232 | SDOperand N0, N1, N2; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 233 | if (isa<ConstantSDNode>(N.Val)) return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 234 | if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 235 | return true; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | void DAGCombiner::Run(bool RunningAfterLegalize) { |
| 240 | // set the instance variable, so that the various visit routines may use it. |
| 241 | AfterLegalize = RunningAfterLegalize; |
| 242 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 243 | // Add all the dag nodes to the worklist. |
| 244 | WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end()); |
| 245 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 246 | // while the worklist isn't empty, inspect the node on the end of it and |
| 247 | // try and combine it. |
| 248 | while (!WorkList.empty()) { |
| 249 | SDNode *N = WorkList.back(); |
| 250 | WorkList.pop_back(); |
| 251 | |
| 252 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
| 253 | // N is deleted from the DAG, since they too may now be dead. |
| 254 | if (N->use_empty()) { |
| 255 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 256 | WorkList.push_back(N->getOperand(i).Val); |
| 257 | |
| 258 | DAG.DeleteNode(N); |
| 259 | removeFromWorkList(N); |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | if (SDNode *Result = visit(N)) { |
| 264 | ++NodesCombined; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 265 | // If we get back the same node we passed in, rather than a new node or |
| 266 | // zero, we know that the node must have defined multiple values and |
| 267 | // CombineTo was used. Since CombineTo takes care of the worklist |
| 268 | // mechanics for us, we have no work to do in this case. |
| 269 | if (Result != N) { |
| 270 | DAG.ReplaceAllUsesWith(N, Result); |
| 271 | |
| 272 | // Push the new node and any users onto the worklist |
| 273 | WorkList.push_back(Result); |
| 274 | AddUsersToWorkList(Result); |
| 275 | |
| 276 | // Nodes can end up on the worklist more than once. Make sure we do |
| 277 | // not process a node that has been replaced. |
| 278 | removeFromWorkList(N); |
| 279 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | SDNode *DAGCombiner::visit(SDNode *N) { |
| 285 | switch(N->getOpcode()) { |
| 286 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 287 | case ISD::TokenFactor: return visitTokenFactor(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 288 | case ISD::ADD: return visitADD(N); |
| 289 | case ISD::SUB: return visitSUB(N); |
| 290 | case ISD::MUL: return visitMUL(N); |
| 291 | case ISD::SDIV: return visitSDIV(N); |
| 292 | case ISD::UDIV: return visitUDIV(N); |
| 293 | case ISD::SREM: return visitSREM(N); |
| 294 | case ISD::UREM: return visitUREM(N); |
| 295 | case ISD::MULHU: return visitMULHU(N); |
| 296 | case ISD::MULHS: return visitMULHS(N); |
| 297 | case ISD::AND: return visitAND(N); |
| 298 | case ISD::OR: return visitOR(N); |
| 299 | case ISD::XOR: return visitXOR(N); |
| 300 | case ISD::SHL: return visitSHL(N); |
| 301 | case ISD::SRA: return visitSRA(N); |
| 302 | case ISD::SRL: return visitSRL(N); |
| 303 | case ISD::CTLZ: return visitCTLZ(N); |
| 304 | case ISD::CTTZ: return visitCTTZ(N); |
| 305 | case ISD::CTPOP: return visitCTPOP(N); |
| 306 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 307 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
| 308 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 309 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
| 310 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 311 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 312 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 313 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 314 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 315 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 316 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 317 | case ISD::FNEG: return visitFNEG(N); |
| 318 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | SDNode *DAGCombiner::visitTokenFactor(SDNode *N) { |
| 324 | // If the token factor only has one operand, fold TF(x) -> x |
| 325 | if (N->getNumOperands() == 1) |
| 326 | return N->getOperand(0).Val; |
| 327 | |
| 328 | // If the token factor has two operands and one is the entry token, replace |
| 329 | // the token factor with the other operand. |
| 330 | if (N->getNumOperands() == 2) { |
| 331 | if (N->getOperand(0).getOpcode() == ISD::EntryToken) |
| 332 | return N->getOperand(1).Val; |
| 333 | if (N->getOperand(1).getOpcode() == ISD::EntryToken) |
| 334 | return N->getOperand(0).Val; |
| 335 | } |
| 336 | return 0; |
| 337 | } |
| 338 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 339 | SDNode *DAGCombiner::visitADD(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 340 | SDOperand N0 = N->getOperand(0); |
| 341 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 342 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 343 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 344 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 345 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 346 | |
| 347 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 348 | if (N0C && N1C) |
| 349 | return DAG.getConstant(N0C->getValue() + N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 350 | N->getValueType(0)).Val; |
| 351 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 352 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 353 | return N0.Val; |
| 354 | // fold floating point (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 355 | if (N0CFP && N1CFP) |
| 356 | return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 357 | N->getValueType(0)).Val; |
| 358 | // fold (A + (-B)) -> A-B |
| 359 | if (N1.getOpcode() == ISD::FNEG) |
| 360 | return DAG.getNode(ISD::SUB, N->getValueType(0), N0, N1.getOperand(0)).Val; |
| 361 | // fold ((-A) + B) -> B-A |
| 362 | if (N0.getOpcode() == ISD::FNEG) |
| 363 | return DAG.getNode(ISD::SUB, N->getValueType(0), N1, N0.getOperand(0)).Val; |
| 364 | // fold ((0-A) + B) -> B-A |
| 365 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 366 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
| 367 | return DAG.getNode(ISD::SUB, N->getValueType(0), N1, N0.getOperand(1)).Val; |
| 368 | // fold (A + (0-B)) -> A-B |
| 369 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 370 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
| 371 | return DAG.getNode(ISD::SUB, N->getValueType(0), N0, N1.getOperand(1)).Val; |
| 372 | // fold (A+(B-A)) -> B for non-fp types |
| 373 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) && |
| 374 | !MVT::isFloatingPoint(N1.getValueType())) |
| 375 | return N1.getOperand(0).Val; |
| 376 | return 0; |
| 377 | } |
| 378 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 379 | SDNode *DAGCombiner::visitSUB(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 380 | SDOperand N0 = N->getOperand(0); |
| 381 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 382 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 383 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 384 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val); |
| 385 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 386 | |
| 387 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 388 | if (N0C && N1C) |
| 389 | return DAG.getConstant(N0C->getValue() - N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 390 | N->getValueType(0)).Val; |
| 391 | // fold (sub x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 392 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 393 | return N0.Val; |
| 394 | // fold floating point (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 395 | if (N0CFP && N1CFP) |
| 396 | return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 397 | N->getValueType(0)).Val; |
| 398 | // fold (A+B)-A -> B |
| 399 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 && |
| 400 | !MVT::isFloatingPoint(N1.getValueType())) |
| 401 | return N0.getOperand(1).Val; |
| 402 | // fold (A+B)-B -> A |
| 403 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 && |
| 404 | !MVT::isFloatingPoint(N1.getValueType())) |
| 405 | return N0.getOperand(0).Val; |
| 406 | // fold (A-(-B)) -> A+B |
| 407 | if (N1.getOpcode() == ISD::FNEG) |
| 408 | return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0)).Val; |
| 409 | return 0; |
| 410 | } |
| 411 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 412 | SDNode *DAGCombiner::visitMUL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 413 | SDOperand N0 = N->getOperand(0); |
| 414 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 415 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 416 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 417 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 418 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 419 | |
| 420 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 421 | if (N0C && N1C) |
| 422 | return DAG.getConstant(N0C->getValue() * N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 423 | N->getValueType(0)).Val; |
| 424 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 425 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 426 | return N1.Val; |
| 427 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 428 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 429 | return DAG.getNode(ISD::SUB, N->getValueType(0), |
| 430 | DAG.getConstant(0, N->getValueType(0)), N0).Val; |
| 431 | // fold (mul x, (1 << c)) -> x << c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 432 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 433 | return DAG.getNode(ISD::SHL, N->getValueType(0), N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 434 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 435 | TLI.getShiftAmountTy())).Val; |
| 436 | // fold floating point (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 437 | if (N0CFP && N1CFP) |
| 438 | return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 439 | N->getValueType(0)).Val; |
| 440 | return 0; |
| 441 | } |
| 442 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 443 | SDNode *DAGCombiner::visitSDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 444 | SDOperand N0 = N->getOperand(0); |
| 445 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 446 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 447 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 448 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val); |
| 449 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 450 | |
| 451 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 452 | if (N0C && N1C && !N1C->isNullValue()) |
| 453 | return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 454 | N->getValueType(0)).Val; |
| 455 | // fold floating point (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 456 | if (N0CFP && N1CFP) |
| 457 | return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 458 | N->getValueType(0)).Val; |
| 459 | return 0; |
| 460 | } |
| 461 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 462 | SDNode *DAGCombiner::visitUDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 463 | SDOperand N0 = N->getOperand(0); |
| 464 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 465 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 466 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 467 | |
| 468 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 469 | if (N0C && N1C && !N1C->isNullValue()) |
| 470 | return DAG.getConstant(N0C->getValue() / N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 471 | N->getValueType(0)).Val; |
| 472 | // fold (udiv x, (1 << c)) -> x >>u c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 473 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 474 | return DAG.getNode(ISD::SRL, N->getValueType(0), N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 475 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 476 | TLI.getShiftAmountTy())).Val; |
| 477 | return 0; |
| 478 | } |
| 479 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 480 | SDNode *DAGCombiner::visitSREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 481 | SDOperand N0 = N->getOperand(0); |
| 482 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 483 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 484 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 485 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 486 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 487 | |
| 488 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 489 | if (N0C && N1C && !N1C->isNullValue()) |
| 490 | return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 491 | N->getValueType(0)).Val; |
| 492 | // fold floating point (srem c1, c2) -> fmod(c1, c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 493 | if (N0CFP && N1CFP) |
| 494 | return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 495 | N->getValueType(0)).Val; |
| 496 | return 0; |
| 497 | } |
| 498 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 499 | SDNode *DAGCombiner::visitUREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 500 | SDOperand N0 = N->getOperand(0); |
| 501 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 502 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 503 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 504 | |
| 505 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 506 | if (N0C && N1C && !N1C->isNullValue()) |
| 507 | return DAG.getConstant(N0C->getValue() % N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 508 | N->getValueType(0)).Val; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 509 | // FIXME: c2 power of 2 -> mask? |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 513 | SDNode *DAGCombiner::visitMULHS(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 514 | SDOperand N0 = N->getOperand(0); |
| 515 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 516 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 517 | |
| 518 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 519 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 520 | return N1.Val; |
| 521 | |
| 522 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 523 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 524 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0, |
| 525 | DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1, |
| 526 | TLI.getShiftAmountTy())).Val; |
| 527 | return 0; |
| 528 | } |
| 529 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 530 | SDNode *DAGCombiner::visitMULHU(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 531 | SDOperand N0 = N->getOperand(0); |
| 532 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 533 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 534 | |
| 535 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 536 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 537 | return N1.Val; |
| 538 | |
| 539 | // fold (mulhu x, 1) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 540 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 541 | return DAG.getConstant(0, N0.getValueType()).Val; |
| 542 | return 0; |
| 543 | } |
| 544 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 545 | SDNode *DAGCombiner::visitAND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 546 | SDOperand N0 = N->getOperand(0); |
| 547 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 548 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 549 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 550 | MVT::ValueType VT = N1.getValueType(); |
| 551 | |
| 552 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 553 | if (N0C && N1C) |
| 554 | return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 555 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 556 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 557 | return N0.Val; |
| 558 | // fold (and x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 559 | if (N1C && MaskedValueIsZero(N0, N1C->getValue(), TLI)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 560 | return DAG.getConstant(0, VT).Val; |
| 561 | // fold (and x, mask containing x) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 562 | if (N1C) { |
| 563 | uint64_t NotC2 = ~N1C->getValue(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 564 | NotC2 &= ~0ULL >> (64-MVT::getSizeInBits(VT)); |
| 565 | if (MaskedValueIsZero(N0, NotC2, TLI)) |
| 566 | return N0.Val; |
| 567 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 568 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 569 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) { |
| 570 | unsigned ExtendBits = |
| 571 | MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT()); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 572 | if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 573 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1).Val; |
| 574 | } |
| 575 | // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF |
| 576 | if (N0.getOpcode() == ISD::OR) |
| 577 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 578 | if ((ORI->getValue() & N1C->getValue()) == N1C->getValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 579 | return N1.Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 583 | SDNode *DAGCombiner::visitOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 584 | SDOperand N0 = N->getOperand(0); |
| 585 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 586 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 587 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 588 | |
| 589 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 590 | if (N0C && N1C) |
| 591 | return DAG.getConstant(N0C->getValue() | N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 592 | N->getValueType(0)).Val; |
| 593 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 594 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 595 | return N0.Val; |
| 596 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 597 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 598 | return N1.Val; |
| 599 | return 0; |
| 600 | } |
| 601 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 602 | SDNode *DAGCombiner::visitXOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 603 | SDOperand N0 = N->getOperand(0); |
| 604 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 605 | SDOperand LHS, RHS, CC; |
| 606 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 607 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 608 | MVT::ValueType VT = N0.getValueType(); |
| 609 | |
| 610 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 611 | if (N0C && N1C) |
| 612 | return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 613 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 614 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 615 | return N0.Val; |
| 616 | // fold !(x cc y) -> (x !cc y) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 617 | if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
| 618 | bool isInt = MVT::isInteger(LHS.getValueType()); |
| 619 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 620 | isInt); |
| 621 | if (N0.getOpcode() == ISD::SETCC) |
| 622 | return DAG.getSetCC(VT, LHS, RHS, NotCC).Val; |
| 623 | if (N0.getOpcode() == ISD::SELECT_CC) |
| 624 | return DAG.getSelectCC(LHS, RHS, N0.getOperand(2), N0.getOperand(3), |
| 625 | NotCC).Val; |
| 626 | assert(0 && "Unhandled SetCC Equivalent!"); |
| 627 | abort(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 628 | } |
| 629 | // fold !(x or y) -> (!x and !y) iff x or y are freely invertible |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 630 | if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::OR) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 631 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
| 632 | if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) { |
| 633 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 634 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
| 635 | return DAG.getNode(ISD::AND, VT, LHS, RHS).Val; |
| 636 | } |
| 637 | } |
| 638 | // fold !(x and y) -> (!x or !y) iff x or y are freely invertible |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 639 | if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::AND) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 640 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
| 641 | if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) { |
| 642 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 643 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
| 644 | return DAG.getNode(ISD::OR, VT, LHS, RHS).Val; |
| 645 | } |
| 646 | } |
| 647 | return 0; |
| 648 | } |
| 649 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 650 | SDNode *DAGCombiner::visitSHL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 651 | SDOperand N0 = N->getOperand(0); |
| 652 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 653 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 654 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 655 | MVT::ValueType VT = N0.getValueType(); |
| 656 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 657 | |
| 658 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 659 | if (N0C && N1C) |
| 660 | return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 661 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 662 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 663 | return N0.Val; |
| 664 | // fold (shl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 665 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 666 | return DAG.getNode(ISD::UNDEF, VT).Val; |
| 667 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 668 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 669 | return N0.Val; |
| 670 | // if (shl x, c) is known to be zero, return 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 671 | if (N1C && MaskedValueIsZero(N0,(~0ULL >> (64-OpSizeInBits))>>N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 672 | TLI)) |
| 673 | return DAG.getConstant(0, VT).Val; |
| 674 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 675 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 676 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 677 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 678 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 679 | if (c1 + c2 > OpSizeInBits) |
| 680 | return DAG.getConstant(0, VT).Val; |
| 681 | return DAG.getNode(ISD::SHL, VT, N0.getOperand(0), |
| 682 | DAG.getConstant(c1 + c2, N1.getValueType())).Val; |
| 683 | } |
| 684 | // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or |
| 685 | // (srl (and x, -1 << c1), c1-c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 686 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 687 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 688 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 689 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 690 | SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 691 | DAG.getConstant(~0ULL << c1, VT)); |
| 692 | if (c2 > c1) |
| 693 | return DAG.getNode(ISD::SHL, VT, Mask, |
| 694 | DAG.getConstant(c2-c1, N1.getValueType())).Val; |
| 695 | else |
| 696 | return DAG.getNode(ISD::SRL, VT, Mask, |
| 697 | DAG.getConstant(c1-c2, N1.getValueType())).Val; |
| 698 | } |
| 699 | // fold (shl (sra x, c1), c1) -> (and x, -1 << c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 700 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 701 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 702 | DAG.getConstant(~0ULL << N1C->getValue(), VT)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 706 | SDNode *DAGCombiner::visitSRA(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 707 | SDOperand N0 = N->getOperand(0); |
| 708 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 709 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 710 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 711 | MVT::ValueType VT = N0.getValueType(); |
| 712 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 713 | |
| 714 | // fold (sra c1, c2) -> c1>>c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 715 | if (N0C && N1C) |
| 716 | return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 717 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 718 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 719 | return N0.Val; |
| 720 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 721 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 722 | return N0.Val; |
| 723 | // fold (sra x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 724 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 725 | return DAG.getNode(ISD::UNDEF, VT).Val; |
| 726 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 727 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 728 | return N0.Val; |
| 729 | // 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^] | 730 | if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 731 | return DAG.getNode(ISD::SRL, VT, N0, N1).Val; |
| 732 | return 0; |
| 733 | } |
| 734 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 735 | SDNode *DAGCombiner::visitSRL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 736 | SDOperand N0 = N->getOperand(0); |
| 737 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 738 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 739 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 740 | MVT::ValueType VT = N0.getValueType(); |
| 741 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 742 | |
| 743 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 744 | if (N0C && N1C) |
| 745 | return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 746 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 747 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 748 | return N0.Val; |
| 749 | // fold (srl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 750 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 751 | return DAG.getNode(ISD::UNDEF, VT).Val; |
| 752 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 753 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 754 | return N0.Val; |
| 755 | // if (srl x, c) is known to be zero, return 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 756 | if (N1C && MaskedValueIsZero(N0,(~0ULL >> (64-OpSizeInBits))<<N1C->getValue(), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 757 | TLI)) |
| 758 | return DAG.getConstant(0, VT).Val; |
| 759 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 760 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 761 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 762 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 763 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 764 | if (c1 + c2 > OpSizeInBits) |
| 765 | return DAG.getConstant(0, VT).Val; |
| 766 | return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), |
| 767 | DAG.getConstant(c1 + c2, N1.getValueType())).Val; |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 772 | SDNode *DAGCombiner::visitCTLZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 773 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 774 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 775 | |
| 776 | // fold (ctlz c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 777 | if (N0C) |
| 778 | return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 779 | N0.getValueType()).Val; |
| 780 | return 0; |
| 781 | } |
| 782 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 783 | SDNode *DAGCombiner::visitCTTZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 784 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 785 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 786 | |
| 787 | // fold (cttz c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 788 | if (N0C) |
| 789 | return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 790 | N0.getValueType()).Val; |
| 791 | return 0; |
| 792 | } |
| 793 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 794 | SDNode *DAGCombiner::visitCTPOP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 795 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 796 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 797 | |
| 798 | // fold (ctpop c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 799 | if (N0C) |
| 800 | return DAG.getConstant(CountPopulation_64(N0C->getValue()), |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 801 | N0.getValueType()).Val; |
| 802 | return 0; |
| 803 | } |
| 804 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 805 | SDNode *DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 806 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 807 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 808 | MVT::ValueType VT = N->getValueType(0); |
| 809 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 810 | // fold (sext c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 811 | if (N0C) |
| 812 | return DAG.getConstant(N0C->getSignExtended(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 813 | // fold (sext (sext x)) -> (sext x) |
| 814 | if (N0.getOpcode() == ISD::SIGN_EXTEND) |
| 815 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0)).Val; |
| 816 | return 0; |
| 817 | } |
| 818 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 819 | SDNode *DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 820 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 821 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 822 | MVT::ValueType VT = N->getValueType(0); |
| 823 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 824 | // fold (zext c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 825 | if (N0C) |
| 826 | return DAG.getConstant(N0C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 827 | // fold (zext (zext x)) -> (zext x) |
| 828 | if (N0.getOpcode() == ISD::ZERO_EXTEND) |
| 829 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0)).Val; |
| 830 | return 0; |
| 831 | } |
| 832 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 833 | SDNode *DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 834 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 835 | SDOperand N1 = N->getOperand(1); |
| 836 | SDOperand LHS, RHS, CC; |
| 837 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 838 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 839 | MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 840 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 841 | // fold (sext_in_reg c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 842 | if (N0C) { |
| 843 | SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 844 | return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate).Val; |
| 845 | } |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 846 | // 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] | 847 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 848 | cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 849 | return N0.Val; |
| 850 | } |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 851 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 852 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 853 | EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) { |
| 854 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1).Val; |
| 855 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 856 | // fold (sext_in_reg (assert_sext x)) -> (assert_sext x) |
| 857 | if (N0.getOpcode() == ISD::AssertSext && |
| 858 | cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) { |
| 859 | return N0.Val; |
| 860 | } |
| 861 | // fold (sext_in_reg (sextload x)) -> (sextload x) |
| 862 | if (N0.getOpcode() == ISD::SEXTLOAD && |
| 863 | cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) { |
| 864 | return N0.Val; |
| 865 | } |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 866 | // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 867 | // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 868 | if (N0.getOpcode() == ISD::SETCC && |
| 869 | TLI.getSetCCResultContents() == |
| 870 | TargetLowering::ZeroOrNegativeOneSetCCResult) |
| 871 | return N0.Val; |
| 872 | // FIXME: this code is currently just ported over from SelectionDAG.cpp |
| 873 | // we probably actually want to handle this in two pieces. Rather than |
| 874 | // checking all the top bits for zero, just check the sign bit here and turn |
| 875 | // it into a zero extend inreg (AND with constant). |
| 876 | // then, let the code for AND figure out if the mask is superfluous rather |
| 877 | // than doing so here. |
| 878 | if (N0.getOpcode() == ISD::AND && |
| 879 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 880 | uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 881 | unsigned NumBits = MVT::getSizeInBits(EVT); |
| 882 | if ((Mask & (~0ULL << (NumBits-1))) == 0) |
| 883 | return N0.Val; |
| 884 | } |
| 885 | return 0; |
| 886 | } |
| 887 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 888 | SDNode *DAGCombiner::visitTRUNCATE(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 889 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 890 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 891 | MVT::ValueType VT = N->getValueType(0); |
| 892 | |
| 893 | // noop truncate |
| 894 | if (N0.getValueType() == N->getValueType(0)) |
| 895 | return N0.Val; |
| 896 | // fold (truncate c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 897 | if (N0C) |
| 898 | return DAG.getConstant(N0C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 899 | // fold (truncate (truncate x)) -> (truncate x) |
| 900 | if (N0.getOpcode() == ISD::TRUNCATE) |
| 901 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)).Val; |
| 902 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
| 903 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){ |
| 904 | if (N0.getValueType() < VT) |
| 905 | // if the source is smaller than the dest, we still need an extend |
| 906 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)).Val; |
| 907 | else if (N0.getValueType() > VT) |
| 908 | // if the source is larger than the dest, than we just need the truncate |
| 909 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)).Val; |
| 910 | else |
| 911 | // if the source and dest are the same type, we can drop both the extend |
| 912 | // and the truncate |
| 913 | return N0.getOperand(0).Val; |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 918 | SDNode *DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 919 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 920 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 921 | MVT::ValueType VT = N->getValueType(0); |
| 922 | |
| 923 | // fold (sint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 924 | if (N0C) |
| 925 | return DAG.getConstantFP(N0C->getSignExtended(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 929 | SDNode *DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 930 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 931 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 932 | MVT::ValueType VT = N->getValueType(0); |
| 933 | |
| 934 | // fold (uint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 935 | if (N0C) |
| 936 | return DAG.getConstantFP(N0C->getValue(), VT).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 940 | SDNode *DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
| 941 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 942 | |
| 943 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 944 | if (N0CFP) |
| 945 | return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 949 | SDNode *DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
| 950 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 951 | |
| 952 | // fold (fp_to_uint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 953 | if (N0CFP) |
| 954 | return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 958 | SDNode *DAGCombiner::visitFP_ROUND(SDNode *N) { |
| 959 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 960 | |
| 961 | // fold (fp_round c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 962 | if (N0CFP) |
| 963 | return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 967 | SDNode *DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 968 | SDOperand N0 = N->getOperand(0); |
| 969 | MVT::ValueType VT = N->getValueType(0); |
| 970 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 971 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 972 | |
| 973 | // noop fp_round_inreg |
| 974 | if (EVT == VT) |
| 975 | return N0.Val; |
| 976 | // fold (fp_round_inreg c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 977 | if (N0CFP) { |
| 978 | SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 979 | return DAG.getNode(ISD::FP_EXTEND, VT, Round).Val; |
| 980 | } |
| 981 | return 0; |
| 982 | } |
| 983 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 984 | SDNode *DAGCombiner::visitFP_EXTEND(SDNode *N) { |
| 985 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 986 | |
| 987 | // fold (fp_extend c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 988 | if (N0CFP) |
| 989 | return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 990 | return 0; |
| 991 | } |
| 992 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 993 | SDNode *DAGCombiner::visitFNEG(SDNode *N) { |
| 994 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 995 | // fold (neg c1) -> -c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 996 | if (N0CFP) |
| 997 | return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 998 | // fold (neg (sub x, y)) -> (sub y, x) |
| 999 | if (N->getOperand(0).getOpcode() == ISD::SUB) |
| 1000 | return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1), |
| 1001 | N->getOperand(0)).Val; |
| 1002 | // fold (neg (neg x)) -> x |
| 1003 | if (N->getOperand(0).getOpcode() == ISD::FNEG) |
| 1004 | return N->getOperand(0).getOperand(0).Val; |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 1008 | SDNode *DAGCombiner::visitFABS(SDNode *N) { |
| 1009 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1010 | // fold (fabs c1) -> fabs(c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame^] | 1011 | if (N0CFP) |
| 1012 | return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0)).Val; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1013 | // fold (fabs (fabs x)) -> (fabs x) |
| 1014 | if (N->getOperand(0).getOpcode() == ISD::FABS) |
| 1015 | return N->getOperand(0).Val; |
| 1016 | // fold (fabs (fneg x)) -> (fabs x) |
| 1017 | if (N->getOperand(0).getOpcode() == ISD::FNEG) |
| 1018 | return DAG.getNode(ISD::FABS, N->getValueType(0), |
| 1019 | N->getOperand(0).getOperand(0)).Val; |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1023 | // SelectionDAG::Combine - This is the entry point for the file. |
| 1024 | // |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1025 | void SelectionDAG::Combine(bool RunningAfterLegalize) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1026 | /// run - This is the main entry point to this class. |
| 1027 | /// |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1028 | DAGCombiner(*this).Run(RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1029 | } |