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