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 /// X = VBIT_CONVERT(Y) and X = VBIT_CONVERT(Y, COUNT,TYPE) - This node
180 /// represents a conversion from or to an ISD::Vector type.
182 /// This is lowered to a BIT_CONVERT of the appropriate input/output types.
183 /// The input and output are required to have the same size and at least one
184 /// is required to be a vector.
186 /// If the source is a vector, this takes three operands (like any other
187 /// vector consumer) which indicate the size and type of the vector input.
188 /// Otherwise it takes one input.
191 /// BINOP(LHS, RHS, COUNT,TYPE)
192 /// Simple abstract vector operators. Unlike the integer and floating point
193 /// binary operators, these nodes also take two additional operands:
194 /// a constant element count, and a value type node indicating the type of
195 /// the elements. The order is count, type, op0, op1. All vector opcodes,
196 /// including VLOAD and VConstant must currently have count and type as
197 /// their last two operands.
198 VADD, VSUB, VMUL, VSDIV, VUDIV,
201 /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
202 /// scalar value into the low element of the resultant vector type. The top
203 /// elements of the vector are undefined.
206 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
207 // an unsigned/signed value of type i[2*n], then return the top part.
210 // Bitwise operators - logical and, logical or, logical xor, shift left,
211 // shift right algebraic (shift in sign bits), shift right logical (shift in
212 // zeroes), rotate left, rotate right, and byteswap.
213 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
215 // Counting operators
218 // Select(COND, TRUEVAL, FALSEVAL)
221 // Select with condition operator - This selects between a true value and
222 // a false value (ops #2 and #3) based on the boolean result of comparing
223 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
224 // condition code in op #4, a CondCodeSDNode.
227 // SetCC operator - This evaluates to a boolean (i1) true value if the
228 // condition is true. The operands to this are the left and right operands
229 // to compare (ops #0, and #1) and the condition code to compare them with
230 // (op #2) as a CondCodeSDNode.
233 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
234 // integer shift operations, just like ADD/SUB_PARTS. The operation
236 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
237 SHL_PARTS, SRA_PARTS, SRL_PARTS,
239 // Conversion operators. These are all single input single output
240 // operations. For all of these, the result type must be strictly
241 // wider or narrower (depending on the operation) than the source
244 // SIGN_EXTEND - Used for integer types, replicating the sign bit
248 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
251 // ANY_EXTEND - Used for integer types. The high bits are undefined.
254 // TRUNCATE - Completely drop the high bits.
257 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
258 // depends on the first letter) to floating point.
262 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
263 // sign extend a small value in a large integer register (e.g. sign
264 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
265 // with the 7th bit). The size of the smaller type is indicated by the 1th
266 // operand, a ValueType node.
269 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
274 // FP_ROUND - Perform a rounding operation from the current
275 // precision down to the specified precision (currently always 64->32).
278 // FP_ROUND_INREG - This operator takes a floating point register, and
279 // rounds it to a floating point value. It then promotes it and returns it
280 // in a register of the same size. This operation effectively just discards
281 // excess precision. The type to round down to is specified by the 1th
282 // operation, a VTSDNode (currently always 64->32->64).
285 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
288 // BIT_CONVERT - Theis operator converts between integer and FP values, as
289 // if one was stored to memory as integer and the other was loaded from the
290 // same address (or equivalently for vector format conversions, etc). The
291 // source and result are required to have the same bit size (e.g.
292 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
293 // conversions, but that is a noop, deleted by getNode().
296 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
297 // absolute value, square root, sine and cosine operations.
298 FNEG, FABS, FSQRT, FSIN, FCOS,
300 // Other operators. LOAD and STORE have token chains as their first
301 // operand, then the same operands as an LLVM load/store instruction, then a
302 // SRCVALUE node that provides alias analysis information.
305 // Abstract vector version of LOAD. VLOAD has a constant element count as
306 // the first operand, followed by a value type node indicating the type of
307 // the elements, a token chain, a pointer operand, and a SRCVALUE node.
310 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
311 // memory and extend them to a larger value (e.g. load a byte into a word
312 // register). All three of these have four operands, a token chain, a
313 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
314 // indicating the type to load.
316 // SEXTLOAD loads the integer operand and sign extends it to a larger
317 // integer result type.
318 // ZEXTLOAD loads the integer operand and zero extends it to a larger
319 // integer result type.
320 // EXTLOAD is used for three things: floating point extending loads,
321 // integer extending loads [the top bits are undefined], and vector
322 // extending loads [load into low elt].
323 EXTLOAD, SEXTLOAD, ZEXTLOAD,
325 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
326 // value and stores it to memory in one operation. This can be used for
327 // either integer or floating point operands. The first four operands of
328 // this are the same as a standard store. The fifth is the ValueType to
329 // store it as (which will be smaller than the source value).
332 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
333 // to a specified boundary. The first operand is the token chain, the
334 // second is the number of bytes to allocate, and the third is the alignment
335 // boundary. The size is guaranteed to be a multiple of the stack
336 // alignment, and the alignment is guaranteed to be bigger than the stack
337 // alignment (if required) or 0 to get standard stack alignment.
340 // Control flow instructions. These all have token chains.
342 // BR - Unconditional branch. The first operand is the chain
343 // operand, the second is the MBB to branch to.
346 // BRCOND - Conditional branch. The first operand is the chain,
347 // the second is the condition, the third is the block to branch
348 // to if the condition is true.
351 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
352 // that the condition is represented as condition code, and two nodes to
353 // compare, rather than as a combined SetCC node. The operands in order are
354 // chain, cc, lhs, rhs, block to branch to if condition is true.
357 // RET - Return from function. The first operand is the chain,
358 // and any subsequent operands are the return values for the
359 // function. This operation can have variable number of operands.
362 // INLINEASM - Represents an inline asm block. This node always has two
363 // return values: a chain and a flag result. The inputs are as follows:
364 // Operand #0 : Input chain.
365 // Operand #1 : a ExternalSymbolSDNode with a pointer to the asm string.
366 // Operand #2n+2: A RegisterNode.
367 // Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
368 // Operand #last: Optional, an incoming flag.
371 // STACKSAVE - STACKSAVE has one operand, an input chain. It produces a
372 // value, the same type as the pointer type for the system, and an output
376 // STACKRESTORE has two operands, an input chain and a pointer to restore to
377 // it returns an output chain.
380 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
381 // correspond to the operands of the LLVM intrinsic functions. The only
382 // result is a token chain. The alignment argument is guaranteed to be a
388 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
389 // a call sequence, and carry arbitrary information that target might want
390 // to know. The first operand is a chain, the rest are specified by the
391 // target and not touched by the DAG optimizers.
392 CALLSEQ_START, // Beginning of a call sequence
393 CALLSEQ_END, // End of a call sequence
395 // VAARG - VAARG has three operands: an input chain, a pointer, and a
396 // SRCVALUE. It returns a pair of values: the vaarg value and a new chain.
399 // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
400 // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
404 // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
405 // pointer, and a SRCVALUE.
408 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
409 // locations with their value. This allows one use alias analysis
410 // information in the backend.
413 // PCMARKER - This corresponds to the pcmarker intrinsic.
416 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
417 // The only operand is a chain and a value and a chain are produced. The
418 // value is the contents of the architecture specific cycle counter like
419 // register (or other high accuracy low latency clock source)
422 // HANDLENODE node - Used as a handle for various purposes.
425 // LOCATION - This node is used to represent a source location for debug
426 // info. It takes token chain as input, then a line number, then a column
427 // number, then a filename, then a working dir. It produces a token chain
431 // DEBUG_LOC - This node is used to represent source line information
432 // embedded in the code. It takes a token chain as input, then a line
433 // number, then a column then a file id (provided by MachineDebugInfo.) It
434 // produces a token chain as output.
437 // DEBUG_LABEL - This node is used to mark a location in the code where a
438 // label should be generated for use by the debug information. It takes a
439 // token chain as input and then a unique id (provided by MachineDebugInfo.)
440 // It produces a token chain as output.
443 // BUILTIN_OP_END - This must be the last enum value in this list.
447 //===--------------------------------------------------------------------===//
448 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
449 /// below work out, when considering SETFALSE (something that never exists
450 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
451 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
452 /// to. If the "N" column is 1, the result of the comparison is undefined if
453 /// the input is a NAN.
455 /// All of these (except for the 'always folded ops') should be handled for
456 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
457 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
459 /// Note that these are laid out in a specific order to allow bit-twiddling
460 /// to transform conditions.
462 // Opcode N U L G E Intuitive operation
463 SETFALSE, // 0 0 0 0 Always false (always folded)
464 SETOEQ, // 0 0 0 1 True if ordered and equal
465 SETOGT, // 0 0 1 0 True if ordered and greater than
466 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
467 SETOLT, // 0 1 0 0 True if ordered and less than
468 SETOLE, // 0 1 0 1 True if ordered and less than or equal
469 SETONE, // 0 1 1 0 True if ordered and operands are unequal
470 SETO, // 0 1 1 1 True if ordered (no nans)
471 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
472 SETUEQ, // 1 0 0 1 True if unordered or equal
473 SETUGT, // 1 0 1 0 True if unordered or greater than
474 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
475 SETULT, // 1 1 0 0 True if unordered or less than
476 SETULE, // 1 1 0 1 True if unordered, less than, or equal
477 SETUNE, // 1 1 1 0 True if unordered or not equal
478 SETTRUE, // 1 1 1 1 Always true (always folded)
479 // Don't care operations: undefined if the input is a nan.
480 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
481 SETEQ, // 1 X 0 0 1 True if equal
482 SETGT, // 1 X 0 1 0 True if greater than
483 SETGE, // 1 X 0 1 1 True if greater than or equal
484 SETLT, // 1 X 1 0 0 True if less than
485 SETLE, // 1 X 1 0 1 True if less than or equal
486 SETNE, // 1 X 1 1 0 True if not equal
487 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
489 SETCC_INVALID // Marker value.
492 /// isSignedIntSetCC - Return true if this is a setcc instruction that
493 /// performs a signed comparison when used with integer operands.
494 inline bool isSignedIntSetCC(CondCode Code) {
495 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
498 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
499 /// performs an unsigned comparison when used with integer operands.
500 inline bool isUnsignedIntSetCC(CondCode Code) {
501 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
504 /// isTrueWhenEqual - Return true if the specified condition returns true if
505 /// the two operands to the condition are equal. Note that if one of the two
506 /// operands is a NaN, this value is meaningless.
507 inline bool isTrueWhenEqual(CondCode Cond) {
508 return ((int)Cond & 1) != 0;
511 /// getUnorderedFlavor - This function returns 0 if the condition is always
512 /// false if an operand is a NaN, 1 if the condition is always true if the
513 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
515 inline unsigned getUnorderedFlavor(CondCode Cond) {
516 return ((int)Cond >> 3) & 3;
519 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
520 /// 'op' is a valid SetCC operation.
521 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
523 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
524 /// when given the operation for (X op Y).
525 CondCode getSetCCSwappedOperands(CondCode Operation);
527 /// getSetCCOrOperation - Return the result of a logical OR between different
528 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
529 /// function returns SETCC_INVALID if it is not possible to represent the
530 /// resultant comparison.
531 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
533 /// getSetCCAndOperation - Return the result of a logical AND between
534 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
535 /// function returns SETCC_INVALID if it is not possible to represent the
536 /// resultant comparison.
537 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
538 } // end llvm::ISD namespace
541 //===----------------------------------------------------------------------===//
542 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
543 /// values as the result of a computation. Many nodes return multiple values,
544 /// from loads (which define a token and a return value) to ADDC (which returns
545 /// a result and a carry value), to calls (which may return an arbitrary number
548 /// As such, each use of a SelectionDAG computation must indicate the node that
549 /// computes it as well as which return value to use from that node. This pair
550 /// of information is represented with the SDOperand value type.
554 SDNode *Val; // The node defining the value we are using.
555 unsigned ResNo; // Which return value of the node we are using.
557 SDOperand() : Val(0) {}
558 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
560 bool operator==(const SDOperand &O) const {
561 return Val == O.Val && ResNo == O.ResNo;
563 bool operator!=(const SDOperand &O) const {
564 return !operator==(O);
566 bool operator<(const SDOperand &O) const {
567 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
570 SDOperand getValue(unsigned R) const {
571 return SDOperand(Val, R);
574 // isOperand - Return true if this node is an operand of N.
575 bool isOperand(SDNode *N) const;
577 /// getValueType - Return the ValueType of the referenced return value.
579 inline MVT::ValueType getValueType() const;
581 // Forwarding methods - These forward to the corresponding methods in SDNode.
582 inline unsigned getOpcode() const;
583 inline unsigned getNodeDepth() const;
584 inline unsigned getNumOperands() const;
585 inline const SDOperand &getOperand(unsigned i) const;
586 inline bool isTargetOpcode() const;
587 inline unsigned getTargetOpcode() const;
589 /// hasOneUse - Return true if there is exactly one operation using this
590 /// result value of the defining operator.
591 inline bool hasOneUse() const;
595 /// simplify_type specializations - Allow casting operators to work directly on
596 /// SDOperands as if they were SDNode*'s.
597 template<> struct simplify_type<SDOperand> {
598 typedef SDNode* SimpleType;
599 static SimpleType getSimplifiedValue(const SDOperand &Val) {
600 return static_cast<SimpleType>(Val.Val);
603 template<> struct simplify_type<const SDOperand> {
604 typedef SDNode* SimpleType;
605 static SimpleType getSimplifiedValue(const SDOperand &Val) {
606 return static_cast<SimpleType>(Val.Val);
611 /// SDNode - Represents one node in the SelectionDAG.
614 /// NodeType - The operation that this node performs.
616 unsigned short NodeType;
618 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
619 /// means that leaves have a depth of 1, things that use only leaves have a
621 unsigned short NodeDepth;
623 /// OperandList - The values that are used by this operation.
625 SDOperand *OperandList;
627 /// ValueList - The types of the values this node defines. SDNode's may
628 /// define multiple values simultaneously.
629 MVT::ValueType *ValueList;
631 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
632 unsigned short NumOperands, NumValues;
634 /// Prev/Next pointers - These pointers form the linked list of of the
635 /// AllNodes list in the current DAG.
637 friend struct ilist_traits<SDNode>;
639 /// Uses - These are all of the SDNode's that use a value produced by this
641 std::vector<SDNode*> Uses;
644 assert(NumOperands == 0 && "Operand list not cleared before deletion");
647 //===--------------------------------------------------------------------===//
650 unsigned getOpcode() const { return NodeType; }
651 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
652 unsigned getTargetOpcode() const {
653 assert(isTargetOpcode() && "Not a target opcode!");
654 return NodeType - ISD::BUILTIN_OP_END;
657 size_t use_size() const { return Uses.size(); }
658 bool use_empty() const { return Uses.empty(); }
659 bool hasOneUse() const { return Uses.size() == 1; }
661 /// getNodeDepth - Return the distance from this node to the leaves in the
662 /// graph. The leaves have a depth of 1.
663 unsigned getNodeDepth() const { return NodeDepth; }
665 typedef std::vector<SDNode*>::const_iterator use_iterator;
666 use_iterator use_begin() const { return Uses.begin(); }
667 use_iterator use_end() const { return Uses.end(); }
669 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
670 /// indicated value. This method ignores uses of other values defined by this
672 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
674 // isOnlyUse - Return true if this node is the only use of N.
675 bool isOnlyUse(SDNode *N) const;
677 // isOperand - Return true if this node is an operand of N.
678 bool isOperand(SDNode *N) const;
680 /// getNumOperands - Return the number of values used by this operation.
682 unsigned getNumOperands() const { return NumOperands; }
684 const SDOperand &getOperand(unsigned Num) const {
685 assert(Num < NumOperands && "Invalid child # of SDNode!");
686 return OperandList[Num];
688 typedef const SDOperand* op_iterator;
689 op_iterator op_begin() const { return OperandList; }
690 op_iterator op_end() const { return OperandList+NumOperands; }
693 /// getNumValues - Return the number of values defined/returned by this
696 unsigned getNumValues() const { return NumValues; }
698 /// getValueType - Return the type of a specified result.
700 MVT::ValueType getValueType(unsigned ResNo) const {
701 assert(ResNo < NumValues && "Illegal result number!");
702 return ValueList[ResNo];
705 typedef const MVT::ValueType* value_iterator;
706 value_iterator value_begin() const { return ValueList; }
707 value_iterator value_end() const { return ValueList+NumValues; }
709 /// getOperationName - Return the opcode of this operation for printing.
711 const char* getOperationName(const SelectionDAG *G = 0) const;
713 void dump(const SelectionDAG *G) const;
715 static bool classof(const SDNode *) { return true; }
718 friend class SelectionDAG;
720 /// getValueTypeList - Return a pointer to the specified value type.
722 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
724 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
725 OperandList = 0; NumOperands = 0;
726 ValueList = getValueTypeList(VT);
730 SDNode(unsigned NT, SDOperand Op)
731 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
732 OperandList = new SDOperand[1];
735 Op.Val->Uses.push_back(this);
740 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
742 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
743 NodeDepth = N1.Val->getNodeDepth()+1;
745 NodeDepth = N2.Val->getNodeDepth()+1;
746 OperandList = new SDOperand[2];
750 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
755 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
757 unsigned ND = N1.Val->getNodeDepth();
758 if (ND < N2.Val->getNodeDepth())
759 ND = N2.Val->getNodeDepth();
760 if (ND < N3.Val->getNodeDepth())
761 ND = N3.Val->getNodeDepth();
764 OperandList = new SDOperand[3];
770 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
771 N3.Val->Uses.push_back(this);
776 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
778 unsigned ND = N1.Val->getNodeDepth();
779 if (ND < N2.Val->getNodeDepth())
780 ND = N2.Val->getNodeDepth();
781 if (ND < N3.Val->getNodeDepth())
782 ND = N3.Val->getNodeDepth();
783 if (ND < N4.Val->getNodeDepth())
784 ND = N4.Val->getNodeDepth();
787 OperandList = new SDOperand[4];
794 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
795 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
800 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
801 NumOperands = Nodes.size();
802 OperandList = new SDOperand[NumOperands];
805 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
806 OperandList[i] = Nodes[i];
807 SDNode *N = OperandList[i].Val;
808 N->Uses.push_back(this);
809 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
817 /// MorphNodeTo - This clears the return value and operands list, and sets the
818 /// opcode of the node to the specified value. This should only be used by
819 /// the SelectionDAG class.
820 void MorphNodeTo(unsigned Opc) {
825 // Clear the operands list, updating used nodes to remove this from their
827 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
828 I->Val->removeUser(this);
829 delete [] OperandList;
834 void setValueTypes(MVT::ValueType VT) {
835 assert(NumValues == 0 && "Should not have values yet!");
836 ValueList = getValueTypeList(VT);
839 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
840 assert(NumValues == 0 && "Should not have values yet!");
845 void setOperands(SDOperand Op0) {
846 assert(NumOperands == 0 && "Should not have operands yet!");
847 OperandList = new SDOperand[1];
848 OperandList[0] = Op0;
850 Op0.Val->Uses.push_back(this);
852 void setOperands(SDOperand Op0, SDOperand Op1) {
853 assert(NumOperands == 0 && "Should not have operands yet!");
854 OperandList = new SDOperand[2];
855 OperandList[0] = Op0;
856 OperandList[1] = Op1;
858 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
860 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
861 assert(NumOperands == 0 && "Should not have operands yet!");
862 OperandList = new SDOperand[3];
863 OperandList[0] = Op0;
864 OperandList[1] = Op1;
865 OperandList[2] = Op2;
867 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
868 Op2.Val->Uses.push_back(this);
870 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
871 assert(NumOperands == 0 && "Should not have operands yet!");
872 OperandList = new SDOperand[4];
873 OperandList[0] = Op0;
874 OperandList[1] = Op1;
875 OperandList[2] = Op2;
876 OperandList[3] = Op3;
878 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
879 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
881 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
883 assert(NumOperands == 0 && "Should not have operands yet!");
884 OperandList = new SDOperand[5];
885 OperandList[0] = Op0;
886 OperandList[1] = Op1;
887 OperandList[2] = Op2;
888 OperandList[3] = Op3;
889 OperandList[4] = Op4;
891 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
892 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
893 Op4.Val->Uses.push_back(this);
895 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
896 SDOperand Op4, SDOperand Op5) {
897 assert(NumOperands == 0 && "Should not have operands yet!");
898 OperandList = new SDOperand[6];
899 OperandList[0] = Op0;
900 OperandList[1] = Op1;
901 OperandList[2] = Op2;
902 OperandList[3] = Op3;
903 OperandList[4] = Op4;
904 OperandList[5] = Op5;
906 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
907 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
908 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
910 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
911 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
912 assert(NumOperands == 0 && "Should not have operands yet!");
913 OperandList = new SDOperand[7];
914 OperandList[0] = Op0;
915 OperandList[1] = Op1;
916 OperandList[2] = Op2;
917 OperandList[3] = Op3;
918 OperandList[4] = Op4;
919 OperandList[5] = Op5;
920 OperandList[6] = Op6;
922 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
923 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
924 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
925 Op6.Val->Uses.push_back(this);
927 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
928 SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
929 assert(NumOperands == 0 && "Should not have operands yet!");
930 OperandList = new SDOperand[8];
931 OperandList[0] = Op0;
932 OperandList[1] = Op1;
933 OperandList[2] = Op2;
934 OperandList[3] = Op3;
935 OperandList[4] = Op4;
936 OperandList[5] = Op5;
937 OperandList[6] = Op6;
938 OperandList[7] = Op7;
940 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
941 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
942 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
943 Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
946 void addUser(SDNode *User) {
947 Uses.push_back(User);
949 void removeUser(SDNode *User) {
950 // Remove this user from the operand's use list.
951 for (unsigned i = Uses.size(); ; --i) {
952 assert(i != 0 && "Didn't find user!");
953 if (Uses[i-1] == User) {
954 Uses[i-1] = Uses.back();
963 // Define inline functions from the SDOperand class.
965 inline unsigned SDOperand::getOpcode() const {
966 return Val->getOpcode();
968 inline unsigned SDOperand::getNodeDepth() const {
969 return Val->getNodeDepth();
971 inline MVT::ValueType SDOperand::getValueType() const {
972 return Val->getValueType(ResNo);
974 inline unsigned SDOperand::getNumOperands() const {
975 return Val->getNumOperands();
977 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
978 return Val->getOperand(i);
980 inline bool SDOperand::isTargetOpcode() const {
981 return Val->isTargetOpcode();
983 inline unsigned SDOperand::getTargetOpcode() const {
984 return Val->getTargetOpcode();
986 inline bool SDOperand::hasOneUse() const {
987 return Val->hasNUsesOfValue(1, ResNo);
990 /// HandleSDNode - This class is used to form a handle around another node that
991 /// is persistant and is updated across invocations of replaceAllUsesWith on its
992 /// operand. This node should be directly created by end-users and not added to
993 /// the AllNodes list.
994 class HandleSDNode : public SDNode {
996 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
998 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
1001 SDOperand getValue() const { return getOperand(0); }
1004 class StringSDNode : public SDNode {
1007 friend class SelectionDAG;
1008 StringSDNode(const std::string &val)
1009 : SDNode(ISD::STRING, MVT::Other), Value(val) {
1012 const std::string &getValue() const { return Value; }
1013 static bool classof(const StringSDNode *) { return true; }
1014 static bool classof(const SDNode *N) {
1015 return N->getOpcode() == ISD::STRING;
1019 class ConstantSDNode : public SDNode {
1022 friend class SelectionDAG;
1023 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1024 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
1028 uint64_t getValue() const { return Value; }
1030 int64_t getSignExtended() const {
1031 unsigned Bits = MVT::getSizeInBits(getValueType(0));
1032 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1035 bool isNullValue() const { return Value == 0; }
1036 bool isAllOnesValue() const {
1037 int NumBits = MVT::getSizeInBits(getValueType(0));
1038 if (NumBits == 64) return Value+1 == 0;
1039 return Value == (1ULL << NumBits)-1;
1042 static bool classof(const ConstantSDNode *) { return true; }
1043 static bool classof(const SDNode *N) {
1044 return N->getOpcode() == ISD::Constant ||
1045 N->getOpcode() == ISD::TargetConstant;
1049 class ConstantFPSDNode : public SDNode {
1052 friend class SelectionDAG;
1053 ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1054 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1059 double getValue() const { return Value; }
1061 /// isExactlyValue - We don't rely on operator== working on double values, as
1062 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1063 /// As such, this method can be used to do an exact bit-for-bit comparison of
1064 /// two floating point values.
1065 bool isExactlyValue(double V) const;
1067 static bool classof(const ConstantFPSDNode *) { return true; }
1068 static bool classof(const SDNode *N) {
1069 return N->getOpcode() == ISD::ConstantFP ||
1070 N->getOpcode() == ISD::TargetConstantFP;
1074 class GlobalAddressSDNode : public SDNode {
1075 GlobalValue *TheGlobal;
1078 friend class SelectionDAG;
1079 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1081 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1083 TheGlobal = const_cast<GlobalValue*>(GA);
1087 GlobalValue *getGlobal() const { return TheGlobal; }
1088 int getOffset() const { return Offset; }
1090 static bool classof(const GlobalAddressSDNode *) { return true; }
1091 static bool classof(const SDNode *N) {
1092 return N->getOpcode() == ISD::GlobalAddress ||
1093 N->getOpcode() == ISD::TargetGlobalAddress;
1098 class FrameIndexSDNode : public SDNode {
1101 friend class SelectionDAG;
1102 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1103 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1106 int getIndex() const { return FI; }
1108 static bool classof(const FrameIndexSDNode *) { return true; }
1109 static bool classof(const SDNode *N) {
1110 return N->getOpcode() == ISD::FrameIndex ||
1111 N->getOpcode() == ISD::TargetFrameIndex;
1115 class ConstantPoolSDNode : public SDNode {
1120 friend class SelectionDAG;
1121 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1123 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1124 C(c), Offset(o), Alignment(0) {}
1125 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1127 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1128 C(c), Offset(o), Alignment(Align) {}
1131 Constant *get() const { return C; }
1132 int getOffset() const { return Offset; }
1134 // Return the alignment of this constant pool object, which is either 0 (for
1135 // default alignment) or log2 of the desired value.
1136 unsigned getAlignment() const { return Alignment; }
1138 static bool classof(const ConstantPoolSDNode *) { return true; }
1139 static bool classof(const SDNode *N) {
1140 return N->getOpcode() == ISD::ConstantPool ||
1141 N->getOpcode() == ISD::TargetConstantPool;
1145 class BasicBlockSDNode : public SDNode {
1146 MachineBasicBlock *MBB;
1148 friend class SelectionDAG;
1149 BasicBlockSDNode(MachineBasicBlock *mbb)
1150 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1153 MachineBasicBlock *getBasicBlock() const { return MBB; }
1155 static bool classof(const BasicBlockSDNode *) { return true; }
1156 static bool classof(const SDNode *N) {
1157 return N->getOpcode() == ISD::BasicBlock;
1161 class SrcValueSDNode : public SDNode {
1165 friend class SelectionDAG;
1166 SrcValueSDNode(const Value* v, int o)
1167 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1170 const Value *getValue() const { return V; }
1171 int getOffset() const { return offset; }
1173 static bool classof(const SrcValueSDNode *) { return true; }
1174 static bool classof(const SDNode *N) {
1175 return N->getOpcode() == ISD::SRCVALUE;
1180 class RegisterSDNode : public SDNode {
1183 friend class SelectionDAG;
1184 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1185 : SDNode(ISD::Register, VT), Reg(reg) {}
1188 unsigned getReg() const { return Reg; }
1190 static bool classof(const RegisterSDNode *) { return true; }
1191 static bool classof(const SDNode *N) {
1192 return N->getOpcode() == ISD::Register;
1196 class ExternalSymbolSDNode : public SDNode {
1199 friend class SelectionDAG;
1200 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1201 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1206 const char *getSymbol() const { return Symbol; }
1208 static bool classof(const ExternalSymbolSDNode *) { return true; }
1209 static bool classof(const SDNode *N) {
1210 return N->getOpcode() == ISD::ExternalSymbol ||
1211 N->getOpcode() == ISD::TargetExternalSymbol;
1215 class CondCodeSDNode : public SDNode {
1216 ISD::CondCode Condition;
1218 friend class SelectionDAG;
1219 CondCodeSDNode(ISD::CondCode Cond)
1220 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1224 ISD::CondCode get() const { return Condition; }
1226 static bool classof(const CondCodeSDNode *) { return true; }
1227 static bool classof(const SDNode *N) {
1228 return N->getOpcode() == ISD::CONDCODE;
1232 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1233 /// to parameterize some operations.
1234 class VTSDNode : public SDNode {
1235 MVT::ValueType ValueType;
1237 friend class SelectionDAG;
1238 VTSDNode(MVT::ValueType VT)
1239 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1242 MVT::ValueType getVT() const { return ValueType; }
1244 static bool classof(const VTSDNode *) { return true; }
1245 static bool classof(const SDNode *N) {
1246 return N->getOpcode() == ISD::VALUETYPE;
1251 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1255 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1257 bool operator==(const SDNodeIterator& x) const {
1258 return Operand == x.Operand;
1260 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1262 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1263 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1264 Operand = I.Operand;
1268 pointer operator*() const {
1269 return Node->getOperand(Operand).Val;
1271 pointer operator->() const { return operator*(); }
1273 SDNodeIterator& operator++() { // Preincrement
1277 SDNodeIterator operator++(int) { // Postincrement
1278 SDNodeIterator tmp = *this; ++*this; return tmp;
1281 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1282 static SDNodeIterator end (SDNode *N) {
1283 return SDNodeIterator(N, N->getNumOperands());
1286 unsigned getOperand() const { return Operand; }
1287 const SDNode *getNode() const { return Node; }
1290 template <> struct GraphTraits<SDNode*> {
1291 typedef SDNode NodeType;
1292 typedef SDNodeIterator ChildIteratorType;
1293 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1294 static inline ChildIteratorType child_begin(NodeType *N) {
1295 return SDNodeIterator::begin(N);
1297 static inline ChildIteratorType child_end(NodeType *N) {
1298 return SDNodeIterator::end(N);
1303 struct ilist_traits<SDNode> {
1304 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1305 static SDNode *getNext(const SDNode *N) { return N->Next; }
1307 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1308 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1310 static SDNode *createSentinel() {
1311 return new SDNode(ISD::EntryToken, MVT::Other);
1313 static void destroySentinel(SDNode *N) { delete N; }
1314 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1317 void addNodeToList(SDNode *NTy) {}
1318 void removeNodeFromList(SDNode *NTy) {}
1319 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1320 const ilist_iterator<SDNode> &X,
1321 const ilist_iterator<SDNode> &Y) {}
1324 } // end llvm namespace