Add selection DAG nodes for subreg insert/extract. PR1350
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Value.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/iterator"
26 #include "llvm/CodeGen/ValueTypes.h"
27 #include "llvm/Support/DataTypes.h"
28 #include <cassert>
29
30 namespace llvm {
31
32 class SelectionDAG;
33 class GlobalValue;
34 class MachineBasicBlock;
35 class MachineConstantPoolValue;
36 class SDNode;
37 template <typename T> struct DenseMapKeyInfo;
38 template <typename T> struct simplify_type;
39 template <typename T> struct ilist_traits;
40 template<typename NodeTy, typename Traits> class iplist;
41 template<typename NodeTy> class ilist_iterator;
42
43 /// SDVTList - This represents a list of ValueType's that has been intern'd by
44 /// a SelectionDAG.  Instances of this simple value class are returned by
45 /// SelectionDAG::getVTList(...).
46 ///
47 struct SDVTList {
48   const MVT::ValueType *VTs;
49   unsigned short NumVTs;
50 };
51
52 /// ISD namespace - This namespace contains an enum which represents all of the
53 /// SelectionDAG node types and value types.
54 ///
55 namespace ISD {
56   namespace ParamFlags {    
57   enum Flags {
58     NoFlagSet         = 0,
59     ZExt              = 1<<0,  ///< Parameter should be zero extended
60     ZExtOffs          = 0,
61     SExt              = 1<<1,  ///< Parameter should be sign extended
62     SExtOffs          = 1,
63     InReg             = 1<<2,  ///< Parameter should be passed in register
64     InRegOffs         = 2,
65     StructReturn      = 1<<3,  ///< Hidden struct-return pointer
66     StructReturnOffs  = 3,
67     ByVal             = 1<<4,  ///< Struct passed by value
68     ByValOffs         = 4,
69     OrigAlignment     = 0x1F<<27,
70     OrigAlignmentOffs = 27
71   };
72   }
73
74   //===--------------------------------------------------------------------===//
75   /// ISD::NodeType enum - This enum defines all of the operators valid in a
76   /// SelectionDAG.
77   ///
78   enum NodeType {
79     // DELETED_NODE - This is an illegal flag 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     // Token factor - 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     STRING, BasicBlock, VALUETYPE, 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 RegSDNode 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, CC#, ISVARARG, ISTAILCALL, 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     /// Bits 31:27 - argument ABI alignment in the first argument piece and
202     /// alignment '1' in other argument pieces.
203     CALL,
204
205     // EXTRACT_ELEMENT - This is used to get the first or second (determined by
206     // a Constant, which is required to be operand #1), element of the aggregate
207     // value specified as operand #0.  This is only for use before legalization,
208     // for values that will be broken into multiple registers.
209     EXTRACT_ELEMENT,
210
211     // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
212     // two values of the same integer value type, this produces a value twice as
213     // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
214     BUILD_PAIR,
215     
216     // MERGE_VALUES - This node takes multiple discrete operands and returns
217     // them all as its individual results.  This nodes has exactly the same
218     // number of inputs and outputs, and is only valid before legalization.
219     // This node is useful for some pieces of the code generator that want to
220     // think about a single node with multiple results, not multiple nodes.
221     MERGE_VALUES,
222
223     // Simple integer binary arithmetic operators.
224     ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
225     
226     // CARRY_FALSE - This node is used when folding other nodes,
227     // like ADDC/SUBC, which indicate the carry result is always false.
228     CARRY_FALSE,
229     
230     // Carry-setting nodes for multiple precision addition and subtraction.
231     // These nodes take two operands of the same value type, and produce two
232     // results.  The first result is the normal add or sub result, the second
233     // result is the carry flag result.
234     ADDC, SUBC,
235     
236     // Carry-using nodes for multiple precision addition and subtraction.  These
237     // nodes take three operands: The first two are the normal lhs and rhs to
238     // the add or sub, and the third is the input carry flag.  These nodes
239     // produce two results; the normal result of the add or sub, and the output
240     // carry flag.  These nodes both read and write a carry flag to allow them
241     // to them to be chained together for add and sub of arbitrarily large
242     // values.
243     ADDE, SUBE,
244     
245     // Simple binary floating point operators.
246     FADD, FSUB, FMUL, FDIV, FREM,
247
248     // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.  NOTE: This
249     // DAG node does not require that X and Y have the same type, just that they
250     // are both floating point.  X and the result must have the same type.
251     // FCOPYSIGN(f32, f64) is allowed.
252     FCOPYSIGN,
253
254     /// BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector
255     /// with the specified, possibly variable, elements.  The number of elements
256     /// is required to be a power of two.
257     BUILD_VECTOR,
258     
259     /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element
260     /// at IDX replaced with VAL.
261     INSERT_VECTOR_ELT,
262
263     /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
264     /// identified by the (potentially variable) element number IDX.
265     EXTRACT_VECTOR_ELT,
266     
267     /// CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of
268     /// vector type with the same length and element type, this produces a
269     /// concatenated vector result value, with length equal to the sum of the
270     /// lengths of the input vectors.
271     CONCAT_VECTORS,
272     
273     /// EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an
274     /// vector value) starting with the (potentially variable) element number
275     /// IDX, which must be a multiple of the result vector length.
276     EXTRACT_SUBVECTOR,
277     
278     /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
279     /// type as VEC1/VEC2.  SHUFFLEVEC is a BUILD_VECTOR of constant int values
280     /// (regardless of whether its datatype is legal or not) that indicate
281     /// which value each result element will get.  The elements of VEC1/VEC2 are
282     /// enumerated in order.  This is quite similar to the Altivec 'vperm'
283     /// instruction, except that the indices must be constants and are in terms
284     /// of the element size of VEC1/VEC2, not in terms of bytes.
285     VECTOR_SHUFFLE,
286     
287     /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
288     /// scalar value into the low element of the resultant vector type.  The top
289     /// elements of the vector are undefined.
290     SCALAR_TO_VECTOR,
291     
292     // EXTRACT_SUBREG - This node is used to extract a sub-register value. 
293     // This node takes a superreg and a constant sub-register index as operands.
294     EXTRACT_SUBREG,
295     
296     // INSERT_SUBREG - This node is used to insert a sub-register value. 
297     // This node takes a superreg, a subreg value, and a constant sub-register
298     // index as operands.
299     INSERT_SUBREG,
300     
301     // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
302     // an unsigned/signed value of type i[2*n], then return the top part.
303     MULHU, MULHS,
304
305     // Bitwise operators - logical and, logical or, logical xor, shift left,
306     // shift right algebraic (shift in sign bits), shift right logical (shift in
307     // zeroes), rotate left, rotate right, and byteswap.
308     AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
309
310     // Counting operators
311     CTTZ, CTLZ, CTPOP,
312
313     // Select(COND, TRUEVAL, FALSEVAL)
314     SELECT, 
315     
316     // Select with condition operator - This selects between a true value and 
317     // a false value (ops #2 and #3) based on the boolean result of comparing
318     // the lhs and rhs (ops #0 and #1) of a conditional expression with the 
319     // condition code in op #4, a CondCodeSDNode.
320     SELECT_CC,
321
322     // SetCC operator - This evaluates to a boolean (i1) true value if the
323     // condition is true.  The operands to this are the left and right operands
324     // to compare (ops #0, and #1) and the condition code to compare them with
325     // (op #2) as a CondCodeSDNode.
326     SETCC,
327
328     // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
329     // integer shift operations, just like ADD/SUB_PARTS.  The operation
330     // ordering is:
331     //       [Lo,Hi] = op [LoLHS,HiLHS], Amt
332     SHL_PARTS, SRA_PARTS, SRL_PARTS,
333
334     // Conversion operators.  These are all single input single output
335     // operations.  For all of these, the result type must be strictly
336     // wider or narrower (depending on the operation) than the source
337     // type.
338
339     // SIGN_EXTEND - Used for integer types, replicating the sign bit
340     // into new bits.
341     SIGN_EXTEND,
342
343     // ZERO_EXTEND - Used for integer types, zeroing the new bits.
344     ZERO_EXTEND,
345
346     // ANY_EXTEND - Used for integer types.  The high bits are undefined.
347     ANY_EXTEND,
348     
349     // TRUNCATE - Completely drop the high bits.
350     TRUNCATE,
351
352     // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
353     // depends on the first letter) to floating point.
354     SINT_TO_FP,
355     UINT_TO_FP,
356
357     // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
358     // sign extend a small value in a large integer register (e.g. sign
359     // extending the low 8 bits of a 32-bit register to fill the top 24 bits
360     // with the 7th bit).  The size of the smaller type is indicated by the 1th
361     // operand, a ValueType node.
362     SIGN_EXTEND_INREG,
363
364     // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
365     // integer.
366     FP_TO_SINT,
367     FP_TO_UINT,
368
369     // FP_ROUND - Perform a rounding operation from the current
370     // precision down to the specified precision (currently always 64->32).
371     FP_ROUND,
372
373     // FP_ROUND_INREG - This operator takes a floating point register, and
374     // rounds it to a floating point value.  It then promotes it and returns it
375     // in a register of the same size.  This operation effectively just discards
376     // excess precision.  The type to round down to is specified by the 1th
377     // operation, a VTSDNode (currently always 64->32->64).
378     FP_ROUND_INREG,
379
380     // FP_EXTEND - Extend a smaller FP type into a larger FP type.
381     FP_EXTEND,
382
383     // BIT_CONVERT - Theis operator converts between integer and FP values, as
384     // if one was stored to memory as integer and the other was loaded from the
385     // same address (or equivalently for vector format conversions, etc).  The 
386     // source and result are required to have the same bit size (e.g. 
387     // f32 <-> i32).  This can also be used for int-to-int or fp-to-fp 
388     // conversions, but that is a noop, deleted by getNode().
389     BIT_CONVERT,
390     
391     // FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI - Perform unary floating point
392     // negation, absolute value, square root, sine and cosine, and powi
393     // operations.
394     FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI,
395     
396     // LOAD and STORE have token chains as their first operand, then the same
397     // operands as an LLVM load/store instruction, then an offset node that
398     // is added / subtracted from the base pointer to form the address (for
399     // indexed memory ops).
400     LOAD, STORE,
401     
402     // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
403     // value and stores it to memory in one operation.  This can be used for
404     // either integer or floating point operands.  The first four operands of
405     // this are the same as a standard store.  The fifth is the ValueType to
406     // store it as (which will be smaller than the source value).
407     TRUNCSTORE,
408
409     // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
410     // to a specified boundary.  This node always has two return values: a new
411     // stack pointer value and a chain. The first operand is the token chain,
412     // the second is the number of bytes to allocate, and the third is the
413     // alignment boundary.  The size is guaranteed to be a multiple of the stack
414     // alignment, and the alignment is guaranteed to be bigger than the stack
415     // alignment (if required) or 0 to get standard stack alignment.
416     DYNAMIC_STACKALLOC,
417
418     // Control flow instructions.  These all have token chains.
419
420     // BR - Unconditional branch.  The first operand is the chain
421     // operand, the second is the MBB to branch to.
422     BR,
423
424     // BRIND - Indirect branch.  The first operand is the chain, the second
425     // is the value to branch to, which must be of the same type as the target's
426     // pointer type.
427     BRIND,
428
429     // BR_JT - Jumptable branch. The first operand is the chain, the second
430     // is the jumptable index, the last one is the jumptable entry index.
431     BR_JT,
432     
433     // BRCOND - Conditional branch.  The first operand is the chain,
434     // the second is the condition, the third is the block to branch
435     // to if the condition is true.
436     BRCOND,
437
438     // BR_CC - Conditional branch.  The behavior is like that of SELECT_CC, in
439     // that the condition is represented as condition code, and two nodes to
440     // compare, rather than as a combined SetCC node.  The operands in order are
441     // chain, cc, lhs, rhs, block to branch to if condition is true.
442     BR_CC,
443     
444     // RET - Return from function.  The first operand is the chain,
445     // and any subsequent operands are pairs of return value and return value
446     // signness for the function.  This operation can have variable number of
447     // operands.
448     RET,
449
450     // INLINEASM - Represents an inline asm block.  This node always has two
451     // return values: a chain and a flag result.  The inputs are as follows:
452     //   Operand #0   : Input chain.
453     //   Operand #1   : a ExternalSymbolSDNode with a pointer to the asm string.
454     //   Operand #2n+2: A RegisterNode.
455     //   Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
456     //   Operand #last: Optional, an incoming flag.
457     INLINEASM,
458     
459     // LABEL - Represents a label in mid basic block used to track
460     // locations needed for debug and exception handling tables.  This node
461     // returns a chain.
462     //   Operand #0 : input chain.
463     //   Operand #1 : module unique number use to identify the label.
464     LABEL,
465     
466     // STACKSAVE - STACKSAVE has one operand, an input chain.  It produces a
467     // value, the same type as the pointer type for the system, and an output
468     // chain.
469     STACKSAVE,
470     
471     // STACKRESTORE has two operands, an input chain and a pointer to restore to
472     // it returns an output chain.
473     STACKRESTORE,
474     
475     // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
476     // correspond to the operands of the LLVM intrinsic functions.  The only
477     // result is a token chain.  The alignment argument is guaranteed to be a
478     // Constant node.
479     MEMSET,
480     MEMMOVE,
481     MEMCPY,
482
483     // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
484     // a call sequence, and carry arbitrary information that target might want
485     // to know.  The first operand is a chain, the rest are specified by the
486     // target and not touched by the DAG optimizers.
487     CALLSEQ_START,  // Beginning of a call sequence
488     CALLSEQ_END,    // End of a call sequence
489     
490     // VAARG - VAARG has three operands: an input chain, a pointer, and a 
491     // SRCVALUE.  It returns a pair of values: the vaarg value and a new chain.
492     VAARG,
493     
494     // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
495     // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
496     // source.
497     VACOPY,
498     
499     // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
500     // pointer, and a SRCVALUE.
501     VAEND, VASTART,
502
503     // SRCVALUE - This corresponds to a Value*, and is used to associate memory
504     // locations with their value.  This allows one use alias analysis
505     // information in the backend.
506     SRCVALUE,
507
508     // PCMARKER - This corresponds to the pcmarker intrinsic.
509     PCMARKER,
510
511     // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
512     // The only operand is a chain and a value and a chain are produced.  The
513     // value is the contents of the architecture specific cycle counter like 
514     // register (or other high accuracy low latency clock source)
515     READCYCLECOUNTER,
516
517     // HANDLENODE node - Used as a handle for various purposes.
518     HANDLENODE,
519
520     // LOCATION - This node is used to represent a source location for debug
521     // info.  It takes token chain as input, then a line number, then a column
522     // number, then a filename, then a working dir.  It produces a token chain
523     // as output.
524     LOCATION,
525     
526     // DEBUG_LOC - This node is used to represent source line information
527     // embedded in the code.  It takes a token chain as input, then a line
528     // number, then a column then a file id (provided by MachineModuleInfo.) It
529     // produces a token chain as output.
530     DEBUG_LOC,
531     
532     // BUILTIN_OP_END - This must be the last enum value in this list.
533     BUILTIN_OP_END
534   };
535
536   /// Node predicates
537
538   /// isBuildVectorAllOnes - Return true if the specified node is a
539   /// BUILD_VECTOR where all of the elements are ~0 or undef.
540   bool isBuildVectorAllOnes(const SDNode *N);
541
542   /// isBuildVectorAllZeros - Return true if the specified node is a
543   /// BUILD_VECTOR where all of the elements are 0 or undef.
544   bool isBuildVectorAllZeros(const SDNode *N);
545   
546   //===--------------------------------------------------------------------===//
547   /// MemIndexedMode enum - This enum defines the load / store indexed 
548   /// addressing modes.
549   ///
550   /// UNINDEXED    "Normal" load / store. The effective address is already
551   ///              computed and is available in the base pointer. The offset
552   ///              operand is always undefined. In addition to producing a
553   ///              chain, an unindexed load produces one value (result of the
554   ///              load); an unindexed store does not produces a value.
555   ///
556   /// PRE_INC      Similar to the unindexed mode where the effective address is
557   /// PRE_DEC      the value of the base pointer add / subtract the offset.
558   ///              It considers the computation as being folded into the load /
559   ///              store operation (i.e. the load / store does the address
560   ///              computation as well as performing the memory transaction).
561   ///              The base operand is always undefined. In addition to
562   ///              producing a chain, pre-indexed load produces two values
563   ///              (result of the load and the result of the address
564   ///              computation); a pre-indexed store produces one value (result
565   ///              of the address computation).
566   ///
567   /// POST_INC     The effective address is the value of the base pointer. The
568   /// POST_DEC     value of the offset operand is then added to / subtracted
569   ///              from the base after memory transaction. In addition to
570   ///              producing a chain, post-indexed load produces two values
571   ///              (the result of the load and the result of the base +/- offset
572   ///              computation); a post-indexed store produces one value (the
573   ///              the result of the base +/- offset computation).
574   ///
575   enum MemIndexedMode {
576     UNINDEXED = 0,
577     PRE_INC,
578     PRE_DEC,
579     POST_INC,
580     POST_DEC,
581     LAST_INDEXED_MODE
582   };
583
584   //===--------------------------------------------------------------------===//
585   /// LoadExtType enum - This enum defines the three variants of LOADEXT
586   /// (load with extension).
587   ///
588   /// SEXTLOAD loads the integer operand and sign extends it to a larger
589   ///          integer result type.
590   /// ZEXTLOAD loads the integer operand and zero extends it to a larger
591   ///          integer result type.
592   /// EXTLOAD  is used for three things: floating point extending loads, 
593   ///          integer extending loads [the top bits are undefined], and vector
594   ///          extending loads [load into low elt].
595   ///
596   enum LoadExtType {
597     NON_EXTLOAD = 0,
598     EXTLOAD,
599     SEXTLOAD,
600     ZEXTLOAD,
601     LAST_LOADX_TYPE
602   };
603
604   //===--------------------------------------------------------------------===//
605   /// ISD::CondCode enum - These are ordered carefully to make the bitfields
606   /// below work out, when considering SETFALSE (something that never exists
607   /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
608   /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
609   /// to.  If the "N" column is 1, the result of the comparison is undefined if
610   /// the input is a NAN.
611   ///
612   /// All of these (except for the 'always folded ops') should be handled for
613   /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
614   /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
615   ///
616   /// Note that these are laid out in a specific order to allow bit-twiddling
617   /// to transform conditions.
618   enum CondCode {
619     // Opcode          N U L G E       Intuitive operation
620     SETFALSE,      //    0 0 0 0       Always false (always folded)
621     SETOEQ,        //    0 0 0 1       True if ordered and equal
622     SETOGT,        //    0 0 1 0       True if ordered and greater than
623     SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
624     SETOLT,        //    0 1 0 0       True if ordered and less than
625     SETOLE,        //    0 1 0 1       True if ordered and less than or equal
626     SETONE,        //    0 1 1 0       True if ordered and operands are unequal
627     SETO,          //    0 1 1 1       True if ordered (no nans)
628     SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
629     SETUEQ,        //    1 0 0 1       True if unordered or equal
630     SETUGT,        //    1 0 1 0       True if unordered or greater than
631     SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
632     SETULT,        //    1 1 0 0       True if unordered or less than
633     SETULE,        //    1 1 0 1       True if unordered, less than, or equal
634     SETUNE,        //    1 1 1 0       True if unordered or not equal
635     SETTRUE,       //    1 1 1 1       Always true (always folded)
636     // Don't care operations: undefined if the input is a nan.
637     SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
638     SETEQ,         //  1 X 0 0 1       True if equal
639     SETGT,         //  1 X 0 1 0       True if greater than
640     SETGE,         //  1 X 0 1 1       True if greater than or equal
641     SETLT,         //  1 X 1 0 0       True if less than
642     SETLE,         //  1 X 1 0 1       True if less than or equal
643     SETNE,         //  1 X 1 1 0       True if not equal
644     SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
645
646     SETCC_INVALID       // Marker value.
647   };
648
649   /// isSignedIntSetCC - Return true if this is a setcc instruction that
650   /// performs a signed comparison when used with integer operands.
651   inline bool isSignedIntSetCC(CondCode Code) {
652     return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
653   }
654
655   /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
656   /// performs an unsigned comparison when used with integer operands.
657   inline bool isUnsignedIntSetCC(CondCode Code) {
658     return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
659   }
660
661   /// isTrueWhenEqual - Return true if the specified condition returns true if
662   /// the two operands to the condition are equal.  Note that if one of the two
663   /// operands is a NaN, this value is meaningless.
664   inline bool isTrueWhenEqual(CondCode Cond) {
665     return ((int)Cond & 1) != 0;
666   }
667
668   /// getUnorderedFlavor - This function returns 0 if the condition is always
669   /// false if an operand is a NaN, 1 if the condition is always true if the
670   /// operand is a NaN, and 2 if the condition is undefined if the operand is a
671   /// NaN.
672   inline unsigned getUnorderedFlavor(CondCode Cond) {
673     return ((int)Cond >> 3) & 3;
674   }
675
676   /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
677   /// 'op' is a valid SetCC operation.
678   CondCode getSetCCInverse(CondCode Operation, bool isInteger);
679
680   /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
681   /// when given the operation for (X op Y).
682   CondCode getSetCCSwappedOperands(CondCode Operation);
683
684   /// getSetCCOrOperation - Return the result of a logical OR between different
685   /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
686   /// function returns SETCC_INVALID if it is not possible to represent the
687   /// resultant comparison.
688   CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
689
690   /// getSetCCAndOperation - Return the result of a logical AND between
691   /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
692   /// function returns SETCC_INVALID if it is not possible to represent the
693   /// resultant comparison.
694   CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
695 }  // end llvm::ISD namespace
696
697
698 //===----------------------------------------------------------------------===//
699 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
700 /// values as the result of a computation.  Many nodes return multiple values,
701 /// from loads (which define a token and a return value) to ADDC (which returns
702 /// a result and a carry value), to calls (which may return an arbitrary number
703 /// of values).
704 ///
705 /// As such, each use of a SelectionDAG computation must indicate the node that
706 /// computes it as well as which return value to use from that node.  This pair
707 /// of information is represented with the SDOperand value type.
708 ///
709 class SDOperand {
710 public:
711   SDNode *Val;        // The node defining the value we are using.
712   unsigned ResNo;     // Which return value of the node we are using.
713
714   SDOperand() : Val(0), ResNo(0) {}
715   SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
716
717   bool operator==(const SDOperand &O) const {
718     return Val == O.Val && ResNo == O.ResNo;
719   }
720   bool operator!=(const SDOperand &O) const {
721     return !operator==(O);
722   }
723   bool operator<(const SDOperand &O) const {
724     return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
725   }
726
727   SDOperand getValue(unsigned R) const {
728     return SDOperand(Val, R);
729   }
730
731   // isOperand - Return true if this node is an operand of N.
732   bool isOperand(SDNode *N) const;
733
734   /// getValueType - Return the ValueType of the referenced return value.
735   ///
736   inline MVT::ValueType getValueType() const;
737
738   // Forwarding methods - These forward to the corresponding methods in SDNode.
739   inline unsigned getOpcode() const;
740   inline unsigned getNumOperands() const;
741   inline const SDOperand &getOperand(unsigned i) const;
742   inline uint64_t getConstantOperandVal(unsigned i) const;
743   inline bool isTargetOpcode() const;
744   inline unsigned getTargetOpcode() const;
745
746   /// hasOneUse - Return true if there is exactly one operation using this
747   /// result value of the defining operator.
748   inline bool hasOneUse() const;
749 };
750
751
752 template<> struct DenseMapKeyInfo<SDOperand> {
753   static inline SDOperand getEmptyKey() { return SDOperand((SDNode*)-1, -1U); }
754   static inline SDOperand getTombstoneKey() { return SDOperand((SDNode*)-1, 0);}
755   static unsigned getHashValue(const SDOperand &Val) {
756     return (unsigned)((uintptr_t)Val.Val >> 4) ^
757            (unsigned)((uintptr_t)Val.Val >> 9) + Val.ResNo;
758   }
759   static bool isPod() { return true; }
760 };
761
762 /// simplify_type specializations - Allow casting operators to work directly on
763 /// SDOperands as if they were SDNode*'s.
764 template<> struct simplify_type<SDOperand> {
765   typedef SDNode* SimpleType;
766   static SimpleType getSimplifiedValue(const SDOperand &Val) {
767     return static_cast<SimpleType>(Val.Val);
768   }
769 };
770 template<> struct simplify_type<const SDOperand> {
771   typedef SDNode* SimpleType;
772   static SimpleType getSimplifiedValue(const SDOperand &Val) {
773     return static_cast<SimpleType>(Val.Val);
774   }
775 };
776
777
778 /// SDNode - Represents one node in the SelectionDAG.
779 ///
780 class SDNode : public FoldingSetNode {
781   /// NodeType - The operation that this node performs.
782   ///
783   unsigned short NodeType;
784   
785   /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
786   /// then they will be delete[]'d when the node is destroyed.
787   bool OperandsNeedDelete : 1;
788
789   /// NodeId - Unique id per SDNode in the DAG.
790   int NodeId;
791
792   /// OperandList - The values that are used by this operation.
793   ///
794   SDOperand *OperandList;
795   
796   /// ValueList - The types of the values this node defines.  SDNode's may
797   /// define multiple values simultaneously.
798   const MVT::ValueType *ValueList;
799
800   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
801   unsigned short NumOperands, NumValues;
802   
803   /// Prev/Next pointers - These pointers form the linked list of of the
804   /// AllNodes list in the current DAG.
805   SDNode *Prev, *Next;
806   friend struct ilist_traits<SDNode>;
807
808   /// Uses - These are all of the SDNode's that use a value produced by this
809   /// node.
810   SmallVector<SDNode*,3> Uses;
811   
812   // Out-of-line virtual method to give class a home.
813   virtual void ANCHOR();
814 public:
815   virtual ~SDNode() {
816     assert(NumOperands == 0 && "Operand list not cleared before deletion");
817     NodeType = ISD::DELETED_NODE;
818   }
819   
820   //===--------------------------------------------------------------------===//
821   //  Accessors
822   //
823   unsigned getOpcode()  const { return NodeType; }
824   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
825   unsigned getTargetOpcode() const {
826     assert(isTargetOpcode() && "Not a target opcode!");
827     return NodeType - ISD::BUILTIN_OP_END;
828   }
829
830   size_t use_size() const { return Uses.size(); }
831   bool use_empty() const { return Uses.empty(); }
832   bool hasOneUse() const { return Uses.size() == 1; }
833
834   /// getNodeId - Return the unique node id.
835   ///
836   int getNodeId() const { return NodeId; }
837
838   typedef SmallVector<SDNode*,3>::const_iterator use_iterator;
839   use_iterator use_begin() const { return Uses.begin(); }
840   use_iterator use_end() const { return Uses.end(); }
841
842   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
843   /// indicated value.  This method ignores uses of other values defined by this
844   /// operation.
845   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
846
847   /// isOnlyUse - Return true if this node is the only use of N.
848   ///
849   bool isOnlyUse(SDNode *N) const;
850
851   /// isOperand - Return true if this node is an operand of N.
852   ///
853   bool isOperand(SDNode *N) const;
854
855   /// isPredecessor - Return true if this node is a predecessor of N. This node
856   /// is either an operand of N or it can be reached by recursively traversing
857   /// up the operands.
858   /// NOTE: this is an expensive method. Use it carefully.
859   bool isPredecessor(SDNode *N) const;
860
861   /// getNumOperands - Return the number of values used by this operation.
862   ///
863   unsigned getNumOperands() const { return NumOperands; }
864
865   /// getConstantOperandVal - Helper method returns the integer value of a 
866   /// ConstantSDNode operand.
867   uint64_t getConstantOperandVal(unsigned Num) const;
868
869   const SDOperand &getOperand(unsigned Num) const {
870     assert(Num < NumOperands && "Invalid child # of SDNode!");
871     return OperandList[Num];
872   }
873
874   typedef const SDOperand* op_iterator;
875   op_iterator op_begin() const { return OperandList; }
876   op_iterator op_end() const { return OperandList+NumOperands; }
877
878
879   SDVTList getVTList() const {
880     SDVTList X = { ValueList, NumValues };
881     return X;
882   };
883   
884   /// getNumValues - Return the number of values defined/returned by this
885   /// operator.
886   ///
887   unsigned getNumValues() const { return NumValues; }
888
889   /// getValueType - Return the type of a specified result.
890   ///
891   MVT::ValueType getValueType(unsigned ResNo) const {
892     assert(ResNo < NumValues && "Illegal result number!");
893     return ValueList[ResNo];
894   }
895
896   typedef const MVT::ValueType* value_iterator;
897   value_iterator value_begin() const { return ValueList; }
898   value_iterator value_end() const { return ValueList+NumValues; }
899
900   /// getOperationName - Return the opcode of this operation for printing.
901   ///
902   std::string getOperationName(const SelectionDAG *G = 0) const;
903   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
904   void dump() const;
905   void dump(const SelectionDAG *G) const;
906
907   static bool classof(const SDNode *) { return true; }
908
909   /// Profile - Gather unique data for the node.
910   ///
911   void Profile(FoldingSetNodeID &ID);
912
913 protected:
914   friend class SelectionDAG;
915   
916   /// getValueTypeList - Return a pointer to the specified value type.
917   ///
918   static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
919   static SDVTList getSDVTList(MVT::ValueType VT) {
920     SDVTList Ret = { getValueTypeList(VT), 1 };
921     return Ret;
922   }
923
924   SDNode(unsigned Opc, SDVTList VTs, const SDOperand *Ops, unsigned NumOps)
925     : NodeType(Opc), NodeId(-1) {
926     OperandsNeedDelete = true;
927     NumOperands = NumOps;
928     OperandList = NumOps ? new SDOperand[NumOperands] : 0;
929     
930     for (unsigned i = 0; i != NumOps; ++i) {
931       OperandList[i] = Ops[i];
932       Ops[i].Val->Uses.push_back(this);
933     }
934     
935     ValueList = VTs.VTs;
936     NumValues = VTs.NumVTs;
937     Prev = 0; Next = 0;
938   }
939   SDNode(unsigned Opc, SDVTList VTs) : NodeType(Opc), NodeId(-1) {
940     OperandsNeedDelete = false;  // Operands set with InitOperands.
941     NumOperands = 0;
942     OperandList = 0;
943     
944     ValueList = VTs.VTs;
945     NumValues = VTs.NumVTs;
946     Prev = 0; Next = 0;
947   }
948   
949   /// InitOperands - Initialize the operands list of this node with the
950   /// specified values, which are part of the node (thus they don't need to be
951   /// copied in or allocated).
952   void InitOperands(SDOperand *Ops, unsigned NumOps) {
953     assert(OperandList == 0 && "Operands already set!");
954     NumOperands = NumOps;
955     OperandList = Ops;
956     
957     for (unsigned i = 0; i != NumOps; ++i)
958       Ops[i].Val->Uses.push_back(this);
959   }
960   
961   /// MorphNodeTo - This frees the operands of the current node, resets the
962   /// opcode, types, and operands to the specified value.  This should only be
963   /// used by the SelectionDAG class.
964   void MorphNodeTo(unsigned Opc, SDVTList L,
965                    const SDOperand *Ops, unsigned NumOps);
966   
967   void addUser(SDNode *User) {
968     Uses.push_back(User);
969   }
970   void removeUser(SDNode *User) {
971     // Remove this user from the operand's use list.
972     for (unsigned i = Uses.size(); ; --i) {
973       assert(i != 0 && "Didn't find user!");
974       if (Uses[i-1] == User) {
975         Uses[i-1] = Uses.back();
976         Uses.pop_back();
977         return;
978       }
979     }
980   }
981
982   void setNodeId(int Id) {
983     NodeId = Id;
984   }
985 };
986
987
988 // Define inline functions from the SDOperand class.
989
990 inline unsigned SDOperand::getOpcode() const {
991   return Val->getOpcode();
992 }
993 inline MVT::ValueType SDOperand::getValueType() const {
994   return Val->getValueType(ResNo);
995 }
996 inline unsigned SDOperand::getNumOperands() const {
997   return Val->getNumOperands();
998 }
999 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
1000   return Val->getOperand(i);
1001 }
1002 inline uint64_t SDOperand::getConstantOperandVal(unsigned i) const {
1003   return Val->getConstantOperandVal(i);
1004 }
1005 inline bool SDOperand::isTargetOpcode() const {
1006   return Val->isTargetOpcode();
1007 }
1008 inline unsigned SDOperand::getTargetOpcode() const {
1009   return Val->getTargetOpcode();
1010 }
1011 inline bool SDOperand::hasOneUse() const {
1012   return Val->hasNUsesOfValue(1, ResNo);
1013 }
1014
1015 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
1016 /// to allow co-allocation of node operands with the node itself.
1017 class UnarySDNode : public SDNode {
1018   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1019   SDOperand Op;
1020 public:
1021   UnarySDNode(unsigned Opc, SDVTList VTs, SDOperand X)
1022     : SDNode(Opc, VTs), Op(X) {
1023     InitOperands(&Op, 1);
1024   }
1025 };
1026
1027 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
1028 /// to allow co-allocation of node operands with the node itself.
1029 class BinarySDNode : public SDNode {
1030   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1031   SDOperand Ops[2];
1032 public:
1033   BinarySDNode(unsigned Opc, SDVTList VTs, SDOperand X, SDOperand Y)
1034     : SDNode(Opc, VTs) {
1035     Ops[0] = X;
1036     Ops[1] = Y;
1037     InitOperands(Ops, 2);
1038   }
1039 };
1040
1041 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
1042 /// to allow co-allocation of node operands with the node itself.
1043 class TernarySDNode : public SDNode {
1044   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1045   SDOperand Ops[3];
1046 public:
1047   TernarySDNode(unsigned Opc, SDVTList VTs, SDOperand X, SDOperand Y,
1048                 SDOperand Z)
1049     : SDNode(Opc, VTs) {
1050     Ops[0] = X;
1051     Ops[1] = Y;
1052     Ops[2] = Z;
1053     InitOperands(Ops, 3);
1054   }
1055 };
1056
1057
1058 /// HandleSDNode - This class is used to form a handle around another node that
1059 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1060 /// operand.  This node should be directly created by end-users and not added to
1061 /// the AllNodes list.
1062 class HandleSDNode : public SDNode {
1063   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1064   SDOperand Op;
1065 public:
1066   explicit HandleSDNode(SDOperand X)
1067     : SDNode(ISD::HANDLENODE, getSDVTList(MVT::Other)), Op(X) {
1068     InitOperands(&Op, 1);
1069   }
1070   ~HandleSDNode();  
1071   SDOperand getValue() const { return Op; }
1072 };
1073
1074 class StringSDNode : public SDNode {
1075   std::string Value;
1076   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1077 protected:
1078   friend class SelectionDAG;
1079   explicit StringSDNode(const std::string &val)
1080     : SDNode(ISD::STRING, getSDVTList(MVT::Other)), Value(val) {
1081   }
1082 public:
1083   const std::string &getValue() const { return Value; }
1084   static bool classof(const StringSDNode *) { return true; }
1085   static bool classof(const SDNode *N) {
1086     return N->getOpcode() == ISD::STRING;
1087   }
1088 };  
1089
1090 class ConstantSDNode : public SDNode {
1091   uint64_t Value;
1092   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1093 protected:
1094   friend class SelectionDAG;
1095   ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1096     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, getSDVTList(VT)),
1097       Value(val) {
1098   }
1099 public:
1100
1101   uint64_t getValue() const { return Value; }
1102
1103   int64_t getSignExtended() const {
1104     unsigned Bits = MVT::getSizeInBits(getValueType(0));
1105     return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1106   }
1107
1108   bool isNullValue() const { return Value == 0; }
1109   bool isAllOnesValue() const {
1110     return Value == MVT::getIntVTBitMask(getValueType(0));
1111   }
1112
1113   static bool classof(const ConstantSDNode *) { return true; }
1114   static bool classof(const SDNode *N) {
1115     return N->getOpcode() == ISD::Constant ||
1116            N->getOpcode() == ISD::TargetConstant;
1117   }
1118 };
1119
1120 class ConstantFPSDNode : public SDNode {
1121   double Value;
1122   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1123 protected:
1124   friend class SelectionDAG;
1125   ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1126     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1127              getSDVTList(VT)), Value(val) {
1128   }
1129 public:
1130
1131   double getValue() const { return Value; }
1132
1133   /// isExactlyValue - We don't rely on operator== working on double values, as
1134   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1135   /// As such, this method can be used to do an exact bit-for-bit comparison of
1136   /// two floating point values.
1137   bool isExactlyValue(double V) const;
1138
1139   static bool classof(const ConstantFPSDNode *) { return true; }
1140   static bool classof(const SDNode *N) {
1141     return N->getOpcode() == ISD::ConstantFP || 
1142            N->getOpcode() == ISD::TargetConstantFP;
1143   }
1144 };
1145
1146 class GlobalAddressSDNode : public SDNode {
1147   GlobalValue *TheGlobal;
1148   int Offset;
1149   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1150 protected:
1151   friend class SelectionDAG;
1152   GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1153                       int o = 0);
1154 public:
1155
1156   GlobalValue *getGlobal() const { return TheGlobal; }
1157   int getOffset() const { return Offset; }
1158
1159   static bool classof(const GlobalAddressSDNode *) { return true; }
1160   static bool classof(const SDNode *N) {
1161     return N->getOpcode() == ISD::GlobalAddress ||
1162            N->getOpcode() == ISD::TargetGlobalAddress ||
1163            N->getOpcode() == ISD::GlobalTLSAddress ||
1164            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1165   }
1166 };
1167
1168 class FrameIndexSDNode : public SDNode {
1169   int FI;
1170   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1171 protected:
1172   friend class SelectionDAG;
1173   FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1174     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, getSDVTList(VT)),
1175       FI(fi) {
1176   }
1177 public:
1178
1179   int getIndex() const { return FI; }
1180
1181   static bool classof(const FrameIndexSDNode *) { return true; }
1182   static bool classof(const SDNode *N) {
1183     return N->getOpcode() == ISD::FrameIndex ||
1184            N->getOpcode() == ISD::TargetFrameIndex;
1185   }
1186 };
1187
1188 class JumpTableSDNode : public SDNode {
1189   int JTI;
1190   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1191 protected:
1192   friend class SelectionDAG;
1193   JumpTableSDNode(int jti, MVT::ValueType VT, bool isTarg)
1194     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, getSDVTList(VT)),
1195       JTI(jti) {
1196   }
1197 public:
1198     
1199     int getIndex() const { return JTI; }
1200   
1201   static bool classof(const JumpTableSDNode *) { return true; }
1202   static bool classof(const SDNode *N) {
1203     return N->getOpcode() == ISD::JumpTable ||
1204            N->getOpcode() == ISD::TargetJumpTable;
1205   }
1206 };
1207
1208 class ConstantPoolSDNode : public SDNode {
1209   union {
1210     Constant *ConstVal;
1211     MachineConstantPoolValue *MachineCPVal;
1212   } Val;
1213   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1214   unsigned Alignment;
1215   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1216 protected:
1217   friend class SelectionDAG;
1218   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1219                      int o=0)
1220     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1221              getSDVTList(VT)), Offset(o), Alignment(0) {
1222     assert((int)Offset >= 0 && "Offset is too large");
1223     Val.ConstVal = c;
1224   }
1225   ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1226                      unsigned Align)
1227     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1228              getSDVTList(VT)), Offset(o), Alignment(Align) {
1229     assert((int)Offset >= 0 && "Offset is too large");
1230     Val.ConstVal = c;
1231   }
1232   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1233                      MVT::ValueType VT, int o=0)
1234     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 
1235              getSDVTList(VT)), Offset(o), Alignment(0) {
1236     assert((int)Offset >= 0 && "Offset is too large");
1237     Val.MachineCPVal = v;
1238     Offset |= 1 << (sizeof(unsigned)*8-1);
1239   }
1240   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1241                      MVT::ValueType VT, int o, unsigned Align)
1242     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1243              getSDVTList(VT)), Offset(o), Alignment(Align) {
1244     assert((int)Offset >= 0 && "Offset is too large");
1245     Val.MachineCPVal = v;
1246     Offset |= 1 << (sizeof(unsigned)*8-1);
1247   }
1248 public:
1249
1250   bool isMachineConstantPoolEntry() const {
1251     return (int)Offset < 0;
1252   }
1253
1254   Constant *getConstVal() const {
1255     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1256     return Val.ConstVal;
1257   }
1258
1259   MachineConstantPoolValue *getMachineCPVal() const {
1260     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1261     return Val.MachineCPVal;
1262   }
1263
1264   int getOffset() const {
1265     return Offset & ~(1 << (sizeof(unsigned)*8-1));
1266   }
1267   
1268   // Return the alignment of this constant pool object, which is either 0 (for
1269   // default alignment) or log2 of the desired value.
1270   unsigned getAlignment() const { return Alignment; }
1271
1272   const Type *getType() const;
1273
1274   static bool classof(const ConstantPoolSDNode *) { return true; }
1275   static bool classof(const SDNode *N) {
1276     return N->getOpcode() == ISD::ConstantPool ||
1277            N->getOpcode() == ISD::TargetConstantPool;
1278   }
1279 };
1280
1281 class BasicBlockSDNode : public SDNode {
1282   MachineBasicBlock *MBB;
1283   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1284 protected:
1285   friend class SelectionDAG;
1286   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1287     : SDNode(ISD::BasicBlock, getSDVTList(MVT::Other)), MBB(mbb) {
1288   }
1289 public:
1290
1291   MachineBasicBlock *getBasicBlock() const { return MBB; }
1292
1293   static bool classof(const BasicBlockSDNode *) { return true; }
1294   static bool classof(const SDNode *N) {
1295     return N->getOpcode() == ISD::BasicBlock;
1296   }
1297 };
1298
1299 class SrcValueSDNode : public SDNode {
1300   const Value *V;
1301   int offset;
1302   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1303 protected:
1304   friend class SelectionDAG;
1305   SrcValueSDNode(const Value* v, int o)
1306     : SDNode(ISD::SRCVALUE, getSDVTList(MVT::Other)), V(v), offset(o) {
1307   }
1308
1309 public:
1310   const Value *getValue() const { return V; }
1311   int getOffset() const { return offset; }
1312
1313   static bool classof(const SrcValueSDNode *) { return true; }
1314   static bool classof(const SDNode *N) {
1315     return N->getOpcode() == ISD::SRCVALUE;
1316   }
1317 };
1318
1319
1320 class RegisterSDNode : public SDNode {
1321   unsigned Reg;
1322   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1323 protected:
1324   friend class SelectionDAG;
1325   RegisterSDNode(unsigned reg, MVT::ValueType VT)
1326     : SDNode(ISD::Register, getSDVTList(VT)), Reg(reg) {
1327   }
1328 public:
1329
1330   unsigned getReg() const { return Reg; }
1331
1332   static bool classof(const RegisterSDNode *) { return true; }
1333   static bool classof(const SDNode *N) {
1334     return N->getOpcode() == ISD::Register;
1335   }
1336 };
1337
1338 class ExternalSymbolSDNode : public SDNode {
1339   const char *Symbol;
1340   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1341 protected:
1342   friend class SelectionDAG;
1343   ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1344     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1345              getSDVTList(VT)), Symbol(Sym) {
1346   }
1347 public:
1348
1349   const char *getSymbol() const { return Symbol; }
1350
1351   static bool classof(const ExternalSymbolSDNode *) { return true; }
1352   static bool classof(const SDNode *N) {
1353     return N->getOpcode() == ISD::ExternalSymbol ||
1354            N->getOpcode() == ISD::TargetExternalSymbol;
1355   }
1356 };
1357
1358 class CondCodeSDNode : public SDNode {
1359   ISD::CondCode Condition;
1360   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1361 protected:
1362   friend class SelectionDAG;
1363   explicit CondCodeSDNode(ISD::CondCode Cond)
1364     : SDNode(ISD::CONDCODE, getSDVTList(MVT::Other)), Condition(Cond) {
1365   }
1366 public:
1367
1368   ISD::CondCode get() const { return Condition; }
1369
1370   static bool classof(const CondCodeSDNode *) { return true; }
1371   static bool classof(const SDNode *N) {
1372     return N->getOpcode() == ISD::CONDCODE;
1373   }
1374 };
1375
1376 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1377 /// to parameterize some operations.
1378 class VTSDNode : public SDNode {
1379   MVT::ValueType ValueType;
1380   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1381 protected:
1382   friend class SelectionDAG;
1383   explicit VTSDNode(MVT::ValueType VT)
1384     : SDNode(ISD::VALUETYPE, getSDVTList(MVT::Other)), ValueType(VT) {
1385   }
1386 public:
1387
1388   MVT::ValueType getVT() const { return ValueType; }
1389
1390   static bool classof(const VTSDNode *) { return true; }
1391   static bool classof(const SDNode *N) {
1392     return N->getOpcode() == ISD::VALUETYPE;
1393   }
1394 };
1395
1396 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1397 ///
1398 class LoadSDNode : public SDNode {
1399   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1400   SDOperand Ops[3];
1401   
1402   // AddrMode - unindexed, pre-indexed, post-indexed.
1403   ISD::MemIndexedMode AddrMode;
1404
1405   // ExtType - non-ext, anyext, sext, zext.
1406   ISD::LoadExtType ExtType;
1407
1408   // LoadedVT - VT of loaded value before extension.
1409   MVT::ValueType LoadedVT;
1410
1411   // SrcValue - Memory location for alias analysis.
1412   const Value *SrcValue;
1413
1414   // SVOffset - Memory location offset.
1415   int SVOffset;
1416
1417   // Alignment - Alignment of memory location in bytes.
1418   unsigned Alignment;
1419
1420   // IsVolatile - True if the load is volatile.
1421   bool IsVolatile;
1422 protected:
1423   friend class SelectionDAG;
1424   LoadSDNode(SDOperand *ChainPtrOff, SDVTList VTs,
1425              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, MVT::ValueType LVT,
1426              const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
1427     : SDNode(ISD::LOAD, VTs),
1428       AddrMode(AM), ExtType(ETy), LoadedVT(LVT), SrcValue(SV), SVOffset(O),
1429       Alignment(Align), IsVolatile(Vol) {
1430     Ops[0] = ChainPtrOff[0]; // Chain
1431     Ops[1] = ChainPtrOff[1]; // Ptr
1432     Ops[2] = ChainPtrOff[2]; // Off
1433     InitOperands(Ops, 3);
1434     assert(Align != 0 && "Loads should have non-zero aligment");
1435     assert((getOffset().getOpcode() == ISD::UNDEF ||
1436             AddrMode != ISD::UNINDEXED) &&
1437            "Only indexed load has a non-undef offset operand");
1438   }
1439 public:
1440
1441   const SDOperand getChain() const { return getOperand(0); }
1442   const SDOperand getBasePtr() const { return getOperand(1); }
1443   const SDOperand getOffset() const { return getOperand(2); }
1444   ISD::MemIndexedMode getAddressingMode() const { return AddrMode; }
1445   ISD::LoadExtType getExtensionType() const { return ExtType; }
1446   MVT::ValueType getLoadedVT() const { return LoadedVT; }
1447   const Value *getSrcValue() const { return SrcValue; }
1448   int getSrcValueOffset() const { return SVOffset; }
1449   unsigned getAlignment() const { return Alignment; }
1450   bool isVolatile() const { return IsVolatile; }
1451
1452   static bool classof(const LoadSDNode *) { return true; }
1453   static bool classof(const SDNode *N) {
1454     return N->getOpcode() == ISD::LOAD;
1455   }
1456 };
1457
1458 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1459 ///
1460 class StoreSDNode : public SDNode {
1461   virtual void ANCHOR();  // Out-of-line virtual method to give class a home.
1462   SDOperand Ops[4];
1463     
1464   // AddrMode - unindexed, pre-indexed, post-indexed.
1465   ISD::MemIndexedMode AddrMode;
1466
1467   // IsTruncStore - True is the op does a truncation before store.
1468   bool IsTruncStore;
1469
1470   // StoredVT - VT of the value after truncation.
1471   MVT::ValueType StoredVT;
1472
1473   // SrcValue - Memory location for alias analysis.
1474   const Value *SrcValue;
1475
1476   // SVOffset - Memory location offset.
1477   int SVOffset;
1478
1479   // Alignment - Alignment of memory location in bytes.
1480   unsigned Alignment;
1481
1482   // IsVolatile - True if the store is volatile.
1483   bool IsVolatile;
1484 protected:
1485   friend class SelectionDAG;
1486   StoreSDNode(SDOperand *ChainValuePtrOff, SDVTList VTs,
1487               ISD::MemIndexedMode AM, bool isTrunc, MVT::ValueType SVT,
1488               const Value *SV, int O=0, unsigned Align=0, bool Vol=false)
1489     : SDNode(ISD::STORE, VTs),
1490       AddrMode(AM), IsTruncStore(isTrunc), StoredVT(SVT), SrcValue(SV),
1491       SVOffset(O), Alignment(Align), IsVolatile(Vol) {
1492     Ops[0] = ChainValuePtrOff[0]; // Chain
1493     Ops[1] = ChainValuePtrOff[1]; // Value
1494     Ops[2] = ChainValuePtrOff[2]; // Ptr
1495     Ops[3] = ChainValuePtrOff[3]; // Off
1496     InitOperands(Ops, 4);
1497     assert(Align != 0 && "Stores should have non-zero aligment");
1498     assert((getOffset().getOpcode() == ISD::UNDEF || 
1499             AddrMode != ISD::UNINDEXED) &&
1500            "Only indexed store has a non-undef offset operand");
1501   }
1502 public:
1503
1504   const SDOperand getChain() const { return getOperand(0); }
1505   const SDOperand getValue() const { return getOperand(1); }
1506   const SDOperand getBasePtr() const { return getOperand(2); }
1507   const SDOperand getOffset() const { return getOperand(3); }
1508   ISD::MemIndexedMode getAddressingMode() const { return AddrMode; }
1509   bool isTruncatingStore() const { return IsTruncStore; }
1510   MVT::ValueType getStoredVT() const { return StoredVT; }
1511   const Value *getSrcValue() const { return SrcValue; }
1512   int getSrcValueOffset() const { return SVOffset; }
1513   unsigned getAlignment() const { return Alignment; }
1514   bool isVolatile() const { return IsVolatile; }
1515
1516   static bool classof(const StoreSDNode *) { return true; }
1517   static bool classof(const SDNode *N) {
1518     return N->getOpcode() == ISD::STORE;
1519   }
1520 };
1521
1522
1523 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1524   SDNode *Node;
1525   unsigned Operand;
1526
1527   SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1528 public:
1529   bool operator==(const SDNodeIterator& x) const {
1530     return Operand == x.Operand;
1531   }
1532   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1533
1534   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1535     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1536     Operand = I.Operand;
1537     return *this;
1538   }
1539
1540   pointer operator*() const {
1541     return Node->getOperand(Operand).Val;
1542   }
1543   pointer operator->() const { return operator*(); }
1544
1545   SDNodeIterator& operator++() {                // Preincrement
1546     ++Operand;
1547     return *this;
1548   }
1549   SDNodeIterator operator++(int) { // Postincrement
1550     SDNodeIterator tmp = *this; ++*this; return tmp;
1551   }
1552
1553   static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1554   static SDNodeIterator end  (SDNode *N) {
1555     return SDNodeIterator(N, N->getNumOperands());
1556   }
1557
1558   unsigned getOperand() const { return Operand; }
1559   const SDNode *getNode() const { return Node; }
1560 };
1561
1562 template <> struct GraphTraits<SDNode*> {
1563   typedef SDNode NodeType;
1564   typedef SDNodeIterator ChildIteratorType;
1565   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1566   static inline ChildIteratorType child_begin(NodeType *N) {
1567     return SDNodeIterator::begin(N);
1568   }
1569   static inline ChildIteratorType child_end(NodeType *N) {
1570     return SDNodeIterator::end(N);
1571   }
1572 };
1573
1574 template<>
1575 struct ilist_traits<SDNode> {
1576   static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1577   static SDNode *getNext(const SDNode *N) { return N->Next; }
1578   
1579   static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1580   static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1581   
1582   static SDNode *createSentinel() {
1583     return new SDNode(ISD::EntryToken, SDNode::getSDVTList(MVT::Other));
1584   }
1585   static void destroySentinel(SDNode *N) { delete N; }
1586   //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1587   
1588   
1589   void addNodeToList(SDNode *NTy) {}
1590   void removeNodeFromList(SDNode *NTy) {}
1591   void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1592                              const ilist_iterator<SDNode> &X,
1593                              const ilist_iterator<SDNode> &Y) {}
1594 };
1595
1596 namespace ISD {
1597   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1598   /// load.
1599   inline bool isNON_EXTLoad(const SDNode *N) {
1600     return N->getOpcode() == ISD::LOAD &&
1601       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1602   }
1603
1604   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1605   ///
1606   inline bool isEXTLoad(const SDNode *N) {
1607     return N->getOpcode() == ISD::LOAD &&
1608       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1609   }
1610
1611   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1612   ///
1613   inline bool isSEXTLoad(const SDNode *N) {
1614     return N->getOpcode() == ISD::LOAD &&
1615       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1616   }
1617
1618   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1619   ///
1620   inline bool isZEXTLoad(const SDNode *N) {
1621     return N->getOpcode() == ISD::LOAD &&
1622       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1623   }
1624
1625   /// isUNINDEXEDLoad - Returns true if the specified node is a unindexed load.
1626   ///
1627   inline bool isUNINDEXEDLoad(const SDNode *N) {
1628     return N->getOpcode() == ISD::LOAD &&
1629       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1630   }
1631
1632   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1633   /// store.
1634   inline bool isNON_TRUNCStore(const SDNode *N) {
1635     return N->getOpcode() == ISD::STORE &&
1636       !cast<StoreSDNode>(N)->isTruncatingStore();
1637   }
1638
1639   /// isTRUNCStore - Returns true if the specified node is a truncating
1640   /// store.
1641   inline bool isTRUNCStore(const SDNode *N) {
1642     return N->getOpcode() == ISD::STORE &&
1643       cast<StoreSDNode>(N)->isTruncatingStore();
1644   }
1645 }
1646
1647
1648 } // end llvm namespace
1649
1650 #endif