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