SelectionDAG.cpp revision 37ce9df0da6cddc3b8bfef9b63d33d058a0f2f15
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/GlobalVariable.h" 17#include "llvm/Intrinsics.h" 18#include "llvm/DerivedTypes.h" 19#include "llvm/Assembly/Writer.h" 20#include "llvm/CodeGen/MachineBasicBlock.h" 21#include "llvm/CodeGen/MachineConstantPool.h" 22#include "llvm/CodeGen/MachineFrameInfo.h" 23#include "llvm/Support/MathExtras.h" 24#include "llvm/Target/MRegisterInfo.h" 25#include "llvm/Target/TargetData.h" 26#include "llvm/Target/TargetLowering.h" 27#include "llvm/Target/TargetInstrInfo.h" 28#include "llvm/Target/TargetMachine.h" 29#include "llvm/ADT/SetVector.h" 30#include "llvm/ADT/SmallPtrSet.h" 31#include "llvm/ADT/SmallVector.h" 32#include "llvm/ADT/StringExtras.h" 33#include <algorithm> 34#include <cmath> 35using namespace llvm; 36 37/// makeVTList - Return an instance of the SDVTList struct initialized with the 38/// specified members. 39static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) { 40 SDVTList Res = {VTs, NumVTs}; 41 return Res; 42} 43 44//===----------------------------------------------------------------------===// 45// ConstantFPSDNode Class 46//===----------------------------------------------------------------------===// 47 48/// isExactlyValue - We don't rely on operator== working on double values, as 49/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 50/// As such, this method can be used to do an exact bit-for-bit comparison of 51/// two floating point values. 52bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const { 53 return Value.bitwiseIsEqual(V); 54} 55 56bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT, 57 const APFloat& Val) { 58 // convert modifies in place, so make a copy. 59 APFloat Val2 = APFloat(Val); 60 switch (VT) { 61 default: 62 return false; // These can't be represented as floating point! 63 64 // FIXME rounding mode needs to be more flexible 65 case MVT::f32: 66 return &Val2.getSemantics() == &APFloat::IEEEsingle || 67 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) == 68 APFloat::opOK; 69 case MVT::f64: 70 return &Val2.getSemantics() == &APFloat::IEEEsingle || 71 &Val2.getSemantics() == &APFloat::IEEEdouble || 72 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) == 73 APFloat::opOK; 74 // TODO: Figure out how to test if we can use a shorter type instead! 75 case MVT::f80: 76 case MVT::f128: 77 case MVT::ppcf128: 78 return true; 79 } 80} 81 82//===----------------------------------------------------------------------===// 83// ISD Namespace 84//===----------------------------------------------------------------------===// 85 86/// isBuildVectorAllOnes - Return true if the specified node is a 87/// BUILD_VECTOR where all of the elements are ~0 or undef. 88bool ISD::isBuildVectorAllOnes(const SDNode *N) { 89 // Look through a bit convert. 90 if (N->getOpcode() == ISD::BIT_CONVERT) 91 N = N->getOperand(0).Val; 92 93 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 94 95 unsigned i = 0, e = N->getNumOperands(); 96 97 // Skip over all of the undef values. 98 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 99 ++i; 100 101 // Do not accept an all-undef vector. 102 if (i == e) return false; 103 104 // Do not accept build_vectors that aren't all constants or which have non-~0 105 // elements. 106 SDOperand NotZero = N->getOperand(i); 107 if (isa<ConstantSDNode>(NotZero)) { 108 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue()) 109 return false; 110 } else if (isa<ConstantFPSDNode>(NotZero)) { 111 MVT::ValueType VT = NotZero.getValueType(); 112 if (VT== MVT::f64) { 113 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF(). 114 convertToAPInt().getZExtValue())) != (uint64_t)-1) 115 return false; 116 } else { 117 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)-> 118 getValueAPF().convertToAPInt().getZExtValue() != 119 (uint32_t)-1) 120 return false; 121 } 122 } else 123 return false; 124 125 // Okay, we have at least one ~0 value, check to see if the rest match or are 126 // undefs. 127 for (++i; i != e; ++i) 128 if (N->getOperand(i) != NotZero && 129 N->getOperand(i).getOpcode() != ISD::UNDEF) 130 return false; 131 return true; 132} 133 134 135/// isBuildVectorAllZeros - Return true if the specified node is a 136/// BUILD_VECTOR where all of the elements are 0 or undef. 137bool ISD::isBuildVectorAllZeros(const SDNode *N) { 138 // Look through a bit convert. 139 if (N->getOpcode() == ISD::BIT_CONVERT) 140 N = N->getOperand(0).Val; 141 142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 143 144 unsigned i = 0, e = N->getNumOperands(); 145 146 // Skip over all of the undef values. 147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 148 ++i; 149 150 // Do not accept an all-undef vector. 151 if (i == e) return false; 152 153 // Do not accept build_vectors that aren't all constants or which have non-~0 154 // elements. 155 SDOperand Zero = N->getOperand(i); 156 if (isa<ConstantSDNode>(Zero)) { 157 if (!cast<ConstantSDNode>(Zero)->isNullValue()) 158 return false; 159 } else if (isa<ConstantFPSDNode>(Zero)) { 160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero()) 161 return false; 162 } else 163 return false; 164 165 // Okay, we have at least one ~0 value, check to see if the rest match or are 166 // undefs. 167 for (++i; i != e; ++i) 168 if (N->getOperand(i) != Zero && 169 N->getOperand(i).getOpcode() != ISD::UNDEF) 170 return false; 171 return true; 172} 173 174/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 175/// when given the operation for (X op Y). 176ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 177 // To perform this operation, we just need to swap the L and G bits of the 178 // operation. 179 unsigned OldL = (Operation >> 2) & 1; 180 unsigned OldG = (Operation >> 1) & 1; 181 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 182 (OldL << 1) | // New G bit 183 (OldG << 2)); // New L bit. 184} 185 186/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 187/// 'op' is a valid SetCC operation. 188ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 189 unsigned Operation = Op; 190 if (isInteger) 191 Operation ^= 7; // Flip L, G, E bits, but not U. 192 else 193 Operation ^= 15; // Flip all of the condition bits. 194 if (Operation > ISD::SETTRUE2) 195 Operation &= ~8; // Don't let N and U bits get set. 196 return ISD::CondCode(Operation); 197} 198 199 200/// isSignedOp - For an integer comparison, return 1 if the comparison is a 201/// signed operation and 2 if the result is an unsigned comparison. Return zero 202/// if the operation does not depend on the sign of the input (setne and seteq). 203static int isSignedOp(ISD::CondCode Opcode) { 204 switch (Opcode) { 205 default: assert(0 && "Illegal integer setcc operation!"); 206 case ISD::SETEQ: 207 case ISD::SETNE: return 0; 208 case ISD::SETLT: 209 case ISD::SETLE: 210 case ISD::SETGT: 211 case ISD::SETGE: return 1; 212 case ISD::SETULT: 213 case ISD::SETULE: 214 case ISD::SETUGT: 215 case ISD::SETUGE: return 2; 216 } 217} 218 219/// getSetCCOrOperation - Return the result of a logical OR between different 220/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 221/// returns SETCC_INVALID if it is not possible to represent the resultant 222/// comparison. 223ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 224 bool isInteger) { 225 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 226 // Cannot fold a signed integer setcc with an unsigned integer setcc. 227 return ISD::SETCC_INVALID; 228 229 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 230 231 // If the N and U bits get set then the resultant comparison DOES suddenly 232 // care about orderedness, and is true when ordered. 233 if (Op > ISD::SETTRUE2) 234 Op &= ~16; // Clear the U bit if the N bit is set. 235 236 // Canonicalize illegal integer setcc's. 237 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT 238 Op = ISD::SETNE; 239 240 return ISD::CondCode(Op); 241} 242 243/// getSetCCAndOperation - Return the result of a logical AND between different 244/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 245/// function returns zero if it is not possible to represent the resultant 246/// comparison. 247ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 248 bool isInteger) { 249 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 250 // Cannot fold a signed setcc with an unsigned setcc. 251 return ISD::SETCC_INVALID; 252 253 // Combine all of the condition bits. 254 ISD::CondCode Result = ISD::CondCode(Op1 & Op2); 255 256 // Canonicalize illegal integer setcc's. 257 if (isInteger) { 258 switch (Result) { 259 default: break; 260 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT 261 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE 262 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE 263 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE 264 } 265 } 266 267 return Result; 268} 269 270const TargetMachine &SelectionDAG::getTarget() const { 271 return TLI.getTargetMachine(); 272} 273 274//===----------------------------------------------------------------------===// 275// SDNode Profile Support 276//===----------------------------------------------------------------------===// 277 278/// AddNodeIDOpcode - Add the node opcode to the NodeID data. 279/// 280static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) { 281 ID.AddInteger(OpC); 282} 283 284/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them 285/// solely with their pointer. 286void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) { 287 ID.AddPointer(VTList.VTs); 288} 289 290/// AddNodeIDOperands - Various routines for adding operands to the NodeID data. 291/// 292static void AddNodeIDOperands(FoldingSetNodeID &ID, 293 const SDOperand *Ops, unsigned NumOps) { 294 for (; NumOps; --NumOps, ++Ops) { 295 ID.AddPointer(Ops->Val); 296 ID.AddInteger(Ops->ResNo); 297 } 298} 299 300static void AddNodeIDNode(FoldingSetNodeID &ID, 301 unsigned short OpC, SDVTList VTList, 302 const SDOperand *OpList, unsigned N) { 303 AddNodeIDOpcode(ID, OpC); 304 AddNodeIDValueTypes(ID, VTList); 305 AddNodeIDOperands(ID, OpList, N); 306} 307 308/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID 309/// data. 310static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) { 311 AddNodeIDOpcode(ID, N->getOpcode()); 312 // Add the return value info. 313 AddNodeIDValueTypes(ID, N->getVTList()); 314 // Add the operand info. 315 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands()); 316 317 // Handle SDNode leafs with special info. 318 switch (N->getOpcode()) { 319 default: break; // Normal nodes don't need extra info. 320 case ISD::TargetConstant: 321 case ISD::Constant: 322 ID.AddInteger(cast<ConstantSDNode>(N)->getValue()); 323 break; 324 case ISD::TargetConstantFP: 325 case ISD::ConstantFP: { 326 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF()); 327 break; 328 } 329 case ISD::TargetGlobalAddress: 330 case ISD::GlobalAddress: 331 case ISD::TargetGlobalTLSAddress: 332 case ISD::GlobalTLSAddress: { 333 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); 334 ID.AddPointer(GA->getGlobal()); 335 ID.AddInteger(GA->getOffset()); 336 break; 337 } 338 case ISD::BasicBlock: 339 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock()); 340 break; 341 case ISD::Register: 342 ID.AddInteger(cast<RegisterSDNode>(N)->getReg()); 343 break; 344 case ISD::SRCVALUE: { 345 SrcValueSDNode *SV = cast<SrcValueSDNode>(N); 346 ID.AddPointer(SV->getValue()); 347 ID.AddInteger(SV->getOffset()); 348 break; 349 } 350 case ISD::FrameIndex: 351 case ISD::TargetFrameIndex: 352 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex()); 353 break; 354 case ISD::JumpTable: 355 case ISD::TargetJumpTable: 356 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex()); 357 break; 358 case ISD::ConstantPool: 359 case ISD::TargetConstantPool: { 360 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N); 361 ID.AddInteger(CP->getAlignment()); 362 ID.AddInteger(CP->getOffset()); 363 if (CP->isMachineConstantPoolEntry()) 364 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID); 365 else 366 ID.AddPointer(CP->getConstVal()); 367 break; 368 } 369 case ISD::LOAD: { 370 LoadSDNode *LD = cast<LoadSDNode>(N); 371 ID.AddInteger(LD->getAddressingMode()); 372 ID.AddInteger(LD->getExtensionType()); 373 ID.AddInteger((unsigned int)(LD->getLoadedVT())); 374 ID.AddPointer(LD->getSrcValue()); 375 ID.AddInteger(LD->getSrcValueOffset()); 376 ID.AddInteger(LD->getAlignment()); 377 ID.AddInteger(LD->isVolatile()); 378 break; 379 } 380 case ISD::STORE: { 381 StoreSDNode *ST = cast<StoreSDNode>(N); 382 ID.AddInteger(ST->getAddressingMode()); 383 ID.AddInteger(ST->isTruncatingStore()); 384 ID.AddInteger((unsigned int)(ST->getStoredVT())); 385 ID.AddPointer(ST->getSrcValue()); 386 ID.AddInteger(ST->getSrcValueOffset()); 387 ID.AddInteger(ST->getAlignment()); 388 ID.AddInteger(ST->isVolatile()); 389 break; 390 } 391 } 392} 393 394//===----------------------------------------------------------------------===// 395// SelectionDAG Class 396//===----------------------------------------------------------------------===// 397 398/// RemoveDeadNodes - This method deletes all unreachable nodes in the 399/// SelectionDAG. 400void SelectionDAG::RemoveDeadNodes() { 401 // Create a dummy node (which is not added to allnodes), that adds a reference 402 // to the root node, preventing it from being deleted. 403 HandleSDNode Dummy(getRoot()); 404 405 SmallVector<SDNode*, 128> DeadNodes; 406 407 // Add all obviously-dead nodes to the DeadNodes worklist. 408 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 409 if (I->use_empty()) 410 DeadNodes.push_back(I); 411 412 // Process the worklist, deleting the nodes and adding their uses to the 413 // worklist. 414 while (!DeadNodes.empty()) { 415 SDNode *N = DeadNodes.back(); 416 DeadNodes.pop_back(); 417 418 // Take the node out of the appropriate CSE map. 419 RemoveNodeFromCSEMaps(N); 420 421 // Next, brutally remove the operand list. This is safe to do, as there are 422 // no cycles in the graph. 423 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 424 SDNode *Operand = I->Val; 425 Operand->removeUser(N); 426 427 // Now that we removed this operand, see if there are no uses of it left. 428 if (Operand->use_empty()) 429 DeadNodes.push_back(Operand); 430 } 431 if (N->OperandsNeedDelete) 432 delete[] N->OperandList; 433 N->OperandList = 0; 434 N->NumOperands = 0; 435 436 // Finally, remove N itself. 437 AllNodes.erase(N); 438 } 439 440 // If the root changed (e.g. it was a dead load, update the root). 441 setRoot(Dummy.getValue()); 442} 443 444void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) { 445 SmallVector<SDNode*, 16> DeadNodes; 446 DeadNodes.push_back(N); 447 448 // Process the worklist, deleting the nodes and adding their uses to the 449 // worklist. 450 while (!DeadNodes.empty()) { 451 SDNode *N = DeadNodes.back(); 452 DeadNodes.pop_back(); 453 454 // Take the node out of the appropriate CSE map. 455 RemoveNodeFromCSEMaps(N); 456 457 // Next, brutally remove the operand list. This is safe to do, as there are 458 // no cycles in the graph. 459 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 460 SDNode *Operand = I->Val; 461 Operand->removeUser(N); 462 463 // Now that we removed this operand, see if there are no uses of it left. 464 if (Operand->use_empty()) 465 DeadNodes.push_back(Operand); 466 } 467 if (N->OperandsNeedDelete) 468 delete[] N->OperandList; 469 N->OperandList = 0; 470 N->NumOperands = 0; 471 472 // Finally, remove N itself. 473 Deleted.push_back(N); 474 AllNodes.erase(N); 475 } 476} 477 478void SelectionDAG::DeleteNode(SDNode *N) { 479 assert(N->use_empty() && "Cannot delete a node that is not dead!"); 480 481 // First take this out of the appropriate CSE map. 482 RemoveNodeFromCSEMaps(N); 483 484 // Finally, remove uses due to operands of this node, remove from the 485 // AllNodes list, and delete the node. 486 DeleteNodeNotInCSEMaps(N); 487} 488 489void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 490 491 // Remove it from the AllNodes list. 492 AllNodes.remove(N); 493 494 // Drop all of the operands and decrement used nodes use counts. 495 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 496 I->Val->removeUser(N); 497 if (N->OperandsNeedDelete) 498 delete[] N->OperandList; 499 N->OperandList = 0; 500 N->NumOperands = 0; 501 502 delete N; 503} 504 505/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 506/// correspond to it. This is useful when we're about to delete or repurpose 507/// the node. We don't want future request for structurally identical nodes 508/// to return N anymore. 509void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 510 bool Erased = false; 511 switch (N->getOpcode()) { 512 case ISD::HANDLENODE: return; // noop. 513 case ISD::STRING: 514 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); 515 break; 516 case ISD::CONDCODE: 517 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 518 "Cond code doesn't exist!"); 519 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 520 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 521 break; 522 case ISD::ExternalSymbol: 523 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 524 break; 525 case ISD::TargetExternalSymbol: 526 Erased = 527 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 528 break; 529 case ISD::VALUETYPE: 530 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0; 531 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; 532 break; 533 default: 534 // Remove it from the CSE Map. 535 Erased = CSEMap.RemoveNode(N); 536 break; 537 } 538#ifndef NDEBUG 539 // Verify that the node was actually in one of the CSE maps, unless it has a 540 // flag result (which cannot be CSE'd) or is one of the special cases that are 541 // not subject to CSE. 542 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && 543 !N->isTargetOpcode()) { 544 N->dump(this); 545 cerr << "\n"; 546 assert(0 && "Node is not in map!"); 547 } 548#endif 549} 550 551/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It 552/// has been taken out and modified in some way. If the specified node already 553/// exists in the CSE maps, do not modify the maps, but return the existing node 554/// instead. If it doesn't exist, add it and return null. 555/// 556SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { 557 assert(N->getNumOperands() && "This is a leaf node!"); 558 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 559 return 0; // Never add these nodes. 560 561 // Check that remaining values produced are not flags. 562 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 563 if (N->getValueType(i) == MVT::Flag) 564 return 0; // Never CSE anything that produces a flag. 565 566 SDNode *New = CSEMap.GetOrInsertNode(N); 567 if (New != N) return New; // Node already existed. 568 return 0; 569} 570 571/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 572/// were replaced with those specified. If this node is never memoized, 573/// return null, otherwise return a pointer to the slot it would take. If a 574/// node already exists with these operands, the slot will be non-null. 575SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op, 576 void *&InsertPos) { 577 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 578 return 0; // Never add these nodes. 579 580 // Check that remaining values produced are not flags. 581 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 582 if (N->getValueType(i) == MVT::Flag) 583 return 0; // Never CSE anything that produces a flag. 584 585 SDOperand Ops[] = { Op }; 586 FoldingSetNodeID ID; 587 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1); 588 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 589} 590 591/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 592/// were replaced with those specified. If this node is never memoized, 593/// return null, otherwise return a pointer to the slot it would take. If a 594/// node already exists with these operands, the slot will be non-null. 595SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 596 SDOperand Op1, SDOperand Op2, 597 void *&InsertPos) { 598 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 599 return 0; // Never add these nodes. 600 601 // Check that remaining values produced are not flags. 602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 603 if (N->getValueType(i) == MVT::Flag) 604 return 0; // Never CSE anything that produces a flag. 605 606 SDOperand Ops[] = { Op1, Op2 }; 607 FoldingSetNodeID ID; 608 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2); 609 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 610} 611 612 613/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 614/// were replaced with those specified. If this node is never memoized, 615/// return null, otherwise return a pointer to the slot it would take. If a 616/// node already exists with these operands, the slot will be non-null. 617SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 618 const SDOperand *Ops,unsigned NumOps, 619 void *&InsertPos) { 620 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 621 return 0; // Never add these nodes. 622 623 // Check that remaining values produced are not flags. 624 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 625 if (N->getValueType(i) == MVT::Flag) 626 return 0; // Never CSE anything that produces a flag. 627 628 FoldingSetNodeID ID; 629 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps); 630 631 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 632 ID.AddInteger(LD->getAddressingMode()); 633 ID.AddInteger(LD->getExtensionType()); 634 ID.AddInteger((unsigned int)(LD->getLoadedVT())); 635 ID.AddPointer(LD->getSrcValue()); 636 ID.AddInteger(LD->getSrcValueOffset()); 637 ID.AddInteger(LD->getAlignment()); 638 ID.AddInteger(LD->isVolatile()); 639 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 640 ID.AddInteger(ST->getAddressingMode()); 641 ID.AddInteger(ST->isTruncatingStore()); 642 ID.AddInteger((unsigned int)(ST->getStoredVT())); 643 ID.AddPointer(ST->getSrcValue()); 644 ID.AddInteger(ST->getSrcValueOffset()); 645 ID.AddInteger(ST->getAlignment()); 646 ID.AddInteger(ST->isVolatile()); 647 } 648 649 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 650} 651 652 653SelectionDAG::~SelectionDAG() { 654 while (!AllNodes.empty()) { 655 SDNode *N = AllNodes.begin(); 656 N->SetNextInBucket(0); 657 if (N->OperandsNeedDelete) 658 delete [] N->OperandList; 659 N->OperandList = 0; 660 N->NumOperands = 0; 661 AllNodes.pop_front(); 662 } 663} 664 665SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { 666 if (Op.getValueType() == VT) return Op; 667 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); 668 return getNode(ISD::AND, Op.getValueType(), Op, 669 getConstant(Imm, Op.getValueType())); 670} 671 672SDOperand SelectionDAG::getString(const std::string &Val) { 673 StringSDNode *&N = StringNodes[Val]; 674 if (!N) { 675 N = new StringSDNode(Val); 676 AllNodes.push_back(N); 677 } 678 return SDOperand(N, 0); 679} 680 681SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) { 682 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 683 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!"); 684 685 // Mask out any bits that are not valid for this constant. 686 Val &= MVT::getIntVTBitMask(VT); 687 688 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; 689 FoldingSetNodeID ID; 690 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 691 ID.AddInteger(Val); 692 void *IP = 0; 693 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 694 return SDOperand(E, 0); 695 SDNode *N = new ConstantSDNode(isT, Val, VT); 696 CSEMap.InsertNode(N, IP); 697 AllNodes.push_back(N); 698 return SDOperand(N, 0); 699} 700 701SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT, 702 bool isTarget) { 703 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 704 705 MVT::ValueType EltVT = 706 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT; 707 708 // Do the map lookup using the actual bit pattern for the floating point 709 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 710 // we don't have issues with SNANs. 711 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; 712 FoldingSetNodeID ID; 713 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 714 ID.AddAPFloat(V); 715 void *IP = 0; 716 SDNode *N = NULL; 717 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 718 if (!MVT::isVector(VT)) 719 return SDOperand(N, 0); 720 if (!N) { 721 N = new ConstantFPSDNode(isTarget, V, EltVT); 722 CSEMap.InsertNode(N, IP); 723 AllNodes.push_back(N); 724 } 725 726 SDOperand Result(N, 0); 727 if (MVT::isVector(VT)) { 728 SmallVector<SDOperand, 8> Ops; 729 Ops.assign(MVT::getVectorNumElements(VT), Result); 730 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); 731 } 732 return Result; 733} 734 735SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT, 736 bool isTarget) { 737 MVT::ValueType EltVT = 738 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT; 739 if (EltVT==MVT::f32) 740 return getConstantFP(APFloat((float)Val), VT, isTarget); 741 else 742 return getConstantFP(APFloat(Val), VT, isTarget); 743} 744 745SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, 746 MVT::ValueType VT, int Offset, 747 bool isTargetGA) { 748 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 749 unsigned Opc; 750 if (GVar && GVar->isThreadLocal()) 751 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress; 752 else 753 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; 754 FoldingSetNodeID ID; 755 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 756 ID.AddPointer(GV); 757 ID.AddInteger(Offset); 758 void *IP = 0; 759 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 760 return SDOperand(E, 0); 761 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset); 762 CSEMap.InsertNode(N, IP); 763 AllNodes.push_back(N); 764 return SDOperand(N, 0); 765} 766 767SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT, 768 bool isTarget) { 769 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; 770 FoldingSetNodeID ID; 771 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 772 ID.AddInteger(FI); 773 void *IP = 0; 774 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 775 return SDOperand(E, 0); 776 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget); 777 CSEMap.InsertNode(N, IP); 778 AllNodes.push_back(N); 779 return SDOperand(N, 0); 780} 781 782SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){ 783 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; 784 FoldingSetNodeID ID; 785 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 786 ID.AddInteger(JTI); 787 void *IP = 0; 788 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 789 return SDOperand(E, 0); 790 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget); 791 CSEMap.InsertNode(N, IP); 792 AllNodes.push_back(N); 793 return SDOperand(N, 0); 794} 795 796SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT, 797 unsigned Alignment, int Offset, 798 bool isTarget) { 799 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 800 FoldingSetNodeID ID; 801 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 802 ID.AddInteger(Alignment); 803 ID.AddInteger(Offset); 804 ID.AddPointer(C); 805 void *IP = 0; 806 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 807 return SDOperand(E, 0); 808 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment); 809 CSEMap.InsertNode(N, IP); 810 AllNodes.push_back(N); 811 return SDOperand(N, 0); 812} 813 814 815SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, 816 MVT::ValueType VT, 817 unsigned Alignment, int Offset, 818 bool isTarget) { 819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 820 FoldingSetNodeID ID; 821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 822 ID.AddInteger(Alignment); 823 ID.AddInteger(Offset); 824 C->AddSelectionDAGCSEId(ID); 825 void *IP = 0; 826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 827 return SDOperand(E, 0); 828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment); 829 CSEMap.InsertNode(N, IP); 830 AllNodes.push_back(N); 831 return SDOperand(N, 0); 832} 833 834 835SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 836 FoldingSetNodeID ID; 837 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0); 838 ID.AddPointer(MBB); 839 void *IP = 0; 840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 841 return SDOperand(E, 0); 842 SDNode *N = new BasicBlockSDNode(MBB); 843 CSEMap.InsertNode(N, IP); 844 AllNodes.push_back(N); 845 return SDOperand(N, 0); 846} 847 848SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { 849 if ((unsigned)VT >= ValueTypeNodes.size()) 850 ValueTypeNodes.resize(VT+1); 851 if (ValueTypeNodes[VT] == 0) { 852 ValueTypeNodes[VT] = new VTSDNode(VT); 853 AllNodes.push_back(ValueTypeNodes[VT]); 854 } 855 856 return SDOperand(ValueTypeNodes[VT], 0); 857} 858 859SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { 860 SDNode *&N = ExternalSymbols[Sym]; 861 if (N) return SDOperand(N, 0); 862 N = new ExternalSymbolSDNode(false, Sym, VT); 863 AllNodes.push_back(N); 864 return SDOperand(N, 0); 865} 866 867SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, 868 MVT::ValueType VT) { 869 SDNode *&N = TargetExternalSymbols[Sym]; 870 if (N) return SDOperand(N, 0); 871 N = new ExternalSymbolSDNode(true, Sym, VT); 872 AllNodes.push_back(N); 873 return SDOperand(N, 0); 874} 875 876SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { 877 if ((unsigned)Cond >= CondCodeNodes.size()) 878 CondCodeNodes.resize(Cond+1); 879 880 if (CondCodeNodes[Cond] == 0) { 881 CondCodeNodes[Cond] = new CondCodeSDNode(Cond); 882 AllNodes.push_back(CondCodeNodes[Cond]); 883 } 884 return SDOperand(CondCodeNodes[Cond], 0); 885} 886 887SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { 888 FoldingSetNodeID ID; 889 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0); 890 ID.AddInteger(RegNo); 891 void *IP = 0; 892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 893 return SDOperand(E, 0); 894 SDNode *N = new RegisterSDNode(RegNo, VT); 895 CSEMap.InsertNode(N, IP); 896 AllNodes.push_back(N); 897 return SDOperand(N, 0); 898} 899 900SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { 901 assert((!V || isa<PointerType>(V->getType())) && 902 "SrcValue is not a pointer?"); 903 904 FoldingSetNodeID ID; 905 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0); 906 ID.AddPointer(V); 907 ID.AddInteger(Offset); 908 void *IP = 0; 909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 910 return SDOperand(E, 0); 911 SDNode *N = new SrcValueSDNode(V, Offset); 912 CSEMap.InsertNode(N, IP); 913 AllNodes.push_back(N); 914 return SDOperand(N, 0); 915} 916 917/// CreateStackTemporary - Create a stack temporary, suitable for holding the 918/// specified value type. 919SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) { 920 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 921 unsigned ByteSize = MVT::getSizeInBits(VT)/8; 922 const Type *Ty = MVT::getTypeForValueType(VT); 923 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty); 924 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign); 925 return getFrameIndex(FrameIdx, TLI.getPointerTy()); 926} 927 928 929SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1, 930 SDOperand N2, ISD::CondCode Cond) { 931 // These setcc operations always fold. 932 switch (Cond) { 933 default: break; 934 case ISD::SETFALSE: 935 case ISD::SETFALSE2: return getConstant(0, VT); 936 case ISD::SETTRUE: 937 case ISD::SETTRUE2: return getConstant(1, VT); 938 939 case ISD::SETOEQ: 940 case ISD::SETOGT: 941 case ISD::SETOGE: 942 case ISD::SETOLT: 943 case ISD::SETOLE: 944 case ISD::SETONE: 945 case ISD::SETO: 946 case ISD::SETUO: 947 case ISD::SETUEQ: 948 case ISD::SETUNE: 949 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!"); 950 break; 951 } 952 953 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { 954 uint64_t C2 = N2C->getValue(); 955 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { 956 uint64_t C1 = N1C->getValue(); 957 958 // Sign extend the operands if required 959 if (ISD::isSignedIntSetCC(Cond)) { 960 C1 = N1C->getSignExtended(); 961 C2 = N2C->getSignExtended(); 962 } 963 964 switch (Cond) { 965 default: assert(0 && "Unknown integer setcc!"); 966 case ISD::SETEQ: return getConstant(C1 == C2, VT); 967 case ISD::SETNE: return getConstant(C1 != C2, VT); 968 case ISD::SETULT: return getConstant(C1 < C2, VT); 969 case ISD::SETUGT: return getConstant(C1 > C2, VT); 970 case ISD::SETULE: return getConstant(C1 <= C2, VT); 971 case ISD::SETUGE: return getConstant(C1 >= C2, VT); 972 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); 973 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); 974 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); 975 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); 976 } 977 } 978 } 979 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) 980 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { 981 // No compile time operations on this type yet. 982 if (N1C->getValueType(0) == MVT::ppcf128) 983 return SDOperand(); 984 985 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF()); 986 switch (Cond) { 987 default: break; 988 case ISD::SETEQ: if (R==APFloat::cmpUnordered) 989 return getNode(ISD::UNDEF, VT); 990 // fall through 991 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT); 992 case ISD::SETNE: if (R==APFloat::cmpUnordered) 993 return getNode(ISD::UNDEF, VT); 994 // fall through 995 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan || 996 R==APFloat::cmpLessThan, VT); 997 case ISD::SETLT: if (R==APFloat::cmpUnordered) 998 return getNode(ISD::UNDEF, VT); 999 // fall through 1000 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT); 1001 case ISD::SETGT: if (R==APFloat::cmpUnordered) 1002 return getNode(ISD::UNDEF, VT); 1003 // fall through 1004 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT); 1005 case ISD::SETLE: if (R==APFloat::cmpUnordered) 1006 return getNode(ISD::UNDEF, VT); 1007 // fall through 1008 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan || 1009 R==APFloat::cmpEqual, VT); 1010 case ISD::SETGE: if (R==APFloat::cmpUnordered) 1011 return getNode(ISD::UNDEF, VT); 1012 // fall through 1013 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan || 1014 R==APFloat::cmpEqual, VT); 1015 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT); 1016 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT); 1017 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered || 1018 R==APFloat::cmpEqual, VT); 1019 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT); 1020 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered || 1021 R==APFloat::cmpLessThan, VT); 1022 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan || 1023 R==APFloat::cmpUnordered, VT); 1024 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT); 1025 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT); 1026 } 1027 } else { 1028 // Ensure that the constant occurs on the RHS. 1029 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 1030 } 1031 1032 // Could not fold it. 1033 return SDOperand(); 1034} 1035 1036/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 1037/// this predicate to simplify operations downstream. Mask is known to be zero 1038/// for bits that V cannot have. 1039bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask, 1040 unsigned Depth) const { 1041 // The masks are not wide enough to represent this type! Should use APInt. 1042 if (Op.getValueType() == MVT::i128) 1043 return false; 1044 1045 uint64_t KnownZero, KnownOne; 1046 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 1047 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1048 return (KnownZero & Mask) == Mask; 1049} 1050 1051/// ComputeMaskedBits - Determine which of the bits specified in Mask are 1052/// known to be either zero or one and return them in the KnownZero/KnownOne 1053/// bitsets. This code only analyzes bits in Mask, in order to short-circuit 1054/// processing. 1055void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask, 1056 uint64_t &KnownZero, uint64_t &KnownOne, 1057 unsigned Depth) const { 1058 KnownZero = KnownOne = 0; // Don't know anything. 1059 if (Depth == 6 || Mask == 0) 1060 return; // Limit search depth. 1061 1062 // The masks are not wide enough to represent this type! Should use APInt. 1063 if (Op.getValueType() == MVT::i128) 1064 return; 1065 1066 uint64_t KnownZero2, KnownOne2; 1067 1068 switch (Op.getOpcode()) { 1069 case ISD::Constant: 1070 // We know all of the bits for a constant! 1071 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask; 1072 KnownZero = ~KnownOne & Mask; 1073 return; 1074 case ISD::AND: 1075 // If either the LHS or the RHS are Zero, the result is zero. 1076 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1077 Mask &= ~KnownZero; 1078 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1079 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1080 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1081 1082 // Output known-1 bits are only known if set in both the LHS & RHS. 1083 KnownOne &= KnownOne2; 1084 // Output known-0 are known to be clear if zero in either the LHS | RHS. 1085 KnownZero |= KnownZero2; 1086 return; 1087 case ISD::OR: 1088 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1089 Mask &= ~KnownOne; 1090 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1091 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1092 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1093 1094 // Output known-0 bits are only known if clear in both the LHS & RHS. 1095 KnownZero &= KnownZero2; 1096 // Output known-1 are known to be set if set in either the LHS | RHS. 1097 KnownOne |= KnownOne2; 1098 return; 1099 case ISD::XOR: { 1100 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1101 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1102 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1103 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1104 1105 // Output known-0 bits are known if clear or set in both the LHS & RHS. 1106 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 1107 // Output known-1 are known to be set if set in only one of the LHS, RHS. 1108 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 1109 KnownZero = KnownZeroOut; 1110 return; 1111 } 1112 case ISD::SELECT: 1113 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1); 1114 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); 1115 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1116 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1117 1118 // Only known if known in both the LHS and RHS. 1119 KnownOne &= KnownOne2; 1120 KnownZero &= KnownZero2; 1121 return; 1122 case ISD::SELECT_CC: 1123 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1); 1124 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1); 1125 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1126 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1127 1128 // Only known if known in both the LHS and RHS. 1129 KnownOne &= KnownOne2; 1130 KnownZero &= KnownZero2; 1131 return; 1132 case ISD::SETCC: 1133 // If we know the result of a setcc has the top bits zero, use this info. 1134 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) 1135 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); 1136 return; 1137 case ISD::SHL: 1138 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 1139 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1140 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(), 1141 KnownZero, KnownOne, Depth+1); 1142 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1143 KnownZero <<= SA->getValue(); 1144 KnownOne <<= SA->getValue(); 1145 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero. 1146 } 1147 return; 1148 case ISD::SRL: 1149 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 1150 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1151 MVT::ValueType VT = Op.getValueType(); 1152 unsigned ShAmt = SA->getValue(); 1153 1154 uint64_t TypeMask = MVT::getIntVTBitMask(VT); 1155 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask, 1156 KnownZero, KnownOne, Depth+1); 1157 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1158 KnownZero &= TypeMask; 1159 KnownOne &= TypeMask; 1160 KnownZero >>= ShAmt; 1161 KnownOne >>= ShAmt; 1162 1163 uint64_t HighBits = (1ULL << ShAmt)-1; 1164 HighBits <<= MVT::getSizeInBits(VT)-ShAmt; 1165 KnownZero |= HighBits; // High bits known zero. 1166 } 1167 return; 1168 case ISD::SRA: 1169 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1170 MVT::ValueType VT = Op.getValueType(); 1171 unsigned ShAmt = SA->getValue(); 1172 1173 // Compute the new bits that are at the top now. 1174 uint64_t TypeMask = MVT::getIntVTBitMask(VT); 1175 1176 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask; 1177 // If any of the demanded bits are produced by the sign extension, we also 1178 // demand the input sign bit. 1179 uint64_t HighBits = (1ULL << ShAmt)-1; 1180 HighBits <<= MVT::getSizeInBits(VT) - ShAmt; 1181 if (HighBits & Mask) 1182 InDemandedMask |= MVT::getIntVTSignBit(VT); 1183 1184 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne, 1185 Depth+1); 1186 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1187 KnownZero &= TypeMask; 1188 KnownOne &= TypeMask; 1189 KnownZero >>= ShAmt; 1190 KnownOne >>= ShAmt; 1191 1192 // Handle the sign bits. 1193 uint64_t SignBit = MVT::getIntVTSignBit(VT); 1194 SignBit >>= ShAmt; // Adjust to where it is now in the mask. 1195 1196 if (KnownZero & SignBit) { 1197 KnownZero |= HighBits; // New bits are known zero. 1198 } else if (KnownOne & SignBit) { 1199 KnownOne |= HighBits; // New bits are known one. 1200 } 1201 } 1202 return; 1203 case ISD::SIGN_EXTEND_INREG: { 1204 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1205 1206 // Sign extension. Compute the demanded bits in the result that are not 1207 // present in the input. 1208 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask; 1209 1210 uint64_t InSignBit = MVT::getIntVTSignBit(EVT); 1211 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT); 1212 1213 // If the sign extended bits are demanded, we know that the sign 1214 // bit is demanded. 1215 if (NewBits) 1216 InputDemandedBits |= InSignBit; 1217 1218 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits, 1219 KnownZero, KnownOne, Depth+1); 1220 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1221 1222 // If the sign bit of the input is known set or clear, then we know the 1223 // top bits of the result. 1224 if (KnownZero & InSignBit) { // Input sign bit known clear 1225 KnownZero |= NewBits; 1226 KnownOne &= ~NewBits; 1227 } else if (KnownOne & InSignBit) { // Input sign bit known set 1228 KnownOne |= NewBits; 1229 KnownZero &= ~NewBits; 1230 } else { // Input sign bit unknown 1231 KnownZero &= ~NewBits; 1232 KnownOne &= ~NewBits; 1233 } 1234 return; 1235 } 1236 case ISD::CTTZ: 1237 case ISD::CTLZ: 1238 case ISD::CTPOP: { 1239 MVT::ValueType VT = Op.getValueType(); 1240 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1; 1241 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT); 1242 KnownOne = 0; 1243 return; 1244 } 1245 case ISD::LOAD: { 1246 if (ISD::isZEXTLoad(Op.Val)) { 1247 LoadSDNode *LD = cast<LoadSDNode>(Op); 1248 MVT::ValueType VT = LD->getLoadedVT(); 1249 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask; 1250 } 1251 return; 1252 } 1253 case ISD::ZERO_EXTEND: { 1254 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType()); 1255 uint64_t NewBits = (~InMask) & Mask; 1256 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 1257 KnownOne, Depth+1); 1258 KnownZero |= NewBits & Mask; 1259 KnownOne &= ~NewBits; 1260 return; 1261 } 1262 case ISD::SIGN_EXTEND: { 1263 MVT::ValueType InVT = Op.getOperand(0).getValueType(); 1264 unsigned InBits = MVT::getSizeInBits(InVT); 1265 uint64_t InMask = MVT::getIntVTBitMask(InVT); 1266 uint64_t InSignBit = 1ULL << (InBits-1); 1267 uint64_t NewBits = (~InMask) & Mask; 1268 uint64_t InDemandedBits = Mask & InMask; 1269 1270 // If any of the sign extended bits are demanded, we know that the sign 1271 // bit is demanded. 1272 if (NewBits & Mask) 1273 InDemandedBits |= InSignBit; 1274 1275 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero, 1276 KnownOne, Depth+1); 1277 // If the sign bit is known zero or one, the top bits match. 1278 if (KnownZero & InSignBit) { 1279 KnownZero |= NewBits; 1280 KnownOne &= ~NewBits; 1281 } else if (KnownOne & InSignBit) { 1282 KnownOne |= NewBits; 1283 KnownZero &= ~NewBits; 1284 } else { // Otherwise, top bits aren't known. 1285 KnownOne &= ~NewBits; 1286 KnownZero &= ~NewBits; 1287 } 1288 return; 1289 } 1290 case ISD::ANY_EXTEND: { 1291 MVT::ValueType VT = Op.getOperand(0).getValueType(); 1292 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT), 1293 KnownZero, KnownOne, Depth+1); 1294 return; 1295 } 1296 case ISD::TRUNCATE: { 1297 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 1298 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1299 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType()); 1300 KnownZero &= OutMask; 1301 KnownOne &= OutMask; 1302 break; 1303 } 1304 case ISD::AssertZext: { 1305 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1306 uint64_t InMask = MVT::getIntVTBitMask(VT); 1307 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 1308 KnownOne, Depth+1); 1309 KnownZero |= (~InMask) & Mask; 1310 return; 1311 } 1312 case ISD::ADD: { 1313 // If either the LHS or the RHS are Zero, the result is zero. 1314 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1315 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1316 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1317 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1318 1319 // Output known-0 bits are known if clear or set in both the low clear bits 1320 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the 1321 // low 3 bits clear. 1322 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero), 1323 CountTrailingZeros_64(~KnownZero2)); 1324 1325 KnownZero = (1ULL << KnownZeroOut) - 1; 1326 KnownOne = 0; 1327 return; 1328 } 1329 case ISD::SUB: { 1330 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)); 1331 if (!CLHS) return; 1332 1333 // We know that the top bits of C-X are clear if X contains less bits 1334 // than C (i.e. no wrap-around can happen). For example, 20-X is 1335 // positive if we can prove that X is >= 0 and < 16. 1336 MVT::ValueType VT = CLHS->getValueType(0); 1337 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear 1338 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1); 1339 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit 1340 MaskV = ~MaskV & MVT::getIntVTBitMask(VT); 1341 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1); 1342 1343 // If all of the MaskV bits are known to be zero, then we know the output 1344 // top bits are zero, because we now know that the output is from [0-C]. 1345 if ((KnownZero & MaskV) == MaskV) { 1346 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue()); 1347 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero. 1348 KnownOne = 0; // No one bits known. 1349 } else { 1350 KnownZero = KnownOne = 0; // Otherwise, nothing known. 1351 } 1352 } 1353 return; 1354 } 1355 default: 1356 // Allow the target to implement this method for its nodes. 1357 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) { 1358 case ISD::INTRINSIC_WO_CHAIN: 1359 case ISD::INTRINSIC_W_CHAIN: 1360 case ISD::INTRINSIC_VOID: 1361 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this); 1362 } 1363 return; 1364 } 1365} 1366 1367/// ComputeNumSignBits - Return the number of times the sign bit of the 1368/// register is replicated into the other bits. We know that at least 1 bit 1369/// is always equal to the sign bit (itself), but other cases can give us 1370/// information. For example, immediately after an "SRA X, 2", we know that 1371/// the top 3 bits are all equal to each other, so we return 3. 1372unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{ 1373 MVT::ValueType VT = Op.getValueType(); 1374 assert(MVT::isInteger(VT) && "Invalid VT!"); 1375 unsigned VTBits = MVT::getSizeInBits(VT); 1376 unsigned Tmp, Tmp2; 1377 1378 if (Depth == 6) 1379 return 1; // Limit search depth. 1380 1381 switch (Op.getOpcode()) { 1382 default: break; 1383 case ISD::AssertSext: 1384 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); 1385 return VTBits-Tmp+1; 1386 case ISD::AssertZext: 1387 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); 1388 return VTBits-Tmp; 1389 1390 case ISD::Constant: { 1391 uint64_t Val = cast<ConstantSDNode>(Op)->getValue(); 1392 // If negative, invert the bits, then look at it. 1393 if (Val & MVT::getIntVTSignBit(VT)) 1394 Val = ~Val; 1395 1396 // Shift the bits so they are the leading bits in the int64_t. 1397 Val <<= 64-VTBits; 1398 1399 // Return # leading zeros. We use 'min' here in case Val was zero before 1400 // shifting. We don't want to return '64' as for an i32 "0". 1401 return std::min(VTBits, CountLeadingZeros_64(Val)); 1402 } 1403 1404 case ISD::SIGN_EXTEND: 1405 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType()); 1406 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp; 1407 1408 case ISD::SIGN_EXTEND_INREG: 1409 // Max of the input and what this extends. 1410 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); 1411 Tmp = VTBits-Tmp+1; 1412 1413 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1414 return std::max(Tmp, Tmp2); 1415 1416 case ISD::SRA: 1417 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1418 // SRA X, C -> adds C sign bits. 1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1420 Tmp += C->getValue(); 1421 if (Tmp > VTBits) Tmp = VTBits; 1422 } 1423 return Tmp; 1424 case ISD::SHL: 1425 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1426 // shl destroys sign bits. 1427 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1428 if (C->getValue() >= VTBits || // Bad shift. 1429 C->getValue() >= Tmp) break; // Shifted all sign bits out. 1430 return Tmp - C->getValue(); 1431 } 1432 break; 1433 case ISD::AND: 1434 case ISD::OR: 1435 case ISD::XOR: // NOT is handled here. 1436 // Logical binary ops preserve the number of sign bits. 1437 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1438 if (Tmp == 1) return 1; // Early out. 1439 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1440 return std::min(Tmp, Tmp2); 1441 1442 case ISD::SELECT: 1443 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1444 if (Tmp == 1) return 1; // Early out. 1445 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1446 return std::min(Tmp, Tmp2); 1447 1448 case ISD::SETCC: 1449 // If setcc returns 0/-1, all bits are sign bits. 1450 if (TLI.getSetCCResultContents() == 1451 TargetLowering::ZeroOrNegativeOneSetCCResult) 1452 return VTBits; 1453 break; 1454 case ISD::ROTL: 1455 case ISD::ROTR: 1456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1457 unsigned RotAmt = C->getValue() & (VTBits-1); 1458 1459 // Handle rotate right by N like a rotate left by 32-N. 1460 if (Op.getOpcode() == ISD::ROTR) 1461 RotAmt = (VTBits-RotAmt) & (VTBits-1); 1462 1463 // If we aren't rotating out all of the known-in sign bits, return the 1464 // number that are left. This handles rotl(sext(x), 1) for example. 1465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1466 if (Tmp > RotAmt+1) return Tmp-RotAmt; 1467 } 1468 break; 1469 case ISD::ADD: 1470 // Add can have at most one carry bit. Thus we know that the output 1471 // is, at worst, one more bit than the inputs. 1472 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1473 if (Tmp == 1) return 1; // Early out. 1474 1475 // Special case decrementing a value (ADD X, -1): 1476 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 1477 if (CRHS->isAllOnesValue()) { 1478 uint64_t KnownZero, KnownOne; 1479 uint64_t Mask = MVT::getIntVTBitMask(VT); 1480 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 1481 1482 // If the input is known to be 0 or 1, the output is 0/-1, which is all 1483 // sign bits set. 1484 if ((KnownZero|1) == Mask) 1485 return VTBits; 1486 1487 // If we are subtracting one from a positive number, there is no carry 1488 // out of the result. 1489 if (KnownZero & MVT::getIntVTSignBit(VT)) 1490 return Tmp; 1491 } 1492 1493 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1494 if (Tmp2 == 1) return 1; 1495 return std::min(Tmp, Tmp2)-1; 1496 break; 1497 1498 case ISD::SUB: 1499 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1500 if (Tmp2 == 1) return 1; 1501 1502 // Handle NEG. 1503 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 1504 if (CLHS->getValue() == 0) { 1505 uint64_t KnownZero, KnownOne; 1506 uint64_t Mask = MVT::getIntVTBitMask(VT); 1507 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1508 // If the input is known to be 0 or 1, the output is 0/-1, which is all 1509 // sign bits set. 1510 if ((KnownZero|1) == Mask) 1511 return VTBits; 1512 1513 // If the input is known to be positive (the sign bit is known clear), 1514 // the output of the NEG has the same number of sign bits as the input. 1515 if (KnownZero & MVT::getIntVTSignBit(VT)) 1516 return Tmp2; 1517 1518 // Otherwise, we treat this like a SUB. 1519 } 1520 1521 // Sub can have at most one carry bit. Thus we know that the output 1522 // is, at worst, one more bit than the inputs. 1523 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1524 if (Tmp == 1) return 1; // Early out. 1525 return std::min(Tmp, Tmp2)-1; 1526 break; 1527 case ISD::TRUNCATE: 1528 // FIXME: it's tricky to do anything useful for this, but it is an important 1529 // case for targets like X86. 1530 break; 1531 } 1532 1533 // Handle LOADX separately here. EXTLOAD case will fallthrough. 1534 if (Op.getOpcode() == ISD::LOAD) { 1535 LoadSDNode *LD = cast<LoadSDNode>(Op); 1536 unsigned ExtType = LD->getExtensionType(); 1537 switch (ExtType) { 1538 default: break; 1539 case ISD::SEXTLOAD: // '17' bits known 1540 Tmp = MVT::getSizeInBits(LD->getLoadedVT()); 1541 return VTBits-Tmp+1; 1542 case ISD::ZEXTLOAD: // '16' bits known 1543 Tmp = MVT::getSizeInBits(LD->getLoadedVT()); 1544 return VTBits-Tmp; 1545 } 1546 } 1547 1548 // Allow the target to implement this method for its nodes. 1549 if (Op.getOpcode() >= ISD::BUILTIN_OP_END || 1550 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 1551 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 1552 Op.getOpcode() == ISD::INTRINSIC_VOID) { 1553 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth); 1554 if (NumBits > 1) return NumBits; 1555 } 1556 1557 // Finally, if we can prove that the top bits of the result are 0's or 1's, 1558 // use this information. 1559 uint64_t KnownZero, KnownOne; 1560 uint64_t Mask = MVT::getIntVTBitMask(VT); 1561 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 1562 1563 uint64_t SignBit = MVT::getIntVTSignBit(VT); 1564 if (KnownZero & SignBit) { // SignBit is 0 1565 Mask = KnownZero; 1566 } else if (KnownOne & SignBit) { // SignBit is 1; 1567 Mask = KnownOne; 1568 } else { 1569 // Nothing known. 1570 return 1; 1571 } 1572 1573 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine 1574 // the number of identical bits in the top of the input value. 1575 Mask ^= ~0ULL; 1576 Mask <<= 64-VTBits; 1577 // Return # leading zeros. We use 'min' here in case Val was zero before 1578 // shifting. We don't want to return '64' as for an i32 "0". 1579 return std::min(VTBits, CountLeadingZeros_64(Mask)); 1580} 1581 1582 1583/// getNode - Gets or creates the specified node. 1584/// 1585SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { 1586 FoldingSetNodeID ID; 1587 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0); 1588 void *IP = 0; 1589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1590 return SDOperand(E, 0); 1591 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT)); 1592 CSEMap.InsertNode(N, IP); 1593 1594 AllNodes.push_back(N); 1595 return SDOperand(N, 0); 1596} 1597 1598SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1599 SDOperand Operand) { 1600 unsigned Tmp1; 1601 // Constant fold unary operations with an integer constant operand. 1602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { 1603 uint64_t Val = C->getValue(); 1604 switch (Opcode) { 1605 default: break; 1606 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); 1607 case ISD::ANY_EXTEND: 1608 case ISD::ZERO_EXTEND: return getConstant(Val, VT); 1609 case ISD::TRUNCATE: return getConstant(Val, VT); 1610 case ISD::UINT_TO_FP: 1611 case ISD::SINT_TO_FP: { 1612 const uint64_t zero[] = {0, 0}; 1613 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero)); 1614 (void)apf.convertFromZeroExtendedInteger(&Val, 1615 MVT::getSizeInBits(Operand.getValueType()), 1616 Opcode==ISD::SINT_TO_FP, 1617 APFloat::rmNearestTiesToEven); 1618 return getConstantFP(apf, VT); 1619 } 1620 case ISD::BIT_CONVERT: 1621 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) 1622 return getConstantFP(BitsToFloat(Val), VT); 1623 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) 1624 return getConstantFP(BitsToDouble(Val), VT); 1625 break; 1626 case ISD::BSWAP: 1627 switch(VT) { 1628 default: assert(0 && "Invalid bswap!"); break; 1629 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT); 1630 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT); 1631 case MVT::i64: return getConstant(ByteSwap_64(Val), VT); 1632 } 1633 break; 1634 case ISD::CTPOP: 1635 switch(VT) { 1636 default: assert(0 && "Invalid ctpop!"); break; 1637 case MVT::i1: return getConstant(Val != 0, VT); 1638 case MVT::i8: 1639 Tmp1 = (unsigned)Val & 0xFF; 1640 return getConstant(CountPopulation_32(Tmp1), VT); 1641 case MVT::i16: 1642 Tmp1 = (unsigned)Val & 0xFFFF; 1643 return getConstant(CountPopulation_32(Tmp1), VT); 1644 case MVT::i32: 1645 return getConstant(CountPopulation_32((unsigned)Val), VT); 1646 case MVT::i64: 1647 return getConstant(CountPopulation_64(Val), VT); 1648 } 1649 case ISD::CTLZ: 1650 switch(VT) { 1651 default: assert(0 && "Invalid ctlz!"); break; 1652 case MVT::i1: return getConstant(Val == 0, VT); 1653 case MVT::i8: 1654 Tmp1 = (unsigned)Val & 0xFF; 1655 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT); 1656 case MVT::i16: 1657 Tmp1 = (unsigned)Val & 0xFFFF; 1658 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT); 1659 case MVT::i32: 1660 return getConstant(CountLeadingZeros_32((unsigned)Val), VT); 1661 case MVT::i64: 1662 return getConstant(CountLeadingZeros_64(Val), VT); 1663 } 1664 case ISD::CTTZ: 1665 switch(VT) { 1666 default: assert(0 && "Invalid cttz!"); break; 1667 case MVT::i1: return getConstant(Val == 0, VT); 1668 case MVT::i8: 1669 Tmp1 = (unsigned)Val | 0x100; 1670 return getConstant(CountTrailingZeros_32(Tmp1), VT); 1671 case MVT::i16: 1672 Tmp1 = (unsigned)Val | 0x10000; 1673 return getConstant(CountTrailingZeros_32(Tmp1), VT); 1674 case MVT::i32: 1675 return getConstant(CountTrailingZeros_32((unsigned)Val), VT); 1676 case MVT::i64: 1677 return getConstant(CountTrailingZeros_64(Val), VT); 1678 } 1679 } 1680 } 1681 1682 // Constant fold unary operations with a floating point constant operand. 1683 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) { 1684 APFloat V = C->getValueAPF(); // make copy 1685 switch (Opcode) { 1686 case ISD::FNEG: 1687 V.changeSign(); 1688 return getConstantFP(V, VT); 1689 case ISD::FABS: 1690 V.clearSign(); 1691 return getConstantFP(V, VT); 1692 case ISD::FP_ROUND: 1693 case ISD::FP_EXTEND: 1694 // This can return overflow, underflow, or inexact; we don't care. 1695 // FIXME need to be more flexible about rounding mode. 1696 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle : 1697 VT==MVT::f64 ? APFloat::IEEEdouble : 1698 VT==MVT::f80 ? APFloat::x87DoubleExtended : 1699 VT==MVT::f128 ? APFloat::IEEEquad : 1700 APFloat::Bogus, 1701 APFloat::rmNearestTiesToEven); 1702 return getConstantFP(V, VT); 1703 case ISD::FP_TO_SINT: 1704 case ISD::FP_TO_UINT: { 1705 integerPart x; 1706 assert(integerPartWidth >= 64); 1707 // FIXME need to be more flexible about rounding mode. 1708 APFloat::opStatus s = V.convertToInteger(&x, 64U, 1709 Opcode==ISD::FP_TO_SINT, 1710 APFloat::rmTowardZero); 1711 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual 1712 break; 1713 return getConstant(x, VT); 1714 } 1715 case ISD::BIT_CONVERT: 1716 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) 1717 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT); 1718 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) 1719 return getConstant(V.convertToAPInt().getZExtValue(), VT); 1720 break; 1721 } 1722 } 1723 1724 unsigned OpOpcode = Operand.Val->getOpcode(); 1725 switch (Opcode) { 1726 case ISD::TokenFactor: 1727 return Operand; // Factor of one node? No factor. 1728 case ISD::FP_ROUND: 1729 case ISD::FP_EXTEND: 1730 assert(MVT::isFloatingPoint(VT) && 1731 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!"); 1732 break; 1733 case ISD::SIGN_EXTEND: 1734 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1735 "Invalid SIGN_EXTEND!"); 1736 if (Operand.getValueType() == VT) return Operand; // noop extension 1737 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!"); 1738 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 1739 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1740 break; 1741 case ISD::ZERO_EXTEND: 1742 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1743 "Invalid ZERO_EXTEND!"); 1744 if (Operand.getValueType() == VT) return Operand; // noop extension 1745 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!"); 1746 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 1747 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); 1748 break; 1749 case ISD::ANY_EXTEND: 1750 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1751 "Invalid ANY_EXTEND!"); 1752 if (Operand.getValueType() == VT) return Operand; // noop extension 1753 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!"); 1754 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) 1755 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 1756 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1757 break; 1758 case ISD::TRUNCATE: 1759 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1760 "Invalid TRUNCATE!"); 1761 if (Operand.getValueType() == VT) return Operand; // noop truncate 1762 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!"); 1763 if (OpOpcode == ISD::TRUNCATE) 1764 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1765 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 1766 OpOpcode == ISD::ANY_EXTEND) { 1767 // If the source is smaller than the dest, we still need an extend. 1768 if (Operand.Val->getOperand(0).getValueType() < VT) 1769 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1770 else if (Operand.Val->getOperand(0).getValueType() > VT) 1771 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1772 else 1773 return Operand.Val->getOperand(0); 1774 } 1775 break; 1776 case ISD::BIT_CONVERT: 1777 // Basic sanity checking. 1778 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()) 1779 && "Cannot BIT_CONVERT between types of different sizes!"); 1780 if (VT == Operand.getValueType()) return Operand; // noop conversion. 1781 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) 1782 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0)); 1783 if (OpOpcode == ISD::UNDEF) 1784 return getNode(ISD::UNDEF, VT); 1785 break; 1786 case ISD::SCALAR_TO_VECTOR: 1787 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) && 1788 MVT::getVectorElementType(VT) == Operand.getValueType() && 1789 "Illegal SCALAR_TO_VECTOR node!"); 1790 break; 1791 case ISD::FNEG: 1792 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) 1793 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), 1794 Operand.Val->getOperand(0)); 1795 if (OpOpcode == ISD::FNEG) // --X -> X 1796 return Operand.Val->getOperand(0); 1797 break; 1798 case ISD::FABS: 1799 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 1800 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); 1801 break; 1802 } 1803 1804 SDNode *N; 1805 SDVTList VTs = getVTList(VT); 1806 if (VT != MVT::Flag) { // Don't CSE flag producing nodes 1807 FoldingSetNodeID ID; 1808 SDOperand Ops[1] = { Operand }; 1809 AddNodeIDNode(ID, Opcode, VTs, Ops, 1); 1810 void *IP = 0; 1811 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1812 return SDOperand(E, 0); 1813 N = new UnarySDNode(Opcode, VTs, Operand); 1814 CSEMap.InsertNode(N, IP); 1815 } else { 1816 N = new UnarySDNode(Opcode, VTs, Operand); 1817 } 1818 AllNodes.push_back(N); 1819 return SDOperand(N, 0); 1820} 1821 1822 1823 1824SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1825 SDOperand N1, SDOperand N2) { 1826#ifndef NDEBUG 1827 switch (Opcode) { 1828 case ISD::TokenFactor: 1829 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 1830 N2.getValueType() == MVT::Other && "Invalid token factor!"); 1831 break; 1832 case ISD::AND: 1833 case ISD::OR: 1834 case ISD::XOR: 1835 case ISD::UDIV: 1836 case ISD::UREM: 1837 case ISD::MULHU: 1838 case ISD::MULHS: 1839 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); 1840 // fall through 1841 case ISD::ADD: 1842 case ISD::SUB: 1843 case ISD::MUL: 1844 case ISD::SDIV: 1845 case ISD::SREM: 1846 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); 1847 // fall through. 1848 case ISD::FADD: 1849 case ISD::FSUB: 1850 case ISD::FMUL: 1851 case ISD::FDIV: 1852 case ISD::FREM: 1853 assert(N1.getValueType() == N2.getValueType() && 1854 N1.getValueType() == VT && "Binary operator types must match!"); 1855 break; 1856 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. 1857 assert(N1.getValueType() == VT && 1858 MVT::isFloatingPoint(N1.getValueType()) && 1859 MVT::isFloatingPoint(N2.getValueType()) && 1860 "Invalid FCOPYSIGN!"); 1861 break; 1862 case ISD::SHL: 1863 case ISD::SRA: 1864 case ISD::SRL: 1865 case ISD::ROTL: 1866 case ISD::ROTR: 1867 assert(VT == N1.getValueType() && 1868 "Shift operators return type must be the same as their first arg"); 1869 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && 1870 VT != MVT::i1 && "Shifts only work on integers"); 1871 break; 1872 case ISD::FP_ROUND_INREG: { 1873 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1874 assert(VT == N1.getValueType() && "Not an inreg round!"); 1875 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && 1876 "Cannot FP_ROUND_INREG integer types"); 1877 assert(EVT <= VT && "Not rounding down!"); 1878 break; 1879 } 1880 case ISD::AssertSext: 1881 case ISD::AssertZext: 1882 case ISD::SIGN_EXTEND_INREG: { 1883 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1884 assert(VT == N1.getValueType() && "Not an inreg extend!"); 1885 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && 1886 "Cannot *_EXTEND_INREG FP types"); 1887 assert(EVT <= VT && "Not extending!"); 1888 } 1889 1890 default: break; 1891 } 1892#endif 1893 1894 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1895 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1896 if (N1C) { 1897 if (Opcode == ISD::SIGN_EXTEND_INREG) { 1898 int64_t Val = N1C->getValue(); 1899 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT()); 1900 Val <<= 64-FromBits; 1901 Val >>= 64-FromBits; 1902 return getConstant(Val, VT); 1903 } 1904 1905 if (N2C) { 1906 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); 1907 switch (Opcode) { 1908 case ISD::ADD: return getConstant(C1 + C2, VT); 1909 case ISD::SUB: return getConstant(C1 - C2, VT); 1910 case ISD::MUL: return getConstant(C1 * C2, VT); 1911 case ISD::UDIV: 1912 if (C2) return getConstant(C1 / C2, VT); 1913 break; 1914 case ISD::UREM : 1915 if (C2) return getConstant(C1 % C2, VT); 1916 break; 1917 case ISD::SDIV : 1918 if (C2) return getConstant(N1C->getSignExtended() / 1919 N2C->getSignExtended(), VT); 1920 break; 1921 case ISD::SREM : 1922 if (C2) return getConstant(N1C->getSignExtended() % 1923 N2C->getSignExtended(), VT); 1924 break; 1925 case ISD::AND : return getConstant(C1 & C2, VT); 1926 case ISD::OR : return getConstant(C1 | C2, VT); 1927 case ISD::XOR : return getConstant(C1 ^ C2, VT); 1928 case ISD::SHL : return getConstant(C1 << C2, VT); 1929 case ISD::SRL : return getConstant(C1 >> C2, VT); 1930 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); 1931 case ISD::ROTL : 1932 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)), 1933 VT); 1934 case ISD::ROTR : 1935 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), 1936 VT); 1937 default: break; 1938 } 1939 } else { // Cannonicalize constant to RHS if commutative 1940 if (isCommutativeBinOp(Opcode)) { 1941 std::swap(N1C, N2C); 1942 std::swap(N1, N2); 1943 } 1944 } 1945 } 1946 1947 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); 1948 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); 1949 if (N1CFP) { 1950 if (N2CFP) { 1951 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF(); 1952 APFloat::opStatus s; 1953 switch (Opcode) { 1954 case ISD::FADD: 1955 s = V1.add(V2, APFloat::rmNearestTiesToEven); 1956 if (s!=APFloat::opInvalidOp) 1957 return getConstantFP(V1, VT); 1958 break; 1959 case ISD::FSUB: 1960 s = V1.subtract(V2, APFloat::rmNearestTiesToEven); 1961 if (s!=APFloat::opInvalidOp) 1962 return getConstantFP(V1, VT); 1963 break; 1964 case ISD::FMUL: 1965 s = V1.multiply(V2, APFloat::rmNearestTiesToEven); 1966 if (s!=APFloat::opInvalidOp) 1967 return getConstantFP(V1, VT); 1968 break; 1969 case ISD::FDIV: 1970 s = V1.divide(V2, APFloat::rmNearestTiesToEven); 1971 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 1972 return getConstantFP(V1, VT); 1973 break; 1974 case ISD::FREM : 1975 s = V1.mod(V2, APFloat::rmNearestTiesToEven); 1976 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 1977 return getConstantFP(V1, VT); 1978 break; 1979 case ISD::FCOPYSIGN: 1980 V1.copySign(V2); 1981 return getConstantFP(V1, VT); 1982 default: break; 1983 } 1984 } else { // Cannonicalize constant to RHS if commutative 1985 if (isCommutativeBinOp(Opcode)) { 1986 std::swap(N1CFP, N2CFP); 1987 std::swap(N1, N2); 1988 } 1989 } 1990 } 1991 1992 // Canonicalize an UNDEF to the RHS, even over a constant. 1993 if (N1.getOpcode() == ISD::UNDEF) { 1994 if (isCommutativeBinOp(Opcode)) { 1995 std::swap(N1, N2); 1996 } else { 1997 switch (Opcode) { 1998 case ISD::FP_ROUND_INREG: 1999 case ISD::SIGN_EXTEND_INREG: 2000 case ISD::SUB: 2001 case ISD::FSUB: 2002 case ISD::FDIV: 2003 case ISD::FREM: 2004 case ISD::SRA: 2005 return N1; // fold op(undef, arg2) -> undef 2006 case ISD::UDIV: 2007 case ISD::SDIV: 2008 case ISD::UREM: 2009 case ISD::SREM: 2010 case ISD::SRL: 2011 case ISD::SHL: 2012 if (!MVT::isVector(VT)) 2013 return getConstant(0, VT); // fold op(undef, arg2) -> 0 2014 // For vectors, we can't easily build an all zero vector, just return 2015 // the LHS. 2016 return N2; 2017 } 2018 } 2019 } 2020 2021 // Fold a bunch of operators when the RHS is undef. 2022 if (N2.getOpcode() == ISD::UNDEF) { 2023 switch (Opcode) { 2024 case ISD::ADD: 2025 case ISD::ADDC: 2026 case ISD::ADDE: 2027 case ISD::SUB: 2028 case ISD::FADD: 2029 case ISD::FSUB: 2030 case ISD::FMUL: 2031 case ISD::FDIV: 2032 case ISD::FREM: 2033 case ISD::UDIV: 2034 case ISD::SDIV: 2035 case ISD::UREM: 2036 case ISD::SREM: 2037 case ISD::XOR: 2038 return N2; // fold op(arg1, undef) -> undef 2039 case ISD::MUL: 2040 case ISD::AND: 2041 case ISD::SRL: 2042 case ISD::SHL: 2043 if (!MVT::isVector(VT)) 2044 return getConstant(0, VT); // fold op(arg1, undef) -> 0 2045 // For vectors, we can't easily build an all zero vector, just return 2046 // the LHS. 2047 return N1; 2048 case ISD::OR: 2049 if (!MVT::isVector(VT)) 2050 return getConstant(MVT::getIntVTBitMask(VT), VT); 2051 // For vectors, we can't easily build an all one vector, just return 2052 // the LHS. 2053 return N1; 2054 case ISD::SRA: 2055 return N1; 2056 } 2057 } 2058 2059 // Fold operations. 2060 switch (Opcode) { 2061 case ISD::TokenFactor: 2062 // Fold trivial token factors. 2063 if (N1.getOpcode() == ISD::EntryToken) return N2; 2064 if (N2.getOpcode() == ISD::EntryToken) return N1; 2065 break; 2066 2067 case ISD::AND: 2068 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's 2069 // worth handling here. 2070 if (N2C && N2C->getValue() == 0) 2071 return N2; 2072 break; 2073 case ISD::OR: 2074 case ISD::XOR: 2075 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's 2076 // worth handling here. 2077 if (N2C && N2C->getValue() == 0) 2078 return N1; 2079 break; 2080 case ISD::FP_ROUND_INREG: 2081 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 2082 break; 2083 case ISD::SIGN_EXTEND_INREG: { 2084 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 2085 if (EVT == VT) return N1; // Not actually extending 2086 break; 2087 } 2088 case ISD::EXTRACT_VECTOR_ELT: 2089 assert(N2C && "Bad EXTRACT_VECTOR_ELT!"); 2090 2091 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is 2092 // expanding copies of large vectors from registers. 2093 if (N1.getOpcode() == ISD::CONCAT_VECTORS && 2094 N1.getNumOperands() > 0) { 2095 unsigned Factor = 2096 MVT::getVectorNumElements(N1.getOperand(0).getValueType()); 2097 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, 2098 N1.getOperand(N2C->getValue() / Factor), 2099 getConstant(N2C->getValue() % Factor, N2.getValueType())); 2100 } 2101 2102 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is 2103 // expanding large vector constants. 2104 if (N1.getOpcode() == ISD::BUILD_VECTOR) 2105 return N1.getOperand(N2C->getValue()); 2106 2107 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector 2108 // operations are lowered to scalars. 2109 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) 2110 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) { 2111 if (IEC == N2C) 2112 return N1.getOperand(1); 2113 else 2114 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2); 2115 } 2116 break; 2117 case ISD::EXTRACT_ELEMENT: 2118 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!"); 2119 2120 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding 2121 // 64-bit integers into 32-bit parts. Instead of building the extract of 2122 // the BUILD_PAIR, only to have legalize rip it apart, just do it now. 2123 if (N1.getOpcode() == ISD::BUILD_PAIR) 2124 return N1.getOperand(N2C->getValue()); 2125 2126 // EXTRACT_ELEMENT of a constant int is also very common. 2127 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 2128 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue(); 2129 return getConstant(C->getValue() >> Shift, VT); 2130 } 2131 break; 2132 2133 // FIXME: figure out how to safely handle things like 2134 // int foo(int x) { return 1 << (x & 255); } 2135 // int bar() { return foo(256); } 2136#if 0 2137 case ISD::SHL: 2138 case ISD::SRL: 2139 case ISD::SRA: 2140 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && 2141 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) 2142 return getNode(Opcode, VT, N1, N2.getOperand(0)); 2143 else if (N2.getOpcode() == ISD::AND) 2144 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { 2145 // If the and is only masking out bits that cannot effect the shift, 2146 // eliminate the and. 2147 unsigned NumBits = MVT::getSizeInBits(VT); 2148 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 2149 return getNode(Opcode, VT, N1, N2.getOperand(0)); 2150 } 2151 break; 2152#endif 2153 } 2154 2155 // Memoize this node if possible. 2156 SDNode *N; 2157 SDVTList VTs = getVTList(VT); 2158 if (VT != MVT::Flag) { 2159 SDOperand Ops[] = { N1, N2 }; 2160 FoldingSetNodeID ID; 2161 AddNodeIDNode(ID, Opcode, VTs, Ops, 2); 2162 void *IP = 0; 2163 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2164 return SDOperand(E, 0); 2165 N = new BinarySDNode(Opcode, VTs, N1, N2); 2166 CSEMap.InsertNode(N, IP); 2167 } else { 2168 N = new BinarySDNode(Opcode, VTs, N1, N2); 2169 } 2170 2171 AllNodes.push_back(N); 2172 return SDOperand(N, 0); 2173} 2174 2175SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2176 SDOperand N1, SDOperand N2, SDOperand N3) { 2177 // Perform various simplifications. 2178 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 2179 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 2180 switch (Opcode) { 2181 case ISD::SETCC: { 2182 // Use FoldSetCC to simplify SETCC's. 2183 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); 2184 if (Simp.Val) return Simp; 2185 break; 2186 } 2187 case ISD::SELECT: 2188 if (N1C) 2189 if (N1C->getValue()) 2190 return N2; // select true, X, Y -> X 2191 else 2192 return N3; // select false, X, Y -> Y 2193 2194 if (N2 == N3) return N2; // select C, X, X -> X 2195 break; 2196 case ISD::BRCOND: 2197 if (N2C) 2198 if (N2C->getValue()) // Unconditional branch 2199 return getNode(ISD::BR, MVT::Other, N1, N3); 2200 else 2201 return N1; // Never-taken branch 2202 break; 2203 case ISD::VECTOR_SHUFFLE: 2204 assert(VT == N1.getValueType() && VT == N2.getValueType() && 2205 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) && 2206 N3.getOpcode() == ISD::BUILD_VECTOR && 2207 MVT::getVectorNumElements(VT) == N3.getNumOperands() && 2208 "Illegal VECTOR_SHUFFLE node!"); 2209 break; 2210 case ISD::BIT_CONVERT: 2211 // Fold bit_convert nodes from a type to themselves. 2212 if (N1.getValueType() == VT) 2213 return N1; 2214 break; 2215 } 2216 2217 // Memoize node if it doesn't produce a flag. 2218 SDNode *N; 2219 SDVTList VTs = getVTList(VT); 2220 if (VT != MVT::Flag) { 2221 SDOperand Ops[] = { N1, N2, N3 }; 2222 FoldingSetNodeID ID; 2223 AddNodeIDNode(ID, Opcode, VTs, Ops, 3); 2224 void *IP = 0; 2225 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2226 return SDOperand(E, 0); 2227 N = new TernarySDNode(Opcode, VTs, N1, N2, N3); 2228 CSEMap.InsertNode(N, IP); 2229 } else { 2230 N = new TernarySDNode(Opcode, VTs, N1, N2, N3); 2231 } 2232 AllNodes.push_back(N); 2233 return SDOperand(N, 0); 2234} 2235 2236SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2237 SDOperand N1, SDOperand N2, SDOperand N3, 2238 SDOperand N4) { 2239 SDOperand Ops[] = { N1, N2, N3, N4 }; 2240 return getNode(Opcode, VT, Ops, 4); 2241} 2242 2243SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2244 SDOperand N1, SDOperand N2, SDOperand N3, 2245 SDOperand N4, SDOperand N5) { 2246 SDOperand Ops[] = { N1, N2, N3, N4, N5 }; 2247 return getNode(Opcode, VT, Ops, 5); 2248} 2249 2250SDOperand SelectionDAG::getLoad(MVT::ValueType VT, 2251 SDOperand Chain, SDOperand Ptr, 2252 const Value *SV, int SVOffset, 2253 bool isVolatile, unsigned Alignment) { 2254 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2255 const Type *Ty = 0; 2256 if (VT != MVT::iPTR) { 2257 Ty = MVT::getTypeForValueType(VT); 2258 } else if (SV) { 2259 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2260 assert(PT && "Value for load must be a pointer"); 2261 Ty = PT->getElementType(); 2262 } 2263 assert(Ty && "Could not get type information for load"); 2264 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2265 } 2266 SDVTList VTs = getVTList(VT, MVT::Other); 2267 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2268 SDOperand Ops[] = { Chain, Ptr, Undef }; 2269 FoldingSetNodeID ID; 2270 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 2271 ID.AddInteger(ISD::UNINDEXED); 2272 ID.AddInteger(ISD::NON_EXTLOAD); 2273 ID.AddInteger((unsigned int)VT); 2274 ID.AddPointer(SV); 2275 ID.AddInteger(SVOffset); 2276 ID.AddInteger(Alignment); 2277 ID.AddInteger(isVolatile); 2278 void *IP = 0; 2279 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2280 return SDOperand(E, 0); 2281 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, 2282 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment, 2283 isVolatile); 2284 CSEMap.InsertNode(N, IP); 2285 AllNodes.push_back(N); 2286 return SDOperand(N, 0); 2287} 2288 2289SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT, 2290 SDOperand Chain, SDOperand Ptr, 2291 const Value *SV, 2292 int SVOffset, MVT::ValueType EVT, 2293 bool isVolatile, unsigned Alignment) { 2294 // If they are asking for an extending load from/to the same thing, return a 2295 // normal load. 2296 if (VT == EVT) 2297 ExtType = ISD::NON_EXTLOAD; 2298 2299 if (MVT::isVector(VT)) 2300 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!"); 2301 else 2302 assert(EVT < VT && "Should only be an extending load, not truncating!"); 2303 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) && 2304 "Cannot sign/zero extend a FP/Vector load!"); 2305 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) && 2306 "Cannot convert from FP to Int or Int -> FP!"); 2307 2308 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2309 const Type *Ty = 0; 2310 if (VT != MVT::iPTR) { 2311 Ty = MVT::getTypeForValueType(VT); 2312 } else if (SV) { 2313 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2314 assert(PT && "Value for load must be a pointer"); 2315 Ty = PT->getElementType(); 2316 } 2317 assert(Ty && "Could not get type information for load"); 2318 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2319 } 2320 SDVTList VTs = getVTList(VT, MVT::Other); 2321 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2322 SDOperand Ops[] = { Chain, Ptr, Undef }; 2323 FoldingSetNodeID ID; 2324 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 2325 ID.AddInteger(ISD::UNINDEXED); 2326 ID.AddInteger(ExtType); 2327 ID.AddInteger((unsigned int)EVT); 2328 ID.AddPointer(SV); 2329 ID.AddInteger(SVOffset); 2330 ID.AddInteger(Alignment); 2331 ID.AddInteger(isVolatile); 2332 void *IP = 0; 2333 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2334 return SDOperand(E, 0); 2335 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT, 2336 SV, SVOffset, Alignment, isVolatile); 2337 CSEMap.InsertNode(N, IP); 2338 AllNodes.push_back(N); 2339 return SDOperand(N, 0); 2340} 2341 2342SDOperand 2343SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base, 2344 SDOperand Offset, ISD::MemIndexedMode AM) { 2345 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad); 2346 assert(LD->getOffset().getOpcode() == ISD::UNDEF && 2347 "Load is already a indexed load!"); 2348 MVT::ValueType VT = OrigLoad.getValueType(); 2349 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other); 2350 SDOperand Ops[] = { LD->getChain(), Base, Offset }; 2351 FoldingSetNodeID ID; 2352 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 2353 ID.AddInteger(AM); 2354 ID.AddInteger(LD->getExtensionType()); 2355 ID.AddInteger((unsigned int)(LD->getLoadedVT())); 2356 ID.AddPointer(LD->getSrcValue()); 2357 ID.AddInteger(LD->getSrcValueOffset()); 2358 ID.AddInteger(LD->getAlignment()); 2359 ID.AddInteger(LD->isVolatile()); 2360 void *IP = 0; 2361 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2362 return SDOperand(E, 0); 2363 SDNode *N = new LoadSDNode(Ops, VTs, AM, 2364 LD->getExtensionType(), LD->getLoadedVT(), 2365 LD->getSrcValue(), LD->getSrcValueOffset(), 2366 LD->getAlignment(), LD->isVolatile()); 2367 CSEMap.InsertNode(N, IP); 2368 AllNodes.push_back(N); 2369 return SDOperand(N, 0); 2370} 2371 2372SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val, 2373 SDOperand Ptr, const Value *SV, int SVOffset, 2374 bool isVolatile, unsigned Alignment) { 2375 MVT::ValueType VT = Val.getValueType(); 2376 2377 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2378 const Type *Ty = 0; 2379 if (VT != MVT::iPTR) { 2380 Ty = MVT::getTypeForValueType(VT); 2381 } else if (SV) { 2382 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2383 assert(PT && "Value for store must be a pointer"); 2384 Ty = PT->getElementType(); 2385 } 2386 assert(Ty && "Could not get type information for store"); 2387 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2388 } 2389 SDVTList VTs = getVTList(MVT::Other); 2390 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2391 SDOperand Ops[] = { Chain, Val, Ptr, Undef }; 2392 FoldingSetNodeID ID; 2393 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 2394 ID.AddInteger(ISD::UNINDEXED); 2395 ID.AddInteger(false); 2396 ID.AddInteger((unsigned int)VT); 2397 ID.AddPointer(SV); 2398 ID.AddInteger(SVOffset); 2399 ID.AddInteger(Alignment); 2400 ID.AddInteger(isVolatile); 2401 void *IP = 0; 2402 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2403 return SDOperand(E, 0); 2404 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false, 2405 VT, SV, SVOffset, Alignment, isVolatile); 2406 CSEMap.InsertNode(N, IP); 2407 AllNodes.push_back(N); 2408 return SDOperand(N, 0); 2409} 2410 2411SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val, 2412 SDOperand Ptr, const Value *SV, 2413 int SVOffset, MVT::ValueType SVT, 2414 bool isVolatile, unsigned Alignment) { 2415 MVT::ValueType VT = Val.getValueType(); 2416 bool isTrunc = VT != SVT; 2417 2418 assert(VT > SVT && "Not a truncation?"); 2419 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) && 2420 "Can't do FP-INT conversion!"); 2421 2422 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2423 const Type *Ty = 0; 2424 if (VT != MVT::iPTR) { 2425 Ty = MVT::getTypeForValueType(VT); 2426 } else if (SV) { 2427 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2428 assert(PT && "Value for store must be a pointer"); 2429 Ty = PT->getElementType(); 2430 } 2431 assert(Ty && "Could not get type information for store"); 2432 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2433 } 2434 SDVTList VTs = getVTList(MVT::Other); 2435 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2436 SDOperand Ops[] = { Chain, Val, Ptr, Undef }; 2437 FoldingSetNodeID ID; 2438 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 2439 ID.AddInteger(ISD::UNINDEXED); 2440 ID.AddInteger(isTrunc); 2441 ID.AddInteger((unsigned int)SVT); 2442 ID.AddPointer(SV); 2443 ID.AddInteger(SVOffset); 2444 ID.AddInteger(Alignment); 2445 ID.AddInteger(isVolatile); 2446 void *IP = 0; 2447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2448 return SDOperand(E, 0); 2449 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc, 2450 SVT, SV, SVOffset, Alignment, isVolatile); 2451 CSEMap.InsertNode(N, IP); 2452 AllNodes.push_back(N); 2453 return SDOperand(N, 0); 2454} 2455 2456SDOperand 2457SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base, 2458 SDOperand Offset, ISD::MemIndexedMode AM) { 2459 StoreSDNode *ST = cast<StoreSDNode>(OrigStore); 2460 assert(ST->getOffset().getOpcode() == ISD::UNDEF && 2461 "Store is already a indexed store!"); 2462 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other); 2463 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset }; 2464 FoldingSetNodeID ID; 2465 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 2466 ID.AddInteger(AM); 2467 ID.AddInteger(ST->isTruncatingStore()); 2468 ID.AddInteger((unsigned int)(ST->getStoredVT())); 2469 ID.AddPointer(ST->getSrcValue()); 2470 ID.AddInteger(ST->getSrcValueOffset()); 2471 ID.AddInteger(ST->getAlignment()); 2472 ID.AddInteger(ST->isVolatile()); 2473 void *IP = 0; 2474 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2475 return SDOperand(E, 0); 2476 SDNode *N = new StoreSDNode(Ops, VTs, AM, 2477 ST->isTruncatingStore(), ST->getStoredVT(), 2478 ST->getSrcValue(), ST->getSrcValueOffset(), 2479 ST->getAlignment(), ST->isVolatile()); 2480 CSEMap.InsertNode(N, IP); 2481 AllNodes.push_back(N); 2482 return SDOperand(N, 0); 2483} 2484 2485SDOperand SelectionDAG::getVAArg(MVT::ValueType VT, 2486 SDOperand Chain, SDOperand Ptr, 2487 SDOperand SV) { 2488 SDOperand Ops[] = { Chain, Ptr, SV }; 2489 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3); 2490} 2491 2492SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2493 const SDOperand *Ops, unsigned NumOps) { 2494 switch (NumOps) { 2495 case 0: return getNode(Opcode, VT); 2496 case 1: return getNode(Opcode, VT, Ops[0]); 2497 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); 2498 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); 2499 default: break; 2500 } 2501 2502 switch (Opcode) { 2503 default: break; 2504 case ISD::SELECT_CC: { 2505 assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); 2506 assert(Ops[0].getValueType() == Ops[1].getValueType() && 2507 "LHS and RHS of condition must have same type!"); 2508 assert(Ops[2].getValueType() == Ops[3].getValueType() && 2509 "True and False arms of SelectCC must have same type!"); 2510 assert(Ops[2].getValueType() == VT && 2511 "select_cc node must be of same type as true and false value!"); 2512 break; 2513 } 2514 case ISD::BR_CC: { 2515 assert(NumOps == 5 && "BR_CC takes 5 operands!"); 2516 assert(Ops[2].getValueType() == Ops[3].getValueType() && 2517 "LHS/RHS of comparison should match types!"); 2518 break; 2519 } 2520 } 2521 2522 // Memoize nodes. 2523 SDNode *N; 2524 SDVTList VTs = getVTList(VT); 2525 if (VT != MVT::Flag) { 2526 FoldingSetNodeID ID; 2527 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps); 2528 void *IP = 0; 2529 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2530 return SDOperand(E, 0); 2531 N = new SDNode(Opcode, VTs, Ops, NumOps); 2532 CSEMap.InsertNode(N, IP); 2533 } else { 2534 N = new SDNode(Opcode, VTs, Ops, NumOps); 2535 } 2536 AllNodes.push_back(N); 2537 return SDOperand(N, 0); 2538} 2539 2540SDOperand SelectionDAG::getNode(unsigned Opcode, 2541 std::vector<MVT::ValueType> &ResultTys, 2542 const SDOperand *Ops, unsigned NumOps) { 2543 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(), 2544 Ops, NumOps); 2545} 2546 2547SDOperand SelectionDAG::getNode(unsigned Opcode, 2548 const MVT::ValueType *VTs, unsigned NumVTs, 2549 const SDOperand *Ops, unsigned NumOps) { 2550 if (NumVTs == 1) 2551 return getNode(Opcode, VTs[0], Ops, NumOps); 2552 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps); 2553} 2554 2555SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2556 const SDOperand *Ops, unsigned NumOps) { 2557 if (VTList.NumVTs == 1) 2558 return getNode(Opcode, VTList.VTs[0], Ops, NumOps); 2559 2560 switch (Opcode) { 2561 // FIXME: figure out how to safely handle things like 2562 // int foo(int x) { return 1 << (x & 255); } 2563 // int bar() { return foo(256); } 2564#if 0 2565 case ISD::SRA_PARTS: 2566 case ISD::SRL_PARTS: 2567 case ISD::SHL_PARTS: 2568 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 2569 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 2570 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 2571 else if (N3.getOpcode() == ISD::AND) 2572 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 2573 // If the and is only masking out bits that cannot effect the shift, 2574 // eliminate the and. 2575 unsigned NumBits = MVT::getSizeInBits(VT)*2; 2576 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 2577 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 2578 } 2579 break; 2580#endif 2581 } 2582 2583 // Memoize the node unless it returns a flag. 2584 SDNode *N; 2585 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) { 2586 FoldingSetNodeID ID; 2587 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 2588 void *IP = 0; 2589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2590 return SDOperand(E, 0); 2591 if (NumOps == 1) 2592 N = new UnarySDNode(Opcode, VTList, Ops[0]); 2593 else if (NumOps == 2) 2594 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]); 2595 else if (NumOps == 3) 2596 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]); 2597 else 2598 N = new SDNode(Opcode, VTList, Ops, NumOps); 2599 CSEMap.InsertNode(N, IP); 2600 } else { 2601 if (NumOps == 1) 2602 N = new UnarySDNode(Opcode, VTList, Ops[0]); 2603 else if (NumOps == 2) 2604 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]); 2605 else if (NumOps == 3) 2606 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]); 2607 else 2608 N = new SDNode(Opcode, VTList, Ops, NumOps); 2609 } 2610 AllNodes.push_back(N); 2611 return SDOperand(N, 0); 2612} 2613 2614SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) { 2615 return getNode(Opcode, VTList, 0, 0); 2616} 2617 2618SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2619 SDOperand N1) { 2620 SDOperand Ops[] = { N1 }; 2621 return getNode(Opcode, VTList, Ops, 1); 2622} 2623 2624SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2625 SDOperand N1, SDOperand N2) { 2626 SDOperand Ops[] = { N1, N2 }; 2627 return getNode(Opcode, VTList, Ops, 2); 2628} 2629 2630SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2631 SDOperand N1, SDOperand N2, SDOperand N3) { 2632 SDOperand Ops[] = { N1, N2, N3 }; 2633 return getNode(Opcode, VTList, Ops, 3); 2634} 2635 2636SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2637 SDOperand N1, SDOperand N2, SDOperand N3, 2638 SDOperand N4) { 2639 SDOperand Ops[] = { N1, N2, N3, N4 }; 2640 return getNode(Opcode, VTList, Ops, 4); 2641} 2642 2643SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2644 SDOperand N1, SDOperand N2, SDOperand N3, 2645 SDOperand N4, SDOperand N5) { 2646 SDOperand Ops[] = { N1, N2, N3, N4, N5 }; 2647 return getNode(Opcode, VTList, Ops, 5); 2648} 2649 2650SDVTList SelectionDAG::getVTList(MVT::ValueType VT) { 2651 if (!MVT::isExtendedVT(VT)) 2652 return makeVTList(SDNode::getValueTypeList(VT), 1); 2653 2654 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2655 E = VTList.end(); I != E; ++I) { 2656 if (I->size() == 1 && (*I)[0] == VT) 2657 return makeVTList(&(*I)[0], 1); 2658 } 2659 std::vector<MVT::ValueType> V; 2660 V.push_back(VT); 2661 VTList.push_front(V); 2662 return makeVTList(&(*VTList.begin())[0], 1); 2663} 2664 2665SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) { 2666 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2667 E = VTList.end(); I != E; ++I) { 2668 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) 2669 return makeVTList(&(*I)[0], 2); 2670 } 2671 std::vector<MVT::ValueType> V; 2672 V.push_back(VT1); 2673 V.push_back(VT2); 2674 VTList.push_front(V); 2675 return makeVTList(&(*VTList.begin())[0], 2); 2676} 2677SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2, 2678 MVT::ValueType VT3) { 2679 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2680 E = VTList.end(); I != E; ++I) { 2681 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 && 2682 (*I)[2] == VT3) 2683 return makeVTList(&(*I)[0], 3); 2684 } 2685 std::vector<MVT::ValueType> V; 2686 V.push_back(VT1); 2687 V.push_back(VT2); 2688 V.push_back(VT3); 2689 VTList.push_front(V); 2690 return makeVTList(&(*VTList.begin())[0], 3); 2691} 2692 2693SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) { 2694 switch (NumVTs) { 2695 case 0: assert(0 && "Cannot have nodes without results!"); 2696 case 1: return getVTList(VTs[0]); 2697 case 2: return getVTList(VTs[0], VTs[1]); 2698 case 3: return getVTList(VTs[0], VTs[1], VTs[2]); 2699 default: break; 2700 } 2701 2702 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2703 E = VTList.end(); I != E; ++I) { 2704 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue; 2705 2706 bool NoMatch = false; 2707 for (unsigned i = 2; i != NumVTs; ++i) 2708 if (VTs[i] != (*I)[i]) { 2709 NoMatch = true; 2710 break; 2711 } 2712 if (!NoMatch) 2713 return makeVTList(&*I->begin(), NumVTs); 2714 } 2715 2716 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs)); 2717 return makeVTList(&*VTList.begin()->begin(), NumVTs); 2718} 2719 2720 2721/// UpdateNodeOperands - *Mutate* the specified node in-place to have the 2722/// specified operands. If the resultant node already exists in the DAG, 2723/// this does not modify the specified node, instead it returns the node that 2724/// already exists. If the resultant node does not exist in the DAG, the 2725/// input node is returned. As a degenerate case, if you specify the same 2726/// input operands as the node already has, the input node is returned. 2727SDOperand SelectionDAG:: 2728UpdateNodeOperands(SDOperand InN, SDOperand Op) { 2729 SDNode *N = InN.Val; 2730 assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 2731 2732 // Check to see if there is no change. 2733 if (Op == N->getOperand(0)) return InN; 2734 2735 // See if the modified node already exists. 2736 void *InsertPos = 0; 2737 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) 2738 return SDOperand(Existing, InN.ResNo); 2739 2740 // Nope it doesn't. Remove the node from it's current place in the maps. 2741 if (InsertPos) 2742 RemoveNodeFromCSEMaps(N); 2743 2744 // Now we update the operands. 2745 N->OperandList[0].Val->removeUser(N); 2746 Op.Val->addUser(N); 2747 N->OperandList[0] = Op; 2748 2749 // If this gets put into a CSE map, add it. 2750 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 2751 return InN; 2752} 2753 2754SDOperand SelectionDAG:: 2755UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) { 2756 SDNode *N = InN.Val; 2757 assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 2758 2759 // Check to see if there is no change. 2760 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 2761 return InN; // No operands changed, just return the input node. 2762 2763 // See if the modified node already exists. 2764 void *InsertPos = 0; 2765 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) 2766 return SDOperand(Existing, InN.ResNo); 2767 2768 // Nope it doesn't. Remove the node from it's current place in the maps. 2769 if (InsertPos) 2770 RemoveNodeFromCSEMaps(N); 2771 2772 // Now we update the operands. 2773 if (N->OperandList[0] != Op1) { 2774 N->OperandList[0].Val->removeUser(N); 2775 Op1.Val->addUser(N); 2776 N->OperandList[0] = Op1; 2777 } 2778 if (N->OperandList[1] != Op2) { 2779 N->OperandList[1].Val->removeUser(N); 2780 Op2.Val->addUser(N); 2781 N->OperandList[1] = Op2; 2782 } 2783 2784 // If this gets put into a CSE map, add it. 2785 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 2786 return InN; 2787} 2788 2789SDOperand SelectionDAG:: 2790UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) { 2791 SDOperand Ops[] = { Op1, Op2, Op3 }; 2792 return UpdateNodeOperands(N, Ops, 3); 2793} 2794 2795SDOperand SelectionDAG:: 2796UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 2797 SDOperand Op3, SDOperand Op4) { 2798 SDOperand Ops[] = { Op1, Op2, Op3, Op4 }; 2799 return UpdateNodeOperands(N, Ops, 4); 2800} 2801 2802SDOperand SelectionDAG:: 2803UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 2804 SDOperand Op3, SDOperand Op4, SDOperand Op5) { 2805 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 }; 2806 return UpdateNodeOperands(N, Ops, 5); 2807} 2808 2809 2810SDOperand SelectionDAG:: 2811UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) { 2812 SDNode *N = InN.Val; 2813 assert(N->getNumOperands() == NumOps && 2814 "Update with wrong number of operands"); 2815 2816 // Check to see if there is no change. 2817 bool AnyChange = false; 2818 for (unsigned i = 0; i != NumOps; ++i) { 2819 if (Ops[i] != N->getOperand(i)) { 2820 AnyChange = true; 2821 break; 2822 } 2823 } 2824 2825 // No operands changed, just return the input node. 2826 if (!AnyChange) return InN; 2827 2828 // See if the modified node already exists. 2829 void *InsertPos = 0; 2830 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos)) 2831 return SDOperand(Existing, InN.ResNo); 2832 2833 // Nope it doesn't. Remove the node from it's current place in the maps. 2834 if (InsertPos) 2835 RemoveNodeFromCSEMaps(N); 2836 2837 // Now we update the operands. 2838 for (unsigned i = 0; i != NumOps; ++i) { 2839 if (N->OperandList[i] != Ops[i]) { 2840 N->OperandList[i].Val->removeUser(N); 2841 Ops[i].Val->addUser(N); 2842 N->OperandList[i] = Ops[i]; 2843 } 2844 } 2845 2846 // If this gets put into a CSE map, add it. 2847 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 2848 return InN; 2849} 2850 2851 2852/// MorphNodeTo - This frees the operands of the current node, resets the 2853/// opcode, types, and operands to the specified value. This should only be 2854/// used by the SelectionDAG class. 2855void SDNode::MorphNodeTo(unsigned Opc, SDVTList L, 2856 const SDOperand *Ops, unsigned NumOps) { 2857 NodeType = Opc; 2858 ValueList = L.VTs; 2859 NumValues = L.NumVTs; 2860 2861 // Clear the operands list, updating used nodes to remove this from their 2862 // use list. 2863 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) 2864 I->Val->removeUser(this); 2865 2866 // If NumOps is larger than the # of operands we currently have, reallocate 2867 // the operand list. 2868 if (NumOps > NumOperands) { 2869 if (OperandsNeedDelete) 2870 delete [] OperandList; 2871 OperandList = new SDOperand[NumOps]; 2872 OperandsNeedDelete = true; 2873 } 2874 2875 // Assign the new operands. 2876 NumOperands = NumOps; 2877 2878 for (unsigned i = 0, e = NumOps; i != e; ++i) { 2879 OperandList[i] = Ops[i]; 2880 SDNode *N = OperandList[i].Val; 2881 N->Uses.push_back(this); 2882 } 2883} 2884 2885/// SelectNodeTo - These are used for target selectors to *mutate* the 2886/// specified node to have the specified return type, Target opcode, and 2887/// operands. Note that target opcodes are stored as 2888/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 2889/// 2890/// Note that SelectNodeTo returns the resultant node. If there is already a 2891/// node of the specified opcode and operands, it returns that node instead of 2892/// the current one. 2893SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2894 MVT::ValueType VT) { 2895 SDVTList VTs = getVTList(VT); 2896 FoldingSetNodeID ID; 2897 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0); 2898 void *IP = 0; 2899 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2900 return ON; 2901 2902 RemoveNodeFromCSEMaps(N); 2903 2904 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0); 2905 2906 CSEMap.InsertNode(N, IP); 2907 return N; 2908} 2909 2910SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2911 MVT::ValueType VT, SDOperand Op1) { 2912 // If an identical node already exists, use it. 2913 SDVTList VTs = getVTList(VT); 2914 SDOperand Ops[] = { Op1 }; 2915 2916 FoldingSetNodeID ID; 2917 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1); 2918 void *IP = 0; 2919 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2920 return ON; 2921 2922 RemoveNodeFromCSEMaps(N); 2923 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1); 2924 CSEMap.InsertNode(N, IP); 2925 return N; 2926} 2927 2928SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2929 MVT::ValueType VT, SDOperand Op1, 2930 SDOperand Op2) { 2931 // If an identical node already exists, use it. 2932 SDVTList VTs = getVTList(VT); 2933 SDOperand Ops[] = { Op1, Op2 }; 2934 2935 FoldingSetNodeID ID; 2936 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 2937 void *IP = 0; 2938 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2939 return ON; 2940 2941 RemoveNodeFromCSEMaps(N); 2942 2943 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 2944 2945 CSEMap.InsertNode(N, IP); // Memoize the new node. 2946 return N; 2947} 2948 2949SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2950 MVT::ValueType VT, SDOperand Op1, 2951 SDOperand Op2, SDOperand Op3) { 2952 // If an identical node already exists, use it. 2953 SDVTList VTs = getVTList(VT); 2954 SDOperand Ops[] = { Op1, Op2, Op3 }; 2955 FoldingSetNodeID ID; 2956 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 2957 void *IP = 0; 2958 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2959 return ON; 2960 2961 RemoveNodeFromCSEMaps(N); 2962 2963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 2964 2965 CSEMap.InsertNode(N, IP); // Memoize the new node. 2966 return N; 2967} 2968 2969SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2970 MVT::ValueType VT, const SDOperand *Ops, 2971 unsigned NumOps) { 2972 // If an identical node already exists, use it. 2973 SDVTList VTs = getVTList(VT); 2974 FoldingSetNodeID ID; 2975 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps); 2976 void *IP = 0; 2977 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2978 return ON; 2979 2980 RemoveNodeFromCSEMaps(N); 2981 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps); 2982 2983 CSEMap.InsertNode(N, IP); // Memoize the new node. 2984 return N; 2985} 2986 2987SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2988 MVT::ValueType VT1, MVT::ValueType VT2, 2989 SDOperand Op1, SDOperand Op2) { 2990 SDVTList VTs = getVTList(VT1, VT2); 2991 FoldingSetNodeID ID; 2992 SDOperand Ops[] = { Op1, Op2 }; 2993 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 2994 void *IP = 0; 2995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2996 return ON; 2997 2998 RemoveNodeFromCSEMaps(N); 2999 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 3000 CSEMap.InsertNode(N, IP); // Memoize the new node. 3001 return N; 3002} 3003 3004SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 3005 MVT::ValueType VT1, MVT::ValueType VT2, 3006 SDOperand Op1, SDOperand Op2, 3007 SDOperand Op3) { 3008 // If an identical node already exists, use it. 3009 SDVTList VTs = getVTList(VT1, VT2); 3010 SDOperand Ops[] = { Op1, Op2, Op3 }; 3011 FoldingSetNodeID ID; 3012 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 3013 void *IP = 0; 3014 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 3015 return ON; 3016 3017 RemoveNodeFromCSEMaps(N); 3018 3019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 3020 CSEMap.InsertNode(N, IP); // Memoize the new node. 3021 return N; 3022} 3023 3024 3025/// getTargetNode - These are used for target selectors to create a new node 3026/// with specified return type(s), target opcode, and operands. 3027/// 3028/// Note that getTargetNode returns the resultant node. If there is already a 3029/// node of the specified opcode and operands, it returns that node instead of 3030/// the current one. 3031SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) { 3032 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val; 3033} 3034SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3035 SDOperand Op1) { 3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val; 3037} 3038SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3039 SDOperand Op1, SDOperand Op2) { 3040 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val; 3041} 3042SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3043 SDOperand Op1, SDOperand Op2, 3044 SDOperand Op3) { 3045 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val; 3046} 3047SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3048 const SDOperand *Ops, unsigned NumOps) { 3049 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val; 3050} 3051SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3052 MVT::ValueType VT2) { 3053 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3054 SDOperand Op; 3055 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val; 3056} 3057SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3058 MVT::ValueType VT2, SDOperand Op1) { 3059 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3060 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val; 3061} 3062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3063 MVT::ValueType VT2, SDOperand Op1, 3064 SDOperand Op2) { 3065 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3066 SDOperand Ops[] = { Op1, Op2 }; 3067 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val; 3068} 3069SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3070 MVT::ValueType VT2, SDOperand Op1, 3071 SDOperand Op2, SDOperand Op3) { 3072 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3073 SDOperand Ops[] = { Op1, Op2, Op3 }; 3074 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val; 3075} 3076SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3077 MVT::ValueType VT2, 3078 const SDOperand *Ops, unsigned NumOps) { 3079 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3080 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val; 3081} 3082SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3083 MVT::ValueType VT2, MVT::ValueType VT3, 3084 SDOperand Op1, SDOperand Op2) { 3085 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 3086 SDOperand Ops[] = { Op1, Op2 }; 3087 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val; 3088} 3089SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3090 MVT::ValueType VT2, MVT::ValueType VT3, 3091 SDOperand Op1, SDOperand Op2, 3092 SDOperand Op3) { 3093 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 3094 SDOperand Ops[] = { Op1, Op2, Op3 }; 3095 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val; 3096} 3097SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3098 MVT::ValueType VT2, MVT::ValueType VT3, 3099 const SDOperand *Ops, unsigned NumOps) { 3100 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val; 3102} 3103SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3104 MVT::ValueType VT2, MVT::ValueType VT3, 3105 MVT::ValueType VT4, 3106 const SDOperand *Ops, unsigned NumOps) { 3107 std::vector<MVT::ValueType> VTList; 3108 VTList.push_back(VT1); 3109 VTList.push_back(VT2); 3110 VTList.push_back(VT3); 3111 VTList.push_back(VT4); 3112 const MVT::ValueType *VTs = getNodeValueTypes(VTList); 3113 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val; 3114} 3115SDNode *SelectionDAG::getTargetNode(unsigned Opcode, 3116 std::vector<MVT::ValueType> &ResultTys, 3117 const SDOperand *Ops, unsigned NumOps) { 3118 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys); 3119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(), 3120 Ops, NumOps).Val; 3121} 3122 3123/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 3124/// This can cause recursive merging of nodes in the DAG. 3125/// 3126/// This version assumes From/To have a single result value. 3127/// 3128void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, 3129 std::vector<SDNode*> *Deleted) { 3130 SDNode *From = FromN.Val, *To = ToN.Val; 3131 assert(From->getNumValues() == 1 && To->getNumValues() == 1 && 3132 "Cannot replace with this method!"); 3133 assert(From != To && "Cannot replace uses of with self"); 3134 3135 while (!From->use_empty()) { 3136 // Process users until they are all gone. 3137 SDNode *U = *From->use_begin(); 3138 3139 // This node is about to morph, remove its old self from the CSE maps. 3140 RemoveNodeFromCSEMaps(U); 3141 3142 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 3143 I != E; ++I) 3144 if (I->Val == From) { 3145 From->removeUser(U); 3146 I->Val = To; 3147 To->addUser(U); 3148 } 3149 3150 // Now that we have modified U, add it back to the CSE maps. If it already 3151 // exists there, recursively merge the results together. 3152 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 3153 ReplaceAllUsesWith(U, Existing, Deleted); 3154 // U is now dead. 3155 if (Deleted) Deleted->push_back(U); 3156 DeleteNodeNotInCSEMaps(U); 3157 } 3158 } 3159} 3160 3161/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 3162/// This can cause recursive merging of nodes in the DAG. 3163/// 3164/// This version assumes From/To have matching types and numbers of result 3165/// values. 3166/// 3167void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 3168 std::vector<SDNode*> *Deleted) { 3169 assert(From != To && "Cannot replace uses of with self"); 3170 assert(From->getNumValues() == To->getNumValues() && 3171 "Cannot use this version of ReplaceAllUsesWith!"); 3172 if (From->getNumValues() == 1) { // If possible, use the faster version. 3173 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); 3174 return; 3175 } 3176 3177 while (!From->use_empty()) { 3178 // Process users until they are all gone. 3179 SDNode *U = *From->use_begin(); 3180 3181 // This node is about to morph, remove its old self from the CSE maps. 3182 RemoveNodeFromCSEMaps(U); 3183 3184 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 3185 I != E; ++I) 3186 if (I->Val == From) { 3187 From->removeUser(U); 3188 I->Val = To; 3189 To->addUser(U); 3190 } 3191 3192 // Now that we have modified U, add it back to the CSE maps. If it already 3193 // exists there, recursively merge the results together. 3194 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 3195 ReplaceAllUsesWith(U, Existing, Deleted); 3196 // U is now dead. 3197 if (Deleted) Deleted->push_back(U); 3198 DeleteNodeNotInCSEMaps(U); 3199 } 3200 } 3201} 3202 3203/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 3204/// This can cause recursive merging of nodes in the DAG. 3205/// 3206/// This version can replace From with any result values. To must match the 3207/// number and types of values returned by From. 3208void SelectionDAG::ReplaceAllUsesWith(SDNode *From, 3209 const SDOperand *To, 3210 std::vector<SDNode*> *Deleted) { 3211 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) { 3212 // Degenerate case handled above. 3213 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); 3214 return; 3215 } 3216 3217 while (!From->use_empty()) { 3218 // Process users until they are all gone. 3219 SDNode *U = *From->use_begin(); 3220 3221 // This node is about to morph, remove its old self from the CSE maps. 3222 RemoveNodeFromCSEMaps(U); 3223 3224 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 3225 I != E; ++I) 3226 if (I->Val == From) { 3227 const SDOperand &ToOp = To[I->ResNo]; 3228 From->removeUser(U); 3229 *I = ToOp; 3230 ToOp.Val->addUser(U); 3231 } 3232 3233 // Now that we have modified U, add it back to the CSE maps. If it already 3234 // exists there, recursively merge the results together. 3235 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 3236 ReplaceAllUsesWith(U, Existing, Deleted); 3237 // U is now dead. 3238 if (Deleted) Deleted->push_back(U); 3239 DeleteNodeNotInCSEMaps(U); 3240 } 3241 } 3242} 3243 3244/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving 3245/// uses of other values produced by From.Val alone. The Deleted vector is 3246/// handled the same was as for ReplaceAllUsesWith. 3247void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To, 3248 std::vector<SDNode*> *Deleted) { 3249 assert(From != To && "Cannot replace a value with itself"); 3250 // Handle the simple, trivial, case efficiently. 3251 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) { 3252 ReplaceAllUsesWith(From, To, Deleted); 3253 return; 3254 } 3255 3256 // Get all of the users of From.Val. We want these in a nice, 3257 // deterministically ordered and uniqued set, so we use a SmallSetVector. 3258 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end()); 3259 3260 std::vector<SDNode*> LocalDeletionVector; 3261 3262 // Pick a deletion vector to use. If the user specified one, use theirs, 3263 // otherwise use a local one. 3264 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector; 3265 while (!Users.empty()) { 3266 // We know that this user uses some value of From. If it is the right 3267 // value, update it. 3268 SDNode *User = Users.back(); 3269 Users.pop_back(); 3270 3271 // Scan for an operand that matches From. 3272 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands; 3273 for (; Op != E; ++Op) 3274 if (*Op == From) break; 3275 3276 // If there are no matches, the user must use some other result of From. 3277 if (Op == E) continue; 3278 3279 // Okay, we know this user needs to be updated. Remove its old self 3280 // from the CSE maps. 3281 RemoveNodeFromCSEMaps(User); 3282 3283 // Update all operands that match "From". 3284 for (; Op != E; ++Op) { 3285 if (*Op == From) { 3286 From.Val->removeUser(User); 3287 *Op = To; 3288 To.Val->addUser(User); 3289 } 3290 } 3291 3292 // Now that we have modified User, add it back to the CSE maps. If it 3293 // already exists there, recursively merge the results together. 3294 SDNode *Existing = AddNonLeafNodeToCSEMaps(User); 3295 if (!Existing) continue; // Continue on to next user. 3296 3297 // If there was already an existing matching node, use ReplaceAllUsesWith 3298 // to replace the dead one with the existing one. However, this can cause 3299 // recursive merging of other unrelated nodes down the line. The merging 3300 // can cause deletion of nodes that used the old value. In this case, 3301 // we have to be certain to remove them from the Users set. 3302 unsigned NumDeleted = DeleteVector->size(); 3303 ReplaceAllUsesWith(User, Existing, DeleteVector); 3304 3305 // User is now dead. 3306 DeleteVector->push_back(User); 3307 DeleteNodeNotInCSEMaps(User); 3308 3309 // We have to be careful here, because ReplaceAllUsesWith could have 3310 // deleted a user of From, which means there may be dangling pointers 3311 // in the "Users" setvector. Scan over the deleted node pointers and 3312 // remove them from the setvector. 3313 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i) 3314 Users.remove((*DeleteVector)[i]); 3315 3316 // If the user doesn't need the set of deleted elements, don't retain them 3317 // to the next loop iteration. 3318 if (Deleted == 0) 3319 LocalDeletionVector.clear(); 3320 } 3321} 3322 3323 3324/// AssignNodeIds - Assign a unique node id for each node in the DAG based on 3325/// their allnodes order. It returns the maximum id. 3326unsigned SelectionDAG::AssignNodeIds() { 3327 unsigned Id = 0; 3328 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){ 3329 SDNode *N = I; 3330 N->setNodeId(Id++); 3331 } 3332 return Id; 3333} 3334 3335/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG 3336/// based on their topological order. It returns the maximum id and a vector 3337/// of the SDNodes* in assigned order by reference. 3338unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) { 3339 unsigned DAGSize = AllNodes.size(); 3340 std::vector<unsigned> InDegree(DAGSize); 3341 std::vector<SDNode*> Sources; 3342 3343 // Use a two pass approach to avoid using a std::map which is slow. 3344 unsigned Id = 0; 3345 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){ 3346 SDNode *N = I; 3347 N->setNodeId(Id++); 3348 unsigned Degree = N->use_size(); 3349 InDegree[N->getNodeId()] = Degree; 3350 if (Degree == 0) 3351 Sources.push_back(N); 3352 } 3353 3354 TopOrder.clear(); 3355 while (!Sources.empty()) { 3356 SDNode *N = Sources.back(); 3357 Sources.pop_back(); 3358 TopOrder.push_back(N); 3359 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 3360 SDNode *P = I->Val; 3361 unsigned Degree = --InDegree[P->getNodeId()]; 3362 if (Degree == 0) 3363 Sources.push_back(P); 3364 } 3365 } 3366 3367 // Second pass, assign the actual topological order as node ids. 3368 Id = 0; 3369 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end(); 3370 TI != TE; ++TI) 3371 (*TI)->setNodeId(Id++); 3372 3373 return Id; 3374} 3375 3376 3377 3378//===----------------------------------------------------------------------===// 3379// SDNode Class 3380//===----------------------------------------------------------------------===// 3381 3382// Out-of-line virtual method to give class a home. 3383void SDNode::ANCHOR() {} 3384void UnarySDNode::ANCHOR() {} 3385void BinarySDNode::ANCHOR() {} 3386void TernarySDNode::ANCHOR() {} 3387void HandleSDNode::ANCHOR() {} 3388void StringSDNode::ANCHOR() {} 3389void ConstantSDNode::ANCHOR() {} 3390void ConstantFPSDNode::ANCHOR() {} 3391void GlobalAddressSDNode::ANCHOR() {} 3392void FrameIndexSDNode::ANCHOR() {} 3393void JumpTableSDNode::ANCHOR() {} 3394void ConstantPoolSDNode::ANCHOR() {} 3395void BasicBlockSDNode::ANCHOR() {} 3396void SrcValueSDNode::ANCHOR() {} 3397void RegisterSDNode::ANCHOR() {} 3398void ExternalSymbolSDNode::ANCHOR() {} 3399void CondCodeSDNode::ANCHOR() {} 3400void VTSDNode::ANCHOR() {} 3401void LoadSDNode::ANCHOR() {} 3402void StoreSDNode::ANCHOR() {} 3403 3404HandleSDNode::~HandleSDNode() { 3405 SDVTList VTs = { 0, 0 }; 3406 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses. 3407} 3408 3409GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, 3410 MVT::ValueType VT, int o) 3411 : SDNode(isa<GlobalVariable>(GA) && 3412 cast<GlobalVariable>(GA)->isThreadLocal() ? 3413 // Thread Local 3414 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) : 3415 // Non Thread Local 3416 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress), 3417 getSDVTList(VT)), Offset(o) { 3418 TheGlobal = const_cast<GlobalValue*>(GA); 3419} 3420 3421/// Profile - Gather unique data for the node. 3422/// 3423void SDNode::Profile(FoldingSetNodeID &ID) { 3424 AddNodeIDNode(ID, this); 3425} 3426 3427/// getValueTypeList - Return a pointer to the specified value type. 3428/// 3429MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { 3430 static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; 3431 VTs[VT] = VT; 3432 return &VTs[VT]; 3433} 3434 3435/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 3436/// indicated value. This method ignores uses of other values defined by this 3437/// operation. 3438bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { 3439 assert(Value < getNumValues() && "Bad value!"); 3440 3441 // If there is only one value, this is easy. 3442 if (getNumValues() == 1) 3443 return use_size() == NUses; 3444 if (use_size() < NUses) return false; 3445 3446 SDOperand TheValue(const_cast<SDNode *>(this), Value); 3447 3448 SmallPtrSet<SDNode*, 32> UsersHandled; 3449 3450 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) { 3451 SDNode *User = *UI; 3452 if (User->getNumOperands() == 1 || 3453 UsersHandled.insert(User)) // First time we've seen this? 3454 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 3455 if (User->getOperand(i) == TheValue) { 3456 if (NUses == 0) 3457 return false; // too many uses 3458 --NUses; 3459 } 3460 } 3461 3462 // Found exactly the right number of uses? 3463 return NUses == 0; 3464} 3465 3466 3467/// hasAnyUseOfValue - Return true if there are any use of the indicated 3468/// value. This method ignores uses of other values defined by this operation. 3469bool SDNode::hasAnyUseOfValue(unsigned Value) const { 3470 assert(Value < getNumValues() && "Bad value!"); 3471 3472 if (use_size() == 0) return false; 3473 3474 SDOperand TheValue(const_cast<SDNode *>(this), Value); 3475 3476 SmallPtrSet<SDNode*, 32> UsersHandled; 3477 3478 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) { 3479 SDNode *User = *UI; 3480 if (User->getNumOperands() == 1 || 3481 UsersHandled.insert(User)) // First time we've seen this? 3482 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 3483 if (User->getOperand(i) == TheValue) { 3484 return true; 3485 } 3486 } 3487 3488 return false; 3489} 3490 3491 3492/// isOnlyUse - Return true if this node is the only use of N. 3493/// 3494bool SDNode::isOnlyUse(SDNode *N) const { 3495 bool Seen = false; 3496 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 3497 SDNode *User = *I; 3498 if (User == this) 3499 Seen = true; 3500 else 3501 return false; 3502 } 3503 3504 return Seen; 3505} 3506 3507/// isOperand - Return true if this node is an operand of N. 3508/// 3509bool SDOperand::isOperand(SDNode *N) const { 3510 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 3511 if (*this == N->getOperand(i)) 3512 return true; 3513 return false; 3514} 3515 3516bool SDNode::isOperand(SDNode *N) const { 3517 for (unsigned i = 0, e = N->NumOperands; i != e; ++i) 3518 if (this == N->OperandList[i].Val) 3519 return true; 3520 return false; 3521} 3522 3523static void findPredecessor(SDNode *N, const SDNode *P, bool &found, 3524 SmallPtrSet<SDNode *, 32> &Visited) { 3525 if (found || !Visited.insert(N)) 3526 return; 3527 3528 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) { 3529 SDNode *Op = N->getOperand(i).Val; 3530 if (Op == P) { 3531 found = true; 3532 return; 3533 } 3534 findPredecessor(Op, P, found, Visited); 3535 } 3536} 3537 3538/// isPredecessor - Return true if this node is a predecessor of N. This node 3539/// is either an operand of N or it can be reached by recursively traversing 3540/// up the operands. 3541/// NOTE: this is an expensive method. Use it carefully. 3542bool SDNode::isPredecessor(SDNode *N) const { 3543 SmallPtrSet<SDNode *, 32> Visited; 3544 bool found = false; 3545 findPredecessor(N, this, found, Visited); 3546 return found; 3547} 3548 3549uint64_t SDNode::getConstantOperandVal(unsigned Num) const { 3550 assert(Num < NumOperands && "Invalid child # of SDNode!"); 3551 return cast<ConstantSDNode>(OperandList[Num])->getValue(); 3552} 3553 3554std::string SDNode::getOperationName(const SelectionDAG *G) const { 3555 switch (getOpcode()) { 3556 default: 3557 if (getOpcode() < ISD::BUILTIN_OP_END) 3558 return "<<Unknown DAG Node>>"; 3559 else { 3560 if (G) { 3561 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 3562 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) 3563 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); 3564 3565 TargetLowering &TLI = G->getTargetLoweringInfo(); 3566 const char *Name = 3567 TLI.getTargetNodeName(getOpcode()); 3568 if (Name) return Name; 3569 } 3570 3571 return "<<Unknown Target Node>>"; 3572 } 3573 3574 case ISD::PCMARKER: return "PCMarker"; 3575 case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 3576 case ISD::SRCVALUE: return "SrcValue"; 3577 case ISD::EntryToken: return "EntryToken"; 3578 case ISD::TokenFactor: return "TokenFactor"; 3579 case ISD::AssertSext: return "AssertSext"; 3580 case ISD::AssertZext: return "AssertZext"; 3581 3582 case ISD::STRING: return "String"; 3583 case ISD::BasicBlock: return "BasicBlock"; 3584 case ISD::VALUETYPE: return "ValueType"; 3585 case ISD::Register: return "Register"; 3586 3587 case ISD::Constant: return "Constant"; 3588 case ISD::ConstantFP: return "ConstantFP"; 3589 case ISD::GlobalAddress: return "GlobalAddress"; 3590 case ISD::GlobalTLSAddress: return "GlobalTLSAddress"; 3591 case ISD::FrameIndex: return "FrameIndex"; 3592 case ISD::JumpTable: return "JumpTable"; 3593 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE"; 3594 case ISD::RETURNADDR: return "RETURNADDR"; 3595 case ISD::FRAMEADDR: return "FRAMEADDR"; 3596 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET"; 3597 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR"; 3598 case ISD::EHSELECTION: return "EHSELECTION"; 3599 case ISD::EH_RETURN: return "EH_RETURN"; 3600 case ISD::ConstantPool: return "ConstantPool"; 3601 case ISD::ExternalSymbol: return "ExternalSymbol"; 3602 case ISD::INTRINSIC_WO_CHAIN: { 3603 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue(); 3604 return Intrinsic::getName((Intrinsic::ID)IID); 3605 } 3606 case ISD::INTRINSIC_VOID: 3607 case ISD::INTRINSIC_W_CHAIN: { 3608 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue(); 3609 return Intrinsic::getName((Intrinsic::ID)IID); 3610 } 3611 3612 case ISD::BUILD_VECTOR: return "BUILD_VECTOR"; 3613 case ISD::TargetConstant: return "TargetConstant"; 3614 case ISD::TargetConstantFP:return "TargetConstantFP"; 3615 case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 3616 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress"; 3617 case ISD::TargetFrameIndex: return "TargetFrameIndex"; 3618 case ISD::TargetJumpTable: return "TargetJumpTable"; 3619 case ISD::TargetConstantPool: return "TargetConstantPool"; 3620 case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 3621 3622 case ISD::CopyToReg: return "CopyToReg"; 3623 case ISD::CopyFromReg: return "CopyFromReg"; 3624 case ISD::UNDEF: return "undef"; 3625 case ISD::MERGE_VALUES: return "merge_values"; 3626 case ISD::INLINEASM: return "inlineasm"; 3627 case ISD::LABEL: return "label"; 3628 case ISD::HANDLENODE: return "handlenode"; 3629 case ISD::FORMAL_ARGUMENTS: return "formal_arguments"; 3630 case ISD::CALL: return "call"; 3631 3632 // Unary operators 3633 case ISD::FABS: return "fabs"; 3634 case ISD::FNEG: return "fneg"; 3635 case ISD::FSQRT: return "fsqrt"; 3636 case ISD::FSIN: return "fsin"; 3637 case ISD::FCOS: return "fcos"; 3638 case ISD::FPOWI: return "fpowi"; 3639 case ISD::FPOW: return "fpow"; 3640 3641 // Binary operators 3642 case ISD::ADD: return "add"; 3643 case ISD::SUB: return "sub"; 3644 case ISD::MUL: return "mul"; 3645 case ISD::MULHU: return "mulhu"; 3646 case ISD::MULHS: return "mulhs"; 3647 case ISD::SDIV: return "sdiv"; 3648 case ISD::UDIV: return "udiv"; 3649 case ISD::SREM: return "srem"; 3650 case ISD::UREM: return "urem"; 3651 case ISD::SMUL_LOHI: return "smul_lohi"; 3652 case ISD::UMUL_LOHI: return "umul_lohi"; 3653 case ISD::SDIVREM: return "sdivrem"; 3654 case ISD::UDIVREM: return "divrem"; 3655 case ISD::AND: return "and"; 3656 case ISD::OR: return "or"; 3657 case ISD::XOR: return "xor"; 3658 case ISD::SHL: return "shl"; 3659 case ISD::SRA: return "sra"; 3660 case ISD::SRL: return "srl"; 3661 case ISD::ROTL: return "rotl"; 3662 case ISD::ROTR: return "rotr"; 3663 case ISD::FADD: return "fadd"; 3664 case ISD::FSUB: return "fsub"; 3665 case ISD::FMUL: return "fmul"; 3666 case ISD::FDIV: return "fdiv"; 3667 case ISD::FREM: return "frem"; 3668 case ISD::FCOPYSIGN: return "fcopysign"; 3669 3670 case ISD::SETCC: return "setcc"; 3671 case ISD::SELECT: return "select"; 3672 case ISD::SELECT_CC: return "select_cc"; 3673 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt"; 3674 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt"; 3675 case ISD::CONCAT_VECTORS: return "concat_vectors"; 3676 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector"; 3677 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector"; 3678 case ISD::VECTOR_SHUFFLE: return "vector_shuffle"; 3679 case ISD::CARRY_FALSE: return "carry_false"; 3680 case ISD::ADDC: return "addc"; 3681 case ISD::ADDE: return "adde"; 3682 case ISD::SUBC: return "subc"; 3683 case ISD::SUBE: return "sube"; 3684 case ISD::SHL_PARTS: return "shl_parts"; 3685 case ISD::SRA_PARTS: return "sra_parts"; 3686 case ISD::SRL_PARTS: return "srl_parts"; 3687 3688 case ISD::EXTRACT_SUBREG: return "extract_subreg"; 3689 case ISD::INSERT_SUBREG: return "insert_subreg"; 3690 3691 // Conversion operators. 3692 case ISD::SIGN_EXTEND: return "sign_extend"; 3693 case ISD::ZERO_EXTEND: return "zero_extend"; 3694 case ISD::ANY_EXTEND: return "any_extend"; 3695 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 3696 case ISD::TRUNCATE: return "truncate"; 3697 case ISD::FP_ROUND: return "fp_round"; 3698 case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 3699 case ISD::FP_EXTEND: return "fp_extend"; 3700 3701 case ISD::SINT_TO_FP: return "sint_to_fp"; 3702 case ISD::UINT_TO_FP: return "uint_to_fp"; 3703 case ISD::FP_TO_SINT: return "fp_to_sint"; 3704 case ISD::FP_TO_UINT: return "fp_to_uint"; 3705 case ISD::BIT_CONVERT: return "bit_convert"; 3706 3707 // Control flow instructions 3708 case ISD::BR: return "br"; 3709 case ISD::BRIND: return "brind"; 3710 case ISD::BR_JT: return "br_jt"; 3711 case ISD::BRCOND: return "brcond"; 3712 case ISD::BR_CC: return "br_cc"; 3713 case ISD::RET: return "ret"; 3714 case ISD::CALLSEQ_START: return "callseq_start"; 3715 case ISD::CALLSEQ_END: return "callseq_end"; 3716 3717 // Other operators 3718 case ISD::LOAD: return "load"; 3719 case ISD::STORE: return "store"; 3720 case ISD::VAARG: return "vaarg"; 3721 case ISD::VACOPY: return "vacopy"; 3722 case ISD::VAEND: return "vaend"; 3723 case ISD::VASTART: return "vastart"; 3724 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 3725 case ISD::EXTRACT_ELEMENT: return "extract_element"; 3726 case ISD::BUILD_PAIR: return "build_pair"; 3727 case ISD::STACKSAVE: return "stacksave"; 3728 case ISD::STACKRESTORE: return "stackrestore"; 3729 3730 // Block memory operations. 3731 case ISD::MEMSET: return "memset"; 3732 case ISD::MEMCPY: return "memcpy"; 3733 case ISD::MEMMOVE: return "memmove"; 3734 3735 // Bit manipulation 3736 case ISD::BSWAP: return "bswap"; 3737 case ISD::CTPOP: return "ctpop"; 3738 case ISD::CTTZ: return "cttz"; 3739 case ISD::CTLZ: return "ctlz"; 3740 3741 // Debug info 3742 case ISD::LOCATION: return "location"; 3743 case ISD::DEBUG_LOC: return "debug_loc"; 3744 3745 // Trampolines 3746 case ISD::TRAMPOLINE: return "trampoline"; 3747 3748 case ISD::CONDCODE: 3749 switch (cast<CondCodeSDNode>(this)->get()) { 3750 default: assert(0 && "Unknown setcc condition!"); 3751 case ISD::SETOEQ: return "setoeq"; 3752 case ISD::SETOGT: return "setogt"; 3753 case ISD::SETOGE: return "setoge"; 3754 case ISD::SETOLT: return "setolt"; 3755 case ISD::SETOLE: return "setole"; 3756 case ISD::SETONE: return "setone"; 3757 3758 case ISD::SETO: return "seto"; 3759 case ISD::SETUO: return "setuo"; 3760 case ISD::SETUEQ: return "setue"; 3761 case ISD::SETUGT: return "setugt"; 3762 case ISD::SETUGE: return "setuge"; 3763 case ISD::SETULT: return "setult"; 3764 case ISD::SETULE: return "setule"; 3765 case ISD::SETUNE: return "setune"; 3766 3767 case ISD::SETEQ: return "seteq"; 3768 case ISD::SETGT: return "setgt"; 3769 case ISD::SETGE: return "setge"; 3770 case ISD::SETLT: return "setlt"; 3771 case ISD::SETLE: return "setle"; 3772 case ISD::SETNE: return "setne"; 3773 } 3774 } 3775} 3776 3777const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) { 3778 switch (AM) { 3779 default: 3780 return ""; 3781 case ISD::PRE_INC: 3782 return "<pre-inc>"; 3783 case ISD::PRE_DEC: 3784 return "<pre-dec>"; 3785 case ISD::POST_INC: 3786 return "<post-inc>"; 3787 case ISD::POST_DEC: 3788 return "<post-dec>"; 3789 } 3790} 3791 3792void SDNode::dump() const { dump(0); } 3793void SDNode::dump(const SelectionDAG *G) const { 3794 cerr << (void*)this << ": "; 3795 3796 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 3797 if (i) cerr << ","; 3798 if (getValueType(i) == MVT::Other) 3799 cerr << "ch"; 3800 else 3801 cerr << MVT::getValueTypeString(getValueType(i)); 3802 } 3803 cerr << " = " << getOperationName(G); 3804 3805 cerr << " "; 3806 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 3807 if (i) cerr << ", "; 3808 cerr << (void*)getOperand(i).Val; 3809 if (unsigned RN = getOperand(i).ResNo) 3810 cerr << ":" << RN; 3811 } 3812 3813 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 3814 cerr << "<" << CSDN->getValue() << ">"; 3815 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 3816 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle) 3817 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">"; 3818 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble) 3819 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">"; 3820 else { 3821 cerr << "<APFloat("; 3822 CSDN->getValueAPF().convertToAPInt().dump(); 3823 cerr << ")>"; 3824 } 3825 } else if (const GlobalAddressSDNode *GADN = 3826 dyn_cast<GlobalAddressSDNode>(this)) { 3827 int offset = GADN->getOffset(); 3828 cerr << "<"; 3829 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">"; 3830 if (offset > 0) 3831 cerr << " + " << offset; 3832 else 3833 cerr << " " << offset; 3834 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 3835 cerr << "<" << FIDN->getIndex() << ">"; 3836 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) { 3837 cerr << "<" << JTDN->getIndex() << ">"; 3838 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 3839 int offset = CP->getOffset(); 3840 if (CP->isMachineConstantPoolEntry()) 3841 cerr << "<" << *CP->getMachineCPVal() << ">"; 3842 else 3843 cerr << "<" << *CP->getConstVal() << ">"; 3844 if (offset > 0) 3845 cerr << " + " << offset; 3846 else 3847 cerr << " " << offset; 3848 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 3849 cerr << "<"; 3850 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 3851 if (LBB) 3852 cerr << LBB->getName() << " "; 3853 cerr << (const void*)BBDN->getBasicBlock() << ">"; 3854 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 3855 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) { 3856 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); 3857 } else { 3858 cerr << " #" << R->getReg(); 3859 } 3860 } else if (const ExternalSymbolSDNode *ES = 3861 dyn_cast<ExternalSymbolSDNode>(this)) { 3862 cerr << "'" << ES->getSymbol() << "'"; 3863 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 3864 if (M->getValue()) 3865 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; 3866 else 3867 cerr << "<null:" << M->getOffset() << ">"; 3868 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 3869 cerr << ":" << MVT::getValueTypeString(N->getVT()); 3870 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) { 3871 bool doExt = true; 3872 switch (LD->getExtensionType()) { 3873 default: doExt = false; break; 3874 case ISD::EXTLOAD: 3875 cerr << " <anyext "; 3876 break; 3877 case ISD::SEXTLOAD: 3878 cerr << " <sext "; 3879 break; 3880 case ISD::ZEXTLOAD: 3881 cerr << " <zext "; 3882 break; 3883 } 3884 if (doExt) 3885 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">"; 3886 3887 const char *AM = getIndexedModeName(LD->getAddressingMode()); 3888 if (*AM) 3889 cerr << " " << AM; 3890 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) { 3891 if (ST->isTruncatingStore()) 3892 cerr << " <trunc " 3893 << MVT::getValueTypeString(ST->getStoredVT()) << ">"; 3894 3895 const char *AM = getIndexedModeName(ST->getAddressingMode()); 3896 if (*AM) 3897 cerr << " " << AM; 3898 } 3899} 3900 3901static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 3902 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 3903 if (N->getOperand(i).Val->hasOneUse()) 3904 DumpNodes(N->getOperand(i).Val, indent+2, G); 3905 else 3906 cerr << "\n" << std::string(indent+2, ' ') 3907 << (void*)N->getOperand(i).Val << ": <multiple use>"; 3908 3909 3910 cerr << "\n" << std::string(indent, ' '); 3911 N->dump(G); 3912} 3913 3914void SelectionDAG::dump() const { 3915 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; 3916 std::vector<const SDNode*> Nodes; 3917 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 3918 I != E; ++I) 3919 Nodes.push_back(I); 3920 3921 std::sort(Nodes.begin(), Nodes.end()); 3922 3923 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 3924 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) 3925 DumpNodes(Nodes[i], 2, this); 3926 } 3927 3928 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this); 3929 3930 cerr << "\n\n"; 3931} 3932 3933const Type *ConstantPoolSDNode::getType() const { 3934 if (isMachineConstantPoolEntry()) 3935 return Val.MachineCPVal->getType(); 3936 return Val.ConstVal->getType(); 3937} 3938