Start adding some new operators, give IMPLICIT_DEF a chain operand.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/CodeGen/ValueTypes.h"
23 #include "llvm/ADT/GraphTraits.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/iterator"
26 #include "llvm/Support/DataTypes.h"
27 #include <cassert>
28 #include <vector>
29
30 namespace llvm {
31
32 class SelectionDAG;
33 class GlobalValue;
34 class MachineBasicBlock;
35 class SDNode;
36 template <typename T> struct simplify_type;
37
38 /// ISD namespace - This namespace contains an enum which represents all of the
39 /// SelectionDAG node types and value types.
40 ///
41 namespace ISD {
42   //===--------------------------------------------------------------------===//
43   /// ISD::NodeType enum - This enum defines all of the operators valid in a
44   /// SelectionDAG.
45   ///
46   enum NodeType {
47     // EntryToken - This is the marker used to indicate the start of the region.
48     EntryToken,
49
50     // Token factor - This node is takes multiple tokens as input and produces a
51     // single token result.  This is used to represent the fact that the operand
52     // operators are independent of each other.
53     TokenFactor,
54     
55     // Various leaf nodes.
56     Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
57     BasicBlock, ExternalSymbol,
58
59     // CopyToReg - This node has chain and child nodes, and an associated
60     // register number.  The instruction selector must guarantee that the value
61     // of the value node is available in the register stored in the RegSDNode
62     // object.
63     CopyToReg,
64
65     // CopyFromReg - This node indicates that the input value is a virtual or
66     // physical register that is defined outside of the scope of this
67     // SelectionDAG.  The register is available from the RegSDNode object.
68     CopyFromReg,
69
70     // ImplicitDef - This node indicates that the specified register is
71     // implicitly defined by some operation (e.g. its a live-in argument).  This
72     // register is indicated in the RegSDNode object.  The only operand to this
73     // is the token chain coming in, the only result is the token chain going
74     // out.
75     ImplicitDef,
76
77     // EXTRACT_ELEMENT - This is used to get the first or second (determined by
78     // a Constant, which is required to be operand #1), element of the aggregate
79     // value specified as operand #0.  This is only for use before legalization,
80     // for values that will be broken into multiple registers.
81     EXTRACT_ELEMENT,
82
83     // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
84     // two values of the same integer value type, this produces a value twice as
85     // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
86     BUILD_PAIR,
87
88
89     // Simple binary arithmetic operators.
90     ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
91
92     // Bitwise operators.
93     AND, OR, XOR, SHL, SRA, SRL,
94
95     // Select operator.
96     SELECT,
97
98     // SetCC operator - This evaluates to a boolean (i1) true value if the
99     // condition is true.  These nodes are instances of the
100     // SetCCSDNode class, which contains the condition code as extra
101     // state.
102     SETCC,
103
104     // addc - Three input, two output operator: (X, Y, C) -> (X+Y+C,
105     // Cout).  X,Y are integer inputs of agreeing size, C is a one bit
106     // value, and two values are produced: the sum and a carry out.
107     ADDC, SUBB,
108
109     // Conversion operators.  These are all single input single output
110     // operations.  For all of these, the result type must be strictly
111     // wider or narrower (depending on the operation) than the source
112     // type.
113
114     // SIGN_EXTEND - Used for integer types, replicating the sign bit
115     // into new bits.
116     SIGN_EXTEND,
117
118     // ZERO_EXTEND - Used for integer types, zeroing the new bits.
119     ZERO_EXTEND,
120
121     // TRUNCATE - Completely drop the high bits.
122     TRUNCATE,
123
124     // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
125     // depends on the first letter) to floating point.
126     SINT_TO_FP,
127     UINT_TO_FP,
128
129     // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
130     // integer.
131     FP_TO_SINT,
132     FP_TO_UINT,
133
134     // FP_ROUND - Perform a rounding operation from the current
135     // precision down to the specified precision.
136     FP_ROUND,
137
138     // FP_EXTEND - Extend a smaller FP type into a larger FP type.
139     FP_EXTEND,
140
141     // Other operators.  LOAD and STORE have token chains as their first
142     // operand, then the same operands as an LLVM load/store instruction.
143     LOAD, STORE,
144
145     // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators are instances of the
146     // MVTSDNode.  All of these load a value from memory and extend them to a
147     // larger value (e.g. load a byte into a word register).  All three of these
148     // have two operands, a chain and a pointer to load from.  The extra value
149     // type is the source type being loaded.
150     //
151     // SEXTLOAD loads the integer operand and sign extends it to a larger
152     //          integer result type.
153     // ZEXTLOAD loads the integer operand and zero extends it to a larger
154     //          integer result type.
155     // EXTLOAD  is used for two things: floating point extending loads, and 
156     //          integer extending loads where it doesn't matter what the high
157     //          bits are set to.  The code generator is allowed to codegen this
158     //          into whichever operation is more efficient.
159     EXTLOAD, SEXTLOAD, ZEXTLOAD,
160
161     // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
162     // value and stores it to memory in one operation.  This can be used for
163     // either integer or floating point operands, and the stored type
164     // represented as the 'extra' value type in the MVTSDNode representing the
165     // operator.  This node has the same three operands as a standard store.
166     TRUNCSTORE,
167
168     // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
169     // to a specified boundary.  The first operand is the token chain, the
170     // second is the number of bytes to allocate, and the third is the alignment
171     // boundary.
172     DYNAMIC_STACKALLOC,
173
174     // Control flow instructions.  These all have token chains.
175     
176     // BR - Unconditional branch.  The first operand is the chain
177     // operand, the second is the MBB to branch to.
178     BR,
179
180     // BRCOND - Conditional branch.  The first operand is the chain,
181     // the second is the condition, the third is the block to branch
182     // to if the condition is true.
183     BRCOND,
184
185     // RET - Return from function.  The first operand is the chain,
186     // and any subsequent operands are the return values for the
187     // function.  This operation can have variable number of operands.
188     RET,
189
190     // CALL - Call to a function pointer.  The first operand is the chain, the
191     // second is the destination function pointer (a GlobalAddress for a direct
192     // call).  Arguments have already been lowered to explicit DAGs according to
193     // the calling convention in effect here.
194     CALL,
195
196     // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
197     // correspond to the operands of the LLVM intrinsic functions.  The only
198     // result is a token chain.  The alignment argument is guaranteed to be a
199     // Constant node.
200     MEMSET,
201     MEMMOVE,
202     MEMCPY,
203     
204     // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and
205     // end of a call sequence and indicate how much the stack pointer needs to
206     // be adjusted for that particular call.  The first operand is a chain, the
207     // second is a ConstantSDNode of intptr type.
208     ADJCALLSTACKDOWN,  // Beginning of a call sequence
209     ADJCALLSTACKUP,    // End of a call sequence
210
211
212     // BUILTIN_OP_END - This must be the last enum value in this list.
213     BUILTIN_OP_END,
214   };
215
216   //===--------------------------------------------------------------------===//
217   /// ISD::CondCode enum - These are ordered carefully to make the bitfields
218   /// below work out, when considering SETFALSE (something that never exists
219   /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
220   /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
221   /// to.  If the "N" column is 1, the result of the comparison is undefined if
222   /// the input is a NAN.
223   ///
224   /// All of these (except for the 'always folded ops') should be handled for
225   /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
226   /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
227   ///
228   /// Note that these are laid out in a specific order to allow bit-twiddling
229   /// to transform conditions.
230   enum CondCode {
231     // Opcode          N U L G E       Intuitive operation
232     SETFALSE,      //    0 0 0 0       Always false (always folded)
233     SETOEQ,        //    0 0 0 1       True if ordered and equal
234     SETOGT,        //    0 0 1 0       True if ordered and greater than
235     SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
236     SETOLT,        //    0 1 0 0       True if ordered and less than
237     SETOLE,        //    0 1 0 1       True if ordered and less than or equal
238     SETONE,        //    0 1 1 0       True if ordered and operands are unequal
239     SETO,          //    0 1 1 1       True if ordered (no nans)
240     SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
241     SETUEQ,        //    1 0 0 1       True if unordered or equal
242     SETUGT,        //    1 0 1 0       True if unordered or greater than
243     SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
244     SETULT,        //    1 1 0 0       True if unordered or less than
245     SETULE,        //    1 1 0 1       True if unordered, less than, or equal 
246     SETUNE,        //    1 1 1 0       True if unordered or not equal
247     SETTRUE,       //    1 1 1 1       Always true (always folded)
248     // Don't care operations: undefined if the input is a nan.
249     SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
250     SETEQ,         //  1 X 0 0 1       True if equal
251     SETGT,         //  1 X 0 1 0       True if greater than
252     SETGE,         //  1 X 0 1 1       True if greater than or equal
253     SETLT,         //  1 X 1 0 0       True if less than
254     SETLE,         //  1 X 1 0 1       True if less than or equal 
255     SETNE,         //  1 X 1 1 0       True if not equal
256     SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
257
258     SETCC_INVALID,      // Marker value.
259   };
260
261   /// isSignedIntSetCC - Return true if this is a setcc instruction that
262   /// performs a signed comparison when used with integer operands.
263   inline bool isSignedIntSetCC(CondCode Code) {
264     return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
265   }
266
267   /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
268   /// performs an unsigned comparison when used with integer operands.
269   inline bool isUnsignedIntSetCC(CondCode Code) {
270     return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
271   }
272
273   /// isTrueWhenEqual - Return true if the specified condition returns true if
274   /// the two operands to the condition are equal.  Note that if one of the two
275   /// operands is a NaN, this value is meaningless.
276   inline bool isTrueWhenEqual(CondCode Cond) {
277     return ((int)Cond & 1) != 0;
278   }
279
280   /// getUnorderedFlavor - This function returns 0 if the condition is always
281   /// false if an operand is a NaN, 1 if the condition is always true if the
282   /// operand is a NaN, and 2 if the condition is undefined if the operand is a
283   /// NaN.
284   inline unsigned getUnorderedFlavor(CondCode Cond) {
285     return ((int)Cond >> 3) & 3;
286   }
287
288   /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
289   /// 'op' is a valid SetCC operation.
290   CondCode getSetCCInverse(CondCode Operation, bool isInteger);
291
292   /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
293   /// when given the operation for (X op Y).
294   CondCode getSetCCSwappedOperands(CondCode Operation);
295
296   /// getSetCCOrOperation - Return the result of a logical OR between different
297   /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
298   /// function returns SETCC_INVALID if it is not possible to represent the
299   /// resultant comparison.
300   CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
301
302   /// getSetCCAndOperation - Return the result of a logical AND between
303   /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
304   /// function returns SETCC_INVALID if it is not possible to represent the
305   /// resultant comparison.
306   CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
307 }  // end llvm::ISD namespace
308
309
310 //===----------------------------------------------------------------------===//
311 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
312 /// values as the result of a computation.  Many nodes return multiple values,
313 /// from loads (which define a token and a return value) to ADDC (which returns
314 /// a result and a carry value), to calls (which may return an arbitrary number
315 /// of values).
316 ///
317 /// As such, each use of a SelectionDAG computation must indicate the node that
318 /// computes it as well as which return value to use from that node.  This pair
319 /// of information is represented with the SDOperand value type.
320 ///
321 class SDOperand {
322 public:
323   SDNode *Val;        // The node defining the value we are using.
324   unsigned ResNo;     // Which return value of the node we are using.
325
326   SDOperand() : Val(0) {}
327   SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
328
329   bool operator==(const SDOperand &O) const {
330     return Val == O.Val && ResNo == O.ResNo;
331   }
332   bool operator!=(const SDOperand &O) const {
333     return !operator==(O);
334   }
335   bool operator<(const SDOperand &O) const {
336     return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
337   }
338
339   SDOperand getValue(unsigned R) const {
340     return SDOperand(Val, R);
341   }
342
343   /// getValueType - Return the ValueType of the referenced return value.
344   ///
345   inline MVT::ValueType getValueType() const;
346
347   // Forwarding methods - These forward to the corresponding methods in SDNode.
348   inline unsigned getOpcode() const;
349   inline unsigned getNumOperands() const;
350   inline const SDOperand &getOperand(unsigned i) const;
351
352   /// hasOneUse - Return true if there is exactly one operation using this
353   /// result value of the defining operator.
354   inline bool hasOneUse() const;
355 };
356
357
358 /// simplify_type specializations - Allow casting operators to work directly on
359 /// SDOperands as if they were SDNode*'s.
360 template<> struct simplify_type<SDOperand> {
361   typedef SDNode* SimpleType;
362   static SimpleType getSimplifiedValue(const SDOperand &Val) {
363     return static_cast<SimpleType>(Val.Val);
364   }
365 };
366 template<> struct simplify_type<const SDOperand> {
367   typedef SDNode* SimpleType;
368   static SimpleType getSimplifiedValue(const SDOperand &Val) {
369     return static_cast<SimpleType>(Val.Val);
370   }
371 };
372
373
374 /// SDNode - Represents one node in the SelectionDAG.
375 ///
376 class SDNode {
377   unsigned NodeType;
378   std::vector<SDOperand> Operands;
379
380   /// Values - The types of the values this node defines.  SDNode's may define
381   /// multiple values simultaneously.
382   std::vector<MVT::ValueType> Values;
383
384   /// Uses - These are all of the SDNode's that use a value produced by this
385   /// node.
386   std::vector<SDNode*> Uses;
387 public:
388
389   //===--------------------------------------------------------------------===//
390   //  Accessors
391   //
392   unsigned getOpcode()  const { return NodeType; }
393
394   size_t use_size() const { return Uses.size(); }
395   bool use_empty() const { return Uses.empty(); }
396   bool hasOneUse() const { return Uses.size() == 1; }
397
398   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
399   /// indicated value.  This method ignores uses of other values defined by this
400   /// operation.
401   bool hasNUsesOfValue(unsigned NUses, unsigned Value);
402
403   /// getNumOperands - Return the number of values used by this operation.
404   ///
405   unsigned getNumOperands() const { return Operands.size(); }
406
407   const SDOperand &getOperand(unsigned Num) {
408     assert(Num < Operands.size() && "Invalid child # of SDNode!");
409     return Operands[Num];
410   }
411
412   const SDOperand &getOperand(unsigned Num) const {
413     assert(Num < Operands.size() && "Invalid child # of SDNode!");
414     return Operands[Num];
415   }
416
417   /// getNumValues - Return the number of values defined/returned by this
418   /// operator.
419   ///
420   unsigned getNumValues() const { return Values.size(); }
421
422   /// getValueType - Return the type of a specified result.
423   ///
424   MVT::ValueType getValueType(unsigned ResNo) const {
425     assert(ResNo < Values.size() && "Illegal result number!");
426     return Values[ResNo];
427   }
428
429   /// getOperationName - Return the opcode of this operation for printing.
430   ///
431   const char* getOperationName() const;
432   void dump() const;
433
434   static bool classof(const SDNode *) { return true; }
435
436 protected:
437   friend class SelectionDAG;
438
439   SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT) {
440     Values.reserve(1);
441     Values.push_back(VT);
442   }
443
444   SDNode(unsigned NT, SDOperand Op)
445     : NodeType(NT) {
446     Operands.reserve(1); Operands.push_back(Op);
447     Op.Val->Uses.push_back(this);
448   }
449   SDNode(unsigned NT, SDOperand N1, SDOperand N2)
450     : NodeType(NT) {
451     Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
452     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
453   }
454   SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
455     : NodeType(NT) {
456     Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
457     Operands.push_back(N3);
458     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
459     N3.Val->Uses.push_back(this);
460   }
461   SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
462     Operands.swap(Nodes);
463     for (unsigned i = 0, e = Operands.size(); i != e; ++i)
464       Operands[i].Val->Uses.push_back(this);
465   }
466
467   virtual ~SDNode() {
468     // FIXME: Drop uses.
469   }
470
471   void setValueTypes(MVT::ValueType VT) {
472     Values.reserve(1);
473     Values.push_back(VT);
474   }
475   void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
476     Values.reserve(2);
477     Values.push_back(VT1);
478     Values.push_back(VT2);
479   }
480   /// Note: this method destroys the vector passed in.
481   void setValueTypes(std::vector<MVT::ValueType> &VTs) {
482     std::swap(Values, VTs);
483   }
484
485   void removeUser(SDNode *User) {
486     // Remove this user from the operand's use list.
487     for (unsigned i = Uses.size(); ; --i) {
488       assert(i != 0 && "Didn't find user!");
489       if (Uses[i-1] == User) {
490         Uses.erase(Uses.begin()+i-1);
491         break;
492       }
493     }
494   }
495 };
496
497
498 // Define inline functions from the SDOperand class.
499
500 inline unsigned SDOperand::getOpcode() const {
501   return Val->getOpcode();
502 }
503 inline MVT::ValueType SDOperand::getValueType() const {
504   return Val->getValueType(ResNo);
505 }
506 inline unsigned SDOperand::getNumOperands() const {
507   return Val->getNumOperands();
508 }
509 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
510   return Val->getOperand(i);
511 }
512 inline bool SDOperand::hasOneUse() const {
513   return Val->hasNUsesOfValue(1, ResNo);
514 }
515
516
517 class ConstantSDNode : public SDNode {
518   uint64_t Value;
519 protected:
520   friend class SelectionDAG;
521   ConstantSDNode(uint64_t val, MVT::ValueType VT)
522     : SDNode(ISD::Constant, VT), Value(val) {
523   }
524 public:
525
526   uint64_t getValue() const { return Value; }
527
528   int64_t getSignExtended() const {
529     unsigned Bits = MVT::getSizeInBits(getValueType(0));
530     return ((int64_t)Value << (64-Bits)) >> (64-Bits);
531   }
532
533   bool isNullValue() const { return Value == 0; }
534   bool isAllOnesValue() const {
535     return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1;
536   }
537
538   static bool classof(const ConstantSDNode *) { return true; }
539   static bool classof(const SDNode *N) {
540     return N->getOpcode() == ISD::Constant;
541   }
542 };
543
544 class ConstantFPSDNode : public SDNode {
545   double Value;
546 protected:
547   friend class SelectionDAG;
548   ConstantFPSDNode(double val, MVT::ValueType VT)
549     : SDNode(ISD::ConstantFP, VT), Value(val) {
550   }
551 public:
552
553   double getValue() const { return Value; }
554
555   /// isExactlyValue - We don't rely on operator== working on double values, as
556   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
557   /// As such, this method can be used to do an exact bit-for-bit comparison of
558   /// two floating point values.
559   bool isExactlyValue(double V) const {
560     union {
561       double V;
562       uint64_t I;
563     } T1;
564     T1.V = Value;
565     union {
566       double V;
567       uint64_t I;
568     } T2;
569     T2.V = V;
570     return T1.I == T2.I;
571   }
572
573   static bool classof(const ConstantFPSDNode *) { return true; }
574   static bool classof(const SDNode *N) {
575     return N->getOpcode() == ISD::ConstantFP;
576   }
577 };
578
579 class GlobalAddressSDNode : public SDNode {
580   GlobalValue *TheGlobal;
581 protected:
582   friend class SelectionDAG;
583   GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
584     : SDNode(ISD::GlobalAddress, VT) {
585     TheGlobal = const_cast<GlobalValue*>(GA);
586   }
587 public:
588
589   GlobalValue *getGlobal() const { return TheGlobal; }
590
591   static bool classof(const GlobalAddressSDNode *) { return true; }
592   static bool classof(const SDNode *N) {
593     return N->getOpcode() == ISD::GlobalAddress;
594   }
595 };
596
597
598 class FrameIndexSDNode : public SDNode {
599   int FI;
600 protected:
601   friend class SelectionDAG;
602   FrameIndexSDNode(int fi, MVT::ValueType VT)
603     : SDNode(ISD::FrameIndex, VT), FI(fi) {}
604 public:
605
606   int getIndex() const { return FI; }
607
608   static bool classof(const FrameIndexSDNode *) { return true; }
609   static bool classof(const SDNode *N) {
610     return N->getOpcode() == ISD::FrameIndex;
611   }
612 };
613
614 class ConstantPoolSDNode : public SDNode {
615   unsigned CPI;
616 protected:
617   friend class SelectionDAG;
618   ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
619     : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
620 public:
621
622   unsigned getIndex() const { return CPI; }
623
624   static bool classof(const ConstantPoolSDNode *) { return true; }
625   static bool classof(const SDNode *N) {
626     return N->getOpcode() == ISD::ConstantPool;
627   }
628 };
629
630 class BasicBlockSDNode : public SDNode {
631   MachineBasicBlock *MBB;
632 protected:
633   friend class SelectionDAG;
634   BasicBlockSDNode(MachineBasicBlock *mbb)
635     : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
636 public:
637
638   MachineBasicBlock *getBasicBlock() const { return MBB; }
639
640   static bool classof(const BasicBlockSDNode *) { return true; }
641   static bool classof(const SDNode *N) {
642     return N->getOpcode() == ISD::BasicBlock;
643   }
644 };
645
646
647 class RegSDNode : public SDNode {
648   unsigned Reg;
649 protected:
650   friend class SelectionDAG;
651   RegSDNode(unsigned Opc, MVT::ValueType VT, SDOperand Chain,
652             SDOperand Src, unsigned reg)
653     : SDNode(Opc, Chain, Src), Reg(reg) {
654     setValueTypes(VT);
655   }
656   RegSDNode(unsigned Opc, MVT::ValueType VT, SDOperand Chain,
657             unsigned reg)
658     : SDNode(Opc, Chain), Reg(reg) {
659     setValueTypes(VT);
660   }
661   RegSDNode(unsigned Opc, MVT::ValueType VT, unsigned reg)
662     : SDNode(Opc, VT), Reg(reg) {
663   }
664 public:
665
666   unsigned getReg() const { return Reg; }
667
668   static bool classof(const RegSDNode *) { return true; }
669   static bool classof(const SDNode *N) {
670     return N->getOpcode() == ISD::CopyToReg ||
671            N->getOpcode() == ISD::CopyFromReg ||
672            N->getOpcode() == ISD::ImplicitDef;
673   }
674 };
675
676 class ExternalSymbolSDNode : public SDNode {
677   const char *Symbol;
678 protected:
679   friend class SelectionDAG;
680   ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
681     : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
682     }
683 public:
684
685   const char *getSymbol() const { return Symbol; }
686
687   static bool classof(const ExternalSymbolSDNode *) { return true; }
688   static bool classof(const SDNode *N) {
689     return N->getOpcode() == ISD::ExternalSymbol;
690   }
691 };
692
693 class SetCCSDNode : public SDNode {
694   ISD::CondCode Condition;
695 protected:
696   friend class SelectionDAG;
697   SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
698     : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
699     setValueTypes(MVT::i1);
700   }
701 public:
702
703   ISD::CondCode getCondition() const { return Condition; }
704
705   static bool classof(const SetCCSDNode *) { return true; }
706   static bool classof(const SDNode *N) {
707     return N->getOpcode() == ISD::SETCC;
708   }
709 };
710
711 /// MVTSDNode - This class is used for operators that require an extra
712 /// value-type to be kept with the node.
713 class MVTSDNode : public SDNode {
714   MVT::ValueType ExtraValueType;
715 protected:
716   friend class SelectionDAG;
717   MVTSDNode(unsigned Opc, MVT::ValueType VT,
718             SDOperand Op0, SDOperand Op1, MVT::ValueType EVT)
719     : SDNode(Opc, Op0, Op1), ExtraValueType(EVT) {
720     setValueTypes(VT);
721   }
722   MVTSDNode(unsigned Opc, MVT::ValueType VT,
723             SDOperand Op0, SDOperand Op1, SDOperand Op2, MVT::ValueType EVT)
724     : SDNode(Opc, Op0, Op1, Op2), ExtraValueType(EVT) {
725     setValueTypes(VT);
726   }
727 public:
728
729   MVT::ValueType getExtraValueType() const { return ExtraValueType; }
730
731   static bool classof(const MVTSDNode *) { return true; }
732   static bool classof(const SDNode *N) {
733     return 
734       N->getOpcode() == ISD::EXTLOAD  ||
735       N->getOpcode() == ISD::SEXTLOAD || 
736       N->getOpcode() == ISD::ZEXTLOAD ||
737       N->getOpcode() == ISD::TRUNCSTORE;
738   }
739 };
740
741 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
742   SDNode *Node;
743   unsigned Operand;
744   
745   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
746 public:
747   bool operator==(const SDNodeIterator& x) const {
748     return Operand == x.Operand;
749   }
750   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
751
752   const SDNodeIterator &operator=(const SDNodeIterator &I) {
753     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
754     Operand = I.Operand;
755     return *this;
756   }
757   
758   pointer operator*() const {
759     return Node->getOperand(Operand).Val;
760   }
761   pointer operator->() const { return operator*(); }
762   
763   SDNodeIterator& operator++() {                // Preincrement
764     ++Operand;
765     return *this;
766   }
767   SDNodeIterator operator++(int) { // Postincrement
768     SDNodeIterator tmp = *this; ++*this; return tmp; 
769   }
770
771   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
772   static SDNodeIterator end  (SDNode *N) {
773     return SDNodeIterator(N, N->getNumOperands());
774   }
775
776   unsigned getOperand() const { return Operand; }
777   const SDNode *getNode() const { return Node; }
778 };
779
780 template <> struct GraphTraits<SDNode*> {
781   typedef SDNode NodeType;
782   typedef SDNodeIterator ChildIteratorType;
783   static inline NodeType *getEntryNode(SDNode *N) { return N; }
784   static inline ChildIteratorType child_begin(NodeType *N) { 
785     return SDNodeIterator::begin(N);
786   }
787   static inline ChildIteratorType child_end(NodeType *N) { 
788     return SDNodeIterator::end(N);
789   }
790 };
791
792
793
794
795 } // end llvm namespace
796
797 #endif