SelectionDAG.cpp revision bd8625b062c468ccd62b713993a45e8dd40e67a7
1//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This implements the SelectionDAG class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/SelectionDAG.h" 15#include "llvm/Constants.h" 16#include "llvm/GlobalValue.h" 17#include "llvm/Assembly/Writer.h" 18#include "llvm/CodeGen/MachineBasicBlock.h" 19#include "llvm/Support/MathExtras.h" 20#include "llvm/Target/TargetLowering.h" 21#include <iostream> 22#include <set> 23#include <cmath> 24#include <algorithm> 25using namespace llvm; 26 27static bool isCommutativeBinOp(unsigned Opcode) { 28 switch (Opcode) { 29 case ISD::ADD: 30 case ISD::MUL: 31 case ISD::AND: 32 case ISD::OR: 33 case ISD::XOR: return true; 34 default: return false; // FIXME: Need commutative info for user ops! 35 } 36} 37 38static bool isAssociativeBinOp(unsigned Opcode) { 39 switch (Opcode) { 40 case ISD::ADD: 41 case ISD::MUL: 42 case ISD::AND: 43 case ISD::OR: 44 case ISD::XOR: return true; 45 default: return false; // FIXME: Need associative info for user ops! 46 } 47} 48 49// isInvertibleForFree - Return true if there is no cost to emitting the logical 50// inverse of this node. 51static bool isInvertibleForFree(SDOperand N) { 52 if (isa<ConstantSDNode>(N.Val)) return true; 53 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse()) 54 return true; 55 return false; 56} 57 58 59/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 60/// when given the operation for (X op Y). 61ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 62 // To perform this operation, we just need to swap the L and G bits of the 63 // operation. 64 unsigned OldL = (Operation >> 2) & 1; 65 unsigned OldG = (Operation >> 1) & 1; 66 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 67 (OldL << 1) | // New G bit 68 (OldG << 2)); // New L bit. 69} 70 71/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 72/// 'op' is a valid SetCC operation. 73ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 74 unsigned Operation = Op; 75 if (isInteger) 76 Operation ^= 7; // Flip L, G, E bits, but not U. 77 else 78 Operation ^= 15; // Flip all of the condition bits. 79 if (Operation > ISD::SETTRUE2) 80 Operation &= ~8; // Don't let N and U bits get set. 81 return ISD::CondCode(Operation); 82} 83 84 85/// isSignedOp - For an integer comparison, return 1 if the comparison is a 86/// signed operation and 2 if the result is an unsigned comparison. Return zero 87/// if the operation does not depend on the sign of the input (setne and seteq). 88static int isSignedOp(ISD::CondCode Opcode) { 89 switch (Opcode) { 90 default: assert(0 && "Illegal integer setcc operation!"); 91 case ISD::SETEQ: 92 case ISD::SETNE: return 0; 93 case ISD::SETLT: 94 case ISD::SETLE: 95 case ISD::SETGT: 96 case ISD::SETGE: return 1; 97 case ISD::SETULT: 98 case ISD::SETULE: 99 case ISD::SETUGT: 100 case ISD::SETUGE: return 2; 101 } 102} 103 104/// getSetCCOrOperation - Return the result of a logical OR between different 105/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 106/// returns SETCC_INVALID if it is not possible to represent the resultant 107/// comparison. 108ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 109 bool isInteger) { 110 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 111 // Cannot fold a signed integer setcc with an unsigned integer setcc. 112 return ISD::SETCC_INVALID; 113 114 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 115 116 // If the N and U bits get set then the resultant comparison DOES suddenly 117 // care about orderedness, and is true when ordered. 118 if (Op > ISD::SETTRUE2) 119 Op &= ~16; // Clear the N bit. 120 return ISD::CondCode(Op); 121} 122 123/// getSetCCAndOperation - Return the result of a logical AND between different 124/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 125/// function returns zero if it is not possible to represent the resultant 126/// comparison. 127ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 128 bool isInteger) { 129 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 130 // Cannot fold a signed setcc with an unsigned setcc. 131 return ISD::SETCC_INVALID; 132 133 // Combine all of the condition bits. 134 return ISD::CondCode(Op1 & Op2); 135} 136 137const TargetMachine &SelectionDAG::getTarget() const { 138 return TLI.getTargetMachine(); 139} 140 141 142/// RemoveDeadNodes - This method deletes all unreachable nodes in the 143/// SelectionDAG, including nodes (like loads) that have uses of their token 144/// chain but no other uses and no side effect. If a node is passed in as an 145/// argument, it is used as the seed for node deletion. 146void SelectionDAG::RemoveDeadNodes(SDNode *N) { 147 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end()); 148 149 // Create a dummy node (which is not added to allnodes), that adds a reference 150 // to the root node, preventing it from being deleted. 151 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot()); 152 153 DeleteNodeIfDead(N, &AllNodeSet); 154 155 Restart: 156 unsigned NumNodes = AllNodeSet.size(); 157 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end(); 158 I != E; ++I) { 159 // Try to delete this node. 160 DeleteNodeIfDead(*I, &AllNodeSet); 161 162 // If we actually deleted any nodes, do not use invalid iterators in 163 // AllNodeSet. 164 if (AllNodeSet.size() != NumNodes) 165 goto Restart; 166 } 167 168 // Restore AllNodes. 169 if (AllNodes.size() != NumNodes) 170 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end()); 171 172 // If the root changed (e.g. it was a dead load, update the root). 173 setRoot(DummyNode->getOperand(0)); 174 175 // Now that we are done with the dummy node, delete it. 176 DummyNode->getOperand(0).Val->removeUser(DummyNode); 177 delete DummyNode; 178} 179 180void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) { 181 if (!N->use_empty()) 182 return; 183 184 // Okay, we really are going to delete this node. First take this out of the 185 // appropriate CSE map. 186 switch (N->getOpcode()) { 187 case ISD::Constant: 188 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(), 189 N->getValueType(0))); 190 break; 191 case ISD::ConstantFP: { 192 union { 193 double DV; 194 uint64_t IV; 195 }; 196 DV = cast<ConstantFPSDNode>(N)->getValue(); 197 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0))); 198 break; 199 } 200 case ISD::CONDCODE: 201 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 202 "Cond code doesn't exist!"); 203 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 204 break; 205 case ISD::GlobalAddress: 206 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal()); 207 break; 208 case ISD::FrameIndex: 209 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); 210 break; 211 case ISD::ConstantPool: 212 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex()); 213 break; 214 case ISD::BasicBlock: 215 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock()); 216 break; 217 case ISD::ExternalSymbol: 218 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 219 break; 220 case ISD::VALUETYPE: 221 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; 222 break; 223 case ISD::SRCVALUE: { 224 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N); 225 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset())); 226 break; 227 } 228 case ISD::LOAD: 229 Loads.erase(std::make_pair(N->getOperand(1), 230 std::make_pair(N->getOperand(0), 231 N->getValueType(0)))); 232 break; 233 default: 234 if (N->getNumOperands() == 1) 235 UnaryOps.erase(std::make_pair(N->getOpcode(), 236 std::make_pair(N->getOperand(0), 237 N->getValueType(0)))); 238 else if (N->getNumOperands() == 2) 239 BinaryOps.erase(std::make_pair(N->getOpcode(), 240 std::make_pair(N->getOperand(0), 241 N->getOperand(1)))); 242 else if (N->getNumValues() == 1) { 243 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 244 OneResultNodes.erase(std::make_pair(N->getOpcode(), 245 std::make_pair(N->getValueType(0), 246 Ops))); 247 } else { 248 // Remove the node from the ArbitraryNodes map. 249 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 250 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 251 ArbitraryNodes.erase(std::make_pair(N->getOpcode(), 252 std::make_pair(RV, Ops))); 253 } 254 break; 255 } 256 257 // Next, brutally remove the operand list. 258 while (!N->Operands.empty()) { 259 SDNode *O = N->Operands.back().Val; 260 N->Operands.pop_back(); 261 O->removeUser(N); 262 263 // Now that we removed this operand, see if there are no uses of it left. 264 DeleteNodeIfDead(O, NodeSet); 265 } 266 267 // Remove the node from the nodes set and delete it. 268 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet; 269 AllNodeSet.erase(N); 270 271 // Now that the node is gone, check to see if any of the operands of this node 272 // are dead now. 273 delete N; 274} 275 276 277SelectionDAG::~SelectionDAG() { 278 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i) 279 delete AllNodes[i]; 280} 281 282SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { 283 if (Op.getValueType() == VT) return Op; 284 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); 285 return getNode(ISD::AND, Op.getValueType(), Op, 286 getConstant(Imm, Op.getValueType())); 287} 288 289SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) { 290 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 291 // Mask out any bits that are not valid for this constant. 292 if (VT != MVT::i64) 293 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; 294 295 SDNode *&N = Constants[std::make_pair(Val, VT)]; 296 if (N) return SDOperand(N, 0); 297 N = new ConstantSDNode(Val, VT); 298 AllNodes.push_back(N); 299 return SDOperand(N, 0); 300} 301 302SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) { 303 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 304 if (VT == MVT::f32) 305 Val = (float)Val; // Mask out extra precision. 306 307 // Do the map lookup using the actual bit pattern for the floating point 308 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 309 // we don't have issues with SNANs. 310 union { 311 double DV; 312 uint64_t IV; 313 }; 314 315 DV = Val; 316 317 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)]; 318 if (N) return SDOperand(N, 0); 319 N = new ConstantFPSDNode(Val, VT); 320 AllNodes.push_back(N); 321 return SDOperand(N, 0); 322} 323 324 325 326SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, 327 MVT::ValueType VT) { 328 SDNode *&N = GlobalValues[GV]; 329 if (N) return SDOperand(N, 0); 330 N = new GlobalAddressSDNode(GV,VT); 331 AllNodes.push_back(N); 332 return SDOperand(N, 0); 333} 334 335SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) { 336 SDNode *&N = FrameIndices[FI]; 337 if (N) return SDOperand(N, 0); 338 N = new FrameIndexSDNode(FI, VT); 339 AllNodes.push_back(N); 340 return SDOperand(N, 0); 341} 342 343SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) { 344 SDNode *N = ConstantPoolIndices[CPIdx]; 345 if (N) return SDOperand(N, 0); 346 N = new ConstantPoolSDNode(CPIdx, VT); 347 AllNodes.push_back(N); 348 return SDOperand(N, 0); 349} 350 351SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 352 SDNode *&N = BBNodes[MBB]; 353 if (N) return SDOperand(N, 0); 354 N = new BasicBlockSDNode(MBB); 355 AllNodes.push_back(N); 356 return SDOperand(N, 0); 357} 358 359SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { 360 if ((unsigned)VT >= ValueTypeNodes.size()) 361 ValueTypeNodes.resize(VT+1); 362 if (ValueTypeNodes[VT] == 0) { 363 ValueTypeNodes[VT] = new VTSDNode(VT); 364 AllNodes.push_back(ValueTypeNodes[VT]); 365 } 366 367 return SDOperand(ValueTypeNodes[VT], 0); 368} 369 370SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { 371 SDNode *&N = ExternalSymbols[Sym]; 372 if (N) return SDOperand(N, 0); 373 N = new ExternalSymbolSDNode(Sym, VT); 374 AllNodes.push_back(N); 375 return SDOperand(N, 0); 376} 377 378SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { 379 if ((unsigned)Cond >= CondCodeNodes.size()) 380 CondCodeNodes.resize(Cond+1); 381 382 if (CondCodeNodes[Cond] == 0) { 383 CondCodeNodes[Cond] = new CondCodeSDNode(Cond); 384 AllNodes.push_back(CondCodeNodes[Cond]); 385 } 386 return SDOperand(CondCodeNodes[Cond], 0); 387} 388 389SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1, 390 SDOperand N2, ISD::CondCode Cond) { 391 // These setcc operations always fold. 392 switch (Cond) { 393 default: break; 394 case ISD::SETFALSE: 395 case ISD::SETFALSE2: return getConstant(0, VT); 396 case ISD::SETTRUE: 397 case ISD::SETTRUE2: return getConstant(1, VT); 398 } 399 400 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { 401 uint64_t C2 = N2C->getValue(); 402 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { 403 uint64_t C1 = N1C->getValue(); 404 405 // Sign extend the operands if required 406 if (ISD::isSignedIntSetCC(Cond)) { 407 C1 = N1C->getSignExtended(); 408 C2 = N2C->getSignExtended(); 409 } 410 411 switch (Cond) { 412 default: assert(0 && "Unknown integer setcc!"); 413 case ISD::SETEQ: return getConstant(C1 == C2, VT); 414 case ISD::SETNE: return getConstant(C1 != C2, VT); 415 case ISD::SETULT: return getConstant(C1 < C2, VT); 416 case ISD::SETUGT: return getConstant(C1 > C2, VT); 417 case ISD::SETULE: return getConstant(C1 <= C2, VT); 418 case ISD::SETUGE: return getConstant(C1 >= C2, VT); 419 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); 420 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); 421 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); 422 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); 423 } 424 } else { 425 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform 426 // the comparison on the input. 427 if (N1.getOpcode() == ISD::ZERO_EXTEND) { 428 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType()); 429 430 // If the comparison constant has bits in the upper part, the 431 // zero-extended value could never match. 432 if (C2 & (~0ULL << InSize)) { 433 unsigned VSize = MVT::getSizeInBits(N1.getValueType()); 434 switch (Cond) { 435 case ISD::SETUGT: 436 case ISD::SETUGE: 437 case ISD::SETEQ: return getConstant(0, VT); 438 case ISD::SETULT: 439 case ISD::SETULE: 440 case ISD::SETNE: return getConstant(1, VT); 441 case ISD::SETGT: 442 case ISD::SETGE: 443 // True if the sign bit of C2 is set. 444 return getConstant((C2 & (1ULL << VSize)) != 0, VT); 445 case ISD::SETLT: 446 case ISD::SETLE: 447 // True if the sign bit of C2 isn't set. 448 return getConstant((C2 & (1ULL << VSize)) == 0, VT); 449 default: 450 break; 451 } 452 } 453 454 // Otherwise, we can perform the comparison with the low bits. 455 switch (Cond) { 456 case ISD::SETEQ: 457 case ISD::SETNE: 458 case ISD::SETUGT: 459 case ISD::SETUGE: 460 case ISD::SETULT: 461 case ISD::SETULE: 462 return getSetCC(VT, N1.getOperand(0), 463 getConstant(C2, N1.getOperand(0).getValueType()), 464 Cond); 465 default: 466 break; // todo, be more careful with signed comparisons 467 } 468 } 469 470 uint64_t MinVal, MaxVal; 471 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0)); 472 if (ISD::isSignedIntSetCC(Cond)) { 473 MinVal = 1ULL << (OperandBitSize-1); 474 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. 475 MaxVal = ~0ULL >> (65-OperandBitSize); 476 else 477 MaxVal = 0; 478 } else { 479 MinVal = 0; 480 MaxVal = ~0ULL >> (64-OperandBitSize); 481 } 482 483 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 484 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 485 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true 486 --C2; // X >= C1 --> X > (C1-1) 487 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 488 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); 489 } 490 491 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 492 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true 493 ++C2; // X <= C1 --> X < (C1+1) 494 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 495 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); 496 } 497 498 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal) 499 return getConstant(0, VT); // X < MIN --> false 500 501 // Canonicalize setgt X, Min --> setne X, Min 502 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal) 503 return getSetCC(VT, N1, N2, ISD::SETNE); 504 505 // If we have setult X, 1, turn it into seteq X, 0 506 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1) 507 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()), 508 ISD::SETEQ); 509 // If we have setugt X, Max-1, turn it into seteq X, Max 510 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1) 511 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()), 512 ISD::SETEQ); 513 514 // If we have "setcc X, C1", check to see if we can shrink the immediate 515 // by changing cc. 516 517 // SETUGT X, SINTMAX -> SETLT X, 0 518 if (Cond == ISD::SETUGT && OperandBitSize != 1 && 519 C2 == (~0ULL >> (65-OperandBitSize))) 520 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT); 521 522 // FIXME: Implement the rest of these. 523 524 525 // Fold bit comparisons when we can. 526 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 527 VT == N1.getValueType() && N1.getOpcode() == ISD::AND) 528 if (ConstantSDNode *AndRHS = 529 dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 530 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 531 // Perform the xform if the AND RHS is a single bit. 532 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { 533 return getNode(ISD::SRL, VT, N1, 534 getConstant(Log2_64(AndRHS->getValue()), 535 TLI.getShiftAmountTy())); 536 } 537 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) { 538 // (X & 8) == 8 --> (X & 8) >> 3 539 // Perform the xform if C2 is a single bit. 540 if ((C2 & (C2-1)) == 0) { 541 return getNode(ISD::SRL, VT, N1, 542 getConstant(Log2_64(C2),TLI.getShiftAmountTy())); 543 } 544 } 545 } 546 } 547 } else if (isa<ConstantSDNode>(N1.Val)) { 548 // Ensure that the constant occurs on the RHS. 549 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 550 } 551 552 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) 553 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { 554 double C1 = N1C->getValue(), C2 = N2C->getValue(); 555 556 switch (Cond) { 557 default: break; // FIXME: Implement the rest of these! 558 case ISD::SETEQ: return getConstant(C1 == C2, VT); 559 case ISD::SETNE: return getConstant(C1 != C2, VT); 560 case ISD::SETLT: return getConstant(C1 < C2, VT); 561 case ISD::SETGT: return getConstant(C1 > C2, VT); 562 case ISD::SETLE: return getConstant(C1 <= C2, VT); 563 case ISD::SETGE: return getConstant(C1 >= C2, VT); 564 } 565 } else { 566 // Ensure that the constant occurs on the RHS. 567 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 568 } 569 570 if (N1 == N2) { 571 // We can always fold X == Y for integer setcc's. 572 if (MVT::isInteger(N1.getValueType())) 573 return getConstant(ISD::isTrueWhenEqual(Cond), VT); 574 unsigned UOF = ISD::getUnorderedFlavor(Cond); 575 if (UOF == 2) // FP operators that are undefined on NaNs. 576 return getConstant(ISD::isTrueWhenEqual(Cond), VT); 577 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) 578 return getConstant(UOF, VT); 579 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO 580 // if it is not already. 581 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO; 582 if (NewCond != Cond) 583 return getSetCC(VT, N1, N2, NewCond); 584 } 585 586 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 587 MVT::isInteger(N1.getValueType())) { 588 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || 589 N1.getOpcode() == ISD::XOR) { 590 // Simplify (X+Y) == (X+Z) --> Y == Z 591 if (N1.getOpcode() == N2.getOpcode()) { 592 if (N1.getOperand(0) == N2.getOperand(0)) 593 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond); 594 if (N1.getOperand(1) == N2.getOperand(1)) 595 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond); 596 if (isCommutativeBinOp(N1.getOpcode())) { 597 // If X op Y == Y op X, try other combinations. 598 if (N1.getOperand(0) == N2.getOperand(1)) 599 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond); 600 if (N1.getOperand(1) == N2.getOperand(0)) 601 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond); 602 } 603 } 604 605 // FIXME: move this stuff to the DAG Combiner when it exists! 606 607 // Simplify (X+Z) == X --> Z == 0 608 if (N1.getOperand(0) == N2) 609 return getSetCC(VT, N1.getOperand(1), 610 getConstant(0, N1.getValueType()), Cond); 611 if (N1.getOperand(1) == N2) { 612 if (isCommutativeBinOp(N1.getOpcode())) 613 return getSetCC(VT, N1.getOperand(0), 614 getConstant(0, N1.getValueType()), Cond); 615 else { 616 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); 617 // (Z-X) == X --> Z == X<<1 618 return getSetCC(VT, N1.getOperand(0), 619 getNode(ISD::SHL, N2.getValueType(), 620 N2, getConstant(1, TLI.getShiftAmountTy())), 621 Cond); 622 } 623 } 624 } 625 626 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB || 627 N2.getOpcode() == ISD::XOR) { 628 // Simplify X == (X+Z) --> Z == 0 629 if (N2.getOperand(0) == N1) 630 return getSetCC(VT, N2.getOperand(1), 631 getConstant(0, N2.getValueType()), Cond); 632 else if (N2.getOperand(1) == N1) 633 return getSetCC(VT, N2.getOperand(0), getConstant(0, N2.getValueType()), 634 Cond); 635 } 636 } 637 638 // Fold away ALL boolean setcc's. 639 if (N1.getValueType() == MVT::i1) { 640 switch (Cond) { 641 default: assert(0 && "Unknown integer setcc!"); 642 case ISD::SETEQ: // X == Y -> (X^Y)^1 643 N1 = getNode(ISD::XOR, MVT::i1, 644 getNode(ISD::XOR, MVT::i1, N1, N2), 645 getConstant(1, MVT::i1)); 646 break; 647 case ISD::SETNE: // X != Y --> (X^Y) 648 N1 = getNode(ISD::XOR, MVT::i1, N1, N2); 649 break; 650 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y 651 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y 652 N1 = getNode(ISD::AND, MVT::i1, N2, 653 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1))); 654 break; 655 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X 656 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X 657 N1 = getNode(ISD::AND, MVT::i1, N1, 658 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1))); 659 break; 660 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y 661 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y 662 N1 = getNode(ISD::OR, MVT::i1, N2, 663 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1))); 664 break; 665 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X 666 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X 667 N1 = getNode(ISD::OR, MVT::i1, N1, 668 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1))); 669 break; 670 } 671 if (VT != MVT::i1) 672 N1 = getNode(ISD::ZERO_EXTEND, VT, N1); 673 return N1; 674 } 675 676 // Could not fold it. 677 return SDOperand(); 678} 679 680 681 682/// getNode - Gets or creates the specified node. 683/// 684SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { 685 SDNode *N = new SDNode(Opcode, VT); 686 AllNodes.push_back(N); 687 return SDOperand(N, 0); 688} 689 690SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 691 SDOperand Operand) { 692 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { 693 uint64_t Val = C->getValue(); 694 switch (Opcode) { 695 default: break; 696 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); 697 case ISD::ZERO_EXTEND: return getConstant(Val, VT); 698 case ISD::TRUNCATE: return getConstant(Val, VT); 699 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); 700 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); 701 } 702 } 703 704 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) 705 switch (Opcode) { 706 case ISD::FNEG: 707 return getConstantFP(-C->getValue(), VT); 708 case ISD::FP_ROUND: 709 case ISD::FP_EXTEND: 710 return getConstantFP(C->getValue(), VT); 711 case ISD::FP_TO_SINT: 712 return getConstant((int64_t)C->getValue(), VT); 713 case ISD::FP_TO_UINT: 714 return getConstant((uint64_t)C->getValue(), VT); 715 } 716 717 unsigned OpOpcode = Operand.Val->getOpcode(); 718 switch (Opcode) { 719 case ISD::TokenFactor: 720 return Operand; // Factor of one node? No factor. 721 case ISD::SIGN_EXTEND: 722 if (Operand.getValueType() == VT) return Operand; // noop extension 723 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 724 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 725 break; 726 case ISD::ZERO_EXTEND: 727 if (Operand.getValueType() == VT) return Operand; // noop extension 728 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 729 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); 730 break; 731 case ISD::TRUNCATE: 732 if (Operand.getValueType() == VT) return Operand; // noop truncate 733 if (OpOpcode == ISD::TRUNCATE) 734 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 735 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) { 736 // If the source is smaller than the dest, we still need an extend. 737 if (Operand.Val->getOperand(0).getValueType() < VT) 738 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 739 else if (Operand.Val->getOperand(0).getValueType() > VT) 740 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 741 else 742 return Operand.Val->getOperand(0); 743 } 744 break; 745 case ISD::FNEG: 746 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X) 747 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1), 748 Operand.Val->getOperand(0)); 749 if (OpOpcode == ISD::FNEG) // --X -> X 750 return Operand.Val->getOperand(0); 751 break; 752 case ISD::FABS: 753 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 754 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); 755 break; 756 } 757 758 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))]; 759 if (N) return SDOperand(N, 0); 760 N = new SDNode(Opcode, Operand); 761 N->setValueTypes(VT); 762 AllNodes.push_back(N); 763 return SDOperand(N, 0); 764} 765 766/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 767/// this predicate to simplify operations downstream. V and Mask are known to 768/// be the same type. 769static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask, 770 const TargetLowering &TLI) { 771 unsigned SrcBits; 772 if (Mask == 0) return true; 773 774 // If we know the result of a setcc has the top bits zero, use this info. 775 switch (Op.getOpcode()) { 776 case ISD::Constant: 777 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0; 778 779 case ISD::SETCC: 780 return ((Mask & 1) == 0) && 781 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult; 782 783 case ISD::ZEXTLOAD: 784 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT()); 785 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. 786 case ISD::ZERO_EXTEND: 787 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType()); 788 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI); 789 790 case ISD::AND: 791 // (X & C1) & C2 == 0 iff C1 & C2 == 0. 792 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 793 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI); 794 795 // FALL THROUGH 796 case ISD::OR: 797 case ISD::XOR: 798 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) && 799 MaskedValueIsZero(Op.getOperand(1), Mask, TLI); 800 case ISD::SELECT: 801 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) && 802 MaskedValueIsZero(Op.getOperand(2), Mask, TLI); 803 804 case ISD::SRL: 805 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0 806 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 807 uint64_t NewVal = Mask << ShAmt->getValue(); 808 SrcBits = MVT::getSizeInBits(Op.getValueType()); 809 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1; 810 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); 811 } 812 return false; 813 case ISD::SHL: 814 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0 815 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 816 uint64_t NewVal = Mask >> ShAmt->getValue(); 817 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); 818 } 819 return false; 820 // TODO we could handle some SRA cases here. 821 default: break; 822 } 823 824 return false; 825} 826 827 828 829SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 830 SDOperand N1, SDOperand N2) { 831#ifndef NDEBUG 832 switch (Opcode) { 833 case ISD::TokenFactor: 834 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 835 N2.getValueType() == MVT::Other && "Invalid token factor!"); 836 break; 837 case ISD::AND: 838 case ISD::OR: 839 case ISD::XOR: 840 case ISD::UDIV: 841 case ISD::UREM: 842 case ISD::MULHU: 843 case ISD::MULHS: 844 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); 845 // fall through 846 case ISD::ADD: 847 case ISD::SUB: 848 case ISD::MUL: 849 case ISD::SDIV: 850 case ISD::SREM: 851 assert(N1.getValueType() == N2.getValueType() && 852 N1.getValueType() == VT && "Binary operator types must match!"); 853 break; 854 855 case ISD::SHL: 856 case ISD::SRA: 857 case ISD::SRL: 858 assert(VT == N1.getValueType() && 859 "Shift operators return type must be the same as their first arg"); 860 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && 861 VT != MVT::i1 && "Shifts only work on integers"); 862 break; 863 case ISD::FP_ROUND_INREG: { 864 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 865 assert(VT == N1.getValueType() && "Not an inreg round!"); 866 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && 867 "Cannot FP_ROUND_INREG integer types"); 868 assert(EVT <= VT && "Not rounding down!"); 869 break; 870 } 871 case ISD::SIGN_EXTEND_INREG: { 872 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 873 assert(VT == N1.getValueType() && "Not an inreg extend!"); 874 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && 875 "Cannot *_EXTEND_INREG FP types"); 876 assert(EVT <= VT && "Not extending!"); 877 } 878 879 default: break; 880 } 881#endif 882 883 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 884 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 885 if (N1C) { 886 if (N2C) { 887 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); 888 switch (Opcode) { 889 case ISD::ADD: return getConstant(C1 + C2, VT); 890 case ISD::SUB: return getConstant(C1 - C2, VT); 891 case ISD::MUL: return getConstant(C1 * C2, VT); 892 case ISD::UDIV: 893 if (C2) return getConstant(C1 / C2, VT); 894 break; 895 case ISD::UREM : 896 if (C2) return getConstant(C1 % C2, VT); 897 break; 898 case ISD::SDIV : 899 if (C2) return getConstant(N1C->getSignExtended() / 900 N2C->getSignExtended(), VT); 901 break; 902 case ISD::SREM : 903 if (C2) return getConstant(N1C->getSignExtended() % 904 N2C->getSignExtended(), VT); 905 break; 906 case ISD::AND : return getConstant(C1 & C2, VT); 907 case ISD::OR : return getConstant(C1 | C2, VT); 908 case ISD::XOR : return getConstant(C1 ^ C2, VT); 909 case ISD::SHL : return getConstant(C1 << (int)C2, VT); 910 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT); 911 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); 912 default: break; 913 } 914 915 } else { // Cannonicalize constant to RHS if commutative 916 if (isCommutativeBinOp(Opcode)) { 917 std::swap(N1C, N2C); 918 std::swap(N1, N2); 919 } 920 } 921 922 switch (Opcode) { 923 default: break; 924 case ISD::SHL: // shl 0, X -> 0 925 if (N1C->isNullValue()) return N1; 926 break; 927 case ISD::SRL: // srl 0, X -> 0 928 if (N1C->isNullValue()) return N1; 929 break; 930 case ISD::SRA: // sra -1, X -> -1 931 if (N1C->isAllOnesValue()) return N1; 932 break; 933 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT 934 // Extending a constant? Just return the extended constant. 935 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1); 936 return getNode(ISD::SIGN_EXTEND, VT, Tmp); 937 } 938 } 939 940 if (N2C) { 941 uint64_t C2 = N2C->getValue(); 942 943 switch (Opcode) { 944 case ISD::ADD: 945 if (!C2) return N1; // add X, 0 -> X 946 break; 947 case ISD::SUB: 948 if (!C2) return N1; // sub X, 0 -> X 949 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT)); 950 case ISD::MUL: 951 if (!C2) return N2; // mul X, 0 -> 0 952 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X 953 return getNode(ISD::SUB, VT, getConstant(0, VT), N1); 954 955 // FIXME: Move this to the DAG combiner when it exists. 956 if ((C2 & C2-1) == 0) { 957 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy()); 958 return getNode(ISD::SHL, VT, N1, ShAmt); 959 } 960 break; 961 962 case ISD::MULHU: 963 case ISD::MULHS: 964 if (!C2) return N2; // mul X, 0 -> 0 965 966 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0 967 return getConstant(0, VT); 968 969 // Many others could be handled here, including -1, powers of 2, etc. 970 break; 971 972 case ISD::UDIV: 973 // FIXME: Move this to the DAG combiner when it exists. 974 if ((C2 & C2-1) == 0 && C2) { 975 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy()); 976 return getNode(ISD::SRL, VT, N1, ShAmt); 977 } 978 break; 979 980 case ISD::SHL: 981 case ISD::SRL: 982 case ISD::SRA: 983 // If the shift amount is bigger than the size of the data, then all the 984 // bits are shifted out. Simplify to undef. 985 if (C2 >= MVT::getSizeInBits(N1.getValueType())) { 986 return getNode(ISD::UNDEF, N1.getValueType()); 987 } 988 if (C2 == 0) return N1; 989 990 if (Opcode == ISD::SHL && N1.getNumOperands() == 2) 991 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 992 unsigned OpSAC = OpSA->getValue(); 993 if (N1.getOpcode() == ISD::SHL) { 994 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType())) 995 return getConstant(0, N1.getValueType()); 996 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0), 997 getConstant(C2+OpSAC, N2.getValueType())); 998 } else if (N1.getOpcode() == ISD::SRL) { 999 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1) 1000 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0), 1001 getConstant(~0ULL << OpSAC, VT)); 1002 if (C2 > OpSAC) { 1003 return getNode(ISD::SHL, VT, Mask, 1004 getConstant(C2-OpSAC, N2.getValueType())); 1005 } else { 1006 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2) 1007 return getNode(ISD::SRL, VT, Mask, 1008 getConstant(OpSAC-C2, N2.getValueType())); 1009 } 1010 } else if (N1.getOpcode() == ISD::SRA) { 1011 // if C1 == C2, just mask out low bits. 1012 if (C2 == OpSAC) 1013 return getNode(ISD::AND, VT, N1.getOperand(0), 1014 getConstant(~0ULL << C2, VT)); 1015 } 1016 } 1017 break; 1018 1019 case ISD::AND: 1020 if (!C2) return N2; // X and 0 -> 0 1021 if (N2C->isAllOnesValue()) 1022 return N1; // X and -1 -> X 1023 1024 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0 1025 return getConstant(0, VT); 1026 1027 { 1028 uint64_t NotC2 = ~C2; 1029 if (VT != MVT::i64) 1030 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1; 1031 1032 if (MaskedValueIsZero(N1, NotC2, TLI)) 1033 return N1; // if (X & ~C2) -> 0, the and is redundant 1034 } 1035 1036 // FIXME: Should add a corresponding version of this for 1037 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which 1038 // we don't have yet. 1039 1040 // and (sign_extend_inreg x:16:32), 1 -> and x, 1 1041 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) { 1042 // If we are masking out the part of our input that was extended, just 1043 // mask the input to the extension directly. 1044 unsigned ExtendBits = 1045 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT()); 1046 if ((C2 & (~0ULL << ExtendBits)) == 0) 1047 return getNode(ISD::AND, VT, N1.getOperand(0), N2); 1048 } else if (N1.getOpcode() == ISD::OR) { 1049 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) 1050 if ((ORI->getValue() & C2) == C2) { 1051 // If the 'or' is setting all of the bits that we are masking for, 1052 // we know the result of the AND will be the AND mask itself. 1053 return N2; 1054 } 1055 } 1056 break; 1057 case ISD::OR: 1058 if (!C2)return N1; // X or 0 -> X 1059 if (N2C->isAllOnesValue()) 1060 return N2; // X or -1 -> -1 1061 break; 1062 case ISD::XOR: 1063 if (!C2) return N1; // X xor 0 -> X 1064 if (N2C->isAllOnesValue()) { 1065 if (N1.Val->getOpcode() == ISD::SETCC){ 1066 SDNode *SetCC = N1.Val; 1067 // !(X op Y) -> (X !op Y) 1068 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType()); 1069 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get(); 1070 return getSetCC(SetCC->getValueType(0), 1071 SetCC->getOperand(0), SetCC->getOperand(1), 1072 ISD::getSetCCInverse(CC, isInteger)); 1073 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) { 1074 SDNode *Op = N1.Val; 1075 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible 1076 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible 1077 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1); 1078 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) { 1079 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS 1080 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS 1081 if (Op->getOpcode() == ISD::AND) 1082 return getNode(ISD::OR, VT, LHS, RHS); 1083 return getNode(ISD::AND, VT, LHS, RHS); 1084 } 1085 } 1086 // X xor -1 -> not(x) ? 1087 } 1088 break; 1089 } 1090 1091 // Reassociate ((X op C1) op C2) if possible. 1092 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode)) 1093 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1))) 1094 return getNode(Opcode, VT, N1.Val->getOperand(0), 1095 getNode(Opcode, VT, N2, N1.Val->getOperand(1))); 1096 } 1097 1098 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); 1099 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); 1100 if (N1CFP) { 1101 if (N2CFP) { 1102 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); 1103 switch (Opcode) { 1104 case ISD::ADD: return getConstantFP(C1 + C2, VT); 1105 case ISD::SUB: return getConstantFP(C1 - C2, VT); 1106 case ISD::MUL: return getConstantFP(C1 * C2, VT); 1107 case ISD::SDIV: 1108 if (C2) return getConstantFP(C1 / C2, VT); 1109 break; 1110 case ISD::SREM : 1111 if (C2) return getConstantFP(fmod(C1, C2), VT); 1112 break; 1113 default: break; 1114 } 1115 1116 } else { // Cannonicalize constant to RHS if commutative 1117 if (isCommutativeBinOp(Opcode)) { 1118 std::swap(N1CFP, N2CFP); 1119 std::swap(N1, N2); 1120 } 1121 } 1122 1123 if (Opcode == ISD::FP_ROUND_INREG) 1124 return getNode(ISD::FP_EXTEND, VT, 1125 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1)); 1126 } 1127 1128 // Finally, fold operations that do not require constants. 1129 switch (Opcode) { 1130 case ISD::TokenFactor: 1131 if (N1.getOpcode() == ISD::EntryToken) 1132 return N2; 1133 if (N2.getOpcode() == ISD::EntryToken) 1134 return N1; 1135 break; 1136 1137 case ISD::AND: 1138 case ISD::OR: 1139 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){ 1140 SDNode *LHS = N1.Val, *RHS = N2.Val; 1141 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0); 1142 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1); 1143 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get(); 1144 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get(); 1145 1146 if (LR == RR && isa<ConstantSDNode>(LR) && 1147 Op2 == Op1 && MVT::isInteger(LL.getValueType())) { 1148 // (X != 0) | (Y != 0) -> (X|Y != 0) 1149 // (X == 0) & (Y == 0) -> (X|Y == 0) 1150 // (X < 0) | (Y < 0) -> (X|Y < 0) 1151 if (cast<ConstantSDNode>(LR)->getValue() == 0 && 1152 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) || 1153 (Op2 == ISD::SETNE && Opcode == ISD::OR) || 1154 (Op2 == ISD::SETLT && Opcode == ISD::OR))) 1155 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR, 1156 Op2); 1157 1158 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) { 1159 // (X == -1) & (Y == -1) -> (X&Y == -1) 1160 // (X != -1) | (Y != -1) -> (X&Y != -1) 1161 // (X > -1) | (Y > -1) -> (X&Y > -1) 1162 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) || 1163 (Opcode == ISD::OR && Op2 == ISD::SETNE) || 1164 (Opcode == ISD::OR && Op2 == ISD::SETGT)) 1165 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL), 1166 LR, Op2); 1167 // (X > -1) & (Y > -1) -> (X|Y > -1) 1168 if (Opcode == ISD::AND && Op2 == ISD::SETGT) 1169 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), 1170 LR, Op2); 1171 } 1172 } 1173 1174 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y) 1175 if (LL == RR && LR == RL) { 1176 Op2 = ISD::getSetCCSwappedOperands(Op2); 1177 goto MatchedBackwards; 1178 } 1179 1180 if (LL == RL && LR == RR) { 1181 MatchedBackwards: 1182 ISD::CondCode Result; 1183 bool isInteger = MVT::isInteger(LL.getValueType()); 1184 if (Opcode == ISD::OR) 1185 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger); 1186 else 1187 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger); 1188 1189 if (Result != ISD::SETCC_INVALID) 1190 return getSetCC(LHS->getValueType(0), LL, LR, Result); 1191 } 1192 } 1193 1194 // and/or zext(a), zext(b) -> zext(and/or a, b) 1195 if (N1.getOpcode() == ISD::ZERO_EXTEND && 1196 N2.getOpcode() == ISD::ZERO_EXTEND && 1197 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType()) 1198 return getNode(ISD::ZERO_EXTEND, VT, 1199 getNode(Opcode, N1.getOperand(0).getValueType(), 1200 N1.getOperand(0), N2.getOperand(0))); 1201 break; 1202 case ISD::XOR: 1203 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0 1204 break; 1205 case ISD::ADD: 1206 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B 1207 return getNode(ISD::SUB, VT, N1, N2.getOperand(0)); 1208 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A 1209 return getNode(ISD::SUB, VT, N2, N1.getOperand(0)); 1210 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && 1211 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0) 1212 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A 1213 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) && 1214 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0) 1215 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B 1216 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) && 1217 !MVT::isFloatingPoint(N2.getValueType())) 1218 return N2.Val->getOperand(0); // A+(B-A) -> B 1219 break; 1220 case ISD::SUB: 1221 if (N1.getOpcode() == ISD::ADD) { 1222 if (N1.Val->getOperand(0) == N2 && 1223 !MVT::isFloatingPoint(N2.getValueType())) 1224 return N1.Val->getOperand(1); // (A+B)-A == B 1225 if (N1.Val->getOperand(1) == N2 && 1226 !MVT::isFloatingPoint(N2.getValueType())) 1227 return N1.Val->getOperand(0); // (A+B)-B == A 1228 } 1229 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B 1230 return getNode(ISD::ADD, VT, N1, N2.getOperand(0)); 1231 break; 1232 case ISD::FP_ROUND_INREG: 1233 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 1234 break; 1235 case ISD::SIGN_EXTEND_INREG: { 1236 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1237 if (EVT == VT) return N1; // Not actually extending 1238 1239 // If we are sign extending an extension, use the original source. 1240 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) 1241 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT) 1242 return N1; 1243 1244 // If we are sign extending a sextload, return just the load. 1245 if (N1.getOpcode() == ISD::SEXTLOAD) 1246 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT) 1247 return N1; 1248 1249 // If we are extending the result of a setcc, and we already know the 1250 // contents of the top bits, eliminate the extension. 1251 if (N1.getOpcode() == ISD::SETCC && 1252 TLI.getSetCCResultContents() == 1253 TargetLowering::ZeroOrNegativeOneSetCCResult) 1254 return N1; 1255 1256 // If we are sign extending the result of an (and X, C) operation, and we 1257 // know the extended bits are zeros already, don't do the extend. 1258 if (N1.getOpcode() == ISD::AND) 1259 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 1260 uint64_t Mask = N1C->getValue(); 1261 unsigned NumBits = MVT::getSizeInBits(EVT); 1262 if ((Mask & (~0ULL << (NumBits-1))) == 0) 1263 return N1; 1264 } 1265 break; 1266 } 1267 1268 // FIXME: figure out how to safely handle things like 1269 // int foo(int x) { return 1 << (x & 255); } 1270 // int bar() { return foo(256); } 1271#if 0 1272 case ISD::SHL: 1273 case ISD::SRL: 1274 case ISD::SRA: 1275 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && 1276 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) 1277 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1278 else if (N2.getOpcode() == ISD::AND) 1279 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { 1280 // If the and is only masking out bits that cannot effect the shift, 1281 // eliminate the and. 1282 unsigned NumBits = MVT::getSizeInBits(VT); 1283 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1284 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1285 } 1286 break; 1287#endif 1288 } 1289 1290 // Memoize this node if possible. 1291 SDNode *N; 1292 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) { 1293 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))]; 1294 if (BON) return SDOperand(BON, 0); 1295 1296 BON = N = new SDNode(Opcode, N1, N2); 1297 } else { 1298 N = new SDNode(Opcode, N1, N2); 1299 } 1300 1301 N->setValueTypes(VT); 1302 AllNodes.push_back(N); 1303 return SDOperand(N, 0); 1304} 1305 1306// setAdjCallChain - This method changes the token chain of an 1307// CALLSEQ_START/END node to be the specified operand. 1308void SDNode::setAdjCallChain(SDOperand N) { 1309 assert(N.getValueType() == MVT::Other); 1310 assert((getOpcode() == ISD::CALLSEQ_START || 1311 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!"); 1312 1313 Operands[0].Val->removeUser(this); 1314 Operands[0] = N; 1315 N.Val->Uses.push_back(this); 1316} 1317 1318 1319 1320SDOperand SelectionDAG::getLoad(MVT::ValueType VT, 1321 SDOperand Chain, SDOperand Ptr, 1322 SDOperand SV) { 1323 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))]; 1324 if (N) return SDOperand(N, 0); 1325 N = new SDNode(ISD::LOAD, Chain, Ptr, SV); 1326 1327 // Loads have a token chain. 1328 N->setValueTypes(VT, MVT::Other); 1329 AllNodes.push_back(N); 1330 return SDOperand(N, 0); 1331} 1332 1333 1334SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT, 1335 SDOperand Chain, SDOperand Ptr, SDOperand SV, 1336 MVT::ValueType EVT) { 1337 std::vector<SDOperand> Ops; 1338 Ops.reserve(4); 1339 Ops.push_back(Chain); 1340 Ops.push_back(Ptr); 1341 Ops.push_back(SV); 1342 Ops.push_back(getValueType(EVT)); 1343 std::vector<MVT::ValueType> VTs; 1344 VTs.reserve(2); 1345 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. 1346 return getNode(Opcode, VTs, Ops); 1347} 1348 1349SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1350 SDOperand N1, SDOperand N2, SDOperand N3) { 1351 // Perform various simplifications. 1352 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1353 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1354 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); 1355 switch (Opcode) { 1356 case ISD::SETCC: { 1357 // Use SimplifySetCC to simplify SETCC's. 1358 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); 1359 if (Simp.Val) return Simp; 1360 break; 1361 } 1362 case ISD::SELECT: 1363 if (N1C) 1364 if (N1C->getValue()) 1365 return N2; // select true, X, Y -> X 1366 else 1367 return N3; // select false, X, Y -> Y 1368 1369 if (N2 == N3) return N2; // select C, X, X -> X 1370 1371 if (VT == MVT::i1) { // Boolean SELECT 1372 if (N2C) { 1373 if (N2C->getValue()) // select C, 1, X -> C | X 1374 return getNode(ISD::OR, VT, N1, N3); 1375 else // select C, 0, X -> ~C & X 1376 return getNode(ISD::AND, VT, 1377 getNode(ISD::XOR, N1.getValueType(), N1, 1378 getConstant(1, N1.getValueType())), N3); 1379 } else if (N3C) { 1380 if (N3C->getValue()) // select C, X, 1 -> ~C | X 1381 return getNode(ISD::OR, VT, 1382 getNode(ISD::XOR, N1.getValueType(), N1, 1383 getConstant(1, N1.getValueType())), N2); 1384 else // select C, X, 0 -> C & X 1385 return getNode(ISD::AND, VT, N1, N2); 1386 } 1387 1388 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y 1389 return getNode(ISD::OR, VT, N1, N3); 1390 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y 1391 return getNode(ISD::AND, VT, N1, N2); 1392 } 1393 1394 // If this is a selectcc, check to see if we can simplify the result. 1395 if (N1.Val->getOpcode() == ISD::SETCC) { 1396 SDNode *SetCC = N1.Val; 1397 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get(); 1398 if (ConstantFPSDNode *CFP = 1399 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1))) 1400 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0 1401 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs 1402 if ((CC == ISD::SETGE || CC == ISD::SETGT) && 1403 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG && 1404 N3.getOperand(0) == N2) 1405 return getNode(ISD::FABS, VT, N2); 1406 1407 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs 1408 if ((CC == ISD::SETLT || CC == ISD::SETLE) && 1409 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG && 1410 N2.getOperand(0) == N3) 1411 return getNode(ISD::FABS, VT, N3); 1412 } 1413 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1), A 1414 if (ConstantSDNode *CN = 1415 dyn_cast<ConstantSDNode>(SetCC->getOperand(1))) 1416 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0) 1417 if (CC == ISD::SETLT) { 1418 MVT::ValueType XType = SetCC->getOperand(0).getValueType(); 1419 MVT::ValueType AType = N2.getValueType(); 1420 if (XType >= AType) { 1421 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a 1422 // single-bit constant. FIXME: remove once the dag combiner 1423 // exists. 1424 if (ConstantSDNode *AC = dyn_cast<ConstantSDNode>(N2)) 1425 if ((AC->getValue() & (AC->getValue()-1)) == 0) { 1426 unsigned ShCtV = Log2_64(AC->getValue()); 1427 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1; 1428 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy()); 1429 SDOperand Shift = getNode(ISD::SRL, XType, 1430 SetCC->getOperand(0), ShCt); 1431 if (XType > AType) 1432 Shift = getNode(ISD::TRUNCATE, AType, Shift); 1433 return getNode(ISD::AND, AType, Shift, N2); 1434 } 1435 1436 1437 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0), 1438 getConstant(MVT::getSizeInBits(XType)-1, 1439 TLI.getShiftAmountTy())); 1440 if (XType > AType) 1441 Shift = getNode(ISD::TRUNCATE, AType, Shift); 1442 return getNode(ISD::AND, AType, Shift, N2); 1443 } 1444 } 1445 } 1446 break; 1447 case ISD::BRCOND: 1448 if (N2C) 1449 if (N2C->getValue()) // Unconditional branch 1450 return getNode(ISD::BR, MVT::Other, N1, N3); 1451 else 1452 return N1; // Never-taken branch 1453 break; 1454 } 1455 1456 std::vector<SDOperand> Ops; 1457 Ops.reserve(3); 1458 Ops.push_back(N1); 1459 Ops.push_back(N2); 1460 Ops.push_back(N3); 1461 1462 // Memoize nodes. 1463 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))]; 1464 if (N) return SDOperand(N, 0); 1465 1466 N = new SDNode(Opcode, N1, N2, N3); 1467 N->setValueTypes(VT); 1468 AllNodes.push_back(N); 1469 return SDOperand(N, 0); 1470} 1471 1472SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1473 SDOperand N1, SDOperand N2, SDOperand N3, 1474 SDOperand N4) { 1475 std::vector<SDOperand> Ops; 1476 Ops.reserve(4); 1477 Ops.push_back(N1); 1478 Ops.push_back(N2); 1479 Ops.push_back(N3); 1480 Ops.push_back(N4); 1481 return getNode(Opcode, VT, Ops); 1482} 1483 1484SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1485 SDOperand N1, SDOperand N2, SDOperand N3, 1486 SDOperand N4, SDOperand N5) { 1487 std::vector<SDOperand> Ops; 1488 Ops.reserve(5); 1489 Ops.push_back(N1); 1490 Ops.push_back(N2); 1491 Ops.push_back(N3); 1492 Ops.push_back(N4); 1493 Ops.push_back(N5); 1494 return getNode(Opcode, VT, Ops); 1495} 1496 1497 1498SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { 1499 assert((!V || isa<PointerType>(V->getType())) && 1500 "SrcValue is not a pointer?"); 1501 SDNode *&N = ValueNodes[std::make_pair(V, Offset)]; 1502 if (N) return SDOperand(N, 0); 1503 1504 N = new SrcValueSDNode(V, Offset); 1505 AllNodes.push_back(N); 1506 return SDOperand(N, 0); 1507} 1508 1509SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1510 std::vector<SDOperand> &Ops) { 1511 switch (Ops.size()) { 1512 case 0: return getNode(Opcode, VT); 1513 case 1: return getNode(Opcode, VT, Ops[0]); 1514 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); 1515 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); 1516 default: break; 1517 } 1518 1519 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val); 1520 switch (Opcode) { 1521 default: break; 1522 case ISD::BRCONDTWOWAY: 1523 if (N1C) 1524 if (N1C->getValue()) // Unconditional branch to true dest. 1525 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]); 1526 else // Unconditional branch to false dest. 1527 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]); 1528 break; 1529 1530 case ISD::TRUNCSTORE: { 1531 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!"); 1532 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT(); 1533#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store 1534 // If this is a truncating store of a constant, convert to the desired type 1535 // and store it instead. 1536 if (isa<Constant>(Ops[0])) { 1537 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); 1538 if (isa<Constant>(Op)) 1539 N1 = Op; 1540 } 1541 // Also for ConstantFP? 1542#endif 1543 if (Ops[0].getValueType() == EVT) // Normal store? 1544 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]); 1545 assert(Ops[1].getValueType() > EVT && "Not a truncation?"); 1546 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) && 1547 "Can't do FP-INT conversion!"); 1548 break; 1549 } 1550 } 1551 1552 // Memoize nodes. 1553 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))]; 1554 if (N) return SDOperand(N, 0); 1555 N = new SDNode(Opcode, Ops); 1556 N->setValueTypes(VT); 1557 AllNodes.push_back(N); 1558 return SDOperand(N, 0); 1559} 1560 1561SDOperand SelectionDAG::getNode(unsigned Opcode, 1562 std::vector<MVT::ValueType> &ResultTys, 1563 std::vector<SDOperand> &Ops) { 1564 if (ResultTys.size() == 1) 1565 return getNode(Opcode, ResultTys[0], Ops); 1566 1567 switch (Opcode) { 1568 case ISD::EXTLOAD: 1569 case ISD::SEXTLOAD: 1570 case ISD::ZEXTLOAD: { 1571 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT(); 1572 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!"); 1573 // If they are asking for an extending load from/to the same thing, return a 1574 // normal load. 1575 if (ResultTys[0] == EVT) 1576 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]); 1577 assert(EVT < ResultTys[0] && 1578 "Should only be an extending load, not truncating!"); 1579 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) && 1580 "Cannot sign/zero extend a FP load!"); 1581 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) && 1582 "Cannot convert from FP to Int or Int -> FP!"); 1583 break; 1584 } 1585 1586 // FIXME: figure out how to safely handle things like 1587 // int foo(int x) { return 1 << (x & 255); } 1588 // int bar() { return foo(256); } 1589#if 0 1590 case ISD::SRA_PARTS: 1591 case ISD::SRL_PARTS: 1592 case ISD::SHL_PARTS: 1593 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 1594 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 1595 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1596 else if (N3.getOpcode() == ISD::AND) 1597 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 1598 // If the and is only masking out bits that cannot effect the shift, 1599 // eliminate the and. 1600 unsigned NumBits = MVT::getSizeInBits(VT)*2; 1601 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1602 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1603 } 1604 break; 1605#endif 1606 } 1607 1608 // Memoize the node. 1609 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, 1610 Ops))]; 1611 if (N) return SDOperand(N, 0); 1612 N = new SDNode(Opcode, Ops); 1613 N->setValueTypes(ResultTys); 1614 AllNodes.push_back(N); 1615 return SDOperand(N, 0); 1616} 1617 1618/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 1619/// indicated value. This method ignores uses of other values defined by this 1620/// operation. 1621bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) { 1622 assert(Value < getNumValues() && "Bad value!"); 1623 1624 // If there is only one value, this is easy. 1625 if (getNumValues() == 1) 1626 return use_size() == NUses; 1627 if (Uses.size() < NUses) return false; 1628 1629 SDOperand TheValue(this, Value); 1630 1631 std::set<SDNode*> UsersHandled; 1632 1633 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end(); 1634 UI != E; ++UI) { 1635 SDNode *User = *UI; 1636 if (User->getNumOperands() == 1 || 1637 UsersHandled.insert(User).second) // First time we've seen this? 1638 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 1639 if (User->getOperand(i) == TheValue) { 1640 if (NUses == 0) 1641 return false; // too many uses 1642 --NUses; 1643 } 1644 } 1645 1646 // Found exactly the right number of uses? 1647 return NUses == 0; 1648} 1649 1650 1651const char *SDNode::getOperationName() const { 1652 switch (getOpcode()) { 1653 default: return "<<Unknown>>"; 1654 case ISD::PCMARKER: return "PCMarker"; 1655 case ISD::SRCVALUE: return "SrcValue"; 1656 case ISD::EntryToken: return "EntryToken"; 1657 case ISD::TokenFactor: return "TokenFactor"; 1658 case ISD::Constant: return "Constant"; 1659 case ISD::ConstantFP: return "ConstantFP"; 1660 case ISD::GlobalAddress: return "GlobalAddress"; 1661 case ISD::FrameIndex: return "FrameIndex"; 1662 case ISD::BasicBlock: return "BasicBlock"; 1663 case ISD::ExternalSymbol: return "ExternalSymbol"; 1664 case ISD::ConstantPool: return "ConstantPoolIndex"; 1665 case ISD::CopyToReg: return "CopyToReg"; 1666 case ISD::CopyFromReg: return "CopyFromReg"; 1667 case ISD::ImplicitDef: return "ImplicitDef"; 1668 case ISD::UNDEF: return "undef"; 1669 1670 // Unary operators 1671 case ISD::FABS: return "fabs"; 1672 case ISD::FNEG: return "fneg"; 1673 case ISD::FSQRT: return "fsqrt"; 1674 case ISD::FSIN: return "fsin"; 1675 case ISD::FCOS: return "fcos"; 1676 1677 // Binary operators 1678 case ISD::ADD: return "add"; 1679 case ISD::SUB: return "sub"; 1680 case ISD::MUL: return "mul"; 1681 case ISD::MULHU: return "mulhu"; 1682 case ISD::MULHS: return "mulhs"; 1683 case ISD::SDIV: return "sdiv"; 1684 case ISD::UDIV: return "udiv"; 1685 case ISD::SREM: return "srem"; 1686 case ISD::UREM: return "urem"; 1687 case ISD::AND: return "and"; 1688 case ISD::OR: return "or"; 1689 case ISD::XOR: return "xor"; 1690 case ISD::SHL: return "shl"; 1691 case ISD::SRA: return "sra"; 1692 case ISD::SRL: return "srl"; 1693 1694 case ISD::SETCC: return "setcc"; 1695 case ISD::SELECT: return "select"; 1696 case ISD::ADD_PARTS: return "add_parts"; 1697 case ISD::SUB_PARTS: return "sub_parts"; 1698 case ISD::SHL_PARTS: return "shl_parts"; 1699 case ISD::SRA_PARTS: return "sra_parts"; 1700 case ISD::SRL_PARTS: return "srl_parts"; 1701 1702 // Conversion operators. 1703 case ISD::SIGN_EXTEND: return "sign_extend"; 1704 case ISD::ZERO_EXTEND: return "zero_extend"; 1705 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 1706 case ISD::TRUNCATE: return "truncate"; 1707 case ISD::FP_ROUND: return "fp_round"; 1708 case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 1709 case ISD::FP_EXTEND: return "fp_extend"; 1710 1711 case ISD::SINT_TO_FP: return "sint_to_fp"; 1712 case ISD::UINT_TO_FP: return "uint_to_fp"; 1713 case ISD::FP_TO_SINT: return "fp_to_sint"; 1714 case ISD::FP_TO_UINT: return "fp_to_uint"; 1715 1716 // Control flow instructions 1717 case ISD::BR: return "br"; 1718 case ISD::BRCOND: return "brcond"; 1719 case ISD::BRCONDTWOWAY: return "brcondtwoway"; 1720 case ISD::RET: return "ret"; 1721 case ISD::CALL: return "call"; 1722 case ISD::TAILCALL:return "tailcall"; 1723 case ISD::CALLSEQ_START: return "callseq_start"; 1724 case ISD::CALLSEQ_END: return "callseq_end"; 1725 1726 // Other operators 1727 case ISD::LOAD: return "load"; 1728 case ISD::STORE: return "store"; 1729 case ISD::EXTLOAD: return "extload"; 1730 case ISD::SEXTLOAD: return "sextload"; 1731 case ISD::ZEXTLOAD: return "zextload"; 1732 case ISD::TRUNCSTORE: return "truncstore"; 1733 1734 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 1735 case ISD::EXTRACT_ELEMENT: return "extract_element"; 1736 case ISD::BUILD_PAIR: return "build_pair"; 1737 case ISD::MEMSET: return "memset"; 1738 case ISD::MEMCPY: return "memcpy"; 1739 case ISD::MEMMOVE: return "memmove"; 1740 1741 // Bit counting 1742 case ISD::CTPOP: return "ctpop"; 1743 case ISD::CTTZ: return "cttz"; 1744 case ISD::CTLZ: return "ctlz"; 1745 1746 // IO Intrinsics 1747 case ISD::READPORT: return "readport"; 1748 case ISD::WRITEPORT: return "writeport"; 1749 case ISD::READIO: return "readio"; 1750 case ISD::WRITEIO: return "writeio"; 1751 1752 case ISD::CONDCODE: 1753 switch (cast<CondCodeSDNode>(this)->get()) { 1754 default: assert(0 && "Unknown setcc condition!"); 1755 case ISD::SETOEQ: return "setoeq"; 1756 case ISD::SETOGT: return "setogt"; 1757 case ISD::SETOGE: return "setoge"; 1758 case ISD::SETOLT: return "setolt"; 1759 case ISD::SETOLE: return "setole"; 1760 case ISD::SETONE: return "setone"; 1761 1762 case ISD::SETO: return "seto"; 1763 case ISD::SETUO: return "setuo"; 1764 case ISD::SETUEQ: return "setue"; 1765 case ISD::SETUGT: return "setugt"; 1766 case ISD::SETUGE: return "setuge"; 1767 case ISD::SETULT: return "setult"; 1768 case ISD::SETULE: return "setule"; 1769 case ISD::SETUNE: return "setune"; 1770 1771 case ISD::SETEQ: return "seteq"; 1772 case ISD::SETGT: return "setgt"; 1773 case ISD::SETGE: return "setge"; 1774 case ISD::SETLT: return "setlt"; 1775 case ISD::SETLE: return "setle"; 1776 case ISD::SETNE: return "setne"; 1777 } 1778 } 1779} 1780 1781void SDNode::dump() const { 1782 std::cerr << (void*)this << ": "; 1783 1784 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 1785 if (i) std::cerr << ","; 1786 if (getValueType(i) == MVT::Other) 1787 std::cerr << "ch"; 1788 else 1789 std::cerr << MVT::getValueTypeString(getValueType(i)); 1790 } 1791 std::cerr << " = " << getOperationName(); 1792 1793 std::cerr << " "; 1794 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1795 if (i) std::cerr << ", "; 1796 std::cerr << (void*)getOperand(i).Val; 1797 if (unsigned RN = getOperand(i).ResNo) 1798 std::cerr << ":" << RN; 1799 } 1800 1801 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 1802 std::cerr << "<" << CSDN->getValue() << ">"; 1803 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 1804 std::cerr << "<" << CSDN->getValue() << ">"; 1805 } else if (const GlobalAddressSDNode *GADN = 1806 dyn_cast<GlobalAddressSDNode>(this)) { 1807 std::cerr << "<"; 1808 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; 1809 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 1810 std::cerr << "<" << FIDN->getIndex() << ">"; 1811 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 1812 std::cerr << "<" << CP->getIndex() << ">"; 1813 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 1814 std::cerr << "<"; 1815 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 1816 if (LBB) 1817 std::cerr << LBB->getName() << " "; 1818 std::cerr << (const void*)BBDN->getBasicBlock() << ">"; 1819 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) { 1820 std::cerr << "<reg #" << C2V->getReg() << ">"; 1821 } else if (const ExternalSymbolSDNode *ES = 1822 dyn_cast<ExternalSymbolSDNode>(this)) { 1823 std::cerr << "'" << ES->getSymbol() << "'"; 1824 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 1825 if (M->getValue()) 1826 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; 1827 else 1828 std::cerr << "<null:" << M->getOffset() << ">"; 1829 } 1830} 1831 1832static void DumpNodes(SDNode *N, unsigned indent) { 1833 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 1834 if (N->getOperand(i).Val->hasOneUse()) 1835 DumpNodes(N->getOperand(i).Val, indent+2); 1836 else 1837 std::cerr << "\n" << std::string(indent+2, ' ') 1838 << (void*)N->getOperand(i).Val << ": <multiple use>"; 1839 1840 1841 std::cerr << "\n" << std::string(indent, ' '); 1842 N->dump(); 1843} 1844 1845void SelectionDAG::dump() const { 1846 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; 1847 std::vector<SDNode*> Nodes(AllNodes); 1848 std::sort(Nodes.begin(), Nodes.end()); 1849 1850 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 1851 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) 1852 DumpNodes(Nodes[i], 2); 1853 } 1854 1855 DumpNodes(getRoot().Val, 2); 1856 1857 std::cerr << "\n\n"; 1858} 1859 1860