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