LLVM  mainline
SelectionDAGNodes.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the SDNode class and derived classes, which are used to
00011 // represent the nodes and operations present in a SelectionDAG.  These nodes
00012 // and operations are machine code level operations, with some similarities to
00013 // the GCC RTL representation.
00014 //
00015 // Clients should include the SelectionDAG.h file instead of this file directly.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
00020 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
00021 
00022 #include "llvm/ADT/BitVector.h"
00023 #include "llvm/ADT/FoldingSet.h"
00024 #include "llvm/ADT/GraphTraits.h"
00025 #include "llvm/ADT/STLExtras.h"
00026 #include "llvm/ADT/SmallPtrSet.h"
00027 #include "llvm/ADT/SmallVector.h"
00028 #include "llvm/ADT/ilist_node.h"
00029 #include "llvm/ADT/iterator_range.h"
00030 #include "llvm/CodeGen/ISDOpcodes.h"
00031 #include "llvm/CodeGen/MachineMemOperand.h"
00032 #include "llvm/CodeGen/ValueTypes.h"
00033 #include "llvm/IR/Constants.h"
00034 #include "llvm/IR/DebugLoc.h"
00035 #include "llvm/IR/Instructions.h"
00036 #include "llvm/Support/DataTypes.h"
00037 #include "llvm/Support/MathExtras.h"
00038 #include <cassert>
00039 
00040 namespace llvm {
00041 
00042 class SelectionDAG;
00043 class GlobalValue;
00044 class MachineBasicBlock;
00045 class MachineConstantPoolValue;
00046 class SDNode;
00047 class Value;
00048 class MCSymbol;
00049 template <typename T> struct DenseMapInfo;
00050 template <typename T> struct simplify_type;
00051 template <typename T> struct ilist_traits;
00052 
00053 /// Returns true if the opcode is a binary operation with flags.
00054 static bool isBinOpWithFlags(unsigned Opcode) {
00055   switch (Opcode) {
00056   case ISD::SDIV:
00057   case ISD::UDIV:
00058   case ISD::SRA:
00059   case ISD::SRL:
00060   case ISD::MUL:
00061   case ISD::ADD:
00062   case ISD::SUB:
00063   case ISD::SHL:
00064     return true;
00065   default:
00066     return false;
00067   }
00068 }
00069 
00070 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
00071                     bool force = false);
00072 
00073 /// This represents a list of ValueType's that has been intern'd by
00074 /// a SelectionDAG.  Instances of this simple value class are returned by
00075 /// SelectionDAG::getVTList(...).
00076 ///
00077 struct SDVTList {
00078   const EVT *VTs;
00079   unsigned int NumVTs;
00080 };
00081 
00082 namespace ISD {
00083   /// Node predicates
00084 
00085   /// Return true if the specified node is a
00086   /// BUILD_VECTOR where all of the elements are ~0 or undef.
00087   bool isBuildVectorAllOnes(const SDNode *N);
00088 
00089   /// Return true if the specified node is a
00090   /// BUILD_VECTOR where all of the elements are 0 or undef.
00091   bool isBuildVectorAllZeros(const SDNode *N);
00092 
00093   /// \brief Return true if the specified node is a BUILD_VECTOR node of
00094   /// all ConstantSDNode or undef.
00095   bool isBuildVectorOfConstantSDNodes(const SDNode *N);
00096 
00097   /// \brief Return true if the specified node is a BUILD_VECTOR node of
00098   /// all ConstantFPSDNode or undef.
00099   bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
00100 
00101   /// Return true if the specified node is a
00102   /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
00103   /// element is not an undef.
00104   bool isScalarToVector(const SDNode *N);
00105 
00106   /// Return true if the node has at least one operand
00107   /// and all operands of the specified node are ISD::UNDEF.
00108   bool allOperandsUndef(const SDNode *N);
00109 }  // end llvm:ISD namespace
00110 
00111 //===----------------------------------------------------------------------===//
00112 /// Unlike LLVM values, Selection DAG nodes may return multiple
00113 /// values as the result of a computation.  Many nodes return multiple values,
00114 /// from loads (which define a token and a return value) to ADDC (which returns
00115 /// a result and a carry value), to calls (which may return an arbitrary number
00116 /// of values).
00117 ///
00118 /// As such, each use of a SelectionDAG computation must indicate the node that
00119 /// computes it as well as which return value to use from that node.  This pair
00120 /// of information is represented with the SDValue value type.
00121 ///
00122 class SDValue {
00123   friend struct DenseMapInfo<SDValue>;
00124 
00125   SDNode *Node;       // The node defining the value we are using.
00126   unsigned ResNo;     // Which return value of the node we are using.
00127 public:
00128   SDValue() : Node(nullptr), ResNo(0) {}
00129   SDValue(SDNode *node, unsigned resno);
00130 
00131   /// get the index which selects a specific result in the SDNode
00132   unsigned getResNo() const { return ResNo; }
00133 
00134   /// get the SDNode which holds the desired result
00135   SDNode *getNode() const { return Node; }
00136 
00137   /// set the SDNode
00138   void setNode(SDNode *N) { Node = N; }
00139 
00140   inline SDNode *operator->() const { return Node; }
00141 
00142   bool operator==(const SDValue &O) const {
00143     return Node == O.Node && ResNo == O.ResNo;
00144   }
00145   bool operator!=(const SDValue &O) const {
00146     return !operator==(O);
00147   }
00148   bool operator<(const SDValue &O) const {
00149     return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
00150   }
00151   explicit operator bool() const {
00152     return Node != nullptr;
00153   }
00154 
00155   SDValue getValue(unsigned R) const {
00156     return SDValue(Node, R);
00157   }
00158 
00159   // Return true if this node is an operand of N.
00160   bool isOperandOf(SDNode *N) const;
00161 
00162   /// Return the ValueType of the referenced return value.
00163   inline EVT getValueType() const;
00164 
00165   /// Return the simple ValueType of the referenced return value.
00166   MVT getSimpleValueType() const {
00167     return getValueType().getSimpleVT();
00168   }
00169 
00170   /// Returns the size of the value in bits.
00171   unsigned getValueSizeInBits() const {
00172     return getValueType().getSizeInBits();
00173   }
00174 
00175   unsigned getScalarValueSizeInBits() const {
00176     return getValueType().getScalarType().getSizeInBits();
00177   }
00178 
00179   // Forwarding methods - These forward to the corresponding methods in SDNode.
00180   inline unsigned getOpcode() const;
00181   inline unsigned getNumOperands() const;
00182   inline const SDValue &getOperand(unsigned i) const;
00183   inline uint64_t getConstantOperandVal(unsigned i) const;
00184   inline bool isTargetMemoryOpcode() const;
00185   inline bool isTargetOpcode() const;
00186   inline bool isMachineOpcode() const;
00187   inline unsigned getMachineOpcode() const;
00188   inline const DebugLoc &getDebugLoc() const;
00189   inline void dump() const;
00190   inline void dumpr() const;
00191 
00192   /// Return true if this operand (which must be a chain) reaches the
00193   /// specified operand without crossing any side-effecting instructions.
00194   /// In practice, this looks through token factors and non-volatile loads.
00195   /// In order to remain efficient, this only
00196   /// looks a couple of nodes in, it does not do an exhaustive search.
00197   bool reachesChainWithoutSideEffects(SDValue Dest,
00198                                       unsigned Depth = 2) const;
00199 
00200   /// Return true if there are no nodes using value ResNo of Node.
00201   inline bool use_empty() const;
00202 
00203   /// Return true if there is exactly one node using value ResNo of Node.
00204   inline bool hasOneUse() const;
00205 };
00206 
00207 
00208 template<> struct DenseMapInfo<SDValue> {
00209   static inline SDValue getEmptyKey() {
00210     SDValue V;
00211     V.ResNo = -1U;
00212     return V;
00213   }
00214   static inline SDValue getTombstoneKey() {
00215     SDValue V;
00216     V.ResNo = -2U;
00217     return V;
00218   }
00219   static unsigned getHashValue(const SDValue &Val) {
00220     return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
00221             (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
00222   }
00223   static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
00224     return LHS == RHS;
00225   }
00226 };
00227 template <> struct isPodLike<SDValue> { static const bool value = true; };
00228 
00229 
00230 /// Allow casting operators to work directly on
00231 /// SDValues as if they were SDNode*'s.
00232 template<> struct simplify_type<SDValue> {
00233   typedef SDNode* SimpleType;
00234   static SimpleType getSimplifiedValue(SDValue &Val) {
00235     return Val.getNode();
00236   }
00237 };
00238 template<> struct simplify_type<const SDValue> {
00239   typedef /*const*/ SDNode* SimpleType;
00240   static SimpleType getSimplifiedValue(const SDValue &Val) {
00241     return Val.getNode();
00242   }
00243 };
00244 
00245 /// Represents a use of a SDNode. This class holds an SDValue,
00246 /// which records the SDNode being used and the result number, a
00247 /// pointer to the SDNode using the value, and Next and Prev pointers,
00248 /// which link together all the uses of an SDNode.
00249 ///
00250 class SDUse {
00251   /// Val - The value being used.
00252   SDValue Val;
00253   /// User - The user of this value.
00254   SDNode *User;
00255   /// Prev, Next - Pointers to the uses list of the SDNode referred by
00256   /// this operand.
00257   SDUse **Prev, *Next;
00258 
00259   SDUse(const SDUse &U) = delete;
00260   void operator=(const SDUse &U) = delete;
00261 
00262 public:
00263   SDUse() : Val(), User(nullptr), Prev(nullptr), Next(nullptr) {}
00264 
00265   /// Normally SDUse will just implicitly convert to an SDValue that it holds.
00266   operator const SDValue&() const { return Val; }
00267 
00268   /// If implicit conversion to SDValue doesn't work, the get() method returns
00269   /// the SDValue.
00270   const SDValue &get() const { return Val; }
00271 
00272   /// This returns the SDNode that contains this Use.
00273   SDNode *getUser() { return User; }
00274 
00275   /// Get the next SDUse in the use list.
00276   SDUse *getNext() const { return Next; }
00277 
00278   /// Convenience function for get().getNode().
00279   SDNode *getNode() const { return Val.getNode(); }
00280   /// Convenience function for get().getResNo().
00281   unsigned getResNo() const { return Val.getResNo(); }
00282   /// Convenience function for get().getValueType().
00283   EVT getValueType() const { return Val.getValueType(); }
00284 
00285   /// Convenience function for get().operator==
00286   bool operator==(const SDValue &V) const {
00287     return Val == V;
00288   }
00289 
00290   /// Convenience function for get().operator!=
00291   bool operator!=(const SDValue &V) const {
00292     return Val != V;
00293   }
00294 
00295   /// Convenience function for get().operator<
00296   bool operator<(const SDValue &V) const {
00297     return Val < V;
00298   }
00299 
00300 private:
00301   friend class SelectionDAG;
00302   friend class SDNode;
00303 
00304   void setUser(SDNode *p) { User = p; }
00305 
00306   /// Remove this use from its existing use list, assign it the
00307   /// given value, and add it to the new value's node's use list.
00308   inline void set(const SDValue &V);
00309   /// Like set, but only supports initializing a newly-allocated
00310   /// SDUse with a non-null value.
00311   inline void setInitial(const SDValue &V);
00312   /// Like set, but only sets the Node portion of the value,
00313   /// leaving the ResNo portion unmodified.
00314   inline void setNode(SDNode *N);
00315 
00316   void addToList(SDUse **List) {
00317     Next = *List;
00318     if (Next) Next->Prev = &Next;
00319     Prev = List;
00320     *List = this;
00321   }
00322 
00323   void removeFromList() {
00324     *Prev = Next;
00325     if (Next) Next->Prev = Prev;
00326   }
00327 };
00328 
00329 /// simplify_type specializations - Allow casting operators to work directly on
00330 /// SDValues as if they were SDNode*'s.
00331 template<> struct simplify_type<SDUse> {
00332   typedef SDNode* SimpleType;
00333   static SimpleType getSimplifiedValue(SDUse &Val) {
00334     return Val.getNode();
00335   }
00336 };
00337 
00338 
00339 /// Represents one node in the SelectionDAG.
00340 ///
00341 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
00342 private:
00343   /// The operation that this node performs.
00344   int16_t NodeType;
00345 
00346   /// This is true if OperandList was new[]'d.  If true,
00347   /// then they will be delete[]'d when the node is destroyed.
00348   uint16_t OperandsNeedDelete : 1;
00349 
00350   /// This tracks whether this node has one or more dbg_value
00351   /// nodes corresponding to it.
00352   uint16_t HasDebugValue : 1;
00353 
00354 protected:
00355   /// This member is defined by this class, but is not used for
00356   /// anything.  Subclasses can use it to hold whatever state they find useful.
00357   /// This field is initialized to zero by the ctor.
00358   uint16_t SubclassData : 14;
00359 
00360 private:
00361   /// Unique id per SDNode in the DAG.
00362   int NodeId;
00363 
00364   /// The values that are used by this operation.
00365   SDUse *OperandList;
00366 
00367   /// The types of the values this node defines.  SDNode's may
00368   /// define multiple values simultaneously.
00369   const EVT *ValueList;
00370 
00371   /// List of uses for this SDNode.
00372   SDUse *UseList;
00373 
00374   /// The number of entries in the Operand/Value list.
00375   unsigned short NumOperands, NumValues;
00376 
00377   /// Source line information.
00378   DebugLoc debugLoc;
00379 
00380   // The ordering of the SDNodes. It roughly corresponds to the ordering of the
00381   // original LLVM instructions.
00382   // This is used for turning off scheduling, because we'll forgo
00383   // the normal scheduling algorithms and output the instructions according to
00384   // this ordering.
00385   unsigned IROrder;
00386 
00387   /// Return a pointer to the specified value type.
00388   static const EVT *getValueTypeList(EVT VT);
00389 
00390   friend class SelectionDAG;
00391   friend struct ilist_traits<SDNode>;
00392 
00393 public:
00394   //===--------------------------------------------------------------------===//
00395   //  Accessors
00396   //
00397 
00398   /// Return the SelectionDAG opcode value for this node. For
00399   /// pre-isel nodes (those for which isMachineOpcode returns false), these
00400   /// are the opcode values in the ISD and <target>ISD namespaces. For
00401   /// post-isel opcodes, see getMachineOpcode.
00402   unsigned getOpcode()  const { return (unsigned short)NodeType; }
00403 
00404   /// Test if this node has a target-specific opcode (in the
00405   /// <target>ISD namespace).
00406   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
00407 
00408   /// Test if this node has a target-specific
00409   /// memory-referencing opcode (in the <target>ISD namespace and
00410   /// greater than FIRST_TARGET_MEMORY_OPCODE).
00411   bool isTargetMemoryOpcode() const {
00412     return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
00413   }
00414 
00415   /// Test if this node is a memory intrinsic (with valid pointer information).
00416   /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
00417   /// non-memory intrinsics (with chains) that are not really instances of
00418   /// MemSDNode. For such nodes, we need some extra state to determine the
00419   /// proper classof relationship.
00420   bool isMemIntrinsic() const {
00421     return (NodeType == ISD::INTRINSIC_W_CHAIN ||
00422             NodeType == ISD::INTRINSIC_VOID) && ((SubclassData >> 13) & 1);
00423   }
00424 
00425   /// Test if this node has a post-isel opcode, directly
00426   /// corresponding to a MachineInstr opcode.
00427   bool isMachineOpcode() const { return NodeType < 0; }
00428 
00429   /// This may only be called if isMachineOpcode returns
00430   /// true. It returns the MachineInstr opcode value that the node's opcode
00431   /// corresponds to.
00432   unsigned getMachineOpcode() const {
00433     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
00434     return ~NodeType;
00435   }
00436 
00437   /// Get this bit.
00438   bool getHasDebugValue() const { return HasDebugValue; }
00439 
00440   /// Set this bit.
00441   void setHasDebugValue(bool b) { HasDebugValue = b; }
00442 
00443   /// Return true if there are no uses of this node.
00444   bool use_empty() const { return UseList == nullptr; }
00445 
00446   /// Return true if there is exactly one use of this node.
00447   bool hasOneUse() const {
00448     return !use_empty() && std::next(use_begin()) == use_end();
00449   }
00450 
00451   /// Return the number of uses of this node. This method takes
00452   /// time proportional to the number of uses.
00453   size_t use_size() const { return std::distance(use_begin(), use_end()); }
00454 
00455   /// Return the unique node id.
00456   int getNodeId() const { return NodeId; }
00457 
00458   /// Set unique node id.
00459   void setNodeId(int Id) { NodeId = Id; }
00460 
00461   /// Return the node ordering.
00462   unsigned getIROrder() const { return IROrder; }
00463 
00464   /// Set the node ordering.
00465   void setIROrder(unsigned Order) { IROrder = Order; }
00466 
00467   /// Return the source location info.
00468   const DebugLoc &getDebugLoc() const { return debugLoc; }
00469 
00470   /// Set source location info.  Try to avoid this, putting
00471   /// it in the constructor is preferable.
00472   void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
00473 
00474   /// This class provides iterator support for SDUse
00475   /// operands that use a specific SDNode.
00476   class use_iterator
00477     : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
00478     SDUse *Op;
00479     explicit use_iterator(SDUse *op) : Op(op) {
00480     }
00481     friend class SDNode;
00482   public:
00483     typedef std::iterator<std::forward_iterator_tag,
00484                           SDUse, ptrdiff_t>::reference reference;
00485     typedef std::iterator<std::forward_iterator_tag,
00486                           SDUse, ptrdiff_t>::pointer pointer;
00487 
00488     use_iterator(const use_iterator &I) : Op(I.Op) {}
00489     use_iterator() : Op(nullptr) {}
00490 
00491     bool operator==(const use_iterator &x) const {
00492       return Op == x.Op;
00493     }
00494     bool operator!=(const use_iterator &x) const {
00495       return !operator==(x);
00496     }
00497 
00498     /// Return true if this iterator is at the end of uses list.
00499     bool atEnd() const { return Op == nullptr; }
00500 
00501     // Iterator traversal: forward iteration only.
00502     use_iterator &operator++() {          // Preincrement
00503       assert(Op && "Cannot increment end iterator!");
00504       Op = Op->getNext();
00505       return *this;
00506     }
00507 
00508     use_iterator operator++(int) {        // Postincrement
00509       use_iterator tmp = *this; ++*this; return tmp;
00510     }
00511 
00512     /// Retrieve a pointer to the current user node.
00513     SDNode *operator*() const {
00514       assert(Op && "Cannot dereference end iterator!");
00515       return Op->getUser();
00516     }
00517 
00518     SDNode *operator->() const { return operator*(); }
00519 
00520     SDUse &getUse() const { return *Op; }
00521 
00522     /// Retrieve the operand # of this use in its user.
00523     unsigned getOperandNo() const {
00524       assert(Op && "Cannot dereference end iterator!");
00525       return (unsigned)(Op - Op->getUser()->OperandList);
00526     }
00527   };
00528 
00529   /// Provide iteration support to walk over all uses of an SDNode.
00530   use_iterator use_begin() const {
00531     return use_iterator(UseList);
00532   }
00533 
00534   static use_iterator use_end() { return use_iterator(nullptr); }
00535 
00536   inline iterator_range<use_iterator> uses() {
00537     return iterator_range<use_iterator>(use_begin(), use_end());
00538   }
00539   inline iterator_range<use_iterator> uses() const {
00540     return iterator_range<use_iterator>(use_begin(), use_end());
00541   }
00542 
00543   /// Return true if there are exactly NUSES uses of the indicated value.
00544   /// This method ignores uses of other values defined by this operation.
00545   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
00546 
00547   /// Return true if there are any use of the indicated value.
00548   /// This method ignores uses of other values defined by this operation.
00549   bool hasAnyUseOfValue(unsigned Value) const;
00550 
00551   /// Return true if this node is the only use of N.
00552   bool isOnlyUserOf(SDNode *N) const;
00553 
00554   /// Return true if this node is an operand of N.
00555   bool isOperandOf(SDNode *N) const;
00556 
00557   /// Return true if this node is a predecessor of N.
00558   /// NOTE: Implemented on top of hasPredecessor and every bit as
00559   /// expensive. Use carefully.
00560   bool isPredecessorOf(const SDNode *N) const {
00561     return N->hasPredecessor(this);
00562   }
00563 
00564   /// Return true if N is a predecessor of this node.
00565   /// N is either an operand of this node, or can be reached by recursively
00566   /// traversing up the operands.
00567   /// NOTE: This is an expensive method. Use it carefully.
00568   bool hasPredecessor(const SDNode *N) const;
00569 
00570   /// Return true if N is a predecessor of this node.
00571   /// N is either an operand of this node, or can be reached by recursively
00572   /// traversing up the operands.
00573   /// In this helper the Visited and worklist sets are held externally to
00574   /// cache predecessors over multiple invocations. If you want to test for
00575   /// multiple predecessors this method is preferable to multiple calls to
00576   /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
00577   /// changes.
00578   /// NOTE: This is still very expensive. Use carefully.
00579   bool hasPredecessorHelper(const SDNode *N,
00580                             SmallPtrSetImpl<const SDNode *> &Visited,
00581                             SmallVectorImpl<const SDNode *> &Worklist) const;
00582 
00583   /// Return the number of values used by this operation.
00584   unsigned getNumOperands() const { return NumOperands; }
00585 
00586   /// Helper method returns the integer value of a ConstantSDNode operand.
00587   uint64_t getConstantOperandVal(unsigned Num) const;
00588 
00589   const SDValue &getOperand(unsigned Num) const {
00590     assert(Num < NumOperands && "Invalid child # of SDNode!");
00591     return OperandList[Num];
00592   }
00593 
00594   typedef SDUse* op_iterator;
00595   op_iterator op_begin() const { return OperandList; }
00596   op_iterator op_end() const { return OperandList+NumOperands; }
00597   ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
00598 
00599   SDVTList getVTList() const {
00600     SDVTList X = { ValueList, NumValues };
00601     return X;
00602   }
00603 
00604   /// If this node has a glue operand, return the node
00605   /// to which the glue operand points. Otherwise return NULL.
00606   SDNode *getGluedNode() const {
00607     if (getNumOperands() != 0 &&
00608       getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
00609       return getOperand(getNumOperands()-1).getNode();
00610     return nullptr;
00611   }
00612 
00613   // If this is a pseudo op, like copyfromreg, look to see if there is a
00614   // real target node glued to it.  If so, return the target node.
00615   const SDNode *getGluedMachineNode() const {
00616     const SDNode *FoundNode = this;
00617 
00618     // Climb up glue edges until a machine-opcode node is found, or the
00619     // end of the chain is reached.
00620     while (!FoundNode->isMachineOpcode()) {
00621       const SDNode *N = FoundNode->getGluedNode();
00622       if (!N) break;
00623       FoundNode = N;
00624     }
00625 
00626     return FoundNode;
00627   }
00628 
00629   /// If this node has a glue value with a user, return
00630   /// the user (there is at most one). Otherwise return NULL.
00631   SDNode *getGluedUser() const {
00632     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
00633       if (UI.getUse().get().getValueType() == MVT::Glue)
00634         return *UI;
00635     return nullptr;
00636   }
00637 
00638   /// Return the number of values defined/returned by this operator.
00639   unsigned getNumValues() const { return NumValues; }
00640 
00641   /// Return the type of a specified result.
00642   EVT getValueType(unsigned ResNo) const {
00643     assert(ResNo < NumValues && "Illegal result number!");
00644     return ValueList[ResNo];
00645   }
00646 
00647   /// Return the type of a specified result as a simple type.
00648   MVT getSimpleValueType(unsigned ResNo) const {
00649     return getValueType(ResNo).getSimpleVT();
00650   }
00651 
00652   /// Returns MVT::getSizeInBits(getValueType(ResNo)).
00653   unsigned getValueSizeInBits(unsigned ResNo) const {
00654     return getValueType(ResNo).getSizeInBits();
00655   }
00656 
00657   typedef const EVT* value_iterator;
00658   value_iterator value_begin() const { return ValueList; }
00659   value_iterator value_end() const { return ValueList+NumValues; }
00660 
00661   /// Return the opcode of this operation for printing.
00662   std::string getOperationName(const SelectionDAG *G = nullptr) const;
00663   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
00664   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
00665   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
00666   void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
00667   void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
00668 
00669   /// Print a SelectionDAG node and all children down to
00670   /// the leaves.  The given SelectionDAG allows target-specific nodes
00671   /// to be printed in human-readable form.  Unlike printr, this will
00672   /// print the whole DAG, including children that appear multiple
00673   /// times.
00674   ///
00675   void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
00676 
00677   /// Print a SelectionDAG node and children up to
00678   /// depth "depth."  The given SelectionDAG allows target-specific
00679   /// nodes to be printed in human-readable form.  Unlike printr, this
00680   /// will print children that appear multiple times wherever they are
00681   /// used.
00682   ///
00683   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
00684                        unsigned depth = 100) const;
00685 
00686 
00687   /// Dump this node, for debugging.
00688   void dump() const;
00689 
00690   /// Dump (recursively) this node and its use-def subgraph.
00691   void dumpr() const;
00692 
00693   /// Dump this node, for debugging.
00694   /// The given SelectionDAG allows target-specific nodes to be printed
00695   /// in human-readable form.
00696   void dump(const SelectionDAG *G) const;
00697 
00698   /// Dump (recursively) this node and its use-def subgraph.
00699   /// The given SelectionDAG allows target-specific nodes to be printed
00700   /// in human-readable form.
00701   void dumpr(const SelectionDAG *G) const;
00702 
00703   /// printrFull to dbgs().  The given SelectionDAG allows
00704   /// target-specific nodes to be printed in human-readable form.
00705   /// Unlike dumpr, this will print the whole DAG, including children
00706   /// that appear multiple times.
00707   void dumprFull(const SelectionDAG *G = nullptr) const;
00708 
00709   /// printrWithDepth to dbgs().  The given
00710   /// SelectionDAG allows target-specific nodes to be printed in
00711   /// human-readable form.  Unlike dumpr, this will print children
00712   /// that appear multiple times wherever they are used.
00713   ///
00714   void dumprWithDepth(const SelectionDAG *G = nullptr,
00715                       unsigned depth = 100) const;
00716 
00717   /// Gather unique data for the node.
00718   void Profile(FoldingSetNodeID &ID) const;
00719 
00720   /// This method should only be used by the SDUse class.
00721   void addUse(SDUse &U) { U.addToList(&UseList); }
00722 
00723 protected:
00724   static SDVTList getSDVTList(EVT VT) {
00725     SDVTList Ret = { getValueTypeList(VT), 1 };
00726     return Ret;
00727   }
00728 
00729   SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00730          ArrayRef<SDValue> Ops)
00731       : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
00732         SubclassData(0), NodeId(-1),
00733         OperandList(Ops.size() ? new SDUse[Ops.size()] : nullptr),
00734         ValueList(VTs.VTs), UseList(nullptr), NumOperands(Ops.size()),
00735         NumValues(VTs.NumVTs), debugLoc(std::move(dl)), IROrder(Order) {
00736     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
00737     assert(NumOperands == Ops.size() &&
00738            "NumOperands wasn't wide enough for its operands!");
00739     assert(NumValues == VTs.NumVTs &&
00740            "NumValues wasn't wide enough for its operands!");
00741     for (unsigned i = 0; i != Ops.size(); ++i) {
00742       assert(OperandList && "no operands available");
00743       OperandList[i].setUser(this);
00744       OperandList[i].setInitial(Ops[i]);
00745     }
00746     checkForCycles(this);
00747   }
00748 
00749   /// This constructor adds no operands itself; operands can be
00750   /// set later with InitOperands.
00751   SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
00752       : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
00753         SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
00754         UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs),
00755         debugLoc(std::move(dl)), IROrder(Order) {
00756     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
00757     assert(NumValues == VTs.NumVTs &&
00758            "NumValues wasn't wide enough for its operands!");
00759   }
00760 
00761   /// Initialize the operands list of this with 1 operand.
00762   void InitOperands(SDUse *Ops, const SDValue &Op0) {
00763     Ops[0].setUser(this);
00764     Ops[0].setInitial(Op0);
00765     NumOperands = 1;
00766     OperandList = Ops;
00767     checkForCycles(this);
00768   }
00769 
00770   /// Initialize the operands list of this with 2 operands.
00771   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
00772     Ops[0].setUser(this);
00773     Ops[0].setInitial(Op0);
00774     Ops[1].setUser(this);
00775     Ops[1].setInitial(Op1);
00776     NumOperands = 2;
00777     OperandList = Ops;
00778     checkForCycles(this);
00779   }
00780 
00781   /// Initialize the operands list of this with 3 operands.
00782   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
00783                     const SDValue &Op2) {
00784     Ops[0].setUser(this);
00785     Ops[0].setInitial(Op0);
00786     Ops[1].setUser(this);
00787     Ops[1].setInitial(Op1);
00788     Ops[2].setUser(this);
00789     Ops[2].setInitial(Op2);
00790     NumOperands = 3;
00791     OperandList = Ops;
00792     checkForCycles(this);
00793   }
00794 
00795   /// Initialize the operands list of this with 4 operands.
00796   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
00797                     const SDValue &Op2, const SDValue &Op3) {
00798     Ops[0].setUser(this);
00799     Ops[0].setInitial(Op0);
00800     Ops[1].setUser(this);
00801     Ops[1].setInitial(Op1);
00802     Ops[2].setUser(this);
00803     Ops[2].setInitial(Op2);
00804     Ops[3].setUser(this);
00805     Ops[3].setInitial(Op3);
00806     NumOperands = 4;
00807     OperandList = Ops;
00808     checkForCycles(this);
00809   }
00810 
00811   /// Initialize the operands list of this with N operands.
00812   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
00813     for (unsigned i = 0; i != N; ++i) {
00814       Ops[i].setUser(this);
00815       Ops[i].setInitial(Vals[i]);
00816     }
00817     NumOperands = N;
00818     assert(NumOperands == N &&
00819            "NumOperands wasn't wide enough for its operands!");
00820     OperandList = Ops;
00821     checkForCycles(this);
00822   }
00823 
00824   /// Release the operands and set this node to have zero operands.
00825   void DropOperands();
00826 };
00827 
00828 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
00829 /// into SDNode creation functions.
00830 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
00831 /// from the original Instruction, and IROrder is the ordinal position of
00832 /// the instruction.
00833 /// When an SDNode is created after the DAG is being built, both DebugLoc and
00834 /// the IROrder are propagated from the original SDNode.
00835 /// So SDLoc class provides two constructors besides the default one, one to
00836 /// be used by the DAGBuilder, the other to be used by others.
00837 class SDLoc {
00838 private:
00839   // Ptr could be used for either Instruction* or SDNode*. It is used for
00840   // Instruction* if IROrder is not -1.
00841   const void *Ptr;
00842   int IROrder;
00843 
00844 public:
00845   SDLoc() : Ptr(nullptr), IROrder(0) {}
00846   SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
00847     assert(N && "null SDNode");
00848   }
00849   SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
00850     assert(Ptr && "null SDNode");
00851   }
00852   SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
00853     assert(Order >= 0 && "bad IROrder");
00854   }
00855   unsigned getIROrder() {
00856     if (IROrder >= 0 || Ptr == nullptr) {
00857       return (unsigned)IROrder;
00858     }
00859     const SDNode *N = (const SDNode*)(Ptr);
00860     return N->getIROrder();
00861   }
00862   DebugLoc getDebugLoc() {
00863     if (!Ptr) {
00864       return DebugLoc();
00865     }
00866     if (IROrder >= 0) {
00867       const Instruction *I = (const Instruction*)(Ptr);
00868       return I->getDebugLoc();
00869     }
00870     const SDNode *N = (const SDNode*)(Ptr);
00871     return N->getDebugLoc();
00872   }
00873 };
00874 
00875 
00876 // Define inline functions from the SDValue class.
00877 
00878 inline SDValue::SDValue(SDNode *node, unsigned resno)
00879     : Node(node), ResNo(resno) {
00880   assert((!Node || ResNo < Node->getNumValues()) &&
00881          "Invalid result number for the given node!");
00882   assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
00883 }
00884 
00885 inline unsigned SDValue::getOpcode() const {
00886   return Node->getOpcode();
00887 }
00888 inline EVT SDValue::getValueType() const {
00889   return Node->getValueType(ResNo);
00890 }
00891 inline unsigned SDValue::getNumOperands() const {
00892   return Node->getNumOperands();
00893 }
00894 inline const SDValue &SDValue::getOperand(unsigned i) const {
00895   return Node->getOperand(i);
00896 }
00897 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
00898   return Node->getConstantOperandVal(i);
00899 }
00900 inline bool SDValue::isTargetOpcode() const {
00901   return Node->isTargetOpcode();
00902 }
00903 inline bool SDValue::isTargetMemoryOpcode() const {
00904   return Node->isTargetMemoryOpcode();
00905 }
00906 inline bool SDValue::isMachineOpcode() const {
00907   return Node->isMachineOpcode();
00908 }
00909 inline unsigned SDValue::getMachineOpcode() const {
00910   return Node->getMachineOpcode();
00911 }
00912 inline bool SDValue::use_empty() const {
00913   return !Node->hasAnyUseOfValue(ResNo);
00914 }
00915 inline bool SDValue::hasOneUse() const {
00916   return Node->hasNUsesOfValue(1, ResNo);
00917 }
00918 inline const DebugLoc &SDValue::getDebugLoc() const {
00919   return Node->getDebugLoc();
00920 }
00921 inline void SDValue::dump() const {
00922   return Node->dump();
00923 }
00924 inline void SDValue::dumpr() const {
00925   return Node->dumpr();
00926 }
00927 // Define inline functions from the SDUse class.
00928 
00929 inline void SDUse::set(const SDValue &V) {
00930   if (Val.getNode()) removeFromList();
00931   Val = V;
00932   if (V.getNode()) V.getNode()->addUse(*this);
00933 }
00934 
00935 inline void SDUse::setInitial(const SDValue &V) {
00936   Val = V;
00937   V.getNode()->addUse(*this);
00938 }
00939 
00940 inline void SDUse::setNode(SDNode *N) {
00941   if (Val.getNode()) removeFromList();
00942   Val.setNode(N);
00943   if (N) N->addUse(*this);
00944 }
00945 
00946 /// This class is used for single-operand SDNodes.  This is solely
00947 /// to allow co-allocation of node operands with the node itself.
00948 class UnarySDNode : public SDNode {
00949   SDUse Op;
00950 public:
00951   UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00952               SDValue X)
00953     : SDNode(Opc, Order, dl, VTs) {
00954     InitOperands(&Op, X);
00955   }
00956 };
00957 
00958 /// This class is used for two-operand SDNodes.  This is solely
00959 /// to allow co-allocation of node operands with the node itself.
00960 class BinarySDNode : public SDNode {
00961   SDUse Ops[2];
00962 public:
00963   BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00964                SDValue X, SDValue Y)
00965     : SDNode(Opc, Order, dl, VTs) {
00966     InitOperands(Ops, X, Y);
00967   }
00968 };
00969 
00970 /// This class is an extension of BinarySDNode
00971 /// used from those opcodes that have associated extra flags.
00972 class BinaryWithFlagsSDNode : public BinarySDNode {
00973   enum { NUW = (1 << 0), NSW = (1 << 1), EXACT = (1 << 2) };
00974 
00975 public:
00976   BinaryWithFlagsSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00977                         SDValue X, SDValue Y)
00978       : BinarySDNode(Opc, Order, dl, VTs, X, Y) {}
00979   /// Return the SubclassData value, which contains an encoding of the flags.
00980   /// This function should be used to add subclass data to the NodeID value.
00981   unsigned getRawSubclassData() const { return SubclassData; }
00982   void setHasNoUnsignedWrap(bool b) {
00983     SubclassData = (SubclassData & ~NUW) | (b ? NUW : 0);
00984   }
00985   void setHasNoSignedWrap(bool b) {
00986     SubclassData = (SubclassData & ~NSW) | (b ? NSW : 0);
00987   }
00988   void setIsExact(bool b) {
00989     SubclassData = (SubclassData & ~EXACT) | (b ? EXACT : 0);
00990   }
00991   bool hasNoUnsignedWrap() const { return SubclassData & NUW; }
00992   bool hasNoSignedWrap() const { return SubclassData & NSW; }
00993   bool isExact() const { return SubclassData & EXACT; }
00994   static bool classof(const SDNode *N) {
00995     return isBinOpWithFlags(N->getOpcode());
00996   }
00997 };
00998 
00999 /// This class is used for three-operand SDNodes. This is solely
01000 /// to allow co-allocation of node operands with the node itself.
01001 class TernarySDNode : public SDNode {
01002   SDUse Ops[3];
01003 public:
01004   TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01005                 SDValue X, SDValue Y, SDValue Z)
01006     : SDNode(Opc, Order, dl, VTs) {
01007     InitOperands(Ops, X, Y, Z);
01008   }
01009 };
01010 
01011 
01012 /// This class is used to form a handle around another node that
01013 /// is persistent and is updated across invocations of replaceAllUsesWith on its
01014 /// operand.  This node should be directly created by end-users and not added to
01015 /// the AllNodes list.
01016 class HandleSDNode : public SDNode {
01017   SDUse Op;
01018 public:
01019   explicit HandleSDNode(SDValue X)
01020     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
01021     InitOperands(&Op, X);
01022   }
01023   ~HandleSDNode();
01024   const SDValue &getValue() const { return Op; }
01025 };
01026 
01027 class AddrSpaceCastSDNode : public UnarySDNode {
01028 private:
01029   unsigned SrcAddrSpace;
01030   unsigned DestAddrSpace;
01031 
01032 public:
01033   AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
01034                       unsigned SrcAS, unsigned DestAS);
01035 
01036   unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
01037   unsigned getDestAddressSpace() const { return DestAddrSpace; }
01038 
01039   static bool classof(const SDNode *N) {
01040     return N->getOpcode() == ISD::ADDRSPACECAST;
01041   }
01042 };
01043 
01044 /// Abstact virtual class for operations for memory operations
01045 class MemSDNode : public SDNode {
01046 private:
01047   // VT of in-memory value.
01048   EVT MemoryVT;
01049 
01050 protected:
01051   /// Memory reference information.
01052   MachineMemOperand *MMO;
01053 
01054 public:
01055   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01056             EVT MemoryVT, MachineMemOperand *MMO);
01057 
01058   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01059             ArrayRef<SDValue> Ops, EVT MemoryVT, MachineMemOperand *MMO);
01060 
01061   bool readMem() const { return MMO->isLoad(); }
01062   bool writeMem() const { return MMO->isStore(); }
01063 
01064   /// Returns alignment and volatility of the memory access
01065   unsigned getOriginalAlignment() const {
01066     return MMO->getBaseAlignment();
01067   }
01068   unsigned getAlignment() const {
01069     return MMO->getAlignment();
01070   }
01071 
01072   /// Return the SubclassData value, which contains an
01073   /// encoding of the volatile flag, as well as bits used by subclasses. This
01074   /// function should only be used to compute a FoldingSetNodeID value.
01075   unsigned getRawSubclassData() const {
01076     return SubclassData;
01077   }
01078 
01079   // We access subclass data here so that we can check consistency
01080   // with MachineMemOperand information.
01081   bool isVolatile() const { return (SubclassData >> 5) & 1; }
01082   bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
01083   bool isInvariant() const { return (SubclassData >> 7) & 1; }
01084 
01085   AtomicOrdering getOrdering() const {
01086     return AtomicOrdering((SubclassData >> 8) & 15);
01087   }
01088   SynchronizationScope getSynchScope() const {
01089     return SynchronizationScope((SubclassData >> 12) & 1);
01090   }
01091 
01092   // Returns the offset from the location of the access.
01093   int64_t getSrcValueOffset() const { return MMO->getOffset(); }
01094 
01095   /// Returns the AA info that describes the dereference.
01096   AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
01097 
01098   /// Returns the Ranges that describes the dereference.
01099   const MDNode *getRanges() const { return MMO->getRanges(); }
01100 
01101   /// Return the type of the in-memory value.
01102   EVT getMemoryVT() const { return MemoryVT; }
01103 
01104   /// Return a MachineMemOperand object describing the memory
01105   /// reference performed by operation.
01106   MachineMemOperand *getMemOperand() const { return MMO; }
01107 
01108   const MachinePointerInfo &getPointerInfo() const {
01109     return MMO->getPointerInfo();
01110   }
01111 
01112   /// Return the address space for the associated pointer
01113   unsigned getAddressSpace() const {
01114     return getPointerInfo().getAddrSpace();
01115   }
01116 
01117   /// Update this MemSDNode's MachineMemOperand information
01118   /// to reflect the alignment of NewMMO, if it has a greater alignment.
01119   /// This must only be used when the new alignment applies to all users of
01120   /// this MachineMemOperand.
01121   void refineAlignment(const MachineMemOperand *NewMMO) {
01122     MMO->refineAlignment(NewMMO);
01123   }
01124 
01125   const SDValue &getChain() const { return getOperand(0); }
01126   const SDValue &getBasePtr() const {
01127     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
01128   }
01129 
01130   // Methods to support isa and dyn_cast
01131   static bool classof(const SDNode *N) {
01132     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
01133     // with either an intrinsic or a target opcode.
01134     return N->getOpcode() == ISD::LOAD                ||
01135            N->getOpcode() == ISD::STORE               ||
01136            N->getOpcode() == ISD::PREFETCH            ||
01137            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
01138            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
01139            N->getOpcode() == ISD::ATOMIC_SWAP         ||
01140            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
01141            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
01142            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
01143            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
01144            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
01145            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
01146            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
01147            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
01148            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
01149            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
01150            N->getOpcode() == ISD::ATOMIC_LOAD         ||
01151            N->getOpcode() == ISD::ATOMIC_STORE        ||
01152            N->getOpcode() == ISD::MLOAD               ||
01153            N->getOpcode() == ISD::MSTORE              ||
01154            N->isMemIntrinsic()                        ||
01155            N->isTargetMemoryOpcode();
01156   }
01157 };
01158 
01159 /// A SDNode reprenting atomic operations.
01160 class AtomicSDNode : public MemSDNode {
01161   SDUse Ops[4];
01162 
01163   /// For cmpxchg instructions, the ordering requirements when a store does not
01164   /// occur.
01165   AtomicOrdering FailureOrdering;
01166 
01167   void InitAtomic(AtomicOrdering SuccessOrdering,
01168                   AtomicOrdering FailureOrdering,
01169                   SynchronizationScope SynchScope) {
01170     // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
01171     assert((SuccessOrdering & 15) == SuccessOrdering &&
01172            "Ordering may not require more than 4 bits!");
01173     assert((FailureOrdering & 15) == FailureOrdering &&
01174            "Ordering may not require more than 4 bits!");
01175     assert((SynchScope & 1) == SynchScope &&
01176            "SynchScope may not require more than 1 bit!");
01177     SubclassData |= SuccessOrdering << 8;
01178     SubclassData |= SynchScope << 12;
01179     this->FailureOrdering = FailureOrdering;
01180     assert(getSuccessOrdering() == SuccessOrdering &&
01181            "Ordering encoding error!");
01182     assert(getFailureOrdering() == FailureOrdering &&
01183            "Ordering encoding error!");
01184     assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
01185   }
01186 
01187 public:
01188   // Opc:   opcode for atomic
01189   // VTL:    value type list
01190   // Chain:  memory chain for operaand
01191   // Ptr:    address to update as a SDValue
01192   // Cmp:    compare value
01193   // Swp:    swap value
01194   // SrcVal: address to update as a Value (used for MemOperand)
01195   // Align:  alignment of memory
01196   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
01197                EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
01198                MachineMemOperand *MMO, AtomicOrdering Ordering,
01199                SynchronizationScope SynchScope)
01200       : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01201     InitAtomic(Ordering, Ordering, SynchScope);
01202     InitOperands(Ops, Chain, Ptr, Cmp, Swp);
01203   }
01204   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
01205                EVT MemVT,
01206                SDValue Chain, SDValue Ptr,
01207                SDValue Val, MachineMemOperand *MMO,
01208                AtomicOrdering Ordering, SynchronizationScope SynchScope)
01209     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01210     InitAtomic(Ordering, Ordering, SynchScope);
01211     InitOperands(Ops, Chain, Ptr, Val);
01212   }
01213   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
01214                EVT MemVT,
01215                SDValue Chain, SDValue Ptr,
01216                MachineMemOperand *MMO,
01217                AtomicOrdering Ordering, SynchronizationScope SynchScope)
01218     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01219     InitAtomic(Ordering, Ordering, SynchScope);
01220     InitOperands(Ops, Chain, Ptr);
01221   }
01222   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
01223                const SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
01224                MachineMemOperand *MMO,
01225                AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
01226                SynchronizationScope SynchScope)
01227     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01228     InitAtomic(SuccessOrdering, FailureOrdering, SynchScope);
01229     assert((DynOps || NumOps <= array_lengthof(Ops)) &&
01230            "Too many ops for internal storage!");
01231     InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
01232   }
01233 
01234   const SDValue &getBasePtr() const { return getOperand(1); }
01235   const SDValue &getVal() const { return getOperand(2); }
01236 
01237   AtomicOrdering getSuccessOrdering() const {
01238     return getOrdering();
01239   }
01240 
01241   // Not quite enough room in SubclassData for everything, so failure gets its
01242   // own field.
01243   AtomicOrdering getFailureOrdering() const {
01244     return FailureOrdering;
01245   }
01246 
01247   bool isCompareAndSwap() const {
01248     unsigned Op = getOpcode();
01249     return Op == ISD::ATOMIC_CMP_SWAP || Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
01250   }
01251 
01252   // Methods to support isa and dyn_cast
01253   static bool classof(const SDNode *N) {
01254     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
01255            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
01256            N->getOpcode() == ISD::ATOMIC_SWAP         ||
01257            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
01258            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
01259            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
01260            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
01261            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
01262            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
01263            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
01264            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
01265            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
01266            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
01267            N->getOpcode() == ISD::ATOMIC_LOAD         ||
01268            N->getOpcode() == ISD::ATOMIC_STORE;
01269   }
01270 };
01271 
01272 /// This SDNode is used for target intrinsics that touch
01273 /// memory and need an associated MachineMemOperand. Its opcode may be
01274 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
01275 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
01276 class MemIntrinsicSDNode : public MemSDNode {
01277 public:
01278   MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01279                      ArrayRef<SDValue> Ops, EVT MemoryVT,
01280                      MachineMemOperand *MMO)
01281     : MemSDNode(Opc, Order, dl, VTs, Ops, MemoryVT, MMO) {
01282     SubclassData |= 1u << 13;
01283   }
01284 
01285   // Methods to support isa and dyn_cast
01286   static bool classof(const SDNode *N) {
01287     // We lower some target intrinsics to their target opcode
01288     // early a node with a target opcode can be of this class
01289     return N->isMemIntrinsic()             ||
01290            N->getOpcode() == ISD::PREFETCH ||
01291            N->isTargetMemoryOpcode();
01292   }
01293 };
01294 
01295 /// This SDNode is used to implement the code generator
01296 /// support for the llvm IR shufflevector instruction.  It combines elements
01297 /// from two input vectors into a new input vector, with the selection and
01298 /// ordering of elements determined by an array of integers, referred to as
01299 /// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
01300 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
01301 /// An index of -1 is treated as undef, such that the code generator may put
01302 /// any value in the corresponding element of the result.
01303 class ShuffleVectorSDNode : public SDNode {
01304   SDUse Ops[2];
01305 
01306   // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
01307   // is freed when the SelectionDAG object is destroyed.
01308   const int *Mask;
01309 protected:
01310   friend class SelectionDAG;
01311   ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
01312                       SDValue N2, const int *M)
01313     : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
01314     InitOperands(Ops, N1, N2);
01315   }
01316 public:
01317 
01318   ArrayRef<int> getMask() const {
01319     EVT VT = getValueType(0);
01320     return makeArrayRef(Mask, VT.getVectorNumElements());
01321   }
01322   int getMaskElt(unsigned Idx) const {
01323     assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
01324     return Mask[Idx];
01325   }
01326 
01327   bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
01328   int  getSplatIndex() const {
01329     assert(isSplat() && "Cannot get splat index for non-splat!");
01330     EVT VT = getValueType(0);
01331     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
01332       if (Mask[i] >= 0)
01333         return Mask[i];
01334     }
01335     llvm_unreachable("Splat with all undef indices?");
01336   }
01337   static bool isSplatMask(const int *Mask, EVT VT);
01338 
01339   /// Change values in a shuffle permute mask assuming
01340   /// the two vector operands have swapped position.
01341   static void commuteMask(SmallVectorImpl<int> &Mask) {
01342     unsigned NumElems = Mask.size();
01343     for (unsigned i = 0; i != NumElems; ++i) {
01344       int idx = Mask[i];
01345       if (idx < 0)
01346         continue;
01347       else if (idx < (int)NumElems)
01348         Mask[i] = idx + NumElems;
01349       else
01350         Mask[i] = idx - NumElems;
01351     }
01352   }
01353 
01354   static bool classof(const SDNode *N) {
01355     return N->getOpcode() == ISD::VECTOR_SHUFFLE;
01356   }
01357 };
01358 
01359 class ConstantSDNode : public SDNode {
01360   const ConstantInt *Value;
01361   friend class SelectionDAG;
01362   ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
01363     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
01364              0, DebugLoc(), getSDVTList(VT)), Value(val) {
01365     SubclassData |= (uint16_t)isOpaque;
01366   }
01367 public:
01368 
01369   const ConstantInt *getConstantIntValue() const { return Value; }
01370   const APInt &getAPIntValue() const { return Value->getValue(); }
01371   uint64_t getZExtValue() const { return Value->getZExtValue(); }
01372   int64_t getSExtValue() const { return Value->getSExtValue(); }
01373 
01374   bool isOne() const { return Value->isOne(); }
01375   bool isNullValue() const { return Value->isNullValue(); }
01376   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
01377 
01378   bool isOpaque() const { return SubclassData & 1; }
01379 
01380   static bool classof(const SDNode *N) {
01381     return N->getOpcode() == ISD::Constant ||
01382            N->getOpcode() == ISD::TargetConstant;
01383   }
01384 };
01385 
01386 class ConstantFPSDNode : public SDNode {
01387   const ConstantFP *Value;
01388   friend class SelectionDAG;
01389   ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
01390     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
01391              0, DebugLoc(), getSDVTList(VT)), Value(val) {
01392   }
01393 public:
01394 
01395   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
01396   const ConstantFP *getConstantFPValue() const { return Value; }
01397 
01398   /// Return true if the value is positive or negative zero.
01399   bool isZero() const { return Value->isZero(); }
01400 
01401   /// Return true if the value is a NaN.
01402   bool isNaN() const { return Value->isNaN(); }
01403 
01404   /// Return true if the value is an infinity
01405   bool isInfinity() const { return Value->isInfinity(); }
01406 
01407   /// Return true if the value is negative.
01408   bool isNegative() const { return Value->isNegative(); }
01409 
01410   /// We don't rely on operator== working on double values, as
01411   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
01412   /// As such, this method can be used to do an exact bit-for-bit comparison of
01413   /// two floating point values.
01414 
01415   /// We leave the version with the double argument here because it's just so
01416   /// convenient to write "2.0" and the like.  Without this function we'd
01417   /// have to duplicate its logic everywhere it's called.
01418   bool isExactlyValue(double V) const {
01419     bool ignored;
01420     APFloat Tmp(V);
01421     Tmp.convert(Value->getValueAPF().getSemantics(),
01422                 APFloat::rmNearestTiesToEven, &ignored);
01423     return isExactlyValue(Tmp);
01424   }
01425   bool isExactlyValue(const APFloat& V) const;
01426 
01427   static bool isValueValidForType(EVT VT, const APFloat& Val);
01428 
01429   static bool classof(const SDNode *N) {
01430     return N->getOpcode() == ISD::ConstantFP ||
01431            N->getOpcode() == ISD::TargetConstantFP;
01432   }
01433 };
01434 
01435 class GlobalAddressSDNode : public SDNode {
01436   const GlobalValue *TheGlobal;
01437   int64_t Offset;
01438   unsigned char TargetFlags;
01439   friend class SelectionDAG;
01440   GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
01441                       const GlobalValue *GA, EVT VT, int64_t o,
01442                       unsigned char TargetFlags);
01443 public:
01444 
01445   const GlobalValue *getGlobal() const { return TheGlobal; }
01446   int64_t getOffset() const { return Offset; }
01447   unsigned char getTargetFlags() const { return TargetFlags; }
01448   // Return the address space this GlobalAddress belongs to.
01449   unsigned getAddressSpace() const;
01450 
01451   static bool classof(const SDNode *N) {
01452     return N->getOpcode() == ISD::GlobalAddress ||
01453            N->getOpcode() == ISD::TargetGlobalAddress ||
01454            N->getOpcode() == ISD::GlobalTLSAddress ||
01455            N->getOpcode() == ISD::TargetGlobalTLSAddress;
01456   }
01457 };
01458 
01459 class FrameIndexSDNode : public SDNode {
01460   int FI;
01461   friend class SelectionDAG;
01462   FrameIndexSDNode(int fi, EVT VT, bool isTarg)
01463     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
01464       0, DebugLoc(), getSDVTList(VT)), FI(fi) {
01465   }
01466 public:
01467 
01468   int getIndex() const { return FI; }
01469 
01470   static bool classof(const SDNode *N) {
01471     return N->getOpcode() == ISD::FrameIndex ||
01472            N->getOpcode() == ISD::TargetFrameIndex;
01473   }
01474 };
01475 
01476 class JumpTableSDNode : public SDNode {
01477   int JTI;
01478   unsigned char TargetFlags;
01479   friend class SelectionDAG;
01480   JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
01481     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
01482       0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
01483   }
01484 public:
01485 
01486   int getIndex() const { return JTI; }
01487   unsigned char getTargetFlags() const { return TargetFlags; }
01488 
01489   static bool classof(const SDNode *N) {
01490     return N->getOpcode() == ISD::JumpTable ||
01491            N->getOpcode() == ISD::TargetJumpTable;
01492   }
01493 };
01494 
01495 class ConstantPoolSDNode : public SDNode {
01496   union {
01497     const Constant *ConstVal;
01498     MachineConstantPoolValue *MachineCPVal;
01499   } Val;
01500   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
01501   unsigned Alignment;  // Minimum alignment requirement of CP (not log2 value).
01502   unsigned char TargetFlags;
01503   friend class SelectionDAG;
01504   ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
01505                      unsigned Align, unsigned char TF)
01506     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
01507              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
01508              TargetFlags(TF) {
01509     assert(Offset >= 0 && "Offset is too large");
01510     Val.ConstVal = c;
01511   }
01512   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
01513                      EVT VT, int o, unsigned Align, unsigned char TF)
01514     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
01515              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
01516              TargetFlags(TF) {
01517     assert(Offset >= 0 && "Offset is too large");
01518     Val.MachineCPVal = v;
01519     Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
01520   }
01521 public:
01522 
01523   bool isMachineConstantPoolEntry() const {
01524     return Offset < 0;
01525   }
01526 
01527   const Constant *getConstVal() const {
01528     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
01529     return Val.ConstVal;
01530   }
01531 
01532   MachineConstantPoolValue *getMachineCPVal() const {
01533     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
01534     return Val.MachineCPVal;
01535   }
01536 
01537   int getOffset() const {
01538     return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
01539   }
01540 
01541   // Return the alignment of this constant pool object, which is either 0 (for
01542   // default alignment) or the desired value.
01543   unsigned getAlignment() const { return Alignment; }
01544   unsigned char getTargetFlags() const { return TargetFlags; }
01545 
01546   Type *getType() const;
01547 
01548   static bool classof(const SDNode *N) {
01549     return N->getOpcode() == ISD::ConstantPool ||
01550            N->getOpcode() == ISD::TargetConstantPool;
01551   }
01552 };
01553 
01554 /// Completely target-dependent object reference.
01555 class TargetIndexSDNode : public SDNode {
01556   unsigned char TargetFlags;
01557   int Index;
01558   int64_t Offset;
01559   friend class SelectionDAG;
01560 public:
01561 
01562   TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
01563     : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
01564       TargetFlags(TF), Index(Idx), Offset(Ofs) {}
01565 public:
01566 
01567   unsigned char getTargetFlags() const { return TargetFlags; }
01568   int getIndex() const { return Index; }
01569   int64_t getOffset() const { return Offset; }
01570 
01571   static bool classof(const SDNode *N) {
01572     return N->getOpcode() == ISD::TargetIndex;
01573   }
01574 };
01575 
01576 class BasicBlockSDNode : public SDNode {
01577   MachineBasicBlock *MBB;
01578   friend class SelectionDAG;
01579   /// Debug info is meaningful and potentially useful here, but we create
01580   /// blocks out of order when they're jumped to, which makes it a bit
01581   /// harder.  Let's see if we need it first.
01582   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
01583     : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
01584   {}
01585 public:
01586 
01587   MachineBasicBlock *getBasicBlock() const { return MBB; }
01588 
01589   static bool classof(const SDNode *N) {
01590     return N->getOpcode() == ISD::BasicBlock;
01591   }
01592 };
01593 
01594 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
01595 class BuildVectorSDNode : public SDNode {
01596   // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
01597   explicit BuildVectorSDNode() = delete;
01598 public:
01599   /// Check if this is a constant splat, and if so, find the
01600   /// smallest element size that splats the vector.  If MinSplatBits is
01601   /// nonzero, the element size must be at least that large.  Note that the
01602   /// splat element may be the entire vector (i.e., a one element vector).
01603   /// Returns the splat element value in SplatValue.  Any undefined bits in
01604   /// that value are zero, and the corresponding bits in the SplatUndef mask
01605   /// are set.  The SplatBitSize value is set to the splat element size in
01606   /// bits.  HasAnyUndefs is set to true if any bits in the vector are
01607   /// undefined.  isBigEndian describes the endianness of the target.
01608   bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
01609                        unsigned &SplatBitSize, bool &HasAnyUndefs,
01610                        unsigned MinSplatBits = 0,
01611                        bool isBigEndian = false) const;
01612 
01613   /// \brief Returns the splatted value or a null value if this is not a splat.
01614   ///
01615   /// If passed a non-null UndefElements bitvector, it will resize it to match
01616   /// the vector width and set the bits where elements are undef.
01617   SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
01618 
01619   /// \brief Returns the splatted constant or null if this is not a constant
01620   /// splat.
01621   ///
01622   /// If passed a non-null UndefElements bitvector, it will resize it to match
01623   /// the vector width and set the bits where elements are undef.
01624   ConstantSDNode *
01625   getConstantSplatNode(BitVector *UndefElements = nullptr) const;
01626 
01627   /// \brief Returns the splatted constant FP or null if this is not a constant
01628   /// FP splat.
01629   ///
01630   /// If passed a non-null UndefElements bitvector, it will resize it to match
01631   /// the vector width and set the bits where elements are undef.
01632   ConstantFPSDNode *
01633   getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
01634 
01635   bool isConstant() const;
01636 
01637   static inline bool classof(const SDNode *N) {
01638     return N->getOpcode() == ISD::BUILD_VECTOR;
01639   }
01640 };
01641 
01642 /// An SDNode that holds an arbitrary LLVM IR Value. This is
01643 /// used when the SelectionDAG needs to make a simple reference to something
01644 /// in the LLVM IR representation.
01645 ///
01646 class SrcValueSDNode : public SDNode {
01647   const Value *V;
01648   friend class SelectionDAG;
01649   /// Create a SrcValue for a general value.
01650   explicit SrcValueSDNode(const Value *v)
01651     : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
01652 
01653 public:
01654   /// Return the contained Value.
01655   const Value *getValue() const { return V; }
01656 
01657   static bool classof(const SDNode *N) {
01658     return N->getOpcode() == ISD::SRCVALUE;
01659   }
01660 };
01661 
01662 class MDNodeSDNode : public SDNode {
01663   const MDNode *MD;
01664   friend class SelectionDAG;
01665   explicit MDNodeSDNode(const MDNode *md)
01666   : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
01667   {}
01668 public:
01669 
01670   const MDNode *getMD() const { return MD; }
01671 
01672   static bool classof(const SDNode *N) {
01673     return N->getOpcode() == ISD::MDNODE_SDNODE;
01674   }
01675 };
01676 
01677 class RegisterSDNode : public SDNode {
01678   unsigned Reg;
01679   friend class SelectionDAG;
01680   RegisterSDNode(unsigned reg, EVT VT)
01681     : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
01682   }
01683 public:
01684 
01685   unsigned getReg() const { return Reg; }
01686 
01687   static bool classof(const SDNode *N) {
01688     return N->getOpcode() == ISD::Register;
01689   }
01690 };
01691 
01692 class RegisterMaskSDNode : public SDNode {
01693   // The memory for RegMask is not owned by the node.
01694   const uint32_t *RegMask;
01695   friend class SelectionDAG;
01696   RegisterMaskSDNode(const uint32_t *mask)
01697     : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
01698       RegMask(mask) {}
01699 public:
01700 
01701   const uint32_t *getRegMask() const { return RegMask; }
01702 
01703   static bool classof(const SDNode *N) {
01704     return N->getOpcode() == ISD::RegisterMask;
01705   }
01706 };
01707 
01708 class BlockAddressSDNode : public SDNode {
01709   const BlockAddress *BA;
01710   int64_t Offset;
01711   unsigned char TargetFlags;
01712   friend class SelectionDAG;
01713   BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
01714                      int64_t o, unsigned char Flags)
01715     : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
01716              BA(ba), Offset(o), TargetFlags(Flags) {
01717   }
01718 public:
01719   const BlockAddress *getBlockAddress() const { return BA; }
01720   int64_t getOffset() const { return Offset; }
01721   unsigned char getTargetFlags() const { return TargetFlags; }
01722 
01723   static bool classof(const SDNode *N) {
01724     return N->getOpcode() == ISD::BlockAddress ||
01725            N->getOpcode() == ISD::TargetBlockAddress;
01726   }
01727 };
01728 
01729 class EHLabelSDNode : public SDNode {
01730   SDUse Chain;
01731   MCSymbol *Label;
01732   friend class SelectionDAG;
01733   EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
01734     : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
01735     InitOperands(&Chain, ch);
01736   }
01737 public:
01738   MCSymbol *getLabel() const { return Label; }
01739 
01740   static bool classof(const SDNode *N) {
01741     return N->getOpcode() == ISD::EH_LABEL;
01742   }
01743 };
01744 
01745 class ExternalSymbolSDNode : public SDNode {
01746   const char *Symbol;
01747   unsigned char TargetFlags;
01748 
01749   friend class SelectionDAG;
01750   ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
01751     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
01752              0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
01753   }
01754 public:
01755 
01756   const char *getSymbol() const { return Symbol; }
01757   unsigned char getTargetFlags() const { return TargetFlags; }
01758 
01759   static bool classof(const SDNode *N) {
01760     return N->getOpcode() == ISD::ExternalSymbol ||
01761            N->getOpcode() == ISD::TargetExternalSymbol;
01762   }
01763 };
01764 
01765 class CondCodeSDNode : public SDNode {
01766   ISD::CondCode Condition;
01767   friend class SelectionDAG;
01768   explicit CondCodeSDNode(ISD::CondCode Cond)
01769     : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
01770       Condition(Cond) {
01771   }
01772 public:
01773 
01774   ISD::CondCode get() const { return Condition; }
01775 
01776   static bool classof(const SDNode *N) {
01777     return N->getOpcode() == ISD::CONDCODE;
01778   }
01779 };
01780 
01781 /// NOTE: avoid using this node as this may disappear in the
01782 /// future and most targets don't support it.
01783 class CvtRndSatSDNode : public SDNode {
01784   ISD::CvtCode CvtCode;
01785   friend class SelectionDAG;
01786   explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
01787                            ArrayRef<SDValue> Ops, ISD::CvtCode Code)
01788     : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops),
01789       CvtCode(Code) {
01790     assert(Ops.size() == 5 && "wrong number of operations");
01791   }
01792 public:
01793   ISD::CvtCode getCvtCode() const { return CvtCode; }
01794 
01795   static bool classof(const SDNode *N) {
01796     return N->getOpcode() == ISD::CONVERT_RNDSAT;
01797   }
01798 };
01799 
01800 /// This class is used to represent EVT's, which are used
01801 /// to parameterize some operations.
01802 class VTSDNode : public SDNode {
01803   EVT ValueType;
01804   friend class SelectionDAG;
01805   explicit VTSDNode(EVT VT)
01806     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
01807       ValueType(VT) {
01808   }
01809 public:
01810 
01811   EVT getVT() const { return ValueType; }
01812 
01813   static bool classof(const SDNode *N) {
01814     return N->getOpcode() == ISD::VALUETYPE;
01815   }
01816 };
01817 
01818 /// Base class for LoadSDNode and StoreSDNode
01819 class LSBaseSDNode : public MemSDNode {
01820   //! Operand array for load and store
01821   /*!
01822     \note Moving this array to the base class captures more
01823     common functionality shared between LoadSDNode and
01824     StoreSDNode
01825    */
01826   SDUse Ops[4];
01827 public:
01828   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
01829                SDValue *Operands, unsigned numOperands,
01830                SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
01831                MachineMemOperand *MMO)
01832     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
01833     SubclassData |= AM << 2;
01834     assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
01835     InitOperands(Ops, Operands, numOperands);
01836     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
01837            "Only indexed loads and stores have a non-undef offset operand");
01838   }
01839 
01840   const SDValue &getOffset() const {
01841     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
01842   }
01843 
01844   /// Return the addressing mode for this load or store:
01845   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
01846   ISD::MemIndexedMode getAddressingMode() const {
01847     return ISD::MemIndexedMode((SubclassData >> 2) & 7);
01848   }
01849 
01850   /// Return true if this is a pre/post inc/dec load/store.
01851   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
01852 
01853   /// Return true if this is NOT a pre/post inc/dec load/store.
01854   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
01855 
01856   static bool classof(const SDNode *N) {
01857     return N->getOpcode() == ISD::LOAD ||
01858            N->getOpcode() == ISD::STORE;
01859   }
01860 };
01861 
01862 /// This class is used to represent ISD::LOAD nodes.
01863 class LoadSDNode : public LSBaseSDNode {
01864   friend class SelectionDAG;
01865   LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
01866              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
01867              MachineMemOperand *MMO)
01868     : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
01869     SubclassData |= (unsigned short)ETy;
01870     assert(getExtensionType() == ETy && "LoadExtType encoding error!");
01871     assert(readMem() && "Load MachineMemOperand is not a load!");
01872     assert(!writeMem() && "Load MachineMemOperand is a store!");
01873   }
01874 public:
01875 
01876   /// Return whether this is a plain node,
01877   /// or one of the varieties of value-extending loads.
01878   ISD::LoadExtType getExtensionType() const {
01879     return ISD::LoadExtType(SubclassData & 3);
01880   }
01881 
01882   const SDValue &getBasePtr() const { return getOperand(1); }
01883   const SDValue &getOffset() const { return getOperand(2); }
01884 
01885   static bool classof(const SDNode *N) {
01886     return N->getOpcode() == ISD::LOAD;
01887   }
01888 };
01889 
01890 /// This class is used to represent ISD::STORE nodes.
01891 class StoreSDNode : public LSBaseSDNode {
01892   friend class SelectionDAG;
01893   StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
01894               SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
01895               MachineMemOperand *MMO)
01896     : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
01897                    VTs, AM, MemVT, MMO) {
01898     SubclassData |= (unsigned short)isTrunc;
01899     assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
01900     assert(!readMem() && "Store MachineMemOperand is a load!");
01901     assert(writeMem() && "Store MachineMemOperand is not a store!");
01902   }
01903 public:
01904 
01905   /// Return true if the op does a truncation before store.
01906   /// For integers this is the same as doing a TRUNCATE and storing the result.
01907   /// For floats, it is the same as doing an FP_ROUND and storing the result.
01908   bool isTruncatingStore() const { return SubclassData & 1; }
01909 
01910   const SDValue &getValue() const { return getOperand(1); }
01911   const SDValue &getBasePtr() const { return getOperand(2); }
01912   const SDValue &getOffset() const { return getOperand(3); }
01913 
01914   static bool classof(const SDNode *N) {
01915     return N->getOpcode() == ISD::STORE;
01916   }
01917 };
01918 
01919 /// This base class is used to represent MLOAD and MSTORE nodes
01920 class MaskedLoadStoreSDNode : public MemSDNode {
01921   // Operands
01922   SDUse Ops[4];
01923 public:
01924   friend class SelectionDAG;
01925   MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
01926                    SDValue *Operands, unsigned numOperands, 
01927                    SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
01928     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
01929     InitOperands(Ops, Operands, numOperands);
01930   }
01931 
01932   // In the both nodes address is Op1, mask is Op2:
01933   // MaskedLoadSDNode (Chain, ptr, mask, src0), src0 is a passthru value
01934   // MaskedStoreSDNode (Chain, ptr, mask, data)
01935   // Mask is a vector of i1 elements
01936   const SDValue &getBasePtr() const { return getOperand(1); }
01937   const SDValue &getMask() const    { return getOperand(2); }
01938 
01939   static bool classof(const SDNode *N) {
01940     return N->getOpcode() == ISD::MLOAD ||
01941            N->getOpcode() == ISD::MSTORE;
01942   }
01943 };
01944 
01945 /// This class is used to represent an MLOAD node
01946 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
01947 public:
01948   friend class SelectionDAG;
01949   MaskedLoadSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
01950                    unsigned numOperands, SDVTList VTs, ISD::LoadExtType ETy,
01951                    EVT MemVT, MachineMemOperand *MMO)
01952     : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, Operands, numOperands,
01953                             VTs, MemVT, MMO) {
01954     SubclassData |= (unsigned short)ETy;
01955   }
01956 
01957   ISD::LoadExtType getExtensionType() const {
01958     return ISD::LoadExtType(SubclassData & 3);
01959   } 
01960   const SDValue &getSrc0() const { return getOperand(3); }
01961   static bool classof(const SDNode *N) {
01962     return N->getOpcode() == ISD::MLOAD;
01963   }
01964 };
01965 
01966 /// This class is used to represent an MSTORE node
01967 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
01968 
01969 public:
01970   friend class SelectionDAG;
01971   MaskedStoreSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
01972                     unsigned numOperands, SDVTList VTs, bool isTrunc, EVT MemVT,
01973                     MachineMemOperand *MMO)
01974     : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, Operands, numOperands,
01975                             VTs, MemVT, MMO) {
01976       SubclassData |= (unsigned short)isTrunc;
01977   }
01978   /// Return true if the op does a truncation before store.
01979   /// For integers this is the same as doing a TRUNCATE and storing the result.
01980   /// For floats, it is the same as doing an FP_ROUND and storing the result.
01981   bool isTruncatingStore() const { return SubclassData & 1; }
01982 
01983   const SDValue &getValue() const { return getOperand(3); }
01984 
01985   static bool classof(const SDNode *N) {
01986     return N->getOpcode() == ISD::MSTORE;
01987   }
01988 };
01989 
01990 /// An SDNode that represents everything that will be needed
01991 /// to construct a MachineInstr. These nodes are created during the
01992 /// instruction selection proper phase.
01993 class MachineSDNode : public SDNode {
01994 public:
01995   typedef MachineMemOperand **mmo_iterator;
01996 
01997 private:
01998   friend class SelectionDAG;
01999   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
02000     : SDNode(Opc, Order, DL, VTs), MemRefs(nullptr), MemRefsEnd(nullptr) {}
02001 
02002   /// Operands for this instruction, if they fit here. If
02003   /// they don't, this field is unused.
02004   SDUse LocalOperands[4];
02005 
02006   /// Memory reference descriptions for this instruction.
02007   mmo_iterator MemRefs;
02008   mmo_iterator MemRefsEnd;
02009 
02010 public:
02011   mmo_iterator memoperands_begin() const { return MemRefs; }
02012   mmo_iterator memoperands_end() const { return MemRefsEnd; }
02013   bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
02014 
02015   /// Assign this MachineSDNodes's memory reference descriptor
02016   /// list. This does not transfer ownership.
02017   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
02018     for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
02019       assert(*MMI && "Null mem ref detected!");
02020     MemRefs = NewMemRefs;
02021     MemRefsEnd = NewMemRefsEnd;
02022   }
02023 
02024   static bool classof(const SDNode *N) {
02025     return N->isMachineOpcode();
02026   }
02027 };
02028 
02029 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
02030                                             SDNode, ptrdiff_t> {
02031   const SDNode *Node;
02032   unsigned Operand;
02033 
02034   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
02035 public:
02036   bool operator==(const SDNodeIterator& x) const {
02037     return Operand == x.Operand;
02038   }
02039   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
02040 
02041   pointer operator*() const {
02042     return Node->getOperand(Operand).getNode();
02043   }
02044   pointer operator->() const { return operator*(); }
02045 
02046   SDNodeIterator& operator++() {                // Preincrement
02047     ++Operand;
02048     return *this;
02049   }
02050   SDNodeIterator operator++(int) { // Postincrement
02051     SDNodeIterator tmp = *this; ++*this; return tmp;
02052   }
02053   size_t operator-(SDNodeIterator Other) const {
02054     assert(Node == Other.Node &&
02055            "Cannot compare iterators of two different nodes!");
02056     return Operand - Other.Operand;
02057   }
02058 
02059   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
02060   static SDNodeIterator end  (const SDNode *N) {
02061     return SDNodeIterator(N, N->getNumOperands());
02062   }
02063 
02064   unsigned getOperand() const { return Operand; }
02065   const SDNode *getNode() const { return Node; }
02066 };
02067 
02068 template <> struct GraphTraits<SDNode*> {
02069   typedef SDNode NodeType;
02070   typedef SDNodeIterator ChildIteratorType;
02071   static inline NodeType *getEntryNode(SDNode *N) { return N; }
02072   static inline ChildIteratorType child_begin(NodeType *N) {
02073     return SDNodeIterator::begin(N);
02074   }
02075   static inline ChildIteratorType child_end(NodeType *N) {
02076     return SDNodeIterator::end(N);
02077   }
02078 };
02079 
02080 /// The largest SDNode class.
02081 typedef AtomicSDNode LargestSDNode;
02082 
02083 /// The SDNode class with the greatest alignment requirement.
02084 typedef GlobalAddressSDNode MostAlignedSDNode;
02085 
02086 namespace ISD {
02087   /// Returns true if the specified node is a non-extending and unindexed load.
02088   inline bool isNormalLoad(const SDNode *N) {
02089     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
02090     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
02091       Ld->getAddressingMode() == ISD::UNINDEXED;
02092   }
02093 
02094   /// Returns true if the specified node is a non-extending load.
02095   inline bool isNON_EXTLoad(const SDNode *N) {
02096     return isa<LoadSDNode>(N) &&
02097       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
02098   }
02099 
02100   /// Returns true if the specified node is a EXTLOAD.
02101   inline bool isEXTLoad(const SDNode *N) {
02102     return isa<LoadSDNode>(N) &&
02103       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
02104   }
02105 
02106   /// Returns true if the specified node is a SEXTLOAD.
02107   inline bool isSEXTLoad(const SDNode *N) {
02108     return isa<LoadSDNode>(N) &&
02109       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
02110   }
02111 
02112   /// Returns true if the specified node is a ZEXTLOAD.
02113   inline bool isZEXTLoad(const SDNode *N) {
02114     return isa<LoadSDNode>(N) &&
02115       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
02116   }
02117 
02118   /// Returns true if the specified node is an unindexed load.
02119   inline bool isUNINDEXEDLoad(const SDNode *N) {
02120     return isa<LoadSDNode>(N) &&
02121       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
02122   }
02123 
02124   /// Returns true if the specified node is a non-truncating
02125   /// and unindexed store.
02126   inline bool isNormalStore(const SDNode *N) {
02127     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
02128     return St && !St->isTruncatingStore() &&
02129       St->getAddressingMode() == ISD::UNINDEXED;
02130   }
02131 
02132   /// Returns true if the specified node is a non-truncating store.
02133   inline bool isNON_TRUNCStore(const SDNode *N) {
02134     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
02135   }
02136 
02137   /// Returns true if the specified node is a truncating store.
02138   inline bool isTRUNCStore(const SDNode *N) {
02139     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
02140   }
02141 
02142   /// Returns true if the specified node is an unindexed store.
02143   inline bool isUNINDEXEDStore(const SDNode *N) {
02144     return isa<StoreSDNode>(N) &&
02145       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
02146   }
02147 }
02148 
02149 } // end llvm namespace
02150 
02151 #endif