SelectionDAGNodes.h revision 1645ed0b93901ec678cd54024c8b09bca632b296
1//===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 7// 8//===----------------------------------------------------------------------===// 9// 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. 14// 15// Clients should include the SelectionDAG.h file instead of this file directly. 16// 17//===----------------------------------------------------------------------===// 18 19#ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H 20#define LLVM_CODEGEN_SELECTIONDAGNODES_H 21 22#include "llvm/CodeGen/ValueTypes.h" 23#include "llvm/Support/DataTypes.h" 24#include <cassert> 25#include <vector> 26 27namespace llvm { 28 29class SelectionDAG; 30class GlobalValue; 31class MachineBasicBlock; 32class SDNode; 33template <typename T> struct simplify_type; 34 35/// ISD namespace - This namespace contains an enum which represents all of the 36/// SelectionDAG node types and value types. 37/// 38namespace ISD { 39 //===--------------------------------------------------------------------===// 40 /// ISD::NodeType enum - This enum defines all of the operators valid in a 41 /// SelectionDAG. 42 /// 43 enum NodeType { 44 // Leaf nodes 45 EntryToken, Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool, 46 BasicBlock, ExternalSymbol, 47 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. 52 CopyToReg, 53 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. 58 CopyFromReg, 59 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. 64 EXTRACT_ELEMENT, 65 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. 69 BUILD_PAIR, 70 71 72 // Simple binary arithmetic operators. 73 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM, 74 75 // Bitwise operators. 76 AND, OR, XOR, SHL, SRA, SRL, 77 78 // Select operator. 79 SELECT, 80 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 84 // state. 85 SETCC, 86 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. 90 ADDC, SUBB, 91 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 95 // type. 96 97 // SIGN_EXTEND - Used for integer types, replicating the sign bit 98 // into new bits. 99 SIGN_EXTEND, 100 101 // ZERO_EXTEND - Used for integer types, zeroing the new bits. 102 ZERO_EXTEND, 103 104 // TRUNCATE - Completely drop the high bits. 105 TRUNCATE, 106 107 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign 108 // depends on the first letter) to floating point. 109 SINT_TO_FP, 110 UINT_TO_FP, 111 112 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned 113 // integer. 114 FP_TO_SINT, 115 FP_TO_UINT, 116 117 // FP_ROUND - Perform a rounding operation from the current 118 // precision down to the specified precision. 119 FP_ROUND, 120 121 // FP_EXTEND - Extend a smaller FP type into a larger FP type. 122 FP_EXTEND, 123 124 // Other operators. LOAD and STORE have token chains. 125 LOAD, STORE, 126 127 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned 128 // to a specified boundary. The first operand is the token chain, the 129 // second is the number of bytes to allocate, and the third is the alignment 130 // boundary. 131 DYNAMIC_STACKALLOC, 132 133 // Control flow instructions. These all have token chains. 134 135 // BR - Unconditional branch. The first operand is the chain 136 // operand, the second is the MBB to branch to. 137 BR, 138 139 // BRCOND - Conditional branch. The first operand is the chain, 140 // the second is the condition, the third is the block to branch 141 // to if the condition is true. 142 BRCOND, 143 144 // RET - Return from function. The first operand is the chain, 145 // and any subsequent operands are the return values for the 146 // function. This operation can have variable number of operands. 147 RET, 148 149 // CALL - Call to a function pointer. The first operand is the chain, the 150 // second is the destination function pointer (a GlobalAddress for a direct 151 // call). Arguments have already been lowered to explicit DAGs according to 152 // the calling convention in effect here. 153 CALL, 154 155 // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and 156 // end of a call sequence and indicate how much the stack pointer needs to 157 // be adjusted for that particular call. The first operand is a chain, the 158 // second is a ConstantSDNode of intptr type. 159 ADJCALLSTACKDOWN, // Beginning of a call sequence 160 ADJCALLSTACKUP, // End of a call sequence 161 162 163 // BUILTIN_OP_END - This must be the last enum value in this list. 164 BUILTIN_OP_END, 165 }; 166 167 //===--------------------------------------------------------------------===// 168 /// ISD::CondCode enum - These are ordered carefully to make the bitfields 169 /// below work out, when considering SETFALSE (something that never exists 170 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered 171 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal 172 /// to. If the "N" column is 1, the result of the comparison is undefined if 173 /// the input is a NAN. 174 /// 175 /// All of these (except for the 'always folded ops') should be handled for 176 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT, 177 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used. 178 /// 179 /// Note that these are laid out in a specific order to allow bit-twiddling 180 /// to transform conditions. 181 enum CondCode { 182 // Opcode N U L G E Intuitive operation 183 SETFALSE, // 0 0 0 0 Always false (always folded) 184 SETOEQ, // 0 0 0 1 True if ordered and equal 185 SETOGT, // 0 0 1 0 True if ordered and greater than 186 SETOGE, // 0 0 1 1 True if ordered and greater than or equal 187 SETOLT, // 0 1 0 0 True if ordered and less than 188 SETOLE, // 0 1 0 1 True if ordered and less than or equal 189 SETONE, // 0 1 1 0 True if ordered and operands are unequal 190 SETO, // 0 1 1 1 True if ordered (no nans) 191 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y) 192 SETUEQ, // 1 0 0 1 True if unordered or equal 193 SETUGT, // 1 0 1 0 True if unordered or greater than 194 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal 195 SETULT, // 1 1 0 0 True if unordered or less than 196 SETULE, // 1 1 0 1 True if unordered, less than, or equal 197 SETUNE, // 1 1 1 0 True if unordered or not equal 198 SETTRUE, // 1 1 1 1 Always true (always folded) 199 // Don't care operations: undefined if the input is a nan. 200 SETFALSE2, // 1 X 0 0 0 Always false (always folded) 201 SETEQ, // 1 X 0 0 1 True if equal 202 SETGT, // 1 X 0 1 0 True if greater than 203 SETGE, // 1 X 0 1 1 True if greater than or equal 204 SETLT, // 1 X 1 0 0 True if less than 205 SETLE, // 1 X 1 0 1 True if less than or equal 206 SETNE, // 1 X 1 1 0 True if not equal 207 SETTRUE2, // 1 X 1 1 1 Always true (always folded) 208 209 SETCC_INVALID, // Marker value. 210 }; 211 212 /// isSignedIntSetCC - Return true if this is a setcc instruction that 213 /// performs a signed comparison when used with integer operands. 214 inline bool isSignedIntSetCC(CondCode Code) { 215 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE; 216 } 217 218 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that 219 /// performs an unsigned comparison when used with integer operands. 220 inline bool isUnsignedIntSetCC(CondCode Code) { 221 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE; 222 } 223 224 /// isTrueWhenEqual - Return true if the specified condition returns true if 225 /// the two operands to the condition are equal. Note that if one of the two 226 /// operands is a NaN, this value is meaningless. 227 inline bool isTrueWhenEqual(CondCode Cond) { 228 return ((int)Cond & 1) != 0; 229 } 230 231 /// getUnorderedFlavor - This function returns 0 if the condition is always 232 /// false if an operand is a NaN, 1 if the condition is always true if the 233 /// operand is a NaN, and 2 if the condition is undefined if the operand is a 234 /// NaN. 235 inline unsigned getUnorderedFlavor(CondCode Cond) { 236 return ((int)Cond >> 3) & 3; 237 } 238 239 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where 240 /// 'op' is a valid SetCC operation. 241 CondCode getSetCCInverse(CondCode Operation, bool isInteger); 242 243 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 244 /// when given the operation for (X op Y). 245 CondCode getSetCCSwappedOperands(CondCode Operation); 246 247 /// getSetCCOrOperation - Return the result of a logical OR between different 248 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This 249 /// function returns SETCC_INVALID if it is not possible to represent the 250 /// resultant comparison. 251 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger); 252 253 /// getSetCCAndOperation - Return the result of a logical AND between 254 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 255 /// function returns SETCC_INVALID if it is not possible to represent the 256 /// resultant comparison. 257 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger); 258} // end llvm::ISD namespace 259 260 261//===----------------------------------------------------------------------===// 262/// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple 263/// values as the result of a computation. Many nodes return multiple values, 264/// from loads (which define a token and a return value) to ADDC (which returns 265/// a result and a carry value), to calls (which may return an arbitrary number 266/// of values). 267/// 268/// As such, each use of a SelectionDAG computation must indicate the node that 269/// computes it as well as which return value to use from that node. This pair 270/// of information is represented with the SDOperand value type. 271/// 272struct SDOperand { 273 SDNode *Val; // The node defining the value we are using. 274 unsigned ResNo; // Which return value of the node we are using. 275 276 SDOperand() : Val(0) {} 277 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {} 278 279 bool operator==(const SDOperand &O) const { 280 return Val == O.Val && ResNo == O.ResNo; 281 } 282 bool operator!=(const SDOperand &O) const { 283 return !operator==(O); 284 } 285 bool operator<(const SDOperand &O) const { 286 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo); 287 } 288 289 SDOperand getValue(unsigned R) const { 290 return SDOperand(Val, R); 291 } 292 293 /// getValueType - Return the ValueType of the referenced return value. 294 /// 295 inline MVT::ValueType getValueType() const; 296 297 // Forwarding methods - These forward to the corresponding methods in SDNode. 298 inline unsigned getOpcode() const; 299 inline unsigned getNumOperands() const; 300 inline const SDOperand &getOperand(unsigned i) const; 301}; 302 303 304/// simplify_type specializations - Allow casting operators to work directly on 305/// SDOperands as if they were SDNode*'s. 306template<> struct simplify_type<SDOperand> { 307 typedef SDNode* SimpleType; 308 static SimpleType getSimplifiedValue(const SDOperand &Val) { 309 return static_cast<SimpleType>(Val.Val); 310 } 311}; 312template<> struct simplify_type<const SDOperand> { 313 typedef SDNode* SimpleType; 314 static SimpleType getSimplifiedValue(const SDOperand &Val) { 315 return static_cast<SimpleType>(Val.Val); 316 } 317}; 318 319 320/// SDNode - Represents one node in the SelectionDAG. 321/// 322class SDNode { 323 unsigned NodeType; 324 std::vector<SDOperand> Operands; 325 326 /// Values - The types of the values this node defines. SDNode's may define 327 /// multiple values simultaneously. 328 std::vector<MVT::ValueType> Values; 329 330 /// Uses - These are all of the SDNode's that use a value produced by this 331 /// node. 332 std::vector<SDNode*> Uses; 333public: 334 335 //===--------------------------------------------------------------------===// 336 // Accessors 337 // 338 unsigned getOpcode() const { return NodeType; } 339 340 size_t use_size() const { return Uses.size(); } 341 bool use_empty() const { return Uses.empty(); } 342 bool hasOneUse() const { return Uses.size() == 1; } 343 344 /// getNumOperands - Return the number of values used by this operation. 345 /// 346 unsigned getNumOperands() const { return Operands.size(); } 347 348 const SDOperand &getOperand(unsigned Num) { 349 assert(Num < Operands.size() && "Invalid child # of SDNode!"); 350 return Operands[Num]; 351 } 352 353 const SDOperand &getOperand(unsigned Num) const { 354 assert(Num < Operands.size() && "Invalid child # of SDNode!"); 355 return Operands[Num]; 356 } 357 358 /// getNumValues - Return the number of values defined/returned by this 359 /// operator. 360 /// 361 unsigned getNumValues() const { return Values.size(); } 362 363 /// getValueType - Return the type of a specified result. 364 /// 365 MVT::ValueType getValueType(unsigned ResNo) const { 366 assert(ResNo < Values.size() && "Illegal result number!"); 367 return Values[ResNo]; 368 } 369 370 void dump() const; 371 372 static bool classof(const SDNode *) { return true; } 373 374protected: 375 friend class SelectionDAG; 376 377 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT) { 378 Values.reserve(1); 379 Values.push_back(VT); 380 } 381 382 SDNode(unsigned NT, SDOperand Op) 383 : NodeType(NT) { 384 Operands.reserve(1); Operands.push_back(Op); 385 Op.Val->Uses.push_back(this); 386 } 387 SDNode(unsigned NT, SDOperand N1, SDOperand N2) 388 : NodeType(NT) { 389 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2); 390 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 391 } 392 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3) 393 : NodeType(NT) { 394 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2); 395 Operands.push_back(N3); 396 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 397 N3.Val->Uses.push_back(this); 398 } 399 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) { 400 Operands.swap(Nodes); 401 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) 402 Nodes[i].Val->Uses.push_back(this); 403 } 404 405 virtual ~SDNode() { 406 // FIXME: Drop uses. 407 } 408 409 void setValueTypes(MVT::ValueType VT) { 410 Values.reserve(1); 411 Values.push_back(VT); 412 } 413 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) { 414 Values.reserve(2); 415 Values.push_back(VT1); 416 Values.push_back(VT2); 417 } 418 /// Note: this method destroys the vector passed in. 419 void setValueTypes(std::vector<MVT::ValueType> &VTs) { 420 std::swap(Values, VTs); 421 } 422 423 void removeUser(SDNode *User) { 424 // Remove this user from the operand's use list. 425 for (unsigned i = Uses.size(); ; --i) { 426 assert(i != 0 && "Didn't find user!"); 427 if (Uses[i-1] == User) { 428 Uses.erase(Uses.begin()+i-1); 429 break; 430 } 431 } 432 } 433}; 434 435 436// Define inline functions from the SDOperand class. 437 438inline unsigned SDOperand::getOpcode() const { 439 return Val->getOpcode(); 440} 441inline MVT::ValueType SDOperand::getValueType() const { 442 return Val->getValueType(ResNo); 443} 444inline unsigned SDOperand::getNumOperands() const { 445 return Val->getNumOperands(); 446} 447inline const SDOperand &SDOperand::getOperand(unsigned i) const { 448 return Val->getOperand(i); 449} 450 451 452 453class ConstantSDNode : public SDNode { 454 uint64_t Value; 455protected: 456 friend class SelectionDAG; 457 ConstantSDNode(uint64_t val, MVT::ValueType VT) 458 : SDNode(ISD::Constant, VT), Value(val) { 459 } 460public: 461 462 uint64_t getValue() const { return Value; } 463 464 int64_t getSignExtended() const { 465 unsigned Bits = MVT::getSizeInBits(getValueType(0)); 466 return ((int64_t)Value << 64-Bits) >> 64-Bits; 467 } 468 469 bool isNullValue() const { return Value == 0; } 470 bool isAllOnesValue() const { 471 return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1; 472 } 473 474 static bool classof(const ConstantSDNode *) { return true; } 475 static bool classof(const SDNode *N) { 476 return N->getOpcode() == ISD::Constant; 477 } 478}; 479 480class ConstantFPSDNode : public SDNode { 481 double Value; 482protected: 483 friend class SelectionDAG; 484 ConstantFPSDNode(double val, MVT::ValueType VT) 485 : SDNode(ISD::ConstantFP, VT), Value(val) { 486 } 487public: 488 489 double getValue() const { return Value; } 490 491 /// isExactlyValue - We don't rely on operator== working on double values, as 492 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 493 /// As such, this method can be used to do an exact bit-for-bit comparison of 494 /// two floating point values. 495 bool isExactlyValue(double V) const { 496 union { 497 double V; 498 uint64_t I; 499 } T1; 500 T1.V = Value; 501 union { 502 double V; 503 uint64_t I; 504 } T2; 505 T2.V = V; 506 return T1.I == T2.I; 507 } 508 509 static bool classof(const ConstantFPSDNode *) { return true; } 510 static bool classof(const SDNode *N) { 511 return N->getOpcode() == ISD::ConstantFP; 512 } 513}; 514 515class GlobalAddressSDNode : public SDNode { 516 GlobalValue *TheGlobal; 517protected: 518 friend class SelectionDAG; 519 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT) 520 : SDNode(ISD::GlobalAddress, VT) { 521 TheGlobal = const_cast<GlobalValue*>(GA); 522 } 523public: 524 525 GlobalValue *getGlobal() const { return TheGlobal; } 526 527 static bool classof(const GlobalAddressSDNode *) { return true; } 528 static bool classof(const SDNode *N) { 529 return N->getOpcode() == ISD::GlobalAddress; 530 } 531}; 532 533 534class FrameIndexSDNode : public SDNode { 535 int FI; 536protected: 537 friend class SelectionDAG; 538 FrameIndexSDNode(int fi, MVT::ValueType VT) 539 : SDNode(ISD::FrameIndex, VT), FI(fi) {} 540public: 541 542 int getIndex() const { return FI; } 543 544 static bool classof(const FrameIndexSDNode *) { return true; } 545 static bool classof(const SDNode *N) { 546 return N->getOpcode() == ISD::FrameIndex; 547 } 548}; 549 550class ConstantPoolSDNode : public SDNode { 551 unsigned CPI; 552protected: 553 friend class SelectionDAG; 554 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT) 555 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {} 556public: 557 558 unsigned getIndex() const { return CPI; } 559 560 static bool classof(const ConstantPoolSDNode *) { return true; } 561 static bool classof(const SDNode *N) { 562 return N->getOpcode() == ISD::ConstantPool; 563 } 564}; 565 566class BasicBlockSDNode : public SDNode { 567 MachineBasicBlock *MBB; 568protected: 569 friend class SelectionDAG; 570 BasicBlockSDNode(MachineBasicBlock *mbb) 571 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {} 572public: 573 574 MachineBasicBlock *getBasicBlock() const { return MBB; } 575 576 static bool classof(const BasicBlockSDNode *) { return true; } 577 static bool classof(const SDNode *N) { 578 return N->getOpcode() == ISD::BasicBlock; 579 } 580}; 581 582 583class CopyRegSDNode : public SDNode { 584 unsigned Reg; 585protected: 586 friend class SelectionDAG; 587 CopyRegSDNode(SDOperand Chain, SDOperand Src, unsigned reg) 588 : SDNode(ISD::CopyToReg, Chain, Src), Reg(reg) { 589 setValueTypes(MVT::Other); // Just a token chain. 590 } 591 CopyRegSDNode(unsigned reg, MVT::ValueType VT) 592 : SDNode(ISD::CopyFromReg, VT), Reg(reg) { 593 } 594public: 595 596 unsigned getReg() const { return Reg; } 597 598 static bool classof(const CopyRegSDNode *) { return true; } 599 static bool classof(const SDNode *N) { 600 return N->getOpcode() == ISD::CopyToReg || 601 N->getOpcode() == ISD::CopyFromReg; 602 } 603}; 604 605class ExternalSymbolSDNode : public SDNode { 606 const char *Symbol; 607protected: 608 friend class SelectionDAG; 609 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT) 610 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) { 611 } 612public: 613 614 const char *getSymbol() const { return Symbol; } 615 616 static bool classof(const ExternalSymbolSDNode *) { return true; } 617 static bool classof(const SDNode *N) { 618 return N->getOpcode() == ISD::ExternalSymbol; 619 } 620}; 621 622class SetCCSDNode : public SDNode { 623 ISD::CondCode Condition; 624protected: 625 friend class SelectionDAG; 626 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS) 627 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) { 628 setValueTypes(MVT::i1); 629 } 630public: 631 632 ISD::CondCode getCondition() const { return Condition; } 633 634 static bool classof(const SetCCSDNode *) { return true; } 635 static bool classof(const SDNode *N) { 636 return N->getOpcode() == ISD::SETCC; 637 } 638}; 639 640} // end llvm namespace 641 642#endif 643