SelectionDAG.cpp revision 204961
1193323Sed//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This implements the SelectionDAG class. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13201360Srdivacky 14193323Sed#include "llvm/CodeGen/SelectionDAG.h" 15201360Srdivacky#include "SDNodeOrdering.h" 16193323Sed#include "llvm/Constants.h" 17193323Sed#include "llvm/Analysis/ValueTracking.h" 18198090Srdivacky#include "llvm/Function.h" 19193323Sed#include "llvm/GlobalAlias.h" 20193323Sed#include "llvm/GlobalVariable.h" 21193323Sed#include "llvm/Intrinsics.h" 22193323Sed#include "llvm/DerivedTypes.h" 23193323Sed#include "llvm/Assembly/Writer.h" 24193323Sed#include "llvm/CallingConv.h" 25193323Sed#include "llvm/CodeGen/MachineBasicBlock.h" 26193323Sed#include "llvm/CodeGen/MachineConstantPool.h" 27193323Sed#include "llvm/CodeGen/MachineFrameInfo.h" 28193323Sed#include "llvm/CodeGen/MachineModuleInfo.h" 29193323Sed#include "llvm/CodeGen/PseudoSourceValue.h" 30193323Sed#include "llvm/Target/TargetRegisterInfo.h" 31193323Sed#include "llvm/Target/TargetData.h" 32200581Srdivacky#include "llvm/Target/TargetFrameInfo.h" 33193323Sed#include "llvm/Target/TargetLowering.h" 34193323Sed#include "llvm/Target/TargetOptions.h" 35193323Sed#include "llvm/Target/TargetInstrInfo.h" 36198396Srdivacky#include "llvm/Target/TargetIntrinsicInfo.h" 37193323Sed#include "llvm/Target/TargetMachine.h" 38193323Sed#include "llvm/Support/CommandLine.h" 39202375Srdivacky#include "llvm/Support/Debug.h" 40198090Srdivacky#include "llvm/Support/ErrorHandling.h" 41195098Sed#include "llvm/Support/ManagedStatic.h" 42193323Sed#include "llvm/Support/MathExtras.h" 43193323Sed#include "llvm/Support/raw_ostream.h" 44195098Sed#include "llvm/System/Mutex.h" 45193323Sed#include "llvm/ADT/SetVector.h" 46193323Sed#include "llvm/ADT/SmallPtrSet.h" 47193323Sed#include "llvm/ADT/SmallSet.h" 48193323Sed#include "llvm/ADT/SmallVector.h" 49193323Sed#include "llvm/ADT/StringExtras.h" 50193323Sed#include <algorithm> 51193323Sed#include <cmath> 52193323Sedusing namespace llvm; 53193323Sed 54193323Sed/// makeVTList - Return an instance of the SDVTList struct initialized with the 55193323Sed/// specified members. 56198090Srdivackystatic SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) { 57193323Sed SDVTList Res = {VTs, NumVTs}; 58193323Sed return Res; 59193323Sed} 60193323Sed 61198090Srdivackystatic const fltSemantics *EVTToAPFloatSemantics(EVT VT) { 62198090Srdivacky switch (VT.getSimpleVT().SimpleTy) { 63198090Srdivacky default: llvm_unreachable("Unknown FP format"); 64193323Sed case MVT::f32: return &APFloat::IEEEsingle; 65193323Sed case MVT::f64: return &APFloat::IEEEdouble; 66193323Sed case MVT::f80: return &APFloat::x87DoubleExtended; 67193323Sed case MVT::f128: return &APFloat::IEEEquad; 68193323Sed case MVT::ppcf128: return &APFloat::PPCDoubleDouble; 69193323Sed } 70193323Sed} 71193323Sed 72193323SedSelectionDAG::DAGUpdateListener::~DAGUpdateListener() {} 73193323Sed 74193323Sed//===----------------------------------------------------------------------===// 75193323Sed// ConstantFPSDNode Class 76193323Sed//===----------------------------------------------------------------------===// 77193323Sed 78193323Sed/// isExactlyValue - We don't rely on operator== working on double values, as 79193323Sed/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 80193323Sed/// As such, this method can be used to do an exact bit-for-bit comparison of 81193323Sed/// two floating point values. 82193323Sedbool ConstantFPSDNode::isExactlyValue(const APFloat& V) const { 83193323Sed return getValueAPF().bitwiseIsEqual(V); 84193323Sed} 85193323Sed 86198090Srdivackybool ConstantFPSDNode::isValueValidForType(EVT VT, 87193323Sed const APFloat& Val) { 88193323Sed assert(VT.isFloatingPoint() && "Can only convert between FP types"); 89193323Sed 90193323Sed // PPC long double cannot be converted to any other type. 91193323Sed if (VT == MVT::ppcf128 || 92193323Sed &Val.getSemantics() == &APFloat::PPCDoubleDouble) 93193323Sed return false; 94193323Sed 95193323Sed // convert modifies in place, so make a copy. 96193323Sed APFloat Val2 = APFloat(Val); 97193323Sed bool losesInfo; 98198090Srdivacky (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven, 99193323Sed &losesInfo); 100193323Sed return !losesInfo; 101193323Sed} 102193323Sed 103193323Sed//===----------------------------------------------------------------------===// 104193323Sed// ISD Namespace 105193323Sed//===----------------------------------------------------------------------===// 106193323Sed 107193323Sed/// isBuildVectorAllOnes - Return true if the specified node is a 108193323Sed/// BUILD_VECTOR where all of the elements are ~0 or undef. 109193323Sedbool ISD::isBuildVectorAllOnes(const SDNode *N) { 110193323Sed // Look through a bit convert. 111193323Sed if (N->getOpcode() == ISD::BIT_CONVERT) 112193323Sed N = N->getOperand(0).getNode(); 113193323Sed 114193323Sed if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 115193323Sed 116193323Sed unsigned i = 0, e = N->getNumOperands(); 117193323Sed 118193323Sed // Skip over all of the undef values. 119193323Sed while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 120193323Sed ++i; 121193323Sed 122193323Sed // Do not accept an all-undef vector. 123193323Sed if (i == e) return false; 124193323Sed 125193323Sed // Do not accept build_vectors that aren't all constants or which have non-~0 126193323Sed // elements. 127193323Sed SDValue NotZero = N->getOperand(i); 128193323Sed if (isa<ConstantSDNode>(NotZero)) { 129193323Sed if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue()) 130193323Sed return false; 131193323Sed } else if (isa<ConstantFPSDNode>(NotZero)) { 132193323Sed if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF(). 133193323Sed bitcastToAPInt().isAllOnesValue()) 134193323Sed return false; 135193323Sed } else 136193323Sed return false; 137193323Sed 138193323Sed // Okay, we have at least one ~0 value, check to see if the rest match or are 139193323Sed // undefs. 140193323Sed for (++i; i != e; ++i) 141193323Sed if (N->getOperand(i) != NotZero && 142193323Sed N->getOperand(i).getOpcode() != ISD::UNDEF) 143193323Sed return false; 144193323Sed return true; 145193323Sed} 146193323Sed 147193323Sed 148193323Sed/// isBuildVectorAllZeros - Return true if the specified node is a 149193323Sed/// BUILD_VECTOR where all of the elements are 0 or undef. 150193323Sedbool ISD::isBuildVectorAllZeros(const SDNode *N) { 151193323Sed // Look through a bit convert. 152193323Sed if (N->getOpcode() == ISD::BIT_CONVERT) 153193323Sed N = N->getOperand(0).getNode(); 154193323Sed 155193323Sed if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 156193323Sed 157193323Sed unsigned i = 0, e = N->getNumOperands(); 158193323Sed 159193323Sed // Skip over all of the undef values. 160193323Sed while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 161193323Sed ++i; 162193323Sed 163193323Sed // Do not accept an all-undef vector. 164193323Sed if (i == e) return false; 165193323Sed 166193574Sed // Do not accept build_vectors that aren't all constants or which have non-0 167193323Sed // elements. 168193323Sed SDValue Zero = N->getOperand(i); 169193323Sed if (isa<ConstantSDNode>(Zero)) { 170193323Sed if (!cast<ConstantSDNode>(Zero)->isNullValue()) 171193323Sed return false; 172193323Sed } else if (isa<ConstantFPSDNode>(Zero)) { 173193323Sed if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero()) 174193323Sed return false; 175193323Sed } else 176193323Sed return false; 177193323Sed 178193574Sed // Okay, we have at least one 0 value, check to see if the rest match or are 179193323Sed // undefs. 180193323Sed for (++i; i != e; ++i) 181193323Sed if (N->getOperand(i) != Zero && 182193323Sed N->getOperand(i).getOpcode() != ISD::UNDEF) 183193323Sed return false; 184193323Sed return true; 185193323Sed} 186193323Sed 187193323Sed/// isScalarToVector - Return true if the specified node is a 188193323Sed/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low 189193323Sed/// element is not an undef. 190193323Sedbool ISD::isScalarToVector(const SDNode *N) { 191193323Sed if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) 192193323Sed return true; 193193323Sed 194193323Sed if (N->getOpcode() != ISD::BUILD_VECTOR) 195193323Sed return false; 196193323Sed if (N->getOperand(0).getOpcode() == ISD::UNDEF) 197193323Sed return false; 198193323Sed unsigned NumElems = N->getNumOperands(); 199193323Sed for (unsigned i = 1; i < NumElems; ++i) { 200193323Sed SDValue V = N->getOperand(i); 201193323Sed if (V.getOpcode() != ISD::UNDEF) 202193323Sed return false; 203193323Sed } 204193323Sed return true; 205193323Sed} 206193323Sed 207193323Sed/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 208193323Sed/// when given the operation for (X op Y). 209193323SedISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 210193323Sed // To perform this operation, we just need to swap the L and G bits of the 211193323Sed // operation. 212193323Sed unsigned OldL = (Operation >> 2) & 1; 213193323Sed unsigned OldG = (Operation >> 1) & 1; 214193323Sed return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 215193323Sed (OldL << 1) | // New G bit 216193323Sed (OldG << 2)); // New L bit. 217193323Sed} 218193323Sed 219193323Sed/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 220193323Sed/// 'op' is a valid SetCC operation. 221193323SedISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 222193323Sed unsigned Operation = Op; 223193323Sed if (isInteger) 224193323Sed Operation ^= 7; // Flip L, G, E bits, but not U. 225193323Sed else 226193323Sed Operation ^= 15; // Flip all of the condition bits. 227193323Sed 228193323Sed if (Operation > ISD::SETTRUE2) 229193323Sed Operation &= ~8; // Don't let N and U bits get set. 230193323Sed 231193323Sed return ISD::CondCode(Operation); 232193323Sed} 233193323Sed 234193323Sed 235193323Sed/// isSignedOp - For an integer comparison, return 1 if the comparison is a 236193323Sed/// signed operation and 2 if the result is an unsigned comparison. Return zero 237193323Sed/// if the operation does not depend on the sign of the input (setne and seteq). 238193323Sedstatic int isSignedOp(ISD::CondCode Opcode) { 239193323Sed switch (Opcode) { 240198090Srdivacky default: llvm_unreachable("Illegal integer setcc operation!"); 241193323Sed case ISD::SETEQ: 242193323Sed case ISD::SETNE: return 0; 243193323Sed case ISD::SETLT: 244193323Sed case ISD::SETLE: 245193323Sed case ISD::SETGT: 246193323Sed case ISD::SETGE: return 1; 247193323Sed case ISD::SETULT: 248193323Sed case ISD::SETULE: 249193323Sed case ISD::SETUGT: 250193323Sed case ISD::SETUGE: return 2; 251193323Sed } 252193323Sed} 253193323Sed 254193323Sed/// getSetCCOrOperation - Return the result of a logical OR between different 255193323Sed/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 256193323Sed/// returns SETCC_INVALID if it is not possible to represent the resultant 257193323Sed/// comparison. 258193323SedISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 259193323Sed bool isInteger) { 260193323Sed if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 261193323Sed // Cannot fold a signed integer setcc with an unsigned integer setcc. 262193323Sed return ISD::SETCC_INVALID; 263193323Sed 264193323Sed unsigned Op = Op1 | Op2; // Combine all of the condition bits. 265193323Sed 266193323Sed // If the N and U bits get set then the resultant comparison DOES suddenly 267193323Sed // care about orderedness, and is true when ordered. 268193323Sed if (Op > ISD::SETTRUE2) 269193323Sed Op &= ~16; // Clear the U bit if the N bit is set. 270193323Sed 271193323Sed // Canonicalize illegal integer setcc's. 272193323Sed if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT 273193323Sed Op = ISD::SETNE; 274193323Sed 275193323Sed return ISD::CondCode(Op); 276193323Sed} 277193323Sed 278193323Sed/// getSetCCAndOperation - Return the result of a logical AND between different 279193323Sed/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 280193323Sed/// function returns zero if it is not possible to represent the resultant 281193323Sed/// comparison. 282193323SedISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 283193323Sed bool isInteger) { 284193323Sed if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 285193323Sed // Cannot fold a signed setcc with an unsigned setcc. 286193323Sed return ISD::SETCC_INVALID; 287193323Sed 288193323Sed // Combine all of the condition bits. 289193323Sed ISD::CondCode Result = ISD::CondCode(Op1 & Op2); 290193323Sed 291193323Sed // Canonicalize illegal integer setcc's. 292193323Sed if (isInteger) { 293193323Sed switch (Result) { 294193323Sed default: break; 295193323Sed case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT 296193323Sed case ISD::SETOEQ: // SETEQ & SETU[LG]E 297193323Sed case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE 298193323Sed case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE 299193323Sed case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE 300193323Sed } 301193323Sed } 302193323Sed 303193323Sed return Result; 304193323Sed} 305193323Sed 306193323Sedconst TargetMachine &SelectionDAG::getTarget() const { 307193323Sed return MF->getTarget(); 308193323Sed} 309193323Sed 310193323Sed//===----------------------------------------------------------------------===// 311193323Sed// SDNode Profile Support 312193323Sed//===----------------------------------------------------------------------===// 313193323Sed 314193323Sed/// AddNodeIDOpcode - Add the node opcode to the NodeID data. 315193323Sed/// 316193323Sedstatic void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) { 317193323Sed ID.AddInteger(OpC); 318193323Sed} 319193323Sed 320193323Sed/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them 321193323Sed/// solely with their pointer. 322193323Sedstatic void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) { 323193323Sed ID.AddPointer(VTList.VTs); 324193323Sed} 325193323Sed 326193323Sed/// AddNodeIDOperands - Various routines for adding operands to the NodeID data. 327193323Sed/// 328193323Sedstatic void AddNodeIDOperands(FoldingSetNodeID &ID, 329193323Sed const SDValue *Ops, unsigned NumOps) { 330193323Sed for (; NumOps; --NumOps, ++Ops) { 331193323Sed ID.AddPointer(Ops->getNode()); 332193323Sed ID.AddInteger(Ops->getResNo()); 333193323Sed } 334193323Sed} 335193323Sed 336193323Sed/// AddNodeIDOperands - Various routines for adding operands to the NodeID data. 337193323Sed/// 338193323Sedstatic void AddNodeIDOperands(FoldingSetNodeID &ID, 339193323Sed const SDUse *Ops, unsigned NumOps) { 340193323Sed for (; NumOps; --NumOps, ++Ops) { 341193323Sed ID.AddPointer(Ops->getNode()); 342193323Sed ID.AddInteger(Ops->getResNo()); 343193323Sed } 344193323Sed} 345193323Sed 346193323Sedstatic void AddNodeIDNode(FoldingSetNodeID &ID, 347193323Sed unsigned short OpC, SDVTList VTList, 348193323Sed const SDValue *OpList, unsigned N) { 349193323Sed AddNodeIDOpcode(ID, OpC); 350193323Sed AddNodeIDValueTypes(ID, VTList); 351193323Sed AddNodeIDOperands(ID, OpList, N); 352193323Sed} 353193323Sed 354193323Sed/// AddNodeIDCustom - If this is an SDNode with special info, add this info to 355193323Sed/// the NodeID data. 356193323Sedstatic void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) { 357193323Sed switch (N->getOpcode()) { 358195098Sed case ISD::TargetExternalSymbol: 359195098Sed case ISD::ExternalSymbol: 360198090Srdivacky llvm_unreachable("Should only be used on nodes with operands"); 361193323Sed default: break; // Normal nodes don't need extra info. 362193323Sed case ISD::TargetConstant: 363193323Sed case ISD::Constant: 364193323Sed ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue()); 365193323Sed break; 366193323Sed case ISD::TargetConstantFP: 367193323Sed case ISD::ConstantFP: { 368193323Sed ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue()); 369193323Sed break; 370193323Sed } 371193323Sed case ISD::TargetGlobalAddress: 372193323Sed case ISD::GlobalAddress: 373193323Sed case ISD::TargetGlobalTLSAddress: 374193323Sed case ISD::GlobalTLSAddress: { 375193323Sed const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); 376193323Sed ID.AddPointer(GA->getGlobal()); 377193323Sed ID.AddInteger(GA->getOffset()); 378195098Sed ID.AddInteger(GA->getTargetFlags()); 379193323Sed break; 380193323Sed } 381193323Sed case ISD::BasicBlock: 382193323Sed ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock()); 383193323Sed break; 384193323Sed case ISD::Register: 385193323Sed ID.AddInteger(cast<RegisterSDNode>(N)->getReg()); 386193323Sed break; 387199989Srdivacky 388193323Sed case ISD::SRCVALUE: 389193323Sed ID.AddPointer(cast<SrcValueSDNode>(N)->getValue()); 390193323Sed break; 391193323Sed case ISD::FrameIndex: 392193323Sed case ISD::TargetFrameIndex: 393193323Sed ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex()); 394193323Sed break; 395193323Sed case ISD::JumpTable: 396193323Sed case ISD::TargetJumpTable: 397193323Sed ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex()); 398195098Sed ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags()); 399193323Sed break; 400193323Sed case ISD::ConstantPool: 401193323Sed case ISD::TargetConstantPool: { 402193323Sed const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N); 403193323Sed ID.AddInteger(CP->getAlignment()); 404193323Sed ID.AddInteger(CP->getOffset()); 405193323Sed if (CP->isMachineConstantPoolEntry()) 406193323Sed CP->getMachineCPVal()->AddSelectionDAGCSEId(ID); 407193323Sed else 408193323Sed ID.AddPointer(CP->getConstVal()); 409195098Sed ID.AddInteger(CP->getTargetFlags()); 410193323Sed break; 411193323Sed } 412193323Sed case ISD::LOAD: { 413193323Sed const LoadSDNode *LD = cast<LoadSDNode>(N); 414193323Sed ID.AddInteger(LD->getMemoryVT().getRawBits()); 415193323Sed ID.AddInteger(LD->getRawSubclassData()); 416193323Sed break; 417193323Sed } 418193323Sed case ISD::STORE: { 419193323Sed const StoreSDNode *ST = cast<StoreSDNode>(N); 420193323Sed ID.AddInteger(ST->getMemoryVT().getRawBits()); 421193323Sed ID.AddInteger(ST->getRawSubclassData()); 422193323Sed break; 423193323Sed } 424193323Sed case ISD::ATOMIC_CMP_SWAP: 425193323Sed case ISD::ATOMIC_SWAP: 426193323Sed case ISD::ATOMIC_LOAD_ADD: 427193323Sed case ISD::ATOMIC_LOAD_SUB: 428193323Sed case ISD::ATOMIC_LOAD_AND: 429193323Sed case ISD::ATOMIC_LOAD_OR: 430193323Sed case ISD::ATOMIC_LOAD_XOR: 431193323Sed case ISD::ATOMIC_LOAD_NAND: 432193323Sed case ISD::ATOMIC_LOAD_MIN: 433193323Sed case ISD::ATOMIC_LOAD_MAX: 434193323Sed case ISD::ATOMIC_LOAD_UMIN: 435193323Sed case ISD::ATOMIC_LOAD_UMAX: { 436193323Sed const AtomicSDNode *AT = cast<AtomicSDNode>(N); 437193323Sed ID.AddInteger(AT->getMemoryVT().getRawBits()); 438193323Sed ID.AddInteger(AT->getRawSubclassData()); 439193323Sed break; 440193323Sed } 441193323Sed case ISD::VECTOR_SHUFFLE: { 442193323Sed const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 443198090Srdivacky for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements(); 444193323Sed i != e; ++i) 445193323Sed ID.AddInteger(SVN->getMaskElt(i)); 446193323Sed break; 447193323Sed } 448198892Srdivacky case ISD::TargetBlockAddress: 449198892Srdivacky case ISD::BlockAddress: { 450199989Srdivacky ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress()); 451199989Srdivacky ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags()); 452198892Srdivacky break; 453198892Srdivacky } 454193323Sed } // end switch (N->getOpcode()) 455193323Sed} 456193323Sed 457193323Sed/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID 458193323Sed/// data. 459193323Sedstatic void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) { 460193323Sed AddNodeIDOpcode(ID, N->getOpcode()); 461193323Sed // Add the return value info. 462193323Sed AddNodeIDValueTypes(ID, N->getVTList()); 463193323Sed // Add the operand info. 464193323Sed AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands()); 465193323Sed 466193323Sed // Handle SDNode leafs with special info. 467193323Sed AddNodeIDCustom(ID, N); 468193323Sed} 469193323Sed 470193323Sed/// encodeMemSDNodeFlags - Generic routine for computing a value for use in 471204642Srdivacky/// the CSE map that carries volatility, temporalness, indexing mode, and 472193323Sed/// extension/truncation information. 473193323Sed/// 474193323Sedstatic inline unsigned 475204642SrdivackyencodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile, 476204642Srdivacky bool isNonTemporal) { 477193323Sed assert((ConvType & 3) == ConvType && 478193323Sed "ConvType may not require more than 2 bits!"); 479193323Sed assert((AM & 7) == AM && 480193323Sed "AM may not require more than 3 bits!"); 481193323Sed return ConvType | 482193323Sed (AM << 2) | 483204642Srdivacky (isVolatile << 5) | 484204642Srdivacky (isNonTemporal << 6); 485193323Sed} 486193323Sed 487193323Sed//===----------------------------------------------------------------------===// 488193323Sed// SelectionDAG Class 489193323Sed//===----------------------------------------------------------------------===// 490193323Sed 491193323Sed/// doNotCSE - Return true if CSE should not be performed for this node. 492193323Sedstatic bool doNotCSE(SDNode *N) { 493193323Sed if (N->getValueType(0) == MVT::Flag) 494193323Sed return true; // Never CSE anything that produces a flag. 495193323Sed 496193323Sed switch (N->getOpcode()) { 497193323Sed default: break; 498193323Sed case ISD::HANDLENODE: 499193323Sed case ISD::EH_LABEL: 500193323Sed return true; // Never CSE these nodes. 501193323Sed } 502193323Sed 503193323Sed // Check that remaining values produced are not flags. 504193323Sed for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 505193323Sed if (N->getValueType(i) == MVT::Flag) 506193323Sed return true; // Never CSE anything that produces a flag. 507193323Sed 508193323Sed return false; 509193323Sed} 510193323Sed 511193323Sed/// RemoveDeadNodes - This method deletes all unreachable nodes in the 512193323Sed/// SelectionDAG. 513193323Sedvoid SelectionDAG::RemoveDeadNodes() { 514193323Sed // Create a dummy node (which is not added to allnodes), that adds a reference 515193323Sed // to the root node, preventing it from being deleted. 516193323Sed HandleSDNode Dummy(getRoot()); 517193323Sed 518193323Sed SmallVector<SDNode*, 128> DeadNodes; 519193323Sed 520193323Sed // Add all obviously-dead nodes to the DeadNodes worklist. 521193323Sed for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 522193323Sed if (I->use_empty()) 523193323Sed DeadNodes.push_back(I); 524193323Sed 525193323Sed RemoveDeadNodes(DeadNodes); 526193323Sed 527193323Sed // If the root changed (e.g. it was a dead load, update the root). 528193323Sed setRoot(Dummy.getValue()); 529193323Sed} 530193323Sed 531193323Sed/// RemoveDeadNodes - This method deletes the unreachable nodes in the 532193323Sed/// given list, and any nodes that become unreachable as a result. 533193323Sedvoid SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes, 534193323Sed DAGUpdateListener *UpdateListener) { 535193323Sed 536193323Sed // Process the worklist, deleting the nodes and adding their uses to the 537193323Sed // worklist. 538193323Sed while (!DeadNodes.empty()) { 539193323Sed SDNode *N = DeadNodes.pop_back_val(); 540193323Sed 541193323Sed if (UpdateListener) 542193323Sed UpdateListener->NodeDeleted(N, 0); 543193323Sed 544193323Sed // Take the node out of the appropriate CSE map. 545193323Sed RemoveNodeFromCSEMaps(N); 546193323Sed 547193323Sed // Next, brutally remove the operand list. This is safe to do, as there are 548193323Sed // no cycles in the graph. 549193323Sed for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { 550193323Sed SDUse &Use = *I++; 551193323Sed SDNode *Operand = Use.getNode(); 552193323Sed Use.set(SDValue()); 553193323Sed 554193323Sed // Now that we removed this operand, see if there are no uses of it left. 555193323Sed if (Operand->use_empty()) 556193323Sed DeadNodes.push_back(Operand); 557193323Sed } 558193323Sed 559193323Sed DeallocateNode(N); 560193323Sed } 561193323Sed} 562193323Sed 563193323Sedvoid SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){ 564193323Sed SmallVector<SDNode*, 16> DeadNodes(1, N); 565193323Sed RemoveDeadNodes(DeadNodes, UpdateListener); 566193323Sed} 567193323Sed 568193323Sedvoid SelectionDAG::DeleteNode(SDNode *N) { 569193323Sed // First take this out of the appropriate CSE map. 570193323Sed RemoveNodeFromCSEMaps(N); 571193323Sed 572193323Sed // Finally, remove uses due to operands of this node, remove from the 573193323Sed // AllNodes list, and delete the node. 574193323Sed DeleteNodeNotInCSEMaps(N); 575193323Sed} 576193323Sed 577193323Sedvoid SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 578193323Sed assert(N != AllNodes.begin() && "Cannot delete the entry node!"); 579193323Sed assert(N->use_empty() && "Cannot delete a node that is not dead!"); 580193323Sed 581193323Sed // Drop all of the operands and decrement used node's use counts. 582193323Sed N->DropOperands(); 583193323Sed 584193323Sed DeallocateNode(N); 585193323Sed} 586193323Sed 587193323Sedvoid SelectionDAG::DeallocateNode(SDNode *N) { 588193323Sed if (N->OperandsNeedDelete) 589193323Sed delete[] N->OperandList; 590193323Sed 591193323Sed // Set the opcode to DELETED_NODE to help catch bugs when node 592193323Sed // memory is reallocated. 593193323Sed N->NodeType = ISD::DELETED_NODE; 594193323Sed 595193323Sed NodeAllocator.Deallocate(AllNodes.remove(N)); 596200581Srdivacky 597200581Srdivacky // Remove the ordering of this node. 598202878Srdivacky Ordering->remove(N); 599193323Sed} 600193323Sed 601193323Sed/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 602193323Sed/// correspond to it. This is useful when we're about to delete or repurpose 603193323Sed/// the node. We don't want future request for structurally identical nodes 604193323Sed/// to return N anymore. 605193323Sedbool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 606193323Sed bool Erased = false; 607193323Sed switch (N->getOpcode()) { 608193323Sed case ISD::EntryToken: 609198090Srdivacky llvm_unreachable("EntryToken should not be in CSEMaps!"); 610193323Sed return false; 611193323Sed case ISD::HANDLENODE: return false; // noop. 612193323Sed case ISD::CONDCODE: 613193323Sed assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 614193323Sed "Cond code doesn't exist!"); 615193323Sed Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 616193323Sed CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 617193323Sed break; 618193323Sed case ISD::ExternalSymbol: 619193323Sed Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 620193323Sed break; 621195098Sed case ISD::TargetExternalSymbol: { 622195098Sed ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N); 623195098Sed Erased = TargetExternalSymbols.erase( 624195098Sed std::pair<std::string,unsigned char>(ESN->getSymbol(), 625195098Sed ESN->getTargetFlags())); 626193323Sed break; 627195098Sed } 628193323Sed case ISD::VALUETYPE: { 629198090Srdivacky EVT VT = cast<VTSDNode>(N)->getVT(); 630193323Sed if (VT.isExtended()) { 631193323Sed Erased = ExtendedValueTypeNodes.erase(VT); 632193323Sed } else { 633198090Srdivacky Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0; 634198090Srdivacky ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0; 635193323Sed } 636193323Sed break; 637193323Sed } 638193323Sed default: 639193323Sed // Remove it from the CSE Map. 640193323Sed Erased = CSEMap.RemoveNode(N); 641193323Sed break; 642193323Sed } 643193323Sed#ifndef NDEBUG 644193323Sed // Verify that the node was actually in one of the CSE maps, unless it has a 645193323Sed // flag result (which cannot be CSE'd) or is one of the special cases that are 646193323Sed // not subject to CSE. 647193323Sed if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && 648193323Sed !N->isMachineOpcode() && !doNotCSE(N)) { 649193323Sed N->dump(this); 650202375Srdivacky dbgs() << "\n"; 651198090Srdivacky llvm_unreachable("Node is not in map!"); 652193323Sed } 653193323Sed#endif 654193323Sed return Erased; 655193323Sed} 656193323Sed 657193323Sed/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE 658193323Sed/// maps and modified in place. Add it back to the CSE maps, unless an identical 659193323Sed/// node already exists, in which case transfer all its users to the existing 660193323Sed/// node. This transfer can potentially trigger recursive merging. 661193323Sed/// 662193323Sedvoid 663193323SedSelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N, 664193323Sed DAGUpdateListener *UpdateListener) { 665193323Sed // For node types that aren't CSE'd, just act as if no identical node 666193323Sed // already exists. 667193323Sed if (!doNotCSE(N)) { 668193323Sed SDNode *Existing = CSEMap.GetOrInsertNode(N); 669193323Sed if (Existing != N) { 670193323Sed // If there was already an existing matching node, use ReplaceAllUsesWith 671193323Sed // to replace the dead one with the existing one. This can cause 672193323Sed // recursive merging of other unrelated nodes down the line. 673193323Sed ReplaceAllUsesWith(N, Existing, UpdateListener); 674193323Sed 675193323Sed // N is now dead. Inform the listener if it exists and delete it. 676193323Sed if (UpdateListener) 677193323Sed UpdateListener->NodeDeleted(N, Existing); 678193323Sed DeleteNodeNotInCSEMaps(N); 679193323Sed return; 680193323Sed } 681193323Sed } 682193323Sed 683193323Sed // If the node doesn't already exist, we updated it. Inform a listener if 684193323Sed // it exists. 685193323Sed if (UpdateListener) 686193323Sed UpdateListener->NodeUpdated(N); 687193323Sed} 688193323Sed 689193323Sed/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 690193323Sed/// were replaced with those specified. If this node is never memoized, 691193323Sed/// return null, otherwise return a pointer to the slot it would take. If a 692193323Sed/// node already exists with these operands, the slot will be non-null. 693193323SedSDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op, 694193323Sed void *&InsertPos) { 695193323Sed if (doNotCSE(N)) 696193323Sed return 0; 697193323Sed 698193323Sed SDValue Ops[] = { Op }; 699193323Sed FoldingSetNodeID ID; 700193323Sed AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1); 701193323Sed AddNodeIDCustom(ID, N); 702200581Srdivacky SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 703200581Srdivacky return Node; 704193323Sed} 705193323Sed 706193323Sed/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 707193323Sed/// were replaced with those specified. If this node is never memoized, 708193323Sed/// return null, otherwise return a pointer to the slot it would take. If a 709193323Sed/// node already exists with these operands, the slot will be non-null. 710193323SedSDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 711193323Sed SDValue Op1, SDValue Op2, 712193323Sed void *&InsertPos) { 713193323Sed if (doNotCSE(N)) 714193323Sed return 0; 715193323Sed 716193323Sed SDValue Ops[] = { Op1, Op2 }; 717193323Sed FoldingSetNodeID ID; 718193323Sed AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2); 719193323Sed AddNodeIDCustom(ID, N); 720200581Srdivacky SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 721200581Srdivacky return Node; 722193323Sed} 723193323Sed 724193323Sed 725193323Sed/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 726193323Sed/// were replaced with those specified. If this node is never memoized, 727193323Sed/// return null, otherwise return a pointer to the slot it would take. If a 728193323Sed/// node already exists with these operands, the slot will be non-null. 729193323SedSDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 730193323Sed const SDValue *Ops,unsigned NumOps, 731193323Sed void *&InsertPos) { 732193323Sed if (doNotCSE(N)) 733193323Sed return 0; 734193323Sed 735193323Sed FoldingSetNodeID ID; 736193323Sed AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps); 737193323Sed AddNodeIDCustom(ID, N); 738200581Srdivacky SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 739200581Srdivacky return Node; 740193323Sed} 741193323Sed 742193323Sed/// VerifyNode - Sanity check the given node. Aborts if it is invalid. 743193323Sedvoid SelectionDAG::VerifyNode(SDNode *N) { 744193323Sed switch (N->getOpcode()) { 745193323Sed default: 746193323Sed break; 747193323Sed case ISD::BUILD_PAIR: { 748198090Srdivacky EVT VT = N->getValueType(0); 749193323Sed assert(N->getNumValues() == 1 && "Too many results!"); 750193323Sed assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) && 751193323Sed "Wrong return type!"); 752193323Sed assert(N->getNumOperands() == 2 && "Wrong number of operands!"); 753193323Sed assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() && 754193323Sed "Mismatched operand types!"); 755193323Sed assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() && 756193323Sed "Wrong operand type!"); 757193323Sed assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() && 758193323Sed "Wrong return type size"); 759193323Sed break; 760193323Sed } 761193323Sed case ISD::BUILD_VECTOR: { 762193323Sed assert(N->getNumValues() == 1 && "Too many results!"); 763193323Sed assert(N->getValueType(0).isVector() && "Wrong return type!"); 764193323Sed assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() && 765193323Sed "Wrong number of operands!"); 766198090Srdivacky EVT EltVT = N->getValueType(0).getVectorElementType(); 767193323Sed for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 768193323Sed assert((I->getValueType() == EltVT || 769193323Sed (EltVT.isInteger() && I->getValueType().isInteger() && 770193323Sed EltVT.bitsLE(I->getValueType()))) && 771193323Sed "Wrong operand type!"); 772193323Sed break; 773193323Sed } 774193323Sed } 775193323Sed} 776193323Sed 777198090Srdivacky/// getEVTAlignment - Compute the default alignment value for the 778193323Sed/// given type. 779193323Sed/// 780198090Srdivackyunsigned SelectionDAG::getEVTAlignment(EVT VT) const { 781193323Sed const Type *Ty = VT == MVT::iPTR ? 782198090Srdivacky PointerType::get(Type::getInt8Ty(*getContext()), 0) : 783198090Srdivacky VT.getTypeForEVT(*getContext()); 784193323Sed 785193323Sed return TLI.getTargetData()->getABITypeAlignment(Ty); 786193323Sed} 787193323Sed 788193323Sed// EntryNode could meaningfully have debug info if we can find it... 789193323SedSelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli) 790193323Sed : TLI(tli), FLI(fli), DW(0), 791193323Sed EntryNode(ISD::EntryToken, DebugLoc::getUnknownLoc(), 792200581Srdivacky getVTList(MVT::Other)), 793200581Srdivacky Root(getEntryNode()), Ordering(0) { 794193323Sed AllNodes.push_back(&EntryNode); 795202878Srdivacky Ordering = new SDNodeOrdering(); 796193323Sed} 797193323Sed 798193323Sedvoid SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi, 799193323Sed DwarfWriter *dw) { 800193323Sed MF = &mf; 801193323Sed MMI = mmi; 802193323Sed DW = dw; 803198090Srdivacky Context = &mf.getFunction()->getContext(); 804193323Sed} 805193323Sed 806193323SedSelectionDAG::~SelectionDAG() { 807193323Sed allnodes_clear(); 808200581Srdivacky delete Ordering; 809193323Sed} 810193323Sed 811193323Sedvoid SelectionDAG::allnodes_clear() { 812193323Sed assert(&*AllNodes.begin() == &EntryNode); 813193323Sed AllNodes.remove(AllNodes.begin()); 814193323Sed while (!AllNodes.empty()) 815193323Sed DeallocateNode(AllNodes.begin()); 816193323Sed} 817193323Sed 818193323Sedvoid SelectionDAG::clear() { 819193323Sed allnodes_clear(); 820193323Sed OperandAllocator.Reset(); 821193323Sed CSEMap.clear(); 822193323Sed 823193323Sed ExtendedValueTypeNodes.clear(); 824193323Sed ExternalSymbols.clear(); 825193323Sed TargetExternalSymbols.clear(); 826193323Sed std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), 827193323Sed static_cast<CondCodeSDNode*>(0)); 828193323Sed std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), 829193323Sed static_cast<SDNode*>(0)); 830193323Sed 831193323Sed EntryNode.UseList = 0; 832193323Sed AllNodes.push_back(&EntryNode); 833193323Sed Root = getEntryNode(); 834203954Srdivacky delete Ordering; 835202878Srdivacky Ordering = new SDNodeOrdering(); 836193323Sed} 837193323Sed 838198090SrdivackySDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) { 839198090Srdivacky return VT.bitsGT(Op.getValueType()) ? 840198090Srdivacky getNode(ISD::SIGN_EXTEND, DL, VT, Op) : 841198090Srdivacky getNode(ISD::TRUNCATE, DL, VT, Op); 842198090Srdivacky} 843198090Srdivacky 844198090SrdivackySDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) { 845198090Srdivacky return VT.bitsGT(Op.getValueType()) ? 846198090Srdivacky getNode(ISD::ZERO_EXTEND, DL, VT, Op) : 847198090Srdivacky getNode(ISD::TRUNCATE, DL, VT, Op); 848198090Srdivacky} 849198090Srdivacky 850198090SrdivackySDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) { 851200581Srdivacky assert(!VT.isVector() && 852200581Srdivacky "getZeroExtendInReg should use the vector element type instead of " 853200581Srdivacky "the vector type!"); 854193323Sed if (Op.getValueType() == VT) return Op; 855200581Srdivacky unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 856200581Srdivacky APInt Imm = APInt::getLowBitsSet(BitWidth, 857193323Sed VT.getSizeInBits()); 858193323Sed return getNode(ISD::AND, DL, Op.getValueType(), Op, 859193323Sed getConstant(Imm, Op.getValueType())); 860193323Sed} 861193323Sed 862193323Sed/// getNOT - Create a bitwise NOT operation as (XOR Val, -1). 863193323Sed/// 864198090SrdivackySDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) { 865204642Srdivacky EVT EltVT = VT.getScalarType(); 866193323Sed SDValue NegOne = 867193323Sed getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); 868193323Sed return getNode(ISD::XOR, DL, VT, Val, NegOne); 869193323Sed} 870193323Sed 871198090SrdivackySDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) { 872204642Srdivacky EVT EltVT = VT.getScalarType(); 873193323Sed assert((EltVT.getSizeInBits() >= 64 || 874193323Sed (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && 875193323Sed "getConstant with a uint64_t value that doesn't fit in the type!"); 876193323Sed return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT); 877193323Sed} 878193323Sed 879198090SrdivackySDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) { 880198090Srdivacky return getConstant(*ConstantInt::get(*Context, Val), VT, isT); 881193323Sed} 882193323Sed 883198090SrdivackySDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) { 884193323Sed assert(VT.isInteger() && "Cannot create FP integer constant!"); 885193323Sed 886204642Srdivacky EVT EltVT = VT.getScalarType(); 887193323Sed assert(Val.getBitWidth() == EltVT.getSizeInBits() && 888193323Sed "APInt size does not match type size!"); 889193323Sed 890193323Sed unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; 891193323Sed FoldingSetNodeID ID; 892193323Sed AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 893193323Sed ID.AddPointer(&Val); 894193323Sed void *IP = 0; 895193323Sed SDNode *N = NULL; 896201360Srdivacky if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 897193323Sed if (!VT.isVector()) 898193323Sed return SDValue(N, 0); 899201360Srdivacky 900193323Sed if (!N) { 901193323Sed N = NodeAllocator.Allocate<ConstantSDNode>(); 902193323Sed new (N) ConstantSDNode(isT, &Val, EltVT); 903193323Sed CSEMap.InsertNode(N, IP); 904193323Sed AllNodes.push_back(N); 905193323Sed } 906193323Sed 907193323Sed SDValue Result(N, 0); 908193323Sed if (VT.isVector()) { 909193323Sed SmallVector<SDValue, 8> Ops; 910193323Sed Ops.assign(VT.getVectorNumElements(), Result); 911193323Sed Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(), 912193323Sed VT, &Ops[0], Ops.size()); 913193323Sed } 914193323Sed return Result; 915193323Sed} 916193323Sed 917193323SedSDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) { 918193323Sed return getConstant(Val, TLI.getPointerTy(), isTarget); 919193323Sed} 920193323Sed 921193323Sed 922198090SrdivackySDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) { 923198090Srdivacky return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget); 924193323Sed} 925193323Sed 926198090SrdivackySDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){ 927193323Sed assert(VT.isFloatingPoint() && "Cannot create integer FP constant!"); 928193323Sed 929204642Srdivacky EVT EltVT = VT.getScalarType(); 930193323Sed 931193323Sed // Do the map lookup using the actual bit pattern for the floating point 932193323Sed // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 933193323Sed // we don't have issues with SNANs. 934193323Sed unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; 935193323Sed FoldingSetNodeID ID; 936193323Sed AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 937193323Sed ID.AddPointer(&V); 938193323Sed void *IP = 0; 939193323Sed SDNode *N = NULL; 940201360Srdivacky if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 941193323Sed if (!VT.isVector()) 942193323Sed return SDValue(N, 0); 943201360Srdivacky 944193323Sed if (!N) { 945193323Sed N = NodeAllocator.Allocate<ConstantFPSDNode>(); 946193323Sed new (N) ConstantFPSDNode(isTarget, &V, EltVT); 947193323Sed CSEMap.InsertNode(N, IP); 948193323Sed AllNodes.push_back(N); 949193323Sed } 950193323Sed 951193323Sed SDValue Result(N, 0); 952193323Sed if (VT.isVector()) { 953193323Sed SmallVector<SDValue, 8> Ops; 954193323Sed Ops.assign(VT.getVectorNumElements(), Result); 955193323Sed // FIXME DebugLoc info might be appropriate here 956193323Sed Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(), 957193323Sed VT, &Ops[0], Ops.size()); 958193323Sed } 959193323Sed return Result; 960193323Sed} 961193323Sed 962198090SrdivackySDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) { 963204642Srdivacky EVT EltVT = VT.getScalarType(); 964193323Sed if (EltVT==MVT::f32) 965193323Sed return getConstantFP(APFloat((float)Val), VT, isTarget); 966193323Sed else 967193323Sed return getConstantFP(APFloat(Val), VT, isTarget); 968193323Sed} 969193323Sed 970193323SedSDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, 971198090Srdivacky EVT VT, int64_t Offset, 972195098Sed bool isTargetGA, 973195098Sed unsigned char TargetFlags) { 974195098Sed assert((TargetFlags == 0 || isTargetGA) && 975195098Sed "Cannot set target flags on target-independent globals"); 976198090Srdivacky 977193323Sed // Truncate (with sign-extension) the offset value to the pointer size. 978198090Srdivacky EVT PTy = TLI.getPointerTy(); 979198090Srdivacky unsigned BitWidth = PTy.getSizeInBits(); 980193323Sed if (BitWidth < 64) 981193323Sed Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth)); 982193323Sed 983193323Sed const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 984193323Sed if (!GVar) { 985193323Sed // If GV is an alias then use the aliasee for determining thread-localness. 986193323Sed if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 987193323Sed GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)); 988193323Sed } 989193323Sed 990195098Sed unsigned Opc; 991193323Sed if (GVar && GVar->isThreadLocal()) 992193323Sed Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress; 993193323Sed else 994193323Sed Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; 995193323Sed 996193323Sed FoldingSetNodeID ID; 997193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 998193323Sed ID.AddPointer(GV); 999193323Sed ID.AddInteger(Offset); 1000195098Sed ID.AddInteger(TargetFlags); 1001193323Sed void *IP = 0; 1002201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1003193323Sed return SDValue(E, 0); 1004201360Srdivacky 1005193323Sed SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>(); 1006195098Sed new (N) GlobalAddressSDNode(Opc, GV, VT, Offset, TargetFlags); 1007193323Sed CSEMap.InsertNode(N, IP); 1008193323Sed AllNodes.push_back(N); 1009193323Sed return SDValue(N, 0); 1010193323Sed} 1011193323Sed 1012198090SrdivackySDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) { 1013193323Sed unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; 1014193323Sed FoldingSetNodeID ID; 1015193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1016193323Sed ID.AddInteger(FI); 1017193323Sed void *IP = 0; 1018201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1019193323Sed return SDValue(E, 0); 1020201360Srdivacky 1021193323Sed SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>(); 1022193323Sed new (N) FrameIndexSDNode(FI, VT, isTarget); 1023193323Sed CSEMap.InsertNode(N, IP); 1024193323Sed AllNodes.push_back(N); 1025193323Sed return SDValue(N, 0); 1026193323Sed} 1027193323Sed 1028198090SrdivackySDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget, 1029195098Sed unsigned char TargetFlags) { 1030195098Sed assert((TargetFlags == 0 || isTarget) && 1031195098Sed "Cannot set target flags on target-independent jump tables"); 1032193323Sed unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; 1033193323Sed FoldingSetNodeID ID; 1034193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1035193323Sed ID.AddInteger(JTI); 1036195098Sed ID.AddInteger(TargetFlags); 1037193323Sed void *IP = 0; 1038201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1039193323Sed return SDValue(E, 0); 1040201360Srdivacky 1041193323Sed SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>(); 1042195098Sed new (N) JumpTableSDNode(JTI, VT, isTarget, TargetFlags); 1043193323Sed CSEMap.InsertNode(N, IP); 1044193323Sed AllNodes.push_back(N); 1045193323Sed return SDValue(N, 0); 1046193323Sed} 1047193323Sed 1048198090SrdivackySDValue SelectionDAG::getConstantPool(Constant *C, EVT VT, 1049193323Sed unsigned Alignment, int Offset, 1050198090Srdivacky bool isTarget, 1051195098Sed unsigned char TargetFlags) { 1052195098Sed assert((TargetFlags == 0 || isTarget) && 1053195098Sed "Cannot set target flags on target-independent globals"); 1054193323Sed if (Alignment == 0) 1055193323Sed Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType()); 1056193323Sed unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 1057193323Sed FoldingSetNodeID ID; 1058193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1059193323Sed ID.AddInteger(Alignment); 1060193323Sed ID.AddInteger(Offset); 1061193323Sed ID.AddPointer(C); 1062195098Sed ID.AddInteger(TargetFlags); 1063193323Sed void *IP = 0; 1064201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1065193323Sed return SDValue(E, 0); 1066201360Srdivacky 1067193323Sed SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>(); 1068195098Sed new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags); 1069193323Sed CSEMap.InsertNode(N, IP); 1070193323Sed AllNodes.push_back(N); 1071193323Sed return SDValue(N, 0); 1072193323Sed} 1073193323Sed 1074193323Sed 1075198090SrdivackySDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT, 1076193323Sed unsigned Alignment, int Offset, 1077195098Sed bool isTarget, 1078195098Sed unsigned char TargetFlags) { 1079195098Sed assert((TargetFlags == 0 || isTarget) && 1080195098Sed "Cannot set target flags on target-independent globals"); 1081193323Sed if (Alignment == 0) 1082193323Sed Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType()); 1083193323Sed unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 1084193323Sed FoldingSetNodeID ID; 1085193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1086193323Sed ID.AddInteger(Alignment); 1087193323Sed ID.AddInteger(Offset); 1088193323Sed C->AddSelectionDAGCSEId(ID); 1089195098Sed ID.AddInteger(TargetFlags); 1090193323Sed void *IP = 0; 1091201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1092193323Sed return SDValue(E, 0); 1093201360Srdivacky 1094193323Sed SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>(); 1095195098Sed new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags); 1096193323Sed CSEMap.InsertNode(N, IP); 1097193323Sed AllNodes.push_back(N); 1098193323Sed return SDValue(N, 0); 1099193323Sed} 1100193323Sed 1101193323SedSDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 1102193323Sed FoldingSetNodeID ID; 1103193323Sed AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0); 1104193323Sed ID.AddPointer(MBB); 1105193323Sed void *IP = 0; 1106201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1107193323Sed return SDValue(E, 0); 1108201360Srdivacky 1109193323Sed SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>(); 1110193323Sed new (N) BasicBlockSDNode(MBB); 1111193323Sed CSEMap.InsertNode(N, IP); 1112193323Sed AllNodes.push_back(N); 1113193323Sed return SDValue(N, 0); 1114193323Sed} 1115193323Sed 1116198090SrdivackySDValue SelectionDAG::getValueType(EVT VT) { 1117198090Srdivacky if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >= 1118198090Srdivacky ValueTypeNodes.size()) 1119198090Srdivacky ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1); 1120193323Sed 1121193323Sed SDNode *&N = VT.isExtended() ? 1122198090Srdivacky ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy]; 1123193323Sed 1124193323Sed if (N) return SDValue(N, 0); 1125193323Sed N = NodeAllocator.Allocate<VTSDNode>(); 1126193323Sed new (N) VTSDNode(VT); 1127193323Sed AllNodes.push_back(N); 1128193323Sed return SDValue(N, 0); 1129193323Sed} 1130193323Sed 1131198090SrdivackySDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) { 1132193323Sed SDNode *&N = ExternalSymbols[Sym]; 1133193323Sed if (N) return SDValue(N, 0); 1134193323Sed N = NodeAllocator.Allocate<ExternalSymbolSDNode>(); 1135195098Sed new (N) ExternalSymbolSDNode(false, Sym, 0, VT); 1136193323Sed AllNodes.push_back(N); 1137193323Sed return SDValue(N, 0); 1138193323Sed} 1139193323Sed 1140198090SrdivackySDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT, 1141195098Sed unsigned char TargetFlags) { 1142195098Sed SDNode *&N = 1143195098Sed TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym, 1144195098Sed TargetFlags)]; 1145193323Sed if (N) return SDValue(N, 0); 1146193323Sed N = NodeAllocator.Allocate<ExternalSymbolSDNode>(); 1147195098Sed new (N) ExternalSymbolSDNode(true, Sym, TargetFlags, VT); 1148193323Sed AllNodes.push_back(N); 1149193323Sed return SDValue(N, 0); 1150193323Sed} 1151193323Sed 1152193323SedSDValue SelectionDAG::getCondCode(ISD::CondCode Cond) { 1153193323Sed if ((unsigned)Cond >= CondCodeNodes.size()) 1154193323Sed CondCodeNodes.resize(Cond+1); 1155193323Sed 1156193323Sed if (CondCodeNodes[Cond] == 0) { 1157193323Sed CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>(); 1158193323Sed new (N) CondCodeSDNode(Cond); 1159193323Sed CondCodeNodes[Cond] = N; 1160193323Sed AllNodes.push_back(N); 1161193323Sed } 1162201360Srdivacky 1163193323Sed return SDValue(CondCodeNodes[Cond], 0); 1164193323Sed} 1165193323Sed 1166193323Sed// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in 1167193323Sed// the shuffle mask M that point at N1 to point at N2, and indices that point 1168193323Sed// N2 to point at N1. 1169193323Sedstatic void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) { 1170193323Sed std::swap(N1, N2); 1171193323Sed int NElts = M.size(); 1172193323Sed for (int i = 0; i != NElts; ++i) { 1173193323Sed if (M[i] >= NElts) 1174193323Sed M[i] -= NElts; 1175193323Sed else if (M[i] >= 0) 1176193323Sed M[i] += NElts; 1177193323Sed } 1178193323Sed} 1179193323Sed 1180198090SrdivackySDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1, 1181193323Sed SDValue N2, const int *Mask) { 1182193323Sed assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE"); 1183198090Srdivacky assert(VT.isVector() && N1.getValueType().isVector() && 1184193323Sed "Vector Shuffle VTs must be a vectors"); 1185193323Sed assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() 1186193323Sed && "Vector Shuffle VTs must have same element type"); 1187193323Sed 1188193323Sed // Canonicalize shuffle undef, undef -> undef 1189193323Sed if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF) 1190198090Srdivacky return getUNDEF(VT); 1191193323Sed 1192198090Srdivacky // Validate that all indices in Mask are within the range of the elements 1193193323Sed // input to the shuffle. 1194193323Sed unsigned NElts = VT.getVectorNumElements(); 1195193323Sed SmallVector<int, 8> MaskVec; 1196193323Sed for (unsigned i = 0; i != NElts; ++i) { 1197193323Sed assert(Mask[i] < (int)(NElts * 2) && "Index out of range"); 1198193323Sed MaskVec.push_back(Mask[i]); 1199193323Sed } 1200198090Srdivacky 1201193323Sed // Canonicalize shuffle v, v -> v, undef 1202193323Sed if (N1 == N2) { 1203193323Sed N2 = getUNDEF(VT); 1204193323Sed for (unsigned i = 0; i != NElts; ++i) 1205193323Sed if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts; 1206193323Sed } 1207198090Srdivacky 1208193323Sed // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. 1209193323Sed if (N1.getOpcode() == ISD::UNDEF) 1210193323Sed commuteShuffle(N1, N2, MaskVec); 1211198090Srdivacky 1212193323Sed // Canonicalize all index into lhs, -> shuffle lhs, undef 1213193323Sed // Canonicalize all index into rhs, -> shuffle rhs, undef 1214193323Sed bool AllLHS = true, AllRHS = true; 1215193323Sed bool N2Undef = N2.getOpcode() == ISD::UNDEF; 1216193323Sed for (unsigned i = 0; i != NElts; ++i) { 1217193323Sed if (MaskVec[i] >= (int)NElts) { 1218193323Sed if (N2Undef) 1219193323Sed MaskVec[i] = -1; 1220193323Sed else 1221193323Sed AllLHS = false; 1222193323Sed } else if (MaskVec[i] >= 0) { 1223193323Sed AllRHS = false; 1224193323Sed } 1225193323Sed } 1226193323Sed if (AllLHS && AllRHS) 1227193323Sed return getUNDEF(VT); 1228193323Sed if (AllLHS && !N2Undef) 1229193323Sed N2 = getUNDEF(VT); 1230193323Sed if (AllRHS) { 1231193323Sed N1 = getUNDEF(VT); 1232193323Sed commuteShuffle(N1, N2, MaskVec); 1233193323Sed } 1234198090Srdivacky 1235193323Sed // If Identity shuffle, or all shuffle in to undef, return that node. 1236193323Sed bool AllUndef = true; 1237193323Sed bool Identity = true; 1238193323Sed for (unsigned i = 0; i != NElts; ++i) { 1239193323Sed if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false; 1240193323Sed if (MaskVec[i] >= 0) AllUndef = false; 1241193323Sed } 1242198090Srdivacky if (Identity && NElts == N1.getValueType().getVectorNumElements()) 1243193323Sed return N1; 1244193323Sed if (AllUndef) 1245193323Sed return getUNDEF(VT); 1246193323Sed 1247193323Sed FoldingSetNodeID ID; 1248193323Sed SDValue Ops[2] = { N1, N2 }; 1249193323Sed AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2); 1250193323Sed for (unsigned i = 0; i != NElts; ++i) 1251193323Sed ID.AddInteger(MaskVec[i]); 1252198090Srdivacky 1253193323Sed void* IP = 0; 1254201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1255193323Sed return SDValue(E, 0); 1256198090Srdivacky 1257193323Sed // Allocate the mask array for the node out of the BumpPtrAllocator, since 1258193323Sed // SDNode doesn't have access to it. This memory will be "leaked" when 1259193323Sed // the node is deallocated, but recovered when the NodeAllocator is released. 1260193323Sed int *MaskAlloc = OperandAllocator.Allocate<int>(NElts); 1261193323Sed memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int)); 1262198090Srdivacky 1263193323Sed ShuffleVectorSDNode *N = NodeAllocator.Allocate<ShuffleVectorSDNode>(); 1264193323Sed new (N) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc); 1265193323Sed CSEMap.InsertNode(N, IP); 1266193323Sed AllNodes.push_back(N); 1267193323Sed return SDValue(N, 0); 1268193323Sed} 1269193323Sed 1270198090SrdivackySDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl, 1271193323Sed SDValue Val, SDValue DTy, 1272193323Sed SDValue STy, SDValue Rnd, SDValue Sat, 1273193323Sed ISD::CvtCode Code) { 1274193323Sed // If the src and dest types are the same and the conversion is between 1275193323Sed // integer types of the same sign or two floats, no conversion is necessary. 1276193323Sed if (DTy == STy && 1277193323Sed (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF)) 1278193323Sed return Val; 1279193323Sed 1280193323Sed FoldingSetNodeID ID; 1281199481Srdivacky SDValue Ops[] = { Val, DTy, STy, Rnd, Sat }; 1282199481Srdivacky AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5); 1283193323Sed void* IP = 0; 1284201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1285193323Sed return SDValue(E, 0); 1286201360Srdivacky 1287193323Sed CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>(); 1288193323Sed new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code); 1289193323Sed CSEMap.InsertNode(N, IP); 1290193323Sed AllNodes.push_back(N); 1291193323Sed return SDValue(N, 0); 1292193323Sed} 1293193323Sed 1294198090SrdivackySDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) { 1295193323Sed FoldingSetNodeID ID; 1296193323Sed AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0); 1297193323Sed ID.AddInteger(RegNo); 1298193323Sed void *IP = 0; 1299201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1300193323Sed return SDValue(E, 0); 1301201360Srdivacky 1302193323Sed SDNode *N = NodeAllocator.Allocate<RegisterSDNode>(); 1303193323Sed new (N) RegisterSDNode(RegNo, VT); 1304193323Sed CSEMap.InsertNode(N, IP); 1305193323Sed AllNodes.push_back(N); 1306193323Sed return SDValue(N, 0); 1307193323Sed} 1308193323Sed 1309193323SedSDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl, 1310193323Sed SDValue Root, 1311193323Sed unsigned LabelID) { 1312193323Sed FoldingSetNodeID ID; 1313193323Sed SDValue Ops[] = { Root }; 1314193323Sed AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1); 1315193323Sed ID.AddInteger(LabelID); 1316193323Sed void *IP = 0; 1317201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1318193323Sed return SDValue(E, 0); 1319201360Srdivacky 1320193323Sed SDNode *N = NodeAllocator.Allocate<LabelSDNode>(); 1321193323Sed new (N) LabelSDNode(Opcode, dl, Root, LabelID); 1322193323Sed CSEMap.InsertNode(N, IP); 1323193323Sed AllNodes.push_back(N); 1324193323Sed return SDValue(N, 0); 1325193323Sed} 1326193323Sed 1327199989SrdivackySDValue SelectionDAG::getBlockAddress(BlockAddress *BA, EVT VT, 1328199989Srdivacky bool isTarget, 1329199989Srdivacky unsigned char TargetFlags) { 1330198892Srdivacky unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress; 1331198892Srdivacky 1332198892Srdivacky FoldingSetNodeID ID; 1333199989Srdivacky AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1334198892Srdivacky ID.AddPointer(BA); 1335199989Srdivacky ID.AddInteger(TargetFlags); 1336198892Srdivacky void *IP = 0; 1337201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1338198892Srdivacky return SDValue(E, 0); 1339201360Srdivacky 1340198892Srdivacky SDNode *N = NodeAllocator.Allocate<BlockAddressSDNode>(); 1341199989Srdivacky new (N) BlockAddressSDNode(Opc, VT, BA, TargetFlags); 1342198892Srdivacky CSEMap.InsertNode(N, IP); 1343198892Srdivacky AllNodes.push_back(N); 1344198892Srdivacky return SDValue(N, 0); 1345198892Srdivacky} 1346198892Srdivacky 1347193323SedSDValue SelectionDAG::getSrcValue(const Value *V) { 1348204642Srdivacky assert((!V || V->getType()->isPointerTy()) && 1349193323Sed "SrcValue is not a pointer?"); 1350193323Sed 1351193323Sed FoldingSetNodeID ID; 1352193323Sed AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0); 1353193323Sed ID.AddPointer(V); 1354193323Sed 1355193323Sed void *IP = 0; 1356201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1357193323Sed return SDValue(E, 0); 1358193323Sed 1359193323Sed SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>(); 1360193323Sed new (N) SrcValueSDNode(V); 1361193323Sed CSEMap.InsertNode(N, IP); 1362193323Sed AllNodes.push_back(N); 1363193323Sed return SDValue(N, 0); 1364193323Sed} 1365193323Sed 1366193323Sed/// getShiftAmountOperand - Return the specified value casted to 1367193323Sed/// the target's desired shift amount type. 1368193323SedSDValue SelectionDAG::getShiftAmountOperand(SDValue Op) { 1369198090Srdivacky EVT OpTy = Op.getValueType(); 1370193323Sed MVT ShTy = TLI.getShiftAmountTy(); 1371193323Sed if (OpTy == ShTy || OpTy.isVector()) return Op; 1372193323Sed 1373193323Sed ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; 1374193323Sed return getNode(Opcode, Op.getDebugLoc(), ShTy, Op); 1375193323Sed} 1376193323Sed 1377193323Sed/// CreateStackTemporary - Create a stack temporary, suitable for holding the 1378193323Sed/// specified value type. 1379198090SrdivackySDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) { 1380193323Sed MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 1381198090Srdivacky unsigned ByteSize = VT.getStoreSize(); 1382198090Srdivacky const Type *Ty = VT.getTypeForEVT(*getContext()); 1383193323Sed unsigned StackAlign = 1384193323Sed std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign); 1385193323Sed 1386199481Srdivacky int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); 1387193323Sed return getFrameIndex(FrameIdx, TLI.getPointerTy()); 1388193323Sed} 1389193323Sed 1390193323Sed/// CreateStackTemporary - Create a stack temporary suitable for holding 1391193323Sed/// either of the specified value types. 1392198090SrdivackySDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) { 1393193323Sed unsigned Bytes = std::max(VT1.getStoreSizeInBits(), 1394193323Sed VT2.getStoreSizeInBits())/8; 1395198090Srdivacky const Type *Ty1 = VT1.getTypeForEVT(*getContext()); 1396198090Srdivacky const Type *Ty2 = VT2.getTypeForEVT(*getContext()); 1397193323Sed const TargetData *TD = TLI.getTargetData(); 1398193323Sed unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1), 1399193323Sed TD->getPrefTypeAlignment(Ty2)); 1400193323Sed 1401193323Sed MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 1402199481Srdivacky int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false); 1403193323Sed return getFrameIndex(FrameIdx, TLI.getPointerTy()); 1404193323Sed} 1405193323Sed 1406198090SrdivackySDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, 1407193323Sed SDValue N2, ISD::CondCode Cond, DebugLoc dl) { 1408193323Sed // These setcc operations always fold. 1409193323Sed switch (Cond) { 1410193323Sed default: break; 1411193323Sed case ISD::SETFALSE: 1412193323Sed case ISD::SETFALSE2: return getConstant(0, VT); 1413193323Sed case ISD::SETTRUE: 1414193323Sed case ISD::SETTRUE2: return getConstant(1, VT); 1415193323Sed 1416193323Sed case ISD::SETOEQ: 1417193323Sed case ISD::SETOGT: 1418193323Sed case ISD::SETOGE: 1419193323Sed case ISD::SETOLT: 1420193323Sed case ISD::SETOLE: 1421193323Sed case ISD::SETONE: 1422193323Sed case ISD::SETO: 1423193323Sed case ISD::SETUO: 1424193323Sed case ISD::SETUEQ: 1425193323Sed case ISD::SETUNE: 1426193323Sed assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!"); 1427193323Sed break; 1428193323Sed } 1429193323Sed 1430193323Sed if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) { 1431193323Sed const APInt &C2 = N2C->getAPIntValue(); 1432193323Sed if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 1433193323Sed const APInt &C1 = N1C->getAPIntValue(); 1434193323Sed 1435193323Sed switch (Cond) { 1436198090Srdivacky default: llvm_unreachable("Unknown integer setcc!"); 1437193323Sed case ISD::SETEQ: return getConstant(C1 == C2, VT); 1438193323Sed case ISD::SETNE: return getConstant(C1 != C2, VT); 1439193323Sed case ISD::SETULT: return getConstant(C1.ult(C2), VT); 1440193323Sed case ISD::SETUGT: return getConstant(C1.ugt(C2), VT); 1441193323Sed case ISD::SETULE: return getConstant(C1.ule(C2), VT); 1442193323Sed case ISD::SETUGE: return getConstant(C1.uge(C2), VT); 1443193323Sed case ISD::SETLT: return getConstant(C1.slt(C2), VT); 1444193323Sed case ISD::SETGT: return getConstant(C1.sgt(C2), VT); 1445193323Sed case ISD::SETLE: return getConstant(C1.sle(C2), VT); 1446193323Sed case ISD::SETGE: return getConstant(C1.sge(C2), VT); 1447193323Sed } 1448193323Sed } 1449193323Sed } 1450193323Sed if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) { 1451193323Sed if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) { 1452193323Sed // No compile time operations on this type yet. 1453193323Sed if (N1C->getValueType(0) == MVT::ppcf128) 1454193323Sed return SDValue(); 1455193323Sed 1456193323Sed APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF()); 1457193323Sed switch (Cond) { 1458193323Sed default: break; 1459193323Sed case ISD::SETEQ: if (R==APFloat::cmpUnordered) 1460193323Sed return getUNDEF(VT); 1461193323Sed // fall through 1462193323Sed case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT); 1463193323Sed case ISD::SETNE: if (R==APFloat::cmpUnordered) 1464193323Sed return getUNDEF(VT); 1465193323Sed // fall through 1466193323Sed case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan || 1467193323Sed R==APFloat::cmpLessThan, VT); 1468193323Sed case ISD::SETLT: if (R==APFloat::cmpUnordered) 1469193323Sed return getUNDEF(VT); 1470193323Sed // fall through 1471193323Sed case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT); 1472193323Sed case ISD::SETGT: if (R==APFloat::cmpUnordered) 1473193323Sed return getUNDEF(VT); 1474193323Sed // fall through 1475193323Sed case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT); 1476193323Sed case ISD::SETLE: if (R==APFloat::cmpUnordered) 1477193323Sed return getUNDEF(VT); 1478193323Sed // fall through 1479193323Sed case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan || 1480193323Sed R==APFloat::cmpEqual, VT); 1481193323Sed case ISD::SETGE: if (R==APFloat::cmpUnordered) 1482193323Sed return getUNDEF(VT); 1483193323Sed // fall through 1484193323Sed case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan || 1485193323Sed R==APFloat::cmpEqual, VT); 1486193323Sed case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT); 1487193323Sed case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT); 1488193323Sed case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered || 1489193323Sed R==APFloat::cmpEqual, VT); 1490193323Sed case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT); 1491193323Sed case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered || 1492193323Sed R==APFloat::cmpLessThan, VT); 1493193323Sed case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan || 1494193323Sed R==APFloat::cmpUnordered, VT); 1495193323Sed case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT); 1496193323Sed case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT); 1497193323Sed } 1498193323Sed } else { 1499193323Sed // Ensure that the constant occurs on the RHS. 1500193323Sed return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 1501193323Sed } 1502193323Sed } 1503193323Sed 1504193323Sed // Could not fold it. 1505193323Sed return SDValue(); 1506193323Sed} 1507193323Sed 1508193323Sed/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We 1509193323Sed/// use this predicate to simplify operations downstream. 1510193323Sedbool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const { 1511198090Srdivacky // This predicate is not safe for vector operations. 1512198090Srdivacky if (Op.getValueType().isVector()) 1513198090Srdivacky return false; 1514198090Srdivacky 1515200581Srdivacky unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 1516193323Sed return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth); 1517193323Sed} 1518193323Sed 1519193323Sed/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 1520193323Sed/// this predicate to simplify operations downstream. Mask is known to be zero 1521193323Sed/// for bits that V cannot have. 1522193323Sedbool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask, 1523193323Sed unsigned Depth) const { 1524193323Sed APInt KnownZero, KnownOne; 1525193323Sed ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 1526193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1527193323Sed return (KnownZero & Mask) == Mask; 1528193323Sed} 1529193323Sed 1530193323Sed/// ComputeMaskedBits - Determine which of the bits specified in Mask are 1531193323Sed/// known to be either zero or one and return them in the KnownZero/KnownOne 1532193323Sed/// bitsets. This code only analyzes bits in Mask, in order to short-circuit 1533193323Sed/// processing. 1534193323Sedvoid SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask, 1535193323Sed APInt &KnownZero, APInt &KnownOne, 1536193323Sed unsigned Depth) const { 1537193323Sed unsigned BitWidth = Mask.getBitWidth(); 1538200581Srdivacky assert(BitWidth == Op.getValueType().getScalarType().getSizeInBits() && 1539193323Sed "Mask size mismatches value type size!"); 1540193323Sed 1541193323Sed KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything. 1542193323Sed if (Depth == 6 || Mask == 0) 1543193323Sed return; // Limit search depth. 1544193323Sed 1545193323Sed APInt KnownZero2, KnownOne2; 1546193323Sed 1547193323Sed switch (Op.getOpcode()) { 1548193323Sed case ISD::Constant: 1549193323Sed // We know all of the bits for a constant! 1550193323Sed KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask; 1551193323Sed KnownZero = ~KnownOne & Mask; 1552193323Sed return; 1553193323Sed case ISD::AND: 1554193323Sed // If either the LHS or the RHS are Zero, the result is zero. 1555193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1556193323Sed ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero, 1557193323Sed KnownZero2, KnownOne2, Depth+1); 1558193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1559193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1560193323Sed 1561193323Sed // Output known-1 bits are only known if set in both the LHS & RHS. 1562193323Sed KnownOne &= KnownOne2; 1563193323Sed // Output known-0 are known to be clear if zero in either the LHS | RHS. 1564193323Sed KnownZero |= KnownZero2; 1565193323Sed return; 1566193323Sed case ISD::OR: 1567193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1568193323Sed ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne, 1569193323Sed KnownZero2, KnownOne2, Depth+1); 1570193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1571193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1572193323Sed 1573193323Sed // Output known-0 bits are only known if clear in both the LHS & RHS. 1574193323Sed KnownZero &= KnownZero2; 1575193323Sed // Output known-1 are known to be set if set in either the LHS | RHS. 1576193323Sed KnownOne |= KnownOne2; 1577193323Sed return; 1578193323Sed case ISD::XOR: { 1579193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1580193323Sed ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1581193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1582193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1583193323Sed 1584193323Sed // Output known-0 bits are known if clear or set in both the LHS & RHS. 1585193323Sed APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 1586193323Sed // Output known-1 are known to be set if set in only one of the LHS, RHS. 1587193323Sed KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 1588193323Sed KnownZero = KnownZeroOut; 1589193323Sed return; 1590193323Sed } 1591193323Sed case ISD::MUL: { 1592193323Sed APInt Mask2 = APInt::getAllOnesValue(BitWidth); 1593193323Sed ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1); 1594193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); 1595193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1596193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1597193323Sed 1598193323Sed // If low bits are zero in either operand, output low known-0 bits. 1599193323Sed // Also compute a conserative estimate for high known-0 bits. 1600193323Sed // More trickiness is possible, but this is sufficient for the 1601193323Sed // interesting case of alignment computation. 1602193323Sed KnownOne.clear(); 1603193323Sed unsigned TrailZ = KnownZero.countTrailingOnes() + 1604193323Sed KnownZero2.countTrailingOnes(); 1605193323Sed unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + 1606193323Sed KnownZero2.countLeadingOnes(), 1607193323Sed BitWidth) - BitWidth; 1608193323Sed 1609193323Sed TrailZ = std::min(TrailZ, BitWidth); 1610193323Sed LeadZ = std::min(LeadZ, BitWidth); 1611193323Sed KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | 1612193323Sed APInt::getHighBitsSet(BitWidth, LeadZ); 1613193323Sed KnownZero &= Mask; 1614193323Sed return; 1615193323Sed } 1616193323Sed case ISD::UDIV: { 1617193323Sed // For the purposes of computing leading zeros we can conservatively 1618193323Sed // treat a udiv as a logical right shift by the power of 2 known to 1619193323Sed // be less than the denominator. 1620193323Sed APInt AllOnes = APInt::getAllOnesValue(BitWidth); 1621193323Sed ComputeMaskedBits(Op.getOperand(0), 1622193323Sed AllOnes, KnownZero2, KnownOne2, Depth+1); 1623193323Sed unsigned LeadZ = KnownZero2.countLeadingOnes(); 1624193323Sed 1625193323Sed KnownOne2.clear(); 1626193323Sed KnownZero2.clear(); 1627193323Sed ComputeMaskedBits(Op.getOperand(1), 1628193323Sed AllOnes, KnownZero2, KnownOne2, Depth+1); 1629193323Sed unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); 1630193323Sed if (RHSUnknownLeadingOnes != BitWidth) 1631193323Sed LeadZ = std::min(BitWidth, 1632193323Sed LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); 1633193323Sed 1634193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask; 1635193323Sed return; 1636193323Sed } 1637193323Sed case ISD::SELECT: 1638193323Sed ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1); 1639193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); 1640193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1641193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1642193323Sed 1643193323Sed // Only known if known in both the LHS and RHS. 1644193323Sed KnownOne &= KnownOne2; 1645193323Sed KnownZero &= KnownZero2; 1646193323Sed return; 1647193323Sed case ISD::SELECT_CC: 1648193323Sed ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1); 1649193323Sed ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1); 1650193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1651193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1652193323Sed 1653193323Sed // Only known if known in both the LHS and RHS. 1654193323Sed KnownOne &= KnownOne2; 1655193323Sed KnownZero &= KnownZero2; 1656193323Sed return; 1657193323Sed case ISD::SADDO: 1658193323Sed case ISD::UADDO: 1659193323Sed case ISD::SSUBO: 1660193323Sed case ISD::USUBO: 1661193323Sed case ISD::SMULO: 1662193323Sed case ISD::UMULO: 1663193323Sed if (Op.getResNo() != 1) 1664193323Sed return; 1665193323Sed // The boolean result conforms to getBooleanContents. Fall through. 1666193323Sed case ISD::SETCC: 1667193323Sed // If we know the result of a setcc has the top bits zero, use this info. 1668193323Sed if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent && 1669193323Sed BitWidth > 1) 1670193323Sed KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 1671193323Sed return; 1672193323Sed case ISD::SHL: 1673193323Sed // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 1674193323Sed if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1675193323Sed unsigned ShAmt = SA->getZExtValue(); 1676193323Sed 1677193323Sed // If the shift count is an invalid immediate, don't do anything. 1678193323Sed if (ShAmt >= BitWidth) 1679193323Sed return; 1680193323Sed 1681193323Sed ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt), 1682193323Sed KnownZero, KnownOne, Depth+1); 1683193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1684193323Sed KnownZero <<= ShAmt; 1685193323Sed KnownOne <<= ShAmt; 1686193323Sed // low bits known zero. 1687193323Sed KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt); 1688193323Sed } 1689193323Sed return; 1690193323Sed case ISD::SRL: 1691193323Sed // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 1692193323Sed if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1693193323Sed unsigned ShAmt = SA->getZExtValue(); 1694193323Sed 1695193323Sed // If the shift count is an invalid immediate, don't do anything. 1696193323Sed if (ShAmt >= BitWidth) 1697193323Sed return; 1698193323Sed 1699193323Sed ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt), 1700193323Sed KnownZero, KnownOne, Depth+1); 1701193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1702193323Sed KnownZero = KnownZero.lshr(ShAmt); 1703193323Sed KnownOne = KnownOne.lshr(ShAmt); 1704193323Sed 1705193323Sed APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask; 1706193323Sed KnownZero |= HighBits; // High bits known zero. 1707193323Sed } 1708193323Sed return; 1709193323Sed case ISD::SRA: 1710193323Sed if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1711193323Sed unsigned ShAmt = SA->getZExtValue(); 1712193323Sed 1713193323Sed // If the shift count is an invalid immediate, don't do anything. 1714193323Sed if (ShAmt >= BitWidth) 1715193323Sed return; 1716193323Sed 1717193323Sed APInt InDemandedMask = (Mask << ShAmt); 1718193323Sed // If any of the demanded bits are produced by the sign extension, we also 1719193323Sed // demand the input sign bit. 1720193323Sed APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask; 1721193323Sed if (HighBits.getBoolValue()) 1722193323Sed InDemandedMask |= APInt::getSignBit(BitWidth); 1723193323Sed 1724193323Sed ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne, 1725193323Sed Depth+1); 1726193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1727193323Sed KnownZero = KnownZero.lshr(ShAmt); 1728193323Sed KnownOne = KnownOne.lshr(ShAmt); 1729193323Sed 1730193323Sed // Handle the sign bits. 1731193323Sed APInt SignBit = APInt::getSignBit(BitWidth); 1732193323Sed SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask. 1733193323Sed 1734193323Sed if (KnownZero.intersects(SignBit)) { 1735193323Sed KnownZero |= HighBits; // New bits are known zero. 1736193323Sed } else if (KnownOne.intersects(SignBit)) { 1737193323Sed KnownOne |= HighBits; // New bits are known one. 1738193323Sed } 1739193323Sed } 1740193323Sed return; 1741193323Sed case ISD::SIGN_EXTEND_INREG: { 1742198090Srdivacky EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1743202375Srdivacky unsigned EBits = EVT.getScalarType().getSizeInBits(); 1744193323Sed 1745193323Sed // Sign extension. Compute the demanded bits in the result that are not 1746193323Sed // present in the input. 1747193323Sed APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask; 1748193323Sed 1749193323Sed APInt InSignBit = APInt::getSignBit(EBits); 1750193323Sed APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits); 1751193323Sed 1752193323Sed // If the sign extended bits are demanded, we know that the sign 1753193323Sed // bit is demanded. 1754193323Sed InSignBit.zext(BitWidth); 1755193323Sed if (NewBits.getBoolValue()) 1756193323Sed InputDemandedBits |= InSignBit; 1757193323Sed 1758193323Sed ComputeMaskedBits(Op.getOperand(0), InputDemandedBits, 1759193323Sed KnownZero, KnownOne, Depth+1); 1760193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1761193323Sed 1762193323Sed // If the sign bit of the input is known set or clear, then we know the 1763193323Sed // top bits of the result. 1764193323Sed if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear 1765193323Sed KnownZero |= NewBits; 1766193323Sed KnownOne &= ~NewBits; 1767193323Sed } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set 1768193323Sed KnownOne |= NewBits; 1769193323Sed KnownZero &= ~NewBits; 1770193323Sed } else { // Input sign bit unknown 1771193323Sed KnownZero &= ~NewBits; 1772193323Sed KnownOne &= ~NewBits; 1773193323Sed } 1774193323Sed return; 1775193323Sed } 1776193323Sed case ISD::CTTZ: 1777193323Sed case ISD::CTLZ: 1778193323Sed case ISD::CTPOP: { 1779193323Sed unsigned LowBits = Log2_32(BitWidth)+1; 1780193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); 1781193323Sed KnownOne.clear(); 1782193323Sed return; 1783193323Sed } 1784193323Sed case ISD::LOAD: { 1785193323Sed if (ISD::isZEXTLoad(Op.getNode())) { 1786193323Sed LoadSDNode *LD = cast<LoadSDNode>(Op); 1787198090Srdivacky EVT VT = LD->getMemoryVT(); 1788202375Srdivacky unsigned MemBits = VT.getScalarType().getSizeInBits(); 1789193323Sed KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask; 1790193323Sed } 1791193323Sed return; 1792193323Sed } 1793193323Sed case ISD::ZERO_EXTEND: { 1794198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1795200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1796193323Sed APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask; 1797193323Sed APInt InMask = Mask; 1798193323Sed InMask.trunc(InBits); 1799193323Sed KnownZero.trunc(InBits); 1800193323Sed KnownOne.trunc(InBits); 1801193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1802193323Sed KnownZero.zext(BitWidth); 1803193323Sed KnownOne.zext(BitWidth); 1804193323Sed KnownZero |= NewBits; 1805193323Sed return; 1806193323Sed } 1807193323Sed case ISD::SIGN_EXTEND: { 1808198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1809200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1810193323Sed APInt InSignBit = APInt::getSignBit(InBits); 1811193323Sed APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask; 1812193323Sed APInt InMask = Mask; 1813193323Sed InMask.trunc(InBits); 1814193323Sed 1815193323Sed // If any of the sign extended bits are demanded, we know that the sign 1816193323Sed // bit is demanded. Temporarily set this bit in the mask for our callee. 1817193323Sed if (NewBits.getBoolValue()) 1818193323Sed InMask |= InSignBit; 1819193323Sed 1820193323Sed KnownZero.trunc(InBits); 1821193323Sed KnownOne.trunc(InBits); 1822193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1823193323Sed 1824193323Sed // Note if the sign bit is known to be zero or one. 1825193323Sed bool SignBitKnownZero = KnownZero.isNegative(); 1826193323Sed bool SignBitKnownOne = KnownOne.isNegative(); 1827193323Sed assert(!(SignBitKnownZero && SignBitKnownOne) && 1828193323Sed "Sign bit can't be known to be both zero and one!"); 1829193323Sed 1830193323Sed // If the sign bit wasn't actually demanded by our caller, we don't 1831193323Sed // want it set in the KnownZero and KnownOne result values. Reset the 1832193323Sed // mask and reapply it to the result values. 1833193323Sed InMask = Mask; 1834193323Sed InMask.trunc(InBits); 1835193323Sed KnownZero &= InMask; 1836193323Sed KnownOne &= InMask; 1837193323Sed 1838193323Sed KnownZero.zext(BitWidth); 1839193323Sed KnownOne.zext(BitWidth); 1840193323Sed 1841193323Sed // If the sign bit is known zero or one, the top bits match. 1842193323Sed if (SignBitKnownZero) 1843193323Sed KnownZero |= NewBits; 1844193323Sed else if (SignBitKnownOne) 1845193323Sed KnownOne |= NewBits; 1846193323Sed return; 1847193323Sed } 1848193323Sed case ISD::ANY_EXTEND: { 1849198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1850200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1851193323Sed APInt InMask = Mask; 1852193323Sed InMask.trunc(InBits); 1853193323Sed KnownZero.trunc(InBits); 1854193323Sed KnownOne.trunc(InBits); 1855193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1856193323Sed KnownZero.zext(BitWidth); 1857193323Sed KnownOne.zext(BitWidth); 1858193323Sed return; 1859193323Sed } 1860193323Sed case ISD::TRUNCATE: { 1861198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1862200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1863193323Sed APInt InMask = Mask; 1864193323Sed InMask.zext(InBits); 1865193323Sed KnownZero.zext(InBits); 1866193323Sed KnownOne.zext(InBits); 1867193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1868193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1869193323Sed KnownZero.trunc(BitWidth); 1870193323Sed KnownOne.trunc(BitWidth); 1871193323Sed break; 1872193323Sed } 1873193323Sed case ISD::AssertZext: { 1874198090Srdivacky EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1875193323Sed APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits()); 1876193323Sed ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 1877193323Sed KnownOne, Depth+1); 1878193323Sed KnownZero |= (~InMask) & Mask; 1879193323Sed return; 1880193323Sed } 1881193323Sed case ISD::FGETSIGN: 1882193323Sed // All bits are zero except the low bit. 1883193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1); 1884193323Sed return; 1885193323Sed 1886193323Sed case ISD::SUB: { 1887193323Sed if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) { 1888193323Sed // We know that the top bits of C-X are clear if X contains less bits 1889193323Sed // than C (i.e. no wrap-around can happen). For example, 20-X is 1890193323Sed // positive if we can prove that X is >= 0 and < 16. 1891193323Sed if (CLHS->getAPIntValue().isNonNegative()) { 1892193323Sed unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros(); 1893193323Sed // NLZ can't be BitWidth with no sign bit 1894193323Sed APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); 1895193323Sed ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2, 1896193323Sed Depth+1); 1897193323Sed 1898193323Sed // If all of the MaskV bits are known to be zero, then we know the 1899193323Sed // output top bits are zero, because we now know that the output is 1900193323Sed // from [0-C]. 1901193323Sed if ((KnownZero2 & MaskV) == MaskV) { 1902193323Sed unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros(); 1903193323Sed // Top bits known zero. 1904193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; 1905193323Sed } 1906193323Sed } 1907193323Sed } 1908193323Sed } 1909193323Sed // fall through 1910193323Sed case ISD::ADD: { 1911193323Sed // Output known-0 bits are known if clear or set in both the low clear bits 1912193323Sed // common to both LHS & RHS. For example, 8+(X<<3) is known to have the 1913193323Sed // low 3 bits clear. 1914193323Sed APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes()); 1915193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); 1916193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1917193323Sed unsigned KnownZeroOut = KnownZero2.countTrailingOnes(); 1918193323Sed 1919193323Sed ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1); 1920193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1921193323Sed KnownZeroOut = std::min(KnownZeroOut, 1922193323Sed KnownZero2.countTrailingOnes()); 1923193323Sed 1924193323Sed KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut); 1925193323Sed return; 1926193323Sed } 1927193323Sed case ISD::SREM: 1928193323Sed if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1929203954Srdivacky const APInt &RA = Rem->getAPIntValue().abs(); 1930203954Srdivacky if (RA.isPowerOf2()) { 1931203954Srdivacky APInt LowBits = RA - 1; 1932193323Sed APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); 1933193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1); 1934193323Sed 1935203954Srdivacky // The low bits of the first operand are unchanged by the srem. 1936203954Srdivacky KnownZero = KnownZero2 & LowBits; 1937203954Srdivacky KnownOne = KnownOne2 & LowBits; 1938203954Srdivacky 1939203954Srdivacky // If the first operand is non-negative or has all low bits zero, then 1940203954Srdivacky // the upper bits are all zero. 1941193323Sed if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) 1942203954Srdivacky KnownZero |= ~LowBits; 1943193323Sed 1944203954Srdivacky // If the first operand is negative and not all low bits are zero, then 1945203954Srdivacky // the upper bits are all one. 1946203954Srdivacky if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) 1947203954Srdivacky KnownOne |= ~LowBits; 1948193323Sed 1949203954Srdivacky KnownZero &= Mask; 1950203954Srdivacky KnownOne &= Mask; 1951203954Srdivacky 1952193323Sed assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 1953193323Sed } 1954193323Sed } 1955193323Sed return; 1956193323Sed case ISD::UREM: { 1957193323Sed if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1958193323Sed const APInt &RA = Rem->getAPIntValue(); 1959193323Sed if (RA.isPowerOf2()) { 1960193323Sed APInt LowBits = (RA - 1); 1961193323Sed APInt Mask2 = LowBits & Mask; 1962193323Sed KnownZero |= ~LowBits & Mask; 1963193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1); 1964193323Sed assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 1965193323Sed break; 1966193323Sed } 1967193323Sed } 1968193323Sed 1969193323Sed // Since the result is less than or equal to either operand, any leading 1970193323Sed // zero bits in either operand must also exist in the result. 1971193323Sed APInt AllOnes = APInt::getAllOnesValue(BitWidth); 1972193323Sed ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne, 1973193323Sed Depth+1); 1974193323Sed ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2, 1975193323Sed Depth+1); 1976193323Sed 1977193323Sed uint32_t Leaders = std::max(KnownZero.countLeadingOnes(), 1978193323Sed KnownZero2.countLeadingOnes()); 1979193323Sed KnownOne.clear(); 1980193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask; 1981193323Sed return; 1982193323Sed } 1983193323Sed default: 1984193323Sed // Allow the target to implement this method for its nodes. 1985193323Sed if (Op.getOpcode() >= ISD::BUILTIN_OP_END) { 1986193323Sed case ISD::INTRINSIC_WO_CHAIN: 1987193323Sed case ISD::INTRINSIC_W_CHAIN: 1988193323Sed case ISD::INTRINSIC_VOID: 1989198090Srdivacky TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this, 1990198090Srdivacky Depth); 1991193323Sed } 1992193323Sed return; 1993193323Sed } 1994193323Sed} 1995193323Sed 1996193323Sed/// ComputeNumSignBits - Return the number of times the sign bit of the 1997193323Sed/// register is replicated into the other bits. We know that at least 1 bit 1998193323Sed/// is always equal to the sign bit (itself), but other cases can give us 1999193323Sed/// information. For example, immediately after an "SRA X, 2", we know that 2000193323Sed/// the top 3 bits are all equal to each other, so we return 3. 2001193323Sedunsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{ 2002198090Srdivacky EVT VT = Op.getValueType(); 2003193323Sed assert(VT.isInteger() && "Invalid VT!"); 2004200581Srdivacky unsigned VTBits = VT.getScalarType().getSizeInBits(); 2005193323Sed unsigned Tmp, Tmp2; 2006193323Sed unsigned FirstAnswer = 1; 2007193323Sed 2008193323Sed if (Depth == 6) 2009193323Sed return 1; // Limit search depth. 2010193323Sed 2011193323Sed switch (Op.getOpcode()) { 2012193323Sed default: break; 2013193323Sed case ISD::AssertSext: 2014193323Sed Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 2015193323Sed return VTBits-Tmp+1; 2016193323Sed case ISD::AssertZext: 2017193323Sed Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 2018193323Sed return VTBits-Tmp; 2019193323Sed 2020193323Sed case ISD::Constant: { 2021193323Sed const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue(); 2022193323Sed // If negative, return # leading ones. 2023193323Sed if (Val.isNegative()) 2024193323Sed return Val.countLeadingOnes(); 2025193323Sed 2026193323Sed // Return # leading zeros. 2027193323Sed return Val.countLeadingZeros(); 2028193323Sed } 2029193323Sed 2030193323Sed case ISD::SIGN_EXTEND: 2031200581Srdivacky Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); 2032193323Sed return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp; 2033193323Sed 2034193323Sed case ISD::SIGN_EXTEND_INREG: 2035193323Sed // Max of the input and what this extends. 2036202375Srdivacky Tmp = 2037202375Srdivacky cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits(); 2038193323Sed Tmp = VTBits-Tmp+1; 2039193323Sed 2040193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2041193323Sed return std::max(Tmp, Tmp2); 2042193323Sed 2043193323Sed case ISD::SRA: 2044193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2045193323Sed // SRA X, C -> adds C sign bits. 2046193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2047193323Sed Tmp += C->getZExtValue(); 2048193323Sed if (Tmp > VTBits) Tmp = VTBits; 2049193323Sed } 2050193323Sed return Tmp; 2051193323Sed case ISD::SHL: 2052193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2053193323Sed // shl destroys sign bits. 2054193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2055193323Sed if (C->getZExtValue() >= VTBits || // Bad shift. 2056193323Sed C->getZExtValue() >= Tmp) break; // Shifted all sign bits out. 2057193323Sed return Tmp - C->getZExtValue(); 2058193323Sed } 2059193323Sed break; 2060193323Sed case ISD::AND: 2061193323Sed case ISD::OR: 2062193323Sed case ISD::XOR: // NOT is handled here. 2063193323Sed // Logical binary ops preserve the number of sign bits at the worst. 2064193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2065193323Sed if (Tmp != 1) { 2066193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2067193323Sed FirstAnswer = std::min(Tmp, Tmp2); 2068193323Sed // We computed what we know about the sign bits as our first 2069193323Sed // answer. Now proceed to the generic code that uses 2070193323Sed // ComputeMaskedBits, and pick whichever answer is better. 2071193323Sed } 2072193323Sed break; 2073193323Sed 2074193323Sed case ISD::SELECT: 2075193323Sed Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2076193323Sed if (Tmp == 1) return 1; // Early out. 2077193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1); 2078193323Sed return std::min(Tmp, Tmp2); 2079193323Sed 2080193323Sed case ISD::SADDO: 2081193323Sed case ISD::UADDO: 2082193323Sed case ISD::SSUBO: 2083193323Sed case ISD::USUBO: 2084193323Sed case ISD::SMULO: 2085193323Sed case ISD::UMULO: 2086193323Sed if (Op.getResNo() != 1) 2087193323Sed break; 2088193323Sed // The boolean result conforms to getBooleanContents. Fall through. 2089193323Sed case ISD::SETCC: 2090193323Sed // If setcc returns 0/-1, all bits are sign bits. 2091193323Sed if (TLI.getBooleanContents() == 2092193323Sed TargetLowering::ZeroOrNegativeOneBooleanContent) 2093193323Sed return VTBits; 2094193323Sed break; 2095193323Sed case ISD::ROTL: 2096193323Sed case ISD::ROTR: 2097193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2098193323Sed unsigned RotAmt = C->getZExtValue() & (VTBits-1); 2099193323Sed 2100193323Sed // Handle rotate right by N like a rotate left by 32-N. 2101193323Sed if (Op.getOpcode() == ISD::ROTR) 2102193323Sed RotAmt = (VTBits-RotAmt) & (VTBits-1); 2103193323Sed 2104193323Sed // If we aren't rotating out all of the known-in sign bits, return the 2105193323Sed // number that are left. This handles rotl(sext(x), 1) for example. 2106193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2107193323Sed if (Tmp > RotAmt+1) return Tmp-RotAmt; 2108193323Sed } 2109193323Sed break; 2110193323Sed case ISD::ADD: 2111193323Sed // Add can have at most one carry bit. Thus we know that the output 2112193323Sed // is, at worst, one more bit than the inputs. 2113193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2114193323Sed if (Tmp == 1) return 1; // Early out. 2115193323Sed 2116193323Sed // Special case decrementing a value (ADD X, -1): 2117193323Sed if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 2118193323Sed if (CRHS->isAllOnesValue()) { 2119193323Sed APInt KnownZero, KnownOne; 2120193323Sed APInt Mask = APInt::getAllOnesValue(VTBits); 2121193323Sed ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 2122193323Sed 2123193323Sed // If the input is known to be 0 or 1, the output is 0/-1, which is all 2124193323Sed // sign bits set. 2125193323Sed if ((KnownZero | APInt(VTBits, 1)) == Mask) 2126193323Sed return VTBits; 2127193323Sed 2128193323Sed // If we are subtracting one from a positive number, there is no carry 2129193323Sed // out of the result. 2130193323Sed if (KnownZero.isNegative()) 2131193323Sed return Tmp; 2132193323Sed } 2133193323Sed 2134193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2135193323Sed if (Tmp2 == 1) return 1; 2136193323Sed return std::min(Tmp, Tmp2)-1; 2137193323Sed break; 2138193323Sed 2139193323Sed case ISD::SUB: 2140193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2141193323Sed if (Tmp2 == 1) return 1; 2142193323Sed 2143193323Sed // Handle NEG. 2144193323Sed if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 2145193323Sed if (CLHS->isNullValue()) { 2146193323Sed APInt KnownZero, KnownOne; 2147193323Sed APInt Mask = APInt::getAllOnesValue(VTBits); 2148193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 2149193323Sed // If the input is known to be 0 or 1, the output is 0/-1, which is all 2150193323Sed // sign bits set. 2151193323Sed if ((KnownZero | APInt(VTBits, 1)) == Mask) 2152193323Sed return VTBits; 2153193323Sed 2154193323Sed // If the input is known to be positive (the sign bit is known clear), 2155193323Sed // the output of the NEG has the same number of sign bits as the input. 2156193323Sed if (KnownZero.isNegative()) 2157193323Sed return Tmp2; 2158193323Sed 2159193323Sed // Otherwise, we treat this like a SUB. 2160193323Sed } 2161193323Sed 2162193323Sed // Sub can have at most one carry bit. Thus we know that the output 2163193323Sed // is, at worst, one more bit than the inputs. 2164193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2165193323Sed if (Tmp == 1) return 1; // Early out. 2166193323Sed return std::min(Tmp, Tmp2)-1; 2167193323Sed break; 2168193323Sed case ISD::TRUNCATE: 2169193323Sed // FIXME: it's tricky to do anything useful for this, but it is an important 2170193323Sed // case for targets like X86. 2171193323Sed break; 2172193323Sed } 2173193323Sed 2174193323Sed // Handle LOADX separately here. EXTLOAD case will fallthrough. 2175193323Sed if (Op.getOpcode() == ISD::LOAD) { 2176193323Sed LoadSDNode *LD = cast<LoadSDNode>(Op); 2177193323Sed unsigned ExtType = LD->getExtensionType(); 2178193323Sed switch (ExtType) { 2179193323Sed default: break; 2180193323Sed case ISD::SEXTLOAD: // '17' bits known 2181202375Srdivacky Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); 2182193323Sed return VTBits-Tmp+1; 2183193323Sed case ISD::ZEXTLOAD: // '16' bits known 2184202375Srdivacky Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); 2185193323Sed return VTBits-Tmp; 2186193323Sed } 2187193323Sed } 2188193323Sed 2189193323Sed // Allow the target to implement this method for its nodes. 2190193323Sed if (Op.getOpcode() >= ISD::BUILTIN_OP_END || 2191193323Sed Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2192193323Sed Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2193193323Sed Op.getOpcode() == ISD::INTRINSIC_VOID) { 2194193323Sed unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth); 2195193323Sed if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits); 2196193323Sed } 2197193323Sed 2198193323Sed // Finally, if we can prove that the top bits of the result are 0's or 1's, 2199193323Sed // use this information. 2200193323Sed APInt KnownZero, KnownOne; 2201193323Sed APInt Mask = APInt::getAllOnesValue(VTBits); 2202193323Sed ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 2203193323Sed 2204193323Sed if (KnownZero.isNegative()) { // sign bit is 0 2205193323Sed Mask = KnownZero; 2206193323Sed } else if (KnownOne.isNegative()) { // sign bit is 1; 2207193323Sed Mask = KnownOne; 2208193323Sed } else { 2209193323Sed // Nothing known. 2210193323Sed return FirstAnswer; 2211193323Sed } 2212193323Sed 2213193323Sed // Okay, we know that the sign bit in Mask is set. Use CLZ to determine 2214193323Sed // the number of identical bits in the top of the input value. 2215193323Sed Mask = ~Mask; 2216193323Sed Mask <<= Mask.getBitWidth()-VTBits; 2217193323Sed // Return # leading zeros. We use 'min' here in case Val was zero before 2218193323Sed // shifting. We don't want to return '64' as for an i32 "0". 2219193323Sed return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros())); 2220193323Sed} 2221193323Sed 2222198090Srdivackybool SelectionDAG::isKnownNeverNaN(SDValue Op) const { 2223198090Srdivacky // If we're told that NaNs won't happen, assume they won't. 2224198090Srdivacky if (FiniteOnlyFPMath()) 2225198090Srdivacky return true; 2226193323Sed 2227198090Srdivacky // If the value is a constant, we can obviously see if it is a NaN or not. 2228198090Srdivacky if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 2229198090Srdivacky return !C->getValueAPF().isNaN(); 2230198090Srdivacky 2231198090Srdivacky // TODO: Recognize more cases here. 2232198090Srdivacky 2233198090Srdivacky return false; 2234198090Srdivacky} 2235198090Srdivacky 2236204642Srdivackybool SelectionDAG::isKnownNeverZero(SDValue Op) const { 2237204642Srdivacky // If the value is a constant, we can obviously see if it is a zero or not. 2238204642Srdivacky if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 2239204642Srdivacky return !C->isZero(); 2240204642Srdivacky 2241204642Srdivacky // TODO: Recognize more cases here. 2242204642Srdivacky 2243204642Srdivacky return false; 2244204642Srdivacky} 2245204642Srdivacky 2246204642Srdivackybool SelectionDAG::isEqualTo(SDValue A, SDValue B) const { 2247204642Srdivacky // Check the obvious case. 2248204642Srdivacky if (A == B) return true; 2249204642Srdivacky 2250204642Srdivacky // For for negative and positive zero. 2251204642Srdivacky if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) 2252204642Srdivacky if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) 2253204642Srdivacky if (CA->isZero() && CB->isZero()) return true; 2254204642Srdivacky 2255204642Srdivacky // Otherwise they may not be equal. 2256204642Srdivacky return false; 2257204642Srdivacky} 2258204642Srdivacky 2259193323Sedbool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const { 2260193323Sed GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); 2261193323Sed if (!GA) return false; 2262193323Sed if (GA->getOffset() != 0) return false; 2263193323Sed GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal()); 2264193323Sed if (!GV) return false; 2265193323Sed MachineModuleInfo *MMI = getMachineModuleInfo(); 2266193323Sed return MMI && MMI->hasDebugInfo(); 2267193323Sed} 2268193323Sed 2269193323Sed 2270193323Sed/// getShuffleScalarElt - Returns the scalar element that will make up the ith 2271193323Sed/// element of the result of the vector shuffle. 2272193323SedSDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N, 2273193323Sed unsigned i) { 2274198090Srdivacky EVT VT = N->getValueType(0); 2275193323Sed DebugLoc dl = N->getDebugLoc(); 2276193323Sed if (N->getMaskElt(i) < 0) 2277193323Sed return getUNDEF(VT.getVectorElementType()); 2278193323Sed unsigned Index = N->getMaskElt(i); 2279193323Sed unsigned NumElems = VT.getVectorNumElements(); 2280193323Sed SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1); 2281193323Sed Index %= NumElems; 2282193323Sed 2283193323Sed if (V.getOpcode() == ISD::BIT_CONVERT) { 2284193323Sed V = V.getOperand(0); 2285198090Srdivacky EVT VVT = V.getValueType(); 2286193323Sed if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems) 2287193323Sed return SDValue(); 2288193323Sed } 2289193323Sed if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) 2290193323Sed return (Index == 0) ? V.getOperand(0) 2291193323Sed : getUNDEF(VT.getVectorElementType()); 2292193323Sed if (V.getOpcode() == ISD::BUILD_VECTOR) 2293193323Sed return V.getOperand(Index); 2294193323Sed if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V)) 2295193323Sed return getShuffleScalarElt(SVN, Index); 2296193323Sed return SDValue(); 2297193323Sed} 2298193323Sed 2299193323Sed 2300193323Sed/// getNode - Gets or creates the specified node. 2301193323Sed/// 2302198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) { 2303193323Sed FoldingSetNodeID ID; 2304193323Sed AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0); 2305193323Sed void *IP = 0; 2306201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2307193323Sed return SDValue(E, 0); 2308201360Srdivacky 2309193323Sed SDNode *N = NodeAllocator.Allocate<SDNode>(); 2310193323Sed new (N) SDNode(Opcode, DL, getVTList(VT)); 2311193323Sed CSEMap.InsertNode(N, IP); 2312193323Sed 2313193323Sed AllNodes.push_back(N); 2314193323Sed#ifndef NDEBUG 2315193323Sed VerifyNode(N); 2316193323Sed#endif 2317193323Sed return SDValue(N, 0); 2318193323Sed} 2319193323Sed 2320193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, 2321198090Srdivacky EVT VT, SDValue Operand) { 2322193323Sed // Constant fold unary operations with an integer constant operand. 2323193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) { 2324193323Sed const APInt &Val = C->getAPIntValue(); 2325193323Sed unsigned BitWidth = VT.getSizeInBits(); 2326193323Sed switch (Opcode) { 2327193323Sed default: break; 2328193323Sed case ISD::SIGN_EXTEND: 2329193323Sed return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT); 2330193323Sed case ISD::ANY_EXTEND: 2331193323Sed case ISD::ZERO_EXTEND: 2332193323Sed case ISD::TRUNCATE: 2333193323Sed return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT); 2334193323Sed case ISD::UINT_TO_FP: 2335193323Sed case ISD::SINT_TO_FP: { 2336193323Sed const uint64_t zero[] = {0, 0}; 2337193323Sed // No compile time operations on this type. 2338193323Sed if (VT==MVT::ppcf128) 2339193323Sed break; 2340193323Sed APFloat apf = APFloat(APInt(BitWidth, 2, zero)); 2341193323Sed (void)apf.convertFromAPInt(Val, 2342193323Sed Opcode==ISD::SINT_TO_FP, 2343193323Sed APFloat::rmNearestTiesToEven); 2344193323Sed return getConstantFP(apf, VT); 2345193323Sed } 2346193323Sed case ISD::BIT_CONVERT: 2347193323Sed if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) 2348193323Sed return getConstantFP(Val.bitsToFloat(), VT); 2349193323Sed else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) 2350193323Sed return getConstantFP(Val.bitsToDouble(), VT); 2351193323Sed break; 2352193323Sed case ISD::BSWAP: 2353193323Sed return getConstant(Val.byteSwap(), VT); 2354193323Sed case ISD::CTPOP: 2355193323Sed return getConstant(Val.countPopulation(), VT); 2356193323Sed case ISD::CTLZ: 2357193323Sed return getConstant(Val.countLeadingZeros(), VT); 2358193323Sed case ISD::CTTZ: 2359193323Sed return getConstant(Val.countTrailingZeros(), VT); 2360193323Sed } 2361193323Sed } 2362193323Sed 2363193323Sed // Constant fold unary operations with a floating point constant operand. 2364193323Sed if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) { 2365193323Sed APFloat V = C->getValueAPF(); // make copy 2366193323Sed if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) { 2367193323Sed switch (Opcode) { 2368193323Sed case ISD::FNEG: 2369193323Sed V.changeSign(); 2370193323Sed return getConstantFP(V, VT); 2371193323Sed case ISD::FABS: 2372193323Sed V.clearSign(); 2373193323Sed return getConstantFP(V, VT); 2374193323Sed case ISD::FP_ROUND: 2375193323Sed case ISD::FP_EXTEND: { 2376193323Sed bool ignored; 2377193323Sed // This can return overflow, underflow, or inexact; we don't care. 2378193323Sed // FIXME need to be more flexible about rounding mode. 2379198090Srdivacky (void)V.convert(*EVTToAPFloatSemantics(VT), 2380193323Sed APFloat::rmNearestTiesToEven, &ignored); 2381193323Sed return getConstantFP(V, VT); 2382193323Sed } 2383193323Sed case ISD::FP_TO_SINT: 2384193323Sed case ISD::FP_TO_UINT: { 2385193323Sed integerPart x[2]; 2386193323Sed bool ignored; 2387193323Sed assert(integerPartWidth >= 64); 2388193323Sed // FIXME need to be more flexible about rounding mode. 2389193323Sed APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(), 2390193323Sed Opcode==ISD::FP_TO_SINT, 2391193323Sed APFloat::rmTowardZero, &ignored); 2392193323Sed if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual 2393193323Sed break; 2394193323Sed APInt api(VT.getSizeInBits(), 2, x); 2395193323Sed return getConstant(api, VT); 2396193323Sed } 2397193323Sed case ISD::BIT_CONVERT: 2398193323Sed if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) 2399193323Sed return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT); 2400193323Sed else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) 2401193323Sed return getConstant(V.bitcastToAPInt().getZExtValue(), VT); 2402193323Sed break; 2403193323Sed } 2404193323Sed } 2405193323Sed } 2406193323Sed 2407193323Sed unsigned OpOpcode = Operand.getNode()->getOpcode(); 2408193323Sed switch (Opcode) { 2409193323Sed case ISD::TokenFactor: 2410193323Sed case ISD::MERGE_VALUES: 2411193323Sed case ISD::CONCAT_VECTORS: 2412193323Sed return Operand; // Factor, merge or concat of one node? No need. 2413198090Srdivacky case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node"); 2414193323Sed case ISD::FP_EXTEND: 2415193323Sed assert(VT.isFloatingPoint() && 2416193323Sed Operand.getValueType().isFloatingPoint() && "Invalid FP cast!"); 2417193323Sed if (Operand.getValueType() == VT) return Operand; // noop conversion. 2418200581Srdivacky assert((!VT.isVector() || 2419200581Srdivacky VT.getVectorNumElements() == 2420200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2421200581Srdivacky "Vector element count mismatch!"); 2422193323Sed if (Operand.getOpcode() == ISD::UNDEF) 2423193323Sed return getUNDEF(VT); 2424193323Sed break; 2425193323Sed case ISD::SIGN_EXTEND: 2426193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2427193323Sed "Invalid SIGN_EXTEND!"); 2428193323Sed if (Operand.getValueType() == VT) return Operand; // noop extension 2429200581Srdivacky assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2430200581Srdivacky "Invalid sext node, dst < src!"); 2431200581Srdivacky assert((!VT.isVector() || 2432200581Srdivacky VT.getVectorNumElements() == 2433200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2434200581Srdivacky "Vector element count mismatch!"); 2435193323Sed if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 2436193323Sed return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2437193323Sed break; 2438193323Sed case ISD::ZERO_EXTEND: 2439193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2440193323Sed "Invalid ZERO_EXTEND!"); 2441193323Sed if (Operand.getValueType() == VT) return Operand; // noop extension 2442200581Srdivacky assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2443200581Srdivacky "Invalid zext node, dst < src!"); 2444200581Srdivacky assert((!VT.isVector() || 2445200581Srdivacky VT.getVectorNumElements() == 2446200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2447200581Srdivacky "Vector element count mismatch!"); 2448193323Sed if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 2449193323Sed return getNode(ISD::ZERO_EXTEND, DL, VT, 2450193323Sed Operand.getNode()->getOperand(0)); 2451193323Sed break; 2452193323Sed case ISD::ANY_EXTEND: 2453193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2454193323Sed "Invalid ANY_EXTEND!"); 2455193323Sed if (Operand.getValueType() == VT) return Operand; // noop extension 2456200581Srdivacky assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2457200581Srdivacky "Invalid anyext node, dst < src!"); 2458200581Srdivacky assert((!VT.isVector() || 2459200581Srdivacky VT.getVectorNumElements() == 2460200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2461200581Srdivacky "Vector element count mismatch!"); 2462193323Sed if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) 2463193323Sed // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 2464193323Sed return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2465193323Sed break; 2466193323Sed case ISD::TRUNCATE: 2467193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2468193323Sed "Invalid TRUNCATE!"); 2469193323Sed if (Operand.getValueType() == VT) return Operand; // noop truncate 2470200581Srdivacky assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) && 2471200581Srdivacky "Invalid truncate node, src < dst!"); 2472200581Srdivacky assert((!VT.isVector() || 2473200581Srdivacky VT.getVectorNumElements() == 2474200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2475200581Srdivacky "Vector element count mismatch!"); 2476193323Sed if (OpOpcode == ISD::TRUNCATE) 2477193323Sed return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); 2478193323Sed else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 2479193323Sed OpOpcode == ISD::ANY_EXTEND) { 2480193323Sed // If the source is smaller than the dest, we still need an extend. 2481200581Srdivacky if (Operand.getNode()->getOperand(0).getValueType().getScalarType() 2482200581Srdivacky .bitsLT(VT.getScalarType())) 2483193323Sed return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2484193323Sed else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT)) 2485193323Sed return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); 2486193323Sed else 2487193323Sed return Operand.getNode()->getOperand(0); 2488193323Sed } 2489193323Sed break; 2490193323Sed case ISD::BIT_CONVERT: 2491193323Sed // Basic sanity checking. 2492193323Sed assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits() 2493193323Sed && "Cannot BIT_CONVERT between types of different sizes!"); 2494193323Sed if (VT == Operand.getValueType()) return Operand; // noop conversion. 2495193323Sed if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) 2496193323Sed return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0)); 2497193323Sed if (OpOpcode == ISD::UNDEF) 2498193323Sed return getUNDEF(VT); 2499193323Sed break; 2500193323Sed case ISD::SCALAR_TO_VECTOR: 2501193323Sed assert(VT.isVector() && !Operand.getValueType().isVector() && 2502193323Sed (VT.getVectorElementType() == Operand.getValueType() || 2503193323Sed (VT.getVectorElementType().isInteger() && 2504193323Sed Operand.getValueType().isInteger() && 2505193323Sed VT.getVectorElementType().bitsLE(Operand.getValueType()))) && 2506193323Sed "Illegal SCALAR_TO_VECTOR node!"); 2507193323Sed if (OpOpcode == ISD::UNDEF) 2508193323Sed return getUNDEF(VT); 2509193323Sed // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined. 2510193323Sed if (OpOpcode == ISD::EXTRACT_VECTOR_ELT && 2511193323Sed isa<ConstantSDNode>(Operand.getOperand(1)) && 2512193323Sed Operand.getConstantOperandVal(1) == 0 && 2513193323Sed Operand.getOperand(0).getValueType() == VT) 2514193323Sed return Operand.getOperand(0); 2515193323Sed break; 2516193323Sed case ISD::FNEG: 2517193323Sed // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0 2518193323Sed if (UnsafeFPMath && OpOpcode == ISD::FSUB) 2519193323Sed return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1), 2520193323Sed Operand.getNode()->getOperand(0)); 2521193323Sed if (OpOpcode == ISD::FNEG) // --X -> X 2522193323Sed return Operand.getNode()->getOperand(0); 2523193323Sed break; 2524193323Sed case ISD::FABS: 2525193323Sed if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 2526193323Sed return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0)); 2527193323Sed break; 2528193323Sed } 2529193323Sed 2530193323Sed SDNode *N; 2531193323Sed SDVTList VTs = getVTList(VT); 2532193323Sed if (VT != MVT::Flag) { // Don't CSE flag producing nodes 2533193323Sed FoldingSetNodeID ID; 2534193323Sed SDValue Ops[1] = { Operand }; 2535193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 1); 2536193323Sed void *IP = 0; 2537201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2538193323Sed return SDValue(E, 0); 2539201360Srdivacky 2540193323Sed N = NodeAllocator.Allocate<UnarySDNode>(); 2541193323Sed new (N) UnarySDNode(Opcode, DL, VTs, Operand); 2542193323Sed CSEMap.InsertNode(N, IP); 2543193323Sed } else { 2544193323Sed N = NodeAllocator.Allocate<UnarySDNode>(); 2545193323Sed new (N) UnarySDNode(Opcode, DL, VTs, Operand); 2546193323Sed } 2547193323Sed 2548193323Sed AllNodes.push_back(N); 2549193323Sed#ifndef NDEBUG 2550193323Sed VerifyNode(N); 2551193323Sed#endif 2552193323Sed return SDValue(N, 0); 2553193323Sed} 2554193323Sed 2555193323SedSDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, 2556198090Srdivacky EVT VT, 2557193323Sed ConstantSDNode *Cst1, 2558193323Sed ConstantSDNode *Cst2) { 2559193323Sed const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue(); 2560193323Sed 2561193323Sed switch (Opcode) { 2562193323Sed case ISD::ADD: return getConstant(C1 + C2, VT); 2563193323Sed case ISD::SUB: return getConstant(C1 - C2, VT); 2564193323Sed case ISD::MUL: return getConstant(C1 * C2, VT); 2565193323Sed case ISD::UDIV: 2566193323Sed if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT); 2567193323Sed break; 2568193323Sed case ISD::UREM: 2569193323Sed if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT); 2570193323Sed break; 2571193323Sed case ISD::SDIV: 2572193323Sed if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT); 2573193323Sed break; 2574193323Sed case ISD::SREM: 2575193323Sed if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT); 2576193323Sed break; 2577193323Sed case ISD::AND: return getConstant(C1 & C2, VT); 2578193323Sed case ISD::OR: return getConstant(C1 | C2, VT); 2579193323Sed case ISD::XOR: return getConstant(C1 ^ C2, VT); 2580193323Sed case ISD::SHL: return getConstant(C1 << C2, VT); 2581193323Sed case ISD::SRL: return getConstant(C1.lshr(C2), VT); 2582193323Sed case ISD::SRA: return getConstant(C1.ashr(C2), VT); 2583193323Sed case ISD::ROTL: return getConstant(C1.rotl(C2), VT); 2584193323Sed case ISD::ROTR: return getConstant(C1.rotr(C2), VT); 2585193323Sed default: break; 2586193323Sed } 2587193323Sed 2588193323Sed return SDValue(); 2589193323Sed} 2590193323Sed 2591198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 2592193323Sed SDValue N1, SDValue N2) { 2593193323Sed ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 2594193323Sed ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); 2595193323Sed switch (Opcode) { 2596193323Sed default: break; 2597193323Sed case ISD::TokenFactor: 2598193323Sed assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 2599193323Sed N2.getValueType() == MVT::Other && "Invalid token factor!"); 2600193323Sed // Fold trivial token factors. 2601193323Sed if (N1.getOpcode() == ISD::EntryToken) return N2; 2602193323Sed if (N2.getOpcode() == ISD::EntryToken) return N1; 2603193323Sed if (N1 == N2) return N1; 2604193323Sed break; 2605193323Sed case ISD::CONCAT_VECTORS: 2606193323Sed // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to 2607193323Sed // one big BUILD_VECTOR. 2608193323Sed if (N1.getOpcode() == ISD::BUILD_VECTOR && 2609193323Sed N2.getOpcode() == ISD::BUILD_VECTOR) { 2610193323Sed SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end()); 2611193323Sed Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end()); 2612193323Sed return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size()); 2613193323Sed } 2614193323Sed break; 2615193323Sed case ISD::AND: 2616193323Sed assert(VT.isInteger() && N1.getValueType() == N2.getValueType() && 2617193323Sed N1.getValueType() == VT && "Binary operator types must match!"); 2618193323Sed // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's 2619193323Sed // worth handling here. 2620193323Sed if (N2C && N2C->isNullValue()) 2621193323Sed return N2; 2622193323Sed if (N2C && N2C->isAllOnesValue()) // X & -1 -> X 2623193323Sed return N1; 2624193323Sed break; 2625193323Sed case ISD::OR: 2626193323Sed case ISD::XOR: 2627193323Sed case ISD::ADD: 2628193323Sed case ISD::SUB: 2629193323Sed assert(VT.isInteger() && N1.getValueType() == N2.getValueType() && 2630193323Sed N1.getValueType() == VT && "Binary operator types must match!"); 2631193323Sed // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so 2632193323Sed // it's worth handling here. 2633193323Sed if (N2C && N2C->isNullValue()) 2634193323Sed return N1; 2635193323Sed break; 2636193323Sed case ISD::UDIV: 2637193323Sed case ISD::UREM: 2638193323Sed case ISD::MULHU: 2639193323Sed case ISD::MULHS: 2640193323Sed case ISD::MUL: 2641193323Sed case ISD::SDIV: 2642193323Sed case ISD::SREM: 2643193323Sed assert(VT.isInteger() && "This operator does not apply to FP types!"); 2644193323Sed // fall through 2645193323Sed case ISD::FADD: 2646193323Sed case ISD::FSUB: 2647193323Sed case ISD::FMUL: 2648193323Sed case ISD::FDIV: 2649193323Sed case ISD::FREM: 2650193323Sed if (UnsafeFPMath) { 2651193323Sed if (Opcode == ISD::FADD) { 2652193323Sed // 0+x --> x 2653193323Sed if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) 2654193323Sed if (CFP->getValueAPF().isZero()) 2655193323Sed return N2; 2656193323Sed // x+0 --> x 2657193323Sed if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) 2658193323Sed if (CFP->getValueAPF().isZero()) 2659193323Sed return N1; 2660193323Sed } else if (Opcode == ISD::FSUB) { 2661193323Sed // x-0 --> x 2662193323Sed if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) 2663193323Sed if (CFP->getValueAPF().isZero()) 2664193323Sed return N1; 2665193323Sed } 2666193323Sed } 2667193323Sed assert(N1.getValueType() == N2.getValueType() && 2668193323Sed N1.getValueType() == VT && "Binary operator types must match!"); 2669193323Sed break; 2670193323Sed case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. 2671193323Sed assert(N1.getValueType() == VT && 2672193323Sed N1.getValueType().isFloatingPoint() && 2673193323Sed N2.getValueType().isFloatingPoint() && 2674193323Sed "Invalid FCOPYSIGN!"); 2675193323Sed break; 2676193323Sed case ISD::SHL: 2677193323Sed case ISD::SRA: 2678193323Sed case ISD::SRL: 2679193323Sed case ISD::ROTL: 2680193323Sed case ISD::ROTR: 2681193323Sed assert(VT == N1.getValueType() && 2682193323Sed "Shift operators return type must be the same as their first arg"); 2683193323Sed assert(VT.isInteger() && N2.getValueType().isInteger() && 2684193323Sed "Shifts only work on integers"); 2685193323Sed 2686193323Sed // Always fold shifts of i1 values so the code generator doesn't need to 2687193323Sed // handle them. Since we know the size of the shift has to be less than the 2688193323Sed // size of the value, the shift/rotate count is guaranteed to be zero. 2689193323Sed if (VT == MVT::i1) 2690193323Sed return N1; 2691202375Srdivacky if (N2C && N2C->isNullValue()) 2692202375Srdivacky return N1; 2693193323Sed break; 2694193323Sed case ISD::FP_ROUND_INREG: { 2695198090Srdivacky EVT EVT = cast<VTSDNode>(N2)->getVT(); 2696193323Sed assert(VT == N1.getValueType() && "Not an inreg round!"); 2697193323Sed assert(VT.isFloatingPoint() && EVT.isFloatingPoint() && 2698193323Sed "Cannot FP_ROUND_INREG integer types"); 2699202375Srdivacky assert(EVT.isVector() == VT.isVector() && 2700202375Srdivacky "FP_ROUND_INREG type should be vector iff the operand " 2701202375Srdivacky "type is vector!"); 2702202375Srdivacky assert((!EVT.isVector() || 2703202375Srdivacky EVT.getVectorNumElements() == VT.getVectorNumElements()) && 2704202375Srdivacky "Vector element counts must match in FP_ROUND_INREG"); 2705193323Sed assert(EVT.bitsLE(VT) && "Not rounding down!"); 2706193323Sed if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 2707193323Sed break; 2708193323Sed } 2709193323Sed case ISD::FP_ROUND: 2710193323Sed assert(VT.isFloatingPoint() && 2711193323Sed N1.getValueType().isFloatingPoint() && 2712193323Sed VT.bitsLE(N1.getValueType()) && 2713193323Sed isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!"); 2714193323Sed if (N1.getValueType() == VT) return N1; // noop conversion. 2715193323Sed break; 2716193323Sed case ISD::AssertSext: 2717193323Sed case ISD::AssertZext: { 2718198090Srdivacky EVT EVT = cast<VTSDNode>(N2)->getVT(); 2719193323Sed assert(VT == N1.getValueType() && "Not an inreg extend!"); 2720193323Sed assert(VT.isInteger() && EVT.isInteger() && 2721193323Sed "Cannot *_EXTEND_INREG FP types"); 2722200581Srdivacky assert(!EVT.isVector() && 2723200581Srdivacky "AssertSExt/AssertZExt type should be the vector element type " 2724200581Srdivacky "rather than the vector type!"); 2725193323Sed assert(EVT.bitsLE(VT) && "Not extending!"); 2726193323Sed if (VT == EVT) return N1; // noop assertion. 2727193323Sed break; 2728193323Sed } 2729193323Sed case ISD::SIGN_EXTEND_INREG: { 2730198090Srdivacky EVT EVT = cast<VTSDNode>(N2)->getVT(); 2731193323Sed assert(VT == N1.getValueType() && "Not an inreg extend!"); 2732193323Sed assert(VT.isInteger() && EVT.isInteger() && 2733193323Sed "Cannot *_EXTEND_INREG FP types"); 2734202375Srdivacky assert(EVT.isVector() == VT.isVector() && 2735202375Srdivacky "SIGN_EXTEND_INREG type should be vector iff the operand " 2736202375Srdivacky "type is vector!"); 2737202375Srdivacky assert((!EVT.isVector() || 2738202375Srdivacky EVT.getVectorNumElements() == VT.getVectorNumElements()) && 2739202375Srdivacky "Vector element counts must match in SIGN_EXTEND_INREG"); 2740202375Srdivacky assert(EVT.bitsLE(VT) && "Not extending!"); 2741193323Sed if (EVT == VT) return N1; // Not actually extending 2742193323Sed 2743193323Sed if (N1C) { 2744193323Sed APInt Val = N1C->getAPIntValue(); 2745202375Srdivacky unsigned FromBits = EVT.getScalarType().getSizeInBits(); 2746193323Sed Val <<= Val.getBitWidth()-FromBits; 2747193323Sed Val = Val.ashr(Val.getBitWidth()-FromBits); 2748193323Sed return getConstant(Val, VT); 2749193323Sed } 2750193323Sed break; 2751193323Sed } 2752193323Sed case ISD::EXTRACT_VECTOR_ELT: 2753193323Sed // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF. 2754193323Sed if (N1.getOpcode() == ISD::UNDEF) 2755193323Sed return getUNDEF(VT); 2756193323Sed 2757193323Sed // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is 2758193323Sed // expanding copies of large vectors from registers. 2759193323Sed if (N2C && 2760193323Sed N1.getOpcode() == ISD::CONCAT_VECTORS && 2761193323Sed N1.getNumOperands() > 0) { 2762193323Sed unsigned Factor = 2763193323Sed N1.getOperand(0).getValueType().getVectorNumElements(); 2764193323Sed return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, 2765193323Sed N1.getOperand(N2C->getZExtValue() / Factor), 2766193323Sed getConstant(N2C->getZExtValue() % Factor, 2767193323Sed N2.getValueType())); 2768193323Sed } 2769193323Sed 2770193323Sed // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is 2771193323Sed // expanding large vector constants. 2772193323Sed if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) { 2773193323Sed SDValue Elt = N1.getOperand(N2C->getZExtValue()); 2774198090Srdivacky EVT VEltTy = N1.getValueType().getVectorElementType(); 2775198090Srdivacky if (Elt.getValueType() != VEltTy) { 2776193323Sed // If the vector element type is not legal, the BUILD_VECTOR operands 2777193323Sed // are promoted and implicitly truncated. Make that explicit here. 2778198090Srdivacky Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt); 2779193323Sed } 2780198090Srdivacky if (VT != VEltTy) { 2781198090Srdivacky // If the vector element type is not legal, the EXTRACT_VECTOR_ELT 2782198090Srdivacky // result is implicitly extended. 2783198090Srdivacky Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt); 2784198090Srdivacky } 2785193323Sed return Elt; 2786193323Sed } 2787193323Sed 2788193323Sed // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector 2789193323Sed // operations are lowered to scalars. 2790193323Sed if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) { 2791203954Srdivacky // If the indices are the same, return the inserted element else 2792203954Srdivacky // if the indices are known different, extract the element from 2793193323Sed // the original vector. 2794203954Srdivacky if (N1.getOperand(2) == N2) { 2795203954Srdivacky if (VT == N1.getOperand(1).getValueType()) 2796203954Srdivacky return N1.getOperand(1); 2797203954Srdivacky else 2798203954Srdivacky return getSExtOrTrunc(N1.getOperand(1), DL, VT); 2799203954Srdivacky } else if (isa<ConstantSDNode>(N1.getOperand(2)) && 2800203954Srdivacky isa<ConstantSDNode>(N2)) 2801193323Sed return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2); 2802193323Sed } 2803193323Sed break; 2804193323Sed case ISD::EXTRACT_ELEMENT: 2805193323Sed assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!"); 2806193323Sed assert(!N1.getValueType().isVector() && !VT.isVector() && 2807193323Sed (N1.getValueType().isInteger() == VT.isInteger()) && 2808193323Sed "Wrong types for EXTRACT_ELEMENT!"); 2809193323Sed 2810193323Sed // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding 2811193323Sed // 64-bit integers into 32-bit parts. Instead of building the extract of 2812193323Sed // the BUILD_PAIR, only to have legalize rip it apart, just do it now. 2813193323Sed if (N1.getOpcode() == ISD::BUILD_PAIR) 2814193323Sed return N1.getOperand(N2C->getZExtValue()); 2815193323Sed 2816193323Sed // EXTRACT_ELEMENT of a constant int is also very common. 2817193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 2818193323Sed unsigned ElementSize = VT.getSizeInBits(); 2819193323Sed unsigned Shift = ElementSize * N2C->getZExtValue(); 2820193323Sed APInt ShiftedVal = C->getAPIntValue().lshr(Shift); 2821193323Sed return getConstant(ShiftedVal.trunc(ElementSize), VT); 2822193323Sed } 2823193323Sed break; 2824193323Sed case ISD::EXTRACT_SUBVECTOR: 2825193323Sed if (N1.getValueType() == VT) // Trivial extraction. 2826193323Sed return N1; 2827193323Sed break; 2828193323Sed } 2829193323Sed 2830193323Sed if (N1C) { 2831193323Sed if (N2C) { 2832193323Sed SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C); 2833193323Sed if (SV.getNode()) return SV; 2834193323Sed } else { // Cannonicalize constant to RHS if commutative 2835193323Sed if (isCommutativeBinOp(Opcode)) { 2836193323Sed std::swap(N1C, N2C); 2837193323Sed std::swap(N1, N2); 2838193323Sed } 2839193323Sed } 2840193323Sed } 2841193323Sed 2842193323Sed // Constant fold FP operations. 2843193323Sed ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode()); 2844193323Sed ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode()); 2845193323Sed if (N1CFP) { 2846193323Sed if (!N2CFP && isCommutativeBinOp(Opcode)) { 2847193323Sed // Cannonicalize constant to RHS if commutative 2848193323Sed std::swap(N1CFP, N2CFP); 2849193323Sed std::swap(N1, N2); 2850193323Sed } else if (N2CFP && VT != MVT::ppcf128) { 2851193323Sed APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF(); 2852193323Sed APFloat::opStatus s; 2853193323Sed switch (Opcode) { 2854193323Sed case ISD::FADD: 2855193323Sed s = V1.add(V2, APFloat::rmNearestTiesToEven); 2856193323Sed if (s != APFloat::opInvalidOp) 2857193323Sed return getConstantFP(V1, VT); 2858193323Sed break; 2859193323Sed case ISD::FSUB: 2860193323Sed s = V1.subtract(V2, APFloat::rmNearestTiesToEven); 2861193323Sed if (s!=APFloat::opInvalidOp) 2862193323Sed return getConstantFP(V1, VT); 2863193323Sed break; 2864193323Sed case ISD::FMUL: 2865193323Sed s = V1.multiply(V2, APFloat::rmNearestTiesToEven); 2866193323Sed if (s!=APFloat::opInvalidOp) 2867193323Sed return getConstantFP(V1, VT); 2868193323Sed break; 2869193323Sed case ISD::FDIV: 2870193323Sed s = V1.divide(V2, APFloat::rmNearestTiesToEven); 2871193323Sed if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 2872193323Sed return getConstantFP(V1, VT); 2873193323Sed break; 2874193323Sed case ISD::FREM : 2875193323Sed s = V1.mod(V2, APFloat::rmNearestTiesToEven); 2876193323Sed if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 2877193323Sed return getConstantFP(V1, VT); 2878193323Sed break; 2879193323Sed case ISD::FCOPYSIGN: 2880193323Sed V1.copySign(V2); 2881193323Sed return getConstantFP(V1, VT); 2882193323Sed default: break; 2883193323Sed } 2884193323Sed } 2885193323Sed } 2886193323Sed 2887193323Sed // Canonicalize an UNDEF to the RHS, even over a constant. 2888193323Sed if (N1.getOpcode() == ISD::UNDEF) { 2889193323Sed if (isCommutativeBinOp(Opcode)) { 2890193323Sed std::swap(N1, N2); 2891193323Sed } else { 2892193323Sed switch (Opcode) { 2893193323Sed case ISD::FP_ROUND_INREG: 2894193323Sed case ISD::SIGN_EXTEND_INREG: 2895193323Sed case ISD::SUB: 2896193323Sed case ISD::FSUB: 2897193323Sed case ISD::FDIV: 2898193323Sed case ISD::FREM: 2899193323Sed case ISD::SRA: 2900193323Sed return N1; // fold op(undef, arg2) -> undef 2901193323Sed case ISD::UDIV: 2902193323Sed case ISD::SDIV: 2903193323Sed case ISD::UREM: 2904193323Sed case ISD::SREM: 2905193323Sed case ISD::SRL: 2906193323Sed case ISD::SHL: 2907193323Sed if (!VT.isVector()) 2908193323Sed return getConstant(0, VT); // fold op(undef, arg2) -> 0 2909193323Sed // For vectors, we can't easily build an all zero vector, just return 2910193323Sed // the LHS. 2911193323Sed return N2; 2912193323Sed } 2913193323Sed } 2914193323Sed } 2915193323Sed 2916193323Sed // Fold a bunch of operators when the RHS is undef. 2917193323Sed if (N2.getOpcode() == ISD::UNDEF) { 2918193323Sed switch (Opcode) { 2919193323Sed case ISD::XOR: 2920193323Sed if (N1.getOpcode() == ISD::UNDEF) 2921193323Sed // Handle undef ^ undef -> 0 special case. This is a common 2922193323Sed // idiom (misuse). 2923193323Sed return getConstant(0, VT); 2924193323Sed // fallthrough 2925193323Sed case ISD::ADD: 2926193323Sed case ISD::ADDC: 2927193323Sed case ISD::ADDE: 2928193323Sed case ISD::SUB: 2929193574Sed case ISD::UDIV: 2930193574Sed case ISD::SDIV: 2931193574Sed case ISD::UREM: 2932193574Sed case ISD::SREM: 2933193574Sed return N2; // fold op(arg1, undef) -> undef 2934193323Sed case ISD::FADD: 2935193323Sed case ISD::FSUB: 2936193323Sed case ISD::FMUL: 2937193323Sed case ISD::FDIV: 2938193323Sed case ISD::FREM: 2939193574Sed if (UnsafeFPMath) 2940193574Sed return N2; 2941193574Sed break; 2942193323Sed case ISD::MUL: 2943193323Sed case ISD::AND: 2944193323Sed case ISD::SRL: 2945193323Sed case ISD::SHL: 2946193323Sed if (!VT.isVector()) 2947193323Sed return getConstant(0, VT); // fold op(arg1, undef) -> 0 2948193323Sed // For vectors, we can't easily build an all zero vector, just return 2949193323Sed // the LHS. 2950193323Sed return N1; 2951193323Sed case ISD::OR: 2952193323Sed if (!VT.isVector()) 2953193323Sed return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT); 2954193323Sed // For vectors, we can't easily build an all one vector, just return 2955193323Sed // the LHS. 2956193323Sed return N1; 2957193323Sed case ISD::SRA: 2958193323Sed return N1; 2959193323Sed } 2960193323Sed } 2961193323Sed 2962193323Sed // Memoize this node if possible. 2963193323Sed SDNode *N; 2964193323Sed SDVTList VTs = getVTList(VT); 2965193323Sed if (VT != MVT::Flag) { 2966193323Sed SDValue Ops[] = { N1, N2 }; 2967193323Sed FoldingSetNodeID ID; 2968193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 2); 2969193323Sed void *IP = 0; 2970201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2971193323Sed return SDValue(E, 0); 2972201360Srdivacky 2973193323Sed N = NodeAllocator.Allocate<BinarySDNode>(); 2974193323Sed new (N) BinarySDNode(Opcode, DL, VTs, N1, N2); 2975193323Sed CSEMap.InsertNode(N, IP); 2976193323Sed } else { 2977193323Sed N = NodeAllocator.Allocate<BinarySDNode>(); 2978193323Sed new (N) BinarySDNode(Opcode, DL, VTs, N1, N2); 2979193323Sed } 2980193323Sed 2981193323Sed AllNodes.push_back(N); 2982193323Sed#ifndef NDEBUG 2983193323Sed VerifyNode(N); 2984193323Sed#endif 2985193323Sed return SDValue(N, 0); 2986193323Sed} 2987193323Sed 2988198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 2989193323Sed SDValue N1, SDValue N2, SDValue N3) { 2990193323Sed // Perform various simplifications. 2991193323Sed ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 2992193323Sed ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); 2993193323Sed switch (Opcode) { 2994193323Sed case ISD::CONCAT_VECTORS: 2995193323Sed // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to 2996193323Sed // one big BUILD_VECTOR. 2997193323Sed if (N1.getOpcode() == ISD::BUILD_VECTOR && 2998193323Sed N2.getOpcode() == ISD::BUILD_VECTOR && 2999193323Sed N3.getOpcode() == ISD::BUILD_VECTOR) { 3000193323Sed SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end()); 3001193323Sed Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end()); 3002193323Sed Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end()); 3003193323Sed return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size()); 3004193323Sed } 3005193323Sed break; 3006193323Sed case ISD::SETCC: { 3007193323Sed // Use FoldSetCC to simplify SETCC's. 3008193323Sed SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL); 3009193323Sed if (Simp.getNode()) return Simp; 3010193323Sed break; 3011193323Sed } 3012193323Sed case ISD::SELECT: 3013193323Sed if (N1C) { 3014193323Sed if (N1C->getZExtValue()) 3015193323Sed return N2; // select true, X, Y -> X 3016193323Sed else 3017193323Sed return N3; // select false, X, Y -> Y 3018193323Sed } 3019193323Sed 3020193323Sed if (N2 == N3) return N2; // select C, X, X -> X 3021193323Sed break; 3022193323Sed case ISD::BRCOND: 3023193323Sed if (N2C) { 3024193323Sed if (N2C->getZExtValue()) // Unconditional branch 3025193323Sed return getNode(ISD::BR, DL, MVT::Other, N1, N3); 3026193323Sed else 3027193323Sed return N1; // Never-taken branch 3028193323Sed } 3029193323Sed break; 3030193323Sed case ISD::VECTOR_SHUFFLE: 3031198090Srdivacky llvm_unreachable("should use getVectorShuffle constructor!"); 3032193323Sed break; 3033193323Sed case ISD::BIT_CONVERT: 3034193323Sed // Fold bit_convert nodes from a type to themselves. 3035193323Sed if (N1.getValueType() == VT) 3036193323Sed return N1; 3037193323Sed break; 3038193323Sed } 3039193323Sed 3040193323Sed // Memoize node if it doesn't produce a flag. 3041193323Sed SDNode *N; 3042193323Sed SDVTList VTs = getVTList(VT); 3043193323Sed if (VT != MVT::Flag) { 3044193323Sed SDValue Ops[] = { N1, N2, N3 }; 3045193323Sed FoldingSetNodeID ID; 3046193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 3); 3047193323Sed void *IP = 0; 3048201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 3049193323Sed return SDValue(E, 0); 3050201360Srdivacky 3051193323Sed N = NodeAllocator.Allocate<TernarySDNode>(); 3052193323Sed new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3); 3053193323Sed CSEMap.InsertNode(N, IP); 3054193323Sed } else { 3055193323Sed N = NodeAllocator.Allocate<TernarySDNode>(); 3056193323Sed new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3); 3057193323Sed } 3058200581Srdivacky 3059193323Sed AllNodes.push_back(N); 3060193323Sed#ifndef NDEBUG 3061193323Sed VerifyNode(N); 3062193323Sed#endif 3063193323Sed return SDValue(N, 0); 3064193323Sed} 3065193323Sed 3066198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 3067193323Sed SDValue N1, SDValue N2, SDValue N3, 3068193323Sed SDValue N4) { 3069193323Sed SDValue Ops[] = { N1, N2, N3, N4 }; 3070193323Sed return getNode(Opcode, DL, VT, Ops, 4); 3071193323Sed} 3072193323Sed 3073198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 3074193323Sed SDValue N1, SDValue N2, SDValue N3, 3075193323Sed SDValue N4, SDValue N5) { 3076193323Sed SDValue Ops[] = { N1, N2, N3, N4, N5 }; 3077193323Sed return getNode(Opcode, DL, VT, Ops, 5); 3078193323Sed} 3079193323Sed 3080198090Srdivacky/// getStackArgumentTokenFactor - Compute a TokenFactor to force all 3081198090Srdivacky/// the incoming stack arguments to be loaded from the stack. 3082198090SrdivackySDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) { 3083198090Srdivacky SmallVector<SDValue, 8> ArgChains; 3084198090Srdivacky 3085198090Srdivacky // Include the original chain at the beginning of the list. When this is 3086198090Srdivacky // used by target LowerCall hooks, this helps legalize find the 3087198090Srdivacky // CALLSEQ_BEGIN node. 3088198090Srdivacky ArgChains.push_back(Chain); 3089198090Srdivacky 3090198090Srdivacky // Add a chain value for each stack argument. 3091198090Srdivacky for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(), 3092198090Srdivacky UE = getEntryNode().getNode()->use_end(); U != UE; ++U) 3093198090Srdivacky if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) 3094198090Srdivacky if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) 3095198090Srdivacky if (FI->getIndex() < 0) 3096198090Srdivacky ArgChains.push_back(SDValue(L, 1)); 3097198090Srdivacky 3098198090Srdivacky // Build a tokenfactor for all the chains. 3099198090Srdivacky return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other, 3100198090Srdivacky &ArgChains[0], ArgChains.size()); 3101198090Srdivacky} 3102198090Srdivacky 3103193323Sed/// getMemsetValue - Vectorized representation of the memset value 3104193323Sed/// operand. 3105198090Srdivackystatic SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG, 3106193323Sed DebugLoc dl) { 3107204642Srdivacky unsigned NumBits = VT.getScalarType().getSizeInBits(); 3108193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { 3109193323Sed APInt Val = APInt(NumBits, C->getZExtValue() & 255); 3110193323Sed unsigned Shift = 8; 3111193323Sed for (unsigned i = NumBits; i > 8; i >>= 1) { 3112193323Sed Val = (Val << Shift) | Val; 3113193323Sed Shift <<= 1; 3114193323Sed } 3115193323Sed if (VT.isInteger()) 3116193323Sed return DAG.getConstant(Val, VT); 3117193323Sed return DAG.getConstantFP(APFloat(Val), VT); 3118193323Sed } 3119193323Sed 3120193323Sed const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3121193323Sed Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value); 3122193323Sed unsigned Shift = 8; 3123193323Sed for (unsigned i = NumBits; i > 8; i >>= 1) { 3124193323Sed Value = DAG.getNode(ISD::OR, dl, VT, 3125193323Sed DAG.getNode(ISD::SHL, dl, VT, Value, 3126193323Sed DAG.getConstant(Shift, 3127193323Sed TLI.getShiftAmountTy())), 3128193323Sed Value); 3129193323Sed Shift <<= 1; 3130193323Sed } 3131193323Sed 3132193323Sed return Value; 3133193323Sed} 3134193323Sed 3135193323Sed/// getMemsetStringVal - Similar to getMemsetValue. Except this is only 3136193323Sed/// used when a memcpy is turned into a memset when the source is a constant 3137193323Sed/// string ptr. 3138198090Srdivackystatic SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG, 3139198090Srdivacky const TargetLowering &TLI, 3140198090Srdivacky std::string &Str, unsigned Offset) { 3141193323Sed // Handle vector with all elements zero. 3142193323Sed if (Str.empty()) { 3143193323Sed if (VT.isInteger()) 3144193323Sed return DAG.getConstant(0, VT); 3145193323Sed unsigned NumElts = VT.getVectorNumElements(); 3146193323Sed MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64; 3147193323Sed return DAG.getNode(ISD::BIT_CONVERT, dl, VT, 3148198090Srdivacky DAG.getConstant(0, 3149198090Srdivacky EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts))); 3150193323Sed } 3151193323Sed 3152193323Sed assert(!VT.isVector() && "Can't handle vector type here!"); 3153193323Sed unsigned NumBits = VT.getSizeInBits(); 3154193323Sed unsigned MSB = NumBits / 8; 3155193323Sed uint64_t Val = 0; 3156193323Sed if (TLI.isLittleEndian()) 3157193323Sed Offset = Offset + MSB - 1; 3158193323Sed for (unsigned i = 0; i != MSB; ++i) { 3159193323Sed Val = (Val << 8) | (unsigned char)Str[Offset]; 3160193323Sed Offset += TLI.isLittleEndian() ? -1 : 1; 3161193323Sed } 3162193323Sed return DAG.getConstant(Val, VT); 3163193323Sed} 3164193323Sed 3165193323Sed/// getMemBasePlusOffset - Returns base and offset node for the 3166193323Sed/// 3167193323Sedstatic SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, 3168193323Sed SelectionDAG &DAG) { 3169198090Srdivacky EVT VT = Base.getValueType(); 3170193323Sed return DAG.getNode(ISD::ADD, Base.getDebugLoc(), 3171193323Sed VT, Base, DAG.getConstant(Offset, VT)); 3172193323Sed} 3173193323Sed 3174193323Sed/// isMemSrcFromString - Returns true if memcpy source is a string constant. 3175193323Sed/// 3176193323Sedstatic bool isMemSrcFromString(SDValue Src, std::string &Str) { 3177193323Sed unsigned SrcDelta = 0; 3178193323Sed GlobalAddressSDNode *G = NULL; 3179193323Sed if (Src.getOpcode() == ISD::GlobalAddress) 3180193323Sed G = cast<GlobalAddressSDNode>(Src); 3181193323Sed else if (Src.getOpcode() == ISD::ADD && 3182193323Sed Src.getOperand(0).getOpcode() == ISD::GlobalAddress && 3183193323Sed Src.getOperand(1).getOpcode() == ISD::Constant) { 3184193323Sed G = cast<GlobalAddressSDNode>(Src.getOperand(0)); 3185193323Sed SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue(); 3186193323Sed } 3187193323Sed if (!G) 3188193323Sed return false; 3189193323Sed 3190193323Sed GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal()); 3191193323Sed if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false)) 3192193323Sed return true; 3193193323Sed 3194193323Sed return false; 3195193323Sed} 3196193323Sed 3197193323Sed/// MeetsMaxMemopRequirement - Determines if the number of memory ops required 3198193323Sed/// to replace the memset / memcpy is below the threshold. It also returns the 3199193323Sed/// types of the sequence of memory ops to perform memset / memcpy. 3200193323Sedstatic 3201198090Srdivackybool MeetsMaxMemopRequirement(std::vector<EVT> &MemOps, 3202193323Sed SDValue Dst, SDValue Src, 3203193323Sed unsigned Limit, uint64_t Size, unsigned &Align, 3204193323Sed std::string &Str, bool &isSrcStr, 3205193323Sed SelectionDAG &DAG, 3206193323Sed const TargetLowering &TLI) { 3207193323Sed isSrcStr = isMemSrcFromString(Src, Str); 3208193323Sed bool isSrcConst = isa<ConstantSDNode>(Src); 3209198090Srdivacky EVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr, DAG); 3210198090Srdivacky bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses(VT); 3211204961Srdivacky if (VT != MVT::Other) { 3212198090Srdivacky const Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 3213198090Srdivacky unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty); 3214193323Sed // If source is a string constant, this will require an unaligned load. 3215193323Sed if (NewAlign > Align && (isSrcConst || AllowUnalign)) { 3216193323Sed if (Dst.getOpcode() != ISD::FrameIndex) { 3217193323Sed // Can't change destination alignment. It requires a unaligned store. 3218193323Sed if (AllowUnalign) 3219204961Srdivacky VT = MVT::Other; 3220193323Sed } else { 3221193323Sed int FI = cast<FrameIndexSDNode>(Dst)->getIndex(); 3222193323Sed MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 3223193323Sed if (MFI->isFixedObjectIndex(FI)) { 3224193323Sed // Can't change destination alignment. It requires a unaligned store. 3225193323Sed if (AllowUnalign) 3226204961Srdivacky VT = MVT::Other; 3227193323Sed } else { 3228193323Sed // Give the stack frame object a larger alignment if needed. 3229193323Sed if (MFI->getObjectAlignment(FI) < NewAlign) 3230193323Sed MFI->setObjectAlignment(FI, NewAlign); 3231193323Sed Align = NewAlign; 3232193323Sed } 3233193323Sed } 3234193323Sed } 3235193323Sed } 3236193323Sed 3237204961Srdivacky if (VT == MVT::Other) { 3238198090Srdivacky if (TLI.allowsUnalignedMemoryAccesses(MVT::i64)) { 3239193323Sed VT = MVT::i64; 3240193323Sed } else { 3241193323Sed switch (Align & 7) { 3242193323Sed case 0: VT = MVT::i64; break; 3243193323Sed case 4: VT = MVT::i32; break; 3244193323Sed case 2: VT = MVT::i16; break; 3245193323Sed default: VT = MVT::i8; break; 3246193323Sed } 3247193323Sed } 3248193323Sed 3249193323Sed MVT LVT = MVT::i64; 3250193323Sed while (!TLI.isTypeLegal(LVT)) 3251198090Srdivacky LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1); 3252193323Sed assert(LVT.isInteger()); 3253193323Sed 3254193323Sed if (VT.bitsGT(LVT)) 3255193323Sed VT = LVT; 3256193323Sed } 3257193323Sed 3258193323Sed unsigned NumMemOps = 0; 3259193323Sed while (Size != 0) { 3260193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3261193323Sed while (VTSize > Size) { 3262193323Sed // For now, only use non-vector load / store's for the left-over pieces. 3263193323Sed if (VT.isVector()) { 3264193323Sed VT = MVT::i64; 3265193323Sed while (!TLI.isTypeLegal(VT)) 3266198090Srdivacky VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1); 3267193323Sed VTSize = VT.getSizeInBits() / 8; 3268193323Sed } else { 3269194710Sed // This can result in a type that is not legal on the target, e.g. 3270194710Sed // 1 or 2 bytes on PPC. 3271198090Srdivacky VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1); 3272193323Sed VTSize >>= 1; 3273193323Sed } 3274193323Sed } 3275193323Sed 3276193323Sed if (++NumMemOps > Limit) 3277193323Sed return false; 3278193323Sed MemOps.push_back(VT); 3279193323Sed Size -= VTSize; 3280193323Sed } 3281193323Sed 3282193323Sed return true; 3283193323Sed} 3284193323Sed 3285193323Sedstatic SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl, 3286193323Sed SDValue Chain, SDValue Dst, 3287193323Sed SDValue Src, uint64_t Size, 3288193323Sed unsigned Align, bool AlwaysInline, 3289193323Sed const Value *DstSV, uint64_t DstSVOff, 3290193323Sed const Value *SrcSV, uint64_t SrcSVOff){ 3291193323Sed const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3292193323Sed 3293193323Sed // Expand memcpy to a series of load and store ops if the size operand falls 3294193323Sed // below a certain threshold. 3295198090Srdivacky std::vector<EVT> MemOps; 3296193323Sed uint64_t Limit = -1ULL; 3297193323Sed if (!AlwaysInline) 3298193323Sed Limit = TLI.getMaxStoresPerMemcpy(); 3299193323Sed unsigned DstAlign = Align; // Destination alignment can change. 3300193323Sed std::string Str; 3301193323Sed bool CopyFromStr; 3302193323Sed if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign, 3303193323Sed Str, CopyFromStr, DAG, TLI)) 3304193323Sed return SDValue(); 3305193323Sed 3306193323Sed 3307193323Sed bool isZeroStr = CopyFromStr && Str.empty(); 3308193323Sed SmallVector<SDValue, 8> OutChains; 3309193323Sed unsigned NumMemOps = MemOps.size(); 3310193323Sed uint64_t SrcOff = 0, DstOff = 0; 3311198090Srdivacky for (unsigned i = 0; i != NumMemOps; ++i) { 3312198090Srdivacky EVT VT = MemOps[i]; 3313193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3314193323Sed SDValue Value, Store; 3315193323Sed 3316193323Sed if (CopyFromStr && (isZeroStr || !VT.isVector())) { 3317193323Sed // It's unlikely a store of a vector immediate can be done in a single 3318193323Sed // instruction. It would require a load from a constantpool first. 3319193323Sed // We also handle store a vector with all zero's. 3320193323Sed // FIXME: Handle other cases where store of vector immediate is done in 3321193323Sed // a single instruction. 3322193323Sed Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff); 3323193323Sed Store = DAG.getStore(Chain, dl, Value, 3324193323Sed getMemBasePlusOffset(Dst, DstOff, DAG), 3325203954Srdivacky DstSV, DstSVOff + DstOff, false, false, DstAlign); 3326193323Sed } else { 3327194710Sed // The type might not be legal for the target. This should only happen 3328194710Sed // if the type is smaller than a legal type, as on PPC, so the right 3329195098Sed // thing to do is generate a LoadExt/StoreTrunc pair. These simplify 3330195098Sed // to Load/Store if NVT==VT. 3331194710Sed // FIXME does the case above also need this? 3332198090Srdivacky EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 3333195098Sed assert(NVT.bitsGE(VT)); 3334195098Sed Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain, 3335195098Sed getMemBasePlusOffset(Src, SrcOff, DAG), 3336203954Srdivacky SrcSV, SrcSVOff + SrcOff, VT, false, false, Align); 3337195098Sed Store = DAG.getTruncStore(Chain, dl, Value, 3338203954Srdivacky getMemBasePlusOffset(Dst, DstOff, DAG), 3339203954Srdivacky DstSV, DstSVOff + DstOff, VT, false, false, 3340203954Srdivacky DstAlign); 3341193323Sed } 3342193323Sed OutChains.push_back(Store); 3343193323Sed SrcOff += VTSize; 3344193323Sed DstOff += VTSize; 3345193323Sed } 3346193323Sed 3347193323Sed return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3348193323Sed &OutChains[0], OutChains.size()); 3349193323Sed} 3350193323Sed 3351193323Sedstatic SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl, 3352193323Sed SDValue Chain, SDValue Dst, 3353193323Sed SDValue Src, uint64_t Size, 3354193323Sed unsigned Align, bool AlwaysInline, 3355193323Sed const Value *DstSV, uint64_t DstSVOff, 3356193323Sed const Value *SrcSV, uint64_t SrcSVOff){ 3357193323Sed const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3358193323Sed 3359193323Sed // Expand memmove to a series of load and store ops if the size operand falls 3360193323Sed // below a certain threshold. 3361198090Srdivacky std::vector<EVT> MemOps; 3362193323Sed uint64_t Limit = -1ULL; 3363193323Sed if (!AlwaysInline) 3364193323Sed Limit = TLI.getMaxStoresPerMemmove(); 3365193323Sed unsigned DstAlign = Align; // Destination alignment can change. 3366193323Sed std::string Str; 3367193323Sed bool CopyFromStr; 3368193323Sed if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign, 3369193323Sed Str, CopyFromStr, DAG, TLI)) 3370193323Sed return SDValue(); 3371193323Sed 3372193323Sed uint64_t SrcOff = 0, DstOff = 0; 3373193323Sed 3374193323Sed SmallVector<SDValue, 8> LoadValues; 3375193323Sed SmallVector<SDValue, 8> LoadChains; 3376193323Sed SmallVector<SDValue, 8> OutChains; 3377193323Sed unsigned NumMemOps = MemOps.size(); 3378193323Sed for (unsigned i = 0; i < NumMemOps; i++) { 3379198090Srdivacky EVT VT = MemOps[i]; 3380193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3381193323Sed SDValue Value, Store; 3382193323Sed 3383193323Sed Value = DAG.getLoad(VT, dl, Chain, 3384193323Sed getMemBasePlusOffset(Src, SrcOff, DAG), 3385203954Srdivacky SrcSV, SrcSVOff + SrcOff, false, false, Align); 3386193323Sed LoadValues.push_back(Value); 3387193323Sed LoadChains.push_back(Value.getValue(1)); 3388193323Sed SrcOff += VTSize; 3389193323Sed } 3390193323Sed Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3391193323Sed &LoadChains[0], LoadChains.size()); 3392193323Sed OutChains.clear(); 3393193323Sed for (unsigned i = 0; i < NumMemOps; i++) { 3394198090Srdivacky EVT VT = MemOps[i]; 3395193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3396193323Sed SDValue Value, Store; 3397193323Sed 3398193323Sed Store = DAG.getStore(Chain, dl, LoadValues[i], 3399193323Sed getMemBasePlusOffset(Dst, DstOff, DAG), 3400203954Srdivacky DstSV, DstSVOff + DstOff, false, false, DstAlign); 3401193323Sed OutChains.push_back(Store); 3402193323Sed DstOff += VTSize; 3403193323Sed } 3404193323Sed 3405193323Sed return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3406193323Sed &OutChains[0], OutChains.size()); 3407193323Sed} 3408193323Sed 3409193323Sedstatic SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl, 3410193323Sed SDValue Chain, SDValue Dst, 3411193323Sed SDValue Src, uint64_t Size, 3412193323Sed unsigned Align, 3413193323Sed const Value *DstSV, uint64_t DstSVOff) { 3414193323Sed const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3415193323Sed 3416193323Sed // Expand memset to a series of load/store ops if the size operand 3417193323Sed // falls below a certain threshold. 3418198090Srdivacky std::vector<EVT> MemOps; 3419193323Sed std::string Str; 3420193323Sed bool CopyFromStr; 3421193323Sed if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(), 3422193323Sed Size, Align, Str, CopyFromStr, DAG, TLI)) 3423193323Sed return SDValue(); 3424193323Sed 3425193323Sed SmallVector<SDValue, 8> OutChains; 3426193323Sed uint64_t DstOff = 0; 3427193323Sed 3428193323Sed unsigned NumMemOps = MemOps.size(); 3429193323Sed for (unsigned i = 0; i < NumMemOps; i++) { 3430198090Srdivacky EVT VT = MemOps[i]; 3431193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3432193323Sed SDValue Value = getMemsetValue(Src, VT, DAG, dl); 3433193323Sed SDValue Store = DAG.getStore(Chain, dl, Value, 3434193323Sed getMemBasePlusOffset(Dst, DstOff, DAG), 3435203954Srdivacky DstSV, DstSVOff + DstOff, false, false, 0); 3436193323Sed OutChains.push_back(Store); 3437193323Sed DstOff += VTSize; 3438193323Sed } 3439193323Sed 3440193323Sed return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3441193323Sed &OutChains[0], OutChains.size()); 3442193323Sed} 3443193323Sed 3444193323SedSDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst, 3445193323Sed SDValue Src, SDValue Size, 3446193323Sed unsigned Align, bool AlwaysInline, 3447193323Sed const Value *DstSV, uint64_t DstSVOff, 3448193323Sed const Value *SrcSV, uint64_t SrcSVOff) { 3449193323Sed 3450193323Sed // Check to see if we should lower the memcpy to loads and stores first. 3451193323Sed // For cases within the target-specified limits, this is the best choice. 3452193323Sed ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 3453193323Sed if (ConstantSize) { 3454193323Sed // Memcpy with size zero? Just return the original chain. 3455193323Sed if (ConstantSize->isNullValue()) 3456193323Sed return Chain; 3457193323Sed 3458193323Sed SDValue Result = 3459193323Sed getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, 3460193323Sed ConstantSize->getZExtValue(), 3461193323Sed Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff); 3462193323Sed if (Result.getNode()) 3463193323Sed return Result; 3464193323Sed } 3465193323Sed 3466193323Sed // Then check to see if we should lower the memcpy with target-specific 3467193323Sed // code. If the target chooses to do this, this is the next best. 3468193323Sed SDValue Result = 3469193323Sed TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align, 3470193323Sed AlwaysInline, 3471193323Sed DstSV, DstSVOff, SrcSV, SrcSVOff); 3472193323Sed if (Result.getNode()) 3473193323Sed return Result; 3474193323Sed 3475193323Sed // If we really need inline code and the target declined to provide it, 3476193323Sed // use a (potentially long) sequence of loads and stores. 3477193323Sed if (AlwaysInline) { 3478193323Sed assert(ConstantSize && "AlwaysInline requires a constant size!"); 3479193323Sed return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, 3480193323Sed ConstantSize->getZExtValue(), Align, true, 3481193323Sed DstSV, DstSVOff, SrcSV, SrcSVOff); 3482193323Sed } 3483193323Sed 3484193323Sed // Emit a library call. 3485193323Sed TargetLowering::ArgListTy Args; 3486193323Sed TargetLowering::ArgListEntry Entry; 3487198090Srdivacky Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext()); 3488193323Sed Entry.Node = Dst; Args.push_back(Entry); 3489193323Sed Entry.Node = Src; Args.push_back(Entry); 3490193323Sed Entry.Node = Size; Args.push_back(Entry); 3491193323Sed // FIXME: pass in DebugLoc 3492193323Sed std::pair<SDValue,SDValue> CallResult = 3493198090Srdivacky TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()), 3494198090Srdivacky false, false, false, false, 0, 3495198090Srdivacky TLI.getLibcallCallingConv(RTLIB::MEMCPY), false, 3496198090Srdivacky /*isReturnValueUsed=*/false, 3497198090Srdivacky getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY), 3498198090Srdivacky TLI.getPointerTy()), 3499204642Srdivacky Args, *this, dl); 3500193323Sed return CallResult.second; 3501193323Sed} 3502193323Sed 3503193323SedSDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst, 3504193323Sed SDValue Src, SDValue Size, 3505193323Sed unsigned Align, 3506193323Sed const Value *DstSV, uint64_t DstSVOff, 3507193323Sed const Value *SrcSV, uint64_t SrcSVOff) { 3508193323Sed 3509193323Sed // Check to see if we should lower the memmove to loads and stores first. 3510193323Sed // For cases within the target-specified limits, this is the best choice. 3511193323Sed ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 3512193323Sed if (ConstantSize) { 3513193323Sed // Memmove with size zero? Just return the original chain. 3514193323Sed if (ConstantSize->isNullValue()) 3515193323Sed return Chain; 3516193323Sed 3517193323Sed SDValue Result = 3518193323Sed getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src, 3519193323Sed ConstantSize->getZExtValue(), 3520193323Sed Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff); 3521193323Sed if (Result.getNode()) 3522193323Sed return Result; 3523193323Sed } 3524193323Sed 3525193323Sed // Then check to see if we should lower the memmove with target-specific 3526193323Sed // code. If the target chooses to do this, this is the next best. 3527193323Sed SDValue Result = 3528193323Sed TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, 3529193323Sed DstSV, DstSVOff, SrcSV, SrcSVOff); 3530193323Sed if (Result.getNode()) 3531193323Sed return Result; 3532193323Sed 3533193323Sed // Emit a library call. 3534193323Sed TargetLowering::ArgListTy Args; 3535193323Sed TargetLowering::ArgListEntry Entry; 3536198090Srdivacky Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext()); 3537193323Sed Entry.Node = Dst; Args.push_back(Entry); 3538193323Sed Entry.Node = Src; Args.push_back(Entry); 3539193323Sed Entry.Node = Size; Args.push_back(Entry); 3540193323Sed // FIXME: pass in DebugLoc 3541193323Sed std::pair<SDValue,SDValue> CallResult = 3542198090Srdivacky TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()), 3543198090Srdivacky false, false, false, false, 0, 3544198090Srdivacky TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false, 3545198090Srdivacky /*isReturnValueUsed=*/false, 3546198090Srdivacky getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE), 3547198090Srdivacky TLI.getPointerTy()), 3548204642Srdivacky Args, *this, dl); 3549193323Sed return CallResult.second; 3550193323Sed} 3551193323Sed 3552193323SedSDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst, 3553193323Sed SDValue Src, SDValue Size, 3554193323Sed unsigned Align, 3555193323Sed const Value *DstSV, uint64_t DstSVOff) { 3556193323Sed 3557193323Sed // Check to see if we should lower the memset to stores first. 3558193323Sed // For cases within the target-specified limits, this is the best choice. 3559193323Sed ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 3560193323Sed if (ConstantSize) { 3561193323Sed // Memset with size zero? Just return the original chain. 3562193323Sed if (ConstantSize->isNullValue()) 3563193323Sed return Chain; 3564193323Sed 3565193323Sed SDValue Result = 3566193323Sed getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), 3567193323Sed Align, DstSV, DstSVOff); 3568193323Sed if (Result.getNode()) 3569193323Sed return Result; 3570193323Sed } 3571193323Sed 3572193323Sed // Then check to see if we should lower the memset with target-specific 3573193323Sed // code. If the target chooses to do this, this is the next best. 3574193323Sed SDValue Result = 3575193323Sed TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, 3576193323Sed DstSV, DstSVOff); 3577193323Sed if (Result.getNode()) 3578193323Sed return Result; 3579193323Sed 3580193323Sed // Emit a library call. 3581198090Srdivacky const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext()); 3582193323Sed TargetLowering::ArgListTy Args; 3583193323Sed TargetLowering::ArgListEntry Entry; 3584193323Sed Entry.Node = Dst; Entry.Ty = IntPtrTy; 3585193323Sed Args.push_back(Entry); 3586193323Sed // Extend or truncate the argument to be an i32 value for the call. 3587193323Sed if (Src.getValueType().bitsGT(MVT::i32)) 3588193323Sed Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src); 3589193323Sed else 3590193323Sed Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src); 3591198090Srdivacky Entry.Node = Src; 3592198090Srdivacky Entry.Ty = Type::getInt32Ty(*getContext()); 3593198090Srdivacky Entry.isSExt = true; 3594193323Sed Args.push_back(Entry); 3595198090Srdivacky Entry.Node = Size; 3596198090Srdivacky Entry.Ty = IntPtrTy; 3597198090Srdivacky Entry.isSExt = false; 3598193323Sed Args.push_back(Entry); 3599193323Sed // FIXME: pass in DebugLoc 3600193323Sed std::pair<SDValue,SDValue> CallResult = 3601198090Srdivacky TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()), 3602198090Srdivacky false, false, false, false, 0, 3603198090Srdivacky TLI.getLibcallCallingConv(RTLIB::MEMSET), false, 3604198090Srdivacky /*isReturnValueUsed=*/false, 3605198090Srdivacky getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET), 3606198090Srdivacky TLI.getPointerTy()), 3607204642Srdivacky Args, *this, dl); 3608193323Sed return CallResult.second; 3609193323Sed} 3610193323Sed 3611198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3612193323Sed SDValue Chain, 3613193323Sed SDValue Ptr, SDValue Cmp, 3614193323Sed SDValue Swp, const Value* PtrVal, 3615193323Sed unsigned Alignment) { 3616198090Srdivacky if (Alignment == 0) // Ensure that codegen never sees alignment 0 3617198090Srdivacky Alignment = getEVTAlignment(MemVT); 3618198090Srdivacky 3619198090Srdivacky // Check if the memory reference references a frame index 3620198090Srdivacky if (!PtrVal) 3621198090Srdivacky if (const FrameIndexSDNode *FI = 3622198090Srdivacky dyn_cast<const FrameIndexSDNode>(Ptr.getNode())) 3623198090Srdivacky PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex()); 3624198090Srdivacky 3625198090Srdivacky MachineFunction &MF = getMachineFunction(); 3626198090Srdivacky unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 3627198090Srdivacky 3628198090Srdivacky // For now, atomics are considered to be volatile always. 3629198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3630198090Srdivacky 3631198090Srdivacky MachineMemOperand *MMO = 3632198090Srdivacky MF.getMachineMemOperand(PtrVal, Flags, 0, 3633198090Srdivacky MemVT.getStoreSize(), Alignment); 3634198090Srdivacky 3635198090Srdivacky return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO); 3636198090Srdivacky} 3637198090Srdivacky 3638198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3639198090Srdivacky SDValue Chain, 3640198090Srdivacky SDValue Ptr, SDValue Cmp, 3641198090Srdivacky SDValue Swp, MachineMemOperand *MMO) { 3642193323Sed assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op"); 3643193323Sed assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types"); 3644193323Sed 3645198090Srdivacky EVT VT = Cmp.getValueType(); 3646193323Sed 3647193323Sed SDVTList VTs = getVTList(VT, MVT::Other); 3648193323Sed FoldingSetNodeID ID; 3649193323Sed ID.AddInteger(MemVT.getRawBits()); 3650193323Sed SDValue Ops[] = {Chain, Ptr, Cmp, Swp}; 3651193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 4); 3652193323Sed void* IP = 0; 3653198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3654198090Srdivacky cast<AtomicSDNode>(E)->refineAlignment(MMO); 3655193323Sed return SDValue(E, 0); 3656198090Srdivacky } 3657193323Sed SDNode* N = NodeAllocator.Allocate<AtomicSDNode>(); 3658198090Srdivacky new (N) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, Ptr, Cmp, Swp, MMO); 3659193323Sed CSEMap.InsertNode(N, IP); 3660193323Sed AllNodes.push_back(N); 3661193323Sed return SDValue(N, 0); 3662193323Sed} 3663193323Sed 3664198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3665193323Sed SDValue Chain, 3666193323Sed SDValue Ptr, SDValue Val, 3667193323Sed const Value* PtrVal, 3668193323Sed unsigned Alignment) { 3669198090Srdivacky if (Alignment == 0) // Ensure that codegen never sees alignment 0 3670198090Srdivacky Alignment = getEVTAlignment(MemVT); 3671198090Srdivacky 3672198090Srdivacky // Check if the memory reference references a frame index 3673198090Srdivacky if (!PtrVal) 3674198090Srdivacky if (const FrameIndexSDNode *FI = 3675198090Srdivacky dyn_cast<const FrameIndexSDNode>(Ptr.getNode())) 3676198090Srdivacky PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex()); 3677198090Srdivacky 3678198090Srdivacky MachineFunction &MF = getMachineFunction(); 3679198090Srdivacky unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 3680198090Srdivacky 3681198090Srdivacky // For now, atomics are considered to be volatile always. 3682198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3683198090Srdivacky 3684198090Srdivacky MachineMemOperand *MMO = 3685198090Srdivacky MF.getMachineMemOperand(PtrVal, Flags, 0, 3686198090Srdivacky MemVT.getStoreSize(), Alignment); 3687198090Srdivacky 3688198090Srdivacky return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO); 3689198090Srdivacky} 3690198090Srdivacky 3691198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3692198090Srdivacky SDValue Chain, 3693198090Srdivacky SDValue Ptr, SDValue Val, 3694198090Srdivacky MachineMemOperand *MMO) { 3695193323Sed assert((Opcode == ISD::ATOMIC_LOAD_ADD || 3696193323Sed Opcode == ISD::ATOMIC_LOAD_SUB || 3697193323Sed Opcode == ISD::ATOMIC_LOAD_AND || 3698193323Sed Opcode == ISD::ATOMIC_LOAD_OR || 3699193323Sed Opcode == ISD::ATOMIC_LOAD_XOR || 3700193323Sed Opcode == ISD::ATOMIC_LOAD_NAND || 3701193323Sed Opcode == ISD::ATOMIC_LOAD_MIN || 3702193323Sed Opcode == ISD::ATOMIC_LOAD_MAX || 3703193323Sed Opcode == ISD::ATOMIC_LOAD_UMIN || 3704193323Sed Opcode == ISD::ATOMIC_LOAD_UMAX || 3705193323Sed Opcode == ISD::ATOMIC_SWAP) && 3706193323Sed "Invalid Atomic Op"); 3707193323Sed 3708198090Srdivacky EVT VT = Val.getValueType(); 3709193323Sed 3710193323Sed SDVTList VTs = getVTList(VT, MVT::Other); 3711193323Sed FoldingSetNodeID ID; 3712193323Sed ID.AddInteger(MemVT.getRawBits()); 3713193323Sed SDValue Ops[] = {Chain, Ptr, Val}; 3714193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 3); 3715193323Sed void* IP = 0; 3716198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3717198090Srdivacky cast<AtomicSDNode>(E)->refineAlignment(MMO); 3718193323Sed return SDValue(E, 0); 3719198090Srdivacky } 3720193323Sed SDNode* N = NodeAllocator.Allocate<AtomicSDNode>(); 3721198090Srdivacky new (N) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, Ptr, Val, MMO); 3722193323Sed CSEMap.InsertNode(N, IP); 3723193323Sed AllNodes.push_back(N); 3724193323Sed return SDValue(N, 0); 3725193323Sed} 3726193323Sed 3727193323Sed/// getMergeValues - Create a MERGE_VALUES node from the given operands. 3728193323Sed/// Allowed to return something different (and simpler) if Simplify is true. 3729193323SedSDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps, 3730193323Sed DebugLoc dl) { 3731193323Sed if (NumOps == 1) 3732193323Sed return Ops[0]; 3733193323Sed 3734198090Srdivacky SmallVector<EVT, 4> VTs; 3735193323Sed VTs.reserve(NumOps); 3736193323Sed for (unsigned i = 0; i < NumOps; ++i) 3737193323Sed VTs.push_back(Ops[i].getValueType()); 3738193323Sed return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps), 3739193323Sed Ops, NumOps); 3740193323Sed} 3741193323Sed 3742193323SedSDValue 3743193323SedSelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, 3744198090Srdivacky const EVT *VTs, unsigned NumVTs, 3745193323Sed const SDValue *Ops, unsigned NumOps, 3746198090Srdivacky EVT MemVT, const Value *srcValue, int SVOff, 3747193323Sed unsigned Align, bool Vol, 3748193323Sed bool ReadMem, bool WriteMem) { 3749193323Sed return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps, 3750193323Sed MemVT, srcValue, SVOff, Align, Vol, 3751193323Sed ReadMem, WriteMem); 3752193323Sed} 3753193323Sed 3754193323SedSDValue 3755193323SedSelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList, 3756193323Sed const SDValue *Ops, unsigned NumOps, 3757198090Srdivacky EVT MemVT, const Value *srcValue, int SVOff, 3758193323Sed unsigned Align, bool Vol, 3759193323Sed bool ReadMem, bool WriteMem) { 3760198090Srdivacky if (Align == 0) // Ensure that codegen never sees alignment 0 3761198090Srdivacky Align = getEVTAlignment(MemVT); 3762198090Srdivacky 3763198090Srdivacky MachineFunction &MF = getMachineFunction(); 3764198090Srdivacky unsigned Flags = 0; 3765198090Srdivacky if (WriteMem) 3766198090Srdivacky Flags |= MachineMemOperand::MOStore; 3767198090Srdivacky if (ReadMem) 3768198090Srdivacky Flags |= MachineMemOperand::MOLoad; 3769198090Srdivacky if (Vol) 3770198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3771198090Srdivacky MachineMemOperand *MMO = 3772198090Srdivacky MF.getMachineMemOperand(srcValue, Flags, SVOff, 3773198090Srdivacky MemVT.getStoreSize(), Align); 3774198090Srdivacky 3775198090Srdivacky return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO); 3776198090Srdivacky} 3777198090Srdivacky 3778198090SrdivackySDValue 3779198090SrdivackySelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList, 3780198090Srdivacky const SDValue *Ops, unsigned NumOps, 3781198090Srdivacky EVT MemVT, MachineMemOperand *MMO) { 3782198090Srdivacky assert((Opcode == ISD::INTRINSIC_VOID || 3783198090Srdivacky Opcode == ISD::INTRINSIC_W_CHAIN || 3784198090Srdivacky (Opcode <= INT_MAX && 3785198090Srdivacky (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) && 3786198090Srdivacky "Opcode is not a memory-accessing opcode!"); 3787198090Srdivacky 3788193323Sed // Memoize the node unless it returns a flag. 3789193323Sed MemIntrinsicSDNode *N; 3790193323Sed if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) { 3791193323Sed FoldingSetNodeID ID; 3792193323Sed AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 3793193323Sed void *IP = 0; 3794198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3795198090Srdivacky cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO); 3796193323Sed return SDValue(E, 0); 3797198090Srdivacky } 3798193323Sed 3799193323Sed N = NodeAllocator.Allocate<MemIntrinsicSDNode>(); 3800198090Srdivacky new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO); 3801193323Sed CSEMap.InsertNode(N, IP); 3802193323Sed } else { 3803193323Sed N = NodeAllocator.Allocate<MemIntrinsicSDNode>(); 3804198090Srdivacky new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO); 3805193323Sed } 3806193323Sed AllNodes.push_back(N); 3807193323Sed return SDValue(N, 0); 3808193323Sed} 3809193323Sed 3810193323SedSDValue 3811193323SedSelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl, 3812198090Srdivacky ISD::LoadExtType ExtType, EVT VT, SDValue Chain, 3813193323Sed SDValue Ptr, SDValue Offset, 3814198090Srdivacky const Value *SV, int SVOffset, EVT MemVT, 3815203954Srdivacky bool isVolatile, bool isNonTemporal, 3816203954Srdivacky unsigned Alignment) { 3817193323Sed if (Alignment == 0) // Ensure that codegen never sees alignment 0 3818198090Srdivacky Alignment = getEVTAlignment(VT); 3819193323Sed 3820198090Srdivacky // Check if the memory reference references a frame index 3821198090Srdivacky if (!SV) 3822198090Srdivacky if (const FrameIndexSDNode *FI = 3823198090Srdivacky dyn_cast<const FrameIndexSDNode>(Ptr.getNode())) 3824198090Srdivacky SV = PseudoSourceValue::getFixedStack(FI->getIndex()); 3825198090Srdivacky 3826198090Srdivacky MachineFunction &MF = getMachineFunction(); 3827198090Srdivacky unsigned Flags = MachineMemOperand::MOLoad; 3828198090Srdivacky if (isVolatile) 3829198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3830203954Srdivacky if (isNonTemporal) 3831203954Srdivacky Flags |= MachineMemOperand::MONonTemporal; 3832198090Srdivacky MachineMemOperand *MMO = 3833198090Srdivacky MF.getMachineMemOperand(SV, Flags, SVOffset, 3834198090Srdivacky MemVT.getStoreSize(), Alignment); 3835198090Srdivacky return getLoad(AM, dl, ExtType, VT, Chain, Ptr, Offset, MemVT, MMO); 3836198090Srdivacky} 3837198090Srdivacky 3838198090SrdivackySDValue 3839198090SrdivackySelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl, 3840198090Srdivacky ISD::LoadExtType ExtType, EVT VT, SDValue Chain, 3841198090Srdivacky SDValue Ptr, SDValue Offset, EVT MemVT, 3842198090Srdivacky MachineMemOperand *MMO) { 3843198090Srdivacky if (VT == MemVT) { 3844193323Sed ExtType = ISD::NON_EXTLOAD; 3845193323Sed } else if (ExtType == ISD::NON_EXTLOAD) { 3846198090Srdivacky assert(VT == MemVT && "Non-extending load from different memory type!"); 3847193323Sed } else { 3848193323Sed // Extending load. 3849200581Srdivacky assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) && 3850200581Srdivacky "Should only be an extending load, not truncating!"); 3851198090Srdivacky assert(VT.isInteger() == MemVT.isInteger() && 3852193323Sed "Cannot convert from FP to Int or Int -> FP!"); 3853200581Srdivacky assert(VT.isVector() == MemVT.isVector() && 3854200581Srdivacky "Cannot use trunc store to convert to or from a vector!"); 3855200581Srdivacky assert((!VT.isVector() || 3856200581Srdivacky VT.getVectorNumElements() == MemVT.getVectorNumElements()) && 3857200581Srdivacky "Cannot use trunc store to change the number of vector elements!"); 3858193323Sed } 3859193323Sed 3860193323Sed bool Indexed = AM != ISD::UNINDEXED; 3861193323Sed assert((Indexed || Offset.getOpcode() == ISD::UNDEF) && 3862193323Sed "Unindexed load with an offset!"); 3863193323Sed 3864193323Sed SDVTList VTs = Indexed ? 3865193323Sed getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other); 3866193323Sed SDValue Ops[] = { Chain, Ptr, Offset }; 3867193323Sed FoldingSetNodeID ID; 3868193323Sed AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 3869198090Srdivacky ID.AddInteger(MemVT.getRawBits()); 3870204642Srdivacky ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(), 3871204642Srdivacky MMO->isNonTemporal())); 3872193323Sed void *IP = 0; 3873198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3874198090Srdivacky cast<LoadSDNode>(E)->refineAlignment(MMO); 3875193323Sed return SDValue(E, 0); 3876198090Srdivacky } 3877193323Sed SDNode *N = NodeAllocator.Allocate<LoadSDNode>(); 3878198090Srdivacky new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, MemVT, MMO); 3879193323Sed CSEMap.InsertNode(N, IP); 3880193323Sed AllNodes.push_back(N); 3881193323Sed return SDValue(N, 0); 3882193323Sed} 3883193323Sed 3884198090SrdivackySDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl, 3885193323Sed SDValue Chain, SDValue Ptr, 3886193323Sed const Value *SV, int SVOffset, 3887203954Srdivacky bool isVolatile, bool isNonTemporal, 3888203954Srdivacky unsigned Alignment) { 3889193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 3890193323Sed return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef, 3891203954Srdivacky SV, SVOffset, VT, isVolatile, isNonTemporal, Alignment); 3892193323Sed} 3893193323Sed 3894198090SrdivackySDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT, 3895193323Sed SDValue Chain, SDValue Ptr, 3896193323Sed const Value *SV, 3897198090Srdivacky int SVOffset, EVT MemVT, 3898203954Srdivacky bool isVolatile, bool isNonTemporal, 3899203954Srdivacky unsigned Alignment) { 3900193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 3901193323Sed return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef, 3902203954Srdivacky SV, SVOffset, MemVT, isVolatile, isNonTemporal, Alignment); 3903193323Sed} 3904193323Sed 3905193323SedSDValue 3906193323SedSelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base, 3907193323Sed SDValue Offset, ISD::MemIndexedMode AM) { 3908193323Sed LoadSDNode *LD = cast<LoadSDNode>(OrigLoad); 3909193323Sed assert(LD->getOffset().getOpcode() == ISD::UNDEF && 3910193323Sed "Load is already a indexed load!"); 3911193323Sed return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(), 3912193323Sed LD->getChain(), Base, Offset, LD->getSrcValue(), 3913193323Sed LD->getSrcValueOffset(), LD->getMemoryVT(), 3914203954Srdivacky LD->isVolatile(), LD->isNonTemporal(), LD->getAlignment()); 3915193323Sed} 3916193323Sed 3917193323SedSDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val, 3918193323Sed SDValue Ptr, const Value *SV, int SVOffset, 3919203954Srdivacky bool isVolatile, bool isNonTemporal, 3920203954Srdivacky unsigned Alignment) { 3921193323Sed if (Alignment == 0) // Ensure that codegen never sees alignment 0 3922198090Srdivacky Alignment = getEVTAlignment(Val.getValueType()); 3923193323Sed 3924198090Srdivacky // Check if the memory reference references a frame index 3925198090Srdivacky if (!SV) 3926198090Srdivacky if (const FrameIndexSDNode *FI = 3927198090Srdivacky dyn_cast<const FrameIndexSDNode>(Ptr.getNode())) 3928198090Srdivacky SV = PseudoSourceValue::getFixedStack(FI->getIndex()); 3929198090Srdivacky 3930198090Srdivacky MachineFunction &MF = getMachineFunction(); 3931198090Srdivacky unsigned Flags = MachineMemOperand::MOStore; 3932198090Srdivacky if (isVolatile) 3933198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3934203954Srdivacky if (isNonTemporal) 3935203954Srdivacky Flags |= MachineMemOperand::MONonTemporal; 3936198090Srdivacky MachineMemOperand *MMO = 3937198090Srdivacky MF.getMachineMemOperand(SV, Flags, SVOffset, 3938198090Srdivacky Val.getValueType().getStoreSize(), Alignment); 3939198090Srdivacky 3940198090Srdivacky return getStore(Chain, dl, Val, Ptr, MMO); 3941198090Srdivacky} 3942198090Srdivacky 3943198090SrdivackySDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val, 3944198090Srdivacky SDValue Ptr, MachineMemOperand *MMO) { 3945198090Srdivacky EVT VT = Val.getValueType(); 3946193323Sed SDVTList VTs = getVTList(MVT::Other); 3947193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 3948193323Sed SDValue Ops[] = { Chain, Val, Ptr, Undef }; 3949193323Sed FoldingSetNodeID ID; 3950193323Sed AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 3951193323Sed ID.AddInteger(VT.getRawBits()); 3952204642Srdivacky ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(), 3953204642Srdivacky MMO->isNonTemporal())); 3954193323Sed void *IP = 0; 3955198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3956198090Srdivacky cast<StoreSDNode>(E)->refineAlignment(MMO); 3957193323Sed return SDValue(E, 0); 3958198090Srdivacky } 3959193323Sed SDNode *N = NodeAllocator.Allocate<StoreSDNode>(); 3960198090Srdivacky new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false, VT, MMO); 3961193323Sed CSEMap.InsertNode(N, IP); 3962193323Sed AllNodes.push_back(N); 3963193323Sed return SDValue(N, 0); 3964193323Sed} 3965193323Sed 3966193323SedSDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, 3967193323Sed SDValue Ptr, const Value *SV, 3968198090Srdivacky int SVOffset, EVT SVT, 3969203954Srdivacky bool isVolatile, bool isNonTemporal, 3970203954Srdivacky unsigned Alignment) { 3971198090Srdivacky if (Alignment == 0) // Ensure that codegen never sees alignment 0 3972198090Srdivacky Alignment = getEVTAlignment(SVT); 3973193323Sed 3974198090Srdivacky // Check if the memory reference references a frame index 3975198090Srdivacky if (!SV) 3976198090Srdivacky if (const FrameIndexSDNode *FI = 3977198090Srdivacky dyn_cast<const FrameIndexSDNode>(Ptr.getNode())) 3978198090Srdivacky SV = PseudoSourceValue::getFixedStack(FI->getIndex()); 3979198090Srdivacky 3980198090Srdivacky MachineFunction &MF = getMachineFunction(); 3981198090Srdivacky unsigned Flags = MachineMemOperand::MOStore; 3982198090Srdivacky if (isVolatile) 3983198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3984203954Srdivacky if (isNonTemporal) 3985203954Srdivacky Flags |= MachineMemOperand::MONonTemporal; 3986198090Srdivacky MachineMemOperand *MMO = 3987198090Srdivacky MF.getMachineMemOperand(SV, Flags, SVOffset, SVT.getStoreSize(), Alignment); 3988198090Srdivacky 3989198090Srdivacky return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO); 3990198090Srdivacky} 3991198090Srdivacky 3992198090SrdivackySDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, 3993198090Srdivacky SDValue Ptr, EVT SVT, 3994198090Srdivacky MachineMemOperand *MMO) { 3995198090Srdivacky EVT VT = Val.getValueType(); 3996198090Srdivacky 3997193323Sed if (VT == SVT) 3998198090Srdivacky return getStore(Chain, dl, Val, Ptr, MMO); 3999193323Sed 4000200581Srdivacky assert(SVT.getScalarType().bitsLT(VT.getScalarType()) && 4001200581Srdivacky "Should only be a truncating store, not extending!"); 4002193323Sed assert(VT.isInteger() == SVT.isInteger() && 4003193323Sed "Can't do FP-INT conversion!"); 4004200581Srdivacky assert(VT.isVector() == SVT.isVector() && 4005200581Srdivacky "Cannot use trunc store to convert to or from a vector!"); 4006200581Srdivacky assert((!VT.isVector() || 4007200581Srdivacky VT.getVectorNumElements() == SVT.getVectorNumElements()) && 4008200581Srdivacky "Cannot use trunc store to change the number of vector elements!"); 4009193323Sed 4010193323Sed SDVTList VTs = getVTList(MVT::Other); 4011193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 4012193323Sed SDValue Ops[] = { Chain, Val, Ptr, Undef }; 4013193323Sed FoldingSetNodeID ID; 4014193323Sed AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 4015193323Sed ID.AddInteger(SVT.getRawBits()); 4016204642Srdivacky ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(), 4017204642Srdivacky MMO->isNonTemporal())); 4018193323Sed void *IP = 0; 4019198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4020198090Srdivacky cast<StoreSDNode>(E)->refineAlignment(MMO); 4021193323Sed return SDValue(E, 0); 4022198090Srdivacky } 4023193323Sed SDNode *N = NodeAllocator.Allocate<StoreSDNode>(); 4024198090Srdivacky new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true, SVT, MMO); 4025193323Sed CSEMap.InsertNode(N, IP); 4026193323Sed AllNodes.push_back(N); 4027193323Sed return SDValue(N, 0); 4028193323Sed} 4029193323Sed 4030193323SedSDValue 4031193323SedSelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base, 4032193323Sed SDValue Offset, ISD::MemIndexedMode AM) { 4033193323Sed StoreSDNode *ST = cast<StoreSDNode>(OrigStore); 4034193323Sed assert(ST->getOffset().getOpcode() == ISD::UNDEF && 4035193323Sed "Store is already a indexed store!"); 4036193323Sed SDVTList VTs = getVTList(Base.getValueType(), MVT::Other); 4037193323Sed SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset }; 4038193323Sed FoldingSetNodeID ID; 4039193323Sed AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 4040193323Sed ID.AddInteger(ST->getMemoryVT().getRawBits()); 4041193323Sed ID.AddInteger(ST->getRawSubclassData()); 4042193323Sed void *IP = 0; 4043201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4044193323Sed return SDValue(E, 0); 4045201360Srdivacky 4046193323Sed SDNode *N = NodeAllocator.Allocate<StoreSDNode>(); 4047193323Sed new (N) StoreSDNode(Ops, dl, VTs, AM, 4048193323Sed ST->isTruncatingStore(), ST->getMemoryVT(), 4049198090Srdivacky ST->getMemOperand()); 4050193323Sed CSEMap.InsertNode(N, IP); 4051193323Sed AllNodes.push_back(N); 4052193323Sed return SDValue(N, 0); 4053193323Sed} 4054193323Sed 4055198090SrdivackySDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl, 4056193323Sed SDValue Chain, SDValue Ptr, 4057193323Sed SDValue SV) { 4058193323Sed SDValue Ops[] = { Chain, Ptr, SV }; 4059193323Sed return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3); 4060193323Sed} 4061193323Sed 4062198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 4063193323Sed const SDUse *Ops, unsigned NumOps) { 4064193323Sed switch (NumOps) { 4065193323Sed case 0: return getNode(Opcode, DL, VT); 4066193323Sed case 1: return getNode(Opcode, DL, VT, Ops[0]); 4067193323Sed case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); 4068193323Sed case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); 4069193323Sed default: break; 4070193323Sed } 4071193323Sed 4072193323Sed // Copy from an SDUse array into an SDValue array for use with 4073193323Sed // the regular getNode logic. 4074193323Sed SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps); 4075193323Sed return getNode(Opcode, DL, VT, &NewOps[0], NumOps); 4076193323Sed} 4077193323Sed 4078198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 4079193323Sed const SDValue *Ops, unsigned NumOps) { 4080193323Sed switch (NumOps) { 4081193323Sed case 0: return getNode(Opcode, DL, VT); 4082193323Sed case 1: return getNode(Opcode, DL, VT, Ops[0]); 4083193323Sed case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); 4084193323Sed case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); 4085193323Sed default: break; 4086193323Sed } 4087193323Sed 4088193323Sed switch (Opcode) { 4089193323Sed default: break; 4090193323Sed case ISD::SELECT_CC: { 4091193323Sed assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); 4092193323Sed assert(Ops[0].getValueType() == Ops[1].getValueType() && 4093193323Sed "LHS and RHS of condition must have same type!"); 4094193323Sed assert(Ops[2].getValueType() == Ops[3].getValueType() && 4095193323Sed "True and False arms of SelectCC must have same type!"); 4096193323Sed assert(Ops[2].getValueType() == VT && 4097193323Sed "select_cc node must be of same type as true and false value!"); 4098193323Sed break; 4099193323Sed } 4100193323Sed case ISD::BR_CC: { 4101193323Sed assert(NumOps == 5 && "BR_CC takes 5 operands!"); 4102193323Sed assert(Ops[2].getValueType() == Ops[3].getValueType() && 4103193323Sed "LHS/RHS of comparison should match types!"); 4104193323Sed break; 4105193323Sed } 4106193323Sed } 4107193323Sed 4108193323Sed // Memoize nodes. 4109193323Sed SDNode *N; 4110193323Sed SDVTList VTs = getVTList(VT); 4111193323Sed 4112193323Sed if (VT != MVT::Flag) { 4113193323Sed FoldingSetNodeID ID; 4114193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps); 4115193323Sed void *IP = 0; 4116193323Sed 4117201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4118193323Sed return SDValue(E, 0); 4119193323Sed 4120193323Sed N = NodeAllocator.Allocate<SDNode>(); 4121193323Sed new (N) SDNode(Opcode, DL, VTs, Ops, NumOps); 4122193323Sed CSEMap.InsertNode(N, IP); 4123193323Sed } else { 4124193323Sed N = NodeAllocator.Allocate<SDNode>(); 4125193323Sed new (N) SDNode(Opcode, DL, VTs, Ops, NumOps); 4126193323Sed } 4127193323Sed 4128193323Sed AllNodes.push_back(N); 4129193323Sed#ifndef NDEBUG 4130193323Sed VerifyNode(N); 4131193323Sed#endif 4132193323Sed return SDValue(N, 0); 4133193323Sed} 4134193323Sed 4135193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, 4136198090Srdivacky const std::vector<EVT> &ResultTys, 4137193323Sed const SDValue *Ops, unsigned NumOps) { 4138193323Sed return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()), 4139193323Sed Ops, NumOps); 4140193323Sed} 4141193323Sed 4142193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, 4143198090Srdivacky const EVT *VTs, unsigned NumVTs, 4144193323Sed const SDValue *Ops, unsigned NumOps) { 4145193323Sed if (NumVTs == 1) 4146193323Sed return getNode(Opcode, DL, VTs[0], Ops, NumOps); 4147193323Sed return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps); 4148193323Sed} 4149193323Sed 4150193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4151193323Sed const SDValue *Ops, unsigned NumOps) { 4152193323Sed if (VTList.NumVTs == 1) 4153193323Sed return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps); 4154193323Sed 4155198090Srdivacky#if 0 4156193323Sed switch (Opcode) { 4157193323Sed // FIXME: figure out how to safely handle things like 4158193323Sed // int foo(int x) { return 1 << (x & 255); } 4159193323Sed // int bar() { return foo(256); } 4160193323Sed case ISD::SRA_PARTS: 4161193323Sed case ISD::SRL_PARTS: 4162193323Sed case ISD::SHL_PARTS: 4163193323Sed if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 4164193323Sed cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 4165193323Sed return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); 4166193323Sed else if (N3.getOpcode() == ISD::AND) 4167193323Sed if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 4168193323Sed // If the and is only masking out bits that cannot effect the shift, 4169193323Sed // eliminate the and. 4170202375Srdivacky unsigned NumBits = VT.getScalarType().getSizeInBits()*2; 4171193323Sed if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 4172193323Sed return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); 4173193323Sed } 4174193323Sed break; 4175198090Srdivacky } 4176193323Sed#endif 4177193323Sed 4178193323Sed // Memoize the node unless it returns a flag. 4179193323Sed SDNode *N; 4180193323Sed if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) { 4181193323Sed FoldingSetNodeID ID; 4182193323Sed AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 4183193323Sed void *IP = 0; 4184201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4185193323Sed return SDValue(E, 0); 4186201360Srdivacky 4187193323Sed if (NumOps == 1) { 4188193323Sed N = NodeAllocator.Allocate<UnarySDNode>(); 4189193323Sed new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]); 4190193323Sed } else if (NumOps == 2) { 4191193323Sed N = NodeAllocator.Allocate<BinarySDNode>(); 4192193323Sed new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]); 4193193323Sed } else if (NumOps == 3) { 4194193323Sed N = NodeAllocator.Allocate<TernarySDNode>(); 4195193323Sed new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]); 4196193323Sed } else { 4197193323Sed N = NodeAllocator.Allocate<SDNode>(); 4198193323Sed new (N) SDNode(Opcode, DL, VTList, Ops, NumOps); 4199193323Sed } 4200193323Sed CSEMap.InsertNode(N, IP); 4201193323Sed } else { 4202193323Sed if (NumOps == 1) { 4203193323Sed N = NodeAllocator.Allocate<UnarySDNode>(); 4204193323Sed new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]); 4205193323Sed } else if (NumOps == 2) { 4206193323Sed N = NodeAllocator.Allocate<BinarySDNode>(); 4207193323Sed new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]); 4208193323Sed } else if (NumOps == 3) { 4209193323Sed N = NodeAllocator.Allocate<TernarySDNode>(); 4210193323Sed new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]); 4211193323Sed } else { 4212193323Sed N = NodeAllocator.Allocate<SDNode>(); 4213193323Sed new (N) SDNode(Opcode, DL, VTList, Ops, NumOps); 4214193323Sed } 4215193323Sed } 4216193323Sed AllNodes.push_back(N); 4217193323Sed#ifndef NDEBUG 4218193323Sed VerifyNode(N); 4219193323Sed#endif 4220193323Sed return SDValue(N, 0); 4221193323Sed} 4222193323Sed 4223193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) { 4224193323Sed return getNode(Opcode, DL, VTList, 0, 0); 4225193323Sed} 4226193323Sed 4227193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4228193323Sed SDValue N1) { 4229193323Sed SDValue Ops[] = { N1 }; 4230193323Sed return getNode(Opcode, DL, VTList, Ops, 1); 4231193323Sed} 4232193323Sed 4233193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4234193323Sed SDValue N1, SDValue N2) { 4235193323Sed SDValue Ops[] = { N1, N2 }; 4236193323Sed return getNode(Opcode, DL, VTList, Ops, 2); 4237193323Sed} 4238193323Sed 4239193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4240193323Sed SDValue N1, SDValue N2, SDValue N3) { 4241193323Sed SDValue Ops[] = { N1, N2, N3 }; 4242193323Sed return getNode(Opcode, DL, VTList, Ops, 3); 4243193323Sed} 4244193323Sed 4245193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4246193323Sed SDValue N1, SDValue N2, SDValue N3, 4247193323Sed SDValue N4) { 4248193323Sed SDValue Ops[] = { N1, N2, N3, N4 }; 4249193323Sed return getNode(Opcode, DL, VTList, Ops, 4); 4250193323Sed} 4251193323Sed 4252193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4253193323Sed SDValue N1, SDValue N2, SDValue N3, 4254193323Sed SDValue N4, SDValue N5) { 4255193323Sed SDValue Ops[] = { N1, N2, N3, N4, N5 }; 4256193323Sed return getNode(Opcode, DL, VTList, Ops, 5); 4257193323Sed} 4258193323Sed 4259198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT) { 4260193323Sed return makeVTList(SDNode::getValueTypeList(VT), 1); 4261193323Sed} 4262193323Sed 4263198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) { 4264193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4265193323Sed E = VTList.rend(); I != E; ++I) 4266193323Sed if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2) 4267193323Sed return *I; 4268193323Sed 4269198090Srdivacky EVT *Array = Allocator.Allocate<EVT>(2); 4270193323Sed Array[0] = VT1; 4271193323Sed Array[1] = VT2; 4272193323Sed SDVTList Result = makeVTList(Array, 2); 4273193323Sed VTList.push_back(Result); 4274193323Sed return Result; 4275193323Sed} 4276193323Sed 4277198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) { 4278193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4279193323Sed E = VTList.rend(); I != E; ++I) 4280193323Sed if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 && 4281193323Sed I->VTs[2] == VT3) 4282193323Sed return *I; 4283193323Sed 4284198090Srdivacky EVT *Array = Allocator.Allocate<EVT>(3); 4285193323Sed Array[0] = VT1; 4286193323Sed Array[1] = VT2; 4287193323Sed Array[2] = VT3; 4288193323Sed SDVTList Result = makeVTList(Array, 3); 4289193323Sed VTList.push_back(Result); 4290193323Sed return Result; 4291193323Sed} 4292193323Sed 4293198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) { 4294193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4295193323Sed E = VTList.rend(); I != E; ++I) 4296193323Sed if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 && 4297193323Sed I->VTs[2] == VT3 && I->VTs[3] == VT4) 4298193323Sed return *I; 4299193323Sed 4300200581Srdivacky EVT *Array = Allocator.Allocate<EVT>(4); 4301193323Sed Array[0] = VT1; 4302193323Sed Array[1] = VT2; 4303193323Sed Array[2] = VT3; 4304193323Sed Array[3] = VT4; 4305193323Sed SDVTList Result = makeVTList(Array, 4); 4306193323Sed VTList.push_back(Result); 4307193323Sed return Result; 4308193323Sed} 4309193323Sed 4310198090SrdivackySDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) { 4311193323Sed switch (NumVTs) { 4312198090Srdivacky case 0: llvm_unreachable("Cannot have nodes without results!"); 4313193323Sed case 1: return getVTList(VTs[0]); 4314193323Sed case 2: return getVTList(VTs[0], VTs[1]); 4315193323Sed case 3: return getVTList(VTs[0], VTs[1], VTs[2]); 4316201360Srdivacky case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]); 4317193323Sed default: break; 4318193323Sed } 4319193323Sed 4320193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4321193323Sed E = VTList.rend(); I != E; ++I) { 4322193323Sed if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1]) 4323193323Sed continue; 4324193323Sed 4325193323Sed bool NoMatch = false; 4326193323Sed for (unsigned i = 2; i != NumVTs; ++i) 4327193323Sed if (VTs[i] != I->VTs[i]) { 4328193323Sed NoMatch = true; 4329193323Sed break; 4330193323Sed } 4331193323Sed if (!NoMatch) 4332193323Sed return *I; 4333193323Sed } 4334193323Sed 4335198090Srdivacky EVT *Array = Allocator.Allocate<EVT>(NumVTs); 4336193323Sed std::copy(VTs, VTs+NumVTs, Array); 4337193323Sed SDVTList Result = makeVTList(Array, NumVTs); 4338193323Sed VTList.push_back(Result); 4339193323Sed return Result; 4340193323Sed} 4341193323Sed 4342193323Sed 4343193323Sed/// UpdateNodeOperands - *Mutate* the specified node in-place to have the 4344193323Sed/// specified operands. If the resultant node already exists in the DAG, 4345193323Sed/// this does not modify the specified node, instead it returns the node that 4346193323Sed/// already exists. If the resultant node does not exist in the DAG, the 4347193323Sed/// input node is returned. As a degenerate case, if you specify the same 4348193323Sed/// input operands as the node already has, the input node is returned. 4349193323SedSDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) { 4350193323Sed SDNode *N = InN.getNode(); 4351193323Sed assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 4352193323Sed 4353193323Sed // Check to see if there is no change. 4354193323Sed if (Op == N->getOperand(0)) return InN; 4355193323Sed 4356193323Sed // See if the modified node already exists. 4357193323Sed void *InsertPos = 0; 4358193323Sed if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) 4359193323Sed return SDValue(Existing, InN.getResNo()); 4360193323Sed 4361193323Sed // Nope it doesn't. Remove the node from its current place in the maps. 4362193323Sed if (InsertPos) 4363193323Sed if (!RemoveNodeFromCSEMaps(N)) 4364193323Sed InsertPos = 0; 4365193323Sed 4366193323Sed // Now we update the operands. 4367193323Sed N->OperandList[0].set(Op); 4368193323Sed 4369193323Sed // If this gets put into a CSE map, add it. 4370193323Sed if (InsertPos) CSEMap.InsertNode(N, InsertPos); 4371193323Sed return InN; 4372193323Sed} 4373193323Sed 4374193323SedSDValue SelectionDAG:: 4375193323SedUpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) { 4376193323Sed SDNode *N = InN.getNode(); 4377193323Sed assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 4378193323Sed 4379193323Sed // Check to see if there is no change. 4380193323Sed if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 4381193323Sed return InN; // No operands changed, just return the input node. 4382193323Sed 4383193323Sed // See if the modified node already exists. 4384193323Sed void *InsertPos = 0; 4385193323Sed if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) 4386193323Sed return SDValue(Existing, InN.getResNo()); 4387193323Sed 4388193323Sed // Nope it doesn't. Remove the node from its current place in the maps. 4389193323Sed if (InsertPos) 4390193323Sed if (!RemoveNodeFromCSEMaps(N)) 4391193323Sed InsertPos = 0; 4392193323Sed 4393193323Sed // Now we update the operands. 4394193323Sed if (N->OperandList[0] != Op1) 4395193323Sed N->OperandList[0].set(Op1); 4396193323Sed if (N->OperandList[1] != Op2) 4397193323Sed N->OperandList[1].set(Op2); 4398193323Sed 4399193323Sed // If this gets put into a CSE map, add it. 4400193323Sed if (InsertPos) CSEMap.InsertNode(N, InsertPos); 4401193323Sed return InN; 4402193323Sed} 4403193323Sed 4404193323SedSDValue SelectionDAG:: 4405193323SedUpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) { 4406193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4407193323Sed return UpdateNodeOperands(N, Ops, 3); 4408193323Sed} 4409193323Sed 4410193323SedSDValue SelectionDAG:: 4411193323SedUpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, 4412193323Sed SDValue Op3, SDValue Op4) { 4413193323Sed SDValue Ops[] = { Op1, Op2, Op3, Op4 }; 4414193323Sed return UpdateNodeOperands(N, Ops, 4); 4415193323Sed} 4416193323Sed 4417193323SedSDValue SelectionDAG:: 4418193323SedUpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, 4419193323Sed SDValue Op3, SDValue Op4, SDValue Op5) { 4420193323Sed SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 }; 4421193323Sed return UpdateNodeOperands(N, Ops, 5); 4422193323Sed} 4423193323Sed 4424193323SedSDValue SelectionDAG:: 4425193323SedUpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) { 4426193323Sed SDNode *N = InN.getNode(); 4427193323Sed assert(N->getNumOperands() == NumOps && 4428193323Sed "Update with wrong number of operands"); 4429193323Sed 4430193323Sed // Check to see if there is no change. 4431193323Sed bool AnyChange = false; 4432193323Sed for (unsigned i = 0; i != NumOps; ++i) { 4433193323Sed if (Ops[i] != N->getOperand(i)) { 4434193323Sed AnyChange = true; 4435193323Sed break; 4436193323Sed } 4437193323Sed } 4438193323Sed 4439193323Sed // No operands changed, just return the input node. 4440193323Sed if (!AnyChange) return InN; 4441193323Sed 4442193323Sed // See if the modified node already exists. 4443193323Sed void *InsertPos = 0; 4444193323Sed if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos)) 4445193323Sed return SDValue(Existing, InN.getResNo()); 4446193323Sed 4447193323Sed // Nope it doesn't. Remove the node from its current place in the maps. 4448193323Sed if (InsertPos) 4449193323Sed if (!RemoveNodeFromCSEMaps(N)) 4450193323Sed InsertPos = 0; 4451193323Sed 4452193323Sed // Now we update the operands. 4453193323Sed for (unsigned i = 0; i != NumOps; ++i) 4454193323Sed if (N->OperandList[i] != Ops[i]) 4455193323Sed N->OperandList[i].set(Ops[i]); 4456193323Sed 4457193323Sed // If this gets put into a CSE map, add it. 4458193323Sed if (InsertPos) CSEMap.InsertNode(N, InsertPos); 4459193323Sed return InN; 4460193323Sed} 4461193323Sed 4462193323Sed/// DropOperands - Release the operands and set this node to have 4463193323Sed/// zero operands. 4464193323Sedvoid SDNode::DropOperands() { 4465193323Sed // Unlike the code in MorphNodeTo that does this, we don't need to 4466193323Sed // watch for dead nodes here. 4467193323Sed for (op_iterator I = op_begin(), E = op_end(); I != E; ) { 4468193323Sed SDUse &Use = *I++; 4469193323Sed Use.set(SDValue()); 4470193323Sed } 4471193323Sed} 4472193323Sed 4473193323Sed/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a 4474193323Sed/// machine opcode. 4475193323Sed/// 4476193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4477198090Srdivacky EVT VT) { 4478193323Sed SDVTList VTs = getVTList(VT); 4479193323Sed return SelectNodeTo(N, MachineOpc, VTs, 0, 0); 4480193323Sed} 4481193323Sed 4482193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4483198090Srdivacky EVT VT, SDValue Op1) { 4484193323Sed SDVTList VTs = getVTList(VT); 4485193323Sed SDValue Ops[] = { Op1 }; 4486193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 1); 4487193323Sed} 4488193323Sed 4489193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4490198090Srdivacky EVT VT, SDValue Op1, 4491193323Sed SDValue Op2) { 4492193323Sed SDVTList VTs = getVTList(VT); 4493193323Sed SDValue Ops[] = { Op1, Op2 }; 4494193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 2); 4495193323Sed} 4496193323Sed 4497193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4498198090Srdivacky EVT VT, SDValue Op1, 4499193323Sed SDValue Op2, SDValue Op3) { 4500193323Sed SDVTList VTs = getVTList(VT); 4501193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4502193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); 4503193323Sed} 4504193323Sed 4505193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4506198090Srdivacky EVT VT, const SDValue *Ops, 4507193323Sed unsigned NumOps) { 4508193323Sed SDVTList VTs = getVTList(VT); 4509193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4510193323Sed} 4511193323Sed 4512193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4513198090Srdivacky EVT VT1, EVT VT2, const SDValue *Ops, 4514193323Sed unsigned NumOps) { 4515193323Sed SDVTList VTs = getVTList(VT1, VT2); 4516193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4517193323Sed} 4518193323Sed 4519193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4520198090Srdivacky EVT VT1, EVT VT2) { 4521193323Sed SDVTList VTs = getVTList(VT1, VT2); 4522193323Sed return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0); 4523193323Sed} 4524193323Sed 4525193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4526198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4527193323Sed const SDValue *Ops, unsigned NumOps) { 4528193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4529193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4530193323Sed} 4531193323Sed 4532193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4533198090Srdivacky EVT VT1, EVT VT2, EVT VT3, EVT VT4, 4534193323Sed const SDValue *Ops, unsigned NumOps) { 4535193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); 4536193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4537193323Sed} 4538193323Sed 4539193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4540198090Srdivacky EVT VT1, EVT VT2, 4541193323Sed SDValue Op1) { 4542193323Sed SDVTList VTs = getVTList(VT1, VT2); 4543193323Sed SDValue Ops[] = { Op1 }; 4544193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 1); 4545193323Sed} 4546193323Sed 4547193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4548198090Srdivacky EVT VT1, EVT VT2, 4549193323Sed SDValue Op1, SDValue Op2) { 4550193323Sed SDVTList VTs = getVTList(VT1, VT2); 4551193323Sed SDValue Ops[] = { Op1, Op2 }; 4552193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 2); 4553193323Sed} 4554193323Sed 4555193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4556198090Srdivacky EVT VT1, EVT VT2, 4557193323Sed SDValue Op1, SDValue Op2, 4558193323Sed SDValue Op3) { 4559193323Sed SDVTList VTs = getVTList(VT1, VT2); 4560193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4561193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); 4562193323Sed} 4563193323Sed 4564193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4565198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4566193323Sed SDValue Op1, SDValue Op2, 4567193323Sed SDValue Op3) { 4568193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4569193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4570193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); 4571193323Sed} 4572193323Sed 4573193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4574193323Sed SDVTList VTs, const SDValue *Ops, 4575193323Sed unsigned NumOps) { 4576204642Srdivacky N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps); 4577204642Srdivacky // Reset the NodeID to -1. 4578204642Srdivacky N->setNodeId(-1); 4579204642Srdivacky return N; 4580193323Sed} 4581193323Sed 4582204642Srdivacky/// MorphNodeTo - This *mutates* the specified node to have the specified 4583193323Sed/// return type, opcode, and operands. 4584193323Sed/// 4585193323Sed/// Note that MorphNodeTo returns the resultant node. If there is already a 4586193323Sed/// node of the specified opcode and operands, it returns that node instead of 4587193323Sed/// the current one. Note that the DebugLoc need not be the same. 4588193323Sed/// 4589193323Sed/// Using MorphNodeTo is faster than creating a new node and swapping it in 4590193323Sed/// with ReplaceAllUsesWith both because it often avoids allocating a new 4591193323Sed/// node, and because it doesn't require CSE recalculation for any of 4592193323Sed/// the node's users. 4593193323Sed/// 4594193323SedSDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc, 4595193323Sed SDVTList VTs, const SDValue *Ops, 4596193323Sed unsigned NumOps) { 4597193323Sed // If an identical node already exists, use it. 4598193323Sed void *IP = 0; 4599193323Sed if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) { 4600193323Sed FoldingSetNodeID ID; 4601193323Sed AddNodeIDNode(ID, Opc, VTs, Ops, NumOps); 4602201360Srdivacky if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 4603193323Sed return ON; 4604193323Sed } 4605193323Sed 4606193323Sed if (!RemoveNodeFromCSEMaps(N)) 4607193323Sed IP = 0; 4608193323Sed 4609193323Sed // Start the morphing. 4610193323Sed N->NodeType = Opc; 4611193323Sed N->ValueList = VTs.VTs; 4612193323Sed N->NumValues = VTs.NumVTs; 4613193323Sed 4614193323Sed // Clear the operands list, updating used nodes to remove this from their 4615193323Sed // use list. Keep track of any operands that become dead as a result. 4616193323Sed SmallPtrSet<SDNode*, 16> DeadNodeSet; 4617193323Sed for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { 4618193323Sed SDUse &Use = *I++; 4619193323Sed SDNode *Used = Use.getNode(); 4620193323Sed Use.set(SDValue()); 4621193323Sed if (Used->use_empty()) 4622193323Sed DeadNodeSet.insert(Used); 4623193323Sed } 4624193323Sed 4625198090Srdivacky if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) { 4626198090Srdivacky // Initialize the memory references information. 4627198090Srdivacky MN->setMemRefs(0, 0); 4628198090Srdivacky // If NumOps is larger than the # of operands we can have in a 4629198090Srdivacky // MachineSDNode, reallocate the operand list. 4630198090Srdivacky if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) { 4631198090Srdivacky if (MN->OperandsNeedDelete) 4632198090Srdivacky delete[] MN->OperandList; 4633198090Srdivacky if (NumOps > array_lengthof(MN->LocalOperands)) 4634198090Srdivacky // We're creating a final node that will live unmorphed for the 4635198090Srdivacky // remainder of the current SelectionDAG iteration, so we can allocate 4636198090Srdivacky // the operands directly out of a pool with no recycling metadata. 4637198090Srdivacky MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), 4638198090Srdivacky Ops, NumOps); 4639198090Srdivacky else 4640198090Srdivacky MN->InitOperands(MN->LocalOperands, Ops, NumOps); 4641198090Srdivacky MN->OperandsNeedDelete = false; 4642198090Srdivacky } else 4643198090Srdivacky MN->InitOperands(MN->OperandList, Ops, NumOps); 4644198090Srdivacky } else { 4645198090Srdivacky // If NumOps is larger than the # of operands we currently have, reallocate 4646198090Srdivacky // the operand list. 4647198090Srdivacky if (NumOps > N->NumOperands) { 4648198090Srdivacky if (N->OperandsNeedDelete) 4649198090Srdivacky delete[] N->OperandList; 4650198090Srdivacky N->InitOperands(new SDUse[NumOps], Ops, NumOps); 4651193323Sed N->OperandsNeedDelete = true; 4652198090Srdivacky } else 4653198396Srdivacky N->InitOperands(N->OperandList, Ops, NumOps); 4654193323Sed } 4655193323Sed 4656193323Sed // Delete any nodes that are still dead after adding the uses for the 4657193323Sed // new operands. 4658204642Srdivacky if (!DeadNodeSet.empty()) { 4659204642Srdivacky SmallVector<SDNode *, 16> DeadNodes; 4660204642Srdivacky for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(), 4661204642Srdivacky E = DeadNodeSet.end(); I != E; ++I) 4662204642Srdivacky if ((*I)->use_empty()) 4663204642Srdivacky DeadNodes.push_back(*I); 4664204642Srdivacky RemoveDeadNodes(DeadNodes); 4665204642Srdivacky } 4666193323Sed 4667193323Sed if (IP) 4668193323Sed CSEMap.InsertNode(N, IP); // Memoize the new node. 4669193323Sed return N; 4670193323Sed} 4671193323Sed 4672193323Sed 4673198090Srdivacky/// getMachineNode - These are used for target selectors to create a new node 4674198090Srdivacky/// with specified return type(s), MachineInstr opcode, and operands. 4675193323Sed/// 4676198090Srdivacky/// Note that getMachineNode returns the resultant node. If there is already a 4677193323Sed/// node of the specified opcode and operands, it returns that node instead of 4678193323Sed/// the current one. 4679198090SrdivackyMachineSDNode * 4680198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) { 4681198090Srdivacky SDVTList VTs = getVTList(VT); 4682198090Srdivacky return getMachineNode(Opcode, dl, VTs, 0, 0); 4683193323Sed} 4684193323Sed 4685198090SrdivackyMachineSDNode * 4686198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) { 4687198090Srdivacky SDVTList VTs = getVTList(VT); 4688198090Srdivacky SDValue Ops[] = { Op1 }; 4689198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4690193323Sed} 4691193323Sed 4692198090SrdivackyMachineSDNode * 4693198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, 4694198090Srdivacky SDValue Op1, SDValue Op2) { 4695198090Srdivacky SDVTList VTs = getVTList(VT); 4696198090Srdivacky SDValue Ops[] = { Op1, Op2 }; 4697198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4698193323Sed} 4699193323Sed 4700198090SrdivackyMachineSDNode * 4701198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, 4702198090Srdivacky SDValue Op1, SDValue Op2, SDValue Op3) { 4703198090Srdivacky SDVTList VTs = getVTList(VT); 4704198090Srdivacky SDValue Ops[] = { Op1, Op2, Op3 }; 4705198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4706193323Sed} 4707193323Sed 4708198090SrdivackyMachineSDNode * 4709198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, 4710198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4711198090Srdivacky SDVTList VTs = getVTList(VT); 4712198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4713193323Sed} 4714193323Sed 4715198090SrdivackyMachineSDNode * 4716198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) { 4717193323Sed SDVTList VTs = getVTList(VT1, VT2); 4718198090Srdivacky return getMachineNode(Opcode, dl, VTs, 0, 0); 4719193323Sed} 4720193323Sed 4721198090SrdivackyMachineSDNode * 4722198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4723198090Srdivacky EVT VT1, EVT VT2, SDValue Op1) { 4724193323Sed SDVTList VTs = getVTList(VT1, VT2); 4725198090Srdivacky SDValue Ops[] = { Op1 }; 4726198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4727193323Sed} 4728193323Sed 4729198090SrdivackyMachineSDNode * 4730198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4731198090Srdivacky EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) { 4732193323Sed SDVTList VTs = getVTList(VT1, VT2); 4733193323Sed SDValue Ops[] = { Op1, Op2 }; 4734198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4735193323Sed} 4736193323Sed 4737198090SrdivackyMachineSDNode * 4738198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4739198090Srdivacky EVT VT1, EVT VT2, SDValue Op1, 4740198090Srdivacky SDValue Op2, SDValue Op3) { 4741193323Sed SDVTList VTs = getVTList(VT1, VT2); 4742193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4743198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4744193323Sed} 4745193323Sed 4746198090SrdivackyMachineSDNode * 4747198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4748198090Srdivacky EVT VT1, EVT VT2, 4749198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4750193323Sed SDVTList VTs = getVTList(VT1, VT2); 4751198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4752193323Sed} 4753193323Sed 4754198090SrdivackyMachineSDNode * 4755198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4756198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4757198090Srdivacky SDValue Op1, SDValue Op2) { 4758193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4759193323Sed SDValue Ops[] = { Op1, Op2 }; 4760198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4761193323Sed} 4762193323Sed 4763198090SrdivackyMachineSDNode * 4764198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4765198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4766198090Srdivacky SDValue Op1, SDValue Op2, SDValue Op3) { 4767193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4768193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4769198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4770193323Sed} 4771193323Sed 4772198090SrdivackyMachineSDNode * 4773198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4774198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4775198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4776193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4777198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4778193323Sed} 4779193323Sed 4780198090SrdivackyMachineSDNode * 4781198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, 4782198090Srdivacky EVT VT2, EVT VT3, EVT VT4, 4783198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4784193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); 4785198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4786193323Sed} 4787193323Sed 4788198090SrdivackyMachineSDNode * 4789198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4790198090Srdivacky const std::vector<EVT> &ResultTys, 4791198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4792198090Srdivacky SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size()); 4793198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4794193323Sed} 4795193323Sed 4796198090SrdivackyMachineSDNode * 4797198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, 4798198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4799198090Srdivacky bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Flag; 4800198090Srdivacky MachineSDNode *N; 4801198090Srdivacky void *IP; 4802198090Srdivacky 4803198090Srdivacky if (DoCSE) { 4804198090Srdivacky FoldingSetNodeID ID; 4805198090Srdivacky AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps); 4806198090Srdivacky IP = 0; 4807201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4808198090Srdivacky return cast<MachineSDNode>(E); 4809198090Srdivacky } 4810198090Srdivacky 4811198090Srdivacky // Allocate a new MachineSDNode. 4812198090Srdivacky N = NodeAllocator.Allocate<MachineSDNode>(); 4813198090Srdivacky new (N) MachineSDNode(~Opcode, DL, VTs); 4814198090Srdivacky 4815198090Srdivacky // Initialize the operands list. 4816198090Srdivacky if (NumOps > array_lengthof(N->LocalOperands)) 4817198090Srdivacky // We're creating a final node that will live unmorphed for the 4818198090Srdivacky // remainder of the current SelectionDAG iteration, so we can allocate 4819198090Srdivacky // the operands directly out of a pool with no recycling metadata. 4820198090Srdivacky N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), 4821198090Srdivacky Ops, NumOps); 4822198090Srdivacky else 4823198090Srdivacky N->InitOperands(N->LocalOperands, Ops, NumOps); 4824198090Srdivacky N->OperandsNeedDelete = false; 4825198090Srdivacky 4826198090Srdivacky if (DoCSE) 4827198090Srdivacky CSEMap.InsertNode(N, IP); 4828198090Srdivacky 4829198090Srdivacky AllNodes.push_back(N); 4830198090Srdivacky#ifndef NDEBUG 4831198090Srdivacky VerifyNode(N); 4832198090Srdivacky#endif 4833198090Srdivacky return N; 4834198090Srdivacky} 4835198090Srdivacky 4836198090Srdivacky/// getTargetExtractSubreg - A convenience function for creating 4837203954Srdivacky/// TargetOpcode::EXTRACT_SUBREG nodes. 4838198090SrdivackySDValue 4839198090SrdivackySelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT, 4840198090Srdivacky SDValue Operand) { 4841198090Srdivacky SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32); 4842203954Srdivacky SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, 4843198090Srdivacky VT, Operand, SRIdxVal); 4844198090Srdivacky return SDValue(Subreg, 0); 4845198090Srdivacky} 4846198090Srdivacky 4847198090Srdivacky/// getTargetInsertSubreg - A convenience function for creating 4848203954Srdivacky/// TargetOpcode::INSERT_SUBREG nodes. 4849198090SrdivackySDValue 4850198090SrdivackySelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT, 4851198090Srdivacky SDValue Operand, SDValue Subreg) { 4852198090Srdivacky SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32); 4853203954Srdivacky SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL, 4854198090Srdivacky VT, Operand, Subreg, SRIdxVal); 4855198090Srdivacky return SDValue(Result, 0); 4856198090Srdivacky} 4857198090Srdivacky 4858193323Sed/// getNodeIfExists - Get the specified node if it's already available, or 4859193323Sed/// else return NULL. 4860193323SedSDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList, 4861193323Sed const SDValue *Ops, unsigned NumOps) { 4862193323Sed if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) { 4863193323Sed FoldingSetNodeID ID; 4864193323Sed AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 4865193323Sed void *IP = 0; 4866201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4867193323Sed return E; 4868193323Sed } 4869193323Sed return NULL; 4870193323Sed} 4871193323Sed 4872204792Srdivackynamespace { 4873204792Srdivacky 4874204792Srdivacky/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node 4875204792Srdivacky/// pointed to by a use iterator is deleted, increment the use iterator 4876204792Srdivacky/// so that it doesn't dangle. 4877204792Srdivacky/// 4878204792Srdivacky/// This class also manages a "downlink" DAGUpdateListener, to forward 4879204792Srdivacky/// messages to ReplaceAllUsesWith's callers. 4880204792Srdivacky/// 4881204792Srdivackyclass RAUWUpdateListener : public SelectionDAG::DAGUpdateListener { 4882204792Srdivacky SelectionDAG::DAGUpdateListener *DownLink; 4883204792Srdivacky SDNode::use_iterator &UI; 4884204792Srdivacky SDNode::use_iterator &UE; 4885204792Srdivacky 4886204792Srdivacky virtual void NodeDeleted(SDNode *N, SDNode *E) { 4887204792Srdivacky // Increment the iterator as needed. 4888204792Srdivacky while (UI != UE && N == *UI) 4889204792Srdivacky ++UI; 4890204792Srdivacky 4891204792Srdivacky // Then forward the message. 4892204792Srdivacky if (DownLink) DownLink->NodeDeleted(N, E); 4893204792Srdivacky } 4894204792Srdivacky 4895204792Srdivacky virtual void NodeUpdated(SDNode *N) { 4896204792Srdivacky // Just forward the message. 4897204792Srdivacky if (DownLink) DownLink->NodeUpdated(N); 4898204792Srdivacky } 4899204792Srdivacky 4900204792Srdivackypublic: 4901204792Srdivacky RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl, 4902204792Srdivacky SDNode::use_iterator &ui, 4903204792Srdivacky SDNode::use_iterator &ue) 4904204792Srdivacky : DownLink(dl), UI(ui), UE(ue) {} 4905204792Srdivacky}; 4906204792Srdivacky 4907204792Srdivacky} 4908204792Srdivacky 4909193323Sed/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 4910193323Sed/// This can cause recursive merging of nodes in the DAG. 4911193323Sed/// 4912193323Sed/// This version assumes From has a single result value. 4913193323Sed/// 4914193323Sedvoid SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To, 4915193323Sed DAGUpdateListener *UpdateListener) { 4916193323Sed SDNode *From = FromN.getNode(); 4917193323Sed assert(From->getNumValues() == 1 && FromN.getResNo() == 0 && 4918193323Sed "Cannot replace with this method!"); 4919193323Sed assert(From != To.getNode() && "Cannot replace uses of with self"); 4920193323Sed 4921193323Sed // Iterate over all the existing uses of From. New uses will be added 4922193323Sed // to the beginning of the use list, which we avoid visiting. 4923193323Sed // This specifically avoids visiting uses of From that arise while the 4924193323Sed // replacement is happening, because any such uses would be the result 4925193323Sed // of CSE: If an existing node looks like From after one of its operands 4926193323Sed // is replaced by To, we don't want to replace of all its users with To 4927193323Sed // too. See PR3018 for more info. 4928193323Sed SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 4929204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 4930193323Sed while (UI != UE) { 4931193323Sed SDNode *User = *UI; 4932193323Sed 4933193323Sed // This node is about to morph, remove its old self from the CSE maps. 4934193323Sed RemoveNodeFromCSEMaps(User); 4935193323Sed 4936193323Sed // A user can appear in a use list multiple times, and when this 4937193323Sed // happens the uses are usually next to each other in the list. 4938193323Sed // To help reduce the number of CSE recomputations, process all 4939193323Sed // the uses of this user that we can find this way. 4940193323Sed do { 4941193323Sed SDUse &Use = UI.getUse(); 4942193323Sed ++UI; 4943193323Sed Use.set(To); 4944193323Sed } while (UI != UE && *UI == User); 4945193323Sed 4946193323Sed // Now that we have modified User, add it back to the CSE maps. If it 4947193323Sed // already exists there, recursively merge the results together. 4948204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 4949193323Sed } 4950193323Sed} 4951193323Sed 4952193323Sed/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 4953193323Sed/// This can cause recursive merging of nodes in the DAG. 4954193323Sed/// 4955193323Sed/// This version assumes that for each value of From, there is a 4956193323Sed/// corresponding value in To in the same position with the same type. 4957193323Sed/// 4958193323Sedvoid SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 4959193323Sed DAGUpdateListener *UpdateListener) { 4960193323Sed#ifndef NDEBUG 4961193323Sed for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) 4962193323Sed assert((!From->hasAnyUseOfValue(i) || 4963193323Sed From->getValueType(i) == To->getValueType(i)) && 4964193323Sed "Cannot use this version of ReplaceAllUsesWith!"); 4965193323Sed#endif 4966193323Sed 4967193323Sed // Handle the trivial case. 4968193323Sed if (From == To) 4969193323Sed return; 4970193323Sed 4971193323Sed // Iterate over just the existing users of From. See the comments in 4972193323Sed // the ReplaceAllUsesWith above. 4973193323Sed SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 4974204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 4975193323Sed while (UI != UE) { 4976193323Sed SDNode *User = *UI; 4977193323Sed 4978193323Sed // This node is about to morph, remove its old self from the CSE maps. 4979193323Sed RemoveNodeFromCSEMaps(User); 4980193323Sed 4981193323Sed // A user can appear in a use list multiple times, and when this 4982193323Sed // happens the uses are usually next to each other in the list. 4983193323Sed // To help reduce the number of CSE recomputations, process all 4984193323Sed // the uses of this user that we can find this way. 4985193323Sed do { 4986193323Sed SDUse &Use = UI.getUse(); 4987193323Sed ++UI; 4988193323Sed Use.setNode(To); 4989193323Sed } while (UI != UE && *UI == User); 4990193323Sed 4991193323Sed // Now that we have modified User, add it back to the CSE maps. If it 4992193323Sed // already exists there, recursively merge the results together. 4993204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 4994193323Sed } 4995193323Sed} 4996193323Sed 4997193323Sed/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 4998193323Sed/// This can cause recursive merging of nodes in the DAG. 4999193323Sed/// 5000193323Sed/// This version can replace From with any result values. To must match the 5001193323Sed/// number and types of values returned by From. 5002193323Sedvoid SelectionDAG::ReplaceAllUsesWith(SDNode *From, 5003193323Sed const SDValue *To, 5004193323Sed DAGUpdateListener *UpdateListener) { 5005193323Sed if (From->getNumValues() == 1) // Handle the simple case efficiently. 5006193323Sed return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener); 5007193323Sed 5008193323Sed // Iterate over just the existing users of From. See the comments in 5009193323Sed // the ReplaceAllUsesWith above. 5010193323Sed SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 5011204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 5012193323Sed while (UI != UE) { 5013193323Sed SDNode *User = *UI; 5014193323Sed 5015193323Sed // This node is about to morph, remove its old self from the CSE maps. 5016193323Sed RemoveNodeFromCSEMaps(User); 5017193323Sed 5018193323Sed // A user can appear in a use list multiple times, and when this 5019193323Sed // happens the uses are usually next to each other in the list. 5020193323Sed // To help reduce the number of CSE recomputations, process all 5021193323Sed // the uses of this user that we can find this way. 5022193323Sed do { 5023193323Sed SDUse &Use = UI.getUse(); 5024193323Sed const SDValue &ToOp = To[Use.getResNo()]; 5025193323Sed ++UI; 5026193323Sed Use.set(ToOp); 5027193323Sed } while (UI != UE && *UI == User); 5028193323Sed 5029193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5030193323Sed // already exists there, recursively merge the results together. 5031204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 5032193323Sed } 5033193323Sed} 5034193323Sed 5035193323Sed/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving 5036193323Sed/// uses of other values produced by From.getNode() alone. The Deleted 5037193323Sed/// vector is handled the same way as for ReplaceAllUsesWith. 5038193323Sedvoid SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To, 5039193323Sed DAGUpdateListener *UpdateListener){ 5040193323Sed // Handle the really simple, really trivial case efficiently. 5041193323Sed if (From == To) return; 5042193323Sed 5043193323Sed // Handle the simple, trivial, case efficiently. 5044193323Sed if (From.getNode()->getNumValues() == 1) { 5045193323Sed ReplaceAllUsesWith(From, To, UpdateListener); 5046193323Sed return; 5047193323Sed } 5048193323Sed 5049193323Sed // Iterate over just the existing users of From. See the comments in 5050193323Sed // the ReplaceAllUsesWith above. 5051193323Sed SDNode::use_iterator UI = From.getNode()->use_begin(), 5052193323Sed UE = From.getNode()->use_end(); 5053204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 5054193323Sed while (UI != UE) { 5055193323Sed SDNode *User = *UI; 5056193323Sed bool UserRemovedFromCSEMaps = false; 5057193323Sed 5058193323Sed // A user can appear in a use list multiple times, and when this 5059193323Sed // happens the uses are usually next to each other in the list. 5060193323Sed // To help reduce the number of CSE recomputations, process all 5061193323Sed // the uses of this user that we can find this way. 5062193323Sed do { 5063193323Sed SDUse &Use = UI.getUse(); 5064193323Sed 5065193323Sed // Skip uses of different values from the same node. 5066193323Sed if (Use.getResNo() != From.getResNo()) { 5067193323Sed ++UI; 5068193323Sed continue; 5069193323Sed } 5070193323Sed 5071193323Sed // If this node hasn't been modified yet, it's still in the CSE maps, 5072193323Sed // so remove its old self from the CSE maps. 5073193323Sed if (!UserRemovedFromCSEMaps) { 5074193323Sed RemoveNodeFromCSEMaps(User); 5075193323Sed UserRemovedFromCSEMaps = true; 5076193323Sed } 5077193323Sed 5078193323Sed ++UI; 5079193323Sed Use.set(To); 5080193323Sed } while (UI != UE && *UI == User); 5081193323Sed 5082193323Sed // We are iterating over all uses of the From node, so if a use 5083193323Sed // doesn't use the specific value, no changes are made. 5084193323Sed if (!UserRemovedFromCSEMaps) 5085193323Sed continue; 5086193323Sed 5087193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5088193323Sed // already exists there, recursively merge the results together. 5089204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 5090193323Sed } 5091193323Sed} 5092193323Sed 5093193323Sednamespace { 5094193323Sed /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith 5095193323Sed /// to record information about a use. 5096193323Sed struct UseMemo { 5097193323Sed SDNode *User; 5098193323Sed unsigned Index; 5099193323Sed SDUse *Use; 5100193323Sed }; 5101193323Sed 5102193323Sed /// operator< - Sort Memos by User. 5103193323Sed bool operator<(const UseMemo &L, const UseMemo &R) { 5104193323Sed return (intptr_t)L.User < (intptr_t)R.User; 5105193323Sed } 5106193323Sed} 5107193323Sed 5108193323Sed/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving 5109193323Sed/// uses of other values produced by From.getNode() alone. The same value 5110193323Sed/// may appear in both the From and To list. The Deleted vector is 5111193323Sed/// handled the same way as for ReplaceAllUsesWith. 5112193323Sedvoid SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From, 5113193323Sed const SDValue *To, 5114193323Sed unsigned Num, 5115193323Sed DAGUpdateListener *UpdateListener){ 5116193323Sed // Handle the simple, trivial case efficiently. 5117193323Sed if (Num == 1) 5118193323Sed return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener); 5119193323Sed 5120193323Sed // Read up all the uses and make records of them. This helps 5121193323Sed // processing new uses that are introduced during the 5122193323Sed // replacement process. 5123193323Sed SmallVector<UseMemo, 4> Uses; 5124193323Sed for (unsigned i = 0; i != Num; ++i) { 5125193323Sed unsigned FromResNo = From[i].getResNo(); 5126193323Sed SDNode *FromNode = From[i].getNode(); 5127193323Sed for (SDNode::use_iterator UI = FromNode->use_begin(), 5128193323Sed E = FromNode->use_end(); UI != E; ++UI) { 5129193323Sed SDUse &Use = UI.getUse(); 5130193323Sed if (Use.getResNo() == FromResNo) { 5131193323Sed UseMemo Memo = { *UI, i, &Use }; 5132193323Sed Uses.push_back(Memo); 5133193323Sed } 5134193323Sed } 5135193323Sed } 5136193323Sed 5137193323Sed // Sort the uses, so that all the uses from a given User are together. 5138193323Sed std::sort(Uses.begin(), Uses.end()); 5139193323Sed 5140193323Sed for (unsigned UseIndex = 0, UseIndexEnd = Uses.size(); 5141193323Sed UseIndex != UseIndexEnd; ) { 5142193323Sed // We know that this user uses some value of From. If it is the right 5143193323Sed // value, update it. 5144193323Sed SDNode *User = Uses[UseIndex].User; 5145193323Sed 5146193323Sed // This node is about to morph, remove its old self from the CSE maps. 5147193323Sed RemoveNodeFromCSEMaps(User); 5148193323Sed 5149193323Sed // The Uses array is sorted, so all the uses for a given User 5150193323Sed // are next to each other in the list. 5151193323Sed // To help reduce the number of CSE recomputations, process all 5152193323Sed // the uses of this user that we can find this way. 5153193323Sed do { 5154193323Sed unsigned i = Uses[UseIndex].Index; 5155193323Sed SDUse &Use = *Uses[UseIndex].Use; 5156193323Sed ++UseIndex; 5157193323Sed 5158193323Sed Use.set(To[i]); 5159193323Sed } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User); 5160193323Sed 5161193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5162193323Sed // already exists there, recursively merge the results together. 5163193323Sed AddModifiedNodeToCSEMaps(User, UpdateListener); 5164193323Sed } 5165193323Sed} 5166193323Sed 5167193323Sed/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG 5168193323Sed/// based on their topological order. It returns the maximum id and a vector 5169193323Sed/// of the SDNodes* in assigned order by reference. 5170193323Sedunsigned SelectionDAG::AssignTopologicalOrder() { 5171193323Sed 5172193323Sed unsigned DAGSize = 0; 5173193323Sed 5174193323Sed // SortedPos tracks the progress of the algorithm. Nodes before it are 5175193323Sed // sorted, nodes after it are unsorted. When the algorithm completes 5176193323Sed // it is at the end of the list. 5177193323Sed allnodes_iterator SortedPos = allnodes_begin(); 5178193323Sed 5179193323Sed // Visit all the nodes. Move nodes with no operands to the front of 5180193323Sed // the list immediately. Annotate nodes that do have operands with their 5181193323Sed // operand count. Before we do this, the Node Id fields of the nodes 5182193323Sed // may contain arbitrary values. After, the Node Id fields for nodes 5183193323Sed // before SortedPos will contain the topological sort index, and the 5184193323Sed // Node Id fields for nodes At SortedPos and after will contain the 5185193323Sed // count of outstanding operands. 5186193323Sed for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) { 5187193323Sed SDNode *N = I++; 5188202878Srdivacky checkForCycles(N); 5189193323Sed unsigned Degree = N->getNumOperands(); 5190193323Sed if (Degree == 0) { 5191193323Sed // A node with no uses, add it to the result array immediately. 5192193323Sed N->setNodeId(DAGSize++); 5193193323Sed allnodes_iterator Q = N; 5194193323Sed if (Q != SortedPos) 5195193323Sed SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q)); 5196202878Srdivacky assert(SortedPos != AllNodes.end() && "Overran node list"); 5197193323Sed ++SortedPos; 5198193323Sed } else { 5199193323Sed // Temporarily use the Node Id as scratch space for the degree count. 5200193323Sed N->setNodeId(Degree); 5201193323Sed } 5202193323Sed } 5203193323Sed 5204193323Sed // Visit all the nodes. As we iterate, moves nodes into sorted order, 5205193323Sed // such that by the time the end is reached all nodes will be sorted. 5206193323Sed for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) { 5207193323Sed SDNode *N = I; 5208202878Srdivacky checkForCycles(N); 5209202878Srdivacky // N is in sorted position, so all its uses have one less operand 5210202878Srdivacky // that needs to be sorted. 5211193323Sed for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 5212193323Sed UI != UE; ++UI) { 5213193323Sed SDNode *P = *UI; 5214193323Sed unsigned Degree = P->getNodeId(); 5215202878Srdivacky assert(Degree != 0 && "Invalid node degree"); 5216193323Sed --Degree; 5217193323Sed if (Degree == 0) { 5218193323Sed // All of P's operands are sorted, so P may sorted now. 5219193323Sed P->setNodeId(DAGSize++); 5220193323Sed if (P != SortedPos) 5221193323Sed SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P)); 5222202878Srdivacky assert(SortedPos != AllNodes.end() && "Overran node list"); 5223193323Sed ++SortedPos; 5224193323Sed } else { 5225193323Sed // Update P's outstanding operand count. 5226193323Sed P->setNodeId(Degree); 5227193323Sed } 5228193323Sed } 5229202878Srdivacky if (I == SortedPos) { 5230203954Srdivacky#ifndef NDEBUG 5231203954Srdivacky SDNode *S = ++I; 5232203954Srdivacky dbgs() << "Overran sorted position:\n"; 5233202878Srdivacky S->dumprFull(); 5234203954Srdivacky#endif 5235203954Srdivacky llvm_unreachable(0); 5236202878Srdivacky } 5237193323Sed } 5238193323Sed 5239193323Sed assert(SortedPos == AllNodes.end() && 5240193323Sed "Topological sort incomplete!"); 5241193323Sed assert(AllNodes.front().getOpcode() == ISD::EntryToken && 5242193323Sed "First node in topological sort is not the entry token!"); 5243193323Sed assert(AllNodes.front().getNodeId() == 0 && 5244193323Sed "First node in topological sort has non-zero id!"); 5245193323Sed assert(AllNodes.front().getNumOperands() == 0 && 5246193323Sed "First node in topological sort has operands!"); 5247193323Sed assert(AllNodes.back().getNodeId() == (int)DAGSize-1 && 5248193323Sed "Last node in topologic sort has unexpected id!"); 5249193323Sed assert(AllNodes.back().use_empty() && 5250193323Sed "Last node in topologic sort has users!"); 5251193323Sed assert(DAGSize == allnodes_size() && "Node count mismatch!"); 5252193323Sed return DAGSize; 5253193323Sed} 5254193323Sed 5255201360Srdivacky/// AssignOrdering - Assign an order to the SDNode. 5256203954Srdivackyvoid SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) { 5257201360Srdivacky assert(SD && "Trying to assign an order to a null node!"); 5258202878Srdivacky Ordering->add(SD, Order); 5259201360Srdivacky} 5260193323Sed 5261201360Srdivacky/// GetOrdering - Get the order for the SDNode. 5262201360Srdivackyunsigned SelectionDAG::GetOrdering(const SDNode *SD) const { 5263201360Srdivacky assert(SD && "Trying to get the order of a null node!"); 5264202878Srdivacky return Ordering->getOrder(SD); 5265201360Srdivacky} 5266193323Sed 5267201360Srdivacky 5268193323Sed//===----------------------------------------------------------------------===// 5269193323Sed// SDNode Class 5270193323Sed//===----------------------------------------------------------------------===// 5271193323Sed 5272193323SedHandleSDNode::~HandleSDNode() { 5273193323Sed DropOperands(); 5274193323Sed} 5275193323Sed 5276195098SedGlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA, 5277198090Srdivacky EVT VT, int64_t o, unsigned char TF) 5278195098Sed : SDNode(Opc, DebugLoc::getUnknownLoc(), getSDVTList(VT)), 5279195098Sed Offset(o), TargetFlags(TF) { 5280193323Sed TheGlobal = const_cast<GlobalValue*>(GA); 5281193323Sed} 5282193323Sed 5283198090SrdivackyMemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt, 5284198090Srdivacky MachineMemOperand *mmo) 5285198090Srdivacky : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) { 5286204642Srdivacky SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), 5287204642Srdivacky MMO->isNonTemporal()); 5288198090Srdivacky assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); 5289204642Srdivacky assert(isNonTemporal() == MMO->isNonTemporal() && 5290204642Srdivacky "Non-temporal encoding error!"); 5291198090Srdivacky assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!"); 5292193323Sed} 5293193323Sed 5294193323SedMemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, 5295198090Srdivacky const SDValue *Ops, unsigned NumOps, EVT memvt, 5296198090Srdivacky MachineMemOperand *mmo) 5297193323Sed : SDNode(Opc, dl, VTs, Ops, NumOps), 5298198090Srdivacky MemoryVT(memvt), MMO(mmo) { 5299204642Srdivacky SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), 5300204642Srdivacky MMO->isNonTemporal()); 5301198090Srdivacky assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); 5302198090Srdivacky assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!"); 5303193323Sed} 5304193323Sed 5305193323Sed/// Profile - Gather unique data for the node. 5306193323Sed/// 5307193323Sedvoid SDNode::Profile(FoldingSetNodeID &ID) const { 5308193323Sed AddNodeIDNode(ID, this); 5309193323Sed} 5310193323Sed 5311198090Srdivackynamespace { 5312198090Srdivacky struct EVTArray { 5313198090Srdivacky std::vector<EVT> VTs; 5314198090Srdivacky 5315198090Srdivacky EVTArray() { 5316198090Srdivacky VTs.reserve(MVT::LAST_VALUETYPE); 5317198090Srdivacky for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i) 5318198090Srdivacky VTs.push_back(MVT((MVT::SimpleValueType)i)); 5319198090Srdivacky } 5320198090Srdivacky }; 5321198090Srdivacky} 5322198090Srdivacky 5323198090Srdivackystatic ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs; 5324198090Srdivackystatic ManagedStatic<EVTArray> SimpleVTArray; 5325195098Sedstatic ManagedStatic<sys::SmartMutex<true> > VTMutex; 5326195098Sed 5327193323Sed/// getValueTypeList - Return a pointer to the specified value type. 5328193323Sed/// 5329198090Srdivackyconst EVT *SDNode::getValueTypeList(EVT VT) { 5330193323Sed if (VT.isExtended()) { 5331198090Srdivacky sys::SmartScopedLock<true> Lock(*VTMutex); 5332195098Sed return &(*EVTs->insert(VT).first); 5333193323Sed } else { 5334198090Srdivacky return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy]; 5335193323Sed } 5336193323Sed} 5337193323Sed 5338193323Sed/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 5339193323Sed/// indicated value. This method ignores uses of other values defined by this 5340193323Sed/// operation. 5341193323Sedbool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { 5342193323Sed assert(Value < getNumValues() && "Bad value!"); 5343193323Sed 5344193323Sed // TODO: Only iterate over uses of a given value of the node 5345193323Sed for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) { 5346193323Sed if (UI.getUse().getResNo() == Value) { 5347193323Sed if (NUses == 0) 5348193323Sed return false; 5349193323Sed --NUses; 5350193323Sed } 5351193323Sed } 5352193323Sed 5353193323Sed // Found exactly the right number of uses? 5354193323Sed return NUses == 0; 5355193323Sed} 5356193323Sed 5357193323Sed 5358193323Sed/// hasAnyUseOfValue - Return true if there are any use of the indicated 5359193323Sed/// value. This method ignores uses of other values defined by this operation. 5360193323Sedbool SDNode::hasAnyUseOfValue(unsigned Value) const { 5361193323Sed assert(Value < getNumValues() && "Bad value!"); 5362193323Sed 5363193323Sed for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) 5364193323Sed if (UI.getUse().getResNo() == Value) 5365193323Sed return true; 5366193323Sed 5367193323Sed return false; 5368193323Sed} 5369193323Sed 5370193323Sed 5371193323Sed/// isOnlyUserOf - Return true if this node is the only use of N. 5372193323Sed/// 5373193323Sedbool SDNode::isOnlyUserOf(SDNode *N) const { 5374193323Sed bool Seen = false; 5375193323Sed for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 5376193323Sed SDNode *User = *I; 5377193323Sed if (User == this) 5378193323Sed Seen = true; 5379193323Sed else 5380193323Sed return false; 5381193323Sed } 5382193323Sed 5383193323Sed return Seen; 5384193323Sed} 5385193323Sed 5386193323Sed/// isOperand - Return true if this node is an operand of N. 5387193323Sed/// 5388193323Sedbool SDValue::isOperandOf(SDNode *N) const { 5389193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 5390193323Sed if (*this == N->getOperand(i)) 5391193323Sed return true; 5392193323Sed return false; 5393193323Sed} 5394193323Sed 5395193323Sedbool SDNode::isOperandOf(SDNode *N) const { 5396193323Sed for (unsigned i = 0, e = N->NumOperands; i != e; ++i) 5397193323Sed if (this == N->OperandList[i].getNode()) 5398193323Sed return true; 5399193323Sed return false; 5400193323Sed} 5401193323Sed 5402193323Sed/// reachesChainWithoutSideEffects - Return true if this operand (which must 5403193323Sed/// be a chain) reaches the specified operand without crossing any 5404193323Sed/// side-effecting instructions. In practice, this looks through token 5405193323Sed/// factors and non-volatile loads. In order to remain efficient, this only 5406193323Sed/// looks a couple of nodes in, it does not do an exhaustive search. 5407193323Sedbool SDValue::reachesChainWithoutSideEffects(SDValue Dest, 5408193323Sed unsigned Depth) const { 5409193323Sed if (*this == Dest) return true; 5410193323Sed 5411193323Sed // Don't search too deeply, we just want to be able to see through 5412193323Sed // TokenFactor's etc. 5413193323Sed if (Depth == 0) return false; 5414193323Sed 5415193323Sed // If this is a token factor, all inputs to the TF happen in parallel. If any 5416193323Sed // of the operands of the TF reach dest, then we can do the xform. 5417193323Sed if (getOpcode() == ISD::TokenFactor) { 5418193323Sed for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 5419193323Sed if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1)) 5420193323Sed return true; 5421193323Sed return false; 5422193323Sed } 5423193323Sed 5424193323Sed // Loads don't have side effects, look through them. 5425193323Sed if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) { 5426193323Sed if (!Ld->isVolatile()) 5427193323Sed return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1); 5428193323Sed } 5429193323Sed return false; 5430193323Sed} 5431193323Sed 5432193323Sed/// isPredecessorOf - Return true if this node is a predecessor of N. This node 5433198892Srdivacky/// is either an operand of N or it can be reached by traversing up the operands. 5434193323Sed/// NOTE: this is an expensive method. Use it carefully. 5435193323Sedbool SDNode::isPredecessorOf(SDNode *N) const { 5436193323Sed SmallPtrSet<SDNode *, 32> Visited; 5437198892Srdivacky SmallVector<SDNode *, 16> Worklist; 5438198892Srdivacky Worklist.push_back(N); 5439198892Srdivacky 5440198892Srdivacky do { 5441198892Srdivacky N = Worklist.pop_back_val(); 5442198892Srdivacky for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 5443198892Srdivacky SDNode *Op = N->getOperand(i).getNode(); 5444198892Srdivacky if (Op == this) 5445198892Srdivacky return true; 5446198892Srdivacky if (Visited.insert(Op)) 5447198892Srdivacky Worklist.push_back(Op); 5448198892Srdivacky } 5449198892Srdivacky } while (!Worklist.empty()); 5450198892Srdivacky 5451198892Srdivacky return false; 5452193323Sed} 5453193323Sed 5454193323Seduint64_t SDNode::getConstantOperandVal(unsigned Num) const { 5455193323Sed assert(Num < NumOperands && "Invalid child # of SDNode!"); 5456193323Sed return cast<ConstantSDNode>(OperandList[Num])->getZExtValue(); 5457193323Sed} 5458193323Sed 5459193323Sedstd::string SDNode::getOperationName(const SelectionDAG *G) const { 5460193323Sed switch (getOpcode()) { 5461193323Sed default: 5462193323Sed if (getOpcode() < ISD::BUILTIN_OP_END) 5463193323Sed return "<<Unknown DAG Node>>"; 5464193323Sed if (isMachineOpcode()) { 5465193323Sed if (G) 5466193323Sed if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 5467193323Sed if (getMachineOpcode() < TII->getNumOpcodes()) 5468193323Sed return TII->get(getMachineOpcode()).getName(); 5469204642Srdivacky return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>"; 5470193323Sed } 5471193323Sed if (G) { 5472193323Sed const TargetLowering &TLI = G->getTargetLoweringInfo(); 5473193323Sed const char *Name = TLI.getTargetNodeName(getOpcode()); 5474193323Sed if (Name) return Name; 5475204642Srdivacky return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>"; 5476193323Sed } 5477204642Srdivacky return "<<Unknown Node #" + utostr(getOpcode()) + ">>"; 5478193323Sed 5479193323Sed#ifndef NDEBUG 5480193323Sed case ISD::DELETED_NODE: 5481193323Sed return "<<Deleted Node!>>"; 5482193323Sed#endif 5483193323Sed case ISD::PREFETCH: return "Prefetch"; 5484193323Sed case ISD::MEMBARRIER: return "MemBarrier"; 5485193323Sed case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap"; 5486193323Sed case ISD::ATOMIC_SWAP: return "AtomicSwap"; 5487193323Sed case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd"; 5488193323Sed case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub"; 5489193323Sed case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd"; 5490193323Sed case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr"; 5491193323Sed case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor"; 5492193323Sed case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand"; 5493193323Sed case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin"; 5494193323Sed case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax"; 5495193323Sed case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin"; 5496193323Sed case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax"; 5497193323Sed case ISD::PCMARKER: return "PCMarker"; 5498193323Sed case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 5499193323Sed case ISD::SRCVALUE: return "SrcValue"; 5500193323Sed case ISD::EntryToken: return "EntryToken"; 5501193323Sed case ISD::TokenFactor: return "TokenFactor"; 5502193323Sed case ISD::AssertSext: return "AssertSext"; 5503193323Sed case ISD::AssertZext: return "AssertZext"; 5504193323Sed 5505193323Sed case ISD::BasicBlock: return "BasicBlock"; 5506193323Sed case ISD::VALUETYPE: return "ValueType"; 5507193323Sed case ISD::Register: return "Register"; 5508193323Sed 5509193323Sed case ISD::Constant: return "Constant"; 5510193323Sed case ISD::ConstantFP: return "ConstantFP"; 5511193323Sed case ISD::GlobalAddress: return "GlobalAddress"; 5512193323Sed case ISD::GlobalTLSAddress: return "GlobalTLSAddress"; 5513193323Sed case ISD::FrameIndex: return "FrameIndex"; 5514193323Sed case ISD::JumpTable: return "JumpTable"; 5515193323Sed case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE"; 5516193323Sed case ISD::RETURNADDR: return "RETURNADDR"; 5517193323Sed case ISD::FRAMEADDR: return "FRAMEADDR"; 5518193323Sed case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET"; 5519193323Sed case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR"; 5520198090Srdivacky case ISD::LSDAADDR: return "LSDAADDR"; 5521193323Sed case ISD::EHSELECTION: return "EHSELECTION"; 5522193323Sed case ISD::EH_RETURN: return "EH_RETURN"; 5523193323Sed case ISD::ConstantPool: return "ConstantPool"; 5524193323Sed case ISD::ExternalSymbol: return "ExternalSymbol"; 5525198892Srdivacky case ISD::BlockAddress: return "BlockAddress"; 5526198396Srdivacky case ISD::INTRINSIC_WO_CHAIN: 5527193323Sed case ISD::INTRINSIC_VOID: 5528193323Sed case ISD::INTRINSIC_W_CHAIN: { 5529198396Srdivacky unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1; 5530198396Srdivacky unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue(); 5531198396Srdivacky if (IID < Intrinsic::num_intrinsics) 5532198396Srdivacky return Intrinsic::getName((Intrinsic::ID)IID); 5533198396Srdivacky else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo()) 5534198396Srdivacky return TII->getName(IID); 5535198396Srdivacky llvm_unreachable("Invalid intrinsic ID"); 5536193323Sed } 5537193323Sed 5538193323Sed case ISD::BUILD_VECTOR: return "BUILD_VECTOR"; 5539193323Sed case ISD::TargetConstant: return "TargetConstant"; 5540193323Sed case ISD::TargetConstantFP:return "TargetConstantFP"; 5541193323Sed case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 5542193323Sed case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress"; 5543193323Sed case ISD::TargetFrameIndex: return "TargetFrameIndex"; 5544193323Sed case ISD::TargetJumpTable: return "TargetJumpTable"; 5545193323Sed case ISD::TargetConstantPool: return "TargetConstantPool"; 5546193323Sed case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 5547198892Srdivacky case ISD::TargetBlockAddress: return "TargetBlockAddress"; 5548193323Sed 5549193323Sed case ISD::CopyToReg: return "CopyToReg"; 5550193323Sed case ISD::CopyFromReg: return "CopyFromReg"; 5551193323Sed case ISD::UNDEF: return "undef"; 5552193323Sed case ISD::MERGE_VALUES: return "merge_values"; 5553193323Sed case ISD::INLINEASM: return "inlineasm"; 5554193323Sed case ISD::EH_LABEL: return "eh_label"; 5555193323Sed case ISD::HANDLENODE: return "handlenode"; 5556193323Sed 5557193323Sed // Unary operators 5558193323Sed case ISD::FABS: return "fabs"; 5559193323Sed case ISD::FNEG: return "fneg"; 5560193323Sed case ISD::FSQRT: return "fsqrt"; 5561193323Sed case ISD::FSIN: return "fsin"; 5562193323Sed case ISD::FCOS: return "fcos"; 5563193323Sed case ISD::FPOWI: return "fpowi"; 5564193323Sed case ISD::FPOW: return "fpow"; 5565193323Sed case ISD::FTRUNC: return "ftrunc"; 5566193323Sed case ISD::FFLOOR: return "ffloor"; 5567193323Sed case ISD::FCEIL: return "fceil"; 5568193323Sed case ISD::FRINT: return "frint"; 5569193323Sed case ISD::FNEARBYINT: return "fnearbyint"; 5570193323Sed 5571193323Sed // Binary operators 5572193323Sed case ISD::ADD: return "add"; 5573193323Sed case ISD::SUB: return "sub"; 5574193323Sed case ISD::MUL: return "mul"; 5575193323Sed case ISD::MULHU: return "mulhu"; 5576193323Sed case ISD::MULHS: return "mulhs"; 5577193323Sed case ISD::SDIV: return "sdiv"; 5578193323Sed case ISD::UDIV: return "udiv"; 5579193323Sed case ISD::SREM: return "srem"; 5580193323Sed case ISD::UREM: return "urem"; 5581193323Sed case ISD::SMUL_LOHI: return "smul_lohi"; 5582193323Sed case ISD::UMUL_LOHI: return "umul_lohi"; 5583193323Sed case ISD::SDIVREM: return "sdivrem"; 5584193323Sed case ISD::UDIVREM: return "udivrem"; 5585193323Sed case ISD::AND: return "and"; 5586193323Sed case ISD::OR: return "or"; 5587193323Sed case ISD::XOR: return "xor"; 5588193323Sed case ISD::SHL: return "shl"; 5589193323Sed case ISD::SRA: return "sra"; 5590193323Sed case ISD::SRL: return "srl"; 5591193323Sed case ISD::ROTL: return "rotl"; 5592193323Sed case ISD::ROTR: return "rotr"; 5593193323Sed case ISD::FADD: return "fadd"; 5594193323Sed case ISD::FSUB: return "fsub"; 5595193323Sed case ISD::FMUL: return "fmul"; 5596193323Sed case ISD::FDIV: return "fdiv"; 5597193323Sed case ISD::FREM: return "frem"; 5598193323Sed case ISD::FCOPYSIGN: return "fcopysign"; 5599193323Sed case ISD::FGETSIGN: return "fgetsign"; 5600193323Sed 5601193323Sed case ISD::SETCC: return "setcc"; 5602193323Sed case ISD::VSETCC: return "vsetcc"; 5603193323Sed case ISD::SELECT: return "select"; 5604193323Sed case ISD::SELECT_CC: return "select_cc"; 5605193323Sed case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt"; 5606193323Sed case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt"; 5607193323Sed case ISD::CONCAT_VECTORS: return "concat_vectors"; 5608193323Sed case ISD::EXTRACT_SUBVECTOR: return "extract_subvector"; 5609193323Sed case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector"; 5610193323Sed case ISD::VECTOR_SHUFFLE: return "vector_shuffle"; 5611193323Sed case ISD::CARRY_FALSE: return "carry_false"; 5612193323Sed case ISD::ADDC: return "addc"; 5613193323Sed case ISD::ADDE: return "adde"; 5614193323Sed case ISD::SADDO: return "saddo"; 5615193323Sed case ISD::UADDO: return "uaddo"; 5616193323Sed case ISD::SSUBO: return "ssubo"; 5617193323Sed case ISD::USUBO: return "usubo"; 5618193323Sed case ISD::SMULO: return "smulo"; 5619193323Sed case ISD::UMULO: return "umulo"; 5620193323Sed case ISD::SUBC: return "subc"; 5621193323Sed case ISD::SUBE: return "sube"; 5622193323Sed case ISD::SHL_PARTS: return "shl_parts"; 5623193323Sed case ISD::SRA_PARTS: return "sra_parts"; 5624193323Sed case ISD::SRL_PARTS: return "srl_parts"; 5625193323Sed 5626193323Sed // Conversion operators. 5627193323Sed case ISD::SIGN_EXTEND: return "sign_extend"; 5628193323Sed case ISD::ZERO_EXTEND: return "zero_extend"; 5629193323Sed case ISD::ANY_EXTEND: return "any_extend"; 5630193323Sed case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 5631193323Sed case ISD::TRUNCATE: return "truncate"; 5632193323Sed case ISD::FP_ROUND: return "fp_round"; 5633193323Sed case ISD::FLT_ROUNDS_: return "flt_rounds"; 5634193323Sed case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 5635193323Sed case ISD::FP_EXTEND: return "fp_extend"; 5636193323Sed 5637193323Sed case ISD::SINT_TO_FP: return "sint_to_fp"; 5638193323Sed case ISD::UINT_TO_FP: return "uint_to_fp"; 5639193323Sed case ISD::FP_TO_SINT: return "fp_to_sint"; 5640193323Sed case ISD::FP_TO_UINT: return "fp_to_uint"; 5641193323Sed case ISD::BIT_CONVERT: return "bit_convert"; 5642193323Sed 5643193323Sed case ISD::CONVERT_RNDSAT: { 5644193323Sed switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) { 5645198090Srdivacky default: llvm_unreachable("Unknown cvt code!"); 5646193323Sed case ISD::CVT_FF: return "cvt_ff"; 5647193323Sed case ISD::CVT_FS: return "cvt_fs"; 5648193323Sed case ISD::CVT_FU: return "cvt_fu"; 5649193323Sed case ISD::CVT_SF: return "cvt_sf"; 5650193323Sed case ISD::CVT_UF: return "cvt_uf"; 5651193323Sed case ISD::CVT_SS: return "cvt_ss"; 5652193323Sed case ISD::CVT_SU: return "cvt_su"; 5653193323Sed case ISD::CVT_US: return "cvt_us"; 5654193323Sed case ISD::CVT_UU: return "cvt_uu"; 5655193323Sed } 5656193323Sed } 5657193323Sed 5658193323Sed // Control flow instructions 5659193323Sed case ISD::BR: return "br"; 5660193323Sed case ISD::BRIND: return "brind"; 5661193323Sed case ISD::BR_JT: return "br_jt"; 5662193323Sed case ISD::BRCOND: return "brcond"; 5663193323Sed case ISD::BR_CC: return "br_cc"; 5664193323Sed case ISD::CALLSEQ_START: return "callseq_start"; 5665193323Sed case ISD::CALLSEQ_END: return "callseq_end"; 5666193323Sed 5667193323Sed // Other operators 5668193323Sed case ISD::LOAD: return "load"; 5669193323Sed case ISD::STORE: return "store"; 5670193323Sed case ISD::VAARG: return "vaarg"; 5671193323Sed case ISD::VACOPY: return "vacopy"; 5672193323Sed case ISD::VAEND: return "vaend"; 5673193323Sed case ISD::VASTART: return "vastart"; 5674193323Sed case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 5675193323Sed case ISD::EXTRACT_ELEMENT: return "extract_element"; 5676193323Sed case ISD::BUILD_PAIR: return "build_pair"; 5677193323Sed case ISD::STACKSAVE: return "stacksave"; 5678193323Sed case ISD::STACKRESTORE: return "stackrestore"; 5679193323Sed case ISD::TRAP: return "trap"; 5680193323Sed 5681193323Sed // Bit manipulation 5682193323Sed case ISD::BSWAP: return "bswap"; 5683193323Sed case ISD::CTPOP: return "ctpop"; 5684193323Sed case ISD::CTTZ: return "cttz"; 5685193323Sed case ISD::CTLZ: return "ctlz"; 5686193323Sed 5687193323Sed // Trampolines 5688193323Sed case ISD::TRAMPOLINE: return "trampoline"; 5689193323Sed 5690193323Sed case ISD::CONDCODE: 5691193323Sed switch (cast<CondCodeSDNode>(this)->get()) { 5692198090Srdivacky default: llvm_unreachable("Unknown setcc condition!"); 5693193323Sed case ISD::SETOEQ: return "setoeq"; 5694193323Sed case ISD::SETOGT: return "setogt"; 5695193323Sed case ISD::SETOGE: return "setoge"; 5696193323Sed case ISD::SETOLT: return "setolt"; 5697193323Sed case ISD::SETOLE: return "setole"; 5698193323Sed case ISD::SETONE: return "setone"; 5699193323Sed 5700193323Sed case ISD::SETO: return "seto"; 5701193323Sed case ISD::SETUO: return "setuo"; 5702193323Sed case ISD::SETUEQ: return "setue"; 5703193323Sed case ISD::SETUGT: return "setugt"; 5704193323Sed case ISD::SETUGE: return "setuge"; 5705193323Sed case ISD::SETULT: return "setult"; 5706193323Sed case ISD::SETULE: return "setule"; 5707193323Sed case ISD::SETUNE: return "setune"; 5708193323Sed 5709193323Sed case ISD::SETEQ: return "seteq"; 5710193323Sed case ISD::SETGT: return "setgt"; 5711193323Sed case ISD::SETGE: return "setge"; 5712193323Sed case ISD::SETLT: return "setlt"; 5713193323Sed case ISD::SETLE: return "setle"; 5714193323Sed case ISD::SETNE: return "setne"; 5715193323Sed } 5716193323Sed } 5717193323Sed} 5718193323Sed 5719193323Sedconst char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) { 5720193323Sed switch (AM) { 5721193323Sed default: 5722193323Sed return ""; 5723193323Sed case ISD::PRE_INC: 5724193323Sed return "<pre-inc>"; 5725193323Sed case ISD::PRE_DEC: 5726193323Sed return "<pre-dec>"; 5727193323Sed case ISD::POST_INC: 5728193323Sed return "<post-inc>"; 5729193323Sed case ISD::POST_DEC: 5730193323Sed return "<post-dec>"; 5731193323Sed } 5732193323Sed} 5733193323Sed 5734193323Sedstd::string ISD::ArgFlagsTy::getArgFlagsString() { 5735193323Sed std::string S = "< "; 5736193323Sed 5737193323Sed if (isZExt()) 5738193323Sed S += "zext "; 5739193323Sed if (isSExt()) 5740193323Sed S += "sext "; 5741193323Sed if (isInReg()) 5742193323Sed S += "inreg "; 5743193323Sed if (isSRet()) 5744193323Sed S += "sret "; 5745193323Sed if (isByVal()) 5746193323Sed S += "byval "; 5747193323Sed if (isNest()) 5748193323Sed S += "nest "; 5749193323Sed if (getByValAlign()) 5750193323Sed S += "byval-align:" + utostr(getByValAlign()) + " "; 5751193323Sed if (getOrigAlign()) 5752193323Sed S += "orig-align:" + utostr(getOrigAlign()) + " "; 5753193323Sed if (getByValSize()) 5754193323Sed S += "byval-size:" + utostr(getByValSize()) + " "; 5755193323Sed return S + ">"; 5756193323Sed} 5757193323Sed 5758193323Sedvoid SDNode::dump() const { dump(0); } 5759193323Sedvoid SDNode::dump(const SelectionDAG *G) const { 5760202375Srdivacky print(dbgs(), G); 5761193323Sed} 5762193323Sed 5763193323Sedvoid SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const { 5764193323Sed OS << (void*)this << ": "; 5765193323Sed 5766193323Sed for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 5767193323Sed if (i) OS << ","; 5768193323Sed if (getValueType(i) == MVT::Other) 5769193323Sed OS << "ch"; 5770193323Sed else 5771198090Srdivacky OS << getValueType(i).getEVTString(); 5772193323Sed } 5773193323Sed OS << " = " << getOperationName(G); 5774193323Sed} 5775193323Sed 5776193323Sedvoid SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const { 5777198090Srdivacky if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) { 5778198090Srdivacky if (!MN->memoperands_empty()) { 5779198090Srdivacky OS << "<"; 5780198090Srdivacky OS << "Mem:"; 5781198090Srdivacky for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(), 5782198090Srdivacky e = MN->memoperands_end(); i != e; ++i) { 5783198090Srdivacky OS << **i; 5784198090Srdivacky if (next(i) != e) 5785198090Srdivacky OS << " "; 5786198090Srdivacky } 5787198090Srdivacky OS << ">"; 5788198090Srdivacky } 5789198090Srdivacky } else if (const ShuffleVectorSDNode *SVN = 5790198090Srdivacky dyn_cast<ShuffleVectorSDNode>(this)) { 5791193323Sed OS << "<"; 5792193323Sed for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) { 5793193323Sed int Idx = SVN->getMaskElt(i); 5794193323Sed if (i) OS << ","; 5795193323Sed if (Idx < 0) 5796193323Sed OS << "u"; 5797193323Sed else 5798193323Sed OS << Idx; 5799193323Sed } 5800193323Sed OS << ">"; 5801198090Srdivacky } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 5802193323Sed OS << '<' << CSDN->getAPIntValue() << '>'; 5803193323Sed } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 5804193323Sed if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle) 5805193323Sed OS << '<' << CSDN->getValueAPF().convertToFloat() << '>'; 5806193323Sed else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble) 5807193323Sed OS << '<' << CSDN->getValueAPF().convertToDouble() << '>'; 5808193323Sed else { 5809193323Sed OS << "<APFloat("; 5810193323Sed CSDN->getValueAPF().bitcastToAPInt().dump(); 5811193323Sed OS << ")>"; 5812193323Sed } 5813193323Sed } else if (const GlobalAddressSDNode *GADN = 5814193323Sed dyn_cast<GlobalAddressSDNode>(this)) { 5815193323Sed int64_t offset = GADN->getOffset(); 5816193323Sed OS << '<'; 5817193323Sed WriteAsOperand(OS, GADN->getGlobal()); 5818193323Sed OS << '>'; 5819193323Sed if (offset > 0) 5820193323Sed OS << " + " << offset; 5821193323Sed else 5822193323Sed OS << " " << offset; 5823198090Srdivacky if (unsigned int TF = GADN->getTargetFlags()) 5824195098Sed OS << " [TF=" << TF << ']'; 5825193323Sed } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 5826193323Sed OS << "<" << FIDN->getIndex() << ">"; 5827193323Sed } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) { 5828193323Sed OS << "<" << JTDN->getIndex() << ">"; 5829198090Srdivacky if (unsigned int TF = JTDN->getTargetFlags()) 5830195098Sed OS << " [TF=" << TF << ']'; 5831193323Sed } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 5832193323Sed int offset = CP->getOffset(); 5833193323Sed if (CP->isMachineConstantPoolEntry()) 5834193323Sed OS << "<" << *CP->getMachineCPVal() << ">"; 5835193323Sed else 5836193323Sed OS << "<" << *CP->getConstVal() << ">"; 5837193323Sed if (offset > 0) 5838193323Sed OS << " + " << offset; 5839193323Sed else 5840193323Sed OS << " " << offset; 5841198090Srdivacky if (unsigned int TF = CP->getTargetFlags()) 5842195098Sed OS << " [TF=" << TF << ']'; 5843193323Sed } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 5844193323Sed OS << "<"; 5845193323Sed const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 5846193323Sed if (LBB) 5847193323Sed OS << LBB->getName() << " "; 5848193323Sed OS << (const void*)BBDN->getBasicBlock() << ">"; 5849193323Sed } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 5850193323Sed if (G && R->getReg() && 5851193323Sed TargetRegisterInfo::isPhysicalRegister(R->getReg())) { 5852198892Srdivacky OS << " %" << G->getTarget().getRegisterInfo()->getName(R->getReg()); 5853193323Sed } else { 5854198892Srdivacky OS << " %reg" << R->getReg(); 5855193323Sed } 5856193323Sed } else if (const ExternalSymbolSDNode *ES = 5857193323Sed dyn_cast<ExternalSymbolSDNode>(this)) { 5858193323Sed OS << "'" << ES->getSymbol() << "'"; 5859198090Srdivacky if (unsigned int TF = ES->getTargetFlags()) 5860195098Sed OS << " [TF=" << TF << ']'; 5861193323Sed } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 5862193323Sed if (M->getValue()) 5863193323Sed OS << "<" << M->getValue() << ">"; 5864193323Sed else 5865193323Sed OS << "<null>"; 5866193323Sed } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 5867198090Srdivacky OS << ":" << N->getVT().getEVTString(); 5868193323Sed } 5869193323Sed else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) { 5870198892Srdivacky OS << "<" << *LD->getMemOperand(); 5871193323Sed 5872193323Sed bool doExt = true; 5873193323Sed switch (LD->getExtensionType()) { 5874193323Sed default: doExt = false; break; 5875198090Srdivacky case ISD::EXTLOAD: OS << ", anyext"; break; 5876198090Srdivacky case ISD::SEXTLOAD: OS << ", sext"; break; 5877198090Srdivacky case ISD::ZEXTLOAD: OS << ", zext"; break; 5878193323Sed } 5879193323Sed if (doExt) 5880198090Srdivacky OS << " from " << LD->getMemoryVT().getEVTString(); 5881193323Sed 5882193323Sed const char *AM = getIndexedModeName(LD->getAddressingMode()); 5883193323Sed if (*AM) 5884198090Srdivacky OS << ", " << AM; 5885198090Srdivacky 5886198090Srdivacky OS << ">"; 5887193323Sed } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) { 5888198892Srdivacky OS << "<" << *ST->getMemOperand(); 5889193323Sed 5890193323Sed if (ST->isTruncatingStore()) 5891198090Srdivacky OS << ", trunc to " << ST->getMemoryVT().getEVTString(); 5892193323Sed 5893193323Sed const char *AM = getIndexedModeName(ST->getAddressingMode()); 5894193323Sed if (*AM) 5895198090Srdivacky OS << ", " << AM; 5896198090Srdivacky 5897198090Srdivacky OS << ">"; 5898198090Srdivacky } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) { 5899198892Srdivacky OS << "<" << *M->getMemOperand() << ">"; 5900198892Srdivacky } else if (const BlockAddressSDNode *BA = 5901198892Srdivacky dyn_cast<BlockAddressSDNode>(this)) { 5902198892Srdivacky OS << "<"; 5903198892Srdivacky WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false); 5904198892Srdivacky OS << ", "; 5905198892Srdivacky WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false); 5906198892Srdivacky OS << ">"; 5907199989Srdivacky if (unsigned int TF = BA->getTargetFlags()) 5908199989Srdivacky OS << " [TF=" << TF << ']'; 5909193323Sed } 5910201360Srdivacky 5911201360Srdivacky if (G) 5912201360Srdivacky if (unsigned Order = G->GetOrdering(this)) 5913201360Srdivacky OS << " [ORD=" << Order << ']'; 5914204642Srdivacky 5915204642Srdivacky if (getNodeId() != -1) 5916204642Srdivacky OS << " [ID=" << getNodeId() << ']'; 5917193323Sed} 5918193323Sed 5919193323Sedvoid SDNode::print(raw_ostream &OS, const SelectionDAG *G) const { 5920193323Sed print_types(OS, G); 5921193323Sed for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 5922199481Srdivacky if (i) OS << ", "; else OS << " "; 5923193323Sed OS << (void*)getOperand(i).getNode(); 5924193323Sed if (unsigned RN = getOperand(i).getResNo()) 5925193323Sed OS << ":" << RN; 5926193323Sed } 5927193323Sed print_details(OS, G); 5928193323Sed} 5929193323Sed 5930202878Srdivackystatic void printrWithDepthHelper(raw_ostream &OS, const SDNode *N, 5931202878Srdivacky const SelectionDAG *G, unsigned depth, 5932202878Srdivacky unsigned indent) 5933202878Srdivacky{ 5934202878Srdivacky if (depth == 0) 5935202878Srdivacky return; 5936202878Srdivacky 5937202878Srdivacky OS.indent(indent); 5938202878Srdivacky 5939202878Srdivacky N->print(OS, G); 5940202878Srdivacky 5941202878Srdivacky if (depth < 1) 5942202878Srdivacky return; 5943202878Srdivacky 5944202878Srdivacky for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 5945202878Srdivacky OS << '\n'; 5946202878Srdivacky printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2); 5947202878Srdivacky } 5948202878Srdivacky} 5949202878Srdivacky 5950202878Srdivackyvoid SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G, 5951202878Srdivacky unsigned depth) const { 5952202878Srdivacky printrWithDepthHelper(OS, this, G, depth, 0); 5953202878Srdivacky} 5954202878Srdivacky 5955202878Srdivackyvoid SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const { 5956202878Srdivacky // Don't print impossibly deep things. 5957202878Srdivacky printrWithDepth(OS, G, 100); 5958202878Srdivacky} 5959202878Srdivacky 5960202878Srdivackyvoid SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const { 5961202878Srdivacky printrWithDepth(dbgs(), G, depth); 5962202878Srdivacky} 5963202878Srdivacky 5964202878Srdivackyvoid SDNode::dumprFull(const SelectionDAG *G) const { 5965202878Srdivacky // Don't print impossibly deep things. 5966202878Srdivacky dumprWithDepth(G, 100); 5967202878Srdivacky} 5968202878Srdivacky 5969193323Sedstatic void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 5970193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 5971193323Sed if (N->getOperand(i).getNode()->hasOneUse()) 5972193323Sed DumpNodes(N->getOperand(i).getNode(), indent+2, G); 5973193323Sed else 5974202375Srdivacky dbgs() << "\n" << std::string(indent+2, ' ') 5975202375Srdivacky << (void*)N->getOperand(i).getNode() << ": <multiple use>"; 5976193323Sed 5977193323Sed 5978202375Srdivacky dbgs() << "\n"; 5979202375Srdivacky dbgs().indent(indent); 5980193323Sed N->dump(G); 5981193323Sed} 5982193323Sed 5983199989SrdivackySDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) { 5984199989Srdivacky assert(N->getNumValues() == 1 && 5985199989Srdivacky "Can't unroll a vector with multiple results!"); 5986199989Srdivacky 5987199989Srdivacky EVT VT = N->getValueType(0); 5988199989Srdivacky unsigned NE = VT.getVectorNumElements(); 5989199989Srdivacky EVT EltVT = VT.getVectorElementType(); 5990199989Srdivacky DebugLoc dl = N->getDebugLoc(); 5991199989Srdivacky 5992199989Srdivacky SmallVector<SDValue, 8> Scalars; 5993199989Srdivacky SmallVector<SDValue, 4> Operands(N->getNumOperands()); 5994199989Srdivacky 5995199989Srdivacky // If ResNE is 0, fully unroll the vector op. 5996199989Srdivacky if (ResNE == 0) 5997199989Srdivacky ResNE = NE; 5998199989Srdivacky else if (NE > ResNE) 5999199989Srdivacky NE = ResNE; 6000199989Srdivacky 6001199989Srdivacky unsigned i; 6002199989Srdivacky for (i= 0; i != NE; ++i) { 6003199989Srdivacky for (unsigned j = 0; j != N->getNumOperands(); ++j) { 6004199989Srdivacky SDValue Operand = N->getOperand(j); 6005199989Srdivacky EVT OperandVT = Operand.getValueType(); 6006199989Srdivacky if (OperandVT.isVector()) { 6007199989Srdivacky // A vector operand; extract a single element. 6008199989Srdivacky EVT OperandEltVT = OperandVT.getVectorElementType(); 6009199989Srdivacky Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, 6010199989Srdivacky OperandEltVT, 6011199989Srdivacky Operand, 6012199989Srdivacky getConstant(i, MVT::i32)); 6013199989Srdivacky } else { 6014199989Srdivacky // A scalar operand; just use it as is. 6015199989Srdivacky Operands[j] = Operand; 6016199989Srdivacky } 6017199989Srdivacky } 6018199989Srdivacky 6019199989Srdivacky switch (N->getOpcode()) { 6020199989Srdivacky default: 6021199989Srdivacky Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, 6022199989Srdivacky &Operands[0], Operands.size())); 6023199989Srdivacky break; 6024199989Srdivacky case ISD::SHL: 6025199989Srdivacky case ISD::SRA: 6026199989Srdivacky case ISD::SRL: 6027199989Srdivacky case ISD::ROTL: 6028199989Srdivacky case ISD::ROTR: 6029199989Srdivacky Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0], 6030199989Srdivacky getShiftAmountOperand(Operands[1]))); 6031199989Srdivacky break; 6032202375Srdivacky case ISD::SIGN_EXTEND_INREG: 6033202375Srdivacky case ISD::FP_ROUND_INREG: { 6034202375Srdivacky EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType(); 6035202375Srdivacky Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, 6036202375Srdivacky Operands[0], 6037202375Srdivacky getValueType(ExtVT))); 6038199989Srdivacky } 6039202375Srdivacky } 6040199989Srdivacky } 6041199989Srdivacky 6042199989Srdivacky for (; i < ResNE; ++i) 6043199989Srdivacky Scalars.push_back(getUNDEF(EltVT)); 6044199989Srdivacky 6045199989Srdivacky return getNode(ISD::BUILD_VECTOR, dl, 6046199989Srdivacky EVT::getVectorVT(*getContext(), EltVT, ResNE), 6047199989Srdivacky &Scalars[0], Scalars.size()); 6048199989Srdivacky} 6049199989Srdivacky 6050200581Srdivacky 6051200581Srdivacky/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a 6052200581Srdivacky/// location that is 'Dist' units away from the location that the 'Base' load 6053200581Srdivacky/// is loading from. 6054200581Srdivackybool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base, 6055200581Srdivacky unsigned Bytes, int Dist) const { 6056200581Srdivacky if (LD->getChain() != Base->getChain()) 6057200581Srdivacky return false; 6058200581Srdivacky EVT VT = LD->getValueType(0); 6059200581Srdivacky if (VT.getSizeInBits() / 8 != Bytes) 6060200581Srdivacky return false; 6061200581Srdivacky 6062200581Srdivacky SDValue Loc = LD->getOperand(1); 6063200581Srdivacky SDValue BaseLoc = Base->getOperand(1); 6064200581Srdivacky if (Loc.getOpcode() == ISD::FrameIndex) { 6065200581Srdivacky if (BaseLoc.getOpcode() != ISD::FrameIndex) 6066200581Srdivacky return false; 6067200581Srdivacky const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo(); 6068200581Srdivacky int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); 6069200581Srdivacky int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 6070200581Srdivacky int FS = MFI->getObjectSize(FI); 6071200581Srdivacky int BFS = MFI->getObjectSize(BFI); 6072200581Srdivacky if (FS != BFS || FS != (int)Bytes) return false; 6073200581Srdivacky return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); 6074200581Srdivacky } 6075200581Srdivacky if (Loc.getOpcode() == ISD::ADD && Loc.getOperand(0) == BaseLoc) { 6076200581Srdivacky ConstantSDNode *V = dyn_cast<ConstantSDNode>(Loc.getOperand(1)); 6077200581Srdivacky if (V && (V->getSExtValue() == Dist*Bytes)) 6078200581Srdivacky return true; 6079200581Srdivacky } 6080200581Srdivacky 6081200581Srdivacky GlobalValue *GV1 = NULL; 6082200581Srdivacky GlobalValue *GV2 = NULL; 6083200581Srdivacky int64_t Offset1 = 0; 6084200581Srdivacky int64_t Offset2 = 0; 6085200581Srdivacky bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1); 6086200581Srdivacky bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); 6087200581Srdivacky if (isGA1 && isGA2 && GV1 == GV2) 6088200581Srdivacky return Offset1 == (Offset2 + Dist*Bytes); 6089200581Srdivacky return false; 6090200581Srdivacky} 6091200581Srdivacky 6092200581Srdivacky 6093200581Srdivacky/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if 6094200581Srdivacky/// it cannot be inferred. 6095200581Srdivackyunsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const { 6096200581Srdivacky // If this is a GlobalAddress + cst, return the alignment. 6097200581Srdivacky GlobalValue *GV; 6098200581Srdivacky int64_t GVOffset = 0; 6099200581Srdivacky if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) 6100200581Srdivacky return MinAlign(GV->getAlignment(), GVOffset); 6101200581Srdivacky 6102200581Srdivacky // If this is a direct reference to a stack slot, use information about the 6103200581Srdivacky // stack slot's alignment. 6104200581Srdivacky int FrameIdx = 1 << 31; 6105200581Srdivacky int64_t FrameOffset = 0; 6106200581Srdivacky if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) { 6107200581Srdivacky FrameIdx = FI->getIndex(); 6108200581Srdivacky } else if (Ptr.getOpcode() == ISD::ADD && 6109200581Srdivacky isa<ConstantSDNode>(Ptr.getOperand(1)) && 6110200581Srdivacky isa<FrameIndexSDNode>(Ptr.getOperand(0))) { 6111200581Srdivacky FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); 6112200581Srdivacky FrameOffset = Ptr.getConstantOperandVal(1); 6113200581Srdivacky } 6114200581Srdivacky 6115200581Srdivacky if (FrameIdx != (1 << 31)) { 6116200581Srdivacky // FIXME: Handle FI+CST. 6117200581Srdivacky const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo(); 6118200581Srdivacky unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx), 6119200581Srdivacky FrameOffset); 6120200581Srdivacky if (MFI.isFixedObjectIndex(FrameIdx)) { 6121200581Srdivacky int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset; 6122200581Srdivacky 6123200581Srdivacky // The alignment of the frame index can be determined from its offset from 6124200581Srdivacky // the incoming frame position. If the frame object is at offset 32 and 6125200581Srdivacky // the stack is guaranteed to be 16-byte aligned, then we know that the 6126200581Srdivacky // object is 16-byte aligned. 6127200581Srdivacky unsigned StackAlign = getTarget().getFrameInfo()->getStackAlignment(); 6128200581Srdivacky unsigned Align = MinAlign(ObjectOffset, StackAlign); 6129200581Srdivacky 6130200581Srdivacky // Finally, the frame object itself may have a known alignment. Factor 6131200581Srdivacky // the alignment + offset into a new alignment. For example, if we know 6132200581Srdivacky // the FI is 8 byte aligned, but the pointer is 4 off, we really have a 6133200581Srdivacky // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte 6134200581Srdivacky // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc. 6135200581Srdivacky return std::max(Align, FIInfoAlign); 6136200581Srdivacky } 6137200581Srdivacky return FIInfoAlign; 6138200581Srdivacky } 6139200581Srdivacky 6140200581Srdivacky return 0; 6141200581Srdivacky} 6142200581Srdivacky 6143193323Sedvoid SelectionDAG::dump() const { 6144202375Srdivacky dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:"; 6145193323Sed 6146193323Sed for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 6147193323Sed I != E; ++I) { 6148193323Sed const SDNode *N = I; 6149193323Sed if (!N->hasOneUse() && N != getRoot().getNode()) 6150193323Sed DumpNodes(N, 2, this); 6151193323Sed } 6152193323Sed 6153193323Sed if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this); 6154193323Sed 6155202375Srdivacky dbgs() << "\n\n"; 6156193323Sed} 6157193323Sed 6158193323Sedvoid SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const { 6159193323Sed print_types(OS, G); 6160193323Sed print_details(OS, G); 6161193323Sed} 6162193323Sed 6163193323Sedtypedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet; 6164193323Sedstatic void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent, 6165193323Sed const SelectionDAG *G, VisitedSDNodeSet &once) { 6166193323Sed if (!once.insert(N)) // If we've been here before, return now. 6167193323Sed return; 6168201360Srdivacky 6169193323Sed // Dump the current SDNode, but don't end the line yet. 6170193323Sed OS << std::string(indent, ' '); 6171193323Sed N->printr(OS, G); 6172201360Srdivacky 6173193323Sed // Having printed this SDNode, walk the children: 6174193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 6175193323Sed const SDNode *child = N->getOperand(i).getNode(); 6176201360Srdivacky 6177193323Sed if (i) OS << ","; 6178193323Sed OS << " "; 6179201360Srdivacky 6180193323Sed if (child->getNumOperands() == 0) { 6181193323Sed // This child has no grandchildren; print it inline right here. 6182193323Sed child->printr(OS, G); 6183193323Sed once.insert(child); 6184201360Srdivacky } else { // Just the address. FIXME: also print the child's opcode. 6185193323Sed OS << (void*)child; 6186193323Sed if (unsigned RN = N->getOperand(i).getResNo()) 6187193323Sed OS << ":" << RN; 6188193323Sed } 6189193323Sed } 6190201360Srdivacky 6191193323Sed OS << "\n"; 6192201360Srdivacky 6193193323Sed // Dump children that have grandchildren on their own line(s). 6194193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 6195193323Sed const SDNode *child = N->getOperand(i).getNode(); 6196193323Sed DumpNodesr(OS, child, indent+2, G, once); 6197193323Sed } 6198193323Sed} 6199193323Sed 6200193323Sedvoid SDNode::dumpr() const { 6201193323Sed VisitedSDNodeSet once; 6202202375Srdivacky DumpNodesr(dbgs(), this, 0, 0, once); 6203193323Sed} 6204193323Sed 6205198090Srdivackyvoid SDNode::dumpr(const SelectionDAG *G) const { 6206198090Srdivacky VisitedSDNodeSet once; 6207202375Srdivacky DumpNodesr(dbgs(), this, 0, G, once); 6208198090Srdivacky} 6209193323Sed 6210198090Srdivacky 6211193323Sed// getAddressSpace - Return the address space this GlobalAddress belongs to. 6212193323Sedunsigned GlobalAddressSDNode::getAddressSpace() const { 6213193323Sed return getGlobal()->getType()->getAddressSpace(); 6214193323Sed} 6215193323Sed 6216193323Sed 6217193323Sedconst Type *ConstantPoolSDNode::getType() const { 6218193323Sed if (isMachineConstantPoolEntry()) 6219193323Sed return Val.MachineCPVal->getType(); 6220193323Sed return Val.ConstVal->getType(); 6221193323Sed} 6222193323Sed 6223193323Sedbool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, 6224193323Sed APInt &SplatUndef, 6225193323Sed unsigned &SplatBitSize, 6226193323Sed bool &HasAnyUndefs, 6227199481Srdivacky unsigned MinSplatBits, 6228199481Srdivacky bool isBigEndian) { 6229198090Srdivacky EVT VT = getValueType(0); 6230193323Sed assert(VT.isVector() && "Expected a vector type"); 6231193323Sed unsigned sz = VT.getSizeInBits(); 6232193323Sed if (MinSplatBits > sz) 6233193323Sed return false; 6234193323Sed 6235193323Sed SplatValue = APInt(sz, 0); 6236193323Sed SplatUndef = APInt(sz, 0); 6237193323Sed 6238193323Sed // Get the bits. Bits with undefined values (when the corresponding element 6239193323Sed // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared 6240193323Sed // in SplatValue. If any of the values are not constant, give up and return 6241193323Sed // false. 6242193323Sed unsigned int nOps = getNumOperands(); 6243193323Sed assert(nOps > 0 && "isConstantSplat has 0-size build vector"); 6244193323Sed unsigned EltBitSize = VT.getVectorElementType().getSizeInBits(); 6245199481Srdivacky 6246199481Srdivacky for (unsigned j = 0; j < nOps; ++j) { 6247199481Srdivacky unsigned i = isBigEndian ? nOps-1-j : j; 6248193323Sed SDValue OpVal = getOperand(i); 6249199481Srdivacky unsigned BitPos = j * EltBitSize; 6250193323Sed 6251193323Sed if (OpVal.getOpcode() == ISD::UNDEF) 6252199481Srdivacky SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize); 6253193323Sed else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) 6254193323Sed SplatValue |= (APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize). 6255193323Sed zextOrTrunc(sz) << BitPos); 6256193323Sed else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) 6257193323Sed SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos; 6258193323Sed else 6259193323Sed return false; 6260193323Sed } 6261193323Sed 6262193323Sed // The build_vector is all constants or undefs. Find the smallest element 6263193323Sed // size that splats the vector. 6264193323Sed 6265193323Sed HasAnyUndefs = (SplatUndef != 0); 6266193323Sed while (sz > 8) { 6267193323Sed 6268193323Sed unsigned HalfSize = sz / 2; 6269193323Sed APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize); 6270193323Sed APInt LowValue = APInt(SplatValue).trunc(HalfSize); 6271193323Sed APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize); 6272193323Sed APInt LowUndef = APInt(SplatUndef).trunc(HalfSize); 6273193323Sed 6274193323Sed // If the two halves do not match (ignoring undef bits), stop here. 6275193323Sed if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) || 6276193323Sed MinSplatBits > HalfSize) 6277193323Sed break; 6278193323Sed 6279193323Sed SplatValue = HighValue | LowValue; 6280193323Sed SplatUndef = HighUndef & LowUndef; 6281198090Srdivacky 6282193323Sed sz = HalfSize; 6283193323Sed } 6284193323Sed 6285193323Sed SplatBitSize = sz; 6286193323Sed return true; 6287193323Sed} 6288193323Sed 6289198090Srdivackybool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) { 6290193323Sed // Find the first non-undef value in the shuffle mask. 6291193323Sed unsigned i, e; 6292193323Sed for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i) 6293193323Sed /* search */; 6294193323Sed 6295193323Sed assert(i != e && "VECTOR_SHUFFLE node with all undef indices!"); 6296198090Srdivacky 6297193323Sed // Make sure all remaining elements are either undef or the same as the first 6298193323Sed // non-undef value. 6299193323Sed for (int Idx = Mask[i]; i != e; ++i) 6300193323Sed if (Mask[i] >= 0 && Mask[i] != Idx) 6301193323Sed return false; 6302193323Sed return true; 6303193323Sed} 6304202878Srdivacky 6305204642Srdivacky#ifdef XDEBUG 6306202878Srdivackystatic void checkForCyclesHelper(const SDNode *N, 6307204642Srdivacky SmallPtrSet<const SDNode*, 32> &Visited, 6308204642Srdivacky SmallPtrSet<const SDNode*, 32> &Checked) { 6309204642Srdivacky // If this node has already been checked, don't check it again. 6310204642Srdivacky if (Checked.count(N)) 6311204642Srdivacky return; 6312204642Srdivacky 6313204642Srdivacky // If a node has already been visited on this depth-first walk, reject it as 6314204642Srdivacky // a cycle. 6315204642Srdivacky if (!Visited.insert(N)) { 6316202878Srdivacky dbgs() << "Offending node:\n"; 6317202878Srdivacky N->dumprFull(); 6318204642Srdivacky errs() << "Detected cycle in SelectionDAG\n"; 6319204642Srdivacky abort(); 6320202878Srdivacky } 6321204642Srdivacky 6322204642Srdivacky for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 6323204642Srdivacky checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked); 6324204642Srdivacky 6325204642Srdivacky Checked.insert(N); 6326204642Srdivacky Visited.erase(N); 6327202878Srdivacky} 6328204642Srdivacky#endif 6329202878Srdivacky 6330202878Srdivackyvoid llvm::checkForCycles(const llvm::SDNode *N) { 6331202878Srdivacky#ifdef XDEBUG 6332202878Srdivacky assert(N && "Checking nonexistant SDNode"); 6333204642Srdivacky SmallPtrSet<const SDNode*, 32> visited; 6334204642Srdivacky SmallPtrSet<const SDNode*, 32> checked; 6335204642Srdivacky checkForCyclesHelper(N, visited, checked); 6336202878Srdivacky#endif 6337202878Srdivacky} 6338202878Srdivacky 6339202878Srdivackyvoid llvm::checkForCycles(const llvm::SelectionDAG *DAG) { 6340202878Srdivacky checkForCycles(DAG->getRoot().getNode()); 6341202878Srdivacky} 6342