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