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