SelectionDAGNodes.h revision 1867054643c20c3027421ab7711664b4d55fe4c6
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/ADT/GraphTraits.h" 24#include "llvm/ADT/GraphTraits.h" 25#include "llvm/ADT/iterator" 26#include "llvm/Support/DataTypes.h" 27#include <cassert> 28#include <vector> 29 30namespace llvm { 31 32class SelectionDAG; 33class GlobalValue; 34class MachineBasicBlock; 35class SDNode; 36template <typename T> struct simplify_type; 37 38/// ISD namespace - This namespace contains an enum which represents all of the 39/// SelectionDAG node types and value types. 40/// 41namespace ISD { 42 //===--------------------------------------------------------------------===// 43 /// ISD::NodeType enum - This enum defines all of the operators valid in a 44 /// SelectionDAG. 45 /// 46 enum NodeType { 47 // EntryToken - This is the marker used to indicate the start of the region. 48 EntryToken, 49 50 // Token factor - This node is takes multiple tokens as input and produces a 51 // single token result. This is used to represent the fact that the operand 52 // operators are independent of each other. 53 TokenFactor, 54 55 // Various leaf nodes. 56 Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool, 57 BasicBlock, ExternalSymbol, 58 59 // CopyToReg - This node has chain and child nodes, and an associated 60 // register number. The instruction selector must guarantee that the value 61 // of the value node is available in the register stored in the RegSDNode 62 // object. 63 CopyToReg, 64 65 // CopyFromReg - This node indicates that the input value is a virtual or 66 // physical register that is defined outside of the scope of this 67 // SelectionDAG. The register is available from the RegSDNode object. 68 CopyFromReg, 69 70 // ImplicitDef - This node indicates that the specified register is 71 // implicitly defined by some operation (e.g. its a live-in argument). This 72 // register is indicated in the RegSDNode object. The only operand to this 73 // is the token chain coming in, the only result is the token chain going 74 // out. 75 ImplicitDef, 76 77 // UNDEF - An undefined node 78 UNDEF, 79 80 // EXTRACT_ELEMENT - This is used to get the first or second (determined by 81 // a Constant, which is required to be operand #1), element of the aggregate 82 // value specified as operand #0. This is only for use before legalization, 83 // for values that will be broken into multiple registers. 84 EXTRACT_ELEMENT, 85 86 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given 87 // two values of the same integer value type, this produces a value twice as 88 // big. Like EXTRACT_ELEMENT, this can only be used before legalization. 89 BUILD_PAIR, 90 91 92 // Simple binary arithmetic operators. 93 ADD, SUB, MUL, MULHU, MULHS, SDIV, UDIV, SREM, UREM, 94 95 // Bitwise operators. 96 AND, OR, XOR, SHL, SRA, SRL, 97 98 // Select operator. 99 SELECT, 100 101 // SetCC operator - This evaluates to a boolean (i1) true value if the 102 // condition is true. These nodes are instances of the 103 // SetCCSDNode class, which contains the condition code as extra 104 // state. 105 SETCC, 106 107 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are 108 // broken into a multiple pieces each, and return the resulting pieces of 109 // doing an atomic add/sub operation. This is used to handle add/sub of 110 // expanded types. The operation ordering is: 111 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS] 112 ADD_PARTS, SUB_PARTS, 113 114 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded 115 // integer shift operations, just like ADD/SUB_PARTS. The operation 116 // ordering is: 117 // [Lo,Hi] = op [LoLHS,HiLHS], Amt 118 SHL_PARTS, SRA_PARTS, SRL_PARTS, 119 120 // Conversion operators. These are all single input single output 121 // operations. For all of these, the result type must be strictly 122 // wider or narrower (depending on the operation) than the source 123 // type. 124 125 // SIGN_EXTEND - Used for integer types, replicating the sign bit 126 // into new bits. 127 SIGN_EXTEND, 128 129 // ZERO_EXTEND - Used for integer types, zeroing the new bits. 130 ZERO_EXTEND, 131 132 // TRUNCATE - Completely drop the high bits. 133 TRUNCATE, 134 135 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign 136 // depends on the first letter) to floating point. 137 SINT_TO_FP, 138 UINT_TO_FP, 139 140 // SIGN_EXTEND_INREG/ZERO_EXTEND_INREG - These operators atomically performs 141 // a SHL/(SRA|SHL) pair to (sign|zero) extend a small value in a large 142 // integer register (e.g. sign extending the low 8 bits of a 32-bit register 143 // to fill the top 24 bits with the 7th bit). The size of the smaller type 144 // is indicated by the ExtraValueType in the MVTSDNode for the operator. 145 SIGN_EXTEND_INREG, 146 ZERO_EXTEND_INREG, 147 148 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned 149 // integer. 150 FP_TO_SINT, 151 FP_TO_UINT, 152 153 // FP_ROUND - Perform a rounding operation from the current 154 // precision down to the specified precision (currently always 64->32). 155 FP_ROUND, 156 157 // FP_ROUND_INREG - This operator takes a floating point register, and 158 // rounds it to a floating point value. It then promotes it and returns it 159 // in a register of the same size. This operation effectively just discards 160 // excess precision. The type to round down to is specified by the 161 // ExtraValueType in the MVTSDNode (currently always 64->32->64). 162 FP_ROUND_INREG, 163 164 // FP_EXTEND - Extend a smaller FP type into a larger FP type. 165 FP_EXTEND, 166 167 // FNEG, FABS - Perform unary floating point negation and absolute value 168 // operations. 169 FNEG, FABS, 170 171 // Other operators. LOAD and STORE have token chains as their first 172 // operand, then the same operands as an LLVM load/store instruction. 173 LOAD, STORE, 174 175 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators are instances of the 176 // MVTSDNode. All of these load a value from memory and extend them to a 177 // larger value (e.g. load a byte into a word register). All three of these 178 // have two operands, a chain and a pointer to load from. The extra value 179 // type is the source type being loaded. 180 // 181 // SEXTLOAD loads the integer operand and sign extends it to a larger 182 // integer result type. 183 // ZEXTLOAD loads the integer operand and zero extends it to a larger 184 // integer result type. 185 // EXTLOAD is used for two things: floating point extending loads, and 186 // integer extending loads where it doesn't matter what the high 187 // bits are set to. The code generator is allowed to codegen this 188 // into whichever operation is more efficient. 189 EXTLOAD, SEXTLOAD, ZEXTLOAD, 190 191 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a 192 // value and stores it to memory in one operation. This can be used for 193 // either integer or floating point operands, and the stored type 194 // represented as the 'extra' value type in the MVTSDNode representing the 195 // operator. This node has the same three operands as a standard store. 196 TRUNCSTORE, 197 198 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned 199 // to a specified boundary. The first operand is the token chain, the 200 // second is the number of bytes to allocate, and the third is the alignment 201 // boundary. 202 DYNAMIC_STACKALLOC, 203 204 // Control flow instructions. These all have token chains. 205 206 // BR - Unconditional branch. The first operand is the chain 207 // operand, the second is the MBB to branch to. 208 BR, 209 210 // BRCOND - Conditional branch. The first operand is the chain, 211 // the second is the condition, the third is the block to branch 212 // to if the condition is true. 213 BRCOND, 214 215 // RET - Return from function. The first operand is the chain, 216 // and any subsequent operands are the return values for the 217 // function. This operation can have variable number of operands. 218 RET, 219 220 // CALL - Call to a function pointer. The first operand is the chain, the 221 // second is the destination function pointer (a GlobalAddress for a direct 222 // call). Arguments have already been lowered to explicit DAGs according to 223 // the calling convention in effect here. 224 CALL, 225 226 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest 227 // correspond to the operands of the LLVM intrinsic functions. The only 228 // result is a token chain. The alignment argument is guaranteed to be a 229 // Constant node. 230 MEMSET, 231 MEMMOVE, 232 MEMCPY, 233 234 // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and 235 // end of a call sequence and indicate how much the stack pointer needs to 236 // be adjusted for that particular call. The first operand is a chain, the 237 // second is a ConstantSDNode of intptr type. 238 ADJCALLSTACKDOWN, // Beginning of a call sequence 239 ADJCALLSTACKUP, // End of a call sequence 240 241 // PCMARKER - This corresponds to the pcmarker intrinsic. 242 PCMARKER, 243 244 // BUILTIN_OP_END - This must be the last enum value in this list. 245 BUILTIN_OP_END, 246 }; 247 248 //===--------------------------------------------------------------------===// 249 /// ISD::CondCode enum - These are ordered carefully to make the bitfields 250 /// below work out, when considering SETFALSE (something that never exists 251 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered 252 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal 253 /// to. If the "N" column is 1, the result of the comparison is undefined if 254 /// the input is a NAN. 255 /// 256 /// All of these (except for the 'always folded ops') should be handled for 257 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT, 258 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used. 259 /// 260 /// Note that these are laid out in a specific order to allow bit-twiddling 261 /// to transform conditions. 262 enum CondCode { 263 // Opcode N U L G E Intuitive operation 264 SETFALSE, // 0 0 0 0 Always false (always folded) 265 SETOEQ, // 0 0 0 1 True if ordered and equal 266 SETOGT, // 0 0 1 0 True if ordered and greater than 267 SETOGE, // 0 0 1 1 True if ordered and greater than or equal 268 SETOLT, // 0 1 0 0 True if ordered and less than 269 SETOLE, // 0 1 0 1 True if ordered and less than or equal 270 SETONE, // 0 1 1 0 True if ordered and operands are unequal 271 SETO, // 0 1 1 1 True if ordered (no nans) 272 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y) 273 SETUEQ, // 1 0 0 1 True if unordered or equal 274 SETUGT, // 1 0 1 0 True if unordered or greater than 275 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal 276 SETULT, // 1 1 0 0 True if unordered or less than 277 SETULE, // 1 1 0 1 True if unordered, less than, or equal 278 SETUNE, // 1 1 1 0 True if unordered or not equal 279 SETTRUE, // 1 1 1 1 Always true (always folded) 280 // Don't care operations: undefined if the input is a nan. 281 SETFALSE2, // 1 X 0 0 0 Always false (always folded) 282 SETEQ, // 1 X 0 0 1 True if equal 283 SETGT, // 1 X 0 1 0 True if greater than 284 SETGE, // 1 X 0 1 1 True if greater than or equal 285 SETLT, // 1 X 1 0 0 True if less than 286 SETLE, // 1 X 1 0 1 True if less than or equal 287 SETNE, // 1 X 1 1 0 True if not equal 288 SETTRUE2, // 1 X 1 1 1 Always true (always folded) 289 290 SETCC_INVALID, // Marker value. 291 }; 292 293 /// isSignedIntSetCC - Return true if this is a setcc instruction that 294 /// performs a signed comparison when used with integer operands. 295 inline bool isSignedIntSetCC(CondCode Code) { 296 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE; 297 } 298 299 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that 300 /// performs an unsigned comparison when used with integer operands. 301 inline bool isUnsignedIntSetCC(CondCode Code) { 302 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE; 303 } 304 305 /// isTrueWhenEqual - Return true if the specified condition returns true if 306 /// the two operands to the condition are equal. Note that if one of the two 307 /// operands is a NaN, this value is meaningless. 308 inline bool isTrueWhenEqual(CondCode Cond) { 309 return ((int)Cond & 1) != 0; 310 } 311 312 /// getUnorderedFlavor - This function returns 0 if the condition is always 313 /// false if an operand is a NaN, 1 if the condition is always true if the 314 /// operand is a NaN, and 2 if the condition is undefined if the operand is a 315 /// NaN. 316 inline unsigned getUnorderedFlavor(CondCode Cond) { 317 return ((int)Cond >> 3) & 3; 318 } 319 320 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where 321 /// 'op' is a valid SetCC operation. 322 CondCode getSetCCInverse(CondCode Operation, bool isInteger); 323 324 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 325 /// when given the operation for (X op Y). 326 CondCode getSetCCSwappedOperands(CondCode Operation); 327 328 /// getSetCCOrOperation - Return the result of a logical OR between different 329 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This 330 /// function returns SETCC_INVALID if it is not possible to represent the 331 /// resultant comparison. 332 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger); 333 334 /// getSetCCAndOperation - Return the result of a logical AND between 335 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 336 /// function returns SETCC_INVALID if it is not possible to represent the 337 /// resultant comparison. 338 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger); 339} // end llvm::ISD namespace 340 341 342//===----------------------------------------------------------------------===// 343/// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple 344/// values as the result of a computation. Many nodes return multiple values, 345/// from loads (which define a token and a return value) to ADDC (which returns 346/// a result and a carry value), to calls (which may return an arbitrary number 347/// of values). 348/// 349/// As such, each use of a SelectionDAG computation must indicate the node that 350/// computes it as well as which return value to use from that node. This pair 351/// of information is represented with the SDOperand value type. 352/// 353class SDOperand { 354public: 355 SDNode *Val; // The node defining the value we are using. 356 unsigned ResNo; // Which return value of the node we are using. 357 358 SDOperand() : Val(0) {} 359 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {} 360 361 bool operator==(const SDOperand &O) const { 362 return Val == O.Val && ResNo == O.ResNo; 363 } 364 bool operator!=(const SDOperand &O) const { 365 return !operator==(O); 366 } 367 bool operator<(const SDOperand &O) const { 368 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo); 369 } 370 371 SDOperand getValue(unsigned R) const { 372 return SDOperand(Val, R); 373 } 374 375 /// getValueType - Return the ValueType of the referenced return value. 376 /// 377 inline MVT::ValueType getValueType() const; 378 379 // Forwarding methods - These forward to the corresponding methods in SDNode. 380 inline unsigned getOpcode() const; 381 inline unsigned getNodeDepth() const; 382 inline unsigned getNumOperands() const; 383 inline const SDOperand &getOperand(unsigned i) const; 384 385 /// hasOneUse - Return true if there is exactly one operation using this 386 /// result value of the defining operator. 387 inline bool hasOneUse() const; 388}; 389 390 391/// simplify_type specializations - Allow casting operators to work directly on 392/// SDOperands as if they were SDNode*'s. 393template<> struct simplify_type<SDOperand> { 394 typedef SDNode* SimpleType; 395 static SimpleType getSimplifiedValue(const SDOperand &Val) { 396 return static_cast<SimpleType>(Val.Val); 397 } 398}; 399template<> struct simplify_type<const SDOperand> { 400 typedef SDNode* SimpleType; 401 static SimpleType getSimplifiedValue(const SDOperand &Val) { 402 return static_cast<SimpleType>(Val.Val); 403 } 404}; 405 406 407/// SDNode - Represents one node in the SelectionDAG. 408/// 409class SDNode { 410 /// NodeType - The operation that this node performs. 411 /// 412 unsigned short NodeType; 413 414 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This 415 /// means that leaves have a depth of 1, things that use only leaves have a 416 /// depth of 2, etc. 417 unsigned short NodeDepth; 418 419 /// Operands - The values that are used by this operation. 420 /// 421 std::vector<SDOperand> Operands; 422 423 /// Values - The types of the values this node defines. SDNode's may define 424 /// multiple values simultaneously. 425 std::vector<MVT::ValueType> Values; 426 427 /// Uses - These are all of the SDNode's that use a value produced by this 428 /// node. 429 std::vector<SDNode*> Uses; 430public: 431 432 //===--------------------------------------------------------------------===// 433 // Accessors 434 // 435 unsigned getOpcode() const { return NodeType; } 436 437 size_t use_size() const { return Uses.size(); } 438 bool use_empty() const { return Uses.empty(); } 439 bool hasOneUse() const { return Uses.size() == 1; } 440 441 /// getNodeDepth - Return the distance from this node to the leaves in the 442 /// graph. The leaves have a depth of 1. 443 unsigned getNodeDepth() const { return NodeDepth; } 444 445 typedef std::vector<SDNode*>::const_iterator use_iterator; 446 use_iterator use_begin() const { return Uses.begin(); } 447 use_iterator use_end() const { return Uses.end(); } 448 449 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 450 /// indicated value. This method ignores uses of other values defined by this 451 /// operation. 452 bool hasNUsesOfValue(unsigned NUses, unsigned Value); 453 454 /// getNumOperands - Return the number of values used by this operation. 455 /// 456 unsigned getNumOperands() const { return Operands.size(); } 457 458 const SDOperand &getOperand(unsigned Num) { 459 assert(Num < Operands.size() && "Invalid child # of SDNode!"); 460 return Operands[Num]; 461 } 462 463 const SDOperand &getOperand(unsigned Num) const { 464 assert(Num < Operands.size() && "Invalid child # of SDNode!"); 465 return Operands[Num]; 466 } 467 468 /// getNumValues - Return the number of values defined/returned by this 469 /// operator. 470 /// 471 unsigned getNumValues() const { return Values.size(); } 472 473 /// getValueType - Return the type of a specified result. 474 /// 475 MVT::ValueType getValueType(unsigned ResNo) const { 476 assert(ResNo < Values.size() && "Illegal result number!"); 477 return Values[ResNo]; 478 } 479 480 /// getOperationName - Return the opcode of this operation for printing. 481 /// 482 const char* getOperationName() const; 483 void dump() const; 484 485 static bool classof(const SDNode *) { return true; } 486 487protected: 488 friend class SelectionDAG; 489 490 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) { 491 Values.reserve(1); 492 Values.push_back(VT); 493 } 494 SDNode(unsigned NT, SDOperand Op) 495 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) { 496 Operands.reserve(1); Operands.push_back(Op); 497 Op.Val->Uses.push_back(this); 498 } 499 SDNode(unsigned NT, SDOperand N1, SDOperand N2) 500 : NodeType(NT) { 501 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth()) 502 NodeDepth = N1.Val->getNodeDepth()+1; 503 else 504 NodeDepth = N2.Val->getNodeDepth()+1; 505 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2); 506 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 507 } 508 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3) 509 : NodeType(NT) { 510 unsigned ND = N1.Val->getNodeDepth(); 511 if (ND < N2.Val->getNodeDepth()) 512 ND = N2.Val->getNodeDepth(); 513 if (ND < N3.Val->getNodeDepth()) 514 ND = N3.Val->getNodeDepth(); 515 NodeDepth = ND+1; 516 517 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2); 518 Operands.push_back(N3); 519 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 520 N3.Val->Uses.push_back(this); 521 } 522 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) { 523 Operands.swap(Nodes); 524 unsigned ND = 0; 525 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 526 Operands[i].Val->Uses.push_back(this); 527 if (ND < Operands[i].Val->getNodeDepth()) 528 ND = Operands[i].Val->getNodeDepth(); 529 } 530 NodeDepth = ND+1; 531 } 532 533 virtual ~SDNode() { 534 // FIXME: Drop uses. 535 } 536 537 void setValueTypes(MVT::ValueType VT) { 538 Values.reserve(1); 539 Values.push_back(VT); 540 } 541 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) { 542 Values.reserve(2); 543 Values.push_back(VT1); 544 Values.push_back(VT2); 545 } 546 /// Note: this method destroys the vector passed in. 547 void setValueTypes(std::vector<MVT::ValueType> &VTs) { 548 std::swap(Values, VTs); 549 } 550 551 void removeUser(SDNode *User) { 552 // Remove this user from the operand's use list. 553 for (unsigned i = Uses.size(); ; --i) { 554 assert(i != 0 && "Didn't find user!"); 555 if (Uses[i-1] == User) { 556 Uses.erase(Uses.begin()+i-1); 557 break; 558 } 559 } 560 } 561}; 562 563 564// Define inline functions from the SDOperand class. 565 566inline unsigned SDOperand::getOpcode() const { 567 return Val->getOpcode(); 568} 569inline unsigned SDOperand::getNodeDepth() const { 570 return Val->getNodeDepth(); 571} 572inline MVT::ValueType SDOperand::getValueType() const { 573 return Val->getValueType(ResNo); 574} 575inline unsigned SDOperand::getNumOperands() const { 576 return Val->getNumOperands(); 577} 578inline const SDOperand &SDOperand::getOperand(unsigned i) const { 579 return Val->getOperand(i); 580} 581inline bool SDOperand::hasOneUse() const { 582 return Val->hasNUsesOfValue(1, ResNo); 583} 584 585 586class ConstantSDNode : public SDNode { 587 uint64_t Value; 588protected: 589 friend class SelectionDAG; 590 ConstantSDNode(uint64_t val, MVT::ValueType VT) 591 : SDNode(ISD::Constant, VT), Value(val) { 592 } 593public: 594 595 uint64_t getValue() const { return Value; } 596 597 int64_t getSignExtended() const { 598 unsigned Bits = MVT::getSizeInBits(getValueType(0)); 599 return ((int64_t)Value << (64-Bits)) >> (64-Bits); 600 } 601 602 bool isNullValue() const { return Value == 0; } 603 bool isAllOnesValue() const { 604 return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1; 605 } 606 607 static bool classof(const ConstantSDNode *) { return true; } 608 static bool classof(const SDNode *N) { 609 return N->getOpcode() == ISD::Constant; 610 } 611}; 612 613class ConstantFPSDNode : public SDNode { 614 double Value; 615protected: 616 friend class SelectionDAG; 617 ConstantFPSDNode(double val, MVT::ValueType VT) 618 : SDNode(ISD::ConstantFP, VT), Value(val) { 619 } 620public: 621 622 double getValue() const { return Value; } 623 624 /// isExactlyValue - We don't rely on operator== working on double values, as 625 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 626 /// As such, this method can be used to do an exact bit-for-bit comparison of 627 /// two floating point values. 628 bool isExactlyValue(double V) const { 629 union { 630 double V; 631 uint64_t I; 632 } T1; 633 T1.V = Value; 634 union { 635 double V; 636 uint64_t I; 637 } T2; 638 T2.V = V; 639 return T1.I == T2.I; 640 } 641 642 static bool classof(const ConstantFPSDNode *) { return true; } 643 static bool classof(const SDNode *N) { 644 return N->getOpcode() == ISD::ConstantFP; 645 } 646}; 647 648class GlobalAddressSDNode : public SDNode { 649 GlobalValue *TheGlobal; 650protected: 651 friend class SelectionDAG; 652 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT) 653 : SDNode(ISD::GlobalAddress, VT) { 654 TheGlobal = const_cast<GlobalValue*>(GA); 655 } 656public: 657 658 GlobalValue *getGlobal() const { return TheGlobal; } 659 660 static bool classof(const GlobalAddressSDNode *) { return true; } 661 static bool classof(const SDNode *N) { 662 return N->getOpcode() == ISD::GlobalAddress; 663 } 664}; 665 666 667class FrameIndexSDNode : public SDNode { 668 int FI; 669protected: 670 friend class SelectionDAG; 671 FrameIndexSDNode(int fi, MVT::ValueType VT) 672 : SDNode(ISD::FrameIndex, VT), FI(fi) {} 673public: 674 675 int getIndex() const { return FI; } 676 677 static bool classof(const FrameIndexSDNode *) { return true; } 678 static bool classof(const SDNode *N) { 679 return N->getOpcode() == ISD::FrameIndex; 680 } 681}; 682 683class ConstantPoolSDNode : public SDNode { 684 unsigned CPI; 685protected: 686 friend class SelectionDAG; 687 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT) 688 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {} 689public: 690 691 unsigned getIndex() const { return CPI; } 692 693 static bool classof(const ConstantPoolSDNode *) { return true; } 694 static bool classof(const SDNode *N) { 695 return N->getOpcode() == ISD::ConstantPool; 696 } 697}; 698 699class BasicBlockSDNode : public SDNode { 700 MachineBasicBlock *MBB; 701protected: 702 friend class SelectionDAG; 703 BasicBlockSDNode(MachineBasicBlock *mbb) 704 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {} 705public: 706 707 MachineBasicBlock *getBasicBlock() const { return MBB; } 708 709 static bool classof(const BasicBlockSDNode *) { return true; } 710 static bool classof(const SDNode *N) { 711 return N->getOpcode() == ISD::BasicBlock; 712 } 713}; 714 715 716class RegSDNode : public SDNode { 717 unsigned Reg; 718protected: 719 friend class SelectionDAG; 720 RegSDNode(unsigned Opc, SDOperand Chain, SDOperand Src, unsigned reg) 721 : SDNode(Opc, Chain, Src), Reg(reg) { 722 } 723 RegSDNode(unsigned Opc, SDOperand Chain, unsigned reg) 724 : SDNode(Opc, Chain), Reg(reg) {} 725public: 726 727 unsigned getReg() const { return Reg; } 728 729 static bool classof(const RegSDNode *) { return true; } 730 static bool classof(const SDNode *N) { 731 return N->getOpcode() == ISD::CopyToReg || 732 N->getOpcode() == ISD::CopyFromReg || 733 N->getOpcode() == ISD::ImplicitDef; 734 } 735}; 736 737class ExternalSymbolSDNode : public SDNode { 738 const char *Symbol; 739protected: 740 friend class SelectionDAG; 741 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT) 742 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) { 743 } 744public: 745 746 const char *getSymbol() const { return Symbol; } 747 748 static bool classof(const ExternalSymbolSDNode *) { return true; } 749 static bool classof(const SDNode *N) { 750 return N->getOpcode() == ISD::ExternalSymbol; 751 } 752}; 753 754class SetCCSDNode : public SDNode { 755 ISD::CondCode Condition; 756protected: 757 friend class SelectionDAG; 758 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS) 759 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) { 760 } 761public: 762 763 ISD::CondCode getCondition() const { return Condition; } 764 765 static bool classof(const SetCCSDNode *) { return true; } 766 static bool classof(const SDNode *N) { 767 return N->getOpcode() == ISD::SETCC; 768 } 769}; 770 771/// MVTSDNode - This class is used for operators that require an extra 772/// value-type to be kept with the node. 773class MVTSDNode : public SDNode { 774 MVT::ValueType ExtraValueType; 775protected: 776 friend class SelectionDAG; 777 MVTSDNode(unsigned Opc, MVT::ValueType VT1, SDOperand Op0, MVT::ValueType EVT) 778 : SDNode(Opc, Op0), ExtraValueType(EVT) { 779 setValueTypes(VT1); 780 } 781 MVTSDNode(unsigned Opc, MVT::ValueType VT1, MVT::ValueType VT2, 782 SDOperand Op0, SDOperand Op1, MVT::ValueType EVT) 783 : SDNode(Opc, Op0, Op1), ExtraValueType(EVT) { 784 setValueTypes(VT1, VT2); 785 } 786 MVTSDNode(unsigned Opc, MVT::ValueType VT, 787 SDOperand Op0, SDOperand Op1, SDOperand Op2, MVT::ValueType EVT) 788 : SDNode(Opc, Op0, Op1, Op2), ExtraValueType(EVT) { 789 setValueTypes(VT); 790 } 791public: 792 793 MVT::ValueType getExtraValueType() const { return ExtraValueType; } 794 795 static bool classof(const MVTSDNode *) { return true; } 796 static bool classof(const SDNode *N) { 797 return 798 N->getOpcode() == ISD::SIGN_EXTEND_INREG || 799 N->getOpcode() == ISD::ZERO_EXTEND_INREG || 800 N->getOpcode() == ISD::FP_ROUND_INREG || 801 N->getOpcode() == ISD::EXTLOAD || 802 N->getOpcode() == ISD::SEXTLOAD || 803 N->getOpcode() == ISD::ZEXTLOAD || 804 N->getOpcode() == ISD::TRUNCSTORE; 805 } 806}; 807 808class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> { 809 SDNode *Node; 810 unsigned Operand; 811 812 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {} 813public: 814 bool operator==(const SDNodeIterator& x) const { 815 return Operand == x.Operand; 816 } 817 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); } 818 819 const SDNodeIterator &operator=(const SDNodeIterator &I) { 820 assert(I.Node == Node && "Cannot assign iterators to two different nodes!"); 821 Operand = I.Operand; 822 return *this; 823 } 824 825 pointer operator*() const { 826 return Node->getOperand(Operand).Val; 827 } 828 pointer operator->() const { return operator*(); } 829 830 SDNodeIterator& operator++() { // Preincrement 831 ++Operand; 832 return *this; 833 } 834 SDNodeIterator operator++(int) { // Postincrement 835 SDNodeIterator tmp = *this; ++*this; return tmp; 836 } 837 838 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); } 839 static SDNodeIterator end (SDNode *N) { 840 return SDNodeIterator(N, N->getNumOperands()); 841 } 842 843 unsigned getOperand() const { return Operand; } 844 const SDNode *getNode() const { return Node; } 845}; 846 847template <> struct GraphTraits<SDNode*> { 848 typedef SDNode NodeType; 849 typedef SDNodeIterator ChildIteratorType; 850 static inline NodeType *getEntryNode(SDNode *N) { return N; } 851 static inline ChildIteratorType child_begin(NodeType *N) { 852 return SDNodeIterator::begin(N); 853 } 854 static inline ChildIteratorType child_end(NodeType *N) { 855 return SDNodeIterator::end(N); 856 } 857}; 858 859 860 861 862} // end llvm namespace 863 864#endif 865