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 /// RESULT{,OUTCHAIN} = INTRINSIC({INCHAIN,} INTRINSICID, arg1, arg2, ...)
84 /// This node represents a target intrinsic function. If the intrinsic
85 /// has side effects, the first operand is a chain pointer and the result
86 /// includes an output chain. After this input is the ID number of the
87 /// intrinsic, from the llvm::intrinsic namespace. The operands to the
91 // CopyToReg - This node has three operands: a chain, a register number to
92 // set to this value, and a value.
95 // CopyFromReg - This node indicates that the input value is a virtual or
96 // physical register that is defined outside of the scope of this
97 // SelectionDAG. The register is available from the RegSDNode object.
100 // UNDEF - An undefined node
103 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
104 // a Constant, which is required to be operand #1), element of the aggregate
105 // value specified as operand #0. This is only for use before legalization,
106 // for values that will be broken into multiple registers.
109 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
110 // two values of the same integer value type, this produces a value twice as
111 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
114 // MERGE_VALUES - This node takes multiple discrete operands and returns
115 // them all as its individual results. This nodes has exactly the same
116 // number of inputs and outputs, and is only valid before legalization.
117 // This node is useful for some pieces of the code generator that want to
118 // think about a single node with multiple results, not multiple nodes.
121 // Simple integer binary arithmetic operators.
122 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
124 // Carry-setting nodes for multiple precision addition and subtraction.
125 // These nodes take two operands of the same value type, and produce two
126 // results. The first result is the normal add or sub result, the second
127 // result is the carry flag result.
130 // Carry-using nodes for multiple precision addition and subtraction. These
131 // nodes take three operands: The first two are the normal lhs and rhs to
132 // the add or sub, and the third is the input carry flag. These nodes
133 // produce two results; the normal result of the add or sub, and the output
134 // carry flag. These nodes both read and write a carry flag to allow them
135 // to them to be chained together for add and sub of arbitrarily large
139 // Simple binary floating point operators.
140 FADD, FSUB, FMUL, FDIV, FREM,
142 // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y. NOTE: This
143 // DAG node does not require that X and Y have the same type, just that they
144 // are both floating point. X and the result must have the same type.
145 // FCOPYSIGN(f32, f64) is allowed.
148 /// VBUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,..., COUNT,TYPE) - Return a vector
149 /// with the specified, possibly variable, elements. The number of elements
150 /// is required to be a power of two.
153 /// BUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...) - Return a vector
154 /// with the specified, possibly variable, elements. The number of elements
155 /// is required to be a power of two.
158 /// VINSERT_VECTOR_ELT(VECTOR, VAL, IDX, COUNT,TYPE) - Given a vector
159 /// VECTOR, an element ELEMENT, and a (potentially variable) index IDX,
160 /// return an vector with the specified element of VECTOR replaced with VAL.
161 /// COUNT and TYPE specify the type of vector, as is standard for V* nodes.
164 /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR (a legal packed
165 /// type) with the element at IDX replaced with VAL.
168 /// VEXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
169 /// (an MVT::Vector value) identified by the (potentially variable) element
173 /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
174 /// (a legal packed type vector) identified by the (potentially variable)
175 /// element number IDX.
178 /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
179 /// type as VEC1/VEC2. SHUFFLEVEC is a BUILD_VECTOR of constant int values
180 /// (regardless of whether its datatype is legal or not) that indicate
181 /// which value each result element will get. The elements of VEC1/VEC2 are
182 /// enumerated in order. This is quite similar to the Altivec 'vperm'
183 /// instruction, except that the indices must be constants and are in terms
184 /// of the element size of VEC1/VEC2, not in terms of bytes.
187 /// X = VBIT_CONVERT(Y) and X = VBIT_CONVERT(Y, COUNT,TYPE) - This node
188 /// represents a conversion from or to an ISD::Vector type.
190 /// This is lowered to a BIT_CONVERT of the appropriate input/output types.
191 /// The input and output are required to have the same size and at least one
192 /// is required to be a vector (if neither is a vector, just use
195 /// If the result is a vector, this takes three operands (like any other
196 /// vector producer) which indicate the size and type of the vector result.
197 /// Otherwise it takes one input.
200 /// BINOP(LHS, RHS, COUNT,TYPE)
201 /// Simple abstract vector operators. Unlike the integer and floating point
202 /// binary operators, these nodes also take two additional operands:
203 /// a constant element count, and a value type node indicating the type of
204 /// the elements. The order is count, type, op0, op1. All vector opcodes,
205 /// including VLOAD and VConstant must currently have count and type as
206 /// their last two operands.
207 VADD, VSUB, VMUL, VSDIV, VUDIV,
210 /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
211 /// scalar value into the low element of the resultant vector type. The top
212 /// elements of the vector are undefined.
215 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
216 // an unsigned/signed value of type i[2*n], then return the top part.
219 // Bitwise operators - logical and, logical or, logical xor, shift left,
220 // shift right algebraic (shift in sign bits), shift right logical (shift in
221 // zeroes), rotate left, rotate right, and byteswap.
222 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
224 // Counting operators
227 // Select(COND, TRUEVAL, FALSEVAL)
230 // Select with condition operator - This selects between a true value and
231 // a false value (ops #2 and #3) based on the boolean result of comparing
232 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
233 // condition code in op #4, a CondCodeSDNode.
236 // SetCC operator - This evaluates to a boolean (i1) true value if the
237 // condition is true. The operands to this are the left and right operands
238 // to compare (ops #0, and #1) and the condition code to compare them with
239 // (op #2) as a CondCodeSDNode.
242 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
243 // integer shift operations, just like ADD/SUB_PARTS. The operation
245 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
246 SHL_PARTS, SRA_PARTS, SRL_PARTS,
248 // Conversion operators. These are all single input single output
249 // operations. For all of these, the result type must be strictly
250 // wider or narrower (depending on the operation) than the source
253 // SIGN_EXTEND - Used for integer types, replicating the sign bit
257 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
260 // ANY_EXTEND - Used for integer types. The high bits are undefined.
263 // TRUNCATE - Completely drop the high bits.
266 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
267 // depends on the first letter) to floating point.
271 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
272 // sign extend a small value in a large integer register (e.g. sign
273 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
274 // with the 7th bit). The size of the smaller type is indicated by the 1th
275 // operand, a ValueType node.
278 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
283 // FP_ROUND - Perform a rounding operation from the current
284 // precision down to the specified precision (currently always 64->32).
287 // FP_ROUND_INREG - This operator takes a floating point register, and
288 // rounds it to a floating point value. It then promotes it and returns it
289 // in a register of the same size. This operation effectively just discards
290 // excess precision. The type to round down to is specified by the 1th
291 // operation, a VTSDNode (currently always 64->32->64).
294 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
297 // BIT_CONVERT - Theis operator converts between integer and FP values, as
298 // if one was stored to memory as integer and the other was loaded from the
299 // same address (or equivalently for vector format conversions, etc). The
300 // source and result are required to have the same bit size (e.g.
301 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
302 // conversions, but that is a noop, deleted by getNode().
305 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
306 // absolute value, square root, sine and cosine operations.
307 FNEG, FABS, FSQRT, FSIN, FCOS,
309 // Other operators. LOAD and STORE have token chains as their first
310 // operand, then the same operands as an LLVM load/store instruction, then a
311 // SRCVALUE node that provides alias analysis information.
314 // Abstract vector version of LOAD. VLOAD has a constant element count as
315 // the first operand, followed by a value type node indicating the type of
316 // the elements, a token chain, a pointer operand, and a SRCVALUE node.
319 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
320 // memory and extend them to a larger value (e.g. load a byte into a word
321 // register). All three of these have four operands, a token chain, a
322 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
323 // indicating the type to load.
325 // SEXTLOAD loads the integer operand and sign extends it to a larger
326 // integer result type.
327 // ZEXTLOAD loads the integer operand and zero extends it to a larger
328 // integer result type.
329 // EXTLOAD is used for three things: floating point extending loads,
330 // integer extending loads [the top bits are undefined], and vector
331 // extending loads [load into low elt].
332 EXTLOAD, SEXTLOAD, ZEXTLOAD,
334 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
335 // value and stores it to memory in one operation. This can be used for
336 // either integer or floating point operands. The first four operands of
337 // this are the same as a standard store. The fifth is the ValueType to
338 // store it as (which will be smaller than the source value).
341 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
342 // to a specified boundary. The first operand is the token chain, the
343 // second is the number of bytes to allocate, and the third is the alignment
344 // boundary. The size is guaranteed to be a multiple of the stack
345 // alignment, and the alignment is guaranteed to be bigger than the stack
346 // alignment (if required) or 0 to get standard stack alignment.
349 // Control flow instructions. These all have token chains.
351 // BR - Unconditional branch. The first operand is the chain
352 // operand, the second is the MBB to branch to.
355 // BRCOND - Conditional branch. The first operand is the chain,
356 // the second is the condition, the third is the block to branch
357 // to if the condition is true.
360 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
361 // that the condition is represented as condition code, and two nodes to
362 // compare, rather than as a combined SetCC node. The operands in order are
363 // chain, cc, lhs, rhs, block to branch to if condition is true.
366 // RET - Return from function. The first operand is the chain,
367 // and any subsequent operands are the return values for the
368 // function. This operation can have variable number of operands.
371 // INLINEASM - Represents an inline asm block. This node always has two
372 // return values: a chain and a flag result. The inputs are as follows:
373 // Operand #0 : Input chain.
374 // Operand #1 : a ExternalSymbolSDNode with a pointer to the asm string.
375 // Operand #2n+2: A RegisterNode.
376 // Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
377 // Operand #last: Optional, an incoming flag.
380 // STACKSAVE - STACKSAVE has one operand, an input chain. It produces a
381 // value, the same type as the pointer type for the system, and an output
385 // STACKRESTORE has two operands, an input chain and a pointer to restore to
386 // it returns an output chain.
389 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
390 // correspond to the operands of the LLVM intrinsic functions. The only
391 // result is a token chain. The alignment argument is guaranteed to be a
397 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
398 // a call sequence, and carry arbitrary information that target might want
399 // to know. The first operand is a chain, the rest are specified by the
400 // target and not touched by the DAG optimizers.
401 CALLSEQ_START, // Beginning of a call sequence
402 CALLSEQ_END, // End of a call sequence
404 // VAARG - VAARG has three operands: an input chain, a pointer, and a
405 // SRCVALUE. It returns a pair of values: the vaarg value and a new chain.
408 // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
409 // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
413 // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
414 // pointer, and a SRCVALUE.
417 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
418 // locations with their value. This allows one use alias analysis
419 // information in the backend.
422 // PCMARKER - This corresponds to the pcmarker intrinsic.
425 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
426 // The only operand is a chain and a value and a chain are produced. The
427 // value is the contents of the architecture specific cycle counter like
428 // register (or other high accuracy low latency clock source)
431 // HANDLENODE node - Used as a handle for various purposes.
434 // LOCATION - This node is used to represent a source location for debug
435 // info. It takes token chain as input, then a line number, then a column
436 // number, then a filename, then a working dir. It produces a token chain
440 // DEBUG_LOC - This node is used to represent source line information
441 // embedded in the code. It takes a token chain as input, then a line
442 // number, then a column then a file id (provided by MachineDebugInfo.) It
443 // produces a token chain as output.
446 // DEBUG_LABEL - This node is used to mark a location in the code where a
447 // label should be generated for use by the debug information. It takes a
448 // token chain as input and then a unique id (provided by MachineDebugInfo.)
449 // It produces a token chain as output.
452 // BUILTIN_OP_END - This must be the last enum value in this list.
456 //===--------------------------------------------------------------------===//
457 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
458 /// below work out, when considering SETFALSE (something that never exists
459 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
460 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
461 /// to. If the "N" column is 1, the result of the comparison is undefined if
462 /// the input is a NAN.
464 /// All of these (except for the 'always folded ops') should be handled for
465 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
466 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
468 /// Note that these are laid out in a specific order to allow bit-twiddling
469 /// to transform conditions.
471 // Opcode N U L G E Intuitive operation
472 SETFALSE, // 0 0 0 0 Always false (always folded)
473 SETOEQ, // 0 0 0 1 True if ordered and equal
474 SETOGT, // 0 0 1 0 True if ordered and greater than
475 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
476 SETOLT, // 0 1 0 0 True if ordered and less than
477 SETOLE, // 0 1 0 1 True if ordered and less than or equal
478 SETONE, // 0 1 1 0 True if ordered and operands are unequal
479 SETO, // 0 1 1 1 True if ordered (no nans)
480 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
481 SETUEQ, // 1 0 0 1 True if unordered or equal
482 SETUGT, // 1 0 1 0 True if unordered or greater than
483 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
484 SETULT, // 1 1 0 0 True if unordered or less than
485 SETULE, // 1 1 0 1 True if unordered, less than, or equal
486 SETUNE, // 1 1 1 0 True if unordered or not equal
487 SETTRUE, // 1 1 1 1 Always true (always folded)
488 // Don't care operations: undefined if the input is a nan.
489 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
490 SETEQ, // 1 X 0 0 1 True if equal
491 SETGT, // 1 X 0 1 0 True if greater than
492 SETGE, // 1 X 0 1 1 True if greater than or equal
493 SETLT, // 1 X 1 0 0 True if less than
494 SETLE, // 1 X 1 0 1 True if less than or equal
495 SETNE, // 1 X 1 1 0 True if not equal
496 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
498 SETCC_INVALID // Marker value.
501 /// isSignedIntSetCC - Return true if this is a setcc instruction that
502 /// performs a signed comparison when used with integer operands.
503 inline bool isSignedIntSetCC(CondCode Code) {
504 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
507 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
508 /// performs an unsigned comparison when used with integer operands.
509 inline bool isUnsignedIntSetCC(CondCode Code) {
510 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
513 /// isTrueWhenEqual - Return true if the specified condition returns true if
514 /// the two operands to the condition are equal. Note that if one of the two
515 /// operands is a NaN, this value is meaningless.
516 inline bool isTrueWhenEqual(CondCode Cond) {
517 return ((int)Cond & 1) != 0;
520 /// getUnorderedFlavor - This function returns 0 if the condition is always
521 /// false if an operand is a NaN, 1 if the condition is always true if the
522 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
524 inline unsigned getUnorderedFlavor(CondCode Cond) {
525 return ((int)Cond >> 3) & 3;
528 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
529 /// 'op' is a valid SetCC operation.
530 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
532 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
533 /// when given the operation for (X op Y).
534 CondCode getSetCCSwappedOperands(CondCode Operation);
536 /// getSetCCOrOperation - Return the result of a logical OR between different
537 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
538 /// function returns SETCC_INVALID if it is not possible to represent the
539 /// resultant comparison.
540 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
542 /// getSetCCAndOperation - Return the result of a logical AND between
543 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
544 /// function returns SETCC_INVALID if it is not possible to represent the
545 /// resultant comparison.
546 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
547 } // end llvm::ISD namespace
550 //===----------------------------------------------------------------------===//
551 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
552 /// values as the result of a computation. Many nodes return multiple values,
553 /// from loads (which define a token and a return value) to ADDC (which returns
554 /// a result and a carry value), to calls (which may return an arbitrary number
557 /// As such, each use of a SelectionDAG computation must indicate the node that
558 /// computes it as well as which return value to use from that node. This pair
559 /// of information is represented with the SDOperand value type.
563 SDNode *Val; // The node defining the value we are using.
564 unsigned ResNo; // Which return value of the node we are using.
566 SDOperand() : Val(0) {}
567 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
569 bool operator==(const SDOperand &O) const {
570 return Val == O.Val && ResNo == O.ResNo;
572 bool operator!=(const SDOperand &O) const {
573 return !operator==(O);
575 bool operator<(const SDOperand &O) const {
576 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
579 SDOperand getValue(unsigned R) const {
580 return SDOperand(Val, R);
583 // isOperand - Return true if this node is an operand of N.
584 bool isOperand(SDNode *N) const;
586 /// getValueType - Return the ValueType of the referenced return value.
588 inline MVT::ValueType getValueType() const;
590 // Forwarding methods - These forward to the corresponding methods in SDNode.
591 inline unsigned getOpcode() const;
592 inline unsigned getNodeDepth() const;
593 inline unsigned getNumOperands() const;
594 inline const SDOperand &getOperand(unsigned i) const;
595 inline bool isTargetOpcode() const;
596 inline unsigned getTargetOpcode() const;
598 /// hasOneUse - Return true if there is exactly one operation using this
599 /// result value of the defining operator.
600 inline bool hasOneUse() const;
604 /// simplify_type specializations - Allow casting operators to work directly on
605 /// SDOperands as if they were SDNode*'s.
606 template<> struct simplify_type<SDOperand> {
607 typedef SDNode* SimpleType;
608 static SimpleType getSimplifiedValue(const SDOperand &Val) {
609 return static_cast<SimpleType>(Val.Val);
612 template<> struct simplify_type<const SDOperand> {
613 typedef SDNode* SimpleType;
614 static SimpleType getSimplifiedValue(const SDOperand &Val) {
615 return static_cast<SimpleType>(Val.Val);
620 /// SDNode - Represents one node in the SelectionDAG.
623 /// NodeType - The operation that this node performs.
625 unsigned short NodeType;
627 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
628 /// means that leaves have a depth of 1, things that use only leaves have a
630 unsigned short NodeDepth;
632 /// OperandList - The values that are used by this operation.
634 SDOperand *OperandList;
636 /// ValueList - The types of the values this node defines. SDNode's may
637 /// define multiple values simultaneously.
638 MVT::ValueType *ValueList;
640 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
641 unsigned short NumOperands, NumValues;
643 /// Prev/Next pointers - These pointers form the linked list of of the
644 /// AllNodes list in the current DAG.
646 friend struct ilist_traits<SDNode>;
648 /// Uses - These are all of the SDNode's that use a value produced by this
650 std::vector<SDNode*> Uses;
653 assert(NumOperands == 0 && "Operand list not cleared before deletion");
656 //===--------------------------------------------------------------------===//
659 unsigned getOpcode() const { return NodeType; }
660 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
661 unsigned getTargetOpcode() const {
662 assert(isTargetOpcode() && "Not a target opcode!");
663 return NodeType - ISD::BUILTIN_OP_END;
666 size_t use_size() const { return Uses.size(); }
667 bool use_empty() const { return Uses.empty(); }
668 bool hasOneUse() const { return Uses.size() == 1; }
670 /// getNodeDepth - Return the distance from this node to the leaves in the
671 /// graph. The leaves have a depth of 1.
672 unsigned getNodeDepth() const { return NodeDepth; }
674 typedef std::vector<SDNode*>::const_iterator use_iterator;
675 use_iterator use_begin() const { return Uses.begin(); }
676 use_iterator use_end() const { return Uses.end(); }
678 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
679 /// indicated value. This method ignores uses of other values defined by this
681 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
683 // isOnlyUse - Return true if this node is the only use of N.
684 bool isOnlyUse(SDNode *N) const;
686 // isOperand - Return true if this node is an operand of N.
687 bool isOperand(SDNode *N) const;
689 /// getNumOperands - Return the number of values used by this operation.
691 unsigned getNumOperands() const { return NumOperands; }
693 const SDOperand &getOperand(unsigned Num) const {
694 assert(Num < NumOperands && "Invalid child # of SDNode!");
695 return OperandList[Num];
697 typedef const SDOperand* op_iterator;
698 op_iterator op_begin() const { return OperandList; }
699 op_iterator op_end() const { return OperandList+NumOperands; }
702 /// getNumValues - Return the number of values defined/returned by this
705 unsigned getNumValues() const { return NumValues; }
707 /// getValueType - Return the type of a specified result.
709 MVT::ValueType getValueType(unsigned ResNo) const {
710 assert(ResNo < NumValues && "Illegal result number!");
711 return ValueList[ResNo];
714 typedef const MVT::ValueType* value_iterator;
715 value_iterator value_begin() const { return ValueList; }
716 value_iterator value_end() const { return ValueList+NumValues; }
718 /// getOperationName - Return the opcode of this operation for printing.
720 const char* getOperationName(const SelectionDAG *G = 0) const;
722 void dump(const SelectionDAG *G) const;
724 static bool classof(const SDNode *) { return true; }
727 friend class SelectionDAG;
729 /// getValueTypeList - Return a pointer to the specified value type.
731 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
733 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
734 OperandList = 0; NumOperands = 0;
735 ValueList = getValueTypeList(VT);
739 SDNode(unsigned NT, SDOperand Op)
740 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
741 OperandList = new SDOperand[1];
744 Op.Val->Uses.push_back(this);
749 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
751 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
752 NodeDepth = N1.Val->getNodeDepth()+1;
754 NodeDepth = N2.Val->getNodeDepth()+1;
755 OperandList = new SDOperand[2];
759 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
764 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
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();
773 OperandList = new SDOperand[3];
779 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
780 N3.Val->Uses.push_back(this);
785 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
787 unsigned ND = N1.Val->getNodeDepth();
788 if (ND < N2.Val->getNodeDepth())
789 ND = N2.Val->getNodeDepth();
790 if (ND < N3.Val->getNodeDepth())
791 ND = N3.Val->getNodeDepth();
792 if (ND < N4.Val->getNodeDepth())
793 ND = N4.Val->getNodeDepth();
796 OperandList = new SDOperand[4];
803 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
804 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
809 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
810 NumOperands = Nodes.size();
811 OperandList = new SDOperand[NumOperands];
814 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
815 OperandList[i] = Nodes[i];
816 SDNode *N = OperandList[i].Val;
817 N->Uses.push_back(this);
818 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
826 /// MorphNodeTo - This clears the return value and operands list, and sets the
827 /// opcode of the node to the specified value. This should only be used by
828 /// the SelectionDAG class.
829 void MorphNodeTo(unsigned Opc) {
834 // Clear the operands list, updating used nodes to remove this from their
836 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
837 I->Val->removeUser(this);
838 delete [] OperandList;
843 void setValueTypes(MVT::ValueType VT) {
844 assert(NumValues == 0 && "Should not have values yet!");
845 ValueList = getValueTypeList(VT);
848 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
849 assert(NumValues == 0 && "Should not have values yet!");
854 void setOperands(SDOperand Op0) {
855 assert(NumOperands == 0 && "Should not have operands yet!");
856 OperandList = new SDOperand[1];
857 OperandList[0] = Op0;
859 Op0.Val->Uses.push_back(this);
861 void setOperands(SDOperand Op0, SDOperand Op1) {
862 assert(NumOperands == 0 && "Should not have operands yet!");
863 OperandList = new SDOperand[2];
864 OperandList[0] = Op0;
865 OperandList[1] = Op1;
867 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
869 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
870 assert(NumOperands == 0 && "Should not have operands yet!");
871 OperandList = new SDOperand[3];
872 OperandList[0] = Op0;
873 OperandList[1] = Op1;
874 OperandList[2] = Op2;
876 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
877 Op2.Val->Uses.push_back(this);
879 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
880 assert(NumOperands == 0 && "Should not have operands yet!");
881 OperandList = new SDOperand[4];
882 OperandList[0] = Op0;
883 OperandList[1] = Op1;
884 OperandList[2] = Op2;
885 OperandList[3] = Op3;
887 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
888 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
890 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
892 assert(NumOperands == 0 && "Should not have operands yet!");
893 OperandList = new SDOperand[5];
894 OperandList[0] = Op0;
895 OperandList[1] = Op1;
896 OperandList[2] = Op2;
897 OperandList[3] = Op3;
898 OperandList[4] = Op4;
900 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
901 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
902 Op4.Val->Uses.push_back(this);
904 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
905 SDOperand Op4, SDOperand Op5) {
906 assert(NumOperands == 0 && "Should not have operands yet!");
907 OperandList = new SDOperand[6];
908 OperandList[0] = Op0;
909 OperandList[1] = Op1;
910 OperandList[2] = Op2;
911 OperandList[3] = Op3;
912 OperandList[4] = Op4;
913 OperandList[5] = Op5;
915 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
916 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
917 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
919 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
920 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
921 assert(NumOperands == 0 && "Should not have operands yet!");
922 OperandList = new SDOperand[7];
923 OperandList[0] = Op0;
924 OperandList[1] = Op1;
925 OperandList[2] = Op2;
926 OperandList[3] = Op3;
927 OperandList[4] = Op4;
928 OperandList[5] = Op5;
929 OperandList[6] = Op6;
931 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
932 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
933 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
934 Op6.Val->Uses.push_back(this);
936 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
937 SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
938 assert(NumOperands == 0 && "Should not have operands yet!");
939 OperandList = new SDOperand[8];
940 OperandList[0] = Op0;
941 OperandList[1] = Op1;
942 OperandList[2] = Op2;
943 OperandList[3] = Op3;
944 OperandList[4] = Op4;
945 OperandList[5] = Op5;
946 OperandList[6] = Op6;
947 OperandList[7] = Op7;
949 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
950 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
951 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
952 Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
955 void addUser(SDNode *User) {
956 Uses.push_back(User);
958 void removeUser(SDNode *User) {
959 // Remove this user from the operand's use list.
960 for (unsigned i = Uses.size(); ; --i) {
961 assert(i != 0 && "Didn't find user!");
962 if (Uses[i-1] == User) {
963 Uses[i-1] = Uses.back();
972 // Define inline functions from the SDOperand class.
974 inline unsigned SDOperand::getOpcode() const {
975 return Val->getOpcode();
977 inline unsigned SDOperand::getNodeDepth() const {
978 return Val->getNodeDepth();
980 inline MVT::ValueType SDOperand::getValueType() const {
981 return Val->getValueType(ResNo);
983 inline unsigned SDOperand::getNumOperands() const {
984 return Val->getNumOperands();
986 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
987 return Val->getOperand(i);
989 inline bool SDOperand::isTargetOpcode() const {
990 return Val->isTargetOpcode();
992 inline unsigned SDOperand::getTargetOpcode() const {
993 return Val->getTargetOpcode();
995 inline bool SDOperand::hasOneUse() const {
996 return Val->hasNUsesOfValue(1, ResNo);
999 /// HandleSDNode - This class is used to form a handle around another node that
1000 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1001 /// operand. This node should be directly created by end-users and not added to
1002 /// the AllNodes list.
1003 class HandleSDNode : public SDNode {
1005 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
1007 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
1010 SDOperand getValue() const { return getOperand(0); }
1013 class StringSDNode : public SDNode {
1016 friend class SelectionDAG;
1017 StringSDNode(const std::string &val)
1018 : SDNode(ISD::STRING, MVT::Other), Value(val) {
1021 const std::string &getValue() const { return Value; }
1022 static bool classof(const StringSDNode *) { return true; }
1023 static bool classof(const SDNode *N) {
1024 return N->getOpcode() == ISD::STRING;
1028 class ConstantSDNode : public SDNode {
1031 friend class SelectionDAG;
1032 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1033 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
1037 uint64_t getValue() const { return Value; }
1039 int64_t getSignExtended() const {
1040 unsigned Bits = MVT::getSizeInBits(getValueType(0));
1041 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1044 bool isNullValue() const { return Value == 0; }
1045 bool isAllOnesValue() const {
1046 int NumBits = MVT::getSizeInBits(getValueType(0));
1047 if (NumBits == 64) return Value+1 == 0;
1048 return Value == (1ULL << NumBits)-1;
1051 static bool classof(const ConstantSDNode *) { return true; }
1052 static bool classof(const SDNode *N) {
1053 return N->getOpcode() == ISD::Constant ||
1054 N->getOpcode() == ISD::TargetConstant;
1058 class ConstantFPSDNode : public SDNode {
1061 friend class SelectionDAG;
1062 ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1063 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1068 double getValue() const { return Value; }
1070 /// isExactlyValue - We don't rely on operator== working on double values, as
1071 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1072 /// As such, this method can be used to do an exact bit-for-bit comparison of
1073 /// two floating point values.
1074 bool isExactlyValue(double V) const;
1076 static bool classof(const ConstantFPSDNode *) { return true; }
1077 static bool classof(const SDNode *N) {
1078 return N->getOpcode() == ISD::ConstantFP ||
1079 N->getOpcode() == ISD::TargetConstantFP;
1083 class GlobalAddressSDNode : public SDNode {
1084 GlobalValue *TheGlobal;
1087 friend class SelectionDAG;
1088 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1090 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1092 TheGlobal = const_cast<GlobalValue*>(GA);
1096 GlobalValue *getGlobal() const { return TheGlobal; }
1097 int getOffset() const { return Offset; }
1099 static bool classof(const GlobalAddressSDNode *) { return true; }
1100 static bool classof(const SDNode *N) {
1101 return N->getOpcode() == ISD::GlobalAddress ||
1102 N->getOpcode() == ISD::TargetGlobalAddress;
1107 class FrameIndexSDNode : public SDNode {
1110 friend class SelectionDAG;
1111 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1112 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1115 int getIndex() const { return FI; }
1117 static bool classof(const FrameIndexSDNode *) { return true; }
1118 static bool classof(const SDNode *N) {
1119 return N->getOpcode() == ISD::FrameIndex ||
1120 N->getOpcode() == ISD::TargetFrameIndex;
1124 class ConstantPoolSDNode : public SDNode {
1129 friend class SelectionDAG;
1130 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1132 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1133 C(c), Offset(o), Alignment(0) {}
1134 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1136 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1137 C(c), Offset(o), Alignment(Align) {}
1140 Constant *get() const { return C; }
1141 int getOffset() const { return Offset; }
1143 // Return the alignment of this constant pool object, which is either 0 (for
1144 // default alignment) or log2 of the desired value.
1145 unsigned getAlignment() const { return Alignment; }
1147 static bool classof(const ConstantPoolSDNode *) { return true; }
1148 static bool classof(const SDNode *N) {
1149 return N->getOpcode() == ISD::ConstantPool ||
1150 N->getOpcode() == ISD::TargetConstantPool;
1154 class BasicBlockSDNode : public SDNode {
1155 MachineBasicBlock *MBB;
1157 friend class SelectionDAG;
1158 BasicBlockSDNode(MachineBasicBlock *mbb)
1159 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1162 MachineBasicBlock *getBasicBlock() const { return MBB; }
1164 static bool classof(const BasicBlockSDNode *) { return true; }
1165 static bool classof(const SDNode *N) {
1166 return N->getOpcode() == ISD::BasicBlock;
1170 class SrcValueSDNode : public SDNode {
1174 friend class SelectionDAG;
1175 SrcValueSDNode(const Value* v, int o)
1176 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1179 const Value *getValue() const { return V; }
1180 int getOffset() const { return offset; }
1182 static bool classof(const SrcValueSDNode *) { return true; }
1183 static bool classof(const SDNode *N) {
1184 return N->getOpcode() == ISD::SRCVALUE;
1189 class RegisterSDNode : public SDNode {
1192 friend class SelectionDAG;
1193 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1194 : SDNode(ISD::Register, VT), Reg(reg) {}
1197 unsigned getReg() const { return Reg; }
1199 static bool classof(const RegisterSDNode *) { return true; }
1200 static bool classof(const SDNode *N) {
1201 return N->getOpcode() == ISD::Register;
1205 class ExternalSymbolSDNode : public SDNode {
1208 friend class SelectionDAG;
1209 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1210 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1215 const char *getSymbol() const { return Symbol; }
1217 static bool classof(const ExternalSymbolSDNode *) { return true; }
1218 static bool classof(const SDNode *N) {
1219 return N->getOpcode() == ISD::ExternalSymbol ||
1220 N->getOpcode() == ISD::TargetExternalSymbol;
1224 class CondCodeSDNode : public SDNode {
1225 ISD::CondCode Condition;
1227 friend class SelectionDAG;
1228 CondCodeSDNode(ISD::CondCode Cond)
1229 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1233 ISD::CondCode get() const { return Condition; }
1235 static bool classof(const CondCodeSDNode *) { return true; }
1236 static bool classof(const SDNode *N) {
1237 return N->getOpcode() == ISD::CONDCODE;
1241 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1242 /// to parameterize some operations.
1243 class VTSDNode : public SDNode {
1244 MVT::ValueType ValueType;
1246 friend class SelectionDAG;
1247 VTSDNode(MVT::ValueType VT)
1248 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1251 MVT::ValueType getVT() const { return ValueType; }
1253 static bool classof(const VTSDNode *) { return true; }
1254 static bool classof(const SDNode *N) {
1255 return N->getOpcode() == ISD::VALUETYPE;
1260 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1264 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1266 bool operator==(const SDNodeIterator& x) const {
1267 return Operand == x.Operand;
1269 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1271 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1272 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1273 Operand = I.Operand;
1277 pointer operator*() const {
1278 return Node->getOperand(Operand).Val;
1280 pointer operator->() const { return operator*(); }
1282 SDNodeIterator& operator++() { // Preincrement
1286 SDNodeIterator operator++(int) { // Postincrement
1287 SDNodeIterator tmp = *this; ++*this; return tmp;
1290 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1291 static SDNodeIterator end (SDNode *N) {
1292 return SDNodeIterator(N, N->getNumOperands());
1295 unsigned getOperand() const { return Operand; }
1296 const SDNode *getNode() const { return Node; }
1299 template <> struct GraphTraits<SDNode*> {
1300 typedef SDNode NodeType;
1301 typedef SDNodeIterator ChildIteratorType;
1302 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1303 static inline ChildIteratorType child_begin(NodeType *N) {
1304 return SDNodeIterator::begin(N);
1306 static inline ChildIteratorType child_end(NodeType *N) {
1307 return SDNodeIterator::end(N);
1312 struct ilist_traits<SDNode> {
1313 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1314 static SDNode *getNext(const SDNode *N) { return N->Next; }
1316 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1317 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1319 static SDNode *createSentinel() {
1320 return new SDNode(ISD::EntryToken, MVT::Other);
1322 static void destroySentinel(SDNode *N) { delete N; }
1323 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1326 void addNodeToList(SDNode *NTy) {}
1327 void removeNodeFromList(SDNode *NTy) {}
1328 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1329 const ilist_iterator<SDNode> &X,
1330 const ilist_iterator<SDNode> &Y) {}
1333 } // end llvm namespace