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> 26 using namespace llvm; 27 28 #define DEBUG_TYPE "dag-patterns" 29 30 //===----------------------------------------------------------------------===// 31 // EEVT::TypeSet Implementation 32 //===----------------------------------------------------------------------===// 33 34 static inline bool isInteger(MVT::SimpleValueType VT) { 35 return MVT(VT).isInteger(); 36 } 37 static inline bool isFloatingPoint(MVT::SimpleValueType VT) { 38 return MVT(VT).isFloatingPoint(); 39 } 40 static inline bool isVector(MVT::SimpleValueType VT) { 41 return MVT(VT).isVector(); 42 } 43 static inline bool isScalar(MVT::SimpleValueType VT) { 44 return !MVT(VT).isVector(); 45 } 46 47 EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) { 48 if (VT == MVT::iAny) 49 EnforceInteger(TP); 50 else if (VT == MVT::fAny) 51 EnforceFloatingPoint(TP); 52 else if (VT == MVT::vAny) 53 EnforceVector(TP); 54 else { 55 assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR || 56 VT == MVT::iPTRAny || VT == MVT::Any) && "Not a concrete type!"); 57 TypeVec.push_back(VT); 58 } 59 } 60 61 62 EEVT::TypeSet::TypeSet(ArrayRef<MVT::SimpleValueType> VTList) { 63 assert(!VTList.empty() && "empty list?"); 64 TypeVec.append(VTList.begin(), VTList.end()); 65 66 if (!VTList.empty()) 67 assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny && 68 VTList[0] != MVT::fAny); 69 70 // Verify no duplicates. 71 array_pod_sort(TypeVec.begin(), TypeVec.end()); 72 assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end()); 73 } 74 75 /// FillWithPossibleTypes - Set to all legal types and return true, only valid 76 /// on completely unknown type sets. 77 bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP, 78 bool (*Pred)(MVT::SimpleValueType), 79 const char *PredicateName) { 80 assert(isCompletelyUnknown()); 81 ArrayRef<MVT::SimpleValueType> LegalTypes = 82 TP.getDAGPatterns().getTargetInfo().getLegalValueTypes(); 83 84 if (TP.hasError()) 85 return false; 86 87 for (unsigned i = 0, e = LegalTypes.size(); i != e; ++i) 88 if (!Pred || Pred(LegalTypes[i])) 89 TypeVec.push_back(LegalTypes[i]); 90 91 // If we have nothing that matches the predicate, bail out. 92 if (TypeVec.empty()) { 93 TP.error("Type inference contradiction found, no " + 94 std::string(PredicateName) + " types found"); 95 return false; 96 } 97 // No need to sort with one element. 98 if (TypeVec.size() == 1) return true; 99 100 // Remove duplicates. 101 array_pod_sort(TypeVec.begin(), TypeVec.end()); 102 TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end()); 103 104 return true; 105 } 106 107 /// hasIntegerTypes - Return true if this TypeSet contains iAny or an 108 /// integer value type. 109 bool EEVT::TypeSet::hasIntegerTypes() const { 110 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 111 if (isInteger(TypeVec[i])) 112 return true; 113 return false; 114 } 115 116 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or 117 /// a floating point value type. 118 bool EEVT::TypeSet::hasFloatingPointTypes() const { 119 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 120 if (isFloatingPoint(TypeVec[i])) 121 return true; 122 return false; 123 } 124 125 /// hasScalarTypes - Return true if this TypeSet contains a scalar value type. 126 bool EEVT::TypeSet::hasScalarTypes() const { 127 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 128 if (isScalar(TypeVec[i])) 129 return true; 130 return false; 131 } 132 133 /// hasVectorTypes - Return true if this TypeSet contains a vAny or a vector 134 /// value type. 135 bool EEVT::TypeSet::hasVectorTypes() const { 136 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 137 if (isVector(TypeVec[i])) 138 return true; 139 return false; 140 } 141 142 143 std::string EEVT::TypeSet::getName() const { 144 if (TypeVec.empty()) return "<empty>"; 145 146 std::string Result; 147 148 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) { 149 std::string VTName = llvm::getEnumName(TypeVec[i]); 150 // Strip off MVT:: prefix if present. 151 if (VTName.substr(0,5) == "MVT::") 152 VTName = VTName.substr(5); 153 if (i) Result += ':'; 154 Result += VTName; 155 } 156 157 if (TypeVec.size() == 1) 158 return Result; 159 return "{" + Result + "}"; 160 } 161 162 /// MergeInTypeInfo - This merges in type information from the specified 163 /// argument. If 'this' changes, it returns true. If the two types are 164 /// contradictory (e.g. merge f32 into i32) then this flags an error. 165 bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){ 166 if (InVT.isCompletelyUnknown() || *this == InVT || TP.hasError()) 167 return false; 168 169 if (isCompletelyUnknown()) { 170 *this = InVT; 171 return true; 172 } 173 174 assert(TypeVec.size() >= 1 && InVT.TypeVec.size() >= 1 && "No unknowns"); 175 176 // Handle the abstract cases, seeing if we can resolve them better. 177 switch (TypeVec[0]) { 178 default: break; 179 case MVT::iPTR: 180 case MVT::iPTRAny: 181 if (InVT.hasIntegerTypes()) { 182 EEVT::TypeSet InCopy(InVT); 183 InCopy.EnforceInteger(TP); 184 InCopy.EnforceScalar(TP); 185 186 if (InCopy.isConcrete()) { 187 // If the RHS has one integer type, upgrade iPTR to i32. 188 TypeVec[0] = InVT.TypeVec[0]; 189 return true; 190 } 191 192 // If the input has multiple scalar integers, this doesn't add any info. 193 if (!InCopy.isCompletelyUnknown()) 194 return false; 195 } 196 break; 197 } 198 199 // If the input constraint is iAny/iPTR and this is an integer type list, 200 // remove non-integer types from the list. 201 if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && 202 hasIntegerTypes()) { 203 bool MadeChange = EnforceInteger(TP); 204 205 // If we're merging in iPTR/iPTRAny and the node currently has a list of 206 // multiple different integer types, replace them with a single iPTR. 207 if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && 208 TypeVec.size() != 1) { 209 TypeVec.resize(1); 210 TypeVec[0] = InVT.TypeVec[0]; 211 MadeChange = true; 212 } 213 214 return MadeChange; 215 } 216 217 // If this is a type list and the RHS is a typelist as well, eliminate entries 218 // from this list that aren't in the other one. 219 bool MadeChange = false; 220 TypeSet InputSet(*this); 221 222 for (unsigned i = 0; i != TypeVec.size(); ++i) { 223 bool InInVT = false; 224 for (unsigned j = 0, e = InVT.TypeVec.size(); j != e; ++j) 225 if (TypeVec[i] == InVT.TypeVec[j]) { 226 InInVT = true; 227 break; 228 } 229 230 if (InInVT) continue; 231 TypeVec.erase(TypeVec.begin()+i--); 232 MadeChange = true; 233 } 234 235 // If we removed all of our types, we have a type contradiction. 236 if (!TypeVec.empty()) 237 return MadeChange; 238 239 // FIXME: Really want an SMLoc here! 240 TP.error("Type inference contradiction found, merging '" + 241 InVT.getName() + "' into '" + InputSet.getName() + "'"); 242 return false; 243 } 244 245 /// EnforceInteger - Remove all non-integer types from this set. 246 bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) { 247 if (TP.hasError()) 248 return false; 249 // If we know nothing, then get the full set. 250 if (TypeVec.empty()) 251 return FillWithPossibleTypes(TP, isInteger, "integer"); 252 if (!hasFloatingPointTypes()) 253 return false; 254 255 TypeSet InputSet(*this); 256 257 // Filter out all the fp types. 258 for (unsigned i = 0; i != TypeVec.size(); ++i) 259 if (!isInteger(TypeVec[i])) 260 TypeVec.erase(TypeVec.begin()+i--); 261 262 if (TypeVec.empty()) { 263 TP.error("Type inference contradiction found, '" + 264 InputSet.getName() + "' needs to be integer"); 265 return false; 266 } 267 return true; 268 } 269 270 /// EnforceFloatingPoint - Remove all integer types from this set. 271 bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) { 272 if (TP.hasError()) 273 return false; 274 // If we know nothing, then get the full set. 275 if (TypeVec.empty()) 276 return FillWithPossibleTypes(TP, isFloatingPoint, "floating point"); 277 278 if (!hasIntegerTypes()) 279 return false; 280 281 TypeSet InputSet(*this); 282 283 // Filter out all the fp types. 284 for (unsigned i = 0; i != TypeVec.size(); ++i) 285 if (!isFloatingPoint(TypeVec[i])) 286 TypeVec.erase(TypeVec.begin()+i--); 287 288 if (TypeVec.empty()) { 289 TP.error("Type inference contradiction found, '" + 290 InputSet.getName() + "' needs to be floating point"); 291 return false; 292 } 293 return true; 294 } 295 296 /// EnforceScalar - Remove all vector types from this. 297 bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) { 298 if (TP.hasError()) 299 return false; 300 301 // If we know nothing, then get the full set. 302 if (TypeVec.empty()) 303 return FillWithPossibleTypes(TP, isScalar, "scalar"); 304 305 if (!hasVectorTypes()) 306 return false; 307 308 TypeSet InputSet(*this); 309 310 // Filter out all the vector types. 311 for (unsigned i = 0; i != TypeVec.size(); ++i) 312 if (!isScalar(TypeVec[i])) 313 TypeVec.erase(TypeVec.begin()+i--); 314 315 if (TypeVec.empty()) { 316 TP.error("Type inference contradiction found, '" + 317 InputSet.getName() + "' needs to be scalar"); 318 return false; 319 } 320 return true; 321 } 322 323 /// EnforceVector - Remove all vector types from this. 324 bool EEVT::TypeSet::EnforceVector(TreePattern &TP) { 325 if (TP.hasError()) 326 return false; 327 328 // If we know nothing, then get the full set. 329 if (TypeVec.empty()) 330 return FillWithPossibleTypes(TP, isVector, "vector"); 331 332 TypeSet InputSet(*this); 333 bool MadeChange = false; 334 335 // Filter out all the scalar types. 336 for (unsigned i = 0; i != TypeVec.size(); ++i) 337 if (!isVector(TypeVec[i])) { 338 TypeVec.erase(TypeVec.begin()+i--); 339 MadeChange = true; 340 } 341 342 if (TypeVec.empty()) { 343 TP.error("Type inference contradiction found, '" + 344 InputSet.getName() + "' needs to be a vector"); 345 return false; 346 } 347 return MadeChange; 348 } 349 350 351 352 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. For vectors 353 /// this shoud be based on the element type. Update this and other based on 354 /// this information. 355 bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) { 356 if (TP.hasError()) 357 return false; 358 359 // Both operands must be integer or FP, but we don't care which. 360 bool MadeChange = false; 361 362 if (isCompletelyUnknown()) 363 MadeChange = FillWithPossibleTypes(TP); 364 365 if (Other.isCompletelyUnknown()) 366 MadeChange = Other.FillWithPossibleTypes(TP); 367 368 // If one side is known to be integer or known to be FP but the other side has 369 // no information, get at least the type integrality info in there. 370 if (!hasFloatingPointTypes()) 371 MadeChange |= Other.EnforceInteger(TP); 372 else if (!hasIntegerTypes()) 373 MadeChange |= Other.EnforceFloatingPoint(TP); 374 if (!Other.hasFloatingPointTypes()) 375 MadeChange |= EnforceInteger(TP); 376 else if (!Other.hasIntegerTypes()) 377 MadeChange |= EnforceFloatingPoint(TP); 378 379 assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() && 380 "Should have a type list now"); 381 382 // If one contains vectors but the other doesn't pull vectors out. 383 if (!hasVectorTypes()) 384 MadeChange |= Other.EnforceScalar(TP); 385 else if (!hasScalarTypes()) 386 MadeChange |= Other.EnforceVector(TP); 387 if (!Other.hasVectorTypes()) 388 MadeChange |= EnforceScalar(TP); 389 else if (!Other.hasScalarTypes()) 390 MadeChange |= EnforceVector(TP); 391 392 // This code does not currently handle nodes which have multiple types, 393 // where some types are integer, and some are fp. Assert that this is not 394 // the case. 395 assert(!(hasIntegerTypes() && hasFloatingPointTypes()) && 396 !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) && 397 "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); 398 399 if (TP.hasError()) 400 return false; 401 402 // Okay, find the smallest type from current set and remove anything the 403 // same or smaller from the other set. We need to ensure that the scalar 404 // type size is smaller than the scalar size of the smallest type. For 405 // vectors, we also need to make sure that the total size is no larger than 406 // the size of the smallest type. 407 TypeSet InputSet(Other); 408 MVT Smallest = TypeVec[0]; 409 for (unsigned i = 0; i != Other.TypeVec.size(); ++i) { 410 MVT OtherVT = Other.TypeVec[i]; 411 // Don't compare vector and non-vector types. 412 if (OtherVT.isVector() != Smallest.isVector()) 413 continue; 414 // The getSizeInBits() check here is only needed for vectors, but is 415 // a subset of the scalar check for scalars so no need to qualify. 416 if (OtherVT.getScalarSizeInBits() <= Smallest.getScalarSizeInBits() || 417 OtherVT.getSizeInBits() < Smallest.getSizeInBits()) { 418 Other.TypeVec.erase(Other.TypeVec.begin()+i--); 419 MadeChange = true; 420 } 421 } 422 423 if (Other.TypeVec.empty()) { 424 TP.error("Type inference contradiction found, '" + InputSet.getName() + 425 "' has nothing larger than '" + getName() +"'!"); 426 return false; 427 } 428 429 // Okay, find the largest type from the other set and remove anything the 430 // same or smaller from the current set. We need to ensure that the scalar 431 // type size is larger than the scalar size of the largest type. For 432 // vectors, we also need to make sure that the total size is no smaller than 433 // the size of the largest type. 434 InputSet = TypeSet(*this); 435 MVT Largest = Other.TypeVec[Other.TypeVec.size()-1]; 436 for (unsigned i = 0; i != TypeVec.size(); ++i) { 437 MVT OtherVT = TypeVec[i]; 438 // Don't compare vector and non-vector types. 439 if (OtherVT.isVector() != Largest.isVector()) 440 continue; 441 // The getSizeInBits() check here is only needed for vectors, but is 442 // a subset of the scalar check for scalars so no need to qualify. 443 if (OtherVT.getScalarSizeInBits() >= Largest.getScalarSizeInBits() || 444 OtherVT.getSizeInBits() > Largest.getSizeInBits()) { 445 TypeVec.erase(TypeVec.begin()+i--); 446 MadeChange = true; 447 } 448 } 449 450 if (TypeVec.empty()) { 451 TP.error("Type inference contradiction found, '" + InputSet.getName() + 452 "' has nothing smaller than '" + Other.getName() +"'!"); 453 return false; 454 } 455 456 return MadeChange; 457 } 458 459 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type 460 /// whose element is specified by VTOperand. 461 bool EEVT::TypeSet::EnforceVectorEltTypeIs(MVT::SimpleValueType VT, 462 TreePattern &TP) { 463 bool MadeChange = false; 464 465 MadeChange |= EnforceVector(TP); 466 467 TypeSet InputSet(*this); 468 469 // Filter out all the types which don't have the right element type. 470 for (unsigned i = 0; i != TypeVec.size(); ++i) { 471 assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 472 if (MVT(TypeVec[i]).getVectorElementType().SimpleTy != VT) { 473 TypeVec.erase(TypeVec.begin()+i--); 474 MadeChange = true; 475 } 476 } 477 478 if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 479 TP.error("Type inference contradiction found, forcing '" + 480 InputSet.getName() + "' to have a vector element"); 481 return false; 482 } 483 484 return MadeChange; 485 } 486 487 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type 488 /// whose element is specified by VTOperand. 489 bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand, 490 TreePattern &TP) { 491 if (TP.hasError()) 492 return false; 493 494 // "This" must be a vector and "VTOperand" must be a scalar. 495 bool MadeChange = false; 496 MadeChange |= EnforceVector(TP); 497 MadeChange |= VTOperand.EnforceScalar(TP); 498 499 // If we know the vector type, it forces the scalar to agree. 500 if (isConcrete()) { 501 MVT IVT = getConcrete(); 502 IVT = IVT.getVectorElementType(); 503 return MadeChange | 504 VTOperand.MergeInTypeInfo(IVT.SimpleTy, TP); 505 } 506 507 // If the scalar type is known, filter out vector types whose element types 508 // disagree. 509 if (!VTOperand.isConcrete()) 510 return MadeChange; 511 512 MVT::SimpleValueType VT = VTOperand.getConcrete(); 513 514 TypeSet InputSet(*this); 515 516 // Filter out all the types which don't have the right element type. 517 for (unsigned i = 0; i != TypeVec.size(); ++i) { 518 assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 519 if (MVT(TypeVec[i]).getVectorElementType().SimpleTy != VT) { 520 TypeVec.erase(TypeVec.begin()+i--); 521 MadeChange = true; 522 } 523 } 524 525 if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 526 TP.error("Type inference contradiction found, forcing '" + 527 InputSet.getName() + "' to have a vector element"); 528 return false; 529 } 530 return MadeChange; 531 } 532 533 /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to be a 534 /// vector type specified by VTOperand. 535 bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(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 vector. 541 bool MadeChange = false; 542 MadeChange |= EnforceVector(TP); 543 MadeChange |= VTOperand.EnforceVector(TP); 544 545 // If one side is known to be integer or known to be FP but the other side has 546 // no information, get at least the type integrality info in there. 547 if (!hasFloatingPointTypes()) 548 MadeChange |= VTOperand.EnforceInteger(TP); 549 else if (!hasIntegerTypes()) 550 MadeChange |= VTOperand.EnforceFloatingPoint(TP); 551 if (!VTOperand.hasFloatingPointTypes()) 552 MadeChange |= EnforceInteger(TP); 553 else if (!VTOperand.hasIntegerTypes()) 554 MadeChange |= EnforceFloatingPoint(TP); 555 556 assert(!isCompletelyUnknown() && !VTOperand.isCompletelyUnknown() && 557 "Should have a type list now"); 558 559 // If we know the vector type, it forces the scalar types to agree. 560 // Also force one vector to have more elements than the other. 561 if (isConcrete()) { 562 MVT IVT = getConcrete(); 563 unsigned NumElems = IVT.getVectorNumElements(); 564 IVT = IVT.getVectorElementType(); 565 566 EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP); 567 MadeChange |= VTOperand.EnforceVectorEltTypeIs(EltTypeSet, TP); 568 569 // Only keep types that have less elements than VTOperand. 570 TypeSet InputSet(VTOperand); 571 572 for (unsigned i = 0; i != VTOperand.TypeVec.size(); ++i) { 573 assert(isVector(VTOperand.TypeVec[i]) && "EnforceVector didn't work"); 574 if (MVT(VTOperand.TypeVec[i]).getVectorNumElements() >= NumElems) { 575 VTOperand.TypeVec.erase(VTOperand.TypeVec.begin()+i--); 576 MadeChange = true; 577 } 578 } 579 if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here! 580 TP.error("Type inference contradiction found, forcing '" + 581 InputSet.getName() + "' to have less vector elements than '" + 582 getName() + "'"); 583 return false; 584 } 585 } else if (VTOperand.isConcrete()) { 586 MVT IVT = VTOperand.getConcrete(); 587 unsigned NumElems = IVT.getVectorNumElements(); 588 IVT = IVT.getVectorElementType(); 589 590 EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP); 591 MadeChange |= EnforceVectorEltTypeIs(EltTypeSet, TP); 592 593 // Only keep types that have more elements than 'this'. 594 TypeSet InputSet(*this); 595 596 for (unsigned i = 0; i != TypeVec.size(); ++i) { 597 assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 598 if (MVT(TypeVec[i]).getVectorNumElements() <= NumElems) { 599 TypeVec.erase(TypeVec.begin()+i--); 600 MadeChange = true; 601 } 602 } 603 if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 604 TP.error("Type inference contradiction found, forcing '" + 605 InputSet.getName() + "' to have more vector elements than '" + 606 VTOperand.getName() + "'"); 607 return false; 608 } 609 } 610 611 return MadeChange; 612 } 613 614 /// EnforceVectorSameNumElts - 'this' is now constrainted to 615 /// be a vector with same num elements as VTOperand. 616 bool EEVT::TypeSet::EnforceVectorSameNumElts(EEVT::TypeSet &VTOperand, 617 TreePattern &TP) { 618 if (TP.hasError()) 619 return false; 620 621 // "This" must be a vector and "VTOperand" must be a vector. 622 bool MadeChange = false; 623 MadeChange |= EnforceVector(TP); 624 MadeChange |= VTOperand.EnforceVector(TP); 625 626 // If we know one of the vector types, it forces the other type to agree. 627 if (isConcrete()) { 628 MVT IVT = getConcrete(); 629 unsigned NumElems = IVT.getVectorNumElements(); 630 631 // Only keep types that have same elements as VTOperand. 632 TypeSet InputSet(VTOperand); 633 634 for (unsigned i = 0; i != VTOperand.TypeVec.size(); ++i) { 635 assert(isVector(VTOperand.TypeVec[i]) && "EnforceVector didn't work"); 636 if (MVT(VTOperand.TypeVec[i]).getVectorNumElements() != NumElems) { 637 VTOperand.TypeVec.erase(VTOperand.TypeVec.begin()+i--); 638 MadeChange = true; 639 } 640 } 641 if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here! 642 TP.error("Type inference contradiction found, forcing '" + 643 InputSet.getName() + "' to have same number elements as '" + 644 getName() + "'"); 645 return false; 646 } 647 } else if (VTOperand.isConcrete()) { 648 MVT IVT = VTOperand.getConcrete(); 649 unsigned NumElems = IVT.getVectorNumElements(); 650 651 // Only keep types that have same elements as 'this'. 652 TypeSet InputSet(*this); 653 654 for (unsigned i = 0; i != TypeVec.size(); ++i) { 655 assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 656 if (MVT(TypeVec[i]).getVectorNumElements() != NumElems) { 657 TypeVec.erase(TypeVec.begin()+i--); 658 MadeChange = true; 659 } 660 } 661 if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 662 TP.error("Type inference contradiction found, forcing '" + 663 InputSet.getName() + "' to have same number elements than '" + 664 VTOperand.getName() + "'"); 665 return false; 666 } 667 } 668 669 return MadeChange; 670 } 671 672 //===----------------------------------------------------------------------===// 673 // Helpers for working with extended types. 674 675 /// Dependent variable map for CodeGenDAGPattern variant generation 676 typedef std::map<std::string, int> DepVarMap; 677 678 /// Const iterator shorthand for DepVarMap 679 typedef DepVarMap::const_iterator DepVarMap_citer; 680 681 static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) { 682 if (N->isLeaf()) { 683 if (isa<DefInit>(N->getLeafValue())) 684 DepMap[N->getName()]++; 685 } else { 686 for (size_t i = 0, e = N->getNumChildren(); i != e; ++i) 687 FindDepVarsOf(N->getChild(i), DepMap); 688 } 689 } 690 691 /// Find dependent variables within child patterns 692 static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) { 693 DepVarMap depcounts; 694 FindDepVarsOf(N, depcounts); 695 for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) { 696 if (i->second > 1) // std::pair<std::string, int> 697 DepVars.insert(i->first); 698 } 699 } 700 701 #ifndef NDEBUG 702 /// Dump the dependent variable set: 703 static void DumpDepVars(MultipleUseVarSet &DepVars) { 704 if (DepVars.empty()) { 705 DEBUG(errs() << "<empty set>"); 706 } else { 707 DEBUG(errs() << "[ "); 708 for (MultipleUseVarSet::const_iterator i = DepVars.begin(), 709 e = DepVars.end(); i != e; ++i) { 710 DEBUG(errs() << (*i) << " "); 711 } 712 DEBUG(errs() << "]"); 713 } 714 } 715 #endif 716 717 718 //===----------------------------------------------------------------------===// 719 // TreePredicateFn Implementation 720 //===----------------------------------------------------------------------===// 721 722 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag. 723 TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) { 724 assert((getPredCode().empty() || getImmCode().empty()) && 725 ".td file corrupt: can't have a node predicate *and* an imm predicate"); 726 } 727 728 std::string TreePredicateFn::getPredCode() const { 729 return PatFragRec->getRecord()->getValueAsString("PredicateCode"); 730 } 731 732 std::string TreePredicateFn::getImmCode() const { 733 return PatFragRec->getRecord()->getValueAsString("ImmediateCode"); 734 } 735 736 737 /// isAlwaysTrue - Return true if this is a noop predicate. 738 bool TreePredicateFn::isAlwaysTrue() const { 739 return getPredCode().empty() && getImmCode().empty(); 740 } 741 742 /// Return the name to use in the generated code to reference this, this is 743 /// "Predicate_foo" if from a pattern fragment "foo". 744 std::string TreePredicateFn::getFnName() const { 745 return "Predicate_" + PatFragRec->getRecord()->getName(); 746 } 747 748 /// getCodeToRunOnSDNode - Return the code for the function body that 749 /// evaluates this predicate. The argument is expected to be in "Node", 750 /// not N. This handles casting and conversion to a concrete node type as 751 /// appropriate. 752 std::string TreePredicateFn::getCodeToRunOnSDNode() const { 753 // Handle immediate predicates first. 754 std::string ImmCode = getImmCode(); 755 if (!ImmCode.empty()) { 756 std::string Result = 757 " int64_t Imm = cast<ConstantSDNode>(Node)->getSExtValue();\n"; 758 return Result + ImmCode; 759 } 760 761 // Handle arbitrary node predicates. 762 assert(!getPredCode().empty() && "Don't have any predicate code!"); 763 std::string ClassName; 764 if (PatFragRec->getOnlyTree()->isLeaf()) 765 ClassName = "SDNode"; 766 else { 767 Record *Op = PatFragRec->getOnlyTree()->getOperator(); 768 ClassName = PatFragRec->getDAGPatterns().getSDNodeInfo(Op).getSDClassName(); 769 } 770 std::string Result; 771 if (ClassName == "SDNode") 772 Result = " SDNode *N = Node;\n"; 773 else 774 Result = " " + ClassName + "*N = cast<" + ClassName + ">(Node);\n"; 775 776 return Result + getPredCode(); 777 } 778 779 //===----------------------------------------------------------------------===// 780 // PatternToMatch implementation 781 // 782 783 784 /// getPatternSize - Return the 'size' of this pattern. We want to match large 785 /// patterns before small ones. This is used to determine the size of a 786 /// pattern. 787 static unsigned getPatternSize(const TreePatternNode *P, 788 const CodeGenDAGPatterns &CGP) { 789 unsigned Size = 3; // The node itself. 790 // If the root node is a ConstantSDNode, increases its size. 791 // e.g. (set R32:$dst, 0). 792 if (P->isLeaf() && isa<IntInit>(P->getLeafValue())) 793 Size += 2; 794 795 // FIXME: This is a hack to statically increase the priority of patterns 796 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. 797 // Later we can allow complexity / cost for each pattern to be (optionally) 798 // specified. To get best possible pattern match we'll need to dynamically 799 // calculate the complexity of all patterns a dag can potentially map to. 800 const ComplexPattern *AM = P->getComplexPatternInfo(CGP); 801 if (AM) { 802 Size += AM->getNumOperands() * 3; 803 804 // We don't want to count any children twice, so return early. 805 return Size; 806 } 807 808 // If this node has some predicate function that must match, it adds to the 809 // complexity of this node. 810 if (!P->getPredicateFns().empty()) 811 ++Size; 812 813 // Count children in the count if they are also nodes. 814 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { 815 TreePatternNode *Child = P->getChild(i); 816 if (!Child->isLeaf() && Child->getNumTypes() && 817 Child->getType(0) != MVT::Other) 818 Size += getPatternSize(Child, CGP); 819 else if (Child->isLeaf()) { 820 if (isa<IntInit>(Child->getLeafValue())) 821 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2). 822 else if (Child->getComplexPatternInfo(CGP)) 823 Size += getPatternSize(Child, CGP); 824 else if (!Child->getPredicateFns().empty()) 825 ++Size; 826 } 827 } 828 829 return Size; 830 } 831 832 /// Compute the complexity metric for the input pattern. This roughly 833 /// corresponds to the number of nodes that are covered. 834 int PatternToMatch:: 835 getPatternComplexity(const CodeGenDAGPatterns &CGP) const { 836 return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity(); 837 } 838 839 840 /// getPredicateCheck - Return a single string containing all of this 841 /// pattern's predicates concatenated with "&&" operators. 842 /// 843 std::string PatternToMatch::getPredicateCheck() const { 844 std::string PredicateCheck; 845 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { 846 if (DefInit *Pred = dyn_cast<DefInit>(Predicates->getElement(i))) { 847 Record *Def = Pred->getDef(); 848 if (!Def->isSubClassOf("Predicate")) { 849 #ifndef NDEBUG 850 Def->dump(); 851 #endif 852 llvm_unreachable("Unknown predicate type!"); 853 } 854 if (!PredicateCheck.empty()) 855 PredicateCheck += " && "; 856 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; 857 } 858 } 859 860 return PredicateCheck; 861 } 862 863 //===----------------------------------------------------------------------===// 864 // SDTypeConstraint implementation 865 // 866 867 SDTypeConstraint::SDTypeConstraint(Record *R) { 868 OperandNo = R->getValueAsInt("OperandNum"); 869 870 if (R->isSubClassOf("SDTCisVT")) { 871 ConstraintType = SDTCisVT; 872 x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); 873 if (x.SDTCisVT_Info.VT == MVT::isVoid) 874 PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT"); 875 876 } else if (R->isSubClassOf("SDTCisPtrTy")) { 877 ConstraintType = SDTCisPtrTy; 878 } else if (R->isSubClassOf("SDTCisInt")) { 879 ConstraintType = SDTCisInt; 880 } else if (R->isSubClassOf("SDTCisFP")) { 881 ConstraintType = SDTCisFP; 882 } else if (R->isSubClassOf("SDTCisVec")) { 883 ConstraintType = SDTCisVec; 884 } else if (R->isSubClassOf("SDTCisSameAs")) { 885 ConstraintType = SDTCisSameAs; 886 x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); 887 } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { 888 ConstraintType = SDTCisVTSmallerThanOp; 889 x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = 890 R->getValueAsInt("OtherOperandNum"); 891 } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { 892 ConstraintType = SDTCisOpSmallerThanOp; 893 x.SDTCisOpSmallerThanOp_Info.BigOperandNum = 894 R->getValueAsInt("BigOperandNum"); 895 } else if (R->isSubClassOf("SDTCisEltOfVec")) { 896 ConstraintType = SDTCisEltOfVec; 897 x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum"); 898 } else if (R->isSubClassOf("SDTCisSubVecOfVec")) { 899 ConstraintType = SDTCisSubVecOfVec; 900 x.SDTCisSubVecOfVec_Info.OtherOperandNum = 901 R->getValueAsInt("OtherOpNum"); 902 } else if (R->isSubClassOf("SDTCVecEltisVT")) { 903 ConstraintType = SDTCVecEltisVT; 904 x.SDTCVecEltisVT_Info.VT = getValueType(R->getValueAsDef("VT")); 905 if (MVT(x.SDTCVecEltisVT_Info.VT).isVector()) 906 PrintFatalError(R->getLoc(), "Cannot use vector type as SDTCVecEltisVT"); 907 if (!MVT(x.SDTCVecEltisVT_Info.VT).isInteger() && 908 !MVT(x.SDTCVecEltisVT_Info.VT).isFloatingPoint()) 909 PrintFatalError(R->getLoc(), "Must use integer or floating point type " 910 "as SDTCVecEltisVT"); 911 } else if (R->isSubClassOf("SDTCisSameNumEltsAs")) { 912 ConstraintType = SDTCisSameNumEltsAs; 913 x.SDTCisSameNumEltsAs_Info.OtherOperandNum = 914 R->getValueAsInt("OtherOperandNum"); 915 } else { 916 PrintFatalError("Unrecognized SDTypeConstraint '" + R->getName() + "'!\n"); 917 } 918 } 919 920 /// getOperandNum - Return the node corresponding to operand #OpNo in tree 921 /// N, and the result number in ResNo. 922 static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N, 923 const SDNodeInfo &NodeInfo, 924 unsigned &ResNo) { 925 unsigned NumResults = NodeInfo.getNumResults(); 926 if (OpNo < NumResults) { 927 ResNo = OpNo; 928 return N; 929 } 930 931 OpNo -= NumResults; 932 933 if (OpNo >= N->getNumChildren()) { 934 std::string S; 935 raw_string_ostream OS(S); 936 OS << "Invalid operand number in type constraint " 937 << (OpNo+NumResults) << " "; 938 N->print(OS); 939 PrintFatalError(OS.str()); 940 } 941 942 return N->getChild(OpNo); 943 } 944 945 /// ApplyTypeConstraint - Given a node in a pattern, apply this type 946 /// constraint to the nodes operands. This returns true if it makes a 947 /// change, false otherwise. If a type contradiction is found, flag an error. 948 bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, 949 const SDNodeInfo &NodeInfo, 950 TreePattern &TP) const { 951 if (TP.hasError()) 952 return false; 953 954 unsigned ResNo = 0; // The result number being referenced. 955 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo); 956 957 switch (ConstraintType) { 958 case SDTCisVT: 959 // Operand must be a particular type. 960 return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP); 961 case SDTCisPtrTy: 962 // Operand must be same as target pointer type. 963 return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP); 964 case SDTCisInt: 965 // Require it to be one of the legal integer VTs. 966 return NodeToApply->getExtType(ResNo).EnforceInteger(TP); 967 case SDTCisFP: 968 // Require it to be one of the legal fp VTs. 969 return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP); 970 case SDTCisVec: 971 // Require it to be one of the legal vector VTs. 972 return NodeToApply->getExtType(ResNo).EnforceVector(TP); 973 case SDTCisSameAs: { 974 unsigned OResNo = 0; 975 TreePatternNode *OtherNode = 976 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo); 977 return NodeToApply->UpdateNodeType(ResNo, OtherNode->getExtType(OResNo),TP)| 978 OtherNode->UpdateNodeType(OResNo,NodeToApply->getExtType(ResNo),TP); 979 } 980 case SDTCisVTSmallerThanOp: { 981 // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must 982 // have an integer type that is smaller than the VT. 983 if (!NodeToApply->isLeaf() || 984 !isa<DefInit>(NodeToApply->getLeafValue()) || 985 !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() 986 ->isSubClassOf("ValueType")) { 987 TP.error(N->getOperator()->getName() + " expects a VT operand!"); 988 return false; 989 } 990 MVT::SimpleValueType VT = 991 getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); 992 993 EEVT::TypeSet TypeListTmp(VT, TP); 994 995 unsigned OResNo = 0; 996 TreePatternNode *OtherNode = 997 getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo, 998 OResNo); 999 1000 return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP); 1001 } 1002 case SDTCisOpSmallerThanOp: { 1003 unsigned BResNo = 0; 1004 TreePatternNode *BigOperand = 1005 getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo, 1006 BResNo); 1007 return NodeToApply->getExtType(ResNo). 1008 EnforceSmallerThan(BigOperand->getExtType(BResNo), TP); 1009 } 1010 case SDTCisEltOfVec: { 1011 unsigned VResNo = 0; 1012 TreePatternNode *VecOperand = 1013 getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo, 1014 VResNo); 1015 1016 // Filter vector types out of VecOperand that don't have the right element 1017 // type. 1018 return VecOperand->getExtType(VResNo). 1019 EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP); 1020 } 1021 case SDTCisSubVecOfVec: { 1022 unsigned VResNo = 0; 1023 TreePatternNode *BigVecOperand = 1024 getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo, 1025 VResNo); 1026 1027 // Filter vector types out of BigVecOperand that don't have the 1028 // right subvector type. 1029 return BigVecOperand->getExtType(VResNo). 1030 EnforceVectorSubVectorTypeIs(NodeToApply->getExtType(ResNo), TP); 1031 } 1032 case SDTCVecEltisVT: { 1033 return NodeToApply->getExtType(ResNo). 1034 EnforceVectorEltTypeIs(x.SDTCVecEltisVT_Info.VT, TP); 1035 } 1036 case SDTCisSameNumEltsAs: { 1037 unsigned OResNo = 0; 1038 TreePatternNode *OtherNode = 1039 getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum, 1040 N, NodeInfo, OResNo); 1041 return OtherNode->getExtType(OResNo). 1042 EnforceVectorSameNumElts(NodeToApply->getExtType(ResNo), TP); 1043 } 1044 } 1045 llvm_unreachable("Invalid ConstraintType!"); 1046 } 1047 1048 // Update the node type to match an instruction operand or result as specified 1049 // in the ins or outs lists on the instruction definition. Return true if the 1050 // type was actually changed. 1051 bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo, 1052 Record *Operand, 1053 TreePattern &TP) { 1054 // The 'unknown' operand indicates that types should be inferred from the 1055 // context. 1056 if (Operand->isSubClassOf("unknown_class")) 1057 return false; 1058 1059 // The Operand class specifies a type directly. 1060 if (Operand->isSubClassOf("Operand")) 1061 return UpdateNodeType(ResNo, getValueType(Operand->getValueAsDef("Type")), 1062 TP); 1063 1064 // PointerLikeRegClass has a type that is determined at runtime. 1065 if (Operand->isSubClassOf("PointerLikeRegClass")) 1066 return UpdateNodeType(ResNo, MVT::iPTR, TP); 1067 1068 // Both RegisterClass and RegisterOperand operands derive their types from a 1069 // register class def. 1070 Record *RC = nullptr; 1071 if (Operand->isSubClassOf("RegisterClass")) 1072 RC = Operand; 1073 else if (Operand->isSubClassOf("RegisterOperand")) 1074 RC = Operand->getValueAsDef("RegClass"); 1075 1076 assert(RC && "Unknown operand type"); 1077 CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo(); 1078 return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP); 1079 } 1080 1081 1082 //===----------------------------------------------------------------------===// 1083 // SDNodeInfo implementation 1084 // 1085 SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { 1086 EnumName = R->getValueAsString("Opcode"); 1087 SDClassName = R->getValueAsString("SDClass"); 1088 Record *TypeProfile = R->getValueAsDef("TypeProfile"); 1089 NumResults = TypeProfile->getValueAsInt("NumResults"); 1090 NumOperands = TypeProfile->getValueAsInt("NumOperands"); 1091 1092 // Parse the properties. 1093 Properties = 0; 1094 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); 1095 for (unsigned i = 0, e = PropList.size(); i != e; ++i) { 1096 if (PropList[i]->getName() == "SDNPCommutative") { 1097 Properties |= 1 << SDNPCommutative; 1098 } else if (PropList[i]->getName() == "SDNPAssociative") { 1099 Properties |= 1 << SDNPAssociative; 1100 } else if (PropList[i]->getName() == "SDNPHasChain") { 1101 Properties |= 1 << SDNPHasChain; 1102 } else if (PropList[i]->getName() == "SDNPOutGlue") { 1103 Properties |= 1 << SDNPOutGlue; 1104 } else if (PropList[i]->getName() == "SDNPInGlue") { 1105 Properties |= 1 << SDNPInGlue; 1106 } else if (PropList[i]->getName() == "SDNPOptInGlue") { 1107 Properties |= 1 << SDNPOptInGlue; 1108 } else if (PropList[i]->getName() == "SDNPMayStore") { 1109 Properties |= 1 << SDNPMayStore; 1110 } else if (PropList[i]->getName() == "SDNPMayLoad") { 1111 Properties |= 1 << SDNPMayLoad; 1112 } else if (PropList[i]->getName() == "SDNPSideEffect") { 1113 Properties |= 1 << SDNPSideEffect; 1114 } else if (PropList[i]->getName() == "SDNPMemOperand") { 1115 Properties |= 1 << SDNPMemOperand; 1116 } else if (PropList[i]->getName() == "SDNPVariadic") { 1117 Properties |= 1 << SDNPVariadic; 1118 } else { 1119 PrintFatalError("Unknown SD Node property '" + 1120 PropList[i]->getName() + "' on node '" + 1121 R->getName() + "'!"); 1122 } 1123 } 1124 1125 1126 // Parse the type constraints. 1127 std::vector<Record*> ConstraintList = 1128 TypeProfile->getValueAsListOfDefs("Constraints"); 1129 TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); 1130 } 1131 1132 /// getKnownType - If the type constraints on this node imply a fixed type 1133 /// (e.g. all stores return void, etc), then return it as an 1134 /// MVT::SimpleValueType. Otherwise, return EEVT::Other. 1135 MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const { 1136 unsigned NumResults = getNumResults(); 1137 assert(NumResults <= 1 && 1138 "We only work with nodes with zero or one result so far!"); 1139 assert(ResNo == 0 && "Only handles single result nodes so far"); 1140 1141 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) { 1142 // Make sure that this applies to the correct node result. 1143 if (TypeConstraints[i].OperandNo >= NumResults) // FIXME: need value # 1144 continue; 1145 1146 switch (TypeConstraints[i].ConstraintType) { 1147 default: break; 1148 case SDTypeConstraint::SDTCisVT: 1149 return TypeConstraints[i].x.SDTCisVT_Info.VT; 1150 case SDTypeConstraint::SDTCisPtrTy: 1151 return MVT::iPTR; 1152 } 1153 } 1154 return MVT::Other; 1155 } 1156 1157 //===----------------------------------------------------------------------===// 1158 // TreePatternNode implementation 1159 // 1160 1161 TreePatternNode::~TreePatternNode() { 1162 #if 0 // FIXME: implement refcounted tree nodes! 1163 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1164 delete getChild(i); 1165 #endif 1166 } 1167 1168 static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) { 1169 if (Operator->getName() == "set" || 1170 Operator->getName() == "implicit") 1171 return 0; // All return nothing. 1172 1173 if (Operator->isSubClassOf("Intrinsic")) 1174 return CDP.getIntrinsic(Operator).IS.RetVTs.size(); 1175 1176 if (Operator->isSubClassOf("SDNode")) 1177 return CDP.getSDNodeInfo(Operator).getNumResults(); 1178 1179 if (Operator->isSubClassOf("PatFrag")) { 1180 // If we've already parsed this pattern fragment, get it. Otherwise, handle 1181 // the forward reference case where one pattern fragment references another 1182 // before it is processed. 1183 if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) 1184 return PFRec->getOnlyTree()->getNumTypes(); 1185 1186 // Get the result tree. 1187 DagInit *Tree = Operator->getValueAsDag("Fragment"); 1188 Record *Op = nullptr; 1189 if (Tree) 1190 if (DefInit *DI = dyn_cast<DefInit>(Tree->getOperator())) 1191 Op = DI->getDef(); 1192 assert(Op && "Invalid Fragment"); 1193 return GetNumNodeResults(Op, CDP); 1194 } 1195 1196 if (Operator->isSubClassOf("Instruction")) { 1197 CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator); 1198 1199 unsigned NumDefsToAdd = InstInfo.Operands.NumDefs; 1200 1201 // Subtract any defaulted outputs. 1202 for (unsigned i = 0; i != InstInfo.Operands.NumDefs; ++i) { 1203 Record *OperandNode = InstInfo.Operands[i].Rec; 1204 1205 if (OperandNode->isSubClassOf("OperandWithDefaultOps") && 1206 !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) 1207 --NumDefsToAdd; 1208 } 1209 1210 // Add on one implicit def if it has a resolvable type. 1211 if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other) 1212 ++NumDefsToAdd; 1213 return NumDefsToAdd; 1214 } 1215 1216 if (Operator->isSubClassOf("SDNodeXForm")) 1217 return 1; // FIXME: Generalize SDNodeXForm 1218 1219 if (Operator->isSubClassOf("ValueType")) 1220 return 1; // A type-cast of one result. 1221 1222 if (Operator->isSubClassOf("ComplexPattern")) 1223 return 1; 1224 1225 Operator->dump(); 1226 PrintFatalError("Unhandled node in GetNumNodeResults"); 1227 } 1228 1229 void TreePatternNode::print(raw_ostream &OS) const { 1230 if (isLeaf()) 1231 OS << *getLeafValue(); 1232 else 1233 OS << '(' << getOperator()->getName(); 1234 1235 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1236 OS << ':' << getExtType(i).getName(); 1237 1238 if (!isLeaf()) { 1239 if (getNumChildren() != 0) { 1240 OS << " "; 1241 getChild(0)->print(OS); 1242 for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { 1243 OS << ", "; 1244 getChild(i)->print(OS); 1245 } 1246 } 1247 OS << ")"; 1248 } 1249 1250 for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i) 1251 OS << "<<P:" << PredicateFns[i].getFnName() << ">>"; 1252 if (TransformFn) 1253 OS << "<<X:" << TransformFn->getName() << ">>"; 1254 if (!getName().empty()) 1255 OS << ":$" << getName(); 1256 1257 } 1258 void TreePatternNode::dump() const { 1259 print(errs()); 1260 } 1261 1262 /// isIsomorphicTo - Return true if this node is recursively 1263 /// isomorphic to the specified node. For this comparison, the node's 1264 /// entire state is considered. The assigned name is ignored, since 1265 /// nodes with differing names are considered isomorphic. However, if 1266 /// the assigned name is present in the dependent variable set, then 1267 /// the assigned name is considered significant and the node is 1268 /// isomorphic if the names match. 1269 bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, 1270 const MultipleUseVarSet &DepVars) const { 1271 if (N == this) return true; 1272 if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || 1273 getPredicateFns() != N->getPredicateFns() || 1274 getTransformFn() != N->getTransformFn()) 1275 return false; 1276 1277 if (isLeaf()) { 1278 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 1279 if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) { 1280 return ((DI->getDef() == NDI->getDef()) 1281 && (DepVars.find(getName()) == DepVars.end() 1282 || getName() == N->getName())); 1283 } 1284 } 1285 return getLeafValue() == N->getLeafValue(); 1286 } 1287 1288 if (N->getOperator() != getOperator() || 1289 N->getNumChildren() != getNumChildren()) return false; 1290 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1291 if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars)) 1292 return false; 1293 return true; 1294 } 1295 1296 /// clone - Make a copy of this tree and all of its children. 1297 /// 1298 TreePatternNode *TreePatternNode::clone() const { 1299 TreePatternNode *New; 1300 if (isLeaf()) { 1301 New = new TreePatternNode(getLeafValue(), getNumTypes()); 1302 } else { 1303 std::vector<TreePatternNode*> CChildren; 1304 CChildren.reserve(Children.size()); 1305 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1306 CChildren.push_back(getChild(i)->clone()); 1307 New = new TreePatternNode(getOperator(), CChildren, getNumTypes()); 1308 } 1309 New->setName(getName()); 1310 New->Types = Types; 1311 New->setPredicateFns(getPredicateFns()); 1312 New->setTransformFn(getTransformFn()); 1313 return New; 1314 } 1315 1316 /// RemoveAllTypes - Recursively strip all the types of this tree. 1317 void TreePatternNode::RemoveAllTypes() { 1318 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1319 Types[i] = EEVT::TypeSet(); // Reset to unknown type. 1320 if (isLeaf()) return; 1321 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1322 getChild(i)->RemoveAllTypes(); 1323 } 1324 1325 1326 /// SubstituteFormalArguments - Replace the formal arguments in this tree 1327 /// with actual values specified by ArgMap. 1328 void TreePatternNode:: 1329 SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { 1330 if (isLeaf()) return; 1331 1332 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1333 TreePatternNode *Child = getChild(i); 1334 if (Child->isLeaf()) { 1335 Init *Val = Child->getLeafValue(); 1336 // Note that, when substituting into an output pattern, Val might be an 1337 // UnsetInit. 1338 if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) && 1339 cast<DefInit>(Val)->getDef()->getName() == "node")) { 1340 // We found a use of a formal argument, replace it with its value. 1341 TreePatternNode *NewChild = ArgMap[Child->getName()]; 1342 assert(NewChild && "Couldn't find formal argument!"); 1343 assert((Child->getPredicateFns().empty() || 1344 NewChild->getPredicateFns() == Child->getPredicateFns()) && 1345 "Non-empty child predicate clobbered!"); 1346 setChild(i, NewChild); 1347 } 1348 } else { 1349 getChild(i)->SubstituteFormalArguments(ArgMap); 1350 } 1351 } 1352 } 1353 1354 1355 /// InlinePatternFragments - If this pattern refers to any pattern 1356 /// fragments, inline them into place, giving us a pattern without any 1357 /// PatFrag references. 1358 TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { 1359 if (TP.hasError()) 1360 return nullptr; 1361 1362 if (isLeaf()) 1363 return this; // nothing to do. 1364 Record *Op = getOperator(); 1365 1366 if (!Op->isSubClassOf("PatFrag")) { 1367 // Just recursively inline children nodes. 1368 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1369 TreePatternNode *Child = getChild(i); 1370 TreePatternNode *NewChild = Child->InlinePatternFragments(TP); 1371 1372 assert((Child->getPredicateFns().empty() || 1373 NewChild->getPredicateFns() == Child->getPredicateFns()) && 1374 "Non-empty child predicate clobbered!"); 1375 1376 setChild(i, NewChild); 1377 } 1378 return this; 1379 } 1380 1381 // Otherwise, we found a reference to a fragment. First, look up its 1382 // TreePattern record. 1383 TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); 1384 1385 // Verify that we are passing the right number of operands. 1386 if (Frag->getNumArgs() != Children.size()) { 1387 TP.error("'" + Op->getName() + "' fragment requires " + 1388 utostr(Frag->getNumArgs()) + " operands!"); 1389 return nullptr; 1390 } 1391 1392 TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); 1393 1394 TreePredicateFn PredFn(Frag); 1395 if (!PredFn.isAlwaysTrue()) 1396 FragTree->addPredicateFn(PredFn); 1397 1398 // Resolve formal arguments to their actual value. 1399 if (Frag->getNumArgs()) { 1400 // Compute the map of formal to actual arguments. 1401 std::map<std::string, TreePatternNode*> ArgMap; 1402 for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) 1403 ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); 1404 1405 FragTree->SubstituteFormalArguments(ArgMap); 1406 } 1407 1408 FragTree->setName(getName()); 1409 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1410 FragTree->UpdateNodeType(i, getExtType(i), TP); 1411 1412 // Transfer in the old predicates. 1413 for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i) 1414 FragTree->addPredicateFn(getPredicateFns()[i]); 1415 1416 // Get a new copy of this fragment to stitch into here. 1417 //delete this; // FIXME: implement refcounting! 1418 1419 // The fragment we inlined could have recursive inlining that is needed. See 1420 // if there are any pattern fragments in it and inline them as needed. 1421 return FragTree->InlinePatternFragments(TP); 1422 } 1423 1424 /// getImplicitType - Check to see if the specified record has an implicit 1425 /// type which should be applied to it. This will infer the type of register 1426 /// references from the register file information, for example. 1427 /// 1428 /// When Unnamed is set, return the type of a DAG operand with no name, such as 1429 /// the F8RC register class argument in: 1430 /// 1431 /// (COPY_TO_REGCLASS GPR:$src, F8RC) 1432 /// 1433 /// When Unnamed is false, return the type of a named DAG operand such as the 1434 /// GPR:$src operand above. 1435 /// 1436 static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo, 1437 bool NotRegisters, 1438 bool Unnamed, 1439 TreePattern &TP) { 1440 // Check to see if this is a register operand. 1441 if (R->isSubClassOf("RegisterOperand")) { 1442 assert(ResNo == 0 && "Regoperand ref only has one result!"); 1443 if (NotRegisters) 1444 return EEVT::TypeSet(); // Unknown. 1445 Record *RegClass = R->getValueAsDef("RegClass"); 1446 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1447 return EEVT::TypeSet(T.getRegisterClass(RegClass).getValueTypes()); 1448 } 1449 1450 // Check to see if this is a register or a register class. 1451 if (R->isSubClassOf("RegisterClass")) { 1452 assert(ResNo == 0 && "Regclass ref only has one result!"); 1453 // An unnamed register class represents itself as an i32 immediate, for 1454 // example on a COPY_TO_REGCLASS instruction. 1455 if (Unnamed) 1456 return EEVT::TypeSet(MVT::i32, TP); 1457 1458 // In a named operand, the register class provides the possible set of 1459 // types. 1460 if (NotRegisters) 1461 return EEVT::TypeSet(); // Unknown. 1462 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1463 return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes()); 1464 } 1465 1466 if (R->isSubClassOf("PatFrag")) { 1467 assert(ResNo == 0 && "FIXME: PatFrag with multiple results?"); 1468 // Pattern fragment types will be resolved when they are inlined. 1469 return EEVT::TypeSet(); // Unknown. 1470 } 1471 1472 if (R->isSubClassOf("Register")) { 1473 assert(ResNo == 0 && "Registers only produce one result!"); 1474 if (NotRegisters) 1475 return EEVT::TypeSet(); // Unknown. 1476 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1477 return EEVT::TypeSet(T.getRegisterVTs(R)); 1478 } 1479 1480 if (R->isSubClassOf("SubRegIndex")) { 1481 assert(ResNo == 0 && "SubRegisterIndices only produce one result!"); 1482 return EEVT::TypeSet(MVT::i32, TP); 1483 } 1484 1485 if (R->isSubClassOf("ValueType")) { 1486 assert(ResNo == 0 && "This node only has one result!"); 1487 // An unnamed VTSDNode represents itself as an MVT::Other immediate. 1488 // 1489 // (sext_inreg GPR:$src, i16) 1490 // ~~~ 1491 if (Unnamed) 1492 return EEVT::TypeSet(MVT::Other, TP); 1493 // With a name, the ValueType simply provides the type of the named 1494 // variable. 1495 // 1496 // (sext_inreg i32:$src, i16) 1497 // ~~~~~~~~ 1498 if (NotRegisters) 1499 return EEVT::TypeSet(); // Unknown. 1500 return EEVT::TypeSet(getValueType(R), TP); 1501 } 1502 1503 if (R->isSubClassOf("CondCode")) { 1504 assert(ResNo == 0 && "This node only has one result!"); 1505 // Using a CondCodeSDNode. 1506 return EEVT::TypeSet(MVT::Other, TP); 1507 } 1508 1509 if (R->isSubClassOf("ComplexPattern")) { 1510 assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?"); 1511 if (NotRegisters) 1512 return EEVT::TypeSet(); // Unknown. 1513 return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(), 1514 TP); 1515 } 1516 if (R->isSubClassOf("PointerLikeRegClass")) { 1517 assert(ResNo == 0 && "Regclass can only have one result!"); 1518 return EEVT::TypeSet(MVT::iPTR, TP); 1519 } 1520 1521 if (R->getName() == "node" || R->getName() == "srcvalue" || 1522 R->getName() == "zero_reg") { 1523 // Placeholder. 1524 return EEVT::TypeSet(); // Unknown. 1525 } 1526 1527 if (R->isSubClassOf("Operand")) 1528 return EEVT::TypeSet(getValueType(R->getValueAsDef("Type"))); 1529 1530 TP.error("Unknown node flavor used in pattern: " + R->getName()); 1531 return EEVT::TypeSet(MVT::Other, TP); 1532 } 1533 1534 1535 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the 1536 /// CodeGenIntrinsic information for it, otherwise return a null pointer. 1537 const CodeGenIntrinsic *TreePatternNode:: 1538 getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { 1539 if (getOperator() != CDP.get_intrinsic_void_sdnode() && 1540 getOperator() != CDP.get_intrinsic_w_chain_sdnode() && 1541 getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) 1542 return nullptr; 1543 1544 unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue(); 1545 return &CDP.getIntrinsicInfo(IID); 1546 } 1547 1548 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern, 1549 /// return the ComplexPattern information, otherwise return null. 1550 const ComplexPattern * 1551 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const { 1552 Record *Rec; 1553 if (isLeaf()) { 1554 DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 1555 if (!DI) 1556 return nullptr; 1557 Rec = DI->getDef(); 1558 } else 1559 Rec = getOperator(); 1560 1561 if (!Rec->isSubClassOf("ComplexPattern")) 1562 return nullptr; 1563 return &CGP.getComplexPattern(Rec); 1564 } 1565 1566 unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const { 1567 // A ComplexPattern specifically declares how many results it fills in. 1568 if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) 1569 return CP->getNumOperands(); 1570 1571 // If MIOperandInfo is specified, that gives the count. 1572 if (isLeaf()) { 1573 DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 1574 if (DI && DI->getDef()->isSubClassOf("Operand")) { 1575 DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo"); 1576 if (MIOps->getNumArgs()) 1577 return MIOps->getNumArgs(); 1578 } 1579 } 1580 1581 // Otherwise there is just one result. 1582 return 1; 1583 } 1584 1585 /// NodeHasProperty - Return true if this node has the specified property. 1586 bool TreePatternNode::NodeHasProperty(SDNP Property, 1587 const CodeGenDAGPatterns &CGP) const { 1588 if (isLeaf()) { 1589 if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) 1590 return CP->hasProperty(Property); 1591 return false; 1592 } 1593 1594 Record *Operator = getOperator(); 1595 if (!Operator->isSubClassOf("SDNode")) return false; 1596 1597 return CGP.getSDNodeInfo(Operator).hasProperty(Property); 1598 } 1599 1600 1601 1602 1603 /// TreeHasProperty - Return true if any node in this tree has the specified 1604 /// property. 1605 bool TreePatternNode::TreeHasProperty(SDNP Property, 1606 const CodeGenDAGPatterns &CGP) const { 1607 if (NodeHasProperty(Property, CGP)) 1608 return true; 1609 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1610 if (getChild(i)->TreeHasProperty(Property, CGP)) 1611 return true; 1612 return false; 1613 } 1614 1615 /// isCommutativeIntrinsic - Return true if the node corresponds to a 1616 /// commutative intrinsic. 1617 bool 1618 TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const { 1619 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) 1620 return Int->isCommutative; 1621 return false; 1622 } 1623 1624 static bool isOperandClass(const TreePatternNode *N, StringRef Class) { 1625 if (!N->isLeaf()) 1626 return N->getOperator()->isSubClassOf(Class); 1627 1628 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue()); 1629 if (DI && DI->getDef()->isSubClassOf(Class)) 1630 return true; 1631 1632 return false; 1633 } 1634 1635 static void emitTooManyOperandsError(TreePattern &TP, 1636 StringRef InstName, 1637 unsigned Expected, 1638 unsigned Actual) { 1639 TP.error("Instruction '" + InstName + "' was provided " + Twine(Actual) + 1640 " operands but expected only " + Twine(Expected) + "!"); 1641 } 1642 1643 static void emitTooFewOperandsError(TreePattern &TP, 1644 StringRef InstName, 1645 unsigned Actual) { 1646 TP.error("Instruction '" + InstName + 1647 "' expects more than the provided " + Twine(Actual) + " operands!"); 1648 } 1649 1650 /// ApplyTypeConstraints - Apply all of the type constraints relevant to 1651 /// this node and its children in the tree. This returns true if it makes a 1652 /// change, false otherwise. If a type contradiction is found, flag an error. 1653 bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { 1654 if (TP.hasError()) 1655 return false; 1656 1657 CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); 1658 if (isLeaf()) { 1659 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 1660 // If it's a regclass or something else known, include the type. 1661 bool MadeChange = false; 1662 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1663 MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i, 1664 NotRegisters, 1665 !hasName(), TP), TP); 1666 return MadeChange; 1667 } 1668 1669 if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) { 1670 assert(Types.size() == 1 && "Invalid IntInit"); 1671 1672 // Int inits are always integers. :) 1673 bool MadeChange = Types[0].EnforceInteger(TP); 1674 1675 if (!Types[0].isConcrete()) 1676 return MadeChange; 1677 1678 MVT::SimpleValueType VT = getType(0); 1679 if (VT == MVT::iPTR || VT == MVT::iPTRAny) 1680 return MadeChange; 1681 1682 unsigned Size = MVT(VT).getSizeInBits(); 1683 // Make sure that the value is representable for this type. 1684 if (Size >= 32) return MadeChange; 1685 1686 // Check that the value doesn't use more bits than we have. It must either 1687 // be a sign- or zero-extended equivalent of the original. 1688 int64_t SignBitAndAbove = II->getValue() >> (Size - 1); 1689 if (SignBitAndAbove == -1 || SignBitAndAbove == 0 || SignBitAndAbove == 1) 1690 return MadeChange; 1691 1692 TP.error("Integer value '" + itostr(II->getValue()) + 1693 "' is out of range for type '" + getEnumName(getType(0)) + "'!"); 1694 return false; 1695 } 1696 return false; 1697 } 1698 1699 // special handling for set, which isn't really an SDNode. 1700 if (getOperator()->getName() == "set") { 1701 assert(getNumTypes() == 0 && "Set doesn't produce a value"); 1702 assert(getNumChildren() >= 2 && "Missing RHS of a set?"); 1703 unsigned NC = getNumChildren(); 1704 1705 TreePatternNode *SetVal = getChild(NC-1); 1706 bool MadeChange = SetVal->ApplyTypeConstraints(TP, NotRegisters); 1707 1708 for (unsigned i = 0; i < NC-1; ++i) { 1709 TreePatternNode *Child = getChild(i); 1710 MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters); 1711 1712 // Types of operands must match. 1713 MadeChange |= Child->UpdateNodeType(0, SetVal->getExtType(i), TP); 1714 MadeChange |= SetVal->UpdateNodeType(i, Child->getExtType(0), TP); 1715 } 1716 return MadeChange; 1717 } 1718 1719 if (getOperator()->getName() == "implicit") { 1720 assert(getNumTypes() == 0 && "Node doesn't produce a value"); 1721 1722 bool MadeChange = false; 1723 for (unsigned i = 0; i < getNumChildren(); ++i) 1724 MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1725 return MadeChange; 1726 } 1727 1728 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) { 1729 bool MadeChange = false; 1730 1731 // Apply the result type to the node. 1732 unsigned NumRetVTs = Int->IS.RetVTs.size(); 1733 unsigned NumParamVTs = Int->IS.ParamVTs.size(); 1734 1735 for (unsigned i = 0, e = NumRetVTs; i != e; ++i) 1736 MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP); 1737 1738 if (getNumChildren() != NumParamVTs + 1) { 1739 TP.error("Intrinsic '" + Int->Name + "' expects " + 1740 utostr(NumParamVTs) + " operands, not " + 1741 utostr(getNumChildren() - 1) + " operands!"); 1742 return false; 1743 } 1744 1745 // Apply type info to the intrinsic ID. 1746 MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP); 1747 1748 for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) { 1749 MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters); 1750 1751 MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i]; 1752 assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case"); 1753 MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP); 1754 } 1755 return MadeChange; 1756 } 1757 1758 if (getOperator()->isSubClassOf("SDNode")) { 1759 const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator()); 1760 1761 // Check that the number of operands is sane. Negative operands -> varargs. 1762 if (NI.getNumOperands() >= 0 && 1763 getNumChildren() != (unsigned)NI.getNumOperands()) { 1764 TP.error(getOperator()->getName() + " node requires exactly " + 1765 itostr(NI.getNumOperands()) + " operands!"); 1766 return false; 1767 } 1768 1769 bool MadeChange = NI.ApplyTypeConstraints(this, TP); 1770 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1771 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1772 return MadeChange; 1773 } 1774 1775 if (getOperator()->isSubClassOf("Instruction")) { 1776 const DAGInstruction &Inst = CDP.getInstruction(getOperator()); 1777 CodeGenInstruction &InstInfo = 1778 CDP.getTargetInfo().getInstruction(getOperator()); 1779 1780 bool MadeChange = false; 1781 1782 // Apply the result types to the node, these come from the things in the 1783 // (outs) list of the instruction. 1784 unsigned NumResultsToAdd = std::min(InstInfo.Operands.NumDefs, 1785 Inst.getNumResults()); 1786 for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo) 1787 MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP); 1788 1789 // If the instruction has implicit defs, we apply the first one as a result. 1790 // FIXME: This sucks, it should apply all implicit defs. 1791 if (!InstInfo.ImplicitDefs.empty()) { 1792 unsigned ResNo = NumResultsToAdd; 1793 1794 // FIXME: Generalize to multiple possible types and multiple possible 1795 // ImplicitDefs. 1796 MVT::SimpleValueType VT = 1797 InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()); 1798 1799 if (VT != MVT::Other) 1800 MadeChange |= UpdateNodeType(ResNo, VT, TP); 1801 } 1802 1803 // If this is an INSERT_SUBREG, constrain the source and destination VTs to 1804 // be the same. 1805 if (getOperator()->getName() == "INSERT_SUBREG") { 1806 assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled"); 1807 MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP); 1808 MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP); 1809 } else if (getOperator()->getName() == "REG_SEQUENCE") { 1810 // We need to do extra, custom typechecking for REG_SEQUENCE since it is 1811 // variadic. 1812 1813 unsigned NChild = getNumChildren(); 1814 if (NChild < 3) { 1815 TP.error("REG_SEQUENCE requires at least 3 operands!"); 1816 return false; 1817 } 1818 1819 if (NChild % 2 == 0) { 1820 TP.error("REG_SEQUENCE requires an odd number of operands!"); 1821 return false; 1822 } 1823 1824 if (!isOperandClass(getChild(0), "RegisterClass")) { 1825 TP.error("REG_SEQUENCE requires a RegisterClass for first operand!"); 1826 return false; 1827 } 1828 1829 for (unsigned I = 1; I < NChild; I += 2) { 1830 TreePatternNode *SubIdxChild = getChild(I + 1); 1831 if (!isOperandClass(SubIdxChild, "SubRegIndex")) { 1832 TP.error("REG_SEQUENCE requires a SubRegIndex for operand " + 1833 itostr(I + 1) + "!"); 1834 return false; 1835 } 1836 } 1837 } 1838 1839 unsigned ChildNo = 0; 1840 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { 1841 Record *OperandNode = Inst.getOperand(i); 1842 1843 // If the instruction expects a predicate or optional def operand, we 1844 // codegen this by setting the operand to it's default value if it has a 1845 // non-empty DefaultOps field. 1846 if (OperandNode->isSubClassOf("OperandWithDefaultOps") && 1847 !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) 1848 continue; 1849 1850 // Verify that we didn't run out of provided operands. 1851 if (ChildNo >= getNumChildren()) { 1852 emitTooFewOperandsError(TP, getOperator()->getName(), getNumChildren()); 1853 return false; 1854 } 1855 1856 TreePatternNode *Child = getChild(ChildNo++); 1857 unsigned ChildResNo = 0; // Instructions always use res #0 of their op. 1858 1859 // If the operand has sub-operands, they may be provided by distinct 1860 // child patterns, so attempt to match each sub-operand separately. 1861 if (OperandNode->isSubClassOf("Operand")) { 1862 DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo"); 1863 if (unsigned NumArgs = MIOpInfo->getNumArgs()) { 1864 // But don't do that if the whole operand is being provided by 1865 // a single ComplexPattern-related Operand. 1866 1867 if (Child->getNumMIResults(CDP) < NumArgs) { 1868 // Match first sub-operand against the child we already have. 1869 Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef(); 1870 MadeChange |= 1871 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); 1872 1873 // And the remaining sub-operands against subsequent children. 1874 for (unsigned Arg = 1; Arg < NumArgs; ++Arg) { 1875 if (ChildNo >= getNumChildren()) { 1876 emitTooFewOperandsError(TP, getOperator()->getName(), 1877 getNumChildren()); 1878 return false; 1879 } 1880 Child = getChild(ChildNo++); 1881 1882 SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef(); 1883 MadeChange |= 1884 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); 1885 } 1886 continue; 1887 } 1888 } 1889 } 1890 1891 // If we didn't match by pieces above, attempt to match the whole 1892 // operand now. 1893 MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP); 1894 } 1895 1896 if (!InstInfo.Operands.isVariadic && ChildNo != getNumChildren()) { 1897 emitTooManyOperandsError(TP, getOperator()->getName(), 1898 ChildNo, getNumChildren()); 1899 return false; 1900 } 1901 1902 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1903 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1904 return MadeChange; 1905 } 1906 1907 if (getOperator()->isSubClassOf("ComplexPattern")) { 1908 bool MadeChange = false; 1909 1910 for (unsigned i = 0; i < getNumChildren(); ++i) 1911 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1912 1913 return MadeChange; 1914 } 1915 1916 assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); 1917 1918 // Node transforms always take one operand. 1919 if (getNumChildren() != 1) { 1920 TP.error("Node transform '" + getOperator()->getName() + 1921 "' requires one operand!"); 1922 return false; 1923 } 1924 1925 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters); 1926 1927 1928 // If either the output or input of the xform does not have exact 1929 // type info. We assume they must be the same. Otherwise, it is perfectly 1930 // legal to transform from one type to a completely different type. 1931 #if 0 1932 if (!hasTypeSet() || !getChild(0)->hasTypeSet()) { 1933 bool MadeChange = UpdateNodeType(getChild(0)->getExtType(), TP); 1934 MadeChange |= getChild(0)->UpdateNodeType(getExtType(), TP); 1935 return MadeChange; 1936 } 1937 #endif 1938 return MadeChange; 1939 } 1940 1941 /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the 1942 /// RHS of a commutative operation, not the on LHS. 1943 static bool OnlyOnRHSOfCommutative(TreePatternNode *N) { 1944 if (!N->isLeaf() && N->getOperator()->getName() == "imm") 1945 return true; 1946 if (N->isLeaf() && isa<IntInit>(N->getLeafValue())) 1947 return true; 1948 return false; 1949 } 1950 1951 1952 /// canPatternMatch - If it is impossible for this pattern to match on this 1953 /// target, fill in Reason and return false. Otherwise, return true. This is 1954 /// used as a sanity check for .td files (to prevent people from writing stuff 1955 /// that can never possibly work), and to prevent the pattern permuter from 1956 /// generating stuff that is useless. 1957 bool TreePatternNode::canPatternMatch(std::string &Reason, 1958 const CodeGenDAGPatterns &CDP) { 1959 if (isLeaf()) return true; 1960 1961 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1962 if (!getChild(i)->canPatternMatch(Reason, CDP)) 1963 return false; 1964 1965 // If this is an intrinsic, handle cases that would make it not match. For 1966 // example, if an operand is required to be an immediate. 1967 if (getOperator()->isSubClassOf("Intrinsic")) { 1968 // TODO: 1969 return true; 1970 } 1971 1972 if (getOperator()->isSubClassOf("ComplexPattern")) 1973 return true; 1974 1975 // If this node is a commutative operator, check that the LHS isn't an 1976 // immediate. 1977 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); 1978 bool isCommIntrinsic = isCommutativeIntrinsic(CDP); 1979 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { 1980 // Scan all of the operands of the node and make sure that only the last one 1981 // is a constant node, unless the RHS also is. 1982 if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { 1983 bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id. 1984 for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i) 1985 if (OnlyOnRHSOfCommutative(getChild(i))) { 1986 Reason="Immediate value must be on the RHS of commutative operators!"; 1987 return false; 1988 } 1989 } 1990 } 1991 1992 return true; 1993 } 1994 1995 //===----------------------------------------------------------------------===// 1996 // TreePattern implementation 1997 // 1998 1999 TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, 2000 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 2001 isInputPattern(isInput), HasError(false) { 2002 for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) 2003 Trees.push_back(ParseTreePattern(RawPat->getElement(i), "")); 2004 } 2005 2006 TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, 2007 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 2008 isInputPattern(isInput), HasError(false) { 2009 Trees.push_back(ParseTreePattern(Pat, "")); 2010 } 2011 2012 TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, 2013 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 2014 isInputPattern(isInput), HasError(false) { 2015 Trees.push_back(Pat); 2016 } 2017 2018 void TreePattern::error(const Twine &Msg) { 2019 if (HasError) 2020 return; 2021 dump(); 2022 PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg); 2023 HasError = true; 2024 } 2025 2026 void TreePattern::ComputeNamedNodes() { 2027 for (unsigned i = 0, e = Trees.size(); i != e; ++i) 2028 ComputeNamedNodes(Trees[i]); 2029 } 2030 2031 void TreePattern::ComputeNamedNodes(TreePatternNode *N) { 2032 if (!N->getName().empty()) 2033 NamedNodes[N->getName()].push_back(N); 2034 2035 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 2036 ComputeNamedNodes(N->getChild(i)); 2037 } 2038 2039 2040 TreePatternNode *TreePattern::ParseTreePattern(Init *TheInit, StringRef OpName){ 2041 if (DefInit *DI = dyn_cast<DefInit>(TheInit)) { 2042 Record *R = DI->getDef(); 2043 2044 // Direct reference to a leaf DagNode or PatFrag? Turn it into a 2045 // TreePatternNode of its own. For example: 2046 /// (foo GPR, imm) -> (foo GPR, (imm)) 2047 if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) 2048 return ParseTreePattern( 2049 DagInit::get(DI, "", 2050 std::vector<std::pair<Init*, std::string> >()), 2051 OpName); 2052 2053 // Input argument? 2054 TreePatternNode *Res = new TreePatternNode(DI, 1); 2055 if (R->getName() == "node" && !OpName.empty()) { 2056 if (OpName.empty()) 2057 error("'node' argument requires a name to match with operand list"); 2058 Args.push_back(OpName); 2059 } 2060 2061 Res->setName(OpName); 2062 return Res; 2063 } 2064 2065 // ?:$name or just $name. 2066 if (isa<UnsetInit>(TheInit)) { 2067 if (OpName.empty()) 2068 error("'?' argument requires a name to match with operand list"); 2069 TreePatternNode *Res = new TreePatternNode(TheInit, 1); 2070 Args.push_back(OpName); 2071 Res->setName(OpName); 2072 return Res; 2073 } 2074 2075 if (IntInit *II = dyn_cast<IntInit>(TheInit)) { 2076 if (!OpName.empty()) 2077 error("Constant int argument should not have a name!"); 2078 return new TreePatternNode(II, 1); 2079 } 2080 2081 if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) { 2082 // Turn this into an IntInit. 2083 Init *II = BI->convertInitializerTo(IntRecTy::get()); 2084 if (!II || !isa<IntInit>(II)) 2085 error("Bits value must be constants!"); 2086 return ParseTreePattern(II, OpName); 2087 } 2088 2089 DagInit *Dag = dyn_cast<DagInit>(TheInit); 2090 if (!Dag) { 2091 TheInit->dump(); 2092 error("Pattern has unexpected init kind!"); 2093 } 2094 DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator()); 2095 if (!OpDef) error("Pattern has unexpected operator type!"); 2096 Record *Operator = OpDef->getDef(); 2097 2098 if (Operator->isSubClassOf("ValueType")) { 2099 // If the operator is a ValueType, then this must be "type cast" of a leaf 2100 // node. 2101 if (Dag->getNumArgs() != 1) 2102 error("Type cast only takes one operand!"); 2103 2104 TreePatternNode *New = ParseTreePattern(Dag->getArg(0), Dag->getArgName(0)); 2105 2106 // Apply the type cast. 2107 assert(New->getNumTypes() == 1 && "FIXME: Unhandled"); 2108 New->UpdateNodeType(0, getValueType(Operator), *this); 2109 2110 if (!OpName.empty()) 2111 error("ValueType cast should not have a name!"); 2112 return New; 2113 } 2114 2115 // Verify that this is something that makes sense for an operator. 2116 if (!Operator->isSubClassOf("PatFrag") && 2117 !Operator->isSubClassOf("SDNode") && 2118 !Operator->isSubClassOf("Instruction") && 2119 !Operator->isSubClassOf("SDNodeXForm") && 2120 !Operator->isSubClassOf("Intrinsic") && 2121 !Operator->isSubClassOf("ComplexPattern") && 2122 Operator->getName() != "set" && 2123 Operator->getName() != "implicit") 2124 error("Unrecognized node '" + Operator->getName() + "'!"); 2125 2126 // Check to see if this is something that is illegal in an input pattern. 2127 if (isInputPattern) { 2128 if (Operator->isSubClassOf("Instruction") || 2129 Operator->isSubClassOf("SDNodeXForm")) 2130 error("Cannot use '" + Operator->getName() + "' in an input pattern!"); 2131 } else { 2132 if (Operator->isSubClassOf("Intrinsic")) 2133 error("Cannot use '" + Operator->getName() + "' in an output pattern!"); 2134 2135 if (Operator->isSubClassOf("SDNode") && 2136 Operator->getName() != "imm" && 2137 Operator->getName() != "fpimm" && 2138 Operator->getName() != "tglobaltlsaddr" && 2139 Operator->getName() != "tconstpool" && 2140 Operator->getName() != "tjumptable" && 2141 Operator->getName() != "tframeindex" && 2142 Operator->getName() != "texternalsym" && 2143 Operator->getName() != "tblockaddress" && 2144 Operator->getName() != "tglobaladdr" && 2145 Operator->getName() != "bb" && 2146 Operator->getName() != "vt") 2147 error("Cannot use '" + Operator->getName() + "' in an output pattern!"); 2148 } 2149 2150 std::vector<TreePatternNode*> Children; 2151 2152 // Parse all the operands. 2153 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) 2154 Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgName(i))); 2155 2156 // If the operator is an intrinsic, then this is just syntactic sugar for for 2157 // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and 2158 // convert the intrinsic name to a number. 2159 if (Operator->isSubClassOf("Intrinsic")) { 2160 const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator); 2161 unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1; 2162 2163 // If this intrinsic returns void, it must have side-effects and thus a 2164 // chain. 2165 if (Int.IS.RetVTs.empty()) 2166 Operator = getDAGPatterns().get_intrinsic_void_sdnode(); 2167 else if (Int.ModRef != CodeGenIntrinsic::NoMem) 2168 // Has side-effects, requires chain. 2169 Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); 2170 else // Otherwise, no chain. 2171 Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); 2172 2173 TreePatternNode *IIDNode = new TreePatternNode(IntInit::get(IID), 1); 2174 Children.insert(Children.begin(), IIDNode); 2175 } 2176 2177 if (Operator->isSubClassOf("ComplexPattern")) { 2178 for (unsigned i = 0; i < Children.size(); ++i) { 2179 TreePatternNode *Child = Children[i]; 2180 2181 if (Child->getName().empty()) 2182 error("All arguments to a ComplexPattern must be named"); 2183 2184 // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)" 2185 // and "(MY_PAT $b, $a)" should not be allowed in the same pattern; 2186 // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)". 2187 auto OperandId = std::make_pair(Operator, i); 2188 auto PrevOp = ComplexPatternOperands.find(Child->getName()); 2189 if (PrevOp != ComplexPatternOperands.end()) { 2190 if (PrevOp->getValue() != OperandId) 2191 error("All ComplexPattern operands must appear consistently: " 2192 "in the same order in just one ComplexPattern instance."); 2193 } else 2194 ComplexPatternOperands[Child->getName()] = OperandId; 2195 } 2196 } 2197 2198 unsigned NumResults = GetNumNodeResults(Operator, CDP); 2199 TreePatternNode *Result = new TreePatternNode(Operator, Children, NumResults); 2200 Result->setName(OpName); 2201 2202 if (!Dag->getName().empty()) { 2203 assert(Result->getName().empty()); 2204 Result->setName(Dag->getName()); 2205 } 2206 return Result; 2207 } 2208 2209 /// SimplifyTree - See if we can simplify this tree to eliminate something that 2210 /// will never match in favor of something obvious that will. This is here 2211 /// strictly as a convenience to target authors because it allows them to write 2212 /// more type generic things and have useless type casts fold away. 2213 /// 2214 /// This returns true if any change is made. 2215 static bool SimplifyTree(TreePatternNode *&N) { 2216 if (N->isLeaf()) 2217 return false; 2218 2219 // If we have a bitconvert with a resolved type and if the source and 2220 // destination types are the same, then the bitconvert is useless, remove it. 2221 if (N->getOperator()->getName() == "bitconvert" && 2222 N->getExtType(0).isConcrete() && 2223 N->getExtType(0) == N->getChild(0)->getExtType(0) && 2224 N->getName().empty()) { 2225 N = N->getChild(0); 2226 SimplifyTree(N); 2227 return true; 2228 } 2229 2230 // Walk all children. 2231 bool MadeChange = false; 2232 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { 2233 TreePatternNode *Child = N->getChild(i); 2234 MadeChange |= SimplifyTree(Child); 2235 N->setChild(i, Child); 2236 } 2237 return MadeChange; 2238 } 2239 2240 2241 2242 /// InferAllTypes - Infer/propagate as many types throughout the expression 2243 /// patterns as possible. Return true if all types are inferred, false 2244 /// otherwise. Flags an error if a type contradiction is found. 2245 bool TreePattern:: 2246 InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) { 2247 if (NamedNodes.empty()) 2248 ComputeNamedNodes(); 2249 2250 bool MadeChange = true; 2251 while (MadeChange) { 2252 MadeChange = false; 2253 for (unsigned i = 0, e = Trees.size(); i != e; ++i) { 2254 MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); 2255 MadeChange |= SimplifyTree(Trees[i]); 2256 } 2257 2258 // If there are constraints on our named nodes, apply them. 2259 for (StringMap<SmallVector<TreePatternNode*,1> >::iterator 2260 I = NamedNodes.begin(), E = NamedNodes.end(); I != E; ++I) { 2261 SmallVectorImpl<TreePatternNode*> &Nodes = I->second; 2262 2263 // If we have input named node types, propagate their types to the named 2264 // values here. 2265 if (InNamedTypes) { 2266 if (!InNamedTypes->count(I->getKey())) { 2267 error("Node '" + std::string(I->getKey()) + 2268 "' in output pattern but not input pattern"); 2269 return true; 2270 } 2271 2272 const SmallVectorImpl<TreePatternNode*> &InNodes = 2273 InNamedTypes->find(I->getKey())->second; 2274 2275 // The input types should be fully resolved by now. 2276 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 2277 // If this node is a register class, and it is the root of the pattern 2278 // then we're mapping something onto an input register. We allow 2279 // changing the type of the input register in this case. This allows 2280 // us to match things like: 2281 // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>; 2282 if (Nodes[i] == Trees[0] && Nodes[i]->isLeaf()) { 2283 DefInit *DI = dyn_cast<DefInit>(Nodes[i]->getLeafValue()); 2284 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || 2285 DI->getDef()->isSubClassOf("RegisterOperand"))) 2286 continue; 2287 } 2288 2289 assert(Nodes[i]->getNumTypes() == 1 && 2290 InNodes[0]->getNumTypes() == 1 && 2291 "FIXME: cannot name multiple result nodes yet"); 2292 MadeChange |= Nodes[i]->UpdateNodeType(0, InNodes[0]->getExtType(0), 2293 *this); 2294 } 2295 } 2296 2297 // If there are multiple nodes with the same name, they must all have the 2298 // same type. 2299 if (I->second.size() > 1) { 2300 for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) { 2301 TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1]; 2302 assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 && 2303 "FIXME: cannot name multiple result nodes yet"); 2304 2305 MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this); 2306 MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this); 2307 } 2308 } 2309 } 2310 } 2311 2312 bool HasUnresolvedTypes = false; 2313 for (unsigned i = 0, e = Trees.size(); i != e; ++i) 2314 HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); 2315 return !HasUnresolvedTypes; 2316 } 2317 2318 void TreePattern::print(raw_ostream &OS) const { 2319 OS << getRecord()->getName(); 2320 if (!Args.empty()) { 2321 OS << "(" << Args[0]; 2322 for (unsigned i = 1, e = Args.size(); i != e; ++i) 2323 OS << ", " << Args[i]; 2324 OS << ")"; 2325 } 2326 OS << ": "; 2327 2328 if (Trees.size() > 1) 2329 OS << "[\n"; 2330 for (unsigned i = 0, e = Trees.size(); i != e; ++i) { 2331 OS << "\t"; 2332 Trees[i]->print(OS); 2333 OS << "\n"; 2334 } 2335 2336 if (Trees.size() > 1) 2337 OS << "]\n"; 2338 } 2339 2340 void TreePattern::dump() const { print(errs()); } 2341 2342 //===----------------------------------------------------------------------===// 2343 // CodeGenDAGPatterns implementation 2344 // 2345 2346 CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : 2347 Records(R), Target(R) { 2348 2349 Intrinsics = LoadIntrinsics(Records, false); 2350 TgtIntrinsics = LoadIntrinsics(Records, true); 2351 ParseNodeInfo(); 2352 ParseNodeTransforms(); 2353 ParseComplexPatterns(); 2354 ParsePatternFragments(); 2355 ParseDefaultOperands(); 2356 ParseInstructions(); 2357 ParsePatternFragments(/*OutFrags*/true); 2358 ParsePatterns(); 2359 2360 // Generate variants. For example, commutative patterns can match 2361 // multiple ways. Add them to PatternsToMatch as well. 2362 GenerateVariants(); 2363 2364 // Infer instruction flags. For example, we can detect loads, 2365 // stores, and side effects in many cases by examining an 2366 // instruction's pattern. 2367 InferInstructionFlags(); 2368 2369 // Verify that instruction flags match the patterns. 2370 VerifyInstructionFlags(); 2371 } 2372 2373 Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const { 2374 Record *N = Records.getDef(Name); 2375 if (!N || !N->isSubClassOf("SDNode")) 2376 PrintFatalError("Error getting SDNode '" + Name + "'!"); 2377 2378 return N; 2379 } 2380 2381 // Parse all of the SDNode definitions for the target, populating SDNodes. 2382 void CodeGenDAGPatterns::ParseNodeInfo() { 2383 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); 2384 while (!Nodes.empty()) { 2385 SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); 2386 Nodes.pop_back(); 2387 } 2388 2389 // Get the builtin intrinsic nodes. 2390 intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); 2391 intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); 2392 intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); 2393 } 2394 2395 /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms 2396 /// map, and emit them to the file as functions. 2397 void CodeGenDAGPatterns::ParseNodeTransforms() { 2398 std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); 2399 while (!Xforms.empty()) { 2400 Record *XFormNode = Xforms.back(); 2401 Record *SDNode = XFormNode->getValueAsDef("Opcode"); 2402 std::string Code = XFormNode->getValueAsString("XFormFunction"); 2403 SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code))); 2404 2405 Xforms.pop_back(); 2406 } 2407 } 2408 2409 void CodeGenDAGPatterns::ParseComplexPatterns() { 2410 std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); 2411 while (!AMs.empty()) { 2412 ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); 2413 AMs.pop_back(); 2414 } 2415 } 2416 2417 2418 /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td 2419 /// file, building up the PatternFragments map. After we've collected them all, 2420 /// inline fragments together as necessary, so that there are no references left 2421 /// inside a pattern fragment to a pattern fragment. 2422 /// 2423 void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) { 2424 std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); 2425 2426 // First step, parse all of the fragments. 2427 for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { 2428 if (OutFrags != Fragments[i]->isSubClassOf("OutPatFrag")) 2429 continue; 2430 2431 DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); 2432 TreePattern *P = 2433 (PatternFragments[Fragments[i]] = llvm::make_unique<TreePattern>( 2434 Fragments[i], Tree, !Fragments[i]->isSubClassOf("OutPatFrag"), 2435 *this)).get(); 2436 2437 // Validate the argument list, converting it to set, to discard duplicates. 2438 std::vector<std::string> &Args = P->getArgList(); 2439 std::set<std::string> OperandsSet(Args.begin(), Args.end()); 2440 2441 if (OperandsSet.count("")) 2442 P->error("Cannot have unnamed 'node' values in pattern fragment!"); 2443 2444 // Parse the operands list. 2445 DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); 2446 DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator()); 2447 // Special cases: ops == outs == ins. Different names are used to 2448 // improve readability. 2449 if (!OpsOp || 2450 (OpsOp->getDef()->getName() != "ops" && 2451 OpsOp->getDef()->getName() != "outs" && 2452 OpsOp->getDef()->getName() != "ins")) 2453 P->error("Operands list should start with '(ops ... '!"); 2454 2455 // Copy over the arguments. 2456 Args.clear(); 2457 for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { 2458 if (!isa<DefInit>(OpsList->getArg(j)) || 2459 cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node") 2460 P->error("Operands list should all be 'node' values."); 2461 if (OpsList->getArgName(j).empty()) 2462 P->error("Operands list should have names for each operand!"); 2463 if (!OperandsSet.count(OpsList->getArgName(j))) 2464 P->error("'" + OpsList->getArgName(j) + 2465 "' does not occur in pattern or was multiply specified!"); 2466 OperandsSet.erase(OpsList->getArgName(j)); 2467 Args.push_back(OpsList->getArgName(j)); 2468 } 2469 2470 if (!OperandsSet.empty()) 2471 P->error("Operands list does not contain an entry for operand '" + 2472 *OperandsSet.begin() + "'!"); 2473 2474 // If there is a code init for this fragment, keep track of the fact that 2475 // this fragment uses it. 2476 TreePredicateFn PredFn(P); 2477 if (!PredFn.isAlwaysTrue()) 2478 P->getOnlyTree()->addPredicateFn(PredFn); 2479 2480 // If there is a node transformation corresponding to this, keep track of 2481 // it. 2482 Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); 2483 if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? 2484 P->getOnlyTree()->setTransformFn(Transform); 2485 } 2486 2487 // Now that we've parsed all of the tree fragments, do a closure on them so 2488 // that there are not references to PatFrags left inside of them. 2489 for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { 2490 if (OutFrags != Fragments[i]->isSubClassOf("OutPatFrag")) 2491 continue; 2492 2493 TreePattern &ThePat = *PatternFragments[Fragments[i]]; 2494 ThePat.InlinePatternFragments(); 2495 2496 // Infer as many types as possible. Don't worry about it if we don't infer 2497 // all of them, some may depend on the inputs of the pattern. 2498 ThePat.InferAllTypes(); 2499 ThePat.resetError(); 2500 2501 // If debugging, print out the pattern fragment result. 2502 DEBUG(ThePat.dump()); 2503 } 2504 } 2505 2506 void CodeGenDAGPatterns::ParseDefaultOperands() { 2507 std::vector<Record*> DefaultOps; 2508 DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps"); 2509 2510 // Find some SDNode. 2511 assert(!SDNodes.empty() && "No SDNodes parsed?"); 2512 Init *SomeSDNode = DefInit::get(SDNodes.begin()->first); 2513 2514 for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) { 2515 DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps"); 2516 2517 // Clone the DefaultInfo dag node, changing the operator from 'ops' to 2518 // SomeSDnode so that we can parse this. 2519 std::vector<std::pair<Init*, std::string> > Ops; 2520 for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) 2521 Ops.push_back(std::make_pair(DefaultInfo->getArg(op), 2522 DefaultInfo->getArgName(op))); 2523 DagInit *DI = DagInit::get(SomeSDNode, "", Ops); 2524 2525 // Create a TreePattern to parse this. 2526 TreePattern P(DefaultOps[i], DI, false, *this); 2527 assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!"); 2528 2529 // Copy the operands over into a DAGDefaultOperand. 2530 DAGDefaultOperand DefaultOpInfo; 2531 2532 TreePatternNode *T = P.getTree(0); 2533 for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) { 2534 TreePatternNode *TPN = T->getChild(op); 2535 while (TPN->ApplyTypeConstraints(P, false)) 2536 /* Resolve all types */; 2537 2538 if (TPN->ContainsUnresolvedType()) { 2539 PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" + 2540 DefaultOps[i]->getName() + 2541 "' doesn't have a concrete type!"); 2542 } 2543 DefaultOpInfo.DefaultOps.push_back(TPN); 2544 } 2545 2546 // Insert it into the DefaultOperands map so we can find it later. 2547 DefaultOperands[DefaultOps[i]] = DefaultOpInfo; 2548 } 2549 } 2550 2551 /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an 2552 /// instruction input. Return true if this is a real use. 2553 static bool HandleUse(TreePattern *I, TreePatternNode *Pat, 2554 std::map<std::string, TreePatternNode*> &InstInputs) { 2555 // No name -> not interesting. 2556 if (Pat->getName().empty()) { 2557 if (Pat->isLeaf()) { 2558 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); 2559 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || 2560 DI->getDef()->isSubClassOf("RegisterOperand"))) 2561 I->error("Input " + DI->getDef()->getName() + " must be named!"); 2562 } 2563 return false; 2564 } 2565 2566 Record *Rec; 2567 if (Pat->isLeaf()) { 2568 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); 2569 if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); 2570 Rec = DI->getDef(); 2571 } else { 2572 Rec = Pat->getOperator(); 2573 } 2574 2575 // SRCVALUE nodes are ignored. 2576 if (Rec->getName() == "srcvalue") 2577 return false; 2578 2579 TreePatternNode *&Slot = InstInputs[Pat->getName()]; 2580 if (!Slot) { 2581 Slot = Pat; 2582 return true; 2583 } 2584 Record *SlotRec; 2585 if (Slot->isLeaf()) { 2586 SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef(); 2587 } else { 2588 assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); 2589 SlotRec = Slot->getOperator(); 2590 } 2591 2592 // Ensure that the inputs agree if we've already seen this input. 2593 if (Rec != SlotRec) 2594 I->error("All $" + Pat->getName() + " inputs must agree with each other"); 2595 if (Slot->getExtTypes() != Pat->getExtTypes()) 2596 I->error("All $" + Pat->getName() + " inputs must agree with each other"); 2597 return true; 2598 } 2599 2600 /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is 2601 /// part of "I", the instruction), computing the set of inputs and outputs of 2602 /// the pattern. Report errors if we see anything naughty. 2603 void CodeGenDAGPatterns:: 2604 FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, 2605 std::map<std::string, TreePatternNode*> &InstInputs, 2606 std::map<std::string, TreePatternNode*>&InstResults, 2607 std::vector<Record*> &InstImpResults) { 2608 if (Pat->isLeaf()) { 2609 bool isUse = HandleUse(I, Pat, InstInputs); 2610 if (!isUse && Pat->getTransformFn()) 2611 I->error("Cannot specify a transform function for a non-input value!"); 2612 return; 2613 } 2614 2615 if (Pat->getOperator()->getName() == "implicit") { 2616 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { 2617 TreePatternNode *Dest = Pat->getChild(i); 2618 if (!Dest->isLeaf()) 2619 I->error("implicitly defined value should be a register!"); 2620 2621 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); 2622 if (!Val || !Val->getDef()->isSubClassOf("Register")) 2623 I->error("implicitly defined value should be a register!"); 2624 InstImpResults.push_back(Val->getDef()); 2625 } 2626 return; 2627 } 2628 2629 if (Pat->getOperator()->getName() != "set") { 2630 // If this is not a set, verify that the children nodes are not void typed, 2631 // and recurse. 2632 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { 2633 if (Pat->getChild(i)->getNumTypes() == 0) 2634 I->error("Cannot have void nodes inside of patterns!"); 2635 FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, 2636 InstImpResults); 2637 } 2638 2639 // If this is a non-leaf node with no children, treat it basically as if 2640 // it were a leaf. This handles nodes like (imm). 2641 bool isUse = HandleUse(I, Pat, InstInputs); 2642 2643 if (!isUse && Pat->getTransformFn()) 2644 I->error("Cannot specify a transform function for a non-input value!"); 2645 return; 2646 } 2647 2648 // Otherwise, this is a set, validate and collect instruction results. 2649 if (Pat->getNumChildren() == 0) 2650 I->error("set requires operands!"); 2651 2652 if (Pat->getTransformFn()) 2653 I->error("Cannot specify a transform function on a set node!"); 2654 2655 // Check the set destinations. 2656 unsigned NumDests = Pat->getNumChildren()-1; 2657 for (unsigned i = 0; i != NumDests; ++i) { 2658 TreePatternNode *Dest = Pat->getChild(i); 2659 if (!Dest->isLeaf()) 2660 I->error("set destination should be a register!"); 2661 2662 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); 2663 if (!Val) { 2664 I->error("set destination should be a register!"); 2665 continue; 2666 } 2667 2668 if (Val->getDef()->isSubClassOf("RegisterClass") || 2669 Val->getDef()->isSubClassOf("ValueType") || 2670 Val->getDef()->isSubClassOf("RegisterOperand") || 2671 Val->getDef()->isSubClassOf("PointerLikeRegClass")) { 2672 if (Dest->getName().empty()) 2673 I->error("set destination must have a name!"); 2674 if (InstResults.count(Dest->getName())) 2675 I->error("cannot set '" + Dest->getName() +"' multiple times"); 2676 InstResults[Dest->getName()] = Dest; 2677 } else if (Val->getDef()->isSubClassOf("Register")) { 2678 InstImpResults.push_back(Val->getDef()); 2679 } else { 2680 I->error("set destination should be a register!"); 2681 } 2682 } 2683 2684 // Verify and collect info from the computation. 2685 FindPatternInputsAndOutputs(I, Pat->getChild(NumDests), 2686 InstInputs, InstResults, InstImpResults); 2687 } 2688 2689 //===----------------------------------------------------------------------===// 2690 // Instruction Analysis 2691 //===----------------------------------------------------------------------===// 2692 2693 class InstAnalyzer { 2694 const CodeGenDAGPatterns &CDP; 2695 public: 2696 bool hasSideEffects; 2697 bool mayStore; 2698 bool mayLoad; 2699 bool isBitcast; 2700 bool isVariadic; 2701 2702 InstAnalyzer(const CodeGenDAGPatterns &cdp) 2703 : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false), 2704 isBitcast(false), isVariadic(false) {} 2705 2706 void Analyze(const TreePattern *Pat) { 2707 // Assume only the first tree is the pattern. The others are clobber nodes. 2708 AnalyzeNode(Pat->getTree(0)); 2709 } 2710 2711 void Analyze(const PatternToMatch *Pat) { 2712 AnalyzeNode(Pat->getSrcPattern()); 2713 } 2714 2715 private: 2716 bool IsNodeBitcast(const TreePatternNode *N) const { 2717 if (hasSideEffects || mayLoad || mayStore || isVariadic) 2718 return false; 2719 2720 if (N->getNumChildren() != 2) 2721 return false; 2722 2723 const TreePatternNode *N0 = N->getChild(0); 2724 if (!N0->isLeaf() || !isa<DefInit>(N0->getLeafValue())) 2725 return false; 2726 2727 const TreePatternNode *N1 = N->getChild(1); 2728 if (N1->isLeaf()) 2729 return false; 2730 if (N1->getNumChildren() != 1 || !N1->getChild(0)->isLeaf()) 2731 return false; 2732 2733 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N1->getOperator()); 2734 if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1) 2735 return false; 2736 return OpInfo.getEnumName() == "ISD::BITCAST"; 2737 } 2738 2739 public: 2740 void AnalyzeNode(const TreePatternNode *N) { 2741 if (N->isLeaf()) { 2742 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) { 2743 Record *LeafRec = DI->getDef(); 2744 // Handle ComplexPattern leaves. 2745 if (LeafRec->isSubClassOf("ComplexPattern")) { 2746 const ComplexPattern &CP = CDP.getComplexPattern(LeafRec); 2747 if (CP.hasProperty(SDNPMayStore)) mayStore = true; 2748 if (CP.hasProperty(SDNPMayLoad)) mayLoad = true; 2749 if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true; 2750 } 2751 } 2752 return; 2753 } 2754 2755 // Analyze children. 2756 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 2757 AnalyzeNode(N->getChild(i)); 2758 2759 // Ignore set nodes, which are not SDNodes. 2760 if (N->getOperator()->getName() == "set") { 2761 isBitcast = IsNodeBitcast(N); 2762 return; 2763 } 2764 2765 // Notice properties of the node. 2766 if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true; 2767 if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true; 2768 if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true; 2769 if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true; 2770 2771 if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) { 2772 // If this is an intrinsic, analyze it. 2773 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadArgMem) 2774 mayLoad = true;// These may load memory. 2775 2776 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteArgMem) 2777 mayStore = true;// Intrinsics that can write to memory are 'mayStore'. 2778 2779 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem) 2780 // WriteMem intrinsics can have other strange effects. 2781 hasSideEffects = true; 2782 } 2783 } 2784 2785 }; 2786 2787 static bool InferFromPattern(CodeGenInstruction &InstInfo, 2788 const InstAnalyzer &PatInfo, 2789 Record *PatDef) { 2790 bool Error = false; 2791 2792 // Remember where InstInfo got its flags. 2793 if (InstInfo.hasUndefFlags()) 2794 InstInfo.InferredFrom = PatDef; 2795 2796 // Check explicitly set flags for consistency. 2797 if (InstInfo.hasSideEffects != PatInfo.hasSideEffects && 2798 !InstInfo.hasSideEffects_Unset) { 2799 // Allow explicitly setting hasSideEffects = 1 on instructions, even when 2800 // the pattern has no side effects. That could be useful for div/rem 2801 // instructions that may trap. 2802 if (!InstInfo.hasSideEffects) { 2803 Error = true; 2804 PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " + 2805 Twine(InstInfo.hasSideEffects)); 2806 } 2807 } 2808 2809 if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) { 2810 Error = true; 2811 PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " + 2812 Twine(InstInfo.mayStore)); 2813 } 2814 2815 if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) { 2816 // Allow explicitly setting mayLoad = 1, even when the pattern has no loads. 2817 // Some targets translate imediates to loads. 2818 if (!InstInfo.mayLoad) { 2819 Error = true; 2820 PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " + 2821 Twine(InstInfo.mayLoad)); 2822 } 2823 } 2824 2825 // Transfer inferred flags. 2826 InstInfo.hasSideEffects |= PatInfo.hasSideEffects; 2827 InstInfo.mayStore |= PatInfo.mayStore; 2828 InstInfo.mayLoad |= PatInfo.mayLoad; 2829 2830 // These flags are silently added without any verification. 2831 InstInfo.isBitcast |= PatInfo.isBitcast; 2832 2833 // Don't infer isVariadic. This flag means something different on SDNodes and 2834 // instructions. For example, a CALL SDNode is variadic because it has the 2835 // call arguments as operands, but a CALL instruction is not variadic - it 2836 // has argument registers as implicit, not explicit uses. 2837 2838 return Error; 2839 } 2840 2841 /// hasNullFragReference - Return true if the DAG has any reference to the 2842 /// null_frag operator. 2843 static bool hasNullFragReference(DagInit *DI) { 2844 DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator()); 2845 if (!OpDef) return false; 2846 Record *Operator = OpDef->getDef(); 2847 2848 // If this is the null fragment, return true. 2849 if (Operator->getName() == "null_frag") return true; 2850 // If any of the arguments reference the null fragment, return true. 2851 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) { 2852 DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i)); 2853 if (Arg && hasNullFragReference(Arg)) 2854 return true; 2855 } 2856 2857 return false; 2858 } 2859 2860 /// hasNullFragReference - Return true if any DAG in the list references 2861 /// the null_frag operator. 2862 static bool hasNullFragReference(ListInit *LI) { 2863 for (unsigned i = 0, e = LI->getSize(); i != e; ++i) { 2864 DagInit *DI = dyn_cast<DagInit>(LI->getElement(i)); 2865 assert(DI && "non-dag in an instruction Pattern list?!"); 2866 if (hasNullFragReference(DI)) 2867 return true; 2868 } 2869 return false; 2870 } 2871 2872 /// Get all the instructions in a tree. 2873 static void 2874 getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) { 2875 if (Tree->isLeaf()) 2876 return; 2877 if (Tree->getOperator()->isSubClassOf("Instruction")) 2878 Instrs.push_back(Tree->getOperator()); 2879 for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i) 2880 getInstructionsInTree(Tree->getChild(i), Instrs); 2881 } 2882 2883 /// Check the class of a pattern leaf node against the instruction operand it 2884 /// represents. 2885 static bool checkOperandClass(CGIOperandList::OperandInfo &OI, 2886 Record *Leaf) { 2887 if (OI.Rec == Leaf) 2888 return true; 2889 2890 // Allow direct value types to be used in instruction set patterns. 2891 // The type will be checked later. 2892 if (Leaf->isSubClassOf("ValueType")) 2893 return true; 2894 2895 // Patterns can also be ComplexPattern instances. 2896 if (Leaf->isSubClassOf("ComplexPattern")) 2897 return true; 2898 2899 return false; 2900 } 2901 2902 const DAGInstruction &CodeGenDAGPatterns::parseInstructionPattern( 2903 CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) { 2904 2905 assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!"); 2906 2907 // Parse the instruction. 2908 TreePattern *I = new TreePattern(CGI.TheDef, Pat, true, *this); 2909 // Inline pattern fragments into it. 2910 I->InlinePatternFragments(); 2911 2912 // Infer as many types as possible. If we cannot infer all of them, we can 2913 // never do anything with this instruction pattern: report it to the user. 2914 if (!I->InferAllTypes()) 2915 I->error("Could not infer all types in pattern!"); 2916 2917 // InstInputs - Keep track of all of the inputs of the instruction, along 2918 // with the record they are declared as. 2919 std::map<std::string, TreePatternNode*> InstInputs; 2920 2921 // InstResults - Keep track of all the virtual registers that are 'set' 2922 // in the instruction, including what reg class they are. 2923 std::map<std::string, TreePatternNode*> InstResults; 2924 2925 std::vector<Record*> InstImpResults; 2926 2927 // Verify that the top-level forms in the instruction are of void type, and 2928 // fill in the InstResults map. 2929 for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { 2930 TreePatternNode *Pat = I->getTree(j); 2931 if (Pat->getNumTypes() != 0) 2932 I->error("Top-level forms in instruction pattern should have" 2933 " void types"); 2934 2935 // Find inputs and outputs, and verify the structure of the uses/defs. 2936 FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, 2937 InstImpResults); 2938 } 2939 2940 // Now that we have inputs and outputs of the pattern, inspect the operands 2941 // list for the instruction. This determines the order that operands are 2942 // added to the machine instruction the node corresponds to. 2943 unsigned NumResults = InstResults.size(); 2944 2945 // Parse the operands list from the (ops) list, validating it. 2946 assert(I->getArgList().empty() && "Args list should still be empty here!"); 2947 2948 // Check that all of the results occur first in the list. 2949 std::vector<Record*> Results; 2950 SmallVector<TreePatternNode *, 2> ResNodes; 2951 for (unsigned i = 0; i != NumResults; ++i) { 2952 if (i == CGI.Operands.size()) 2953 I->error("'" + InstResults.begin()->first + 2954 "' set but does not appear in operand list!"); 2955 const std::string &OpName = CGI.Operands[i].Name; 2956 2957 // Check that it exists in InstResults. 2958 TreePatternNode *RNode = InstResults[OpName]; 2959 if (!RNode) 2960 I->error("Operand $" + OpName + " does not exist in operand list!"); 2961 2962 ResNodes.push_back(RNode); 2963 2964 Record *R = cast<DefInit>(RNode->getLeafValue())->getDef(); 2965 if (!R) 2966 I->error("Operand $" + OpName + " should be a set destination: all " 2967 "outputs must occur before inputs in operand list!"); 2968 2969 if (!checkOperandClass(CGI.Operands[i], R)) 2970 I->error("Operand $" + OpName + " class mismatch!"); 2971 2972 // Remember the return type. 2973 Results.push_back(CGI.Operands[i].Rec); 2974 2975 // Okay, this one checks out. 2976 InstResults.erase(OpName); 2977 } 2978 2979 // Loop over the inputs next. Make a copy of InstInputs so we can destroy 2980 // the copy while we're checking the inputs. 2981 std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); 2982 2983 std::vector<TreePatternNode*> ResultNodeOperands; 2984 std::vector<Record*> Operands; 2985 for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) { 2986 CGIOperandList::OperandInfo &Op = CGI.Operands[i]; 2987 const std::string &OpName = Op.Name; 2988 if (OpName.empty()) 2989 I->error("Operand #" + utostr(i) + " in operands list has no name!"); 2990 2991 if (!InstInputsCheck.count(OpName)) { 2992 // If this is an operand with a DefaultOps set filled in, we can ignore 2993 // this. When we codegen it, we will do so as always executed. 2994 if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) { 2995 // Does it have a non-empty DefaultOps field? If so, ignore this 2996 // operand. 2997 if (!getDefaultOperand(Op.Rec).DefaultOps.empty()) 2998 continue; 2999 } 3000 I->error("Operand $" + OpName + 3001 " does not appear in the instruction pattern"); 3002 } 3003 TreePatternNode *InVal = InstInputsCheck[OpName]; 3004 InstInputsCheck.erase(OpName); // It occurred, remove from map. 3005 3006 if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) { 3007 Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); 3008 if (!checkOperandClass(Op, InRec)) 3009 I->error("Operand $" + OpName + "'s register class disagrees" 3010 " between the operand and pattern"); 3011 } 3012 Operands.push_back(Op.Rec); 3013 3014 // Construct the result for the dest-pattern operand list. 3015 TreePatternNode *OpNode = InVal->clone(); 3016 3017 // No predicate is useful on the result. 3018 OpNode->clearPredicateFns(); 3019 3020 // Promote the xform function to be an explicit node if set. 3021 if (Record *Xform = OpNode->getTransformFn()) { 3022 OpNode->setTransformFn(nullptr); 3023 std::vector<TreePatternNode*> Children; 3024 Children.push_back(OpNode); 3025 OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes()); 3026 } 3027 3028 ResultNodeOperands.push_back(OpNode); 3029 } 3030 3031 if (!InstInputsCheck.empty()) 3032 I->error("Input operand $" + InstInputsCheck.begin()->first + 3033 " occurs in pattern but not in operands list!"); 3034 3035 TreePatternNode *ResultPattern = 3036 new TreePatternNode(I->getRecord(), ResultNodeOperands, 3037 GetNumNodeResults(I->getRecord(), *this)); 3038 // Copy fully inferred output node types to instruction result pattern. 3039 for (unsigned i = 0; i != NumResults; ++i) { 3040 assert(ResNodes[i]->getNumTypes() == 1 && "FIXME: Unhandled"); 3041 ResultPattern->setType(i, ResNodes[i]->getExtType(0)); 3042 } 3043 3044 // Create and insert the instruction. 3045 // FIXME: InstImpResults should not be part of DAGInstruction. 3046 DAGInstruction TheInst(I, Results, Operands, InstImpResults); 3047 DAGInsts.insert(std::make_pair(I->getRecord(), TheInst)); 3048 3049 // Use a temporary tree pattern to infer all types and make sure that the 3050 // constructed result is correct. This depends on the instruction already 3051 // being inserted into the DAGInsts map. 3052 TreePattern Temp(I->getRecord(), ResultPattern, false, *this); 3053 Temp.InferAllTypes(&I->getNamedNodesMap()); 3054 3055 DAGInstruction &TheInsertedInst = DAGInsts.find(I->getRecord())->second; 3056 TheInsertedInst.setResultPattern(Temp.getOnlyTree()); 3057 3058 return TheInsertedInst; 3059 } 3060 3061 /// ParseInstructions - Parse all of the instructions, inlining and resolving 3062 /// any fragments involved. This populates the Instructions list with fully 3063 /// resolved instructions. 3064 void CodeGenDAGPatterns::ParseInstructions() { 3065 std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); 3066 3067 for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { 3068 ListInit *LI = nullptr; 3069 3070 if (isa<ListInit>(Instrs[i]->getValueInit("Pattern"))) 3071 LI = Instrs[i]->getValueAsListInit("Pattern"); 3072 3073 // If there is no pattern, only collect minimal information about the 3074 // instruction for its operand list. We have to assume that there is one 3075 // result, as we have no detailed info. A pattern which references the 3076 // null_frag operator is as-if no pattern were specified. Normally this 3077 // is from a multiclass expansion w/ a SDPatternOperator passed in as 3078 // null_frag. 3079 if (!LI || LI->empty() || hasNullFragReference(LI)) { 3080 std::vector<Record*> Results; 3081 std::vector<Record*> Operands; 3082 3083 CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); 3084 3085 if (InstInfo.Operands.size() != 0) { 3086 for (unsigned j = 0, e = InstInfo.Operands.NumDefs; j < e; ++j) 3087 Results.push_back(InstInfo.Operands[j].Rec); 3088 3089 // The rest are inputs. 3090 for (unsigned j = InstInfo.Operands.NumDefs, 3091 e = InstInfo.Operands.size(); j < e; ++j) 3092 Operands.push_back(InstInfo.Operands[j].Rec); 3093 } 3094 3095 // Create and insert the instruction. 3096 std::vector<Record*> ImpResults; 3097 Instructions.insert(std::make_pair(Instrs[i], 3098 DAGInstruction(nullptr, Results, Operands, ImpResults))); 3099 continue; // no pattern. 3100 } 3101 3102 CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]); 3103 const DAGInstruction &DI = parseInstructionPattern(CGI, LI, Instructions); 3104 3105 (void)DI; 3106 DEBUG(DI.getPattern()->dump()); 3107 } 3108 3109 // If we can, convert the instructions to be patterns that are matched! 3110 for (std::map<Record*, DAGInstruction, LessRecordByID>::iterator II = 3111 Instructions.begin(), 3112 E = Instructions.end(); II != E; ++II) { 3113 DAGInstruction &TheInst = II->second; 3114 TreePattern *I = TheInst.getPattern(); 3115 if (!I) continue; // No pattern. 3116 3117 // FIXME: Assume only the first tree is the pattern. The others are clobber 3118 // nodes. 3119 TreePatternNode *Pattern = I->getTree(0); 3120 TreePatternNode *SrcPattern; 3121 if (Pattern->getOperator()->getName() == "set") { 3122 SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone(); 3123 } else{ 3124 // Not a set (store or something?) 3125 SrcPattern = Pattern; 3126 } 3127 3128 Record *Instr = II->first; 3129 AddPatternToMatch(I, 3130 PatternToMatch(Instr, 3131 Instr->getValueAsListInit("Predicates"), 3132 SrcPattern, 3133 TheInst.getResultPattern(), 3134 TheInst.getImpResults(), 3135 Instr->getValueAsInt("AddedComplexity"), 3136 Instr->getID())); 3137 } 3138 } 3139 3140 3141 typedef std::pair<const TreePatternNode*, unsigned> NameRecord; 3142 3143 static void FindNames(const TreePatternNode *P, 3144 std::map<std::string, NameRecord> &Names, 3145 TreePattern *PatternTop) { 3146 if (!P->getName().empty()) { 3147 NameRecord &Rec = Names[P->getName()]; 3148 // If this is the first instance of the name, remember the node. 3149 if (Rec.second++ == 0) 3150 Rec.first = P; 3151 else if (Rec.first->getExtTypes() != P->getExtTypes()) 3152 PatternTop->error("repetition of value: $" + P->getName() + 3153 " where different uses have different types!"); 3154 } 3155 3156 if (!P->isLeaf()) { 3157 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) 3158 FindNames(P->getChild(i), Names, PatternTop); 3159 } 3160 } 3161 3162 void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern, 3163 const PatternToMatch &PTM) { 3164 // Do some sanity checking on the pattern we're about to match. 3165 std::string Reason; 3166 if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) { 3167 PrintWarning(Pattern->getRecord()->getLoc(), 3168 Twine("Pattern can never match: ") + Reason); 3169 return; 3170 } 3171 3172 // If the source pattern's root is a complex pattern, that complex pattern 3173 // must specify the nodes it can potentially match. 3174 if (const ComplexPattern *CP = 3175 PTM.getSrcPattern()->getComplexPatternInfo(*this)) 3176 if (CP->getRootNodes().empty()) 3177 Pattern->error("ComplexPattern at root must specify list of opcodes it" 3178 " could match"); 3179 3180 3181 // Find all of the named values in the input and output, ensure they have the 3182 // same type. 3183 std::map<std::string, NameRecord> SrcNames, DstNames; 3184 FindNames(PTM.getSrcPattern(), SrcNames, Pattern); 3185 FindNames(PTM.getDstPattern(), DstNames, Pattern); 3186 3187 // Scan all of the named values in the destination pattern, rejecting them if 3188 // they don't exist in the input pattern. 3189 for (std::map<std::string, NameRecord>::iterator 3190 I = DstNames.begin(), E = DstNames.end(); I != E; ++I) { 3191 if (SrcNames[I->first].first == nullptr) 3192 Pattern->error("Pattern has input without matching name in output: $" + 3193 I->first); 3194 } 3195 3196 // Scan all of the named values in the source pattern, rejecting them if the 3197 // name isn't used in the dest, and isn't used to tie two values together. 3198 for (std::map<std::string, NameRecord>::iterator 3199 I = SrcNames.begin(), E = SrcNames.end(); I != E; ++I) 3200 if (DstNames[I->first].first == nullptr && SrcNames[I->first].second == 1) 3201 Pattern->error("Pattern has dead named input: $" + I->first); 3202 3203 PatternsToMatch.push_back(PTM); 3204 } 3205 3206 3207 3208 void CodeGenDAGPatterns::InferInstructionFlags() { 3209 const std::vector<const CodeGenInstruction*> &Instructions = 3210 Target.getInstructionsByEnumValue(); 3211 3212 // First try to infer flags from the primary instruction pattern, if any. 3213 SmallVector<CodeGenInstruction*, 8> Revisit; 3214 unsigned Errors = 0; 3215 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { 3216 CodeGenInstruction &InstInfo = 3217 const_cast<CodeGenInstruction &>(*Instructions[i]); 3218 3219 // Get the primary instruction pattern. 3220 const TreePattern *Pattern = getInstruction(InstInfo.TheDef).getPattern(); 3221 if (!Pattern) { 3222 if (InstInfo.hasUndefFlags()) 3223 Revisit.push_back(&InstInfo); 3224 continue; 3225 } 3226 InstAnalyzer PatInfo(*this); 3227 PatInfo.Analyze(Pattern); 3228 Errors += InferFromPattern(InstInfo, PatInfo, InstInfo.TheDef); 3229 } 3230 3231 // Second, look for single-instruction patterns defined outside the 3232 // instruction. 3233 for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) { 3234 const PatternToMatch &PTM = *I; 3235 3236 // We can only infer from single-instruction patterns, otherwise we won't 3237 // know which instruction should get the flags. 3238 SmallVector<Record*, 8> PatInstrs; 3239 getInstructionsInTree(PTM.getDstPattern(), PatInstrs); 3240 if (PatInstrs.size() != 1) 3241 continue; 3242 3243 // Get the single instruction. 3244 CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front()); 3245 3246 // Only infer properties from the first pattern. We'll verify the others. 3247 if (InstInfo.InferredFrom) 3248 continue; 3249 3250 InstAnalyzer PatInfo(*this); 3251 PatInfo.Analyze(&PTM); 3252 Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord()); 3253 } 3254 3255 if (Errors) 3256 PrintFatalError("pattern conflicts"); 3257 3258 // Revisit instructions with undefined flags and no pattern. 3259 if (Target.guessInstructionProperties()) { 3260 for (unsigned i = 0, e = Revisit.size(); i != e; ++i) { 3261 CodeGenInstruction &InstInfo = *Revisit[i]; 3262 if (InstInfo.InferredFrom) 3263 continue; 3264 // The mayLoad and mayStore flags default to false. 3265 // Conservatively assume hasSideEffects if it wasn't explicit. 3266 if (InstInfo.hasSideEffects_Unset) 3267 InstInfo.hasSideEffects = true; 3268 } 3269 return; 3270 } 3271 3272 // Complain about any flags that are still undefined. 3273 for (unsigned i = 0, e = Revisit.size(); i != e; ++i) { 3274 CodeGenInstruction &InstInfo = *Revisit[i]; 3275 if (InstInfo.InferredFrom) 3276 continue; 3277 if (InstInfo.hasSideEffects_Unset) 3278 PrintError(InstInfo.TheDef->getLoc(), 3279 "Can't infer hasSideEffects from patterns"); 3280 if (InstInfo.mayStore_Unset) 3281 PrintError(InstInfo.TheDef->getLoc(), 3282 "Can't infer mayStore from patterns"); 3283 if (InstInfo.mayLoad_Unset) 3284 PrintError(InstInfo.TheDef->getLoc(), 3285 "Can't infer mayLoad from patterns"); 3286 } 3287 } 3288 3289 3290 /// Verify instruction flags against pattern node properties. 3291 void CodeGenDAGPatterns::VerifyInstructionFlags() { 3292 unsigned Errors = 0; 3293 for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) { 3294 const PatternToMatch &PTM = *I; 3295 SmallVector<Record*, 8> Instrs; 3296 getInstructionsInTree(PTM.getDstPattern(), Instrs); 3297 if (Instrs.empty()) 3298 continue; 3299 3300 // Count the number of instructions with each flag set. 3301 unsigned NumSideEffects = 0; 3302 unsigned NumStores = 0; 3303 unsigned NumLoads = 0; 3304 for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { 3305 const CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); 3306 NumSideEffects += InstInfo.hasSideEffects; 3307 NumStores += InstInfo.mayStore; 3308 NumLoads += InstInfo.mayLoad; 3309 } 3310 3311 // Analyze the source pattern. 3312 InstAnalyzer PatInfo(*this); 3313 PatInfo.Analyze(&PTM); 3314 3315 // Collect error messages. 3316 SmallVector<std::string, 4> Msgs; 3317 3318 // Check for missing flags in the output. 3319 // Permit extra flags for now at least. 3320 if (PatInfo.hasSideEffects && !NumSideEffects) 3321 Msgs.push_back("pattern has side effects, but hasSideEffects isn't set"); 3322 3323 // Don't verify store flags on instructions with side effects. At least for 3324 // intrinsics, side effects implies mayStore. 3325 if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores) 3326 Msgs.push_back("pattern may store, but mayStore isn't set"); 3327 3328 // Similarly, mayStore implies mayLoad on intrinsics. 3329 if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads) 3330 Msgs.push_back("pattern may load, but mayLoad isn't set"); 3331 3332 // Print error messages. 3333 if (Msgs.empty()) 3334 continue; 3335 ++Errors; 3336 3337 for (unsigned i = 0, e = Msgs.size(); i != e; ++i) 3338 PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msgs[i]) + " on the " + 3339 (Instrs.size() == 1 ? 3340 "instruction" : "output instructions")); 3341 // Provide the location of the relevant instruction definitions. 3342 for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { 3343 if (Instrs[i] != PTM.getSrcRecord()) 3344 PrintError(Instrs[i]->getLoc(), "defined here"); 3345 const CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); 3346 if (InstInfo.InferredFrom && 3347 InstInfo.InferredFrom != InstInfo.TheDef && 3348 InstInfo.InferredFrom != PTM.getSrcRecord()) 3349 PrintError(InstInfo.InferredFrom->getLoc(), "inferred from patttern"); 3350 } 3351 } 3352 if (Errors) 3353 PrintFatalError("Errors in DAG patterns"); 3354 } 3355 3356 /// Given a pattern result with an unresolved type, see if we can find one 3357 /// instruction with an unresolved result type. Force this result type to an 3358 /// arbitrary element if it's possible types to converge results. 3359 static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) { 3360 if (N->isLeaf()) 3361 return false; 3362 3363 // Analyze children. 3364 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 3365 if (ForceArbitraryInstResultType(N->getChild(i), TP)) 3366 return true; 3367 3368 if (!N->getOperator()->isSubClassOf("Instruction")) 3369 return false; 3370 3371 // If this type is already concrete or completely unknown we can't do 3372 // anything. 3373 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) { 3374 if (N->getExtType(i).isCompletelyUnknown() || N->getExtType(i).isConcrete()) 3375 continue; 3376 3377 // Otherwise, force its type to the first possibility (an arbitrary choice). 3378 if (N->getExtType(i).MergeInTypeInfo(N->getExtType(i).getTypeList()[0], TP)) 3379 return true; 3380 } 3381 3382 return false; 3383 } 3384 3385 void CodeGenDAGPatterns::ParsePatterns() { 3386 std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); 3387 3388 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { 3389 Record *CurPattern = Patterns[i]; 3390 DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch"); 3391 3392 // If the pattern references the null_frag, there's nothing to do. 3393 if (hasNullFragReference(Tree)) 3394 continue; 3395 3396 TreePattern *Pattern = new TreePattern(CurPattern, Tree, true, *this); 3397 3398 // Inline pattern fragments into it. 3399 Pattern->InlinePatternFragments(); 3400 3401 ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs"); 3402 if (LI->empty()) continue; // no pattern. 3403 3404 // Parse the instruction. 3405 TreePattern Result(CurPattern, LI, false, *this); 3406 3407 // Inline pattern fragments into it. 3408 Result.InlinePatternFragments(); 3409 3410 if (Result.getNumTrees() != 1) 3411 Result.error("Cannot handle instructions producing instructions " 3412 "with temporaries yet!"); 3413 3414 bool IterateInference; 3415 bool InferredAllPatternTypes, InferredAllResultTypes; 3416 do { 3417 // Infer as many types as possible. If we cannot infer all of them, we 3418 // can never do anything with this pattern: report it to the user. 3419 InferredAllPatternTypes = 3420 Pattern->InferAllTypes(&Pattern->getNamedNodesMap()); 3421 3422 // Infer as many types as possible. If we cannot infer all of them, we 3423 // can never do anything with this pattern: report it to the user. 3424 InferredAllResultTypes = 3425 Result.InferAllTypes(&Pattern->getNamedNodesMap()); 3426 3427 IterateInference = false; 3428 3429 // Apply the type of the result to the source pattern. This helps us 3430 // resolve cases where the input type is known to be a pointer type (which 3431 // is considered resolved), but the result knows it needs to be 32- or 3432 // 64-bits. Infer the other way for good measure. 3433 for (unsigned i = 0, e = std::min(Result.getTree(0)->getNumTypes(), 3434 Pattern->getTree(0)->getNumTypes()); 3435 i != e; ++i) { 3436 IterateInference = Pattern->getTree(0)->UpdateNodeType( 3437 i, Result.getTree(0)->getExtType(i), Result); 3438 IterateInference |= Result.getTree(0)->UpdateNodeType( 3439 i, Pattern->getTree(0)->getExtType(i), Result); 3440 } 3441 3442 // If our iteration has converged and the input pattern's types are fully 3443 // resolved but the result pattern is not fully resolved, we may have a 3444 // situation where we have two instructions in the result pattern and 3445 // the instructions require a common register class, but don't care about 3446 // what actual MVT is used. This is actually a bug in our modelling: 3447 // output patterns should have register classes, not MVTs. 3448 // 3449 // In any case, to handle this, we just go through and disambiguate some 3450 // arbitrary types to the result pattern's nodes. 3451 if (!IterateInference && InferredAllPatternTypes && 3452 !InferredAllResultTypes) 3453 IterateInference = 3454 ForceArbitraryInstResultType(Result.getTree(0), Result); 3455 } while (IterateInference); 3456 3457 // Verify that we inferred enough types that we can do something with the 3458 // pattern and result. If these fire the user has to add type casts. 3459 if (!InferredAllPatternTypes) 3460 Pattern->error("Could not infer all types in pattern!"); 3461 if (!InferredAllResultTypes) { 3462 Pattern->dump(); 3463 Result.error("Could not infer all types in pattern result!"); 3464 } 3465 3466 // Validate that the input pattern is correct. 3467 std::map<std::string, TreePatternNode*> InstInputs; 3468 std::map<std::string, TreePatternNode*> InstResults; 3469 std::vector<Record*> InstImpResults; 3470 for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j) 3471 FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j), 3472 InstInputs, InstResults, 3473 InstImpResults); 3474 3475 // Promote the xform function to be an explicit node if set. 3476 TreePatternNode *DstPattern = Result.getOnlyTree(); 3477 std::vector<TreePatternNode*> ResultNodeOperands; 3478 for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) { 3479 TreePatternNode *OpNode = DstPattern->getChild(ii); 3480 if (Record *Xform = OpNode->getTransformFn()) { 3481 OpNode->setTransformFn(nullptr); 3482 std::vector<TreePatternNode*> Children; 3483 Children.push_back(OpNode); 3484 OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes()); 3485 } 3486 ResultNodeOperands.push_back(OpNode); 3487 } 3488 DstPattern = Result.getOnlyTree(); 3489 if (!DstPattern->isLeaf()) 3490 DstPattern = new TreePatternNode(DstPattern->getOperator(), 3491 ResultNodeOperands, 3492 DstPattern->getNumTypes()); 3493 3494 for (unsigned i = 0, e = Result.getOnlyTree()->getNumTypes(); i != e; ++i) 3495 DstPattern->setType(i, Result.getOnlyTree()->getExtType(i)); 3496 3497 TreePattern Temp(Result.getRecord(), DstPattern, false, *this); 3498 Temp.InferAllTypes(); 3499 3500 3501 AddPatternToMatch(Pattern, 3502 PatternToMatch(CurPattern, 3503 CurPattern->getValueAsListInit("Predicates"), 3504 Pattern->getTree(0), 3505 Temp.getOnlyTree(), InstImpResults, 3506 CurPattern->getValueAsInt("AddedComplexity"), 3507 CurPattern->getID())); 3508 } 3509 } 3510 3511 /// CombineChildVariants - Given a bunch of permutations of each child of the 3512 /// 'operator' node, put them together in all possible ways. 3513 static void CombineChildVariants(TreePatternNode *Orig, 3514 const std::vector<std::vector<TreePatternNode*> > &ChildVariants, 3515 std::vector<TreePatternNode*> &OutVariants, 3516 CodeGenDAGPatterns &CDP, 3517 const MultipleUseVarSet &DepVars) { 3518 // Make sure that each operand has at least one variant to choose from. 3519 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) 3520 if (ChildVariants[i].empty()) 3521 return; 3522 3523 // The end result is an all-pairs construction of the resultant pattern. 3524 std::vector<unsigned> Idxs; 3525 Idxs.resize(ChildVariants.size()); 3526 bool NotDone; 3527 do { 3528 #ifndef NDEBUG 3529 DEBUG(if (!Idxs.empty()) { 3530 errs() << Orig->getOperator()->getName() << ": Idxs = [ "; 3531 for (unsigned i = 0; i < Idxs.size(); ++i) { 3532 errs() << Idxs[i] << " "; 3533 } 3534 errs() << "]\n"; 3535 }); 3536 #endif 3537 // Create the variant and add it to the output list. 3538 std::vector<TreePatternNode*> NewChildren; 3539 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) 3540 NewChildren.push_back(ChildVariants[i][Idxs[i]]); 3541 TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren, 3542 Orig->getNumTypes()); 3543 3544 // Copy over properties. 3545 R->setName(Orig->getName()); 3546 R->setPredicateFns(Orig->getPredicateFns()); 3547 R->setTransformFn(Orig->getTransformFn()); 3548 for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i) 3549 R->setType(i, Orig->getExtType(i)); 3550 3551 // If this pattern cannot match, do not include it as a variant. 3552 std::string ErrString; 3553 if (!R->canPatternMatch(ErrString, CDP)) { 3554 delete R; 3555 } else { 3556 bool AlreadyExists = false; 3557 3558 // Scan to see if this pattern has already been emitted. We can get 3559 // duplication due to things like commuting: 3560 // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) 3561 // which are the same pattern. Ignore the dups. 3562 for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) 3563 if (R->isIsomorphicTo(OutVariants[i], DepVars)) { 3564 AlreadyExists = true; 3565 break; 3566 } 3567 3568 if (AlreadyExists) 3569 delete R; 3570 else 3571 OutVariants.push_back(R); 3572 } 3573 3574 // Increment indices to the next permutation by incrementing the 3575 // indicies from last index backward, e.g., generate the sequence 3576 // [0, 0], [0, 1], [1, 0], [1, 1]. 3577 int IdxsIdx; 3578 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) { 3579 if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size()) 3580 Idxs[IdxsIdx] = 0; 3581 else 3582 break; 3583 } 3584 NotDone = (IdxsIdx >= 0); 3585 } while (NotDone); 3586 } 3587 3588 /// CombineChildVariants - A helper function for binary operators. 3589 /// 3590 static void CombineChildVariants(TreePatternNode *Orig, 3591 const std::vector<TreePatternNode*> &LHS, 3592 const std::vector<TreePatternNode*> &RHS, 3593 std::vector<TreePatternNode*> &OutVariants, 3594 CodeGenDAGPatterns &CDP, 3595 const MultipleUseVarSet &DepVars) { 3596 std::vector<std::vector<TreePatternNode*> > ChildVariants; 3597 ChildVariants.push_back(LHS); 3598 ChildVariants.push_back(RHS); 3599 CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars); 3600 } 3601 3602 3603 static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, 3604 std::vector<TreePatternNode *> &Children) { 3605 assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); 3606 Record *Operator = N->getOperator(); 3607 3608 // Only permit raw nodes. 3609 if (!N->getName().empty() || !N->getPredicateFns().empty() || 3610 N->getTransformFn()) { 3611 Children.push_back(N); 3612 return; 3613 } 3614 3615 if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) 3616 Children.push_back(N->getChild(0)); 3617 else 3618 GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); 3619 3620 if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) 3621 Children.push_back(N->getChild(1)); 3622 else 3623 GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); 3624 } 3625 3626 /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of 3627 /// the (potentially recursive) pattern by using algebraic laws. 3628 /// 3629 static void GenerateVariantsOf(TreePatternNode *N, 3630 std::vector<TreePatternNode*> &OutVariants, 3631 CodeGenDAGPatterns &CDP, 3632 const MultipleUseVarSet &DepVars) { 3633 // We cannot permute leaves or ComplexPattern uses. 3634 if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) { 3635 OutVariants.push_back(N); 3636 return; 3637 } 3638 3639 // Look up interesting info about the node. 3640 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator()); 3641 3642 // If this node is associative, re-associate. 3643 if (NodeInfo.hasProperty(SDNPAssociative)) { 3644 // Re-associate by pulling together all of the linked operators 3645 std::vector<TreePatternNode*> MaximalChildren; 3646 GatherChildrenOfAssociativeOpcode(N, MaximalChildren); 3647 3648 // Only handle child sizes of 3. Otherwise we'll end up trying too many 3649 // permutations. 3650 if (MaximalChildren.size() == 3) { 3651 // Find the variants of all of our maximal children. 3652 std::vector<TreePatternNode*> AVariants, BVariants, CVariants; 3653 GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars); 3654 GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars); 3655 GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars); 3656 3657 // There are only two ways we can permute the tree: 3658 // (A op B) op C and A op (B op C) 3659 // Within these forms, we can also permute A/B/C. 3660 3661 // Generate legal pair permutations of A/B/C. 3662 std::vector<TreePatternNode*> ABVariants; 3663 std::vector<TreePatternNode*> BAVariants; 3664 std::vector<TreePatternNode*> ACVariants; 3665 std::vector<TreePatternNode*> CAVariants; 3666 std::vector<TreePatternNode*> BCVariants; 3667 std::vector<TreePatternNode*> CBVariants; 3668 CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars); 3669 CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars); 3670 CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars); 3671 CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars); 3672 CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars); 3673 CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars); 3674 3675 // Combine those into the result: (x op x) op x 3676 CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars); 3677 CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars); 3678 CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars); 3679 CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars); 3680 CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars); 3681 CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars); 3682 3683 // Combine those into the result: x op (x op x) 3684 CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars); 3685 CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars); 3686 CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars); 3687 CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars); 3688 CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars); 3689 CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars); 3690 return; 3691 } 3692 } 3693 3694 // Compute permutations of all children. 3695 std::vector<std::vector<TreePatternNode*> > ChildVariants; 3696 ChildVariants.resize(N->getNumChildren()); 3697 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 3698 GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars); 3699 3700 // Build all permutations based on how the children were formed. 3701 CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars); 3702 3703 // If this node is commutative, consider the commuted order. 3704 bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP); 3705 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { 3706 assert((N->getNumChildren()==2 || isCommIntrinsic) && 3707 "Commutative but doesn't have 2 children!"); 3708 // Don't count children which are actually register references. 3709 unsigned NC = 0; 3710 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { 3711 TreePatternNode *Child = N->getChild(i); 3712 if (Child->isLeaf()) 3713 if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) { 3714 Record *RR = DI->getDef(); 3715 if (RR->isSubClassOf("Register")) 3716 continue; 3717 } 3718 NC++; 3719 } 3720 // Consider the commuted order. 3721 if (isCommIntrinsic) { 3722 // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd 3723 // operands are the commutative operands, and there might be more operands 3724 // after those. 3725 assert(NC >= 3 && 3726 "Commutative intrinsic should have at least 3 childrean!"); 3727 std::vector<std::vector<TreePatternNode*> > Variants; 3728 Variants.push_back(ChildVariants[0]); // Intrinsic id. 3729 Variants.push_back(ChildVariants[2]); 3730 Variants.push_back(ChildVariants[1]); 3731 for (unsigned i = 3; i != NC; ++i) 3732 Variants.push_back(ChildVariants[i]); 3733 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars); 3734 } else if (NC == 2) 3735 CombineChildVariants(N, ChildVariants[1], ChildVariants[0], 3736 OutVariants, CDP, DepVars); 3737 } 3738 } 3739 3740 3741 // GenerateVariants - Generate variants. For example, commutative patterns can 3742 // match multiple ways. Add them to PatternsToMatch as well. 3743 void CodeGenDAGPatterns::GenerateVariants() { 3744 DEBUG(errs() << "Generating instruction variants.\n"); 3745 3746 // Loop over all of the patterns we've collected, checking to see if we can 3747 // generate variants of the instruction, through the exploitation of 3748 // identities. This permits the target to provide aggressive matching without 3749 // the .td file having to contain tons of variants of instructions. 3750 // 3751 // Note that this loop adds new patterns to the PatternsToMatch list, but we 3752 // intentionally do not reconsider these. Any variants of added patterns have 3753 // already been added. 3754 // 3755 for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { 3756 MultipleUseVarSet DepVars; 3757 std::vector<TreePatternNode*> Variants; 3758 FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars); 3759 DEBUG(errs() << "Dependent/multiply used variables: "); 3760 DEBUG(DumpDepVars(DepVars)); 3761 DEBUG(errs() << "\n"); 3762 GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this, 3763 DepVars); 3764 3765 assert(!Variants.empty() && "Must create at least original variant!"); 3766 Variants.erase(Variants.begin()); // Remove the original pattern. 3767 3768 if (Variants.empty()) // No variants for this pattern. 3769 continue; 3770 3771 DEBUG(errs() << "FOUND VARIANTS OF: "; 3772 PatternsToMatch[i].getSrcPattern()->dump(); 3773 errs() << "\n"); 3774 3775 for (unsigned v = 0, e = Variants.size(); v != e; ++v) { 3776 TreePatternNode *Variant = Variants[v]; 3777 3778 DEBUG(errs() << " VAR#" << v << ": "; 3779 Variant->dump(); 3780 errs() << "\n"); 3781 3782 // Scan to see if an instruction or explicit pattern already matches this. 3783 bool AlreadyExists = false; 3784 for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { 3785 // Skip if the top level predicates do not match. 3786 if (PatternsToMatch[i].getPredicates() != 3787 PatternsToMatch[p].getPredicates()) 3788 continue; 3789 // Check to see if this variant already exists. 3790 if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(), 3791 DepVars)) { 3792 DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n"); 3793 AlreadyExists = true; 3794 break; 3795 } 3796 } 3797 // If we already have it, ignore the variant. 3798 if (AlreadyExists) continue; 3799 3800 // Otherwise, add it to the list of patterns we have. 3801 PatternsToMatch. 3802 push_back(PatternToMatch(PatternsToMatch[i].getSrcRecord(), 3803 PatternsToMatch[i].getPredicates(), 3804 Variant, PatternsToMatch[i].getDstPattern(), 3805 PatternsToMatch[i].getDstRegs(), 3806 PatternsToMatch[i].getAddedComplexity(), 3807 Record::getNewUID())); 3808 } 3809 3810 DEBUG(errs() << "\n"); 3811 } 3812 } 3813