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 = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...)
84 /// This node represents a target intrinsic function with no side effects.
85 /// The first operand is the ID number of the intrinsic from the
86 /// llvm::Intrinsic namespace. The operands to the intrinsic follow. The
87 /// node has returns the result of the intrinsic.
90 /// RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...)
91 /// This node represents a target intrinsic function with side effects that
92 /// returns a result. The first operand is a chain pointer. The second is
93 /// the ID number of the intrinsic from the llvm::Intrinsic namespace. The
94 /// operands to the intrinsic follow. The node has two results, the result
95 /// of the intrinsic and an output chain.
98 /// OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...)
99 /// This node represents a target intrinsic function with side effects that
100 /// does not return a result. The first operand is a chain pointer. The
101 /// second is the ID number of the intrinsic from the llvm::Intrinsic
102 /// namespace. The operands to the intrinsic follow.
105 // CopyToReg - This node has three operands: a chain, a register number to
106 // set to this value, and a value.
109 // CopyFromReg - This node indicates that the input value is a virtual or
110 // physical register that is defined outside of the scope of this
111 // SelectionDAG. The register is available from the RegSDNode object.
114 // UNDEF - An undefined node
117 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
118 // a Constant, which is required to be operand #1), element of the aggregate
119 // value specified as operand #0. This is only for use before legalization,
120 // for values that will be broken into multiple registers.
123 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
124 // two values of the same integer value type, this produces a value twice as
125 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
128 // MERGE_VALUES - This node takes multiple discrete operands and returns
129 // them all as its individual results. This nodes has exactly the same
130 // number of inputs and outputs, and is only valid before legalization.
131 // This node is useful for some pieces of the code generator that want to
132 // think about a single node with multiple results, not multiple nodes.
135 // Simple integer binary arithmetic operators.
136 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
138 // Carry-setting nodes for multiple precision addition and subtraction.
139 // These nodes take two operands of the same value type, and produce two
140 // results. The first result is the normal add or sub result, the second
141 // result is the carry flag result.
144 // Carry-using nodes for multiple precision addition and subtraction. These
145 // nodes take three operands: The first two are the normal lhs and rhs to
146 // the add or sub, and the third is the input carry flag. These nodes
147 // produce two results; the normal result of the add or sub, and the output
148 // carry flag. These nodes both read and write a carry flag to allow them
149 // to them to be chained together for add and sub of arbitrarily large
153 // Simple binary floating point operators.
154 FADD, FSUB, FMUL, FDIV, FREM,
156 // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y. NOTE: This
157 // DAG node does not require that X and Y have the same type, just that they
158 // are both floating point. X and the result must have the same type.
159 // FCOPYSIGN(f32, f64) is allowed.
162 /// VBUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,..., COUNT,TYPE) - Return a vector
163 /// with the specified, possibly variable, elements. The number of elements
164 /// is required to be a power of two.
167 /// BUILD_VECTOR(ELT1, ELT2, ELT3, ELT4,...) - Return a vector
168 /// with the specified, possibly variable, elements. The number of elements
169 /// is required to be a power of two.
172 /// VINSERT_VECTOR_ELT(VECTOR, VAL, IDX, COUNT,TYPE) - Given a vector
173 /// VECTOR, an element ELEMENT, and a (potentially variable) index IDX,
174 /// return an vector with the specified element of VECTOR replaced with VAL.
175 /// COUNT and TYPE specify the type of vector, as is standard for V* nodes.
178 /// INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR (a legal packed
179 /// type) with the element at IDX replaced with VAL.
182 /// VEXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
183 /// (an MVT::Vector value) identified by the (potentially variable) element
187 /// EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR
188 /// (a legal packed type vector) identified by the (potentially variable)
189 /// element number IDX.
192 /// VECTOR_SHUFFLE(VEC1, VEC2, SHUFFLEVEC) - Returns a vector, of the same
193 /// type as VEC1/VEC2. SHUFFLEVEC is a BUILD_VECTOR of constant int values
194 /// (regardless of whether its datatype is legal or not) that indicate
195 /// which value each result element will get. The elements of VEC1/VEC2 are
196 /// enumerated in order. This is quite similar to the Altivec 'vperm'
197 /// instruction, except that the indices must be constants and are in terms
198 /// of the element size of VEC1/VEC2, not in terms of bytes.
201 /// X = VBIT_CONVERT(Y) and X = VBIT_CONVERT(Y, COUNT,TYPE) - This node
202 /// represents a conversion from or to an ISD::Vector type.
204 /// This is lowered to a BIT_CONVERT of the appropriate input/output types.
205 /// The input and output are required to have the same size and at least one
206 /// is required to be a vector (if neither is a vector, just use
209 /// If the result is a vector, this takes three operands (like any other
210 /// vector producer) which indicate the size and type of the vector result.
211 /// Otherwise it takes one input.
214 /// BINOP(LHS, RHS, COUNT,TYPE)
215 /// Simple abstract vector operators. Unlike the integer and floating point
216 /// binary operators, these nodes also take two additional operands:
217 /// a constant element count, and a value type node indicating the type of
218 /// the elements. The order is count, type, op0, op1. All vector opcodes,
219 /// including VLOAD and VConstant must currently have count and type as
220 /// their last two operands.
221 VADD, VSUB, VMUL, VSDIV, VUDIV,
224 /// SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a
225 /// scalar value into the low element of the resultant vector type. The top
226 /// elements of the vector are undefined.
229 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
230 // an unsigned/signed value of type i[2*n], then return the top part.
233 // Bitwise operators - logical and, logical or, logical xor, shift left,
234 // shift right algebraic (shift in sign bits), shift right logical (shift in
235 // zeroes), rotate left, rotate right, and byteswap.
236 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
238 // Counting operators
241 // Select(COND, TRUEVAL, FALSEVAL)
244 // Select with condition operator - This selects between a true value and
245 // a false value (ops #2 and #3) based on the boolean result of comparing
246 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
247 // condition code in op #4, a CondCodeSDNode.
250 // SetCC operator - This evaluates to a boolean (i1) true value if the
251 // condition is true. The operands to this are the left and right operands
252 // to compare (ops #0, and #1) and the condition code to compare them with
253 // (op #2) as a CondCodeSDNode.
256 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
257 // integer shift operations, just like ADD/SUB_PARTS. The operation
259 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
260 SHL_PARTS, SRA_PARTS, SRL_PARTS,
262 // Conversion operators. These are all single input single output
263 // operations. For all of these, the result type must be strictly
264 // wider or narrower (depending on the operation) than the source
267 // SIGN_EXTEND - Used for integer types, replicating the sign bit
271 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
274 // ANY_EXTEND - Used for integer types. The high bits are undefined.
277 // TRUNCATE - Completely drop the high bits.
280 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
281 // depends on the first letter) to floating point.
285 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
286 // sign extend a small value in a large integer register (e.g. sign
287 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
288 // with the 7th bit). The size of the smaller type is indicated by the 1th
289 // operand, a ValueType node.
292 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
297 // FP_ROUND - Perform a rounding operation from the current
298 // precision down to the specified precision (currently always 64->32).
301 // FP_ROUND_INREG - This operator takes a floating point register, and
302 // rounds it to a floating point value. It then promotes it and returns it
303 // in a register of the same size. This operation effectively just discards
304 // excess precision. The type to round down to is specified by the 1th
305 // operation, a VTSDNode (currently always 64->32->64).
308 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
311 // BIT_CONVERT - Theis operator converts between integer and FP values, as
312 // if one was stored to memory as integer and the other was loaded from the
313 // same address (or equivalently for vector format conversions, etc). The
314 // source and result are required to have the same bit size (e.g.
315 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
316 // conversions, but that is a noop, deleted by getNode().
319 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
320 // absolute value, square root, sine and cosine operations.
321 FNEG, FABS, FSQRT, FSIN, FCOS,
323 // Other operators. LOAD and STORE have token chains as their first
324 // operand, then the same operands as an LLVM load/store instruction, then a
325 // SRCVALUE node that provides alias analysis information.
328 // Abstract vector version of LOAD. VLOAD has a constant element count as
329 // the first operand, followed by a value type node indicating the type of
330 // the elements, a token chain, a pointer operand, and a SRCVALUE node.
333 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
334 // memory and extend them to a larger value (e.g. load a byte into a word
335 // register). All three of these have four operands, a token chain, a
336 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
337 // indicating the type to load.
339 // SEXTLOAD loads the integer operand and sign extends it to a larger
340 // integer result type.
341 // ZEXTLOAD loads the integer operand and zero extends it to a larger
342 // integer result type.
343 // EXTLOAD is used for three things: floating point extending loads,
344 // integer extending loads [the top bits are undefined], and vector
345 // extending loads [load into low elt].
346 EXTLOAD, SEXTLOAD, ZEXTLOAD,
348 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
349 // value and stores it to memory in one operation. This can be used for
350 // either integer or floating point operands. The first four operands of
351 // this are the same as a standard store. The fifth is the ValueType to
352 // store it as (which will be smaller than the source value).
355 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
356 // to a specified boundary. The first operand is the token chain, the
357 // second is the number of bytes to allocate, and the third is the alignment
358 // boundary. The size is guaranteed to be a multiple of the stack
359 // alignment, and the alignment is guaranteed to be bigger than the stack
360 // alignment (if required) or 0 to get standard stack alignment.
363 // Control flow instructions. These all have token chains.
365 // BR - Unconditional branch. The first operand is the chain
366 // operand, the second is the MBB to branch to.
369 // BRCOND - Conditional branch. The first operand is the chain,
370 // the second is the condition, the third is the block to branch
371 // to if the condition is true.
374 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
375 // that the condition is represented as condition code, and two nodes to
376 // compare, rather than as a combined SetCC node. The operands in order are
377 // chain, cc, lhs, rhs, block to branch to if condition is true.
380 // RET - Return from function. The first operand is the chain,
381 // and any subsequent operands are the return values for the
382 // function. This operation can have variable number of operands.
385 // INLINEASM - Represents an inline asm block. This node always has two
386 // return values: a chain and a flag result. The inputs are as follows:
387 // Operand #0 : Input chain.
388 // Operand #1 : a ExternalSymbolSDNode with a pointer to the asm string.
389 // Operand #2n+2: A RegisterNode.
390 // Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
391 // Operand #last: Optional, an incoming flag.
394 // STACKSAVE - STACKSAVE has one operand, an input chain. It produces a
395 // value, the same type as the pointer type for the system, and an output
399 // STACKRESTORE has two operands, an input chain and a pointer to restore to
400 // it returns an output chain.
403 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
404 // correspond to the operands of the LLVM intrinsic functions. The only
405 // result is a token chain. The alignment argument is guaranteed to be a
411 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
412 // a call sequence, and carry arbitrary information that target might want
413 // to know. The first operand is a chain, the rest are specified by the
414 // target and not touched by the DAG optimizers.
415 CALLSEQ_START, // Beginning of a call sequence
416 CALLSEQ_END, // End of a call sequence
418 // VAARG - VAARG has three operands: an input chain, a pointer, and a
419 // SRCVALUE. It returns a pair of values: the vaarg value and a new chain.
422 // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
423 // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
427 // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
428 // pointer, and a SRCVALUE.
431 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
432 // locations with their value. This allows one use alias analysis
433 // information in the backend.
436 // PCMARKER - This corresponds to the pcmarker intrinsic.
439 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
440 // The only operand is a chain and a value and a chain are produced. The
441 // value is the contents of the architecture specific cycle counter like
442 // register (or other high accuracy low latency clock source)
445 // HANDLENODE node - Used as a handle for various purposes.
448 // LOCATION - This node is used to represent a source location for debug
449 // info. It takes token chain as input, then a line number, then a column
450 // number, then a filename, then a working dir. It produces a token chain
454 // DEBUG_LOC - This node is used to represent source line information
455 // embedded in the code. It takes a token chain as input, then a line
456 // number, then a column then a file id (provided by MachineDebugInfo.) It
457 // produces a token chain as output.
460 // DEBUG_LABEL - This node is used to mark a location in the code where a
461 // label should be generated for use by the debug information. It takes a
462 // token chain as input and then a unique id (provided by MachineDebugInfo.)
463 // It produces a token chain as output.
466 // BUILTIN_OP_END - This must be the last enum value in this list.
472 /// isBuildVectorAllOnes - Return true if the specified node is a
473 /// BUILD_VECTOR where all of the elements are ~0 or undef.
474 bool isBuildVectorAllOnes(const SDNode *N);
476 /// isBuildVectorAllZeros - Return true if the specified node is a
477 /// BUILD_VECTOR where all of the elements are 0 or undef.
478 bool isBuildVectorAllZeros(const SDNode *N);
480 //===--------------------------------------------------------------------===//
481 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
482 /// below work out, when considering SETFALSE (something that never exists
483 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
484 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
485 /// to. If the "N" column is 1, the result of the comparison is undefined if
486 /// the input is a NAN.
488 /// All of these (except for the 'always folded ops') should be handled for
489 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
490 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
492 /// Note that these are laid out in a specific order to allow bit-twiddling
493 /// to transform conditions.
495 // Opcode N U L G E Intuitive operation
496 SETFALSE, // 0 0 0 0 Always false (always folded)
497 SETOEQ, // 0 0 0 1 True if ordered and equal
498 SETOGT, // 0 0 1 0 True if ordered and greater than
499 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
500 SETOLT, // 0 1 0 0 True if ordered and less than
501 SETOLE, // 0 1 0 1 True if ordered and less than or equal
502 SETONE, // 0 1 1 0 True if ordered and operands are unequal
503 SETO, // 0 1 1 1 True if ordered (no nans)
504 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
505 SETUEQ, // 1 0 0 1 True if unordered or equal
506 SETUGT, // 1 0 1 0 True if unordered or greater than
507 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
508 SETULT, // 1 1 0 0 True if unordered or less than
509 SETULE, // 1 1 0 1 True if unordered, less than, or equal
510 SETUNE, // 1 1 1 0 True if unordered or not equal
511 SETTRUE, // 1 1 1 1 Always true (always folded)
512 // Don't care operations: undefined if the input is a nan.
513 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
514 SETEQ, // 1 X 0 0 1 True if equal
515 SETGT, // 1 X 0 1 0 True if greater than
516 SETGE, // 1 X 0 1 1 True if greater than or equal
517 SETLT, // 1 X 1 0 0 True if less than
518 SETLE, // 1 X 1 0 1 True if less than or equal
519 SETNE, // 1 X 1 1 0 True if not equal
520 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
522 SETCC_INVALID // Marker value.
525 /// isSignedIntSetCC - Return true if this is a setcc instruction that
526 /// performs a signed comparison when used with integer operands.
527 inline bool isSignedIntSetCC(CondCode Code) {
528 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
531 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
532 /// performs an unsigned comparison when used with integer operands.
533 inline bool isUnsignedIntSetCC(CondCode Code) {
534 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
537 /// isTrueWhenEqual - Return true if the specified condition returns true if
538 /// the two operands to the condition are equal. Note that if one of the two
539 /// operands is a NaN, this value is meaningless.
540 inline bool isTrueWhenEqual(CondCode Cond) {
541 return ((int)Cond & 1) != 0;
544 /// getUnorderedFlavor - This function returns 0 if the condition is always
545 /// false if an operand is a NaN, 1 if the condition is always true if the
546 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
548 inline unsigned getUnorderedFlavor(CondCode Cond) {
549 return ((int)Cond >> 3) & 3;
552 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
553 /// 'op' is a valid SetCC operation.
554 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
556 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
557 /// when given the operation for (X op Y).
558 CondCode getSetCCSwappedOperands(CondCode Operation);
560 /// getSetCCOrOperation - Return the result of a logical OR between different
561 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
562 /// function returns SETCC_INVALID if it is not possible to represent the
563 /// resultant comparison.
564 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
566 /// getSetCCAndOperation - Return the result of a logical AND between
567 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
568 /// function returns SETCC_INVALID if it is not possible to represent the
569 /// resultant comparison.
570 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
571 } // end llvm::ISD namespace
574 //===----------------------------------------------------------------------===//
575 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
576 /// values as the result of a computation. Many nodes return multiple values,
577 /// from loads (which define a token and a return value) to ADDC (which returns
578 /// a result and a carry value), to calls (which may return an arbitrary number
581 /// As such, each use of a SelectionDAG computation must indicate the node that
582 /// computes it as well as which return value to use from that node. This pair
583 /// of information is represented with the SDOperand value type.
587 SDNode *Val; // The node defining the value we are using.
588 unsigned ResNo; // Which return value of the node we are using.
590 SDOperand() : Val(0) {}
591 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
593 bool operator==(const SDOperand &O) const {
594 return Val == O.Val && ResNo == O.ResNo;
596 bool operator!=(const SDOperand &O) const {
597 return !operator==(O);
599 bool operator<(const SDOperand &O) const {
600 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
603 SDOperand getValue(unsigned R) const {
604 return SDOperand(Val, R);
607 // isOperand - Return true if this node is an operand of N.
608 bool isOperand(SDNode *N) const;
610 /// getValueType - Return the ValueType of the referenced return value.
612 inline MVT::ValueType getValueType() const;
614 // Forwarding methods - These forward to the corresponding methods in SDNode.
615 inline unsigned getOpcode() const;
616 inline unsigned getNodeDepth() const;
617 inline unsigned getNumOperands() const;
618 inline const SDOperand &getOperand(unsigned i) const;
619 inline bool isTargetOpcode() const;
620 inline unsigned getTargetOpcode() const;
622 /// hasOneUse - Return true if there is exactly one operation using this
623 /// result value of the defining operator.
624 inline bool hasOneUse() const;
628 /// simplify_type specializations - Allow casting operators to work directly on
629 /// SDOperands as if they were SDNode*'s.
630 template<> struct simplify_type<SDOperand> {
631 typedef SDNode* SimpleType;
632 static SimpleType getSimplifiedValue(const SDOperand &Val) {
633 return static_cast<SimpleType>(Val.Val);
636 template<> struct simplify_type<const SDOperand> {
637 typedef SDNode* SimpleType;
638 static SimpleType getSimplifiedValue(const SDOperand &Val) {
639 return static_cast<SimpleType>(Val.Val);
644 /// SDNode - Represents one node in the SelectionDAG.
647 /// NodeType - The operation that this node performs.
649 unsigned short NodeType;
651 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
652 /// means that leaves have a depth of 1, things that use only leaves have a
654 unsigned short NodeDepth;
656 /// OperandList - The values that are used by this operation.
658 SDOperand *OperandList;
660 /// ValueList - The types of the values this node defines. SDNode's may
661 /// define multiple values simultaneously.
662 MVT::ValueType *ValueList;
664 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
665 unsigned short NumOperands, NumValues;
667 /// Prev/Next pointers - These pointers form the linked list of of the
668 /// AllNodes list in the current DAG.
670 friend struct ilist_traits<SDNode>;
672 /// Uses - These are all of the SDNode's that use a value produced by this
674 std::vector<SDNode*> Uses;
677 assert(NumOperands == 0 && "Operand list not cleared before deletion");
680 //===--------------------------------------------------------------------===//
683 unsigned getOpcode() const { return NodeType; }
684 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
685 unsigned getTargetOpcode() const {
686 assert(isTargetOpcode() && "Not a target opcode!");
687 return NodeType - ISD::BUILTIN_OP_END;
690 size_t use_size() const { return Uses.size(); }
691 bool use_empty() const { return Uses.empty(); }
692 bool hasOneUse() const { return Uses.size() == 1; }
694 /// getNodeDepth - Return the distance from this node to the leaves in the
695 /// graph. The leaves have a depth of 1.
696 unsigned getNodeDepth() const { return NodeDepth; }
698 typedef std::vector<SDNode*>::const_iterator use_iterator;
699 use_iterator use_begin() const { return Uses.begin(); }
700 use_iterator use_end() const { return Uses.end(); }
702 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
703 /// indicated value. This method ignores uses of other values defined by this
705 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
707 // isOnlyUse - Return true if this node is the only use of N.
708 bool isOnlyUse(SDNode *N) const;
710 // isOperand - Return true if this node is an operand of N.
711 bool isOperand(SDNode *N) const;
713 /// getNumOperands - Return the number of values used by this operation.
715 unsigned getNumOperands() const { return NumOperands; }
717 const SDOperand &getOperand(unsigned Num) const {
718 assert(Num < NumOperands && "Invalid child # of SDNode!");
719 return OperandList[Num];
721 typedef const SDOperand* op_iterator;
722 op_iterator op_begin() const { return OperandList; }
723 op_iterator op_end() const { return OperandList+NumOperands; }
726 /// getNumValues - Return the number of values defined/returned by this
729 unsigned getNumValues() const { return NumValues; }
731 /// getValueType - Return the type of a specified result.
733 MVT::ValueType getValueType(unsigned ResNo) const {
734 assert(ResNo < NumValues && "Illegal result number!");
735 return ValueList[ResNo];
738 typedef const MVT::ValueType* value_iterator;
739 value_iterator value_begin() const { return ValueList; }
740 value_iterator value_end() const { return ValueList+NumValues; }
742 /// getOperationName - Return the opcode of this operation for printing.
744 const char* getOperationName(const SelectionDAG *G = 0) const;
746 void dump(const SelectionDAG *G) const;
748 static bool classof(const SDNode *) { return true; }
751 friend class SelectionDAG;
753 /// getValueTypeList - Return a pointer to the specified value type.
755 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
757 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
758 OperandList = 0; NumOperands = 0;
759 ValueList = getValueTypeList(VT);
763 SDNode(unsigned NT, SDOperand Op)
764 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
765 OperandList = new SDOperand[1];
768 Op.Val->Uses.push_back(this);
773 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
775 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
776 NodeDepth = N1.Val->getNodeDepth()+1;
778 NodeDepth = N2.Val->getNodeDepth()+1;
779 OperandList = new SDOperand[2];
783 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
788 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
790 unsigned ND = N1.Val->getNodeDepth();
791 if (ND < N2.Val->getNodeDepth())
792 ND = N2.Val->getNodeDepth();
793 if (ND < N3.Val->getNodeDepth())
794 ND = N3.Val->getNodeDepth();
797 OperandList = new SDOperand[3];
803 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
804 N3.Val->Uses.push_back(this);
809 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
811 unsigned ND = N1.Val->getNodeDepth();
812 if (ND < N2.Val->getNodeDepth())
813 ND = N2.Val->getNodeDepth();
814 if (ND < N3.Val->getNodeDepth())
815 ND = N3.Val->getNodeDepth();
816 if (ND < N4.Val->getNodeDepth())
817 ND = N4.Val->getNodeDepth();
820 OperandList = new SDOperand[4];
827 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
828 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
833 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
834 NumOperands = Nodes.size();
835 OperandList = new SDOperand[NumOperands];
838 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
839 OperandList[i] = Nodes[i];
840 SDNode *N = OperandList[i].Val;
841 N->Uses.push_back(this);
842 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
850 /// MorphNodeTo - This clears the return value and operands list, and sets the
851 /// opcode of the node to the specified value. This should only be used by
852 /// the SelectionDAG class.
853 void MorphNodeTo(unsigned Opc) {
858 // Clear the operands list, updating used nodes to remove this from their
860 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
861 I->Val->removeUser(this);
862 delete [] OperandList;
867 void setValueTypes(MVT::ValueType VT) {
868 assert(NumValues == 0 && "Should not have values yet!");
869 ValueList = getValueTypeList(VT);
872 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
873 assert(NumValues == 0 && "Should not have values yet!");
878 void setOperands(SDOperand Op0) {
879 assert(NumOperands == 0 && "Should not have operands yet!");
880 OperandList = new SDOperand[1];
881 OperandList[0] = Op0;
883 Op0.Val->Uses.push_back(this);
885 void setOperands(SDOperand Op0, SDOperand Op1) {
886 assert(NumOperands == 0 && "Should not have operands yet!");
887 OperandList = new SDOperand[2];
888 OperandList[0] = Op0;
889 OperandList[1] = Op1;
891 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
893 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
894 assert(NumOperands == 0 && "Should not have operands yet!");
895 OperandList = new SDOperand[3];
896 OperandList[0] = Op0;
897 OperandList[1] = Op1;
898 OperandList[2] = Op2;
900 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
901 Op2.Val->Uses.push_back(this);
903 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
904 assert(NumOperands == 0 && "Should not have operands yet!");
905 OperandList = new SDOperand[4];
906 OperandList[0] = Op0;
907 OperandList[1] = Op1;
908 OperandList[2] = Op2;
909 OperandList[3] = Op3;
911 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
912 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
914 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
916 assert(NumOperands == 0 && "Should not have operands yet!");
917 OperandList = new SDOperand[5];
918 OperandList[0] = Op0;
919 OperandList[1] = Op1;
920 OperandList[2] = Op2;
921 OperandList[3] = Op3;
922 OperandList[4] = Op4;
924 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
925 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
926 Op4.Val->Uses.push_back(this);
928 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
929 SDOperand Op4, SDOperand Op5) {
930 assert(NumOperands == 0 && "Should not have operands yet!");
931 OperandList = new SDOperand[6];
932 OperandList[0] = Op0;
933 OperandList[1] = Op1;
934 OperandList[2] = Op2;
935 OperandList[3] = Op3;
936 OperandList[4] = Op4;
937 OperandList[5] = Op5;
939 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
940 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
941 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
943 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
944 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
945 assert(NumOperands == 0 && "Should not have operands yet!");
946 OperandList = new SDOperand[7];
947 OperandList[0] = Op0;
948 OperandList[1] = Op1;
949 OperandList[2] = Op2;
950 OperandList[3] = Op3;
951 OperandList[4] = Op4;
952 OperandList[5] = Op5;
953 OperandList[6] = Op6;
955 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
956 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
957 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
958 Op6.Val->Uses.push_back(this);
960 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
961 SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
962 assert(NumOperands == 0 && "Should not have operands yet!");
963 OperandList = new SDOperand[8];
964 OperandList[0] = Op0;
965 OperandList[1] = Op1;
966 OperandList[2] = Op2;
967 OperandList[3] = Op3;
968 OperandList[4] = Op4;
969 OperandList[5] = Op5;
970 OperandList[6] = Op6;
971 OperandList[7] = Op7;
973 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
974 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
975 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
976 Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
979 void addUser(SDNode *User) {
980 Uses.push_back(User);
982 void removeUser(SDNode *User) {
983 // Remove this user from the operand's use list.
984 for (unsigned i = Uses.size(); ; --i) {
985 assert(i != 0 && "Didn't find user!");
986 if (Uses[i-1] == User) {
987 Uses[i-1] = Uses.back();
996 // Define inline functions from the SDOperand class.
998 inline unsigned SDOperand::getOpcode() const {
999 return Val->getOpcode();
1001 inline unsigned SDOperand::getNodeDepth() const {
1002 return Val->getNodeDepth();
1004 inline MVT::ValueType SDOperand::getValueType() const {
1005 return Val->getValueType(ResNo);
1007 inline unsigned SDOperand::getNumOperands() const {
1008 return Val->getNumOperands();
1010 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
1011 return Val->getOperand(i);
1013 inline bool SDOperand::isTargetOpcode() const {
1014 return Val->isTargetOpcode();
1016 inline unsigned SDOperand::getTargetOpcode() const {
1017 return Val->getTargetOpcode();
1019 inline bool SDOperand::hasOneUse() const {
1020 return Val->hasNUsesOfValue(1, ResNo);
1023 /// HandleSDNode - This class is used to form a handle around another node that
1024 /// is persistant and is updated across invocations of replaceAllUsesWith on its
1025 /// operand. This node should be directly created by end-users and not added to
1026 /// the AllNodes list.
1027 class HandleSDNode : public SDNode {
1029 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
1031 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
1034 SDOperand getValue() const { return getOperand(0); }
1037 class StringSDNode : public SDNode {
1040 friend class SelectionDAG;
1041 StringSDNode(const std::string &val)
1042 : SDNode(ISD::STRING, MVT::Other), Value(val) {
1045 const std::string &getValue() const { return Value; }
1046 static bool classof(const StringSDNode *) { return true; }
1047 static bool classof(const SDNode *N) {
1048 return N->getOpcode() == ISD::STRING;
1052 class ConstantSDNode : public SDNode {
1055 friend class SelectionDAG;
1056 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
1057 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
1061 uint64_t getValue() const { return Value; }
1063 int64_t getSignExtended() const {
1064 unsigned Bits = MVT::getSizeInBits(getValueType(0));
1065 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
1068 bool isNullValue() const { return Value == 0; }
1069 bool isAllOnesValue() const {
1070 int NumBits = MVT::getSizeInBits(getValueType(0));
1071 if (NumBits == 64) return Value+1 == 0;
1072 return Value == (1ULL << NumBits)-1;
1075 static bool classof(const ConstantSDNode *) { return true; }
1076 static bool classof(const SDNode *N) {
1077 return N->getOpcode() == ISD::Constant ||
1078 N->getOpcode() == ISD::TargetConstant;
1082 class ConstantFPSDNode : public SDNode {
1085 friend class SelectionDAG;
1086 ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1087 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1092 double getValue() const { return Value; }
1094 /// isExactlyValue - We don't rely on operator== working on double values, as
1095 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1096 /// As such, this method can be used to do an exact bit-for-bit comparison of
1097 /// two floating point values.
1098 bool isExactlyValue(double V) const;
1100 static bool classof(const ConstantFPSDNode *) { return true; }
1101 static bool classof(const SDNode *N) {
1102 return N->getOpcode() == ISD::ConstantFP ||
1103 N->getOpcode() == ISD::TargetConstantFP;
1107 class GlobalAddressSDNode : public SDNode {
1108 GlobalValue *TheGlobal;
1111 friend class SelectionDAG;
1112 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1114 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1116 TheGlobal = const_cast<GlobalValue*>(GA);
1120 GlobalValue *getGlobal() const { return TheGlobal; }
1121 int getOffset() const { return Offset; }
1123 static bool classof(const GlobalAddressSDNode *) { return true; }
1124 static bool classof(const SDNode *N) {
1125 return N->getOpcode() == ISD::GlobalAddress ||
1126 N->getOpcode() == ISD::TargetGlobalAddress;
1131 class FrameIndexSDNode : public SDNode {
1134 friend class SelectionDAG;
1135 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1136 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1139 int getIndex() const { return FI; }
1141 static bool classof(const FrameIndexSDNode *) { return true; }
1142 static bool classof(const SDNode *N) {
1143 return N->getOpcode() == ISD::FrameIndex ||
1144 N->getOpcode() == ISD::TargetFrameIndex;
1148 class ConstantPoolSDNode : public SDNode {
1153 friend class SelectionDAG;
1154 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1156 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1157 C(c), Offset(o), Alignment(0) {}
1158 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1160 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1161 C(c), Offset(o), Alignment(Align) {}
1164 Constant *get() const { return C; }
1165 int getOffset() const { return Offset; }
1167 // Return the alignment of this constant pool object, which is either 0 (for
1168 // default alignment) or log2 of the desired value.
1169 unsigned getAlignment() const { return Alignment; }
1171 static bool classof(const ConstantPoolSDNode *) { return true; }
1172 static bool classof(const SDNode *N) {
1173 return N->getOpcode() == ISD::ConstantPool ||
1174 N->getOpcode() == ISD::TargetConstantPool;
1178 class BasicBlockSDNode : public SDNode {
1179 MachineBasicBlock *MBB;
1181 friend class SelectionDAG;
1182 BasicBlockSDNode(MachineBasicBlock *mbb)
1183 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1186 MachineBasicBlock *getBasicBlock() const { return MBB; }
1188 static bool classof(const BasicBlockSDNode *) { return true; }
1189 static bool classof(const SDNode *N) {
1190 return N->getOpcode() == ISD::BasicBlock;
1194 class SrcValueSDNode : public SDNode {
1198 friend class SelectionDAG;
1199 SrcValueSDNode(const Value* v, int o)
1200 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1203 const Value *getValue() const { return V; }
1204 int getOffset() const { return offset; }
1206 static bool classof(const SrcValueSDNode *) { return true; }
1207 static bool classof(const SDNode *N) {
1208 return N->getOpcode() == ISD::SRCVALUE;
1213 class RegisterSDNode : public SDNode {
1216 friend class SelectionDAG;
1217 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1218 : SDNode(ISD::Register, VT), Reg(reg) {}
1221 unsigned getReg() const { return Reg; }
1223 static bool classof(const RegisterSDNode *) { return true; }
1224 static bool classof(const SDNode *N) {
1225 return N->getOpcode() == ISD::Register;
1229 class ExternalSymbolSDNode : public SDNode {
1232 friend class SelectionDAG;
1233 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1234 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1239 const char *getSymbol() const { return Symbol; }
1241 static bool classof(const ExternalSymbolSDNode *) { return true; }
1242 static bool classof(const SDNode *N) {
1243 return N->getOpcode() == ISD::ExternalSymbol ||
1244 N->getOpcode() == ISD::TargetExternalSymbol;
1248 class CondCodeSDNode : public SDNode {
1249 ISD::CondCode Condition;
1251 friend class SelectionDAG;
1252 CondCodeSDNode(ISD::CondCode Cond)
1253 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1257 ISD::CondCode get() const { return Condition; }
1259 static bool classof(const CondCodeSDNode *) { return true; }
1260 static bool classof(const SDNode *N) {
1261 return N->getOpcode() == ISD::CONDCODE;
1265 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1266 /// to parameterize some operations.
1267 class VTSDNode : public SDNode {
1268 MVT::ValueType ValueType;
1270 friend class SelectionDAG;
1271 VTSDNode(MVT::ValueType VT)
1272 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1275 MVT::ValueType getVT() const { return ValueType; }
1277 static bool classof(const VTSDNode *) { return true; }
1278 static bool classof(const SDNode *N) {
1279 return N->getOpcode() == ISD::VALUETYPE;
1284 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1288 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1290 bool operator==(const SDNodeIterator& x) const {
1291 return Operand == x.Operand;
1293 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1295 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1296 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1297 Operand = I.Operand;
1301 pointer operator*() const {
1302 return Node->getOperand(Operand).Val;
1304 pointer operator->() const { return operator*(); }
1306 SDNodeIterator& operator++() { // Preincrement
1310 SDNodeIterator operator++(int) { // Postincrement
1311 SDNodeIterator tmp = *this; ++*this; return tmp;
1314 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1315 static SDNodeIterator end (SDNode *N) {
1316 return SDNodeIterator(N, N->getNumOperands());
1319 unsigned getOperand() const { return Operand; }
1320 const SDNode *getNode() const { return Node; }
1323 template <> struct GraphTraits<SDNode*> {
1324 typedef SDNode NodeType;
1325 typedef SDNodeIterator ChildIteratorType;
1326 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1327 static inline ChildIteratorType child_begin(NodeType *N) {
1328 return SDNodeIterator::begin(N);
1330 static inline ChildIteratorType child_end(NodeType *N) {
1331 return SDNodeIterator::end(N);
1336 struct ilist_traits<SDNode> {
1337 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1338 static SDNode *getNext(const SDNode *N) { return N->Next; }
1340 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1341 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1343 static SDNode *createSentinel() {
1344 return new SDNode(ISD::EntryToken, MVT::Other);
1346 static void destroySentinel(SDNode *N) { delete N; }
1347 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1350 void addNodeToList(SDNode *NTy) {}
1351 void removeNodeFromList(SDNode *NTy) {}
1352 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1353 const ilist_iterator<SDNode> &X,
1354 const ilist_iterator<SDNode> &Y) {}
1357 } // end llvm namespace