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