006bf6e8f2cc6ed8a4383d7db532081c2c5480f3
[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   /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
769   /// then they will be delete[]'d when the node is destroyed.
770   bool OperandsNeedDelete : 1;
771
772   /// NodeId - Unique id per SDNode in the DAG.
773   int NodeId;
774
775   /// OperandList - The values that are used by this operation.
776   ///
777   SDOperand *OperandList;
778   
779   /// ValueList - The types of the values this node defines.  SDNode's may
780   /// define multiple values simultaneously.
781   const MVT::ValueType *ValueList;
782
783   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
784   unsigned short NumOperands, NumValues;
785   
786   /// Prev/Next pointers - These pointers form the linked list of of the
787   /// AllNodes list in the current DAG.
788   SDNode *Prev, *Next;
789   friend struct ilist_traits<SDNode>;
790
791   /// Uses - These are all of the SDNode's that use a value produced by this
792   /// node.
793   SmallVector<SDNode*,3> Uses;
794   
795   // Out-of-line virtual method to give class a home.
796   virtual void ANCHOR();
797 public:
798   virtual ~SDNode() {
799     assert(NumOperands == 0 && "Operand list not cleared before deletion");
800     NodeType = ISD::DELETED_NODE;
801   }
802   
803   //===--------------------------------------------------------------------===//
804   //  Accessors
805   //
806   unsigned getOpcode()  const { return NodeType; }
807   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
808   unsigned getTargetOpcode() const {
809     assert(isTargetOpcode() && "Not a target opcode!");
810     return NodeType - ISD::BUILTIN_OP_END;
811   }
812
813   size_t use_size() const { return Uses.size(); }
814   bool use_empty() const { return Uses.empty(); }
815   bool hasOneUse() const { return Uses.size() == 1; }
816
817   /// getNodeId - Return the unique node id.
818   ///
819   int getNodeId() const { return NodeId; }
820
821   typedef SmallVector<SDNode*,3>::const_iterator use_iterator;
822   use_iterator use_begin() const { return Uses.begin(); }
823   use_iterator use_end() const { return Uses.end(); }
824
825   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
826   /// indicated value.  This method ignores uses of other values defined by this
827   /// operation.
828   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
829
830   /// isOnlyUse - Return true if this node is the only use of N.
831   ///
832   bool isOnlyUse(SDNode *N) const;
833
834   /// isOperand - Return true if this node is an operand of N.
835   ///
836   bool isOperand(SDNode *N) const;
837
838   /// isPredecessor - Return true if this node is a predecessor of N. This node
839   /// is either an operand of N or it can be reached by recursively traversing
840   /// up the operands.
841   /// NOTE: this is an expensive method. Use it carefully.
842   bool isPredecessor(SDNode *N) const;
843
844   /// getNumOperands - Return the number of values used by this operation.
845   ///
846   unsigned getNumOperands() const { return NumOperands; }
847
848   /// getConstantOperandVal - Helper method returns the integer value of a 
849   /// ConstantSDNode operand.
850   uint64_t getConstantOperandVal(unsigned Num) const;
851
852   const SDOperand &getOperand(unsigned Num) const {
853     assert(Num < NumOperands && "Invalid child # of SDNode!");
854     return OperandList[Num];
855   }
856
857   typedef const SDOperand* op_iterator;
858   op_iterator op_begin() const { return OperandList; }
859   op_iterator op_end() const { return OperandList+NumOperands; }
860
861
862   SDVTList getVTList() const {
863     SDVTList X = { ValueList, NumValues };
864     return X;
865   };
866   
867   /// getNumValues - Return the number of values defined/returned by this
868   /// operator.
869   ///
870   unsigned getNumValues() const { return NumValues; }
871
872   /// getValueType - Return the type of a specified result.
873   ///
874   MVT::ValueType getValueType(unsigned ResNo) const {
875     assert(ResNo < NumValues && "Illegal result number!");
876     return ValueList[ResNo];
877   }
878
879   typedef const MVT::ValueType* value_iterator;
880   value_iterator value_begin() const { return ValueList; }
881   value_iterator value_end() const { return ValueList+NumValues; }
882
883   /// getOperationName - Return the opcode of this operation for printing.
884   ///
885   const char* getOperationName(const SelectionDAG *G = 0) const;
886   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
887   void dump() const;
888   void dump(const SelectionDAG *G) const;
889
890   static bool classof(const SDNode *) { return true; }
891
892   /// Profile - Gather unique data for the node.
893   ///
894   void Profile(FoldingSetNodeID &ID);
895
896 protected:
897   friend class SelectionDAG;
898   
899   /// getValueTypeList - Return a pointer to the specified value type.
900   ///
901   static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
902   static SDVTList getSDVTList(MVT::ValueType VT) {
903     SDVTList Ret = { getValueTypeList(VT), 1 };
904     return Ret;
905   }
906
907   SDNode(unsigned Opc, SDVTList VTs, const SDOperand *Ops, unsigned NumOps)
908     : NodeType(Opc), NodeId(-1) {
909     OperandsNeedDelete = true;
910     NumOperands = NumOps;
911     OperandList = NumOps ? new SDOperand[NumOperands] : 0;
912     
913     for (unsigned i = 0; i != NumOps; ++i) {
914       OperandList[i] = Ops[i];
915       Ops[i].Val->Uses.push_back(this);
916     }
917     
918     ValueList = VTs.VTs;
919     NumValues = VTs.NumVTs;
920     Prev = 0; Next = 0;
921   }
922   SDNode(unsigned Opc, SDVTList VTs) : NodeType(Opc), NodeId(-1) {
923     OperandsNeedDelete = false;  // Operands set with InitOperands.
924     NumOperands = 0;
925     OperandList = 0;
926     
927     ValueList = VTs.VTs;
928     NumValues = VTs.NumVTs;
929     Prev = 0; Next = 0;
930   }
931   
932   /// InitOperands - Initialize the operands list of this node with the
933   /// specified values, which are part of the node (thus they don't need to be
934   /// copied in or allocated).
935   void InitOperands(SDOperand *Ops, unsigned NumOps) {
936     assert(OperandList == 0 && "Operands already set!");
937     NumOperands = NumOps;
938     OperandList = Ops;
939     
940     for (unsigned i = 0; i != NumOps; ++i)
941       Ops[i].Val->Uses.push_back(this);
942   }
943   
944   /// MorphNodeTo - This frees the operands of the current node, resets the
945   /// opcode, types, and operands to the specified value.  This should only be
946   /// used by the SelectionDAG class.
947   void MorphNodeTo(unsigned Opc, SDVTList L,
948                    const SDOperand *Ops, unsigned NumOps);
949   
950   void addUser(SDNode *User) {
951     Uses.push_back(User);
952   }
953   void removeUser(SDNode *User) {
954     // Remove this user from the operand's use list.
955     for (unsigned i = Uses.size(); ; --i) {
956       assert(i != 0 && "Didn't find user!");
957       if (Uses[i-1] == User) {
958         Uses[i-1] = Uses.back();
959         Uses.pop_back();
960         return;
961       }
962     }
963   }
964
965   void setNodeId(int Id) {
966     NodeId = Id;
967   }
968 };
969
970
971 // Define inline functions from the SDOperand class.
972
973 inline unsigned SDOperand::getOpcode() const {
974   return Val->getOpcode();
975 }
976 inline MVT::ValueType SDOperand::getValueType() const {
977   return Val->getValueType(ResNo);
978 }
979 inline unsigned SDOperand::getNumOperands() const {
980   return Val->getNumOperands();
981 }
982 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
983   return Val->getOperand(i);
984 }
985 inline uint64_t SDOperand::getConstantOperandVal(unsigned i) const {
986   return Val->getConstantOperandVal(i);
987 }
988 inline bool SDOperand::isTargetOpcode() const {
989   return Val->isTargetOpcode();
990 }
991 inline unsigned SDOperand::getTargetOpcode() const {
992   return Val->getTargetOpcode();
993 }
994 inline bool SDOperand::hasOneUse() const {
995   return Val->hasNUsesOfValue(1, ResNo);
996 }
997
998 /// HandleSDNode - This class is used to form a handle around another node that
999 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1000 /// operand.  This node should be directly created by end-users and not added to
1001 /// the AllNodes list.
1002 class HandleSDNode : public SDNode {
1003   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1004   SDOperand Op;
1005 public:
1006   HandleSDNode(SDOperand X)
1007     : SDNode(ISD::HANDLENODE, getSDVTList(MVT::Other)), Op(X) {
1008     InitOperands(&Op, 1);
1009   }
1010   ~HandleSDNode();  
1011   SDOperand getValue() const { return Op; }
1012 };
1013
1014 class StringSDNode : public SDNode {
1015   std::string Value;
1016   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1017 protected:
1018   friend class SelectionDAG;
1019   StringSDNode(const std::string &val)
1020     : SDNode(ISD::STRING, getSDVTList(MVT::Other)), Value(val) {
1021   }
1022 public:
1023   const std::string &getValue() const { return Value; }
1024   static bool classof(const StringSDNode *) { return true; }
1025   static bool classof(const SDNode *N) {
1026     return N->getOpcode() == ISD::STRING;
1027   }
1028 };  
1029
1030 class ConstantSDNode : public SDNode {
1031   uint64_t Value;
1032   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1033 protected:
1034   friend class SelectionDAG;
1035   ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1036     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, getSDVTList(VT)),
1037       Value(val) {
1038   }
1039 public:
1040
1041   uint64_t getValue() const { return Value; }
1042
1043   int64_t getSignExtended() const {
1044     unsigned Bits = MVT::getSizeInBits(getValueType(0));
1045     return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1046   }
1047
1048   bool isNullValue() const { return Value == 0; }
1049   bool isAllOnesValue() const {
1050     return Value == MVT::getIntVTBitMask(getValueType(0));
1051   }
1052
1053   static bool classof(const ConstantSDNode *) { return true; }
1054   static bool classof(const SDNode *N) {
1055     return N->getOpcode() == ISD::Constant ||
1056            N->getOpcode() == ISD::TargetConstant;
1057   }
1058 };
1059
1060 class ConstantFPSDNode : public SDNode {
1061   double Value;
1062   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1063 protected:
1064   friend class SelectionDAG;
1065   ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1066     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1067              getSDVTList(VT)), Value(val) {
1068   }
1069 public:
1070
1071   double getValue() const { return Value; }
1072
1073   /// isExactlyValue - We don't rely on operator== working on double values, as
1074   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1075   /// As such, this method can be used to do an exact bit-for-bit comparison of
1076   /// two floating point values.
1077   bool isExactlyValue(double V) const;
1078
1079   static bool classof(const ConstantFPSDNode *) { return true; }
1080   static bool classof(const SDNode *N) {
1081     return N->getOpcode() == ISD::ConstantFP || 
1082            N->getOpcode() == ISD::TargetConstantFP;
1083   }
1084 };
1085
1086 class GlobalAddressSDNode : public SDNode {
1087   GlobalValue *TheGlobal;
1088   int Offset;
1089   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1090 protected:
1091   friend class SelectionDAG;
1092   GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1093                       int o = 0)
1094     : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress,
1095              getSDVTList(VT)), Offset(o) {
1096     TheGlobal = const_cast<GlobalValue*>(GA);
1097   }
1098 public:
1099
1100   GlobalValue *getGlobal() const { return TheGlobal; }
1101   int getOffset() const { return Offset; }
1102
1103   static bool classof(const GlobalAddressSDNode *) { return true; }
1104   static bool classof(const SDNode *N) {
1105     return N->getOpcode() == ISD::GlobalAddress ||
1106            N->getOpcode() == ISD::TargetGlobalAddress;
1107   }
1108 };
1109
1110
1111 class FrameIndexSDNode : public SDNode {
1112   int FI;
1113   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1114 protected:
1115   friend class SelectionDAG;
1116   FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1117     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, getSDVTList(VT)),
1118       FI(fi) {
1119   }
1120 public:
1121
1122   int getIndex() const { return FI; }
1123
1124   static bool classof(const FrameIndexSDNode *) { return true; }
1125   static bool classof(const SDNode *N) {
1126     return N->getOpcode() == ISD::FrameIndex ||
1127            N->getOpcode() == ISD::TargetFrameIndex;
1128   }
1129 };
1130
1131 class JumpTableSDNode : public SDNode {
1132   int JTI;
1133   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1134 protected:
1135   friend class SelectionDAG;
1136   JumpTableSDNode(int jti, MVT::ValueType VT, bool isTarg)
1137     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, getSDVTList(VT)),
1138       JTI(jti) {
1139   }
1140 public:
1141     
1142     int getIndex() const { return JTI; }
1143   
1144   static bool classof(const JumpTableSDNode *) { return true; }
1145   static bool classof(const SDNode *N) {
1146     return N->getOpcode() == ISD::JumpTable ||
1147            N->getOpcode() == ISD::TargetJumpTable;
1148   }
1149 };
1150
1151 class ConstantPoolSDNode : public SDNode {
1152   union {
1153     Constant *ConstVal;
1154     MachineConstantPoolValue *MachineCPVal;
1155   } Val;
1156   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1157   unsigned Alignment;
1158   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1159 protected:
1160   friend class SelectionDAG;
1161   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1162                      int o=0)
1163     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1164              getSDVTList(VT)), Offset(o), Alignment(0) {
1165     assert((int)Offset >= 0 && "Offset is too large");
1166     Val.ConstVal = c;
1167   }
1168   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1169                      unsigned Align)
1170     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1171              getSDVTList(VT)), Offset(o), Alignment(Align) {
1172     assert((int)Offset >= 0 && "Offset is too large");
1173     Val.ConstVal = c;
1174   }
1175   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1176                      MVT::ValueType VT, int o=0)
1177     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1178              getSDVTList(VT)), Offset(o), Alignment(0) {
1179     assert((int)Offset >= 0 && "Offset is too large");
1180     Val.MachineCPVal = v;
1181     Offset |= 1 << (sizeof(unsigned)*8-1);
1182   }
1183   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1184                      MVT::ValueType VT, int o, unsigned Align)
1185     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1186              getSDVTList(VT)), Offset(o), Alignment(Align) {
1187     assert((int)Offset >= 0 && "Offset is too large");
1188     Val.MachineCPVal = v;
1189     Offset |= 1 << (sizeof(unsigned)*8-1);
1190   }
1191 public:
1192
1193   bool isMachineConstantPoolEntry() const {
1194     return (int)Offset < 0;
1195   }
1196
1197   Constant *getConstVal() const {
1198     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1199     return Val.ConstVal;
1200   }
1201
1202   MachineConstantPoolValue *getMachineCPVal() const {
1203     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1204     return Val.MachineCPVal;
1205   }
1206
1207   int getOffset() const {
1208     return Offset & ~(1 << (sizeof(unsigned)*8-1));
1209   }
1210   
1211   // Return the alignment of this constant pool object, which is either 0 (for
1212   // default alignment) or log2 of the desired value.
1213   unsigned getAlignment() const { return Alignment; }
1214
1215   const Type *getType() const;
1216
1217   static bool classof(const ConstantPoolSDNode *) { return true; }
1218   static bool classof(const SDNode *N) {
1219     return N->getOpcode() == ISD::ConstantPool ||
1220            N->getOpcode() == ISD::TargetConstantPool;
1221   }
1222 };
1223
1224 class BasicBlockSDNode : public SDNode {
1225   MachineBasicBlock *MBB;
1226   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1227 protected:
1228   friend class SelectionDAG;
1229   BasicBlockSDNode(MachineBasicBlock *mbb)
1230     : SDNode(ISD::BasicBlock, getSDVTList(MVT::Other)), MBB(mbb) {
1231   }
1232 public:
1233
1234   MachineBasicBlock *getBasicBlock() const { return MBB; }
1235
1236   static bool classof(const BasicBlockSDNode *) { return true; }
1237   static bool classof(const SDNode *N) {
1238     return N->getOpcode() == ISD::BasicBlock;
1239   }
1240 };
1241
1242 class SrcValueSDNode : public SDNode {
1243   const Value *V;
1244   int offset;
1245   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1246 protected:
1247   friend class SelectionDAG;
1248   SrcValueSDNode(const Value* v, int o)
1249     : SDNode(ISD::SRCVALUE, getSDVTList(MVT::Other)), V(v), offset(o) {
1250   }
1251
1252 public:
1253   const Value *getValue() const { return V; }
1254   int getOffset() const { return offset; }
1255
1256   static bool classof(const SrcValueSDNode *) { return true; }
1257   static bool classof(const SDNode *N) {
1258     return N->getOpcode() == ISD::SRCVALUE;
1259   }
1260 };
1261
1262
1263 class RegisterSDNode : public SDNode {
1264   unsigned Reg;
1265   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1266 protected:
1267   friend class SelectionDAG;
1268   RegisterSDNode(unsigned reg, MVT::ValueType VT)
1269     : SDNode(ISD::Register, getSDVTList(VT)), Reg(reg) {
1270   }
1271 public:
1272
1273   unsigned getReg() const { return Reg; }
1274
1275   static bool classof(const RegisterSDNode *) { return true; }
1276   static bool classof(const SDNode *N) {
1277     return N->getOpcode() == ISD::Register;
1278   }
1279 };
1280
1281 class ExternalSymbolSDNode : public SDNode {
1282   const char *Symbol;
1283   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1284 protected:
1285   friend class SelectionDAG;
1286   ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1287     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1288              getSDVTList(VT)), Symbol(Sym) {
1289   }
1290 public:
1291
1292   const char *getSymbol() const { return Symbol; }
1293
1294   static bool classof(const ExternalSymbolSDNode *) { return true; }
1295   static bool classof(const SDNode *N) {
1296     return N->getOpcode() == ISD::ExternalSymbol ||
1297            N->getOpcode() == ISD::TargetExternalSymbol;
1298   }
1299 };
1300
1301 class CondCodeSDNode : public SDNode {
1302   ISD::CondCode Condition;
1303   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1304 protected:
1305   friend class SelectionDAG;
1306   CondCodeSDNode(ISD::CondCode Cond)
1307     : SDNode(ISD::CONDCODE, getSDVTList(MVT::Other)), Condition(Cond) {
1308   }
1309 public:
1310
1311   ISD::CondCode get() const { return Condition; }
1312
1313   static bool classof(const CondCodeSDNode *) { return true; }
1314   static bool classof(const SDNode *N) {
1315     return N->getOpcode() == ISD::CONDCODE;
1316   }
1317 };
1318
1319 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1320 /// to parameterize some operations.
1321 class VTSDNode : public SDNode {
1322   MVT::ValueType ValueType;
1323   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1324 protected:
1325   friend class SelectionDAG;
1326   VTSDNode(MVT::ValueType VT)
1327     : SDNode(ISD::VALUETYPE, getSDVTList(MVT::Other)), ValueType(VT) {
1328   }
1329 public:
1330
1331   MVT::ValueType getVT() const { return ValueType; }
1332
1333   static bool classof(const VTSDNode *) { return true; }
1334   static bool classof(const SDNode *N) {
1335     return N->getOpcode() == ISD::VALUETYPE;
1336   }
1337 };
1338
1339 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1340 ///
1341 class LoadSDNode : public SDNode {
1342   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1343   SDOperand Ops[3];
1344   
1345   // AddrMode - unindexed, pre-indexed, post-indexed.
1346   ISD::MemIndexedMode AddrMode;
1347
1348   // ExtType - non-ext, anyext, sext, zext.
1349   ISD::LoadExtType ExtType;
1350
1351   // LoadedVT - VT of loaded value before extension.
1352   MVT::ValueType LoadedVT;
1353
1354   // SrcValue - Memory location for alias analysis.
1355   const Value *SrcValue;
1356
1357   // SVOffset - Memory location offset.
1358   int SVOffset;
1359
1360   // Alignment - Alignment of memory location in bytes.
1361   unsigned Alignment;
1362
1363   // IsVolatile - True if the load is volatile.
1364   bool IsVolatile;
1365 protected:
1366   friend class SelectionDAG;
1367   LoadSDNode(SDOperand *ChainPtrOff, SDVTList VTs,
1368              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, MVT::ValueType LVT,
1369              const Value *SV, int O=0, unsigned Align=1, bool Vol=false)
1370     : SDNode(ISD::LOAD, VTs),
1371       AddrMode(AM), ExtType(ETy), LoadedVT(LVT), SrcValue(SV), SVOffset(O),
1372       Alignment(Align), IsVolatile(Vol) {
1373     Ops[0] = ChainPtrOff[0]; // Chain
1374     Ops[1] = ChainPtrOff[1]; // Ptr
1375     Ops[2] = ChainPtrOff[2]; // Off
1376     InitOperands(Ops, 3);
1377     assert((getOffset().getOpcode() == ISD::UNDEF ||
1378             AddrMode != ISD::UNINDEXED) &&
1379            "Only indexed load has a non-undef offset operand");
1380   }
1381 public:
1382
1383   const SDOperand getChain() const { return getOperand(0); }
1384   const SDOperand getBasePtr() const { return getOperand(1); }
1385   const SDOperand getOffset() const { return getOperand(2); }
1386   ISD::MemIndexedMode getAddressingMode() const { return AddrMode; }
1387   ISD::LoadExtType getExtensionType() const { return ExtType; }
1388   MVT::ValueType getLoadedVT() const { return LoadedVT; }
1389   const Value *getSrcValue() const { return SrcValue; }
1390   int getSrcValueOffset() const { return SVOffset; }
1391   unsigned getAlignment() const { return Alignment; }
1392   bool isVolatile() const { return IsVolatile; }
1393
1394   static bool classof(const LoadSDNode *) { return true; }
1395   static bool classof(const SDNode *N) {
1396     return N->getOpcode() == ISD::LOAD;
1397   }
1398 };
1399
1400 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1401 ///
1402 class StoreSDNode : public SDNode {
1403   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1404   SDOperand Ops[4];
1405     
1406   // AddrMode - unindexed, pre-indexed, post-indexed.
1407   ISD::MemIndexedMode AddrMode;
1408
1409   // IsTruncStore - True is the op does a truncation before store.
1410   bool IsTruncStore;
1411
1412   // StoredVT - VT of the value after truncation.
1413   MVT::ValueType StoredVT;
1414
1415   // SrcValue - Memory location for alias analysis.
1416   const Value *SrcValue;
1417
1418   // SVOffset - Memory location offset.
1419   int SVOffset;
1420
1421   // Alignment - Alignment of memory location in bytes.
1422   unsigned Alignment;
1423
1424   // IsVolatile - True if the store is volatile.
1425   bool IsVolatile;
1426 protected:
1427   friend class SelectionDAG;
1428   StoreSDNode(SDOperand *ChainValuePtrOff, SDVTList VTs,
1429               ISD::MemIndexedMode AM, bool isTrunc, MVT::ValueType SVT,
1430               const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
1431     : SDNode(ISD::STORE, VTs),
1432       AddrMode(AM), IsTruncStore(isTrunc), StoredVT(SVT), SrcValue(SV),
1433       SVOffset(O), Alignment(Align), IsVolatile(Vol) {
1434     Ops[0] = ChainValuePtrOff[0]; // Chain
1435     Ops[1] = ChainValuePtrOff[1]; // Value
1436     Ops[2] = ChainValuePtrOff[2]; // Ptr
1437     Ops[3] = ChainValuePtrOff[3]; // Off
1438     InitOperands(Ops, 4);
1439     assert((getOffset().getOpcode() == ISD::UNDEF || 
1440             AddrMode != ISD::UNINDEXED) &&
1441            "Only indexed store has a non-undef offset operand");
1442   }
1443 public:
1444
1445   const SDOperand getChain() const { return getOperand(0); }
1446   const SDOperand getValue() const { return getOperand(1); }
1447   const SDOperand getBasePtr() const { return getOperand(2); }
1448   const SDOperand getOffset() const { return getOperand(3); }
1449   ISD::MemIndexedMode getAddressingMode() const { return AddrMode; }
1450   bool isTruncatingStore() const { return IsTruncStore; }
1451   MVT::ValueType getStoredVT() const { return StoredVT; }
1452   const Value *getSrcValue() const { return SrcValue; }
1453   int getSrcValueOffset() const { return SVOffset; }
1454   unsigned getAlignment() const { return Alignment; }
1455   bool isVolatile() const { return IsVolatile; }
1456
1457   static bool classof(const StoreSDNode *) { return true; }
1458   static bool classof(const SDNode *N) {
1459     return N->getOpcode() == ISD::STORE;
1460   }
1461 };
1462
1463
1464 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1465   SDNode *Node;
1466   unsigned Operand;
1467
1468   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1469 public:
1470   bool operator==(const SDNodeIterator& x) const {
1471     return Operand == x.Operand;
1472   }
1473   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1474
1475   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1476     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1477     Operand = I.Operand;
1478     return *this;
1479   }
1480
1481   pointer operator*() const {
1482     return Node->getOperand(Operand).Val;
1483   }
1484   pointer operator->() const { return operator*(); }
1485
1486   SDNodeIterator& operator++() {                // Preincrement
1487     ++Operand;
1488     return *this;
1489   }
1490   SDNodeIterator operator++(int) { // Postincrement
1491     SDNodeIterator tmp = *this; ++*this; return tmp;
1492   }
1493
1494   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1495   static SDNodeIterator end  (SDNode *N) {
1496     return SDNodeIterator(N, N->getNumOperands());
1497   }
1498
1499   unsigned getOperand() const { return Operand; }
1500   const SDNode *getNode() const { return Node; }
1501 };
1502
1503 template <> struct GraphTraits<SDNode*> {
1504   typedef SDNode NodeType;
1505   typedef SDNodeIterator ChildIteratorType;
1506   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1507   static inline ChildIteratorType child_begin(NodeType *N) {
1508     return SDNodeIterator::begin(N);
1509   }
1510   static inline ChildIteratorType child_end(NodeType *N) {
1511     return SDNodeIterator::end(N);
1512   }
1513 };
1514
1515 template<>
1516 struct ilist_traits<SDNode> {
1517   static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1518   static SDNode *getNext(const SDNode *N) { return N->Next; }
1519   
1520   static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1521   static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1522   
1523   static SDNode *createSentinel() {
1524     return new SDNode(ISD::EntryToken, SDNode::getSDVTList(MVT::Other));
1525   }
1526   static void destroySentinel(SDNode *N) { delete N; }
1527   //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1528   
1529   
1530   void addNodeToList(SDNode *NTy) {}
1531   void removeNodeFromList(SDNode *NTy) {}
1532   void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1533                              const ilist_iterator<SDNode> &X,
1534                              const ilist_iterator<SDNode> &Y) {}
1535 };
1536
1537 namespace ISD {
1538   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1539   /// load.
1540   inline bool isNON_EXTLoad(const SDNode *N) {
1541     return N->getOpcode() == ISD::LOAD &&
1542       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1543   }
1544
1545   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1546   ///
1547   inline bool isEXTLoad(const SDNode *N) {
1548     return N->getOpcode() == ISD::LOAD &&
1549       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1550   }
1551
1552   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1553   ///
1554   inline bool isSEXTLoad(const SDNode *N) {
1555     return N->getOpcode() == ISD::LOAD &&
1556       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1557   }
1558
1559   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1560   ///
1561   inline bool isZEXTLoad(const SDNode *N) {
1562     return N->getOpcode() == ISD::LOAD &&
1563       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1564   }
1565
1566   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1567   /// store.
1568   inline bool isNON_TRUNCStore(const SDNode *N) {
1569     return N->getOpcode() == ISD::STORE &&
1570       !cast<StoreSDNode>(N)->isTruncatingStore();
1571   }
1572
1573   /// isTRUNCStore - Returns true if the specified node is a truncating
1574   /// store.
1575   inline bool isTRUNCStore(const SDNode *N) {
1576     return N->getOpcode() == ISD::STORE &&
1577       cast<StoreSDNode>(N)->isTruncatingStore();
1578   }
1579 }
1580
1581
1582 } // end llvm namespace
1583
1584 #endif