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