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