LCOV - code coverage report
Current view: top level - lib/TableGen - Record.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 973 1079 90.2 %
Date: 2018-06-17 00:07:59 Functions: 159 169 94.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Record.cpp - Record implementation ---------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Implement the tablegen record classes.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/ADT/ArrayRef.h"
      15             : #include "llvm/ADT/DenseMap.h"
      16             : #include "llvm/ADT/FoldingSet.h"
      17             : #include "llvm/ADT/SmallString.h"
      18             : #include "llvm/ADT/SmallVector.h"
      19             : #include "llvm/ADT/StringExtras.h"
      20             : #include "llvm/ADT/StringMap.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/Config/llvm-config.h"
      23             : #include "llvm/Support/Allocator.h"
      24             : #include "llvm/Support/Casting.h"
      25             : #include "llvm/Support/Compiler.h"
      26             : #include "llvm/Support/ErrorHandling.h"
      27             : #include "llvm/Support/SMLoc.h"
      28             : #include "llvm/Support/raw_ostream.h"
      29             : #include "llvm/TableGen/Error.h"
      30             : #include "llvm/TableGen/Record.h"
      31             : #include <cassert>
      32             : #include <cstdint>
      33             : #include <memory>
      34             : #include <string>
      35             : #include <utility>
      36             : #include <vector>
      37             : 
      38             : using namespace llvm;
      39             : 
      40         336 : static BumpPtrAllocator Allocator;
      41             : 
      42             : //===----------------------------------------------------------------------===//
      43             : //    Type implementations
      44             : //===----------------------------------------------------------------------===//
      45             : 
      46         336 : BitRecTy BitRecTy::Shared;
      47         336 : CodeRecTy CodeRecTy::Shared;
      48         336 : IntRecTy IntRecTy::Shared;
      49         336 : StringRecTy StringRecTy::Shared;
      50         336 : DagRecTy DagRecTy::Shared;
      51             : 
      52             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
      53             : LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
      54             : #endif
      55             : 
      56     1247070 : ListRecTy *RecTy::getListTy() {
      57     1247070 :   if (!ListTy)
      58       12454 :     ListTy = new(Allocator) ListRecTy(this);
      59     1247070 :   return ListTy;
      60             : }
      61             : 
      62       62256 : bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
      63             :   assert(RHS && "NULL pointer");
      64       62256 :   return Kind == RHS->getRecTyKind();
      65             : }
      66             : 
      67     1122635 : bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
      68             : 
      69        2628 : bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
      70        2628 :   if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
      71             :     return true;
      72             :   if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
      73           0 :     return BitsTy->getNumBits() == 1;
      74             :   return false;
      75             : }
      76             : 
      77     1089257 : BitsRecTy *BitsRecTy::get(unsigned Sz) {
      78     1089456 :   static std::vector<BitsRecTy*> Shared;
      79     2178514 :   if (Sz >= Shared.size())
      80         386 :     Shared.resize(Sz + 1);
      81     1089257 :   BitsRecTy *&Ty = Shared[Sz];
      82     1089257 :   if (!Ty)
      83        2757 :     Ty = new(Allocator) BitsRecTy(Sz);
      84     1089257 :   return Ty;
      85             : }
      86             : 
      87         250 : std::string BitsRecTy::getAsString() const {
      88         750 :   return "bits<" + utostr(Size) + ">";
      89             : }
      90             : 
      91        3241 : bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
      92        3241 :   if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
      93          11 :     return cast<BitsRecTy>(RHS)->Size == Size;
      94        3230 :   RecTyKind kind = RHS->getRecTyKind();
      95        3230 :   return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
      96             : }
      97             : 
      98       27557 : bool BitsRecTy::typeIsA(const RecTy *RHS) const {
      99             :   if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
     100           8 :     return RHSb->Size == Size;
     101             :   return false;
     102             : }
     103             : 
     104      347231 : bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     105      347231 :   RecTyKind kind = RHS->getRecTyKind();
     106      347231 :   return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
     107             : }
     108             : 
     109         128 : bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     110         128 :   RecTyKind Kind = RHS->getRecTyKind();
     111         128 :   return Kind == CodeRecTyKind || Kind == StringRecTyKind;
     112             : }
     113             : 
     114         516 : std::string StringRecTy::getAsString() const {
     115         516 :   return "string";
     116             : }
     117             : 
     118       58522 : bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     119       58522 :   RecTyKind Kind = RHS->getRecTyKind();
     120       58522 :   return Kind == StringRecTyKind || Kind == CodeRecTyKind;
     121             : }
     122             : 
     123         245 : std::string ListRecTy::getAsString() const {
     124         735 :   return "list<" + Ty->getAsString() + ">";
     125             : }
     126             : 
     127      167467 : bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     128             :   if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
     129      167467 :     return Ty->typeIsConvertibleTo(ListTy->getElementType());
     130             :   return false;
     131             : }
     132             : 
     133     4472469 : bool ListRecTy::typeIsA(const RecTy *RHS) const {
     134             :   if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
     135     4472469 :     return getElementType()->typeIsA(RHSl->getElementType());
     136             :   return false;
     137             : }
     138             : 
     139         178 : std::string DagRecTy::getAsString() const {
     140         178 :   return "dag";
     141             : }
     142             : 
     143     3697379 : static void ProfileRecordRecTy(FoldingSetNodeID &ID,
     144             :                                ArrayRef<Record *> Classes) {
     145     3697379 :   ID.AddInteger(Classes.size());
     146    12984353 :   for (Record *R : Classes)
     147     4643487 :     ID.AddPointer(R);
     148     3697379 : }
     149             : 
     150     1595626 : RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) {
     151     1595626 :   if (UnsortedClasses.empty()) {
     152        2564 :     static RecordRecTy AnyRecord(0);
     153             :     return &AnyRecord;
     154             :   }
     155             : 
     156             :   FoldingSet<RecordRecTy> &ThePool =
     157     1593258 :       UnsortedClasses[0]->getRecords().RecordTypePool;
     158             : 
     159             :   SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
     160             :                                    UnsortedClasses.end());
     161             :   llvm::sort(Classes.begin(), Classes.end(),
     162      542180 :              [](Record *LHS, Record *RHS) {
     163     1084360 :                return LHS->getNameInitAsString() < RHS->getNameInitAsString();
     164      542180 :              });
     165             : 
     166             :   FoldingSetNodeID ID;
     167     1593258 :   ProfileRecordRecTy(ID, Classes);
     168             : 
     169     1593258 :   void *IP = nullptr;
     170     1531784 :   if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
     171             :     return Ty;
     172             : 
     173             : #ifndef NDEBUG
     174             :   // Check for redundancy.
     175             :   for (unsigned i = 0; i < Classes.size(); ++i) {
     176             :     for (unsigned j = 0; j < Classes.size(); ++j) {
     177             :       assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
     178             :     }
     179             :     assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
     180             :   }
     181             : #endif
     182             : 
     183             :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()),
     184       61474 :                                  alignof(RecordRecTy));
     185       61474 :   RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
     186             :   std::uninitialized_copy(Classes.begin(), Classes.end(),
     187             :                           Ty->getTrailingObjects<Record *>());
     188       61474 :   ThePool.InsertNode(Ty, IP);
     189       61474 :   return Ty;
     190             : }
     191             : 
     192     2104121 : void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
     193     2104121 :   ProfileRecordRecTy(ID, getClasses());
     194     2104121 : }
     195             : 
     196         127 : std::string RecordRecTy::getAsString() const {
     197         127 :   if (NumClasses == 1)
     198         125 :     return getClasses()[0]->getNameInitAsString();
     199             : 
     200           2 :   std::string Str = "{";
     201             :   bool First = true;
     202           2 :   for (Record *R : getClasses()) {
     203           0 :     if (!First)
     204             :       Str += ", ";
     205             :     First = false;
     206           0 :     Str += R->getNameInitAsString();
     207             :   }
     208             :   Str += "}";
     209             :   return Str;
     210             : }
     211             : 
     212    10711149 : bool RecordRecTy::isSubClassOf(Record *Class) const {
     213             :   return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
     214    27666074 :                                       return MySuperClass == Class ||
     215             :                                              MySuperClass->isSubClassOf(Class);
     216    10711149 :                                     });
     217             : }
     218             : 
     219    13823333 : bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     220    13823333 :   if (this == RHS)
     221             :     return true;
     222             : 
     223             :   const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
     224             :   if (!RTy)
     225             :     return false;
     226             : 
     227             :   return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
     228             :                                            return isSubClassOf(TargetClass);
     229    10324828 :                                          });
     230             : }
     231             : 
     232    10021007 : bool RecordRecTy::typeIsA(const RecTy *RHS) const {
     233    10021007 :   return typeIsConvertibleTo(RHS);
     234             : }
     235             : 
     236      209656 : static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
     237             :   SmallVector<Record *, 4> CommonSuperClasses;
     238             :   SmallVector<Record *, 4> Stack;
     239             : 
     240      209656 :   Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end());
     241             : 
     242      982298 :   while (!Stack.empty()) {
     243      386321 :     Record *R = Stack.back();
     244             :     Stack.pop_back();
     245             : 
     246      386321 :     if (T2->isSubClassOf(R)) {
     247      211928 :       CommonSuperClasses.push_back(R);
     248             :     } else {
     249      174393 :       R->getDirectSuperClasses(Stack);
     250             :     }
     251             :   }
     252             : 
     253      419312 :   return RecordRecTy::get(CommonSuperClasses);
     254             : }
     255             : 
     256     2112068 : RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
     257     2112068 :   if (T1 == T2)
     258             :     return T1;
     259             : 
     260             :   if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
     261             :     if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
     262      209656 :       return resolveRecordTypes(RecTy1, RecTy2);
     263             :   }
     264             : 
     265        4022 :   if (T1->typeIsConvertibleTo(T2))
     266             :     return T2;
     267         608 :   if (T2->typeIsConvertibleTo(T1))
     268             :     return T1;
     269             : 
     270             :   if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
     271             :     if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
     272           0 :       RecTy* NewType = resolveTypes(ListTy1->getElementType(),
     273           0 :                                     ListTy2->getElementType());
     274           0 :       if (NewType)
     275           0 :         return NewType->getListTy();
     276             :     }
     277             :   }
     278             : 
     279             :   return nullptr;
     280             : }
     281             : 
     282             : //===----------------------------------------------------------------------===//
     283             : //    Initializer implementations
     284             : //===----------------------------------------------------------------------===//
     285             : 
     286           0 : void Init::anchor() {}
     287             : 
     288             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     289             : LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
     290             : #endif
     291             : 
     292      979842 : UnsetInit *UnsetInit::get() {
     293      980174 :   static UnsetInit TheInit;
     294      979842 :   return &TheInit;
     295             : }
     296             : 
     297    14273838 : Init *UnsetInit::getCastTo(RecTy *Ty) const {
     298    14273838 :   return const_cast<UnsetInit *>(this);
     299             : }
     300             : 
     301           0 : Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
     302           0 :   return const_cast<UnsetInit *>(this);
     303             : }
     304             : 
     305    12203894 : BitInit *BitInit::get(bool V) {
     306    12204162 :   static BitInit True(true);
     307    12204162 :   static BitInit False(false);
     308             : 
     309    12203894 :   return V ? &True : &False;
     310             : }
     311             : 
     312     3589137 : Init *BitInit::convertInitializerTo(RecTy *Ty) const {
     313     3589137 :   if (isa<BitRecTy>(Ty))
     314        2115 :     return const_cast<BitInit *>(this);
     315             : 
     316     3587022 :   if (isa<IntRecTy>(Ty))
     317     3587022 :     return IntInit::get(getValue());
     318             : 
     319             :   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
     320             :     // Can only convert single bit.
     321           0 :     if (BRT->getNumBits() == 1)
     322           0 :       return BitsInit::get(const_cast<BitInit *>(this));
     323             :   }
     324             : 
     325             :   return nullptr;
     326             : }
     327             : 
     328             : static void
     329    21751703 : ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
     330    21751703 :   ID.AddInteger(Range.size());
     331             : 
     332   987000663 :   for (Init *I : Range)
     333   482624480 :     ID.AddPointer(I);
     334    21751703 : }
     335             : 
     336     7721493 : BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
     337     7721692 :   static FoldingSet<BitsInit> ThePool;
     338             : 
     339             :   FoldingSetNodeID ID;
     340     7721493 :   ProfileBitsInit(ID, Range);
     341             : 
     342     7721493 :   void *IP = nullptr;
     343     6945847 :   if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     344             :     return I;
     345             : 
     346             :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
     347      775646 :                                  alignof(BitsInit));
     348      775646 :   BitsInit *I = new(Mem) BitsInit(Range.size());
     349             :   std::uninitialized_copy(Range.begin(), Range.end(),
     350             :                           I->getTrailingObjects<Init *>());
     351      775646 :   ThePool.InsertNode(I, IP);
     352      775646 :   return I;
     353             : }
     354             : 
     355    14030210 : void BitsInit::Profile(FoldingSetNodeID &ID) const {
     356    28060420 :   ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
     357    14030210 : }
     358             : 
     359     2718949 : Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
     360     2718949 :   if (isa<BitRecTy>(Ty)) {
     361      112408 :     if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
     362      112408 :     return getBit(0);
     363             :   }
     364             : 
     365             :   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
     366             :     // If the number of bits is right, return it.  Otherwise we need to expand
     367             :     // or truncate.
     368      147818 :     if (getNumBits() != BRT->getNumBits()) return nullptr;
     369      147810 :     return const_cast<BitsInit *>(this);
     370             :   }
     371             : 
     372     2458723 :   if (isa<IntRecTy>(Ty)) {
     373             :     int64_t Result = 0;
     374     7690521 :     for (unsigned i = 0, e = getNumBits(); i != e; ++i)
     375             :       if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
     376     5231798 :         Result |= static_cast<int64_t>(Bit->getValue()) << i;
     377             :       else
     378             :         return nullptr;
     379     2457743 :     return IntInit::get(Result);
     380             :   }
     381             : 
     382             :   return nullptr;
     383             : }
     384             : 
     385             : Init *
     386           0 : BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
     387           0 :   SmallVector<Init *, 16> NewBits(Bits.size());
     388             : 
     389           0 :   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
     390           0 :     if (Bits[i] >= getNumBits())
     391             :       return nullptr;
     392           0 :     NewBits[i] = getBit(Bits[i]);
     393             :   }
     394           0 :   return BitsInit::get(NewBits);
     395             : }
     396             : 
     397       95677 : bool BitsInit::isConcrete() const {
     398      231326 :   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     399      135649 :     if (!getBit(i)->isConcrete())
     400             :       return false;
     401             :   }
     402             :   return true;
     403             : }
     404             : 
     405        1042 : std::string BitsInit::getAsString() const {
     406        1042 :   std::string Result = "{ ";
     407        5474 :   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     408        4432 :     if (i) Result += ", ";
     409        8864 :     if (Init *Bit = getBit(e-i-1))
     410        8864 :       Result += Bit->getAsString();
     411             :     else
     412             :       Result += "*";
     413             :   }
     414        2084 :   return Result + " }";
     415             : }
     416             : 
     417             : // resolveReferences - If there are any field references that refer to fields
     418             : // that have been filled in, we can propagate the values now.
     419    43624909 : Init *BitsInit::resolveReferences(Resolver &R) const {
     420             :   bool Changed = false;
     421    87249818 :   SmallVector<Init *, 16> NewBits(getNumBits());
     422             : 
     423             :   Init *CachedBitVarRef = nullptr;
     424             :   Init *CachedBitVarResolved = nullptr;
     425             : 
     426   463675280 :   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     427             :     Init *CurBit = getBit(i);
     428             :     Init *NewBit = CurBit;
     429             : 
     430             :     if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
     431   154412647 :       if (CurBitVar->getBitVar() != CachedBitVarRef) {
     432             :         CachedBitVarRef = CurBitVar->getBitVar();
     433    40030890 :         CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
     434             :       }
     435             : 
     436   154412647 :       NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
     437             :     } else {
     438             :       // getBit(0) implicitly converts int and bits<1> values to bit.
     439   265637724 :       NewBit = CurBit->resolveReferences(R)->getBit(0);
     440             :     }
     441             : 
     442   420050371 :     if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
     443             :       NewBit = CurBit;
     444   420050371 :     NewBits[i] = NewBit;
     445   420050371 :     Changed |= CurBit != NewBit;
     446             :   }
     447             : 
     448    43624909 :   if (Changed)
     449     5922401 :     return BitsInit::get(NewBits);
     450             : 
     451    37702508 :   return const_cast<BitsInit *>(this);
     452             : }
     453             : 
     454    10609198 : IntInit *IntInit::get(int64_t V) {
     455    10609505 :   static DenseMap<int64_t, IntInit*> ThePool;
     456             : 
     457             :   IntInit *&I = ThePool[V];
     458    10742018 :   if (!I) I = new(Allocator) IntInit(V);
     459    10609198 :   return I;
     460             : }
     461             : 
     462      867883 : std::string IntInit::getAsString() const {
     463      867883 :   return itostr(Value);
     464             : }
     465             : 
     466             : static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
     467             :   // For example, with NumBits == 4, we permit Values from [-7 .. 15].
     468     1142149 :   return (NumBits >= sizeof(Value) * 8) ||
     469     1142596 :          (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
     470             : }
     471             : 
     472     6326813 : Init *IntInit::convertInitializerTo(RecTy *Ty) const {
     473     6326813 :   if (isa<IntRecTy>(Ty))
     474     4437378 :     return const_cast<IntInit *>(this);
     475             : 
     476     1889435 :   if (isa<BitRecTy>(Ty)) {
     477      747025 :     int64_t Val = getValue();
     478      747025 :     if (Val != 0 && Val != 1) return nullptr;  // Only accept 0 or 1 for a bit!
     479      747022 :     return BitInit::get(Val != 0);
     480             :   }
     481             : 
     482             :   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
     483     1142410 :     int64_t Value = getValue();
     484             :     // Make sure this bitfield is large enough to hold the integer value.
     485     1142410 :     if (!canFitInBitfield(Value, BRT->getNumBits()))
     486             :       return nullptr;
     487             : 
     488     2284814 :     SmallVector<Init *, 16> NewBits(BRT->getNumBits());
     489    15538115 :     for (unsigned i = 0; i != BRT->getNumBits(); ++i)
     490    14395708 :       NewBits[i] = BitInit::get(Value & (1LL << i));
     491             : 
     492     1142407 :     return BitsInit::get(NewBits);
     493             :   }
     494             : 
     495             :   return nullptr;
     496             : }
     497             : 
     498             : Init *
     499           0 : IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
     500           0 :   SmallVector<Init *, 16> NewBits(Bits.size());
     501             : 
     502           0 :   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
     503           0 :     if (Bits[i] >= 64)
     504             :       return nullptr;
     505             : 
     506           0 :     NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
     507             :   }
     508           0 :   return BitsInit::get(NewBits);
     509             : }
     510             : 
     511       25111 : CodeInit *CodeInit::get(StringRef V) {
     512       25329 :   static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
     513             : 
     514             :   auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
     515       25111 :   if (!Entry.second)
     516       21367 :     Entry.second = new(Allocator) CodeInit(Entry.getKey());
     517       25111 :   return Entry.second;
     518             : }
     519             : 
     520   124724445 : StringInit *StringInit::get(StringRef V) {
     521   124724781 :   static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
     522             : 
     523             :   auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
     524   124724445 :   if (!Entry.second)
     525     7726262 :     Entry.second = new(Allocator) StringInit(Entry.getKey());
     526   124724445 :   return Entry.second;
     527             : }
     528             : 
     529      461044 : Init *StringInit::convertInitializerTo(RecTy *Ty) const {
     530      461044 :   if (isa<StringRecTy>(Ty))
     531           0 :     return const_cast<StringInit *>(this);
     532      461044 :   if (isa<CodeRecTy>(Ty))
     533        6190 :     return CodeInit::get(getValue());
     534             : 
     535             :   return nullptr;
     536             : }
     537             : 
     538          30 : Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
     539          30 :   if (isa<CodeRecTy>(Ty))
     540           0 :     return const_cast<CodeInit *>(this);
     541          30 :   if (isa<StringRecTy>(Ty))
     542          30 :     return StringInit::get(getValue());
     543             : 
     544             :   return nullptr;
     545             : }
     546             : 
     547    19323378 : static void ProfileListInit(FoldingSetNodeID &ID,
     548             :                             ArrayRef<Init *> Range,
     549             :                             RecTy *EltTy) {
     550    19323378 :   ID.AddInteger(Range.size());
     551    19323378 :   ID.AddPointer(EltTy);
     552             : 
     553   116739834 :   for (Init *I : Range)
     554    48708228 :     ID.AddPointer(I);
     555    19323378 : }
     556             : 
     557     6803053 : ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
     558     6803320 :   static FoldingSet<ListInit> ThePool;
     559             : 
     560             :   FoldingSetNodeID ID;
     561     6803053 :   ProfileListInit(ID, Range, EltTy);
     562             : 
     563     6803053 :   void *IP = nullptr;
     564     5615986 :   if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     565             :     return I;
     566             : 
     567             :   assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
     568             :          cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
     569             : 
     570             :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
     571     1187067 :                                  alignof(ListInit));
     572     1187067 :   ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
     573             :   std::uninitialized_copy(Range.begin(), Range.end(),
     574             :                           I->getTrailingObjects<Init *>());
     575     1187067 :   ThePool.InsertNode(I, IP);
     576     1187067 :   return I;
     577             : }
     578             : 
     579    12520325 : void ListInit::Profile(FoldingSetNodeID &ID) const {
     580    12520325 :   RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
     581             : 
     582    12520325 :   ProfileListInit(ID, getValues(), EltTy);
     583    12520325 : }
     584             : 
     585           0 : Init *ListInit::convertInitializerTo(RecTy *Ty) const {
     586           0 :   if (getType() == Ty)
     587           0 :     return const_cast<ListInit*>(this);
     588             : 
     589             :   if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
     590             :     SmallVector<Init*, 8> Elements;
     591           0 :     Elements.reserve(getValues().size());
     592             : 
     593             :     // Verify that all of the elements of the list are subclasses of the
     594             :     // appropriate class!
     595             :     bool Changed = false;
     596           0 :     RecTy *ElementType = LRT->getElementType();
     597           0 :     for (Init *I : getValues())
     598           0 :       if (Init *CI = I->convertInitializerTo(ElementType)) {
     599           0 :         Elements.push_back(CI);
     600           0 :         if (CI != I)
     601             :           Changed = true;
     602             :       } else
     603           0 :         return nullptr;
     604             : 
     605           0 :     if (!Changed)
     606           0 :       return const_cast<ListInit*>(this);
     607           0 :     return ListInit::get(Elements, ElementType);
     608             :   }
     609             : 
     610             :   return nullptr;
     611             : }
     612             : 
     613        1118 : Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
     614             :   SmallVector<Init*, 8> Vals;
     615        1118 :   Vals.reserve(Elements.size());
     616       22742 :   for (unsigned Element : Elements) {
     617       10812 :     if (Element >= size())
     618             :       return nullptr;
     619       10812 :     Vals.push_back(getElement(Element));
     620             :   }
     621        1118 :   return ListInit::get(Vals, getElementType());
     622             : }
     623             : 
     624     1938029 : Record *ListInit::getElementAsRecord(unsigned i) const {
     625             :   assert(i < NumValues && "List element index out of range!");
     626             :   DefInit *DI = dyn_cast<DefInit>(getElement(i));
     627             :   if (!DI)
     628           0 :     PrintFatalError("Expected record in list!");
     629     1938029 :   return DI->getDef();
     630             : }
     631             : 
     632    39792960 : Init *ListInit::resolveReferences(Resolver &R) const {
     633             :   SmallVector<Init*, 8> Resolved;
     634    79585920 :   Resolved.reserve(size());
     635             :   bool Changed = false;
     636             : 
     637   119403190 :   for (Init *CurElt : getValues()) {
     638    39805115 :     Init *E = CurElt->resolveReferences(R);
     639    39805115 :     Changed |= E != CurElt;
     640    39805115 :     Resolved.push_back(E);
     641             :   }
     642             : 
     643    39792960 :   if (Changed)
     644     1499653 :     return ListInit::get(Resolved, getElementType());
     645    38293307 :   return const_cast<ListInit *>(this);
     646             : }
     647             : 
     648    10480786 : bool ListInit::isConcrete() const {
     649    32346860 :   for (Init *Element : *this) {
     650    10933037 :     if (!Element->isConcrete())
     651             :       return false;
     652             :   }
     653             :   return true;
     654             : }
     655             : 
     656         127 : std::string ListInit::getAsString() const {
     657         127 :   std::string Result = "[";
     658             :   const char *sep = "";
     659         711 :   for (Init *Element : *this) {
     660             :     Result += sep;
     661             :     sep = ", ";
     662         584 :     Result += Element->getAsString();
     663             :   }
     664         254 :   return Result + "]";
     665             : }
     666             : 
     667    11827705 : Init *OpInit::getBit(unsigned Bit) const {
     668    11827705 :   if (getType() == BitRecTy::get())
     669         392 :     return const_cast<OpInit*>(this);
     670    11827313 :   return VarBitInit::get(const_cast<OpInit*>(this), Bit);
     671             : }
     672             : 
     673             : static void
     674    11233763 : ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
     675    11233763 :   ID.AddInteger(Opcode);
     676    11233763 :   ID.AddPointer(Op);
     677    11233763 :   ID.AddPointer(Type);
     678    11233763 : }
     679             : 
     680     4586441 : UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
     681     4586663 :   static FoldingSet<UnOpInit> ThePool;
     682             : 
     683             :   FoldingSetNodeID ID;
     684     4586441 :   ProfileUnOpInit(ID, Opc, LHS, Type);
     685             : 
     686     4586441 :   void *IP = nullptr;
     687     4236297 :   if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     688             :     return I;
     689             : 
     690             :   UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
     691      350144 :   ThePool.InsertNode(I, IP);
     692      350144 :   return I;
     693             : }
     694             : 
     695     6647322 : void UnOpInit::Profile(FoldingSetNodeID &ID) const {
     696     6647322 :   ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
     697     6647322 : }
     698             : 
     699     4585931 : Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
     700     4585931 :   switch (getOpcode()) {
     701     4124776 :   case CAST:
     702     8249552 :     if (isa<StringRecTy>(getType())) {
     703      356242 :       if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
     704             :         return LHSs;
     705             : 
     706             :       if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
     707      464343 :         return StringInit::get(LHSd->getAsString());
     708             : 
     709             :       if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
     710      539712 :         return StringInit::get(LHSi->getAsString());
     711     3768534 :     } else if (isa<RecordRecTy>(getType())) {
     712     2141969 :       if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
     713             :         assert(CurRec && "NULL pointer");
     714             :         Record *D;
     715             : 
     716             :         // Self-references are allowed, but their resolution is delayed until
     717             :         // the final resolve to ensure that we get the correct type for them.
     718     2007683 :         if (Name == CurRec->getNameInit()) {
     719         537 :           if (!IsFinal)
     720             :             break;
     721             :           D = CurRec;
     722             :         } else {
     723     2007146 :           D = CurRec->getRecords().getDef(Name->getValue());
     724     2007146 :           if (!D) {
     725      105657 :             if (IsFinal)
     726           1 :               PrintFatalError(CurRec->getLoc(),
     727             :                               Twine("Undefined reference to record: '") +
     728           1 :                               Name->getValue() + "'\n");
     729             :             break;
     730             :           }
     731             :         }
     732             : 
     733     1902012 :         DefInit *DI = DefInit::get(D);
     734     1902012 :         if (!DI->getType()->typeIsA(getType())) {
     735           1 :           PrintFatalError(CurRec->getLoc(),
     736             :                           Twine("Expected type '") +
     737           1 :                           getType()->getAsString() + "', got '" +
     738           1 :                           DI->getType()->getAsString() + "' in: " +
     739           1 :                           getAsString() + "\n");
     740             :         }
     741             :         return DI;
     742             :       }
     743             :     }
     744             : 
     745     1782408 :     if (Init *NewInit = LHS->convertInitializerTo(getType()))
     746      149227 :       return NewInit;
     747             :     break;
     748             : 
     749        2172 :   case HEAD:
     750        2172 :     if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
     751             :       assert(!LHSl->empty() && "Empty list in head");
     752        1973 :       return LHSl->getElement(0);
     753             :     }
     754             :     break;
     755             : 
     756        4232 :   case TAIL:
     757        4232 :     if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
     758             :       assert(!LHSl->empty() && "Empty list in tail");
     759             :       // Note the +1.  We can't just pass the result of getValues()
     760             :       // directly.
     761        3982 :       return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
     762             :     }
     763             :     break;
     764             : 
     765      448221 :   case SIZE:
     766      448221 :     if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
     767      429289 :       return IntInit::get(LHSl->size());
     768             :     break;
     769             : 
     770        6530 :   case EMPTY:
     771        6530 :     if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
     772       12518 :       return IntInit::get(LHSl->empty());
     773             :     if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
     774           0 :       return IntInit::get(LHSs->getValue().empty());
     775             :     break;
     776             :   }
     777      274549 :   return const_cast<UnOpInit *>(this);
     778             : }
     779             : 
     780     7368765 : Init *UnOpInit::resolveReferences(Resolver &R) const {
     781     7368765 :   Init *lhs = LHS->resolveReferences(R);
     782             : 
     783     7368765 :   if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
     784     4546509 :     return (UnOpInit::get(getOpcode(), lhs, getType()))
     785     9093018 :         ->Fold(R.getCurrentRecord(), R.isFinal());
     786     2822256 :   return const_cast<UnOpInit *>(this);
     787             : }
     788             : 
     789          82 : std::string UnOpInit::getAsString() const {
     790             :   std::string Result;
     791          82 :   switch (getOpcode()) {
     792         272 :   case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
     793             :   case HEAD: Result = "!head"; break;
     794             :   case TAIL: Result = "!tail"; break;
     795             :   case SIZE: Result = "!size"; break;
     796             :   case EMPTY: Result = "!empty"; break;
     797             :   }
     798         410 :   return Result + "(" + LHS->getAsString() + ")";
     799             : }
     800             : 
     801             : static void
     802    37016284 : ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
     803             :                  RecTy *Type) {
     804    37016284 :   ID.AddInteger(Opcode);
     805    37016284 :   ID.AddPointer(LHS);
     806    37016284 :   ID.AddPointer(RHS);
     807    37016284 :   ID.AddPointer(Type);
     808    37016284 : }
     809             : 
     810    13643473 : BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
     811             :                           Init *RHS, RecTy *Type) {
     812    13643707 :   static FoldingSet<BinOpInit> ThePool;
     813             : 
     814             :   FoldingSetNodeID ID;
     815    13643473 :   ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
     816             : 
     817    13643473 :   void *IP = nullptr;
     818    10467793 :   if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     819             :     return I;
     820             : 
     821             :   BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
     822     3175680 :   ThePool.InsertNode(I, IP);
     823     3175680 :   return I;
     824             : }
     825             : 
     826    23372811 : void BinOpInit::Profile(FoldingSetNodeID &ID) const {
     827    23372811 :   ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
     828    23372811 : }
     829             : 
     830    17821460 : static StringInit *ConcatStringInits(const StringInit *I0,
     831             :                                      const StringInit *I1) {
     832             :   SmallString<80> Concat(I0->getValue());
     833             :   Concat.append(I1->getValue());
     834    35642920 :   return StringInit::get(Concat);
     835             : }
     836             : 
     837    11602122 : Init *BinOpInit::getStrConcat(Init *I0, Init *I1) {
     838             :   // Shortcut for the common case of concatenating two strings.
     839             :   if (const StringInit *I0s = dyn_cast<StringInit>(I0))
     840             :     if (const StringInit *I1s = dyn_cast<StringInit>(I1))
     841    11450504 :       return ConcatStringInits(I0s, I1s);
     842      151618 :   return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
     843             : }
     844             : 
     845    13491855 : Init *BinOpInit::Fold(Record *CurRec) const {
     846    13491855 :   switch (getOpcode()) {
     847     1499791 :   case CONCAT: {
     848     1499791 :     DagInit *LHSs = dyn_cast<DagInit>(LHS);
     849     1499791 :     DagInit *RHSs = dyn_cast<DagInit>(RHS);
     850     1499791 :     if (LHSs && RHSs) {
     851     1029052 :       DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
     852     1029052 :       DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
     853     1029052 :       if (!LOp || !ROp)
     854             :         break;
     855     1029051 :       if (LOp->getDef() != ROp->getDef()) {
     856           0 :         PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
     857           0 :                         LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
     858             :                         "'");
     859             :       }
     860             :       SmallVector<Init*, 8> Args;
     861             :       SmallVector<StringInit*, 8> ArgNames;
     862     2461057 :       for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
     863     1432006 :         Args.push_back(LHSs->getArg(i));
     864     1432006 :         ArgNames.push_back(LHSs->getArgName(i));
     865             :       }
     866     7550341 :       for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
     867     6521290 :         Args.push_back(RHSs->getArg(i));
     868     6521290 :         ArgNames.push_back(RHSs->getArgName(i));
     869             :       }
     870     1029051 :       return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
     871             :     }
     872             :     break;
     873             :   }
     874     1763068 :   case LISTCONCAT: {
     875     1763068 :     ListInit *LHSs = dyn_cast<ListInit>(LHS);
     876     1763068 :     ListInit *RHSs = dyn_cast<ListInit>(RHS);
     877     1763068 :     if (LHSs && RHSs) {
     878             :       SmallVector<Init *, 8> Args;
     879     1591617 :       Args.insert(Args.end(), LHSs->begin(), LHSs->end());
     880     1591617 :       Args.insert(Args.end(), RHSs->begin(), RHSs->end());
     881     1591617 :       return ListInit::get(Args, LHSs->getElementType());
     882             :     }
     883             :     break;
     884             :   }
     885     7235944 :   case STRCONCAT: {
     886     7235944 :     StringInit *LHSs = dyn_cast<StringInit>(LHS);
     887     7235944 :     StringInit *RHSs = dyn_cast<StringInit>(RHS);
     888     7235944 :     if (LHSs && RHSs)
     889     6370956 :       return ConcatStringInits(LHSs, RHSs);
     890             :     break;
     891             :   }
     892      916808 :   case EQ:
     893             :   case NE:
     894             :   case LE:
     895             :   case LT:
     896             :   case GE:
     897             :   case GT: {
     898             :     // try to fold eq comparison for 'bit' and 'int', otherwise fallback
     899             :     // to string objects.
     900             :     IntInit *L =
     901      916808 :         dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
     902             :     IntInit *R =
     903      916808 :         dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
     904             : 
     905      916808 :     if (L && R) {
     906             :       bool Result;
     907      671796 :       switch (getOpcode()) {
     908      670199 :       case EQ: Result = L->getValue() == R->getValue(); break;
     909           6 :       case NE: Result = L->getValue() != R->getValue(); break;
     910         994 :       case LE: Result = L->getValue() <= R->getValue(); break;
     911         589 :       case LT: Result = L->getValue() < R->getValue(); break;
     912           4 :       case GE: Result = L->getValue() >= R->getValue(); break;
     913           4 :       case GT: Result = L->getValue() > R->getValue(); break;
     914           0 :       default: llvm_unreachable("unhandled comparison");
     915             :       }
     916      671796 :       return BitInit::get(Result);
     917             :     }
     918             : 
     919      245012 :     if (getOpcode() == EQ || getOpcode() == NE) {
     920      244828 :       StringInit *LHSs = dyn_cast<StringInit>(LHS);
     921      244828 :       StringInit *RHSs = dyn_cast<StringInit>(RHS);
     922             : 
     923             :       // Make sure we've resolved
     924      244828 :       if (LHSs && RHSs) {
     925             :         bool Equal = LHSs->getValue() == RHSs->getValue();
     926      218519 :         return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
     927             :       }
     928             :     }
     929             : 
     930             :     break;
     931             :   }
     932     2076244 :   case ADD:
     933             :   case AND:
     934             :   case OR:
     935             :   case SHL:
     936             :   case SRA:
     937             :   case SRL: {
     938             :     IntInit *LHSi =
     939     2076244 :       dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
     940             :     IntInit *RHSi =
     941     2076244 :       dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
     942     2076244 :     if (LHSi && RHSi) {
     943     2003680 :       int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
     944             :       int64_t Result;
     945     2003680 :       switch (getOpcode()) {
     946           0 :       default: llvm_unreachable("Bad opcode!");
     947      574875 :       case ADD: Result = LHSv +  RHSv; break;
     948       55721 :       case AND: Result = LHSv &  RHSv; break;
     949      795575 :       case OR: Result = LHSv | RHSv; break;
     950      324984 :       case SHL: Result = LHSv << RHSv; break;
     951           1 :       case SRA: Result = LHSv >> RHSv; break;
     952      252524 :       case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
     953             :       }
     954     2003680 :       return IntInit::get(Result);
     955             :     }
     956             :     break;
     957             :   }
     958             :   }
     959     1606236 :   return const_cast<BinOpInit *>(this);
     960             : }
     961             : 
     962    22363697 : Init *BinOpInit::resolveReferences(Resolver &R) const {
     963    22363697 :   Init *lhs = LHS->resolveReferences(R);
     964    22363697 :   Init *rhs = RHS->resolveReferences(R);
     965             : 
     966    22363697 :   if (LHS != lhs || RHS != rhs)
     967    13448135 :     return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
     968    26896270 :         ->Fold(R.getCurrentRecord());
     969     8915562 :   return const_cast<BinOpInit *>(this);
     970             : }
     971             : 
     972         102 : std::string BinOpInit::getAsString() const {
     973             :   std::string Result;
     974         102 :   switch (getOpcode()) {
     975             :   case CONCAT: Result = "!con"; break;
     976             :   case ADD: Result = "!add"; break;
     977             :   case AND: Result = "!and"; break;
     978             :   case OR: Result = "!or"; break;
     979             :   case SHL: Result = "!shl"; break;
     980             :   case SRA: Result = "!sra"; break;
     981             :   case SRL: Result = "!srl"; break;
     982             :   case EQ: Result = "!eq"; break;
     983             :   case NE: Result = "!ne"; break;
     984             :   case LE: Result = "!le"; break;
     985             :   case LT: Result = "!lt"; break;
     986             :   case GE: Result = "!ge"; break;
     987             :   case GT: Result = "!gt"; break;
     988             :   case LISTCONCAT: Result = "!listconcat"; break;
     989             :   case STRCONCAT: Result = "!strconcat"; break;
     990             :   }
     991         816 :   return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
     992             : }
     993             : 
     994             : static void
     995     5008085 : ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
     996             :                   Init *RHS, RecTy *Type) {
     997     5008085 :   ID.AddInteger(Opcode);
     998     5008085 :   ID.AddPointer(LHS);
     999     5008085 :   ID.AddPointer(MHS);
    1000     5008085 :   ID.AddPointer(RHS);
    1001     5008085 :   ID.AddPointer(Type);
    1002     5008085 : }
    1003             : 
    1004     1819438 : TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
    1005             :                             RecTy *Type) {
    1006     1819639 :   static FoldingSet<TernOpInit> ThePool;
    1007             : 
    1008             :   FoldingSetNodeID ID;
    1009     1819438 :   ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
    1010             : 
    1011     1819438 :   void *IP = nullptr;
    1012     1542828 :   if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1013             :     return I;
    1014             : 
    1015             :   TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
    1016      276610 :   ThePool.InsertNode(I, IP);
    1017      276610 :   return I;
    1018             : }
    1019             : 
    1020     3188647 : void TernOpInit::Profile(FoldingSetNodeID &ID) const {
    1021     3188647 :   ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
    1022     3188647 : }
    1023             : 
    1024     2669817 : static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
    1025             :   MapResolver R(CurRec);
    1026             :   R.set(LHS, MHSe);
    1027     5339634 :   return RHS->resolveReferences(R);
    1028             : }
    1029             : 
    1030       21030 : static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
    1031             :                              Record *CurRec) {
    1032             :   bool Change = false;
    1033       21030 :   Init *Val = ForeachApply(LHS, MHSd->getOperator(), RHS, CurRec);
    1034       21030 :   if (Val != MHSd->getOperator())
    1035             :     Change = true;
    1036             : 
    1037             :   SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs;
    1038      258866 :   for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
    1039             :     Init *Arg = MHSd->getArg(i);
    1040             :     Init *NewArg;
    1041             :     StringInit *ArgName = MHSd->getArgName(i);
    1042             : 
    1043             :     if (DagInit *Argd = dyn_cast<DagInit>(Arg))
    1044          14 :       NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
    1045             :     else
    1046      118904 :       NewArg = ForeachApply(LHS, Arg, RHS, CurRec);
    1047             : 
    1048      118918 :     NewArgs.push_back(std::make_pair(NewArg, ArgName));
    1049      118918 :     if (Arg != NewArg)
    1050             :       Change = true;
    1051             :   }
    1052             : 
    1053       21030 :   if (Change)
    1054       21030 :     return DagInit::get(Val, nullptr, NewArgs);
    1055             :   return MHSd;
    1056             : }
    1057             : 
    1058             : // Applies RHS to all elements of MHS, using LHS as a temp variable.
    1059      836085 : static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
    1060             :                            Record *CurRec) {
    1061             :   if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
    1062       21016 :     return ForeachDagApply(LHS, MHSd, RHS, CurRec);
    1063             : 
    1064             :   if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
    1065             :     SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
    1066             : 
    1067     5863459 :     for (Init *&Item : NewList) {
    1068     2529883 :       Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec);
    1069     2529883 :       if (NewItem != Item)
    1070     2397580 :         Item = NewItem;
    1071             :     }
    1072     1607386 :     return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
    1073             :   }
    1074             : 
    1075             :   return nullptr;
    1076             : }
    1077             : 
    1078     1819438 : Init *TernOpInit::Fold(Record *CurRec) const {
    1079     1819438 :   switch (getOpcode()) {
    1080      336045 :   case SUBST: {
    1081      336045 :     DefInit *LHSd = dyn_cast<DefInit>(LHS);
    1082             :     VarInit *LHSv = dyn_cast<VarInit>(LHS);
    1083             :     StringInit *LHSs = dyn_cast<StringInit>(LHS);
    1084             : 
    1085      336045 :     DefInit *MHSd = dyn_cast<DefInit>(MHS);
    1086             :     VarInit *MHSv = dyn_cast<VarInit>(MHS);
    1087             :     StringInit *MHSs = dyn_cast<StringInit>(MHS);
    1088             : 
    1089      336045 :     DefInit *RHSd = dyn_cast<DefInit>(RHS);
    1090             :     VarInit *RHSv = dyn_cast<VarInit>(RHS);
    1091             :     StringInit *RHSs = dyn_cast<StringInit>(RHS);
    1092             : 
    1093      336045 :     if (LHSd && MHSd && RHSd) {
    1094      285894 :       Record *Val = RHSd->getDef();
    1095      857682 :       if (LHSd->getAsString() == RHSd->getAsString())
    1096       27541 :         Val = MHSd->getDef();
    1097      285894 :       return DefInit::get(Val);
    1098             :     }
    1099       50151 :     if (LHSv && MHSv && RHSv) {
    1100           0 :       std::string Val = RHSv->getName();
    1101           0 :       if (LHSv->getAsString() == RHSv->getAsString())
    1102           0 :         Val = MHSv->getName();
    1103           0 :       return VarInit::get(Val, getType());
    1104             :     }
    1105       50151 :     if (LHSs && MHSs && RHSs) {
    1106             :       std::string Val = RHSs->getValue();
    1107             : 
    1108             :       std::string::size_type found;
    1109             :       std::string::size_type idx = 0;
    1110             :       while (true) {
    1111        9154 :         found = Val.find(LHSs->getValue(), idx);
    1112        6503 :         if (found == std::string::npos)
    1113             :           break;
    1114        2651 :         Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
    1115        2651 :         idx = found + MHSs->getValue().size();
    1116             :       }
    1117             : 
    1118        3852 :       return StringInit::get(Val);
    1119             :     }
    1120             :     break;
    1121             :   }
    1122             : 
    1123      836085 :   case FOREACH: {
    1124      836085 :     if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
    1125             :       return Result;
    1126             :     break;
    1127             :   }
    1128             : 
    1129      464398 :   case IF: {
    1130      464398 :     if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
    1131      464398 :                             LHS->convertInitializerTo(IntRecTy::get()))) {
    1132           0 :       if (LHSi->getValue())
    1133           0 :         return MHS;
    1134           0 :       return RHS;
    1135             :     }
    1136             :     break;
    1137             :   }
    1138             : 
    1139      182910 :   case DAG: {
    1140      182910 :     ListInit *MHSl = dyn_cast<ListInit>(MHS);
    1141      182910 :     ListInit *RHSl = dyn_cast<ListInit>(RHS);
    1142      182910 :     bool MHSok = MHSl || isa<UnsetInit>(MHS);
    1143      182910 :     bool RHSok = RHSl || isa<UnsetInit>(RHS);
    1144             : 
    1145      182910 :     if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
    1146             :       break; // Typically prevented by the parser, but might happen with template args
    1147             : 
    1148      182910 :     if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
    1149             :       SmallVector<std::pair<Init *, StringInit *>, 8> Children;
    1150      140155 :       unsigned Size = MHSl ? MHSl->size() : RHSl->size();
    1151     1042767 :       for (unsigned i = 0; i != Size; ++i) {
    1152      451339 :         Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
    1153      451341 :         Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
    1154      451337 :         if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
    1155          31 :           return const_cast<TernOpInit *>(this);
    1156      451306 :         Children.emplace_back(Node, dyn_cast<StringInit>(Name));
    1157             :       }
    1158      140124 :       return DagInit::get(LHS, nullptr, Children);
    1159             :     }
    1160             :     break;
    1161             :   }
    1162             :   }
    1163             : 
    1164      564828 :   return const_cast<TernOpInit *>(this);
    1165             : }
    1166             : 
    1167    13833116 : Init *TernOpInit::resolveReferences(Resolver &R) const {
    1168    13833116 :   Init *lhs = LHS->resolveReferences(R);
    1169             : 
    1170    13833116 :   if (getOpcode() == IF && lhs != LHS) {
    1171     5095676 :     if (IntInit *Value = dyn_cast_or_null<IntInit>(
    1172     5095676 :                              lhs->convertInitializerTo(IntRecTy::get()))) {
    1173             :       // Short-circuit
    1174     5026948 :       if (Value->getValue())
    1175     2206304 :         return MHS->resolveReferences(R);
    1176     2820644 :       return RHS->resolveReferences(R);
    1177             :     }
    1178             :   }
    1179             : 
    1180     8806168 :   Init *mhs = MHS->resolveReferences(R);
    1181             :   Init *rhs;
    1182             : 
    1183     8806168 :   if (getOpcode() == FOREACH) {
    1184             :     ShadowResolver SR(R);
    1185             :     SR.addShadow(lhs);
    1186     1352332 :     rhs = RHS->resolveReferences(SR);
    1187             :   } else {
    1188     7453836 :     rhs = RHS->resolveReferences(R);
    1189             :   }
    1190             : 
    1191     8806168 :   if (LHS != lhs || MHS != mhs || RHS != rhs)
    1192     1799874 :     return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
    1193     3599748 :         ->Fold(R.getCurrentRecord());
    1194     7006294 :   return const_cast<TernOpInit *>(this);
    1195             : }
    1196             : 
    1197         108 : std::string TernOpInit::getAsString() const {
    1198             :   std::string Result;
    1199             :   bool UnquotedLHS = false;
    1200         108 :   switch (getOpcode()) {
    1201             :   case SUBST: Result = "!subst"; break;
    1202          24 :   case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
    1203             :   case IF: Result = "!if"; break;
    1204             :   case DAG: Result = "!dag"; break;
    1205             :   }
    1206         324 :   return (Result + "(" +
    1207         432 :           (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
    1208         864 :           ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
    1209             : }
    1210             : 
    1211      581462 : static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B,
    1212             :                               Init *Start, Init *List, Init *Expr,
    1213             :                               RecTy *Type) {
    1214      581462 :   ID.AddPointer(Start);
    1215      581462 :   ID.AddPointer(List);
    1216      581462 :   ID.AddPointer(A);
    1217      581462 :   ID.AddPointer(B);
    1218      581462 :   ID.AddPointer(Expr);
    1219      581462 :   ID.AddPointer(Type);
    1220      581462 : }
    1221             : 
    1222      221880 : FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
    1223             :                             Init *Expr, RecTy *Type) {
    1224      222063 :   static FoldingSet<FoldOpInit> ThePool;
    1225             : 
    1226             :   FoldingSetNodeID ID;
    1227      221880 :   ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
    1228             : 
    1229      221880 :   void *IP = nullptr;
    1230      181755 :   if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1231             :     return I;
    1232             : 
    1233             :   FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
    1234       40125 :   ThePool.InsertNode(I, IP);
    1235       40125 :   return I;
    1236             : }
    1237             : 
    1238      359582 : void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
    1239      359582 :   ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
    1240      359582 : }
    1241             : 
    1242      221880 : Init *FoldOpInit::Fold(Record *CurRec) const {
    1243      221880 :   if (ListInit *LI = dyn_cast<ListInit>(List)) {
    1244      221321 :     Init *Accum = Start;
    1245      879631 :     for (Init *Elt : *LI) {
    1246             :       MapResolver R(CurRec);
    1247      329155 :       R.set(A, Accum);
    1248      329155 :       R.set(B, Elt);
    1249      329155 :       Accum = Expr->resolveReferences(R);
    1250             :     }
    1251             :     return Accum;
    1252             :   }
    1253         559 :   return const_cast<FoldOpInit *>(this);
    1254             : }
    1255             : 
    1256      342820 : Init *FoldOpInit::resolveReferences(Resolver &R) const {
    1257      342820 :   Init *NewStart = Start->resolveReferences(R);
    1258      342820 :   Init *NewList = List->resolveReferences(R);
    1259             :   ShadowResolver SR(R);
    1260      342820 :   SR.addShadow(A);
    1261      342820 :   SR.addShadow(B);
    1262      342820 :   Init *NewExpr = Expr->resolveReferences(SR);
    1263             : 
    1264      342820 :   if (Start == NewStart && List == NewList && Expr == NewExpr)
    1265      121500 :     return const_cast<FoldOpInit *>(this);
    1266             : 
    1267      221320 :   return get(NewStart, NewList, A, B, NewExpr, getType())
    1268      442640 :       ->Fold(R.getCurrentRecord());
    1269             : }
    1270             : 
    1271           0 : Init *FoldOpInit::getBit(unsigned Bit) const {
    1272           0 :   return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
    1273             : }
    1274             : 
    1275           4 : std::string FoldOpInit::getAsString() const {
    1276          20 :   return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
    1277          24 :           ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
    1278          16 :           ", " + Expr->getAsString() + ")")
    1279           8 :       .str();
    1280             : }
    1281             : 
    1282             : static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType,
    1283             :                              Init *Expr) {
    1284     1655318 :   ID.AddPointer(CheckType);
    1285     1655318 :   ID.AddPointer(Expr);
    1286             : }
    1287             : 
    1288      807846 : IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
    1289      808027 :   static FoldingSet<IsAOpInit> ThePool;
    1290             : 
    1291             :   FoldingSetNodeID ID;
    1292             :   ProfileIsAOpInit(ID, CheckType, Expr);
    1293             : 
    1294      807846 :   void *IP = nullptr;
    1295      806400 :   if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1296             :     return I;
    1297             : 
    1298             :   IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
    1299        1446 :   ThePool.InsertNode(I, IP);
    1300        1446 :   return I;
    1301             : }
    1302             : 
    1303      847472 : void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
    1304      847472 :   ProfileIsAOpInit(ID, CheckType, Expr);
    1305      847472 : }
    1306             : 
    1307      807846 : Init *IsAOpInit::Fold() const {
    1308      807846 :   if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
    1309             :     // Is the expression type known to be (a subclass of) the desired type?
    1310      807846 :     if (TI->getType()->typeIsConvertibleTo(CheckType))
    1311      434222 :       return IntInit::get(1);
    1312             : 
    1313      747248 :     if (isa<RecordRecTy>(CheckType)) {
    1314             :       // If the target type is not a subclass of the expression type, or if
    1315             :       // the expression has fully resolved to a record, we know that it can't
    1316             :       // be of the required type.
    1317      747248 :       if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
    1318      373082 :         return IntInit::get(0);
    1319             :     } else {
    1320             :       // We treat non-record types as not castable.
    1321           0 :       return IntInit::get(0);
    1322             :     }
    1323             :   }
    1324         542 :   return const_cast<IsAOpInit *>(this);
    1325             : }
    1326             : 
    1327     1170124 : Init *IsAOpInit::resolveReferences(Resolver &R) const {
    1328     1170124 :   Init *NewExpr = Expr->resolveReferences(R);
    1329     1170124 :   if (Expr != NewExpr)
    1330      807304 :     return get(CheckType, NewExpr)->Fold();
    1331      362820 :   return const_cast<IsAOpInit *>(this);
    1332             : }
    1333             : 
    1334           0 : Init *IsAOpInit::getBit(unsigned Bit) const {
    1335           0 :   return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
    1336             : }
    1337             : 
    1338           2 : std::string IsAOpInit::getAsString() const {
    1339           6 :   return (Twine("!isa<") + CheckType->getAsString() + ">(" +
    1340           8 :           Expr->getAsString() + ")")
    1341           4 :       .str();
    1342             : }
    1343             : 
    1344      274426 : RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
    1345      274426 :   if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
    1346      274426 :     for (Record *Rec : RecordType->getClasses()) {
    1347      274426 :       if (RecordVal *Field = Rec->getValue(FieldName))
    1348      274426 :         return Field->getType();
    1349             :     }
    1350             :   }
    1351             :   return nullptr;
    1352             : }
    1353             : 
    1354             : Init *
    1355      865864 : TypedInit::convertInitializerTo(RecTy *Ty) const {
    1356      865864 :   if (getType() == Ty || getType()->typeIsA(Ty))
    1357      340060 :     return const_cast<TypedInit *>(this);
    1358             : 
    1359     1388423 :   if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
    1360        7332 :       cast<BitsRecTy>(Ty)->getNumBits() == 1)
    1361       14664 :     return BitsInit::get({const_cast<TypedInit *>(this)});
    1362             : 
    1363             :   return nullptr;
    1364             : }
    1365             : 
    1366       44716 : Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
    1367       44716 :   BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
    1368             :   if (!T) return nullptr;  // Cannot subscript a non-bits variable.
    1369       44716 :   unsigned NumBits = T->getNumBits();
    1370             : 
    1371             :   SmallVector<Init *, 16> NewBits;
    1372       44716 :   NewBits.reserve(Bits.size());
    1373      371450 :   for (unsigned Bit : Bits) {
    1374      163367 :     if (Bit >= NumBits)
    1375             :       return nullptr;
    1376             : 
    1377      163367 :     NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
    1378             :   }
    1379       44716 :   return BitsInit::get(NewBits);
    1380             : }
    1381             : 
    1382   311652138 : Init *TypedInit::getCastTo(RecTy *Ty) const {
    1383             :   // Handle the common case quickly
    1384   311652138 :   if (getType() == Ty || getType()->typeIsA(Ty))
    1385   311019919 :     return const_cast<TypedInit *>(this);
    1386             : 
    1387      632219 :   if (Init *Converted = convertInitializerTo(Ty)) {
    1388             :     assert(!isa<TypedInit>(Converted) ||
    1389             :            cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
    1390             :     return Converted;
    1391             :   }
    1392             : 
    1393        4125 :   if (!getType()->typeIsConvertibleTo(Ty))
    1394             :     return nullptr;
    1395             : 
    1396             :   return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
    1397        4115 :       ->Fold(nullptr);
    1398             : }
    1399             : 
    1400         302 : Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
    1401         302 :   ListRecTy *T = dyn_cast<ListRecTy>(getType());
    1402             :   if (!T) return nullptr;  // Cannot subscript a non-list variable.
    1403             : 
    1404         302 :   if (Elements.size() == 1)
    1405         301 :     return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
    1406             : 
    1407             :   SmallVector<Init*, 8> ListInits;
    1408           1 :   ListInits.reserve(Elements.size());
    1409           5 :   for (unsigned Element : Elements)
    1410           2 :     ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
    1411             :                                                 Element));
    1412           2 :   return ListInit::get(ListInits, T->getElementType());
    1413             : }
    1414             : 
    1415             : 
    1416           0 : VarInit *VarInit::get(StringRef VN, RecTy *T) {
    1417           0 :   Init *Value = StringInit::get(VN);
    1418           0 :   return VarInit::get(Value, T);
    1419             : }
    1420             : 
    1421     1066736 : VarInit *VarInit::get(Init *VN, RecTy *T) {
    1422             :   using Key = std::pair<RecTy *, Init *>;
    1423     1067062 :   static DenseMap<Key, VarInit*> ThePool;
    1424             : 
    1425     1066736 :   Key TheKey(std::make_pair(T, VN));
    1426             : 
    1427             :   VarInit *&I = ThePool[TheKey];
    1428     1066736 :   if (!I)
    1429      603896 :     I = new(Allocator) VarInit(VN, T);
    1430     1066736 :   return I;
    1431             : }
    1432             : 
    1433     7611403 : StringRef VarInit::getName() const {
    1434     7611403 :   StringInit *NameString = cast<StringInit>(getNameInit());
    1435     7611403 :   return NameString->getValue();
    1436             : }
    1437             : 
    1438    94247411 : Init *VarInit::getBit(unsigned Bit) const {
    1439    94247411 :   if (getType() == BitRecTy::get())
    1440    25420586 :     return const_cast<VarInit*>(this);
    1441    68826825 :   return VarBitInit::get(const_cast<VarInit*>(this), Bit);
    1442             : }
    1443             : 
    1444   124182656 : Init *VarInit::resolveReferences(Resolver &R) const {
    1445   124182656 :   if (Init *Val = R.resolve(VarName))
    1446             :     return Val;
    1447    73907049 :   return const_cast<VarInit *>(this);
    1448             : }
    1449             : 
    1450   108285223 : VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
    1451             :   using Key = std::pair<TypedInit *, unsigned>;
    1452   108285406 :   static DenseMap<Key, VarBitInit*> ThePool;
    1453             : 
    1454   108285223 :   Key TheKey(std::make_pair(T, B));
    1455             : 
    1456             :   VarBitInit *&I = ThePool[TheKey];
    1457   108285223 :   if (!I)
    1458      634751 :     I = new(Allocator) VarBitInit(T, B);
    1459   108285223 :   return I;
    1460             : }
    1461             : 
    1462         238 : std::string VarBitInit::getAsString() const {
    1463        1190 :   return TI->getAsString() + "{" + utostr(Bit) + "}";
    1464             : }
    1465             : 
    1466        1230 : Init *VarBitInit::resolveReferences(Resolver &R) const {
    1467        1230 :   Init *I = TI->resolveReferences(R);
    1468        1230 :   if (TI != I)
    1469         616 :     return I->getBit(getBitNum());
    1470             : 
    1471         614 :   return const_cast<VarBitInit*>(this);
    1472             : }
    1473             : 
    1474         355 : VarListElementInit *VarListElementInit::get(TypedInit *T,
    1475             :                                             unsigned E) {
    1476             :   using Key = std::pair<TypedInit *, unsigned>;
    1477         544 :   static DenseMap<Key, VarListElementInit*> ThePool;
    1478             : 
    1479         355 :   Key TheKey(std::make_pair(T, E));
    1480             : 
    1481             :   VarListElementInit *&I = ThePool[TheKey];
    1482         695 :   if (!I) I = new(Allocator) VarListElementInit(T, E);
    1483         355 :   return I;
    1484             : }
    1485             : 
    1486          25 : std::string VarListElementInit::getAsString() const {
    1487         125 :   return TI->getAsString() + "[" + utostr(Element) + "]";
    1488             : }
    1489             : 
    1490       28211 : Init *VarListElementInit::resolveReferences(Resolver &R) const {
    1491       28211 :   Init *NewTI = TI->resolveReferences(R);
    1492             :   if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
    1493             :     // Leave out-of-bounds array references as-is. This can happen without
    1494             :     // being an error, e.g. in the untaken "branch" of an !if expression.
    1495       14978 :     if (getElementNum() < List->size())
    1496       10868 :       return List->getElement(getElementNum());
    1497             :   }
    1498       17343 :   if (NewTI != TI && isa<TypedInit>(NewTI))
    1499          52 :     return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
    1500       17291 :   return const_cast<VarListElementInit *>(this);
    1501             : }
    1502             : 
    1503           0 : Init *VarListElementInit::getBit(unsigned Bit) const {
    1504           0 :   if (getType() == BitRecTy::get())
    1505           0 :     return const_cast<VarListElementInit*>(this);
    1506           0 :   return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
    1507             : }
    1508             : 
    1509      913502 : DefInit::DefInit(Record *D)
    1510     1827004 :     : TypedInit(IK_DefInit, D->getType()), Def(D) {}
    1511             : 
    1512     2528859 : DefInit *DefInit::get(Record *R) {
    1513     2528859 :   return R->getDefInit();
    1514             : }
    1515             : 
    1516       25292 : Init *DefInit::convertInitializerTo(RecTy *Ty) const {
    1517             :   if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
    1518       25292 :     if (getType()->typeIsConvertibleTo(RRT))
    1519       25292 :       return const_cast<DefInit *>(this);
    1520             :   return nullptr;
    1521             : }
    1522             : 
    1523     1461715 : RecTy *DefInit::getFieldType(StringInit *FieldName) const {
    1524     2923430 :   if (const RecordVal *RV = Def->getValue(FieldName))
    1525     1461715 :     return RV->getType();
    1526             :   return nullptr;
    1527             : }
    1528             : 
    1529     4570353 : std::string DefInit::getAsString() const {
    1530     9140706 :   return Def->getName();
    1531             : }
    1532             : 
    1533     7273576 : static void ProfileVarDefInit(FoldingSetNodeID &ID,
    1534             :                               Record *Class,
    1535             :                               ArrayRef<Init *> Args) {
    1536     7273576 :   ID.AddInteger(Args.size());
    1537     7273576 :   ID.AddPointer(Class);
    1538             : 
    1539    41291724 :   for (Init *I : Args)
    1540    17009074 :     ID.AddPointer(I);
    1541     7273576 : }
    1542             : 
    1543     2857438 : VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
    1544     2857662 :   static FoldingSet<VarDefInit> ThePool;
    1545             : 
    1546             :   FoldingSetNodeID ID;
    1547     2857438 :   ProfileVarDefInit(ID, Class, Args);
    1548             : 
    1549     2857438 :   void *IP = nullptr;
    1550     2584694 :   if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1551             :     return I;
    1552             : 
    1553             :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
    1554      272744 :                                  alignof(VarDefInit));
    1555      272744 :   VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
    1556             :   std::uninitialized_copy(Args.begin(), Args.end(),
    1557             :                           I->getTrailingObjects<Init *>());
    1558      272744 :   ThePool.InsertNode(I, IP);
    1559      272744 :   return I;
    1560             : }
    1561             : 
    1562     4416138 : void VarDefInit::Profile(FoldingSetNodeID &ID) const {
    1563     4416138 :   ProfileVarDefInit(ID, Class, args());
    1564     4416138 : }
    1565             : 
    1566     2260676 : DefInit *VarDefInit::instantiate() {
    1567     2260676 :   if (!Def) {
    1568      254610 :     RecordKeeper &Records = Class->getRecords();
    1569      254610 :     auto NewRecOwner = make_unique<Record>(Records.getNewAnonymousName(),
    1570      509220 :                                            Class->getLoc(), Records,
    1571      509220 :                                            /*IsAnonymous=*/true);
    1572             :     Record *NewRec = NewRecOwner.get();
    1573             : 
    1574             :     // Copy values from class to instance
    1575     5339036 :     for (const RecordVal &Val : Class->getValues())
    1576             :       NewRec->addValue(Val);
    1577             : 
    1578             :     // Substitute and resolve template arguments
    1579      254610 :     ArrayRef<Init *> TArgs = Class->getTemplateArgs();
    1580             :     MapResolver R(NewRec);
    1581             : 
    1582      961696 :     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
    1583      707086 :       if (i < args_size())
    1584      636497 :         R.set(TArgs[i], getArg(i));
    1585             :       else
    1586      141178 :         R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
    1587             : 
    1588      707086 :       NewRec->removeValue(TArgs[i]);
    1589             :     }
    1590             : 
    1591      254610 :     NewRec->resolveReferences(R);
    1592             : 
    1593             :     // Add superclasses.
    1594      254610 :     ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
    1595      538956 :     for (const auto &SCPair : SCs)
    1596      142173 :       NewRec->addSuperClass(SCPair.first, SCPair.second);
    1597             : 
    1598             :     NewRec->addSuperClass(Class,
    1599             :                           SMRange(Class->getLoc().back(),
    1600      254610 :                                   Class->getLoc().back()));
    1601             : 
    1602             :     // Resolve internal references and store in record keeper
    1603      254610 :     NewRec->resolveReferences();
    1604      254610 :     Records.addDef(std::move(NewRecOwner));
    1605             : 
    1606      254610 :     Def = DefInit::get(NewRec);
    1607             :   }
    1608             : 
    1609     2260676 :   return Def;
    1610             : }
    1611             : 
    1612     3150383 : Init *VarDefInit::resolveReferences(Resolver &R) const {
    1613             :   TrackUnresolvedResolver UR(&R);
    1614             :   bool Changed = false;
    1615             :   SmallVector<Init *, 8> NewArgs;
    1616     6300766 :   NewArgs.reserve(args_size());
    1617             : 
    1618    17714403 :   for (Init *Arg : args()) {
    1619     7282010 :     Init *NewArg = Arg->resolveReferences(UR);
    1620     7282010 :     NewArgs.push_back(NewArg);
    1621     7282010 :     Changed |= NewArg != Arg;
    1622             :   }
    1623             : 
    1624     3150383 :   if (Changed) {
    1625     2563518 :     auto New = VarDefInit::get(Class, NewArgs);
    1626     2563518 :     if (!UR.foundUnresolved())
    1627     2177102 :       return New->instantiate();
    1628             :     return New;
    1629             :   }
    1630      586865 :   return const_cast<VarDefInit *>(this);
    1631             : }
    1632             : 
    1633      293920 : Init *VarDefInit::Fold() const {
    1634      293920 :   if (Def)
    1635             :     return Def;
    1636             : 
    1637             :   TrackUnresolvedResolver R;
    1638      459994 :   for (Init *Arg : args())
    1639      184786 :     Arg->resolveReferences(R);
    1640             : 
    1641       90422 :   if (!R.foundUnresolved())
    1642       83574 :     return const_cast<VarDefInit *>(this)->instantiate();
    1643        6848 :   return const_cast<VarDefInit *>(this);
    1644             : }
    1645             : 
    1646           8 : std::string VarDefInit::getAsString() const {
    1647          24 :   std::string Result = Class->getNameInitAsString() + "<";
    1648             :   const char *sep = "";
    1649          28 :   for (Init *Arg : args()) {
    1650             :     Result += sep;
    1651             :     sep = ", ";
    1652          20 :     Result += Arg->getAsString();
    1653             :   }
    1654          16 :   return Result + ">";
    1655             : }
    1656             : 
    1657    19066483 : FieldInit *FieldInit::get(Init *R, StringInit *FN) {
    1658             :   using Key = std::pair<Init *, StringInit *>;
    1659    19066697 :   static DenseMap<Key, FieldInit*> ThePool;
    1660             : 
    1661    19066483 :   Key TheKey(std::make_pair(R, FN));
    1662             : 
    1663             :   FieldInit *&I = ThePool[TheKey];
    1664    20691513 :   if (!I) I = new(Allocator) FieldInit(R, FN);
    1665    19066483 :   return I;
    1666             : }
    1667             : 
    1668    27470616 : Init *FieldInit::getBit(unsigned Bit) const {
    1669    27470616 :   if (getType() == BitRecTy::get())
    1670        2898 :     return const_cast<FieldInit*>(this);
    1671    27467718 :   return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
    1672             : }
    1673             : 
    1674    33735183 : Init *FieldInit::resolveReferences(Resolver &R) const {
    1675    33735183 :   Init *NewRec = Rec->resolveReferences(R);
    1676    33735183 :   if (NewRec != Rec)
    1677    18955372 :     return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
    1678    14779811 :   return const_cast<FieldInit *>(this);
    1679             : }
    1680             : 
    1681    19066483 : Init *FieldInit::Fold(Record *CurRec) const {
    1682    19066483 :   if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
    1683    18392281 :     Record *Def = DI->getDef();
    1684    18392281 :     if (Def == CurRec)
    1685           1 :       PrintFatalError(CurRec->getLoc(),
    1686             :                       Twine("Attempting to access field '") +
    1687           1 :                       FieldName->getAsUnquotedString() + "' of '" +
    1688           1 :                       Rec->getAsString() + "' is a forbidden self-reference");
    1689    36784560 :     Init *FieldVal = Def->getValue(FieldName)->getValue();
    1690    18392280 :     if (FieldVal->isComplete())
    1691             :       return FieldVal;
    1692             :   }
    1693      674202 :   return const_cast<FieldInit *>(this);
    1694             : }
    1695             : 
    1696    16629638 : static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
    1697             :                            ArrayRef<Init *> ArgRange,
    1698             :                            ArrayRef<StringInit *> NameRange) {
    1699    16629638 :   ID.AddPointer(V);
    1700    16629638 :   ID.AddPointer(VN);
    1701             : 
    1702             :   ArrayRef<Init *>::iterator Arg = ArgRange.begin();
    1703    16629638 :   ArrayRef<StringInit *>::iterator Name = NameRange.begin();
    1704    63497180 :   while (Arg != ArgRange.end()) {
    1705             :     assert(Name != NameRange.end() && "Arg name underflow!");
    1706    46867542 :     ID.AddPointer(*Arg++);
    1707    46867542 :     ID.AddPointer(*Name++);
    1708             :   }
    1709             :   assert(Name == NameRange.end() && "Arg name overflow!");
    1710    16629638 : }
    1711             : 
    1712             : DagInit *
    1713     5694429 : DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
    1714             :              ArrayRef<StringInit *> NameRange) {
    1715     5694624 :   static FoldingSet<DagInit> ThePool;
    1716             : 
    1717             :   FoldingSetNodeID ID;
    1718     5694429 :   ProfileDagInit(ID, V, VN, ArgRange, NameRange);
    1719             : 
    1720     5694429 :   void *IP = nullptr;
    1721     3375049 :   if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1722             :     return I;
    1723             : 
    1724     2319380 :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
    1725     2319380 :   DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
    1726             :   std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
    1727             :                           I->getTrailingObjects<Init *>());
    1728             :   std::uninitialized_copy(NameRange.begin(), NameRange.end(),
    1729             :                           I->getTrailingObjects<StringInit *>());
    1730     2319380 :   ThePool.InsertNode(I, IP);
    1731     2319380 :   return I;
    1732             : }
    1733             : 
    1734             : DagInit *
    1735     1317849 : DagInit::get(Init *V, StringInit *VN,
    1736             :              ArrayRef<std::pair<Init*, StringInit*>> args) {
    1737             :   SmallVector<Init *, 8> Args;
    1738             :   SmallVector<StringInit *, 8> Names;
    1739             : 
    1740     5862147 :   for (const auto &Arg : args) {
    1741     2272149 :     Args.push_back(Arg.first);
    1742     2272149 :     Names.push_back(Arg.second);
    1743             :   }
    1744             : 
    1745     2635698 :   return DagInit::get(V, VN, Args, Names);
    1746             : }
    1747             : 
    1748    10935209 : void DagInit::Profile(FoldingSetNodeID &ID) const {
    1749    10935209 :   ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
    1750    10935209 : }
    1751             : 
    1752    19419267 : Init *DagInit::resolveReferences(Resolver &R) const {
    1753             :   SmallVector<Init*, 8> NewArgs;
    1754    38838534 :   NewArgs.reserve(arg_size());
    1755             :   bool ArgsChanged = false;
    1756    90024511 :   for (const Init *Arg : getArgs()) {
    1757    35302622 :     Init *NewArg = Arg->resolveReferences(R);
    1758    35302622 :     NewArgs.push_back(NewArg);
    1759    35302622 :     ArgsChanged |= NewArg != Arg;
    1760             :   }
    1761             : 
    1762    19419267 :   Init *Op = Val->resolveReferences(R);
    1763    19419267 :   if (Op != Val || ArgsChanged)
    1764     3347529 :     return DagInit::get(Op, ValName, NewArgs, getArgNames());
    1765             : 
    1766    16071738 :   return const_cast<DagInit *>(this);
    1767             : }
    1768             : 
    1769     6162897 : bool DagInit::isConcrete() const {
    1770     6162897 :   if (!Val->isConcrete())
    1771             :     return false;
    1772    33218323 :   for (const Init *Elt : getArgs()) {
    1773    13527713 :     if (!Elt->isConcrete())
    1774             :       return false;
    1775             :   }
    1776             :   return true;
    1777             : }
    1778             : 
    1779        4632 : std::string DagInit::getAsString() const {
    1780        9264 :   std::string Result = "(" + Val->getAsString();
    1781        4632 :   if (ValName)
    1782           3 :     Result += ":" + ValName->getAsUnquotedString();
    1783        4632 :   if (!arg_empty()) {
    1784       13866 :     Result += " " + getArg(0)->getAsString();
    1785       11140 :     if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
    1786       13325 :     for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
    1787       26109 :       Result += ", " + getArg(i)->getAsString();
    1788       20235 :       if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
    1789             :     }
    1790             :   }
    1791        9264 :   return Result + ")";
    1792             : }
    1793             : 
    1794             : //===----------------------------------------------------------------------===//
    1795             : //    Other implementations
    1796             : //===----------------------------------------------------------------------===//
    1797             : 
    1798      581782 : RecordVal::RecordVal(Init *N, RecTy *T, bool P)
    1799      581782 :   : Name(N), TyAndPrefix(T, P) {
    1800      581782 :   setValue(UnsetInit::get());
    1801             :   assert(Value && "Cannot create unset value for current type!");
    1802      581782 : }
    1803             : 
    1804     1813191 : StringRef RecordVal::getName() const {
    1805     1813191 :   return cast<StringInit>(getNameInit())->getValue();
    1806             : }
    1807             : 
    1808   325696040 : bool RecordVal::setValue(Init *V) {
    1809   325696040 :   if (V) {
    1810   651392080 :     Value = V->getCastTo(getType());
    1811   325696040 :     if (Value) {
    1812             :       assert(!isa<TypedInit>(Value) ||
    1813             :              cast<TypedInit>(Value)->getType()->typeIsA(getType()));
    1814             :       if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
    1815    33211153 :         if (!isa<BitsInit>(Value)) {
    1816             :           SmallVector<Init *, 64> Bits;
    1817      161031 :           Bits.reserve(BTy->getNumBits());
    1818     1294451 :           for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i)
    1819     1133420 :             Bits.push_back(Value->getBit(i));
    1820      161031 :           Value = BitsInit::get(Bits);
    1821             :         }
    1822             :       }
    1823             :     }
    1824   325696040 :     return Value == nullptr;
    1825             :   }
    1826           0 :   Value = nullptr;
    1827           0 :   return false;
    1828             : }
    1829             : 
    1830             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    1831             : LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
    1832             : #endif
    1833             : 
    1834        1826 : void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
    1835        1826 :   if (getPrefix()) OS << "field ";
    1836        3652 :   OS << *getType() << " " << getNameInitAsString();
    1837             : 
    1838        1826 :   if (getValue())
    1839        1817 :     OS << " = " << *getValue();
    1840             : 
    1841        1826 :   if (PrintSem) OS << ";\n";
    1842        1826 : }
    1843             : 
    1844             : unsigned Record::LastID = 0;
    1845             : 
    1846     3639692 : void Record::checkName() {
    1847             :   // Ensure the record name has string type.
    1848     3639692 :   const TypedInit *TypedName = cast<const TypedInit>(Name);
    1849     7279384 :   if (!isa<StringRecTy>(TypedName->getType()))
    1850           0 :     PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
    1851             :                                   "' is not a string!");
    1852     3639692 : }
    1853             : 
    1854      914012 : RecordRecTy *Record::getType() {
    1855             :   SmallVector<Record *, 4> DirectSCs;
    1856      914012 :   getDirectSuperClasses(DirectSCs);
    1857     1828024 :   return RecordRecTy::get(DirectSCs);
    1858             : }
    1859             : 
    1860     9640357 : DefInit *Record::getDefInit() {
    1861     9640357 :   if (!TheInit)
    1862      913502 :     TheInit = new(Allocator) DefInit(this);
    1863     9640357 :   return TheInit;
    1864             : }
    1865             : 
    1866     1156375 : void Record::setName(Init *NewName) {
    1867     1156375 :   Name = NewName;
    1868     1156375 :   checkName();
    1869             :   // DO NOT resolve record values to the name at this point because
    1870             :   // there might be default values for arguments of this def.  Those
    1871             :   // arguments might not have been resolved yet so we don't want to
    1872             :   // prematurely assume values for those arguments were not passed to
    1873             :   // this def.
    1874             :   //
    1875             :   // Nonetheless, it may be that some of this Record's values
    1876             :   // reference the record name.  Indeed, the reason for having the
    1877             :   // record name be an Init is to provide this flexibility.  The extra
    1878             :   // resolve steps after completely instantiating defs takes care of
    1879             :   // this.  See TGParser::ParseDef and TGParser::ParseDefm.
    1880     1156375 : }
    1881             : 
    1882     1088405 : void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
    1883             :   ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
    1884     3821643 :   while (!SCs.empty()) {
    1885             :     // Superclasses are in reverse preorder, so 'back' is a direct superclass,
    1886             :     // and its transitive superclasses are directly preceding it.
    1887     1366619 :     Record *SC = SCs.back().first;
    1888     1366619 :     SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
    1889     1366619 :     Classes.push_back(SC);
    1890             :   }
    1891     1088405 : }
    1892             : 
    1893    10813900 : void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
    1894   641937832 :   for (RecordVal &Value : Values) {
    1895   315561969 :     if (SkipVal == &Value) // Skip resolve the same field as the given one
    1896           0 :       continue;
    1897   315561969 :     if (Init *V = Value.getValue()) {
    1898   315561953 :       Init *VR = V->resolveReferences(R);
    1899   315561950 :       if (Value.setValue(VR)) {
    1900             :         std::string Type;
    1901             :         if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
    1902           0 :           Type =
    1903           0 :               (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
    1904           0 :         PrintFatalError(getLoc(), Twine("Invalid value ") + Type +
    1905             :                                       "is found when setting '" +
    1906             :                                       Value.getNameInitAsString() +
    1907             :                                       "' of type '" +
    1908           0 :                                       Value.getType()->getAsString() +
    1909             :                                       "' after resolving references: " +
    1910           0 :                                       VR->getAsUnquotedString() + "\n");
    1911             :       }
    1912             :     }
    1913             :   }
    1914    10813897 :   Init *OldName = getNameInit();
    1915    10813897 :   Init *NewName = Name->resolveReferences(R);
    1916    10813897 :   if (NewName != OldName) {
    1917             :     // Re-register with RecordKeeper.
    1918      928212 :     setName(NewName);
    1919             :   }
    1920    10813897 : }
    1921             : 
    1922     3200786 : void Record::resolveReferences() {
    1923     3200784 :   RecordResolver R(*this);
    1924             :   R.setFinal(true);
    1925     3200786 :   resolveReferences(R);
    1926     3200784 : }
    1927             : 
    1928           0 : void Record::resolveReferencesTo(const RecordVal *RV) {
    1929             :   RecordValResolver R(*this, RV);
    1930           0 :   resolveReferences(R, RV);
    1931           0 : }
    1932             : 
    1933             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    1934             : LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
    1935             : #endif
    1936             : 
    1937         881 : raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
    1938         881 :   OS << R.getNameInitAsString();
    1939             : 
    1940             :   ArrayRef<Init *> TArgs = R.getTemplateArgs();
    1941         881 :   if (!TArgs.empty()) {
    1942         169 :     OS << "<";
    1943             :     bool NeedComma = false;
    1944         661 :     for (const Init *TA : TArgs) {
    1945         246 :       if (NeedComma) OS << ", ";
    1946             :       NeedComma = true;
    1947             :       const RecordVal *RV = R.getValue(TA);
    1948             :       assert(RV && "Template argument record not found??");
    1949         246 :       RV->print(OS, false);
    1950             :     }
    1951         169 :     OS << ">";
    1952             :   }
    1953             : 
    1954         881 :   OS << " {";
    1955             :   ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
    1956         881 :   if (!SC.empty()) {
    1957         628 :     OS << "\t//";
    1958        2062 :     for (const auto &SuperPair : SC)
    1959        1434 :       OS << " " << SuperPair.first->getNameInitAsString();
    1960             :   }
    1961         881 :   OS << "\n";
    1962             : 
    1963        4533 :   for (const RecordVal &Val : R.getValues())
    1964        1829 :     if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
    1965           3 :       OS << Val;
    1966        4533 :   for (const RecordVal &Val : R.getValues())
    1967        3649 :     if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
    1968        1577 :       OS << Val;
    1969             : 
    1970         881 :   return OS << "}\n";
    1971             : }
    1972             : 
    1973      717168 : Init *Record::getValueInit(StringRef FieldName) const {
    1974             :   const RecordVal *R = getValue(FieldName);
    1975      717168 :   if (!R || !R->getValue())
    1976           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    1977             :       "' does not have a field named `" + FieldName + "'!\n");
    1978      717168 :   return R->getValue();
    1979             : }
    1980             : 
    1981     9654209 : StringRef Record::getValueAsString(StringRef FieldName) const {
    1982             :   const RecordVal *R = getValue(FieldName);
    1983     9654209 :   if (!R || !R->getValue())
    1984           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    1985             :       "' does not have a field named `" + FieldName + "'!\n");
    1986             : 
    1987             :   if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
    1988             :     return SI->getValue();
    1989             :   if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
    1990             :     return CI->getValue();
    1991             : 
    1992           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    1993             :     FieldName + "' does not have a string initializer!");
    1994             : }
    1995             : 
    1996      769830 : BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
    1997             :   const RecordVal *R = getValue(FieldName);
    1998      769830 :   if (!R || !R->getValue())
    1999           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2000             :       "' does not have a field named `" + FieldName + "'!\n");
    2001             : 
    2002             :   if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
    2003      769830 :     return BI;
    2004           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2005             :     FieldName + "' does not have a BitsInit initializer!");
    2006             : }
    2007             : 
    2008     5725553 : ListInit *Record::getValueAsListInit(StringRef FieldName) const {
    2009             :   const RecordVal *R = getValue(FieldName);
    2010     5725553 :   if (!R || !R->getValue())
    2011           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2012             :       "' does not have a field named `" + FieldName + "'!\n");
    2013             : 
    2014             :   if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
    2015     5725553 :     return LI;
    2016           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2017             :     FieldName + "' does not have a list initializer!");
    2018             : }
    2019             : 
    2020             : std::vector<Record*>
    2021     4184564 : Record::getValueAsListOfDefs(StringRef FieldName) const {
    2022     4184564 :   ListInit *List = getValueAsListInit(FieldName);
    2023             :   std::vector<Record*> Defs;
    2024    15785314 :   for (Init *I : List->getValues()) {
    2025             :     if (DefInit *DI = dyn_cast<DefInit>(I))
    2026    11600750 :       Defs.push_back(DI->getDef());
    2027             :     else
    2028           0 :       PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2029             :         FieldName + "' list is not entirely DefInit!");
    2030             :   }
    2031     4184564 :   return Defs;
    2032             : }
    2033             : 
    2034     5880597 : int64_t Record::getValueAsInt(StringRef FieldName) const {
    2035             :   const RecordVal *R = getValue(FieldName);
    2036     5880597 :   if (!R || !R->getValue())
    2037           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2038             :       "' does not have a field named `" + FieldName + "'!\n");
    2039             : 
    2040             :   if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
    2041     5880597 :     return II->getValue();
    2042           0 :   PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
    2043             :                                 FieldName +
    2044             :                                 "' does not have an int initializer: " +
    2045           0 :                                 R->getValue()->getAsString());
    2046             : }
    2047             : 
    2048             : std::vector<int64_t>
    2049       69536 : Record::getValueAsListOfInts(StringRef FieldName) const {
    2050       69536 :   ListInit *List = getValueAsListInit(FieldName);
    2051             :   std::vector<int64_t> Ints;
    2052      144162 :   for (Init *I : List->getValues()) {
    2053             :     if (IntInit *II = dyn_cast<IntInit>(I))
    2054       74626 :       Ints.push_back(II->getValue());
    2055             :     else
    2056           0 :       PrintFatalError(getLoc(),
    2057           0 :                       Twine("Record `") + getName() + "', field `" + FieldName +
    2058             :                           "' does not have a list of ints initializer: " +
    2059           0 :                           I->getAsString());
    2060             :   }
    2061       69536 :   return Ints;
    2062             : }
    2063             : 
    2064             : std::vector<StringRef>
    2065       10973 : Record::getValueAsListOfStrings(StringRef FieldName) const {
    2066       10973 :   ListInit *List = getValueAsListInit(FieldName);
    2067             :   std::vector<StringRef> Strings;
    2068       34169 :   for (Init *I : List->getValues()) {
    2069             :     if (StringInit *SI = dyn_cast<StringInit>(I))
    2070       23196 :       Strings.push_back(SI->getValue());
    2071             :     else
    2072           0 :       PrintFatalError(getLoc(),
    2073           0 :                       Twine("Record `") + getName() + "', field `" + FieldName +
    2074             :                           "' does not have a list of strings initializer: " +
    2075           0 :                           I->getAsString());
    2076             :   }
    2077       10973 :   return Strings;
    2078             : }
    2079             : 
    2080    14417916 : Record *Record::getValueAsDef(StringRef FieldName) const {
    2081             :   const RecordVal *R = getValue(FieldName);
    2082    14417916 :   if (!R || !R->getValue())
    2083           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2084             :       "' does not have a field named `" + FieldName + "'!\n");
    2085             : 
    2086             :   if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
    2087    14417916 :     return DI->getDef();
    2088           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2089             :     FieldName + "' does not have a def initializer!");
    2090             : }
    2091             : 
    2092    35674644 : bool Record::getValueAsBit(StringRef FieldName) const {
    2093             :   const RecordVal *R = getValue(FieldName);
    2094    35674644 :   if (!R || !R->getValue())
    2095           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2096             :       "' does not have a field named `" + FieldName + "'!\n");
    2097             : 
    2098             :   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
    2099    35674644 :     return BI->getValue();
    2100           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2101             :     FieldName + "' does not have a bit initializer!");
    2102             : }
    2103             : 
    2104     2433980 : bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
    2105             :   const RecordVal *R = getValue(FieldName);
    2106     2433980 :   if (!R || !R->getValue())
    2107           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2108             :       "' does not have a field named `" + FieldName.str() + "'!\n");
    2109             : 
    2110     2433980 :   if (isa<UnsetInit>(R->getValue())) {
    2111     1709800 :     Unset = true;
    2112     1709800 :     return false;
    2113             :   }
    2114      724180 :   Unset = false;
    2115             :   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
    2116      724180 :     return BI->getValue();
    2117           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2118             :     FieldName + "' does not have a bit initializer!");
    2119             : }
    2120             : 
    2121     3788152 : DagInit *Record::getValueAsDag(StringRef FieldName) const {
    2122             :   const RecordVal *R = getValue(FieldName);
    2123     3788152 :   if (!R || !R->getValue())
    2124           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2125             :       "' does not have a field named `" + FieldName + "'!\n");
    2126             : 
    2127             :   if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
    2128     3788152 :     return DI;
    2129           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2130             :     FieldName + "' does not have a dag initializer!");
    2131             : }
    2132             : 
    2133             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    2134             : LLVM_DUMP_METHOD void MultiClass::dump() const {
    2135             :   errs() << "Record:\n";
    2136             :   Rec.dump();
    2137             : 
    2138             :   errs() << "Defs:\n";
    2139             :   for (const auto &Proto : DefPrototypes)
    2140             :     Proto->dump();
    2141             : }
    2142             : 
    2143             : LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
    2144             : #endif
    2145             : 
    2146          79 : raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
    2147          79 :   OS << "------------- Classes -----------------\n";
    2148         280 :   for (const auto &C : RK.getClasses())
    2149         402 :     OS << "class " << *C.second;
    2150             : 
    2151          79 :   OS << "------------- Defs -----------------\n";
    2152         759 :   for (const auto &D : RK.getDefs())
    2153        1360 :     OS << "def " << *D.second;
    2154          79 :   return OS;
    2155             : }
    2156             : 
    2157             : /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
    2158             : /// an identifier.
    2159      820533 : Init *RecordKeeper::getNewAnonymousName() {
    2160     3282132 :   return StringInit::get("anonymous_" + utostr(AnonCounter++));
    2161             : }
    2162             : 
    2163             : std::vector<Record *>
    2164        3106 : RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
    2165        3106 :   Record *Class = getClass(ClassName);
    2166        3106 :   if (!Class)
    2167           0 :     PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
    2168             : 
    2169             :   std::vector<Record*> Defs;
    2170    49497048 :   for (const auto &D : getDefs())
    2171    49493942 :     if (D.second->isSubClassOf(Class))
    2172     3945208 :       Defs.push_back(D.second.get());
    2173             : 
    2174        3106 :   return Defs;
    2175             : }
    2176             : 
    2177     5724747 : Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
    2178             :                         Init *Name, StringRef Scoper) {
    2179             :   Init *NewName =
    2180     5724747 :       BinOpInit::getStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
    2181     5724747 :   NewName = BinOpInit::getStrConcat(NewName, Name);
    2182     5724747 :   if (CurMultiClass && Scoper != "::") {
    2183           0 :     Init *Prefix = BinOpInit::getStrConcat(CurMultiClass->Rec.getNameInit(),
    2184           0 :                                            StringInit::get("::"));
    2185           0 :     NewName = BinOpInit::getStrConcat(Prefix, NewName);
    2186             :   }
    2187             : 
    2188             :   if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
    2189           0 :     NewName = BinOp->Fold(&CurRec);
    2190     5724747 :   return NewName;
    2191             : }
    2192             : 
    2193    95155579 : Init *MapResolver::resolve(Init *VarName) {
    2194    95155579 :   auto It = Map.find(VarName);
    2195    95155579 :   if (It == Map.end())
    2196             :     return nullptr;
    2197             : 
    2198    24420410 :   Init *I = It->second.V;
    2199             : 
    2200    43956618 :   if (!It->second.Resolved && Map.size() > 1) {
    2201             :     // Resolve mutual references among the mapped variables, but prevent
    2202             :     // infinite recursion.
    2203             :     Map.erase(It);
    2204    12625324 :     I = I->resolveReferences(*this);
    2205    12625324 :     Map[VarName] = {I, true};
    2206             :   }
    2207             : 
    2208             :   return I;
    2209             : }
    2210             : 
    2211    25856846 : Init *RecordResolver::resolve(Init *VarName) {
    2212    25856846 :   Init *Val = Cache.lookup(VarName);
    2213     6383300 :   if (Val)
    2214             :     return Val;
    2215             : 
    2216    24561942 :   for (Init *S : Stack) {
    2217     2544198 :     if (S == VarName)
    2218             :       return nullptr; // prevent infinite recursion
    2219             :   }
    2220             : 
    2221    38947092 :   if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
    2222    38947092 :     if (!isa<UnsetInit>(RV->getValue())) {
    2223             :       Val = RV->getValue();
    2224    19471897 :       Stack.push_back(VarName);
    2225    19471897 :       Val = Val->resolveReferences(*this);
    2226             :       Stack.pop_back();
    2227             :     }
    2228             :   }
    2229             : 
    2230    19473546 :   Cache[VarName] = Val;
    2231    19473546 :   return Val;
    2232             : }
    2233             : 
    2234     9189312 : Init *TrackUnresolvedResolver::resolve(Init *VarName) {
    2235             :   Init *I = nullptr;
    2236             : 
    2237     9189312 :   if (R) {
    2238     9149374 :     I = R->resolve(VarName);
    2239     9149374 :     if (I && !FoundUnresolved) {
    2240             :       // Do not recurse into the resolved initializer, as that would change
    2241             :       // the behavior of the resolver we're delegating, but do check to see
    2242             :       // if there are unresolved variables remaining.
    2243             :       TrackUnresolvedResolver Sub;
    2244     5514657 :       I->resolveReferences(Sub);
    2245     5514657 :       FoundUnresolved |= Sub.FoundUnresolved;
    2246             :     }
    2247             :   }
    2248             : 
    2249     9149374 :   if (!I)
    2250     3115080 :     FoundUnresolved = true;
    2251     9189312 :   return I;
    2252             : }
    2253             : 
    2254       14616 : Init *HasReferenceResolver::resolve(Init *VarName)
    2255             : {
    2256       14616 :   if (VarName == VarNameToTrack)
    2257       12452 :     Found = true;
    2258       14616 :   return nullptr;
    2259        1008 : }

Generated by: LCOV version 1.13