SelectionDAG.cpp revision 82c3d8f81ab20dc7571f29ffc46a5bb1b7ed8323
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/Intrinsics.h" 18#include "llvm/Assembly/Writer.h" 19#include "llvm/CodeGen/MachineBasicBlock.h" 20#include "llvm/CodeGen/MachineConstantPool.h" 21#include "llvm/Support/MathExtras.h" 22#include "llvm/Target/MRegisterInfo.h" 23#include "llvm/Target/TargetLowering.h" 24#include "llvm/Target/TargetInstrInfo.h" 25#include "llvm/Target/TargetMachine.h" 26#include "llvm/ADT/SetVector.h" 27#include "llvm/ADT/SmallVector.h" 28#include "llvm/ADT/StringExtras.h" 29#include <iostream> 30#include <set> 31#include <cmath> 32#include <algorithm> 33using namespace llvm; 34 35/// makeVTList - Return an instance of the SDVTList struct initialized with the 36/// specified members. 37static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) { 38 SDVTList Res = {VTs, NumVTs}; 39 return Res; 40} 41 42// isInvertibleForFree - Return true if there is no cost to emitting the logical 43// inverse of this node. 44static bool isInvertibleForFree(SDOperand N) { 45 if (isa<ConstantSDNode>(N.Val)) return true; 46 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse()) 47 return true; 48 return false; 49} 50 51//===----------------------------------------------------------------------===// 52// ConstantFPSDNode Class 53//===----------------------------------------------------------------------===// 54 55/// isExactlyValue - We don't rely on operator== working on double values, as 56/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 57/// As such, this method can be used to do an exact bit-for-bit comparison of 58/// two floating point values. 59bool ConstantFPSDNode::isExactlyValue(double V) const { 60 return DoubleToBits(V) == DoubleToBits(Value); 61} 62 63//===----------------------------------------------------------------------===// 64// ISD Namespace 65//===----------------------------------------------------------------------===// 66 67/// isBuildVectorAllOnes - Return true if the specified node is a 68/// BUILD_VECTOR where all of the elements are ~0 or undef. 69bool ISD::isBuildVectorAllOnes(const SDNode *N) { 70 // Look through a bit convert. 71 if (N->getOpcode() == ISD::BIT_CONVERT) 72 N = N->getOperand(0).Val; 73 74 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 75 76 unsigned i = 0, e = N->getNumOperands(); 77 78 // Skip over all of the undef values. 79 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 80 ++i; 81 82 // Do not accept an all-undef vector. 83 if (i == e) return false; 84 85 // Do not accept build_vectors that aren't all constants or which have non-~0 86 // elements. 87 SDOperand NotZero = N->getOperand(i); 88 if (isa<ConstantSDNode>(NotZero)) { 89 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue()) 90 return false; 91 } else if (isa<ConstantFPSDNode>(NotZero)) { 92 MVT::ValueType VT = NotZero.getValueType(); 93 if (VT== MVT::f64) { 94 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) != 95 (uint64_t)-1) 96 return false; 97 } else { 98 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) != 99 (uint32_t)-1) 100 return false; 101 } 102 } else 103 return false; 104 105 // Okay, we have at least one ~0 value, check to see if the rest match or are 106 // undefs. 107 for (++i; i != e; ++i) 108 if (N->getOperand(i) != NotZero && 109 N->getOperand(i).getOpcode() != ISD::UNDEF) 110 return false; 111 return true; 112} 113 114 115/// isBuildVectorAllZeros - Return true if the specified node is a 116/// BUILD_VECTOR where all of the elements are 0 or undef. 117bool ISD::isBuildVectorAllZeros(const SDNode *N) { 118 // Look through a bit convert. 119 if (N->getOpcode() == ISD::BIT_CONVERT) 120 N = N->getOperand(0).Val; 121 122 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 123 124 unsigned i = 0, e = N->getNumOperands(); 125 126 // Skip over all of the undef values. 127 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 128 ++i; 129 130 // Do not accept an all-undef vector. 131 if (i == e) return false; 132 133 // Do not accept build_vectors that aren't all constants or which have non-~0 134 // elements. 135 SDOperand Zero = N->getOperand(i); 136 if (isa<ConstantSDNode>(Zero)) { 137 if (!cast<ConstantSDNode>(Zero)->isNullValue()) 138 return false; 139 } else if (isa<ConstantFPSDNode>(Zero)) { 140 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0)) 141 return false; 142 } else 143 return false; 144 145 // Okay, we have at least one ~0 value, check to see if the rest match or are 146 // undefs. 147 for (++i; i != e; ++i) 148 if (N->getOperand(i) != Zero && 149 N->getOperand(i).getOpcode() != ISD::UNDEF) 150 return false; 151 return true; 152} 153 154/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 155/// when given the operation for (X op Y). 156ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 157 // To perform this operation, we just need to swap the L and G bits of the 158 // operation. 159 unsigned OldL = (Operation >> 2) & 1; 160 unsigned OldG = (Operation >> 1) & 1; 161 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 162 (OldL << 1) | // New G bit 163 (OldG << 2)); // New L bit. 164} 165 166/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 167/// 'op' is a valid SetCC operation. 168ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 169 unsigned Operation = Op; 170 if (isInteger) 171 Operation ^= 7; // Flip L, G, E bits, but not U. 172 else 173 Operation ^= 15; // Flip all of the condition bits. 174 if (Operation > ISD::SETTRUE2) 175 Operation &= ~8; // Don't let N and U bits get set. 176 return ISD::CondCode(Operation); 177} 178 179 180/// isSignedOp - For an integer comparison, return 1 if the comparison is a 181/// signed operation and 2 if the result is an unsigned comparison. Return zero 182/// if the operation does not depend on the sign of the input (setne and seteq). 183static int isSignedOp(ISD::CondCode Opcode) { 184 switch (Opcode) { 185 default: assert(0 && "Illegal integer setcc operation!"); 186 case ISD::SETEQ: 187 case ISD::SETNE: return 0; 188 case ISD::SETLT: 189 case ISD::SETLE: 190 case ISD::SETGT: 191 case ISD::SETGE: return 1; 192 case ISD::SETULT: 193 case ISD::SETULE: 194 case ISD::SETUGT: 195 case ISD::SETUGE: return 2; 196 } 197} 198 199/// getSetCCOrOperation - Return the result of a logical OR between different 200/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 201/// returns SETCC_INVALID if it is not possible to represent the resultant 202/// comparison. 203ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 204 bool isInteger) { 205 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 206 // Cannot fold a signed integer setcc with an unsigned integer setcc. 207 return ISD::SETCC_INVALID; 208 209 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 210 211 // If the N and U bits get set then the resultant comparison DOES suddenly 212 // care about orderedness, and is true when ordered. 213 if (Op > ISD::SETTRUE2) 214 Op &= ~16; // Clear the U bit if the N bit is set. 215 216 // Canonicalize illegal integer setcc's. 217 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT 218 Op = ISD::SETNE; 219 220 return ISD::CondCode(Op); 221} 222 223/// getSetCCAndOperation - Return the result of a logical AND between different 224/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 225/// function returns zero if it is not possible to represent the resultant 226/// comparison. 227ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 228 bool isInteger) { 229 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 230 // Cannot fold a signed setcc with an unsigned setcc. 231 return ISD::SETCC_INVALID; 232 233 // Combine all of the condition bits. 234 ISD::CondCode Result = ISD::CondCode(Op1 & Op2); 235 236 // Canonicalize illegal integer setcc's. 237 if (isInteger) { 238 switch (Result) { 239 default: break; 240 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT 241 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE 242 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE 243 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE 244 } 245 } 246 247 return Result; 248} 249 250const TargetMachine &SelectionDAG::getTarget() const { 251 return TLI.getTargetMachine(); 252} 253 254//===----------------------------------------------------------------------===// 255// SelectionDAG Class 256//===----------------------------------------------------------------------===// 257 258/// RemoveDeadNodes - This method deletes all unreachable nodes in the 259/// SelectionDAG. 260void SelectionDAG::RemoveDeadNodes() { 261 // Create a dummy node (which is not added to allnodes), that adds a reference 262 // to the root node, preventing it from being deleted. 263 HandleSDNode Dummy(getRoot()); 264 265 SmallVector<SDNode*, 128> DeadNodes; 266 267 // Add all obviously-dead nodes to the DeadNodes worklist. 268 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 269 if (I->use_empty()) 270 DeadNodes.push_back(I); 271 272 // Process the worklist, deleting the nodes and adding their uses to the 273 // worklist. 274 while (!DeadNodes.empty()) { 275 SDNode *N = DeadNodes.back(); 276 DeadNodes.pop_back(); 277 278 // Take the node out of the appropriate CSE map. 279 RemoveNodeFromCSEMaps(N); 280 281 // Next, brutally remove the operand list. This is safe to do, as there are 282 // no cycles in the graph. 283 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 284 SDNode *Operand = I->Val; 285 Operand->removeUser(N); 286 287 // Now that we removed this operand, see if there are no uses of it left. 288 if (Operand->use_empty()) 289 DeadNodes.push_back(Operand); 290 } 291 delete[] N->OperandList; 292 N->OperandList = 0; 293 N->NumOperands = 0; 294 295 // Finally, remove N itself. 296 AllNodes.erase(N); 297 } 298 299 // If the root changed (e.g. it was a dead load, update the root). 300 setRoot(Dummy.getValue()); 301} 302 303void SelectionDAG::DeleteNode(SDNode *N) { 304 assert(N->use_empty() && "Cannot delete a node that is not dead!"); 305 306 // First take this out of the appropriate CSE map. 307 RemoveNodeFromCSEMaps(N); 308 309 // Finally, remove uses due to operands of this node, remove from the 310 // AllNodes list, and delete the node. 311 DeleteNodeNotInCSEMaps(N); 312} 313 314void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 315 316 // Remove it from the AllNodes list. 317 AllNodes.remove(N); 318 319 // Drop all of the operands and decrement used nodes use counts. 320 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 321 I->Val->removeUser(N); 322 delete[] N->OperandList; 323 N->OperandList = 0; 324 N->NumOperands = 0; 325 326 delete N; 327} 328 329/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 330/// correspond to it. This is useful when we're about to delete or repurpose 331/// the node. We don't want future request for structurally identical nodes 332/// to return N anymore. 333void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 334 bool Erased = false; 335 switch (N->getOpcode()) { 336 case ISD::HANDLENODE: return; // noop. 337 case ISD::STRING: 338 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); 339 break; 340 case ISD::CONDCODE: 341 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 342 "Cond code doesn't exist!"); 343 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 344 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 345 break; 346 case ISD::ExternalSymbol: 347 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 348 break; 349 case ISD::TargetExternalSymbol: 350 Erased = 351 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 352 break; 353 case ISD::VALUETYPE: 354 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0; 355 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; 356 break; 357 default: 358 // Remove it from the CSE Map. 359 Erased = CSEMap.RemoveNode(N); 360 break; 361 } 362#ifndef NDEBUG 363 // Verify that the node was actually in one of the CSE maps, unless it has a 364 // flag result (which cannot be CSE'd) or is one of the special cases that are 365 // not subject to CSE. 366 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && 367 !N->isTargetOpcode()) { 368 N->dump(); 369 std::cerr << "\n"; 370 assert(0 && "Node is not in map!"); 371 } 372#endif 373} 374 375/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It 376/// has been taken out and modified in some way. If the specified node already 377/// exists in the CSE maps, do not modify the maps, but return the existing node 378/// instead. If it doesn't exist, add it and return null. 379/// 380SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { 381 assert(N->getNumOperands() && "This is a leaf node!"); 382 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 383 return 0; // Never add these nodes. 384 385 // Check that remaining values produced are not flags. 386 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 387 if (N->getValueType(i) == MVT::Flag) 388 return 0; // Never CSE anything that produces a flag. 389 390 SDNode *New = CSEMap.GetOrInsertNode(N); 391 if (New != N) return New; // Node already existed. 392 return 0; 393} 394 395/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 396/// were replaced with those specified. If this node is never memoized, 397/// return null, otherwise return a pointer to the slot it would take. If a 398/// node already exists with these operands, the slot will be non-null. 399SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op, 400 void *&InsertPos) { 401 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 402 return 0; // Never add these nodes. 403 404 // Check that remaining values produced are not flags. 405 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 406 if (N->getValueType(i) == MVT::Flag) 407 return 0; // Never CSE anything that produces a flag. 408 409 SelectionDAGCSEMap::NodeID ID; 410 ID.SetOpcode(N->getOpcode()); 411 ID.SetValueTypes(N->getVTList()); 412 ID.SetOperands(Op); 413 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 414} 415 416/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 417/// were replaced with those specified. If this node is never memoized, 418/// return null, otherwise return a pointer to the slot it would take. If a 419/// node already exists with these operands, the slot will be non-null. 420SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 421 SDOperand Op1, SDOperand Op2, 422 void *&InsertPos) { 423 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 424 return 0; // Never add these nodes. 425 426 // Check that remaining values produced are not flags. 427 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 428 if (N->getValueType(i) == MVT::Flag) 429 return 0; // Never CSE anything that produces a flag. 430 431 SelectionDAGCSEMap::NodeID ID; 432 ID.SetOpcode(N->getOpcode()); 433 ID.SetValueTypes(N->getVTList()); 434 ID.SetOperands(Op1, Op2); 435 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 436} 437 438 439/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 440/// were replaced with those specified. If this node is never memoized, 441/// return null, otherwise return a pointer to the slot it would take. If a 442/// node already exists with these operands, the slot will be non-null. 443SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 444 const SDOperand *Ops,unsigned NumOps, 445 void *&InsertPos) { 446 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 447 return 0; // Never add these nodes. 448 449 // Check that remaining values produced are not flags. 450 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 451 if (N->getValueType(i) == MVT::Flag) 452 return 0; // Never CSE anything that produces a flag. 453 454 SelectionDAGCSEMap::NodeID ID; 455 ID.SetOpcode(N->getOpcode()); 456 ID.SetValueTypes(N->getVTList()); 457 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 458 ID.AddInteger(LD->getAddressingMode()); 459 ID.AddInteger(LD->getExtensionType()); 460 ID.AddInteger(LD->getLoadVT()); 461 ID.AddPointer(LD->getSrcValue()); 462 ID.AddInteger(LD->getSrcValueOffset()); 463 ID.AddInteger(LD->getAlignment()); 464 ID.AddInteger(LD->isVolatile()); 465 } 466 ID.SetOperands(Ops, NumOps); 467 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 468} 469 470 471SelectionDAG::~SelectionDAG() { 472 while (!AllNodes.empty()) { 473 SDNode *N = AllNodes.begin(); 474 N->SetNextInBucket(0); 475 delete [] N->OperandList; 476 N->OperandList = 0; 477 N->NumOperands = 0; 478 AllNodes.pop_front(); 479 } 480} 481 482SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { 483 if (Op.getValueType() == VT) return Op; 484 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); 485 return getNode(ISD::AND, Op.getValueType(), Op, 486 getConstant(Imm, Op.getValueType())); 487} 488 489SDOperand SelectionDAG::getString(const std::string &Val) { 490 StringSDNode *&N = StringNodes[Val]; 491 if (!N) { 492 N = new StringSDNode(Val); 493 AllNodes.push_back(N); 494 } 495 return SDOperand(N, 0); 496} 497 498SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) { 499 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 500 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!"); 501 502 // Mask out any bits that are not valid for this constant. 503 Val &= MVT::getIntVTBitMask(VT); 504 505 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; 506 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 507 ID.AddInteger(Val); 508 void *IP = 0; 509 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 510 return SDOperand(E, 0); 511 SDNode *N = new ConstantSDNode(isT, Val, VT); 512 CSEMap.InsertNode(N, IP); 513 AllNodes.push_back(N); 514 return SDOperand(N, 0); 515} 516 517 518SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT, 519 bool isTarget) { 520 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 521 if (VT == MVT::f32) 522 Val = (float)Val; // Mask out extra precision. 523 524 // Do the map lookup using the actual bit pattern for the floating point 525 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 526 // we don't have issues with SNANs. 527 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; 528 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 529 ID.AddInteger(DoubleToBits(Val)); 530 void *IP = 0; 531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 532 return SDOperand(E, 0); 533 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT); 534 CSEMap.InsertNode(N, IP); 535 AllNodes.push_back(N); 536 return SDOperand(N, 0); 537} 538 539SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, 540 MVT::ValueType VT, int Offset, 541 bool isTargetGA) { 542 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; 543 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 544 ID.AddPointer(GV); 545 ID.AddInteger(Offset); 546 void *IP = 0; 547 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 548 return SDOperand(E, 0); 549 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset); 550 CSEMap.InsertNode(N, IP); 551 AllNodes.push_back(N); 552 return SDOperand(N, 0); 553} 554 555SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT, 556 bool isTarget) { 557 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; 558 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 559 ID.AddInteger(FI); 560 void *IP = 0; 561 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 562 return SDOperand(E, 0); 563 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget); 564 CSEMap.InsertNode(N, IP); 565 AllNodes.push_back(N); 566 return SDOperand(N, 0); 567} 568 569SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){ 570 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; 571 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 572 ID.AddInteger(JTI); 573 void *IP = 0; 574 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 575 return SDOperand(E, 0); 576 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget); 577 CSEMap.InsertNode(N, IP); 578 AllNodes.push_back(N); 579 return SDOperand(N, 0); 580} 581 582SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT, 583 unsigned Alignment, int Offset, 584 bool isTarget) { 585 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 586 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 587 ID.AddInteger(Alignment); 588 ID.AddInteger(Offset); 589 ID.AddPointer(C); 590 void *IP = 0; 591 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 592 return SDOperand(E, 0); 593 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment); 594 CSEMap.InsertNode(N, IP); 595 AllNodes.push_back(N); 596 return SDOperand(N, 0); 597} 598 599 600SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, 601 MVT::ValueType VT, 602 unsigned Alignment, int Offset, 603 bool isTarget) { 604 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 605 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT)); 606 ID.AddInteger(Alignment); 607 ID.AddInteger(Offset); 608 C->AddSelectionDAGCSEId(&ID); 609 void *IP = 0; 610 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 611 return SDOperand(E, 0); 612 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment); 613 CSEMap.InsertNode(N, IP); 614 AllNodes.push_back(N); 615 return SDOperand(N, 0); 616} 617 618 619SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 620 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other)); 621 ID.AddPointer(MBB); 622 void *IP = 0; 623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 624 return SDOperand(E, 0); 625 SDNode *N = new BasicBlockSDNode(MBB); 626 CSEMap.InsertNode(N, IP); 627 AllNodes.push_back(N); 628 return SDOperand(N, 0); 629} 630 631SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { 632 if ((unsigned)VT >= ValueTypeNodes.size()) 633 ValueTypeNodes.resize(VT+1); 634 if (ValueTypeNodes[VT] == 0) { 635 ValueTypeNodes[VT] = new VTSDNode(VT); 636 AllNodes.push_back(ValueTypeNodes[VT]); 637 } 638 639 return SDOperand(ValueTypeNodes[VT], 0); 640} 641 642SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { 643 SDNode *&N = ExternalSymbols[Sym]; 644 if (N) return SDOperand(N, 0); 645 N = new ExternalSymbolSDNode(false, Sym, VT); 646 AllNodes.push_back(N); 647 return SDOperand(N, 0); 648} 649 650SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, 651 MVT::ValueType VT) { 652 SDNode *&N = TargetExternalSymbols[Sym]; 653 if (N) return SDOperand(N, 0); 654 N = new ExternalSymbolSDNode(true, Sym, VT); 655 AllNodes.push_back(N); 656 return SDOperand(N, 0); 657} 658 659SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { 660 if ((unsigned)Cond >= CondCodeNodes.size()) 661 CondCodeNodes.resize(Cond+1); 662 663 if (CondCodeNodes[Cond] == 0) { 664 CondCodeNodes[Cond] = new CondCodeSDNode(Cond); 665 AllNodes.push_back(CondCodeNodes[Cond]); 666 } 667 return SDOperand(CondCodeNodes[Cond], 0); 668} 669 670SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { 671 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT)); 672 ID.AddInteger(RegNo); 673 void *IP = 0; 674 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 675 return SDOperand(E, 0); 676 SDNode *N = new RegisterSDNode(RegNo, VT); 677 CSEMap.InsertNode(N, IP); 678 AllNodes.push_back(N); 679 return SDOperand(N, 0); 680} 681 682SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { 683 assert((!V || isa<PointerType>(V->getType())) && 684 "SrcValue is not a pointer?"); 685 686 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other)); 687 ID.AddPointer(V); 688 ID.AddInteger(Offset); 689 void *IP = 0; 690 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 691 return SDOperand(E, 0); 692 SDNode *N = new SrcValueSDNode(V, Offset); 693 CSEMap.InsertNode(N, IP); 694 AllNodes.push_back(N); 695 return SDOperand(N, 0); 696} 697 698SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1, 699 SDOperand N2, ISD::CondCode Cond) { 700 // These setcc operations always fold. 701 switch (Cond) { 702 default: break; 703 case ISD::SETFALSE: 704 case ISD::SETFALSE2: return getConstant(0, VT); 705 case ISD::SETTRUE: 706 case ISD::SETTRUE2: return getConstant(1, VT); 707 708 case ISD::SETOEQ: 709 case ISD::SETOGT: 710 case ISD::SETOGE: 711 case ISD::SETOLT: 712 case ISD::SETOLE: 713 case ISD::SETONE: 714 case ISD::SETO: 715 case ISD::SETUO: 716 case ISD::SETUEQ: 717 case ISD::SETUNE: 718 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!"); 719 break; 720 } 721 722 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { 723 uint64_t C2 = N2C->getValue(); 724 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { 725 uint64_t C1 = N1C->getValue(); 726 727 // Sign extend the operands if required 728 if (ISD::isSignedIntSetCC(Cond)) { 729 C1 = N1C->getSignExtended(); 730 C2 = N2C->getSignExtended(); 731 } 732 733 switch (Cond) { 734 default: assert(0 && "Unknown integer setcc!"); 735 case ISD::SETEQ: return getConstant(C1 == C2, VT); 736 case ISD::SETNE: return getConstant(C1 != C2, VT); 737 case ISD::SETULT: return getConstant(C1 < C2, VT); 738 case ISD::SETUGT: return getConstant(C1 > C2, VT); 739 case ISD::SETULE: return getConstant(C1 <= C2, VT); 740 case ISD::SETUGE: return getConstant(C1 >= C2, VT); 741 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); 742 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); 743 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); 744 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); 745 } 746 } else { 747 // If the LHS is a ZERO_EXTEND, perform the comparison on the input. 748 if (N1.getOpcode() == ISD::ZERO_EXTEND) { 749 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType()); 750 751 // If the comparison constant has bits in the upper part, the 752 // zero-extended value could never match. 753 if (C2 & (~0ULL << InSize)) { 754 unsigned VSize = MVT::getSizeInBits(N1.getValueType()); 755 switch (Cond) { 756 case ISD::SETUGT: 757 case ISD::SETUGE: 758 case ISD::SETEQ: return getConstant(0, VT); 759 case ISD::SETULT: 760 case ISD::SETULE: 761 case ISD::SETNE: return getConstant(1, VT); 762 case ISD::SETGT: 763 case ISD::SETGE: 764 // True if the sign bit of C2 is set. 765 return getConstant((C2 & (1ULL << VSize)) != 0, VT); 766 case ISD::SETLT: 767 case ISD::SETLE: 768 // True if the sign bit of C2 isn't set. 769 return getConstant((C2 & (1ULL << VSize)) == 0, VT); 770 default: 771 break; 772 } 773 } 774 775 // Otherwise, we can perform the comparison with the low bits. 776 switch (Cond) { 777 case ISD::SETEQ: 778 case ISD::SETNE: 779 case ISD::SETUGT: 780 case ISD::SETUGE: 781 case ISD::SETULT: 782 case ISD::SETULE: 783 return getSetCC(VT, N1.getOperand(0), 784 getConstant(C2, N1.getOperand(0).getValueType()), 785 Cond); 786 default: 787 break; // todo, be more careful with signed comparisons 788 } 789 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG && 790 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 791 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT(); 792 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); 793 MVT::ValueType ExtDstTy = N1.getValueType(); 794 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); 795 796 // If the extended part has any inconsistent bits, it cannot ever 797 // compare equal. In other words, they have to be all ones or all 798 // zeros. 799 uint64_t ExtBits = 800 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); 801 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits) 802 return getConstant(Cond == ISD::SETNE, VT); 803 804 // Otherwise, make this a use of a zext. 805 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy), 806 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy), 807 Cond); 808 } 809 810 uint64_t MinVal, MaxVal; 811 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0)); 812 if (ISD::isSignedIntSetCC(Cond)) { 813 MinVal = 1ULL << (OperandBitSize-1); 814 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. 815 MaxVal = ~0ULL >> (65-OperandBitSize); 816 else 817 MaxVal = 0; 818 } else { 819 MinVal = 0; 820 MaxVal = ~0ULL >> (64-OperandBitSize); 821 } 822 823 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 824 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 825 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true 826 --C2; // X >= C1 --> X > (C1-1) 827 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 828 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); 829 } 830 831 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 832 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true 833 ++C2; // X <= C1 --> X < (C1+1) 834 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 835 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); 836 } 837 838 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal) 839 return getConstant(0, VT); // X < MIN --> false 840 841 // Canonicalize setgt X, Min --> setne X, Min 842 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal) 843 return getSetCC(VT, N1, N2, ISD::SETNE); 844 845 // If we have setult X, 1, turn it into seteq X, 0 846 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1) 847 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()), 848 ISD::SETEQ); 849 // If we have setugt X, Max-1, turn it into seteq X, Max 850 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1) 851 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()), 852 ISD::SETEQ); 853 854 // If we have "setcc X, C1", check to see if we can shrink the immediate 855 // by changing cc. 856 857 // SETUGT X, SINTMAX -> SETLT X, 0 858 if (Cond == ISD::SETUGT && OperandBitSize != 1 && 859 C2 == (~0ULL >> (65-OperandBitSize))) 860 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT); 861 862 // FIXME: Implement the rest of these. 863 864 865 // Fold bit comparisons when we can. 866 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 867 VT == N1.getValueType() && N1.getOpcode() == ISD::AND) 868 if (ConstantSDNode *AndRHS = 869 dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 870 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 871 // Perform the xform if the AND RHS is a single bit. 872 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { 873 return getNode(ISD::SRL, VT, N1, 874 getConstant(Log2_64(AndRHS->getValue()), 875 TLI.getShiftAmountTy())); 876 } 877 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) { 878 // (X & 8) == 8 --> (X & 8) >> 3 879 // Perform the xform if C2 is a single bit. 880 if ((C2 & (C2-1)) == 0) { 881 return getNode(ISD::SRL, VT, N1, 882 getConstant(Log2_64(C2),TLI.getShiftAmountTy())); 883 } 884 } 885 } 886 } 887 } else if (isa<ConstantSDNode>(N1.Val)) { 888 // Ensure that the constant occurs on the RHS. 889 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 890 } 891 892 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) 893 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { 894 double C1 = N1C->getValue(), C2 = N2C->getValue(); 895 896 switch (Cond) { 897 default: break; // FIXME: Implement the rest of these! 898 case ISD::SETEQ: return getConstant(C1 == C2, VT); 899 case ISD::SETNE: return getConstant(C1 != C2, VT); 900 case ISD::SETLT: return getConstant(C1 < C2, VT); 901 case ISD::SETGT: return getConstant(C1 > C2, VT); 902 case ISD::SETLE: return getConstant(C1 <= C2, VT); 903 case ISD::SETGE: return getConstant(C1 >= C2, VT); 904 } 905 } else { 906 // Ensure that the constant occurs on the RHS. 907 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 908 } 909 910 // Could not fold it. 911 return SDOperand(); 912} 913 914/// getNode - Gets or creates the specified node. 915/// 916SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { 917 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT)); 918 void *IP = 0; 919 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 920 return SDOperand(E, 0); 921 SDNode *N = new SDNode(Opcode, VT); 922 CSEMap.InsertNode(N, IP); 923 924 AllNodes.push_back(N); 925 return SDOperand(N, 0); 926} 927 928SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 929 SDOperand Operand) { 930 unsigned Tmp1; 931 // Constant fold unary operations with an integer constant operand. 932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { 933 uint64_t Val = C->getValue(); 934 switch (Opcode) { 935 default: break; 936 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); 937 case ISD::ANY_EXTEND: 938 case ISD::ZERO_EXTEND: return getConstant(Val, VT); 939 case ISD::TRUNCATE: return getConstant(Val, VT); 940 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); 941 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); 942 case ISD::BIT_CONVERT: 943 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) 944 return getConstantFP(BitsToFloat(Val), VT); 945 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) 946 return getConstantFP(BitsToDouble(Val), VT); 947 break; 948 case ISD::BSWAP: 949 switch(VT) { 950 default: assert(0 && "Invalid bswap!"); break; 951 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT); 952 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT); 953 case MVT::i64: return getConstant(ByteSwap_64(Val), VT); 954 } 955 break; 956 case ISD::CTPOP: 957 switch(VT) { 958 default: assert(0 && "Invalid ctpop!"); break; 959 case MVT::i1: return getConstant(Val != 0, VT); 960 case MVT::i8: 961 Tmp1 = (unsigned)Val & 0xFF; 962 return getConstant(CountPopulation_32(Tmp1), VT); 963 case MVT::i16: 964 Tmp1 = (unsigned)Val & 0xFFFF; 965 return getConstant(CountPopulation_32(Tmp1), VT); 966 case MVT::i32: 967 return getConstant(CountPopulation_32((unsigned)Val), VT); 968 case MVT::i64: 969 return getConstant(CountPopulation_64(Val), VT); 970 } 971 case ISD::CTLZ: 972 switch(VT) { 973 default: assert(0 && "Invalid ctlz!"); break; 974 case MVT::i1: return getConstant(Val == 0, VT); 975 case MVT::i8: 976 Tmp1 = (unsigned)Val & 0xFF; 977 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT); 978 case MVT::i16: 979 Tmp1 = (unsigned)Val & 0xFFFF; 980 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT); 981 case MVT::i32: 982 return getConstant(CountLeadingZeros_32((unsigned)Val), VT); 983 case MVT::i64: 984 return getConstant(CountLeadingZeros_64(Val), VT); 985 } 986 case ISD::CTTZ: 987 switch(VT) { 988 default: assert(0 && "Invalid cttz!"); break; 989 case MVT::i1: return getConstant(Val == 0, VT); 990 case MVT::i8: 991 Tmp1 = (unsigned)Val | 0x100; 992 return getConstant(CountTrailingZeros_32(Tmp1), VT); 993 case MVT::i16: 994 Tmp1 = (unsigned)Val | 0x10000; 995 return getConstant(CountTrailingZeros_32(Tmp1), VT); 996 case MVT::i32: 997 return getConstant(CountTrailingZeros_32((unsigned)Val), VT); 998 case MVT::i64: 999 return getConstant(CountTrailingZeros_64(Val), VT); 1000 } 1001 } 1002 } 1003 1004 // Constant fold unary operations with an floating point constant operand. 1005 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) 1006 switch (Opcode) { 1007 case ISD::FNEG: 1008 return getConstantFP(-C->getValue(), VT); 1009 case ISD::FABS: 1010 return getConstantFP(fabs(C->getValue()), VT); 1011 case ISD::FP_ROUND: 1012 case ISD::FP_EXTEND: 1013 return getConstantFP(C->getValue(), VT); 1014 case ISD::FP_TO_SINT: 1015 return getConstant((int64_t)C->getValue(), VT); 1016 case ISD::FP_TO_UINT: 1017 return getConstant((uint64_t)C->getValue(), VT); 1018 case ISD::BIT_CONVERT: 1019 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) 1020 return getConstant(FloatToBits(C->getValue()), VT); 1021 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) 1022 return getConstant(DoubleToBits(C->getValue()), VT); 1023 break; 1024 } 1025 1026 unsigned OpOpcode = Operand.Val->getOpcode(); 1027 switch (Opcode) { 1028 case ISD::TokenFactor: 1029 return Operand; // Factor of one node? No factor. 1030 case ISD::SIGN_EXTEND: 1031 if (Operand.getValueType() == VT) return Operand; // noop extension 1032 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!"); 1033 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 1034 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1035 break; 1036 case ISD::ZERO_EXTEND: 1037 if (Operand.getValueType() == VT) return Operand; // noop extension 1038 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!"); 1039 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 1040 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); 1041 break; 1042 case ISD::ANY_EXTEND: 1043 if (Operand.getValueType() == VT) return Operand; // noop extension 1044 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!"); 1045 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) 1046 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 1047 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1048 break; 1049 case ISD::TRUNCATE: 1050 if (Operand.getValueType() == VT) return Operand; // noop truncate 1051 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!"); 1052 if (OpOpcode == ISD::TRUNCATE) 1053 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1054 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 1055 OpOpcode == ISD::ANY_EXTEND) { 1056 // If the source is smaller than the dest, we still need an extend. 1057 if (Operand.Val->getOperand(0).getValueType() < VT) 1058 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1059 else if (Operand.Val->getOperand(0).getValueType() > VT) 1060 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1061 else 1062 return Operand.Val->getOperand(0); 1063 } 1064 break; 1065 case ISD::BIT_CONVERT: 1066 // Basic sanity checking. 1067 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()) 1068 && "Cannot BIT_CONVERT between two different types!"); 1069 if (VT == Operand.getValueType()) return Operand; // noop conversion. 1070 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) 1071 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0)); 1072 if (OpOpcode == ISD::UNDEF) 1073 return getNode(ISD::UNDEF, VT); 1074 break; 1075 case ISD::SCALAR_TO_VECTOR: 1076 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) && 1077 MVT::getVectorBaseType(VT) == Operand.getValueType() && 1078 "Illegal SCALAR_TO_VECTOR node!"); 1079 break; 1080 case ISD::FNEG: 1081 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) 1082 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), 1083 Operand.Val->getOperand(0)); 1084 if (OpOpcode == ISD::FNEG) // --X -> X 1085 return Operand.Val->getOperand(0); 1086 break; 1087 case ISD::FABS: 1088 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 1089 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); 1090 break; 1091 } 1092 1093 SDNode *N; 1094 SDVTList VTs = getVTList(VT); 1095 if (VT != MVT::Flag) { // Don't CSE flag producing nodes 1096 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand); 1097 void *IP = 0; 1098 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1099 return SDOperand(E, 0); 1100 N = new SDNode(Opcode, Operand); 1101 N->setValueTypes(VTs); 1102 CSEMap.InsertNode(N, IP); 1103 } else { 1104 N = new SDNode(Opcode, Operand); 1105 N->setValueTypes(VTs); 1106 } 1107 AllNodes.push_back(N); 1108 return SDOperand(N, 0); 1109} 1110 1111 1112 1113SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1114 SDOperand N1, SDOperand N2) { 1115#ifndef NDEBUG 1116 switch (Opcode) { 1117 case ISD::TokenFactor: 1118 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 1119 N2.getValueType() == MVT::Other && "Invalid token factor!"); 1120 break; 1121 case ISD::AND: 1122 case ISD::OR: 1123 case ISD::XOR: 1124 case ISD::UDIV: 1125 case ISD::UREM: 1126 case ISD::MULHU: 1127 case ISD::MULHS: 1128 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); 1129 // fall through 1130 case ISD::ADD: 1131 case ISD::SUB: 1132 case ISD::MUL: 1133 case ISD::SDIV: 1134 case ISD::SREM: 1135 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); 1136 // fall through. 1137 case ISD::FADD: 1138 case ISD::FSUB: 1139 case ISD::FMUL: 1140 case ISD::FDIV: 1141 case ISD::FREM: 1142 assert(N1.getValueType() == N2.getValueType() && 1143 N1.getValueType() == VT && "Binary operator types must match!"); 1144 break; 1145 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. 1146 assert(N1.getValueType() == VT && 1147 MVT::isFloatingPoint(N1.getValueType()) && 1148 MVT::isFloatingPoint(N2.getValueType()) && 1149 "Invalid FCOPYSIGN!"); 1150 break; 1151 case ISD::SHL: 1152 case ISD::SRA: 1153 case ISD::SRL: 1154 case ISD::ROTL: 1155 case ISD::ROTR: 1156 assert(VT == N1.getValueType() && 1157 "Shift operators return type must be the same as their first arg"); 1158 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && 1159 VT != MVT::i1 && "Shifts only work on integers"); 1160 break; 1161 case ISD::FP_ROUND_INREG: { 1162 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1163 assert(VT == N1.getValueType() && "Not an inreg round!"); 1164 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && 1165 "Cannot FP_ROUND_INREG integer types"); 1166 assert(EVT <= VT && "Not rounding down!"); 1167 break; 1168 } 1169 case ISD::AssertSext: 1170 case ISD::AssertZext: 1171 case ISD::SIGN_EXTEND_INREG: { 1172 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1173 assert(VT == N1.getValueType() && "Not an inreg extend!"); 1174 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && 1175 "Cannot *_EXTEND_INREG FP types"); 1176 assert(EVT <= VT && "Not extending!"); 1177 } 1178 1179 default: break; 1180 } 1181#endif 1182 1183 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1184 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1185 if (N1C) { 1186 if (Opcode == ISD::SIGN_EXTEND_INREG) { 1187 int64_t Val = N1C->getValue(); 1188 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT()); 1189 Val <<= 64-FromBits; 1190 Val >>= 64-FromBits; 1191 return getConstant(Val, VT); 1192 } 1193 1194 if (N2C) { 1195 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); 1196 switch (Opcode) { 1197 case ISD::ADD: return getConstant(C1 + C2, VT); 1198 case ISD::SUB: return getConstant(C1 - C2, VT); 1199 case ISD::MUL: return getConstant(C1 * C2, VT); 1200 case ISD::UDIV: 1201 if (C2) return getConstant(C1 / C2, VT); 1202 break; 1203 case ISD::UREM : 1204 if (C2) return getConstant(C1 % C2, VT); 1205 break; 1206 case ISD::SDIV : 1207 if (C2) return getConstant(N1C->getSignExtended() / 1208 N2C->getSignExtended(), VT); 1209 break; 1210 case ISD::SREM : 1211 if (C2) return getConstant(N1C->getSignExtended() % 1212 N2C->getSignExtended(), VT); 1213 break; 1214 case ISD::AND : return getConstant(C1 & C2, VT); 1215 case ISD::OR : return getConstant(C1 | C2, VT); 1216 case ISD::XOR : return getConstant(C1 ^ C2, VT); 1217 case ISD::SHL : return getConstant(C1 << C2, VT); 1218 case ISD::SRL : return getConstant(C1 >> C2, VT); 1219 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); 1220 case ISD::ROTL : 1221 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)), 1222 VT); 1223 case ISD::ROTR : 1224 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), 1225 VT); 1226 default: break; 1227 } 1228 } else { // Cannonicalize constant to RHS if commutative 1229 if (isCommutativeBinOp(Opcode)) { 1230 std::swap(N1C, N2C); 1231 std::swap(N1, N2); 1232 } 1233 } 1234 } 1235 1236 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); 1237 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); 1238 if (N1CFP) { 1239 if (N2CFP) { 1240 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); 1241 switch (Opcode) { 1242 case ISD::FADD: return getConstantFP(C1 + C2, VT); 1243 case ISD::FSUB: return getConstantFP(C1 - C2, VT); 1244 case ISD::FMUL: return getConstantFP(C1 * C2, VT); 1245 case ISD::FDIV: 1246 if (C2) return getConstantFP(C1 / C2, VT); 1247 break; 1248 case ISD::FREM : 1249 if (C2) return getConstantFP(fmod(C1, C2), VT); 1250 break; 1251 case ISD::FCOPYSIGN: { 1252 union { 1253 double F; 1254 uint64_t I; 1255 } u1; 1256 union { 1257 double F; 1258 int64_t I; 1259 } u2; 1260 u1.F = C1; 1261 u2.F = C2; 1262 if (u2.I < 0) // Sign bit of RHS set? 1263 u1.I |= 1ULL << 63; // Set the sign bit of the LHS. 1264 else 1265 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS. 1266 return getConstantFP(u1.F, VT); 1267 } 1268 default: break; 1269 } 1270 } else { // Cannonicalize constant to RHS if commutative 1271 if (isCommutativeBinOp(Opcode)) { 1272 std::swap(N1CFP, N2CFP); 1273 std::swap(N1, N2); 1274 } 1275 } 1276 } 1277 1278 // Canonicalize an UNDEF to the RHS, even over a constant. 1279 if (N1.getOpcode() == ISD::UNDEF) { 1280 if (isCommutativeBinOp(Opcode)) { 1281 std::swap(N1, N2); 1282 } else { 1283 switch (Opcode) { 1284 case ISD::FP_ROUND_INREG: 1285 case ISD::SIGN_EXTEND_INREG: 1286 case ISD::SUB: 1287 case ISD::FSUB: 1288 case ISD::FDIV: 1289 case ISD::FREM: 1290 case ISD::SRA: 1291 return N1; // fold op(undef, arg2) -> undef 1292 case ISD::UDIV: 1293 case ISD::SDIV: 1294 case ISD::UREM: 1295 case ISD::SREM: 1296 case ISD::SRL: 1297 case ISD::SHL: 1298 return getConstant(0, VT); // fold op(undef, arg2) -> 0 1299 } 1300 } 1301 } 1302 1303 // Fold a bunch of operators when the RHS is undef. 1304 if (N2.getOpcode() == ISD::UNDEF) { 1305 switch (Opcode) { 1306 case ISD::ADD: 1307 case ISD::SUB: 1308 case ISD::FADD: 1309 case ISD::FSUB: 1310 case ISD::FMUL: 1311 case ISD::FDIV: 1312 case ISD::FREM: 1313 case ISD::UDIV: 1314 case ISD::SDIV: 1315 case ISD::UREM: 1316 case ISD::SREM: 1317 case ISD::XOR: 1318 return N2; // fold op(arg1, undef) -> undef 1319 case ISD::MUL: 1320 case ISD::AND: 1321 case ISD::SRL: 1322 case ISD::SHL: 1323 return getConstant(0, VT); // fold op(arg1, undef) -> 0 1324 case ISD::OR: 1325 return getConstant(MVT::getIntVTBitMask(VT), VT); 1326 case ISD::SRA: 1327 return N1; 1328 } 1329 } 1330 1331 // Finally, fold operations that do not require constants. 1332 switch (Opcode) { 1333 case ISD::FP_ROUND_INREG: 1334 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 1335 break; 1336 case ISD::SIGN_EXTEND_INREG: { 1337 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1338 if (EVT == VT) return N1; // Not actually extending 1339 break; 1340 } 1341 case ISD::EXTRACT_ELEMENT: 1342 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!"); 1343 1344 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding 1345 // 64-bit integers into 32-bit parts. Instead of building the extract of 1346 // the BUILD_PAIR, only to have legalize rip it apart, just do it now. 1347 if (N1.getOpcode() == ISD::BUILD_PAIR) 1348 return N1.getOperand(N2C->getValue()); 1349 1350 // EXTRACT_ELEMENT of a constant int is also very common. 1351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 1352 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue(); 1353 return getConstant(C->getValue() >> Shift, VT); 1354 } 1355 break; 1356 1357 // FIXME: figure out how to safely handle things like 1358 // int foo(int x) { return 1 << (x & 255); } 1359 // int bar() { return foo(256); } 1360#if 0 1361 case ISD::SHL: 1362 case ISD::SRL: 1363 case ISD::SRA: 1364 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && 1365 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) 1366 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1367 else if (N2.getOpcode() == ISD::AND) 1368 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { 1369 // If the and is only masking out bits that cannot effect the shift, 1370 // eliminate the and. 1371 unsigned NumBits = MVT::getSizeInBits(VT); 1372 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1373 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1374 } 1375 break; 1376#endif 1377 } 1378 1379 // Memoize this node if possible. 1380 SDNode *N; 1381 SDVTList VTs = getVTList(VT); 1382 if (VT != MVT::Flag) { 1383 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2); 1384 void *IP = 0; 1385 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1386 return SDOperand(E, 0); 1387 N = new SDNode(Opcode, N1, N2); 1388 N->setValueTypes(VTs); 1389 CSEMap.InsertNode(N, IP); 1390 } else { 1391 N = new SDNode(Opcode, N1, N2); 1392 N->setValueTypes(VTs); 1393 } 1394 1395 AllNodes.push_back(N); 1396 return SDOperand(N, 0); 1397} 1398 1399SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1400 SDOperand N1, SDOperand N2, SDOperand N3) { 1401 // Perform various simplifications. 1402 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1403 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1404 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); 1405 switch (Opcode) { 1406 case ISD::SETCC: { 1407 // Use SimplifySetCC to simplify SETCC's. 1408 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); 1409 if (Simp.Val) return Simp; 1410 break; 1411 } 1412 case ISD::SELECT: 1413 if (N1C) 1414 if (N1C->getValue()) 1415 return N2; // select true, X, Y -> X 1416 else 1417 return N3; // select false, X, Y -> Y 1418 1419 if (N2 == N3) return N2; // select C, X, X -> X 1420 break; 1421 case ISD::BRCOND: 1422 if (N2C) 1423 if (N2C->getValue()) // Unconditional branch 1424 return getNode(ISD::BR, MVT::Other, N1, N3); 1425 else 1426 return N1; // Never-taken branch 1427 break; 1428 case ISD::VECTOR_SHUFFLE: 1429 assert(VT == N1.getValueType() && VT == N2.getValueType() && 1430 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) && 1431 N3.getOpcode() == ISD::BUILD_VECTOR && 1432 MVT::getVectorNumElements(VT) == N3.getNumOperands() && 1433 "Illegal VECTOR_SHUFFLE node!"); 1434 break; 1435 } 1436 1437 // Memoize node if it doesn't produce a flag. 1438 SDNode *N; 1439 SDVTList VTs = getVTList(VT); 1440 if (VT != MVT::Flag) { 1441 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3); 1442 void *IP = 0; 1443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1444 return SDOperand(E, 0); 1445 N = new SDNode(Opcode, N1, N2, N3); 1446 N->setValueTypes(VTs); 1447 CSEMap.InsertNode(N, IP); 1448 } else { 1449 N = new SDNode(Opcode, N1, N2, N3); 1450 N->setValueTypes(VTs); 1451 } 1452 AllNodes.push_back(N); 1453 return SDOperand(N, 0); 1454} 1455 1456SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1457 SDOperand N1, SDOperand N2, SDOperand N3, 1458 SDOperand N4) { 1459 SDOperand Ops[] = { N1, N2, N3, N4 }; 1460 return getNode(Opcode, VT, Ops, 4); 1461} 1462 1463SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1464 SDOperand N1, SDOperand N2, SDOperand N3, 1465 SDOperand N4, SDOperand N5) { 1466 SDOperand Ops[] = { N1, N2, N3, N4, N5 }; 1467 return getNode(Opcode, VT, Ops, 5); 1468} 1469 1470SDOperand SelectionDAG::getLoad(MVT::ValueType VT, 1471 SDOperand Chain, SDOperand Ptr, 1472 const Value *SV, int SVOffset, 1473 bool isVolatile) { 1474 // FIXME: Alignment == 1 for now. 1475 unsigned Alignment = 1; 1476 SDVTList VTs = getVTList(VT, MVT::Other); 1477 SDOperand Undef = getNode(ISD::UNDEF, VT); 1478 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef); 1479 ID.AddInteger(ISD::UNINDEXED); 1480 ID.AddInteger(ISD::NON_EXTLOAD); 1481 ID.AddInteger(VT); 1482 ID.AddPointer(SV); 1483 ID.AddInteger(SVOffset); 1484 ID.AddInteger(Alignment); 1485 ID.AddInteger(isVolatile); 1486 void *IP = 0; 1487 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1488 return SDOperand(E, 0); 1489 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::NON_EXTLOAD, VT, 1490 SV, SVOffset, Alignment, isVolatile); 1491 N->setValueTypes(VTs); 1492 CSEMap.InsertNode(N, IP); 1493 AllNodes.push_back(N); 1494 return SDOperand(N, 0); 1495} 1496 1497SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT, 1498 SDOperand Chain, SDOperand Ptr, const Value *SV, 1499 int SVOffset, MVT::ValueType EVT, 1500 bool isVolatile) { 1501 // If they are asking for an extending load from/to the same thing, return a 1502 // normal load. 1503 if (VT == EVT) 1504 ExtType = ISD::NON_EXTLOAD; 1505 1506 if (MVT::isVector(VT)) 1507 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!"); 1508 else 1509 assert(EVT < VT && "Should only be an extending load, not truncating!"); 1510 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) && 1511 "Cannot sign/zero extend a FP/Vector load!"); 1512 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) && 1513 "Cannot convert from FP to Int or Int -> FP!"); 1514 1515 // FIXME: Alignment == 1 for now. 1516 unsigned Alignment = 1; 1517 SDVTList VTs = getVTList(VT, MVT::Other); 1518 SDOperand Undef = getNode(ISD::UNDEF, VT); 1519 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef); 1520 ID.AddInteger(ISD::UNINDEXED); 1521 ID.AddInteger(ExtType); 1522 ID.AddInteger(EVT); 1523 ID.AddPointer(SV); 1524 ID.AddInteger(SVOffset); 1525 ID.AddInteger(Alignment); 1526 ID.AddInteger(isVolatile); 1527 void *IP = 0; 1528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1529 return SDOperand(E, 0); 1530 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ExtType, EVT, SV, SVOffset, 1531 Alignment, isVolatile); 1532 N->setValueTypes(VTs); 1533 CSEMap.InsertNode(N, IP); 1534 AllNodes.push_back(N); 1535 return SDOperand(N, 0); 1536} 1537 1538SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT, 1539 SDOperand Chain, SDOperand Ptr, 1540 SDOperand SV) { 1541 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32), 1542 getValueType(EVT) }; 1543 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5); 1544} 1545 1546SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value, 1547 SDOperand Ptr, SDOperand SV) { 1548 SDVTList VTs = getVTList(MVT::Other); 1549 SDOperand Ops[] = { Chain, Value, Ptr, SV }; 1550 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4); 1551 void *IP = 0; 1552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1553 return SDOperand(E, 0); 1554 SDNode *N = new SDNode(ISD::STORE, Chain, Value, Ptr, SV); 1555 N->setValueTypes(VTs); 1556 CSEMap.InsertNode(N, IP); 1557 AllNodes.push_back(N); 1558 return SDOperand(N, 0); 1559} 1560 1561SDOperand SelectionDAG::getVAArg(MVT::ValueType VT, 1562 SDOperand Chain, SDOperand Ptr, 1563 SDOperand SV) { 1564 SDOperand Ops[] = { Chain, Ptr, SV }; 1565 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3); 1566} 1567 1568SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1569 const SDOperand *Ops, unsigned NumOps) { 1570 switch (NumOps) { 1571 case 0: return getNode(Opcode, VT); 1572 case 1: return getNode(Opcode, VT, Ops[0]); 1573 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); 1574 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); 1575 default: break; 1576 } 1577 1578 switch (Opcode) { 1579 default: break; 1580 case ISD::TRUNCSTORE: { 1581 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!"); 1582 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT(); 1583#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store 1584 // If this is a truncating store of a constant, convert to the desired type 1585 // and store it instead. 1586 if (isa<Constant>(Ops[0])) { 1587 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); 1588 if (isa<Constant>(Op)) 1589 N1 = Op; 1590 } 1591 // Also for ConstantFP? 1592#endif 1593 if (Ops[0].getValueType() == EVT) // Normal store? 1594 return getStore(Ops[0], Ops[1], Ops[2], Ops[3]); 1595 assert(Ops[1].getValueType() > EVT && "Not a truncation?"); 1596 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) && 1597 "Can't do FP-INT conversion!"); 1598 break; 1599 } 1600 case ISD::SELECT_CC: { 1601 assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); 1602 assert(Ops[0].getValueType() == Ops[1].getValueType() && 1603 "LHS and RHS of condition must have same type!"); 1604 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1605 "True and False arms of SelectCC must have same type!"); 1606 assert(Ops[2].getValueType() == VT && 1607 "select_cc node must be of same type as true and false value!"); 1608 break; 1609 } 1610 case ISD::BR_CC: { 1611 assert(NumOps == 5 && "BR_CC takes 5 operands!"); 1612 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1613 "LHS/RHS of comparison should match types!"); 1614 break; 1615 } 1616 } 1617 1618 // Memoize nodes. 1619 SDNode *N; 1620 SDVTList VTs = getVTList(VT); 1621 if (VT != MVT::Flag) { 1622 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps); 1623 void *IP = 0; 1624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1625 return SDOperand(E, 0); 1626 N = new SDNode(Opcode, Ops, NumOps); 1627 N->setValueTypes(VTs); 1628 CSEMap.InsertNode(N, IP); 1629 } else { 1630 N = new SDNode(Opcode, Ops, NumOps); 1631 N->setValueTypes(VTs); 1632 } 1633 AllNodes.push_back(N); 1634 return SDOperand(N, 0); 1635} 1636 1637SDOperand SelectionDAG::getNode(unsigned Opcode, 1638 std::vector<MVT::ValueType> &ResultTys, 1639 const SDOperand *Ops, unsigned NumOps) { 1640 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(), 1641 Ops, NumOps); 1642} 1643 1644SDOperand SelectionDAG::getNode(unsigned Opcode, 1645 const MVT::ValueType *VTs, unsigned NumVTs, 1646 const SDOperand *Ops, unsigned NumOps) { 1647 if (NumVTs == 1) 1648 return getNode(Opcode, VTs[0], Ops, NumOps); 1649 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps); 1650} 1651 1652SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 1653 const SDOperand *Ops, unsigned NumOps) { 1654 if (VTList.NumVTs == 1) 1655 return getNode(Opcode, VTList.VTs[0], Ops, NumOps); 1656 1657 switch (Opcode) { 1658 // FIXME: figure out how to safely handle things like 1659 // int foo(int x) { return 1 << (x & 255); } 1660 // int bar() { return foo(256); } 1661#if 0 1662 case ISD::SRA_PARTS: 1663 case ISD::SRL_PARTS: 1664 case ISD::SHL_PARTS: 1665 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 1666 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 1667 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1668 else if (N3.getOpcode() == ISD::AND) 1669 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 1670 // If the and is only masking out bits that cannot effect the shift, 1671 // eliminate the and. 1672 unsigned NumBits = MVT::getSizeInBits(VT)*2; 1673 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1674 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1675 } 1676 break; 1677#endif 1678 } 1679 1680 // Memoize the node unless it returns a flag. 1681 SDNode *N; 1682 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) { 1683 SelectionDAGCSEMap::NodeID ID; 1684 ID.SetOpcode(Opcode); 1685 ID.SetValueTypes(VTList); 1686 ID.SetOperands(&Ops[0], NumOps); 1687 void *IP = 0; 1688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1689 return SDOperand(E, 0); 1690 N = new SDNode(Opcode, Ops, NumOps); 1691 N->setValueTypes(VTList); 1692 CSEMap.InsertNode(N, IP); 1693 } else { 1694 N = new SDNode(Opcode, Ops, NumOps); 1695 N->setValueTypes(VTList); 1696 } 1697 AllNodes.push_back(N); 1698 return SDOperand(N, 0); 1699} 1700 1701SDVTList SelectionDAG::getVTList(MVT::ValueType VT) { 1702 return makeVTList(SDNode::getValueTypeList(VT), 1); 1703} 1704 1705SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) { 1706 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 1707 E = VTList.end(); I != E; ++I) { 1708 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) 1709 return makeVTList(&(*I)[0], 2); 1710 } 1711 std::vector<MVT::ValueType> V; 1712 V.push_back(VT1); 1713 V.push_back(VT2); 1714 VTList.push_front(V); 1715 return makeVTList(&(*VTList.begin())[0], 2); 1716} 1717SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2, 1718 MVT::ValueType VT3) { 1719 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 1720 E = VTList.end(); I != E; ++I) { 1721 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 && 1722 (*I)[2] == VT3) 1723 return makeVTList(&(*I)[0], 3); 1724 } 1725 std::vector<MVT::ValueType> V; 1726 V.push_back(VT1); 1727 V.push_back(VT2); 1728 V.push_back(VT3); 1729 VTList.push_front(V); 1730 return makeVTList(&(*VTList.begin())[0], 3); 1731} 1732 1733SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) { 1734 switch (NumVTs) { 1735 case 0: assert(0 && "Cannot have nodes without results!"); 1736 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1); 1737 case 2: return getVTList(VTs[0], VTs[1]); 1738 case 3: return getVTList(VTs[0], VTs[1], VTs[2]); 1739 default: break; 1740 } 1741 1742 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 1743 E = VTList.end(); I != E; ++I) { 1744 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue; 1745 1746 bool NoMatch = false; 1747 for (unsigned i = 2; i != NumVTs; ++i) 1748 if (VTs[i] != (*I)[i]) { 1749 NoMatch = true; 1750 break; 1751 } 1752 if (!NoMatch) 1753 return makeVTList(&*I->begin(), NumVTs); 1754 } 1755 1756 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs)); 1757 return makeVTList(&*VTList.begin()->begin(), NumVTs); 1758} 1759 1760 1761/// UpdateNodeOperands - *Mutate* the specified node in-place to have the 1762/// specified operands. If the resultant node already exists in the DAG, 1763/// this does not modify the specified node, instead it returns the node that 1764/// already exists. If the resultant node does not exist in the DAG, the 1765/// input node is returned. As a degenerate case, if you specify the same 1766/// input operands as the node already has, the input node is returned. 1767SDOperand SelectionDAG:: 1768UpdateNodeOperands(SDOperand InN, SDOperand Op) { 1769 SDNode *N = InN.Val; 1770 assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 1771 1772 // Check to see if there is no change. 1773 if (Op == N->getOperand(0)) return InN; 1774 1775 // See if the modified node already exists. 1776 void *InsertPos = 0; 1777 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) 1778 return SDOperand(Existing, InN.ResNo); 1779 1780 // Nope it doesn't. Remove the node from it's current place in the maps. 1781 if (InsertPos) 1782 RemoveNodeFromCSEMaps(N); 1783 1784 // Now we update the operands. 1785 N->OperandList[0].Val->removeUser(N); 1786 Op.Val->addUser(N); 1787 N->OperandList[0] = Op; 1788 1789 // If this gets put into a CSE map, add it. 1790 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 1791 return InN; 1792} 1793 1794SDOperand SelectionDAG:: 1795UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) { 1796 SDNode *N = InN.Val; 1797 assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 1798 1799 // Check to see if there is no change. 1800 bool AnyChange = false; 1801 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 1802 return InN; // No operands changed, just return the input node. 1803 1804 // See if the modified node already exists. 1805 void *InsertPos = 0; 1806 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) 1807 return SDOperand(Existing, InN.ResNo); 1808 1809 // Nope it doesn't. Remove the node from it's current place in the maps. 1810 if (InsertPos) 1811 RemoveNodeFromCSEMaps(N); 1812 1813 // Now we update the operands. 1814 if (N->OperandList[0] != Op1) { 1815 N->OperandList[0].Val->removeUser(N); 1816 Op1.Val->addUser(N); 1817 N->OperandList[0] = Op1; 1818 } 1819 if (N->OperandList[1] != Op2) { 1820 N->OperandList[1].Val->removeUser(N); 1821 Op2.Val->addUser(N); 1822 N->OperandList[1] = Op2; 1823 } 1824 1825 // If this gets put into a CSE map, add it. 1826 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 1827 return InN; 1828} 1829 1830SDOperand SelectionDAG:: 1831UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) { 1832 SDOperand Ops[] = { Op1, Op2, Op3 }; 1833 return UpdateNodeOperands(N, Ops, 3); 1834} 1835 1836SDOperand SelectionDAG:: 1837UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 1838 SDOperand Op3, SDOperand Op4) { 1839 SDOperand Ops[] = { Op1, Op2, Op3, Op4 }; 1840 return UpdateNodeOperands(N, Ops, 4); 1841} 1842 1843SDOperand SelectionDAG:: 1844UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 1845 SDOperand Op3, SDOperand Op4, SDOperand Op5) { 1846 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 }; 1847 return UpdateNodeOperands(N, Ops, 5); 1848} 1849 1850 1851SDOperand SelectionDAG:: 1852UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) { 1853 SDNode *N = InN.Val; 1854 assert(N->getNumOperands() == NumOps && 1855 "Update with wrong number of operands"); 1856 1857 // Check to see if there is no change. 1858 bool AnyChange = false; 1859 for (unsigned i = 0; i != NumOps; ++i) { 1860 if (Ops[i] != N->getOperand(i)) { 1861 AnyChange = true; 1862 break; 1863 } 1864 } 1865 1866 // No operands changed, just return the input node. 1867 if (!AnyChange) return InN; 1868 1869 // See if the modified node already exists. 1870 void *InsertPos = 0; 1871 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos)) 1872 return SDOperand(Existing, InN.ResNo); 1873 1874 // Nope it doesn't. Remove the node from it's current place in the maps. 1875 if (InsertPos) 1876 RemoveNodeFromCSEMaps(N); 1877 1878 // Now we update the operands. 1879 for (unsigned i = 0; i != NumOps; ++i) { 1880 if (N->OperandList[i] != Ops[i]) { 1881 N->OperandList[i].Val->removeUser(N); 1882 Ops[i].Val->addUser(N); 1883 N->OperandList[i] = Ops[i]; 1884 } 1885 } 1886 1887 // If this gets put into a CSE map, add it. 1888 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 1889 return InN; 1890} 1891 1892 1893 1894 1895/// SelectNodeTo - These are used for target selectors to *mutate* the 1896/// specified node to have the specified return type, Target opcode, and 1897/// operands. Note that target opcodes are stored as 1898/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 1899/// 1900/// Note that SelectNodeTo returns the resultant node. If there is already a 1901/// node of the specified opcode and operands, it returns that node instead of 1902/// the current one. 1903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1904 MVT::ValueType VT) { 1905 SDVTList VTs = getVTList(VT); 1906 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs); 1907 void *IP = 0; 1908 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 1909 return ON; 1910 1911 RemoveNodeFromCSEMaps(N); 1912 1913 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1914 N->setValueTypes(VTs); 1915 1916 CSEMap.InsertNode(N, IP); 1917 return N; 1918} 1919 1920SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1921 MVT::ValueType VT, SDOperand Op1) { 1922 // If an identical node already exists, use it. 1923 SDVTList VTs = getVTList(VT); 1924 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1); 1925 void *IP = 0; 1926 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 1927 return ON; 1928 1929 RemoveNodeFromCSEMaps(N); 1930 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1931 N->setValueTypes(VTs); 1932 N->setOperands(Op1); 1933 CSEMap.InsertNode(N, IP); 1934 return N; 1935} 1936 1937SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1938 MVT::ValueType VT, SDOperand Op1, 1939 SDOperand Op2) { 1940 // If an identical node already exists, use it. 1941 SDVTList VTs = getVTList(VT); 1942 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2); 1943 void *IP = 0; 1944 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 1945 return ON; 1946 1947 RemoveNodeFromCSEMaps(N); 1948 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1949 N->setValueTypes(VTs); 1950 N->setOperands(Op1, Op2); 1951 1952 CSEMap.InsertNode(N, IP); // Memoize the new node. 1953 return N; 1954} 1955 1956SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1957 MVT::ValueType VT, SDOperand Op1, 1958 SDOperand Op2, SDOperand Op3) { 1959 // If an identical node already exists, use it. 1960 SDVTList VTs = getVTList(VT); 1961 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, 1962 Op1, Op2, Op3); 1963 void *IP = 0; 1964 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 1965 return ON; 1966 1967 RemoveNodeFromCSEMaps(N); 1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1969 N->setValueTypes(VTs); 1970 N->setOperands(Op1, Op2, Op3); 1971 1972 CSEMap.InsertNode(N, IP); // Memoize the new node. 1973 return N; 1974} 1975 1976SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1977 MVT::ValueType VT, const SDOperand *Ops, 1978 unsigned NumOps) { 1979 // If an identical node already exists, use it. 1980 SDVTList VTs = getVTList(VT); 1981 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs); 1982 for (unsigned i = 0; i != NumOps; ++i) 1983 ID.AddOperand(Ops[i]); 1984 void *IP = 0; 1985 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 1986 return ON; 1987 1988 RemoveNodeFromCSEMaps(N); 1989 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1990 N->setValueTypes(VTs); 1991 N->setOperands(Ops, NumOps); 1992 1993 CSEMap.InsertNode(N, IP); // Memoize the new node. 1994 return N; 1995} 1996 1997SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1998 MVT::ValueType VT1, MVT::ValueType VT2, 1999 SDOperand Op1, SDOperand Op2) { 2000 SDVTList VTs = getVTList(VT1, VT2); 2001 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2); 2002 void *IP = 0; 2003 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2004 return ON; 2005 2006 RemoveNodeFromCSEMaps(N); 2007 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 2008 N->setValueTypes(VTs); 2009 N->setOperands(Op1, Op2); 2010 2011 CSEMap.InsertNode(N, IP); // Memoize the new node. 2012 return N; 2013} 2014 2015SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2016 MVT::ValueType VT1, MVT::ValueType VT2, 2017 SDOperand Op1, SDOperand Op2, 2018 SDOperand Op3) { 2019 // If an identical node already exists, use it. 2020 SDVTList VTs = getVTList(VT1, VT2); 2021 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, 2022 Op1, Op2, Op3); 2023 void *IP = 0; 2024 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2025 return ON; 2026 2027 RemoveNodeFromCSEMaps(N); 2028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 2029 N->setValueTypes(VTs); 2030 N->setOperands(Op1, Op2, Op3); 2031 2032 CSEMap.InsertNode(N, IP); // Memoize the new node. 2033 return N; 2034} 2035 2036 2037/// getTargetNode - These are used for target selectors to create a new node 2038/// with specified return type(s), target opcode, and operands. 2039/// 2040/// Note that getTargetNode returns the resultant node. If there is already a 2041/// node of the specified opcode and operands, it returns that node instead of 2042/// the current one. 2043SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) { 2044 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val; 2045} 2046SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 2047 SDOperand Op1) { 2048 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val; 2049} 2050SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 2051 SDOperand Op1, SDOperand Op2) { 2052 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val; 2053} 2054SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 2055 SDOperand Op1, SDOperand Op2, SDOperand Op3) { 2056 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val; 2057} 2058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 2059 const SDOperand *Ops, unsigned NumOps) { 2060 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val; 2061} 2062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 2063 MVT::ValueType VT2, SDOperand Op1) { 2064 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 2065 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val; 2066} 2067SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 2068 MVT::ValueType VT2, SDOperand Op1, 2069 SDOperand Op2) { 2070 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 2071 SDOperand Ops[] = { Op1, Op2 }; 2072 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val; 2073} 2074SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 2075 MVT::ValueType VT2, SDOperand Op1, 2076 SDOperand Op2, SDOperand Op3) { 2077 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 2078 SDOperand Ops[] = { Op1, Op2, Op3 }; 2079 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val; 2080} 2081SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 2082 MVT::ValueType VT2, 2083 const SDOperand *Ops, unsigned NumOps) { 2084 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 2085 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val; 2086} 2087SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 2088 MVT::ValueType VT2, MVT::ValueType VT3, 2089 SDOperand Op1, SDOperand Op2) { 2090 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 2091 SDOperand Ops[] = { Op1, Op2 }; 2092 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val; 2093} 2094SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 2095 MVT::ValueType VT2, MVT::ValueType VT3, 2096 const SDOperand *Ops, unsigned NumOps) { 2097 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 2098 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val; 2099} 2100 2101/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 2102/// This can cause recursive merging of nodes in the DAG. 2103/// 2104/// This version assumes From/To have a single result value. 2105/// 2106void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, 2107 std::vector<SDNode*> *Deleted) { 2108 SDNode *From = FromN.Val, *To = ToN.Val; 2109 assert(From->getNumValues() == 1 && To->getNumValues() == 1 && 2110 "Cannot replace with this method!"); 2111 assert(From != To && "Cannot replace uses of with self"); 2112 2113 while (!From->use_empty()) { 2114 // Process users until they are all gone. 2115 SDNode *U = *From->use_begin(); 2116 2117 // This node is about to morph, remove its old self from the CSE maps. 2118 RemoveNodeFromCSEMaps(U); 2119 2120 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 2121 I != E; ++I) 2122 if (I->Val == From) { 2123 From->removeUser(U); 2124 I->Val = To; 2125 To->addUser(U); 2126 } 2127 2128 // Now that we have modified U, add it back to the CSE maps. If it already 2129 // exists there, recursively merge the results together. 2130 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 2131 ReplaceAllUsesWith(U, Existing, Deleted); 2132 // U is now dead. 2133 if (Deleted) Deleted->push_back(U); 2134 DeleteNodeNotInCSEMaps(U); 2135 } 2136 } 2137} 2138 2139/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 2140/// This can cause recursive merging of nodes in the DAG. 2141/// 2142/// This version assumes From/To have matching types and numbers of result 2143/// values. 2144/// 2145void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 2146 std::vector<SDNode*> *Deleted) { 2147 assert(From != To && "Cannot replace uses of with self"); 2148 assert(From->getNumValues() == To->getNumValues() && 2149 "Cannot use this version of ReplaceAllUsesWith!"); 2150 if (From->getNumValues() == 1) { // If possible, use the faster version. 2151 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); 2152 return; 2153 } 2154 2155 while (!From->use_empty()) { 2156 // Process users until they are all gone. 2157 SDNode *U = *From->use_begin(); 2158 2159 // This node is about to morph, remove its old self from the CSE maps. 2160 RemoveNodeFromCSEMaps(U); 2161 2162 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 2163 I != E; ++I) 2164 if (I->Val == From) { 2165 From->removeUser(U); 2166 I->Val = To; 2167 To->addUser(U); 2168 } 2169 2170 // Now that we have modified U, add it back to the CSE maps. If it already 2171 // exists there, recursively merge the results together. 2172 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 2173 ReplaceAllUsesWith(U, Existing, Deleted); 2174 // U is now dead. 2175 if (Deleted) Deleted->push_back(U); 2176 DeleteNodeNotInCSEMaps(U); 2177 } 2178 } 2179} 2180 2181/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 2182/// This can cause recursive merging of nodes in the DAG. 2183/// 2184/// This version can replace From with any result values. To must match the 2185/// number and types of values returned by From. 2186void SelectionDAG::ReplaceAllUsesWith(SDNode *From, 2187 const SDOperand *To, 2188 std::vector<SDNode*> *Deleted) { 2189 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) { 2190 // Degenerate case handled above. 2191 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); 2192 return; 2193 } 2194 2195 while (!From->use_empty()) { 2196 // Process users until they are all gone. 2197 SDNode *U = *From->use_begin(); 2198 2199 // This node is about to morph, remove its old self from the CSE maps. 2200 RemoveNodeFromCSEMaps(U); 2201 2202 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 2203 I != E; ++I) 2204 if (I->Val == From) { 2205 const SDOperand &ToOp = To[I->ResNo]; 2206 From->removeUser(U); 2207 *I = ToOp; 2208 ToOp.Val->addUser(U); 2209 } 2210 2211 // Now that we have modified U, add it back to the CSE maps. If it already 2212 // exists there, recursively merge the results together. 2213 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 2214 ReplaceAllUsesWith(U, Existing, Deleted); 2215 // U is now dead. 2216 if (Deleted) Deleted->push_back(U); 2217 DeleteNodeNotInCSEMaps(U); 2218 } 2219 } 2220} 2221 2222/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving 2223/// uses of other values produced by From.Val alone. The Deleted vector is 2224/// handled the same was as for ReplaceAllUsesWith. 2225void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To, 2226 std::vector<SDNode*> &Deleted) { 2227 assert(From != To && "Cannot replace a value with itself"); 2228 // Handle the simple, trivial, case efficiently. 2229 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) { 2230 ReplaceAllUsesWith(From, To, &Deleted); 2231 return; 2232 } 2233 2234 // Get all of the users in a nice, deterministically ordered, uniqued set. 2235 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end()); 2236 2237 while (!Users.empty()) { 2238 // We know that this user uses some value of From. If it is the right 2239 // value, update it. 2240 SDNode *User = Users.back(); 2241 Users.pop_back(); 2242 2243 for (SDOperand *Op = User->OperandList, 2244 *E = User->OperandList+User->NumOperands; Op != E; ++Op) { 2245 if (*Op == From) { 2246 // Okay, we know this user needs to be updated. Remove its old self 2247 // from the CSE maps. 2248 RemoveNodeFromCSEMaps(User); 2249 2250 // Update all operands that match "From". 2251 for (; Op != E; ++Op) { 2252 if (*Op == From) { 2253 From.Val->removeUser(User); 2254 *Op = To; 2255 To.Val->addUser(User); 2256 } 2257 } 2258 2259 // Now that we have modified User, add it back to the CSE maps. If it 2260 // already exists there, recursively merge the results together. 2261 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) { 2262 unsigned NumDeleted = Deleted.size(); 2263 ReplaceAllUsesWith(User, Existing, &Deleted); 2264 2265 // User is now dead. 2266 Deleted.push_back(User); 2267 DeleteNodeNotInCSEMaps(User); 2268 2269 // We have to be careful here, because ReplaceAllUsesWith could have 2270 // deleted a user of From, which means there may be dangling pointers 2271 // in the "Users" setvector. Scan over the deleted node pointers and 2272 // remove them from the setvector. 2273 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i) 2274 Users.remove(Deleted[i]); 2275 } 2276 break; // Exit the operand scanning loop. 2277 } 2278 } 2279 } 2280} 2281 2282 2283/// AssignNodeIds - Assign a unique node id for each node in the DAG based on 2284/// their allnodes order. It returns the maximum id. 2285unsigned SelectionDAG::AssignNodeIds() { 2286 unsigned Id = 0; 2287 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){ 2288 SDNode *N = I; 2289 N->setNodeId(Id++); 2290 } 2291 return Id; 2292} 2293 2294/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG 2295/// based on their topological order. It returns the maximum id and a vector 2296/// of the SDNodes* in assigned order by reference. 2297unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) { 2298 unsigned DAGSize = AllNodes.size(); 2299 std::vector<unsigned> InDegree(DAGSize); 2300 std::vector<SDNode*> Sources; 2301 2302 // Use a two pass approach to avoid using a std::map which is slow. 2303 unsigned Id = 0; 2304 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){ 2305 SDNode *N = I; 2306 N->setNodeId(Id++); 2307 unsigned Degree = N->use_size(); 2308 InDegree[N->getNodeId()] = Degree; 2309 if (Degree == 0) 2310 Sources.push_back(N); 2311 } 2312 2313 TopOrder.clear(); 2314 while (!Sources.empty()) { 2315 SDNode *N = Sources.back(); 2316 Sources.pop_back(); 2317 TopOrder.push_back(N); 2318 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 2319 SDNode *P = I->Val; 2320 unsigned Degree = --InDegree[P->getNodeId()]; 2321 if (Degree == 0) 2322 Sources.push_back(P); 2323 } 2324 } 2325 2326 // Second pass, assign the actual topological order as node ids. 2327 Id = 0; 2328 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end(); 2329 TI != TE; ++TI) 2330 (*TI)->setNodeId(Id++); 2331 2332 return Id; 2333} 2334 2335 2336 2337//===----------------------------------------------------------------------===// 2338// SDNode Class 2339//===----------------------------------------------------------------------===// 2340 2341// Out-of-line virtual method to give class a home. 2342void SDNode::ANCHOR() { 2343} 2344 2345/// getValueTypeList - Return a pointer to the specified value type. 2346/// 2347MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { 2348 static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; 2349 VTs[VT] = VT; 2350 return &VTs[VT]; 2351} 2352 2353/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 2354/// indicated value. This method ignores uses of other values defined by this 2355/// operation. 2356bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { 2357 assert(Value < getNumValues() && "Bad value!"); 2358 2359 // If there is only one value, this is easy. 2360 if (getNumValues() == 1) 2361 return use_size() == NUses; 2362 if (Uses.size() < NUses) return false; 2363 2364 SDOperand TheValue(const_cast<SDNode *>(this), Value); 2365 2366 std::set<SDNode*> UsersHandled; 2367 2368 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) { 2369 SDNode *User = *UI; 2370 if (User->getNumOperands() == 1 || 2371 UsersHandled.insert(User).second) // First time we've seen this? 2372 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 2373 if (User->getOperand(i) == TheValue) { 2374 if (NUses == 0) 2375 return false; // too many uses 2376 --NUses; 2377 } 2378 } 2379 2380 // Found exactly the right number of uses? 2381 return NUses == 0; 2382} 2383 2384 2385// isOnlyUse - Return true if this node is the only use of N. 2386bool SDNode::isOnlyUse(SDNode *N) const { 2387 bool Seen = false; 2388 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 2389 SDNode *User = *I; 2390 if (User == this) 2391 Seen = true; 2392 else 2393 return false; 2394 } 2395 2396 return Seen; 2397} 2398 2399// isOperand - Return true if this node is an operand of N. 2400bool SDOperand::isOperand(SDNode *N) const { 2401 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 2402 if (*this == N->getOperand(i)) 2403 return true; 2404 return false; 2405} 2406 2407bool SDNode::isOperand(SDNode *N) const { 2408 for (unsigned i = 0, e = N->NumOperands; i != e; ++i) 2409 if (this == N->OperandList[i].Val) 2410 return true; 2411 return false; 2412} 2413 2414uint64_t SDNode::getConstantOperandVal(unsigned Num) const { 2415 assert(Num < NumOperands && "Invalid child # of SDNode!"); 2416 return cast<ConstantSDNode>(OperandList[Num])->getValue(); 2417} 2418 2419const char *SDNode::getOperationName(const SelectionDAG *G) const { 2420 switch (getOpcode()) { 2421 default: 2422 if (getOpcode() < ISD::BUILTIN_OP_END) 2423 return "<<Unknown DAG Node>>"; 2424 else { 2425 if (G) { 2426 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 2427 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) 2428 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); 2429 2430 TargetLowering &TLI = G->getTargetLoweringInfo(); 2431 const char *Name = 2432 TLI.getTargetNodeName(getOpcode()); 2433 if (Name) return Name; 2434 } 2435 2436 return "<<Unknown Target Node>>"; 2437 } 2438 2439 case ISD::PCMARKER: return "PCMarker"; 2440 case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 2441 case ISD::SRCVALUE: return "SrcValue"; 2442 case ISD::EntryToken: return "EntryToken"; 2443 case ISD::TokenFactor: return "TokenFactor"; 2444 case ISD::AssertSext: return "AssertSext"; 2445 case ISD::AssertZext: return "AssertZext"; 2446 2447 case ISD::STRING: return "String"; 2448 case ISD::BasicBlock: return "BasicBlock"; 2449 case ISD::VALUETYPE: return "ValueType"; 2450 case ISD::Register: return "Register"; 2451 2452 case ISD::Constant: return "Constant"; 2453 case ISD::ConstantFP: return "ConstantFP"; 2454 case ISD::GlobalAddress: return "GlobalAddress"; 2455 case ISD::FrameIndex: return "FrameIndex"; 2456 case ISD::JumpTable: return "JumpTable"; 2457 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE"; 2458 case ISD::ConstantPool: return "ConstantPool"; 2459 case ISD::ExternalSymbol: return "ExternalSymbol"; 2460 case ISD::INTRINSIC_WO_CHAIN: { 2461 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue(); 2462 return Intrinsic::getName((Intrinsic::ID)IID); 2463 } 2464 case ISD::INTRINSIC_VOID: 2465 case ISD::INTRINSIC_W_CHAIN: { 2466 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue(); 2467 return Intrinsic::getName((Intrinsic::ID)IID); 2468 } 2469 2470 case ISD::BUILD_VECTOR: return "BUILD_VECTOR"; 2471 case ISD::TargetConstant: return "TargetConstant"; 2472 case ISD::TargetConstantFP:return "TargetConstantFP"; 2473 case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 2474 case ISD::TargetFrameIndex: return "TargetFrameIndex"; 2475 case ISD::TargetJumpTable: return "TargetJumpTable"; 2476 case ISD::TargetConstantPool: return "TargetConstantPool"; 2477 case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 2478 2479 case ISD::CopyToReg: return "CopyToReg"; 2480 case ISD::CopyFromReg: return "CopyFromReg"; 2481 case ISD::UNDEF: return "undef"; 2482 case ISD::MERGE_VALUES: return "mergevalues"; 2483 case ISD::INLINEASM: return "inlineasm"; 2484 case ISD::HANDLENODE: return "handlenode"; 2485 case ISD::FORMAL_ARGUMENTS: return "formal_arguments"; 2486 case ISD::CALL: return "call"; 2487 2488 // Unary operators 2489 case ISD::FABS: return "fabs"; 2490 case ISD::FNEG: return "fneg"; 2491 case ISD::FSQRT: return "fsqrt"; 2492 case ISD::FSIN: return "fsin"; 2493 case ISD::FCOS: return "fcos"; 2494 case ISD::FPOWI: return "fpowi"; 2495 2496 // Binary operators 2497 case ISD::ADD: return "add"; 2498 case ISD::SUB: return "sub"; 2499 case ISD::MUL: return "mul"; 2500 case ISD::MULHU: return "mulhu"; 2501 case ISD::MULHS: return "mulhs"; 2502 case ISD::SDIV: return "sdiv"; 2503 case ISD::UDIV: return "udiv"; 2504 case ISD::SREM: return "srem"; 2505 case ISD::UREM: return "urem"; 2506 case ISD::AND: return "and"; 2507 case ISD::OR: return "or"; 2508 case ISD::XOR: return "xor"; 2509 case ISD::SHL: return "shl"; 2510 case ISD::SRA: return "sra"; 2511 case ISD::SRL: return "srl"; 2512 case ISD::ROTL: return "rotl"; 2513 case ISD::ROTR: return "rotr"; 2514 case ISD::FADD: return "fadd"; 2515 case ISD::FSUB: return "fsub"; 2516 case ISD::FMUL: return "fmul"; 2517 case ISD::FDIV: return "fdiv"; 2518 case ISD::FREM: return "frem"; 2519 case ISD::FCOPYSIGN: return "fcopysign"; 2520 case ISD::VADD: return "vadd"; 2521 case ISD::VSUB: return "vsub"; 2522 case ISD::VMUL: return "vmul"; 2523 case ISD::VSDIV: return "vsdiv"; 2524 case ISD::VUDIV: return "vudiv"; 2525 case ISD::VAND: return "vand"; 2526 case ISD::VOR: return "vor"; 2527 case ISD::VXOR: return "vxor"; 2528 2529 case ISD::SETCC: return "setcc"; 2530 case ISD::SELECT: return "select"; 2531 case ISD::SELECT_CC: return "select_cc"; 2532 case ISD::VSELECT: return "vselect"; 2533 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt"; 2534 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt"; 2535 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt"; 2536 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt"; 2537 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector"; 2538 case ISD::VBUILD_VECTOR: return "vbuild_vector"; 2539 case ISD::VECTOR_SHUFFLE: return "vector_shuffle"; 2540 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle"; 2541 case ISD::VBIT_CONVERT: return "vbit_convert"; 2542 case ISD::ADDC: return "addc"; 2543 case ISD::ADDE: return "adde"; 2544 case ISD::SUBC: return "subc"; 2545 case ISD::SUBE: return "sube"; 2546 case ISD::SHL_PARTS: return "shl_parts"; 2547 case ISD::SRA_PARTS: return "sra_parts"; 2548 case ISD::SRL_PARTS: return "srl_parts"; 2549 2550 // Conversion operators. 2551 case ISD::SIGN_EXTEND: return "sign_extend"; 2552 case ISD::ZERO_EXTEND: return "zero_extend"; 2553 case ISD::ANY_EXTEND: return "any_extend"; 2554 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 2555 case ISD::TRUNCATE: return "truncate"; 2556 case ISD::FP_ROUND: return "fp_round"; 2557 case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 2558 case ISD::FP_EXTEND: return "fp_extend"; 2559 2560 case ISD::SINT_TO_FP: return "sint_to_fp"; 2561 case ISD::UINT_TO_FP: return "uint_to_fp"; 2562 case ISD::FP_TO_SINT: return "fp_to_sint"; 2563 case ISD::FP_TO_UINT: return "fp_to_uint"; 2564 case ISD::BIT_CONVERT: return "bit_convert"; 2565 2566 // Control flow instructions 2567 case ISD::BR: return "br"; 2568 case ISD::BRIND: return "brind"; 2569 case ISD::BRCOND: return "brcond"; 2570 case ISD::BR_CC: return "br_cc"; 2571 case ISD::RET: return "ret"; 2572 case ISD::CALLSEQ_START: return "callseq_start"; 2573 case ISD::CALLSEQ_END: return "callseq_end"; 2574 2575 // Other operators 2576 case ISD::LOAD: return "load"; 2577 case ISD::STORE: return "store"; 2578 case ISD::VLOAD: return "vload"; 2579 case ISD::TRUNCSTORE: return "truncstore"; 2580 case ISD::VAARG: return "vaarg"; 2581 case ISD::VACOPY: return "vacopy"; 2582 case ISD::VAEND: return "vaend"; 2583 case ISD::VASTART: return "vastart"; 2584 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 2585 case ISD::EXTRACT_ELEMENT: return "extract_element"; 2586 case ISD::BUILD_PAIR: return "build_pair"; 2587 case ISD::STACKSAVE: return "stacksave"; 2588 case ISD::STACKRESTORE: return "stackrestore"; 2589 2590 // Block memory operations. 2591 case ISD::MEMSET: return "memset"; 2592 case ISD::MEMCPY: return "memcpy"; 2593 case ISD::MEMMOVE: return "memmove"; 2594 2595 // Bit manipulation 2596 case ISD::BSWAP: return "bswap"; 2597 case ISD::CTPOP: return "ctpop"; 2598 case ISD::CTTZ: return "cttz"; 2599 case ISD::CTLZ: return "ctlz"; 2600 2601 // Debug info 2602 case ISD::LOCATION: return "location"; 2603 case ISD::DEBUG_LOC: return "debug_loc"; 2604 case ISD::DEBUG_LABEL: return "debug_label"; 2605 2606 case ISD::CONDCODE: 2607 switch (cast<CondCodeSDNode>(this)->get()) { 2608 default: assert(0 && "Unknown setcc condition!"); 2609 case ISD::SETOEQ: return "setoeq"; 2610 case ISD::SETOGT: return "setogt"; 2611 case ISD::SETOGE: return "setoge"; 2612 case ISD::SETOLT: return "setolt"; 2613 case ISD::SETOLE: return "setole"; 2614 case ISD::SETONE: return "setone"; 2615 2616 case ISD::SETO: return "seto"; 2617 case ISD::SETUO: return "setuo"; 2618 case ISD::SETUEQ: return "setue"; 2619 case ISD::SETUGT: return "setugt"; 2620 case ISD::SETUGE: return "setuge"; 2621 case ISD::SETULT: return "setult"; 2622 case ISD::SETULE: return "setule"; 2623 case ISD::SETUNE: return "setune"; 2624 2625 case ISD::SETEQ: return "seteq"; 2626 case ISD::SETGT: return "setgt"; 2627 case ISD::SETGE: return "setge"; 2628 case ISD::SETLT: return "setlt"; 2629 case ISD::SETLE: return "setle"; 2630 case ISD::SETNE: return "setne"; 2631 } 2632 } 2633} 2634 2635void SDNode::dump() const { dump(0); } 2636void SDNode::dump(const SelectionDAG *G) const { 2637 std::cerr << (void*)this << ": "; 2638 2639 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 2640 if (i) std::cerr << ","; 2641 if (getValueType(i) == MVT::Other) 2642 std::cerr << "ch"; 2643 else 2644 std::cerr << MVT::getValueTypeString(getValueType(i)); 2645 } 2646 std::cerr << " = " << getOperationName(G); 2647 2648 std::cerr << " "; 2649 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 2650 if (i) std::cerr << ", "; 2651 std::cerr << (void*)getOperand(i).Val; 2652 if (unsigned RN = getOperand(i).ResNo) 2653 std::cerr << ":" << RN; 2654 } 2655 2656 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 2657 std::cerr << "<" << CSDN->getValue() << ">"; 2658 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 2659 std::cerr << "<" << CSDN->getValue() << ">"; 2660 } else if (const GlobalAddressSDNode *GADN = 2661 dyn_cast<GlobalAddressSDNode>(this)) { 2662 int offset = GADN->getOffset(); 2663 std::cerr << "<"; 2664 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; 2665 if (offset > 0) 2666 std::cerr << " + " << offset; 2667 else 2668 std::cerr << " " << offset; 2669 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 2670 std::cerr << "<" << FIDN->getIndex() << ">"; 2671 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 2672 int offset = CP->getOffset(); 2673 if (CP->isMachineConstantPoolEntry()) 2674 std::cerr << "<" << *CP->getMachineCPVal() << ">"; 2675 else 2676 std::cerr << "<" << *CP->getConstVal() << ">"; 2677 if (offset > 0) 2678 std::cerr << " + " << offset; 2679 else 2680 std::cerr << " " << offset; 2681 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 2682 std::cerr << "<"; 2683 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 2684 if (LBB) 2685 std::cerr << LBB->getName() << " "; 2686 std::cerr << (const void*)BBDN->getBasicBlock() << ">"; 2687 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 2688 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) { 2689 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); 2690 } else { 2691 std::cerr << " #" << R->getReg(); 2692 } 2693 } else if (const ExternalSymbolSDNode *ES = 2694 dyn_cast<ExternalSymbolSDNode>(this)) { 2695 std::cerr << "'" << ES->getSymbol() << "'"; 2696 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 2697 if (M->getValue()) 2698 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; 2699 else 2700 std::cerr << "<null:" << M->getOffset() << ">"; 2701 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 2702 std::cerr << ":" << getValueTypeString(N->getVT()); 2703 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) { 2704 bool doExt = true; 2705 switch (LD->getExtensionType()) { 2706 default: doExt = false; break; 2707 case ISD::EXTLOAD: 2708 std::cerr << " <anyext "; 2709 break; 2710 case ISD::SEXTLOAD: 2711 std::cerr << " <sext "; 2712 break; 2713 case ISD::ZEXTLOAD: 2714 std::cerr << " <zext "; 2715 break; 2716 } 2717 if (doExt) 2718 std::cerr << MVT::getValueTypeString(LD->getLoadVT()) << ">"; 2719 2720 if (LD->getAddressingMode() == ISD::PRE_INDEXED) 2721 std::cerr << " <pre>"; 2722 else if (LD->getAddressingMode() == ISD::POST_INDEXED) 2723 std::cerr << " <post>"; 2724 } 2725} 2726 2727static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 2728 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 2729 if (N->getOperand(i).Val->hasOneUse()) 2730 DumpNodes(N->getOperand(i).Val, indent+2, G); 2731 else 2732 std::cerr << "\n" << std::string(indent+2, ' ') 2733 << (void*)N->getOperand(i).Val << ": <multiple use>"; 2734 2735 2736 std::cerr << "\n" << std::string(indent, ' '); 2737 N->dump(G); 2738} 2739 2740void SelectionDAG::dump() const { 2741 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; 2742 std::vector<const SDNode*> Nodes; 2743 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 2744 I != E; ++I) 2745 Nodes.push_back(I); 2746 2747 std::sort(Nodes.begin(), Nodes.end()); 2748 2749 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 2750 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) 2751 DumpNodes(Nodes[i], 2, this); 2752 } 2753 2754 DumpNodes(getRoot().Val, 2, this); 2755 2756 std::cerr << "\n\n"; 2757} 2758 2759const Type *ConstantPoolSDNode::getType() const { 2760 if (isMachineConstantPoolEntry()) 2761 return Val.MachineCPVal->getType(); 2762 return Val.ConstVal->getType(); 2763} 2764