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