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, GlobalAddress, FrameIndex, ConstantPool,
67 BasicBlock, ExternalSymbol, VALUETYPE, CONDCODE, Register,
69 // TargetConstant - Like Constant, but the DAG does not do any folding or
70 // simplification of the constant. This is used by the DAG->DAG selector.
73 // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or
74 // anything else with this node, and this is valid in the target-specific
75 // dag, turning into a GlobalAddress operand.
81 // CopyToReg - This node has three operands: a chain, a register number to
82 // set to this value, and a value.
85 // CopyFromReg - This node indicates that the input value is a virtual or
86 // physical register that is defined outside of the scope of this
87 // SelectionDAG. The register is available from the RegSDNode object.
90 // ImplicitDef - This node indicates that the specified register is
91 // implicitly defined by some operation (e.g. its a live-in argument). The
92 // two operands to this are the token chain coming in and the register.
93 // The only result is the token chain going out.
96 // UNDEF - An undefined node
99 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
100 // a Constant, which is required to be operand #1), element of the aggregate
101 // value specified as operand #0. This is only for use before legalization,
102 // for values that will be broken into multiple registers.
105 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
106 // two values of the same integer value type, this produces a value twice as
107 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
110 // MERGE_VALUES - This node takes multiple discrete operands and returns
111 // them all as its individual results. This nodes has exactly the same
112 // number of inputs and outputs, and is only valid before legalization.
113 // This node is useful for some pieces of the code generator that want to
114 // think about a single node with multiple results, not multiple nodes.
117 // Simple integer binary arithmetic operators.
118 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
120 // Simple binary floating point operators.
121 FADD, FSUB, FMUL, FDIV, FREM,
123 // Simple abstract vector operators. Unlike the integer and floating point
124 // binary operators, these nodes also take two additional operands:
125 // a constant element count, and a value type node indicating the type of
126 // the elements. The order is op0, op1, count, type. All vector opcodes,
127 // including VLOAD, must currently have count and type as their 3rd and 4th
131 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
132 // an unsigned/signed value of type i[2*n], then return the top part.
135 // Bitwise operators.
136 AND, OR, XOR, SHL, SRA, SRL,
138 // Counting operators
144 // Select with condition operator - This selects between a true value and
145 // a false value (ops #2 and #3) based on the boolean result of comparing
146 // the lhs and rhs (ops #0 and #1) of a conditional expression with the
147 // condition code in op #4, a CondCodeSDNode.
150 // SetCC operator - This evaluates to a boolean (i1) true value if the
151 // condition is true. The operands to this are the left and right operands
152 // to compare (ops #0, and #1) and the condition code to compare them with
153 // (op #2) as a CondCodeSDNode.
156 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
157 // broken into a multiple pieces each, and return the resulting pieces of
158 // doing an atomic add/sub operation. This is used to handle add/sub of
159 // expanded types. The operation ordering is:
160 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
161 ADD_PARTS, SUB_PARTS,
163 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
164 // integer shift operations, just like ADD/SUB_PARTS. The operation
166 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
167 SHL_PARTS, SRA_PARTS, SRL_PARTS,
169 // Conversion operators. These are all single input single output
170 // operations. For all of these, the result type must be strictly
171 // wider or narrower (depending on the operation) than the source
174 // SIGN_EXTEND - Used for integer types, replicating the sign bit
178 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
181 // ANY_EXTEND - Used for integer types. The high bits are undefined.
184 // TRUNCATE - Completely drop the high bits.
187 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
188 // depends on the first letter) to floating point.
192 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
193 // sign extend a small value in a large integer register (e.g. sign
194 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
195 // with the 7th bit). The size of the smaller type is indicated by the 1th
196 // operand, a ValueType node.
199 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
204 // FP_ROUND - Perform a rounding operation from the current
205 // precision down to the specified precision (currently always 64->32).
208 // FP_ROUND_INREG - This operator takes a floating point register, and
209 // rounds it to a floating point value. It then promotes it and returns it
210 // in a register of the same size. This operation effectively just discards
211 // excess precision. The type to round down to is specified by the 1th
212 // operation, a VTSDNode (currently always 64->32->64).
215 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
218 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
219 // absolute value, square root, sine and cosine operations.
220 FNEG, FABS, FSQRT, FSIN, FCOS,
222 // Other operators. LOAD and STORE have token chains as their first
223 // operand, then the same operands as an LLVM load/store instruction, then a
224 // SRCVALUE node that provides alias analysis information.
227 // Abstract vector version of LOAD. VLOAD has a token chain as the first
228 // operand, followed by a pointer operand, a constant element count, a value
229 // type node indicating the type of the elements, and a SRCVALUE node.
232 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from
233 // memory and extend them to a larger value (e.g. load a byte into a word
234 // register). All three of these have four operands, a token chain, a
235 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node
236 // indicating the type to load.
238 // SEXTLOAD loads the integer operand and sign extends it to a larger
239 // integer result type.
240 // ZEXTLOAD loads the integer operand and zero extends it to a larger
241 // integer result type.
242 // EXTLOAD is used for two things: floating point extending loads, and
243 // integer extending loads where it doesn't matter what the high
244 // bits are set to. The code generator is allowed to codegen this
245 // into whichever operation is more efficient.
246 EXTLOAD, SEXTLOAD, ZEXTLOAD,
248 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
249 // value and stores it to memory in one operation. This can be used for
250 // either integer or floating point operands. The first four operands of
251 // this are the same as a standard store. The fifth is the ValueType to
252 // store it as (which will be smaller than the source value).
255 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
256 // to a specified boundary. The first operand is the token chain, the
257 // second is the number of bytes to allocate, and the third is the alignment
258 // boundary. The size is guaranteed to be a multiple of the stack
259 // alignment, and the alignment is guaranteed to be bigger than the stack
260 // alignment (if required) or 0 to get standard stack alignment.
263 // Control flow instructions. These all have token chains.
265 // BR - Unconditional branch. The first operand is the chain
266 // operand, the second is the MBB to branch to.
269 // BRCOND - Conditional branch. The first operand is the chain,
270 // the second is the condition, the third is the block to branch
271 // to if the condition is true.
274 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
275 // chain, the second is the condition, the third is the block to branch to
276 // if true, and the forth is the block to branch to if false. Targets
277 // usually do not implement this, preferring to have legalize demote the
278 // operation to BRCOND/BR pairs when necessary.
281 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in
282 // that the condition is represented as condition code, and two nodes to
283 // compare, rather than as a combined SetCC node. The operands in order are
284 // chain, cc, lhs, rhs, block to branch to if condition is true.
287 // BRTWOWAY_CC - Two-way conditional branch. The operands in order are
288 // chain, cc, lhs, rhs, block to branch to if condition is true, block to
289 // branch to if condition is false. Targets usually do not implement this,
290 // preferring to have legalize demote the operation to BRCOND/BR pairs.
293 // RET - Return from function. The first operand is the chain,
294 // and any subsequent operands are the return values for the
295 // function. This operation can have variable number of operands.
298 // CALL - Call to a function pointer. The first operand is the chain, the
299 // second is the destination function pointer (a GlobalAddress for a direct
300 // call). Arguments have already been lowered to explicit DAGs according to
301 // the calling convention in effect here. TAILCALL is the same as CALL, but
302 // the callee is known not to access the stack of the caller.
306 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
307 // correspond to the operands of the LLVM intrinsic functions. The only
308 // result is a token chain. The alignment argument is guaranteed to be a
314 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
315 // a call sequence, and carry arbitrary information that target might want
316 // to know. The first operand is a chain, the rest are specified by the
317 // target and not touched by the DAG optimizers.
318 CALLSEQ_START, // Beginning of a call sequence
319 CALLSEQ_END, // End of a call sequence
321 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
322 // locations with their value. This allows one use alias analysis
323 // information in the backend.
326 // PCMARKER - This corresponds to the pcmarker intrinsic.
329 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
330 // The only operand is a chain and a value and a chain are produced. The
331 // value is the contents of the architecture specific cycle counter like
332 // register (or other high accuracy low latency clock source)
335 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM
336 // intrinsics of the same name. The first operand is a token chain, the
337 // other operands match the intrinsic. These produce a token chain in
338 // addition to a value (if any).
339 READPORT, WRITEPORT, READIO, WRITEIO,
341 // HANDLENODE node - Used as a handle for various purposes.
344 // BUILTIN_OP_END - This must be the last enum value in this list.
348 //===--------------------------------------------------------------------===//
349 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
350 /// below work out, when considering SETFALSE (something that never exists
351 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
352 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
353 /// to. If the "N" column is 1, the result of the comparison is undefined if
354 /// the input is a NAN.
356 /// All of these (except for the 'always folded ops') should be handled for
357 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
358 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
360 /// Note that these are laid out in a specific order to allow bit-twiddling
361 /// to transform conditions.
363 // Opcode N U L G E Intuitive operation
364 SETFALSE, // 0 0 0 0 Always false (always folded)
365 SETOEQ, // 0 0 0 1 True if ordered and equal
366 SETOGT, // 0 0 1 0 True if ordered and greater than
367 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
368 SETOLT, // 0 1 0 0 True if ordered and less than
369 SETOLE, // 0 1 0 1 True if ordered and less than or equal
370 SETONE, // 0 1 1 0 True if ordered and operands are unequal
371 SETO, // 0 1 1 1 True if ordered (no nans)
372 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
373 SETUEQ, // 1 0 0 1 True if unordered or equal
374 SETUGT, // 1 0 1 0 True if unordered or greater than
375 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
376 SETULT, // 1 1 0 0 True if unordered or less than
377 SETULE, // 1 1 0 1 True if unordered, less than, or equal
378 SETUNE, // 1 1 1 0 True if unordered or not equal
379 SETTRUE, // 1 1 1 1 Always true (always folded)
380 // Don't care operations: undefined if the input is a nan.
381 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
382 SETEQ, // 1 X 0 0 1 True if equal
383 SETGT, // 1 X 0 1 0 True if greater than
384 SETGE, // 1 X 0 1 1 True if greater than or equal
385 SETLT, // 1 X 1 0 0 True if less than
386 SETLE, // 1 X 1 0 1 True if less than or equal
387 SETNE, // 1 X 1 1 0 True if not equal
388 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
390 SETCC_INVALID, // Marker value.
393 /// isSignedIntSetCC - Return true if this is a setcc instruction that
394 /// performs a signed comparison when used with integer operands.
395 inline bool isSignedIntSetCC(CondCode Code) {
396 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
399 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
400 /// performs an unsigned comparison when used with integer operands.
401 inline bool isUnsignedIntSetCC(CondCode Code) {
402 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
405 /// isTrueWhenEqual - Return true if the specified condition returns true if
406 /// the two operands to the condition are equal. Note that if one of the two
407 /// operands is a NaN, this value is meaningless.
408 inline bool isTrueWhenEqual(CondCode Cond) {
409 return ((int)Cond & 1) != 0;
412 /// getUnorderedFlavor - This function returns 0 if the condition is always
413 /// false if an operand is a NaN, 1 if the condition is always true if the
414 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
416 inline unsigned getUnorderedFlavor(CondCode Cond) {
417 return ((int)Cond >> 3) & 3;
420 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
421 /// 'op' is a valid SetCC operation.
422 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
424 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
425 /// when given the operation for (X op Y).
426 CondCode getSetCCSwappedOperands(CondCode Operation);
428 /// getSetCCOrOperation - Return the result of a logical OR between different
429 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
430 /// function returns SETCC_INVALID if it is not possible to represent the
431 /// resultant comparison.
432 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
434 /// getSetCCAndOperation - Return the result of a logical AND between
435 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
436 /// function returns SETCC_INVALID if it is not possible to represent the
437 /// resultant comparison.
438 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
439 } // end llvm::ISD namespace
442 //===----------------------------------------------------------------------===//
443 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
444 /// values as the result of a computation. Many nodes return multiple values,
445 /// from loads (which define a token and a return value) to ADDC (which returns
446 /// a result and a carry value), to calls (which may return an arbitrary number
449 /// As such, each use of a SelectionDAG computation must indicate the node that
450 /// computes it as well as which return value to use from that node. This pair
451 /// of information is represented with the SDOperand value type.
455 SDNode *Val; // The node defining the value we are using.
456 unsigned ResNo; // Which return value of the node we are using.
458 SDOperand() : Val(0) {}
459 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
461 bool operator==(const SDOperand &O) const {
462 return Val == O.Val && ResNo == O.ResNo;
464 bool operator!=(const SDOperand &O) const {
465 return !operator==(O);
467 bool operator<(const SDOperand &O) const {
468 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
471 SDOperand getValue(unsigned R) const {
472 return SDOperand(Val, R);
475 /// getValueType - Return the ValueType of the referenced return value.
477 inline MVT::ValueType getValueType() const;
479 // Forwarding methods - These forward to the corresponding methods in SDNode.
480 inline unsigned getOpcode() const;
481 inline unsigned getNodeDepth() const;
482 inline unsigned getNumOperands() const;
483 inline const SDOperand &getOperand(unsigned i) const;
484 inline bool isTargetOpcode() const;
485 inline unsigned getTargetOpcode() const;
487 /// hasOneUse - Return true if there is exactly one operation using this
488 /// result value of the defining operator.
489 inline bool hasOneUse() const;
493 /// simplify_type specializations - Allow casting operators to work directly on
494 /// SDOperands as if they were SDNode*'s.
495 template<> struct simplify_type<SDOperand> {
496 typedef SDNode* SimpleType;
497 static SimpleType getSimplifiedValue(const SDOperand &Val) {
498 return static_cast<SimpleType>(Val.Val);
501 template<> struct simplify_type<const SDOperand> {
502 typedef SDNode* SimpleType;
503 static SimpleType getSimplifiedValue(const SDOperand &Val) {
504 return static_cast<SimpleType>(Val.Val);
509 /// SDNode - Represents one node in the SelectionDAG.
512 /// NodeType - The operation that this node performs.
514 unsigned short NodeType;
516 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
517 /// means that leaves have a depth of 1, things that use only leaves have a
519 unsigned short NodeDepth;
521 /// OperandList - The values that are used by this operation.
523 SDOperand *OperandList;
525 /// ValueList - The types of the values this node defines. SDNode's may
526 /// define multiple values simultaneously.
527 MVT::ValueType *ValueList;
529 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
530 unsigned short NumOperands, NumValues;
532 /// Prev/Next pointers - These pointers form the linked list of of the
533 /// AllNodes list in the current DAG.
535 friend struct ilist_traits<SDNode>;
537 /// Uses - These are all of the SDNode's that use a value produced by this
539 std::vector<SDNode*> Uses;
542 assert(NumOperands == 0 && "Operand list not cleared before deletion");
545 //===--------------------------------------------------------------------===//
548 unsigned getOpcode() const { return NodeType; }
549 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
550 unsigned getTargetOpcode() const {
551 assert(isTargetOpcode() && "Not a target opcode!");
552 return NodeType - ISD::BUILTIN_OP_END;
555 size_t use_size() const { return Uses.size(); }
556 bool use_empty() const { return Uses.empty(); }
557 bool hasOneUse() const { return Uses.size() == 1; }
559 /// getNodeDepth - Return the distance from this node to the leaves in the
560 /// graph. The leaves have a depth of 1.
561 unsigned getNodeDepth() const { return NodeDepth; }
563 typedef std::vector<SDNode*>::const_iterator use_iterator;
564 use_iterator use_begin() const { return Uses.begin(); }
565 use_iterator use_end() const { return Uses.end(); }
567 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
568 /// indicated value. This method ignores uses of other values defined by this
570 bool hasNUsesOfValue(unsigned NUses, unsigned Value);
572 /// getNumOperands - Return the number of values used by this operation.
574 unsigned getNumOperands() const { return NumOperands; }
576 const SDOperand &getOperand(unsigned Num) const {
577 assert(Num < NumOperands && "Invalid child # of SDNode!");
578 return OperandList[Num];
580 typedef const SDOperand* op_iterator;
581 op_iterator op_begin() const { return OperandList; }
582 op_iterator op_end() const { return OperandList+NumOperands; }
585 /// getNumValues - Return the number of values defined/returned by this
588 unsigned getNumValues() const { return NumValues; }
590 /// getValueType - Return the type of a specified result.
592 MVT::ValueType getValueType(unsigned ResNo) const {
593 assert(ResNo < NumValues && "Illegal result number!");
594 return ValueList[ResNo];
597 typedef const MVT::ValueType* value_iterator;
598 value_iterator value_begin() const { return ValueList; }
599 value_iterator value_end() const { return ValueList+NumValues; }
601 /// getOperationName - Return the opcode of this operation for printing.
603 const char* getOperationName(const SelectionDAG *G = 0) const;
605 void dump(const SelectionDAG *G) const;
607 static bool classof(const SDNode *) { return true; }
610 /// setAdjCallChain - This method should only be used by the legalizer.
611 void setAdjCallChain(SDOperand N);
614 friend class SelectionDAG;
616 /// getValueTypeList - Return a pointer to the specified value type.
618 static MVT::ValueType *getValueTypeList(MVT::ValueType VT);
620 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
621 OperandList = 0; NumOperands = 0;
622 ValueList = getValueTypeList(VT);
626 SDNode(unsigned NT, SDOperand Op)
627 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
628 OperandList = new SDOperand[1];
631 Op.Val->Uses.push_back(this);
636 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
638 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
639 NodeDepth = N1.Val->getNodeDepth()+1;
641 NodeDepth = N2.Val->getNodeDepth()+1;
642 OperandList = new SDOperand[2];
646 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
651 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
653 unsigned ND = N1.Val->getNodeDepth();
654 if (ND < N2.Val->getNodeDepth())
655 ND = N2.Val->getNodeDepth();
656 if (ND < N3.Val->getNodeDepth())
657 ND = N3.Val->getNodeDepth();
660 OperandList = new SDOperand[3];
666 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
667 N3.Val->Uses.push_back(this);
672 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
674 unsigned ND = N1.Val->getNodeDepth();
675 if (ND < N2.Val->getNodeDepth())
676 ND = N2.Val->getNodeDepth();
677 if (ND < N3.Val->getNodeDepth())
678 ND = N3.Val->getNodeDepth();
679 if (ND < N4.Val->getNodeDepth())
680 ND = N4.Val->getNodeDepth();
683 OperandList = new SDOperand[4];
690 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
691 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
696 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) {
697 NumOperands = Nodes.size();
698 OperandList = new SDOperand[NumOperands];
701 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
702 OperandList[i] = Nodes[i];
703 SDNode *N = OperandList[i].Val;
704 N->Uses.push_back(this);
705 if (ND < N->getNodeDepth()) ND = N->getNodeDepth();
713 /// MorphNodeTo - This clears the return value and operands list, and sets the
714 /// opcode of the node to the specified value. This should only be used by
715 /// the SelectionDAG class.
716 void MorphNodeTo(unsigned Opc) {
721 // Clear the operands list, updating used nodes to remove this from their
723 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
724 I->Val->removeUser(this);
725 delete [] OperandList;
730 void setValueTypes(MVT::ValueType VT) {
731 assert(NumValues == 0 && "Should not have values yet!");
732 ValueList = getValueTypeList(VT);
735 void setValueTypes(MVT::ValueType *List, unsigned NumVal) {
736 assert(NumValues == 0 && "Should not have values yet!");
741 void setOperands(SDOperand Op0) {
742 assert(NumOperands == 0 && "Should not have operands yet!");
743 OperandList = new SDOperand[1];
744 OperandList[0] = Op0;
746 Op0.Val->Uses.push_back(this);
748 void setOperands(SDOperand Op0, SDOperand Op1) {
749 assert(NumOperands == 0 && "Should not have operands yet!");
750 OperandList = new SDOperand[2];
751 OperandList[0] = Op0;
752 OperandList[1] = Op1;
754 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
756 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) {
757 assert(NumOperands == 0 && "Should not have operands yet!");
758 OperandList = new SDOperand[3];
759 OperandList[0] = Op0;
760 OperandList[1] = Op1;
761 OperandList[2] = Op2;
763 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
764 Op2.Val->Uses.push_back(this);
766 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
767 assert(NumOperands == 0 && "Should not have operands yet!");
768 OperandList = new SDOperand[4];
769 OperandList[0] = Op0;
770 OperandList[1] = Op1;
771 OperandList[2] = Op2;
772 OperandList[3] = Op3;
774 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
775 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
777 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3,
779 assert(NumOperands == 0 && "Should not have operands yet!");
780 OperandList = new SDOperand[5];
781 OperandList[0] = Op0;
782 OperandList[1] = Op1;
783 OperandList[2] = Op2;
784 OperandList[3] = Op3;
785 OperandList[4] = Op4;
787 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this);
788 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this);
789 Op4.Val->Uses.push_back(this);
791 void addUser(SDNode *User) {
792 Uses.push_back(User);
794 void removeUser(SDNode *User) {
795 // Remove this user from the operand's use list.
796 for (unsigned i = Uses.size(); ; --i) {
797 assert(i != 0 && "Didn't find user!");
798 if (Uses[i-1] == User) {
799 Uses[i-1] = Uses.back();
808 // Define inline functions from the SDOperand class.
810 inline unsigned SDOperand::getOpcode() const {
811 return Val->getOpcode();
813 inline unsigned SDOperand::getNodeDepth() const {
814 return Val->getNodeDepth();
816 inline MVT::ValueType SDOperand::getValueType() const {
817 return Val->getValueType(ResNo);
819 inline unsigned SDOperand::getNumOperands() const {
820 return Val->getNumOperands();
822 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
823 return Val->getOperand(i);
825 inline bool SDOperand::isTargetOpcode() const {
826 return Val->isTargetOpcode();
828 inline unsigned SDOperand::getTargetOpcode() const {
829 return Val->getTargetOpcode();
831 inline bool SDOperand::hasOneUse() const {
832 return Val->hasNUsesOfValue(1, ResNo);
835 /// HandleSDNode - This class is used to form a handle around another node that
836 /// is persistant and is updated across invocations of replaceAllUsesWith on its
837 /// operand. This node should be directly created by end-users and not added to
838 /// the AllNodes list.
839 class HandleSDNode : public SDNode {
841 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {}
843 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses.
846 SDOperand getValue() const { return getOperand(0); }
850 class ConstantSDNode : public SDNode {
853 friend class SelectionDAG;
854 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT)
855 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) {
859 uint64_t getValue() const { return Value; }
861 int64_t getSignExtended() const {
862 unsigned Bits = MVT::getSizeInBits(getValueType(0));
863 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
866 bool isNullValue() const { return Value == 0; }
867 bool isAllOnesValue() const {
868 int NumBits = MVT::getSizeInBits(getValueType(0));
869 if (NumBits == 64) return Value+1 == 0;
870 return Value == (1ULL << NumBits)-1;
873 static bool classof(const ConstantSDNode *) { return true; }
874 static bool classof(const SDNode *N) {
875 return N->getOpcode() == ISD::Constant ||
876 N->getOpcode() == ISD::TargetConstant;
880 class ConstantFPSDNode : public SDNode {
883 friend class SelectionDAG;
884 ConstantFPSDNode(double val, MVT::ValueType VT)
885 : SDNode(ISD::ConstantFP, VT), Value(val) {
889 double getValue() const { return Value; }
891 /// isExactlyValue - We don't rely on operator== working on double values, as
892 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
893 /// As such, this method can be used to do an exact bit-for-bit comparison of
894 /// two floating point values.
895 bool isExactlyValue(double V) const;
897 static bool classof(const ConstantFPSDNode *) { return true; }
898 static bool classof(const SDNode *N) {
899 return N->getOpcode() == ISD::ConstantFP;
903 class GlobalAddressSDNode : public SDNode {
904 GlobalValue *TheGlobal;
906 friend class SelectionDAG;
907 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT)
908 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT) {
909 TheGlobal = const_cast<GlobalValue*>(GA);
913 GlobalValue *getGlobal() const { return TheGlobal; }
915 static bool classof(const GlobalAddressSDNode *) { return true; }
916 static bool classof(const SDNode *N) {
917 return N->getOpcode() == ISD::GlobalAddress ||
918 N->getOpcode() == ISD::TargetGlobalAddress;
923 class FrameIndexSDNode : public SDNode {
926 friend class SelectionDAG;
927 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg)
928 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {}
931 int getIndex() const { return FI; }
933 static bool classof(const FrameIndexSDNode *) { return true; }
934 static bool classof(const SDNode *N) {
935 return N->getOpcode() == ISD::FrameIndex ||
936 N->getOpcode() == ISD::TargetFrameIndex;
940 class ConstantPoolSDNode : public SDNode {
943 friend class SelectionDAG;
944 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget)
945 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
949 Constant *get() const { return C; }
951 static bool classof(const ConstantPoolSDNode *) { return true; }
952 static bool classof(const SDNode *N) {
953 return N->getOpcode() == ISD::ConstantPool ||
954 N->getOpcode() == ISD::TargetConstantPool;
958 class BasicBlockSDNode : public SDNode {
959 MachineBasicBlock *MBB;
961 friend class SelectionDAG;
962 BasicBlockSDNode(MachineBasicBlock *mbb)
963 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
966 MachineBasicBlock *getBasicBlock() const { return MBB; }
968 static bool classof(const BasicBlockSDNode *) { return true; }
969 static bool classof(const SDNode *N) {
970 return N->getOpcode() == ISD::BasicBlock;
974 class SrcValueSDNode : public SDNode {
978 friend class SelectionDAG;
979 SrcValueSDNode(const Value* v, int o)
980 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
983 const Value *getValue() const { return V; }
984 int getOffset() const { return offset; }
986 static bool classof(const SrcValueSDNode *) { return true; }
987 static bool classof(const SDNode *N) {
988 return N->getOpcode() == ISD::SRCVALUE;
993 class RegisterSDNode : public SDNode {
996 friend class SelectionDAG;
997 RegisterSDNode(unsigned reg, MVT::ValueType VT)
998 : SDNode(ISD::Register, VT), Reg(reg) {}
1001 unsigned getReg() const { return Reg; }
1003 static bool classof(const RegisterSDNode *) { return true; }
1004 static bool classof(const SDNode *N) {
1005 return N->getOpcode() == ISD::Register;
1009 class ExternalSymbolSDNode : public SDNode {
1012 friend class SelectionDAG;
1013 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT)
1014 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT),
1019 const char *getSymbol() const { return Symbol; }
1021 static bool classof(const ExternalSymbolSDNode *) { return true; }
1022 static bool classof(const SDNode *N) {
1023 return N->getOpcode() == ISD::ExternalSymbol ||
1024 N->getOpcode() == ISD::TargetExternalSymbol;
1028 class CondCodeSDNode : public SDNode {
1029 ISD::CondCode Condition;
1031 friend class SelectionDAG;
1032 CondCodeSDNode(ISD::CondCode Cond)
1033 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) {
1037 ISD::CondCode get() const { return Condition; }
1039 static bool classof(const CondCodeSDNode *) { return true; }
1040 static bool classof(const SDNode *N) {
1041 return N->getOpcode() == ISD::CONDCODE;
1045 /// VTSDNode - This class is used to represent MVT::ValueType's, which are used
1046 /// to parameterize some operations.
1047 class VTSDNode : public SDNode {
1048 MVT::ValueType ValueType;
1050 friend class SelectionDAG;
1051 VTSDNode(MVT::ValueType VT)
1052 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {}
1055 MVT::ValueType getVT() const { return ValueType; }
1057 static bool classof(const VTSDNode *) { return true; }
1058 static bool classof(const SDNode *N) {
1059 return N->getOpcode() == ISD::VALUETYPE;
1064 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
1068 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1070 bool operator==(const SDNodeIterator& x) const {
1071 return Operand == x.Operand;
1073 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1075 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1076 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1077 Operand = I.Operand;
1081 pointer operator*() const {
1082 return Node->getOperand(Operand).Val;
1084 pointer operator->() const { return operator*(); }
1086 SDNodeIterator& operator++() { // Preincrement
1090 SDNodeIterator operator++(int) { // Postincrement
1091 SDNodeIterator tmp = *this; ++*this; return tmp;
1094 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1095 static SDNodeIterator end (SDNode *N) {
1096 return SDNodeIterator(N, N->getNumOperands());
1099 unsigned getOperand() const { return Operand; }
1100 const SDNode *getNode() const { return Node; }
1103 template <> struct GraphTraits<SDNode*> {
1104 typedef SDNode NodeType;
1105 typedef SDNodeIterator ChildIteratorType;
1106 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1107 static inline ChildIteratorType child_begin(NodeType *N) {
1108 return SDNodeIterator::begin(N);
1110 static inline ChildIteratorType child_end(NodeType *N) {
1111 return SDNodeIterator::end(N);
1116 struct ilist_traits<SDNode> {
1117 static SDNode *getPrev(const SDNode *N) { return N->Prev; }
1118 static SDNode *getNext(const SDNode *N) { return N->Next; }
1120 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; }
1121 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; }
1123 static SDNode *createSentinel() {
1124 return new SDNode(ISD::EntryToken, MVT::Other);
1126 static void destroySentinel(SDNode *N) { delete N; }
1127 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); }
1130 void addNodeToList(SDNode *NTy) {}
1131 void removeNodeFromList(SDNode *NTy) {}
1132 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2,
1133 const ilist_iterator<SDNode> &X,
1134 const ilist_iterator<SDNode> &Y) {}
1137 } // end llvm namespace