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