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 Constant, ConstantFP, STRING,
67 GlobalAddress, FrameIndex, ConstantPool,
68 BasicBlock, ExternalSymbol, VALUETYPE, CONDCODE, Register,
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 // CopyToReg - This node has three operands: a chain, a register number to
89 // set to this value, and a value.
92 // CopyFromReg - This node indicates that the input value is a virtual or
93 // physical register that is defined outside of the scope of this
94 // SelectionDAG. The register is available from the RegSDNode object.
97 // UNDEF - An undefined node
100 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
101 // a Constant, which is required to be operand #1), element of the aggregate
102 // value specified as operand #0. This is only for use before legalization,
103 // for values that will be broken into multiple registers.
106 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
107 // two values of the same integer value type, this produces a value twice as
108 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
111 // MERGE_VALUES - This node takes multiple discrete operands and returns
112 // them all as its individual results. This nodes has exactly the same
113 // number of inputs and outputs, and is only valid before legalization.
114 // This node is useful for some pieces of the code generator that want to
115 // think about a single node with multiple results, not multiple nodes.
118 // Simple integer binary arithmetic operators.
119 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
121 // Simple binary floating point operators.
122 FADD, FSUB, FMUL, FDIV, FREM,
124 // Simple abstract vector operators. Unlike the integer and floating point
125 // binary operators, these nodes also take two additional operands:
126 // a constant element count, and a value type node indicating the type of
127 // the elements. The order is op0, op1, count, type. All vector opcodes,
128 // including VLOAD, must currently have count and type as their 3rd and 4th
132 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
133 // an unsigned/signed value of type i[2*n], then return the top part.
136 // Bitwise operators - logical and, logical or, logical xor, shift left,
137 // shift right algebraic (shift in sign bits), shift right logical (shift in
138 // zeroes), rotate left, rotate right, and byteswap.
139 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
141 // Counting operators
147 // Select with condition operator - This selects between a true value and
148 // a false value (ops #2 and #3) based on the boolean result of comparing
149 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
150 // condition code in op #4, a CondCodeSDNode.
153 // SetCC operator - This evaluates to a boolean (i1) true value if the
154 // condition is true. The operands to this are the left and right operands
155 // to compare (ops #0, and #1) and the condition code to compare them with
156 // (op #2) as a CondCodeSDNode.
159 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
160 // broken into a multiple pieces each, and return the resulting pieces of
161 // doing an atomic add/sub operation. This is used to handle add/sub of
162 // expanded types. The operation ordering is:
163 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
164 ADD_PARTS, SUB_PARTS,
166 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
167 // integer shift operations, just like ADD/SUB_PARTS. The operation
169 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
170 SHL_PARTS, SRA_PARTS, SRL_PARTS,
172 // Conversion operators. These are all single input single output
173 // operations. For all of these, the result type must be strictly
174 // wider or narrower (depending on the operation) than the source
177 // SIGN_EXTEND - Used for integer types, replicating the sign bit
181 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
184 // ANY_EXTEND - Used for integer types. The high bits are undefined.
187 // TRUNCATE - Completely drop the high bits.
190 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
191 // depends on the first letter) to floating point.
195 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
196 // sign extend a small value in a large integer register (e.g. sign
197 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
198 // with the 7th bit). The size of the smaller type is indicated by the 1th
199 // operand, a ValueType node.
202 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
207 // FP_ROUND - Perform a rounding operation from the current
208 // precision down to the specified precision (currently always 64->32).
211 // FP_ROUND_INREG - This operator takes a floating point register, and
212 // rounds it to a floating point value. It then promotes it and returns it
213 // in a register of the same size. This operation effectively just discards
214 // excess precision. The type to round down to is specified by the 1th
215 // operation, a VTSDNode (currently always 64->32->64).
218 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
221 // BIT_CONVERT - Theis operator converts between integer and FP values, as
222 // if one was stored to memory as integer and the other was loaded from the
223 // same address (or equivalently for vector format conversions, etc). The
224 // source and result are required to have the same bit size (e.g.
225 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
226 // conversions, but that is a noop, deleted by getNode().
229 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
230 // absolute value, square root, sine and cosine operations.
231 FNEG, FABS, FSQRT, FSIN, FCOS,
233 // Other operators. LOAD and STORE have token chains as their first
234 // operand, then the same operands as an LLVM load/store instruction, then a
235 // SRCVALUE node that provides alias analysis information.
238 // Abstract vector version of LOAD. VLOAD has a token chain as the first
239 // operand, followed by a pointer operand, a constant element count, a value
240 // type node indicating the type of the elements, and a SRCVALUE node.
243 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
244 // memory and extend them to a larger value (e.g. load a byte into a word
245 // register). All three of these have four operands, a token chain, a
246 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
247 // indicating the type to load.
249 // SEXTLOAD loads the integer operand and sign extends it to a larger
250 // integer result type.
251 // ZEXTLOAD loads the integer operand and zero extends it to a larger
252 // integer result type.
253 // EXTLOAD is used for two things: floating point extending loads, and
254 // integer extending loads where it doesn't matter what the high
255 // bits are set to. The code generator is allowed to codegen this
256 // into whichever operation is more efficient.
257 EXTLOAD, SEXTLOAD, ZEXTLOAD,
259 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
260 // value and stores it to memory in one operation. This can be used for
261 // either integer or floating point operands. The first four operands of
262 // this are the same as a standard store. The fifth is the ValueType to
263 // store it as (which will be smaller than the source value).
266 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
267 // to a specified boundary. The first operand is the token chain, the
268 // second is the number of bytes to allocate, and the third is the alignment
269 // boundary. The size is guaranteed to be a multiple of the stack
270 // alignment, and the alignment is guaranteed to be bigger than the stack
271 // alignment (if required) or 0 to get standard stack alignment.
274 // Control flow instructions. These all have token chains.
276 // BR - Unconditional branch. The first operand is the chain
277 // operand, the second is the MBB to branch to.
280 // BRCOND - Conditional branch. The first operand is the chain,
281 // the second is the condition, the third is the block to branch
282 // to if the condition is true.
285 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
286 // chain, the second is the condition, the third is the block to branch to
287 // if true, and the forth is the block to branch to if false. Targets
288 // usually do not implement this, preferring to have legalize demote the
289 // operation to BRCOND/BR pairs when necessary.
292 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
293 // that the condition is represented as condition code, and two nodes to
294 // compare, rather than as a combined SetCC node. The operands in order are
295 // chain, cc, lhs, rhs, block to branch to if condition is true.
298 // BRTWOWAY_CC - Two-way conditional branch. The operands in order are
299 // chain, cc, lhs, rhs, block to branch to if condition is true, block to
300 // branch to if condition is false. Targets usually do not implement this,
301 // preferring to have legalize demote the operation to BRCOND/BR pairs.
304 // RET - Return from function. The first operand is the chain,
305 // and any subsequent operands are the return values for the
306 // function. This operation can have variable number of operands.
309 // INLINEASM - Represents an inline asm block. This node always has two
310 // return values: a chain and a flag result. The inputs are as follows:
311 // Operand #0 : Input chain.
312 // Operand #1 : a ExternalSymbolSDNode with a pointer to the asm string.
313 // Operand #2n+2: A RegisterNode.
314 // Operand #2n+3: A TargetConstant, indicating if the reg is a use/def
315 // Operand #last: Optional, an incoming flag.
318 // STACKSAVE - STACKSAVE has one operand, an input chain. It produces a
319 // value, the same type as the pointer type for the system, and an output
323 // STACKRESTORE has two operands, an input chain and a pointer to restore to
324 // it returns an output chain.
327 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
328 // correspond to the operands of the LLVM intrinsic functions. The only
329 // result is a token chain. The alignment argument is guaranteed to be a
335 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
336 // a call sequence, and carry arbitrary information that target might want
337 // to know. The first operand is a chain, the rest are specified by the
338 // target and not touched by the DAG optimizers.
339 CALLSEQ_START, // Beginning of a call sequence
340 CALLSEQ_END, // End of a call sequence
342 // VAARG - VAARG has three operands: an input chain, a pointer, and a
343 // SRCVALUE. It returns a pair of values: the vaarg value and a new chain.
346 // VACOPY - VACOPY has five operands: an input chain, a destination pointer,
347 // a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the
351 // VAEND, VASTART - VAEND and VASTART have three operands: an input chain, a
352 // pointer, and a SRCVALUE.
355 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
356 // locations with their value. This allows one use alias analysis
357 // information in the backend.
360 // PCMARKER - This corresponds to the pcmarker intrinsic.
363 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
364 // The only operand is a chain and a value and a chain are produced. The
365 // value is the contents of the architecture specific cycle counter like
366 // register (or other high accuracy low latency clock source)
369 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM
370 // intrinsics of the same name. The first operand is a token chain, the
371 // other operands match the intrinsic. These produce a token chain in
372 // addition to a value (if any).
373 READPORT, WRITEPORT, READIO, WRITEIO,
375 // HANDLENODE node - Used as a handle for various purposes.
378 // LOCATION - This node is used to represent a source location for debug
379 // info. It takes token chain as input, then a line number, then a column
380 // number, then a filename, then a working dir. It produces a token chain
384 // DEBUG_LOC - This node is used to represent source line information
385 // embedded in the code. It takes a token chain as input, then a line
386 // number, then a column then a file id (provided by MachineDebugInfo.) It
387 // produces a token chain as output.
390 // DEBUG_LABEL - This node is used to mark a location in the code where a
391 // label should be generated for use by the debug information. It takes a
392 // token chain as input and then a unique id (provided by MachineDebugInfo.)
393 // It produces a token chain as output.
396 // BUILTIN_OP_END - This must be the last enum value in this list.
400 //===--------------------------------------------------------------------===//
401 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
402 /// below work out, when considering SETFALSE (something that never exists
403 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
404 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
405 /// to. If the "N" column is 1, the result of the comparison is undefined if
406 /// the input is a NAN.
408 /// All of these (except for the 'always folded ops') should be handled for
409 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
410 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
412 /// Note that these are laid out in a specific order to allow bit-twiddling
413 /// to transform conditions.
415 // Opcode N U L G E Intuitive operation
416 SETFALSE, // 0 0 0 0 Always false (always folded)
417 SETOEQ, // 0 0 0 1 True if ordered and equal
418 SETOGT, // 0 0 1 0 True if ordered and greater than
419 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
420 SETOLT, // 0 1 0 0 True if ordered and less than
421 SETOLE, // 0 1 0 1 True if ordered and less than or equal
422 SETONE, // 0 1 1 0 True if ordered and operands are unequal
423 SETO, // 0 1 1 1 True if ordered (no nans)
424 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
425 SETUEQ, // 1 0 0 1 True if unordered or equal
426 SETUGT, // 1 0 1 0 True if unordered or greater than
427 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
428 SETULT, // 1 1 0 0 True if unordered or less than
429 SETULE, // 1 1 0 1 True if unordered, less than, or equal
430 SETUNE, // 1 1 1 0 True if unordered or not equal
431 SETTRUE, // 1 1 1 1 Always true (always folded)
432 // Don't care operations: undefined if the input is a nan.
433 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
434 SETEQ, // 1 X 0 0 1 True if equal
435 SETGT, // 1 X 0 1 0 True if greater than
436 SETGE, // 1 X 0 1 1 True if greater than or equal
437 SETLT, // 1 X 1 0 0 True if less than
438 SETLE, // 1 X 1 0 1 True if less than or equal
439 SETNE, // 1 X 1 1 0 True if not equal
440 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
442 SETCC_INVALID, // Marker value.
445 /// isSignedIntSetCC - Return true if this is a setcc instruction that
446 /// performs a signed comparison when used with integer operands.
447 inline bool isSignedIntSetCC(CondCode Code) {
448 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
451 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
452 /// performs an unsigned comparison when used with integer operands.
453 inline bool isUnsignedIntSetCC(CondCode Code) {
454 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
457 /// isTrueWhenEqual - Return true if the specified condition returns true if
458 /// the two operands to the condition are equal. Note that if one of the two
459 /// operands is a NaN, this value is meaningless.
460 inline bool isTrueWhenEqual(CondCode Cond) {
461 return ((int)Cond & 1) != 0;
464 /// getUnorderedFlavor - This function returns 0 if the condition is always
465 /// false if an operand is a NaN, 1 if the condition is always true if the
466 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
468 inline unsigned getUnorderedFlavor(CondCode Cond) {
469 return ((int)Cond >> 3) & 3;
472 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
473 /// 'op' is a valid SetCC operation.
474 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
476 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
477 /// when given the operation for (X op Y).
478 CondCode getSetCCSwappedOperands(CondCode Operation);
480 /// getSetCCOrOperation - Return the result of a logical OR between different
481 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
482 /// function returns SETCC_INVALID if it is not possible to represent the
483 /// resultant comparison.
484 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
486 /// getSetCCAndOperation - Return the result of a logical AND between
487 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
488 /// function returns SETCC_INVALID if it is not possible to represent the
489 /// resultant comparison.
490 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
491 } // end llvm::ISD namespace
494 //===----------------------------------------------------------------------===//
495 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
496 /// values as the result of a computation. Many nodes return multiple values,
497 /// from loads (which define a token and a return value) to ADDC (which returns
498 /// a result and a carry value), to calls (which may return an arbitrary number
501 /// As such, each use of a SelectionDAG computation must indicate the node that
502 /// computes it as well as which return value to use from that node. This pair
503 /// of information is represented with the SDOperand value type.
507 SDNode *Val; // The node defining the value we are using.
508 unsigned ResNo; // Which return value of the node we are using.
510 SDOperand() : Val(0) {}
511 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
513 bool operator==(const SDOperand &O) const {
514 return Val == O.Val && ResNo == O.ResNo;
516 bool operator!=(const SDOperand &O) const {
517 return !operator==(O);
519 bool operator<(const SDOperand &O) const {
520 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
523 SDOperand getValue(unsigned R) const {
524 return SDOperand(Val, R);
527 /// getValueType - Return the ValueType of the referenced return value.
529 inline MVT::ValueType getValueType() const;
531 // Forwarding methods - These forward to the corresponding methods in SDNode.
532 inline unsigned getOpcode() const;
533 inline unsigned getNodeDepth() const;
534 inline unsigned getNumOperands() const;
535 inline const SDOperand &getOperand(unsigned i) const;
536 inline bool isTargetOpcode() const;
537 inline unsigned getTargetOpcode() const;
539 /// hasOneUse - Return true if there is exactly one operation using this
540 /// result value of the defining operator.
541 inline bool hasOneUse() const;
545 /// simplify_type specializations - Allow casting operators to work directly on
546 /// SDOperands as if they were SDNode*'s.
547 template<> struct simplify_type<SDOperand> {
548 typedef SDNode* SimpleType;
549 static SimpleType getSimplifiedValue(const SDOperand &Val) {
550 return static_cast<SimpleType>(Val.Val);
553 template<> struct simplify_type<const SDOperand> {
554 typedef SDNode* SimpleType;
555 static SimpleType getSimplifiedValue(const SDOperand &Val) {
556 return static_cast<SimpleType>(Val.Val);
561 /// SDNode - Represents one node in the SelectionDAG.
564 /// NodeType - The operation that this node performs.
566 unsigned short NodeType;
568 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
569 /// means that leaves have a depth of 1, things that use only leaves have a
571 unsigned short NodeDepth;
573 /// OperandList - The values that are used by this operation.
575 SDOperand *OperandList;
577 /// ValueList - The types of the values this node defines. SDNode's may
578 /// define multiple values simultaneously.
579 MVT::ValueType *ValueList;
581 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
582 unsigned short NumOperands, NumValues;
584 /// Prev/Next pointers - These pointers form the linked list of of the
585 /// AllNodes list in the current DAG.
587 friend struct ilist_traits<SDNode>;
589 /// Uses - These are all of the SDNode's that use a value produced by this
591 std::vector<SDNode*> Uses;
594 assert(NumOperands == 0 && "Operand list not cleared before deletion");
597 //===--------------------------------------------------------------------===//
600 unsigned getOpcode() const { return NodeType; }
601 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
602 unsigned getTargetOpcode() const {
603 assert(isTargetOpcode() && "Not a target opcode!");
604 return NodeType - ISD::BUILTIN_OP_END;
607 size_t use_size() const { return Uses.size(); }
608 bool use_empty() const { return Uses.empty(); }
609 bool hasOneUse() const { return Uses.size() == 1; }
611 /// getNodeDepth - Return the distance from this node to the leaves in the
612 /// graph. The leaves have a depth of 1.
613 unsigned getNodeDepth() const { return NodeDepth; }
615 typedef std::vector<SDNode*>::const_iterator use_iterator;
616 use_iterator use_begin() const { return Uses.begin(); }
617 use_iterator use_end() const { return Uses.end(); }
619 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
620 /// indicated value. This method ignores uses of other values defined by this
622 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
624 // isOnlyUse - Return true if this node is the only use of N.
625 bool isOnlyUse(SDNode *N) const;
627 /// getNumOperands - Return the number of values used by this operation.
629 unsigned getNumOperands() const { return NumOperands; }
631 const SDOperand &getOperand(unsigned Num) const {
632 assert(Num < NumOperands && "Invalid child # of SDNode!");
633 return OperandList[Num];
635 typedef const SDOperand* op_iterator;
636 op_iterator op_begin() const { return OperandList; }
637 op_iterator op_end() const { return OperandList+NumOperands; }
640 /// getNumValues - Return the number of values defined/returned by this
643 unsigned getNumValues() const { return NumValues; }
645 /// getValueType - Return the type of a specified result.
647 MVT::ValueType getValueType(unsigned ResNo) const {
648 assert(ResNo < NumValues && "Illegal result number!");
649 return ValueList[ResNo];
652 typedef const MVT::ValueType* value_iterator;
653 value_iterator value_begin() const { return ValueList; }
654 value_iterator value_end() const { return ValueList+NumValues; }
656 /// getOperationName - Return the opcode of this operation for printing.
658 const char* getOperationName(const SelectionDAG *G = 0) const;
660 void dump(const SelectionDAG *G) const;
662 static bool classof(const SDNode *) { return true; }
665 friend class SelectionDAG;
667 /// getValueTypeList - Return a pointer to the specified value type.
669 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
671 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
672 OperandList = 0; NumOperands = 0;
673 ValueList = getValueTypeList(VT);
677 SDNode(unsigned NT, SDOperand Op)
678 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
679 OperandList = new SDOperand[1];
682 Op.Val->Uses.push_back(this);
687 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
689 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
690 NodeDepth = N1.Val->getNodeDepth()+1;
692 NodeDepth = N2.Val->getNodeDepth()+1;
693 OperandList = new SDOperand[2];
697 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
702 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
704 unsigned ND = N1.Val->getNodeDepth();
705 if (ND < N2.Val->getNodeDepth())
706 ND = N2.Val->getNodeDepth();
707 if (ND < N3.Val->getNodeDepth())
708 ND = N3.Val->getNodeDepth();
711 OperandList = new SDOperand[3];
717 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
718 N3.Val->Uses.push_back(this);
723 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
725 unsigned ND = N1.Val->getNodeDepth();
726 if (ND < N2.Val->getNodeDepth())
727 ND = N2.Val->getNodeDepth();
728 if (ND < N3.Val->getNodeDepth())
729 ND = N3.Val->getNodeDepth();
730 if (ND < N4.Val->getNodeDepth())
731 ND = N4.Val->getNodeDepth();
734 OperandList = new SDOperand[4];
741 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
742 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
747 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
748 NumOperands = Nodes.size();
749 OperandList = new SDOperand[NumOperands];
752 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
753 OperandList[i] = Nodes[i];
754 SDNode *N = OperandList[i].Val;
755 N->Uses.push_back(this);
756 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
764 /// MorphNodeTo - This clears the return value and operands list, and sets the
765 /// opcode of the node to the specified value. This should only be used by
766 /// the SelectionDAG class.
767 void MorphNodeTo(unsigned Opc) {
772 // Clear the operands list, updating used nodes to remove this from their
774 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
775 I->Val->removeUser(this);
776 delete [] OperandList;
781 void setValueTypes(MVT::ValueType VT) {
782 assert(NumValues == 0 && "Should not have values yet!");
783 ValueList = getValueTypeList(VT);
786 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
787 assert(NumValues == 0 && "Should not have values yet!");
792 void setOperands(SDOperand Op0) {
793 assert(NumOperands == 0 && "Should not have operands yet!");
794 OperandList = new SDOperand[1];
795 OperandList[0] = Op0;
797 Op0.Val->Uses.push_back(this);
799 void setOperands(SDOperand Op0, SDOperand Op1) {
800 assert(NumOperands == 0 && "Should not have operands yet!");
801 OperandList = new SDOperand[2];
802 OperandList[0] = Op0;
803 OperandList[1] = Op1;
805 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
807 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
808 assert(NumOperands == 0 && "Should not have operands yet!");
809 OperandList = new SDOperand[3];
810 OperandList[0] = Op0;
811 OperandList[1] = Op1;
812 OperandList[2] = Op2;
814 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
815 Op2.Val->Uses.push_back(this);
817 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
818 assert(NumOperands == 0 && "Should not have operands yet!");
819 OperandList = new SDOperand[4];
820 OperandList[0] = Op0;
821 OperandList[1] = Op1;
822 OperandList[2] = Op2;
823 OperandList[3] = Op3;
825 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
826 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
828 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
830 assert(NumOperands == 0 && "Should not have operands yet!");
831 OperandList = new SDOperand[5];
832 OperandList[0] = Op0;
833 OperandList[1] = Op1;
834 OperandList[2] = Op2;
835 OperandList[3] = Op3;
836 OperandList[4] = Op4;
838 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
839 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
840 Op4.Val->Uses.push_back(this);
842 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
843 SDOperand Op4, SDOperand Op5) {
844 assert(NumOperands == 0 && "Should not have operands yet!");
845 OperandList = new SDOperand[6];
846 OperandList[0] = Op0;
847 OperandList[1] = Op1;
848 OperandList[2] = Op2;
849 OperandList[3] = Op3;
850 OperandList[4] = Op4;
851 OperandList[5] = Op5;
853 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
854 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
855 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
857 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
858 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
859 assert(NumOperands == 0 && "Should not have operands yet!");
860 OperandList = new SDOperand[7];
861 OperandList[0] = Op0;
862 OperandList[1] = Op1;
863 OperandList[2] = Op2;
864 OperandList[3] = Op3;
865 OperandList[4] = Op4;
866 OperandList[5] = Op5;
867 OperandList[6] = Op6;
869 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
870 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
871 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
872 Op6.Val->Uses.push_back(this);
874 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
875 SDOperand Op4, SDOperand Op5, SDOperand Op6, SDOperand Op7) {
876 assert(NumOperands == 0 && "Should not have operands yet!");
877 OperandList = new SDOperand[8];
878 OperandList[0] = Op0;
879 OperandList[1] = Op1;
880 OperandList[2] = Op2;
881 OperandList[3] = Op3;
882 OperandList[4] = Op4;
883 OperandList[5] = Op5;
884 OperandList[6] = Op6;
885 OperandList[7] = Op7;
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);
889 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
890 Op6.Val->Uses.push_back(this); Op7.Val->Uses.push_back(this);
893 void addUser(SDNode *User) {
894 Uses.push_back(User);
896 void removeUser(SDNode *User) {
897 // Remove this user from the operand's use list.
898 for (unsigned i = Uses.size(); ; --i) {
899 assert(i != 0 && "Didn't find user!");
900 if (Uses[i-1] == User) {
901 Uses[i-1] = Uses.back();
910 // Define inline functions from the SDOperand class.
912 inline unsigned SDOperand::getOpcode() const {
913 return Val->getOpcode();
915 inline unsigned SDOperand::getNodeDepth() const {
916 return Val->getNodeDepth();
918 inline MVT::ValueType SDOperand::getValueType() const {
919 return Val->getValueType(ResNo);
921 inline unsigned SDOperand::getNumOperands() const {
922 return Val->getNumOperands();
924 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
925 return Val->getOperand(i);
927 inline bool SDOperand::isTargetOpcode() const {
928 return Val->isTargetOpcode();
930 inline unsigned SDOperand::getTargetOpcode() const {
931 return Val->getTargetOpcode();
933 inline bool SDOperand::hasOneUse() const {
934 return Val->hasNUsesOfValue(1, ResNo);
937 /// HandleSDNode - This class is used to form a handle around another node that
938 /// is persistant and is updated across invocations of replaceAllUsesWith on its
939 /// operand. This node should be directly created by end-users and not added to
940 /// the AllNodes list.
941 class HandleSDNode : public SDNode {
943 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
945 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
948 SDOperand getValue() const { return getOperand(0); }
951 class StringSDNode : public SDNode {
954 friend class SelectionDAG;
955 StringSDNode(const std::string &val)
956 : SDNode(ISD::STRING, MVT::Other), Value(val) {
959 const std::string &getValue() const { return Value; }
960 static bool classof(const StringSDNode *) { return true; }
961 static bool classof(const SDNode *N) {
962 return N->getOpcode() == ISD::STRING;
966 class ConstantSDNode : public SDNode {
969 friend class SelectionDAG;
970 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
971 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
975 uint64_t getValue() const { return Value; }
977 int64_t getSignExtended() const {
978 unsigned Bits = MVT::getSizeInBits(getValueType(0));
979 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
982 bool isNullValue() const { return Value == 0; }
983 bool isAllOnesValue() const {
984 int NumBits = MVT::getSizeInBits(getValueType(0));
985 if (NumBits == 64) return Value+1 == 0;
986 return Value == (1ULL << NumBits)-1;
989 static bool classof(const ConstantSDNode *) { return true; }
990 static bool classof(const SDNode *N) {
991 return N->getOpcode() == ISD::Constant ||
992 N->getOpcode() == ISD::TargetConstant;
996 class ConstantFPSDNode : public SDNode {
999 friend class SelectionDAG;
1000 ConstantFPSDNode(bool isTarget, double val, MVT::ValueType VT)
1001 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, VT),
1006 double getValue() const { return Value; }
1008 /// isExactlyValue - We don't rely on operator== working on double values, as
1009 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1010 /// As such, this method can be used to do an exact bit-for-bit comparison of
1011 /// two floating point values.
1012 bool isExactlyValue(double V) const;
1014 static bool classof(const ConstantFPSDNode *) { return true; }
1015 static bool classof(const SDNode *N) {
1016 return N->getOpcode() == ISD::ConstantFP ||
1017 N->getOpcode() == ISD::TargetConstantFP;
1021 class GlobalAddressSDNode : public SDNode {
1022 GlobalValue *TheGlobal;
1025 friend class SelectionDAG;
1026 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
1028 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT) {
1029 TheGlobal = const_cast<GlobalValue*>(GA);
1034 GlobalValue *getGlobal() const { return TheGlobal; }
1035 int getOffset() const { return offset; }
1037 static bool classof(const GlobalAddressSDNode *) { return true; }
1038 static bool classof(const SDNode *N) {
1039 return N->getOpcode() == ISD::GlobalAddress ||
1040 N->getOpcode() == ISD::TargetGlobalAddress;
1045 class FrameIndexSDNode : public SDNode {
1048 friend class SelectionDAG;
1049 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
1050 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
1053 int getIndex() const { return FI; }
1055 static bool classof(const FrameIndexSDNode *) { return true; }
1056 static bool classof(const SDNode *N) {
1057 return N->getOpcode() == ISD::FrameIndex ||
1058 N->getOpcode() == ISD::TargetFrameIndex;
1062 class ConstantPoolSDNode : public SDNode {
1066 friend class SelectionDAG;
1067 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget)
1068 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1069 C(c), Alignment(0) {}
1070 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, unsigned Align,
1072 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1073 C(c), Alignment(Align) {}
1076 Constant *get() const { return C; }
1078 // Return the alignment of this constant pool object, which is either 0 (for
1079 // default alignment) or log2 of the desired value.
1080 unsigned getAlignment() const { return Alignment; }
1082 static bool classof(const ConstantPoolSDNode *) { return true; }
1083 static bool classof(const SDNode *N) {
1084 return N->getOpcode() == ISD::ConstantPool ||
1085 N->getOpcode() == ISD::TargetConstantPool;
1089 class BasicBlockSDNode : public SDNode {
1090 MachineBasicBlock *MBB;
1092 friend class SelectionDAG;
1093 BasicBlockSDNode(MachineBasicBlock *mbb)
1094 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1097 MachineBasicBlock *getBasicBlock() const { return MBB; }
1099 static bool classof(const BasicBlockSDNode *) { return true; }
1100 static bool classof(const SDNode *N) {
1101 return N->getOpcode() == ISD::BasicBlock;
1105 class SrcValueSDNode : public SDNode {
1109 friend class SelectionDAG;
1110 SrcValueSDNode(const Value* v, int o)
1111 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1114 const Value *getValue() const { return V; }
1115 int getOffset() const { return offset; }
1117 static bool classof(const SrcValueSDNode *) { return true; }
1118 static bool classof(const SDNode *N) {
1119 return N->getOpcode() == ISD::SRCVALUE;
1124 class RegisterSDNode : public SDNode {
1127 friend class SelectionDAG;
1128 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1129 : SDNode(ISD::Register, VT), Reg(reg) {}
1132 unsigned getReg() const { return Reg; }
1134 static bool classof(const RegisterSDNode *) { return true; }
1135 static bool classof(const SDNode *N) {
1136 return N->getOpcode() == ISD::Register;
1140 class ExternalSymbolSDNode : public SDNode {
1143 friend class SelectionDAG;
1144 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1145 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1150 const char *getSymbol() const { return Symbol; }
1152 static bool classof(const ExternalSymbolSDNode *) { return true; }
1153 static bool classof(const SDNode *N) {
1154 return N->getOpcode() == ISD::ExternalSymbol ||
1155 N->getOpcode() == ISD::TargetExternalSymbol;
1159 class CondCodeSDNode : public SDNode {
1160 ISD::CondCode Condition;
1162 friend class SelectionDAG;
1163 CondCodeSDNode(ISD::CondCode Cond)
1164 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1168 ISD::CondCode get() const { return Condition; }
1170 static bool classof(const CondCodeSDNode *) { return true; }
1171 static bool classof(const SDNode *N) {
1172 return N->getOpcode() == ISD::CONDCODE;
1176 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1177 /// to parameterize some operations.
1178 class VTSDNode : public SDNode {
1179 MVT::ValueType ValueType;
1181 friend class SelectionDAG;
1182 VTSDNode(MVT::ValueType VT)
1183 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1186 MVT::ValueType getVT() const { return ValueType; }
1188 static bool classof(const VTSDNode *) { return true; }
1189 static bool classof(const SDNode *N) {
1190 return N->getOpcode() == ISD::VALUETYPE;
1195 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1199 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1201 bool operator==(const SDNodeIterator& x) const {
1202 return Operand == x.Operand;
1204 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1206 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1207 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1208 Operand = I.Operand;
1212 pointer operator*() const {
1213 return Node->getOperand(Operand).Val;
1215 pointer operator->() const { return operator*(); }
1217 SDNodeIterator& operator++() { // Preincrement
1221 SDNodeIterator operator++(int) { // Postincrement
1222 SDNodeIterator tmp = *this; ++*this; return tmp;
1225 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1226 static SDNodeIterator end (SDNode *N) {
1227 return SDNodeIterator(N, N->getNumOperands());
1230 unsigned getOperand() const { return Operand; }
1231 const SDNode *getNode() const { return Node; }
1234 template <> struct GraphTraits<SDNode*> {
1235 typedef SDNode NodeType;
1236 typedef SDNodeIterator ChildIteratorType;
1237 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1238 static inline ChildIteratorType child_begin(NodeType *N) {
1239 return SDNodeIterator::begin(N);
1241 static inline ChildIteratorType child_end(NodeType *N) {
1242 return SDNodeIterator::end(N);
1247 struct ilist_traits<SDNode> {
1248 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1249 static SDNode *getNext(const SDNode *N) { return N->Next; }
1251 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1252 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1254 static SDNode *createSentinel() {
1255 return new SDNode(ISD::EntryToken, MVT::Other);
1257 static void destroySentinel(SDNode *N) { delete N; }
1258 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1261 void addNodeToList(SDNode *NTy) {}
1262 void removeNodeFromList(SDNode *NTy) {}
1263 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1264 const ilist_iterator<SDNode> &X,
1265 const ilist_iterator<SDNode> &Y) {}
1268 } // end llvm namespace