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