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