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