2f2137da044de33b97b980d7208f7f36466af58a
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAGNodes.h
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
38 namespace llvm {
39
40 class SelectionDAG;
41 class GlobalValue;
42 class MachineBasicBlock;
43 class MachineConstantPoolValue;
44 class SDNode;
45 class Value;
46 class MCSymbol;
47 template <typename T> struct DenseMapInfo;
48 template <typename T> struct simplify_type;
49 template <typename T> struct ilist_traits;
50
51 void 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 ///
57 struct SDVTList {
58   const EVT *VTs;
59   unsigned int NumVTs;
60 };
61
62 namespace 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 ///
94 class SDValue {
95   SDNode *Node;       // The node defining the value we are using.
96   unsigned ResNo;     // Which return value of the node we are using.
97 public:
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
177 template<> 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 };
192 template <> 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.
197 template<> struct simplify_type<SDValue> {
198   typedef SDNode* SimpleType;
199   static SimpleType getSimplifiedValue(SDValue &Val) {
200     return Val.getNode();
201   }
202 };
203 template<> 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 ///
215 class 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
227 public:
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
265 private:
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.
296 template<> 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 ///
306 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
307 private:
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
320 protected:
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
326 private:
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
360 public:
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 { return N->hasPredecessor(this); }
524
525   /// hasPredecessor - Return true if N is a predecessor of this node.
526   /// N is either an operand of this node, or can be reached by recursively
527   /// traversing up the operands.
528   /// NOTE: This is an expensive method. Use it carefully.
529   bool hasPredecessor(const SDNode *N) const;
530
531   /// hasPredecesorHelper - Return true if N is a predecessor of this node.
532   /// N is either an operand of this node, or can be reached by recursively
533   /// traversing up the operands.
534   /// In this helper the Visited and worklist sets are held externally to
535   /// cache predecessors over multiple invocations. If you want to test for
536   /// multiple predecessors this method is preferable to multiple calls to
537   /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
538   /// changes.
539   /// NOTE: This is still very expensive. Use carefully.
540   bool hasPredecessorHelper(const SDNode *N,
541                             SmallPtrSet<const SDNode *, 32> &Visited,
542                             SmallVectorImpl<const SDNode *> &Worklist) const;
543
544   /// getNumOperands - Return the number of values used by this operation.
545   ///
546   unsigned getNumOperands() const { return NumOperands; }
547
548   /// getConstantOperandVal - Helper method returns the integer value of a
549   /// ConstantSDNode operand.
550   uint64_t getConstantOperandVal(unsigned Num) const;
551
552   const SDValue &getOperand(unsigned Num) const {
553     assert(Num < NumOperands && "Invalid child # of SDNode!");
554     return OperandList[Num];
555   }
556
557   typedef SDUse* op_iterator;
558   op_iterator op_begin() const { return OperandList; }
559   op_iterator op_end() const { return OperandList+NumOperands; }
560
561   SDVTList getVTList() const {
562     SDVTList X = { ValueList, NumValues };
563     return X;
564   }
565
566   /// getGluedNode - If this node has a glue operand, return the node
567   /// to which the glue operand points. Otherwise return NULL.
568   SDNode *getGluedNode() const {
569     if (getNumOperands() != 0 &&
570       getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
571       return getOperand(getNumOperands()-1).getNode();
572     return 0;
573   }
574
575   // If this is a pseudo op, like copyfromreg, look to see if there is a
576   // real target node glued to it.  If so, return the target node.
577   const SDNode *getGluedMachineNode() const {
578     const SDNode *FoundNode = this;
579
580     // Climb up glue edges until a machine-opcode node is found, or the
581     // end of the chain is reached.
582     while (!FoundNode->isMachineOpcode()) {
583       const SDNode *N = FoundNode->getGluedNode();
584       if (!N) break;
585       FoundNode = N;
586     }
587
588     return FoundNode;
589   }
590
591   /// getGluedUser - If this node has a glue value with a user, return
592   /// the user (there is at most one). Otherwise return NULL.
593   SDNode *getGluedUser() const {
594     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
595       if (UI.getUse().get().getValueType() == MVT::Glue)
596         return *UI;
597     return 0;
598   }
599
600   /// getNumValues - Return the number of values defined/returned by this
601   /// operator.
602   ///
603   unsigned getNumValues() const { return NumValues; }
604
605   /// getValueType - Return the type of a specified result.
606   ///
607   EVT getValueType(unsigned ResNo) const {
608     assert(ResNo < NumValues && "Illegal result number!");
609     return ValueList[ResNo];
610   }
611
612   /// Return the type of a specified result as a simple type.
613   ///
614   MVT getSimpleValueType(unsigned ResNo) const {
615     return getValueType(ResNo).getSimpleVT();
616   }
617
618   /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
619   ///
620   unsigned getValueSizeInBits(unsigned ResNo) const {
621     return getValueType(ResNo).getSizeInBits();
622   }
623
624   typedef const EVT* value_iterator;
625   value_iterator value_begin() const { return ValueList; }
626   value_iterator value_end() const { return ValueList+NumValues; }
627
628   /// getOperationName - Return the opcode of this operation for printing.
629   ///
630   std::string getOperationName(const SelectionDAG *G = 0) const;
631   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
632   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
633   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
634   void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
635   void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
636
637   /// printrFull - Print a SelectionDAG node and all children down to
638   /// the leaves.  The given SelectionDAG allows target-specific nodes
639   /// to be printed in human-readable form.  Unlike printr, this will
640   /// print the whole DAG, including children that appear multiple
641   /// times.
642   ///
643   void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
644
645   /// printrWithDepth - Print a SelectionDAG node and children up to
646   /// depth "depth."  The given SelectionDAG allows target-specific
647   /// nodes to be printed in human-readable form.  Unlike printr, this
648   /// will print children that appear multiple times wherever they are
649   /// used.
650   ///
651   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
652                        unsigned depth = 100) const;
653
654
655   /// dump - Dump this node, for debugging.
656   void dump() const;
657
658   /// dumpr - Dump (recursively) this node and its use-def subgraph.
659   void dumpr() const;
660
661   /// dump - Dump this node, for debugging.
662   /// The given SelectionDAG allows target-specific nodes to be printed
663   /// in human-readable form.
664   void dump(const SelectionDAG *G) const;
665
666   /// dumpr - Dump (recursively) this node and its use-def subgraph.
667   /// The given SelectionDAG allows target-specific nodes to be printed
668   /// in human-readable form.
669   void dumpr(const SelectionDAG *G) const;
670
671   /// dumprFull - printrFull to dbgs().  The given SelectionDAG allows
672   /// target-specific nodes to be printed in human-readable form.
673   /// Unlike dumpr, this will print the whole DAG, including children
674   /// that appear multiple times.
675   ///
676   void dumprFull(const SelectionDAG *G = 0) const;
677
678   /// dumprWithDepth - printrWithDepth to dbgs().  The given
679   /// SelectionDAG allows target-specific nodes to be printed in
680   /// human-readable form.  Unlike dumpr, this will print children
681   /// that appear multiple times wherever they are used.
682   ///
683   void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
684
685   /// Profile - Gather unique data for the node.
686   ///
687   void Profile(FoldingSetNodeID &ID) const;
688
689   /// addUse - This method should only be used by the SDUse class.
690   ///
691   void addUse(SDUse &U) { U.addToList(&UseList); }
692
693 protected:
694   static SDVTList getSDVTList(EVT VT) {
695     SDVTList Ret = { getValueTypeList(VT), 1 };
696     return Ret;
697   }
698
699   SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs,
700          const SDValue *Ops, unsigned NumOps)
701     : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
702       SubclassData(0), NodeId(-1),
703       OperandList(NumOps ? new SDUse[NumOps] : 0),
704       ValueList(VTs.VTs), UseList(NULL),
705       NumOperands(NumOps), NumValues(VTs.NumVTs),
706       debugLoc(dl), IROrder(Order) {
707     for (unsigned i = 0; i != NumOps; ++i) {
708       OperandList[i].setUser(this);
709       OperandList[i].setInitial(Ops[i]);
710     }
711     checkForCycles(this);
712   }
713
714   /// This constructor adds no operands itself; operands can be
715   /// set later with InitOperands.
716   SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs)
717     : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
718       SubclassData(0), NodeId(-1), OperandList(0),
719       ValueList(VTs.VTs), UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
720       debugLoc(dl), IROrder(Order) {}
721
722   /// InitOperands - Initialize the operands list of this with 1 operand.
723   void InitOperands(SDUse *Ops, const SDValue &Op0) {
724     Ops[0].setUser(this);
725     Ops[0].setInitial(Op0);
726     NumOperands = 1;
727     OperandList = Ops;
728     checkForCycles(this);
729   }
730
731   /// InitOperands - Initialize the operands list of this with 2 operands.
732   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
733     Ops[0].setUser(this);
734     Ops[0].setInitial(Op0);
735     Ops[1].setUser(this);
736     Ops[1].setInitial(Op1);
737     NumOperands = 2;
738     OperandList = Ops;
739     checkForCycles(this);
740   }
741
742   /// InitOperands - Initialize the operands list of this with 3 operands.
743   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
744                     const SDValue &Op2) {
745     Ops[0].setUser(this);
746     Ops[0].setInitial(Op0);
747     Ops[1].setUser(this);
748     Ops[1].setInitial(Op1);
749     Ops[2].setUser(this);
750     Ops[2].setInitial(Op2);
751     NumOperands = 3;
752     OperandList = Ops;
753     checkForCycles(this);
754   }
755
756   /// InitOperands - Initialize the operands list of this with 4 operands.
757   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
758                     const SDValue &Op2, const SDValue &Op3) {
759     Ops[0].setUser(this);
760     Ops[0].setInitial(Op0);
761     Ops[1].setUser(this);
762     Ops[1].setInitial(Op1);
763     Ops[2].setUser(this);
764     Ops[2].setInitial(Op2);
765     Ops[3].setUser(this);
766     Ops[3].setInitial(Op3);
767     NumOperands = 4;
768     OperandList = Ops;
769     checkForCycles(this);
770   }
771
772   /// InitOperands - Initialize the operands list of this with N operands.
773   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
774     for (unsigned i = 0; i != N; ++i) {
775       Ops[i].setUser(this);
776       Ops[i].setInitial(Vals[i]);
777     }
778     NumOperands = N;
779     OperandList = Ops;
780     checkForCycles(this);
781   }
782
783   /// DropOperands - Release the operands and set this node to have
784   /// zero operands.
785   void DropOperands();
786 };
787
788 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
789 /// into SDNode creation functions.
790 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
791 /// from the original Instruction, and IROrder is the ordinal position of
792 /// the instruction.
793 /// When an SDNode is created after the DAG is being built, both DebugLoc and
794 /// the IROrder are propagated from the original SDNode.
795 /// So SDLoc class provides two constructors besides the default one, one to
796 /// be used by the DAGBuilder, the other to be used by others.
797 class SDLoc {
798 private:
799   // Ptr could be used for either Instruction* or SDNode*. It is used for
800   // Instruction* if IROrder is not -1.
801   const void *Ptr;
802   int IROrder;
803
804 public:
805   SDLoc() : Ptr(NULL), IROrder(0) {}
806   SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
807     assert(N && "null SDNode");
808   }
809   SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
810     assert(Ptr && "null SDNode");
811   }
812   SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
813     assert(Order >= 0 && "bad IROrder");
814   }
815   unsigned getIROrder() {
816     if (IROrder >= 0 || Ptr == NULL) {
817       return (unsigned)IROrder;
818     }
819     const SDNode *N = (const SDNode*)(Ptr);
820     return N->getIROrder();
821   }
822   DebugLoc getDebugLoc() {
823     if (Ptr == NULL) {
824       return DebugLoc();
825     }
826     if (IROrder >= 0) {
827       const Instruction *I = (const Instruction*)(Ptr);
828       return I->getDebugLoc();
829     }
830     const SDNode *N = (const SDNode*)(Ptr);
831     return N->getDebugLoc();
832   }
833 };
834
835
836 // Define inline functions from the SDValue class.
837
838 inline unsigned SDValue::getOpcode() const {
839   return Node->getOpcode();
840 }
841 inline EVT SDValue::getValueType() const {
842   return Node->getValueType(ResNo);
843 }
844 inline unsigned SDValue::getNumOperands() const {
845   return Node->getNumOperands();
846 }
847 inline const SDValue &SDValue::getOperand(unsigned i) const {
848   return Node->getOperand(i);
849 }
850 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
851   return Node->getConstantOperandVal(i);
852 }
853 inline bool SDValue::isTargetOpcode() const {
854   return Node->isTargetOpcode();
855 }
856 inline bool SDValue::isTargetMemoryOpcode() const {
857   return Node->isTargetMemoryOpcode();
858 }
859 inline bool SDValue::isMachineOpcode() const {
860   return Node->isMachineOpcode();
861 }
862 inline unsigned SDValue::getMachineOpcode() const {
863   return Node->getMachineOpcode();
864 }
865 inline bool SDValue::use_empty() const {
866   return !Node->hasAnyUseOfValue(ResNo);
867 }
868 inline bool SDValue::hasOneUse() const {
869   return Node->hasNUsesOfValue(1, ResNo);
870 }
871 inline const DebugLoc SDValue::getDebugLoc() const {
872   return Node->getDebugLoc();
873 }
874 inline void SDValue::dump() const {
875   return Node->dump();
876 }
877 inline void SDValue::dumpr() const {
878   return Node->dumpr();
879 }
880 // Define inline functions from the SDUse class.
881
882 inline void SDUse::set(const SDValue &V) {
883   if (Val.getNode()) removeFromList();
884   Val = V;
885   if (V.getNode()) V.getNode()->addUse(*this);
886 }
887
888 inline void SDUse::setInitial(const SDValue &V) {
889   Val = V;
890   V.getNode()->addUse(*this);
891 }
892
893 inline void SDUse::setNode(SDNode *N) {
894   if (Val.getNode()) removeFromList();
895   Val.setNode(N);
896   if (N) N->addUse(*this);
897 }
898
899 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
900 /// to allow co-allocation of node operands with the node itself.
901 class UnarySDNode : public SDNode {
902   SDUse Op;
903 public:
904   UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, SDValue X)
905     : SDNode(Opc, Order, dl, VTs) {
906     InitOperands(&Op, X);
907   }
908 };
909
910 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
911 /// to allow co-allocation of node operands with the node itself.
912 class BinarySDNode : public SDNode {
913   SDUse Ops[2];
914 public:
915   BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
916     : SDNode(Opc, Order, dl, VTs) {
917     InitOperands(Ops, X, Y);
918   }
919 };
920
921 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
922 /// to allow co-allocation of node operands with the node itself.
923 class TernarySDNode : public SDNode {
924   SDUse Ops[3];
925 public:
926   TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
927                 SDValue X, SDValue Y, SDValue Z)
928     : SDNode(Opc, Order, dl, VTs) {
929     InitOperands(Ops, X, Y, Z);
930   }
931 };
932
933
934 /// HandleSDNode - This class is used to form a handle around another node that
935 /// is persistent and is updated across invocations of replaceAllUsesWith on its
936 /// operand.  This node should be directly created by end-users and not added to
937 /// the AllNodes list.
938 class HandleSDNode : public SDNode {
939   SDUse Op;
940 public:
941   // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
942   // fixed.
943 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
944   explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
945 #else
946   explicit HandleSDNode(SDValue X)
947 #endif
948     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
949     InitOperands(&Op, X);
950   }
951   ~HandleSDNode();
952   const SDValue &getValue() const { return Op; }
953 };
954
955 /// Abstact virtual class for operations for memory operations
956 class MemSDNode : public SDNode {
957 private:
958   // MemoryVT - VT of in-memory value.
959   EVT MemoryVT;
960
961 protected:
962   /// MMO - Memory reference information.
963   MachineMemOperand *MMO;
964
965 public:
966   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
967             EVT MemoryVT, MachineMemOperand *MMO);
968
969   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
970             unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
971
972   bool readMem() const { return MMO->isLoad(); }
973   bool writeMem() const { return MMO->isStore(); }
974
975   /// Returns alignment and volatility of the memory access
976   unsigned getOriginalAlignment() const { 
977     return MMO->getBaseAlignment();
978   }
979   unsigned getAlignment() const {
980     return MMO->getAlignment();
981   }
982
983   /// getRawSubclassData - Return the SubclassData value, which contains an
984   /// encoding of the volatile flag, as well as bits used by subclasses. This
985   /// function should only be used to compute a FoldingSetNodeID value.
986   unsigned getRawSubclassData() const {
987     return SubclassData;
988   }
989
990   // We access subclass data here so that we can check consistency
991   // with MachineMemOperand information.
992   bool isVolatile() const { return (SubclassData >> 5) & 1; }
993   bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
994   bool isInvariant() const { return (SubclassData >> 7) & 1; }
995
996   AtomicOrdering getOrdering() const {
997     return AtomicOrdering((SubclassData >> 8) & 15);
998   }
999   SynchronizationScope getSynchScope() const {
1000     return SynchronizationScope((SubclassData >> 12) & 1);
1001   }
1002
1003   /// Returns the SrcValue and offset that describes the location of the access
1004   const Value *getSrcValue() const { return MMO->getValue(); }
1005   int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1006
1007   /// Returns the TBAAInfo that describes the dereference.
1008   const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
1009
1010   /// Returns the Ranges that describes the dereference.
1011   const MDNode *getRanges() const { return MMO->getRanges(); }
1012
1013   /// getMemoryVT - Return the type of the in-memory value.
1014   EVT getMemoryVT() const { return MemoryVT; }
1015
1016   /// getMemOperand - Return a MachineMemOperand object describing the memory
1017   /// reference performed by operation.
1018   MachineMemOperand *getMemOperand() const { return MMO; }
1019
1020   const MachinePointerInfo &getPointerInfo() const {
1021     return MMO->getPointerInfo();
1022   }
1023
1024   /// getAddressSpace - Return the address space for the associated pointer
1025   unsigned getAddressSpace() const {
1026     return getPointerInfo().getAddrSpace();
1027   }
1028
1029   /// refineAlignment - Update this MemSDNode's MachineMemOperand information
1030   /// to reflect the alignment of NewMMO, if it has a greater alignment.
1031   /// This must only be used when the new alignment applies to all users of
1032   /// this MachineMemOperand.
1033   void refineAlignment(const MachineMemOperand *NewMMO) {
1034     MMO->refineAlignment(NewMMO);
1035   }
1036
1037   const SDValue &getChain() const { return getOperand(0); }
1038   const SDValue &getBasePtr() const {
1039     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1040   }
1041
1042   // Methods to support isa and dyn_cast
1043   static bool classof(const SDNode *N) {
1044     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1045     // with either an intrinsic or a target opcode.
1046     return N->getOpcode() == ISD::LOAD                ||
1047            N->getOpcode() == ISD::STORE               ||
1048            N->getOpcode() == ISD::PREFETCH            ||
1049            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1050            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1051            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1052            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1053            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1054            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1055            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1056            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1057            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1058            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1059            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1060            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1061            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1062            N->getOpcode() == ISD::ATOMIC_STORE        ||
1063            N->isTargetMemoryOpcode();
1064   }
1065 };
1066
1067 /// AtomicSDNode - A SDNode reprenting atomic operations.
1068 ///
1069 class AtomicSDNode : public MemSDNode {
1070   SDUse Ops[4];
1071
1072   void InitAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope) {
1073     // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1074     assert((Ordering & 15) == Ordering &&
1075            "Ordering may not require more than 4 bits!");
1076     assert((SynchScope & 1) == SynchScope &&
1077            "SynchScope may not require more than 1 bit!");
1078     SubclassData |= Ordering << 8;
1079     SubclassData |= SynchScope << 12;
1080     assert(getOrdering() == Ordering && "Ordering encoding error!");
1081     assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1082   }
1083
1084 public:
1085   // Opc:   opcode for atomic
1086   // VTL:    value type list
1087   // Chain:  memory chain for operaand
1088   // Ptr:    address to update as a SDValue
1089   // Cmp:    compare value
1090   // Swp:    swap value
1091   // SrcVal: address to update as a Value (used for MemOperand)
1092   // Align:  alignment of memory
1093   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1094                SDValue Chain, SDValue Ptr,
1095                SDValue Cmp, SDValue Swp, MachineMemOperand *MMO,
1096                AtomicOrdering Ordering, SynchronizationScope SynchScope)
1097     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1098     InitAtomic(Ordering, SynchScope);
1099     InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1100   }
1101   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1102                SDValue Chain, SDValue Ptr,
1103                SDValue Val, MachineMemOperand *MMO,
1104                AtomicOrdering Ordering, SynchronizationScope SynchScope)
1105     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1106     InitAtomic(Ordering, SynchScope);
1107     InitOperands(Ops, Chain, Ptr, Val);
1108   }
1109   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1110                SDValue Chain, SDValue Ptr,
1111                MachineMemOperand *MMO,
1112                AtomicOrdering Ordering, SynchronizationScope SynchScope)
1113     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1114     InitAtomic(Ordering, SynchScope);
1115     InitOperands(Ops, Chain, Ptr);
1116   }
1117
1118   const SDValue &getBasePtr() const { return getOperand(1); }
1119   const SDValue &getVal() const { return getOperand(2); }
1120
1121   bool isCompareAndSwap() const {
1122     unsigned Op = getOpcode();
1123     return Op == ISD::ATOMIC_CMP_SWAP;
1124   }
1125
1126   // Methods to support isa and dyn_cast
1127   static bool classof(const SDNode *N) {
1128     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1129            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1130            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1131            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1132            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1133            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1134            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1135            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1136            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1137            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1138            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1139            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1140            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1141            N->getOpcode() == ISD::ATOMIC_STORE;
1142   }
1143 };
1144
1145 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1146 /// memory and need an associated MachineMemOperand. Its opcode may be
1147 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1148 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1149 class MemIntrinsicSDNode : public MemSDNode {
1150 public:
1151   MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1152                      const SDValue *Ops, unsigned NumOps,
1153                      EVT MemoryVT, MachineMemOperand *MMO)
1154     : MemSDNode(Opc, Order, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1155   }
1156
1157   // Methods to support isa and dyn_cast
1158   static bool classof(const SDNode *N) {
1159     // We lower some target intrinsics to their target opcode
1160     // early a node with a target opcode can be of this class
1161     return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1162            N->getOpcode() == ISD::INTRINSIC_VOID ||
1163            N->getOpcode() == ISD::PREFETCH ||
1164            N->isTargetMemoryOpcode();
1165   }
1166 };
1167
1168 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1169 /// support for the llvm IR shufflevector instruction.  It combines elements
1170 /// from two input vectors into a new input vector, with the selection and
1171 /// ordering of elements determined by an array of integers, referred to as
1172 /// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
1173 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1174 /// An index of -1 is treated as undef, such that the code generator may put
1175 /// any value in the corresponding element of the result.
1176 class ShuffleVectorSDNode : public SDNode {
1177   SDUse Ops[2];
1178
1179   // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1180   // is freed when the SelectionDAG object is destroyed.
1181   const int *Mask;
1182 protected:
1183   friend class SelectionDAG;
1184   ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1, SDValue N2, const int *M)
1185     : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
1186     InitOperands(Ops, N1, N2);
1187   }
1188 public:
1189
1190   ArrayRef<int> getMask() const {
1191     EVT VT = getValueType(0);
1192     return makeArrayRef(Mask, VT.getVectorNumElements());
1193   }
1194   int getMaskElt(unsigned Idx) const {
1195     assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1196     return Mask[Idx];
1197   }
1198   
1199   bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1200   int  getSplatIndex() const { 
1201     assert(isSplat() && "Cannot get splat index for non-splat!");
1202     EVT VT = getValueType(0);
1203     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1204       if (Mask[i] != -1)
1205         return Mask[i];
1206     }
1207     return -1;
1208   }
1209   static bool isSplatMask(const int *Mask, EVT VT);
1210
1211   static bool classof(const SDNode *N) {
1212     return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1213   }
1214 };
1215   
1216 class ConstantSDNode : public SDNode {
1217   const ConstantInt *Value;
1218   friend class SelectionDAG;
1219   ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1220     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1221              0, DebugLoc(), getSDVTList(VT)), Value(val) {
1222   }
1223 public:
1224
1225   const ConstantInt *getConstantIntValue() const { return Value; }
1226   const APInt &getAPIntValue() const { return Value->getValue(); }
1227   uint64_t getZExtValue() const { return Value->getZExtValue(); }
1228   int64_t getSExtValue() const { return Value->getSExtValue(); }
1229
1230   bool isOne() const { return Value->isOne(); }
1231   bool isNullValue() const { return Value->isNullValue(); }
1232   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1233
1234   static bool classof(const SDNode *N) {
1235     return N->getOpcode() == ISD::Constant ||
1236            N->getOpcode() == ISD::TargetConstant;
1237   }
1238 };
1239
1240 class ConstantFPSDNode : public SDNode {
1241   const ConstantFP *Value;
1242   friend class SelectionDAG;
1243   ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1244     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1245              0, DebugLoc(), getSDVTList(VT)), Value(val) {
1246   }
1247 public:
1248
1249   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1250   const ConstantFP *getConstantFPValue() const { return Value; }
1251
1252   /// isZero - Return true if the value is positive or negative zero.
1253   bool isZero() const { return Value->isZero(); }
1254
1255   /// isNaN - Return true if the value is a NaN.
1256   bool isNaN() const { return Value->isNaN(); }
1257
1258   /// isExactlyValue - We don't rely on operator== working on double values, as
1259   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1260   /// As such, this method can be used to do an exact bit-for-bit comparison of
1261   /// two floating point values.
1262
1263   /// We leave the version with the double argument here because it's just so
1264   /// convenient to write "2.0" and the like.  Without this function we'd
1265   /// have to duplicate its logic everywhere it's called.
1266   bool isExactlyValue(double V) const {
1267     bool ignored;
1268     APFloat Tmp(V);
1269     Tmp.convert(Value->getValueAPF().getSemantics(),
1270                 APFloat::rmNearestTiesToEven, &ignored);
1271     return isExactlyValue(Tmp);
1272   }
1273   bool isExactlyValue(const APFloat& V) const;
1274
1275   static bool isValueValidForType(EVT VT, const APFloat& Val);
1276
1277   static bool classof(const SDNode *N) {
1278     return N->getOpcode() == ISD::ConstantFP ||
1279            N->getOpcode() == ISD::TargetConstantFP;
1280   }
1281 };
1282
1283 class GlobalAddressSDNode : public SDNode {
1284   const GlobalValue *TheGlobal;
1285   int64_t Offset;
1286   unsigned char TargetFlags;
1287   friend class SelectionDAG;
1288   GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL, const GlobalValue *GA, EVT VT,
1289                       int64_t o, unsigned char TargetFlags);
1290 public:
1291
1292   const GlobalValue *getGlobal() const { return TheGlobal; }
1293   int64_t getOffset() const { return Offset; }
1294   unsigned char getTargetFlags() const { return TargetFlags; }
1295   // Return the address space this GlobalAddress belongs to.
1296   unsigned getAddressSpace() const;
1297
1298   static bool classof(const SDNode *N) {
1299     return N->getOpcode() == ISD::GlobalAddress ||
1300            N->getOpcode() == ISD::TargetGlobalAddress ||
1301            N->getOpcode() == ISD::GlobalTLSAddress ||
1302            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1303   }
1304 };
1305
1306 class FrameIndexSDNode : public SDNode {
1307   int FI;
1308   friend class SelectionDAG;
1309   FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1310     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1311       0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1312   }
1313 public:
1314
1315   int getIndex() const { return FI; }
1316
1317   static bool classof(const SDNode *N) {
1318     return N->getOpcode() == ISD::FrameIndex ||
1319            N->getOpcode() == ISD::TargetFrameIndex;
1320   }
1321 };
1322
1323 class JumpTableSDNode : public SDNode {
1324   int JTI;
1325   unsigned char TargetFlags;
1326   friend class SelectionDAG;
1327   JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1328     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1329       0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1330   }
1331 public:
1332
1333   int getIndex() const { return JTI; }
1334   unsigned char getTargetFlags() const { return TargetFlags; }
1335
1336   static bool classof(const SDNode *N) {
1337     return N->getOpcode() == ISD::JumpTable ||
1338            N->getOpcode() == ISD::TargetJumpTable;
1339   }
1340 };
1341
1342 class ConstantPoolSDNode : public SDNode {
1343   union {
1344     const Constant *ConstVal;
1345     MachineConstantPoolValue *MachineCPVal;
1346   } Val;
1347   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1348   unsigned Alignment;  // Minimum alignment requirement of CP (not log2 value).
1349   unsigned char TargetFlags;
1350   friend class SelectionDAG;
1351   ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1352                      unsigned Align, unsigned char TF)
1353     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0, DebugLoc(),
1354              getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1355     assert(Offset >= 0 && "Offset is too large");
1356     Val.ConstVal = c;
1357   }
1358   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1359                      EVT VT, int o, unsigned Align, unsigned char TF)
1360     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0, DebugLoc(),
1361              getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1362     assert(Offset >= 0 && "Offset is too large");
1363     Val.MachineCPVal = v;
1364     Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1365   }
1366 public:
1367   
1368
1369   bool isMachineConstantPoolEntry() const {
1370     return Offset < 0;
1371   }
1372
1373   const Constant *getConstVal() const {
1374     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1375     return Val.ConstVal;
1376   }
1377
1378   MachineConstantPoolValue *getMachineCPVal() const {
1379     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1380     return Val.MachineCPVal;
1381   }
1382
1383   int getOffset() const {
1384     return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1385   }
1386
1387   // Return the alignment of this constant pool object, which is either 0 (for
1388   // default alignment) or the desired value.
1389   unsigned getAlignment() const { return Alignment; }
1390   unsigned char getTargetFlags() const { return TargetFlags; }
1391
1392   Type *getType() const;
1393
1394   static bool classof(const SDNode *N) {
1395     return N->getOpcode() == ISD::ConstantPool ||
1396            N->getOpcode() == ISD::TargetConstantPool;
1397   }
1398 };
1399
1400 /// Completely target-dependent object reference.
1401 class TargetIndexSDNode : public SDNode {
1402   unsigned char TargetFlags;
1403   int Index;
1404   int64_t Offset;
1405   friend class SelectionDAG;
1406 public:
1407
1408   TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1409     : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1410       TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1411 public:
1412
1413   unsigned char getTargetFlags() const { return TargetFlags; }
1414   int getIndex() const { return Index; }
1415   int64_t getOffset() const { return Offset; }
1416
1417   static bool classof(const SDNode *N) {
1418     return N->getOpcode() == ISD::TargetIndex;
1419   }
1420 };
1421
1422 class BasicBlockSDNode : public SDNode {
1423   MachineBasicBlock *MBB;
1424   friend class SelectionDAG;
1425   /// Debug info is meaningful and potentially useful here, but we create
1426   /// blocks out of order when they're jumped to, which makes it a bit
1427   /// harder.  Let's see if we need it first.
1428   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1429     : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1430   }
1431 public:
1432
1433   MachineBasicBlock *getBasicBlock() const { return MBB; }
1434
1435   static bool classof(const SDNode *N) {
1436     return N->getOpcode() == ISD::BasicBlock;
1437   }
1438 };
1439
1440 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1441 /// BUILD_VECTORs.
1442 class BuildVectorSDNode : public SDNode {
1443   // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1444   explicit BuildVectorSDNode() LLVM_DELETED_FUNCTION;
1445 public:
1446   /// isConstantSplat - Check if this is a constant splat, and if so, find the
1447   /// smallest element size that splats the vector.  If MinSplatBits is
1448   /// nonzero, the element size must be at least that large.  Note that the
1449   /// splat element may be the entire vector (i.e., a one element vector).
1450   /// Returns the splat element value in SplatValue.  Any undefined bits in
1451   /// that value are zero, and the corresponding bits in the SplatUndef mask
1452   /// are set.  The SplatBitSize value is set to the splat element size in
1453   /// bits.  HasAnyUndefs is set to true if any bits in the vector are
1454   /// undefined.  isBigEndian describes the endianness of the target.
1455   bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1456                        unsigned &SplatBitSize, bool &HasAnyUndefs,
1457                        unsigned MinSplatBits = 0, bool isBigEndian = false);
1458
1459   static inline bool classof(const SDNode *N) {
1460     return N->getOpcode() == ISD::BUILD_VECTOR;
1461   }
1462 };
1463
1464 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1465 /// used when the SelectionDAG needs to make a simple reference to something
1466 /// in the LLVM IR representation.
1467 ///
1468 class SrcValueSDNode : public SDNode {
1469   const Value *V;
1470   friend class SelectionDAG;
1471   /// Create a SrcValue for a general value.
1472   explicit SrcValueSDNode(const Value *v)
1473     : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1474
1475 public:
1476   /// getValue - return the contained Value.
1477   const Value *getValue() const { return V; }
1478
1479   static bool classof(const SDNode *N) {
1480     return N->getOpcode() == ISD::SRCVALUE;
1481   }
1482 };
1483   
1484 class MDNodeSDNode : public SDNode {
1485   const MDNode *MD;
1486   friend class SelectionDAG;
1487   explicit MDNodeSDNode(const MDNode *md)
1488   : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1489 public:
1490   
1491   const MDNode *getMD() const { return MD; }
1492   
1493   static bool classof(const SDNode *N) {
1494     return N->getOpcode() == ISD::MDNODE_SDNODE;
1495   }
1496 };
1497
1498
1499 class RegisterSDNode : public SDNode {
1500   unsigned Reg;
1501   friend class SelectionDAG;
1502   RegisterSDNode(unsigned reg, EVT VT)
1503     : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1504   }
1505 public:
1506
1507   unsigned getReg() const { return Reg; }
1508
1509   static bool classof(const SDNode *N) {
1510     return N->getOpcode() == ISD::Register;
1511   }
1512 };
1513
1514 class RegisterMaskSDNode : public SDNode {
1515   // The memory for RegMask is not owned by the node.
1516   const uint32_t *RegMask;
1517   friend class SelectionDAG;
1518   RegisterMaskSDNode(const uint32_t *mask)
1519     : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1520       RegMask(mask) {}
1521 public:
1522
1523   const uint32_t *getRegMask() const { return RegMask; }
1524
1525   static bool classof(const SDNode *N) {
1526     return N->getOpcode() == ISD::RegisterMask;
1527   }
1528 };
1529
1530 class BlockAddressSDNode : public SDNode {
1531   const BlockAddress *BA;
1532   int64_t Offset;
1533   unsigned char TargetFlags;
1534   friend class SelectionDAG;
1535   BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1536                      int64_t o, unsigned char Flags)
1537     : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1538              BA(ba), Offset(o), TargetFlags(Flags) {
1539   }
1540 public:
1541   const BlockAddress *getBlockAddress() const { return BA; }
1542   int64_t getOffset() const { return Offset; }
1543   unsigned char getTargetFlags() const { return TargetFlags; }
1544
1545   static bool classof(const SDNode *N) {
1546     return N->getOpcode() == ISD::BlockAddress ||
1547            N->getOpcode() == ISD::TargetBlockAddress;
1548   }
1549 };
1550
1551 class EHLabelSDNode : public SDNode {
1552   SDUse Chain;
1553   MCSymbol *Label;
1554   friend class SelectionDAG;
1555   EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
1556     : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
1557     InitOperands(&Chain, ch);
1558   }
1559 public:
1560   MCSymbol *getLabel() const { return Label; }
1561
1562   static bool classof(const SDNode *N) {
1563     return N->getOpcode() == ISD::EH_LABEL;
1564   }
1565 };
1566
1567 class ExternalSymbolSDNode : public SDNode {
1568   const char *Symbol;
1569   unsigned char TargetFlags;
1570   
1571   friend class SelectionDAG;
1572   ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1573     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1574              0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1575   }
1576 public:
1577
1578   const char *getSymbol() const { return Symbol; }
1579   unsigned char getTargetFlags() const { return TargetFlags; }
1580
1581   static bool classof(const SDNode *N) {
1582     return N->getOpcode() == ISD::ExternalSymbol ||
1583            N->getOpcode() == ISD::TargetExternalSymbol;
1584   }
1585 };
1586
1587 class CondCodeSDNode : public SDNode {
1588   ISD::CondCode Condition;
1589   friend class SelectionDAG;
1590   explicit CondCodeSDNode(ISD::CondCode Cond)
1591     : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1592       Condition(Cond) {
1593   }
1594 public:
1595
1596   ISD::CondCode get() const { return Condition; }
1597
1598   static bool classof(const SDNode *N) {
1599     return N->getOpcode() == ISD::CONDCODE;
1600   }
1601 };
1602   
1603 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1604 /// future and most targets don't support it.
1605 class CvtRndSatSDNode : public SDNode {
1606   ISD::CvtCode CvtCode;
1607   friend class SelectionDAG;
1608   explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl, const SDValue *Ops,
1609                            unsigned NumOps, ISD::CvtCode Code)
1610     : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops, NumOps),
1611       CvtCode(Code) {
1612     assert(NumOps == 5 && "wrong number of operations");
1613   }
1614 public:
1615   ISD::CvtCode getCvtCode() const { return CvtCode; }
1616
1617   static bool classof(const SDNode *N) {
1618     return N->getOpcode() == ISD::CONVERT_RNDSAT;
1619   }
1620 };
1621
1622 /// VTSDNode - This class is used to represent EVT's, which are used
1623 /// to parameterize some operations.
1624 class VTSDNode : public SDNode {
1625   EVT ValueType;
1626   friend class SelectionDAG;
1627   explicit VTSDNode(EVT VT)
1628     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1629       ValueType(VT) {
1630   }
1631 public:
1632
1633   EVT getVT() const { return ValueType; }
1634
1635   static bool classof(const SDNode *N) {
1636     return N->getOpcode() == ISD::VALUETYPE;
1637   }
1638 };
1639
1640 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1641 ///
1642 class LSBaseSDNode : public MemSDNode {
1643   //! Operand array for load and store
1644   /*!
1645     \note Moving this array to the base class captures more
1646     common functionality shared between LoadSDNode and
1647     StoreSDNode
1648    */
1649   SDUse Ops[4];
1650 public:
1651   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl, SDValue *Operands,
1652                unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1653                EVT MemVT, MachineMemOperand *MMO)
1654     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1655     SubclassData |= AM << 2;
1656     assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1657     InitOperands(Ops, Operands, numOperands);
1658     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1659            "Only indexed loads and stores have a non-undef offset operand");
1660   }
1661
1662   const SDValue &getOffset() const {
1663     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1664   }
1665
1666   /// getAddressingMode - Return the addressing mode for this load or store:
1667   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1668   ISD::MemIndexedMode getAddressingMode() const {
1669     return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1670   }
1671
1672   /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1673   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1674
1675   /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1676   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1677
1678   static bool classof(const SDNode *N) {
1679     return N->getOpcode() == ISD::LOAD ||
1680            N->getOpcode() == ISD::STORE;
1681   }
1682 };
1683
1684 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1685 ///
1686 class LoadSDNode : public LSBaseSDNode {
1687   friend class SelectionDAG;
1688   LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
1689              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1690              MachineMemOperand *MMO)
1691     : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
1692     SubclassData |= (unsigned short)ETy;
1693     assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1694     assert(readMem() && "Load MachineMemOperand is not a load!");
1695     assert(!writeMem() && "Load MachineMemOperand is a store!");
1696   }
1697 public:
1698
1699   /// getExtensionType - Return whether this is a plain node,
1700   /// or one of the varieties of value-extending loads.
1701   ISD::LoadExtType getExtensionType() const {
1702     return ISD::LoadExtType(SubclassData & 3);
1703   }
1704
1705   const SDValue &getBasePtr() const { return getOperand(1); }
1706   const SDValue &getOffset() const { return getOperand(2); }
1707
1708   static bool classof(const SDNode *N) {
1709     return N->getOpcode() == ISD::LOAD;
1710   }
1711 };
1712
1713 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1714 ///
1715 class StoreSDNode : public LSBaseSDNode {
1716   friend class SelectionDAG;
1717   StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
1718               SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1719               MachineMemOperand *MMO)
1720     : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
1721                    VTs, AM, MemVT, MMO) {
1722     SubclassData |= (unsigned short)isTrunc;
1723     assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1724     assert(!readMem() && "Store MachineMemOperand is a load!");
1725     assert(writeMem() && "Store MachineMemOperand is not a store!");
1726   }
1727 public:
1728
1729   /// isTruncatingStore - Return true if the op does a truncation before store.
1730   /// For integers this is the same as doing a TRUNCATE and storing the result.
1731   /// For floats, it is the same as doing an FP_ROUND and storing the result.
1732   bool isTruncatingStore() const { return SubclassData & 1; }
1733
1734   const SDValue &getValue() const { return getOperand(1); }
1735   const SDValue &getBasePtr() const { return getOperand(2); }
1736   const SDValue &getOffset() const { return getOperand(3); }
1737
1738   static bool classof(const SDNode *N) {
1739     return N->getOpcode() == ISD::STORE;
1740   }
1741 };
1742
1743 /// MachineSDNode - An SDNode that represents everything that will be needed
1744 /// to construct a MachineInstr. These nodes are created during the
1745 /// instruction selection proper phase.
1746 ///
1747 class MachineSDNode : public SDNode {
1748 public:
1749   typedef MachineMemOperand **mmo_iterator;
1750
1751 private:
1752   friend class SelectionDAG;
1753   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
1754     : SDNode(Opc, Order, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1755
1756   /// LocalOperands - Operands for this instruction, if they fit here. If
1757   /// they don't, this field is unused.
1758   SDUse LocalOperands[4];
1759
1760   /// MemRefs - Memory reference descriptions for this instruction.
1761   mmo_iterator MemRefs;
1762   mmo_iterator MemRefsEnd;
1763
1764 public:
1765   mmo_iterator memoperands_begin() const { return MemRefs; }
1766   mmo_iterator memoperands_end() const { return MemRefsEnd; }
1767   bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1768
1769   /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1770   /// list. This does not transfer ownership.
1771   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1772     for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1773       assert(*MMI && "Null mem ref detected!");
1774     MemRefs = NewMemRefs;
1775     MemRefsEnd = NewMemRefsEnd;
1776   }
1777
1778   static bool classof(const SDNode *N) {
1779     return N->isMachineOpcode();
1780   }
1781 };
1782
1783 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1784                                             SDNode, ptrdiff_t> {
1785   const SDNode *Node;
1786   unsigned Operand;
1787
1788   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1789 public:
1790   bool operator==(const SDNodeIterator& x) const {
1791     return Operand == x.Operand;
1792   }
1793   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1794
1795   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1796     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1797     Operand = I.Operand;
1798     return *this;
1799   }
1800
1801   pointer operator*() const {
1802     return Node->getOperand(Operand).getNode();
1803   }
1804   pointer operator->() const { return operator*(); }
1805
1806   SDNodeIterator& operator++() {                // Preincrement
1807     ++Operand;
1808     return *this;
1809   }
1810   SDNodeIterator operator++(int) { // Postincrement
1811     SDNodeIterator tmp = *this; ++*this; return tmp;
1812   }
1813   size_t operator-(SDNodeIterator Other) const {
1814     assert(Node == Other.Node &&
1815            "Cannot compare iterators of two different nodes!");
1816     return Operand - Other.Operand;
1817   }
1818
1819   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1820   static SDNodeIterator end  (const SDNode *N) {
1821     return SDNodeIterator(N, N->getNumOperands());
1822   }
1823
1824   unsigned getOperand() const { return Operand; }
1825   const SDNode *getNode() const { return Node; }
1826 };
1827
1828 template <> struct GraphTraits<SDNode*> {
1829   typedef SDNode NodeType;
1830   typedef SDNodeIterator ChildIteratorType;
1831   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1832   static inline ChildIteratorType child_begin(NodeType *N) {
1833     return SDNodeIterator::begin(N);
1834   }
1835   static inline ChildIteratorType child_end(NodeType *N) {
1836     return SDNodeIterator::end(N);
1837   }
1838 };
1839
1840 /// LargestSDNode - The largest SDNode class.
1841 ///
1842 typedef LoadSDNode LargestSDNode;
1843
1844 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1845 /// requirement.
1846 ///
1847 typedef GlobalAddressSDNode MostAlignedSDNode;
1848
1849 namespace ISD {
1850   /// isNormalLoad - Returns true if the specified node is a non-extending
1851   /// and unindexed load.
1852   inline bool isNormalLoad(const SDNode *N) {
1853     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1854     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1855       Ld->getAddressingMode() == ISD::UNINDEXED;
1856   }
1857
1858   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1859   /// load.
1860   inline bool isNON_EXTLoad(const SDNode *N) {
1861     return isa<LoadSDNode>(N) &&
1862       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1863   }
1864
1865   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1866   ///
1867   inline bool isEXTLoad(const SDNode *N) {
1868     return isa<LoadSDNode>(N) &&
1869       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1870   }
1871
1872   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1873   ///
1874   inline bool isSEXTLoad(const SDNode *N) {
1875     return isa<LoadSDNode>(N) &&
1876       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1877   }
1878
1879   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1880   ///
1881   inline bool isZEXTLoad(const SDNode *N) {
1882     return isa<LoadSDNode>(N) &&
1883       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1884   }
1885
1886   /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1887   ///
1888   inline bool isUNINDEXEDLoad(const SDNode *N) {
1889     return isa<LoadSDNode>(N) &&
1890       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1891   }
1892
1893   /// isNormalStore - Returns true if the specified node is a non-truncating
1894   /// and unindexed store.
1895   inline bool isNormalStore(const SDNode *N) {
1896     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1897     return St && !St->isTruncatingStore() &&
1898       St->getAddressingMode() == ISD::UNINDEXED;
1899   }
1900
1901   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1902   /// store.
1903   inline bool isNON_TRUNCStore(const SDNode *N) {
1904     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1905   }
1906
1907   /// isTRUNCStore - Returns true if the specified node is a truncating
1908   /// store.
1909   inline bool isTRUNCStore(const SDNode *N) {
1910     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1911   }
1912
1913   /// isUNINDEXEDStore - Returns true if the specified node is an
1914   /// unindexed store.
1915   inline bool isUNINDEXEDStore(const SDNode *N) {
1916     return isa<StoreSDNode>(N) &&
1917       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1918   }
1919 }
1920
1921 } // end llvm namespace
1922
1923 #endif