Add DebugLoc field and simple accessors.
[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 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   DebugLoc getDebugLoc() const { return debugLoc; }
1156
1157   /// setDebugLoc - Set source location info.
1158   void setDebugLoc(DebugLoc sl) { debugLoc = sl; }
1159
1160   /// use_iterator - This class provides iterator support for SDUse
1161   /// operands that use a specific SDNode. 
1162   class use_iterator
1163     : public forward_iterator<SDUse, ptrdiff_t> {
1164     SDUse *Op;
1165     explicit use_iterator(SDUse *op) : Op(op) {
1166     }
1167     friend class SDNode;
1168   public:
1169     typedef forward_iterator<SDUse, ptrdiff_t>::reference reference;
1170     typedef forward_iterator<SDUse, ptrdiff_t>::pointer pointer;
1171
1172     use_iterator(const use_iterator &I) : Op(I.Op) {}
1173     use_iterator() : Op(0) {}
1174
1175     bool operator==(const use_iterator &x) const {
1176       return Op == x.Op;
1177     }
1178     bool operator!=(const use_iterator &x) const {
1179       return !operator==(x);
1180     }
1181  
1182     /// atEnd - return true if this iterator is at the end of uses list.
1183     bool atEnd() const { return Op == 0; }
1184
1185     // Iterator traversal: forward iteration only.
1186     use_iterator &operator++() {          // Preincrement
1187       assert(Op && "Cannot increment end iterator!");
1188       Op = Op->getNext();
1189       return *this;
1190     }
1191
1192     use_iterator operator++(int) {        // Postincrement
1193       use_iterator tmp = *this; ++*this; return tmp;
1194     }
1195
1196     /// Retrieve a pointer to the current user node.
1197     SDNode *operator*() const {
1198       assert(Op && "Cannot dereference end iterator!");
1199       return Op->getUser();
1200     }
1201
1202     SDNode *operator->() const { return operator*(); }
1203
1204     SDUse &getUse() const { return *Op; }
1205
1206     /// getOperandNo - Retrieve the operand # of this use in its user.
1207     ///
1208     unsigned getOperandNo() const {
1209       assert(Op && "Cannot dereference end iterator!");
1210       return (unsigned)(Op - Op->getUser()->OperandList);
1211     }
1212   };
1213
1214   /// use_begin/use_end - Provide iteration support to walk over all uses
1215   /// of an SDNode.
1216
1217   use_iterator use_begin() const {
1218     return use_iterator(UseList);
1219   }
1220
1221   static use_iterator use_end() { return use_iterator(0); }
1222
1223
1224   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1225   /// indicated value.  This method ignores uses of other values defined by this
1226   /// operation.
1227   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
1228
1229   /// hasAnyUseOfValue - Return true if there are any use of the indicated
1230   /// value. This method ignores uses of other values defined by this operation.
1231   bool hasAnyUseOfValue(unsigned Value) const;
1232
1233   /// isOnlyUserOf - Return true if this node is the only use of N.
1234   ///
1235   bool isOnlyUserOf(SDNode *N) const;
1236
1237   /// isOperandOf - Return true if this node is an operand of N.
1238   ///
1239   bool isOperandOf(SDNode *N) const;
1240
1241   /// isPredecessorOf - Return true if this node is a predecessor of N. This
1242   /// node is either an operand of N or it can be reached by recursively
1243   /// traversing up the operands.
1244   /// NOTE: this is an expensive method. Use it carefully.
1245   bool isPredecessorOf(SDNode *N) const;
1246
1247   /// getNumOperands - Return the number of values used by this operation.
1248   ///
1249   unsigned getNumOperands() const { return NumOperands; }
1250
1251   /// getConstantOperandVal - Helper method returns the integer value of a 
1252   /// ConstantSDNode operand.
1253   uint64_t getConstantOperandVal(unsigned Num) const;
1254
1255   const SDValue &getOperand(unsigned Num) const {
1256     assert(Num < NumOperands && "Invalid child # of SDNode!");
1257     return OperandList[Num];
1258   }
1259
1260   typedef SDUse* op_iterator;
1261   op_iterator op_begin() const { return OperandList; }
1262   op_iterator op_end() const { return OperandList+NumOperands; }
1263
1264   SDVTList getVTList() const {
1265     SDVTList X = { ValueList, NumValues };
1266     return X;
1267   };
1268
1269   /// getFlaggedNode - If this node has a flag operand, return the node
1270   /// to which the flag operand points. Otherwise return NULL.
1271   SDNode *getFlaggedNode() const {
1272     if (getNumOperands() != 0 &&
1273         getOperand(getNumOperands()-1).getValueType() == MVT::Flag)
1274       return getOperand(getNumOperands()-1).getNode();
1275     return 0;
1276   }
1277
1278   // If this is a pseudo op, like copyfromreg, look to see if there is a
1279   // real target node flagged to it.  If so, return the target node.
1280   const SDNode *getFlaggedMachineNode() const {
1281     const SDNode *FoundNode = this;
1282
1283     // Climb up flag edges until a machine-opcode node is found, or the
1284     // end of the chain is reached.
1285     while (!FoundNode->isMachineOpcode()) {
1286       const SDNode *N = FoundNode->getFlaggedNode();
1287       if (!N) break;
1288       FoundNode = N;
1289     }
1290
1291     return FoundNode;
1292   }
1293
1294   /// getNumValues - Return the number of values defined/returned by this
1295   /// operator.
1296   ///
1297   unsigned getNumValues() const { return NumValues; }
1298
1299   /// getValueType - Return the type of a specified result.
1300   ///
1301   MVT getValueType(unsigned ResNo) const {
1302     assert(ResNo < NumValues && "Illegal result number!");
1303     return ValueList[ResNo];
1304   }
1305
1306   /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
1307   ///
1308   unsigned getValueSizeInBits(unsigned ResNo) const {
1309     return getValueType(ResNo).getSizeInBits();
1310   }
1311
1312   typedef const MVT* value_iterator;
1313   value_iterator value_begin() const { return ValueList; }
1314   value_iterator value_end() const { return ValueList+NumValues; }
1315
1316   /// getOperationName - Return the opcode of this operation for printing.
1317   ///
1318   std::string getOperationName(const SelectionDAG *G = 0) const;
1319   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
1320   void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
1321   void dump() const;
1322   void dump(const SelectionDAG *G) const;
1323
1324   static bool classof(const SDNode *) { return true; }
1325
1326   /// Profile - Gather unique data for the node.
1327   ///
1328   void Profile(FoldingSetNodeID &ID) const;
1329
1330   /// addUse - This method should only be used by the SDUse class.
1331   ///
1332   void addUse(SDUse &U) { U.addToList(&UseList); }
1333
1334 protected:
1335   static SDVTList getSDVTList(MVT VT) {
1336     SDVTList Ret = { getValueTypeList(VT), 1 };
1337     return Ret;
1338   }
1339
1340   /// The constructors that supply DebugLoc explicitly should be preferred
1341   /// for new code.
1342   SDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops, unsigned NumOps)
1343     : NodeType(Opc), OperandsNeedDelete(true), SubclassData(0),
1344       NodeId(-1), debugLoc(DebugLoc::getNoDebugLoc()),
1345       OperandList(NumOps ? new SDUse[NumOps] : 0),
1346       ValueList(VTs.VTs),
1347       NumOperands(NumOps), NumValues(VTs.NumVTs),
1348       UseList(NULL) {
1349     for (unsigned i = 0; i != NumOps; ++i) {
1350       OperandList[i].setUser(this);
1351       OperandList[i].setInitial(Ops[i]);
1352     }
1353   }
1354
1355   /// This constructor adds no operands itself; operands can be
1356   /// set later with InitOperands.
1357   SDNode(unsigned Opc, SDVTList VTs)
1358     : NodeType(Opc), OperandsNeedDelete(false), SubclassData(0),
1359       NodeId(-1), debugLoc(DebugLoc::getNoDebugLoc()), OperandList(0), 
1360       ValueList(VTs.VTs), NumOperands(0), NumValues(VTs.NumVTs),
1361       UseList(NULL) {}
1362
1363   /// The next two constructors specify DebugLoc explicitly; the intent
1364   /// is that they will replace the above two over time, and eventually
1365   /// the ones above can be removed.
1366   SDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops, unsigned NumOps,
1367          DebugLoc sl)
1368     : NodeType(Opc), OperandsNeedDelete(true), SubclassData(0),
1369       NodeId(-1), debugLoc(sl),
1370       OperandList(NumOps ? new SDUse[NumOps] : 0),
1371       ValueList(VTs.VTs),
1372       NumOperands(NumOps), NumValues(VTs.NumVTs),
1373       UseList(NULL) {
1374     for (unsigned i = 0; i != NumOps; ++i) {
1375       OperandList[i].setUser(this);
1376       OperandList[i].setInitial(Ops[i]);
1377     }
1378   }
1379
1380   /// This constructor adds no operands itself; operands can be
1381   /// set later with InitOperands.
1382   SDNode(unsigned Opc, SDVTList VTs, DebugLoc sl)
1383     : NodeType(Opc), OperandsNeedDelete(false), SubclassData(0),
1384       NodeId(-1), debugLoc(sl), OperandList(0),
1385       ValueList(VTs.VTs), NumOperands(0), NumValues(VTs.NumVTs),
1386       UseList(NULL) {}
1387   
1388   /// InitOperands - Initialize the operands list of this with 1 operand.
1389   void InitOperands(SDUse *Ops, const SDValue &Op0) {
1390     Ops[0].setUser(this);
1391     Ops[0].setInitial(Op0);
1392     NumOperands = 1;
1393     OperandList = Ops;
1394   }
1395
1396   /// InitOperands - Initialize the operands list of this with 2 operands.
1397   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
1398     Ops[0].setUser(this);
1399     Ops[0].setInitial(Op0);
1400     Ops[1].setUser(this);
1401     Ops[1].setInitial(Op1);
1402     NumOperands = 2;
1403     OperandList = Ops;
1404   }
1405
1406   /// InitOperands - Initialize the operands list of this with 3 operands.
1407   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
1408                     const SDValue &Op2) {
1409     Ops[0].setUser(this);
1410     Ops[0].setInitial(Op0);
1411     Ops[1].setUser(this);
1412     Ops[1].setInitial(Op1);
1413     Ops[2].setUser(this);
1414     Ops[2].setInitial(Op2);
1415     NumOperands = 3;
1416     OperandList = Ops;
1417   }
1418
1419   /// InitOperands - Initialize the operands list of this with 4 operands.
1420   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
1421                     const SDValue &Op2, const SDValue &Op3) {
1422     Ops[0].setUser(this);
1423     Ops[0].setInitial(Op0);
1424     Ops[1].setUser(this);
1425     Ops[1].setInitial(Op1);
1426     Ops[2].setUser(this);
1427     Ops[2].setInitial(Op2);
1428     Ops[3].setUser(this);
1429     Ops[3].setInitial(Op3);
1430     NumOperands = 4;
1431     OperandList = Ops;
1432   }
1433
1434   /// InitOperands - Initialize the operands list of this with N operands.
1435   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
1436     for (unsigned i = 0; i != N; ++i) {
1437       Ops[i].setUser(this);
1438       Ops[i].setInitial(Vals[i]);
1439     }
1440     NumOperands = N;
1441     OperandList = Ops;
1442   }
1443
1444   /// DropOperands - Release the operands and set this node to have
1445   /// zero operands.
1446   void DropOperands();
1447 };
1448
1449
1450 // Define inline functions from the SDValue class.
1451
1452 inline unsigned SDValue::getOpcode() const {
1453   return Node->getOpcode();
1454 }
1455 inline MVT SDValue::getValueType() const {
1456   return Node->getValueType(ResNo);
1457 }
1458 inline unsigned SDValue::getNumOperands() const {
1459   return Node->getNumOperands();
1460 }
1461 inline const SDValue &SDValue::getOperand(unsigned i) const {
1462   return Node->getOperand(i);
1463 }
1464 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1465   return Node->getConstantOperandVal(i);
1466 }
1467 inline bool SDValue::isTargetOpcode() const {
1468   return Node->isTargetOpcode();
1469 }
1470 inline bool SDValue::isMachineOpcode() const {
1471   return Node->isMachineOpcode();
1472 }
1473 inline unsigned SDValue::getMachineOpcode() const {
1474   return Node->getMachineOpcode();
1475 }
1476 inline bool SDValue::use_empty() const {
1477   return !Node->hasAnyUseOfValue(ResNo);
1478 }
1479 inline bool SDValue::hasOneUse() const {
1480   return Node->hasNUsesOfValue(1, ResNo);
1481 }
1482 inline DebugLoc SDValue::getDebugLoc() const {
1483   return Node->getDebugLoc();
1484 }
1485
1486 // Define inline functions from the SDUse class.
1487
1488 inline void SDUse::set(const SDValue &V) {
1489   if (Val.getNode()) removeFromList();
1490   Val = V;
1491   if (V.getNode()) V.getNode()->addUse(*this);
1492 }
1493
1494 inline void SDUse::setInitial(const SDValue &V) {
1495   Val = V;
1496   V.getNode()->addUse(*this);
1497 }
1498
1499 inline void SDUse::setNode(SDNode *N) {
1500   if (Val.getNode()) removeFromList();
1501   Val.setNode(N);
1502   if (N) N->addUse(*this);
1503 }
1504
1505 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
1506 /// to allow co-allocation of node operands with the node itself.
1507 class UnarySDNode : public SDNode {
1508   SDUse Op;
1509 public:
1510   UnarySDNode(unsigned Opc, SDVTList VTs, SDValue X)
1511     : SDNode(Opc, VTs) {
1512     InitOperands(&Op, X);
1513   }
1514 };
1515
1516 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
1517 /// to allow co-allocation of node operands with the node itself.
1518 class BinarySDNode : public SDNode {
1519   SDUse Ops[2];
1520 public:
1521   BinarySDNode(unsigned Opc, SDVTList VTs, SDValue X, SDValue Y)
1522     : SDNode(Opc, VTs) {
1523     InitOperands(Ops, X, Y);
1524   }
1525 };
1526
1527 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
1528 /// to allow co-allocation of node operands with the node itself.
1529 class TernarySDNode : public SDNode {
1530   SDUse Ops[3];
1531 public:
1532   TernarySDNode(unsigned Opc, SDVTList VTs, SDValue X, SDValue Y,
1533                 SDValue Z)
1534     : SDNode(Opc, VTs) {
1535     InitOperands(Ops, X, Y, Z);
1536   }
1537 };
1538
1539
1540 /// HandleSDNode - This class is used to form a handle around another node that
1541 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1542 /// operand.  This node should be directly created by end-users and not added to
1543 /// the AllNodes list.
1544 class HandleSDNode : public SDNode {
1545   SDUse Op;
1546 public:
1547   // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
1548   // fixed.
1549 #ifdef __GNUC__
1550   explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
1551 #else
1552   explicit HandleSDNode(SDValue X)
1553 #endif
1554     : SDNode(ISD::HANDLENODE, getSDVTList(MVT::Other)) {
1555     InitOperands(&Op, X);
1556   }
1557   ~HandleSDNode();  
1558   const SDValue &getValue() const { return Op; }
1559 };
1560
1561 /// Abstact virtual class for operations for memory operations
1562 class MemSDNode : public SDNode {
1563 private:
1564   // MemoryVT - VT of in-memory value.
1565   MVT MemoryVT;
1566
1567   //! SrcValue - Memory location for alias analysis.
1568   const Value *SrcValue;
1569
1570   //! SVOffset - Memory location offset. Note that base is defined in MemSDNode
1571   int SVOffset;
1572
1573   /// Flags - the low bit indicates whether this is a volatile reference;
1574   /// the remainder is a log2 encoding of the alignment in bytes.
1575   unsigned Flags;
1576
1577 public:
1578   MemSDNode(unsigned Opc, SDVTList VTs, MVT MemoryVT,
1579             const Value *srcValue, int SVOff,
1580             unsigned alignment, bool isvolatile);
1581
1582   MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops, unsigned NumOps,
1583             MVT MemoryVT, const Value *srcValue, int SVOff,
1584             unsigned alignment, bool isvolatile);
1585
1586   /// Returns alignment and volatility of the memory access
1587   unsigned getAlignment() const { return (1u << (Flags >> 1)) >> 1; }
1588   bool isVolatile() const { return Flags & 1; }
1589   
1590   /// Returns the SrcValue and offset that describes the location of the access
1591   const Value *getSrcValue() const { return SrcValue; }
1592   int getSrcValueOffset() const { return SVOffset; }
1593   
1594   /// getMemoryVT - Return the type of the in-memory value.
1595   MVT getMemoryVT() const { return MemoryVT; }
1596     
1597   /// getMemOperand - Return a MachineMemOperand object describing the memory
1598   /// reference performed by operation.
1599   MachineMemOperand getMemOperand() const;
1600
1601   const SDValue &getChain() const { return getOperand(0); }
1602   const SDValue &getBasePtr() const {
1603     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1604   }
1605
1606   /// getRawFlags - Represent the flags as a bunch of bits.
1607   ///
1608   unsigned getRawFlags() const { return Flags; }
1609
1610   // Methods to support isa and dyn_cast
1611   static bool classof(const MemSDNode *) { return true; }
1612   static bool classof(const SDNode *N) {
1613     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1614     // with either an intrinsic or a target opcode.
1615     return N->getOpcode() == ISD::LOAD                ||
1616            N->getOpcode() == ISD::STORE               ||
1617            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1618            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1619            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1620            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1621            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1622            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1623            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1624            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1625            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1626            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1627            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1628            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1629            N->getOpcode() == ISD::INTRINSIC_W_CHAIN   ||
1630            N->getOpcode() == ISD::INTRINSIC_VOID      ||
1631            N->isTargetOpcode();
1632   }  
1633 };
1634
1635 /// AtomicSDNode - A SDNode reprenting atomic operations.
1636 ///
1637 class AtomicSDNode : public MemSDNode {
1638   SDUse Ops[4];
1639   
1640 public:
1641   // Opc:   opcode for atomic
1642   // VTL:    value type list
1643   // Chain:  memory chain for operaand
1644   // Ptr:    address to update as a SDValue
1645   // Cmp:    compare value
1646   // Swp:    swap value
1647   // SrcVal: address to update as a Value (used for MemOperand)
1648   // Align:  alignment of memory
1649   AtomicSDNode(unsigned Opc, SDVTList VTL, MVT MemVT,
1650                SDValue Chain, SDValue Ptr,
1651                SDValue Cmp, SDValue Swp, const Value* SrcVal,
1652                unsigned Align=0)
1653     : MemSDNode(Opc, VTL, MemVT, SrcVal, /*SVOffset=*/0,
1654                 Align, /*isVolatile=*/true) {
1655     InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1656   }
1657   AtomicSDNode(unsigned Opc, SDVTList VTL, MVT MemVT,
1658                SDValue Chain, SDValue Ptr, 
1659                SDValue Val, const Value* SrcVal, unsigned Align=0)
1660     : MemSDNode(Opc, VTL, MemVT, SrcVal, /*SVOffset=*/0,
1661                 Align, /*isVolatile=*/true) {
1662     InitOperands(Ops, Chain, Ptr, Val);
1663   }
1664   
1665   const SDValue &getBasePtr() const { return getOperand(1); }
1666   const SDValue &getVal() const { return getOperand(2); }
1667
1668   bool isCompareAndSwap() const { 
1669     unsigned Op = getOpcode(); 
1670     return Op == ISD::ATOMIC_CMP_SWAP;
1671   }
1672
1673   // Methods to support isa and dyn_cast
1674   static bool classof(const AtomicSDNode *) { return true; }
1675   static bool classof(const SDNode *N) {
1676     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1677            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1678            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1679            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1680            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1681            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1682            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1683            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1684            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1685            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1686            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1687            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX;
1688   }
1689 };
1690
1691 /// MemIntrinsicSDNode - This SDNode is used for target intrinsic that touches
1692 /// memory and need an associated memory operand.
1693 ///
1694 class MemIntrinsicSDNode : public MemSDNode {
1695   bool ReadMem;  // Intrinsic reads memory
1696   bool WriteMem; // Intrinsic writes memory
1697 public:
1698   MemIntrinsicSDNode(unsigned Opc, SDVTList VTs,
1699                      const SDValue *Ops, unsigned NumOps,
1700                      MVT MemoryVT, const Value *srcValue, int SVO,
1701                      unsigned Align, bool Vol, bool ReadMem, bool WriteMem)
1702     : MemSDNode(Opc, VTs, Ops, NumOps, MemoryVT, srcValue, SVO, Align, Vol),
1703       ReadMem(ReadMem), WriteMem(WriteMem) {
1704   }
1705
1706   bool readMem() const { return ReadMem; }
1707   bool writeMem() const { return WriteMem; }
1708
1709   // Methods to support isa and dyn_cast
1710   static bool classof(const MemIntrinsicSDNode *) { return true; }
1711   static bool classof(const SDNode *N) {
1712     // We lower some target intrinsics to their target opcode
1713     // early a node with a target opcode can be of this class
1714     return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1715            N->getOpcode() == ISD::INTRINSIC_VOID ||
1716            N->isTargetOpcode();
1717   }
1718 };
1719
1720 class ConstantSDNode : public SDNode {
1721   const ConstantInt *Value;
1722 protected:
1723   friend class SelectionDAG;
1724   ConstantSDNode(bool isTarget, const ConstantInt *val, MVT VT)
1725     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, getSDVTList(VT)),
1726       Value(val) {
1727   }
1728 public:
1729
1730   const ConstantInt *getConstantIntValue() const { return Value; }
1731   const APInt &getAPIntValue() const { return Value->getValue(); }
1732   uint64_t getZExtValue() const { return Value->getZExtValue(); }
1733   int64_t getSExtValue() const { return Value->getSExtValue(); }
1734
1735   bool isNullValue() const { return Value->isNullValue(); }
1736   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1737
1738   static bool classof(const ConstantSDNode *) { return true; }
1739   static bool classof(const SDNode *N) {
1740     return N->getOpcode() == ISD::Constant ||
1741            N->getOpcode() == ISD::TargetConstant;
1742   }
1743 };
1744
1745 class ConstantFPSDNode : public SDNode {
1746   const ConstantFP *Value;
1747 protected:
1748   friend class SelectionDAG;
1749   ConstantFPSDNode(bool isTarget, const ConstantFP *val, MVT VT)
1750     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1751              getSDVTList(VT)), Value(val) {
1752   }
1753 public:
1754
1755   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1756   const ConstantFP *getConstantFPValue() const { return Value; }
1757
1758   /// isExactlyValue - We don't rely on operator== working on double values, as
1759   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1760   /// As such, this method can be used to do an exact bit-for-bit comparison of
1761   /// two floating point values.
1762
1763   /// We leave the version with the double argument here because it's just so
1764   /// convenient to write "2.0" and the like.  Without this function we'd 
1765   /// have to duplicate its logic everywhere it's called.
1766   bool isExactlyValue(double V) const {
1767     bool ignored;
1768     // convert is not supported on this type
1769     if (&Value->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1770       return false;
1771     APFloat Tmp(V);
1772     Tmp.convert(Value->getValueAPF().getSemantics(),
1773                 APFloat::rmNearestTiesToEven, &ignored);
1774     return isExactlyValue(Tmp);
1775   }
1776   bool isExactlyValue(const APFloat& V) const;
1777
1778   bool isValueValidForType(MVT VT, const APFloat& Val);
1779
1780   static bool classof(const ConstantFPSDNode *) { return true; }
1781   static bool classof(const SDNode *N) {
1782     return N->getOpcode() == ISD::ConstantFP || 
1783            N->getOpcode() == ISD::TargetConstantFP;
1784   }
1785 };
1786
1787 class GlobalAddressSDNode : public SDNode {
1788   GlobalValue *TheGlobal;
1789   int64_t Offset;
1790 protected:
1791   friend class SelectionDAG;
1792   GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT VT,
1793                       int64_t o = 0);
1794 public:
1795
1796   GlobalValue *getGlobal() const { return TheGlobal; }
1797   int64_t getOffset() const { return Offset; }
1798
1799   static bool classof(const GlobalAddressSDNode *) { return true; }
1800   static bool classof(const SDNode *N) {
1801     return N->getOpcode() == ISD::GlobalAddress ||
1802            N->getOpcode() == ISD::TargetGlobalAddress ||
1803            N->getOpcode() == ISD::GlobalTLSAddress ||
1804            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1805   }
1806 };
1807
1808 class FrameIndexSDNode : public SDNode {
1809   int FI;
1810 protected:
1811   friend class SelectionDAG;
1812   FrameIndexSDNode(int fi, MVT VT, bool isTarg)
1813     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, getSDVTList(VT)),
1814       FI(fi) {
1815   }
1816 public:
1817
1818   int getIndex() const { return FI; }
1819
1820   static bool classof(const FrameIndexSDNode *) { return true; }
1821   static bool classof(const SDNode *N) {
1822     return N->getOpcode() == ISD::FrameIndex ||
1823            N->getOpcode() == ISD::TargetFrameIndex;
1824   }
1825 };
1826
1827 class JumpTableSDNode : public SDNode {
1828   int JTI;
1829 protected:
1830   friend class SelectionDAG;
1831   JumpTableSDNode(int jti, MVT VT, bool isTarg)
1832     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, getSDVTList(VT)),
1833       JTI(jti) {
1834   }
1835 public:
1836     
1837   int getIndex() const { return JTI; }
1838   
1839   static bool classof(const JumpTableSDNode *) { return true; }
1840   static bool classof(const SDNode *N) {
1841     return N->getOpcode() == ISD::JumpTable ||
1842            N->getOpcode() == ISD::TargetJumpTable;
1843   }
1844 };
1845
1846 class ConstantPoolSDNode : public SDNode {
1847   union {
1848     Constant *ConstVal;
1849     MachineConstantPoolValue *MachineCPVal;
1850   } Val;
1851   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1852   unsigned Alignment;
1853 protected:
1854   friend class SelectionDAG;
1855   ConstantPoolSDNode(bool isTarget, Constant *c, MVT VT, int o=0)
1856     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1857              getSDVTList(VT)), Offset(o), Alignment(0) {
1858     assert((int)Offset >= 0 && "Offset is too large");
1859     Val.ConstVal = c;
1860   }
1861   ConstantPoolSDNode(bool isTarget, Constant *c, MVT VT, int o, unsigned Align)
1862     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1863              getSDVTList(VT)), Offset(o), Alignment(Align) {
1864     assert((int)Offset >= 0 && "Offset is too large");
1865     Val.ConstVal = c;
1866   }
1867   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1868                      MVT VT, int o=0)
1869     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1870              getSDVTList(VT)), Offset(o), Alignment(0) {
1871     assert((int)Offset >= 0 && "Offset is too large");
1872     Val.MachineCPVal = v;
1873     Offset |= 1 << (sizeof(unsigned)*8-1);
1874   }
1875   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1876                      MVT VT, int o, unsigned Align)
1877     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1878              getSDVTList(VT)), Offset(o), Alignment(Align) {
1879     assert((int)Offset >= 0 && "Offset is too large");
1880     Val.MachineCPVal = v;
1881     Offset |= 1 << (sizeof(unsigned)*8-1);
1882   }
1883 public:
1884
1885   bool isMachineConstantPoolEntry() const {
1886     return (int)Offset < 0;
1887   }
1888
1889   Constant *getConstVal() const {
1890     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1891     return Val.ConstVal;
1892   }
1893
1894   MachineConstantPoolValue *getMachineCPVal() const {
1895     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1896     return Val.MachineCPVal;
1897   }
1898
1899   int getOffset() const {
1900     return Offset & ~(1 << (sizeof(unsigned)*8-1));
1901   }
1902   
1903   // Return the alignment of this constant pool object, which is either 0 (for
1904   // default alignment) or log2 of the desired value.
1905   unsigned getAlignment() const { return Alignment; }
1906
1907   const Type *getType() const;
1908
1909   static bool classof(const ConstantPoolSDNode *) { return true; }
1910   static bool classof(const SDNode *N) {
1911     return N->getOpcode() == ISD::ConstantPool ||
1912            N->getOpcode() == ISD::TargetConstantPool;
1913   }
1914 };
1915
1916 class BasicBlockSDNode : public SDNode {
1917   MachineBasicBlock *MBB;
1918 protected:
1919   friend class SelectionDAG;
1920   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1921     : SDNode(ISD::BasicBlock, getSDVTList(MVT::Other)), MBB(mbb) {
1922   }
1923 public:
1924
1925   MachineBasicBlock *getBasicBlock() const { return MBB; }
1926
1927   static bool classof(const BasicBlockSDNode *) { return true; }
1928   static bool classof(const SDNode *N) {
1929     return N->getOpcode() == ISD::BasicBlock;
1930   }
1931 };
1932
1933 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1934 /// used when the SelectionDAG needs to make a simple reference to something
1935 /// in the LLVM IR representation.
1936 ///
1937 /// Note that this is not used for carrying alias information; that is done
1938 /// with MemOperandSDNode, which includes a Value which is required to be a
1939 /// pointer, and several other fields specific to memory references.
1940 ///
1941 class SrcValueSDNode : public SDNode {
1942   const Value *V;
1943 protected:
1944   friend class SelectionDAG;
1945   /// Create a SrcValue for a general value.
1946   explicit SrcValueSDNode(const Value *v)
1947     : SDNode(ISD::SRCVALUE, getSDVTList(MVT::Other)), V(v) {}
1948
1949 public:
1950   /// getValue - return the contained Value.
1951   const Value *getValue() const { return V; }
1952
1953   static bool classof(const SrcValueSDNode *) { return true; }
1954   static bool classof(const SDNode *N) {
1955     return N->getOpcode() == ISD::SRCVALUE;
1956   }
1957 };
1958
1959
1960 /// MemOperandSDNode - An SDNode that holds a MachineMemOperand. This is
1961 /// used to represent a reference to memory after ISD::LOAD
1962 /// and ISD::STORE have been lowered.
1963 ///
1964 class MemOperandSDNode : public SDNode {
1965 protected:
1966   friend class SelectionDAG;
1967   /// Create a MachineMemOperand node
1968   explicit MemOperandSDNode(const MachineMemOperand &mo)
1969     : SDNode(ISD::MEMOPERAND, getSDVTList(MVT::Other)), MO(mo) {}
1970
1971 public:
1972   /// MO - The contained MachineMemOperand.
1973   const MachineMemOperand MO;
1974
1975   static bool classof(const MemOperandSDNode *) { return true; }
1976   static bool classof(const SDNode *N) {
1977     return N->getOpcode() == ISD::MEMOPERAND;
1978   }
1979 };
1980
1981
1982 class RegisterSDNode : public SDNode {
1983   unsigned Reg;
1984 protected:
1985   friend class SelectionDAG;
1986   RegisterSDNode(unsigned reg, MVT VT)
1987     : SDNode(ISD::Register, getSDVTList(VT)), Reg(reg) {
1988   }
1989 public:
1990
1991   unsigned getReg() const { return Reg; }
1992
1993   static bool classof(const RegisterSDNode *) { return true; }
1994   static bool classof(const SDNode *N) {
1995     return N->getOpcode() == ISD::Register;
1996   }
1997 };
1998
1999 class DbgStopPointSDNode : public SDNode {
2000   SDUse Chain;
2001   unsigned Line;
2002   unsigned Column;
2003   Value *CU;
2004 protected:
2005   friend class SelectionDAG;
2006   DbgStopPointSDNode(SDValue ch, unsigned l, unsigned c,
2007                      Value *cu)
2008     : SDNode(ISD::DBG_STOPPOINT, getSDVTList(MVT::Other)),
2009       Line(l), Column(c), CU(cu) {
2010     InitOperands(&Chain, ch);
2011   }
2012 public:
2013   unsigned getLine() const { return Line; }
2014   unsigned getColumn() const { return Column; }
2015   Value *getCompileUnit() const { return CU; }
2016
2017   static bool classof(const DbgStopPointSDNode *) { return true; }
2018   static bool classof(const SDNode *N) {
2019     return N->getOpcode() == ISD::DBG_STOPPOINT;
2020   }
2021 };
2022
2023 class LabelSDNode : public SDNode {
2024   SDUse Chain;
2025   unsigned LabelID;
2026 protected:
2027   friend class SelectionDAG;
2028   LabelSDNode(unsigned NodeTy, SDValue ch, unsigned id)
2029     : SDNode(NodeTy, getSDVTList(MVT::Other)), LabelID(id) {
2030     InitOperands(&Chain, ch);
2031   }
2032 public:
2033   unsigned getLabelID() const { return LabelID; }
2034
2035   static bool classof(const LabelSDNode *) { return true; }
2036   static bool classof(const SDNode *N) {
2037     return N->getOpcode() == ISD::DBG_LABEL ||
2038            N->getOpcode() == ISD::EH_LABEL;
2039   }
2040 };
2041
2042 class ExternalSymbolSDNode : public SDNode {
2043   const char *Symbol;
2044 protected:
2045   friend class SelectionDAG;
2046   ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT VT)
2047     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
2048              getSDVTList(VT)), Symbol(Sym) {
2049   }
2050 public:
2051
2052   const char *getSymbol() const { return Symbol; }
2053
2054   static bool classof(const ExternalSymbolSDNode *) { return true; }
2055   static bool classof(const SDNode *N) {
2056     return N->getOpcode() == ISD::ExternalSymbol ||
2057            N->getOpcode() == ISD::TargetExternalSymbol;
2058   }
2059 };
2060
2061 class CondCodeSDNode : public SDNode {
2062   ISD::CondCode Condition;
2063 protected:
2064   friend class SelectionDAG;
2065   explicit CondCodeSDNode(ISD::CondCode Cond)
2066     : SDNode(ISD::CONDCODE, getSDVTList(MVT::Other)), Condition(Cond) {
2067   }
2068 public:
2069
2070   ISD::CondCode get() const { return Condition; }
2071
2072   static bool classof(const CondCodeSDNode *) { return true; }
2073   static bool classof(const SDNode *N) {
2074     return N->getOpcode() == ISD::CONDCODE;
2075   }
2076 };
2077
2078 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
2079 /// future and most targets don't support it.
2080 class CvtRndSatSDNode : public SDNode {
2081   ISD::CvtCode CvtCode;
2082 protected:
2083   friend class SelectionDAG;
2084   explicit CvtRndSatSDNode(MVT VT, const SDValue *Ops, unsigned NumOps,
2085                            ISD::CvtCode Code)
2086     : SDNode(ISD::CONVERT_RNDSAT, getSDVTList(VT), Ops, NumOps), CvtCode(Code) {
2087     assert(NumOps == 5 && "wrong number of operations");
2088   }
2089 public:
2090   ISD::CvtCode getCvtCode() const { return CvtCode; }
2091
2092   static bool classof(const CvtRndSatSDNode *) { return true; }
2093   static bool classof(const SDNode *N) {
2094     return N->getOpcode() == ISD::CONVERT_RNDSAT;
2095   }
2096 };
2097
2098 namespace ISD {
2099   struct ArgFlagsTy {
2100   private:
2101     static const uint64_t NoFlagSet      = 0ULL;
2102     static const uint64_t ZExt           = 1ULL<<0;  ///< Zero extended
2103     static const uint64_t ZExtOffs       = 0;
2104     static const uint64_t SExt           = 1ULL<<1;  ///< Sign extended
2105     static const uint64_t SExtOffs       = 1;
2106     static const uint64_t InReg          = 1ULL<<2;  ///< Passed in register
2107     static const uint64_t InRegOffs      = 2;
2108     static const uint64_t SRet           = 1ULL<<3;  ///< Hidden struct-ret ptr
2109     static const uint64_t SRetOffs       = 3;
2110     static const uint64_t ByVal          = 1ULL<<4;  ///< Struct passed by value
2111     static const uint64_t ByValOffs      = 4;
2112     static const uint64_t Nest           = 1ULL<<5;  ///< Nested fn static chain
2113     static const uint64_t NestOffs       = 5;
2114     static const uint64_t ByValAlign     = 0xFULL << 6; //< Struct alignment
2115     static const uint64_t ByValAlignOffs = 6;
2116     static const uint64_t Split          = 1ULL << 10;
2117     static const uint64_t SplitOffs      = 10;
2118     static const uint64_t OrigAlign      = 0x1FULL<<27;
2119     static const uint64_t OrigAlignOffs  = 27;
2120     static const uint64_t ByValSize      = 0xffffffffULL << 32; //< Struct size
2121     static const uint64_t ByValSizeOffs  = 32;
2122
2123     static const uint64_t One            = 1ULL; //< 1 of this type, for shifts
2124
2125     uint64_t Flags;
2126   public:
2127     ArgFlagsTy() : Flags(0) { }
2128
2129     bool isZExt()   const { return Flags & ZExt; }
2130     void setZExt()  { Flags |= One << ZExtOffs; }
2131
2132     bool isSExt()   const { return Flags & SExt; }
2133     void setSExt()  { Flags |= One << SExtOffs; }
2134
2135     bool isInReg()  const { return Flags & InReg; }
2136     void setInReg() { Flags |= One << InRegOffs; }
2137
2138     bool isSRet()   const { return Flags & SRet; }
2139     void setSRet()  { Flags |= One << SRetOffs; }
2140
2141     bool isByVal()  const { return Flags & ByVal; }
2142     void setByVal() { Flags |= One << ByValOffs; }
2143
2144     bool isNest()   const { return Flags & Nest; }
2145     void setNest()  { Flags |= One << NestOffs; }
2146
2147     unsigned getByValAlign() const {
2148       return (unsigned) 
2149         ((One << ((Flags & ByValAlign) >> ByValAlignOffs)) / 2);
2150     }
2151     void setByValAlign(unsigned A) {
2152       Flags = (Flags & ~ByValAlign) |
2153         (uint64_t(Log2_32(A) + 1) << ByValAlignOffs);
2154     }
2155             
2156     bool isSplit()   const { return Flags & Split; }
2157     void setSplit()  { Flags |= One << SplitOffs; }
2158
2159     unsigned getOrigAlign() const {
2160       return (unsigned)
2161         ((One << ((Flags & OrigAlign) >> OrigAlignOffs)) / 2);
2162     }
2163     void setOrigAlign(unsigned A) {
2164       Flags = (Flags & ~OrigAlign) |
2165         (uint64_t(Log2_32(A) + 1) << OrigAlignOffs);
2166     }
2167
2168     unsigned getByValSize() const {
2169       return (unsigned)((Flags & ByValSize) >> ByValSizeOffs);
2170     }
2171     void setByValSize(unsigned S) {
2172       Flags = (Flags & ~ByValSize) | (uint64_t(S) << ByValSizeOffs);
2173     }
2174
2175     /// getArgFlagsString - Returns the flags as a string, eg: "zext align:4".
2176     std::string getArgFlagsString();
2177
2178     /// getRawBits - Represent the flags as a bunch of bits.
2179     uint64_t getRawBits() const { return Flags; }
2180   };
2181 }
2182
2183 /// ARG_FLAGSSDNode - Leaf node holding parameter flags.
2184 class ARG_FLAGSSDNode : public SDNode {
2185   ISD::ArgFlagsTy TheFlags;
2186 protected:
2187   friend class SelectionDAG;
2188   explicit ARG_FLAGSSDNode(ISD::ArgFlagsTy Flags)
2189     : SDNode(ISD::ARG_FLAGS, getSDVTList(MVT::Other)), TheFlags(Flags) {
2190   }
2191 public:
2192   ISD::ArgFlagsTy getArgFlags() const { return TheFlags; }
2193
2194   static bool classof(const ARG_FLAGSSDNode *) { return true; }
2195   static bool classof(const SDNode *N) {
2196     return N->getOpcode() == ISD::ARG_FLAGS;
2197   }
2198 };
2199
2200 /// CallSDNode - Node for calls -- ISD::CALL.
2201 class CallSDNode : public SDNode {
2202   unsigned CallingConv;
2203   bool IsVarArg;
2204   bool IsTailCall;
2205   // We might eventually want a full-blown Attributes for the result; that
2206   // will expand the size of the representation.  At the moment we only
2207   // need Inreg.
2208   bool Inreg;
2209 protected:
2210   friend class SelectionDAG;
2211   CallSDNode(unsigned cc, bool isvararg, bool istailcall, bool isinreg,
2212              SDVTList VTs, const SDValue *Operands, unsigned numOperands)
2213     : SDNode(ISD::CALL, VTs, Operands, numOperands),
2214       CallingConv(cc), IsVarArg(isvararg), IsTailCall(istailcall),
2215       Inreg(isinreg) {}
2216 public:
2217   unsigned getCallingConv() const { return CallingConv; }
2218   unsigned isVarArg() const { return IsVarArg; }
2219   unsigned isTailCall() const { return IsTailCall; }
2220   unsigned isInreg() const { return Inreg; }
2221
2222   /// Set this call to not be marked as a tail call. Normally setter
2223   /// methods in SDNodes are unsafe because it breaks the CSE map,
2224   /// but we don't include the tail call flag for calls so it's ok
2225   /// in this case.
2226   void setNotTailCall() { IsTailCall = false; }
2227
2228   SDValue getChain() const { return getOperand(0); }
2229   SDValue getCallee() const { return getOperand(1); }
2230
2231   unsigned getNumArgs() const { return (getNumOperands() - 2) / 2; }
2232   SDValue getArg(unsigned i) const { return getOperand(2+2*i); }
2233   SDValue getArgFlagsVal(unsigned i) const {
2234     return getOperand(3+2*i);
2235   }
2236   ISD::ArgFlagsTy getArgFlags(unsigned i) const {
2237     return cast<ARG_FLAGSSDNode>(getArgFlagsVal(i).getNode())->getArgFlags();
2238   }
2239
2240   unsigned getNumRetVals() const { return getNumValues() - 1; }
2241   MVT getRetValType(unsigned i) const { return getValueType(i); }
2242
2243   static bool classof(const CallSDNode *) { return true; }
2244   static bool classof(const SDNode *N) {
2245     return N->getOpcode() == ISD::CALL;
2246   }
2247 };
2248
2249 /// VTSDNode - This class is used to represent MVT's, which are used
2250 /// to parameterize some operations.
2251 class VTSDNode : public SDNode {
2252   MVT ValueType;
2253 protected:
2254   friend class SelectionDAG;
2255   explicit VTSDNode(MVT VT)
2256     : SDNode(ISD::VALUETYPE, getSDVTList(MVT::Other)), ValueType(VT) {
2257   }
2258 public:
2259
2260   MVT getVT() const { return ValueType; }
2261
2262   static bool classof(const VTSDNode *) { return true; }
2263   static bool classof(const SDNode *N) {
2264     return N->getOpcode() == ISD::VALUETYPE;
2265   }
2266 };
2267
2268 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
2269 ///
2270 class LSBaseSDNode : public MemSDNode {
2271 protected:
2272   //! Operand array for load and store
2273   /*!
2274     \note Moving this array to the base class captures more
2275     common functionality shared between LoadSDNode and
2276     StoreSDNode
2277    */
2278   SDUse Ops[4];
2279 public:
2280   LSBaseSDNode(ISD::NodeType NodeTy, SDValue *Operands, unsigned numOperands,
2281                SDVTList VTs, ISD::MemIndexedMode AM, MVT VT,
2282                const Value *SV, int SVO, unsigned Align, bool Vol)
2283     : MemSDNode(NodeTy, VTs, VT, SV, SVO, Align, Vol) {
2284     SubclassData = AM;
2285     InitOperands(Ops, Operands, numOperands);
2286     assert(Align != 0 && "Loads and stores should have non-zero aligment");
2287     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
2288            "Only indexed loads and stores have a non-undef offset operand");
2289   }
2290
2291   const SDValue &getOffset() const {
2292     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
2293   }
2294
2295   /// getAddressingMode - Return the addressing mode for this load or store:
2296   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2297   ISD::MemIndexedMode getAddressingMode() const {
2298     return ISD::MemIndexedMode(SubclassData & 7);
2299   }
2300
2301   /// isIndexed - Return true if this is a pre/post inc/dec load/store.
2302   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2303
2304   /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
2305   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2306
2307   static bool classof(const LSBaseSDNode *) { return true; }
2308   static bool classof(const SDNode *N) {
2309     return N->getOpcode() == ISD::LOAD ||
2310            N->getOpcode() == ISD::STORE;
2311   }
2312 };
2313
2314 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
2315 ///
2316 class LoadSDNode : public LSBaseSDNode {
2317 protected:
2318   friend class SelectionDAG;
2319   LoadSDNode(SDValue *ChainPtrOff, SDVTList VTs,
2320              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, MVT LVT,
2321              const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
2322     : LSBaseSDNode(ISD::LOAD, ChainPtrOff, 3,
2323                    VTs, AM, LVT, SV, O, Align, Vol) {
2324     SubclassData |= (unsigned short)ETy << 3;
2325   }
2326 public:
2327
2328   /// getExtensionType - Return whether this is a plain node,
2329   /// or one of the varieties of value-extending loads.
2330   ISD::LoadExtType getExtensionType() const {
2331     return ISD::LoadExtType((SubclassData >> 3) & 3);
2332   }
2333
2334   const SDValue &getBasePtr() const { return getOperand(1); }
2335   const SDValue &getOffset() const { return getOperand(2); }
2336   
2337   static bool classof(const LoadSDNode *) { return true; }
2338   static bool classof(const SDNode *N) {
2339     return N->getOpcode() == ISD::LOAD;
2340   }
2341 };
2342
2343 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
2344 ///
2345 class StoreSDNode : public LSBaseSDNode {
2346 protected:
2347   friend class SelectionDAG;
2348   StoreSDNode(SDValue *ChainValuePtrOff, SDVTList VTs,
2349               ISD::MemIndexedMode AM, bool isTrunc, MVT SVT,
2350               const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
2351     : LSBaseSDNode(ISD::STORE, ChainValuePtrOff, 4,
2352                    VTs, AM, SVT, SV, O, Align, Vol) {
2353     SubclassData |= (unsigned short)isTrunc << 3;
2354   }
2355 public:
2356
2357   /// isTruncatingStore - Return true if the op does a truncation before store.
2358   /// For integers this is the same as doing a TRUNCATE and storing the result.
2359   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2360   bool isTruncatingStore() const { return (SubclassData >> 3) & 1; }
2361
2362   const SDValue &getValue() const { return getOperand(1); }
2363   const SDValue &getBasePtr() const { return getOperand(2); }
2364   const SDValue &getOffset() const { return getOperand(3); }
2365   
2366   static bool classof(const StoreSDNode *) { return true; }
2367   static bool classof(const SDNode *N) {
2368     return N->getOpcode() == ISD::STORE;
2369   }
2370 };
2371
2372
2373 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
2374   SDNode *Node;
2375   unsigned Operand;
2376
2377   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2378 public:
2379   bool operator==(const SDNodeIterator& x) const {
2380     return Operand == x.Operand;
2381   }
2382   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2383
2384   const SDNodeIterator &operator=(const SDNodeIterator &I) {
2385     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
2386     Operand = I.Operand;
2387     return *this;
2388   }
2389
2390   pointer operator*() const {
2391     return Node->getOperand(Operand).getNode();
2392   }
2393   pointer operator->() const { return operator*(); }
2394
2395   SDNodeIterator& operator++() {                // Preincrement
2396     ++Operand;
2397     return *this;
2398   }
2399   SDNodeIterator operator++(int) { // Postincrement
2400     SDNodeIterator tmp = *this; ++*this; return tmp;
2401   }
2402
2403   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
2404   static SDNodeIterator end  (SDNode *N) {
2405     return SDNodeIterator(N, N->getNumOperands());
2406   }
2407
2408   unsigned getOperand() const { return Operand; }
2409   const SDNode *getNode() const { return Node; }
2410 };
2411
2412 template <> struct GraphTraits<SDNode*> {
2413   typedef SDNode NodeType;
2414   typedef SDNodeIterator ChildIteratorType;
2415   static inline NodeType *getEntryNode(SDNode *N) { return N; }
2416   static inline ChildIteratorType child_begin(NodeType *N) {
2417     return SDNodeIterator::begin(N);
2418   }
2419   static inline ChildIteratorType child_end(NodeType *N) {
2420     return SDNodeIterator::end(N);
2421   }
2422 };
2423
2424 /// LargestSDNode - The largest SDNode class.
2425 ///
2426 typedef LoadSDNode LargestSDNode;
2427
2428 /// MostAlignedSDNode - The SDNode class with the greatest alignment
2429 /// requirement.
2430 ///
2431 typedef ARG_FLAGSSDNode MostAlignedSDNode;
2432
2433 namespace ISD {
2434   /// isNormalLoad - Returns true if the specified node is a non-extending
2435   /// and unindexed load.
2436   inline bool isNormalLoad(const SDNode *N) {
2437     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2438     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2439       Ld->getAddressingMode() == ISD::UNINDEXED;
2440   }
2441
2442   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
2443   /// load.
2444   inline bool isNON_EXTLoad(const SDNode *N) {
2445     return isa<LoadSDNode>(N) &&
2446       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2447   }
2448
2449   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
2450   ///
2451   inline bool isEXTLoad(const SDNode *N) {
2452     return isa<LoadSDNode>(N) &&
2453       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2454   }
2455
2456   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
2457   ///
2458   inline bool isSEXTLoad(const SDNode *N) {
2459     return isa<LoadSDNode>(N) &&
2460       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2461   }
2462
2463   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
2464   ///
2465   inline bool isZEXTLoad(const SDNode *N) {
2466     return isa<LoadSDNode>(N) &&
2467       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2468   }
2469
2470   /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
2471   ///
2472   inline bool isUNINDEXEDLoad(const SDNode *N) {
2473     return isa<LoadSDNode>(N) &&
2474       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2475   }
2476
2477   /// isNormalStore - Returns true if the specified node is a non-truncating
2478   /// and unindexed store.
2479   inline bool isNormalStore(const SDNode *N) {
2480     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2481     return St && !St->isTruncatingStore() &&
2482       St->getAddressingMode() == ISD::UNINDEXED;
2483   }
2484
2485   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
2486   /// store.
2487   inline bool isNON_TRUNCStore(const SDNode *N) {
2488     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
2489   }
2490
2491   /// isTRUNCStore - Returns true if the specified node is a truncating
2492   /// store.
2493   inline bool isTRUNCStore(const SDNode *N) {
2494     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
2495   }
2496
2497   /// isUNINDEXEDStore - Returns true if the specified node is an
2498   /// unindexed store.
2499   inline bool isUNINDEXEDStore(const SDNode *N) {
2500     return isa<StoreSDNode>(N) &&
2501       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2502   }
2503 }
2504
2505
2506 } // end llvm namespace
2507
2508 #endif