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