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