SelectionDAG.cpp revision 218893
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" 16205218Srdivacky#include "SDNodeDbgValue.h" 17193323Sed#include "llvm/Constants.h" 18208599Srdivacky#include "llvm/Analysis/DebugInfo.h" 19193323Sed#include "llvm/Analysis/ValueTracking.h" 20198090Srdivacky#include "llvm/Function.h" 21193323Sed#include "llvm/GlobalAlias.h" 22193323Sed#include "llvm/GlobalVariable.h" 23193323Sed#include "llvm/Intrinsics.h" 24193323Sed#include "llvm/DerivedTypes.h" 25193323Sed#include "llvm/Assembly/Writer.h" 26193323Sed#include "llvm/CallingConv.h" 27193323Sed#include "llvm/CodeGen/MachineBasicBlock.h" 28193323Sed#include "llvm/CodeGen/MachineConstantPool.h" 29193323Sed#include "llvm/CodeGen/MachineFrameInfo.h" 30193323Sed#include "llvm/CodeGen/MachineModuleInfo.h" 31193323Sed#include "llvm/CodeGen/PseudoSourceValue.h" 32193323Sed#include "llvm/Target/TargetRegisterInfo.h" 33193323Sed#include "llvm/Target/TargetData.h" 34193323Sed#include "llvm/Target/TargetLowering.h" 35208599Srdivacky#include "llvm/Target/TargetSelectionDAGInfo.h" 36193323Sed#include "llvm/Target/TargetOptions.h" 37193323Sed#include "llvm/Target/TargetInstrInfo.h" 38198396Srdivacky#include "llvm/Target/TargetIntrinsicInfo.h" 39193323Sed#include "llvm/Target/TargetMachine.h" 40193323Sed#include "llvm/Support/CommandLine.h" 41202375Srdivacky#include "llvm/Support/Debug.h" 42198090Srdivacky#include "llvm/Support/ErrorHandling.h" 43195098Sed#include "llvm/Support/ManagedStatic.h" 44193323Sed#include "llvm/Support/MathExtras.h" 45193323Sed#include "llvm/Support/raw_ostream.h" 46218893Sdim#include "llvm/Support/Mutex.h" 47193323Sed#include "llvm/ADT/SetVector.h" 48193323Sed#include "llvm/ADT/SmallPtrSet.h" 49193323Sed#include "llvm/ADT/SmallSet.h" 50193323Sed#include "llvm/ADT/SmallVector.h" 51193323Sed#include "llvm/ADT/StringExtras.h" 52193323Sed#include <algorithm> 53193323Sed#include <cmath> 54193323Sedusing namespace llvm; 55193323Sed 56193323Sed/// makeVTList - Return an instance of the SDVTList struct initialized with the 57193323Sed/// specified members. 58198090Srdivackystatic SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) { 59193323Sed SDVTList Res = {VTs, NumVTs}; 60193323Sed return Res; 61193323Sed} 62193323Sed 63198090Srdivackystatic const fltSemantics *EVTToAPFloatSemantics(EVT VT) { 64198090Srdivacky switch (VT.getSimpleVT().SimpleTy) { 65198090Srdivacky default: llvm_unreachable("Unknown FP format"); 66193323Sed case MVT::f32: return &APFloat::IEEEsingle; 67193323Sed case MVT::f64: return &APFloat::IEEEdouble; 68193323Sed case MVT::f80: return &APFloat::x87DoubleExtended; 69193323Sed case MVT::f128: return &APFloat::IEEEquad; 70193323Sed case MVT::ppcf128: return &APFloat::PPCDoubleDouble; 71193323Sed } 72193323Sed} 73193323Sed 74193323SedSelectionDAG::DAGUpdateListener::~DAGUpdateListener() {} 75193323Sed 76193323Sed//===----------------------------------------------------------------------===// 77193323Sed// ConstantFPSDNode Class 78193323Sed//===----------------------------------------------------------------------===// 79193323Sed 80193323Sed/// isExactlyValue - We don't rely on operator== working on double values, as 81193323Sed/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 82193323Sed/// As such, this method can be used to do an exact bit-for-bit comparison of 83193323Sed/// two floating point values. 84193323Sedbool ConstantFPSDNode::isExactlyValue(const APFloat& V) const { 85193323Sed return getValueAPF().bitwiseIsEqual(V); 86193323Sed} 87193323Sed 88198090Srdivackybool ConstantFPSDNode::isValueValidForType(EVT VT, 89193323Sed const APFloat& Val) { 90193323Sed assert(VT.isFloatingPoint() && "Can only convert between FP types"); 91193323Sed 92193323Sed // PPC long double cannot be converted to any other type. 93193323Sed if (VT == MVT::ppcf128 || 94193323Sed &Val.getSemantics() == &APFloat::PPCDoubleDouble) 95193323Sed return false; 96193323Sed 97193323Sed // convert modifies in place, so make a copy. 98193323Sed APFloat Val2 = APFloat(Val); 99193323Sed bool losesInfo; 100198090Srdivacky (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven, 101193323Sed &losesInfo); 102193323Sed return !losesInfo; 103193323Sed} 104193323Sed 105193323Sed//===----------------------------------------------------------------------===// 106193323Sed// ISD Namespace 107193323Sed//===----------------------------------------------------------------------===// 108193323Sed 109193323Sed/// isBuildVectorAllOnes - Return true if the specified node is a 110193323Sed/// BUILD_VECTOR where all of the elements are ~0 or undef. 111193323Sedbool ISD::isBuildVectorAllOnes(const SDNode *N) { 112193323Sed // Look through a bit convert. 113218893Sdim if (N->getOpcode() == ISD::BITCAST) 114193323Sed N = N->getOperand(0).getNode(); 115193323Sed 116193323Sed if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 117193323Sed 118193323Sed unsigned i = 0, e = N->getNumOperands(); 119193323Sed 120193323Sed // Skip over all of the undef values. 121193323Sed while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 122193323Sed ++i; 123193323Sed 124193323Sed // Do not accept an all-undef vector. 125193323Sed if (i == e) return false; 126193323Sed 127193323Sed // Do not accept build_vectors that aren't all constants or which have non-~0 128193323Sed // elements. 129193323Sed SDValue NotZero = N->getOperand(i); 130193323Sed if (isa<ConstantSDNode>(NotZero)) { 131193323Sed if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue()) 132193323Sed return false; 133193323Sed } else if (isa<ConstantFPSDNode>(NotZero)) { 134193323Sed if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF(). 135193323Sed bitcastToAPInt().isAllOnesValue()) 136193323Sed return false; 137193323Sed } else 138193323Sed return false; 139193323Sed 140193323Sed // Okay, we have at least one ~0 value, check to see if the rest match or are 141193323Sed // undefs. 142193323Sed for (++i; i != e; ++i) 143193323Sed if (N->getOperand(i) != NotZero && 144193323Sed N->getOperand(i).getOpcode() != ISD::UNDEF) 145193323Sed return false; 146193323Sed return true; 147193323Sed} 148193323Sed 149193323Sed 150193323Sed/// isBuildVectorAllZeros - Return true if the specified node is a 151193323Sed/// BUILD_VECTOR where all of the elements are 0 or undef. 152193323Sedbool ISD::isBuildVectorAllZeros(const SDNode *N) { 153193323Sed // Look through a bit convert. 154218893Sdim if (N->getOpcode() == ISD::BITCAST) 155193323Sed N = N->getOperand(0).getNode(); 156193323Sed 157193323Sed if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 158193323Sed 159193323Sed unsigned i = 0, e = N->getNumOperands(); 160193323Sed 161193323Sed // Skip over all of the undef values. 162193323Sed while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 163193323Sed ++i; 164193323Sed 165193323Sed // Do not accept an all-undef vector. 166193323Sed if (i == e) return false; 167193323Sed 168193574Sed // Do not accept build_vectors that aren't all constants or which have non-0 169193323Sed // elements. 170193323Sed SDValue Zero = N->getOperand(i); 171193323Sed if (isa<ConstantSDNode>(Zero)) { 172193323Sed if (!cast<ConstantSDNode>(Zero)->isNullValue()) 173193323Sed return false; 174193323Sed } else if (isa<ConstantFPSDNode>(Zero)) { 175193323Sed if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero()) 176193323Sed return false; 177193323Sed } else 178193323Sed return false; 179193323Sed 180193574Sed // Okay, we have at least one 0 value, check to see if the rest match or are 181193323Sed // undefs. 182193323Sed for (++i; i != e; ++i) 183193323Sed if (N->getOperand(i) != Zero && 184193323Sed N->getOperand(i).getOpcode() != ISD::UNDEF) 185193323Sed return false; 186193323Sed return true; 187193323Sed} 188193323Sed 189193323Sed/// isScalarToVector - Return true if the specified node is a 190193323Sed/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low 191193323Sed/// element is not an undef. 192193323Sedbool ISD::isScalarToVector(const SDNode *N) { 193193323Sed if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) 194193323Sed return true; 195193323Sed 196193323Sed if (N->getOpcode() != ISD::BUILD_VECTOR) 197193323Sed return false; 198193323Sed if (N->getOperand(0).getOpcode() == ISD::UNDEF) 199193323Sed return false; 200193323Sed unsigned NumElems = N->getNumOperands(); 201218893Sdim if (NumElems == 1) 202218893Sdim return false; 203193323Sed for (unsigned i = 1; i < NumElems; ++i) { 204193323Sed SDValue V = N->getOperand(i); 205193323Sed if (V.getOpcode() != ISD::UNDEF) 206193323Sed return false; 207193323Sed } 208193323Sed return true; 209193323Sed} 210193323Sed 211193323Sed/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 212193323Sed/// when given the operation for (X op Y). 213193323SedISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 214193323Sed // To perform this operation, we just need to swap the L and G bits of the 215193323Sed // operation. 216193323Sed unsigned OldL = (Operation >> 2) & 1; 217193323Sed unsigned OldG = (Operation >> 1) & 1; 218193323Sed return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 219193323Sed (OldL << 1) | // New G bit 220193323Sed (OldG << 2)); // New L bit. 221193323Sed} 222193323Sed 223193323Sed/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 224193323Sed/// 'op' is a valid SetCC operation. 225193323SedISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 226193323Sed unsigned Operation = Op; 227193323Sed if (isInteger) 228193323Sed Operation ^= 7; // Flip L, G, E bits, but not U. 229193323Sed else 230193323Sed Operation ^= 15; // Flip all of the condition bits. 231193323Sed 232193323Sed if (Operation > ISD::SETTRUE2) 233193323Sed Operation &= ~8; // Don't let N and U bits get set. 234193323Sed 235193323Sed return ISD::CondCode(Operation); 236193323Sed} 237193323Sed 238193323Sed 239193323Sed/// isSignedOp - For an integer comparison, return 1 if the comparison is a 240193323Sed/// signed operation and 2 if the result is an unsigned comparison. Return zero 241193323Sed/// if the operation does not depend on the sign of the input (setne and seteq). 242193323Sedstatic int isSignedOp(ISD::CondCode Opcode) { 243193323Sed switch (Opcode) { 244198090Srdivacky default: llvm_unreachable("Illegal integer setcc operation!"); 245193323Sed case ISD::SETEQ: 246193323Sed case ISD::SETNE: return 0; 247193323Sed case ISD::SETLT: 248193323Sed case ISD::SETLE: 249193323Sed case ISD::SETGT: 250193323Sed case ISD::SETGE: return 1; 251193323Sed case ISD::SETULT: 252193323Sed case ISD::SETULE: 253193323Sed case ISD::SETUGT: 254193323Sed case ISD::SETUGE: return 2; 255193323Sed } 256193323Sed} 257193323Sed 258193323Sed/// getSetCCOrOperation - Return the result of a logical OR between different 259193323Sed/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 260193323Sed/// returns SETCC_INVALID if it is not possible to represent the resultant 261193323Sed/// comparison. 262193323SedISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 263193323Sed bool isInteger) { 264193323Sed if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 265193323Sed // Cannot fold a signed integer setcc with an unsigned integer setcc. 266193323Sed return ISD::SETCC_INVALID; 267193323Sed 268193323Sed unsigned Op = Op1 | Op2; // Combine all of the condition bits. 269193323Sed 270193323Sed // If the N and U bits get set then the resultant comparison DOES suddenly 271193323Sed // care about orderedness, and is true when ordered. 272193323Sed if (Op > ISD::SETTRUE2) 273193323Sed Op &= ~16; // Clear the U bit if the N bit is set. 274193323Sed 275193323Sed // Canonicalize illegal integer setcc's. 276193323Sed if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT 277193323Sed Op = ISD::SETNE; 278193323Sed 279193323Sed return ISD::CondCode(Op); 280193323Sed} 281193323Sed 282193323Sed/// getSetCCAndOperation - Return the result of a logical AND between different 283193323Sed/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 284193323Sed/// function returns zero if it is not possible to represent the resultant 285193323Sed/// comparison. 286193323SedISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 287193323Sed bool isInteger) { 288193323Sed if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 289193323Sed // Cannot fold a signed setcc with an unsigned setcc. 290193323Sed return ISD::SETCC_INVALID; 291193323Sed 292193323Sed // Combine all of the condition bits. 293193323Sed ISD::CondCode Result = ISD::CondCode(Op1 & Op2); 294193323Sed 295193323Sed // Canonicalize illegal integer setcc's. 296193323Sed if (isInteger) { 297193323Sed switch (Result) { 298193323Sed default: break; 299193323Sed case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT 300193323Sed case ISD::SETOEQ: // SETEQ & SETU[LG]E 301193323Sed case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE 302193323Sed case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE 303193323Sed case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE 304193323Sed } 305193323Sed } 306193323Sed 307193323Sed return Result; 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) { 493218893Sdim if (N->getValueType(0) == MVT::Glue) 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) 505218893Sdim if (N->getValueType(i) == MVT::Glue) 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); 599205218Srdivacky 600206083Srdivacky // If any of the SDDbgValue nodes refer to this SDNode, invalidate them. 601206083Srdivacky SmallVector<SDDbgValue*, 2> &DbgVals = DbgInfo->getSDDbgValues(N); 602206083Srdivacky for (unsigned i = 0, e = DbgVals.size(); i != e; ++i) 603206083Srdivacky DbgVals[i]->setIsInvalidated(); 604193323Sed} 605193323Sed 606193323Sed/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 607193323Sed/// correspond to it. This is useful when we're about to delete or repurpose 608193323Sed/// the node. We don't want future request for structurally identical nodes 609193323Sed/// to return N anymore. 610193323Sedbool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 611193323Sed bool Erased = false; 612193323Sed switch (N->getOpcode()) { 613193323Sed case ISD::HANDLENODE: return false; // noop. 614193323Sed case ISD::CONDCODE: 615193323Sed assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 616193323Sed "Cond code doesn't exist!"); 617193323Sed Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 618193323Sed CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 619193323Sed break; 620193323Sed case ISD::ExternalSymbol: 621193323Sed Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 622193323Sed break; 623195098Sed case ISD::TargetExternalSymbol: { 624195098Sed ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N); 625195098Sed Erased = TargetExternalSymbols.erase( 626195098Sed std::pair<std::string,unsigned char>(ESN->getSymbol(), 627195098Sed ESN->getTargetFlags())); 628193323Sed break; 629195098Sed } 630193323Sed case ISD::VALUETYPE: { 631198090Srdivacky EVT VT = cast<VTSDNode>(N)->getVT(); 632193323Sed if (VT.isExtended()) { 633193323Sed Erased = ExtendedValueTypeNodes.erase(VT); 634193323Sed } else { 635198090Srdivacky Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0; 636198090Srdivacky ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0; 637193323Sed } 638193323Sed break; 639193323Sed } 640193323Sed default: 641193323Sed // Remove it from the CSE Map. 642218893Sdim assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!"); 643218893Sdim assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!"); 644193323Sed Erased = CSEMap.RemoveNode(N); 645193323Sed break; 646193323Sed } 647193323Sed#ifndef NDEBUG 648193323Sed // Verify that the node was actually in one of the CSE maps, unless it has a 649193323Sed // flag result (which cannot be CSE'd) or is one of the special cases that are 650193323Sed // not subject to CSE. 651218893Sdim if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue && 652193323Sed !N->isMachineOpcode() && !doNotCSE(N)) { 653193323Sed N->dump(this); 654202375Srdivacky dbgs() << "\n"; 655198090Srdivacky llvm_unreachable("Node is not in map!"); 656193323Sed } 657193323Sed#endif 658193323Sed return Erased; 659193323Sed} 660193323Sed 661193323Sed/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE 662193323Sed/// maps and modified in place. Add it back to the CSE maps, unless an identical 663193323Sed/// node already exists, in which case transfer all its users to the existing 664193323Sed/// node. This transfer can potentially trigger recursive merging. 665193323Sed/// 666193323Sedvoid 667193323SedSelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N, 668193323Sed DAGUpdateListener *UpdateListener) { 669193323Sed // For node types that aren't CSE'd, just act as if no identical node 670193323Sed // already exists. 671193323Sed if (!doNotCSE(N)) { 672193323Sed SDNode *Existing = CSEMap.GetOrInsertNode(N); 673193323Sed if (Existing != N) { 674193323Sed // If there was already an existing matching node, use ReplaceAllUsesWith 675193323Sed // to replace the dead one with the existing one. This can cause 676193323Sed // recursive merging of other unrelated nodes down the line. 677193323Sed ReplaceAllUsesWith(N, Existing, UpdateListener); 678193323Sed 679193323Sed // N is now dead. Inform the listener if it exists and delete it. 680193323Sed if (UpdateListener) 681193323Sed UpdateListener->NodeDeleted(N, Existing); 682193323Sed DeleteNodeNotInCSEMaps(N); 683193323Sed return; 684193323Sed } 685193323Sed } 686193323Sed 687193323Sed // If the node doesn't already exist, we updated it. Inform a listener if 688193323Sed // it exists. 689193323Sed if (UpdateListener) 690193323Sed UpdateListener->NodeUpdated(N); 691193323Sed} 692193323Sed 693193323Sed/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 694193323Sed/// were replaced with those specified. If this node is never memoized, 695193323Sed/// return null, otherwise return a pointer to the slot it would take. If a 696193323Sed/// node already exists with these operands, the slot will be non-null. 697193323SedSDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op, 698193323Sed void *&InsertPos) { 699193323Sed if (doNotCSE(N)) 700193323Sed return 0; 701193323Sed 702193323Sed SDValue Ops[] = { Op }; 703193323Sed FoldingSetNodeID ID; 704193323Sed AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1); 705193323Sed AddNodeIDCustom(ID, N); 706200581Srdivacky SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 707200581Srdivacky return Node; 708193323Sed} 709193323Sed 710193323Sed/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 711193323Sed/// were replaced with those specified. If this node is never memoized, 712193323Sed/// return null, otherwise return a pointer to the slot it would take. If a 713193323Sed/// node already exists with these operands, the slot will be non-null. 714193323SedSDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 715193323Sed SDValue Op1, SDValue Op2, 716193323Sed void *&InsertPos) { 717193323Sed if (doNotCSE(N)) 718193323Sed return 0; 719193323Sed 720193323Sed SDValue Ops[] = { Op1, Op2 }; 721193323Sed FoldingSetNodeID ID; 722193323Sed AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2); 723193323Sed AddNodeIDCustom(ID, N); 724200581Srdivacky SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 725200581Srdivacky return Node; 726193323Sed} 727193323Sed 728193323Sed 729193323Sed/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 730193323Sed/// were replaced with those specified. If this node is never memoized, 731193323Sed/// return null, otherwise return a pointer to the slot it would take. If a 732193323Sed/// node already exists with these operands, the slot will be non-null. 733193323SedSDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 734193323Sed const SDValue *Ops,unsigned NumOps, 735193323Sed void *&InsertPos) { 736193323Sed if (doNotCSE(N)) 737193323Sed return 0; 738193323Sed 739193323Sed FoldingSetNodeID ID; 740193323Sed AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps); 741193323Sed AddNodeIDCustom(ID, N); 742200581Srdivacky SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 743200581Srdivacky return Node; 744193323Sed} 745193323Sed 746218893Sdim#ifndef NDEBUG 747218893Sdim/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid. 748218893Sdimstatic void VerifyNodeCommon(SDNode *N) { 749193323Sed switch (N->getOpcode()) { 750193323Sed default: 751193323Sed break; 752193323Sed case ISD::BUILD_PAIR: { 753198090Srdivacky EVT VT = N->getValueType(0); 754193323Sed assert(N->getNumValues() == 1 && "Too many results!"); 755193323Sed assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) && 756193323Sed "Wrong return type!"); 757193323Sed assert(N->getNumOperands() == 2 && "Wrong number of operands!"); 758193323Sed assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() && 759193323Sed "Mismatched operand types!"); 760193323Sed assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() && 761193323Sed "Wrong operand type!"); 762193323Sed assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() && 763193323Sed "Wrong return type size"); 764193323Sed break; 765193323Sed } 766193323Sed case ISD::BUILD_VECTOR: { 767193323Sed assert(N->getNumValues() == 1 && "Too many results!"); 768193323Sed assert(N->getValueType(0).isVector() && "Wrong return type!"); 769193323Sed assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() && 770193323Sed "Wrong number of operands!"); 771198090Srdivacky EVT EltVT = N->getValueType(0).getVectorElementType(); 772193323Sed for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 773193323Sed assert((I->getValueType() == EltVT || 774193323Sed (EltVT.isInteger() && I->getValueType().isInteger() && 775193323Sed EltVT.bitsLE(I->getValueType()))) && 776193323Sed "Wrong operand type!"); 777193323Sed break; 778193323Sed } 779193323Sed } 780193323Sed} 781193323Sed 782218893Sdim/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid. 783218893Sdimstatic void VerifySDNode(SDNode *N) { 784218893Sdim // The SDNode allocators cannot be used to allocate nodes with fields that are 785218893Sdim // not present in an SDNode! 786218893Sdim assert(!isa<MemSDNode>(N) && "Bad MemSDNode!"); 787218893Sdim assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!"); 788218893Sdim assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!"); 789218893Sdim assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!"); 790218893Sdim assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!"); 791218893Sdim assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!"); 792218893Sdim assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!"); 793218893Sdim assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!"); 794218893Sdim assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!"); 795218893Sdim assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!"); 796218893Sdim assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!"); 797218893Sdim assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!"); 798218893Sdim assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!"); 799218893Sdim assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!"); 800218893Sdim assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!"); 801218893Sdim assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!"); 802218893Sdim assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!"); 803218893Sdim assert(!isa<VTSDNode>(N) && "Bad VTSDNode!"); 804218893Sdim assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!"); 805218893Sdim 806218893Sdim VerifyNodeCommon(N); 807218893Sdim} 808218893Sdim 809218893Sdim/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is 810218893Sdim/// invalid. 811218893Sdimstatic void VerifyMachineNode(SDNode *N) { 812218893Sdim // The MachineNode allocators cannot be used to allocate nodes with fields 813218893Sdim // that are not present in a MachineNode! 814218893Sdim // Currently there are no such nodes. 815218893Sdim 816218893Sdim VerifyNodeCommon(N); 817218893Sdim} 818218893Sdim#endif // NDEBUG 819218893Sdim 820198090Srdivacky/// getEVTAlignment - Compute the default alignment value for the 821193323Sed/// given type. 822193323Sed/// 823198090Srdivackyunsigned SelectionDAG::getEVTAlignment(EVT VT) const { 824193323Sed const Type *Ty = VT == MVT::iPTR ? 825198090Srdivacky PointerType::get(Type::getInt8Ty(*getContext()), 0) : 826198090Srdivacky VT.getTypeForEVT(*getContext()); 827193323Sed 828193323Sed return TLI.getTargetData()->getABITypeAlignment(Ty); 829193323Sed} 830193323Sed 831193323Sed// EntryNode could meaningfully have debug info if we can find it... 832210299SedSelectionDAG::SelectionDAG(const TargetMachine &tm) 833208599Srdivacky : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()), 834206124Srdivacky EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)), 835200581Srdivacky Root(getEntryNode()), Ordering(0) { 836193323Sed AllNodes.push_back(&EntryNode); 837202878Srdivacky Ordering = new SDNodeOrdering(); 838205218Srdivacky DbgInfo = new SDDbgInfo(); 839193323Sed} 840193323Sed 841206274Srdivackyvoid SelectionDAG::init(MachineFunction &mf) { 842193323Sed MF = &mf; 843198090Srdivacky Context = &mf.getFunction()->getContext(); 844193323Sed} 845193323Sed 846193323SedSelectionDAG::~SelectionDAG() { 847193323Sed allnodes_clear(); 848200581Srdivacky delete Ordering; 849205218Srdivacky delete DbgInfo; 850193323Sed} 851193323Sed 852193323Sedvoid SelectionDAG::allnodes_clear() { 853193323Sed assert(&*AllNodes.begin() == &EntryNode); 854193323Sed AllNodes.remove(AllNodes.begin()); 855193323Sed while (!AllNodes.empty()) 856193323Sed DeallocateNode(AllNodes.begin()); 857193323Sed} 858193323Sed 859193323Sedvoid SelectionDAG::clear() { 860193323Sed allnodes_clear(); 861193323Sed OperandAllocator.Reset(); 862193323Sed CSEMap.clear(); 863193323Sed 864193323Sed ExtendedValueTypeNodes.clear(); 865193323Sed ExternalSymbols.clear(); 866193323Sed TargetExternalSymbols.clear(); 867193323Sed std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), 868193323Sed static_cast<CondCodeSDNode*>(0)); 869193323Sed std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), 870193323Sed static_cast<SDNode*>(0)); 871193323Sed 872193323Sed EntryNode.UseList = 0; 873193323Sed AllNodes.push_back(&EntryNode); 874193323Sed Root = getEntryNode(); 875210299Sed Ordering->clear(); 876206083Srdivacky DbgInfo->clear(); 877193323Sed} 878193323Sed 879198090SrdivackySDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) { 880198090Srdivacky return VT.bitsGT(Op.getValueType()) ? 881198090Srdivacky getNode(ISD::SIGN_EXTEND, DL, VT, Op) : 882198090Srdivacky getNode(ISD::TRUNCATE, DL, VT, Op); 883198090Srdivacky} 884198090Srdivacky 885198090SrdivackySDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) { 886198090Srdivacky return VT.bitsGT(Op.getValueType()) ? 887198090Srdivacky getNode(ISD::ZERO_EXTEND, DL, VT, Op) : 888198090Srdivacky getNode(ISD::TRUNCATE, DL, VT, Op); 889198090Srdivacky} 890198090Srdivacky 891198090SrdivackySDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) { 892200581Srdivacky assert(!VT.isVector() && 893200581Srdivacky "getZeroExtendInReg should use the vector element type instead of " 894200581Srdivacky "the vector type!"); 895193323Sed if (Op.getValueType() == VT) return Op; 896200581Srdivacky unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 897200581Srdivacky APInt Imm = APInt::getLowBitsSet(BitWidth, 898193323Sed VT.getSizeInBits()); 899193323Sed return getNode(ISD::AND, DL, Op.getValueType(), Op, 900193323Sed getConstant(Imm, Op.getValueType())); 901193323Sed} 902193323Sed 903193323Sed/// getNOT - Create a bitwise NOT operation as (XOR Val, -1). 904193323Sed/// 905198090SrdivackySDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) { 906204642Srdivacky EVT EltVT = VT.getScalarType(); 907193323Sed SDValue NegOne = 908193323Sed getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); 909193323Sed return getNode(ISD::XOR, DL, VT, Val, NegOne); 910193323Sed} 911193323Sed 912198090SrdivackySDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) { 913204642Srdivacky EVT EltVT = VT.getScalarType(); 914193323Sed assert((EltVT.getSizeInBits() >= 64 || 915193323Sed (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && 916193323Sed "getConstant with a uint64_t value that doesn't fit in the type!"); 917193323Sed return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT); 918193323Sed} 919193323Sed 920198090SrdivackySDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) { 921198090Srdivacky return getConstant(*ConstantInt::get(*Context, Val), VT, isT); 922193323Sed} 923193323Sed 924198090SrdivackySDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) { 925193323Sed assert(VT.isInteger() && "Cannot create FP integer constant!"); 926193323Sed 927204642Srdivacky EVT EltVT = VT.getScalarType(); 928193323Sed assert(Val.getBitWidth() == EltVT.getSizeInBits() && 929193323Sed "APInt size does not match type size!"); 930193323Sed 931193323Sed unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; 932193323Sed FoldingSetNodeID ID; 933193323Sed AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 934193323Sed ID.AddPointer(&Val); 935193323Sed void *IP = 0; 936193323Sed SDNode *N = NULL; 937201360Srdivacky if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 938193323Sed if (!VT.isVector()) 939193323Sed return SDValue(N, 0); 940201360Srdivacky 941193323Sed if (!N) { 942205407Srdivacky N = new (NodeAllocator) ConstantSDNode(isT, &Val, EltVT); 943193323Sed CSEMap.InsertNode(N, IP); 944193323Sed AllNodes.push_back(N); 945193323Sed } 946193323Sed 947193323Sed SDValue Result(N, 0); 948193323Sed if (VT.isVector()) { 949193323Sed SmallVector<SDValue, 8> Ops; 950193323Sed Ops.assign(VT.getVectorNumElements(), Result); 951206124Srdivacky Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size()); 952193323Sed } 953193323Sed return Result; 954193323Sed} 955193323Sed 956193323SedSDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) { 957193323Sed return getConstant(Val, TLI.getPointerTy(), isTarget); 958193323Sed} 959193323Sed 960193323Sed 961198090SrdivackySDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) { 962198090Srdivacky return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget); 963193323Sed} 964193323Sed 965198090SrdivackySDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){ 966193323Sed assert(VT.isFloatingPoint() && "Cannot create integer FP constant!"); 967193323Sed 968204642Srdivacky EVT EltVT = VT.getScalarType(); 969193323Sed 970193323Sed // Do the map lookup using the actual bit pattern for the floating point 971193323Sed // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 972193323Sed // we don't have issues with SNANs. 973193323Sed unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; 974193323Sed FoldingSetNodeID ID; 975193323Sed AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 976193323Sed ID.AddPointer(&V); 977193323Sed void *IP = 0; 978193323Sed SDNode *N = NULL; 979201360Srdivacky if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 980193323Sed if (!VT.isVector()) 981193323Sed return SDValue(N, 0); 982201360Srdivacky 983193323Sed if (!N) { 984205407Srdivacky N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT); 985193323Sed CSEMap.InsertNode(N, IP); 986193323Sed AllNodes.push_back(N); 987193323Sed } 988193323Sed 989193323Sed SDValue Result(N, 0); 990193323Sed if (VT.isVector()) { 991193323Sed SmallVector<SDValue, 8> Ops; 992193323Sed Ops.assign(VT.getVectorNumElements(), Result); 993193323Sed // FIXME DebugLoc info might be appropriate here 994206124Srdivacky Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size()); 995193323Sed } 996193323Sed return Result; 997193323Sed} 998193323Sed 999198090SrdivackySDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) { 1000204642Srdivacky EVT EltVT = VT.getScalarType(); 1001193323Sed if (EltVT==MVT::f32) 1002193323Sed return getConstantFP(APFloat((float)Val), VT, isTarget); 1003208599Srdivacky else if (EltVT==MVT::f64) 1004193323Sed return getConstantFP(APFloat(Val), VT, isTarget); 1005208599Srdivacky else if (EltVT==MVT::f80 || EltVT==MVT::f128) { 1006208599Srdivacky bool ignored; 1007208599Srdivacky APFloat apf = APFloat(Val); 1008208599Srdivacky apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven, 1009208599Srdivacky &ignored); 1010208599Srdivacky return getConstantFP(apf, VT, isTarget); 1011208599Srdivacky } else { 1012208599Srdivacky assert(0 && "Unsupported type in getConstantFP"); 1013208599Srdivacky return SDValue(); 1014208599Srdivacky } 1015193323Sed} 1016193323Sed 1017210299SedSDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL, 1018198090Srdivacky EVT VT, int64_t Offset, 1019195098Sed bool isTargetGA, 1020195098Sed unsigned char TargetFlags) { 1021195098Sed assert((TargetFlags == 0 || isTargetGA) && 1022195098Sed "Cannot set target flags on target-independent globals"); 1023198090Srdivacky 1024193323Sed // Truncate (with sign-extension) the offset value to the pointer size. 1025198090Srdivacky EVT PTy = TLI.getPointerTy(); 1026198090Srdivacky unsigned BitWidth = PTy.getSizeInBits(); 1027193323Sed if (BitWidth < 64) 1028193323Sed Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth)); 1029193323Sed 1030193323Sed const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 1031193323Sed if (!GVar) { 1032193323Sed // If GV is an alias then use the aliasee for determining thread-localness. 1033193323Sed if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 1034193323Sed GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)); 1035193323Sed } 1036193323Sed 1037195098Sed unsigned Opc; 1038193323Sed if (GVar && GVar->isThreadLocal()) 1039193323Sed Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress; 1040193323Sed else 1041193323Sed Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; 1042193323Sed 1043193323Sed FoldingSetNodeID ID; 1044193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1045193323Sed ID.AddPointer(GV); 1046193323Sed ID.AddInteger(Offset); 1047195098Sed ID.AddInteger(TargetFlags); 1048193323Sed void *IP = 0; 1049201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1050193323Sed return SDValue(E, 0); 1051201360Srdivacky 1052210299Sed SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT, 1053205407Srdivacky Offset, TargetFlags); 1054193323Sed CSEMap.InsertNode(N, IP); 1055193323Sed AllNodes.push_back(N); 1056193323Sed return SDValue(N, 0); 1057193323Sed} 1058193323Sed 1059198090SrdivackySDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) { 1060193323Sed unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; 1061193323Sed FoldingSetNodeID ID; 1062193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1063193323Sed ID.AddInteger(FI); 1064193323Sed void *IP = 0; 1065201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1066193323Sed return SDValue(E, 0); 1067201360Srdivacky 1068205407Srdivacky SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget); 1069193323Sed CSEMap.InsertNode(N, IP); 1070193323Sed AllNodes.push_back(N); 1071193323Sed return SDValue(N, 0); 1072193323Sed} 1073193323Sed 1074198090SrdivackySDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget, 1075195098Sed unsigned char TargetFlags) { 1076195098Sed assert((TargetFlags == 0 || isTarget) && 1077195098Sed "Cannot set target flags on target-independent jump tables"); 1078193323Sed unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; 1079193323Sed FoldingSetNodeID ID; 1080193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1081193323Sed ID.AddInteger(JTI); 1082195098Sed ID.AddInteger(TargetFlags); 1083193323Sed void *IP = 0; 1084201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1085193323Sed return SDValue(E, 0); 1086201360Srdivacky 1087205407Srdivacky SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget, 1088205407Srdivacky TargetFlags); 1089193323Sed CSEMap.InsertNode(N, IP); 1090193323Sed AllNodes.push_back(N); 1091193323Sed return SDValue(N, 0); 1092193323Sed} 1093193323Sed 1094207618SrdivackySDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT, 1095193323Sed unsigned Alignment, int Offset, 1096198090Srdivacky bool isTarget, 1097195098Sed unsigned char TargetFlags) { 1098195098Sed assert((TargetFlags == 0 || isTarget) && 1099195098Sed "Cannot set target flags on target-independent globals"); 1100193323Sed if (Alignment == 0) 1101193323Sed Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType()); 1102193323Sed unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 1103193323Sed FoldingSetNodeID ID; 1104193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1105193323Sed ID.AddInteger(Alignment); 1106193323Sed ID.AddInteger(Offset); 1107193323Sed ID.AddPointer(C); 1108195098Sed ID.AddInteger(TargetFlags); 1109193323Sed void *IP = 0; 1110201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1111193323Sed return SDValue(E, 0); 1112201360Srdivacky 1113205407Srdivacky SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset, 1114205407Srdivacky Alignment, TargetFlags); 1115193323Sed CSEMap.InsertNode(N, IP); 1116193323Sed AllNodes.push_back(N); 1117193323Sed return SDValue(N, 0); 1118193323Sed} 1119193323Sed 1120193323Sed 1121198090SrdivackySDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT, 1122193323Sed unsigned Alignment, int Offset, 1123195098Sed bool isTarget, 1124195098Sed unsigned char TargetFlags) { 1125195098Sed assert((TargetFlags == 0 || isTarget) && 1126195098Sed "Cannot set target flags on target-independent globals"); 1127193323Sed if (Alignment == 0) 1128193323Sed Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType()); 1129193323Sed unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 1130193323Sed FoldingSetNodeID ID; 1131193323Sed AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1132193323Sed ID.AddInteger(Alignment); 1133193323Sed ID.AddInteger(Offset); 1134193323Sed C->AddSelectionDAGCSEId(ID); 1135195098Sed ID.AddInteger(TargetFlags); 1136193323Sed void *IP = 0; 1137201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1138193323Sed return SDValue(E, 0); 1139201360Srdivacky 1140205407Srdivacky SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset, 1141205407Srdivacky Alignment, TargetFlags); 1142193323Sed CSEMap.InsertNode(N, IP); 1143193323Sed AllNodes.push_back(N); 1144193323Sed return SDValue(N, 0); 1145193323Sed} 1146193323Sed 1147193323SedSDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 1148193323Sed FoldingSetNodeID ID; 1149193323Sed AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0); 1150193323Sed ID.AddPointer(MBB); 1151193323Sed void *IP = 0; 1152201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1153193323Sed return SDValue(E, 0); 1154201360Srdivacky 1155205407Srdivacky SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB); 1156193323Sed CSEMap.InsertNode(N, IP); 1157193323Sed AllNodes.push_back(N); 1158193323Sed return SDValue(N, 0); 1159193323Sed} 1160193323Sed 1161198090SrdivackySDValue SelectionDAG::getValueType(EVT VT) { 1162198090Srdivacky if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >= 1163198090Srdivacky ValueTypeNodes.size()) 1164198090Srdivacky ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1); 1165193323Sed 1166193323Sed SDNode *&N = VT.isExtended() ? 1167198090Srdivacky ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy]; 1168193323Sed 1169193323Sed if (N) return SDValue(N, 0); 1170205407Srdivacky N = new (NodeAllocator) VTSDNode(VT); 1171193323Sed AllNodes.push_back(N); 1172193323Sed return SDValue(N, 0); 1173193323Sed} 1174193323Sed 1175198090SrdivackySDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) { 1176193323Sed SDNode *&N = ExternalSymbols[Sym]; 1177193323Sed if (N) return SDValue(N, 0); 1178205407Srdivacky N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT); 1179193323Sed AllNodes.push_back(N); 1180193323Sed return SDValue(N, 0); 1181193323Sed} 1182193323Sed 1183198090SrdivackySDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT, 1184195098Sed unsigned char TargetFlags) { 1185195098Sed SDNode *&N = 1186195098Sed TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym, 1187195098Sed TargetFlags)]; 1188193323Sed if (N) return SDValue(N, 0); 1189205407Srdivacky N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT); 1190193323Sed AllNodes.push_back(N); 1191193323Sed return SDValue(N, 0); 1192193323Sed} 1193193323Sed 1194193323SedSDValue SelectionDAG::getCondCode(ISD::CondCode Cond) { 1195193323Sed if ((unsigned)Cond >= CondCodeNodes.size()) 1196193323Sed CondCodeNodes.resize(Cond+1); 1197193323Sed 1198193323Sed if (CondCodeNodes[Cond] == 0) { 1199205407Srdivacky CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond); 1200193323Sed CondCodeNodes[Cond] = N; 1201193323Sed AllNodes.push_back(N); 1202193323Sed } 1203201360Srdivacky 1204193323Sed return SDValue(CondCodeNodes[Cond], 0); 1205193323Sed} 1206193323Sed 1207193323Sed// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in 1208193323Sed// the shuffle mask M that point at N1 to point at N2, and indices that point 1209193323Sed// N2 to point at N1. 1210193323Sedstatic void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) { 1211193323Sed std::swap(N1, N2); 1212193323Sed int NElts = M.size(); 1213193323Sed for (int i = 0; i != NElts; ++i) { 1214193323Sed if (M[i] >= NElts) 1215193323Sed M[i] -= NElts; 1216193323Sed else if (M[i] >= 0) 1217193323Sed M[i] += NElts; 1218193323Sed } 1219193323Sed} 1220193323Sed 1221198090SrdivackySDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1, 1222193323Sed SDValue N2, const int *Mask) { 1223193323Sed assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE"); 1224198090Srdivacky assert(VT.isVector() && N1.getValueType().isVector() && 1225193323Sed "Vector Shuffle VTs must be a vectors"); 1226193323Sed assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() 1227193323Sed && "Vector Shuffle VTs must have same element type"); 1228193323Sed 1229193323Sed // Canonicalize shuffle undef, undef -> undef 1230193323Sed if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF) 1231198090Srdivacky return getUNDEF(VT); 1232193323Sed 1233198090Srdivacky // Validate that all indices in Mask are within the range of the elements 1234193323Sed // input to the shuffle. 1235193323Sed unsigned NElts = VT.getVectorNumElements(); 1236193323Sed SmallVector<int, 8> MaskVec; 1237193323Sed for (unsigned i = 0; i != NElts; ++i) { 1238193323Sed assert(Mask[i] < (int)(NElts * 2) && "Index out of range"); 1239193323Sed MaskVec.push_back(Mask[i]); 1240193323Sed } 1241198090Srdivacky 1242193323Sed // Canonicalize shuffle v, v -> v, undef 1243193323Sed if (N1 == N2) { 1244193323Sed N2 = getUNDEF(VT); 1245193323Sed for (unsigned i = 0; i != NElts; ++i) 1246193323Sed if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts; 1247193323Sed } 1248198090Srdivacky 1249193323Sed // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. 1250193323Sed if (N1.getOpcode() == ISD::UNDEF) 1251193323Sed commuteShuffle(N1, N2, MaskVec); 1252198090Srdivacky 1253193323Sed // Canonicalize all index into lhs, -> shuffle lhs, undef 1254193323Sed // Canonicalize all index into rhs, -> shuffle rhs, undef 1255193323Sed bool AllLHS = true, AllRHS = true; 1256193323Sed bool N2Undef = N2.getOpcode() == ISD::UNDEF; 1257193323Sed for (unsigned i = 0; i != NElts; ++i) { 1258193323Sed if (MaskVec[i] >= (int)NElts) { 1259193323Sed if (N2Undef) 1260193323Sed MaskVec[i] = -1; 1261193323Sed else 1262193323Sed AllLHS = false; 1263193323Sed } else if (MaskVec[i] >= 0) { 1264193323Sed AllRHS = false; 1265193323Sed } 1266193323Sed } 1267193323Sed if (AllLHS && AllRHS) 1268193323Sed return getUNDEF(VT); 1269193323Sed if (AllLHS && !N2Undef) 1270193323Sed N2 = getUNDEF(VT); 1271193323Sed if (AllRHS) { 1272193323Sed N1 = getUNDEF(VT); 1273193323Sed commuteShuffle(N1, N2, MaskVec); 1274193323Sed } 1275198090Srdivacky 1276193323Sed // If Identity shuffle, or all shuffle in to undef, return that node. 1277193323Sed bool AllUndef = true; 1278193323Sed bool Identity = true; 1279193323Sed for (unsigned i = 0; i != NElts; ++i) { 1280193323Sed if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false; 1281193323Sed if (MaskVec[i] >= 0) AllUndef = false; 1282193323Sed } 1283198090Srdivacky if (Identity && NElts == N1.getValueType().getVectorNumElements()) 1284193323Sed return N1; 1285193323Sed if (AllUndef) 1286193323Sed return getUNDEF(VT); 1287193323Sed 1288193323Sed FoldingSetNodeID ID; 1289193323Sed SDValue Ops[2] = { N1, N2 }; 1290193323Sed AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2); 1291193323Sed for (unsigned i = 0; i != NElts; ++i) 1292193323Sed ID.AddInteger(MaskVec[i]); 1293198090Srdivacky 1294193323Sed void* IP = 0; 1295201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1296193323Sed return SDValue(E, 0); 1297198090Srdivacky 1298193323Sed // Allocate the mask array for the node out of the BumpPtrAllocator, since 1299193323Sed // SDNode doesn't have access to it. This memory will be "leaked" when 1300193323Sed // the node is deallocated, but recovered when the NodeAllocator is released. 1301193323Sed int *MaskAlloc = OperandAllocator.Allocate<int>(NElts); 1302193323Sed memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int)); 1303198090Srdivacky 1304205407Srdivacky ShuffleVectorSDNode *N = 1305205407Srdivacky new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc); 1306193323Sed CSEMap.InsertNode(N, IP); 1307193323Sed AllNodes.push_back(N); 1308193323Sed return SDValue(N, 0); 1309193323Sed} 1310193323Sed 1311198090SrdivackySDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl, 1312193323Sed SDValue Val, SDValue DTy, 1313193323Sed SDValue STy, SDValue Rnd, SDValue Sat, 1314193323Sed ISD::CvtCode Code) { 1315193323Sed // If the src and dest types are the same and the conversion is between 1316193323Sed // integer types of the same sign or two floats, no conversion is necessary. 1317193323Sed if (DTy == STy && 1318193323Sed (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF)) 1319193323Sed return Val; 1320193323Sed 1321193323Sed FoldingSetNodeID ID; 1322199481Srdivacky SDValue Ops[] = { Val, DTy, STy, Rnd, Sat }; 1323199481Srdivacky AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5); 1324193323Sed void* IP = 0; 1325201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1326193323Sed return SDValue(E, 0); 1327201360Srdivacky 1328205407Srdivacky CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5, 1329205407Srdivacky Code); 1330193323Sed CSEMap.InsertNode(N, IP); 1331193323Sed AllNodes.push_back(N); 1332193323Sed return SDValue(N, 0); 1333193323Sed} 1334193323Sed 1335198090SrdivackySDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) { 1336193323Sed FoldingSetNodeID ID; 1337193323Sed AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0); 1338193323Sed ID.AddInteger(RegNo); 1339193323Sed void *IP = 0; 1340201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1341193323Sed return SDValue(E, 0); 1342201360Srdivacky 1343205407Srdivacky SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT); 1344193323Sed CSEMap.InsertNode(N, IP); 1345193323Sed AllNodes.push_back(N); 1346193323Sed return SDValue(N, 0); 1347193323Sed} 1348193323Sed 1349205218SrdivackySDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) { 1350193323Sed FoldingSetNodeID ID; 1351193323Sed SDValue Ops[] = { Root }; 1352205218Srdivacky AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1); 1353205218Srdivacky ID.AddPointer(Label); 1354193323Sed void *IP = 0; 1355201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1356193323Sed return SDValue(E, 0); 1357218893Sdim 1358205407Srdivacky SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label); 1359193323Sed CSEMap.InsertNode(N, IP); 1360193323Sed AllNodes.push_back(N); 1361193323Sed return SDValue(N, 0); 1362193323Sed} 1363193323Sed 1364205218Srdivacky 1365207618SrdivackySDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT, 1366199989Srdivacky bool isTarget, 1367199989Srdivacky unsigned char TargetFlags) { 1368198892Srdivacky unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress; 1369198892Srdivacky 1370198892Srdivacky FoldingSetNodeID ID; 1371199989Srdivacky AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 1372198892Srdivacky ID.AddPointer(BA); 1373199989Srdivacky ID.AddInteger(TargetFlags); 1374198892Srdivacky void *IP = 0; 1375201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1376198892Srdivacky return SDValue(E, 0); 1377201360Srdivacky 1378205407Srdivacky SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags); 1379198892Srdivacky CSEMap.InsertNode(N, IP); 1380198892Srdivacky AllNodes.push_back(N); 1381198892Srdivacky return SDValue(N, 0); 1382198892Srdivacky} 1383198892Srdivacky 1384193323SedSDValue SelectionDAG::getSrcValue(const Value *V) { 1385204642Srdivacky assert((!V || V->getType()->isPointerTy()) && 1386193323Sed "SrcValue is not a pointer?"); 1387193323Sed 1388193323Sed FoldingSetNodeID ID; 1389193323Sed AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0); 1390193323Sed ID.AddPointer(V); 1391193323Sed 1392193323Sed void *IP = 0; 1393201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1394193323Sed return SDValue(E, 0); 1395193323Sed 1396205407Srdivacky SDNode *N = new (NodeAllocator) SrcValueSDNode(V); 1397193323Sed CSEMap.InsertNode(N, IP); 1398193323Sed AllNodes.push_back(N); 1399193323Sed return SDValue(N, 0); 1400193323Sed} 1401193323Sed 1402207618Srdivacky/// getMDNode - Return an MDNodeSDNode which holds an MDNode. 1403207618SrdivackySDValue SelectionDAG::getMDNode(const MDNode *MD) { 1404207618Srdivacky FoldingSetNodeID ID; 1405207618Srdivacky AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0); 1406207618Srdivacky ID.AddPointer(MD); 1407218893Sdim 1408207618Srdivacky void *IP = 0; 1409207618Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1410207618Srdivacky return SDValue(E, 0); 1411218893Sdim 1412207618Srdivacky SDNode *N = new (NodeAllocator) MDNodeSDNode(MD); 1413207618Srdivacky CSEMap.InsertNode(N, IP); 1414207618Srdivacky AllNodes.push_back(N); 1415207618Srdivacky return SDValue(N, 0); 1416207618Srdivacky} 1417207618Srdivacky 1418207618Srdivacky 1419193323Sed/// getShiftAmountOperand - Return the specified value casted to 1420193323Sed/// the target's desired shift amount type. 1421193323SedSDValue SelectionDAG::getShiftAmountOperand(SDValue Op) { 1422198090Srdivacky EVT OpTy = Op.getValueType(); 1423193323Sed MVT ShTy = TLI.getShiftAmountTy(); 1424193323Sed if (OpTy == ShTy || OpTy.isVector()) return Op; 1425193323Sed 1426193323Sed ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; 1427193323Sed return getNode(Opcode, Op.getDebugLoc(), ShTy, Op); 1428193323Sed} 1429193323Sed 1430193323Sed/// CreateStackTemporary - Create a stack temporary, suitable for holding the 1431193323Sed/// specified value type. 1432198090SrdivackySDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) { 1433193323Sed MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 1434198090Srdivacky unsigned ByteSize = VT.getStoreSize(); 1435198090Srdivacky const Type *Ty = VT.getTypeForEVT(*getContext()); 1436193323Sed unsigned StackAlign = 1437193323Sed std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign); 1438193323Sed 1439199481Srdivacky int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); 1440193323Sed return getFrameIndex(FrameIdx, TLI.getPointerTy()); 1441193323Sed} 1442193323Sed 1443193323Sed/// CreateStackTemporary - Create a stack temporary suitable for holding 1444193323Sed/// either of the specified value types. 1445198090SrdivackySDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) { 1446193323Sed unsigned Bytes = std::max(VT1.getStoreSizeInBits(), 1447193323Sed VT2.getStoreSizeInBits())/8; 1448198090Srdivacky const Type *Ty1 = VT1.getTypeForEVT(*getContext()); 1449198090Srdivacky const Type *Ty2 = VT2.getTypeForEVT(*getContext()); 1450193323Sed const TargetData *TD = TLI.getTargetData(); 1451193323Sed unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1), 1452193323Sed TD->getPrefTypeAlignment(Ty2)); 1453193323Sed 1454193323Sed MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 1455199481Srdivacky int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false); 1456193323Sed return getFrameIndex(FrameIdx, TLI.getPointerTy()); 1457193323Sed} 1458193323Sed 1459198090SrdivackySDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, 1460193323Sed SDValue N2, ISD::CondCode Cond, DebugLoc dl) { 1461193323Sed // These setcc operations always fold. 1462193323Sed switch (Cond) { 1463193323Sed default: break; 1464193323Sed case ISD::SETFALSE: 1465193323Sed case ISD::SETFALSE2: return getConstant(0, VT); 1466193323Sed case ISD::SETTRUE: 1467193323Sed case ISD::SETTRUE2: return getConstant(1, VT); 1468193323Sed 1469193323Sed case ISD::SETOEQ: 1470193323Sed case ISD::SETOGT: 1471193323Sed case ISD::SETOGE: 1472193323Sed case ISD::SETOLT: 1473193323Sed case ISD::SETOLE: 1474193323Sed case ISD::SETONE: 1475193323Sed case ISD::SETO: 1476193323Sed case ISD::SETUO: 1477193323Sed case ISD::SETUEQ: 1478193323Sed case ISD::SETUNE: 1479193323Sed assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!"); 1480193323Sed break; 1481193323Sed } 1482193323Sed 1483193323Sed if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) { 1484193323Sed const APInt &C2 = N2C->getAPIntValue(); 1485193323Sed if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 1486193323Sed const APInt &C1 = N1C->getAPIntValue(); 1487193323Sed 1488193323Sed switch (Cond) { 1489198090Srdivacky default: llvm_unreachable("Unknown integer setcc!"); 1490193323Sed case ISD::SETEQ: return getConstant(C1 == C2, VT); 1491193323Sed case ISD::SETNE: return getConstant(C1 != C2, VT); 1492193323Sed case ISD::SETULT: return getConstant(C1.ult(C2), VT); 1493193323Sed case ISD::SETUGT: return getConstant(C1.ugt(C2), VT); 1494193323Sed case ISD::SETULE: return getConstant(C1.ule(C2), VT); 1495193323Sed case ISD::SETUGE: return getConstant(C1.uge(C2), VT); 1496193323Sed case ISD::SETLT: return getConstant(C1.slt(C2), VT); 1497193323Sed case ISD::SETGT: return getConstant(C1.sgt(C2), VT); 1498193323Sed case ISD::SETLE: return getConstant(C1.sle(C2), VT); 1499193323Sed case ISD::SETGE: return getConstant(C1.sge(C2), VT); 1500193323Sed } 1501193323Sed } 1502193323Sed } 1503193323Sed if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) { 1504193323Sed if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) { 1505193323Sed // No compile time operations on this type yet. 1506193323Sed if (N1C->getValueType(0) == MVT::ppcf128) 1507193323Sed return SDValue(); 1508193323Sed 1509193323Sed APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF()); 1510193323Sed switch (Cond) { 1511193323Sed default: break; 1512193323Sed case ISD::SETEQ: if (R==APFloat::cmpUnordered) 1513193323Sed return getUNDEF(VT); 1514193323Sed // fall through 1515193323Sed case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT); 1516193323Sed case ISD::SETNE: if (R==APFloat::cmpUnordered) 1517193323Sed return getUNDEF(VT); 1518193323Sed // fall through 1519193323Sed case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan || 1520193323Sed R==APFloat::cmpLessThan, VT); 1521193323Sed case ISD::SETLT: if (R==APFloat::cmpUnordered) 1522193323Sed return getUNDEF(VT); 1523193323Sed // fall through 1524193323Sed case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT); 1525193323Sed case ISD::SETGT: if (R==APFloat::cmpUnordered) 1526193323Sed return getUNDEF(VT); 1527193323Sed // fall through 1528193323Sed case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT); 1529193323Sed case ISD::SETLE: if (R==APFloat::cmpUnordered) 1530193323Sed return getUNDEF(VT); 1531193323Sed // fall through 1532193323Sed case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan || 1533193323Sed R==APFloat::cmpEqual, VT); 1534193323Sed case ISD::SETGE: if (R==APFloat::cmpUnordered) 1535193323Sed return getUNDEF(VT); 1536193323Sed // fall through 1537193323Sed case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan || 1538193323Sed R==APFloat::cmpEqual, VT); 1539193323Sed case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT); 1540193323Sed case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT); 1541193323Sed case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered || 1542193323Sed R==APFloat::cmpEqual, VT); 1543193323Sed case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT); 1544193323Sed case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered || 1545193323Sed R==APFloat::cmpLessThan, VT); 1546193323Sed case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan || 1547193323Sed R==APFloat::cmpUnordered, VT); 1548193323Sed case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT); 1549193323Sed case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT); 1550193323Sed } 1551193323Sed } else { 1552193323Sed // Ensure that the constant occurs on the RHS. 1553193323Sed return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 1554193323Sed } 1555193323Sed } 1556193323Sed 1557193323Sed // Could not fold it. 1558193323Sed return SDValue(); 1559193323Sed} 1560193323Sed 1561193323Sed/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We 1562193323Sed/// use this predicate to simplify operations downstream. 1563193323Sedbool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const { 1564198090Srdivacky // This predicate is not safe for vector operations. 1565198090Srdivacky if (Op.getValueType().isVector()) 1566198090Srdivacky return false; 1567198090Srdivacky 1568200581Srdivacky unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 1569193323Sed return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth); 1570193323Sed} 1571193323Sed 1572193323Sed/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 1573193323Sed/// this predicate to simplify operations downstream. Mask is known to be zero 1574193323Sed/// for bits that V cannot have. 1575193323Sedbool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask, 1576193323Sed unsigned Depth) const { 1577193323Sed APInt KnownZero, KnownOne; 1578193323Sed ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 1579193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1580193323Sed return (KnownZero & Mask) == Mask; 1581193323Sed} 1582193323Sed 1583193323Sed/// ComputeMaskedBits - Determine which of the bits specified in Mask are 1584193323Sed/// known to be either zero or one and return them in the KnownZero/KnownOne 1585193323Sed/// bitsets. This code only analyzes bits in Mask, in order to short-circuit 1586193323Sed/// processing. 1587193323Sedvoid SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask, 1588193323Sed APInt &KnownZero, APInt &KnownOne, 1589193323Sed unsigned Depth) const { 1590193323Sed unsigned BitWidth = Mask.getBitWidth(); 1591200581Srdivacky assert(BitWidth == Op.getValueType().getScalarType().getSizeInBits() && 1592193323Sed "Mask size mismatches value type size!"); 1593193323Sed 1594193323Sed KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything. 1595193323Sed if (Depth == 6 || Mask == 0) 1596193323Sed return; // Limit search depth. 1597193323Sed 1598193323Sed APInt KnownZero2, KnownOne2; 1599193323Sed 1600193323Sed switch (Op.getOpcode()) { 1601193323Sed case ISD::Constant: 1602193323Sed // We know all of the bits for a constant! 1603193323Sed KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask; 1604193323Sed KnownZero = ~KnownOne & Mask; 1605193323Sed return; 1606193323Sed case ISD::AND: 1607193323Sed // If either the LHS or the RHS are Zero, the result is zero. 1608193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1609193323Sed ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero, 1610193323Sed KnownZero2, KnownOne2, Depth+1); 1611193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1612193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1613193323Sed 1614193323Sed // Output known-1 bits are only known if set in both the LHS & RHS. 1615193323Sed KnownOne &= KnownOne2; 1616193323Sed // Output known-0 are known to be clear if zero in either the LHS | RHS. 1617193323Sed KnownZero |= KnownZero2; 1618193323Sed return; 1619193323Sed case ISD::OR: 1620193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1621193323Sed ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne, 1622193323Sed KnownZero2, KnownOne2, Depth+1); 1623193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1624193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1625193323Sed 1626193323Sed // Output known-0 bits are only known if clear in both the LHS & RHS. 1627193323Sed KnownZero &= KnownZero2; 1628193323Sed // Output known-1 are known to be set if set in either the LHS | RHS. 1629193323Sed KnownOne |= KnownOne2; 1630193323Sed return; 1631193323Sed case ISD::XOR: { 1632193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1633193323Sed ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1634193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1635193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1636193323Sed 1637193323Sed // Output known-0 bits are known if clear or set in both the LHS & RHS. 1638193323Sed APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 1639193323Sed // Output known-1 are known to be set if set in only one of the LHS, RHS. 1640193323Sed KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 1641193323Sed KnownZero = KnownZeroOut; 1642193323Sed return; 1643193323Sed } 1644193323Sed case ISD::MUL: { 1645193323Sed APInt Mask2 = APInt::getAllOnesValue(BitWidth); 1646193323Sed ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1); 1647193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); 1648193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1649193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1650193323Sed 1651193323Sed // If low bits are zero in either operand, output low known-0 bits. 1652193323Sed // Also compute a conserative estimate for high known-0 bits. 1653193323Sed // More trickiness is possible, but this is sufficient for the 1654193323Sed // interesting case of alignment computation. 1655218893Sdim KnownOne.clearAllBits(); 1656193323Sed unsigned TrailZ = KnownZero.countTrailingOnes() + 1657193323Sed KnownZero2.countTrailingOnes(); 1658193323Sed unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + 1659193323Sed KnownZero2.countLeadingOnes(), 1660193323Sed BitWidth) - BitWidth; 1661193323Sed 1662193323Sed TrailZ = std::min(TrailZ, BitWidth); 1663193323Sed LeadZ = std::min(LeadZ, BitWidth); 1664193323Sed KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | 1665193323Sed APInt::getHighBitsSet(BitWidth, LeadZ); 1666193323Sed KnownZero &= Mask; 1667193323Sed return; 1668193323Sed } 1669193323Sed case ISD::UDIV: { 1670193323Sed // For the purposes of computing leading zeros we can conservatively 1671193323Sed // treat a udiv as a logical right shift by the power of 2 known to 1672193323Sed // be less than the denominator. 1673193323Sed APInt AllOnes = APInt::getAllOnesValue(BitWidth); 1674193323Sed ComputeMaskedBits(Op.getOperand(0), 1675193323Sed AllOnes, KnownZero2, KnownOne2, Depth+1); 1676193323Sed unsigned LeadZ = KnownZero2.countLeadingOnes(); 1677193323Sed 1678218893Sdim KnownOne2.clearAllBits(); 1679218893Sdim KnownZero2.clearAllBits(); 1680193323Sed ComputeMaskedBits(Op.getOperand(1), 1681193323Sed AllOnes, KnownZero2, KnownOne2, Depth+1); 1682193323Sed unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); 1683193323Sed if (RHSUnknownLeadingOnes != BitWidth) 1684193323Sed LeadZ = std::min(BitWidth, 1685193323Sed LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); 1686193323Sed 1687193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask; 1688193323Sed return; 1689193323Sed } 1690193323Sed case ISD::SELECT: 1691193323Sed ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1); 1692193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); 1693193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1694193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1695193323Sed 1696193323Sed // Only known if known in both the LHS and RHS. 1697193323Sed KnownOne &= KnownOne2; 1698193323Sed KnownZero &= KnownZero2; 1699193323Sed return; 1700193323Sed case ISD::SELECT_CC: 1701193323Sed ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1); 1702193323Sed ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1); 1703193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1704193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1705193323Sed 1706193323Sed // Only known if known in both the LHS and RHS. 1707193323Sed KnownOne &= KnownOne2; 1708193323Sed KnownZero &= KnownZero2; 1709193323Sed return; 1710193323Sed case ISD::SADDO: 1711193323Sed case ISD::UADDO: 1712193323Sed case ISD::SSUBO: 1713193323Sed case ISD::USUBO: 1714193323Sed case ISD::SMULO: 1715193323Sed case ISD::UMULO: 1716193323Sed if (Op.getResNo() != 1) 1717193323Sed return; 1718193323Sed // The boolean result conforms to getBooleanContents. Fall through. 1719193323Sed case ISD::SETCC: 1720193323Sed // If we know the result of a setcc has the top bits zero, use this info. 1721193323Sed if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent && 1722193323Sed BitWidth > 1) 1723193323Sed KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 1724193323Sed return; 1725193323Sed case ISD::SHL: 1726193323Sed // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 1727193323Sed if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1728193323Sed unsigned ShAmt = SA->getZExtValue(); 1729193323Sed 1730193323Sed // If the shift count is an invalid immediate, don't do anything. 1731193323Sed if (ShAmt >= BitWidth) 1732193323Sed return; 1733193323Sed 1734193323Sed ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt), 1735193323Sed KnownZero, KnownOne, Depth+1); 1736193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1737193323Sed KnownZero <<= ShAmt; 1738193323Sed KnownOne <<= ShAmt; 1739193323Sed // low bits known zero. 1740193323Sed KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt); 1741193323Sed } 1742193323Sed return; 1743193323Sed case ISD::SRL: 1744193323Sed // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 1745193323Sed if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1746193323Sed unsigned ShAmt = SA->getZExtValue(); 1747193323Sed 1748193323Sed // If the shift count is an invalid immediate, don't do anything. 1749193323Sed if (ShAmt >= BitWidth) 1750193323Sed return; 1751193323Sed 1752193323Sed ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt), 1753193323Sed KnownZero, KnownOne, Depth+1); 1754193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1755193323Sed KnownZero = KnownZero.lshr(ShAmt); 1756193323Sed KnownOne = KnownOne.lshr(ShAmt); 1757193323Sed 1758193323Sed APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask; 1759193323Sed KnownZero |= HighBits; // High bits known zero. 1760193323Sed } 1761193323Sed return; 1762193323Sed case ISD::SRA: 1763193323Sed if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1764193323Sed unsigned ShAmt = SA->getZExtValue(); 1765193323Sed 1766193323Sed // If the shift count is an invalid immediate, don't do anything. 1767193323Sed if (ShAmt >= BitWidth) 1768193323Sed return; 1769193323Sed 1770193323Sed APInt InDemandedMask = (Mask << ShAmt); 1771193323Sed // If any of the demanded bits are produced by the sign extension, we also 1772193323Sed // demand the input sign bit. 1773193323Sed APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask; 1774193323Sed if (HighBits.getBoolValue()) 1775193323Sed InDemandedMask |= APInt::getSignBit(BitWidth); 1776193323Sed 1777193323Sed ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne, 1778193323Sed Depth+1); 1779193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1780193323Sed KnownZero = KnownZero.lshr(ShAmt); 1781193323Sed KnownOne = KnownOne.lshr(ShAmt); 1782193323Sed 1783193323Sed // Handle the sign bits. 1784193323Sed APInt SignBit = APInt::getSignBit(BitWidth); 1785193323Sed SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask. 1786193323Sed 1787193323Sed if (KnownZero.intersects(SignBit)) { 1788193323Sed KnownZero |= HighBits; // New bits are known zero. 1789193323Sed } else if (KnownOne.intersects(SignBit)) { 1790193323Sed KnownOne |= HighBits; // New bits are known one. 1791193323Sed } 1792193323Sed } 1793193323Sed return; 1794193323Sed case ISD::SIGN_EXTEND_INREG: { 1795198090Srdivacky EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1796202375Srdivacky unsigned EBits = EVT.getScalarType().getSizeInBits(); 1797193323Sed 1798193323Sed // Sign extension. Compute the demanded bits in the result that are not 1799193323Sed // present in the input. 1800193323Sed APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask; 1801193323Sed 1802193323Sed APInt InSignBit = APInt::getSignBit(EBits); 1803193323Sed APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits); 1804193323Sed 1805193323Sed // If the sign extended bits are demanded, we know that the sign 1806193323Sed // bit is demanded. 1807218893Sdim InSignBit = InSignBit.zext(BitWidth); 1808193323Sed if (NewBits.getBoolValue()) 1809193323Sed InputDemandedBits |= InSignBit; 1810193323Sed 1811193323Sed ComputeMaskedBits(Op.getOperand(0), InputDemandedBits, 1812193323Sed KnownZero, KnownOne, Depth+1); 1813193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1814193323Sed 1815193323Sed // If the sign bit of the input is known set or clear, then we know the 1816193323Sed // top bits of the result. 1817193323Sed if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear 1818193323Sed KnownZero |= NewBits; 1819193323Sed KnownOne &= ~NewBits; 1820193323Sed } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set 1821193323Sed KnownOne |= NewBits; 1822193323Sed KnownZero &= ~NewBits; 1823193323Sed } else { // Input sign bit unknown 1824193323Sed KnownZero &= ~NewBits; 1825193323Sed KnownOne &= ~NewBits; 1826193323Sed } 1827193323Sed return; 1828193323Sed } 1829193323Sed case ISD::CTTZ: 1830193323Sed case ISD::CTLZ: 1831193323Sed case ISD::CTPOP: { 1832193323Sed unsigned LowBits = Log2_32(BitWidth)+1; 1833193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); 1834218893Sdim KnownOne.clearAllBits(); 1835193323Sed return; 1836193323Sed } 1837193323Sed case ISD::LOAD: { 1838193323Sed if (ISD::isZEXTLoad(Op.getNode())) { 1839193323Sed LoadSDNode *LD = cast<LoadSDNode>(Op); 1840198090Srdivacky EVT VT = LD->getMemoryVT(); 1841202375Srdivacky unsigned MemBits = VT.getScalarType().getSizeInBits(); 1842193323Sed KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask; 1843193323Sed } 1844193323Sed return; 1845193323Sed } 1846193323Sed case ISD::ZERO_EXTEND: { 1847198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1848200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1849193323Sed APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask; 1850218893Sdim APInt InMask = Mask.trunc(InBits); 1851218893Sdim KnownZero = KnownZero.trunc(InBits); 1852218893Sdim KnownOne = KnownOne.trunc(InBits); 1853193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1854218893Sdim KnownZero = KnownZero.zext(BitWidth); 1855218893Sdim KnownOne = KnownOne.zext(BitWidth); 1856193323Sed KnownZero |= NewBits; 1857193323Sed return; 1858193323Sed } 1859193323Sed case ISD::SIGN_EXTEND: { 1860198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1861200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1862193323Sed APInt InSignBit = APInt::getSignBit(InBits); 1863193323Sed APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask; 1864218893Sdim APInt InMask = Mask.trunc(InBits); 1865193323Sed 1866193323Sed // If any of the sign extended bits are demanded, we know that the sign 1867193323Sed // bit is demanded. Temporarily set this bit in the mask for our callee. 1868193323Sed if (NewBits.getBoolValue()) 1869193323Sed InMask |= InSignBit; 1870193323Sed 1871218893Sdim KnownZero = KnownZero.trunc(InBits); 1872218893Sdim KnownOne = KnownOne.trunc(InBits); 1873193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1874193323Sed 1875193323Sed // Note if the sign bit is known to be zero or one. 1876193323Sed bool SignBitKnownZero = KnownZero.isNegative(); 1877193323Sed bool SignBitKnownOne = KnownOne.isNegative(); 1878193323Sed assert(!(SignBitKnownZero && SignBitKnownOne) && 1879193323Sed "Sign bit can't be known to be both zero and one!"); 1880193323Sed 1881193323Sed // If the sign bit wasn't actually demanded by our caller, we don't 1882193323Sed // want it set in the KnownZero and KnownOne result values. Reset the 1883193323Sed // mask and reapply it to the result values. 1884218893Sdim InMask = Mask.trunc(InBits); 1885193323Sed KnownZero &= InMask; 1886193323Sed KnownOne &= InMask; 1887193323Sed 1888218893Sdim KnownZero = KnownZero.zext(BitWidth); 1889218893Sdim KnownOne = KnownOne.zext(BitWidth); 1890193323Sed 1891193323Sed // If the sign bit is known zero or one, the top bits match. 1892193323Sed if (SignBitKnownZero) 1893193323Sed KnownZero |= NewBits; 1894193323Sed else if (SignBitKnownOne) 1895193323Sed KnownOne |= NewBits; 1896193323Sed return; 1897193323Sed } 1898193323Sed case ISD::ANY_EXTEND: { 1899198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1900200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1901218893Sdim APInt InMask = Mask.trunc(InBits); 1902218893Sdim KnownZero = KnownZero.trunc(InBits); 1903218893Sdim KnownOne = KnownOne.trunc(InBits); 1904193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1905218893Sdim KnownZero = KnownZero.zext(BitWidth); 1906218893Sdim KnownOne = KnownOne.zext(BitWidth); 1907193323Sed return; 1908193323Sed } 1909193323Sed case ISD::TRUNCATE: { 1910198090Srdivacky EVT InVT = Op.getOperand(0).getValueType(); 1911200581Srdivacky unsigned InBits = InVT.getScalarType().getSizeInBits(); 1912218893Sdim APInt InMask = Mask.zext(InBits); 1913218893Sdim KnownZero = KnownZero.zext(InBits); 1914218893Sdim KnownOne = KnownOne.zext(InBits); 1915193323Sed ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1); 1916193323Sed assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1917218893Sdim KnownZero = KnownZero.trunc(BitWidth); 1918218893Sdim KnownOne = KnownOne.trunc(BitWidth); 1919193323Sed break; 1920193323Sed } 1921193323Sed case ISD::AssertZext: { 1922198090Srdivacky EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1923193323Sed APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits()); 1924193323Sed ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 1925193323Sed KnownOne, Depth+1); 1926193323Sed KnownZero |= (~InMask) & Mask; 1927193323Sed return; 1928193323Sed } 1929193323Sed case ISD::FGETSIGN: 1930193323Sed // All bits are zero except the low bit. 1931193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1); 1932193323Sed return; 1933193323Sed 1934193323Sed case ISD::SUB: { 1935193323Sed if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) { 1936193323Sed // We know that the top bits of C-X are clear if X contains less bits 1937193323Sed // than C (i.e. no wrap-around can happen). For example, 20-X is 1938193323Sed // positive if we can prove that X is >= 0 and < 16. 1939193323Sed if (CLHS->getAPIntValue().isNonNegative()) { 1940193323Sed unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros(); 1941193323Sed // NLZ can't be BitWidth with no sign bit 1942193323Sed APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); 1943193323Sed ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2, 1944193323Sed Depth+1); 1945193323Sed 1946193323Sed // If all of the MaskV bits are known to be zero, then we know the 1947193323Sed // output top bits are zero, because we now know that the output is 1948193323Sed // from [0-C]. 1949193323Sed if ((KnownZero2 & MaskV) == MaskV) { 1950193323Sed unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros(); 1951193323Sed // Top bits known zero. 1952193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; 1953193323Sed } 1954193323Sed } 1955193323Sed } 1956193323Sed } 1957193323Sed // fall through 1958218893Sdim case ISD::ADD: 1959218893Sdim case ISD::ADDE: { 1960193323Sed // Output known-0 bits are known if clear or set in both the low clear bits 1961193323Sed // common to both LHS & RHS. For example, 8+(X<<3) is known to have the 1962193323Sed // low 3 bits clear. 1963207618Srdivacky APInt Mask2 = APInt::getLowBitsSet(BitWidth, 1964207618Srdivacky BitWidth - Mask.countLeadingZeros()); 1965193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); 1966193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1967193323Sed unsigned KnownZeroOut = KnownZero2.countTrailingOnes(); 1968193323Sed 1969193323Sed ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1); 1970193323Sed assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1971193323Sed KnownZeroOut = std::min(KnownZeroOut, 1972193323Sed KnownZero2.countTrailingOnes()); 1973193323Sed 1974218893Sdim if (Op.getOpcode() == ISD::ADD) { 1975218893Sdim KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut); 1976218893Sdim return; 1977218893Sdim } 1978218893Sdim 1979218893Sdim // With ADDE, a carry bit may be added in, so we can only use this 1980218893Sdim // information if we know (at least) that the low two bits are clear. We 1981218893Sdim // then return to the caller that the low bit is unknown but that other bits 1982218893Sdim // are known zero. 1983218893Sdim if (KnownZeroOut >= 2) // ADDE 1984218893Sdim KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut); 1985193323Sed return; 1986193323Sed } 1987193323Sed case ISD::SREM: 1988193323Sed if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1989203954Srdivacky const APInt &RA = Rem->getAPIntValue().abs(); 1990203954Srdivacky if (RA.isPowerOf2()) { 1991203954Srdivacky APInt LowBits = RA - 1; 1992193323Sed APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); 1993193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1); 1994193323Sed 1995203954Srdivacky // The low bits of the first operand are unchanged by the srem. 1996203954Srdivacky KnownZero = KnownZero2 & LowBits; 1997203954Srdivacky KnownOne = KnownOne2 & LowBits; 1998203954Srdivacky 1999203954Srdivacky // If the first operand is non-negative or has all low bits zero, then 2000203954Srdivacky // the upper bits are all zero. 2001193323Sed if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) 2002203954Srdivacky KnownZero |= ~LowBits; 2003193323Sed 2004203954Srdivacky // If the first operand is negative and not all low bits are zero, then 2005203954Srdivacky // the upper bits are all one. 2006203954Srdivacky if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) 2007203954Srdivacky KnownOne |= ~LowBits; 2008193323Sed 2009203954Srdivacky KnownZero &= Mask; 2010203954Srdivacky KnownOne &= Mask; 2011203954Srdivacky 2012193323Sed assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 2013193323Sed } 2014193323Sed } 2015193323Sed return; 2016193323Sed case ISD::UREM: { 2017193323Sed if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2018193323Sed const APInt &RA = Rem->getAPIntValue(); 2019193323Sed if (RA.isPowerOf2()) { 2020193323Sed APInt LowBits = (RA - 1); 2021193323Sed APInt Mask2 = LowBits & Mask; 2022193323Sed KnownZero |= ~LowBits & Mask; 2023193323Sed ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1); 2024193323Sed assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 2025193323Sed break; 2026193323Sed } 2027193323Sed } 2028193323Sed 2029193323Sed // Since the result is less than or equal to either operand, any leading 2030193323Sed // zero bits in either operand must also exist in the result. 2031193323Sed APInt AllOnes = APInt::getAllOnesValue(BitWidth); 2032193323Sed ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne, 2033193323Sed Depth+1); 2034193323Sed ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2, 2035193323Sed Depth+1); 2036193323Sed 2037193323Sed uint32_t Leaders = std::max(KnownZero.countLeadingOnes(), 2038193323Sed KnownZero2.countLeadingOnes()); 2039218893Sdim KnownOne.clearAllBits(); 2040193323Sed KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask; 2041193323Sed return; 2042193323Sed } 2043218893Sdim case ISD::FrameIndex: 2044218893Sdim case ISD::TargetFrameIndex: 2045218893Sdim if (unsigned Align = InferPtrAlignment(Op)) { 2046218893Sdim // The low bits are known zero if the pointer is aligned. 2047218893Sdim KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align)); 2048218893Sdim return; 2049218893Sdim } 2050218893Sdim break; 2051218893Sdim 2052193323Sed default: 2053193323Sed // Allow the target to implement this method for its nodes. 2054193323Sed if (Op.getOpcode() >= ISD::BUILTIN_OP_END) { 2055193323Sed case ISD::INTRINSIC_WO_CHAIN: 2056193323Sed case ISD::INTRINSIC_W_CHAIN: 2057193323Sed case ISD::INTRINSIC_VOID: 2058198090Srdivacky TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this, 2059198090Srdivacky Depth); 2060193323Sed } 2061193323Sed return; 2062193323Sed } 2063193323Sed} 2064193323Sed 2065193323Sed/// ComputeNumSignBits - Return the number of times the sign bit of the 2066193323Sed/// register is replicated into the other bits. We know that at least 1 bit 2067193323Sed/// is always equal to the sign bit (itself), but other cases can give us 2068193323Sed/// information. For example, immediately after an "SRA X, 2", we know that 2069193323Sed/// the top 3 bits are all equal to each other, so we return 3. 2070193323Sedunsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{ 2071198090Srdivacky EVT VT = Op.getValueType(); 2072193323Sed assert(VT.isInteger() && "Invalid VT!"); 2073200581Srdivacky unsigned VTBits = VT.getScalarType().getSizeInBits(); 2074193323Sed unsigned Tmp, Tmp2; 2075193323Sed unsigned FirstAnswer = 1; 2076193323Sed 2077193323Sed if (Depth == 6) 2078193323Sed return 1; // Limit search depth. 2079193323Sed 2080193323Sed switch (Op.getOpcode()) { 2081193323Sed default: break; 2082193323Sed case ISD::AssertSext: 2083193323Sed Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 2084193323Sed return VTBits-Tmp+1; 2085193323Sed case ISD::AssertZext: 2086193323Sed Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 2087193323Sed return VTBits-Tmp; 2088193323Sed 2089193323Sed case ISD::Constant: { 2090193323Sed const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue(); 2091193323Sed // If negative, return # leading ones. 2092193323Sed if (Val.isNegative()) 2093193323Sed return Val.countLeadingOnes(); 2094193323Sed 2095193323Sed // Return # leading zeros. 2096193323Sed return Val.countLeadingZeros(); 2097193323Sed } 2098193323Sed 2099193323Sed case ISD::SIGN_EXTEND: 2100200581Srdivacky Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); 2101193323Sed return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp; 2102193323Sed 2103193323Sed case ISD::SIGN_EXTEND_INREG: 2104193323Sed // Max of the input and what this extends. 2105202375Srdivacky Tmp = 2106202375Srdivacky cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits(); 2107193323Sed Tmp = VTBits-Tmp+1; 2108193323Sed 2109193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2110193323Sed return std::max(Tmp, Tmp2); 2111193323Sed 2112193323Sed case ISD::SRA: 2113193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2114193323Sed // SRA X, C -> adds C sign bits. 2115193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2116193323Sed Tmp += C->getZExtValue(); 2117193323Sed if (Tmp > VTBits) Tmp = VTBits; 2118193323Sed } 2119193323Sed return Tmp; 2120193323Sed case ISD::SHL: 2121193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2122193323Sed // shl destroys sign bits. 2123193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2124193323Sed if (C->getZExtValue() >= VTBits || // Bad shift. 2125193323Sed C->getZExtValue() >= Tmp) break; // Shifted all sign bits out. 2126193323Sed return Tmp - C->getZExtValue(); 2127193323Sed } 2128193323Sed break; 2129193323Sed case ISD::AND: 2130193323Sed case ISD::OR: 2131193323Sed case ISD::XOR: // NOT is handled here. 2132193323Sed // Logical binary ops preserve the number of sign bits at the worst. 2133193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2134193323Sed if (Tmp != 1) { 2135193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2136193323Sed FirstAnswer = std::min(Tmp, Tmp2); 2137193323Sed // We computed what we know about the sign bits as our first 2138193323Sed // answer. Now proceed to the generic code that uses 2139193323Sed // ComputeMaskedBits, and pick whichever answer is better. 2140193323Sed } 2141193323Sed break; 2142193323Sed 2143193323Sed case ISD::SELECT: 2144193323Sed Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2145193323Sed if (Tmp == 1) return 1; // Early out. 2146193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1); 2147193323Sed return std::min(Tmp, Tmp2); 2148193323Sed 2149193323Sed case ISD::SADDO: 2150193323Sed case ISD::UADDO: 2151193323Sed case ISD::SSUBO: 2152193323Sed case ISD::USUBO: 2153193323Sed case ISD::SMULO: 2154193323Sed case ISD::UMULO: 2155193323Sed if (Op.getResNo() != 1) 2156193323Sed break; 2157193323Sed // The boolean result conforms to getBooleanContents. Fall through. 2158193323Sed case ISD::SETCC: 2159193323Sed // If setcc returns 0/-1, all bits are sign bits. 2160193323Sed if (TLI.getBooleanContents() == 2161193323Sed TargetLowering::ZeroOrNegativeOneBooleanContent) 2162193323Sed return VTBits; 2163193323Sed break; 2164193323Sed case ISD::ROTL: 2165193323Sed case ISD::ROTR: 2166193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2167193323Sed unsigned RotAmt = C->getZExtValue() & (VTBits-1); 2168193323Sed 2169193323Sed // Handle rotate right by N like a rotate left by 32-N. 2170193323Sed if (Op.getOpcode() == ISD::ROTR) 2171193323Sed RotAmt = (VTBits-RotAmt) & (VTBits-1); 2172193323Sed 2173193323Sed // If we aren't rotating out all of the known-in sign bits, return the 2174193323Sed // number that are left. This handles rotl(sext(x), 1) for example. 2175193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2176193323Sed if (Tmp > RotAmt+1) return Tmp-RotAmt; 2177193323Sed } 2178193323Sed break; 2179193323Sed case ISD::ADD: 2180193323Sed // Add can have at most one carry bit. Thus we know that the output 2181193323Sed // is, at worst, one more bit than the inputs. 2182193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2183193323Sed if (Tmp == 1) return 1; // Early out. 2184193323Sed 2185193323Sed // Special case decrementing a value (ADD X, -1): 2186193323Sed if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 2187193323Sed if (CRHS->isAllOnesValue()) { 2188193323Sed APInt KnownZero, KnownOne; 2189193323Sed APInt Mask = APInt::getAllOnesValue(VTBits); 2190193323Sed ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 2191193323Sed 2192193323Sed // If the input is known to be 0 or 1, the output is 0/-1, which is all 2193193323Sed // sign bits set. 2194193323Sed if ((KnownZero | APInt(VTBits, 1)) == Mask) 2195193323Sed return VTBits; 2196193323Sed 2197193323Sed // If we are subtracting one from a positive number, there is no carry 2198193323Sed // out of the result. 2199193323Sed if (KnownZero.isNegative()) 2200193323Sed return Tmp; 2201193323Sed } 2202193323Sed 2203193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2204193323Sed if (Tmp2 == 1) return 1; 2205193323Sed return std::min(Tmp, Tmp2)-1; 2206193323Sed break; 2207193323Sed 2208193323Sed case ISD::SUB: 2209193323Sed Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2210193323Sed if (Tmp2 == 1) return 1; 2211193323Sed 2212193323Sed // Handle NEG. 2213193323Sed if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 2214193323Sed if (CLHS->isNullValue()) { 2215193323Sed APInt KnownZero, KnownOne; 2216193323Sed APInt Mask = APInt::getAllOnesValue(VTBits); 2217193323Sed ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 2218193323Sed // If the input is known to be 0 or 1, the output is 0/-1, which is all 2219193323Sed // sign bits set. 2220193323Sed if ((KnownZero | APInt(VTBits, 1)) == Mask) 2221193323Sed return VTBits; 2222193323Sed 2223193323Sed // If the input is known to be positive (the sign bit is known clear), 2224193323Sed // the output of the NEG has the same number of sign bits as the input. 2225193323Sed if (KnownZero.isNegative()) 2226193323Sed return Tmp2; 2227193323Sed 2228193323Sed // Otherwise, we treat this like a SUB. 2229193323Sed } 2230193323Sed 2231193323Sed // Sub can have at most one carry bit. Thus we know that the output 2232193323Sed // is, at worst, one more bit than the inputs. 2233193323Sed Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2234193323Sed if (Tmp == 1) return 1; // Early out. 2235193323Sed return std::min(Tmp, Tmp2)-1; 2236193323Sed break; 2237193323Sed case ISD::TRUNCATE: 2238193323Sed // FIXME: it's tricky to do anything useful for this, but it is an important 2239193323Sed // case for targets like X86. 2240193323Sed break; 2241193323Sed } 2242193323Sed 2243193323Sed // Handle LOADX separately here. EXTLOAD case will fallthrough. 2244193323Sed if (Op.getOpcode() == ISD::LOAD) { 2245193323Sed LoadSDNode *LD = cast<LoadSDNode>(Op); 2246193323Sed unsigned ExtType = LD->getExtensionType(); 2247193323Sed switch (ExtType) { 2248193323Sed default: break; 2249193323Sed case ISD::SEXTLOAD: // '17' bits known 2250202375Srdivacky Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); 2251193323Sed return VTBits-Tmp+1; 2252193323Sed case ISD::ZEXTLOAD: // '16' bits known 2253202375Srdivacky Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); 2254193323Sed return VTBits-Tmp; 2255193323Sed } 2256193323Sed } 2257193323Sed 2258193323Sed // Allow the target to implement this method for its nodes. 2259193323Sed if (Op.getOpcode() >= ISD::BUILTIN_OP_END || 2260193323Sed Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2261193323Sed Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2262193323Sed Op.getOpcode() == ISD::INTRINSIC_VOID) { 2263193323Sed unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth); 2264193323Sed if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits); 2265193323Sed } 2266193323Sed 2267193323Sed // Finally, if we can prove that the top bits of the result are 0's or 1's, 2268193323Sed // use this information. 2269193323Sed APInt KnownZero, KnownOne; 2270193323Sed APInt Mask = APInt::getAllOnesValue(VTBits); 2271193323Sed ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 2272193323Sed 2273193323Sed if (KnownZero.isNegative()) { // sign bit is 0 2274193323Sed Mask = KnownZero; 2275193323Sed } else if (KnownOne.isNegative()) { // sign bit is 1; 2276193323Sed Mask = KnownOne; 2277193323Sed } else { 2278193323Sed // Nothing known. 2279193323Sed return FirstAnswer; 2280193323Sed } 2281193323Sed 2282193323Sed // Okay, we know that the sign bit in Mask is set. Use CLZ to determine 2283193323Sed // the number of identical bits in the top of the input value. 2284193323Sed Mask = ~Mask; 2285193323Sed Mask <<= Mask.getBitWidth()-VTBits; 2286193323Sed // Return # leading zeros. We use 'min' here in case Val was zero before 2287193323Sed // shifting. We don't want to return '64' as for an i32 "0". 2288193323Sed return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros())); 2289193323Sed} 2290193323Sed 2291218893Sdim/// isBaseWithConstantOffset - Return true if the specified operand is an 2292218893Sdim/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an 2293218893Sdim/// ISD::OR with a ConstantSDNode that is guaranteed to have the same 2294218893Sdim/// semantics as an ADD. This handles the equivalence: 2295218893Sdim/// X|Cst == X+Cst iff X&Cst = 0. 2296218893Sdimbool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const { 2297218893Sdim if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) || 2298218893Sdim !isa<ConstantSDNode>(Op.getOperand(1))) 2299218893Sdim return false; 2300218893Sdim 2301218893Sdim if (Op.getOpcode() == ISD::OR && 2302218893Sdim !MaskedValueIsZero(Op.getOperand(0), 2303218893Sdim cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue())) 2304218893Sdim return false; 2305218893Sdim 2306218893Sdim return true; 2307218893Sdim} 2308218893Sdim 2309218893Sdim 2310198090Srdivackybool SelectionDAG::isKnownNeverNaN(SDValue Op) const { 2311198090Srdivacky // If we're told that NaNs won't happen, assume they won't. 2312212904Sdim if (NoNaNsFPMath) 2313198090Srdivacky return true; 2314193323Sed 2315198090Srdivacky // If the value is a constant, we can obviously see if it is a NaN or not. 2316198090Srdivacky if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 2317198090Srdivacky return !C->getValueAPF().isNaN(); 2318198090Srdivacky 2319198090Srdivacky // TODO: Recognize more cases here. 2320198090Srdivacky 2321198090Srdivacky return false; 2322198090Srdivacky} 2323198090Srdivacky 2324204642Srdivackybool SelectionDAG::isKnownNeverZero(SDValue Op) const { 2325204642Srdivacky // If the value is a constant, we can obviously see if it is a zero or not. 2326204642Srdivacky if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 2327204642Srdivacky return !C->isZero(); 2328204642Srdivacky 2329204642Srdivacky // TODO: Recognize more cases here. 2330204642Srdivacky 2331204642Srdivacky return false; 2332204642Srdivacky} 2333204642Srdivacky 2334204642Srdivackybool SelectionDAG::isEqualTo(SDValue A, SDValue B) const { 2335204642Srdivacky // Check the obvious case. 2336204642Srdivacky if (A == B) return true; 2337204642Srdivacky 2338204642Srdivacky // For for negative and positive zero. 2339204642Srdivacky if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) 2340204642Srdivacky if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) 2341204642Srdivacky if (CA->isZero() && CB->isZero()) return true; 2342204642Srdivacky 2343204642Srdivacky // Otherwise they may not be equal. 2344204642Srdivacky return false; 2345204642Srdivacky} 2346204642Srdivacky 2347193323Sedbool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const { 2348193323Sed GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); 2349193323Sed if (!GA) return false; 2350193323Sed if (GA->getOffset() != 0) return false; 2351207618Srdivacky const GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal()); 2352193323Sed if (!GV) return false; 2353206274Srdivacky return MF->getMMI().hasDebugInfo(); 2354193323Sed} 2355193323Sed 2356193323Sed 2357193323Sed/// getNode - Gets or creates the specified node. 2358193323Sed/// 2359198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) { 2360193323Sed FoldingSetNodeID ID; 2361193323Sed AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0); 2362193323Sed void *IP = 0; 2363201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2364193323Sed return SDValue(E, 0); 2365201360Srdivacky 2366205407Srdivacky SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT)); 2367193323Sed CSEMap.InsertNode(N, IP); 2368193323Sed 2369193323Sed AllNodes.push_back(N); 2370193323Sed#ifndef NDEBUG 2371218893Sdim VerifySDNode(N); 2372193323Sed#endif 2373193323Sed return SDValue(N, 0); 2374193323Sed} 2375193323Sed 2376193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, 2377198090Srdivacky EVT VT, SDValue Operand) { 2378193323Sed // Constant fold unary operations with an integer constant operand. 2379193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) { 2380193323Sed const APInt &Val = C->getAPIntValue(); 2381193323Sed switch (Opcode) { 2382193323Sed default: break; 2383193323Sed case ISD::SIGN_EXTEND: 2384218893Sdim return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT); 2385193323Sed case ISD::ANY_EXTEND: 2386193323Sed case ISD::ZERO_EXTEND: 2387193323Sed case ISD::TRUNCATE: 2388218893Sdim return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT); 2389193323Sed case ISD::UINT_TO_FP: 2390193323Sed case ISD::SINT_TO_FP: { 2391205218Srdivacky // No compile time operations on ppcf128. 2392205218Srdivacky if (VT == MVT::ppcf128) break; 2393218893Sdim APFloat apf(APInt::getNullValue(VT.getSizeInBits())); 2394193323Sed (void)apf.convertFromAPInt(Val, 2395193323Sed Opcode==ISD::SINT_TO_FP, 2396193323Sed APFloat::rmNearestTiesToEven); 2397193323Sed return getConstantFP(apf, VT); 2398193323Sed } 2399218893Sdim case ISD::BITCAST: 2400193323Sed if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) 2401193323Sed return getConstantFP(Val.bitsToFloat(), VT); 2402193323Sed else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) 2403193323Sed return getConstantFP(Val.bitsToDouble(), VT); 2404193323Sed break; 2405193323Sed case ISD::BSWAP: 2406193323Sed return getConstant(Val.byteSwap(), VT); 2407193323Sed case ISD::CTPOP: 2408193323Sed return getConstant(Val.countPopulation(), VT); 2409193323Sed case ISD::CTLZ: 2410193323Sed return getConstant(Val.countLeadingZeros(), VT); 2411193323Sed case ISD::CTTZ: 2412193323Sed return getConstant(Val.countTrailingZeros(), VT); 2413193323Sed } 2414193323Sed } 2415193323Sed 2416193323Sed // Constant fold unary operations with a floating point constant operand. 2417193323Sed if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) { 2418193323Sed APFloat V = C->getValueAPF(); // make copy 2419193323Sed if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) { 2420193323Sed switch (Opcode) { 2421193323Sed case ISD::FNEG: 2422193323Sed V.changeSign(); 2423193323Sed return getConstantFP(V, VT); 2424193323Sed case ISD::FABS: 2425193323Sed V.clearSign(); 2426193323Sed return getConstantFP(V, VT); 2427193323Sed case ISD::FP_ROUND: 2428193323Sed case ISD::FP_EXTEND: { 2429193323Sed bool ignored; 2430193323Sed // This can return overflow, underflow, or inexact; we don't care. 2431193323Sed // FIXME need to be more flexible about rounding mode. 2432198090Srdivacky (void)V.convert(*EVTToAPFloatSemantics(VT), 2433193323Sed APFloat::rmNearestTiesToEven, &ignored); 2434193323Sed return getConstantFP(V, VT); 2435193323Sed } 2436193323Sed case ISD::FP_TO_SINT: 2437193323Sed case ISD::FP_TO_UINT: { 2438193323Sed integerPart x[2]; 2439193323Sed bool ignored; 2440193323Sed assert(integerPartWidth >= 64); 2441193323Sed // FIXME need to be more flexible about rounding mode. 2442193323Sed APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(), 2443193323Sed Opcode==ISD::FP_TO_SINT, 2444193323Sed APFloat::rmTowardZero, &ignored); 2445193323Sed if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual 2446193323Sed break; 2447193323Sed APInt api(VT.getSizeInBits(), 2, x); 2448193323Sed return getConstant(api, VT); 2449193323Sed } 2450218893Sdim case ISD::BITCAST: 2451193323Sed if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) 2452193323Sed return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT); 2453193323Sed else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) 2454193323Sed return getConstant(V.bitcastToAPInt().getZExtValue(), VT); 2455193323Sed break; 2456193323Sed } 2457193323Sed } 2458193323Sed } 2459193323Sed 2460193323Sed unsigned OpOpcode = Operand.getNode()->getOpcode(); 2461193323Sed switch (Opcode) { 2462193323Sed case ISD::TokenFactor: 2463193323Sed case ISD::MERGE_VALUES: 2464193323Sed case ISD::CONCAT_VECTORS: 2465193323Sed return Operand; // Factor, merge or concat of one node? No need. 2466198090Srdivacky case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node"); 2467193323Sed case ISD::FP_EXTEND: 2468193323Sed assert(VT.isFloatingPoint() && 2469193323Sed Operand.getValueType().isFloatingPoint() && "Invalid FP cast!"); 2470193323Sed if (Operand.getValueType() == VT) return Operand; // noop conversion. 2471200581Srdivacky assert((!VT.isVector() || 2472200581Srdivacky VT.getVectorNumElements() == 2473200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2474200581Srdivacky "Vector element count mismatch!"); 2475193323Sed if (Operand.getOpcode() == ISD::UNDEF) 2476193323Sed return getUNDEF(VT); 2477193323Sed break; 2478193323Sed case ISD::SIGN_EXTEND: 2479193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2480193323Sed "Invalid SIGN_EXTEND!"); 2481193323Sed if (Operand.getValueType() == VT) return Operand; // noop extension 2482200581Srdivacky assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2483200581Srdivacky "Invalid sext node, dst < src!"); 2484200581Srdivacky assert((!VT.isVector() || 2485200581Srdivacky VT.getVectorNumElements() == 2486200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2487200581Srdivacky "Vector element count mismatch!"); 2488193323Sed if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 2489193323Sed return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2490193323Sed break; 2491193323Sed case ISD::ZERO_EXTEND: 2492193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2493193323Sed "Invalid ZERO_EXTEND!"); 2494193323Sed if (Operand.getValueType() == VT) return Operand; // noop extension 2495200581Srdivacky assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2496200581Srdivacky "Invalid zext node, dst < src!"); 2497200581Srdivacky assert((!VT.isVector() || 2498200581Srdivacky VT.getVectorNumElements() == 2499200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2500200581Srdivacky "Vector element count mismatch!"); 2501193323Sed if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 2502193323Sed return getNode(ISD::ZERO_EXTEND, DL, VT, 2503193323Sed Operand.getNode()->getOperand(0)); 2504193323Sed break; 2505193323Sed case ISD::ANY_EXTEND: 2506193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2507193323Sed "Invalid ANY_EXTEND!"); 2508193323Sed if (Operand.getValueType() == VT) return Operand; // noop extension 2509200581Srdivacky assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2510200581Srdivacky "Invalid anyext node, dst < src!"); 2511200581Srdivacky assert((!VT.isVector() || 2512200581Srdivacky VT.getVectorNumElements() == 2513200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2514200581Srdivacky "Vector element count mismatch!"); 2515210299Sed 2516210299Sed if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 2517210299Sed OpOpcode == ISD::ANY_EXTEND) 2518193323Sed // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 2519193323Sed return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2520210299Sed 2521210299Sed // (ext (trunx x)) -> x 2522210299Sed if (OpOpcode == ISD::TRUNCATE) { 2523210299Sed SDValue OpOp = Operand.getNode()->getOperand(0); 2524210299Sed if (OpOp.getValueType() == VT) 2525210299Sed return OpOp; 2526210299Sed } 2527193323Sed break; 2528193323Sed case ISD::TRUNCATE: 2529193323Sed assert(VT.isInteger() && Operand.getValueType().isInteger() && 2530193323Sed "Invalid TRUNCATE!"); 2531193323Sed if (Operand.getValueType() == VT) return Operand; // noop truncate 2532200581Srdivacky assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) && 2533200581Srdivacky "Invalid truncate node, src < dst!"); 2534200581Srdivacky assert((!VT.isVector() || 2535200581Srdivacky VT.getVectorNumElements() == 2536200581Srdivacky Operand.getValueType().getVectorNumElements()) && 2537200581Srdivacky "Vector element count mismatch!"); 2538193323Sed if (OpOpcode == ISD::TRUNCATE) 2539193323Sed return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); 2540193323Sed else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 2541193323Sed OpOpcode == ISD::ANY_EXTEND) { 2542193323Sed // If the source is smaller than the dest, we still need an extend. 2543200581Srdivacky if (Operand.getNode()->getOperand(0).getValueType().getScalarType() 2544200581Srdivacky .bitsLT(VT.getScalarType())) 2545193323Sed return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2546193323Sed else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT)) 2547193323Sed return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); 2548193323Sed else 2549193323Sed return Operand.getNode()->getOperand(0); 2550193323Sed } 2551193323Sed break; 2552218893Sdim case ISD::BITCAST: 2553193323Sed // Basic sanity checking. 2554193323Sed assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits() 2555218893Sdim && "Cannot BITCAST between types of different sizes!"); 2556193323Sed if (VT == Operand.getValueType()) return Operand; // noop conversion. 2557218893Sdim if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x) 2558218893Sdim return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0)); 2559193323Sed if (OpOpcode == ISD::UNDEF) 2560193323Sed return getUNDEF(VT); 2561193323Sed break; 2562193323Sed case ISD::SCALAR_TO_VECTOR: 2563193323Sed assert(VT.isVector() && !Operand.getValueType().isVector() && 2564193323Sed (VT.getVectorElementType() == Operand.getValueType() || 2565193323Sed (VT.getVectorElementType().isInteger() && 2566193323Sed Operand.getValueType().isInteger() && 2567193323Sed VT.getVectorElementType().bitsLE(Operand.getValueType()))) && 2568193323Sed "Illegal SCALAR_TO_VECTOR node!"); 2569193323Sed if (OpOpcode == ISD::UNDEF) 2570193323Sed return getUNDEF(VT); 2571193323Sed // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined. 2572193323Sed if (OpOpcode == ISD::EXTRACT_VECTOR_ELT && 2573193323Sed isa<ConstantSDNode>(Operand.getOperand(1)) && 2574193323Sed Operand.getConstantOperandVal(1) == 0 && 2575193323Sed Operand.getOperand(0).getValueType() == VT) 2576193323Sed return Operand.getOperand(0); 2577193323Sed break; 2578193323Sed case ISD::FNEG: 2579193323Sed // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0 2580193323Sed if (UnsafeFPMath && OpOpcode == ISD::FSUB) 2581193323Sed return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1), 2582193323Sed Operand.getNode()->getOperand(0)); 2583193323Sed if (OpOpcode == ISD::FNEG) // --X -> X 2584193323Sed return Operand.getNode()->getOperand(0); 2585193323Sed break; 2586193323Sed case ISD::FABS: 2587193323Sed if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 2588193323Sed return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0)); 2589193323Sed break; 2590193323Sed } 2591193323Sed 2592193323Sed SDNode *N; 2593193323Sed SDVTList VTs = getVTList(VT); 2594218893Sdim if (VT != MVT::Glue) { // Don't CSE flag producing nodes 2595193323Sed FoldingSetNodeID ID; 2596193323Sed SDValue Ops[1] = { Operand }; 2597193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 1); 2598193323Sed void *IP = 0; 2599201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2600193323Sed return SDValue(E, 0); 2601201360Srdivacky 2602205407Srdivacky N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand); 2603193323Sed CSEMap.InsertNode(N, IP); 2604193323Sed } else { 2605205407Srdivacky N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand); 2606193323Sed } 2607193323Sed 2608193323Sed AllNodes.push_back(N); 2609193323Sed#ifndef NDEBUG 2610218893Sdim VerifySDNode(N); 2611193323Sed#endif 2612193323Sed return SDValue(N, 0); 2613193323Sed} 2614193323Sed 2615193323SedSDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, 2616198090Srdivacky EVT VT, 2617193323Sed ConstantSDNode *Cst1, 2618193323Sed ConstantSDNode *Cst2) { 2619193323Sed const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue(); 2620193323Sed 2621193323Sed switch (Opcode) { 2622193323Sed case ISD::ADD: return getConstant(C1 + C2, VT); 2623193323Sed case ISD::SUB: return getConstant(C1 - C2, VT); 2624193323Sed case ISD::MUL: return getConstant(C1 * C2, VT); 2625193323Sed case ISD::UDIV: 2626193323Sed if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT); 2627193323Sed break; 2628193323Sed case ISD::UREM: 2629193323Sed if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT); 2630193323Sed break; 2631193323Sed case ISD::SDIV: 2632193323Sed if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT); 2633193323Sed break; 2634193323Sed case ISD::SREM: 2635193323Sed if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT); 2636193323Sed break; 2637193323Sed case ISD::AND: return getConstant(C1 & C2, VT); 2638193323Sed case ISD::OR: return getConstant(C1 | C2, VT); 2639193323Sed case ISD::XOR: return getConstant(C1 ^ C2, VT); 2640193323Sed case ISD::SHL: return getConstant(C1 << C2, VT); 2641193323Sed case ISD::SRL: return getConstant(C1.lshr(C2), VT); 2642193323Sed case ISD::SRA: return getConstant(C1.ashr(C2), VT); 2643193323Sed case ISD::ROTL: return getConstant(C1.rotl(C2), VT); 2644193323Sed case ISD::ROTR: return getConstant(C1.rotr(C2), VT); 2645193323Sed default: break; 2646193323Sed } 2647193323Sed 2648193323Sed return SDValue(); 2649193323Sed} 2650193323Sed 2651198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 2652193323Sed SDValue N1, SDValue N2) { 2653193323Sed ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 2654193323Sed ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); 2655193323Sed switch (Opcode) { 2656193323Sed default: break; 2657193323Sed case ISD::TokenFactor: 2658193323Sed assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 2659193323Sed N2.getValueType() == MVT::Other && "Invalid token factor!"); 2660193323Sed // Fold trivial token factors. 2661193323Sed if (N1.getOpcode() == ISD::EntryToken) return N2; 2662193323Sed if (N2.getOpcode() == ISD::EntryToken) return N1; 2663193323Sed if (N1 == N2) return N1; 2664193323Sed break; 2665193323Sed case ISD::CONCAT_VECTORS: 2666193323Sed // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to 2667193323Sed // one big BUILD_VECTOR. 2668193323Sed if (N1.getOpcode() == ISD::BUILD_VECTOR && 2669193323Sed N2.getOpcode() == ISD::BUILD_VECTOR) { 2670212904Sdim SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), 2671212904Sdim N1.getNode()->op_end()); 2672210299Sed Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end()); 2673193323Sed return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size()); 2674193323Sed } 2675193323Sed break; 2676193323Sed case ISD::AND: 2677208599Srdivacky assert(VT.isInteger() && "This operator does not apply to FP types!"); 2678208599Srdivacky assert(N1.getValueType() == N2.getValueType() && 2679193323Sed N1.getValueType() == VT && "Binary operator types must match!"); 2680193323Sed // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's 2681193323Sed // worth handling here. 2682193323Sed if (N2C && N2C->isNullValue()) 2683193323Sed return N2; 2684193323Sed if (N2C && N2C->isAllOnesValue()) // X & -1 -> X 2685193323Sed return N1; 2686193323Sed break; 2687193323Sed case ISD::OR: 2688193323Sed case ISD::XOR: 2689193323Sed case ISD::ADD: 2690193323Sed case ISD::SUB: 2691208599Srdivacky assert(VT.isInteger() && "This operator does not apply to FP types!"); 2692208599Srdivacky assert(N1.getValueType() == N2.getValueType() && 2693193323Sed N1.getValueType() == VT && "Binary operator types must match!"); 2694193323Sed // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so 2695193323Sed // it's worth handling here. 2696193323Sed if (N2C && N2C->isNullValue()) 2697193323Sed return N1; 2698193323Sed break; 2699193323Sed case ISD::UDIV: 2700193323Sed case ISD::UREM: 2701193323Sed case ISD::MULHU: 2702193323Sed case ISD::MULHS: 2703193323Sed case ISD::MUL: 2704193323Sed case ISD::SDIV: 2705193323Sed case ISD::SREM: 2706193323Sed assert(VT.isInteger() && "This operator does not apply to FP types!"); 2707208599Srdivacky assert(N1.getValueType() == N2.getValueType() && 2708208599Srdivacky N1.getValueType() == VT && "Binary operator types must match!"); 2709208599Srdivacky break; 2710193323Sed case ISD::FADD: 2711193323Sed case ISD::FSUB: 2712193323Sed case ISD::FMUL: 2713193323Sed case ISD::FDIV: 2714193323Sed case ISD::FREM: 2715193323Sed if (UnsafeFPMath) { 2716193323Sed if (Opcode == ISD::FADD) { 2717193323Sed // 0+x --> x 2718193323Sed if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) 2719193323Sed if (CFP->getValueAPF().isZero()) 2720193323Sed return N2; 2721193323Sed // x+0 --> x 2722193323Sed if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) 2723193323Sed if (CFP->getValueAPF().isZero()) 2724193323Sed return N1; 2725193323Sed } else if (Opcode == ISD::FSUB) { 2726193323Sed // x-0 --> x 2727193323Sed if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) 2728193323Sed if (CFP->getValueAPF().isZero()) 2729193323Sed return N1; 2730193323Sed } 2731193323Sed } 2732208599Srdivacky assert(VT.isFloatingPoint() && "This operator only applies to FP types!"); 2733193323Sed assert(N1.getValueType() == N2.getValueType() && 2734193323Sed N1.getValueType() == VT && "Binary operator types must match!"); 2735193323Sed break; 2736193323Sed case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. 2737193323Sed assert(N1.getValueType() == VT && 2738193323Sed N1.getValueType().isFloatingPoint() && 2739193323Sed N2.getValueType().isFloatingPoint() && 2740193323Sed "Invalid FCOPYSIGN!"); 2741193323Sed break; 2742193323Sed case ISD::SHL: 2743193323Sed case ISD::SRA: 2744193323Sed case ISD::SRL: 2745193323Sed case ISD::ROTL: 2746193323Sed case ISD::ROTR: 2747193323Sed assert(VT == N1.getValueType() && 2748193323Sed "Shift operators return type must be the same as their first arg"); 2749193323Sed assert(VT.isInteger() && N2.getValueType().isInteger() && 2750193323Sed "Shifts only work on integers"); 2751218893Sdim // Verify that the shift amount VT is bit enough to hold valid shift 2752218893Sdim // amounts. This catches things like trying to shift an i1024 value by an 2753218893Sdim // i8, which is easy to fall into in generic code that uses 2754218893Sdim // TLI.getShiftAmount(). 2755218893Sdim assert(N2.getValueType().getSizeInBits() >= 2756218893Sdim Log2_32_Ceil(N1.getValueType().getSizeInBits()) && 2757218893Sdim "Invalid use of small shift amount with oversized value!"); 2758193323Sed 2759193323Sed // Always fold shifts of i1 values so the code generator doesn't need to 2760193323Sed // handle them. Since we know the size of the shift has to be less than the 2761193323Sed // size of the value, the shift/rotate count is guaranteed to be zero. 2762193323Sed if (VT == MVT::i1) 2763193323Sed return N1; 2764202375Srdivacky if (N2C && N2C->isNullValue()) 2765202375Srdivacky return N1; 2766193323Sed break; 2767193323Sed case ISD::FP_ROUND_INREG: { 2768198090Srdivacky EVT EVT = cast<VTSDNode>(N2)->getVT(); 2769193323Sed assert(VT == N1.getValueType() && "Not an inreg round!"); 2770193323Sed assert(VT.isFloatingPoint() && EVT.isFloatingPoint() && 2771193323Sed "Cannot FP_ROUND_INREG integer types"); 2772202375Srdivacky assert(EVT.isVector() == VT.isVector() && 2773202375Srdivacky "FP_ROUND_INREG type should be vector iff the operand " 2774202375Srdivacky "type is vector!"); 2775202375Srdivacky assert((!EVT.isVector() || 2776202375Srdivacky EVT.getVectorNumElements() == VT.getVectorNumElements()) && 2777202375Srdivacky "Vector element counts must match in FP_ROUND_INREG"); 2778193323Sed assert(EVT.bitsLE(VT) && "Not rounding down!"); 2779193323Sed if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 2780193323Sed break; 2781193323Sed } 2782193323Sed case ISD::FP_ROUND: 2783193323Sed assert(VT.isFloatingPoint() && 2784193323Sed N1.getValueType().isFloatingPoint() && 2785193323Sed VT.bitsLE(N1.getValueType()) && 2786193323Sed isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!"); 2787193323Sed if (N1.getValueType() == VT) return N1; // noop conversion. 2788193323Sed break; 2789193323Sed case ISD::AssertSext: 2790193323Sed case ISD::AssertZext: { 2791198090Srdivacky EVT EVT = cast<VTSDNode>(N2)->getVT(); 2792193323Sed assert(VT == N1.getValueType() && "Not an inreg extend!"); 2793193323Sed assert(VT.isInteger() && EVT.isInteger() && 2794193323Sed "Cannot *_EXTEND_INREG FP types"); 2795200581Srdivacky assert(!EVT.isVector() && 2796200581Srdivacky "AssertSExt/AssertZExt type should be the vector element type " 2797200581Srdivacky "rather than the vector type!"); 2798193323Sed assert(EVT.bitsLE(VT) && "Not extending!"); 2799193323Sed if (VT == EVT) return N1; // noop assertion. 2800193323Sed break; 2801193323Sed } 2802193323Sed case ISD::SIGN_EXTEND_INREG: { 2803198090Srdivacky EVT EVT = cast<VTSDNode>(N2)->getVT(); 2804193323Sed assert(VT == N1.getValueType() && "Not an inreg extend!"); 2805193323Sed assert(VT.isInteger() && EVT.isInteger() && 2806193323Sed "Cannot *_EXTEND_INREG FP types"); 2807202375Srdivacky assert(EVT.isVector() == VT.isVector() && 2808202375Srdivacky "SIGN_EXTEND_INREG type should be vector iff the operand " 2809202375Srdivacky "type is vector!"); 2810202375Srdivacky assert((!EVT.isVector() || 2811202375Srdivacky EVT.getVectorNumElements() == VT.getVectorNumElements()) && 2812202375Srdivacky "Vector element counts must match in SIGN_EXTEND_INREG"); 2813202375Srdivacky assert(EVT.bitsLE(VT) && "Not extending!"); 2814193323Sed if (EVT == VT) return N1; // Not actually extending 2815193323Sed 2816193323Sed if (N1C) { 2817193323Sed APInt Val = N1C->getAPIntValue(); 2818202375Srdivacky unsigned FromBits = EVT.getScalarType().getSizeInBits(); 2819193323Sed Val <<= Val.getBitWidth()-FromBits; 2820193323Sed Val = Val.ashr(Val.getBitWidth()-FromBits); 2821193323Sed return getConstant(Val, VT); 2822193323Sed } 2823193323Sed break; 2824193323Sed } 2825193323Sed case ISD::EXTRACT_VECTOR_ELT: 2826193323Sed // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF. 2827193323Sed if (N1.getOpcode() == ISD::UNDEF) 2828193323Sed return getUNDEF(VT); 2829193323Sed 2830193323Sed // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is 2831193323Sed // expanding copies of large vectors from registers. 2832193323Sed if (N2C && 2833193323Sed N1.getOpcode() == ISD::CONCAT_VECTORS && 2834193323Sed N1.getNumOperands() > 0) { 2835193323Sed unsigned Factor = 2836193323Sed N1.getOperand(0).getValueType().getVectorNumElements(); 2837193323Sed return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, 2838193323Sed N1.getOperand(N2C->getZExtValue() / Factor), 2839193323Sed getConstant(N2C->getZExtValue() % Factor, 2840193323Sed N2.getValueType())); 2841193323Sed } 2842193323Sed 2843193323Sed // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is 2844193323Sed // expanding large vector constants. 2845193323Sed if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) { 2846193323Sed SDValue Elt = N1.getOperand(N2C->getZExtValue()); 2847198090Srdivacky EVT VEltTy = N1.getValueType().getVectorElementType(); 2848198090Srdivacky if (Elt.getValueType() != VEltTy) { 2849193323Sed // If the vector element type is not legal, the BUILD_VECTOR operands 2850193323Sed // are promoted and implicitly truncated. Make that explicit here. 2851198090Srdivacky Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt); 2852193323Sed } 2853198090Srdivacky if (VT != VEltTy) { 2854198090Srdivacky // If the vector element type is not legal, the EXTRACT_VECTOR_ELT 2855198090Srdivacky // result is implicitly extended. 2856198090Srdivacky Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt); 2857198090Srdivacky } 2858193323Sed return Elt; 2859193323Sed } 2860193323Sed 2861193323Sed // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector 2862193323Sed // operations are lowered to scalars. 2863193323Sed if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) { 2864203954Srdivacky // If the indices are the same, return the inserted element else 2865203954Srdivacky // if the indices are known different, extract the element from 2866193323Sed // the original vector. 2867207618Srdivacky SDValue N1Op2 = N1.getOperand(2); 2868207618Srdivacky ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode()); 2869207618Srdivacky 2870207618Srdivacky if (N1Op2C && N2C) { 2871207618Srdivacky if (N1Op2C->getZExtValue() == N2C->getZExtValue()) { 2872207618Srdivacky if (VT == N1.getOperand(1).getValueType()) 2873207618Srdivacky return N1.getOperand(1); 2874207618Srdivacky else 2875207618Srdivacky return getSExtOrTrunc(N1.getOperand(1), DL, VT); 2876207618Srdivacky } 2877207618Srdivacky 2878193323Sed return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2); 2879207618Srdivacky } 2880193323Sed } 2881193323Sed break; 2882193323Sed case ISD::EXTRACT_ELEMENT: 2883193323Sed assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!"); 2884193323Sed assert(!N1.getValueType().isVector() && !VT.isVector() && 2885193323Sed (N1.getValueType().isInteger() == VT.isInteger()) && 2886193323Sed "Wrong types for EXTRACT_ELEMENT!"); 2887193323Sed 2888193323Sed // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding 2889193323Sed // 64-bit integers into 32-bit parts. Instead of building the extract of 2890193323Sed // the BUILD_PAIR, only to have legalize rip it apart, just do it now. 2891193323Sed if (N1.getOpcode() == ISD::BUILD_PAIR) 2892193323Sed return N1.getOperand(N2C->getZExtValue()); 2893193323Sed 2894193323Sed // EXTRACT_ELEMENT of a constant int is also very common. 2895193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 2896193323Sed unsigned ElementSize = VT.getSizeInBits(); 2897193323Sed unsigned Shift = ElementSize * N2C->getZExtValue(); 2898193323Sed APInt ShiftedVal = C->getAPIntValue().lshr(Shift); 2899193323Sed return getConstant(ShiftedVal.trunc(ElementSize), VT); 2900193323Sed } 2901193323Sed break; 2902218893Sdim case ISD::EXTRACT_SUBVECTOR: { 2903218893Sdim SDValue Index = N2; 2904218893Sdim if (VT.isSimple() && N1.getValueType().isSimple()) { 2905218893Sdim assert(VT.isVector() && N1.getValueType().isVector() && 2906218893Sdim "Extract subvector VTs must be a vectors!"); 2907218893Sdim assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() && 2908218893Sdim "Extract subvector VTs must have the same element type!"); 2909218893Sdim assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() && 2910218893Sdim "Extract subvector must be from larger vector to smaller vector!"); 2911218893Sdim 2912218893Sdim if (isa<ConstantSDNode>(Index.getNode())) { 2913218893Sdim assert((VT.getVectorNumElements() + 2914218893Sdim cast<ConstantSDNode>(Index.getNode())->getZExtValue() 2915218893Sdim <= N1.getValueType().getVectorNumElements()) 2916218893Sdim && "Extract subvector overflow!"); 2917218893Sdim } 2918218893Sdim 2919218893Sdim // Trivial extraction. 2920218893Sdim if (VT.getSimpleVT() == N1.getValueType().getSimpleVT()) 2921218893Sdim return N1; 2922218893Sdim } 2923193323Sed break; 2924193323Sed } 2925218893Sdim } 2926193323Sed 2927193323Sed if (N1C) { 2928193323Sed if (N2C) { 2929193323Sed SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C); 2930193323Sed if (SV.getNode()) return SV; 2931193323Sed } else { // Cannonicalize constant to RHS if commutative 2932193323Sed if (isCommutativeBinOp(Opcode)) { 2933193323Sed std::swap(N1C, N2C); 2934193323Sed std::swap(N1, N2); 2935193323Sed } 2936193323Sed } 2937193323Sed } 2938193323Sed 2939193323Sed // Constant fold FP operations. 2940193323Sed ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode()); 2941193323Sed ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode()); 2942193323Sed if (N1CFP) { 2943193323Sed if (!N2CFP && isCommutativeBinOp(Opcode)) { 2944193323Sed // Cannonicalize constant to RHS if commutative 2945193323Sed std::swap(N1CFP, N2CFP); 2946193323Sed std::swap(N1, N2); 2947193323Sed } else if (N2CFP && VT != MVT::ppcf128) { 2948193323Sed APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF(); 2949193323Sed APFloat::opStatus s; 2950193323Sed switch (Opcode) { 2951193323Sed case ISD::FADD: 2952193323Sed s = V1.add(V2, APFloat::rmNearestTiesToEven); 2953193323Sed if (s != APFloat::opInvalidOp) 2954193323Sed return getConstantFP(V1, VT); 2955193323Sed break; 2956193323Sed case ISD::FSUB: 2957193323Sed s = V1.subtract(V2, APFloat::rmNearestTiesToEven); 2958193323Sed if (s!=APFloat::opInvalidOp) 2959193323Sed return getConstantFP(V1, VT); 2960193323Sed break; 2961193323Sed case ISD::FMUL: 2962193323Sed s = V1.multiply(V2, APFloat::rmNearestTiesToEven); 2963193323Sed if (s!=APFloat::opInvalidOp) 2964193323Sed return getConstantFP(V1, VT); 2965193323Sed break; 2966193323Sed case ISD::FDIV: 2967193323Sed s = V1.divide(V2, APFloat::rmNearestTiesToEven); 2968193323Sed if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 2969193323Sed return getConstantFP(V1, VT); 2970193323Sed break; 2971193323Sed case ISD::FREM : 2972193323Sed s = V1.mod(V2, APFloat::rmNearestTiesToEven); 2973193323Sed if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 2974193323Sed return getConstantFP(V1, VT); 2975193323Sed break; 2976193323Sed case ISD::FCOPYSIGN: 2977193323Sed V1.copySign(V2); 2978193323Sed return getConstantFP(V1, VT); 2979193323Sed default: break; 2980193323Sed } 2981193323Sed } 2982193323Sed } 2983193323Sed 2984193323Sed // Canonicalize an UNDEF to the RHS, even over a constant. 2985193323Sed if (N1.getOpcode() == ISD::UNDEF) { 2986193323Sed if (isCommutativeBinOp(Opcode)) { 2987193323Sed std::swap(N1, N2); 2988193323Sed } else { 2989193323Sed switch (Opcode) { 2990193323Sed case ISD::FP_ROUND_INREG: 2991193323Sed case ISD::SIGN_EXTEND_INREG: 2992193323Sed case ISD::SUB: 2993193323Sed case ISD::FSUB: 2994193323Sed case ISD::FDIV: 2995193323Sed case ISD::FREM: 2996193323Sed case ISD::SRA: 2997193323Sed return N1; // fold op(undef, arg2) -> undef 2998193323Sed case ISD::UDIV: 2999193323Sed case ISD::SDIV: 3000193323Sed case ISD::UREM: 3001193323Sed case ISD::SREM: 3002193323Sed case ISD::SRL: 3003193323Sed case ISD::SHL: 3004193323Sed if (!VT.isVector()) 3005193323Sed return getConstant(0, VT); // fold op(undef, arg2) -> 0 3006193323Sed // For vectors, we can't easily build an all zero vector, just return 3007193323Sed // the LHS. 3008193323Sed return N2; 3009193323Sed } 3010193323Sed } 3011193323Sed } 3012193323Sed 3013193323Sed // Fold a bunch of operators when the RHS is undef. 3014193323Sed if (N2.getOpcode() == ISD::UNDEF) { 3015193323Sed switch (Opcode) { 3016193323Sed case ISD::XOR: 3017193323Sed if (N1.getOpcode() == ISD::UNDEF) 3018193323Sed // Handle undef ^ undef -> 0 special case. This is a common 3019193323Sed // idiom (misuse). 3020193323Sed return getConstant(0, VT); 3021193323Sed // fallthrough 3022193323Sed case ISD::ADD: 3023193323Sed case ISD::ADDC: 3024193323Sed case ISD::ADDE: 3025193323Sed case ISD::SUB: 3026193574Sed case ISD::UDIV: 3027193574Sed case ISD::SDIV: 3028193574Sed case ISD::UREM: 3029193574Sed case ISD::SREM: 3030193574Sed return N2; // fold op(arg1, undef) -> undef 3031193323Sed case ISD::FADD: 3032193323Sed case ISD::FSUB: 3033193323Sed case ISD::FMUL: 3034193323Sed case ISD::FDIV: 3035193323Sed case ISD::FREM: 3036193574Sed if (UnsafeFPMath) 3037193574Sed return N2; 3038193574Sed break; 3039193323Sed case ISD::MUL: 3040193323Sed case ISD::AND: 3041193323Sed case ISD::SRL: 3042193323Sed case ISD::SHL: 3043193323Sed if (!VT.isVector()) 3044193323Sed return getConstant(0, VT); // fold op(arg1, undef) -> 0 3045193323Sed // For vectors, we can't easily build an all zero vector, just return 3046193323Sed // the LHS. 3047193323Sed return N1; 3048193323Sed case ISD::OR: 3049193323Sed if (!VT.isVector()) 3050193323Sed return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT); 3051193323Sed // For vectors, we can't easily build an all one vector, just return 3052193323Sed // the LHS. 3053193323Sed return N1; 3054193323Sed case ISD::SRA: 3055193323Sed return N1; 3056193323Sed } 3057193323Sed } 3058193323Sed 3059193323Sed // Memoize this node if possible. 3060193323Sed SDNode *N; 3061193323Sed SDVTList VTs = getVTList(VT); 3062218893Sdim if (VT != MVT::Glue) { 3063193323Sed SDValue Ops[] = { N1, N2 }; 3064193323Sed FoldingSetNodeID ID; 3065193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 2); 3066193323Sed void *IP = 0; 3067201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 3068193323Sed return SDValue(E, 0); 3069201360Srdivacky 3070205407Srdivacky N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2); 3071193323Sed CSEMap.InsertNode(N, IP); 3072193323Sed } else { 3073205407Srdivacky N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2); 3074193323Sed } 3075193323Sed 3076193323Sed AllNodes.push_back(N); 3077193323Sed#ifndef NDEBUG 3078218893Sdim VerifySDNode(N); 3079193323Sed#endif 3080193323Sed return SDValue(N, 0); 3081193323Sed} 3082193323Sed 3083198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 3084193323Sed SDValue N1, SDValue N2, SDValue N3) { 3085193323Sed // Perform various simplifications. 3086193323Sed ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 3087193323Sed switch (Opcode) { 3088193323Sed case ISD::CONCAT_VECTORS: 3089193323Sed // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to 3090193323Sed // one big BUILD_VECTOR. 3091193323Sed if (N1.getOpcode() == ISD::BUILD_VECTOR && 3092193323Sed N2.getOpcode() == ISD::BUILD_VECTOR && 3093193323Sed N3.getOpcode() == ISD::BUILD_VECTOR) { 3094212904Sdim SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), 3095212904Sdim N1.getNode()->op_end()); 3096210299Sed Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end()); 3097210299Sed Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end()); 3098193323Sed return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size()); 3099193323Sed } 3100193323Sed break; 3101193323Sed case ISD::SETCC: { 3102193323Sed // Use FoldSetCC to simplify SETCC's. 3103193323Sed SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL); 3104193323Sed if (Simp.getNode()) return Simp; 3105193323Sed break; 3106193323Sed } 3107193323Sed case ISD::SELECT: 3108193323Sed if (N1C) { 3109193323Sed if (N1C->getZExtValue()) 3110193323Sed return N2; // select true, X, Y -> X 3111193323Sed else 3112193323Sed return N3; // select false, X, Y -> Y 3113193323Sed } 3114193323Sed 3115193323Sed if (N2 == N3) return N2; // select C, X, X -> X 3116193323Sed break; 3117193323Sed case ISD::VECTOR_SHUFFLE: 3118198090Srdivacky llvm_unreachable("should use getVectorShuffle constructor!"); 3119193323Sed break; 3120218893Sdim case ISD::INSERT_SUBVECTOR: { 3121218893Sdim SDValue Index = N3; 3122218893Sdim if (VT.isSimple() && N1.getValueType().isSimple() 3123218893Sdim && N2.getValueType().isSimple()) { 3124218893Sdim assert(VT.isVector() && N1.getValueType().isVector() && 3125218893Sdim N2.getValueType().isVector() && 3126218893Sdim "Insert subvector VTs must be a vectors"); 3127218893Sdim assert(VT == N1.getValueType() && 3128218893Sdim "Dest and insert subvector source types must match!"); 3129218893Sdim assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() && 3130218893Sdim "Insert subvector must be from smaller vector to larger vector!"); 3131218893Sdim if (isa<ConstantSDNode>(Index.getNode())) { 3132218893Sdim assert((N2.getValueType().getVectorNumElements() + 3133218893Sdim cast<ConstantSDNode>(Index.getNode())->getZExtValue() 3134218893Sdim <= VT.getVectorNumElements()) 3135218893Sdim && "Insert subvector overflow!"); 3136218893Sdim } 3137218893Sdim 3138218893Sdim // Trivial insertion. 3139218893Sdim if (VT.getSimpleVT() == N2.getValueType().getSimpleVT()) 3140218893Sdim return N2; 3141218893Sdim } 3142218893Sdim break; 3143218893Sdim } 3144218893Sdim case ISD::BITCAST: 3145193323Sed // Fold bit_convert nodes from a type to themselves. 3146193323Sed if (N1.getValueType() == VT) 3147193323Sed return N1; 3148193323Sed break; 3149193323Sed } 3150193323Sed 3151193323Sed // Memoize node if it doesn't produce a flag. 3152193323Sed SDNode *N; 3153193323Sed SDVTList VTs = getVTList(VT); 3154218893Sdim if (VT != MVT::Glue) { 3155193323Sed SDValue Ops[] = { N1, N2, N3 }; 3156193323Sed FoldingSetNodeID ID; 3157193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 3); 3158193323Sed void *IP = 0; 3159201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 3160193323Sed return SDValue(E, 0); 3161201360Srdivacky 3162205407Srdivacky N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3); 3163193323Sed CSEMap.InsertNode(N, IP); 3164193323Sed } else { 3165205407Srdivacky N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3); 3166193323Sed } 3167200581Srdivacky 3168193323Sed AllNodes.push_back(N); 3169193323Sed#ifndef NDEBUG 3170218893Sdim VerifySDNode(N); 3171193323Sed#endif 3172193323Sed return SDValue(N, 0); 3173193323Sed} 3174193323Sed 3175198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 3176193323Sed SDValue N1, SDValue N2, SDValue N3, 3177193323Sed SDValue N4) { 3178193323Sed SDValue Ops[] = { N1, N2, N3, N4 }; 3179193323Sed return getNode(Opcode, DL, VT, Ops, 4); 3180193323Sed} 3181193323Sed 3182198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 3183193323Sed SDValue N1, SDValue N2, SDValue N3, 3184193323Sed SDValue N4, SDValue N5) { 3185193323Sed SDValue Ops[] = { N1, N2, N3, N4, N5 }; 3186193323Sed return getNode(Opcode, DL, VT, Ops, 5); 3187193323Sed} 3188193323Sed 3189198090Srdivacky/// getStackArgumentTokenFactor - Compute a TokenFactor to force all 3190198090Srdivacky/// the incoming stack arguments to be loaded from the stack. 3191198090SrdivackySDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) { 3192198090Srdivacky SmallVector<SDValue, 8> ArgChains; 3193198090Srdivacky 3194198090Srdivacky // Include the original chain at the beginning of the list. When this is 3195198090Srdivacky // used by target LowerCall hooks, this helps legalize find the 3196198090Srdivacky // CALLSEQ_BEGIN node. 3197198090Srdivacky ArgChains.push_back(Chain); 3198198090Srdivacky 3199198090Srdivacky // Add a chain value for each stack argument. 3200198090Srdivacky for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(), 3201198090Srdivacky UE = getEntryNode().getNode()->use_end(); U != UE; ++U) 3202198090Srdivacky if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) 3203198090Srdivacky if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) 3204198090Srdivacky if (FI->getIndex() < 0) 3205198090Srdivacky ArgChains.push_back(SDValue(L, 1)); 3206198090Srdivacky 3207198090Srdivacky // Build a tokenfactor for all the chains. 3208198090Srdivacky return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other, 3209198090Srdivacky &ArgChains[0], ArgChains.size()); 3210198090Srdivacky} 3211198090Srdivacky 3212218893Sdim/// SplatByte - Distribute ByteVal over NumBits bits. 3213218893Sdimstatic APInt SplatByte(unsigned NumBits, uint8_t ByteVal) { 3214218893Sdim APInt Val = APInt(NumBits, ByteVal); 3215218893Sdim unsigned Shift = 8; 3216218893Sdim for (unsigned i = NumBits; i > 8; i >>= 1) { 3217218893Sdim Val = (Val << Shift) | Val; 3218218893Sdim Shift <<= 1; 3219218893Sdim } 3220218893Sdim return Val; 3221218893Sdim} 3222218893Sdim 3223193323Sed/// getMemsetValue - Vectorized representation of the memset value 3224193323Sed/// operand. 3225198090Srdivackystatic SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG, 3226193323Sed DebugLoc dl) { 3227206124Srdivacky assert(Value.getOpcode() != ISD::UNDEF); 3228206124Srdivacky 3229204642Srdivacky unsigned NumBits = VT.getScalarType().getSizeInBits(); 3230193323Sed if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { 3231218893Sdim APInt Val = SplatByte(NumBits, C->getZExtValue() & 255); 3232193323Sed if (VT.isInteger()) 3233193323Sed return DAG.getConstant(Val, VT); 3234193323Sed return DAG.getConstantFP(APFloat(Val), VT); 3235193323Sed } 3236193323Sed 3237193323Sed Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value); 3238218893Sdim if (NumBits > 8) { 3239218893Sdim // Use a multiplication with 0x010101... to extend the input to the 3240218893Sdim // required length. 3241218893Sdim APInt Magic = SplatByte(NumBits, 0x01); 3242218893Sdim Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT)); 3243193323Sed } 3244193323Sed 3245193323Sed return Value; 3246193323Sed} 3247193323Sed 3248193323Sed/// getMemsetStringVal - Similar to getMemsetValue. Except this is only 3249193323Sed/// used when a memcpy is turned into a memset when the source is a constant 3250193323Sed/// string ptr. 3251198090Srdivackystatic SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG, 3252198090Srdivacky const TargetLowering &TLI, 3253198090Srdivacky std::string &Str, unsigned Offset) { 3254193323Sed // Handle vector with all elements zero. 3255193323Sed if (Str.empty()) { 3256193323Sed if (VT.isInteger()) 3257193323Sed return DAG.getConstant(0, VT); 3258218893Sdim else if (VT == MVT::f32 || VT == MVT::f64) 3259206083Srdivacky return DAG.getConstantFP(0.0, VT); 3260206083Srdivacky else if (VT.isVector()) { 3261206083Srdivacky unsigned NumElts = VT.getVectorNumElements(); 3262206083Srdivacky MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64; 3263218893Sdim return DAG.getNode(ISD::BITCAST, dl, VT, 3264206083Srdivacky DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(), 3265206083Srdivacky EltVT, NumElts))); 3266206083Srdivacky } else 3267206083Srdivacky llvm_unreachable("Expected type!"); 3268193323Sed } 3269193323Sed 3270193323Sed assert(!VT.isVector() && "Can't handle vector type here!"); 3271193323Sed unsigned NumBits = VT.getSizeInBits(); 3272193323Sed unsigned MSB = NumBits / 8; 3273193323Sed uint64_t Val = 0; 3274193323Sed if (TLI.isLittleEndian()) 3275193323Sed Offset = Offset + MSB - 1; 3276193323Sed for (unsigned i = 0; i != MSB; ++i) { 3277193323Sed Val = (Val << 8) | (unsigned char)Str[Offset]; 3278193323Sed Offset += TLI.isLittleEndian() ? -1 : 1; 3279193323Sed } 3280193323Sed return DAG.getConstant(Val, VT); 3281193323Sed} 3282193323Sed 3283193323Sed/// getMemBasePlusOffset - Returns base and offset node for the 3284193323Sed/// 3285193323Sedstatic SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, 3286193323Sed SelectionDAG &DAG) { 3287198090Srdivacky EVT VT = Base.getValueType(); 3288193323Sed return DAG.getNode(ISD::ADD, Base.getDebugLoc(), 3289193323Sed VT, Base, DAG.getConstant(Offset, VT)); 3290193323Sed} 3291193323Sed 3292193323Sed/// isMemSrcFromString - Returns true if memcpy source is a string constant. 3293193323Sed/// 3294193323Sedstatic bool isMemSrcFromString(SDValue Src, std::string &Str) { 3295193323Sed unsigned SrcDelta = 0; 3296193323Sed GlobalAddressSDNode *G = NULL; 3297193323Sed if (Src.getOpcode() == ISD::GlobalAddress) 3298193323Sed G = cast<GlobalAddressSDNode>(Src); 3299193323Sed else if (Src.getOpcode() == ISD::ADD && 3300193323Sed Src.getOperand(0).getOpcode() == ISD::GlobalAddress && 3301193323Sed Src.getOperand(1).getOpcode() == ISD::Constant) { 3302193323Sed G = cast<GlobalAddressSDNode>(Src.getOperand(0)); 3303193323Sed SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue(); 3304193323Sed } 3305193323Sed if (!G) 3306193323Sed return false; 3307193323Sed 3308207618Srdivacky const GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal()); 3309193323Sed if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false)) 3310193323Sed return true; 3311193323Sed 3312193323Sed return false; 3313193323Sed} 3314193323Sed 3315206083Srdivacky/// FindOptimalMemOpLowering - Determines the optimial series memory ops 3316206083Srdivacky/// to replace the memset / memcpy. Return true if the number of memory ops 3317206083Srdivacky/// is below the threshold. It returns the types of the sequence of 3318206083Srdivacky/// memory ops to perform memset / memcpy by reference. 3319206083Srdivackystatic bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps, 3320206083Srdivacky unsigned Limit, uint64_t Size, 3321206083Srdivacky unsigned DstAlign, unsigned SrcAlign, 3322206124Srdivacky bool NonScalarIntSafe, 3323207618Srdivacky bool MemcpyStrSrc, 3324206083Srdivacky SelectionDAG &DAG, 3325206083Srdivacky const TargetLowering &TLI) { 3326206083Srdivacky assert((SrcAlign == 0 || SrcAlign >= DstAlign) && 3327206083Srdivacky "Expecting memcpy / memset source to meet alignment requirement!"); 3328206083Srdivacky // If 'SrcAlign' is zero, that means the memory operation does not need load 3329206083Srdivacky // the value, i.e. memset or memcpy from constant string. Otherwise, it's 3330206083Srdivacky // the inferred alignment of the source. 'DstAlign', on the other hand, is the 3331206083Srdivacky // specified alignment of the memory operation. If it is zero, that means 3332207618Srdivacky // it's possible to change the alignment of the destination. 'MemcpyStrSrc' 3333207618Srdivacky // indicates whether the memcpy source is constant so it does not need to be 3334207618Srdivacky // loaded. 3335206124Srdivacky EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign, 3336207618Srdivacky NonScalarIntSafe, MemcpyStrSrc, 3337207618Srdivacky DAG.getMachineFunction()); 3338193323Sed 3339204961Srdivacky if (VT == MVT::Other) { 3340206274Srdivacky if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() || 3341206083Srdivacky TLI.allowsUnalignedMemoryAccesses(VT)) { 3342206274Srdivacky VT = TLI.getPointerTy(); 3343193323Sed } else { 3344206083Srdivacky switch (DstAlign & 7) { 3345193323Sed case 0: VT = MVT::i64; break; 3346193323Sed case 4: VT = MVT::i32; break; 3347193323Sed case 2: VT = MVT::i16; break; 3348193323Sed default: VT = MVT::i8; break; 3349193323Sed } 3350193323Sed } 3351193323Sed 3352193323Sed MVT LVT = MVT::i64; 3353193323Sed while (!TLI.isTypeLegal(LVT)) 3354198090Srdivacky LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1); 3355193323Sed assert(LVT.isInteger()); 3356193323Sed 3357193323Sed if (VT.bitsGT(LVT)) 3358193323Sed VT = LVT; 3359193323Sed } 3360193323Sed 3361193323Sed unsigned NumMemOps = 0; 3362193323Sed while (Size != 0) { 3363193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3364193323Sed while (VTSize > Size) { 3365193323Sed // For now, only use non-vector load / store's for the left-over pieces. 3366206083Srdivacky if (VT.isVector() || VT.isFloatingPoint()) { 3367193323Sed VT = MVT::i64; 3368193323Sed while (!TLI.isTypeLegal(VT)) 3369198090Srdivacky VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1); 3370193323Sed VTSize = VT.getSizeInBits() / 8; 3371193323Sed } else { 3372194710Sed // This can result in a type that is not legal on the target, e.g. 3373194710Sed // 1 or 2 bytes on PPC. 3374198090Srdivacky VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1); 3375193323Sed VTSize >>= 1; 3376193323Sed } 3377193323Sed } 3378193323Sed 3379193323Sed if (++NumMemOps > Limit) 3380193323Sed return false; 3381193323Sed MemOps.push_back(VT); 3382193323Sed Size -= VTSize; 3383193323Sed } 3384193323Sed 3385193323Sed return true; 3386193323Sed} 3387193323Sed 3388193323Sedstatic SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl, 3389206083Srdivacky SDValue Chain, SDValue Dst, 3390206083Srdivacky SDValue Src, uint64_t Size, 3391206274Srdivacky unsigned Align, bool isVol, 3392206274Srdivacky bool AlwaysInline, 3393218893Sdim MachinePointerInfo DstPtrInfo, 3394218893Sdim MachinePointerInfo SrcPtrInfo) { 3395206124Srdivacky // Turn a memcpy of undef to nop. 3396206124Srdivacky if (Src.getOpcode() == ISD::UNDEF) 3397206124Srdivacky return Chain; 3398193323Sed 3399193323Sed // Expand memcpy to a series of load and store ops if the size operand falls 3400193323Sed // below a certain threshold. 3401218893Sdim // TODO: In the AlwaysInline case, if the size is big then generate a loop 3402218893Sdim // rather than maybe a humongous number of loads and stores. 3403206124Srdivacky const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3404198090Srdivacky std::vector<EVT> MemOps; 3405206083Srdivacky bool DstAlignCanChange = false; 3406218893Sdim MachineFunction &MF = DAG.getMachineFunction(); 3407218893Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 3408218893Sdim bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize); 3409206083Srdivacky FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); 3410206083Srdivacky if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) 3411206083Srdivacky DstAlignCanChange = true; 3412206083Srdivacky unsigned SrcAlign = DAG.InferPtrAlignment(Src); 3413206083Srdivacky if (Align > SrcAlign) 3414206083Srdivacky SrcAlign = Align; 3415193323Sed std::string Str; 3416206083Srdivacky bool CopyFromStr = isMemSrcFromString(Src, Str); 3417206083Srdivacky bool isZeroStr = CopyFromStr && Str.empty(); 3418218893Sdim unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize); 3419218893Sdim 3420206083Srdivacky if (!FindOptimalMemOpLowering(MemOps, Limit, Size, 3421206083Srdivacky (DstAlignCanChange ? 0 : Align), 3422207618Srdivacky (isZeroStr ? 0 : SrcAlign), 3423207618Srdivacky true, CopyFromStr, DAG, TLI)) 3424193323Sed return SDValue(); 3425193323Sed 3426206083Srdivacky if (DstAlignCanChange) { 3427206083Srdivacky const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); 3428206083Srdivacky unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty); 3429206083Srdivacky if (NewAlign > Align) { 3430206083Srdivacky // Give the stack frame object a larger alignment if needed. 3431206083Srdivacky if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) 3432206083Srdivacky MFI->setObjectAlignment(FI->getIndex(), NewAlign); 3433206083Srdivacky Align = NewAlign; 3434206083Srdivacky } 3435206083Srdivacky } 3436193323Sed 3437193323Sed SmallVector<SDValue, 8> OutChains; 3438193323Sed unsigned NumMemOps = MemOps.size(); 3439193323Sed uint64_t SrcOff = 0, DstOff = 0; 3440198090Srdivacky for (unsigned i = 0; i != NumMemOps; ++i) { 3441198090Srdivacky EVT VT = MemOps[i]; 3442193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3443193323Sed SDValue Value, Store; 3444193323Sed 3445206083Srdivacky if (CopyFromStr && 3446206083Srdivacky (isZeroStr || (VT.isInteger() && !VT.isVector()))) { 3447193323Sed // It's unlikely a store of a vector immediate can be done in a single 3448193323Sed // instruction. It would require a load from a constantpool first. 3449206083Srdivacky // We only handle zero vectors here. 3450193323Sed // FIXME: Handle other cases where store of vector immediate is done in 3451193323Sed // a single instruction. 3452193323Sed Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff); 3453193323Sed Store = DAG.getStore(Chain, dl, Value, 3454193323Sed getMemBasePlusOffset(Dst, DstOff, DAG), 3455218893Sdim DstPtrInfo.getWithOffset(DstOff), isVol, 3456218893Sdim false, Align); 3457193323Sed } else { 3458194710Sed // The type might not be legal for the target. This should only happen 3459194710Sed // if the type is smaller than a legal type, as on PPC, so the right 3460195098Sed // thing to do is generate a LoadExt/StoreTrunc pair. These simplify 3461195098Sed // to Load/Store if NVT==VT. 3462194710Sed // FIXME does the case above also need this? 3463198090Srdivacky EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 3464195098Sed assert(NVT.bitsGE(VT)); 3465218893Sdim Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain, 3466195098Sed getMemBasePlusOffset(Src, SrcOff, DAG), 3467218893Sdim SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false, 3468206083Srdivacky MinAlign(SrcAlign, SrcOff)); 3469195098Sed Store = DAG.getTruncStore(Chain, dl, Value, 3470203954Srdivacky getMemBasePlusOffset(Dst, DstOff, DAG), 3471218893Sdim DstPtrInfo.getWithOffset(DstOff), VT, isVol, 3472218893Sdim false, Align); 3473193323Sed } 3474193323Sed OutChains.push_back(Store); 3475193323Sed SrcOff += VTSize; 3476193323Sed DstOff += VTSize; 3477193323Sed } 3478193323Sed 3479193323Sed return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3480193323Sed &OutChains[0], OutChains.size()); 3481193323Sed} 3482193323Sed 3483193323Sedstatic SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl, 3484206083Srdivacky SDValue Chain, SDValue Dst, 3485206083Srdivacky SDValue Src, uint64_t Size, 3486206274Srdivacky unsigned Align, bool isVol, 3487206274Srdivacky bool AlwaysInline, 3488218893Sdim MachinePointerInfo DstPtrInfo, 3489218893Sdim MachinePointerInfo SrcPtrInfo) { 3490206124Srdivacky // Turn a memmove of undef to nop. 3491206124Srdivacky if (Src.getOpcode() == ISD::UNDEF) 3492206124Srdivacky return Chain; 3493193323Sed 3494193323Sed // Expand memmove to a series of load and store ops if the size operand falls 3495193323Sed // below a certain threshold. 3496206124Srdivacky const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3497198090Srdivacky std::vector<EVT> MemOps; 3498206083Srdivacky bool DstAlignCanChange = false; 3499218893Sdim MachineFunction &MF = DAG.getMachineFunction(); 3500218893Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 3501218893Sdim bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize); 3502206083Srdivacky FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); 3503206083Srdivacky if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) 3504206083Srdivacky DstAlignCanChange = true; 3505206083Srdivacky unsigned SrcAlign = DAG.InferPtrAlignment(Src); 3506206083Srdivacky if (Align > SrcAlign) 3507206083Srdivacky SrcAlign = Align; 3508218893Sdim unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize); 3509206083Srdivacky 3510206083Srdivacky if (!FindOptimalMemOpLowering(MemOps, Limit, Size, 3511206083Srdivacky (DstAlignCanChange ? 0 : Align), 3512207618Srdivacky SrcAlign, true, false, DAG, TLI)) 3513193323Sed return SDValue(); 3514193323Sed 3515206083Srdivacky if (DstAlignCanChange) { 3516206083Srdivacky const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); 3517206083Srdivacky unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty); 3518206083Srdivacky if (NewAlign > Align) { 3519206083Srdivacky // Give the stack frame object a larger alignment if needed. 3520206083Srdivacky if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) 3521206083Srdivacky MFI->setObjectAlignment(FI->getIndex(), NewAlign); 3522206083Srdivacky Align = NewAlign; 3523206083Srdivacky } 3524206083Srdivacky } 3525206083Srdivacky 3526193323Sed uint64_t SrcOff = 0, DstOff = 0; 3527193323Sed SmallVector<SDValue, 8> LoadValues; 3528193323Sed SmallVector<SDValue, 8> LoadChains; 3529193323Sed SmallVector<SDValue, 8> OutChains; 3530193323Sed unsigned NumMemOps = MemOps.size(); 3531193323Sed for (unsigned i = 0; i < NumMemOps; i++) { 3532198090Srdivacky EVT VT = MemOps[i]; 3533193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3534193323Sed SDValue Value, Store; 3535193323Sed 3536193323Sed Value = DAG.getLoad(VT, dl, Chain, 3537193323Sed getMemBasePlusOffset(Src, SrcOff, DAG), 3538218893Sdim SrcPtrInfo.getWithOffset(SrcOff), isVol, 3539218893Sdim false, SrcAlign); 3540193323Sed LoadValues.push_back(Value); 3541193323Sed LoadChains.push_back(Value.getValue(1)); 3542193323Sed SrcOff += VTSize; 3543193323Sed } 3544193323Sed Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3545193323Sed &LoadChains[0], LoadChains.size()); 3546193323Sed OutChains.clear(); 3547193323Sed for (unsigned i = 0; i < NumMemOps; i++) { 3548198090Srdivacky EVT VT = MemOps[i]; 3549193323Sed unsigned VTSize = VT.getSizeInBits() / 8; 3550193323Sed SDValue Value, Store; 3551193323Sed 3552193323Sed Store = DAG.getStore(Chain, dl, LoadValues[i], 3553193323Sed getMemBasePlusOffset(Dst, DstOff, DAG), 3554218893Sdim DstPtrInfo.getWithOffset(DstOff), isVol, false, Align); 3555193323Sed OutChains.push_back(Store); 3556193323Sed DstOff += VTSize; 3557193323Sed } 3558193323Sed 3559193323Sed return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3560193323Sed &OutChains[0], OutChains.size()); 3561193323Sed} 3562193323Sed 3563193323Sedstatic SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl, 3564206083Srdivacky SDValue Chain, SDValue Dst, 3565206083Srdivacky SDValue Src, uint64_t Size, 3566206274Srdivacky unsigned Align, bool isVol, 3567218893Sdim MachinePointerInfo DstPtrInfo) { 3568206124Srdivacky // Turn a memset of undef to nop. 3569206124Srdivacky if (Src.getOpcode() == ISD::UNDEF) 3570206124Srdivacky return Chain; 3571193323Sed 3572193323Sed // Expand memset to a series of load/store ops if the size operand 3573193323Sed // falls below a certain threshold. 3574206124Srdivacky const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3575198090Srdivacky std::vector<EVT> MemOps; 3576206083Srdivacky bool DstAlignCanChange = false; 3577218893Sdim MachineFunction &MF = DAG.getMachineFunction(); 3578218893Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 3579218893Sdim bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize); 3580206083Srdivacky FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); 3581206083Srdivacky if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) 3582206083Srdivacky DstAlignCanChange = true; 3583206124Srdivacky bool NonScalarIntSafe = 3584206124Srdivacky isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue(); 3585218893Sdim if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize), 3586206083Srdivacky Size, (DstAlignCanChange ? 0 : Align), 0, 3587207618Srdivacky NonScalarIntSafe, false, DAG, TLI)) 3588193323Sed return SDValue(); 3589193323Sed 3590206083Srdivacky if (DstAlignCanChange) { 3591206083Srdivacky const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); 3592206083Srdivacky unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty); 3593206083Srdivacky if (NewAlign > Align) { 3594206083Srdivacky // Give the stack frame object a larger alignment if needed. 3595206083Srdivacky if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) 3596206083Srdivacky MFI->setObjectAlignment(FI->getIndex(), NewAlign); 3597206083Srdivacky Align = NewAlign; 3598206083Srdivacky } 3599206083Srdivacky } 3600206083Srdivacky 3601193323Sed SmallVector<SDValue, 8> OutChains; 3602193323Sed uint64_t DstOff = 0; 3603193323Sed unsigned NumMemOps = MemOps.size(); 3604218893Sdim 3605218893Sdim // Find the largest store and generate the bit pattern for it. 3606218893Sdim EVT LargestVT = MemOps[0]; 3607218893Sdim for (unsigned i = 1; i < NumMemOps; i++) 3608218893Sdim if (MemOps[i].bitsGT(LargestVT)) 3609218893Sdim LargestVT = MemOps[i]; 3610218893Sdim SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl); 3611218893Sdim 3612193323Sed for (unsigned i = 0; i < NumMemOps; i++) { 3613198090Srdivacky EVT VT = MemOps[i]; 3614218893Sdim 3615218893Sdim // If this store is smaller than the largest store see whether we can get 3616218893Sdim // the smaller value for free with a truncate. 3617218893Sdim SDValue Value = MemSetValue; 3618218893Sdim if (VT.bitsLT(LargestVT)) { 3619218893Sdim if (!LargestVT.isVector() && !VT.isVector() && 3620218893Sdim TLI.isTruncateFree(LargestVT, VT)) 3621218893Sdim Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue); 3622218893Sdim else 3623218893Sdim Value = getMemsetValue(Src, VT, DAG, dl); 3624218893Sdim } 3625218893Sdim assert(Value.getValueType() == VT && "Value with wrong type."); 3626193323Sed SDValue Store = DAG.getStore(Chain, dl, Value, 3627193323Sed getMemBasePlusOffset(Dst, DstOff, DAG), 3628218893Sdim DstPtrInfo.getWithOffset(DstOff), 3629218893Sdim isVol, false, Align); 3630193323Sed OutChains.push_back(Store); 3631218893Sdim DstOff += VT.getSizeInBits() / 8; 3632193323Sed } 3633193323Sed 3634193323Sed return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 3635193323Sed &OutChains[0], OutChains.size()); 3636193323Sed} 3637193323Sed 3638193323SedSDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst, 3639193323Sed SDValue Src, SDValue Size, 3640206274Srdivacky unsigned Align, bool isVol, bool AlwaysInline, 3641218893Sdim MachinePointerInfo DstPtrInfo, 3642218893Sdim MachinePointerInfo SrcPtrInfo) { 3643193323Sed 3644193323Sed // Check to see if we should lower the memcpy to loads and stores first. 3645193323Sed // For cases within the target-specified limits, this is the best choice. 3646193323Sed ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 3647193323Sed if (ConstantSize) { 3648193323Sed // Memcpy with size zero? Just return the original chain. 3649193323Sed if (ConstantSize->isNullValue()) 3650193323Sed return Chain; 3651193323Sed 3652206083Srdivacky SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, 3653206083Srdivacky ConstantSize->getZExtValue(),Align, 3654218893Sdim isVol, false, DstPtrInfo, SrcPtrInfo); 3655193323Sed if (Result.getNode()) 3656193323Sed return Result; 3657193323Sed } 3658193323Sed 3659193323Sed // Then check to see if we should lower the memcpy with target-specific 3660193323Sed // code. If the target chooses to do this, this is the next best. 3661193323Sed SDValue Result = 3662208599Srdivacky TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align, 3663206274Srdivacky isVol, AlwaysInline, 3664218893Sdim DstPtrInfo, SrcPtrInfo); 3665193323Sed if (Result.getNode()) 3666193323Sed return Result; 3667193323Sed 3668193323Sed // If we really need inline code and the target declined to provide it, 3669193323Sed // use a (potentially long) sequence of loads and stores. 3670193323Sed if (AlwaysInline) { 3671193323Sed assert(ConstantSize && "AlwaysInline requires a constant size!"); 3672193323Sed return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, 3673206274Srdivacky ConstantSize->getZExtValue(), Align, isVol, 3674218893Sdim true, DstPtrInfo, SrcPtrInfo); 3675193323Sed } 3676193323Sed 3677206274Srdivacky // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc 3678206274Srdivacky // memcpy is not guaranteed to be safe. libc memcpys aren't required to 3679206274Srdivacky // respect volatile, so they may do things like read or write memory 3680206274Srdivacky // beyond the given memory regions. But fixing this isn't easy, and most 3681206274Srdivacky // people don't care. 3682206274Srdivacky 3683193323Sed // Emit a library call. 3684193323Sed TargetLowering::ArgListTy Args; 3685193323Sed TargetLowering::ArgListEntry Entry; 3686198090Srdivacky Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext()); 3687193323Sed Entry.Node = Dst; Args.push_back(Entry); 3688193323Sed Entry.Node = Src; Args.push_back(Entry); 3689193323Sed Entry.Node = Size; Args.push_back(Entry); 3690193323Sed // FIXME: pass in DebugLoc 3691193323Sed std::pair<SDValue,SDValue> CallResult = 3692198090Srdivacky TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()), 3693198090Srdivacky false, false, false, false, 0, 3694198090Srdivacky TLI.getLibcallCallingConv(RTLIB::MEMCPY), false, 3695198090Srdivacky /*isReturnValueUsed=*/false, 3696198090Srdivacky getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY), 3697198090Srdivacky TLI.getPointerTy()), 3698204642Srdivacky Args, *this, dl); 3699193323Sed return CallResult.second; 3700193323Sed} 3701193323Sed 3702193323SedSDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst, 3703193323Sed SDValue Src, SDValue Size, 3704206274Srdivacky unsigned Align, bool isVol, 3705218893Sdim MachinePointerInfo DstPtrInfo, 3706218893Sdim MachinePointerInfo SrcPtrInfo) { 3707193323Sed 3708193323Sed // Check to see if we should lower the memmove to loads and stores first. 3709193323Sed // For cases within the target-specified limits, this is the best choice. 3710193323Sed ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 3711193323Sed if (ConstantSize) { 3712193323Sed // Memmove with size zero? Just return the original chain. 3713193323Sed if (ConstantSize->isNullValue()) 3714193323Sed return Chain; 3715193323Sed 3716193323Sed SDValue Result = 3717193323Sed getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src, 3718206274Srdivacky ConstantSize->getZExtValue(), Align, isVol, 3719218893Sdim false, DstPtrInfo, SrcPtrInfo); 3720193323Sed if (Result.getNode()) 3721193323Sed return Result; 3722193323Sed } 3723193323Sed 3724193323Sed // Then check to see if we should lower the memmove with target-specific 3725193323Sed // code. If the target chooses to do this, this is the next best. 3726193323Sed SDValue Result = 3727208599Srdivacky TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol, 3728218893Sdim DstPtrInfo, SrcPtrInfo); 3729193323Sed if (Result.getNode()) 3730193323Sed return Result; 3731193323Sed 3732207618Srdivacky // FIXME: If the memmove is volatile, lowering it to plain libc memmove may 3733207618Srdivacky // not be safe. See memcpy above for more details. 3734207618Srdivacky 3735193323Sed // Emit a library call. 3736193323Sed TargetLowering::ArgListTy Args; 3737193323Sed TargetLowering::ArgListEntry Entry; 3738198090Srdivacky Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext()); 3739193323Sed Entry.Node = Dst; Args.push_back(Entry); 3740193323Sed Entry.Node = Src; Args.push_back(Entry); 3741193323Sed Entry.Node = Size; Args.push_back(Entry); 3742193323Sed // FIXME: pass in DebugLoc 3743193323Sed std::pair<SDValue,SDValue> CallResult = 3744198090Srdivacky TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()), 3745198090Srdivacky false, false, false, false, 0, 3746198090Srdivacky TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false, 3747198090Srdivacky /*isReturnValueUsed=*/false, 3748198090Srdivacky getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE), 3749198090Srdivacky TLI.getPointerTy()), 3750204642Srdivacky Args, *this, dl); 3751193323Sed return CallResult.second; 3752193323Sed} 3753193323Sed 3754193323SedSDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst, 3755193323Sed SDValue Src, SDValue Size, 3756206274Srdivacky unsigned Align, bool isVol, 3757218893Sdim MachinePointerInfo DstPtrInfo) { 3758193323Sed 3759193323Sed // Check to see if we should lower the memset to stores first. 3760193323Sed // For cases within the target-specified limits, this is the best choice. 3761193323Sed ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 3762193323Sed if (ConstantSize) { 3763193323Sed // Memset with size zero? Just return the original chain. 3764193323Sed if (ConstantSize->isNullValue()) 3765193323Sed return Chain; 3766193323Sed 3767206274Srdivacky SDValue Result = 3768206274Srdivacky getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), 3769218893Sdim Align, isVol, DstPtrInfo); 3770206274Srdivacky 3771193323Sed if (Result.getNode()) 3772193323Sed return Result; 3773193323Sed } 3774193323Sed 3775193323Sed // Then check to see if we should lower the memset with target-specific 3776193323Sed // code. If the target chooses to do this, this is the next best. 3777193323Sed SDValue Result = 3778208599Srdivacky TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol, 3779218893Sdim DstPtrInfo); 3780193323Sed if (Result.getNode()) 3781193323Sed return Result; 3782193323Sed 3783218893Sdim // Emit a library call. 3784198090Srdivacky const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext()); 3785193323Sed TargetLowering::ArgListTy Args; 3786193323Sed TargetLowering::ArgListEntry Entry; 3787193323Sed Entry.Node = Dst; Entry.Ty = IntPtrTy; 3788193323Sed Args.push_back(Entry); 3789193323Sed // Extend or truncate the argument to be an i32 value for the call. 3790193323Sed if (Src.getValueType().bitsGT(MVT::i32)) 3791193323Sed Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src); 3792193323Sed else 3793193323Sed Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src); 3794198090Srdivacky Entry.Node = Src; 3795198090Srdivacky Entry.Ty = Type::getInt32Ty(*getContext()); 3796198090Srdivacky Entry.isSExt = true; 3797193323Sed Args.push_back(Entry); 3798198090Srdivacky Entry.Node = Size; 3799198090Srdivacky Entry.Ty = IntPtrTy; 3800198090Srdivacky Entry.isSExt = false; 3801193323Sed Args.push_back(Entry); 3802193323Sed // FIXME: pass in DebugLoc 3803193323Sed std::pair<SDValue,SDValue> CallResult = 3804198090Srdivacky TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()), 3805198090Srdivacky false, false, false, false, 0, 3806198090Srdivacky TLI.getLibcallCallingConv(RTLIB::MEMSET), false, 3807198090Srdivacky /*isReturnValueUsed=*/false, 3808198090Srdivacky getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET), 3809198090Srdivacky TLI.getPointerTy()), 3810204642Srdivacky Args, *this, dl); 3811193323Sed return CallResult.second; 3812193323Sed} 3813193323Sed 3814198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3815218893Sdim SDValue Chain, SDValue Ptr, SDValue Cmp, 3816218893Sdim SDValue Swp, MachinePointerInfo PtrInfo, 3817193323Sed unsigned Alignment) { 3818198090Srdivacky if (Alignment == 0) // Ensure that codegen never sees alignment 0 3819198090Srdivacky Alignment = getEVTAlignment(MemVT); 3820198090Srdivacky 3821198090Srdivacky MachineFunction &MF = getMachineFunction(); 3822198090Srdivacky unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 3823198090Srdivacky 3824198090Srdivacky // For now, atomics are considered to be volatile always. 3825198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3826198090Srdivacky 3827198090Srdivacky MachineMemOperand *MMO = 3828218893Sdim MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment); 3829198090Srdivacky 3830198090Srdivacky return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO); 3831198090Srdivacky} 3832198090Srdivacky 3833198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3834198090Srdivacky SDValue Chain, 3835198090Srdivacky SDValue Ptr, SDValue Cmp, 3836198090Srdivacky SDValue Swp, MachineMemOperand *MMO) { 3837193323Sed assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op"); 3838193323Sed assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types"); 3839193323Sed 3840198090Srdivacky EVT VT = Cmp.getValueType(); 3841193323Sed 3842193323Sed SDVTList VTs = getVTList(VT, MVT::Other); 3843193323Sed FoldingSetNodeID ID; 3844193323Sed ID.AddInteger(MemVT.getRawBits()); 3845193323Sed SDValue Ops[] = {Chain, Ptr, Cmp, Swp}; 3846193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 4); 3847193323Sed void* IP = 0; 3848198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3849198090Srdivacky cast<AtomicSDNode>(E)->refineAlignment(MMO); 3850193323Sed return SDValue(E, 0); 3851198090Srdivacky } 3852205407Srdivacky SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, 3853205407Srdivacky Ptr, Cmp, Swp, MMO); 3854193323Sed CSEMap.InsertNode(N, IP); 3855193323Sed AllNodes.push_back(N); 3856193323Sed return SDValue(N, 0); 3857193323Sed} 3858193323Sed 3859198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3860193323Sed SDValue Chain, 3861193323Sed SDValue Ptr, SDValue Val, 3862193323Sed const Value* PtrVal, 3863193323Sed unsigned Alignment) { 3864198090Srdivacky if (Alignment == 0) // Ensure that codegen never sees alignment 0 3865198090Srdivacky Alignment = getEVTAlignment(MemVT); 3866198090Srdivacky 3867198090Srdivacky MachineFunction &MF = getMachineFunction(); 3868198090Srdivacky unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 3869198090Srdivacky 3870198090Srdivacky // For now, atomics are considered to be volatile always. 3871198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3872198090Srdivacky 3873198090Srdivacky MachineMemOperand *MMO = 3874218893Sdim MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags, 3875198090Srdivacky MemVT.getStoreSize(), Alignment); 3876198090Srdivacky 3877198090Srdivacky return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO); 3878198090Srdivacky} 3879198090Srdivacky 3880198090SrdivackySDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, 3881198090Srdivacky SDValue Chain, 3882198090Srdivacky SDValue Ptr, SDValue Val, 3883198090Srdivacky MachineMemOperand *MMO) { 3884193323Sed assert((Opcode == ISD::ATOMIC_LOAD_ADD || 3885193323Sed Opcode == ISD::ATOMIC_LOAD_SUB || 3886193323Sed Opcode == ISD::ATOMIC_LOAD_AND || 3887193323Sed Opcode == ISD::ATOMIC_LOAD_OR || 3888193323Sed Opcode == ISD::ATOMIC_LOAD_XOR || 3889193323Sed Opcode == ISD::ATOMIC_LOAD_NAND || 3890193323Sed Opcode == ISD::ATOMIC_LOAD_MIN || 3891193323Sed Opcode == ISD::ATOMIC_LOAD_MAX || 3892193323Sed Opcode == ISD::ATOMIC_LOAD_UMIN || 3893193323Sed Opcode == ISD::ATOMIC_LOAD_UMAX || 3894193323Sed Opcode == ISD::ATOMIC_SWAP) && 3895193323Sed "Invalid Atomic Op"); 3896193323Sed 3897198090Srdivacky EVT VT = Val.getValueType(); 3898193323Sed 3899193323Sed SDVTList VTs = getVTList(VT, MVT::Other); 3900193323Sed FoldingSetNodeID ID; 3901193323Sed ID.AddInteger(MemVT.getRawBits()); 3902193323Sed SDValue Ops[] = {Chain, Ptr, Val}; 3903193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, 3); 3904193323Sed void* IP = 0; 3905198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3906198090Srdivacky cast<AtomicSDNode>(E)->refineAlignment(MMO); 3907193323Sed return SDValue(E, 0); 3908198090Srdivacky } 3909205407Srdivacky SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, 3910205407Srdivacky Ptr, Val, MMO); 3911193323Sed CSEMap.InsertNode(N, IP); 3912193323Sed AllNodes.push_back(N); 3913193323Sed return SDValue(N, 0); 3914193323Sed} 3915193323Sed 3916193323Sed/// getMergeValues - Create a MERGE_VALUES node from the given operands. 3917193323SedSDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps, 3918193323Sed DebugLoc dl) { 3919193323Sed if (NumOps == 1) 3920193323Sed return Ops[0]; 3921193323Sed 3922198090Srdivacky SmallVector<EVT, 4> VTs; 3923193323Sed VTs.reserve(NumOps); 3924193323Sed for (unsigned i = 0; i < NumOps; ++i) 3925193323Sed VTs.push_back(Ops[i].getValueType()); 3926193323Sed return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps), 3927193323Sed Ops, NumOps); 3928193323Sed} 3929193323Sed 3930193323SedSDValue 3931193323SedSelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, 3932198090Srdivacky const EVT *VTs, unsigned NumVTs, 3933193323Sed const SDValue *Ops, unsigned NumOps, 3934218893Sdim EVT MemVT, MachinePointerInfo PtrInfo, 3935193323Sed unsigned Align, bool Vol, 3936193323Sed bool ReadMem, bool WriteMem) { 3937193323Sed return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps, 3938218893Sdim MemVT, PtrInfo, Align, Vol, 3939193323Sed ReadMem, WriteMem); 3940193323Sed} 3941193323Sed 3942193323SedSDValue 3943193323SedSelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList, 3944193323Sed const SDValue *Ops, unsigned NumOps, 3945218893Sdim EVT MemVT, MachinePointerInfo PtrInfo, 3946193323Sed unsigned Align, bool Vol, 3947193323Sed bool ReadMem, bool WriteMem) { 3948198090Srdivacky if (Align == 0) // Ensure that codegen never sees alignment 0 3949198090Srdivacky Align = getEVTAlignment(MemVT); 3950198090Srdivacky 3951198090Srdivacky MachineFunction &MF = getMachineFunction(); 3952198090Srdivacky unsigned Flags = 0; 3953198090Srdivacky if (WriteMem) 3954198090Srdivacky Flags |= MachineMemOperand::MOStore; 3955198090Srdivacky if (ReadMem) 3956198090Srdivacky Flags |= MachineMemOperand::MOLoad; 3957198090Srdivacky if (Vol) 3958198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 3959198090Srdivacky MachineMemOperand *MMO = 3960218893Sdim MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align); 3961198090Srdivacky 3962198090Srdivacky return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO); 3963198090Srdivacky} 3964198090Srdivacky 3965198090SrdivackySDValue 3966198090SrdivackySelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList, 3967198090Srdivacky const SDValue *Ops, unsigned NumOps, 3968198090Srdivacky EVT MemVT, MachineMemOperand *MMO) { 3969198090Srdivacky assert((Opcode == ISD::INTRINSIC_VOID || 3970198090Srdivacky Opcode == ISD::INTRINSIC_W_CHAIN || 3971218893Sdim Opcode == ISD::PREFETCH || 3972198090Srdivacky (Opcode <= INT_MAX && 3973198090Srdivacky (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) && 3974198090Srdivacky "Opcode is not a memory-accessing opcode!"); 3975198090Srdivacky 3976193323Sed // Memoize the node unless it returns a flag. 3977193323Sed MemIntrinsicSDNode *N; 3978218893Sdim if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { 3979193323Sed FoldingSetNodeID ID; 3980193323Sed AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 3981193323Sed void *IP = 0; 3982198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 3983198090Srdivacky cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO); 3984193323Sed return SDValue(E, 0); 3985198090Srdivacky } 3986193323Sed 3987205407Srdivacky N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, 3988205407Srdivacky MemVT, MMO); 3989193323Sed CSEMap.InsertNode(N, IP); 3990193323Sed } else { 3991205407Srdivacky N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, 3992205407Srdivacky MemVT, MMO); 3993193323Sed } 3994193323Sed AllNodes.push_back(N); 3995193323Sed return SDValue(N, 0); 3996193323Sed} 3997193323Sed 3998218893Sdim/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a 3999218893Sdim/// MachinePointerInfo record from it. This is particularly useful because the 4000218893Sdim/// code generator has many cases where it doesn't bother passing in a 4001218893Sdim/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst". 4002218893Sdimstatic MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) { 4003218893Sdim // If this is FI+Offset, we can model it. 4004218893Sdim if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) 4005218893Sdim return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset); 4006218893Sdim 4007218893Sdim // If this is (FI+Offset1)+Offset2, we can model it. 4008218893Sdim if (Ptr.getOpcode() != ISD::ADD || 4009218893Sdim !isa<ConstantSDNode>(Ptr.getOperand(1)) || 4010218893Sdim !isa<FrameIndexSDNode>(Ptr.getOperand(0))) 4011218893Sdim return MachinePointerInfo(); 4012218893Sdim 4013218893Sdim int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); 4014218893Sdim return MachinePointerInfo::getFixedStack(FI, Offset+ 4015218893Sdim cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue()); 4016218893Sdim} 4017218893Sdim 4018218893Sdim/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a 4019218893Sdim/// MachinePointerInfo record from it. This is particularly useful because the 4020218893Sdim/// code generator has many cases where it doesn't bother passing in a 4021218893Sdim/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst". 4022218893Sdimstatic MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) { 4023218893Sdim // If the 'Offset' value isn't a constant, we can't handle this. 4024218893Sdim if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp)) 4025218893Sdim return InferPointerInfo(Ptr, OffsetNode->getSExtValue()); 4026218893Sdim if (OffsetOp.getOpcode() == ISD::UNDEF) 4027218893Sdim return InferPointerInfo(Ptr); 4028218893Sdim return MachinePointerInfo(); 4029218893Sdim} 4030218893Sdim 4031218893Sdim 4032193323SedSDValue 4033210299SedSelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, 4034210299Sed EVT VT, DebugLoc dl, SDValue Chain, 4035193323Sed SDValue Ptr, SDValue Offset, 4036218893Sdim MachinePointerInfo PtrInfo, EVT MemVT, 4037203954Srdivacky bool isVolatile, bool isNonTemporal, 4038218893Sdim unsigned Alignment, const MDNode *TBAAInfo) { 4039193323Sed if (Alignment == 0) // Ensure that codegen never sees alignment 0 4040198090Srdivacky Alignment = getEVTAlignment(VT); 4041193323Sed 4042198090Srdivacky unsigned Flags = MachineMemOperand::MOLoad; 4043198090Srdivacky if (isVolatile) 4044198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 4045203954Srdivacky if (isNonTemporal) 4046203954Srdivacky Flags |= MachineMemOperand::MONonTemporal; 4047218893Sdim 4048218893Sdim // If we don't have a PtrInfo, infer the trivial frame index case to simplify 4049218893Sdim // clients. 4050218893Sdim if (PtrInfo.V == 0) 4051218893Sdim PtrInfo = InferPointerInfo(Ptr, Offset); 4052218893Sdim 4053218893Sdim MachineFunction &MF = getMachineFunction(); 4054198090Srdivacky MachineMemOperand *MMO = 4055218893Sdim MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment, 4056218893Sdim TBAAInfo); 4057210299Sed return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO); 4058198090Srdivacky} 4059198090Srdivacky 4060198090SrdivackySDValue 4061218893SdimSelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, 4062210299Sed EVT VT, DebugLoc dl, SDValue Chain, 4063198090Srdivacky SDValue Ptr, SDValue Offset, EVT MemVT, 4064198090Srdivacky MachineMemOperand *MMO) { 4065198090Srdivacky if (VT == MemVT) { 4066193323Sed ExtType = ISD::NON_EXTLOAD; 4067193323Sed } else if (ExtType == ISD::NON_EXTLOAD) { 4068198090Srdivacky assert(VT == MemVT && "Non-extending load from different memory type!"); 4069193323Sed } else { 4070193323Sed // Extending load. 4071200581Srdivacky assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) && 4072200581Srdivacky "Should only be an extending load, not truncating!"); 4073198090Srdivacky assert(VT.isInteger() == MemVT.isInteger() && 4074193323Sed "Cannot convert from FP to Int or Int -> FP!"); 4075200581Srdivacky assert(VT.isVector() == MemVT.isVector() && 4076200581Srdivacky "Cannot use trunc store to convert to or from a vector!"); 4077200581Srdivacky assert((!VT.isVector() || 4078200581Srdivacky VT.getVectorNumElements() == MemVT.getVectorNumElements()) && 4079200581Srdivacky "Cannot use trunc store to change the number of vector elements!"); 4080193323Sed } 4081193323Sed 4082193323Sed bool Indexed = AM != ISD::UNINDEXED; 4083193323Sed assert((Indexed || Offset.getOpcode() == ISD::UNDEF) && 4084193323Sed "Unindexed load with an offset!"); 4085193323Sed 4086193323Sed SDVTList VTs = Indexed ? 4087193323Sed getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other); 4088193323Sed SDValue Ops[] = { Chain, Ptr, Offset }; 4089193323Sed FoldingSetNodeID ID; 4090193323Sed AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 4091198090Srdivacky ID.AddInteger(MemVT.getRawBits()); 4092204642Srdivacky ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(), 4093204642Srdivacky MMO->isNonTemporal())); 4094193323Sed void *IP = 0; 4095198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4096198090Srdivacky cast<LoadSDNode>(E)->refineAlignment(MMO); 4097193323Sed return SDValue(E, 0); 4098198090Srdivacky } 4099205407Srdivacky SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType, 4100205407Srdivacky MemVT, MMO); 4101193323Sed CSEMap.InsertNode(N, IP); 4102193323Sed AllNodes.push_back(N); 4103193323Sed return SDValue(N, 0); 4104193323Sed} 4105193323Sed 4106198090SrdivackySDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl, 4107193323Sed SDValue Chain, SDValue Ptr, 4108218893Sdim MachinePointerInfo PtrInfo, 4109203954Srdivacky bool isVolatile, bool isNonTemporal, 4110218893Sdim unsigned Alignment, const MDNode *TBAAInfo) { 4111193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 4112210299Sed return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef, 4113218893Sdim PtrInfo, VT, isVolatile, isNonTemporal, Alignment, TBAAInfo); 4114193323Sed} 4115193323Sed 4116218893SdimSDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT, 4117193323Sed SDValue Chain, SDValue Ptr, 4118218893Sdim MachinePointerInfo PtrInfo, EVT MemVT, 4119203954Srdivacky bool isVolatile, bool isNonTemporal, 4120218893Sdim unsigned Alignment, const MDNode *TBAAInfo) { 4121193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 4122210299Sed return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, 4123218893Sdim PtrInfo, MemVT, isVolatile, isNonTemporal, Alignment, 4124218893Sdim TBAAInfo); 4125193323Sed} 4126193323Sed 4127218893Sdim 4128193323SedSDValue 4129193323SedSelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base, 4130193323Sed SDValue Offset, ISD::MemIndexedMode AM) { 4131193323Sed LoadSDNode *LD = cast<LoadSDNode>(OrigLoad); 4132193323Sed assert(LD->getOffset().getOpcode() == ISD::UNDEF && 4133193323Sed "Load is already a indexed load!"); 4134210299Sed return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl, 4135218893Sdim LD->getChain(), Base, Offset, LD->getPointerInfo(), 4136218893Sdim LD->getMemoryVT(), 4137203954Srdivacky LD->isVolatile(), LD->isNonTemporal(), LD->getAlignment()); 4138193323Sed} 4139193323Sed 4140193323SedSDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val, 4141218893Sdim SDValue Ptr, MachinePointerInfo PtrInfo, 4142203954Srdivacky bool isVolatile, bool isNonTemporal, 4143218893Sdim unsigned Alignment, const MDNode *TBAAInfo) { 4144193323Sed if (Alignment == 0) // Ensure that codegen never sees alignment 0 4145198090Srdivacky Alignment = getEVTAlignment(Val.getValueType()); 4146193323Sed 4147198090Srdivacky unsigned Flags = MachineMemOperand::MOStore; 4148198090Srdivacky if (isVolatile) 4149198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 4150203954Srdivacky if (isNonTemporal) 4151203954Srdivacky Flags |= MachineMemOperand::MONonTemporal; 4152218893Sdim 4153218893Sdim if (PtrInfo.V == 0) 4154218893Sdim PtrInfo = InferPointerInfo(Ptr); 4155218893Sdim 4156218893Sdim MachineFunction &MF = getMachineFunction(); 4157198090Srdivacky MachineMemOperand *MMO = 4158218893Sdim MF.getMachineMemOperand(PtrInfo, Flags, 4159218893Sdim Val.getValueType().getStoreSize(), Alignment, 4160218893Sdim TBAAInfo); 4161198090Srdivacky 4162198090Srdivacky return getStore(Chain, dl, Val, Ptr, MMO); 4163198090Srdivacky} 4164198090Srdivacky 4165198090SrdivackySDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val, 4166198090Srdivacky SDValue Ptr, MachineMemOperand *MMO) { 4167198090Srdivacky EVT VT = Val.getValueType(); 4168193323Sed SDVTList VTs = getVTList(MVT::Other); 4169193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 4170193323Sed SDValue Ops[] = { Chain, Val, Ptr, Undef }; 4171193323Sed FoldingSetNodeID ID; 4172193323Sed AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 4173193323Sed ID.AddInteger(VT.getRawBits()); 4174204642Srdivacky ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(), 4175204642Srdivacky MMO->isNonTemporal())); 4176193323Sed void *IP = 0; 4177198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4178198090Srdivacky cast<StoreSDNode>(E)->refineAlignment(MMO); 4179193323Sed return SDValue(E, 0); 4180198090Srdivacky } 4181205407Srdivacky SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, 4182205407Srdivacky false, VT, MMO); 4183193323Sed CSEMap.InsertNode(N, IP); 4184193323Sed AllNodes.push_back(N); 4185193323Sed return SDValue(N, 0); 4186193323Sed} 4187193323Sed 4188193323SedSDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, 4189218893Sdim SDValue Ptr, MachinePointerInfo PtrInfo, 4190218893Sdim EVT SVT,bool isVolatile, bool isNonTemporal, 4191218893Sdim unsigned Alignment, 4192218893Sdim const MDNode *TBAAInfo) { 4193198090Srdivacky if (Alignment == 0) // Ensure that codegen never sees alignment 0 4194198090Srdivacky Alignment = getEVTAlignment(SVT); 4195193323Sed 4196198090Srdivacky unsigned Flags = MachineMemOperand::MOStore; 4197198090Srdivacky if (isVolatile) 4198198090Srdivacky Flags |= MachineMemOperand::MOVolatile; 4199203954Srdivacky if (isNonTemporal) 4200203954Srdivacky Flags |= MachineMemOperand::MONonTemporal; 4201218893Sdim 4202218893Sdim if (PtrInfo.V == 0) 4203218893Sdim PtrInfo = InferPointerInfo(Ptr); 4204218893Sdim 4205218893Sdim MachineFunction &MF = getMachineFunction(); 4206198090Srdivacky MachineMemOperand *MMO = 4207218893Sdim MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment, 4208218893Sdim TBAAInfo); 4209198090Srdivacky 4210198090Srdivacky return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO); 4211198090Srdivacky} 4212198090Srdivacky 4213198090SrdivackySDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, 4214198090Srdivacky SDValue Ptr, EVT SVT, 4215198090Srdivacky MachineMemOperand *MMO) { 4216198090Srdivacky EVT VT = Val.getValueType(); 4217198090Srdivacky 4218193323Sed if (VT == SVT) 4219198090Srdivacky return getStore(Chain, dl, Val, Ptr, MMO); 4220193323Sed 4221200581Srdivacky assert(SVT.getScalarType().bitsLT(VT.getScalarType()) && 4222200581Srdivacky "Should only be a truncating store, not extending!"); 4223193323Sed assert(VT.isInteger() == SVT.isInteger() && 4224193323Sed "Can't do FP-INT conversion!"); 4225200581Srdivacky assert(VT.isVector() == SVT.isVector() && 4226200581Srdivacky "Cannot use trunc store to convert to or from a vector!"); 4227200581Srdivacky assert((!VT.isVector() || 4228200581Srdivacky VT.getVectorNumElements() == SVT.getVectorNumElements()) && 4229200581Srdivacky "Cannot use trunc store to change the number of vector elements!"); 4230193323Sed 4231193323Sed SDVTList VTs = getVTList(MVT::Other); 4232193323Sed SDValue Undef = getUNDEF(Ptr.getValueType()); 4233193323Sed SDValue Ops[] = { Chain, Val, Ptr, Undef }; 4234193323Sed FoldingSetNodeID ID; 4235193323Sed AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 4236193323Sed ID.AddInteger(SVT.getRawBits()); 4237204642Srdivacky ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(), 4238204642Srdivacky MMO->isNonTemporal())); 4239193323Sed void *IP = 0; 4240198090Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4241198090Srdivacky cast<StoreSDNode>(E)->refineAlignment(MMO); 4242193323Sed return SDValue(E, 0); 4243198090Srdivacky } 4244205407Srdivacky SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, 4245205407Srdivacky true, SVT, MMO); 4246193323Sed CSEMap.InsertNode(N, IP); 4247193323Sed AllNodes.push_back(N); 4248193323Sed return SDValue(N, 0); 4249193323Sed} 4250193323Sed 4251193323SedSDValue 4252193323SedSelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base, 4253193323Sed SDValue Offset, ISD::MemIndexedMode AM) { 4254193323Sed StoreSDNode *ST = cast<StoreSDNode>(OrigStore); 4255193323Sed assert(ST->getOffset().getOpcode() == ISD::UNDEF && 4256193323Sed "Store is already a indexed store!"); 4257193323Sed SDVTList VTs = getVTList(Base.getValueType(), MVT::Other); 4258193323Sed SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset }; 4259193323Sed FoldingSetNodeID ID; 4260193323Sed AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 4261193323Sed ID.AddInteger(ST->getMemoryVT().getRawBits()); 4262193323Sed ID.AddInteger(ST->getRawSubclassData()); 4263193323Sed void *IP = 0; 4264201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4265193323Sed return SDValue(E, 0); 4266201360Srdivacky 4267205407Srdivacky SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM, 4268205407Srdivacky ST->isTruncatingStore(), 4269205407Srdivacky ST->getMemoryVT(), 4270205407Srdivacky ST->getMemOperand()); 4271193323Sed CSEMap.InsertNode(N, IP); 4272193323Sed AllNodes.push_back(N); 4273193323Sed return SDValue(N, 0); 4274193323Sed} 4275193323Sed 4276198090SrdivackySDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl, 4277193323Sed SDValue Chain, SDValue Ptr, 4278210299Sed SDValue SV, 4279210299Sed unsigned Align) { 4280210299Sed SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) }; 4281210299Sed return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4); 4282193323Sed} 4283193323Sed 4284198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 4285193323Sed const SDUse *Ops, unsigned NumOps) { 4286193323Sed switch (NumOps) { 4287193323Sed case 0: return getNode(Opcode, DL, VT); 4288193323Sed case 1: return getNode(Opcode, DL, VT, Ops[0]); 4289193323Sed case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); 4290193323Sed case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); 4291193323Sed default: break; 4292193323Sed } 4293193323Sed 4294193323Sed // Copy from an SDUse array into an SDValue array for use with 4295193323Sed // the regular getNode logic. 4296193323Sed SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps); 4297193323Sed return getNode(Opcode, DL, VT, &NewOps[0], NumOps); 4298193323Sed} 4299193323Sed 4300198090SrdivackySDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, 4301193323Sed const SDValue *Ops, unsigned NumOps) { 4302193323Sed switch (NumOps) { 4303193323Sed case 0: return getNode(Opcode, DL, VT); 4304193323Sed case 1: return getNode(Opcode, DL, VT, Ops[0]); 4305193323Sed case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); 4306193323Sed case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); 4307193323Sed default: break; 4308193323Sed } 4309193323Sed 4310193323Sed switch (Opcode) { 4311193323Sed default: break; 4312193323Sed case ISD::SELECT_CC: { 4313193323Sed assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); 4314193323Sed assert(Ops[0].getValueType() == Ops[1].getValueType() && 4315193323Sed "LHS and RHS of condition must have same type!"); 4316193323Sed assert(Ops[2].getValueType() == Ops[3].getValueType() && 4317193323Sed "True and False arms of SelectCC must have same type!"); 4318193323Sed assert(Ops[2].getValueType() == VT && 4319193323Sed "select_cc node must be of same type as true and false value!"); 4320193323Sed break; 4321193323Sed } 4322193323Sed case ISD::BR_CC: { 4323193323Sed assert(NumOps == 5 && "BR_CC takes 5 operands!"); 4324193323Sed assert(Ops[2].getValueType() == Ops[3].getValueType() && 4325193323Sed "LHS/RHS of comparison should match types!"); 4326193323Sed break; 4327193323Sed } 4328193323Sed } 4329193323Sed 4330193323Sed // Memoize nodes. 4331193323Sed SDNode *N; 4332193323Sed SDVTList VTs = getVTList(VT); 4333193323Sed 4334218893Sdim if (VT != MVT::Glue) { 4335193323Sed FoldingSetNodeID ID; 4336193323Sed AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps); 4337193323Sed void *IP = 0; 4338193323Sed 4339201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4340193323Sed return SDValue(E, 0); 4341193323Sed 4342205407Srdivacky N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps); 4343193323Sed CSEMap.InsertNode(N, IP); 4344193323Sed } else { 4345205407Srdivacky N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps); 4346193323Sed } 4347193323Sed 4348193323Sed AllNodes.push_back(N); 4349193323Sed#ifndef NDEBUG 4350218893Sdim VerifySDNode(N); 4351193323Sed#endif 4352193323Sed return SDValue(N, 0); 4353193323Sed} 4354193323Sed 4355193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, 4356198090Srdivacky const std::vector<EVT> &ResultTys, 4357193323Sed const SDValue *Ops, unsigned NumOps) { 4358193323Sed return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()), 4359193323Sed Ops, NumOps); 4360193323Sed} 4361193323Sed 4362193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, 4363198090Srdivacky const EVT *VTs, unsigned NumVTs, 4364193323Sed const SDValue *Ops, unsigned NumOps) { 4365193323Sed if (NumVTs == 1) 4366193323Sed return getNode(Opcode, DL, VTs[0], Ops, NumOps); 4367193323Sed return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps); 4368193323Sed} 4369193323Sed 4370193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4371193323Sed const SDValue *Ops, unsigned NumOps) { 4372193323Sed if (VTList.NumVTs == 1) 4373193323Sed return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps); 4374193323Sed 4375198090Srdivacky#if 0 4376193323Sed switch (Opcode) { 4377193323Sed // FIXME: figure out how to safely handle things like 4378193323Sed // int foo(int x) { return 1 << (x & 255); } 4379193323Sed // int bar() { return foo(256); } 4380193323Sed case ISD::SRA_PARTS: 4381193323Sed case ISD::SRL_PARTS: 4382193323Sed case ISD::SHL_PARTS: 4383193323Sed if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 4384193323Sed cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 4385193323Sed return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); 4386193323Sed else if (N3.getOpcode() == ISD::AND) 4387193323Sed if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 4388193323Sed // If the and is only masking out bits that cannot effect the shift, 4389193323Sed // eliminate the and. 4390202375Srdivacky unsigned NumBits = VT.getScalarType().getSizeInBits()*2; 4391193323Sed if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 4392193323Sed return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); 4393193323Sed } 4394193323Sed break; 4395198090Srdivacky } 4396193323Sed#endif 4397193323Sed 4398193323Sed // Memoize the node unless it returns a flag. 4399193323Sed SDNode *N; 4400218893Sdim if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { 4401193323Sed FoldingSetNodeID ID; 4402193323Sed AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 4403193323Sed void *IP = 0; 4404201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 4405193323Sed return SDValue(E, 0); 4406201360Srdivacky 4407193323Sed if (NumOps == 1) { 4408205407Srdivacky N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]); 4409193323Sed } else if (NumOps == 2) { 4410205407Srdivacky N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]); 4411193323Sed } else if (NumOps == 3) { 4412205407Srdivacky N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], 4413205407Srdivacky Ops[2]); 4414193323Sed } else { 4415205407Srdivacky N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps); 4416193323Sed } 4417193323Sed CSEMap.InsertNode(N, IP); 4418193323Sed } else { 4419193323Sed if (NumOps == 1) { 4420205407Srdivacky N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]); 4421193323Sed } else if (NumOps == 2) { 4422205407Srdivacky N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]); 4423193323Sed } else if (NumOps == 3) { 4424205407Srdivacky N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], 4425205407Srdivacky Ops[2]); 4426193323Sed } else { 4427205407Srdivacky N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps); 4428193323Sed } 4429193323Sed } 4430193323Sed AllNodes.push_back(N); 4431193323Sed#ifndef NDEBUG 4432218893Sdim VerifySDNode(N); 4433193323Sed#endif 4434193323Sed return SDValue(N, 0); 4435193323Sed} 4436193323Sed 4437193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) { 4438193323Sed return getNode(Opcode, DL, VTList, 0, 0); 4439193323Sed} 4440193323Sed 4441193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4442193323Sed SDValue N1) { 4443193323Sed SDValue Ops[] = { N1 }; 4444193323Sed return getNode(Opcode, DL, VTList, Ops, 1); 4445193323Sed} 4446193323Sed 4447193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4448193323Sed SDValue N1, SDValue N2) { 4449193323Sed SDValue Ops[] = { N1, N2 }; 4450193323Sed return getNode(Opcode, DL, VTList, Ops, 2); 4451193323Sed} 4452193323Sed 4453193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4454193323Sed SDValue N1, SDValue N2, SDValue N3) { 4455193323Sed SDValue Ops[] = { N1, N2, N3 }; 4456193323Sed return getNode(Opcode, DL, VTList, Ops, 3); 4457193323Sed} 4458193323Sed 4459193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4460193323Sed SDValue N1, SDValue N2, SDValue N3, 4461193323Sed SDValue N4) { 4462193323Sed SDValue Ops[] = { N1, N2, N3, N4 }; 4463193323Sed return getNode(Opcode, DL, VTList, Ops, 4); 4464193323Sed} 4465193323Sed 4466193323SedSDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList, 4467193323Sed SDValue N1, SDValue N2, SDValue N3, 4468193323Sed SDValue N4, SDValue N5) { 4469193323Sed SDValue Ops[] = { N1, N2, N3, N4, N5 }; 4470193323Sed return getNode(Opcode, DL, VTList, Ops, 5); 4471193323Sed} 4472193323Sed 4473198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT) { 4474193323Sed return makeVTList(SDNode::getValueTypeList(VT), 1); 4475193323Sed} 4476193323Sed 4477198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) { 4478193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4479193323Sed E = VTList.rend(); I != E; ++I) 4480193323Sed if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2) 4481193323Sed return *I; 4482193323Sed 4483198090Srdivacky EVT *Array = Allocator.Allocate<EVT>(2); 4484193323Sed Array[0] = VT1; 4485193323Sed Array[1] = VT2; 4486193323Sed SDVTList Result = makeVTList(Array, 2); 4487193323Sed VTList.push_back(Result); 4488193323Sed return Result; 4489193323Sed} 4490193323Sed 4491198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) { 4492193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4493193323Sed E = VTList.rend(); I != E; ++I) 4494193323Sed if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 && 4495193323Sed I->VTs[2] == VT3) 4496193323Sed return *I; 4497193323Sed 4498198090Srdivacky EVT *Array = Allocator.Allocate<EVT>(3); 4499193323Sed Array[0] = VT1; 4500193323Sed Array[1] = VT2; 4501193323Sed Array[2] = VT3; 4502193323Sed SDVTList Result = makeVTList(Array, 3); 4503193323Sed VTList.push_back(Result); 4504193323Sed return Result; 4505193323Sed} 4506193323Sed 4507198090SrdivackySDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) { 4508193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4509193323Sed E = VTList.rend(); I != E; ++I) 4510193323Sed if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 && 4511193323Sed I->VTs[2] == VT3 && I->VTs[3] == VT4) 4512193323Sed return *I; 4513193323Sed 4514200581Srdivacky EVT *Array = Allocator.Allocate<EVT>(4); 4515193323Sed Array[0] = VT1; 4516193323Sed Array[1] = VT2; 4517193323Sed Array[2] = VT3; 4518193323Sed Array[3] = VT4; 4519193323Sed SDVTList Result = makeVTList(Array, 4); 4520193323Sed VTList.push_back(Result); 4521193323Sed return Result; 4522193323Sed} 4523193323Sed 4524198090SrdivackySDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) { 4525193323Sed switch (NumVTs) { 4526198090Srdivacky case 0: llvm_unreachable("Cannot have nodes without results!"); 4527193323Sed case 1: return getVTList(VTs[0]); 4528193323Sed case 2: return getVTList(VTs[0], VTs[1]); 4529193323Sed case 3: return getVTList(VTs[0], VTs[1], VTs[2]); 4530201360Srdivacky case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]); 4531193323Sed default: break; 4532193323Sed } 4533193323Sed 4534193323Sed for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(), 4535193323Sed E = VTList.rend(); I != E; ++I) { 4536193323Sed if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1]) 4537193323Sed continue; 4538193323Sed 4539193323Sed bool NoMatch = false; 4540193323Sed for (unsigned i = 2; i != NumVTs; ++i) 4541193323Sed if (VTs[i] != I->VTs[i]) { 4542193323Sed NoMatch = true; 4543193323Sed break; 4544193323Sed } 4545193323Sed if (!NoMatch) 4546193323Sed return *I; 4547193323Sed } 4548193323Sed 4549198090Srdivacky EVT *Array = Allocator.Allocate<EVT>(NumVTs); 4550193323Sed std::copy(VTs, VTs+NumVTs, Array); 4551193323Sed SDVTList Result = makeVTList(Array, NumVTs); 4552193323Sed VTList.push_back(Result); 4553193323Sed return Result; 4554193323Sed} 4555193323Sed 4556193323Sed 4557193323Sed/// UpdateNodeOperands - *Mutate* the specified node in-place to have the 4558193323Sed/// specified operands. If the resultant node already exists in the DAG, 4559193323Sed/// this does not modify the specified node, instead it returns the node that 4560193323Sed/// already exists. If the resultant node does not exist in the DAG, the 4561193323Sed/// input node is returned. As a degenerate case, if you specify the same 4562193323Sed/// input operands as the node already has, the input node is returned. 4563210299SedSDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) { 4564193323Sed assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 4565193323Sed 4566193323Sed // Check to see if there is no change. 4567210299Sed if (Op == N->getOperand(0)) return N; 4568193323Sed 4569193323Sed // See if the modified node already exists. 4570193323Sed void *InsertPos = 0; 4571193323Sed if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) 4572210299Sed return Existing; 4573193323Sed 4574193323Sed // Nope it doesn't. Remove the node from its current place in the maps. 4575193323Sed if (InsertPos) 4576193323Sed if (!RemoveNodeFromCSEMaps(N)) 4577193323Sed InsertPos = 0; 4578193323Sed 4579193323Sed // Now we update the operands. 4580193323Sed N->OperandList[0].set(Op); 4581193323Sed 4582193323Sed // If this gets put into a CSE map, add it. 4583193323Sed if (InsertPos) CSEMap.InsertNode(N, InsertPos); 4584210299Sed return N; 4585193323Sed} 4586193323Sed 4587210299SedSDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) { 4588193323Sed assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 4589193323Sed 4590193323Sed // Check to see if there is no change. 4591193323Sed if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 4592210299Sed return N; // No operands changed, just return the input node. 4593193323Sed 4594193323Sed // See if the modified node already exists. 4595193323Sed void *InsertPos = 0; 4596193323Sed if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) 4597210299Sed return Existing; 4598193323Sed 4599193323Sed // Nope it doesn't. Remove the node from its current place in the maps. 4600193323Sed if (InsertPos) 4601193323Sed if (!RemoveNodeFromCSEMaps(N)) 4602193323Sed InsertPos = 0; 4603193323Sed 4604193323Sed // Now we update the operands. 4605193323Sed if (N->OperandList[0] != Op1) 4606193323Sed N->OperandList[0].set(Op1); 4607193323Sed if (N->OperandList[1] != Op2) 4608193323Sed N->OperandList[1].set(Op2); 4609193323Sed 4610193323Sed // If this gets put into a CSE map, add it. 4611193323Sed if (InsertPos) CSEMap.InsertNode(N, InsertPos); 4612210299Sed return N; 4613193323Sed} 4614193323Sed 4615210299SedSDNode *SelectionDAG:: 4616210299SedUpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) { 4617193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4618193323Sed return UpdateNodeOperands(N, Ops, 3); 4619193323Sed} 4620193323Sed 4621210299SedSDNode *SelectionDAG:: 4622210299SedUpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 4623193323Sed SDValue Op3, SDValue Op4) { 4624193323Sed SDValue Ops[] = { Op1, Op2, Op3, Op4 }; 4625193323Sed return UpdateNodeOperands(N, Ops, 4); 4626193323Sed} 4627193323Sed 4628210299SedSDNode *SelectionDAG:: 4629210299SedUpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 4630193323Sed SDValue Op3, SDValue Op4, SDValue Op5) { 4631193323Sed SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 }; 4632193323Sed return UpdateNodeOperands(N, Ops, 5); 4633193323Sed} 4634193323Sed 4635210299SedSDNode *SelectionDAG:: 4636210299SedUpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) { 4637193323Sed assert(N->getNumOperands() == NumOps && 4638193323Sed "Update with wrong number of operands"); 4639193323Sed 4640193323Sed // Check to see if there is no change. 4641193323Sed bool AnyChange = false; 4642193323Sed for (unsigned i = 0; i != NumOps; ++i) { 4643193323Sed if (Ops[i] != N->getOperand(i)) { 4644193323Sed AnyChange = true; 4645193323Sed break; 4646193323Sed } 4647193323Sed } 4648193323Sed 4649193323Sed // No operands changed, just return the input node. 4650210299Sed if (!AnyChange) return N; 4651193323Sed 4652193323Sed // See if the modified node already exists. 4653193323Sed void *InsertPos = 0; 4654193323Sed if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos)) 4655210299Sed return Existing; 4656193323Sed 4657193323Sed // Nope it doesn't. Remove the node from its current place in the maps. 4658193323Sed if (InsertPos) 4659193323Sed if (!RemoveNodeFromCSEMaps(N)) 4660193323Sed InsertPos = 0; 4661193323Sed 4662193323Sed // Now we update the operands. 4663193323Sed for (unsigned i = 0; i != NumOps; ++i) 4664193323Sed if (N->OperandList[i] != Ops[i]) 4665193323Sed N->OperandList[i].set(Ops[i]); 4666193323Sed 4667193323Sed // If this gets put into a CSE map, add it. 4668193323Sed if (InsertPos) CSEMap.InsertNode(N, InsertPos); 4669210299Sed return N; 4670193323Sed} 4671193323Sed 4672193323Sed/// DropOperands - Release the operands and set this node to have 4673193323Sed/// zero operands. 4674193323Sedvoid SDNode::DropOperands() { 4675193323Sed // Unlike the code in MorphNodeTo that does this, we don't need to 4676193323Sed // watch for dead nodes here. 4677193323Sed for (op_iterator I = op_begin(), E = op_end(); I != E; ) { 4678193323Sed SDUse &Use = *I++; 4679193323Sed Use.set(SDValue()); 4680193323Sed } 4681193323Sed} 4682193323Sed 4683193323Sed/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a 4684193323Sed/// machine opcode. 4685193323Sed/// 4686193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4687198090Srdivacky EVT VT) { 4688193323Sed SDVTList VTs = getVTList(VT); 4689193323Sed return SelectNodeTo(N, MachineOpc, VTs, 0, 0); 4690193323Sed} 4691193323Sed 4692193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4693198090Srdivacky EVT VT, SDValue Op1) { 4694193323Sed SDVTList VTs = getVTList(VT); 4695193323Sed SDValue Ops[] = { Op1 }; 4696193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 1); 4697193323Sed} 4698193323Sed 4699193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4700198090Srdivacky EVT VT, SDValue Op1, 4701193323Sed SDValue Op2) { 4702193323Sed SDVTList VTs = getVTList(VT); 4703193323Sed SDValue Ops[] = { Op1, Op2 }; 4704193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 2); 4705193323Sed} 4706193323Sed 4707193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4708198090Srdivacky EVT VT, SDValue Op1, 4709193323Sed SDValue Op2, SDValue Op3) { 4710193323Sed SDVTList VTs = getVTList(VT); 4711193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4712193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); 4713193323Sed} 4714193323Sed 4715193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4716198090Srdivacky EVT VT, const SDValue *Ops, 4717193323Sed unsigned NumOps) { 4718193323Sed SDVTList VTs = getVTList(VT); 4719193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4720193323Sed} 4721193323Sed 4722193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4723198090Srdivacky EVT VT1, EVT VT2, const SDValue *Ops, 4724193323Sed unsigned NumOps) { 4725193323Sed SDVTList VTs = getVTList(VT1, VT2); 4726193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4727193323Sed} 4728193323Sed 4729193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4730198090Srdivacky EVT VT1, EVT VT2) { 4731193323Sed SDVTList VTs = getVTList(VT1, VT2); 4732193323Sed return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0); 4733193323Sed} 4734193323Sed 4735193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4736198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4737193323Sed const SDValue *Ops, unsigned NumOps) { 4738193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4739193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4740193323Sed} 4741193323Sed 4742193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4743198090Srdivacky EVT VT1, EVT VT2, EVT VT3, EVT VT4, 4744193323Sed const SDValue *Ops, unsigned NumOps) { 4745193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); 4746193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); 4747193323Sed} 4748193323Sed 4749193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4750198090Srdivacky EVT VT1, EVT VT2, 4751193323Sed SDValue Op1) { 4752193323Sed SDVTList VTs = getVTList(VT1, VT2); 4753193323Sed SDValue Ops[] = { Op1 }; 4754193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 1); 4755193323Sed} 4756193323Sed 4757193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4758198090Srdivacky EVT VT1, EVT VT2, 4759193323Sed SDValue Op1, SDValue Op2) { 4760193323Sed SDVTList VTs = getVTList(VT1, VT2); 4761193323Sed SDValue Ops[] = { Op1, Op2 }; 4762193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 2); 4763193323Sed} 4764193323Sed 4765193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4766198090Srdivacky EVT VT1, EVT VT2, 4767193323Sed SDValue Op1, SDValue Op2, 4768193323Sed SDValue Op3) { 4769193323Sed SDVTList VTs = getVTList(VT1, VT2); 4770193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4771193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); 4772193323Sed} 4773193323Sed 4774193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4775198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4776193323Sed SDValue Op1, SDValue Op2, 4777193323Sed SDValue Op3) { 4778193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4779193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4780193323Sed return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); 4781193323Sed} 4782193323Sed 4783193323SedSDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 4784193323Sed SDVTList VTs, const SDValue *Ops, 4785193323Sed unsigned NumOps) { 4786204642Srdivacky N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps); 4787204642Srdivacky // Reset the NodeID to -1. 4788204642Srdivacky N->setNodeId(-1); 4789204642Srdivacky return N; 4790193323Sed} 4791193323Sed 4792204642Srdivacky/// MorphNodeTo - This *mutates* the specified node to have the specified 4793193323Sed/// return type, opcode, and operands. 4794193323Sed/// 4795193323Sed/// Note that MorphNodeTo returns the resultant node. If there is already a 4796193323Sed/// node of the specified opcode and operands, it returns that node instead of 4797193323Sed/// the current one. Note that the DebugLoc need not be the same. 4798193323Sed/// 4799193323Sed/// Using MorphNodeTo is faster than creating a new node and swapping it in 4800193323Sed/// with ReplaceAllUsesWith both because it often avoids allocating a new 4801193323Sed/// node, and because it doesn't require CSE recalculation for any of 4802193323Sed/// the node's users. 4803193323Sed/// 4804193323SedSDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc, 4805193323Sed SDVTList VTs, const SDValue *Ops, 4806193323Sed unsigned NumOps) { 4807193323Sed // If an identical node already exists, use it. 4808193323Sed void *IP = 0; 4809218893Sdim if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) { 4810193323Sed FoldingSetNodeID ID; 4811193323Sed AddNodeIDNode(ID, Opc, VTs, Ops, NumOps); 4812201360Srdivacky if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 4813193323Sed return ON; 4814193323Sed } 4815193323Sed 4816193323Sed if (!RemoveNodeFromCSEMaps(N)) 4817193323Sed IP = 0; 4818193323Sed 4819193323Sed // Start the morphing. 4820193323Sed N->NodeType = Opc; 4821193323Sed N->ValueList = VTs.VTs; 4822193323Sed N->NumValues = VTs.NumVTs; 4823193323Sed 4824193323Sed // Clear the operands list, updating used nodes to remove this from their 4825193323Sed // use list. Keep track of any operands that become dead as a result. 4826193323Sed SmallPtrSet<SDNode*, 16> DeadNodeSet; 4827193323Sed for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { 4828193323Sed SDUse &Use = *I++; 4829193323Sed SDNode *Used = Use.getNode(); 4830193323Sed Use.set(SDValue()); 4831193323Sed if (Used->use_empty()) 4832193323Sed DeadNodeSet.insert(Used); 4833193323Sed } 4834193323Sed 4835198090Srdivacky if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) { 4836198090Srdivacky // Initialize the memory references information. 4837198090Srdivacky MN->setMemRefs(0, 0); 4838198090Srdivacky // If NumOps is larger than the # of operands we can have in a 4839198090Srdivacky // MachineSDNode, reallocate the operand list. 4840198090Srdivacky if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) { 4841198090Srdivacky if (MN->OperandsNeedDelete) 4842198090Srdivacky delete[] MN->OperandList; 4843198090Srdivacky if (NumOps > array_lengthof(MN->LocalOperands)) 4844198090Srdivacky // We're creating a final node that will live unmorphed for the 4845198090Srdivacky // remainder of the current SelectionDAG iteration, so we can allocate 4846198090Srdivacky // the operands directly out of a pool with no recycling metadata. 4847198090Srdivacky MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), 4848205407Srdivacky Ops, NumOps); 4849198090Srdivacky else 4850198090Srdivacky MN->InitOperands(MN->LocalOperands, Ops, NumOps); 4851198090Srdivacky MN->OperandsNeedDelete = false; 4852198090Srdivacky } else 4853198090Srdivacky MN->InitOperands(MN->OperandList, Ops, NumOps); 4854198090Srdivacky } else { 4855198090Srdivacky // If NumOps is larger than the # of operands we currently have, reallocate 4856198090Srdivacky // the operand list. 4857198090Srdivacky if (NumOps > N->NumOperands) { 4858198090Srdivacky if (N->OperandsNeedDelete) 4859198090Srdivacky delete[] N->OperandList; 4860198090Srdivacky N->InitOperands(new SDUse[NumOps], Ops, NumOps); 4861193323Sed N->OperandsNeedDelete = true; 4862198090Srdivacky } else 4863198396Srdivacky N->InitOperands(N->OperandList, Ops, NumOps); 4864193323Sed } 4865193323Sed 4866193323Sed // Delete any nodes that are still dead after adding the uses for the 4867193323Sed // new operands. 4868204642Srdivacky if (!DeadNodeSet.empty()) { 4869204642Srdivacky SmallVector<SDNode *, 16> DeadNodes; 4870204642Srdivacky for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(), 4871204642Srdivacky E = DeadNodeSet.end(); I != E; ++I) 4872204642Srdivacky if ((*I)->use_empty()) 4873204642Srdivacky DeadNodes.push_back(*I); 4874204642Srdivacky RemoveDeadNodes(DeadNodes); 4875204642Srdivacky } 4876193323Sed 4877193323Sed if (IP) 4878193323Sed CSEMap.InsertNode(N, IP); // Memoize the new node. 4879193323Sed return N; 4880193323Sed} 4881193323Sed 4882193323Sed 4883198090Srdivacky/// getMachineNode - These are used for target selectors to create a new node 4884198090Srdivacky/// with specified return type(s), MachineInstr opcode, and operands. 4885193323Sed/// 4886198090Srdivacky/// Note that getMachineNode returns the resultant node. If there is already a 4887193323Sed/// node of the specified opcode and operands, it returns that node instead of 4888193323Sed/// the current one. 4889198090SrdivackyMachineSDNode * 4890198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) { 4891198090Srdivacky SDVTList VTs = getVTList(VT); 4892198090Srdivacky return getMachineNode(Opcode, dl, VTs, 0, 0); 4893193323Sed} 4894193323Sed 4895198090SrdivackyMachineSDNode * 4896198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) { 4897198090Srdivacky SDVTList VTs = getVTList(VT); 4898198090Srdivacky SDValue Ops[] = { Op1 }; 4899198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4900193323Sed} 4901193323Sed 4902198090SrdivackyMachineSDNode * 4903198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, 4904198090Srdivacky SDValue Op1, SDValue Op2) { 4905198090Srdivacky SDVTList VTs = getVTList(VT); 4906198090Srdivacky SDValue Ops[] = { Op1, Op2 }; 4907198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4908193323Sed} 4909193323Sed 4910198090SrdivackyMachineSDNode * 4911198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, 4912198090Srdivacky SDValue Op1, SDValue Op2, SDValue Op3) { 4913198090Srdivacky SDVTList VTs = getVTList(VT); 4914198090Srdivacky SDValue Ops[] = { Op1, Op2, Op3 }; 4915198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4916193323Sed} 4917193323Sed 4918198090SrdivackyMachineSDNode * 4919198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, 4920198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4921198090Srdivacky SDVTList VTs = getVTList(VT); 4922198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4923193323Sed} 4924193323Sed 4925198090SrdivackyMachineSDNode * 4926198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) { 4927193323Sed SDVTList VTs = getVTList(VT1, VT2); 4928198090Srdivacky return getMachineNode(Opcode, dl, VTs, 0, 0); 4929193323Sed} 4930193323Sed 4931198090SrdivackyMachineSDNode * 4932198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4933198090Srdivacky EVT VT1, EVT VT2, SDValue Op1) { 4934193323Sed SDVTList VTs = getVTList(VT1, VT2); 4935198090Srdivacky SDValue Ops[] = { Op1 }; 4936198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4937193323Sed} 4938193323Sed 4939198090SrdivackyMachineSDNode * 4940198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4941198090Srdivacky EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) { 4942193323Sed SDVTList VTs = getVTList(VT1, VT2); 4943193323Sed SDValue Ops[] = { Op1, Op2 }; 4944198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4945193323Sed} 4946193323Sed 4947198090SrdivackyMachineSDNode * 4948198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4949198090Srdivacky EVT VT1, EVT VT2, SDValue Op1, 4950198090Srdivacky SDValue Op2, SDValue Op3) { 4951193323Sed SDVTList VTs = getVTList(VT1, VT2); 4952193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4953198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4954193323Sed} 4955193323Sed 4956198090SrdivackyMachineSDNode * 4957198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4958198090Srdivacky EVT VT1, EVT VT2, 4959198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4960193323Sed SDVTList VTs = getVTList(VT1, VT2); 4961198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4962193323Sed} 4963193323Sed 4964198090SrdivackyMachineSDNode * 4965198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4966198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4967198090Srdivacky SDValue Op1, SDValue Op2) { 4968193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4969193323Sed SDValue Ops[] = { Op1, Op2 }; 4970198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4971193323Sed} 4972193323Sed 4973198090SrdivackyMachineSDNode * 4974198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4975198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4976198090Srdivacky SDValue Op1, SDValue Op2, SDValue Op3) { 4977193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4978193323Sed SDValue Ops[] = { Op1, Op2, Op3 }; 4979198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops)); 4980193323Sed} 4981193323Sed 4982198090SrdivackyMachineSDNode * 4983198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 4984198090Srdivacky EVT VT1, EVT VT2, EVT VT3, 4985198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4986193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3); 4987198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4988193323Sed} 4989193323Sed 4990198090SrdivackyMachineSDNode * 4991198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, 4992198090Srdivacky EVT VT2, EVT VT3, EVT VT4, 4993198090Srdivacky const SDValue *Ops, unsigned NumOps) { 4994193323Sed SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); 4995198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 4996193323Sed} 4997193323Sed 4998198090SrdivackyMachineSDNode * 4999198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, 5000198090Srdivacky const std::vector<EVT> &ResultTys, 5001198090Srdivacky const SDValue *Ops, unsigned NumOps) { 5002198090Srdivacky SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size()); 5003198090Srdivacky return getMachineNode(Opcode, dl, VTs, Ops, NumOps); 5004193323Sed} 5005193323Sed 5006198090SrdivackyMachineSDNode * 5007198090SrdivackySelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, 5008198090Srdivacky const SDValue *Ops, unsigned NumOps) { 5009218893Sdim bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue; 5010198090Srdivacky MachineSDNode *N; 5011218893Sdim void *IP = 0; 5012198090Srdivacky 5013198090Srdivacky if (DoCSE) { 5014198090Srdivacky FoldingSetNodeID ID; 5015198090Srdivacky AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps); 5016198090Srdivacky IP = 0; 5017201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 5018198090Srdivacky return cast<MachineSDNode>(E); 5019198090Srdivacky } 5020198090Srdivacky 5021198090Srdivacky // Allocate a new MachineSDNode. 5022205407Srdivacky N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs); 5023198090Srdivacky 5024198090Srdivacky // Initialize the operands list. 5025198090Srdivacky if (NumOps > array_lengthof(N->LocalOperands)) 5026198090Srdivacky // We're creating a final node that will live unmorphed for the 5027198090Srdivacky // remainder of the current SelectionDAG iteration, so we can allocate 5028198090Srdivacky // the operands directly out of a pool with no recycling metadata. 5029198090Srdivacky N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), 5030198090Srdivacky Ops, NumOps); 5031198090Srdivacky else 5032198090Srdivacky N->InitOperands(N->LocalOperands, Ops, NumOps); 5033198090Srdivacky N->OperandsNeedDelete = false; 5034198090Srdivacky 5035198090Srdivacky if (DoCSE) 5036198090Srdivacky CSEMap.InsertNode(N, IP); 5037198090Srdivacky 5038198090Srdivacky AllNodes.push_back(N); 5039198090Srdivacky#ifndef NDEBUG 5040218893Sdim VerifyMachineNode(N); 5041198090Srdivacky#endif 5042198090Srdivacky return N; 5043198090Srdivacky} 5044198090Srdivacky 5045198090Srdivacky/// getTargetExtractSubreg - A convenience function for creating 5046203954Srdivacky/// TargetOpcode::EXTRACT_SUBREG nodes. 5047198090SrdivackySDValue 5048198090SrdivackySelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT, 5049198090Srdivacky SDValue Operand) { 5050198090Srdivacky SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32); 5051203954Srdivacky SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, 5052198090Srdivacky VT, Operand, SRIdxVal); 5053198090Srdivacky return SDValue(Subreg, 0); 5054198090Srdivacky} 5055198090Srdivacky 5056198090Srdivacky/// getTargetInsertSubreg - A convenience function for creating 5057203954Srdivacky/// TargetOpcode::INSERT_SUBREG nodes. 5058198090SrdivackySDValue 5059198090SrdivackySelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT, 5060198090Srdivacky SDValue Operand, SDValue Subreg) { 5061198090Srdivacky SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32); 5062203954Srdivacky SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL, 5063198090Srdivacky VT, Operand, Subreg, SRIdxVal); 5064198090Srdivacky return SDValue(Result, 0); 5065198090Srdivacky} 5066198090Srdivacky 5067193323Sed/// getNodeIfExists - Get the specified node if it's already available, or 5068193323Sed/// else return NULL. 5069193323SedSDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList, 5070193323Sed const SDValue *Ops, unsigned NumOps) { 5071218893Sdim if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { 5072193323Sed FoldingSetNodeID ID; 5073193323Sed AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 5074193323Sed void *IP = 0; 5075201360Srdivacky if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 5076193323Sed return E; 5077193323Sed } 5078193323Sed return NULL; 5079193323Sed} 5080193323Sed 5081206083Srdivacky/// getDbgValue - Creates a SDDbgValue node. 5082206083Srdivacky/// 5083206083SrdivackySDDbgValue * 5084206083SrdivackySelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off, 5085206083Srdivacky DebugLoc DL, unsigned O) { 5086206083Srdivacky return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O); 5087206083Srdivacky} 5088206083Srdivacky 5089206083SrdivackySDDbgValue * 5090207618SrdivackySelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off, 5091206083Srdivacky DebugLoc DL, unsigned O) { 5092206083Srdivacky return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O); 5093206083Srdivacky} 5094206083Srdivacky 5095206083SrdivackySDDbgValue * 5096206083SrdivackySelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off, 5097206083Srdivacky DebugLoc DL, unsigned O) { 5098206083Srdivacky return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O); 5099206083Srdivacky} 5100206083Srdivacky 5101204792Srdivackynamespace { 5102204792Srdivacky 5103204792Srdivacky/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node 5104204792Srdivacky/// pointed to by a use iterator is deleted, increment the use iterator 5105204792Srdivacky/// so that it doesn't dangle. 5106204792Srdivacky/// 5107204792Srdivacky/// This class also manages a "downlink" DAGUpdateListener, to forward 5108204792Srdivacky/// messages to ReplaceAllUsesWith's callers. 5109204792Srdivacky/// 5110204792Srdivackyclass RAUWUpdateListener : public SelectionDAG::DAGUpdateListener { 5111204792Srdivacky SelectionDAG::DAGUpdateListener *DownLink; 5112204792Srdivacky SDNode::use_iterator &UI; 5113204792Srdivacky SDNode::use_iterator &UE; 5114204792Srdivacky 5115204792Srdivacky virtual void NodeDeleted(SDNode *N, SDNode *E) { 5116204792Srdivacky // Increment the iterator as needed. 5117204792Srdivacky while (UI != UE && N == *UI) 5118204792Srdivacky ++UI; 5119204792Srdivacky 5120204792Srdivacky // Then forward the message. 5121204792Srdivacky if (DownLink) DownLink->NodeDeleted(N, E); 5122204792Srdivacky } 5123204792Srdivacky 5124204792Srdivacky virtual void NodeUpdated(SDNode *N) { 5125204792Srdivacky // Just forward the message. 5126204792Srdivacky if (DownLink) DownLink->NodeUpdated(N); 5127204792Srdivacky } 5128204792Srdivacky 5129204792Srdivackypublic: 5130204792Srdivacky RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl, 5131204792Srdivacky SDNode::use_iterator &ui, 5132204792Srdivacky SDNode::use_iterator &ue) 5133204792Srdivacky : DownLink(dl), UI(ui), UE(ue) {} 5134204792Srdivacky}; 5135204792Srdivacky 5136204792Srdivacky} 5137204792Srdivacky 5138193323Sed/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 5139193323Sed/// This can cause recursive merging of nodes in the DAG. 5140193323Sed/// 5141193323Sed/// This version assumes From has a single result value. 5142193323Sed/// 5143193323Sedvoid SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To, 5144193323Sed DAGUpdateListener *UpdateListener) { 5145193323Sed SDNode *From = FromN.getNode(); 5146193323Sed assert(From->getNumValues() == 1 && FromN.getResNo() == 0 && 5147193323Sed "Cannot replace with this method!"); 5148193323Sed assert(From != To.getNode() && "Cannot replace uses of with self"); 5149193323Sed 5150193323Sed // Iterate over all the existing uses of From. New uses will be added 5151193323Sed // to the beginning of the use list, which we avoid visiting. 5152193323Sed // This specifically avoids visiting uses of From that arise while the 5153193323Sed // replacement is happening, because any such uses would be the result 5154193323Sed // of CSE: If an existing node looks like From after one of its operands 5155193323Sed // is replaced by To, we don't want to replace of all its users with To 5156193323Sed // too. See PR3018 for more info. 5157193323Sed SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 5158204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 5159193323Sed while (UI != UE) { 5160193323Sed SDNode *User = *UI; 5161193323Sed 5162193323Sed // This node is about to morph, remove its old self from the CSE maps. 5163193323Sed RemoveNodeFromCSEMaps(User); 5164193323Sed 5165193323Sed // A user can appear in a use list multiple times, and when this 5166193323Sed // happens the uses are usually next to each other in the list. 5167193323Sed // To help reduce the number of CSE recomputations, process all 5168193323Sed // the uses of this user that we can find this way. 5169193323Sed do { 5170193323Sed SDUse &Use = UI.getUse(); 5171193323Sed ++UI; 5172193323Sed Use.set(To); 5173193323Sed } while (UI != UE && *UI == User); 5174193323Sed 5175193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5176193323Sed // already exists there, recursively merge the results together. 5177204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 5178193323Sed } 5179193323Sed} 5180193323Sed 5181193323Sed/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 5182193323Sed/// This can cause recursive merging of nodes in the DAG. 5183193323Sed/// 5184193323Sed/// This version assumes that for each value of From, there is a 5185193323Sed/// corresponding value in To in the same position with the same type. 5186193323Sed/// 5187193323Sedvoid SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 5188193323Sed DAGUpdateListener *UpdateListener) { 5189193323Sed#ifndef NDEBUG 5190193323Sed for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) 5191193323Sed assert((!From->hasAnyUseOfValue(i) || 5192193323Sed From->getValueType(i) == To->getValueType(i)) && 5193193323Sed "Cannot use this version of ReplaceAllUsesWith!"); 5194193323Sed#endif 5195193323Sed 5196193323Sed // Handle the trivial case. 5197193323Sed if (From == To) 5198193323Sed return; 5199193323Sed 5200193323Sed // Iterate over just the existing users of From. See the comments in 5201193323Sed // the ReplaceAllUsesWith above. 5202193323Sed SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 5203204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 5204193323Sed while (UI != UE) { 5205193323Sed SDNode *User = *UI; 5206193323Sed 5207193323Sed // This node is about to morph, remove its old self from the CSE maps. 5208193323Sed RemoveNodeFromCSEMaps(User); 5209193323Sed 5210193323Sed // A user can appear in a use list multiple times, and when this 5211193323Sed // happens the uses are usually next to each other in the list. 5212193323Sed // To help reduce the number of CSE recomputations, process all 5213193323Sed // the uses of this user that we can find this way. 5214193323Sed do { 5215193323Sed SDUse &Use = UI.getUse(); 5216193323Sed ++UI; 5217193323Sed Use.setNode(To); 5218193323Sed } while (UI != UE && *UI == User); 5219193323Sed 5220193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5221193323Sed // already exists there, recursively merge the results together. 5222204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 5223193323Sed } 5224193323Sed} 5225193323Sed 5226193323Sed/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 5227193323Sed/// This can cause recursive merging of nodes in the DAG. 5228193323Sed/// 5229193323Sed/// This version can replace From with any result values. To must match the 5230193323Sed/// number and types of values returned by From. 5231193323Sedvoid SelectionDAG::ReplaceAllUsesWith(SDNode *From, 5232193323Sed const SDValue *To, 5233193323Sed DAGUpdateListener *UpdateListener) { 5234193323Sed if (From->getNumValues() == 1) // Handle the simple case efficiently. 5235193323Sed return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener); 5236193323Sed 5237193323Sed // Iterate over just the existing users of From. See the comments in 5238193323Sed // the ReplaceAllUsesWith above. 5239193323Sed SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 5240204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 5241193323Sed while (UI != UE) { 5242193323Sed SDNode *User = *UI; 5243193323Sed 5244193323Sed // This node is about to morph, remove its old self from the CSE maps. 5245193323Sed RemoveNodeFromCSEMaps(User); 5246193323Sed 5247193323Sed // A user can appear in a use list multiple times, and when this 5248193323Sed // happens the uses are usually next to each other in the list. 5249193323Sed // To help reduce the number of CSE recomputations, process all 5250193323Sed // the uses of this user that we can find this way. 5251193323Sed do { 5252193323Sed SDUse &Use = UI.getUse(); 5253193323Sed const SDValue &ToOp = To[Use.getResNo()]; 5254193323Sed ++UI; 5255193323Sed Use.set(ToOp); 5256193323Sed } while (UI != UE && *UI == User); 5257193323Sed 5258193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5259193323Sed // already exists there, recursively merge the results together. 5260204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 5261193323Sed } 5262193323Sed} 5263193323Sed 5264193323Sed/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving 5265193323Sed/// uses of other values produced by From.getNode() alone. The Deleted 5266193323Sed/// vector is handled the same way as for ReplaceAllUsesWith. 5267193323Sedvoid SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To, 5268193323Sed DAGUpdateListener *UpdateListener){ 5269193323Sed // Handle the really simple, really trivial case efficiently. 5270193323Sed if (From == To) return; 5271193323Sed 5272193323Sed // Handle the simple, trivial, case efficiently. 5273193323Sed if (From.getNode()->getNumValues() == 1) { 5274193323Sed ReplaceAllUsesWith(From, To, UpdateListener); 5275193323Sed return; 5276193323Sed } 5277193323Sed 5278193323Sed // Iterate over just the existing users of From. See the comments in 5279193323Sed // the ReplaceAllUsesWith above. 5280193323Sed SDNode::use_iterator UI = From.getNode()->use_begin(), 5281193323Sed UE = From.getNode()->use_end(); 5282204792Srdivacky RAUWUpdateListener Listener(UpdateListener, UI, UE); 5283193323Sed while (UI != UE) { 5284193323Sed SDNode *User = *UI; 5285193323Sed bool UserRemovedFromCSEMaps = false; 5286193323Sed 5287193323Sed // A user can appear in a use list multiple times, and when this 5288193323Sed // happens the uses are usually next to each other in the list. 5289193323Sed // To help reduce the number of CSE recomputations, process all 5290193323Sed // the uses of this user that we can find this way. 5291193323Sed do { 5292193323Sed SDUse &Use = UI.getUse(); 5293193323Sed 5294193323Sed // Skip uses of different values from the same node. 5295193323Sed if (Use.getResNo() != From.getResNo()) { 5296193323Sed ++UI; 5297193323Sed continue; 5298193323Sed } 5299193323Sed 5300193323Sed // If this node hasn't been modified yet, it's still in the CSE maps, 5301193323Sed // so remove its old self from the CSE maps. 5302193323Sed if (!UserRemovedFromCSEMaps) { 5303193323Sed RemoveNodeFromCSEMaps(User); 5304193323Sed UserRemovedFromCSEMaps = true; 5305193323Sed } 5306193323Sed 5307193323Sed ++UI; 5308193323Sed Use.set(To); 5309193323Sed } while (UI != UE && *UI == User); 5310193323Sed 5311193323Sed // We are iterating over all uses of the From node, so if a use 5312193323Sed // doesn't use the specific value, no changes are made. 5313193323Sed if (!UserRemovedFromCSEMaps) 5314193323Sed continue; 5315193323Sed 5316193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5317193323Sed // already exists there, recursively merge the results together. 5318204792Srdivacky AddModifiedNodeToCSEMaps(User, &Listener); 5319193323Sed } 5320193323Sed} 5321193323Sed 5322193323Sednamespace { 5323193323Sed /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith 5324193323Sed /// to record information about a use. 5325193323Sed struct UseMemo { 5326193323Sed SDNode *User; 5327193323Sed unsigned Index; 5328193323Sed SDUse *Use; 5329193323Sed }; 5330193323Sed 5331193323Sed /// operator< - Sort Memos by User. 5332193323Sed bool operator<(const UseMemo &L, const UseMemo &R) { 5333193323Sed return (intptr_t)L.User < (intptr_t)R.User; 5334193323Sed } 5335193323Sed} 5336193323Sed 5337193323Sed/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving 5338193323Sed/// uses of other values produced by From.getNode() alone. The same value 5339193323Sed/// may appear in both the From and To list. The Deleted vector is 5340193323Sed/// handled the same way as for ReplaceAllUsesWith. 5341193323Sedvoid SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From, 5342193323Sed const SDValue *To, 5343193323Sed unsigned Num, 5344193323Sed DAGUpdateListener *UpdateListener){ 5345193323Sed // Handle the simple, trivial case efficiently. 5346193323Sed if (Num == 1) 5347193323Sed return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener); 5348193323Sed 5349193323Sed // Read up all the uses and make records of them. This helps 5350193323Sed // processing new uses that are introduced during the 5351193323Sed // replacement process. 5352193323Sed SmallVector<UseMemo, 4> Uses; 5353193323Sed for (unsigned i = 0; i != Num; ++i) { 5354193323Sed unsigned FromResNo = From[i].getResNo(); 5355193323Sed SDNode *FromNode = From[i].getNode(); 5356193323Sed for (SDNode::use_iterator UI = FromNode->use_begin(), 5357193323Sed E = FromNode->use_end(); UI != E; ++UI) { 5358193323Sed SDUse &Use = UI.getUse(); 5359193323Sed if (Use.getResNo() == FromResNo) { 5360193323Sed UseMemo Memo = { *UI, i, &Use }; 5361193323Sed Uses.push_back(Memo); 5362193323Sed } 5363193323Sed } 5364193323Sed } 5365193323Sed 5366193323Sed // Sort the uses, so that all the uses from a given User are together. 5367193323Sed std::sort(Uses.begin(), Uses.end()); 5368193323Sed 5369193323Sed for (unsigned UseIndex = 0, UseIndexEnd = Uses.size(); 5370193323Sed UseIndex != UseIndexEnd; ) { 5371193323Sed // We know that this user uses some value of From. If it is the right 5372193323Sed // value, update it. 5373193323Sed SDNode *User = Uses[UseIndex].User; 5374193323Sed 5375193323Sed // This node is about to morph, remove its old self from the CSE maps. 5376193323Sed RemoveNodeFromCSEMaps(User); 5377193323Sed 5378193323Sed // The Uses array is sorted, so all the uses for a given User 5379193323Sed // are next to each other in the list. 5380193323Sed // To help reduce the number of CSE recomputations, process all 5381193323Sed // the uses of this user that we can find this way. 5382193323Sed do { 5383193323Sed unsigned i = Uses[UseIndex].Index; 5384193323Sed SDUse &Use = *Uses[UseIndex].Use; 5385193323Sed ++UseIndex; 5386193323Sed 5387193323Sed Use.set(To[i]); 5388193323Sed } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User); 5389193323Sed 5390193323Sed // Now that we have modified User, add it back to the CSE maps. If it 5391193323Sed // already exists there, recursively merge the results together. 5392193323Sed AddModifiedNodeToCSEMaps(User, UpdateListener); 5393193323Sed } 5394193323Sed} 5395193323Sed 5396193323Sed/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG 5397193323Sed/// based on their topological order. It returns the maximum id and a vector 5398193323Sed/// of the SDNodes* in assigned order by reference. 5399193323Sedunsigned SelectionDAG::AssignTopologicalOrder() { 5400193323Sed 5401193323Sed unsigned DAGSize = 0; 5402193323Sed 5403193323Sed // SortedPos tracks the progress of the algorithm. Nodes before it are 5404193323Sed // sorted, nodes after it are unsorted. When the algorithm completes 5405193323Sed // it is at the end of the list. 5406193323Sed allnodes_iterator SortedPos = allnodes_begin(); 5407193323Sed 5408193323Sed // Visit all the nodes. Move nodes with no operands to the front of 5409193323Sed // the list immediately. Annotate nodes that do have operands with their 5410193323Sed // operand count. Before we do this, the Node Id fields of the nodes 5411193323Sed // may contain arbitrary values. After, the Node Id fields for nodes 5412193323Sed // before SortedPos will contain the topological sort index, and the 5413193323Sed // Node Id fields for nodes At SortedPos and after will contain the 5414193323Sed // count of outstanding operands. 5415193323Sed for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) { 5416193323Sed SDNode *N = I++; 5417202878Srdivacky checkForCycles(N); 5418193323Sed unsigned Degree = N->getNumOperands(); 5419193323Sed if (Degree == 0) { 5420193323Sed // A node with no uses, add it to the result array immediately. 5421193323Sed N->setNodeId(DAGSize++); 5422193323Sed allnodes_iterator Q = N; 5423193323Sed if (Q != SortedPos) 5424193323Sed SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q)); 5425202878Srdivacky assert(SortedPos != AllNodes.end() && "Overran node list"); 5426193323Sed ++SortedPos; 5427193323Sed } else { 5428193323Sed // Temporarily use the Node Id as scratch space for the degree count. 5429193323Sed N->setNodeId(Degree); 5430193323Sed } 5431193323Sed } 5432193323Sed 5433193323Sed // Visit all the nodes. As we iterate, moves nodes into sorted order, 5434193323Sed // such that by the time the end is reached all nodes will be sorted. 5435193323Sed for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) { 5436193323Sed SDNode *N = I; 5437202878Srdivacky checkForCycles(N); 5438202878Srdivacky // N is in sorted position, so all its uses have one less operand 5439202878Srdivacky // that needs to be sorted. 5440193323Sed for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 5441193323Sed UI != UE; ++UI) { 5442193323Sed SDNode *P = *UI; 5443193323Sed unsigned Degree = P->getNodeId(); 5444202878Srdivacky assert(Degree != 0 && "Invalid node degree"); 5445193323Sed --Degree; 5446193323Sed if (Degree == 0) { 5447193323Sed // All of P's operands are sorted, so P may sorted now. 5448193323Sed P->setNodeId(DAGSize++); 5449193323Sed if (P != SortedPos) 5450193323Sed SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P)); 5451202878Srdivacky assert(SortedPos != AllNodes.end() && "Overran node list"); 5452193323Sed ++SortedPos; 5453193323Sed } else { 5454193323Sed // Update P's outstanding operand count. 5455193323Sed P->setNodeId(Degree); 5456193323Sed } 5457193323Sed } 5458202878Srdivacky if (I == SortedPos) { 5459203954Srdivacky#ifndef NDEBUG 5460203954Srdivacky SDNode *S = ++I; 5461203954Srdivacky dbgs() << "Overran sorted position:\n"; 5462202878Srdivacky S->dumprFull(); 5463203954Srdivacky#endif 5464203954Srdivacky llvm_unreachable(0); 5465202878Srdivacky } 5466193323Sed } 5467193323Sed 5468193323Sed assert(SortedPos == AllNodes.end() && 5469193323Sed "Topological sort incomplete!"); 5470193323Sed assert(AllNodes.front().getOpcode() == ISD::EntryToken && 5471193323Sed "First node in topological sort is not the entry token!"); 5472193323Sed assert(AllNodes.front().getNodeId() == 0 && 5473193323Sed "First node in topological sort has non-zero id!"); 5474193323Sed assert(AllNodes.front().getNumOperands() == 0 && 5475193323Sed "First node in topological sort has operands!"); 5476193323Sed assert(AllNodes.back().getNodeId() == (int)DAGSize-1 && 5477193323Sed "Last node in topologic sort has unexpected id!"); 5478193323Sed assert(AllNodes.back().use_empty() && 5479193323Sed "Last node in topologic sort has users!"); 5480193323Sed assert(DAGSize == allnodes_size() && "Node count mismatch!"); 5481193323Sed return DAGSize; 5482193323Sed} 5483193323Sed 5484201360Srdivacky/// AssignOrdering - Assign an order to the SDNode. 5485203954Srdivackyvoid SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) { 5486201360Srdivacky assert(SD && "Trying to assign an order to a null node!"); 5487202878Srdivacky Ordering->add(SD, Order); 5488201360Srdivacky} 5489193323Sed 5490201360Srdivacky/// GetOrdering - Get the order for the SDNode. 5491201360Srdivackyunsigned SelectionDAG::GetOrdering(const SDNode *SD) const { 5492201360Srdivacky assert(SD && "Trying to get the order of a null node!"); 5493202878Srdivacky return Ordering->getOrder(SD); 5494201360Srdivacky} 5495193323Sed 5496206083Srdivacky/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the 5497206083Srdivacky/// value is produced by SD. 5498207618Srdivackyvoid SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) { 5499207618Srdivacky DbgInfo->add(DB, SD, isParameter); 5500206083Srdivacky if (SD) 5501206083Srdivacky SD->setHasDebugValue(true); 5502205218Srdivacky} 5503201360Srdivacky 5504218893Sdim/// TransferDbgValues - Transfer SDDbgValues. 5505218893Sdimvoid SelectionDAG::TransferDbgValues(SDValue From, SDValue To) { 5506218893Sdim if (From == To || !From.getNode()->getHasDebugValue()) 5507218893Sdim return; 5508218893Sdim SDNode *FromNode = From.getNode(); 5509218893Sdim SDNode *ToNode = To.getNode(); 5510218893Sdim SmallVector<SDDbgValue *, 2> &DVs = GetDbgValues(FromNode); 5511218893Sdim SmallVector<SDDbgValue *, 2> ClonedDVs; 5512218893Sdim for (SmallVector<SDDbgValue *, 2>::iterator I = DVs.begin(), E = DVs.end(); 5513218893Sdim I != E; ++I) { 5514218893Sdim SDDbgValue *Dbg = *I; 5515218893Sdim if (Dbg->getKind() == SDDbgValue::SDNODE) { 5516218893Sdim SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(), 5517218893Sdim Dbg->getOffset(), Dbg->getDebugLoc(), 5518218893Sdim Dbg->getOrder()); 5519218893Sdim ClonedDVs.push_back(Clone); 5520218893Sdim } 5521218893Sdim } 5522218893Sdim for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(), 5523218893Sdim E = ClonedDVs.end(); I != E; ++I) 5524218893Sdim AddDbgValue(*I, ToNode, false); 5525218893Sdim} 5526218893Sdim 5527193323Sed//===----------------------------------------------------------------------===// 5528193323Sed// SDNode Class 5529193323Sed//===----------------------------------------------------------------------===// 5530193323Sed 5531193323SedHandleSDNode::~HandleSDNode() { 5532193323Sed DropOperands(); 5533193323Sed} 5534193323Sed 5535210299SedGlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL, 5536210299Sed const GlobalValue *GA, 5537198090Srdivacky EVT VT, int64_t o, unsigned char TF) 5538210299Sed : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) { 5539207618Srdivacky TheGlobal = GA; 5540193323Sed} 5541193323Sed 5542198090SrdivackyMemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt, 5543198090Srdivacky MachineMemOperand *mmo) 5544198090Srdivacky : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) { 5545204642Srdivacky SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), 5546204642Srdivacky MMO->isNonTemporal()); 5547198090Srdivacky assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); 5548204642Srdivacky assert(isNonTemporal() == MMO->isNonTemporal() && 5549204642Srdivacky "Non-temporal encoding error!"); 5550198090Srdivacky assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!"); 5551193323Sed} 5552193323Sed 5553193323SedMemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, 5554218893Sdim const SDValue *Ops, unsigned NumOps, EVT memvt, 5555198090Srdivacky MachineMemOperand *mmo) 5556193323Sed : SDNode(Opc, dl, VTs, Ops, NumOps), 5557198090Srdivacky MemoryVT(memvt), MMO(mmo) { 5558204642Srdivacky SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), 5559204642Srdivacky MMO->isNonTemporal()); 5560198090Srdivacky assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); 5561198090Srdivacky assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!"); 5562193323Sed} 5563193323Sed 5564193323Sed/// Profile - Gather unique data for the node. 5565193323Sed/// 5566193323Sedvoid SDNode::Profile(FoldingSetNodeID &ID) const { 5567193323Sed AddNodeIDNode(ID, this); 5568193323Sed} 5569193323Sed 5570198090Srdivackynamespace { 5571198090Srdivacky struct EVTArray { 5572198090Srdivacky std::vector<EVT> VTs; 5573218893Sdim 5574198090Srdivacky EVTArray() { 5575198090Srdivacky VTs.reserve(MVT::LAST_VALUETYPE); 5576198090Srdivacky for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i) 5577198090Srdivacky VTs.push_back(MVT((MVT::SimpleValueType)i)); 5578198090Srdivacky } 5579198090Srdivacky }; 5580198090Srdivacky} 5581198090Srdivacky 5582198090Srdivackystatic ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs; 5583198090Srdivackystatic ManagedStatic<EVTArray> SimpleVTArray; 5584195098Sedstatic ManagedStatic<sys::SmartMutex<true> > VTMutex; 5585195098Sed 5586193323Sed/// getValueTypeList - Return a pointer to the specified value type. 5587193323Sed/// 5588198090Srdivackyconst EVT *SDNode::getValueTypeList(EVT VT) { 5589193323Sed if (VT.isExtended()) { 5590198090Srdivacky sys::SmartScopedLock<true> Lock(*VTMutex); 5591195098Sed return &(*EVTs->insert(VT).first); 5592193323Sed } else { 5593218893Sdim assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE && 5594208599Srdivacky "Value type out of range!"); 5595198090Srdivacky return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy]; 5596193323Sed } 5597193323Sed} 5598193323Sed 5599193323Sed/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 5600193323Sed/// indicated value. This method ignores uses of other values defined by this 5601193323Sed/// operation. 5602193323Sedbool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { 5603193323Sed assert(Value < getNumValues() && "Bad value!"); 5604193323Sed 5605193323Sed // TODO: Only iterate over uses of a given value of the node 5606193323Sed for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) { 5607193323Sed if (UI.getUse().getResNo() == Value) { 5608193323Sed if (NUses == 0) 5609193323Sed return false; 5610193323Sed --NUses; 5611193323Sed } 5612193323Sed } 5613193323Sed 5614193323Sed // Found exactly the right number of uses? 5615193323Sed return NUses == 0; 5616193323Sed} 5617193323Sed 5618193323Sed 5619193323Sed/// hasAnyUseOfValue - Return true if there are any use of the indicated 5620193323Sed/// value. This method ignores uses of other values defined by this operation. 5621193323Sedbool SDNode::hasAnyUseOfValue(unsigned Value) const { 5622193323Sed assert(Value < getNumValues() && "Bad value!"); 5623193323Sed 5624193323Sed for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) 5625193323Sed if (UI.getUse().getResNo() == Value) 5626193323Sed return true; 5627193323Sed 5628193323Sed return false; 5629193323Sed} 5630193323Sed 5631193323Sed 5632193323Sed/// isOnlyUserOf - Return true if this node is the only use of N. 5633193323Sed/// 5634193323Sedbool SDNode::isOnlyUserOf(SDNode *N) const { 5635193323Sed bool Seen = false; 5636193323Sed for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 5637193323Sed SDNode *User = *I; 5638193323Sed if (User == this) 5639193323Sed Seen = true; 5640193323Sed else 5641193323Sed return false; 5642193323Sed } 5643193323Sed 5644193323Sed return Seen; 5645193323Sed} 5646193323Sed 5647193323Sed/// isOperand - Return true if this node is an operand of N. 5648193323Sed/// 5649193323Sedbool SDValue::isOperandOf(SDNode *N) const { 5650193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 5651193323Sed if (*this == N->getOperand(i)) 5652193323Sed return true; 5653193323Sed return false; 5654193323Sed} 5655193323Sed 5656193323Sedbool SDNode::isOperandOf(SDNode *N) const { 5657193323Sed for (unsigned i = 0, e = N->NumOperands; i != e; ++i) 5658193323Sed if (this == N->OperandList[i].getNode()) 5659193323Sed return true; 5660193323Sed return false; 5661193323Sed} 5662193323Sed 5663193323Sed/// reachesChainWithoutSideEffects - Return true if this operand (which must 5664193323Sed/// be a chain) reaches the specified operand without crossing any 5665218893Sdim/// side-effecting instructions on any chain path. In practice, this looks 5666218893Sdim/// through token factors and non-volatile loads. In order to remain efficient, 5667218893Sdim/// this only looks a couple of nodes in, it does not do an exhaustive search. 5668193323Sedbool SDValue::reachesChainWithoutSideEffects(SDValue Dest, 5669193323Sed unsigned Depth) const { 5670193323Sed if (*this == Dest) return true; 5671193323Sed 5672193323Sed // Don't search too deeply, we just want to be able to see through 5673193323Sed // TokenFactor's etc. 5674193323Sed if (Depth == 0) return false; 5675193323Sed 5676193323Sed // If this is a token factor, all inputs to the TF happen in parallel. If any 5677218893Sdim // of the operands of the TF does not reach dest, then we cannot do the xform. 5678193323Sed if (getOpcode() == ISD::TokenFactor) { 5679193323Sed for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 5680218893Sdim if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1)) 5681218893Sdim return false; 5682218893Sdim return true; 5683193323Sed } 5684193323Sed 5685193323Sed // Loads don't have side effects, look through them. 5686193323Sed if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) { 5687193323Sed if (!Ld->isVolatile()) 5688193323Sed return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1); 5689193323Sed } 5690193323Sed return false; 5691193323Sed} 5692193323Sed 5693193323Sed/// isPredecessorOf - Return true if this node is a predecessor of N. This node 5694198892Srdivacky/// is either an operand of N or it can be reached by traversing up the operands. 5695193323Sed/// NOTE: this is an expensive method. Use it carefully. 5696193323Sedbool SDNode::isPredecessorOf(SDNode *N) const { 5697193323Sed SmallPtrSet<SDNode *, 32> Visited; 5698198892Srdivacky SmallVector<SDNode *, 16> Worklist; 5699198892Srdivacky Worklist.push_back(N); 5700198892Srdivacky 5701198892Srdivacky do { 5702198892Srdivacky N = Worklist.pop_back_val(); 5703198892Srdivacky for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 5704198892Srdivacky SDNode *Op = N->getOperand(i).getNode(); 5705198892Srdivacky if (Op == this) 5706198892Srdivacky return true; 5707198892Srdivacky if (Visited.insert(Op)) 5708198892Srdivacky Worklist.push_back(Op); 5709198892Srdivacky } 5710198892Srdivacky } while (!Worklist.empty()); 5711198892Srdivacky 5712198892Srdivacky return false; 5713193323Sed} 5714193323Sed 5715193323Seduint64_t SDNode::getConstantOperandVal(unsigned Num) const { 5716193323Sed assert(Num < NumOperands && "Invalid child # of SDNode!"); 5717193323Sed return cast<ConstantSDNode>(OperandList[Num])->getZExtValue(); 5718193323Sed} 5719193323Sed 5720193323Sedstd::string SDNode::getOperationName(const SelectionDAG *G) const { 5721193323Sed switch (getOpcode()) { 5722193323Sed default: 5723193323Sed if (getOpcode() < ISD::BUILTIN_OP_END) 5724193323Sed return "<<Unknown DAG Node>>"; 5725193323Sed if (isMachineOpcode()) { 5726193323Sed if (G) 5727193323Sed if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 5728193323Sed if (getMachineOpcode() < TII->getNumOpcodes()) 5729193323Sed return TII->get(getMachineOpcode()).getName(); 5730204642Srdivacky return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>"; 5731193323Sed } 5732193323Sed if (G) { 5733193323Sed const TargetLowering &TLI = G->getTargetLoweringInfo(); 5734193323Sed const char *Name = TLI.getTargetNodeName(getOpcode()); 5735193323Sed if (Name) return Name; 5736204642Srdivacky return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>"; 5737193323Sed } 5738204642Srdivacky return "<<Unknown Node #" + utostr(getOpcode()) + ">>"; 5739193323Sed 5740193323Sed#ifndef NDEBUG 5741193323Sed case ISD::DELETED_NODE: 5742193323Sed return "<<Deleted Node!>>"; 5743193323Sed#endif 5744193323Sed case ISD::PREFETCH: return "Prefetch"; 5745193323Sed case ISD::MEMBARRIER: return "MemBarrier"; 5746193323Sed case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap"; 5747193323Sed case ISD::ATOMIC_SWAP: return "AtomicSwap"; 5748193323Sed case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd"; 5749193323Sed case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub"; 5750193323Sed case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd"; 5751193323Sed case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr"; 5752193323Sed case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor"; 5753193323Sed case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand"; 5754193323Sed case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin"; 5755193323Sed case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax"; 5756193323Sed case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin"; 5757193323Sed case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax"; 5758193323Sed case ISD::PCMARKER: return "PCMarker"; 5759193323Sed case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 5760193323Sed case ISD::SRCVALUE: return "SrcValue"; 5761207618Srdivacky case ISD::MDNODE_SDNODE: return "MDNode"; 5762193323Sed case ISD::EntryToken: return "EntryToken"; 5763193323Sed case ISD::TokenFactor: return "TokenFactor"; 5764193323Sed case ISD::AssertSext: return "AssertSext"; 5765193323Sed case ISD::AssertZext: return "AssertZext"; 5766193323Sed 5767193323Sed case ISD::BasicBlock: return "BasicBlock"; 5768193323Sed case ISD::VALUETYPE: return "ValueType"; 5769193323Sed case ISD::Register: return "Register"; 5770193323Sed 5771193323Sed case ISD::Constant: return "Constant"; 5772193323Sed case ISD::ConstantFP: return "ConstantFP"; 5773193323Sed case ISD::GlobalAddress: return "GlobalAddress"; 5774193323Sed case ISD::GlobalTLSAddress: return "GlobalTLSAddress"; 5775193323Sed case ISD::FrameIndex: return "FrameIndex"; 5776193323Sed case ISD::JumpTable: return "JumpTable"; 5777193323Sed case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE"; 5778193323Sed case ISD::RETURNADDR: return "RETURNADDR"; 5779193323Sed case ISD::FRAMEADDR: return "FRAMEADDR"; 5780193323Sed case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET"; 5781193323Sed case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR"; 5782198090Srdivacky case ISD::LSDAADDR: return "LSDAADDR"; 5783193323Sed case ISD::EHSELECTION: return "EHSELECTION"; 5784193323Sed case ISD::EH_RETURN: return "EH_RETURN"; 5785208599Srdivacky case ISD::EH_SJLJ_SETJMP: return "EH_SJLJ_SETJMP"; 5786208599Srdivacky case ISD::EH_SJLJ_LONGJMP: return "EH_SJLJ_LONGJMP"; 5787218893Sdim case ISD::EH_SJLJ_DISPATCHSETUP: return "EH_SJLJ_DISPATCHSETUP"; 5788193323Sed case ISD::ConstantPool: return "ConstantPool"; 5789193323Sed case ISD::ExternalSymbol: return "ExternalSymbol"; 5790198892Srdivacky case ISD::BlockAddress: return "BlockAddress"; 5791198396Srdivacky case ISD::INTRINSIC_WO_CHAIN: 5792193323Sed case ISD::INTRINSIC_VOID: 5793193323Sed case ISD::INTRINSIC_W_CHAIN: { 5794198396Srdivacky unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1; 5795198396Srdivacky unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue(); 5796198396Srdivacky if (IID < Intrinsic::num_intrinsics) 5797198396Srdivacky return Intrinsic::getName((Intrinsic::ID)IID); 5798198396Srdivacky else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo()) 5799198396Srdivacky return TII->getName(IID); 5800198396Srdivacky llvm_unreachable("Invalid intrinsic ID"); 5801193323Sed } 5802193323Sed 5803193323Sed case ISD::BUILD_VECTOR: return "BUILD_VECTOR"; 5804193323Sed case ISD::TargetConstant: return "TargetConstant"; 5805193323Sed case ISD::TargetConstantFP:return "TargetConstantFP"; 5806193323Sed case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 5807193323Sed case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress"; 5808193323Sed case ISD::TargetFrameIndex: return "TargetFrameIndex"; 5809193323Sed case ISD::TargetJumpTable: return "TargetJumpTable"; 5810193323Sed case ISD::TargetConstantPool: return "TargetConstantPool"; 5811193323Sed case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 5812198892Srdivacky case ISD::TargetBlockAddress: return "TargetBlockAddress"; 5813193323Sed 5814193323Sed case ISD::CopyToReg: return "CopyToReg"; 5815193323Sed case ISD::CopyFromReg: return "CopyFromReg"; 5816193323Sed case ISD::UNDEF: return "undef"; 5817193323Sed case ISD::MERGE_VALUES: return "merge_values"; 5818193323Sed case ISD::INLINEASM: return "inlineasm"; 5819193323Sed case ISD::EH_LABEL: return "eh_label"; 5820193323Sed case ISD::HANDLENODE: return "handlenode"; 5821193323Sed 5822193323Sed // Unary operators 5823193323Sed case ISD::FABS: return "fabs"; 5824193323Sed case ISD::FNEG: return "fneg"; 5825193323Sed case ISD::FSQRT: return "fsqrt"; 5826193323Sed case ISD::FSIN: return "fsin"; 5827193323Sed case ISD::FCOS: return "fcos"; 5828193323Sed case ISD::FTRUNC: return "ftrunc"; 5829193323Sed case ISD::FFLOOR: return "ffloor"; 5830193323Sed case ISD::FCEIL: return "fceil"; 5831193323Sed case ISD::FRINT: return "frint"; 5832193323Sed case ISD::FNEARBYINT: return "fnearbyint"; 5833210299Sed case ISD::FEXP: return "fexp"; 5834210299Sed case ISD::FEXP2: return "fexp2"; 5835210299Sed case ISD::FLOG: return "flog"; 5836210299Sed case ISD::FLOG2: return "flog2"; 5837210299Sed case ISD::FLOG10: return "flog10"; 5838193323Sed 5839193323Sed // Binary operators 5840193323Sed case ISD::ADD: return "add"; 5841193323Sed case ISD::SUB: return "sub"; 5842193323Sed case ISD::MUL: return "mul"; 5843193323Sed case ISD::MULHU: return "mulhu"; 5844193323Sed case ISD::MULHS: return "mulhs"; 5845193323Sed case ISD::SDIV: return "sdiv"; 5846193323Sed case ISD::UDIV: return "udiv"; 5847193323Sed case ISD::SREM: return "srem"; 5848193323Sed case ISD::UREM: return "urem"; 5849193323Sed case ISD::SMUL_LOHI: return "smul_lohi"; 5850193323Sed case ISD::UMUL_LOHI: return "umul_lohi"; 5851193323Sed case ISD::SDIVREM: return "sdivrem"; 5852193323Sed case ISD::UDIVREM: return "udivrem"; 5853193323Sed case ISD::AND: return "and"; 5854193323Sed case ISD::OR: return "or"; 5855193323Sed case ISD::XOR: return "xor"; 5856193323Sed case ISD::SHL: return "shl"; 5857193323Sed case ISD::SRA: return "sra"; 5858193323Sed case ISD::SRL: return "srl"; 5859193323Sed case ISD::ROTL: return "rotl"; 5860193323Sed case ISD::ROTR: return "rotr"; 5861193323Sed case ISD::FADD: return "fadd"; 5862193323Sed case ISD::FSUB: return "fsub"; 5863193323Sed case ISD::FMUL: return "fmul"; 5864193323Sed case ISD::FDIV: return "fdiv"; 5865193323Sed case ISD::FREM: return "frem"; 5866193323Sed case ISD::FCOPYSIGN: return "fcopysign"; 5867193323Sed case ISD::FGETSIGN: return "fgetsign"; 5868210299Sed case ISD::FPOW: return "fpow"; 5869193323Sed 5870210299Sed case ISD::FPOWI: return "fpowi"; 5871193323Sed case ISD::SETCC: return "setcc"; 5872193323Sed case ISD::VSETCC: return "vsetcc"; 5873193323Sed case ISD::SELECT: return "select"; 5874193323Sed case ISD::SELECT_CC: return "select_cc"; 5875193323Sed case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt"; 5876193323Sed case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt"; 5877193323Sed case ISD::CONCAT_VECTORS: return "concat_vectors"; 5878218893Sdim case ISD::INSERT_SUBVECTOR: return "insert_subvector"; 5879193323Sed case ISD::EXTRACT_SUBVECTOR: return "extract_subvector"; 5880193323Sed case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector"; 5881193323Sed case ISD::VECTOR_SHUFFLE: return "vector_shuffle"; 5882193323Sed case ISD::CARRY_FALSE: return "carry_false"; 5883193323Sed case ISD::ADDC: return "addc"; 5884193323Sed case ISD::ADDE: return "adde"; 5885193323Sed case ISD::SADDO: return "saddo"; 5886193323Sed case ISD::UADDO: return "uaddo"; 5887193323Sed case ISD::SSUBO: return "ssubo"; 5888193323Sed case ISD::USUBO: return "usubo"; 5889193323Sed case ISD::SMULO: return "smulo"; 5890193323Sed case ISD::UMULO: return "umulo"; 5891193323Sed case ISD::SUBC: return "subc"; 5892193323Sed case ISD::SUBE: return "sube"; 5893193323Sed case ISD::SHL_PARTS: return "shl_parts"; 5894193323Sed case ISD::SRA_PARTS: return "sra_parts"; 5895193323Sed case ISD::SRL_PARTS: return "srl_parts"; 5896193323Sed 5897193323Sed // Conversion operators. 5898193323Sed case ISD::SIGN_EXTEND: return "sign_extend"; 5899193323Sed case ISD::ZERO_EXTEND: return "zero_extend"; 5900193323Sed case ISD::ANY_EXTEND: return "any_extend"; 5901193323Sed case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 5902193323Sed case ISD::TRUNCATE: return "truncate"; 5903193323Sed case ISD::FP_ROUND: return "fp_round"; 5904193323Sed case ISD::FLT_ROUNDS_: return "flt_rounds"; 5905193323Sed case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 5906193323Sed case ISD::FP_EXTEND: return "fp_extend"; 5907193323Sed 5908193323Sed case ISD::SINT_TO_FP: return "sint_to_fp"; 5909193323Sed case ISD::UINT_TO_FP: return "uint_to_fp"; 5910193323Sed case ISD::FP_TO_SINT: return "fp_to_sint"; 5911193323Sed case ISD::FP_TO_UINT: return "fp_to_uint"; 5912218893Sdim case ISD::BITCAST: return "bit_convert"; 5913205218Srdivacky case ISD::FP16_TO_FP32: return "fp16_to_fp32"; 5914205218Srdivacky case ISD::FP32_TO_FP16: return "fp32_to_fp16"; 5915193323Sed 5916193323Sed case ISD::CONVERT_RNDSAT: { 5917193323Sed switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) { 5918198090Srdivacky default: llvm_unreachable("Unknown cvt code!"); 5919193323Sed case ISD::CVT_FF: return "cvt_ff"; 5920193323Sed case ISD::CVT_FS: return "cvt_fs"; 5921193323Sed case ISD::CVT_FU: return "cvt_fu"; 5922193323Sed case ISD::CVT_SF: return "cvt_sf"; 5923193323Sed case ISD::CVT_UF: return "cvt_uf"; 5924193323Sed case ISD::CVT_SS: return "cvt_ss"; 5925193323Sed case ISD::CVT_SU: return "cvt_su"; 5926193323Sed case ISD::CVT_US: return "cvt_us"; 5927193323Sed case ISD::CVT_UU: return "cvt_uu"; 5928193323Sed } 5929193323Sed } 5930193323Sed 5931193323Sed // Control flow instructions 5932193323Sed case ISD::BR: return "br"; 5933193323Sed case ISD::BRIND: return "brind"; 5934193323Sed case ISD::BR_JT: return "br_jt"; 5935193323Sed case ISD::BRCOND: return "brcond"; 5936193323Sed case ISD::BR_CC: return "br_cc"; 5937193323Sed case ISD::CALLSEQ_START: return "callseq_start"; 5938193323Sed case ISD::CALLSEQ_END: return "callseq_end"; 5939193323Sed 5940193323Sed // Other operators 5941193323Sed case ISD::LOAD: return "load"; 5942193323Sed case ISD::STORE: return "store"; 5943193323Sed case ISD::VAARG: return "vaarg"; 5944193323Sed case ISD::VACOPY: return "vacopy"; 5945193323Sed case ISD::VAEND: return "vaend"; 5946193323Sed case ISD::VASTART: return "vastart"; 5947193323Sed case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 5948193323Sed case ISD::EXTRACT_ELEMENT: return "extract_element"; 5949193323Sed case ISD::BUILD_PAIR: return "build_pair"; 5950193323Sed case ISD::STACKSAVE: return "stacksave"; 5951193323Sed case ISD::STACKRESTORE: return "stackrestore"; 5952193323Sed case ISD::TRAP: return "trap"; 5953193323Sed 5954193323Sed // Bit manipulation 5955193323Sed case ISD::BSWAP: return "bswap"; 5956193323Sed case ISD::CTPOP: return "ctpop"; 5957193323Sed case ISD::CTTZ: return "cttz"; 5958193323Sed case ISD::CTLZ: return "ctlz"; 5959193323Sed 5960193323Sed // Trampolines 5961193323Sed case ISD::TRAMPOLINE: return "trampoline"; 5962193323Sed 5963193323Sed case ISD::CONDCODE: 5964193323Sed switch (cast<CondCodeSDNode>(this)->get()) { 5965198090Srdivacky default: llvm_unreachable("Unknown setcc condition!"); 5966193323Sed case ISD::SETOEQ: return "setoeq"; 5967193323Sed case ISD::SETOGT: return "setogt"; 5968193323Sed case ISD::SETOGE: return "setoge"; 5969193323Sed case ISD::SETOLT: return "setolt"; 5970193323Sed case ISD::SETOLE: return "setole"; 5971193323Sed case ISD::SETONE: return "setone"; 5972193323Sed 5973193323Sed case ISD::SETO: return "seto"; 5974193323Sed case ISD::SETUO: return "setuo"; 5975193323Sed case ISD::SETUEQ: return "setue"; 5976193323Sed case ISD::SETUGT: return "setugt"; 5977193323Sed case ISD::SETUGE: return "setuge"; 5978193323Sed case ISD::SETULT: return "setult"; 5979193323Sed case ISD::SETULE: return "setule"; 5980193323Sed case ISD::SETUNE: return "setune"; 5981193323Sed 5982193323Sed case ISD::SETEQ: return "seteq"; 5983193323Sed case ISD::SETGT: return "setgt"; 5984193323Sed case ISD::SETGE: return "setge"; 5985193323Sed case ISD::SETLT: return "setlt"; 5986193323Sed case ISD::SETLE: return "setle"; 5987193323Sed case ISD::SETNE: return "setne"; 5988193323Sed } 5989193323Sed } 5990193323Sed} 5991193323Sed 5992193323Sedconst char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) { 5993193323Sed switch (AM) { 5994193323Sed default: 5995193323Sed return ""; 5996193323Sed case ISD::PRE_INC: 5997193323Sed return "<pre-inc>"; 5998193323Sed case ISD::PRE_DEC: 5999193323Sed return "<pre-dec>"; 6000193323Sed case ISD::POST_INC: 6001193323Sed return "<post-inc>"; 6002193323Sed case ISD::POST_DEC: 6003193323Sed return "<post-dec>"; 6004193323Sed } 6005193323Sed} 6006193323Sed 6007193323Sedstd::string ISD::ArgFlagsTy::getArgFlagsString() { 6008193323Sed std::string S = "< "; 6009193323Sed 6010193323Sed if (isZExt()) 6011193323Sed S += "zext "; 6012193323Sed if (isSExt()) 6013193323Sed S += "sext "; 6014193323Sed if (isInReg()) 6015193323Sed S += "inreg "; 6016193323Sed if (isSRet()) 6017193323Sed S += "sret "; 6018193323Sed if (isByVal()) 6019193323Sed S += "byval "; 6020193323Sed if (isNest()) 6021193323Sed S += "nest "; 6022193323Sed if (getByValAlign()) 6023193323Sed S += "byval-align:" + utostr(getByValAlign()) + " "; 6024193323Sed if (getOrigAlign()) 6025193323Sed S += "orig-align:" + utostr(getOrigAlign()) + " "; 6026193323Sed if (getByValSize()) 6027193323Sed S += "byval-size:" + utostr(getByValSize()) + " "; 6028193323Sed return S + ">"; 6029193323Sed} 6030193323Sed 6031193323Sedvoid SDNode::dump() const { dump(0); } 6032193323Sedvoid SDNode::dump(const SelectionDAG *G) const { 6033202375Srdivacky print(dbgs(), G); 6034212904Sdim dbgs() << '\n'; 6035193323Sed} 6036193323Sed 6037193323Sedvoid SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const { 6038193323Sed OS << (void*)this << ": "; 6039193323Sed 6040193323Sed for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 6041193323Sed if (i) OS << ","; 6042193323Sed if (getValueType(i) == MVT::Other) 6043193323Sed OS << "ch"; 6044193323Sed else 6045198090Srdivacky OS << getValueType(i).getEVTString(); 6046193323Sed } 6047193323Sed OS << " = " << getOperationName(G); 6048193323Sed} 6049193323Sed 6050193323Sedvoid SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const { 6051198090Srdivacky if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) { 6052198090Srdivacky if (!MN->memoperands_empty()) { 6053198090Srdivacky OS << "<"; 6054198090Srdivacky OS << "Mem:"; 6055198090Srdivacky for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(), 6056198090Srdivacky e = MN->memoperands_end(); i != e; ++i) { 6057198090Srdivacky OS << **i; 6058212904Sdim if (llvm::next(i) != e) 6059198090Srdivacky OS << " "; 6060198090Srdivacky } 6061198090Srdivacky OS << ">"; 6062198090Srdivacky } 6063198090Srdivacky } else if (const ShuffleVectorSDNode *SVN = 6064198090Srdivacky dyn_cast<ShuffleVectorSDNode>(this)) { 6065193323Sed OS << "<"; 6066193323Sed for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) { 6067193323Sed int Idx = SVN->getMaskElt(i); 6068193323Sed if (i) OS << ","; 6069193323Sed if (Idx < 0) 6070193323Sed OS << "u"; 6071193323Sed else 6072193323Sed OS << Idx; 6073193323Sed } 6074193323Sed OS << ">"; 6075198090Srdivacky } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 6076193323Sed OS << '<' << CSDN->getAPIntValue() << '>'; 6077193323Sed } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 6078193323Sed if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle) 6079193323Sed OS << '<' << CSDN->getValueAPF().convertToFloat() << '>'; 6080193323Sed else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble) 6081193323Sed OS << '<' << CSDN->getValueAPF().convertToDouble() << '>'; 6082193323Sed else { 6083193323Sed OS << "<APFloat("; 6084193323Sed CSDN->getValueAPF().bitcastToAPInt().dump(); 6085193323Sed OS << ")>"; 6086193323Sed } 6087193323Sed } else if (const GlobalAddressSDNode *GADN = 6088193323Sed dyn_cast<GlobalAddressSDNode>(this)) { 6089193323Sed int64_t offset = GADN->getOffset(); 6090193323Sed OS << '<'; 6091193323Sed WriteAsOperand(OS, GADN->getGlobal()); 6092193323Sed OS << '>'; 6093193323Sed if (offset > 0) 6094193323Sed OS << " + " << offset; 6095193323Sed else 6096193323Sed OS << " " << offset; 6097198090Srdivacky if (unsigned int TF = GADN->getTargetFlags()) 6098195098Sed OS << " [TF=" << TF << ']'; 6099193323Sed } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 6100193323Sed OS << "<" << FIDN->getIndex() << ">"; 6101193323Sed } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) { 6102193323Sed OS << "<" << JTDN->getIndex() << ">"; 6103198090Srdivacky if (unsigned int TF = JTDN->getTargetFlags()) 6104195098Sed OS << " [TF=" << TF << ']'; 6105193323Sed } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 6106193323Sed int offset = CP->getOffset(); 6107193323Sed if (CP->isMachineConstantPoolEntry()) 6108193323Sed OS << "<" << *CP->getMachineCPVal() << ">"; 6109193323Sed else 6110193323Sed OS << "<" << *CP->getConstVal() << ">"; 6111193323Sed if (offset > 0) 6112193323Sed OS << " + " << offset; 6113193323Sed else 6114193323Sed OS << " " << offset; 6115198090Srdivacky if (unsigned int TF = CP->getTargetFlags()) 6116195098Sed OS << " [TF=" << TF << ']'; 6117193323Sed } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 6118193323Sed OS << "<"; 6119193323Sed const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 6120193323Sed if (LBB) 6121193323Sed OS << LBB->getName() << " "; 6122193323Sed OS << (const void*)BBDN->getBasicBlock() << ">"; 6123193323Sed } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 6124218893Sdim OS << ' ' << PrintReg(R->getReg(), G ? G->getTarget().getRegisterInfo() :0); 6125193323Sed } else if (const ExternalSymbolSDNode *ES = 6126193323Sed dyn_cast<ExternalSymbolSDNode>(this)) { 6127193323Sed OS << "'" << ES->getSymbol() << "'"; 6128198090Srdivacky if (unsigned int TF = ES->getTargetFlags()) 6129195098Sed OS << " [TF=" << TF << ']'; 6130193323Sed } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 6131193323Sed if (M->getValue()) 6132193323Sed OS << "<" << M->getValue() << ">"; 6133193323Sed else 6134193323Sed OS << "<null>"; 6135207618Srdivacky } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) { 6136207618Srdivacky if (MD->getMD()) 6137207618Srdivacky OS << "<" << MD->getMD() << ">"; 6138207618Srdivacky else 6139207618Srdivacky OS << "<null>"; 6140193323Sed } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 6141198090Srdivacky OS << ":" << N->getVT().getEVTString(); 6142193323Sed } 6143193323Sed else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) { 6144198892Srdivacky OS << "<" << *LD->getMemOperand(); 6145193323Sed 6146193323Sed bool doExt = true; 6147193323Sed switch (LD->getExtensionType()) { 6148193323Sed default: doExt = false; break; 6149198090Srdivacky case ISD::EXTLOAD: OS << ", anyext"; break; 6150198090Srdivacky case ISD::SEXTLOAD: OS << ", sext"; break; 6151198090Srdivacky case ISD::ZEXTLOAD: OS << ", zext"; break; 6152193323Sed } 6153193323Sed if (doExt) 6154198090Srdivacky OS << " from " << LD->getMemoryVT().getEVTString(); 6155193323Sed 6156193323Sed const char *AM = getIndexedModeName(LD->getAddressingMode()); 6157193323Sed if (*AM) 6158198090Srdivacky OS << ", " << AM; 6159198090Srdivacky 6160198090Srdivacky OS << ">"; 6161193323Sed } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) { 6162198892Srdivacky OS << "<" << *ST->getMemOperand(); 6163193323Sed 6164193323Sed if (ST->isTruncatingStore()) 6165198090Srdivacky OS << ", trunc to " << ST->getMemoryVT().getEVTString(); 6166193323Sed 6167193323Sed const char *AM = getIndexedModeName(ST->getAddressingMode()); 6168193323Sed if (*AM) 6169198090Srdivacky OS << ", " << AM; 6170218893Sdim 6171198090Srdivacky OS << ">"; 6172198090Srdivacky } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) { 6173198892Srdivacky OS << "<" << *M->getMemOperand() << ">"; 6174198892Srdivacky } else if (const BlockAddressSDNode *BA = 6175198892Srdivacky dyn_cast<BlockAddressSDNode>(this)) { 6176198892Srdivacky OS << "<"; 6177198892Srdivacky WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false); 6178198892Srdivacky OS << ", "; 6179198892Srdivacky WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false); 6180198892Srdivacky OS << ">"; 6181199989Srdivacky if (unsigned int TF = BA->getTargetFlags()) 6182199989Srdivacky OS << " [TF=" << TF << ']'; 6183193323Sed } 6184201360Srdivacky 6185201360Srdivacky if (G) 6186201360Srdivacky if (unsigned Order = G->GetOrdering(this)) 6187201360Srdivacky OS << " [ORD=" << Order << ']'; 6188205218Srdivacky 6189204642Srdivacky if (getNodeId() != -1) 6190204642Srdivacky OS << " [ID=" << getNodeId() << ']'; 6191208599Srdivacky 6192208599Srdivacky DebugLoc dl = getDebugLoc(); 6193208599Srdivacky if (G && !dl.isUnknown()) { 6194208599Srdivacky DIScope 6195208599Srdivacky Scope(dl.getScope(G->getMachineFunction().getFunction()->getContext())); 6196208599Srdivacky OS << " dbg:"; 6197208599Srdivacky // Omit the directory, since it's usually long and uninteresting. 6198208599Srdivacky if (Scope.Verify()) 6199208599Srdivacky OS << Scope.getFilename(); 6200208599Srdivacky else 6201208599Srdivacky OS << "<unknown>"; 6202208599Srdivacky OS << ':' << dl.getLine(); 6203208599Srdivacky if (dl.getCol() != 0) 6204208599Srdivacky OS << ':' << dl.getCol(); 6205208599Srdivacky } 6206193323Sed} 6207193323Sed 6208193323Sedvoid SDNode::print(raw_ostream &OS, const SelectionDAG *G) const { 6209193323Sed print_types(OS, G); 6210193323Sed for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 6211199481Srdivacky if (i) OS << ", "; else OS << " "; 6212193323Sed OS << (void*)getOperand(i).getNode(); 6213193323Sed if (unsigned RN = getOperand(i).getResNo()) 6214193323Sed OS << ":" << RN; 6215193323Sed } 6216193323Sed print_details(OS, G); 6217193323Sed} 6218193323Sed 6219202878Srdivackystatic void printrWithDepthHelper(raw_ostream &OS, const SDNode *N, 6220202878Srdivacky const SelectionDAG *G, unsigned depth, 6221218893Sdim unsigned indent) 6222202878Srdivacky{ 6223202878Srdivacky if (depth == 0) 6224202878Srdivacky return; 6225202878Srdivacky 6226202878Srdivacky OS.indent(indent); 6227202878Srdivacky 6228202878Srdivacky N->print(OS, G); 6229202878Srdivacky 6230202878Srdivacky if (depth < 1) 6231202878Srdivacky return; 6232202878Srdivacky 6233202878Srdivacky for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 6234202878Srdivacky OS << '\n'; 6235202878Srdivacky printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2); 6236202878Srdivacky } 6237202878Srdivacky} 6238202878Srdivacky 6239202878Srdivackyvoid SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G, 6240202878Srdivacky unsigned depth) const { 6241202878Srdivacky printrWithDepthHelper(OS, this, G, depth, 0); 6242218893Sdim} 6243202878Srdivacky 6244202878Srdivackyvoid SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const { 6245202878Srdivacky // Don't print impossibly deep things. 6246202878Srdivacky printrWithDepth(OS, G, 100); 6247202878Srdivacky} 6248202878Srdivacky 6249202878Srdivackyvoid SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const { 6250202878Srdivacky printrWithDepth(dbgs(), G, depth); 6251202878Srdivacky} 6252202878Srdivacky 6253202878Srdivackyvoid SDNode::dumprFull(const SelectionDAG *G) const { 6254202878Srdivacky // Don't print impossibly deep things. 6255202878Srdivacky dumprWithDepth(G, 100); 6256218893Sdim} 6257202878Srdivacky 6258193323Sedstatic void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 6259193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 6260193323Sed if (N->getOperand(i).getNode()->hasOneUse()) 6261193323Sed DumpNodes(N->getOperand(i).getNode(), indent+2, G); 6262193323Sed else 6263202375Srdivacky dbgs() << "\n" << std::string(indent+2, ' ') 6264202375Srdivacky << (void*)N->getOperand(i).getNode() << ": <multiple use>"; 6265193323Sed 6266193323Sed 6267202375Srdivacky dbgs() << "\n"; 6268202375Srdivacky dbgs().indent(indent); 6269193323Sed N->dump(G); 6270193323Sed} 6271193323Sed 6272199989SrdivackySDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) { 6273199989Srdivacky assert(N->getNumValues() == 1 && 6274199989Srdivacky "Can't unroll a vector with multiple results!"); 6275199989Srdivacky 6276199989Srdivacky EVT VT = N->getValueType(0); 6277199989Srdivacky unsigned NE = VT.getVectorNumElements(); 6278199989Srdivacky EVT EltVT = VT.getVectorElementType(); 6279199989Srdivacky DebugLoc dl = N->getDebugLoc(); 6280199989Srdivacky 6281199989Srdivacky SmallVector<SDValue, 8> Scalars; 6282199989Srdivacky SmallVector<SDValue, 4> Operands(N->getNumOperands()); 6283199989Srdivacky 6284199989Srdivacky // If ResNE is 0, fully unroll the vector op. 6285199989Srdivacky if (ResNE == 0) 6286199989Srdivacky ResNE = NE; 6287199989Srdivacky else if (NE > ResNE) 6288199989Srdivacky NE = ResNE; 6289199989Srdivacky 6290199989Srdivacky unsigned i; 6291199989Srdivacky for (i= 0; i != NE; ++i) { 6292207618Srdivacky for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) { 6293199989Srdivacky SDValue Operand = N->getOperand(j); 6294199989Srdivacky EVT OperandVT = Operand.getValueType(); 6295199989Srdivacky if (OperandVT.isVector()) { 6296199989Srdivacky // A vector operand; extract a single element. 6297199989Srdivacky EVT OperandEltVT = OperandVT.getVectorElementType(); 6298199989Srdivacky Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, 6299199989Srdivacky OperandEltVT, 6300199989Srdivacky Operand, 6301199989Srdivacky getConstant(i, MVT::i32)); 6302199989Srdivacky } else { 6303199989Srdivacky // A scalar operand; just use it as is. 6304199989Srdivacky Operands[j] = Operand; 6305199989Srdivacky } 6306199989Srdivacky } 6307199989Srdivacky 6308199989Srdivacky switch (N->getOpcode()) { 6309199989Srdivacky default: 6310199989Srdivacky Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, 6311199989Srdivacky &Operands[0], Operands.size())); 6312199989Srdivacky break; 6313199989Srdivacky case ISD::SHL: 6314199989Srdivacky case ISD::SRA: 6315199989Srdivacky case ISD::SRL: 6316199989Srdivacky case ISD::ROTL: 6317199989Srdivacky case ISD::ROTR: 6318199989Srdivacky Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0], 6319199989Srdivacky getShiftAmountOperand(Operands[1]))); 6320199989Srdivacky break; 6321202375Srdivacky case ISD::SIGN_EXTEND_INREG: 6322202375Srdivacky case ISD::FP_ROUND_INREG: { 6323202375Srdivacky EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType(); 6324202375Srdivacky Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, 6325202375Srdivacky Operands[0], 6326202375Srdivacky getValueType(ExtVT))); 6327199989Srdivacky } 6328202375Srdivacky } 6329199989Srdivacky } 6330199989Srdivacky 6331199989Srdivacky for (; i < ResNE; ++i) 6332199989Srdivacky Scalars.push_back(getUNDEF(EltVT)); 6333199989Srdivacky 6334199989Srdivacky return getNode(ISD::BUILD_VECTOR, dl, 6335199989Srdivacky EVT::getVectorVT(*getContext(), EltVT, ResNE), 6336199989Srdivacky &Scalars[0], Scalars.size()); 6337199989Srdivacky} 6338199989Srdivacky 6339200581Srdivacky 6340218893Sdim/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a 6341218893Sdim/// location that is 'Dist' units away from the location that the 'Base' load 6342200581Srdivacky/// is loading from. 6343218893Sdimbool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base, 6344200581Srdivacky unsigned Bytes, int Dist) const { 6345200581Srdivacky if (LD->getChain() != Base->getChain()) 6346200581Srdivacky return false; 6347200581Srdivacky EVT VT = LD->getValueType(0); 6348200581Srdivacky if (VT.getSizeInBits() / 8 != Bytes) 6349200581Srdivacky return false; 6350200581Srdivacky 6351200581Srdivacky SDValue Loc = LD->getOperand(1); 6352200581Srdivacky SDValue BaseLoc = Base->getOperand(1); 6353200581Srdivacky if (Loc.getOpcode() == ISD::FrameIndex) { 6354200581Srdivacky if (BaseLoc.getOpcode() != ISD::FrameIndex) 6355200581Srdivacky return false; 6356200581Srdivacky const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo(); 6357200581Srdivacky int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); 6358200581Srdivacky int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 6359200581Srdivacky int FS = MFI->getObjectSize(FI); 6360200581Srdivacky int BFS = MFI->getObjectSize(BFI); 6361200581Srdivacky if (FS != BFS || FS != (int)Bytes) return false; 6362200581Srdivacky return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); 6363200581Srdivacky } 6364200581Srdivacky 6365218893Sdim // Handle X+C 6366218893Sdim if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc && 6367218893Sdim cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes) 6368218893Sdim return true; 6369218893Sdim 6370207618Srdivacky const GlobalValue *GV1 = NULL; 6371207618Srdivacky const GlobalValue *GV2 = NULL; 6372200581Srdivacky int64_t Offset1 = 0; 6373200581Srdivacky int64_t Offset2 = 0; 6374200581Srdivacky bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1); 6375200581Srdivacky bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); 6376200581Srdivacky if (isGA1 && isGA2 && GV1 == GV2) 6377200581Srdivacky return Offset1 == (Offset2 + Dist*Bytes); 6378200581Srdivacky return false; 6379200581Srdivacky} 6380200581Srdivacky 6381200581Srdivacky 6382200581Srdivacky/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if 6383200581Srdivacky/// it cannot be inferred. 6384200581Srdivackyunsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const { 6385200581Srdivacky // If this is a GlobalAddress + cst, return the alignment. 6386207618Srdivacky const GlobalValue *GV; 6387200581Srdivacky int64_t GVOffset = 0; 6388206083Srdivacky if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) { 6389206083Srdivacky // If GV has specified alignment, then use it. Otherwise, use the preferred 6390206083Srdivacky // alignment. 6391206083Srdivacky unsigned Align = GV->getAlignment(); 6392206083Srdivacky if (!Align) { 6393207618Srdivacky if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { 6394206083Srdivacky if (GVar->hasInitializer()) { 6395206083Srdivacky const TargetData *TD = TLI.getTargetData(); 6396206083Srdivacky Align = TD->getPreferredAlignment(GVar); 6397206083Srdivacky } 6398206083Srdivacky } 6399206083Srdivacky } 6400206083Srdivacky return MinAlign(Align, GVOffset); 6401206083Srdivacky } 6402200581Srdivacky 6403200581Srdivacky // If this is a direct reference to a stack slot, use information about the 6404200581Srdivacky // stack slot's alignment. 6405200581Srdivacky int FrameIdx = 1 << 31; 6406200581Srdivacky int64_t FrameOffset = 0; 6407200581Srdivacky if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) { 6408200581Srdivacky FrameIdx = FI->getIndex(); 6409218893Sdim } else if (isBaseWithConstantOffset(Ptr) && 6410200581Srdivacky isa<FrameIndexSDNode>(Ptr.getOperand(0))) { 6411218893Sdim // Handle FI+Cst 6412200581Srdivacky FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); 6413200581Srdivacky FrameOffset = Ptr.getConstantOperandVal(1); 6414200581Srdivacky } 6415200581Srdivacky 6416200581Srdivacky if (FrameIdx != (1 << 31)) { 6417200581Srdivacky const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo(); 6418200581Srdivacky unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx), 6419200581Srdivacky FrameOffset); 6420200581Srdivacky return FIInfoAlign; 6421200581Srdivacky } 6422200581Srdivacky 6423200581Srdivacky return 0; 6424200581Srdivacky} 6425200581Srdivacky 6426193323Sedvoid SelectionDAG::dump() const { 6427202375Srdivacky dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:"; 6428193323Sed 6429193323Sed for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 6430193323Sed I != E; ++I) { 6431193323Sed const SDNode *N = I; 6432193323Sed if (!N->hasOneUse() && N != getRoot().getNode()) 6433193323Sed DumpNodes(N, 2, this); 6434193323Sed } 6435193323Sed 6436193323Sed if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this); 6437193323Sed 6438202375Srdivacky dbgs() << "\n\n"; 6439193323Sed} 6440193323Sed 6441193323Sedvoid SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const { 6442193323Sed print_types(OS, G); 6443193323Sed print_details(OS, G); 6444193323Sed} 6445193323Sed 6446193323Sedtypedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet; 6447193323Sedstatic void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent, 6448193323Sed const SelectionDAG *G, VisitedSDNodeSet &once) { 6449193323Sed if (!once.insert(N)) // If we've been here before, return now. 6450193323Sed return; 6451201360Srdivacky 6452193323Sed // Dump the current SDNode, but don't end the line yet. 6453193323Sed OS << std::string(indent, ' '); 6454193323Sed N->printr(OS, G); 6455201360Srdivacky 6456193323Sed // Having printed this SDNode, walk the children: 6457193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 6458193323Sed const SDNode *child = N->getOperand(i).getNode(); 6459201360Srdivacky 6460193323Sed if (i) OS << ","; 6461193323Sed OS << " "; 6462201360Srdivacky 6463193323Sed if (child->getNumOperands() == 0) { 6464193323Sed // This child has no grandchildren; print it inline right here. 6465193323Sed child->printr(OS, G); 6466193323Sed once.insert(child); 6467201360Srdivacky } else { // Just the address. FIXME: also print the child's opcode. 6468193323Sed OS << (void*)child; 6469193323Sed if (unsigned RN = N->getOperand(i).getResNo()) 6470193323Sed OS << ":" << RN; 6471193323Sed } 6472193323Sed } 6473201360Srdivacky 6474193323Sed OS << "\n"; 6475201360Srdivacky 6476193323Sed // Dump children that have grandchildren on their own line(s). 6477193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 6478193323Sed const SDNode *child = N->getOperand(i).getNode(); 6479193323Sed DumpNodesr(OS, child, indent+2, G, once); 6480193323Sed } 6481193323Sed} 6482193323Sed 6483193323Sedvoid SDNode::dumpr() const { 6484193323Sed VisitedSDNodeSet once; 6485202375Srdivacky DumpNodesr(dbgs(), this, 0, 0, once); 6486193323Sed} 6487193323Sed 6488198090Srdivackyvoid SDNode::dumpr(const SelectionDAG *G) const { 6489198090Srdivacky VisitedSDNodeSet once; 6490202375Srdivacky DumpNodesr(dbgs(), this, 0, G, once); 6491198090Srdivacky} 6492193323Sed 6493198090Srdivacky 6494193323Sed// getAddressSpace - Return the address space this GlobalAddress belongs to. 6495193323Sedunsigned GlobalAddressSDNode::getAddressSpace() const { 6496193323Sed return getGlobal()->getType()->getAddressSpace(); 6497193323Sed} 6498193323Sed 6499193323Sed 6500193323Sedconst Type *ConstantPoolSDNode::getType() const { 6501193323Sed if (isMachineConstantPoolEntry()) 6502193323Sed return Val.MachineCPVal->getType(); 6503193323Sed return Val.ConstVal->getType(); 6504193323Sed} 6505193323Sed 6506193323Sedbool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, 6507193323Sed APInt &SplatUndef, 6508193323Sed unsigned &SplatBitSize, 6509193323Sed bool &HasAnyUndefs, 6510199481Srdivacky unsigned MinSplatBits, 6511199481Srdivacky bool isBigEndian) { 6512198090Srdivacky EVT VT = getValueType(0); 6513193323Sed assert(VT.isVector() && "Expected a vector type"); 6514193323Sed unsigned sz = VT.getSizeInBits(); 6515193323Sed if (MinSplatBits > sz) 6516193323Sed return false; 6517193323Sed 6518193323Sed SplatValue = APInt(sz, 0); 6519193323Sed SplatUndef = APInt(sz, 0); 6520193323Sed 6521193323Sed // Get the bits. Bits with undefined values (when the corresponding element 6522193323Sed // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared 6523193323Sed // in SplatValue. If any of the values are not constant, give up and return 6524193323Sed // false. 6525193323Sed unsigned int nOps = getNumOperands(); 6526193323Sed assert(nOps > 0 && "isConstantSplat has 0-size build vector"); 6527193323Sed unsigned EltBitSize = VT.getVectorElementType().getSizeInBits(); 6528199481Srdivacky 6529199481Srdivacky for (unsigned j = 0; j < nOps; ++j) { 6530199481Srdivacky unsigned i = isBigEndian ? nOps-1-j : j; 6531193323Sed SDValue OpVal = getOperand(i); 6532199481Srdivacky unsigned BitPos = j * EltBitSize; 6533193323Sed 6534193323Sed if (OpVal.getOpcode() == ISD::UNDEF) 6535199481Srdivacky SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize); 6536193323Sed else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) 6537218893Sdim SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize). 6538207618Srdivacky zextOrTrunc(sz) << BitPos; 6539193323Sed else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) 6540193323Sed SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos; 6541193323Sed else 6542193323Sed return false; 6543193323Sed } 6544193323Sed 6545193323Sed // The build_vector is all constants or undefs. Find the smallest element 6546193323Sed // size that splats the vector. 6547193323Sed 6548193323Sed HasAnyUndefs = (SplatUndef != 0); 6549193323Sed while (sz > 8) { 6550193323Sed 6551193323Sed unsigned HalfSize = sz / 2; 6552218893Sdim APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize); 6553218893Sdim APInt LowValue = SplatValue.trunc(HalfSize); 6554218893Sdim APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize); 6555218893Sdim APInt LowUndef = SplatUndef.trunc(HalfSize); 6556193323Sed 6557193323Sed // If the two halves do not match (ignoring undef bits), stop here. 6558193323Sed if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) || 6559193323Sed MinSplatBits > HalfSize) 6560193323Sed break; 6561193323Sed 6562193323Sed SplatValue = HighValue | LowValue; 6563193323Sed SplatUndef = HighUndef & LowUndef; 6564198090Srdivacky 6565193323Sed sz = HalfSize; 6566193323Sed } 6567193323Sed 6568193323Sed SplatBitSize = sz; 6569193323Sed return true; 6570193323Sed} 6571193323Sed 6572198090Srdivackybool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) { 6573193323Sed // Find the first non-undef value in the shuffle mask. 6574193323Sed unsigned i, e; 6575193323Sed for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i) 6576193323Sed /* search */; 6577193323Sed 6578193323Sed assert(i != e && "VECTOR_SHUFFLE node with all undef indices!"); 6579198090Srdivacky 6580193323Sed // Make sure all remaining elements are either undef or the same as the first 6581193323Sed // non-undef value. 6582193323Sed for (int Idx = Mask[i]; i != e; ++i) 6583193323Sed if (Mask[i] >= 0 && Mask[i] != Idx) 6584193323Sed return false; 6585193323Sed return true; 6586193323Sed} 6587202878Srdivacky 6588204642Srdivacky#ifdef XDEBUG 6589202878Srdivackystatic void checkForCyclesHelper(const SDNode *N, 6590204642Srdivacky SmallPtrSet<const SDNode*, 32> &Visited, 6591204642Srdivacky SmallPtrSet<const SDNode*, 32> &Checked) { 6592204642Srdivacky // If this node has already been checked, don't check it again. 6593204642Srdivacky if (Checked.count(N)) 6594204642Srdivacky return; 6595218893Sdim 6596204642Srdivacky // If a node has already been visited on this depth-first walk, reject it as 6597204642Srdivacky // a cycle. 6598204642Srdivacky if (!Visited.insert(N)) { 6599202878Srdivacky dbgs() << "Offending node:\n"; 6600202878Srdivacky N->dumprFull(); 6601204642Srdivacky errs() << "Detected cycle in SelectionDAG\n"; 6602204642Srdivacky abort(); 6603202878Srdivacky } 6604218893Sdim 6605204642Srdivacky for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 6606204642Srdivacky checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked); 6607218893Sdim 6608204642Srdivacky Checked.insert(N); 6609204642Srdivacky Visited.erase(N); 6610202878Srdivacky} 6611204642Srdivacky#endif 6612202878Srdivacky 6613202878Srdivackyvoid llvm::checkForCycles(const llvm::SDNode *N) { 6614202878Srdivacky#ifdef XDEBUG 6615202878Srdivacky assert(N && "Checking nonexistant SDNode"); 6616204642Srdivacky SmallPtrSet<const SDNode*, 32> visited; 6617204642Srdivacky SmallPtrSet<const SDNode*, 32> checked; 6618204642Srdivacky checkForCyclesHelper(N, visited, checked); 6619202878Srdivacky#endif 6620202878Srdivacky} 6621202878Srdivacky 6622202878Srdivackyvoid llvm::checkForCycles(const llvm::SelectionDAG *DAG) { 6623202878Srdivacky checkForCycles(DAG->getRoot().getNode()); 6624202878Srdivacky} 6625