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. This is used by the DAG->DAG selector.
78 // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
79 // anything else with this node, and this is valid in the target-specific
80 // dag, turning into a GlobalAddress operand.
86 // CopyToReg - This node has three operands: a chain, a register number to
87 // set to this value, and a value.
90 // CopyFromReg - This node indicates that the input value is a virtual or
91 // physical register that is defined outside of the scope of this
92 // SelectionDAG. The register is available from the RegSDNode object.
95 // UNDEF - An undefined node
98 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
99 // a Constant, which is required to be operand #1), element of the aggregate
100 // value specified as operand #0. This is only for use before legalization,
101 // for values that will be broken into multiple registers.
104 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
105 // two values of the same integer value type, this produces a value twice as
106 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
109 // MERGE_VALUES - This node takes multiple discrete operands and returns
110 // them all as its individual results. This nodes has exactly the same
111 // number of inputs and outputs, and is only valid before legalization.
112 // This node is useful for some pieces of the code generator that want to
113 // think about a single node with multiple results, not multiple nodes.
116 // Simple integer binary arithmetic operators.
117 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
119 // Simple binary floating point operators.
120 FADD, FSUB, FMUL, FDIV, FREM,
122 // Simple abstract vector operators. Unlike the integer and floating point
123 // binary operators, these nodes also take two additional operands:
124 // a constant element count, and a value type node indicating the type of
125 // the elements. The order is op0, op1, count, type. All vector opcodes,
126 // including VLOAD, must currently have count and type as their 3rd and 4th
130 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
131 // an unsigned/signed value of type i[2*n], then return the top part.
134 // Bitwise operators - logical and, logical or, logical xor, shift left,
135 // shift right algebraic (shift in sign bits), shift right logical (shift in
136 // zeroes), rotate left, rotate right, and byteswap.
137 AND, OR, XOR, SHL, SRA, SRL, ROTL, ROTR, BSWAP,
139 // Counting operators
145 // Select with condition operator - This selects between a true value and
146 // a false value (ops #2 and #3) based on the boolean result of comparing
147 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
148 // condition code in op #4, a CondCodeSDNode.
151 // SetCC operator - This evaluates to a boolean (i1) true value if the
152 // condition is true. The operands to this are the left and right operands
153 // to compare (ops #0, and #1) and the condition code to compare them with
154 // (op #2) as a CondCodeSDNode.
157 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
158 // broken into a multiple pieces each, and return the resulting pieces of
159 // doing an atomic add/sub operation. This is used to handle add/sub of
160 // expanded types. The operation ordering is:
161 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
162 ADD_PARTS, SUB_PARTS,
164 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
165 // integer shift operations, just like ADD/SUB_PARTS. The operation
167 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
168 SHL_PARTS, SRA_PARTS, SRL_PARTS,
170 // Conversion operators. These are all single input single output
171 // operations. For all of these, the result type must be strictly
172 // wider or narrower (depending on the operation) than the source
175 // SIGN_EXTEND - Used for integer types, replicating the sign bit
179 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
182 // ANY_EXTEND - Used for integer types. The high bits are undefined.
185 // TRUNCATE - Completely drop the high bits.
188 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
189 // depends on the first letter) to floating point.
193 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
194 // sign extend a small value in a large integer register (e.g. sign
195 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
196 // with the 7th bit). The size of the smaller type is indicated by the 1th
197 // operand, a ValueType node.
200 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
205 // FP_ROUND - Perform a rounding operation from the current
206 // precision down to the specified precision (currently always 64->32).
209 // FP_ROUND_INREG - This operator takes a floating point register, and
210 // rounds it to a floating point value. It then promotes it and returns it
211 // in a register of the same size. This operation effectively just discards
212 // excess precision. The type to round down to is specified by the 1th
213 // operation, a VTSDNode (currently always 64->32->64).
216 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
219 // BIT_CONVERT - Theis operator converts between integer and FP values, as
220 // if one was stored to memory as integer and the other was loaded from the
221 // same address (or equivalently for vector format conversions, etc). The
222 // source and result are required to have the same bit size (e.g.
223 // f32 <-> i32). This can also be used for int-to-int or fp-to-fp
224 // conversions, but that is a noop, deleted by getNode().
227 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
228 // absolute value, square root, sine and cosine operations.
229 FNEG, FABS, FSQRT, FSIN, FCOS,
231 // Other operators. LOAD and STORE have token chains as their first
232 // operand, then the same operands as an LLVM load/store instruction, then a
233 // SRCVALUE node that provides alias analysis information.
236 // Abstract vector version of LOAD. VLOAD has a token chain as the first
237 // operand, followed by a pointer operand, a constant element count, a value
238 // type node indicating the type of the elements, and a SRCVALUE node.
241 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
242 // memory and extend them to a larger value (e.g. load a byte into a word
243 // register). All three of these have four operands, a token chain, a
244 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
245 // indicating the type to load.
247 // SEXTLOAD loads the integer operand and sign extends it to a larger
248 // integer result type.
249 // ZEXTLOAD loads the integer operand and zero extends it to a larger
250 // integer result type.
251 // EXTLOAD is used for two things: floating point extending loads, and
252 // integer extending loads where it doesn't matter what the high
253 // bits are set to. The code generator is allowed to codegen this
254 // into whichever operation is more efficient.
255 EXTLOAD, SEXTLOAD, ZEXTLOAD,
257 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
258 // value and stores it to memory in one operation. This can be used for
259 // either integer or floating point operands. The first four operands of
260 // this are the same as a standard store. The fifth is the ValueType to
261 // store it as (which will be smaller than the source value).
264 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
265 // to a specified boundary. The first operand is the token chain, the
266 // second is the number of bytes to allocate, and the third is the alignment
267 // boundary. The size is guaranteed to be a multiple of the stack
268 // alignment, and the alignment is guaranteed to be bigger than the stack
269 // alignment (if required) or 0 to get standard stack alignment.
272 // Control flow instructions. These all have token chains.
274 // BR - Unconditional branch. The first operand is the chain
275 // operand, the second is the MBB to branch to.
278 // BRCOND - Conditional branch. The first operand is the chain,
279 // the second is the condition, the third is the block to branch
280 // to if the condition is true.
283 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
284 // chain, the second is the condition, the third is the block to branch to
285 // if true, and the forth is the block to branch to if false. Targets
286 // usually do not implement this, preferring to have legalize demote the
287 // operation to BRCOND/BR pairs when necessary.
290 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
291 // that the condition is represented as condition code, and two nodes to
292 // compare, rather than as a combined SetCC node. The operands in order are
293 // chain, cc, lhs, rhs, block to branch to if condition is true.
296 // BRTWOWAY_CC - Two-way conditional branch. The operands in order are
297 // chain, cc, lhs, rhs, block to branch to if condition is true, block to
298 // branch to if condition is false. Targets usually do not implement this,
299 // preferring to have legalize demote the operation to BRCOND/BR pairs.
302 // RET - Return from function. The first operand is the chain,
303 // and any subsequent operands are the return values for the
304 // function. This operation can have variable number of operands.
307 // CALL - Call to a function pointer. The first operand is the chain, the
308 // second is the destination function pointer (a GlobalAddress for a direct
309 // call). Arguments have already been lowered to explicit DAGs according to
310 // the calling convention in effect here. TAILCALL is the same as CALL, but
311 // the callee is known not to access the stack of the caller.
315 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
316 // correspond to the operands of the LLVM intrinsic functions. The only
317 // result is a token chain. The alignment argument is guaranteed to be a
323 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
324 // a call sequence, and carry arbitrary information that target might want
325 // to know. The first operand is a chain, the rest are specified by the
326 // target and not touched by the DAG optimizers.
327 CALLSEQ_START, // Beginning of a call sequence
328 CALLSEQ_END, // End of a call sequence
330 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
331 // locations with their value. This allows one use alias analysis
332 // information in the backend.
335 // PCMARKER - This corresponds to the pcmarker intrinsic.
338 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
339 // The only operand is a chain and a value and a chain are produced. The
340 // value is the contents of the architecture specific cycle counter like
341 // register (or other high accuracy low latency clock source)
344 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM
345 // intrinsics of the same name. The first operand is a token chain, the
346 // other operands match the intrinsic. These produce a token chain in
347 // addition to a value (if any).
348 READPORT, WRITEPORT, READIO, WRITEIO,
350 // HANDLENODE node - Used as a handle for various purposes.
353 // LOCATION - This node is used to represent a source location for debug
354 // info. It takes token chain as input, then a line number, then a column
355 // number, then a filename, then a working dir. It produces a token chain
359 // DEBUG_LOC - This node is used to represent source line information
360 // embedded in the code. It takes a token chain as input, then a line
361 // number, then a column then a file id (provided by MachineDebugInfo.) It
362 // produces a token chain as output.
365 // DEBUG_LABEL - This node is used to mark a location in the code where a
366 // label should be generated for use by the debug information. It takes a
367 // token chain as input and then a unique id (provided by MachineDebugInfo.)
368 // It produces a token chain as output.
371 // BUILTIN_OP_END - This must be the last enum value in this list.
375 //===--------------------------------------------------------------------===//
376 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
377 /// below work out, when considering SETFALSE (something that never exists
378 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
379 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
380 /// to. If the "N" column is 1, the result of the comparison is undefined if
381 /// the input is a NAN.
383 /// All of these (except for the 'always folded ops') should be handled for
384 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
385 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
387 /// Note that these are laid out in a specific order to allow bit-twiddling
388 /// to transform conditions.
390 // Opcode N U L G E Intuitive operation
391 SETFALSE, // 0 0 0 0 Always false (always folded)
392 SETOEQ, // 0 0 0 1 True if ordered and equal
393 SETOGT, // 0 0 1 0 True if ordered and greater than
394 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
395 SETOLT, // 0 1 0 0 True if ordered and less than
396 SETOLE, // 0 1 0 1 True if ordered and less than or equal
397 SETONE, // 0 1 1 0 True if ordered and operands are unequal
398 SETO, // 0 1 1 1 True if ordered (no nans)
399 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
400 SETUEQ, // 1 0 0 1 True if unordered or equal
401 SETUGT, // 1 0 1 0 True if unordered or greater than
402 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
403 SETULT, // 1 1 0 0 True if unordered or less than
404 SETULE, // 1 1 0 1 True if unordered, less than, or equal
405 SETUNE, // 1 1 1 0 True if unordered or not equal
406 SETTRUE, // 1 1 1 1 Always true (always folded)
407 // Don't care operations: undefined if the input is a nan.
408 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
409 SETEQ, // 1 X 0 0 1 True if equal
410 SETGT, // 1 X 0 1 0 True if greater than
411 SETGE, // 1 X 0 1 1 True if greater than or equal
412 SETLT, // 1 X 1 0 0 True if less than
413 SETLE, // 1 X 1 0 1 True if less than or equal
414 SETNE, // 1 X 1 1 0 True if not equal
415 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
417 SETCC_INVALID, // Marker value.
420 /// isSignedIntSetCC - Return true if this is a setcc instruction that
421 /// performs a signed comparison when used with integer operands.
422 inline bool isSignedIntSetCC(CondCode Code) {
423 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
426 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
427 /// performs an unsigned comparison when used with integer operands.
428 inline bool isUnsignedIntSetCC(CondCode Code) {
429 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
432 /// isTrueWhenEqual - Return true if the specified condition returns true if
433 /// the two operands to the condition are equal. Note that if one of the two
434 /// operands is a NaN, this value is meaningless.
435 inline bool isTrueWhenEqual(CondCode Cond) {
436 return ((int)Cond & 1) != 0;
439 /// getUnorderedFlavor - This function returns 0 if the condition is always
440 /// false if an operand is a NaN, 1 if the condition is always true if the
441 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
443 inline unsigned getUnorderedFlavor(CondCode Cond) {
444 return ((int)Cond >> 3) & 3;
447 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
448 /// 'op' is a valid SetCC operation.
449 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
451 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
452 /// when given the operation for (X op Y).
453 CondCode getSetCCSwappedOperands(CondCode Operation);
455 /// getSetCCOrOperation - Return the result of a logical OR between different
456 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
457 /// function returns SETCC_INVALID if it is not possible to represent the
458 /// resultant comparison.
459 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
461 /// getSetCCAndOperation - Return the result of a logical AND between
462 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
463 /// function returns SETCC_INVALID if it is not possible to represent the
464 /// resultant comparison.
465 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
466 } // end llvm::ISD namespace
469 //===----------------------------------------------------------------------===//
470 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
471 /// values as the result of a computation. Many nodes return multiple values,
472 /// from loads (which define a token and a return value) to ADDC (which returns
473 /// a result and a carry value), to calls (which may return an arbitrary number
476 /// As such, each use of a SelectionDAG computation must indicate the node that
477 /// computes it as well as which return value to use from that node. This pair
478 /// of information is represented with the SDOperand value type.
482 SDNode *Val; // The node defining the value we are using.
483 unsigned ResNo; // Which return value of the node we are using.
485 SDOperand() : Val(0) {}
486 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
488 bool operator==(const SDOperand &O) const {
489 return Val == O.Val && ResNo == O.ResNo;
491 bool operator!=(const SDOperand &O) const {
492 return !operator==(O);
494 bool operator<(const SDOperand &O) const {
495 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
498 SDOperand getValue(unsigned R) const {
499 return SDOperand(Val, R);
502 /// getValueType - Return the ValueType of the referenced return value.
504 inline MVT::ValueType getValueType() const;
506 // Forwarding methods - These forward to the corresponding methods in SDNode.
507 inline unsigned getOpcode() const;
508 inline unsigned getNodeDepth() const;
509 inline unsigned getNumOperands() const;
510 inline const SDOperand &getOperand(unsigned i) const;
511 inline bool isTargetOpcode() const;
512 inline unsigned getTargetOpcode() const;
514 /// hasOneUse - Return true if there is exactly one operation using this
515 /// result value of the defining operator.
516 inline bool hasOneUse() const;
520 /// simplify_type specializations - Allow casting operators to work directly on
521 /// SDOperands as if they were SDNode*'s.
522 template<> struct simplify_type<SDOperand> {
523 typedef SDNode* SimpleType;
524 static SimpleType getSimplifiedValue(const SDOperand &Val) {
525 return static_cast<SimpleType>(Val.Val);
528 template<> struct simplify_type<const SDOperand> {
529 typedef SDNode* SimpleType;
530 static SimpleType getSimplifiedValue(const SDOperand &Val) {
531 return static_cast<SimpleType>(Val.Val);
536 /// SDNode - Represents one node in the SelectionDAG.
539 /// NodeType - The operation that this node performs.
541 unsigned short NodeType;
543 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
544 /// means that leaves have a depth of 1, things that use only leaves have a
546 unsigned short NodeDepth;
548 /// OperandList - The values that are used by this operation.
550 SDOperand *OperandList;
552 /// ValueList - The types of the values this node defines. SDNode's may
553 /// define multiple values simultaneously.
554 MVT::ValueType *ValueList;
556 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
557 unsigned short NumOperands, NumValues;
559 /// Prev/Next pointers - These pointers form the linked list of of the
560 /// AllNodes list in the current DAG.
562 friend struct ilist_traits<SDNode>;
564 /// Uses - These are all of the SDNode's that use a value produced by this
566 std::vector<SDNode*> Uses;
569 assert(NumOperands == 0 && "Operand list not cleared before deletion");
572 //===--------------------------------------------------------------------===//
575 unsigned getOpcode() const { return NodeType; }
576 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
577 unsigned getTargetOpcode() const {
578 assert(isTargetOpcode() && "Not a target opcode!");
579 return NodeType - ISD::BUILTIN_OP_END;
582 size_t use_size() const { return Uses.size(); }
583 bool use_empty() const { return Uses.empty(); }
584 bool hasOneUse() const { return Uses.size() == 1; }
586 /// getNodeDepth - Return the distance from this node to the leaves in the
587 /// graph. The leaves have a depth of 1.
588 unsigned getNodeDepth() const { return NodeDepth; }
590 typedef std::vector<SDNode*>::const_iterator use_iterator;
591 use_iterator use_begin() const { return Uses.begin(); }
592 use_iterator use_end() const { return Uses.end(); }
594 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
595 /// indicated value. This method ignores uses of other values defined by this
597 bool hasNUsesOfValue(unsigned NUses, unsigned Value);
599 /// getNumOperands - Return the number of values used by this operation.
601 unsigned getNumOperands() const { return NumOperands; }
603 const SDOperand &getOperand(unsigned Num) const {
604 assert(Num < NumOperands && "Invalid child # of SDNode!");
605 return OperandList[Num];
607 typedef const SDOperand* op_iterator;
608 op_iterator op_begin() const { return OperandList; }
609 op_iterator op_end() const { return OperandList+NumOperands; }
612 /// getNumValues - Return the number of values defined/returned by this
615 unsigned getNumValues() const { return NumValues; }
617 /// getValueType - Return the type of a specified result.
619 MVT::ValueType getValueType(unsigned ResNo) const {
620 assert(ResNo < NumValues && "Illegal result number!");
621 return ValueList[ResNo];
624 typedef const MVT::ValueType* value_iterator;
625 value_iterator value_begin() const { return ValueList; }
626 value_iterator value_end() const { return ValueList+NumValues; }
628 /// getOperationName - Return the opcode of this operation for printing.
630 const char* getOperationName(const SelectionDAG *G = 0) const;
632 void dump(const SelectionDAG *G) const;
634 static bool classof(const SDNode *) { return true; }
637 /// setAdjCallChain - This method should only be used by the legalizer.
638 void setAdjCallChain(SDOperand N);
641 friend class SelectionDAG;
643 /// getValueTypeList - Return a pointer to the specified value type.
645 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
647 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
648 OperandList = 0; NumOperands = 0;
649 ValueList = getValueTypeList(VT);
653 SDNode(unsigned NT, SDOperand Op)
654 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
655 OperandList = new SDOperand[1];
658 Op.Val->Uses.push_back(this);
663 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
665 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
666 NodeDepth = N1.Val->getNodeDepth()+1;
668 NodeDepth = N2.Val->getNodeDepth()+1;
669 OperandList = new SDOperand[2];
673 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
678 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
680 unsigned ND = N1.Val->getNodeDepth();
681 if (ND < N2.Val->getNodeDepth())
682 ND = N2.Val->getNodeDepth();
683 if (ND < N3.Val->getNodeDepth())
684 ND = N3.Val->getNodeDepth();
687 OperandList = new SDOperand[3];
693 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
694 N3.Val->Uses.push_back(this);
699 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
701 unsigned ND = N1.Val->getNodeDepth();
702 if (ND < N2.Val->getNodeDepth())
703 ND = N2.Val->getNodeDepth();
704 if (ND < N3.Val->getNodeDepth())
705 ND = N3.Val->getNodeDepth();
706 if (ND < N4.Val->getNodeDepth())
707 ND = N4.Val->getNodeDepth();
710 OperandList = new SDOperand[4];
717 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
718 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
723 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
724 NumOperands = Nodes.size();
725 OperandList = new SDOperand[NumOperands];
728 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
729 OperandList[i] = Nodes[i];
730 SDNode *N = OperandList[i].Val;
731 N->Uses.push_back(this);
732 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
740 /// MorphNodeTo - This clears the return value and operands list, and sets the
741 /// opcode of the node to the specified value. This should only be used by
742 /// the SelectionDAG class.
743 void MorphNodeTo(unsigned Opc) {
748 // Clear the operands list, updating used nodes to remove this from their
750 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
751 I->Val->removeUser(this);
752 delete [] OperandList;
757 void setValueTypes(MVT::ValueType VT) {
758 assert(NumValues == 0 && "Should not have values yet!");
759 ValueList = getValueTypeList(VT);
762 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
763 assert(NumValues == 0 && "Should not have values yet!");
768 void setOperands(SDOperand Op0) {
769 assert(NumOperands == 0 && "Should not have operands yet!");
770 OperandList = new SDOperand[1];
771 OperandList[0] = Op0;
773 Op0.Val->Uses.push_back(this);
775 void setOperands(SDOperand Op0, SDOperand Op1) {
776 assert(NumOperands == 0 && "Should not have operands yet!");
777 OperandList = new SDOperand[2];
778 OperandList[0] = Op0;
779 OperandList[1] = Op1;
781 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
783 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
784 assert(NumOperands == 0 && "Should not have operands yet!");
785 OperandList = new SDOperand[3];
786 OperandList[0] = Op0;
787 OperandList[1] = Op1;
788 OperandList[2] = Op2;
790 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
791 Op2.Val->Uses.push_back(this);
793 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
794 assert(NumOperands == 0 && "Should not have operands yet!");
795 OperandList = new SDOperand[4];
796 OperandList[0] = Op0;
797 OperandList[1] = Op1;
798 OperandList[2] = Op2;
799 OperandList[3] = Op3;
801 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
802 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
804 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
806 assert(NumOperands == 0 && "Should not have operands yet!");
807 OperandList = new SDOperand[5];
808 OperandList[0] = Op0;
809 OperandList[1] = Op1;
810 OperandList[2] = Op2;
811 OperandList[3] = Op3;
812 OperandList[4] = Op4;
814 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
815 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
816 Op4.Val->Uses.push_back(this);
818 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
819 SDOperand Op4, SDOperand Op5) {
820 assert(NumOperands == 0 && "Should not have operands yet!");
821 OperandList = new SDOperand[6];
822 OperandList[0] = Op0;
823 OperandList[1] = Op1;
824 OperandList[2] = Op2;
825 OperandList[3] = Op3;
826 OperandList[4] = Op4;
827 OperandList[5] = Op5;
829 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
830 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
831 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this);
833 void addUser(SDNode *User) {
834 Uses.push_back(User);
836 void removeUser(SDNode *User) {
837 // Remove this user from the operand's use list.
838 for (unsigned i = Uses.size(); ; --i) {
839 assert(i != 0 && "Didn't find user!");
840 if (Uses[i-1] == User) {
841 Uses[i-1] = Uses.back();
850 // Define inline functions from the SDOperand class.
852 inline unsigned SDOperand::getOpcode() const {
853 return Val->getOpcode();
855 inline unsigned SDOperand::getNodeDepth() const {
856 return Val->getNodeDepth();
858 inline MVT::ValueType SDOperand::getValueType() const {
859 return Val->getValueType(ResNo);
861 inline unsigned SDOperand::getNumOperands() const {
862 return Val->getNumOperands();
864 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
865 return Val->getOperand(i);
867 inline bool SDOperand::isTargetOpcode() const {
868 return Val->isTargetOpcode();
870 inline unsigned SDOperand::getTargetOpcode() const {
871 return Val->getTargetOpcode();
873 inline bool SDOperand::hasOneUse() const {
874 return Val->hasNUsesOfValue(1, ResNo);
877 /// HandleSDNode - This class is used to form a handle around another node that
878 /// is persistant and is updated across invocations of replaceAllUsesWith on its
879 /// operand. This node should be directly created by end-users and not added to
880 /// the AllNodes list.
881 class HandleSDNode : public SDNode {
883 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
885 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
888 SDOperand getValue() const { return getOperand(0); }
891 class StringSDNode : public SDNode {
894 friend class SelectionDAG;
895 StringSDNode(const std::string &val)
896 : SDNode(ISD::STRING, MVT::Other), Value(val) {
899 const std::string &getValue() const { return Value; }
900 static bool classof(const StringSDNode *) { return true; }
901 static bool classof(const SDNode *N) {
902 return N->getOpcode() == ISD::STRING;
906 class ConstantSDNode : public SDNode {
909 friend class SelectionDAG;
910 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
911 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
915 uint64_t getValue() const { return Value; }
917 int64_t getSignExtended() const {
918 unsigned Bits = MVT::getSizeInBits(getValueType(0));
919 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
922 bool isNullValue() const { return Value == 0; }
923 bool isAllOnesValue() const {
924 int NumBits = MVT::getSizeInBits(getValueType(0));
925 if (NumBits == 64) return Value+1 == 0;
926 return Value == (1ULL << NumBits)-1;
929 static bool classof(const ConstantSDNode *) { return true; }
930 static bool classof(const SDNode *N) {
931 return N->getOpcode() == ISD::Constant ||
932 N->getOpcode() == ISD::TargetConstant;
936 class ConstantFPSDNode : public SDNode {
939 friend class SelectionDAG;
940 ConstantFPSDNode(double val, MVT::ValueType VT)
941 : SDNode(ISD::ConstantFP, VT), Value(val) {
945 double getValue() const { return Value; }
947 /// isExactlyValue - We don't rely on operator== working on double values, as
948 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
949 /// As such, this method can be used to do an exact bit-for-bit comparison of
950 /// two floating point values.
951 bool isExactlyValue(double V) const;
953 static bool classof(const ConstantFPSDNode *) { return true; }
954 static bool classof(const SDNode *N) {
955 return N->getOpcode() == ISD::ConstantFP;
959 class GlobalAddressSDNode : public SDNode {
960 GlobalValue *TheGlobal;
963 friend class SelectionDAG;
964 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT,
966 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT) {
967 TheGlobal = const_cast<GlobalValue*>(GA);
972 GlobalValue *getGlobal() const { return TheGlobal; }
973 int getOffset() const { return offset; }
975 static bool classof(const GlobalAddressSDNode *) { return true; }
976 static bool classof(const SDNode *N) {
977 return N->getOpcode() == ISD::GlobalAddress ||
978 N->getOpcode() == ISD::TargetGlobalAddress;
983 class FrameIndexSDNode : public SDNode {
986 friend class SelectionDAG;
987 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
988 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
991 int getIndex() const { return FI; }
993 static bool classof(const FrameIndexSDNode *) { return true; }
994 static bool classof(const SDNode *N) {
995 return N->getOpcode() == ISD::FrameIndex ||
996 N->getOpcode() == ISD::TargetFrameIndex;
1000 class ConstantPoolSDNode : public SDNode {
1003 friend class SelectionDAG;
1004 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget)
1005 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
1009 Constant *get() const { return C; }
1011 static bool classof(const ConstantPoolSDNode *) { return true; }
1012 static bool classof(const SDNode *N) {
1013 return N->getOpcode() == ISD::ConstantPool ||
1014 N->getOpcode() == ISD::TargetConstantPool;
1018 class BasicBlockSDNode : public SDNode {
1019 MachineBasicBlock *MBB;
1021 friend class SelectionDAG;
1022 BasicBlockSDNode(MachineBasicBlock *mbb)
1023 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
1026 MachineBasicBlock *getBasicBlock() const { return MBB; }
1028 static bool classof(const BasicBlockSDNode *) { return true; }
1029 static bool classof(const SDNode *N) {
1030 return N->getOpcode() == ISD::BasicBlock;
1034 class SrcValueSDNode : public SDNode {
1038 friend class SelectionDAG;
1039 SrcValueSDNode(const Value* v, int o)
1040 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
1043 const Value *getValue() const { return V; }
1044 int getOffset() const { return offset; }
1046 static bool classof(const SrcValueSDNode *) { return true; }
1047 static bool classof(const SDNode *N) {
1048 return N->getOpcode() == ISD::SRCVALUE;
1053 class RegisterSDNode : public SDNode {
1056 friend class SelectionDAG;
1057 RegisterSDNode(unsigned reg, MVT::ValueType VT)
1058 : SDNode(ISD::Register, VT), Reg(reg) {}
1061 unsigned getReg() const { return Reg; }
1063 static bool classof(const RegisterSDNode *) { return true; }
1064 static bool classof(const SDNode *N) {
1065 return N->getOpcode() == ISD::Register;
1069 class ExternalSymbolSDNode : public SDNode {
1072 friend class SelectionDAG;
1073 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1074 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1079 const char *getSymbol() const { return Symbol; }
1081 static bool classof(const ExternalSymbolSDNode *) { return true; }
1082 static bool classof(const SDNode *N) {
1083 return N->getOpcode() == ISD::ExternalSymbol ||
1084 N->getOpcode() == ISD::TargetExternalSymbol;
1088 class CondCodeSDNode : public SDNode {
1089 ISD::CondCode Condition;
1091 friend class SelectionDAG;
1092 CondCodeSDNode(ISD::CondCode Cond)
1093 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1097 ISD::CondCode get() const { return Condition; }
1099 static bool classof(const CondCodeSDNode *) { return true; }
1100 static bool classof(const SDNode *N) {
1101 return N->getOpcode() == ISD::CONDCODE;
1105 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1106 /// to parameterize some operations.
1107 class VTSDNode : public SDNode {
1108 MVT::ValueType ValueType;
1110 friend class SelectionDAG;
1111 VTSDNode(MVT::ValueType VT)
1112 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1115 MVT::ValueType getVT() const { return ValueType; }
1117 static bool classof(const VTSDNode *) { return true; }
1118 static bool classof(const SDNode *N) {
1119 return N->getOpcode() == ISD::VALUETYPE;
1124 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1128 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1130 bool operator==(const SDNodeIterator& x) const {
1131 return Operand == x.Operand;
1133 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1135 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1136 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1137 Operand = I.Operand;
1141 pointer operator*() const {
1142 return Node->getOperand(Operand).Val;
1144 pointer operator->() const { return operator*(); }
1146 SDNodeIterator& operator++() { // Preincrement
1150 SDNodeIterator operator++(int) { // Postincrement
1151 SDNodeIterator tmp = *this; ++*this; return tmp;
1154 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1155 static SDNodeIterator end (SDNode *N) {
1156 return SDNodeIterator(N, N->getNumOperands());
1159 unsigned getOperand() const { return Operand; }
1160 const SDNode *getNode() const { return Node; }
1163 template <> struct GraphTraits<SDNode*> {
1164 typedef SDNode NodeType;
1165 typedef SDNodeIterator ChildIteratorType;
1166 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1167 static inline ChildIteratorType child_begin(NodeType *N) {
1168 return SDNodeIterator::begin(N);
1170 static inline ChildIteratorType child_end(NodeType *N) {
1171 return SDNodeIterator::end(N);
1176 struct ilist_traits<SDNode> {
1177 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1178 static SDNode *getNext(const SDNode *N) { return N->Next; }
1180 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1181 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1183 static SDNode *createSentinel() {
1184 return new SDNode(ISD::EntryToken, MVT::Other);
1186 static void destroySentinel(SDNode *N) { delete N; }
1187 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1190 void addNodeToList(SDNode *NTy) {}
1191 void removeNodeFromList(SDNode *NTy) {}
1192 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1193 const ilist_iterator<SDNode> &X,
1194 const ilist_iterator<SDNode> &Y) {}
1197 } // end llvm namespace