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