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