c22b90ceedf7f4bc507d9c1c3cee83f5ddf33807
[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 is distributed under the University of Illinois Open Source
6 // 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/Constants.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/iterator.h"
26 #include "llvm/ADT/ilist_node.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/CodeGen/ValueTypes.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/Support/Allocator.h"
31 #include "llvm/Support/RecyclingAllocator.h"
32 #include "llvm/Support/DataTypes.h"
33 #include "llvm/CodeGen/DebugLoc.h"
34 #include <cassert>
35
36 namespace llvm {
37
38 class SelectionDAG;
39 class GlobalValue;
40 class MachineBasicBlock;
41 class MachineConstantPoolValue;
42 class SDNode;
43 class Value;
44 template <typename T> struct DenseMapInfo;
45 template <typename T> struct simplify_type;
46 template <typename T> struct ilist_traits;
47
48 /// SDVTList - This represents a list of ValueType's that has been intern'd by
49 /// a SelectionDAG.  Instances of this simple value class are returned by
50 /// SelectionDAG::getVTList(...).
51 ///
52 struct SDVTList {
53   const MVT *VTs;
54   unsigned short NumVTs;
55 };
56
57 /// ISD namespace - This namespace contains an enum which represents all of the
58 /// SelectionDAG node types and value types.
59 ///
60 namespace ISD {
61
62   //===--------------------------------------------------------------------===//
63   /// ISD::NodeType enum - This enum defines all of the operators valid in a
64   /// SelectionDAG.
65   ///
66   enum NodeType {
67     // DELETED_NODE - This is an illegal flag value that is used to catch
68     // errors.  This opcode is not a legal opcode for any node.
69     DELETED_NODE,
70     
71     // EntryToken - This is the marker used to indicate the start of the region.
72     EntryToken,
73
74     // TokenFactor - This node takes multiple tokens as input and produces a
75     // single token result.  This is used to represent the fact that the operand
76     // operators are independent of each other.
77     TokenFactor,
78     
79     // AssertSext, AssertZext - These nodes record if a register contains a 
80     // value that has already been zero or sign extended from a narrower type.  
81     // These nodes take two operands.  The first is the node that has already 
82     // been extended, and the second is a value type node indicating the width
83     // of the extension
84     AssertSext, AssertZext,
85
86     // Various leaf nodes.
87     BasicBlock, VALUETYPE, ARG_FLAGS, CONDCODE, Register,
88     Constant, ConstantFP,
89     GlobalAddress, GlobalTLSAddress, FrameIndex,
90     JumpTable, ConstantPool, ExternalSymbol,
91
92     // The address of the GOT
93     GLOBAL_OFFSET_TABLE,
94     
95     // FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and
96     // llvm.returnaddress on the DAG.  These nodes take one operand, the index
97     // of the frame or return address to return.  An index of zero corresponds
98     // to the current function's frame or return address, an index of one to the
99     // parent's frame or return address, and so on.
100     FRAMEADDR, RETURNADDR,
101
102     // FRAME_TO_ARGS_OFFSET - This node represents offset from frame pointer to
103     // first (possible) on-stack argument. This is needed for correct stack
104     // adjustment during unwind.
105     FRAME_TO_ARGS_OFFSET,
106     
107     // RESULT, OUTCHAIN = EXCEPTIONADDR(INCHAIN) - This node represents the
108     // address of the exception block on entry to an landing pad block.
109     EXCEPTIONADDR,
110     
111     // RESULT, OUTCHAIN = EHSELECTION(INCHAIN, EXCEPTION) - This node represents
112     // the selection index of the exception thrown.
113     EHSELECTION,
114
115     // OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents
116     // 'eh_return' gcc dwarf builtin, which is used to return from
117     // exception. The general meaning is: adjust stack by OFFSET and pass
118     // execution to HANDLER. Many platform-related details also :)
119     EH_RETURN,
120
121     // TargetConstant* - Like Constant*, but the DAG does not do any folding or
122     // simplification of the constant.
123     TargetConstant,
124     TargetConstantFP,
125     
126     // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
127     // anything else with this node, and this is valid in the target-specific
128     // dag, turning into a GlobalAddress operand.
129     TargetGlobalAddress,
130     TargetGlobalTLSAddress,
131     TargetFrameIndex,
132     TargetJumpTable,
133     TargetConstantPool,
134     TargetExternalSymbol,
135     
136     /// RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...)
137     /// This node represents a target intrinsic function with no side effects.
138     /// The first operand is the ID number of the intrinsic from the
139     /// llvm::Intrinsic namespace.  The operands to the intrinsic follow.  The
140     /// node has returns the result of the intrinsic.
141     INTRINSIC_WO_CHAIN,
142     
143     /// RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...)
144     /// This node represents a target intrinsic function with side effects that
145     /// returns a result.  The first operand is a chain pointer.  The second is
146     /// the ID number of the intrinsic from the llvm::Intrinsic namespace.  The
147     /// operands to the intrinsic follow.  The node has two results, the result
148     /// of the intrinsic and an output chain.
149     INTRINSIC_W_CHAIN,
150
151     /// OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...)
152     /// This node represents a target intrinsic function with side effects that
153     /// does not return a result.  The first operand is a chain pointer.  The
154     /// second is the ID number of the intrinsic from the llvm::Intrinsic
155     /// namespace.  The operands to the intrinsic follow.
156     INTRINSIC_VOID,
157     
158     // CopyToReg - This node has three operands: a chain, a register number to
159     // set to this value, and a value.  
160     CopyToReg,
161
162     // CopyFromReg - This node indicates that the input value is a virtual or
163     // physical register that is defined outside of the scope of this
164     // SelectionDAG.  The register is available from the RegisterSDNode object.
165     CopyFromReg,
166
167     // UNDEF - An undefined node
168     UNDEF,
169     
170     /// FORMAL_ARGUMENTS(CHAIN, CC#, ISVARARG, FLAG0, ..., FLAGn) - This node
171     /// represents the formal arguments for a function.  CC# is a Constant value
172     /// indicating the calling convention of the function, and ISVARARG is a
173     /// flag that indicates whether the function is varargs or not. This node
174     /// has one result value for each incoming argument, plus one for the output
175     /// chain. It must be custom legalized. See description of CALL node for
176     /// FLAG argument contents explanation.
177     /// 
178     FORMAL_ARGUMENTS,
179     
180     /// RV1, RV2...RVn, CHAIN = CALL(CHAIN, CALLEE,
181     ///                              ARG0, FLAG0, ARG1, FLAG1, ... ARGn, FLAGn)
182     /// This node represents a fully general function call, before the legalizer
183     /// runs.  This has one result value for each argument / flag pair, plus
184     /// a chain result. It must be custom legalized. Flag argument indicates
185     /// misc. argument attributes. Currently:
186     /// Bit 0 - signness
187     /// Bit 1 - 'inreg' attribute
188     /// Bit 2 - 'sret' attribute
189     /// Bit 4 - 'byval' attribute
190     /// Bit 5 - 'nest' attribute
191     /// Bit 6-9 - alignment of byval structures
192     /// Bit 10-26 - size of byval structures
193     /// Bits 31:27 - argument ABI alignment in the first argument piece and
194     /// alignment '1' in other argument pieces.
195     ///
196     /// CALL nodes use the CallSDNode subclass of SDNode, which
197     /// additionally carries information about the calling convention,
198     /// whether the call is varargs, and if it's marked as a tail call.
199     ///
200     CALL,
201
202     // EXTRACT_ELEMENT - This is used to get the lower or upper (determined by
203     // a Constant, which is required to be operand #1) half of the integer or
204     // float value specified as operand #0.  This is only for use before
205     // legalization, for values that will be broken into multiple registers.
206     EXTRACT_ELEMENT,
207
208     // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
209     // two values of the same integer value type, this produces a value twice as
210     // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
211     BUILD_PAIR,
212
213     // MERGE_VALUES - This node takes multiple discrete operands and returns
214     // them all as its individual results.  This nodes has exactly the same
215     // number of inputs and outputs, and is only valid before legalization.
216     // This node is useful for some pieces of the code generator that want to
217     // think about a single node with multiple results, not multiple nodes.
218     MERGE_VALUES,
219
220     // Simple integer binary arithmetic operators.
221     ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
222
223     // SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing
224     // a signed/unsigned value of type i[2*N], and return the full value as
225     // two results, each of type iN.
226     SMUL_LOHI, UMUL_LOHI,
227
228     // SDIVREM/UDIVREM - Divide two integers and produce both a quotient and
229     // remainder result.
230     SDIVREM, UDIVREM,
231     
232     // CARRY_FALSE - This node is used when folding other nodes,
233     // like ADDC/SUBC, which indicate the carry result is always false.
234     CARRY_FALSE,
235     
236     // Carry-setting nodes for multiple precision addition and subtraction.
237     // These nodes take two operands of the same value type, and produce two
238     // results.  The first result is the normal add or sub result, the second
239     // result is the carry flag result.
240     ADDC, SUBC,
241     
242     // Carry-using nodes for multiple precision addition and subtraction.  These
243     // nodes take three operands: The first two are the normal lhs and rhs to
244     // the add or sub, and the third is the input carry flag.  These nodes
245     // produce two results; the normal result of the add or sub, and the output
246     // carry flag.  These nodes both read and write a carry flag to allow them
247     // to them to be chained together for add and sub of arbitrarily large
248     // values.
249     ADDE, SUBE,
250
251     // RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
252     // These nodes take two operands: the normal LHS and RHS to the add. They
253     // produce two results: the normal result of the add, and a boolean that
254     // indicates if an overflow occured (*not* a flag, because it may be stored
255     // to memory, etc.).  If the type of the boolean is not i1 then the high
256     // bits conform to getBooleanContents.
257     // These nodes are generated from the llvm.[su]add.with.overflow intrinsics.
258     SADDO, UADDO,
259
260     // Same for subtraction
261     SSUBO, USUBO,
262
263     // Same for multiplication
264     SMULO, UMULO,
265
266     // Simple binary floating point operators.
267     FADD, FSUB, FMUL, FDIV, FREM,
268
269     // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.  NOTE: This
270     // DAG node does not require that X and Y have the same type, just that they
271     // are both floating point.  X and the result must have the same type.
272     // FCOPYSIGN(f32, f64) is allowed.
273     FCOPYSIGN,
274
275     // INT = FGETSIGN(FP) - Return the sign bit of the specified floating point
276     // value as an integer 0/1 value.
277     FGETSIGN,
278     
279     /// BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector
280     /// with the specified, possibly variable, elements.  The number of elements
281     /// is required to be a power of two.
282     BUILD_VECTOR,
283     
284     /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element
285     /// at IDX replaced with VAL.  If the type of VAL is larger than the vector
286     /// element type then VAL is truncated before replacement.
287     INSERT_VECTOR_ELT,
288
289     /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
290     /// identified by the (potentially variable) element number IDX.
291     EXTRACT_VECTOR_ELT,
292     
293     /// CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of
294     /// vector type with the same length and element type, this produces a
295     /// concatenated vector result value, with length equal to the sum of the
296     /// lengths of the input vectors.
297     CONCAT_VECTORS,
298     
299     /// EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an
300     /// vector value) starting with the (potentially variable) element number
301     /// IDX, which must be a multiple of the result vector length.
302     EXTRACT_SUBVECTOR,
303
304     /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
305     /// type as VEC1/VEC2.  SHUFFLEVEC is a BUILD_VECTOR of constant int values
306     /// (maybe of an illegal datatype) or undef that indicate which value each
307     /// result element will get.  The elements of VEC1/VEC2 are enumerated in
308     /// order.  This is quite similar to the Altivec 'vperm' instruction, except
309     /// that the indices must be constants and are in terms of the element size
310     /// of VEC1/VEC2, not in terms of bytes.
311     VECTOR_SHUFFLE,
312
313     /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
314     /// scalar value into element 0 of the resultant vector type.  The top
315     /// elements 1 to N-1 of the N-element vector are undefined.
316     SCALAR_TO_VECTOR,
317     
318     // EXTRACT_SUBREG - This node is used to extract a sub-register value. 
319     // This node takes a superreg and a constant sub-register index as operands.
320     // Note sub-register indices must be increasing. That is, if the
321     // sub-register index of a 8-bit sub-register is N, then the index for a
322     // 16-bit sub-register must be at least N+1.
323     EXTRACT_SUBREG,
324     
325     // INSERT_SUBREG - This node is used to insert a sub-register value. 
326     // This node takes a superreg, a subreg value, and a constant sub-register
327     // index as operands.
328     INSERT_SUBREG,
329     
330     // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
331     // an unsigned/signed value of type i[2*N], then return the top part.
332     MULHU, MULHS,
333
334     // Bitwise operators - logical and, logical or, logical xor, shift left,
335     // shift right algebraic (shift in sign bits), shift right logical (shift in
336     // zeroes), rotate left, rotate right, and byteswap.
337     AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
338
339     // Counting operators
340     CTTZ, CTLZ, CTPOP,
341
342     // Select(COND, TRUEVAL, FALSEVAL).  If the type of the boolean COND is not
343     // i1 then the high bits must conform to getBooleanContents.
344     SELECT,
345
346     // Select with condition operator - This selects between a true value and 
347     // a false value (ops #2 and #3) based on the boolean result of comparing
348     // the lhs and rhs (ops #0 and #1) of a conditional expression with the 
349     // condition code in op #4, a CondCodeSDNode.
350     SELECT_CC,
351
352     // SetCC operator - This evaluates to a true value iff the condition is
353     // true.  If the result value type is not i1 then the high bits conform
354     // to getBooleanContents.  The operands to this are the left and right
355     // operands to compare (ops #0, and #1) and the condition code to compare
356     // them with (op #2) as a CondCodeSDNode.
357     SETCC,
358
359     // Vector SetCC operator - This evaluates to a vector of integer elements
360     // with the high bit in each element set to true if the comparison is true
361     // and false if the comparison is false.  All other bits in each element 
362     // are undefined.  The operands to this are the left and right operands
363     // to compare (ops #0, and #1) and the condition code to compare them with
364     // (op #2) as a CondCodeSDNode.
365     VSETCC,
366
367     // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
368     // integer shift operations, just like ADD/SUB_PARTS.  The operation
369     // ordering is:
370     //       [Lo,Hi] = op [LoLHS,HiLHS], Amt
371     SHL_PARTS, SRA_PARTS, SRL_PARTS,
372
373     // Conversion operators.  These are all single input single output
374     // operations.  For all of these, the result type must be strictly
375     // wider or narrower (depending on the operation) than the source
376     // type.
377
378     // SIGN_EXTEND - Used for integer types, replicating the sign bit
379     // into new bits.
380     SIGN_EXTEND,
381
382     // ZERO_EXTEND - Used for integer types, zeroing the new bits.
383     ZERO_EXTEND,
384
385     // ANY_EXTEND - Used for integer types.  The high bits are undefined.
386     ANY_EXTEND,
387     
388     // TRUNCATE - Completely drop the high bits.
389     TRUNCATE,
390
391     // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
392     // depends on the first letter) to floating point.
393     SINT_TO_FP,
394     UINT_TO_FP,
395
396     // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
397     // sign extend a small value in a large integer register (e.g. sign
398     // extending the low 8 bits of a 32-bit register to fill the top 24 bits
399     // with the 7th bit).  The size of the smaller type is indicated by the 1th
400     // operand, a ValueType node.
401     SIGN_EXTEND_INREG,
402
403     /// FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
404     /// integer.
405     FP_TO_SINT,
406     FP_TO_UINT,
407
408     /// X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type
409     /// down to the precision of the destination VT.  TRUNC is a flag, which is
410     /// always an integer that is zero or one.  If TRUNC is 0, this is a
411     /// normal rounding, if it is 1, this FP_ROUND is known to not change the
412     /// value of Y.
413     ///
414     /// The TRUNC = 1 case is used in cases where we know that the value will
415     /// not be modified by the node, because Y is not using any of the extra
416     /// precision of source type.  This allows certain transformations like
417     /// FP_EXTEND(FP_ROUND(X,1)) -> X which are not safe for 
418     /// FP_EXTEND(FP_ROUND(X,0)) because the extra bits aren't removed.
419     FP_ROUND,
420     
421     // FLT_ROUNDS_ - Returns current rounding mode:
422     // -1 Undefined
423     //  0 Round to 0
424     //  1 Round to nearest
425     //  2 Round to +inf
426     //  3 Round to -inf
427     FLT_ROUNDS_,
428
429     /// X = FP_ROUND_INREG(Y, VT) - This operator takes an FP register, and
430     /// rounds it to a floating point value.  It then promotes it and returns it
431     /// in a register of the same size.  This operation effectively just
432     /// discards excess precision.  The type to round down to is specified by
433     /// the VT operand, a VTSDNode.
434     FP_ROUND_INREG,
435
436     /// X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
437     FP_EXTEND,
438
439     // BIT_CONVERT - Theis operator converts between integer and FP values, as
440     // if one was stored to memory as integer and the other was loaded from the
441     // same address (or equivalently for vector format conversions, etc).  The 
442     // source and result are required to have the same bit size (e.g. 
443     // f32 <-> i32).  This can also be used for int-to-int or fp-to-fp 
444     // conversions, but that is a noop, deleted by getNode().
445     BIT_CONVERT,
446     
447     // CONVERT_RNDSAT - This operator is used to support various conversions
448     // between various types (float, signed, unsigned and vectors of those
449     // types) with rounding and saturation. NOTE: Avoid using this operator as
450     // most target don't support it and the operator might be removed in the
451     // future. It takes the following arguments:
452     //   0) value
453     //   1) dest type (type to convert to)
454     //   2) src type (type to convert from)
455     //   3) rounding imm
456     //   4) saturation imm
457     //   5) ISD::CvtCode indicating the type of conversion to do
458     CONVERT_RNDSAT,
459     
460     // FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW,
461     // FLOG, FLOG2, FLOG10, FEXP, FEXP2,
462     // FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR - Perform various unary floating
463     // point operations. These are inspired by libm.
464     FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW,
465     FLOG, FLOG2, FLOG10, FEXP, FEXP2,
466     FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR,
467     
468     // LOAD and STORE have token chains as their first operand, then the same
469     // operands as an LLVM load/store instruction, then an offset node that
470     // is added / subtracted from the base pointer to form the address (for
471     // indexed memory ops).
472     LOAD, STORE,
473
474     // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
475     // to a specified boundary.  This node always has two return values: a new
476     // stack pointer value and a chain. The first operand is the token chain,
477     // the second is the number of bytes to allocate, and the third is the
478     // alignment boundary.  The size is guaranteed to be a multiple of the stack
479     // alignment, and the alignment is guaranteed to be bigger than the stack
480     // alignment (if required) or 0 to get standard stack alignment.
481     DYNAMIC_STACKALLOC,
482
483     // Control flow instructions.  These all have token chains.
484
485     // BR - Unconditional branch.  The first operand is the chain
486     // operand, the second is the MBB to branch to.
487     BR,
488
489     // BRIND - Indirect branch.  The first operand is the chain, the second
490     // is the value to branch to, which must be of the same type as the target's
491     // pointer type.
492     BRIND,
493
494     // BR_JT - Jumptable branch. The first operand is the chain, the second
495     // is the jumptable index, the last one is the jumptable entry index.
496     BR_JT,
497
498     // BRCOND - Conditional branch.  The first operand is the chain, the
499     // second is the condition, the third is the block to branch to if the
500     // condition is true.  If the type of the condition is not i1, then the
501     // high bits must conform to getBooleanContents.
502     BRCOND,
503
504     // BR_CC - Conditional branch.  The behavior is like that of SELECT_CC, in
505     // that the condition is represented as condition code, and two nodes to
506     // compare, rather than as a combined SetCC node.  The operands in order are
507     // chain, cc, lhs, rhs, block to branch to if condition is true.
508     BR_CC,
509     
510     // RET - Return from function.  The first operand is the chain,
511     // and any subsequent operands are pairs of return value and return value
512     // attributes (see CALL for description of attributes) for the function.
513     // This operation can have variable number of operands.
514     RET,
515
516     // INLINEASM - Represents an inline asm block.  This node always has two
517     // return values: a chain and a flag result.  The inputs are as follows:
518     //   Operand #0   : Input chain.
519     //   Operand #1   : a ExternalSymbolSDNode with a pointer to the asm string.
520     //   Operand #2n+2: A RegisterNode.
521     //   Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
522     //   Operand #last: Optional, an incoming flag.
523     INLINEASM,
524     
525     // DBG_LABEL, EH_LABEL - Represents a label in mid basic block used to track
526     // locations needed for debug and exception handling tables.  These nodes
527     // take a chain as input and return a chain.
528     DBG_LABEL,
529     EH_LABEL,
530
531     // DECLARE - Represents a llvm.dbg.declare intrinsic. It's used to track
532     // local variable declarations for debugging information. First operand is
533     // a chain, while the next two operands are first two arguments (address
534     // and variable) of a llvm.dbg.declare instruction.
535     DECLARE,
536     
537     // STACKSAVE - STACKSAVE has one operand, an input chain.  It produces a
538     // value, the same type as the pointer type for the system, and an output
539     // chain.
540     STACKSAVE,
541     
542     // STACKRESTORE has two operands, an input chain and a pointer to restore to
543     // it returns an output chain.
544     STACKRESTORE,
545     
546     // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
547     // a call sequence, and carry arbitrary information that target might want
548     // to know.  The first operand is a chain, the rest are specified by the
549     // target and not touched by the DAG optimizers.
550     // CALLSEQ_START..CALLSEQ_END pairs may not be nested.
551     CALLSEQ_START,  // Beginning of a call sequence
552     CALLSEQ_END,    // End of a call sequence
553     
554     // VAARG - VAARG has three operands: an input chain, a pointer, and a 
555     // SRCVALUE.  It returns a pair of values: the vaarg value and a new chain.
556     VAARG,
557     
558     // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
559     // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
560     // source.
561     VACOPY,
562     
563     // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
564     // pointer, and a SRCVALUE.
565     VAEND, VASTART,
566
567     // SRCVALUE - This is a node type that holds a Value* that is used to
568     // make reference to a value in the LLVM IR.
569     SRCVALUE,
570
571     // MEMOPERAND - This is a node that contains a MachineMemOperand which
572     // records information about a memory reference. This is used to make
573     // AliasAnalysis queries from the backend.
574     MEMOPERAND,
575
576     // PCMARKER - This corresponds to the pcmarker intrinsic.
577     PCMARKER,
578
579     // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
580     // The only operand is a chain and a value and a chain are produced.  The
581     // value is the contents of the architecture specific cycle counter like 
582     // register (or other high accuracy low latency clock source)
583     READCYCLECOUNTER,
584
585     // HANDLENODE node - Used as a handle for various purposes.
586     HANDLENODE,
587
588     // DBG_STOPPOINT - This node is used to represent a source location for
589     // debug info.  It takes token chain as input, and carries a line number,
590     // column number, and a pointer to a CompileUnit object identifying
591     // the containing compilation unit.  It produces a token chain as output.
592     DBG_STOPPOINT,
593     
594     // DEBUG_LOC - This node is used to represent source line information
595     // embedded in the code.  It takes a token chain as input, then a line
596     // number, then a column then a file id (provided by MachineModuleInfo.) It
597     // produces a token chain as output.
598     DEBUG_LOC,
599
600     // TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
601     // It takes as input a token chain, the pointer to the trampoline,
602     // the pointer to the nested function, the pointer to pass for the
603     // 'nest' parameter, a SRCVALUE for the trampoline and another for
604     // the nested function (allowing targets to access the original
605     // Function*).  It produces the result of the intrinsic and a token
606     // chain as output.
607     TRAMPOLINE,
608
609     // TRAP - Trapping instruction
610     TRAP,
611
612     // PREFETCH - This corresponds to a prefetch intrinsic. It takes chains are
613     // their first operand. The other operands are the address to prefetch,
614     // read / write specifier, and locality specifier.
615     PREFETCH,
616
617     // OUTCHAIN = MEMBARRIER(INCHAIN, load-load, load-store, store-load, 
618     //                       store-store, device)
619     // This corresponds to the memory.barrier intrinsic.
620     // it takes an input chain, 4 operands to specify the type of barrier, an
621     // operand specifying if the barrier applies to device and uncached memory
622     // and produces an output chain.
623     MEMBARRIER,
624
625     // Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap)
626     // this corresponds to the atomic.lcs intrinsic.
627     // cmp is compared to *ptr, and if equal, swap is stored in *ptr.
628     // the return is always the original value in *ptr
629     ATOMIC_CMP_SWAP,
630
631     // Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt)
632     // this corresponds to the atomic.swap intrinsic.
633     // amt is stored to *ptr atomically.
634     // the return is always the original value in *ptr
635     ATOMIC_SWAP,
636
637     // Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN, ptr, amt)
638     // this corresponds to the atomic.load.[OpName] intrinsic.
639     // op(*ptr, amt) is stored to *ptr atomically.
640     // the return is always the original value in *ptr
641     ATOMIC_LOAD_ADD,
642     ATOMIC_LOAD_SUB,
643     ATOMIC_LOAD_AND,
644     ATOMIC_LOAD_OR,
645     ATOMIC_LOAD_XOR,
646     ATOMIC_LOAD_NAND,
647     ATOMIC_LOAD_MIN,
648     ATOMIC_LOAD_MAX,
649     ATOMIC_LOAD_UMIN,
650     ATOMIC_LOAD_UMAX,
651     
652     // BUILTIN_OP_END - This must be the last enum value in this list.
653     BUILTIN_OP_END
654   };
655
656   /// Node predicates
657
658   /// isBuildVectorAllOnes - Return true if the specified node is a
659   /// BUILD_VECTOR where all of the elements are ~0 or undef.
660   bool isBuildVectorAllOnes(const SDNode *N);
661
662   /// isBuildVectorAllZeros - Return true if the specified node is a
663   /// BUILD_VECTOR where all of the elements are 0 or undef.
664   bool isBuildVectorAllZeros(const SDNode *N);
665
666   /// isScalarToVector - Return true if the specified node is a
667   /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
668   /// element is not an undef.
669   bool isScalarToVector(const SDNode *N);
670
671   /// isDebugLabel - Return true if the specified node represents a debug
672   /// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
673   bool isDebugLabel(const SDNode *N);
674   
675   //===--------------------------------------------------------------------===//
676   /// MemIndexedMode enum - This enum defines the load / store indexed 
677   /// addressing modes.
678   ///
679   /// UNINDEXED    "Normal" load / store. The effective address is already
680   ///              computed and is available in the base pointer. The offset
681   ///              operand is always undefined. In addition to producing a
682   ///              chain, an unindexed load produces one value (result of the
683   ///              load); an unindexed store does not produce a value.
684   ///
685   /// PRE_INC      Similar to the unindexed mode where the effective address is
686   /// PRE_DEC      the value of the base pointer add / subtract the offset.
687   ///              It considers the computation as being folded into the load /
688   ///              store operation (i.e. the load / store does the address
689   ///              computation as well as performing the memory transaction).
690   ///              The base operand is always undefined. In addition to
691   ///              producing a chain, pre-indexed load produces two values
692   ///              (result of the load and the result of the address
693   ///              computation); a pre-indexed store produces one value (result
694   ///              of the address computation).
695   ///
696   /// POST_INC     The effective address is the value of the base pointer. The
697   /// POST_DEC     value of the offset operand is then added to / subtracted
698   ///              from the base after memory transaction. In addition to
699   ///              producing a chain, post-indexed load produces two values
700   ///              (the result of the load and the result of the base +/- offset
701   ///              computation); a post-indexed store produces one value (the
702   ///              the result of the base +/- offset computation).
703   ///
704   enum MemIndexedMode {
705     UNINDEXED = 0,
706     PRE_INC,
707     PRE_DEC,
708     POST_INC,
709     POST_DEC,
710     LAST_INDEXED_MODE
711   };
712
713   //===--------------------------------------------------------------------===//
714   /// LoadExtType enum - This enum defines the three variants of LOADEXT
715   /// (load with extension).
716   ///
717   /// SEXTLOAD loads the integer operand and sign extends it to a larger
718   ///          integer result type.
719   /// ZEXTLOAD loads the integer operand and zero extends it to a larger
720   ///          integer result type.
721   /// EXTLOAD  is used for three things: floating point extending loads, 
722   ///          integer extending loads [the top bits are undefined], and vector
723   ///          extending loads [load into low elt].
724   ///
725   enum LoadExtType {
726     NON_EXTLOAD = 0,
727     EXTLOAD,
728     SEXTLOAD,
729     ZEXTLOAD,
730     LAST_LOADEXT_TYPE
731   };
732
733   //===--------------------------------------------------------------------===//
734   /// ISD::CondCode enum - These are ordered carefully to make the bitfields
735   /// below work out, when considering SETFALSE (something that never exists
736   /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
737   /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
738   /// to.  If the "N" column is 1, the result of the comparison is undefined if
739   /// the input is a NAN.
740   ///
741   /// All of these (except for the 'always folded ops') should be handled for
742   /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
743   /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
744   ///
745   /// Note that these are laid out in a specific order to allow bit-twiddling
746   /// to transform conditions.
747   enum CondCode {
748     // Opcode          N U L G E       Intuitive operation
749     SETFALSE,      //    0 0 0 0       Always false (always folded)
750     SETOEQ,        //    0 0 0 1       True if ordered and equal
751     SETOGT,        //    0 0 1 0       True if ordered and greater than
752     SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
753     SETOLT,        //    0 1 0 0       True if ordered and less than
754     SETOLE,        //    0 1 0 1       True if ordered and less than or equal
755     SETONE,        //    0 1 1 0       True if ordered and operands are unequal
756     SETO,          //    0 1 1 1       True if ordered (no nans)
757     SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
758     SETUEQ,        //    1 0 0 1       True if unordered or equal
759     SETUGT,        //    1 0 1 0       True if unordered or greater than
760     SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
761     SETULT,        //    1 1 0 0       True if unordered or less than
762     SETULE,        //    1 1 0 1       True if unordered, less than, or equal
763     SETUNE,        //    1 1 1 0       True if unordered or not equal
764     SETTRUE,       //    1 1 1 1       Always true (always folded)
765     // Don't care operations: undefined if the input is a nan.
766     SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
767     SETEQ,         //  1 X 0 0 1       True if equal
768     SETGT,         //  1 X 0 1 0       True if greater than
769     SETGE,         //  1 X 0 1 1       True if greater than or equal
770     SETLT,         //  1 X 1 0 0       True if less than
771     SETLE,         //  1 X 1 0 1       True if less than or equal
772     SETNE,         //  1 X 1 1 0       True if not equal
773     SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
774
775     SETCC_INVALID       // Marker value.
776   };
777
778   /// isSignedIntSetCC - Return true if this is a setcc instruction that
779   /// performs a signed comparison when used with integer operands.
780   inline bool isSignedIntSetCC(CondCode Code) {
781     return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
782   }
783
784   /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
785   /// performs an unsigned comparison when used with integer operands.
786   inline bool isUnsignedIntSetCC(CondCode Code) {
787     return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
788   }
789
790   /// isTrueWhenEqual - Return true if the specified condition returns true if
791   /// the two operands to the condition are equal.  Note that if one of the two
792   /// operands is a NaN, this value is meaningless.
793   inline bool isTrueWhenEqual(CondCode Cond) {
794     return ((int)Cond & 1) != 0;
795   }
796
797   /// getUnorderedFlavor - This function returns 0 if the condition is always
798   /// false if an operand is a NaN, 1 if the condition is always true if the
799   /// operand is a NaN, and 2 if the condition is undefined if the operand is a
800   /// NaN.
801   inline unsigned getUnorderedFlavor(CondCode Cond) {
802     return ((int)Cond >> 3) & 3;
803   }
804
805   /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
806   /// 'op' is a valid SetCC operation.
807   CondCode getSetCCInverse(CondCode Operation, bool isInteger);
808
809   /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
810   /// when given the operation for (X op Y).
811   CondCode getSetCCSwappedOperands(CondCode Operation);
812
813   /// getSetCCOrOperation - Return the result of a logical OR between different
814   /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
815   /// function returns SETCC_INVALID if it is not possible to represent the
816   /// resultant comparison.
817   CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
818
819   /// getSetCCAndOperation - Return the result of a logical AND between
820   /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
821   /// function returns SETCC_INVALID if it is not possible to represent the
822   /// resultant comparison.
823   CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
824
825   //===--------------------------------------------------------------------===//
826   /// CvtCode enum - This enum defines the various converts CONVERT_RNDSAT 
827   /// supports.
828   enum CvtCode {
829     CVT_FF,     // Float from Float
830     CVT_FS,     // Float from Signed
831     CVT_FU,     // Float from Unsigned
832     CVT_SF,     // Signed from Float
833     CVT_UF,     // Unsigned from Float
834     CVT_SS,     // Signed from Signed
835     CVT_SU,     // Signed from Unsigned
836     CVT_US,     // Unsigned from Signed
837     CVT_UU,     // Unsigned from Unsigned
838     CVT_INVALID // Marker - Invalid opcode
839   };
840 }  // end llvm::ISD namespace
841
842
843 //===----------------------------------------------------------------------===//
844 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
845 /// values as the result of a computation.  Many nodes return multiple values,
846 /// from loads (which define a token and a return value) to ADDC (which returns
847 /// a result and a carry value), to calls (which may return an arbitrary number
848 /// of values).
849 ///
850 /// As such, each use of a SelectionDAG computation must indicate the node that
851 /// computes it as well as which return value to use from that node.  This pair
852 /// of information is represented with the SDValue value type.
853 ///
854 class SDValue {
855   SDNode *Node;       // The node defining the value we are using.
856   unsigned ResNo;     // Which return value of the node we are using.
857 public:
858   SDValue() : Node(0), ResNo(0) {}
859   SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
860
861   /// get the index which selects a specific result in the SDNode
862   unsigned getResNo() const { return ResNo; }
863
864   /// get the SDNode which holds the desired result
865   SDNode *getNode() const { return Node; }
866
867   /// set the SDNode
868   void setNode(SDNode *N) { Node = N; }
869
870   bool operator==(const SDValue &O) const {
871     return Node == O.Node && ResNo == O.ResNo;
872   }
873   bool operator!=(const SDValue &O) const {
874     return !operator==(O);
875   }
876   bool operator<(const SDValue &O) const {
877     return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
878   }
879
880   SDValue getValue(unsigned R) const {
881     return SDValue(Node, R);
882   }
883
884   // isOperandOf - Return true if this node is an operand of N.
885   bool isOperandOf(SDNode *N) const;
886
887   /// getValueType - Return the ValueType of the referenced return value.
888   ///
889   inline MVT getValueType() const;
890
891   /// getValueSizeInBits - Returns the size of the value in bits.
892   ///
893   unsigned getValueSizeInBits() const {
894     return getValueType().getSizeInBits();
895   }
896
897   // Forwarding methods - These forward to the corresponding methods in SDNode.
898   inline unsigned getOpcode() const;
899   inline unsigned getNumOperands() const;
900   inline const SDValue &getOperand(unsigned i) const;
901   inline uint64_t getConstantOperandVal(unsigned i) const;
902   inline bool isTargetOpcode() const;
903   inline bool isMachineOpcode() const;
904   inline unsigned getMachineOpcode() const;
905   inline const DebugLoc getDebugLoc() const;
906
907   
908   /// reachesChainWithoutSideEffects - Return true if this operand (which must
909   /// be a chain) reaches the specified operand without crossing any 
910   /// side-effecting instructions.  In practice, this looks through token
911   /// factors and non-volatile loads.  In order to remain efficient, this only
912   /// looks a couple of nodes in, it does not do an exhaustive search.
913   bool reachesChainWithoutSideEffects(SDValue Dest, 
914                                       unsigned Depth = 2) const;
915   
916   /// use_empty - Return true if there are no nodes using value ResNo
917   /// of Node.
918   ///
919   inline bool use_empty() const;
920
921   /// hasOneUse - Return true if there is exactly one node using value
922   /// ResNo of Node.
923   ///
924   inline bool hasOneUse() const;
925 };
926
927
928 template<> struct DenseMapInfo<SDValue> {
929   static inline SDValue getEmptyKey() { 
930     return SDValue((SDNode*)-1, -1U); 
931   }
932   static inline SDValue getTombstoneKey() { 
933     return SDValue((SDNode*)-1, 0);
934   }
935   static unsigned getHashValue(const SDValue &Val) {
936     return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
937             (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
938   }
939   static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
940     return LHS == RHS;
941   }
942   static bool isPod() { return true; }
943 };
944
945 /// simplify_type specializations - Allow casting operators to work directly on
946 /// SDValues as if they were SDNode*'s.
947 template<> struct simplify_type<SDValue> {
948   typedef SDNode* SimpleType;
949   static SimpleType getSimplifiedValue(const SDValue &Val) {
950     return static_cast<SimpleType>(Val.getNode());
951   }
952 };
953 template<> struct simplify_type<const SDValue> {
954   typedef SDNode* SimpleType;
955   static SimpleType getSimplifiedValue(const SDValue &Val) {
956     return static_cast<SimpleType>(Val.getNode());
957   }
958 };
959
960 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
961 /// which records the SDNode being used and the result number, a
962 /// pointer to the SDNode using the value, and Next and Prev pointers,
963 /// which link together all the uses of an SDNode.
964 ///
965 class SDUse {
966   /// Val - The value being used.
967   SDValue Val;
968   /// User - The user of this value.
969   SDNode *User;
970   /// Prev, Next - Pointers to the uses list of the SDNode referred by 
971   /// this operand.
972   SDUse **Prev, *Next;
973
974   SDUse(const SDUse &U);          // Do not implement
975   void operator=(const SDUse &U); // Do not implement
976
977 public:
978   SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
979
980   /// Normally SDUse will just implicitly convert to an SDValue that it holds.
981   operator const SDValue&() const { return Val; }
982
983   /// If implicit conversion to SDValue doesn't work, the get() method returns
984   /// the SDValue.
985   const SDValue &get() const { return Val; }
986   
987   /// getUser - This returns the SDNode that contains this Use.
988   SDNode *getUser() { return User; }
989
990   /// getNext - Get the next SDUse in the use list.
991   SDUse *getNext() const { return Next; }
992
993   /// getNode - Convenience function for get().getNode().
994   SDNode *getNode() const { return Val.getNode(); }
995   /// getResNo - Convenience function for get().getResNo().
996   unsigned getResNo() const { return Val.getResNo(); }
997   /// getValueType - Convenience function for get().getValueType().
998   MVT getValueType() const { return Val.getValueType(); }
999
1000   /// operator== - Convenience function for get().operator==
1001   bool operator==(const SDValue &V) const {
1002     return Val == V;
1003   }
1004   
1005   /// operator!= - Convenience function for get().operator!=
1006   bool operator!=(const SDValue &V) const {
1007     return Val != V;
1008   }
1009
1010   /// operator< - Convenience function for get().operator<
1011   bool operator<(const SDValue &V) const {
1012     return Val < V;
1013   }
1014
1015 private:
1016   friend class SelectionDAG;
1017   friend class SDNode;
1018
1019   void setUser(SDNode *p) { User = p; }
1020
1021   /// set - Remove this use from its existing use list, assign it the
1022   /// given value, and add it to the new value's node's use list.
1023   inline void set(const SDValue &V);
1024   /// setInitial - like set, but only supports initializing a newly-allocated
1025   /// SDUse with a non-null value.
1026   inline void setInitial(const SDValue &V);
1027   /// setNode - like set, but only sets the Node portion of the value,
1028   /// leaving the ResNo portion unmodified.
1029   inline void setNode(SDNode *N);
1030
1031   void addToList(SDUse **List) {
1032     Next = *List;
1033     if (Next) Next->Prev = &Next;
1034     Prev = List;
1035     *List = this;
1036   }
1037
1038   void removeFromList() {
1039     *Prev = Next;
1040     if (Next) Next->Prev = Prev;
1041   }
1042 };
1043
1044 /// simplify_type specializations - Allow casting operators to work directly on
1045 /// SDValues as if they were SDNode*'s.
1046 template<> struct simplify_type<SDUse> {
1047   typedef SDNode* SimpleType;
1048   static SimpleType getSimplifiedValue(const SDUse &Val) {
1049     return static_cast<SimpleType>(Val.getNode());
1050   }
1051 };
1052 template<> struct simplify_type<const SDUse> {
1053   typedef SDNode* SimpleType;
1054   static SimpleType getSimplifiedValue(const SDUse &Val) {
1055     return static_cast<SimpleType>(Val.getNode());
1056   }
1057 };
1058
1059
1060 /// SDNode - Represents one node in the SelectionDAG.
1061 ///
1062 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
1063 private:
1064   /// NodeType - The operation that this node performs.
1065   ///
1066   short NodeType;
1067   
1068   /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
1069   /// then they will be delete[]'d when the node is destroyed.
1070   unsigned short OperandsNeedDelete : 1;
1071
1072 protected:
1073   /// SubclassData - This member is defined by this class, but is not used for
1074   /// anything.  Subclasses can use it to hold whatever state they find useful.
1075   /// This field is initialized to zero by the ctor.
1076   unsigned short SubclassData : 15;
1077
1078 private:
1079   /// NodeId - Unique id per SDNode in the DAG.
1080   int NodeId;
1081
1082   /// debugLoc - source line information.
1083   DebugLoc debugLoc;
1084
1085   /// OperandList - The values that are used by this operation.
1086   ///
1087   SDUse *OperandList;
1088   
1089   /// ValueList - The types of the values this node defines.  SDNode's may
1090   /// define multiple values simultaneously.
1091   const MVT *ValueList;
1092
1093   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
1094   unsigned short NumOperands, NumValues;
1095   
1096   /// UseList - List of uses for this SDNode.
1097   SDUse *UseList;
1098
1099   /// getValueTypeList - Return a pointer to the specified value type.
1100   static const MVT *getValueTypeList(MVT VT);
1101
1102   friend class SelectionDAG;
1103   friend struct ilist_traits<SDNode>;
1104
1105 public:
1106   //===--------------------------------------------------------------------===//
1107   //  Accessors
1108   //
1109
1110   /// getOpcode - Return the SelectionDAG opcode value for this node. For
1111   /// pre-isel nodes (those for which isMachineOpcode returns false), these
1112   /// are the opcode values in the ISD and <target>ISD namespaces. For
1113   /// post-isel opcodes, see getMachineOpcode.
1114   unsigned getOpcode()  const { return (unsigned short)NodeType; }
1115
1116   /// isTargetOpcode - Test if this node has a target-specific opcode (in the
1117   /// <target>ISD namespace).
1118   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
1119
1120   /// isMachineOpcode - Test if this node has a post-isel opcode, directly
1121   /// corresponding to a MachineInstr opcode.
1122   bool isMachineOpcode() const { return NodeType < 0; }
1123
1124   /// getMachineOpcode - This may only be called if isMachineOpcode returns
1125   /// true. It returns the MachineInstr opcode value that the node's opcode
1126   /// corresponds to.
1127   unsigned getMachineOpcode() const {
1128     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
1129     return ~NodeType;
1130   }
1131
1132   /// use_empty - Return true if there are no uses of this node.
1133   ///
1134   bool use_empty() const { return UseList == NULL; }
1135
1136   /// hasOneUse - Return true if there is exactly one use of this node.
1137   ///
1138   bool hasOneUse() const {
1139     return !use_empty() && next(use_begin()) == use_end();
1140   }
1141
1142   /// use_size - Return the number of uses of this node. This method takes
1143   /// time proportional to the number of uses.
1144   ///
1145   size_t use_size() const { return std::distance(use_begin(), use_end()); }
1146
1147   /// getNodeId - Return the unique node id.
1148   ///
1149   int getNodeId() const { return NodeId; }
1150
1151   /// setNodeId - Set unique node id.
1152   void setNodeId(int Id) { NodeId = Id; }
1153
1154   /// getDebugLoc - Return the source location info.
1155   const DebugLoc getDebugLoc() const { return debugLoc; }
1156
1157   /// setDebugLoc - Set source location info.  Try to avoid this, putting
1158   /// it in the constructor is preferable.
1159   void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
1160
1161   /// use_iterator - This class provides iterator support for SDUse
1162   /// operands that use a specific SDNode. 
1163   class use_iterator
1164     : public forward_iterator<SDUse, ptrdiff_t> {
1165     SDUse *Op;
1166     explicit use_iterator(SDUse *op) : Op(op) {
1167     }
1168     friend class SDNode;
1169   public:
1170     typedef forward_iterator<SDUse, ptrdiff_t>::reference reference;
1171     typedef forward_iterator<SDUse, ptrdiff_t>::pointer pointer;
1172
1173     use_iterator(const use_iterator &I) : Op(I.Op) {}
1174     use_iterator() : Op(0) {}
1175
1176     bool operator==(const use_iterator &x) const {
1177       return Op == x.Op;
1178     }
1179     bool operator!=(const use_iterator &x) const {
1180       return !operator==(x);
1181     }
1182  
1183     /// atEnd - return true if this iterator is at the end of uses list.
1184     bool atEnd() const { return Op == 0; }
1185
1186     // Iterator traversal: forward iteration only.
1187     use_iterator &operator++() {          // Preincrement
1188       assert(Op && "Cannot increment end iterator!");
1189       Op = Op->getNext();
1190       return *this;
1191     }
1192
1193     use_iterator operator++(int) {        // Postincrement
1194       use_iterator tmp = *this; ++*this; return tmp;
1195     }
1196
1197     /// Retrieve a pointer to the current user node.
1198     SDNode *operator*() const {
1199       assert(Op && "Cannot dereference end iterator!");
1200       return Op->getUser();
1201     }
1202
1203     SDNode *operator->() const { return operator*(); }
1204
1205     SDUse &getUse() const { return *Op; }
1206
1207     /// getOperandNo - Retrieve the operand # of this use in its user.
1208     ///
1209     unsigned getOperandNo() const {
1210       assert(Op && "Cannot dereference end iterator!");
1211       return (unsigned)(Op - Op->getUser()->OperandList);
1212     }
1213   };
1214
1215   /// use_begin/use_end - Provide iteration support to walk over all uses
1216   /// of an SDNode.
1217
1218   use_iterator use_begin() const {
1219     return use_iterator(UseList);
1220   }
1221
1222   static use_iterator use_end() { return use_iterator(0); }
1223
1224
1225   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1226   /// indicated value.  This method ignores uses of other values defined by this
1227   /// operation.
1228   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
1229
1230   /// hasAnyUseOfValue - Return true if there are any use of the indicated
1231   /// value. This method ignores uses of other values defined by this operation.
1232   bool hasAnyUseOfValue(unsigned Value) const;
1233
1234   /// isOnlyUserOf - Return true if this node is the only use of N.
1235   ///
1236   bool isOnlyUserOf(SDNode *N) const;
1237
1238   /// isOperandOf - Return true if this node is an operand of N.
1239   ///
1240   bool isOperandOf(SDNode *N) const;
1241
1242   /// isPredecessorOf - Return true if this node is a predecessor of N. This
1243   /// node is either an operand of N or it can be reached by recursively
1244   /// traversing up the operands.
1245   /// NOTE: this is an expensive method. Use it carefully.
1246   bool isPredecessorOf(SDNode *N) const;
1247
1248   /// getNumOperands - Return the number of values used by this operation.
1249   ///
1250   unsigned getNumOperands() const { return NumOperands; }
1251
1252   /// getConstantOperandVal - Helper method returns the integer value of a 
1253   /// ConstantSDNode operand.
1254   uint64_t getConstantOperandVal(unsigned Num) const;
1255
1256   const SDValue &getOperand(unsigned Num) const {
1257     assert(Num < NumOperands && "Invalid child # of SDNode!");
1258     return OperandList[Num];
1259   }
1260
1261   typedef SDUse* op_iterator;
1262   op_iterator op_begin() const { return OperandList; }
1263   op_iterator op_end() const { return OperandList+NumOperands; }
1264
1265   SDVTList getVTList() const {
1266     SDVTList X = { ValueList, NumValues };
1267     return X;
1268   };
1269
1270   /// getFlaggedNode - If this node has a flag operand, return the node
1271   /// to which the flag operand points. Otherwise return NULL.
1272   SDNode *getFlaggedNode() const {
1273     if (getNumOperands() != 0 &&
1274         getOperand(getNumOperands()-1).getValueType() == MVT::Flag)
1275       return getOperand(getNumOperands()-1).getNode();
1276     return 0;
1277   }
1278
1279   // If this is a pseudo op, like copyfromreg, look to see if there is a
1280   // real target node flagged to it.  If so, return the target node.
1281   const SDNode *getFlaggedMachineNode() const {
1282     const SDNode *FoundNode = this;
1283
1284     // Climb up flag edges until a machine-opcode node is found, or the
1285     // end of the chain is reached.
1286     while (!FoundNode->isMachineOpcode()) {
1287       const SDNode *N = FoundNode->getFlaggedNode();
1288       if (!N) break;
1289       FoundNode = N;
1290     }
1291
1292     return FoundNode;
1293   }
1294
1295   /// getNumValues - Return the number of values defined/returned by this
1296   /// operator.
1297   ///
1298   unsigned getNumValues() const { return NumValues; }
1299
1300   /// getValueType - Return the type of a specified result.
1301   ///
1302   MVT getValueType(unsigned ResNo) const {
1303     assert(ResNo < NumValues && "Illegal result number!");
1304     return ValueList[ResNo];
1305   }
1306
1307   /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
1308   ///
1309   unsigned getValueSizeInBits(unsigned ResNo) const {
1310     return getValueType(ResNo).getSizeInBits();
1311   }
1312
1313   typedef const MVT* value_iterator;
1314   value_iterator value_begin() const { return ValueList; }
1315   value_iterator value_end() const { return ValueList+NumValues; }
1316
1317   /// getOperationName - Return the opcode of this operation for printing.
1318   ///
1319   std::string getOperationName(const SelectionDAG *G = 0) const;
1320   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
1321   void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
1322   void dump() const;
1323   void dump(const SelectionDAG *G) const;
1324
1325   static bool classof(const SDNode *) { return true; }
1326
1327   /// Profile - Gather unique data for the node.
1328   ///
1329   void Profile(FoldingSetNodeID &ID) const;
1330
1331   /// addUse - This method should only be used by the SDUse class.
1332   ///
1333   void addUse(SDUse &U) { U.addToList(&UseList); }
1334
1335 protected:
1336   static SDVTList getSDVTList(MVT VT) {
1337     SDVTList Ret = { getValueTypeList(VT), 1 };
1338     return Ret;
1339   }
1340
1341   /// The constructors that supply DebugLoc explicitly should be preferred
1342   /// for new code.
1343   SDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops, unsigned NumOps)
1344     : NodeType(Opc), OperandsNeedDelete(true), SubclassData(0),
1345       NodeId(-1), debugLoc(DebugLoc::getUnknownLoc()),
1346       OperandList(NumOps ? new SDUse[NumOps] : 0),
1347       ValueList(VTs.VTs),
1348       NumOperands(NumOps), NumValues(VTs.NumVTs),
1349       UseList(NULL) {
1350     for (unsigned i = 0; i != NumOps; ++i) {
1351       OperandList[i].setUser(this);
1352       OperandList[i].setInitial(Ops[i]);
1353     }
1354   }
1355
1356   /// This constructor adds no operands itself; operands can be
1357   /// set later with InitOperands.
1358   SDNode(unsigned Opc, SDVTList VTs)
1359     : NodeType(Opc), OperandsNeedDelete(false), SubclassData(0),
1360       NodeId(-1), debugLoc(DebugLoc::getUnknownLoc()), OperandList(0), 
1361       ValueList(VTs.VTs), NumOperands(0), NumValues(VTs.NumVTs),
1362       UseList(NULL) {}
1363
1364   /// The next two constructors specify DebugLoc explicitly; the intent
1365   /// is that they will replace the above two over time, and eventually
1366   /// the ones above can be removed.
1367   SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops, 
1368          unsigned NumOps)
1369     : NodeType(Opc), OperandsNeedDelete(true), SubclassData(0),
1370       NodeId(-1), debugLoc(dl),
1371       OperandList(NumOps ? new SDUse[NumOps] : 0),
1372       ValueList(VTs.VTs),
1373       NumOperands(NumOps), NumValues(VTs.NumVTs),
1374       UseList(NULL) {
1375     for (unsigned i = 0; i != NumOps; ++i) {
1376       OperandList[i].setUser(this);
1377       OperandList[i].setInitial(Ops[i]);
1378     }
1379   }
1380
1381   /// This constructor adds no operands itself; operands can be
1382   /// set later with InitOperands.
1383   SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
1384     : NodeType(Opc), OperandsNeedDelete(false), SubclassData(0),
1385       NodeId(-1), debugLoc(dl), OperandList(0),
1386       ValueList(VTs.VTs), NumOperands(0), NumValues(VTs.NumVTs),
1387       UseList(NULL) {}
1388   
1389   /// InitOperands - Initialize the operands list of this with 1 operand.
1390   void InitOperands(SDUse *Ops, const SDValue &Op0) {
1391     Ops[0].setUser(this);
1392     Ops[0].setInitial(Op0);
1393     NumOperands = 1;
1394     OperandList = Ops;
1395   }
1396
1397   /// InitOperands - Initialize the operands list of this with 2 operands.
1398   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
1399     Ops[0].setUser(this);
1400     Ops[0].setInitial(Op0);
1401     Ops[1].setUser(this);
1402     Ops[1].setInitial(Op1);
1403     NumOperands = 2;
1404     OperandList = Ops;
1405   }
1406
1407   /// InitOperands - Initialize the operands list of this with 3 operands.
1408   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
1409                     const SDValue &Op2) {
1410     Ops[0].setUser(this);
1411     Ops[0].setInitial(Op0);
1412     Ops[1].setUser(this);
1413     Ops[1].setInitial(Op1);
1414     Ops[2].setUser(this);
1415     Ops[2].setInitial(Op2);
1416     NumOperands = 3;
1417     OperandList = Ops;
1418   }
1419
1420   /// InitOperands - Initialize the operands list of this with 4 operands.
1421   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
1422                     const SDValue &Op2, const SDValue &Op3) {
1423     Ops[0].setUser(this);
1424     Ops[0].setInitial(Op0);
1425     Ops[1].setUser(this);
1426     Ops[1].setInitial(Op1);
1427     Ops[2].setUser(this);
1428     Ops[2].setInitial(Op2);
1429     Ops[3].setUser(this);
1430     Ops[3].setInitial(Op3);
1431     NumOperands = 4;
1432     OperandList = Ops;
1433   }
1434
1435   /// InitOperands - Initialize the operands list of this with N operands.
1436   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
1437     for (unsigned i = 0; i != N; ++i) {
1438       Ops[i].setUser(this);
1439       Ops[i].setInitial(Vals[i]);
1440     }
1441     NumOperands = N;
1442     OperandList = Ops;
1443   }
1444
1445   /// DropOperands - Release the operands and set this node to have
1446   /// zero operands.
1447   void DropOperands();
1448 };
1449
1450
1451 // Define inline functions from the SDValue class.
1452
1453 inline unsigned SDValue::getOpcode() const {
1454   return Node->getOpcode();
1455 }
1456 inline MVT SDValue::getValueType() const {
1457   return Node->getValueType(ResNo);
1458 }
1459 inline unsigned SDValue::getNumOperands() const {
1460   return Node->getNumOperands();
1461 }
1462 inline const SDValue &SDValue::getOperand(unsigned i) const {
1463   return Node->getOperand(i);
1464 }
1465 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1466   return Node->getConstantOperandVal(i);
1467 }
1468 inline bool SDValue::isTargetOpcode() const {
1469   return Node->isTargetOpcode();
1470 }
1471 inline bool SDValue::isMachineOpcode() const {
1472   return Node->isMachineOpcode();
1473 }
1474 inline unsigned SDValue::getMachineOpcode() const {
1475   return Node->getMachineOpcode();
1476 }
1477 inline bool SDValue::use_empty() const {
1478   return !Node->hasAnyUseOfValue(ResNo);
1479 }
1480 inline bool SDValue::hasOneUse() const {
1481   return Node->hasNUsesOfValue(1, ResNo);
1482 }
1483 inline const DebugLoc SDValue::getDebugLoc() const {
1484   return Node->getDebugLoc();
1485 }
1486
1487 // Define inline functions from the SDUse class.
1488
1489 inline void SDUse::set(const SDValue &V) {
1490   if (Val.getNode()) removeFromList();
1491   Val = V;
1492   if (V.getNode()) V.getNode()->addUse(*this);
1493 }
1494
1495 inline void SDUse::setInitial(const SDValue &V) {
1496   Val = V;
1497   V.getNode()->addUse(*this);
1498 }
1499
1500 inline void SDUse::setNode(SDNode *N) {
1501   if (Val.getNode()) removeFromList();
1502   Val.setNode(N);
1503   if (N) N->addUse(*this);
1504 }
1505
1506 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
1507 /// to allow co-allocation of node operands with the node itself.
1508 class UnarySDNode : public SDNode {
1509   SDUse Op;
1510 public:
1511   UnarySDNode(unsigned Opc, SDVTList VTs, SDValue X)
1512     : SDNode(Opc, VTs) {
1513     InitOperands(&Op, X);
1514   }
1515 };
1516
1517 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
1518 /// to allow co-allocation of node operands with the node itself.
1519 class BinarySDNode : public SDNode {
1520   SDUse Ops[2];
1521 public:
1522   BinarySDNode(unsigned Opc, SDVTList VTs, SDValue X, SDValue Y)
1523     : SDNode(Opc, VTs) {
1524     InitOperands(Ops, X, Y);
1525   }
1526 };
1527
1528 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
1529 /// to allow co-allocation of node operands with the node itself.
1530 class TernarySDNode : public SDNode {
1531   SDUse Ops[3];
1532 public:
1533   TernarySDNode(unsigned Opc, SDVTList VTs, SDValue X, SDValue Y,
1534                 SDValue Z)
1535     : SDNode(Opc, VTs) {
1536     InitOperands(Ops, X, Y, Z);
1537   }
1538 };
1539
1540
1541 /// HandleSDNode - This class is used to form a handle around another node that
1542 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1543 /// operand.  This node should be directly created by end-users and not added to
1544 /// the AllNodes list.
1545 class HandleSDNode : public SDNode {
1546   SDUse Op;
1547 public:
1548   // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
1549   // fixed.
1550 #ifdef __GNUC__
1551   explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
1552 #else
1553   explicit HandleSDNode(SDValue X)
1554 #endif
1555     : SDNode(ISD::HANDLENODE, getSDVTList(MVT::Other)) {
1556     InitOperands(&Op, X);
1557   }
1558   ~HandleSDNode();  
1559   const SDValue &getValue() const { return Op; }
1560 };
1561
1562 /// Abstact virtual class for operations for memory operations
1563 class MemSDNode : public SDNode {
1564 private:
1565   // MemoryVT - VT of in-memory value.
1566   MVT MemoryVT;
1567
1568   //! SrcValue - Memory location for alias analysis.
1569   const Value *SrcValue;
1570
1571   //! SVOffset - Memory location offset. Note that base is defined in MemSDNode
1572   int SVOffset;
1573
1574   /// Flags - the low bit indicates whether this is a volatile reference;
1575   /// the remainder is a log2 encoding of the alignment in bytes.
1576   unsigned Flags;
1577
1578 public:
1579   MemSDNode(unsigned Opc, SDVTList VTs, MVT MemoryVT,
1580             const Value *srcValue, int SVOff,
1581             unsigned alignment, bool isvolatile);
1582
1583   MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops, unsigned NumOps,
1584             MVT MemoryVT, const Value *srcValue, int SVOff,
1585             unsigned alignment, bool isvolatile);
1586
1587   /// Returns alignment and volatility of the memory access
1588   unsigned getAlignment() const { return (1u << (Flags >> 1)) >> 1; }
1589   bool isVolatile() const { return Flags & 1; }
1590   
1591   /// Returns the SrcValue and offset that describes the location of the access
1592   const Value *getSrcValue() const { return SrcValue; }
1593   int getSrcValueOffset() const { return SVOffset; }
1594   
1595   /// getMemoryVT - Return the type of the in-memory value.
1596   MVT getMemoryVT() const { return MemoryVT; }
1597     
1598   /// getMemOperand - Return a MachineMemOperand object describing the memory
1599   /// reference performed by operation.
1600   MachineMemOperand getMemOperand() const;
1601
1602   const SDValue &getChain() const { return getOperand(0); }
1603   const SDValue &getBasePtr() const {
1604     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1605   }
1606
1607   /// getRawFlags - Represent the flags as a bunch of bits.
1608   ///
1609   unsigned getRawFlags() const { return Flags; }
1610
1611   // Methods to support isa and dyn_cast
1612   static bool classof(const MemSDNode *) { return true; }
1613   static bool classof(const SDNode *N) {
1614     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1615     // with either an intrinsic or a target opcode.
1616     return N->getOpcode() == ISD::LOAD                ||
1617            N->getOpcode() == ISD::STORE               ||
1618            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1619            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1620            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1621            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1622            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1623            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1624            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1625            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1626            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1627            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1628            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1629            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1630            N->getOpcode() == ISD::INTRINSIC_W_CHAIN   ||
1631            N->getOpcode() == ISD::INTRINSIC_VOID      ||
1632            N->isTargetOpcode();
1633   }  
1634 };
1635
1636 /// AtomicSDNode - A SDNode reprenting atomic operations.
1637 ///
1638 class AtomicSDNode : public MemSDNode {
1639   SDUse Ops[4];
1640   
1641 public:
1642   // Opc:   opcode for atomic
1643   // VTL:    value type list
1644   // Chain:  memory chain for operaand
1645   // Ptr:    address to update as a SDValue
1646   // Cmp:    compare value
1647   // Swp:    swap value
1648   // SrcVal: address to update as a Value (used for MemOperand)
1649   // Align:  alignment of memory
1650   AtomicSDNode(unsigned Opc, SDVTList VTL, MVT MemVT,
1651                SDValue Chain, SDValue Ptr,
1652                SDValue Cmp, SDValue Swp, const Value* SrcVal,
1653                unsigned Align=0)
1654     : MemSDNode(Opc, VTL, MemVT, SrcVal, /*SVOffset=*/0,
1655                 Align, /*isVolatile=*/true) {
1656     InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1657   }
1658   AtomicSDNode(unsigned Opc, SDVTList VTL, MVT MemVT,
1659                SDValue Chain, SDValue Ptr, 
1660                SDValue Val, const Value* SrcVal, unsigned Align=0)
1661     : MemSDNode(Opc, VTL, MemVT, SrcVal, /*SVOffset=*/0,
1662                 Align, /*isVolatile=*/true) {
1663     InitOperands(Ops, Chain, Ptr, Val);
1664   }
1665   
1666   const SDValue &getBasePtr() const { return getOperand(1); }
1667   const SDValue &getVal() const { return getOperand(2); }
1668
1669   bool isCompareAndSwap() const { 
1670     unsigned Op = getOpcode(); 
1671     return Op == ISD::ATOMIC_CMP_SWAP;
1672   }
1673
1674   // Methods to support isa and dyn_cast
1675   static bool classof(const AtomicSDNode *) { return true; }
1676   static bool classof(const SDNode *N) {
1677     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1678            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1679            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1680            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1681            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1682            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1683            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1684            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1685            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1686            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1687            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1688            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX;
1689   }
1690 };
1691
1692 /// MemIntrinsicSDNode - This SDNode is used for target intrinsic that touches
1693 /// memory and need an associated memory operand.
1694 ///
1695 class MemIntrinsicSDNode : public MemSDNode {
1696   bool ReadMem;  // Intrinsic reads memory
1697   bool WriteMem; // Intrinsic writes memory
1698 public:
1699   MemIntrinsicSDNode(unsigned Opc, SDVTList VTs,
1700                      const SDValue *Ops, unsigned NumOps,
1701                      MVT MemoryVT, const Value *srcValue, int SVO,
1702                      unsigned Align, bool Vol, bool ReadMem, bool WriteMem)
1703     : MemSDNode(Opc, VTs, Ops, NumOps, MemoryVT, srcValue, SVO, Align, Vol),
1704       ReadMem(ReadMem), WriteMem(WriteMem) {
1705   }
1706
1707   bool readMem() const { return ReadMem; }
1708   bool writeMem() const { return WriteMem; }
1709
1710   // Methods to support isa and dyn_cast
1711   static bool classof(const MemIntrinsicSDNode *) { return true; }
1712   static bool classof(const SDNode *N) {
1713     // We lower some target intrinsics to their target opcode
1714     // early a node with a target opcode can be of this class
1715     return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1716            N->getOpcode() == ISD::INTRINSIC_VOID ||
1717            N->isTargetOpcode();
1718   }
1719 };
1720
1721 class ConstantSDNode : public SDNode {
1722   const ConstantInt *Value;
1723 protected:
1724   friend class SelectionDAG;
1725   ConstantSDNode(bool isTarget, const ConstantInt *val, MVT VT)
1726     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, getSDVTList(VT)),
1727       Value(val) {
1728   }
1729 public:
1730
1731   const ConstantInt *getConstantIntValue() const { return Value; }
1732   const APInt &getAPIntValue() const { return Value->getValue(); }
1733   uint64_t getZExtValue() const { return Value->getZExtValue(); }
1734   int64_t getSExtValue() const { return Value->getSExtValue(); }
1735
1736   bool isNullValue() const { return Value->isNullValue(); }
1737   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1738
1739   static bool classof(const ConstantSDNode *) { return true; }
1740   static bool classof(const SDNode *N) {
1741     return N->getOpcode() == ISD::Constant ||
1742            N->getOpcode() == ISD::TargetConstant;
1743   }
1744 };
1745
1746 class ConstantFPSDNode : public SDNode {
1747   const ConstantFP *Value;
1748 protected:
1749   friend class SelectionDAG;
1750   ConstantFPSDNode(bool isTarget, const ConstantFP *val, MVT VT)
1751     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1752              getSDVTList(VT)), Value(val) {
1753   }
1754 public:
1755
1756   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1757   const ConstantFP *getConstantFPValue() const { return Value; }
1758
1759   /// isExactlyValue - We don't rely on operator== working on double values, as
1760   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1761   /// As such, this method can be used to do an exact bit-for-bit comparison of
1762   /// two floating point values.
1763
1764   /// We leave the version with the double argument here because it's just so
1765   /// convenient to write "2.0" and the like.  Without this function we'd 
1766   /// have to duplicate its logic everywhere it's called.
1767   bool isExactlyValue(double V) const {
1768     bool ignored;
1769     // convert is not supported on this type
1770     if (&Value->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1771       return false;
1772     APFloat Tmp(V);
1773     Tmp.convert(Value->getValueAPF().getSemantics(),
1774                 APFloat::rmNearestTiesToEven, &ignored);
1775     return isExactlyValue(Tmp);
1776   }
1777   bool isExactlyValue(const APFloat& V) const;
1778
1779   bool isValueValidForType(MVT VT, const APFloat& Val);
1780
1781   static bool classof(const ConstantFPSDNode *) { return true; }
1782   static bool classof(const SDNode *N) {
1783     return N->getOpcode() == ISD::ConstantFP || 
1784            N->getOpcode() == ISD::TargetConstantFP;
1785   }
1786 };
1787
1788 class GlobalAddressSDNode : public SDNode {
1789   GlobalValue *TheGlobal;
1790   int64_t Offset;
1791 protected:
1792   friend class SelectionDAG;
1793   GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT VT,
1794                       int64_t o = 0);
1795 public:
1796
1797   GlobalValue *getGlobal() const { return TheGlobal; }
1798   int64_t getOffset() const { return Offset; }
1799
1800   static bool classof(const GlobalAddressSDNode *) { return true; }
1801   static bool classof(const SDNode *N) {
1802     return N->getOpcode() == ISD::GlobalAddress ||
1803            N->getOpcode() == ISD::TargetGlobalAddress ||
1804            N->getOpcode() == ISD::GlobalTLSAddress ||
1805            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1806   }
1807 };
1808
1809 class FrameIndexSDNode : public SDNode {
1810   int FI;
1811 protected:
1812   friend class SelectionDAG;
1813   FrameIndexSDNode(int fi, MVT VT, bool isTarg)
1814     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, getSDVTList(VT)),
1815       FI(fi) {
1816   }
1817 public:
1818
1819   int getIndex() const { return FI; }
1820
1821   static bool classof(const FrameIndexSDNode *) { return true; }
1822   static bool classof(const SDNode *N) {
1823     return N->getOpcode() == ISD::FrameIndex ||
1824            N->getOpcode() == ISD::TargetFrameIndex;
1825   }
1826 };
1827
1828 class JumpTableSDNode : public SDNode {
1829   int JTI;
1830 protected:
1831   friend class SelectionDAG;
1832   JumpTableSDNode(int jti, MVT VT, bool isTarg)
1833     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, getSDVTList(VT)),
1834       JTI(jti) {
1835   }
1836 public:
1837     
1838   int getIndex() const { return JTI; }
1839   
1840   static bool classof(const JumpTableSDNode *) { return true; }
1841   static bool classof(const SDNode *N) {
1842     return N->getOpcode() == ISD::JumpTable ||
1843            N->getOpcode() == ISD::TargetJumpTable;
1844   }
1845 };
1846
1847 class ConstantPoolSDNode : public SDNode {
1848   union {
1849     Constant *ConstVal;
1850     MachineConstantPoolValue *MachineCPVal;
1851   } Val;
1852   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1853   unsigned Alignment;
1854 protected:
1855   friend class SelectionDAG;
1856   ConstantPoolSDNode(bool isTarget, Constant *c, MVT VT, int o=0)
1857     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1858              getSDVTList(VT)), Offset(o), Alignment(0) {
1859     assert((int)Offset >= 0 && "Offset is too large");
1860     Val.ConstVal = c;
1861   }
1862   ConstantPoolSDNode(bool isTarget, Constant *c, MVT VT, int o, unsigned Align)
1863     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1864              getSDVTList(VT)), Offset(o), Alignment(Align) {
1865     assert((int)Offset >= 0 && "Offset is too large");
1866     Val.ConstVal = c;
1867   }
1868   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1869                      MVT VT, int o=0)
1870     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1871              getSDVTList(VT)), Offset(o), Alignment(0) {
1872     assert((int)Offset >= 0 && "Offset is too large");
1873     Val.MachineCPVal = v;
1874     Offset |= 1 << (sizeof(unsigned)*8-1);
1875   }
1876   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1877                      MVT VT, int o, unsigned Align)
1878     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1879              getSDVTList(VT)), Offset(o), Alignment(Align) {
1880     assert((int)Offset >= 0 && "Offset is too large");
1881     Val.MachineCPVal = v;
1882     Offset |= 1 << (sizeof(unsigned)*8-1);
1883   }
1884 public:
1885
1886   bool isMachineConstantPoolEntry() const {
1887     return (int)Offset < 0;
1888   }
1889
1890   Constant *getConstVal() const {
1891     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1892     return Val.ConstVal;
1893   }
1894
1895   MachineConstantPoolValue *getMachineCPVal() const {
1896     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1897     return Val.MachineCPVal;
1898   }
1899
1900   int getOffset() const {
1901     return Offset & ~(1 << (sizeof(unsigned)*8-1));
1902   }
1903   
1904   // Return the alignment of this constant pool object, which is either 0 (for
1905   // default alignment) or log2 of the desired value.
1906   unsigned getAlignment() const { return Alignment; }
1907
1908   const Type *getType() const;
1909
1910   static bool classof(const ConstantPoolSDNode *) { return true; }
1911   static bool classof(const SDNode *N) {
1912     return N->getOpcode() == ISD::ConstantPool ||
1913            N->getOpcode() == ISD::TargetConstantPool;
1914   }
1915 };
1916
1917 class BasicBlockSDNode : public SDNode {
1918   MachineBasicBlock *MBB;
1919 protected:
1920   friend class SelectionDAG;
1921   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1922     : SDNode(ISD::BasicBlock, getSDVTList(MVT::Other)), MBB(mbb) {
1923   }
1924 public:
1925
1926   MachineBasicBlock *getBasicBlock() const { return MBB; }
1927
1928   static bool classof(const BasicBlockSDNode *) { return true; }
1929   static bool classof(const SDNode *N) {
1930     return N->getOpcode() == ISD::BasicBlock;
1931   }
1932 };
1933
1934 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1935 /// used when the SelectionDAG needs to make a simple reference to something
1936 /// in the LLVM IR representation.
1937 ///
1938 /// Note that this is not used for carrying alias information; that is done
1939 /// with MemOperandSDNode, which includes a Value which is required to be a
1940 /// pointer, and several other fields specific to memory references.
1941 ///
1942 class SrcValueSDNode : public SDNode {
1943   const Value *V;
1944 protected:
1945   friend class SelectionDAG;
1946   /// Create a SrcValue for a general value.
1947   explicit SrcValueSDNode(const Value *v)
1948     : SDNode(ISD::SRCVALUE, getSDVTList(MVT::Other)), V(v) {}
1949
1950 public:
1951   /// getValue - return the contained Value.
1952   const Value *getValue() const { return V; }
1953
1954   static bool classof(const SrcValueSDNode *) { return true; }
1955   static bool classof(const SDNode *N) {
1956     return N->getOpcode() == ISD::SRCVALUE;
1957   }
1958 };
1959
1960
1961 /// MemOperandSDNode - An SDNode that holds a MachineMemOperand. This is
1962 /// used to represent a reference to memory after ISD::LOAD
1963 /// and ISD::STORE have been lowered.
1964 ///
1965 class MemOperandSDNode : public SDNode {
1966 protected:
1967   friend class SelectionDAG;
1968   /// Create a MachineMemOperand node
1969   explicit MemOperandSDNode(const MachineMemOperand &mo)
1970     : SDNode(ISD::MEMOPERAND, getSDVTList(MVT::Other)), MO(mo) {}
1971
1972 public:
1973   /// MO - The contained MachineMemOperand.
1974   const MachineMemOperand MO;
1975
1976   static bool classof(const MemOperandSDNode *) { return true; }
1977   static bool classof(const SDNode *N) {
1978     return N->getOpcode() == ISD::MEMOPERAND;
1979   }
1980 };
1981
1982
1983 class RegisterSDNode : public SDNode {
1984   unsigned Reg;
1985 protected:
1986   friend class SelectionDAG;
1987   RegisterSDNode(unsigned reg, MVT VT)
1988     : SDNode(ISD::Register, getSDVTList(VT)), Reg(reg) {
1989   }
1990 public:
1991
1992   unsigned getReg() const { return Reg; }
1993
1994   static bool classof(const RegisterSDNode *) { return true; }
1995   static bool classof(const SDNode *N) {
1996     return N->getOpcode() == ISD::Register;
1997   }
1998 };
1999
2000 class DbgStopPointSDNode : public SDNode {
2001   SDUse Chain;
2002   unsigned Line;
2003   unsigned Column;
2004   Value *CU;
2005 protected:
2006   friend class SelectionDAG;
2007   DbgStopPointSDNode(SDValue ch, unsigned l, unsigned c,
2008                      Value *cu)
2009     : SDNode(ISD::DBG_STOPPOINT, getSDVTList(MVT::Other)),
2010       Line(l), Column(c), CU(cu) {
2011     InitOperands(&Chain, ch);
2012   }
2013 public:
2014   unsigned getLine() const { return Line; }
2015   unsigned getColumn() const { return Column; }
2016   Value *getCompileUnit() const { return CU; }
2017
2018   static bool classof(const DbgStopPointSDNode *) { return true; }
2019   static bool classof(const SDNode *N) {
2020     return N->getOpcode() == ISD::DBG_STOPPOINT;
2021   }
2022 };
2023
2024 class LabelSDNode : public SDNode {
2025   SDUse Chain;
2026   unsigned LabelID;
2027 protected:
2028   friend class SelectionDAG;
2029   LabelSDNode(unsigned NodeTy, SDValue ch, unsigned id)
2030     : SDNode(NodeTy, getSDVTList(MVT::Other)), LabelID(id) {
2031     InitOperands(&Chain, ch);
2032   }
2033 public:
2034   unsigned getLabelID() const { return LabelID; }
2035
2036   static bool classof(const LabelSDNode *) { return true; }
2037   static bool classof(const SDNode *N) {
2038     return N->getOpcode() == ISD::DBG_LABEL ||
2039            N->getOpcode() == ISD::EH_LABEL;
2040   }
2041 };
2042
2043 class ExternalSymbolSDNode : public SDNode {
2044   const char *Symbol;
2045 protected:
2046   friend class SelectionDAG;
2047   ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT VT)
2048     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
2049              getSDVTList(VT)), Symbol(Sym) {
2050   }
2051 public:
2052
2053   const char *getSymbol() const { return Symbol; }
2054
2055   static bool classof(const ExternalSymbolSDNode *) { return true; }
2056   static bool classof(const SDNode *N) {
2057     return N->getOpcode() == ISD::ExternalSymbol ||
2058            N->getOpcode() == ISD::TargetExternalSymbol;
2059   }
2060 };
2061
2062 class CondCodeSDNode : public SDNode {
2063   ISD::CondCode Condition;
2064 protected:
2065   friend class SelectionDAG;
2066   explicit CondCodeSDNode(ISD::CondCode Cond)
2067     : SDNode(ISD::CONDCODE, getSDVTList(MVT::Other)), Condition(Cond) {
2068   }
2069 public:
2070
2071   ISD::CondCode get() const { return Condition; }
2072
2073   static bool classof(const CondCodeSDNode *) { return true; }
2074   static bool classof(const SDNode *N) {
2075     return N->getOpcode() == ISD::CONDCODE;
2076   }
2077 };
2078
2079 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
2080 /// future and most targets don't support it.
2081 class CvtRndSatSDNode : public SDNode {
2082   ISD::CvtCode CvtCode;
2083 protected:
2084   friend class SelectionDAG;
2085   explicit CvtRndSatSDNode(MVT VT, const SDValue *Ops, unsigned NumOps,
2086                            ISD::CvtCode Code)
2087     : SDNode(ISD::CONVERT_RNDSAT, getSDVTList(VT), Ops, NumOps), CvtCode(Code) {
2088     assert(NumOps == 5 && "wrong number of operations");
2089   }
2090 public:
2091   ISD::CvtCode getCvtCode() const { return CvtCode; }
2092
2093   static bool classof(const CvtRndSatSDNode *) { return true; }
2094   static bool classof(const SDNode *N) {
2095     return N->getOpcode() == ISD::CONVERT_RNDSAT;
2096   }
2097 };
2098
2099 namespace ISD {
2100   struct ArgFlagsTy {
2101   private:
2102     static const uint64_t NoFlagSet      = 0ULL;
2103     static const uint64_t ZExt           = 1ULL<<0;  ///< Zero extended
2104     static const uint64_t ZExtOffs       = 0;
2105     static const uint64_t SExt           = 1ULL<<1;  ///< Sign extended
2106     static const uint64_t SExtOffs       = 1;
2107     static const uint64_t InReg          = 1ULL<<2;  ///< Passed in register
2108     static const uint64_t InRegOffs      = 2;
2109     static const uint64_t SRet           = 1ULL<<3;  ///< Hidden struct-ret ptr
2110     static const uint64_t SRetOffs       = 3;
2111     static const uint64_t ByVal          = 1ULL<<4;  ///< Struct passed by value
2112     static const uint64_t ByValOffs      = 4;
2113     static const uint64_t Nest           = 1ULL<<5;  ///< Nested fn static chain
2114     static const uint64_t NestOffs       = 5;
2115     static const uint64_t ByValAlign     = 0xFULL << 6; //< Struct alignment
2116     static const uint64_t ByValAlignOffs = 6;
2117     static const uint64_t Split          = 1ULL << 10;
2118     static const uint64_t SplitOffs      = 10;
2119     static const uint64_t OrigAlign      = 0x1FULL<<27;
2120     static const uint64_t OrigAlignOffs  = 27;
2121     static const uint64_t ByValSize      = 0xffffffffULL << 32; //< Struct size
2122     static const uint64_t ByValSizeOffs  = 32;
2123
2124     static const uint64_t One            = 1ULL; //< 1 of this type, for shifts
2125
2126     uint64_t Flags;
2127   public:
2128     ArgFlagsTy() : Flags(0) { }
2129
2130     bool isZExt()   const { return Flags & ZExt; }
2131     void setZExt()  { Flags |= One << ZExtOffs; }
2132
2133     bool isSExt()   const { return Flags & SExt; }
2134     void setSExt()  { Flags |= One << SExtOffs; }
2135
2136     bool isInReg()  const { return Flags & InReg; }
2137     void setInReg() { Flags |= One << InRegOffs; }
2138
2139     bool isSRet()   const { return Flags & SRet; }
2140     void setSRet()  { Flags |= One << SRetOffs; }
2141
2142     bool isByVal()  const { return Flags & ByVal; }
2143     void setByVal() { Flags |= One << ByValOffs; }
2144
2145     bool isNest()   const { return Flags & Nest; }
2146     void setNest()  { Flags |= One << NestOffs; }
2147
2148     unsigned getByValAlign() const {
2149       return (unsigned) 
2150         ((One << ((Flags & ByValAlign) >> ByValAlignOffs)) / 2);
2151     }
2152     void setByValAlign(unsigned A) {
2153       Flags = (Flags & ~ByValAlign) |
2154         (uint64_t(Log2_32(A) + 1) << ByValAlignOffs);
2155     }
2156             
2157     bool isSplit()   const { return Flags & Split; }
2158     void setSplit()  { Flags |= One << SplitOffs; }
2159
2160     unsigned getOrigAlign() const {
2161       return (unsigned)
2162         ((One << ((Flags & OrigAlign) >> OrigAlignOffs)) / 2);
2163     }
2164     void setOrigAlign(unsigned A) {
2165       Flags = (Flags & ~OrigAlign) |
2166         (uint64_t(Log2_32(A) + 1) << OrigAlignOffs);
2167     }
2168
2169     unsigned getByValSize() const {
2170       return (unsigned)((Flags & ByValSize) >> ByValSizeOffs);
2171     }
2172     void setByValSize(unsigned S) {
2173       Flags = (Flags & ~ByValSize) | (uint64_t(S) << ByValSizeOffs);
2174     }
2175
2176     /// getArgFlagsString - Returns the flags as a string, eg: "zext align:4".
2177     std::string getArgFlagsString();
2178
2179     /// getRawBits - Represent the flags as a bunch of bits.
2180     uint64_t getRawBits() const { return Flags; }
2181   };
2182 }
2183
2184 /// ARG_FLAGSSDNode - Leaf node holding parameter flags.
2185 class ARG_FLAGSSDNode : public SDNode {
2186   ISD::ArgFlagsTy TheFlags;
2187 protected:
2188   friend class SelectionDAG;
2189   explicit ARG_FLAGSSDNode(ISD::ArgFlagsTy Flags)
2190     : SDNode(ISD::ARG_FLAGS, getSDVTList(MVT::Other)), TheFlags(Flags) {
2191   }
2192 public:
2193   ISD::ArgFlagsTy getArgFlags() const { return TheFlags; }
2194
2195   static bool classof(const ARG_FLAGSSDNode *) { return true; }
2196   static bool classof(const SDNode *N) {
2197     return N->getOpcode() == ISD::ARG_FLAGS;
2198   }
2199 };
2200
2201 /// CallSDNode - Node for calls -- ISD::CALL.
2202 class CallSDNode : public SDNode {
2203   unsigned CallingConv;
2204   bool IsVarArg;
2205   bool IsTailCall;
2206   // We might eventually want a full-blown Attributes for the result; that
2207   // will expand the size of the representation.  At the moment we only
2208   // need Inreg.
2209   bool Inreg;
2210 protected:
2211   friend class SelectionDAG;
2212   CallSDNode(unsigned cc, bool isvararg, bool istailcall, bool isinreg,
2213              SDVTList VTs, const SDValue *Operands, unsigned numOperands)
2214     : SDNode(ISD::CALL, VTs, Operands, numOperands),
2215       CallingConv(cc), IsVarArg(isvararg), IsTailCall(istailcall),
2216       Inreg(isinreg) {}
2217 public:
2218   unsigned getCallingConv() const { return CallingConv; }
2219   unsigned isVarArg() const { return IsVarArg; }
2220   unsigned isTailCall() const { return IsTailCall; }
2221   unsigned isInreg() const { return Inreg; }
2222
2223   /// Set this call to not be marked as a tail call. Normally setter
2224   /// methods in SDNodes are unsafe because it breaks the CSE map,
2225   /// but we don't include the tail call flag for calls so it's ok
2226   /// in this case.
2227   void setNotTailCall() { IsTailCall = false; }
2228
2229   SDValue getChain() const { return getOperand(0); }
2230   SDValue getCallee() const { return getOperand(1); }
2231
2232   unsigned getNumArgs() const { return (getNumOperands() - 2) / 2; }
2233   SDValue getArg(unsigned i) const { return getOperand(2+2*i); }
2234   SDValue getArgFlagsVal(unsigned i) const {
2235     return getOperand(3+2*i);
2236   }
2237   ISD::ArgFlagsTy getArgFlags(unsigned i) const {
2238     return cast<ARG_FLAGSSDNode>(getArgFlagsVal(i).getNode())->getArgFlags();
2239   }
2240
2241   unsigned getNumRetVals() const { return getNumValues() - 1; }
2242   MVT getRetValType(unsigned i) const { return getValueType(i); }
2243
2244   static bool classof(const CallSDNode *) { return true; }
2245   static bool classof(const SDNode *N) {
2246     return N->getOpcode() == ISD::CALL;
2247   }
2248 };
2249
2250 /// VTSDNode - This class is used to represent MVT's, which are used
2251 /// to parameterize some operations.
2252 class VTSDNode : public SDNode {
2253   MVT ValueType;
2254 protected:
2255   friend class SelectionDAG;
2256   explicit VTSDNode(MVT VT)
2257     : SDNode(ISD::VALUETYPE, getSDVTList(MVT::Other)), ValueType(VT) {
2258   }
2259 public:
2260
2261   MVT getVT() const { return ValueType; }
2262
2263   static bool classof(const VTSDNode *) { return true; }
2264   static bool classof(const SDNode *N) {
2265     return N->getOpcode() == ISD::VALUETYPE;
2266   }
2267 };
2268
2269 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
2270 ///
2271 class LSBaseSDNode : public MemSDNode {
2272 protected:
2273   //! Operand array for load and store
2274   /*!
2275     \note Moving this array to the base class captures more
2276     common functionality shared between LoadSDNode and
2277     StoreSDNode
2278    */
2279   SDUse Ops[4];
2280 public:
2281   LSBaseSDNode(ISD::NodeType NodeTy, SDValue *Operands, unsigned numOperands,
2282                SDVTList VTs, ISD::MemIndexedMode AM, MVT VT,
2283                const Value *SV, int SVO, unsigned Align, bool Vol)
2284     : MemSDNode(NodeTy, VTs, VT, SV, SVO, Align, Vol) {
2285     SubclassData = AM;
2286     InitOperands(Ops, Operands, numOperands);
2287     assert(Align != 0 && "Loads and stores should have non-zero aligment");
2288     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
2289            "Only indexed loads and stores have a non-undef offset operand");
2290   }
2291
2292   const SDValue &getOffset() const {
2293     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
2294   }
2295
2296   /// getAddressingMode - Return the addressing mode for this load or store:
2297   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2298   ISD::MemIndexedMode getAddressingMode() const {
2299     return ISD::MemIndexedMode(SubclassData & 7);
2300   }
2301
2302   /// isIndexed - Return true if this is a pre/post inc/dec load/store.
2303   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2304
2305   /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
2306   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2307
2308   static bool classof(const LSBaseSDNode *) { return true; }
2309   static bool classof(const SDNode *N) {
2310     return N->getOpcode() == ISD::LOAD ||
2311            N->getOpcode() == ISD::STORE;
2312   }
2313 };
2314
2315 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
2316 ///
2317 class LoadSDNode : public LSBaseSDNode {
2318 protected:
2319   friend class SelectionDAG;
2320   LoadSDNode(SDValue *ChainPtrOff, SDVTList VTs,
2321              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, MVT LVT,
2322              const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
2323     : LSBaseSDNode(ISD::LOAD, ChainPtrOff, 3,
2324                    VTs, AM, LVT, SV, O, Align, Vol) {
2325     SubclassData |= (unsigned short)ETy << 3;
2326   }
2327 public:
2328
2329   /// getExtensionType - Return whether this is a plain node,
2330   /// or one of the varieties of value-extending loads.
2331   ISD::LoadExtType getExtensionType() const {
2332     return ISD::LoadExtType((SubclassData >> 3) & 3);
2333   }
2334
2335   const SDValue &getBasePtr() const { return getOperand(1); }
2336   const SDValue &getOffset() const { return getOperand(2); }
2337   
2338   static bool classof(const LoadSDNode *) { return true; }
2339   static bool classof(const SDNode *N) {
2340     return N->getOpcode() == ISD::LOAD;
2341   }
2342 };
2343
2344 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
2345 ///
2346 class StoreSDNode : public LSBaseSDNode {
2347 protected:
2348   friend class SelectionDAG;
2349   StoreSDNode(SDValue *ChainValuePtrOff, SDVTList VTs,
2350               ISD::MemIndexedMode AM, bool isTrunc, MVT SVT,
2351               const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
2352     : LSBaseSDNode(ISD::STORE, ChainValuePtrOff, 4,
2353                    VTs, AM, SVT, SV, O, Align, Vol) {
2354     SubclassData |= (unsigned short)isTrunc << 3;
2355   }
2356 public:
2357
2358   /// isTruncatingStore - Return true if the op does a truncation before store.
2359   /// For integers this is the same as doing a TRUNCATE and storing the result.
2360   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2361   bool isTruncatingStore() const { return (SubclassData >> 3) & 1; }
2362
2363   const SDValue &getValue() const { return getOperand(1); }
2364   const SDValue &getBasePtr() const { return getOperand(2); }
2365   const SDValue &getOffset() const { return getOperand(3); }
2366   
2367   static bool classof(const StoreSDNode *) { return true; }
2368   static bool classof(const SDNode *N) {
2369     return N->getOpcode() == ISD::STORE;
2370   }
2371 };
2372
2373
2374 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
2375   SDNode *Node;
2376   unsigned Operand;
2377
2378   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2379 public:
2380   bool operator==(const SDNodeIterator& x) const {
2381     return Operand == x.Operand;
2382   }
2383   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2384
2385   const SDNodeIterator &operator=(const SDNodeIterator &I) {
2386     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
2387     Operand = I.Operand;
2388     return *this;
2389   }
2390
2391   pointer operator*() const {
2392     return Node->getOperand(Operand).getNode();
2393   }
2394   pointer operator->() const { return operator*(); }
2395
2396   SDNodeIterator& operator++() {                // Preincrement
2397     ++Operand;
2398     return *this;
2399   }
2400   SDNodeIterator operator++(int) { // Postincrement
2401     SDNodeIterator tmp = *this; ++*this; return tmp;
2402   }
2403
2404   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
2405   static SDNodeIterator end  (SDNode *N) {
2406     return SDNodeIterator(N, N->getNumOperands());
2407   }
2408
2409   unsigned getOperand() const { return Operand; }
2410   const SDNode *getNode() const { return Node; }
2411 };
2412
2413 template <> struct GraphTraits<SDNode*> {
2414   typedef SDNode NodeType;
2415   typedef SDNodeIterator ChildIteratorType;
2416   static inline NodeType *getEntryNode(SDNode *N) { return N; }
2417   static inline ChildIteratorType child_begin(NodeType *N) {
2418     return SDNodeIterator::begin(N);
2419   }
2420   static inline ChildIteratorType child_end(NodeType *N) {
2421     return SDNodeIterator::end(N);
2422   }
2423 };
2424
2425 /// LargestSDNode - The largest SDNode class.
2426 ///
2427 typedef LoadSDNode LargestSDNode;
2428
2429 /// MostAlignedSDNode - The SDNode class with the greatest alignment
2430 /// requirement.
2431 ///
2432 typedef ARG_FLAGSSDNode MostAlignedSDNode;
2433
2434 namespace ISD {
2435   /// isNormalLoad - Returns true if the specified node is a non-extending
2436   /// and unindexed load.
2437   inline bool isNormalLoad(const SDNode *N) {
2438     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2439     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2440       Ld->getAddressingMode() == ISD::UNINDEXED;
2441   }
2442
2443   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
2444   /// load.
2445   inline bool isNON_EXTLoad(const SDNode *N) {
2446     return isa<LoadSDNode>(N) &&
2447       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2448   }
2449
2450   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
2451   ///
2452   inline bool isEXTLoad(const SDNode *N) {
2453     return isa<LoadSDNode>(N) &&
2454       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2455   }
2456
2457   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
2458   ///
2459   inline bool isSEXTLoad(const SDNode *N) {
2460     return isa<LoadSDNode>(N) &&
2461       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2462   }
2463
2464   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
2465   ///
2466   inline bool isZEXTLoad(const SDNode *N) {
2467     return isa<LoadSDNode>(N) &&
2468       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2469   }
2470
2471   /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
2472   ///
2473   inline bool isUNINDEXEDLoad(const SDNode *N) {
2474     return isa<LoadSDNode>(N) &&
2475       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2476   }
2477
2478   /// isNormalStore - Returns true if the specified node is a non-truncating
2479   /// and unindexed store.
2480   inline bool isNormalStore(const SDNode *N) {
2481     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2482     return St && !St->isTruncatingStore() &&
2483       St->getAddressingMode() == ISD::UNINDEXED;
2484   }
2485
2486   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
2487   /// store.
2488   inline bool isNON_TRUNCStore(const SDNode *N) {
2489     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
2490   }
2491
2492   /// isTRUNCStore - Returns true if the specified node is a truncating
2493   /// store.
2494   inline bool isTRUNCStore(const SDNode *N) {
2495     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
2496   }
2497
2498   /// isUNINDEXEDStore - Returns true if the specified node is an
2499   /// unindexed store.
2500   inline bool isUNINDEXEDStore(const SDNode *N) {
2501     return isa<StoreSDNode>(N) &&
2502       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2503   }
2504 }
2505
2506
2507 } // end llvm namespace
2508
2509 #endif