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