add some nodes for extractelement
[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/Value.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 template <typename T> struct ilist_traits;
38 template<typename NodeTy, typename Traits> class iplist;
39 template<typename NodeTy> class ilist_iterator;
40
41 /// ISD namespace - This namespace contains an enum which represents all of the
42 /// SelectionDAG node types and value types.
43 ///
44 namespace ISD {
45   //===--------------------------------------------------------------------===//
46   /// ISD::NodeType enum - This enum defines all of the operators valid in a
47   /// SelectionDAG.
48   ///
49   enum NodeType {
50     // EntryToken - This is the marker used to indicate the start of the region.
51     EntryToken,
52
53     // Token factor - This node takes multiple tokens as input and produces a
54     // single token result.  This is used to represent the fact that the operand
55     // operators are independent of each other.
56     TokenFactor,
57     
58     // AssertSext, AssertZext - These nodes record if a register contains a 
59     // value that has already been zero or sign extended from a narrower type.  
60     // These nodes take two operands.  The first is the node that has already 
61     // been extended, and the second is a value type node indicating the width
62     // of the extension
63     AssertSext, AssertZext,
64
65     // Various leaf nodes.
66     STRING, BasicBlock, VALUETYPE, CONDCODE, Register,
67     Constant, ConstantFP,
68     GlobalAddress, FrameIndex, ConstantPool, ExternalSymbol,
69
70     // TargetConstant* - Like Constant*, but the DAG does not do any folding or
71     // simplification of the constant.
72     TargetConstant,
73     TargetConstantFP,
74     
75     // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
76     // anything else with this node, and this is valid in the target-specific
77     // dag, turning into a GlobalAddress operand.
78     TargetGlobalAddress,
79     TargetFrameIndex,
80     TargetConstantPool,
81     TargetExternalSymbol,
82
83     // CopyToReg - This node has three operands: a chain, a register number to
84     // set to this value, and a value.  
85     CopyToReg,
86
87     // CopyFromReg - This node indicates that the input value is a virtual or
88     // physical register that is defined outside of the scope of this
89     // SelectionDAG.  The register is available from the RegSDNode object.
90     CopyFromReg,
91
92     // UNDEF - An undefined node
93     UNDEF,
94
95     // EXTRACT_ELEMENT - This is used to get the first or second (determined by
96     // a Constant, which is required to be operand #1), element of the aggregate
97     // value specified as operand #0.  This is only for use before legalization,
98     // for values that will be broken into multiple registers.
99     EXTRACT_ELEMENT,
100
101     // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
102     // two values of the same integer value type, this produces a value twice as
103     // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
104     BUILD_PAIR,
105     
106     // MERGE_VALUES - This node takes multiple discrete operands and returns
107     // them all as its individual results.  This nodes has exactly the same
108     // number of inputs and outputs, and is only valid before legalization.
109     // This node is useful for some pieces of the code generator that want to
110     // think about a single node with multiple results, not multiple nodes.
111     MERGE_VALUES,
112
113     // Simple integer binary arithmetic operators.
114     ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
115     
116     // Carry-setting nodes for multiple precision addition and subtraction.
117     // These nodes take two operands of the same value type, and produce two
118     // results.  The first result is the normal add or sub result, the second
119     // result is the carry flag result.
120     ADDC, SUBC,
121     
122     // Carry-using nodes for multiple precision addition and subtraction.  These
123     // nodes take three operands: The first two are the normal lhs and rhs to
124     // the add or sub, and the third is the input carry flag.  These nodes
125     // produce two results; the normal result of the add or sub, and the output
126     // carry flag.  These nodes both read and write a carry flag to allow them
127     // to them to be chained together for add and sub of arbitrarily large
128     // values.
129     ADDE, SUBE,
130     
131     // Simple binary floating point operators.
132     FADD, FSUB, FMUL, FDIV, FREM,
133
134     // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.  NOTE: This
135     // DAG node does not require that X and Y have the same type, just that they
136     // are both floating point.  X and the result must have the same type.
137     // FCOPYSIGN(f32, f64) is allowed.
138     FCOPYSIGN,
139
140     /// VBUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...,  COUNT,TYPE) - Return a vector
141     /// with the specified, possibly variable, elements.  The number of elements
142     /// is required to be a power of two.
143     VBUILD_VECTOR,
144
145     /// BUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...) - Return a vector
146     /// with the specified, possibly variable, elements.  The number of elements
147     /// is required to be a power of two.
148     BUILD_VECTOR,
149     
150     /// VINSERT_VECTOR_ELT(VECTOR, VAL, IDX,  COUNT,TYPE) - Given a vector
151     /// VECTOR, an element ELEMENT, and a (potentially variable) index IDX,
152     /// return an vector with the specified element of VECTOR replaced with VAL.
153     /// COUNT and TYPE specify the type of vector, as is standard for V* nodes.
154     VINSERT_VECTOR_ELT,
155     
156     /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR (a legal packed
157     /// type) with the element at IDX replaced with VAL.
158     INSERT_VECTOR_ELT,
159
160     /// VEXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
161     /// (an MVT::Vector value) identified by the (potentially variable) element
162     /// number IDX.
163     VEXTRACT_VECTOR_ELT,
164     
165     /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
166     /// (a legal packed type vector) identified by the (potentially variable)
167     /// element number IDX.
168     EXTRACT_VECTOR_ELT,
169     
170     /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
171     /// type as VEC1/VEC2.  SHUFFLEVEC is a BUILD_VECTOR of constant int values
172     /// (regardless of whether its datatype is legal or not) that indicate
173     /// which value each result element will get.  The elements of VEC1/VEC2 are
174     /// enumerated in order.  This is quite similar to the Altivec 'vperm'
175     /// instruction, except that the indices must be constants and are in terms
176     /// of the element size of VEC1/VEC2, not in terms of bytes.
177     VECTOR_SHUFFLE,
178     
179     /// BINOP(LHS, RHS,  COUNT,TYPE)
180     /// Simple abstract vector operators.  Unlike the integer and floating point
181     /// binary operators, these nodes also take two additional operands:
182     /// a constant element count, and a value type node indicating the type of
183     /// the elements.  The order is count, type, op0, op1.  All vector opcodes,
184     /// including VLOAD and VConstant must currently have count and type as
185     /// their last two operands.
186     VADD, VSUB, VMUL, VSDIV, VUDIV,
187     VAND, VOR, VXOR,
188     
189     /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
190     /// scalar value into the low element of the resultant vector type.  The top
191     /// elements of the vector are undefined.
192     SCALAR_TO_VECTOR,
193     
194     // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
195     // an unsigned/signed value of type i[2*n], then return the top part.
196     MULHU, MULHS,
197
198     // Bitwise operators - logical and, logical or, logical xor, shift left,
199     // shift right algebraic (shift in sign bits), shift right logical (shift in
200     // zeroes), rotate left, rotate right, and byteswap.
201     AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
202
203     // Counting operators
204     CTTZ, CTLZ, CTPOP,
205
206     // Select(COND, TRUEVAL, FALSEVAL)
207     SELECT, 
208     
209     // Select with condition operator - This selects between a true value and 
210     // a false value (ops #2 and #3) based on the boolean result of comparing
211     // the lhs and rhs (ops #0 and #1) of a conditional expression with the 
212     // condition code in op #4, a CondCodeSDNode.
213     SELECT_CC,
214
215     // SetCC operator - This evaluates to a boolean (i1) true value if the
216     // condition is true.  The operands to this are the left and right operands
217     // to compare (ops #0, and #1) and the condition code to compare them with
218     // (op #2) as a CondCodeSDNode.
219     SETCC,
220
221     // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
222     // integer shift operations, just like ADD/SUB_PARTS.  The operation
223     // ordering is:
224     //       [Lo,Hi] = op [LoLHS,HiLHS], Amt
225     SHL_PARTS, SRA_PARTS, SRL_PARTS,
226
227     // Conversion operators.  These are all single input single output
228     // operations.  For all of these, the result type must be strictly
229     // wider or narrower (depending on the operation) than the source
230     // type.
231
232     // SIGN_EXTEND - Used for integer types, replicating the sign bit
233     // into new bits.
234     SIGN_EXTEND,
235
236     // ZERO_EXTEND - Used for integer types, zeroing the new bits.
237     ZERO_EXTEND,
238
239     // ANY_EXTEND - Used for integer types.  The high bits are undefined.
240     ANY_EXTEND,
241     
242     // TRUNCATE - Completely drop the high bits.
243     TRUNCATE,
244
245     // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
246     // depends on the first letter) to floating point.
247     SINT_TO_FP,
248     UINT_TO_FP,
249
250     // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
251     // sign extend a small value in a large integer register (e.g. sign
252     // extending the low 8 bits of a 32-bit register to fill the top 24 bits
253     // with the 7th bit).  The size of the smaller type is indicated by the 1th
254     // operand, a ValueType node.
255     SIGN_EXTEND_INREG,
256
257     // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
258     // integer.
259     FP_TO_SINT,
260     FP_TO_UINT,
261
262     // FP_ROUND - Perform a rounding operation from the current
263     // precision down to the specified precision (currently always 64->32).
264     FP_ROUND,
265
266     // FP_ROUND_INREG - This operator takes a floating point register, and
267     // rounds it to a floating point value.  It then promotes it and returns it
268     // in a register of the same size.  This operation effectively just discards
269     // excess precision.  The type to round down to is specified by the 1th
270     // operation, a VTSDNode (currently always 64->32->64).
271     FP_ROUND_INREG,
272
273     // FP_EXTEND - Extend a smaller FP type into a larger FP type.
274     FP_EXTEND,
275
276     // BIT_CONVERT - Theis operator converts between integer and FP values, as
277     // if one was stored to memory as integer and the other was loaded from the
278     // same address (or equivalently for vector format conversions, etc).  The 
279     // source and result are required to have the same bit size (e.g. 
280     // f32 <-> i32).  This can also be used for int-to-int or fp-to-fp 
281     // conversions, but that is a noop, deleted by getNode().
282     BIT_CONVERT,
283     
284     // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
285     // absolute value, square root, sine and cosine operations.
286     FNEG, FABS, FSQRT, FSIN, FCOS,
287     
288     // Other operators.  LOAD and STORE have token chains as their first
289     // operand, then the same operands as an LLVM load/store instruction, then a
290     // SRCVALUE node that provides alias analysis information.
291     LOAD, STORE,
292     
293     // Abstract vector version of LOAD.  VLOAD has a constant element count as
294     // the first operand, followed by a value type node indicating the type of
295     // the elements, a token chain, a pointer operand, and a SRCVALUE node.
296     VLOAD,
297
298     // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
299     // memory and extend them to a larger value (e.g. load a byte into a word
300     // register).  All three of these have four operands, a token chain, a
301     // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
302     // indicating the type to load.
303     //
304     // SEXTLOAD loads the integer operand and sign extends it to a larger
305     //          integer result type.
306     // ZEXTLOAD loads the integer operand and zero extends it to a larger
307     //          integer result type.
308     // EXTLOAD  is used for three things: floating point extending loads, 
309     //          integer extending loads [the top bits are undefined], and vector
310     //          extending loads [load into low elt].
311     EXTLOAD, SEXTLOAD, ZEXTLOAD,
312
313     // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
314     // value and stores it to memory in one operation.  This can be used for
315     // either integer or floating point operands.  The first four operands of
316     // this are the same as a standard store.  The fifth is the ValueType to
317     // store it as (which will be smaller than the source value).
318     TRUNCSTORE,
319
320     // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
321     // to a specified boundary.  The first operand is the token chain, the
322     // second is the number of bytes to allocate, and the third is the alignment
323     // boundary.  The size is guaranteed to be a multiple of the stack 
324     // alignment, and the alignment is guaranteed to be bigger than the stack 
325     // alignment (if required) or 0 to get standard stack alignment.
326     DYNAMIC_STACKALLOC,
327
328     // Control flow instructions.  These all have token chains.
329
330     // BR - Unconditional branch.  The first operand is the chain
331     // operand, the second is the MBB to branch to.
332     BR,
333
334     // BRCOND - Conditional branch.  The first operand is the chain,
335     // the second is the condition, the third is the block to branch
336     // to if the condition is true.
337     BRCOND,
338
339     // BR_CC - Conditional branch.  The behavior is like that of SELECT_CC, in
340     // that the condition is represented as condition code, and two nodes to
341     // compare, rather than as a combined SetCC node.  The operands in order are
342     // chain, cc, lhs, rhs, block to branch to if condition is true.
343     BR_CC,
344     
345     // RET - Return from function.  The first operand is the chain,
346     // and any subsequent operands are the return values for the
347     // function.  This operation can have variable number of operands.
348     RET,
349
350     // INLINEASM - Represents an inline asm block.  This node always has two
351     // return values: a chain and a flag result.  The inputs are as follows:
352     //   Operand #0   : Input chain.
353     //   Operand #1   : a ExternalSymbolSDNode with a pointer to the asm string.
354     //   Operand #2n+2: A RegisterNode.
355     //   Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
356     //   Operand #last: Optional, an incoming flag.
357     INLINEASM,
358
359     // STACKSAVE - STACKSAVE has one operand, an input chain.  It produces a
360     // value, the same type as the pointer type for the system, and an output
361     // chain.
362     STACKSAVE,
363     
364     // STACKRESTORE has two operands, an input chain and a pointer to restore to
365     // it returns an output chain.
366     STACKRESTORE,
367     
368     // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
369     // correspond to the operands of the LLVM intrinsic functions.  The only
370     // result is a token chain.  The alignment argument is guaranteed to be a
371     // Constant node.
372     MEMSET,
373     MEMMOVE,
374     MEMCPY,
375
376     // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
377     // a call sequence, and carry arbitrary information that target might want
378     // to know.  The first operand is a chain, the rest are specified by the
379     // target and not touched by the DAG optimizers.
380     CALLSEQ_START,  // Beginning of a call sequence
381     CALLSEQ_END,    // End of a call sequence
382     
383     // VAARG - VAARG has three operands: an input chain, a pointer, and a 
384     // SRCVALUE.  It returns a pair of values: the vaarg value and a new chain.
385     VAARG,
386     
387     // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
388     // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
389     // source.
390     VACOPY,
391     
392     // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
393     // pointer, and a SRCVALUE.
394     VAEND, VASTART,
395
396     // SRCVALUE - This corresponds to a Value*, and is used to associate memory
397     // locations with their value.  This allows one use alias analysis
398     // information in the backend.
399     SRCVALUE,
400
401     // PCMARKER - This corresponds to the pcmarker intrinsic.
402     PCMARKER,
403
404     // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
405     // The only operand is a chain and a value and a chain are produced.  The
406     // value is the contents of the architecture specific cycle counter like 
407     // register (or other high accuracy low latency clock source)
408     READCYCLECOUNTER,
409
410     // HANDLENODE node - Used as a handle for various purposes.
411     HANDLENODE,
412
413     // LOCATION - This node is used to represent a source location for debug
414     // info.  It takes token chain as input, then a line number, then a column
415     // number, then a filename, then a working dir.  It produces a token chain
416     // as output.
417     LOCATION,
418     
419     // DEBUG_LOC - This node is used to represent source line information
420     // embedded in the code.  It takes a token chain as input, then a line
421     // number, then a column then a file id (provided by MachineDebugInfo.) It
422     // produces a token chain as output.
423     DEBUG_LOC,
424     
425     // DEBUG_LABEL - This node is used to mark a location in the code where a
426     // label should be generated for use by the debug information.  It takes a
427     // token chain as input and then a unique id (provided by MachineDebugInfo.)
428     // It produces a token chain as output.
429     DEBUG_LABEL,
430     
431     // BUILTIN_OP_END - This must be the last enum value in this list.
432     BUILTIN_OP_END
433   };
434
435   //===--------------------------------------------------------------------===//
436   /// ISD::CondCode enum - These are ordered carefully to make the bitfields
437   /// below work out, when considering SETFALSE (something that never exists
438   /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
439   /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
440   /// to.  If the "N" column is 1, the result of the comparison is undefined if
441   /// the input is a NAN.
442   ///
443   /// All of these (except for the 'always folded ops') should be handled for
444   /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
445   /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
446   ///
447   /// Note that these are laid out in a specific order to allow bit-twiddling
448   /// to transform conditions.
449   enum CondCode {
450     // Opcode          N U L G E       Intuitive operation
451     SETFALSE,      //    0 0 0 0       Always false (always folded)
452     SETOEQ,        //    0 0 0 1       True if ordered and equal
453     SETOGT,        //    0 0 1 0       True if ordered and greater than
454     SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
455     SETOLT,        //    0 1 0 0       True if ordered and less than
456     SETOLE,        //    0 1 0 1       True if ordered and less than or equal
457     SETONE,        //    0 1 1 0       True if ordered and operands are unequal
458     SETO,          //    0 1 1 1       True if ordered (no nans)
459     SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
460     SETUEQ,        //    1 0 0 1       True if unordered or equal
461     SETUGT,        //    1 0 1 0       True if unordered or greater than
462     SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
463     SETULT,        //    1 1 0 0       True if unordered or less than
464     SETULE,        //    1 1 0 1       True if unordered, less than, or equal
465     SETUNE,        //    1 1 1 0       True if unordered or not equal
466     SETTRUE,       //    1 1 1 1       Always true (always folded)
467     // Don't care operations: undefined if the input is a nan.
468     SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
469     SETEQ,         //  1 X 0 0 1       True if equal
470     SETGT,         //  1 X 0 1 0       True if greater than
471     SETGE,         //  1 X 0 1 1       True if greater than or equal
472     SETLT,         //  1 X 1 0 0       True if less than
473     SETLE,         //  1 X 1 0 1       True if less than or equal
474     SETNE,         //  1 X 1 1 0       True if not equal
475     SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
476
477     SETCC_INVALID       // Marker value.
478   };
479
480   /// isSignedIntSetCC - Return true if this is a setcc instruction that
481   /// performs a signed comparison when used with integer operands.
482   inline bool isSignedIntSetCC(CondCode Code) {
483     return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
484   }
485
486   /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
487   /// performs an unsigned comparison when used with integer operands.
488   inline bool isUnsignedIntSetCC(CondCode Code) {
489     return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
490   }
491
492   /// isTrueWhenEqual - Return true if the specified condition returns true if
493   /// the two operands to the condition are equal.  Note that if one of the two
494   /// operands is a NaN, this value is meaningless.
495   inline bool isTrueWhenEqual(CondCode Cond) {
496     return ((int)Cond & 1) != 0;
497   }
498
499   /// getUnorderedFlavor - This function returns 0 if the condition is always
500   /// false if an operand is a NaN, 1 if the condition is always true if the
501   /// operand is a NaN, and 2 if the condition is undefined if the operand is a
502   /// NaN.
503   inline unsigned getUnorderedFlavor(CondCode Cond) {
504     return ((int)Cond >> 3) & 3;
505   }
506
507   /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
508   /// 'op' is a valid SetCC operation.
509   CondCode getSetCCInverse(CondCode Operation, bool isInteger);
510
511   /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
512   /// when given the operation for (X op Y).
513   CondCode getSetCCSwappedOperands(CondCode Operation);
514
515   /// getSetCCOrOperation - Return the result of a logical OR between different
516   /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
517   /// function returns SETCC_INVALID if it is not possible to represent the
518   /// resultant comparison.
519   CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
520
521   /// getSetCCAndOperation - Return the result of a logical AND between
522   /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
523   /// function returns SETCC_INVALID if it is not possible to represent the
524   /// resultant comparison.
525   CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
526 }  // end llvm::ISD namespace
527
528
529 //===----------------------------------------------------------------------===//
530 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
531 /// values as the result of a computation.  Many nodes return multiple values,
532 /// from loads (which define a token and a return value) to ADDC (which returns
533 /// a result and a carry value), to calls (which may return an arbitrary number
534 /// of values).
535 ///
536 /// As such, each use of a SelectionDAG computation must indicate the node that
537 /// computes it as well as which return value to use from that node.  This pair
538 /// of information is represented with the SDOperand value type.
539 ///
540 class SDOperand {
541 public:
542   SDNode *Val;        // The node defining the value we are using.
543   unsigned ResNo;     // Which return value of the node we are using.
544
545   SDOperand() : Val(0) {}
546   SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
547
548   bool operator==(const SDOperand &O) const {
549     return Val == O.Val && ResNo == O.ResNo;
550   }
551   bool operator!=(const SDOperand &O) const {
552     return !operator==(O);
553   }
554   bool operator<(const SDOperand &O) const {
555     return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
556   }
557
558   SDOperand getValue(unsigned R) const {
559     return SDOperand(Val, R);
560   }
561
562   // isOperand - Return true if this node is an operand of N.
563   bool isOperand(SDNode *N) const;
564
565   /// getValueType - Return the ValueType of the referenced return value.
566   ///
567   inline MVT::ValueType getValueType() const;
568
569   // Forwarding methods - These forward to the corresponding methods in SDNode.
570   inline unsigned getOpcode() const;
571   inline unsigned getNodeDepth() const;
572   inline unsigned getNumOperands() const;
573   inline const SDOperand &getOperand(unsigned i) const;
574   inline bool isTargetOpcode() const;
575   inline unsigned getTargetOpcode() const;
576
577   /// hasOneUse - Return true if there is exactly one operation using this
578   /// result value of the defining operator.
579   inline bool hasOneUse() const;
580 };
581
582
583 /// simplify_type specializations - Allow casting operators to work directly on
584 /// SDOperands as if they were SDNode*'s.
585 template<> struct simplify_type<SDOperand> {
586   typedef SDNode* SimpleType;
587   static SimpleType getSimplifiedValue(const SDOperand &Val) {
588     return static_cast<SimpleType>(Val.Val);
589   }
590 };
591 template<> struct simplify_type<const SDOperand> {
592   typedef SDNode* SimpleType;
593   static SimpleType getSimplifiedValue(const SDOperand &Val) {
594     return static_cast<SimpleType>(Val.Val);
595   }
596 };
597
598
599 /// SDNode - Represents one node in the SelectionDAG.
600 ///
601 class SDNode {
602   /// NodeType - The operation that this node performs.
603   ///
604   unsigned short NodeType;
605
606   /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1.  This
607   /// means that leaves have a depth of 1, things that use only leaves have a
608   /// depth of 2, etc.
609   unsigned short NodeDepth;
610
611   /// OperandList - The values that are used by this operation.
612   ///
613   SDOperand *OperandList;
614   
615   /// ValueList - The types of the values this node defines.  SDNode's may
616   /// define multiple values simultaneously.
617   MVT::ValueType *ValueList;
618
619   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
620   unsigned short NumOperands, NumValues;
621   
622   /// Prev/Next pointers - These pointers form the linked list of of the
623   /// AllNodes list in the current DAG.
624   SDNode *Prev, *Next;
625   friend struct ilist_traits<SDNode>;
626
627   /// Uses - These are all of the SDNode's that use a value produced by this
628   /// node.
629   std::vector<SDNode*> Uses;
630 public:
631   virtual ~SDNode() {
632     assert(NumOperands == 0 && "Operand list not cleared before deletion");
633   }
634   
635   //===--------------------------------------------------------------------===//
636   //  Accessors
637   //
638   unsigned getOpcode()  const { return NodeType; }
639   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
640   unsigned getTargetOpcode() const {
641     assert(isTargetOpcode() && "Not a target opcode!");
642     return NodeType - ISD::BUILTIN_OP_END;
643   }
644
645   size_t use_size() const { return Uses.size(); }
646   bool use_empty() const { return Uses.empty(); }
647   bool hasOneUse() const { return Uses.size() == 1; }
648
649   /// getNodeDepth - Return the distance from this node to the leaves in the
650   /// graph.  The leaves have a depth of 1.
651   unsigned getNodeDepth() const { return NodeDepth; }
652
653   typedef std::vector<SDNode*>::const_iterator use_iterator;
654   use_iterator use_begin() const { return Uses.begin(); }
655   use_iterator use_end() const { return Uses.end(); }
656
657   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
658   /// indicated value.  This method ignores uses of other values defined by this
659   /// operation.
660   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
661
662   // isOnlyUse - Return true if this node is the only use of N.
663   bool isOnlyUse(SDNode *N) const;
664
665   // isOperand - Return true if this node is an operand of N.
666   bool isOperand(SDNode *N) const;
667
668   /// getNumOperands - Return the number of values used by this operation.
669   ///
670   unsigned getNumOperands() const { return NumOperands; }
671
672   const SDOperand &getOperand(unsigned Num) const {
673     assert(Num < NumOperands && "Invalid child # of SDNode!");
674     return OperandList[Num];
675   }
676   typedef const SDOperand* op_iterator;
677   op_iterator op_begin() const { return OperandList; }
678   op_iterator op_end() const { return OperandList+NumOperands; }
679
680
681   /// getNumValues - Return the number of values defined/returned by this
682   /// operator.
683   ///
684   unsigned getNumValues() const { return NumValues; }
685
686   /// getValueType - Return the type of a specified result.
687   ///
688   MVT::ValueType getValueType(unsigned ResNo) const {
689     assert(ResNo < NumValues && "Illegal result number!");
690     return ValueList[ResNo];
691   }
692
693   typedef const MVT::ValueType* value_iterator;
694   value_iterator value_begin() const { return ValueList; }
695   value_iterator value_end() const { return ValueList+NumValues; }
696
697   /// getOperationName - Return the opcode of this operation for printing.
698   ///
699   const char* getOperationName(const SelectionDAG *G = 0) const;
700   void dump() const;
701   void dump(const SelectionDAG *G) const;
702
703   static bool classof(const SDNode *) { return true; }
704
705 protected:
706   friend class SelectionDAG;
707   
708   /// getValueTypeList - Return a pointer to the specified value type.
709   ///
710   static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
711
712   SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
713     OperandList = 0; NumOperands = 0;
714     ValueList = getValueTypeList(VT);
715     NumValues = 1;
716     Prev = 0; Next = 0;
717   }
718   SDNode(unsigned NT, SDOperand Op)
719     : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
720     OperandList = new SDOperand[1];
721     OperandList[0] = Op;
722     NumOperands = 1;
723     Op.Val->Uses.push_back(this);
724     ValueList = 0;
725     NumValues = 0;
726     Prev = 0; Next = 0;
727   }
728   SDNode(unsigned NT, SDOperand N1, SDOperand N2)
729     : NodeType(NT) {
730     if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
731       NodeDepth = N1.Val->getNodeDepth()+1;
732     else
733       NodeDepth = N2.Val->getNodeDepth()+1;
734     OperandList = new SDOperand[2];
735     OperandList[0] = N1;
736     OperandList[1] = N2;
737     NumOperands = 2;
738     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
739     ValueList = 0;
740     NumValues = 0;
741     Prev = 0; Next = 0;
742   }
743   SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
744     : NodeType(NT) {
745     unsigned ND = N1.Val->getNodeDepth();
746     if (ND < N2.Val->getNodeDepth())
747       ND = N2.Val->getNodeDepth();
748     if (ND < N3.Val->getNodeDepth())
749       ND = N3.Val->getNodeDepth();
750     NodeDepth = ND+1;
751
752     OperandList = new SDOperand[3];
753     OperandList[0] = N1;
754     OperandList[1] = N2;
755     OperandList[2] = N3;
756     NumOperands = 3;
757     
758     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
759     N3.Val->Uses.push_back(this);
760     ValueList = 0;
761     NumValues = 0;
762     Prev = 0; Next = 0;
763   }
764   SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
765     : NodeType(NT) {
766     unsigned ND = N1.Val->getNodeDepth();
767     if (ND < N2.Val->getNodeDepth())
768       ND = N2.Val->getNodeDepth();
769     if (ND < N3.Val->getNodeDepth())
770       ND = N3.Val->getNodeDepth();
771     if (ND < N4.Val->getNodeDepth())
772       ND = N4.Val->getNodeDepth();
773     NodeDepth = ND+1;
774
775     OperandList = new SDOperand[4];
776     OperandList[0] = N1;
777     OperandList[1] = N2;
778     OperandList[2] = N3;
779     OperandList[3] = N4;
780     NumOperands = 4;
781     
782     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
783     N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
784     ValueList = 0;
785     NumValues = 0;
786     Prev = 0; Next = 0;
787   }
788   SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
789     NumOperands = Nodes.size();
790     OperandList = new SDOperand[NumOperands];
791     
792     unsigned ND = 0;
793     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
794       OperandList[i] = Nodes[i];
795       SDNode *N = OperandList[i].Val;
796       N->Uses.push_back(this);
797       if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
798     }
799     NodeDepth = ND+1;
800     ValueList = 0;
801     NumValues = 0;
802     Prev = 0; Next = 0;
803   }
804
805   /// MorphNodeTo - This clears the return value and operands list, and sets the
806   /// opcode of the node to the specified value.  This should only be used by
807   /// the SelectionDAG class.
808   void MorphNodeTo(unsigned Opc) {
809     NodeType = Opc;
810     ValueList = 0;
811     NumValues = 0;
812     
813     // Clear the operands list, updating used nodes to remove this from their
814     // use list.
815     for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
816       I->Val->removeUser(this);
817     delete [] OperandList;
818     OperandList = 0;
819     NumOperands = 0;
820   }
821   
822   void setValueTypes(MVT::ValueType VT) {
823     assert(NumValues == 0 && "Should not have values yet!");
824     ValueList = getValueTypeList(VT);
825     NumValues = 1;
826   }
827   void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
828     assert(NumValues == 0 && "Should not have values yet!");
829     ValueList = List;
830     NumValues = NumVal;
831   }
832   
833   void setOperands(SDOperand Op0) {
834     assert(NumOperands == 0 && "Should not have operands yet!");
835     OperandList = new SDOperand[1];
836     OperandList[0] = Op0;
837     NumOperands = 1;
838     Op0.Val->Uses.push_back(this);
839   }
840   void setOperands(SDOperand Op0, SDOperand Op1) {
841     assert(NumOperands == 0 && "Should not have operands yet!");
842     OperandList = new SDOperand[2];
843     OperandList[0] = Op0;
844     OperandList[1] = Op1;
845     NumOperands = 2;
846     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
847   }
848   void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
849     assert(NumOperands == 0 && "Should not have operands yet!");
850     OperandList = new SDOperand[3];
851     OperandList[0] = Op0;
852     OperandList[1] = Op1;
853     OperandList[2] = Op2;
854     NumOperands = 3;
855     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
856     Op2.Val->Uses.push_back(this);
857   }
858   void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
859     assert(NumOperands == 0 && "Should not have operands yet!");
860     OperandList = new SDOperand[4];
861     OperandList[0] = Op0;
862     OperandList[1] = Op1;
863     OperandList[2] = Op2;
864     OperandList[3] = Op3;
865     NumOperands = 4;
866     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
867     Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
868   }
869   void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
870                    SDOperand Op4) {
871     assert(NumOperands == 0 && "Should not have operands yet!");
872     OperandList = new SDOperand[5];
873     OperandList[0] = Op0;
874     OperandList[1] = Op1;
875     OperandList[2] = Op2;
876     OperandList[3] = Op3;
877     OperandList[4] = Op4;
878     NumOperands = 5;
879     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
880     Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
881     Op4.Val->Uses.push_back(this);
882   }
883   void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
884                    SDOperand Op4, SDOperand Op5) {
885     assert(NumOperands == 0 && "Should not have operands yet!");
886     OperandList = new SDOperand[6];
887     OperandList[0] = Op0;
888     OperandList[1] = Op1;
889     OperandList[2] = Op2;
890     OperandList[3] = Op3;
891     OperandList[4] = Op4;
892     OperandList[5] = Op5;
893     NumOperands = 6;
894     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
895     Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
896     Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
897   }
898   void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
899                    SDOperand Op4, SDOperand Op5, SDOperand Op6) {
900     assert(NumOperands == 0 && "Should not have operands yet!");
901     OperandList = new SDOperand[7];
902     OperandList[0] = Op0;
903     OperandList[1] = Op1;
904     OperandList[2] = Op2;
905     OperandList[3] = Op3;
906     OperandList[4] = Op4;
907     OperandList[5] = Op5;
908     OperandList[6] = Op6;
909     NumOperands = 7;
910     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
911     Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
912     Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
913     Op6.Val->Uses.push_back(this);
914   }
915   void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
916                    SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
917     assert(NumOperands == 0 && "Should not have operands yet!");
918     OperandList = new SDOperand[8];
919     OperandList[0] = Op0;
920     OperandList[1] = Op1;
921     OperandList[2] = Op2;
922     OperandList[3] = Op3;
923     OperandList[4] = Op4;
924     OperandList[5] = Op5;
925     OperandList[6] = Op6;
926     OperandList[7] = Op7;
927     NumOperands = 8;
928     Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
929     Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
930     Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
931     Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
932   }
933
934   void addUser(SDNode *User) {
935     Uses.push_back(User);
936   }
937   void removeUser(SDNode *User) {
938     // Remove this user from the operand's use list.
939     for (unsigned i = Uses.size(); ; --i) {
940       assert(i != 0 && "Didn't find user!");
941       if (Uses[i-1] == User) {
942         Uses[i-1] = Uses.back();
943         Uses.pop_back();
944         return;
945       }
946     }
947   }
948 };
949
950
951 // Define inline functions from the SDOperand class.
952
953 inline unsigned SDOperand::getOpcode() const {
954   return Val->getOpcode();
955 }
956 inline unsigned SDOperand::getNodeDepth() const {
957   return Val->getNodeDepth();
958 }
959 inline MVT::ValueType SDOperand::getValueType() const {
960   return Val->getValueType(ResNo);
961 }
962 inline unsigned SDOperand::getNumOperands() const {
963   return Val->getNumOperands();
964 }
965 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
966   return Val->getOperand(i);
967 }
968 inline bool SDOperand::isTargetOpcode() const {
969   return Val->isTargetOpcode();
970 }
971 inline unsigned SDOperand::getTargetOpcode() const {
972   return Val->getTargetOpcode();
973 }
974 inline bool SDOperand::hasOneUse() const {
975   return Val->hasNUsesOfValue(1, ResNo);
976 }
977
978 /// HandleSDNode - This class is used to form a handle around another node that
979 /// is persistant and is updated across invocations of replaceAllUsesWith on its
980 /// operand.  This node should be directly created by end-users and not added to
981 /// the AllNodes list.
982 class HandleSDNode : public SDNode {
983 public:
984   HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
985   ~HandleSDNode() {
986     MorphNodeTo(ISD::HANDLENODE);  // Drops operand uses.
987   }
988   
989   SDOperand getValue() const { return getOperand(0); }
990 };
991
992 class StringSDNode : public SDNode {
993   std::string Value;
994 protected:
995   friend class SelectionDAG;
996   StringSDNode(const std::string &val)
997     : SDNode(ISD::STRING, MVT::Other), Value(val) {
998   }
999 public:
1000   const std::string &getValue() const { return Value; }
1001   static bool classof(const StringSDNode *) { return true; }
1002   static bool classof(const SDNode *N) {
1003     return N->getOpcode() == ISD::STRING;
1004   }
1005 };  
1006
1007 class ConstantSDNode : public SDNode {
1008   uint64_t Value;
1009 protected:
1010   friend class SelectionDAG;
1011   ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1012     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
1013   }
1014 public:
1015
1016   uint64_t getValue() const { return Value; }
1017
1018   int64_t getSignExtended() const {
1019     unsigned Bits = MVT::getSizeInBits(getValueType(0));
1020     return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1021   }
1022
1023   bool isNullValue() const { return Value == 0; }
1024   bool isAllOnesValue() const {
1025     int NumBits = MVT::getSizeInBits(getValueType(0));
1026     if (NumBits == 64) return Value+1 == 0;
1027     return Value == (1ULL << NumBits)-1;
1028   }
1029
1030   static bool classof(const ConstantSDNode *) { return true; }
1031   static bool classof(const SDNode *N) {
1032     return N->getOpcode() == ISD::Constant ||
1033            N->getOpcode() == ISD::TargetConstant;
1034   }
1035 };
1036
1037 class ConstantFPSDNode : public SDNode {
1038   double Value;
1039 protected:
1040   friend class SelectionDAG;
1041   ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1042     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT), 
1043       Value(val) {
1044   }
1045 public:
1046
1047   double getValue() const { return Value; }
1048
1049   /// isExactlyValue - We don't rely on operator== working on double values, as
1050   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1051   /// As such, this method can be used to do an exact bit-for-bit comparison of
1052   /// two floating point values.
1053   bool isExactlyValue(double V) const;
1054
1055   static bool classof(const ConstantFPSDNode *) { return true; }
1056   static bool classof(const SDNode *N) {
1057     return N->getOpcode() == ISD::ConstantFP || 
1058            N->getOpcode() == ISD::TargetConstantFP;
1059   }
1060 };
1061
1062 class GlobalAddressSDNode : public SDNode {
1063   GlobalValue *TheGlobal;
1064   int Offset;
1065 protected:
1066   friend class SelectionDAG;
1067   GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1068                       int o=0)
1069     : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1070       Offset(o) {
1071     TheGlobal = const_cast<GlobalValue*>(GA);
1072   }
1073 public:
1074
1075   GlobalValue *getGlobal() const { return TheGlobal; }
1076   int getOffset() const { return Offset; }
1077
1078   static bool classof(const GlobalAddressSDNode *) { return true; }
1079   static bool classof(const SDNode *N) {
1080     return N->getOpcode() == ISD::GlobalAddress ||
1081            N->getOpcode() == ISD::TargetGlobalAddress;
1082   }
1083 };
1084
1085
1086 class FrameIndexSDNode : public SDNode {
1087   int FI;
1088 protected:
1089   friend class SelectionDAG;
1090   FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1091     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1092 public:
1093
1094   int getIndex() const { return FI; }
1095
1096   static bool classof(const FrameIndexSDNode *) { return true; }
1097   static bool classof(const SDNode *N) {
1098     return N->getOpcode() == ISD::FrameIndex ||
1099            N->getOpcode() == ISD::TargetFrameIndex;
1100   }
1101 };
1102
1103 class ConstantPoolSDNode : public SDNode {
1104   Constant *C;
1105   int Offset;
1106   unsigned Alignment;
1107 protected:
1108   friend class SelectionDAG;
1109   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1110                      int o=0)
1111     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1112       C(c), Offset(o), Alignment(0) {}
1113   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1114                      unsigned Align)
1115     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1116       C(c), Offset(o), Alignment(Align) {}
1117 public:
1118
1119   Constant *get() const { return C; }
1120   int getOffset() const { return Offset; }
1121   
1122   // Return the alignment of this constant pool object, which is either 0 (for
1123   // default alignment) or log2 of the desired value.
1124   unsigned getAlignment() const { return Alignment; }
1125
1126   static bool classof(const ConstantPoolSDNode *) { return true; }
1127   static bool classof(const SDNode *N) {
1128     return N->getOpcode() == ISD::ConstantPool ||
1129            N->getOpcode() == ISD::TargetConstantPool;
1130   }
1131 };
1132
1133 class BasicBlockSDNode : public SDNode {
1134   MachineBasicBlock *MBB;
1135 protected:
1136   friend class SelectionDAG;
1137   BasicBlockSDNode(MachineBasicBlock *mbb)
1138     : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1139 public:
1140
1141   MachineBasicBlock *getBasicBlock() const { return MBB; }
1142
1143   static bool classof(const BasicBlockSDNode *) { return true; }
1144   static bool classof(const SDNode *N) {
1145     return N->getOpcode() == ISD::BasicBlock;
1146   }
1147 };
1148
1149 class SrcValueSDNode : public SDNode {
1150   const Value *V;
1151   int offset;
1152 protected:
1153   friend class SelectionDAG;
1154   SrcValueSDNode(const Value* v, int o)
1155     : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1156
1157 public:
1158   const Value *getValue() const { return V; }
1159   int getOffset() const { return offset; }
1160
1161   static bool classof(const SrcValueSDNode *) { return true; }
1162   static bool classof(const SDNode *N) {
1163     return N->getOpcode() == ISD::SRCVALUE;
1164   }
1165 };
1166
1167
1168 class RegisterSDNode : public SDNode {
1169   unsigned Reg;
1170 protected:
1171   friend class SelectionDAG;
1172   RegisterSDNode(unsigned reg, MVT::ValueType VT)
1173     : SDNode(ISD::Register, VT), Reg(reg) {}
1174 public:
1175
1176   unsigned getReg() const { return Reg; }
1177
1178   static bool classof(const RegisterSDNode *) { return true; }
1179   static bool classof(const SDNode *N) {
1180     return N->getOpcode() == ISD::Register;
1181   }
1182 };
1183
1184 class ExternalSymbolSDNode : public SDNode {
1185   const char *Symbol;
1186 protected:
1187   friend class SelectionDAG;
1188   ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1189     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1190       Symbol(Sym) {
1191     }
1192 public:
1193
1194   const char *getSymbol() const { return Symbol; }
1195
1196   static bool classof(const ExternalSymbolSDNode *) { return true; }
1197   static bool classof(const SDNode *N) {
1198     return N->getOpcode() == ISD::ExternalSymbol ||
1199            N->getOpcode() == ISD::TargetExternalSymbol;
1200   }
1201 };
1202
1203 class CondCodeSDNode : public SDNode {
1204   ISD::CondCode Condition;
1205 protected:
1206   friend class SelectionDAG;
1207   CondCodeSDNode(ISD::CondCode Cond)
1208     : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1209   }
1210 public:
1211
1212   ISD::CondCode get() const { return Condition; }
1213
1214   static bool classof(const CondCodeSDNode *) { return true; }
1215   static bool classof(const SDNode *N) {
1216     return N->getOpcode() == ISD::CONDCODE;
1217   }
1218 };
1219
1220 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1221 /// to parameterize some operations.
1222 class VTSDNode : public SDNode {
1223   MVT::ValueType ValueType;
1224 protected:
1225   friend class SelectionDAG;
1226   VTSDNode(MVT::ValueType VT)
1227     : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1228 public:
1229
1230   MVT::ValueType getVT() const { return ValueType; }
1231
1232   static bool classof(const VTSDNode *) { return true; }
1233   static bool classof(const SDNode *N) {
1234     return N->getOpcode() == ISD::VALUETYPE;
1235   }
1236 };
1237
1238
1239 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1240   SDNode *Node;
1241   unsigned Operand;
1242
1243   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1244 public:
1245   bool operator==(const SDNodeIterator& x) const {
1246     return Operand == x.Operand;
1247   }
1248   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1249
1250   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1251     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1252     Operand = I.Operand;
1253     return *this;
1254   }
1255
1256   pointer operator*() const {
1257     return Node->getOperand(Operand).Val;
1258   }
1259   pointer operator->() const { return operator*(); }
1260
1261   SDNodeIterator& operator++() {                // Preincrement
1262     ++Operand;
1263     return *this;
1264   }
1265   SDNodeIterator operator++(int) { // Postincrement
1266     SDNodeIterator tmp = *this; ++*this; return tmp;
1267   }
1268
1269   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1270   static SDNodeIterator end  (SDNode *N) {
1271     return SDNodeIterator(N, N->getNumOperands());
1272   }
1273
1274   unsigned getOperand() const { return Operand; }
1275   const SDNode *getNode() const { return Node; }
1276 };
1277
1278 template <> struct GraphTraits<SDNode*> {
1279   typedef SDNode NodeType;
1280   typedef SDNodeIterator ChildIteratorType;
1281   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1282   static inline ChildIteratorType child_begin(NodeType *N) {
1283     return SDNodeIterator::begin(N);
1284   }
1285   static inline ChildIteratorType child_end(NodeType *N) {
1286     return SDNodeIterator::end(N);
1287   }
1288 };
1289
1290 template<>
1291 struct ilist_traits<SDNode> {
1292   static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1293   static SDNode *getNext(const SDNode *N) { return N->Next; }
1294   
1295   static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1296   static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1297   
1298   static SDNode *createSentinel() {
1299     return new SDNode(ISD::EntryToken, MVT::Other);
1300   }
1301   static void destroySentinel(SDNode *N) { delete N; }
1302   //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1303   
1304   
1305   void addNodeToList(SDNode *NTy) {}
1306   void removeNodeFromList(SDNode *NTy) {}
1307   void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1308                              const ilist_iterator<SDNode> &X,
1309                              const ilist_iterator<SDNode> &Y) {}
1310 };
1311
1312 } // end llvm namespace
1313
1314 #endif