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