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