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