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