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.
79 // CopyToReg - This node has three operands: a chain, a register number to
80 // set to this value, and a value.
83 // CopyFromReg - This node indicates that the input value is a virtual or
84 // physical register that is defined outside of the scope of this
85 // SelectionDAG. The register is available from the RegSDNode object.
88 // ImplicitDef - This node indicates that the specified register is
89 // implicitly defined by some operation (e.g. its a live-in argument). The
90 // two operands to this are the token chain coming in and the register.
91 // The only result is the token chain going out.
94 // UNDEF - An undefined node
97 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
98 // a Constant, which is required to be operand #1), element of the aggregate
99 // value specified as operand #0. This is only for use before legalization,
100 // for values that will be broken into multiple registers.
103 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
104 // two values of the same integer value type, this produces a value twice as
105 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
109 // Simple integer binary arithmetic operators.
110 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
112 // Simple binary floating point operators.
113 FADD, FSUB, FMUL, FDIV, FREM,
115 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
116 // an unsigned/signed value of type i[2*n], then return the top part.
119 // Bitwise operators.
120 AND, OR, XOR, SHL, SRA, SRL,
122 // Counting operators
128 // Select with condition operator - This selects between a true value and
129 // a false value (ops #2 and #3) based on the boolean result of comparing
130 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
131 // condition code in op #4, a CondCodeSDNode.
134 // SetCC operator - This evaluates to a boolean (i1) true value if the
135 // condition is true. The operands to this are the left and right operands
136 // to compare (ops #0, and #1) and the condition code to compare them with
137 // (op #2) as a CondCodeSDNode.
140 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
141 // broken into a multiple pieces each, and return the resulting pieces of
142 // doing an atomic add/sub operation. This is used to handle add/sub of
143 // expanded types. The operation ordering is:
144 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
145 ADD_PARTS, SUB_PARTS,
147 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
148 // integer shift operations, just like ADD/SUB_PARTS. The operation
150 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
151 SHL_PARTS, SRA_PARTS, SRL_PARTS,
153 // Conversion operators. These are all single input single output
154 // operations. For all of these, the result type must be strictly
155 // wider or narrower (depending on the operation) than the source
158 // SIGN_EXTEND - Used for integer types, replicating the sign bit
162 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
165 // ANY_EXTEND - Used for integer types. The high bits are undefined.
168 // TRUNCATE - Completely drop the high bits.
171 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
172 // depends on the first letter) to floating point.
176 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
177 // sign extend a small value in a large integer register (e.g. sign
178 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
179 // with the 7th bit). The size of the smaller type is indicated by the 1th
180 // operand, a ValueType node.
183 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
188 // FP_ROUND - Perform a rounding operation from the current
189 // precision down to the specified precision (currently always 64->32).
192 // FP_ROUND_INREG - This operator takes a floating point register, and
193 // rounds it to a floating point value. It then promotes it and returns it
194 // in a register of the same size. This operation effectively just discards
195 // excess precision. The type to round down to is specified by the 1th
196 // operation, a VTSDNode (currently always 64->32->64).
199 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
202 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
203 // absolute value, square root, sine and cosine operations.
204 FNEG, FABS, FSQRT, FSIN, FCOS,
206 // Other operators. LOAD and STORE have token chains as their first
207 // operand, then the same operands as an LLVM load/store instruction, then a
208 // SRCVALUE node that provides alias analysis information.
211 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
212 // memory and extend them to a larger value (e.g. load a byte into a word
213 // register). All three of these have four operands, a token chain, a
214 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
215 // indicating the type to load.
217 // SEXTLOAD loads the integer operand and sign extends it to a larger
218 // integer result type.
219 // ZEXTLOAD loads the integer operand and zero extends it to a larger
220 // integer result type.
221 // EXTLOAD is used for two things: floating point extending loads, and
222 // integer extending loads where it doesn't matter what the high
223 // bits are set to. The code generator is allowed to codegen this
224 // into whichever operation is more efficient.
225 EXTLOAD, SEXTLOAD, ZEXTLOAD,
227 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
228 // value and stores it to memory in one operation. This can be used for
229 // either integer or floating point operands. The first four operands of
230 // this are the same as a standard store. The fifth is the ValueType to
231 // store it as (which will be smaller than the source value).
234 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
235 // to a specified boundary. The first operand is the token chain, the
236 // second is the number of bytes to allocate, and the third is the alignment
237 // boundary. The size is guaranteed to be a multiple of the stack
238 // alignment, and the alignment is guaranteed to be bigger than the stack
239 // alignment (if required) or 0 to get standard stack alignment.
242 // Control flow instructions. These all have token chains.
244 // BR - Unconditional branch. The first operand is the chain
245 // operand, the second is the MBB to branch to.
248 // BRCOND - Conditional branch. The first operand is the chain,
249 // the second is the condition, the third is the block to branch
250 // to if the condition is true.
253 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
254 // chain, the second is the condition, the third is the block to branch to
255 // if true, and the forth is the block to branch to if false. Targets
256 // usually do not implement this, preferring to have legalize demote the
257 // operation to BRCOND/BR pairs when necessary.
260 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
261 // that the condition is represented as condition code, and two nodes to
262 // compare, rather than as a combined SetCC node. The operands in order are
263 // chain, cc, lhs, rhs, block to branch to if condition is true.
266 // BRTWOWAY_CC - Two-way conditional branch. The operands in order are
267 // chain, cc, lhs, rhs, block to branch to if condition is true, block to
268 // branch to if condition is false. Targets usually do not implement this,
269 // preferring to have legalize demote the operation to BRCOND/BR pairs.
272 // RET - Return from function. The first operand is the chain,
273 // and any subsequent operands are the return values for the
274 // function. This operation can have variable number of operands.
277 // CALL - Call to a function pointer. The first operand is the chain, the
278 // second is the destination function pointer (a GlobalAddress for a direct
279 // call). Arguments have already been lowered to explicit DAGs according to
280 // the calling convention in effect here. TAILCALL is the same as CALL, but
281 // the callee is known not to access the stack of the caller.
285 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
286 // correspond to the operands of the LLVM intrinsic functions. The only
287 // result is a token chain. The alignment argument is guaranteed to be a
293 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
294 // a call sequence, and carry arbitrary information that target might want
295 // to know. The first operand is a chain, the rest are specified by the
296 // target and not touched by the DAG optimizers.
297 CALLSEQ_START, // Beginning of a call sequence
298 CALLSEQ_END, // End of a call sequence
300 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
301 // locations with their value. This allows one use alias analysis
302 // information in the backend.
305 // PCMARKER - This corresponds to the pcmarker intrinsic.
308 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM
309 // intrinsics of the same name. The first operand is a token chain, the
310 // other operands match the intrinsic. These produce a token chain in
311 // addition to a value (if any).
312 READPORT, WRITEPORT, READIO, WRITEIO,
314 // HANDLENODE node - Used as a handle for various purposes.
317 // BUILTIN_OP_END - This must be the last enum value in this list.
321 //===--------------------------------------------------------------------===//
322 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
323 /// below work out, when considering SETFALSE (something that never exists
324 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
325 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
326 /// to. If the "N" column is 1, the result of the comparison is undefined if
327 /// the input is a NAN.
329 /// All of these (except for the 'always folded ops') should be handled for
330 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
331 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
333 /// Note that these are laid out in a specific order to allow bit-twiddling
334 /// to transform conditions.
336 // Opcode N U L G E Intuitive operation
337 SETFALSE, // 0 0 0 0 Always false (always folded)
338 SETOEQ, // 0 0 0 1 True if ordered and equal
339 SETOGT, // 0 0 1 0 True if ordered and greater than
340 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
341 SETOLT, // 0 1 0 0 True if ordered and less than
342 SETOLE, // 0 1 0 1 True if ordered and less than or equal
343 SETONE, // 0 1 1 0 True if ordered and operands are unequal
344 SETO, // 0 1 1 1 True if ordered (no nans)
345 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
346 SETUEQ, // 1 0 0 1 True if unordered or equal
347 SETUGT, // 1 0 1 0 True if unordered or greater than
348 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
349 SETULT, // 1 1 0 0 True if unordered or less than
350 SETULE, // 1 1 0 1 True if unordered, less than, or equal
351 SETUNE, // 1 1 1 0 True if unordered or not equal
352 SETTRUE, // 1 1 1 1 Always true (always folded)
353 // Don't care operations: undefined if the input is a nan.
354 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
355 SETEQ, // 1 X 0 0 1 True if equal
356 SETGT, // 1 X 0 1 0 True if greater than
357 SETGE, // 1 X 0 1 1 True if greater than or equal
358 SETLT, // 1 X 1 0 0 True if less than
359 SETLE, // 1 X 1 0 1 True if less than or equal
360 SETNE, // 1 X 1 1 0 True if not equal
361 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
363 SETCC_INVALID, // Marker value.
366 /// isSignedIntSetCC - Return true if this is a setcc instruction that
367 /// performs a signed comparison when used with integer operands.
368 inline bool isSignedIntSetCC(CondCode Code) {
369 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
372 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
373 /// performs an unsigned comparison when used with integer operands.
374 inline bool isUnsignedIntSetCC(CondCode Code) {
375 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
378 /// isTrueWhenEqual - Return true if the specified condition returns true if
379 /// the two operands to the condition are equal. Note that if one of the two
380 /// operands is a NaN, this value is meaningless.
381 inline bool isTrueWhenEqual(CondCode Cond) {
382 return ((int)Cond & 1) != 0;
385 /// getUnorderedFlavor - This function returns 0 if the condition is always
386 /// false if an operand is a NaN, 1 if the condition is always true if the
387 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
389 inline unsigned getUnorderedFlavor(CondCode Cond) {
390 return ((int)Cond >> 3) & 3;
393 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
394 /// 'op' is a valid SetCC operation.
395 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
397 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
398 /// when given the operation for (X op Y).
399 CondCode getSetCCSwappedOperands(CondCode Operation);
401 /// getSetCCOrOperation - Return the result of a logical OR between different
402 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
403 /// function returns SETCC_INVALID if it is not possible to represent the
404 /// resultant comparison.
405 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
407 /// getSetCCAndOperation - Return the result of a logical AND between
408 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
409 /// function returns SETCC_INVALID if it is not possible to represent the
410 /// resultant comparison.
411 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
412 } // end llvm::ISD namespace
415 //===----------------------------------------------------------------------===//
416 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
417 /// values as the result of a computation. Many nodes return multiple values,
418 /// from loads (which define a token and a return value) to ADDC (which returns
419 /// a result and a carry value), to calls (which may return an arbitrary number
422 /// As such, each use of a SelectionDAG computation must indicate the node that
423 /// computes it as well as which return value to use from that node. This pair
424 /// of information is represented with the SDOperand value type.
428 SDNode *Val; // The node defining the value we are using.
429 unsigned ResNo; // Which return value of the node we are using.
431 SDOperand() : Val(0) {}
432 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
434 bool operator==(const SDOperand &O) const {
435 return Val == O.Val && ResNo == O.ResNo;
437 bool operator!=(const SDOperand &O) const {
438 return !operator==(O);
440 bool operator<(const SDOperand &O) const {
441 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
444 SDOperand getValue(unsigned R) const {
445 return SDOperand(Val, R);
448 /// getValueType - Return the ValueType of the referenced return value.
450 inline MVT::ValueType getValueType() const;
452 // Forwarding methods - These forward to the corresponding methods in SDNode.
453 inline unsigned getOpcode() const;
454 inline unsigned getNodeDepth() const;
455 inline unsigned getNumOperands() const;
456 inline const SDOperand &getOperand(unsigned i) const;
457 inline bool isTargetOpcode() const;
458 inline unsigned getTargetOpcode() const;
460 /// hasOneUse - Return true if there is exactly one operation using this
461 /// result value of the defining operator.
462 inline bool hasOneUse() const;
466 /// simplify_type specializations - Allow casting operators to work directly on
467 /// SDOperands as if they were SDNode*'s.
468 template<> struct simplify_type<SDOperand> {
469 typedef SDNode* SimpleType;
470 static SimpleType getSimplifiedValue(const SDOperand &Val) {
471 return static_cast<SimpleType>(Val.Val);
474 template<> struct simplify_type<const SDOperand> {
475 typedef SDNode* SimpleType;
476 static SimpleType getSimplifiedValue(const SDOperand &Val) {
477 return static_cast<SimpleType>(Val.Val);
482 /// SDNode - Represents one node in the SelectionDAG.
485 /// NodeType - The operation that this node performs.
487 unsigned short NodeType;
489 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
490 /// means that leaves have a depth of 1, things that use only leaves have a
492 unsigned short NodeDepth;
494 /// Operands - The values that are used by this operation.
496 std::vector<SDOperand> Operands;
498 /// Values - The types of the values this node defines. SDNode's may define
499 /// multiple values simultaneously.
500 std::vector<MVT::ValueType> Values;
502 /// Uses - These are all of the SDNode's that use a value produced by this
504 std::vector<SDNode*> Uses;
507 //===--------------------------------------------------------------------===//
510 unsigned getOpcode() const { return NodeType; }
511 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
512 unsigned getTargetOpcode() const {
513 assert(isTargetOpcode() && "Not a target opcode!");
514 return NodeType - ISD::BUILTIN_OP_END;
517 size_t use_size() const { return Uses.size(); }
518 bool use_empty() const { return Uses.empty(); }
519 bool hasOneUse() const { return Uses.size() == 1; }
521 /// getNodeDepth - Return the distance from this node to the leaves in the
522 /// graph. The leaves have a depth of 1.
523 unsigned getNodeDepth() const { return NodeDepth; }
525 typedef std::vector<SDNode*>::const_iterator use_iterator;
526 use_iterator use_begin() const { return Uses.begin(); }
527 use_iterator use_end() const { return Uses.end(); }
529 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
530 /// indicated value. This method ignores uses of other values defined by this
532 bool hasNUsesOfValue(unsigned NUses, unsigned Value);
534 /// getNumOperands - Return the number of values used by this operation.
536 unsigned getNumOperands() const { return Operands.size(); }
538 const SDOperand &getOperand(unsigned Num) {
539 assert(Num < Operands.size() && "Invalid child # of SDNode!");
540 return Operands[Num];
543 const SDOperand &getOperand(unsigned Num) const {
544 assert(Num < Operands.size() && "Invalid child # of SDNode!");
545 return Operands[Num];
547 typedef std::vector<SDOperand>::const_iterator op_iterator;
548 op_iterator op_begin() const { return Operands.begin(); }
549 op_iterator op_end() const { return Operands.end(); }
552 /// getNumValues - Return the number of values defined/returned by this
555 unsigned getNumValues() const { return Values.size(); }
557 /// getValueType - Return the type of a specified result.
559 MVT::ValueType getValueType(unsigned ResNo) const {
560 assert(ResNo < Values.size() && "Illegal result number!");
561 return Values[ResNo];
564 typedef std::vector<MVT::ValueType>::const_iterator value_iterator;
565 value_iterator value_begin() const { return Values.begin(); }
566 value_iterator value_end() const { return Values.end(); }
568 /// getOperationName - Return the opcode of this operation for printing.
570 const char* getOperationName(const SelectionDAG *G = 0) const;
572 void dump(const SelectionDAG *G) const;
574 static bool classof(const SDNode *) { return true; }
577 /// setAdjCallChain - This method should only be used by the legalizer.
578 void setAdjCallChain(SDOperand N);
581 friend class SelectionDAG;
583 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
585 Values.push_back(VT);
587 SDNode(unsigned NT, SDOperand Op)
588 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
589 Operands.reserve(1); Operands.push_back(Op);
590 Op.Val->Uses.push_back(this);
592 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
594 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
595 NodeDepth = N1.Val->getNodeDepth()+1;
597 NodeDepth = N2.Val->getNodeDepth()+1;
598 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
599 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
601 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
603 unsigned ND = N1.Val->getNodeDepth();
604 if (ND < N2.Val->getNodeDepth())
605 ND = N2.Val->getNodeDepth();
606 if (ND < N3.Val->getNodeDepth())
607 ND = N3.Val->getNodeDepth();
610 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
611 Operands.push_back(N3);
612 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
613 N3.Val->Uses.push_back(this);
615 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
617 unsigned ND = N1.Val->getNodeDepth();
618 if (ND < N2.Val->getNodeDepth())
619 ND = N2.Val->getNodeDepth();
620 if (ND < N3.Val->getNodeDepth())
621 ND = N3.Val->getNodeDepth();
622 if (ND < N4.Val->getNodeDepth())
623 ND = N4.Val->getNodeDepth();
626 Operands.reserve(4); Operands.push_back(N1); Operands.push_back(N2);
627 Operands.push_back(N3); Operands.push_back(N4);
628 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
629 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
631 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
632 Operands.swap(Nodes);
634 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
635 Operands[i].Val->Uses.push_back(this);
636 if (ND < Operands[i].Val->getNodeDepth())
637 ND = Operands[i].Val->getNodeDepth();
644 /// MorphNodeTo - This clears the return value and operands list, and sets the
645 /// opcode of the node to the specified value. This should only be used by
646 /// the SelectionDAG class.
647 void MorphNodeTo(unsigned Opc) {
651 // Clear the operands list, updating used nodes to remove this from their
653 while (!Operands.empty()) {
654 SDNode *O = Operands.back().Val;
660 void setValueTypes(MVT::ValueType VT) {
662 Values.push_back(VT);
664 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
666 Values.push_back(VT1);
667 Values.push_back(VT2);
669 /// Note: this method destroys the vector passed in.
670 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
671 std::swap(Values, VTs);
674 void setOperands(SDOperand Op0) {
676 Operands.push_back(Op0);
677 Op0.Val->Uses.push_back(this);
679 void setOperands(SDOperand Op0, SDOperand Op1) {
681 Operands.push_back(Op0);
682 Operands.push_back(Op1);
683 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
685 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
687 Operands.push_back(Op0);
688 Operands.push_back(Op1);
689 Operands.push_back(Op2);
690 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
691 Op2.Val->Uses.push_back(this);
693 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 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
700 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
702 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
705 Operands.push_back(Op0);
706 Operands.push_back(Op1);
707 Operands.push_back(Op2);
708 Operands.push_back(Op3);
709 Operands.push_back(Op4);
710 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
711 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
712 Op4.Val->Uses.push_back(this);
714 void addUser(SDNode *User) {
715 Uses.push_back(User);
717 void removeUser(SDNode *User) {
718 // Remove this user from the operand's use list.
719 for (unsigned i = Uses.size(); ; --i) {
720 assert(i != 0 && "Didn't find user!");
721 if (Uses[i-1] == User) {
722 Uses[i-1] = Uses.back();
731 // Define inline functions from the SDOperand class.
733 inline unsigned SDOperand::getOpcode() const {
734 return Val->getOpcode();
736 inline unsigned SDOperand::getNodeDepth() const {
737 return Val->getNodeDepth();
739 inline MVT::ValueType SDOperand::getValueType() const {
740 return Val->getValueType(ResNo);
742 inline unsigned SDOperand::getNumOperands() const {
743 return Val->getNumOperands();
745 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
746 return Val->getOperand(i);
748 inline bool SDOperand::isTargetOpcode() const {
749 return Val->isTargetOpcode();
751 inline unsigned SDOperand::getTargetOpcode() const {
752 return Val->getTargetOpcode();
754 inline bool SDOperand::hasOneUse() const {
755 return Val->hasNUsesOfValue(1, ResNo);
758 /// HandleSDNode - This class is used to form a handle around another node that
759 /// is persistant and is updated across invocations of replaceAllUsesWith on its
760 /// operand. This node should be directly created by end-users and not added to
761 /// the AllNodes list.
762 class HandleSDNode : public SDNode {
764 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
766 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
769 SDOperand getValue() const { return getOperand(0); }
773 class ConstantSDNode : public SDNode {
776 friend class SelectionDAG;
777 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
778 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
782 uint64_t getValue() const { return Value; }
784 int64_t getSignExtended() const {
785 unsigned Bits = MVT::getSizeInBits(getValueType(0));
786 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
789 bool isNullValue() const { return Value == 0; }
790 bool isAllOnesValue() const {
791 int NumBits = MVT::getSizeInBits(getValueType(0));
792 if (NumBits == 64) return Value+1 == 0;
793 return Value == (1ULL << NumBits)-1;
796 static bool classof(const ConstantSDNode *) { return true; }
797 static bool classof(const SDNode *N) {
798 return N->getOpcode() == ISD::Constant ||
799 N->getOpcode() == ISD::TargetConstant;
803 class ConstantFPSDNode : public SDNode {
806 friend class SelectionDAG;
807 ConstantFPSDNode(double val, MVT::ValueType VT)
808 : SDNode(ISD::ConstantFP, VT), Value(val) {
812 double getValue() const { return Value; }
814 /// isExactlyValue - We don't rely on operator== working on double values, as
815 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
816 /// As such, this method can be used to do an exact bit-for-bit comparison of
817 /// two floating point values.
818 bool isExactlyValue(double V) const;
820 static bool classof(const ConstantFPSDNode *) { return true; }
821 static bool classof(const SDNode *N) {
822 return N->getOpcode() == ISD::ConstantFP;
826 class GlobalAddressSDNode : public SDNode {
827 GlobalValue *TheGlobal;
829 friend class SelectionDAG;
830 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT)
831 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT) {
832 TheGlobal = const_cast<GlobalValue*>(GA);
836 GlobalValue *getGlobal() const { return TheGlobal; }
838 static bool classof(const GlobalAddressSDNode *) { return true; }
839 static bool classof(const SDNode *N) {
840 return N->getOpcode() == ISD::GlobalAddress ||
841 N->getOpcode() == ISD::TargetGlobalAddress;
846 class FrameIndexSDNode : public SDNode {
849 friend class SelectionDAG;
850 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
851 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
854 int getIndex() const { return FI; }
856 static bool classof(const FrameIndexSDNode *) { return true; }
857 static bool classof(const SDNode *N) {
858 return N->getOpcode() == ISD::FrameIndex ||
859 N->getOpcode() == ISD::TargetFrameIndex;
863 class ConstantPoolSDNode : public SDNode {
866 friend class SelectionDAG;
867 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget)
868 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
872 Constant *get() const { return C; }
874 static bool classof(const ConstantPoolSDNode *) { return true; }
875 static bool classof(const SDNode *N) {
876 return N->getOpcode() == ISD::ConstantPool ||
877 N->getOpcode() == ISD::TargetConstantPool;
881 class BasicBlockSDNode : public SDNode {
882 MachineBasicBlock *MBB;
884 friend class SelectionDAG;
885 BasicBlockSDNode(MachineBasicBlock *mbb)
886 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
889 MachineBasicBlock *getBasicBlock() const { return MBB; }
891 static bool classof(const BasicBlockSDNode *) { return true; }
892 static bool classof(const SDNode *N) {
893 return N->getOpcode() == ISD::BasicBlock;
897 class SrcValueSDNode : public SDNode {
901 friend class SelectionDAG;
902 SrcValueSDNode(const Value* v, int o)
903 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
906 const Value *getValue() const { return V; }
907 int getOffset() const { return offset; }
909 static bool classof(const SrcValueSDNode *) { return true; }
910 static bool classof(const SDNode *N) {
911 return N->getOpcode() == ISD::SRCVALUE;
916 class RegisterSDNode : public SDNode {
919 friend class SelectionDAG;
920 RegisterSDNode(unsigned reg, MVT::ValueType VT)
921 : SDNode(ISD::Register, VT), Reg(reg) {}
924 unsigned getReg() const { return Reg; }
926 static bool classof(const RegisterSDNode *) { return true; }
927 static bool classof(const SDNode *N) {
928 return N->getOpcode() == ISD::Register;
932 class ExternalSymbolSDNode : public SDNode {
935 friend class SelectionDAG;
936 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
937 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
942 const char *getSymbol() const { return Symbol; }
944 static bool classof(const ExternalSymbolSDNode *) { return true; }
945 static bool classof(const SDNode *N) {
946 return N->getOpcode() == ISD::ExternalSymbol ||
947 N->getOpcode() == ISD::TargetExternalSymbol;
951 class CondCodeSDNode : public SDNode {
952 ISD::CondCode Condition;
954 friend class SelectionDAG;
955 CondCodeSDNode(ISD::CondCode Cond)
956 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
960 ISD::CondCode get() const { return Condition; }
962 static bool classof(const CondCodeSDNode *) { return true; }
963 static bool classof(const SDNode *N) {
964 return N->getOpcode() == ISD::CONDCODE;
968 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
969 /// to parameterize some operations.
970 class VTSDNode : public SDNode {
971 MVT::ValueType ValueType;
973 friend class SelectionDAG;
974 VTSDNode(MVT::ValueType VT)
975 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
978 MVT::ValueType getVT() const { return ValueType; }
980 static bool classof(const VTSDNode *) { return true; }
981 static bool classof(const SDNode *N) {
982 return N->getOpcode() == ISD::VALUETYPE;
987 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
991 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
993 bool operator==(const SDNodeIterator& x) const {
994 return Operand == x.Operand;
996 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
998 const SDNodeIterator &operator=(const SDNodeIterator &I) {
999 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1000 Operand = I.Operand;
1004 pointer operator*() const {
1005 return Node->getOperand(Operand).Val;
1007 pointer operator->() const { return operator*(); }
1009 SDNodeIterator& operator++() { // Preincrement
1013 SDNodeIterator operator++(int) { // Postincrement
1014 SDNodeIterator tmp = *this; ++*this; return tmp;
1017 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1018 static SDNodeIterator end (SDNode *N) {
1019 return SDNodeIterator(N, N->getNumOperands());
1022 unsigned getOperand() const { return Operand; }
1023 const SDNode *getNode() const { return Node; }
1026 template <> struct GraphTraits<SDNode*> {
1027 typedef SDNode NodeType;
1028 typedef SDNodeIterator ChildIteratorType;
1029 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1030 static inline ChildIteratorType child_begin(NodeType *N) {
1031 return SDNodeIterator::begin(N);
1033 static inline ChildIteratorType child_end(NodeType *N) {
1034 return SDNodeIterator::end(N);
1038 } // end llvm namespace