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