SelectionDAGNodes.h revision 263508
1//===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the SDNode class and derived classes, which are used to
11// represent the nodes and operations present in a SelectionDAG.  These nodes
12// and operations are machine code level operations, with some similarities to
13// the GCC RTL representation.
14//
15// Clients should include the SelectionDAG.h file instead of this file directly.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
20#define LLVM_CODEGEN_SELECTIONDAGNODES_H
21
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/GraphTraits.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/ilist_node.h"
28#include "llvm/CodeGen/ISDOpcodes.h"
29#include "llvm/CodeGen/MachineMemOperand.h"
30#include "llvm/CodeGen/ValueTypes.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/Support/DataTypes.h"
34#include "llvm/Support/DebugLoc.h"
35#include "llvm/Support/MathExtras.h"
36#include <cassert>
37
38namespace llvm {
39
40class SelectionDAG;
41class GlobalValue;
42class MachineBasicBlock;
43class MachineConstantPoolValue;
44class SDNode;
45class Value;
46class MCSymbol;
47template <typename T> struct DenseMapInfo;
48template <typename T> struct simplify_type;
49template <typename T> struct ilist_traits;
50
51void checkForCycles(const SDNode *N);
52
53/// SDVTList - This represents a list of ValueType's that has been intern'd by
54/// a SelectionDAG.  Instances of this simple value class are returned by
55/// SelectionDAG::getVTList(...).
56///
57struct SDVTList {
58  const EVT *VTs;
59  unsigned int NumVTs;
60};
61
62namespace ISD {
63  /// Node predicates
64
65  /// isBuildVectorAllOnes - Return true if the specified node is a
66  /// BUILD_VECTOR where all of the elements are ~0 or undef.
67  bool isBuildVectorAllOnes(const SDNode *N);
68
69  /// isBuildVectorAllZeros - Return true if the specified node is a
70  /// BUILD_VECTOR where all of the elements are 0 or undef.
71  bool isBuildVectorAllZeros(const SDNode *N);
72
73  /// isScalarToVector - Return true if the specified node is a
74  /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
75  /// element is not an undef.
76  bool isScalarToVector(const SDNode *N);
77
78  /// allOperandsUndef - Return true if the node has at least one operand
79  /// and all operands of the specified node are ISD::UNDEF.
80  bool allOperandsUndef(const SDNode *N);
81}  // end llvm:ISD namespace
82
83//===----------------------------------------------------------------------===//
84/// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
85/// values as the result of a computation.  Many nodes return multiple values,
86/// from loads (which define a token and a return value) to ADDC (which returns
87/// a result and a carry value), to calls (which may return an arbitrary number
88/// of values).
89///
90/// As such, each use of a SelectionDAG computation must indicate the node that
91/// computes it as well as which return value to use from that node.  This pair
92/// of information is represented with the SDValue value type.
93///
94class SDValue {
95  SDNode *Node;       // The node defining the value we are using.
96  unsigned ResNo;     // Which return value of the node we are using.
97public:
98  SDValue() : Node(0), ResNo(0) {}
99  SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
100
101  /// get the index which selects a specific result in the SDNode
102  unsigned getResNo() const { return ResNo; }
103
104  /// get the SDNode which holds the desired result
105  SDNode *getNode() const { return Node; }
106
107  /// set the SDNode
108  void setNode(SDNode *N) { Node = N; }
109
110  inline SDNode *operator->() const { return Node; }
111
112  bool operator==(const SDValue &O) const {
113    return Node == O.Node && ResNo == O.ResNo;
114  }
115  bool operator!=(const SDValue &O) const {
116    return !operator==(O);
117  }
118  bool operator<(const SDValue &O) const {
119    return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
120  }
121
122  SDValue getValue(unsigned R) const {
123    return SDValue(Node, R);
124  }
125
126  // isOperandOf - Return true if this node is an operand of N.
127  bool isOperandOf(SDNode *N) const;
128
129  /// getValueType - Return the ValueType of the referenced return value.
130  ///
131  inline EVT getValueType() const;
132
133  /// Return the simple ValueType of the referenced return value.
134  MVT getSimpleValueType() const {
135    return getValueType().getSimpleVT();
136  }
137
138  /// getValueSizeInBits - Returns the size of the value in bits.
139  ///
140  unsigned getValueSizeInBits() const {
141    return getValueType().getSizeInBits();
142  }
143
144  // Forwarding methods - These forward to the corresponding methods in SDNode.
145  inline unsigned getOpcode() const;
146  inline unsigned getNumOperands() const;
147  inline const SDValue &getOperand(unsigned i) const;
148  inline uint64_t getConstantOperandVal(unsigned i) const;
149  inline bool isTargetMemoryOpcode() const;
150  inline bool isTargetOpcode() const;
151  inline bool isMachineOpcode() const;
152  inline unsigned getMachineOpcode() const;
153  inline const DebugLoc getDebugLoc() const;
154  inline void dump() const;
155  inline void dumpr() const;
156
157  /// reachesChainWithoutSideEffects - Return true if this operand (which must
158  /// be a chain) reaches the specified operand without crossing any
159  /// side-effecting instructions.  In practice, this looks through token
160  /// factors and non-volatile loads.  In order to remain efficient, this only
161  /// looks a couple of nodes in, it does not do an exhaustive search.
162  bool reachesChainWithoutSideEffects(SDValue Dest,
163                                      unsigned Depth = 2) const;
164
165  /// use_empty - Return true if there are no nodes using value ResNo
166  /// of Node.
167  ///
168  inline bool use_empty() const;
169
170  /// hasOneUse - Return true if there is exactly one node using value
171  /// ResNo of Node.
172  ///
173  inline bool hasOneUse() const;
174};
175
176
177template<> struct DenseMapInfo<SDValue> {
178  static inline SDValue getEmptyKey() {
179    return SDValue((SDNode*)-1, -1U);
180  }
181  static inline SDValue getTombstoneKey() {
182    return SDValue((SDNode*)-1, 0);
183  }
184  static unsigned getHashValue(const SDValue &Val) {
185    return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
186            (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
187  }
188  static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
189    return LHS == RHS;
190  }
191};
192template <> struct isPodLike<SDValue> { static const bool value = true; };
193
194
195/// simplify_type specializations - Allow casting operators to work directly on
196/// SDValues as if they were SDNode*'s.
197template<> struct simplify_type<SDValue> {
198  typedef SDNode* SimpleType;
199  static SimpleType getSimplifiedValue(SDValue &Val) {
200    return Val.getNode();
201  }
202};
203template<> struct simplify_type<const SDValue> {
204  typedef /*const*/ SDNode* SimpleType;
205  static SimpleType getSimplifiedValue(const SDValue &Val) {
206    return Val.getNode();
207  }
208};
209
210/// SDUse - Represents a use of a SDNode. This class holds an SDValue,
211/// which records the SDNode being used and the result number, a
212/// pointer to the SDNode using the value, and Next and Prev pointers,
213/// which link together all the uses of an SDNode.
214///
215class SDUse {
216  /// Val - The value being used.
217  SDValue Val;
218  /// User - The user of this value.
219  SDNode *User;
220  /// Prev, Next - Pointers to the uses list of the SDNode referred by
221  /// this operand.
222  SDUse **Prev, *Next;
223
224  SDUse(const SDUse &U) LLVM_DELETED_FUNCTION;
225  void operator=(const SDUse &U) LLVM_DELETED_FUNCTION;
226
227public:
228  SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
229
230  /// Normally SDUse will just implicitly convert to an SDValue that it holds.
231  operator const SDValue&() const { return Val; }
232
233  /// If implicit conversion to SDValue doesn't work, the get() method returns
234  /// the SDValue.
235  const SDValue &get() const { return Val; }
236
237  /// getUser - This returns the SDNode that contains this Use.
238  SDNode *getUser() { return User; }
239
240  /// getNext - Get the next SDUse in the use list.
241  SDUse *getNext() const { return Next; }
242
243  /// getNode - Convenience function for get().getNode().
244  SDNode *getNode() const { return Val.getNode(); }
245  /// getResNo - Convenience function for get().getResNo().
246  unsigned getResNo() const { return Val.getResNo(); }
247  /// getValueType - Convenience function for get().getValueType().
248  EVT getValueType() const { return Val.getValueType(); }
249
250  /// operator== - Convenience function for get().operator==
251  bool operator==(const SDValue &V) const {
252    return Val == V;
253  }
254
255  /// operator!= - Convenience function for get().operator!=
256  bool operator!=(const SDValue &V) const {
257    return Val != V;
258  }
259
260  /// operator< - Convenience function for get().operator<
261  bool operator<(const SDValue &V) const {
262    return Val < V;
263  }
264
265private:
266  friend class SelectionDAG;
267  friend class SDNode;
268
269  void setUser(SDNode *p) { User = p; }
270
271  /// set - Remove this use from its existing use list, assign it the
272  /// given value, and add it to the new value's node's use list.
273  inline void set(const SDValue &V);
274  /// setInitial - like set, but only supports initializing a newly-allocated
275  /// SDUse with a non-null value.
276  inline void setInitial(const SDValue &V);
277  /// setNode - like set, but only sets the Node portion of the value,
278  /// leaving the ResNo portion unmodified.
279  inline void setNode(SDNode *N);
280
281  void addToList(SDUse **List) {
282    Next = *List;
283    if (Next) Next->Prev = &Next;
284    Prev = List;
285    *List = this;
286  }
287
288  void removeFromList() {
289    *Prev = Next;
290    if (Next) Next->Prev = Prev;
291  }
292};
293
294/// simplify_type specializations - Allow casting operators to work directly on
295/// SDValues as if they were SDNode*'s.
296template<> struct simplify_type<SDUse> {
297  typedef SDNode* SimpleType;
298  static SimpleType getSimplifiedValue(SDUse &Val) {
299    return Val.getNode();
300  }
301};
302
303
304/// SDNode - Represents one node in the SelectionDAG.
305///
306class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
307private:
308  /// NodeType - The operation that this node performs.
309  ///
310  int16_t NodeType;
311
312  /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
313  /// then they will be delete[]'d when the node is destroyed.
314  uint16_t OperandsNeedDelete : 1;
315
316  /// HasDebugValue - This tracks whether this node has one or more dbg_value
317  /// nodes corresponding to it.
318  uint16_t HasDebugValue : 1;
319
320protected:
321  /// SubclassData - This member is defined by this class, but is not used for
322  /// anything.  Subclasses can use it to hold whatever state they find useful.
323  /// This field is initialized to zero by the ctor.
324  uint16_t SubclassData : 14;
325
326private:
327  /// NodeId - Unique id per SDNode in the DAG.
328  int NodeId;
329
330  /// OperandList - The values that are used by this operation.
331  ///
332  SDUse *OperandList;
333
334  /// ValueList - The types of the values this node defines.  SDNode's may
335  /// define multiple values simultaneously.
336  const EVT *ValueList;
337
338  /// UseList - List of uses for this SDNode.
339  SDUse *UseList;
340
341  /// NumOperands/NumValues - The number of entries in the Operand/Value list.
342  unsigned short NumOperands, NumValues;
343
344  /// debugLoc - source line information.
345  DebugLoc debugLoc;
346
347  // The ordering of the SDNodes. It roughly corresponds to the ordering of the
348  // original LLVM instructions.
349  // This is used for turning off scheduling, because we'll forgo
350  // the normal scheduling algorithms and output the instructions according to
351  // this ordering.
352  unsigned IROrder;
353
354  /// getValueTypeList - Return a pointer to the specified value type.
355  static const EVT *getValueTypeList(EVT VT);
356
357  friend class SelectionDAG;
358  friend struct ilist_traits<SDNode>;
359
360public:
361  //===--------------------------------------------------------------------===//
362  //  Accessors
363  //
364
365  /// getOpcode - Return the SelectionDAG opcode value for this node. For
366  /// pre-isel nodes (those for which isMachineOpcode returns false), these
367  /// are the opcode values in the ISD and <target>ISD namespaces. For
368  /// post-isel opcodes, see getMachineOpcode.
369  unsigned getOpcode()  const { return (unsigned short)NodeType; }
370
371  /// isTargetOpcode - Test if this node has a target-specific opcode (in the
372  /// \<target\>ISD namespace).
373  bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
374
375  /// isTargetMemoryOpcode - Test if this node has a target-specific
376  /// memory-referencing opcode (in the \<target\>ISD namespace and
377  /// greater than FIRST_TARGET_MEMORY_OPCODE).
378  bool isTargetMemoryOpcode() const {
379    return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
380  }
381
382  /// isMachineOpcode - Test if this node has a post-isel opcode, directly
383  /// corresponding to a MachineInstr opcode.
384  bool isMachineOpcode() const { return NodeType < 0; }
385
386  /// getMachineOpcode - This may only be called if isMachineOpcode returns
387  /// true. It returns the MachineInstr opcode value that the node's opcode
388  /// corresponds to.
389  unsigned getMachineOpcode() const {
390    assert(isMachineOpcode() && "Not a MachineInstr opcode!");
391    return ~NodeType;
392  }
393
394  /// getHasDebugValue - get this bit.
395  bool getHasDebugValue() const { return HasDebugValue; }
396
397  /// setHasDebugValue - set this bit.
398  void setHasDebugValue(bool b) { HasDebugValue = b; }
399
400  /// use_empty - Return true if there are no uses of this node.
401  ///
402  bool use_empty() const { return UseList == NULL; }
403
404  /// hasOneUse - Return true if there is exactly one use of this node.
405  ///
406  bool hasOneUse() const {
407    return !use_empty() && llvm::next(use_begin()) == use_end();
408  }
409
410  /// use_size - Return the number of uses of this node. This method takes
411  /// time proportional to the number of uses.
412  ///
413  size_t use_size() const { return std::distance(use_begin(), use_end()); }
414
415  /// getNodeId - Return the unique node id.
416  ///
417  int getNodeId() const { return NodeId; }
418
419  /// setNodeId - Set unique node id.
420  void setNodeId(int Id) { NodeId = Id; }
421
422  /// getIROrder - Return the node ordering.
423  ///
424  unsigned getIROrder() const { return IROrder; }
425
426  /// setIROrder - Set the node ordering.
427  ///
428  void setIROrder(unsigned Order) { IROrder = Order; }
429
430  /// getDebugLoc - Return the source location info.
431  const DebugLoc getDebugLoc() const { return debugLoc; }
432
433  /// setDebugLoc - Set source location info.  Try to avoid this, putting
434  /// it in the constructor is preferable.
435  void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
436
437  /// use_iterator - This class provides iterator support for SDUse
438  /// operands that use a specific SDNode.
439  class use_iterator
440    : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
441    SDUse *Op;
442    explicit use_iterator(SDUse *op) : Op(op) {
443    }
444    friend class SDNode;
445  public:
446    typedef std::iterator<std::forward_iterator_tag,
447                          SDUse, ptrdiff_t>::reference reference;
448    typedef std::iterator<std::forward_iterator_tag,
449                          SDUse, ptrdiff_t>::pointer pointer;
450
451    use_iterator(const use_iterator &I) : Op(I.Op) {}
452    use_iterator() : Op(0) {}
453
454    bool operator==(const use_iterator &x) const {
455      return Op == x.Op;
456    }
457    bool operator!=(const use_iterator &x) const {
458      return !operator==(x);
459    }
460
461    /// atEnd - return true if this iterator is at the end of uses list.
462    bool atEnd() const { return Op == 0; }
463
464    // Iterator traversal: forward iteration only.
465    use_iterator &operator++() {          // Preincrement
466      assert(Op && "Cannot increment end iterator!");
467      Op = Op->getNext();
468      return *this;
469    }
470
471    use_iterator operator++(int) {        // Postincrement
472      use_iterator tmp = *this; ++*this; return tmp;
473    }
474
475    /// Retrieve a pointer to the current user node.
476    SDNode *operator*() const {
477      assert(Op && "Cannot dereference end iterator!");
478      return Op->getUser();
479    }
480
481    SDNode *operator->() const { return operator*(); }
482
483    SDUse &getUse() const { return *Op; }
484
485    /// getOperandNo - Retrieve the operand # of this use in its user.
486    ///
487    unsigned getOperandNo() const {
488      assert(Op && "Cannot dereference end iterator!");
489      return (unsigned)(Op - Op->getUser()->OperandList);
490    }
491  };
492
493  /// use_begin/use_end - Provide iteration support to walk over all uses
494  /// of an SDNode.
495
496  use_iterator use_begin() const {
497    return use_iterator(UseList);
498  }
499
500  static use_iterator use_end() { return use_iterator(0); }
501
502
503  /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
504  /// indicated value.  This method ignores uses of other values defined by this
505  /// operation.
506  bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
507
508  /// hasAnyUseOfValue - Return true if there are any use of the indicated
509  /// value. This method ignores uses of other values defined by this operation.
510  bool hasAnyUseOfValue(unsigned Value) const;
511
512  /// isOnlyUserOf - Return true if this node is the only use of N.
513  ///
514  bool isOnlyUserOf(SDNode *N) const;
515
516  /// isOperandOf - Return true if this node is an operand of N.
517  ///
518  bool isOperandOf(SDNode *N) const;
519
520  /// isPredecessorOf - Return true if this node is a predecessor of N.
521  /// NOTE: Implemented on top of hasPredecessor and every bit as
522  /// expensive. Use carefully.
523  bool isPredecessorOf(const SDNode *N) const {
524    return N->hasPredecessor(this);
525  }
526
527  /// hasPredecessor - Return true if N is a predecessor of this node.
528  /// N is either an operand of this node, or can be reached by recursively
529  /// traversing up the operands.
530  /// NOTE: This is an expensive method. Use it carefully.
531  bool hasPredecessor(const SDNode *N) const;
532
533  /// hasPredecesorHelper - Return true if N is a predecessor of this node.
534  /// N is either an operand of this node, or can be reached by recursively
535  /// traversing up the operands.
536  /// In this helper the Visited and worklist sets are held externally to
537  /// cache predecessors over multiple invocations. If you want to test for
538  /// multiple predecessors this method is preferable to multiple calls to
539  /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
540  /// changes.
541  /// NOTE: This is still very expensive. Use carefully.
542  bool hasPredecessorHelper(const SDNode *N,
543                            SmallPtrSet<const SDNode *, 32> &Visited,
544                            SmallVectorImpl<const SDNode *> &Worklist) const;
545
546  /// getNumOperands - Return the number of values used by this operation.
547  ///
548  unsigned getNumOperands() const { return NumOperands; }
549
550  /// getConstantOperandVal - Helper method returns the integer value of a
551  /// ConstantSDNode operand.
552  uint64_t getConstantOperandVal(unsigned Num) const;
553
554  const SDValue &getOperand(unsigned Num) const {
555    assert(Num < NumOperands && "Invalid child # of SDNode!");
556    return OperandList[Num];
557  }
558
559  typedef SDUse* op_iterator;
560  op_iterator op_begin() const { return OperandList; }
561  op_iterator op_end() const { return OperandList+NumOperands; }
562
563  SDVTList getVTList() const {
564    SDVTList X = { ValueList, NumValues };
565    return X;
566  }
567
568  /// getGluedNode - If this node has a glue operand, return the node
569  /// to which the glue operand points. Otherwise return NULL.
570  SDNode *getGluedNode() const {
571    if (getNumOperands() != 0 &&
572      getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
573      return getOperand(getNumOperands()-1).getNode();
574    return 0;
575  }
576
577  // If this is a pseudo op, like copyfromreg, look to see if there is a
578  // real target node glued to it.  If so, return the target node.
579  const SDNode *getGluedMachineNode() const {
580    const SDNode *FoundNode = this;
581
582    // Climb up glue edges until a machine-opcode node is found, or the
583    // end of the chain is reached.
584    while (!FoundNode->isMachineOpcode()) {
585      const SDNode *N = FoundNode->getGluedNode();
586      if (!N) break;
587      FoundNode = N;
588    }
589
590    return FoundNode;
591  }
592
593  /// getGluedUser - If this node has a glue value with a user, return
594  /// the user (there is at most one). Otherwise return NULL.
595  SDNode *getGluedUser() const {
596    for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
597      if (UI.getUse().get().getValueType() == MVT::Glue)
598        return *UI;
599    return 0;
600  }
601
602  /// getNumValues - Return the number of values defined/returned by this
603  /// operator.
604  ///
605  unsigned getNumValues() const { return NumValues; }
606
607  /// getValueType - Return the type of a specified result.
608  ///
609  EVT getValueType(unsigned ResNo) const {
610    assert(ResNo < NumValues && "Illegal result number!");
611    return ValueList[ResNo];
612  }
613
614  /// Return the type of a specified result as a simple type.
615  ///
616  MVT getSimpleValueType(unsigned ResNo) const {
617    return getValueType(ResNo).getSimpleVT();
618  }
619
620  /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
621  ///
622  unsigned getValueSizeInBits(unsigned ResNo) const {
623    return getValueType(ResNo).getSizeInBits();
624  }
625
626  typedef const EVT* value_iterator;
627  value_iterator value_begin() const { return ValueList; }
628  value_iterator value_end() const { return ValueList+NumValues; }
629
630  /// getOperationName - Return the opcode of this operation for printing.
631  ///
632  std::string getOperationName(const SelectionDAG *G = 0) const;
633  static const char* getIndexedModeName(ISD::MemIndexedMode AM);
634  void print_types(raw_ostream &OS, const SelectionDAG *G) const;
635  void print_details(raw_ostream &OS, const SelectionDAG *G) const;
636  void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
637  void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
638
639  /// printrFull - Print a SelectionDAG node and all children down to
640  /// the leaves.  The given SelectionDAG allows target-specific nodes
641  /// to be printed in human-readable form.  Unlike printr, this will
642  /// print the whole DAG, including children that appear multiple
643  /// times.
644  ///
645  void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
646
647  /// printrWithDepth - Print a SelectionDAG node and children up to
648  /// depth "depth."  The given SelectionDAG allows target-specific
649  /// nodes to be printed in human-readable form.  Unlike printr, this
650  /// will print children that appear multiple times wherever they are
651  /// used.
652  ///
653  void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
654                       unsigned depth = 100) const;
655
656
657  /// dump - Dump this node, for debugging.
658  void dump() const;
659
660  /// dumpr - Dump (recursively) this node and its use-def subgraph.
661  void dumpr() const;
662
663  /// dump - Dump this node, for debugging.
664  /// The given SelectionDAG allows target-specific nodes to be printed
665  /// in human-readable form.
666  void dump(const SelectionDAG *G) const;
667
668  /// dumpr - Dump (recursively) this node and its use-def subgraph.
669  /// The given SelectionDAG allows target-specific nodes to be printed
670  /// in human-readable form.
671  void dumpr(const SelectionDAG *G) const;
672
673  /// dumprFull - printrFull to dbgs().  The given SelectionDAG allows
674  /// target-specific nodes to be printed in human-readable form.
675  /// Unlike dumpr, this will print the whole DAG, including children
676  /// that appear multiple times.
677  ///
678  void dumprFull(const SelectionDAG *G = 0) const;
679
680  /// dumprWithDepth - printrWithDepth to dbgs().  The given
681  /// SelectionDAG allows target-specific nodes to be printed in
682  /// human-readable form.  Unlike dumpr, this will print children
683  /// that appear multiple times wherever they are used.
684  ///
685  void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
686
687  /// Profile - Gather unique data for the node.
688  ///
689  void Profile(FoldingSetNodeID &ID) const;
690
691  /// addUse - This method should only be used by the SDUse class.
692  ///
693  void addUse(SDUse &U) { U.addToList(&UseList); }
694
695protected:
696  static SDVTList getSDVTList(EVT VT) {
697    SDVTList Ret = { getValueTypeList(VT), 1 };
698    return Ret;
699  }
700
701  SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs,
702         const SDValue *Ops, unsigned NumOps)
703    : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
704      SubclassData(0), NodeId(-1),
705      OperandList(NumOps ? new SDUse[NumOps] : 0),
706      ValueList(VTs.VTs), UseList(NULL),
707      NumOperands(NumOps), NumValues(VTs.NumVTs),
708      debugLoc(dl), IROrder(Order) {
709    for (unsigned i = 0; i != NumOps; ++i) {
710      OperandList[i].setUser(this);
711      OperandList[i].setInitial(Ops[i]);
712    }
713    checkForCycles(this);
714  }
715
716  /// This constructor adds no operands itself; operands can be
717  /// set later with InitOperands.
718  SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs)
719    : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
720      SubclassData(0), NodeId(-1), OperandList(0),
721      ValueList(VTs.VTs), UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
722      debugLoc(dl), IROrder(Order) {}
723
724  /// InitOperands - Initialize the operands list of this with 1 operand.
725  void InitOperands(SDUse *Ops, const SDValue &Op0) {
726    Ops[0].setUser(this);
727    Ops[0].setInitial(Op0);
728    NumOperands = 1;
729    OperandList = Ops;
730    checkForCycles(this);
731  }
732
733  /// InitOperands - Initialize the operands list of this with 2 operands.
734  void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
735    Ops[0].setUser(this);
736    Ops[0].setInitial(Op0);
737    Ops[1].setUser(this);
738    Ops[1].setInitial(Op1);
739    NumOperands = 2;
740    OperandList = Ops;
741    checkForCycles(this);
742  }
743
744  /// InitOperands - Initialize the operands list of this with 3 operands.
745  void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
746                    const SDValue &Op2) {
747    Ops[0].setUser(this);
748    Ops[0].setInitial(Op0);
749    Ops[1].setUser(this);
750    Ops[1].setInitial(Op1);
751    Ops[2].setUser(this);
752    Ops[2].setInitial(Op2);
753    NumOperands = 3;
754    OperandList = Ops;
755    checkForCycles(this);
756  }
757
758  /// InitOperands - Initialize the operands list of this with 4 operands.
759  void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
760                    const SDValue &Op2, const SDValue &Op3) {
761    Ops[0].setUser(this);
762    Ops[0].setInitial(Op0);
763    Ops[1].setUser(this);
764    Ops[1].setInitial(Op1);
765    Ops[2].setUser(this);
766    Ops[2].setInitial(Op2);
767    Ops[3].setUser(this);
768    Ops[3].setInitial(Op3);
769    NumOperands = 4;
770    OperandList = Ops;
771    checkForCycles(this);
772  }
773
774  /// InitOperands - Initialize the operands list of this with N operands.
775  void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
776    for (unsigned i = 0; i != N; ++i) {
777      Ops[i].setUser(this);
778      Ops[i].setInitial(Vals[i]);
779    }
780    NumOperands = N;
781    OperandList = Ops;
782    checkForCycles(this);
783  }
784
785  /// DropOperands - Release the operands and set this node to have
786  /// zero operands.
787  void DropOperands();
788};
789
790/// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
791/// into SDNode creation functions.
792/// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
793/// from the original Instruction, and IROrder is the ordinal position of
794/// the instruction.
795/// When an SDNode is created after the DAG is being built, both DebugLoc and
796/// the IROrder are propagated from the original SDNode.
797/// So SDLoc class provides two constructors besides the default one, one to
798/// be used by the DAGBuilder, the other to be used by others.
799class SDLoc {
800private:
801  // Ptr could be used for either Instruction* or SDNode*. It is used for
802  // Instruction* if IROrder is not -1.
803  const void *Ptr;
804  int IROrder;
805
806public:
807  SDLoc() : Ptr(NULL), IROrder(0) {}
808  SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
809    assert(N && "null SDNode");
810  }
811  SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
812    assert(Ptr && "null SDNode");
813  }
814  SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
815    assert(Order >= 0 && "bad IROrder");
816  }
817  unsigned getIROrder() {
818    if (IROrder >= 0 || Ptr == NULL) {
819      return (unsigned)IROrder;
820    }
821    const SDNode *N = (const SDNode*)(Ptr);
822    return N->getIROrder();
823  }
824  DebugLoc getDebugLoc() {
825    if (Ptr == NULL) {
826      return DebugLoc();
827    }
828    if (IROrder >= 0) {
829      const Instruction *I = (const Instruction*)(Ptr);
830      return I->getDebugLoc();
831    }
832    const SDNode *N = (const SDNode*)(Ptr);
833    return N->getDebugLoc();
834  }
835};
836
837
838// Define inline functions from the SDValue class.
839
840inline unsigned SDValue::getOpcode() const {
841  return Node->getOpcode();
842}
843inline EVT SDValue::getValueType() const {
844  return Node->getValueType(ResNo);
845}
846inline unsigned SDValue::getNumOperands() const {
847  return Node->getNumOperands();
848}
849inline const SDValue &SDValue::getOperand(unsigned i) const {
850  return Node->getOperand(i);
851}
852inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
853  return Node->getConstantOperandVal(i);
854}
855inline bool SDValue::isTargetOpcode() const {
856  return Node->isTargetOpcode();
857}
858inline bool SDValue::isTargetMemoryOpcode() const {
859  return Node->isTargetMemoryOpcode();
860}
861inline bool SDValue::isMachineOpcode() const {
862  return Node->isMachineOpcode();
863}
864inline unsigned SDValue::getMachineOpcode() const {
865  return Node->getMachineOpcode();
866}
867inline bool SDValue::use_empty() const {
868  return !Node->hasAnyUseOfValue(ResNo);
869}
870inline bool SDValue::hasOneUse() const {
871  return Node->hasNUsesOfValue(1, ResNo);
872}
873inline const DebugLoc SDValue::getDebugLoc() const {
874  return Node->getDebugLoc();
875}
876inline void SDValue::dump() const {
877  return Node->dump();
878}
879inline void SDValue::dumpr() const {
880  return Node->dumpr();
881}
882// Define inline functions from the SDUse class.
883
884inline void SDUse::set(const SDValue &V) {
885  if (Val.getNode()) removeFromList();
886  Val = V;
887  if (V.getNode()) V.getNode()->addUse(*this);
888}
889
890inline void SDUse::setInitial(const SDValue &V) {
891  Val = V;
892  V.getNode()->addUse(*this);
893}
894
895inline void SDUse::setNode(SDNode *N) {
896  if (Val.getNode()) removeFromList();
897  Val.setNode(N);
898  if (N) N->addUse(*this);
899}
900
901/// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
902/// to allow co-allocation of node operands with the node itself.
903class UnarySDNode : public SDNode {
904  SDUse Op;
905public:
906  UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
907              SDValue X)
908    : SDNode(Opc, Order, dl, VTs) {
909    InitOperands(&Op, X);
910  }
911};
912
913/// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
914/// to allow co-allocation of node operands with the node itself.
915class BinarySDNode : public SDNode {
916  SDUse Ops[2];
917public:
918  BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
919               SDValue X, SDValue Y)
920    : SDNode(Opc, Order, dl, VTs) {
921    InitOperands(Ops, X, Y);
922  }
923};
924
925/// TernarySDNode - This class is used for three-operand SDNodes. This is solely
926/// to allow co-allocation of node operands with the node itself.
927class TernarySDNode : public SDNode {
928  SDUse Ops[3];
929public:
930  TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
931                SDValue X, SDValue Y, SDValue Z)
932    : SDNode(Opc, Order, dl, VTs) {
933    InitOperands(Ops, X, Y, Z);
934  }
935};
936
937
938/// HandleSDNode - This class is used to form a handle around another node that
939/// is persistent and is updated across invocations of replaceAllUsesWith on its
940/// operand.  This node should be directly created by end-users and not added to
941/// the AllNodes list.
942class HandleSDNode : public SDNode {
943  SDUse Op;
944public:
945  explicit HandleSDNode(SDValue X)
946    : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
947    InitOperands(&Op, X);
948  }
949  ~HandleSDNode();
950  const SDValue &getValue() const { return Op; }
951};
952
953class AddrSpaceCastSDNode : public UnarySDNode {
954private:
955  unsigned SrcAddrSpace;
956  unsigned DestAddrSpace;
957
958public:
959  AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
960                      unsigned SrcAS, unsigned DestAS);
961
962  unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
963  unsigned getDestAddressSpace() const { return DestAddrSpace; }
964
965  static bool classof(const SDNode *N) {
966    return N->getOpcode() == ISD::ADDRSPACECAST;
967  }
968};
969
970/// Abstact virtual class for operations for memory operations
971class MemSDNode : public SDNode {
972private:
973  // MemoryVT - VT of in-memory value.
974  EVT MemoryVT;
975
976protected:
977  /// MMO - Memory reference information.
978  MachineMemOperand *MMO;
979
980public:
981  MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
982            EVT MemoryVT, MachineMemOperand *MMO);
983
984  MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
985            const SDValue *Ops,
986            unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
987
988  bool readMem() const { return MMO->isLoad(); }
989  bool writeMem() const { return MMO->isStore(); }
990
991  /// Returns alignment and volatility of the memory access
992  unsigned getOriginalAlignment() const {
993    return MMO->getBaseAlignment();
994  }
995  unsigned getAlignment() const {
996    return MMO->getAlignment();
997  }
998
999  /// getRawSubclassData - Return the SubclassData value, which contains an
1000  /// encoding of the volatile flag, as well as bits used by subclasses. This
1001  /// function should only be used to compute a FoldingSetNodeID value.
1002  unsigned getRawSubclassData() const {
1003    return SubclassData;
1004  }
1005
1006  // We access subclass data here so that we can check consistency
1007  // with MachineMemOperand information.
1008  bool isVolatile() const { return (SubclassData >> 5) & 1; }
1009  bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
1010  bool isInvariant() const { return (SubclassData >> 7) & 1; }
1011
1012  AtomicOrdering getOrdering() const {
1013    return AtomicOrdering((SubclassData >> 8) & 15);
1014  }
1015  SynchronizationScope getSynchScope() const {
1016    return SynchronizationScope((SubclassData >> 12) & 1);
1017  }
1018
1019  /// Returns the SrcValue and offset that describes the location of the access
1020  const Value *getSrcValue() const { return MMO->getValue(); }
1021  int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1022
1023  /// Returns the TBAAInfo that describes the dereference.
1024  const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
1025
1026  /// Returns the Ranges that describes the dereference.
1027  const MDNode *getRanges() const { return MMO->getRanges(); }
1028
1029  /// getMemoryVT - Return the type of the in-memory value.
1030  EVT getMemoryVT() const { return MemoryVT; }
1031
1032  /// getMemOperand - Return a MachineMemOperand object describing the memory
1033  /// reference performed by operation.
1034  MachineMemOperand *getMemOperand() const { return MMO; }
1035
1036  const MachinePointerInfo &getPointerInfo() const {
1037    return MMO->getPointerInfo();
1038  }
1039
1040  /// getAddressSpace - Return the address space for the associated pointer
1041  unsigned getAddressSpace() const {
1042    return getPointerInfo().getAddrSpace();
1043  }
1044
1045  /// refineAlignment - Update this MemSDNode's MachineMemOperand information
1046  /// to reflect the alignment of NewMMO, if it has a greater alignment.
1047  /// This must only be used when the new alignment applies to all users of
1048  /// this MachineMemOperand.
1049  void refineAlignment(const MachineMemOperand *NewMMO) {
1050    MMO->refineAlignment(NewMMO);
1051  }
1052
1053  const SDValue &getChain() const { return getOperand(0); }
1054  const SDValue &getBasePtr() const {
1055    return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1056  }
1057
1058  // Methods to support isa and dyn_cast
1059  static bool classof(const SDNode *N) {
1060    // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1061    // with either an intrinsic or a target opcode.
1062    return N->getOpcode() == ISD::LOAD                ||
1063           N->getOpcode() == ISD::STORE               ||
1064           N->getOpcode() == ISD::PREFETCH            ||
1065           N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1066           N->getOpcode() == ISD::ATOMIC_SWAP         ||
1067           N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1068           N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1069           N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1070           N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1071           N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1072           N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1073           N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1074           N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1075           N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1076           N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1077           N->getOpcode() == ISD::ATOMIC_LOAD         ||
1078           N->getOpcode() == ISD::ATOMIC_STORE        ||
1079           N->isTargetMemoryOpcode();
1080  }
1081};
1082
1083/// AtomicSDNode - A SDNode reprenting atomic operations.
1084///
1085class AtomicSDNode : public MemSDNode {
1086  SDUse Ops[4];
1087
1088  void InitAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope) {
1089    // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1090    assert((Ordering & 15) == Ordering &&
1091           "Ordering may not require more than 4 bits!");
1092    assert((SynchScope & 1) == SynchScope &&
1093           "SynchScope may not require more than 1 bit!");
1094    SubclassData |= Ordering << 8;
1095    SubclassData |= SynchScope << 12;
1096    assert(getOrdering() == Ordering && "Ordering encoding error!");
1097    assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1098  }
1099
1100public:
1101  // Opc:   opcode for atomic
1102  // VTL:    value type list
1103  // Chain:  memory chain for operaand
1104  // Ptr:    address to update as a SDValue
1105  // Cmp:    compare value
1106  // Swp:    swap value
1107  // SrcVal: address to update as a Value (used for MemOperand)
1108  // Align:  alignment of memory
1109  AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1110               EVT MemVT,
1111               SDValue Chain, SDValue Ptr,
1112               SDValue Cmp, SDValue Swp, MachineMemOperand *MMO,
1113               AtomicOrdering Ordering, SynchronizationScope SynchScope)
1114    : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1115    InitAtomic(Ordering, SynchScope);
1116    InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1117  }
1118  AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1119               EVT MemVT,
1120               SDValue Chain, SDValue Ptr,
1121               SDValue Val, MachineMemOperand *MMO,
1122               AtomicOrdering Ordering, SynchronizationScope SynchScope)
1123    : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1124    InitAtomic(Ordering, SynchScope);
1125    InitOperands(Ops, Chain, Ptr, Val);
1126  }
1127  AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1128               EVT MemVT,
1129               SDValue Chain, SDValue Ptr,
1130               MachineMemOperand *MMO,
1131               AtomicOrdering Ordering, SynchronizationScope SynchScope)
1132    : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1133    InitAtomic(Ordering, SynchScope);
1134    InitOperands(Ops, Chain, Ptr);
1135  }
1136  AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1137               SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
1138               MachineMemOperand *MMO,
1139               AtomicOrdering Ordering, SynchronizationScope SynchScope)
1140    : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1141    InitAtomic(Ordering, SynchScope);
1142    assert((DynOps || NumOps <= array_lengthof(Ops)) &&
1143           "Too many ops for internal storage!");
1144    InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
1145  }
1146
1147  const SDValue &getBasePtr() const { return getOperand(1); }
1148  const SDValue &getVal() const { return getOperand(2); }
1149
1150  bool isCompareAndSwap() const {
1151    unsigned Op = getOpcode();
1152    return Op == ISD::ATOMIC_CMP_SWAP;
1153  }
1154
1155  // Methods to support isa and dyn_cast
1156  static bool classof(const SDNode *N) {
1157    return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1158           N->getOpcode() == ISD::ATOMIC_SWAP         ||
1159           N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1160           N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1161           N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1162           N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1163           N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1164           N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1165           N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1166           N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1167           N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1168           N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1169           N->getOpcode() == ISD::ATOMIC_LOAD         ||
1170           N->getOpcode() == ISD::ATOMIC_STORE;
1171  }
1172};
1173
1174/// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1175/// memory and need an associated MachineMemOperand. Its opcode may be
1176/// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1177/// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1178class MemIntrinsicSDNode : public MemSDNode {
1179public:
1180  MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1181                     const SDValue *Ops, unsigned NumOps,
1182                     EVT MemoryVT, MachineMemOperand *MMO)
1183    : MemSDNode(Opc, Order, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1184  }
1185
1186  // Methods to support isa and dyn_cast
1187  static bool classof(const SDNode *N) {
1188    // We lower some target intrinsics to their target opcode
1189    // early a node with a target opcode can be of this class
1190    return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1191           N->getOpcode() == ISD::INTRINSIC_VOID ||
1192           N->getOpcode() == ISD::PREFETCH ||
1193           N->isTargetMemoryOpcode();
1194  }
1195};
1196
1197/// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1198/// support for the llvm IR shufflevector instruction.  It combines elements
1199/// from two input vectors into a new input vector, with the selection and
1200/// ordering of elements determined by an array of integers, referred to as
1201/// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
1202/// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1203/// An index of -1 is treated as undef, such that the code generator may put
1204/// any value in the corresponding element of the result.
1205class ShuffleVectorSDNode : public SDNode {
1206  SDUse Ops[2];
1207
1208  // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1209  // is freed when the SelectionDAG object is destroyed.
1210  const int *Mask;
1211protected:
1212  friend class SelectionDAG;
1213  ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
1214                      SDValue N2, const int *M)
1215    : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
1216    InitOperands(Ops, N1, N2);
1217  }
1218public:
1219
1220  ArrayRef<int> getMask() const {
1221    EVT VT = getValueType(0);
1222    return makeArrayRef(Mask, VT.getVectorNumElements());
1223  }
1224  int getMaskElt(unsigned Idx) const {
1225    assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1226    return Mask[Idx];
1227  }
1228
1229  bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1230  int  getSplatIndex() const {
1231    assert(isSplat() && "Cannot get splat index for non-splat!");
1232    EVT VT = getValueType(0);
1233    for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1234      if (Mask[i] >= 0)
1235        return Mask[i];
1236    }
1237    llvm_unreachable("Splat with all undef indices?");
1238  }
1239  static bool isSplatMask(const int *Mask, EVT VT);
1240
1241  static bool classof(const SDNode *N) {
1242    return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1243  }
1244};
1245
1246class ConstantSDNode : public SDNode {
1247  const ConstantInt *Value;
1248  friend class SelectionDAG;
1249  ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1250    : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1251             0, DebugLoc(), getSDVTList(VT)), Value(val) {
1252  }
1253public:
1254
1255  const ConstantInt *getConstantIntValue() const { return Value; }
1256  const APInt &getAPIntValue() const { return Value->getValue(); }
1257  uint64_t getZExtValue() const { return Value->getZExtValue(); }
1258  int64_t getSExtValue() const { return Value->getSExtValue(); }
1259
1260  bool isOne() const { return Value->isOne(); }
1261  bool isNullValue() const { return Value->isNullValue(); }
1262  bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1263
1264  static bool classof(const SDNode *N) {
1265    return N->getOpcode() == ISD::Constant ||
1266           N->getOpcode() == ISD::TargetConstant;
1267  }
1268};
1269
1270class ConstantFPSDNode : public SDNode {
1271  const ConstantFP *Value;
1272  friend class SelectionDAG;
1273  ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1274    : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1275             0, DebugLoc(), getSDVTList(VT)), Value(val) {
1276  }
1277public:
1278
1279  const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1280  const ConstantFP *getConstantFPValue() const { return Value; }
1281
1282  /// isZero - Return true if the value is positive or negative zero.
1283  bool isZero() const { return Value->isZero(); }
1284
1285  /// isNaN - Return true if the value is a NaN.
1286  bool isNaN() const { return Value->isNaN(); }
1287
1288  /// isExactlyValue - We don't rely on operator== working on double values, as
1289  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1290  /// As such, this method can be used to do an exact bit-for-bit comparison of
1291  /// two floating point values.
1292
1293  /// We leave the version with the double argument here because it's just so
1294  /// convenient to write "2.0" and the like.  Without this function we'd
1295  /// have to duplicate its logic everywhere it's called.
1296  bool isExactlyValue(double V) const {
1297    bool ignored;
1298    APFloat Tmp(V);
1299    Tmp.convert(Value->getValueAPF().getSemantics(),
1300                APFloat::rmNearestTiesToEven, &ignored);
1301    return isExactlyValue(Tmp);
1302  }
1303  bool isExactlyValue(const APFloat& V) const;
1304
1305  static bool isValueValidForType(EVT VT, const APFloat& Val);
1306
1307  static bool classof(const SDNode *N) {
1308    return N->getOpcode() == ISD::ConstantFP ||
1309           N->getOpcode() == ISD::TargetConstantFP;
1310  }
1311};
1312
1313class GlobalAddressSDNode : public SDNode {
1314  const GlobalValue *TheGlobal;
1315  int64_t Offset;
1316  unsigned char TargetFlags;
1317  friend class SelectionDAG;
1318  GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
1319                      const GlobalValue *GA, EVT VT, int64_t o,
1320                      unsigned char TargetFlags);
1321public:
1322
1323  const GlobalValue *getGlobal() const { return TheGlobal; }
1324  int64_t getOffset() const { return Offset; }
1325  unsigned char getTargetFlags() const { return TargetFlags; }
1326  // Return the address space this GlobalAddress belongs to.
1327  unsigned getAddressSpace() const;
1328
1329  static bool classof(const SDNode *N) {
1330    return N->getOpcode() == ISD::GlobalAddress ||
1331           N->getOpcode() == ISD::TargetGlobalAddress ||
1332           N->getOpcode() == ISD::GlobalTLSAddress ||
1333           N->getOpcode() == ISD::TargetGlobalTLSAddress;
1334  }
1335};
1336
1337class FrameIndexSDNode : public SDNode {
1338  int FI;
1339  friend class SelectionDAG;
1340  FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1341    : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1342      0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1343  }
1344public:
1345
1346  int getIndex() const { return FI; }
1347
1348  static bool classof(const SDNode *N) {
1349    return N->getOpcode() == ISD::FrameIndex ||
1350           N->getOpcode() == ISD::TargetFrameIndex;
1351  }
1352};
1353
1354class JumpTableSDNode : public SDNode {
1355  int JTI;
1356  unsigned char TargetFlags;
1357  friend class SelectionDAG;
1358  JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1359    : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1360      0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1361  }
1362public:
1363
1364  int getIndex() const { return JTI; }
1365  unsigned char getTargetFlags() const { return TargetFlags; }
1366
1367  static bool classof(const SDNode *N) {
1368    return N->getOpcode() == ISD::JumpTable ||
1369           N->getOpcode() == ISD::TargetJumpTable;
1370  }
1371};
1372
1373class ConstantPoolSDNode : public SDNode {
1374  union {
1375    const Constant *ConstVal;
1376    MachineConstantPoolValue *MachineCPVal;
1377  } Val;
1378  int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1379  unsigned Alignment;  // Minimum alignment requirement of CP (not log2 value).
1380  unsigned char TargetFlags;
1381  friend class SelectionDAG;
1382  ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1383                     unsigned Align, unsigned char TF)
1384    : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1385             DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1386             TargetFlags(TF) {
1387    assert(Offset >= 0 && "Offset is too large");
1388    Val.ConstVal = c;
1389  }
1390  ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1391                     EVT VT, int o, unsigned Align, unsigned char TF)
1392    : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1393             DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1394             TargetFlags(TF) {
1395    assert(Offset >= 0 && "Offset is too large");
1396    Val.MachineCPVal = v;
1397    Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1398  }
1399public:
1400
1401  bool isMachineConstantPoolEntry() const {
1402    return Offset < 0;
1403  }
1404
1405  const Constant *getConstVal() const {
1406    assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1407    return Val.ConstVal;
1408  }
1409
1410  MachineConstantPoolValue *getMachineCPVal() const {
1411    assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1412    return Val.MachineCPVal;
1413  }
1414
1415  int getOffset() const {
1416    return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1417  }
1418
1419  // Return the alignment of this constant pool object, which is either 0 (for
1420  // default alignment) or the desired value.
1421  unsigned getAlignment() const { return Alignment; }
1422  unsigned char getTargetFlags() const { return TargetFlags; }
1423
1424  Type *getType() const;
1425
1426  static bool classof(const SDNode *N) {
1427    return N->getOpcode() == ISD::ConstantPool ||
1428           N->getOpcode() == ISD::TargetConstantPool;
1429  }
1430};
1431
1432/// Completely target-dependent object reference.
1433class TargetIndexSDNode : public SDNode {
1434  unsigned char TargetFlags;
1435  int Index;
1436  int64_t Offset;
1437  friend class SelectionDAG;
1438public:
1439
1440  TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1441    : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1442      TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1443public:
1444
1445  unsigned char getTargetFlags() const { return TargetFlags; }
1446  int getIndex() const { return Index; }
1447  int64_t getOffset() const { return Offset; }
1448
1449  static bool classof(const SDNode *N) {
1450    return N->getOpcode() == ISD::TargetIndex;
1451  }
1452};
1453
1454class BasicBlockSDNode : public SDNode {
1455  MachineBasicBlock *MBB;
1456  friend class SelectionDAG;
1457  /// Debug info is meaningful and potentially useful here, but we create
1458  /// blocks out of order when they're jumped to, which makes it a bit
1459  /// harder.  Let's see if we need it first.
1460  explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1461    : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1462  {}
1463public:
1464
1465  MachineBasicBlock *getBasicBlock() const { return MBB; }
1466
1467  static bool classof(const SDNode *N) {
1468    return N->getOpcode() == ISD::BasicBlock;
1469  }
1470};
1471
1472/// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1473/// BUILD_VECTORs.
1474class BuildVectorSDNode : public SDNode {
1475  // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1476  explicit BuildVectorSDNode() LLVM_DELETED_FUNCTION;
1477public:
1478  /// isConstantSplat - Check if this is a constant splat, and if so, find the
1479  /// smallest element size that splats the vector.  If MinSplatBits is
1480  /// nonzero, the element size must be at least that large.  Note that the
1481  /// splat element may be the entire vector (i.e., a one element vector).
1482  /// Returns the splat element value in SplatValue.  Any undefined bits in
1483  /// that value are zero, and the corresponding bits in the SplatUndef mask
1484  /// are set.  The SplatBitSize value is set to the splat element size in
1485  /// bits.  HasAnyUndefs is set to true if any bits in the vector are
1486  /// undefined.  isBigEndian describes the endianness of the target.
1487  bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1488                       unsigned &SplatBitSize, bool &HasAnyUndefs,
1489                       unsigned MinSplatBits = 0, bool isBigEndian = false);
1490
1491  static inline bool classof(const SDNode *N) {
1492    return N->getOpcode() == ISD::BUILD_VECTOR;
1493  }
1494};
1495
1496/// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1497/// used when the SelectionDAG needs to make a simple reference to something
1498/// in the LLVM IR representation.
1499///
1500class SrcValueSDNode : public SDNode {
1501  const Value *V;
1502  friend class SelectionDAG;
1503  /// Create a SrcValue for a general value.
1504  explicit SrcValueSDNode(const Value *v)
1505    : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1506
1507public:
1508  /// getValue - return the contained Value.
1509  const Value *getValue() const { return V; }
1510
1511  static bool classof(const SDNode *N) {
1512    return N->getOpcode() == ISD::SRCVALUE;
1513  }
1514};
1515
1516class MDNodeSDNode : public SDNode {
1517  const MDNode *MD;
1518  friend class SelectionDAG;
1519  explicit MDNodeSDNode(const MDNode *md)
1520  : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
1521  {}
1522public:
1523
1524  const MDNode *getMD() const { return MD; }
1525
1526  static bool classof(const SDNode *N) {
1527    return N->getOpcode() == ISD::MDNODE_SDNODE;
1528  }
1529};
1530
1531class RegisterSDNode : public SDNode {
1532  unsigned Reg;
1533  friend class SelectionDAG;
1534  RegisterSDNode(unsigned reg, EVT VT)
1535    : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1536  }
1537public:
1538
1539  unsigned getReg() const { return Reg; }
1540
1541  static bool classof(const SDNode *N) {
1542    return N->getOpcode() == ISD::Register;
1543  }
1544};
1545
1546class RegisterMaskSDNode : public SDNode {
1547  // The memory for RegMask is not owned by the node.
1548  const uint32_t *RegMask;
1549  friend class SelectionDAG;
1550  RegisterMaskSDNode(const uint32_t *mask)
1551    : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1552      RegMask(mask) {}
1553public:
1554
1555  const uint32_t *getRegMask() const { return RegMask; }
1556
1557  static bool classof(const SDNode *N) {
1558    return N->getOpcode() == ISD::RegisterMask;
1559  }
1560};
1561
1562class BlockAddressSDNode : public SDNode {
1563  const BlockAddress *BA;
1564  int64_t Offset;
1565  unsigned char TargetFlags;
1566  friend class SelectionDAG;
1567  BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1568                     int64_t o, unsigned char Flags)
1569    : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1570             BA(ba), Offset(o), TargetFlags(Flags) {
1571  }
1572public:
1573  const BlockAddress *getBlockAddress() const { return BA; }
1574  int64_t getOffset() const { return Offset; }
1575  unsigned char getTargetFlags() const { return TargetFlags; }
1576
1577  static bool classof(const SDNode *N) {
1578    return N->getOpcode() == ISD::BlockAddress ||
1579           N->getOpcode() == ISD::TargetBlockAddress;
1580  }
1581};
1582
1583class EHLabelSDNode : public SDNode {
1584  SDUse Chain;
1585  MCSymbol *Label;
1586  friend class SelectionDAG;
1587  EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
1588    : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
1589    InitOperands(&Chain, ch);
1590  }
1591public:
1592  MCSymbol *getLabel() const { return Label; }
1593
1594  static bool classof(const SDNode *N) {
1595    return N->getOpcode() == ISD::EH_LABEL;
1596  }
1597};
1598
1599class ExternalSymbolSDNode : public SDNode {
1600  const char *Symbol;
1601  unsigned char TargetFlags;
1602
1603  friend class SelectionDAG;
1604  ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1605    : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1606             0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1607  }
1608public:
1609
1610  const char *getSymbol() const { return Symbol; }
1611  unsigned char getTargetFlags() const { return TargetFlags; }
1612
1613  static bool classof(const SDNode *N) {
1614    return N->getOpcode() == ISD::ExternalSymbol ||
1615           N->getOpcode() == ISD::TargetExternalSymbol;
1616  }
1617};
1618
1619class CondCodeSDNode : public SDNode {
1620  ISD::CondCode Condition;
1621  friend class SelectionDAG;
1622  explicit CondCodeSDNode(ISD::CondCode Cond)
1623    : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1624      Condition(Cond) {
1625  }
1626public:
1627
1628  ISD::CondCode get() const { return Condition; }
1629
1630  static bool classof(const SDNode *N) {
1631    return N->getOpcode() == ISD::CONDCODE;
1632  }
1633};
1634
1635/// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1636/// future and most targets don't support it.
1637class CvtRndSatSDNode : public SDNode {
1638  ISD::CvtCode CvtCode;
1639  friend class SelectionDAG;
1640  explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
1641                           const SDValue *Ops, unsigned NumOps,
1642                           ISD::CvtCode Code)
1643    : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops, NumOps),
1644      CvtCode(Code) {
1645    assert(NumOps == 5 && "wrong number of operations");
1646  }
1647public:
1648  ISD::CvtCode getCvtCode() const { return CvtCode; }
1649
1650  static bool classof(const SDNode *N) {
1651    return N->getOpcode() == ISD::CONVERT_RNDSAT;
1652  }
1653};
1654
1655/// VTSDNode - This class is used to represent EVT's, which are used
1656/// to parameterize some operations.
1657class VTSDNode : public SDNode {
1658  EVT ValueType;
1659  friend class SelectionDAG;
1660  explicit VTSDNode(EVT VT)
1661    : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1662      ValueType(VT) {
1663  }
1664public:
1665
1666  EVT getVT() const { return ValueType; }
1667
1668  static bool classof(const SDNode *N) {
1669    return N->getOpcode() == ISD::VALUETYPE;
1670  }
1671};
1672
1673/// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1674///
1675class LSBaseSDNode : public MemSDNode {
1676  //! Operand array for load and store
1677  /*!
1678    \note Moving this array to the base class captures more
1679    common functionality shared between LoadSDNode and
1680    StoreSDNode
1681   */
1682  SDUse Ops[4];
1683public:
1684  LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
1685               SDValue *Operands, unsigned numOperands,
1686               SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1687               MachineMemOperand *MMO)
1688    : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1689    SubclassData |= AM << 2;
1690    assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1691    InitOperands(Ops, Operands, numOperands);
1692    assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1693           "Only indexed loads and stores have a non-undef offset operand");
1694  }
1695
1696  const SDValue &getOffset() const {
1697    return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1698  }
1699
1700  /// getAddressingMode - Return the addressing mode for this load or store:
1701  /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1702  ISD::MemIndexedMode getAddressingMode() const {
1703    return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1704  }
1705
1706  /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1707  bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1708
1709  /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1710  bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1711
1712  static bool classof(const SDNode *N) {
1713    return N->getOpcode() == ISD::LOAD ||
1714           N->getOpcode() == ISD::STORE;
1715  }
1716};
1717
1718/// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1719///
1720class LoadSDNode : public LSBaseSDNode {
1721  friend class SelectionDAG;
1722  LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
1723             ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1724             MachineMemOperand *MMO)
1725    : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
1726    SubclassData |= (unsigned short)ETy;
1727    assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1728    assert(readMem() && "Load MachineMemOperand is not a load!");
1729    assert(!writeMem() && "Load MachineMemOperand is a store!");
1730  }
1731public:
1732
1733  /// getExtensionType - Return whether this is a plain node,
1734  /// or one of the varieties of value-extending loads.
1735  ISD::LoadExtType getExtensionType() const {
1736    return ISD::LoadExtType(SubclassData & 3);
1737  }
1738
1739  const SDValue &getBasePtr() const { return getOperand(1); }
1740  const SDValue &getOffset() const { return getOperand(2); }
1741
1742  static bool classof(const SDNode *N) {
1743    return N->getOpcode() == ISD::LOAD;
1744  }
1745};
1746
1747/// StoreSDNode - This class is used to represent ISD::STORE nodes.
1748///
1749class StoreSDNode : public LSBaseSDNode {
1750  friend class SelectionDAG;
1751  StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
1752              SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1753              MachineMemOperand *MMO)
1754    : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
1755                   VTs, AM, MemVT, MMO) {
1756    SubclassData |= (unsigned short)isTrunc;
1757    assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1758    assert(!readMem() && "Store MachineMemOperand is a load!");
1759    assert(writeMem() && "Store MachineMemOperand is not a store!");
1760  }
1761public:
1762
1763  /// isTruncatingStore - Return true if the op does a truncation before store.
1764  /// For integers this is the same as doing a TRUNCATE and storing the result.
1765  /// For floats, it is the same as doing an FP_ROUND and storing the result.
1766  bool isTruncatingStore() const { return SubclassData & 1; }
1767
1768  const SDValue &getValue() const { return getOperand(1); }
1769  const SDValue &getBasePtr() const { return getOperand(2); }
1770  const SDValue &getOffset() const { return getOperand(3); }
1771
1772  static bool classof(const SDNode *N) {
1773    return N->getOpcode() == ISD::STORE;
1774  }
1775};
1776
1777/// MachineSDNode - An SDNode that represents everything that will be needed
1778/// to construct a MachineInstr. These nodes are created during the
1779/// instruction selection proper phase.
1780///
1781class MachineSDNode : public SDNode {
1782public:
1783  typedef MachineMemOperand **mmo_iterator;
1784
1785private:
1786  friend class SelectionDAG;
1787  MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
1788    : SDNode(Opc, Order, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1789
1790  /// LocalOperands - Operands for this instruction, if they fit here. If
1791  /// they don't, this field is unused.
1792  SDUse LocalOperands[4];
1793
1794  /// MemRefs - Memory reference descriptions for this instruction.
1795  mmo_iterator MemRefs;
1796  mmo_iterator MemRefsEnd;
1797
1798public:
1799  mmo_iterator memoperands_begin() const { return MemRefs; }
1800  mmo_iterator memoperands_end() const { return MemRefsEnd; }
1801  bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1802
1803  /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1804  /// list. This does not transfer ownership.
1805  void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1806    for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1807      assert(*MMI && "Null mem ref detected!");
1808    MemRefs = NewMemRefs;
1809    MemRefsEnd = NewMemRefsEnd;
1810  }
1811
1812  static bool classof(const SDNode *N) {
1813    return N->isMachineOpcode();
1814  }
1815};
1816
1817class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1818                                            SDNode, ptrdiff_t> {
1819  const SDNode *Node;
1820  unsigned Operand;
1821
1822  SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1823public:
1824  bool operator==(const SDNodeIterator& x) const {
1825    return Operand == x.Operand;
1826  }
1827  bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1828
1829  const SDNodeIterator &operator=(const SDNodeIterator &I) {
1830    assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1831    Operand = I.Operand;
1832    return *this;
1833  }
1834
1835  pointer operator*() const {
1836    return Node->getOperand(Operand).getNode();
1837  }
1838  pointer operator->() const { return operator*(); }
1839
1840  SDNodeIterator& operator++() {                // Preincrement
1841    ++Operand;
1842    return *this;
1843  }
1844  SDNodeIterator operator++(int) { // Postincrement
1845    SDNodeIterator tmp = *this; ++*this; return tmp;
1846  }
1847  size_t operator-(SDNodeIterator Other) const {
1848    assert(Node == Other.Node &&
1849           "Cannot compare iterators of two different nodes!");
1850    return Operand - Other.Operand;
1851  }
1852
1853  static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1854  static SDNodeIterator end  (const SDNode *N) {
1855    return SDNodeIterator(N, N->getNumOperands());
1856  }
1857
1858  unsigned getOperand() const { return Operand; }
1859  const SDNode *getNode() const { return Node; }
1860};
1861
1862template <> struct GraphTraits<SDNode*> {
1863  typedef SDNode NodeType;
1864  typedef SDNodeIterator ChildIteratorType;
1865  static inline NodeType *getEntryNode(SDNode *N) { return N; }
1866  static inline ChildIteratorType child_begin(NodeType *N) {
1867    return SDNodeIterator::begin(N);
1868  }
1869  static inline ChildIteratorType child_end(NodeType *N) {
1870    return SDNodeIterator::end(N);
1871  }
1872};
1873
1874/// LargestSDNode - The largest SDNode class.
1875///
1876typedef AtomicSDNode LargestSDNode;
1877
1878/// MostAlignedSDNode - The SDNode class with the greatest alignment
1879/// requirement.
1880///
1881typedef GlobalAddressSDNode MostAlignedSDNode;
1882
1883namespace ISD {
1884  /// isNormalLoad - Returns true if the specified node is a non-extending
1885  /// and unindexed load.
1886  inline bool isNormalLoad(const SDNode *N) {
1887    const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1888    return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1889      Ld->getAddressingMode() == ISD::UNINDEXED;
1890  }
1891
1892  /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1893  /// load.
1894  inline bool isNON_EXTLoad(const SDNode *N) {
1895    return isa<LoadSDNode>(N) &&
1896      cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1897  }
1898
1899  /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1900  ///
1901  inline bool isEXTLoad(const SDNode *N) {
1902    return isa<LoadSDNode>(N) &&
1903      cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1904  }
1905
1906  /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1907  ///
1908  inline bool isSEXTLoad(const SDNode *N) {
1909    return isa<LoadSDNode>(N) &&
1910      cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1911  }
1912
1913  /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1914  ///
1915  inline bool isZEXTLoad(const SDNode *N) {
1916    return isa<LoadSDNode>(N) &&
1917      cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1918  }
1919
1920  /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1921  ///
1922  inline bool isUNINDEXEDLoad(const SDNode *N) {
1923    return isa<LoadSDNode>(N) &&
1924      cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1925  }
1926
1927  /// isNormalStore - Returns true if the specified node is a non-truncating
1928  /// and unindexed store.
1929  inline bool isNormalStore(const SDNode *N) {
1930    const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1931    return St && !St->isTruncatingStore() &&
1932      St->getAddressingMode() == ISD::UNINDEXED;
1933  }
1934
1935  /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1936  /// store.
1937  inline bool isNON_TRUNCStore(const SDNode *N) {
1938    return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1939  }
1940
1941  /// isTRUNCStore - Returns true if the specified node is a truncating
1942  /// store.
1943  inline bool isTRUNCStore(const SDNode *N) {
1944    return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1945  }
1946
1947  /// isUNINDEXEDStore - Returns true if the specified node is an
1948  /// unindexed store.
1949  inline bool isUNINDEXEDStore(const SDNode *N) {
1950    return isa<StoreSDNode>(N) &&
1951      cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1952  }
1953}
1954
1955} // end llvm namespace
1956
1957#endif
1958