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 // ConstantVec works like Constant or ConstantFP, except that it is not a
71 // leaf node. All operands are either Constant or ConstantFP nodes.
74 // TargetConstant* - Like Constant*, but the DAG does not do any folding or
75 // simplification of the constant.
80 // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
81 // anything else with this node, and this is valid in the target-specific
82 // dag, turning into a GlobalAddress operand.
88 // Abstract version of ConstantVec with abstract Vector type. The first node
89 // is a constant element count, the second is a value type indicating the
90 // type of the elements.
93 // CopyToReg - This node has three operands: a chain, a register number to
94 // set to this value, and a value.
97 // CopyFromReg - This node indicates that the input value is a virtual or
98 // physical register that is defined outside of the scope of this
99 // SelectionDAG. The register is available from the RegSDNode object.
102 // UNDEF - An undefined node
105 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
106 // a Constant, which is required to be operand #1), element of the aggregate
107 // value specified as operand #0. This is only for use before legalization,
108 // for values that will be broken into multiple registers.
111 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
112 // two values of the same integer value type, this produces a value twice as
113 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
116 // MERGE_VALUES - This node takes multiple discrete operands and returns
117 // them all as its individual results. This nodes has exactly the same
118 // number of inputs and outputs, and is only valid before legalization.
119 // This node is useful for some pieces of the code generator that want to
120 // think about a single node with multiple results, not multiple nodes.
123 // Simple integer binary arithmetic operators.
124 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
126 // Carry-setting nodes for multiple precision addition and subtraction.
127 // These nodes take two operands of the same value type, and produce two
128 // results. The first result is the normal add or sub result, the second
129 // result is the carry flag result.
132 // Carry-using nodes for multiple precision addition and subtraction. These
133 // nodes take three operands: The first two are the normal lhs and rhs to
134 // the add or sub, and the third is the input carry flag. These nodes
135 // produce two results; the normal result of the add or sub, and the output
136 // carry flag. These nodes both read and write a carry flag to allow them
137 // to them to be chained together for add and sub of arbitrarily large
141 // Simple binary floating point operators.
142 FADD, FSUB, FMUL, FDIV, FREM,
144 // FCOPYSIGN(X, Y) - Return the value of X with the sign of Y. NOTE: This
145 // DAG node does not require that X and Y have the same type, just that they
146 // are both floating point. X and the result must have the same type.
147 // FCOPYSIGN(f32, f64) is allowed.
150 // Simple abstract vector operators. Unlike the integer and floating point
151 // binary operators, these nodes also take two additional operands:
152 // a constant element count, and a value type node indicating the type of
153 // the elements. The order is count, type, op0, op1. All vector opcodes,
154 // including VLOAD and VConstant must currently have count and type as
155 // their 1st and 2nd arguments.
156 VADD, VSUB, VMUL, VSDIV, VUDIV,
159 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
160 // an unsigned/signed value of type i[2*n], then return the top part.
163 // Bitwise operators - logical and, logical or, logical xor, shift left,
164 // shift right algebraic (shift in sign bits), shift right logical (shift in
165 // zeroes), rotate left, rotate right, and byteswap.
166 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
168 // Counting operators
174 // Select with condition operator - This selects between a true value and
175 // a false value (ops #2 and #3) based on the boolean result of comparing
176 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
177 // condition code in op #4, a CondCodeSDNode.
180 // SetCC operator - This evaluates to a boolean (i1) true value if the
181 // condition is true. The operands to this are the left and right operands
182 // to compare (ops #0, and #1) and the condition code to compare them with
183 // (op #2) as a CondCodeSDNode.
186 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
187 // integer shift operations, just like ADD/SUB_PARTS. The operation
189 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
190 SHL_PARTS, SRA_PARTS, SRL_PARTS,
192 // Conversion operators. These are all single input single output
193 // operations. For all of these, the result type must be strictly
194 // wider or narrower (depending on the operation) than the source
197 // SIGN_EXTEND - Used for integer types, replicating the sign bit
201 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
204 // ANY_EXTEND - Used for integer types. The high bits are undefined.
207 // TRUNCATE - Completely drop the high bits.
210 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
211 // depends on the first letter) to floating point.
215 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
216 // sign extend a small value in a large integer register (e.g. sign
217 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
218 // with the 7th bit). The size of the smaller type is indicated by the 1th
219 // operand, a ValueType node.
222 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
227 // FP_ROUND - Perform a rounding operation from the current
228 // precision down to the specified precision (currently always 64->32).
231 // FP_ROUND_INREG - This operator takes a floating point register, and
232 // rounds it to a floating point value. It then promotes it and returns it
233 // in a register of the same size. This operation effectively just discards
234 // excess precision. The type to round down to is specified by the 1th
235 // operation, a VTSDNode (currently always 64->32->64).
238 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
241 // BIT_CONVERT - Theis operator converts between integer and FP values, as
242 // if one was stored to memory as integer and the other was loaded from the
243 // same address (or equivalently for vector format conversions, etc). The
244 // source and result are required to have the same bit size (e.g.
245 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
246 // conversions, but that is a noop, deleted by getNode().
249 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
250 // absolute value, square root, sine and cosine operations.
251 FNEG, FABS, FSQRT, FSIN, FCOS,
253 // Other operators. LOAD and STORE have token chains as their first
254 // operand, then the same operands as an LLVM load/store instruction, then a
255 // SRCVALUE node that provides alias analysis information.
258 // Abstract vector version of LOAD. VLOAD has a constant element count as
259 // the first operand, followed by a value type node indicating the type of
260 // the elements, a token chain, a pointer operand, and a SRCVALUE node.
263 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
264 // memory and extend them to a larger value (e.g. load a byte into a word
265 // register). All three of these have four operands, a token chain, a
266 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
267 // indicating the type to load.
269 // SEXTLOAD loads the integer operand and sign extends it to a larger
270 // integer result type.
271 // ZEXTLOAD loads the integer operand and zero extends it to a larger
272 // integer result type.
273 // EXTLOAD is used for two things: floating point extending loads, and
274 // integer extending loads where it doesn't matter what the high
275 // bits are set to. The code generator is allowed to codegen this
276 // into whichever operation is more efficient.
277 EXTLOAD, SEXTLOAD, ZEXTLOAD,
279 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
280 // value and stores it to memory in one operation. This can be used for
281 // either integer or floating point operands. The first four operands of
282 // this are the same as a standard store. The fifth is the ValueType to
283 // store it as (which will be smaller than the source value).
286 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
287 // to a specified boundary. The first operand is the token chain, the
288 // second is the number of bytes to allocate, and the third is the alignment
289 // boundary. The size is guaranteed to be a multiple of the stack
290 // alignment, and the alignment is guaranteed to be bigger than the stack
291 // alignment (if required) or 0 to get standard stack alignment.
294 // Control flow instructions. These all have token chains.
296 // BR - Unconditional branch. The first operand is the chain
297 // operand, the second is the MBB to branch to.
300 // BRCOND - Conditional branch. The first operand is the chain,
301 // the second is the condition, the third is the block to branch
302 // to if the condition is true.
305 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
306 // that the condition is represented as condition code, and two nodes to
307 // compare, rather than as a combined SetCC node. The operands in order are
308 // chain, cc, lhs, rhs, block to branch to if condition is true.
311 // RET - Return from function. The first operand is the chain,
312 // and any subsequent operands are the return values for the
313 // function. This operation can have variable number of operands.
316 // INLINEASM - Represents an inline asm block. This node always has two
317 // return values: a chain and a flag result. The inputs are as follows:
318 // Operand #0 : Input chain.
319 // Operand #1 : a ExternalSymbolSDNode with a pointer to the asm string.
320 // Operand #2n+2: A RegisterNode.
321 // Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
322 // Operand #last: Optional, an incoming flag.
325 // STACKSAVE - STACKSAVE has one operand, an input chain. It produces a
326 // value, the same type as the pointer type for the system, and an output
330 // STACKRESTORE has two operands, an input chain and a pointer to restore to
331 // it returns an output chain.
334 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
335 // correspond to the operands of the LLVM intrinsic functions. The only
336 // result is a token chain. The alignment argument is guaranteed to be a
342 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
343 // a call sequence, and carry arbitrary information that target might want
344 // to know. The first operand is a chain, the rest are specified by the
345 // target and not touched by the DAG optimizers.
346 CALLSEQ_START, // Beginning of a call sequence
347 CALLSEQ_END, // End of a call sequence
349 // VAARG - VAARG has three operands: an input chain, a pointer, and a
350 // SRCVALUE. It returns a pair of values: the vaarg value and a new chain.
353 // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
354 // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
358 // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
359 // pointer, and a SRCVALUE.
362 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
363 // locations with their value. This allows one use alias analysis
364 // information in the backend.
367 // PCMARKER - This corresponds to the pcmarker intrinsic.
370 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
371 // The only operand is a chain and a value and a chain are produced. The
372 // value is the contents of the architecture specific cycle counter like
373 // register (or other high accuracy low latency clock source)
376 // HANDLENODE node - Used as a handle for various purposes.
379 // LOCATION - This node is used to represent a source location for debug
380 // info. It takes token chain as input, then a line number, then a column
381 // number, then a filename, then a working dir. It produces a token chain
385 // DEBUG_LOC - This node is used to represent source line information
386 // embedded in the code. It takes a token chain as input, then a line
387 // number, then a column then a file id (provided by MachineDebugInfo.) It
388 // produces a token chain as output.
391 // DEBUG_LABEL - This node is used to mark a location in the code where a
392 // label should be generated for use by the debug information. It takes a
393 // token chain as input and then a unique id (provided by MachineDebugInfo.)
394 // It produces a token chain as output.
397 // BUILTIN_OP_END - This must be the last enum value in this list.
401 //===--------------------------------------------------------------------===//
402 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
403 /// below work out, when considering SETFALSE (something that never exists
404 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
405 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
406 /// to. If the "N" column is 1, the result of the comparison is undefined if
407 /// the input is a NAN.
409 /// All of these (except for the 'always folded ops') should be handled for
410 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
411 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
413 /// Note that these are laid out in a specific order to allow bit-twiddling
414 /// to transform conditions.
416 // Opcode N U L G E Intuitive operation
417 SETFALSE, // 0 0 0 0 Always false (always folded)
418 SETOEQ, // 0 0 0 1 True if ordered and equal
419 SETOGT, // 0 0 1 0 True if ordered and greater than
420 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
421 SETOLT, // 0 1 0 0 True if ordered and less than
422 SETOLE, // 0 1 0 1 True if ordered and less than or equal
423 SETONE, // 0 1 1 0 True if ordered and operands are unequal
424 SETO, // 0 1 1 1 True if ordered (no nans)
425 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
426 SETUEQ, // 1 0 0 1 True if unordered or equal
427 SETUGT, // 1 0 1 0 True if unordered or greater than
428 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
429 SETULT, // 1 1 0 0 True if unordered or less than
430 SETULE, // 1 1 0 1 True if unordered, less than, or equal
431 SETUNE, // 1 1 1 0 True if unordered or not equal
432 SETTRUE, // 1 1 1 1 Always true (always folded)
433 // Don't care operations: undefined if the input is a nan.
434 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
435 SETEQ, // 1 X 0 0 1 True if equal
436 SETGT, // 1 X 0 1 0 True if greater than
437 SETGE, // 1 X 0 1 1 True if greater than or equal
438 SETLT, // 1 X 1 0 0 True if less than
439 SETLE, // 1 X 1 0 1 True if less than or equal
440 SETNE, // 1 X 1 1 0 True if not equal
441 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
443 SETCC_INVALID // Marker value.
446 /// isSignedIntSetCC - Return true if this is a setcc instruction that
447 /// performs a signed comparison when used with integer operands.
448 inline bool isSignedIntSetCC(CondCode Code) {
449 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
452 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
453 /// performs an unsigned comparison when used with integer operands.
454 inline bool isUnsignedIntSetCC(CondCode Code) {
455 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
458 /// isTrueWhenEqual - Return true if the specified condition returns true if
459 /// the two operands to the condition are equal. Note that if one of the two
460 /// operands is a NaN, this value is meaningless.
461 inline bool isTrueWhenEqual(CondCode Cond) {
462 return ((int)Cond & 1) != 0;
465 /// getUnorderedFlavor - This function returns 0 if the condition is always
466 /// false if an operand is a NaN, 1 if the condition is always true if the
467 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
469 inline unsigned getUnorderedFlavor(CondCode Cond) {
470 return ((int)Cond >> 3) & 3;
473 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
474 /// 'op' is a valid SetCC operation.
475 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
477 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
478 /// when given the operation for (X op Y).
479 CondCode getSetCCSwappedOperands(CondCode Operation);
481 /// getSetCCOrOperation - Return the result of a logical OR between different
482 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
483 /// function returns SETCC_INVALID if it is not possible to represent the
484 /// resultant comparison.
485 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
487 /// getSetCCAndOperation - Return the result of a logical AND between
488 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
489 /// function returns SETCC_INVALID if it is not possible to represent the
490 /// resultant comparison.
491 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
492 } // end llvm::ISD namespace
495 //===----------------------------------------------------------------------===//
496 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
497 /// values as the result of a computation. Many nodes return multiple values,
498 /// from loads (which define a token and a return value) to ADDC (which returns
499 /// a result and a carry value), to calls (which may return an arbitrary number
502 /// As such, each use of a SelectionDAG computation must indicate the node that
503 /// computes it as well as which return value to use from that node. This pair
504 /// of information is represented with the SDOperand value type.
508 SDNode *Val; // The node defining the value we are using.
509 unsigned ResNo; // Which return value of the node we are using.
511 SDOperand() : Val(0) {}
512 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
514 bool operator==(const SDOperand &O) const {
515 return Val == O.Val && ResNo == O.ResNo;
517 bool operator!=(const SDOperand &O) const {
518 return !operator==(O);
520 bool operator<(const SDOperand &O) const {
521 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
524 SDOperand getValue(unsigned R) const {
525 return SDOperand(Val, R);
528 // isOperand - Return true if this node is an operand of N.
529 bool isOperand(SDNode *N) const;
531 /// getValueType - Return the ValueType of the referenced return value.
533 inline MVT::ValueType getValueType() const;
535 // Forwarding methods - These forward to the corresponding methods in SDNode.
536 inline unsigned getOpcode() const;
537 inline unsigned getNodeDepth() const;
538 inline unsigned getNumOperands() const;
539 inline const SDOperand &getOperand(unsigned i) const;
540 inline bool isTargetOpcode() const;
541 inline unsigned getTargetOpcode() const;
543 /// hasOneUse - Return true if there is exactly one operation using this
544 /// result value of the defining operator.
545 inline bool hasOneUse() const;
549 /// simplify_type specializations - Allow casting operators to work directly on
550 /// SDOperands as if they were SDNode*'s.
551 template<> struct simplify_type<SDOperand> {
552 typedef SDNode* SimpleType;
553 static SimpleType getSimplifiedValue(const SDOperand &Val) {
554 return static_cast<SimpleType>(Val.Val);
557 template<> struct simplify_type<const SDOperand> {
558 typedef SDNode* SimpleType;
559 static SimpleType getSimplifiedValue(const SDOperand &Val) {
560 return static_cast<SimpleType>(Val.Val);
565 /// SDNode - Represents one node in the SelectionDAG.
568 /// NodeType - The operation that this node performs.
570 unsigned short NodeType;
572 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
573 /// means that leaves have a depth of 1, things that use only leaves have a
575 unsigned short NodeDepth;
577 /// OperandList - The values that are used by this operation.
579 SDOperand *OperandList;
581 /// ValueList - The types of the values this node defines. SDNode's may
582 /// define multiple values simultaneously.
583 MVT::ValueType *ValueList;
585 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
586 unsigned short NumOperands, NumValues;
588 /// Prev/Next pointers - These pointers form the linked list of of the
589 /// AllNodes list in the current DAG.
591 friend struct ilist_traits<SDNode>;
593 /// Uses - These are all of the SDNode's that use a value produced by this
595 std::vector<SDNode*> Uses;
598 assert(NumOperands == 0 && "Operand list not cleared before deletion");
601 //===--------------------------------------------------------------------===//
604 unsigned getOpcode() const { return NodeType; }
605 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
606 unsigned getTargetOpcode() const {
607 assert(isTargetOpcode() && "Not a target opcode!");
608 return NodeType - ISD::BUILTIN_OP_END;
611 size_t use_size() const { return Uses.size(); }
612 bool use_empty() const { return Uses.empty(); }
613 bool hasOneUse() const { return Uses.size() == 1; }
615 /// getNodeDepth - Return the distance from this node to the leaves in the
616 /// graph. The leaves have a depth of 1.
617 unsigned getNodeDepth() const { return NodeDepth; }
619 typedef std::vector<SDNode*>::const_iterator use_iterator;
620 use_iterator use_begin() const { return Uses.begin(); }
621 use_iterator use_end() const { return Uses.end(); }
623 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
624 /// indicated value. This method ignores uses of other values defined by this
626 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
628 // isOnlyUse - Return true if this node is the only use of N.
629 bool isOnlyUse(SDNode *N) const;
631 // isOperand - Return true if this node is an operand of N.
632 bool isOperand(SDNode *N) const;
634 /// getNumOperands - Return the number of values used by this operation.
636 unsigned getNumOperands() const { return NumOperands; }
638 const SDOperand &getOperand(unsigned Num) const {
639 assert(Num < NumOperands && "Invalid child # of SDNode!");
640 return OperandList[Num];
642 typedef const SDOperand* op_iterator;
643 op_iterator op_begin() const { return OperandList; }
644 op_iterator op_end() const { return OperandList+NumOperands; }
647 /// getNumValues - Return the number of values defined/returned by this
650 unsigned getNumValues() const { return NumValues; }
652 /// getValueType - Return the type of a specified result.
654 MVT::ValueType getValueType(unsigned ResNo) const {
655 assert(ResNo < NumValues && "Illegal result number!");
656 return ValueList[ResNo];
659 typedef const MVT::ValueType* value_iterator;
660 value_iterator value_begin() const { return ValueList; }
661 value_iterator value_end() const { return ValueList+NumValues; }
663 /// getOperationName - Return the opcode of this operation for printing.
665 const char* getOperationName(const SelectionDAG *G = 0) const;
667 void dump(const SelectionDAG *G) const;
669 static bool classof(const SDNode *) { return true; }
672 friend class SelectionDAG;
674 /// getValueTypeList - Return a pointer to the specified value type.
676 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
678 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
679 OperandList = 0; NumOperands = 0;
680 ValueList = getValueTypeList(VT);
684 SDNode(unsigned NT, SDOperand Op)
685 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
686 OperandList = new SDOperand[1];
689 Op.Val->Uses.push_back(this);
694 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
696 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
697 NodeDepth = N1.Val->getNodeDepth()+1;
699 NodeDepth = N2.Val->getNodeDepth()+1;
700 OperandList = new SDOperand[2];
704 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
709 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
711 unsigned ND = N1.Val->getNodeDepth();
712 if (ND < N2.Val->getNodeDepth())
713 ND = N2.Val->getNodeDepth();
714 if (ND < N3.Val->getNodeDepth())
715 ND = N3.Val->getNodeDepth();
718 OperandList = new SDOperand[3];
724 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
725 N3.Val->Uses.push_back(this);
730 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
732 unsigned ND = N1.Val->getNodeDepth();
733 if (ND < N2.Val->getNodeDepth())
734 ND = N2.Val->getNodeDepth();
735 if (ND < N3.Val->getNodeDepth())
736 ND = N3.Val->getNodeDepth();
737 if (ND < N4.Val->getNodeDepth())
738 ND = N4.Val->getNodeDepth();
741 OperandList = new SDOperand[4];
748 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
749 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
754 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
755 NumOperands = Nodes.size();
756 OperandList = new SDOperand[NumOperands];
759 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
760 OperandList[i] = Nodes[i];
761 SDNode *N = OperandList[i].Val;
762 N->Uses.push_back(this);
763 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
771 /// MorphNodeTo - This clears the return value and operands list, and sets the
772 /// opcode of the node to the specified value. This should only be used by
773 /// the SelectionDAG class.
774 void MorphNodeTo(unsigned Opc) {
779 // Clear the operands list, updating used nodes to remove this from their
781 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
782 I->Val->removeUser(this);
783 delete [] OperandList;
788 void setValueTypes(MVT::ValueType VT) {
789 assert(NumValues == 0 && "Should not have values yet!");
790 ValueList = getValueTypeList(VT);
793 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
794 assert(NumValues == 0 && "Should not have values yet!");
799 void setOperands(SDOperand Op0) {
800 assert(NumOperands == 0 && "Should not have operands yet!");
801 OperandList = new SDOperand[1];
802 OperandList[0] = Op0;
804 Op0.Val->Uses.push_back(this);
806 void setOperands(SDOperand Op0, SDOperand Op1) {
807 assert(NumOperands == 0 && "Should not have operands yet!");
808 OperandList = new SDOperand[2];
809 OperandList[0] = Op0;
810 OperandList[1] = Op1;
812 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
814 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
815 assert(NumOperands == 0 && "Should not have operands yet!");
816 OperandList = new SDOperand[3];
817 OperandList[0] = Op0;
818 OperandList[1] = Op1;
819 OperandList[2] = Op2;
821 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
822 Op2.Val->Uses.push_back(this);
824 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
825 assert(NumOperands == 0 && "Should not have operands yet!");
826 OperandList = new SDOperand[4];
827 OperandList[0] = Op0;
828 OperandList[1] = Op1;
829 OperandList[2] = Op2;
830 OperandList[3] = Op3;
832 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
833 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
835 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
837 assert(NumOperands == 0 && "Should not have operands yet!");
838 OperandList = new SDOperand[5];
839 OperandList[0] = Op0;
840 OperandList[1] = Op1;
841 OperandList[2] = Op2;
842 OperandList[3] = Op3;
843 OperandList[4] = Op4;
845 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
846 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
847 Op4.Val->Uses.push_back(this);
849 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
850 SDOperand Op4, SDOperand Op5) {
851 assert(NumOperands == 0 && "Should not have operands yet!");
852 OperandList = new SDOperand[6];
853 OperandList[0] = Op0;
854 OperandList[1] = Op1;
855 OperandList[2] = Op2;
856 OperandList[3] = Op3;
857 OperandList[4] = Op4;
858 OperandList[5] = Op5;
860 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
861 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
862 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
864 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
865 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
866 assert(NumOperands == 0 && "Should not have operands yet!");
867 OperandList = new SDOperand[7];
868 OperandList[0] = Op0;
869 OperandList[1] = Op1;
870 OperandList[2] = Op2;
871 OperandList[3] = Op3;
872 OperandList[4] = Op4;
873 OperandList[5] = Op5;
874 OperandList[6] = Op6;
876 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
877 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
878 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
879 Op6.Val->Uses.push_back(this);
881 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
882 SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
883 assert(NumOperands == 0 && "Should not have operands yet!");
884 OperandList = new SDOperand[8];
885 OperandList[0] = Op0;
886 OperandList[1] = Op1;
887 OperandList[2] = Op2;
888 OperandList[3] = Op3;
889 OperandList[4] = Op4;
890 OperandList[5] = Op5;
891 OperandList[6] = Op6;
892 OperandList[7] = Op7;
894 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
895 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
896 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
897 Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
900 void addUser(SDNode *User) {
901 Uses.push_back(User);
903 void removeUser(SDNode *User) {
904 // Remove this user from the operand's use list.
905 for (unsigned i = Uses.size(); ; --i) {
906 assert(i != 0 && "Didn't find user!");
907 if (Uses[i-1] == User) {
908 Uses[i-1] = Uses.back();
917 // Define inline functions from the SDOperand class.
919 inline unsigned SDOperand::getOpcode() const {
920 return Val->getOpcode();
922 inline unsigned SDOperand::getNodeDepth() const {
923 return Val->getNodeDepth();
925 inline MVT::ValueType SDOperand::getValueType() const {
926 return Val->getValueType(ResNo);
928 inline unsigned SDOperand::getNumOperands() const {
929 return Val->getNumOperands();
931 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
932 return Val->getOperand(i);
934 inline bool SDOperand::isTargetOpcode() const {
935 return Val->isTargetOpcode();
937 inline unsigned SDOperand::getTargetOpcode() const {
938 return Val->getTargetOpcode();
940 inline bool SDOperand::hasOneUse() const {
941 return Val->hasNUsesOfValue(1, ResNo);
944 /// HandleSDNode - This class is used to form a handle around another node that
945 /// is persistant and is updated across invocations of replaceAllUsesWith on its
946 /// operand. This node should be directly created by end-users and not added to
947 /// the AllNodes list.
948 class HandleSDNode : public SDNode {
950 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
952 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
955 SDOperand getValue() const { return getOperand(0); }
958 class StringSDNode : public SDNode {
961 friend class SelectionDAG;
962 StringSDNode(const std::string &val)
963 : SDNode(ISD::STRING, MVT::Other), Value(val) {
966 const std::string &getValue() const { return Value; }
967 static bool classof(const StringSDNode *) { return true; }
968 static bool classof(const SDNode *N) {
969 return N->getOpcode() == ISD::STRING;
973 class ConstantSDNode : public SDNode {
976 friend class SelectionDAG;
977 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
978 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
982 uint64_t getValue() const { return Value; }
984 int64_t getSignExtended() const {
985 unsigned Bits = MVT::getSizeInBits(getValueType(0));
986 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
989 bool isNullValue() const { return Value == 0; }
990 bool isAllOnesValue() const {
991 int NumBits = MVT::getSizeInBits(getValueType(0));
992 if (NumBits == 64) return Value+1 == 0;
993 return Value == (1ULL << NumBits)-1;
996 static bool classof(const ConstantSDNode *) { return true; }
997 static bool classof(const SDNode *N) {
998 return N->getOpcode() == ISD::Constant ||
999 N->getOpcode() == ISD::TargetConstant;
1003 class ConstantFPSDNode : public SDNode {
1006 friend class SelectionDAG;
1007 ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1008 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1013 double getValue() const { return Value; }
1015 /// isExactlyValue - We don't rely on operator== working on double values, as
1016 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1017 /// As such, this method can be used to do an exact bit-for-bit comparison of
1018 /// two floating point values.
1019 bool isExactlyValue(double V) const;
1021 static bool classof(const ConstantFPSDNode *) { return true; }
1022 static bool classof(const SDNode *N) {
1023 return N->getOpcode() == ISD::ConstantFP ||
1024 N->getOpcode() == ISD::TargetConstantFP;
1028 class GlobalAddressSDNode : public SDNode {
1029 GlobalValue *TheGlobal;
1032 friend class SelectionDAG;
1033 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1035 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT),
1037 TheGlobal = const_cast<GlobalValue*>(GA);
1041 GlobalValue *getGlobal() const { return TheGlobal; }
1042 int getOffset() const { return Offset; }
1044 static bool classof(const GlobalAddressSDNode *) { return true; }
1045 static bool classof(const SDNode *N) {
1046 return N->getOpcode() == ISD::GlobalAddress ||
1047 N->getOpcode() == ISD::TargetGlobalAddress;
1052 class FrameIndexSDNode : public SDNode {
1055 friend class SelectionDAG;
1056 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1057 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1060 int getIndex() const { return FI; }
1062 static bool classof(const FrameIndexSDNode *) { return true; }
1063 static bool classof(const SDNode *N) {
1064 return N->getOpcode() == ISD::FrameIndex ||
1065 N->getOpcode() == ISD::TargetFrameIndex;
1069 class ConstantPoolSDNode : public SDNode {
1074 friend class SelectionDAG;
1075 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT,
1077 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1078 C(c), Offset(o), Alignment(0) {}
1079 ConstantPoolSDNode(bool isTarget, Constant *c, MVT::ValueType VT, int o,
1081 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1082 C(c), Offset(o), Alignment(Align) {}
1085 Constant *get() const { return C; }
1086 int getOffset() const { return Offset; }
1088 // Return the alignment of this constant pool object, which is either 0 (for
1089 // default alignment) or log2 of the desired value.
1090 unsigned getAlignment() const { return Alignment; }
1092 static bool classof(const ConstantPoolSDNode *) { return true; }
1093 static bool classof(const SDNode *N) {
1094 return N->getOpcode() == ISD::ConstantPool ||
1095 N->getOpcode() == ISD::TargetConstantPool;
1099 class BasicBlockSDNode : public SDNode {
1100 MachineBasicBlock *MBB;
1102 friend class SelectionDAG;
1103 BasicBlockSDNode(MachineBasicBlock *mbb)
1104 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1107 MachineBasicBlock *getBasicBlock() const { return MBB; }
1109 static bool classof(const BasicBlockSDNode *) { return true; }
1110 static bool classof(const SDNode *N) {
1111 return N->getOpcode() == ISD::BasicBlock;
1115 class SrcValueSDNode : public SDNode {
1119 friend class SelectionDAG;
1120 SrcValueSDNode(const Value* v, int o)
1121 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1124 const Value *getValue() const { return V; }
1125 int getOffset() const { return offset; }
1127 static bool classof(const SrcValueSDNode *) { return true; }
1128 static bool classof(const SDNode *N) {
1129 return N->getOpcode() == ISD::SRCVALUE;
1134 class RegisterSDNode : public SDNode {
1137 friend class SelectionDAG;
1138 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1139 : SDNode(ISD::Register, VT), Reg(reg) {}
1142 unsigned getReg() const { return Reg; }
1144 static bool classof(const RegisterSDNode *) { return true; }
1145 static bool classof(const SDNode *N) {
1146 return N->getOpcode() == ISD::Register;
1150 class ExternalSymbolSDNode : public SDNode {
1153 friend class SelectionDAG;
1154 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1155 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1160 const char *getSymbol() const { return Symbol; }
1162 static bool classof(const ExternalSymbolSDNode *) { return true; }
1163 static bool classof(const SDNode *N) {
1164 return N->getOpcode() == ISD::ExternalSymbol ||
1165 N->getOpcode() == ISD::TargetExternalSymbol;
1169 class CondCodeSDNode : public SDNode {
1170 ISD::CondCode Condition;
1172 friend class SelectionDAG;
1173 CondCodeSDNode(ISD::CondCode Cond)
1174 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1178 ISD::CondCode get() const { return Condition; }
1180 static bool classof(const CondCodeSDNode *) { return true; }
1181 static bool classof(const SDNode *N) {
1182 return N->getOpcode() == ISD::CONDCODE;
1186 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1187 /// to parameterize some operations.
1188 class VTSDNode : public SDNode {
1189 MVT::ValueType ValueType;
1191 friend class SelectionDAG;
1192 VTSDNode(MVT::ValueType VT)
1193 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1196 MVT::ValueType getVT() const { return ValueType; }
1198 static bool classof(const VTSDNode *) { return true; }
1199 static bool classof(const SDNode *N) {
1200 return N->getOpcode() == ISD::VALUETYPE;
1205 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1209 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1211 bool operator==(const SDNodeIterator& x) const {
1212 return Operand == x.Operand;
1214 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1216 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1217 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1218 Operand = I.Operand;
1222 pointer operator*() const {
1223 return Node->getOperand(Operand).Val;
1225 pointer operator->() const { return operator*(); }
1227 SDNodeIterator& operator++() { // Preincrement
1231 SDNodeIterator operator++(int) { // Postincrement
1232 SDNodeIterator tmp = *this; ++*this; return tmp;
1235 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1236 static SDNodeIterator end (SDNode *N) {
1237 return SDNodeIterator(N, N->getNumOperands());
1240 unsigned getOperand() const { return Operand; }
1241 const SDNode *getNode() const { return Node; }
1244 template <> struct GraphTraits<SDNode*> {
1245 typedef SDNode NodeType;
1246 typedef SDNodeIterator ChildIteratorType;
1247 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1248 static inline ChildIteratorType child_begin(NodeType *N) {
1249 return SDNodeIterator::begin(N);
1251 static inline ChildIteratorType child_end(NodeType *N) {
1252 return SDNodeIterator::end(N);
1257 struct ilist_traits<SDNode> {
1258 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1259 static SDNode *getNext(const SDNode *N) { return N->Next; }
1261 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1262 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1264 static SDNode *createSentinel() {
1265 return new SDNode(ISD::EntryToken, MVT::Other);
1267 static void destroySentinel(SDNode *N) { delete N; }
1268 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1271 void addNodeToList(SDNode *NTy) {}
1272 void removeNodeFromList(SDNode *NTy) {}
1273 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1274 const ilist_iterator<SDNode> &X,
1275 const ilist_iterator<SDNode> &Y) {}
1278 } // end llvm namespace