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