9068d7809962d4090878cbeb63c86628b7b9915d
[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/FoldingSet.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/iterator"
26 #include "llvm/ADT/APFloat.h"
27 #include "llvm/CodeGen/ValueTypes.h"
28 #include "llvm/Support/DataTypes.h"
29 #include <cassert>
30
31 namespace llvm {
32
33 class SelectionDAG;
34 class GlobalValue;
35 class MachineBasicBlock;
36 class MachineConstantPoolValue;
37 class SDNode;
38 template <typename T> struct DenseMapKeyInfo;
39 template <typename T> struct simplify_type;
40 template <typename T> struct ilist_traits;
41 template<typename NodeTy, typename Traits> class iplist;
42 template<typename NodeTy> class ilist_iterator;
43
44 /// SDVTList - This represents a list of ValueType's that has been intern'd by
45 /// a SelectionDAG.  Instances of this simple value class are returned by
46 /// SelectionDAG::getVTList(...).
47 ///
48 struct SDVTList {
49   const MVT::ValueType *VTs;
50   unsigned short NumVTs;
51 };
52
53 /// ISD namespace - This namespace contains an enum which represents all of the
54 /// SelectionDAG node types and value types.
55 ///
56 namespace ISD {
57   namespace ParamFlags {    
58   enum Flags {
59     NoFlagSet         = 0,
60     ZExt              = 1<<0,  ///< Parameter should be zero extended
61     ZExtOffs          = 0,
62     SExt              = 1<<1,  ///< Parameter should be sign extended
63     SExtOffs          = 1,
64     InReg             = 1<<2,  ///< Parameter should be passed in register
65     InRegOffs         = 2,
66     StructReturn      = 1<<3,  ///< Hidden struct-return pointer
67     StructReturnOffs  = 3,
68     ByVal             = 1<<4,  ///< Struct passed by value
69     ByValOffs         = 4,
70     Nest              = 1<<5,  ///< Parameter is nested function static chain
71     NestOffs          = 5,
72     ByValAlign        = 0xF << 6, //< The alignment of the struct
73     ByValAlignOffs    = 6,
74     ByValSize         = 0x1ffff << 10, //< The size of the struct
75     ByValSizeOffs     = 10,
76     OrigAlignment     = 0x1F<<27,
77     OrigAlignmentOffs = 27
78   };
79   }
80
81   //===--------------------------------------------------------------------===//
82   /// ISD::NodeType enum - This enum defines all of the operators valid in a
83   /// SelectionDAG.
84   ///
85   enum NodeType {
86     // DELETED_NODE - This is an illegal flag value that is used to catch
87     // errors.  This opcode is not a legal opcode for any node.
88     DELETED_NODE,
89     
90     // EntryToken - This is the marker used to indicate the start of the region.
91     EntryToken,
92
93     // Token factor - This node takes multiple tokens as input and produces a
94     // single token result.  This is used to represent the fact that the operand
95     // operators are independent of each other.
96     TokenFactor,
97     
98     // AssertSext, AssertZext - These nodes record if a register contains a 
99     // value that has already been zero or sign extended from a narrower type.  
100     // These nodes take two operands.  The first is the node that has already 
101     // been extended, and the second is a value type node indicating the width
102     // of the extension
103     AssertSext, AssertZext,
104
105     // Various leaf nodes.
106     STRING, BasicBlock, VALUETYPE, CONDCODE, Register,
107     Constant, ConstantFP,
108     GlobalAddress, GlobalTLSAddress, FrameIndex,
109     JumpTable, ConstantPool, ExternalSymbol,
110
111     // The address of the GOT
112     GLOBAL_OFFSET_TABLE,
113     
114     // FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and
115     // llvm.returnaddress on the DAG.  These nodes take one operand, the index
116     // of the frame or return address to return.  An index of zero corresponds
117     // to the current function's frame or return address, an index of one to the
118     // parent's frame or return address, and so on.
119     FRAMEADDR, RETURNADDR,
120
121     // FRAME_TO_ARGS_OFFSET - This node represents offset from frame pointer to
122     // first (possible) on-stack argument. This is needed for correct stack
123     // adjustment during unwind.
124     FRAME_TO_ARGS_OFFSET,
125     
126     // RESULT, OUTCHAIN = EXCEPTIONADDR(INCHAIN) - This node represents the
127     // address of the exception block on entry to an landing pad block.
128     EXCEPTIONADDR,
129     
130     // RESULT, OUTCHAIN = EHSELECTION(INCHAIN, EXCEPTION) - This node represents
131     // the selection index of the exception thrown.
132     EHSELECTION,
133
134     // OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents
135     // 'eh_return' gcc dwarf builtin, which is used to return from
136     // exception. The general meaning is: adjust stack by OFFSET and pass
137     // execution to HANDLER. Many platform-related details also :)
138     EH_RETURN,
139
140     // TargetConstant* - Like Constant*, but the DAG does not do any folding or
141     // simplification of the constant.
142     TargetConstant,
143     TargetConstantFP,
144     
145     // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
146     // anything else with this node, and this is valid in the target-specific
147     // dag, turning into a GlobalAddress operand.
148     TargetGlobalAddress,
149     TargetGlobalTLSAddress,
150     TargetFrameIndex,
151     TargetJumpTable,
152     TargetConstantPool,
153     TargetExternalSymbol,
154     
155     /// RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...)
156     /// This node represents a target intrinsic function with no side effects.
157     /// The first operand is the ID number of the intrinsic from the
158     /// llvm::Intrinsic namespace.  The operands to the intrinsic follow.  The
159     /// node has returns the result of the intrinsic.
160     INTRINSIC_WO_CHAIN,
161     
162     /// RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...)
163     /// This node represents a target intrinsic function with side effects that
164     /// returns a result.  The first operand is a chain pointer.  The second is
165     /// the ID number of the intrinsic from the llvm::Intrinsic namespace.  The
166     /// operands to the intrinsic follow.  The node has two results, the result
167     /// of the intrinsic and an output chain.
168     INTRINSIC_W_CHAIN,
169
170     /// OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...)
171     /// This node represents a target intrinsic function with side effects that
172     /// does not return a result.  The first operand is a chain pointer.  The
173     /// second is the ID number of the intrinsic from the llvm::Intrinsic
174     /// namespace.  The operands to the intrinsic follow.
175     INTRINSIC_VOID,
176     
177     // CopyToReg - This node has three operands: a chain, a register number to
178     // set to this value, and a value.  
179     CopyToReg,
180
181     // CopyFromReg - This node indicates that the input value is a virtual or
182     // physical register that is defined outside of the scope of this
183     // SelectionDAG.  The register is available from the RegSDNode object.
184     CopyFromReg,
185
186     // UNDEF - An undefined node
187     UNDEF,
188     
189     /// FORMAL_ARGUMENTS(CHAIN, CC#, ISVARARG, FLAG0, ..., FLAGn) - This node
190     /// represents the formal arguments for a function.  CC# is a Constant value
191     /// indicating the calling convention of the function, and ISVARARG is a
192     /// flag that indicates whether the function is varargs or not. This node
193     /// has one result value for each incoming argument, plus one for the output
194     /// chain. It must be custom legalized. See description of CALL node for
195     /// FLAG argument contents explanation.
196     /// 
197     FORMAL_ARGUMENTS,
198     
199     /// RV1, RV2...RVn, CHAIN = CALL(CHAIN, CC#, ISVARARG, ISTAILCALL, CALLEE,
200     ///                              ARG0, FLAG0, ARG1, FLAG1, ... ARGn, FLAGn)
201     /// This node represents a fully general function call, before the legalizer
202     /// runs.  This has one result value for each argument / flag pair, plus
203     /// a chain result. It must be custom legalized. Flag argument indicates
204     /// misc. argument attributes. Currently:
205     /// Bit 0 - signness
206     /// Bit 1 - 'inreg' attribute
207     /// Bit 2 - 'sret' attribute
208     /// Bit 4 - 'byval' attribute
209     /// Bit 5 - 'nest' attribute
210     /// Bit 6-9 - alignment of byval structures
211     /// Bit 10-26 - size of byval structures
212     /// Bits 31:27 - argument ABI alignment in the first argument piece and
213     /// alignment '1' in other argument pieces.
214     CALL,
215
216     // EXTRACT_ELEMENT - This is used to get the first or second (determined by
217     // a Constant, which is required to be operand #1), element of the aggregate
218     // value specified as operand #0.  This is only for use before legalization,
219     // for values that will be broken into multiple registers.
220     EXTRACT_ELEMENT,
221
222     // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
223     // two values of the same integer value type, this produces a value twice as
224     // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
225     BUILD_PAIR,
226     
227     // MERGE_VALUES - This node takes multiple discrete operands and returns
228     // them all as its individual results.  This nodes has exactly the same
229     // number of inputs and outputs, and is only valid before legalization.
230     // This node is useful for some pieces of the code generator that want to
231     // think about a single node with multiple results, not multiple nodes.
232     MERGE_VALUES,
233
234     // Simple integer binary arithmetic operators.
235     ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
236     
237     // CARRY_FALSE - This node is used when folding other nodes,
238     // like ADDC/SUBC, which indicate the carry result is always false.
239     CARRY_FALSE,
240     
241     // Carry-setting nodes for multiple precision addition and subtraction.
242     // These nodes take two operands of the same value type, and produce two
243     // results.  The first result is the normal add or sub result, the second
244     // result is the carry flag result.
245     ADDC, SUBC,
246     
247     // Carry-using nodes for multiple precision addition and subtraction.  These
248     // nodes take three operands: The first two are the normal lhs and rhs to
249     // the add or sub, and the third is the input carry flag.  These nodes
250     // produce two results; the normal result of the add or sub, and the output
251     // carry flag.  These nodes both read and write a carry flag to allow them
252     // to them to be chained together for add and sub of arbitrarily large
253     // values.
254     ADDE, SUBE,
255     
256     // Simple binary floating point operators.
257     FADD, FSUB, FMUL, FDIV, FREM,
258
259     // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.  NOTE: This
260     // DAG node does not require that X and Y have the same type, just that they
261     // are both floating point.  X and the result must have the same type.
262     // FCOPYSIGN(f32, f64) is allowed.
263     FCOPYSIGN,
264
265     /// BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector
266     /// with the specified, possibly variable, elements.  The number of elements
267     /// is required to be a power of two.
268     BUILD_VECTOR,
269     
270     /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element
271     /// at IDX replaced with VAL.
272     INSERT_VECTOR_ELT,
273
274     /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
275     /// identified by the (potentially variable) element number IDX.
276     EXTRACT_VECTOR_ELT,
277     
278     /// CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of
279     /// vector type with the same length and element type, this produces a
280     /// concatenated vector result value, with length equal to the sum of the
281     /// lengths of the input vectors.
282     CONCAT_VECTORS,
283     
284     /// EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an
285     /// vector value) starting with the (potentially variable) element number
286     /// IDX, which must be a multiple of the result vector length.
287     EXTRACT_SUBVECTOR,
288     
289     /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
290     /// type as VEC1/VEC2.  SHUFFLEVEC is a BUILD_VECTOR of constant int values
291     /// (regardless of whether its datatype is legal or not) that indicate
292     /// which value each result element will get.  The elements of VEC1/VEC2 are
293     /// enumerated in order.  This is quite similar to the Altivec 'vperm'
294     /// instruction, except that the indices must be constants and are in terms
295     /// of the element size of VEC1/VEC2, not in terms of bytes.
296     VECTOR_SHUFFLE,
297     
298     /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
299     /// scalar value into the low element of the resultant vector type.  The top
300     /// elements of the vector are undefined.
301     SCALAR_TO_VECTOR,
302     
303     // EXTRACT_SUBREG - This node is used to extract a sub-register value. 
304     // This node takes a superreg and a constant sub-register index as operands.
305     EXTRACT_SUBREG,
306     
307     // INSERT_SUBREG - This node is used to insert a sub-register value. 
308     // This node takes a superreg, a subreg value, and a constant sub-register
309     // index as operands.
310     INSERT_SUBREG,
311     
312     // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
313     // an unsigned/signed value of type i[2*n], then return the top part.
314     MULHU, MULHS,
315
316     // Bitwise operators - logical and, logical or, logical xor, shift left,
317     // shift right algebraic (shift in sign bits), shift right logical (shift in
318     // zeroes), rotate left, rotate right, and byteswap.
319     AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
320
321     // Counting operators
322     CTTZ, CTLZ, CTPOP,
323
324     // Select(COND, TRUEVAL, FALSEVAL)
325     SELECT, 
326     
327     // Select with condition operator - This selects between a true value and 
328     // a false value (ops #2 and #3) based on the boolean result of comparing
329     // the lhs and rhs (ops #0 and #1) of a conditional expression with the 
330     // condition code in op #4, a CondCodeSDNode.
331     SELECT_CC,
332
333     // SetCC operator - This evaluates to a boolean (i1) true value if the
334     // condition is true.  The operands to this are the left and right operands
335     // to compare (ops #0, and #1) and the condition code to compare them with
336     // (op #2) as a CondCodeSDNode.
337     SETCC,
338
339     // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
340     // integer shift operations, just like ADD/SUB_PARTS.  The operation
341     // ordering is:
342     //       [Lo,Hi] = op [LoLHS,HiLHS], Amt
343     SHL_PARTS, SRA_PARTS, SRL_PARTS,
344
345     // Conversion operators.  These are all single input single output
346     // operations.  For all of these, the result type must be strictly
347     // wider or narrower (depending on the operation) than the source
348     // type.
349
350     // SIGN_EXTEND - Used for integer types, replicating the sign bit
351     // into new bits.
352     SIGN_EXTEND,
353
354     // ZERO_EXTEND - Used for integer types, zeroing the new bits.
355     ZERO_EXTEND,
356
357     // ANY_EXTEND - Used for integer types.  The high bits are undefined.
358     ANY_EXTEND,
359     
360     // TRUNCATE - Completely drop the high bits.
361     TRUNCATE,
362
363     // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
364     // depends on the first letter) to floating point.
365     SINT_TO_FP,
366     UINT_TO_FP,
367
368     // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
369     // sign extend a small value in a large integer register (e.g. sign
370     // extending the low 8 bits of a 32-bit register to fill the top 24 bits
371     // with the 7th bit).  The size of the smaller type is indicated by the 1th
372     // operand, a ValueType node.
373     SIGN_EXTEND_INREG,
374
375     // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
376     // integer.
377     FP_TO_SINT,
378     FP_TO_UINT,
379
380     // FP_ROUND - Perform a rounding operation from the current
381     // precision down to the specified precision (currently always 64->32).
382     FP_ROUND,
383
384     // FP_ROUND_INREG - This operator takes a floating point register, and
385     // rounds it to a floating point value.  It then promotes it and returns it
386     // in a register of the same size.  This operation effectively just discards
387     // excess precision.  The type to round down to is specified by the 1th
388     // operation, a VTSDNode (currently always 64->32->64).
389     FP_ROUND_INREG,
390
391     // FP_EXTEND - Extend a smaller FP type into a larger FP type.
392     FP_EXTEND,
393
394     // BIT_CONVERT - Theis operator converts between integer and FP values, as
395     // if one was stored to memory as integer and the other was loaded from the
396     // same address (or equivalently for vector format conversions, etc).  The 
397     // source and result are required to have the same bit size (e.g. 
398     // f32 <-> i32).  This can also be used for int-to-int or fp-to-fp 
399     // conversions, but that is a noop, deleted by getNode().
400     BIT_CONVERT,
401     
402     // FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI - Perform unary floating point
403     // negation, absolute value, square root, sine and cosine, and powi
404     // operations.
405     FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI,
406     
407     // LOAD and STORE have token chains as their first operand, then the same
408     // operands as an LLVM load/store instruction, then an offset node that
409     // is added / subtracted from the base pointer to form the address (for
410     // indexed memory ops).
411     LOAD, STORE,
412     
413     // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
414     // value and stores it to memory in one operation.  This can be used for
415     // either integer or floating point operands.  The first four operands of
416     // this are the same as a standard store.  The fifth is the ValueType to
417     // store it as (which will be smaller than the source value).
418     TRUNCSTORE,
419
420     // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
421     // to a specified boundary.  This node always has two return values: a new
422     // stack pointer value and a chain. The first operand is the token chain,
423     // the second is the number of bytes to allocate, and the third is the
424     // alignment boundary.  The size is guaranteed to be a multiple of the stack
425     // alignment, and the alignment is guaranteed to be bigger than the stack
426     // alignment (if required) or 0 to get standard stack alignment.
427     DYNAMIC_STACKALLOC,
428
429     // Control flow instructions.  These all have token chains.
430
431     // BR - Unconditional branch.  The first operand is the chain
432     // operand, the second is the MBB to branch to.
433     BR,
434
435     // BRIND - Indirect branch.  The first operand is the chain, the second
436     // is the value to branch to, which must be of the same type as the target's
437     // pointer type.
438     BRIND,
439
440     // BR_JT - Jumptable branch. The first operand is the chain, the second
441     // is the jumptable index, the last one is the jumptable entry index.
442     BR_JT,
443     
444     // BRCOND - Conditional branch.  The first operand is the chain,
445     // the second is the condition, the third is the block to branch
446     // to if the condition is true.
447     BRCOND,
448
449     // BR_CC - Conditional branch.  The behavior is like that of SELECT_CC, in
450     // that the condition is represented as condition code, and two nodes to
451     // compare, rather than as a combined SetCC node.  The operands in order are
452     // chain, cc, lhs, rhs, block to branch to if condition is true.
453     BR_CC,
454     
455     // RET - Return from function.  The first operand is the chain,
456     // and any subsequent operands are pairs of return value and return value
457     // signness for the function.  This operation can have variable number of
458     // operands.
459     RET,
460
461     // INLINEASM - Represents an inline asm block.  This node always has two
462     // return values: a chain and a flag result.  The inputs are as follows:
463     //   Operand #0   : Input chain.
464     //   Operand #1   : a ExternalSymbolSDNode with a pointer to the asm string.
465     //   Operand #2n+2: A RegisterNode.
466     //   Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
467     //   Operand #last: Optional, an incoming flag.
468     INLINEASM,
469     
470     // LABEL - Represents a label in mid basic block used to track
471     // locations needed for debug and exception handling tables.  This node
472     // returns a chain.
473     //   Operand #0 : input chain.
474     //   Operand #1 : module unique number use to identify the label.
475     LABEL,
476     
477     // STACKSAVE - STACKSAVE has one operand, an input chain.  It produces a
478     // value, the same type as the pointer type for the system, and an output
479     // chain.
480     STACKSAVE,
481     
482     // STACKRESTORE has two operands, an input chain and a pointer to restore to
483     // it returns an output chain.
484     STACKRESTORE,
485     
486     // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
487     // correspond to the operands of the LLVM intrinsic functions.  The only
488     // result is a token chain.  The alignment argument is guaranteed to be a
489     // Constant node.
490     MEMSET,
491     MEMMOVE,
492     MEMCPY,
493
494     // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
495     // a call sequence, and carry arbitrary information that target might want
496     // to know.  The first operand is a chain, the rest are specified by the
497     // target and not touched by the DAG optimizers.
498     CALLSEQ_START,  // Beginning of a call sequence
499     CALLSEQ_END,    // End of a call sequence
500     
501     // VAARG - VAARG has three operands: an input chain, a pointer, and a 
502     // SRCVALUE.  It returns a pair of values: the vaarg value and a new chain.
503     VAARG,
504     
505     // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
506     // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
507     // source.
508     VACOPY,
509     
510     // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
511     // pointer, and a SRCVALUE.
512     VAEND, VASTART,
513
514     // SRCVALUE - This corresponds to a Value*, and is used to associate memory
515     // locations with their value.  This allows one use alias analysis
516     // information in the backend.
517     SRCVALUE,
518
519     // PCMARKER - This corresponds to the pcmarker intrinsic.
520     PCMARKER,
521
522     // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
523     // The only operand is a chain and a value and a chain are produced.  The
524     // value is the contents of the architecture specific cycle counter like 
525     // register (or other high accuracy low latency clock source)
526     READCYCLECOUNTER,
527
528     // HANDLENODE node - Used as a handle for various purposes.
529     HANDLENODE,
530
531     // LOCATION - This node is used to represent a source location for debug
532     // info.  It takes token chain as input, then a line number, then a column
533     // number, then a filename, then a working dir.  It produces a token chain
534     // as output.
535     LOCATION,
536     
537     // DEBUG_LOC - This node is used to represent source line information
538     // embedded in the code.  It takes a token chain as input, then a line
539     // number, then a column then a file id (provided by MachineModuleInfo.) It
540     // produces a token chain as output.
541     DEBUG_LOC,
542
543     // ADJUST_TRAMP - This corresponds to the adjust_trampoline intrinsic.
544     // It takes a value as input and returns a value as output.
545     ADJUST_TRAMP,
546
547     // TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
548     // It takes as input a token chain, the pointer to the trampoline,
549     // the pointer to the nested function, the pointer to pass for the
550     // 'nest' parameter, a SRCVALUE for the trampoline and another for
551     // the nested function (allowing targets to access the original
552     // Function*).  It produces a token chain as output.
553     TRAMPOLINE,
554
555     // BUILTIN_OP_END - This must be the last enum value in this list.
556     BUILTIN_OP_END
557   };
558
559   /// Node predicates
560
561   /// isBuildVectorAllOnes - Return true if the specified node is a
562   /// BUILD_VECTOR where all of the elements are ~0 or undef.
563   bool isBuildVectorAllOnes(const SDNode *N);
564
565   /// isBuildVectorAllZeros - Return true if the specified node is a
566   /// BUILD_VECTOR where all of the elements are 0 or undef.
567   bool isBuildVectorAllZeros(const SDNode *N);
568   
569   //===--------------------------------------------------------------------===//
570   /// MemIndexedMode enum - This enum defines the load / store indexed 
571   /// addressing modes.
572   ///
573   /// UNINDEXED    "Normal" load / store. The effective address is already
574   ///              computed and is available in the base pointer. The offset
575   ///              operand is always undefined. In addition to producing a
576   ///              chain, an unindexed load produces one value (result of the
577   ///              load); an unindexed store does not produces a value.
578   ///
579   /// PRE_INC      Similar to the unindexed mode where the effective address is
580   /// PRE_DEC      the value of the base pointer add / subtract the offset.
581   ///              It considers the computation as being folded into the load /
582   ///              store operation (i.e. the load / store does the address
583   ///              computation as well as performing the memory transaction).
584   ///              The base operand is always undefined. In addition to
585   ///              producing a chain, pre-indexed load produces two values
586   ///              (result of the load and the result of the address
587   ///              computation); a pre-indexed store produces one value (result
588   ///              of the address computation).
589   ///
590   /// POST_INC     The effective address is the value of the base pointer. The
591   /// POST_DEC     value of the offset operand is then added to / subtracted
592   ///              from the base after memory transaction. In addition to
593   ///              producing a chain, post-indexed load produces two values
594   ///              (the result of the load and the result of the base +/- offset
595   ///              computation); a post-indexed store produces one value (the
596   ///              the result of the base +/- offset computation).
597   ///
598   enum MemIndexedMode {
599     UNINDEXED = 0,
600     PRE_INC,
601     PRE_DEC,
602     POST_INC,
603     POST_DEC,
604     LAST_INDEXED_MODE
605   };
606
607   //===--------------------------------------------------------------------===//
608   /// LoadExtType enum - This enum defines the three variants of LOADEXT
609   /// (load with extension).
610   ///
611   /// SEXTLOAD loads the integer operand and sign extends it to a larger
612   ///          integer result type.
613   /// ZEXTLOAD loads the integer operand and zero extends it to a larger
614   ///          integer result type.
615   /// EXTLOAD  is used for three things: floating point extending loads, 
616   ///          integer extending loads [the top bits are undefined], and vector
617   ///          extending loads [load into low elt].
618   ///
619   enum LoadExtType {
620     NON_EXTLOAD = 0,
621     EXTLOAD,
622     SEXTLOAD,
623     ZEXTLOAD,
624     LAST_LOADX_TYPE
625   };
626
627   //===--------------------------------------------------------------------===//
628   /// ISD::CondCode enum - These are ordered carefully to make the bitfields
629   /// below work out, when considering SETFALSE (something that never exists
630   /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
631   /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
632   /// to.  If the "N" column is 1, the result of the comparison is undefined if
633   /// the input is a NAN.
634   ///
635   /// All of these (except for the 'always folded ops') should be handled for
636   /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
637   /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
638   ///
639   /// Note that these are laid out in a specific order to allow bit-twiddling
640   /// to transform conditions.
641   enum CondCode {
642     // Opcode          N U L G E       Intuitive operation
643     SETFALSE,      //    0 0 0 0       Always false (always folded)
644     SETOEQ,        //    0 0 0 1       True if ordered and equal
645     SETOGT,        //    0 0 1 0       True if ordered and greater than
646     SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
647     SETOLT,        //    0 1 0 0       True if ordered and less than
648     SETOLE,        //    0 1 0 1       True if ordered and less than or equal
649     SETONE,        //    0 1 1 0       True if ordered and operands are unequal
650     SETO,          //    0 1 1 1       True if ordered (no nans)
651     SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
652     SETUEQ,        //    1 0 0 1       True if unordered or equal
653     SETUGT,        //    1 0 1 0       True if unordered or greater than
654     SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
655     SETULT,        //    1 1 0 0       True if unordered or less than
656     SETULE,        //    1 1 0 1       True if unordered, less than, or equal
657     SETUNE,        //    1 1 1 0       True if unordered or not equal
658     SETTRUE,       //    1 1 1 1       Always true (always folded)
659     // Don't care operations: undefined if the input is a nan.
660     SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
661     SETEQ,         //  1 X 0 0 1       True if equal
662     SETGT,         //  1 X 0 1 0       True if greater than
663     SETGE,         //  1 X 0 1 1       True if greater than or equal
664     SETLT,         //  1 X 1 0 0       True if less than
665     SETLE,         //  1 X 1 0 1       True if less than or equal
666     SETNE,         //  1 X 1 1 0       True if not equal
667     SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
668
669     SETCC_INVALID       // Marker value.
670   };
671
672   /// isSignedIntSetCC - Return true if this is a setcc instruction that
673   /// performs a signed comparison when used with integer operands.
674   inline bool isSignedIntSetCC(CondCode Code) {
675     return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
676   }
677
678   /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
679   /// performs an unsigned comparison when used with integer operands.
680   inline bool isUnsignedIntSetCC(CondCode Code) {
681     return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
682   }
683
684   /// isTrueWhenEqual - Return true if the specified condition returns true if
685   /// the two operands to the condition are equal.  Note that if one of the two
686   /// operands is a NaN, this value is meaningless.
687   inline bool isTrueWhenEqual(CondCode Cond) {
688     return ((int)Cond & 1) != 0;
689   }
690
691   /// getUnorderedFlavor - This function returns 0 if the condition is always
692   /// false if an operand is a NaN, 1 if the condition is always true if the
693   /// operand is a NaN, and 2 if the condition is undefined if the operand is a
694   /// NaN.
695   inline unsigned getUnorderedFlavor(CondCode Cond) {
696     return ((int)Cond >> 3) & 3;
697   }
698
699   /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
700   /// 'op' is a valid SetCC operation.
701   CondCode getSetCCInverse(CondCode Operation, bool isInteger);
702
703   /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
704   /// when given the operation for (X op Y).
705   CondCode getSetCCSwappedOperands(CondCode Operation);
706
707   /// getSetCCOrOperation - Return the result of a logical OR between different
708   /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
709   /// function returns SETCC_INVALID if it is not possible to represent the
710   /// resultant comparison.
711   CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
712
713   /// getSetCCAndOperation - Return the result of a logical AND between
714   /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
715   /// function returns SETCC_INVALID if it is not possible to represent the
716   /// resultant comparison.
717   CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
718 }  // end llvm::ISD namespace
719
720
721 //===----------------------------------------------------------------------===//
722 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
723 /// values as the result of a computation.  Many nodes return multiple values,
724 /// from loads (which define a token and a return value) to ADDC (which returns
725 /// a result and a carry value), to calls (which may return an arbitrary number
726 /// of values).
727 ///
728 /// As such, each use of a SelectionDAG computation must indicate the node that
729 /// computes it as well as which return value to use from that node.  This pair
730 /// of information is represented with the SDOperand value type.
731 ///
732 class SDOperand {
733 public:
734   SDNode *Val;        // The node defining the value we are using.
735   unsigned ResNo;     // Which return value of the node we are using.
736
737   SDOperand() : Val(0), ResNo(0) {}
738   SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
739
740   bool operator==(const SDOperand &O) const {
741     return Val == O.Val && ResNo == O.ResNo;
742   }
743   bool operator!=(const SDOperand &O) const {
744     return !operator==(O);
745   }
746   bool operator<(const SDOperand &O) const {
747     return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
748   }
749
750   SDOperand getValue(unsigned R) const {
751     return SDOperand(Val, R);
752   }
753
754   // isOperand - Return true if this node is an operand of N.
755   bool isOperand(SDNode *N) const;
756
757   /// getValueType - Return the ValueType of the referenced return value.
758   ///
759   inline MVT::ValueType getValueType() const;
760
761   // Forwarding methods - These forward to the corresponding methods in SDNode.
762   inline unsigned getOpcode() const;
763   inline unsigned getNumOperands() const;
764   inline const SDOperand &getOperand(unsigned i) const;
765   inline uint64_t getConstantOperandVal(unsigned i) const;
766   inline bool isTargetOpcode() const;
767   inline unsigned getTargetOpcode() const;
768
769   /// hasOneUse - Return true if there is exactly one operation using this
770   /// result value of the defining operator.
771   inline bool hasOneUse() const;
772 };
773
774
775 template<> struct DenseMapKeyInfo<SDOperand> {
776   static inline SDOperand getEmptyKey() { return SDOperand((SDNode*)-1, -1U); }
777   static inline SDOperand getTombstoneKey() { return SDOperand((SDNode*)-1, 0);}
778   static unsigned getHashValue(const SDOperand &Val) {
779     return (unsigned)((uintptr_t)Val.Val >> 4) ^
780            (unsigned)((uintptr_t)Val.Val >> 9) + Val.ResNo;
781   }
782   static bool isPod() { return true; }
783 };
784
785 /// simplify_type specializations - Allow casting operators to work directly on
786 /// SDOperands as if they were SDNode*'s.
787 template<> struct simplify_type<SDOperand> {
788   typedef SDNode* SimpleType;
789   static SimpleType getSimplifiedValue(const SDOperand &Val) {
790     return static_cast<SimpleType>(Val.Val);
791   }
792 };
793 template<> struct simplify_type<const SDOperand> {
794   typedef SDNode* SimpleType;
795   static SimpleType getSimplifiedValue(const SDOperand &Val) {
796     return static_cast<SimpleType>(Val.Val);
797   }
798 };
799
800
801 /// SDNode - Represents one node in the SelectionDAG.
802 ///
803 class SDNode : public FoldingSetNode {
804   /// NodeType - The operation that this node performs.
805   ///
806   unsigned short NodeType;
807   
808   /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
809   /// then they will be delete[]'d when the node is destroyed.
810   bool OperandsNeedDelete : 1;
811
812   /// NodeId - Unique id per SDNode in the DAG.
813   int NodeId;
814
815   /// OperandList - The values that are used by this operation.
816   ///
817   SDOperand *OperandList;
818   
819   /// ValueList - The types of the values this node defines.  SDNode's may
820   /// define multiple values simultaneously.
821   const MVT::ValueType *ValueList;
822
823   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
824   unsigned short NumOperands, NumValues;
825   
826   /// Prev/Next pointers - These pointers form the linked list of of the
827   /// AllNodes list in the current DAG.
828   SDNode *Prev, *Next;
829   friend struct ilist_traits<SDNode>;
830
831   /// Uses - These are all of the SDNode's that use a value produced by this
832   /// node.
833   SmallVector<SDNode*,3> Uses;
834   
835   // Out-of-line virtual method to give class a home.
836   virtual void ANCHOR();
837 public:
838   virtual ~SDNode() {
839     assert(NumOperands == 0 && "Operand list not cleared before deletion");
840     NodeType = ISD::DELETED_NODE;
841   }
842   
843   //===--------------------------------------------------------------------===//
844   //  Accessors
845   //
846   unsigned getOpcode()  const { return NodeType; }
847   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
848   unsigned getTargetOpcode() const {
849     assert(isTargetOpcode() && "Not a target opcode!");
850     return NodeType - ISD::BUILTIN_OP_END;
851   }
852
853   size_t use_size() const { return Uses.size(); }
854   bool use_empty() const { return Uses.empty(); }
855   bool hasOneUse() const { return Uses.size() == 1; }
856
857   /// getNodeId - Return the unique node id.
858   ///
859   int getNodeId() const { return NodeId; }
860
861   typedef SmallVector<SDNode*,3>::const_iterator use_iterator;
862   use_iterator use_begin() const { return Uses.begin(); }
863   use_iterator use_end() const { return Uses.end(); }
864
865   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
866   /// indicated value.  This method ignores uses of other values defined by this
867   /// operation.
868   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
869
870   /// hasAnyUseOfValue - Return true if there are any use of the indicated
871   /// value. This method ignores uses of other values defined by this operation.
872   bool hasAnyUseOfValue(unsigned Value) const;
873
874   /// isOnlyUse - Return true if this node is the only use of N.
875   ///
876   bool isOnlyUse(SDNode *N) const;
877
878   /// isOperand - Return true if this node is an operand of N.
879   ///
880   bool isOperand(SDNode *N) const;
881
882   /// isPredecessor - Return true if this node is a predecessor of N. This node
883   /// is either an operand of N or it can be reached by recursively traversing
884   /// up the operands.
885   /// NOTE: this is an expensive method. Use it carefully.
886   bool isPredecessor(SDNode *N) const;
887
888   /// getNumOperands - Return the number of values used by this operation.
889   ///
890   unsigned getNumOperands() const { return NumOperands; }
891
892   /// getConstantOperandVal - Helper method returns the integer value of a 
893   /// ConstantSDNode operand.
894   uint64_t getConstantOperandVal(unsigned Num) const;
895
896   const SDOperand &getOperand(unsigned Num) const {
897     assert(Num < NumOperands && "Invalid child # of SDNode!");
898     return OperandList[Num];
899   }
900
901   typedef const SDOperand* op_iterator;
902   op_iterator op_begin() const { return OperandList; }
903   op_iterator op_end() const { return OperandList+NumOperands; }
904
905
906   SDVTList getVTList() const {
907     SDVTList X = { ValueList, NumValues };
908     return X;
909   };
910   
911   /// getNumValues - Return the number of values defined/returned by this
912   /// operator.
913   ///
914   unsigned getNumValues() const { return NumValues; }
915
916   /// getValueType - Return the type of a specified result.
917   ///
918   MVT::ValueType getValueType(unsigned ResNo) const {
919     assert(ResNo < NumValues && "Illegal result number!");
920     return ValueList[ResNo];
921   }
922
923   typedef const MVT::ValueType* value_iterator;
924   value_iterator value_begin() const { return ValueList; }
925   value_iterator value_end() const { return ValueList+NumValues; }
926
927   /// getOperationName - Return the opcode of this operation for printing.
928   ///
929   std::string getOperationName(const SelectionDAG *G = 0) const;
930   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
931   void dump() const;
932   void dump(const SelectionDAG *G) const;
933
934   static bool classof(const SDNode *) { return true; }
935
936   /// Profile - Gather unique data for the node.
937   ///
938   void Profile(FoldingSetNodeID &ID);
939
940 protected:
941   friend class SelectionDAG;
942   
943   /// getValueTypeList - Return a pointer to the specified value type.
944   ///
945   static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
946   static SDVTList getSDVTList(MVT::ValueType VT) {
947     SDVTList Ret = { getValueTypeList(VT), 1 };
948     return Ret;
949   }
950
951   SDNode(unsigned Opc, SDVTList VTs, const SDOperand *Ops, unsigned NumOps)
952     : NodeType(Opc), NodeId(-1) {
953     OperandsNeedDelete = true;
954     NumOperands = NumOps;
955     OperandList = NumOps ? new SDOperand[NumOperands] : 0;
956     
957     for (unsigned i = 0; i != NumOps; ++i) {
958       OperandList[i] = Ops[i];
959       Ops[i].Val->Uses.push_back(this);
960     }
961     
962     ValueList = VTs.VTs;
963     NumValues = VTs.NumVTs;
964     Prev = 0; Next = 0;
965   }
966   SDNode(unsigned Opc, SDVTList VTs) : NodeType(Opc), NodeId(-1) {
967     OperandsNeedDelete = false;  // Operands set with InitOperands.
968     NumOperands = 0;
969     OperandList = 0;
970     
971     ValueList = VTs.VTs;
972     NumValues = VTs.NumVTs;
973     Prev = 0; Next = 0;
974   }
975   
976   /// InitOperands - Initialize the operands list of this node with the
977   /// specified values, which are part of the node (thus they don't need to be
978   /// copied in or allocated).
979   void InitOperands(SDOperand *Ops, unsigned NumOps) {
980     assert(OperandList == 0 && "Operands already set!");
981     NumOperands = NumOps;
982     OperandList = Ops;
983     
984     for (unsigned i = 0; i != NumOps; ++i)
985       Ops[i].Val->Uses.push_back(this);
986   }
987   
988   /// MorphNodeTo - This frees the operands of the current node, resets the
989   /// opcode, types, and operands to the specified value.  This should only be
990   /// used by the SelectionDAG class.
991   void MorphNodeTo(unsigned Opc, SDVTList L,
992                    const SDOperand *Ops, unsigned NumOps);
993   
994   void addUser(SDNode *User) {
995     Uses.push_back(User);
996   }
997   void removeUser(SDNode *User) {
998     // Remove this user from the operand's use list.
999     for (unsigned i = Uses.size(); ; --i) {
1000       assert(i != 0 && "Didn't find user!");
1001       if (Uses[i-1] == User) {
1002         Uses[i-1] = Uses.back();
1003         Uses.pop_back();
1004         return;
1005       }
1006     }
1007   }
1008
1009   void setNodeId(int Id) {
1010     NodeId = Id;
1011   }
1012 };
1013
1014
1015 // Define inline functions from the SDOperand class.
1016
1017 inline unsigned SDOperand::getOpcode() const {
1018   return Val->getOpcode();
1019 }
1020 inline MVT::ValueType SDOperand::getValueType() const {
1021   return Val->getValueType(ResNo);
1022 }
1023 inline unsigned SDOperand::getNumOperands() const {
1024   return Val->getNumOperands();
1025 }
1026 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
1027   return Val->getOperand(i);
1028 }
1029 inline uint64_t SDOperand::getConstantOperandVal(unsigned i) const {
1030   return Val->getConstantOperandVal(i);
1031 }
1032 inline bool SDOperand::isTargetOpcode() const {
1033   return Val->isTargetOpcode();
1034 }
1035 inline unsigned SDOperand::getTargetOpcode() const {
1036   return Val->getTargetOpcode();
1037 }
1038 inline bool SDOperand::hasOneUse() const {
1039   return Val->hasNUsesOfValue(1, ResNo);
1040 }
1041
1042 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
1043 /// to allow co-allocation of node operands with the node itself.
1044 class UnarySDNode : public SDNode {
1045   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1046   SDOperand Op;
1047 public:
1048   UnarySDNode(unsigned Opc, SDVTList VTs, SDOperand X)
1049     : SDNode(Opc, VTs), Op(X) {
1050     InitOperands(&Op, 1);
1051   }
1052 };
1053
1054 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
1055 /// to allow co-allocation of node operands with the node itself.
1056 class BinarySDNode : public SDNode {
1057   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1058   SDOperand Ops[2];
1059 public:
1060   BinarySDNode(unsigned Opc, SDVTList VTs, SDOperand X, SDOperand Y)
1061     : SDNode(Opc, VTs) {
1062     Ops[0] = X;
1063     Ops[1] = Y;
1064     InitOperands(Ops, 2);
1065   }
1066 };
1067
1068 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
1069 /// to allow co-allocation of node operands with the node itself.
1070 class TernarySDNode : public SDNode {
1071   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1072   SDOperand Ops[3];
1073 public:
1074   TernarySDNode(unsigned Opc, SDVTList VTs, SDOperand X, SDOperand Y,
1075                 SDOperand Z)
1076     : SDNode(Opc, VTs) {
1077     Ops[0] = X;
1078     Ops[1] = Y;
1079     Ops[2] = Z;
1080     InitOperands(Ops, 3);
1081   }
1082 };
1083
1084
1085 /// HandleSDNode - This class is used to form a handle around another node that
1086 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1087 /// operand.  This node should be directly created by end-users and not added to
1088 /// the AllNodes list.
1089 class HandleSDNode : public SDNode {
1090   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1091   SDOperand Op;
1092 public:
1093   explicit HandleSDNode(SDOperand X)
1094     : SDNode(ISD::HANDLENODE, getSDVTList(MVT::Other)), Op(X) {
1095     InitOperands(&Op, 1);
1096   }
1097   ~HandleSDNode();  
1098   SDOperand getValue() const { return Op; }
1099 };
1100
1101 class StringSDNode : public SDNode {
1102   std::string Value;
1103   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1104 protected:
1105   friend class SelectionDAG;
1106   explicit StringSDNode(const std::string &val)
1107     : SDNode(ISD::STRING, getSDVTList(MVT::Other)), Value(val) {
1108   }
1109 public:
1110   const std::string &getValue() const { return Value; }
1111   static bool classof(const StringSDNode *) { return true; }
1112   static bool classof(const SDNode *N) {
1113     return N->getOpcode() == ISD::STRING;
1114   }
1115 };  
1116
1117 class ConstantSDNode : public SDNode {
1118   uint64_t Value;
1119   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1120 protected:
1121   friend class SelectionDAG;
1122   ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1123     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, getSDVTList(VT)),
1124       Value(val) {
1125   }
1126 public:
1127
1128   uint64_t getValue() const { return Value; }
1129
1130   int64_t getSignExtended() const {
1131     unsigned Bits = MVT::getSizeInBits(getValueType(0));
1132     return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1133   }
1134
1135   bool isNullValue() const { return Value == 0; }
1136   bool isAllOnesValue() const {
1137     return Value == MVT::getIntVTBitMask(getValueType(0));
1138   }
1139
1140   static bool classof(const ConstantSDNode *) { return true; }
1141   static bool classof(const SDNode *N) {
1142     return N->getOpcode() == ISD::Constant ||
1143            N->getOpcode() == ISD::TargetConstant;
1144   }
1145 };
1146
1147 class ConstantFPSDNode : public SDNode {
1148   APFloat Value;
1149   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1150 protected:
1151   friend class SelectionDAG;
1152   ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1153     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1154              getSDVTList(VT)), Value(APFloat(val)) {
1155   }
1156 public:
1157
1158   double getValue() const { return Value.convertToDouble(); }
1159
1160   /// isExactlyValue - We don't rely on operator== working on double values, as
1161   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1162   /// As such, this method can be used to do an exact bit-for-bit comparison of
1163   /// two floating point values.
1164   bool isExactlyValue(double V) const;
1165
1166   static bool classof(const ConstantFPSDNode *) { return true; }
1167   static bool classof(const SDNode *N) {
1168     return N->getOpcode() == ISD::ConstantFP || 
1169            N->getOpcode() == ISD::TargetConstantFP;
1170   }
1171 };
1172
1173 class GlobalAddressSDNode : public SDNode {
1174   GlobalValue *TheGlobal;
1175   int Offset;
1176   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1177 protected:
1178   friend class SelectionDAG;
1179   GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1180                       int o = 0);
1181 public:
1182
1183   GlobalValue *getGlobal() const { return TheGlobal; }
1184   int getOffset() const { return Offset; }
1185
1186   static bool classof(const GlobalAddressSDNode *) { return true; }
1187   static bool classof(const SDNode *N) {
1188     return N->getOpcode() == ISD::GlobalAddress ||
1189            N->getOpcode() == ISD::TargetGlobalAddress ||
1190            N->getOpcode() == ISD::GlobalTLSAddress ||
1191            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1192   }
1193 };
1194
1195 class FrameIndexSDNode : public SDNode {
1196   int FI;
1197   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1198 protected:
1199   friend class SelectionDAG;
1200   FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1201     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, getSDVTList(VT)),
1202       FI(fi) {
1203   }
1204 public:
1205
1206   int getIndex() const { return FI; }
1207
1208   static bool classof(const FrameIndexSDNode *) { return true; }
1209   static bool classof(const SDNode *N) {
1210     return N->getOpcode() == ISD::FrameIndex ||
1211            N->getOpcode() == ISD::TargetFrameIndex;
1212   }
1213 };
1214
1215 class JumpTableSDNode : public SDNode {
1216   int JTI;
1217   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1218 protected:
1219   friend class SelectionDAG;
1220   JumpTableSDNode(int jti, MVT::ValueType VT, bool isTarg)
1221     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, getSDVTList(VT)),
1222       JTI(jti) {
1223   }
1224 public:
1225     
1226     int getIndex() const { return JTI; }
1227   
1228   static bool classof(const JumpTableSDNode *) { return true; }
1229   static bool classof(const SDNode *N) {
1230     return N->getOpcode() == ISD::JumpTable ||
1231            N->getOpcode() == ISD::TargetJumpTable;
1232   }
1233 };
1234
1235 class ConstantPoolSDNode : public SDNode {
1236   union {
1237     Constant *ConstVal;
1238     MachineConstantPoolValue *MachineCPVal;
1239   } Val;
1240   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1241   unsigned Alignment;
1242   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1243 protected:
1244   friend class SelectionDAG;
1245   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1246                      int o=0)
1247     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1248              getSDVTList(VT)), Offset(o), Alignment(0) {
1249     assert((int)Offset >= 0 && "Offset is too large");
1250     Val.ConstVal = c;
1251   }
1252   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1253                      unsigned Align)
1254     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1255              getSDVTList(VT)), Offset(o), Alignment(Align) {
1256     assert((int)Offset >= 0 && "Offset is too large");
1257     Val.ConstVal = c;
1258   }
1259   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1260                      MVT::ValueType VT, int o=0)
1261     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1262              getSDVTList(VT)), Offset(o), Alignment(0) {
1263     assert((int)Offset >= 0 && "Offset is too large");
1264     Val.MachineCPVal = v;
1265     Offset |= 1 << (sizeof(unsigned)*8-1);
1266   }
1267   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1268                      MVT::ValueType VT, int o, unsigned Align)
1269     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1270              getSDVTList(VT)), Offset(o), Alignment(Align) {
1271     assert((int)Offset >= 0 && "Offset is too large");
1272     Val.MachineCPVal = v;
1273     Offset |= 1 << (sizeof(unsigned)*8-1);
1274   }
1275 public:
1276
1277   bool isMachineConstantPoolEntry() const {
1278     return (int)Offset < 0;
1279   }
1280
1281   Constant *getConstVal() const {
1282     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1283     return Val.ConstVal;
1284   }
1285
1286   MachineConstantPoolValue *getMachineCPVal() const {
1287     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1288     return Val.MachineCPVal;
1289   }
1290
1291   int getOffset() const {
1292     return Offset & ~(1 << (sizeof(unsigned)*8-1));
1293   }
1294   
1295   // Return the alignment of this constant pool object, which is either 0 (for
1296   // default alignment) or log2 of the desired value.
1297   unsigned getAlignment() const { return Alignment; }
1298
1299   const Type *getType() const;
1300
1301   static bool classof(const ConstantPoolSDNode *) { return true; }
1302   static bool classof(const SDNode *N) {
1303     return N->getOpcode() == ISD::ConstantPool ||
1304            N->getOpcode() == ISD::TargetConstantPool;
1305   }
1306 };
1307
1308 class BasicBlockSDNode : public SDNode {
1309   MachineBasicBlock *MBB;
1310   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1311 protected:
1312   friend class SelectionDAG;
1313   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1314     : SDNode(ISD::BasicBlock, getSDVTList(MVT::Other)), MBB(mbb) {
1315   }
1316 public:
1317
1318   MachineBasicBlock *getBasicBlock() const { return MBB; }
1319
1320   static bool classof(const BasicBlockSDNode *) { return true; }
1321   static bool classof(const SDNode *N) {
1322     return N->getOpcode() == ISD::BasicBlock;
1323   }
1324 };
1325
1326 class SrcValueSDNode : public SDNode {
1327   const Value *V;
1328   int offset;
1329   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1330 protected:
1331   friend class SelectionDAG;
1332   SrcValueSDNode(const Value* v, int o)
1333     : SDNode(ISD::SRCVALUE, getSDVTList(MVT::Other)), V(v), offset(o) {
1334   }
1335
1336 public:
1337   const Value *getValue() const { return V; }
1338   int getOffset() const { return offset; }
1339
1340   static bool classof(const SrcValueSDNode *) { return true; }
1341   static bool classof(const SDNode *N) {
1342     return N->getOpcode() == ISD::SRCVALUE;
1343   }
1344 };
1345
1346
1347 class RegisterSDNode : public SDNode {
1348   unsigned Reg;
1349   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1350 protected:
1351   friend class SelectionDAG;
1352   RegisterSDNode(unsigned reg, MVT::ValueType VT)
1353     : SDNode(ISD::Register, getSDVTList(VT)), Reg(reg) {
1354   }
1355 public:
1356
1357   unsigned getReg() const { return Reg; }
1358
1359   static bool classof(const RegisterSDNode *) { return true; }
1360   static bool classof(const SDNode *N) {
1361     return N->getOpcode() == ISD::Register;
1362   }
1363 };
1364
1365 class ExternalSymbolSDNode : public SDNode {
1366   const char *Symbol;
1367   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1368 protected:
1369   friend class SelectionDAG;
1370   ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1371     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1372              getSDVTList(VT)), Symbol(Sym) {
1373   }
1374 public:
1375
1376   const char *getSymbol() const { return Symbol; }
1377
1378   static bool classof(const ExternalSymbolSDNode *) { return true; }
1379   static bool classof(const SDNode *N) {
1380     return N->getOpcode() == ISD::ExternalSymbol ||
1381            N->getOpcode() == ISD::TargetExternalSymbol;
1382   }
1383 };
1384
1385 class CondCodeSDNode : public SDNode {
1386   ISD::CondCode Condition;
1387   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1388 protected:
1389   friend class SelectionDAG;
1390   explicit CondCodeSDNode(ISD::CondCode Cond)
1391     : SDNode(ISD::CONDCODE, getSDVTList(MVT::Other)), Condition(Cond) {
1392   }
1393 public:
1394
1395   ISD::CondCode get() const { return Condition; }
1396
1397   static bool classof(const CondCodeSDNode *) { return true; }
1398   static bool classof(const SDNode *N) {
1399     return N->getOpcode() == ISD::CONDCODE;
1400   }
1401 };
1402
1403 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1404 /// to parameterize some operations.
1405 class VTSDNode : public SDNode {
1406   MVT::ValueType ValueType;
1407   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1408 protected:
1409   friend class SelectionDAG;
1410   explicit VTSDNode(MVT::ValueType VT)
1411     : SDNode(ISD::VALUETYPE, getSDVTList(MVT::Other)), ValueType(VT) {
1412   }
1413 public:
1414
1415   MVT::ValueType getVT() const { return ValueType; }
1416
1417   static bool classof(const VTSDNode *) { return true; }
1418   static bool classof(const SDNode *N) {
1419     return N->getOpcode() == ISD::VALUETYPE;
1420   }
1421 };
1422
1423 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1424 ///
1425 class LoadSDNode : public SDNode {
1426   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1427   SDOperand Ops[3];
1428   
1429   // AddrMode - unindexed, pre-indexed, post-indexed.
1430   ISD::MemIndexedMode AddrMode;
1431
1432   // ExtType - non-ext, anyext, sext, zext.
1433   ISD::LoadExtType ExtType;
1434
1435   // LoadedVT - VT of loaded value before extension.
1436   MVT::ValueType LoadedVT;
1437
1438   // SrcValue - Memory location for alias analysis.
1439   const Value *SrcValue;
1440
1441   // SVOffset - Memory location offset.
1442   int SVOffset;
1443
1444   // Alignment - Alignment of memory location in bytes.
1445   unsigned Alignment;
1446
1447   // IsVolatile - True if the load is volatile.
1448   bool IsVolatile;
1449 protected:
1450   friend class SelectionDAG;
1451   LoadSDNode(SDOperand *ChainPtrOff, SDVTList VTs,
1452              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, MVT::ValueType LVT,
1453              const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
1454     : SDNode(ISD::LOAD, VTs),
1455       AddrMode(AM), ExtType(ETy), LoadedVT(LVT), SrcValue(SV), SVOffset(O),
1456       Alignment(Align), IsVolatile(Vol) {
1457     Ops[0] = ChainPtrOff[0]; // Chain
1458     Ops[1] = ChainPtrOff[1]; // Ptr
1459     Ops[2] = ChainPtrOff[2]; // Off
1460     InitOperands(Ops, 3);
1461     assert(Align != 0 && "Loads should have non-zero aligment");
1462     assert((getOffset().getOpcode() == ISD::UNDEF ||
1463             AddrMode != ISD::UNINDEXED) &&
1464            "Only indexed load has a non-undef offset operand");
1465   }
1466 public:
1467
1468   const SDOperand getChain() const { return getOperand(0); }
1469   const SDOperand getBasePtr() const { return getOperand(1); }
1470   const SDOperand getOffset() const { return getOperand(2); }
1471   ISD::MemIndexedMode getAddressingMode() const { return AddrMode; }
1472   ISD::LoadExtType getExtensionType() const { return ExtType; }
1473   MVT::ValueType getLoadedVT() const { return LoadedVT; }
1474   const Value *getSrcValue() const { return SrcValue; }
1475   int getSrcValueOffset() const { return SVOffset; }
1476   unsigned getAlignment() const { return Alignment; }
1477   bool isVolatile() const { return IsVolatile; }
1478
1479   static bool classof(const LoadSDNode *) { return true; }
1480   static bool classof(const SDNode *N) {
1481     return N->getOpcode() == ISD::LOAD;
1482   }
1483 };
1484
1485 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1486 ///
1487 class StoreSDNode : public SDNode {
1488   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1489   SDOperand Ops[4];
1490     
1491   // AddrMode - unindexed, pre-indexed, post-indexed.
1492   ISD::MemIndexedMode AddrMode;
1493
1494   // IsTruncStore - True is the op does a truncation before store.
1495   bool IsTruncStore;
1496
1497   // StoredVT - VT of the value after truncation.
1498   MVT::ValueType StoredVT;
1499
1500   // SrcValue - Memory location for alias analysis.
1501   const Value *SrcValue;
1502
1503   // SVOffset - Memory location offset.
1504   int SVOffset;
1505
1506   // Alignment - Alignment of memory location in bytes.
1507   unsigned Alignment;
1508
1509   // IsVolatile - True if the store is volatile.
1510   bool IsVolatile;
1511 protected:
1512   friend class SelectionDAG;
1513   StoreSDNode(SDOperand *ChainValuePtrOff, SDVTList VTs,
1514               ISD::MemIndexedMode AM, bool isTrunc, MVT::ValueType SVT,
1515               const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
1516     : SDNode(ISD::STORE, VTs),
1517       AddrMode(AM), IsTruncStore(isTrunc), StoredVT(SVT), SrcValue(SV),
1518       SVOffset(O), Alignment(Align), IsVolatile(Vol) {
1519     Ops[0] = ChainValuePtrOff[0]; // Chain
1520     Ops[1] = ChainValuePtrOff[1]; // Value
1521     Ops[2] = ChainValuePtrOff[2]; // Ptr
1522     Ops[3] = ChainValuePtrOff[3]; // Off
1523     InitOperands(Ops, 4);
1524     assert(Align != 0 && "Stores should have non-zero aligment");
1525     assert((getOffset().getOpcode() == ISD::UNDEF || 
1526             AddrMode != ISD::UNINDEXED) &&
1527            "Only indexed store has a non-undef offset operand");
1528   }
1529 public:
1530
1531   const SDOperand getChain() const { return getOperand(0); }
1532   const SDOperand getValue() const { return getOperand(1); }
1533   const SDOperand getBasePtr() const { return getOperand(2); }
1534   const SDOperand getOffset() const { return getOperand(3); }
1535   ISD::MemIndexedMode getAddressingMode() const { return AddrMode; }
1536   bool isTruncatingStore() const { return IsTruncStore; }
1537   MVT::ValueType getStoredVT() const { return StoredVT; }
1538   const Value *getSrcValue() const { return SrcValue; }
1539   int getSrcValueOffset() const { return SVOffset; }
1540   unsigned getAlignment() const { return Alignment; }
1541   bool isVolatile() const { return IsVolatile; }
1542
1543   static bool classof(const StoreSDNode *) { return true; }
1544   static bool classof(const SDNode *N) {
1545     return N->getOpcode() == ISD::STORE;
1546   }
1547 };
1548
1549
1550 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1551   SDNode *Node;
1552   unsigned Operand;
1553
1554   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1555 public:
1556   bool operator==(const SDNodeIterator& x) const {
1557     return Operand == x.Operand;
1558   }
1559   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1560
1561   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1562     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1563     Operand = I.Operand;
1564     return *this;
1565   }
1566
1567   pointer operator*() const {
1568     return Node->getOperand(Operand).Val;
1569   }
1570   pointer operator->() const { return operator*(); }
1571
1572   SDNodeIterator& operator++() {                // Preincrement
1573     ++Operand;
1574     return *this;
1575   }
1576   SDNodeIterator operator++(int) { // Postincrement
1577     SDNodeIterator tmp = *this; ++*this; return tmp;
1578   }
1579
1580   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1581   static SDNodeIterator end  (SDNode *N) {
1582     return SDNodeIterator(N, N->getNumOperands());
1583   }
1584
1585   unsigned getOperand() const { return Operand; }
1586   const SDNode *getNode() const { return Node; }
1587 };
1588
1589 template <> struct GraphTraits<SDNode*> {
1590   typedef SDNode NodeType;
1591   typedef SDNodeIterator ChildIteratorType;
1592   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1593   static inline ChildIteratorType child_begin(NodeType *N) {
1594     return SDNodeIterator::begin(N);
1595   }
1596   static inline ChildIteratorType child_end(NodeType *N) {
1597     return SDNodeIterator::end(N);
1598   }
1599 };
1600
1601 template<>
1602 struct ilist_traits<SDNode> {
1603   static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1604   static SDNode *getNext(const SDNode *N) { return N->Next; }
1605   
1606   static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1607   static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1608   
1609   static SDNode *createSentinel() {
1610     return new SDNode(ISD::EntryToken, SDNode::getSDVTList(MVT::Other));
1611   }
1612   static void destroySentinel(SDNode *N) { delete N; }
1613   //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1614   
1615   
1616   void addNodeToList(SDNode *NTy) {}
1617   void removeNodeFromList(SDNode *NTy) {}
1618   void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1619                              const ilist_iterator<SDNode> &X,
1620                              const ilist_iterator<SDNode> &Y) {}
1621 };
1622
1623 namespace ISD {
1624   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1625   /// load.
1626   inline bool isNON_EXTLoad(const SDNode *N) {
1627     return N->getOpcode() == ISD::LOAD &&
1628       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1629   }
1630
1631   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1632   ///
1633   inline bool isEXTLoad(const SDNode *N) {
1634     return N->getOpcode() == ISD::LOAD &&
1635       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1636   }
1637
1638   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1639   ///
1640   inline bool isSEXTLoad(const SDNode *N) {
1641     return N->getOpcode() == ISD::LOAD &&
1642       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1643   }
1644
1645   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1646   ///
1647   inline bool isZEXTLoad(const SDNode *N) {
1648     return N->getOpcode() == ISD::LOAD &&
1649       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1650   }
1651
1652   /// isUNINDEXEDLoad - Returns true if the specified node is a unindexed load.
1653   ///
1654   inline bool isUNINDEXEDLoad(const SDNode *N) {
1655     return N->getOpcode() == ISD::LOAD &&
1656       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1657   }
1658
1659   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1660   /// store.
1661   inline bool isNON_TRUNCStore(const SDNode *N) {
1662     return N->getOpcode() == ISD::STORE &&
1663       !cast<StoreSDNode>(N)->isTruncatingStore();
1664   }
1665
1666   /// isTRUNCStore - Returns true if the specified node is a truncating
1667   /// store.
1668   inline bool isTRUNCStore(const SDNode *N) {
1669     return N->getOpcode() == ISD::STORE &&
1670       cast<StoreSDNode>(N)->isTruncatingStore();
1671   }
1672 }
1673
1674
1675 } // end llvm namespace
1676
1677 #endif