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