SelectionDAGNodes.h revision 002d83418a7be0083752fa910a6187e65124c78e
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/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; 37template <typename T> struct ilist_traits; 38template<typename NodeTy, typename Traits> class iplist; 39template<typename NodeTy> class ilist_iterator; 40 41/// ISD namespace - This namespace contains an enum which represents all of the 42/// SelectionDAG node types and value types. 43/// 44namespace ISD { 45 //===--------------------------------------------------------------------===// 46 /// ISD::NodeType enum - This enum defines all of the operators valid in a 47 /// SelectionDAG. 48 /// 49 enum NodeType { 50 // EntryToken - This is the marker used to indicate the start of the region. 51 EntryToken, 52 53 // Token factor - This node takes multiple tokens as input and produces a 54 // single token result. This is used to represent the fact that the operand 55 // operators are independent of each other. 56 TokenFactor, 57 58 // AssertSext, AssertZext - These nodes record if a register contains a 59 // value that has already been zero or sign extended from a narrower type. 60 // These nodes take two operands. The first is the node that has already 61 // been extended, and the second is a value type node indicating the width 62 // of the extension 63 AssertSext, AssertZext, 64 65 // Various leaf nodes. 66 Constant, ConstantFP, STRING, 67 GlobalAddress, FrameIndex, ConstantPool, 68 BasicBlock, ExternalSymbol, VALUETYPE, CONDCODE, Register, 69 70 // ConstantVec works like Constant or ConstantFP, except that it is not a 71 // leaf node. All operands are either Constant or ConstantFP nodes. 72 ConstantVec, 73 74 // TargetConstant - Like Constant, but the DAG does not do any folding or 75 // simplification of the constant. This is used by the DAG->DAG selector. 76 TargetConstant, 77 78 // TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or 79 // anything else with this node, and this is valid in the target-specific 80 // dag, turning into a GlobalAddress operand. 81 TargetGlobalAddress, 82 TargetFrameIndex, 83 TargetConstantPool, 84 TargetExternalSymbol, 85 86 // CopyToReg - This node has three operands: a chain, a register number to 87 // set to this value, and a value. 88 CopyToReg, 89 90 // CopyFromReg - This node indicates that the input value is a virtual or 91 // physical register that is defined outside of the scope of this 92 // SelectionDAG. The register is available from the RegSDNode object. 93 CopyFromReg, 94 95 // UNDEF - An undefined node 96 UNDEF, 97 98 // EXTRACT_ELEMENT - This is used to get the first or second (determined by 99 // a Constant, which is required to be operand #1), element of the aggregate 100 // value specified as operand #0. This is only for use before legalization, 101 // for values that will be broken into multiple registers. 102 EXTRACT_ELEMENT, 103 104 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given 105 // two values of the same integer value type, this produces a value twice as 106 // big. Like EXTRACT_ELEMENT, this can only be used before legalization. 107 BUILD_PAIR, 108 109 // MERGE_VALUES - This node takes multiple discrete operands and returns 110 // them all as its individual results. This nodes has exactly the same 111 // number of inputs and outputs, and is only valid before legalization. 112 // This node is useful for some pieces of the code generator that want to 113 // think about a single node with multiple results, not multiple nodes. 114 MERGE_VALUES, 115 116 // Simple integer binary arithmetic operators. 117 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM, 118 119 // Simple binary floating point operators. 120 FADD, FSUB, FMUL, FDIV, FREM, 121 122 // Simple abstract vector operators. Unlike the integer and floating point 123 // binary operators, these nodes also take two additional operands: 124 // a constant element count, and a value type node indicating the type of 125 // the elements. The order is op0, op1, count, type. All vector opcodes, 126 // including VLOAD, must currently have count and type as their 3rd and 4th 127 // arguments. 128 VADD, VSUB, VMUL, 129 130 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing 131 // an unsigned/signed value of type i[2*n], then return the top part. 132 MULHU, MULHS, 133 134 // Bitwise operators. 135 AND, OR, XOR, SHL, SRA, SRL, 136 137 // Counting operators 138 CTTZ, CTLZ, CTPOP, 139 140 // Select 141 SELECT, 142 143 // Select with condition operator - This selects between a true value and 144 // a false value (ops #2 and #3) based on the boolean result of comparing 145 // the lhs and rhs (ops #0 and #1) of a conditional expression with the 146 // condition code in op #4, a CondCodeSDNode. 147 SELECT_CC, 148 149 // SetCC operator - This evaluates to a boolean (i1) true value if the 150 // condition is true. The operands to this are the left and right operands 151 // to compare (ops #0, and #1) and the condition code to compare them with 152 // (op #2) as a CondCodeSDNode. 153 SETCC, 154 155 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are 156 // broken into a multiple pieces each, and return the resulting pieces of 157 // doing an atomic add/sub operation. This is used to handle add/sub of 158 // expanded types. The operation ordering is: 159 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS] 160 ADD_PARTS, SUB_PARTS, 161 162 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded 163 // integer shift operations, just like ADD/SUB_PARTS. The operation 164 // ordering is: 165 // [Lo,Hi] = op [LoLHS,HiLHS], Amt 166 SHL_PARTS, SRA_PARTS, SRL_PARTS, 167 168 // Conversion operators. These are all single input single output 169 // operations. For all of these, the result type must be strictly 170 // wider or narrower (depending on the operation) than the source 171 // type. 172 173 // SIGN_EXTEND - Used for integer types, replicating the sign bit 174 // into new bits. 175 SIGN_EXTEND, 176 177 // ZERO_EXTEND - Used for integer types, zeroing the new bits. 178 ZERO_EXTEND, 179 180 // ANY_EXTEND - Used for integer types. The high bits are undefined. 181 ANY_EXTEND, 182 183 // TRUNCATE - Completely drop the high bits. 184 TRUNCATE, 185 186 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign 187 // depends on the first letter) to floating point. 188 SINT_TO_FP, 189 UINT_TO_FP, 190 191 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to 192 // sign extend a small value in a large integer register (e.g. sign 193 // extending the low 8 bits of a 32-bit register to fill the top 24 bits 194 // with the 7th bit). The size of the smaller type is indicated by the 1th 195 // operand, a ValueType node. 196 SIGN_EXTEND_INREG, 197 198 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned 199 // integer. 200 FP_TO_SINT, 201 FP_TO_UINT, 202 203 // FP_ROUND - Perform a rounding operation from the current 204 // precision down to the specified precision (currently always 64->32). 205 FP_ROUND, 206 207 // FP_ROUND_INREG - This operator takes a floating point register, and 208 // rounds it to a floating point value. It then promotes it and returns it 209 // in a register of the same size. This operation effectively just discards 210 // excess precision. The type to round down to is specified by the 1th 211 // operation, a VTSDNode (currently always 64->32->64). 212 FP_ROUND_INREG, 213 214 // FP_EXTEND - Extend a smaller FP type into a larger FP type. 215 FP_EXTEND, 216 217 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation, 218 // absolute value, square root, sine and cosine operations. 219 FNEG, FABS, FSQRT, FSIN, FCOS, 220 221 // Other operators. LOAD and STORE have token chains as their first 222 // operand, then the same operands as an LLVM load/store instruction, then a 223 // SRCVALUE node that provides alias analysis information. 224 LOAD, STORE, 225 226 // Abstract vector version of LOAD. VLOAD has a token chain as the first 227 // operand, followed by a pointer operand, a constant element count, a value 228 // type node indicating the type of the elements, and a SRCVALUE node. 229 VLOAD, 230 231 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators all load a value from 232 // memory and extend them to a larger value (e.g. load a byte into a word 233 // register). All three of these have four operands, a token chain, a 234 // pointer to load from, a SRCVALUE for alias analysis, and a VALUETYPE node 235 // indicating the type to load. 236 // 237 // SEXTLOAD loads the integer operand and sign extends it to a larger 238 // integer result type. 239 // ZEXTLOAD loads the integer operand and zero extends it to a larger 240 // integer result type. 241 // EXTLOAD is used for two things: floating point extending loads, and 242 // integer extending loads where it doesn't matter what the high 243 // bits are set to. The code generator is allowed to codegen this 244 // into whichever operation is more efficient. 245 EXTLOAD, SEXTLOAD, ZEXTLOAD, 246 247 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a 248 // value and stores it to memory in one operation. This can be used for 249 // either integer or floating point operands. The first four operands of 250 // this are the same as a standard store. The fifth is the ValueType to 251 // store it as (which will be smaller than the source value). 252 TRUNCSTORE, 253 254 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned 255 // to a specified boundary. The first operand is the token chain, the 256 // second is the number of bytes to allocate, and the third is the alignment 257 // boundary. The size is guaranteed to be a multiple of the stack 258 // alignment, and the alignment is guaranteed to be bigger than the stack 259 // alignment (if required) or 0 to get standard stack alignment. 260 DYNAMIC_STACKALLOC, 261 262 // Control flow instructions. These all have token chains. 263 264 // BR - Unconditional branch. The first operand is the chain 265 // operand, the second is the MBB to branch to. 266 BR, 267 268 // BRCOND - Conditional branch. The first operand is the chain, 269 // the second is the condition, the third is the block to branch 270 // to if the condition is true. 271 BRCOND, 272 273 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the 274 // chain, the second is the condition, the third is the block to branch to 275 // if true, and the forth is the block to branch to if false. Targets 276 // usually do not implement this, preferring to have legalize demote the 277 // operation to BRCOND/BR pairs when necessary. 278 BRCONDTWOWAY, 279 280 // BR_CC - Conditional branch. The behavior is like that of SELECT_CC, in 281 // that the condition is represented as condition code, and two nodes to 282 // compare, rather than as a combined SetCC node. The operands in order are 283 // chain, cc, lhs, rhs, block to branch to if condition is true. 284 BR_CC, 285 286 // BRTWOWAY_CC - Two-way conditional branch. The operands in order are 287 // chain, cc, lhs, rhs, block to branch to if condition is true, block to 288 // branch to if condition is false. Targets usually do not implement this, 289 // preferring to have legalize demote the operation to BRCOND/BR pairs. 290 BRTWOWAY_CC, 291 292 // RET - Return from function. The first operand is the chain, 293 // and any subsequent operands are the return values for the 294 // function. This operation can have variable number of operands. 295 RET, 296 297 // CALL - Call to a function pointer. The first operand is the chain, the 298 // second is the destination function pointer (a GlobalAddress for a direct 299 // call). Arguments have already been lowered to explicit DAGs according to 300 // the calling convention in effect here. TAILCALL is the same as CALL, but 301 // the callee is known not to access the stack of the caller. 302 CALL, 303 TAILCALL, 304 305 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest 306 // correspond to the operands of the LLVM intrinsic functions. The only 307 // result is a token chain. The alignment argument is guaranteed to be a 308 // Constant node. 309 MEMSET, 310 MEMMOVE, 311 MEMCPY, 312 313 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of 314 // a call sequence, and carry arbitrary information that target might want 315 // to know. The first operand is a chain, the rest are specified by the 316 // target and not touched by the DAG optimizers. 317 CALLSEQ_START, // Beginning of a call sequence 318 CALLSEQ_END, // End of a call sequence 319 320 // SRCVALUE - This corresponds to a Value*, and is used to associate memory 321 // locations with their value. This allows one use alias analysis 322 // information in the backend. 323 SRCVALUE, 324 325 // PCMARKER - This corresponds to the pcmarker intrinsic. 326 PCMARKER, 327 328 // READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic. 329 // The only operand is a chain and a value and a chain are produced. The 330 // value is the contents of the architecture specific cycle counter like 331 // register (or other high accuracy low latency clock source) 332 READCYCLECOUNTER, 333 334 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM 335 // intrinsics of the same name. The first operand is a token chain, the 336 // other operands match the intrinsic. These produce a token chain in 337 // addition to a value (if any). 338 READPORT, WRITEPORT, READIO, WRITEIO, 339 340 // HANDLENODE node - Used as a handle for various purposes. 341 HANDLENODE, 342 343 // LOCATION - This node is used to represent a source location for debug 344 // info. It takes token chain as input, then a line number, then a column 345 // number, then a filename, then a working dir. It produces a token chain 346 // as output. 347 LOCATION, 348 349 // DEBUG_LOC - This node is used to represent source line information 350 // embedded in the code. It takes token chain as input, then a line number, 351 // then a column then a file id (provided by MachineDebugInfo. It produces 352 // a token chain as output. 353 DEBUG_LOC, 354 355 // BUILTIN_OP_END - This must be the last enum value in this list. 356 BUILTIN_OP_END, 357 }; 358 359 //===--------------------------------------------------------------------===// 360 /// ISD::CondCode enum - These are ordered carefully to make the bitfields 361 /// below work out, when considering SETFALSE (something that never exists 362 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered 363 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal 364 /// to. If the "N" column is 1, the result of the comparison is undefined if 365 /// the input is a NAN. 366 /// 367 /// All of these (except for the 'always folded ops') should be handled for 368 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT, 369 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used. 370 /// 371 /// Note that these are laid out in a specific order to allow bit-twiddling 372 /// to transform conditions. 373 enum CondCode { 374 // Opcode N U L G E Intuitive operation 375 SETFALSE, // 0 0 0 0 Always false (always folded) 376 SETOEQ, // 0 0 0 1 True if ordered and equal 377 SETOGT, // 0 0 1 0 True if ordered and greater than 378 SETOGE, // 0 0 1 1 True if ordered and greater than or equal 379 SETOLT, // 0 1 0 0 True if ordered and less than 380 SETOLE, // 0 1 0 1 True if ordered and less than or equal 381 SETONE, // 0 1 1 0 True if ordered and operands are unequal 382 SETO, // 0 1 1 1 True if ordered (no nans) 383 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y) 384 SETUEQ, // 1 0 0 1 True if unordered or equal 385 SETUGT, // 1 0 1 0 True if unordered or greater than 386 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal 387 SETULT, // 1 1 0 0 True if unordered or less than 388 SETULE, // 1 1 0 1 True if unordered, less than, or equal 389 SETUNE, // 1 1 1 0 True if unordered or not equal 390 SETTRUE, // 1 1 1 1 Always true (always folded) 391 // Don't care operations: undefined if the input is a nan. 392 SETFALSE2, // 1 X 0 0 0 Always false (always folded) 393 SETEQ, // 1 X 0 0 1 True if equal 394 SETGT, // 1 X 0 1 0 True if greater than 395 SETGE, // 1 X 0 1 1 True if greater than or equal 396 SETLT, // 1 X 1 0 0 True if less than 397 SETLE, // 1 X 1 0 1 True if less than or equal 398 SETNE, // 1 X 1 1 0 True if not equal 399 SETTRUE2, // 1 X 1 1 1 Always true (always folded) 400 401 SETCC_INVALID, // Marker value. 402 }; 403 404 /// isSignedIntSetCC - Return true if this is a setcc instruction that 405 /// performs a signed comparison when used with integer operands. 406 inline bool isSignedIntSetCC(CondCode Code) { 407 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE; 408 } 409 410 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that 411 /// performs an unsigned comparison when used with integer operands. 412 inline bool isUnsignedIntSetCC(CondCode Code) { 413 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE; 414 } 415 416 /// isTrueWhenEqual - Return true if the specified condition returns true if 417 /// the two operands to the condition are equal. Note that if one of the two 418 /// operands is a NaN, this value is meaningless. 419 inline bool isTrueWhenEqual(CondCode Cond) { 420 return ((int)Cond & 1) != 0; 421 } 422 423 /// getUnorderedFlavor - This function returns 0 if the condition is always 424 /// false if an operand is a NaN, 1 if the condition is always true if the 425 /// operand is a NaN, and 2 if the condition is undefined if the operand is a 426 /// NaN. 427 inline unsigned getUnorderedFlavor(CondCode Cond) { 428 return ((int)Cond >> 3) & 3; 429 } 430 431 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where 432 /// 'op' is a valid SetCC operation. 433 CondCode getSetCCInverse(CondCode Operation, bool isInteger); 434 435 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 436 /// when given the operation for (X op Y). 437 CondCode getSetCCSwappedOperands(CondCode Operation); 438 439 /// getSetCCOrOperation - Return the result of a logical OR between different 440 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This 441 /// function returns SETCC_INVALID if it is not possible to represent the 442 /// resultant comparison. 443 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger); 444 445 /// getSetCCAndOperation - Return the result of a logical AND between 446 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 447 /// function returns SETCC_INVALID if it is not possible to represent the 448 /// resultant comparison. 449 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger); 450} // end llvm::ISD namespace 451 452 453//===----------------------------------------------------------------------===// 454/// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple 455/// values as the result of a computation. Many nodes return multiple values, 456/// from loads (which define a token and a return value) to ADDC (which returns 457/// a result and a carry value), to calls (which may return an arbitrary number 458/// of values). 459/// 460/// As such, each use of a SelectionDAG computation must indicate the node that 461/// computes it as well as which return value to use from that node. This pair 462/// of information is represented with the SDOperand value type. 463/// 464class SDOperand { 465public: 466 SDNode *Val; // The node defining the value we are using. 467 unsigned ResNo; // Which return value of the node we are using. 468 469 SDOperand() : Val(0) {} 470 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {} 471 472 bool operator==(const SDOperand &O) const { 473 return Val == O.Val && ResNo == O.ResNo; 474 } 475 bool operator!=(const SDOperand &O) const { 476 return !operator==(O); 477 } 478 bool operator<(const SDOperand &O) const { 479 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo); 480 } 481 482 SDOperand getValue(unsigned R) const { 483 return SDOperand(Val, R); 484 } 485 486 /// getValueType - Return the ValueType of the referenced return value. 487 /// 488 inline MVT::ValueType getValueType() const; 489 490 // Forwarding methods - These forward to the corresponding methods in SDNode. 491 inline unsigned getOpcode() const; 492 inline unsigned getNodeDepth() const; 493 inline unsigned getNumOperands() const; 494 inline const SDOperand &getOperand(unsigned i) const; 495 inline bool isTargetOpcode() const; 496 inline unsigned getTargetOpcode() const; 497 498 /// hasOneUse - Return true if there is exactly one operation using this 499 /// result value of the defining operator. 500 inline bool hasOneUse() const; 501}; 502 503 504/// simplify_type specializations - Allow casting operators to work directly on 505/// SDOperands as if they were SDNode*'s. 506template<> struct simplify_type<SDOperand> { 507 typedef SDNode* SimpleType; 508 static SimpleType getSimplifiedValue(const SDOperand &Val) { 509 return static_cast<SimpleType>(Val.Val); 510 } 511}; 512template<> struct simplify_type<const SDOperand> { 513 typedef SDNode* SimpleType; 514 static SimpleType getSimplifiedValue(const SDOperand &Val) { 515 return static_cast<SimpleType>(Val.Val); 516 } 517}; 518 519 520/// SDNode - Represents one node in the SelectionDAG. 521/// 522class SDNode { 523 /// NodeType - The operation that this node performs. 524 /// 525 unsigned short NodeType; 526 527 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This 528 /// means that leaves have a depth of 1, things that use only leaves have a 529 /// depth of 2, etc. 530 unsigned short NodeDepth; 531 532 /// OperandList - The values that are used by this operation. 533 /// 534 SDOperand *OperandList; 535 536 /// ValueList - The types of the values this node defines. SDNode's may 537 /// define multiple values simultaneously. 538 MVT::ValueType *ValueList; 539 540 /// NumOperands/NumValues - The number of entries in the Operand/Value list. 541 unsigned short NumOperands, NumValues; 542 543 /// Prev/Next pointers - These pointers form the linked list of of the 544 /// AllNodes list in the current DAG. 545 SDNode *Prev, *Next; 546 friend struct ilist_traits<SDNode>; 547 548 /// Uses - These are all of the SDNode's that use a value produced by this 549 /// node. 550 std::vector<SDNode*> Uses; 551public: 552 virtual ~SDNode() { 553 assert(NumOperands == 0 && "Operand list not cleared before deletion"); 554 } 555 556 //===--------------------------------------------------------------------===// 557 // Accessors 558 // 559 unsigned getOpcode() const { return NodeType; } 560 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; } 561 unsigned getTargetOpcode() const { 562 assert(isTargetOpcode() && "Not a target opcode!"); 563 return NodeType - ISD::BUILTIN_OP_END; 564 } 565 566 size_t use_size() const { return Uses.size(); } 567 bool use_empty() const { return Uses.empty(); } 568 bool hasOneUse() const { return Uses.size() == 1; } 569 570 /// getNodeDepth - Return the distance from this node to the leaves in the 571 /// graph. The leaves have a depth of 1. 572 unsigned getNodeDepth() const { return NodeDepth; } 573 574 typedef std::vector<SDNode*>::const_iterator use_iterator; 575 use_iterator use_begin() const { return Uses.begin(); } 576 use_iterator use_end() const { return Uses.end(); } 577 578 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 579 /// indicated value. This method ignores uses of other values defined by this 580 /// operation. 581 bool hasNUsesOfValue(unsigned NUses, unsigned Value); 582 583 /// getNumOperands - Return the number of values used by this operation. 584 /// 585 unsigned getNumOperands() const { return NumOperands; } 586 587 const SDOperand &getOperand(unsigned Num) const { 588 assert(Num < NumOperands && "Invalid child # of SDNode!"); 589 return OperandList[Num]; 590 } 591 typedef const SDOperand* op_iterator; 592 op_iterator op_begin() const { return OperandList; } 593 op_iterator op_end() const { return OperandList+NumOperands; } 594 595 596 /// getNumValues - Return the number of values defined/returned by this 597 /// operator. 598 /// 599 unsigned getNumValues() const { return NumValues; } 600 601 /// getValueType - Return the type of a specified result. 602 /// 603 MVT::ValueType getValueType(unsigned ResNo) const { 604 assert(ResNo < NumValues && "Illegal result number!"); 605 return ValueList[ResNo]; 606 } 607 608 typedef const MVT::ValueType* value_iterator; 609 value_iterator value_begin() const { return ValueList; } 610 value_iterator value_end() const { return ValueList+NumValues; } 611 612 /// getOperationName - Return the opcode of this operation for printing. 613 /// 614 const char* getOperationName(const SelectionDAG *G = 0) const; 615 void dump() const; 616 void dump(const SelectionDAG *G) const; 617 618 static bool classof(const SDNode *) { return true; } 619 620 621 /// setAdjCallChain - This method should only be used by the legalizer. 622 void setAdjCallChain(SDOperand N); 623 624protected: 625 friend class SelectionDAG; 626 627 /// getValueTypeList - Return a pointer to the specified value type. 628 /// 629 static MVT::ValueType *getValueTypeList(MVT::ValueType VT); 630 631 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) { 632 OperandList = 0; NumOperands = 0; 633 ValueList = getValueTypeList(VT); 634 NumValues = 1; 635 Prev = 0; Next = 0; 636 } 637 SDNode(unsigned NT, SDOperand Op) 638 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) { 639 OperandList = new SDOperand[1]; 640 OperandList[0] = Op; 641 NumOperands = 1; 642 Op.Val->Uses.push_back(this); 643 ValueList = 0; 644 NumValues = 0; 645 Prev = 0; Next = 0; 646 } 647 SDNode(unsigned NT, SDOperand N1, SDOperand N2) 648 : NodeType(NT) { 649 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth()) 650 NodeDepth = N1.Val->getNodeDepth()+1; 651 else 652 NodeDepth = N2.Val->getNodeDepth()+1; 653 OperandList = new SDOperand[2]; 654 OperandList[0] = N1; 655 OperandList[1] = N2; 656 NumOperands = 2; 657 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 658 ValueList = 0; 659 NumValues = 0; 660 Prev = 0; Next = 0; 661 } 662 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3) 663 : NodeType(NT) { 664 unsigned ND = N1.Val->getNodeDepth(); 665 if (ND < N2.Val->getNodeDepth()) 666 ND = N2.Val->getNodeDepth(); 667 if (ND < N3.Val->getNodeDepth()) 668 ND = N3.Val->getNodeDepth(); 669 NodeDepth = ND+1; 670 671 OperandList = new SDOperand[3]; 672 OperandList[0] = N1; 673 OperandList[1] = N2; 674 OperandList[2] = N3; 675 NumOperands = 3; 676 677 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 678 N3.Val->Uses.push_back(this); 679 ValueList = 0; 680 NumValues = 0; 681 Prev = 0; Next = 0; 682 } 683 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4) 684 : NodeType(NT) { 685 unsigned ND = N1.Val->getNodeDepth(); 686 if (ND < N2.Val->getNodeDepth()) 687 ND = N2.Val->getNodeDepth(); 688 if (ND < N3.Val->getNodeDepth()) 689 ND = N3.Val->getNodeDepth(); 690 if (ND < N4.Val->getNodeDepth()) 691 ND = N4.Val->getNodeDepth(); 692 NodeDepth = ND+1; 693 694 OperandList = new SDOperand[4]; 695 OperandList[0] = N1; 696 OperandList[1] = N2; 697 OperandList[2] = N3; 698 OperandList[3] = N4; 699 NumOperands = 4; 700 701 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this); 702 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this); 703 ValueList = 0; 704 NumValues = 0; 705 Prev = 0; Next = 0; 706 } 707 SDNode(unsigned Opc, const std::vector<SDOperand> &Nodes) : NodeType(Opc) { 708 NumOperands = Nodes.size(); 709 OperandList = new SDOperand[NumOperands]; 710 711 unsigned ND = 0; 712 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 713 OperandList[i] = Nodes[i]; 714 SDNode *N = OperandList[i].Val; 715 N->Uses.push_back(this); 716 if (ND < N->getNodeDepth()) ND = N->getNodeDepth(); 717 } 718 NodeDepth = ND+1; 719 ValueList = 0; 720 NumValues = 0; 721 Prev = 0; Next = 0; 722 } 723 724 /// MorphNodeTo - This clears the return value and operands list, and sets the 725 /// opcode of the node to the specified value. This should only be used by 726 /// the SelectionDAG class. 727 void MorphNodeTo(unsigned Opc) { 728 NodeType = Opc; 729 ValueList = 0; 730 NumValues = 0; 731 732 // Clear the operands list, updating used nodes to remove this from their 733 // use list. 734 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) 735 I->Val->removeUser(this); 736 delete [] OperandList; 737 OperandList = 0; 738 NumOperands = 0; 739 } 740 741 void setValueTypes(MVT::ValueType VT) { 742 assert(NumValues == 0 && "Should not have values yet!"); 743 ValueList = getValueTypeList(VT); 744 NumValues = 1; 745 } 746 void setValueTypes(MVT::ValueType *List, unsigned NumVal) { 747 assert(NumValues == 0 && "Should not have values yet!"); 748 ValueList = List; 749 NumValues = NumVal; 750 } 751 752 void setOperands(SDOperand Op0) { 753 assert(NumOperands == 0 && "Should not have operands yet!"); 754 OperandList = new SDOperand[1]; 755 OperandList[0] = Op0; 756 NumOperands = 1; 757 Op0.Val->Uses.push_back(this); 758 } 759 void setOperands(SDOperand Op0, SDOperand Op1) { 760 assert(NumOperands == 0 && "Should not have operands yet!"); 761 OperandList = new SDOperand[2]; 762 OperandList[0] = Op0; 763 OperandList[1] = Op1; 764 NumOperands = 2; 765 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this); 766 } 767 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2) { 768 assert(NumOperands == 0 && "Should not have operands yet!"); 769 OperandList = new SDOperand[3]; 770 OperandList[0] = Op0; 771 OperandList[1] = Op1; 772 OperandList[2] = Op2; 773 NumOperands = 3; 774 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this); 775 Op2.Val->Uses.push_back(this); 776 } 777 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3) { 778 assert(NumOperands == 0 && "Should not have operands yet!"); 779 OperandList = new SDOperand[4]; 780 OperandList[0] = Op0; 781 OperandList[1] = Op1; 782 OperandList[2] = Op2; 783 OperandList[3] = Op3; 784 NumOperands = 4; 785 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this); 786 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this); 787 } 788 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3, 789 SDOperand Op4) { 790 assert(NumOperands == 0 && "Should not have operands yet!"); 791 OperandList = new SDOperand[5]; 792 OperandList[0] = Op0; 793 OperandList[1] = Op1; 794 OperandList[2] = Op2; 795 OperandList[3] = Op3; 796 OperandList[4] = Op4; 797 NumOperands = 5; 798 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this); 799 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this); 800 Op4.Val->Uses.push_back(this); 801 } 802 void setOperands(SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3, 803 SDOperand Op4, SDOperand Op5) { 804 assert(NumOperands == 0 && "Should not have operands yet!"); 805 OperandList = new SDOperand[6]; 806 OperandList[0] = Op0; 807 OperandList[1] = Op1; 808 OperandList[2] = Op2; 809 OperandList[3] = Op3; 810 OperandList[4] = Op4; 811 OperandList[5] = Op5; 812 NumOperands = 6; 813 Op0.Val->Uses.push_back(this); Op1.Val->Uses.push_back(this); 814 Op2.Val->Uses.push_back(this); Op3.Val->Uses.push_back(this); 815 Op4.Val->Uses.push_back(this); Op5.Val->Uses.push_back(this); 816 } 817 void addUser(SDNode *User) { 818 Uses.push_back(User); 819 } 820 void removeUser(SDNode *User) { 821 // Remove this user from the operand's use list. 822 for (unsigned i = Uses.size(); ; --i) { 823 assert(i != 0 && "Didn't find user!"); 824 if (Uses[i-1] == User) { 825 Uses[i-1] = Uses.back(); 826 Uses.pop_back(); 827 return; 828 } 829 } 830 } 831}; 832 833 834// Define inline functions from the SDOperand class. 835 836inline unsigned SDOperand::getOpcode() const { 837 return Val->getOpcode(); 838} 839inline unsigned SDOperand::getNodeDepth() const { 840 return Val->getNodeDepth(); 841} 842inline MVT::ValueType SDOperand::getValueType() const { 843 return Val->getValueType(ResNo); 844} 845inline unsigned SDOperand::getNumOperands() const { 846 return Val->getNumOperands(); 847} 848inline const SDOperand &SDOperand::getOperand(unsigned i) const { 849 return Val->getOperand(i); 850} 851inline bool SDOperand::isTargetOpcode() const { 852 return Val->isTargetOpcode(); 853} 854inline unsigned SDOperand::getTargetOpcode() const { 855 return Val->getTargetOpcode(); 856} 857inline bool SDOperand::hasOneUse() const { 858 return Val->hasNUsesOfValue(1, ResNo); 859} 860 861/// HandleSDNode - This class is used to form a handle around another node that 862/// is persistant and is updated across invocations of replaceAllUsesWith on its 863/// operand. This node should be directly created by end-users and not added to 864/// the AllNodes list. 865class HandleSDNode : public SDNode { 866public: 867 HandleSDNode(SDOperand X) : SDNode(ISD::HANDLENODE, X) {} 868 ~HandleSDNode() { 869 MorphNodeTo(ISD::HANDLENODE); // Drops operand uses. 870 } 871 872 SDOperand getValue() const { return getOperand(0); } 873}; 874 875class StringSDNode : public SDNode { 876 std::string Value; 877protected: 878 friend class SelectionDAG; 879 StringSDNode(const std::string &val) 880 : SDNode(ISD::STRING, MVT::Other), Value(val) { 881 } 882public: 883 const std::string &getValue() const { return Value; } 884 static bool classof(const StringSDNode *) { return true; } 885 static bool classof(const SDNode *N) { 886 return N->getOpcode() == ISD::STRING; 887 } 888}; 889 890class ConstantSDNode : public SDNode { 891 uint64_t Value; 892protected: 893 friend class SelectionDAG; 894 ConstantSDNode(bool isTarget, uint64_t val, MVT::ValueType VT) 895 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, VT), Value(val) { 896 } 897public: 898 899 uint64_t getValue() const { return Value; } 900 901 int64_t getSignExtended() const { 902 unsigned Bits = MVT::getSizeInBits(getValueType(0)); 903 return ((int64_t)Value << (64-Bits)) >> (64-Bits); 904 } 905 906 bool isNullValue() const { return Value == 0; } 907 bool isAllOnesValue() const { 908 int NumBits = MVT::getSizeInBits(getValueType(0)); 909 if (NumBits == 64) return Value+1 == 0; 910 return Value == (1ULL << NumBits)-1; 911 } 912 913 static bool classof(const ConstantSDNode *) { return true; } 914 static bool classof(const SDNode *N) { 915 return N->getOpcode() == ISD::Constant || 916 N->getOpcode() == ISD::TargetConstant; 917 } 918}; 919 920class ConstantFPSDNode : public SDNode { 921 double Value; 922protected: 923 friend class SelectionDAG; 924 ConstantFPSDNode(double val, MVT::ValueType VT) 925 : SDNode(ISD::ConstantFP, VT), Value(val) { 926 } 927public: 928 929 double getValue() const { return Value; } 930 931 /// isExactlyValue - We don't rely on operator== working on double values, as 932 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 933 /// As such, this method can be used to do an exact bit-for-bit comparison of 934 /// two floating point values. 935 bool isExactlyValue(double V) const; 936 937 static bool classof(const ConstantFPSDNode *) { return true; } 938 static bool classof(const SDNode *N) { 939 return N->getOpcode() == ISD::ConstantFP; 940 } 941}; 942 943class GlobalAddressSDNode : public SDNode { 944 GlobalValue *TheGlobal; 945 int offset; 946protected: 947 friend class SelectionDAG; 948 GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, MVT::ValueType VT, 949 int o=0) 950 : SDNode(isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress, VT) { 951 TheGlobal = const_cast<GlobalValue*>(GA); 952 offset = o; 953 } 954public: 955 956 GlobalValue *getGlobal() const { return TheGlobal; } 957 int getOffset() const { return offset; } 958 959 static bool classof(const GlobalAddressSDNode *) { return true; } 960 static bool classof(const SDNode *N) { 961 return N->getOpcode() == ISD::GlobalAddress || 962 N->getOpcode() == ISD::TargetGlobalAddress; 963 } 964}; 965 966 967class FrameIndexSDNode : public SDNode { 968 int FI; 969protected: 970 friend class SelectionDAG; 971 FrameIndexSDNode(int fi, MVT::ValueType VT, bool isTarg) 972 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, VT), FI(fi) {} 973public: 974 975 int getIndex() const { return FI; } 976 977 static bool classof(const FrameIndexSDNode *) { return true; } 978 static bool classof(const SDNode *N) { 979 return N->getOpcode() == ISD::FrameIndex || 980 N->getOpcode() == ISD::TargetFrameIndex; 981 } 982}; 983 984class ConstantPoolSDNode : public SDNode { 985 Constant *C; 986protected: 987 friend class SelectionDAG; 988 ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget) 989 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT), 990 C(c) {} 991public: 992 993 Constant *get() const { return C; } 994 995 static bool classof(const ConstantPoolSDNode *) { return true; } 996 static bool classof(const SDNode *N) { 997 return N->getOpcode() == ISD::ConstantPool || 998 N->getOpcode() == ISD::TargetConstantPool; 999 } 1000}; 1001 1002class BasicBlockSDNode : public SDNode { 1003 MachineBasicBlock *MBB; 1004protected: 1005 friend class SelectionDAG; 1006 BasicBlockSDNode(MachineBasicBlock *mbb) 1007 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {} 1008public: 1009 1010 MachineBasicBlock *getBasicBlock() const { return MBB; } 1011 1012 static bool classof(const BasicBlockSDNode *) { return true; } 1013 static bool classof(const SDNode *N) { 1014 return N->getOpcode() == ISD::BasicBlock; 1015 } 1016}; 1017 1018class SrcValueSDNode : public SDNode { 1019 const Value *V; 1020 int offset; 1021protected: 1022 friend class SelectionDAG; 1023 SrcValueSDNode(const Value* v, int o) 1024 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {} 1025 1026public: 1027 const Value *getValue() const { return V; } 1028 int getOffset() const { return offset; } 1029 1030 static bool classof(const SrcValueSDNode *) { return true; } 1031 static bool classof(const SDNode *N) { 1032 return N->getOpcode() == ISD::SRCVALUE; 1033 } 1034}; 1035 1036 1037class RegisterSDNode : public SDNode { 1038 unsigned Reg; 1039protected: 1040 friend class SelectionDAG; 1041 RegisterSDNode(unsigned reg, MVT::ValueType VT) 1042 : SDNode(ISD::Register, VT), Reg(reg) {} 1043public: 1044 1045 unsigned getReg() const { return Reg; } 1046 1047 static bool classof(const RegisterSDNode *) { return true; } 1048 static bool classof(const SDNode *N) { 1049 return N->getOpcode() == ISD::Register; 1050 } 1051}; 1052 1053class ExternalSymbolSDNode : public SDNode { 1054 const char *Symbol; 1055protected: 1056 friend class SelectionDAG; 1057 ExternalSymbolSDNode(bool isTarget, const char *Sym, MVT::ValueType VT) 1058 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, VT), 1059 Symbol(Sym) { 1060 } 1061public: 1062 1063 const char *getSymbol() const { return Symbol; } 1064 1065 static bool classof(const ExternalSymbolSDNode *) { return true; } 1066 static bool classof(const SDNode *N) { 1067 return N->getOpcode() == ISD::ExternalSymbol || 1068 N->getOpcode() == ISD::TargetExternalSymbol; 1069 } 1070}; 1071 1072class CondCodeSDNode : public SDNode { 1073 ISD::CondCode Condition; 1074protected: 1075 friend class SelectionDAG; 1076 CondCodeSDNode(ISD::CondCode Cond) 1077 : SDNode(ISD::CONDCODE, MVT::Other), Condition(Cond) { 1078 } 1079public: 1080 1081 ISD::CondCode get() const { return Condition; } 1082 1083 static bool classof(const CondCodeSDNode *) { return true; } 1084 static bool classof(const SDNode *N) { 1085 return N->getOpcode() == ISD::CONDCODE; 1086 } 1087}; 1088 1089/// VTSDNode - This class is used to represent MVT::ValueType's, which are used 1090/// to parameterize some operations. 1091class VTSDNode : public SDNode { 1092 MVT::ValueType ValueType; 1093protected: 1094 friend class SelectionDAG; 1095 VTSDNode(MVT::ValueType VT) 1096 : SDNode(ISD::VALUETYPE, MVT::Other), ValueType(VT) {} 1097public: 1098 1099 MVT::ValueType getVT() const { return ValueType; } 1100 1101 static bool classof(const VTSDNode *) { return true; } 1102 static bool classof(const SDNode *N) { 1103 return N->getOpcode() == ISD::VALUETYPE; 1104 } 1105}; 1106 1107 1108class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> { 1109 SDNode *Node; 1110 unsigned Operand; 1111 1112 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {} 1113public: 1114 bool operator==(const SDNodeIterator& x) const { 1115 return Operand == x.Operand; 1116 } 1117 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); } 1118 1119 const SDNodeIterator &operator=(const SDNodeIterator &I) { 1120 assert(I.Node == Node && "Cannot assign iterators to two different nodes!"); 1121 Operand = I.Operand; 1122 return *this; 1123 } 1124 1125 pointer operator*() const { 1126 return Node->getOperand(Operand).Val; 1127 } 1128 pointer operator->() const { return operator*(); } 1129 1130 SDNodeIterator& operator++() { // Preincrement 1131 ++Operand; 1132 return *this; 1133 } 1134 SDNodeIterator operator++(int) { // Postincrement 1135 SDNodeIterator tmp = *this; ++*this; return tmp; 1136 } 1137 1138 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); } 1139 static SDNodeIterator end (SDNode *N) { 1140 return SDNodeIterator(N, N->getNumOperands()); 1141 } 1142 1143 unsigned getOperand() const { return Operand; } 1144 const SDNode *getNode() const { return Node; } 1145}; 1146 1147template <> struct GraphTraits<SDNode*> { 1148 typedef SDNode NodeType; 1149 typedef SDNodeIterator ChildIteratorType; 1150 static inline NodeType *getEntryNode(SDNode *N) { return N; } 1151 static inline ChildIteratorType child_begin(NodeType *N) { 1152 return SDNodeIterator::begin(N); 1153 } 1154 static inline ChildIteratorType child_end(NodeType *N) { 1155 return SDNodeIterator::end(N); 1156 } 1157}; 1158 1159template<> 1160struct ilist_traits<SDNode> { 1161 static SDNode *getPrev(const SDNode *N) { return N->Prev; } 1162 static SDNode *getNext(const SDNode *N) { return N->Next; } 1163 1164 static void setPrev(SDNode *N, SDNode *Prev) { N->Prev = Prev; } 1165 static void setNext(SDNode *N, SDNode *Next) { N->Next = Next; } 1166 1167 static SDNode *createSentinel() { 1168 return new SDNode(ISD::EntryToken, MVT::Other); 1169 } 1170 static void destroySentinel(SDNode *N) { delete N; } 1171 //static SDNode *createNode(const SDNode &V) { return new SDNode(V); } 1172 1173 1174 void addNodeToList(SDNode *NTy) {} 1175 void removeNodeFromList(SDNode *NTy) {} 1176 void transferNodesFromList(iplist<SDNode, ilist_traits> &L2, 1177 const ilist_iterator<SDNode> &X, 1178 const ilist_iterator<SDNode> &Y) {} 1179}; 1180 1181} // end llvm namespace 1182 1183#endif 1184