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