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