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