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