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