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