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