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