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