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