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