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