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