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 /// These are IR-level optimization flags that may be propagated to SDNodes.
00930 /// TODO: This data structure should be shared by the IR optimizer and the
00931 /// the backend.
00932 struct SDNodeFlags {
00933 private:
00934   bool NoUnsignedWrap : 1;
00935   bool NoSignedWrap : 1;
00936   bool Exact : 1;
00937   bool UnsafeAlgebra : 1;
00938   bool NoNaNs : 1;
00939   bool NoInfs : 1;
00940   bool NoSignedZeros : 1;
00941   bool AllowReciprocal : 1;
00942 
00943 public:
00944   /// Default constructor turns off all optimization flags.
00945   SDNodeFlags() {
00946     NoUnsignedWrap = false;
00947     NoSignedWrap = false;
00948     Exact = false;
00949     UnsafeAlgebra = false;
00950     NoNaNs = false;
00951     NoInfs = false;
00952     NoSignedZeros = false;
00953     AllowReciprocal = false;
00954   }
00955 
00956   // These are mutators for each flag.
00957   void setNoUnsignedWrap(bool b) { NoUnsignedWrap = b; }
00958   void setNoSignedWrap(bool b) { NoSignedWrap = b; }
00959   void setExact(bool b) { Exact = b; }
00960   void setUnsafeAlgebra(bool b) { UnsafeAlgebra = b; }
00961   void setNoNaNs(bool b) { NoNaNs = b; }
00962   void setNoInfs(bool b) { NoInfs = b; }
00963   void setNoSignedZeros(bool b) { NoSignedZeros = b; }
00964   void setAllowReciprocal(bool b) { AllowReciprocal = b; }
00965 
00966   // These are accessors for each flag.
00967   bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
00968   bool hasNoSignedWrap() const { return NoSignedWrap; }
00969   bool hasExact() const { return Exact; }
00970   bool hasUnsafeAlgebra() const { return UnsafeAlgebra; }
00971   bool hasNoNaNs() const { return NoNaNs; }
00972   bool hasNoInfs() const { return NoInfs; }
00973   bool hasNoSignedZeros() const { return NoSignedZeros; }
00974   bool hasAllowReciprocal() const { return AllowReciprocal; }
00975 
00976   /// Return a raw encoding of the flags.
00977   /// This function should only be used to add data to the NodeID value.
00978   unsigned getRawFlags() const {
00979     return (NoUnsignedWrap << 0) | (NoSignedWrap << 1) | (Exact << 2) |
00980            (UnsafeAlgebra << 3) | (NoNaNs << 4) | (NoInfs << 5) |
00981            (NoSignedZeros << 6) | (AllowReciprocal << 7);
00982   }
00983 };
00984 
00985 /// This class is used for single-operand SDNodes.  This is solely
00986 /// to allow co-allocation of node operands with the node itself.
00987 class UnarySDNode : public SDNode {
00988   SDUse Op;
00989 public:
00990   UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
00991               SDValue X)
00992     : SDNode(Opc, Order, dl, VTs) {
00993     InitOperands(&Op, X);
00994   }
00995 };
00996 
00997 /// This class is used for two-operand SDNodes.  This is solely
00998 /// to allow co-allocation of node operands with the node itself.
00999 class BinarySDNode : public SDNode {
01000   SDUse Ops[2];
01001 public:
01002   BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01003                SDValue X, SDValue Y)
01004     : SDNode(Opc, Order, dl, VTs) {
01005     InitOperands(Ops, X, Y);
01006   }
01007 };
01008 
01009 /// Returns true if the opcode is a binary operation with flags.
01010 static bool isBinOpWithFlags(unsigned Opcode) {
01011   switch (Opcode) {
01012   case ISD::SDIV:
01013   case ISD::UDIV:
01014   case ISD::SRA:
01015   case ISD::SRL:
01016   case ISD::MUL:
01017   case ISD::ADD:
01018   case ISD::SUB:
01019   case ISD::SHL:
01020     return true;
01021   default:
01022     return false;
01023   }
01024 }
01025 
01026 /// This class is an extension of BinarySDNode
01027 /// used from those opcodes that have associated extra flags.
01028 class BinaryWithFlagsSDNode : public BinarySDNode {
01029 public:
01030   SDNodeFlags Flags;
01031   BinaryWithFlagsSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01032                         SDValue X, SDValue Y)
01033       : BinarySDNode(Opc, Order, dl, VTs, X, Y), Flags() {}
01034   static bool classof(const SDNode *N) {
01035     return isBinOpWithFlags(N->getOpcode());
01036   }
01037 };
01038 
01039 /// This class is used for three-operand SDNodes. This is solely
01040 /// to allow co-allocation of node operands with the node itself.
01041 class TernarySDNode : public SDNode {
01042   SDUse Ops[3];
01043 public:
01044   TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01045                 SDValue X, SDValue Y, SDValue Z)
01046     : SDNode(Opc, Order, dl, VTs) {
01047     InitOperands(Ops, X, Y, Z);
01048   }
01049 };
01050 
01051 
01052 /// This class is used to form a handle around another node that
01053 /// is persistent and is updated across invocations of replaceAllUsesWith on its
01054 /// operand.  This node should be directly created by end-users and not added to
01055 /// the AllNodes list.
01056 class HandleSDNode : public SDNode {
01057   SDUse Op;
01058 public:
01059   explicit HandleSDNode(SDValue X)
01060     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
01061     InitOperands(&Op, X);
01062   }
01063   ~HandleSDNode();
01064   const SDValue &getValue() const { return Op; }
01065 };
01066 
01067 class AddrSpaceCastSDNode : public UnarySDNode {
01068 private:
01069   unsigned SrcAddrSpace;
01070   unsigned DestAddrSpace;
01071 
01072 public:
01073   AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
01074                       unsigned SrcAS, unsigned DestAS);
01075 
01076   unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
01077   unsigned getDestAddressSpace() const { return DestAddrSpace; }
01078 
01079   static bool classof(const SDNode *N) {
01080     return N->getOpcode() == ISD::ADDRSPACECAST;
01081   }
01082 };
01083 
01084 /// This is an abstract virtual class for memory operations.
01085 class MemSDNode : public SDNode {
01086 private:
01087   // VT of in-memory value.
01088   EVT MemoryVT;
01089 
01090 protected:
01091   /// Memory reference information.
01092   MachineMemOperand *MMO;
01093 
01094 public:
01095   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01096             EVT MemoryVT, MachineMemOperand *MMO);
01097 
01098   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01099             ArrayRef<SDValue> Ops, EVT MemoryVT, MachineMemOperand *MMO);
01100 
01101   bool readMem() const { return MMO->isLoad(); }
01102   bool writeMem() const { return MMO->isStore(); }
01103 
01104   /// Returns alignment and volatility of the memory access
01105   unsigned getOriginalAlignment() const {
01106     return MMO->getBaseAlignment();
01107   }
01108   unsigned getAlignment() const {
01109     return MMO->getAlignment();
01110   }
01111 
01112   /// Return the SubclassData value, which contains an
01113   /// encoding of the volatile flag, as well as bits used by subclasses. This
01114   /// function should only be used to compute a FoldingSetNodeID value.
01115   unsigned getRawSubclassData() const {
01116     return SubclassData;
01117   }
01118 
01119   // We access subclass data here so that we can check consistency
01120   // with MachineMemOperand information.
01121   bool isVolatile() const { return (SubclassData >> 5) & 1; }
01122   bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
01123   bool isInvariant() const { return (SubclassData >> 7) & 1; }
01124 
01125   AtomicOrdering getOrdering() const {
01126     return AtomicOrdering((SubclassData >> 8) & 15);
01127   }
01128   SynchronizationScope getSynchScope() const {
01129     return SynchronizationScope((SubclassData >> 12) & 1);
01130   }
01131 
01132   // Returns the offset from the location of the access.
01133   int64_t getSrcValueOffset() const { return MMO->getOffset(); }
01134 
01135   /// Returns the AA info that describes the dereference.
01136   AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
01137 
01138   /// Returns the Ranges that describes the dereference.
01139   const MDNode *getRanges() const { return MMO->getRanges(); }
01140 
01141   /// Return the type of the in-memory value.
01142   EVT getMemoryVT() const { return MemoryVT; }
01143 
01144   /// Return a MachineMemOperand object describing the memory
01145   /// reference performed by operation.
01146   MachineMemOperand *getMemOperand() const { return MMO; }
01147 
01148   const MachinePointerInfo &getPointerInfo() const {
01149     return MMO->getPointerInfo();
01150   }
01151 
01152   /// Return the address space for the associated pointer
01153   unsigned getAddressSpace() const {
01154     return getPointerInfo().getAddrSpace();
01155   }
01156 
01157   /// Update this MemSDNode's MachineMemOperand information
01158   /// to reflect the alignment of NewMMO, if it has a greater alignment.
01159   /// This must only be used when the new alignment applies to all users of
01160   /// this MachineMemOperand.
01161   void refineAlignment(const MachineMemOperand *NewMMO) {
01162     MMO->refineAlignment(NewMMO);
01163   }
01164 
01165   const SDValue &getChain() const { return getOperand(0); }
01166   const SDValue &getBasePtr() const {
01167     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
01168   }
01169 
01170   // Methods to support isa and dyn_cast
01171   static bool classof(const SDNode *N) {
01172     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
01173     // with either an intrinsic or a target opcode.
01174     return N->getOpcode() == ISD::LOAD                ||
01175            N->getOpcode() == ISD::STORE               ||
01176            N->getOpcode() == ISD::PREFETCH            ||
01177            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
01178            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
01179            N->getOpcode() == ISD::ATOMIC_SWAP         ||
01180            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
01181            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
01182            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
01183            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
01184            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
01185            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
01186            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
01187            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
01188            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
01189            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
01190            N->getOpcode() == ISD::ATOMIC_LOAD         ||
01191            N->getOpcode() == ISD::ATOMIC_STORE        ||
01192            N->getOpcode() == ISD::MLOAD               ||
01193            N->getOpcode() == ISD::MSTORE              ||
01194            N->getOpcode() == ISD::MGATHER             ||
01195            N->getOpcode() == ISD::MSCATTER            ||
01196            N->isMemIntrinsic()                        ||
01197            N->isTargetMemoryOpcode();
01198   }
01199 };
01200 
01201 /// This is an SDNode representing atomic operations.
01202 class AtomicSDNode : public MemSDNode {
01203   SDUse Ops[4];
01204 
01205   /// For cmpxchg instructions, the ordering requirements when a store does not
01206   /// occur.
01207   AtomicOrdering FailureOrdering;
01208 
01209   void InitAtomic(AtomicOrdering SuccessOrdering,
01210                   AtomicOrdering FailureOrdering,
01211                   SynchronizationScope SynchScope) {
01212     // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
01213     assert((SuccessOrdering & 15) == SuccessOrdering &&
01214            "Ordering may not require more than 4 bits!");
01215     assert((FailureOrdering & 15) == FailureOrdering &&
01216            "Ordering may not require more than 4 bits!");
01217     assert((SynchScope & 1) == SynchScope &&
01218            "SynchScope may not require more than 1 bit!");
01219     SubclassData |= SuccessOrdering << 8;
01220     SubclassData |= SynchScope << 12;
01221     this->FailureOrdering = FailureOrdering;
01222     assert(getSuccessOrdering() == SuccessOrdering &&
01223            "Ordering encoding error!");
01224     assert(getFailureOrdering() == FailureOrdering &&
01225            "Ordering encoding error!");
01226     assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
01227   }
01228 
01229 public:
01230   // Opc:   opcode for atomic
01231   // VTL:    value type list
01232   // Chain:  memory chain for operaand
01233   // Ptr:    address to update as a SDValue
01234   // Cmp:    compare value
01235   // Swp:    swap value
01236   // SrcVal: address to update as a Value (used for MemOperand)
01237   // Align:  alignment of memory
01238   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
01239                EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
01240                MachineMemOperand *MMO, AtomicOrdering Ordering,
01241                SynchronizationScope SynchScope)
01242       : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01243     InitAtomic(Ordering, Ordering, SynchScope);
01244     InitOperands(Ops, Chain, Ptr, Cmp, Swp);
01245   }
01246   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
01247                EVT MemVT,
01248                SDValue Chain, SDValue Ptr,
01249                SDValue Val, MachineMemOperand *MMO,
01250                AtomicOrdering Ordering, SynchronizationScope SynchScope)
01251     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01252     InitAtomic(Ordering, Ordering, SynchScope);
01253     InitOperands(Ops, Chain, Ptr, Val);
01254   }
01255   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
01256                EVT MemVT,
01257                SDValue Chain, SDValue Ptr,
01258                MachineMemOperand *MMO,
01259                AtomicOrdering Ordering, SynchronizationScope SynchScope)
01260     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01261     InitAtomic(Ordering, Ordering, SynchScope);
01262     InitOperands(Ops, Chain, Ptr);
01263   }
01264   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
01265                const SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
01266                MachineMemOperand *MMO,
01267                AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
01268                SynchronizationScope SynchScope)
01269     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
01270     InitAtomic(SuccessOrdering, FailureOrdering, SynchScope);
01271     assert((DynOps || NumOps <= array_lengthof(Ops)) &&
01272            "Too many ops for internal storage!");
01273     InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
01274   }
01275 
01276   const SDValue &getBasePtr() const { return getOperand(1); }
01277   const SDValue &getVal() const { return getOperand(2); }
01278 
01279   AtomicOrdering getSuccessOrdering() const {
01280     return getOrdering();
01281   }
01282 
01283   // Not quite enough room in SubclassData for everything, so failure gets its
01284   // own field.
01285   AtomicOrdering getFailureOrdering() const {
01286     return FailureOrdering;
01287   }
01288 
01289   bool isCompareAndSwap() const {
01290     unsigned Op = getOpcode();
01291     return Op == ISD::ATOMIC_CMP_SWAP || Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
01292   }
01293 
01294   // Methods to support isa and dyn_cast
01295   static bool classof(const SDNode *N) {
01296     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
01297            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
01298            N->getOpcode() == ISD::ATOMIC_SWAP         ||
01299            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
01300            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
01301            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
01302            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
01303            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
01304            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
01305            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
01306            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
01307            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
01308            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
01309            N->getOpcode() == ISD::ATOMIC_LOAD         ||
01310            N->getOpcode() == ISD::ATOMIC_STORE;
01311   }
01312 };
01313 
01314 /// This SDNode is used for target intrinsics that touch
01315 /// memory and need an associated MachineMemOperand. Its opcode may be
01316 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
01317 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
01318 class MemIntrinsicSDNode : public MemSDNode {
01319 public:
01320   MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
01321                      ArrayRef<SDValue> Ops, EVT MemoryVT,
01322                      MachineMemOperand *MMO)
01323     : MemSDNode(Opc, Order, dl, VTs, Ops, MemoryVT, MMO) {
01324     SubclassData |= 1u << 13;
01325   }
01326 
01327   // Methods to support isa and dyn_cast
01328   static bool classof(const SDNode *N) {
01329     // We lower some target intrinsics to their target opcode
01330     // early a node with a target opcode can be of this class
01331     return N->isMemIntrinsic()             ||
01332            N->getOpcode() == ISD::PREFETCH ||
01333            N->isTargetMemoryOpcode();
01334   }
01335 };
01336 
01337 /// This SDNode is used to implement the code generator
01338 /// support for the llvm IR shufflevector instruction.  It combines elements
01339 /// from two input vectors into a new input vector, with the selection and
01340 /// ordering of elements determined by an array of integers, referred to as
01341 /// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
01342 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
01343 /// An index of -1 is treated as undef, such that the code generator may put
01344 /// any value in the corresponding element of the result.
01345 class ShuffleVectorSDNode : public SDNode {
01346   SDUse Ops[2];
01347 
01348   // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
01349   // is freed when the SelectionDAG object is destroyed.
01350   const int *Mask;
01351 protected:
01352   friend class SelectionDAG;
01353   ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
01354                       SDValue N2, const int *M)
01355     : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
01356     InitOperands(Ops, N1, N2);
01357   }
01358 public:
01359 
01360   ArrayRef<int> getMask() const {
01361     EVT VT = getValueType(0);
01362     return makeArrayRef(Mask, VT.getVectorNumElements());
01363   }
01364   int getMaskElt(unsigned Idx) const {
01365     assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
01366     return Mask[Idx];
01367   }
01368 
01369   bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
01370   int  getSplatIndex() const {
01371     assert(isSplat() && "Cannot get splat index for non-splat!");
01372     EVT VT = getValueType(0);
01373     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
01374       if (Mask[i] >= 0)
01375         return Mask[i];
01376     }
01377     llvm_unreachable("Splat with all undef indices?");
01378   }
01379   static bool isSplatMask(const int *Mask, EVT VT);
01380 
01381   /// Change values in a shuffle permute mask assuming
01382   /// the two vector operands have swapped position.
01383   static void commuteMask(SmallVectorImpl<int> &Mask) {
01384     unsigned NumElems = Mask.size();
01385     for (unsigned i = 0; i != NumElems; ++i) {
01386       int idx = Mask[i];
01387       if (idx < 0)
01388         continue;
01389       else if (idx < (int)NumElems)
01390         Mask[i] = idx + NumElems;
01391       else
01392         Mask[i] = idx - NumElems;
01393     }
01394   }
01395 
01396   static bool classof(const SDNode *N) {
01397     return N->getOpcode() == ISD::VECTOR_SHUFFLE;
01398   }
01399 };
01400 
01401 class ConstantSDNode : public SDNode {
01402   const ConstantInt *Value;
01403   friend class SelectionDAG;
01404   ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val,
01405                  DebugLoc DL, EVT VT)
01406     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
01407              0, DL, getSDVTList(VT)), Value(val) {
01408     SubclassData |= (uint16_t)isOpaque;
01409   }
01410 public:
01411 
01412   const ConstantInt *getConstantIntValue() const { return Value; }
01413   const APInt &getAPIntValue() const { return Value->getValue(); }
01414   uint64_t getZExtValue() const { return Value->getZExtValue(); }
01415   int64_t getSExtValue() const { return Value->getSExtValue(); }
01416 
01417   bool isOne() const { return Value->isOne(); }
01418   bool isNullValue() const { return Value->isNullValue(); }
01419   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
01420 
01421   bool isOpaque() const { return SubclassData & 1; }
01422 
01423   static bool classof(const SDNode *N) {
01424     return N->getOpcode() == ISD::Constant ||
01425            N->getOpcode() == ISD::TargetConstant;
01426   }
01427 };
01428 
01429 class ConstantFPSDNode : public SDNode {
01430   const ConstantFP *Value;
01431   friend class SelectionDAG;
01432   ConstantFPSDNode(bool isTarget, const ConstantFP *val, DebugLoc DL, EVT VT)
01433     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
01434              0, DL, getSDVTList(VT)), Value(val) {
01435   }
01436 public:
01437 
01438   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
01439   const ConstantFP *getConstantFPValue() const { return Value; }
01440 
01441   /// Return true if the value is positive or negative zero.
01442   bool isZero() const { return Value->isZero(); }
01443 
01444   /// Return true if the value is a NaN.
01445   bool isNaN() const { return Value->isNaN(); }
01446 
01447   /// Return true if the value is an infinity
01448   bool isInfinity() const { return Value->isInfinity(); }
01449 
01450   /// Return true if the value is negative.
01451   bool isNegative() const { return Value->isNegative(); }
01452 
01453   /// We don't rely on operator== working on double values, as
01454   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
01455   /// As such, this method can be used to do an exact bit-for-bit comparison of
01456   /// two floating point values.
01457 
01458   /// We leave the version with the double argument here because it's just so
01459   /// convenient to write "2.0" and the like.  Without this function we'd
01460   /// have to duplicate its logic everywhere it's called.
01461   bool isExactlyValue(double V) const {
01462     bool ignored;
01463     APFloat Tmp(V);
01464     Tmp.convert(Value->getValueAPF().getSemantics(),
01465                 APFloat::rmNearestTiesToEven, &ignored);
01466     return isExactlyValue(Tmp);
01467   }
01468   bool isExactlyValue(const APFloat& V) const;
01469 
01470   static bool isValueValidForType(EVT VT, const APFloat& Val);
01471 
01472   static bool classof(const SDNode *N) {
01473     return N->getOpcode() == ISD::ConstantFP ||
01474            N->getOpcode() == ISD::TargetConstantFP;
01475   }
01476 };
01477 
01478 class GlobalAddressSDNode : public SDNode {
01479   const GlobalValue *TheGlobal;
01480   int64_t Offset;
01481   unsigned char TargetFlags;
01482   friend class SelectionDAG;
01483   GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
01484                       const GlobalValue *GA, EVT VT, int64_t o,
01485                       unsigned char TargetFlags);
01486 public:
01487 
01488   const GlobalValue *getGlobal() const { return TheGlobal; }
01489   int64_t getOffset() const { return Offset; }
01490   unsigned char getTargetFlags() const { return TargetFlags; }
01491   // Return the address space this GlobalAddress belongs to.
01492   unsigned getAddressSpace() const;
01493 
01494   static bool classof(const SDNode *N) {
01495     return N->getOpcode() == ISD::GlobalAddress ||
01496            N->getOpcode() == ISD::TargetGlobalAddress ||
01497            N->getOpcode() == ISD::GlobalTLSAddress ||
01498            N->getOpcode() == ISD::TargetGlobalTLSAddress;
01499   }
01500 };
01501 
01502 class FrameIndexSDNode : public SDNode {
01503   int FI;
01504   friend class SelectionDAG;
01505   FrameIndexSDNode(int fi, EVT VT, bool isTarg)
01506     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
01507       0, DebugLoc(), getSDVTList(VT)), FI(fi) {
01508   }
01509 public:
01510 
01511   int getIndex() const { return FI; }
01512 
01513   static bool classof(const SDNode *N) {
01514     return N->getOpcode() == ISD::FrameIndex ||
01515            N->getOpcode() == ISD::TargetFrameIndex;
01516   }
01517 };
01518 
01519 class JumpTableSDNode : public SDNode {
01520   int JTI;
01521   unsigned char TargetFlags;
01522   friend class SelectionDAG;
01523   JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
01524     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
01525       0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
01526   }
01527 public:
01528 
01529   int getIndex() const { return JTI; }
01530   unsigned char getTargetFlags() const { return TargetFlags; }
01531 
01532   static bool classof(const SDNode *N) {
01533     return N->getOpcode() == ISD::JumpTable ||
01534            N->getOpcode() == ISD::TargetJumpTable;
01535   }
01536 };
01537 
01538 class ConstantPoolSDNode : public SDNode {
01539   union {
01540     const Constant *ConstVal;
01541     MachineConstantPoolValue *MachineCPVal;
01542   } Val;
01543   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
01544   unsigned Alignment;  // Minimum alignment requirement of CP (not log2 value).
01545   unsigned char TargetFlags;
01546   friend class SelectionDAG;
01547   ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
01548                      unsigned Align, unsigned char TF)
01549     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
01550              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
01551              TargetFlags(TF) {
01552     assert(Offset >= 0 && "Offset is too large");
01553     Val.ConstVal = c;
01554   }
01555   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
01556                      EVT VT, int o, unsigned Align, unsigned char TF)
01557     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
01558              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
01559              TargetFlags(TF) {
01560     assert(Offset >= 0 && "Offset is too large");
01561     Val.MachineCPVal = v;
01562     Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
01563   }
01564 public:
01565 
01566   bool isMachineConstantPoolEntry() const {
01567     return Offset < 0;
01568   }
01569 
01570   const Constant *getConstVal() const {
01571     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
01572     return Val.ConstVal;
01573   }
01574 
01575   MachineConstantPoolValue *getMachineCPVal() const {
01576     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
01577     return Val.MachineCPVal;
01578   }
01579 
01580   int getOffset() const {
01581     return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
01582   }
01583 
01584   // Return the alignment of this constant pool object, which is either 0 (for
01585   // default alignment) or the desired value.
01586   unsigned getAlignment() const { return Alignment; }
01587   unsigned char getTargetFlags() const { return TargetFlags; }
01588 
01589   Type *getType() const;
01590 
01591   static bool classof(const SDNode *N) {
01592     return N->getOpcode() == ISD::ConstantPool ||
01593            N->getOpcode() == ISD::TargetConstantPool;
01594   }
01595 };
01596 
01597 /// Completely target-dependent object reference.
01598 class TargetIndexSDNode : public SDNode {
01599   unsigned char TargetFlags;
01600   int Index;
01601   int64_t Offset;
01602   friend class SelectionDAG;
01603 public:
01604 
01605   TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
01606     : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
01607       TargetFlags(TF), Index(Idx), Offset(Ofs) {}
01608 public:
01609 
01610   unsigned char getTargetFlags() const { return TargetFlags; }
01611   int getIndex() const { return Index; }
01612   int64_t getOffset() const { return Offset; }
01613 
01614   static bool classof(const SDNode *N) {
01615     return N->getOpcode() == ISD::TargetIndex;
01616   }
01617 };
01618 
01619 class BasicBlockSDNode : public SDNode {
01620   MachineBasicBlock *MBB;
01621   friend class SelectionDAG;
01622   /// Debug info is meaningful and potentially useful here, but we create
01623   /// blocks out of order when they're jumped to, which makes it a bit
01624   /// harder.  Let's see if we need it first.
01625   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
01626     : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
01627   {}
01628 public:
01629 
01630   MachineBasicBlock *getBasicBlock() const { return MBB; }
01631 
01632   static bool classof(const SDNode *N) {
01633     return N->getOpcode() == ISD::BasicBlock;
01634   }
01635 };
01636 
01637 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
01638 class BuildVectorSDNode : public SDNode {
01639   // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
01640   explicit BuildVectorSDNode() = delete;
01641 public:
01642   /// Check if this is a constant splat, and if so, find the
01643   /// smallest element size that splats the vector.  If MinSplatBits is
01644   /// nonzero, the element size must be at least that large.  Note that the
01645   /// splat element may be the entire vector (i.e., a one element vector).
01646   /// Returns the splat element value in SplatValue.  Any undefined bits in
01647   /// that value are zero, and the corresponding bits in the SplatUndef mask
01648   /// are set.  The SplatBitSize value is set to the splat element size in
01649   /// bits.  HasAnyUndefs is set to true if any bits in the vector are
01650   /// undefined.  isBigEndian describes the endianness of the target.
01651   bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
01652                        unsigned &SplatBitSize, bool &HasAnyUndefs,
01653                        unsigned MinSplatBits = 0,
01654                        bool isBigEndian = false) const;
01655 
01656   /// \brief Returns the splatted value or a null value if this is not a splat.
01657   ///
01658   /// If passed a non-null UndefElements bitvector, it will resize it to match
01659   /// the vector width and set the bits where elements are undef.
01660   SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
01661 
01662   /// \brief Returns the splatted constant or null if this is not a constant
01663   /// splat.
01664   ///
01665   /// If passed a non-null UndefElements bitvector, it will resize it to match
01666   /// the vector width and set the bits where elements are undef.
01667   ConstantSDNode *
01668   getConstantSplatNode(BitVector *UndefElements = nullptr) const;
01669 
01670   /// \brief Returns the splatted constant FP or null if this is not a constant
01671   /// FP splat.
01672   ///
01673   /// If passed a non-null UndefElements bitvector, it will resize it to match
01674   /// the vector width and set the bits where elements are undef.
01675   ConstantFPSDNode *
01676   getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
01677 
01678   bool isConstant() const;
01679 
01680   static inline bool classof(const SDNode *N) {
01681     return N->getOpcode() == ISD::BUILD_VECTOR;
01682   }
01683 };
01684 
01685 /// An SDNode that holds an arbitrary LLVM IR Value. This is
01686 /// used when the SelectionDAG needs to make a simple reference to something
01687 /// in the LLVM IR representation.
01688 ///
01689 class SrcValueSDNode : public SDNode {
01690   const Value *V;
01691   friend class SelectionDAG;
01692   /// Create a SrcValue for a general value.
01693   explicit SrcValueSDNode(const Value *v)
01694     : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
01695 
01696 public:
01697   /// Return the contained Value.
01698   const Value *getValue() const { return V; }
01699 
01700   static bool classof(const SDNode *N) {
01701     return N->getOpcode() == ISD::SRCVALUE;
01702   }
01703 };
01704 
01705 class MDNodeSDNode : public SDNode {
01706   const MDNode *MD;
01707   friend class SelectionDAG;
01708   explicit MDNodeSDNode(const MDNode *md)
01709   : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
01710   {}
01711 public:
01712 
01713   const MDNode *getMD() const { return MD; }
01714 
01715   static bool classof(const SDNode *N) {
01716     return N->getOpcode() == ISD::MDNODE_SDNODE;
01717   }
01718 };
01719 
01720 class RegisterSDNode : public SDNode {
01721   unsigned Reg;
01722   friend class SelectionDAG;
01723   RegisterSDNode(unsigned reg, EVT VT)
01724     : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
01725   }
01726 public:
01727 
01728   unsigned getReg() const { return Reg; }
01729 
01730   static bool classof(const SDNode *N) {
01731     return N->getOpcode() == ISD::Register;
01732   }
01733 };
01734 
01735 class RegisterMaskSDNode : public SDNode {
01736   // The memory for RegMask is not owned by the node.
01737   const uint32_t *RegMask;
01738   friend class SelectionDAG;
01739   RegisterMaskSDNode(const uint32_t *mask)
01740     : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
01741       RegMask(mask) {}
01742 public:
01743 
01744   const uint32_t *getRegMask() const { return RegMask; }
01745 
01746   static bool classof(const SDNode *N) {
01747     return N->getOpcode() == ISD::RegisterMask;
01748   }
01749 };
01750 
01751 class BlockAddressSDNode : public SDNode {
01752   const BlockAddress *BA;
01753   int64_t Offset;
01754   unsigned char TargetFlags;
01755   friend class SelectionDAG;
01756   BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
01757                      int64_t o, unsigned char Flags)
01758     : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
01759              BA(ba), Offset(o), TargetFlags(Flags) {
01760   }
01761 public:
01762   const BlockAddress *getBlockAddress() const { return BA; }
01763   int64_t getOffset() const { return Offset; }
01764   unsigned char getTargetFlags() const { return TargetFlags; }
01765 
01766   static bool classof(const SDNode *N) {
01767     return N->getOpcode() == ISD::BlockAddress ||
01768            N->getOpcode() == ISD::TargetBlockAddress;
01769   }
01770 };
01771 
01772 class EHLabelSDNode : public SDNode {
01773   SDUse Chain;
01774   MCSymbol *Label;
01775   friend class SelectionDAG;
01776   EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
01777     : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
01778     InitOperands(&Chain, ch);
01779   }
01780 public:
01781   MCSymbol *getLabel() const { return Label; }
01782 
01783   static bool classof(const SDNode *N) {
01784     return N->getOpcode() == ISD::EH_LABEL;
01785   }
01786 };
01787 
01788 class ExternalSymbolSDNode : public SDNode {
01789   const char *Symbol;
01790   unsigned char TargetFlags;
01791 
01792   friend class SelectionDAG;
01793   ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
01794     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
01795              0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
01796   }
01797 public:
01798 
01799   const char *getSymbol() const { return Symbol; }
01800   unsigned char getTargetFlags() const { return TargetFlags; }
01801 
01802   static bool classof(const SDNode *N) {
01803     return N->getOpcode() == ISD::ExternalSymbol ||
01804            N->getOpcode() == ISD::TargetExternalSymbol;
01805   }
01806 };
01807 
01808 class CondCodeSDNode : public SDNode {
01809   ISD::CondCode Condition;
01810   friend class SelectionDAG;
01811   explicit CondCodeSDNode(ISD::CondCode Cond)
01812     : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
01813       Condition(Cond) {
01814   }
01815 public:
01816 
01817   ISD::CondCode get() const { return Condition; }
01818 
01819   static bool classof(const SDNode *N) {
01820     return N->getOpcode() == ISD::CONDCODE;
01821   }
01822 };
01823 
01824 /// NOTE: avoid using this node as this may disappear in the
01825 /// future and most targets don't support it.
01826 class CvtRndSatSDNode : public SDNode {
01827   ISD::CvtCode CvtCode;
01828   friend class SelectionDAG;
01829   explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
01830                            ArrayRef<SDValue> Ops, ISD::CvtCode Code)
01831     : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops),
01832       CvtCode(Code) {
01833     assert(Ops.size() == 5 && "wrong number of operations");
01834   }
01835 public:
01836   ISD::CvtCode getCvtCode() const { return CvtCode; }
01837 
01838   static bool classof(const SDNode *N) {
01839     return N->getOpcode() == ISD::CONVERT_RNDSAT;
01840   }
01841 };
01842 
01843 /// This class is used to represent EVT's, which are used
01844 /// to parameterize some operations.
01845 class VTSDNode : public SDNode {
01846   EVT ValueType;
01847   friend class SelectionDAG;
01848   explicit VTSDNode(EVT VT)
01849     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
01850       ValueType(VT) {
01851   }
01852 public:
01853 
01854   EVT getVT() const { return ValueType; }
01855 
01856   static bool classof(const SDNode *N) {
01857     return N->getOpcode() == ISD::VALUETYPE;
01858   }
01859 };
01860 
01861 /// Base class for LoadSDNode and StoreSDNode
01862 class LSBaseSDNode : public MemSDNode {
01863   //! Operand array for load and store
01864   /*!
01865     \note Moving this array to the base class captures more
01866     common functionality shared between LoadSDNode and
01867     StoreSDNode
01868    */
01869   SDUse Ops[4];
01870 public:
01871   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
01872                SDValue *Operands, unsigned numOperands,
01873                SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
01874                MachineMemOperand *MMO)
01875     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
01876     SubclassData |= AM << 2;
01877     assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
01878     InitOperands(Ops, Operands, numOperands);
01879     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
01880            "Only indexed loads and stores have a non-undef offset operand");
01881   }
01882 
01883   const SDValue &getOffset() const {
01884     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
01885   }
01886 
01887   /// Return the addressing mode for this load or store:
01888   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
01889   ISD::MemIndexedMode getAddressingMode() const {
01890     return ISD::MemIndexedMode((SubclassData >> 2) & 7);
01891   }
01892 
01893   /// Return true if this is a pre/post inc/dec load/store.
01894   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
01895 
01896   /// Return true if this is NOT a pre/post inc/dec load/store.
01897   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
01898 
01899   static bool classof(const SDNode *N) {
01900     return N->getOpcode() == ISD::LOAD ||
01901            N->getOpcode() == ISD::STORE;
01902   }
01903 };
01904 
01905 /// This class is used to represent ISD::LOAD nodes.
01906 class LoadSDNode : public LSBaseSDNode {
01907   friend class SelectionDAG;
01908   LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
01909              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
01910              MachineMemOperand *MMO)
01911     : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
01912     SubclassData |= (unsigned short)ETy;
01913     assert(getExtensionType() == ETy && "LoadExtType encoding error!");
01914     assert(readMem() && "Load MachineMemOperand is not a load!");
01915     assert(!writeMem() && "Load MachineMemOperand is a store!");
01916   }
01917 public:
01918 
01919   /// Return whether this is a plain node,
01920   /// or one of the varieties of value-extending loads.
01921   ISD::LoadExtType getExtensionType() const {
01922     return ISD::LoadExtType(SubclassData & 3);
01923   }
01924 
01925   const SDValue &getBasePtr() const { return getOperand(1); }
01926   const SDValue &getOffset() const { return getOperand(2); }
01927 
01928   static bool classof(const SDNode *N) {
01929     return N->getOpcode() == ISD::LOAD;
01930   }
01931 };
01932 
01933 /// This class is used to represent ISD::STORE nodes.
01934 class StoreSDNode : public LSBaseSDNode {
01935   friend class SelectionDAG;
01936   StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
01937               SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
01938               MachineMemOperand *MMO)
01939     : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
01940                    VTs, AM, MemVT, MMO) {
01941     SubclassData |= (unsigned short)isTrunc;
01942     assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
01943     assert(!readMem() && "Store MachineMemOperand is a load!");
01944     assert(writeMem() && "Store MachineMemOperand is not a store!");
01945   }
01946 public:
01947 
01948   /// Return true if the op does a truncation before store.
01949   /// For integers this is the same as doing a TRUNCATE and storing the result.
01950   /// For floats, it is the same as doing an FP_ROUND and storing the result.
01951   bool isTruncatingStore() const { return SubclassData & 1; }
01952 
01953   const SDValue &getValue() const { return getOperand(1); }
01954   const SDValue &getBasePtr() const { return getOperand(2); }
01955   const SDValue &getOffset() const { return getOperand(3); }
01956 
01957   static bool classof(const SDNode *N) {
01958     return N->getOpcode() == ISD::STORE;
01959   }
01960 };
01961 
01962 /// This base class is used to represent MLOAD and MSTORE nodes
01963 class MaskedLoadStoreSDNode : public MemSDNode {
01964   // Operands
01965   SDUse Ops[4];
01966 public:
01967   friend class SelectionDAG;
01968   MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
01969                    SDValue *Operands, unsigned numOperands, 
01970                    SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
01971     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
01972     InitOperands(Ops, Operands, numOperands);
01973   }
01974 
01975   // In the both nodes address is Op1, mask is Op2:
01976   // MaskedLoadSDNode (Chain, ptr, mask, src0), src0 is a passthru value
01977   // MaskedStoreSDNode (Chain, ptr, mask, data)
01978   // Mask is a vector of i1 elements
01979   const SDValue &getBasePtr() const { return getOperand(1); }
01980   const SDValue &getMask() const    { return getOperand(2); }
01981 
01982   static bool classof(const SDNode *N) {
01983     return N->getOpcode() == ISD::MLOAD ||
01984            N->getOpcode() == ISD::MSTORE;
01985   }
01986 };
01987 
01988 /// This class is used to represent an MLOAD node
01989 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
01990 public:
01991   friend class SelectionDAG;
01992   MaskedLoadSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
01993                    unsigned numOperands, SDVTList VTs, ISD::LoadExtType ETy,
01994                    EVT MemVT, MachineMemOperand *MMO)
01995     : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, Operands, numOperands,
01996                             VTs, MemVT, MMO) {
01997     SubclassData |= (unsigned short)ETy;
01998   }
01999 
02000   ISD::LoadExtType getExtensionType() const {
02001     return ISD::LoadExtType(SubclassData & 3);
02002   } 
02003   const SDValue &getSrc0() const { return getOperand(3); }
02004   static bool classof(const SDNode *N) {
02005     return N->getOpcode() == ISD::MLOAD;
02006   }
02007 };
02008 
02009 /// This class is used to represent an MSTORE node
02010 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
02011 
02012 public:
02013   friend class SelectionDAG;
02014   MaskedStoreSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
02015                     unsigned numOperands, SDVTList VTs, bool isTrunc, EVT MemVT,
02016                     MachineMemOperand *MMO)
02017     : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, Operands, numOperands,
02018                             VTs, MemVT, MMO) {
02019       SubclassData |= (unsigned short)isTrunc;
02020   }
02021   /// Return true if the op does a truncation before store.
02022   /// For integers this is the same as doing a TRUNCATE and storing the result.
02023   /// For floats, it is the same as doing an FP_ROUND and storing the result.
02024   bool isTruncatingStore() const { return SubclassData & 1; }
02025 
02026   const SDValue &getValue() const { return getOperand(3); }
02027 
02028   static bool classof(const SDNode *N) {
02029     return N->getOpcode() == ISD::MSTORE;
02030   }
02031 };
02032 
02033 /// This is a base class used to represent
02034 /// MGATHER and MSCATTER nodes
02035 ///
02036 class MaskedGatherScatterSDNode : public MemSDNode {
02037   // Operands
02038   SDUse Ops[5];
02039 public:
02040   friend class SelectionDAG;
02041   MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
02042                             ArrayRef<SDValue> Operands, SDVTList VTs, EVT MemVT,
02043                             MachineMemOperand *MMO)
02044     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
02045     assert(Operands.size() == 5 && "Incompatible number of operands");
02046     InitOperands(Ops, Operands.data(), Operands.size());
02047   }
02048 
02049   // In the both nodes address is Op1, mask is Op2:
02050   // MaskedGatherSDNode  (Chain, src0, mask, base, index), src0 is a passthru value
02051   // MaskedScatterSDNode (Chain, value, mask, base, index)
02052   // Mask is a vector of i1 elements
02053   const SDValue &getBasePtr() const { return getOperand(3); }
02054   const SDValue &getIndex()   const { return getOperand(4); }
02055   const SDValue &getMask()    const { return getOperand(2); }
02056   const SDValue &getValue()   const { return getOperand(1); }
02057 
02058   static bool classof(const SDNode *N) {
02059     return N->getOpcode() == ISD::MGATHER ||
02060            N->getOpcode() == ISD::MSCATTER;
02061   }
02062 };
02063 
02064 /// This class is used to represent an MGATHER node
02065 ///
02066 class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
02067 public:
02068   friend class SelectionDAG;
02069   MaskedGatherSDNode(unsigned Order, DebugLoc dl, ArrayRef<SDValue> Operands, 
02070                      SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
02071     : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, Operands, VTs, MemVT,
02072                                 MMO) {
02073     assert(getValue().getValueType() == getValueType(0) &&
02074            "Incompatible type of the PathThru value in MaskedGatherSDNode");
02075     assert(getMask().getValueType().getVectorNumElements() == 
02076            getValueType(0).getVectorNumElements() && 
02077            "Vector width mismatch between mask and data");
02078     assert(getMask().getValueType().getScalarType() == MVT::i1 && 
02079            "Vector width mismatch between mask and data");
02080   }
02081 
02082   static bool classof(const SDNode *N) {
02083     return N->getOpcode() == ISD::MGATHER;
02084   }
02085 };
02086 
02087 /// This class is used to represent an MSCATTER node
02088 ///
02089 class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
02090 
02091 public:
02092   friend class SelectionDAG;
02093   MaskedScatterSDNode(unsigned Order, DebugLoc dl,ArrayRef<SDValue> Operands,
02094                       SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
02095     : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, Operands, VTs, MemVT,
02096                                 MMO) {
02097     assert(getMask().getValueType().getVectorNumElements() == 
02098            getValue().getValueType().getVectorNumElements() && 
02099            "Vector width mismatch between mask and data");
02100     assert(getMask().getValueType().getScalarType() == MVT::i1 && 
02101            "Vector width mismatch between mask and data");
02102   }
02103 
02104   static bool classof(const SDNode *N) {
02105     return N->getOpcode() == ISD::MSCATTER;
02106   }
02107 };
02108 
02109 /// An SDNode that represents everything that will be needed
02110 /// to construct a MachineInstr. These nodes are created during the
02111 /// instruction selection proper phase.
02112 class MachineSDNode : public SDNode {
02113 public:
02114   typedef MachineMemOperand **mmo_iterator;
02115 
02116 private:
02117   friend class SelectionDAG;
02118   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
02119     : SDNode(Opc, Order, DL, VTs), MemRefs(nullptr), MemRefsEnd(nullptr) {}
02120 
02121   /// Operands for this instruction, if they fit here. If
02122   /// they don't, this field is unused.
02123   SDUse LocalOperands[4];
02124 
02125   /// Memory reference descriptions for this instruction.
02126   mmo_iterator MemRefs;
02127   mmo_iterator MemRefsEnd;
02128 
02129 public:
02130   mmo_iterator memoperands_begin() const { return MemRefs; }
02131   mmo_iterator memoperands_end() const { return MemRefsEnd; }
02132   bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
02133 
02134   /// Assign this MachineSDNodes's memory reference descriptor
02135   /// list. This does not transfer ownership.
02136   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
02137     for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
02138       assert(*MMI && "Null mem ref detected!");
02139     MemRefs = NewMemRefs;
02140     MemRefsEnd = NewMemRefsEnd;
02141   }
02142 
02143   static bool classof(const SDNode *N) {
02144     return N->isMachineOpcode();
02145   }
02146 };
02147 
02148 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
02149                                             SDNode, ptrdiff_t> {
02150   const SDNode *Node;
02151   unsigned Operand;
02152 
02153   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
02154 public:
02155   bool operator==(const SDNodeIterator& x) const {
02156     return Operand == x.Operand;
02157   }
02158   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
02159 
02160   pointer operator*() const {
02161     return Node->getOperand(Operand).getNode();
02162   }
02163   pointer operator->() const { return operator*(); }
02164 
02165   SDNodeIterator& operator++() {                // Preincrement
02166     ++Operand;
02167     return *this;
02168   }
02169   SDNodeIterator operator++(int) { // Postincrement
02170     SDNodeIterator tmp = *this; ++*this; return tmp;
02171   }
02172   size_t operator-(SDNodeIterator Other) const {
02173     assert(Node == Other.Node &&
02174            "Cannot compare iterators of two different nodes!");
02175     return Operand - Other.Operand;
02176   }
02177 
02178   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
02179   static SDNodeIterator end  (const SDNode *N) {
02180     return SDNodeIterator(N, N->getNumOperands());
02181   }
02182 
02183   unsigned getOperand() const { return Operand; }
02184   const SDNode *getNode() const { return Node; }
02185 };
02186 
02187 template <> struct GraphTraits<SDNode*> {
02188   typedef SDNode NodeType;
02189   typedef SDNodeIterator ChildIteratorType;
02190   static inline NodeType *getEntryNode(SDNode *N) { return N; }
02191   static inline ChildIteratorType child_begin(NodeType *N) {
02192     return SDNodeIterator::begin(N);
02193   }
02194   static inline ChildIteratorType child_end(NodeType *N) {
02195     return SDNodeIterator::end(N);
02196   }
02197 };
02198 
02199 /// The largest SDNode class.
02200 typedef MaskedGatherScatterSDNode LargestSDNode;
02201 
02202 /// The SDNode class with the greatest alignment requirement.
02203 typedef GlobalAddressSDNode MostAlignedSDNode;
02204 
02205 namespace ISD {
02206   /// Returns true if the specified node is a non-extending and unindexed load.
02207   inline bool isNormalLoad(const SDNode *N) {
02208     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
02209     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
02210       Ld->getAddressingMode() == ISD::UNINDEXED;
02211   }
02212 
02213   /// Returns true if the specified node is a non-extending load.
02214   inline bool isNON_EXTLoad(const SDNode *N) {
02215     return isa<LoadSDNode>(N) &&
02216       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
02217   }
02218 
02219   /// Returns true if the specified node is a EXTLOAD.
02220   inline bool isEXTLoad(const SDNode *N) {
02221     return isa<LoadSDNode>(N) &&
02222       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
02223   }
02224 
02225   /// Returns true if the specified node is a SEXTLOAD.
02226   inline bool isSEXTLoad(const SDNode *N) {
02227     return isa<LoadSDNode>(N) &&
02228       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
02229   }
02230 
02231   /// Returns true if the specified node is a ZEXTLOAD.
02232   inline bool isZEXTLoad(const SDNode *N) {
02233     return isa<LoadSDNode>(N) &&
02234       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
02235   }
02236 
02237   /// Returns true if the specified node is an unindexed load.
02238   inline bool isUNINDEXEDLoad(const SDNode *N) {
02239     return isa<LoadSDNode>(N) &&
02240       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
02241   }
02242 
02243   /// Returns true if the specified node is a non-truncating
02244   /// and unindexed store.
02245   inline bool isNormalStore(const SDNode *N) {
02246     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
02247     return St && !St->isTruncatingStore() &&
02248       St->getAddressingMode() == ISD::UNINDEXED;
02249   }
02250 
02251   /// Returns true if the specified node is a non-truncating store.
02252   inline bool isNON_TRUNCStore(const SDNode *N) {
02253     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
02254   }
02255 
02256   /// Returns true if the specified node is a truncating store.
02257   inline bool isTRUNCStore(const SDNode *N) {
02258     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
02259   }
02260 
02261   /// Returns true if the specified node is an unindexed store.
02262   inline bool isUNINDEXEDStore(const SDNode *N) {
02263     return isa<StoreSDNode>(N) &&
02264       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
02265   }
02266 }
02267 
02268 } // end llvm namespace
02269 
02270 #endif