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