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/GraphTraits.h"
26 #include "llvm/ADT/iterator"
27 #include "llvm/Support/DataTypes.h"
35 class MachineBasicBlock;
37 template <typename T> struct simplify_type;
39 /// ISD namespace - This namespace contains an enum which represents all of the
40 /// SelectionDAG node types and value types.
43 //===--------------------------------------------------------------------===//
44 /// ISD::NodeType enum - This enum defines all of the operators valid in a
48 // EntryToken - This is the marker used to indicate the start of the region.
51 // Token factor - This node takes multiple tokens as input and produces a
52 // single token result. This is used to represent the fact that the operand
53 // operators are independent of each other.
56 // AssertSext, AssertZext - These nodes record if a register contains a
57 // value that has already been zero or sign extended from a narrower type.
58 // These nodes take two operands. The first is the node that has already
59 // been extended, and the second is a value type node indicating the width
61 AssertSext, AssertZext,
63 // Various leaf nodes.
64 Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
65 BasicBlock, ExternalSymbol, VALUETYPE, CONDCODE, Register,
67 // TargetConstant - Like Constant, but the DAG does not do any folding or
68 // simplification of the constant. This is used by the DAG->DAG selector.
71 // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
72 // anything else with this node, and this is valid in the target-specific
73 // dag, turning into a GlobalAddress operand.
78 // CopyToReg - This node has three operands: a chain, a register number to
79 // set to this value, and a value.
82 // CopyFromReg - This node indicates that the input value is a virtual or
83 // physical register that is defined outside of the scope of this
84 // SelectionDAG. The register is available from the RegSDNode object.
87 // ImplicitDef - This node indicates that the specified register is
88 // implicitly defined by some operation (e.g. its a live-in argument). The
89 // two operands to this are the token chain coming in and the register.
90 // The only result is the token chain going out.
93 // UNDEF - An undefined node
96 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
97 // a Constant, which is required to be operand #1), element of the aggregate
98 // value specified as operand #0. This is only for use before legalization,
99 // for values that will be broken into multiple registers.
102 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
103 // two values of the same integer value type, this produces a value twice as
104 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
108 // Simple binary arithmetic operators.
109 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
111 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
112 // an unsigned/signed value of type i[2*n], then return the top part.
115 // Bitwise operators.
116 AND, OR, XOR, SHL, SRA, SRL,
118 // Counting operators
124 // Select with condition operator - This selects between a true value and
125 // a false value (ops #2 and #3) based on the boolean result of comparing
126 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
127 // condition code in op #4, a CondCodeSDNode.
130 // SetCC operator - This evaluates to a boolean (i1) true value if the
131 // condition is true. The operands to this are the left and right operands
132 // to compare (ops #0, and #1) and the condition code to compare them with
133 // (op #2) as a CondCodeSDNode.
136 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
137 // broken into a multiple pieces each, and return the resulting pieces of
138 // doing an atomic add/sub operation. This is used to handle add/sub of
139 // expanded types. The operation ordering is:
140 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
141 ADD_PARTS, SUB_PARTS,
143 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
144 // integer shift operations, just like ADD/SUB_PARTS. The operation
146 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
147 SHL_PARTS, SRA_PARTS, SRL_PARTS,
149 // Conversion operators. These are all single input single output
150 // operations. For all of these, the result type must be strictly
151 // wider or narrower (depending on the operation) than the source
154 // SIGN_EXTEND - Used for integer types, replicating the sign bit
158 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
161 // TRUNCATE - Completely drop the high bits.
164 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
165 // depends on the first letter) to floating point.
169 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
170 // sign extend a small value in a large integer register (e.g. sign
171 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
172 // with the 7th bit). The size of the smaller type is indicated by the 1th
173 // operand, a ValueType node.
176 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
181 // FP_ROUND - Perform a rounding operation from the current
182 // precision down to the specified precision (currently always 64->32).
185 // FP_ROUND_INREG - This operator takes a floating point register, and
186 // rounds it to a floating point value. It then promotes it and returns it
187 // in a register of the same size. This operation effectively just discards
188 // excess precision. The type to round down to is specified by the 1th
189 // operation, a VTSDNode (currently always 64->32->64).
192 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
195 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
196 // absolute value, square root, sine and cosine operations.
197 FNEG, FABS, FSQRT, FSIN, FCOS,
199 // Other operators. LOAD and STORE have token chains as their first
200 // operand, then the same operands as an LLVM load/store instruction, then a
201 // SRCVALUE node that provides alias analysis information.
204 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
205 // memory and extend them to a larger value (e.g. load a byte into a word
206 // register). All three of these have four operands, a token chain, a
207 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
208 // indicating the type to load.
210 // SEXTLOAD loads the integer operand and sign extends it to a larger
211 // integer result type.
212 // ZEXTLOAD loads the integer operand and zero extends it to a larger
213 // integer result type.
214 // EXTLOAD is used for two things: floating point extending loads, and
215 // integer extending loads where it doesn't matter what the high
216 // bits are set to. The code generator is allowed to codegen this
217 // into whichever operation is more efficient.
218 EXTLOAD, SEXTLOAD, ZEXTLOAD,
220 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
221 // value and stores it to memory in one operation. This can be used for
222 // either integer or floating point operands. The first four operands of
223 // this are the same as a standard store. The fifth is the ValueType to
224 // store it as (which will be smaller than the source value).
227 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
228 // to a specified boundary. The first operand is the token chain, the
229 // second is the number of bytes to allocate, and the third is the alignment
230 // boundary. The size is guaranteed to be a multiple of the stack
231 // alignment, and the alignment is guaranteed to be bigger than the stack
232 // alignment (if required) or 0 to get standard stack alignment.
235 // Control flow instructions. These all have token chains.
237 // BR - Unconditional branch. The first operand is the chain
238 // operand, the second is the MBB to branch to.
241 // BRCOND - Conditional branch. The first operand is the chain,
242 // the second is the condition, the third is the block to branch
243 // to if the condition is true.
246 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
247 // chain, the second is the condition, the third is the block to branch to
248 // if true, and the forth is the block to branch to if false. Targets
249 // usually do not implement this, preferring to have legalize demote the
250 // operation to BRCOND/BR pairs when necessary.
253 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
254 // that the condition is represented as condition code, and two nodes to
255 // compare, rather than as a combined SetCC node. The operands in order are
256 // chain, cc, lhs, rhs, block to branch to if condition is true.
259 // BRTWOWAY_CC - Two-way conditional branch. The operands in order are
260 // chain, cc, lhs, rhs, block to branch to if condition is true, block to
261 // branch to if condition is false. Targets usually do not implement this,
262 // preferring to have legalize demote the operation to BRCOND/BR pairs.
265 // RET - Return from function. The first operand is the chain,
266 // and any subsequent operands are the return values for the
267 // function. This operation can have variable number of operands.
270 // CALL - Call to a function pointer. The first operand is the chain, the
271 // second is the destination function pointer (a GlobalAddress for a direct
272 // call). Arguments have already been lowered to explicit DAGs according to
273 // the calling convention in effect here. TAILCALL is the same as CALL, but
274 // the callee is known not to access the stack of the caller.
278 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
279 // correspond to the operands of the LLVM intrinsic functions. The only
280 // result is a token chain. The alignment argument is guaranteed to be a
286 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
287 // a call sequence, and carry arbitrary information that target might want
288 // to know. The first operand is a chain, the rest are specified by the
289 // target and not touched by the DAG optimizers.
290 CALLSEQ_START, // Beginning of a call sequence
291 CALLSEQ_END, // End of a call sequence
293 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
294 // locations with their value. This allows one use alias analysis
295 // information in the backend.
298 // PCMARKER - This corresponds to the pcmarker intrinsic.
301 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM
302 // intrinsics of the same name. The first operand is a token chain, the
303 // other operands match the intrinsic. These produce a token chain in
304 // addition to a value (if any).
305 READPORT, WRITEPORT, READIO, WRITEIO,
307 // BUILTIN_OP_END - This must be the last enum value in this list.
311 //===--------------------------------------------------------------------===//
312 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
313 /// below work out, when considering SETFALSE (something that never exists
314 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
315 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
316 /// to. If the "N" column is 1, the result of the comparison is undefined if
317 /// the input is a NAN.
319 /// All of these (except for the 'always folded ops') should be handled for
320 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
321 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
323 /// Note that these are laid out in a specific order to allow bit-twiddling
324 /// to transform conditions.
326 // Opcode N U L G E Intuitive operation
327 SETFALSE, // 0 0 0 0 Always false (always folded)
328 SETOEQ, // 0 0 0 1 True if ordered and equal
329 SETOGT, // 0 0 1 0 True if ordered and greater than
330 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
331 SETOLT, // 0 1 0 0 True if ordered and less than
332 SETOLE, // 0 1 0 1 True if ordered and less than or equal
333 SETONE, // 0 1 1 0 True if ordered and operands are unequal
334 SETO, // 0 1 1 1 True if ordered (no nans)
335 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
336 SETUEQ, // 1 0 0 1 True if unordered or equal
337 SETUGT, // 1 0 1 0 True if unordered or greater than
338 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
339 SETULT, // 1 1 0 0 True if unordered or less than
340 SETULE, // 1 1 0 1 True if unordered, less than, or equal
341 SETUNE, // 1 1 1 0 True if unordered or not equal
342 SETTRUE, // 1 1 1 1 Always true (always folded)
343 // Don't care operations: undefined if the input is a nan.
344 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
345 SETEQ, // 1 X 0 0 1 True if equal
346 SETGT, // 1 X 0 1 0 True if greater than
347 SETGE, // 1 X 0 1 1 True if greater than or equal
348 SETLT, // 1 X 1 0 0 True if less than
349 SETLE, // 1 X 1 0 1 True if less than or equal
350 SETNE, // 1 X 1 1 0 True if not equal
351 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
353 SETCC_INVALID, // Marker value.
356 /// isSignedIntSetCC - Return true if this is a setcc instruction that
357 /// performs a signed comparison when used with integer operands.
358 inline bool isSignedIntSetCC(CondCode Code) {
359 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
362 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
363 /// performs an unsigned comparison when used with integer operands.
364 inline bool isUnsignedIntSetCC(CondCode Code) {
365 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
368 /// isTrueWhenEqual - Return true if the specified condition returns true if
369 /// the two operands to the condition are equal. Note that if one of the two
370 /// operands is a NaN, this value is meaningless.
371 inline bool isTrueWhenEqual(CondCode Cond) {
372 return ((int)Cond & 1) != 0;
375 /// getUnorderedFlavor - This function returns 0 if the condition is always
376 /// false if an operand is a NaN, 1 if the condition is always true if the
377 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
379 inline unsigned getUnorderedFlavor(CondCode Cond) {
380 return ((int)Cond >> 3) & 3;
383 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
384 /// 'op' is a valid SetCC operation.
385 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
387 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
388 /// when given the operation for (X op Y).
389 CondCode getSetCCSwappedOperands(CondCode Operation);
391 /// getSetCCOrOperation - Return the result of a logical OR between different
392 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
393 /// function returns SETCC_INVALID if it is not possible to represent the
394 /// resultant comparison.
395 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
397 /// getSetCCAndOperation - Return the result of a logical AND between
398 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
399 /// function returns SETCC_INVALID if it is not possible to represent the
400 /// resultant comparison.
401 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
402 } // end llvm::ISD namespace
405 //===----------------------------------------------------------------------===//
406 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
407 /// values as the result of a computation. Many nodes return multiple values,
408 /// from loads (which define a token and a return value) to ADDC (which returns
409 /// a result and a carry value), to calls (which may return an arbitrary number
412 /// As such, each use of a SelectionDAG computation must indicate the node that
413 /// computes it as well as which return value to use from that node. This pair
414 /// of information is represented with the SDOperand value type.
418 SDNode *Val; // The node defining the value we are using.
419 unsigned ResNo; // Which return value of the node we are using.
421 SDOperand() : Val(0) {}
422 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
424 bool operator==(const SDOperand &O) const {
425 return Val == O.Val && ResNo == O.ResNo;
427 bool operator!=(const SDOperand &O) const {
428 return !operator==(O);
430 bool operator<(const SDOperand &O) const {
431 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
434 SDOperand getValue(unsigned R) const {
435 return SDOperand(Val, R);
438 /// getValueType - Return the ValueType of the referenced return value.
440 inline MVT::ValueType getValueType() const;
442 // Forwarding methods - These forward to the corresponding methods in SDNode.
443 inline unsigned getOpcode() const;
444 inline unsigned getNodeDepth() const;
445 inline unsigned getNumOperands() const;
446 inline const SDOperand &getOperand(unsigned i) const;
447 inline bool isTargetOpcode() const;
448 inline unsigned getTargetOpcode() const;
450 /// hasOneUse - Return true if there is exactly one operation using this
451 /// result value of the defining operator.
452 inline bool hasOneUse() const;
456 /// simplify_type specializations - Allow casting operators to work directly on
457 /// SDOperands as if they were SDNode*'s.
458 template<> struct simplify_type<SDOperand> {
459 typedef SDNode* SimpleType;
460 static SimpleType getSimplifiedValue(const SDOperand &Val) {
461 return static_cast<SimpleType>(Val.Val);
464 template<> struct simplify_type<const SDOperand> {
465 typedef SDNode* SimpleType;
466 static SimpleType getSimplifiedValue(const SDOperand &Val) {
467 return static_cast<SimpleType>(Val.Val);
472 /// SDNode - Represents one node in the SelectionDAG.
475 /// NodeType - The operation that this node performs.
477 unsigned short NodeType;
479 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
480 /// means that leaves have a depth of 1, things that use only leaves have a
482 unsigned short NodeDepth;
484 /// Operands - The values that are used by this operation.
486 std::vector<SDOperand> Operands;
488 /// Values - The types of the values this node defines. SDNode's may define
489 /// multiple values simultaneously.
490 std::vector<MVT::ValueType> Values;
492 /// Uses - These are all of the SDNode's that use a value produced by this
494 std::vector<SDNode*> Uses;
497 //===--------------------------------------------------------------------===//
500 unsigned getOpcode() const { return NodeType; }
501 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
502 unsigned getTargetOpcode() const {
503 assert(isTargetOpcode() && "Not a target opcode!");
504 return NodeType - ISD::BUILTIN_OP_END;
507 size_t use_size() const { return Uses.size(); }
508 bool use_empty() const { return Uses.empty(); }
509 bool hasOneUse() const { return Uses.size() == 1; }
511 /// getNodeDepth - Return the distance from this node to the leaves in the
512 /// graph. The leaves have a depth of 1.
513 unsigned getNodeDepth() const { return NodeDepth; }
515 typedef std::vector<SDNode*>::const_iterator use_iterator;
516 use_iterator use_begin() const { return Uses.begin(); }
517 use_iterator use_end() const { return Uses.end(); }
519 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
520 /// indicated value. This method ignores uses of other values defined by this
522 bool hasNUsesOfValue(unsigned NUses, unsigned Value);
524 /// getNumOperands - Return the number of values used by this operation.
526 unsigned getNumOperands() const { return Operands.size(); }
528 const SDOperand &getOperand(unsigned Num) {
529 assert(Num < Operands.size() && "Invalid child # of SDNode!");
530 return Operands[Num];
533 const SDOperand &getOperand(unsigned Num) const {
534 assert(Num < Operands.size() && "Invalid child # of SDNode!");
535 return Operands[Num];
537 typedef std::vector<SDOperand>::const_iterator op_iterator;
538 op_iterator op_begin() const { return Operands.begin(); }
539 op_iterator op_end() const { return Operands.end(); }
542 /// getNumValues - Return the number of values defined/returned by this
545 unsigned getNumValues() const { return Values.size(); }
547 /// getValueType - Return the type of a specified result.
549 MVT::ValueType getValueType(unsigned ResNo) const {
550 assert(ResNo < Values.size() && "Illegal result number!");
551 return Values[ResNo];
554 typedef std::vector<MVT::ValueType>::const_iterator value_iterator;
555 value_iterator value_begin() const { return Values.begin(); }
556 value_iterator value_end() const { return Values.end(); }
558 /// getOperationName - Return the opcode of this operation for printing.
560 const char* getOperationName(const SelectionDAG *G = 0) const;
562 void dump(const SelectionDAG *G) const;
564 static bool classof(const SDNode *) { return true; }
567 /// setAdjCallChain - This method should only be used by the legalizer.
568 void setAdjCallChain(SDOperand N);
571 friend class SelectionDAG;
573 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
575 Values.push_back(VT);
577 SDNode(unsigned NT, SDOperand Op)
578 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
579 Operands.reserve(1); Operands.push_back(Op);
580 Op.Val->Uses.push_back(this);
582 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
584 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
585 NodeDepth = N1.Val->getNodeDepth()+1;
587 NodeDepth = N2.Val->getNodeDepth()+1;
588 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
589 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
591 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
593 unsigned ND = N1.Val->getNodeDepth();
594 if (ND < N2.Val->getNodeDepth())
595 ND = N2.Val->getNodeDepth();
596 if (ND < N3.Val->getNodeDepth())
597 ND = N3.Val->getNodeDepth();
600 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
601 Operands.push_back(N3);
602 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
603 N3.Val->Uses.push_back(this);
605 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
607 unsigned ND = N1.Val->getNodeDepth();
608 if (ND < N2.Val->getNodeDepth())
609 ND = N2.Val->getNodeDepth();
610 if (ND < N3.Val->getNodeDepth())
611 ND = N3.Val->getNodeDepth();
612 if (ND < N4.Val->getNodeDepth())
613 ND = N4.Val->getNodeDepth();
616 Operands.reserve(4); Operands.push_back(N1); Operands.push_back(N2);
617 Operands.push_back(N3); Operands.push_back(N4);
618 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
619 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
621 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
622 Operands.swap(Nodes);
624 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
625 Operands[i].Val->Uses.push_back(this);
626 if (ND < Operands[i].Val->getNodeDepth())
627 ND = Operands[i].Val->getNodeDepth();
634 /// MorphNodeTo - This clears the return value and operands list, and sets the
635 /// opcode of the node to the specified value. This should only be used by
636 /// the SelectionDAG class.
637 void MorphNodeTo(unsigned Opc) {
641 // Clear the operands list, updating used nodes to remove this from their
643 while (!Operands.empty()) {
644 SDNode *O = Operands.back().Val;
650 void setValueTypes(MVT::ValueType VT) {
652 Values.push_back(VT);
654 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
656 Values.push_back(VT1);
657 Values.push_back(VT2);
659 /// Note: this method destroys the vector passed in.
660 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
661 std::swap(Values, VTs);
664 void setOperands(SDOperand Op0) {
666 Operands.push_back(Op0);
667 Op0.Val->Uses.push_back(this);
669 void setOperands(SDOperand Op0, SDOperand Op1) {
671 Operands.push_back(Op0);
672 Operands.push_back(Op1);
673 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
675 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
677 Operands.push_back(Op0);
678 Operands.push_back(Op1);
679 Operands.push_back(Op2);
680 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
681 Op2.Val->Uses.push_back(this);
683 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
685 Operands.push_back(Op0);
686 Operands.push_back(Op1);
687 Operands.push_back(Op2);
688 Operands.push_back(Op3);
689 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
690 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
692 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
695 Operands.push_back(Op0);
696 Operands.push_back(Op1);
697 Operands.push_back(Op2);
698 Operands.push_back(Op3);
699 Operands.push_back(Op4);
700 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
701 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
702 Op4.Val->Uses.push_back(this);
704 void addUser(SDNode *User) {
705 Uses.push_back(User);
707 void removeUser(SDNode *User) {
708 // Remove this user from the operand's use list.
709 for (unsigned i = Uses.size(); ; --i) {
710 assert(i != 0 && "Didn't find user!");
711 if (Uses[i-1] == User) {
712 Uses[i-1] = Uses.back();
721 // Define inline functions from the SDOperand class.
723 inline unsigned SDOperand::getOpcode() const {
724 return Val->getOpcode();
726 inline unsigned SDOperand::getNodeDepth() const {
727 return Val->getNodeDepth();
729 inline MVT::ValueType SDOperand::getValueType() const {
730 return Val->getValueType(ResNo);
732 inline unsigned SDOperand::getNumOperands() const {
733 return Val->getNumOperands();
735 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
736 return Val->getOperand(i);
738 inline bool SDOperand::isTargetOpcode() const {
739 return Val->isTargetOpcode();
741 inline unsigned SDOperand::getTargetOpcode() const {
742 return Val->getTargetOpcode();
744 inline bool SDOperand::hasOneUse() const {
745 return Val->hasNUsesOfValue(1, ResNo);
749 class ConstantSDNode : public SDNode {
752 friend class SelectionDAG;
753 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
754 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
758 uint64_t getValue() const { return Value; }
760 int64_t getSignExtended() const {
761 unsigned Bits = MVT::getSizeInBits(getValueType(0));
762 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
765 bool isNullValue() const { return Value == 0; }
766 bool isAllOnesValue() const {
767 int NumBits = MVT::getSizeInBits(getValueType(0));
768 if (NumBits == 64) return Value+1 == 0;
769 return Value == (1ULL << NumBits)-1;
772 static bool classof(const ConstantSDNode *) { return true; }
773 static bool classof(const SDNode *N) {
774 return N->getOpcode() == ISD::Constant ||
775 N->getOpcode() == ISD::TargetConstant;
779 class ConstantFPSDNode : public SDNode {
782 friend class SelectionDAG;
783 ConstantFPSDNode(double val, MVT::ValueType VT)
784 : SDNode(ISD::ConstantFP, VT), Value(val) {
788 double getValue() const { return Value; }
790 /// isExactlyValue - We don't rely on operator== working on double values, as
791 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
792 /// As such, this method can be used to do an exact bit-for-bit comparison of
793 /// two floating point values.
794 bool isExactlyValue(double V) const;
796 static bool classof(const ConstantFPSDNode *) { return true; }
797 static bool classof(const SDNode *N) {
798 return N->getOpcode() == ISD::ConstantFP;
802 class GlobalAddressSDNode : public SDNode {
803 GlobalValue *TheGlobal;
805 friend class SelectionDAG;
806 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT)
807 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT) {
808 TheGlobal = const_cast<GlobalValue*>(GA);
812 GlobalValue *getGlobal() const { return TheGlobal; }
814 static bool classof(const GlobalAddressSDNode *) { return true; }
815 static bool classof(const SDNode *N) {
816 return N->getOpcode() == ISD::GlobalAddress ||
817 N->getOpcode() == ISD::TargetGlobalAddress;
822 class FrameIndexSDNode : public SDNode {
825 friend class SelectionDAG;
826 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
827 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
830 int getIndex() const { return FI; }
832 static bool classof(const FrameIndexSDNode *) { return true; }
833 static bool classof(const SDNode *N) {
834 return N->getOpcode() == ISD::FrameIndex ||
835 N->getOpcode() == ISD::TargetFrameIndex;
839 class ConstantPoolSDNode : public SDNode {
842 friend class SelectionDAG;
843 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget)
844 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
848 Constant *get() const { return C; }
850 static bool classof(const ConstantPoolSDNode *) { return true; }
851 static bool classof(const SDNode *N) {
852 return N->getOpcode() == ISD::ConstantPool ||
853 N->getOpcode() == ISD::TargetConstantPool;
857 class BasicBlockSDNode : public SDNode {
858 MachineBasicBlock *MBB;
860 friend class SelectionDAG;
861 BasicBlockSDNode(MachineBasicBlock *mbb)
862 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
865 MachineBasicBlock *getBasicBlock() const { return MBB; }
867 static bool classof(const BasicBlockSDNode *) { return true; }
868 static bool classof(const SDNode *N) {
869 return N->getOpcode() == ISD::BasicBlock;
873 class SrcValueSDNode : public SDNode {
877 friend class SelectionDAG;
878 SrcValueSDNode(const Value* v, int o)
879 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
882 const Value *getValue() const { return V; }
883 int getOffset() const { return offset; }
885 static bool classof(const SrcValueSDNode *) { return true; }
886 static bool classof(const SDNode *N) {
887 return N->getOpcode() == ISD::SRCVALUE;
892 class RegisterSDNode : public SDNode {
895 friend class SelectionDAG;
896 RegisterSDNode(unsigned reg, MVT::ValueType VT)
897 : SDNode(ISD::Register, VT), Reg(reg) {}
900 unsigned getReg() const { return Reg; }
902 static bool classof(const RegisterSDNode *) { return true; }
903 static bool classof(const SDNode *N) {
904 return N->getOpcode() == ISD::Register;
908 class ExternalSymbolSDNode : public SDNode {
911 friend class SelectionDAG;
912 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
913 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
917 const char *getSymbol() const { return Symbol; }
919 static bool classof(const ExternalSymbolSDNode *) { return true; }
920 static bool classof(const SDNode *N) {
921 return N->getOpcode() == ISD::ExternalSymbol;
925 class CondCodeSDNode : public SDNode {
926 ISD::CondCode Condition;
928 friend class SelectionDAG;
929 CondCodeSDNode(ISD::CondCode Cond)
930 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
934 ISD::CondCode get() const { return Condition; }
936 static bool classof(const CondCodeSDNode *) { return true; }
937 static bool classof(const SDNode *N) {
938 return N->getOpcode() == ISD::CONDCODE;
942 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
943 /// to parameterize some operations.
944 class VTSDNode : public SDNode {
945 MVT::ValueType ValueType;
947 friend class SelectionDAG;
948 VTSDNode(MVT::ValueType VT)
949 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
952 MVT::ValueType getVT() const { return ValueType; }
954 static bool classof(const VTSDNode *) { return true; }
955 static bool classof(const SDNode *N) {
956 return N->getOpcode() == ISD::VALUETYPE;
961 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
965 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
967 bool operator==(const SDNodeIterator& x) const {
968 return Operand == x.Operand;
970 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
972 const SDNodeIterator &operator=(const SDNodeIterator &I) {
973 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
978 pointer operator*() const {
979 return Node->getOperand(Operand).Val;
981 pointer operator->() const { return operator*(); }
983 SDNodeIterator& operator++() { // Preincrement
987 SDNodeIterator operator++(int) { // Postincrement
988 SDNodeIterator tmp = *this; ++*this; return tmp;
991 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
992 static SDNodeIterator end (SDNode *N) {
993 return SDNodeIterator(N, N->getNumOperands());
996 unsigned getOperand() const { return Operand; }
997 const SDNode *getNode() const { return Node; }
1000 template <> struct GraphTraits<SDNode*> {
1001 typedef SDNode NodeType;
1002 typedef SDNodeIterator ChildIteratorType;
1003 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1004 static inline ChildIteratorType child_begin(NodeType *N) {
1005 return SDNodeIterator::begin(N);
1007 static inline ChildIteratorType child_end(NodeType *N) {
1008 return SDNodeIterator::end(N);
1012 } // end llvm namespace