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