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