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