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