SelectionDAG.cpp revision 36ce69195ed488034d0bb11180cc2ebd923679c8
1//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This implements the SelectionDAG class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/SelectionDAG.h" 15#include "llvm/Constants.h" 16#include "llvm/GlobalValue.h" 17#include "llvm/Assembly/Writer.h" 18#include "llvm/CodeGen/MachineBasicBlock.h" 19#include "llvm/Support/MathExtras.h" 20#include "llvm/Target/MRegisterInfo.h" 21#include "llvm/Target/TargetLowering.h" 22#include "llvm/Target/TargetInstrInfo.h" 23#include "llvm/Target/TargetMachine.h" 24#include <iostream> 25#include <set> 26#include <cmath> 27#include <algorithm> 28using namespace llvm; 29 30static bool isCommutativeBinOp(unsigned Opcode) { 31 switch (Opcode) { 32 case ISD::ADD: 33 case ISD::MUL: 34 case ISD::FADD: 35 case ISD::FMUL: 36 case ISD::AND: 37 case ISD::OR: 38 case ISD::XOR: return true; 39 default: return false; // FIXME: Need commutative info for user ops! 40 } 41} 42 43static bool isAssociativeBinOp(unsigned Opcode) { 44 switch (Opcode) { 45 case ISD::ADD: 46 case ISD::MUL: 47 case ISD::AND: 48 case ISD::OR: 49 case ISD::XOR: return true; 50 default: return false; // FIXME: Need associative info for user ops! 51 } 52} 53 54// isInvertibleForFree - Return true if there is no cost to emitting the logical 55// inverse of this node. 56static bool isInvertibleForFree(SDOperand N) { 57 if (isa<ConstantSDNode>(N.Val)) return true; 58 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse()) 59 return true; 60 return false; 61} 62 63//===----------------------------------------------------------------------===// 64// ConstantFPSDNode Class 65//===----------------------------------------------------------------------===// 66 67/// isExactlyValue - We don't rely on operator== working on double values, as 68/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 69/// As such, this method can be used to do an exact bit-for-bit comparison of 70/// two floating point values. 71bool ConstantFPSDNode::isExactlyValue(double V) const { 72 return DoubleToBits(V) == DoubleToBits(Value); 73} 74 75//===----------------------------------------------------------------------===// 76// ISD Class 77//===----------------------------------------------------------------------===// 78 79/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 80/// when given the operation for (X op Y). 81ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 82 // To perform this operation, we just need to swap the L and G bits of the 83 // operation. 84 unsigned OldL = (Operation >> 2) & 1; 85 unsigned OldG = (Operation >> 1) & 1; 86 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 87 (OldL << 1) | // New G bit 88 (OldG << 2)); // New L bit. 89} 90 91/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 92/// 'op' is a valid SetCC operation. 93ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 94 unsigned Operation = Op; 95 if (isInteger) 96 Operation ^= 7; // Flip L, G, E bits, but not U. 97 else 98 Operation ^= 15; // Flip all of the condition bits. 99 if (Operation > ISD::SETTRUE2) 100 Operation &= ~8; // Don't let N and U bits get set. 101 return ISD::CondCode(Operation); 102} 103 104 105/// isSignedOp - For an integer comparison, return 1 if the comparison is a 106/// signed operation and 2 if the result is an unsigned comparison. Return zero 107/// if the operation does not depend on the sign of the input (setne and seteq). 108static int isSignedOp(ISD::CondCode Opcode) { 109 switch (Opcode) { 110 default: assert(0 && "Illegal integer setcc operation!"); 111 case ISD::SETEQ: 112 case ISD::SETNE: return 0; 113 case ISD::SETLT: 114 case ISD::SETLE: 115 case ISD::SETGT: 116 case ISD::SETGE: return 1; 117 case ISD::SETULT: 118 case ISD::SETULE: 119 case ISD::SETUGT: 120 case ISD::SETUGE: return 2; 121 } 122} 123 124/// getSetCCOrOperation - Return the result of a logical OR between different 125/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 126/// returns SETCC_INVALID if it is not possible to represent the resultant 127/// comparison. 128ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 129 bool isInteger) { 130 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 131 // Cannot fold a signed integer setcc with an unsigned integer setcc. 132 return ISD::SETCC_INVALID; 133 134 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 135 136 // If the N and U bits get set then the resultant comparison DOES suddenly 137 // care about orderedness, and is true when ordered. 138 if (Op > ISD::SETTRUE2) 139 Op &= ~16; // Clear the N bit. 140 return ISD::CondCode(Op); 141} 142 143/// getSetCCAndOperation - Return the result of a logical AND between different 144/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 145/// function returns zero if it is not possible to represent the resultant 146/// comparison. 147ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 148 bool isInteger) { 149 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 150 // Cannot fold a signed setcc with an unsigned setcc. 151 return ISD::SETCC_INVALID; 152 153 // Combine all of the condition bits. 154 return ISD::CondCode(Op1 & Op2); 155} 156 157const TargetMachine &SelectionDAG::getTarget() const { 158 return TLI.getTargetMachine(); 159} 160 161//===----------------------------------------------------------------------===// 162// SelectionDAG Class 163//===----------------------------------------------------------------------===// 164 165/// RemoveDeadNodes - This method deletes all unreachable nodes in the 166/// SelectionDAG, including nodes (like loads) that have uses of their token 167/// chain but no other uses and no side effect. If a node is passed in as an 168/// argument, it is used as the seed for node deletion. 169void SelectionDAG::RemoveDeadNodes(SDNode *N) { 170 // Create a dummy node (which is not added to allnodes), that adds a reference 171 // to the root node, preventing it from being deleted. 172 HandleSDNode Dummy(getRoot()); 173 174 bool MadeChange = false; 175 176 // If we have a hint to start from, use it. 177 if (N && N->use_empty()) { 178 DestroyDeadNode(N); 179 MadeChange = true; 180 } 181 182 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 183 if (I->use_empty() && I->getOpcode() != 65535) { 184 // Node is dead, recursively delete newly dead uses. 185 DestroyDeadNode(I); 186 MadeChange = true; 187 } 188 189 // Walk the nodes list, removing the nodes we've marked as dead. 190 if (MadeChange) { 191 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) { 192 SDNode *N = I++; 193 if (N->use_empty()) 194 AllNodes.erase(N); 195 } 196 } 197 198 // If the root changed (e.g. it was a dead load, update the root). 199 setRoot(Dummy.getValue()); 200} 201 202/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the 203/// graph. If it is the last user of any of its operands, recursively process 204/// them the same way. 205/// 206void SelectionDAG::DestroyDeadNode(SDNode *N) { 207 // Okay, we really are going to delete this node. First take this out of the 208 // appropriate CSE map. 209 RemoveNodeFromCSEMaps(N); 210 211 // Next, brutally remove the operand list. This is safe to do, as there are 212 // no cycles in the graph. 213 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 214 SDNode *O = I->Val; 215 O->removeUser(N); 216 217 // Now that we removed this operand, see if there are no uses of it left. 218 if (O->use_empty()) 219 DestroyDeadNode(O); 220 } 221 delete[] N->OperandList; 222 N->OperandList = 0; 223 N->NumOperands = 0; 224 225 // Mark the node as dead. 226 N->MorphNodeTo(65535); 227} 228 229void SelectionDAG::DeleteNode(SDNode *N) { 230 assert(N->use_empty() && "Cannot delete a node that is not dead!"); 231 232 // First take this out of the appropriate CSE map. 233 RemoveNodeFromCSEMaps(N); 234 235 // Finally, remove uses due to operands of this node, remove from the 236 // AllNodes list, and delete the node. 237 DeleteNodeNotInCSEMaps(N); 238} 239 240void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 241 242 // Remove it from the AllNodes list. 243 AllNodes.remove(N); 244 245 // Drop all of the operands and decrement used nodes use counts. 246 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 247 I->Val->removeUser(N); 248 delete[] N->OperandList; 249 N->OperandList = 0; 250 N->NumOperands = 0; 251 252 delete N; 253} 254 255/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 256/// correspond to it. This is useful when we're about to delete or repurpose 257/// the node. We don't want future request for structurally identical nodes 258/// to return N anymore. 259void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 260 bool Erased = false; 261 switch (N->getOpcode()) { 262 case ISD::HANDLENODE: return; // noop. 263 case ISD::Constant: 264 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(), 265 N->getValueType(0))); 266 break; 267 case ISD::TargetConstant: 268 Erased = TargetConstants.erase(std::make_pair( 269 cast<ConstantSDNode>(N)->getValue(), 270 N->getValueType(0))); 271 break; 272 case ISD::ConstantFP: { 273 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()); 274 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0))); 275 break; 276 } 277 case ISD::STRING: 278 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); 279 break; 280 case ISD::CONDCODE: 281 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 282 "Cond code doesn't exist!"); 283 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 284 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 285 break; 286 case ISD::GlobalAddress: 287 Erased = GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal()); 288 break; 289 case ISD::TargetGlobalAddress: 290 Erased =TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal()); 291 break; 292 case ISD::FrameIndex: 293 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); 294 break; 295 case ISD::TargetFrameIndex: 296 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); 297 break; 298 case ISD::ConstantPool: 299 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get()); 300 break; 301 case ISD::TargetConstantPool: 302 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get()); 303 break; 304 case ISD::BasicBlock: 305 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock()); 306 break; 307 case ISD::ExternalSymbol: 308 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 309 break; 310 case ISD::TargetExternalSymbol: 311 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 312 break; 313 case ISD::VALUETYPE: 314 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0; 315 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; 316 break; 317 case ISD::Register: 318 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(), 319 N->getValueType(0))); 320 break; 321 case ISD::SRCVALUE: { 322 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N); 323 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset())); 324 break; 325 } 326 case ISD::LOAD: 327 Erased = Loads.erase(std::make_pair(N->getOperand(1), 328 std::make_pair(N->getOperand(0), 329 N->getValueType(0)))); 330 break; 331 default: 332 if (N->getNumValues() == 1) { 333 if (N->getNumOperands() == 0) { 334 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(), 335 N->getValueType(0))); 336 } else if (N->getNumOperands() == 1) { 337 Erased = 338 UnaryOps.erase(std::make_pair(N->getOpcode(), 339 std::make_pair(N->getOperand(0), 340 N->getValueType(0)))); 341 } else if (N->getNumOperands() == 2) { 342 Erased = 343 BinaryOps.erase(std::make_pair(N->getOpcode(), 344 std::make_pair(N->getOperand(0), 345 N->getOperand(1)))); 346 } else { 347 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 348 Erased = 349 OneResultNodes.erase(std::make_pair(N->getOpcode(), 350 std::make_pair(N->getValueType(0), 351 Ops))); 352 } 353 } else { 354 // Remove the node from the ArbitraryNodes map. 355 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 356 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 357 Erased = 358 ArbitraryNodes.erase(std::make_pair(N->getOpcode(), 359 std::make_pair(RV, Ops))); 360 } 361 break; 362 } 363#ifndef NDEBUG 364 // Verify that the node was actually in one of the CSE maps, unless it has a 365 // flag result (which cannot be CSE'd) or is one of the special cases that are 366 // not subject to CSE. 367 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && 368 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START && 369 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) { 370 371 N->dump(); 372 assert(0 && "Node is not in map!"); 373 } 374#endif 375} 376 377/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It 378/// has been taken out and modified in some way. If the specified node already 379/// exists in the CSE maps, do not modify the maps, but return the existing node 380/// instead. If it doesn't exist, add it and return null. 381/// 382SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { 383 assert(N->getNumOperands() && "This is a leaf node!"); 384 if (N->getOpcode() == ISD::LOAD) { 385 SDNode *&L = Loads[std::make_pair(N->getOperand(1), 386 std::make_pair(N->getOperand(0), 387 N->getValueType(0)))]; 388 if (L) return L; 389 L = N; 390 } else if (N->getOpcode() == ISD::HANDLENODE) { 391 return 0; // never add it. 392 } else if (N->getNumOperands() == 1) { 393 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(), 394 std::make_pair(N->getOperand(0), 395 N->getValueType(0)))]; 396 if (U) return U; 397 U = N; 398 } else if (N->getNumOperands() == 2) { 399 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(), 400 std::make_pair(N->getOperand(0), 401 N->getOperand(1)))]; 402 if (B) return B; 403 B = N; 404 } else if (N->getNumValues() == 1) { 405 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 406 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(), 407 std::make_pair(N->getValueType(0), Ops))]; 408 if (ORN) return ORN; 409 ORN = N; 410 } else { 411 // Remove the node from the ArbitraryNodes map. 412 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 413 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 414 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(), 415 std::make_pair(RV, Ops))]; 416 if (AN) return AN; 417 AN = N; 418 } 419 return 0; 420} 421 422 423 424SelectionDAG::~SelectionDAG() { 425 while (!AllNodes.empty()) { 426 SDNode *N = AllNodes.begin(); 427 delete [] N->OperandList; 428 N->OperandList = 0; 429 N->NumOperands = 0; 430 AllNodes.pop_front(); 431 } 432} 433 434SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { 435 if (Op.getValueType() == VT) return Op; 436 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); 437 return getNode(ISD::AND, Op.getValueType(), Op, 438 getConstant(Imm, Op.getValueType())); 439} 440 441SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) { 442 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 443 // Mask out any bits that are not valid for this constant. 444 if (VT != MVT::i64) 445 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; 446 447 SDNode *&N = Constants[std::make_pair(Val, VT)]; 448 if (N) return SDOperand(N, 0); 449 N = new ConstantSDNode(false, Val, VT); 450 AllNodes.push_back(N); 451 return SDOperand(N, 0); 452} 453 454SDOperand SelectionDAG::getString(const std::string &Val) { 455 StringSDNode *&N = StringNodes[Val]; 456 if (!N) { 457 N = new StringSDNode(Val); 458 AllNodes.push_back(N); 459 } 460 return SDOperand(N, 0); 461} 462 463SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) { 464 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 465 // Mask out any bits that are not valid for this constant. 466 if (VT != MVT::i64) 467 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; 468 469 SDNode *&N = TargetConstants[std::make_pair(Val, VT)]; 470 if (N) return SDOperand(N, 0); 471 N = new ConstantSDNode(true, Val, VT); 472 AllNodes.push_back(N); 473 return SDOperand(N, 0); 474} 475 476SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) { 477 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 478 if (VT == MVT::f32) 479 Val = (float)Val; // Mask out extra precision. 480 481 // Do the map lookup using the actual bit pattern for the floating point 482 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 483 // we don't have issues with SNANs. 484 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)]; 485 if (N) return SDOperand(N, 0); 486 N = new ConstantFPSDNode(Val, VT); 487 AllNodes.push_back(N); 488 return SDOperand(N, 0); 489} 490 491 492 493SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, 494 MVT::ValueType VT) { 495 SDNode *&N = GlobalValues[GV]; 496 if (N) return SDOperand(N, 0); 497 N = new GlobalAddressSDNode(false, GV, VT); 498 AllNodes.push_back(N); 499 return SDOperand(N, 0); 500} 501 502SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV, 503 MVT::ValueType VT) { 504 SDNode *&N = TargetGlobalValues[GV]; 505 if (N) return SDOperand(N, 0); 506 N = new GlobalAddressSDNode(true, GV, VT); 507 AllNodes.push_back(N); 508 return SDOperand(N, 0); 509} 510 511SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) { 512 SDNode *&N = FrameIndices[FI]; 513 if (N) return SDOperand(N, 0); 514 N = new FrameIndexSDNode(FI, VT, false); 515 AllNodes.push_back(N); 516 return SDOperand(N, 0); 517} 518 519SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) { 520 SDNode *&N = TargetFrameIndices[FI]; 521 if (N) return SDOperand(N, 0); 522 N = new FrameIndexSDNode(FI, VT, true); 523 AllNodes.push_back(N); 524 return SDOperand(N, 0); 525} 526 527SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) { 528 SDNode *&N = ConstantPoolIndices[C]; 529 if (N) return SDOperand(N, 0); 530 N = new ConstantPoolSDNode(C, VT, false); 531 AllNodes.push_back(N); 532 return SDOperand(N, 0); 533} 534 535SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) { 536 SDNode *&N = TargetConstantPoolIndices[C]; 537 if (N) return SDOperand(N, 0); 538 N = new ConstantPoolSDNode(C, VT, true); 539 AllNodes.push_back(N); 540 return SDOperand(N, 0); 541} 542 543SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 544 SDNode *&N = BBNodes[MBB]; 545 if (N) return SDOperand(N, 0); 546 N = new BasicBlockSDNode(MBB); 547 AllNodes.push_back(N); 548 return SDOperand(N, 0); 549} 550 551SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { 552 if ((unsigned)VT >= ValueTypeNodes.size()) 553 ValueTypeNodes.resize(VT+1); 554 if (ValueTypeNodes[VT] == 0) { 555 ValueTypeNodes[VT] = new VTSDNode(VT); 556 AllNodes.push_back(ValueTypeNodes[VT]); 557 } 558 559 return SDOperand(ValueTypeNodes[VT], 0); 560} 561 562SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { 563 SDNode *&N = ExternalSymbols[Sym]; 564 if (N) return SDOperand(N, 0); 565 N = new ExternalSymbolSDNode(false, Sym, VT); 566 AllNodes.push_back(N); 567 return SDOperand(N, 0); 568} 569 570SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) { 571 SDNode *&N = TargetExternalSymbols[Sym]; 572 if (N) return SDOperand(N, 0); 573 N = new ExternalSymbolSDNode(true, Sym, VT); 574 AllNodes.push_back(N); 575 return SDOperand(N, 0); 576} 577 578SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { 579 if ((unsigned)Cond >= CondCodeNodes.size()) 580 CondCodeNodes.resize(Cond+1); 581 582 if (CondCodeNodes[Cond] == 0) { 583 CondCodeNodes[Cond] = new CondCodeSDNode(Cond); 584 AllNodes.push_back(CondCodeNodes[Cond]); 585 } 586 return SDOperand(CondCodeNodes[Cond], 0); 587} 588 589SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { 590 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)]; 591 if (!Reg) { 592 Reg = new RegisterSDNode(RegNo, VT); 593 AllNodes.push_back(Reg); 594 } 595 return SDOperand(Reg, 0); 596} 597 598SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1, 599 SDOperand N2, ISD::CondCode Cond) { 600 // These setcc operations always fold. 601 switch (Cond) { 602 default: break; 603 case ISD::SETFALSE: 604 case ISD::SETFALSE2: return getConstant(0, VT); 605 case ISD::SETTRUE: 606 case ISD::SETTRUE2: return getConstant(1, VT); 607 } 608 609 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { 610 uint64_t C2 = N2C->getValue(); 611 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { 612 uint64_t C1 = N1C->getValue(); 613 614 // Sign extend the operands if required 615 if (ISD::isSignedIntSetCC(Cond)) { 616 C1 = N1C->getSignExtended(); 617 C2 = N2C->getSignExtended(); 618 } 619 620 switch (Cond) { 621 default: assert(0 && "Unknown integer setcc!"); 622 case ISD::SETEQ: return getConstant(C1 == C2, VT); 623 case ISD::SETNE: return getConstant(C1 != C2, VT); 624 case ISD::SETULT: return getConstant(C1 < C2, VT); 625 case ISD::SETUGT: return getConstant(C1 > C2, VT); 626 case ISD::SETULE: return getConstant(C1 <= C2, VT); 627 case ISD::SETUGE: return getConstant(C1 >= C2, VT); 628 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); 629 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); 630 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); 631 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); 632 } 633 } else { 634 // If the LHS is a ZERO_EXTEND, perform the comparison on the input. 635 if (N1.getOpcode() == ISD::ZERO_EXTEND) { 636 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType()); 637 638 // If the comparison constant has bits in the upper part, the 639 // zero-extended value could never match. 640 if (C2 & (~0ULL << InSize)) { 641 unsigned VSize = MVT::getSizeInBits(N1.getValueType()); 642 switch (Cond) { 643 case ISD::SETUGT: 644 case ISD::SETUGE: 645 case ISD::SETEQ: return getConstant(0, VT); 646 case ISD::SETULT: 647 case ISD::SETULE: 648 case ISD::SETNE: return getConstant(1, VT); 649 case ISD::SETGT: 650 case ISD::SETGE: 651 // True if the sign bit of C2 is set. 652 return getConstant((C2 & (1ULL << VSize)) != 0, VT); 653 case ISD::SETLT: 654 case ISD::SETLE: 655 // True if the sign bit of C2 isn't set. 656 return getConstant((C2 & (1ULL << VSize)) == 0, VT); 657 default: 658 break; 659 } 660 } 661 662 // Otherwise, we can perform the comparison with the low bits. 663 switch (Cond) { 664 case ISD::SETEQ: 665 case ISD::SETNE: 666 case ISD::SETUGT: 667 case ISD::SETUGE: 668 case ISD::SETULT: 669 case ISD::SETULE: 670 return getSetCC(VT, N1.getOperand(0), 671 getConstant(C2, N1.getOperand(0).getValueType()), 672 Cond); 673 default: 674 break; // todo, be more careful with signed comparisons 675 } 676 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG && 677 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 678 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT(); 679 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); 680 MVT::ValueType ExtDstTy = N1.getValueType(); 681 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); 682 683 // If the extended part has any inconsistent bits, it cannot ever 684 // compare equal. In other words, they have to be all ones or all 685 // zeros. 686 uint64_t ExtBits = 687 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); 688 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits) 689 return getConstant(Cond == ISD::SETNE, VT); 690 691 // Otherwise, make this a use of a zext. 692 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy), 693 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy), 694 Cond); 695 } 696 697 uint64_t MinVal, MaxVal; 698 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0)); 699 if (ISD::isSignedIntSetCC(Cond)) { 700 MinVal = 1ULL << (OperandBitSize-1); 701 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. 702 MaxVal = ~0ULL >> (65-OperandBitSize); 703 else 704 MaxVal = 0; 705 } else { 706 MinVal = 0; 707 MaxVal = ~0ULL >> (64-OperandBitSize); 708 } 709 710 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 711 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 712 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true 713 --C2; // X >= C1 --> X > (C1-1) 714 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 715 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); 716 } 717 718 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 719 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true 720 ++C2; // X <= C1 --> X < (C1+1) 721 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 722 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); 723 } 724 725 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal) 726 return getConstant(0, VT); // X < MIN --> false 727 728 // Canonicalize setgt X, Min --> setne X, Min 729 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal) 730 return getSetCC(VT, N1, N2, ISD::SETNE); 731 732 // If we have setult X, 1, turn it into seteq X, 0 733 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1) 734 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()), 735 ISD::SETEQ); 736 // If we have setugt X, Max-1, turn it into seteq X, Max 737 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1) 738 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()), 739 ISD::SETEQ); 740 741 // If we have "setcc X, C1", check to see if we can shrink the immediate 742 // by changing cc. 743 744 // SETUGT X, SINTMAX -> SETLT X, 0 745 if (Cond == ISD::SETUGT && OperandBitSize != 1 && 746 C2 == (~0ULL >> (65-OperandBitSize))) 747 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT); 748 749 // FIXME: Implement the rest of these. 750 751 752 // Fold bit comparisons when we can. 753 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 754 VT == N1.getValueType() && N1.getOpcode() == ISD::AND) 755 if (ConstantSDNode *AndRHS = 756 dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 757 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 758 // Perform the xform if the AND RHS is a single bit. 759 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { 760 return getNode(ISD::SRL, VT, N1, 761 getConstant(Log2_64(AndRHS->getValue()), 762 TLI.getShiftAmountTy())); 763 } 764 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) { 765 // (X & 8) == 8 --> (X & 8) >> 3 766 // Perform the xform if C2 is a single bit. 767 if ((C2 & (C2-1)) == 0) { 768 return getNode(ISD::SRL, VT, N1, 769 getConstant(Log2_64(C2),TLI.getShiftAmountTy())); 770 } 771 } 772 } 773 } 774 } else if (isa<ConstantSDNode>(N1.Val)) { 775 // Ensure that the constant occurs on the RHS. 776 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 777 } 778 779 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) 780 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { 781 double C1 = N1C->getValue(), C2 = N2C->getValue(); 782 783 switch (Cond) { 784 default: break; // FIXME: Implement the rest of these! 785 case ISD::SETEQ: return getConstant(C1 == C2, VT); 786 case ISD::SETNE: return getConstant(C1 != C2, VT); 787 case ISD::SETLT: return getConstant(C1 < C2, VT); 788 case ISD::SETGT: return getConstant(C1 > C2, VT); 789 case ISD::SETLE: return getConstant(C1 <= C2, VT); 790 case ISD::SETGE: return getConstant(C1 >= C2, VT); 791 } 792 } else { 793 // Ensure that the constant occurs on the RHS. 794 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 795 } 796 797 // Could not fold it. 798 return SDOperand(); 799} 800 801/// getNode - Gets or creates the specified node. 802/// 803SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { 804 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)]; 805 if (!N) { 806 N = new SDNode(Opcode, VT); 807 AllNodes.push_back(N); 808 } 809 return SDOperand(N, 0); 810} 811 812SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 813 SDOperand Operand) { 814 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { 815 uint64_t Val = C->getValue(); 816 switch (Opcode) { 817 default: break; 818 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); 819 case ISD::ANY_EXTEND: 820 case ISD::ZERO_EXTEND: return getConstant(Val, VT); 821 case ISD::TRUNCATE: return getConstant(Val, VT); 822 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); 823 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); 824 } 825 } 826 827 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) 828 switch (Opcode) { 829 case ISD::FNEG: 830 return getConstantFP(-C->getValue(), VT); 831 case ISD::FP_ROUND: 832 case ISD::FP_EXTEND: 833 return getConstantFP(C->getValue(), VT); 834 case ISD::FP_TO_SINT: 835 return getConstant((int64_t)C->getValue(), VT); 836 case ISD::FP_TO_UINT: 837 return getConstant((uint64_t)C->getValue(), VT); 838 } 839 840 unsigned OpOpcode = Operand.Val->getOpcode(); 841 switch (Opcode) { 842 case ISD::TokenFactor: 843 return Operand; // Factor of one node? No factor. 844 case ISD::SIGN_EXTEND: 845 if (Operand.getValueType() == VT) return Operand; // noop extension 846 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 847 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 848 break; 849 case ISD::ZERO_EXTEND: 850 if (Operand.getValueType() == VT) return Operand; // noop extension 851 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 852 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); 853 break; 854 case ISD::ANY_EXTEND: 855 if (Operand.getValueType() == VT) return Operand; // noop extension 856 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) 857 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 858 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 859 break; 860 case ISD::TRUNCATE: 861 if (Operand.getValueType() == VT) return Operand; // noop truncate 862 if (OpOpcode == ISD::TRUNCATE) 863 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 864 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 865 OpOpcode == ISD::ANY_EXTEND) { 866 // If the source is smaller than the dest, we still need an extend. 867 if (Operand.Val->getOperand(0).getValueType() < VT) 868 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 869 else if (Operand.Val->getOperand(0).getValueType() > VT) 870 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 871 else 872 return Operand.Val->getOperand(0); 873 } 874 break; 875 case ISD::FNEG: 876 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) 877 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), 878 Operand.Val->getOperand(0)); 879 if (OpOpcode == ISD::FNEG) // --X -> X 880 return Operand.Val->getOperand(0); 881 break; 882 case ISD::FABS: 883 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 884 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); 885 break; 886 } 887 888 SDNode *N; 889 if (VT != MVT::Flag) { // Don't CSE flag producing nodes 890 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))]; 891 if (E) return SDOperand(E, 0); 892 E = N = new SDNode(Opcode, Operand); 893 } else { 894 N = new SDNode(Opcode, Operand); 895 } 896 N->setValueTypes(VT); 897 AllNodes.push_back(N); 898 return SDOperand(N, 0); 899} 900 901 902 903SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 904 SDOperand N1, SDOperand N2) { 905#ifndef NDEBUG 906 switch (Opcode) { 907 case ISD::TokenFactor: 908 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 909 N2.getValueType() == MVT::Other && "Invalid token factor!"); 910 break; 911 case ISD::AND: 912 case ISD::OR: 913 case ISD::XOR: 914 case ISD::UDIV: 915 case ISD::UREM: 916 case ISD::MULHU: 917 case ISD::MULHS: 918 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); 919 // fall through 920 case ISD::ADD: 921 case ISD::SUB: 922 case ISD::MUL: 923 case ISD::SDIV: 924 case ISD::SREM: 925 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); 926 // fall through. 927 case ISD::FADD: 928 case ISD::FSUB: 929 case ISD::FMUL: 930 case ISD::FDIV: 931 case ISD::FREM: 932 assert(N1.getValueType() == N2.getValueType() && 933 N1.getValueType() == VT && "Binary operator types must match!"); 934 break; 935 936 case ISD::SHL: 937 case ISD::SRA: 938 case ISD::SRL: 939 assert(VT == N1.getValueType() && 940 "Shift operators return type must be the same as their first arg"); 941 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && 942 VT != MVT::i1 && "Shifts only work on integers"); 943 break; 944 case ISD::FP_ROUND_INREG: { 945 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 946 assert(VT == N1.getValueType() && "Not an inreg round!"); 947 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && 948 "Cannot FP_ROUND_INREG integer types"); 949 assert(EVT <= VT && "Not rounding down!"); 950 break; 951 } 952 case ISD::AssertSext: 953 case ISD::AssertZext: 954 case ISD::SIGN_EXTEND_INREG: { 955 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 956 assert(VT == N1.getValueType() && "Not an inreg extend!"); 957 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && 958 "Cannot *_EXTEND_INREG FP types"); 959 assert(EVT <= VT && "Not extending!"); 960 } 961 962 default: break; 963 } 964#endif 965 966 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 967 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 968 if (N1C) { 969 if (N2C) { 970 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); 971 switch (Opcode) { 972 case ISD::ADD: return getConstant(C1 + C2, VT); 973 case ISD::SUB: return getConstant(C1 - C2, VT); 974 case ISD::MUL: return getConstant(C1 * C2, VT); 975 case ISD::UDIV: 976 if (C2) return getConstant(C1 / C2, VT); 977 break; 978 case ISD::UREM : 979 if (C2) return getConstant(C1 % C2, VT); 980 break; 981 case ISD::SDIV : 982 if (C2) return getConstant(N1C->getSignExtended() / 983 N2C->getSignExtended(), VT); 984 break; 985 case ISD::SREM : 986 if (C2) return getConstant(N1C->getSignExtended() % 987 N2C->getSignExtended(), VT); 988 break; 989 case ISD::AND : return getConstant(C1 & C2, VT); 990 case ISD::OR : return getConstant(C1 | C2, VT); 991 case ISD::XOR : return getConstant(C1 ^ C2, VT); 992 case ISD::SHL : return getConstant(C1 << C2, VT); 993 case ISD::SRL : return getConstant(C1 >> C2, VT); 994 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); 995 default: break; 996 } 997 } else { // Cannonicalize constant to RHS if commutative 998 if (isCommutativeBinOp(Opcode)) { 999 std::swap(N1C, N2C); 1000 std::swap(N1, N2); 1001 } 1002 } 1003 } 1004 1005 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); 1006 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); 1007 if (N1CFP) { 1008 if (N2CFP) { 1009 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); 1010 switch (Opcode) { 1011 case ISD::FADD: return getConstantFP(C1 + C2, VT); 1012 case ISD::FSUB: return getConstantFP(C1 - C2, VT); 1013 case ISD::FMUL: return getConstantFP(C1 * C2, VT); 1014 case ISD::FDIV: 1015 if (C2) return getConstantFP(C1 / C2, VT); 1016 break; 1017 case ISD::FREM : 1018 if (C2) return getConstantFP(fmod(C1, C2), VT); 1019 break; 1020 default: break; 1021 } 1022 } else { // Cannonicalize constant to RHS if commutative 1023 if (isCommutativeBinOp(Opcode)) { 1024 std::swap(N1CFP, N2CFP); 1025 std::swap(N1, N2); 1026 } 1027 } 1028 } 1029 1030 // Finally, fold operations that do not require constants. 1031 switch (Opcode) { 1032 case ISD::FP_ROUND_INREG: 1033 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 1034 break; 1035 case ISD::SIGN_EXTEND_INREG: { 1036 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1037 if (EVT == VT) return N1; // Not actually extending 1038 break; 1039 } 1040 1041 // FIXME: figure out how to safely handle things like 1042 // int foo(int x) { return 1 << (x & 255); } 1043 // int bar() { return foo(256); } 1044#if 0 1045 case ISD::SHL: 1046 case ISD::SRL: 1047 case ISD::SRA: 1048 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && 1049 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) 1050 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1051 else if (N2.getOpcode() == ISD::AND) 1052 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { 1053 // If the and is only masking out bits that cannot effect the shift, 1054 // eliminate the and. 1055 unsigned NumBits = MVT::getSizeInBits(VT); 1056 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1057 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1058 } 1059 break; 1060#endif 1061 } 1062 1063 // Memoize this node if possible. 1064 SDNode *N; 1065 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END && 1066 VT != MVT::Flag) { 1067 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))]; 1068 if (BON) return SDOperand(BON, 0); 1069 1070 BON = N = new SDNode(Opcode, N1, N2); 1071 } else { 1072 N = new SDNode(Opcode, N1, N2); 1073 } 1074 1075 N->setValueTypes(VT); 1076 AllNodes.push_back(N); 1077 return SDOperand(N, 0); 1078} 1079 1080// setAdjCallChain - This method changes the token chain of an 1081// CALLSEQ_START/END node to be the specified operand. 1082void SDNode::setAdjCallChain(SDOperand N) { 1083 assert(N.getValueType() == MVT::Other); 1084 assert((getOpcode() == ISD::CALLSEQ_START || 1085 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!"); 1086 1087 OperandList[0].Val->removeUser(this); 1088 OperandList[0] = N; 1089 OperandList[0].Val->Uses.push_back(this); 1090} 1091 1092 1093 1094SDOperand SelectionDAG::getLoad(MVT::ValueType VT, 1095 SDOperand Chain, SDOperand Ptr, 1096 SDOperand SV) { 1097 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))]; 1098 if (N) return SDOperand(N, 0); 1099 N = new SDNode(ISD::LOAD, Chain, Ptr, SV); 1100 1101 // Loads have a token chain. 1102 setNodeValueTypes(N, VT, MVT::Other); 1103 AllNodes.push_back(N); 1104 return SDOperand(N, 0); 1105} 1106 1107SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT, 1108 SDOperand Chain, SDOperand Ptr, 1109 SDOperand SV) { 1110 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))]; 1111 if (N) return SDOperand(N, 0); 1112 std::vector<SDOperand> Ops; 1113 Ops.reserve(5); 1114 Ops.push_back(Chain); 1115 Ops.push_back(Ptr); 1116 Ops.push_back(getConstant(Count, MVT::i32)); 1117 Ops.push_back(getValueType(EVT)); 1118 Ops.push_back(SV); 1119 std::vector<MVT::ValueType> VTs; 1120 VTs.reserve(2); 1121 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain. 1122 return getNode(ISD::VLOAD, VTs, Ops); 1123} 1124 1125SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT, 1126 SDOperand Chain, SDOperand Ptr, SDOperand SV, 1127 MVT::ValueType EVT) { 1128 std::vector<SDOperand> Ops; 1129 Ops.reserve(4); 1130 Ops.push_back(Chain); 1131 Ops.push_back(Ptr); 1132 Ops.push_back(SV); 1133 Ops.push_back(getValueType(EVT)); 1134 std::vector<MVT::ValueType> VTs; 1135 VTs.reserve(2); 1136 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. 1137 return getNode(Opcode, VTs, Ops); 1138} 1139 1140SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1141 SDOperand N1, SDOperand N2, SDOperand N3) { 1142 // Perform various simplifications. 1143 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1144 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1145 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); 1146 switch (Opcode) { 1147 case ISD::SETCC: { 1148 // Use SimplifySetCC to simplify SETCC's. 1149 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); 1150 if (Simp.Val) return Simp; 1151 break; 1152 } 1153 case ISD::SELECT: 1154 if (N1C) 1155 if (N1C->getValue()) 1156 return N2; // select true, X, Y -> X 1157 else 1158 return N3; // select false, X, Y -> Y 1159 1160 if (N2 == N3) return N2; // select C, X, X -> X 1161 break; 1162 case ISD::BRCOND: 1163 if (N2C) 1164 if (N2C->getValue()) // Unconditional branch 1165 return getNode(ISD::BR, MVT::Other, N1, N3); 1166 else 1167 return N1; // Never-taken branch 1168 break; 1169 } 1170 1171 std::vector<SDOperand> Ops; 1172 Ops.reserve(3); 1173 Ops.push_back(N1); 1174 Ops.push_back(N2); 1175 Ops.push_back(N3); 1176 1177 // Memoize node if it doesn't produce a flag. 1178 SDNode *N; 1179 if (VT != MVT::Flag) { 1180 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))]; 1181 if (E) return SDOperand(E, 0); 1182 E = N = new SDNode(Opcode, N1, N2, N3); 1183 } else { 1184 N = new SDNode(Opcode, N1, N2, N3); 1185 } 1186 N->setValueTypes(VT); 1187 AllNodes.push_back(N); 1188 return SDOperand(N, 0); 1189} 1190 1191SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1192 SDOperand N1, SDOperand N2, SDOperand N3, 1193 SDOperand N4) { 1194 std::vector<SDOperand> Ops; 1195 Ops.reserve(4); 1196 Ops.push_back(N1); 1197 Ops.push_back(N2); 1198 Ops.push_back(N3); 1199 Ops.push_back(N4); 1200 return getNode(Opcode, VT, Ops); 1201} 1202 1203SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1204 SDOperand N1, SDOperand N2, SDOperand N3, 1205 SDOperand N4, SDOperand N5) { 1206 std::vector<SDOperand> Ops; 1207 Ops.reserve(5); 1208 Ops.push_back(N1); 1209 Ops.push_back(N2); 1210 Ops.push_back(N3); 1211 Ops.push_back(N4); 1212 Ops.push_back(N5); 1213 return getNode(Opcode, VT, Ops); 1214} 1215 1216 1217SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { 1218 assert((!V || isa<PointerType>(V->getType())) && 1219 "SrcValue is not a pointer?"); 1220 SDNode *&N = ValueNodes[std::make_pair(V, Offset)]; 1221 if (N) return SDOperand(N, 0); 1222 1223 N = new SrcValueSDNode(V, Offset); 1224 AllNodes.push_back(N); 1225 return SDOperand(N, 0); 1226} 1227 1228SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1229 std::vector<SDOperand> &Ops) { 1230 switch (Ops.size()) { 1231 case 0: return getNode(Opcode, VT); 1232 case 1: return getNode(Opcode, VT, Ops[0]); 1233 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); 1234 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); 1235 default: break; 1236 } 1237 1238 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val); 1239 switch (Opcode) { 1240 default: break; 1241 case ISD::BRCONDTWOWAY: 1242 if (N1C) 1243 if (N1C->getValue()) // Unconditional branch to true dest. 1244 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]); 1245 else // Unconditional branch to false dest. 1246 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]); 1247 break; 1248 case ISD::BRTWOWAY_CC: 1249 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!"); 1250 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1251 "LHS and RHS of comparison must have same type!"); 1252 break; 1253 case ISD::TRUNCSTORE: { 1254 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!"); 1255 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT(); 1256#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store 1257 // If this is a truncating store of a constant, convert to the desired type 1258 // and store it instead. 1259 if (isa<Constant>(Ops[0])) { 1260 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); 1261 if (isa<Constant>(Op)) 1262 N1 = Op; 1263 } 1264 // Also for ConstantFP? 1265#endif 1266 if (Ops[0].getValueType() == EVT) // Normal store? 1267 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]); 1268 assert(Ops[1].getValueType() > EVT && "Not a truncation?"); 1269 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) && 1270 "Can't do FP-INT conversion!"); 1271 break; 1272 } 1273 case ISD::SELECT_CC: { 1274 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!"); 1275 assert(Ops[0].getValueType() == Ops[1].getValueType() && 1276 "LHS and RHS of condition must have same type!"); 1277 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1278 "True and False arms of SelectCC must have same type!"); 1279 assert(Ops[2].getValueType() == VT && 1280 "select_cc node must be of same type as true and false value!"); 1281 break; 1282 } 1283 case ISD::BR_CC: { 1284 assert(Ops.size() == 5 && "BR_CC takes 5 operands!"); 1285 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1286 "LHS/RHS of comparison should match types!"); 1287 break; 1288 } 1289 } 1290 1291 // Memoize nodes. 1292 SDNode *N; 1293 if (VT != MVT::Flag) { 1294 SDNode *&E = 1295 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))]; 1296 if (E) return SDOperand(E, 0); 1297 E = N = new SDNode(Opcode, Ops); 1298 } else { 1299 N = new SDNode(Opcode, Ops); 1300 } 1301 N->setValueTypes(VT); 1302 AllNodes.push_back(N); 1303 return SDOperand(N, 0); 1304} 1305 1306SDOperand SelectionDAG::getNode(unsigned Opcode, 1307 std::vector<MVT::ValueType> &ResultTys, 1308 std::vector<SDOperand> &Ops) { 1309 if (ResultTys.size() == 1) 1310 return getNode(Opcode, ResultTys[0], Ops); 1311 1312 switch (Opcode) { 1313 case ISD::EXTLOAD: 1314 case ISD::SEXTLOAD: 1315 case ISD::ZEXTLOAD: { 1316 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT(); 1317 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!"); 1318 // If they are asking for an extending load from/to the same thing, return a 1319 // normal load. 1320 if (ResultTys[0] == EVT) 1321 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]); 1322 assert(EVT < ResultTys[0] && 1323 "Should only be an extending load, not truncating!"); 1324 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) && 1325 "Cannot sign/zero extend a FP load!"); 1326 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) && 1327 "Cannot convert from FP to Int or Int -> FP!"); 1328 break; 1329 } 1330 1331 // FIXME: figure out how to safely handle things like 1332 // int foo(int x) { return 1 << (x & 255); } 1333 // int bar() { return foo(256); } 1334#if 0 1335 case ISD::SRA_PARTS: 1336 case ISD::SRL_PARTS: 1337 case ISD::SHL_PARTS: 1338 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 1339 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 1340 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1341 else if (N3.getOpcode() == ISD::AND) 1342 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 1343 // If the and is only masking out bits that cannot effect the shift, 1344 // eliminate the and. 1345 unsigned NumBits = MVT::getSizeInBits(VT)*2; 1346 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1347 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1348 } 1349 break; 1350#endif 1351 } 1352 1353 // Memoize the node unless it returns a flag. 1354 SDNode *N; 1355 if (ResultTys.back() != MVT::Flag) { 1356 SDNode *&E = 1357 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))]; 1358 if (E) return SDOperand(E, 0); 1359 E = N = new SDNode(Opcode, Ops); 1360 } else { 1361 N = new SDNode(Opcode, Ops); 1362 } 1363 setNodeValueTypes(N, ResultTys); 1364 AllNodes.push_back(N); 1365 return SDOperand(N, 0); 1366} 1367 1368void SelectionDAG::setNodeValueTypes(SDNode *N, 1369 std::vector<MVT::ValueType> &RetVals) { 1370 switch (RetVals.size()) { 1371 case 0: return; 1372 case 1: N->setValueTypes(RetVals[0]); return; 1373 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return; 1374 default: break; 1375 } 1376 1377 std::list<std::vector<MVT::ValueType> >::iterator I = 1378 std::find(VTList.begin(), VTList.end(), RetVals); 1379 if (I == VTList.end()) { 1380 VTList.push_front(RetVals); 1381 I = VTList.begin(); 1382 } 1383 1384 N->setValueTypes(&(*I)[0], I->size()); 1385} 1386 1387void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1, 1388 MVT::ValueType VT2) { 1389 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 1390 E = VTList.end(); I != E; ++I) { 1391 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) { 1392 N->setValueTypes(&(*I)[0], 2); 1393 return; 1394 } 1395 } 1396 std::vector<MVT::ValueType> V; 1397 V.push_back(VT1); 1398 V.push_back(VT2); 1399 VTList.push_front(V); 1400 N->setValueTypes(&(*VTList.begin())[0], 2); 1401} 1402 1403 1404/// SelectNodeTo - These are used for target selectors to *mutate* the 1405/// specified node to have the specified return type, Target opcode, and 1406/// operands. Note that target opcodes are stored as 1407/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 1408void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1409 MVT::ValueType VT) { 1410 RemoveNodeFromCSEMaps(N); 1411 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1412 N->setValueTypes(VT); 1413} 1414 1415void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1416 MVT::ValueType VT, SDOperand Op1) { 1417 RemoveNodeFromCSEMaps(N); 1418 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1419 N->setValueTypes(VT); 1420 N->setOperands(Op1); 1421} 1422 1423void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1424 MVT::ValueType VT, SDOperand Op1, 1425 SDOperand Op2) { 1426 RemoveNodeFromCSEMaps(N); 1427 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1428 N->setValueTypes(VT); 1429 N->setOperands(Op1, Op2); 1430} 1431 1432void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1433 MVT::ValueType VT, SDOperand Op1, 1434 SDOperand Op2, SDOperand Op3) { 1435 RemoveNodeFromCSEMaps(N); 1436 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1437 N->setValueTypes(VT); 1438 N->setOperands(Op1, Op2, Op3); 1439} 1440 1441void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1442 MVT::ValueType VT, SDOperand Op1, 1443 SDOperand Op2, SDOperand Op3, SDOperand Op4) { 1444 RemoveNodeFromCSEMaps(N); 1445 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1446 N->setValueTypes(VT); 1447 N->setOperands(Op1, Op2, Op3, Op4); 1448} 1449 1450void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1451 MVT::ValueType VT, SDOperand Op1, 1452 SDOperand Op2, SDOperand Op3, SDOperand Op4, 1453 SDOperand Op5) { 1454 RemoveNodeFromCSEMaps(N); 1455 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1456 N->setValueTypes(VT); 1457 N->setOperands(Op1, Op2, Op3, Op4, Op5); 1458} 1459 1460void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1461 MVT::ValueType VT1, MVT::ValueType VT2, 1462 SDOperand Op1, SDOperand Op2) { 1463 RemoveNodeFromCSEMaps(N); 1464 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1465 setNodeValueTypes(N, VT1, VT2); 1466 N->setOperands(Op1, Op2); 1467} 1468 1469void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1470 MVT::ValueType VT1, MVT::ValueType VT2, 1471 SDOperand Op1, SDOperand Op2, SDOperand Op3) { 1472 RemoveNodeFromCSEMaps(N); 1473 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1474 setNodeValueTypes(N, VT1, VT2); 1475 N->setOperands(Op1, Op2, Op3); 1476} 1477 1478void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1479 MVT::ValueType VT1, MVT::ValueType VT2, 1480 SDOperand Op1, SDOperand Op2, 1481 SDOperand Op3, SDOperand Op4) { 1482 RemoveNodeFromCSEMaps(N); 1483 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1484 setNodeValueTypes(N, VT1, VT2); 1485 N->setOperands(Op1, Op2, Op3, Op4); 1486} 1487 1488void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1489 MVT::ValueType VT1, MVT::ValueType VT2, 1490 SDOperand Op1, SDOperand Op2, 1491 SDOperand Op3, SDOperand Op4, SDOperand Op5) { 1492 RemoveNodeFromCSEMaps(N); 1493 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1494 setNodeValueTypes(N, VT1, VT2); 1495 N->setOperands(Op1, Op2, Op3, Op4, Op5); 1496} 1497 1498// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 1499/// This can cause recursive merging of nodes in the DAG. 1500/// 1501/// This version assumes From/To have a single result value. 1502/// 1503void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, 1504 std::vector<SDNode*> *Deleted) { 1505 SDNode *From = FromN.Val, *To = ToN.Val; 1506 assert(From->getNumValues() == 1 && To->getNumValues() == 1 && 1507 "Cannot replace with this method!"); 1508 assert(From != To && "Cannot replace uses of with self"); 1509 1510 while (!From->use_empty()) { 1511 // Process users until they are all gone. 1512 SDNode *U = *From->use_begin(); 1513 1514 // This node is about to morph, remove its old self from the CSE maps. 1515 RemoveNodeFromCSEMaps(U); 1516 1517 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 1518 I != E; ++I) 1519 if (I->Val == From) { 1520 From->removeUser(U); 1521 I->Val = To; 1522 To->addUser(U); 1523 } 1524 1525 // Now that we have modified U, add it back to the CSE maps. If it already 1526 // exists there, recursively merge the results together. 1527 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 1528 ReplaceAllUsesWith(U, Existing, Deleted); 1529 // U is now dead. 1530 if (Deleted) Deleted->push_back(U); 1531 DeleteNodeNotInCSEMaps(U); 1532 } 1533 } 1534} 1535 1536/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 1537/// This can cause recursive merging of nodes in the DAG. 1538/// 1539/// This version assumes From/To have matching types and numbers of result 1540/// values. 1541/// 1542void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 1543 std::vector<SDNode*> *Deleted) { 1544 assert(From != To && "Cannot replace uses of with self"); 1545 assert(From->getNumValues() == To->getNumValues() && 1546 "Cannot use this version of ReplaceAllUsesWith!"); 1547 if (From->getNumValues() == 1) { // If possible, use the faster version. 1548 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); 1549 return; 1550 } 1551 1552 while (!From->use_empty()) { 1553 // Process users until they are all gone. 1554 SDNode *U = *From->use_begin(); 1555 1556 // This node is about to morph, remove its old self from the CSE maps. 1557 RemoveNodeFromCSEMaps(U); 1558 1559 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 1560 I != E; ++I) 1561 if (I->Val == From) { 1562 From->removeUser(U); 1563 I->Val = To; 1564 To->addUser(U); 1565 } 1566 1567 // Now that we have modified U, add it back to the CSE maps. If it already 1568 // exists there, recursively merge the results together. 1569 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 1570 ReplaceAllUsesWith(U, Existing, Deleted); 1571 // U is now dead. 1572 if (Deleted) Deleted->push_back(U); 1573 DeleteNodeNotInCSEMaps(U); 1574 } 1575 } 1576} 1577 1578/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 1579/// This can cause recursive merging of nodes in the DAG. 1580/// 1581/// This version can replace From with any result values. To must match the 1582/// number and types of values returned by From. 1583void SelectionDAG::ReplaceAllUsesWith(SDNode *From, 1584 const std::vector<SDOperand> &To, 1585 std::vector<SDNode*> *Deleted) { 1586 assert(From->getNumValues() == To.size() && 1587 "Incorrect number of values to replace with!"); 1588 if (To.size() == 1 && To[0].Val->getNumValues() == 1) { 1589 // Degenerate case handled above. 1590 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); 1591 return; 1592 } 1593 1594 while (!From->use_empty()) { 1595 // Process users until they are all gone. 1596 SDNode *U = *From->use_begin(); 1597 1598 // This node is about to morph, remove its old self from the CSE maps. 1599 RemoveNodeFromCSEMaps(U); 1600 1601 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 1602 I != E; ++I) 1603 if (I->Val == From) { 1604 const SDOperand &ToOp = To[I->ResNo]; 1605 From->removeUser(U); 1606 *I = ToOp; 1607 ToOp.Val->addUser(U); 1608 } 1609 1610 // Now that we have modified U, add it back to the CSE maps. If it already 1611 // exists there, recursively merge the results together. 1612 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 1613 ReplaceAllUsesWith(U, Existing, Deleted); 1614 // U is now dead. 1615 if (Deleted) Deleted->push_back(U); 1616 DeleteNodeNotInCSEMaps(U); 1617 } 1618 } 1619} 1620 1621 1622//===----------------------------------------------------------------------===// 1623// SDNode Class 1624//===----------------------------------------------------------------------===// 1625 1626 1627/// getValueTypeList - Return a pointer to the specified value type. 1628/// 1629MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { 1630 static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; 1631 VTs[VT] = VT; 1632 return &VTs[VT]; 1633} 1634 1635/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 1636/// indicated value. This method ignores uses of other values defined by this 1637/// operation. 1638bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) { 1639 assert(Value < getNumValues() && "Bad value!"); 1640 1641 // If there is only one value, this is easy. 1642 if (getNumValues() == 1) 1643 return use_size() == NUses; 1644 if (Uses.size() < NUses) return false; 1645 1646 SDOperand TheValue(this, Value); 1647 1648 std::set<SDNode*> UsersHandled; 1649 1650 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end(); 1651 UI != E; ++UI) { 1652 SDNode *User = *UI; 1653 if (User->getNumOperands() == 1 || 1654 UsersHandled.insert(User).second) // First time we've seen this? 1655 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 1656 if (User->getOperand(i) == TheValue) { 1657 if (NUses == 0) 1658 return false; // too many uses 1659 --NUses; 1660 } 1661 } 1662 1663 // Found exactly the right number of uses? 1664 return NUses == 0; 1665} 1666 1667 1668const char *SDNode::getOperationName(const SelectionDAG *G) const { 1669 switch (getOpcode()) { 1670 default: 1671 if (getOpcode() < ISD::BUILTIN_OP_END) 1672 return "<<Unknown DAG Node>>"; 1673 else { 1674 if (G) 1675 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 1676 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) 1677 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); 1678 return "<<Unknown Target Node>>"; 1679 } 1680 1681 case ISD::PCMARKER: return "PCMarker"; 1682 case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 1683 case ISD::SRCVALUE: return "SrcValue"; 1684 case ISD::VALUETYPE: return "ValueType"; 1685 case ISD::STRING: return "String"; 1686 case ISD::EntryToken: return "EntryToken"; 1687 case ISD::TokenFactor: return "TokenFactor"; 1688 case ISD::AssertSext: return "AssertSext"; 1689 case ISD::AssertZext: return "AssertZext"; 1690 case ISD::Constant: return "Constant"; 1691 case ISD::TargetConstant: return "TargetConstant"; 1692 case ISD::ConstantFP: return "ConstantFP"; 1693 case ISD::GlobalAddress: return "GlobalAddress"; 1694 case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 1695 case ISD::FrameIndex: return "FrameIndex"; 1696 case ISD::TargetFrameIndex: return "TargetFrameIndex"; 1697 case ISD::BasicBlock: return "BasicBlock"; 1698 case ISD::Register: return "Register"; 1699 case ISD::ExternalSymbol: return "ExternalSymbol"; 1700 case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 1701 case ISD::ConstantPool: return "ConstantPool"; 1702 case ISD::TargetConstantPool: return "TargetConstantPool"; 1703 case ISD::CopyToReg: return "CopyToReg"; 1704 case ISD::CopyFromReg: return "CopyFromReg"; 1705 case ISD::ImplicitDef: return "ImplicitDef"; 1706 case ISD::UNDEF: return "undef"; 1707 1708 // Unary operators 1709 case ISD::FABS: return "fabs"; 1710 case ISD::FNEG: return "fneg"; 1711 case ISD::FSQRT: return "fsqrt"; 1712 case ISD::FSIN: return "fsin"; 1713 case ISD::FCOS: return "fcos"; 1714 1715 // Binary operators 1716 case ISD::ADD: return "add"; 1717 case ISD::SUB: return "sub"; 1718 case ISD::MUL: return "mul"; 1719 case ISD::MULHU: return "mulhu"; 1720 case ISD::MULHS: return "mulhs"; 1721 case ISD::SDIV: return "sdiv"; 1722 case ISD::UDIV: return "udiv"; 1723 case ISD::SREM: return "srem"; 1724 case ISD::UREM: return "urem"; 1725 case ISD::AND: return "and"; 1726 case ISD::OR: return "or"; 1727 case ISD::XOR: return "xor"; 1728 case ISD::SHL: return "shl"; 1729 case ISD::SRA: return "sra"; 1730 case ISD::SRL: return "srl"; 1731 case ISD::FADD: return "fadd"; 1732 case ISD::FSUB: return "fsub"; 1733 case ISD::FMUL: return "fmul"; 1734 case ISD::FDIV: return "fdiv"; 1735 case ISD::FREM: return "frem"; 1736 case ISD::VADD: return "vadd"; 1737 case ISD::VSUB: return "vsub"; 1738 case ISD::VMUL: return "vmul"; 1739 1740 case ISD::SETCC: return "setcc"; 1741 case ISD::SELECT: return "select"; 1742 case ISD::SELECT_CC: return "select_cc"; 1743 case ISD::ADD_PARTS: return "add_parts"; 1744 case ISD::SUB_PARTS: return "sub_parts"; 1745 case ISD::SHL_PARTS: return "shl_parts"; 1746 case ISD::SRA_PARTS: return "sra_parts"; 1747 case ISD::SRL_PARTS: return "srl_parts"; 1748 1749 // Conversion operators. 1750 case ISD::SIGN_EXTEND: return "sign_extend"; 1751 case ISD::ZERO_EXTEND: return "zero_extend"; 1752 case ISD::ANY_EXTEND: return "any_extend"; 1753 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 1754 case ISD::TRUNCATE: return "truncate"; 1755 case ISD::FP_ROUND: return "fp_round"; 1756 case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 1757 case ISD::FP_EXTEND: return "fp_extend"; 1758 1759 case ISD::SINT_TO_FP: return "sint_to_fp"; 1760 case ISD::UINT_TO_FP: return "uint_to_fp"; 1761 case ISD::FP_TO_SINT: return "fp_to_sint"; 1762 case ISD::FP_TO_UINT: return "fp_to_uint"; 1763 1764 // Control flow instructions 1765 case ISD::BR: return "br"; 1766 case ISD::BRCOND: return "brcond"; 1767 case ISD::BRCONDTWOWAY: return "brcondtwoway"; 1768 case ISD::BR_CC: return "br_cc"; 1769 case ISD::BRTWOWAY_CC: return "brtwoway_cc"; 1770 case ISD::RET: return "ret"; 1771 case ISD::CALL: return "call"; 1772 case ISD::TAILCALL:return "tailcall"; 1773 case ISD::CALLSEQ_START: return "callseq_start"; 1774 case ISD::CALLSEQ_END: return "callseq_end"; 1775 1776 // Other operators 1777 case ISD::LOAD: return "load"; 1778 case ISD::STORE: return "store"; 1779 case ISD::VLOAD: return "vload"; 1780 case ISD::EXTLOAD: return "extload"; 1781 case ISD::SEXTLOAD: return "sextload"; 1782 case ISD::ZEXTLOAD: return "zextload"; 1783 case ISD::TRUNCSTORE: return "truncstore"; 1784 1785 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 1786 case ISD::EXTRACT_ELEMENT: return "extract_element"; 1787 case ISD::BUILD_PAIR: return "build_pair"; 1788 case ISD::MEMSET: return "memset"; 1789 case ISD::MEMCPY: return "memcpy"; 1790 case ISD::MEMMOVE: return "memmove"; 1791 1792 // Bit counting 1793 case ISD::CTPOP: return "ctpop"; 1794 case ISD::CTTZ: return "cttz"; 1795 case ISD::CTLZ: return "ctlz"; 1796 1797 // IO Intrinsics 1798 case ISD::READPORT: return "readport"; 1799 case ISD::WRITEPORT: return "writeport"; 1800 case ISD::READIO: return "readio"; 1801 case ISD::WRITEIO: return "writeio"; 1802 1803 // Debug info 1804 case ISD::LOCATION: return "location"; 1805 1806 case ISD::CONDCODE: 1807 switch (cast<CondCodeSDNode>(this)->get()) { 1808 default: assert(0 && "Unknown setcc condition!"); 1809 case ISD::SETOEQ: return "setoeq"; 1810 case ISD::SETOGT: return "setogt"; 1811 case ISD::SETOGE: return "setoge"; 1812 case ISD::SETOLT: return "setolt"; 1813 case ISD::SETOLE: return "setole"; 1814 case ISD::SETONE: return "setone"; 1815 1816 case ISD::SETO: return "seto"; 1817 case ISD::SETUO: return "setuo"; 1818 case ISD::SETUEQ: return "setue"; 1819 case ISD::SETUGT: return "setugt"; 1820 case ISD::SETUGE: return "setuge"; 1821 case ISD::SETULT: return "setult"; 1822 case ISD::SETULE: return "setule"; 1823 case ISD::SETUNE: return "setune"; 1824 1825 case ISD::SETEQ: return "seteq"; 1826 case ISD::SETGT: return "setgt"; 1827 case ISD::SETGE: return "setge"; 1828 case ISD::SETLT: return "setlt"; 1829 case ISD::SETLE: return "setle"; 1830 case ISD::SETNE: return "setne"; 1831 } 1832 } 1833} 1834 1835void SDNode::dump() const { dump(0); } 1836void SDNode::dump(const SelectionDAG *G) const { 1837 std::cerr << (void*)this << ": "; 1838 1839 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 1840 if (i) std::cerr << ","; 1841 if (getValueType(i) == MVT::Other) 1842 std::cerr << "ch"; 1843 else 1844 std::cerr << MVT::getValueTypeString(getValueType(i)); 1845 } 1846 std::cerr << " = " << getOperationName(G); 1847 1848 std::cerr << " "; 1849 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1850 if (i) std::cerr << ", "; 1851 std::cerr << (void*)getOperand(i).Val; 1852 if (unsigned RN = getOperand(i).ResNo) 1853 std::cerr << ":" << RN; 1854 } 1855 1856 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 1857 std::cerr << "<" << CSDN->getValue() << ">"; 1858 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 1859 std::cerr << "<" << CSDN->getValue() << ">"; 1860 } else if (const GlobalAddressSDNode *GADN = 1861 dyn_cast<GlobalAddressSDNode>(this)) { 1862 std::cerr << "<"; 1863 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; 1864 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 1865 std::cerr << "<" << FIDN->getIndex() << ">"; 1866 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 1867 std::cerr << "<" << *CP->get() << ">"; 1868 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 1869 std::cerr << "<"; 1870 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 1871 if (LBB) 1872 std::cerr << LBB->getName() << " "; 1873 std::cerr << (const void*)BBDN->getBasicBlock() << ">"; 1874 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 1875 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) { 1876 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); 1877 } else { 1878 std::cerr << " #" << R->getReg(); 1879 } 1880 } else if (const ExternalSymbolSDNode *ES = 1881 dyn_cast<ExternalSymbolSDNode>(this)) { 1882 std::cerr << "'" << ES->getSymbol() << "'"; 1883 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 1884 if (M->getValue()) 1885 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; 1886 else 1887 std::cerr << "<null:" << M->getOffset() << ">"; 1888 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 1889 std::cerr << ":" << getValueTypeString(N->getVT()); 1890 } 1891} 1892 1893static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 1894 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 1895 if (N->getOperand(i).Val->hasOneUse()) 1896 DumpNodes(N->getOperand(i).Val, indent+2, G); 1897 else 1898 std::cerr << "\n" << std::string(indent+2, ' ') 1899 << (void*)N->getOperand(i).Val << ": <multiple use>"; 1900 1901 1902 std::cerr << "\n" << std::string(indent, ' '); 1903 N->dump(G); 1904} 1905 1906void SelectionDAG::dump() const { 1907 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; 1908 std::vector<const SDNode*> Nodes; 1909 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 1910 I != E; ++I) 1911 Nodes.push_back(I); 1912 1913 std::sort(Nodes.begin(), Nodes.end()); 1914 1915 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 1916 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) 1917 DumpNodes(Nodes[i], 2, this); 1918 } 1919 1920 DumpNodes(getRoot().Val, 2, this); 1921 1922 std::cerr << "\n\n"; 1923} 1924 1925