1 //===- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file declares the SelectionDAG class, and transitively defines the 10 // SDNode class and subclasses. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_SELECTIONDAG_H 15 #define LLVM_CODEGEN_SELECTIONDAG_H 16 17 #include "llvm/ADT/APFloat.h" 18 #include "llvm/ADT/APInt.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/DenseSet.h" 22 #include "llvm/ADT/FoldingSet.h" 23 #include "llvm/ADT/SetVector.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/ADT/StringMap.h" 26 #include "llvm/ADT/ilist.h" 27 #include "llvm/ADT/iterator.h" 28 #include "llvm/ADT/iterator_range.h" 29 #include "llvm/CodeGen/DAGCombine.h" 30 #include "llvm/CodeGen/ISDOpcodes.h" 31 #include "llvm/CodeGen/MachineFunction.h" 32 #include "llvm/CodeGen/MachineMemOperand.h" 33 #include "llvm/CodeGen/SelectionDAGNodes.h" 34 #include "llvm/CodeGen/ValueTypes.h" 35 #include "llvm/IR/DebugLoc.h" 36 #include "llvm/IR/Instructions.h" 37 #include "llvm/IR/Metadata.h" 38 #include "llvm/Support/Allocator.h" 39 #include "llvm/Support/ArrayRecycler.h" 40 #include "llvm/Support/AtomicOrdering.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/CodeGen.h" 43 #include "llvm/Support/ErrorHandling.h" 44 #include "llvm/Support/MachineValueType.h" 45 #include "llvm/Support/RecyclingAllocator.h" 46 #include <algorithm> 47 #include <cassert> 48 #include <cstdint> 49 #include <functional> 50 #include <map> 51 #include <string> 52 #include <tuple> 53 #include <utility> 54 #include <vector> 55 56 namespace llvm { 57 58 class AAResults; 59 class BlockAddress; 60 class BlockFrequencyInfo; 61 class Constant; 62 class ConstantFP; 63 class ConstantInt; 64 class DataLayout; 65 struct fltSemantics; 66 class FunctionLoweringInfo; 67 class GlobalValue; 68 struct KnownBits; 69 class LegacyDivergenceAnalysis; 70 class LLVMContext; 71 class MachineBasicBlock; 72 class MachineConstantPoolValue; 73 class MCSymbol; 74 class OptimizationRemarkEmitter; 75 class ProfileSummaryInfo; 76 class SDDbgValue; 77 class SDDbgOperand; 78 class SDDbgLabel; 79 class SelectionDAG; 80 class SelectionDAGTargetInfo; 81 class TargetLibraryInfo; 82 class TargetLowering; 83 class TargetMachine; 84 class TargetSubtargetInfo; 85 class Value; 86 87 class SDVTListNode : public FoldingSetNode { 88 friend struct FoldingSetTrait<SDVTListNode>; 89 90 /// A reference to an Interned FoldingSetNodeID for this node. 91 /// The Allocator in SelectionDAG holds the data. 92 /// SDVTList contains all types which are frequently accessed in SelectionDAG. 93 /// The size of this list is not expected to be big so it won't introduce 94 /// a memory penalty. 95 FoldingSetNodeIDRef FastID; 96 const EVT *VTs; 97 unsigned int NumVTs; 98 /// The hash value for SDVTList is fixed, so cache it to avoid 99 /// hash calculation. 100 unsigned HashValue; 101 102 public: 103 SDVTListNode(const FoldingSetNodeIDRef ID, const EVT *VT, unsigned int Num) : 104 FastID(ID), VTs(VT), NumVTs(Num) { 105 HashValue = ID.ComputeHash(); 106 } 107 108 SDVTList getSDVTList() { 109 SDVTList result = {VTs, NumVTs}; 110 return result; 111 } 112 }; 113 114 /// Specialize FoldingSetTrait for SDVTListNode 115 /// to avoid computing temp FoldingSetNodeID and hash value. 116 template<> struct FoldingSetTrait<SDVTListNode> : DefaultFoldingSetTrait<SDVTListNode> { 117 static void Profile(const SDVTListNode &X, FoldingSetNodeID& ID) { 118 ID = X.FastID; 119 } 120 121 static bool Equals(const SDVTListNode &X, const FoldingSetNodeID &ID, 122 unsigned IDHash, FoldingSetNodeID &TempID) { 123 if (X.HashValue != IDHash) 124 return false; 125 return ID == X.FastID; 126 } 127 128 static unsigned ComputeHash(const SDVTListNode &X, FoldingSetNodeID &TempID) { 129 return X.HashValue; 130 } 131 }; 132 133 template <> struct ilist_alloc_traits<SDNode> { 134 static void deleteNode(SDNode *) { 135 llvm_unreachable("ilist_traits<SDNode> shouldn't see a deleteNode call!"); 136 } 137 }; 138 139 /// Keeps track of dbg_value information through SDISel. We do 140 /// not build SDNodes for these so as not to perturb the generated code; 141 /// instead the info is kept off to the side in this structure. Each SDNode may 142 /// have one or more associated dbg_value entries. This information is kept in 143 /// DbgValMap. 144 /// Byval parameters are handled separately because they don't use alloca's, 145 /// which busts the normal mechanism. There is good reason for handling all 146 /// parameters separately: they may not have code generated for them, they 147 /// should always go at the beginning of the function regardless of other code 148 /// motion, and debug info for them is potentially useful even if the parameter 149 /// is unused. Right now only byval parameters are handled separately. 150 class SDDbgInfo { 151 BumpPtrAllocator Alloc; 152 SmallVector<SDDbgValue*, 32> DbgValues; 153 SmallVector<SDDbgValue*, 32> ByvalParmDbgValues; 154 SmallVector<SDDbgLabel*, 4> DbgLabels; 155 using DbgValMapType = DenseMap<const SDNode *, SmallVector<SDDbgValue *, 2>>; 156 DbgValMapType DbgValMap; 157 158 public: 159 SDDbgInfo() = default; 160 SDDbgInfo(const SDDbgInfo &) = delete; 161 SDDbgInfo &operator=(const SDDbgInfo &) = delete; 162 163 void add(SDDbgValue *V, bool isParameter); 164 165 void add(SDDbgLabel *L) { DbgLabels.push_back(L); } 166 167 /// Invalidate all DbgValues attached to the node and remove 168 /// it from the Node-to-DbgValues map. 169 void erase(const SDNode *Node); 170 171 void clear() { 172 DbgValMap.clear(); 173 DbgValues.clear(); 174 ByvalParmDbgValues.clear(); 175 DbgLabels.clear(); 176 Alloc.Reset(); 177 } 178 179 BumpPtrAllocator &getAlloc() { return Alloc; } 180 181 bool empty() const { 182 return DbgValues.empty() && ByvalParmDbgValues.empty() && DbgLabels.empty(); 183 } 184 185 ArrayRef<SDDbgValue*> getSDDbgValues(const SDNode *Node) const { 186 auto I = DbgValMap.find(Node); 187 if (I != DbgValMap.end()) 188 return I->second; 189 return ArrayRef<SDDbgValue*>(); 190 } 191 192 using DbgIterator = SmallVectorImpl<SDDbgValue*>::iterator; 193 using DbgLabelIterator = SmallVectorImpl<SDDbgLabel*>::iterator; 194 195 DbgIterator DbgBegin() { return DbgValues.begin(); } 196 DbgIterator DbgEnd() { return DbgValues.end(); } 197 DbgIterator ByvalParmDbgBegin() { return ByvalParmDbgValues.begin(); } 198 DbgIterator ByvalParmDbgEnd() { return ByvalParmDbgValues.end(); } 199 DbgLabelIterator DbgLabelBegin() { return DbgLabels.begin(); } 200 DbgLabelIterator DbgLabelEnd() { return DbgLabels.end(); } 201 }; 202 203 void checkForCycles(const SelectionDAG *DAG, bool force = false); 204 205 /// This is used to represent a portion of an LLVM function in a low-level 206 /// Data Dependence DAG representation suitable for instruction selection. 207 /// This DAG is constructed as the first step of instruction selection in order 208 /// to allow implementation of machine specific optimizations 209 /// and code simplifications. 210 /// 211 /// The representation used by the SelectionDAG is a target-independent 212 /// representation, which has some similarities to the GCC RTL representation, 213 /// but is significantly more simple, powerful, and is a graph form instead of a 214 /// linear form. 215 /// 216 class SelectionDAG { 217 const TargetMachine &TM; 218 const SelectionDAGTargetInfo *TSI = nullptr; 219 const TargetLowering *TLI = nullptr; 220 const TargetLibraryInfo *LibInfo = nullptr; 221 MachineFunction *MF; 222 Pass *SDAGISelPass = nullptr; 223 LLVMContext *Context; 224 CodeGenOpt::Level OptLevel; 225 226 LegacyDivergenceAnalysis * DA = nullptr; 227 FunctionLoweringInfo * FLI = nullptr; 228 229 /// The function-level optimization remark emitter. Used to emit remarks 230 /// whenever manipulating the DAG. 231 OptimizationRemarkEmitter *ORE; 232 233 ProfileSummaryInfo *PSI = nullptr; 234 BlockFrequencyInfo *BFI = nullptr; 235 236 /// The starting token. 237 SDNode EntryNode; 238 239 /// The root of the entire DAG. 240 SDValue Root; 241 242 /// A linked list of nodes in the current DAG. 243 ilist<SDNode> AllNodes; 244 245 /// The AllocatorType for allocating SDNodes. We use 246 /// pool allocation with recycling. 247 using NodeAllocatorType = RecyclingAllocator<BumpPtrAllocator, SDNode, 248 sizeof(LargestSDNode), 249 alignof(MostAlignedSDNode)>; 250 251 /// Pool allocation for nodes. 252 NodeAllocatorType NodeAllocator; 253 254 /// This structure is used to memoize nodes, automatically performing 255 /// CSE with existing nodes when a duplicate is requested. 256 FoldingSet<SDNode> CSEMap; 257 258 /// Pool allocation for machine-opcode SDNode operands. 259 BumpPtrAllocator OperandAllocator; 260 ArrayRecycler<SDUse> OperandRecycler; 261 262 /// Pool allocation for misc. objects that are created once per SelectionDAG. 263 BumpPtrAllocator Allocator; 264 265 /// Tracks dbg_value and dbg_label information through SDISel. 266 SDDbgInfo *DbgInfo; 267 268 using CallSiteInfo = MachineFunction::CallSiteInfo; 269 using CallSiteInfoImpl = MachineFunction::CallSiteInfoImpl; 270 271 struct CallSiteDbgInfo { 272 CallSiteInfo CSInfo; 273 MDNode *HeapAllocSite = nullptr; 274 bool NoMerge = false; 275 }; 276 277 DenseMap<const SDNode *, CallSiteDbgInfo> SDCallSiteDbgInfo; 278 279 uint16_t NextPersistentId = 0; 280 281 public: 282 /// Clients of various APIs that cause global effects on 283 /// the DAG can optionally implement this interface. This allows the clients 284 /// to handle the various sorts of updates that happen. 285 /// 286 /// A DAGUpdateListener automatically registers itself with DAG when it is 287 /// constructed, and removes itself when destroyed in RAII fashion. 288 struct DAGUpdateListener { 289 DAGUpdateListener *const Next; 290 SelectionDAG &DAG; 291 292 explicit DAGUpdateListener(SelectionDAG &D) 293 : Next(D.UpdateListeners), DAG(D) { 294 DAG.UpdateListeners = this; 295 } 296 297 virtual ~DAGUpdateListener() { 298 assert(DAG.UpdateListeners == this && 299 "DAGUpdateListeners must be destroyed in LIFO order"); 300 DAG.UpdateListeners = Next; 301 } 302 303 /// The node N that was deleted and, if E is not null, an 304 /// equivalent node E that replaced it. 305 virtual void NodeDeleted(SDNode *N, SDNode *E); 306 307 /// The node N that was updated. 308 virtual void NodeUpdated(SDNode *N); 309 310 /// The node N that was inserted. 311 virtual void NodeInserted(SDNode *N); 312 }; 313 314 struct DAGNodeDeletedListener : public DAGUpdateListener { 315 std::function<void(SDNode *, SDNode *)> Callback; 316 317 DAGNodeDeletedListener(SelectionDAG &DAG, 318 std::function<void(SDNode *, SDNode *)> Callback) 319 : DAGUpdateListener(DAG), Callback(std::move(Callback)) {} 320 321 void NodeDeleted(SDNode *N, SDNode *E) override { Callback(N, E); } 322 323 private: 324 virtual void anchor(); 325 }; 326 327 /// Help to insert SDNodeFlags automatically in transforming. Use 328 /// RAII to save and resume flags in current scope. 329 class FlagInserter { 330 SelectionDAG &DAG; 331 SDNodeFlags Flags; 332 FlagInserter *LastInserter; 333 334 public: 335 FlagInserter(SelectionDAG &SDAG, SDNodeFlags Flags) 336 : DAG(SDAG), Flags(Flags), 337 LastInserter(SDAG.getFlagInserter()) { 338 SDAG.setFlagInserter(this); 339 } 340 FlagInserter(SelectionDAG &SDAG, SDNode *N) 341 : FlagInserter(SDAG, N->getFlags()) {} 342 343 FlagInserter(const FlagInserter &) = delete; 344 FlagInserter &operator=(const FlagInserter &) = delete; 345 ~FlagInserter() { DAG.setFlagInserter(LastInserter); } 346 347 SDNodeFlags getFlags() const { return Flags; } 348 }; 349 350 /// When true, additional steps are taken to 351 /// ensure that getConstant() and similar functions return DAG nodes that 352 /// have legal types. This is important after type legalization since 353 /// any illegally typed nodes generated after this point will not experience 354 /// type legalization. 355 bool NewNodesMustHaveLegalTypes = false; 356 357 private: 358 /// DAGUpdateListener is a friend so it can manipulate the listener stack. 359 friend struct DAGUpdateListener; 360 361 /// Linked list of registered DAGUpdateListener instances. 362 /// This stack is maintained by DAGUpdateListener RAII. 363 DAGUpdateListener *UpdateListeners = nullptr; 364 365 /// Implementation of setSubgraphColor. 366 /// Return whether we had to truncate the search. 367 bool setSubgraphColorHelper(SDNode *N, const char *Color, 368 DenseSet<SDNode *> &visited, 369 int level, bool &printed); 370 371 template <typename SDNodeT, typename... ArgTypes> 372 SDNodeT *newSDNode(ArgTypes &&... Args) { 373 return new (NodeAllocator.template Allocate<SDNodeT>()) 374 SDNodeT(std::forward<ArgTypes>(Args)...); 375 } 376 377 /// Build a synthetic SDNodeT with the given args and extract its subclass 378 /// data as an integer (e.g. for use in a folding set). 379 /// 380 /// The args to this function are the same as the args to SDNodeT's 381 /// constructor, except the second arg (assumed to be a const DebugLoc&) is 382 /// omitted. 383 template <typename SDNodeT, typename... ArgTypes> 384 static uint16_t getSyntheticNodeSubclassData(unsigned IROrder, 385 ArgTypes &&... Args) { 386 // The compiler can reduce this expression to a constant iff we pass an 387 // empty DebugLoc. Thankfully, the debug location doesn't have any bearing 388 // on the subclass data. 389 return SDNodeT(IROrder, DebugLoc(), std::forward<ArgTypes>(Args)...) 390 .getRawSubclassData(); 391 } 392 393 template <typename SDNodeTy> 394 static uint16_t getSyntheticNodeSubclassData(unsigned Opc, unsigned Order, 395 SDVTList VTs, EVT MemoryVT, 396 MachineMemOperand *MMO) { 397 return SDNodeTy(Opc, Order, DebugLoc(), VTs, MemoryVT, MMO) 398 .getRawSubclassData(); 399 } 400 401 void createOperands(SDNode *Node, ArrayRef<SDValue> Vals); 402 403 void removeOperands(SDNode *Node) { 404 if (!Node->OperandList) 405 return; 406 OperandRecycler.deallocate( 407 ArrayRecycler<SDUse>::Capacity::get(Node->NumOperands), 408 Node->OperandList); 409 Node->NumOperands = 0; 410 Node->OperandList = nullptr; 411 } 412 void CreateTopologicalOrder(std::vector<SDNode*>& Order); 413 414 public: 415 // Maximum depth for recursive analysis such as computeKnownBits, etc. 416 static constexpr unsigned MaxRecursionDepth = 6; 417 418 explicit SelectionDAG(const TargetMachine &TM, CodeGenOpt::Level); 419 SelectionDAG(const SelectionDAG &) = delete; 420 SelectionDAG &operator=(const SelectionDAG &) = delete; 421 ~SelectionDAG(); 422 423 /// Prepare this SelectionDAG to process code in the given MachineFunction. 424 void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, 425 Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, 426 LegacyDivergenceAnalysis * Divergence, 427 ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin); 428 429 void setFunctionLoweringInfo(FunctionLoweringInfo * FuncInfo) { 430 FLI = FuncInfo; 431 } 432 433 /// Clear state and free memory necessary to make this 434 /// SelectionDAG ready to process a new block. 435 void clear(); 436 437 MachineFunction &getMachineFunction() const { return *MF; } 438 const Pass *getPass() const { return SDAGISelPass; } 439 440 const DataLayout &getDataLayout() const { return MF->getDataLayout(); } 441 const TargetMachine &getTarget() const { return TM; } 442 const TargetSubtargetInfo &getSubtarget() const { return MF->getSubtarget(); } 443 const TargetLowering &getTargetLoweringInfo() const { return *TLI; } 444 const TargetLibraryInfo &getLibInfo() const { return *LibInfo; } 445 const SelectionDAGTargetInfo &getSelectionDAGInfo() const { return *TSI; } 446 const LegacyDivergenceAnalysis *getDivergenceAnalysis() const { return DA; } 447 LLVMContext *getContext() const { return Context; } 448 OptimizationRemarkEmitter &getORE() const { return *ORE; } 449 ProfileSummaryInfo *getPSI() const { return PSI; } 450 BlockFrequencyInfo *getBFI() const { return BFI; } 451 452 FlagInserter *getFlagInserter() { return Inserter; } 453 void setFlagInserter(FlagInserter *FI) { Inserter = FI; } 454 455 /// Just dump dot graph to a user-provided path and title. 456 /// This doesn't open the dot viewer program and 457 /// helps visualization when outside debugging session. 458 /// FileName expects absolute path. If provided 459 /// without any path separators then the file 460 /// will be created in the current directory. 461 /// Error will be emitted if the path is insane. 462 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 463 LLVM_DUMP_METHOD void dumpDotGraph(const Twine &FileName, const Twine &Title); 464 #endif 465 466 /// Pop up a GraphViz/gv window with the DAG rendered using 'dot'. 467 void viewGraph(const std::string &Title); 468 void viewGraph(); 469 470 #ifndef NDEBUG 471 std::map<const SDNode *, std::string> NodeGraphAttrs; 472 #endif 473 474 /// Clear all previously defined node graph attributes. 475 /// Intended to be used from a debugging tool (eg. gdb). 476 void clearGraphAttrs(); 477 478 /// Set graph attributes for a node. (eg. "color=red".) 479 void setGraphAttrs(const SDNode *N, const char *Attrs); 480 481 /// Get graph attributes for a node. (eg. "color=red".) 482 /// Used from getNodeAttributes. 483 std::string getGraphAttrs(const SDNode *N) const; 484 485 /// Convenience for setting node color attribute. 486 void setGraphColor(const SDNode *N, const char *Color); 487 488 /// Convenience for setting subgraph color attribute. 489 void setSubgraphColor(SDNode *N, const char *Color); 490 491 using allnodes_const_iterator = ilist<SDNode>::const_iterator; 492 493 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); } 494 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); } 495 496 using allnodes_iterator = ilist<SDNode>::iterator; 497 498 allnodes_iterator allnodes_begin() { return AllNodes.begin(); } 499 allnodes_iterator allnodes_end() { return AllNodes.end(); } 500 501 ilist<SDNode>::size_type allnodes_size() const { 502 return AllNodes.size(); 503 } 504 505 iterator_range<allnodes_iterator> allnodes() { 506 return make_range(allnodes_begin(), allnodes_end()); 507 } 508 iterator_range<allnodes_const_iterator> allnodes() const { 509 return make_range(allnodes_begin(), allnodes_end()); 510 } 511 512 /// Return the root tag of the SelectionDAG. 513 const SDValue &getRoot() const { return Root; } 514 515 /// Return the token chain corresponding to the entry of the function. 516 SDValue getEntryNode() const { 517 return SDValue(const_cast<SDNode *>(&EntryNode), 0); 518 } 519 520 /// Set the current root tag of the SelectionDAG. 521 /// 522 const SDValue &setRoot(SDValue N) { 523 assert((!N.getNode() || N.getValueType() == MVT::Other) && 524 "DAG root value is not a chain!"); 525 if (N.getNode()) 526 checkForCycles(N.getNode(), this); 527 Root = N; 528 if (N.getNode()) 529 checkForCycles(this); 530 return Root; 531 } 532 533 #ifndef NDEBUG 534 void VerifyDAGDiverence(); 535 #endif 536 537 /// This iterates over the nodes in the SelectionDAG, folding 538 /// certain types of nodes together, or eliminating superfluous nodes. The 539 /// Level argument controls whether Combine is allowed to produce nodes and 540 /// types that are illegal on the target. 541 void Combine(CombineLevel Level, AAResults *AA, 542 CodeGenOpt::Level OptLevel); 543 544 /// This transforms the SelectionDAG into a SelectionDAG that 545 /// only uses types natively supported by the target. 546 /// Returns "true" if it made any changes. 547 /// 548 /// Note that this is an involved process that may invalidate pointers into 549 /// the graph. 550 bool LegalizeTypes(); 551 552 /// This transforms the SelectionDAG into a SelectionDAG that is 553 /// compatible with the target instruction selector, as indicated by the 554 /// TargetLowering object. 555 /// 556 /// Note that this is an involved process that may invalidate pointers into 557 /// the graph. 558 void Legalize(); 559 560 /// Transforms a SelectionDAG node and any operands to it into a node 561 /// that is compatible with the target instruction selector, as indicated by 562 /// the TargetLowering object. 563 /// 564 /// \returns true if \c N is a valid, legal node after calling this. 565 /// 566 /// This essentially runs a single recursive walk of the \c Legalize process 567 /// over the given node (and its operands). This can be used to incrementally 568 /// legalize the DAG. All of the nodes which are directly replaced, 569 /// potentially including N, are added to the output parameter \c 570 /// UpdatedNodes so that the delta to the DAG can be understood by the 571 /// caller. 572 /// 573 /// When this returns false, N has been legalized in a way that make the 574 /// pointer passed in no longer valid. It may have even been deleted from the 575 /// DAG, and so it shouldn't be used further. When this returns true, the 576 /// N passed in is a legal node, and can be immediately processed as such. 577 /// This may still have done some work on the DAG, and will still populate 578 /// UpdatedNodes with any new nodes replacing those originally in the DAG. 579 bool LegalizeOp(SDNode *N, SmallSetVector<SDNode *, 16> &UpdatedNodes); 580 581 /// This transforms the SelectionDAG into a SelectionDAG 582 /// that only uses vector math operations supported by the target. This is 583 /// necessary as a separate step from Legalize because unrolling a vector 584 /// operation can introduce illegal types, which requires running 585 /// LegalizeTypes again. 586 /// 587 /// This returns true if it made any changes; in that case, LegalizeTypes 588 /// is called again before Legalize. 589 /// 590 /// Note that this is an involved process that may invalidate pointers into 591 /// the graph. 592 bool LegalizeVectors(); 593 594 /// This method deletes all unreachable nodes in the SelectionDAG. 595 void RemoveDeadNodes(); 596 597 /// Remove the specified node from the system. This node must 598 /// have no referrers. 599 void DeleteNode(SDNode *N); 600 601 /// Return an SDVTList that represents the list of values specified. 602 SDVTList getVTList(EVT VT); 603 SDVTList getVTList(EVT VT1, EVT VT2); 604 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3); 605 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4); 606 SDVTList getVTList(ArrayRef<EVT> VTs); 607 608 //===--------------------------------------------------------------------===// 609 // Node creation methods. 610 611 /// Create a ConstantSDNode wrapping a constant value. 612 /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR. 613 /// 614 /// If only legal types can be produced, this does the necessary 615 /// transformations (e.g., if the vector element type is illegal). 616 /// @{ 617 SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, 618 bool isTarget = false, bool isOpaque = false); 619 SDValue getConstant(const APInt &Val, const SDLoc &DL, EVT VT, 620 bool isTarget = false, bool isOpaque = false); 621 622 SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget = false, 623 bool IsOpaque = false) { 624 return getConstant(APInt::getAllOnesValue(VT.getScalarSizeInBits()), DL, 625 VT, IsTarget, IsOpaque); 626 } 627 628 SDValue getConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT, 629 bool isTarget = false, bool isOpaque = false); 630 SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, 631 bool isTarget = false); 632 SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL, 633 bool LegalTypes = true); 634 SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, 635 bool isTarget = false); 636 637 SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, 638 bool isOpaque = false) { 639 return getConstant(Val, DL, VT, true, isOpaque); 640 } 641 SDValue getTargetConstant(const APInt &Val, const SDLoc &DL, EVT VT, 642 bool isOpaque = false) { 643 return getConstant(Val, DL, VT, true, isOpaque); 644 } 645 SDValue getTargetConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT, 646 bool isOpaque = false) { 647 return getConstant(Val, DL, VT, true, isOpaque); 648 } 649 650 /// Create a true or false constant of type \p VT using the target's 651 /// BooleanContent for type \p OpVT. 652 SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT); 653 /// @} 654 655 /// Create a ConstantFPSDNode wrapping a constant value. 656 /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR. 657 /// 658 /// If only legal types can be produced, this does the necessary 659 /// transformations (e.g., if the vector element type is illegal). 660 /// The forms that take a double should only be used for simple constants 661 /// that can be exactly represented in VT. No checks are made. 662 /// @{ 663 SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, 664 bool isTarget = false); 665 SDValue getConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT, 666 bool isTarget = false); 667 SDValue getConstantFP(const ConstantFP &V, const SDLoc &DL, EVT VT, 668 bool isTarget = false); 669 SDValue getTargetConstantFP(double Val, const SDLoc &DL, EVT VT) { 670 return getConstantFP(Val, DL, VT, true); 671 } 672 SDValue getTargetConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT) { 673 return getConstantFP(Val, DL, VT, true); 674 } 675 SDValue getTargetConstantFP(const ConstantFP &Val, const SDLoc &DL, EVT VT) { 676 return getConstantFP(Val, DL, VT, true); 677 } 678 /// @} 679 680 SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, 681 int64_t offset = 0, bool isTargetGA = false, 682 unsigned TargetFlags = 0); 683 SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, 684 int64_t offset = 0, unsigned TargetFlags = 0) { 685 return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags); 686 } 687 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false); 688 SDValue getTargetFrameIndex(int FI, EVT VT) { 689 return getFrameIndex(FI, VT, true); 690 } 691 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false, 692 unsigned TargetFlags = 0); 693 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags = 0) { 694 return getJumpTable(JTI, VT, true, TargetFlags); 695 } 696 SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align = None, 697 int Offs = 0, bool isT = false, 698 unsigned TargetFlags = 0); 699 SDValue getTargetConstantPool(const Constant *C, EVT VT, 700 MaybeAlign Align = None, int Offset = 0, 701 unsigned TargetFlags = 0) { 702 return getConstantPool(C, VT, Align, Offset, true, TargetFlags); 703 } 704 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT, 705 MaybeAlign Align = None, int Offs = 0, 706 bool isT = false, unsigned TargetFlags = 0); 707 SDValue getTargetConstantPool(MachineConstantPoolValue *C, EVT VT, 708 MaybeAlign Align = None, int Offset = 0, 709 unsigned TargetFlags = 0) { 710 return getConstantPool(C, VT, Align, Offset, true, TargetFlags); 711 } 712 SDValue getTargetIndex(int Index, EVT VT, int64_t Offset = 0, 713 unsigned TargetFlags = 0); 714 // When generating a branch to a BB, we don't in general know enough 715 // to provide debug info for the BB at that time, so keep this one around. 716 SDValue getBasicBlock(MachineBasicBlock *MBB); 717 SDValue getExternalSymbol(const char *Sym, EVT VT); 718 SDValue getTargetExternalSymbol(const char *Sym, EVT VT, 719 unsigned TargetFlags = 0); 720 SDValue getMCSymbol(MCSymbol *Sym, EVT VT); 721 722 SDValue getValueType(EVT); 723 SDValue getRegister(unsigned Reg, EVT VT); 724 SDValue getRegisterMask(const uint32_t *RegMask); 725 SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label); 726 SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, 727 MCSymbol *Label); 728 SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset = 0, 729 bool isTarget = false, unsigned TargetFlags = 0); 730 SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT, 731 int64_t Offset = 0, unsigned TargetFlags = 0) { 732 return getBlockAddress(BA, VT, Offset, true, TargetFlags); 733 } 734 735 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, 736 SDValue N) { 737 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain, 738 getRegister(Reg, N.getValueType()), N); 739 } 740 741 // This version of the getCopyToReg method takes an extra operand, which 742 // indicates that there is potentially an incoming glue value (if Glue is not 743 // null) and that there should be a glue result. 744 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N, 745 SDValue Glue) { 746 SDVTList VTs = getVTList(MVT::Other, MVT::Glue); 747 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Glue }; 748 return getNode(ISD::CopyToReg, dl, VTs, 749 makeArrayRef(Ops, Glue.getNode() ? 4 : 3)); 750 } 751 752 // Similar to last getCopyToReg() except parameter Reg is a SDValue 753 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, SDValue Reg, SDValue N, 754 SDValue Glue) { 755 SDVTList VTs = getVTList(MVT::Other, MVT::Glue); 756 SDValue Ops[] = { Chain, Reg, N, Glue }; 757 return getNode(ISD::CopyToReg, dl, VTs, 758 makeArrayRef(Ops, Glue.getNode() ? 4 : 3)); 759 } 760 761 SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT) { 762 SDVTList VTs = getVTList(VT, MVT::Other); 763 SDValue Ops[] = { Chain, getRegister(Reg, VT) }; 764 return getNode(ISD::CopyFromReg, dl, VTs, Ops); 765 } 766 767 // This version of the getCopyFromReg method takes an extra operand, which 768 // indicates that there is potentially an incoming glue value (if Glue is not 769 // null) and that there should be a glue result. 770 SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT, 771 SDValue Glue) { 772 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Glue); 773 SDValue Ops[] = { Chain, getRegister(Reg, VT), Glue }; 774 return getNode(ISD::CopyFromReg, dl, VTs, 775 makeArrayRef(Ops, Glue.getNode() ? 3 : 2)); 776 } 777 778 SDValue getCondCode(ISD::CondCode Cond); 779 780 /// Return an ISD::VECTOR_SHUFFLE node. The number of elements in VT, 781 /// which must be a vector type, must match the number of mask elements 782 /// NumElts. An integer mask element equal to -1 is treated as undefined. 783 SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, 784 ArrayRef<int> Mask); 785 786 /// Return an ISD::BUILD_VECTOR node. The number of elements in VT, 787 /// which must be a vector type, must match the number of operands in Ops. 788 /// The operands must have the same type as (or, for integers, a type wider 789 /// than) VT's element type. 790 SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDValue> Ops) { 791 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later. 792 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops); 793 } 794 795 /// Return an ISD::BUILD_VECTOR node. The number of elements in VT, 796 /// which must be a vector type, must match the number of operands in Ops. 797 /// The operands must have the same type as (or, for integers, a type wider 798 /// than) VT's element type. 799 SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDUse> Ops) { 800 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later. 801 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops); 802 } 803 804 /// Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all 805 /// elements. VT must be a vector type. Op's type must be the same as (or, 806 /// for integers, a type wider than) VT's element type. 807 SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op) { 808 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later. 809 if (Op.getOpcode() == ISD::UNDEF) { 810 assert((VT.getVectorElementType() == Op.getValueType() || 811 (VT.isInteger() && 812 VT.getVectorElementType().bitsLE(Op.getValueType()))) && 813 "A splatted value must have a width equal or (for integers) " 814 "greater than the vector element type!"); 815 return getNode(ISD::UNDEF, SDLoc(), VT); 816 } 817 818 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Op); 819 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops); 820 } 821 822 // Return a splat ISD::SPLAT_VECTOR node, consisting of Op splatted to all 823 // elements. 824 SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op) { 825 if (Op.getOpcode() == ISD::UNDEF) { 826 assert((VT.getVectorElementType() == Op.getValueType() || 827 (VT.isInteger() && 828 VT.getVectorElementType().bitsLE(Op.getValueType()))) && 829 "A splatted value must have a width equal or (for integers) " 830 "greater than the vector element type!"); 831 return getNode(ISD::UNDEF, SDLoc(), VT); 832 } 833 return getNode(ISD::SPLAT_VECTOR, DL, VT, Op); 834 } 835 836 /// Returns a vector of type ResVT whose elements contain the linear sequence 837 /// <0, Step, Step * 2, Step * 3, ...> 838 SDValue getStepVector(const SDLoc &DL, EVT ResVT, SDValue Step); 839 840 /// Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to 841 /// the shuffle node in input but with swapped operands. 842 /// 843 /// Example: shuffle A, B, <0,5,2,7> -> shuffle B, A, <4,1,6,3> 844 SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV); 845 846 /// Convert Op, which must be of float type, to the 847 /// float type VT, by either extending or rounding (by truncation). 848 SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT); 849 850 /// Convert Op, which must be a STRICT operation of float type, to the 851 /// float type VT, by either extending or rounding (by truncation). 852 std::pair<SDValue, SDValue> 853 getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT); 854 855 /// Convert Op, which must be of integer type, to the 856 /// integer type VT, by either any-extending or truncating it. 857 SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT); 858 859 /// Convert Op, which must be of integer type, to the 860 /// integer type VT, by either sign-extending or truncating it. 861 SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT); 862 863 /// Convert Op, which must be of integer type, to the 864 /// integer type VT, by either zero-extending or truncating it. 865 SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT); 866 867 /// Return the expression required to zero extend the Op 868 /// value assuming it was the smaller SrcTy value. 869 SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT); 870 871 /// Convert Op, which must be of integer type, to the integer type VT, by 872 /// either truncating it or performing either zero or sign extension as 873 /// appropriate extension for the pointer's semantics. 874 SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT); 875 876 /// Return the expression required to extend the Op as a pointer value 877 /// assuming it was the smaller SrcTy value. This may be either a zero extend 878 /// or a sign extend. 879 SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT); 880 881 /// Convert Op, which must be of integer type, to the integer type VT, 882 /// by using an extension appropriate for the target's 883 /// BooleanContent for type OpVT or truncating it. 884 SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT); 885 886 /// Create a bitwise NOT operation as (XOR Val, -1). 887 SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT); 888 889 /// Create a logical NOT operation as (XOR Val, BooleanOne). 890 SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT); 891 892 /// Returns sum of the base pointer and offset. 893 /// Unlike getObjectPtrOffset this does not set NoUnsignedWrap by default. 894 SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, 895 const SDNodeFlags Flags = SDNodeFlags()); 896 SDValue getMemBasePlusOffset(SDValue Base, SDValue Offset, const SDLoc &DL, 897 const SDNodeFlags Flags = SDNodeFlags()); 898 899 /// Create an add instruction with appropriate flags when used for 900 /// addressing some offset of an object. i.e. if a load is split into multiple 901 /// components, create an add nuw from the base pointer to the offset. 902 SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset) { 903 SDNodeFlags Flags; 904 Flags.setNoUnsignedWrap(true); 905 return getMemBasePlusOffset(Ptr, Offset, SL, Flags); 906 } 907 908 SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, SDValue Offset) { 909 // The object itself can't wrap around the address space, so it shouldn't be 910 // possible for the adds of the offsets to the split parts to overflow. 911 SDNodeFlags Flags; 912 Flags.setNoUnsignedWrap(true); 913 return getMemBasePlusOffset(Ptr, Offset, SL, Flags); 914 } 915 916 /// Return a new CALLSEQ_START node, that starts new call frame, in which 917 /// InSize bytes are set up inside CALLSEQ_START..CALLSEQ_END sequence and 918 /// OutSize specifies part of the frame set up prior to the sequence. 919 SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, 920 const SDLoc &DL) { 921 SDVTList VTs = getVTList(MVT::Other, MVT::Glue); 922 SDValue Ops[] = { Chain, 923 getIntPtrConstant(InSize, DL, true), 924 getIntPtrConstant(OutSize, DL, true) }; 925 return getNode(ISD::CALLSEQ_START, DL, VTs, Ops); 926 } 927 928 /// Return a new CALLSEQ_END node, which always must have a 929 /// glue result (to ensure it's not CSE'd). 930 /// CALLSEQ_END does not have a useful SDLoc. 931 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, 932 SDValue InGlue, const SDLoc &DL) { 933 SDVTList NodeTys = getVTList(MVT::Other, MVT::Glue); 934 SmallVector<SDValue, 4> Ops; 935 Ops.push_back(Chain); 936 Ops.push_back(Op1); 937 Ops.push_back(Op2); 938 if (InGlue.getNode()) 939 Ops.push_back(InGlue); 940 return getNode(ISD::CALLSEQ_END, DL, NodeTys, Ops); 941 } 942 943 /// Return true if the result of this operation is always undefined. 944 bool isUndef(unsigned Opcode, ArrayRef<SDValue> Ops); 945 946 /// Return an UNDEF node. UNDEF does not have a useful SDLoc. 947 SDValue getUNDEF(EVT VT) { 948 return getNode(ISD::UNDEF, SDLoc(), VT); 949 } 950 951 /// Return a node that represents the runtime scaling 'MulImm * RuntimeVL'. 952 SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm) { 953 assert(MulImm.getMinSignedBits() <= VT.getSizeInBits() && 954 "Immediate does not fit VT"); 955 return getNode(ISD::VSCALE, DL, VT, 956 getConstant(MulImm.sextOrTrunc(VT.getSizeInBits()), DL, VT)); 957 } 958 959 /// Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc. 960 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) { 961 return getNode(ISD::GLOBAL_OFFSET_TABLE, SDLoc(), VT); 962 } 963 964 /// Gets or creates the specified node. 965 /// 966 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, 967 ArrayRef<SDUse> Ops); 968 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, 969 ArrayRef<SDValue> Ops, const SDNodeFlags Flags); 970 SDValue getNode(unsigned Opcode, const SDLoc &DL, ArrayRef<EVT> ResultTys, 971 ArrayRef<SDValue> Ops); 972 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, 973 ArrayRef<SDValue> Ops, const SDNodeFlags Flags); 974 975 // Use flags from current flag inserter. 976 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, 977 ArrayRef<SDValue> Ops); 978 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, 979 ArrayRef<SDValue> Ops); 980 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue Operand); 981 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1, 982 SDValue N2); 983 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1, 984 SDValue N2, SDValue N3); 985 986 // Specialize based on number of operands. 987 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT); 988 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue Operand, 989 const SDNodeFlags Flags); 990 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1, 991 SDValue N2, const SDNodeFlags Flags); 992 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1, 993 SDValue N2, SDValue N3, const SDNodeFlags Flags); 994 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1, 995 SDValue N2, SDValue N3, SDValue N4); 996 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1, 997 SDValue N2, SDValue N3, SDValue N4, SDValue N5); 998 999 // Specialize again based on number of operands for nodes with a VTList 1000 // rather than a single VT. 1001 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList); 1002 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N); 1003 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1, 1004 SDValue N2); 1005 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1, 1006 SDValue N2, SDValue N3); 1007 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1, 1008 SDValue N2, SDValue N3, SDValue N4); 1009 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1, 1010 SDValue N2, SDValue N3, SDValue N4, SDValue N5); 1011 1012 /// Compute a TokenFactor to force all the incoming stack arguments to be 1013 /// loaded from the stack. This is used in tail call lowering to protect 1014 /// stack arguments from being clobbered. 1015 SDValue getStackArgumentTokenFactor(SDValue Chain); 1016 1017 SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, 1018 SDValue Size, Align Alignment, bool isVol, 1019 bool AlwaysInline, bool isTailCall, 1020 MachinePointerInfo DstPtrInfo, 1021 MachinePointerInfo SrcPtrInfo); 1022 1023 SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, 1024 SDValue Size, Align Alignment, bool isVol, bool isTailCall, 1025 MachinePointerInfo DstPtrInfo, 1026 MachinePointerInfo SrcPtrInfo); 1027 1028 SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, 1029 SDValue Size, Align Alignment, bool isVol, bool isTailCall, 1030 MachinePointerInfo DstPtrInfo); 1031 1032 SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, 1033 unsigned DstAlign, SDValue Src, unsigned SrcAlign, 1034 SDValue Size, Type *SizeTy, unsigned ElemSz, 1035 bool isTailCall, MachinePointerInfo DstPtrInfo, 1036 MachinePointerInfo SrcPtrInfo); 1037 1038 SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, 1039 unsigned DstAlign, SDValue Src, unsigned SrcAlign, 1040 SDValue Size, Type *SizeTy, unsigned ElemSz, 1041 bool isTailCall, MachinePointerInfo DstPtrInfo, 1042 MachinePointerInfo SrcPtrInfo); 1043 1044 SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, 1045 unsigned DstAlign, SDValue Value, SDValue Size, 1046 Type *SizeTy, unsigned ElemSz, bool isTailCall, 1047 MachinePointerInfo DstPtrInfo); 1048 1049 /// Helper function to make it easier to build SetCC's if you just have an 1050 /// ISD::CondCode instead of an SDValue. 1051 SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, 1052 ISD::CondCode Cond, SDValue Chain = SDValue(), 1053 bool IsSignaling = false) { 1054 assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() && 1055 "Cannot compare scalars to vectors"); 1056 assert(LHS.getValueType().isVector() == VT.isVector() && 1057 "Cannot compare scalars to vectors"); 1058 assert(Cond != ISD::SETCC_INVALID && 1059 "Cannot create a setCC of an invalid node."); 1060 if (Chain) 1061 return getNode(IsSignaling ? ISD::STRICT_FSETCCS : ISD::STRICT_FSETCC, DL, 1062 {VT, MVT::Other}, {Chain, LHS, RHS, getCondCode(Cond)}); 1063 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond)); 1064 } 1065 1066 /// Helper function to make it easier to build Select's if you just have 1067 /// operands and don't want to check for vector. 1068 SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, 1069 SDValue RHS) { 1070 assert(LHS.getValueType() == RHS.getValueType() && 1071 "Cannot use select on differing types"); 1072 assert(VT.isVector() == LHS.getValueType().isVector() && 1073 "Cannot mix vectors and scalars"); 1074 auto Opcode = Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT; 1075 return getNode(Opcode, DL, VT, Cond, LHS, RHS); 1076 } 1077 1078 /// Helper function to make it easier to build SelectCC's if you just have an 1079 /// ISD::CondCode instead of an SDValue. 1080 SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, 1081 SDValue False, ISD::CondCode Cond) { 1082 return getNode(ISD::SELECT_CC, DL, True.getValueType(), LHS, RHS, True, 1083 False, getCondCode(Cond)); 1084 } 1085 1086 /// Try to simplify a select/vselect into 1 of its operands or a constant. 1087 SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal); 1088 1089 /// Try to simplify a shift into 1 of its operands or a constant. 1090 SDValue simplifyShift(SDValue X, SDValue Y); 1091 1092 /// Try to simplify a floating-point binary operation into 1 of its operands 1093 /// or a constant. 1094 SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, 1095 SDNodeFlags Flags); 1096 1097 /// VAArg produces a result and token chain, and takes a pointer 1098 /// and a source value as input. 1099 SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, 1100 SDValue SV, unsigned Align); 1101 1102 /// Gets a node for an atomic cmpxchg op. There are two 1103 /// valid Opcodes. ISD::ATOMIC_CMO_SWAP produces the value loaded and a 1104 /// chain result. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS produces the value loaded, 1105 /// a success flag (initially i1), and a chain. 1106 SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, 1107 SDVTList VTs, SDValue Chain, SDValue Ptr, 1108 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO); 1109 1110 /// Gets a node for an atomic op, produces result (if relevant) 1111 /// and chain and takes 2 operands. 1112 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, 1113 SDValue Ptr, SDValue Val, MachineMemOperand *MMO); 1114 1115 /// Gets a node for an atomic op, produces result and chain and 1116 /// takes 1 operand. 1117 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, EVT VT, 1118 SDValue Chain, SDValue Ptr, MachineMemOperand *MMO); 1119 1120 /// Gets a node for an atomic op, produces result and chain and takes N 1121 /// operands. 1122 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, 1123 SDVTList VTList, ArrayRef<SDValue> Ops, 1124 MachineMemOperand *MMO); 1125 1126 /// Creates a MemIntrinsicNode that may produce a 1127 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID, 1128 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not 1129 /// less than FIRST_TARGET_MEMORY_OPCODE. 1130 SDValue getMemIntrinsicNode( 1131 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops, 1132 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, 1133 MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad | 1134 MachineMemOperand::MOStore, 1135 uint64_t Size = 0, const AAMDNodes &AAInfo = AAMDNodes()); 1136 1137 inline SDValue getMemIntrinsicNode( 1138 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops, 1139 EVT MemVT, MachinePointerInfo PtrInfo, MaybeAlign Alignment = None, 1140 MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad | 1141 MachineMemOperand::MOStore, 1142 uint64_t Size = 0, const AAMDNodes &AAInfo = AAMDNodes()) { 1143 // Ensure that codegen never sees alignment 0 1144 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, PtrInfo, 1145 Alignment.getValueOr(getEVTAlign(MemVT)), Flags, 1146 Size, AAInfo); 1147 } 1148 1149 SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, 1150 ArrayRef<SDValue> Ops, EVT MemVT, 1151 MachineMemOperand *MMO); 1152 1153 /// Creates a LifetimeSDNode that starts (`IsStart==true`) or ends 1154 /// (`IsStart==false`) the lifetime of the portion of `FrameIndex` between 1155 /// offsets `Offset` and `Offset + Size`. 1156 SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, 1157 int FrameIndex, int64_t Size, int64_t Offset = -1); 1158 1159 /// Creates a PseudoProbeSDNode with function GUID `Guid` and 1160 /// the index of the block `Index` it is probing, as well as the attributes 1161 /// `attr` of the probe. 1162 SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, 1163 uint64_t Index, uint32_t Attr); 1164 1165 /// Create a MERGE_VALUES node from the given operands. 1166 SDValue getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl); 1167 1168 /// Loads are not normal binary operators: their result type is not 1169 /// determined by their operands, and they produce a value AND a token chain. 1170 /// 1171 /// This function will set the MOLoad flag on MMOFlags, but you can set it if 1172 /// you want. The MOStore flag must not be set. 1173 SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, 1174 MachinePointerInfo PtrInfo, 1175 MaybeAlign Alignment = MaybeAlign(), 1176 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1177 const AAMDNodes &AAInfo = AAMDNodes(), 1178 const MDNode *Ranges = nullptr); 1179 /// FIXME: Remove once transition to Align is over. 1180 inline SDValue 1181 getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, 1182 MachinePointerInfo PtrInfo, unsigned Alignment, 1183 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1184 const AAMDNodes &AAInfo = AAMDNodes(), 1185 const MDNode *Ranges = nullptr) { 1186 return getLoad(VT, dl, Chain, Ptr, PtrInfo, MaybeAlign(Alignment), MMOFlags, 1187 AAInfo, Ranges); 1188 } 1189 SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, 1190 MachineMemOperand *MMO); 1191 SDValue 1192 getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, 1193 SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, 1194 MaybeAlign Alignment = MaybeAlign(), 1195 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1196 const AAMDNodes &AAInfo = AAMDNodes()); 1197 /// FIXME: Remove once transition to Align is over. 1198 inline SDValue 1199 getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, 1200 SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, 1201 unsigned Alignment, 1202 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1203 const AAMDNodes &AAInfo = AAMDNodes()) { 1204 return getExtLoad(ExtType, dl, VT, Chain, Ptr, PtrInfo, MemVT, 1205 MaybeAlign(Alignment), MMOFlags, AAInfo); 1206 } 1207 SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, 1208 SDValue Chain, SDValue Ptr, EVT MemVT, 1209 MachineMemOperand *MMO); 1210 SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, 1211 SDValue Offset, ISD::MemIndexedMode AM); 1212 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, 1213 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, 1214 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, 1215 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1216 const AAMDNodes &AAInfo = AAMDNodes(), 1217 const MDNode *Ranges = nullptr); 1218 inline SDValue getLoad( 1219 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, 1220 SDValue Chain, SDValue Ptr, SDValue Offset, MachinePointerInfo PtrInfo, 1221 EVT MemVT, MaybeAlign Alignment = MaybeAlign(), 1222 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1223 const AAMDNodes &AAInfo = AAMDNodes(), const MDNode *Ranges = nullptr) { 1224 // Ensures that codegen never sees a None Alignment. 1225 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, PtrInfo, MemVT, 1226 Alignment.getValueOr(getEVTAlign(MemVT)), MMOFlags, AAInfo, 1227 Ranges); 1228 } 1229 /// FIXME: Remove once transition to Align is over. 1230 inline SDValue 1231 getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, 1232 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, 1233 MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment, 1234 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1235 const AAMDNodes &AAInfo = AAMDNodes(), 1236 const MDNode *Ranges = nullptr) { 1237 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, PtrInfo, MemVT, 1238 MaybeAlign(Alignment), MMOFlags, AAInfo, Ranges); 1239 } 1240 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, 1241 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, 1242 EVT MemVT, MachineMemOperand *MMO); 1243 1244 /// Helper function to build ISD::STORE nodes. 1245 /// 1246 /// This function will set the MOStore flag on MMOFlags, but you can set it if 1247 /// you want. The MOLoad and MOInvariant flags must not be set. 1248 1249 SDValue 1250 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1251 MachinePointerInfo PtrInfo, Align Alignment, 1252 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1253 const AAMDNodes &AAInfo = AAMDNodes()); 1254 inline SDValue 1255 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1256 MachinePointerInfo PtrInfo, MaybeAlign Alignment = MaybeAlign(), 1257 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1258 const AAMDNodes &AAInfo = AAMDNodes()) { 1259 return getStore(Chain, dl, Val, Ptr, PtrInfo, 1260 Alignment.getValueOr(getEVTAlign(Val.getValueType())), 1261 MMOFlags, AAInfo); 1262 } 1263 /// FIXME: Remove once transition to Align is over. 1264 inline SDValue 1265 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1266 MachinePointerInfo PtrInfo, unsigned Alignment, 1267 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1268 const AAMDNodes &AAInfo = AAMDNodes()) { 1269 return getStore(Chain, dl, Val, Ptr, PtrInfo, MaybeAlign(Alignment), 1270 MMOFlags, AAInfo); 1271 } 1272 SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1273 MachineMemOperand *MMO); 1274 SDValue 1275 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1276 MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, 1277 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1278 const AAMDNodes &AAInfo = AAMDNodes()); 1279 inline SDValue 1280 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1281 MachinePointerInfo PtrInfo, EVT SVT, 1282 MaybeAlign Alignment = MaybeAlign(), 1283 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1284 const AAMDNodes &AAInfo = AAMDNodes()) { 1285 return getTruncStore(Chain, dl, Val, Ptr, PtrInfo, SVT, 1286 Alignment.getValueOr(getEVTAlign(SVT)), MMOFlags, 1287 AAInfo); 1288 } 1289 /// FIXME: Remove once transition to Align is over. 1290 inline SDValue 1291 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, 1292 MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment, 1293 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone, 1294 const AAMDNodes &AAInfo = AAMDNodes()) { 1295 return getTruncStore(Chain, dl, Val, Ptr, PtrInfo, SVT, 1296 MaybeAlign(Alignment), MMOFlags, AAInfo); 1297 } 1298 SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, 1299 SDValue Ptr, EVT SVT, MachineMemOperand *MMO); 1300 SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, 1301 SDValue Offset, ISD::MemIndexedMode AM); 1302 1303 SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, 1304 SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, 1305 MachineMemOperand *MMO, ISD::MemIndexedMode AM, 1306 ISD::LoadExtType, bool IsExpanding = false); 1307 SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, 1308 SDValue Offset, ISD::MemIndexedMode AM); 1309 SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, 1310 SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, 1311 MachineMemOperand *MMO, ISD::MemIndexedMode AM, 1312 bool IsTruncating = false, bool IsCompressing = false); 1313 SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, 1314 SDValue Base, SDValue Offset, 1315 ISD::MemIndexedMode AM); 1316 SDValue getMaskedGather(SDVTList VTs, EVT VT, const SDLoc &dl, 1317 ArrayRef<SDValue> Ops, MachineMemOperand *MMO, 1318 ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy); 1319 SDValue getMaskedScatter(SDVTList VTs, EVT VT, const SDLoc &dl, 1320 ArrayRef<SDValue> Ops, MachineMemOperand *MMO, 1321 ISD::MemIndexType IndexType, 1322 bool IsTruncating = false); 1323 1324 /// Construct a node to track a Value* through the backend. 1325 SDValue getSrcValue(const Value *v); 1326 1327 /// Return an MDNodeSDNode which holds an MDNode. 1328 SDValue getMDNode(const MDNode *MD); 1329 1330 /// Return a bitcast using the SDLoc of the value operand, and casting to the 1331 /// provided type. Use getNode to set a custom SDLoc. 1332 SDValue getBitcast(EVT VT, SDValue V); 1333 1334 /// Return an AddrSpaceCastSDNode. 1335 SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, 1336 unsigned DestAS); 1337 1338 /// Return a freeze using the SDLoc of the value operand. 1339 SDValue getFreeze(SDValue V); 1340 1341 /// Return an AssertAlignSDNode. 1342 SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A); 1343 1344 /// Return the specified value casted to 1345 /// the target's desired shift amount type. 1346 SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op); 1347 1348 /// Expand the specified \c ISD::VAARG node as the Legalize pass would. 1349 SDValue expandVAArg(SDNode *Node); 1350 1351 /// Expand the specified \c ISD::VACOPY node as the Legalize pass would. 1352 SDValue expandVACopy(SDNode *Node); 1353 1354 /// Returs an GlobalAddress of the function from the current module with 1355 /// name matching the given ExternalSymbol. Additionally can provide the 1356 /// matched function. 1357 /// Panics the function doesn't exists. 1358 SDValue getSymbolFunctionGlobalAddress(SDValue Op, 1359 Function **TargetFunction = nullptr); 1360 1361 /// *Mutate* the specified node in-place to have the 1362 /// specified operands. If the resultant node already exists in the DAG, 1363 /// this does not modify the specified node, instead it returns the node that 1364 /// already exists. If the resultant node does not exist in the DAG, the 1365 /// input node is returned. As a degenerate case, if you specify the same 1366 /// input operands as the node already has, the input node is returned. 1367 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op); 1368 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2); 1369 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 1370 SDValue Op3); 1371 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 1372 SDValue Op3, SDValue Op4); 1373 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, 1374 SDValue Op3, SDValue Op4, SDValue Op5); 1375 SDNode *UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops); 1376 1377 /// Creates a new TokenFactor containing \p Vals. If \p Vals contains 64k 1378 /// values or more, move values into new TokenFactors in 64k-1 blocks, until 1379 /// the final TokenFactor has less than 64k operands. 1380 SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl<SDValue> &Vals); 1381 1382 /// *Mutate* the specified machine node's memory references to the provided 1383 /// list. 1384 void setNodeMemRefs(MachineSDNode *N, 1385 ArrayRef<MachineMemOperand *> NewMemRefs); 1386 1387 // Calculate divergence of node \p N based on its operands. 1388 bool calculateDivergence(SDNode *N); 1389 1390 // Propagates the change in divergence to users 1391 void updateDivergence(SDNode * N); 1392 1393 /// These are used for target selectors to *mutate* the 1394 /// specified node to have the specified return type, Target opcode, and 1395 /// operands. Note that target opcodes are stored as 1396 /// ~TargetOpcode in the node opcode field. The resultant node is returned. 1397 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT); 1398 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT, SDValue Op1); 1399 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT, 1400 SDValue Op1, SDValue Op2); 1401 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT, 1402 SDValue Op1, SDValue Op2, SDValue Op3); 1403 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT, 1404 ArrayRef<SDValue> Ops); 1405 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1, EVT VT2); 1406 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1, 1407 EVT VT2, ArrayRef<SDValue> Ops); 1408 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1, 1409 EVT VT2, EVT VT3, ArrayRef<SDValue> Ops); 1410 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1, 1411 EVT VT2, SDValue Op1, SDValue Op2); 1412 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, SDVTList VTs, 1413 ArrayRef<SDValue> Ops); 1414 1415 /// This *mutates* the specified node to have the specified 1416 /// return type, opcode, and operands. 1417 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, 1418 ArrayRef<SDValue> Ops); 1419 1420 /// Mutate the specified strict FP node to its non-strict equivalent, 1421 /// unlinking the node from its chain and dropping the metadata arguments. 1422 /// The node must be a strict FP node. 1423 SDNode *mutateStrictFPToFP(SDNode *Node); 1424 1425 /// These are used for target selectors to create a new node 1426 /// with specified return type(s), MachineInstr opcode, and operands. 1427 /// 1428 /// Note that getMachineNode returns the resultant node. If there is already 1429 /// a node of the specified opcode and operands, it returns that node instead 1430 /// of the current one. 1431 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT); 1432 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT, 1433 SDValue Op1); 1434 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT, 1435 SDValue Op1, SDValue Op2); 1436 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT, 1437 SDValue Op1, SDValue Op2, SDValue Op3); 1438 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT, 1439 ArrayRef<SDValue> Ops); 1440 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1, 1441 EVT VT2, SDValue Op1, SDValue Op2); 1442 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1, 1443 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3); 1444 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1, 1445 EVT VT2, ArrayRef<SDValue> Ops); 1446 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1, 1447 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2); 1448 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1, 1449 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, 1450 SDValue Op3); 1451 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1, 1452 EVT VT2, EVT VT3, ArrayRef<SDValue> Ops); 1453 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, 1454 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops); 1455 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, SDVTList VTs, 1456 ArrayRef<SDValue> Ops); 1457 1458 /// A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes. 1459 SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, 1460 SDValue Operand); 1461 1462 /// A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes. 1463 SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, 1464 SDValue Operand, SDValue Subreg); 1465 1466 /// Get the specified node if it's already available, or else return NULL. 1467 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTList, 1468 ArrayRef<SDValue> Ops, const SDNodeFlags Flags); 1469 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTList, 1470 ArrayRef<SDValue> Ops); 1471 1472 /// Check if a node exists without modifying its flags. 1473 bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef<SDValue> Ops); 1474 1475 /// Creates a SDDbgValue node. 1476 SDDbgValue *getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, 1477 unsigned R, bool IsIndirect, const DebugLoc &DL, 1478 unsigned O); 1479 1480 /// Creates a constant SDDbgValue node. 1481 SDDbgValue *getConstantDbgValue(DIVariable *Var, DIExpression *Expr, 1482 const Value *C, const DebugLoc &DL, 1483 unsigned O); 1484 1485 /// Creates a FrameIndex SDDbgValue node. 1486 SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, 1487 unsigned FI, bool IsIndirect, 1488 const DebugLoc &DL, unsigned O); 1489 1490 /// Creates a FrameIndex SDDbgValue node. 1491 SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, 1492 unsigned FI, 1493 ArrayRef<SDNode *> Dependencies, 1494 bool IsIndirect, const DebugLoc &DL, 1495 unsigned O); 1496 1497 /// Creates a VReg SDDbgValue node. 1498 SDDbgValue *getVRegDbgValue(DIVariable *Var, DIExpression *Expr, 1499 unsigned VReg, bool IsIndirect, 1500 const DebugLoc &DL, unsigned O); 1501 1502 /// Creates a SDDbgValue node from a list of locations. 1503 SDDbgValue *getDbgValueList(DIVariable *Var, DIExpression *Expr, 1504 ArrayRef<SDDbgOperand> Locs, 1505 ArrayRef<SDNode *> Dependencies, bool IsIndirect, 1506 const DebugLoc &DL, unsigned O, bool IsVariadic); 1507 1508 /// Creates a SDDbgLabel node. 1509 SDDbgLabel *getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O); 1510 1511 /// Transfer debug values from one node to another, while optionally 1512 /// generating fragment expressions for split-up values. If \p InvalidateDbg 1513 /// is set, debug values are invalidated after they are transferred. 1514 void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits = 0, 1515 unsigned SizeInBits = 0, bool InvalidateDbg = true); 1516 1517 /// Remove the specified node from the system. If any of its 1518 /// operands then becomes dead, remove them as well. Inform UpdateListener 1519 /// for each node deleted. 1520 void RemoveDeadNode(SDNode *N); 1521 1522 /// This method deletes the unreachable nodes in the 1523 /// given list, and any nodes that become unreachable as a result. 1524 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes); 1525 1526 /// Modify anything using 'From' to use 'To' instead. 1527 /// This can cause recursive merging of nodes in the DAG. Use the first 1528 /// version if 'From' is known to have a single result, use the second 1529 /// if you have two nodes with identical results (or if 'To' has a superset 1530 /// of the results of 'From'), use the third otherwise. 1531 /// 1532 /// These methods all take an optional UpdateListener, which (if not null) is 1533 /// informed about nodes that are deleted and modified due to recursive 1534 /// changes in the dag. 1535 /// 1536 /// These functions only replace all existing uses. It's possible that as 1537 /// these replacements are being performed, CSE may cause the From node 1538 /// to be given new uses. These new uses of From are left in place, and 1539 /// not automatically transferred to To. 1540 /// 1541 void ReplaceAllUsesWith(SDValue From, SDValue To); 1542 void ReplaceAllUsesWith(SDNode *From, SDNode *To); 1543 void ReplaceAllUsesWith(SDNode *From, const SDValue *To); 1544 1545 /// Replace any uses of From with To, leaving 1546 /// uses of other values produced by From.getNode() alone. 1547 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To); 1548 1549 /// Like ReplaceAllUsesOfValueWith, but for multiple values at once. 1550 /// This correctly handles the case where 1551 /// there is an overlap between the From values and the To values. 1552 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, 1553 unsigned Num); 1554 1555 /// If an existing load has uses of its chain, create a token factor node with 1556 /// that chain and the new memory node's chain and update users of the old 1557 /// chain to the token factor. This ensures that the new memory node will have 1558 /// the same relative memory dependency position as the old load. Returns the 1559 /// new merged load chain. 1560 SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain); 1561 1562 /// If an existing load has uses of its chain, create a token factor node with 1563 /// that chain and the new memory node's chain and update users of the old 1564 /// chain to the token factor. This ensures that the new memory node will have 1565 /// the same relative memory dependency position as the old load. Returns the 1566 /// new merged load chain. 1567 SDValue makeEquivalentMemoryOrdering(LoadSDNode *OldLoad, SDValue NewMemOp); 1568 1569 /// Topological-sort the AllNodes list and a 1570 /// assign a unique node id for each node in the DAG based on their 1571 /// topological order. Returns the number of nodes. 1572 unsigned AssignTopologicalOrder(); 1573 1574 /// Move node N in the AllNodes list to be immediately 1575 /// before the given iterator Position. This may be used to update the 1576 /// topological ordering when the list of nodes is modified. 1577 void RepositionNode(allnodes_iterator Position, SDNode *N) { 1578 AllNodes.insert(Position, AllNodes.remove(N)); 1579 } 1580 1581 /// Returns an APFloat semantics tag appropriate for the given type. If VT is 1582 /// a vector type, the element semantics are returned. 1583 static const fltSemantics &EVTToAPFloatSemantics(EVT VT) { 1584 switch (VT.getScalarType().getSimpleVT().SimpleTy) { 1585 default: llvm_unreachable("Unknown FP format"); 1586 case MVT::f16: return APFloat::IEEEhalf(); 1587 case MVT::bf16: return APFloat::BFloat(); 1588 case MVT::f32: return APFloat::IEEEsingle(); 1589 case MVT::f64: return APFloat::IEEEdouble(); 1590 case MVT::f80: return APFloat::x87DoubleExtended(); 1591 case MVT::f128: return APFloat::IEEEquad(); 1592 case MVT::ppcf128: return APFloat::PPCDoubleDouble(); 1593 } 1594 } 1595 1596 /// Add a dbg_value SDNode. If SD is non-null that means the 1597 /// value is produced by SD. 1598 void AddDbgValue(SDDbgValue *DB, bool isParameter); 1599 1600 /// Add a dbg_label SDNode. 1601 void AddDbgLabel(SDDbgLabel *DB); 1602 1603 /// Get the debug values which reference the given SDNode. 1604 ArrayRef<SDDbgValue*> GetDbgValues(const SDNode* SD) const { 1605 return DbgInfo->getSDDbgValues(SD); 1606 } 1607 1608 public: 1609 /// Return true if there are any SDDbgValue nodes associated 1610 /// with this SelectionDAG. 1611 bool hasDebugValues() const { return !DbgInfo->empty(); } 1612 1613 SDDbgInfo::DbgIterator DbgBegin() const { return DbgInfo->DbgBegin(); } 1614 SDDbgInfo::DbgIterator DbgEnd() const { return DbgInfo->DbgEnd(); } 1615 1616 SDDbgInfo::DbgIterator ByvalParmDbgBegin() const { 1617 return DbgInfo->ByvalParmDbgBegin(); 1618 } 1619 SDDbgInfo::DbgIterator ByvalParmDbgEnd() const { 1620 return DbgInfo->ByvalParmDbgEnd(); 1621 } 1622 1623 SDDbgInfo::DbgLabelIterator DbgLabelBegin() const { 1624 return DbgInfo->DbgLabelBegin(); 1625 } 1626 SDDbgInfo::DbgLabelIterator DbgLabelEnd() const { 1627 return DbgInfo->DbgLabelEnd(); 1628 } 1629 1630 /// To be invoked on an SDNode that is slated to be erased. This 1631 /// function mirrors \c llvm::salvageDebugInfo. 1632 void salvageDebugInfo(SDNode &N); 1633 1634 void dump() const; 1635 1636 /// In most cases this function returns the ABI alignment for a given type, 1637 /// except for illegal vector types where the alignment exceeds that of the 1638 /// stack. In such cases we attempt to break the vector down to a legal type 1639 /// and return the ABI alignment for that instead. 1640 Align getReducedAlign(EVT VT, bool UseABI); 1641 1642 /// Create a stack temporary based on the size in bytes and the alignment 1643 SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment); 1644 1645 /// Create a stack temporary, suitable for holding the specified value type. 1646 /// If minAlign is specified, the slot size will have at least that alignment. 1647 SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1); 1648 1649 /// Create a stack temporary suitable for holding either of the specified 1650 /// value types. 1651 SDValue CreateStackTemporary(EVT VT1, EVT VT2); 1652 1653 SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, 1654 const GlobalAddressSDNode *GA, 1655 const SDNode *N2); 1656 1657 SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, 1658 ArrayRef<SDValue> Ops); 1659 1660 SDValue FoldConstantVectorArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, 1661 ArrayRef<SDValue> Ops, 1662 const SDNodeFlags Flags = SDNodeFlags()); 1663 1664 /// Fold floating-point operations with 2 operands when both operands are 1665 /// constants and/or undefined. 1666 SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, 1667 SDValue N1, SDValue N2); 1668 1669 /// Constant fold a setcc to true or false. 1670 SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, 1671 const SDLoc &dl); 1672 1673 /// See if the specified operand can be simplified with the knowledge that 1674 /// only the bits specified by DemandedBits are used. If so, return the 1675 /// simpler operand, otherwise return a null SDValue. 1676 /// 1677 /// (This exists alongside SimplifyDemandedBits because GetDemandedBits can 1678 /// simplify nodes with multiple uses more aggressively.) 1679 SDValue GetDemandedBits(SDValue V, const APInt &DemandedBits); 1680 1681 /// See if the specified operand can be simplified with the knowledge that 1682 /// only the bits specified by DemandedBits are used in the elements specified 1683 /// by DemandedElts. If so, return the simpler operand, otherwise return a 1684 /// null SDValue. 1685 /// 1686 /// (This exists alongside SimplifyDemandedBits because GetDemandedBits can 1687 /// simplify nodes with multiple uses more aggressively.) 1688 SDValue GetDemandedBits(SDValue V, const APInt &DemandedBits, 1689 const APInt &DemandedElts); 1690 1691 /// Return true if the sign bit of Op is known to be zero. 1692 /// We use this predicate to simplify operations downstream. 1693 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const; 1694 1695 /// Return true if 'Op & Mask' is known to be zero. We 1696 /// use this predicate to simplify operations downstream. Op and Mask are 1697 /// known to be the same type. 1698 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, 1699 unsigned Depth = 0) const; 1700 1701 /// Return true if 'Op & Mask' is known to be zero in DemandedElts. We 1702 /// use this predicate to simplify operations downstream. Op and Mask are 1703 /// known to be the same type. 1704 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, 1705 const APInt &DemandedElts, unsigned Depth = 0) const; 1706 1707 /// Return true if the DemandedElts of the vector Op are all zero. We 1708 /// use this predicate to simplify operations downstream. 1709 bool MaskedElementsAreZero(SDValue Op, const APInt &DemandedElts, 1710 unsigned Depth = 0) const; 1711 1712 /// Return true if '(Op & Mask) == Mask'. 1713 /// Op and Mask are known to be the same type. 1714 bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, 1715 unsigned Depth = 0) const; 1716 1717 /// Determine which bits of Op are known to be either zero or one and return 1718 /// them in Known. For vectors, the known bits are those that are shared by 1719 /// every vector element. 1720 /// Targets can implement the computeKnownBitsForTargetNode method in the 1721 /// TargetLowering class to allow target nodes to be understood. 1722 KnownBits computeKnownBits(SDValue Op, unsigned Depth = 0) const; 1723 1724 /// Determine which bits of Op are known to be either zero or one and return 1725 /// them in Known. The DemandedElts argument allows us to only collect the 1726 /// known bits that are shared by the requested vector elements. 1727 /// Targets can implement the computeKnownBitsForTargetNode method in the 1728 /// TargetLowering class to allow target nodes to be understood. 1729 KnownBits computeKnownBits(SDValue Op, const APInt &DemandedElts, 1730 unsigned Depth = 0) const; 1731 1732 /// Used to represent the possible overflow behavior of an operation. 1733 /// Never: the operation cannot overflow. 1734 /// Always: the operation will always overflow. 1735 /// Sometime: the operation may or may not overflow. 1736 enum OverflowKind { 1737 OFK_Never, 1738 OFK_Sometime, 1739 OFK_Always, 1740 }; 1741 1742 /// Determine if the result of the addition of 2 node can overflow. 1743 OverflowKind computeOverflowKind(SDValue N0, SDValue N1) const; 1744 1745 /// Test if the given value is known to have exactly one bit set. This differs 1746 /// from computeKnownBits in that it doesn't necessarily determine which bit 1747 /// is set. 1748 bool isKnownToBeAPowerOfTwo(SDValue Val) const; 1749 1750 /// Return the number of times the sign bit of the register is replicated into 1751 /// the other bits. We know that at least 1 bit is always equal to the sign 1752 /// bit (itself), but other cases can give us information. For example, 1753 /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal 1754 /// to each other, so we return 3. Targets can implement the 1755 /// ComputeNumSignBitsForTarget method in the TargetLowering class to allow 1756 /// target nodes to be understood. 1757 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const; 1758 1759 /// Return the number of times the sign bit of the register is replicated into 1760 /// the other bits. We know that at least 1 bit is always equal to the sign 1761 /// bit (itself), but other cases can give us information. For example, 1762 /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal 1763 /// to each other, so we return 3. The DemandedElts argument allows 1764 /// us to only collect the minimum sign bits of the requested vector elements. 1765 /// Targets can implement the ComputeNumSignBitsForTarget method in the 1766 /// TargetLowering class to allow target nodes to be understood. 1767 unsigned ComputeNumSignBits(SDValue Op, const APInt &DemandedElts, 1768 unsigned Depth = 0) const; 1769 1770 /// Return true if the specified operand is an ISD::ADD with a ConstantSDNode 1771 /// on the right-hand side, or if it is an ISD::OR with a ConstantSDNode that 1772 /// is guaranteed to have the same semantics as an ADD. This handles the 1773 /// equivalence: 1774 /// X|Cst == X+Cst iff X&Cst = 0. 1775 bool isBaseWithConstantOffset(SDValue Op) const; 1776 1777 /// Test whether the given SDValue is known to never be NaN. If \p SNaN is 1778 /// true, returns if \p Op is known to never be a signaling NaN (it may still 1779 /// be a qNaN). 1780 bool isKnownNeverNaN(SDValue Op, bool SNaN = false, unsigned Depth = 0) const; 1781 1782 /// \returns true if \p Op is known to never be a signaling NaN. 1783 bool isKnownNeverSNaN(SDValue Op, unsigned Depth = 0) const { 1784 return isKnownNeverNaN(Op, true, Depth); 1785 } 1786 1787 /// Test whether the given floating point SDValue is known to never be 1788 /// positive or negative zero. 1789 bool isKnownNeverZeroFloat(SDValue Op) const; 1790 1791 /// Test whether the given SDValue is known to contain non-zero value(s). 1792 bool isKnownNeverZero(SDValue Op) const; 1793 1794 /// Test whether two SDValues are known to compare equal. This 1795 /// is true if they are the same value, or if one is negative zero and the 1796 /// other positive zero. 1797 bool isEqualTo(SDValue A, SDValue B) const; 1798 1799 /// Return true if A and B have no common bits set. As an example, this can 1800 /// allow an 'add' to be transformed into an 'or'. 1801 bool haveNoCommonBitsSet(SDValue A, SDValue B) const; 1802 1803 /// Test whether \p V has a splatted value for all the demanded elements. 1804 /// 1805 /// On success \p UndefElts will indicate the elements that have UNDEF 1806 /// values instead of the splat value, this is only guaranteed to be correct 1807 /// for \p DemandedElts. 1808 /// 1809 /// NOTE: The function will return true for a demanded splat of UNDEF values. 1810 bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, 1811 unsigned Depth = 0); 1812 1813 /// Test whether \p V has a splatted value. 1814 bool isSplatValue(SDValue V, bool AllowUndefs = false); 1815 1816 /// If V is a splatted value, return the source vector and its splat index. 1817 SDValue getSplatSourceVector(SDValue V, int &SplatIndex); 1818 1819 /// If V is a splat vector, return its scalar source operand by extracting 1820 /// that element from the source vector. If LegalTypes is true, this method 1821 /// may only return a legally-typed splat value. If it cannot legalize the 1822 /// splatted value it will return SDValue(). 1823 SDValue getSplatValue(SDValue V, bool LegalTypes = false); 1824 1825 /// If a SHL/SRA/SRL node \p V has a constant or splat constant shift amount 1826 /// that is less than the element bit-width of the shift node, return it. 1827 const APInt *getValidShiftAmountConstant(SDValue V, 1828 const APInt &DemandedElts) const; 1829 1830 /// If a SHL/SRA/SRL node \p V has constant shift amounts that are all less 1831 /// than the element bit-width of the shift node, return the minimum value. 1832 const APInt * 1833 getValidMinimumShiftAmountConstant(SDValue V, 1834 const APInt &DemandedElts) const; 1835 1836 /// If a SHL/SRA/SRL node \p V has constant shift amounts that are all less 1837 /// than the element bit-width of the shift node, return the maximum value. 1838 const APInt * 1839 getValidMaximumShiftAmountConstant(SDValue V, 1840 const APInt &DemandedElts) const; 1841 1842 /// Match a binop + shuffle pyramid that represents a horizontal reduction 1843 /// over the elements of a vector starting from the EXTRACT_VECTOR_ELT node /p 1844 /// Extract. The reduction must use one of the opcodes listed in /p 1845 /// CandidateBinOps and on success /p BinOp will contain the matching opcode. 1846 /// Returns the vector that is being reduced on, or SDValue() if a reduction 1847 /// was not matched. If \p AllowPartials is set then in the case of a 1848 /// reduction pattern that only matches the first few stages, the extracted 1849 /// subvector of the start of the reduction is returned. 1850 SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, 1851 ArrayRef<ISD::NodeType> CandidateBinOps, 1852 bool AllowPartials = false); 1853 1854 /// Utility function used by legalize and lowering to 1855 /// "unroll" a vector operation by splitting out the scalars and operating 1856 /// on each element individually. If the ResNE is 0, fully unroll the vector 1857 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE. 1858 /// If the ResNE is greater than the width of the vector op, unroll the 1859 /// vector op and fill the end of the resulting vector with UNDEFS. 1860 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0); 1861 1862 /// Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes. 1863 /// This is a separate function because those opcodes have two results. 1864 std::pair<SDValue, SDValue> UnrollVectorOverflowOp(SDNode *N, 1865 unsigned ResNE = 0); 1866 1867 /// Return true if loads are next to each other and can be 1868 /// merged. Check that both are nonvolatile and if LD is loading 1869 /// 'Bytes' bytes from a location that is 'Dist' units away from the 1870 /// location that the 'Base' load is loading from. 1871 bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, 1872 unsigned Bytes, int Dist) const; 1873 1874 /// Infer alignment of a load / store address. Return None if it cannot be 1875 /// inferred. 1876 MaybeAlign InferPtrAlign(SDValue Ptr) const; 1877 1878 /// Compute the VTs needed for the low/hi parts of a type 1879 /// which is split (or expanded) into two not necessarily identical pieces. 1880 std::pair<EVT, EVT> GetSplitDestVTs(const EVT &VT) const; 1881 1882 /// Compute the VTs needed for the low/hi parts of a type, dependent on an 1883 /// enveloping VT that has been split into two identical pieces. Sets the 1884 /// HisIsEmpty flag when hi type has zero storage size. 1885 std::pair<EVT, EVT> GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, 1886 bool *HiIsEmpty) const; 1887 1888 /// Split the vector with EXTRACT_SUBVECTOR using the provides 1889 /// VTs and return the low/high part. 1890 std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL, 1891 const EVT &LoVT, const EVT &HiVT); 1892 1893 /// Split the vector with EXTRACT_SUBVECTOR and return the low/high part. 1894 std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL) { 1895 EVT LoVT, HiVT; 1896 std::tie(LoVT, HiVT) = GetSplitDestVTs(N.getValueType()); 1897 return SplitVector(N, DL, LoVT, HiVT); 1898 } 1899 1900 /// Split the node's operand with EXTRACT_SUBVECTOR and 1901 /// return the low/high part. 1902 std::pair<SDValue, SDValue> SplitVectorOperand(const SDNode *N, unsigned OpNo) 1903 { 1904 return SplitVector(N->getOperand(OpNo), SDLoc(N)); 1905 } 1906 1907 /// Widen the vector up to the next power of two using INSERT_SUBVECTOR. 1908 SDValue WidenVector(const SDValue &N, const SDLoc &DL); 1909 1910 /// Append the extracted elements from Start to Count out of the vector Op in 1911 /// Args. If Count is 0, all of the elements will be extracted. The extracted 1912 /// elements will have type EVT if it is provided, and otherwise their type 1913 /// will be Op's element type. 1914 void ExtractVectorElements(SDValue Op, SmallVectorImpl<SDValue> &Args, 1915 unsigned Start = 0, unsigned Count = 0, 1916 EVT EltVT = EVT()); 1917 1918 /// Compute the default alignment value for the given type. 1919 Align getEVTAlign(EVT MemoryVT) const; 1920 /// Compute the default alignment value for the given type. 1921 /// FIXME: Remove once transition to Align is over. 1922 inline unsigned getEVTAlignment(EVT MemoryVT) const { 1923 return getEVTAlign(MemoryVT).value(); 1924 } 1925 1926 /// Test whether the given value is a constant int or similar node. 1927 SDNode *isConstantIntBuildVectorOrConstantInt(SDValue N) const; 1928 1929 /// Test whether the given value is a constant FP or similar node. 1930 SDNode *isConstantFPBuildVectorOrConstantFP(SDValue N) const ; 1931 1932 /// \returns true if \p N is any kind of constant or build_vector of 1933 /// constants, int or float. If a vector, it may not necessarily be a splat. 1934 inline bool isConstantValueOfAnyType(SDValue N) const { 1935 return isConstantIntBuildVectorOrConstantInt(N) || 1936 isConstantFPBuildVectorOrConstantFP(N); 1937 } 1938 1939 void addCallSiteInfo(const SDNode *CallNode, CallSiteInfoImpl &&CallInfo) { 1940 SDCallSiteDbgInfo[CallNode].CSInfo = std::move(CallInfo); 1941 } 1942 1943 CallSiteInfo getSDCallSiteInfo(const SDNode *CallNode) { 1944 auto I = SDCallSiteDbgInfo.find(CallNode); 1945 if (I != SDCallSiteDbgInfo.end()) 1946 return std::move(I->second).CSInfo; 1947 return CallSiteInfo(); 1948 } 1949 1950 void addHeapAllocSite(const SDNode *Node, MDNode *MD) { 1951 SDCallSiteDbgInfo[Node].HeapAllocSite = MD; 1952 } 1953 1954 /// Return the HeapAllocSite type associated with the SDNode, if it exists. 1955 MDNode *getHeapAllocSite(const SDNode *Node) { 1956 auto It = SDCallSiteDbgInfo.find(Node); 1957 if (It == SDCallSiteDbgInfo.end()) 1958 return nullptr; 1959 return It->second.HeapAllocSite; 1960 } 1961 1962 void addNoMergeSiteInfo(const SDNode *Node, bool NoMerge) { 1963 if (NoMerge) 1964 SDCallSiteDbgInfo[Node].NoMerge = NoMerge; 1965 } 1966 1967 bool getNoMergeSiteInfo(const SDNode *Node) { 1968 auto I = SDCallSiteDbgInfo.find(Node); 1969 if (I == SDCallSiteDbgInfo.end()) 1970 return false; 1971 return I->second.NoMerge; 1972 } 1973 1974 /// Return the current function's default denormal handling kind for the given 1975 /// floating point type. 1976 DenormalMode getDenormalMode(EVT VT) const { 1977 return MF->getDenormalMode(EVTToAPFloatSemantics(VT)); 1978 } 1979 1980 bool shouldOptForSize() const; 1981 1982 /// Get the (commutative) neutral element for the given opcode, if it exists. 1983 SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, 1984 SDNodeFlags Flags); 1985 1986 private: 1987 void InsertNode(SDNode *N); 1988 bool RemoveNodeFromCSEMaps(SDNode *N); 1989 void AddModifiedNodeToCSEMaps(SDNode *N); 1990 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos); 1991 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2, 1992 void *&InsertPos); 1993 SDNode *FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops, 1994 void *&InsertPos); 1995 SDNode *UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &loc); 1996 1997 void DeleteNodeNotInCSEMaps(SDNode *N); 1998 void DeallocateNode(SDNode *N); 1999 2000 void allnodes_clear(); 2001 2002 /// Look up the node specified by ID in CSEMap. If it exists, return it. If 2003 /// not, return the insertion token that will make insertion faster. This 2004 /// overload is for nodes other than Constant or ConstantFP, use the other one 2005 /// for those. 2006 SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos); 2007 2008 /// Look up the node specified by ID in CSEMap. If it exists, return it. If 2009 /// not, return the insertion token that will make insertion faster. Performs 2010 /// additional processing for constant nodes. 2011 SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, const SDLoc &DL, 2012 void *&InsertPos); 2013 2014 /// List of non-single value types. 2015 FoldingSet<SDVTListNode> VTListMap; 2016 2017 /// Maps to auto-CSE operations. 2018 std::vector<CondCodeSDNode*> CondCodeNodes; 2019 2020 std::vector<SDNode*> ValueTypeNodes; 2021 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes; 2022 StringMap<SDNode*> ExternalSymbols; 2023 2024 std::map<std::pair<std::string, unsigned>, SDNode *> TargetExternalSymbols; 2025 DenseMap<MCSymbol *, SDNode *> MCSymbols; 2026 2027 FlagInserter *Inserter = nullptr; 2028 }; 2029 2030 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> { 2031 using nodes_iterator = pointer_iterator<SelectionDAG::allnodes_iterator>; 2032 2033 static nodes_iterator nodes_begin(SelectionDAG *G) { 2034 return nodes_iterator(G->allnodes_begin()); 2035 } 2036 2037 static nodes_iterator nodes_end(SelectionDAG *G) { 2038 return nodes_iterator(G->allnodes_end()); 2039 } 2040 }; 2041 2042 } // end namespace llvm 2043 2044 #endif // LLVM_CODEGEN_SELECTIONDAG_H 2045