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