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