SelectionDAGNodes.h revision 49c6d3eba8869cf945f6c55ea243466e2b406fe8
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/CodeGen/ValueTypes.h"
23#include "llvm/Value.h"
24#include "llvm/ADT/GraphTraits.h"
25#include "llvm/ADT/iterator"
26#include "llvm/Support/DataTypes.h"
27#include <cassert>
28#include <vector>
29
30namespace llvm {
31
32class SelectionDAG;
33class GlobalValue;
34class MachineBasicBlock;
35class SDNode;
36template <typename T> struct simplify_type;
37template <typename T> struct ilist_traits;
38template<typename NodeTy, typename Traits> class iplist;
39template<typename NodeTy> class ilist_iterator;
40
41/// ISD namespace - This namespace contains an enum which represents all of the
42/// SelectionDAG node types and value types.
43///
44namespace ISD {
45  //===--------------------------------------------------------------------===//
46  /// ISD::NodeType enum - This enum defines all of the operators valid in a
47  /// SelectionDAG.
48  ///
49  enum NodeType {
50    // EntryToken - This is the marker used to indicate the start of the region.
51    EntryToken,
52
53    // Token factor - This node takes multiple tokens as input and produces a
54    // single token result.  This is used to represent the fact that the operand
55    // operators are independent of each other.
56    TokenFactor,
57
58    // AssertSext, AssertZext - These nodes record if a register contains a
59    // value that has already been zero or sign extended from a narrower type.
60    // These nodes take two operands.  The first is the node that has already
61    // been extended, and the second is a value type node indicating the width
62    // of the extension
63    AssertSext, AssertZext,
64
65    // Various leaf nodes.
66    STRING, BasicBlock, VALUETYPE, CONDCODE, Register,
67    Constant, ConstantFP,
68    GlobalAddress, FrameIndex, ConstantPool, ExternalSymbol,
69
70    // TargetConstant* - Like Constant*, but the DAG does not do any folding or
71    // simplification of the constant.
72    TargetConstant,
73    TargetConstantFP,
74
75    // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
76    // anything else with this node, and this is valid in the target-specific
77    // dag, turning into a GlobalAddress operand.
78    TargetGlobalAddress,
79    TargetFrameIndex,
80    TargetConstantPool,
81    TargetExternalSymbol,
82
83    // CopyToReg - This node has three operands: a chain, a register number to
84    // set to this value, and a value.
85    CopyToReg,
86
87    // CopyFromReg - This node indicates that the input value is a virtual or
88    // physical register that is defined outside of the scope of this
89    // SelectionDAG.  The register is available from the RegSDNode object.
90    CopyFromReg,
91
92    // UNDEF - An undefined node
93    UNDEF,
94
95    // EXTRACT_ELEMENT - This is used to get the first or second (determined by
96    // a Constant, which is required to be operand #1), element of the aggregate
97    // value specified as operand #0.  This is only for use before legalization,
98    // for values that will be broken into multiple registers.
99    EXTRACT_ELEMENT,
100
101    // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
102    // two values of the same integer value type, this produces a value twice as
103    // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
104    BUILD_PAIR,
105
106    // MERGE_VALUES - This node takes multiple discrete operands and returns
107    // them all as its individual results.  This nodes has exactly the same
108    // number of inputs and outputs, and is only valid before legalization.
109    // This node is useful for some pieces of the code generator that want to
110    // think about a single node with multiple results, not multiple nodes.
111    MERGE_VALUES,
112
113    // Simple integer binary arithmetic operators.
114    ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
115
116    // Carry-setting nodes for multiple precision addition and subtraction.
117    // These nodes take two operands of the same value type, and produce two
118    // results.  The first result is the normal add or sub result, the second
119    // result is the carry flag result.
120    ADDC, SUBC,
121
122    // Carry-using nodes for multiple precision addition and subtraction.  These
123    // nodes take three operands: The first two are the normal lhs and rhs to
124    // the add or sub, and the third is the input carry flag.  These nodes
125    // produce two results; the normal result of the add or sub, and the output
126    // carry flag.  These nodes both read and write a carry flag to allow them
127    // to them to be chained together for add and sub of arbitrarily large
128    // values.
129    ADDE, SUBE,
130
131    // Simple binary floating point operators.
132    FADD, FSUB, FMUL, FDIV, FREM,
133
134    // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.  NOTE: This
135    // DAG node does not require that X and Y have the same type, just that they
136    // are both floating point.  X and the result must have the same type.
137    // FCOPYSIGN(f32, f64) is allowed.
138    FCOPYSIGN,
139
140    /// VBUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...,  COUNT,TYPE) - Return a vector
141    /// with the specified, possibly variable, elements.  The number of elements
142    /// is required to be a power of two.
143    VBUILD_VECTOR,
144
145    /// BUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...) - Return a vector
146    /// with the specified, possibly variable, elements.  The number of elements
147    /// is required to be a power of two.
148    BUILD_VECTOR,
149
150    /// VINSERT_VECTOR_ELT(VECTOR, VAL, IDX,  COUNT,TYPE) - Given a vector
151    /// VECTOR, an element ELEMENT, and a (potentially variable) index IDX,
152    /// return an vector with the specified element of VECTOR replaced with VAL.
153    /// COUNT and TYPE specify the type of vector, as is standard for V* nodes.
154    VINSERT_VECTOR_ELT,
155
156    /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR (a legal packed
157    /// type) with the element at IDX replaced with VAL.
158    INSERT_VECTOR_ELT,
159
160    /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
161    /// type as VEC1/VEC2.  SHUFFLEVEC is a BUILD_VECTOR of constant int values
162    /// (regardless of whether its datatype is legal or not) that indicate
163    /// which value each result element will get.  The elements of VEC1/VEC2 are
164    /// enumerated in order.  This is quite similar to the Altivec 'vperm'
165    /// instruction, except that the indices must be constants and are in terms
166    /// of the element size of VEC1/VEC2, not in terms of bytes.
167    VECTOR_SHUFFLE,
168
169    /// BINOP(LHS, RHS,  COUNT,TYPE)
170    /// Simple abstract vector operators.  Unlike the integer and floating point
171    /// binary operators, these nodes also take two additional operands:
172    /// a constant element count, and a value type node indicating the type of
173    /// the elements.  The order is count, type, op0, op1.  All vector opcodes,
174    /// including VLOAD and VConstant must currently have count and type as
175    /// their last two operands.
176    VADD, VSUB, VMUL, VSDIV, VUDIV,
177    VAND, VOR, VXOR,
178
179    /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
180    /// scalar value into the low element of the resultant vector type.  The top
181    /// elements of the vector are undefined.
182    SCALAR_TO_VECTOR,
183
184    // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
185    // an unsigned/signed value of type i[2*n], then return the top part.
186    MULHU, MULHS,
187
188    // Bitwise operators - logical and, logical or, logical xor, shift left,
189    // shift right algebraic (shift in sign bits), shift right logical (shift in
190    // zeroes), rotate left, rotate right, and byteswap.
191    AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
192
193    // Counting operators
194    CTTZ, CTLZ, CTPOP,
195
196    // Select(COND, TRUEVAL, FALSEVAL)
197    SELECT,
198
199    // Select with condition operator - This selects between a true value and
200    // a false value (ops #2 and #3) based on the boolean result of comparing
201    // the lhs and rhs (ops #0 and #1) of a conditional expression with the
202    // condition code in op #4, a CondCodeSDNode.
203    SELECT_CC,
204
205    // SetCC operator - This evaluates to a boolean (i1) true value if the
206    // condition is true.  The operands to this are the left and right operands
207    // to compare (ops #0, and #1) and the condition code to compare them with
208    // (op #2) as a CondCodeSDNode.
209    SETCC,
210
211    // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
212    // integer shift operations, just like ADD/SUB_PARTS.  The operation
213    // ordering is:
214    //       [Lo,Hi] = op [LoLHS,HiLHS], Amt
215    SHL_PARTS, SRA_PARTS, SRL_PARTS,
216
217    // Conversion operators.  These are all single input single output
218    // operations.  For all of these, the result type must be strictly
219    // wider or narrower (depending on the operation) than the source
220    // type.
221
222    // SIGN_EXTEND - Used for integer types, replicating the sign bit
223    // into new bits.
224    SIGN_EXTEND,
225
226    // ZERO_EXTEND - Used for integer types, zeroing the new bits.
227    ZERO_EXTEND,
228
229    // ANY_EXTEND - Used for integer types.  The high bits are undefined.
230    ANY_EXTEND,
231
232    // TRUNCATE - Completely drop the high bits.
233    TRUNCATE,
234
235    // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
236    // depends on the first letter) to floating point.
237    SINT_TO_FP,
238    UINT_TO_FP,
239
240    // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
241    // sign extend a small value in a large integer register (e.g. sign
242    // extending the low 8 bits of a 32-bit register to fill the top 24 bits
243    // with the 7th bit).  The size of the smaller type is indicated by the 1th
244    // operand, a ValueType node.
245    SIGN_EXTEND_INREG,
246
247    // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
248    // integer.
249    FP_TO_SINT,
250    FP_TO_UINT,
251
252    // FP_ROUND - Perform a rounding operation from the current
253    // precision down to the specified precision (currently always 64->32).
254    FP_ROUND,
255
256    // FP_ROUND_INREG - This operator takes a floating point register, and
257    // rounds it to a floating point value.  It then promotes it and returns it
258    // in a register of the same size.  This operation effectively just discards
259    // excess precision.  The type to round down to is specified by the 1th
260    // operation, a VTSDNode (currently always 64->32->64).
261    FP_ROUND_INREG,
262
263    // FP_EXTEND - Extend a smaller FP type into a larger FP type.
264    FP_EXTEND,
265
266    // BIT_CONVERT - Theis operator converts between integer and FP values, as
267    // if one was stored to memory as integer and the other was loaded from the
268    // same address (or equivalently for vector format conversions, etc).  The
269    // source and result are required to have the same bit size (e.g.
270    // f32 <-> i32).  This can also be used for int-to-int or fp-to-fp
271    // conversions, but that is a noop, deleted by getNode().
272    BIT_CONVERT,
273
274    // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
275    // absolute value, square root, sine and cosine operations.
276    FNEG, FABS, FSQRT, FSIN, FCOS,
277
278    // Other operators.  LOAD and STORE have token chains as their first
279    // operand, then the same operands as an LLVM load/store instruction, then a
280    // SRCVALUE node that provides alias analysis information.
281    LOAD, STORE,
282
283    // Abstract vector version of LOAD.  VLOAD has a constant element count as
284    // the first operand, followed by a value type node indicating the type of
285    // the elements, a token chain, a pointer operand, and a SRCVALUE node.
286    VLOAD,
287
288    // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
289    // memory and extend them to a larger value (e.g. load a byte into a word
290    // register).  All three of these have four operands, a token chain, a
291    // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
292    // indicating the type to load.
293    //
294    // SEXTLOAD loads the integer operand and sign extends it to a larger
295    //          integer result type.
296    // ZEXTLOAD loads the integer operand and zero extends it to a larger
297    //          integer result type.
298    // EXTLOAD  is used for three things: floating point extending loads,
299    //          integer extending loads [the top bits are undefined], and vector
300    //          extending loads [load into low elt].
301    EXTLOAD, SEXTLOAD, ZEXTLOAD,
302
303    // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
304    // value and stores it to memory in one operation.  This can be used for
305    // either integer or floating point operands.  The first four operands of
306    // this are the same as a standard store.  The fifth is the ValueType to
307    // store it as (which will be smaller than the source value).
308    TRUNCSTORE,
309
310    // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
311    // to a specified boundary.  The first operand is the token chain, the
312    // second is the number of bytes to allocate, and the third is the alignment
313    // boundary.  The size is guaranteed to be a multiple of the stack
314    // alignment, and the alignment is guaranteed to be bigger than the stack
315    // alignment (if required) or 0 to get standard stack alignment.
316    DYNAMIC_STACKALLOC,
317
318    // Control flow instructions.  These all have token chains.
319
320    // BR - Unconditional branch.  The first operand is the chain
321    // operand, the second is the MBB to branch to.
322    BR,
323
324    // BRCOND - Conditional branch.  The first operand is the chain,
325    // the second is the condition, the third is the block to branch
326    // to if the condition is true.
327    BRCOND,
328
329    // BR_CC - Conditional branch.  The behavior is like that of SELECT_CC, in
330    // that the condition is represented as condition code, and two nodes to
331    // compare, rather than as a combined SetCC node.  The operands in order are
332    // chain, cc, lhs, rhs, block to branch to if condition is true.
333    BR_CC,
334
335    // RET - Return from function.  The first operand is the chain,
336    // and any subsequent operands are the return values for the
337    // function.  This operation can have variable number of operands.
338    RET,
339
340    // INLINEASM - Represents an inline asm block.  This node always has two
341    // return values: a chain and a flag result.  The inputs are as follows:
342    //   Operand #0   : Input chain.
343    //   Operand #1   : a ExternalSymbolSDNode with a pointer to the asm string.
344    //   Operand #2n+2: A RegisterNode.
345    //   Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
346    //   Operand #last: Optional, an incoming flag.
347    INLINEASM,
348
349    // STACKSAVE - STACKSAVE has one operand, an input chain.  It produces a
350    // value, the same type as the pointer type for the system, and an output
351    // chain.
352    STACKSAVE,
353
354    // STACKRESTORE has two operands, an input chain and a pointer to restore to
355    // it returns an output chain.
356    STACKRESTORE,
357
358    // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
359    // correspond to the operands of the LLVM intrinsic functions.  The only
360    // result is a token chain.  The alignment argument is guaranteed to be a
361    // Constant node.
362    MEMSET,
363    MEMMOVE,
364    MEMCPY,
365
366    // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
367    // a call sequence, and carry arbitrary information that target might want
368    // to know.  The first operand is a chain, the rest are specified by the
369    // target and not touched by the DAG optimizers.
370    CALLSEQ_START,  // Beginning of a call sequence
371    CALLSEQ_END,    // End of a call sequence
372
373    // VAARG - VAARG has three operands: an input chain, a pointer, and a
374    // SRCVALUE.  It returns a pair of values: the vaarg value and a new chain.
375    VAARG,
376
377    // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
378    // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
379    // source.
380    VACOPY,
381
382    // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
383    // pointer, and a SRCVALUE.
384    VAEND, VASTART,
385
386    // SRCVALUE - This corresponds to a Value*, and is used to associate memory
387    // locations with their value.  This allows one use alias analysis
388    // information in the backend.
389    SRCVALUE,
390
391    // PCMARKER - This corresponds to the pcmarker intrinsic.
392    PCMARKER,
393
394    // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
395    // The only operand is a chain and a value and a chain are produced.  The
396    // value is the contents of the architecture specific cycle counter like
397    // register (or other high accuracy low latency clock source)
398    READCYCLECOUNTER,
399
400    // HANDLENODE node - Used as a handle for various purposes.
401    HANDLENODE,
402
403    // LOCATION - This node is used to represent a source location for debug
404    // info.  It takes token chain as input, then a line number, then a column
405    // number, then a filename, then a working dir.  It produces a token chain
406    // as output.
407    LOCATION,
408
409    // DEBUG_LOC - This node is used to represent source line information
410    // embedded in the code.  It takes a token chain as input, then a line
411    // number, then a column then a file id (provided by MachineDebugInfo.) It
412    // produces a token chain as output.
413    DEBUG_LOC,
414
415    // DEBUG_LABEL - This node is used to mark a location in the code where a
416    // label should be generated for use by the debug information.  It takes a
417    // token chain as input and then a unique id (provided by MachineDebugInfo.)
418    // It produces a token chain as output.
419    DEBUG_LABEL,
420
421    // BUILTIN_OP_END - This must be the last enum value in this list.
422    BUILTIN_OP_END
423  };
424
425  //===--------------------------------------------------------------------===//
426  /// ISD::CondCode enum - These are ordered carefully to make the bitfields
427  /// below work out, when considering SETFALSE (something that never exists
428  /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
429  /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
430  /// to.  If the "N" column is 1, the result of the comparison is undefined if
431  /// the input is a NAN.
432  ///
433  /// All of these (except for the 'always folded ops') should be handled for
434  /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
435  /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
436  ///
437  /// Note that these are laid out in a specific order to allow bit-twiddling
438  /// to transform conditions.
439  enum CondCode {
440    // Opcode          N U L G E       Intuitive operation
441    SETFALSE,      //    0 0 0 0       Always false (always folded)
442    SETOEQ,        //    0 0 0 1       True if ordered and equal
443    SETOGT,        //    0 0 1 0       True if ordered and greater than
444    SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
445    SETOLT,        //    0 1 0 0       True if ordered and less than
446    SETOLE,        //    0 1 0 1       True if ordered and less than or equal
447    SETONE,        //    0 1 1 0       True if ordered and operands are unequal
448    SETO,          //    0 1 1 1       True if ordered (no nans)
449    SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
450    SETUEQ,        //    1 0 0 1       True if unordered or equal
451    SETUGT,        //    1 0 1 0       True if unordered or greater than
452    SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
453    SETULT,        //    1 1 0 0       True if unordered or less than
454    SETULE,        //    1 1 0 1       True if unordered, less than, or equal
455    SETUNE,        //    1 1 1 0       True if unordered or not equal
456    SETTRUE,       //    1 1 1 1       Always true (always folded)
457    // Don't care operations: undefined if the input is a nan.
458    SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
459    SETEQ,         //  1 X 0 0 1       True if equal
460    SETGT,         //  1 X 0 1 0       True if greater than
461    SETGE,         //  1 X 0 1 1       True if greater than or equal
462    SETLT,         //  1 X 1 0 0       True if less than
463    SETLE,         //  1 X 1 0 1       True if less than or equal
464    SETNE,         //  1 X 1 1 0       True if not equal
465    SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
466
467    SETCC_INVALID       // Marker value.
468  };
469
470  /// isSignedIntSetCC - Return true if this is a setcc instruction that
471  /// performs a signed comparison when used with integer operands.
472  inline bool isSignedIntSetCC(CondCode Code) {
473    return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
474  }
475
476  /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
477  /// performs an unsigned comparison when used with integer operands.
478  inline bool isUnsignedIntSetCC(CondCode Code) {
479    return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
480  }
481
482  /// isTrueWhenEqual - Return true if the specified condition returns true if
483  /// the two operands to the condition are equal.  Note that if one of the two
484  /// operands is a NaN, this value is meaningless.
485  inline bool isTrueWhenEqual(CondCode Cond) {
486    return ((int)Cond & 1) != 0;
487  }
488
489  /// getUnorderedFlavor - This function returns 0 if the condition is always
490  /// false if an operand is a NaN, 1 if the condition is always true if the
491  /// operand is a NaN, and 2 if the condition is undefined if the operand is a
492  /// NaN.
493  inline unsigned getUnorderedFlavor(CondCode Cond) {
494    return ((int)Cond >> 3) & 3;
495  }
496
497  /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
498  /// 'op' is a valid SetCC operation.
499  CondCode getSetCCInverse(CondCode Operation, bool isInteger);
500
501  /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
502  /// when given the operation for (X op Y).
503  CondCode getSetCCSwappedOperands(CondCode Operation);
504
505  /// getSetCCOrOperation - Return the result of a logical OR between different
506  /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
507  /// function returns SETCC_INVALID if it is not possible to represent the
508  /// resultant comparison.
509  CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
510
511  /// getSetCCAndOperation - Return the result of a logical AND between
512  /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
513  /// function returns SETCC_INVALID if it is not possible to represent the
514  /// resultant comparison.
515  CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
516}  // end llvm::ISD namespace
517
518
519//===----------------------------------------------------------------------===//
520/// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
521/// values as the result of a computation.  Many nodes return multiple values,
522/// from loads (which define a token and a return value) to ADDC (which returns
523/// a result and a carry value), to calls (which may return an arbitrary number
524/// of values).
525///
526/// As such, each use of a SelectionDAG computation must indicate the node that
527/// computes it as well as which return value to use from that node.  This pair
528/// of information is represented with the SDOperand value type.
529///
530class SDOperand {
531public:
532  SDNode *Val;        // The node defining the value we are using.
533  unsigned ResNo;     // Which return value of the node we are using.
534
535  SDOperand() : Val(0) {}
536  SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
537
538  bool operator==(const SDOperand &O) const {
539    return Val == O.Val && ResNo == O.ResNo;
540  }
541  bool operator!=(const SDOperand &O) const {
542    return !operator==(O);
543  }
544  bool operator<(const SDOperand &O) const {
545    return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
546  }
547
548  SDOperand getValue(unsigned R) const {
549    return SDOperand(Val, R);
550  }
551
552  // isOperand - Return true if this node is an operand of N.
553  bool isOperand(SDNode *N) const;
554
555  /// getValueType - Return the ValueType of the referenced return value.
556  ///
557  inline MVT::ValueType getValueType() const;
558
559  // Forwarding methods - These forward to the corresponding methods in SDNode.
560  inline unsigned getOpcode() const;
561  inline unsigned getNodeDepth() const;
562  inline unsigned getNumOperands() const;
563  inline const SDOperand &getOperand(unsigned i) const;
564  inline bool isTargetOpcode() const;
565  inline unsigned getTargetOpcode() const;
566
567  /// hasOneUse - Return true if there is exactly one operation using this
568  /// result value of the defining operator.
569  inline bool hasOneUse() const;
570};
571
572
573/// simplify_type specializations - Allow casting operators to work directly on
574/// SDOperands as if they were SDNode*'s.
575template<> struct simplify_type<SDOperand> {
576  typedef SDNode* SimpleType;
577  static SimpleType getSimplifiedValue(const SDOperand &Val) {
578    return static_cast<SimpleType>(Val.Val);
579  }
580};
581template<> struct simplify_type<const SDOperand> {
582  typedef SDNode* SimpleType;
583  static SimpleType getSimplifiedValue(const SDOperand &Val) {
584    return static_cast<SimpleType>(Val.Val);
585  }
586};
587
588
589/// SDNode - Represents one node in the SelectionDAG.
590///
591class SDNode {
592  /// NodeType - The operation that this node performs.
593  ///
594  unsigned short NodeType;
595
596  /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1.  This
597  /// means that leaves have a depth of 1, things that use only leaves have a
598  /// depth of 2, etc.
599  unsigned short NodeDepth;
600
601  /// OperandList - The values that are used by this operation.
602  ///
603  SDOperand *OperandList;
604
605  /// ValueList - The types of the values this node defines.  SDNode's may
606  /// define multiple values simultaneously.
607  MVT::ValueType *ValueList;
608
609  /// NumOperands/NumValues - The number of entries in the Operand/Value list.
610  unsigned short NumOperands, NumValues;
611
612  /// Prev/Next pointers - These pointers form the linked list of of the
613  /// AllNodes list in the current DAG.
614  SDNode *Prev, *Next;
615  friend struct ilist_traits<SDNode>;
616
617  /// Uses - These are all of the SDNode's that use a value produced by this
618  /// node.
619  std::vector<SDNode*> Uses;
620public:
621  virtual ~SDNode() {
622    assert(NumOperands == 0 && "Operand list not cleared before deletion");
623  }
624
625  //===--------------------------------------------------------------------===//
626  //  Accessors
627  //
628  unsigned getOpcode()  const { return NodeType; }
629  bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
630  unsigned getTargetOpcode() const {
631    assert(isTargetOpcode() && "Not a target opcode!");
632    return NodeType - ISD::BUILTIN_OP_END;
633  }
634
635  size_t use_size() const { return Uses.size(); }
636  bool use_empty() const { return Uses.empty(); }
637  bool hasOneUse() const { return Uses.size() == 1; }
638
639  /// getNodeDepth - Return the distance from this node to the leaves in the
640  /// graph.  The leaves have a depth of 1.
641  unsigned getNodeDepth() const { return NodeDepth; }
642
643  typedef std::vector<SDNode*>::const_iterator use_iterator;
644  use_iterator use_begin() const { return Uses.begin(); }
645  use_iterator use_end() const { return Uses.end(); }
646
647  /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
648  /// indicated value.  This method ignores uses of other values defined by this
649  /// operation.
650  bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
651
652  // isOnlyUse - Return true if this node is the only use of N.
653  bool isOnlyUse(SDNode *N) const;
654
655  // isOperand - Return true if this node is an operand of N.
656  bool isOperand(SDNode *N) const;
657
658  /// getNumOperands - Return the number of values used by this operation.
659  ///
660  unsigned getNumOperands() const { return NumOperands; }
661
662  const SDOperand &getOperand(unsigned Num) const {
663    assert(Num < NumOperands && "Invalid child # of SDNode!");
664    return OperandList[Num];
665  }
666  typedef const SDOperand* op_iterator;
667  op_iterator op_begin() const { return OperandList; }
668  op_iterator op_end() const { return OperandList+NumOperands; }
669
670
671  /// getNumValues - Return the number of values defined/returned by this
672  /// operator.
673  ///
674  unsigned getNumValues() const { return NumValues; }
675
676  /// getValueType - Return the type of a specified result.
677  ///
678  MVT::ValueType getValueType(unsigned ResNo) const {
679    assert(ResNo < NumValues && "Illegal result number!");
680    return ValueList[ResNo];
681  }
682
683  typedef const MVT::ValueType* value_iterator;
684  value_iterator value_begin() const { return ValueList; }
685  value_iterator value_end() const { return ValueList+NumValues; }
686
687  /// getOperationName - Return the opcode of this operation for printing.
688  ///
689  const char* getOperationName(const SelectionDAG *G = 0) const;
690  void dump() const;
691  void dump(const SelectionDAG *G) const;
692
693  static bool classof(const SDNode *) { return true; }
694
695protected:
696  friend class SelectionDAG;
697
698  /// getValueTypeList - Return a pointer to the specified value type.
699  ///
700  static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
701
702  SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
703    OperandList = 0; NumOperands = 0;
704    ValueList = getValueTypeList(VT);
705    NumValues = 1;
706    Prev = 0; Next = 0;
707  }
708  SDNode(unsigned NT, SDOperand Op)
709    : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
710    OperandList = new SDOperand[1];
711    OperandList[0] = Op;
712    NumOperands = 1;
713    Op.Val->Uses.push_back(this);
714    ValueList = 0;
715    NumValues = 0;
716    Prev = 0; Next = 0;
717  }
718  SDNode(unsigned NT, SDOperand N1, SDOperand N2)
719    : NodeType(NT) {
720    if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
721      NodeDepth = N1.Val->getNodeDepth()+1;
722    else
723      NodeDepth = N2.Val->getNodeDepth()+1;
724    OperandList = new SDOperand[2];
725    OperandList[0] = N1;
726    OperandList[1] = N2;
727    NumOperands = 2;
728    N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
729    ValueList = 0;
730    NumValues = 0;
731    Prev = 0; Next = 0;
732  }
733  SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
734    : NodeType(NT) {
735    unsigned ND = N1.Val->getNodeDepth();
736    if (ND < N2.Val->getNodeDepth())
737      ND = N2.Val->getNodeDepth();
738    if (ND < N3.Val->getNodeDepth())
739      ND = N3.Val->getNodeDepth();
740    NodeDepth = ND+1;
741
742    OperandList = new SDOperand[3];
743    OperandList[0] = N1;
744    OperandList[1] = N2;
745    OperandList[2] = N3;
746    NumOperands = 3;
747
748    N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
749    N3.Val->Uses.push_back(this);
750    ValueList = 0;
751    NumValues = 0;
752    Prev = 0; Next = 0;
753  }
754  SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
755    : NodeType(NT) {
756    unsigned ND = N1.Val->getNodeDepth();
757    if (ND < N2.Val->getNodeDepth())
758      ND = N2.Val->getNodeDepth();
759    if (ND < N3.Val->getNodeDepth())
760      ND = N3.Val->getNodeDepth();
761    if (ND < N4.Val->getNodeDepth())
762      ND = N4.Val->getNodeDepth();
763    NodeDepth = ND+1;
764
765    OperandList = new SDOperand[4];
766    OperandList[0] = N1;
767    OperandList[1] = N2;
768    OperandList[2] = N3;
769    OperandList[3] = N4;
770    NumOperands = 4;
771
772    N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
773    N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
774    ValueList = 0;
775    NumValues = 0;
776    Prev = 0; Next = 0;
777  }
778  SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
779    NumOperands = Nodes.size();
780    OperandList = new SDOperand[NumOperands];
781
782    unsigned ND = 0;
783    for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
784      OperandList[i] = Nodes[i];
785      SDNode *N = OperandList[i].Val;
786      N->Uses.push_back(this);
787      if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
788    }
789    NodeDepth = ND+1;
790    ValueList = 0;
791    NumValues = 0;
792    Prev = 0; Next = 0;
793  }
794
795  /// MorphNodeTo - This clears the return value and operands list, and sets the
796  /// opcode of the node to the specified value.  This should only be used by
797  /// the SelectionDAG class.
798  void MorphNodeTo(unsigned Opc) {
799    NodeType = Opc;
800    ValueList = 0;
801    NumValues = 0;
802
803    // Clear the operands list, updating used nodes to remove this from their
804    // use list.
805    for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
806      I->Val->removeUser(this);
807    delete [] OperandList;
808    OperandList = 0;
809    NumOperands = 0;
810  }
811
812  void setValueTypes(MVT::ValueType VT) {
813    assert(NumValues == 0 && "Should not have values yet!");
814    ValueList = getValueTypeList(VT);
815    NumValues = 1;
816  }
817  void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
818    assert(NumValues == 0 && "Should not have values yet!");
819    ValueList = List;
820    NumValues = NumVal;
821  }
822
823  void setOperands(SDOperand Op0) {
824    assert(NumOperands == 0 && "Should not have operands yet!");
825    OperandList = new SDOperand[1];
826    OperandList[0] = Op0;
827    NumOperands = 1;
828    Op0.Val->Uses.push_back(this);
829  }
830  void setOperands(SDOperand Op0, SDOperand Op1) {
831    assert(NumOperands == 0 && "Should not have operands yet!");
832    OperandList = new SDOperand[2];
833    OperandList[0] = Op0;
834    OperandList[1] = Op1;
835    NumOperands = 2;
836    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
837  }
838  void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
839    assert(NumOperands == 0 && "Should not have operands yet!");
840    OperandList = new SDOperand[3];
841    OperandList[0] = Op0;
842    OperandList[1] = Op1;
843    OperandList[2] = Op2;
844    NumOperands = 3;
845    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
846    Op2.Val->Uses.push_back(this);
847  }
848  void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
849    assert(NumOperands == 0 && "Should not have operands yet!");
850    OperandList = new SDOperand[4];
851    OperandList[0] = Op0;
852    OperandList[1] = Op1;
853    OperandList[2] = Op2;
854    OperandList[3] = Op3;
855    NumOperands = 4;
856    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
857    Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
858  }
859  void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
860                   SDOperand Op4) {
861    assert(NumOperands == 0 && "Should not have operands yet!");
862    OperandList = new SDOperand[5];
863    OperandList[0] = Op0;
864    OperandList[1] = Op1;
865    OperandList[2] = Op2;
866    OperandList[3] = Op3;
867    OperandList[4] = Op4;
868    NumOperands = 5;
869    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
870    Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
871    Op4.Val->Uses.push_back(this);
872  }
873  void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
874                   SDOperand Op4, SDOperand Op5) {
875    assert(NumOperands == 0 && "Should not have operands yet!");
876    OperandList = new SDOperand[6];
877    OperandList[0] = Op0;
878    OperandList[1] = Op1;
879    OperandList[2] = Op2;
880    OperandList[3] = Op3;
881    OperandList[4] = Op4;
882    OperandList[5] = Op5;
883    NumOperands = 6;
884    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
885    Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
886    Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
887  }
888  void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
889                   SDOperand Op4, SDOperand Op5, SDOperand Op6) {
890    assert(NumOperands == 0 && "Should not have operands yet!");
891    OperandList = new SDOperand[7];
892    OperandList[0] = Op0;
893    OperandList[1] = Op1;
894    OperandList[2] = Op2;
895    OperandList[3] = Op3;
896    OperandList[4] = Op4;
897    OperandList[5] = Op5;
898    OperandList[6] = Op6;
899    NumOperands = 7;
900    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
901    Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
902    Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
903    Op6.Val->Uses.push_back(this);
904  }
905  void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
906                   SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
907    assert(NumOperands == 0 && "Should not have operands yet!");
908    OperandList = new SDOperand[8];
909    OperandList[0] = Op0;
910    OperandList[1] = Op1;
911    OperandList[2] = Op2;
912    OperandList[3] = Op3;
913    OperandList[4] = Op4;
914    OperandList[5] = Op5;
915    OperandList[6] = Op6;
916    OperandList[7] = Op7;
917    NumOperands = 8;
918    Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
919    Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
920    Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
921    Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
922  }
923
924  void addUser(SDNode *User) {
925    Uses.push_back(User);
926  }
927  void removeUser(SDNode *User) {
928    // Remove this user from the operand's use list.
929    for (unsigned i = Uses.size(); ; --i) {
930      assert(i != 0 && "Didn't find user!");
931      if (Uses[i-1] == User) {
932        Uses[i-1] = Uses.back();
933        Uses.pop_back();
934        return;
935      }
936    }
937  }
938};
939
940
941// Define inline functions from the SDOperand class.
942
943inline unsigned SDOperand::getOpcode() const {
944  return Val->getOpcode();
945}
946inline unsigned SDOperand::getNodeDepth() const {
947  return Val->getNodeDepth();
948}
949inline MVT::ValueType SDOperand::getValueType() const {
950  return Val->getValueType(ResNo);
951}
952inline unsigned SDOperand::getNumOperands() const {
953  return Val->getNumOperands();
954}
955inline const SDOperand &SDOperand::getOperand(unsigned i) const {
956  return Val->getOperand(i);
957}
958inline bool SDOperand::isTargetOpcode() const {
959  return Val->isTargetOpcode();
960}
961inline unsigned SDOperand::getTargetOpcode() const {
962  return Val->getTargetOpcode();
963}
964inline bool SDOperand::hasOneUse() const {
965  return Val->hasNUsesOfValue(1, ResNo);
966}
967
968/// HandleSDNode - This class is used to form a handle around another node that
969/// is persistant and is updated across invocations of replaceAllUsesWith on its
970/// operand.  This node should be directly created by end-users and not added to
971/// the AllNodes list.
972class HandleSDNode : public SDNode {
973public:
974  HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
975  ~HandleSDNode() {
976    MorphNodeTo(ISD::HANDLENODE);  // Drops operand uses.
977  }
978
979  SDOperand getValue() const { return getOperand(0); }
980};
981
982class StringSDNode : public SDNode {
983  std::string Value;
984protected:
985  friend class SelectionDAG;
986  StringSDNode(const std::string &val)
987    : SDNode(ISD::STRING, MVT::Other), Value(val) {
988  }
989public:
990  const std::string &getValue() const { return Value; }
991  static bool classof(const StringSDNode *) { return true; }
992  static bool classof(const SDNode *N) {
993    return N->getOpcode() == ISD::STRING;
994  }
995};
996
997class ConstantSDNode : public SDNode {
998  uint64_t Value;
999protected:
1000  friend class SelectionDAG;
1001  ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1002    : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
1003  }
1004public:
1005
1006  uint64_t getValue() const { return Value; }
1007
1008  int64_t getSignExtended() const {
1009    unsigned Bits = MVT::getSizeInBits(getValueType(0));
1010    return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1011  }
1012
1013  bool isNullValue() const { return Value == 0; }
1014  bool isAllOnesValue() const {
1015    int NumBits = MVT::getSizeInBits(getValueType(0));
1016    if (NumBits == 64) return Value+1 == 0;
1017    return Value == (1ULL << NumBits)-1;
1018  }
1019
1020  static bool classof(const ConstantSDNode *) { return true; }
1021  static bool classof(const SDNode *N) {
1022    return N->getOpcode() == ISD::Constant ||
1023           N->getOpcode() == ISD::TargetConstant;
1024  }
1025};
1026
1027class ConstantFPSDNode : public SDNode {
1028  double Value;
1029protected:
1030  friend class SelectionDAG;
1031  ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1032    : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1033      Value(val) {
1034  }
1035public:
1036
1037  double getValue() const { return Value; }
1038
1039  /// isExactlyValue - We don't rely on operator== working on double values, as
1040  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1041  /// As such, this method can be used to do an exact bit-for-bit comparison of
1042  /// two floating point values.
1043  bool isExactlyValue(double V) const;
1044
1045  static bool classof(const ConstantFPSDNode *) { return true; }
1046  static bool classof(const SDNode *N) {
1047    return N->getOpcode() == ISD::ConstantFP ||
1048           N->getOpcode() == ISD::TargetConstantFP;
1049  }
1050};
1051
1052class GlobalAddressSDNode : public SDNode {
1053  GlobalValue *TheGlobal;
1054  int Offset;
1055protected:
1056  friend class SelectionDAG;
1057  GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1058                      int o=0)
1059    : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1060      Offset(o) {
1061    TheGlobal = const_cast<GlobalValue*>(GA);
1062  }
1063public:
1064
1065  GlobalValue *getGlobal() const { return TheGlobal; }
1066  int getOffset() const { return Offset; }
1067
1068  static bool classof(const GlobalAddressSDNode *) { return true; }
1069  static bool classof(const SDNode *N) {
1070    return N->getOpcode() == ISD::GlobalAddress ||
1071           N->getOpcode() == ISD::TargetGlobalAddress;
1072  }
1073};
1074
1075
1076class FrameIndexSDNode : public SDNode {
1077  int FI;
1078protected:
1079  friend class SelectionDAG;
1080  FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1081    : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1082public:
1083
1084  int getIndex() const { return FI; }
1085
1086  static bool classof(const FrameIndexSDNode *) { return true; }
1087  static bool classof(const SDNode *N) {
1088    return N->getOpcode() == ISD::FrameIndex ||
1089           N->getOpcode() == ISD::TargetFrameIndex;
1090  }
1091};
1092
1093class ConstantPoolSDNode : public SDNode {
1094  Constant *C;
1095  int Offset;
1096  unsigned Alignment;
1097protected:
1098  friend class SelectionDAG;
1099  ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1100                     int o=0)
1101    : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1102      C(c), Offset(o), Alignment(0) {}
1103  ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1104                     unsigned Align)
1105    : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1106      C(c), Offset(o), Alignment(Align) {}
1107public:
1108
1109  Constant *get() const { return C; }
1110  int getOffset() const { return Offset; }
1111
1112  // Return the alignment of this constant pool object, which is either 0 (for
1113  // default alignment) or log2 of the desired value.
1114  unsigned getAlignment() const { return Alignment; }
1115
1116  static bool classof(const ConstantPoolSDNode *) { return true; }
1117  static bool classof(const SDNode *N) {
1118    return N->getOpcode() == ISD::ConstantPool ||
1119           N->getOpcode() == ISD::TargetConstantPool;
1120  }
1121};
1122
1123class BasicBlockSDNode : public SDNode {
1124  MachineBasicBlock *MBB;
1125protected:
1126  friend class SelectionDAG;
1127  BasicBlockSDNode(MachineBasicBlock *mbb)
1128    : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1129public:
1130
1131  MachineBasicBlock *getBasicBlock() const { return MBB; }
1132
1133  static bool classof(const BasicBlockSDNode *) { return true; }
1134  static bool classof(const SDNode *N) {
1135    return N->getOpcode() == ISD::BasicBlock;
1136  }
1137};
1138
1139class SrcValueSDNode : public SDNode {
1140  const Value *V;
1141  int offset;
1142protected:
1143  friend class SelectionDAG;
1144  SrcValueSDNode(const Value* v, int o)
1145    : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1146
1147public:
1148  const Value *getValue() const { return V; }
1149  int getOffset() const { return offset; }
1150
1151  static bool classof(const SrcValueSDNode *) { return true; }
1152  static bool classof(const SDNode *N) {
1153    return N->getOpcode() == ISD::SRCVALUE;
1154  }
1155};
1156
1157
1158class RegisterSDNode : public SDNode {
1159  unsigned Reg;
1160protected:
1161  friend class SelectionDAG;
1162  RegisterSDNode(unsigned reg, MVT::ValueType VT)
1163    : SDNode(ISD::Register, VT), Reg(reg) {}
1164public:
1165
1166  unsigned getReg() const { return Reg; }
1167
1168  static bool classof(const RegisterSDNode *) { return true; }
1169  static bool classof(const SDNode *N) {
1170    return N->getOpcode() == ISD::Register;
1171  }
1172};
1173
1174class ExternalSymbolSDNode : public SDNode {
1175  const char *Symbol;
1176protected:
1177  friend class SelectionDAG;
1178  ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1179    : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1180      Symbol(Sym) {
1181    }
1182public:
1183
1184  const char *getSymbol() const { return Symbol; }
1185
1186  static bool classof(const ExternalSymbolSDNode *) { return true; }
1187  static bool classof(const SDNode *N) {
1188    return N->getOpcode() == ISD::ExternalSymbol ||
1189           N->getOpcode() == ISD::TargetExternalSymbol;
1190  }
1191};
1192
1193class CondCodeSDNode : public SDNode {
1194  ISD::CondCode Condition;
1195protected:
1196  friend class SelectionDAG;
1197  CondCodeSDNode(ISD::CondCode Cond)
1198    : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1199  }
1200public:
1201
1202  ISD::CondCode get() const { return Condition; }
1203
1204  static bool classof(const CondCodeSDNode *) { return true; }
1205  static bool classof(const SDNode *N) {
1206    return N->getOpcode() == ISD::CONDCODE;
1207  }
1208};
1209
1210/// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1211/// to parameterize some operations.
1212class VTSDNode : public SDNode {
1213  MVT::ValueType ValueType;
1214protected:
1215  friend class SelectionDAG;
1216  VTSDNode(MVT::ValueType VT)
1217    : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1218public:
1219
1220  MVT::ValueType getVT() const { return ValueType; }
1221
1222  static bool classof(const VTSDNode *) { return true; }
1223  static bool classof(const SDNode *N) {
1224    return N->getOpcode() == ISD::VALUETYPE;
1225  }
1226};
1227
1228
1229class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1230  SDNode *Node;
1231  unsigned Operand;
1232
1233  SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1234public:
1235  bool operator==(const SDNodeIterator& x) const {
1236    return Operand == x.Operand;
1237  }
1238  bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1239
1240  const SDNodeIterator &operator=(const SDNodeIterator &I) {
1241    assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1242    Operand = I.Operand;
1243    return *this;
1244  }
1245
1246  pointer operator*() const {
1247    return Node->getOperand(Operand).Val;
1248  }
1249  pointer operator->() const { return operator*(); }
1250
1251  SDNodeIterator& operator++() {                // Preincrement
1252    ++Operand;
1253    return *this;
1254  }
1255  SDNodeIterator operator++(int) { // Postincrement
1256    SDNodeIterator tmp = *this; ++*this; return tmp;
1257  }
1258
1259  static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1260  static SDNodeIterator end  (SDNode *N) {
1261    return SDNodeIterator(N, N->getNumOperands());
1262  }
1263
1264  unsigned getOperand() const { return Operand; }
1265  const SDNode *getNode() const { return Node; }
1266};
1267
1268template <> struct GraphTraits<SDNode*> {
1269  typedef SDNode NodeType;
1270  typedef SDNodeIterator ChildIteratorType;
1271  static inline NodeType *getEntryNode(SDNode *N) { return N; }
1272  static inline ChildIteratorType child_begin(NodeType *N) {
1273    return SDNodeIterator::begin(N);
1274  }
1275  static inline ChildIteratorType child_end(NodeType *N) {
1276    return SDNodeIterator::end(N);
1277  }
1278};
1279
1280template<>
1281struct ilist_traits<SDNode> {
1282  static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1283  static SDNode *getNext(const SDNode *N) { return N->Next; }
1284
1285  static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1286  static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1287
1288  static SDNode *createSentinel() {
1289    return new SDNode(ISD::EntryToken, MVT::Other);
1290  }
1291  static void destroySentinel(SDNode *N) { delete N; }
1292  //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1293
1294
1295  void addNodeToList(SDNode *NTy) {}
1296  void removeNodeFromList(SDNode *NTy) {}
1297  void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1298                             const ilist_iterator<SDNode> &X,
1299                             const ilist_iterator<SDNode> &Y) {}
1300};
1301
1302} // end llvm namespace
1303
1304#endif
1305