1 //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the CodeGenDAGPatterns class, which is used to read and 10 // represent the patterns present in a .td file for instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenDAGPatterns.h" 15 #include "CodeGenInstruction.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/ADT/MapVector.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringMap.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/Support/Debug.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/TypeSize.h" 27 #include "llvm/TableGen/Error.h" 28 #include "llvm/TableGen/Record.h" 29 #include <algorithm> 30 #include <cstdio> 31 #include <iterator> 32 #include <set> 33 using namespace llvm; 34 35 #define DEBUG_TYPE "dag-patterns" 36 37 static inline bool isIntegerOrPtr(MVT VT) { 38 return VT.isInteger() || VT == MVT::iPTR; 39 } 40 static inline bool isFloatingPoint(MVT VT) { 41 return VT.isFloatingPoint(); 42 } 43 static inline bool isVector(MVT VT) { 44 return VT.isVector(); 45 } 46 static inline bool isScalar(MVT VT) { 47 return !VT.isVector(); 48 } 49 50 template <typename Predicate> 51 static bool berase_if(MachineValueTypeSet &S, Predicate P) { 52 bool Erased = false; 53 // It is ok to iterate over MachineValueTypeSet and remove elements from it 54 // at the same time. 55 for (MVT T : S) { 56 if (!P(T)) 57 continue; 58 Erased = true; 59 S.erase(T); 60 } 61 return Erased; 62 } 63 64 // --- TypeSetByHwMode 65 66 // This is a parameterized type-set class. For each mode there is a list 67 // of types that are currently possible for a given tree node. Type 68 // inference will apply to each mode separately. 69 70 TypeSetByHwMode::TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList) { 71 for (const ValueTypeByHwMode &VVT : VTList) { 72 insert(VVT); 73 AddrSpaces.push_back(VVT.PtrAddrSpace); 74 } 75 } 76 77 bool TypeSetByHwMode::isValueTypeByHwMode(bool AllowEmpty) const { 78 for (const auto &I : *this) { 79 if (I.second.size() > 1) 80 return false; 81 if (!AllowEmpty && I.second.empty()) 82 return false; 83 } 84 return true; 85 } 86 87 ValueTypeByHwMode TypeSetByHwMode::getValueTypeByHwMode() const { 88 assert(isValueTypeByHwMode(true) && 89 "The type set has multiple types for at least one HW mode"); 90 ValueTypeByHwMode VVT; 91 auto ASI = AddrSpaces.begin(); 92 93 for (const auto &I : *this) { 94 MVT T = I.second.empty() ? MVT::Other : *I.second.begin(); 95 VVT.getOrCreateTypeForMode(I.first, T); 96 if (ASI != AddrSpaces.end()) 97 VVT.PtrAddrSpace = *ASI++; 98 } 99 return VVT; 100 } 101 102 bool TypeSetByHwMode::isPossible() const { 103 for (const auto &I : *this) 104 if (!I.second.empty()) 105 return true; 106 return false; 107 } 108 109 bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) { 110 bool Changed = false; 111 bool ContainsDefault = false; 112 MVT DT = MVT::Other; 113 114 for (const auto &P : VVT) { 115 unsigned M = P.first; 116 // Make sure there exists a set for each specific mode from VVT. 117 Changed |= getOrCreate(M).insert(P.second).second; 118 // Cache VVT's default mode. 119 if (DefaultMode == M) { 120 ContainsDefault = true; 121 DT = P.second; 122 } 123 } 124 125 // If VVT has a default mode, add the corresponding type to all 126 // modes in "this" that do not exist in VVT. 127 if (ContainsDefault) 128 for (auto &I : *this) 129 if (!VVT.hasMode(I.first)) 130 Changed |= I.second.insert(DT).second; 131 132 return Changed; 133 } 134 135 // Constrain the type set to be the intersection with VTS. 136 bool TypeSetByHwMode::constrain(const TypeSetByHwMode &VTS) { 137 bool Changed = false; 138 if (hasDefault()) { 139 for (const auto &I : VTS) { 140 unsigned M = I.first; 141 if (M == DefaultMode || hasMode(M)) 142 continue; 143 Map.insert({M, Map.at(DefaultMode)}); 144 Changed = true; 145 } 146 } 147 148 for (auto &I : *this) { 149 unsigned M = I.first; 150 SetType &S = I.second; 151 if (VTS.hasMode(M) || VTS.hasDefault()) { 152 Changed |= intersect(I.second, VTS.get(M)); 153 } else if (!S.empty()) { 154 S.clear(); 155 Changed = true; 156 } 157 } 158 return Changed; 159 } 160 161 template <typename Predicate> 162 bool TypeSetByHwMode::constrain(Predicate P) { 163 bool Changed = false; 164 for (auto &I : *this) 165 Changed |= berase_if(I.second, [&P](MVT VT) { return !P(VT); }); 166 return Changed; 167 } 168 169 template <typename Predicate> 170 bool TypeSetByHwMode::assign_if(const TypeSetByHwMode &VTS, Predicate P) { 171 assert(empty()); 172 for (const auto &I : VTS) { 173 SetType &S = getOrCreate(I.first); 174 for (auto J : I.second) 175 if (P(J)) 176 S.insert(J); 177 } 178 return !empty(); 179 } 180 181 void TypeSetByHwMode::writeToStream(raw_ostream &OS) const { 182 SmallVector<unsigned, 4> Modes; 183 Modes.reserve(Map.size()); 184 185 for (const auto &I : *this) 186 Modes.push_back(I.first); 187 if (Modes.empty()) { 188 OS << "{}"; 189 return; 190 } 191 array_pod_sort(Modes.begin(), Modes.end()); 192 193 OS << '{'; 194 for (unsigned M : Modes) { 195 OS << ' ' << getModeName(M) << ':'; 196 writeToStream(get(M), OS); 197 } 198 OS << " }"; 199 } 200 201 void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) { 202 SmallVector<MVT, 4> Types(S.begin(), S.end()); 203 array_pod_sort(Types.begin(), Types.end()); 204 205 OS << '['; 206 ListSeparator LS(" "); 207 for (const MVT &T : Types) 208 OS << LS << ValueTypeByHwMode::getMVTName(T); 209 OS << ']'; 210 } 211 212 bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const { 213 // The isSimple call is much quicker than hasDefault - check this first. 214 bool IsSimple = isSimple(); 215 bool VTSIsSimple = VTS.isSimple(); 216 if (IsSimple && VTSIsSimple) 217 return *begin() == *VTS.begin(); 218 219 // Speedup: We have a default if the set is simple. 220 bool HaveDefault = IsSimple || hasDefault(); 221 bool VTSHaveDefault = VTSIsSimple || VTS.hasDefault(); 222 if (HaveDefault != VTSHaveDefault) 223 return false; 224 225 SmallSet<unsigned, 4> Modes; 226 for (auto &I : *this) 227 Modes.insert(I.first); 228 for (const auto &I : VTS) 229 Modes.insert(I.first); 230 231 if (HaveDefault) { 232 // Both sets have default mode. 233 for (unsigned M : Modes) { 234 if (get(M) != VTS.get(M)) 235 return false; 236 } 237 } else { 238 // Neither set has default mode. 239 for (unsigned M : Modes) { 240 // If there is no default mode, an empty set is equivalent to not having 241 // the corresponding mode. 242 bool NoModeThis = !hasMode(M) || get(M).empty(); 243 bool NoModeVTS = !VTS.hasMode(M) || VTS.get(M).empty(); 244 if (NoModeThis != NoModeVTS) 245 return false; 246 if (!NoModeThis) 247 if (get(M) != VTS.get(M)) 248 return false; 249 } 250 } 251 252 return true; 253 } 254 255 namespace llvm { 256 raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T) { 257 T.writeToStream(OS); 258 return OS; 259 } 260 } 261 262 LLVM_DUMP_METHOD 263 void TypeSetByHwMode::dump() const { 264 dbgs() << *this << '\n'; 265 } 266 267 bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) { 268 bool OutP = Out.count(MVT::iPTR), InP = In.count(MVT::iPTR); 269 auto Int = [&In](MVT T) -> bool { return !In.count(T); }; 270 271 if (OutP == InP) 272 return berase_if(Out, Int); 273 274 // Compute the intersection of scalars separately to account for only 275 // one set containing iPTR. 276 // The intersection of iPTR with a set of integer scalar types that does not 277 // include iPTR will result in the most specific scalar type: 278 // - iPTR is more specific than any set with two elements or more 279 // - iPTR is less specific than any single integer scalar type. 280 // For example 281 // { iPTR } * { i32 } -> { i32 } 282 // { iPTR } * { i32 i64 } -> { iPTR } 283 // and 284 // { iPTR i32 } * { i32 } -> { i32 } 285 // { iPTR i32 } * { i32 i64 } -> { i32 i64 } 286 // { iPTR i32 } * { i32 i64 i128 } -> { iPTR i32 } 287 288 // Compute the difference between the two sets in such a way that the 289 // iPTR is in the set that is being subtracted. This is to see if there 290 // are any extra scalars in the set without iPTR that are not in the 291 // set containing iPTR. Then the iPTR could be considered a "wildcard" 292 // matching these scalars. If there is only one such scalar, it would 293 // replace the iPTR, if there are more, the iPTR would be retained. 294 SetType Diff; 295 if (InP) { 296 Diff = Out; 297 berase_if(Diff, [&In](MVT T) { return In.count(T); }); 298 // Pre-remove these elements and rely only on InP/OutP to determine 299 // whether a change has been made. 300 berase_if(Out, [&Diff](MVT T) { return Diff.count(T); }); 301 } else { 302 Diff = In; 303 berase_if(Diff, [&Out](MVT T) { return Out.count(T); }); 304 Out.erase(MVT::iPTR); 305 } 306 307 // The actual intersection. 308 bool Changed = berase_if(Out, Int); 309 unsigned NumD = Diff.size(); 310 if (NumD == 0) 311 return Changed; 312 313 if (NumD == 1) { 314 Out.insert(*Diff.begin()); 315 // This is a change only if Out was the one with iPTR (which is now 316 // being replaced). 317 Changed |= OutP; 318 } else { 319 // Multiple elements from Out are now replaced with iPTR. 320 Out.insert(MVT::iPTR); 321 Changed |= !OutP; 322 } 323 return Changed; 324 } 325 326 bool TypeSetByHwMode::validate() const { 327 #ifndef NDEBUG 328 if (empty()) 329 return true; 330 bool AllEmpty = true; 331 for (const auto &I : *this) 332 AllEmpty &= I.second.empty(); 333 return !AllEmpty; 334 #endif 335 return true; 336 } 337 338 // --- TypeInfer 339 340 bool TypeInfer::MergeInTypeInfo(TypeSetByHwMode &Out, 341 const TypeSetByHwMode &In) { 342 ValidateOnExit _1(Out, *this); 343 In.validate(); 344 if (In.empty() || Out == In || TP.hasError()) 345 return false; 346 if (Out.empty()) { 347 Out = In; 348 return true; 349 } 350 351 bool Changed = Out.constrain(In); 352 if (Changed && Out.empty()) 353 TP.error("Type contradiction"); 354 355 return Changed; 356 } 357 358 bool TypeInfer::forceArbitrary(TypeSetByHwMode &Out) { 359 ValidateOnExit _1(Out, *this); 360 if (TP.hasError()) 361 return false; 362 assert(!Out.empty() && "cannot pick from an empty set"); 363 364 bool Changed = false; 365 for (auto &I : Out) { 366 TypeSetByHwMode::SetType &S = I.second; 367 if (S.size() <= 1) 368 continue; 369 MVT T = *S.begin(); // Pick the first element. 370 S.clear(); 371 S.insert(T); 372 Changed = true; 373 } 374 return Changed; 375 } 376 377 bool TypeInfer::EnforceInteger(TypeSetByHwMode &Out) { 378 ValidateOnExit _1(Out, *this); 379 if (TP.hasError()) 380 return false; 381 if (!Out.empty()) 382 return Out.constrain(isIntegerOrPtr); 383 384 return Out.assign_if(getLegalTypes(), isIntegerOrPtr); 385 } 386 387 bool TypeInfer::EnforceFloatingPoint(TypeSetByHwMode &Out) { 388 ValidateOnExit _1(Out, *this); 389 if (TP.hasError()) 390 return false; 391 if (!Out.empty()) 392 return Out.constrain(isFloatingPoint); 393 394 return Out.assign_if(getLegalTypes(), isFloatingPoint); 395 } 396 397 bool TypeInfer::EnforceScalar(TypeSetByHwMode &Out) { 398 ValidateOnExit _1(Out, *this); 399 if (TP.hasError()) 400 return false; 401 if (!Out.empty()) 402 return Out.constrain(isScalar); 403 404 return Out.assign_if(getLegalTypes(), isScalar); 405 } 406 407 bool TypeInfer::EnforceVector(TypeSetByHwMode &Out) { 408 ValidateOnExit _1(Out, *this); 409 if (TP.hasError()) 410 return false; 411 if (!Out.empty()) 412 return Out.constrain(isVector); 413 414 return Out.assign_if(getLegalTypes(), isVector); 415 } 416 417 bool TypeInfer::EnforceAny(TypeSetByHwMode &Out) { 418 ValidateOnExit _1(Out, *this); 419 if (TP.hasError() || !Out.empty()) 420 return false; 421 422 Out = getLegalTypes(); 423 return true; 424 } 425 426 template <typename Iter, typename Pred, typename Less> 427 static Iter min_if(Iter B, Iter E, Pred P, Less L) { 428 if (B == E) 429 return E; 430 Iter Min = E; 431 for (Iter I = B; I != E; ++I) { 432 if (!P(*I)) 433 continue; 434 if (Min == E || L(*I, *Min)) 435 Min = I; 436 } 437 return Min; 438 } 439 440 template <typename Iter, typename Pred, typename Less> 441 static Iter max_if(Iter B, Iter E, Pred P, Less L) { 442 if (B == E) 443 return E; 444 Iter Max = E; 445 for (Iter I = B; I != E; ++I) { 446 if (!P(*I)) 447 continue; 448 if (Max == E || L(*Max, *I)) 449 Max = I; 450 } 451 return Max; 452 } 453 454 /// Make sure that for each type in Small, there exists a larger type in Big. 455 bool TypeInfer::EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big, 456 bool SmallIsVT) { 457 ValidateOnExit _1(Small, *this), _2(Big, *this); 458 if (TP.hasError()) 459 return false; 460 bool Changed = false; 461 462 assert((!SmallIsVT || !Small.empty()) && 463 "Small should not be empty for SDTCisVTSmallerThanOp"); 464 465 if (Small.empty()) 466 Changed |= EnforceAny(Small); 467 if (Big.empty()) 468 Changed |= EnforceAny(Big); 469 470 assert(Small.hasDefault() && Big.hasDefault()); 471 472 SmallVector<unsigned, 4> Modes; 473 union_modes(Small, Big, Modes); 474 475 // 1. Only allow integer or floating point types and make sure that 476 // both sides are both integer or both floating point. 477 // 2. Make sure that either both sides have vector types, or neither 478 // of them does. 479 for (unsigned M : Modes) { 480 TypeSetByHwMode::SetType &S = Small.get(M); 481 TypeSetByHwMode::SetType &B = Big.get(M); 482 483 assert((!SmallIsVT || !S.empty()) && "Expected non-empty type"); 484 485 if (any_of(S, isIntegerOrPtr) && any_of(B, isIntegerOrPtr)) { 486 auto NotInt = [](MVT VT) { return !isIntegerOrPtr(VT); }; 487 Changed |= berase_if(S, NotInt); 488 Changed |= berase_if(B, NotInt); 489 } else if (any_of(S, isFloatingPoint) && any_of(B, isFloatingPoint)) { 490 auto NotFP = [](MVT VT) { return !isFloatingPoint(VT); }; 491 Changed |= berase_if(S, NotFP); 492 Changed |= berase_if(B, NotFP); 493 } else if (SmallIsVT && B.empty()) { 494 // B is empty and since S is a specific VT, it will never be empty. Don't 495 // report this as a change, just clear S and continue. This prevents an 496 // infinite loop. 497 S.clear(); 498 } else if (S.empty() || B.empty()) { 499 Changed = !S.empty() || !B.empty(); 500 S.clear(); 501 B.clear(); 502 } else { 503 TP.error("Incompatible types"); 504 return Changed; 505 } 506 507 if (none_of(S, isVector) || none_of(B, isVector)) { 508 Changed |= berase_if(S, isVector); 509 Changed |= berase_if(B, isVector); 510 } 511 } 512 513 auto LT = [](MVT A, MVT B) -> bool { 514 // Always treat non-scalable MVTs as smaller than scalable MVTs for the 515 // purposes of ordering. 516 auto ASize = std::make_tuple(A.isScalableVector(), A.getScalarSizeInBits(), 517 A.getSizeInBits().getKnownMinSize()); 518 auto BSize = std::make_tuple(B.isScalableVector(), B.getScalarSizeInBits(), 519 B.getSizeInBits().getKnownMinSize()); 520 return ASize < BSize; 521 }; 522 auto SameKindLE = [](MVT A, MVT B) -> bool { 523 // This function is used when removing elements: when a vector is compared 524 // to a non-vector or a scalable vector to any non-scalable MVT, it should 525 // return false (to avoid removal). 526 if (std::make_tuple(A.isVector(), A.isScalableVector()) != 527 std::make_tuple(B.isVector(), B.isScalableVector())) 528 return false; 529 530 return std::make_tuple(A.getScalarSizeInBits(), 531 A.getSizeInBits().getKnownMinSize()) <= 532 std::make_tuple(B.getScalarSizeInBits(), 533 B.getSizeInBits().getKnownMinSize()); 534 }; 535 536 for (unsigned M : Modes) { 537 TypeSetByHwMode::SetType &S = Small.get(M); 538 TypeSetByHwMode::SetType &B = Big.get(M); 539 // MinS = min scalar in Small, remove all scalars from Big that are 540 // smaller-or-equal than MinS. 541 auto MinS = min_if(S.begin(), S.end(), isScalar, LT); 542 if (MinS != S.end()) 543 Changed |= berase_if(B, std::bind(SameKindLE, 544 std::placeholders::_1, *MinS)); 545 546 // MaxS = max scalar in Big, remove all scalars from Small that are 547 // larger than MaxS. 548 auto MaxS = max_if(B.begin(), B.end(), isScalar, LT); 549 if (MaxS != B.end()) 550 Changed |= berase_if(S, std::bind(SameKindLE, 551 *MaxS, std::placeholders::_1)); 552 553 // MinV = min vector in Small, remove all vectors from Big that are 554 // smaller-or-equal than MinV. 555 auto MinV = min_if(S.begin(), S.end(), isVector, LT); 556 if (MinV != S.end()) 557 Changed |= berase_if(B, std::bind(SameKindLE, 558 std::placeholders::_1, *MinV)); 559 560 // MaxV = max vector in Big, remove all vectors from Small that are 561 // larger than MaxV. 562 auto MaxV = max_if(B.begin(), B.end(), isVector, LT); 563 if (MaxV != B.end()) 564 Changed |= berase_if(S, std::bind(SameKindLE, 565 *MaxV, std::placeholders::_1)); 566 } 567 568 return Changed; 569 } 570 571 /// 1. Ensure that for each type T in Vec, T is a vector type, and that 572 /// for each type U in Elem, U is a scalar type. 573 /// 2. Ensure that for each (scalar) type U in Elem, there exists a (vector) 574 /// type T in Vec, such that U is the element type of T. 575 bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, 576 TypeSetByHwMode &Elem) { 577 ValidateOnExit _1(Vec, *this), _2(Elem, *this); 578 if (TP.hasError()) 579 return false; 580 bool Changed = false; 581 582 if (Vec.empty()) 583 Changed |= EnforceVector(Vec); 584 if (Elem.empty()) 585 Changed |= EnforceScalar(Elem); 586 587 SmallVector<unsigned, 4> Modes; 588 union_modes(Vec, Elem, Modes); 589 for (unsigned M : Modes) { 590 TypeSetByHwMode::SetType &V = Vec.get(M); 591 TypeSetByHwMode::SetType &E = Elem.get(M); 592 593 Changed |= berase_if(V, isScalar); // Scalar = !vector 594 Changed |= berase_if(E, isVector); // Vector = !scalar 595 assert(!V.empty() && !E.empty()); 596 597 MachineValueTypeSet VT, ST; 598 // Collect element types from the "vector" set. 599 for (MVT T : V) 600 VT.insert(T.getVectorElementType()); 601 // Collect scalar types from the "element" set. 602 for (MVT T : E) 603 ST.insert(T); 604 605 // Remove from V all (vector) types whose element type is not in S. 606 Changed |= berase_if(V, [&ST](MVT T) -> bool { 607 return !ST.count(T.getVectorElementType()); 608 }); 609 // Remove from E all (scalar) types, for which there is no corresponding 610 // type in V. 611 Changed |= berase_if(E, [&VT](MVT T) -> bool { return !VT.count(T); }); 612 } 613 614 return Changed; 615 } 616 617 bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, 618 const ValueTypeByHwMode &VVT) { 619 TypeSetByHwMode Tmp(VVT); 620 ValidateOnExit _1(Vec, *this), _2(Tmp, *this); 621 return EnforceVectorEltTypeIs(Vec, Tmp); 622 } 623 624 /// Ensure that for each type T in Sub, T is a vector type, and there 625 /// exists a type U in Vec such that U is a vector type with the same 626 /// element type as T and at least as many elements as T. 627 bool TypeInfer::EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec, 628 TypeSetByHwMode &Sub) { 629 ValidateOnExit _1(Vec, *this), _2(Sub, *this); 630 if (TP.hasError()) 631 return false; 632 633 /// Return true if B is a suB-vector of P, i.e. P is a suPer-vector of B. 634 auto IsSubVec = [](MVT B, MVT P) -> bool { 635 if (!B.isVector() || !P.isVector()) 636 return false; 637 // Logically a <4 x i32> is a valid subvector of <n x 4 x i32> 638 // but until there are obvious use-cases for this, keep the 639 // types separate. 640 if (B.isScalableVector() != P.isScalableVector()) 641 return false; 642 if (B.getVectorElementType() != P.getVectorElementType()) 643 return false; 644 return B.getVectorMinNumElements() < P.getVectorMinNumElements(); 645 }; 646 647 /// Return true if S has no element (vector type) that T is a sub-vector of, 648 /// i.e. has the same element type as T and more elements. 649 auto NoSubV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool { 650 for (auto I : S) 651 if (IsSubVec(T, I)) 652 return false; 653 return true; 654 }; 655 656 /// Return true if S has no element (vector type) that T is a super-vector 657 /// of, i.e. has the same element type as T and fewer elements. 658 auto NoSupV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool { 659 for (auto I : S) 660 if (IsSubVec(I, T)) 661 return false; 662 return true; 663 }; 664 665 bool Changed = false; 666 667 if (Vec.empty()) 668 Changed |= EnforceVector(Vec); 669 if (Sub.empty()) 670 Changed |= EnforceVector(Sub); 671 672 SmallVector<unsigned, 4> Modes; 673 union_modes(Vec, Sub, Modes); 674 for (unsigned M : Modes) { 675 TypeSetByHwMode::SetType &S = Sub.get(M); 676 TypeSetByHwMode::SetType &V = Vec.get(M); 677 678 Changed |= berase_if(S, isScalar); 679 680 // Erase all types from S that are not sub-vectors of a type in V. 681 Changed |= berase_if(S, std::bind(NoSubV, V, std::placeholders::_1)); 682 683 // Erase all types from V that are not super-vectors of a type in S. 684 Changed |= berase_if(V, std::bind(NoSupV, S, std::placeholders::_1)); 685 } 686 687 return Changed; 688 } 689 690 /// 1. Ensure that V has a scalar type iff W has a scalar type. 691 /// 2. Ensure that for each vector type T in V, there exists a vector 692 /// type U in W, such that T and U have the same number of elements. 693 /// 3. Ensure that for each vector type U in W, there exists a vector 694 /// type T in V, such that T and U have the same number of elements 695 /// (reverse of 2). 696 bool TypeInfer::EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W) { 697 ValidateOnExit _1(V, *this), _2(W, *this); 698 if (TP.hasError()) 699 return false; 700 701 bool Changed = false; 702 if (V.empty()) 703 Changed |= EnforceAny(V); 704 if (W.empty()) 705 Changed |= EnforceAny(W); 706 707 // An actual vector type cannot have 0 elements, so we can treat scalars 708 // as zero-length vectors. This way both vectors and scalars can be 709 // processed identically. 710 auto NoLength = [](const SmallDenseSet<ElementCount> &Lengths, 711 MVT T) -> bool { 712 return !Lengths.count(T.isVector() ? T.getVectorElementCount() 713 : ElementCount::getNull()); 714 }; 715 716 SmallVector<unsigned, 4> Modes; 717 union_modes(V, W, Modes); 718 for (unsigned M : Modes) { 719 TypeSetByHwMode::SetType &VS = V.get(M); 720 TypeSetByHwMode::SetType &WS = W.get(M); 721 722 SmallDenseSet<ElementCount> VN, WN; 723 for (MVT T : VS) 724 VN.insert(T.isVector() ? T.getVectorElementCount() 725 : ElementCount::getNull()); 726 for (MVT T : WS) 727 WN.insert(T.isVector() ? T.getVectorElementCount() 728 : ElementCount::getNull()); 729 730 Changed |= berase_if(VS, std::bind(NoLength, WN, std::placeholders::_1)); 731 Changed |= berase_if(WS, std::bind(NoLength, VN, std::placeholders::_1)); 732 } 733 return Changed; 734 } 735 736 namespace { 737 struct TypeSizeComparator { 738 bool operator()(const TypeSize &LHS, const TypeSize &RHS) const { 739 return std::make_tuple(LHS.isScalable(), LHS.getKnownMinValue()) < 740 std::make_tuple(RHS.isScalable(), RHS.getKnownMinValue()); 741 } 742 }; 743 } // end anonymous namespace 744 745 /// 1. Ensure that for each type T in A, there exists a type U in B, 746 /// such that T and U have equal size in bits. 747 /// 2. Ensure that for each type U in B, there exists a type T in A 748 /// such that T and U have equal size in bits (reverse of 1). 749 bool TypeInfer::EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B) { 750 ValidateOnExit _1(A, *this), _2(B, *this); 751 if (TP.hasError()) 752 return false; 753 bool Changed = false; 754 if (A.empty()) 755 Changed |= EnforceAny(A); 756 if (B.empty()) 757 Changed |= EnforceAny(B); 758 759 typedef SmallSet<TypeSize, 2, TypeSizeComparator> TypeSizeSet; 760 761 auto NoSize = [](const TypeSizeSet &Sizes, MVT T) -> bool { 762 return !Sizes.count(T.getSizeInBits()); 763 }; 764 765 SmallVector<unsigned, 4> Modes; 766 union_modes(A, B, Modes); 767 for (unsigned M : Modes) { 768 TypeSetByHwMode::SetType &AS = A.get(M); 769 TypeSetByHwMode::SetType &BS = B.get(M); 770 TypeSizeSet AN, BN; 771 772 for (MVT T : AS) 773 AN.insert(T.getSizeInBits()); 774 for (MVT T : BS) 775 BN.insert(T.getSizeInBits()); 776 777 Changed |= berase_if(AS, std::bind(NoSize, BN, std::placeholders::_1)); 778 Changed |= berase_if(BS, std::bind(NoSize, AN, std::placeholders::_1)); 779 } 780 781 return Changed; 782 } 783 784 void TypeInfer::expandOverloads(TypeSetByHwMode &VTS) { 785 ValidateOnExit _1(VTS, *this); 786 const TypeSetByHwMode &Legal = getLegalTypes(); 787 assert(Legal.isDefaultOnly() && "Default-mode only expected"); 788 const TypeSetByHwMode::SetType &LegalTypes = Legal.get(DefaultMode); 789 790 for (auto &I : VTS) 791 expandOverloads(I.second, LegalTypes); 792 } 793 794 void TypeInfer::expandOverloads(TypeSetByHwMode::SetType &Out, 795 const TypeSetByHwMode::SetType &Legal) { 796 std::set<MVT> Ovs; 797 for (MVT T : Out) { 798 if (!T.isOverloaded()) 799 continue; 800 801 Ovs.insert(T); 802 // MachineValueTypeSet allows iteration and erasing. 803 Out.erase(T); 804 } 805 806 for (MVT Ov : Ovs) { 807 switch (Ov.SimpleTy) { 808 case MVT::iPTRAny: 809 Out.insert(MVT::iPTR); 810 return; 811 case MVT::iAny: 812 for (MVT T : MVT::integer_valuetypes()) 813 if (Legal.count(T)) 814 Out.insert(T); 815 for (MVT T : MVT::integer_fixedlen_vector_valuetypes()) 816 if (Legal.count(T)) 817 Out.insert(T); 818 for (MVT T : MVT::integer_scalable_vector_valuetypes()) 819 if (Legal.count(T)) 820 Out.insert(T); 821 return; 822 case MVT::fAny: 823 for (MVT T : MVT::fp_valuetypes()) 824 if (Legal.count(T)) 825 Out.insert(T); 826 for (MVT T : MVT::fp_fixedlen_vector_valuetypes()) 827 if (Legal.count(T)) 828 Out.insert(T); 829 for (MVT T : MVT::fp_scalable_vector_valuetypes()) 830 if (Legal.count(T)) 831 Out.insert(T); 832 return; 833 case MVT::vAny: 834 for (MVT T : MVT::vector_valuetypes()) 835 if (Legal.count(T)) 836 Out.insert(T); 837 return; 838 case MVT::Any: 839 for (MVT T : MVT::all_valuetypes()) 840 if (Legal.count(T)) 841 Out.insert(T); 842 return; 843 default: 844 break; 845 } 846 } 847 } 848 849 const TypeSetByHwMode &TypeInfer::getLegalTypes() { 850 if (!LegalTypesCached) { 851 TypeSetByHwMode::SetType &LegalTypes = LegalCache.getOrCreate(DefaultMode); 852 // Stuff all types from all modes into the default mode. 853 const TypeSetByHwMode <S = TP.getDAGPatterns().getLegalTypes(); 854 for (const auto &I : LTS) 855 LegalTypes.insert(I.second); 856 LegalTypesCached = true; 857 } 858 assert(LegalCache.isDefaultOnly() && "Default-mode only expected"); 859 return LegalCache; 860 } 861 862 #ifndef NDEBUG 863 TypeInfer::ValidateOnExit::~ValidateOnExit() { 864 if (Infer.Validate && !VTS.validate()) { 865 dbgs() << "Type set is empty for each HW mode:\n" 866 "possible type contradiction in the pattern below " 867 "(use -print-records with llvm-tblgen to see all " 868 "expanded records).\n"; 869 Infer.TP.dump(); 870 dbgs() << "Generated from record:\n"; 871 Infer.TP.getRecord()->dump(); 872 PrintFatalError(Infer.TP.getRecord()->getLoc(), 873 "Type set is empty for each HW mode in '" + 874 Infer.TP.getRecord()->getName() + "'"); 875 } 876 } 877 #endif 878 879 880 //===----------------------------------------------------------------------===// 881 // ScopedName Implementation 882 //===----------------------------------------------------------------------===// 883 884 bool ScopedName::operator==(const ScopedName &o) const { 885 return Scope == o.Scope && Identifier == o.Identifier; 886 } 887 888 bool ScopedName::operator!=(const ScopedName &o) const { 889 return !(*this == o); 890 } 891 892 893 //===----------------------------------------------------------------------===// 894 // TreePredicateFn Implementation 895 //===----------------------------------------------------------------------===// 896 897 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag. 898 TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) { 899 assert( 900 (!hasPredCode() || !hasImmCode()) && 901 ".td file corrupt: can't have a node predicate *and* an imm predicate"); 902 } 903 904 bool TreePredicateFn::hasPredCode() const { 905 return isLoad() || isStore() || isAtomic() || 906 !PatFragRec->getRecord()->getValueAsString("PredicateCode").empty(); 907 } 908 909 std::string TreePredicateFn::getPredCode() const { 910 std::string Code; 911 912 if (!isLoad() && !isStore() && !isAtomic()) { 913 Record *MemoryVT = getMemoryVT(); 914 915 if (MemoryVT) 916 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 917 "MemoryVT requires IsLoad or IsStore"); 918 } 919 920 if (!isLoad() && !isStore()) { 921 if (isUnindexed()) 922 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 923 "IsUnindexed requires IsLoad or IsStore"); 924 925 Record *ScalarMemoryVT = getScalarMemoryVT(); 926 927 if (ScalarMemoryVT) 928 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 929 "ScalarMemoryVT requires IsLoad or IsStore"); 930 } 931 932 if (isLoad() + isStore() + isAtomic() > 1) 933 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 934 "IsLoad, IsStore, and IsAtomic are mutually exclusive"); 935 936 if (isLoad()) { 937 if (!isUnindexed() && !isNonExtLoad() && !isAnyExtLoad() && 938 !isSignExtLoad() && !isZeroExtLoad() && getMemoryVT() == nullptr && 939 getScalarMemoryVT() == nullptr && getAddressSpaces() == nullptr && 940 getMinAlignment() < 1) 941 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 942 "IsLoad cannot be used by itself"); 943 } else { 944 if (isNonExtLoad()) 945 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 946 "IsNonExtLoad requires IsLoad"); 947 if (isAnyExtLoad()) 948 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 949 "IsAnyExtLoad requires IsLoad"); 950 if (isSignExtLoad()) 951 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 952 "IsSignExtLoad requires IsLoad"); 953 if (isZeroExtLoad()) 954 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 955 "IsZeroExtLoad requires IsLoad"); 956 } 957 958 if (isStore()) { 959 if (!isUnindexed() && !isTruncStore() && !isNonTruncStore() && 960 getMemoryVT() == nullptr && getScalarMemoryVT() == nullptr && 961 getAddressSpaces() == nullptr && getMinAlignment() < 1) 962 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 963 "IsStore cannot be used by itself"); 964 } else { 965 if (isNonTruncStore()) 966 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 967 "IsNonTruncStore requires IsStore"); 968 if (isTruncStore()) 969 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 970 "IsTruncStore requires IsStore"); 971 } 972 973 if (isAtomic()) { 974 if (getMemoryVT() == nullptr && !isAtomicOrderingMonotonic() && 975 getAddressSpaces() == nullptr && 976 !isAtomicOrderingAcquire() && !isAtomicOrderingRelease() && 977 !isAtomicOrderingAcquireRelease() && 978 !isAtomicOrderingSequentiallyConsistent() && 979 !isAtomicOrderingAcquireOrStronger() && 980 !isAtomicOrderingReleaseOrStronger() && 981 !isAtomicOrderingWeakerThanAcquire() && 982 !isAtomicOrderingWeakerThanRelease()) 983 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 984 "IsAtomic cannot be used by itself"); 985 } else { 986 if (isAtomicOrderingMonotonic()) 987 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 988 "IsAtomicOrderingMonotonic requires IsAtomic"); 989 if (isAtomicOrderingAcquire()) 990 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 991 "IsAtomicOrderingAcquire requires IsAtomic"); 992 if (isAtomicOrderingRelease()) 993 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 994 "IsAtomicOrderingRelease requires IsAtomic"); 995 if (isAtomicOrderingAcquireRelease()) 996 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 997 "IsAtomicOrderingAcquireRelease requires IsAtomic"); 998 if (isAtomicOrderingSequentiallyConsistent()) 999 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1000 "IsAtomicOrderingSequentiallyConsistent requires IsAtomic"); 1001 if (isAtomicOrderingAcquireOrStronger()) 1002 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1003 "IsAtomicOrderingAcquireOrStronger requires IsAtomic"); 1004 if (isAtomicOrderingReleaseOrStronger()) 1005 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1006 "IsAtomicOrderingReleaseOrStronger requires IsAtomic"); 1007 if (isAtomicOrderingWeakerThanAcquire()) 1008 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1009 "IsAtomicOrderingWeakerThanAcquire requires IsAtomic"); 1010 } 1011 1012 if (isLoad() || isStore() || isAtomic()) { 1013 if (ListInit *AddressSpaces = getAddressSpaces()) { 1014 Code += "unsigned AddrSpace = cast<MemSDNode>(N)->getAddressSpace();\n" 1015 " if ("; 1016 1017 ListSeparator LS(" && "); 1018 for (Init *Val : AddressSpaces->getValues()) { 1019 Code += LS; 1020 1021 IntInit *IntVal = dyn_cast<IntInit>(Val); 1022 if (!IntVal) { 1023 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1024 "AddressSpaces element must be integer"); 1025 } 1026 1027 Code += "AddrSpace != " + utostr(IntVal->getValue()); 1028 } 1029 1030 Code += ")\nreturn false;\n"; 1031 } 1032 1033 int64_t MinAlign = getMinAlignment(); 1034 if (MinAlign > 0) { 1035 Code += "if (cast<MemSDNode>(N)->getAlign() < Align("; 1036 Code += utostr(MinAlign); 1037 Code += "))\nreturn false;\n"; 1038 } 1039 1040 Record *MemoryVT = getMemoryVT(); 1041 1042 if (MemoryVT) 1043 Code += ("if (cast<MemSDNode>(N)->getMemoryVT() != MVT::" + 1044 MemoryVT->getName() + ") return false;\n") 1045 .str(); 1046 } 1047 1048 if (isAtomic() && isAtomicOrderingMonotonic()) 1049 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != " 1050 "AtomicOrdering::Monotonic) return false;\n"; 1051 if (isAtomic() && isAtomicOrderingAcquire()) 1052 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != " 1053 "AtomicOrdering::Acquire) return false;\n"; 1054 if (isAtomic() && isAtomicOrderingRelease()) 1055 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != " 1056 "AtomicOrdering::Release) return false;\n"; 1057 if (isAtomic() && isAtomicOrderingAcquireRelease()) 1058 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != " 1059 "AtomicOrdering::AcquireRelease) return false;\n"; 1060 if (isAtomic() && isAtomicOrderingSequentiallyConsistent()) 1061 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != " 1062 "AtomicOrdering::SequentiallyConsistent) return false;\n"; 1063 1064 if (isAtomic() && isAtomicOrderingAcquireOrStronger()) 1065 Code += "if (!isAcquireOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) " 1066 "return false;\n"; 1067 if (isAtomic() && isAtomicOrderingWeakerThanAcquire()) 1068 Code += "if (isAcquireOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) " 1069 "return false;\n"; 1070 1071 if (isAtomic() && isAtomicOrderingReleaseOrStronger()) 1072 Code += "if (!isReleaseOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) " 1073 "return false;\n"; 1074 if (isAtomic() && isAtomicOrderingWeakerThanRelease()) 1075 Code += "if (isReleaseOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) " 1076 "return false;\n"; 1077 1078 if (isLoad() || isStore()) { 1079 StringRef SDNodeName = isLoad() ? "LoadSDNode" : "StoreSDNode"; 1080 1081 if (isUnindexed()) 1082 Code += ("if (cast<" + SDNodeName + 1083 ">(N)->getAddressingMode() != ISD::UNINDEXED) " 1084 "return false;\n") 1085 .str(); 1086 1087 if (isLoad()) { 1088 if ((isNonExtLoad() + isAnyExtLoad() + isSignExtLoad() + 1089 isZeroExtLoad()) > 1) 1090 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1091 "IsNonExtLoad, IsAnyExtLoad, IsSignExtLoad, and " 1092 "IsZeroExtLoad are mutually exclusive"); 1093 if (isNonExtLoad()) 1094 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != " 1095 "ISD::NON_EXTLOAD) return false;\n"; 1096 if (isAnyExtLoad()) 1097 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::EXTLOAD) " 1098 "return false;\n"; 1099 if (isSignExtLoad()) 1100 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::SEXTLOAD) " 1101 "return false;\n"; 1102 if (isZeroExtLoad()) 1103 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::ZEXTLOAD) " 1104 "return false;\n"; 1105 } else { 1106 if ((isNonTruncStore() + isTruncStore()) > 1) 1107 PrintFatalError( 1108 getOrigPatFragRecord()->getRecord()->getLoc(), 1109 "IsNonTruncStore, and IsTruncStore are mutually exclusive"); 1110 if (isNonTruncStore()) 1111 Code += 1112 " if (cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n"; 1113 if (isTruncStore()) 1114 Code += 1115 " if (!cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n"; 1116 } 1117 1118 Record *ScalarMemoryVT = getScalarMemoryVT(); 1119 1120 if (ScalarMemoryVT) 1121 Code += ("if (cast<" + SDNodeName + 1122 ">(N)->getMemoryVT().getScalarType() != MVT::" + 1123 ScalarMemoryVT->getName() + ") return false;\n") 1124 .str(); 1125 } 1126 1127 std::string PredicateCode = 1128 std::string(PatFragRec->getRecord()->getValueAsString("PredicateCode")); 1129 1130 Code += PredicateCode; 1131 1132 if (PredicateCode.empty() && !Code.empty()) 1133 Code += "return true;\n"; 1134 1135 return Code; 1136 } 1137 1138 bool TreePredicateFn::hasImmCode() const { 1139 return !PatFragRec->getRecord()->getValueAsString("ImmediateCode").empty(); 1140 } 1141 1142 std::string TreePredicateFn::getImmCode() const { 1143 return std::string( 1144 PatFragRec->getRecord()->getValueAsString("ImmediateCode")); 1145 } 1146 1147 bool TreePredicateFn::immCodeUsesAPInt() const { 1148 return getOrigPatFragRecord()->getRecord()->getValueAsBit("IsAPInt"); 1149 } 1150 1151 bool TreePredicateFn::immCodeUsesAPFloat() const { 1152 bool Unset; 1153 // The return value will be false when IsAPFloat is unset. 1154 return getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset("IsAPFloat", 1155 Unset); 1156 } 1157 1158 bool TreePredicateFn::isPredefinedPredicateEqualTo(StringRef Field, 1159 bool Value) const { 1160 bool Unset; 1161 bool Result = 1162 getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset(Field, Unset); 1163 if (Unset) 1164 return false; 1165 return Result == Value; 1166 } 1167 bool TreePredicateFn::usesOperands() const { 1168 return isPredefinedPredicateEqualTo("PredicateCodeUsesOperands", true); 1169 } 1170 bool TreePredicateFn::isLoad() const { 1171 return isPredefinedPredicateEqualTo("IsLoad", true); 1172 } 1173 bool TreePredicateFn::isStore() const { 1174 return isPredefinedPredicateEqualTo("IsStore", true); 1175 } 1176 bool TreePredicateFn::isAtomic() const { 1177 return isPredefinedPredicateEqualTo("IsAtomic", true); 1178 } 1179 bool TreePredicateFn::isUnindexed() const { 1180 return isPredefinedPredicateEqualTo("IsUnindexed", true); 1181 } 1182 bool TreePredicateFn::isNonExtLoad() const { 1183 return isPredefinedPredicateEqualTo("IsNonExtLoad", true); 1184 } 1185 bool TreePredicateFn::isAnyExtLoad() const { 1186 return isPredefinedPredicateEqualTo("IsAnyExtLoad", true); 1187 } 1188 bool TreePredicateFn::isSignExtLoad() const { 1189 return isPredefinedPredicateEqualTo("IsSignExtLoad", true); 1190 } 1191 bool TreePredicateFn::isZeroExtLoad() const { 1192 return isPredefinedPredicateEqualTo("IsZeroExtLoad", true); 1193 } 1194 bool TreePredicateFn::isNonTruncStore() const { 1195 return isPredefinedPredicateEqualTo("IsTruncStore", false); 1196 } 1197 bool TreePredicateFn::isTruncStore() const { 1198 return isPredefinedPredicateEqualTo("IsTruncStore", true); 1199 } 1200 bool TreePredicateFn::isAtomicOrderingMonotonic() const { 1201 return isPredefinedPredicateEqualTo("IsAtomicOrderingMonotonic", true); 1202 } 1203 bool TreePredicateFn::isAtomicOrderingAcquire() const { 1204 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquire", true); 1205 } 1206 bool TreePredicateFn::isAtomicOrderingRelease() const { 1207 return isPredefinedPredicateEqualTo("IsAtomicOrderingRelease", true); 1208 } 1209 bool TreePredicateFn::isAtomicOrderingAcquireRelease() const { 1210 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireRelease", true); 1211 } 1212 bool TreePredicateFn::isAtomicOrderingSequentiallyConsistent() const { 1213 return isPredefinedPredicateEqualTo("IsAtomicOrderingSequentiallyConsistent", 1214 true); 1215 } 1216 bool TreePredicateFn::isAtomicOrderingAcquireOrStronger() const { 1217 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", true); 1218 } 1219 bool TreePredicateFn::isAtomicOrderingWeakerThanAcquire() const { 1220 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", false); 1221 } 1222 bool TreePredicateFn::isAtomicOrderingReleaseOrStronger() const { 1223 return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", true); 1224 } 1225 bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const { 1226 return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", false); 1227 } 1228 Record *TreePredicateFn::getMemoryVT() const { 1229 Record *R = getOrigPatFragRecord()->getRecord(); 1230 if (R->isValueUnset("MemoryVT")) 1231 return nullptr; 1232 return R->getValueAsDef("MemoryVT"); 1233 } 1234 1235 ListInit *TreePredicateFn::getAddressSpaces() const { 1236 Record *R = getOrigPatFragRecord()->getRecord(); 1237 if (R->isValueUnset("AddressSpaces")) 1238 return nullptr; 1239 return R->getValueAsListInit("AddressSpaces"); 1240 } 1241 1242 int64_t TreePredicateFn::getMinAlignment() const { 1243 Record *R = getOrigPatFragRecord()->getRecord(); 1244 if (R->isValueUnset("MinAlignment")) 1245 return 0; 1246 return R->getValueAsInt("MinAlignment"); 1247 } 1248 1249 Record *TreePredicateFn::getScalarMemoryVT() const { 1250 Record *R = getOrigPatFragRecord()->getRecord(); 1251 if (R->isValueUnset("ScalarMemoryVT")) 1252 return nullptr; 1253 return R->getValueAsDef("ScalarMemoryVT"); 1254 } 1255 bool TreePredicateFn::hasGISelPredicateCode() const { 1256 return !PatFragRec->getRecord() 1257 ->getValueAsString("GISelPredicateCode") 1258 .empty(); 1259 } 1260 std::string TreePredicateFn::getGISelPredicateCode() const { 1261 return std::string( 1262 PatFragRec->getRecord()->getValueAsString("GISelPredicateCode")); 1263 } 1264 1265 StringRef TreePredicateFn::getImmType() const { 1266 if (immCodeUsesAPInt()) 1267 return "const APInt &"; 1268 if (immCodeUsesAPFloat()) 1269 return "const APFloat &"; 1270 return "int64_t"; 1271 } 1272 1273 StringRef TreePredicateFn::getImmTypeIdentifier() const { 1274 if (immCodeUsesAPInt()) 1275 return "APInt"; 1276 if (immCodeUsesAPFloat()) 1277 return "APFloat"; 1278 return "I64"; 1279 } 1280 1281 /// isAlwaysTrue - Return true if this is a noop predicate. 1282 bool TreePredicateFn::isAlwaysTrue() const { 1283 return !hasPredCode() && !hasImmCode(); 1284 } 1285 1286 /// Return the name to use in the generated code to reference this, this is 1287 /// "Predicate_foo" if from a pattern fragment "foo". 1288 std::string TreePredicateFn::getFnName() const { 1289 return "Predicate_" + PatFragRec->getRecord()->getName().str(); 1290 } 1291 1292 /// getCodeToRunOnSDNode - Return the code for the function body that 1293 /// evaluates this predicate. The argument is expected to be in "Node", 1294 /// not N. This handles casting and conversion to a concrete node type as 1295 /// appropriate. 1296 std::string TreePredicateFn::getCodeToRunOnSDNode() const { 1297 // Handle immediate predicates first. 1298 std::string ImmCode = getImmCode(); 1299 if (!ImmCode.empty()) { 1300 if (isLoad()) 1301 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1302 "IsLoad cannot be used with ImmLeaf or its subclasses"); 1303 if (isStore()) 1304 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1305 "IsStore cannot be used with ImmLeaf or its subclasses"); 1306 if (isUnindexed()) 1307 PrintFatalError( 1308 getOrigPatFragRecord()->getRecord()->getLoc(), 1309 "IsUnindexed cannot be used with ImmLeaf or its subclasses"); 1310 if (isNonExtLoad()) 1311 PrintFatalError( 1312 getOrigPatFragRecord()->getRecord()->getLoc(), 1313 "IsNonExtLoad cannot be used with ImmLeaf or its subclasses"); 1314 if (isAnyExtLoad()) 1315 PrintFatalError( 1316 getOrigPatFragRecord()->getRecord()->getLoc(), 1317 "IsAnyExtLoad cannot be used with ImmLeaf or its subclasses"); 1318 if (isSignExtLoad()) 1319 PrintFatalError( 1320 getOrigPatFragRecord()->getRecord()->getLoc(), 1321 "IsSignExtLoad cannot be used with ImmLeaf or its subclasses"); 1322 if (isZeroExtLoad()) 1323 PrintFatalError( 1324 getOrigPatFragRecord()->getRecord()->getLoc(), 1325 "IsZeroExtLoad cannot be used with ImmLeaf or its subclasses"); 1326 if (isNonTruncStore()) 1327 PrintFatalError( 1328 getOrigPatFragRecord()->getRecord()->getLoc(), 1329 "IsNonTruncStore cannot be used with ImmLeaf or its subclasses"); 1330 if (isTruncStore()) 1331 PrintFatalError( 1332 getOrigPatFragRecord()->getRecord()->getLoc(), 1333 "IsTruncStore cannot be used with ImmLeaf or its subclasses"); 1334 if (getMemoryVT()) 1335 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1336 "MemoryVT cannot be used with ImmLeaf or its subclasses"); 1337 if (getScalarMemoryVT()) 1338 PrintFatalError( 1339 getOrigPatFragRecord()->getRecord()->getLoc(), 1340 "ScalarMemoryVT cannot be used with ImmLeaf or its subclasses"); 1341 1342 std::string Result = (" " + getImmType() + " Imm = ").str(); 1343 if (immCodeUsesAPFloat()) 1344 Result += "cast<ConstantFPSDNode>(Node)->getValueAPF();\n"; 1345 else if (immCodeUsesAPInt()) 1346 Result += "cast<ConstantSDNode>(Node)->getAPIntValue();\n"; 1347 else 1348 Result += "cast<ConstantSDNode>(Node)->getSExtValue();\n"; 1349 return Result + ImmCode; 1350 } 1351 1352 // Handle arbitrary node predicates. 1353 assert(hasPredCode() && "Don't have any predicate code!"); 1354 1355 // If this is using PatFrags, there are multiple trees to search. They should 1356 // all have the same class. FIXME: Is there a way to find a common 1357 // superclass? 1358 StringRef ClassName; 1359 for (const auto &Tree : PatFragRec->getTrees()) { 1360 StringRef TreeClassName; 1361 if (Tree->isLeaf()) 1362 TreeClassName = "SDNode"; 1363 else { 1364 Record *Op = Tree->getOperator(); 1365 const SDNodeInfo &Info = PatFragRec->getDAGPatterns().getSDNodeInfo(Op); 1366 TreeClassName = Info.getSDClassName(); 1367 } 1368 1369 if (ClassName.empty()) 1370 ClassName = TreeClassName; 1371 else if (ClassName != TreeClassName) { 1372 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(), 1373 "PatFrags trees do not have consistent class"); 1374 } 1375 } 1376 1377 std::string Result; 1378 if (ClassName == "SDNode") 1379 Result = " SDNode *N = Node;\n"; 1380 else 1381 Result = " auto *N = cast<" + ClassName.str() + ">(Node);\n"; 1382 1383 return (Twine(Result) + " (void)N;\n" + getPredCode()).str(); 1384 } 1385 1386 //===----------------------------------------------------------------------===// 1387 // PatternToMatch implementation 1388 // 1389 1390 static bool isImmAllOnesAllZerosMatch(const TreePatternNode *P) { 1391 if (!P->isLeaf()) 1392 return false; 1393 DefInit *DI = dyn_cast<DefInit>(P->getLeafValue()); 1394 if (!DI) 1395 return false; 1396 1397 Record *R = DI->getDef(); 1398 return R->getName() == "immAllOnesV" || R->getName() == "immAllZerosV"; 1399 } 1400 1401 /// getPatternSize - Return the 'size' of this pattern. We want to match large 1402 /// patterns before small ones. This is used to determine the size of a 1403 /// pattern. 1404 static unsigned getPatternSize(const TreePatternNode *P, 1405 const CodeGenDAGPatterns &CGP) { 1406 unsigned Size = 3; // The node itself. 1407 // If the root node is a ConstantSDNode, increases its size. 1408 // e.g. (set R32:$dst, 0). 1409 if (P->isLeaf() && isa<IntInit>(P->getLeafValue())) 1410 Size += 2; 1411 1412 if (const ComplexPattern *AM = P->getComplexPatternInfo(CGP)) { 1413 Size += AM->getComplexity(); 1414 // We don't want to count any children twice, so return early. 1415 return Size; 1416 } 1417 1418 // If this node has some predicate function that must match, it adds to the 1419 // complexity of this node. 1420 if (!P->getPredicateCalls().empty()) 1421 ++Size; 1422 1423 // Count children in the count if they are also nodes. 1424 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { 1425 const TreePatternNode *Child = P->getChild(i); 1426 if (!Child->isLeaf() && Child->getNumTypes()) { 1427 const TypeSetByHwMode &T0 = Child->getExtType(0); 1428 // At this point, all variable type sets should be simple, i.e. only 1429 // have a default mode. 1430 if (T0.getMachineValueType() != MVT::Other) { 1431 Size += getPatternSize(Child, CGP); 1432 continue; 1433 } 1434 } 1435 if (Child->isLeaf()) { 1436 if (isa<IntInit>(Child->getLeafValue())) 1437 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2). 1438 else if (Child->getComplexPatternInfo(CGP)) 1439 Size += getPatternSize(Child, CGP); 1440 else if (isImmAllOnesAllZerosMatch(Child)) 1441 Size += 4; // Matches a build_vector(+3) and a predicate (+1). 1442 else if (!Child->getPredicateCalls().empty()) 1443 ++Size; 1444 } 1445 } 1446 1447 return Size; 1448 } 1449 1450 /// Compute the complexity metric for the input pattern. This roughly 1451 /// corresponds to the number of nodes that are covered. 1452 int PatternToMatch:: 1453 getPatternComplexity(const CodeGenDAGPatterns &CGP) const { 1454 return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity(); 1455 } 1456 1457 void PatternToMatch::getPredicateRecords( 1458 SmallVectorImpl<Record *> &PredicateRecs) const { 1459 for (Init *I : Predicates->getValues()) { 1460 if (DefInit *Pred = dyn_cast<DefInit>(I)) { 1461 Record *Def = Pred->getDef(); 1462 if (!Def->isSubClassOf("Predicate")) { 1463 #ifndef NDEBUG 1464 Def->dump(); 1465 #endif 1466 llvm_unreachable("Unknown predicate type!"); 1467 } 1468 PredicateRecs.push_back(Def); 1469 } 1470 } 1471 // Sort so that different orders get canonicalized to the same string. 1472 llvm::sort(PredicateRecs, LessRecord()); 1473 } 1474 1475 /// getPredicateCheck - Return a single string containing all of this 1476 /// pattern's predicates concatenated with "&&" operators. 1477 /// 1478 std::string PatternToMatch::getPredicateCheck() const { 1479 SmallVector<Record *, 4> PredicateRecs; 1480 getPredicateRecords(PredicateRecs); 1481 1482 SmallString<128> PredicateCheck; 1483 for (Record *Pred : PredicateRecs) { 1484 StringRef CondString = Pred->getValueAsString("CondString"); 1485 if (CondString.empty()) 1486 continue; 1487 if (!PredicateCheck.empty()) 1488 PredicateCheck += " && "; 1489 PredicateCheck += "("; 1490 PredicateCheck += CondString; 1491 PredicateCheck += ")"; 1492 } 1493 1494 if (!HwModeFeatures.empty()) { 1495 if (!PredicateCheck.empty()) 1496 PredicateCheck += " && "; 1497 PredicateCheck += HwModeFeatures; 1498 } 1499 1500 return std::string(PredicateCheck); 1501 } 1502 1503 //===----------------------------------------------------------------------===// 1504 // SDTypeConstraint implementation 1505 // 1506 1507 SDTypeConstraint::SDTypeConstraint(Record *R, const CodeGenHwModes &CGH) { 1508 OperandNo = R->getValueAsInt("OperandNum"); 1509 1510 if (R->isSubClassOf("SDTCisVT")) { 1511 ConstraintType = SDTCisVT; 1512 VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH); 1513 for (const auto &P : VVT) 1514 if (P.second == MVT::isVoid) 1515 PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT"); 1516 } else if (R->isSubClassOf("SDTCisPtrTy")) { 1517 ConstraintType = SDTCisPtrTy; 1518 } else if (R->isSubClassOf("SDTCisInt")) { 1519 ConstraintType = SDTCisInt; 1520 } else if (R->isSubClassOf("SDTCisFP")) { 1521 ConstraintType = SDTCisFP; 1522 } else if (R->isSubClassOf("SDTCisVec")) { 1523 ConstraintType = SDTCisVec; 1524 } else if (R->isSubClassOf("SDTCisSameAs")) { 1525 ConstraintType = SDTCisSameAs; 1526 x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); 1527 } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { 1528 ConstraintType = SDTCisVTSmallerThanOp; 1529 x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = 1530 R->getValueAsInt("OtherOperandNum"); 1531 } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { 1532 ConstraintType = SDTCisOpSmallerThanOp; 1533 x.SDTCisOpSmallerThanOp_Info.BigOperandNum = 1534 R->getValueAsInt("BigOperandNum"); 1535 } else if (R->isSubClassOf("SDTCisEltOfVec")) { 1536 ConstraintType = SDTCisEltOfVec; 1537 x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum"); 1538 } else if (R->isSubClassOf("SDTCisSubVecOfVec")) { 1539 ConstraintType = SDTCisSubVecOfVec; 1540 x.SDTCisSubVecOfVec_Info.OtherOperandNum = 1541 R->getValueAsInt("OtherOpNum"); 1542 } else if (R->isSubClassOf("SDTCVecEltisVT")) { 1543 ConstraintType = SDTCVecEltisVT; 1544 VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH); 1545 for (const auto &P : VVT) { 1546 MVT T = P.second; 1547 if (T.isVector()) 1548 PrintFatalError(R->getLoc(), 1549 "Cannot use vector type as SDTCVecEltisVT"); 1550 if (!T.isInteger() && !T.isFloatingPoint()) 1551 PrintFatalError(R->getLoc(), "Must use integer or floating point type " 1552 "as SDTCVecEltisVT"); 1553 } 1554 } else if (R->isSubClassOf("SDTCisSameNumEltsAs")) { 1555 ConstraintType = SDTCisSameNumEltsAs; 1556 x.SDTCisSameNumEltsAs_Info.OtherOperandNum = 1557 R->getValueAsInt("OtherOperandNum"); 1558 } else if (R->isSubClassOf("SDTCisSameSizeAs")) { 1559 ConstraintType = SDTCisSameSizeAs; 1560 x.SDTCisSameSizeAs_Info.OtherOperandNum = 1561 R->getValueAsInt("OtherOperandNum"); 1562 } else { 1563 PrintFatalError(R->getLoc(), 1564 "Unrecognized SDTypeConstraint '" + R->getName() + "'!\n"); 1565 } 1566 } 1567 1568 /// getOperandNum - Return the node corresponding to operand #OpNo in tree 1569 /// N, and the result number in ResNo. 1570 static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N, 1571 const SDNodeInfo &NodeInfo, 1572 unsigned &ResNo) { 1573 unsigned NumResults = NodeInfo.getNumResults(); 1574 if (OpNo < NumResults) { 1575 ResNo = OpNo; 1576 return N; 1577 } 1578 1579 OpNo -= NumResults; 1580 1581 if (OpNo >= N->getNumChildren()) { 1582 std::string S; 1583 raw_string_ostream OS(S); 1584 OS << "Invalid operand number in type constraint " 1585 << (OpNo+NumResults) << " "; 1586 N->print(OS); 1587 PrintFatalError(S); 1588 } 1589 1590 return N->getChild(OpNo); 1591 } 1592 1593 /// ApplyTypeConstraint - Given a node in a pattern, apply this type 1594 /// constraint to the nodes operands. This returns true if it makes a 1595 /// change, false otherwise. If a type contradiction is found, flag an error. 1596 bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, 1597 const SDNodeInfo &NodeInfo, 1598 TreePattern &TP) const { 1599 if (TP.hasError()) 1600 return false; 1601 1602 unsigned ResNo = 0; // The result number being referenced. 1603 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo); 1604 TypeInfer &TI = TP.getInfer(); 1605 1606 switch (ConstraintType) { 1607 case SDTCisVT: 1608 // Operand must be a particular type. 1609 return NodeToApply->UpdateNodeType(ResNo, VVT, TP); 1610 case SDTCisPtrTy: 1611 // Operand must be same as target pointer type. 1612 return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP); 1613 case SDTCisInt: 1614 // Require it to be one of the legal integer VTs. 1615 return TI.EnforceInteger(NodeToApply->getExtType(ResNo)); 1616 case SDTCisFP: 1617 // Require it to be one of the legal fp VTs. 1618 return TI.EnforceFloatingPoint(NodeToApply->getExtType(ResNo)); 1619 case SDTCisVec: 1620 // Require it to be one of the legal vector VTs. 1621 return TI.EnforceVector(NodeToApply->getExtType(ResNo)); 1622 case SDTCisSameAs: { 1623 unsigned OResNo = 0; 1624 TreePatternNode *OtherNode = 1625 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo); 1626 return (int)NodeToApply->UpdateNodeType(ResNo, 1627 OtherNode->getExtType(OResNo), TP) | 1628 (int)OtherNode->UpdateNodeType(OResNo, 1629 NodeToApply->getExtType(ResNo), TP); 1630 } 1631 case SDTCisVTSmallerThanOp: { 1632 // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must 1633 // have an integer type that is smaller than the VT. 1634 if (!NodeToApply->isLeaf() || 1635 !isa<DefInit>(NodeToApply->getLeafValue()) || 1636 !cast<DefInit>(NodeToApply->getLeafValue())->getDef() 1637 ->isSubClassOf("ValueType")) { 1638 TP.error(N->getOperator()->getName() + " expects a VT operand!"); 1639 return false; 1640 } 1641 DefInit *DI = cast<DefInit>(NodeToApply->getLeafValue()); 1642 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1643 auto VVT = getValueTypeByHwMode(DI->getDef(), T.getHwModes()); 1644 TypeSetByHwMode TypeListTmp(VVT); 1645 1646 unsigned OResNo = 0; 1647 TreePatternNode *OtherNode = 1648 getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo, 1649 OResNo); 1650 1651 return TI.EnforceSmallerThan(TypeListTmp, OtherNode->getExtType(OResNo), 1652 /*SmallIsVT*/ true); 1653 } 1654 case SDTCisOpSmallerThanOp: { 1655 unsigned BResNo = 0; 1656 TreePatternNode *BigOperand = 1657 getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo, 1658 BResNo); 1659 return TI.EnforceSmallerThan(NodeToApply->getExtType(ResNo), 1660 BigOperand->getExtType(BResNo)); 1661 } 1662 case SDTCisEltOfVec: { 1663 unsigned VResNo = 0; 1664 TreePatternNode *VecOperand = 1665 getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo, 1666 VResNo); 1667 // Filter vector types out of VecOperand that don't have the right element 1668 // type. 1669 return TI.EnforceVectorEltTypeIs(VecOperand->getExtType(VResNo), 1670 NodeToApply->getExtType(ResNo)); 1671 } 1672 case SDTCisSubVecOfVec: { 1673 unsigned VResNo = 0; 1674 TreePatternNode *BigVecOperand = 1675 getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo, 1676 VResNo); 1677 1678 // Filter vector types out of BigVecOperand that don't have the 1679 // right subvector type. 1680 return TI.EnforceVectorSubVectorTypeIs(BigVecOperand->getExtType(VResNo), 1681 NodeToApply->getExtType(ResNo)); 1682 } 1683 case SDTCVecEltisVT: { 1684 return TI.EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), VVT); 1685 } 1686 case SDTCisSameNumEltsAs: { 1687 unsigned OResNo = 0; 1688 TreePatternNode *OtherNode = 1689 getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum, 1690 N, NodeInfo, OResNo); 1691 return TI.EnforceSameNumElts(OtherNode->getExtType(OResNo), 1692 NodeToApply->getExtType(ResNo)); 1693 } 1694 case SDTCisSameSizeAs: { 1695 unsigned OResNo = 0; 1696 TreePatternNode *OtherNode = 1697 getOperandNum(x.SDTCisSameSizeAs_Info.OtherOperandNum, 1698 N, NodeInfo, OResNo); 1699 return TI.EnforceSameSize(OtherNode->getExtType(OResNo), 1700 NodeToApply->getExtType(ResNo)); 1701 } 1702 } 1703 llvm_unreachable("Invalid ConstraintType!"); 1704 } 1705 1706 // Update the node type to match an instruction operand or result as specified 1707 // in the ins or outs lists on the instruction definition. Return true if the 1708 // type was actually changed. 1709 bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo, 1710 Record *Operand, 1711 TreePattern &TP) { 1712 // The 'unknown' operand indicates that types should be inferred from the 1713 // context. 1714 if (Operand->isSubClassOf("unknown_class")) 1715 return false; 1716 1717 // The Operand class specifies a type directly. 1718 if (Operand->isSubClassOf("Operand")) { 1719 Record *R = Operand->getValueAsDef("Type"); 1720 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1721 return UpdateNodeType(ResNo, getValueTypeByHwMode(R, T.getHwModes()), TP); 1722 } 1723 1724 // PointerLikeRegClass has a type that is determined at runtime. 1725 if (Operand->isSubClassOf("PointerLikeRegClass")) 1726 return UpdateNodeType(ResNo, MVT::iPTR, TP); 1727 1728 // Both RegisterClass and RegisterOperand operands derive their types from a 1729 // register class def. 1730 Record *RC = nullptr; 1731 if (Operand->isSubClassOf("RegisterClass")) 1732 RC = Operand; 1733 else if (Operand->isSubClassOf("RegisterOperand")) 1734 RC = Operand->getValueAsDef("RegClass"); 1735 1736 assert(RC && "Unknown operand type"); 1737 CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo(); 1738 return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP); 1739 } 1740 1741 bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const { 1742 for (unsigned i = 0, e = Types.size(); i != e; ++i) 1743 if (!TP.getInfer().isConcrete(Types[i], true)) 1744 return true; 1745 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1746 if (getChild(i)->ContainsUnresolvedType(TP)) 1747 return true; 1748 return false; 1749 } 1750 1751 bool TreePatternNode::hasProperTypeByHwMode() const { 1752 for (const TypeSetByHwMode &S : Types) 1753 if (!S.isDefaultOnly()) 1754 return true; 1755 for (const TreePatternNodePtr &C : Children) 1756 if (C->hasProperTypeByHwMode()) 1757 return true; 1758 return false; 1759 } 1760 1761 bool TreePatternNode::hasPossibleType() const { 1762 for (const TypeSetByHwMode &S : Types) 1763 if (!S.isPossible()) 1764 return false; 1765 for (const TreePatternNodePtr &C : Children) 1766 if (!C->hasPossibleType()) 1767 return false; 1768 return true; 1769 } 1770 1771 bool TreePatternNode::setDefaultMode(unsigned Mode) { 1772 for (TypeSetByHwMode &S : Types) { 1773 S.makeSimple(Mode); 1774 // Check if the selected mode had a type conflict. 1775 if (S.get(DefaultMode).empty()) 1776 return false; 1777 } 1778 for (const TreePatternNodePtr &C : Children) 1779 if (!C->setDefaultMode(Mode)) 1780 return false; 1781 return true; 1782 } 1783 1784 //===----------------------------------------------------------------------===// 1785 // SDNodeInfo implementation 1786 // 1787 SDNodeInfo::SDNodeInfo(Record *R, const CodeGenHwModes &CGH) : Def(R) { 1788 EnumName = R->getValueAsString("Opcode"); 1789 SDClassName = R->getValueAsString("SDClass"); 1790 Record *TypeProfile = R->getValueAsDef("TypeProfile"); 1791 NumResults = TypeProfile->getValueAsInt("NumResults"); 1792 NumOperands = TypeProfile->getValueAsInt("NumOperands"); 1793 1794 // Parse the properties. 1795 Properties = parseSDPatternOperatorProperties(R); 1796 1797 // Parse the type constraints. 1798 std::vector<Record*> ConstraintList = 1799 TypeProfile->getValueAsListOfDefs("Constraints"); 1800 for (Record *R : ConstraintList) 1801 TypeConstraints.emplace_back(R, CGH); 1802 } 1803 1804 /// getKnownType - If the type constraints on this node imply a fixed type 1805 /// (e.g. all stores return void, etc), then return it as an 1806 /// MVT::SimpleValueType. Otherwise, return EEVT::Other. 1807 MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const { 1808 unsigned NumResults = getNumResults(); 1809 assert(NumResults <= 1 && 1810 "We only work with nodes with zero or one result so far!"); 1811 assert(ResNo == 0 && "Only handles single result nodes so far"); 1812 1813 for (const SDTypeConstraint &Constraint : TypeConstraints) { 1814 // Make sure that this applies to the correct node result. 1815 if (Constraint.OperandNo >= NumResults) // FIXME: need value # 1816 continue; 1817 1818 switch (Constraint.ConstraintType) { 1819 default: break; 1820 case SDTypeConstraint::SDTCisVT: 1821 if (Constraint.VVT.isSimple()) 1822 return Constraint.VVT.getSimple().SimpleTy; 1823 break; 1824 case SDTypeConstraint::SDTCisPtrTy: 1825 return MVT::iPTR; 1826 } 1827 } 1828 return MVT::Other; 1829 } 1830 1831 //===----------------------------------------------------------------------===// 1832 // TreePatternNode implementation 1833 // 1834 1835 static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) { 1836 if (Operator->getName() == "set" || 1837 Operator->getName() == "implicit") 1838 return 0; // All return nothing. 1839 1840 if (Operator->isSubClassOf("Intrinsic")) 1841 return CDP.getIntrinsic(Operator).IS.RetVTs.size(); 1842 1843 if (Operator->isSubClassOf("SDNode")) 1844 return CDP.getSDNodeInfo(Operator).getNumResults(); 1845 1846 if (Operator->isSubClassOf("PatFrags")) { 1847 // If we've already parsed this pattern fragment, get it. Otherwise, handle 1848 // the forward reference case where one pattern fragment references another 1849 // before it is processed. 1850 if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) { 1851 // The number of results of a fragment with alternative records is the 1852 // maximum number of results across all alternatives. 1853 unsigned NumResults = 0; 1854 for (const auto &T : PFRec->getTrees()) 1855 NumResults = std::max(NumResults, T->getNumTypes()); 1856 return NumResults; 1857 } 1858 1859 ListInit *LI = Operator->getValueAsListInit("Fragments"); 1860 assert(LI && "Invalid Fragment"); 1861 unsigned NumResults = 0; 1862 for (Init *I : LI->getValues()) { 1863 Record *Op = nullptr; 1864 if (DagInit *Dag = dyn_cast<DagInit>(I)) 1865 if (DefInit *DI = dyn_cast<DefInit>(Dag->getOperator())) 1866 Op = DI->getDef(); 1867 assert(Op && "Invalid Fragment"); 1868 NumResults = std::max(NumResults, GetNumNodeResults(Op, CDP)); 1869 } 1870 return NumResults; 1871 } 1872 1873 if (Operator->isSubClassOf("Instruction")) { 1874 CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator); 1875 1876 unsigned NumDefsToAdd = InstInfo.Operands.NumDefs; 1877 1878 // Subtract any defaulted outputs. 1879 for (unsigned i = 0; i != InstInfo.Operands.NumDefs; ++i) { 1880 Record *OperandNode = InstInfo.Operands[i].Rec; 1881 1882 if (OperandNode->isSubClassOf("OperandWithDefaultOps") && 1883 !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) 1884 --NumDefsToAdd; 1885 } 1886 1887 // Add on one implicit def if it has a resolvable type. 1888 if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other) 1889 ++NumDefsToAdd; 1890 return NumDefsToAdd; 1891 } 1892 1893 if (Operator->isSubClassOf("SDNodeXForm")) 1894 return 1; // FIXME: Generalize SDNodeXForm 1895 1896 if (Operator->isSubClassOf("ValueType")) 1897 return 1; // A type-cast of one result. 1898 1899 if (Operator->isSubClassOf("ComplexPattern")) 1900 return 1; 1901 1902 errs() << *Operator; 1903 PrintFatalError("Unhandled node in GetNumNodeResults"); 1904 } 1905 1906 void TreePatternNode::print(raw_ostream &OS) const { 1907 if (isLeaf()) 1908 OS << *getLeafValue(); 1909 else 1910 OS << '(' << getOperator()->getName(); 1911 1912 for (unsigned i = 0, e = Types.size(); i != e; ++i) { 1913 OS << ':'; 1914 getExtType(i).writeToStream(OS); 1915 } 1916 1917 if (!isLeaf()) { 1918 if (getNumChildren() != 0) { 1919 OS << " "; 1920 ListSeparator LS; 1921 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1922 OS << LS; 1923 getChild(i)->print(OS); 1924 } 1925 } 1926 OS << ")"; 1927 } 1928 1929 for (const TreePredicateCall &Pred : PredicateCalls) { 1930 OS << "<<P:"; 1931 if (Pred.Scope) 1932 OS << Pred.Scope << ":"; 1933 OS << Pred.Fn.getFnName() << ">>"; 1934 } 1935 if (TransformFn) 1936 OS << "<<X:" << TransformFn->getName() << ">>"; 1937 if (!getName().empty()) 1938 OS << ":$" << getName(); 1939 1940 for (const ScopedName &Name : NamesAsPredicateArg) 1941 OS << ":$pred:" << Name.getScope() << ":" << Name.getIdentifier(); 1942 } 1943 void TreePatternNode::dump() const { 1944 print(errs()); 1945 } 1946 1947 /// isIsomorphicTo - Return true if this node is recursively 1948 /// isomorphic to the specified node. For this comparison, the node's 1949 /// entire state is considered. The assigned name is ignored, since 1950 /// nodes with differing names are considered isomorphic. However, if 1951 /// the assigned name is present in the dependent variable set, then 1952 /// the assigned name is considered significant and the node is 1953 /// isomorphic if the names match. 1954 bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, 1955 const MultipleUseVarSet &DepVars) const { 1956 if (N == this) return true; 1957 if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || 1958 getPredicateCalls() != N->getPredicateCalls() || 1959 getTransformFn() != N->getTransformFn()) 1960 return false; 1961 1962 if (isLeaf()) { 1963 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 1964 if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) { 1965 return ((DI->getDef() == NDI->getDef()) 1966 && (DepVars.find(getName()) == DepVars.end() 1967 || getName() == N->getName())); 1968 } 1969 } 1970 return getLeafValue() == N->getLeafValue(); 1971 } 1972 1973 if (N->getOperator() != getOperator() || 1974 N->getNumChildren() != getNumChildren()) return false; 1975 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1976 if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars)) 1977 return false; 1978 return true; 1979 } 1980 1981 /// clone - Make a copy of this tree and all of its children. 1982 /// 1983 TreePatternNodePtr TreePatternNode::clone() const { 1984 TreePatternNodePtr New; 1985 if (isLeaf()) { 1986 New = std::make_shared<TreePatternNode>(getLeafValue(), getNumTypes()); 1987 } else { 1988 std::vector<TreePatternNodePtr> CChildren; 1989 CChildren.reserve(Children.size()); 1990 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1991 CChildren.push_back(getChild(i)->clone()); 1992 New = std::make_shared<TreePatternNode>(getOperator(), std::move(CChildren), 1993 getNumTypes()); 1994 } 1995 New->setName(getName()); 1996 New->setNamesAsPredicateArg(getNamesAsPredicateArg()); 1997 New->Types = Types; 1998 New->setPredicateCalls(getPredicateCalls()); 1999 New->setTransformFn(getTransformFn()); 2000 return New; 2001 } 2002 2003 /// RemoveAllTypes - Recursively strip all the types of this tree. 2004 void TreePatternNode::RemoveAllTypes() { 2005 // Reset to unknown type. 2006 std::fill(Types.begin(), Types.end(), TypeSetByHwMode()); 2007 if (isLeaf()) return; 2008 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 2009 getChild(i)->RemoveAllTypes(); 2010 } 2011 2012 2013 /// SubstituteFormalArguments - Replace the formal arguments in this tree 2014 /// with actual values specified by ArgMap. 2015 void TreePatternNode::SubstituteFormalArguments( 2016 std::map<std::string, TreePatternNodePtr> &ArgMap) { 2017 if (isLeaf()) return; 2018 2019 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 2020 TreePatternNode *Child = getChild(i); 2021 if (Child->isLeaf()) { 2022 Init *Val = Child->getLeafValue(); 2023 // Note that, when substituting into an output pattern, Val might be an 2024 // UnsetInit. 2025 if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) && 2026 cast<DefInit>(Val)->getDef()->getName() == "node")) { 2027 // We found a use of a formal argument, replace it with its value. 2028 TreePatternNodePtr NewChild = ArgMap[Child->getName()]; 2029 assert(NewChild && "Couldn't find formal argument!"); 2030 assert((Child->getPredicateCalls().empty() || 2031 NewChild->getPredicateCalls() == Child->getPredicateCalls()) && 2032 "Non-empty child predicate clobbered!"); 2033 setChild(i, std::move(NewChild)); 2034 } 2035 } else { 2036 getChild(i)->SubstituteFormalArguments(ArgMap); 2037 } 2038 } 2039 } 2040 2041 2042 /// InlinePatternFragments - If this pattern refers to any pattern 2043 /// fragments, return the set of inlined versions (this can be more than 2044 /// one if a PatFrags record has multiple alternatives). 2045 void TreePatternNode::InlinePatternFragments( 2046 TreePatternNodePtr T, TreePattern &TP, 2047 std::vector<TreePatternNodePtr> &OutAlternatives) { 2048 2049 if (TP.hasError()) 2050 return; 2051 2052 if (isLeaf()) { 2053 OutAlternatives.push_back(T); // nothing to do. 2054 return; 2055 } 2056 2057 Record *Op = getOperator(); 2058 2059 if (!Op->isSubClassOf("PatFrags")) { 2060 if (getNumChildren() == 0) { 2061 OutAlternatives.push_back(T); 2062 return; 2063 } 2064 2065 // Recursively inline children nodes. 2066 std::vector<std::vector<TreePatternNodePtr> > ChildAlternatives; 2067 ChildAlternatives.resize(getNumChildren()); 2068 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 2069 TreePatternNodePtr Child = getChildShared(i); 2070 Child->InlinePatternFragments(Child, TP, ChildAlternatives[i]); 2071 // If there are no alternatives for any child, there are no 2072 // alternatives for this expression as whole. 2073 if (ChildAlternatives[i].empty()) 2074 return; 2075 2076 assert((Child->getPredicateCalls().empty() || 2077 llvm::all_of(ChildAlternatives[i], 2078 [&](const TreePatternNodePtr &NewChild) { 2079 return NewChild->getPredicateCalls() == 2080 Child->getPredicateCalls(); 2081 })) && 2082 "Non-empty child predicate clobbered!"); 2083 } 2084 2085 // The end result is an all-pairs construction of the resultant pattern. 2086 std::vector<unsigned> Idxs; 2087 Idxs.resize(ChildAlternatives.size()); 2088 bool NotDone; 2089 do { 2090 // Create the variant and add it to the output list. 2091 std::vector<TreePatternNodePtr> NewChildren; 2092 for (unsigned i = 0, e = ChildAlternatives.size(); i != e; ++i) 2093 NewChildren.push_back(ChildAlternatives[i][Idxs[i]]); 2094 TreePatternNodePtr R = std::make_shared<TreePatternNode>( 2095 getOperator(), std::move(NewChildren), getNumTypes()); 2096 2097 // Copy over properties. 2098 R->setName(getName()); 2099 R->setNamesAsPredicateArg(getNamesAsPredicateArg()); 2100 R->setPredicateCalls(getPredicateCalls()); 2101 R->setTransformFn(getTransformFn()); 2102 for (unsigned i = 0, e = getNumTypes(); i != e; ++i) 2103 R->setType(i, getExtType(i)); 2104 for (unsigned i = 0, e = getNumResults(); i != e; ++i) 2105 R->setResultIndex(i, getResultIndex(i)); 2106 2107 // Register alternative. 2108 OutAlternatives.push_back(R); 2109 2110 // Increment indices to the next permutation by incrementing the 2111 // indices from last index backward, e.g., generate the sequence 2112 // [0, 0], [0, 1], [1, 0], [1, 1]. 2113 int IdxsIdx; 2114 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) { 2115 if (++Idxs[IdxsIdx] == ChildAlternatives[IdxsIdx].size()) 2116 Idxs[IdxsIdx] = 0; 2117 else 2118 break; 2119 } 2120 NotDone = (IdxsIdx >= 0); 2121 } while (NotDone); 2122 2123 return; 2124 } 2125 2126 // Otherwise, we found a reference to a fragment. First, look up its 2127 // TreePattern record. 2128 TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); 2129 2130 // Verify that we are passing the right number of operands. 2131 if (Frag->getNumArgs() != Children.size()) { 2132 TP.error("'" + Op->getName() + "' fragment requires " + 2133 Twine(Frag->getNumArgs()) + " operands!"); 2134 return; 2135 } 2136 2137 TreePredicateFn PredFn(Frag); 2138 unsigned Scope = 0; 2139 if (TreePredicateFn(Frag).usesOperands()) 2140 Scope = TP.getDAGPatterns().allocateScope(); 2141 2142 // Compute the map of formal to actual arguments. 2143 std::map<std::string, TreePatternNodePtr> ArgMap; 2144 for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) { 2145 TreePatternNodePtr Child = getChildShared(i); 2146 if (Scope != 0) { 2147 Child = Child->clone(); 2148 Child->addNameAsPredicateArg(ScopedName(Scope, Frag->getArgName(i))); 2149 } 2150 ArgMap[Frag->getArgName(i)] = Child; 2151 } 2152 2153 // Loop over all fragment alternatives. 2154 for (const auto &Alternative : Frag->getTrees()) { 2155 TreePatternNodePtr FragTree = Alternative->clone(); 2156 2157 if (!PredFn.isAlwaysTrue()) 2158 FragTree->addPredicateCall(PredFn, Scope); 2159 2160 // Resolve formal arguments to their actual value. 2161 if (Frag->getNumArgs()) 2162 FragTree->SubstituteFormalArguments(ArgMap); 2163 2164 // Transfer types. Note that the resolved alternative may have fewer 2165 // (but not more) results than the PatFrags node. 2166 FragTree->setName(getName()); 2167 for (unsigned i = 0, e = FragTree->getNumTypes(); i != e; ++i) 2168 FragTree->UpdateNodeType(i, getExtType(i), TP); 2169 2170 // Transfer in the old predicates. 2171 for (const TreePredicateCall &Pred : getPredicateCalls()) 2172 FragTree->addPredicateCall(Pred); 2173 2174 // The fragment we inlined could have recursive inlining that is needed. See 2175 // if there are any pattern fragments in it and inline them as needed. 2176 FragTree->InlinePatternFragments(FragTree, TP, OutAlternatives); 2177 } 2178 } 2179 2180 /// getImplicitType - Check to see if the specified record has an implicit 2181 /// type which should be applied to it. This will infer the type of register 2182 /// references from the register file information, for example. 2183 /// 2184 /// When Unnamed is set, return the type of a DAG operand with no name, such as 2185 /// the F8RC register class argument in: 2186 /// 2187 /// (COPY_TO_REGCLASS GPR:$src, F8RC) 2188 /// 2189 /// When Unnamed is false, return the type of a named DAG operand such as the 2190 /// GPR:$src operand above. 2191 /// 2192 static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo, 2193 bool NotRegisters, 2194 bool Unnamed, 2195 TreePattern &TP) { 2196 CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); 2197 2198 // Check to see if this is a register operand. 2199 if (R->isSubClassOf("RegisterOperand")) { 2200 assert(ResNo == 0 && "Regoperand ref only has one result!"); 2201 if (NotRegisters) 2202 return TypeSetByHwMode(); // Unknown. 2203 Record *RegClass = R->getValueAsDef("RegClass"); 2204 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 2205 return TypeSetByHwMode(T.getRegisterClass(RegClass).getValueTypes()); 2206 } 2207 2208 // Check to see if this is a register or a register class. 2209 if (R->isSubClassOf("RegisterClass")) { 2210 assert(ResNo == 0 && "Regclass ref only has one result!"); 2211 // An unnamed register class represents itself as an i32 immediate, for 2212 // example on a COPY_TO_REGCLASS instruction. 2213 if (Unnamed) 2214 return TypeSetByHwMode(MVT::i32); 2215 2216 // In a named operand, the register class provides the possible set of 2217 // types. 2218 if (NotRegisters) 2219 return TypeSetByHwMode(); // Unknown. 2220 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 2221 return TypeSetByHwMode(T.getRegisterClass(R).getValueTypes()); 2222 } 2223 2224 if (R->isSubClassOf("PatFrags")) { 2225 assert(ResNo == 0 && "FIXME: PatFrag with multiple results?"); 2226 // Pattern fragment types will be resolved when they are inlined. 2227 return TypeSetByHwMode(); // Unknown. 2228 } 2229 2230 if (R->isSubClassOf("Register")) { 2231 assert(ResNo == 0 && "Registers only produce one result!"); 2232 if (NotRegisters) 2233 return TypeSetByHwMode(); // Unknown. 2234 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 2235 return TypeSetByHwMode(T.getRegisterVTs(R)); 2236 } 2237 2238 if (R->isSubClassOf("SubRegIndex")) { 2239 assert(ResNo == 0 && "SubRegisterIndices only produce one result!"); 2240 return TypeSetByHwMode(MVT::i32); 2241 } 2242 2243 if (R->isSubClassOf("ValueType")) { 2244 assert(ResNo == 0 && "This node only has one result!"); 2245 // An unnamed VTSDNode represents itself as an MVT::Other immediate. 2246 // 2247 // (sext_inreg GPR:$src, i16) 2248 // ~~~ 2249 if (Unnamed) 2250 return TypeSetByHwMode(MVT::Other); 2251 // With a name, the ValueType simply provides the type of the named 2252 // variable. 2253 // 2254 // (sext_inreg i32:$src, i16) 2255 // ~~~~~~~~ 2256 if (NotRegisters) 2257 return TypeSetByHwMode(); // Unknown. 2258 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes(); 2259 return TypeSetByHwMode(getValueTypeByHwMode(R, CGH)); 2260 } 2261 2262 if (R->isSubClassOf("CondCode")) { 2263 assert(ResNo == 0 && "This node only has one result!"); 2264 // Using a CondCodeSDNode. 2265 return TypeSetByHwMode(MVT::Other); 2266 } 2267 2268 if (R->isSubClassOf("ComplexPattern")) { 2269 assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?"); 2270 if (NotRegisters) 2271 return TypeSetByHwMode(); // Unknown. 2272 Record *T = CDP.getComplexPattern(R).getValueType(); 2273 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes(); 2274 return TypeSetByHwMode(getValueTypeByHwMode(T, CGH)); 2275 } 2276 if (R->isSubClassOf("PointerLikeRegClass")) { 2277 assert(ResNo == 0 && "Regclass can only have one result!"); 2278 TypeSetByHwMode VTS(MVT::iPTR); 2279 TP.getInfer().expandOverloads(VTS); 2280 return VTS; 2281 } 2282 2283 if (R->getName() == "node" || R->getName() == "srcvalue" || 2284 R->getName() == "zero_reg" || R->getName() == "immAllOnesV" || 2285 R->getName() == "immAllZerosV" || R->getName() == "undef_tied_input") { 2286 // Placeholder. 2287 return TypeSetByHwMode(); // Unknown. 2288 } 2289 2290 if (R->isSubClassOf("Operand")) { 2291 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes(); 2292 Record *T = R->getValueAsDef("Type"); 2293 return TypeSetByHwMode(getValueTypeByHwMode(T, CGH)); 2294 } 2295 2296 TP.error("Unknown node flavor used in pattern: " + R->getName()); 2297 return TypeSetByHwMode(MVT::Other); 2298 } 2299 2300 2301 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the 2302 /// CodeGenIntrinsic information for it, otherwise return a null pointer. 2303 const CodeGenIntrinsic *TreePatternNode:: 2304 getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { 2305 if (getOperator() != CDP.get_intrinsic_void_sdnode() && 2306 getOperator() != CDP.get_intrinsic_w_chain_sdnode() && 2307 getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) 2308 return nullptr; 2309 2310 unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue(); 2311 return &CDP.getIntrinsicInfo(IID); 2312 } 2313 2314 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern, 2315 /// return the ComplexPattern information, otherwise return null. 2316 const ComplexPattern * 2317 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const { 2318 Record *Rec; 2319 if (isLeaf()) { 2320 DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 2321 if (!DI) 2322 return nullptr; 2323 Rec = DI->getDef(); 2324 } else 2325 Rec = getOperator(); 2326 2327 if (!Rec->isSubClassOf("ComplexPattern")) 2328 return nullptr; 2329 return &CGP.getComplexPattern(Rec); 2330 } 2331 2332 unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const { 2333 // A ComplexPattern specifically declares how many results it fills in. 2334 if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) 2335 return CP->getNumOperands(); 2336 2337 // If MIOperandInfo is specified, that gives the count. 2338 if (isLeaf()) { 2339 DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 2340 if (DI && DI->getDef()->isSubClassOf("Operand")) { 2341 DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo"); 2342 if (MIOps->getNumArgs()) 2343 return MIOps->getNumArgs(); 2344 } 2345 } 2346 2347 // Otherwise there is just one result. 2348 return 1; 2349 } 2350 2351 /// NodeHasProperty - Return true if this node has the specified property. 2352 bool TreePatternNode::NodeHasProperty(SDNP Property, 2353 const CodeGenDAGPatterns &CGP) const { 2354 if (isLeaf()) { 2355 if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) 2356 return CP->hasProperty(Property); 2357 2358 return false; 2359 } 2360 2361 if (Property != SDNPHasChain) { 2362 // The chain proprety is already present on the different intrinsic node 2363 // types (intrinsic_w_chain, intrinsic_void), and is not explicitly listed 2364 // on the intrinsic. Anything else is specific to the individual intrinsic. 2365 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CGP)) 2366 return Int->hasProperty(Property); 2367 } 2368 2369 if (!Operator->isSubClassOf("SDPatternOperator")) 2370 return false; 2371 2372 return CGP.getSDNodeInfo(Operator).hasProperty(Property); 2373 } 2374 2375 2376 2377 2378 /// TreeHasProperty - Return true if any node in this tree has the specified 2379 /// property. 2380 bool TreePatternNode::TreeHasProperty(SDNP Property, 2381 const CodeGenDAGPatterns &CGP) const { 2382 if (NodeHasProperty(Property, CGP)) 2383 return true; 2384 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 2385 if (getChild(i)->TreeHasProperty(Property, CGP)) 2386 return true; 2387 return false; 2388 } 2389 2390 /// isCommutativeIntrinsic - Return true if the node corresponds to a 2391 /// commutative intrinsic. 2392 bool 2393 TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const { 2394 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) 2395 return Int->isCommutative; 2396 return false; 2397 } 2398 2399 static bool isOperandClass(const TreePatternNode *N, StringRef Class) { 2400 if (!N->isLeaf()) 2401 return N->getOperator()->isSubClassOf(Class); 2402 2403 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue()); 2404 if (DI && DI->getDef()->isSubClassOf(Class)) 2405 return true; 2406 2407 return false; 2408 } 2409 2410 static void emitTooManyOperandsError(TreePattern &TP, 2411 StringRef InstName, 2412 unsigned Expected, 2413 unsigned Actual) { 2414 TP.error("Instruction '" + InstName + "' was provided " + Twine(Actual) + 2415 " operands but expected only " + Twine(Expected) + "!"); 2416 } 2417 2418 static void emitTooFewOperandsError(TreePattern &TP, 2419 StringRef InstName, 2420 unsigned Actual) { 2421 TP.error("Instruction '" + InstName + 2422 "' expects more than the provided " + Twine(Actual) + " operands!"); 2423 } 2424 2425 /// ApplyTypeConstraints - Apply all of the type constraints relevant to 2426 /// this node and its children in the tree. This returns true if it makes a 2427 /// change, false otherwise. If a type contradiction is found, flag an error. 2428 bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { 2429 if (TP.hasError()) 2430 return false; 2431 2432 CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); 2433 if (isLeaf()) { 2434 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 2435 // If it's a regclass or something else known, include the type. 2436 bool MadeChange = false; 2437 for (unsigned i = 0, e = Types.size(); i != e; ++i) 2438 MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i, 2439 NotRegisters, 2440 !hasName(), TP), TP); 2441 return MadeChange; 2442 } 2443 2444 if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) { 2445 assert(Types.size() == 1 && "Invalid IntInit"); 2446 2447 // Int inits are always integers. :) 2448 bool MadeChange = TP.getInfer().EnforceInteger(Types[0]); 2449 2450 if (!TP.getInfer().isConcrete(Types[0], false)) 2451 return MadeChange; 2452 2453 ValueTypeByHwMode VVT = TP.getInfer().getConcrete(Types[0], false); 2454 for (auto &P : VVT) { 2455 MVT::SimpleValueType VT = P.second.SimpleTy; 2456 if (VT == MVT::iPTR || VT == MVT::iPTRAny) 2457 continue; 2458 unsigned Size = MVT(VT).getFixedSizeInBits(); 2459 // Make sure that the value is representable for this type. 2460 if (Size >= 32) 2461 continue; 2462 // Check that the value doesn't use more bits than we have. It must 2463 // either be a sign- or zero-extended equivalent of the original. 2464 int64_t SignBitAndAbove = II->getValue() >> (Size - 1); 2465 if (SignBitAndAbove == -1 || SignBitAndAbove == 0 || 2466 SignBitAndAbove == 1) 2467 continue; 2468 2469 TP.error("Integer value '" + Twine(II->getValue()) + 2470 "' is out of range for type '" + getEnumName(VT) + "'!"); 2471 break; 2472 } 2473 return MadeChange; 2474 } 2475 2476 return false; 2477 } 2478 2479 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) { 2480 bool MadeChange = false; 2481 2482 // Apply the result type to the node. 2483 unsigned NumRetVTs = Int->IS.RetVTs.size(); 2484 unsigned NumParamVTs = Int->IS.ParamVTs.size(); 2485 2486 for (unsigned i = 0, e = NumRetVTs; i != e; ++i) 2487 MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP); 2488 2489 if (getNumChildren() != NumParamVTs + 1) { 2490 TP.error("Intrinsic '" + Int->Name + "' expects " + Twine(NumParamVTs) + 2491 " operands, not " + Twine(getNumChildren() - 1) + " operands!"); 2492 return false; 2493 } 2494 2495 // Apply type info to the intrinsic ID. 2496 MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP); 2497 2498 for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) { 2499 MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters); 2500 2501 MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i]; 2502 assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case"); 2503 MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP); 2504 } 2505 return MadeChange; 2506 } 2507 2508 if (getOperator()->isSubClassOf("SDNode")) { 2509 const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator()); 2510 2511 // Check that the number of operands is sane. Negative operands -> varargs. 2512 if (NI.getNumOperands() >= 0 && 2513 getNumChildren() != (unsigned)NI.getNumOperands()) { 2514 TP.error(getOperator()->getName() + " node requires exactly " + 2515 Twine(NI.getNumOperands()) + " operands!"); 2516 return false; 2517 } 2518 2519 bool MadeChange = false; 2520 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 2521 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 2522 MadeChange |= NI.ApplyTypeConstraints(this, TP); 2523 return MadeChange; 2524 } 2525 2526 if (getOperator()->isSubClassOf("Instruction")) { 2527 const DAGInstruction &Inst = CDP.getInstruction(getOperator()); 2528 CodeGenInstruction &InstInfo = 2529 CDP.getTargetInfo().getInstruction(getOperator()); 2530 2531 bool MadeChange = false; 2532 2533 // Apply the result types to the node, these come from the things in the 2534 // (outs) list of the instruction. 2535 unsigned NumResultsToAdd = std::min(InstInfo.Operands.NumDefs, 2536 Inst.getNumResults()); 2537 for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo) 2538 MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP); 2539 2540 // If the instruction has implicit defs, we apply the first one as a result. 2541 // FIXME: This sucks, it should apply all implicit defs. 2542 if (!InstInfo.ImplicitDefs.empty()) { 2543 unsigned ResNo = NumResultsToAdd; 2544 2545 // FIXME: Generalize to multiple possible types and multiple possible 2546 // ImplicitDefs. 2547 MVT::SimpleValueType VT = 2548 InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()); 2549 2550 if (VT != MVT::Other) 2551 MadeChange |= UpdateNodeType(ResNo, VT, TP); 2552 } 2553 2554 // If this is an INSERT_SUBREG, constrain the source and destination VTs to 2555 // be the same. 2556 if (getOperator()->getName() == "INSERT_SUBREG") { 2557 assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled"); 2558 MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP); 2559 MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP); 2560 } else if (getOperator()->getName() == "REG_SEQUENCE") { 2561 // We need to do extra, custom typechecking for REG_SEQUENCE since it is 2562 // variadic. 2563 2564 unsigned NChild = getNumChildren(); 2565 if (NChild < 3) { 2566 TP.error("REG_SEQUENCE requires at least 3 operands!"); 2567 return false; 2568 } 2569 2570 if (NChild % 2 == 0) { 2571 TP.error("REG_SEQUENCE requires an odd number of operands!"); 2572 return false; 2573 } 2574 2575 if (!isOperandClass(getChild(0), "RegisterClass")) { 2576 TP.error("REG_SEQUENCE requires a RegisterClass for first operand!"); 2577 return false; 2578 } 2579 2580 for (unsigned I = 1; I < NChild; I += 2) { 2581 TreePatternNode *SubIdxChild = getChild(I + 1); 2582 if (!isOperandClass(SubIdxChild, "SubRegIndex")) { 2583 TP.error("REG_SEQUENCE requires a SubRegIndex for operand " + 2584 Twine(I + 1) + "!"); 2585 return false; 2586 } 2587 } 2588 } 2589 2590 unsigned NumResults = Inst.getNumResults(); 2591 unsigned NumFixedOperands = InstInfo.Operands.size(); 2592 2593 // If one or more operands with a default value appear at the end of the 2594 // formal operand list for an instruction, we allow them to be overridden 2595 // by optional operands provided in the pattern. 2596 // 2597 // But if an operand B without a default appears at any point after an 2598 // operand A with a default, then we don't allow A to be overridden, 2599 // because there would be no way to specify whether the next operand in 2600 // the pattern was intended to override A or skip it. 2601 unsigned NonOverridableOperands = NumFixedOperands; 2602 while (NonOverridableOperands > NumResults && 2603 CDP.operandHasDefault(InstInfo.Operands[NonOverridableOperands-1].Rec)) 2604 --NonOverridableOperands; 2605 2606 unsigned ChildNo = 0; 2607 assert(NumResults <= NumFixedOperands); 2608 for (unsigned i = NumResults, e = NumFixedOperands; i != e; ++i) { 2609 Record *OperandNode = InstInfo.Operands[i].Rec; 2610 2611 // If the operand has a default value, do we use it? We must use the 2612 // default if we've run out of children of the pattern DAG to consume, 2613 // or if the operand is followed by a non-defaulted one. 2614 if (CDP.operandHasDefault(OperandNode) && 2615 (i < NonOverridableOperands || ChildNo >= getNumChildren())) 2616 continue; 2617 2618 // If we have run out of child nodes and there _isn't_ a default 2619 // value we can use for the next operand, give an error. 2620 if (ChildNo >= getNumChildren()) { 2621 emitTooFewOperandsError(TP, getOperator()->getName(), getNumChildren()); 2622 return false; 2623 } 2624 2625 TreePatternNode *Child = getChild(ChildNo++); 2626 unsigned ChildResNo = 0; // Instructions always use res #0 of their op. 2627 2628 // If the operand has sub-operands, they may be provided by distinct 2629 // child patterns, so attempt to match each sub-operand separately. 2630 if (OperandNode->isSubClassOf("Operand")) { 2631 DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo"); 2632 if (unsigned NumArgs = MIOpInfo->getNumArgs()) { 2633 // But don't do that if the whole operand is being provided by 2634 // a single ComplexPattern-related Operand. 2635 2636 if (Child->getNumMIResults(CDP) < NumArgs) { 2637 // Match first sub-operand against the child we already have. 2638 Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef(); 2639 MadeChange |= 2640 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); 2641 2642 // And the remaining sub-operands against subsequent children. 2643 for (unsigned Arg = 1; Arg < NumArgs; ++Arg) { 2644 if (ChildNo >= getNumChildren()) { 2645 emitTooFewOperandsError(TP, getOperator()->getName(), 2646 getNumChildren()); 2647 return false; 2648 } 2649 Child = getChild(ChildNo++); 2650 2651 SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef(); 2652 MadeChange |= 2653 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); 2654 } 2655 continue; 2656 } 2657 } 2658 } 2659 2660 // If we didn't match by pieces above, attempt to match the whole 2661 // operand now. 2662 MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP); 2663 } 2664 2665 if (!InstInfo.Operands.isVariadic && ChildNo != getNumChildren()) { 2666 emitTooManyOperandsError(TP, getOperator()->getName(), 2667 ChildNo, getNumChildren()); 2668 return false; 2669 } 2670 2671 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 2672 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 2673 return MadeChange; 2674 } 2675 2676 if (getOperator()->isSubClassOf("ComplexPattern")) { 2677 bool MadeChange = false; 2678 2679 if (!NotRegisters) { 2680 assert(Types.size() == 1 && "ComplexPatterns only produce one result!"); 2681 Record *T = CDP.getComplexPattern(getOperator()).getValueType(); 2682 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes(); 2683 const ValueTypeByHwMode VVT = getValueTypeByHwMode(T, CGH); 2684 // TODO: AArch64 and AMDGPU use ComplexPattern<untyped, ...> and then 2685 // exclusively use those as non-leaf nodes with explicit type casts, so 2686 // for backwards compatibility we do no inference in that case. This is 2687 // not supported when the ComplexPattern is used as a leaf value, 2688 // however; this inconsistency should be resolved, either by adding this 2689 // case there or by altering the backends to not do this (e.g. using Any 2690 // instead may work). 2691 if (!VVT.isSimple() || VVT.getSimple() != MVT::Untyped) 2692 MadeChange |= UpdateNodeType(0, VVT, TP); 2693 } 2694 2695 for (unsigned i = 0; i < getNumChildren(); ++i) 2696 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 2697 2698 return MadeChange; 2699 } 2700 2701 assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); 2702 2703 // Node transforms always take one operand. 2704 if (getNumChildren() != 1) { 2705 TP.error("Node transform '" + getOperator()->getName() + 2706 "' requires one operand!"); 2707 return false; 2708 } 2709 2710 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters); 2711 return MadeChange; 2712 } 2713 2714 /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the 2715 /// RHS of a commutative operation, not the on LHS. 2716 static bool OnlyOnRHSOfCommutative(TreePatternNode *N) { 2717 if (!N->isLeaf() && N->getOperator()->getName() == "imm") 2718 return true; 2719 if (N->isLeaf() && isa<IntInit>(N->getLeafValue())) 2720 return true; 2721 if (isImmAllOnesAllZerosMatch(N)) 2722 return true; 2723 return false; 2724 } 2725 2726 2727 /// canPatternMatch - If it is impossible for this pattern to match on this 2728 /// target, fill in Reason and return false. Otherwise, return true. This is 2729 /// used as a sanity check for .td files (to prevent people from writing stuff 2730 /// that can never possibly work), and to prevent the pattern permuter from 2731 /// generating stuff that is useless. 2732 bool TreePatternNode::canPatternMatch(std::string &Reason, 2733 const CodeGenDAGPatterns &CDP) { 2734 if (isLeaf()) return true; 2735 2736 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 2737 if (!getChild(i)->canPatternMatch(Reason, CDP)) 2738 return false; 2739 2740 // If this is an intrinsic, handle cases that would make it not match. For 2741 // example, if an operand is required to be an immediate. 2742 if (getOperator()->isSubClassOf("Intrinsic")) { 2743 // TODO: 2744 return true; 2745 } 2746 2747 if (getOperator()->isSubClassOf("ComplexPattern")) 2748 return true; 2749 2750 // If this node is a commutative operator, check that the LHS isn't an 2751 // immediate. 2752 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); 2753 bool isCommIntrinsic = isCommutativeIntrinsic(CDP); 2754 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { 2755 // Scan all of the operands of the node and make sure that only the last one 2756 // is a constant node, unless the RHS also is. 2757 if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { 2758 unsigned Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id. 2759 for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i) 2760 if (OnlyOnRHSOfCommutative(getChild(i))) { 2761 Reason="Immediate value must be on the RHS of commutative operators!"; 2762 return false; 2763 } 2764 } 2765 } 2766 2767 return true; 2768 } 2769 2770 //===----------------------------------------------------------------------===// 2771 // TreePattern implementation 2772 // 2773 2774 TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, 2775 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 2776 isInputPattern(isInput), HasError(false), 2777 Infer(*this) { 2778 for (Init *I : RawPat->getValues()) 2779 Trees.push_back(ParseTreePattern(I, "")); 2780 } 2781 2782 TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, 2783 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 2784 isInputPattern(isInput), HasError(false), 2785 Infer(*this) { 2786 Trees.push_back(ParseTreePattern(Pat, "")); 2787 } 2788 2789 TreePattern::TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput, 2790 CodeGenDAGPatterns &cdp) 2791 : TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false), 2792 Infer(*this) { 2793 Trees.push_back(Pat); 2794 } 2795 2796 void TreePattern::error(const Twine &Msg) { 2797 if (HasError) 2798 return; 2799 dump(); 2800 PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg); 2801 HasError = true; 2802 } 2803 2804 void TreePattern::ComputeNamedNodes() { 2805 for (TreePatternNodePtr &Tree : Trees) 2806 ComputeNamedNodes(Tree.get()); 2807 } 2808 2809 void TreePattern::ComputeNamedNodes(TreePatternNode *N) { 2810 if (!N->getName().empty()) 2811 NamedNodes[N->getName()].push_back(N); 2812 2813 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 2814 ComputeNamedNodes(N->getChild(i)); 2815 } 2816 2817 TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit, 2818 StringRef OpName) { 2819 RecordKeeper &RK = TheInit->getRecordKeeper(); 2820 if (DefInit *DI = dyn_cast<DefInit>(TheInit)) { 2821 Record *R = DI->getDef(); 2822 2823 // Direct reference to a leaf DagNode or PatFrag? Turn it into a 2824 // TreePatternNode of its own. For example: 2825 /// (foo GPR, imm) -> (foo GPR, (imm)) 2826 if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrags")) 2827 return ParseTreePattern( 2828 DagInit::get(DI, nullptr, 2829 std::vector<std::pair<Init*, StringInit*> >()), 2830 OpName); 2831 2832 // Input argument? 2833 TreePatternNodePtr Res = std::make_shared<TreePatternNode>(DI, 1); 2834 if (R->getName() == "node" && !OpName.empty()) { 2835 if (OpName.empty()) 2836 error("'node' argument requires a name to match with operand list"); 2837 Args.push_back(std::string(OpName)); 2838 } 2839 2840 Res->setName(OpName); 2841 return Res; 2842 } 2843 2844 // ?:$name or just $name. 2845 if (isa<UnsetInit>(TheInit)) { 2846 if (OpName.empty()) 2847 error("'?' argument requires a name to match with operand list"); 2848 TreePatternNodePtr Res = std::make_shared<TreePatternNode>(TheInit, 1); 2849 Args.push_back(std::string(OpName)); 2850 Res->setName(OpName); 2851 return Res; 2852 } 2853 2854 if (isa<IntInit>(TheInit) || isa<BitInit>(TheInit)) { 2855 if (!OpName.empty()) 2856 error("Constant int or bit argument should not have a name!"); 2857 if (isa<BitInit>(TheInit)) 2858 TheInit = TheInit->convertInitializerTo(IntRecTy::get(RK)); 2859 return std::make_shared<TreePatternNode>(TheInit, 1); 2860 } 2861 2862 if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) { 2863 // Turn this into an IntInit. 2864 Init *II = BI->convertInitializerTo(IntRecTy::get(RK)); 2865 if (!II || !isa<IntInit>(II)) 2866 error("Bits value must be constants!"); 2867 return ParseTreePattern(II, OpName); 2868 } 2869 2870 DagInit *Dag = dyn_cast<DagInit>(TheInit); 2871 if (!Dag) { 2872 TheInit->print(errs()); 2873 error("Pattern has unexpected init kind!"); 2874 } 2875 DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator()); 2876 if (!OpDef) error("Pattern has unexpected operator type!"); 2877 Record *Operator = OpDef->getDef(); 2878 2879 if (Operator->isSubClassOf("ValueType")) { 2880 // If the operator is a ValueType, then this must be "type cast" of a leaf 2881 // node. 2882 if (Dag->getNumArgs() != 1) 2883 error("Type cast only takes one operand!"); 2884 2885 TreePatternNodePtr New = 2886 ParseTreePattern(Dag->getArg(0), Dag->getArgNameStr(0)); 2887 2888 // Apply the type cast. 2889 if (New->getNumTypes() != 1) 2890 error("Type cast can only have one type!"); 2891 const CodeGenHwModes &CGH = getDAGPatterns().getTargetInfo().getHwModes(); 2892 New->UpdateNodeType(0, getValueTypeByHwMode(Operator, CGH), *this); 2893 2894 if (!OpName.empty()) 2895 error("ValueType cast should not have a name!"); 2896 return New; 2897 } 2898 2899 // Verify that this is something that makes sense for an operator. 2900 if (!Operator->isSubClassOf("PatFrags") && 2901 !Operator->isSubClassOf("SDNode") && 2902 !Operator->isSubClassOf("Instruction") && 2903 !Operator->isSubClassOf("SDNodeXForm") && 2904 !Operator->isSubClassOf("Intrinsic") && 2905 !Operator->isSubClassOf("ComplexPattern") && 2906 Operator->getName() != "set" && 2907 Operator->getName() != "implicit") 2908 error("Unrecognized node '" + Operator->getName() + "'!"); 2909 2910 // Check to see if this is something that is illegal in an input pattern. 2911 if (isInputPattern) { 2912 if (Operator->isSubClassOf("Instruction") || 2913 Operator->isSubClassOf("SDNodeXForm")) 2914 error("Cannot use '" + Operator->getName() + "' in an input pattern!"); 2915 } else { 2916 if (Operator->isSubClassOf("Intrinsic")) 2917 error("Cannot use '" + Operator->getName() + "' in an output pattern!"); 2918 2919 if (Operator->isSubClassOf("SDNode") && 2920 Operator->getName() != "imm" && 2921 Operator->getName() != "timm" && 2922 Operator->getName() != "fpimm" && 2923 Operator->getName() != "tglobaltlsaddr" && 2924 Operator->getName() != "tconstpool" && 2925 Operator->getName() != "tjumptable" && 2926 Operator->getName() != "tframeindex" && 2927 Operator->getName() != "texternalsym" && 2928 Operator->getName() != "tblockaddress" && 2929 Operator->getName() != "tglobaladdr" && 2930 Operator->getName() != "bb" && 2931 Operator->getName() != "vt" && 2932 Operator->getName() != "mcsym") 2933 error("Cannot use '" + Operator->getName() + "' in an output pattern!"); 2934 } 2935 2936 std::vector<TreePatternNodePtr> Children; 2937 2938 // Parse all the operands. 2939 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) 2940 Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgNameStr(i))); 2941 2942 // Get the actual number of results before Operator is converted to an intrinsic 2943 // node (which is hard-coded to have either zero or one result). 2944 unsigned NumResults = GetNumNodeResults(Operator, CDP); 2945 2946 // If the operator is an intrinsic, then this is just syntactic sugar for 2947 // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and 2948 // convert the intrinsic name to a number. 2949 if (Operator->isSubClassOf("Intrinsic")) { 2950 const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator); 2951 unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1; 2952 2953 // If this intrinsic returns void, it must have side-effects and thus a 2954 // chain. 2955 if (Int.IS.RetVTs.empty()) 2956 Operator = getDAGPatterns().get_intrinsic_void_sdnode(); 2957 else if (Int.ModRef != CodeGenIntrinsic::NoMem || Int.hasSideEffects) 2958 // Has side-effects, requires chain. 2959 Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); 2960 else // Otherwise, no chain. 2961 Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); 2962 2963 Children.insert(Children.begin(), std::make_shared<TreePatternNode>( 2964 IntInit::get(RK, IID), 1)); 2965 } 2966 2967 if (Operator->isSubClassOf("ComplexPattern")) { 2968 for (unsigned i = 0; i < Children.size(); ++i) { 2969 TreePatternNodePtr Child = Children[i]; 2970 2971 if (Child->getName().empty()) 2972 error("All arguments to a ComplexPattern must be named"); 2973 2974 // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)" 2975 // and "(MY_PAT $b, $a)" should not be allowed in the same pattern; 2976 // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)". 2977 auto OperandId = std::make_pair(Operator, i); 2978 auto PrevOp = ComplexPatternOperands.find(Child->getName()); 2979 if (PrevOp != ComplexPatternOperands.end()) { 2980 if (PrevOp->getValue() != OperandId) 2981 error("All ComplexPattern operands must appear consistently: " 2982 "in the same order in just one ComplexPattern instance."); 2983 } else 2984 ComplexPatternOperands[Child->getName()] = OperandId; 2985 } 2986 } 2987 2988 TreePatternNodePtr Result = 2989 std::make_shared<TreePatternNode>(Operator, std::move(Children), 2990 NumResults); 2991 Result->setName(OpName); 2992 2993 if (Dag->getName()) { 2994 assert(Result->getName().empty()); 2995 Result->setName(Dag->getNameStr()); 2996 } 2997 return Result; 2998 } 2999 3000 /// SimplifyTree - See if we can simplify this tree to eliminate something that 3001 /// will never match in favor of something obvious that will. This is here 3002 /// strictly as a convenience to target authors because it allows them to write 3003 /// more type generic things and have useless type casts fold away. 3004 /// 3005 /// This returns true if any change is made. 3006 static bool SimplifyTree(TreePatternNodePtr &N) { 3007 if (N->isLeaf()) 3008 return false; 3009 3010 // If we have a bitconvert with a resolved type and if the source and 3011 // destination types are the same, then the bitconvert is useless, remove it. 3012 // 3013 // We make an exception if the types are completely empty. This can come up 3014 // when the pattern being simplified is in the Fragments list of a PatFrags, 3015 // so that the operand is just an untyped "node". In that situation we leave 3016 // bitconverts unsimplified, and simplify them later once the fragment is 3017 // expanded into its true context. 3018 if (N->getOperator()->getName() == "bitconvert" && 3019 N->getExtType(0).isValueTypeByHwMode(false) && 3020 !N->getExtType(0).empty() && 3021 N->getExtType(0) == N->getChild(0)->getExtType(0) && 3022 N->getName().empty()) { 3023 N = N->getChildShared(0); 3024 SimplifyTree(N); 3025 return true; 3026 } 3027 3028 // Walk all children. 3029 bool MadeChange = false; 3030 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { 3031 TreePatternNodePtr Child = N->getChildShared(i); 3032 MadeChange |= SimplifyTree(Child); 3033 N->setChild(i, std::move(Child)); 3034 } 3035 return MadeChange; 3036 } 3037 3038 3039 3040 /// InferAllTypes - Infer/propagate as many types throughout the expression 3041 /// patterns as possible. Return true if all types are inferred, false 3042 /// otherwise. Flags an error if a type contradiction is found. 3043 bool TreePattern:: 3044 InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) { 3045 if (NamedNodes.empty()) 3046 ComputeNamedNodes(); 3047 3048 bool MadeChange = true; 3049 while (MadeChange) { 3050 MadeChange = false; 3051 for (TreePatternNodePtr &Tree : Trees) { 3052 MadeChange |= Tree->ApplyTypeConstraints(*this, false); 3053 MadeChange |= SimplifyTree(Tree); 3054 } 3055 3056 // If there are constraints on our named nodes, apply them. 3057 for (auto &Entry : NamedNodes) { 3058 SmallVectorImpl<TreePatternNode*> &Nodes = Entry.second; 3059 3060 // If we have input named node types, propagate their types to the named 3061 // values here. 3062 if (InNamedTypes) { 3063 if (!InNamedTypes->count(Entry.getKey())) { 3064 error("Node '" + std::string(Entry.getKey()) + 3065 "' in output pattern but not input pattern"); 3066 return true; 3067 } 3068 3069 const SmallVectorImpl<TreePatternNode*> &InNodes = 3070 InNamedTypes->find(Entry.getKey())->second; 3071 3072 // The input types should be fully resolved by now. 3073 for (TreePatternNode *Node : Nodes) { 3074 // If this node is a register class, and it is the root of the pattern 3075 // then we're mapping something onto an input register. We allow 3076 // changing the type of the input register in this case. This allows 3077 // us to match things like: 3078 // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>; 3079 if (Node == Trees[0].get() && Node->isLeaf()) { 3080 DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue()); 3081 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || 3082 DI->getDef()->isSubClassOf("RegisterOperand"))) 3083 continue; 3084 } 3085 3086 assert(Node->getNumTypes() == 1 && 3087 InNodes[0]->getNumTypes() == 1 && 3088 "FIXME: cannot name multiple result nodes yet"); 3089 MadeChange |= Node->UpdateNodeType(0, InNodes[0]->getExtType(0), 3090 *this); 3091 } 3092 } 3093 3094 // If there are multiple nodes with the same name, they must all have the 3095 // same type. 3096 if (Entry.second.size() > 1) { 3097 for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) { 3098 TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1]; 3099 assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 && 3100 "FIXME: cannot name multiple result nodes yet"); 3101 3102 MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this); 3103 MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this); 3104 } 3105 } 3106 } 3107 } 3108 3109 bool HasUnresolvedTypes = false; 3110 for (const TreePatternNodePtr &Tree : Trees) 3111 HasUnresolvedTypes |= Tree->ContainsUnresolvedType(*this); 3112 return !HasUnresolvedTypes; 3113 } 3114 3115 void TreePattern::print(raw_ostream &OS) const { 3116 OS << getRecord()->getName(); 3117 if (!Args.empty()) { 3118 OS << "("; 3119 ListSeparator LS; 3120 for (const std::string &Arg : Args) 3121 OS << LS << Arg; 3122 OS << ")"; 3123 } 3124 OS << ": "; 3125 3126 if (Trees.size() > 1) 3127 OS << "[\n"; 3128 for (const TreePatternNodePtr &Tree : Trees) { 3129 OS << "\t"; 3130 Tree->print(OS); 3131 OS << "\n"; 3132 } 3133 3134 if (Trees.size() > 1) 3135 OS << "]\n"; 3136 } 3137 3138 void TreePattern::dump() const { print(errs()); } 3139 3140 //===----------------------------------------------------------------------===// 3141 // CodeGenDAGPatterns implementation 3142 // 3143 3144 CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R, 3145 PatternRewriterFn PatternRewriter) 3146 : Records(R), Target(R), LegalVTS(Target.getLegalValueTypes()), 3147 PatternRewriter(PatternRewriter) { 3148 3149 Intrinsics = CodeGenIntrinsicTable(Records); 3150 ParseNodeInfo(); 3151 ParseNodeTransforms(); 3152 ParseComplexPatterns(); 3153 ParsePatternFragments(); 3154 ParseDefaultOperands(); 3155 ParseInstructions(); 3156 ParsePatternFragments(/*OutFrags*/true); 3157 ParsePatterns(); 3158 3159 // Generate variants. For example, commutative patterns can match 3160 // multiple ways. Add them to PatternsToMatch as well. 3161 GenerateVariants(); 3162 3163 // Break patterns with parameterized types into a series of patterns, 3164 // where each one has a fixed type and is predicated on the conditions 3165 // of the associated HW mode. 3166 ExpandHwModeBasedTypes(); 3167 3168 // Infer instruction flags. For example, we can detect loads, 3169 // stores, and side effects in many cases by examining an 3170 // instruction's pattern. 3171 InferInstructionFlags(); 3172 3173 // Verify that instruction flags match the patterns. 3174 VerifyInstructionFlags(); 3175 } 3176 3177 Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const { 3178 Record *N = Records.getDef(Name); 3179 if (!N || !N->isSubClassOf("SDNode")) 3180 PrintFatalError("Error getting SDNode '" + Name + "'!"); 3181 3182 return N; 3183 } 3184 3185 // Parse all of the SDNode definitions for the target, populating SDNodes. 3186 void CodeGenDAGPatterns::ParseNodeInfo() { 3187 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); 3188 const CodeGenHwModes &CGH = getTargetInfo().getHwModes(); 3189 3190 while (!Nodes.empty()) { 3191 Record *R = Nodes.back(); 3192 SDNodes.insert(std::make_pair(R, SDNodeInfo(R, CGH))); 3193 Nodes.pop_back(); 3194 } 3195 3196 // Get the builtin intrinsic nodes. 3197 intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); 3198 intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); 3199 intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); 3200 } 3201 3202 /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms 3203 /// map, and emit them to the file as functions. 3204 void CodeGenDAGPatterns::ParseNodeTransforms() { 3205 std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); 3206 while (!Xforms.empty()) { 3207 Record *XFormNode = Xforms.back(); 3208 Record *SDNode = XFormNode->getValueAsDef("Opcode"); 3209 StringRef Code = XFormNode->getValueAsString("XFormFunction"); 3210 SDNodeXForms.insert( 3211 std::make_pair(XFormNode, NodeXForm(SDNode, std::string(Code)))); 3212 3213 Xforms.pop_back(); 3214 } 3215 } 3216 3217 void CodeGenDAGPatterns::ParseComplexPatterns() { 3218 std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); 3219 while (!AMs.empty()) { 3220 ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); 3221 AMs.pop_back(); 3222 } 3223 } 3224 3225 3226 /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td 3227 /// file, building up the PatternFragments map. After we've collected them all, 3228 /// inline fragments together as necessary, so that there are no references left 3229 /// inside a pattern fragment to a pattern fragment. 3230 /// 3231 void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) { 3232 std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrags"); 3233 3234 // First step, parse all of the fragments. 3235 for (Record *Frag : Fragments) { 3236 if (OutFrags != Frag->isSubClassOf("OutPatFrag")) 3237 continue; 3238 3239 ListInit *LI = Frag->getValueAsListInit("Fragments"); 3240 TreePattern *P = 3241 (PatternFragments[Frag] = std::make_unique<TreePattern>( 3242 Frag, LI, !Frag->isSubClassOf("OutPatFrag"), 3243 *this)).get(); 3244 3245 // Validate the argument list, converting it to set, to discard duplicates. 3246 std::vector<std::string> &Args = P->getArgList(); 3247 // Copy the args so we can take StringRefs to them. 3248 auto ArgsCopy = Args; 3249 SmallDenseSet<StringRef, 4> OperandsSet; 3250 OperandsSet.insert(ArgsCopy.begin(), ArgsCopy.end()); 3251 3252 if (OperandsSet.count("")) 3253 P->error("Cannot have unnamed 'node' values in pattern fragment!"); 3254 3255 // Parse the operands list. 3256 DagInit *OpsList = Frag->getValueAsDag("Operands"); 3257 DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator()); 3258 // Special cases: ops == outs == ins. Different names are used to 3259 // improve readability. 3260 if (!OpsOp || 3261 (OpsOp->getDef()->getName() != "ops" && 3262 OpsOp->getDef()->getName() != "outs" && 3263 OpsOp->getDef()->getName() != "ins")) 3264 P->error("Operands list should start with '(ops ... '!"); 3265 3266 // Copy over the arguments. 3267 Args.clear(); 3268 for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { 3269 if (!isa<DefInit>(OpsList->getArg(j)) || 3270 cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node") 3271 P->error("Operands list should all be 'node' values."); 3272 if (!OpsList->getArgName(j)) 3273 P->error("Operands list should have names for each operand!"); 3274 StringRef ArgNameStr = OpsList->getArgNameStr(j); 3275 if (!OperandsSet.count(ArgNameStr)) 3276 P->error("'" + ArgNameStr + 3277 "' does not occur in pattern or was multiply specified!"); 3278 OperandsSet.erase(ArgNameStr); 3279 Args.push_back(std::string(ArgNameStr)); 3280 } 3281 3282 if (!OperandsSet.empty()) 3283 P->error("Operands list does not contain an entry for operand '" + 3284 *OperandsSet.begin() + "'!"); 3285 3286 // If there is a node transformation corresponding to this, keep track of 3287 // it. 3288 Record *Transform = Frag->getValueAsDef("OperandTransform"); 3289 if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? 3290 for (const auto &T : P->getTrees()) 3291 T->setTransformFn(Transform); 3292 } 3293 3294 // Now that we've parsed all of the tree fragments, do a closure on them so 3295 // that there are not references to PatFrags left inside of them. 3296 for (Record *Frag : Fragments) { 3297 if (OutFrags != Frag->isSubClassOf("OutPatFrag")) 3298 continue; 3299 3300 TreePattern &ThePat = *PatternFragments[Frag]; 3301 ThePat.InlinePatternFragments(); 3302 3303 // Infer as many types as possible. Don't worry about it if we don't infer 3304 // all of them, some may depend on the inputs of the pattern. Also, don't 3305 // validate type sets; validation may cause spurious failures e.g. if a 3306 // fragment needs floating-point types but the current target does not have 3307 // any (this is only an error if that fragment is ever used!). 3308 { 3309 TypeInfer::SuppressValidation SV(ThePat.getInfer()); 3310 ThePat.InferAllTypes(); 3311 ThePat.resetError(); 3312 } 3313 3314 // If debugging, print out the pattern fragment result. 3315 LLVM_DEBUG(ThePat.dump()); 3316 } 3317 } 3318 3319 void CodeGenDAGPatterns::ParseDefaultOperands() { 3320 std::vector<Record*> DefaultOps; 3321 DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps"); 3322 3323 // Find some SDNode. 3324 assert(!SDNodes.empty() && "No SDNodes parsed?"); 3325 Init *SomeSDNode = DefInit::get(SDNodes.begin()->first); 3326 3327 for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) { 3328 DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps"); 3329 3330 // Clone the DefaultInfo dag node, changing the operator from 'ops' to 3331 // SomeSDnode so that we can parse this. 3332 std::vector<std::pair<Init*, StringInit*> > Ops; 3333 for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) 3334 Ops.push_back(std::make_pair(DefaultInfo->getArg(op), 3335 DefaultInfo->getArgName(op))); 3336 DagInit *DI = DagInit::get(SomeSDNode, nullptr, Ops); 3337 3338 // Create a TreePattern to parse this. 3339 TreePattern P(DefaultOps[i], DI, false, *this); 3340 assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!"); 3341 3342 // Copy the operands over into a DAGDefaultOperand. 3343 DAGDefaultOperand DefaultOpInfo; 3344 3345 const TreePatternNodePtr &T = P.getTree(0); 3346 for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) { 3347 TreePatternNodePtr TPN = T->getChildShared(op); 3348 while (TPN->ApplyTypeConstraints(P, false)) 3349 /* Resolve all types */; 3350 3351 if (TPN->ContainsUnresolvedType(P)) { 3352 PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" + 3353 DefaultOps[i]->getName() + 3354 "' doesn't have a concrete type!"); 3355 } 3356 DefaultOpInfo.DefaultOps.push_back(std::move(TPN)); 3357 } 3358 3359 // Insert it into the DefaultOperands map so we can find it later. 3360 DefaultOperands[DefaultOps[i]] = DefaultOpInfo; 3361 } 3362 } 3363 3364 /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an 3365 /// instruction input. Return true if this is a real use. 3366 static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat, 3367 std::map<std::string, TreePatternNodePtr> &InstInputs) { 3368 // No name -> not interesting. 3369 if (Pat->getName().empty()) { 3370 if (Pat->isLeaf()) { 3371 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); 3372 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || 3373 DI->getDef()->isSubClassOf("RegisterOperand"))) 3374 I.error("Input " + DI->getDef()->getName() + " must be named!"); 3375 } 3376 return false; 3377 } 3378 3379 Record *Rec; 3380 if (Pat->isLeaf()) { 3381 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); 3382 if (!DI) 3383 I.error("Input $" + Pat->getName() + " must be an identifier!"); 3384 Rec = DI->getDef(); 3385 } else { 3386 Rec = Pat->getOperator(); 3387 } 3388 3389 // SRCVALUE nodes are ignored. 3390 if (Rec->getName() == "srcvalue") 3391 return false; 3392 3393 TreePatternNodePtr &Slot = InstInputs[Pat->getName()]; 3394 if (!Slot) { 3395 Slot = Pat; 3396 return true; 3397 } 3398 Record *SlotRec; 3399 if (Slot->isLeaf()) { 3400 SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef(); 3401 } else { 3402 assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); 3403 SlotRec = Slot->getOperator(); 3404 } 3405 3406 // Ensure that the inputs agree if we've already seen this input. 3407 if (Rec != SlotRec) 3408 I.error("All $" + Pat->getName() + " inputs must agree with each other"); 3409 // Ensure that the types can agree as well. 3410 Slot->UpdateNodeType(0, Pat->getExtType(0), I); 3411 Pat->UpdateNodeType(0, Slot->getExtType(0), I); 3412 if (Slot->getExtTypes() != Pat->getExtTypes()) 3413 I.error("All $" + Pat->getName() + " inputs must agree with each other"); 3414 return true; 3415 } 3416 3417 /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is 3418 /// part of "I", the instruction), computing the set of inputs and outputs of 3419 /// the pattern. Report errors if we see anything naughty. 3420 void CodeGenDAGPatterns::FindPatternInputsAndOutputs( 3421 TreePattern &I, TreePatternNodePtr Pat, 3422 std::map<std::string, TreePatternNodePtr> &InstInputs, 3423 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>> 3424 &InstResults, 3425 std::vector<Record *> &InstImpResults) { 3426 3427 // The instruction pattern still has unresolved fragments. For *named* 3428 // nodes we must resolve those here. This may not result in multiple 3429 // alternatives. 3430 if (!Pat->getName().empty()) { 3431 TreePattern SrcPattern(I.getRecord(), Pat, true, *this); 3432 SrcPattern.InlinePatternFragments(); 3433 SrcPattern.InferAllTypes(); 3434 Pat = SrcPattern.getOnlyTree(); 3435 } 3436 3437 if (Pat->isLeaf()) { 3438 bool isUse = HandleUse(I, Pat, InstInputs); 3439 if (!isUse && Pat->getTransformFn()) 3440 I.error("Cannot specify a transform function for a non-input value!"); 3441 return; 3442 } 3443 3444 if (Pat->getOperator()->getName() == "implicit") { 3445 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { 3446 TreePatternNode *Dest = Pat->getChild(i); 3447 if (!Dest->isLeaf()) 3448 I.error("implicitly defined value should be a register!"); 3449 3450 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); 3451 if (!Val || !Val->getDef()->isSubClassOf("Register")) 3452 I.error("implicitly defined value should be a register!"); 3453 InstImpResults.push_back(Val->getDef()); 3454 } 3455 return; 3456 } 3457 3458 if (Pat->getOperator()->getName() != "set") { 3459 // If this is not a set, verify that the children nodes are not void typed, 3460 // and recurse. 3461 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { 3462 if (Pat->getChild(i)->getNumTypes() == 0) 3463 I.error("Cannot have void nodes inside of patterns!"); 3464 FindPatternInputsAndOutputs(I, Pat->getChildShared(i), InstInputs, 3465 InstResults, InstImpResults); 3466 } 3467 3468 // If this is a non-leaf node with no children, treat it basically as if 3469 // it were a leaf. This handles nodes like (imm). 3470 bool isUse = HandleUse(I, Pat, InstInputs); 3471 3472 if (!isUse && Pat->getTransformFn()) 3473 I.error("Cannot specify a transform function for a non-input value!"); 3474 return; 3475 } 3476 3477 // Otherwise, this is a set, validate and collect instruction results. 3478 if (Pat->getNumChildren() == 0) 3479 I.error("set requires operands!"); 3480 3481 if (Pat->getTransformFn()) 3482 I.error("Cannot specify a transform function on a set node!"); 3483 3484 // Check the set destinations. 3485 unsigned NumDests = Pat->getNumChildren()-1; 3486 for (unsigned i = 0; i != NumDests; ++i) { 3487 TreePatternNodePtr Dest = Pat->getChildShared(i); 3488 // For set destinations we also must resolve fragments here. 3489 TreePattern DestPattern(I.getRecord(), Dest, false, *this); 3490 DestPattern.InlinePatternFragments(); 3491 DestPattern.InferAllTypes(); 3492 Dest = DestPattern.getOnlyTree(); 3493 3494 if (!Dest->isLeaf()) 3495 I.error("set destination should be a register!"); 3496 3497 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); 3498 if (!Val) { 3499 I.error("set destination should be a register!"); 3500 continue; 3501 } 3502 3503 if (Val->getDef()->isSubClassOf("RegisterClass") || 3504 Val->getDef()->isSubClassOf("ValueType") || 3505 Val->getDef()->isSubClassOf("RegisterOperand") || 3506 Val->getDef()->isSubClassOf("PointerLikeRegClass")) { 3507 if (Dest->getName().empty()) 3508 I.error("set destination must have a name!"); 3509 if (InstResults.count(Dest->getName())) 3510 I.error("cannot set '" + Dest->getName() + "' multiple times"); 3511 InstResults[Dest->getName()] = Dest; 3512 } else if (Val->getDef()->isSubClassOf("Register")) { 3513 InstImpResults.push_back(Val->getDef()); 3514 } else { 3515 I.error("set destination should be a register!"); 3516 } 3517 } 3518 3519 // Verify and collect info from the computation. 3520 FindPatternInputsAndOutputs(I, Pat->getChildShared(NumDests), InstInputs, 3521 InstResults, InstImpResults); 3522 } 3523 3524 //===----------------------------------------------------------------------===// 3525 // Instruction Analysis 3526 //===----------------------------------------------------------------------===// 3527 3528 class InstAnalyzer { 3529 const CodeGenDAGPatterns &CDP; 3530 public: 3531 bool hasSideEffects; 3532 bool mayStore; 3533 bool mayLoad; 3534 bool isBitcast; 3535 bool isVariadic; 3536 bool hasChain; 3537 3538 InstAnalyzer(const CodeGenDAGPatterns &cdp) 3539 : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false), 3540 isBitcast(false), isVariadic(false), hasChain(false) {} 3541 3542 void Analyze(const PatternToMatch &Pat) { 3543 const TreePatternNode *N = Pat.getSrcPattern(); 3544 AnalyzeNode(N); 3545 // These properties are detected only on the root node. 3546 isBitcast = IsNodeBitcast(N); 3547 } 3548 3549 private: 3550 bool IsNodeBitcast(const TreePatternNode *N) const { 3551 if (hasSideEffects || mayLoad || mayStore || isVariadic) 3552 return false; 3553 3554 if (N->isLeaf()) 3555 return false; 3556 if (N->getNumChildren() != 1 || !N->getChild(0)->isLeaf()) 3557 return false; 3558 3559 if (N->getOperator()->isSubClassOf("ComplexPattern")) 3560 return false; 3561 3562 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator()); 3563 if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1) 3564 return false; 3565 return OpInfo.getEnumName() == "ISD::BITCAST"; 3566 } 3567 3568 public: 3569 void AnalyzeNode(const TreePatternNode *N) { 3570 if (N->isLeaf()) { 3571 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) { 3572 Record *LeafRec = DI->getDef(); 3573 // Handle ComplexPattern leaves. 3574 if (LeafRec->isSubClassOf("ComplexPattern")) { 3575 const ComplexPattern &CP = CDP.getComplexPattern(LeafRec); 3576 if (CP.hasProperty(SDNPMayStore)) mayStore = true; 3577 if (CP.hasProperty(SDNPMayLoad)) mayLoad = true; 3578 if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true; 3579 } 3580 } 3581 return; 3582 } 3583 3584 // Analyze children. 3585 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 3586 AnalyzeNode(N->getChild(i)); 3587 3588 // Notice properties of the node. 3589 if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true; 3590 if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true; 3591 if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true; 3592 if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true; 3593 if (N->NodeHasProperty(SDNPHasChain, CDP)) hasChain = true; 3594 3595 if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) { 3596 // If this is an intrinsic, analyze it. 3597 if (IntInfo->ModRef & CodeGenIntrinsic::MR_Ref) 3598 mayLoad = true;// These may load memory. 3599 3600 if (IntInfo->ModRef & CodeGenIntrinsic::MR_Mod) 3601 mayStore = true;// Intrinsics that can write to memory are 'mayStore'. 3602 3603 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem || 3604 IntInfo->hasSideEffects) 3605 // ReadWriteMem intrinsics can have other strange effects. 3606 hasSideEffects = true; 3607 } 3608 } 3609 3610 }; 3611 3612 static bool InferFromPattern(CodeGenInstruction &InstInfo, 3613 const InstAnalyzer &PatInfo, 3614 Record *PatDef) { 3615 bool Error = false; 3616 3617 // Remember where InstInfo got its flags. 3618 if (InstInfo.hasUndefFlags()) 3619 InstInfo.InferredFrom = PatDef; 3620 3621 // Check explicitly set flags for consistency. 3622 if (InstInfo.hasSideEffects != PatInfo.hasSideEffects && 3623 !InstInfo.hasSideEffects_Unset) { 3624 // Allow explicitly setting hasSideEffects = 1 on instructions, even when 3625 // the pattern has no side effects. That could be useful for div/rem 3626 // instructions that may trap. 3627 if (!InstInfo.hasSideEffects) { 3628 Error = true; 3629 PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " + 3630 Twine(InstInfo.hasSideEffects)); 3631 } 3632 } 3633 3634 if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) { 3635 Error = true; 3636 PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " + 3637 Twine(InstInfo.mayStore)); 3638 } 3639 3640 if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) { 3641 // Allow explicitly setting mayLoad = 1, even when the pattern has no loads. 3642 // Some targets translate immediates to loads. 3643 if (!InstInfo.mayLoad) { 3644 Error = true; 3645 PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " + 3646 Twine(InstInfo.mayLoad)); 3647 } 3648 } 3649 3650 // Transfer inferred flags. 3651 InstInfo.hasSideEffects |= PatInfo.hasSideEffects; 3652 InstInfo.mayStore |= PatInfo.mayStore; 3653 InstInfo.mayLoad |= PatInfo.mayLoad; 3654 3655 // These flags are silently added without any verification. 3656 // FIXME: To match historical behavior of TableGen, for now add those flags 3657 // only when we're inferring from the primary instruction pattern. 3658 if (PatDef->isSubClassOf("Instruction")) { 3659 InstInfo.isBitcast |= PatInfo.isBitcast; 3660 InstInfo.hasChain |= PatInfo.hasChain; 3661 InstInfo.hasChain_Inferred = true; 3662 } 3663 3664 // Don't infer isVariadic. This flag means something different on SDNodes and 3665 // instructions. For example, a CALL SDNode is variadic because it has the 3666 // call arguments as operands, but a CALL instruction is not variadic - it 3667 // has argument registers as implicit, not explicit uses. 3668 3669 return Error; 3670 } 3671 3672 /// hasNullFragReference - Return true if the DAG has any reference to the 3673 /// null_frag operator. 3674 static bool hasNullFragReference(DagInit *DI) { 3675 DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator()); 3676 if (!OpDef) return false; 3677 Record *Operator = OpDef->getDef(); 3678 3679 // If this is the null fragment, return true. 3680 if (Operator->getName() == "null_frag") return true; 3681 // If any of the arguments reference the null fragment, return true. 3682 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) { 3683 if (auto Arg = dyn_cast<DefInit>(DI->getArg(i))) 3684 if (Arg->getDef()->getName() == "null_frag") 3685 return true; 3686 DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i)); 3687 if (Arg && hasNullFragReference(Arg)) 3688 return true; 3689 } 3690 3691 return false; 3692 } 3693 3694 /// hasNullFragReference - Return true if any DAG in the list references 3695 /// the null_frag operator. 3696 static bool hasNullFragReference(ListInit *LI) { 3697 for (Init *I : LI->getValues()) { 3698 DagInit *DI = dyn_cast<DagInit>(I); 3699 assert(DI && "non-dag in an instruction Pattern list?!"); 3700 if (hasNullFragReference(DI)) 3701 return true; 3702 } 3703 return false; 3704 } 3705 3706 /// Get all the instructions in a tree. 3707 static void 3708 getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) { 3709 if (Tree->isLeaf()) 3710 return; 3711 if (Tree->getOperator()->isSubClassOf("Instruction")) 3712 Instrs.push_back(Tree->getOperator()); 3713 for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i) 3714 getInstructionsInTree(Tree->getChild(i), Instrs); 3715 } 3716 3717 /// Check the class of a pattern leaf node against the instruction operand it 3718 /// represents. 3719 static bool checkOperandClass(CGIOperandList::OperandInfo &OI, 3720 Record *Leaf) { 3721 if (OI.Rec == Leaf) 3722 return true; 3723 3724 // Allow direct value types to be used in instruction set patterns. 3725 // The type will be checked later. 3726 if (Leaf->isSubClassOf("ValueType")) 3727 return true; 3728 3729 // Patterns can also be ComplexPattern instances. 3730 if (Leaf->isSubClassOf("ComplexPattern")) 3731 return true; 3732 3733 return false; 3734 } 3735 3736 void CodeGenDAGPatterns::parseInstructionPattern( 3737 CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) { 3738 3739 assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!"); 3740 3741 // Parse the instruction. 3742 TreePattern I(CGI.TheDef, Pat, true, *this); 3743 3744 // InstInputs - Keep track of all of the inputs of the instruction, along 3745 // with the record they are declared as. 3746 std::map<std::string, TreePatternNodePtr> InstInputs; 3747 3748 // InstResults - Keep track of all the virtual registers that are 'set' 3749 // in the instruction, including what reg class they are. 3750 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>> 3751 InstResults; 3752 3753 std::vector<Record*> InstImpResults; 3754 3755 // Verify that the top-level forms in the instruction are of void type, and 3756 // fill in the InstResults map. 3757 SmallString<32> TypesString; 3758 for (unsigned j = 0, e = I.getNumTrees(); j != e; ++j) { 3759 TypesString.clear(); 3760 TreePatternNodePtr Pat = I.getTree(j); 3761 if (Pat->getNumTypes() != 0) { 3762 raw_svector_ostream OS(TypesString); 3763 ListSeparator LS; 3764 for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) { 3765 OS << LS; 3766 Pat->getExtType(k).writeToStream(OS); 3767 } 3768 I.error("Top-level forms in instruction pattern should have" 3769 " void types, has types " + 3770 OS.str()); 3771 } 3772 3773 // Find inputs and outputs, and verify the structure of the uses/defs. 3774 FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, 3775 InstImpResults); 3776 } 3777 3778 // Now that we have inputs and outputs of the pattern, inspect the operands 3779 // list for the instruction. This determines the order that operands are 3780 // added to the machine instruction the node corresponds to. 3781 unsigned NumResults = InstResults.size(); 3782 3783 // Parse the operands list from the (ops) list, validating it. 3784 assert(I.getArgList().empty() && "Args list should still be empty here!"); 3785 3786 // Check that all of the results occur first in the list. 3787 std::vector<Record*> Results; 3788 std::vector<unsigned> ResultIndices; 3789 SmallVector<TreePatternNodePtr, 2> ResNodes; 3790 for (unsigned i = 0; i != NumResults; ++i) { 3791 if (i == CGI.Operands.size()) { 3792 const std::string &OpName = 3793 llvm::find_if( 3794 InstResults, 3795 [](const std::pair<std::string, TreePatternNodePtr> &P) { 3796 return P.second; 3797 }) 3798 ->first; 3799 3800 I.error("'" + OpName + "' set but does not appear in operand list!"); 3801 } 3802 3803 const std::string &OpName = CGI.Operands[i].Name; 3804 3805 // Check that it exists in InstResults. 3806 auto InstResultIter = InstResults.find(OpName); 3807 if (InstResultIter == InstResults.end() || !InstResultIter->second) 3808 I.error("Operand $" + OpName + " does not exist in operand list!"); 3809 3810 TreePatternNodePtr RNode = InstResultIter->second; 3811 Record *R = cast<DefInit>(RNode->getLeafValue())->getDef(); 3812 ResNodes.push_back(std::move(RNode)); 3813 if (!R) 3814 I.error("Operand $" + OpName + " should be a set destination: all " 3815 "outputs must occur before inputs in operand list!"); 3816 3817 if (!checkOperandClass(CGI.Operands[i], R)) 3818 I.error("Operand $" + OpName + " class mismatch!"); 3819 3820 // Remember the return type. 3821 Results.push_back(CGI.Operands[i].Rec); 3822 3823 // Remember the result index. 3824 ResultIndices.push_back(std::distance(InstResults.begin(), InstResultIter)); 3825 3826 // Okay, this one checks out. 3827 InstResultIter->second = nullptr; 3828 } 3829 3830 // Loop over the inputs next. 3831 std::vector<TreePatternNodePtr> ResultNodeOperands; 3832 std::vector<Record*> Operands; 3833 for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) { 3834 CGIOperandList::OperandInfo &Op = CGI.Operands[i]; 3835 const std::string &OpName = Op.Name; 3836 if (OpName.empty()) 3837 I.error("Operand #" + Twine(i) + " in operands list has no name!"); 3838 3839 if (!InstInputs.count(OpName)) { 3840 // If this is an operand with a DefaultOps set filled in, we can ignore 3841 // this. When we codegen it, we will do so as always executed. 3842 if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) { 3843 // Does it have a non-empty DefaultOps field? If so, ignore this 3844 // operand. 3845 if (!getDefaultOperand(Op.Rec).DefaultOps.empty()) 3846 continue; 3847 } 3848 I.error("Operand $" + OpName + 3849 " does not appear in the instruction pattern"); 3850 } 3851 TreePatternNodePtr InVal = InstInputs[OpName]; 3852 InstInputs.erase(OpName); // It occurred, remove from map. 3853 3854 if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) { 3855 Record *InRec = cast<DefInit>(InVal->getLeafValue())->getDef(); 3856 if (!checkOperandClass(Op, InRec)) 3857 I.error("Operand $" + OpName + "'s register class disagrees" 3858 " between the operand and pattern"); 3859 } 3860 Operands.push_back(Op.Rec); 3861 3862 // Construct the result for the dest-pattern operand list. 3863 TreePatternNodePtr OpNode = InVal->clone(); 3864 3865 // No predicate is useful on the result. 3866 OpNode->clearPredicateCalls(); 3867 3868 // Promote the xform function to be an explicit node if set. 3869 if (Record *Xform = OpNode->getTransformFn()) { 3870 OpNode->setTransformFn(nullptr); 3871 std::vector<TreePatternNodePtr> Children; 3872 Children.push_back(OpNode); 3873 OpNode = std::make_shared<TreePatternNode>(Xform, std::move(Children), 3874 OpNode->getNumTypes()); 3875 } 3876 3877 ResultNodeOperands.push_back(std::move(OpNode)); 3878 } 3879 3880 if (!InstInputs.empty()) 3881 I.error("Input operand $" + InstInputs.begin()->first + 3882 " occurs in pattern but not in operands list!"); 3883 3884 TreePatternNodePtr ResultPattern = std::make_shared<TreePatternNode>( 3885 I.getRecord(), std::move(ResultNodeOperands), 3886 GetNumNodeResults(I.getRecord(), *this)); 3887 // Copy fully inferred output node types to instruction result pattern. 3888 for (unsigned i = 0; i != NumResults; ++i) { 3889 assert(ResNodes[i]->getNumTypes() == 1 && "FIXME: Unhandled"); 3890 ResultPattern->setType(i, ResNodes[i]->getExtType(0)); 3891 ResultPattern->setResultIndex(i, ResultIndices[i]); 3892 } 3893 3894 // FIXME: Assume only the first tree is the pattern. The others are clobber 3895 // nodes. 3896 TreePatternNodePtr Pattern = I.getTree(0); 3897 TreePatternNodePtr SrcPattern; 3898 if (Pattern->getOperator()->getName() == "set") { 3899 SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone(); 3900 } else{ 3901 // Not a set (store or something?) 3902 SrcPattern = Pattern; 3903 } 3904 3905 // Create and insert the instruction. 3906 // FIXME: InstImpResults should not be part of DAGInstruction. 3907 Record *R = I.getRecord(); 3908 DAGInsts.emplace(std::piecewise_construct, std::forward_as_tuple(R), 3909 std::forward_as_tuple(Results, Operands, InstImpResults, 3910 SrcPattern, ResultPattern)); 3911 3912 LLVM_DEBUG(I.dump()); 3913 } 3914 3915 /// ParseInstructions - Parse all of the instructions, inlining and resolving 3916 /// any fragments involved. This populates the Instructions list with fully 3917 /// resolved instructions. 3918 void CodeGenDAGPatterns::ParseInstructions() { 3919 std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); 3920 3921 for (Record *Instr : Instrs) { 3922 ListInit *LI = nullptr; 3923 3924 if (isa<ListInit>(Instr->getValueInit("Pattern"))) 3925 LI = Instr->getValueAsListInit("Pattern"); 3926 3927 // If there is no pattern, only collect minimal information about the 3928 // instruction for its operand list. We have to assume that there is one 3929 // result, as we have no detailed info. A pattern which references the 3930 // null_frag operator is as-if no pattern were specified. Normally this 3931 // is from a multiclass expansion w/ a SDPatternOperator passed in as 3932 // null_frag. 3933 if (!LI || LI->empty() || hasNullFragReference(LI)) { 3934 std::vector<Record*> Results; 3935 std::vector<Record*> Operands; 3936 3937 CodeGenInstruction &InstInfo = Target.getInstruction(Instr); 3938 3939 if (InstInfo.Operands.size() != 0) { 3940 for (unsigned j = 0, e = InstInfo.Operands.NumDefs; j < e; ++j) 3941 Results.push_back(InstInfo.Operands[j].Rec); 3942 3943 // The rest are inputs. 3944 for (unsigned j = InstInfo.Operands.NumDefs, 3945 e = InstInfo.Operands.size(); j < e; ++j) 3946 Operands.push_back(InstInfo.Operands[j].Rec); 3947 } 3948 3949 // Create and insert the instruction. 3950 std::vector<Record*> ImpResults; 3951 Instructions.insert(std::make_pair(Instr, 3952 DAGInstruction(Results, Operands, ImpResults))); 3953 continue; // no pattern. 3954 } 3955 3956 CodeGenInstruction &CGI = Target.getInstruction(Instr); 3957 parseInstructionPattern(CGI, LI, Instructions); 3958 } 3959 3960 // If we can, convert the instructions to be patterns that are matched! 3961 for (auto &Entry : Instructions) { 3962 Record *Instr = Entry.first; 3963 DAGInstruction &TheInst = Entry.second; 3964 TreePatternNodePtr SrcPattern = TheInst.getSrcPattern(); 3965 TreePatternNodePtr ResultPattern = TheInst.getResultPattern(); 3966 3967 if (SrcPattern && ResultPattern) { 3968 TreePattern Pattern(Instr, SrcPattern, true, *this); 3969 TreePattern Result(Instr, ResultPattern, false, *this); 3970 ParseOnePattern(Instr, Pattern, Result, TheInst.getImpResults()); 3971 } 3972 } 3973 } 3974 3975 typedef std::pair<TreePatternNode *, unsigned> NameRecord; 3976 3977 static void FindNames(TreePatternNode *P, 3978 std::map<std::string, NameRecord> &Names, 3979 TreePattern *PatternTop) { 3980 if (!P->getName().empty()) { 3981 NameRecord &Rec = Names[P->getName()]; 3982 // If this is the first instance of the name, remember the node. 3983 if (Rec.second++ == 0) 3984 Rec.first = P; 3985 else if (Rec.first->getExtTypes() != P->getExtTypes()) 3986 PatternTop->error("repetition of value: $" + P->getName() + 3987 " where different uses have different types!"); 3988 } 3989 3990 if (!P->isLeaf()) { 3991 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) 3992 FindNames(P->getChild(i), Names, PatternTop); 3993 } 3994 } 3995 3996 void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern, 3997 PatternToMatch &&PTM) { 3998 // Do some sanity checking on the pattern we're about to match. 3999 std::string Reason; 4000 if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) { 4001 PrintWarning(Pattern->getRecord()->getLoc(), 4002 Twine("Pattern can never match: ") + Reason); 4003 return; 4004 } 4005 4006 // If the source pattern's root is a complex pattern, that complex pattern 4007 // must specify the nodes it can potentially match. 4008 if (const ComplexPattern *CP = 4009 PTM.getSrcPattern()->getComplexPatternInfo(*this)) 4010 if (CP->getRootNodes().empty()) 4011 Pattern->error("ComplexPattern at root must specify list of opcodes it" 4012 " could match"); 4013 4014 4015 // Find all of the named values in the input and output, ensure they have the 4016 // same type. 4017 std::map<std::string, NameRecord> SrcNames, DstNames; 4018 FindNames(PTM.getSrcPattern(), SrcNames, Pattern); 4019 FindNames(PTM.getDstPattern(), DstNames, Pattern); 4020 4021 // Scan all of the named values in the destination pattern, rejecting them if 4022 // they don't exist in the input pattern. 4023 for (const auto &Entry : DstNames) { 4024 if (SrcNames[Entry.first].first == nullptr) 4025 Pattern->error("Pattern has input without matching name in output: $" + 4026 Entry.first); 4027 } 4028 4029 // Scan all of the named values in the source pattern, rejecting them if the 4030 // name isn't used in the dest, and isn't used to tie two values together. 4031 for (const auto &Entry : SrcNames) 4032 if (DstNames[Entry.first].first == nullptr && 4033 SrcNames[Entry.first].second == 1) 4034 Pattern->error("Pattern has dead named input: $" + Entry.first); 4035 4036 PatternsToMatch.push_back(std::move(PTM)); 4037 } 4038 4039 void CodeGenDAGPatterns::InferInstructionFlags() { 4040 ArrayRef<const CodeGenInstruction*> Instructions = 4041 Target.getInstructionsByEnumValue(); 4042 4043 unsigned Errors = 0; 4044 4045 // Try to infer flags from all patterns in PatternToMatch. These include 4046 // both the primary instruction patterns (which always come first) and 4047 // patterns defined outside the instruction. 4048 for (const PatternToMatch &PTM : ptms()) { 4049 // We can only infer from single-instruction patterns, otherwise we won't 4050 // know which instruction should get the flags. 4051 SmallVector<Record*, 8> PatInstrs; 4052 getInstructionsInTree(PTM.getDstPattern(), PatInstrs); 4053 if (PatInstrs.size() != 1) 4054 continue; 4055 4056 // Get the single instruction. 4057 CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front()); 4058 4059 // Only infer properties from the first pattern. We'll verify the others. 4060 if (InstInfo.InferredFrom) 4061 continue; 4062 4063 InstAnalyzer PatInfo(*this); 4064 PatInfo.Analyze(PTM); 4065 Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord()); 4066 } 4067 4068 if (Errors) 4069 PrintFatalError("pattern conflicts"); 4070 4071 // If requested by the target, guess any undefined properties. 4072 if (Target.guessInstructionProperties()) { 4073 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { 4074 CodeGenInstruction *InstInfo = 4075 const_cast<CodeGenInstruction *>(Instructions[i]); 4076 if (InstInfo->InferredFrom) 4077 continue; 4078 // The mayLoad and mayStore flags default to false. 4079 // Conservatively assume hasSideEffects if it wasn't explicit. 4080 if (InstInfo->hasSideEffects_Unset) 4081 InstInfo->hasSideEffects = true; 4082 } 4083 return; 4084 } 4085 4086 // Complain about any flags that are still undefined. 4087 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { 4088 CodeGenInstruction *InstInfo = 4089 const_cast<CodeGenInstruction *>(Instructions[i]); 4090 if (InstInfo->InferredFrom) 4091 continue; 4092 if (InstInfo->hasSideEffects_Unset) 4093 PrintError(InstInfo->TheDef->getLoc(), 4094 "Can't infer hasSideEffects from patterns"); 4095 if (InstInfo->mayStore_Unset) 4096 PrintError(InstInfo->TheDef->getLoc(), 4097 "Can't infer mayStore from patterns"); 4098 if (InstInfo->mayLoad_Unset) 4099 PrintError(InstInfo->TheDef->getLoc(), 4100 "Can't infer mayLoad from patterns"); 4101 } 4102 } 4103 4104 4105 /// Verify instruction flags against pattern node properties. 4106 void CodeGenDAGPatterns::VerifyInstructionFlags() { 4107 unsigned Errors = 0; 4108 for (const PatternToMatch &PTM : ptms()) { 4109 SmallVector<Record*, 8> Instrs; 4110 getInstructionsInTree(PTM.getDstPattern(), Instrs); 4111 if (Instrs.empty()) 4112 continue; 4113 4114 // Count the number of instructions with each flag set. 4115 unsigned NumSideEffects = 0; 4116 unsigned NumStores = 0; 4117 unsigned NumLoads = 0; 4118 for (const Record *Instr : Instrs) { 4119 const CodeGenInstruction &InstInfo = Target.getInstruction(Instr); 4120 NumSideEffects += InstInfo.hasSideEffects; 4121 NumStores += InstInfo.mayStore; 4122 NumLoads += InstInfo.mayLoad; 4123 } 4124 4125 // Analyze the source pattern. 4126 InstAnalyzer PatInfo(*this); 4127 PatInfo.Analyze(PTM); 4128 4129 // Collect error messages. 4130 SmallVector<std::string, 4> Msgs; 4131 4132 // Check for missing flags in the output. 4133 // Permit extra flags for now at least. 4134 if (PatInfo.hasSideEffects && !NumSideEffects) 4135 Msgs.push_back("pattern has side effects, but hasSideEffects isn't set"); 4136 4137 // Don't verify store flags on instructions with side effects. At least for 4138 // intrinsics, side effects implies mayStore. 4139 if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores) 4140 Msgs.push_back("pattern may store, but mayStore isn't set"); 4141 4142 // Similarly, mayStore implies mayLoad on intrinsics. 4143 if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads) 4144 Msgs.push_back("pattern may load, but mayLoad isn't set"); 4145 4146 // Print error messages. 4147 if (Msgs.empty()) 4148 continue; 4149 ++Errors; 4150 4151 for (const std::string &Msg : Msgs) 4152 PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msg) + " on the " + 4153 (Instrs.size() == 1 ? 4154 "instruction" : "output instructions")); 4155 // Provide the location of the relevant instruction definitions. 4156 for (const Record *Instr : Instrs) { 4157 if (Instr != PTM.getSrcRecord()) 4158 PrintError(Instr->getLoc(), "defined here"); 4159 const CodeGenInstruction &InstInfo = Target.getInstruction(Instr); 4160 if (InstInfo.InferredFrom && 4161 InstInfo.InferredFrom != InstInfo.TheDef && 4162 InstInfo.InferredFrom != PTM.getSrcRecord()) 4163 PrintError(InstInfo.InferredFrom->getLoc(), "inferred from pattern"); 4164 } 4165 } 4166 if (Errors) 4167 PrintFatalError("Errors in DAG patterns"); 4168 } 4169 4170 /// Given a pattern result with an unresolved type, see if we can find one 4171 /// instruction with an unresolved result type. Force this result type to an 4172 /// arbitrary element if it's possible types to converge results. 4173 static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) { 4174 if (N->isLeaf()) 4175 return false; 4176 4177 // Analyze children. 4178 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 4179 if (ForceArbitraryInstResultType(N->getChild(i), TP)) 4180 return true; 4181 4182 if (!N->getOperator()->isSubClassOf("Instruction")) 4183 return false; 4184 4185 // If this type is already concrete or completely unknown we can't do 4186 // anything. 4187 TypeInfer &TI = TP.getInfer(); 4188 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) { 4189 if (N->getExtType(i).empty() || TI.isConcrete(N->getExtType(i), false)) 4190 continue; 4191 4192 // Otherwise, force its type to an arbitrary choice. 4193 if (TI.forceArbitrary(N->getExtType(i))) 4194 return true; 4195 } 4196 4197 return false; 4198 } 4199 4200 // Promote xform function to be an explicit node wherever set. 4201 static TreePatternNodePtr PromoteXForms(TreePatternNodePtr N) { 4202 if (Record *Xform = N->getTransformFn()) { 4203 N->setTransformFn(nullptr); 4204 std::vector<TreePatternNodePtr> Children; 4205 Children.push_back(PromoteXForms(N)); 4206 return std::make_shared<TreePatternNode>(Xform, std::move(Children), 4207 N->getNumTypes()); 4208 } 4209 4210 if (!N->isLeaf()) 4211 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { 4212 TreePatternNodePtr Child = N->getChildShared(i); 4213 N->setChild(i, PromoteXForms(Child)); 4214 } 4215 return N; 4216 } 4217 4218 void CodeGenDAGPatterns::ParseOnePattern(Record *TheDef, 4219 TreePattern &Pattern, TreePattern &Result, 4220 const std::vector<Record *> &InstImpResults) { 4221 4222 // Inline pattern fragments and expand multiple alternatives. 4223 Pattern.InlinePatternFragments(); 4224 Result.InlinePatternFragments(); 4225 4226 if (Result.getNumTrees() != 1) 4227 Result.error("Cannot use multi-alternative fragments in result pattern!"); 4228 4229 // Infer types. 4230 bool IterateInference; 4231 bool InferredAllPatternTypes, InferredAllResultTypes; 4232 do { 4233 // Infer as many types as possible. If we cannot infer all of them, we 4234 // can never do anything with this pattern: report it to the user. 4235 InferredAllPatternTypes = 4236 Pattern.InferAllTypes(&Pattern.getNamedNodesMap()); 4237 4238 // Infer as many types as possible. If we cannot infer all of them, we 4239 // can never do anything with this pattern: report it to the user. 4240 InferredAllResultTypes = 4241 Result.InferAllTypes(&Pattern.getNamedNodesMap()); 4242 4243 IterateInference = false; 4244 4245 // Apply the type of the result to the source pattern. This helps us 4246 // resolve cases where the input type is known to be a pointer type (which 4247 // is considered resolved), but the result knows it needs to be 32- or 4248 // 64-bits. Infer the other way for good measure. 4249 for (const auto &T : Pattern.getTrees()) 4250 for (unsigned i = 0, e = std::min(Result.getOnlyTree()->getNumTypes(), 4251 T->getNumTypes()); 4252 i != e; ++i) { 4253 IterateInference |= T->UpdateNodeType( 4254 i, Result.getOnlyTree()->getExtType(i), Result); 4255 IterateInference |= Result.getOnlyTree()->UpdateNodeType( 4256 i, T->getExtType(i), Result); 4257 } 4258 4259 // If our iteration has converged and the input pattern's types are fully 4260 // resolved but the result pattern is not fully resolved, we may have a 4261 // situation where we have two instructions in the result pattern and 4262 // the instructions require a common register class, but don't care about 4263 // what actual MVT is used. This is actually a bug in our modelling: 4264 // output patterns should have register classes, not MVTs. 4265 // 4266 // In any case, to handle this, we just go through and disambiguate some 4267 // arbitrary types to the result pattern's nodes. 4268 if (!IterateInference && InferredAllPatternTypes && 4269 !InferredAllResultTypes) 4270 IterateInference = 4271 ForceArbitraryInstResultType(Result.getTree(0).get(), Result); 4272 } while (IterateInference); 4273 4274 // Verify that we inferred enough types that we can do something with the 4275 // pattern and result. If these fire the user has to add type casts. 4276 if (!InferredAllPatternTypes) 4277 Pattern.error("Could not infer all types in pattern!"); 4278 if (!InferredAllResultTypes) { 4279 Pattern.dump(); 4280 Result.error("Could not infer all types in pattern result!"); 4281 } 4282 4283 // Promote xform function to be an explicit node wherever set. 4284 TreePatternNodePtr DstShared = PromoteXForms(Result.getOnlyTree()); 4285 4286 TreePattern Temp(Result.getRecord(), DstShared, false, *this); 4287 Temp.InferAllTypes(); 4288 4289 ListInit *Preds = TheDef->getValueAsListInit("Predicates"); 4290 int Complexity = TheDef->getValueAsInt("AddedComplexity"); 4291 4292 if (PatternRewriter) 4293 PatternRewriter(&Pattern); 4294 4295 // A pattern may end up with an "impossible" type, i.e. a situation 4296 // where all types have been eliminated for some node in this pattern. 4297 // This could occur for intrinsics that only make sense for a specific 4298 // value type, and use a specific register class. If, for some mode, 4299 // that register class does not accept that type, the type inference 4300 // will lead to a contradiction, which is not an error however, but 4301 // a sign that this pattern will simply never match. 4302 if (Temp.getOnlyTree()->hasPossibleType()) 4303 for (const auto &T : Pattern.getTrees()) 4304 if (T->hasPossibleType()) 4305 AddPatternToMatch(&Pattern, 4306 PatternToMatch(TheDef, Preds, T, Temp.getOnlyTree(), 4307 InstImpResults, Complexity, 4308 TheDef->getID())); 4309 } 4310 4311 void CodeGenDAGPatterns::ParsePatterns() { 4312 std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); 4313 4314 for (Record *CurPattern : Patterns) { 4315 DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch"); 4316 4317 // If the pattern references the null_frag, there's nothing to do. 4318 if (hasNullFragReference(Tree)) 4319 continue; 4320 4321 TreePattern Pattern(CurPattern, Tree, true, *this); 4322 4323 ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs"); 4324 if (LI->empty()) continue; // no pattern. 4325 4326 // Parse the instruction. 4327 TreePattern Result(CurPattern, LI, false, *this); 4328 4329 if (Result.getNumTrees() != 1) 4330 Result.error("Cannot handle instructions producing instructions " 4331 "with temporaries yet!"); 4332 4333 // Validate that the input pattern is correct. 4334 std::map<std::string, TreePatternNodePtr> InstInputs; 4335 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>> 4336 InstResults; 4337 std::vector<Record*> InstImpResults; 4338 for (unsigned j = 0, ee = Pattern.getNumTrees(); j != ee; ++j) 4339 FindPatternInputsAndOutputs(Pattern, Pattern.getTree(j), InstInputs, 4340 InstResults, InstImpResults); 4341 4342 ParseOnePattern(CurPattern, Pattern, Result, InstImpResults); 4343 } 4344 } 4345 4346 static void collectModes(std::set<unsigned> &Modes, const TreePatternNode *N) { 4347 for (const TypeSetByHwMode &VTS : N->getExtTypes()) 4348 for (const auto &I : VTS) 4349 Modes.insert(I.first); 4350 4351 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 4352 collectModes(Modes, N->getChild(i)); 4353 } 4354 4355 void CodeGenDAGPatterns::ExpandHwModeBasedTypes() { 4356 const CodeGenHwModes &CGH = getTargetInfo().getHwModes(); 4357 std::vector<PatternToMatch> Copy; 4358 PatternsToMatch.swap(Copy); 4359 4360 auto AppendPattern = [this](PatternToMatch &P, unsigned Mode, 4361 StringRef Check) { 4362 TreePatternNodePtr NewSrc = P.getSrcPattern()->clone(); 4363 TreePatternNodePtr NewDst = P.getDstPattern()->clone(); 4364 if (!NewSrc->setDefaultMode(Mode) || !NewDst->setDefaultMode(Mode)) { 4365 return; 4366 } 4367 4368 PatternsToMatch.emplace_back(P.getSrcRecord(), P.getPredicates(), 4369 std::move(NewSrc), std::move(NewDst), 4370 P.getDstRegs(), P.getAddedComplexity(), 4371 Record::getNewUID(Records), Mode, Check); 4372 }; 4373 4374 for (PatternToMatch &P : Copy) { 4375 TreePatternNodePtr SrcP = nullptr, DstP = nullptr; 4376 if (P.getSrcPattern()->hasProperTypeByHwMode()) 4377 SrcP = P.getSrcPatternShared(); 4378 if (P.getDstPattern()->hasProperTypeByHwMode()) 4379 DstP = P.getDstPatternShared(); 4380 if (!SrcP && !DstP) { 4381 PatternsToMatch.push_back(P); 4382 continue; 4383 } 4384 4385 std::set<unsigned> Modes; 4386 if (SrcP) 4387 collectModes(Modes, SrcP.get()); 4388 if (DstP) 4389 collectModes(Modes, DstP.get()); 4390 4391 // The predicate for the default mode needs to be constructed for each 4392 // pattern separately. 4393 // Since not all modes must be present in each pattern, if a mode m is 4394 // absent, then there is no point in constructing a check for m. If such 4395 // a check was created, it would be equivalent to checking the default 4396 // mode, except not all modes' predicates would be a part of the checking 4397 // code. The subsequently generated check for the default mode would then 4398 // have the exact same patterns, but a different predicate code. To avoid 4399 // duplicated patterns with different predicate checks, construct the 4400 // default check as a negation of all predicates that are actually present 4401 // in the source/destination patterns. 4402 SmallString<128> DefaultCheck; 4403 4404 for (unsigned M : Modes) { 4405 if (M == DefaultMode) 4406 continue; 4407 4408 // Fill the map entry for this mode. 4409 const HwMode &HM = CGH.getMode(M); 4410 AppendPattern(P, M, "(MF->getSubtarget().checkFeatures(\"" + HM.Features + "\"))"); 4411 4412 // Add negations of the HM's predicates to the default predicate. 4413 if (!DefaultCheck.empty()) 4414 DefaultCheck += " && "; 4415 DefaultCheck += "(!(MF->getSubtarget().checkFeatures(\""; 4416 DefaultCheck += HM.Features; 4417 DefaultCheck += "\")))"; 4418 } 4419 4420 bool HasDefault = Modes.count(DefaultMode); 4421 if (HasDefault) 4422 AppendPattern(P, DefaultMode, DefaultCheck); 4423 } 4424 } 4425 4426 /// Dependent variable map for CodeGenDAGPattern variant generation 4427 typedef StringMap<int> DepVarMap; 4428 4429 static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) { 4430 if (N->isLeaf()) { 4431 if (N->hasName() && isa<DefInit>(N->getLeafValue())) 4432 DepMap[N->getName()]++; 4433 } else { 4434 for (size_t i = 0, e = N->getNumChildren(); i != e; ++i) 4435 FindDepVarsOf(N->getChild(i), DepMap); 4436 } 4437 } 4438 4439 /// Find dependent variables within child patterns 4440 static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) { 4441 DepVarMap depcounts; 4442 FindDepVarsOf(N, depcounts); 4443 for (const auto &Pair : depcounts) { 4444 if (Pair.getValue() > 1) 4445 DepVars.insert(Pair.getKey()); 4446 } 4447 } 4448 4449 #ifndef NDEBUG 4450 /// Dump the dependent variable set: 4451 static void DumpDepVars(MultipleUseVarSet &DepVars) { 4452 if (DepVars.empty()) { 4453 LLVM_DEBUG(errs() << "<empty set>"); 4454 } else { 4455 LLVM_DEBUG(errs() << "[ "); 4456 for (const auto &DepVar : DepVars) { 4457 LLVM_DEBUG(errs() << DepVar.getKey() << " "); 4458 } 4459 LLVM_DEBUG(errs() << "]"); 4460 } 4461 } 4462 #endif 4463 4464 4465 /// CombineChildVariants - Given a bunch of permutations of each child of the 4466 /// 'operator' node, put them together in all possible ways. 4467 static void CombineChildVariants( 4468 TreePatternNodePtr Orig, 4469 const std::vector<std::vector<TreePatternNodePtr>> &ChildVariants, 4470 std::vector<TreePatternNodePtr> &OutVariants, CodeGenDAGPatterns &CDP, 4471 const MultipleUseVarSet &DepVars) { 4472 // Make sure that each operand has at least one variant to choose from. 4473 for (const auto &Variants : ChildVariants) 4474 if (Variants.empty()) 4475 return; 4476 4477 // The end result is an all-pairs construction of the resultant pattern. 4478 std::vector<unsigned> Idxs; 4479 Idxs.resize(ChildVariants.size()); 4480 bool NotDone; 4481 do { 4482 #ifndef NDEBUG 4483 LLVM_DEBUG(if (!Idxs.empty()) { 4484 errs() << Orig->getOperator()->getName() << ": Idxs = [ "; 4485 for (unsigned Idx : Idxs) { 4486 errs() << Idx << " "; 4487 } 4488 errs() << "]\n"; 4489 }); 4490 #endif 4491 // Create the variant and add it to the output list. 4492 std::vector<TreePatternNodePtr> NewChildren; 4493 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) 4494 NewChildren.push_back(ChildVariants[i][Idxs[i]]); 4495 TreePatternNodePtr R = std::make_shared<TreePatternNode>( 4496 Orig->getOperator(), std::move(NewChildren), Orig->getNumTypes()); 4497 4498 // Copy over properties. 4499 R->setName(Orig->getName()); 4500 R->setNamesAsPredicateArg(Orig->getNamesAsPredicateArg()); 4501 R->setPredicateCalls(Orig->getPredicateCalls()); 4502 R->setTransformFn(Orig->getTransformFn()); 4503 for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i) 4504 R->setType(i, Orig->getExtType(i)); 4505 4506 // If this pattern cannot match, do not include it as a variant. 4507 std::string ErrString; 4508 // Scan to see if this pattern has already been emitted. We can get 4509 // duplication due to things like commuting: 4510 // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) 4511 // which are the same pattern. Ignore the dups. 4512 if (R->canPatternMatch(ErrString, CDP) && 4513 none_of(OutVariants, [&](TreePatternNodePtr Variant) { 4514 return R->isIsomorphicTo(Variant.get(), DepVars); 4515 })) 4516 OutVariants.push_back(R); 4517 4518 // Increment indices to the next permutation by incrementing the 4519 // indices from last index backward, e.g., generate the sequence 4520 // [0, 0], [0, 1], [1, 0], [1, 1]. 4521 int IdxsIdx; 4522 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) { 4523 if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size()) 4524 Idxs[IdxsIdx] = 0; 4525 else 4526 break; 4527 } 4528 NotDone = (IdxsIdx >= 0); 4529 } while (NotDone); 4530 } 4531 4532 /// CombineChildVariants - A helper function for binary operators. 4533 /// 4534 static void CombineChildVariants(TreePatternNodePtr Orig, 4535 const std::vector<TreePatternNodePtr> &LHS, 4536 const std::vector<TreePatternNodePtr> &RHS, 4537 std::vector<TreePatternNodePtr> &OutVariants, 4538 CodeGenDAGPatterns &CDP, 4539 const MultipleUseVarSet &DepVars) { 4540 std::vector<std::vector<TreePatternNodePtr>> ChildVariants; 4541 ChildVariants.push_back(LHS); 4542 ChildVariants.push_back(RHS); 4543 CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars); 4544 } 4545 4546 static void 4547 GatherChildrenOfAssociativeOpcode(TreePatternNodePtr N, 4548 std::vector<TreePatternNodePtr> &Children) { 4549 assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); 4550 Record *Operator = N->getOperator(); 4551 4552 // Only permit raw nodes. 4553 if (!N->getName().empty() || !N->getPredicateCalls().empty() || 4554 N->getTransformFn()) { 4555 Children.push_back(N); 4556 return; 4557 } 4558 4559 if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) 4560 Children.push_back(N->getChildShared(0)); 4561 else 4562 GatherChildrenOfAssociativeOpcode(N->getChildShared(0), Children); 4563 4564 if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) 4565 Children.push_back(N->getChildShared(1)); 4566 else 4567 GatherChildrenOfAssociativeOpcode(N->getChildShared(1), Children); 4568 } 4569 4570 /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of 4571 /// the (potentially recursive) pattern by using algebraic laws. 4572 /// 4573 static void GenerateVariantsOf(TreePatternNodePtr N, 4574 std::vector<TreePatternNodePtr> &OutVariants, 4575 CodeGenDAGPatterns &CDP, 4576 const MultipleUseVarSet &DepVars) { 4577 // We cannot permute leaves or ComplexPattern uses. 4578 if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) { 4579 OutVariants.push_back(N); 4580 return; 4581 } 4582 4583 // Look up interesting info about the node. 4584 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator()); 4585 4586 // If this node is associative, re-associate. 4587 if (NodeInfo.hasProperty(SDNPAssociative)) { 4588 // Re-associate by pulling together all of the linked operators 4589 std::vector<TreePatternNodePtr> MaximalChildren; 4590 GatherChildrenOfAssociativeOpcode(N, MaximalChildren); 4591 4592 // Only handle child sizes of 3. Otherwise we'll end up trying too many 4593 // permutations. 4594 if (MaximalChildren.size() == 3) { 4595 // Find the variants of all of our maximal children. 4596 std::vector<TreePatternNodePtr> AVariants, BVariants, CVariants; 4597 GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars); 4598 GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars); 4599 GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars); 4600 4601 // There are only two ways we can permute the tree: 4602 // (A op B) op C and A op (B op C) 4603 // Within these forms, we can also permute A/B/C. 4604 4605 // Generate legal pair permutations of A/B/C. 4606 std::vector<TreePatternNodePtr> ABVariants; 4607 std::vector<TreePatternNodePtr> BAVariants; 4608 std::vector<TreePatternNodePtr> ACVariants; 4609 std::vector<TreePatternNodePtr> CAVariants; 4610 std::vector<TreePatternNodePtr> BCVariants; 4611 std::vector<TreePatternNodePtr> CBVariants; 4612 CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars); 4613 CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars); 4614 CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars); 4615 CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars); 4616 CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars); 4617 CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars); 4618 4619 // Combine those into the result: (x op x) op x 4620 CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars); 4621 CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars); 4622 CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars); 4623 CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars); 4624 CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars); 4625 CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars); 4626 4627 // Combine those into the result: x op (x op x) 4628 CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars); 4629 CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars); 4630 CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars); 4631 CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars); 4632 CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars); 4633 CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars); 4634 return; 4635 } 4636 } 4637 4638 // Compute permutations of all children. 4639 std::vector<std::vector<TreePatternNodePtr>> ChildVariants; 4640 ChildVariants.resize(N->getNumChildren()); 4641 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 4642 GenerateVariantsOf(N->getChildShared(i), ChildVariants[i], CDP, DepVars); 4643 4644 // Build all permutations based on how the children were formed. 4645 CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars); 4646 4647 // If this node is commutative, consider the commuted order. 4648 bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP); 4649 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { 4650 unsigned Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id. 4651 assert(N->getNumChildren() >= (2 + Skip) && 4652 "Commutative but doesn't have 2 children!"); 4653 // Don't allow commuting children which are actually register references. 4654 bool NoRegisters = true; 4655 unsigned i = 0 + Skip; 4656 unsigned e = 2 + Skip; 4657 for (; i != e; ++i) { 4658 TreePatternNode *Child = N->getChild(i); 4659 if (Child->isLeaf()) 4660 if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) { 4661 Record *RR = DI->getDef(); 4662 if (RR->isSubClassOf("Register")) 4663 NoRegisters = false; 4664 } 4665 } 4666 // Consider the commuted order. 4667 if (NoRegisters) { 4668 std::vector<std::vector<TreePatternNodePtr>> Variants; 4669 unsigned i = 0; 4670 if (isCommIntrinsic) 4671 Variants.push_back(std::move(ChildVariants[i++])); // Intrinsic id. 4672 Variants.push_back(std::move(ChildVariants[i + 1])); 4673 Variants.push_back(std::move(ChildVariants[i])); 4674 i += 2; 4675 // Remaining operands are not commuted. 4676 for (; i != N->getNumChildren(); ++i) 4677 Variants.push_back(std::move(ChildVariants[i])); 4678 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars); 4679 } 4680 } 4681 } 4682 4683 4684 // GenerateVariants - Generate variants. For example, commutative patterns can 4685 // match multiple ways. Add them to PatternsToMatch as well. 4686 void CodeGenDAGPatterns::GenerateVariants() { 4687 LLVM_DEBUG(errs() << "Generating instruction variants.\n"); 4688 4689 // Loop over all of the patterns we've collected, checking to see if we can 4690 // generate variants of the instruction, through the exploitation of 4691 // identities. This permits the target to provide aggressive matching without 4692 // the .td file having to contain tons of variants of instructions. 4693 // 4694 // Note that this loop adds new patterns to the PatternsToMatch list, but we 4695 // intentionally do not reconsider these. Any variants of added patterns have 4696 // already been added. 4697 // 4698 for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { 4699 MultipleUseVarSet DepVars; 4700 std::vector<TreePatternNodePtr> Variants; 4701 FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars); 4702 LLVM_DEBUG(errs() << "Dependent/multiply used variables: "); 4703 LLVM_DEBUG(DumpDepVars(DepVars)); 4704 LLVM_DEBUG(errs() << "\n"); 4705 GenerateVariantsOf(PatternsToMatch[i].getSrcPatternShared(), Variants, 4706 *this, DepVars); 4707 4708 assert(PatternsToMatch[i].getHwModeFeatures().empty() && 4709 "HwModes should not have been expanded yet!"); 4710 4711 assert(!Variants.empty() && "Must create at least original variant!"); 4712 if (Variants.size() == 1) // No additional variants for this pattern. 4713 continue; 4714 4715 LLVM_DEBUG(errs() << "FOUND VARIANTS OF: "; 4716 PatternsToMatch[i].getSrcPattern()->dump(); errs() << "\n"); 4717 4718 for (unsigned v = 0, e = Variants.size(); v != e; ++v) { 4719 TreePatternNodePtr Variant = Variants[v]; 4720 4721 LLVM_DEBUG(errs() << " VAR#" << v << ": "; Variant->dump(); 4722 errs() << "\n"); 4723 4724 // Scan to see if an instruction or explicit pattern already matches this. 4725 bool AlreadyExists = false; 4726 for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { 4727 // Skip if the top level predicates do not match. 4728 if ((i != p) && (PatternsToMatch[i].getPredicates() != 4729 PatternsToMatch[p].getPredicates())) 4730 continue; 4731 // Check to see if this variant already exists. 4732 if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(), 4733 DepVars)) { 4734 LLVM_DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n"); 4735 AlreadyExists = true; 4736 break; 4737 } 4738 } 4739 // If we already have it, ignore the variant. 4740 if (AlreadyExists) continue; 4741 4742 // Otherwise, add it to the list of patterns we have. 4743 PatternsToMatch.emplace_back( 4744 PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(), 4745 Variant, PatternsToMatch[i].getDstPatternShared(), 4746 PatternsToMatch[i].getDstRegs(), 4747 PatternsToMatch[i].getAddedComplexity(), Record::getNewUID(Records), 4748 PatternsToMatch[i].getForceMode(), 4749 PatternsToMatch[i].getHwModeFeatures()); 4750 } 4751 4752 LLVM_DEBUG(errs() << "\n"); 4753 } 4754 } 4755