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