SelectionDAG.cpp revision 3181a771ff5b2090b7ed55f9b18a684ea8fe625a
1//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// 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 implements the SelectionDAG class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/SelectionDAG.h" 15#include "llvm/Constants.h" 16#include "llvm/GlobalValue.h" 17#include "llvm/Assembly/Writer.h" 18#include "llvm/CodeGen/MachineBasicBlock.h" 19#include "llvm/Support/MathExtras.h" 20#include "llvm/Target/MRegisterInfo.h" 21#include "llvm/Target/TargetLowering.h" 22#include "llvm/Target/TargetInstrInfo.h" 23#include "llvm/Target/TargetMachine.h" 24#include "llvm/ADT/StringExtras.h" 25#include <iostream> 26#include <set> 27#include <cmath> 28#include <algorithm> 29using namespace llvm; 30 31static bool isCommutativeBinOp(unsigned Opcode) { 32 switch (Opcode) { 33 case ISD::ADD: 34 case ISD::MUL: 35 case ISD::MULHU: 36 case ISD::MULHS: 37 case ISD::FADD: 38 case ISD::FMUL: 39 case ISD::AND: 40 case ISD::OR: 41 case ISD::XOR: return true; 42 default: return false; // FIXME: Need commutative info for user ops! 43 } 44} 45 46static bool isAssociativeBinOp(unsigned Opcode) { 47 switch (Opcode) { 48 case ISD::ADD: 49 case ISD::MUL: 50 case ISD::AND: 51 case ISD::OR: 52 case ISD::XOR: return true; 53 default: return false; // FIXME: Need associative info for user ops! 54 } 55} 56 57// isInvertibleForFree - Return true if there is no cost to emitting the logical 58// inverse of this node. 59static bool isInvertibleForFree(SDOperand N) { 60 if (isa<ConstantSDNode>(N.Val)) return true; 61 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse()) 62 return true; 63 return false; 64} 65 66//===----------------------------------------------------------------------===// 67// ConstantFPSDNode Class 68//===----------------------------------------------------------------------===// 69 70/// isExactlyValue - We don't rely on operator== working on double values, as 71/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 72/// As such, this method can be used to do an exact bit-for-bit comparison of 73/// two floating point values. 74bool ConstantFPSDNode::isExactlyValue(double V) const { 75 return DoubleToBits(V) == DoubleToBits(Value); 76} 77 78//===----------------------------------------------------------------------===// 79// ISD Class 80//===----------------------------------------------------------------------===// 81 82/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 83/// when given the operation for (X op Y). 84ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 85 // To perform this operation, we just need to swap the L and G bits of the 86 // operation. 87 unsigned OldL = (Operation >> 2) & 1; 88 unsigned OldG = (Operation >> 1) & 1; 89 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 90 (OldL << 1) | // New G bit 91 (OldG << 2)); // New L bit. 92} 93 94/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 95/// 'op' is a valid SetCC operation. 96ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 97 unsigned Operation = Op; 98 if (isInteger) 99 Operation ^= 7; // Flip L, G, E bits, but not U. 100 else 101 Operation ^= 15; // Flip all of the condition bits. 102 if (Operation > ISD::SETTRUE2) 103 Operation &= ~8; // Don't let N and U bits get set. 104 return ISD::CondCode(Operation); 105} 106 107 108/// isSignedOp - For an integer comparison, return 1 if the comparison is a 109/// signed operation and 2 if the result is an unsigned comparison. Return zero 110/// if the operation does not depend on the sign of the input (setne and seteq). 111static int isSignedOp(ISD::CondCode Opcode) { 112 switch (Opcode) { 113 default: assert(0 && "Illegal integer setcc operation!"); 114 case ISD::SETEQ: 115 case ISD::SETNE: return 0; 116 case ISD::SETLT: 117 case ISD::SETLE: 118 case ISD::SETGT: 119 case ISD::SETGE: return 1; 120 case ISD::SETULT: 121 case ISD::SETULE: 122 case ISD::SETUGT: 123 case ISD::SETUGE: return 2; 124 } 125} 126 127/// getSetCCOrOperation - Return the result of a logical OR between different 128/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 129/// returns SETCC_INVALID if it is not possible to represent the resultant 130/// comparison. 131ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 132 bool isInteger) { 133 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 134 // Cannot fold a signed integer setcc with an unsigned integer setcc. 135 return ISD::SETCC_INVALID; 136 137 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 138 139 // If the N and U bits get set then the resultant comparison DOES suddenly 140 // care about orderedness, and is true when ordered. 141 if (Op > ISD::SETTRUE2) 142 Op &= ~16; // Clear the N bit. 143 return ISD::CondCode(Op); 144} 145 146/// getSetCCAndOperation - Return the result of a logical AND between different 147/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 148/// function returns zero if it is not possible to represent the resultant 149/// comparison. 150ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 151 bool isInteger) { 152 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 153 // Cannot fold a signed setcc with an unsigned setcc. 154 return ISD::SETCC_INVALID; 155 156 // Combine all of the condition bits. 157 return ISD::CondCode(Op1 & Op2); 158} 159 160const TargetMachine &SelectionDAG::getTarget() const { 161 return TLI.getTargetMachine(); 162} 163 164//===----------------------------------------------------------------------===// 165// SelectionDAG Class 166//===----------------------------------------------------------------------===// 167 168/// RemoveDeadNodes - This method deletes all unreachable nodes in the 169/// SelectionDAG, including nodes (like loads) that have uses of their token 170/// chain but no other uses and no side effect. If a node is passed in as an 171/// argument, it is used as the seed for node deletion. 172void SelectionDAG::RemoveDeadNodes(SDNode *N) { 173 // Create a dummy node (which is not added to allnodes), that adds a reference 174 // to the root node, preventing it from being deleted. 175 HandleSDNode Dummy(getRoot()); 176 177 bool MadeChange = false; 178 179 // If we have a hint to start from, use it. 180 if (N && N->use_empty()) { 181 DestroyDeadNode(N); 182 MadeChange = true; 183 } 184 185 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 186 if (I->use_empty() && I->getOpcode() != 65535) { 187 // Node is dead, recursively delete newly dead uses. 188 DestroyDeadNode(I); 189 MadeChange = true; 190 } 191 192 // Walk the nodes list, removing the nodes we've marked as dead. 193 if (MadeChange) { 194 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) { 195 SDNode *N = I++; 196 if (N->use_empty()) 197 AllNodes.erase(N); 198 } 199 } 200 201 // If the root changed (e.g. it was a dead load, update the root). 202 setRoot(Dummy.getValue()); 203} 204 205/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the 206/// graph. If it is the last user of any of its operands, recursively process 207/// them the same way. 208/// 209void SelectionDAG::DestroyDeadNode(SDNode *N) { 210 // Okay, we really are going to delete this node. First take this out of the 211 // appropriate CSE map. 212 RemoveNodeFromCSEMaps(N); 213 214 // Next, brutally remove the operand list. This is safe to do, as there are 215 // no cycles in the graph. 216 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 217 SDNode *O = I->Val; 218 O->removeUser(N); 219 220 // Now that we removed this operand, see if there are no uses of it left. 221 if (O->use_empty()) 222 DestroyDeadNode(O); 223 } 224 delete[] N->OperandList; 225 N->OperandList = 0; 226 N->NumOperands = 0; 227 228 // Mark the node as dead. 229 N->MorphNodeTo(65535); 230} 231 232void SelectionDAG::DeleteNode(SDNode *N) { 233 assert(N->use_empty() && "Cannot delete a node that is not dead!"); 234 235 // First take this out of the appropriate CSE map. 236 RemoveNodeFromCSEMaps(N); 237 238 // Finally, remove uses due to operands of this node, remove from the 239 // AllNodes list, and delete the node. 240 DeleteNodeNotInCSEMaps(N); 241} 242 243void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 244 245 // Remove it from the AllNodes list. 246 AllNodes.remove(N); 247 248 // Drop all of the operands and decrement used nodes use counts. 249 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 250 I->Val->removeUser(N); 251 delete[] N->OperandList; 252 N->OperandList = 0; 253 N->NumOperands = 0; 254 255 delete N; 256} 257 258/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 259/// correspond to it. This is useful when we're about to delete or repurpose 260/// the node. We don't want future request for structurally identical nodes 261/// to return N anymore. 262void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 263 bool Erased = false; 264 switch (N->getOpcode()) { 265 case ISD::HANDLENODE: return; // noop. 266 case ISD::Constant: 267 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(), 268 N->getValueType(0))); 269 break; 270 case ISD::TargetConstant: 271 Erased = TargetConstants.erase(std::make_pair( 272 cast<ConstantSDNode>(N)->getValue(), 273 N->getValueType(0))); 274 break; 275 case ISD::ConstantFP: { 276 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()); 277 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0))); 278 break; 279 } 280 case ISD::TargetConstantFP: { 281 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()); 282 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0))); 283 break; 284 } 285 case ISD::STRING: 286 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); 287 break; 288 case ISD::CONDCODE: 289 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 290 "Cond code doesn't exist!"); 291 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 292 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 293 break; 294 case ISD::GlobalAddress: { 295 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N); 296 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(), 297 GN->getOffset())); 298 break; 299 } 300 case ISD::TargetGlobalAddress: { 301 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N); 302 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(), 303 GN->getOffset())); 304 break; 305 } 306 case ISD::FrameIndex: 307 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); 308 break; 309 case ISD::TargetFrameIndex: 310 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); 311 break; 312 case ISD::ConstantPool: 313 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get()); 314 break; 315 case ISD::TargetConstantPool: 316 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get()); 317 break; 318 case ISD::BasicBlock: 319 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock()); 320 break; 321 case ISD::ExternalSymbol: 322 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 323 break; 324 case ISD::TargetExternalSymbol: 325 Erased = 326 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 327 break; 328 case ISD::VALUETYPE: 329 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0; 330 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; 331 break; 332 case ISD::Register: 333 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(), 334 N->getValueType(0))); 335 break; 336 case ISD::SRCVALUE: { 337 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N); 338 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset())); 339 break; 340 } 341 case ISD::LOAD: 342 Erased = Loads.erase(std::make_pair(N->getOperand(1), 343 std::make_pair(N->getOperand(0), 344 N->getValueType(0)))); 345 break; 346 default: 347 if (N->getNumValues() == 1) { 348 if (N->getNumOperands() == 0) { 349 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(), 350 N->getValueType(0))); 351 } else if (N->getNumOperands() == 1) { 352 Erased = 353 UnaryOps.erase(std::make_pair(N->getOpcode(), 354 std::make_pair(N->getOperand(0), 355 N->getValueType(0)))); 356 } else if (N->getNumOperands() == 2) { 357 Erased = 358 BinaryOps.erase(std::make_pair(N->getOpcode(), 359 std::make_pair(N->getOperand(0), 360 N->getOperand(1)))); 361 } else { 362 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 363 Erased = 364 OneResultNodes.erase(std::make_pair(N->getOpcode(), 365 std::make_pair(N->getValueType(0), 366 Ops))); 367 } 368 } else { 369 // Remove the node from the ArbitraryNodes map. 370 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 371 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 372 Erased = 373 ArbitraryNodes.erase(std::make_pair(N->getOpcode(), 374 std::make_pair(RV, Ops))); 375 } 376 break; 377 } 378#ifndef NDEBUG 379 // Verify that the node was actually in one of the CSE maps, unless it has a 380 // flag result (which cannot be CSE'd) or is one of the special cases that are 381 // not subject to CSE. 382 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && 383 N->getOpcode() != ISD::CALLSEQ_START && 384 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) { 385 386 N->dump(); 387 assert(0 && "Node is not in map!"); 388 } 389#endif 390} 391 392/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It 393/// has been taken out and modified in some way. If the specified node already 394/// exists in the CSE maps, do not modify the maps, but return the existing node 395/// instead. If it doesn't exist, add it and return null. 396/// 397SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { 398 assert(N->getNumOperands() && "This is a leaf node!"); 399 if (N->getOpcode() == ISD::CALLSEQ_START || 400 N->getOpcode() == ISD::CALLSEQ_END || 401 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 402 return 0; // Never add these nodes. 403 404 // Check that remaining values produced are not flags. 405 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 406 if (N->getValueType(i) == MVT::Flag) 407 return 0; // Never CSE anything that produces a flag. 408 409 if (N->getNumValues() == 1) { 410 if (N->getNumOperands() == 1) { 411 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(), 412 std::make_pair(N->getOperand(0), 413 N->getValueType(0)))]; 414 if (U) return U; 415 U = N; 416 } else if (N->getNumOperands() == 2) { 417 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(), 418 std::make_pair(N->getOperand(0), 419 N->getOperand(1)))]; 420 if (B) return B; 421 B = N; 422 } else { 423 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 424 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(), 425 std::make_pair(N->getValueType(0), Ops))]; 426 if (ORN) return ORN; 427 ORN = N; 428 } 429 } else { 430 if (N->getOpcode() == ISD::LOAD) { 431 SDNode *&L = Loads[std::make_pair(N->getOperand(1), 432 std::make_pair(N->getOperand(0), 433 N->getValueType(0)))]; 434 if (L) return L; 435 L = N; 436 } else { 437 // Remove the node from the ArbitraryNodes map. 438 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 439 std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); 440 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(), 441 std::make_pair(RV, Ops))]; 442 if (AN) return AN; 443 AN = N; 444 } 445 } 446 return 0; 447} 448 449/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 450/// were replaced with those specified. If this node is never memoized, 451/// return null, otherwise return a pointer to the slot it would take. If a 452/// node already exists with these operands, the slot will be non-null. 453SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) { 454 if (N->getOpcode() == ISD::CALLSEQ_START || 455 N->getOpcode() == ISD::CALLSEQ_END || 456 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 457 return 0; // Never add these nodes. 458 459 // Check that remaining values produced are not flags. 460 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 461 if (N->getValueType(i) == MVT::Flag) 462 return 0; // Never CSE anything that produces a flag. 463 464 if (N->getNumValues() == 1) { 465 return &UnaryOps[std::make_pair(N->getOpcode(), 466 std::make_pair(Op, N->getValueType(0)))]; 467 } else { 468 // Remove the node from the ArbitraryNodes map. 469 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 470 std::vector<SDOperand> Ops; 471 Ops.push_back(Op); 472 return &ArbitraryNodes[std::make_pair(N->getOpcode(), 473 std::make_pair(RV, Ops))]; 474 } 475 return 0; 476} 477 478/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 479/// were replaced with those specified. If this node is never memoized, 480/// return null, otherwise return a pointer to the slot it would take. If a 481/// node already exists with these operands, the slot will be non-null. 482SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, 483 SDOperand Op1, SDOperand Op2) { 484 if (N->getOpcode() == ISD::CALLSEQ_START || 485 N->getOpcode() == ISD::CALLSEQ_END || 486 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 487 return 0; // Never add these nodes. 488 489 // Check that remaining values produced are not flags. 490 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 491 if (N->getValueType(i) == MVT::Flag) 492 return 0; // Never CSE anything that produces a flag. 493 494 if (N->getNumValues() == 1) { 495 return &BinaryOps[std::make_pair(N->getOpcode(), 496 std::make_pair(Op1, Op2))]; 497 } else { 498 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 499 std::vector<SDOperand> Ops; 500 Ops.push_back(Op1); 501 Ops.push_back(Op2); 502 return &ArbitraryNodes[std::make_pair(N->getOpcode(), 503 std::make_pair(RV, Ops))]; 504 } 505 return 0; 506} 507 508 509/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 510/// were replaced with those specified. If this node is never memoized, 511/// return null, otherwise return a pointer to the slot it would take. If a 512/// node already exists with these operands, the slot will be non-null. 513SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, 514 const std::vector<SDOperand> &Ops) { 515 if (N->getOpcode() == ISD::CALLSEQ_START || 516 N->getOpcode() == ISD::CALLSEQ_END || 517 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 518 return 0; // Never add these nodes. 519 520 // Check that remaining values produced are not flags. 521 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 522 if (N->getValueType(i) == MVT::Flag) 523 return 0; // Never CSE anything that produces a flag. 524 525 if (N->getNumValues() == 1) { 526 if (N->getNumOperands() == 1) { 527 return &UnaryOps[std::make_pair(N->getOpcode(), 528 std::make_pair(Ops[0], 529 N->getValueType(0)))]; 530 } else if (N->getNumOperands() == 2) { 531 return &BinaryOps[std::make_pair(N->getOpcode(), 532 std::make_pair(Ops[0], Ops[1]))]; 533 } else { 534 return &OneResultNodes[std::make_pair(N->getOpcode(), 535 std::make_pair(N->getValueType(0), 536 Ops))]; 537 } 538 } else { 539 if (N->getOpcode() == ISD::LOAD) { 540 return &Loads[std::make_pair(Ops[1], 541 std::make_pair(Ops[0], N->getValueType(0)))]; 542 } else { 543 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); 544 return &ArbitraryNodes[std::make_pair(N->getOpcode(), 545 std::make_pair(RV, Ops))]; 546 } 547 } 548 return 0; 549} 550 551 552SelectionDAG::~SelectionDAG() { 553 while (!AllNodes.empty()) { 554 SDNode *N = AllNodes.begin(); 555 delete [] N->OperandList; 556 N->OperandList = 0; 557 N->NumOperands = 0; 558 AllNodes.pop_front(); 559 } 560} 561 562SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { 563 if (Op.getValueType() == VT) return Op; 564 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); 565 return getNode(ISD::AND, Op.getValueType(), Op, 566 getConstant(Imm, Op.getValueType())); 567} 568 569SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) { 570 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 571 // Mask out any bits that are not valid for this constant. 572 if (VT != MVT::i64) 573 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; 574 575 SDNode *&N = Constants[std::make_pair(Val, VT)]; 576 if (N) return SDOperand(N, 0); 577 N = new ConstantSDNode(false, Val, VT); 578 AllNodes.push_back(N); 579 return SDOperand(N, 0); 580} 581 582SDOperand SelectionDAG::getString(const std::string &Val) { 583 StringSDNode *&N = StringNodes[Val]; 584 if (!N) { 585 N = new StringSDNode(Val); 586 AllNodes.push_back(N); 587 } 588 return SDOperand(N, 0); 589} 590 591SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) { 592 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 593 // Mask out any bits that are not valid for this constant. 594 if (VT != MVT::i64) 595 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; 596 597 SDNode *&N = TargetConstants[std::make_pair(Val, VT)]; 598 if (N) return SDOperand(N, 0); 599 N = new ConstantSDNode(true, Val, VT); 600 AllNodes.push_back(N); 601 return SDOperand(N, 0); 602} 603 604SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) { 605 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 606 if (VT == MVT::f32) 607 Val = (float)Val; // Mask out extra precision. 608 609 // Do the map lookup using the actual bit pattern for the floating point 610 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 611 // we don't have issues with SNANs. 612 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)]; 613 if (N) return SDOperand(N, 0); 614 N = new ConstantFPSDNode(false, Val, VT); 615 AllNodes.push_back(N); 616 return SDOperand(N, 0); 617} 618 619SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) { 620 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 621 if (VT == MVT::f32) 622 Val = (float)Val; // Mask out extra precision. 623 624 // Do the map lookup using the actual bit pattern for the floating point 625 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 626 // we don't have issues with SNANs. 627 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)]; 628 if (N) return SDOperand(N, 0); 629 N = new ConstantFPSDNode(true, Val, VT); 630 AllNodes.push_back(N); 631 return SDOperand(N, 0); 632} 633 634SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, 635 MVT::ValueType VT, int offset) { 636 SDNode *&N = GlobalValues[std::make_pair(GV, offset)]; 637 if (N) return SDOperand(N, 0); 638 N = new GlobalAddressSDNode(false, GV, VT, offset); 639 AllNodes.push_back(N); 640 return SDOperand(N, 0); 641} 642 643SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV, 644 MVT::ValueType VT, int offset) { 645 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)]; 646 if (N) return SDOperand(N, 0); 647 N = new GlobalAddressSDNode(true, GV, VT, offset); 648 AllNodes.push_back(N); 649 return SDOperand(N, 0); 650} 651 652SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) { 653 SDNode *&N = FrameIndices[FI]; 654 if (N) return SDOperand(N, 0); 655 N = new FrameIndexSDNode(FI, VT, false); 656 AllNodes.push_back(N); 657 return SDOperand(N, 0); 658} 659 660SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) { 661 SDNode *&N = TargetFrameIndices[FI]; 662 if (N) return SDOperand(N, 0); 663 N = new FrameIndexSDNode(FI, VT, true); 664 AllNodes.push_back(N); 665 return SDOperand(N, 0); 666} 667 668SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) { 669 SDNode *&N = ConstantPoolIndices[C]; 670 if (N) return SDOperand(N, 0); 671 N = new ConstantPoolSDNode(C, VT, false); 672 AllNodes.push_back(N); 673 return SDOperand(N, 0); 674} 675 676SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) { 677 SDNode *&N = TargetConstantPoolIndices[C]; 678 if (N) return SDOperand(N, 0); 679 N = new ConstantPoolSDNode(C, VT, true); 680 AllNodes.push_back(N); 681 return SDOperand(N, 0); 682} 683 684SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 685 SDNode *&N = BBNodes[MBB]; 686 if (N) return SDOperand(N, 0); 687 N = new BasicBlockSDNode(MBB); 688 AllNodes.push_back(N); 689 return SDOperand(N, 0); 690} 691 692SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { 693 if ((unsigned)VT >= ValueTypeNodes.size()) 694 ValueTypeNodes.resize(VT+1); 695 if (ValueTypeNodes[VT] == 0) { 696 ValueTypeNodes[VT] = new VTSDNode(VT); 697 AllNodes.push_back(ValueTypeNodes[VT]); 698 } 699 700 return SDOperand(ValueTypeNodes[VT], 0); 701} 702 703SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { 704 SDNode *&N = ExternalSymbols[Sym]; 705 if (N) return SDOperand(N, 0); 706 N = new ExternalSymbolSDNode(false, Sym, VT); 707 AllNodes.push_back(N); 708 return SDOperand(N, 0); 709} 710 711SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, 712 MVT::ValueType VT) { 713 SDNode *&N = TargetExternalSymbols[Sym]; 714 if (N) return SDOperand(N, 0); 715 N = new ExternalSymbolSDNode(true, Sym, VT); 716 AllNodes.push_back(N); 717 return SDOperand(N, 0); 718} 719 720SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { 721 if ((unsigned)Cond >= CondCodeNodes.size()) 722 CondCodeNodes.resize(Cond+1); 723 724 if (CondCodeNodes[Cond] == 0) { 725 CondCodeNodes[Cond] = new CondCodeSDNode(Cond); 726 AllNodes.push_back(CondCodeNodes[Cond]); 727 } 728 return SDOperand(CondCodeNodes[Cond], 0); 729} 730 731SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { 732 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)]; 733 if (!Reg) { 734 Reg = new RegisterSDNode(RegNo, VT); 735 AllNodes.push_back(Reg); 736 } 737 return SDOperand(Reg, 0); 738} 739 740SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1, 741 SDOperand N2, ISD::CondCode Cond) { 742 // These setcc operations always fold. 743 switch (Cond) { 744 default: break; 745 case ISD::SETFALSE: 746 case ISD::SETFALSE2: return getConstant(0, VT); 747 case ISD::SETTRUE: 748 case ISD::SETTRUE2: return getConstant(1, VT); 749 } 750 751 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { 752 uint64_t C2 = N2C->getValue(); 753 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { 754 uint64_t C1 = N1C->getValue(); 755 756 // Sign extend the operands if required 757 if (ISD::isSignedIntSetCC(Cond)) { 758 C1 = N1C->getSignExtended(); 759 C2 = N2C->getSignExtended(); 760 } 761 762 switch (Cond) { 763 default: assert(0 && "Unknown integer setcc!"); 764 case ISD::SETEQ: return getConstant(C1 == C2, VT); 765 case ISD::SETNE: return getConstant(C1 != C2, VT); 766 case ISD::SETULT: return getConstant(C1 < C2, VT); 767 case ISD::SETUGT: return getConstant(C1 > C2, VT); 768 case ISD::SETULE: return getConstant(C1 <= C2, VT); 769 case ISD::SETUGE: return getConstant(C1 >= C2, VT); 770 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); 771 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); 772 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); 773 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); 774 } 775 } else { 776 // If the LHS is a ZERO_EXTEND, perform the comparison on the input. 777 if (N1.getOpcode() == ISD::ZERO_EXTEND) { 778 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType()); 779 780 // If the comparison constant has bits in the upper part, the 781 // zero-extended value could never match. 782 if (C2 & (~0ULL << InSize)) { 783 unsigned VSize = MVT::getSizeInBits(N1.getValueType()); 784 switch (Cond) { 785 case ISD::SETUGT: 786 case ISD::SETUGE: 787 case ISD::SETEQ: return getConstant(0, VT); 788 case ISD::SETULT: 789 case ISD::SETULE: 790 case ISD::SETNE: return getConstant(1, VT); 791 case ISD::SETGT: 792 case ISD::SETGE: 793 // True if the sign bit of C2 is set. 794 return getConstant((C2 & (1ULL << VSize)) != 0, VT); 795 case ISD::SETLT: 796 case ISD::SETLE: 797 // True if the sign bit of C2 isn't set. 798 return getConstant((C2 & (1ULL << VSize)) == 0, VT); 799 default: 800 break; 801 } 802 } 803 804 // Otherwise, we can perform the comparison with the low bits. 805 switch (Cond) { 806 case ISD::SETEQ: 807 case ISD::SETNE: 808 case ISD::SETUGT: 809 case ISD::SETUGE: 810 case ISD::SETULT: 811 case ISD::SETULE: 812 return getSetCC(VT, N1.getOperand(0), 813 getConstant(C2, N1.getOperand(0).getValueType()), 814 Cond); 815 default: 816 break; // todo, be more careful with signed comparisons 817 } 818 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG && 819 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 820 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT(); 821 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); 822 MVT::ValueType ExtDstTy = N1.getValueType(); 823 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); 824 825 // If the extended part has any inconsistent bits, it cannot ever 826 // compare equal. In other words, they have to be all ones or all 827 // zeros. 828 uint64_t ExtBits = 829 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); 830 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits) 831 return getConstant(Cond == ISD::SETNE, VT); 832 833 // Otherwise, make this a use of a zext. 834 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy), 835 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy), 836 Cond); 837 } 838 839 uint64_t MinVal, MaxVal; 840 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0)); 841 if (ISD::isSignedIntSetCC(Cond)) { 842 MinVal = 1ULL << (OperandBitSize-1); 843 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. 844 MaxVal = ~0ULL >> (65-OperandBitSize); 845 else 846 MaxVal = 0; 847 } else { 848 MinVal = 0; 849 MaxVal = ~0ULL >> (64-OperandBitSize); 850 } 851 852 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 853 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 854 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true 855 --C2; // X >= C1 --> X > (C1-1) 856 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 857 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); 858 } 859 860 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 861 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true 862 ++C2; // X <= C1 --> X < (C1+1) 863 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), 864 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); 865 } 866 867 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal) 868 return getConstant(0, VT); // X < MIN --> false 869 870 // Canonicalize setgt X, Min --> setne X, Min 871 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal) 872 return getSetCC(VT, N1, N2, ISD::SETNE); 873 874 // If we have setult X, 1, turn it into seteq X, 0 875 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1) 876 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()), 877 ISD::SETEQ); 878 // If we have setugt X, Max-1, turn it into seteq X, Max 879 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1) 880 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()), 881 ISD::SETEQ); 882 883 // If we have "setcc X, C1", check to see if we can shrink the immediate 884 // by changing cc. 885 886 // SETUGT X, SINTMAX -> SETLT X, 0 887 if (Cond == ISD::SETUGT && OperandBitSize != 1 && 888 C2 == (~0ULL >> (65-OperandBitSize))) 889 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT); 890 891 // FIXME: Implement the rest of these. 892 893 894 // Fold bit comparisons when we can. 895 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 896 VT == N1.getValueType() && N1.getOpcode() == ISD::AND) 897 if (ConstantSDNode *AndRHS = 898 dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 899 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 900 // Perform the xform if the AND RHS is a single bit. 901 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { 902 return getNode(ISD::SRL, VT, N1, 903 getConstant(Log2_64(AndRHS->getValue()), 904 TLI.getShiftAmountTy())); 905 } 906 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) { 907 // (X & 8) == 8 --> (X & 8) >> 3 908 // Perform the xform if C2 is a single bit. 909 if ((C2 & (C2-1)) == 0) { 910 return getNode(ISD::SRL, VT, N1, 911 getConstant(Log2_64(C2),TLI.getShiftAmountTy())); 912 } 913 } 914 } 915 } 916 } else if (isa<ConstantSDNode>(N1.Val)) { 917 // Ensure that the constant occurs on the RHS. 918 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 919 } 920 921 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) 922 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { 923 double C1 = N1C->getValue(), C2 = N2C->getValue(); 924 925 switch (Cond) { 926 default: break; // FIXME: Implement the rest of these! 927 case ISD::SETEQ: return getConstant(C1 == C2, VT); 928 case ISD::SETNE: return getConstant(C1 != C2, VT); 929 case ISD::SETLT: return getConstant(C1 < C2, VT); 930 case ISD::SETGT: return getConstant(C1 > C2, VT); 931 case ISD::SETLE: return getConstant(C1 <= C2, VT); 932 case ISD::SETGE: return getConstant(C1 >= C2, VT); 933 } 934 } else { 935 // Ensure that the constant occurs on the RHS. 936 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 937 } 938 939 // Could not fold it. 940 return SDOperand(); 941} 942 943/// getNode - Gets or creates the specified node. 944/// 945SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { 946 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)]; 947 if (!N) { 948 N = new SDNode(Opcode, VT); 949 AllNodes.push_back(N); 950 } 951 return SDOperand(N, 0); 952} 953 954SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 955 SDOperand Operand) { 956 unsigned Tmp1; 957 // Constant fold unary operations with an integer constant operand. 958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { 959 uint64_t Val = C->getValue(); 960 switch (Opcode) { 961 default: break; 962 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); 963 case ISD::ANY_EXTEND: 964 case ISD::ZERO_EXTEND: return getConstant(Val, VT); 965 case ISD::TRUNCATE: return getConstant(Val, VT); 966 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); 967 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); 968 case ISD::BIT_CONVERT: 969 if (VT == MVT::f32) { 970 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!"); 971 return getConstantFP(BitsToFloat(Val), VT); 972 } else if (VT == MVT::f64) { 973 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!"); 974 return getConstantFP(BitsToDouble(Val), VT); 975 } 976 break; 977 case ISD::BSWAP: 978 switch(VT) { 979 default: assert(0 && "Invalid bswap!"); break; 980 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT); 981 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT); 982 case MVT::i64: return getConstant(ByteSwap_64(Val), VT); 983 } 984 break; 985 case ISD::CTPOP: 986 switch(VT) { 987 default: assert(0 && "Invalid ctpop!"); break; 988 case MVT::i1: return getConstant(Val != 0, VT); 989 case MVT::i8: 990 Tmp1 = (unsigned)Val & 0xFF; 991 return getConstant(CountPopulation_32(Tmp1), VT); 992 case MVT::i16: 993 Tmp1 = (unsigned)Val & 0xFFFF; 994 return getConstant(CountPopulation_32(Tmp1), VT); 995 case MVT::i32: 996 return getConstant(CountPopulation_32((unsigned)Val), VT); 997 case MVT::i64: 998 return getConstant(CountPopulation_64(Val), VT); 999 } 1000 case ISD::CTLZ: 1001 switch(VT) { 1002 default: assert(0 && "Invalid ctlz!"); break; 1003 case MVT::i1: return getConstant(Val == 0, VT); 1004 case MVT::i8: 1005 Tmp1 = (unsigned)Val & 0xFF; 1006 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT); 1007 case MVT::i16: 1008 Tmp1 = (unsigned)Val & 0xFFFF; 1009 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT); 1010 case MVT::i32: 1011 return getConstant(CountLeadingZeros_32((unsigned)Val), VT); 1012 case MVT::i64: 1013 return getConstant(CountLeadingZeros_64(Val), VT); 1014 } 1015 case ISD::CTTZ: 1016 switch(VT) { 1017 default: assert(0 && "Invalid cttz!"); break; 1018 case MVT::i1: return getConstant(Val == 0, VT); 1019 case MVT::i8: 1020 Tmp1 = (unsigned)Val | 0x100; 1021 return getConstant(CountTrailingZeros_32(Tmp1), VT); 1022 case MVT::i16: 1023 Tmp1 = (unsigned)Val | 0x10000; 1024 return getConstant(CountTrailingZeros_32(Tmp1), VT); 1025 case MVT::i32: 1026 return getConstant(CountTrailingZeros_32((unsigned)Val), VT); 1027 case MVT::i64: 1028 return getConstant(CountTrailingZeros_64(Val), VT); 1029 } 1030 } 1031 } 1032 1033 // Constant fold unary operations with an floating point constant operand. 1034 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) 1035 switch (Opcode) { 1036 case ISD::FNEG: 1037 return getConstantFP(-C->getValue(), VT); 1038 case ISD::FABS: 1039 return getConstantFP(fabs(C->getValue()), VT); 1040 case ISD::FP_ROUND: 1041 case ISD::FP_EXTEND: 1042 return getConstantFP(C->getValue(), VT); 1043 case ISD::FP_TO_SINT: 1044 return getConstant((int64_t)C->getValue(), VT); 1045 case ISD::FP_TO_UINT: 1046 return getConstant((uint64_t)C->getValue(), VT); 1047 case ISD::BIT_CONVERT: 1048 if (VT == MVT::i32) { 1049 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!"); 1050 return getConstant(FloatToBits(C->getValue()), VT); 1051 } else if (VT == MVT::i64) { 1052 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!"); 1053 return getConstant(DoubleToBits(C->getValue()), VT); 1054 } 1055 break; 1056 } 1057 1058 unsigned OpOpcode = Operand.Val->getOpcode(); 1059 switch (Opcode) { 1060 case ISD::TokenFactor: 1061 return Operand; // Factor of one node? No factor. 1062 case ISD::SIGN_EXTEND: 1063 if (Operand.getValueType() == VT) return Operand; // noop extension 1064 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 1065 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1066 break; 1067 case ISD::ZERO_EXTEND: 1068 if (Operand.getValueType() == VT) return Operand; // noop extension 1069 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 1070 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); 1071 break; 1072 case ISD::ANY_EXTEND: 1073 if (Operand.getValueType() == VT) return Operand; // noop extension 1074 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) 1075 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 1076 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1077 break; 1078 case ISD::TRUNCATE: 1079 if (Operand.getValueType() == VT) return Operand; // noop truncate 1080 if (OpOpcode == ISD::TRUNCATE) 1081 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1082 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 1083 OpOpcode == ISD::ANY_EXTEND) { 1084 // If the source is smaller than the dest, we still need an extend. 1085 if (Operand.Val->getOperand(0).getValueType() < VT) 1086 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1087 else if (Operand.Val->getOperand(0).getValueType() > VT) 1088 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1089 else 1090 return Operand.Val->getOperand(0); 1091 } 1092 break; 1093 case ISD::BIT_CONVERT: 1094 // Basic sanity checking. 1095 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) && 1096 "Cannot BIT_CONVERT between two different types!"); 1097 if (VT == Operand.getValueType()) return Operand; // noop conversion. 1098 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) 1099 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0)); 1100 break; 1101 case ISD::FNEG: 1102 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) 1103 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), 1104 Operand.Val->getOperand(0)); 1105 if (OpOpcode == ISD::FNEG) // --X -> X 1106 return Operand.Val->getOperand(0); 1107 break; 1108 case ISD::FABS: 1109 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 1110 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); 1111 break; 1112 } 1113 1114 SDNode *N; 1115 if (VT != MVT::Flag) { // Don't CSE flag producing nodes 1116 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))]; 1117 if (E) return SDOperand(E, 0); 1118 E = N = new SDNode(Opcode, Operand); 1119 } else { 1120 N = new SDNode(Opcode, Operand); 1121 } 1122 N->setValueTypes(VT); 1123 AllNodes.push_back(N); 1124 return SDOperand(N, 0); 1125} 1126 1127 1128 1129SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1130 SDOperand N1, SDOperand N2) { 1131#ifndef NDEBUG 1132 switch (Opcode) { 1133 case ISD::TokenFactor: 1134 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 1135 N2.getValueType() == MVT::Other && "Invalid token factor!"); 1136 break; 1137 case ISD::AND: 1138 case ISD::OR: 1139 case ISD::XOR: 1140 case ISD::UDIV: 1141 case ISD::UREM: 1142 case ISD::MULHU: 1143 case ISD::MULHS: 1144 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); 1145 // fall through 1146 case ISD::ADD: 1147 case ISD::SUB: 1148 case ISD::MUL: 1149 case ISD::SDIV: 1150 case ISD::SREM: 1151 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); 1152 // fall through. 1153 case ISD::FADD: 1154 case ISD::FSUB: 1155 case ISD::FMUL: 1156 case ISD::FDIV: 1157 case ISD::FREM: 1158 assert(N1.getValueType() == N2.getValueType() && 1159 N1.getValueType() == VT && "Binary operator types must match!"); 1160 break; 1161 1162 case ISD::SHL: 1163 case ISD::SRA: 1164 case ISD::SRL: 1165 case ISD::ROTL: 1166 case ISD::ROTR: 1167 assert(VT == N1.getValueType() && 1168 "Shift operators return type must be the same as their first arg"); 1169 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && 1170 VT != MVT::i1 && "Shifts only work on integers"); 1171 break; 1172 case ISD::FP_ROUND_INREG: { 1173 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1174 assert(VT == N1.getValueType() && "Not an inreg round!"); 1175 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && 1176 "Cannot FP_ROUND_INREG integer types"); 1177 assert(EVT <= VT && "Not rounding down!"); 1178 break; 1179 } 1180 case ISD::AssertSext: 1181 case ISD::AssertZext: 1182 case ISD::SIGN_EXTEND_INREG: { 1183 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1184 assert(VT == N1.getValueType() && "Not an inreg extend!"); 1185 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && 1186 "Cannot *_EXTEND_INREG FP types"); 1187 assert(EVT <= VT && "Not extending!"); 1188 } 1189 1190 default: break; 1191 } 1192#endif 1193 1194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1195 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1196 if (N1C) { 1197 if (N2C) { 1198 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); 1199 switch (Opcode) { 1200 case ISD::ADD: return getConstant(C1 + C2, VT); 1201 case ISD::SUB: return getConstant(C1 - C2, VT); 1202 case ISD::MUL: return getConstant(C1 * C2, VT); 1203 case ISD::UDIV: 1204 if (C2) return getConstant(C1 / C2, VT); 1205 break; 1206 case ISD::UREM : 1207 if (C2) return getConstant(C1 % C2, VT); 1208 break; 1209 case ISD::SDIV : 1210 if (C2) return getConstant(N1C->getSignExtended() / 1211 N2C->getSignExtended(), VT); 1212 break; 1213 case ISD::SREM : 1214 if (C2) return getConstant(N1C->getSignExtended() % 1215 N2C->getSignExtended(), VT); 1216 break; 1217 case ISD::AND : return getConstant(C1 & C2, VT); 1218 case ISD::OR : return getConstant(C1 | C2, VT); 1219 case ISD::XOR : return getConstant(C1 ^ C2, VT); 1220 case ISD::SHL : return getConstant(C1 << C2, VT); 1221 case ISD::SRL : return getConstant(C1 >> C2, VT); 1222 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); 1223 case ISD::ROTL : 1224 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)), 1225 VT); 1226 case ISD::ROTR : 1227 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), 1228 VT); 1229 default: break; 1230 } 1231 } else { // Cannonicalize constant to RHS if commutative 1232 if (isCommutativeBinOp(Opcode)) { 1233 std::swap(N1C, N2C); 1234 std::swap(N1, N2); 1235 } 1236 } 1237 } 1238 1239 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); 1240 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); 1241 if (N1CFP) { 1242 if (N2CFP) { 1243 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); 1244 switch (Opcode) { 1245 case ISD::FADD: return getConstantFP(C1 + C2, VT); 1246 case ISD::FSUB: return getConstantFP(C1 - C2, VT); 1247 case ISD::FMUL: return getConstantFP(C1 * C2, VT); 1248 case ISD::FDIV: 1249 if (C2) return getConstantFP(C1 / C2, VT); 1250 break; 1251 case ISD::FREM : 1252 if (C2) return getConstantFP(fmod(C1, C2), VT); 1253 break; 1254 default: break; 1255 } 1256 } else { // Cannonicalize constant to RHS if commutative 1257 if (isCommutativeBinOp(Opcode)) { 1258 std::swap(N1CFP, N2CFP); 1259 std::swap(N1, N2); 1260 } 1261 } 1262 } 1263 1264 // Finally, fold operations that do not require constants. 1265 switch (Opcode) { 1266 case ISD::FP_ROUND_INREG: 1267 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 1268 break; 1269 case ISD::SIGN_EXTEND_INREG: { 1270 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1271 if (EVT == VT) return N1; // Not actually extending 1272 break; 1273 } 1274 1275 // FIXME: figure out how to safely handle things like 1276 // int foo(int x) { return 1 << (x & 255); } 1277 // int bar() { return foo(256); } 1278#if 0 1279 case ISD::SHL: 1280 case ISD::SRL: 1281 case ISD::SRA: 1282 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && 1283 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) 1284 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1285 else if (N2.getOpcode() == ISD::AND) 1286 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { 1287 // If the and is only masking out bits that cannot effect the shift, 1288 // eliminate the and. 1289 unsigned NumBits = MVT::getSizeInBits(VT); 1290 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1291 return getNode(Opcode, VT, N1, N2.getOperand(0)); 1292 } 1293 break; 1294#endif 1295 } 1296 1297 // Memoize this node if possible. 1298 SDNode *N; 1299 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END && 1300 VT != MVT::Flag) { 1301 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))]; 1302 if (BON) return SDOperand(BON, 0); 1303 1304 BON = N = new SDNode(Opcode, N1, N2); 1305 } else { 1306 N = new SDNode(Opcode, N1, N2); 1307 } 1308 1309 N->setValueTypes(VT); 1310 AllNodes.push_back(N); 1311 return SDOperand(N, 0); 1312} 1313 1314SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1315 SDOperand N1, SDOperand N2, SDOperand N3) { 1316 // Perform various simplifications. 1317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1318 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1319 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); 1320 switch (Opcode) { 1321 case ISD::SETCC: { 1322 // Use SimplifySetCC to simplify SETCC's. 1323 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); 1324 if (Simp.Val) return Simp; 1325 break; 1326 } 1327 case ISD::SELECT: 1328 if (N1C) 1329 if (N1C->getValue()) 1330 return N2; // select true, X, Y -> X 1331 else 1332 return N3; // select false, X, Y -> Y 1333 1334 if (N2 == N3) return N2; // select C, X, X -> X 1335 break; 1336 case ISD::BRCOND: 1337 if (N2C) 1338 if (N2C->getValue()) // Unconditional branch 1339 return getNode(ISD::BR, MVT::Other, N1, N3); 1340 else 1341 return N1; // Never-taken branch 1342 break; 1343 } 1344 1345 std::vector<SDOperand> Ops; 1346 Ops.reserve(3); 1347 Ops.push_back(N1); 1348 Ops.push_back(N2); 1349 Ops.push_back(N3); 1350 1351 // Memoize node if it doesn't produce a flag. 1352 SDNode *N; 1353 if (VT != MVT::Flag) { 1354 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))]; 1355 if (E) return SDOperand(E, 0); 1356 E = N = new SDNode(Opcode, N1, N2, N3); 1357 } else { 1358 N = new SDNode(Opcode, N1, N2, N3); 1359 } 1360 N->setValueTypes(VT); 1361 AllNodes.push_back(N); 1362 return SDOperand(N, 0); 1363} 1364 1365SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1366 SDOperand N1, SDOperand N2, SDOperand N3, 1367 SDOperand N4) { 1368 std::vector<SDOperand> Ops; 1369 Ops.reserve(4); 1370 Ops.push_back(N1); 1371 Ops.push_back(N2); 1372 Ops.push_back(N3); 1373 Ops.push_back(N4); 1374 return getNode(Opcode, VT, Ops); 1375} 1376 1377SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1378 SDOperand N1, SDOperand N2, SDOperand N3, 1379 SDOperand N4, SDOperand N5) { 1380 std::vector<SDOperand> Ops; 1381 Ops.reserve(5); 1382 Ops.push_back(N1); 1383 Ops.push_back(N2); 1384 Ops.push_back(N3); 1385 Ops.push_back(N4); 1386 Ops.push_back(N5); 1387 return getNode(Opcode, VT, Ops); 1388} 1389 1390// setAdjCallChain - This method changes the token chain of an 1391// CALLSEQ_START/END node to be the specified operand. 1392void SDNode::setAdjCallChain(SDOperand N) { 1393 assert(N.getValueType() == MVT::Other); 1394 assert((getOpcode() == ISD::CALLSEQ_START || 1395 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!"); 1396 1397 OperandList[0].Val->removeUser(this); 1398 OperandList[0] = N; 1399 OperandList[0].Val->Uses.push_back(this); 1400} 1401 1402// setAdjCallFlag - This method changes the flag input of an 1403// CALLSEQ_START/END node to be the specified operand. 1404void SDNode::setAdjCallFlag(SDOperand N) { 1405 assert(N.getValueType() == MVT::Flag); 1406 assert((getOpcode() == ISD::CALLSEQ_START || 1407 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!"); 1408 1409 SDOperand &FlagOp = OperandList[getNumOperands()-1]; 1410 assert(FlagOp.getValueType() == MVT::Flag); 1411 FlagOp.Val->removeUser(this); 1412 FlagOp = N; 1413 FlagOp.Val->Uses.push_back(this); 1414} 1415 1416 1417SDOperand SelectionDAG::getLoad(MVT::ValueType VT, 1418 SDOperand Chain, SDOperand Ptr, 1419 SDOperand SV) { 1420 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))]; 1421 if (N) return SDOperand(N, 0); 1422 N = new SDNode(ISD::LOAD, Chain, Ptr, SV); 1423 1424 // Loads have a token chain. 1425 setNodeValueTypes(N, VT, MVT::Other); 1426 AllNodes.push_back(N); 1427 return SDOperand(N, 0); 1428} 1429 1430SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT, 1431 SDOperand Chain, SDOperand Ptr, 1432 SDOperand SV) { 1433 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))]; 1434 if (N) return SDOperand(N, 0); 1435 std::vector<SDOperand> Ops; 1436 Ops.reserve(5); 1437 Ops.push_back(Chain); 1438 Ops.push_back(Ptr); 1439 Ops.push_back(getConstant(Count, MVT::i32)); 1440 Ops.push_back(getValueType(EVT)); 1441 Ops.push_back(SV); 1442 std::vector<MVT::ValueType> VTs; 1443 VTs.reserve(2); 1444 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain. 1445 return getNode(ISD::VLOAD, VTs, Ops); 1446} 1447 1448SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT, 1449 SDOperand Chain, SDOperand Ptr, SDOperand SV, 1450 MVT::ValueType EVT) { 1451 std::vector<SDOperand> Ops; 1452 Ops.reserve(4); 1453 Ops.push_back(Chain); 1454 Ops.push_back(Ptr); 1455 Ops.push_back(SV); 1456 Ops.push_back(getValueType(EVT)); 1457 std::vector<MVT::ValueType> VTs; 1458 VTs.reserve(2); 1459 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. 1460 return getNode(Opcode, VTs, Ops); 1461} 1462 1463SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { 1464 assert((!V || isa<PointerType>(V->getType())) && 1465 "SrcValue is not a pointer?"); 1466 SDNode *&N = ValueNodes[std::make_pair(V, Offset)]; 1467 if (N) return SDOperand(N, 0); 1468 1469 N = new SrcValueSDNode(V, Offset); 1470 AllNodes.push_back(N); 1471 return SDOperand(N, 0); 1472} 1473 1474SDOperand SelectionDAG::getVAArg(MVT::ValueType VT, 1475 SDOperand Chain, SDOperand Ptr, 1476 SDOperand SV) { 1477 std::vector<SDOperand> Ops; 1478 Ops.reserve(3); 1479 Ops.push_back(Chain); 1480 Ops.push_back(Ptr); 1481 Ops.push_back(SV); 1482 std::vector<MVT::ValueType> VTs; 1483 VTs.reserve(2); 1484 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. 1485 return getNode(ISD::VAARG, VTs, Ops); 1486} 1487 1488SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1489 std::vector<SDOperand> &Ops) { 1490 switch (Ops.size()) { 1491 case 0: return getNode(Opcode, VT); 1492 case 1: return getNode(Opcode, VT, Ops[0]); 1493 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); 1494 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); 1495 default: break; 1496 } 1497 1498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val); 1499 switch (Opcode) { 1500 default: break; 1501 case ISD::BRCONDTWOWAY: 1502 if (N1C) 1503 if (N1C->getValue()) // Unconditional branch to true dest. 1504 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]); 1505 else // Unconditional branch to false dest. 1506 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]); 1507 break; 1508 case ISD::BRTWOWAY_CC: 1509 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!"); 1510 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1511 "LHS and RHS of comparison must have same type!"); 1512 break; 1513 case ISD::TRUNCSTORE: { 1514 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!"); 1515 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT(); 1516#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store 1517 // If this is a truncating store of a constant, convert to the desired type 1518 // and store it instead. 1519 if (isa<Constant>(Ops[0])) { 1520 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); 1521 if (isa<Constant>(Op)) 1522 N1 = Op; 1523 } 1524 // Also for ConstantFP? 1525#endif 1526 if (Ops[0].getValueType() == EVT) // Normal store? 1527 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]); 1528 assert(Ops[1].getValueType() > EVT && "Not a truncation?"); 1529 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) && 1530 "Can't do FP-INT conversion!"); 1531 break; 1532 } 1533 case ISD::SELECT_CC: { 1534 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!"); 1535 assert(Ops[0].getValueType() == Ops[1].getValueType() && 1536 "LHS and RHS of condition must have same type!"); 1537 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1538 "True and False arms of SelectCC must have same type!"); 1539 assert(Ops[2].getValueType() == VT && 1540 "select_cc node must be of same type as true and false value!"); 1541 break; 1542 } 1543 case ISD::BR_CC: { 1544 assert(Ops.size() == 5 && "BR_CC takes 5 operands!"); 1545 assert(Ops[2].getValueType() == Ops[3].getValueType() && 1546 "LHS/RHS of comparison should match types!"); 1547 break; 1548 } 1549 } 1550 1551 // Memoize nodes. 1552 SDNode *N; 1553 if (VT != MVT::Flag) { 1554 SDNode *&E = 1555 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))]; 1556 if (E) return SDOperand(E, 0); 1557 E = N = new SDNode(Opcode, Ops); 1558 } else { 1559 N = new SDNode(Opcode, Ops); 1560 } 1561 N->setValueTypes(VT); 1562 AllNodes.push_back(N); 1563 return SDOperand(N, 0); 1564} 1565 1566SDOperand SelectionDAG::getNode(unsigned Opcode, 1567 std::vector<MVT::ValueType> &ResultTys, 1568 std::vector<SDOperand> &Ops) { 1569 if (ResultTys.size() == 1) 1570 return getNode(Opcode, ResultTys[0], Ops); 1571 1572 switch (Opcode) { 1573 case ISD::EXTLOAD: 1574 case ISD::SEXTLOAD: 1575 case ISD::ZEXTLOAD: { 1576 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT(); 1577 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!"); 1578 // If they are asking for an extending load from/to the same thing, return a 1579 // normal load. 1580 if (ResultTys[0] == EVT) 1581 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]); 1582 assert(EVT < ResultTys[0] && 1583 "Should only be an extending load, not truncating!"); 1584 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) && 1585 "Cannot sign/zero extend a FP load!"); 1586 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) && 1587 "Cannot convert from FP to Int or Int -> FP!"); 1588 break; 1589 } 1590 1591 // FIXME: figure out how to safely handle things like 1592 // int foo(int x) { return 1 << (x & 255); } 1593 // int bar() { return foo(256); } 1594#if 0 1595 case ISD::SRA_PARTS: 1596 case ISD::SRL_PARTS: 1597 case ISD::SHL_PARTS: 1598 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 1599 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 1600 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1601 else if (N3.getOpcode() == ISD::AND) 1602 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 1603 // If the and is only masking out bits that cannot effect the shift, 1604 // eliminate the and. 1605 unsigned NumBits = MVT::getSizeInBits(VT)*2; 1606 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 1607 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 1608 } 1609 break; 1610#endif 1611 } 1612 1613 // Memoize the node unless it returns a flag. 1614 SDNode *N; 1615 if (ResultTys.back() != MVT::Flag) { 1616 SDNode *&E = 1617 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))]; 1618 if (E) return SDOperand(E, 0); 1619 E = N = new SDNode(Opcode, Ops); 1620 } else { 1621 N = new SDNode(Opcode, Ops); 1622 } 1623 setNodeValueTypes(N, ResultTys); 1624 AllNodes.push_back(N); 1625 return SDOperand(N, 0); 1626} 1627 1628void SelectionDAG::setNodeValueTypes(SDNode *N, 1629 std::vector<MVT::ValueType> &RetVals) { 1630 switch (RetVals.size()) { 1631 case 0: return; 1632 case 1: N->setValueTypes(RetVals[0]); return; 1633 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return; 1634 default: break; 1635 } 1636 1637 std::list<std::vector<MVT::ValueType> >::iterator I = 1638 std::find(VTList.begin(), VTList.end(), RetVals); 1639 if (I == VTList.end()) { 1640 VTList.push_front(RetVals); 1641 I = VTList.begin(); 1642 } 1643 1644 N->setValueTypes(&(*I)[0], I->size()); 1645} 1646 1647void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1, 1648 MVT::ValueType VT2) { 1649 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 1650 E = VTList.end(); I != E; ++I) { 1651 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) { 1652 N->setValueTypes(&(*I)[0], 2); 1653 return; 1654 } 1655 } 1656 std::vector<MVT::ValueType> V; 1657 V.push_back(VT1); 1658 V.push_back(VT2); 1659 VTList.push_front(V); 1660 N->setValueTypes(&(*VTList.begin())[0], 2); 1661} 1662 1663/// UpdateNodeOperands - *Mutate* the specified node in-place to have the 1664/// specified operands. If the resultant node already exists in the DAG, 1665/// this does not modify the specified node, instead it returns the node that 1666/// already exists. If the resultant node does not exist in the DAG, the 1667/// input node is returned. As a degenerate case, if you specify the same 1668/// input operands as the node already has, the input node is returned. 1669SDOperand SelectionDAG:: 1670UpdateNodeOperands(SDOperand InN, SDOperand Op) { 1671 SDNode *N = InN.Val; 1672 assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 1673 1674 // Check to see if there is no change. 1675 if (Op == N->getOperand(0)) return InN; 1676 1677 // See if the modified node already exists. 1678 SDNode **NewSlot = FindModifiedNodeSlot(N, Op); 1679 if (NewSlot && *NewSlot) 1680 return SDOperand(*NewSlot, InN.ResNo); 1681 1682 // Nope it doesn't. Remove the node from it's current place in the maps. 1683 if (NewSlot) 1684 RemoveNodeFromCSEMaps(N); 1685 1686 // Now we update the operands. 1687 N->OperandList[0].Val->removeUser(N); 1688 Op.Val->addUser(N); 1689 N->OperandList[0] = Op; 1690 1691 // If this gets put into a CSE map, add it. 1692 if (NewSlot) *NewSlot = N; 1693 return InN; 1694} 1695 1696SDOperand SelectionDAG:: 1697UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) { 1698 SDNode *N = InN.Val; 1699 assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 1700 1701 // Check to see if there is no change. 1702 bool AnyChange = false; 1703 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 1704 return InN; // No operands changed, just return the input node. 1705 1706 // See if the modified node already exists. 1707 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2); 1708 if (NewSlot && *NewSlot) 1709 return SDOperand(*NewSlot, InN.ResNo); 1710 1711 // Nope it doesn't. Remove the node from it's current place in the maps. 1712 if (NewSlot) 1713 RemoveNodeFromCSEMaps(N); 1714 1715 // Now we update the operands. 1716 if (N->OperandList[0] != Op1) { 1717 N->OperandList[0].Val->removeUser(N); 1718 Op1.Val->addUser(N); 1719 N->OperandList[0] = Op1; 1720 } 1721 if (N->OperandList[1] != Op2) { 1722 N->OperandList[1].Val->removeUser(N); 1723 Op2.Val->addUser(N); 1724 N->OperandList[1] = Op2; 1725 } 1726 1727 // If this gets put into a CSE map, add it. 1728 if (NewSlot) *NewSlot = N; 1729 return InN; 1730} 1731 1732SDOperand SelectionDAG:: 1733UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) { 1734 std::vector<SDOperand> Ops; 1735 Ops.push_back(Op1); 1736 Ops.push_back(Op2); 1737 Ops.push_back(Op3); 1738 return UpdateNodeOperands(N, Ops); 1739} 1740 1741SDOperand SelectionDAG:: 1742UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 1743 SDOperand Op3, SDOperand Op4) { 1744 std::vector<SDOperand> Ops; 1745 Ops.push_back(Op1); 1746 Ops.push_back(Op2); 1747 Ops.push_back(Op3); 1748 Ops.push_back(Op4); 1749 return UpdateNodeOperands(N, Ops); 1750} 1751 1752SDOperand SelectionDAG:: 1753UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 1754 SDOperand Op3, SDOperand Op4, SDOperand Op5) { 1755 std::vector<SDOperand> Ops; 1756 Ops.push_back(Op1); 1757 Ops.push_back(Op2); 1758 Ops.push_back(Op3); 1759 Ops.push_back(Op4); 1760 Ops.push_back(Op5); 1761 return UpdateNodeOperands(N, Ops); 1762} 1763 1764 1765SDOperand SelectionDAG:: 1766UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) { 1767 SDNode *N = InN.Val; 1768 assert(N->getNumOperands() == Ops.size() && 1769 "Update with wrong number of operands"); 1770 1771 // Check to see if there is no change. 1772 unsigned NumOps = Ops.size(); 1773 bool AnyChange = false; 1774 for (unsigned i = 0; i != NumOps; ++i) { 1775 if (Ops[i] != N->getOperand(i)) { 1776 AnyChange = true; 1777 break; 1778 } 1779 } 1780 1781 // No operands changed, just return the input node. 1782 if (!AnyChange) return InN; 1783 1784 // See if the modified node already exists. 1785 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops); 1786 if (NewSlot && *NewSlot) 1787 return SDOperand(*NewSlot, InN.ResNo); 1788 1789 // Nope it doesn't. Remove the node from it's current place in the maps. 1790 if (NewSlot) 1791 RemoveNodeFromCSEMaps(N); 1792 1793 // Now we update the operands. 1794 for (unsigned i = 0; i != NumOps; ++i) { 1795 if (N->OperandList[i] != Ops[i]) { 1796 N->OperandList[i].Val->removeUser(N); 1797 Ops[i].Val->addUser(N); 1798 N->OperandList[i] = Ops[i]; 1799 } 1800 } 1801 1802 // If this gets put into a CSE map, add it. 1803 if (NewSlot) *NewSlot = N; 1804 return InN; 1805} 1806 1807 1808 1809 1810/// SelectNodeTo - These are used for target selectors to *mutate* the 1811/// specified node to have the specified return type, Target opcode, and 1812/// operands. Note that target opcodes are stored as 1813/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 1814/// 1815/// Note that SelectNodeTo returns the resultant node. If there is already a 1816/// node of the specified opcode and operands, it returns that node instead of 1817/// the current one. 1818SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1819 MVT::ValueType VT) { 1820 // If an identical node already exists, use it. 1821 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)]; 1822 if (ON) return SDOperand(ON, 0); 1823 1824 RemoveNodeFromCSEMaps(N); 1825 1826 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1827 N->setValueTypes(VT); 1828 1829 ON = N; // Memoize the new node. 1830 return SDOperand(N, 0); 1831} 1832 1833SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1834 MVT::ValueType VT, SDOperand Op1) { 1835 // If an identical node already exists, use it. 1836 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1837 std::make_pair(Op1, VT))]; 1838 if (ON) return SDOperand(ON, 0); 1839 1840 RemoveNodeFromCSEMaps(N); 1841 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1842 N->setValueTypes(VT); 1843 N->setOperands(Op1); 1844 1845 ON = N; // Memoize the new node. 1846 return SDOperand(N, 0); 1847} 1848 1849SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1850 MVT::ValueType VT, SDOperand Op1, 1851 SDOperand Op2) { 1852 // If an identical node already exists, use it. 1853 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1854 std::make_pair(Op1, Op2))]; 1855 if (ON) return SDOperand(ON, 0); 1856 1857 RemoveNodeFromCSEMaps(N); 1858 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1859 N->setValueTypes(VT); 1860 N->setOperands(Op1, Op2); 1861 1862 ON = N; // Memoize the new node. 1863 return SDOperand(N, 0); 1864} 1865 1866SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1867 MVT::ValueType VT, SDOperand Op1, 1868 SDOperand Op2, SDOperand Op3) { 1869 // If an identical node already exists, use it. 1870 std::vector<SDOperand> OpList; 1871 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 1872 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1873 std::make_pair(VT, OpList))]; 1874 if (ON) return SDOperand(ON, 0); 1875 1876 RemoveNodeFromCSEMaps(N); 1877 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1878 N->setValueTypes(VT); 1879 N->setOperands(Op1, Op2, Op3); 1880 1881 ON = N; // Memoize the new node. 1882 return SDOperand(N, 0); 1883} 1884 1885SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1886 MVT::ValueType VT, SDOperand Op1, 1887 SDOperand Op2, SDOperand Op3, 1888 SDOperand Op4) { 1889 // If an identical node already exists, use it. 1890 std::vector<SDOperand> OpList; 1891 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 1892 OpList.push_back(Op4); 1893 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1894 std::make_pair(VT, OpList))]; 1895 if (ON) return SDOperand(ON, 0); 1896 1897 RemoveNodeFromCSEMaps(N); 1898 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1899 N->setValueTypes(VT); 1900 N->setOperands(Op1, Op2, Op3, Op4); 1901 1902 ON = N; // Memoize the new node. 1903 return SDOperand(N, 0); 1904} 1905 1906SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1907 MVT::ValueType VT, SDOperand Op1, 1908 SDOperand Op2, SDOperand Op3,SDOperand Op4, 1909 SDOperand Op5) { 1910 // If an identical node already exists, use it. 1911 std::vector<SDOperand> OpList; 1912 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 1913 OpList.push_back(Op4); OpList.push_back(Op5); 1914 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1915 std::make_pair(VT, OpList))]; 1916 if (ON) return SDOperand(ON, 0); 1917 1918 RemoveNodeFromCSEMaps(N); 1919 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1920 N->setValueTypes(VT); 1921 N->setOperands(Op1, Op2, Op3, Op4, Op5); 1922 1923 ON = N; // Memoize the new node. 1924 return SDOperand(N, 0); 1925} 1926 1927SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1928 MVT::ValueType VT, SDOperand Op1, 1929 SDOperand Op2, SDOperand Op3,SDOperand Op4, 1930 SDOperand Op5, SDOperand Op6) { 1931 // If an identical node already exists, use it. 1932 std::vector<SDOperand> OpList; 1933 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 1934 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); 1935 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1936 std::make_pair(VT, OpList))]; 1937 if (ON) return SDOperand(ON, 0); 1938 1939 RemoveNodeFromCSEMaps(N); 1940 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1941 N->setValueTypes(VT); 1942 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6); 1943 1944 ON = N; // Memoize the new node. 1945 return SDOperand(N, 0); 1946} 1947 1948SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1949 MVT::ValueType VT, SDOperand Op1, 1950 SDOperand Op2, SDOperand Op3,SDOperand Op4, 1951 SDOperand Op5, SDOperand Op6, 1952 SDOperand Op7) { 1953 // If an identical node already exists, use it. 1954 std::vector<SDOperand> OpList; 1955 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 1956 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); 1957 OpList.push_back(Op7); 1958 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1959 std::make_pair(VT, OpList))]; 1960 if (ON) return SDOperand(ON, 0); 1961 1962 RemoveNodeFromCSEMaps(N); 1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1964 N->setValueTypes(VT); 1965 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7); 1966 1967 ON = N; // Memoize the new node. 1968 return SDOperand(N, 0); 1969} 1970SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1971 MVT::ValueType VT, SDOperand Op1, 1972 SDOperand Op2, SDOperand Op3,SDOperand Op4, 1973 SDOperand Op5, SDOperand Op6, 1974 SDOperand Op7, SDOperand Op8) { 1975 // If an identical node already exists, use it. 1976 std::vector<SDOperand> OpList; 1977 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 1978 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); 1979 OpList.push_back(Op7); OpList.push_back(Op8); 1980 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 1981 std::make_pair(VT, OpList))]; 1982 if (ON) return SDOperand(ON, 0); 1983 1984 RemoveNodeFromCSEMaps(N); 1985 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 1986 N->setValueTypes(VT); 1987 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8); 1988 1989 ON = N; // Memoize the new node. 1990 return SDOperand(N, 0); 1991} 1992 1993SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 1994 MVT::ValueType VT1, MVT::ValueType VT2, 1995 SDOperand Op1, SDOperand Op2) { 1996 // If an identical node already exists, use it. 1997 std::vector<SDOperand> OpList; 1998 OpList.push_back(Op1); OpList.push_back(Op2); 1999 std::vector<MVT::ValueType> VTList; 2000 VTList.push_back(VT1); VTList.push_back(VT2); 2001 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 2002 std::make_pair(VTList, OpList))]; 2003 if (ON) return SDOperand(ON, 0); 2004 2005 RemoveNodeFromCSEMaps(N); 2006 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 2007 setNodeValueTypes(N, VT1, VT2); 2008 N->setOperands(Op1, Op2); 2009 2010 ON = N; // Memoize the new node. 2011 return SDOperand(N, 0); 2012} 2013 2014SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2015 MVT::ValueType VT1, MVT::ValueType VT2, 2016 SDOperand Op1, SDOperand Op2, 2017 SDOperand Op3) { 2018 // If an identical node already exists, use it. 2019 std::vector<SDOperand> OpList; 2020 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 2021 std::vector<MVT::ValueType> VTList; 2022 VTList.push_back(VT1); VTList.push_back(VT2); 2023 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 2024 std::make_pair(VTList, OpList))]; 2025 if (ON) return SDOperand(ON, 0); 2026 2027 RemoveNodeFromCSEMaps(N); 2028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 2029 setNodeValueTypes(N, VT1, VT2); 2030 N->setOperands(Op1, Op2, Op3); 2031 2032 ON = N; // Memoize the new node. 2033 return SDOperand(N, 0); 2034} 2035 2036SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2037 MVT::ValueType VT1, MVT::ValueType VT2, 2038 SDOperand Op1, SDOperand Op2, 2039 SDOperand Op3, SDOperand Op4) { 2040 // If an identical node already exists, use it. 2041 std::vector<SDOperand> OpList; 2042 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 2043 OpList.push_back(Op4); 2044 std::vector<MVT::ValueType> VTList; 2045 VTList.push_back(VT1); VTList.push_back(VT2); 2046 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 2047 std::make_pair(VTList, OpList))]; 2048 if (ON) return SDOperand(ON, 0); 2049 2050 RemoveNodeFromCSEMaps(N); 2051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 2052 setNodeValueTypes(N, VT1, VT2); 2053 N->setOperands(Op1, Op2, Op3, Op4); 2054 2055 ON = N; // Memoize the new node. 2056 return SDOperand(N, 0); 2057} 2058 2059SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2060 MVT::ValueType VT1, MVT::ValueType VT2, 2061 SDOperand Op1, SDOperand Op2, 2062 SDOperand Op3, SDOperand Op4, 2063 SDOperand Op5) { 2064 // If an identical node already exists, use it. 2065 std::vector<SDOperand> OpList; 2066 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); 2067 OpList.push_back(Op4); OpList.push_back(Op5); 2068 std::vector<MVT::ValueType> VTList; 2069 VTList.push_back(VT1); VTList.push_back(VT2); 2070 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, 2071 std::make_pair(VTList, OpList))]; 2072 if (ON) return SDOperand(ON, 0); 2073 2074 RemoveNodeFromCSEMaps(N); 2075 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); 2076 setNodeValueTypes(N, VT1, VT2); 2077 N->setOperands(Op1, Op2, Op3, Op4, Op5); 2078 2079 ON = N; // Memoize the new node. 2080 return SDOperand(N, 0); 2081} 2082 2083// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 2084/// This can cause recursive merging of nodes in the DAG. 2085/// 2086/// This version assumes From/To have a single result value. 2087/// 2088void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, 2089 std::vector<SDNode*> *Deleted) { 2090 SDNode *From = FromN.Val, *To = ToN.Val; 2091 assert(From->getNumValues() == 1 && To->getNumValues() == 1 && 2092 "Cannot replace with this method!"); 2093 assert(From != To && "Cannot replace uses of with self"); 2094 2095 while (!From->use_empty()) { 2096 // Process users until they are all gone. 2097 SDNode *U = *From->use_begin(); 2098 2099 // This node is about to morph, remove its old self from the CSE maps. 2100 RemoveNodeFromCSEMaps(U); 2101 2102 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 2103 I != E; ++I) 2104 if (I->Val == From) { 2105 From->removeUser(U); 2106 I->Val = To; 2107 To->addUser(U); 2108 } 2109 2110 // Now that we have modified U, add it back to the CSE maps. If it already 2111 // exists there, recursively merge the results together. 2112 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 2113 ReplaceAllUsesWith(U, Existing, Deleted); 2114 // U is now dead. 2115 if (Deleted) Deleted->push_back(U); 2116 DeleteNodeNotInCSEMaps(U); 2117 } 2118 } 2119} 2120 2121/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 2122/// This can cause recursive merging of nodes in the DAG. 2123/// 2124/// This version assumes From/To have matching types and numbers of result 2125/// values. 2126/// 2127void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 2128 std::vector<SDNode*> *Deleted) { 2129 assert(From != To && "Cannot replace uses of with self"); 2130 assert(From->getNumValues() == To->getNumValues() && 2131 "Cannot use this version of ReplaceAllUsesWith!"); 2132 if (From->getNumValues() == 1) { // If possible, use the faster version. 2133 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); 2134 return; 2135 } 2136 2137 while (!From->use_empty()) { 2138 // Process users until they are all gone. 2139 SDNode *U = *From->use_begin(); 2140 2141 // This node is about to morph, remove its old self from the CSE maps. 2142 RemoveNodeFromCSEMaps(U); 2143 2144 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 2145 I != E; ++I) 2146 if (I->Val == From) { 2147 From->removeUser(U); 2148 I->Val = To; 2149 To->addUser(U); 2150 } 2151 2152 // Now that we have modified U, add it back to the CSE maps. If it already 2153 // exists there, recursively merge the results together. 2154 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 2155 ReplaceAllUsesWith(U, Existing, Deleted); 2156 // U is now dead. 2157 if (Deleted) Deleted->push_back(U); 2158 DeleteNodeNotInCSEMaps(U); 2159 } 2160 } 2161} 2162 2163/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 2164/// This can cause recursive merging of nodes in the DAG. 2165/// 2166/// This version can replace From with any result values. To must match the 2167/// number and types of values returned by From. 2168void SelectionDAG::ReplaceAllUsesWith(SDNode *From, 2169 const std::vector<SDOperand> &To, 2170 std::vector<SDNode*> *Deleted) { 2171 assert(From->getNumValues() == To.size() && 2172 "Incorrect number of values to replace with!"); 2173 if (To.size() == 1 && To[0].Val->getNumValues() == 1) { 2174 // Degenerate case handled above. 2175 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); 2176 return; 2177 } 2178 2179 while (!From->use_empty()) { 2180 // Process users until they are all gone. 2181 SDNode *U = *From->use_begin(); 2182 2183 // This node is about to morph, remove its old self from the CSE maps. 2184 RemoveNodeFromCSEMaps(U); 2185 2186 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 2187 I != E; ++I) 2188 if (I->Val == From) { 2189 const SDOperand &ToOp = To[I->ResNo]; 2190 From->removeUser(U); 2191 *I = ToOp; 2192 ToOp.Val->addUser(U); 2193 } 2194 2195 // Now that we have modified U, add it back to the CSE maps. If it already 2196 // exists there, recursively merge the results together. 2197 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 2198 ReplaceAllUsesWith(U, Existing, Deleted); 2199 // U is now dead. 2200 if (Deleted) Deleted->push_back(U); 2201 DeleteNodeNotInCSEMaps(U); 2202 } 2203 } 2204} 2205 2206 2207//===----------------------------------------------------------------------===// 2208// SDNode Class 2209//===----------------------------------------------------------------------===// 2210 2211 2212/// getValueTypeList - Return a pointer to the specified value type. 2213/// 2214MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { 2215 static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; 2216 VTs[VT] = VT; 2217 return &VTs[VT]; 2218} 2219 2220/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 2221/// indicated value. This method ignores uses of other values defined by this 2222/// operation. 2223bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) { 2224 assert(Value < getNumValues() && "Bad value!"); 2225 2226 // If there is only one value, this is easy. 2227 if (getNumValues() == 1) 2228 return use_size() == NUses; 2229 if (Uses.size() < NUses) return false; 2230 2231 SDOperand TheValue(this, Value); 2232 2233 std::set<SDNode*> UsersHandled; 2234 2235 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end(); 2236 UI != E; ++UI) { 2237 SDNode *User = *UI; 2238 if (User->getNumOperands() == 1 || 2239 UsersHandled.insert(User).second) // First time we've seen this? 2240 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 2241 if (User->getOperand(i) == TheValue) { 2242 if (NUses == 0) 2243 return false; // too many uses 2244 --NUses; 2245 } 2246 } 2247 2248 // Found exactly the right number of uses? 2249 return NUses == 0; 2250} 2251 2252 2253const char *SDNode::getOperationName(const SelectionDAG *G) const { 2254 switch (getOpcode()) { 2255 default: 2256 if (getOpcode() < ISD::BUILTIN_OP_END) 2257 return "<<Unknown DAG Node>>"; 2258 else { 2259 if (G) { 2260 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 2261 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) 2262 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); 2263 2264 TargetLowering &TLI = G->getTargetLoweringInfo(); 2265 const char *Name = 2266 TLI.getTargetNodeName(getOpcode()); 2267 if (Name) return Name; 2268 } 2269 2270 return "<<Unknown Target Node>>"; 2271 } 2272 2273 case ISD::PCMARKER: return "PCMarker"; 2274 case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 2275 case ISD::SRCVALUE: return "SrcValue"; 2276 case ISD::VALUETYPE: return "ValueType"; 2277 case ISD::STRING: return "String"; 2278 case ISD::EntryToken: return "EntryToken"; 2279 case ISD::TokenFactor: return "TokenFactor"; 2280 case ISD::AssertSext: return "AssertSext"; 2281 case ISD::AssertZext: return "AssertZext"; 2282 case ISD::Constant: return "Constant"; 2283 case ISD::TargetConstant: return "TargetConstant"; 2284 case ISD::ConstantFP: return "ConstantFP"; 2285 case ISD::ConstantVec: return "ConstantVec"; 2286 case ISD::GlobalAddress: return "GlobalAddress"; 2287 case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 2288 case ISD::FrameIndex: return "FrameIndex"; 2289 case ISD::TargetFrameIndex: return "TargetFrameIndex"; 2290 case ISD::BasicBlock: return "BasicBlock"; 2291 case ISD::Register: return "Register"; 2292 case ISD::ExternalSymbol: return "ExternalSymbol"; 2293 case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 2294 case ISD::ConstantPool: return "ConstantPool"; 2295 case ISD::TargetConstantPool: return "TargetConstantPool"; 2296 case ISD::CopyToReg: return "CopyToReg"; 2297 case ISD::CopyFromReg: return "CopyFromReg"; 2298 case ISD::UNDEF: return "undef"; 2299 case ISD::MERGE_VALUES: return "mergevalues"; 2300 case ISD::INLINEASM: return "inlineasm"; 2301 2302 // Unary operators 2303 case ISD::FABS: return "fabs"; 2304 case ISD::FNEG: return "fneg"; 2305 case ISD::FSQRT: return "fsqrt"; 2306 case ISD::FSIN: return "fsin"; 2307 case ISD::FCOS: return "fcos"; 2308 2309 // Binary operators 2310 case ISD::ADD: return "add"; 2311 case ISD::SUB: return "sub"; 2312 case ISD::MUL: return "mul"; 2313 case ISD::MULHU: return "mulhu"; 2314 case ISD::MULHS: return "mulhs"; 2315 case ISD::SDIV: return "sdiv"; 2316 case ISD::UDIV: return "udiv"; 2317 case ISD::SREM: return "srem"; 2318 case ISD::UREM: return "urem"; 2319 case ISD::AND: return "and"; 2320 case ISD::OR: return "or"; 2321 case ISD::XOR: return "xor"; 2322 case ISD::SHL: return "shl"; 2323 case ISD::SRA: return "sra"; 2324 case ISD::SRL: return "srl"; 2325 case ISD::ROTL: return "rotl"; 2326 case ISD::ROTR: return "rotr"; 2327 case ISD::FADD: return "fadd"; 2328 case ISD::FSUB: return "fsub"; 2329 case ISD::FMUL: return "fmul"; 2330 case ISD::FDIV: return "fdiv"; 2331 case ISD::FREM: return "frem"; 2332 case ISD::VADD: return "vadd"; 2333 case ISD::VSUB: return "vsub"; 2334 case ISD::VMUL: return "vmul"; 2335 2336 case ISD::SETCC: return "setcc"; 2337 case ISD::SELECT: return "select"; 2338 case ISD::SELECT_CC: return "select_cc"; 2339 case ISD::ADD_PARTS: return "add_parts"; 2340 case ISD::SUB_PARTS: return "sub_parts"; 2341 case ISD::SHL_PARTS: return "shl_parts"; 2342 case ISD::SRA_PARTS: return "sra_parts"; 2343 case ISD::SRL_PARTS: return "srl_parts"; 2344 2345 // Conversion operators. 2346 case ISD::SIGN_EXTEND: return "sign_extend"; 2347 case ISD::ZERO_EXTEND: return "zero_extend"; 2348 case ISD::ANY_EXTEND: return "any_extend"; 2349 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 2350 case ISD::TRUNCATE: return "truncate"; 2351 case ISD::FP_ROUND: return "fp_round"; 2352 case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 2353 case ISD::FP_EXTEND: return "fp_extend"; 2354 2355 case ISD::SINT_TO_FP: return "sint_to_fp"; 2356 case ISD::UINT_TO_FP: return "uint_to_fp"; 2357 case ISD::FP_TO_SINT: return "fp_to_sint"; 2358 case ISD::FP_TO_UINT: return "fp_to_uint"; 2359 case ISD::BIT_CONVERT: return "bit_convert"; 2360 2361 // Control flow instructions 2362 case ISD::BR: return "br"; 2363 case ISD::BRCOND: return "brcond"; 2364 case ISD::BRCONDTWOWAY: return "brcondtwoway"; 2365 case ISD::BR_CC: return "br_cc"; 2366 case ISD::BRTWOWAY_CC: return "brtwoway_cc"; 2367 case ISD::RET: return "ret"; 2368 case ISD::CALLSEQ_START: return "callseq_start"; 2369 case ISD::CALLSEQ_END: return "callseq_end"; 2370 2371 // Other operators 2372 case ISD::LOAD: return "load"; 2373 case ISD::STORE: return "store"; 2374 case ISD::VLOAD: return "vload"; 2375 case ISD::EXTLOAD: return "extload"; 2376 case ISD::SEXTLOAD: return "sextload"; 2377 case ISD::ZEXTLOAD: return "zextload"; 2378 case ISD::TRUNCSTORE: return "truncstore"; 2379 case ISD::VAARG: return "vaarg"; 2380 case ISD::VACOPY: return "vacopy"; 2381 case ISD::VAEND: return "vaend"; 2382 case ISD::VASTART: return "vastart"; 2383 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 2384 case ISD::EXTRACT_ELEMENT: return "extract_element"; 2385 case ISD::BUILD_PAIR: return "build_pair"; 2386 case ISD::STACKSAVE: return "stacksave"; 2387 case ISD::STACKRESTORE: return "stackrestore"; 2388 2389 // Block memory operations. 2390 case ISD::MEMSET: return "memset"; 2391 case ISD::MEMCPY: return "memcpy"; 2392 case ISD::MEMMOVE: return "memmove"; 2393 2394 // Bit manipulation 2395 case ISD::BSWAP: return "bswap"; 2396 case ISD::CTPOP: return "ctpop"; 2397 case ISD::CTTZ: return "cttz"; 2398 case ISD::CTLZ: return "ctlz"; 2399 2400 // IO Intrinsics 2401 case ISD::READPORT: return "readport"; 2402 case ISD::WRITEPORT: return "writeport"; 2403 case ISD::READIO: return "readio"; 2404 case ISD::WRITEIO: return "writeio"; 2405 2406 // Debug info 2407 case ISD::LOCATION: return "location"; 2408 case ISD::DEBUG_LOC: return "debug_loc"; 2409 case ISD::DEBUG_LABEL: return "debug_label"; 2410 2411 case ISD::CONDCODE: 2412 switch (cast<CondCodeSDNode>(this)->get()) { 2413 default: assert(0 && "Unknown setcc condition!"); 2414 case ISD::SETOEQ: return "setoeq"; 2415 case ISD::SETOGT: return "setogt"; 2416 case ISD::SETOGE: return "setoge"; 2417 case ISD::SETOLT: return "setolt"; 2418 case ISD::SETOLE: return "setole"; 2419 case ISD::SETONE: return "setone"; 2420 2421 case ISD::SETO: return "seto"; 2422 case ISD::SETUO: return "setuo"; 2423 case ISD::SETUEQ: return "setue"; 2424 case ISD::SETUGT: return "setugt"; 2425 case ISD::SETUGE: return "setuge"; 2426 case ISD::SETULT: return "setult"; 2427 case ISD::SETULE: return "setule"; 2428 case ISD::SETUNE: return "setune"; 2429 2430 case ISD::SETEQ: return "seteq"; 2431 case ISD::SETGT: return "setgt"; 2432 case ISD::SETGE: return "setge"; 2433 case ISD::SETLT: return "setlt"; 2434 case ISD::SETLE: return "setle"; 2435 case ISD::SETNE: return "setne"; 2436 } 2437 } 2438} 2439 2440void SDNode::dump() const { dump(0); } 2441void SDNode::dump(const SelectionDAG *G) const { 2442 std::cerr << (void*)this << ": "; 2443 2444 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 2445 if (i) std::cerr << ","; 2446 if (getValueType(i) == MVT::Other) 2447 std::cerr << "ch"; 2448 else 2449 std::cerr << MVT::getValueTypeString(getValueType(i)); 2450 } 2451 std::cerr << " = " << getOperationName(G); 2452 2453 std::cerr << " "; 2454 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 2455 if (i) std::cerr << ", "; 2456 std::cerr << (void*)getOperand(i).Val; 2457 if (unsigned RN = getOperand(i).ResNo) 2458 std::cerr << ":" << RN; 2459 } 2460 2461 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 2462 std::cerr << "<" << CSDN->getValue() << ">"; 2463 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 2464 std::cerr << "<" << CSDN->getValue() << ">"; 2465 } else if (const GlobalAddressSDNode *GADN = 2466 dyn_cast<GlobalAddressSDNode>(this)) { 2467 int offset = GADN->getOffset(); 2468 std::cerr << "<"; 2469 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; 2470 if (offset > 0) 2471 std::cerr << " + " << offset; 2472 else 2473 std::cerr << " " << offset; 2474 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 2475 std::cerr << "<" << FIDN->getIndex() << ">"; 2476 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 2477 std::cerr << "<" << *CP->get() << ">"; 2478 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 2479 std::cerr << "<"; 2480 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 2481 if (LBB) 2482 std::cerr << LBB->getName() << " "; 2483 std::cerr << (const void*)BBDN->getBasicBlock() << ">"; 2484 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 2485 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) { 2486 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); 2487 } else { 2488 std::cerr << " #" << R->getReg(); 2489 } 2490 } else if (const ExternalSymbolSDNode *ES = 2491 dyn_cast<ExternalSymbolSDNode>(this)) { 2492 std::cerr << "'" << ES->getSymbol() << "'"; 2493 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 2494 if (M->getValue()) 2495 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; 2496 else 2497 std::cerr << "<null:" << M->getOffset() << ">"; 2498 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 2499 std::cerr << ":" << getValueTypeString(N->getVT()); 2500 } 2501} 2502 2503static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 2504 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 2505 if (N->getOperand(i).Val->hasOneUse()) 2506 DumpNodes(N->getOperand(i).Val, indent+2, G); 2507 else 2508 std::cerr << "\n" << std::string(indent+2, ' ') 2509 << (void*)N->getOperand(i).Val << ": <multiple use>"; 2510 2511 2512 std::cerr << "\n" << std::string(indent, ' '); 2513 N->dump(G); 2514} 2515 2516void SelectionDAG::dump() const { 2517 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; 2518 std::vector<const SDNode*> Nodes; 2519 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 2520 I != E; ++I) 2521 Nodes.push_back(I); 2522 2523 std::sort(Nodes.begin(), Nodes.end()); 2524 2525 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 2526 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) 2527 DumpNodes(Nodes[i], 2, this); 2528 } 2529 2530 DumpNodes(getRoot().Val, 2, this); 2531 2532 std::cerr << "\n\n"; 2533} 2534 2535