/3rd_party/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
C++ | 3598 lines | 2441 code | 551 blank | 606 comment | 992 complexity | 50d18ca4d9763c33a656f73137f5068a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, JSON, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, BSD-2-Clause
Large files files are truncated, but you can click here to view the full file
1//===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the CodeGenDAGPatterns class, which is used to read and 11// represent the patterns present in a .td file for instructions. 12// 13//===----------------------------------------------------------------------===// 14 15#include "CodeGenDAGPatterns.h" 16#include "llvm/ADT/STLExtras.h" 17#include "llvm/ADT/StringExtras.h" 18#include "llvm/ADT/Twine.h" 19#include "llvm/Support/Debug.h" 20#include "llvm/Support/ErrorHandling.h" 21#include "llvm/TableGen/Error.h" 22#include "llvm/TableGen/Record.h" 23#include <algorithm> 24#include <cstdio> 25#include <set> 26using namespace llvm; 27 28//===----------------------------------------------------------------------===// 29// EEVT::TypeSet Implementation 30//===----------------------------------------------------------------------===// 31 32static inline bool isInteger(MVT::SimpleValueType VT) { 33 return MVT(VT).isInteger(); 34} 35static inline bool isFloatingPoint(MVT::SimpleValueType VT) { 36 return MVT(VT).isFloatingPoint(); 37} 38static inline bool isVector(MVT::SimpleValueType VT) { 39 return MVT(VT).isVector(); 40} 41static inline bool isScalar(MVT::SimpleValueType VT) { 42 return !MVT(VT).isVector(); 43} 44 45EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) { 46 if (VT == MVT::iAny) 47 EnforceInteger(TP); 48 else if (VT == MVT::fAny) 49 EnforceFloatingPoint(TP); 50 else if (VT == MVT::vAny) 51 EnforceVector(TP); 52 else { 53 assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR || 54 VT == MVT::iPTRAny) && "Not a concrete type!"); 55 TypeVec.push_back(VT); 56 } 57} 58 59 60EEVT::TypeSet::TypeSet(ArrayRef<MVT::SimpleValueType> VTList) { 61 assert(!VTList.empty() && "empty list?"); 62 TypeVec.append(VTList.begin(), VTList.end()); 63 64 if (!VTList.empty()) 65 assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny && 66 VTList[0] != MVT::fAny); 67 68 // Verify no duplicates. 69 array_pod_sort(TypeVec.begin(), TypeVec.end()); 70 assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end()); 71} 72 73/// FillWithPossibleTypes - Set to all legal types and return true, only valid 74/// on completely unknown type sets. 75bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP, 76 bool (*Pred)(MVT::SimpleValueType), 77 const char *PredicateName) { 78 assert(isCompletelyUnknown()); 79 ArrayRef<MVT::SimpleValueType> LegalTypes = 80 TP.getDAGPatterns().getTargetInfo().getLegalValueTypes(); 81 82 if (TP.hasError()) 83 return false; 84 85 for (unsigned i = 0, e = LegalTypes.size(); i != e; ++i) 86 if (Pred == 0 || Pred(LegalTypes[i])) 87 TypeVec.push_back(LegalTypes[i]); 88 89 // If we have nothing that matches the predicate, bail out. 90 if (TypeVec.empty()) { 91 TP.error("Type inference contradiction found, no " + 92 std::string(PredicateName) + " types found"); 93 return false; 94 } 95 // No need to sort with one element. 96 if (TypeVec.size() == 1) return true; 97 98 // Remove duplicates. 99 array_pod_sort(TypeVec.begin(), TypeVec.end()); 100 TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end()); 101 102 return true; 103} 104 105/// hasIntegerTypes - Return true if this TypeSet contains iAny or an 106/// integer value type. 107bool EEVT::TypeSet::hasIntegerTypes() const { 108 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 109 if (isInteger(TypeVec[i])) 110 return true; 111 return false; 112} 113 114/// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or 115/// a floating point value type. 116bool EEVT::TypeSet::hasFloatingPointTypes() const { 117 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 118 if (isFloatingPoint(TypeVec[i])) 119 return true; 120 return false; 121} 122 123/// hasVectorTypes - Return true if this TypeSet contains a vAny or a vector 124/// value type. 125bool EEVT::TypeSet::hasVectorTypes() const { 126 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 127 if (isVector(TypeVec[i])) 128 return true; 129 return false; 130} 131 132 133std::string EEVT::TypeSet::getName() const { 134 if (TypeVec.empty()) return "<empty>"; 135 136 std::string Result; 137 138 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) { 139 std::string VTName = llvm::getEnumName(TypeVec[i]); 140 // Strip off MVT:: prefix if present. 141 if (VTName.substr(0,5) == "MVT::") 142 VTName = VTName.substr(5); 143 if (i) Result += ':'; 144 Result += VTName; 145 } 146 147 if (TypeVec.size() == 1) 148 return Result; 149 return "{" + Result + "}"; 150} 151 152/// MergeInTypeInfo - This merges in type information from the specified 153/// argument. If 'this' changes, it returns true. If the two types are 154/// contradictory (e.g. merge f32 into i32) then this flags an error. 155bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){ 156 if (InVT.isCompletelyUnknown() || *this == InVT || TP.hasError()) 157 return false; 158 159 if (isCompletelyUnknown()) { 160 *this = InVT; 161 return true; 162 } 163 164 assert(TypeVec.size() >= 1 && InVT.TypeVec.size() >= 1 && "No unknowns"); 165 166 // Handle the abstract cases, seeing if we can resolve them better. 167 switch (TypeVec[0]) { 168 default: break; 169 case MVT::iPTR: 170 case MVT::iPTRAny: 171 if (InVT.hasIntegerTypes()) { 172 EEVT::TypeSet InCopy(InVT); 173 InCopy.EnforceInteger(TP); 174 InCopy.EnforceScalar(TP); 175 176 if (InCopy.isConcrete()) { 177 // If the RHS has one integer type, upgrade iPTR to i32. 178 TypeVec[0] = InVT.TypeVec[0]; 179 return true; 180 } 181 182 // If the input has multiple scalar integers, this doesn't add any info. 183 if (!InCopy.isCompletelyUnknown()) 184 return false; 185 } 186 break; 187 } 188 189 // If the input constraint is iAny/iPTR and this is an integer type list, 190 // remove non-integer types from the list. 191 if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && 192 hasIntegerTypes()) { 193 bool MadeChange = EnforceInteger(TP); 194 195 // If we're merging in iPTR/iPTRAny and the node currently has a list of 196 // multiple different integer types, replace them with a single iPTR. 197 if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && 198 TypeVec.size() != 1) { 199 TypeVec.resize(1); 200 TypeVec[0] = InVT.TypeVec[0]; 201 MadeChange = true; 202 } 203 204 return MadeChange; 205 } 206 207 // If this is a type list and the RHS is a typelist as well, eliminate entries 208 // from this list that aren't in the other one. 209 bool MadeChange = false; 210 TypeSet InputSet(*this); 211 212 for (unsigned i = 0; i != TypeVec.size(); ++i) { 213 bool InInVT = false; 214 for (unsigned j = 0, e = InVT.TypeVec.size(); j != e; ++j) 215 if (TypeVec[i] == InVT.TypeVec[j]) { 216 InInVT = true; 217 break; 218 } 219 220 if (InInVT) continue; 221 TypeVec.erase(TypeVec.begin()+i--); 222 MadeChange = true; 223 } 224 225 // If we removed all of our types, we have a type contradiction. 226 if (!TypeVec.empty()) 227 return MadeChange; 228 229 // FIXME: Really want an SMLoc here! 230 TP.error("Type inference contradiction found, merging '" + 231 InVT.getName() + "' into '" + InputSet.getName() + "'"); 232 return false; 233} 234 235/// EnforceInteger - Remove all non-integer types from this set. 236bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) { 237 if (TP.hasError()) 238 return false; 239 // If we know nothing, then get the full set. 240 if (TypeVec.empty()) 241 return FillWithPossibleTypes(TP, isInteger, "integer"); 242 if (!hasFloatingPointTypes()) 243 return false; 244 245 TypeSet InputSet(*this); 246 247 // Filter out all the fp types. 248 for (unsigned i = 0; i != TypeVec.size(); ++i) 249 if (!isInteger(TypeVec[i])) 250 TypeVec.erase(TypeVec.begin()+i--); 251 252 if (TypeVec.empty()) { 253 TP.error("Type inference contradiction found, '" + 254 InputSet.getName() + "' needs to be integer"); 255 return false; 256 } 257 return true; 258} 259 260/// EnforceFloatingPoint - Remove all integer types from this set. 261bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) { 262 if (TP.hasError()) 263 return false; 264 // If we know nothing, then get the full set. 265 if (TypeVec.empty()) 266 return FillWithPossibleTypes(TP, isFloatingPoint, "floating point"); 267 268 if (!hasIntegerTypes()) 269 return false; 270 271 TypeSet InputSet(*this); 272 273 // Filter out all the fp types. 274 for (unsigned i = 0; i != TypeVec.size(); ++i) 275 if (!isFloatingPoint(TypeVec[i])) 276 TypeVec.erase(TypeVec.begin()+i--); 277 278 if (TypeVec.empty()) { 279 TP.error("Type inference contradiction found, '" + 280 InputSet.getName() + "' needs to be floating point"); 281 return false; 282 } 283 return true; 284} 285 286/// EnforceScalar - Remove all vector types from this. 287bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) { 288 if (TP.hasError()) 289 return false; 290 291 // If we know nothing, then get the full set. 292 if (TypeVec.empty()) 293 return FillWithPossibleTypes(TP, isScalar, "scalar"); 294 295 if (!hasVectorTypes()) 296 return false; 297 298 TypeSet InputSet(*this); 299 300 // Filter out all the vector types. 301 for (unsigned i = 0; i != TypeVec.size(); ++i) 302 if (!isScalar(TypeVec[i])) 303 TypeVec.erase(TypeVec.begin()+i--); 304 305 if (TypeVec.empty()) { 306 TP.error("Type inference contradiction found, '" + 307 InputSet.getName() + "' needs to be scalar"); 308 return false; 309 } 310 return true; 311} 312 313/// EnforceVector - Remove all vector types from this. 314bool EEVT::TypeSet::EnforceVector(TreePattern &TP) { 315 if (TP.hasError()) 316 return false; 317 318 // If we know nothing, then get the full set. 319 if (TypeVec.empty()) 320 return FillWithPossibleTypes(TP, isVector, "vector"); 321 322 TypeSet InputSet(*this); 323 bool MadeChange = false; 324 325 // Filter out all the scalar types. 326 for (unsigned i = 0; i != TypeVec.size(); ++i) 327 if (!isVector(TypeVec[i])) { 328 TypeVec.erase(TypeVec.begin()+i--); 329 MadeChange = true; 330 } 331 332 if (TypeVec.empty()) { 333 TP.error("Type inference contradiction found, '" + 334 InputSet.getName() + "' needs to be a vector"); 335 return false; 336 } 337 return MadeChange; 338} 339 340 341 342/// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update 343/// this an other based on this information. 344bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) { 345 if (TP.hasError()) 346 return false; 347 348 // Both operands must be integer or FP, but we don't care which. 349 bool MadeChange = false; 350 351 if (isCompletelyUnknown()) 352 MadeChange = FillWithPossibleTypes(TP); 353 354 if (Other.isCompletelyUnknown()) 355 MadeChange = Other.FillWithPossibleTypes(TP); 356 357 // If one side is known to be integer or known to be FP but the other side has 358 // no information, get at least the type integrality info in there. 359 if (!hasFloatingPointTypes()) 360 MadeChange |= Other.EnforceInteger(TP); 361 else if (!hasIntegerTypes()) 362 MadeChange |= Other.EnforceFloatingPoint(TP); 363 if (!Other.hasFloatingPointTypes()) 364 MadeChange |= EnforceInteger(TP); 365 else if (!Other.hasIntegerTypes()) 366 MadeChange |= EnforceFloatingPoint(TP); 367 368 assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() && 369 "Should have a type list now"); 370 371 // If one contains vectors but the other doesn't pull vectors out. 372 if (!hasVectorTypes()) 373 MadeChange |= Other.EnforceScalar(TP); 374 if (!hasVectorTypes()) 375 MadeChange |= EnforceScalar(TP); 376 377 if (TypeVec.size() == 1 && Other.TypeVec.size() == 1) { 378 // If we are down to concrete types, this code does not currently 379 // handle nodes which have multiple types, where some types are 380 // integer, and some are fp. Assert that this is not the case. 381 assert(!(hasIntegerTypes() && hasFloatingPointTypes()) && 382 !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) && 383 "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); 384 385 // Otherwise, if these are both vector types, either this vector 386 // must have a larger bitsize than the other, or this element type 387 // must be larger than the other. 388 MVT Type(TypeVec[0]); 389 MVT OtherType(Other.TypeVec[0]); 390 391 if (hasVectorTypes() && Other.hasVectorTypes()) { 392 if (Type.getSizeInBits() >= OtherType.getSizeInBits()) 393 if (Type.getVectorElementType().getSizeInBits() 394 >= OtherType.getVectorElementType().getSizeInBits()) { 395 TP.error("Type inference contradiction found, '" + 396 getName() + "' element type not smaller than '" + 397 Other.getName() +"'!"); 398 return false; 399 } 400 } else 401 // For scalar types, the bitsize of this type must be larger 402 // than that of the other. 403 if (Type.getSizeInBits() >= OtherType.getSizeInBits()) { 404 TP.error("Type inference contradiction found, '" + 405 getName() + "' is not smaller than '" + 406 Other.getName() +"'!"); 407 return false; 408 } 409 } 410 411 412 // Handle int and fp as disjoint sets. This won't work for patterns 413 // that have mixed fp/int types but those are likely rare and would 414 // not have been accepted by this code previously. 415 416 // Okay, find the smallest type from the current set and remove it from the 417 // largest set. 418 MVT::SimpleValueType SmallestInt = MVT::LAST_VALUETYPE; 419 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 420 if (isInteger(TypeVec[i])) { 421 SmallestInt = TypeVec[i]; 422 break; 423 } 424 for (unsigned i = 1, e = TypeVec.size(); i != e; ++i) 425 if (isInteger(TypeVec[i]) && TypeVec[i] < SmallestInt) 426 SmallestInt = TypeVec[i]; 427 428 MVT::SimpleValueType SmallestFP = MVT::LAST_VALUETYPE; 429 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 430 if (isFloatingPoint(TypeVec[i])) { 431 SmallestFP = TypeVec[i]; 432 break; 433 } 434 for (unsigned i = 1, e = TypeVec.size(); i != e; ++i) 435 if (isFloatingPoint(TypeVec[i]) && TypeVec[i] < SmallestFP) 436 SmallestFP = TypeVec[i]; 437 438 int OtherIntSize = 0; 439 int OtherFPSize = 0; 440 for (SmallVectorImpl<MVT::SimpleValueType>::iterator TVI = 441 Other.TypeVec.begin(); 442 TVI != Other.TypeVec.end(); 443 /* NULL */) { 444 if (isInteger(*TVI)) { 445 ++OtherIntSize; 446 if (*TVI == SmallestInt) { 447 TVI = Other.TypeVec.erase(TVI); 448 --OtherIntSize; 449 MadeChange = true; 450 continue; 451 } 452 } else if (isFloatingPoint(*TVI)) { 453 ++OtherFPSize; 454 if (*TVI == SmallestFP) { 455 TVI = Other.TypeVec.erase(TVI); 456 --OtherFPSize; 457 MadeChange = true; 458 continue; 459 } 460 } 461 ++TVI; 462 } 463 464 // If this is the only type in the large set, the constraint can never be 465 // satisfied. 466 if ((Other.hasIntegerTypes() && OtherIntSize == 0) || 467 (Other.hasFloatingPointTypes() && OtherFPSize == 0)) { 468 TP.error("Type inference contradiction found, '" + 469 Other.getName() + "' has nothing larger than '" + getName() +"'!"); 470 return false; 471 } 472 473 // Okay, find the largest type in the Other set and remove it from the 474 // current set. 475 MVT::SimpleValueType LargestInt = MVT::Other; 476 for (unsigned i = 0, e = Other.TypeVec.size(); i != e; ++i) 477 if (isInteger(Other.TypeVec[i])) { 478 LargestInt = Other.TypeVec[i]; 479 break; 480 } 481 for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i) 482 if (isInteger(Other.TypeVec[i]) && Other.TypeVec[i] > LargestInt) 483 LargestInt = Other.TypeVec[i]; 484 485 MVT::SimpleValueType LargestFP = MVT::Other; 486 for (unsigned i = 0, e = Other.TypeVec.size(); i != e; ++i) 487 if (isFloatingPoint(Other.TypeVec[i])) { 488 LargestFP = Other.TypeVec[i]; 489 break; 490 } 491 for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i) 492 if (isFloatingPoint(Other.TypeVec[i]) && Other.TypeVec[i] > LargestFP) 493 LargestFP = Other.TypeVec[i]; 494 495 int IntSize = 0; 496 int FPSize = 0; 497 for (SmallVectorImpl<MVT::SimpleValueType>::iterator TVI = 498 TypeVec.begin(); 499 TVI != TypeVec.end(); 500 /* NULL */) { 501 if (isInteger(*TVI)) { 502 ++IntSize; 503 if (*TVI == LargestInt) { 504 TVI = TypeVec.erase(TVI); 505 --IntSize; 506 MadeChange = true; 507 continue; 508 } 509 } else if (isFloatingPoint(*TVI)) { 510 ++FPSize; 511 if (*TVI == LargestFP) { 512 TVI = TypeVec.erase(TVI); 513 --FPSize; 514 MadeChange = true; 515 continue; 516 } 517 } 518 ++TVI; 519 } 520 521 // If this is the only type in the small set, the constraint can never be 522 // satisfied. 523 if ((hasIntegerTypes() && IntSize == 0) || 524 (hasFloatingPointTypes() && FPSize == 0)) { 525 TP.error("Type inference contradiction found, '" + 526 getName() + "' has nothing smaller than '" + Other.getName()+"'!"); 527 return false; 528 } 529 530 return MadeChange; 531} 532 533/// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type 534/// whose element is specified by VTOperand. 535bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand, 536 TreePattern &TP) { 537 if (TP.hasError()) 538 return false; 539 540 // "This" must be a vector and "VTOperand" must be a scalar. 541 bool MadeChange = false; 542 MadeChange |= EnforceVector(TP); 543 MadeChange |= VTOperand.EnforceScalar(TP); 544 545 // If we know the vector type, it forces the scalar to agree. 546 if (isConcrete()) { 547 MVT IVT = getConcrete(); 548 IVT = IVT.getVectorElementType(); 549 return MadeChange | 550 VTOperand.MergeInTypeInfo(IVT.SimpleTy, TP); 551 } 552 553 // If the scalar type is known, filter out vector types whose element types 554 // disagree. 555 if (!VTOperand.isConcrete()) 556 return MadeChange; 557 558 MVT::SimpleValueType VT = VTOperand.getConcrete(); 559 560 TypeSet InputSet(*this); 561 562 // Filter out all the types which don't have the right element type. 563 for (unsigned i = 0; i != TypeVec.size(); ++i) { 564 assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 565 if (MVT(TypeVec[i]).getVectorElementType().SimpleTy != VT) { 566 TypeVec.erase(TypeVec.begin()+i--); 567 MadeChange = true; 568 } 569 } 570 571 if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 572 TP.error("Type inference contradiction found, forcing '" + 573 InputSet.getName() + "' to have a vector element"); 574 return false; 575 } 576 return MadeChange; 577} 578 579/// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to be a 580/// vector type specified by VTOperand. 581bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand, 582 TreePattern &TP) { 583 // "This" must be a vector and "VTOperand" must be a vector. 584 bool MadeChange = false; 585 MadeChange |= EnforceVector(TP); 586 MadeChange |= VTOperand.EnforceVector(TP); 587 588 // "This" must be larger than "VTOperand." 589 MadeChange |= VTOperand.EnforceSmallerThan(*this, TP); 590 591 // If we know the vector type, it forces the scalar types to agree. 592 if (isConcrete()) { 593 MVT IVT = getConcrete(); 594 IVT = IVT.getVectorElementType(); 595 596 EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP); 597 MadeChange |= VTOperand.EnforceVectorEltTypeIs(EltTypeSet, TP); 598 } else if (VTOperand.isConcrete()) { 599 MVT IVT = VTOperand.getConcrete(); 600 IVT = IVT.getVectorElementType(); 601 602 EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP); 603 MadeChange |= EnforceVectorEltTypeIs(EltTypeSet, TP); 604 } 605 606 return MadeChange; 607} 608 609//===----------------------------------------------------------------------===// 610// Helpers for working with extended types. 611 612/// Dependent variable map for CodeGenDAGPattern variant generation 613typedef std::map<std::string, int> DepVarMap; 614 615/// Const iterator shorthand for DepVarMap 616typedef DepVarMap::const_iterator DepVarMap_citer; 617 618static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) { 619 if (N->isLeaf()) { 620 if (isa<DefInit>(N->getLeafValue())) 621 DepMap[N->getName()]++; 622 } else { 623 for (size_t i = 0, e = N->getNumChildren(); i != e; ++i) 624 FindDepVarsOf(N->getChild(i), DepMap); 625 } 626} 627 628/// Find dependent variables within child patterns 629static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) { 630 DepVarMap depcounts; 631 FindDepVarsOf(N, depcounts); 632 for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) { 633 if (i->second > 1) // std::pair<std::string, int> 634 DepVars.insert(i->first); 635 } 636} 637 638#ifndef NDEBUG 639/// Dump the dependent variable set: 640static void DumpDepVars(MultipleUseVarSet &DepVars) { 641 if (DepVars.empty()) { 642 DEBUG(errs() << "<empty set>"); 643 } else { 644 DEBUG(errs() << "[ "); 645 for (MultipleUseVarSet::const_iterator i = DepVars.begin(), 646 e = DepVars.end(); i != e; ++i) { 647 DEBUG(errs() << (*i) << " "); 648 } 649 DEBUG(errs() << "]"); 650 } 651} 652#endif 653 654 655//===----------------------------------------------------------------------===// 656// TreePredicateFn Implementation 657//===----------------------------------------------------------------------===// 658 659/// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag. 660TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) { 661 assert((getPredCode().empty() || getImmCode().empty()) && 662 ".td file corrupt: can't have a node predicate *and* an imm predicate"); 663} 664 665std::string TreePredicateFn::getPredCode() const { 666 return PatFragRec->getRecord()->getValueAsString("PredicateCode"); 667} 668 669std::string TreePredicateFn::getImmCode() const { 670 return PatFragRec->getRecord()->getValueAsString("ImmediateCode"); 671} 672 673 674/// isAlwaysTrue - Return true if this is a noop predicate. 675bool TreePredicateFn::isAlwaysTrue() const { 676 return getPredCode().empty() && getImmCode().empty(); 677} 678 679/// Return the name to use in the generated code to reference this, this is 680/// "Predicate_foo" if from a pattern fragment "foo". 681std::string TreePredicateFn::getFnName() const { 682 return "Predicate_" + PatFragRec->getRecord()->getName(); 683} 684 685/// getCodeToRunOnSDNode - Return the code for the function body that 686/// evaluates this predicate. The argument is expected to be in "Node", 687/// not N. This handles casting and conversion to a concrete node type as 688/// appropriate. 689std::string TreePredicateFn::getCodeToRunOnSDNode() const { 690 // Handle immediate predicates first. 691 std::string ImmCode = getImmCode(); 692 if (!ImmCode.empty()) { 693 std::string Result = 694 " int64_t Imm = cast<ConstantSDNode>(Node)->getSExtValue();\n"; 695 return Result + ImmCode; 696 } 697 698 // Handle arbitrary node predicates. 699 assert(!getPredCode().empty() && "Don't have any predicate code!"); 700 std::string ClassName; 701 if (PatFragRec->getOnlyTree()->isLeaf()) 702 ClassName = "SDNode"; 703 else { 704 Record *Op = PatFragRec->getOnlyTree()->getOperator(); 705 ClassName = PatFragRec->getDAGPatterns().getSDNodeInfo(Op).getSDClassName(); 706 } 707 std::string Result; 708 if (ClassName == "SDNode") 709 Result = " SDNode *N = Node;\n"; 710 else 711 Result = " " + ClassName + "*N = cast<" + ClassName + ">(Node);\n"; 712 713 return Result + getPredCode(); 714} 715 716//===----------------------------------------------------------------------===// 717// PatternToMatch implementation 718// 719 720 721/// getPatternSize - Return the 'size' of this pattern. We want to match large 722/// patterns before small ones. This is used to determine the size of a 723/// pattern. 724static unsigned getPatternSize(const TreePatternNode *P, 725 const CodeGenDAGPatterns &CGP) { 726 unsigned Size = 3; // The node itself. 727 // If the root node is a ConstantSDNode, increases its size. 728 // e.g. (set R32:$dst, 0). 729 if (P->isLeaf() && isa<IntInit>(P->getLeafValue())) 730 Size += 2; 731 732 // FIXME: This is a hack to statically increase the priority of patterns 733 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. 734 // Later we can allow complexity / cost for each pattern to be (optionally) 735 // specified. To get best possible pattern match we'll need to dynamically 736 // calculate the complexity of all patterns a dag can potentially map to. 737 const ComplexPattern *AM = P->getComplexPatternInfo(CGP); 738 if (AM) 739 Size += AM->getNumOperands() * 3; 740 741 // If this node has some predicate function that must match, it adds to the 742 // complexity of this node. 743 if (!P->getPredicateFns().empty()) 744 ++Size; 745 746 // Count children in the count if they are also nodes. 747 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { 748 TreePatternNode *Child = P->getChild(i); 749 if (!Child->isLeaf() && Child->getNumTypes() && 750 Child->getType(0) != MVT::Other) 751 Size += getPatternSize(Child, CGP); 752 else if (Child->isLeaf()) { 753 if (isa<IntInit>(Child->getLeafValue())) 754 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2). 755 else if (Child->getComplexPatternInfo(CGP)) 756 Size += getPatternSize(Child, CGP); 757 else if (!Child->getPredicateFns().empty()) 758 ++Size; 759 } 760 } 761 762 return Size; 763} 764 765/// Compute the complexity metric for the input pattern. This roughly 766/// corresponds to the number of nodes that are covered. 767unsigned PatternToMatch:: 768getPatternComplexity(const CodeGenDAGPatterns &CGP) const { 769 return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity(); 770} 771 772 773/// getPredicateCheck - Return a single string containing all of this 774/// pattern's predicates concatenated with "&&" operators. 775/// 776std::string PatternToMatch::getPredicateCheck() const { 777 std::string PredicateCheck; 778 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { 779 if (DefInit *Pred = dyn_cast<DefInit>(Predicates->getElement(i))) { 780 Record *Def = Pred->getDef(); 781 if (!Def->isSubClassOf("Predicate")) { 782#ifndef NDEBUG 783 Def->dump(); 784#endif 785 llvm_unreachable("Unknown predicate type!"); 786 } 787 if (!PredicateCheck.empty()) 788 PredicateCheck += " && "; 789 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; 790 } 791 } 792 793 return PredicateCheck; 794} 795 796//===----------------------------------------------------------------------===// 797// SDTypeConstraint implementation 798// 799 800SDTypeConstraint::SDTypeConstraint(Record *R) { 801 OperandNo = R->getValueAsInt("OperandNum"); 802 803 if (R->isSubClassOf("SDTCisVT")) { 804 ConstraintType = SDTCisVT; 805 x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); 806 if (x.SDTCisVT_Info.VT == MVT::isVoid) 807 PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT"); 808 809 } else if (R->isSubClassOf("SDTCisPtrTy")) { 810 ConstraintType = SDTCisPtrTy; 811 } else if (R->isSubClassOf("SDTCisInt")) { 812 ConstraintType = SDTCisInt; 813 } else if (R->isSubClassOf("SDTCisFP")) { 814 ConstraintType = SDTCisFP; 815 } else if (R->isSubClassOf("SDTCisVec")) { 816 ConstraintType = SDTCisVec; 817 } else if (R->isSubClassOf("SDTCisSameAs")) { 818 ConstraintType = SDTCisSameAs; 819 x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); 820 } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { 821 ConstraintType = SDTCisVTSmallerThanOp; 822 x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = 823 R->getValueAsInt("OtherOperandNum"); 824 } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { 825 ConstraintType = SDTCisOpSmallerThanOp; 826 x.SDTCisOpSmallerThanOp_Info.BigOperandNum = 827 R->getValueAsInt("BigOperandNum"); 828 } else if (R->isSubClassOf("SDTCisEltOfVec")) { 829 ConstraintType = SDTCisEltOfVec; 830 x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum"); 831 } else if (R->isSubClassOf("SDTCisSubVecOfVec")) { 832 ConstraintType = SDTCisSubVecOfVec; 833 x.SDTCisSubVecOfVec_Info.OtherOperandNum = 834 R->getValueAsInt("OtherOpNum"); 835 } else { 836 errs() << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; 837 exit(1); 838 } 839} 840 841/// getOperandNum - Return the node corresponding to operand #OpNo in tree 842/// N, and the result number in ResNo. 843static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N, 844 const SDNodeInfo &NodeInfo, 845 unsigned &ResNo) { 846 unsigned NumResults = NodeInfo.getNumResults(); 847 if (OpNo < NumResults) { 848 ResNo = OpNo; 849 return N; 850 } 851 852 OpNo -= NumResults; 853 854 if (OpNo >= N->getNumChildren()) { 855 errs() << "Invalid operand number in type constraint " 856 << (OpNo+NumResults) << " "; 857 N->dump(); 858 errs() << '\n'; 859 exit(1); 860 } 861 862 return N->getChild(OpNo); 863} 864 865/// ApplyTypeConstraint - Given a node in a pattern, apply this type 866/// constraint to the nodes operands. This returns true if it makes a 867/// change, false otherwise. If a type contradiction is found, flag an error. 868bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, 869 const SDNodeInfo &NodeInfo, 870 TreePattern &TP) const { 871 if (TP.hasError()) 872 return false; 873 874 unsigned ResNo = 0; // The result number being referenced. 875 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo); 876 877 switch (ConstraintType) { 878 case SDTCisVT: 879 // Operand must be a particular type. 880 return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP); 881 case SDTCisPtrTy: 882 // Operand must be same as target pointer type. 883 return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP); 884 case SDTCisInt: 885 // Require it to be one of the legal integer VTs. 886 return NodeToApply->getExtType(ResNo).EnforceInteger(TP); 887 case SDTCisFP: 888 // Require it to be one of the legal fp VTs. 889 return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP); 890 case SDTCisVec: 891 // Require it to be one of the legal vector VTs. 892 return NodeToApply->getExtType(ResNo).EnforceVector(TP); 893 case SDTCisSameAs: { 894 unsigned OResNo = 0; 895 TreePatternNode *OtherNode = 896 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo); 897 return NodeToApply->UpdateNodeType(OResNo, OtherNode->getExtType(ResNo),TP)| 898 OtherNode->UpdateNodeType(ResNo,NodeToApply->getExtType(OResNo),TP); 899 } 900 case SDTCisVTSmallerThanOp: { 901 // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must 902 // have an integer type that is smaller than the VT. 903 if (!NodeToApply->isLeaf() || 904 !isa<DefInit>(NodeToApply->getLeafValue()) || 905 !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() 906 ->isSubClassOf("ValueType")) { 907 TP.error(N->getOperator()->getName() + " expects a VT operand!"); 908 return false; 909 } 910 MVT::SimpleValueType VT = 911 getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); 912 913 EEVT::TypeSet TypeListTmp(VT, TP); 914 915 unsigned OResNo = 0; 916 TreePatternNode *OtherNode = 917 getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo, 918 OResNo); 919 920 return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP); 921 } 922 case SDTCisOpSmallerThanOp: { 923 unsigned BResNo = 0; 924 TreePatternNode *BigOperand = 925 getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo, 926 BResNo); 927 return NodeToApply->getExtType(ResNo). 928 EnforceSmallerThan(BigOperand->getExtType(BResNo), TP); 929 } 930 case SDTCisEltOfVec: { 931 unsigned VResNo = 0; 932 TreePatternNode *VecOperand = 933 getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo, 934 VResNo); 935 936 // Filter vector types out of VecOperand that don't have the right element 937 // type. 938 return VecOperand->getExtType(VResNo). 939 EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP); 940 } 941 case SDTCisSubVecOfVec: { 942 unsigned VResNo = 0; 943 TreePatternNode *BigVecOperand = 944 getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo, 945 VResNo); 946 947 // Filter vector types out of BigVecOperand that don't have the 948 // right subvector type. 949 return BigVecOperand->getExtType(VResNo). 950 EnforceVectorSubVectorTypeIs(NodeToApply->getExtType(ResNo), TP); 951 } 952 } 953 llvm_unreachable("Invalid ConstraintType!"); 954} 955 956// Update the node type to match an instruction operand or result as specified 957// in the ins or outs lists on the instruction definition. Return true if the 958// type was actually changed. 959bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo, 960 Record *Operand, 961 TreePattern &TP) { 962 // The 'unknown' operand indicates that types should be inferred from the 963 // context. 964 if (Operand->isSubClassOf("unknown_class")) 965 return false; 966 967 // The Operand class specifies a type directly. 968 if (Operand->isSubClassOf("Operand")) 969 return UpdateNodeType(ResNo, getValueType(Operand->getValueAsDef("Type")), 970 TP); 971 972 // PointerLikeRegClass has a type that is determined at runtime. 973 if (Operand->isSubClassOf("PointerLikeRegClass")) 974 return UpdateNodeType(ResNo, MVT::iPTR, TP); 975 976 // Both RegisterClass and RegisterOperand operands derive their types from a 977 // register class def. 978 Record *RC = 0; 979 if (Operand->isSubClassOf("RegisterClass")) 980 RC = Operand; 981 else if (Operand->isSubClassOf("RegisterOperand")) 982 RC = Operand->getValueAsDef("RegClass"); 983 984 assert(RC && "Unknown operand type"); 985 CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo(); 986 return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP); 987} 988 989 990//===----------------------------------------------------------------------===// 991// SDNodeInfo implementation 992// 993SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { 994 EnumName = R->getValueAsString("Opcode"); 995 SDClassName = R->getValueAsString("SDClass"); 996 Record *TypeProfile = R->getValueAsDef("TypeProfile"); 997 NumResults = TypeProfile->getValueAsInt("NumResults"); 998 NumOperands = TypeProfile->getValueAsInt("NumOperands"); 999 1000 // Parse the properties. 1001 Properties = 0; 1002 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); 1003 for (unsigned i = 0, e = PropList.size(); i != e; ++i) { 1004 if (PropList[i]->getName() == "SDNPCommutative") { 1005 Properties |= 1 << SDNPCommutative; 1006 } else if (PropList[i]->getName() == "SDNPAssociative") { 1007 Properties |= 1 << SDNPAssociative; 1008 } else if (PropList[i]->getName() == "SDNPHasChain") { 1009 Properties |= 1 << SDNPHasChain; 1010 } else if (PropList[i]->getName() == "SDNPOutGlue") { 1011 Properties |= 1 << SDNPOutGlue; 1012 } else if (PropList[i]->getName() == "SDNPInGlue") { 1013 Properties |= 1 << SDNPInGlue; 1014 } else if (PropList[i]->getName() == "SDNPOptInGlue") { 1015 Properties |= 1 << SDNPOptInGlue; 1016 } else if (PropList[i]->getName() == "SDNPMayStore") { 1017 Properties |= 1 << SDNPMayStore; 1018 } else if (PropList[i]->getName() == "SDNPMayLoad") { 1019 Properties |= 1 << SDNPMayLoad; 1020 } else if (PropList[i]->getName() == "SDNPSideEffect") { 1021 Properties |= 1 << SDNPSideEffect; 1022 } else if (PropList[i]->getName() == "SDNPMemOperand") { 1023 Properties |= 1 << SDNPMemOperand; 1024 } else if (PropList[i]->getName() == "SDNPVariadic") { 1025 Properties |= 1 << SDNPVariadic; 1026 } else { 1027 errs() << "Unknown SD Node property '" << PropList[i]->getName() 1028 << "' on node '" << R->getName() << "'!\n"; 1029 exit(1); 1030 } 1031 } 1032 1033 1034 // Parse the type constraints. 1035 std::vector<Record*> ConstraintList = 1036 TypeProfile->getValueAsListOfDefs("Constraints"); 1037 TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); 1038} 1039 1040/// getKnownType - If the type constraints on this node imply a fixed type 1041/// (e.g. all stores return void, etc), then return it as an 1042/// MVT::SimpleValueType. Otherwise, return EEVT::Other. 1043MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const { 1044 unsigned NumResults = getNumResults(); 1045 assert(NumResults <= 1 && 1046 "We only work with nodes with zero or one result so far!"); 1047 assert(ResNo == 0 && "Only handles single result nodes so far"); 1048 1049 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) { 1050 // Make sure that this applies to the correct node result. 1051 if (TypeConstraints[i].OperandNo >= NumResults) // FIXME: need value # 1052 continue; 1053 1054 switch (TypeConstraints[i].ConstraintType) { 1055 default: break; 1056 case SDTypeConstraint::SDTCisVT: 1057 return TypeConstraints[i].x.SDTCisVT_Info.VT; 1058 case SDTypeConstraint::SDTCisPtrTy: 1059 return MVT::iPTR; 1060 } 1061 } 1062 return MVT::Other; 1063} 1064 1065//===----------------------------------------------------------------------===// 1066// TreePatternNode implementation 1067// 1068 1069TreePatternNode::~TreePatternNode() { 1070#if 0 // FIXME: implement refcounted tree nodes! 1071 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1072 delete getChild(i); 1073#endif 1074} 1075 1076static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) { 1077 if (Operator->getName() == "set" || 1078 Operator->getName() == "implicit") 1079 return 0; // All return nothing. 1080 1081 if (Operator->isSubClassOf("Intrinsic")) 1082 return CDP.getIntrinsic(Operator).IS.RetVTs.size(); 1083 1084 if (Operator->isSubClassOf("SDNode")) 1085 return CDP.getSDNodeInfo(Operator).getNumResults(); 1086 1087 if (Operator->isSubClassOf("PatFrag")) { 1088 // If we've already parsed this pattern fragment, get it. Otherwise, handle 1089 // the forward reference case where one pattern fragment references another 1090 // before it is processed. 1091 if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) 1092 return PFRec->getOnlyTree()->getNumTypes(); 1093 1094 // Get the result tree. 1095 DagInit *Tree = Operator->getValueAsDag("Fragment"); 1096 Record *Op = 0; 1097 if (Tree) 1098 if (DefInit *DI = dyn_cast<DefInit>(Tree->getOperator())) 1099 Op = DI->getDef(); 1100 assert(Op && "Invalid Fragment"); 1101 return GetNumNodeResults(Op, CDP); 1102 } 1103 1104 if (Operator->isSubClassOf("Instruction")) { 1105 CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator); 1106 1107 // FIXME: Should allow access to all the results here. 1108 unsigned NumDefsToAdd = InstInfo.Operands.NumDefs ? 1 : 0; 1109 1110 // Add on one implicit def if it has a resolvable type. 1111 if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other) 1112 ++NumDefsToAdd; 1113 return NumDefsToAdd; 1114 } 1115 1116 if (Operator->isSubClassOf("SDNodeXForm")) 1117 return 1; // FIXME: Generalize SDNodeXForm 1118 1119 Operator->dump(); 1120 errs() << "Unhandled node in GetNumNodeResults\n"; 1121 exit(1); 1122} 1123 1124void TreePatternNode::print(raw_ostream &OS) const { 1125 if (isLeaf()) 1126 OS << *getLeafValue(); 1127 else 1128 OS << '(' << getOperator()->getName(); 1129 1130 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1131 OS << ':' << getExtType(i).getName(); 1132 1133 if (!isLeaf()) { 1134 if (getNumChildren() != 0) { 1135 OS << " "; 1136 getChild(0)->print(OS); 1137 for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { 1138 OS << ", "; 1139 getChild(i)->print(OS); 1140 } 1141 } 1142 OS << ")"; 1143 } 1144 1145 for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i) 1146 OS << "<<P:" << PredicateFns[i].getFnName() << ">>"; 1147 if (TransformFn) 1148 OS << "<<X:" << TransformFn->getName() << ">>"; 1149 if (!getName().empty()) 1150 OS << ":$" << getName(); 1151 1152} 1153void TreePatternNode::dump() const { 1154 print(errs()); 1155} 1156 1157/// isIsomorphicTo - Return true if this node is recursively 1158/// isomorphic to the specified node. For this comparison, the node's 1159/// entire state is considered. The assigned name is ignored, since 1160/// nodes with differing names are considered isomorphic. However, if 1161/// the assigned name is present in the dependent variable set, then 1162/// the assigned name is considered significant and the node is 1163/// isomorphic if the names match. 1164bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, 1165 const MultipleUseVarSet &DepVars) const { 1166 if (N == this) return true; 1167 if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || 1168 getPredicateFns() != N->getPredicateFns() || 1169 getTransformFn() != N->getTransformFn()) 1170 return false; 1171 1172 if (isLeaf()) { 1173 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 1174 if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) { 1175 return ((DI->getDef() == NDI->getDef()) 1176 && (DepVars.find(getName()) == DepVars.end() 1177 || getName() == N->getName())); 1178 } 1179 } 1180 return getLeafValue() == N->getLeafValue(); 1181 } 1182 1183 if (N->getOperator() != getOperator() || 1184 N->getNumChildren() != getNumChildren()) return false; 1185 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1186 if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars)) 1187 return false; 1188 return true; 1189} 1190 1191/// clone - Make a copy of this tree and all of its children. 1192/// 1193TreePatternNode *TreePatternNode::clone() const { 1194 TreePatternNode *New; 1195 if (isLeaf()) { 1196 New = new TreePatternNode(getLeafValue(), getNumTypes()); 1197 } else { 1198 std::vector<TreePatternNode*> CChildren; 1199 CChildren.reserve(Children.size()); 1200 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1201 CChildren.push_back(getChild(i)->clone()); 1202 New = new TreePatternNode(getOperator(), CChildren, getNumTypes()); 1203 } 1204 New->setName(getName()); 1205 New->Types = Types; 1206 New->setPredicateFns(getPredicateFns()); 1207 New->setTransformFn(getTransformFn()); 1208 return New; 1209} 1210 1211/// RemoveAllTypes - Recursively strip all the types of this tree. 1212void TreePatternNode::RemoveAllTypes() { 1213 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1214 Types[i] = EEVT::TypeSet(); // Reset to unknown type. 1215 if (isLeaf()) return; 1216 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1217 getChild(i)->RemoveAllTypes(); 1218} 1219 1220 1221/// SubstituteFormalArguments - Replace the formal arguments in this tree 1222/// with actual values specified by ArgMap. 1223void TreePatternNode:: 1224SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { 1225 if (isLeaf()) return; 1226 1227 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1228 TreePatternNode *Child = getChild(i); 1229 if (Child->isLeaf()) { 1230 Init *Val = Child->getLeafValue(); 1231 if (isa<DefInit>(Val) && 1232 cast<DefInit>(Val)->getDef()->getName() == "node") { 1233 // We found a use of a formal argument, replace it with its value. 1234 TreePatternNode *NewChild = ArgMap[Child->getName()]; 1235 assert(NewChild && "Couldn't find formal argument!"); 1236 assert((Child->getPredicateFns().empty() || 1237 NewChild->getPredicateFns() == Child->getPredicateFns()) && 1238 "Non-empty child predicate clobbered!"); 1239 setChild(i, NewChild); 1240 } 1241 } else { 1242 getChild(i)->SubstituteFormalArguments(ArgMap); 1243 } 1244 } 1245} 1246 1247 1248/// InlinePatternFragments - If this pattern refers to any pattern 1249/// fragments, inline them into place, giving us a pattern without any 1250/// PatFrag references. 1251TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { 1252 if (TP.hasError()) 1253 return 0; 1254 1255 if (isLeaf()) 1256 return this; // nothing to do. 1257 Record *Op = getOperator(); 1258 1259 if (!Op->isSubClassOf("PatFrag")) { 1260 // Just recursively inline children nodes. 1261 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1262 TreePatternNode *Child = getChild(i); 1263 TreePatternNode *NewChild = Child->InlinePatternFragments(TP); 1264 1265 assert((Child->getPredicateFns().empty() || 1266 NewChild->getPredicateFns() == Child->getPredicateFns()) && 1267 "Non-empty child predicate clobbered!"); 1268 1269 setChild(i, NewChild); 1270 } 1271 return this; 1272 } 1273 1274 // Otherwise, we found a reference to a fragment. First, look up its 1275 // TreePattern record. 1276 TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); 1277 1278 // Verify that we are passing the right number of operands. 1279 if (Frag->getNumArgs() != Children.size()) { 1280 TP.error("'" + Op->getName() + "' fragment requires " + 1281 utostr(Frag->getNumArgs()) + " operands!"); 1282 return 0; 1283 } 1284 1285 TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); 1286 1287 TreePredicateFn PredFn(Frag); 1288 if (!PredFn.isAlwaysTrue()) 1289 FragTree->addPredicateFn(PredFn); 1290 1291 // Resolve formal arguments to their actual value. 1292 if (Frag->getNumArgs()) { 1293 // Compute the map of formal to actual arguments. 1294 std::map<std::string, TreePatternNode*> ArgMap; 1295 for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) 1296 ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); 1297 1298 FragTree->SubstituteFormalArguments(ArgMap); 1299 } 1300 1301 FragTree->setName(getName()); 1302 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1303 FragTree->UpdateNodeType(i, getExtType(i), TP); 1304 1305 // Transfer in the old predicates. 1306 for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i) 1307 FragTree->addPredicateFn(getPredicateFns()[i]); 1308 1309 // Get a new copy of this fragment to stitch into here. 1310 //delete this; // FIXME: implement refcounting! 1311 1312 // The fragment we inlined could have recursive inlining that is needed. See 1313 // if there are any pattern fragments in it and inline them as needed. 1314 return FragTree->InlinePatternFragments(TP); 1315} 1316 1317/// getImplicitType - Check to see if the specified record has an implicit 1318/// type which should be applied to it. This will infer the type of register 1319/// references from the register file information, for example. 1320/// 1321/// When Unnamed is set, return the type of a DAG operand with no name, such as 1322/// the F8RC register class argument in: 1323/// 1324/// (COPY_TO_REGCLASS GPR:$src, F8RC) 1325/// 1326/// When Unnamed is false, return the type of a named DAG operand such as the 1327/// GPR:$src operand above. 1328/// 1329static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo, 1330 bool NotRegisters, 1331 bool Unnamed, 1332 TreePattern &TP) { 1333 // Check to see if this is a register operand. 1334 if (R->isSubClassOf("RegisterOperand")) { 1335 assert(ResNo == 0 && "Regoperand ref only has one result!"); 1336 if (NotRegisters) 1337 return EEVT::TypeSet(); // Unknown. 1338 Record *RegClass = R->getValueAsDef("RegClass"); 1339 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1340 return EEVT::TypeSet(T.getRegisterClass(RegClass).getValueTypes()); 1341 } 1342 1343 // Check to see if this is a register or a register class. 1344 if (R->isSubClassOf("RegisterClass")) { 1345 assert(ResNo == 0 && "Regclass ref only has one result!"); 1346 // An unnamed register class represents itself as an i32 immediate, for 1347 // example on a COPY_TO_REGCLASS instruction. 1348 if (Unnamed) 1349 return EEVT::TypeSet(MVT::i32, TP); 1350 1351 // In a named operand, the register class provides the possible set of 1352 // types. 1353 if (NotRegisters) 1354 return EEVT::TypeSet(); // Unknown. 1355 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1356 return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes()); 1357 } 1358 1359 if (R->isSubClassOf("PatFrag")) { 1360 assert(ResNo == 0 && "FIXME: PatFrag with multiple results?"); 1361 // Pattern fragment types will be resolved when they are inlined. 1362 return EEVT::TypeSet(); // Unknown. 1363 } 1364 1365 if (R->isSubClassOf("Register")) { 1366 assert(ResNo == 0 && "Registers only produce one result!"); 1367 if (NotRegisters) 1368 return EEVT::TypeSet(); // Unknown. 1369 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1370 return EEVT::TypeSet(T.getRegisterVTs(R)); 1371 } 1372 1373 if (R->isSubClassOf("SubRegIndex")) { 1374 assert(ResNo == 0 && "SubRegisterIndices only produce one result!"); 1375 return EEVT::TypeSet(); 1376 } 1377 1378 if (R->isSubClassOf("ValueType")) { 1379 assert(ResNo == 0 && "This node only has one result!"); 1380 // An unnamed VTSDNode represents itself as an MVT::Other immediate. 1381 // 1382 // (sext_inreg GPR:$src, i16) 1383 // ~~~ 1384 if (Unnamed) 1385 return EEVT::TypeSet(MVT::Other, TP); 1386 // With a name, the ValueType simply provides the type of the named 1387 // variable. 1388 // 1389 // (sext_inreg i32:$src, i16) 1390 // ~~~~~~~~ 1391 if (NotRegisters) 1392 return EEVT::TypeSet(); // Unknown. 1393 return EEVT::TypeSet(getValueType(R), TP); 1394 } 1395 1396 if (R->isSubClassOf("CondCode")) { 1397 assert(ResNo == 0 && "This node only has one result!"); 1398 // Using a CondCodeSDNode. 1399 return EEVT::TypeSet(MVT::Other, TP); 1400 } 1401 1402 if (R->isSubClassOf("ComplexPattern")) { 1403 assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?"); 1404 if (NotRegisters) 1405 return EEVT::TypeSet(); // Unknown. 1406 return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(), 1407 TP); 1408 } 1409 if (R->isSubClassOf("PointerLikeRegClass")) { 1410 assert(ResNo == 0 && "Regclass can only have one result!"); 1411 return EEVT::TypeSet(MVT::iPTR, TP); 1412 } 1413 1414 if (R->getName() == "node" || R->getName() == "srcvalue" || 1415 R->getName() == "zero_reg") { 1416 // Placeholder. 1417 return EEVT::TypeSet(); // Unknown. 1418 } 1419 1420 TP.error("Unknown node flavor used in pattern: " + R->getName()); 1421 return EEVT::TypeSet(MVT::Other, TP); 1422} 1423 1424 1425/// getIntrinsicInfo - If this node corresponds to an intrinsic, return the 1426/// CodeGenIntrinsic information for it, otherwise return a null pointer. 1427const CodeGenIntrinsic *TreePatternNode:: 1428getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { 1429 if (getOperator() != CDP.get_intrinsic_void_sdnode() && 1430 getOperator() != CDP.get_intrinsic_w_chain_sdnode() && 1431 getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) 1432 return 0; 1433 1434 unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue(); 1435 return &CDP.getIntrinsicInfo(IID); 1436} 1437 1438/// getComplexPatternInfo - If this node corresponds to a ComplexPattern, 1439/// return the ComplexPattern information, otherwise return null. 1440const ComplexPattern * 1441TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const { 1442 if (!isLeaf()) return 0; 1443 1444 DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 1445 if (DI && DI->getDef()->isSubClassOf("ComplexPattern")) 1446 return &CGP.getComplexPattern(DI->getDef()); 1447 return 0; 1448} 1449 1450/// NodeHasProperty - Return true if this node has the specified property. 1451bool TreePatternNode::NodeHasProperty(SDNP Property, 1452 const CodeGenDAGPatterns …
Large files files are truncated, but you can click here to view the full file