1 //===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 // Clients should include the SelectionDAG.h file instead of this file directly.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
20 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
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"
34 class MachineBasicBlock;
36 template <typename T> struct simplify_type;
37 template <typename T> struct ilist_traits;
38 template<typename NodeTy, typename Traits> class iplist;
39 template<typename NodeTy> class ilist_iterator;
41 /// ISD namespace - This namespace contains an enum which represents all of the
42 /// SelectionDAG node types and value types.
45 //===--------------------------------------------------------------------===//
46 /// ISD::NodeType enum - This enum defines all of the operators valid in a
50 // EntryToken - This is the marker used to indicate the start of the region.
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.
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
63 AssertSext, AssertZext,
65 // Various leaf nodes.
66 STRING, BasicBlock, VALUETYPE, CONDCODE, Register,
68 GlobalAddress, FrameIndex, ConstantPool, ExternalSymbol,
70 // TargetConstant* - Like Constant*, but the DAG does not do any folding or
71 // simplification of the constant.
75 // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
76 // anything else with this node, and this is valid in the target-specific
77 // dag, turning into a GlobalAddress operand.
83 // CopyToReg - This node has three operands: a chain, a register number to
84 // set to this value, and a value.
87 // CopyFromReg - This node indicates that the input value is a virtual or
88 // physical register that is defined outside of the scope of this
89 // SelectionDAG. The register is available from the RegSDNode object.
92 // UNDEF - An undefined node
95 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
96 // a Constant, which is required to be operand #1), element of the aggregate
97 // value specified as operand #0. This is only for use before legalization,
98 // for values that will be broken into multiple registers.
101 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
102 // two values of the same integer value type, this produces a value twice as
103 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
106 // MERGE_VALUES - This node takes multiple discrete operands and returns
107 // them all as its individual results. This nodes has exactly the same
108 // number of inputs and outputs, and is only valid before legalization.
109 // This node is useful for some pieces of the code generator that want to
110 // think about a single node with multiple results, not multiple nodes.
113 // Simple integer binary arithmetic operators.
114 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
116 // Carry-setting nodes for multiple precision addition and subtraction.
117 // These nodes take two operands of the same value type, and produce two
118 // results. The first result is the normal add or sub result, the second
119 // result is the carry flag result.
122 // Carry-using nodes for multiple precision addition and subtraction. These
123 // nodes take three operands: The first two are the normal lhs and rhs to
124 // the add or sub, and the third is the input carry flag. These nodes
125 // produce two results; the normal result of the add or sub, and the output
126 // carry flag. These nodes both read and write a carry flag to allow them
127 // to them to be chained together for add and sub of arbitrarily large
131 // Simple binary floating point operators.
132 FADD, FSUB, FMUL, FDIV, FREM,
134 // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y. NOTE: This
135 // DAG node does not require that X and Y have the same type, just that they
136 // are both floating point. X and the result must have the same type.
137 // FCOPYSIGN(f32, f64) is allowed.
140 /// VBUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,..., COUNT,TYPE) - Return a vector
141 /// with the specified, possibly variable, elements. The number of elements
142 /// is required to be a power of two.
145 /// BUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...) - Return a vector
146 /// with the specified, possibly variable, elements. The number of elements
147 /// is required to be a power of two.
150 /// VINSERT_VECTOR_ELT(VECTOR, VAL, IDX, COUNT,TYPE) - Given a vector
151 /// VECTOR, an element ELEMENT, and a (potentially variable) index IDX,
152 /// return an vector with the specified element of VECTOR replaced with VAL.
153 /// COUNT and TYPE specify the type of vector, as is standard for V* nodes.
156 /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR (a legal packed
157 /// type) with the element at IDX replaced with VAL.
160 /// VEXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
161 /// (an MVT::Vector value) identified by the (potentially variable) element
165 /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
166 /// (a legal packed type vector) identified by the (potentially variable)
167 /// element number IDX.
170 /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
171 /// type as VEC1/VEC2. SHUFFLEVEC is a BUILD_VECTOR of constant int values
172 /// (regardless of whether its datatype is legal or not) that indicate
173 /// which value each result element will get. The elements of VEC1/VEC2 are
174 /// enumerated in order. This is quite similar to the Altivec 'vperm'
175 /// instruction, except that the indices must be constants and are in terms
176 /// of the element size of VEC1/VEC2, not in terms of bytes.
179 /// BINOP(LHS, RHS, COUNT,TYPE)
180 /// Simple abstract vector operators. Unlike the integer and floating point
181 /// binary operators, these nodes also take two additional operands:
182 /// a constant element count, and a value type node indicating the type of
183 /// the elements. The order is count, type, op0, op1. All vector opcodes,
184 /// including VLOAD and VConstant must currently have count and type as
185 /// their last two operands.
186 VADD, VSUB, VMUL, VSDIV, VUDIV,
189 /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
190 /// scalar value into the low element of the resultant vector type. The top
191 /// elements of the vector are undefined.
194 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
195 // an unsigned/signed value of type i[2*n], then return the top part.
198 // Bitwise operators - logical and, logical or, logical xor, shift left,
199 // shift right algebraic (shift in sign bits), shift right logical (shift in
200 // zeroes), rotate left, rotate right, and byteswap.
201 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
203 // Counting operators
206 // Select(COND, TRUEVAL, FALSEVAL)
209 // Select with condition operator - This selects between a true value and
210 // a false value (ops #2 and #3) based on the boolean result of comparing
211 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
212 // condition code in op #4, a CondCodeSDNode.
215 // SetCC operator - This evaluates to a boolean (i1) true value if the
216 // condition is true. The operands to this are the left and right operands
217 // to compare (ops #0, and #1) and the condition code to compare them with
218 // (op #2) as a CondCodeSDNode.
221 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
222 // integer shift operations, just like ADD/SUB_PARTS. The operation
224 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
225 SHL_PARTS, SRA_PARTS, SRL_PARTS,
227 // Conversion operators. These are all single input single output
228 // operations. For all of these, the result type must be strictly
229 // wider or narrower (depending on the operation) than the source
232 // SIGN_EXTEND - Used for integer types, replicating the sign bit
236 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
239 // ANY_EXTEND - Used for integer types. The high bits are undefined.
242 // TRUNCATE - Completely drop the high bits.
245 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
246 // depends on the first letter) to floating point.
250 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
251 // sign extend a small value in a large integer register (e.g. sign
252 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
253 // with the 7th bit). The size of the smaller type is indicated by the 1th
254 // operand, a ValueType node.
257 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
262 // FP_ROUND - Perform a rounding operation from the current
263 // precision down to the specified precision (currently always 64->32).
266 // FP_ROUND_INREG - This operator takes a floating point register, and
267 // rounds it to a floating point value. It then promotes it and returns it
268 // in a register of the same size. This operation effectively just discards
269 // excess precision. The type to round down to is specified by the 1th
270 // operation, a VTSDNode (currently always 64->32->64).
273 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
276 // BIT_CONVERT - Theis operator converts between integer and FP values, as
277 // if one was stored to memory as integer and the other was loaded from the
278 // same address (or equivalently for vector format conversions, etc). The
279 // source and result are required to have the same bit size (e.g.
280 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
281 // conversions, but that is a noop, deleted by getNode().
284 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
285 // absolute value, square root, sine and cosine operations.
286 FNEG, FABS, FSQRT, FSIN, FCOS,
288 // Other operators. LOAD and STORE have token chains as their first
289 // operand, then the same operands as an LLVM load/store instruction, then a
290 // SRCVALUE node that provides alias analysis information.
293 // Abstract vector version of LOAD. VLOAD has a constant element count as
294 // the first operand, followed by a value type node indicating the type of
295 // the elements, a token chain, a pointer operand, and a SRCVALUE node.
298 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
299 // memory and extend them to a larger value (e.g. load a byte into a word
300 // register). All three of these have four operands, a token chain, a
301 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
302 // indicating the type to load.
304 // SEXTLOAD loads the integer operand and sign extends it to a larger
305 // integer result type.
306 // ZEXTLOAD loads the integer operand and zero extends it to a larger
307 // integer result type.
308 // EXTLOAD is used for three things: floating point extending loads,
309 // integer extending loads [the top bits are undefined], and vector
310 // extending loads [load into low elt].
311 EXTLOAD, SEXTLOAD, ZEXTLOAD,
313 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
314 // value and stores it to memory in one operation. This can be used for
315 // either integer or floating point operands. The first four operands of
316 // this are the same as a standard store. The fifth is the ValueType to
317 // store it as (which will be smaller than the source value).
320 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
321 // to a specified boundary. The first operand is the token chain, the
322 // second is the number of bytes to allocate, and the third is the alignment
323 // boundary. The size is guaranteed to be a multiple of the stack
324 // alignment, and the alignment is guaranteed to be bigger than the stack
325 // alignment (if required) or 0 to get standard stack alignment.
328 // Control flow instructions. These all have token chains.
330 // BR - Unconditional branch. The first operand is the chain
331 // operand, the second is the MBB to branch to.
334 // BRCOND - Conditional branch. The first operand is the chain,
335 // the second is the condition, the third is the block to branch
336 // to if the condition is true.
339 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
340 // that the condition is represented as condition code, and two nodes to
341 // compare, rather than as a combined SetCC node. The operands in order are
342 // chain, cc, lhs, rhs, block to branch to if condition is true.
345 // RET - Return from function. The first operand is the chain,
346 // and any subsequent operands are the return values for the
347 // function. This operation can have variable number of operands.
350 // INLINEASM - Represents an inline asm block. This node always has two
351 // return values: a chain and a flag result. The inputs are as follows:
352 // Operand #0 : Input chain.
353 // Operand #1 : a ExternalSymbolSDNode with a pointer to the asm string.
354 // Operand #2n+2: A RegisterNode.
355 // Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
356 // Operand #last: Optional, an incoming flag.
359 // STACKSAVE - STACKSAVE has one operand, an input chain. It produces a
360 // value, the same type as the pointer type for the system, and an output
364 // STACKRESTORE has two operands, an input chain and a pointer to restore to
365 // it returns an output chain.
368 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
369 // correspond to the operands of the LLVM intrinsic functions. The only
370 // result is a token chain. The alignment argument is guaranteed to be a
376 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
377 // a call sequence, and carry arbitrary information that target might want
378 // to know. The first operand is a chain, the rest are specified by the
379 // target and not touched by the DAG optimizers.
380 CALLSEQ_START, // Beginning of a call sequence
381 CALLSEQ_END, // End of a call sequence
383 // VAARG - VAARG has three operands: an input chain, a pointer, and a
384 // SRCVALUE. It returns a pair of values: the vaarg value and a new chain.
387 // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
388 // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
392 // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
393 // pointer, and a SRCVALUE.
396 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
397 // locations with their value. This allows one use alias analysis
398 // information in the backend.
401 // PCMARKER - This corresponds to the pcmarker intrinsic.
404 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
405 // The only operand is a chain and a value and a chain are produced. The
406 // value is the contents of the architecture specific cycle counter like
407 // register (or other high accuracy low latency clock source)
410 // HANDLENODE node - Used as a handle for various purposes.
413 // LOCATION - This node is used to represent a source location for debug
414 // info. It takes token chain as input, then a line number, then a column
415 // number, then a filename, then a working dir. It produces a token chain
419 // DEBUG_LOC - This node is used to represent source line information
420 // embedded in the code. It takes a token chain as input, then a line
421 // number, then a column then a file id (provided by MachineDebugInfo.) It
422 // produces a token chain as output.
425 // DEBUG_LABEL - This node is used to mark a location in the code where a
426 // label should be generated for use by the debug information. It takes a
427 // token chain as input and then a unique id (provided by MachineDebugInfo.)
428 // It produces a token chain as output.
431 // BUILTIN_OP_END - This must be the last enum value in this list.
435 //===--------------------------------------------------------------------===//
436 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
437 /// below work out, when considering SETFALSE (something that never exists
438 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
439 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
440 /// to. If the "N" column is 1, the result of the comparison is undefined if
441 /// the input is a NAN.
443 /// All of these (except for the 'always folded ops') should be handled for
444 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
445 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
447 /// Note that these are laid out in a specific order to allow bit-twiddling
448 /// to transform conditions.
450 // Opcode N U L G E Intuitive operation
451 SETFALSE, // 0 0 0 0 Always false (always folded)
452 SETOEQ, // 0 0 0 1 True if ordered and equal
453 SETOGT, // 0 0 1 0 True if ordered and greater than
454 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
455 SETOLT, // 0 1 0 0 True if ordered and less than
456 SETOLE, // 0 1 0 1 True if ordered and less than or equal
457 SETONE, // 0 1 1 0 True if ordered and operands are unequal
458 SETO, // 0 1 1 1 True if ordered (no nans)
459 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
460 SETUEQ, // 1 0 0 1 True if unordered or equal
461 SETUGT, // 1 0 1 0 True if unordered or greater than
462 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
463 SETULT, // 1 1 0 0 True if unordered or less than
464 SETULE, // 1 1 0 1 True if unordered, less than, or equal
465 SETUNE, // 1 1 1 0 True if unordered or not equal
466 SETTRUE, // 1 1 1 1 Always true (always folded)
467 // Don't care operations: undefined if the input is a nan.
468 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
469 SETEQ, // 1 X 0 0 1 True if equal
470 SETGT, // 1 X 0 1 0 True if greater than
471 SETGE, // 1 X 0 1 1 True if greater than or equal
472 SETLT, // 1 X 1 0 0 True if less than
473 SETLE, // 1 X 1 0 1 True if less than or equal
474 SETNE, // 1 X 1 1 0 True if not equal
475 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
477 SETCC_INVALID // Marker value.
480 /// isSignedIntSetCC - Return true if this is a setcc instruction that
481 /// performs a signed comparison when used with integer operands.
482 inline bool isSignedIntSetCC(CondCode Code) {
483 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
486 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
487 /// performs an unsigned comparison when used with integer operands.
488 inline bool isUnsignedIntSetCC(CondCode Code) {
489 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
492 /// isTrueWhenEqual - Return true if the specified condition returns true if
493 /// the two operands to the condition are equal. Note that if one of the two
494 /// operands is a NaN, this value is meaningless.
495 inline bool isTrueWhenEqual(CondCode Cond) {
496 return ((int)Cond & 1) != 0;
499 /// getUnorderedFlavor - This function returns 0 if the condition is always
500 /// false if an operand is a NaN, 1 if the condition is always true if the
501 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
503 inline unsigned getUnorderedFlavor(CondCode Cond) {
504 return ((int)Cond >> 3) & 3;
507 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
508 /// 'op' is a valid SetCC operation.
509 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
511 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
512 /// when given the operation for (X op Y).
513 CondCode getSetCCSwappedOperands(CondCode Operation);
515 /// getSetCCOrOperation - Return the result of a logical OR between different
516 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
517 /// function returns SETCC_INVALID if it is not possible to represent the
518 /// resultant comparison.
519 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
521 /// getSetCCAndOperation - Return the result of a logical AND between
522 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
523 /// function returns SETCC_INVALID if it is not possible to represent the
524 /// resultant comparison.
525 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
526 } // end llvm::ISD namespace
529 //===----------------------------------------------------------------------===//
530 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
531 /// values as the result of a computation. Many nodes return multiple values,
532 /// from loads (which define a token and a return value) to ADDC (which returns
533 /// a result and a carry value), to calls (which may return an arbitrary number
536 /// As such, each use of a SelectionDAG computation must indicate the node that
537 /// computes it as well as which return value to use from that node. This pair
538 /// of information is represented with the SDOperand value type.
542 SDNode *Val; // The node defining the value we are using.
543 unsigned ResNo; // Which return value of the node we are using.
545 SDOperand() : Val(0) {}
546 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
548 bool operator==(const SDOperand &O) const {
549 return Val == O.Val && ResNo == O.ResNo;
551 bool operator!=(const SDOperand &O) const {
552 return !operator==(O);
554 bool operator<(const SDOperand &O) const {
555 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
558 SDOperand getValue(unsigned R) const {
559 return SDOperand(Val, R);
562 // isOperand - Return true if this node is an operand of N.
563 bool isOperand(SDNode *N) const;
565 /// getValueType - Return the ValueType of the referenced return value.
567 inline MVT::ValueType getValueType() const;
569 // Forwarding methods - These forward to the corresponding methods in SDNode.
570 inline unsigned getOpcode() const;
571 inline unsigned getNodeDepth() const;
572 inline unsigned getNumOperands() const;
573 inline const SDOperand &getOperand(unsigned i) const;
574 inline bool isTargetOpcode() const;
575 inline unsigned getTargetOpcode() const;
577 /// hasOneUse - Return true if there is exactly one operation using this
578 /// result value of the defining operator.
579 inline bool hasOneUse() const;
583 /// simplify_type specializations - Allow casting operators to work directly on
584 /// SDOperands as if they were SDNode*'s.
585 template<> struct simplify_type<SDOperand> {
586 typedef SDNode* SimpleType;
587 static SimpleType getSimplifiedValue(const SDOperand &Val) {
588 return static_cast<SimpleType>(Val.Val);
591 template<> struct simplify_type<const SDOperand> {
592 typedef SDNode* SimpleType;
593 static SimpleType getSimplifiedValue(const SDOperand &Val) {
594 return static_cast<SimpleType>(Val.Val);
599 /// SDNode - Represents one node in the SelectionDAG.
602 /// NodeType - The operation that this node performs.
604 unsigned short NodeType;
606 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
607 /// means that leaves have a depth of 1, things that use only leaves have a
609 unsigned short NodeDepth;
611 /// OperandList - The values that are used by this operation.
613 SDOperand *OperandList;
615 /// ValueList - The types of the values this node defines. SDNode's may
616 /// define multiple values simultaneously.
617 MVT::ValueType *ValueList;
619 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
620 unsigned short NumOperands, NumValues;
622 /// Prev/Next pointers - These pointers form the linked list of of the
623 /// AllNodes list in the current DAG.
625 friend struct ilist_traits<SDNode>;
627 /// Uses - These are all of the SDNode's that use a value produced by this
629 std::vector<SDNode*> Uses;
632 assert(NumOperands == 0 && "Operand list not cleared before deletion");
635 //===--------------------------------------------------------------------===//
638 unsigned getOpcode() const { return NodeType; }
639 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
640 unsigned getTargetOpcode() const {
641 assert(isTargetOpcode() && "Not a target opcode!");
642 return NodeType - ISD::BUILTIN_OP_END;
645 size_t use_size() const { return Uses.size(); }
646 bool use_empty() const { return Uses.empty(); }
647 bool hasOneUse() const { return Uses.size() == 1; }
649 /// getNodeDepth - Return the distance from this node to the leaves in the
650 /// graph. The leaves have a depth of 1.
651 unsigned getNodeDepth() const { return NodeDepth; }
653 typedef std::vector<SDNode*>::const_iterator use_iterator;
654 use_iterator use_begin() const { return Uses.begin(); }
655 use_iterator use_end() const { return Uses.end(); }
657 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
658 /// indicated value. This method ignores uses of other values defined by this
660 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
662 // isOnlyUse - Return true if this node is the only use of N.
663 bool isOnlyUse(SDNode *N) const;
665 // isOperand - Return true if this node is an operand of N.
666 bool isOperand(SDNode *N) const;
668 /// getNumOperands - Return the number of values used by this operation.
670 unsigned getNumOperands() const { return NumOperands; }
672 const SDOperand &getOperand(unsigned Num) const {
673 assert(Num < NumOperands && "Invalid child # of SDNode!");
674 return OperandList[Num];
676 typedef const SDOperand* op_iterator;
677 op_iterator op_begin() const { return OperandList; }
678 op_iterator op_end() const { return OperandList+NumOperands; }
681 /// getNumValues - Return the number of values defined/returned by this
684 unsigned getNumValues() const { return NumValues; }
686 /// getValueType - Return the type of a specified result.
688 MVT::ValueType getValueType(unsigned ResNo) const {
689 assert(ResNo < NumValues && "Illegal result number!");
690 return ValueList[ResNo];
693 typedef const MVT::ValueType* value_iterator;
694 value_iterator value_begin() const { return ValueList; }
695 value_iterator value_end() const { return ValueList+NumValues; }
697 /// getOperationName - Return the opcode of this operation for printing.
699 const char* getOperationName(const SelectionDAG *G = 0) const;
701 void dump(const SelectionDAG *G) const;
703 static bool classof(const SDNode *) { return true; }
706 friend class SelectionDAG;
708 /// getValueTypeList - Return a pointer to the specified value type.
710 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
712 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
713 OperandList = 0; NumOperands = 0;
714 ValueList = getValueTypeList(VT);
718 SDNode(unsigned NT, SDOperand Op)
719 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
720 OperandList = new SDOperand[1];
723 Op.Val->Uses.push_back(this);
728 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
730 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
731 NodeDepth = N1.Val->getNodeDepth()+1;
733 NodeDepth = N2.Val->getNodeDepth()+1;
734 OperandList = new SDOperand[2];
738 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
743 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
745 unsigned ND = N1.Val->getNodeDepth();
746 if (ND < N2.Val->getNodeDepth())
747 ND = N2.Val->getNodeDepth();
748 if (ND < N3.Val->getNodeDepth())
749 ND = N3.Val->getNodeDepth();
752 OperandList = new SDOperand[3];
758 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
759 N3.Val->Uses.push_back(this);
764 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
766 unsigned ND = N1.Val->getNodeDepth();
767 if (ND < N2.Val->getNodeDepth())
768 ND = N2.Val->getNodeDepth();
769 if (ND < N3.Val->getNodeDepth())
770 ND = N3.Val->getNodeDepth();
771 if (ND < N4.Val->getNodeDepth())
772 ND = N4.Val->getNodeDepth();
775 OperandList = new SDOperand[4];
782 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
783 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
788 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
789 NumOperands = Nodes.size();
790 OperandList = new SDOperand[NumOperands];
793 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
794 OperandList[i] = Nodes[i];
795 SDNode *N = OperandList[i].Val;
796 N->Uses.push_back(this);
797 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
805 /// MorphNodeTo - This clears the return value and operands list, and sets the
806 /// opcode of the node to the specified value. This should only be used by
807 /// the SelectionDAG class.
808 void MorphNodeTo(unsigned Opc) {
813 // Clear the operands list, updating used nodes to remove this from their
815 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
816 I->Val->removeUser(this);
817 delete [] OperandList;
822 void setValueTypes(MVT::ValueType VT) {
823 assert(NumValues == 0 && "Should not have values yet!");
824 ValueList = getValueTypeList(VT);
827 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
828 assert(NumValues == 0 && "Should not have values yet!");
833 void setOperands(SDOperand Op0) {
834 assert(NumOperands == 0 && "Should not have operands yet!");
835 OperandList = new SDOperand[1];
836 OperandList[0] = Op0;
838 Op0.Val->Uses.push_back(this);
840 void setOperands(SDOperand Op0, SDOperand Op1) {
841 assert(NumOperands == 0 && "Should not have operands yet!");
842 OperandList = new SDOperand[2];
843 OperandList[0] = Op0;
844 OperandList[1] = Op1;
846 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
848 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
849 assert(NumOperands == 0 && "Should not have operands yet!");
850 OperandList = new SDOperand[3];
851 OperandList[0] = Op0;
852 OperandList[1] = Op1;
853 OperandList[2] = Op2;
855 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
856 Op2.Val->Uses.push_back(this);
858 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
859 assert(NumOperands == 0 && "Should not have operands yet!");
860 OperandList = new SDOperand[4];
861 OperandList[0] = Op0;
862 OperandList[1] = Op1;
863 OperandList[2] = Op2;
864 OperandList[3] = Op3;
866 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
867 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
869 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
871 assert(NumOperands == 0 && "Should not have operands yet!");
872 OperandList = new SDOperand[5];
873 OperandList[0] = Op0;
874 OperandList[1] = Op1;
875 OperandList[2] = Op2;
876 OperandList[3] = Op3;
877 OperandList[4] = Op4;
879 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
880 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
881 Op4.Val->Uses.push_back(this);
883 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
884 SDOperand Op4, SDOperand Op5) {
885 assert(NumOperands == 0 && "Should not have operands yet!");
886 OperandList = new SDOperand[6];
887 OperandList[0] = Op0;
888 OperandList[1] = Op1;
889 OperandList[2] = Op2;
890 OperandList[3] = Op3;
891 OperandList[4] = Op4;
892 OperandList[5] = Op5;
894 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
895 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
896 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
898 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
899 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
900 assert(NumOperands == 0 && "Should not have operands yet!");
901 OperandList = new SDOperand[7];
902 OperandList[0] = Op0;
903 OperandList[1] = Op1;
904 OperandList[2] = Op2;
905 OperandList[3] = Op3;
906 OperandList[4] = Op4;
907 OperandList[5] = Op5;
908 OperandList[6] = Op6;
910 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
911 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
912 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
913 Op6.Val->Uses.push_back(this);
915 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
916 SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
917 assert(NumOperands == 0 && "Should not have operands yet!");
918 OperandList = new SDOperand[8];
919 OperandList[0] = Op0;
920 OperandList[1] = Op1;
921 OperandList[2] = Op2;
922 OperandList[3] = Op3;
923 OperandList[4] = Op4;
924 OperandList[5] = Op5;
925 OperandList[6] = Op6;
926 OperandList[7] = Op7;
928 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
929 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
930 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
931 Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
934 void addUser(SDNode *User) {
935 Uses.push_back(User);
937 void removeUser(SDNode *User) {
938 // Remove this user from the operand's use list.
939 for (unsigned i = Uses.size(); ; --i) {
940 assert(i != 0 && "Didn't find user!");
941 if (Uses[i-1] == User) {
942 Uses[i-1] = Uses.back();
951 // Define inline functions from the SDOperand class.
953 inline unsigned SDOperand::getOpcode() const {
954 return Val->getOpcode();
956 inline unsigned SDOperand::getNodeDepth() const {
957 return Val->getNodeDepth();
959 inline MVT::ValueType SDOperand::getValueType() const {
960 return Val->getValueType(ResNo);
962 inline unsigned SDOperand::getNumOperands() const {
963 return Val->getNumOperands();
965 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
966 return Val->getOperand(i);
968 inline bool SDOperand::isTargetOpcode() const {
969 return Val->isTargetOpcode();
971 inline unsigned SDOperand::getTargetOpcode() const {
972 return Val->getTargetOpcode();
974 inline bool SDOperand::hasOneUse() const {
975 return Val->hasNUsesOfValue(1, ResNo);
978 /// HandleSDNode - This class is used to form a handle around another node that
979 /// is persistant and is updated across invocations of replaceAllUsesWith on its
980 /// operand. This node should be directly created by end-users and not added to
981 /// the AllNodes list.
982 class HandleSDNode : public SDNode {
984 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
986 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
989 SDOperand getValue() const { return getOperand(0); }
992 class StringSDNode : public SDNode {
995 friend class SelectionDAG;
996 StringSDNode(const std::string &val)
997 : SDNode(ISD::STRING, MVT::Other), Value(val) {
1000 const std::string &getValue() const { return Value; }
1001 static bool classof(const StringSDNode *) { return true; }
1002 static bool classof(const SDNode *N) {
1003 return N->getOpcode() == ISD::STRING;
1007 class ConstantSDNode : public SDNode {
1010 friend class SelectionDAG;
1011 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1012 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
1016 uint64_t getValue() const { return Value; }
1018 int64_t getSignExtended() const {
1019 unsigned Bits = MVT::getSizeInBits(getValueType(0));
1020 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1023 bool isNullValue() const { return Value == 0; }
1024 bool isAllOnesValue() const {
1025 int NumBits = MVT::getSizeInBits(getValueType(0));
1026 if (NumBits == 64) return Value+1 == 0;
1027 return Value == (1ULL << NumBits)-1;
1030 static bool classof(const ConstantSDNode *) { return true; }
1031 static bool classof(const SDNode *N) {
1032 return N->getOpcode() == ISD::Constant ||
1033 N->getOpcode() == ISD::TargetConstant;
1037 class ConstantFPSDNode : public SDNode {
1040 friend class SelectionDAG;
1041 ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1042 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1047 double getValue() const { return Value; }
1049 /// isExactlyValue - We don't rely on operator== working on double values, as
1050 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1051 /// As such, this method can be used to do an exact bit-for-bit comparison of
1052 /// two floating point values.
1053 bool isExactlyValue(double V) const;
1055 static bool classof(const ConstantFPSDNode *) { return true; }
1056 static bool classof(const SDNode *N) {
1057 return N->getOpcode() == ISD::ConstantFP ||
1058 N->getOpcode() == ISD::TargetConstantFP;
1062 class GlobalAddressSDNode : public SDNode {
1063 GlobalValue *TheGlobal;
1066 friend class SelectionDAG;
1067 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1069 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1071 TheGlobal = const_cast<GlobalValue*>(GA);
1075 GlobalValue *getGlobal() const { return TheGlobal; }
1076 int getOffset() const { return Offset; }
1078 static bool classof(const GlobalAddressSDNode *) { return true; }
1079 static bool classof(const SDNode *N) {
1080 return N->getOpcode() == ISD::GlobalAddress ||
1081 N->getOpcode() == ISD::TargetGlobalAddress;
1086 class FrameIndexSDNode : public SDNode {
1089 friend class SelectionDAG;
1090 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1091 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1094 int getIndex() const { return FI; }
1096 static bool classof(const FrameIndexSDNode *) { return true; }
1097 static bool classof(const SDNode *N) {
1098 return N->getOpcode() == ISD::FrameIndex ||
1099 N->getOpcode() == ISD::TargetFrameIndex;
1103 class ConstantPoolSDNode : public SDNode {
1108 friend class SelectionDAG;
1109 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1111 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1112 C(c), Offset(o), Alignment(0) {}
1113 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1115 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1116 C(c), Offset(o), Alignment(Align) {}
1119 Constant *get() const { return C; }
1120 int getOffset() const { return Offset; }
1122 // Return the alignment of this constant pool object, which is either 0 (for
1123 // default alignment) or log2 of the desired value.
1124 unsigned getAlignment() const { return Alignment; }
1126 static bool classof(const ConstantPoolSDNode *) { return true; }
1127 static bool classof(const SDNode *N) {
1128 return N->getOpcode() == ISD::ConstantPool ||
1129 N->getOpcode() == ISD::TargetConstantPool;
1133 class BasicBlockSDNode : public SDNode {
1134 MachineBasicBlock *MBB;
1136 friend class SelectionDAG;
1137 BasicBlockSDNode(MachineBasicBlock *mbb)
1138 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1141 MachineBasicBlock *getBasicBlock() const { return MBB; }
1143 static bool classof(const BasicBlockSDNode *) { return true; }
1144 static bool classof(const SDNode *N) {
1145 return N->getOpcode() == ISD::BasicBlock;
1149 class SrcValueSDNode : public SDNode {
1153 friend class SelectionDAG;
1154 SrcValueSDNode(const Value* v, int o)
1155 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1158 const Value *getValue() const { return V; }
1159 int getOffset() const { return offset; }
1161 static bool classof(const SrcValueSDNode *) { return true; }
1162 static bool classof(const SDNode *N) {
1163 return N->getOpcode() == ISD::SRCVALUE;
1168 class RegisterSDNode : public SDNode {
1171 friend class SelectionDAG;
1172 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1173 : SDNode(ISD::Register, VT), Reg(reg) {}
1176 unsigned getReg() const { return Reg; }
1178 static bool classof(const RegisterSDNode *) { return true; }
1179 static bool classof(const SDNode *N) {
1180 return N->getOpcode() == ISD::Register;
1184 class ExternalSymbolSDNode : public SDNode {
1187 friend class SelectionDAG;
1188 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1189 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1194 const char *getSymbol() const { return Symbol; }
1196 static bool classof(const ExternalSymbolSDNode *) { return true; }
1197 static bool classof(const SDNode *N) {
1198 return N->getOpcode() == ISD::ExternalSymbol ||
1199 N->getOpcode() == ISD::TargetExternalSymbol;
1203 class CondCodeSDNode : public SDNode {
1204 ISD::CondCode Condition;
1206 friend class SelectionDAG;
1207 CondCodeSDNode(ISD::CondCode Cond)
1208 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1212 ISD::CondCode get() const { return Condition; }
1214 static bool classof(const CondCodeSDNode *) { return true; }
1215 static bool classof(const SDNode *N) {
1216 return N->getOpcode() == ISD::CONDCODE;
1220 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1221 /// to parameterize some operations.
1222 class VTSDNode : public SDNode {
1223 MVT::ValueType ValueType;
1225 friend class SelectionDAG;
1226 VTSDNode(MVT::ValueType VT)
1227 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1230 MVT::ValueType getVT() const { return ValueType; }
1232 static bool classof(const VTSDNode *) { return true; }
1233 static bool classof(const SDNode *N) {
1234 return N->getOpcode() == ISD::VALUETYPE;
1239 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1243 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1245 bool operator==(const SDNodeIterator& x) const {
1246 return Operand == x.Operand;
1248 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1250 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1251 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1252 Operand = I.Operand;
1256 pointer operator*() const {
1257 return Node->getOperand(Operand).Val;
1259 pointer operator->() const { return operator*(); }
1261 SDNodeIterator& operator++() { // Preincrement
1265 SDNodeIterator operator++(int) { // Postincrement
1266 SDNodeIterator tmp = *this; ++*this; return tmp;
1269 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1270 static SDNodeIterator end (SDNode *N) {
1271 return SDNodeIterator(N, N->getNumOperands());
1274 unsigned getOperand() const { return Operand; }
1275 const SDNode *getNode() const { return Node; }
1278 template <> struct GraphTraits<SDNode*> {
1279 typedef SDNode NodeType;
1280 typedef SDNodeIterator ChildIteratorType;
1281 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1282 static inline ChildIteratorType child_begin(NodeType *N) {
1283 return SDNodeIterator::begin(N);
1285 static inline ChildIteratorType child_end(NodeType *N) {
1286 return SDNodeIterator::end(N);
1291 struct ilist_traits<SDNode> {
1292 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1293 static SDNode *getNext(const SDNode *N) { return N->Next; }
1295 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1296 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1298 static SDNode *createSentinel() {
1299 return new SDNode(ISD::EntryToken, MVT::Other);
1301 static void destroySentinel(SDNode *N) { delete N; }
1302 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1305 void addNodeToList(SDNode *NTy) {}
1306 void removeNodeFromList(SDNode *NTy) {}
1307 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1308 const ilist_iterator<SDNode> &X,
1309 const ilist_iterator<SDNode> &Y) {}
1312 } // end llvm namespace