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 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
00054                     bool force = false);
00055 
00056 /// This represents a list of ValueType's that has been intern'd by
00057 /// a SelectionDAG.  Instances of this simple value class are returned by
00058 /// SelectionDAG::getVTList(...).
00059 ///
00060 struct SDVTList {
00061   const EVT *VTs;
00062   unsigned int NumVTs;
00063 };
00064 
00065 namespace ISD {
00066   /// Node predicates
00067 
00068   /// Return true if the specified node is a
00069   /// BUILD_VECTOR where all of the elements are ~0 or undef.
00070   bool isBuildVectorAllOnes(const SDNode *N);
00071 
00072   /// Return true if the specified node is a
00073   /// BUILD_VECTOR where all of the elements are 0 or undef.
00074   bool isBuildVectorAllZeros(const SDNode *N);
00075 
00076   /// \brief Return true if the specified node is a BUILD_VECTOR node of
00077   /// all ConstantSDNode or undef.
00078   bool isBuildVectorOfConstantSDNodes(const SDNode *N);
00079 
00080   /// \brief Return true if the specified node is a BUILD_VECTOR node of
00081   /// all ConstantFPSDNode or undef.
00082   bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
00083 
00084   /// Return true if the specified node is a
00085   /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
00086   /// element is not an undef.
00087   bool isScalarToVector(const SDNode *N);
00088 
00089   /// Return true if the node has at least one operand
00090   /// and all operands of the specified node are ISD::UNDEF.
00091   bool allOperandsUndef(const SDNode *N);
00092 }  // end llvm:ISD namespace
00093 
00094 //===----------------------------------------------------------------------===//
00095 /// Unlike LLVM values, Selection DAG nodes may return multiple
00096 /// values as the result of a computation.  Many nodes return multiple values,
00097 /// from loads (which define a token and a return value) to ADDC (which returns
00098 /// a result and a carry value), to calls (which may return an arbitrary number
00099 /// of values).
00100 ///
00101 /// As such, each use of a SelectionDAG computation must indicate the node that
00102 /// computes it as well as which return value to use from that node.  This pair
00103 /// of information is represented with the SDValue value type.
00104 ///
00105 class SDValue {
00106   friend struct DenseMapInfo<SDValue>;
00107 
00108   SDNode *Node;       // The node defining the value we are using.
00109   unsigned ResNo;     // Which return value of the node we are using.
00110 public:
00111   SDValue() : Node(nullptr), ResNo(0) {}
00112   SDValue(SDNode *node, unsigned resno);
00113 
00114   /// get the index which selects a specific result in the SDNode
00115   unsigned getResNo() const { return ResNo; }
00116 
00117   /// get the SDNode which holds the desired result
00118   SDNode *getNode() const { return Node; }
00119 
00120   /// set the SDNode
00121   void setNode(SDNode *N) { Node = N; }
00122 
00123   inline SDNode *operator->() const { return Node; }
00124 
00125   bool operator==(const SDValue &O) const {
00126     return Node == O.Node && ResNo == O.ResNo;
00127   }
00128   bool operator!=(const SDValue &O) const {
00129     return !operator==(O);
00130   }
00131   bool operator<(const SDValue &O) const {
00132     return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
00133   }
00134   explicit operator bool() const {
00135     return Node != nullptr;
00136   }
00137 
00138   SDValue getValue(unsigned R) const {
00139     return SDValue(Node, R);
00140   }
00141 
00142   // Return true if this node is an operand of N.
00143   bool isOperandOf(SDNode *N) const;
00144 
00145   /// Return the ValueType of the referenced return value.
00146   inline EVT getValueType() const;
00147 
00148   /// Return the simple ValueType of the referenced return value.
00149   MVT getSimpleValueType() const {
00150     return getValueType().getSimpleVT();
00151   }
00152 
00153   /// Returns the size of the value in bits.
00154   unsigned getValueSizeInBits() const {
00155     return getValueType().getSizeInBits();
00156   }
00157 
00158   unsigned getScalarValueSizeInBits() const {
00159     return getValueType().getScalarType().getSizeInBits();
00160   }
00161 
00162   // Forwarding methods - These forward to the corresponding methods in SDNode.
00163   inline unsigned getOpcode() const;
00164   inline unsigned getNumOperands() const;
00165   inline const SDValue &getOperand(unsigned i) const;
00166   inline uint64_t getConstantOperandVal(unsigned i) const;
00167   inline bool isTargetMemoryOpcode() const;
00168   inline bool isTargetOpcode() const;
00169   inline bool isMachineOpcode() const;
00170   inline unsigned getMachineOpcode() const;
00171   inline const DebugLoc &getDebugLoc() const;
00172   inline void dump() const;
00173   inline void dumpr() const;
00174 
00175   /// Return true if this operand (which must be a chain) reaches the
00176   /// specified operand without crossing any side-effecting instructions.
00177   /// In practice, this looks through token factors and non-volatile loads.
00178   /// In order to remain efficient, this only
00179   /// looks a couple of nodes in, it does not do an exhaustive search.
00180   bool reachesChainWithoutSideEffects(SDValue Dest,
00181                                       unsigned Depth = 2) const;
00182 
00183   /// Return true if there are no nodes using value ResNo of Node.
00184   inline bool use_empty() const;
00185 
00186   /// Return true if there is exactly one node using value ResNo of Node.
00187   inline bool hasOneUse() const;
00188 };
00189 
00190 
00191 template<> struct DenseMapInfo<SDValue> {
00192   static inline SDValue getEmptyKey() {
00193     SDValue V;
00194     V.ResNo = -1U;
00195     return V;
00196   }
00197   static inline SDValue getTombstoneKey() {
00198     SDValue V;
00199     V.ResNo = -2U;
00200     return V;
00201   }
00202   static unsigned getHashValue(const SDValue &Val) {
00203     return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
00204             (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
00205   }
00206   static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
00207     return LHS == RHS;
00208   }
00209 };
00210 template <> struct isPodLike<SDValue> { static const bool value = true; };
00211 
00212 
00213 /// Allow casting operators to work directly on
00214 /// SDValues as if they were SDNode*'s.
00215 template<> struct simplify_type<SDValue> {
00216   typedef SDNode* SimpleType;
00217   static SimpleType getSimplifiedValue(SDValue &Val) {
00218     return Val.getNode();
00219   }
00220 };
00221 template<> struct simplify_type<const SDValue> {
00222   typedef /*const*/ SDNode* SimpleType;
00223   static SimpleType getSimplifiedValue(const SDValue &Val) {
00224     return Val.getNode();
00225   }
00226 };
00227 
00228 /// Represents a use of a SDNode. This class holds an SDValue,
00229 /// which records the SDNode being used and the result number, a
00230 /// pointer to the SDNode using the value, and Next and Prev pointers,
00231 /// which link together all the uses of an SDNode.
00232 ///
00233 class SDUse {
00234   /// Val - The value being used.
00235   SDValue Val;
00236   /// User - The user of this value.
00237   SDNode *User;
00238   /// Prev, Next - Pointers to the uses list of the SDNode referred by
00239   /// this operand.
00240   SDUse **Prev, *Next;
00241 
00242   SDUse(const SDUse &U) = delete;
00243   void operator=(const SDUse &U) = delete;
00244 
00245 public:
00246   SDUse() : Val(), User(nullptr), Prev(nullptr), Next(nullptr) {}
00247 
00248   /// Normally SDUse will just implicitly convert to an SDValue that it holds.
00249   operator const SDValue&() const { return Val; }
00250 
00251   /// If implicit conversion to SDValue doesn't work, the get() method returns
00252   /// the SDValue.
00253   const SDValue &get() const { return Val; }
00254 
00255   /// This returns the SDNode that contains this Use.
00256   SDNode *getUser() { return User; }
00257 
00258   /// Get the next SDUse in the use list.
00259   SDUse *getNext() const { return Next; }
00260 
00261   /// Convenience function for get().getNode().
00262   SDNode *getNode() const { return Val.getNode(); }
00263   /// Convenience function for get().getResNo().
00264   unsigned getResNo() const { return Val.getResNo(); }
00265   /// Convenience function for get().getValueType().
00266   EVT getValueType() const { return Val.getValueType(); }
00267 
00268   /// Convenience function for get().operator==
00269   bool operator==(const SDValue &V) const {
00270     return Val == V;
00271   }
00272 
00273   /// Convenience function for get().operator!=
00274   bool operator!=(const SDValue &V) const {
00275     return Val != V;
00276   }
00277 
00278   /// Convenience function for get().operator<
00279   bool operator<(const SDValue &V) const {
00280     return Val < V;
00281   }
00282 
00283 private:
00284   friend class SelectionDAG;
00285   friend class SDNode;
00286 
00287   void setUser(SDNode *p) { User = p; }
00288 
00289   /// Remove this use from its existing use list, assign it the
00290   /// given value, and add it to the new value's node's use list.
00291   inline void set(const SDValue &V);
00292   /// Like set, but only supports initializing a newly-allocated
00293   /// SDUse with a non-null value.
00294   inline void setInitial(const SDValue &V);
00295   /// Like set, but only sets the Node portion of the value,
00296   /// leaving the ResNo portion unmodified.
00297   inline void setNode(SDNode *N);
00298 
00299   void addToList(SDUse **List) {
00300     Next = *List;
00301     if (Next) Next->Prev = &Next;
00302     Prev = List;
00303     *List = this;
00304   }
00305 
00306   void removeFromList() {
00307     *Prev = Next;
00308     if (Next) Next->Prev = Prev;
00309   }
00310 };
00311 
00312 /// simplify_type specializations - Allow casting operators to work directly on
00313 /// SDValues as if they were SDNode*'s.
00314 template<> struct simplify_type<SDUse> {
00315   typedef SDNode* SimpleType;
00316   static SimpleType getSimplifiedValue(SDUse &Val) {
00317     return Val.getNode();
00318   }
00319 };
00320 
00321 
00322 /// Represents one node in the SelectionDAG.
00323 ///
00324 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
00325 private:
00326   /// The operation that this node performs.
00327   int16_t NodeType;
00328 
00329   /// This is true if OperandList was new[]'d.  If true,
00330   /// then they will be delete[]'d when the node is destroyed.
00331   uint16_t OperandsNeedDelete : 1;
00332 
00333   /// This tracks whether this node has one or more dbg_value
00334   /// nodes corresponding to it.
00335   uint16_t HasDebugValue : 1;
00336 
00337 protected:
00338   /// This member is defined by this class, but is not used for
00339   /// anything.  Subclasses can use it to hold whatever state they find useful.
00340   /// This field is initialized to zero by the ctor.
00341   uint16_t SubclassData : 14;
00342 
00343 private:
00344   /// Unique id per SDNode in the DAG.
00345   int NodeId;
00346 
00347   /// The values that are used by this operation.
00348   SDUse *OperandList;
00349 
00350   /// The types of the values this node defines.  SDNode's may
00351   /// define multiple values simultaneously.
00352   const EVT *ValueList;
00353 
00354   /// List of uses for this SDNode.
00355   SDUse *UseList;
00356 
00357   /// The number of entries in the Operand/Value list.
00358   unsigned short NumOperands, NumValues;
00359 
00360   /// Source line information.
00361   DebugLoc debugLoc;
00362 
00363   // The ordering of the SDNodes. It roughly corresponds to the ordering of the
00364   // original LLVM instructions.
00365   // This is used for turning off scheduling, because we'll forgo
00366   // the normal scheduling algorithms and output the instructions according to
00367   // this ordering.
00368   unsigned IROrder;
00369 
00370   /// Return a pointer to the specified value type.
00371   static const EVT *getValueTypeList(EVT VT);
00372 
00373   friend class SelectionDAG;
00374   friend struct ilist_traits<SDNode>;
00375 
00376 public:
00377   //===--------------------------------------------------------------------===//
00378   //  Accessors
00379   //
00380 
00381   /// Return the SelectionDAG opcode value for this node. For
00382   /// pre-isel nodes (those for which isMachineOpcode returns false), these
00383   /// are the opcode values in the ISD and <target>ISD namespaces. For
00384   /// post-isel opcodes, see getMachineOpcode.
00385   unsigned getOpcode()  const { return (unsigned short)NodeType; }
00386 
00387   /// Test if this node has a target-specific opcode (in the
00388   /// <target>ISD namespace).
00389   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
00390 
00391   /// Test if this node has a target-specific
00392   /// memory-referencing opcode (in the <target>ISD namespace and
00393   /// greater than FIRST_TARGET_MEMORY_OPCODE).
00394   bool isTargetMemoryOpcode() const {
00395     return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
00396   }
00397 
00398   /// Test if this node is a memory intrinsic (with valid pointer information).
00399   /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
00400   /// non-memory intrinsics (with chains) that are not really instances of
00401   /// MemSDNode. For such nodes, we need some extra state to determine the
00402   /// proper classof relationship.
00403   bool isMemIntrinsic() const {
00404     return (NodeType == ISD::INTRINSIC_W_CHAIN ||
00405             NodeType == ISD::INTRINSIC_VOID) && ((SubclassData >> 13) & 1);
00406   }
00407 
00408   /// Test if this node has a post-isel opcode, directly
00409   /// corresponding to a MachineInstr opcode.
00410   bool isMachineOpcode() const { return NodeType < 0; }
00411 
00412   /// This may only be called if isMachineOpcode returns
00413   /// true. It returns the MachineInstr opcode value that the node's opcode
00414   /// corresponds to.
00415   unsigned getMachineOpcode() const {
00416     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
00417     return ~NodeType;
00418   }
00419 
00420   /// Get this bit.
00421   bool getHasDebugValue() const { return HasDebugValue; }
00422 
00423   /// Set this bit.
00424   void setHasDebugValue(bool b) { HasDebugValue = b; }
00425 
00426   /// Return true if there are no uses of this node.
00427   bool use_empty() const { return UseList == nullptr; }
00428 
00429   /// Return true if there is exactly one use of this node.
00430   bool hasOneUse() const {
00431     return !use_empty() && std::next(use_begin()) == use_end();
00432   }
00433 
00434   /// Return the number of uses of this node. This method takes
00435   /// time proportional to the number of uses.
00436   size_t use_size() const { return std::distance(use_begin(), use_end()); }
00437 
00438   /// Return the unique node id.
00439   int getNodeId() const { return NodeId; }
00440 
00441   /// Set unique node id.
00442   void setNodeId(int Id) { NodeId = Id; }
00443 
00444   /// Return the node ordering.
00445   unsigned getIROrder() const { return IROrder; }
00446 
00447   /// Set the node ordering.
00448   void setIROrder(unsigned Order) { IROrder = Order; }
00449 
00450   /// Return the source location info.
00451   const DebugLoc &getDebugLoc() const { return debugLoc; }
00452 
00453   /// Set source location info.  Try to avoid this, putting
00454   /// it in the constructor is preferable.
00455   void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
00456 
00457   /// This class provides iterator support for SDUse
00458   /// operands that use a specific SDNode.
00459   class use_iterator
00460     : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
00461     SDUse *Op;
00462     explicit use_iterator(SDUse *op) : Op(op) {
00463     }
00464     friend class SDNode;
00465   public:
00466     typedef std::iterator<std::forward_iterator_tag,
00467                           SDUse, ptrdiff_t>::reference reference;
00468     typedef std::iterator<std::forward_iterator_tag,
00469                           SDUse, ptrdiff_t>::pointer pointer;
00470 
00471     use_iterator(const use_iterator &I) : Op(I.Op) {}
00472     use_iterator() : Op(nullptr) {}
00473 
00474     bool operator==(const use_iterator &x) const {
00475       return Op == x.Op;
00476     }
00477     bool operator!=(const use_iterator &x) const {
00478       return !operator==(x);
00479     }
00480 
00481     /// Return true if this iterator is at the end of uses list.
00482     bool atEnd() const { return Op == nullptr; }
00483 
00484     // Iterator traversal: forward iteration only.
00485     use_iterator &operator++() {          // Preincrement
00486       assert(Op && "Cannot increment end iterator!");
00487       Op = Op->getNext();
00488       return *this;
00489     }
00490 
00491     use_iterator operator++(int) {        // Postincrement
00492       use_iterator tmp = *this; ++*this; return tmp;
00493     }
00494 
00495     /// Retrieve a pointer to the current user node.
00496     SDNode *operator*() const {
00497       assert(Op && "Cannot dereference end iterator!");
00498       return Op->getUser();
00499     }
00500 
00501     SDNode *operator->() const { return operator*(); }
00502 
00503     SDUse &getUse() const { return *Op; }
00504 
00505     /// Retrieve the operand # of this use in its user.
00506     unsigned getOperandNo() const {
00507       assert(Op && "Cannot dereference end iterator!");
00508       return (unsigned)(Op - Op->getUser()->OperandList);
00509     }
00510   };
00511 
00512   /// Provide iteration support to walk over all uses of an SDNode.
00513   use_iterator use_begin() const {
00514     return use_iterator(UseList);
00515   }
00516 
00517   static use_iterator use_end() { return use_iterator(nullptr); }
00518 
00519   inline iterator_range<use_iterator> uses() {
00520     return iterator_range<use_iterator>(use_begin(), use_end());
00521   }
00522   inline iterator_range<use_iterator> uses() const {
00523     return iterator_range<use_iterator>(use_begin(), use_end());
00524   }
00525 
00526   /// Return true if there are exactly NUSES uses of the indicated value.
00527   /// This method ignores uses of other values defined by this operation.
00528   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
00529 
00530   /// Return true if there are any use of the indicated value.
00531   /// This method ignores uses of other values defined by this operation.
00532   bool hasAnyUseOfValue(unsigned Value) const;
00533 
00534   /// Return true if this node is the only use of N.
00535   bool isOnlyUserOf(SDNode *N) const;
00536 
00537   /// Return true if this node is an operand of N.
00538   bool isOperandOf(SDNode *N) const;
00539 
00540   /// Return true if this node is a predecessor of N.
00541   /// NOTE: Implemented on top of hasPredecessor and every bit as
00542   /// expensive. Use carefully.
00543   bool isPredecessorOf(const SDNode *N) const {
00544     return N->hasPredecessor(this);
00545   }
00546 
00547   /// Return true if N is a predecessor of this node.
00548   /// N is either an operand of this node, or can be reached by recursively
00549   /// traversing up the operands.
00550   /// NOTE: This is an expensive method. Use it carefully.
00551   bool hasPredecessor(const SDNode *N) const;
00552 
00553   /// Return true if N is a predecessor of this node.
00554   /// N is either an operand of this node, or can be reached by recursively
00555   /// traversing up the operands.
00556   /// In this helper the Visited and worklist sets are held externally to
00557   /// cache predecessors over multiple invocations. If you want to test for
00558   /// multiple predecessors this method is preferable to multiple calls to
00559   /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
00560   /// changes.
00561   /// NOTE: This is still very expensive. Use carefully.
00562   bool hasPredecessorHelper(const SDNode *N,
00563                             SmallPtrSetImpl<const SDNode *> &Visited,
00564                             SmallVectorImpl<const SDNode *> &Worklist) const;
00565 
00566   /// Return the number of values used by this operation.
00567   unsigned getNumOperands() const { return NumOperands; }
00568 
00569   /// Helper method returns the integer value of a ConstantSDNode operand.
00570   uint64_t getConstantOperandVal(unsigned Num) const;
00571 
00572   const SDValue &getOperand(unsigned Num) const {
00573     assert(Num < NumOperands && "Invalid child # of SDNode!");
00574     return OperandList[Num];
00575   }
00576 
00577   typedef SDUse* op_iterator;
00578   op_iterator op_begin() const { return OperandList; }
00579   op_iterator op_end() const { return OperandList+NumOperands; }
00580   ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
00581 
00582   SDVTList getVTList() const {
00583     SDVTList X = { ValueList, NumValues };
00584     return X;
00585   }
00586 
00587   /// If this node has a glue operand, return the node
00588   /// to which the glue operand points. Otherwise return NULL.
00589   SDNode *getGluedNode() const {
00590     if (getNumOperands() != 0 &&
00591       getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
00592       return getOperand(getNumOperands()-1).getNode();
00593     return nullptr;
00594   }
00595 
00596   // If this is a pseudo op, like copyfromreg, look to see if there is a
00597   // real target node glued to it.  If so, return the target node.
00598   const SDNode *getGluedMachineNode() const {
00599     const SDNode *FoundNode = this;
00600 
00601     // Climb up glue edges until a machine-opcode node is found, or the
00602     // end of the chain is reached.
00603     while (!FoundNode->isMachineOpcode()) {
00604       const SDNode *N = FoundNode->getGluedNode();
00605       if (!N) break;
00606       FoundNode = N;
00607     }
00608 
00609     return FoundNode;
00610   }
00611 
00612   /// If this node has a glue value with a user, return
00613   /// the user (there is at most one). Otherwise return NULL.
00614   SDNode *getGluedUser() const {
00615     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
00616       if (UI.getUse().get().getValueType() == MVT::Glue)
00617         return *UI;
00618     return nullptr;
00619   }
00620 
00621   /// Return the number of values defined/returned by this operator.
00622   unsigned getNumValues() const { return NumValues; }
00623 
00624   /// Return the type of a specified result.
00625   EVT getValueType(unsigned ResNo) const {
00626     assert(ResNo < NumValues && "Illegal result number!");
00627     return ValueList[ResNo];
00628   }
00629 
00630   /// Return the type of a specified result as a simple type.
00631   MVT getSimpleValueType(unsigned ResNo) const {
00632     return getValueType(ResNo).getSimpleVT();
00633   }
00634 
00635   /// Returns MVT::getSizeInBits(getValueType(ResNo)).
00636   unsigned getValueSizeInBits(unsigned ResNo) const {
00637     return getValueType(ResNo).getSizeInBits();
00638   }
00639 
00640   typedef const EVT* value_iterator;
00641   value_iterator value_begin() const { return ValueList; }
00642   value_iterator value_end() const { return ValueList+NumValues; }
00643 
00644   /// Return the opcode of this operation for printing.
00645   std::string getOperationName(const SelectionDAG *G = nullptr) const;
00646   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
00647   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
00648   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
00649   void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
00650   void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
00651 
00652   /// Print a SelectionDAG node and all children down to
00653   /// the leaves.  The given SelectionDAG allows target-specific nodes
00654   /// to be printed in human-readable form.  Unlike printr, this will
00655   /// print the whole DAG, including children that appear multiple
00656   /// times.
00657   ///
00658   void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
00659 
00660   /// Print a SelectionDAG node and children up to
00661   /// depth "depth."  The given SelectionDAG allows target-specific
00662   /// nodes to be printed in human-readable form.  Unlike printr, this
00663   /// will print children that appear multiple times wherever they are
00664   /// used.
00665   ///
00666   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
00667                        unsigned depth = 100) const;
00668 
00669 
00670   /// Dump this node, for debugging.
00671   void dump() const;
00672 
00673   /// Dump (recursively) this node and its use-def subgraph.
00674   void dumpr() const;
00675 
00676   /// Dump this node, for debugging.
00677   /// The given SelectionDAG allows target-specific nodes to be printed
00678   /// in human-readable form.
00679   void dump(const SelectionDAG *G) const;
00680 
00681   /// Dump (recursively) this node and its use-def subgraph.
00682   /// The given SelectionDAG allows target-specific nodes to be printed
00683   /// in human-readable form.
00684   void dumpr(const SelectionDAG *G) const;
00685 
00686   /// printrFull to dbgs().  The given SelectionDAG allows
00687   /// target-specific nodes to be printed in human-readable form.
00688   /// Unlike dumpr, this will print the whole DAG, including children
00689   /// that appear multiple times.
00690   void dumprFull(const SelectionDAG *G = nullptr) const;
00691 
00692   /// printrWithDepth to dbgs().  The given
00693   /// SelectionDAG allows target-specific nodes to be printed in
00694   /// human-readable form.  Unlike dumpr, this will print children
00695   /// that appear multiple times wherever they are used.
00696   ///
00697   void dumprWithDepth(const SelectionDAG *G = nullptr,
00698                       unsigned depth = 100) const;
00699 
00700   /// Gather unique data for the node.
00701   void Profile(FoldingSetNodeID &ID) const;
00702 
00703   /// This method should only be used by the SDUse class.
00704   void addUse(SDUse &U) { U.addToList(&UseList); }
00705 
00706 protected:
00707   static SDVTList getSDVTList(EVT VT) {
00708     SDVTList Ret = { getValueTypeList(VT), 1 };
00709     return Ret;
00710   }
00711 
00712   SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00713          ArrayRef<SDValue> Ops)
00714       : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
00715         SubclassData(0), NodeId(-1),
00716         OperandList(Ops.size() ? new SDUse[Ops.size()] : nullptr),
00717         ValueList(VTs.VTs), UseList(nullptr), NumOperands(Ops.size()),
00718         NumValues(VTs.NumVTs), debugLoc(std::move(dl)), IROrder(Order) {
00719     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
00720     assert(NumOperands == Ops.size() &&
00721            "NumOperands wasn't wide enough for its operands!");
00722     assert(NumValues == VTs.NumVTs &&
00723            "NumValues wasn't wide enough for its operands!");
00724     for (unsigned i = 0; i != Ops.size(); ++i) {
00725       assert(OperandList && "no operands available");
00726       OperandList[i].setUser(this);
00727       OperandList[i].setInitial(Ops[i]);
00728     }
00729     checkForCycles(this);
00730   }
00731 
00732   /// This constructor adds no operands itself; operands can be
00733   /// set later with InitOperands.
00734   SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
00735       : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
00736         SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
00737         UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs),
00738         debugLoc(std::move(dl)), IROrder(Order) {
00739     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
00740     assert(NumValues == VTs.NumVTs &&
00741            "NumValues wasn't wide enough for its operands!");
00742   }
00743 
00744   /// Initialize the operands list of this with 1 operand.
00745   void InitOperands(SDUse *Ops, const SDValue &Op0) {
00746     Ops[0].setUser(this);
00747     Ops[0].setInitial(Op0);
00748     NumOperands = 1;
00749     OperandList = Ops;
00750     checkForCycles(this);
00751   }
00752 
00753   /// Initialize the operands list of this with 2 operands.
00754   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
00755     Ops[0].setUser(this);
00756     Ops[0].setInitial(Op0);
00757     Ops[1].setUser(this);
00758     Ops[1].setInitial(Op1);
00759     NumOperands = 2;
00760     OperandList = Ops;
00761     checkForCycles(this);
00762   }
00763 
00764   /// Initialize the operands list of this with 3 operands.
00765   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
00766                     const SDValue &Op2) {
00767     Ops[0].setUser(this);
00768     Ops[0].setInitial(Op0);
00769     Ops[1].setUser(this);
00770     Ops[1].setInitial(Op1);
00771     Ops[2].setUser(this);
00772     Ops[2].setInitial(Op2);
00773     NumOperands = 3;
00774     OperandList = Ops;
00775     checkForCycles(this);
00776   }
00777 
00778   /// Initialize the operands list of this with 4 operands.
00779   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
00780                     const SDValue &Op2, const SDValue &Op3) {
00781     Ops[0].setUser(this);
00782     Ops[0].setInitial(Op0);
00783     Ops[1].setUser(this);
00784     Ops[1].setInitial(Op1);
00785     Ops[2].setUser(this);
00786     Ops[2].setInitial(Op2);
00787     Ops[3].setUser(this);
00788     Ops[3].setInitial(Op3);
00789     NumOperands = 4;
00790     OperandList = Ops;
00791     checkForCycles(this);
00792   }
00793 
00794   /// Initialize the operands list of this with N operands.
00795   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
00796     for (unsigned i = 0; i != N; ++i) {
00797       Ops[i].setUser(this);
00798       Ops[i].setInitial(Vals[i]);
00799     }
00800     NumOperands = N;
00801     assert(NumOperands == N &&
00802            "NumOperands wasn't wide enough for its operands!");
00803     OperandList = Ops;
00804     checkForCycles(this);
00805   }
00806 
00807   /// Release the operands and set this node to have zero operands.
00808   void DropOperands();
00809 };
00810 
00811 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
00812 /// into SDNode creation functions.
00813 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
00814 /// from the original Instruction, and IROrder is the ordinal position of
00815 /// the instruction.
00816 /// When an SDNode is created after the DAG is being built, both DebugLoc and
00817 /// the IROrder are propagated from the original SDNode.
00818 /// So SDLoc class provides two constructors besides the default one, one to
00819 /// be used by the DAGBuilder, the other to be used by others.
00820 class SDLoc {
00821 private:
00822   // Ptr could be used for either Instruction* or SDNode*. It is used for
00823   // Instruction* if IROrder is not -1.
00824   const void *Ptr;
00825   int IROrder;
00826 
00827 public:
00828   SDLoc() : Ptr(nullptr), IROrder(0) {}
00829   SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
00830     assert(N && "null SDNode");
00831   }
00832   SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
00833     assert(Ptr && "null SDNode");
00834   }
00835   SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
00836     assert(Order >= 0 && "bad IROrder");
00837   }
00838   unsigned getIROrder() {
00839     if (IROrder >= 0 || Ptr == nullptr) {
00840       return (unsigned)IROrder;
00841     }
00842     const SDNode *N = (const SDNode*)(Ptr);
00843     return N->getIROrder();
00844   }
00845   DebugLoc getDebugLoc() {
00846     if (!Ptr) {
00847       return DebugLoc();
00848     }
00849     if (IROrder >= 0) {
00850       const Instruction *I = (const Instruction*)(Ptr);
00851       return I->getDebugLoc();
00852     }
00853     const SDNode *N = (const SDNode*)(Ptr);
00854     return N->getDebugLoc();
00855   }
00856 };
00857 
00858 
00859 // Define inline functions from the SDValue class.
00860 
00861 inline SDValue::SDValue(SDNode *node, unsigned resno)
00862     : Node(node), ResNo(resno) {
00863   assert((!Node || ResNo < Node->getNumValues()) &&
00864          "Invalid result number for the given node!");
00865   assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
00866 }
00867 
00868 inline unsigned SDValue::getOpcode() const {
00869   return Node->getOpcode();
00870 }
00871 inline EVT SDValue::getValueType() const {
00872   return Node->getValueType(ResNo);
00873 }
00874 inline unsigned SDValue::getNumOperands() const {
00875   return Node->getNumOperands();
00876 }
00877 inline const SDValue &SDValue::getOperand(unsigned i) const {
00878   return Node->getOperand(i);
00879 }
00880 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
00881   return Node->getConstantOperandVal(i);
00882 }
00883 inline bool SDValue::isTargetOpcode() const {
00884   return Node->isTargetOpcode();
00885 }
00886 inline bool SDValue::isTargetMemoryOpcode() const {
00887   return Node->isTargetMemoryOpcode();
00888 }
00889 inline bool SDValue::isMachineOpcode() const {
00890   return Node->isMachineOpcode();
00891 }
00892 inline unsigned SDValue::getMachineOpcode() const {
00893   return Node->getMachineOpcode();
00894 }
00895 inline bool SDValue::use_empty() const {
00896   return !Node->hasAnyUseOfValue(ResNo);
00897 }
00898 inline bool SDValue::hasOneUse() const {
00899   return Node->hasNUsesOfValue(1, ResNo);
00900 }
00901 inline const DebugLoc &SDValue::getDebugLoc() const {
00902   return Node->getDebugLoc();
00903 }
00904 inline void SDValue::dump() const {
00905   return Node->dump();
00906 }
00907 inline void SDValue::dumpr() const {
00908   return Node->dumpr();
00909 }
00910 // Define inline functions from the SDUse class.
00911 
00912 inline void SDUse::set(const SDValue &V) {
00913   if (Val.getNode()) removeFromList();
00914   Val = V;
00915   if (V.getNode()) V.getNode()->addUse(*this);
00916 }
00917 
00918 inline void SDUse::setInitial(const SDValue &V) {
00919   Val = V;
00920   V.getNode()->addUse(*this);
00921 }
00922 
00923 inline void SDUse::setNode(SDNode *N) {
00924   if (Val.getNode()) removeFromList();
00925   Val.setNode(N);
00926   if (N) N->addUse(*this);
00927 }
00928 
00929 /// This class is used for single-operand SDNodes.  This is solely
00930 /// to allow co-allocation of node operands with the node itself.
00931 class UnarySDNode : public SDNode {
00932   SDUse Op;
00933 public:
00934   UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00935               SDValue X)
00936     : SDNode(Opc, Order, dl, VTs) {
00937     InitOperands(&Op, X);
00938   }
00939 };
00940 
00941 /// This class is used for two-operand SDNodes.  This is solely
00942 /// to allow co-allocation of node operands with the node itself.
00943 class BinarySDNode : public SDNode {
00944   SDUse Ops[2];
00945 public:
00946   BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00947                SDValue X, SDValue Y)
00948     : SDNode(Opc, Order, dl, VTs) {
00949     InitOperands(Ops, X, Y);
00950   }
00951 };
00952 
00953 /// Returns true if the opcode is a binary operation with flags.
00954 static bool isBinOpWithFlags(unsigned Opcode) {
00955   switch (Opcode) {
00956   case ISD::SDIV:
00957   case ISD::UDIV:
00958   case ISD::SRA:
00959   case ISD::SRL:
00960   case ISD::MUL:
00961   case ISD::ADD:
00962   case ISD::SUB:
00963   case ISD::SHL:
00964     return true;
00965   default:
00966     return false;
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