SelectionDAG.cpp revision 81310138619ac46a4eb5ea9a28929c9f5ea8012c
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/GlobalVariable.h" 17#include "llvm/Intrinsics.h" 18#include "llvm/DerivedTypes.h" 19#include "llvm/Assembly/Writer.h" 20#include "llvm/CodeGen/MachineBasicBlock.h" 21#include "llvm/CodeGen/MachineConstantPool.h" 22#include "llvm/CodeGen/MachineFrameInfo.h" 23#include "llvm/Support/MathExtras.h" 24#include "llvm/Target/MRegisterInfo.h" 25#include "llvm/Target/TargetData.h" 26#include "llvm/Target/TargetLowering.h" 27#include "llvm/Target/TargetInstrInfo.h" 28#include "llvm/Target/TargetMachine.h" 29#include "llvm/ADT/SetVector.h" 30#include "llvm/ADT/SmallPtrSet.h" 31#include "llvm/ADT/SmallSet.h" 32#include "llvm/ADT/SmallVector.h" 33#include "llvm/ADT/StringExtras.h" 34#include <algorithm> 35#include <cmath> 36using namespace llvm; 37 38/// makeVTList - Return an instance of the SDVTList struct initialized with the 39/// specified members. 40static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) { 41 SDVTList Res = {VTs, NumVTs}; 42 return Res; 43} 44 45//===----------------------------------------------------------------------===// 46// ConstantFPSDNode Class 47//===----------------------------------------------------------------------===// 48 49/// isExactlyValue - We don't rely on operator== working on double values, as 50/// it returns true for things that are clearly not equal, like -0.0 and 0.0. 51/// As such, this method can be used to do an exact bit-for-bit comparison of 52/// two floating point values. 53bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const { 54 return Value.bitwiseIsEqual(V); 55} 56 57bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT, 58 const APFloat& Val) { 59 // convert modifies in place, so make a copy. 60 APFloat Val2 = APFloat(Val); 61 switch (VT) { 62 default: 63 return false; // These can't be represented as floating point! 64 65 // FIXME rounding mode needs to be more flexible 66 case MVT::f32: 67 return &Val2.getSemantics() == &APFloat::IEEEsingle || 68 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) == 69 APFloat::opOK; 70 case MVT::f64: 71 return &Val2.getSemantics() == &APFloat::IEEEsingle || 72 &Val2.getSemantics() == &APFloat::IEEEdouble || 73 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) == 74 APFloat::opOK; 75 // TODO: Figure out how to test if we can use a shorter type instead! 76 case MVT::f80: 77 case MVT::f128: 78 case MVT::ppcf128: 79 return true; 80 } 81} 82 83//===----------------------------------------------------------------------===// 84// ISD Namespace 85//===----------------------------------------------------------------------===// 86 87/// isBuildVectorAllOnes - Return true if the specified node is a 88/// BUILD_VECTOR where all of the elements are ~0 or undef. 89bool ISD::isBuildVectorAllOnes(const SDNode *N) { 90 // Look through a bit convert. 91 if (N->getOpcode() == ISD::BIT_CONVERT) 92 N = N->getOperand(0).Val; 93 94 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 95 96 unsigned i = 0, e = N->getNumOperands(); 97 98 // Skip over all of the undef values. 99 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 100 ++i; 101 102 // Do not accept an all-undef vector. 103 if (i == e) return false; 104 105 // Do not accept build_vectors that aren't all constants or which have non-~0 106 // elements. 107 SDOperand NotZero = N->getOperand(i); 108 if (isa<ConstantSDNode>(NotZero)) { 109 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue()) 110 return false; 111 } else if (isa<ConstantFPSDNode>(NotZero)) { 112 MVT::ValueType VT = NotZero.getValueType(); 113 if (VT== MVT::f64) { 114 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF(). 115 convertToAPInt().getZExtValue())) != (uint64_t)-1) 116 return false; 117 } else { 118 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)-> 119 getValueAPF().convertToAPInt().getZExtValue() != 120 (uint32_t)-1) 121 return false; 122 } 123 } else 124 return false; 125 126 // Okay, we have at least one ~0 value, check to see if the rest match or are 127 // undefs. 128 for (++i; i != e; ++i) 129 if (N->getOperand(i) != NotZero && 130 N->getOperand(i).getOpcode() != ISD::UNDEF) 131 return false; 132 return true; 133} 134 135 136/// isBuildVectorAllZeros - Return true if the specified node is a 137/// BUILD_VECTOR where all of the elements are 0 or undef. 138bool ISD::isBuildVectorAllZeros(const SDNode *N) { 139 // Look through a bit convert. 140 if (N->getOpcode() == ISD::BIT_CONVERT) 141 N = N->getOperand(0).Val; 142 143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false; 144 145 unsigned i = 0, e = N->getNumOperands(); 146 147 // Skip over all of the undef values. 148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) 149 ++i; 150 151 // Do not accept an all-undef vector. 152 if (i == e) return false; 153 154 // Do not accept build_vectors that aren't all constants or which have non-~0 155 // elements. 156 SDOperand Zero = N->getOperand(i); 157 if (isa<ConstantSDNode>(Zero)) { 158 if (!cast<ConstantSDNode>(Zero)->isNullValue()) 159 return false; 160 } else if (isa<ConstantFPSDNode>(Zero)) { 161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero()) 162 return false; 163 } else 164 return false; 165 166 // Okay, we have at least one ~0 value, check to see if the rest match or are 167 // undefs. 168 for (++i; i != e; ++i) 169 if (N->getOperand(i) != Zero && 170 N->getOperand(i).getOpcode() != ISD::UNDEF) 171 return false; 172 return true; 173} 174 175/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) 176/// when given the operation for (X op Y). 177ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { 178 // To perform this operation, we just need to swap the L and G bits of the 179 // operation. 180 unsigned OldL = (Operation >> 2) & 1; 181 unsigned OldG = (Operation >> 1) & 1; 182 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits 183 (OldL << 1) | // New G bit 184 (OldG << 2)); // New L bit. 185} 186 187/// getSetCCInverse - Return the operation corresponding to !(X op Y), where 188/// 'op' is a valid SetCC operation. 189ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { 190 unsigned Operation = Op; 191 if (isInteger) 192 Operation ^= 7; // Flip L, G, E bits, but not U. 193 else 194 Operation ^= 15; // Flip all of the condition bits. 195 if (Operation > ISD::SETTRUE2) 196 Operation &= ~8; // Don't let N and U bits get set. 197 return ISD::CondCode(Operation); 198} 199 200 201/// isSignedOp - For an integer comparison, return 1 if the comparison is a 202/// signed operation and 2 if the result is an unsigned comparison. Return zero 203/// if the operation does not depend on the sign of the input (setne and seteq). 204static int isSignedOp(ISD::CondCode Opcode) { 205 switch (Opcode) { 206 default: assert(0 && "Illegal integer setcc operation!"); 207 case ISD::SETEQ: 208 case ISD::SETNE: return 0; 209 case ISD::SETLT: 210 case ISD::SETLE: 211 case ISD::SETGT: 212 case ISD::SETGE: return 1; 213 case ISD::SETULT: 214 case ISD::SETULE: 215 case ISD::SETUGT: 216 case ISD::SETUGE: return 2; 217 } 218} 219 220/// getSetCCOrOperation - Return the result of a logical OR between different 221/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function 222/// returns SETCC_INVALID if it is not possible to represent the resultant 223/// comparison. 224ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, 225 bool isInteger) { 226 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 227 // Cannot fold a signed integer setcc with an unsigned integer setcc. 228 return ISD::SETCC_INVALID; 229 230 unsigned Op = Op1 | Op2; // Combine all of the condition bits. 231 232 // If the N and U bits get set then the resultant comparison DOES suddenly 233 // care about orderedness, and is true when ordered. 234 if (Op > ISD::SETTRUE2) 235 Op &= ~16; // Clear the U bit if the N bit is set. 236 237 // Canonicalize illegal integer setcc's. 238 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT 239 Op = ISD::SETNE; 240 241 return ISD::CondCode(Op); 242} 243 244/// getSetCCAndOperation - Return the result of a logical AND between different 245/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This 246/// function returns zero if it is not possible to represent the resultant 247/// comparison. 248ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, 249 bool isInteger) { 250 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) 251 // Cannot fold a signed setcc with an unsigned setcc. 252 return ISD::SETCC_INVALID; 253 254 // Combine all of the condition bits. 255 ISD::CondCode Result = ISD::CondCode(Op1 & Op2); 256 257 // Canonicalize illegal integer setcc's. 258 if (isInteger) { 259 switch (Result) { 260 default: break; 261 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT 262 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE 263 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE 264 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE 265 } 266 } 267 268 return Result; 269} 270 271const TargetMachine &SelectionDAG::getTarget() const { 272 return TLI.getTargetMachine(); 273} 274 275//===----------------------------------------------------------------------===// 276// SDNode Profile Support 277//===----------------------------------------------------------------------===// 278 279/// AddNodeIDOpcode - Add the node opcode to the NodeID data. 280/// 281static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) { 282 ID.AddInteger(OpC); 283} 284 285/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them 286/// solely with their pointer. 287void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) { 288 ID.AddPointer(VTList.VTs); 289} 290 291/// AddNodeIDOperands - Various routines for adding operands to the NodeID data. 292/// 293static void AddNodeIDOperands(FoldingSetNodeID &ID, 294 const SDOperand *Ops, unsigned NumOps) { 295 for (; NumOps; --NumOps, ++Ops) { 296 ID.AddPointer(Ops->Val); 297 ID.AddInteger(Ops->ResNo); 298 } 299} 300 301static void AddNodeIDNode(FoldingSetNodeID &ID, 302 unsigned short OpC, SDVTList VTList, 303 const SDOperand *OpList, unsigned N) { 304 AddNodeIDOpcode(ID, OpC); 305 AddNodeIDValueTypes(ID, VTList); 306 AddNodeIDOperands(ID, OpList, N); 307} 308 309/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID 310/// data. 311static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) { 312 AddNodeIDOpcode(ID, N->getOpcode()); 313 // Add the return value info. 314 AddNodeIDValueTypes(ID, N->getVTList()); 315 // Add the operand info. 316 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands()); 317 318 // Handle SDNode leafs with special info. 319 switch (N->getOpcode()) { 320 default: break; // Normal nodes don't need extra info. 321 case ISD::TargetConstant: 322 case ISD::Constant: 323 ID.AddInteger(cast<ConstantSDNode>(N)->getValue()); 324 break; 325 case ISD::TargetConstantFP: 326 case ISD::ConstantFP: { 327 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF()); 328 break; 329 } 330 case ISD::TargetGlobalAddress: 331 case ISD::GlobalAddress: 332 case ISD::TargetGlobalTLSAddress: 333 case ISD::GlobalTLSAddress: { 334 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); 335 ID.AddPointer(GA->getGlobal()); 336 ID.AddInteger(GA->getOffset()); 337 break; 338 } 339 case ISD::BasicBlock: 340 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock()); 341 break; 342 case ISD::Register: 343 ID.AddInteger(cast<RegisterSDNode>(N)->getReg()); 344 break; 345 case ISD::SRCVALUE: { 346 SrcValueSDNode *SV = cast<SrcValueSDNode>(N); 347 ID.AddPointer(SV->getValue()); 348 ID.AddInteger(SV->getOffset()); 349 break; 350 } 351 case ISD::FrameIndex: 352 case ISD::TargetFrameIndex: 353 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex()); 354 break; 355 case ISD::JumpTable: 356 case ISD::TargetJumpTable: 357 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex()); 358 break; 359 case ISD::ConstantPool: 360 case ISD::TargetConstantPool: { 361 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N); 362 ID.AddInteger(CP->getAlignment()); 363 ID.AddInteger(CP->getOffset()); 364 if (CP->isMachineConstantPoolEntry()) 365 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID); 366 else 367 ID.AddPointer(CP->getConstVal()); 368 break; 369 } 370 case ISD::LOAD: { 371 LoadSDNode *LD = cast<LoadSDNode>(N); 372 ID.AddInteger(LD->getAddressingMode()); 373 ID.AddInteger(LD->getExtensionType()); 374 ID.AddInteger((unsigned int)(LD->getLoadedVT())); 375 ID.AddPointer(LD->getSrcValue()); 376 ID.AddInteger(LD->getSrcValueOffset()); 377 ID.AddInteger(LD->getAlignment()); 378 ID.AddInteger(LD->isVolatile()); 379 break; 380 } 381 case ISD::STORE: { 382 StoreSDNode *ST = cast<StoreSDNode>(N); 383 ID.AddInteger(ST->getAddressingMode()); 384 ID.AddInteger(ST->isTruncatingStore()); 385 ID.AddInteger((unsigned int)(ST->getStoredVT())); 386 ID.AddPointer(ST->getSrcValue()); 387 ID.AddInteger(ST->getSrcValueOffset()); 388 ID.AddInteger(ST->getAlignment()); 389 ID.AddInteger(ST->isVolatile()); 390 break; 391 } 392 } 393} 394 395//===----------------------------------------------------------------------===// 396// SelectionDAG Class 397//===----------------------------------------------------------------------===// 398 399/// RemoveDeadNodes - This method deletes all unreachable nodes in the 400/// SelectionDAG. 401void SelectionDAG::RemoveDeadNodes() { 402 // Create a dummy node (which is not added to allnodes), that adds a reference 403 // to the root node, preventing it from being deleted. 404 HandleSDNode Dummy(getRoot()); 405 406 SmallVector<SDNode*, 128> DeadNodes; 407 408 // Add all obviously-dead nodes to the DeadNodes worklist. 409 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) 410 if (I->use_empty()) 411 DeadNodes.push_back(I); 412 413 // Process the worklist, deleting the nodes and adding their uses to the 414 // worklist. 415 while (!DeadNodes.empty()) { 416 SDNode *N = DeadNodes.back(); 417 DeadNodes.pop_back(); 418 419 // Take the node out of the appropriate CSE map. 420 RemoveNodeFromCSEMaps(N); 421 422 // Next, brutally remove the operand list. This is safe to do, as there are 423 // no cycles in the graph. 424 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 425 SDNode *Operand = I->Val; 426 Operand->removeUser(N); 427 428 // Now that we removed this operand, see if there are no uses of it left. 429 if (Operand->use_empty()) 430 DeadNodes.push_back(Operand); 431 } 432 if (N->OperandsNeedDelete) 433 delete[] N->OperandList; 434 N->OperandList = 0; 435 N->NumOperands = 0; 436 437 // Finally, remove N itself. 438 AllNodes.erase(N); 439 } 440 441 // If the root changed (e.g. it was a dead load, update the root). 442 setRoot(Dummy.getValue()); 443} 444 445void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) { 446 SmallVector<SDNode*, 16> DeadNodes; 447 DeadNodes.push_back(N); 448 449 // Process the worklist, deleting the nodes and adding their uses to the 450 // worklist. 451 while (!DeadNodes.empty()) { 452 SDNode *N = DeadNodes.back(); 453 DeadNodes.pop_back(); 454 455 // Take the node out of the appropriate CSE map. 456 RemoveNodeFromCSEMaps(N); 457 458 // Next, brutally remove the operand list. This is safe to do, as there are 459 // no cycles in the graph. 460 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 461 SDNode *Operand = I->Val; 462 Operand->removeUser(N); 463 464 // Now that we removed this operand, see if there are no uses of it left. 465 if (Operand->use_empty()) 466 DeadNodes.push_back(Operand); 467 } 468 if (N->OperandsNeedDelete) 469 delete[] N->OperandList; 470 N->OperandList = 0; 471 N->NumOperands = 0; 472 473 // Finally, remove N itself. 474 Deleted.push_back(N); 475 AllNodes.erase(N); 476 } 477} 478 479void SelectionDAG::DeleteNode(SDNode *N) { 480 assert(N->use_empty() && "Cannot delete a node that is not dead!"); 481 482 // First take this out of the appropriate CSE map. 483 RemoveNodeFromCSEMaps(N); 484 485 // Finally, remove uses due to operands of this node, remove from the 486 // AllNodes list, and delete the node. 487 DeleteNodeNotInCSEMaps(N); 488} 489 490void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { 491 492 // Remove it from the AllNodes list. 493 AllNodes.remove(N); 494 495 // Drop all of the operands and decrement used nodes use counts. 496 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) 497 I->Val->removeUser(N); 498 if (N->OperandsNeedDelete) 499 delete[] N->OperandList; 500 N->OperandList = 0; 501 N->NumOperands = 0; 502 503 delete N; 504} 505 506/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that 507/// correspond to it. This is useful when we're about to delete or repurpose 508/// the node. We don't want future request for structurally identical nodes 509/// to return N anymore. 510void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { 511 bool Erased = false; 512 switch (N->getOpcode()) { 513 case ISD::HANDLENODE: return; // noop. 514 case ISD::STRING: 515 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); 516 break; 517 case ISD::CONDCODE: 518 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && 519 "Cond code doesn't exist!"); 520 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; 521 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; 522 break; 523 case ISD::ExternalSymbol: 524 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 525 break; 526 case ISD::TargetExternalSymbol: 527 Erased = 528 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); 529 break; 530 case ISD::VALUETYPE: { 531 MVT::ValueType VT = cast<VTSDNode>(N)->getVT(); 532 if (MVT::isExtendedVT(VT)) { 533 Erased = ExtendedValueTypeNodes.erase(VT); 534 } else { 535 Erased = ValueTypeNodes[VT] != 0; 536 ValueTypeNodes[VT] = 0; 537 } 538 break; 539 } 540 default: 541 // Remove it from the CSE Map. 542 Erased = CSEMap.RemoveNode(N); 543 break; 544 } 545#ifndef NDEBUG 546 // Verify that the node was actually in one of the CSE maps, unless it has a 547 // flag result (which cannot be CSE'd) or is one of the special cases that are 548 // not subject to CSE. 549 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && 550 !N->isTargetOpcode()) { 551 N->dump(this); 552 cerr << "\n"; 553 assert(0 && "Node is not in map!"); 554 } 555#endif 556} 557 558/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It 559/// has been taken out and modified in some way. If the specified node already 560/// exists in the CSE maps, do not modify the maps, but return the existing node 561/// instead. If it doesn't exist, add it and return null. 562/// 563SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { 564 assert(N->getNumOperands() && "This is a leaf node!"); 565 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 566 return 0; // Never add these nodes. 567 568 // Check that remaining values produced are not flags. 569 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 570 if (N->getValueType(i) == MVT::Flag) 571 return 0; // Never CSE anything that produces a flag. 572 573 SDNode *New = CSEMap.GetOrInsertNode(N); 574 if (New != N) return New; // Node already existed. 575 return 0; 576} 577 578/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 579/// were replaced with those specified. If this node is never memoized, 580/// return null, otherwise return a pointer to the slot it would take. If a 581/// node already exists with these operands, the slot will be non-null. 582SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op, 583 void *&InsertPos) { 584 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 585 return 0; // Never add these nodes. 586 587 // Check that remaining values produced are not flags. 588 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 589 if (N->getValueType(i) == MVT::Flag) 590 return 0; // Never CSE anything that produces a flag. 591 592 SDOperand Ops[] = { Op }; 593 FoldingSetNodeID ID; 594 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1); 595 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 596} 597 598/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 599/// were replaced with those specified. If this node is never memoized, 600/// return null, otherwise return a pointer to the slot it would take. If a 601/// node already exists with these operands, the slot will be non-null. 602SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 603 SDOperand Op1, SDOperand Op2, 604 void *&InsertPos) { 605 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 606 return 0; // Never add these nodes. 607 608 // Check that remaining values produced are not flags. 609 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 610 if (N->getValueType(i) == MVT::Flag) 611 return 0; // Never CSE anything that produces a flag. 612 613 SDOperand Ops[] = { Op1, Op2 }; 614 FoldingSetNodeID ID; 615 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2); 616 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 617} 618 619 620/// FindModifiedNodeSlot - Find a slot for the specified node if its operands 621/// were replaced with those specified. If this node is never memoized, 622/// return null, otherwise return a pointer to the slot it would take. If a 623/// node already exists with these operands, the slot will be non-null. 624SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, 625 const SDOperand *Ops,unsigned NumOps, 626 void *&InsertPos) { 627 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) 628 return 0; // Never add these nodes. 629 630 // Check that remaining values produced are not flags. 631 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) 632 if (N->getValueType(i) == MVT::Flag) 633 return 0; // Never CSE anything that produces a flag. 634 635 FoldingSetNodeID ID; 636 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps); 637 638 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 639 ID.AddInteger(LD->getAddressingMode()); 640 ID.AddInteger(LD->getExtensionType()); 641 ID.AddInteger((unsigned int)(LD->getLoadedVT())); 642 ID.AddPointer(LD->getSrcValue()); 643 ID.AddInteger(LD->getSrcValueOffset()); 644 ID.AddInteger(LD->getAlignment()); 645 ID.AddInteger(LD->isVolatile()); 646 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 647 ID.AddInteger(ST->getAddressingMode()); 648 ID.AddInteger(ST->isTruncatingStore()); 649 ID.AddInteger((unsigned int)(ST->getStoredVT())); 650 ID.AddPointer(ST->getSrcValue()); 651 ID.AddInteger(ST->getSrcValueOffset()); 652 ID.AddInteger(ST->getAlignment()); 653 ID.AddInteger(ST->isVolatile()); 654 } 655 656 return CSEMap.FindNodeOrInsertPos(ID, InsertPos); 657} 658 659 660SelectionDAG::~SelectionDAG() { 661 while (!AllNodes.empty()) { 662 SDNode *N = AllNodes.begin(); 663 N->SetNextInBucket(0); 664 if (N->OperandsNeedDelete) 665 delete [] N->OperandList; 666 N->OperandList = 0; 667 N->NumOperands = 0; 668 AllNodes.pop_front(); 669 } 670} 671 672SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { 673 if (Op.getValueType() == VT) return Op; 674 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); 675 return getNode(ISD::AND, Op.getValueType(), Op, 676 getConstant(Imm, Op.getValueType())); 677} 678 679SDOperand SelectionDAG::getString(const std::string &Val) { 680 StringSDNode *&N = StringNodes[Val]; 681 if (!N) { 682 N = new StringSDNode(Val); 683 AllNodes.push_back(N); 684 } 685 return SDOperand(N, 0); 686} 687 688SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) { 689 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); 690 691 MVT::ValueType EltVT = 692 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT; 693 694 // Mask out any bits that are not valid for this constant. 695 Val &= MVT::getIntVTBitMask(EltVT); 696 697 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; 698 FoldingSetNodeID ID; 699 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 700 ID.AddInteger(Val); 701 void *IP = 0; 702 SDNode *N = NULL; 703 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 704 if (!MVT::isVector(VT)) 705 return SDOperand(N, 0); 706 if (!N) { 707 N = new ConstantSDNode(isT, Val, EltVT); 708 CSEMap.InsertNode(N, IP); 709 AllNodes.push_back(N); 710 } 711 712 SDOperand Result(N, 0); 713 if (MVT::isVector(VT)) { 714 SmallVector<SDOperand, 8> Ops; 715 Ops.assign(MVT::getVectorNumElements(VT), Result); 716 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); 717 } 718 return Result; 719} 720 721SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT, 722 bool isTarget) { 723 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); 724 725 MVT::ValueType EltVT = 726 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT; 727 728 // Do the map lookup using the actual bit pattern for the floating point 729 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and 730 // we don't have issues with SNANs. 731 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; 732 FoldingSetNodeID ID; 733 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); 734 ID.AddAPFloat(V); 735 void *IP = 0; 736 SDNode *N = NULL; 737 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) 738 if (!MVT::isVector(VT)) 739 return SDOperand(N, 0); 740 if (!N) { 741 N = new ConstantFPSDNode(isTarget, V, EltVT); 742 CSEMap.InsertNode(N, IP); 743 AllNodes.push_back(N); 744 } 745 746 SDOperand Result(N, 0); 747 if (MVT::isVector(VT)) { 748 SmallVector<SDOperand, 8> Ops; 749 Ops.assign(MVT::getVectorNumElements(VT), Result); 750 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); 751 } 752 return Result; 753} 754 755SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT, 756 bool isTarget) { 757 MVT::ValueType EltVT = 758 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT; 759 if (EltVT==MVT::f32) 760 return getConstantFP(APFloat((float)Val), VT, isTarget); 761 else 762 return getConstantFP(APFloat(Val), VT, isTarget); 763} 764 765SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, 766 MVT::ValueType VT, int Offset, 767 bool isTargetGA) { 768 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 769 unsigned Opc; 770 if (GVar && GVar->isThreadLocal()) 771 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress; 772 else 773 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; 774 FoldingSetNodeID ID; 775 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 776 ID.AddPointer(GV); 777 ID.AddInteger(Offset); 778 void *IP = 0; 779 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 780 return SDOperand(E, 0); 781 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset); 782 CSEMap.InsertNode(N, IP); 783 AllNodes.push_back(N); 784 return SDOperand(N, 0); 785} 786 787SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT, 788 bool isTarget) { 789 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; 790 FoldingSetNodeID ID; 791 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 792 ID.AddInteger(FI); 793 void *IP = 0; 794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 795 return SDOperand(E, 0); 796 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget); 797 CSEMap.InsertNode(N, IP); 798 AllNodes.push_back(N); 799 return SDOperand(N, 0); 800} 801 802SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){ 803 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; 804 FoldingSetNodeID ID; 805 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 806 ID.AddInteger(JTI); 807 void *IP = 0; 808 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 809 return SDOperand(E, 0); 810 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget); 811 CSEMap.InsertNode(N, IP); 812 AllNodes.push_back(N); 813 return SDOperand(N, 0); 814} 815 816SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT, 817 unsigned Alignment, int Offset, 818 bool isTarget) { 819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 820 FoldingSetNodeID ID; 821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 822 ID.AddInteger(Alignment); 823 ID.AddInteger(Offset); 824 ID.AddPointer(C); 825 void *IP = 0; 826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 827 return SDOperand(E, 0); 828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment); 829 CSEMap.InsertNode(N, IP); 830 AllNodes.push_back(N); 831 return SDOperand(N, 0); 832} 833 834 835SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, 836 MVT::ValueType VT, 837 unsigned Alignment, int Offset, 838 bool isTarget) { 839 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; 840 FoldingSetNodeID ID; 841 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); 842 ID.AddInteger(Alignment); 843 ID.AddInteger(Offset); 844 C->AddSelectionDAGCSEId(ID); 845 void *IP = 0; 846 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 847 return SDOperand(E, 0); 848 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment); 849 CSEMap.InsertNode(N, IP); 850 AllNodes.push_back(N); 851 return SDOperand(N, 0); 852} 853 854 855SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { 856 FoldingSetNodeID ID; 857 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0); 858 ID.AddPointer(MBB); 859 void *IP = 0; 860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 861 return SDOperand(E, 0); 862 SDNode *N = new BasicBlockSDNode(MBB); 863 CSEMap.InsertNode(N, IP); 864 AllNodes.push_back(N); 865 return SDOperand(N, 0); 866} 867 868SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { 869 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size()) 870 ValueTypeNodes.resize(VT+1); 871 872 SDNode *&N = MVT::isExtendedVT(VT) ? 873 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT]; 874 875 if (N) return SDOperand(N, 0); 876 N = new VTSDNode(VT); 877 AllNodes.push_back(N); 878 return SDOperand(N, 0); 879} 880 881SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { 882 SDNode *&N = ExternalSymbols[Sym]; 883 if (N) return SDOperand(N, 0); 884 N = new ExternalSymbolSDNode(false, Sym, VT); 885 AllNodes.push_back(N); 886 return SDOperand(N, 0); 887} 888 889SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, 890 MVT::ValueType VT) { 891 SDNode *&N = TargetExternalSymbols[Sym]; 892 if (N) return SDOperand(N, 0); 893 N = new ExternalSymbolSDNode(true, Sym, VT); 894 AllNodes.push_back(N); 895 return SDOperand(N, 0); 896} 897 898SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { 899 if ((unsigned)Cond >= CondCodeNodes.size()) 900 CondCodeNodes.resize(Cond+1); 901 902 if (CondCodeNodes[Cond] == 0) { 903 CondCodeNodes[Cond] = new CondCodeSDNode(Cond); 904 AllNodes.push_back(CondCodeNodes[Cond]); 905 } 906 return SDOperand(CondCodeNodes[Cond], 0); 907} 908 909SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { 910 FoldingSetNodeID ID; 911 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0); 912 ID.AddInteger(RegNo); 913 void *IP = 0; 914 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 915 return SDOperand(E, 0); 916 SDNode *N = new RegisterSDNode(RegNo, VT); 917 CSEMap.InsertNode(N, IP); 918 AllNodes.push_back(N); 919 return SDOperand(N, 0); 920} 921 922SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { 923 assert((!V || isa<PointerType>(V->getType())) && 924 "SrcValue is not a pointer?"); 925 926 FoldingSetNodeID ID; 927 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0); 928 ID.AddPointer(V); 929 ID.AddInteger(Offset); 930 void *IP = 0; 931 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 932 return SDOperand(E, 0); 933 SDNode *N = new SrcValueSDNode(V, Offset); 934 CSEMap.InsertNode(N, IP); 935 AllNodes.push_back(N); 936 return SDOperand(N, 0); 937} 938 939/// CreateStackTemporary - Create a stack temporary, suitable for holding the 940/// specified value type. 941SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) { 942 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); 943 unsigned ByteSize = MVT::getSizeInBits(VT)/8; 944 const Type *Ty = MVT::getTypeForValueType(VT); 945 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty); 946 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign); 947 return getFrameIndex(FrameIdx, TLI.getPointerTy()); 948} 949 950 951SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1, 952 SDOperand N2, ISD::CondCode Cond) { 953 // These setcc operations always fold. 954 switch (Cond) { 955 default: break; 956 case ISD::SETFALSE: 957 case ISD::SETFALSE2: return getConstant(0, VT); 958 case ISD::SETTRUE: 959 case ISD::SETTRUE2: return getConstant(1, VT); 960 961 case ISD::SETOEQ: 962 case ISD::SETOGT: 963 case ISD::SETOGE: 964 case ISD::SETOLT: 965 case ISD::SETOLE: 966 case ISD::SETONE: 967 case ISD::SETO: 968 case ISD::SETUO: 969 case ISD::SETUEQ: 970 case ISD::SETUNE: 971 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!"); 972 break; 973 } 974 975 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { 976 uint64_t C2 = N2C->getValue(); 977 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { 978 uint64_t C1 = N1C->getValue(); 979 980 // Sign extend the operands if required 981 if (ISD::isSignedIntSetCC(Cond)) { 982 C1 = N1C->getSignExtended(); 983 C2 = N2C->getSignExtended(); 984 } 985 986 switch (Cond) { 987 default: assert(0 && "Unknown integer setcc!"); 988 case ISD::SETEQ: return getConstant(C1 == C2, VT); 989 case ISD::SETNE: return getConstant(C1 != C2, VT); 990 case ISD::SETULT: return getConstant(C1 < C2, VT); 991 case ISD::SETUGT: return getConstant(C1 > C2, VT); 992 case ISD::SETULE: return getConstant(C1 <= C2, VT); 993 case ISD::SETUGE: return getConstant(C1 >= C2, VT); 994 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); 995 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); 996 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); 997 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); 998 } 999 } 1000 } 1001 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) 1002 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { 1003 // No compile time operations on this type yet. 1004 if (N1C->getValueType(0) == MVT::ppcf128) 1005 return SDOperand(); 1006 1007 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF()); 1008 switch (Cond) { 1009 default: break; 1010 case ISD::SETEQ: if (R==APFloat::cmpUnordered) 1011 return getNode(ISD::UNDEF, VT); 1012 // fall through 1013 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT); 1014 case ISD::SETNE: if (R==APFloat::cmpUnordered) 1015 return getNode(ISD::UNDEF, VT); 1016 // fall through 1017 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan || 1018 R==APFloat::cmpLessThan, VT); 1019 case ISD::SETLT: if (R==APFloat::cmpUnordered) 1020 return getNode(ISD::UNDEF, VT); 1021 // fall through 1022 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT); 1023 case ISD::SETGT: if (R==APFloat::cmpUnordered) 1024 return getNode(ISD::UNDEF, VT); 1025 // fall through 1026 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT); 1027 case ISD::SETLE: if (R==APFloat::cmpUnordered) 1028 return getNode(ISD::UNDEF, VT); 1029 // fall through 1030 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan || 1031 R==APFloat::cmpEqual, VT); 1032 case ISD::SETGE: if (R==APFloat::cmpUnordered) 1033 return getNode(ISD::UNDEF, VT); 1034 // fall through 1035 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan || 1036 R==APFloat::cmpEqual, VT); 1037 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT); 1038 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT); 1039 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered || 1040 R==APFloat::cmpEqual, VT); 1041 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT); 1042 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered || 1043 R==APFloat::cmpLessThan, VT); 1044 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan || 1045 R==APFloat::cmpUnordered, VT); 1046 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT); 1047 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT); 1048 } 1049 } else { 1050 // Ensure that the constant occurs on the RHS. 1051 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); 1052 } 1053 1054 // Could not fold it. 1055 return SDOperand(); 1056} 1057 1058/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 1059/// this predicate to simplify operations downstream. Mask is known to be zero 1060/// for bits that V cannot have. 1061bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask, 1062 unsigned Depth) const { 1063 // The masks are not wide enough to represent this type! Should use APInt. 1064 if (Op.getValueType() == MVT::i128) 1065 return false; 1066 1067 uint64_t KnownZero, KnownOne; 1068 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 1069 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1070 return (KnownZero & Mask) == Mask; 1071} 1072 1073/// ComputeMaskedBits - Determine which of the bits specified in Mask are 1074/// known to be either zero or one and return them in the KnownZero/KnownOne 1075/// bitsets. This code only analyzes bits in Mask, in order to short-circuit 1076/// processing. 1077void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask, 1078 uint64_t &KnownZero, uint64_t &KnownOne, 1079 unsigned Depth) const { 1080 KnownZero = KnownOne = 0; // Don't know anything. 1081 if (Depth == 6 || Mask == 0) 1082 return; // Limit search depth. 1083 1084 // The masks are not wide enough to represent this type! Should use APInt. 1085 if (Op.getValueType() == MVT::i128) 1086 return; 1087 1088 uint64_t KnownZero2, KnownOne2; 1089 1090 switch (Op.getOpcode()) { 1091 case ISD::Constant: 1092 // We know all of the bits for a constant! 1093 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask; 1094 KnownZero = ~KnownOne & Mask; 1095 return; 1096 case ISD::AND: 1097 // If either the LHS or the RHS are Zero, the result is zero. 1098 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1099 Mask &= ~KnownZero; 1100 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1101 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1102 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1103 1104 // Output known-1 bits are only known if set in both the LHS & RHS. 1105 KnownOne &= KnownOne2; 1106 // Output known-0 are known to be clear if zero in either the LHS | RHS. 1107 KnownZero |= KnownZero2; 1108 return; 1109 case ISD::OR: 1110 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1111 Mask &= ~KnownOne; 1112 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1113 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1114 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1115 1116 // Output known-0 bits are only known if clear in both the LHS & RHS. 1117 KnownZero &= KnownZero2; 1118 // Output known-1 are known to be set if set in either the LHS | RHS. 1119 KnownOne |= KnownOne2; 1120 return; 1121 case ISD::XOR: { 1122 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1123 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1124 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1125 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1126 1127 // Output known-0 bits are known if clear or set in both the LHS & RHS. 1128 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 1129 // Output known-1 are known to be set if set in only one of the LHS, RHS. 1130 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 1131 KnownZero = KnownZeroOut; 1132 return; 1133 } 1134 case ISD::SELECT: 1135 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1); 1136 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); 1137 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1138 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1139 1140 // Only known if known in both the LHS and RHS. 1141 KnownOne &= KnownOne2; 1142 KnownZero &= KnownZero2; 1143 return; 1144 case ISD::SELECT_CC: 1145 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1); 1146 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1); 1147 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1148 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1149 1150 // Only known if known in both the LHS and RHS. 1151 KnownOne &= KnownOne2; 1152 KnownZero &= KnownZero2; 1153 return; 1154 case ISD::SETCC: 1155 // If we know the result of a setcc has the top bits zero, use this info. 1156 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) 1157 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); 1158 return; 1159 case ISD::SHL: 1160 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 1161 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1162 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(), 1163 KnownZero, KnownOne, Depth+1); 1164 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1165 KnownZero <<= SA->getValue(); 1166 KnownOne <<= SA->getValue(); 1167 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero. 1168 } 1169 return; 1170 case ISD::SRL: 1171 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 1172 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1173 MVT::ValueType VT = Op.getValueType(); 1174 unsigned ShAmt = SA->getValue(); 1175 1176 uint64_t TypeMask = MVT::getIntVTBitMask(VT); 1177 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask, 1178 KnownZero, KnownOne, Depth+1); 1179 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1180 KnownZero &= TypeMask; 1181 KnownOne &= TypeMask; 1182 KnownZero >>= ShAmt; 1183 KnownOne >>= ShAmt; 1184 1185 uint64_t HighBits = (1ULL << ShAmt)-1; 1186 HighBits <<= MVT::getSizeInBits(VT)-ShAmt; 1187 KnownZero |= HighBits; // High bits known zero. 1188 } 1189 return; 1190 case ISD::SRA: 1191 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1192 MVT::ValueType VT = Op.getValueType(); 1193 unsigned ShAmt = SA->getValue(); 1194 1195 // Compute the new bits that are at the top now. 1196 uint64_t TypeMask = MVT::getIntVTBitMask(VT); 1197 1198 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask; 1199 // If any of the demanded bits are produced by the sign extension, we also 1200 // demand the input sign bit. 1201 uint64_t HighBits = (1ULL << ShAmt)-1; 1202 HighBits <<= MVT::getSizeInBits(VT) - ShAmt; 1203 if (HighBits & Mask) 1204 InDemandedMask |= MVT::getIntVTSignBit(VT); 1205 1206 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne, 1207 Depth+1); 1208 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1209 KnownZero &= TypeMask; 1210 KnownOne &= TypeMask; 1211 KnownZero >>= ShAmt; 1212 KnownOne >>= ShAmt; 1213 1214 // Handle the sign bits. 1215 uint64_t SignBit = MVT::getIntVTSignBit(VT); 1216 SignBit >>= ShAmt; // Adjust to where it is now in the mask. 1217 1218 if (KnownZero & SignBit) { 1219 KnownZero |= HighBits; // New bits are known zero. 1220 } else if (KnownOne & SignBit) { 1221 KnownOne |= HighBits; // New bits are known one. 1222 } 1223 } 1224 return; 1225 case ISD::SIGN_EXTEND_INREG: { 1226 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1227 1228 // Sign extension. Compute the demanded bits in the result that are not 1229 // present in the input. 1230 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask; 1231 1232 uint64_t InSignBit = MVT::getIntVTSignBit(EVT); 1233 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT); 1234 1235 // If the sign extended bits are demanded, we know that the sign 1236 // bit is demanded. 1237 if (NewBits) 1238 InputDemandedBits |= InSignBit; 1239 1240 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits, 1241 KnownZero, KnownOne, Depth+1); 1242 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1243 1244 // If the sign bit of the input is known set or clear, then we know the 1245 // top bits of the result. 1246 if (KnownZero & InSignBit) { // Input sign bit known clear 1247 KnownZero |= NewBits; 1248 KnownOne &= ~NewBits; 1249 } else if (KnownOne & InSignBit) { // Input sign bit known set 1250 KnownOne |= NewBits; 1251 KnownZero &= ~NewBits; 1252 } else { // Input sign bit unknown 1253 KnownZero &= ~NewBits; 1254 KnownOne &= ~NewBits; 1255 } 1256 return; 1257 } 1258 case ISD::CTTZ: 1259 case ISD::CTLZ: 1260 case ISD::CTPOP: { 1261 MVT::ValueType VT = Op.getValueType(); 1262 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1; 1263 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT); 1264 KnownOne = 0; 1265 return; 1266 } 1267 case ISD::LOAD: { 1268 if (ISD::isZEXTLoad(Op.Val)) { 1269 LoadSDNode *LD = cast<LoadSDNode>(Op); 1270 MVT::ValueType VT = LD->getLoadedVT(); 1271 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask; 1272 } 1273 return; 1274 } 1275 case ISD::ZERO_EXTEND: { 1276 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType()); 1277 uint64_t NewBits = (~InMask) & Mask; 1278 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 1279 KnownOne, Depth+1); 1280 KnownZero |= NewBits & Mask; 1281 KnownOne &= ~NewBits; 1282 return; 1283 } 1284 case ISD::SIGN_EXTEND: { 1285 MVT::ValueType InVT = Op.getOperand(0).getValueType(); 1286 unsigned InBits = MVT::getSizeInBits(InVT); 1287 uint64_t InMask = MVT::getIntVTBitMask(InVT); 1288 uint64_t InSignBit = 1ULL << (InBits-1); 1289 uint64_t NewBits = (~InMask) & Mask; 1290 uint64_t InDemandedBits = Mask & InMask; 1291 1292 // If any of the sign extended bits are demanded, we know that the sign 1293 // bit is demanded. 1294 if (NewBits & Mask) 1295 InDemandedBits |= InSignBit; 1296 1297 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero, 1298 KnownOne, Depth+1); 1299 // If the sign bit is known zero or one, the top bits match. 1300 if (KnownZero & InSignBit) { 1301 KnownZero |= NewBits; 1302 KnownOne &= ~NewBits; 1303 } else if (KnownOne & InSignBit) { 1304 KnownOne |= NewBits; 1305 KnownZero &= ~NewBits; 1306 } else { // Otherwise, top bits aren't known. 1307 KnownOne &= ~NewBits; 1308 KnownZero &= ~NewBits; 1309 } 1310 return; 1311 } 1312 case ISD::ANY_EXTEND: { 1313 MVT::ValueType VT = Op.getOperand(0).getValueType(); 1314 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT), 1315 KnownZero, KnownOne, Depth+1); 1316 return; 1317 } 1318 case ISD::TRUNCATE: { 1319 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 1320 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1321 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType()); 1322 KnownZero &= OutMask; 1323 KnownOne &= OutMask; 1324 break; 1325 } 1326 case ISD::AssertZext: { 1327 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1328 uint64_t InMask = MVT::getIntVTBitMask(VT); 1329 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 1330 KnownOne, Depth+1); 1331 KnownZero |= (~InMask) & Mask; 1332 return; 1333 } 1334 case ISD::ADD: { 1335 // If either the LHS or the RHS are Zero, the result is zero. 1336 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1337 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); 1338 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1339 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 1340 1341 // Output known-0 bits are known if clear or set in both the low clear bits 1342 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the 1343 // low 3 bits clear. 1344 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero), 1345 CountTrailingZeros_64(~KnownZero2)); 1346 1347 KnownZero = (1ULL << KnownZeroOut) - 1; 1348 KnownOne = 0; 1349 return; 1350 } 1351 case ISD::SUB: { 1352 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)); 1353 if (!CLHS) return; 1354 1355 // We know that the top bits of C-X are clear if X contains less bits 1356 // than C (i.e. no wrap-around can happen). For example, 20-X is 1357 // positive if we can prove that X is >= 0 and < 16. 1358 MVT::ValueType VT = CLHS->getValueType(0); 1359 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear 1360 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1); 1361 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit 1362 MaskV = ~MaskV & MVT::getIntVTBitMask(VT); 1363 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1); 1364 1365 // If all of the MaskV bits are known to be zero, then we know the output 1366 // top bits are zero, because we now know that the output is from [0-C]. 1367 if ((KnownZero & MaskV) == MaskV) { 1368 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue()); 1369 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero. 1370 KnownOne = 0; // No one bits known. 1371 } else { 1372 KnownZero = KnownOne = 0; // Otherwise, nothing known. 1373 } 1374 } 1375 return; 1376 } 1377 default: 1378 // Allow the target to implement this method for its nodes. 1379 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) { 1380 case ISD::INTRINSIC_WO_CHAIN: 1381 case ISD::INTRINSIC_W_CHAIN: 1382 case ISD::INTRINSIC_VOID: 1383 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this); 1384 } 1385 return; 1386 } 1387} 1388 1389/// ComputeNumSignBits - Return the number of times the sign bit of the 1390/// register is replicated into the other bits. We know that at least 1 bit 1391/// is always equal to the sign bit (itself), but other cases can give us 1392/// information. For example, immediately after an "SRA X, 2", we know that 1393/// the top 3 bits are all equal to each other, so we return 3. 1394unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{ 1395 MVT::ValueType VT = Op.getValueType(); 1396 assert(MVT::isInteger(VT) && "Invalid VT!"); 1397 unsigned VTBits = MVT::getSizeInBits(VT); 1398 unsigned Tmp, Tmp2; 1399 1400 if (Depth == 6) 1401 return 1; // Limit search depth. 1402 1403 switch (Op.getOpcode()) { 1404 default: break; 1405 case ISD::AssertSext: 1406 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); 1407 return VTBits-Tmp+1; 1408 case ISD::AssertZext: 1409 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); 1410 return VTBits-Tmp; 1411 1412 case ISD::Constant: { 1413 uint64_t Val = cast<ConstantSDNode>(Op)->getValue(); 1414 // If negative, invert the bits, then look at it. 1415 if (Val & MVT::getIntVTSignBit(VT)) 1416 Val = ~Val; 1417 1418 // Shift the bits so they are the leading bits in the int64_t. 1419 Val <<= 64-VTBits; 1420 1421 // Return # leading zeros. We use 'min' here in case Val was zero before 1422 // shifting. We don't want to return '64' as for an i32 "0". 1423 return std::min(VTBits, CountLeadingZeros_64(Val)); 1424 } 1425 1426 case ISD::SIGN_EXTEND: 1427 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType()); 1428 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp; 1429 1430 case ISD::SIGN_EXTEND_INREG: 1431 // Max of the input and what this extends. 1432 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); 1433 Tmp = VTBits-Tmp+1; 1434 1435 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1436 return std::max(Tmp, Tmp2); 1437 1438 case ISD::SRA: 1439 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1440 // SRA X, C -> adds C sign bits. 1441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1442 Tmp += C->getValue(); 1443 if (Tmp > VTBits) Tmp = VTBits; 1444 } 1445 return Tmp; 1446 case ISD::SHL: 1447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1448 // shl destroys sign bits. 1449 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1450 if (C->getValue() >= VTBits || // Bad shift. 1451 C->getValue() >= Tmp) break; // Shifted all sign bits out. 1452 return Tmp - C->getValue(); 1453 } 1454 break; 1455 case ISD::AND: 1456 case ISD::OR: 1457 case ISD::XOR: // NOT is handled here. 1458 // Logical binary ops preserve the number of sign bits. 1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1460 if (Tmp == 1) return 1; // Early out. 1461 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1462 return std::min(Tmp, Tmp2); 1463 1464 case ISD::SELECT: 1465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1466 if (Tmp == 1) return 1; // Early out. 1467 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1468 return std::min(Tmp, Tmp2); 1469 1470 case ISD::SETCC: 1471 // If setcc returns 0/-1, all bits are sign bits. 1472 if (TLI.getSetCCResultContents() == 1473 TargetLowering::ZeroOrNegativeOneSetCCResult) 1474 return VTBits; 1475 break; 1476 case ISD::ROTL: 1477 case ISD::ROTR: 1478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 1479 unsigned RotAmt = C->getValue() & (VTBits-1); 1480 1481 // Handle rotate right by N like a rotate left by 32-N. 1482 if (Op.getOpcode() == ISD::ROTR) 1483 RotAmt = (VTBits-RotAmt) & (VTBits-1); 1484 1485 // If we aren't rotating out all of the known-in sign bits, return the 1486 // number that are left. This handles rotl(sext(x), 1) for example. 1487 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1488 if (Tmp > RotAmt+1) return Tmp-RotAmt; 1489 } 1490 break; 1491 case ISD::ADD: 1492 // Add can have at most one carry bit. Thus we know that the output 1493 // is, at worst, one more bit than the inputs. 1494 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1495 if (Tmp == 1) return 1; // Early out. 1496 1497 // Special case decrementing a value (ADD X, -1): 1498 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 1499 if (CRHS->isAllOnesValue()) { 1500 uint64_t KnownZero, KnownOne; 1501 uint64_t Mask = MVT::getIntVTBitMask(VT); 1502 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 1503 1504 // If the input is known to be 0 or 1, the output is 0/-1, which is all 1505 // sign bits set. 1506 if ((KnownZero|1) == Mask) 1507 return VTBits; 1508 1509 // If we are subtracting one from a positive number, there is no carry 1510 // out of the result. 1511 if (KnownZero & MVT::getIntVTSignBit(VT)) 1512 return Tmp; 1513 } 1514 1515 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1516 if (Tmp2 == 1) return 1; 1517 return std::min(Tmp, Tmp2)-1; 1518 break; 1519 1520 case ISD::SUB: 1521 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); 1522 if (Tmp2 == 1) return 1; 1523 1524 // Handle NEG. 1525 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) 1526 if (CLHS->getValue() == 0) { 1527 uint64_t KnownZero, KnownOne; 1528 uint64_t Mask = MVT::getIntVTBitMask(VT); 1529 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1); 1530 // If the input is known to be 0 or 1, the output is 0/-1, which is all 1531 // sign bits set. 1532 if ((KnownZero|1) == Mask) 1533 return VTBits; 1534 1535 // If the input is known to be positive (the sign bit is known clear), 1536 // the output of the NEG has the same number of sign bits as the input. 1537 if (KnownZero & MVT::getIntVTSignBit(VT)) 1538 return Tmp2; 1539 1540 // Otherwise, we treat this like a SUB. 1541 } 1542 1543 // Sub can have at most one carry bit. Thus we know that the output 1544 // is, at worst, one more bit than the inputs. 1545 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); 1546 if (Tmp == 1) return 1; // Early out. 1547 return std::min(Tmp, Tmp2)-1; 1548 break; 1549 case ISD::TRUNCATE: 1550 // FIXME: it's tricky to do anything useful for this, but it is an important 1551 // case for targets like X86. 1552 break; 1553 } 1554 1555 // Handle LOADX separately here. EXTLOAD case will fallthrough. 1556 if (Op.getOpcode() == ISD::LOAD) { 1557 LoadSDNode *LD = cast<LoadSDNode>(Op); 1558 unsigned ExtType = LD->getExtensionType(); 1559 switch (ExtType) { 1560 default: break; 1561 case ISD::SEXTLOAD: // '17' bits known 1562 Tmp = MVT::getSizeInBits(LD->getLoadedVT()); 1563 return VTBits-Tmp+1; 1564 case ISD::ZEXTLOAD: // '16' bits known 1565 Tmp = MVT::getSizeInBits(LD->getLoadedVT()); 1566 return VTBits-Tmp; 1567 } 1568 } 1569 1570 // Allow the target to implement this method for its nodes. 1571 if (Op.getOpcode() >= ISD::BUILTIN_OP_END || 1572 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 1573 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 1574 Op.getOpcode() == ISD::INTRINSIC_VOID) { 1575 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth); 1576 if (NumBits > 1) return NumBits; 1577 } 1578 1579 // Finally, if we can prove that the top bits of the result are 0's or 1's, 1580 // use this information. 1581 uint64_t KnownZero, KnownOne; 1582 uint64_t Mask = MVT::getIntVTBitMask(VT); 1583 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth); 1584 1585 uint64_t SignBit = MVT::getIntVTSignBit(VT); 1586 if (KnownZero & SignBit) { // SignBit is 0 1587 Mask = KnownZero; 1588 } else if (KnownOne & SignBit) { // SignBit is 1; 1589 Mask = KnownOne; 1590 } else { 1591 // Nothing known. 1592 return 1; 1593 } 1594 1595 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine 1596 // the number of identical bits in the top of the input value. 1597 Mask ^= ~0ULL; 1598 Mask <<= 64-VTBits; 1599 // Return # leading zeros. We use 'min' here in case Val was zero before 1600 // shifting. We don't want to return '64' as for an i32 "0". 1601 return std::min(VTBits, CountLeadingZeros_64(Mask)); 1602} 1603 1604 1605/// getNode - Gets or creates the specified node. 1606/// 1607SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { 1608 FoldingSetNodeID ID; 1609 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0); 1610 void *IP = 0; 1611 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1612 return SDOperand(E, 0); 1613 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT)); 1614 CSEMap.InsertNode(N, IP); 1615 1616 AllNodes.push_back(N); 1617 return SDOperand(N, 0); 1618} 1619 1620SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1621 SDOperand Operand) { 1622 unsigned Tmp1; 1623 // Constant fold unary operations with an integer constant operand. 1624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { 1625 uint64_t Val = C->getValue(); 1626 switch (Opcode) { 1627 default: break; 1628 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); 1629 case ISD::ANY_EXTEND: 1630 case ISD::ZERO_EXTEND: return getConstant(Val, VT); 1631 case ISD::TRUNCATE: return getConstant(Val, VT); 1632 case ISD::UINT_TO_FP: 1633 case ISD::SINT_TO_FP: { 1634 const uint64_t zero[] = {0, 0}; 1635 // No compile time operations on this type. 1636 if (VT==MVT::ppcf128) 1637 break; 1638 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero)); 1639 (void)apf.convertFromZeroExtendedInteger(&Val, 1640 MVT::getSizeInBits(Operand.getValueType()), 1641 Opcode==ISD::SINT_TO_FP, 1642 APFloat::rmNearestTiesToEven); 1643 return getConstantFP(apf, VT); 1644 } 1645 case ISD::BIT_CONVERT: 1646 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) 1647 return getConstantFP(BitsToFloat(Val), VT); 1648 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) 1649 return getConstantFP(BitsToDouble(Val), VT); 1650 break; 1651 case ISD::BSWAP: 1652 switch(VT) { 1653 default: assert(0 && "Invalid bswap!"); break; 1654 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT); 1655 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT); 1656 case MVT::i64: return getConstant(ByteSwap_64(Val), VT); 1657 } 1658 break; 1659 case ISD::CTPOP: 1660 switch(VT) { 1661 default: assert(0 && "Invalid ctpop!"); break; 1662 case MVT::i1: return getConstant(Val != 0, VT); 1663 case MVT::i8: 1664 Tmp1 = (unsigned)Val & 0xFF; 1665 return getConstant(CountPopulation_32(Tmp1), VT); 1666 case MVT::i16: 1667 Tmp1 = (unsigned)Val & 0xFFFF; 1668 return getConstant(CountPopulation_32(Tmp1), VT); 1669 case MVT::i32: 1670 return getConstant(CountPopulation_32((unsigned)Val), VT); 1671 case MVT::i64: 1672 return getConstant(CountPopulation_64(Val), VT); 1673 } 1674 case ISD::CTLZ: 1675 switch(VT) { 1676 default: assert(0 && "Invalid ctlz!"); break; 1677 case MVT::i1: return getConstant(Val == 0, VT); 1678 case MVT::i8: 1679 Tmp1 = (unsigned)Val & 0xFF; 1680 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT); 1681 case MVT::i16: 1682 Tmp1 = (unsigned)Val & 0xFFFF; 1683 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT); 1684 case MVT::i32: 1685 return getConstant(CountLeadingZeros_32((unsigned)Val), VT); 1686 case MVT::i64: 1687 return getConstant(CountLeadingZeros_64(Val), VT); 1688 } 1689 case ISD::CTTZ: 1690 switch(VT) { 1691 default: assert(0 && "Invalid cttz!"); break; 1692 case MVT::i1: return getConstant(Val == 0, VT); 1693 case MVT::i8: 1694 Tmp1 = (unsigned)Val | 0x100; 1695 return getConstant(CountTrailingZeros_32(Tmp1), VT); 1696 case MVT::i16: 1697 Tmp1 = (unsigned)Val | 0x10000; 1698 return getConstant(CountTrailingZeros_32(Tmp1), VT); 1699 case MVT::i32: 1700 return getConstant(CountTrailingZeros_32((unsigned)Val), VT); 1701 case MVT::i64: 1702 return getConstant(CountTrailingZeros_64(Val), VT); 1703 } 1704 } 1705 } 1706 1707 // Constant fold unary operations with a floating point constant operand. 1708 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) { 1709 APFloat V = C->getValueAPF(); // make copy 1710 if (VT!=MVT::ppcf128 && Operand.getValueType()!=MVT::ppcf128) { 1711 switch (Opcode) { 1712 case ISD::FNEG: 1713 V.changeSign(); 1714 return getConstantFP(V, VT); 1715 case ISD::FABS: 1716 V.clearSign(); 1717 return getConstantFP(V, VT); 1718 case ISD::FP_ROUND: 1719 case ISD::FP_EXTEND: 1720 // This can return overflow, underflow, or inexact; we don't care. 1721 // FIXME need to be more flexible about rounding mode. 1722 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle : 1723 VT==MVT::f64 ? APFloat::IEEEdouble : 1724 VT==MVT::f80 ? APFloat::x87DoubleExtended : 1725 VT==MVT::f128 ? APFloat::IEEEquad : 1726 APFloat::Bogus, 1727 APFloat::rmNearestTiesToEven); 1728 return getConstantFP(V, VT); 1729 case ISD::FP_TO_SINT: 1730 case ISD::FP_TO_UINT: { 1731 integerPart x; 1732 assert(integerPartWidth >= 64); 1733 // FIXME need to be more flexible about rounding mode. 1734 APFloat::opStatus s = V.convertToInteger(&x, 64U, 1735 Opcode==ISD::FP_TO_SINT, 1736 APFloat::rmTowardZero); 1737 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual 1738 break; 1739 return getConstant(x, VT); 1740 } 1741 case ISD::BIT_CONVERT: 1742 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) 1743 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT); 1744 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) 1745 return getConstant(V.convertToAPInt().getZExtValue(), VT); 1746 break; 1747 } 1748 } 1749 } 1750 1751 unsigned OpOpcode = Operand.Val->getOpcode(); 1752 switch (Opcode) { 1753 case ISD::TokenFactor: 1754 return Operand; // Factor of one node? No factor. 1755 case ISD::FP_ROUND: 1756 case ISD::FP_EXTEND: 1757 assert(MVT::isFloatingPoint(VT) && 1758 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!"); 1759 break; 1760 case ISD::SIGN_EXTEND: 1761 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1762 "Invalid SIGN_EXTEND!"); 1763 if (Operand.getValueType() == VT) return Operand; // noop extension 1764 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT) 1765 && "Invalid sext node, dst < src!"); 1766 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) 1767 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1768 break; 1769 case ISD::ZERO_EXTEND: 1770 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1771 "Invalid ZERO_EXTEND!"); 1772 if (Operand.getValueType() == VT) return Operand; // noop extension 1773 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT) 1774 && "Invalid zext node, dst < src!"); 1775 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) 1776 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); 1777 break; 1778 case ISD::ANY_EXTEND: 1779 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1780 "Invalid ANY_EXTEND!"); 1781 if (Operand.getValueType() == VT) return Operand; // noop extension 1782 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT) 1783 && "Invalid anyext node, dst < src!"); 1784 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) 1785 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) 1786 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1787 break; 1788 case ISD::TRUNCATE: 1789 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) && 1790 "Invalid TRUNCATE!"); 1791 if (Operand.getValueType() == VT) return Operand; // noop truncate 1792 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT) 1793 && "Invalid truncate node, src < dst!"); 1794 if (OpOpcode == ISD::TRUNCATE) 1795 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1796 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || 1797 OpOpcode == ISD::ANY_EXTEND) { 1798 // If the source is smaller than the dest, we still need an extend. 1799 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType()) 1800 < MVT::getSizeInBits(VT)) 1801 return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); 1802 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType()) 1803 > MVT::getSizeInBits(VT)) 1804 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); 1805 else 1806 return Operand.Val->getOperand(0); 1807 } 1808 break; 1809 case ISD::BIT_CONVERT: 1810 // Basic sanity checking. 1811 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()) 1812 && "Cannot BIT_CONVERT between types of different sizes!"); 1813 if (VT == Operand.getValueType()) return Operand; // noop conversion. 1814 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) 1815 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0)); 1816 if (OpOpcode == ISD::UNDEF) 1817 return getNode(ISD::UNDEF, VT); 1818 break; 1819 case ISD::SCALAR_TO_VECTOR: 1820 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) && 1821 MVT::getVectorElementType(VT) == Operand.getValueType() && 1822 "Illegal SCALAR_TO_VECTOR node!"); 1823 break; 1824 case ISD::FNEG: 1825 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) 1826 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), 1827 Operand.Val->getOperand(0)); 1828 if (OpOpcode == ISD::FNEG) // --X -> X 1829 return Operand.Val->getOperand(0); 1830 break; 1831 case ISD::FABS: 1832 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) 1833 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); 1834 break; 1835 } 1836 1837 SDNode *N; 1838 SDVTList VTs = getVTList(VT); 1839 if (VT != MVT::Flag) { // Don't CSE flag producing nodes 1840 FoldingSetNodeID ID; 1841 SDOperand Ops[1] = { Operand }; 1842 AddNodeIDNode(ID, Opcode, VTs, Ops, 1); 1843 void *IP = 0; 1844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 1845 return SDOperand(E, 0); 1846 N = new UnarySDNode(Opcode, VTs, Operand); 1847 CSEMap.InsertNode(N, IP); 1848 } else { 1849 N = new UnarySDNode(Opcode, VTs, Operand); 1850 } 1851 AllNodes.push_back(N); 1852 return SDOperand(N, 0); 1853} 1854 1855 1856 1857SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 1858 SDOperand N1, SDOperand N2) { 1859#ifndef NDEBUG 1860 switch (Opcode) { 1861 case ISD::TokenFactor: 1862 assert(VT == MVT::Other && N1.getValueType() == MVT::Other && 1863 N2.getValueType() == MVT::Other && "Invalid token factor!"); 1864 break; 1865 case ISD::AND: 1866 case ISD::OR: 1867 case ISD::XOR: 1868 case ISD::UDIV: 1869 case ISD::UREM: 1870 case ISD::MULHU: 1871 case ISD::MULHS: 1872 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); 1873 // fall through 1874 case ISD::ADD: 1875 case ISD::SUB: 1876 case ISD::MUL: 1877 case ISD::SDIV: 1878 case ISD::SREM: 1879 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); 1880 // fall through. 1881 case ISD::FADD: 1882 case ISD::FSUB: 1883 case ISD::FMUL: 1884 case ISD::FDIV: 1885 case ISD::FREM: 1886 assert(N1.getValueType() == N2.getValueType() && 1887 N1.getValueType() == VT && "Binary operator types must match!"); 1888 break; 1889 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. 1890 assert(N1.getValueType() == VT && 1891 MVT::isFloatingPoint(N1.getValueType()) && 1892 MVT::isFloatingPoint(N2.getValueType()) && 1893 "Invalid FCOPYSIGN!"); 1894 break; 1895 case ISD::SHL: 1896 case ISD::SRA: 1897 case ISD::SRL: 1898 case ISD::ROTL: 1899 case ISD::ROTR: 1900 assert(VT == N1.getValueType() && 1901 "Shift operators return type must be the same as their first arg"); 1902 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && 1903 VT != MVT::i1 && "Shifts only work on integers"); 1904 break; 1905 case ISD::FP_ROUND_INREG: { 1906 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1907 assert(VT == N1.getValueType() && "Not an inreg round!"); 1908 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && 1909 "Cannot FP_ROUND_INREG integer types"); 1910 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) && 1911 "Not rounding down!"); 1912 break; 1913 } 1914 case ISD::AssertSext: 1915 case ISD::AssertZext: 1916 case ISD::SIGN_EXTEND_INREG: { 1917 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 1918 assert(VT == N1.getValueType() && "Not an inreg extend!"); 1919 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && 1920 "Cannot *_EXTEND_INREG FP types"); 1921 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) && 1922 "Not extending!"); 1923 } 1924 1925 default: break; 1926 } 1927#endif 1928 1929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 1930 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 1931 if (N1C) { 1932 if (Opcode == ISD::SIGN_EXTEND_INREG) { 1933 int64_t Val = N1C->getValue(); 1934 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT()); 1935 Val <<= 64-FromBits; 1936 Val >>= 64-FromBits; 1937 return getConstant(Val, VT); 1938 } 1939 1940 if (N2C) { 1941 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); 1942 switch (Opcode) { 1943 case ISD::ADD: return getConstant(C1 + C2, VT); 1944 case ISD::SUB: return getConstant(C1 - C2, VT); 1945 case ISD::MUL: return getConstant(C1 * C2, VT); 1946 case ISD::UDIV: 1947 if (C2) return getConstant(C1 / C2, VT); 1948 break; 1949 case ISD::UREM : 1950 if (C2) return getConstant(C1 % C2, VT); 1951 break; 1952 case ISD::SDIV : 1953 if (C2) return getConstant(N1C->getSignExtended() / 1954 N2C->getSignExtended(), VT); 1955 break; 1956 case ISD::SREM : 1957 if (C2) return getConstant(N1C->getSignExtended() % 1958 N2C->getSignExtended(), VT); 1959 break; 1960 case ISD::AND : return getConstant(C1 & C2, VT); 1961 case ISD::OR : return getConstant(C1 | C2, VT); 1962 case ISD::XOR : return getConstant(C1 ^ C2, VT); 1963 case ISD::SHL : return getConstant(C1 << C2, VT); 1964 case ISD::SRL : return getConstant(C1 >> C2, VT); 1965 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); 1966 case ISD::ROTL : 1967 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)), 1968 VT); 1969 case ISD::ROTR : 1970 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), 1971 VT); 1972 default: break; 1973 } 1974 } else { // Cannonicalize constant to RHS if commutative 1975 if (isCommutativeBinOp(Opcode)) { 1976 std::swap(N1C, N2C); 1977 std::swap(N1, N2); 1978 } 1979 } 1980 } 1981 1982 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); 1983 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); 1984 if (N1CFP) { 1985 if (N2CFP && VT!=MVT::ppcf128) { 1986 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF(); 1987 APFloat::opStatus s; 1988 switch (Opcode) { 1989 case ISD::FADD: 1990 s = V1.add(V2, APFloat::rmNearestTiesToEven); 1991 if (s!=APFloat::opInvalidOp) 1992 return getConstantFP(V1, VT); 1993 break; 1994 case ISD::FSUB: 1995 s = V1.subtract(V2, APFloat::rmNearestTiesToEven); 1996 if (s!=APFloat::opInvalidOp) 1997 return getConstantFP(V1, VT); 1998 break; 1999 case ISD::FMUL: 2000 s = V1.multiply(V2, APFloat::rmNearestTiesToEven); 2001 if (s!=APFloat::opInvalidOp) 2002 return getConstantFP(V1, VT); 2003 break; 2004 case ISD::FDIV: 2005 s = V1.divide(V2, APFloat::rmNearestTiesToEven); 2006 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 2007 return getConstantFP(V1, VT); 2008 break; 2009 case ISD::FREM : 2010 s = V1.mod(V2, APFloat::rmNearestTiesToEven); 2011 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) 2012 return getConstantFP(V1, VT); 2013 break; 2014 case ISD::FCOPYSIGN: 2015 V1.copySign(V2); 2016 return getConstantFP(V1, VT); 2017 default: break; 2018 } 2019 } else { // Cannonicalize constant to RHS if commutative 2020 if (isCommutativeBinOp(Opcode)) { 2021 std::swap(N1CFP, N2CFP); 2022 std::swap(N1, N2); 2023 } 2024 } 2025 } 2026 2027 // Canonicalize an UNDEF to the RHS, even over a constant. 2028 if (N1.getOpcode() == ISD::UNDEF) { 2029 if (isCommutativeBinOp(Opcode)) { 2030 std::swap(N1, N2); 2031 } else { 2032 switch (Opcode) { 2033 case ISD::FP_ROUND_INREG: 2034 case ISD::SIGN_EXTEND_INREG: 2035 case ISD::SUB: 2036 case ISD::FSUB: 2037 case ISD::FDIV: 2038 case ISD::FREM: 2039 case ISD::SRA: 2040 return N1; // fold op(undef, arg2) -> undef 2041 case ISD::UDIV: 2042 case ISD::SDIV: 2043 case ISD::UREM: 2044 case ISD::SREM: 2045 case ISD::SRL: 2046 case ISD::SHL: 2047 if (!MVT::isVector(VT)) 2048 return getConstant(0, VT); // fold op(undef, arg2) -> 0 2049 // For vectors, we can't easily build an all zero vector, just return 2050 // the LHS. 2051 return N2; 2052 } 2053 } 2054 } 2055 2056 // Fold a bunch of operators when the RHS is undef. 2057 if (N2.getOpcode() == ISD::UNDEF) { 2058 switch (Opcode) { 2059 case ISD::ADD: 2060 case ISD::ADDC: 2061 case ISD::ADDE: 2062 case ISD::SUB: 2063 case ISD::FADD: 2064 case ISD::FSUB: 2065 case ISD::FMUL: 2066 case ISD::FDIV: 2067 case ISD::FREM: 2068 case ISD::UDIV: 2069 case ISD::SDIV: 2070 case ISD::UREM: 2071 case ISD::SREM: 2072 case ISD::XOR: 2073 return N2; // fold op(arg1, undef) -> undef 2074 case ISD::MUL: 2075 case ISD::AND: 2076 case ISD::SRL: 2077 case ISD::SHL: 2078 if (!MVT::isVector(VT)) 2079 return getConstant(0, VT); // fold op(arg1, undef) -> 0 2080 // For vectors, we can't easily build an all zero vector, just return 2081 // the LHS. 2082 return N1; 2083 case ISD::OR: 2084 if (!MVT::isVector(VT)) 2085 return getConstant(MVT::getIntVTBitMask(VT), VT); 2086 // For vectors, we can't easily build an all one vector, just return 2087 // the LHS. 2088 return N1; 2089 case ISD::SRA: 2090 return N1; 2091 } 2092 } 2093 2094 // Fold operations. 2095 switch (Opcode) { 2096 case ISD::TokenFactor: 2097 // Fold trivial token factors. 2098 if (N1.getOpcode() == ISD::EntryToken) return N2; 2099 if (N2.getOpcode() == ISD::EntryToken) return N1; 2100 break; 2101 2102 case ISD::AND: 2103 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's 2104 // worth handling here. 2105 if (N2C && N2C->getValue() == 0) 2106 return N2; 2107 break; 2108 case ISD::OR: 2109 case ISD::XOR: 2110 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's 2111 // worth handling here. 2112 if (N2C && N2C->getValue() == 0) 2113 return N1; 2114 break; 2115 case ISD::FP_ROUND_INREG: 2116 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. 2117 break; 2118 case ISD::SIGN_EXTEND_INREG: { 2119 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); 2120 if (EVT == VT) return N1; // Not actually extending 2121 break; 2122 } 2123 case ISD::EXTRACT_VECTOR_ELT: 2124 assert(N2C && "Bad EXTRACT_VECTOR_ELT!"); 2125 2126 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is 2127 // expanding copies of large vectors from registers. 2128 if (N1.getOpcode() == ISD::CONCAT_VECTORS && 2129 N1.getNumOperands() > 0) { 2130 unsigned Factor = 2131 MVT::getVectorNumElements(N1.getOperand(0).getValueType()); 2132 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, 2133 N1.getOperand(N2C->getValue() / Factor), 2134 getConstant(N2C->getValue() % Factor, N2.getValueType())); 2135 } 2136 2137 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is 2138 // expanding large vector constants. 2139 if (N1.getOpcode() == ISD::BUILD_VECTOR) 2140 return N1.getOperand(N2C->getValue()); 2141 2142 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector 2143 // operations are lowered to scalars. 2144 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) 2145 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) { 2146 if (IEC == N2C) 2147 return N1.getOperand(1); 2148 else 2149 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2); 2150 } 2151 break; 2152 case ISD::EXTRACT_ELEMENT: 2153 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!"); 2154 2155 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding 2156 // 64-bit integers into 32-bit parts. Instead of building the extract of 2157 // the BUILD_PAIR, only to have legalize rip it apart, just do it now. 2158 if (N1.getOpcode() == ISD::BUILD_PAIR) 2159 return N1.getOperand(N2C->getValue()); 2160 2161 // EXTRACT_ELEMENT of a constant int is also very common. 2162 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 2163 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue(); 2164 return getConstant(C->getValue() >> Shift, VT); 2165 } 2166 break; 2167 2168 // FIXME: figure out how to safely handle things like 2169 // int foo(int x) { return 1 << (x & 255); } 2170 // int bar() { return foo(256); } 2171#if 0 2172 case ISD::SHL: 2173 case ISD::SRL: 2174 case ISD::SRA: 2175 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && 2176 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) 2177 return getNode(Opcode, VT, N1, N2.getOperand(0)); 2178 else if (N2.getOpcode() == ISD::AND) 2179 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { 2180 // If the and is only masking out bits that cannot effect the shift, 2181 // eliminate the and. 2182 unsigned NumBits = MVT::getSizeInBits(VT); 2183 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 2184 return getNode(Opcode, VT, N1, N2.getOperand(0)); 2185 } 2186 break; 2187#endif 2188 } 2189 2190 // Memoize this node if possible. 2191 SDNode *N; 2192 SDVTList VTs = getVTList(VT); 2193 if (VT != MVT::Flag) { 2194 SDOperand Ops[] = { N1, N2 }; 2195 FoldingSetNodeID ID; 2196 AddNodeIDNode(ID, Opcode, VTs, Ops, 2); 2197 void *IP = 0; 2198 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2199 return SDOperand(E, 0); 2200 N = new BinarySDNode(Opcode, VTs, N1, N2); 2201 CSEMap.InsertNode(N, IP); 2202 } else { 2203 N = new BinarySDNode(Opcode, VTs, N1, N2); 2204 } 2205 2206 AllNodes.push_back(N); 2207 return SDOperand(N, 0); 2208} 2209 2210SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2211 SDOperand N1, SDOperand N2, SDOperand N3) { 2212 // Perform various simplifications. 2213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); 2214 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); 2215 switch (Opcode) { 2216 case ISD::SETCC: { 2217 // Use FoldSetCC to simplify SETCC's. 2218 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); 2219 if (Simp.Val) return Simp; 2220 break; 2221 } 2222 case ISD::SELECT: 2223 if (N1C) 2224 if (N1C->getValue()) 2225 return N2; // select true, X, Y -> X 2226 else 2227 return N3; // select false, X, Y -> Y 2228 2229 if (N2 == N3) return N2; // select C, X, X -> X 2230 break; 2231 case ISD::BRCOND: 2232 if (N2C) 2233 if (N2C->getValue()) // Unconditional branch 2234 return getNode(ISD::BR, MVT::Other, N1, N3); 2235 else 2236 return N1; // Never-taken branch 2237 break; 2238 case ISD::VECTOR_SHUFFLE: 2239 assert(VT == N1.getValueType() && VT == N2.getValueType() && 2240 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) && 2241 N3.getOpcode() == ISD::BUILD_VECTOR && 2242 MVT::getVectorNumElements(VT) == N3.getNumOperands() && 2243 "Illegal VECTOR_SHUFFLE node!"); 2244 break; 2245 case ISD::BIT_CONVERT: 2246 // Fold bit_convert nodes from a type to themselves. 2247 if (N1.getValueType() == VT) 2248 return N1; 2249 break; 2250 } 2251 2252 // Memoize node if it doesn't produce a flag. 2253 SDNode *N; 2254 SDVTList VTs = getVTList(VT); 2255 if (VT != MVT::Flag) { 2256 SDOperand Ops[] = { N1, N2, N3 }; 2257 FoldingSetNodeID ID; 2258 AddNodeIDNode(ID, Opcode, VTs, Ops, 3); 2259 void *IP = 0; 2260 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2261 return SDOperand(E, 0); 2262 N = new TernarySDNode(Opcode, VTs, N1, N2, N3); 2263 CSEMap.InsertNode(N, IP); 2264 } else { 2265 N = new TernarySDNode(Opcode, VTs, N1, N2, N3); 2266 } 2267 AllNodes.push_back(N); 2268 return SDOperand(N, 0); 2269} 2270 2271SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2272 SDOperand N1, SDOperand N2, SDOperand N3, 2273 SDOperand N4) { 2274 SDOperand Ops[] = { N1, N2, N3, N4 }; 2275 return getNode(Opcode, VT, Ops, 4); 2276} 2277 2278SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2279 SDOperand N1, SDOperand N2, SDOperand N3, 2280 SDOperand N4, SDOperand N5) { 2281 SDOperand Ops[] = { N1, N2, N3, N4, N5 }; 2282 return getNode(Opcode, VT, Ops, 5); 2283} 2284 2285SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest, 2286 SDOperand Src, SDOperand Size, 2287 SDOperand Align, 2288 SDOperand AlwaysInline) { 2289 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline }; 2290 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6); 2291} 2292 2293SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest, 2294 SDOperand Src, SDOperand Size, 2295 SDOperand Align, 2296 SDOperand AlwaysInline) { 2297 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline }; 2298 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6); 2299} 2300 2301SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest, 2302 SDOperand Src, SDOperand Size, 2303 SDOperand Align, 2304 SDOperand AlwaysInline) { 2305 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline }; 2306 return getNode(ISD::MEMSET, MVT::Other, Ops, 6); 2307} 2308 2309SDOperand SelectionDAG::getLoad(MVT::ValueType VT, 2310 SDOperand Chain, SDOperand Ptr, 2311 const Value *SV, int SVOffset, 2312 bool isVolatile, unsigned Alignment) { 2313 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2314 const Type *Ty = 0; 2315 if (VT != MVT::iPTR) { 2316 Ty = MVT::getTypeForValueType(VT); 2317 } else if (SV) { 2318 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2319 assert(PT && "Value for load must be a pointer"); 2320 Ty = PT->getElementType(); 2321 } 2322 assert(Ty && "Could not get type information for load"); 2323 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2324 } 2325 SDVTList VTs = getVTList(VT, MVT::Other); 2326 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2327 SDOperand Ops[] = { Chain, Ptr, Undef }; 2328 FoldingSetNodeID ID; 2329 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 2330 ID.AddInteger(ISD::UNINDEXED); 2331 ID.AddInteger(ISD::NON_EXTLOAD); 2332 ID.AddInteger((unsigned int)VT); 2333 ID.AddPointer(SV); 2334 ID.AddInteger(SVOffset); 2335 ID.AddInteger(Alignment); 2336 ID.AddInteger(isVolatile); 2337 void *IP = 0; 2338 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2339 return SDOperand(E, 0); 2340 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, 2341 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment, 2342 isVolatile); 2343 CSEMap.InsertNode(N, IP); 2344 AllNodes.push_back(N); 2345 return SDOperand(N, 0); 2346} 2347 2348SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT, 2349 SDOperand Chain, SDOperand Ptr, 2350 const Value *SV, 2351 int SVOffset, MVT::ValueType EVT, 2352 bool isVolatile, unsigned Alignment) { 2353 // If they are asking for an extending load from/to the same thing, return a 2354 // normal load. 2355 if (VT == EVT) 2356 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment); 2357 2358 if (MVT::isVector(VT)) 2359 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!"); 2360 else 2361 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) && 2362 "Should only be an extending load, not truncating!"); 2363 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) && 2364 "Cannot sign/zero extend a FP/Vector load!"); 2365 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) && 2366 "Cannot convert from FP to Int or Int -> FP!"); 2367 2368 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2369 const Type *Ty = 0; 2370 if (VT != MVT::iPTR) { 2371 Ty = MVT::getTypeForValueType(VT); 2372 } else if (SV) { 2373 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2374 assert(PT && "Value for load must be a pointer"); 2375 Ty = PT->getElementType(); 2376 } 2377 assert(Ty && "Could not get type information for load"); 2378 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2379 } 2380 SDVTList VTs = getVTList(VT, MVT::Other); 2381 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2382 SDOperand Ops[] = { Chain, Ptr, Undef }; 2383 FoldingSetNodeID ID; 2384 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 2385 ID.AddInteger(ISD::UNINDEXED); 2386 ID.AddInteger(ExtType); 2387 ID.AddInteger((unsigned int)EVT); 2388 ID.AddPointer(SV); 2389 ID.AddInteger(SVOffset); 2390 ID.AddInteger(Alignment); 2391 ID.AddInteger(isVolatile); 2392 void *IP = 0; 2393 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2394 return SDOperand(E, 0); 2395 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT, 2396 SV, SVOffset, Alignment, isVolatile); 2397 CSEMap.InsertNode(N, IP); 2398 AllNodes.push_back(N); 2399 return SDOperand(N, 0); 2400} 2401 2402SDOperand 2403SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base, 2404 SDOperand Offset, ISD::MemIndexedMode AM) { 2405 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad); 2406 assert(LD->getOffset().getOpcode() == ISD::UNDEF && 2407 "Load is already a indexed load!"); 2408 MVT::ValueType VT = OrigLoad.getValueType(); 2409 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other); 2410 SDOperand Ops[] = { LD->getChain(), Base, Offset }; 2411 FoldingSetNodeID ID; 2412 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); 2413 ID.AddInteger(AM); 2414 ID.AddInteger(LD->getExtensionType()); 2415 ID.AddInteger((unsigned int)(LD->getLoadedVT())); 2416 ID.AddPointer(LD->getSrcValue()); 2417 ID.AddInteger(LD->getSrcValueOffset()); 2418 ID.AddInteger(LD->getAlignment()); 2419 ID.AddInteger(LD->isVolatile()); 2420 void *IP = 0; 2421 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2422 return SDOperand(E, 0); 2423 SDNode *N = new LoadSDNode(Ops, VTs, AM, 2424 LD->getExtensionType(), LD->getLoadedVT(), 2425 LD->getSrcValue(), LD->getSrcValueOffset(), 2426 LD->getAlignment(), LD->isVolatile()); 2427 CSEMap.InsertNode(N, IP); 2428 AllNodes.push_back(N); 2429 return SDOperand(N, 0); 2430} 2431 2432SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val, 2433 SDOperand Ptr, const Value *SV, int SVOffset, 2434 bool isVolatile, unsigned Alignment) { 2435 MVT::ValueType VT = Val.getValueType(); 2436 2437 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2438 const Type *Ty = 0; 2439 if (VT != MVT::iPTR) { 2440 Ty = MVT::getTypeForValueType(VT); 2441 } else if (SV) { 2442 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2443 assert(PT && "Value for store must be a pointer"); 2444 Ty = PT->getElementType(); 2445 } 2446 assert(Ty && "Could not get type information for store"); 2447 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2448 } 2449 SDVTList VTs = getVTList(MVT::Other); 2450 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2451 SDOperand Ops[] = { Chain, Val, Ptr, Undef }; 2452 FoldingSetNodeID ID; 2453 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 2454 ID.AddInteger(ISD::UNINDEXED); 2455 ID.AddInteger(false); 2456 ID.AddInteger((unsigned int)VT); 2457 ID.AddPointer(SV); 2458 ID.AddInteger(SVOffset); 2459 ID.AddInteger(Alignment); 2460 ID.AddInteger(isVolatile); 2461 void *IP = 0; 2462 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2463 return SDOperand(E, 0); 2464 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false, 2465 VT, SV, SVOffset, Alignment, isVolatile); 2466 CSEMap.InsertNode(N, IP); 2467 AllNodes.push_back(N); 2468 return SDOperand(N, 0); 2469} 2470 2471SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val, 2472 SDOperand Ptr, const Value *SV, 2473 int SVOffset, MVT::ValueType SVT, 2474 bool isVolatile, unsigned Alignment) { 2475 MVT::ValueType VT = Val.getValueType(); 2476 2477 if (VT == SVT) 2478 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment); 2479 2480 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) && 2481 "Not a truncation?"); 2482 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) && 2483 "Can't do FP-INT conversion!"); 2484 2485 if (Alignment == 0) { // Ensure that codegen never sees alignment 0 2486 const Type *Ty = 0; 2487 if (VT != MVT::iPTR) { 2488 Ty = MVT::getTypeForValueType(VT); 2489 } else if (SV) { 2490 const PointerType *PT = dyn_cast<PointerType>(SV->getType()); 2491 assert(PT && "Value for store must be a pointer"); 2492 Ty = PT->getElementType(); 2493 } 2494 assert(Ty && "Could not get type information for store"); 2495 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty); 2496 } 2497 SDVTList VTs = getVTList(MVT::Other); 2498 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType()); 2499 SDOperand Ops[] = { Chain, Val, Ptr, Undef }; 2500 FoldingSetNodeID ID; 2501 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 2502 ID.AddInteger(ISD::UNINDEXED); 2503 ID.AddInteger(1); 2504 ID.AddInteger((unsigned int)SVT); 2505 ID.AddPointer(SV); 2506 ID.AddInteger(SVOffset); 2507 ID.AddInteger(Alignment); 2508 ID.AddInteger(isVolatile); 2509 void *IP = 0; 2510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2511 return SDOperand(E, 0); 2512 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true, 2513 SVT, SV, SVOffset, Alignment, isVolatile); 2514 CSEMap.InsertNode(N, IP); 2515 AllNodes.push_back(N); 2516 return SDOperand(N, 0); 2517} 2518 2519SDOperand 2520SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base, 2521 SDOperand Offset, ISD::MemIndexedMode AM) { 2522 StoreSDNode *ST = cast<StoreSDNode>(OrigStore); 2523 assert(ST->getOffset().getOpcode() == ISD::UNDEF && 2524 "Store is already a indexed store!"); 2525 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other); 2526 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset }; 2527 FoldingSetNodeID ID; 2528 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); 2529 ID.AddInteger(AM); 2530 ID.AddInteger(ST->isTruncatingStore()); 2531 ID.AddInteger((unsigned int)(ST->getStoredVT())); 2532 ID.AddPointer(ST->getSrcValue()); 2533 ID.AddInteger(ST->getSrcValueOffset()); 2534 ID.AddInteger(ST->getAlignment()); 2535 ID.AddInteger(ST->isVolatile()); 2536 void *IP = 0; 2537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2538 return SDOperand(E, 0); 2539 SDNode *N = new StoreSDNode(Ops, VTs, AM, 2540 ST->isTruncatingStore(), ST->getStoredVT(), 2541 ST->getSrcValue(), ST->getSrcValueOffset(), 2542 ST->getAlignment(), ST->isVolatile()); 2543 CSEMap.InsertNode(N, IP); 2544 AllNodes.push_back(N); 2545 return SDOperand(N, 0); 2546} 2547 2548SDOperand SelectionDAG::getVAArg(MVT::ValueType VT, 2549 SDOperand Chain, SDOperand Ptr, 2550 SDOperand SV) { 2551 SDOperand Ops[] = { Chain, Ptr, SV }; 2552 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3); 2553} 2554 2555SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, 2556 const SDOperand *Ops, unsigned NumOps) { 2557 switch (NumOps) { 2558 case 0: return getNode(Opcode, VT); 2559 case 1: return getNode(Opcode, VT, Ops[0]); 2560 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); 2561 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); 2562 default: break; 2563 } 2564 2565 switch (Opcode) { 2566 default: break; 2567 case ISD::SELECT_CC: { 2568 assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); 2569 assert(Ops[0].getValueType() == Ops[1].getValueType() && 2570 "LHS and RHS of condition must have same type!"); 2571 assert(Ops[2].getValueType() == Ops[3].getValueType() && 2572 "True and False arms of SelectCC must have same type!"); 2573 assert(Ops[2].getValueType() == VT && 2574 "select_cc node must be of same type as true and false value!"); 2575 break; 2576 } 2577 case ISD::BR_CC: { 2578 assert(NumOps == 5 && "BR_CC takes 5 operands!"); 2579 assert(Ops[2].getValueType() == Ops[3].getValueType() && 2580 "LHS/RHS of comparison should match types!"); 2581 break; 2582 } 2583 } 2584 2585 // Memoize nodes. 2586 SDNode *N; 2587 SDVTList VTs = getVTList(VT); 2588 if (VT != MVT::Flag) { 2589 FoldingSetNodeID ID; 2590 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps); 2591 void *IP = 0; 2592 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2593 return SDOperand(E, 0); 2594 N = new SDNode(Opcode, VTs, Ops, NumOps); 2595 CSEMap.InsertNode(N, IP); 2596 } else { 2597 N = new SDNode(Opcode, VTs, Ops, NumOps); 2598 } 2599 AllNodes.push_back(N); 2600 return SDOperand(N, 0); 2601} 2602 2603SDOperand SelectionDAG::getNode(unsigned Opcode, 2604 std::vector<MVT::ValueType> &ResultTys, 2605 const SDOperand *Ops, unsigned NumOps) { 2606 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(), 2607 Ops, NumOps); 2608} 2609 2610SDOperand SelectionDAG::getNode(unsigned Opcode, 2611 const MVT::ValueType *VTs, unsigned NumVTs, 2612 const SDOperand *Ops, unsigned NumOps) { 2613 if (NumVTs == 1) 2614 return getNode(Opcode, VTs[0], Ops, NumOps); 2615 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps); 2616} 2617 2618SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2619 const SDOperand *Ops, unsigned NumOps) { 2620 if (VTList.NumVTs == 1) 2621 return getNode(Opcode, VTList.VTs[0], Ops, NumOps); 2622 2623 switch (Opcode) { 2624 // FIXME: figure out how to safely handle things like 2625 // int foo(int x) { return 1 << (x & 255); } 2626 // int bar() { return foo(256); } 2627#if 0 2628 case ISD::SRA_PARTS: 2629 case ISD::SRL_PARTS: 2630 case ISD::SHL_PARTS: 2631 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && 2632 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) 2633 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 2634 else if (N3.getOpcode() == ISD::AND) 2635 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { 2636 // If the and is only masking out bits that cannot effect the shift, 2637 // eliminate the and. 2638 unsigned NumBits = MVT::getSizeInBits(VT)*2; 2639 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) 2640 return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); 2641 } 2642 break; 2643#endif 2644 } 2645 2646 // Memoize the node unless it returns a flag. 2647 SDNode *N; 2648 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) { 2649 FoldingSetNodeID ID; 2650 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); 2651 void *IP = 0; 2652 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) 2653 return SDOperand(E, 0); 2654 if (NumOps == 1) 2655 N = new UnarySDNode(Opcode, VTList, Ops[0]); 2656 else if (NumOps == 2) 2657 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]); 2658 else if (NumOps == 3) 2659 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]); 2660 else 2661 N = new SDNode(Opcode, VTList, Ops, NumOps); 2662 CSEMap.InsertNode(N, IP); 2663 } else { 2664 if (NumOps == 1) 2665 N = new UnarySDNode(Opcode, VTList, Ops[0]); 2666 else if (NumOps == 2) 2667 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]); 2668 else if (NumOps == 3) 2669 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]); 2670 else 2671 N = new SDNode(Opcode, VTList, Ops, NumOps); 2672 } 2673 AllNodes.push_back(N); 2674 return SDOperand(N, 0); 2675} 2676 2677SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) { 2678 return getNode(Opcode, VTList, 0, 0); 2679} 2680 2681SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2682 SDOperand N1) { 2683 SDOperand Ops[] = { N1 }; 2684 return getNode(Opcode, VTList, Ops, 1); 2685} 2686 2687SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2688 SDOperand N1, SDOperand N2) { 2689 SDOperand Ops[] = { N1, N2 }; 2690 return getNode(Opcode, VTList, Ops, 2); 2691} 2692 2693SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2694 SDOperand N1, SDOperand N2, SDOperand N3) { 2695 SDOperand Ops[] = { N1, N2, N3 }; 2696 return getNode(Opcode, VTList, Ops, 3); 2697} 2698 2699SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2700 SDOperand N1, SDOperand N2, SDOperand N3, 2701 SDOperand N4) { 2702 SDOperand Ops[] = { N1, N2, N3, N4 }; 2703 return getNode(Opcode, VTList, Ops, 4); 2704} 2705 2706SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList, 2707 SDOperand N1, SDOperand N2, SDOperand N3, 2708 SDOperand N4, SDOperand N5) { 2709 SDOperand Ops[] = { N1, N2, N3, N4, N5 }; 2710 return getNode(Opcode, VTList, Ops, 5); 2711} 2712 2713SDVTList SelectionDAG::getVTList(MVT::ValueType VT) { 2714 return makeVTList(SDNode::getValueTypeList(VT), 1); 2715} 2716 2717SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) { 2718 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2719 E = VTList.end(); I != E; ++I) { 2720 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) 2721 return makeVTList(&(*I)[0], 2); 2722 } 2723 std::vector<MVT::ValueType> V; 2724 V.push_back(VT1); 2725 V.push_back(VT2); 2726 VTList.push_front(V); 2727 return makeVTList(&(*VTList.begin())[0], 2); 2728} 2729SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2, 2730 MVT::ValueType VT3) { 2731 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2732 E = VTList.end(); I != E; ++I) { 2733 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 && 2734 (*I)[2] == VT3) 2735 return makeVTList(&(*I)[0], 3); 2736 } 2737 std::vector<MVT::ValueType> V; 2738 V.push_back(VT1); 2739 V.push_back(VT2); 2740 V.push_back(VT3); 2741 VTList.push_front(V); 2742 return makeVTList(&(*VTList.begin())[0], 3); 2743} 2744 2745SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) { 2746 switch (NumVTs) { 2747 case 0: assert(0 && "Cannot have nodes without results!"); 2748 case 1: return getVTList(VTs[0]); 2749 case 2: return getVTList(VTs[0], VTs[1]); 2750 case 3: return getVTList(VTs[0], VTs[1], VTs[2]); 2751 default: break; 2752 } 2753 2754 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), 2755 E = VTList.end(); I != E; ++I) { 2756 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue; 2757 2758 bool NoMatch = false; 2759 for (unsigned i = 2; i != NumVTs; ++i) 2760 if (VTs[i] != (*I)[i]) { 2761 NoMatch = true; 2762 break; 2763 } 2764 if (!NoMatch) 2765 return makeVTList(&*I->begin(), NumVTs); 2766 } 2767 2768 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs)); 2769 return makeVTList(&*VTList.begin()->begin(), NumVTs); 2770} 2771 2772 2773/// UpdateNodeOperands - *Mutate* the specified node in-place to have the 2774/// specified operands. If the resultant node already exists in the DAG, 2775/// this does not modify the specified node, instead it returns the node that 2776/// already exists. If the resultant node does not exist in the DAG, the 2777/// input node is returned. As a degenerate case, if you specify the same 2778/// input operands as the node already has, the input node is returned. 2779SDOperand SelectionDAG:: 2780UpdateNodeOperands(SDOperand InN, SDOperand Op) { 2781 SDNode *N = InN.Val; 2782 assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); 2783 2784 // Check to see if there is no change. 2785 if (Op == N->getOperand(0)) return InN; 2786 2787 // See if the modified node already exists. 2788 void *InsertPos = 0; 2789 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) 2790 return SDOperand(Existing, InN.ResNo); 2791 2792 // Nope it doesn't. Remove the node from it's current place in the maps. 2793 if (InsertPos) 2794 RemoveNodeFromCSEMaps(N); 2795 2796 // Now we update the operands. 2797 N->OperandList[0].Val->removeUser(N); 2798 Op.Val->addUser(N); 2799 N->OperandList[0] = Op; 2800 2801 // If this gets put into a CSE map, add it. 2802 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 2803 return InN; 2804} 2805 2806SDOperand SelectionDAG:: 2807UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) { 2808 SDNode *N = InN.Val; 2809 assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); 2810 2811 // Check to see if there is no change. 2812 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) 2813 return InN; // No operands changed, just return the input node. 2814 2815 // See if the modified node already exists. 2816 void *InsertPos = 0; 2817 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) 2818 return SDOperand(Existing, InN.ResNo); 2819 2820 // Nope it doesn't. Remove the node from it's current place in the maps. 2821 if (InsertPos) 2822 RemoveNodeFromCSEMaps(N); 2823 2824 // Now we update the operands. 2825 if (N->OperandList[0] != Op1) { 2826 N->OperandList[0].Val->removeUser(N); 2827 Op1.Val->addUser(N); 2828 N->OperandList[0] = Op1; 2829 } 2830 if (N->OperandList[1] != Op2) { 2831 N->OperandList[1].Val->removeUser(N); 2832 Op2.Val->addUser(N); 2833 N->OperandList[1] = Op2; 2834 } 2835 2836 // If this gets put into a CSE map, add it. 2837 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 2838 return InN; 2839} 2840 2841SDOperand SelectionDAG:: 2842UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) { 2843 SDOperand Ops[] = { Op1, Op2, Op3 }; 2844 return UpdateNodeOperands(N, Ops, 3); 2845} 2846 2847SDOperand SelectionDAG:: 2848UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 2849 SDOperand Op3, SDOperand Op4) { 2850 SDOperand Ops[] = { Op1, Op2, Op3, Op4 }; 2851 return UpdateNodeOperands(N, Ops, 4); 2852} 2853 2854SDOperand SelectionDAG:: 2855UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, 2856 SDOperand Op3, SDOperand Op4, SDOperand Op5) { 2857 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 }; 2858 return UpdateNodeOperands(N, Ops, 5); 2859} 2860 2861 2862SDOperand SelectionDAG:: 2863UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) { 2864 SDNode *N = InN.Val; 2865 assert(N->getNumOperands() == NumOps && 2866 "Update with wrong number of operands"); 2867 2868 // Check to see if there is no change. 2869 bool AnyChange = false; 2870 for (unsigned i = 0; i != NumOps; ++i) { 2871 if (Ops[i] != N->getOperand(i)) { 2872 AnyChange = true; 2873 break; 2874 } 2875 } 2876 2877 // No operands changed, just return the input node. 2878 if (!AnyChange) return InN; 2879 2880 // See if the modified node already exists. 2881 void *InsertPos = 0; 2882 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos)) 2883 return SDOperand(Existing, InN.ResNo); 2884 2885 // Nope it doesn't. Remove the node from it's current place in the maps. 2886 if (InsertPos) 2887 RemoveNodeFromCSEMaps(N); 2888 2889 // Now we update the operands. 2890 for (unsigned i = 0; i != NumOps; ++i) { 2891 if (N->OperandList[i] != Ops[i]) { 2892 N->OperandList[i].Val->removeUser(N); 2893 Ops[i].Val->addUser(N); 2894 N->OperandList[i] = Ops[i]; 2895 } 2896 } 2897 2898 // If this gets put into a CSE map, add it. 2899 if (InsertPos) CSEMap.InsertNode(N, InsertPos); 2900 return InN; 2901} 2902 2903 2904/// MorphNodeTo - This frees the operands of the current node, resets the 2905/// opcode, types, and operands to the specified value. This should only be 2906/// used by the SelectionDAG class. 2907void SDNode::MorphNodeTo(unsigned Opc, SDVTList L, 2908 const SDOperand *Ops, unsigned NumOps) { 2909 NodeType = Opc; 2910 ValueList = L.VTs; 2911 NumValues = L.NumVTs; 2912 2913 // Clear the operands list, updating used nodes to remove this from their 2914 // use list. 2915 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) 2916 I->Val->removeUser(this); 2917 2918 // If NumOps is larger than the # of operands we currently have, reallocate 2919 // the operand list. 2920 if (NumOps > NumOperands) { 2921 if (OperandsNeedDelete) 2922 delete [] OperandList; 2923 OperandList = new SDOperand[NumOps]; 2924 OperandsNeedDelete = true; 2925 } 2926 2927 // Assign the new operands. 2928 NumOperands = NumOps; 2929 2930 for (unsigned i = 0, e = NumOps; i != e; ++i) { 2931 OperandList[i] = Ops[i]; 2932 SDNode *N = OperandList[i].Val; 2933 N->Uses.push_back(this); 2934 } 2935} 2936 2937/// SelectNodeTo - These are used for target selectors to *mutate* the 2938/// specified node to have the specified return type, Target opcode, and 2939/// operands. Note that target opcodes are stored as 2940/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 2941/// 2942/// Note that SelectNodeTo returns the resultant node. If there is already a 2943/// node of the specified opcode and operands, it returns that node instead of 2944/// the current one. 2945SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2946 MVT::ValueType VT) { 2947 SDVTList VTs = getVTList(VT); 2948 FoldingSetNodeID ID; 2949 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0); 2950 void *IP = 0; 2951 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2952 return ON; 2953 2954 RemoveNodeFromCSEMaps(N); 2955 2956 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0); 2957 2958 CSEMap.InsertNode(N, IP); 2959 return N; 2960} 2961 2962SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2963 MVT::ValueType VT, SDOperand Op1) { 2964 // If an identical node already exists, use it. 2965 SDVTList VTs = getVTList(VT); 2966 SDOperand Ops[] = { Op1 }; 2967 2968 FoldingSetNodeID ID; 2969 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1); 2970 void *IP = 0; 2971 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2972 return ON; 2973 2974 RemoveNodeFromCSEMaps(N); 2975 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1); 2976 CSEMap.InsertNode(N, IP); 2977 return N; 2978} 2979 2980SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 2981 MVT::ValueType VT, SDOperand Op1, 2982 SDOperand Op2) { 2983 // If an identical node already exists, use it. 2984 SDVTList VTs = getVTList(VT); 2985 SDOperand Ops[] = { Op1, Op2 }; 2986 2987 FoldingSetNodeID ID; 2988 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 2989 void *IP = 0; 2990 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 2991 return ON; 2992 2993 RemoveNodeFromCSEMaps(N); 2994 2995 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 2996 2997 CSEMap.InsertNode(N, IP); // Memoize the new node. 2998 return N; 2999} 3000 3001SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 3002 MVT::ValueType VT, SDOperand Op1, 3003 SDOperand Op2, SDOperand Op3) { 3004 // If an identical node already exists, use it. 3005 SDVTList VTs = getVTList(VT); 3006 SDOperand Ops[] = { Op1, Op2, Op3 }; 3007 FoldingSetNodeID ID; 3008 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 3009 void *IP = 0; 3010 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 3011 return ON; 3012 3013 RemoveNodeFromCSEMaps(N); 3014 3015 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 3016 3017 CSEMap.InsertNode(N, IP); // Memoize the new node. 3018 return N; 3019} 3020 3021SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 3022 MVT::ValueType VT, const SDOperand *Ops, 3023 unsigned NumOps) { 3024 // If an identical node already exists, use it. 3025 SDVTList VTs = getVTList(VT); 3026 FoldingSetNodeID ID; 3027 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps); 3028 void *IP = 0; 3029 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 3030 return ON; 3031 3032 RemoveNodeFromCSEMaps(N); 3033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps); 3034 3035 CSEMap.InsertNode(N, IP); // Memoize the new node. 3036 return N; 3037} 3038 3039SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 3040 MVT::ValueType VT1, MVT::ValueType VT2, 3041 SDOperand Op1, SDOperand Op2) { 3042 SDVTList VTs = getVTList(VT1, VT2); 3043 FoldingSetNodeID ID; 3044 SDOperand Ops[] = { Op1, Op2 }; 3045 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 3046 void *IP = 0; 3047 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 3048 return ON; 3049 3050 RemoveNodeFromCSEMaps(N); 3051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2); 3052 CSEMap.InsertNode(N, IP); // Memoize the new node. 3053 return N; 3054} 3055 3056SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 3057 MVT::ValueType VT1, MVT::ValueType VT2, 3058 SDOperand Op1, SDOperand Op2, 3059 SDOperand Op3) { 3060 // If an identical node already exists, use it. 3061 SDVTList VTs = getVTList(VT1, VT2); 3062 SDOperand Ops[] = { Op1, Op2, Op3 }; 3063 FoldingSetNodeID ID; 3064 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 3065 void *IP = 0; 3066 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) 3067 return ON; 3068 3069 RemoveNodeFromCSEMaps(N); 3070 3071 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3); 3072 CSEMap.InsertNode(N, IP); // Memoize the new node. 3073 return N; 3074} 3075 3076 3077/// getTargetNode - These are used for target selectors to create a new node 3078/// with specified return type(s), target opcode, and operands. 3079/// 3080/// Note that getTargetNode returns the resultant node. If there is already a 3081/// node of the specified opcode and operands, it returns that node instead of 3082/// the current one. 3083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) { 3084 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val; 3085} 3086SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3087 SDOperand Op1) { 3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val; 3089} 3090SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3091 SDOperand Op1, SDOperand Op2) { 3092 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val; 3093} 3094SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3095 SDOperand Op1, SDOperand Op2, 3096 SDOperand Op3) { 3097 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val; 3098} 3099SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, 3100 const SDOperand *Ops, unsigned NumOps) { 3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val; 3102} 3103SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3104 MVT::ValueType VT2) { 3105 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3106 SDOperand Op; 3107 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val; 3108} 3109SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3110 MVT::ValueType VT2, SDOperand Op1) { 3111 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3112 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val; 3113} 3114SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3115 MVT::ValueType VT2, SDOperand Op1, 3116 SDOperand Op2) { 3117 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3118 SDOperand Ops[] = { Op1, Op2 }; 3119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val; 3120} 3121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3122 MVT::ValueType VT2, SDOperand Op1, 3123 SDOperand Op2, SDOperand Op3) { 3124 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3125 SDOperand Ops[] = { Op1, Op2, Op3 }; 3126 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val; 3127} 3128SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3129 MVT::ValueType VT2, 3130 const SDOperand *Ops, unsigned NumOps) { 3131 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2); 3132 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val; 3133} 3134SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3135 MVT::ValueType VT2, MVT::ValueType VT3, 3136 SDOperand Op1, SDOperand Op2) { 3137 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 3138 SDOperand Ops[] = { Op1, Op2 }; 3139 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val; 3140} 3141SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3142 MVT::ValueType VT2, MVT::ValueType VT3, 3143 SDOperand Op1, SDOperand Op2, 3144 SDOperand Op3) { 3145 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 3146 SDOperand Ops[] = { Op1, Op2, Op3 }; 3147 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val; 3148} 3149SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3150 MVT::ValueType VT2, MVT::ValueType VT3, 3151 const SDOperand *Ops, unsigned NumOps) { 3152 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3); 3153 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val; 3154} 3155SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, 3156 MVT::ValueType VT2, MVT::ValueType VT3, 3157 MVT::ValueType VT4, 3158 const SDOperand *Ops, unsigned NumOps) { 3159 std::vector<MVT::ValueType> VTList; 3160 VTList.push_back(VT1); 3161 VTList.push_back(VT2); 3162 VTList.push_back(VT3); 3163 VTList.push_back(VT4); 3164 const MVT::ValueType *VTs = getNodeValueTypes(VTList); 3165 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val; 3166} 3167SDNode *SelectionDAG::getTargetNode(unsigned Opcode, 3168 std::vector<MVT::ValueType> &ResultTys, 3169 const SDOperand *Ops, unsigned NumOps) { 3170 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys); 3171 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(), 3172 Ops, NumOps).Val; 3173} 3174 3175/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 3176/// This can cause recursive merging of nodes in the DAG. 3177/// 3178/// This version assumes From/To have a single result value. 3179/// 3180void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, 3181 std::vector<SDNode*> *Deleted) { 3182 SDNode *From = FromN.Val, *To = ToN.Val; 3183 assert(From->getNumValues() == 1 && To->getNumValues() == 1 && 3184 "Cannot replace with this method!"); 3185 assert(From != To && "Cannot replace uses of with self"); 3186 3187 while (!From->use_empty()) { 3188 // Process users until they are all gone. 3189 SDNode *U = *From->use_begin(); 3190 3191 // This node is about to morph, remove its old self from the CSE maps. 3192 RemoveNodeFromCSEMaps(U); 3193 3194 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 3195 I != E; ++I) 3196 if (I->Val == From) { 3197 From->removeUser(U); 3198 I->Val = To; 3199 To->addUser(U); 3200 } 3201 3202 // Now that we have modified U, add it back to the CSE maps. If it already 3203 // exists there, recursively merge the results together. 3204 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 3205 ReplaceAllUsesWith(U, Existing, Deleted); 3206 // U is now dead. 3207 if (Deleted) Deleted->push_back(U); 3208 DeleteNodeNotInCSEMaps(U); 3209 } 3210 } 3211} 3212 3213/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 3214/// This can cause recursive merging of nodes in the DAG. 3215/// 3216/// This version assumes From/To have matching types and numbers of result 3217/// values. 3218/// 3219void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, 3220 std::vector<SDNode*> *Deleted) { 3221 assert(From != To && "Cannot replace uses of with self"); 3222 assert(From->getNumValues() == To->getNumValues() && 3223 "Cannot use this version of ReplaceAllUsesWith!"); 3224 if (From->getNumValues() == 1) { // If possible, use the faster version. 3225 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); 3226 return; 3227 } 3228 3229 while (!From->use_empty()) { 3230 // Process users until they are all gone. 3231 SDNode *U = *From->use_begin(); 3232 3233 // This node is about to morph, remove its old self from the CSE maps. 3234 RemoveNodeFromCSEMaps(U); 3235 3236 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 3237 I != E; ++I) 3238 if (I->Val == From) { 3239 From->removeUser(U); 3240 I->Val = To; 3241 To->addUser(U); 3242 } 3243 3244 // Now that we have modified U, add it back to the CSE maps. If it already 3245 // exists there, recursively merge the results together. 3246 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 3247 ReplaceAllUsesWith(U, Existing, Deleted); 3248 // U is now dead. 3249 if (Deleted) Deleted->push_back(U); 3250 DeleteNodeNotInCSEMaps(U); 3251 } 3252 } 3253} 3254 3255/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 3256/// This can cause recursive merging of nodes in the DAG. 3257/// 3258/// This version can replace From with any result values. To must match the 3259/// number and types of values returned by From. 3260void SelectionDAG::ReplaceAllUsesWith(SDNode *From, 3261 const SDOperand *To, 3262 std::vector<SDNode*> *Deleted) { 3263 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) { 3264 // Degenerate case handled above. 3265 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); 3266 return; 3267 } 3268 3269 while (!From->use_empty()) { 3270 // Process users until they are all gone. 3271 SDNode *U = *From->use_begin(); 3272 3273 // This node is about to morph, remove its old self from the CSE maps. 3274 RemoveNodeFromCSEMaps(U); 3275 3276 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; 3277 I != E; ++I) 3278 if (I->Val == From) { 3279 const SDOperand &ToOp = To[I->ResNo]; 3280 From->removeUser(U); 3281 *I = ToOp; 3282 ToOp.Val->addUser(U); 3283 } 3284 3285 // Now that we have modified U, add it back to the CSE maps. If it already 3286 // exists there, recursively merge the results together. 3287 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { 3288 ReplaceAllUsesWith(U, Existing, Deleted); 3289 // U is now dead. 3290 if (Deleted) Deleted->push_back(U); 3291 DeleteNodeNotInCSEMaps(U); 3292 } 3293 } 3294} 3295 3296/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving 3297/// uses of other values produced by From.Val alone. The Deleted vector is 3298/// handled the same was as for ReplaceAllUsesWith. 3299void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To, 3300 std::vector<SDNode*> *Deleted) { 3301 assert(From != To && "Cannot replace a value with itself"); 3302 // Handle the simple, trivial, case efficiently. 3303 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) { 3304 ReplaceAllUsesWith(From, To, Deleted); 3305 return; 3306 } 3307 3308 // Get all of the users of From.Val. We want these in a nice, 3309 // deterministically ordered and uniqued set, so we use a SmallSetVector. 3310 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end()); 3311 3312 std::vector<SDNode*> LocalDeletionVector; 3313 3314 // Pick a deletion vector to use. If the user specified one, use theirs, 3315 // otherwise use a local one. 3316 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector; 3317 while (!Users.empty()) { 3318 // We know that this user uses some value of From. If it is the right 3319 // value, update it. 3320 SDNode *User = Users.back(); 3321 Users.pop_back(); 3322 3323 // Scan for an operand that matches From. 3324 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands; 3325 for (; Op != E; ++Op) 3326 if (*Op == From) break; 3327 3328 // If there are no matches, the user must use some other result of From. 3329 if (Op == E) continue; 3330 3331 // Okay, we know this user needs to be updated. Remove its old self 3332 // from the CSE maps. 3333 RemoveNodeFromCSEMaps(User); 3334 3335 // Update all operands that match "From". 3336 for (; Op != E; ++Op) { 3337 if (*Op == From) { 3338 From.Val->removeUser(User); 3339 *Op = To; 3340 To.Val->addUser(User); 3341 } 3342 } 3343 3344 // Now that we have modified User, add it back to the CSE maps. If it 3345 // already exists there, recursively merge the results together. 3346 SDNode *Existing = AddNonLeafNodeToCSEMaps(User); 3347 if (!Existing) continue; // Continue on to next user. 3348 3349 // If there was already an existing matching node, use ReplaceAllUsesWith 3350 // to replace the dead one with the existing one. However, this can cause 3351 // recursive merging of other unrelated nodes down the line. The merging 3352 // can cause deletion of nodes that used the old value. In this case, 3353 // we have to be certain to remove them from the Users set. 3354 unsigned NumDeleted = DeleteVector->size(); 3355 ReplaceAllUsesWith(User, Existing, DeleteVector); 3356 3357 // User is now dead. 3358 DeleteVector->push_back(User); 3359 DeleteNodeNotInCSEMaps(User); 3360 3361 // We have to be careful here, because ReplaceAllUsesWith could have 3362 // deleted a user of From, which means there may be dangling pointers 3363 // in the "Users" setvector. Scan over the deleted node pointers and 3364 // remove them from the setvector. 3365 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i) 3366 Users.remove((*DeleteVector)[i]); 3367 3368 // If the user doesn't need the set of deleted elements, don't retain them 3369 // to the next loop iteration. 3370 if (Deleted == 0) 3371 LocalDeletionVector.clear(); 3372 } 3373} 3374 3375 3376/// AssignNodeIds - Assign a unique node id for each node in the DAG based on 3377/// their allnodes order. It returns the maximum id. 3378unsigned SelectionDAG::AssignNodeIds() { 3379 unsigned Id = 0; 3380 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){ 3381 SDNode *N = I; 3382 N->setNodeId(Id++); 3383 } 3384 return Id; 3385} 3386 3387/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG 3388/// based on their topological order. It returns the maximum id and a vector 3389/// of the SDNodes* in assigned order by reference. 3390unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) { 3391 unsigned DAGSize = AllNodes.size(); 3392 std::vector<unsigned> InDegree(DAGSize); 3393 std::vector<SDNode*> Sources; 3394 3395 // Use a two pass approach to avoid using a std::map which is slow. 3396 unsigned Id = 0; 3397 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){ 3398 SDNode *N = I; 3399 N->setNodeId(Id++); 3400 unsigned Degree = N->use_size(); 3401 InDegree[N->getNodeId()] = Degree; 3402 if (Degree == 0) 3403 Sources.push_back(N); 3404 } 3405 3406 TopOrder.clear(); 3407 while (!Sources.empty()) { 3408 SDNode *N = Sources.back(); 3409 Sources.pop_back(); 3410 TopOrder.push_back(N); 3411 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { 3412 SDNode *P = I->Val; 3413 unsigned Degree = --InDegree[P->getNodeId()]; 3414 if (Degree == 0) 3415 Sources.push_back(P); 3416 } 3417 } 3418 3419 // Second pass, assign the actual topological order as node ids. 3420 Id = 0; 3421 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end(); 3422 TI != TE; ++TI) 3423 (*TI)->setNodeId(Id++); 3424 3425 return Id; 3426} 3427 3428 3429 3430//===----------------------------------------------------------------------===// 3431// SDNode Class 3432//===----------------------------------------------------------------------===// 3433 3434// Out-of-line virtual method to give class a home. 3435void SDNode::ANCHOR() {} 3436void UnarySDNode::ANCHOR() {} 3437void BinarySDNode::ANCHOR() {} 3438void TernarySDNode::ANCHOR() {} 3439void HandleSDNode::ANCHOR() {} 3440void StringSDNode::ANCHOR() {} 3441void ConstantSDNode::ANCHOR() {} 3442void ConstantFPSDNode::ANCHOR() {} 3443void GlobalAddressSDNode::ANCHOR() {} 3444void FrameIndexSDNode::ANCHOR() {} 3445void JumpTableSDNode::ANCHOR() {} 3446void ConstantPoolSDNode::ANCHOR() {} 3447void BasicBlockSDNode::ANCHOR() {} 3448void SrcValueSDNode::ANCHOR() {} 3449void RegisterSDNode::ANCHOR() {} 3450void ExternalSymbolSDNode::ANCHOR() {} 3451void CondCodeSDNode::ANCHOR() {} 3452void VTSDNode::ANCHOR() {} 3453void LoadSDNode::ANCHOR() {} 3454void StoreSDNode::ANCHOR() {} 3455 3456HandleSDNode::~HandleSDNode() { 3457 SDVTList VTs = { 0, 0 }; 3458 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses. 3459} 3460 3461GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA, 3462 MVT::ValueType VT, int o) 3463 : SDNode(isa<GlobalVariable>(GA) && 3464 cast<GlobalVariable>(GA)->isThreadLocal() ? 3465 // Thread Local 3466 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) : 3467 // Non Thread Local 3468 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress), 3469 getSDVTList(VT)), Offset(o) { 3470 TheGlobal = const_cast<GlobalValue*>(GA); 3471} 3472 3473/// Profile - Gather unique data for the node. 3474/// 3475void SDNode::Profile(FoldingSetNodeID &ID) { 3476 AddNodeIDNode(ID, this); 3477} 3478 3479/// getValueTypeList - Return a pointer to the specified value type. 3480/// 3481MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { 3482 if (MVT::isExtendedVT(VT)) { 3483 static std::set<MVT::ValueType> EVTs; 3484 return (MVT::ValueType *)&(*EVTs.insert(VT).first); 3485 } else { 3486 static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; 3487 VTs[VT] = VT; 3488 return &VTs[VT]; 3489 } 3490} 3491 3492/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the 3493/// indicated value. This method ignores uses of other values defined by this 3494/// operation. 3495bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { 3496 assert(Value < getNumValues() && "Bad value!"); 3497 3498 // If there is only one value, this is easy. 3499 if (getNumValues() == 1) 3500 return use_size() == NUses; 3501 if (use_size() < NUses) return false; 3502 3503 SDOperand TheValue(const_cast<SDNode *>(this), Value); 3504 3505 SmallPtrSet<SDNode*, 32> UsersHandled; 3506 3507 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) { 3508 SDNode *User = *UI; 3509 if (User->getNumOperands() == 1 || 3510 UsersHandled.insert(User)) // First time we've seen this? 3511 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 3512 if (User->getOperand(i) == TheValue) { 3513 if (NUses == 0) 3514 return false; // too many uses 3515 --NUses; 3516 } 3517 } 3518 3519 // Found exactly the right number of uses? 3520 return NUses == 0; 3521} 3522 3523 3524/// hasAnyUseOfValue - Return true if there are any use of the indicated 3525/// value. This method ignores uses of other values defined by this operation. 3526bool SDNode::hasAnyUseOfValue(unsigned Value) const { 3527 assert(Value < getNumValues() && "Bad value!"); 3528 3529 if (use_size() == 0) return false; 3530 3531 SDOperand TheValue(const_cast<SDNode *>(this), Value); 3532 3533 SmallPtrSet<SDNode*, 32> UsersHandled; 3534 3535 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) { 3536 SDNode *User = *UI; 3537 if (User->getNumOperands() == 1 || 3538 UsersHandled.insert(User)) // First time we've seen this? 3539 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 3540 if (User->getOperand(i) == TheValue) { 3541 return true; 3542 } 3543 } 3544 3545 return false; 3546} 3547 3548 3549/// isOnlyUse - Return true if this node is the only use of N. 3550/// 3551bool SDNode::isOnlyUse(SDNode *N) const { 3552 bool Seen = false; 3553 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 3554 SDNode *User = *I; 3555 if (User == this) 3556 Seen = true; 3557 else 3558 return false; 3559 } 3560 3561 return Seen; 3562} 3563 3564/// isOperand - Return true if this node is an operand of N. 3565/// 3566bool SDOperand::isOperand(SDNode *N) const { 3567 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 3568 if (*this == N->getOperand(i)) 3569 return true; 3570 return false; 3571} 3572 3573bool SDNode::isOperand(SDNode *N) const { 3574 for (unsigned i = 0, e = N->NumOperands; i != e; ++i) 3575 if (this == N->OperandList[i].Val) 3576 return true; 3577 return false; 3578} 3579 3580static void findPredecessor(SDNode *N, const SDNode *P, bool &found, 3581 SmallPtrSet<SDNode *, 32> &Visited) { 3582 if (found || !Visited.insert(N)) 3583 return; 3584 3585 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) { 3586 SDNode *Op = N->getOperand(i).Val; 3587 if (Op == P) { 3588 found = true; 3589 return; 3590 } 3591 findPredecessor(Op, P, found, Visited); 3592 } 3593} 3594 3595/// isPredecessor - Return true if this node is a predecessor of N. This node 3596/// is either an operand of N or it can be reached by recursively traversing 3597/// up the operands. 3598/// NOTE: this is an expensive method. Use it carefully. 3599bool SDNode::isPredecessor(SDNode *N) const { 3600 SmallPtrSet<SDNode *, 32> Visited; 3601 bool found = false; 3602 findPredecessor(N, this, found, Visited); 3603 return found; 3604} 3605 3606uint64_t SDNode::getConstantOperandVal(unsigned Num) const { 3607 assert(Num < NumOperands && "Invalid child # of SDNode!"); 3608 return cast<ConstantSDNode>(OperandList[Num])->getValue(); 3609} 3610 3611std::string SDNode::getOperationName(const SelectionDAG *G) const { 3612 switch (getOpcode()) { 3613 default: 3614 if (getOpcode() < ISD::BUILTIN_OP_END) 3615 return "<<Unknown DAG Node>>"; 3616 else { 3617 if (G) { 3618 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) 3619 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) 3620 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); 3621 3622 TargetLowering &TLI = G->getTargetLoweringInfo(); 3623 const char *Name = 3624 TLI.getTargetNodeName(getOpcode()); 3625 if (Name) return Name; 3626 } 3627 3628 return "<<Unknown Target Node>>"; 3629 } 3630 3631 case ISD::PCMARKER: return "PCMarker"; 3632 case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; 3633 case ISD::SRCVALUE: return "SrcValue"; 3634 case ISD::EntryToken: return "EntryToken"; 3635 case ISD::TokenFactor: return "TokenFactor"; 3636 case ISD::AssertSext: return "AssertSext"; 3637 case ISD::AssertZext: return "AssertZext"; 3638 3639 case ISD::STRING: return "String"; 3640 case ISD::BasicBlock: return "BasicBlock"; 3641 case ISD::VALUETYPE: return "ValueType"; 3642 case ISD::Register: return "Register"; 3643 3644 case ISD::Constant: return "Constant"; 3645 case ISD::ConstantFP: return "ConstantFP"; 3646 case ISD::GlobalAddress: return "GlobalAddress"; 3647 case ISD::GlobalTLSAddress: return "GlobalTLSAddress"; 3648 case ISD::FrameIndex: return "FrameIndex"; 3649 case ISD::JumpTable: return "JumpTable"; 3650 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE"; 3651 case ISD::RETURNADDR: return "RETURNADDR"; 3652 case ISD::FRAMEADDR: return "FRAMEADDR"; 3653 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET"; 3654 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR"; 3655 case ISD::EHSELECTION: return "EHSELECTION"; 3656 case ISD::EH_RETURN: return "EH_RETURN"; 3657 case ISD::ConstantPool: return "ConstantPool"; 3658 case ISD::ExternalSymbol: return "ExternalSymbol"; 3659 case ISD::INTRINSIC_WO_CHAIN: { 3660 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue(); 3661 return Intrinsic::getName((Intrinsic::ID)IID); 3662 } 3663 case ISD::INTRINSIC_VOID: 3664 case ISD::INTRINSIC_W_CHAIN: { 3665 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue(); 3666 return Intrinsic::getName((Intrinsic::ID)IID); 3667 } 3668 3669 case ISD::BUILD_VECTOR: return "BUILD_VECTOR"; 3670 case ISD::TargetConstant: return "TargetConstant"; 3671 case ISD::TargetConstantFP:return "TargetConstantFP"; 3672 case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; 3673 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress"; 3674 case ISD::TargetFrameIndex: return "TargetFrameIndex"; 3675 case ISD::TargetJumpTable: return "TargetJumpTable"; 3676 case ISD::TargetConstantPool: return "TargetConstantPool"; 3677 case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; 3678 3679 case ISD::CopyToReg: return "CopyToReg"; 3680 case ISD::CopyFromReg: return "CopyFromReg"; 3681 case ISD::UNDEF: return "undef"; 3682 case ISD::MERGE_VALUES: return "merge_values"; 3683 case ISD::INLINEASM: return "inlineasm"; 3684 case ISD::LABEL: return "label"; 3685 case ISD::HANDLENODE: return "handlenode"; 3686 case ISD::FORMAL_ARGUMENTS: return "formal_arguments"; 3687 case ISD::CALL: return "call"; 3688 3689 // Unary operators 3690 case ISD::FABS: return "fabs"; 3691 case ISD::FNEG: return "fneg"; 3692 case ISD::FSQRT: return "fsqrt"; 3693 case ISD::FSIN: return "fsin"; 3694 case ISD::FCOS: return "fcos"; 3695 case ISD::FPOWI: return "fpowi"; 3696 case ISD::FPOW: return "fpow"; 3697 3698 // Binary operators 3699 case ISD::ADD: return "add"; 3700 case ISD::SUB: return "sub"; 3701 case ISD::MUL: return "mul"; 3702 case ISD::MULHU: return "mulhu"; 3703 case ISD::MULHS: return "mulhs"; 3704 case ISD::SDIV: return "sdiv"; 3705 case ISD::UDIV: return "udiv"; 3706 case ISD::SREM: return "srem"; 3707 case ISD::UREM: return "urem"; 3708 case ISD::SMUL_LOHI: return "smul_lohi"; 3709 case ISD::UMUL_LOHI: return "umul_lohi"; 3710 case ISD::SDIVREM: return "sdivrem"; 3711 case ISD::UDIVREM: return "divrem"; 3712 case ISD::AND: return "and"; 3713 case ISD::OR: return "or"; 3714 case ISD::XOR: return "xor"; 3715 case ISD::SHL: return "shl"; 3716 case ISD::SRA: return "sra"; 3717 case ISD::SRL: return "srl"; 3718 case ISD::ROTL: return "rotl"; 3719 case ISD::ROTR: return "rotr"; 3720 case ISD::FADD: return "fadd"; 3721 case ISD::FSUB: return "fsub"; 3722 case ISD::FMUL: return "fmul"; 3723 case ISD::FDIV: return "fdiv"; 3724 case ISD::FREM: return "frem"; 3725 case ISD::FCOPYSIGN: return "fcopysign"; 3726 3727 case ISD::SETCC: return "setcc"; 3728 case ISD::SELECT: return "select"; 3729 case ISD::SELECT_CC: return "select_cc"; 3730 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt"; 3731 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt"; 3732 case ISD::CONCAT_VECTORS: return "concat_vectors"; 3733 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector"; 3734 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector"; 3735 case ISD::VECTOR_SHUFFLE: return "vector_shuffle"; 3736 case ISD::CARRY_FALSE: return "carry_false"; 3737 case ISD::ADDC: return "addc"; 3738 case ISD::ADDE: return "adde"; 3739 case ISD::SUBC: return "subc"; 3740 case ISD::SUBE: return "sube"; 3741 case ISD::SHL_PARTS: return "shl_parts"; 3742 case ISD::SRA_PARTS: return "sra_parts"; 3743 case ISD::SRL_PARTS: return "srl_parts"; 3744 3745 case ISD::EXTRACT_SUBREG: return "extract_subreg"; 3746 case ISD::INSERT_SUBREG: return "insert_subreg"; 3747 3748 // Conversion operators. 3749 case ISD::SIGN_EXTEND: return "sign_extend"; 3750 case ISD::ZERO_EXTEND: return "zero_extend"; 3751 case ISD::ANY_EXTEND: return "any_extend"; 3752 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; 3753 case ISD::TRUNCATE: return "truncate"; 3754 case ISD::FP_ROUND: return "fp_round"; 3755 case ISD::FLT_ROUNDS: return "flt_rounds"; 3756 case ISD::FP_ROUND_INREG: return "fp_round_inreg"; 3757 case ISD::FP_EXTEND: return "fp_extend"; 3758 3759 case ISD::SINT_TO_FP: return "sint_to_fp"; 3760 case ISD::UINT_TO_FP: return "uint_to_fp"; 3761 case ISD::FP_TO_SINT: return "fp_to_sint"; 3762 case ISD::FP_TO_UINT: return "fp_to_uint"; 3763 case ISD::BIT_CONVERT: return "bit_convert"; 3764 3765 // Control flow instructions 3766 case ISD::BR: return "br"; 3767 case ISD::BRIND: return "brind"; 3768 case ISD::BR_JT: return "br_jt"; 3769 case ISD::BRCOND: return "brcond"; 3770 case ISD::BR_CC: return "br_cc"; 3771 case ISD::RET: return "ret"; 3772 case ISD::CALLSEQ_START: return "callseq_start"; 3773 case ISD::CALLSEQ_END: return "callseq_end"; 3774 3775 // Other operators 3776 case ISD::LOAD: return "load"; 3777 case ISD::STORE: return "store"; 3778 case ISD::VAARG: return "vaarg"; 3779 case ISD::VACOPY: return "vacopy"; 3780 case ISD::VAEND: return "vaend"; 3781 case ISD::VASTART: return "vastart"; 3782 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; 3783 case ISD::EXTRACT_ELEMENT: return "extract_element"; 3784 case ISD::BUILD_PAIR: return "build_pair"; 3785 case ISD::STACKSAVE: return "stacksave"; 3786 case ISD::STACKRESTORE: return "stackrestore"; 3787 3788 // Block memory operations. 3789 case ISD::MEMSET: return "memset"; 3790 case ISD::MEMCPY: return "memcpy"; 3791 case ISD::MEMMOVE: return "memmove"; 3792 3793 // Bit manipulation 3794 case ISD::BSWAP: return "bswap"; 3795 case ISD::CTPOP: return "ctpop"; 3796 case ISD::CTTZ: return "cttz"; 3797 case ISD::CTLZ: return "ctlz"; 3798 3799 // Debug info 3800 case ISD::LOCATION: return "location"; 3801 case ISD::DEBUG_LOC: return "debug_loc"; 3802 3803 // Trampolines 3804 case ISD::TRAMPOLINE: return "trampoline"; 3805 3806 case ISD::CONDCODE: 3807 switch (cast<CondCodeSDNode>(this)->get()) { 3808 default: assert(0 && "Unknown setcc condition!"); 3809 case ISD::SETOEQ: return "setoeq"; 3810 case ISD::SETOGT: return "setogt"; 3811 case ISD::SETOGE: return "setoge"; 3812 case ISD::SETOLT: return "setolt"; 3813 case ISD::SETOLE: return "setole"; 3814 case ISD::SETONE: return "setone"; 3815 3816 case ISD::SETO: return "seto"; 3817 case ISD::SETUO: return "setuo"; 3818 case ISD::SETUEQ: return "setue"; 3819 case ISD::SETUGT: return "setugt"; 3820 case ISD::SETUGE: return "setuge"; 3821 case ISD::SETULT: return "setult"; 3822 case ISD::SETULE: return "setule"; 3823 case ISD::SETUNE: return "setune"; 3824 3825 case ISD::SETEQ: return "seteq"; 3826 case ISD::SETGT: return "setgt"; 3827 case ISD::SETGE: return "setge"; 3828 case ISD::SETLT: return "setlt"; 3829 case ISD::SETLE: return "setle"; 3830 case ISD::SETNE: return "setne"; 3831 } 3832 } 3833} 3834 3835const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) { 3836 switch (AM) { 3837 default: 3838 return ""; 3839 case ISD::PRE_INC: 3840 return "<pre-inc>"; 3841 case ISD::PRE_DEC: 3842 return "<pre-dec>"; 3843 case ISD::POST_INC: 3844 return "<post-inc>"; 3845 case ISD::POST_DEC: 3846 return "<post-dec>"; 3847 } 3848} 3849 3850void SDNode::dump() const { dump(0); } 3851void SDNode::dump(const SelectionDAG *G) const { 3852 cerr << (void*)this << ": "; 3853 3854 for (unsigned i = 0, e = getNumValues(); i != e; ++i) { 3855 if (i) cerr << ","; 3856 if (getValueType(i) == MVT::Other) 3857 cerr << "ch"; 3858 else 3859 cerr << MVT::getValueTypeString(getValueType(i)); 3860 } 3861 cerr << " = " << getOperationName(G); 3862 3863 cerr << " "; 3864 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 3865 if (i) cerr << ", "; 3866 cerr << (void*)getOperand(i).Val; 3867 if (unsigned RN = getOperand(i).ResNo) 3868 cerr << ":" << RN; 3869 } 3870 3871 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) { 3872 SDNode *Mask = getOperand(2).Val; 3873 cerr << "<"; 3874 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) { 3875 if (i) cerr << ","; 3876 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF) 3877 cerr << "u"; 3878 else 3879 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue(); 3880 } 3881 cerr << ">"; 3882 } 3883 3884 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { 3885 cerr << "<" << CSDN->getValue() << ">"; 3886 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { 3887 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle) 3888 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">"; 3889 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble) 3890 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">"; 3891 else { 3892 cerr << "<APFloat("; 3893 CSDN->getValueAPF().convertToAPInt().dump(); 3894 cerr << ")>"; 3895 } 3896 } else if (const GlobalAddressSDNode *GADN = 3897 dyn_cast<GlobalAddressSDNode>(this)) { 3898 int offset = GADN->getOffset(); 3899 cerr << "<"; 3900 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">"; 3901 if (offset > 0) 3902 cerr << " + " << offset; 3903 else 3904 cerr << " " << offset; 3905 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { 3906 cerr << "<" << FIDN->getIndex() << ">"; 3907 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) { 3908 cerr << "<" << JTDN->getIndex() << ">"; 3909 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ 3910 int offset = CP->getOffset(); 3911 if (CP->isMachineConstantPoolEntry()) 3912 cerr << "<" << *CP->getMachineCPVal() << ">"; 3913 else 3914 cerr << "<" << *CP->getConstVal() << ">"; 3915 if (offset > 0) 3916 cerr << " + " << offset; 3917 else 3918 cerr << " " << offset; 3919 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { 3920 cerr << "<"; 3921 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 3922 if (LBB) 3923 cerr << LBB->getName() << " "; 3924 cerr << (const void*)BBDN->getBasicBlock() << ">"; 3925 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { 3926 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) { 3927 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); 3928 } else { 3929 cerr << " #" << R->getReg(); 3930 } 3931 } else if (const ExternalSymbolSDNode *ES = 3932 dyn_cast<ExternalSymbolSDNode>(this)) { 3933 cerr << "'" << ES->getSymbol() << "'"; 3934 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { 3935 if (M->getValue()) 3936 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; 3937 else 3938 cerr << "<null:" << M->getOffset() << ">"; 3939 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { 3940 cerr << ":" << MVT::getValueTypeString(N->getVT()); 3941 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) { 3942 const Value *SrcValue = LD->getSrcValue(); 3943 int SrcOffset = LD->getSrcValueOffset(); 3944 cerr << " <"; 3945 if (SrcValue) 3946 cerr << SrcValue; 3947 else 3948 cerr << "null"; 3949 cerr << ":" << SrcOffset << ">"; 3950 3951 bool doExt = true; 3952 switch (LD->getExtensionType()) { 3953 default: doExt = false; break; 3954 case ISD::EXTLOAD: 3955 cerr << " <anyext "; 3956 break; 3957 case ISD::SEXTLOAD: 3958 cerr << " <sext "; 3959 break; 3960 case ISD::ZEXTLOAD: 3961 cerr << " <zext "; 3962 break; 3963 } 3964 if (doExt) 3965 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">"; 3966 3967 const char *AM = getIndexedModeName(LD->getAddressingMode()); 3968 if (*AM) 3969 cerr << " " << AM; 3970 if (LD->isVolatile()) 3971 cerr << " <volatile>"; 3972 cerr << " alignment=" << LD->getAlignment(); 3973 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) { 3974 const Value *SrcValue = ST->getSrcValue(); 3975 int SrcOffset = ST->getSrcValueOffset(); 3976 cerr << " <"; 3977 if (SrcValue) 3978 cerr << SrcValue; 3979 else 3980 cerr << "null"; 3981 cerr << ":" << SrcOffset << ">"; 3982 3983 if (ST->isTruncatingStore()) 3984 cerr << " <trunc " 3985 << MVT::getValueTypeString(ST->getStoredVT()) << ">"; 3986 3987 const char *AM = getIndexedModeName(ST->getAddressingMode()); 3988 if (*AM) 3989 cerr << " " << AM; 3990 if (ST->isVolatile()) 3991 cerr << " <volatile>"; 3992 cerr << " alignment=" << ST->getAlignment(); 3993 } 3994} 3995 3996static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { 3997 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 3998 if (N->getOperand(i).Val->hasOneUse()) 3999 DumpNodes(N->getOperand(i).Val, indent+2, G); 4000 else 4001 cerr << "\n" << std::string(indent+2, ' ') 4002 << (void*)N->getOperand(i).Val << ": <multiple use>"; 4003 4004 4005 cerr << "\n" << std::string(indent, ' '); 4006 N->dump(G); 4007} 4008 4009void SelectionDAG::dump() const { 4010 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; 4011 std::vector<const SDNode*> Nodes; 4012 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); 4013 I != E; ++I) 4014 Nodes.push_back(I); 4015 4016 std::sort(Nodes.begin(), Nodes.end()); 4017 4018 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 4019 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) 4020 DumpNodes(Nodes[i], 2, this); 4021 } 4022 4023 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this); 4024 4025 cerr << "\n\n"; 4026} 4027 4028const Type *ConstantPoolSDNode::getType() const { 4029 if (isMachineConstantPoolEntry()) 4030 return Val.MachineCPVal->getType(); 4031 return Val.ConstVal->getType(); 4032} 4033