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