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"
30 class MachineBasicBlock;
32 template <typename T> struct simplify_type;
34 /// ISD namespace - This namespace contains an enum which represents all of the
35 /// SelectionDAG node types and value types.
38 //===--------------------------------------------------------------------===//
39 /// ISD::NodeType enum - This enum defines all of the operators valid in a
44 EntryToken, Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
45 BasicBlock, ExternalSymbol,
47 // CopyToReg - This node has chain and child nodes, and an associated
48 // register number. The instruction selector must guarantee that the value
49 // of the value node is available in the virtual register stored in the
50 // CopyRegSDNode object.
53 // CopyFromReg - This node indicates that the input value is a virtual or
54 // physical register that is defined outside of the scope of this
55 // SelectionDAG. The virtual register is available from the
56 // CopyRegSDNode object.
59 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
60 // a Constant, which is required to be operand #1), element of the aggregate
61 // value specified as operand #0. This is only for use before legalization,
62 // for values that will be broken into multiple registers.
65 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
66 // two values of the same integer value type, this produces a value twice as
67 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
71 // Simple binary arithmetic operators.
72 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
75 AND, OR, XOR, SHL, SRA, SRL,
80 // SetCC operator - This evaluates to a boolean (i1) true value if the
81 // condition is true. These nodes are instances of the
82 // SetCCSDNode class, which contains the condition code as extra
86 // addc - Three input, two output operator: (X, Y, C) -> (X+Y+C,
87 // Cout). X,Y are integer inputs of agreeing size, C is a one bit
88 // value, and two values are produced: the sum and a carry out.
91 // Conversion operators. These are all single input single output
92 // operations. For all of these, the result type must be strictly
93 // wider or narrower (depending on the operation) than the source
96 // SIGN_EXTEND - Used for integer types, replicating the sign bit
100 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
103 // TRUNCATE - Completely drop the high bits.
106 // FP_ROUND - Perform a rounding operation from the current
107 // precision down to the specified precision.
110 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
113 // Other operators. LOAD and STORE have token chains.
116 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
117 // to a specified boundary. The first operand is the token chain, the
118 // second is the number of bytes to allocate, and the third is the alignment
122 // Control flow instructions. These all have token chains.
124 // BR - Unconditional branch. The first operand is the chain
125 // operand, the second is the MBB to branch to.
128 // BRCOND - Conditional branch. The first operand is the chain,
129 // the second is the condition, the third is the block to branch
130 // to if the condition is true.
133 // RET - Return from function. The first operand is the chain,
134 // and any subsequent operands are the return values for the
135 // function. This operation can have variable number of operands.
138 // CALL - Call to a function pointer. The first operand is the chain, the
139 // second is the destination function pointer (a GlobalAddress for a direct
140 // call). Arguments have already been lowered to explicit DAGs according to
141 // the calling convention in effect here.
144 // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and
145 // end of a call sequence and indicate how much the stack pointer needs to
146 // be adjusted for that particular call. The first operand is a chain, the
147 // second is a ConstantSDNode of intptr type.
148 ADJCALLSTACKDOWN, // Beginning of a call sequence
149 ADJCALLSTACKUP, // End of a call sequence
152 // BUILTIN_OP_END - This must be the last enum value in this list.
156 //===--------------------------------------------------------------------===//
157 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
158 /// below work out, when considering SETFALSE (something that never exists
159 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
160 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
161 /// to. If the "N" column is 1, the result of the comparison is undefined if
162 /// the input is a NAN.
164 /// All of these (except for the 'always folded ops') should be handled for
165 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
166 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
168 /// Note that these are laid out in a specific order to allow bit-twiddling
169 /// to transform conditions.
171 // Opcode N U L G E Intuitive operation
172 SETFALSE, // 0 0 0 0 Always false (always folded)
173 SETOEQ, // 0 0 0 1 True if ordered and equal
174 SETOGT, // 0 0 1 0 True if ordered and greater than
175 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
176 SETOLT, // 0 1 0 0 True if ordered and less than
177 SETOLE, // 0 1 0 1 True if ordered and less than or equal
178 SETONE, // 0 1 1 0 True if ordered and operands are unequal
179 SETO, // 0 1 1 1 True if ordered (no nans)
180 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
181 SETUEQ, // 1 0 0 1 True if unordered or equal
182 SETUGT, // 1 0 1 0 True if unordered or greater than
183 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
184 SETULT, // 1 1 0 0 True if unordered or less than
185 SETULE, // 1 1 0 1 True if unordered, less than, or equal
186 SETUNE, // 1 1 1 0 True if unordered or not equal
187 SETTRUE, // 1 1 1 1 Always true (always folded)
188 // Don't care operations: undefined if the input is a nan.
189 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
190 SETEQ, // 1 X 0 0 1 True if equal
191 SETGT, // 1 X 0 1 0 True if greater than
192 SETGE, // 1 X 0 1 1 True if greater than or equal
193 SETLT, // 1 X 1 0 0 True if less than
194 SETLE, // 1 X 1 0 1 True if less than or equal
195 SETNE, // 1 X 1 1 0 True if not equal
196 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
198 SETCC_INVALID, // Marker value.
201 /// isSignedIntSetCC - Return true if this is a setcc instruction that
202 /// performs a signed comparison when used with integer operands.
203 inline bool isSignedIntSetCC(CondCode Code) {
204 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
207 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
208 /// performs an unsigned comparison when used with integer operands.
209 inline bool isUnsignedIntSetCC(CondCode Code) {
210 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
213 /// isTrueWhenEqual - Return true if the specified condition returns true if
214 /// the two operands to the condition are equal. Note that if one of the two
215 /// operands is a NaN, this value is meaningless.
216 inline bool isTrueWhenEqual(CondCode Cond) {
217 return ((int)Cond & 1) != 0;
220 /// getUnorderedFlavor - This function returns 0 if the condition is always
221 /// false if an operand is a NaN, 1 if the condition is always true if the
222 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
224 inline unsigned getUnorderedFlavor(CondCode Cond) {
225 return ((int)Cond >> 3) & 3;
228 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
229 /// 'op' is a valid SetCC operation.
230 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
232 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
233 /// when given the operation for (X op Y).
234 CondCode getSetCCSwappedOperands(CondCode Operation);
236 /// getSetCCOrOperation - Return the result of a logical OR between different
237 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
238 /// function returns SETCC_INVALID if it is not possible to represent the
239 /// resultant comparison.
240 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
242 /// getSetCCAndOperation - Return the result of a logical AND between
243 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
244 /// function returns SETCC_INVALID if it is not possible to represent the
245 /// resultant comparison.
246 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
247 } // end llvm::ISD namespace
250 //===----------------------------------------------------------------------===//
251 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
252 /// values as the result of a computation. Many nodes return multiple values,
253 /// from loads (which define a token and a return value) to ADDC (which returns
254 /// a result and a carry value), to calls (which may return an arbitrary number
257 /// As such, each use of a SelectionDAG computation must indicate the node that
258 /// computes it as well as which return value to use from that node. This pair
259 /// of information is represented with the SDOperand value type.
262 SDNode *Val; // The node defining the value we are using.
263 unsigned ResNo; // Which return value of the node we are using.
265 SDOperand() : Val(0) {}
266 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
268 bool operator==(const SDOperand &O) const {
269 return Val == O.Val && ResNo == O.ResNo;
271 bool operator!=(const SDOperand &O) const {
272 return !operator==(O);
274 bool operator<(const SDOperand &O) const {
275 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
278 SDOperand getValue(unsigned R) const {
279 return SDOperand(Val, R);
282 /// getValueType - Return the ValueType of the referenced return value.
284 inline MVT::ValueType getValueType() const;
286 // Forwarding methods - These forward to the corresponding methods in SDNode.
287 inline unsigned getOpcode() const;
288 inline unsigned getNumOperands() const;
289 inline const SDOperand &getOperand(unsigned i) const;
293 /// simplify_type specializations - Allow casting operators to work directly on
294 /// SDOperands as if they were SDNode*'s.
295 template<> struct simplify_type<SDOperand> {
296 typedef SDNode* SimpleType;
297 static SimpleType getSimplifiedValue(const SDOperand &Val) {
298 return static_cast<SimpleType>(Val.Val);
301 template<> struct simplify_type<const SDOperand> {
302 typedef SDNode* SimpleType;
303 static SimpleType getSimplifiedValue(const SDOperand &Val) {
304 return static_cast<SimpleType>(Val.Val);
309 /// SDNode - Represents one node in the SelectionDAG.
313 std::vector<SDOperand> Operands;
315 /// Values - The types of the values this node defines. SDNode's may define
316 /// multiple values simultaneously.
317 std::vector<MVT::ValueType> Values;
319 /// Uses - These are all of the SDNode's that use a value produced by this
321 std::vector<SDNode*> Uses;
324 //===--------------------------------------------------------------------===//
327 unsigned getOpcode() const { return NodeType; }
329 size_t use_size() const { return Uses.size(); }
330 bool use_empty() const { return Uses.empty(); }
331 bool hasOneUse() const { return Uses.size() == 1; }
333 /// getNumOperands - Return the number of values used by this operation.
335 unsigned getNumOperands() const { return Operands.size(); }
337 const SDOperand &getOperand(unsigned Num) {
338 assert(Num < Operands.size() && "Invalid child # of SDNode!");
339 return Operands[Num];
342 const SDOperand &getOperand(unsigned Num) const {
343 assert(Num < Operands.size() && "Invalid child # of SDNode!");
344 return Operands[Num];
347 /// getNumValues - Return the number of values defined/returned by this
350 unsigned getNumValues() const { return Values.size(); }
352 /// getValueType - Return the type of a specified result.
354 MVT::ValueType getValueType(unsigned ResNo) const {
355 assert(ResNo < Values.size() && "Illegal result number!");
356 return Values[ResNo];
361 static bool classof(const SDNode *) { return true; }
364 friend class SelectionDAG;
366 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT) {
368 Values.push_back(VT);
371 SDNode(unsigned NT, SDOperand Op)
373 Operands.reserve(1); Operands.push_back(Op);
374 Op.Val->Uses.push_back(this);
376 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
378 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
379 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
381 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
383 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
384 Operands.push_back(N3);
385 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
386 N3.Val->Uses.push_back(this);
388 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
389 Operands.swap(Nodes);
390 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
391 Nodes[i].Val->Uses.push_back(this);
398 void setValueTypes(MVT::ValueType VT) {
400 Values.push_back(VT);
402 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
404 Values.push_back(VT1);
405 Values.push_back(VT2);
407 /// Note: this method destroys the vector passed in.
408 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
409 std::swap(Values, VTs);
414 // Define inline functions from the SDOperand class.
416 inline unsigned SDOperand::getOpcode() const {
417 return Val->getOpcode();
419 inline MVT::ValueType SDOperand::getValueType() const {
420 return Val->getValueType(ResNo);
422 inline unsigned SDOperand::getNumOperands() const {
423 return Val->getNumOperands();
425 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
426 return Val->getOperand(i);
431 class ConstantSDNode : public SDNode {
434 friend class SelectionDAG;
435 ConstantSDNode(uint64_t val, MVT::ValueType VT)
436 : SDNode(ISD::Constant, VT), Value(val) {
440 uint64_t getValue() const { return Value; }
442 int64_t getSignExtended() const {
443 unsigned Bits = MVT::getSizeInBits(getValueType(0));
444 return ((int64_t)Value << 64-Bits) >> 64-Bits;
447 bool isNullValue() const { return Value == 0; }
448 bool isAllOnesValue() const {
449 return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1;
452 static bool classof(const ConstantSDNode *) { return true; }
453 static bool classof(const SDNode *N) {
454 return N->getOpcode() == ISD::Constant;
458 class ConstantFPSDNode : public SDNode {
461 friend class SelectionDAG;
462 ConstantFPSDNode(double val, MVT::ValueType VT)
463 : SDNode(ISD::ConstantFP, VT), Value(val) {
467 double getValue() const { return Value; }
469 /// isExactlyValue - We don't rely on operator== working on double values, as
470 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
471 /// As such, this method can be used to do an exact bit-for-bit comparison of
472 /// two floating point values.
473 bool isExactlyValue(double V) const {
487 static bool classof(const ConstantFPSDNode *) { return true; }
488 static bool classof(const SDNode *N) {
489 return N->getOpcode() == ISD::ConstantFP;
493 class GlobalAddressSDNode : public SDNode {
494 GlobalValue *TheGlobal;
496 friend class SelectionDAG;
497 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
498 : SDNode(ISD::GlobalAddress, VT) {
499 TheGlobal = const_cast<GlobalValue*>(GA);
504 GlobalValue *getGlobal() const { return TheGlobal; }
506 static bool classof(const GlobalAddressSDNode *) { return true; }
507 static bool classof(const SDNode *N) {
508 return N->getOpcode() == ISD::GlobalAddress;
513 class FrameIndexSDNode : public SDNode {
516 friend class SelectionDAG;
517 FrameIndexSDNode(int fi, MVT::ValueType VT)
518 : SDNode(ISD::FrameIndex, VT), FI(fi) {}
521 int getIndex() const { return FI; }
523 static bool classof(const FrameIndexSDNode *) { return true; }
524 static bool classof(const SDNode *N) {
525 return N->getOpcode() == ISD::FrameIndex;
529 class ConstantPoolSDNode : public SDNode {
532 friend class SelectionDAG;
533 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
534 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
537 unsigned getIndex() const { return CPI; }
539 static bool classof(const ConstantPoolSDNode *) { return true; }
540 static bool classof(const SDNode *N) {
541 return N->getOpcode() == ISD::ConstantPool;
545 class BasicBlockSDNode : public SDNode {
546 MachineBasicBlock *MBB;
548 friend class SelectionDAG;
549 BasicBlockSDNode(MachineBasicBlock *mbb)
550 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
553 MachineBasicBlock *getBasicBlock() const { return MBB; }
555 static bool classof(const BasicBlockSDNode *) { return true; }
556 static bool classof(const SDNode *N) {
557 return N->getOpcode() == ISD::BasicBlock;
562 class CopyRegSDNode : public SDNode {
565 friend class SelectionDAG;
566 CopyRegSDNode(SDOperand Chain, SDOperand Src, unsigned reg)
567 : SDNode(ISD::CopyToReg, Chain, Src), Reg(reg) {
568 setValueTypes(MVT::Other); // Just a token chain.
570 CopyRegSDNode(unsigned reg, MVT::ValueType VT)
571 : SDNode(ISD::CopyFromReg, VT), Reg(reg) {
575 unsigned getReg() const { return Reg; }
577 static bool classof(const CopyRegSDNode *) { return true; }
578 static bool classof(const SDNode *N) {
579 return N->getOpcode() == ISD::CopyToReg ||
580 N->getOpcode() == ISD::CopyFromReg;
584 class ExternalSymbolSDNode : public SDNode {
587 friend class SelectionDAG;
588 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
589 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
593 const char *getSymbol() const { return Symbol; }
595 static bool classof(const ExternalSymbolSDNode *) { return true; }
596 static bool classof(const SDNode *N) {
597 return N->getOpcode() == ISD::ExternalSymbol;
601 class SetCCSDNode : public SDNode {
602 ISD::CondCode Condition;
604 friend class SelectionDAG;
605 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
606 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
607 setValueTypes(MVT::i1);
611 ISD::CondCode getCondition() const { return Condition; }
613 static bool classof(const SetCCSDNode *) { return true; }
614 static bool classof(const SDNode *N) {
615 return N->getOpcode() == ISD::SETCC;
619 } // end llvm namespace