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