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/Support/DataTypes.h"
31 class MachineBasicBlock;
33 template <typename T> struct simplify_type;
35 /// ISD namespace - This namespace contains an enum which represents all of the
36 /// SelectionDAG node types and value types.
39 //===--------------------------------------------------------------------===//
40 /// ISD::NodeType enum - This enum defines all of the operators valid in a
45 EntryToken, Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
46 BasicBlock, ExternalSymbol,
48 // CopyToReg - This node has chain and child nodes, and an associated
49 // register number. The instruction selector must guarantee that the value
50 // of the value node is available in the virtual register stored in the
51 // CopyRegSDNode object.
54 // CopyFromReg - This node indicates that the input value is a virtual or
55 // physical register that is defined outside of the scope of this
56 // SelectionDAG. The virtual register is available from the
57 // CopyRegSDNode object.
60 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
61 // a Constant, which is required to be operand #1), element of the aggregate
62 // value specified as operand #0. This is only for use before legalization,
63 // for values that will be broken into multiple registers.
66 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
67 // two values of the same integer value type, this produces a value twice as
68 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
72 // Simple binary arithmetic operators.
73 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
76 AND, OR, XOR, SHL, SRA, SRL,
81 // SetCC operator - This evaluates to a boolean (i1) true value if the
82 // condition is true. These nodes are instances of the
83 // SetCCSDNode class, which contains the condition code as extra
87 // addc - Three input, two output operator: (X, Y, C) -> (X+Y+C,
88 // Cout). X,Y are integer inputs of agreeing size, C is a one bit
89 // value, and two values are produced: the sum and a carry out.
92 // Conversion operators. These are all single input single output
93 // operations. For all of these, the result type must be strictly
94 // wider or narrower (depending on the operation) than the source
97 // SIGN_EXTEND - Used for integer types, replicating the sign bit
101 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
104 // TRUNCATE - Completely drop the high bits.
107 // FP_ROUND - Perform a rounding operation from the current
108 // precision down to the specified precision.
111 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
114 // Other operators. LOAD and STORE have token chains.
117 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
118 // to a specified boundary. The first operand is the token chain, the
119 // second is the number of bytes to allocate, and the third is the alignment
123 // Control flow instructions. These all have token chains.
125 // BR - Unconditional branch. The first operand is the chain
126 // operand, the second is the MBB to branch to.
129 // BRCOND - Conditional branch. The first operand is the chain,
130 // the second is the condition, the third is the block to branch
131 // to if the condition is true.
134 // RET - Return from function. The first operand is the chain,
135 // and any subsequent operands are the return values for the
136 // function. This operation can have variable number of operands.
139 // CALL - Call to a function pointer. The first operand is the chain, the
140 // second is the destination function pointer (a GlobalAddress for a direct
141 // call). Arguments have already been lowered to explicit DAGs according to
142 // the calling convention in effect here.
145 // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and
146 // end of a call sequence and indicate how much the stack pointer needs to
147 // be adjusted for that particular call. The first operand is a chain, the
148 // second is a ConstantSDNode of intptr type.
149 ADJCALLSTACKDOWN, // Beginning of a call sequence
150 ADJCALLSTACKUP, // End of a call sequence
153 // BUILTIN_OP_END - This must be the last enum value in this list.
157 //===--------------------------------------------------------------------===//
158 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
159 /// below work out, when considering SETFALSE (something that never exists
160 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
161 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
162 /// to. If the "N" column is 1, the result of the comparison is undefined if
163 /// the input is a NAN.
165 /// All of these (except for the 'always folded ops') should be handled for
166 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
167 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
169 /// Note that these are laid out in a specific order to allow bit-twiddling
170 /// to transform conditions.
172 // Opcode N U L G E Intuitive operation
173 SETFALSE, // 0 0 0 0 Always false (always folded)
174 SETOEQ, // 0 0 0 1 True if ordered and equal
175 SETOGT, // 0 0 1 0 True if ordered and greater than
176 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
177 SETOLT, // 0 1 0 0 True if ordered and less than
178 SETOLE, // 0 1 0 1 True if ordered and less than or equal
179 SETONE, // 0 1 1 0 True if ordered and operands are unequal
180 SETO, // 0 1 1 1 True if ordered (no nans)
181 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
182 SETUEQ, // 1 0 0 1 True if unordered or equal
183 SETUGT, // 1 0 1 0 True if unordered or greater than
184 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
185 SETULT, // 1 1 0 0 True if unordered or less than
186 SETULE, // 1 1 0 1 True if unordered, less than, or equal
187 SETUNE, // 1 1 1 0 True if unordered or not equal
188 SETTRUE, // 1 1 1 1 Always true (always folded)
189 // Don't care operations: undefined if the input is a nan.
190 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
191 SETEQ, // 1 X 0 0 1 True if equal
192 SETGT, // 1 X 0 1 0 True if greater than
193 SETGE, // 1 X 0 1 1 True if greater than or equal
194 SETLT, // 1 X 1 0 0 True if less than
195 SETLE, // 1 X 1 0 1 True if less than or equal
196 SETNE, // 1 X 1 1 0 True if not equal
197 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
199 SETCC_INVALID, // Marker value.
202 /// isSignedIntSetCC - Return true if this is a setcc instruction that
203 /// performs a signed comparison when used with integer operands.
204 inline bool isSignedIntSetCC(CondCode Code) {
205 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
208 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
209 /// performs an unsigned comparison when used with integer operands.
210 inline bool isUnsignedIntSetCC(CondCode Code) {
211 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
214 /// isTrueWhenEqual - Return true if the specified condition returns true if
215 /// the two operands to the condition are equal. Note that if one of the two
216 /// operands is a NaN, this value is meaningless.
217 inline bool isTrueWhenEqual(CondCode Cond) {
218 return ((int)Cond & 1) != 0;
221 /// getUnorderedFlavor - This function returns 0 if the condition is always
222 /// false if an operand is a NaN, 1 if the condition is always true if the
223 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
225 inline unsigned getUnorderedFlavor(CondCode Cond) {
226 return ((int)Cond >> 3) & 3;
229 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
230 /// 'op' is a valid SetCC operation.
231 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
233 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
234 /// when given the operation for (X op Y).
235 CondCode getSetCCSwappedOperands(CondCode Operation);
237 /// getSetCCOrOperation - Return the result of a logical OR between different
238 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
239 /// function returns SETCC_INVALID if it is not possible to represent the
240 /// resultant comparison.
241 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
243 /// getSetCCAndOperation - Return the result of a logical AND between
244 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
245 /// function returns SETCC_INVALID if it is not possible to represent the
246 /// resultant comparison.
247 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
248 } // end llvm::ISD namespace
251 //===----------------------------------------------------------------------===//
252 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
253 /// values as the result of a computation. Many nodes return multiple values,
254 /// from loads (which define a token and a return value) to ADDC (which returns
255 /// a result and a carry value), to calls (which may return an arbitrary number
258 /// As such, each use of a SelectionDAG computation must indicate the node that
259 /// computes it as well as which return value to use from that node. This pair
260 /// of information is represented with the SDOperand value type.
263 SDNode *Val; // The node defining the value we are using.
264 unsigned ResNo; // Which return value of the node we are using.
266 SDOperand() : Val(0) {}
267 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
269 bool operator==(const SDOperand &O) const {
270 return Val == O.Val && ResNo == O.ResNo;
272 bool operator!=(const SDOperand &O) const {
273 return !operator==(O);
275 bool operator<(const SDOperand &O) const {
276 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
279 SDOperand getValue(unsigned R) const {
280 return SDOperand(Val, R);
283 /// getValueType - Return the ValueType of the referenced return value.
285 inline MVT::ValueType getValueType() const;
287 // Forwarding methods - These forward to the corresponding methods in SDNode.
288 inline unsigned getOpcode() const;
289 inline unsigned getNumOperands() const;
290 inline const SDOperand &getOperand(unsigned i) const;
294 /// simplify_type specializations - Allow casting operators to work directly on
295 /// SDOperands as if they were SDNode*'s.
296 template<> struct simplify_type<SDOperand> {
297 typedef SDNode* SimpleType;
298 static SimpleType getSimplifiedValue(const SDOperand &Val) {
299 return static_cast<SimpleType>(Val.Val);
302 template<> struct simplify_type<const SDOperand> {
303 typedef SDNode* SimpleType;
304 static SimpleType getSimplifiedValue(const SDOperand &Val) {
305 return static_cast<SimpleType>(Val.Val);
310 /// SDNode - Represents one node in the SelectionDAG.
314 std::vector<SDOperand> Operands;
316 /// Values - The types of the values this node defines. SDNode's may define
317 /// multiple values simultaneously.
318 std::vector<MVT::ValueType> Values;
320 /// Uses - These are all of the SDNode's that use a value produced by this
322 std::vector<SDNode*> Uses;
325 //===--------------------------------------------------------------------===//
328 unsigned getOpcode() const { return NodeType; }
330 size_t use_size() const { return Uses.size(); }
331 bool use_empty() const { return Uses.empty(); }
332 bool hasOneUse() const { return Uses.size() == 1; }
334 /// getNumOperands - Return the number of values used by this operation.
336 unsigned getNumOperands() const { return Operands.size(); }
338 const SDOperand &getOperand(unsigned Num) {
339 assert(Num < Operands.size() && "Invalid child # of SDNode!");
340 return Operands[Num];
343 const SDOperand &getOperand(unsigned Num) const {
344 assert(Num < Operands.size() && "Invalid child # of SDNode!");
345 return Operands[Num];
348 /// getNumValues - Return the number of values defined/returned by this
351 unsigned getNumValues() const { return Values.size(); }
353 /// getValueType - Return the type of a specified result.
355 MVT::ValueType getValueType(unsigned ResNo) const {
356 assert(ResNo < Values.size() && "Illegal result number!");
357 return Values[ResNo];
362 static bool classof(const SDNode *) { return true; }
365 friend class SelectionDAG;
367 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT) {
369 Values.push_back(VT);
372 SDNode(unsigned NT, SDOperand Op)
374 Operands.reserve(1); Operands.push_back(Op);
375 Op.Val->Uses.push_back(this);
377 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
379 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
380 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
382 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
384 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
385 Operands.push_back(N3);
386 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
387 N3.Val->Uses.push_back(this);
389 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
390 Operands.swap(Nodes);
391 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
392 Nodes[i].Val->Uses.push_back(this);
399 void setValueTypes(MVT::ValueType VT) {
401 Values.push_back(VT);
403 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
405 Values.push_back(VT1);
406 Values.push_back(VT2);
408 /// Note: this method destroys the vector passed in.
409 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
410 std::swap(Values, VTs);
415 // Define inline functions from the SDOperand class.
417 inline unsigned SDOperand::getOpcode() const {
418 return Val->getOpcode();
420 inline MVT::ValueType SDOperand::getValueType() const {
421 return Val->getValueType(ResNo);
423 inline unsigned SDOperand::getNumOperands() const {
424 return Val->getNumOperands();
426 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
427 return Val->getOperand(i);
432 class ConstantSDNode : public SDNode {
435 friend class SelectionDAG;
436 ConstantSDNode(uint64_t val, MVT::ValueType VT)
437 : SDNode(ISD::Constant, VT), Value(val) {
441 uint64_t getValue() const { return Value; }
443 int64_t getSignExtended() const {
444 unsigned Bits = MVT::getSizeInBits(getValueType(0));
445 return ((int64_t)Value << 64-Bits) >> 64-Bits;
448 bool isNullValue() const { return Value == 0; }
449 bool isAllOnesValue() const {
450 return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1;
453 static bool classof(const ConstantSDNode *) { return true; }
454 static bool classof(const SDNode *N) {
455 return N->getOpcode() == ISD::Constant;
459 class ConstantFPSDNode : public SDNode {
462 friend class SelectionDAG;
463 ConstantFPSDNode(double val, MVT::ValueType VT)
464 : SDNode(ISD::ConstantFP, VT), Value(val) {
468 double getValue() const { return Value; }
470 /// isExactlyValue - We don't rely on operator== working on double values, as
471 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
472 /// As such, this method can be used to do an exact bit-for-bit comparison of
473 /// two floating point values.
474 bool isExactlyValue(double V) const {
488 static bool classof(const ConstantFPSDNode *) { return true; }
489 static bool classof(const SDNode *N) {
490 return N->getOpcode() == ISD::ConstantFP;
494 class GlobalAddressSDNode : public SDNode {
495 GlobalValue *TheGlobal;
497 friend class SelectionDAG;
498 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
499 : SDNode(ISD::GlobalAddress, VT) {
500 TheGlobal = const_cast<GlobalValue*>(GA);
505 GlobalValue *getGlobal() const { return TheGlobal; }
507 static bool classof(const GlobalAddressSDNode *) { return true; }
508 static bool classof(const SDNode *N) {
509 return N->getOpcode() == ISD::GlobalAddress;
514 class FrameIndexSDNode : public SDNode {
517 friend class SelectionDAG;
518 FrameIndexSDNode(int fi, MVT::ValueType VT)
519 : SDNode(ISD::FrameIndex, VT), FI(fi) {}
522 int getIndex() const { return FI; }
524 static bool classof(const FrameIndexSDNode *) { return true; }
525 static bool classof(const SDNode *N) {
526 return N->getOpcode() == ISD::FrameIndex;
530 class ConstantPoolSDNode : public SDNode {
533 friend class SelectionDAG;
534 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
535 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
538 unsigned getIndex() const { return CPI; }
540 static bool classof(const ConstantPoolSDNode *) { return true; }
541 static bool classof(const SDNode *N) {
542 return N->getOpcode() == ISD::ConstantPool;
546 class BasicBlockSDNode : public SDNode {
547 MachineBasicBlock *MBB;
549 friend class SelectionDAG;
550 BasicBlockSDNode(MachineBasicBlock *mbb)
551 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
554 MachineBasicBlock *getBasicBlock() const { return MBB; }
556 static bool classof(const BasicBlockSDNode *) { return true; }
557 static bool classof(const SDNode *N) {
558 return N->getOpcode() == ISD::BasicBlock;
563 class CopyRegSDNode : public SDNode {
566 friend class SelectionDAG;
567 CopyRegSDNode(SDOperand Chain, SDOperand Src, unsigned reg)
568 : SDNode(ISD::CopyToReg, Chain, Src), Reg(reg) {
569 setValueTypes(MVT::Other); // Just a token chain.
571 CopyRegSDNode(unsigned reg, MVT::ValueType VT)
572 : SDNode(ISD::CopyFromReg, VT), Reg(reg) {
576 unsigned getReg() const { return Reg; }
578 static bool classof(const CopyRegSDNode *) { return true; }
579 static bool classof(const SDNode *N) {
580 return N->getOpcode() == ISD::CopyToReg ||
581 N->getOpcode() == ISD::CopyFromReg;
585 class ExternalSymbolSDNode : public SDNode {
588 friend class SelectionDAG;
589 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
590 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
594 const char *getSymbol() const { return Symbol; }
596 static bool classof(const ExternalSymbolSDNode *) { return true; }
597 static bool classof(const SDNode *N) {
598 return N->getOpcode() == ISD::ExternalSymbol;
602 class SetCCSDNode : public SDNode {
603 ISD::CondCode Condition;
605 friend class SelectionDAG;
606 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
607 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
608 setValueTypes(MVT::i1);
612 ISD::CondCode getCondition() const { return Condition; }
614 static bool classof(const SetCCSDNode *) { return true; }
615 static bool classof(const SDNode *N) {
616 return N->getOpcode() == ISD::SETCC;
620 } // end llvm namespace