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