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