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