1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This implements the SelectionDAG class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/SelectionDAG.h" 15 #include "SDNodeDbgValue.h" 16 #include "llvm/ADT/SetVector.h" 17 #include "llvm/ADT/SmallPtrSet.h" 18 #include "llvm/ADT/SmallSet.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/CodeGen/MachineBasicBlock.h" 23 #include "llvm/CodeGen/MachineConstantPool.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineModuleInfo.h" 26 #include "llvm/IR/CallingConv.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/DebugInfo.h" 30 #include "llvm/IR/DerivedTypes.h" 31 #include "llvm/IR/Function.h" 32 #include "llvm/IR/GlobalAlias.h" 33 #include "llvm/IR/GlobalVariable.h" 34 #include "llvm/IR/Intrinsics.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/ErrorHandling.h" 38 #include "llvm/Support/ManagedStatic.h" 39 #include "llvm/Support/MathExtras.h" 40 #include "llvm/Support/Mutex.h" 41 #include "llvm/Support/raw_ostream.h" 42 #include "llvm/Target/TargetInstrInfo.h" 43 #include "llvm/Target/TargetIntrinsicInfo.h" 44 #include "llvm/Target/TargetLowering.h" 45 #include "llvm/Target/TargetMachine.h" 46 #include "llvm/Target/TargetOptions.h" 47 #include "llvm/Target/TargetRegisterInfo.h" 48 #include "llvm/Target/TargetSelectionDAGInfo.h" 49 #include "llvm/Target/TargetSubtargetInfo.h" 50 #include <algorithm> 51 #include <cmath> 52 53 using namespace llvm; 54 55 /// makeVTList - Return an instance of the SDVTList struct initialized with the 56 /// specified members. 57 static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) { 58 SDVTList Res = {VTs, NumVTs}; 59 return Res; 60 } 61 62 // Default null implementations of the callbacks. 63 void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {} 64 void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {} 65 66 //===----------------------------------------------------------------------===// 67 // ConstantFPSDNode Class 68 //===----------------------------------------------------------------------===// 69 70 /// isExactlyValue - We don't rely on operator== working on double values, as 71 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 72 /// As such, this method can be used to do an exact bit-for-bit comparison of 73 /// two floating point values. 74 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const { 75 return getValueAPF().bitwiseIsEqual(V); 76 } 77 78 bool ConstantFPSDNode::isValueValidForType(EVT VT, 79 const APFloat& Val) { 80 assert(VT.isFloatingPoint() && "Can only convert between FP types"); 81 82 // convert modifies in place, so make a copy. 83 APFloat Val2 = APFloat(Val); 84 bool losesInfo; 85 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT), 86 APFloat::rmNearestTiesToEven, 87 &losesInfo); 88 return !losesInfo; 89 } 90 91 //===----------------------------------------------------------------------===// 92 // ISD Namespace 93 //===----------------------------------------------------------------------===// 94 95 /// isBuildVectorAllOnes - Return true if the specified node is a 96 /// BUILD_VECTOR where all of the elements are ~0 or undef. 97 bool ISD::isBuildVectorAllOnes(const SDNode *N) { 98 // Look through a bit convert. 99 while (N->getOpcode() == ISD::BITCAST) 100 N = N->getOperand(0).getNode(); 101 102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 103 104 unsigned i = 0, e = N->getNumOperands(); 105 106 // Skip over all of the undef values. 107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 108 ++i; 109 110 // Do not accept an all-undef vector. 111 if (i == e) return false; 112 113 // Do not accept build_vectors that aren't all constants or which have non-~0 114 // elements. We have to be a bit careful here, as the type of the constant 115 // may not be the same as the type of the vector elements due to type 116 // legalization (the elements are promoted to a legal type for the target and 117 // a vector of a type may be legal when the base element type is not). 118 // We only want to check enough bits to cover the vector elements, because 119 // we care if the resultant vector is all ones, not whether the individual 120 // constants are. 121 SDValue NotZero = N->getOperand(i); 122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits(); 123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) { 124 if (CN->getAPIntValue().countTrailingOnes() < EltSize) 125 return false; 126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) { 127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize) 128 return false; 129 } else 130 return false; 131 132 // Okay, we have at least one ~0 value, check to see if the rest match or are 133 // undefs. Even with the above element type twiddling, this should be OK, as 134 // the same type legalization should have applied to all the elements. 135 for (++i; i != e; ++i) 136 if (N->getOperand(i) != NotZero && 137 N->getOperand(i).getOpcode() != ISD::UNDEF) 138 return false; 139 return true; 140 } 141 142 143 /// isBuildVectorAllZeros - Return true if the specified node is a 144 /// BUILD_VECTOR where all of the elements are 0 or undef. 145 bool ISD::isBuildVectorAllZeros(const SDNode *N) { 146 // Look through a bit convert. 147 while (N->getOpcode() == ISD::BITCAST) 148 N = N->getOperand(0).getNode(); 149 150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 151 152 bool IsAllUndef = true; 153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) { 154 if (N->getOperand(i).getOpcode() == ISD::UNDEF) 155 continue; 156 IsAllUndef = false; 157 // Do not accept build_vectors that aren't all constants or which have non-0 158 // elements. We have to be a bit careful here, as the type of the constant 159 // may not be the same as the type of the vector elements due to type 160 // legalization (the elements are promoted to a legal type for the target 161 // and a vector of a type may be legal when the base element type is not). 162 // We only want to check enough bits to cover the vector elements, because 163 // we care if the resultant vector is all zeros, not whether the individual 164 // constants are. 165 SDValue Zero = N->getOperand(i); 166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits(); 167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) { 168 if (CN->getAPIntValue().countTrailingZeros() < EltSize) 169 return false; 170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) { 171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize) 172 return false; 173 } else 174 return false; 175 } 176 177 // Do not accept an all-undef vector. 178 if (IsAllUndef) 179 return false; 180 return true; 181 } 182 183 /// \brief Return true if the specified node is a BUILD_VECTOR node of 184 /// all ConstantSDNode or undef. 185 bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) { 186 if (N->getOpcode() != ISD::BUILD_VECTOR) 187 return false; 188 189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 190 SDValue Op = N->getOperand(i); 191 if (Op.getOpcode() == ISD::UNDEF) 192 continue; 193 if (!isa<ConstantSDNode>(Op)) 194 return false; 195 } 196 return true; 197 } 198 199 /// \brief Return true if the specified node is a BUILD_VECTOR node of 200 /// all ConstantFPSDNode or undef. 201 bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) { 202 if (N->getOpcode() != ISD::BUILD_VECTOR) 203 return false; 204 205 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 206 SDValue Op = N->getOperand(i); 207 if (Op.getOpcode() == ISD::UNDEF) 208 continue; 209 if (!isa<ConstantFPSDNode>(Op)) 210 return false; 211 } 212 return true; 213 } 214 215 /// isScalarToVector - Return true if the specified node is a 216 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low 217 /// element is not an undef. 218 bool ISD::isScalarToVector(const SDNode *N) { 219 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) 220 return true; 221 222 if (N->getOpcode() != ISD::BUILD_VECTOR) 223 return false; 224 if (N->getOperand(0).getOpcode() == ISD::UNDEF) 225 return false; 226 unsigned NumElems = N->getNumOperands(); 227 if (NumElems == 1) 228 return false; 229 for (unsigned i = 1; i < NumElems; ++i) { 230 SDValue V = N->getOperand(i); 231 if (V.getOpcode() != ISD::UNDEF) 232 return false; 233 } 234 return true; 235 } 236 237 /// allOperandsUndef - Return true if the node has at least one operand 238 /// and all operands of the specified node are ISD::UNDEF. 239 bool ISD::allOperandsUndef(const SDNode *N) { 240 // Return false if the node has no operands. 241 // This is "logically inconsistent" with the definition of "all" but 242 // is probably the desired behavior. 243 if (N->getNumOperands() == 0) 244 return false; 245 246 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i) 247 if (N->getOperand(i).getOpcode() != ISD::UNDEF) 248 return false; 249 250 return true; 251 } 252 253 ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) { 254 switch (ExtType) { 255 case ISD::EXTLOAD: 256 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND; 257 case ISD::SEXTLOAD: 258 return ISD::SIGN_EXTEND; 259 case ISD::ZEXTLOAD: 260 return ISD::ZERO_EXTEND; 261 default: 262 break; 263 } 264 265 llvm_unreachable("Invalid LoadExtType"); 266 } 267 268 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 269 /// when given the operation for (X op Y). 270 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 271 // To perform this operation, we just need to swap the L and G bits of the 272 // operation. 273 unsigned OldL = (Operation >> 2) & 1; 274 unsigned OldG = (Operation >> 1) & 1; 275 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 276 (OldL << 1) | // New G bit 277 (OldG << 2)); // New L bit. 278 } 279 280 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where 281 /// 'op' is a valid SetCC operation. 282 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 283 unsigned Operation = Op; 284 if (isInteger) 285 Operation ^= 7; // Flip L, G, E bits, but not U. 286 else 287 Operation ^= 15; // Flip all of the condition bits. 288 289 if (Operation > ISD::SETTRUE2) 290 Operation &= ~8; // Don't let N and U bits get set. 291 292 return ISD::CondCode(Operation); 293 } 294 295 296 /// isSignedOp - For an integer comparison, return 1 if the comparison is a 297 /// signed operation and 2 if the result is an unsigned comparison. Return zero 298 /// if the operation does not depend on the sign of the input (setne and seteq). 299 static int isSignedOp(ISD::CondCode Opcode) { 300 switch (Opcode) { 301 default: llvm_unreachable("Illegal integer setcc operation!"); 302 case ISD::SETEQ: 303 case ISD::SETNE: return 0; 304 case ISD::SETLT: 305 case ISD::SETLE: 306 case ISD::SETGT: 307 case ISD::SETGE: return 1; 308 case ISD::SETULT: 309 case ISD::SETULE: 310 case ISD::SETUGT: 311 case ISD::SETUGE: return 2; 312 } 313 } 314 315 /// getSetCCOrOperation - Return the result of a logical OR between different 316 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 317 /// returns SETCC_INVALID if it is not possible to represent the resultant 318 /// comparison. 319 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 320 bool isInteger) { 321 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 322 // Cannot fold a signed integer setcc with an unsigned integer setcc. 323 return ISD::SETCC_INVALID; 324 325 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 326 327 // If the N and U bits get set then the resultant comparison DOES suddenly 328 // care about orderedness, and is true when ordered. 329 if (Op > ISD::SETTRUE2) 330 Op &= ~16; // Clear the U bit if the N bit is set. 331 332 // Canonicalize illegal integer setcc's. 333 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT 334 Op = ISD::SETNE; 335 336 return ISD::CondCode(Op); 337 } 338 339 /// getSetCCAndOperation - Return the result of a logical AND between different 340 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 341 /// function returns zero if it is not possible to represent the resultant 342 /// comparison. 343 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 344 bool isInteger) { 345 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 346 // Cannot fold a signed setcc with an unsigned setcc. 347 return ISD::SETCC_INVALID; 348 349 // Combine all of the condition bits. 350 ISD::CondCode Result = ISD::CondCode(Op1 & Op2); 351 352 // Canonicalize illegal integer setcc's. 353 if (isInteger) { 354 switch (Result) { 355 default: break; 356 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT 357 case ISD::SETOEQ: // SETEQ & SETU[LG]E 358 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE 359 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE 360 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE 361 } 362 } 363 364 return Result; 365 } 366 367 //===----------------------------------------------------------------------===// 368 // SDNode Profile Support 369 //===----------------------------------------------------------------------===// 370 371 /// AddNodeIDOpcode - Add the node opcode to the NodeID data. 372 /// 373 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) { 374 ID.AddInteger(OpC); 375 } 376 377 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them 378 /// solely with their pointer. 379 static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) { 380 ID.AddPointer(VTList.VTs); 381 } 382 383 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data. 384 /// 385 static void AddNodeIDOperands(FoldingSetNodeID &ID, 386 ArrayRef<SDValue> Ops) { 387 for (auto& Op : Ops) { 388 ID.AddPointer(Op.getNode()); 389 ID.AddInteger(Op.getResNo()); 390 } 391 } 392 393 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data. 394 /// 395 static void AddNodeIDOperands(FoldingSetNodeID &ID, 396 ArrayRef<SDUse> Ops) { 397 for (auto& Op : Ops) { 398 ID.AddPointer(Op.getNode()); 399 ID.AddInteger(Op.getResNo()); 400 } 401 } 402 403 static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw, 404 bool exact) { 405 ID.AddBoolean(nuw); 406 ID.AddBoolean(nsw); 407 ID.AddBoolean(exact); 408 } 409 410 /// AddBinaryNodeIDCustom - Add BinarySDNodes special infos 411 static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode, 412 bool nuw, bool nsw, bool exact) { 413 if (isBinOpWithFlags(Opcode)) 414 AddBinaryNodeIDCustom(ID, nuw, nsw, exact); 415 } 416 417 static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC, 418 SDVTList VTList, ArrayRef<SDValue> OpList) { 419 AddNodeIDOpcode(ID, OpC); 420 AddNodeIDValueTypes(ID, VTList); 421 AddNodeIDOperands(ID, OpList); 422 } 423 424 /// AddNodeIDCustom - If this is an SDNode with special info, add this info to 425 /// the NodeID data. 426 static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) { 427 switch (N->getOpcode()) { 428 case ISD::TargetExternalSymbol: 429 case ISD::ExternalSymbol: 430 llvm_unreachable("Should only be used on nodes with operands"); 431 default: break; // Normal nodes don't need extra info. 432 case ISD::TargetConstant: 433 case ISD::Constant: { 434 const ConstantSDNode *C = cast<ConstantSDNode>(N); 435 ID.AddPointer(C->getConstantIntValue()); 436 ID.AddBoolean(C->isOpaque()); 437 break; 438 } 439 case ISD::TargetConstantFP: 440 case ISD::ConstantFP: { 441 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue()); 442 break; 443 } 444 case ISD::TargetGlobalAddress: 445 case ISD::GlobalAddress: 446 case ISD::TargetGlobalTLSAddress: 447 case ISD::GlobalTLSAddress: { 448 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); 449 ID.AddPointer(GA->getGlobal()); 450 ID.AddInteger(GA->getOffset()); 451 ID.AddInteger(GA->getTargetFlags()); 452 ID.AddInteger(GA->getAddressSpace()); 453 break; 454 } 455 case ISD::BasicBlock: 456 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock()); 457 break; 458 case ISD::Register: 459 ID.AddInteger(cast<RegisterSDNode>(N)->getReg()); 460 break; 461 case ISD::RegisterMask: 462 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask()); 463 break; 464 case ISD::SRCVALUE: 465 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue()); 466 break; 467 case ISD::FrameIndex: 468 case ISD::TargetFrameIndex: 469 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex()); 470 break; 471 case ISD::JumpTable: 472 case ISD::TargetJumpTable: 473 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex()); 474 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags()); 475 break; 476 case ISD::ConstantPool: 477 case ISD::TargetConstantPool: { 478 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N); 479 ID.AddInteger(CP->getAlignment()); 480 ID.AddInteger(CP->getOffset()); 481 if (CP->isMachineConstantPoolEntry()) 482 CP->getMachineCPVal()->addSelectionDAGCSEId(ID); 483 else 484 ID.AddPointer(CP->getConstVal()); 485 ID.AddInteger(CP->getTargetFlags()); 486 break; 487 } 488 case ISD::TargetIndex: { 489 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N); 490 ID.AddInteger(TI->getIndex()); 491 ID.AddInteger(TI->getOffset()); 492 ID.AddInteger(TI->getTargetFlags()); 493 break; 494 } 495 case ISD::LOAD: { 496 const LoadSDNode *LD = cast<LoadSDNode>(N); 497 ID.AddInteger(LD->getMemoryVT().getRawBits()); 498 ID.AddInteger(LD->getRawSubclassData()); 499 ID.AddInteger(LD->getPointerInfo().getAddrSpace()); 500 break; 501 } 502 case ISD::STORE: { 503 const StoreSDNode *ST = cast<StoreSDNode>(N); 504 ID.AddInteger(ST->getMemoryVT().getRawBits()); 505 ID.AddInteger(ST->getRawSubclassData()); 506 ID.AddInteger(ST->getPointerInfo().getAddrSpace()); 507 break; 508 } 509 case ISD::SDIV: 510 case ISD::UDIV: 511 case ISD::SRA: 512 case ISD::SRL: 513 case ISD::MUL: 514 case ISD::ADD: 515 case ISD::SUB: 516 case ISD::SHL: { 517 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N); 518 AddBinaryNodeIDCustom(ID, N->getOpcode(), 519 BinNode->Flags.hasNoUnsignedWrap(), 520 BinNode->Flags.hasNoSignedWrap(), 521 BinNode->Flags.hasExact()); 522 break; 523 } 524 case ISD::ATOMIC_CMP_SWAP: 525 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 526 case ISD::ATOMIC_SWAP: 527 case ISD::ATOMIC_LOAD_ADD: 528 case ISD::ATOMIC_LOAD_SUB: 529 case ISD::ATOMIC_LOAD_AND: 530 case ISD::ATOMIC_LOAD_OR: 531 case ISD::ATOMIC_LOAD_XOR: 532 case ISD::ATOMIC_LOAD_NAND: 533 case ISD::ATOMIC_LOAD_MIN: 534 case ISD::ATOMIC_LOAD_MAX: 535 case ISD::ATOMIC_LOAD_UMIN: 536 case ISD::ATOMIC_LOAD_UMAX: 537 case ISD::ATOMIC_LOAD: 538 case ISD::ATOMIC_STORE: { 539 const AtomicSDNode *AT = cast<AtomicSDNode>(N); 540 ID.AddInteger(AT->getMemoryVT().getRawBits()); 541 ID.AddInteger(AT->getRawSubclassData()); 542 ID.AddInteger(AT->getPointerInfo().getAddrSpace()); 543 break; 544 } 545 case ISD::PREFETCH: { 546 const MemSDNode *PF = cast<MemSDNode>(N); 547 ID.AddInteger(PF->getPointerInfo().getAddrSpace()); 548 break; 549 } 550 case ISD::VECTOR_SHUFFLE: { 551 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 552 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements(); 553 i != e; ++i) 554 ID.AddInteger(SVN->getMaskElt(i)); 555 break; 556 } 557 case ISD::TargetBlockAddress: 558 case ISD::BlockAddress: { 559 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N); 560 ID.AddPointer(BA->getBlockAddress()); 561 ID.AddInteger(BA->getOffset()); 562 ID.AddInteger(BA->getTargetFlags()); 563 break; 564 } 565 } // end switch (N->getOpcode()) 566 567 // Target specific memory nodes could also have address spaces to check. 568 if (N->isTargetMemoryOpcode()) 569 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace()); 570 } 571 572 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID 573 /// data. 574 static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) { 575 AddNodeIDOpcode(ID, N->getOpcode()); 576 // Add the return value info. 577 AddNodeIDValueTypes(ID, N->getVTList()); 578 // Add the operand info. 579 AddNodeIDOperands(ID, N->ops()); 580 581 // Handle SDNode leafs with special info. 582 AddNodeIDCustom(ID, N); 583 } 584 585 /// encodeMemSDNodeFlags - Generic routine for computing a value for use in 586 /// the CSE map that carries volatility, temporalness, indexing mode, and 587 /// extension/truncation information. 588 /// 589 static inline unsigned 590 encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile, 591 bool isNonTemporal, bool isInvariant) { 592 assert((ConvType & 3) == ConvType && 593 "ConvType may not require more than 2 bits!"); 594 assert((AM & 7) == AM && 595 "AM may not require more than 3 bits!"); 596 return ConvType | 597 (AM << 2) | 598 (isVolatile << 5) | 599 (isNonTemporal << 6) | 600 (isInvariant << 7); 601 } 602 603 //===----------------------------------------------------------------------===// 604 // SelectionDAG Class 605 //===----------------------------------------------------------------------===// 606 607 /// doNotCSE - Return true if CSE should not be performed for this node. 608 static bool doNotCSE(SDNode *N) { 609 if (N->getValueType(0) == MVT::Glue) 610 return true; // Never CSE anything that produces a flag. 611 612 switch (N->getOpcode()) { 613 default: break; 614 case ISD::HANDLENODE: 615 case ISD::EH_LABEL: 616 return true; // Never CSE these nodes. 617 } 618 619 // Check that remaining values produced are not flags. 620 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 621 if (N->getValueType(i) == MVT::Glue) 622 return true; // Never CSE anything that produces a flag. 623 624 return false; 625 } 626 627 /// RemoveDeadNodes - This method deletes all unreachable nodes in the 628 /// SelectionDAG. 629 void SelectionDAG::RemoveDeadNodes() { 630 // Create a dummy node (which is not added to allnodes), that adds a reference 631 // to the root node, preventing it from being deleted. 632 HandleSDNode Dummy(getRoot()); 633 634 SmallVector<SDNode*, 128> DeadNodes; 635 636 // Add all obviously-dead nodes to the DeadNodes worklist. 637 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 638 if (I->use_empty()) 639 DeadNodes.push_back(I); 640 641 RemoveDeadNodes(DeadNodes); 642 643 // If the root changed (e.g. it was a dead load, update the root). 644 setRoot(Dummy.getValue()); 645 } 646 647 /// RemoveDeadNodes - This method deletes the unreachable nodes in the 648 /// given list, and any nodes that become unreachable as a result. 649 void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) { 650 651 // Process the worklist, deleting the nodes and adding their uses to the 652 // worklist. 653 while (!DeadNodes.empty()) { 654 SDNode *N = DeadNodes.pop_back_val(); 655 656 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next) 657 DUL->NodeDeleted(N, nullptr); 658 659 // Take the node out of the appropriate CSE map. 660 RemoveNodeFromCSEMaps(N); 661 662 // Next, brutally remove the operand list. This is safe to do, as there are 663 // no cycles in the graph. 664 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { 665 SDUse &Use = *I++; 666 SDNode *Operand = Use.getNode(); 667 Use.set(SDValue()); 668 669 // Now that we removed this operand, see if there are no uses of it left. 670 if (Operand->use_empty()) 671 DeadNodes.push_back(Operand); 672 } 673 674 DeallocateNode(N); 675 } 676 } 677 678 void SelectionDAG::RemoveDeadNode(SDNode *N){ 679 SmallVector<SDNode*, 16> DeadNodes(1, N); 680 681 // Create a dummy node that adds a reference to the root node, preventing 682 // it from being deleted. (This matters if the root is an operand of the 683 // dead node.) 684 HandleSDNode Dummy(getRoot()); 685 686 RemoveDeadNodes(DeadNodes); 687 } 688 689 void SelectionDAG::DeleteNode(SDNode *N) { 690 // First take this out of the appropriate CSE map. 691 RemoveNodeFromCSEMaps(N); 692 693 // Finally, remove uses due to operands of this node, remove from the 694 // AllNodes list, and delete the node. 695 DeleteNodeNotInCSEMaps(N); 696 } 697 698 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 699 assert(N != AllNodes.begin() && "Cannot delete the entry node!"); 700 assert(N->use_empty() && "Cannot delete a node that is not dead!"); 701 702 // Drop all of the operands and decrement used node's use counts. 703 N->DropOperands(); 704 705 DeallocateNode(N); 706 } 707 708 void SDDbgInfo::erase(const SDNode *Node) { 709 DbgValMapType::iterator I = DbgValMap.find(Node); 710 if (I == DbgValMap.end()) 711 return; 712 for (auto &Val: I->second) 713 Val->setIsInvalidated(); 714 DbgValMap.erase(I); 715 } 716 717 void SelectionDAG::DeallocateNode(SDNode *N) { 718 if (N->OperandsNeedDelete) 719 delete[] N->OperandList; 720 721 // Set the opcode to DELETED_NODE to help catch bugs when node 722 // memory is reallocated. 723 N->NodeType = ISD::DELETED_NODE; 724 725 NodeAllocator.Deallocate(AllNodes.remove(N)); 726 727 // If any of the SDDbgValue nodes refer to this SDNode, invalidate 728 // them and forget about that node. 729 DbgInfo->erase(N); 730 } 731 732 #ifndef NDEBUG 733 /// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid. 734 static void VerifySDNode(SDNode *N) { 735 switch (N->getOpcode()) { 736 default: 737 break; 738 case ISD::BUILD_PAIR: { 739 EVT VT = N->getValueType(0); 740 assert(N->getNumValues() == 1 && "Too many results!"); 741 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) && 742 "Wrong return type!"); 743 assert(N->getNumOperands() == 2 && "Wrong number of operands!"); 744 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() && 745 "Mismatched operand types!"); 746 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() && 747 "Wrong operand type!"); 748 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() && 749 "Wrong return type size"); 750 break; 751 } 752 case ISD::BUILD_VECTOR: { 753 assert(N->getNumValues() == 1 && "Too many results!"); 754 assert(N->getValueType(0).isVector() && "Wrong return type!"); 755 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() && 756 "Wrong number of operands!"); 757 EVT EltVT = N->getValueType(0).getVectorElementType(); 758 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 759 assert((I->getValueType() == EltVT || 760 (EltVT.isInteger() && I->getValueType().isInteger() && 761 EltVT.bitsLE(I->getValueType()))) && 762 "Wrong operand type!"); 763 assert(I->getValueType() == N->getOperand(0).getValueType() && 764 "Operands must all have the same type"); 765 } 766 break; 767 } 768 } 769 } 770 #endif // NDEBUG 771 772 /// \brief Insert a newly allocated node into the DAG. 773 /// 774 /// Handles insertion into the all nodes list and CSE map, as well as 775 /// verification and other common operations when a new node is allocated. 776 void SelectionDAG::InsertNode(SDNode *N) { 777 AllNodes.push_back(N); 778 #ifndef NDEBUG 779 VerifySDNode(N); 780 #endif 781 } 782 783 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 784 /// correspond to it. This is useful when we're about to delete or repurpose 785 /// the node. We don't want future request for structurally identical nodes 786 /// to return N anymore. 787 bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 788 bool Erased = false; 789 switch (N->getOpcode()) { 790 case ISD::HANDLENODE: return false; // noop. 791 case ISD::CONDCODE: 792 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 793 "Cond code doesn't exist!"); 794 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr; 795 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr; 796 break; 797 case ISD::ExternalSymbol: 798 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 799 break; 800 case ISD::TargetExternalSymbol: { 801 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N); 802 Erased = TargetExternalSymbols.erase( 803 std::pair<std::string,unsigned char>(ESN->getSymbol(), 804 ESN->getTargetFlags())); 805 break; 806 } 807 case ISD::VALUETYPE: { 808 EVT VT = cast<VTSDNode>(N)->getVT(); 809 if (VT.isExtended()) { 810 Erased = ExtendedValueTypeNodes.erase(VT); 811 } else { 812 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr; 813 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr; 814 } 815 break; 816 } 817 default: 818 // Remove it from the CSE Map. 819 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!"); 820 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!"); 821 Erased = CSEMap.RemoveNode(N); 822 break; 823 } 824 #ifndef NDEBUG 825 // Verify that the node was actually in one of the CSE maps, unless it has a 826 // flag result (which cannot be CSE'd) or is one of the special cases that are 827 // not subject to CSE. 828 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue && 829 !N->isMachineOpcode() && !doNotCSE(N)) { 830 N->dump(this); 831 dbgs() << "\n"; 832 llvm_unreachable("Node is not in map!"); 833 } 834 #endif 835 return Erased; 836 } 837 838 /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE 839 /// maps and modified in place. Add it back to the CSE maps, unless an identical 840 /// node already exists, in which case transfer all its users to the existing 841 /// node. This transfer can potentially trigger recursive merging. 842 /// 843 void 844 SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) { 845 // For node types that aren't CSE'd, just act as if no identical node 846 // already exists. 847 if (!doNotCSE(N)) { 848 SDNode *Existing = CSEMap.GetOrInsertNode(N); 849 if (Existing != N) { 850 // If there was already an existing matching node, use ReplaceAllUsesWith 851 // to replace the dead one with the existing one. This can cause 852 // recursive merging of other unrelated nodes down the line. 853 ReplaceAllUsesWith(N, Existing); 854 855 // N is now dead. Inform the listeners and delete it. 856 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next) 857 DUL->NodeDeleted(N, Existing); 858 DeleteNodeNotInCSEMaps(N); 859 return; 860 } 861 } 862 863 // If the node doesn't already exist, we updated it. Inform listeners. 864 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next) 865 DUL->NodeUpdated(N); 866 } 867 868 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands 869 /// were replaced with those specified. If this node is never memoized, 870 /// return null, otherwise return a pointer to the slot it would take. If a 871 /// node already exists with these operands, the slot will be non-null. 872 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op, 873 void *&InsertPos) { 874 if (doNotCSE(N)) 875 return nullptr; 876 877 SDValue Ops[] = { Op }; 878 FoldingSetNodeID ID; 879 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops); 880 AddNodeIDCustom(ID, N); 881 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 882 return Node; 883 } 884 885 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands 886 /// were replaced with those specified. If this node is never memoized, 887 /// return null, otherwise return a pointer to the slot it would take. If a 888 /// node already exists with these operands, the slot will be non-null. 889 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 890 SDValue Op1, SDValue Op2, 891 void *&InsertPos) { 892 if (doNotCSE(N)) 893 return nullptr; 894 895 SDValue Ops[] = { Op1, Op2 }; 896 FoldingSetNodeID ID; 897 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops); 898 AddNodeIDCustom(ID, N); 899 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 900 return Node; 901 } 902 903 904 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands 905 /// were replaced with those specified. If this node is never memoized, 906 /// return null, otherwise return a pointer to the slot it would take. If a 907 /// node already exists with these operands, the slot will be non-null. 908 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops, 909 void *&InsertPos) { 910 if (doNotCSE(N)) 911 return nullptr; 912 913 FoldingSetNodeID ID; 914 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops); 915 AddNodeIDCustom(ID, N); 916 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); 917 return Node; 918 } 919 920 /// getEVTAlignment - Compute the default alignment value for the 921 /// given type. 922 /// 923 unsigned SelectionDAG::getEVTAlignment(EVT VT) const { 924 Type *Ty = VT == MVT::iPTR ? 925 PointerType::get(Type::getInt8Ty(*getContext()), 0) : 926 VT.getTypeForEVT(*getContext()); 927 928 return TLI->getDataLayout()->getABITypeAlignment(Ty); 929 } 930 931 // EntryNode could meaningfully have debug info if we can find it... 932 SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL) 933 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL), 934 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)), 935 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false), 936 UpdateListeners(nullptr) { 937 AllNodes.push_back(&EntryNode); 938 DbgInfo = new SDDbgInfo(); 939 } 940 941 void SelectionDAG::init(MachineFunction &mf) { 942 MF = &mf; 943 TLI = getSubtarget().getTargetLowering(); 944 TSI = getSubtarget().getSelectionDAGInfo(); 945 Context = &mf.getFunction()->getContext(); 946 } 947 948 SelectionDAG::~SelectionDAG() { 949 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners"); 950 allnodes_clear(); 951 delete DbgInfo; 952 } 953 954 void SelectionDAG::allnodes_clear() { 955 assert(&*AllNodes.begin() == &EntryNode); 956 AllNodes.remove(AllNodes.begin()); 957 while (!AllNodes.empty()) 958 DeallocateNode(AllNodes.begin()); 959 } 960 961 BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL, 962 SDVTList VTs, SDValue N1, 963 SDValue N2, bool nuw, bool nsw, 964 bool exact) { 965 if (isBinOpWithFlags(Opcode)) { 966 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode( 967 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2); 968 FN->Flags.setNoUnsignedWrap(nuw); 969 FN->Flags.setNoSignedWrap(nsw); 970 FN->Flags.setExact(exact); 971 972 return FN; 973 } 974 975 BinarySDNode *N = new (NodeAllocator) 976 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2); 977 return N; 978 } 979 980 void SelectionDAG::clear() { 981 allnodes_clear(); 982 OperandAllocator.Reset(); 983 CSEMap.clear(); 984 985 ExtendedValueTypeNodes.clear(); 986 ExternalSymbols.clear(); 987 TargetExternalSymbols.clear(); 988 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), 989 static_cast<CondCodeSDNode*>(nullptr)); 990 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), 991 static_cast<SDNode*>(nullptr)); 992 993 EntryNode.UseList = nullptr; 994 AllNodes.push_back(&EntryNode); 995 Root = getEntryNode(); 996 DbgInfo->clear(); 997 } 998 999 SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) { 1000 return VT.bitsGT(Op.getValueType()) ? 1001 getNode(ISD::ANY_EXTEND, DL, VT, Op) : 1002 getNode(ISD::TRUNCATE, DL, VT, Op); 1003 } 1004 1005 SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) { 1006 return VT.bitsGT(Op.getValueType()) ? 1007 getNode(ISD::SIGN_EXTEND, DL, VT, Op) : 1008 getNode(ISD::TRUNCATE, DL, VT, Op); 1009 } 1010 1011 SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) { 1012 return VT.bitsGT(Op.getValueType()) ? 1013 getNode(ISD::ZERO_EXTEND, DL, VT, Op) : 1014 getNode(ISD::TRUNCATE, DL, VT, Op); 1015 } 1016 1017 SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT, 1018 EVT OpVT) { 1019 if (VT.bitsLE(Op.getValueType())) 1020 return getNode(ISD::TRUNCATE, SL, VT, Op); 1021 1022 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT); 1023 return getNode(TLI->getExtendForContent(BType), SL, VT, Op); 1024 } 1025 1026 SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) { 1027 assert(!VT.isVector() && 1028 "getZeroExtendInReg should use the vector element type instead of " 1029 "the vector type!"); 1030 if (Op.getValueType() == VT) return Op; 1031 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 1032 APInt Imm = APInt::getLowBitsSet(BitWidth, 1033 VT.getSizeInBits()); 1034 return getNode(ISD::AND, DL, Op.getValueType(), Op, 1035 getConstant(Imm, DL, Op.getValueType())); 1036 } 1037 1038 SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) { 1039 assert(VT.isVector() && "This DAG node is restricted to vector types."); 1040 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() && 1041 "The sizes of the input and result must match in order to perform the " 1042 "extend in-register."); 1043 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() && 1044 "The destination vector type must have fewer lanes than the input."); 1045 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op); 1046 } 1047 1048 SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) { 1049 assert(VT.isVector() && "This DAG node is restricted to vector types."); 1050 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() && 1051 "The sizes of the input and result must match in order to perform the " 1052 "extend in-register."); 1053 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() && 1054 "The destination vector type must have fewer lanes than the input."); 1055 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op); 1056 } 1057 1058 SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) { 1059 assert(VT.isVector() && "This DAG node is restricted to vector types."); 1060 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() && 1061 "The sizes of the input and result must match in order to perform the " 1062 "extend in-register."); 1063 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() && 1064 "The destination vector type must have fewer lanes than the input."); 1065 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op); 1066 } 1067 1068 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1). 1069 /// 1070 SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) { 1071 EVT EltVT = VT.getScalarType(); 1072 SDValue NegOne = 1073 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT); 1074 return getNode(ISD::XOR, DL, VT, Val, NegOne); 1075 } 1076 1077 SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) { 1078 EVT EltVT = VT.getScalarType(); 1079 SDValue TrueValue; 1080 switch (TLI->getBooleanContents(VT)) { 1081 case TargetLowering::ZeroOrOneBooleanContent: 1082 case TargetLowering::UndefinedBooleanContent: 1083 TrueValue = getConstant(1, DL, VT); 1084 break; 1085 case TargetLowering::ZeroOrNegativeOneBooleanContent: 1086 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, 1087 VT); 1088 break; 1089 } 1090 return getNode(ISD::XOR, DL, VT, Val, TrueValue); 1091 } 1092 1093 SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT, 1094 bool isO) { 1095 EVT EltVT = VT.getScalarType(); 1096 assert((EltVT.getSizeInBits() >= 64 || 1097 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && 1098 "getConstant with a uint64_t value that doesn't fit in the type!"); 1099 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO); 1100 } 1101 1102 SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT, 1103 bool isO) 1104 { 1105 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO); 1106 } 1107 1108 SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT, 1109 bool isT, bool isO) { 1110 assert(VT.isInteger() && "Cannot create FP integer constant!"); 1111 1112 EVT EltVT = VT.getScalarType(); 1113 const ConstantInt *Elt = &Val; 1114 1115 // In some cases the vector type is legal but the element type is illegal and 1116 // needs to be promoted, for example v8i8 on ARM. In this case, promote the 1117 // inserted value (the type does not need to match the vector element type). 1118 // Any extra bits introduced will be truncated away. 1119 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) == 1120 TargetLowering::TypePromoteInteger) { 1121 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT); 1122 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits()); 1123 Elt = ConstantInt::get(*getContext(), NewVal); 1124 } 1125 // In other cases the element type is illegal and needs to be expanded, for 1126 // example v2i64 on MIPS32. In this case, find the nearest legal type, split 1127 // the value into n parts and use a vector type with n-times the elements. 1128 // Then bitcast to the type requested. 1129 // Legalizing constants too early makes the DAGCombiner's job harder so we 1130 // only legalize if the DAG tells us we must produce legal types. 1131 else if (NewNodesMustHaveLegalTypes && VT.isVector() && 1132 TLI->getTypeAction(*getContext(), EltVT) == 1133 TargetLowering::TypeExpandInteger) { 1134 APInt NewVal = Elt->getValue(); 1135 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT); 1136 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits(); 1137 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits; 1138 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts); 1139 1140 // Check the temporary vector is the correct size. If this fails then 1141 // getTypeToTransformTo() probably returned a type whose size (in bits) 1142 // isn't a power-of-2 factor of the requested type size. 1143 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits()); 1144 1145 SmallVector<SDValue, 2> EltParts; 1146 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) { 1147 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits) 1148 .trunc(ViaEltSizeInBits), DL, 1149 ViaEltVT, isT, isO)); 1150 } 1151 1152 // EltParts is currently in little endian order. If we actually want 1153 // big-endian order then reverse it now. 1154 if (TLI->isBigEndian()) 1155 std::reverse(EltParts.begin(), EltParts.end()); 1156 1157 // The elements must be reversed when the element order is different 1158 // to the endianness of the elements (because the BITCAST is itself a 1159 // vector shuffle in this situation). However, we do not need any code to 1160 // perform this reversal because getConstant() is producing a vector 1161 // splat. 1162 // This situation occurs in MIPS MSA. 1163 1164 SmallVector<SDValue, 8> Ops; 1165 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) 1166 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end()); 1167 1168 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT, 1169 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT, 1170 Ops)); 1171 return Result; 1172 } 1173 1174 assert(Elt->getBitWidth() == EltVT.getSizeInBits() && 1175 "APInt size does not match type size!"); 1176 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; 1177 FoldingSetNodeID ID; 1178 AddNodeIDNode(ID, Opc, getVTList(EltVT), None); 1179 ID.AddPointer(Elt); 1180 ID.AddBoolean(isO); 1181 void *IP = nullptr; 1182 SDNode *N = nullptr; 1183 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 1184 if (!VT.isVector()) 1185 return SDValue(N, 0); 1186 1187 if (!N) { 1188 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(), 1189 EltVT); 1190 CSEMap.InsertNode(N, IP); 1191 InsertNode(N); 1192 } 1193 1194 SDValue Result(N, 0); 1195 if (VT.isVector()) { 1196 SmallVector<SDValue, 8> Ops; 1197 Ops.assign(VT.getVectorNumElements(), Result); 1198 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops); 1199 } 1200 return Result; 1201 } 1202 1203 SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) { 1204 return getConstant(Val, DL, TLI->getPointerTy(), isTarget); 1205 } 1206 1207 SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT, 1208 bool isTarget) { 1209 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget); 1210 } 1211 1212 SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT, 1213 bool isTarget){ 1214 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!"); 1215 1216 EVT EltVT = VT.getScalarType(); 1217 1218 // Do the map lookup using the actual bit pattern for the floating point 1219 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 1220 // we don't have issues with SNANs. 1221 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; 1222 FoldingSetNodeID ID; 1223 AddNodeIDNode(ID, Opc, getVTList(EltVT), None); 1224 ID.AddPointer(&V); 1225 void *IP = nullptr; 1226 SDNode *N = nullptr; 1227 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 1228 if (!VT.isVector()) 1229 return SDValue(N, 0); 1230 1231 if (!N) { 1232 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT); 1233 CSEMap.InsertNode(N, IP); 1234 InsertNode(N); 1235 } 1236 1237 SDValue Result(N, 0); 1238 if (VT.isVector()) { 1239 SmallVector<SDValue, 8> Ops; 1240 Ops.assign(VT.getVectorNumElements(), Result); 1241 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops); 1242 } 1243 return Result; 1244 } 1245 1246 SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT, 1247 bool isTarget) { 1248 EVT EltVT = VT.getScalarType(); 1249 if (EltVT==MVT::f32) 1250 return getConstantFP(APFloat((float)Val), DL, VT, isTarget); 1251 else if (EltVT==MVT::f64) 1252 return getConstantFP(APFloat(Val), DL, VT, isTarget); 1253 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 || 1254 EltVT==MVT::f16) { 1255 bool ignored; 1256 APFloat apf = APFloat(Val); 1257 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven, 1258 &ignored); 1259 return getConstantFP(apf, DL, VT, isTarget); 1260 } else 1261 llvm_unreachable("Unsupported type in getConstantFP"); 1262 } 1263 1264 SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL, 1265 EVT VT, int64_t Offset, 1266 bool isTargetGA, 1267 unsigned char TargetFlags) { 1268 assert((TargetFlags == 0 || isTargetGA) && 1269 "Cannot set target flags on target-independent globals"); 1270 1271 // Truncate (with sign-extension) the offset value to the pointer size. 1272 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType()); 1273 if (BitWidth < 64) 1274 Offset = SignExtend64(Offset, BitWidth); 1275 1276 unsigned Opc; 1277 if (GV->isThreadLocal()) 1278 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress; 1279 else 1280 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; 1281 1282 FoldingSetNodeID ID; 1283 AddNodeIDNode(ID, Opc, getVTList(VT), None); 1284 ID.AddPointer(GV); 1285 ID.AddInteger(Offset); 1286 ID.AddInteger(TargetFlags); 1287 ID.AddInteger(GV->getType()->getAddressSpace()); 1288 void *IP = nullptr; 1289 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1290 return SDValue(E, 0); 1291 1292 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(), 1293 DL.getDebugLoc(), GV, VT, 1294 Offset, TargetFlags); 1295 CSEMap.InsertNode(N, IP); 1296 InsertNode(N); 1297 return SDValue(N, 0); 1298 } 1299 1300 SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) { 1301 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; 1302 FoldingSetNodeID ID; 1303 AddNodeIDNode(ID, Opc, getVTList(VT), None); 1304 ID.AddInteger(FI); 1305 void *IP = nullptr; 1306 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1307 return SDValue(E, 0); 1308 1309 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget); 1310 CSEMap.InsertNode(N, IP); 1311 InsertNode(N); 1312 return SDValue(N, 0); 1313 } 1314 1315 SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget, 1316 unsigned char TargetFlags) { 1317 assert((TargetFlags == 0 || isTarget) && 1318 "Cannot set target flags on target-independent jump tables"); 1319 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; 1320 FoldingSetNodeID ID; 1321 AddNodeIDNode(ID, Opc, getVTList(VT), None); 1322 ID.AddInteger(JTI); 1323 ID.AddInteger(TargetFlags); 1324 void *IP = nullptr; 1325 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1326 return SDValue(E, 0); 1327 1328 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget, 1329 TargetFlags); 1330 CSEMap.InsertNode(N, IP); 1331 InsertNode(N); 1332 return SDValue(N, 0); 1333 } 1334 1335 SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT, 1336 unsigned Alignment, int Offset, 1337 bool isTarget, 1338 unsigned char TargetFlags) { 1339 assert((TargetFlags == 0 || isTarget) && 1340 "Cannot set target flags on target-independent globals"); 1341 if (Alignment == 0) 1342 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType()); 1343 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 1344 FoldingSetNodeID ID; 1345 AddNodeIDNode(ID, Opc, getVTList(VT), None); 1346 ID.AddInteger(Alignment); 1347 ID.AddInteger(Offset); 1348 ID.AddPointer(C); 1349 ID.AddInteger(TargetFlags); 1350 void *IP = nullptr; 1351 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1352 return SDValue(E, 0); 1353 1354 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset, 1355 Alignment, TargetFlags); 1356 CSEMap.InsertNode(N, IP); 1357 InsertNode(N); 1358 return SDValue(N, 0); 1359 } 1360 1361 1362 SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT, 1363 unsigned Alignment, int Offset, 1364 bool isTarget, 1365 unsigned char TargetFlags) { 1366 assert((TargetFlags == 0 || isTarget) && 1367 "Cannot set target flags on target-independent globals"); 1368 if (Alignment == 0) 1369 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType()); 1370 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 1371 FoldingSetNodeID ID; 1372 AddNodeIDNode(ID, Opc, getVTList(VT), None); 1373 ID.AddInteger(Alignment); 1374 ID.AddInteger(Offset); 1375 C->addSelectionDAGCSEId(ID); 1376 ID.AddInteger(TargetFlags); 1377 void *IP = nullptr; 1378 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1379 return SDValue(E, 0); 1380 1381 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset, 1382 Alignment, TargetFlags); 1383 CSEMap.InsertNode(N, IP); 1384 InsertNode(N); 1385 return SDValue(N, 0); 1386 } 1387 1388 SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset, 1389 unsigned char TargetFlags) { 1390 FoldingSetNodeID ID; 1391 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None); 1392 ID.AddInteger(Index); 1393 ID.AddInteger(Offset); 1394 ID.AddInteger(TargetFlags); 1395 void *IP = nullptr; 1396 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1397 return SDValue(E, 0); 1398 1399 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset, 1400 TargetFlags); 1401 CSEMap.InsertNode(N, IP); 1402 InsertNode(N); 1403 return SDValue(N, 0); 1404 } 1405 1406 SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 1407 FoldingSetNodeID ID; 1408 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None); 1409 ID.AddPointer(MBB); 1410 void *IP = nullptr; 1411 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1412 return SDValue(E, 0); 1413 1414 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB); 1415 CSEMap.InsertNode(N, IP); 1416 InsertNode(N); 1417 return SDValue(N, 0); 1418 } 1419 1420 SDValue SelectionDAG::getValueType(EVT VT) { 1421 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >= 1422 ValueTypeNodes.size()) 1423 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1); 1424 1425 SDNode *&N = VT.isExtended() ? 1426 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy]; 1427 1428 if (N) return SDValue(N, 0); 1429 N = new (NodeAllocator) VTSDNode(VT); 1430 InsertNode(N); 1431 return SDValue(N, 0); 1432 } 1433 1434 SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) { 1435 SDNode *&N = ExternalSymbols[Sym]; 1436 if (N) return SDValue(N, 0); 1437 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT); 1438 InsertNode(N); 1439 return SDValue(N, 0); 1440 } 1441 1442 SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT, 1443 unsigned char TargetFlags) { 1444 SDNode *&N = 1445 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym, 1446 TargetFlags)]; 1447 if (N) return SDValue(N, 0); 1448 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT); 1449 InsertNode(N); 1450 return SDValue(N, 0); 1451 } 1452 1453 SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) { 1454 if ((unsigned)Cond >= CondCodeNodes.size()) 1455 CondCodeNodes.resize(Cond+1); 1456 1457 if (!CondCodeNodes[Cond]) { 1458 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond); 1459 CondCodeNodes[Cond] = N; 1460 InsertNode(N); 1461 } 1462 1463 return SDValue(CondCodeNodes[Cond], 0); 1464 } 1465 1466 // commuteShuffle - swaps the values of N1 and N2, and swaps all indices in 1467 // the shuffle mask M that point at N1 to point at N2, and indices that point 1468 // N2 to point at N1. 1469 static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) { 1470 std::swap(N1, N2); 1471 ShuffleVectorSDNode::commuteMask(M); 1472 } 1473 1474 SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1, 1475 SDValue N2, const int *Mask) { 1476 assert(VT == N1.getValueType() && VT == N2.getValueType() && 1477 "Invalid VECTOR_SHUFFLE"); 1478 1479 // Canonicalize shuffle undef, undef -> undef 1480 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF) 1481 return getUNDEF(VT); 1482 1483 // Validate that all indices in Mask are within the range of the elements 1484 // input to the shuffle. 1485 unsigned NElts = VT.getVectorNumElements(); 1486 SmallVector<int, 8> MaskVec; 1487 for (unsigned i = 0; i != NElts; ++i) { 1488 assert(Mask[i] < (int)(NElts * 2) && "Index out of range"); 1489 MaskVec.push_back(Mask[i]); 1490 } 1491 1492 // Canonicalize shuffle v, v -> v, undef 1493 if (N1 == N2) { 1494 N2 = getUNDEF(VT); 1495 for (unsigned i = 0; i != NElts; ++i) 1496 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts; 1497 } 1498 1499 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. 1500 if (N1.getOpcode() == ISD::UNDEF) 1501 commuteShuffle(N1, N2, MaskVec); 1502 1503 // If shuffling a splat, try to blend the splat instead. We do this here so 1504 // that even when this arises during lowering we don't have to re-handle it. 1505 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) { 1506 BitVector UndefElements; 1507 SDValue Splat = BV->getSplatValue(&UndefElements); 1508 if (!Splat) 1509 return; 1510 1511 for (int i = 0; i < (int)NElts; ++i) { 1512 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts)) 1513 continue; 1514 1515 // If this input comes from undef, mark it as such. 1516 if (UndefElements[MaskVec[i] - Offset]) { 1517 MaskVec[i] = -1; 1518 continue; 1519 } 1520 1521 // If we can blend a non-undef lane, use that instead. 1522 if (!UndefElements[i]) 1523 MaskVec[i] = i + Offset; 1524 } 1525 }; 1526 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1)) 1527 BlendSplat(N1BV, 0); 1528 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2)) 1529 BlendSplat(N2BV, NElts); 1530 1531 // Canonicalize all index into lhs, -> shuffle lhs, undef 1532 // Canonicalize all index into rhs, -> shuffle rhs, undef 1533 bool AllLHS = true, AllRHS = true; 1534 bool N2Undef = N2.getOpcode() == ISD::UNDEF; 1535 for (unsigned i = 0; i != NElts; ++i) { 1536 if (MaskVec[i] >= (int)NElts) { 1537 if (N2Undef) 1538 MaskVec[i] = -1; 1539 else 1540 AllLHS = false; 1541 } else if (MaskVec[i] >= 0) { 1542 AllRHS = false; 1543 } 1544 } 1545 if (AllLHS && AllRHS) 1546 return getUNDEF(VT); 1547 if (AllLHS && !N2Undef) 1548 N2 = getUNDEF(VT); 1549 if (AllRHS) { 1550 N1 = getUNDEF(VT); 1551 commuteShuffle(N1, N2, MaskVec); 1552 } 1553 // Reset our undef status after accounting for the mask. 1554 N2Undef = N2.getOpcode() == ISD::UNDEF; 1555 // Re-check whether both sides ended up undef. 1556 if (N1.getOpcode() == ISD::UNDEF && N2Undef) 1557 return getUNDEF(VT); 1558 1559 // If Identity shuffle return that node. 1560 bool Identity = true, AllSame = true; 1561 for (unsigned i = 0; i != NElts; ++i) { 1562 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false; 1563 if (MaskVec[i] != MaskVec[0]) AllSame = false; 1564 } 1565 if (Identity && NElts) 1566 return N1; 1567 1568 // Shuffling a constant splat doesn't change the result. 1569 if (N2Undef) { 1570 SDValue V = N1; 1571 1572 // Look through any bitcasts. We check that these don't change the number 1573 // (and size) of elements and just changes their types. 1574 while (V.getOpcode() == ISD::BITCAST) 1575 V = V->getOperand(0); 1576 1577 // A splat should always show up as a build vector node. 1578 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) { 1579 BitVector UndefElements; 1580 SDValue Splat = BV->getSplatValue(&UndefElements); 1581 // If this is a splat of an undef, shuffling it is also undef. 1582 if (Splat && Splat.getOpcode() == ISD::UNDEF) 1583 return getUNDEF(VT); 1584 1585 bool SameNumElts = 1586 V.getValueType().getVectorNumElements() == VT.getVectorNumElements(); 1587 1588 // We only have a splat which can skip shuffles if there is a splatted 1589 // value and no undef lanes rearranged by the shuffle. 1590 if (Splat && UndefElements.none()) { 1591 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the 1592 // number of elements match or the value splatted is a zero constant. 1593 if (SameNumElts) 1594 return N1; 1595 if (auto *C = dyn_cast<ConstantSDNode>(Splat)) 1596 if (C->isNullValue()) 1597 return N1; 1598 } 1599 1600 // If the shuffle itself creates a splat, build the vector directly. 1601 if (AllSame && SameNumElts) { 1602 const SDValue &Splatted = BV->getOperand(MaskVec[0]); 1603 SmallVector<SDValue, 8> Ops(NElts, Splatted); 1604 1605 EVT BuildVT = BV->getValueType(0); 1606 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops); 1607 1608 // We may have jumped through bitcasts, so the type of the 1609 // BUILD_VECTOR may not match the type of the shuffle. 1610 if (BuildVT != VT) 1611 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV); 1612 return NewBV; 1613 } 1614 } 1615 } 1616 1617 FoldingSetNodeID ID; 1618 SDValue Ops[2] = { N1, N2 }; 1619 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops); 1620 for (unsigned i = 0; i != NElts; ++i) 1621 ID.AddInteger(MaskVec[i]); 1622 1623 void* IP = nullptr; 1624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1625 return SDValue(E, 0); 1626 1627 // Allocate the mask array for the node out of the BumpPtrAllocator, since 1628 // SDNode doesn't have access to it. This memory will be "leaked" when 1629 // the node is deallocated, but recovered when the NodeAllocator is released. 1630 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts); 1631 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int)); 1632 1633 ShuffleVectorSDNode *N = 1634 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(), 1635 dl.getDebugLoc(), N1, N2, 1636 MaskAlloc); 1637 CSEMap.InsertNode(N, IP); 1638 InsertNode(N); 1639 return SDValue(N, 0); 1640 } 1641 1642 SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) { 1643 MVT VT = SV.getSimpleValueType(0); 1644 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end()); 1645 ShuffleVectorSDNode::commuteMask(MaskVec); 1646 1647 SDValue Op0 = SV.getOperand(0); 1648 SDValue Op1 = SV.getOperand(1); 1649 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]); 1650 } 1651 1652 SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl, 1653 SDValue Val, SDValue DTy, 1654 SDValue STy, SDValue Rnd, SDValue Sat, 1655 ISD::CvtCode Code) { 1656 // If the src and dest types are the same and the conversion is between 1657 // integer types of the same sign or two floats, no conversion is necessary. 1658 if (DTy == STy && 1659 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF)) 1660 return Val; 1661 1662 FoldingSetNodeID ID; 1663 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat }; 1664 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops); 1665 void* IP = nullptr; 1666 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1667 return SDValue(E, 0); 1668 1669 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(), 1670 dl.getDebugLoc(), 1671 Ops, Code); 1672 CSEMap.InsertNode(N, IP); 1673 InsertNode(N); 1674 return SDValue(N, 0); 1675 } 1676 1677 SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) { 1678 FoldingSetNodeID ID; 1679 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None); 1680 ID.AddInteger(RegNo); 1681 void *IP = nullptr; 1682 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1683 return SDValue(E, 0); 1684 1685 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT); 1686 CSEMap.InsertNode(N, IP); 1687 InsertNode(N); 1688 return SDValue(N, 0); 1689 } 1690 1691 SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) { 1692 FoldingSetNodeID ID; 1693 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None); 1694 ID.AddPointer(RegMask); 1695 void *IP = nullptr; 1696 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1697 return SDValue(E, 0); 1698 1699 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask); 1700 CSEMap.InsertNode(N, IP); 1701 InsertNode(N); 1702 return SDValue(N, 0); 1703 } 1704 1705 SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) { 1706 FoldingSetNodeID ID; 1707 SDValue Ops[] = { Root }; 1708 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops); 1709 ID.AddPointer(Label); 1710 void *IP = nullptr; 1711 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1712 return SDValue(E, 0); 1713 1714 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(), 1715 dl.getDebugLoc(), Root, Label); 1716 CSEMap.InsertNode(N, IP); 1717 InsertNode(N); 1718 return SDValue(N, 0); 1719 } 1720 1721 1722 SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT, 1723 int64_t Offset, 1724 bool isTarget, 1725 unsigned char TargetFlags) { 1726 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress; 1727 1728 FoldingSetNodeID ID; 1729 AddNodeIDNode(ID, Opc, getVTList(VT), None); 1730 ID.AddPointer(BA); 1731 ID.AddInteger(Offset); 1732 ID.AddInteger(TargetFlags); 1733 void *IP = nullptr; 1734 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1735 return SDValue(E, 0); 1736 1737 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset, 1738 TargetFlags); 1739 CSEMap.InsertNode(N, IP); 1740 InsertNode(N); 1741 return SDValue(N, 0); 1742 } 1743 1744 SDValue SelectionDAG::getSrcValue(const Value *V) { 1745 assert((!V || V->getType()->isPointerTy()) && 1746 "SrcValue is not a pointer?"); 1747 1748 FoldingSetNodeID ID; 1749 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None); 1750 ID.AddPointer(V); 1751 1752 void *IP = nullptr; 1753 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1754 return SDValue(E, 0); 1755 1756 SDNode *N = new (NodeAllocator) SrcValueSDNode(V); 1757 CSEMap.InsertNode(N, IP); 1758 InsertNode(N); 1759 return SDValue(N, 0); 1760 } 1761 1762 /// getMDNode - Return an MDNodeSDNode which holds an MDNode. 1763 SDValue SelectionDAG::getMDNode(const MDNode *MD) { 1764 FoldingSetNodeID ID; 1765 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None); 1766 ID.AddPointer(MD); 1767 1768 void *IP = nullptr; 1769 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1770 return SDValue(E, 0); 1771 1772 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD); 1773 CSEMap.InsertNode(N, IP); 1774 InsertNode(N); 1775 return SDValue(N, 0); 1776 } 1777 1778 /// getAddrSpaceCast - Return an AddrSpaceCastSDNode. 1779 SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr, 1780 unsigned SrcAS, unsigned DestAS) { 1781 SDValue Ops[] = {Ptr}; 1782 FoldingSetNodeID ID; 1783 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops); 1784 ID.AddInteger(SrcAS); 1785 ID.AddInteger(DestAS); 1786 1787 void *IP = nullptr; 1788 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1789 return SDValue(E, 0); 1790 1791 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(), 1792 dl.getDebugLoc(), 1793 VT, Ptr, SrcAS, DestAS); 1794 CSEMap.InsertNode(N, IP); 1795 InsertNode(N); 1796 return SDValue(N, 0); 1797 } 1798 1799 /// getShiftAmountOperand - Return the specified value casted to 1800 /// the target's desired shift amount type. 1801 SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) { 1802 EVT OpTy = Op.getValueType(); 1803 EVT ShTy = TLI->getShiftAmountTy(LHSTy); 1804 if (OpTy == ShTy || OpTy.isVector()) return Op; 1805 1806 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; 1807 return getNode(Opcode, SDLoc(Op), ShTy, Op); 1808 } 1809 1810 /// CreateStackTemporary - Create a stack temporary, suitable for holding the 1811 /// specified value type. 1812 SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) { 1813 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 1814 unsigned ByteSize = VT.getStoreSize(); 1815 Type *Ty = VT.getTypeForEVT(*getContext()); 1816 unsigned StackAlign = 1817 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign); 1818 1819 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); 1820 return getFrameIndex(FrameIdx, TLI->getPointerTy()); 1821 } 1822 1823 /// CreateStackTemporary - Create a stack temporary suitable for holding 1824 /// either of the specified value types. 1825 SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) { 1826 unsigned Bytes = std::max(VT1.getStoreSizeInBits(), 1827 VT2.getStoreSizeInBits())/8; 1828 Type *Ty1 = VT1.getTypeForEVT(*getContext()); 1829 Type *Ty2 = VT2.getTypeForEVT(*getContext()); 1830 const DataLayout *TD = TLI->getDataLayout(); 1831 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1), 1832 TD->getPrefTypeAlignment(Ty2)); 1833 1834 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 1835 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false); 1836 return getFrameIndex(FrameIdx, TLI->getPointerTy()); 1837 } 1838 1839 SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, 1840 SDValue N2, ISD::CondCode Cond, SDLoc dl) { 1841 // These setcc operations always fold. 1842 switch (Cond) { 1843 default: break; 1844 case ISD::SETFALSE: 1845 case ISD::SETFALSE2: return getConstant(0, dl, VT); 1846 case ISD::SETTRUE: 1847 case ISD::SETTRUE2: { 1848 TargetLowering::BooleanContent Cnt = 1849 TLI->getBooleanContents(N1->getValueType(0)); 1850 return getConstant( 1851 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl, 1852 VT); 1853 } 1854 1855 case ISD::SETOEQ: 1856 case ISD::SETOGT: 1857 case ISD::SETOGE: 1858 case ISD::SETOLT: 1859 case ISD::SETOLE: 1860 case ISD::SETONE: 1861 case ISD::SETO: 1862 case ISD::SETUO: 1863 case ISD::SETUEQ: 1864 case ISD::SETUNE: 1865 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!"); 1866 break; 1867 } 1868 1869 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) { 1870 const APInt &C2 = N2C->getAPIntValue(); 1871 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 1872 const APInt &C1 = N1C->getAPIntValue(); 1873 1874 switch (Cond) { 1875 default: llvm_unreachable("Unknown integer setcc!"); 1876 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT); 1877 case ISD::SETNE: return getConstant(C1 != C2, dl, VT); 1878 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT); 1879 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT); 1880 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT); 1881 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT); 1882 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT); 1883 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT); 1884 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT); 1885 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT); 1886 } 1887 } 1888 } 1889 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) { 1890 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) { 1891 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF()); 1892 switch (Cond) { 1893 default: break; 1894 case ISD::SETEQ: if (R==APFloat::cmpUnordered) 1895 return getUNDEF(VT); 1896 // fall through 1897 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT); 1898 case ISD::SETNE: if (R==APFloat::cmpUnordered) 1899 return getUNDEF(VT); 1900 // fall through 1901 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan || 1902 R==APFloat::cmpLessThan, dl, VT); 1903 case ISD::SETLT: if (R==APFloat::cmpUnordered) 1904 return getUNDEF(VT); 1905 // fall through 1906 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT); 1907 case ISD::SETGT: if (R==APFloat::cmpUnordered) 1908 return getUNDEF(VT); 1909 // fall through 1910 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT); 1911 case ISD::SETLE: if (R==APFloat::cmpUnordered) 1912 return getUNDEF(VT); 1913 // fall through 1914 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan || 1915 R==APFloat::cmpEqual, dl, VT); 1916 case ISD::SETGE: if (R==APFloat::cmpUnordered) 1917 return getUNDEF(VT); 1918 // fall through 1919 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan || 1920 R==APFloat::cmpEqual, dl, VT); 1921 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT); 1922 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT); 1923 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered || 1924 R==APFloat::cmpEqual, dl, VT); 1925 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT); 1926 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered || 1927 R==APFloat::cmpLessThan, dl, VT); 1928 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan || 1929 R==APFloat::cmpUnordered, dl, VT); 1930 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT); 1931 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT); 1932 } 1933 } else { 1934 // Ensure that the constant occurs on the RHS. 1935 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond); 1936 MVT CompVT = N1.getValueType().getSimpleVT(); 1937 if (!TLI->isCondCodeLegal(SwappedCond, CompVT)) 1938 return SDValue(); 1939 1940 return getSetCC(dl, VT, N2, N1, SwappedCond); 1941 } 1942 } 1943 1944 // Could not fold it. 1945 return SDValue(); 1946 } 1947 1948 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We 1949 /// use this predicate to simplify operations downstream. 1950 bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const { 1951 // This predicate is not safe for vector operations. 1952 if (Op.getValueType().isVector()) 1953 return false; 1954 1955 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 1956 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth); 1957 } 1958 1959 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 1960 /// this predicate to simplify operations downstream. Mask is known to be zero 1961 /// for bits that V cannot have. 1962 bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask, 1963 unsigned Depth) const { 1964 APInt KnownZero, KnownOne; 1965 computeKnownBits(Op, KnownZero, KnownOne, Depth); 1966 return (KnownZero & Mask) == Mask; 1967 } 1968 1969 /// Determine which bits of Op are known to be either zero or one and return 1970 /// them in the KnownZero/KnownOne bitsets. 1971 void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero, 1972 APInt &KnownOne, unsigned Depth) const { 1973 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); 1974 1975 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything. 1976 if (Depth == 6) 1977 return; // Limit search depth. 1978 1979 APInt KnownZero2, KnownOne2; 1980 1981 switch (Op.getOpcode()) { 1982 case ISD::Constant: 1983 // We know all of the bits for a constant! 1984 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue(); 1985 KnownZero = ~KnownOne; 1986 break; 1987 case ISD::AND: 1988 // If either the LHS or the RHS are Zero, the result is zero. 1989 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); 1990 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 1991 1992 // Output known-1 bits are only known if set in both the LHS & RHS. 1993 KnownOne &= KnownOne2; 1994 // Output known-0 are known to be clear if zero in either the LHS | RHS. 1995 KnownZero |= KnownZero2; 1996 break; 1997 case ISD::OR: 1998 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); 1999 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 2000 2001 // Output known-0 bits are only known if clear in both the LHS & RHS. 2002 KnownZero &= KnownZero2; 2003 // Output known-1 are known to be set if set in either the LHS | RHS. 2004 KnownOne |= KnownOne2; 2005 break; 2006 case ISD::XOR: { 2007 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); 2008 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 2009 2010 // Output known-0 bits are known if clear or set in both the LHS & RHS. 2011 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 2012 // Output known-1 are known to be set if set in only one of the LHS, RHS. 2013 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 2014 KnownZero = KnownZeroOut; 2015 break; 2016 } 2017 case ISD::MUL: { 2018 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); 2019 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 2020 2021 // If low bits are zero in either operand, output low known-0 bits. 2022 // Also compute a conserative estimate for high known-0 bits. 2023 // More trickiness is possible, but this is sufficient for the 2024 // interesting case of alignment computation. 2025 KnownOne.clearAllBits(); 2026 unsigned TrailZ = KnownZero.countTrailingOnes() + 2027 KnownZero2.countTrailingOnes(); 2028 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + 2029 KnownZero2.countLeadingOnes(), 2030 BitWidth) - BitWidth; 2031 2032 TrailZ = std::min(TrailZ, BitWidth); 2033 LeadZ = std::min(LeadZ, BitWidth); 2034 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | 2035 APInt::getHighBitsSet(BitWidth, LeadZ); 2036 break; 2037 } 2038 case ISD::UDIV: { 2039 // For the purposes of computing leading zeros we can conservatively 2040 // treat a udiv as a logical right shift by the power of 2 known to 2041 // be less than the denominator. 2042 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 2043 unsigned LeadZ = KnownZero2.countLeadingOnes(); 2044 2045 KnownOne2.clearAllBits(); 2046 KnownZero2.clearAllBits(); 2047 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); 2048 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); 2049 if (RHSUnknownLeadingOnes != BitWidth) 2050 LeadZ = std::min(BitWidth, 2051 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); 2052 2053 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ); 2054 break; 2055 } 2056 case ISD::SELECT: 2057 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1); 2058 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); 2059 2060 // Only known if known in both the LHS and RHS. 2061 KnownOne &= KnownOne2; 2062 KnownZero &= KnownZero2; 2063 break; 2064 case ISD::SELECT_CC: 2065 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1); 2066 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1); 2067 2068 // Only known if known in both the LHS and RHS. 2069 KnownOne &= KnownOne2; 2070 KnownZero &= KnownZero2; 2071 break; 2072 case ISD::SADDO: 2073 case ISD::UADDO: 2074 case ISD::SSUBO: 2075 case ISD::USUBO: 2076 case ISD::SMULO: 2077 case ISD::UMULO: 2078 if (Op.getResNo() != 1) 2079 break; 2080 // The boolean result conforms to getBooleanContents. 2081 // If we know the result of a setcc has the top bits zero, use this info. 2082 // We know that we have an integer-based boolean since these operations 2083 // are only available for integer. 2084 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) == 2085 TargetLowering::ZeroOrOneBooleanContent && 2086 BitWidth > 1) 2087 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 2088 break; 2089 case ISD::SETCC: 2090 // If we know the result of a setcc has the top bits zero, use this info. 2091 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) == 2092 TargetLowering::ZeroOrOneBooleanContent && 2093 BitWidth > 1) 2094 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 2095 break; 2096 case ISD::SHL: 2097 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 2098 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2099 unsigned ShAmt = SA->getZExtValue(); 2100 2101 // If the shift count is an invalid immediate, don't do anything. 2102 if (ShAmt >= BitWidth) 2103 break; 2104 2105 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2106 KnownZero <<= ShAmt; 2107 KnownOne <<= ShAmt; 2108 // low bits known zero. 2109 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt); 2110 } 2111 break; 2112 case ISD::SRL: 2113 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 2114 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2115 unsigned ShAmt = SA->getZExtValue(); 2116 2117 // If the shift count is an invalid immediate, don't do anything. 2118 if (ShAmt >= BitWidth) 2119 break; 2120 2121 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2122 KnownZero = KnownZero.lshr(ShAmt); 2123 KnownOne = KnownOne.lshr(ShAmt); 2124 2125 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); 2126 KnownZero |= HighBits; // High bits known zero. 2127 } 2128 break; 2129 case ISD::SRA: 2130 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2131 unsigned ShAmt = SA->getZExtValue(); 2132 2133 // If the shift count is an invalid immediate, don't do anything. 2134 if (ShAmt >= BitWidth) 2135 break; 2136 2137 // If any of the demanded bits are produced by the sign extension, we also 2138 // demand the input sign bit. 2139 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); 2140 2141 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2142 KnownZero = KnownZero.lshr(ShAmt); 2143 KnownOne = KnownOne.lshr(ShAmt); 2144 2145 // Handle the sign bits. 2146 APInt SignBit = APInt::getSignBit(BitWidth); 2147 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask. 2148 2149 if (KnownZero.intersects(SignBit)) { 2150 KnownZero |= HighBits; // New bits are known zero. 2151 } else if (KnownOne.intersects(SignBit)) { 2152 KnownOne |= HighBits; // New bits are known one. 2153 } 2154 } 2155 break; 2156 case ISD::SIGN_EXTEND_INREG: { 2157 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2158 unsigned EBits = EVT.getScalarType().getSizeInBits(); 2159 2160 // Sign extension. Compute the demanded bits in the result that are not 2161 // present in the input. 2162 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits); 2163 2164 APInt InSignBit = APInt::getSignBit(EBits); 2165 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits); 2166 2167 // If the sign extended bits are demanded, we know that the sign 2168 // bit is demanded. 2169 InSignBit = InSignBit.zext(BitWidth); 2170 if (NewBits.getBoolValue()) 2171 InputDemandedBits |= InSignBit; 2172 2173 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2174 KnownOne &= InputDemandedBits; 2175 KnownZero &= InputDemandedBits; 2176 2177 // If the sign bit of the input is known set or clear, then we know the 2178 // top bits of the result. 2179 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear 2180 KnownZero |= NewBits; 2181 KnownOne &= ~NewBits; 2182 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set 2183 KnownOne |= NewBits; 2184 KnownZero &= ~NewBits; 2185 } else { // Input sign bit unknown 2186 KnownZero &= ~NewBits; 2187 KnownOne &= ~NewBits; 2188 } 2189 break; 2190 } 2191 case ISD::CTTZ: 2192 case ISD::CTTZ_ZERO_UNDEF: 2193 case ISD::CTLZ: 2194 case ISD::CTLZ_ZERO_UNDEF: 2195 case ISD::CTPOP: { 2196 unsigned LowBits = Log2_32(BitWidth)+1; 2197 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); 2198 KnownOne.clearAllBits(); 2199 break; 2200 } 2201 case ISD::LOAD: { 2202 LoadSDNode *LD = cast<LoadSDNode>(Op); 2203 // If this is a ZEXTLoad and we are looking at the loaded value. 2204 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) { 2205 EVT VT = LD->getMemoryVT(); 2206 unsigned MemBits = VT.getScalarType().getSizeInBits(); 2207 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 2208 } else if (const MDNode *Ranges = LD->getRanges()) { 2209 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero); 2210 } 2211 break; 2212 } 2213 case ISD::ZERO_EXTEND: { 2214 EVT InVT = Op.getOperand(0).getValueType(); 2215 unsigned InBits = InVT.getScalarType().getSizeInBits(); 2216 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits); 2217 KnownZero = KnownZero.trunc(InBits); 2218 KnownOne = KnownOne.trunc(InBits); 2219 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2220 KnownZero = KnownZero.zext(BitWidth); 2221 KnownOne = KnownOne.zext(BitWidth); 2222 KnownZero |= NewBits; 2223 break; 2224 } 2225 case ISD::SIGN_EXTEND: { 2226 EVT InVT = Op.getOperand(0).getValueType(); 2227 unsigned InBits = InVT.getScalarType().getSizeInBits(); 2228 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits); 2229 2230 KnownZero = KnownZero.trunc(InBits); 2231 KnownOne = KnownOne.trunc(InBits); 2232 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2233 2234 // Note if the sign bit is known to be zero or one. 2235 bool SignBitKnownZero = KnownZero.isNegative(); 2236 bool SignBitKnownOne = KnownOne.isNegative(); 2237 2238 KnownZero = KnownZero.zext(BitWidth); 2239 KnownOne = KnownOne.zext(BitWidth); 2240 2241 // If the sign bit is known zero or one, the top bits match. 2242 if (SignBitKnownZero) 2243 KnownZero |= NewBits; 2244 else if (SignBitKnownOne) 2245 KnownOne |= NewBits; 2246 break; 2247 } 2248 case ISD::ANY_EXTEND: { 2249 EVT InVT = Op.getOperand(0).getValueType(); 2250 unsigned InBits = InVT.getScalarType().getSizeInBits(); 2251 KnownZero = KnownZero.trunc(InBits); 2252 KnownOne = KnownOne.trunc(InBits); 2253 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2254 KnownZero = KnownZero.zext(BitWidth); 2255 KnownOne = KnownOne.zext(BitWidth); 2256 break; 2257 } 2258 case ISD::TRUNCATE: { 2259 EVT InVT = Op.getOperand(0).getValueType(); 2260 unsigned InBits = InVT.getScalarType().getSizeInBits(); 2261 KnownZero = KnownZero.zext(InBits); 2262 KnownOne = KnownOne.zext(InBits); 2263 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2264 KnownZero = KnownZero.trunc(BitWidth); 2265 KnownOne = KnownOne.trunc(BitWidth); 2266 break; 2267 } 2268 case ISD::AssertZext: { 2269 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2270 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits()); 2271 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2272 KnownZero |= (~InMask); 2273 KnownOne &= (~KnownZero); 2274 break; 2275 } 2276 case ISD::FGETSIGN: 2277 // All bits are zero except the low bit. 2278 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1); 2279 break; 2280 2281 case ISD::SUB: { 2282 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) { 2283 // We know that the top bits of C-X are clear if X contains less bits 2284 // than C (i.e. no wrap-around can happen). For example, 20-X is 2285 // positive if we can prove that X is >= 0 and < 16. 2286 if (CLHS->getAPIntValue().isNonNegative()) { 2287 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros(); 2288 // NLZ can't be BitWidth with no sign bit 2289 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); 2290 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); 2291 2292 // If all of the MaskV bits are known to be zero, then we know the 2293 // output top bits are zero, because we now know that the output is 2294 // from [0-C]. 2295 if ((KnownZero2 & MaskV) == MaskV) { 2296 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros(); 2297 // Top bits known zero. 2298 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2); 2299 } 2300 } 2301 } 2302 } 2303 // fall through 2304 case ISD::ADD: 2305 case ISD::ADDE: { 2306 // Output known-0 bits are known if clear or set in both the low clear bits 2307 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the 2308 // low 3 bits clear. 2309 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 2310 unsigned KnownZeroOut = KnownZero2.countTrailingOnes(); 2311 2312 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); 2313 KnownZeroOut = std::min(KnownZeroOut, 2314 KnownZero2.countTrailingOnes()); 2315 2316 if (Op.getOpcode() == ISD::ADD) { 2317 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut); 2318 break; 2319 } 2320 2321 // With ADDE, a carry bit may be added in, so we can only use this 2322 // information if we know (at least) that the low two bits are clear. We 2323 // then return to the caller that the low bit is unknown but that other bits 2324 // are known zero. 2325 if (KnownZeroOut >= 2) // ADDE 2326 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut); 2327 break; 2328 } 2329 case ISD::SREM: 2330 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2331 const APInt &RA = Rem->getAPIntValue().abs(); 2332 if (RA.isPowerOf2()) { 2333 APInt LowBits = RA - 1; 2334 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1); 2335 2336 // The low bits of the first operand are unchanged by the srem. 2337 KnownZero = KnownZero2 & LowBits; 2338 KnownOne = KnownOne2 & LowBits; 2339 2340 // If the first operand is non-negative or has all low bits zero, then 2341 // the upper bits are all zero. 2342 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) 2343 KnownZero |= ~LowBits; 2344 2345 // If the first operand is negative and not all low bits are zero, then 2346 // the upper bits are all one. 2347 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) 2348 KnownOne |= ~LowBits; 2349 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 2350 } 2351 } 2352 break; 2353 case ISD::UREM: { 2354 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2355 const APInt &RA = Rem->getAPIntValue(); 2356 if (RA.isPowerOf2()) { 2357 APInt LowBits = (RA - 1); 2358 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1); 2359 2360 // The upper bits are all zero, the lower ones are unchanged. 2361 KnownZero = KnownZero2 | ~LowBits; 2362 KnownOne = KnownOne2 & LowBits; 2363 break; 2364 } 2365 } 2366 2367 // Since the result is less than or equal to either operand, any leading 2368 // zero bits in either operand must also exist in the result. 2369 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2370 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); 2371 2372 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(), 2373 KnownZero2.countLeadingOnes()); 2374 KnownOne.clearAllBits(); 2375 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders); 2376 break; 2377 } 2378 case ISD::EXTRACT_ELEMENT: { 2379 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2380 const unsigned Index = 2381 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2382 const unsigned BitWidth = Op.getValueType().getSizeInBits(); 2383 2384 // Remove low part of known bits mask 2385 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth); 2386 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth); 2387 2388 // Remove high part of known bit mask 2389 KnownZero = KnownZero.trunc(BitWidth); 2390 KnownOne = KnownOne.trunc(BitWidth); 2391 break; 2392 } 2393 case ISD::FrameIndex: 2394 case ISD::TargetFrameIndex: 2395 if (unsigned Align = InferPtrAlignment(Op)) { 2396 // The low bits are known zero if the pointer is aligned. 2397 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align)); 2398 break; 2399 } 2400 break; 2401 2402 default: 2403 if (Op.getOpcode() < ISD::BUILTIN_OP_END) 2404 break; 2405 // Fallthrough 2406 case ISD::INTRINSIC_WO_CHAIN: 2407 case ISD::INTRINSIC_W_CHAIN: 2408 case ISD::INTRINSIC_VOID: 2409 // Allow the target to implement this method for its nodes. 2410 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth); 2411 break; 2412 } 2413 2414 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 2415 } 2416 2417 /// ComputeNumSignBits - Return the number of times the sign bit of the 2418 /// register is replicated into the other bits. We know that at least 1 bit 2419 /// is always equal to the sign bit (itself), but other cases can give us 2420 /// information. For example, immediately after an "SRA X, 2", we know that 2421 /// the top 3 bits are all equal to each other, so we return 3. 2422 unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{ 2423 EVT VT = Op.getValueType(); 2424 assert(VT.isInteger() && "Invalid VT!"); 2425 unsigned VTBits = VT.getScalarType().getSizeInBits(); 2426 unsigned Tmp, Tmp2; 2427 unsigned FirstAnswer = 1; 2428 2429 if (Depth == 6) 2430 return 1; // Limit search depth. 2431 2432 switch (Op.getOpcode()) { 2433 default: break; 2434 case ISD::AssertSext: 2435 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 2436 return VTBits-Tmp+1; 2437 case ISD::AssertZext: 2438 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 2439 return VTBits-Tmp; 2440 2441 case ISD::Constant: { 2442 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue(); 2443 return Val.getNumSignBits(); 2444 } 2445 2446 case ISD::SIGN_EXTEND: 2447 Tmp = 2448 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); 2449 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp; 2450 2451 case ISD::SIGN_EXTEND_INREG: 2452 // Max of the input and what this extends. 2453 Tmp = 2454 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits(); 2455 Tmp = VTBits-Tmp+1; 2456 2457 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2458 return std::max(Tmp, Tmp2); 2459 2460 case ISD::SRA: 2461 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2462 // SRA X, C -> adds C sign bits. 2463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2464 Tmp += C->getZExtValue(); 2465 if (Tmp > VTBits) Tmp = VTBits; 2466 } 2467 return Tmp; 2468 case ISD::SHL: 2469 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2470 // shl destroys sign bits. 2471 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2472 if (C->getZExtValue() >= VTBits || // Bad shift. 2473 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out. 2474 return Tmp - C->getZExtValue(); 2475 } 2476 break; 2477 case ISD::AND: 2478 case ISD::OR: 2479 case ISD::XOR: // NOT is handled here. 2480 // Logical binary ops preserve the number of sign bits at the worst. 2481 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2482 if (Tmp != 1) { 2483 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2484 FirstAnswer = std::min(Tmp, Tmp2); 2485 // We computed what we know about the sign bits as our first 2486 // answer. Now proceed to the generic code that uses 2487 // computeKnownBits, and pick whichever answer is better. 2488 } 2489 break; 2490 2491 case ISD::SELECT: 2492 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2493 if (Tmp == 1) return 1; // Early out. 2494 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1); 2495 return std::min(Tmp, Tmp2); 2496 2497 case ISD::SADDO: 2498 case ISD::UADDO: 2499 case ISD::SSUBO: 2500 case ISD::USUBO: 2501 case ISD::SMULO: 2502 case ISD::UMULO: 2503 if (Op.getResNo() != 1) 2504 break; 2505 // The boolean result conforms to getBooleanContents. Fall through. 2506 // If setcc returns 0/-1, all bits are sign bits. 2507 // We know that we have an integer-based boolean since these operations 2508 // are only available for integer. 2509 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) == 2510 TargetLowering::ZeroOrNegativeOneBooleanContent) 2511 return VTBits; 2512 break; 2513 case ISD::SETCC: 2514 // If setcc returns 0/-1, all bits are sign bits. 2515 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) == 2516 TargetLowering::ZeroOrNegativeOneBooleanContent) 2517 return VTBits; 2518 break; 2519 case ISD::ROTL: 2520 case ISD::ROTR: 2521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2522 unsigned RotAmt = C->getZExtValue() & (VTBits-1); 2523 2524 // Handle rotate right by N like a rotate left by 32-N. 2525 if (Op.getOpcode() == ISD::ROTR) 2526 RotAmt = (VTBits-RotAmt) & (VTBits-1); 2527 2528 // If we aren't rotating out all of the known-in sign bits, return the 2529 // number that are left. This handles rotl(sext(x), 1) for example. 2530 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2531 if (Tmp > RotAmt+1) return Tmp-RotAmt; 2532 } 2533 break; 2534 case ISD::ADD: 2535 // Add can have at most one carry bit. Thus we know that the output 2536 // is, at worst, one more bit than the inputs. 2537 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2538 if (Tmp == 1) return 1; // Early out. 2539 2540 // Special case decrementing a value (ADD X, -1): 2541 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 2542 if (CRHS->isAllOnesValue()) { 2543 APInt KnownZero, KnownOne; 2544 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 2545 2546 // If the input is known to be 0 or 1, the output is 0/-1, which is all 2547 // sign bits set. 2548 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue()) 2549 return VTBits; 2550 2551 // If we are subtracting one from a positive number, there is no carry 2552 // out of the result. 2553 if (KnownZero.isNegative()) 2554 return Tmp; 2555 } 2556 2557 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2558 if (Tmp2 == 1) return 1; 2559 return std::min(Tmp, Tmp2)-1; 2560 2561 case ISD::SUB: 2562 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 2563 if (Tmp2 == 1) return 1; 2564 2565 // Handle NEG. 2566 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 2567 if (CLHS->isNullValue()) { 2568 APInt KnownZero, KnownOne; 2569 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); 2570 // If the input is known to be 0 or 1, the output is 0/-1, which is all 2571 // sign bits set. 2572 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue()) 2573 return VTBits; 2574 2575 // If the input is known to be positive (the sign bit is known clear), 2576 // the output of the NEG has the same number of sign bits as the input. 2577 if (KnownZero.isNegative()) 2578 return Tmp2; 2579 2580 // Otherwise, we treat this like a SUB. 2581 } 2582 2583 // Sub can have at most one carry bit. Thus we know that the output 2584 // is, at worst, one more bit than the inputs. 2585 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2586 if (Tmp == 1) return 1; // Early out. 2587 return std::min(Tmp, Tmp2)-1; 2588 case ISD::TRUNCATE: 2589 // FIXME: it's tricky to do anything useful for this, but it is an important 2590 // case for targets like X86. 2591 break; 2592 case ISD::EXTRACT_ELEMENT: { 2593 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1); 2594 const int BitWidth = Op.getValueType().getSizeInBits(); 2595 const int Items = 2596 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth; 2597 2598 // Get reverse index (starting from 1), Op1 value indexes elements from 2599 // little end. Sign starts at big end. 2600 const int rIndex = Items - 1 - 2601 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2602 2603 // If the sign portion ends in our element the substraction gives correct 2604 // result. Otherwise it gives either negative or > bitwidth result 2605 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0); 2606 } 2607 } 2608 2609 // If we are looking at the loaded value of the SDNode. 2610 if (Op.getResNo() == 0) { 2611 // Handle LOADX separately here. EXTLOAD case will fallthrough. 2612 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { 2613 unsigned ExtType = LD->getExtensionType(); 2614 switch (ExtType) { 2615 default: break; 2616 case ISD::SEXTLOAD: // '17' bits known 2617 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); 2618 return VTBits-Tmp+1; 2619 case ISD::ZEXTLOAD: // '16' bits known 2620 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); 2621 return VTBits-Tmp; 2622 } 2623 } 2624 } 2625 2626 // Allow the target to implement this method for its nodes. 2627 if (Op.getOpcode() >= ISD::BUILTIN_OP_END || 2628 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2629 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2630 Op.getOpcode() == ISD::INTRINSIC_VOID) { 2631 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth); 2632 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits); 2633 } 2634 2635 // Finally, if we can prove that the top bits of the result are 0's or 1's, 2636 // use this information. 2637 APInt KnownZero, KnownOne; 2638 computeKnownBits(Op, KnownZero, KnownOne, Depth); 2639 2640 APInt Mask; 2641 if (KnownZero.isNegative()) { // sign bit is 0 2642 Mask = KnownZero; 2643 } else if (KnownOne.isNegative()) { // sign bit is 1; 2644 Mask = KnownOne; 2645 } else { 2646 // Nothing known. 2647 return FirstAnswer; 2648 } 2649 2650 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine 2651 // the number of identical bits in the top of the input value. 2652 Mask = ~Mask; 2653 Mask <<= Mask.getBitWidth()-VTBits; 2654 // Return # leading zeros. We use 'min' here in case Val was zero before 2655 // shifting. We don't want to return '64' as for an i32 "0". 2656 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros())); 2657 } 2658 2659 /// isBaseWithConstantOffset - Return true if the specified operand is an 2660 /// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an 2661 /// ISD::OR with a ConstantSDNode that is guaranteed to have the same 2662 /// semantics as an ADD. This handles the equivalence: 2663 /// X|Cst == X+Cst iff X&Cst = 0. 2664 bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const { 2665 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) || 2666 !isa<ConstantSDNode>(Op.getOperand(1))) 2667 return false; 2668 2669 if (Op.getOpcode() == ISD::OR && 2670 !MaskedValueIsZero(Op.getOperand(0), 2671 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue())) 2672 return false; 2673 2674 return true; 2675 } 2676 2677 2678 bool SelectionDAG::isKnownNeverNaN(SDValue Op) const { 2679 // If we're told that NaNs won't happen, assume they won't. 2680 if (getTarget().Options.NoNaNsFPMath) 2681 return true; 2682 2683 // If the value is a constant, we can obviously see if it is a NaN or not. 2684 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 2685 return !C->getValueAPF().isNaN(); 2686 2687 // TODO: Recognize more cases here. 2688 2689 return false; 2690 } 2691 2692 bool SelectionDAG::isKnownNeverZero(SDValue Op) const { 2693 // If the value is a constant, we can obviously see if it is a zero or not. 2694 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 2695 return !C->isZero(); 2696 2697 // TODO: Recognize more cases here. 2698 switch (Op.getOpcode()) { 2699 default: break; 2700 case ISD::OR: 2701 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 2702 return !C->isNullValue(); 2703 break; 2704 } 2705 2706 return false; 2707 } 2708 2709 bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const { 2710 // Check the obvious case. 2711 if (A == B) return true; 2712 2713 // For for negative and positive zero. 2714 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) 2715 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) 2716 if (CA->isZero() && CB->isZero()) return true; 2717 2718 // Otherwise they may not be equal. 2719 return false; 2720 } 2721 2722 /// getNode - Gets or creates the specified node. 2723 /// 2724 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) { 2725 FoldingSetNodeID ID; 2726 AddNodeIDNode(ID, Opcode, getVTList(VT), None); 2727 void *IP = nullptr; 2728 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2729 return SDValue(E, 0); 2730 2731 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), 2732 DL.getDebugLoc(), getVTList(VT)); 2733 CSEMap.InsertNode(N, IP); 2734 2735 InsertNode(N); 2736 return SDValue(N, 0); 2737 } 2738 2739 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, 2740 EVT VT, SDValue Operand) { 2741 // Constant fold unary operations with an integer constant operand. Even 2742 // opaque constant will be folded, because the folding of unary operations 2743 // doesn't create new constants with different values. Nevertheless, the 2744 // opaque flag is preserved during folding to prevent future folding with 2745 // other constants. 2746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) { 2747 const APInt &Val = C->getAPIntValue(); 2748 switch (Opcode) { 2749 default: break; 2750 case ISD::SIGN_EXTEND: 2751 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT, 2752 C->isTargetOpcode(), C->isOpaque()); 2753 case ISD::ANY_EXTEND: 2754 case ISD::ZERO_EXTEND: 2755 case ISD::TRUNCATE: 2756 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT, 2757 C->isTargetOpcode(), C->isOpaque()); 2758 case ISD::UINT_TO_FP: 2759 case ISD::SINT_TO_FP: { 2760 APFloat apf(EVTToAPFloatSemantics(VT), 2761 APInt::getNullValue(VT.getSizeInBits())); 2762 (void)apf.convertFromAPInt(Val, 2763 Opcode==ISD::SINT_TO_FP, 2764 APFloat::rmNearestTiesToEven); 2765 return getConstantFP(apf, DL, VT); 2766 } 2767 case ISD::BITCAST: 2768 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16) 2769 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT); 2770 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) 2771 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT); 2772 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) 2773 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT); 2774 break; 2775 case ISD::BSWAP: 2776 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(), 2777 C->isOpaque()); 2778 case ISD::CTPOP: 2779 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(), 2780 C->isOpaque()); 2781 case ISD::CTLZ: 2782 case ISD::CTLZ_ZERO_UNDEF: 2783 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(), 2784 C->isOpaque()); 2785 case ISD::CTTZ: 2786 case ISD::CTTZ_ZERO_UNDEF: 2787 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(), 2788 C->isOpaque()); 2789 } 2790 } 2791 2792 // Constant fold unary operations with a floating point constant operand. 2793 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) { 2794 APFloat V = C->getValueAPF(); // make copy 2795 switch (Opcode) { 2796 case ISD::FNEG: 2797 V.changeSign(); 2798 return getConstantFP(V, DL, VT); 2799 case ISD::FABS: 2800 V.clearSign(); 2801 return getConstantFP(V, DL, VT); 2802 case ISD::FCEIL: { 2803 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive); 2804 if (fs == APFloat::opOK || fs == APFloat::opInexact) 2805 return getConstantFP(V, DL, VT); 2806 break; 2807 } 2808 case ISD::FTRUNC: { 2809 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero); 2810 if (fs == APFloat::opOK || fs == APFloat::opInexact) 2811 return getConstantFP(V, DL, VT); 2812 break; 2813 } 2814 case ISD::FFLOOR: { 2815 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative); 2816 if (fs == APFloat::opOK || fs == APFloat::opInexact) 2817 return getConstantFP(V, DL, VT); 2818 break; 2819 } 2820 case ISD::FP_EXTEND: { 2821 bool ignored; 2822 // This can return overflow, underflow, or inexact; we don't care. 2823 // FIXME need to be more flexible about rounding mode. 2824 (void)V.convert(EVTToAPFloatSemantics(VT), 2825 APFloat::rmNearestTiesToEven, &ignored); 2826 return getConstantFP(V, DL, VT); 2827 } 2828 case ISD::FP_TO_SINT: 2829 case ISD::FP_TO_UINT: { 2830 integerPart x[2]; 2831 bool ignored; 2832 static_assert(integerPartWidth >= 64, "APFloat parts too small!"); 2833 // FIXME need to be more flexible about rounding mode. 2834 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(), 2835 Opcode==ISD::FP_TO_SINT, 2836 APFloat::rmTowardZero, &ignored); 2837 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual 2838 break; 2839 APInt api(VT.getSizeInBits(), x); 2840 return getConstant(api, DL, VT); 2841 } 2842 case ISD::BITCAST: 2843 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16) 2844 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT); 2845 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) 2846 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT); 2847 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) 2848 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT); 2849 break; 2850 } 2851 } 2852 2853 // Constant fold unary operations with a vector integer or float operand. 2854 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) { 2855 if (BV->isConstant()) { 2856 switch (Opcode) { 2857 default: 2858 // FIXME: Entirely reasonable to perform folding of other unary 2859 // operations here as the need arises. 2860 break; 2861 case ISD::FNEG: 2862 case ISD::FABS: 2863 case ISD::FCEIL: 2864 case ISD::FTRUNC: 2865 case ISD::FFLOOR: 2866 case ISD::FP_EXTEND: 2867 case ISD::FP_TO_SINT: 2868 case ISD::FP_TO_UINT: 2869 case ISD::TRUNCATE: 2870 case ISD::UINT_TO_FP: 2871 case ISD::SINT_TO_FP: { 2872 EVT SVT = VT.getScalarType(); 2873 EVT InVT = BV->getValueType(0); 2874 EVT InSVT = InVT.getScalarType(); 2875 2876 // Find legal integer scalar type for constant promotion. 2877 EVT LegalSVT = SVT; 2878 if (SVT.isInteger()) { 2879 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT); 2880 assert(LegalSVT.bitsGE(SVT) && "Unexpected legal scalar type size"); 2881 } 2882 2883 // Let the above scalar folding handle the folding of each element. 2884 SmallVector<SDValue, 8> Ops; 2885 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) { 2886 SDValue OpN = BV->getOperand(i); 2887 EVT OpVT = OpN.getValueType(); 2888 2889 // Build vector (integer) scalar operands may need implicit 2890 // truncation - do this before constant folding. 2891 if (OpVT.isInteger() && OpVT.bitsGT(InSVT)) 2892 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN); 2893 2894 OpN = getNode(Opcode, DL, SVT, OpN); 2895 2896 // Legalize the (integer) scalar constant if necessary. 2897 if (LegalSVT != SVT) 2898 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN); 2899 2900 if (OpN.getOpcode() != ISD::UNDEF && 2901 OpN.getOpcode() != ISD::Constant && 2902 OpN.getOpcode() != ISD::ConstantFP) 2903 break; 2904 Ops.push_back(OpN); 2905 } 2906 if (Ops.size() == VT.getVectorNumElements()) 2907 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops); 2908 break; 2909 } 2910 } 2911 } 2912 } 2913 2914 unsigned OpOpcode = Operand.getNode()->getOpcode(); 2915 switch (Opcode) { 2916 case ISD::TokenFactor: 2917 case ISD::MERGE_VALUES: 2918 case ISD::CONCAT_VECTORS: 2919 return Operand; // Factor, merge or concat of one node? No need. 2920 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node"); 2921 case ISD::FP_EXTEND: 2922 assert(VT.isFloatingPoint() && 2923 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!"); 2924 if (Operand.getValueType() == VT) return Operand; // noop conversion. 2925 assert((!VT.isVector() || 2926 VT.getVectorNumElements() == 2927 Operand.getValueType().getVectorNumElements()) && 2928 "Vector element count mismatch!"); 2929 if (Operand.getOpcode() == ISD::UNDEF) 2930 return getUNDEF(VT); 2931 break; 2932 case ISD::SIGN_EXTEND: 2933 assert(VT.isInteger() && Operand.getValueType().isInteger() && 2934 "Invalid SIGN_EXTEND!"); 2935 if (Operand.getValueType() == VT) return Operand; // noop extension 2936 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2937 "Invalid sext node, dst < src!"); 2938 assert((!VT.isVector() || 2939 VT.getVectorNumElements() == 2940 Operand.getValueType().getVectorNumElements()) && 2941 "Vector element count mismatch!"); 2942 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 2943 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2944 else if (OpOpcode == ISD::UNDEF) 2945 // sext(undef) = 0, because the top bits will all be the same. 2946 return getConstant(0, DL, VT); 2947 break; 2948 case ISD::ZERO_EXTEND: 2949 assert(VT.isInteger() && Operand.getValueType().isInteger() && 2950 "Invalid ZERO_EXTEND!"); 2951 if (Operand.getValueType() == VT) return Operand; // noop extension 2952 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2953 "Invalid zext node, dst < src!"); 2954 assert((!VT.isVector() || 2955 VT.getVectorNumElements() == 2956 Operand.getValueType().getVectorNumElements()) && 2957 "Vector element count mismatch!"); 2958 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 2959 return getNode(ISD::ZERO_EXTEND, DL, VT, 2960 Operand.getNode()->getOperand(0)); 2961 else if (OpOpcode == ISD::UNDEF) 2962 // zext(undef) = 0, because the top bits will be zero. 2963 return getConstant(0, DL, VT); 2964 break; 2965 case ISD::ANY_EXTEND: 2966 assert(VT.isInteger() && Operand.getValueType().isInteger() && 2967 "Invalid ANY_EXTEND!"); 2968 if (Operand.getValueType() == VT) return Operand; // noop extension 2969 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && 2970 "Invalid anyext node, dst < src!"); 2971 assert((!VT.isVector() || 2972 VT.getVectorNumElements() == 2973 Operand.getValueType().getVectorNumElements()) && 2974 "Vector element count mismatch!"); 2975 2976 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 2977 OpOpcode == ISD::ANY_EXTEND) 2978 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 2979 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 2980 else if (OpOpcode == ISD::UNDEF) 2981 return getUNDEF(VT); 2982 2983 // (ext (trunx x)) -> x 2984 if (OpOpcode == ISD::TRUNCATE) { 2985 SDValue OpOp = Operand.getNode()->getOperand(0); 2986 if (OpOp.getValueType() == VT) 2987 return OpOp; 2988 } 2989 break; 2990 case ISD::TRUNCATE: 2991 assert(VT.isInteger() && Operand.getValueType().isInteger() && 2992 "Invalid TRUNCATE!"); 2993 if (Operand.getValueType() == VT) return Operand; // noop truncate 2994 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) && 2995 "Invalid truncate node, src < dst!"); 2996 assert((!VT.isVector() || 2997 VT.getVectorNumElements() == 2998 Operand.getValueType().getVectorNumElements()) && 2999 "Vector element count mismatch!"); 3000 if (OpOpcode == ISD::TRUNCATE) 3001 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); 3002 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 3003 OpOpcode == ISD::ANY_EXTEND) { 3004 // If the source is smaller than the dest, we still need an extend. 3005 if (Operand.getNode()->getOperand(0).getValueType().getScalarType() 3006 .bitsLT(VT.getScalarType())) 3007 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); 3008 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT)) 3009 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); 3010 return Operand.getNode()->getOperand(0); 3011 } 3012 if (OpOpcode == ISD::UNDEF) 3013 return getUNDEF(VT); 3014 break; 3015 case ISD::BITCAST: 3016 // Basic sanity checking. 3017 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits() 3018 && "Cannot BITCAST between types of different sizes!"); 3019 if (VT == Operand.getValueType()) return Operand; // noop conversion. 3020 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x) 3021 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0)); 3022 if (OpOpcode == ISD::UNDEF) 3023 return getUNDEF(VT); 3024 break; 3025 case ISD::SCALAR_TO_VECTOR: 3026 assert(VT.isVector() && !Operand.getValueType().isVector() && 3027 (VT.getVectorElementType() == Operand.getValueType() || 3028 (VT.getVectorElementType().isInteger() && 3029 Operand.getValueType().isInteger() && 3030 VT.getVectorElementType().bitsLE(Operand.getValueType()))) && 3031 "Illegal SCALAR_TO_VECTOR node!"); 3032 if (OpOpcode == ISD::UNDEF) 3033 return getUNDEF(VT); 3034 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined. 3035 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT && 3036 isa<ConstantSDNode>(Operand.getOperand(1)) && 3037 Operand.getConstantOperandVal(1) == 0 && 3038 Operand.getOperand(0).getValueType() == VT) 3039 return Operand.getOperand(0); 3040 break; 3041 case ISD::FNEG: 3042 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0 3043 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB) 3044 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1), 3045 Operand.getNode()->getOperand(0)); 3046 if (OpOpcode == ISD::FNEG) // --X -> X 3047 return Operand.getNode()->getOperand(0); 3048 break; 3049 case ISD::FABS: 3050 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 3051 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0)); 3052 break; 3053 } 3054 3055 SDNode *N; 3056 SDVTList VTs = getVTList(VT); 3057 if (VT != MVT::Glue) { // Don't CSE flag producing nodes 3058 FoldingSetNodeID ID; 3059 SDValue Ops[1] = { Operand }; 3060 AddNodeIDNode(ID, Opcode, VTs, Ops); 3061 void *IP = nullptr; 3062 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 3063 return SDValue(E, 0); 3064 3065 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), 3066 DL.getDebugLoc(), VTs, Operand); 3067 CSEMap.InsertNode(N, IP); 3068 } else { 3069 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), 3070 DL.getDebugLoc(), VTs, Operand); 3071 } 3072 3073 InsertNode(N); 3074 return SDValue(N, 0); 3075 } 3076 3077 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT, 3078 SDNode *Cst1, SDNode *Cst2) { 3079 // If the opcode is a target-specific ISD node, there's nothing we can 3080 // do here and the operand rules may not line up with the below, so 3081 // bail early. 3082 if (Opcode >= ISD::BUILTIN_OP_END) 3083 return SDValue(); 3084 3085 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs; 3086 SmallVector<SDValue, 4> Outputs; 3087 EVT SVT = VT.getScalarType(); 3088 3089 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1); 3090 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2); 3091 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque())) 3092 return SDValue(); 3093 3094 if (Scalar1 && Scalar2) 3095 // Scalar instruction. 3096 Inputs.push_back(std::make_pair(Scalar1, Scalar2)); 3097 else { 3098 // For vectors extract each constant element into Inputs so we can constant 3099 // fold them individually. 3100 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1); 3101 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2); 3102 if (!BV1 || !BV2) 3103 return SDValue(); 3104 3105 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!"); 3106 3107 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) { 3108 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I)); 3109 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I)); 3110 if (!V1 || !V2) // Not a constant, bail. 3111 return SDValue(); 3112 3113 if (V1->isOpaque() || V2->isOpaque()) 3114 return SDValue(); 3115 3116 // Avoid BUILD_VECTOR nodes that perform implicit truncation. 3117 // FIXME: This is valid and could be handled by truncating the APInts. 3118 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT) 3119 return SDValue(); 3120 3121 Inputs.push_back(std::make_pair(V1, V2)); 3122 } 3123 } 3124 3125 // We have a number of constant values, constant fold them element by element. 3126 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) { 3127 const APInt &C1 = Inputs[I].first->getAPIntValue(); 3128 const APInt &C2 = Inputs[I].second->getAPIntValue(); 3129 3130 switch (Opcode) { 3131 case ISD::ADD: 3132 Outputs.push_back(getConstant(C1 + C2, DL, SVT)); 3133 break; 3134 case ISD::SUB: 3135 Outputs.push_back(getConstant(C1 - C2, DL, SVT)); 3136 break; 3137 case ISD::MUL: 3138 Outputs.push_back(getConstant(C1 * C2, DL, SVT)); 3139 break; 3140 case ISD::UDIV: 3141 if (!C2.getBoolValue()) 3142 return SDValue(); 3143 Outputs.push_back(getConstant(C1.udiv(C2), DL, SVT)); 3144 break; 3145 case ISD::UREM: 3146 if (!C2.getBoolValue()) 3147 return SDValue(); 3148 Outputs.push_back(getConstant(C1.urem(C2), DL, SVT)); 3149 break; 3150 case ISD::SDIV: 3151 if (!C2.getBoolValue()) 3152 return SDValue(); 3153 Outputs.push_back(getConstant(C1.sdiv(C2), DL, SVT)); 3154 break; 3155 case ISD::SREM: 3156 if (!C2.getBoolValue()) 3157 return SDValue(); 3158 Outputs.push_back(getConstant(C1.srem(C2), DL, SVT)); 3159 break; 3160 case ISD::AND: 3161 Outputs.push_back(getConstant(C1 & C2, DL, SVT)); 3162 break; 3163 case ISD::OR: 3164 Outputs.push_back(getConstant(C1 | C2, DL, SVT)); 3165 break; 3166 case ISD::XOR: 3167 Outputs.push_back(getConstant(C1 ^ C2, DL, SVT)); 3168 break; 3169 case ISD::SHL: 3170 Outputs.push_back(getConstant(C1 << C2, DL, SVT)); 3171 break; 3172 case ISD::SRL: 3173 Outputs.push_back(getConstant(C1.lshr(C2), DL, SVT)); 3174 break; 3175 case ISD::SRA: 3176 Outputs.push_back(getConstant(C1.ashr(C2), DL, SVT)); 3177 break; 3178 case ISD::ROTL: 3179 Outputs.push_back(getConstant(C1.rotl(C2), DL, SVT)); 3180 break; 3181 case ISD::ROTR: 3182 Outputs.push_back(getConstant(C1.rotr(C2), DL, SVT)); 3183 break; 3184 default: 3185 return SDValue(); 3186 } 3187 } 3188 3189 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() && 3190 "Expected a scalar or vector!")); 3191 3192 // Handle the scalar case first. 3193 if (!VT.isVector()) 3194 return Outputs.back(); 3195 3196 // We may have a vector type but a scalar result. Create a splat. 3197 Outputs.resize(VT.getVectorNumElements(), Outputs.back()); 3198 3199 // Build a big vector out of the scalar elements we generated. 3200 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs); 3201 } 3202 3203 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, 3204 SDValue N2, bool nuw, bool nsw, bool exact) { 3205 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 3206 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); 3207 switch (Opcode) { 3208 default: break; 3209 case ISD::TokenFactor: 3210 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 3211 N2.getValueType() == MVT::Other && "Invalid token factor!"); 3212 // Fold trivial token factors. 3213 if (N1.getOpcode() == ISD::EntryToken) return N2; 3214 if (N2.getOpcode() == ISD::EntryToken) return N1; 3215 if (N1 == N2) return N1; 3216 break; 3217 case ISD::CONCAT_VECTORS: 3218 // Concat of UNDEFs is UNDEF. 3219 if (N1.getOpcode() == ISD::UNDEF && 3220 N2.getOpcode() == ISD::UNDEF) 3221 return getUNDEF(VT); 3222 3223 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to 3224 // one big BUILD_VECTOR. 3225 if (N1.getOpcode() == ISD::BUILD_VECTOR && 3226 N2.getOpcode() == ISD::BUILD_VECTOR) { 3227 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), 3228 N1.getNode()->op_end()); 3229 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end()); 3230 3231 // BUILD_VECTOR requires all inputs to be of the same type, find the 3232 // maximum type and extend them all. 3233 EVT SVT = VT.getScalarType(); 3234 for (SDValue Op : Elts) 3235 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT); 3236 if (SVT.bitsGT(VT.getScalarType())) 3237 for (SDValue &Op : Elts) 3238 Op = TLI->isZExtFree(Op.getValueType(), SVT) 3239 ? getZExtOrTrunc(Op, DL, SVT) 3240 : getSExtOrTrunc(Op, DL, SVT); 3241 3242 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts); 3243 } 3244 break; 3245 case ISD::AND: 3246 assert(VT.isInteger() && "This operator does not apply to FP types!"); 3247 assert(N1.getValueType() == N2.getValueType() && 3248 N1.getValueType() == VT && "Binary operator types must match!"); 3249 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's 3250 // worth handling here. 3251 if (N2C && N2C->isNullValue()) 3252 return N2; 3253 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X 3254 return N1; 3255 break; 3256 case ISD::OR: 3257 case ISD::XOR: 3258 case ISD::ADD: 3259 case ISD::SUB: 3260 assert(VT.isInteger() && "This operator does not apply to FP types!"); 3261 assert(N1.getValueType() == N2.getValueType() && 3262 N1.getValueType() == VT && "Binary operator types must match!"); 3263 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so 3264 // it's worth handling here. 3265 if (N2C && N2C->isNullValue()) 3266 return N1; 3267 break; 3268 case ISD::UDIV: 3269 case ISD::UREM: 3270 case ISD::MULHU: 3271 case ISD::MULHS: 3272 case ISD::MUL: 3273 case ISD::SDIV: 3274 case ISD::SREM: 3275 assert(VT.isInteger() && "This operator does not apply to FP types!"); 3276 assert(N1.getValueType() == N2.getValueType() && 3277 N1.getValueType() == VT && "Binary operator types must match!"); 3278 break; 3279 case ISD::FADD: 3280 case ISD::FSUB: 3281 case ISD::FMUL: 3282 case ISD::FDIV: 3283 case ISD::FREM: 3284 if (getTarget().Options.UnsafeFPMath) { 3285 if (Opcode == ISD::FADD) { 3286 // 0+x --> x 3287 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) 3288 if (CFP->getValueAPF().isZero()) 3289 return N2; 3290 // x+0 --> x 3291 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) 3292 if (CFP->getValueAPF().isZero()) 3293 return N1; 3294 } else if (Opcode == ISD::FSUB) { 3295 // x-0 --> x 3296 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) 3297 if (CFP->getValueAPF().isZero()) 3298 return N1; 3299 } else if (Opcode == ISD::FMUL) { 3300 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1); 3301 SDValue V = N2; 3302 3303 // If the first operand isn't the constant, try the second 3304 if (!CFP) { 3305 CFP = dyn_cast<ConstantFPSDNode>(N2); 3306 V = N1; 3307 } 3308 3309 if (CFP) { 3310 // 0*x --> 0 3311 if (CFP->isZero()) 3312 return SDValue(CFP,0); 3313 // 1*x --> x 3314 if (CFP->isExactlyValue(1.0)) 3315 return V; 3316 } 3317 } 3318 } 3319 assert(VT.isFloatingPoint() && "This operator only applies to FP types!"); 3320 assert(N1.getValueType() == N2.getValueType() && 3321 N1.getValueType() == VT && "Binary operator types must match!"); 3322 break; 3323 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. 3324 assert(N1.getValueType() == VT && 3325 N1.getValueType().isFloatingPoint() && 3326 N2.getValueType().isFloatingPoint() && 3327 "Invalid FCOPYSIGN!"); 3328 break; 3329 case ISD::SHL: 3330 case ISD::SRA: 3331 case ISD::SRL: 3332 case ISD::ROTL: 3333 case ISD::ROTR: 3334 assert(VT == N1.getValueType() && 3335 "Shift operators return type must be the same as their first arg"); 3336 assert(VT.isInteger() && N2.getValueType().isInteger() && 3337 "Shifts only work on integers"); 3338 assert((!VT.isVector() || VT == N2.getValueType()) && 3339 "Vector shift amounts must be in the same as their first arg"); 3340 // Verify that the shift amount VT is bit enough to hold valid shift 3341 // amounts. This catches things like trying to shift an i1024 value by an 3342 // i8, which is easy to fall into in generic code that uses 3343 // TLI.getShiftAmount(). 3344 assert(N2.getValueType().getSizeInBits() >= 3345 Log2_32_Ceil(N1.getValueType().getSizeInBits()) && 3346 "Invalid use of small shift amount with oversized value!"); 3347 3348 // Always fold shifts of i1 values so the code generator doesn't need to 3349 // handle them. Since we know the size of the shift has to be less than the 3350 // size of the value, the shift/rotate count is guaranteed to be zero. 3351 if (VT == MVT::i1) 3352 return N1; 3353 if (N2C && N2C->isNullValue()) 3354 return N1; 3355 break; 3356 case ISD::FP_ROUND_INREG: { 3357 EVT EVT = cast<VTSDNode>(N2)->getVT(); 3358 assert(VT == N1.getValueType() && "Not an inreg round!"); 3359 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() && 3360 "Cannot FP_ROUND_INREG integer types"); 3361 assert(EVT.isVector() == VT.isVector() && 3362 "FP_ROUND_INREG type should be vector iff the operand " 3363 "type is vector!"); 3364 assert((!EVT.isVector() || 3365 EVT.getVectorNumElements() == VT.getVectorNumElements()) && 3366 "Vector element counts must match in FP_ROUND_INREG"); 3367 assert(EVT.bitsLE(VT) && "Not rounding down!"); 3368 (void)EVT; 3369 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 3370 break; 3371 } 3372 case ISD::FP_ROUND: 3373 assert(VT.isFloatingPoint() && 3374 N1.getValueType().isFloatingPoint() && 3375 VT.bitsLE(N1.getValueType()) && 3376 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!"); 3377 if (N1.getValueType() == VT) return N1; // noop conversion. 3378 break; 3379 case ISD::AssertSext: 3380 case ISD::AssertZext: { 3381 EVT EVT = cast<VTSDNode>(N2)->getVT(); 3382 assert(VT == N1.getValueType() && "Not an inreg extend!"); 3383 assert(VT.isInteger() && EVT.isInteger() && 3384 "Cannot *_EXTEND_INREG FP types"); 3385 assert(!EVT.isVector() && 3386 "AssertSExt/AssertZExt type should be the vector element type " 3387 "rather than the vector type!"); 3388 assert(EVT.bitsLE(VT) && "Not extending!"); 3389 if (VT == EVT) return N1; // noop assertion. 3390 break; 3391 } 3392 case ISD::SIGN_EXTEND_INREG: { 3393 EVT EVT = cast<VTSDNode>(N2)->getVT(); 3394 assert(VT == N1.getValueType() && "Not an inreg extend!"); 3395 assert(VT.isInteger() && EVT.isInteger() && 3396 "Cannot *_EXTEND_INREG FP types"); 3397 assert(EVT.isVector() == VT.isVector() && 3398 "SIGN_EXTEND_INREG type should be vector iff the operand " 3399 "type is vector!"); 3400 assert((!EVT.isVector() || 3401 EVT.getVectorNumElements() == VT.getVectorNumElements()) && 3402 "Vector element counts must match in SIGN_EXTEND_INREG"); 3403 assert(EVT.bitsLE(VT) && "Not extending!"); 3404 if (EVT == VT) return N1; // Not actually extending 3405 3406 if (N1C) { 3407 APInt Val = N1C->getAPIntValue(); 3408 unsigned FromBits = EVT.getScalarType().getSizeInBits(); 3409 Val <<= Val.getBitWidth()-FromBits; 3410 Val = Val.ashr(Val.getBitWidth()-FromBits); 3411 return getConstant(Val, DL, VT); 3412 } 3413 break; 3414 } 3415 case ISD::EXTRACT_VECTOR_ELT: 3416 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF. 3417 if (N1.getOpcode() == ISD::UNDEF) 3418 return getUNDEF(VT); 3419 3420 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is 3421 // expanding copies of large vectors from registers. 3422 if (N2C && 3423 N1.getOpcode() == ISD::CONCAT_VECTORS && 3424 N1.getNumOperands() > 0) { 3425 unsigned Factor = 3426 N1.getOperand(0).getValueType().getVectorNumElements(); 3427 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, 3428 N1.getOperand(N2C->getZExtValue() / Factor), 3429 getConstant(N2C->getZExtValue() % Factor, DL, 3430 N2.getValueType())); 3431 } 3432 3433 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is 3434 // expanding large vector constants. 3435 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) { 3436 SDValue Elt = N1.getOperand(N2C->getZExtValue()); 3437 3438 if (VT != Elt.getValueType()) 3439 // If the vector element type is not legal, the BUILD_VECTOR operands 3440 // are promoted and implicitly truncated, and the result implicitly 3441 // extended. Make that explicit here. 3442 Elt = getAnyExtOrTrunc(Elt, DL, VT); 3443 3444 return Elt; 3445 } 3446 3447 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector 3448 // operations are lowered to scalars. 3449 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) { 3450 // If the indices are the same, return the inserted element else 3451 // if the indices are known different, extract the element from 3452 // the original vector. 3453 SDValue N1Op2 = N1.getOperand(2); 3454 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode()); 3455 3456 if (N1Op2C && N2C) { 3457 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) { 3458 if (VT == N1.getOperand(1).getValueType()) 3459 return N1.getOperand(1); 3460 else 3461 return getSExtOrTrunc(N1.getOperand(1), DL, VT); 3462 } 3463 3464 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2); 3465 } 3466 } 3467 break; 3468 case ISD::EXTRACT_ELEMENT: 3469 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!"); 3470 assert(!N1.getValueType().isVector() && !VT.isVector() && 3471 (N1.getValueType().isInteger() == VT.isInteger()) && 3472 N1.getValueType() != VT && 3473 "Wrong types for EXTRACT_ELEMENT!"); 3474 3475 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding 3476 // 64-bit integers into 32-bit parts. Instead of building the extract of 3477 // the BUILD_PAIR, only to have legalize rip it apart, just do it now. 3478 if (N1.getOpcode() == ISD::BUILD_PAIR) 3479 return N1.getOperand(N2C->getZExtValue()); 3480 3481 // EXTRACT_ELEMENT of a constant int is also very common. 3482 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 3483 unsigned ElementSize = VT.getSizeInBits(); 3484 unsigned Shift = ElementSize * N2C->getZExtValue(); 3485 APInt ShiftedVal = C->getAPIntValue().lshr(Shift); 3486 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT); 3487 } 3488 break; 3489 case ISD::EXTRACT_SUBVECTOR: { 3490 SDValue Index = N2; 3491 if (VT.isSimple() && N1.getValueType().isSimple()) { 3492 assert(VT.isVector() && N1.getValueType().isVector() && 3493 "Extract subvector VTs must be a vectors!"); 3494 assert(VT.getVectorElementType() == 3495 N1.getValueType().getVectorElementType() && 3496 "Extract subvector VTs must have the same element type!"); 3497 assert(VT.getSimpleVT() <= N1.getSimpleValueType() && 3498 "Extract subvector must be from larger vector to smaller vector!"); 3499 3500 if (isa<ConstantSDNode>(Index.getNode())) { 3501 assert((VT.getVectorNumElements() + 3502 cast<ConstantSDNode>(Index.getNode())->getZExtValue() 3503 <= N1.getValueType().getVectorNumElements()) 3504 && "Extract subvector overflow!"); 3505 } 3506 3507 // Trivial extraction. 3508 if (VT.getSimpleVT() == N1.getSimpleValueType()) 3509 return N1; 3510 } 3511 break; 3512 } 3513 } 3514 3515 // Perform trivial constant folding. 3516 if (SDValue SV = 3517 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode())) 3518 return SV; 3519 3520 // Canonicalize constant to RHS if commutative. 3521 if (N1C && !N2C && isCommutativeBinOp(Opcode)) { 3522 std::swap(N1C, N2C); 3523 std::swap(N1, N2); 3524 } 3525 3526 // Constant fold FP operations. 3527 bool HasFPExceptions = TLI->hasFloatingPointExceptions(); 3528 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode()); 3529 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode()); 3530 if (N1CFP) { 3531 if (!N2CFP && isCommutativeBinOp(Opcode)) { 3532 // Canonicalize constant to RHS if commutative. 3533 std::swap(N1CFP, N2CFP); 3534 std::swap(N1, N2); 3535 } else if (N2CFP) { 3536 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF(); 3537 APFloat::opStatus s; 3538 switch (Opcode) { 3539 case ISD::FADD: 3540 s = V1.add(V2, APFloat::rmNearestTiesToEven); 3541 if (!HasFPExceptions || s != APFloat::opInvalidOp) 3542 return getConstantFP(V1, DL, VT); 3543 break; 3544 case ISD::FSUB: 3545 s = V1.subtract(V2, APFloat::rmNearestTiesToEven); 3546 if (!HasFPExceptions || s!=APFloat::opInvalidOp) 3547 return getConstantFP(V1, DL, VT); 3548 break; 3549 case ISD::FMUL: 3550 s = V1.multiply(V2, APFloat::rmNearestTiesToEven); 3551 if (!HasFPExceptions || s!=APFloat::opInvalidOp) 3552 return getConstantFP(V1, DL, VT); 3553 break; 3554 case ISD::FDIV: 3555 s = V1.divide(V2, APFloat::rmNearestTiesToEven); 3556 if (!HasFPExceptions || (s!=APFloat::opInvalidOp && 3557 s!=APFloat::opDivByZero)) { 3558 return getConstantFP(V1, DL, VT); 3559 } 3560 break; 3561 case ISD::FREM : 3562 s = V1.mod(V2, APFloat::rmNearestTiesToEven); 3563 if (!HasFPExceptions || (s!=APFloat::opInvalidOp && 3564 s!=APFloat::opDivByZero)) { 3565 return getConstantFP(V1, DL, VT); 3566 } 3567 break; 3568 case ISD::FCOPYSIGN: 3569 V1.copySign(V2); 3570 return getConstantFP(V1, DL, VT); 3571 default: break; 3572 } 3573 } 3574 3575 if (Opcode == ISD::FP_ROUND) { 3576 APFloat V = N1CFP->getValueAPF(); // make copy 3577 bool ignored; 3578 // This can return overflow, underflow, or inexact; we don't care. 3579 // FIXME need to be more flexible about rounding mode. 3580 (void)V.convert(EVTToAPFloatSemantics(VT), 3581 APFloat::rmNearestTiesToEven, &ignored); 3582 return getConstantFP(V, DL, VT); 3583 } 3584 } 3585 3586 // Canonicalize an UNDEF to the RHS, even over a constant. 3587 if (N1.getOpcode() == ISD::UNDEF) { 3588 if (isCommutativeBinOp(Opcode)) { 3589 std::swap(N1, N2); 3590 } else { 3591 switch (Opcode) { 3592 case ISD::FP_ROUND_INREG: 3593 case ISD::SIGN_EXTEND_INREG: 3594 case ISD::SUB: 3595 case ISD::FSUB: 3596 case ISD::FDIV: 3597 case ISD::FREM: 3598 case ISD::SRA: 3599 return N1; // fold op(undef, arg2) -> undef 3600 case ISD::UDIV: 3601 case ISD::SDIV: 3602 case ISD::UREM: 3603 case ISD::SREM: 3604 case ISD::SRL: 3605 case ISD::SHL: 3606 if (!VT.isVector()) 3607 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0 3608 // For vectors, we can't easily build an all zero vector, just return 3609 // the LHS. 3610 return N2; 3611 } 3612 } 3613 } 3614 3615 // Fold a bunch of operators when the RHS is undef. 3616 if (N2.getOpcode() == ISD::UNDEF) { 3617 switch (Opcode) { 3618 case ISD::XOR: 3619 if (N1.getOpcode() == ISD::UNDEF) 3620 // Handle undef ^ undef -> 0 special case. This is a common 3621 // idiom (misuse). 3622 return getConstant(0, DL, VT); 3623 // fallthrough 3624 case ISD::ADD: 3625 case ISD::ADDC: 3626 case ISD::ADDE: 3627 case ISD::SUB: 3628 case ISD::UDIV: 3629 case ISD::SDIV: 3630 case ISD::UREM: 3631 case ISD::SREM: 3632 return N2; // fold op(arg1, undef) -> undef 3633 case ISD::FADD: 3634 case ISD::FSUB: 3635 case ISD::FMUL: 3636 case ISD::FDIV: 3637 case ISD::FREM: 3638 if (getTarget().Options.UnsafeFPMath) 3639 return N2; 3640 break; 3641 case ISD::MUL: 3642 case ISD::AND: 3643 case ISD::SRL: 3644 case ISD::SHL: 3645 if (!VT.isVector()) 3646 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0 3647 // For vectors, we can't easily build an all zero vector, just return 3648 // the LHS. 3649 return N1; 3650 case ISD::OR: 3651 if (!VT.isVector()) 3652 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT); 3653 // For vectors, we can't easily build an all one vector, just return 3654 // the LHS. 3655 return N1; 3656 case ISD::SRA: 3657 return N1; 3658 } 3659 } 3660 3661 // Memoize this node if possible. 3662 BinarySDNode *N; 3663 SDVTList VTs = getVTList(VT); 3664 const bool BinOpHasFlags = isBinOpWithFlags(Opcode); 3665 if (VT != MVT::Glue) { 3666 SDValue Ops[] = {N1, N2}; 3667 FoldingSetNodeID ID; 3668 AddNodeIDNode(ID, Opcode, VTs, Ops); 3669 if (BinOpHasFlags) 3670 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact); 3671 void *IP = nullptr; 3672 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 3673 return SDValue(E, 0); 3674 3675 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact); 3676 3677 CSEMap.InsertNode(N, IP); 3678 } else { 3679 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact); 3680 } 3681 3682 InsertNode(N); 3683 return SDValue(N, 0); 3684 } 3685 3686 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, 3687 SDValue N1, SDValue N2, SDValue N3) { 3688 // Perform various simplifications. 3689 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 3690 switch (Opcode) { 3691 case ISD::FMA: { 3692 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); 3693 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2); 3694 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3); 3695 if (N1CFP && N2CFP && N3CFP) { 3696 APFloat V1 = N1CFP->getValueAPF(); 3697 const APFloat &V2 = N2CFP->getValueAPF(); 3698 const APFloat &V3 = N3CFP->getValueAPF(); 3699 APFloat::opStatus s = 3700 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven); 3701 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp) 3702 return getConstantFP(V1, DL, VT); 3703 } 3704 break; 3705 } 3706 case ISD::CONCAT_VECTORS: 3707 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to 3708 // one big BUILD_VECTOR. 3709 if (N1.getOpcode() == ISD::BUILD_VECTOR && 3710 N2.getOpcode() == ISD::BUILD_VECTOR && 3711 N3.getOpcode() == ISD::BUILD_VECTOR) { 3712 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), 3713 N1.getNode()->op_end()); 3714 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end()); 3715 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end()); 3716 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts); 3717 } 3718 break; 3719 case ISD::SETCC: { 3720 // Use FoldSetCC to simplify SETCC's. 3721 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL); 3722 if (Simp.getNode()) return Simp; 3723 break; 3724 } 3725 case ISD::SELECT: 3726 if (N1C) { 3727 if (N1C->getZExtValue()) 3728 return N2; // select true, X, Y -> X 3729 return N3; // select false, X, Y -> Y 3730 } 3731 3732 if (N2 == N3) return N2; // select C, X, X -> X 3733 break; 3734 case ISD::VECTOR_SHUFFLE: 3735 llvm_unreachable("should use getVectorShuffle constructor!"); 3736 case ISD::INSERT_SUBVECTOR: { 3737 SDValue Index = N3; 3738 if (VT.isSimple() && N1.getValueType().isSimple() 3739 && N2.getValueType().isSimple()) { 3740 assert(VT.isVector() && N1.getValueType().isVector() && 3741 N2.getValueType().isVector() && 3742 "Insert subvector VTs must be a vectors"); 3743 assert(VT == N1.getValueType() && 3744 "Dest and insert subvector source types must match!"); 3745 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() && 3746 "Insert subvector must be from smaller vector to larger vector!"); 3747 if (isa<ConstantSDNode>(Index.getNode())) { 3748 assert((N2.getValueType().getVectorNumElements() + 3749 cast<ConstantSDNode>(Index.getNode())->getZExtValue() 3750 <= VT.getVectorNumElements()) 3751 && "Insert subvector overflow!"); 3752 } 3753 3754 // Trivial insertion. 3755 if (VT.getSimpleVT() == N2.getSimpleValueType()) 3756 return N2; 3757 } 3758 break; 3759 } 3760 case ISD::BITCAST: 3761 // Fold bit_convert nodes from a type to themselves. 3762 if (N1.getValueType() == VT) 3763 return N1; 3764 break; 3765 } 3766 3767 // Memoize node if it doesn't produce a flag. 3768 SDNode *N; 3769 SDVTList VTs = getVTList(VT); 3770 if (VT != MVT::Glue) { 3771 SDValue Ops[] = { N1, N2, N3 }; 3772 FoldingSetNodeID ID; 3773 AddNodeIDNode(ID, Opcode, VTs, Ops); 3774 void *IP = nullptr; 3775 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 3776 return SDValue(E, 0); 3777 3778 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), 3779 DL.getDebugLoc(), VTs, N1, N2, N3); 3780 CSEMap.InsertNode(N, IP); 3781 } else { 3782 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), 3783 DL.getDebugLoc(), VTs, N1, N2, N3); 3784 } 3785 3786 InsertNode(N); 3787 return SDValue(N, 0); 3788 } 3789 3790 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, 3791 SDValue N1, SDValue N2, SDValue N3, 3792 SDValue N4) { 3793 SDValue Ops[] = { N1, N2, N3, N4 }; 3794 return getNode(Opcode, DL, VT, Ops); 3795 } 3796 3797 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, 3798 SDValue N1, SDValue N2, SDValue N3, 3799 SDValue N4, SDValue N5) { 3800 SDValue Ops[] = { N1, N2, N3, N4, N5 }; 3801 return getNode(Opcode, DL, VT, Ops); 3802 } 3803 3804 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all 3805 /// the incoming stack arguments to be loaded from the stack. 3806 SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) { 3807 SmallVector<SDValue, 8> ArgChains; 3808 3809 // Include the original chain at the beginning of the list. When this is 3810 // used by target LowerCall hooks, this helps legalize find the 3811 // CALLSEQ_BEGIN node. 3812 ArgChains.push_back(Chain); 3813 3814 // Add a chain value for each stack argument. 3815 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(), 3816 UE = getEntryNode().getNode()->use_end(); U != UE; ++U) 3817 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) 3818 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) 3819 if (FI->getIndex() < 0) 3820 ArgChains.push_back(SDValue(L, 1)); 3821 3822 // Build a tokenfactor for all the chains. 3823 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains); 3824 } 3825 3826 /// getMemsetValue - Vectorized representation of the memset value 3827 /// operand. 3828 static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG, 3829 SDLoc dl) { 3830 assert(Value.getOpcode() != ISD::UNDEF); 3831 3832 unsigned NumBits = VT.getScalarType().getSizeInBits(); 3833 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { 3834 assert(C->getAPIntValue().getBitWidth() == 8); 3835 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue()); 3836 if (VT.isInteger()) 3837 return DAG.getConstant(Val, dl, VT); 3838 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl, 3839 VT); 3840 } 3841 3842 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?"); 3843 EVT IntVT = VT.getScalarType(); 3844 if (!IntVT.isInteger()) 3845 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits()); 3846 3847 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value); 3848 if (NumBits > 8) { 3849 // Use a multiplication with 0x010101... to extend the input to the 3850 // required length. 3851 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01)); 3852 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value, 3853 DAG.getConstant(Magic, dl, IntVT)); 3854 } 3855 3856 if (VT != Value.getValueType() && !VT.isInteger()) 3857 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value); 3858 if (VT != Value.getValueType()) { 3859 assert(VT.getVectorElementType() == Value.getValueType() && 3860 "value type should be one vector element here"); 3861 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value); 3862 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps); 3863 } 3864 3865 return Value; 3866 } 3867 3868 /// getMemsetStringVal - Similar to getMemsetValue. Except this is only 3869 /// used when a memcpy is turned into a memset when the source is a constant 3870 /// string ptr. 3871 static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG, 3872 const TargetLowering &TLI, StringRef Str) { 3873 // Handle vector with all elements zero. 3874 if (Str.empty()) { 3875 if (VT.isInteger()) 3876 return DAG.getConstant(0, dl, VT); 3877 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128) 3878 return DAG.getConstantFP(0.0, dl, VT); 3879 else if (VT.isVector()) { 3880 unsigned NumElts = VT.getVectorNumElements(); 3881 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64; 3882 return DAG.getNode(ISD::BITCAST, dl, VT, 3883 DAG.getConstant(0, dl, 3884 EVT::getVectorVT(*DAG.getContext(), 3885 EltVT, NumElts))); 3886 } else 3887 llvm_unreachable("Expected type!"); 3888 } 3889 3890 assert(!VT.isVector() && "Can't handle vector type here!"); 3891 unsigned NumVTBits = VT.getSizeInBits(); 3892 unsigned NumVTBytes = NumVTBits / 8; 3893 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size())); 3894 3895 APInt Val(NumVTBits, 0); 3896 if (TLI.isLittleEndian()) { 3897 for (unsigned i = 0; i != NumBytes; ++i) 3898 Val |= (uint64_t)(unsigned char)Str[i] << i*8; 3899 } else { 3900 for (unsigned i = 0; i != NumBytes; ++i) 3901 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8; 3902 } 3903 3904 // If the "cost" of materializing the integer immediate is less than the cost 3905 // of a load, then it is cost effective to turn the load into the immediate. 3906 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 3907 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty)) 3908 return DAG.getConstant(Val, dl, VT); 3909 return SDValue(nullptr, 0); 3910 } 3911 3912 /// getMemBasePlusOffset - Returns base and offset node for the 3913 /// 3914 static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl, 3915 SelectionDAG &DAG) { 3916 EVT VT = Base.getValueType(); 3917 return DAG.getNode(ISD::ADD, dl, 3918 VT, Base, DAG.getConstant(Offset, dl, VT)); 3919 } 3920 3921 /// isMemSrcFromString - Returns true if memcpy source is a string constant. 3922 /// 3923 static bool isMemSrcFromString(SDValue Src, StringRef &Str) { 3924 unsigned SrcDelta = 0; 3925 GlobalAddressSDNode *G = nullptr; 3926 if (Src.getOpcode() == ISD::GlobalAddress) 3927 G = cast<GlobalAddressSDNode>(Src); 3928 else if (Src.getOpcode() == ISD::ADD && 3929 Src.getOperand(0).getOpcode() == ISD::GlobalAddress && 3930 Src.getOperand(1).getOpcode() == ISD::Constant) { 3931 G = cast<GlobalAddressSDNode>(Src.getOperand(0)); 3932 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue(); 3933 } 3934 if (!G) 3935 return false; 3936 3937 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false); 3938 } 3939 3940 /// FindOptimalMemOpLowering - Determines the optimial series memory ops 3941 /// to replace the memset / memcpy. Return true if the number of memory ops 3942 /// is below the threshold. It returns the types of the sequence of 3943 /// memory ops to perform memset / memcpy by reference. 3944 static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps, 3945 unsigned Limit, uint64_t Size, 3946 unsigned DstAlign, unsigned SrcAlign, 3947 bool IsMemset, 3948 bool ZeroMemset, 3949 bool MemcpyStrSrc, 3950 bool AllowOverlap, 3951 SelectionDAG &DAG, 3952 const TargetLowering &TLI) { 3953 assert((SrcAlign == 0 || SrcAlign >= DstAlign) && 3954 "Expecting memcpy / memset source to meet alignment requirement!"); 3955 // If 'SrcAlign' is zero, that means the memory operation does not need to 3956 // load the value, i.e. memset or memcpy from constant string. Otherwise, 3957 // it's the inferred alignment of the source. 'DstAlign', on the other hand, 3958 // is the specified alignment of the memory operation. If it is zero, that 3959 // means it's possible to change the alignment of the destination. 3960 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does 3961 // not need to be loaded. 3962 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign, 3963 IsMemset, ZeroMemset, MemcpyStrSrc, 3964 DAG.getMachineFunction()); 3965 3966 if (VT == MVT::Other) { 3967 unsigned AS = 0; 3968 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) || 3969 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) { 3970 VT = TLI.getPointerTy(); 3971 } else { 3972 switch (DstAlign & 7) { 3973 case 0: VT = MVT::i64; break; 3974 case 4: VT = MVT::i32; break; 3975 case 2: VT = MVT::i16; break; 3976 default: VT = MVT::i8; break; 3977 } 3978 } 3979 3980 MVT LVT = MVT::i64; 3981 while (!TLI.isTypeLegal(LVT)) 3982 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1); 3983 assert(LVT.isInteger()); 3984 3985 if (VT.bitsGT(LVT)) 3986 VT = LVT; 3987 } 3988 3989 unsigned NumMemOps = 0; 3990 while (Size != 0) { 3991 unsigned VTSize = VT.getSizeInBits() / 8; 3992 while (VTSize > Size) { 3993 // For now, only use non-vector load / store's for the left-over pieces. 3994 EVT NewVT = VT; 3995 unsigned NewVTSize; 3996 3997 bool Found = false; 3998 if (VT.isVector() || VT.isFloatingPoint()) { 3999 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32; 4000 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) && 4001 TLI.isSafeMemOpType(NewVT.getSimpleVT())) 4002 Found = true; 4003 else if (NewVT == MVT::i64 && 4004 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) && 4005 TLI.isSafeMemOpType(MVT::f64)) { 4006 // i64 is usually not legal on 32-bit targets, but f64 may be. 4007 NewVT = MVT::f64; 4008 Found = true; 4009 } 4010 } 4011 4012 if (!Found) { 4013 do { 4014 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1); 4015 if (NewVT == MVT::i8) 4016 break; 4017 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT())); 4018 } 4019 NewVTSize = NewVT.getSizeInBits() / 8; 4020 4021 // If the new VT cannot cover all of the remaining bits, then consider 4022 // issuing a (or a pair of) unaligned and overlapping load / store. 4023 // FIXME: Only does this for 64-bit or more since we don't have proper 4024 // cost model for unaligned load / store. 4025 bool Fast; 4026 unsigned AS = 0; 4027 if (NumMemOps && AllowOverlap && 4028 VTSize >= 8 && NewVTSize < Size && 4029 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast) 4030 VTSize = Size; 4031 else { 4032 VT = NewVT; 4033 VTSize = NewVTSize; 4034 } 4035 } 4036 4037 if (++NumMemOps > Limit) 4038 return false; 4039 4040 MemOps.push_back(VT); 4041 Size -= VTSize; 4042 } 4043 4044 return true; 4045 } 4046 4047 static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl, 4048 SDValue Chain, SDValue Dst, 4049 SDValue Src, uint64_t Size, 4050 unsigned Align, bool isVol, 4051 bool AlwaysInline, 4052 MachinePointerInfo DstPtrInfo, 4053 MachinePointerInfo SrcPtrInfo) { 4054 // Turn a memcpy of undef to nop. 4055 if (Src.getOpcode() == ISD::UNDEF) 4056 return Chain; 4057 4058 // Expand memcpy to a series of load and store ops if the size operand falls 4059 // below a certain threshold. 4060 // TODO: In the AlwaysInline case, if the size is big then generate a loop 4061 // rather than maybe a humongous number of loads and stores. 4062 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4063 std::vector<EVT> MemOps; 4064 bool DstAlignCanChange = false; 4065 MachineFunction &MF = DAG.getMachineFunction(); 4066 MachineFrameInfo *MFI = MF.getFrameInfo(); 4067 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize); 4068 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); 4069 if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) 4070 DstAlignCanChange = true; 4071 unsigned SrcAlign = DAG.InferPtrAlignment(Src); 4072 if (Align > SrcAlign) 4073 SrcAlign = Align; 4074 StringRef Str; 4075 bool CopyFromStr = isMemSrcFromString(Src, Str); 4076 bool isZeroStr = CopyFromStr && Str.empty(); 4077 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize); 4078 4079 if (!FindOptimalMemOpLowering(MemOps, Limit, Size, 4080 (DstAlignCanChange ? 0 : Align), 4081 (isZeroStr ? 0 : SrcAlign), 4082 false, false, CopyFromStr, true, DAG, TLI)) 4083 return SDValue(); 4084 4085 if (DstAlignCanChange) { 4086 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); 4087 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty); 4088 4089 // Don't promote to an alignment that would require dynamic stack 4090 // realignment. 4091 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 4092 if (!TRI->needsStackRealignment(MF)) 4093 while (NewAlign > Align && 4094 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign)) 4095 NewAlign /= 2; 4096 4097 if (NewAlign > Align) { 4098 // Give the stack frame object a larger alignment if needed. 4099 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) 4100 MFI->setObjectAlignment(FI->getIndex(), NewAlign); 4101 Align = NewAlign; 4102 } 4103 } 4104 4105 SmallVector<SDValue, 8> OutChains; 4106 unsigned NumMemOps = MemOps.size(); 4107 uint64_t SrcOff = 0, DstOff = 0; 4108 for (unsigned i = 0; i != NumMemOps; ++i) { 4109 EVT VT = MemOps[i]; 4110 unsigned VTSize = VT.getSizeInBits() / 8; 4111 SDValue Value, Store; 4112 4113 if (VTSize > Size) { 4114 // Issuing an unaligned load / store pair that overlaps with the previous 4115 // pair. Adjust the offset accordingly. 4116 assert(i == NumMemOps-1 && i != 0); 4117 SrcOff -= VTSize - Size; 4118 DstOff -= VTSize - Size; 4119 } 4120 4121 if (CopyFromStr && 4122 (isZeroStr || (VT.isInteger() && !VT.isVector()))) { 4123 // It's unlikely a store of a vector immediate can be done in a single 4124 // instruction. It would require a load from a constantpool first. 4125 // We only handle zero vectors here. 4126 // FIXME: Handle other cases where store of vector immediate is done in 4127 // a single instruction. 4128 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff)); 4129 if (Value.getNode()) 4130 Store = DAG.getStore(Chain, dl, Value, 4131 getMemBasePlusOffset(Dst, DstOff, dl, DAG), 4132 DstPtrInfo.getWithOffset(DstOff), isVol, 4133 false, Align); 4134 } 4135 4136 if (!Store.getNode()) { 4137 // The type might not be legal for the target. This should only happen 4138 // if the type is smaller than a legal type, as on PPC, so the right 4139 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify 4140 // to Load/Store if NVT==VT. 4141 // FIXME does the case above also need this? 4142 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 4143 assert(NVT.bitsGE(VT)); 4144 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain, 4145 getMemBasePlusOffset(Src, SrcOff, dl, DAG), 4146 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false, 4147 false, MinAlign(SrcAlign, SrcOff)); 4148 Store = DAG.getTruncStore(Chain, dl, Value, 4149 getMemBasePlusOffset(Dst, DstOff, dl, DAG), 4150 DstPtrInfo.getWithOffset(DstOff), VT, isVol, 4151 false, Align); 4152 } 4153 OutChains.push_back(Store); 4154 SrcOff += VTSize; 4155 DstOff += VTSize; 4156 Size -= VTSize; 4157 } 4158 4159 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains); 4160 } 4161 4162 static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl, 4163 SDValue Chain, SDValue Dst, 4164 SDValue Src, uint64_t Size, 4165 unsigned Align, bool isVol, 4166 bool AlwaysInline, 4167 MachinePointerInfo DstPtrInfo, 4168 MachinePointerInfo SrcPtrInfo) { 4169 // Turn a memmove of undef to nop. 4170 if (Src.getOpcode() == ISD::UNDEF) 4171 return Chain; 4172 4173 // Expand memmove to a series of load and store ops if the size operand falls 4174 // below a certain threshold. 4175 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4176 std::vector<EVT> MemOps; 4177 bool DstAlignCanChange = false; 4178 MachineFunction &MF = DAG.getMachineFunction(); 4179 MachineFrameInfo *MFI = MF.getFrameInfo(); 4180 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize); 4181 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); 4182 if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) 4183 DstAlignCanChange = true; 4184 unsigned SrcAlign = DAG.InferPtrAlignment(Src); 4185 if (Align > SrcAlign) 4186 SrcAlign = Align; 4187 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize); 4188 4189 if (!FindOptimalMemOpLowering(MemOps, Limit, Size, 4190 (DstAlignCanChange ? 0 : Align), SrcAlign, 4191 false, false, false, false, DAG, TLI)) 4192 return SDValue(); 4193 4194 if (DstAlignCanChange) { 4195 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); 4196 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty); 4197 if (NewAlign > Align) { 4198 // Give the stack frame object a larger alignment if needed. 4199 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) 4200 MFI->setObjectAlignment(FI->getIndex(), NewAlign); 4201 Align = NewAlign; 4202 } 4203 } 4204 4205 uint64_t SrcOff = 0, DstOff = 0; 4206 SmallVector<SDValue, 8> LoadValues; 4207 SmallVector<SDValue, 8> LoadChains; 4208 SmallVector<SDValue, 8> OutChains; 4209 unsigned NumMemOps = MemOps.size(); 4210 for (unsigned i = 0; i < NumMemOps; i++) { 4211 EVT VT = MemOps[i]; 4212 unsigned VTSize = VT.getSizeInBits() / 8; 4213 SDValue Value; 4214 4215 Value = DAG.getLoad(VT, dl, Chain, 4216 getMemBasePlusOffset(Src, SrcOff, dl, DAG), 4217 SrcPtrInfo.getWithOffset(SrcOff), isVol, 4218 false, false, SrcAlign); 4219 LoadValues.push_back(Value); 4220 LoadChains.push_back(Value.getValue(1)); 4221 SrcOff += VTSize; 4222 } 4223 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 4224 OutChains.clear(); 4225 for (unsigned i = 0; i < NumMemOps; i++) { 4226 EVT VT = MemOps[i]; 4227 unsigned VTSize = VT.getSizeInBits() / 8; 4228 SDValue Store; 4229 4230 Store = DAG.getStore(Chain, dl, LoadValues[i], 4231 getMemBasePlusOffset(Dst, DstOff, dl, DAG), 4232 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align); 4233 OutChains.push_back(Store); 4234 DstOff += VTSize; 4235 } 4236 4237 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains); 4238 } 4239 4240 /// \brief Lower the call to 'memset' intrinsic function into a series of store 4241 /// operations. 4242 /// 4243 /// \param DAG Selection DAG where lowered code is placed. 4244 /// \param dl Link to corresponding IR location. 4245 /// \param Chain Control flow dependency. 4246 /// \param Dst Pointer to destination memory location. 4247 /// \param Src Value of byte to write into the memory. 4248 /// \param Size Number of bytes to write. 4249 /// \param Align Alignment of the destination in bytes. 4250 /// \param isVol True if destination is volatile. 4251 /// \param DstPtrInfo IR information on the memory pointer. 4252 /// \returns New head in the control flow, if lowering was successful, empty 4253 /// SDValue otherwise. 4254 /// 4255 /// The function tries to replace 'llvm.memset' intrinsic with several store 4256 /// operations and value calculation code. This is usually profitable for small 4257 /// memory size. 4258 static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl, 4259 SDValue Chain, SDValue Dst, 4260 SDValue Src, uint64_t Size, 4261 unsigned Align, bool isVol, 4262 MachinePointerInfo DstPtrInfo) { 4263 // Turn a memset of undef to nop. 4264 if (Src.getOpcode() == ISD::UNDEF) 4265 return Chain; 4266 4267 // Expand memset to a series of load/store ops if the size operand 4268 // falls below a certain threshold. 4269 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4270 std::vector<EVT> MemOps; 4271 bool DstAlignCanChange = false; 4272 MachineFunction &MF = DAG.getMachineFunction(); 4273 MachineFrameInfo *MFI = MF.getFrameInfo(); 4274 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize); 4275 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); 4276 if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) 4277 DstAlignCanChange = true; 4278 bool IsZeroVal = 4279 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue(); 4280 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize), 4281 Size, (DstAlignCanChange ? 0 : Align), 0, 4282 true, IsZeroVal, false, true, DAG, TLI)) 4283 return SDValue(); 4284 4285 if (DstAlignCanChange) { 4286 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); 4287 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty); 4288 if (NewAlign > Align) { 4289 // Give the stack frame object a larger alignment if needed. 4290 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) 4291 MFI->setObjectAlignment(FI->getIndex(), NewAlign); 4292 Align = NewAlign; 4293 } 4294 } 4295 4296 SmallVector<SDValue, 8> OutChains; 4297 uint64_t DstOff = 0; 4298 unsigned NumMemOps = MemOps.size(); 4299 4300 // Find the largest store and generate the bit pattern for it. 4301 EVT LargestVT = MemOps[0]; 4302 for (unsigned i = 1; i < NumMemOps; i++) 4303 if (MemOps[i].bitsGT(LargestVT)) 4304 LargestVT = MemOps[i]; 4305 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl); 4306 4307 for (unsigned i = 0; i < NumMemOps; i++) { 4308 EVT VT = MemOps[i]; 4309 unsigned VTSize = VT.getSizeInBits() / 8; 4310 if (VTSize > Size) { 4311 // Issuing an unaligned load / store pair that overlaps with the previous 4312 // pair. Adjust the offset accordingly. 4313 assert(i == NumMemOps-1 && i != 0); 4314 DstOff -= VTSize - Size; 4315 } 4316 4317 // If this store is smaller than the largest store see whether we can get 4318 // the smaller value for free with a truncate. 4319 SDValue Value = MemSetValue; 4320 if (VT.bitsLT(LargestVT)) { 4321 if (!LargestVT.isVector() && !VT.isVector() && 4322 TLI.isTruncateFree(LargestVT, VT)) 4323 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue); 4324 else 4325 Value = getMemsetValue(Src, VT, DAG, dl); 4326 } 4327 assert(Value.getValueType() == VT && "Value with wrong type."); 4328 SDValue Store = DAG.getStore(Chain, dl, Value, 4329 getMemBasePlusOffset(Dst, DstOff, dl, DAG), 4330 DstPtrInfo.getWithOffset(DstOff), 4331 isVol, false, Align); 4332 OutChains.push_back(Store); 4333 DstOff += VT.getSizeInBits() / 8; 4334 Size -= VTSize; 4335 } 4336 4337 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains); 4338 } 4339 4340 SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst, 4341 SDValue Src, SDValue Size, 4342 unsigned Align, bool isVol, bool AlwaysInline, 4343 bool isTailCall, MachinePointerInfo DstPtrInfo, 4344 MachinePointerInfo SrcPtrInfo) { 4345 assert(Align && "The SDAG layer expects explicit alignment and reserves 0"); 4346 4347 // Check to see if we should lower the memcpy to loads and stores first. 4348 // For cases within the target-specified limits, this is the best choice. 4349 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 4350 if (ConstantSize) { 4351 // Memcpy with size zero? Just return the original chain. 4352 if (ConstantSize->isNullValue()) 4353 return Chain; 4354 4355 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, 4356 ConstantSize->getZExtValue(),Align, 4357 isVol, false, DstPtrInfo, SrcPtrInfo); 4358 if (Result.getNode()) 4359 return Result; 4360 } 4361 4362 // Then check to see if we should lower the memcpy with target-specific 4363 // code. If the target chooses to do this, this is the next best. 4364 if (TSI) { 4365 SDValue Result = TSI->EmitTargetCodeForMemcpy( 4366 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline, 4367 DstPtrInfo, SrcPtrInfo); 4368 if (Result.getNode()) 4369 return Result; 4370 } 4371 4372 // If we really need inline code and the target declined to provide it, 4373 // use a (potentially long) sequence of loads and stores. 4374 if (AlwaysInline) { 4375 assert(ConstantSize && "AlwaysInline requires a constant size!"); 4376 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, 4377 ConstantSize->getZExtValue(), Align, isVol, 4378 true, DstPtrInfo, SrcPtrInfo); 4379 } 4380 4381 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc 4382 // memcpy is not guaranteed to be safe. libc memcpys aren't required to 4383 // respect volatile, so they may do things like read or write memory 4384 // beyond the given memory regions. But fixing this isn't easy, and most 4385 // people don't care. 4386 4387 // Emit a library call. 4388 TargetLowering::ArgListTy Args; 4389 TargetLowering::ArgListEntry Entry; 4390 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext()); 4391 Entry.Node = Dst; Args.push_back(Entry); 4392 Entry.Node = Src; Args.push_back(Entry); 4393 Entry.Node = Size; Args.push_back(Entry); 4394 // FIXME: pass in SDLoc 4395 TargetLowering::CallLoweringInfo CLI(*this); 4396 CLI.setDebugLoc(dl).setChain(Chain) 4397 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY), 4398 Type::getVoidTy(*getContext()), 4399 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY), 4400 TLI->getPointerTy()), std::move(Args), 0) 4401 .setDiscardResult() 4402 .setTailCall(isTailCall); 4403 4404 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI); 4405 return CallResult.second; 4406 } 4407 4408 SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst, 4409 SDValue Src, SDValue Size, 4410 unsigned Align, bool isVol, bool isTailCall, 4411 MachinePointerInfo DstPtrInfo, 4412 MachinePointerInfo SrcPtrInfo) { 4413 assert(Align && "The SDAG layer expects explicit alignment and reserves 0"); 4414 4415 // Check to see if we should lower the memmove to loads and stores first. 4416 // For cases within the target-specified limits, this is the best choice. 4417 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 4418 if (ConstantSize) { 4419 // Memmove with size zero? Just return the original chain. 4420 if (ConstantSize->isNullValue()) 4421 return Chain; 4422 4423 SDValue Result = 4424 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src, 4425 ConstantSize->getZExtValue(), Align, isVol, 4426 false, DstPtrInfo, SrcPtrInfo); 4427 if (Result.getNode()) 4428 return Result; 4429 } 4430 4431 // Then check to see if we should lower the memmove with target-specific 4432 // code. If the target chooses to do this, this is the next best. 4433 if (TSI) { 4434 SDValue Result = TSI->EmitTargetCodeForMemmove( 4435 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo); 4436 if (Result.getNode()) 4437 return Result; 4438 } 4439 4440 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may 4441 // not be safe. See memcpy above for more details. 4442 4443 // Emit a library call. 4444 TargetLowering::ArgListTy Args; 4445 TargetLowering::ArgListEntry Entry; 4446 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext()); 4447 Entry.Node = Dst; Args.push_back(Entry); 4448 Entry.Node = Src; Args.push_back(Entry); 4449 Entry.Node = Size; Args.push_back(Entry); 4450 // FIXME: pass in SDLoc 4451 TargetLowering::CallLoweringInfo CLI(*this); 4452 CLI.setDebugLoc(dl).setChain(Chain) 4453 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE), 4454 Type::getVoidTy(*getContext()), 4455 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE), 4456 TLI->getPointerTy()), std::move(Args), 0) 4457 .setDiscardResult() 4458 .setTailCall(isTailCall); 4459 4460 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI); 4461 return CallResult.second; 4462 } 4463 4464 SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst, 4465 SDValue Src, SDValue Size, 4466 unsigned Align, bool isVol, bool isTailCall, 4467 MachinePointerInfo DstPtrInfo) { 4468 assert(Align && "The SDAG layer expects explicit alignment and reserves 0"); 4469 4470 // Check to see if we should lower the memset to stores first. 4471 // For cases within the target-specified limits, this is the best choice. 4472 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 4473 if (ConstantSize) { 4474 // Memset with size zero? Just return the original chain. 4475 if (ConstantSize->isNullValue()) 4476 return Chain; 4477 4478 SDValue Result = 4479 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), 4480 Align, isVol, DstPtrInfo); 4481 4482 if (Result.getNode()) 4483 return Result; 4484 } 4485 4486 // Then check to see if we should lower the memset with target-specific 4487 // code. If the target chooses to do this, this is the next best. 4488 if (TSI) { 4489 SDValue Result = TSI->EmitTargetCodeForMemset( 4490 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo); 4491 if (Result.getNode()) 4492 return Result; 4493 } 4494 4495 // Emit a library call. 4496 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext()); 4497 TargetLowering::ArgListTy Args; 4498 TargetLowering::ArgListEntry Entry; 4499 Entry.Node = Dst; Entry.Ty = IntPtrTy; 4500 Args.push_back(Entry); 4501 Entry.Node = Src; 4502 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext()); 4503 Args.push_back(Entry); 4504 Entry.Node = Size; 4505 Entry.Ty = IntPtrTy; 4506 Args.push_back(Entry); 4507 4508 // FIXME: pass in SDLoc 4509 TargetLowering::CallLoweringInfo CLI(*this); 4510 CLI.setDebugLoc(dl).setChain(Chain) 4511 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET), 4512 Type::getVoidTy(*getContext()), 4513 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET), 4514 TLI->getPointerTy()), std::move(Args), 0) 4515 .setDiscardResult() 4516 .setTailCall(isTailCall); 4517 4518 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI); 4519 return CallResult.second; 4520 } 4521 4522 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, 4523 SDVTList VTList, ArrayRef<SDValue> Ops, 4524 MachineMemOperand *MMO, 4525 AtomicOrdering SuccessOrdering, 4526 AtomicOrdering FailureOrdering, 4527 SynchronizationScope SynchScope) { 4528 FoldingSetNodeID ID; 4529 ID.AddInteger(MemVT.getRawBits()); 4530 AddNodeIDNode(ID, Opcode, VTList, Ops); 4531 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 4532 void* IP = nullptr; 4533 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4534 cast<AtomicSDNode>(E)->refineAlignment(MMO); 4535 return SDValue(E, 0); 4536 } 4537 4538 // Allocate the operands array for the node out of the BumpPtrAllocator, since 4539 // SDNode doesn't have access to it. This memory will be "leaked" when 4540 // the node is deallocated, but recovered when the allocator is released. 4541 // If the number of operands is less than 5 we use AtomicSDNode's internal 4542 // storage. 4543 unsigned NumOps = Ops.size(); 4544 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) 4545 : nullptr; 4546 4547 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(), 4548 dl.getDebugLoc(), VTList, MemVT, 4549 Ops.data(), DynOps, NumOps, MMO, 4550 SuccessOrdering, FailureOrdering, 4551 SynchScope); 4552 CSEMap.InsertNode(N, IP); 4553 InsertNode(N); 4554 return SDValue(N, 0); 4555 } 4556 4557 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, 4558 SDVTList VTList, ArrayRef<SDValue> Ops, 4559 MachineMemOperand *MMO, 4560 AtomicOrdering Ordering, 4561 SynchronizationScope SynchScope) { 4562 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering, 4563 Ordering, SynchScope); 4564 } 4565 4566 SDValue SelectionDAG::getAtomicCmpSwap( 4567 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain, 4568 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo, 4569 unsigned Alignment, AtomicOrdering SuccessOrdering, 4570 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) { 4571 assert(Opcode == ISD::ATOMIC_CMP_SWAP || 4572 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 4573 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types"); 4574 4575 if (Alignment == 0) // Ensure that codegen never sees alignment 0 4576 Alignment = getEVTAlignment(MemVT); 4577 4578 MachineFunction &MF = getMachineFunction(); 4579 4580 // FIXME: Volatile isn't really correct; we should keep track of atomic 4581 // orderings in the memoperand. 4582 unsigned Flags = MachineMemOperand::MOVolatile; 4583 Flags |= MachineMemOperand::MOLoad; 4584 Flags |= MachineMemOperand::MOStore; 4585 4586 MachineMemOperand *MMO = 4587 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment); 4588 4589 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO, 4590 SuccessOrdering, FailureOrdering, SynchScope); 4591 } 4592 4593 SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT, 4594 SDVTList VTs, SDValue Chain, SDValue Ptr, 4595 SDValue Cmp, SDValue Swp, 4596 MachineMemOperand *MMO, 4597 AtomicOrdering SuccessOrdering, 4598 AtomicOrdering FailureOrdering, 4599 SynchronizationScope SynchScope) { 4600 assert(Opcode == ISD::ATOMIC_CMP_SWAP || 4601 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 4602 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types"); 4603 4604 SDValue Ops[] = {Chain, Ptr, Cmp, Swp}; 4605 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, 4606 SuccessOrdering, FailureOrdering, SynchScope); 4607 } 4608 4609 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, 4610 SDValue Chain, 4611 SDValue Ptr, SDValue Val, 4612 const Value* PtrVal, 4613 unsigned Alignment, 4614 AtomicOrdering Ordering, 4615 SynchronizationScope SynchScope) { 4616 if (Alignment == 0) // Ensure that codegen never sees alignment 0 4617 Alignment = getEVTAlignment(MemVT); 4618 4619 MachineFunction &MF = getMachineFunction(); 4620 // An atomic store does not load. An atomic load does not store. 4621 // (An atomicrmw obviously both loads and stores.) 4622 // For now, atomics are considered to be volatile always, and they are 4623 // chained as such. 4624 // FIXME: Volatile isn't really correct; we should keep track of atomic 4625 // orderings in the memoperand. 4626 unsigned Flags = MachineMemOperand::MOVolatile; 4627 if (Opcode != ISD::ATOMIC_STORE) 4628 Flags |= MachineMemOperand::MOLoad; 4629 if (Opcode != ISD::ATOMIC_LOAD) 4630 Flags |= MachineMemOperand::MOStore; 4631 4632 MachineMemOperand *MMO = 4633 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags, 4634 MemVT.getStoreSize(), Alignment); 4635 4636 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO, 4637 Ordering, SynchScope); 4638 } 4639 4640 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, 4641 SDValue Chain, 4642 SDValue Ptr, SDValue Val, 4643 MachineMemOperand *MMO, 4644 AtomicOrdering Ordering, 4645 SynchronizationScope SynchScope) { 4646 assert((Opcode == ISD::ATOMIC_LOAD_ADD || 4647 Opcode == ISD::ATOMIC_LOAD_SUB || 4648 Opcode == ISD::ATOMIC_LOAD_AND || 4649 Opcode == ISD::ATOMIC_LOAD_OR || 4650 Opcode == ISD::ATOMIC_LOAD_XOR || 4651 Opcode == ISD::ATOMIC_LOAD_NAND || 4652 Opcode == ISD::ATOMIC_LOAD_MIN || 4653 Opcode == ISD::ATOMIC_LOAD_MAX || 4654 Opcode == ISD::ATOMIC_LOAD_UMIN || 4655 Opcode == ISD::ATOMIC_LOAD_UMAX || 4656 Opcode == ISD::ATOMIC_SWAP || 4657 Opcode == ISD::ATOMIC_STORE) && 4658 "Invalid Atomic Op"); 4659 4660 EVT VT = Val.getValueType(); 4661 4662 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) : 4663 getVTList(VT, MVT::Other); 4664 SDValue Ops[] = {Chain, Ptr, Val}; 4665 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope); 4666 } 4667 4668 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, 4669 EVT VT, SDValue Chain, 4670 SDValue Ptr, 4671 MachineMemOperand *MMO, 4672 AtomicOrdering Ordering, 4673 SynchronizationScope SynchScope) { 4674 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op"); 4675 4676 SDVTList VTs = getVTList(VT, MVT::Other); 4677 SDValue Ops[] = {Chain, Ptr}; 4678 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope); 4679 } 4680 4681 /// getMergeValues - Create a MERGE_VALUES node from the given operands. 4682 SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) { 4683 if (Ops.size() == 1) 4684 return Ops[0]; 4685 4686 SmallVector<EVT, 4> VTs; 4687 VTs.reserve(Ops.size()); 4688 for (unsigned i = 0; i < Ops.size(); ++i) 4689 VTs.push_back(Ops[i].getValueType()); 4690 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops); 4691 } 4692 4693 SDValue 4694 SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList, 4695 ArrayRef<SDValue> Ops, 4696 EVT MemVT, MachinePointerInfo PtrInfo, 4697 unsigned Align, bool Vol, 4698 bool ReadMem, bool WriteMem, unsigned Size) { 4699 if (Align == 0) // Ensure that codegen never sees alignment 0 4700 Align = getEVTAlignment(MemVT); 4701 4702 MachineFunction &MF = getMachineFunction(); 4703 unsigned Flags = 0; 4704 if (WriteMem) 4705 Flags |= MachineMemOperand::MOStore; 4706 if (ReadMem) 4707 Flags |= MachineMemOperand::MOLoad; 4708 if (Vol) 4709 Flags |= MachineMemOperand::MOVolatile; 4710 if (!Size) 4711 Size = MemVT.getStoreSize(); 4712 MachineMemOperand *MMO = 4713 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align); 4714 4715 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO); 4716 } 4717 4718 SDValue 4719 SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList, 4720 ArrayRef<SDValue> Ops, EVT MemVT, 4721 MachineMemOperand *MMO) { 4722 assert((Opcode == ISD::INTRINSIC_VOID || 4723 Opcode == ISD::INTRINSIC_W_CHAIN || 4724 Opcode == ISD::PREFETCH || 4725 Opcode == ISD::LIFETIME_START || 4726 Opcode == ISD::LIFETIME_END || 4727 (Opcode <= INT_MAX && 4728 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) && 4729 "Opcode is not a memory-accessing opcode!"); 4730 4731 // Memoize the node unless it returns a flag. 4732 MemIntrinsicSDNode *N; 4733 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { 4734 FoldingSetNodeID ID; 4735 AddNodeIDNode(ID, Opcode, VTList, Ops); 4736 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 4737 void *IP = nullptr; 4738 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4739 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO); 4740 return SDValue(E, 0); 4741 } 4742 4743 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(), 4744 dl.getDebugLoc(), VTList, Ops, 4745 MemVT, MMO); 4746 CSEMap.InsertNode(N, IP); 4747 } else { 4748 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(), 4749 dl.getDebugLoc(), VTList, Ops, 4750 MemVT, MMO); 4751 } 4752 InsertNode(N); 4753 return SDValue(N, 0); 4754 } 4755 4756 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a 4757 /// MachinePointerInfo record from it. This is particularly useful because the 4758 /// code generator has many cases where it doesn't bother passing in a 4759 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst". 4760 static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) { 4761 // If this is FI+Offset, we can model it. 4762 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) 4763 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset); 4764 4765 // If this is (FI+Offset1)+Offset2, we can model it. 4766 if (Ptr.getOpcode() != ISD::ADD || 4767 !isa<ConstantSDNode>(Ptr.getOperand(1)) || 4768 !isa<FrameIndexSDNode>(Ptr.getOperand(0))) 4769 return MachinePointerInfo(); 4770 4771 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); 4772 return MachinePointerInfo::getFixedStack(FI, Offset+ 4773 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue()); 4774 } 4775 4776 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a 4777 /// MachinePointerInfo record from it. This is particularly useful because the 4778 /// code generator has many cases where it doesn't bother passing in a 4779 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst". 4780 static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) { 4781 // If the 'Offset' value isn't a constant, we can't handle this. 4782 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp)) 4783 return InferPointerInfo(Ptr, OffsetNode->getSExtValue()); 4784 if (OffsetOp.getOpcode() == ISD::UNDEF) 4785 return InferPointerInfo(Ptr); 4786 return MachinePointerInfo(); 4787 } 4788 4789 4790 SDValue 4791 SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, 4792 EVT VT, SDLoc dl, SDValue Chain, 4793 SDValue Ptr, SDValue Offset, 4794 MachinePointerInfo PtrInfo, EVT MemVT, 4795 bool isVolatile, bool isNonTemporal, bool isInvariant, 4796 unsigned Alignment, const AAMDNodes &AAInfo, 4797 const MDNode *Ranges) { 4798 assert(Chain.getValueType() == MVT::Other && 4799 "Invalid chain type"); 4800 if (Alignment == 0) // Ensure that codegen never sees alignment 0 4801 Alignment = getEVTAlignment(VT); 4802 4803 unsigned Flags = MachineMemOperand::MOLoad; 4804 if (isVolatile) 4805 Flags |= MachineMemOperand::MOVolatile; 4806 if (isNonTemporal) 4807 Flags |= MachineMemOperand::MONonTemporal; 4808 if (isInvariant) 4809 Flags |= MachineMemOperand::MOInvariant; 4810 4811 // If we don't have a PtrInfo, infer the trivial frame index case to simplify 4812 // clients. 4813 if (PtrInfo.V.isNull()) 4814 PtrInfo = InferPointerInfo(Ptr, Offset); 4815 4816 MachineFunction &MF = getMachineFunction(); 4817 MachineMemOperand *MMO = 4818 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment, 4819 AAInfo, Ranges); 4820 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO); 4821 } 4822 4823 SDValue 4824 SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, 4825 EVT VT, SDLoc dl, SDValue Chain, 4826 SDValue Ptr, SDValue Offset, EVT MemVT, 4827 MachineMemOperand *MMO) { 4828 if (VT == MemVT) { 4829 ExtType = ISD::NON_EXTLOAD; 4830 } else if (ExtType == ISD::NON_EXTLOAD) { 4831 assert(VT == MemVT && "Non-extending load from different memory type!"); 4832 } else { 4833 // Extending load. 4834 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) && 4835 "Should only be an extending load, not truncating!"); 4836 assert(VT.isInteger() == MemVT.isInteger() && 4837 "Cannot convert from FP to Int or Int -> FP!"); 4838 assert(VT.isVector() == MemVT.isVector() && 4839 "Cannot use an ext load to convert to or from a vector!"); 4840 assert((!VT.isVector() || 4841 VT.getVectorNumElements() == MemVT.getVectorNumElements()) && 4842 "Cannot use an ext load to change the number of vector elements!"); 4843 } 4844 4845 bool Indexed = AM != ISD::UNINDEXED; 4846 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) && 4847 "Unindexed load with an offset!"); 4848 4849 SDVTList VTs = Indexed ? 4850 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other); 4851 SDValue Ops[] = { Chain, Ptr, Offset }; 4852 FoldingSetNodeID ID; 4853 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops); 4854 ID.AddInteger(MemVT.getRawBits()); 4855 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(), 4856 MMO->isNonTemporal(), 4857 MMO->isInvariant())); 4858 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 4859 void *IP = nullptr; 4860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4861 cast<LoadSDNode>(E)->refineAlignment(MMO); 4862 return SDValue(E, 0); 4863 } 4864 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(), 4865 dl.getDebugLoc(), VTs, AM, ExtType, 4866 MemVT, MMO); 4867 CSEMap.InsertNode(N, IP); 4868 InsertNode(N); 4869 return SDValue(N, 0); 4870 } 4871 4872 SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl, 4873 SDValue Chain, SDValue Ptr, 4874 MachinePointerInfo PtrInfo, 4875 bool isVolatile, bool isNonTemporal, 4876 bool isInvariant, unsigned Alignment, 4877 const AAMDNodes &AAInfo, 4878 const MDNode *Ranges) { 4879 SDValue Undef = getUNDEF(Ptr.getValueType()); 4880 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef, 4881 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment, 4882 AAInfo, Ranges); 4883 } 4884 4885 SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl, 4886 SDValue Chain, SDValue Ptr, 4887 MachineMemOperand *MMO) { 4888 SDValue Undef = getUNDEF(Ptr.getValueType()); 4889 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef, 4890 VT, MMO); 4891 } 4892 4893 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT, 4894 SDValue Chain, SDValue Ptr, 4895 MachinePointerInfo PtrInfo, EVT MemVT, 4896 bool isVolatile, bool isNonTemporal, 4897 bool isInvariant, unsigned Alignment, 4898 const AAMDNodes &AAInfo) { 4899 SDValue Undef = getUNDEF(Ptr.getValueType()); 4900 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, 4901 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant, 4902 Alignment, AAInfo); 4903 } 4904 4905 4906 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT, 4907 SDValue Chain, SDValue Ptr, EVT MemVT, 4908 MachineMemOperand *MMO) { 4909 SDValue Undef = getUNDEF(Ptr.getValueType()); 4910 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, 4911 MemVT, MMO); 4912 } 4913 4914 SDValue 4915 SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base, 4916 SDValue Offset, ISD::MemIndexedMode AM) { 4917 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad); 4918 assert(LD->getOffset().getOpcode() == ISD::UNDEF && 4919 "Load is already a indexed load!"); 4920 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl, 4921 LD->getChain(), Base, Offset, LD->getPointerInfo(), 4922 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(), 4923 false, LD->getAlignment()); 4924 } 4925 4926 SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val, 4927 SDValue Ptr, MachinePointerInfo PtrInfo, 4928 bool isVolatile, bool isNonTemporal, 4929 unsigned Alignment, const AAMDNodes &AAInfo) { 4930 assert(Chain.getValueType() == MVT::Other && 4931 "Invalid chain type"); 4932 if (Alignment == 0) // Ensure that codegen never sees alignment 0 4933 Alignment = getEVTAlignment(Val.getValueType()); 4934 4935 unsigned Flags = MachineMemOperand::MOStore; 4936 if (isVolatile) 4937 Flags |= MachineMemOperand::MOVolatile; 4938 if (isNonTemporal) 4939 Flags |= MachineMemOperand::MONonTemporal; 4940 4941 if (PtrInfo.V.isNull()) 4942 PtrInfo = InferPointerInfo(Ptr); 4943 4944 MachineFunction &MF = getMachineFunction(); 4945 MachineMemOperand *MMO = 4946 MF.getMachineMemOperand(PtrInfo, Flags, 4947 Val.getValueType().getStoreSize(), Alignment, 4948 AAInfo); 4949 4950 return getStore(Chain, dl, Val, Ptr, MMO); 4951 } 4952 4953 SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val, 4954 SDValue Ptr, MachineMemOperand *MMO) { 4955 assert(Chain.getValueType() == MVT::Other && 4956 "Invalid chain type"); 4957 EVT VT = Val.getValueType(); 4958 SDVTList VTs = getVTList(MVT::Other); 4959 SDValue Undef = getUNDEF(Ptr.getValueType()); 4960 SDValue Ops[] = { Chain, Val, Ptr, Undef }; 4961 FoldingSetNodeID ID; 4962 AddNodeIDNode(ID, ISD::STORE, VTs, Ops); 4963 ID.AddInteger(VT.getRawBits()); 4964 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(), 4965 MMO->isNonTemporal(), MMO->isInvariant())); 4966 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 4967 void *IP = nullptr; 4968 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 4969 cast<StoreSDNode>(E)->refineAlignment(MMO); 4970 return SDValue(E, 0); 4971 } 4972 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), 4973 dl.getDebugLoc(), VTs, 4974 ISD::UNINDEXED, false, VT, MMO); 4975 CSEMap.InsertNode(N, IP); 4976 InsertNode(N); 4977 return SDValue(N, 0); 4978 } 4979 4980 SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, 4981 SDValue Ptr, MachinePointerInfo PtrInfo, 4982 EVT SVT,bool isVolatile, bool isNonTemporal, 4983 unsigned Alignment, 4984 const AAMDNodes &AAInfo) { 4985 assert(Chain.getValueType() == MVT::Other && 4986 "Invalid chain type"); 4987 if (Alignment == 0) // Ensure that codegen never sees alignment 0 4988 Alignment = getEVTAlignment(SVT); 4989 4990 unsigned Flags = MachineMemOperand::MOStore; 4991 if (isVolatile) 4992 Flags |= MachineMemOperand::MOVolatile; 4993 if (isNonTemporal) 4994 Flags |= MachineMemOperand::MONonTemporal; 4995 4996 if (PtrInfo.V.isNull()) 4997 PtrInfo = InferPointerInfo(Ptr); 4998 4999 MachineFunction &MF = getMachineFunction(); 5000 MachineMemOperand *MMO = 5001 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment, 5002 AAInfo); 5003 5004 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO); 5005 } 5006 5007 SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, 5008 SDValue Ptr, EVT SVT, 5009 MachineMemOperand *MMO) { 5010 EVT VT = Val.getValueType(); 5011 5012 assert(Chain.getValueType() == MVT::Other && 5013 "Invalid chain type"); 5014 if (VT == SVT) 5015 return getStore(Chain, dl, Val, Ptr, MMO); 5016 5017 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) && 5018 "Should only be a truncating store, not extending!"); 5019 assert(VT.isInteger() == SVT.isInteger() && 5020 "Can't do FP-INT conversion!"); 5021 assert(VT.isVector() == SVT.isVector() && 5022 "Cannot use trunc store to convert to or from a vector!"); 5023 assert((!VT.isVector() || 5024 VT.getVectorNumElements() == SVT.getVectorNumElements()) && 5025 "Cannot use trunc store to change the number of vector elements!"); 5026 5027 SDVTList VTs = getVTList(MVT::Other); 5028 SDValue Undef = getUNDEF(Ptr.getValueType()); 5029 SDValue Ops[] = { Chain, Val, Ptr, Undef }; 5030 FoldingSetNodeID ID; 5031 AddNodeIDNode(ID, ISD::STORE, VTs, Ops); 5032 ID.AddInteger(SVT.getRawBits()); 5033 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(), 5034 MMO->isNonTemporal(), MMO->isInvariant())); 5035 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 5036 void *IP = nullptr; 5037 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 5038 cast<StoreSDNode>(E)->refineAlignment(MMO); 5039 return SDValue(E, 0); 5040 } 5041 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), 5042 dl.getDebugLoc(), VTs, 5043 ISD::UNINDEXED, true, SVT, MMO); 5044 CSEMap.InsertNode(N, IP); 5045 InsertNode(N); 5046 return SDValue(N, 0); 5047 } 5048 5049 SDValue 5050 SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base, 5051 SDValue Offset, ISD::MemIndexedMode AM) { 5052 StoreSDNode *ST = cast<StoreSDNode>(OrigStore); 5053 assert(ST->getOffset().getOpcode() == ISD::UNDEF && 5054 "Store is already a indexed store!"); 5055 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other); 5056 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset }; 5057 FoldingSetNodeID ID; 5058 AddNodeIDNode(ID, ISD::STORE, VTs, Ops); 5059 ID.AddInteger(ST->getMemoryVT().getRawBits()); 5060 ID.AddInteger(ST->getRawSubclassData()); 5061 ID.AddInteger(ST->getPointerInfo().getAddrSpace()); 5062 void *IP = nullptr; 5063 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 5064 return SDValue(E, 0); 5065 5066 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), 5067 dl.getDebugLoc(), VTs, AM, 5068 ST->isTruncatingStore(), 5069 ST->getMemoryVT(), 5070 ST->getMemOperand()); 5071 CSEMap.InsertNode(N, IP); 5072 InsertNode(N); 5073 return SDValue(N, 0); 5074 } 5075 5076 SDValue 5077 SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain, 5078 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT, 5079 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) { 5080 5081 SDVTList VTs = getVTList(VT, MVT::Other); 5082 SDValue Ops[] = { Chain, Ptr, Mask, Src0 }; 5083 FoldingSetNodeID ID; 5084 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops); 5085 ID.AddInteger(VT.getRawBits()); 5086 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED, 5087 MMO->isVolatile(), 5088 MMO->isNonTemporal(), 5089 MMO->isInvariant())); 5090 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 5091 void *IP = nullptr; 5092 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 5093 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO); 5094 return SDValue(E, 0); 5095 } 5096 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(), 5097 dl.getDebugLoc(), Ops, 4, VTs, 5098 ExtTy, MemVT, MMO); 5099 CSEMap.InsertNode(N, IP); 5100 InsertNode(N); 5101 return SDValue(N, 0); 5102 } 5103 5104 SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val, 5105 SDValue Ptr, SDValue Mask, EVT MemVT, 5106 MachineMemOperand *MMO, bool isTrunc) { 5107 assert(Chain.getValueType() == MVT::Other && 5108 "Invalid chain type"); 5109 EVT VT = Val.getValueType(); 5110 SDVTList VTs = getVTList(MVT::Other); 5111 SDValue Ops[] = { Chain, Ptr, Mask, Val }; 5112 FoldingSetNodeID ID; 5113 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops); 5114 ID.AddInteger(VT.getRawBits()); 5115 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(), 5116 MMO->isNonTemporal(), MMO->isInvariant())); 5117 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 5118 void *IP = nullptr; 5119 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 5120 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO); 5121 return SDValue(E, 0); 5122 } 5123 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(), 5124 dl.getDebugLoc(), Ops, 4, 5125 VTs, isTrunc, MemVT, MMO); 5126 CSEMap.InsertNode(N, IP); 5127 InsertNode(N); 5128 return SDValue(N, 0); 5129 } 5130 5131 SDValue 5132 SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl, 5133 ArrayRef<SDValue> Ops, 5134 MachineMemOperand *MMO) { 5135 5136 FoldingSetNodeID ID; 5137 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops); 5138 ID.AddInteger(VT.getRawBits()); 5139 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED, 5140 MMO->isVolatile(), 5141 MMO->isNonTemporal(), 5142 MMO->isInvariant())); 5143 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 5144 void *IP = nullptr; 5145 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 5146 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO); 5147 return SDValue(E, 0); 5148 } 5149 MaskedGatherSDNode *N = 5150 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(), 5151 Ops, VTs, VT, MMO); 5152 CSEMap.InsertNode(N, IP); 5153 InsertNode(N); 5154 return SDValue(N, 0); 5155 } 5156 5157 SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl, 5158 ArrayRef<SDValue> Ops, 5159 MachineMemOperand *MMO) { 5160 FoldingSetNodeID ID; 5161 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops); 5162 ID.AddInteger(VT.getRawBits()); 5163 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(), 5164 MMO->isNonTemporal(), 5165 MMO->isInvariant())); 5166 ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); 5167 void *IP = nullptr; 5168 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 5169 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO); 5170 return SDValue(E, 0); 5171 } 5172 SDNode *N = 5173 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(), 5174 Ops, VTs, VT, MMO); 5175 CSEMap.InsertNode(N, IP); 5176 InsertNode(N); 5177 return SDValue(N, 0); 5178 } 5179 5180 SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl, 5181 SDValue Chain, SDValue Ptr, 5182 SDValue SV, 5183 unsigned Align) { 5184 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) }; 5185 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops); 5186 } 5187 5188 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, 5189 ArrayRef<SDUse> Ops) { 5190 switch (Ops.size()) { 5191 case 0: return getNode(Opcode, DL, VT); 5192 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0])); 5193 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); 5194 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); 5195 default: break; 5196 } 5197 5198 // Copy from an SDUse array into an SDValue array for use with 5199 // the regular getNode logic. 5200 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end()); 5201 return getNode(Opcode, DL, VT, NewOps); 5202 } 5203 5204 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, 5205 ArrayRef<SDValue> Ops) { 5206 unsigned NumOps = Ops.size(); 5207 switch (NumOps) { 5208 case 0: return getNode(Opcode, DL, VT); 5209 case 1: return getNode(Opcode, DL, VT, Ops[0]); 5210 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); 5211 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); 5212 default: break; 5213 } 5214 5215 switch (Opcode) { 5216 default: break; 5217 case ISD::SELECT_CC: { 5218 assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); 5219 assert(Ops[0].getValueType() == Ops[1].getValueType() && 5220 "LHS and RHS of condition must have same type!"); 5221 assert(Ops[2].getValueType() == Ops[3].getValueType() && 5222 "True and False arms of SelectCC must have same type!"); 5223 assert(Ops[2].getValueType() == VT && 5224 "select_cc node must be of same type as true and false value!"); 5225 break; 5226 } 5227 case ISD::BR_CC: { 5228 assert(NumOps == 5 && "BR_CC takes 5 operands!"); 5229 assert(Ops[2].getValueType() == Ops[3].getValueType() && 5230 "LHS/RHS of comparison should match types!"); 5231 break; 5232 } 5233 } 5234 5235 // Memoize nodes. 5236 SDNode *N; 5237 SDVTList VTs = getVTList(VT); 5238 5239 if (VT != MVT::Glue) { 5240 FoldingSetNodeID ID; 5241 AddNodeIDNode(ID, Opcode, VTs, Ops); 5242 void *IP = nullptr; 5243 5244 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 5245 return SDValue(E, 0); 5246 5247 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), 5248 VTs, Ops); 5249 CSEMap.InsertNode(N, IP); 5250 } else { 5251 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), 5252 VTs, Ops); 5253 } 5254 5255 InsertNode(N); 5256 return SDValue(N, 0); 5257 } 5258 5259 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, 5260 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) { 5261 return getNode(Opcode, DL, getVTList(ResultTys), Ops); 5262 } 5263 5264 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, 5265 ArrayRef<SDValue> Ops) { 5266 if (VTList.NumVTs == 1) 5267 return getNode(Opcode, DL, VTList.VTs[0], Ops); 5268 5269 #if 0 5270 switch (Opcode) { 5271 // FIXME: figure out how to safely handle things like 5272 // int foo(int x) { return 1 << (x & 255); } 5273 // int bar() { return foo(256); } 5274 case ISD::SRA_PARTS: 5275 case ISD::SRL_PARTS: 5276 case ISD::SHL_PARTS: 5277 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 5278 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 5279 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); 5280 else if (N3.getOpcode() == ISD::AND) 5281 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 5282 // If the and is only masking out bits that cannot effect the shift, 5283 // eliminate the and. 5284 unsigned NumBits = VT.getScalarType().getSizeInBits()*2; 5285 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 5286 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); 5287 } 5288 break; 5289 } 5290 #endif 5291 5292 // Memoize the node unless it returns a flag. 5293 SDNode *N; 5294 unsigned NumOps = Ops.size(); 5295 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { 5296 FoldingSetNodeID ID; 5297 AddNodeIDNode(ID, Opcode, VTList, Ops); 5298 void *IP = nullptr; 5299 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 5300 return SDValue(E, 0); 5301 5302 if (NumOps == 1) { 5303 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), 5304 DL.getDebugLoc(), VTList, Ops[0]); 5305 } else if (NumOps == 2) { 5306 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), 5307 DL.getDebugLoc(), VTList, Ops[0], 5308 Ops[1]); 5309 } else if (NumOps == 3) { 5310 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), 5311 DL.getDebugLoc(), VTList, Ops[0], 5312 Ops[1], Ops[2]); 5313 } else { 5314 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), 5315 VTList, Ops); 5316 } 5317 CSEMap.InsertNode(N, IP); 5318 } else { 5319 if (NumOps == 1) { 5320 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), 5321 DL.getDebugLoc(), VTList, Ops[0]); 5322 } else if (NumOps == 2) { 5323 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), 5324 DL.getDebugLoc(), VTList, Ops[0], 5325 Ops[1]); 5326 } else if (NumOps == 3) { 5327 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), 5328 DL.getDebugLoc(), VTList, Ops[0], 5329 Ops[1], Ops[2]); 5330 } else { 5331 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), 5332 VTList, Ops); 5333 } 5334 } 5335 InsertNode(N); 5336 return SDValue(N, 0); 5337 } 5338 5339 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) { 5340 return getNode(Opcode, DL, VTList, None); 5341 } 5342 5343 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, 5344 SDValue N1) { 5345 SDValue Ops[] = { N1 }; 5346 return getNode(Opcode, DL, VTList, Ops); 5347 } 5348 5349 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, 5350 SDValue N1, SDValue N2) { 5351 SDValue Ops[] = { N1, N2 }; 5352 return getNode(Opcode, DL, VTList, Ops); 5353 } 5354 5355 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, 5356 SDValue N1, SDValue N2, SDValue N3) { 5357 SDValue Ops[] = { N1, N2, N3 }; 5358 return getNode(Opcode, DL, VTList, Ops); 5359 } 5360 5361 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, 5362 SDValue N1, SDValue N2, SDValue N3, 5363 SDValue N4) { 5364 SDValue Ops[] = { N1, N2, N3, N4 }; 5365 return getNode(Opcode, DL, VTList, Ops); 5366 } 5367 5368 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, 5369 SDValue N1, SDValue N2, SDValue N3, 5370 SDValue N4, SDValue N5) { 5371 SDValue Ops[] = { N1, N2, N3, N4, N5 }; 5372 return getNode(Opcode, DL, VTList, Ops); 5373 } 5374 5375 SDVTList SelectionDAG::getVTList(EVT VT) { 5376 return makeVTList(SDNode::getValueTypeList(VT), 1); 5377 } 5378 5379 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) { 5380 FoldingSetNodeID ID; 5381 ID.AddInteger(2U); 5382 ID.AddInteger(VT1.getRawBits()); 5383 ID.AddInteger(VT2.getRawBits()); 5384 5385 void *IP = nullptr; 5386 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); 5387 if (!Result) { 5388 EVT *Array = Allocator.Allocate<EVT>(2); 5389 Array[0] = VT1; 5390 Array[1] = VT2; 5391 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2); 5392 VTListMap.InsertNode(Result, IP); 5393 } 5394 return Result->getSDVTList(); 5395 } 5396 5397 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) { 5398 FoldingSetNodeID ID; 5399 ID.AddInteger(3U); 5400 ID.AddInteger(VT1.getRawBits()); 5401 ID.AddInteger(VT2.getRawBits()); 5402 ID.AddInteger(VT3.getRawBits()); 5403 5404 void *IP = nullptr; 5405 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); 5406 if (!Result) { 5407 EVT *Array = Allocator.Allocate<EVT>(3); 5408 Array[0] = VT1; 5409 Array[1] = VT2; 5410 Array[2] = VT3; 5411 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3); 5412 VTListMap.InsertNode(Result, IP); 5413 } 5414 return Result->getSDVTList(); 5415 } 5416 5417 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) { 5418 FoldingSetNodeID ID; 5419 ID.AddInteger(4U); 5420 ID.AddInteger(VT1.getRawBits()); 5421 ID.AddInteger(VT2.getRawBits()); 5422 ID.AddInteger(VT3.getRawBits()); 5423 ID.AddInteger(VT4.getRawBits()); 5424 5425 void *IP = nullptr; 5426 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); 5427 if (!Result) { 5428 EVT *Array = Allocator.Allocate<EVT>(4); 5429 Array[0] = VT1; 5430 Array[1] = VT2; 5431 Array[2] = VT3; 5432 Array[3] = VT4; 5433 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4); 5434 VTListMap.InsertNode(Result, IP); 5435 } 5436 return Result->getSDVTList(); 5437 } 5438 5439 SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) { 5440 unsigned NumVTs = VTs.size(); 5441 FoldingSetNodeID ID; 5442 ID.AddInteger(NumVTs); 5443 for (unsigned index = 0; index < NumVTs; index++) { 5444 ID.AddInteger(VTs[index].getRawBits()); 5445 } 5446 5447 void *IP = nullptr; 5448 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); 5449 if (!Result) { 5450 EVT *Array = Allocator.Allocate<EVT>(NumVTs); 5451 std::copy(VTs.begin(), VTs.end(), Array); 5452 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs); 5453 VTListMap.InsertNode(Result, IP); 5454 } 5455 return Result->getSDVTList(); 5456 } 5457 5458 5459 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the 5460 /// specified operands. If the resultant node already exists in the DAG, 5461 /// this does not modify the specified node, instead it returns the node that 5462 /// already exists. If the resultant node does not exist in the DAG, the 5463 /// input node is returned. As a degenerate case, if you specify the same 5464 /// input operands as the node already has, the input node is returned. 5465 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) { 5466 assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 5467 5468 // Check to see if there is no change. 5469 if (Op == N->getOperand(0)) return N; 5470 5471 // See if the modified node already exists. 5472 void *InsertPos = nullptr; 5473 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) 5474 return Existing; 5475 5476 // Nope it doesn't. Remove the node from its current place in the maps. 5477 if (InsertPos) 5478 if (!RemoveNodeFromCSEMaps(N)) 5479 InsertPos = nullptr; 5480 5481 // Now we update the operands. 5482 N->OperandList[0].set(Op); 5483 5484 // If this gets put into a CSE map, add it. 5485 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 5486 return N; 5487 } 5488 5489 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) { 5490 assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 5491 5492 // Check to see if there is no change. 5493 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 5494 return N; // No operands changed, just return the input node. 5495 5496 // See if the modified node already exists. 5497 void *InsertPos = nullptr; 5498 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) 5499 return Existing; 5500 5501 // Nope it doesn't. Remove the node from its current place in the maps. 5502 if (InsertPos) 5503 if (!RemoveNodeFromCSEMaps(N)) 5504 InsertPos = nullptr; 5505 5506 // Now we update the operands. 5507 if (N->OperandList[0] != Op1) 5508 N->OperandList[0].set(Op1); 5509 if (N->OperandList[1] != Op2) 5510 N->OperandList[1].set(Op2); 5511 5512 // If this gets put into a CSE map, add it. 5513 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 5514 return N; 5515 } 5516 5517 SDNode *SelectionDAG:: 5518 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) { 5519 SDValue Ops[] = { Op1, Op2, Op3 }; 5520 return UpdateNodeOperands(N, Ops); 5521 } 5522 5523 SDNode *SelectionDAG:: 5524 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 5525 SDValue Op3, SDValue Op4) { 5526 SDValue Ops[] = { Op1, Op2, Op3, Op4 }; 5527 return UpdateNodeOperands(N, Ops); 5528 } 5529 5530 SDNode *SelectionDAG:: 5531 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 5532 SDValue Op3, SDValue Op4, SDValue Op5) { 5533 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 }; 5534 return UpdateNodeOperands(N, Ops); 5535 } 5536 5537 SDNode *SelectionDAG:: 5538 UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) { 5539 unsigned NumOps = Ops.size(); 5540 assert(N->getNumOperands() == NumOps && 5541 "Update with wrong number of operands"); 5542 5543 // If no operands changed just return the input node. 5544 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin())) 5545 return N; 5546 5547 // See if the modified node already exists. 5548 void *InsertPos = nullptr; 5549 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos)) 5550 return Existing; 5551 5552 // Nope it doesn't. Remove the node from its current place in the maps. 5553 if (InsertPos) 5554 if (!RemoveNodeFromCSEMaps(N)) 5555 InsertPos = nullptr; 5556 5557 // Now we update the operands. 5558 for (unsigned i = 0; i != NumOps; ++i) 5559 if (N->OperandList[i] != Ops[i]) 5560 N->OperandList[i].set(Ops[i]); 5561 5562 // If this gets put into a CSE map, add it. 5563 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 5564 return N; 5565 } 5566 5567 /// DropOperands - Release the operands and set this node to have 5568 /// zero operands. 5569 void SDNode::DropOperands() { 5570 // Unlike the code in MorphNodeTo that does this, we don't need to 5571 // watch for dead nodes here. 5572 for (op_iterator I = op_begin(), E = op_end(); I != E; ) { 5573 SDUse &Use = *I++; 5574 Use.set(SDValue()); 5575 } 5576 } 5577 5578 /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a 5579 /// machine opcode. 5580 /// 5581 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5582 EVT VT) { 5583 SDVTList VTs = getVTList(VT); 5584 return SelectNodeTo(N, MachineOpc, VTs, None); 5585 } 5586 5587 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5588 EVT VT, SDValue Op1) { 5589 SDVTList VTs = getVTList(VT); 5590 SDValue Ops[] = { Op1 }; 5591 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5592 } 5593 5594 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5595 EVT VT, SDValue Op1, 5596 SDValue Op2) { 5597 SDVTList VTs = getVTList(VT); 5598 SDValue Ops[] = { Op1, Op2 }; 5599 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5600 } 5601 5602 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5603 EVT VT, SDValue Op1, 5604 SDValue Op2, SDValue Op3) { 5605 SDVTList VTs = getVTList(VT); 5606 SDValue Ops[] = { Op1, Op2, Op3 }; 5607 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5608 } 5609 5610 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5611 EVT VT, ArrayRef<SDValue> Ops) { 5612 SDVTList VTs = getVTList(VT); 5613 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5614 } 5615 5616 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5617 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) { 5618 SDVTList VTs = getVTList(VT1, VT2); 5619 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5620 } 5621 5622 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5623 EVT VT1, EVT VT2) { 5624 SDVTList VTs = getVTList(VT1, VT2); 5625 return SelectNodeTo(N, MachineOpc, VTs, None); 5626 } 5627 5628 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5629 EVT VT1, EVT VT2, EVT VT3, 5630 ArrayRef<SDValue> Ops) { 5631 SDVTList VTs = getVTList(VT1, VT2, VT3); 5632 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5633 } 5634 5635 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5636 EVT VT1, EVT VT2, EVT VT3, EVT VT4, 5637 ArrayRef<SDValue> Ops) { 5638 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); 5639 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5640 } 5641 5642 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5643 EVT VT1, EVT VT2, 5644 SDValue Op1) { 5645 SDVTList VTs = getVTList(VT1, VT2); 5646 SDValue Ops[] = { Op1 }; 5647 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5648 } 5649 5650 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5651 EVT VT1, EVT VT2, 5652 SDValue Op1, SDValue Op2) { 5653 SDVTList VTs = getVTList(VT1, VT2); 5654 SDValue Ops[] = { Op1, Op2 }; 5655 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5656 } 5657 5658 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5659 EVT VT1, EVT VT2, 5660 SDValue Op1, SDValue Op2, 5661 SDValue Op3) { 5662 SDVTList VTs = getVTList(VT1, VT2); 5663 SDValue Ops[] = { Op1, Op2, Op3 }; 5664 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5665 } 5666 5667 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5668 EVT VT1, EVT VT2, EVT VT3, 5669 SDValue Op1, SDValue Op2, 5670 SDValue Op3) { 5671 SDVTList VTs = getVTList(VT1, VT2, VT3); 5672 SDValue Ops[] = { Op1, Op2, Op3 }; 5673 return SelectNodeTo(N, MachineOpc, VTs, Ops); 5674 } 5675 5676 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, 5677 SDVTList VTs,ArrayRef<SDValue> Ops) { 5678 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops); 5679 // Reset the NodeID to -1. 5680 N->setNodeId(-1); 5681 return N; 5682 } 5683 5684 /// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away 5685 /// the line number information on the merged node since it is not possible to 5686 /// preserve the information that operation is associated with multiple lines. 5687 /// This will make the debugger working better at -O0, were there is a higher 5688 /// probability having other instructions associated with that line. 5689 /// 5690 /// For IROrder, we keep the smaller of the two 5691 SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) { 5692 DebugLoc NLoc = N->getDebugLoc(); 5693 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) { 5694 N->setDebugLoc(DebugLoc()); 5695 } 5696 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder()); 5697 N->setIROrder(Order); 5698 return N; 5699 } 5700 5701 /// MorphNodeTo - This *mutates* the specified node to have the specified 5702 /// return type, opcode, and operands. 5703 /// 5704 /// Note that MorphNodeTo returns the resultant node. If there is already a 5705 /// node of the specified opcode and operands, it returns that node instead of 5706 /// the current one. Note that the SDLoc need not be the same. 5707 /// 5708 /// Using MorphNodeTo is faster than creating a new node and swapping it in 5709 /// with ReplaceAllUsesWith both because it often avoids allocating a new 5710 /// node, and because it doesn't require CSE recalculation for any of 5711 /// the node's users. 5712 /// 5713 /// However, note that MorphNodeTo recursively deletes dead nodes from the DAG. 5714 /// As a consequence it isn't appropriate to use from within the DAG combiner or 5715 /// the legalizer which maintain worklists that would need to be updated when 5716 /// deleting things. 5717 SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc, 5718 SDVTList VTs, ArrayRef<SDValue> Ops) { 5719 unsigned NumOps = Ops.size(); 5720 // If an identical node already exists, use it. 5721 void *IP = nullptr; 5722 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) { 5723 FoldingSetNodeID ID; 5724 AddNodeIDNode(ID, Opc, VTs, Ops); 5725 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 5726 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N)); 5727 } 5728 5729 if (!RemoveNodeFromCSEMaps(N)) 5730 IP = nullptr; 5731 5732 // Start the morphing. 5733 N->NodeType = Opc; 5734 N->ValueList = VTs.VTs; 5735 N->NumValues = VTs.NumVTs; 5736 5737 // Clear the operands list, updating used nodes to remove this from their 5738 // use list. Keep track of any operands that become dead as a result. 5739 SmallPtrSet<SDNode*, 16> DeadNodeSet; 5740 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { 5741 SDUse &Use = *I++; 5742 SDNode *Used = Use.getNode(); 5743 Use.set(SDValue()); 5744 if (Used->use_empty()) 5745 DeadNodeSet.insert(Used); 5746 } 5747 5748 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) { 5749 // Initialize the memory references information. 5750 MN->setMemRefs(nullptr, nullptr); 5751 // If NumOps is larger than the # of operands we can have in a 5752 // MachineSDNode, reallocate the operand list. 5753 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) { 5754 if (MN->OperandsNeedDelete) 5755 delete[] MN->OperandList; 5756 if (NumOps > array_lengthof(MN->LocalOperands)) 5757 // We're creating a final node that will live unmorphed for the 5758 // remainder of the current SelectionDAG iteration, so we can allocate 5759 // the operands directly out of a pool with no recycling metadata. 5760 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), 5761 Ops.data(), NumOps); 5762 else 5763 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps); 5764 MN->OperandsNeedDelete = false; 5765 } else 5766 MN->InitOperands(MN->OperandList, Ops.data(), NumOps); 5767 } else { 5768 // If NumOps is larger than the # of operands we currently have, reallocate 5769 // the operand list. 5770 if (NumOps > N->NumOperands) { 5771 if (N->OperandsNeedDelete) 5772 delete[] N->OperandList; 5773 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps); 5774 N->OperandsNeedDelete = true; 5775 } else 5776 N->InitOperands(N->OperandList, Ops.data(), NumOps); 5777 } 5778 5779 // Delete any nodes that are still dead after adding the uses for the 5780 // new operands. 5781 if (!DeadNodeSet.empty()) { 5782 SmallVector<SDNode *, 16> DeadNodes; 5783 for (SDNode *N : DeadNodeSet) 5784 if (N->use_empty()) 5785 DeadNodes.push_back(N); 5786 RemoveDeadNodes(DeadNodes); 5787 } 5788 5789 if (IP) 5790 CSEMap.InsertNode(N, IP); // Memoize the new node. 5791 return N; 5792 } 5793 5794 5795 /// getMachineNode - These are used for target selectors to create a new node 5796 /// with specified return type(s), MachineInstr opcode, and operands. 5797 /// 5798 /// Note that getMachineNode returns the resultant node. If there is already a 5799 /// node of the specified opcode and operands, it returns that node instead of 5800 /// the current one. 5801 MachineSDNode * 5802 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) { 5803 SDVTList VTs = getVTList(VT); 5804 return getMachineNode(Opcode, dl, VTs, None); 5805 } 5806 5807 MachineSDNode * 5808 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) { 5809 SDVTList VTs = getVTList(VT); 5810 SDValue Ops[] = { Op1 }; 5811 return getMachineNode(Opcode, dl, VTs, Ops); 5812 } 5813 5814 MachineSDNode * 5815 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, 5816 SDValue Op1, SDValue Op2) { 5817 SDVTList VTs = getVTList(VT); 5818 SDValue Ops[] = { Op1, Op2 }; 5819 return getMachineNode(Opcode, dl, VTs, Ops); 5820 } 5821 5822 MachineSDNode * 5823 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, 5824 SDValue Op1, SDValue Op2, SDValue Op3) { 5825 SDVTList VTs = getVTList(VT); 5826 SDValue Ops[] = { Op1, Op2, Op3 }; 5827 return getMachineNode(Opcode, dl, VTs, Ops); 5828 } 5829 5830 MachineSDNode * 5831 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, 5832 ArrayRef<SDValue> Ops) { 5833 SDVTList VTs = getVTList(VT); 5834 return getMachineNode(Opcode, dl, VTs, Ops); 5835 } 5836 5837 MachineSDNode * 5838 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) { 5839 SDVTList VTs = getVTList(VT1, VT2); 5840 return getMachineNode(Opcode, dl, VTs, None); 5841 } 5842 5843 MachineSDNode * 5844 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5845 EVT VT1, EVT VT2, SDValue Op1) { 5846 SDVTList VTs = getVTList(VT1, VT2); 5847 SDValue Ops[] = { Op1 }; 5848 return getMachineNode(Opcode, dl, VTs, Ops); 5849 } 5850 5851 MachineSDNode * 5852 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5853 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) { 5854 SDVTList VTs = getVTList(VT1, VT2); 5855 SDValue Ops[] = { Op1, Op2 }; 5856 return getMachineNode(Opcode, dl, VTs, Ops); 5857 } 5858 5859 MachineSDNode * 5860 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5861 EVT VT1, EVT VT2, SDValue Op1, 5862 SDValue Op2, SDValue Op3) { 5863 SDVTList VTs = getVTList(VT1, VT2); 5864 SDValue Ops[] = { Op1, Op2, Op3 }; 5865 return getMachineNode(Opcode, dl, VTs, Ops); 5866 } 5867 5868 MachineSDNode * 5869 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5870 EVT VT1, EVT VT2, 5871 ArrayRef<SDValue> Ops) { 5872 SDVTList VTs = getVTList(VT1, VT2); 5873 return getMachineNode(Opcode, dl, VTs, Ops); 5874 } 5875 5876 MachineSDNode * 5877 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5878 EVT VT1, EVT VT2, EVT VT3, 5879 SDValue Op1, SDValue Op2) { 5880 SDVTList VTs = getVTList(VT1, VT2, VT3); 5881 SDValue Ops[] = { Op1, Op2 }; 5882 return getMachineNode(Opcode, dl, VTs, Ops); 5883 } 5884 5885 MachineSDNode * 5886 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5887 EVT VT1, EVT VT2, EVT VT3, 5888 SDValue Op1, SDValue Op2, SDValue Op3) { 5889 SDVTList VTs = getVTList(VT1, VT2, VT3); 5890 SDValue Ops[] = { Op1, Op2, Op3 }; 5891 return getMachineNode(Opcode, dl, VTs, Ops); 5892 } 5893 5894 MachineSDNode * 5895 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5896 EVT VT1, EVT VT2, EVT VT3, 5897 ArrayRef<SDValue> Ops) { 5898 SDVTList VTs = getVTList(VT1, VT2, VT3); 5899 return getMachineNode(Opcode, dl, VTs, Ops); 5900 } 5901 5902 MachineSDNode * 5903 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, 5904 EVT VT2, EVT VT3, EVT VT4, 5905 ArrayRef<SDValue> Ops) { 5906 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); 5907 return getMachineNode(Opcode, dl, VTs, Ops); 5908 } 5909 5910 MachineSDNode * 5911 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, 5912 ArrayRef<EVT> ResultTys, 5913 ArrayRef<SDValue> Ops) { 5914 SDVTList VTs = getVTList(ResultTys); 5915 return getMachineNode(Opcode, dl, VTs, Ops); 5916 } 5917 5918 MachineSDNode * 5919 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs, 5920 ArrayRef<SDValue> OpsArray) { 5921 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue; 5922 MachineSDNode *N; 5923 void *IP = nullptr; 5924 const SDValue *Ops = OpsArray.data(); 5925 unsigned NumOps = OpsArray.size(); 5926 5927 if (DoCSE) { 5928 FoldingSetNodeID ID; 5929 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray); 5930 IP = nullptr; 5931 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { 5932 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL)); 5933 } 5934 } 5935 5936 // Allocate a new MachineSDNode. 5937 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(), 5938 DL.getDebugLoc(), VTs); 5939 5940 // Initialize the operands list. 5941 if (NumOps > array_lengthof(N->LocalOperands)) 5942 // We're creating a final node that will live unmorphed for the 5943 // remainder of the current SelectionDAG iteration, so we can allocate 5944 // the operands directly out of a pool with no recycling metadata. 5945 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), 5946 Ops, NumOps); 5947 else 5948 N->InitOperands(N->LocalOperands, Ops, NumOps); 5949 N->OperandsNeedDelete = false; 5950 5951 if (DoCSE) 5952 CSEMap.InsertNode(N, IP); 5953 5954 InsertNode(N); 5955 return N; 5956 } 5957 5958 /// getTargetExtractSubreg - A convenience function for creating 5959 /// TargetOpcode::EXTRACT_SUBREG nodes. 5960 SDValue 5961 SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT, 5962 SDValue Operand) { 5963 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32); 5964 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, 5965 VT, Operand, SRIdxVal); 5966 return SDValue(Subreg, 0); 5967 } 5968 5969 /// getTargetInsertSubreg - A convenience function for creating 5970 /// TargetOpcode::INSERT_SUBREG nodes. 5971 SDValue 5972 SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT, 5973 SDValue Operand, SDValue Subreg) { 5974 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32); 5975 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL, 5976 VT, Operand, Subreg, SRIdxVal); 5977 return SDValue(Result, 0); 5978 } 5979 5980 /// getNodeIfExists - Get the specified node if it's already available, or 5981 /// else return NULL. 5982 SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList, 5983 ArrayRef<SDValue> Ops, bool nuw, bool nsw, 5984 bool exact) { 5985 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) { 5986 FoldingSetNodeID ID; 5987 AddNodeIDNode(ID, Opcode, VTList, Ops); 5988 if (isBinOpWithFlags(Opcode)) 5989 AddBinaryNodeIDCustom(ID, nuw, nsw, exact); 5990 void *IP = nullptr; 5991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 5992 return E; 5993 } 5994 return nullptr; 5995 } 5996 5997 /// getDbgValue - Creates a SDDbgValue node. 5998 /// 5999 /// SDNode 6000 SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N, 6001 unsigned R, bool IsIndirect, uint64_t Off, 6002 DebugLoc DL, unsigned O) { 6003 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && 6004 "Expected inlined-at fields to agree"); 6005 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O); 6006 } 6007 6008 /// Constant 6009 SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr, 6010 const Value *C, uint64_t Off, 6011 DebugLoc DL, unsigned O) { 6012 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && 6013 "Expected inlined-at fields to agree"); 6014 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O); 6015 } 6016 6017 /// FrameIndex 6018 SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr, 6019 unsigned FI, uint64_t Off, 6020 DebugLoc DL, unsigned O) { 6021 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && 6022 "Expected inlined-at fields to agree"); 6023 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O); 6024 } 6025 6026 namespace { 6027 6028 /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node 6029 /// pointed to by a use iterator is deleted, increment the use iterator 6030 /// so that it doesn't dangle. 6031 /// 6032 class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener { 6033 SDNode::use_iterator &UI; 6034 SDNode::use_iterator &UE; 6035 6036 void NodeDeleted(SDNode *N, SDNode *E) override { 6037 // Increment the iterator as needed. 6038 while (UI != UE && N == *UI) 6039 ++UI; 6040 } 6041 6042 public: 6043 RAUWUpdateListener(SelectionDAG &d, 6044 SDNode::use_iterator &ui, 6045 SDNode::use_iterator &ue) 6046 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {} 6047 }; 6048 6049 } 6050 6051 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 6052 /// This can cause recursive merging of nodes in the DAG. 6053 /// 6054 /// This version assumes From has a single result value. 6055 /// 6056 void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) { 6057 SDNode *From = FromN.getNode(); 6058 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 && 6059 "Cannot replace with this method!"); 6060 assert(From != To.getNode() && "Cannot replace uses of with self"); 6061 6062 // Iterate over all the existing uses of From. New uses will be added 6063 // to the beginning of the use list, which we avoid visiting. 6064 // This specifically avoids visiting uses of From that arise while the 6065 // replacement is happening, because any such uses would be the result 6066 // of CSE: If an existing node looks like From after one of its operands 6067 // is replaced by To, we don't want to replace of all its users with To 6068 // too. See PR3018 for more info. 6069 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 6070 RAUWUpdateListener Listener(*this, UI, UE); 6071 while (UI != UE) { 6072 SDNode *User = *UI; 6073 6074 // This node is about to morph, remove its old self from the CSE maps. 6075 RemoveNodeFromCSEMaps(User); 6076 6077 // A user can appear in a use list multiple times, and when this 6078 // happens the uses are usually next to each other in the list. 6079 // To help reduce the number of CSE recomputations, process all 6080 // the uses of this user that we can find this way. 6081 do { 6082 SDUse &Use = UI.getUse(); 6083 ++UI; 6084 Use.set(To); 6085 } while (UI != UE && *UI == User); 6086 6087 // Now that we have modified User, add it back to the CSE maps. If it 6088 // already exists there, recursively merge the results together. 6089 AddModifiedNodeToCSEMaps(User); 6090 } 6091 6092 // If we just RAUW'd the root, take note. 6093 if (FromN == getRoot()) 6094 setRoot(To); 6095 } 6096 6097 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 6098 /// This can cause recursive merging of nodes in the DAG. 6099 /// 6100 /// This version assumes that for each value of From, there is a 6101 /// corresponding value in To in the same position with the same type. 6102 /// 6103 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) { 6104 #ifndef NDEBUG 6105 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) 6106 assert((!From->hasAnyUseOfValue(i) || 6107 From->getValueType(i) == To->getValueType(i)) && 6108 "Cannot use this version of ReplaceAllUsesWith!"); 6109 #endif 6110 6111 // Handle the trivial case. 6112 if (From == To) 6113 return; 6114 6115 // Iterate over just the existing users of From. See the comments in 6116 // the ReplaceAllUsesWith above. 6117 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 6118 RAUWUpdateListener Listener(*this, UI, UE); 6119 while (UI != UE) { 6120 SDNode *User = *UI; 6121 6122 // This node is about to morph, remove its old self from the CSE maps. 6123 RemoveNodeFromCSEMaps(User); 6124 6125 // A user can appear in a use list multiple times, and when this 6126 // happens the uses are usually next to each other in the list. 6127 // To help reduce the number of CSE recomputations, process all 6128 // the uses of this user that we can find this way. 6129 do { 6130 SDUse &Use = UI.getUse(); 6131 ++UI; 6132 Use.setNode(To); 6133 } while (UI != UE && *UI == User); 6134 6135 // Now that we have modified User, add it back to the CSE maps. If it 6136 // already exists there, recursively merge the results together. 6137 AddModifiedNodeToCSEMaps(User); 6138 } 6139 6140 // If we just RAUW'd the root, take note. 6141 if (From == getRoot().getNode()) 6142 setRoot(SDValue(To, getRoot().getResNo())); 6143 } 6144 6145 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 6146 /// This can cause recursive merging of nodes in the DAG. 6147 /// 6148 /// This version can replace From with any result values. To must match the 6149 /// number and types of values returned by From. 6150 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) { 6151 if (From->getNumValues() == 1) // Handle the simple case efficiently. 6152 return ReplaceAllUsesWith(SDValue(From, 0), To[0]); 6153 6154 // Iterate over just the existing users of From. See the comments in 6155 // the ReplaceAllUsesWith above. 6156 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); 6157 RAUWUpdateListener Listener(*this, UI, UE); 6158 while (UI != UE) { 6159 SDNode *User = *UI; 6160 6161 // This node is about to morph, remove its old self from the CSE maps. 6162 RemoveNodeFromCSEMaps(User); 6163 6164 // A user can appear in a use list multiple times, and when this 6165 // happens the uses are usually next to each other in the list. 6166 // To help reduce the number of CSE recomputations, process all 6167 // the uses of this user that we can find this way. 6168 do { 6169 SDUse &Use = UI.getUse(); 6170 const SDValue &ToOp = To[Use.getResNo()]; 6171 ++UI; 6172 Use.set(ToOp); 6173 } while (UI != UE && *UI == User); 6174 6175 // Now that we have modified User, add it back to the CSE maps. If it 6176 // already exists there, recursively merge the results together. 6177 AddModifiedNodeToCSEMaps(User); 6178 } 6179 6180 // If we just RAUW'd the root, take note. 6181 if (From == getRoot().getNode()) 6182 setRoot(SDValue(To[getRoot().getResNo()])); 6183 } 6184 6185 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving 6186 /// uses of other values produced by From.getNode() alone. The Deleted 6187 /// vector is handled the same way as for ReplaceAllUsesWith. 6188 void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){ 6189 // Handle the really simple, really trivial case efficiently. 6190 if (From == To) return; 6191 6192 // Handle the simple, trivial, case efficiently. 6193 if (From.getNode()->getNumValues() == 1) { 6194 ReplaceAllUsesWith(From, To); 6195 return; 6196 } 6197 6198 // Iterate over just the existing users of From. See the comments in 6199 // the ReplaceAllUsesWith above. 6200 SDNode::use_iterator UI = From.getNode()->use_begin(), 6201 UE = From.getNode()->use_end(); 6202 RAUWUpdateListener Listener(*this, UI, UE); 6203 while (UI != UE) { 6204 SDNode *User = *UI; 6205 bool UserRemovedFromCSEMaps = false; 6206 6207 // A user can appear in a use list multiple times, and when this 6208 // happens the uses are usually next to each other in the list. 6209 // To help reduce the number of CSE recomputations, process all 6210 // the uses of this user that we can find this way. 6211 do { 6212 SDUse &Use = UI.getUse(); 6213 6214 // Skip uses of different values from the same node. 6215 if (Use.getResNo() != From.getResNo()) { 6216 ++UI; 6217 continue; 6218 } 6219 6220 // If this node hasn't been modified yet, it's still in the CSE maps, 6221 // so remove its old self from the CSE maps. 6222 if (!UserRemovedFromCSEMaps) { 6223 RemoveNodeFromCSEMaps(User); 6224 UserRemovedFromCSEMaps = true; 6225 } 6226 6227 ++UI; 6228 Use.set(To); 6229 } while (UI != UE && *UI == User); 6230 6231 // We are iterating over all uses of the From node, so if a use 6232 // doesn't use the specific value, no changes are made. 6233 if (!UserRemovedFromCSEMaps) 6234 continue; 6235 6236 // Now that we have modified User, add it back to the CSE maps. If it 6237 // already exists there, recursively merge the results together. 6238 AddModifiedNodeToCSEMaps(User); 6239 } 6240 6241 // If we just RAUW'd the root, take note. 6242 if (From == getRoot()) 6243 setRoot(To); 6244 } 6245 6246 namespace { 6247 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith 6248 /// to record information about a use. 6249 struct UseMemo { 6250 SDNode *User; 6251 unsigned Index; 6252 SDUse *Use; 6253 }; 6254 6255 /// operator< - Sort Memos by User. 6256 bool operator<(const UseMemo &L, const UseMemo &R) { 6257 return (intptr_t)L.User < (intptr_t)R.User; 6258 } 6259 } 6260 6261 /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving 6262 /// uses of other values produced by From.getNode() alone. The same value 6263 /// may appear in both the From and To list. The Deleted vector is 6264 /// handled the same way as for ReplaceAllUsesWith. 6265 void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From, 6266 const SDValue *To, 6267 unsigned Num){ 6268 // Handle the simple, trivial case efficiently. 6269 if (Num == 1) 6270 return ReplaceAllUsesOfValueWith(*From, *To); 6271 6272 // Read up all the uses and make records of them. This helps 6273 // processing new uses that are introduced during the 6274 // replacement process. 6275 SmallVector<UseMemo, 4> Uses; 6276 for (unsigned i = 0; i != Num; ++i) { 6277 unsigned FromResNo = From[i].getResNo(); 6278 SDNode *FromNode = From[i].getNode(); 6279 for (SDNode::use_iterator UI = FromNode->use_begin(), 6280 E = FromNode->use_end(); UI != E; ++UI) { 6281 SDUse &Use = UI.getUse(); 6282 if (Use.getResNo() == FromResNo) { 6283 UseMemo Memo = { *UI, i, &Use }; 6284 Uses.push_back(Memo); 6285 } 6286 } 6287 } 6288 6289 // Sort the uses, so that all the uses from a given User are together. 6290 std::sort(Uses.begin(), Uses.end()); 6291 6292 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size(); 6293 UseIndex != UseIndexEnd; ) { 6294 // We know that this user uses some value of From. If it is the right 6295 // value, update it. 6296 SDNode *User = Uses[UseIndex].User; 6297 6298 // This node is about to morph, remove its old self from the CSE maps. 6299 RemoveNodeFromCSEMaps(User); 6300 6301 // The Uses array is sorted, so all the uses for a given User 6302 // are next to each other in the list. 6303 // To help reduce the number of CSE recomputations, process all 6304 // the uses of this user that we can find this way. 6305 do { 6306 unsigned i = Uses[UseIndex].Index; 6307 SDUse &Use = *Uses[UseIndex].Use; 6308 ++UseIndex; 6309 6310 Use.set(To[i]); 6311 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User); 6312 6313 // Now that we have modified User, add it back to the CSE maps. If it 6314 // already exists there, recursively merge the results together. 6315 AddModifiedNodeToCSEMaps(User); 6316 } 6317 } 6318 6319 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG 6320 /// based on their topological order. It returns the maximum id and a vector 6321 /// of the SDNodes* in assigned order by reference. 6322 unsigned SelectionDAG::AssignTopologicalOrder() { 6323 6324 unsigned DAGSize = 0; 6325 6326 // SortedPos tracks the progress of the algorithm. Nodes before it are 6327 // sorted, nodes after it are unsorted. When the algorithm completes 6328 // it is at the end of the list. 6329 allnodes_iterator SortedPos = allnodes_begin(); 6330 6331 // Visit all the nodes. Move nodes with no operands to the front of 6332 // the list immediately. Annotate nodes that do have operands with their 6333 // operand count. Before we do this, the Node Id fields of the nodes 6334 // may contain arbitrary values. After, the Node Id fields for nodes 6335 // before SortedPos will contain the topological sort index, and the 6336 // Node Id fields for nodes At SortedPos and after will contain the 6337 // count of outstanding operands. 6338 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) { 6339 SDNode *N = I++; 6340 checkForCycles(N, this); 6341 unsigned Degree = N->getNumOperands(); 6342 if (Degree == 0) { 6343 // A node with no uses, add it to the result array immediately. 6344 N->setNodeId(DAGSize++); 6345 allnodes_iterator Q = N; 6346 if (Q != SortedPos) 6347 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q)); 6348 assert(SortedPos != AllNodes.end() && "Overran node list"); 6349 ++SortedPos; 6350 } else { 6351 // Temporarily use the Node Id as scratch space for the degree count. 6352 N->setNodeId(Degree); 6353 } 6354 } 6355 6356 // Visit all the nodes. As we iterate, move nodes into sorted order, 6357 // such that by the time the end is reached all nodes will be sorted. 6358 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) { 6359 SDNode *N = I; 6360 checkForCycles(N, this); 6361 // N is in sorted position, so all its uses have one less operand 6362 // that needs to be sorted. 6363 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 6364 UI != UE; ++UI) { 6365 SDNode *P = *UI; 6366 unsigned Degree = P->getNodeId(); 6367 assert(Degree != 0 && "Invalid node degree"); 6368 --Degree; 6369 if (Degree == 0) { 6370 // All of P's operands are sorted, so P may sorted now. 6371 P->setNodeId(DAGSize++); 6372 if (P != SortedPos) 6373 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P)); 6374 assert(SortedPos != AllNodes.end() && "Overran node list"); 6375 ++SortedPos; 6376 } else { 6377 // Update P's outstanding operand count. 6378 P->setNodeId(Degree); 6379 } 6380 } 6381 if (I == SortedPos) { 6382 #ifndef NDEBUG 6383 SDNode *S = ++I; 6384 dbgs() << "Overran sorted position:\n"; 6385 S->dumprFull(this); dbgs() << "\n"; 6386 dbgs() << "Checking if this is due to cycles\n"; 6387 checkForCycles(this, true); 6388 #endif 6389 llvm_unreachable(nullptr); 6390 } 6391 } 6392 6393 assert(SortedPos == AllNodes.end() && 6394 "Topological sort incomplete!"); 6395 assert(AllNodes.front().getOpcode() == ISD::EntryToken && 6396 "First node in topological sort is not the entry token!"); 6397 assert(AllNodes.front().getNodeId() == 0 && 6398 "First node in topological sort has non-zero id!"); 6399 assert(AllNodes.front().getNumOperands() == 0 && 6400 "First node in topological sort has operands!"); 6401 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 && 6402 "Last node in topologic sort has unexpected id!"); 6403 assert(AllNodes.back().use_empty() && 6404 "Last node in topologic sort has users!"); 6405 assert(DAGSize == allnodes_size() && "Node count mismatch!"); 6406 return DAGSize; 6407 } 6408 6409 /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the 6410 /// value is produced by SD. 6411 void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) { 6412 if (SD) { 6413 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue()); 6414 SD->setHasDebugValue(true); 6415 } 6416 DbgInfo->add(DB, SD, isParameter); 6417 } 6418 6419 /// TransferDbgValues - Transfer SDDbgValues. 6420 void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) { 6421 if (From == To || !From.getNode()->getHasDebugValue()) 6422 return; 6423 SDNode *FromNode = From.getNode(); 6424 SDNode *ToNode = To.getNode(); 6425 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode); 6426 SmallVector<SDDbgValue *, 2> ClonedDVs; 6427 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end(); 6428 I != E; ++I) { 6429 SDDbgValue *Dbg = *I; 6430 if (Dbg->getKind() == SDDbgValue::SDNODE) { 6431 SDDbgValue *Clone = 6432 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode, 6433 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(), 6434 Dbg->getDebugLoc(), Dbg->getOrder()); 6435 ClonedDVs.push_back(Clone); 6436 } 6437 } 6438 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(), 6439 E = ClonedDVs.end(); I != E; ++I) 6440 AddDbgValue(*I, ToNode, false); 6441 } 6442 6443 //===----------------------------------------------------------------------===// 6444 // SDNode Class 6445 //===----------------------------------------------------------------------===// 6446 6447 HandleSDNode::~HandleSDNode() { 6448 DropOperands(); 6449 } 6450 6451 GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order, 6452 DebugLoc DL, const GlobalValue *GA, 6453 EVT VT, int64_t o, unsigned char TF) 6454 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) { 6455 TheGlobal = GA; 6456 } 6457 6458 AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, 6459 SDValue X, unsigned SrcAS, 6460 unsigned DestAS) 6461 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X), 6462 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {} 6463 6464 MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, 6465 EVT memvt, MachineMemOperand *mmo) 6466 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) { 6467 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), 6468 MMO->isNonTemporal(), MMO->isInvariant()); 6469 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); 6470 assert(isNonTemporal() == MMO->isNonTemporal() && 6471 "Non-temporal encoding error!"); 6472 // We check here that the size of the memory operand fits within the size of 6473 // the MMO. This is because the MMO might indicate only a possible address 6474 // range instead of specifying the affected memory addresses precisely. 6475 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!"); 6476 } 6477 6478 MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, 6479 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo) 6480 : SDNode(Opc, Order, dl, VTs, Ops), 6481 MemoryVT(memvt), MMO(mmo) { 6482 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), 6483 MMO->isNonTemporal(), MMO->isInvariant()); 6484 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); 6485 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!"); 6486 } 6487 6488 /// Profile - Gather unique data for the node. 6489 /// 6490 void SDNode::Profile(FoldingSetNodeID &ID) const { 6491 AddNodeIDNode(ID, this); 6492 } 6493 6494 namespace { 6495 struct EVTArray { 6496 std::vector<EVT> VTs; 6497 6498 EVTArray() { 6499 VTs.reserve(MVT::LAST_VALUETYPE); 6500 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i) 6501 VTs.push_back(MVT((MVT::SimpleValueType)i)); 6502 } 6503 }; 6504 } 6505 6506 static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs; 6507 static ManagedStatic<EVTArray> SimpleVTArray; 6508 static ManagedStatic<sys::SmartMutex<true> > VTMutex; 6509 6510 /// getValueTypeList - Return a pointer to the specified value type. 6511 /// 6512 const EVT *SDNode::getValueTypeList(EVT VT) { 6513 if (VT.isExtended()) { 6514 sys::SmartScopedLock<true> Lock(*VTMutex); 6515 return &(*EVTs->insert(VT).first); 6516 } else { 6517 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE && 6518 "Value type out of range!"); 6519 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy]; 6520 } 6521 } 6522 6523 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 6524 /// indicated value. This method ignores uses of other values defined by this 6525 /// operation. 6526 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { 6527 assert(Value < getNumValues() && "Bad value!"); 6528 6529 // TODO: Only iterate over uses of a given value of the node 6530 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) { 6531 if (UI.getUse().getResNo() == Value) { 6532 if (NUses == 0) 6533 return false; 6534 --NUses; 6535 } 6536 } 6537 6538 // Found exactly the right number of uses? 6539 return NUses == 0; 6540 } 6541 6542 6543 /// hasAnyUseOfValue - Return true if there are any use of the indicated 6544 /// value. This method ignores uses of other values defined by this operation. 6545 bool SDNode::hasAnyUseOfValue(unsigned Value) const { 6546 assert(Value < getNumValues() && "Bad value!"); 6547 6548 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) 6549 if (UI.getUse().getResNo() == Value) 6550 return true; 6551 6552 return false; 6553 } 6554 6555 6556 /// isOnlyUserOf - Return true if this node is the only use of N. 6557 /// 6558 bool SDNode::isOnlyUserOf(SDNode *N) const { 6559 bool Seen = false; 6560 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 6561 SDNode *User = *I; 6562 if (User == this) 6563 Seen = true; 6564 else 6565 return false; 6566 } 6567 6568 return Seen; 6569 } 6570 6571 /// isOperand - Return true if this node is an operand of N. 6572 /// 6573 bool SDValue::isOperandOf(SDNode *N) const { 6574 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 6575 if (*this == N->getOperand(i)) 6576 return true; 6577 return false; 6578 } 6579 6580 bool SDNode::isOperandOf(SDNode *N) const { 6581 for (unsigned i = 0, e = N->NumOperands; i != e; ++i) 6582 if (this == N->OperandList[i].getNode()) 6583 return true; 6584 return false; 6585 } 6586 6587 /// reachesChainWithoutSideEffects - Return true if this operand (which must 6588 /// be a chain) reaches the specified operand without crossing any 6589 /// side-effecting instructions on any chain path. In practice, this looks 6590 /// through token factors and non-volatile loads. In order to remain efficient, 6591 /// this only looks a couple of nodes in, it does not do an exhaustive search. 6592 bool SDValue::reachesChainWithoutSideEffects(SDValue Dest, 6593 unsigned Depth) const { 6594 if (*this == Dest) return true; 6595 6596 // Don't search too deeply, we just want to be able to see through 6597 // TokenFactor's etc. 6598 if (Depth == 0) return false; 6599 6600 // If this is a token factor, all inputs to the TF happen in parallel. If any 6601 // of the operands of the TF does not reach dest, then we cannot do the xform. 6602 if (getOpcode() == ISD::TokenFactor) { 6603 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 6604 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1)) 6605 return false; 6606 return true; 6607 } 6608 6609 // Loads don't have side effects, look through them. 6610 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) { 6611 if (!Ld->isVolatile()) 6612 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1); 6613 } 6614 return false; 6615 } 6616 6617 /// hasPredecessor - Return true if N is a predecessor of this node. 6618 /// N is either an operand of this node, or can be reached by recursively 6619 /// traversing up the operands. 6620 /// NOTE: This is an expensive method. Use it carefully. 6621 bool SDNode::hasPredecessor(const SDNode *N) const { 6622 SmallPtrSet<const SDNode *, 32> Visited; 6623 SmallVector<const SDNode *, 16> Worklist; 6624 return hasPredecessorHelper(N, Visited, Worklist); 6625 } 6626 6627 bool 6628 SDNode::hasPredecessorHelper(const SDNode *N, 6629 SmallPtrSetImpl<const SDNode *> &Visited, 6630 SmallVectorImpl<const SDNode *> &Worklist) const { 6631 if (Visited.empty()) { 6632 Worklist.push_back(this); 6633 } else { 6634 // Take a look in the visited set. If we've already encountered this node 6635 // we needn't search further. 6636 if (Visited.count(N)) 6637 return true; 6638 } 6639 6640 // Haven't visited N yet. Continue the search. 6641 while (!Worklist.empty()) { 6642 const SDNode *M = Worklist.pop_back_val(); 6643 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { 6644 SDNode *Op = M->getOperand(i).getNode(); 6645 if (Visited.insert(Op).second) 6646 Worklist.push_back(Op); 6647 if (Op == N) 6648 return true; 6649 } 6650 } 6651 6652 return false; 6653 } 6654 6655 uint64_t SDNode::getConstantOperandVal(unsigned Num) const { 6656 assert(Num < NumOperands && "Invalid child # of SDNode!"); 6657 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue(); 6658 } 6659 6660 SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) { 6661 assert(N->getNumValues() == 1 && 6662 "Can't unroll a vector with multiple results!"); 6663 6664 EVT VT = N->getValueType(0); 6665 unsigned NE = VT.getVectorNumElements(); 6666 EVT EltVT = VT.getVectorElementType(); 6667 SDLoc dl(N); 6668 6669 SmallVector<SDValue, 8> Scalars; 6670 SmallVector<SDValue, 4> Operands(N->getNumOperands()); 6671 6672 // If ResNE is 0, fully unroll the vector op. 6673 if (ResNE == 0) 6674 ResNE = NE; 6675 else if (NE > ResNE) 6676 NE = ResNE; 6677 6678 unsigned i; 6679 for (i= 0; i != NE; ++i) { 6680 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) { 6681 SDValue Operand = N->getOperand(j); 6682 EVT OperandVT = Operand.getValueType(); 6683 if (OperandVT.isVector()) { 6684 // A vector operand; extract a single element. 6685 EVT OperandEltVT = OperandVT.getVectorElementType(); 6686 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, 6687 OperandEltVT, 6688 Operand, 6689 getConstant(i, dl, TLI->getVectorIdxTy())); 6690 } else { 6691 // A scalar operand; just use it as is. 6692 Operands[j] = Operand; 6693 } 6694 } 6695 6696 switch (N->getOpcode()) { 6697 default: 6698 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands)); 6699 break; 6700 case ISD::VSELECT: 6701 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands)); 6702 break; 6703 case ISD::SHL: 6704 case ISD::SRA: 6705 case ISD::SRL: 6706 case ISD::ROTL: 6707 case ISD::ROTR: 6708 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0], 6709 getShiftAmountOperand(Operands[0].getValueType(), 6710 Operands[1]))); 6711 break; 6712 case ISD::SIGN_EXTEND_INREG: 6713 case ISD::FP_ROUND_INREG: { 6714 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType(); 6715 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, 6716 Operands[0], 6717 getValueType(ExtVT))); 6718 } 6719 } 6720 } 6721 6722 for (; i < ResNE; ++i) 6723 Scalars.push_back(getUNDEF(EltVT)); 6724 6725 return getNode(ISD::BUILD_VECTOR, dl, 6726 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars); 6727 } 6728 6729 6730 /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a 6731 /// location that is 'Dist' units away from the location that the 'Base' load 6732 /// is loading from. 6733 bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base, 6734 unsigned Bytes, int Dist) const { 6735 if (LD->getChain() != Base->getChain()) 6736 return false; 6737 EVT VT = LD->getValueType(0); 6738 if (VT.getSizeInBits() / 8 != Bytes) 6739 return false; 6740 6741 SDValue Loc = LD->getOperand(1); 6742 SDValue BaseLoc = Base->getOperand(1); 6743 if (Loc.getOpcode() == ISD::FrameIndex) { 6744 if (BaseLoc.getOpcode() != ISD::FrameIndex) 6745 return false; 6746 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo(); 6747 int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); 6748 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 6749 int FS = MFI->getObjectSize(FI); 6750 int BFS = MFI->getObjectSize(BFI); 6751 if (FS != BFS || FS != (int)Bytes) return false; 6752 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); 6753 } 6754 6755 // Handle X + C. 6756 if (isBaseWithConstantOffset(Loc)) { 6757 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue(); 6758 if (Loc.getOperand(0) == BaseLoc) { 6759 // If the base location is a simple address with no offset itself, then 6760 // the second load's first add operand should be the base address. 6761 if (LocOffset == Dist * (int)Bytes) 6762 return true; 6763 } else if (isBaseWithConstantOffset(BaseLoc)) { 6764 // The base location itself has an offset, so subtract that value from the 6765 // second load's offset before comparing to distance * size. 6766 int64_t BOffset = 6767 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue(); 6768 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) { 6769 if ((LocOffset - BOffset) == Dist * (int)Bytes) 6770 return true; 6771 } 6772 } 6773 } 6774 const GlobalValue *GV1 = nullptr; 6775 const GlobalValue *GV2 = nullptr; 6776 int64_t Offset1 = 0; 6777 int64_t Offset2 = 0; 6778 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1); 6779 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); 6780 if (isGA1 && isGA2 && GV1 == GV2) 6781 return Offset1 == (Offset2 + Dist*Bytes); 6782 return false; 6783 } 6784 6785 6786 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if 6787 /// it cannot be inferred. 6788 unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const { 6789 // If this is a GlobalAddress + cst, return the alignment. 6790 const GlobalValue *GV; 6791 int64_t GVOffset = 0; 6792 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) { 6793 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType()); 6794 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0); 6795 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne, 6796 *TLI->getDataLayout()); 6797 unsigned AlignBits = KnownZero.countTrailingOnes(); 6798 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0; 6799 if (Align) 6800 return MinAlign(Align, GVOffset); 6801 } 6802 6803 // If this is a direct reference to a stack slot, use information about the 6804 // stack slot's alignment. 6805 int FrameIdx = 1 << 31; 6806 int64_t FrameOffset = 0; 6807 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) { 6808 FrameIdx = FI->getIndex(); 6809 } else if (isBaseWithConstantOffset(Ptr) && 6810 isa<FrameIndexSDNode>(Ptr.getOperand(0))) { 6811 // Handle FI+Cst 6812 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); 6813 FrameOffset = Ptr.getConstantOperandVal(1); 6814 } 6815 6816 if (FrameIdx != (1 << 31)) { 6817 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo(); 6818 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx), 6819 FrameOffset); 6820 return FIInfoAlign; 6821 } 6822 6823 return 0; 6824 } 6825 6826 /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type 6827 /// which is split (or expanded) into two not necessarily identical pieces. 6828 std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const { 6829 // Currently all types are split in half. 6830 EVT LoVT, HiVT; 6831 if (!VT.isVector()) { 6832 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT); 6833 } else { 6834 unsigned NumElements = VT.getVectorNumElements(); 6835 assert(!(NumElements & 1) && "Splitting vector, but not in half!"); 6836 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(), 6837 NumElements/2); 6838 } 6839 return std::make_pair(LoVT, HiVT); 6840 } 6841 6842 /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the 6843 /// low/high part. 6844 std::pair<SDValue, SDValue> 6845 SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, 6846 const EVT &HiVT) { 6847 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <= 6848 N.getValueType().getVectorNumElements() && 6849 "More vector elements requested than available!"); 6850 SDValue Lo, Hi; 6851 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N, 6852 getConstant(0, DL, TLI->getVectorIdxTy())); 6853 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N, 6854 getConstant(LoVT.getVectorNumElements(), DL, 6855 TLI->getVectorIdxTy())); 6856 return std::make_pair(Lo, Hi); 6857 } 6858 6859 void SelectionDAG::ExtractVectorElements(SDValue Op, 6860 SmallVectorImpl<SDValue> &Args, 6861 unsigned Start, unsigned Count) { 6862 EVT VT = Op.getValueType(); 6863 if (Count == 0) 6864 Count = VT.getVectorNumElements(); 6865 6866 EVT EltVT = VT.getVectorElementType(); 6867 EVT IdxTy = TLI->getVectorIdxTy(); 6868 SDLoc SL(Op); 6869 for (unsigned i = Start, e = Start + Count; i != e; ++i) { 6870 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 6871 Op, getConstant(i, SL, IdxTy))); 6872 } 6873 } 6874 6875 // getAddressSpace - Return the address space this GlobalAddress belongs to. 6876 unsigned GlobalAddressSDNode::getAddressSpace() const { 6877 return getGlobal()->getType()->getAddressSpace(); 6878 } 6879 6880 6881 Type *ConstantPoolSDNode::getType() const { 6882 if (isMachineConstantPoolEntry()) 6883 return Val.MachineCPVal->getType(); 6884 return Val.ConstVal->getType(); 6885 } 6886 6887 bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, 6888 APInt &SplatUndef, 6889 unsigned &SplatBitSize, 6890 bool &HasAnyUndefs, 6891 unsigned MinSplatBits, 6892 bool isBigEndian) const { 6893 EVT VT = getValueType(0); 6894 assert(VT.isVector() && "Expected a vector type"); 6895 unsigned sz = VT.getSizeInBits(); 6896 if (MinSplatBits > sz) 6897 return false; 6898 6899 SplatValue = APInt(sz, 0); 6900 SplatUndef = APInt(sz, 0); 6901 6902 // Get the bits. Bits with undefined values (when the corresponding element 6903 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared 6904 // in SplatValue. If any of the values are not constant, give up and return 6905 // false. 6906 unsigned int nOps = getNumOperands(); 6907 assert(nOps > 0 && "isConstantSplat has 0-size build vector"); 6908 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits(); 6909 6910 for (unsigned j = 0; j < nOps; ++j) { 6911 unsigned i = isBigEndian ? nOps-1-j : j; 6912 SDValue OpVal = getOperand(i); 6913 unsigned BitPos = j * EltBitSize; 6914 6915 if (OpVal.getOpcode() == ISD::UNDEF) 6916 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize); 6917 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) 6918 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize). 6919 zextOrTrunc(sz) << BitPos; 6920 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) 6921 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos; 6922 else 6923 return false; 6924 } 6925 6926 // The build_vector is all constants or undefs. Find the smallest element 6927 // size that splats the vector. 6928 6929 HasAnyUndefs = (SplatUndef != 0); 6930 while (sz > 8) { 6931 6932 unsigned HalfSize = sz / 2; 6933 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize); 6934 APInt LowValue = SplatValue.trunc(HalfSize); 6935 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize); 6936 APInt LowUndef = SplatUndef.trunc(HalfSize); 6937 6938 // If the two halves do not match (ignoring undef bits), stop here. 6939 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) || 6940 MinSplatBits > HalfSize) 6941 break; 6942 6943 SplatValue = HighValue | LowValue; 6944 SplatUndef = HighUndef & LowUndef; 6945 6946 sz = HalfSize; 6947 } 6948 6949 SplatBitSize = sz; 6950 return true; 6951 } 6952 6953 SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const { 6954 if (UndefElements) { 6955 UndefElements->clear(); 6956 UndefElements->resize(getNumOperands()); 6957 } 6958 SDValue Splatted; 6959 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 6960 SDValue Op = getOperand(i); 6961 if (Op.getOpcode() == ISD::UNDEF) { 6962 if (UndefElements) 6963 (*UndefElements)[i] = true; 6964 } else if (!Splatted) { 6965 Splatted = Op; 6966 } else if (Splatted != Op) { 6967 return SDValue(); 6968 } 6969 } 6970 6971 if (!Splatted) { 6972 assert(getOperand(0).getOpcode() == ISD::UNDEF && 6973 "Can only have a splat without a constant for all undefs."); 6974 return getOperand(0); 6975 } 6976 6977 return Splatted; 6978 } 6979 6980 ConstantSDNode * 6981 BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const { 6982 return dyn_cast_or_null<ConstantSDNode>( 6983 getSplatValue(UndefElements).getNode()); 6984 } 6985 6986 ConstantFPSDNode * 6987 BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const { 6988 return dyn_cast_or_null<ConstantFPSDNode>( 6989 getSplatValue(UndefElements).getNode()); 6990 } 6991 6992 bool BuildVectorSDNode::isConstant() const { 6993 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 6994 unsigned Opc = getOperand(i).getOpcode(); 6995 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP) 6996 return false; 6997 } 6998 return true; 6999 } 7000 7001 bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) { 7002 // Find the first non-undef value in the shuffle mask. 7003 unsigned i, e; 7004 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i) 7005 /* search */; 7006 7007 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!"); 7008 7009 // Make sure all remaining elements are either undef or the same as the first 7010 // non-undef value. 7011 for (int Idx = Mask[i]; i != e; ++i) 7012 if (Mask[i] >= 0 && Mask[i] != Idx) 7013 return false; 7014 return true; 7015 } 7016 7017 #ifndef NDEBUG 7018 static void checkForCyclesHelper(const SDNode *N, 7019 SmallPtrSetImpl<const SDNode*> &Visited, 7020 SmallPtrSetImpl<const SDNode*> &Checked, 7021 const llvm::SelectionDAG *DAG) { 7022 // If this node has already been checked, don't check it again. 7023 if (Checked.count(N)) 7024 return; 7025 7026 // If a node has already been visited on this depth-first walk, reject it as 7027 // a cycle. 7028 if (!Visited.insert(N).second) { 7029 errs() << "Detected cycle in SelectionDAG\n"; 7030 dbgs() << "Offending node:\n"; 7031 N->dumprFull(DAG); dbgs() << "\n"; 7032 abort(); 7033 } 7034 7035 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 7036 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG); 7037 7038 Checked.insert(N); 7039 Visited.erase(N); 7040 } 7041 #endif 7042 7043 void llvm::checkForCycles(const llvm::SDNode *N, 7044 const llvm::SelectionDAG *DAG, 7045 bool force) { 7046 #ifndef NDEBUG 7047 bool check = force; 7048 #ifdef XDEBUG 7049 check = true; 7050 #endif // XDEBUG 7051 if (check) { 7052 assert(N && "Checking nonexistent SDNode"); 7053 SmallPtrSet<const SDNode*, 32> visited; 7054 SmallPtrSet<const SDNode*, 32> checked; 7055 checkForCyclesHelper(N, visited, checked, DAG); 7056 } 7057 #endif // !NDEBUG 7058 } 7059 7060 void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) { 7061 checkForCycles(DAG->getRoot().getNode(), DAG, force); 7062 } 7063