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/ADT/GraphTraits.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;
38 /// ISD namespace - This namespace contains an enum which represents all of the
39 /// SelectionDAG node types and value types.
42 //===--------------------------------------------------------------------===//
43 /// ISD::NodeType enum - This enum defines all of the operators valid in a
48 EntryToken, Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
49 BasicBlock, ExternalSymbol,
51 // CopyToReg - This node has chain and child nodes, and an associated
52 // register number. The instruction selector must guarantee that the value
53 // of the value node is available in the virtual register stored in the
54 // CopyRegSDNode object.
57 // CopyFromReg - This node indicates that the input value is a virtual or
58 // physical register that is defined outside of the scope of this
59 // SelectionDAG. The virtual register is available from the
60 // CopyRegSDNode object.
63 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
64 // a Constant, which is required to be operand #1), element of the aggregate
65 // value specified as operand #0. This is only for use before legalization,
66 // for values that will be broken into multiple registers.
69 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
70 // two values of the same integer value type, this produces a value twice as
71 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
75 // Simple binary arithmetic operators.
76 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
79 AND, OR, XOR, SHL, SRA, SRL,
84 // SetCC operator - This evaluates to a boolean (i1) true value if the
85 // condition is true. These nodes are instances of the
86 // SetCCSDNode class, which contains the condition code as extra
90 // addc - Three input, two output operator: (X, Y, C) -> (X+Y+C,
91 // Cout). X,Y are integer inputs of agreeing size, C is a one bit
92 // value, and two values are produced: the sum and a carry out.
95 // Conversion operators. These are all single input single output
96 // operations. For all of these, the result type must be strictly
97 // wider or narrower (depending on the operation) than the source
100 // SIGN_EXTEND - Used for integer types, replicating the sign bit
104 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
107 // TRUNCATE - Completely drop the high bits.
110 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
111 // depends on the first letter) to floating point.
115 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
120 // FP_ROUND - Perform a rounding operation from the current
121 // precision down to the specified precision.
124 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
127 // Other operators. LOAD and STORE have token chains.
130 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
131 // to a specified boundary. The first operand is the token chain, the
132 // second is the number of bytes to allocate, and the third is the alignment
136 // Control flow instructions. These all have token chains.
138 // BR - Unconditional branch. The first operand is the chain
139 // operand, the second is the MBB to branch to.
142 // BRCOND - Conditional branch. The first operand is the chain,
143 // the second is the condition, the third is the block to branch
144 // to if the condition is true.
147 // RET - Return from function. The first operand is the chain,
148 // and any subsequent operands are the return values for the
149 // function. This operation can have variable number of operands.
152 // CALL - Call to a function pointer. The first operand is the chain, the
153 // second is the destination function pointer (a GlobalAddress for a direct
154 // call). Arguments have already been lowered to explicit DAGs according to
155 // the calling convention in effect here.
158 // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and
159 // end of a call sequence and indicate how much the stack pointer needs to
160 // be adjusted for that particular call. The first operand is a chain, the
161 // second is a ConstantSDNode of intptr type.
162 ADJCALLSTACKDOWN, // Beginning of a call sequence
163 ADJCALLSTACKUP, // End of a call sequence
166 // BUILTIN_OP_END - This must be the last enum value in this list.
170 //===--------------------------------------------------------------------===//
171 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
172 /// below work out, when considering SETFALSE (something that never exists
173 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
174 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
175 /// to. If the "N" column is 1, the result of the comparison is undefined if
176 /// the input is a NAN.
178 /// All of these (except for the 'always folded ops') should be handled for
179 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
180 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
182 /// Note that these are laid out in a specific order to allow bit-twiddling
183 /// to transform conditions.
185 // Opcode N U L G E Intuitive operation
186 SETFALSE, // 0 0 0 0 Always false (always folded)
187 SETOEQ, // 0 0 0 1 True if ordered and equal
188 SETOGT, // 0 0 1 0 True if ordered and greater than
189 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
190 SETOLT, // 0 1 0 0 True if ordered and less than
191 SETOLE, // 0 1 0 1 True if ordered and less than or equal
192 SETONE, // 0 1 1 0 True if ordered and operands are unequal
193 SETO, // 0 1 1 1 True if ordered (no nans)
194 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
195 SETUEQ, // 1 0 0 1 True if unordered or equal
196 SETUGT, // 1 0 1 0 True if unordered or greater than
197 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
198 SETULT, // 1 1 0 0 True if unordered or less than
199 SETULE, // 1 1 0 1 True if unordered, less than, or equal
200 SETUNE, // 1 1 1 0 True if unordered or not equal
201 SETTRUE, // 1 1 1 1 Always true (always folded)
202 // Don't care operations: undefined if the input is a nan.
203 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
204 SETEQ, // 1 X 0 0 1 True if equal
205 SETGT, // 1 X 0 1 0 True if greater than
206 SETGE, // 1 X 0 1 1 True if greater than or equal
207 SETLT, // 1 X 1 0 0 True if less than
208 SETLE, // 1 X 1 0 1 True if less than or equal
209 SETNE, // 1 X 1 1 0 True if not equal
210 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
212 SETCC_INVALID, // Marker value.
215 /// isSignedIntSetCC - Return true if this is a setcc instruction that
216 /// performs a signed comparison when used with integer operands.
217 inline bool isSignedIntSetCC(CondCode Code) {
218 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
221 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
222 /// performs an unsigned comparison when used with integer operands.
223 inline bool isUnsignedIntSetCC(CondCode Code) {
224 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
227 /// isTrueWhenEqual - Return true if the specified condition returns true if
228 /// the two operands to the condition are equal. Note that if one of the two
229 /// operands is a NaN, this value is meaningless.
230 inline bool isTrueWhenEqual(CondCode Cond) {
231 return ((int)Cond & 1) != 0;
234 /// getUnorderedFlavor - This function returns 0 if the condition is always
235 /// false if an operand is a NaN, 1 if the condition is always true if the
236 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
238 inline unsigned getUnorderedFlavor(CondCode Cond) {
239 return ((int)Cond >> 3) & 3;
242 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
243 /// 'op' is a valid SetCC operation.
244 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
246 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
247 /// when given the operation for (X op Y).
248 CondCode getSetCCSwappedOperands(CondCode Operation);
250 /// getSetCCOrOperation - Return the result of a logical OR between different
251 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
252 /// function returns SETCC_INVALID if it is not possible to represent the
253 /// resultant comparison.
254 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
256 /// getSetCCAndOperation - Return the result of a logical AND between
257 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
258 /// function returns SETCC_INVALID if it is not possible to represent the
259 /// resultant comparison.
260 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
261 } // end llvm::ISD namespace
264 //===----------------------------------------------------------------------===//
265 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
266 /// values as the result of a computation. Many nodes return multiple values,
267 /// from loads (which define a token and a return value) to ADDC (which returns
268 /// a result and a carry value), to calls (which may return an arbitrary number
271 /// As such, each use of a SelectionDAG computation must indicate the node that
272 /// computes it as well as which return value to use from that node. This pair
273 /// of information is represented with the SDOperand value type.
277 SDNode *Val; // The node defining the value we are using.
278 unsigned ResNo; // Which return value of the node we are using.
280 SDOperand() : Val(0) {}
281 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
283 bool operator==(const SDOperand &O) const {
284 return Val == O.Val && ResNo == O.ResNo;
286 bool operator!=(const SDOperand &O) const {
287 return !operator==(O);
289 bool operator<(const SDOperand &O) const {
290 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
293 SDOperand getValue(unsigned R) const {
294 return SDOperand(Val, R);
297 /// getValueType - Return the ValueType of the referenced return value.
299 inline MVT::ValueType getValueType() const;
301 // Forwarding methods - These forward to the corresponding methods in SDNode.
302 inline unsigned getOpcode() const;
303 inline unsigned getNumOperands() const;
304 inline const SDOperand &getOperand(unsigned i) const;
308 /// simplify_type specializations - Allow casting operators to work directly on
309 /// SDOperands as if they were SDNode*'s.
310 template<> struct simplify_type<SDOperand> {
311 typedef SDNode* SimpleType;
312 static SimpleType getSimplifiedValue(const SDOperand &Val) {
313 return static_cast<SimpleType>(Val.Val);
316 template<> struct simplify_type<const SDOperand> {
317 typedef SDNode* SimpleType;
318 static SimpleType getSimplifiedValue(const SDOperand &Val) {
319 return static_cast<SimpleType>(Val.Val);
324 /// SDNode - Represents one node in the SelectionDAG.
328 std::vector<SDOperand> Operands;
330 /// Values - The types of the values this node defines. SDNode's may define
331 /// multiple values simultaneously.
332 std::vector<MVT::ValueType> Values;
334 /// Uses - These are all of the SDNode's that use a value produced by this
336 std::vector<SDNode*> Uses;
339 //===--------------------------------------------------------------------===//
342 unsigned getOpcode() const { return NodeType; }
344 size_t use_size() const { return Uses.size(); }
345 bool use_empty() const { return Uses.empty(); }
346 bool hasOneUse() const { return Uses.size() == 1; }
348 /// getNumOperands - Return the number of values used by this operation.
350 unsigned getNumOperands() const { return Operands.size(); }
352 const SDOperand &getOperand(unsigned Num) {
353 assert(Num < Operands.size() && "Invalid child # of SDNode!");
354 return Operands[Num];
357 const SDOperand &getOperand(unsigned Num) const {
358 assert(Num < Operands.size() && "Invalid child # of SDNode!");
359 return Operands[Num];
362 /// getNumValues - Return the number of values defined/returned by this
365 unsigned getNumValues() const { return Values.size(); }
367 /// getValueType - Return the type of a specified result.
369 MVT::ValueType getValueType(unsigned ResNo) const {
370 assert(ResNo < Values.size() && "Illegal result number!");
371 return Values[ResNo];
376 static bool classof(const SDNode *) { return true; }
379 friend class SelectionDAG;
381 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT) {
383 Values.push_back(VT);
386 SDNode(unsigned NT, SDOperand Op)
388 Operands.reserve(1); Operands.push_back(Op);
389 Op.Val->Uses.push_back(this);
391 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
393 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
394 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
396 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
398 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
399 Operands.push_back(N3);
400 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
401 N3.Val->Uses.push_back(this);
403 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
404 Operands.swap(Nodes);
405 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
406 Nodes[i].Val->Uses.push_back(this);
413 void setValueTypes(MVT::ValueType VT) {
415 Values.push_back(VT);
417 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
419 Values.push_back(VT1);
420 Values.push_back(VT2);
422 /// Note: this method destroys the vector passed in.
423 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
424 std::swap(Values, VTs);
427 void removeUser(SDNode *User) {
428 // Remove this user from the operand's use list.
429 for (unsigned i = Uses.size(); ; --i) {
430 assert(i != 0 && "Didn't find user!");
431 if (Uses[i-1] == User) {
432 Uses.erase(Uses.begin()+i-1);
440 // Define inline functions from the SDOperand class.
442 inline unsigned SDOperand::getOpcode() const {
443 return Val->getOpcode();
445 inline MVT::ValueType SDOperand::getValueType() const {
446 return Val->getValueType(ResNo);
448 inline unsigned SDOperand::getNumOperands() const {
449 return Val->getNumOperands();
451 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
452 return Val->getOperand(i);
457 class ConstantSDNode : public SDNode {
460 friend class SelectionDAG;
461 ConstantSDNode(uint64_t val, MVT::ValueType VT)
462 : SDNode(ISD::Constant, VT), Value(val) {
466 uint64_t getValue() const { return Value; }
468 int64_t getSignExtended() const {
469 unsigned Bits = MVT::getSizeInBits(getValueType(0));
470 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
473 bool isNullValue() const { return Value == 0; }
474 bool isAllOnesValue() const {
475 return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1;
478 static bool classof(const ConstantSDNode *) { return true; }
479 static bool classof(const SDNode *N) {
480 return N->getOpcode() == ISD::Constant;
484 class ConstantFPSDNode : public SDNode {
487 friend class SelectionDAG;
488 ConstantFPSDNode(double val, MVT::ValueType VT)
489 : SDNode(ISD::ConstantFP, VT), Value(val) {
493 double getValue() const { return Value; }
495 /// isExactlyValue - We don't rely on operator== working on double values, as
496 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
497 /// As such, this method can be used to do an exact bit-for-bit comparison of
498 /// two floating point values.
499 bool isExactlyValue(double V) const {
513 static bool classof(const ConstantFPSDNode *) { return true; }
514 static bool classof(const SDNode *N) {
515 return N->getOpcode() == ISD::ConstantFP;
519 class GlobalAddressSDNode : public SDNode {
520 GlobalValue *TheGlobal;
522 friend class SelectionDAG;
523 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
524 : SDNode(ISD::GlobalAddress, VT) {
525 TheGlobal = const_cast<GlobalValue*>(GA);
529 GlobalValue *getGlobal() const { return TheGlobal; }
531 static bool classof(const GlobalAddressSDNode *) { return true; }
532 static bool classof(const SDNode *N) {
533 return N->getOpcode() == ISD::GlobalAddress;
538 class FrameIndexSDNode : public SDNode {
541 friend class SelectionDAG;
542 FrameIndexSDNode(int fi, MVT::ValueType VT)
543 : SDNode(ISD::FrameIndex, VT), FI(fi) {}
546 int getIndex() const { return FI; }
548 static bool classof(const FrameIndexSDNode *) { return true; }
549 static bool classof(const SDNode *N) {
550 return N->getOpcode() == ISD::FrameIndex;
554 class ConstantPoolSDNode : public SDNode {
557 friend class SelectionDAG;
558 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
559 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
562 unsigned getIndex() const { return CPI; }
564 static bool classof(const ConstantPoolSDNode *) { return true; }
565 static bool classof(const SDNode *N) {
566 return N->getOpcode() == ISD::ConstantPool;
570 class BasicBlockSDNode : public SDNode {
571 MachineBasicBlock *MBB;
573 friend class SelectionDAG;
574 BasicBlockSDNode(MachineBasicBlock *mbb)
575 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
578 MachineBasicBlock *getBasicBlock() const { return MBB; }
580 static bool classof(const BasicBlockSDNode *) { return true; }
581 static bool classof(const SDNode *N) {
582 return N->getOpcode() == ISD::BasicBlock;
587 class CopyRegSDNode : public SDNode {
590 friend class SelectionDAG;
591 CopyRegSDNode(SDOperand Chain, SDOperand Src, unsigned reg)
592 : SDNode(ISD::CopyToReg, Chain, Src), Reg(reg) {
593 setValueTypes(MVT::Other); // Just a token chain.
595 CopyRegSDNode(unsigned reg, MVT::ValueType VT)
596 : SDNode(ISD::CopyFromReg, VT), Reg(reg) {
600 unsigned getReg() const { return Reg; }
602 static bool classof(const CopyRegSDNode *) { return true; }
603 static bool classof(const SDNode *N) {
604 return N->getOpcode() == ISD::CopyToReg ||
605 N->getOpcode() == ISD::CopyFromReg;
609 class ExternalSymbolSDNode : public SDNode {
612 friend class SelectionDAG;
613 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
614 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
618 const char *getSymbol() const { return Symbol; }
620 static bool classof(const ExternalSymbolSDNode *) { return true; }
621 static bool classof(const SDNode *N) {
622 return N->getOpcode() == ISD::ExternalSymbol;
626 class SetCCSDNode : public SDNode {
627 ISD::CondCode Condition;
629 friend class SelectionDAG;
630 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
631 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
632 setValueTypes(MVT::i1);
636 ISD::CondCode getCondition() const { return Condition; }
638 static bool classof(const SetCCSDNode *) { return true; }
639 static bool classof(const SDNode *N) {
640 return N->getOpcode() == ISD::SETCC;
645 class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
649 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
651 bool operator==(const SDNodeIterator& x) const {
652 return Operand == x.Operand;
654 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
656 const SDNodeIterator &operator=(const SDNodeIterator &I) {
657 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
662 pointer operator*() const {
663 return Node->getOperand(Operand).Val;
665 pointer operator->() const { return operator*(); }
667 SDNodeIterator& operator++() { // Preincrement
671 SDNodeIterator operator++(int) { // Postincrement
672 SDNodeIterator tmp = *this; ++*this; return tmp;
675 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
676 static SDNodeIterator end (SDNode *N) {
677 return SDNodeIterator(N, N->getNumOperands());
680 unsigned getOperand() const { return Operand; }
681 const SDNode *getNode() const { return Node; }
684 template <> struct GraphTraits<SDNode*> {
685 typedef SDNode NodeType;
686 typedef SDNodeIterator ChildIteratorType;
687 static inline NodeType *getEntryNode(SDNode *N) { return N; }
688 static inline ChildIteratorType child_begin(NodeType *N) {
689 return SDNodeIterator::begin(N);
691 static inline ChildIteratorType child_end(NodeType *N) {
692 return SDNodeIterator::end(N);
699 } // end llvm namespace