LCOV - code coverage report
Current view: top level - lib/TableGen - Record.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 970 1070 90.7 %
Date: 2018-07-13 00:08:38 Functions: 159 168 94.6 %
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         348 : static BumpPtrAllocator Allocator;
      41             : 
      42             : //===----------------------------------------------------------------------===//
      43             : //    Type implementations
      44             : //===----------------------------------------------------------------------===//
      45             : 
      46         348 : BitRecTy BitRecTy::Shared;
      47         348 : CodeRecTy CodeRecTy::Shared;
      48         348 : IntRecTy IntRecTy::Shared;
      49         348 : StringRecTy StringRecTy::Shared;
      50         348 : 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     1192959 : ListRecTy *RecTy::getListTy() {
      57     1192959 :   if (!ListTy)
      58       12926 :     ListTy = new(Allocator) ListRecTy(this);
      59     1192959 :   return ListTy;
      60             : }
      61             : 
      62       61853 : bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
      63             :   assert(RHS && "NULL pointer");
      64       61853 :   return Kind == RHS->getRecTyKind();
      65             : }
      66             : 
      67     1089672 : bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
      68             : 
      69        2708 : bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
      70        2708 :   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     1096657 : BitsRecTy *BitsRecTy::get(unsigned Sz) {
      78     1096869 :   static std::vector<BitsRecTy*> Shared;
      79     2193314 :   if (Sz >= Shared.size())
      80         596 :     Shared.resize(Sz + 1);
      81     1096657 :   BitsRecTy *&Ty = Shared[Sz];
      82     1096657 :   if (!Ty)
      83        2942 :     Ty = new(Allocator) BitsRecTy(Sz);
      84     1096657 :   return Ty;
      85             : }
      86             : 
      87         250 : std::string BitsRecTy::getAsString() const {
      88         750 :   return "bits<" + utostr(Size) + ">";
      89             : }
      90             : 
      91        3264 : bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
      92        3264 :   if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
      93          11 :     return cast<BitsRecTy>(RHS)->Size == Size;
      94        3253 :   RecTyKind kind = RHS->getRecTyKind();
      95        3253 :   return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
      96             : }
      97             : 
      98       27724 : 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      351082 : bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     105      351082 :   RecTyKind kind = RHS->getRecTyKind();
     106      351082 :   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       58863 : bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     119       58863 :   RecTyKind Kind = RHS->getRecTyKind();
     120       58863 :   return Kind == StringRecTyKind || Kind == CodeRecTyKind;
     121             : }
     122             : 
     123         245 : std::string ListRecTy::getAsString() const {
     124         735 :   return "list<" + Ty->getAsString() + ">";
     125             : }
     126             : 
     127      160890 : bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     128             :   if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
     129      160890 :     return Ty->typeIsConvertibleTo(ListTy->getElementType());
     130             :   return false;
     131             : }
     132             : 
     133     3092284 : bool ListRecTy::typeIsA(const RecTy *RHS) const {
     134             :   if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
     135     3092284 :     return getElementType()->typeIsA(RHSl->getElementType());
     136             :   return false;
     137             : }
     138             : 
     139         178 : std::string DagRecTy::getAsString() const {
     140         178 :   return "dag";
     141             : }
     142             : 
     143     3710607 : static void ProfileRecordRecTy(FoldingSetNodeID &ID,
     144             :                                ArrayRef<Record *> Classes) {
     145     3710607 :   ID.AddInteger(Classes.size());
     146    13585085 :   for (Record *R : Classes)
     147     4937239 :     ID.AddPointer(R);
     148     3710607 : }
     149             : 
     150     1589219 : RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) {
     151     1589219 :   if (UnsortedClasses.empty()) {
     152        2633 :     static RecordRecTy AnyRecord(0);
     153             :     return &AnyRecord;
     154             :   }
     155             : 
     156             :   FoldingSet<RecordRecTy> &ThePool =
     157     1586792 :       UnsortedClasses[0]->getRecords().RecordTypePool;
     158             : 
     159             :   SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
     160             :                                    UnsortedClasses.end());
     161             :   llvm::sort(Classes.begin(), Classes.end(),
     162      654197 :              [](Record *LHS, Record *RHS) {
     163     1308394 :                return LHS->getNameInitAsString() < RHS->getNameInitAsString();
     164      654197 :              });
     165             : 
     166             :   FoldingSetNodeID ID;
     167     1586792 :   ProfileRecordRecTy(ID, Classes);
     168             : 
     169     1586792 :   void *IP = nullptr;
     170     1523266 :   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       63526 :                                  alignof(RecordRecTy));
     185       63526 :   RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
     186             :   std::uninitialized_copy(Classes.begin(), Classes.end(),
     187             :                           Ty->getTrailingObjects<Record *>());
     188       63526 :   ThePool.InsertNode(Ty, IP);
     189       63526 :   return Ty;
     190             : }
     191             : 
     192     2123815 : void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
     193     2123815 :   ProfileRecordRecTy(ID, getClasses());
     194     2123815 : }
     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     7548683 : bool RecordRecTy::isSubClassOf(Record *Class) const {
     213             :   return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
     214    18695589 :                                       return MySuperClass == Class ||
     215             :                                              MySuperClass->isSubClassOf(Class);
     216     7548683 :                                     });
     217             : }
     218             : 
     219    10465899 : bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
     220    10465899 :   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     7143907 :                                          });
     230             : }
     231             : 
     232     6622453 : bool RecordRecTy::typeIsA(const RecTy *RHS) const {
     233     6622453 :   return typeIsConvertibleTo(RHS);
     234             : }
     235             : 
     236      220811 : static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
     237             :   SmallVector<Record *, 4> CommonSuperClasses;
     238             :   SmallVector<Record *, 4> Stack;
     239             : 
     240      220811 :   Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end());
     241             : 
     242     1030323 :   while (!Stack.empty()) {
     243      404756 :     Record *R = Stack.back();
     244             :     Stack.pop_back();
     245             : 
     246      404756 :     if (T2->isSubClassOf(R)) {
     247      224475 :       CommonSuperClasses.push_back(R);
     248             :     } else {
     249      180281 :       R->getDirectSuperClasses(Stack);
     250             :     }
     251             :   }
     252             : 
     253      441622 :   return RecordRecTy::get(CommonSuperClasses);
     254             : }
     255             : 
     256     2141581 : RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
     257     2141581 :   if (T1 == T2)
     258             :     return T1;
     259             : 
     260             :   if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
     261             :     if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
     262      220811 :       return resolveRecordTypes(RecTy1, RecTy2);
     263             :   }
     264             : 
     265        4102 :   if (T1->typeIsConvertibleTo(T2))
     266             :     return T2;
     267         598 :   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      996946 : UnsetInit *UnsetInit::get() {
     293      997290 :   static UnsetInit TheInit;
     294      996946 :   return &TheInit;
     295             : }
     296             : 
     297    11533177 : Init *UnsetInit::getCastTo(RecTy *Ty) const {
     298    11533177 :   return const_cast<UnsetInit *>(this);
     299             : }
     300             : 
     301       23820 : Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
     302       23820 :   return const_cast<UnsetInit *>(this);
     303             : }
     304             : 
     305    11934345 : BitInit *BitInit::get(bool V) {
     306    11934626 :   static BitInit True(true);
     307    11934626 :   static BitInit False(false);
     308             : 
     309    11934345 :   return V ? &True : &False;
     310             : }
     311             : 
     312     3118746 : Init *BitInit::convertInitializerTo(RecTy *Ty) const {
     313     3118746 :   if (isa<BitRecTy>(Ty))
     314        2115 :     return const_cast<BitInit *>(this);
     315             : 
     316     3116631 :   if (isa<IntRecTy>(Ty))
     317     3116631 :     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    21621143 : ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
     330    21621143 :   ID.AddInteger(Range.size());
     331             : 
     332   954017407 :   for (Init *I : Range)
     333   466198132 :     ID.AddPointer(I);
     334    21621143 : }
     335             : 
     336     7564714 : BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
     337     7564926 :   static FoldingSet<BitsInit> ThePool;
     338             : 
     339             :   FoldingSetNodeID ID;
     340     7564714 :   ProfileBitsInit(ID, Range);
     341             : 
     342     7564714 :   void *IP = nullptr;
     343     6784847 :   if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     344             :     return I;
     345             : 
     346             :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
     347      779867 :                                  alignof(BitsInit));
     348      779867 :   BitsInit *I = new(Mem) BitsInit(Range.size());
     349             :   std::uninitialized_copy(Range.begin(), Range.end(),
     350             :                           I->getTrailingObjects<Init *>());
     351      779867 :   ThePool.InsertNode(I, IP);
     352      779867 :   return I;
     353             : }
     354             : 
     355    14056429 : void BitsInit::Profile(FoldingSetNodeID &ID) const {
     356    28112858 :   ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
     357    14056429 : }
     358             : 
     359     2618797 : Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
     360     2618797 :   if (isa<BitRecTy>(Ty)) {
     361        1123 :     if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
     362        1123 :     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     2469856 :   if (isa<IntRecTy>(Ty)) {
     373             :     int64_t Result = 0;
     374     7842607 :     for (unsigned i = 0, e = getNumBits(); i != e; ++i)
     375             :       if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
     376     5372751 :         Result |= static_cast<int64_t>(Bit->getValue()) << i;
     377             :       else
     378             :         return nullptr;
     379     2468606 :     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       12187 : bool BitsInit::isConcrete() const {
     398       67586 :   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     399       55399 :     if (!getBit(i)->isConcrete())
     400             :       return false;
     401             :   }
     402             :   return true;
     403             : }
     404             : 
     405        1150 : std::string BitsInit::getAsString() const {
     406        1150 :   std::string Result = "{ ";
     407        6122 :   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     408        4972 :     if (i) Result += ", ";
     409        9944 :     if (Init *Bit = getBit(e-i-1))
     410        9944 :       Result += Bit->getAsString();
     411             :     else
     412             :       Result += "*";
     413             :   }
     414        2300 :   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    30929773 : Init *BitsInit::resolveReferences(Resolver &R) const {
     420             :   bool Changed = false;
     421    61859546 :   SmallVector<Init *, 16> NewBits(getNumBits());
     422             : 
     423             :   Init *CachedBitVarRef = nullptr;
     424             :   Init *CachedBitVarResolved = nullptr;
     425             : 
     426   317185005 :   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   113320177 :       if (CurBitVar->getBitVar() != CachedBitVarRef) {
     432             :         CachedBitVarRef = CurBitVar->getBitVar();
     433    28838472 :         CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
     434             :       }
     435             : 
     436   113320177 :       NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
     437             :     } else {
     438             :       // getBit(0) implicitly converts int and bits<1> values to bit.
     439   172935055 :       NewBit = CurBit->resolveReferences(R)->getBit(0);
     440             :     }
     441             : 
     442   286255232 :     if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
     443             :       NewBit = CurBit;
     444   286255232 :     NewBits[i] = NewBit;
     445   286255232 :     Changed |= CurBit != NewBit;
     446             :   }
     447             : 
     448    30929773 :   if (Changed)
     449     5751806 :     return BitsInit::get(NewBits);
     450             : 
     451    25177967 :   return const_cast<BitsInit *>(this);
     452             : }
     453             : 
     454     9882522 : IntInit *IntInit::get(int64_t V) {
     455     9882841 :   static DenseMap<int64_t, IntInit*> ThePool;
     456             : 
     457             :   IntInit *&I = ThePool[V];
     458    10016712 :   if (!I) I = new(Allocator) IntInit(V);
     459     9882522 :   return I;
     460             : }
     461             : 
     462      550585 : std::string IntInit::getAsString() const {
     463      550585 :   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     1134623 :   return (NumBits >= sizeof(Value) * 8) ||
     469     1135364 :          (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
     470             : }
     471             : 
     472     6105649 : Init *IntInit::convertInitializerTo(RecTy *Ty) const {
     473     6105649 :   if (isa<IntRecTy>(Ty))
     474     4283368 :     return const_cast<IntInit *>(this);
     475             : 
     476     1822281 :   if (isa<BitRecTy>(Ty)) {
     477      687381 :     int64_t Val = getValue();
     478      687381 :     if (Val != 0 && Val != 1) return nullptr;  // Only accept 0 or 1 for a bit!
     479      687378 :     return BitInit::get(Val != 0);
     480             :   }
     481             : 
     482             :   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
     483     1134900 :     int64_t Value = getValue();
     484             :     // Make sure this bitfield is large enough to hold the integer value.
     485     1134900 :     if (!canFitInBitfield(Value, BRT->getNumBits()))
     486             :       return nullptr;
     487             : 
     488     2269794 :     SmallVector<Init *, 16> NewBits(BRT->getNumBits());
     489    15417311 :     for (unsigned i = 0; i != BRT->getNumBits(); ++i)
     490    14282414 :       NewBits[i] = BitInit::get(Value & (1LL << i));
     491             : 
     492     1134897 :     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       20701 : CodeInit *CodeInit::get(StringRef V) {
     512       20927 :   static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
     513             : 
     514             :   auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
     515       20701 :   if (!Entry.second)
     516       16746 :     Entry.second = new(Allocator) CodeInit(Entry.getKey());
     517       20701 :   return Entry.second;
     518             : }
     519             : 
     520   117728856 : StringInit *StringInit::get(StringRef V) {
     521   117729204 :   static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
     522             : 
     523             :   auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
     524   117728856 :   if (!Entry.second)
     525     7742917 :     Entry.second = new(Allocator) StringInit(Entry.getKey());
     526   117728856 :   return Entry.second;
     527             : }
     528             : 
     529      432837 : Init *StringInit::convertInitializerTo(RecTy *Ty) const {
     530      432837 :   if (isa<StringRecTy>(Ty))
     531           0 :     return const_cast<StringInit *>(this);
     532      432837 :   if (isa<CodeRecTy>(Ty))
     533         970 :     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    17466386 : static void ProfileListInit(FoldingSetNodeID &ID,
     548             :                             ArrayRef<Init *> Range,
     549             :                             RecTy *EltTy) {
     550    17466386 :   ID.AddInteger(Range.size());
     551    17466386 :   ID.AddPointer(EltTy);
     552             : 
     553   104724574 :   for (Init *I : Range)
     554    43629094 :     ID.AddPointer(I);
     555    17466386 : }
     556             : 
     557     6128928 : ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
     558     6129207 :   static FoldingSet<ListInit> ThePool;
     559             : 
     560             :   FoldingSetNodeID ID;
     561     6128928 :   ProfileListInit(ID, Range, EltTy);
     562             : 
     563     6128928 :   void *IP = nullptr;
     564     4997257 :   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     1131671 :                                  alignof(ListInit));
     572     1131671 :   ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
     573             :   std::uninitialized_copy(Range.begin(), Range.end(),
     574             :                           I->getTrailingObjects<Init *>());
     575     1131671 :   ThePool.InsertNode(I, IP);
     576     1131671 :   return I;
     577             : }
     578             : 
     579    11337458 : void ListInit::Profile(FoldingSetNodeID &ID) const {
     580    11337458 :   RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
     581             : 
     582    11337458 :   ProfileListInit(ID, getValues(), EltTy);
     583    11337458 : }
     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     1965553 : 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     1965553 :   return DI->getDef();
     630             : }
     631             : 
     632    28952132 : Init *ListInit::resolveReferences(Resolver &R) const {
     633             :   SmallVector<Init*, 8> Resolved;
     634    57904264 :   Resolved.reserve(size());
     635             :   bool Changed = false;
     636             : 
     637    92301888 :   for (Init *CurElt : getValues()) {
     638    31674878 :     Init *E = CurElt->resolveReferences(R);
     639    31674878 :     Changed |= E != CurElt;
     640    31674878 :     Resolved.push_back(E);
     641             :   }
     642             : 
     643    28952132 :   if (Changed)
     644     1303989 :     return ListInit::get(Resolved, getElementType());
     645    27648143 :   return const_cast<ListInit *>(this);
     646             : }
     647             : 
     648     9930549 : bool ListInit::isConcrete() const {
     649    30418523 :   for (Init *Element : *this) {
     650    10243987 :     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     8080608 : Init *OpInit::getBit(unsigned Bit) const {
     668     8080608 :   if (getType() == BitRecTy::get())
     669         211 :     return const_cast<OpInit*>(this);
     670     8080397 :   return VarBitInit::get(const_cast<OpInit*>(this), Bit);
     671             : }
     672             : 
     673             : static void
     674     9791942 : ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
     675     9791942 :   ID.AddInteger(Opcode);
     676     9791942 :   ID.AddPointer(Op);
     677     9791942 :   ID.AddPointer(Type);
     678     9791942 : }
     679             : 
     680     3938430 : UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
     681     3938664 :   static FoldingSet<UnOpInit> ThePool;
     682             : 
     683             :   FoldingSetNodeID ID;
     684     3938430 :   ProfileUnOpInit(ID, Opc, LHS, Type);
     685             : 
     686     3938430 :   void *IP = nullptr;
     687     3548604 :   if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     688             :     return I;
     689             : 
     690             :   UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
     691      389826 :   ThePool.InsertNode(I, IP);
     692      389826 :   return I;
     693             : }
     694             : 
     695     5853512 : void UnOpInit::Profile(FoldingSetNodeID &ID) const {
     696     5853512 :   ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
     697     5853512 : }
     698             : 
     699     3937920 : Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
     700     3937920 :   switch (getOpcode()) {
     701     3697827 :   case CAST:
     702     7395654 :     if (isa<StringRecTy>(getType())) {
     703      241893 :       if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
     704             :         return LHSs;
     705             : 
     706             :       if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
     707      185559 :         return StringInit::get(LHSd->getAsString());
     708             : 
     709             :       if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
     710      477036 :         return StringInit::get(LHSi->getAsString());
     711     3455934 :     } else if (isa<RecordRecTy>(getType())) {
     712     1987433 :       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     1885811 :         if (Name == CurRec->getNameInit()) {
     719      108890 :           if (!IsFinal)
     720             :             break;
     721             :           D = CurRec;
     722             :         } else {
     723     1776921 :           D = CurRec->getRecords().getDef(Name->getValue());
     724     1776921 :           if (!D) {
     725      232828 :             if (IsFinal)
     726           1 :               PrintFatalError(CurRec->getLoc(),
     727             :                               Twine("Undefined reference to record: '") +
     728           1 :                               Name->getValue() + "'\n");
     729             :             break;
     730             :           }
     731             :         }
     732             : 
     733     1651445 :         DefInit *DI = DefInit::get(D);
     734     1651445 :         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     1591151 :     if (Init *NewInit = LHS->convertInitializerTo(getType()))
     746      117307 :       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        4432 :   case TAIL:
     757        4432 :     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        4172 :       return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
     762             :     }
     763             :     break;
     764             : 
     765      197751 :   case SIZE:
     766      197751 :     if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
     767      178659 :       return IntInit::get(LHSl->size());
     768             :     break;
     769             : 
     770       35738 :   case EMPTY:
     771       35738 :     if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
     772       70754 :       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      371584 :   return const_cast<UnOpInit *>(this);
     778             : }
     779             : 
     780     5665192 : Init *UnOpInit::resolveReferences(Resolver &R) const {
     781     5665192 :   Init *lhs = LHS->resolveReferences(R);
     782             : 
     783     5665192 :   if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
     784     3897377 :     return (UnOpInit::get(getOpcode(), lhs, getType()))
     785     7794754 :         ->Fold(R.getCurrentRecord(), R.isFinal());
     786     1767815 :   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    28874470 : ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
     803             :                  RecTy *Type) {
     804    28874470 :   ID.AddInteger(Opcode);
     805    28874470 :   ID.AddPointer(LHS);
     806    28874470 :   ID.AddPointer(RHS);
     807    28874470 :   ID.AddPointer(Type);
     808    28874470 : }
     809             : 
     810    10992966 : BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
     811             :                           Init *RHS, RecTy *Type) {
     812    10993210 :   static FoldingSet<BinOpInit> ThePool;
     813             : 
     814             :   FoldingSetNodeID ID;
     815    10992966 :   ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
     816             : 
     817    10992966 :   void *IP = nullptr;
     818     8254345 :   if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
     819             :     return I;
     820             : 
     821             :   BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
     822     2738621 :   ThePool.InsertNode(I, IP);
     823     2738621 :   return I;
     824             : }
     825             : 
     826    17881504 : void BinOpInit::Profile(FoldingSetNodeID &ID) const {
     827    17881504 :   ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
     828    17881504 : }
     829             : 
     830    17189241 : static StringInit *ConcatStringInits(const StringInit *I0,
     831             :                                      const StringInit *I1) {
     832             :   SmallString<80> Concat(I0->getValue());
     833             :   Concat.append(I1->getValue());
     834    34378482 :   return StringInit::get(Concat);
     835             : }
     836             : 
     837    11878242 : 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    11724801 :       return ConcatStringInits(I0s, I1s);
     842      153441 :   return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
     843             : }
     844             : 
     845    10839525 : Init *BinOpInit::Fold(Record *CurRec) const {
     846    10839525 :   switch (getOpcode()) {
     847       74988 :   case CONCAT: {
     848       74988 :     DagInit *LHSs = dyn_cast<DagInit>(LHS);
     849       74988 :     DagInit *RHSs = dyn_cast<DagInit>(RHS);
     850       74988 :     if (LHSs && RHSs) {
     851       59904 :       DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
     852       59904 :       DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
     853       59904 :       if (!LOp || !ROp)
     854             :         break;
     855       59903 :       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      385190 :       for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
     863      325287 :         Args.push_back(LHSs->getArg(i));
     864      325287 :         ArgNames.push_back(LHSs->getArgName(i));
     865             :       }
     866      138131 :       for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
     867       78228 :         Args.push_back(RHSs->getArg(i));
     868       78228 :         ArgNames.push_back(RHSs->getArgName(i));
     869             :       }
     870       59903 :       return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
     871             :     }
     872             :     break;
     873             :   }
     874     1563379 :   case LISTCONCAT: {
     875     1563379 :     ListInit *LHSs = dyn_cast<ListInit>(LHS);
     876     1563379 :     ListInit *RHSs = dyn_cast<ListInit>(RHS);
     877     1563379 :     if (LHSs && RHSs) {
     878             :       SmallVector<Init *, 8> Args;
     879     1388001 :       Args.insert(Args.end(), LHSs->begin(), LHSs->end());
     880     1388001 :       Args.insert(Args.end(), RHSs->begin(), RHSs->end());
     881     1388001 :       return ListInit::get(Args, LHSs->getElementType());
     882             :     }
     883             :     break;
     884             :   }
     885     6269559 :   case STRCONCAT: {
     886     6269559 :     StringInit *LHSs = dyn_cast<StringInit>(LHS);
     887     6269559 :     StringInit *RHSs = dyn_cast<StringInit>(RHS);
     888     6269559 :     if (LHSs && RHSs)
     889     5464440 :       return ConcatStringInits(LHSs, RHSs);
     890             :     break;
     891             :   }
     892      873625 :   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      873625 :         dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
     902             :     IntInit *R =
     903      873625 :         dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
     904             : 
     905      873625 :     if (L && R) {
     906             :       bool Result;
     907      637946 :       switch (getOpcode()) {
     908      625134 :       case EQ: Result = L->getValue() == R->getValue(); break;
     909           6 :       case NE: Result = L->getValue() != R->getValue(); break;
     910       12784 :       case LE: Result = L->getValue() <= R->getValue(); break;
     911          14 :       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      637946 :       return BitInit::get(Result);
     917             :     }
     918             : 
     919      235679 :     if (getOpcode() == EQ || getOpcode() == NE) {
     920      233075 :       StringInit *LHSs = dyn_cast<StringInit>(LHS);
     921      233075 :       StringInit *RHSs = dyn_cast<StringInit>(RHS);
     922             : 
     923             :       // Make sure we've resolved
     924      233075 :       if (LHSs && RHSs) {
     925             :         bool Equal = LHSs->getValue() == RHSs->getValue();
     926      206990 :         return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
     927             :       }
     928             :     }
     929             : 
     930             :     break;
     931             :   }
     932     2057974 :   case ADD:
     933             :   case AND:
     934             :   case OR:
     935             :   case SHL:
     936             :   case SRA:
     937             :   case SRL: {
     938             :     IntInit *LHSi =
     939     2057974 :       dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
     940             :     IntInit *RHSi =
     941     2057974 :       dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
     942     2057974 :     if (LHSi && RHSi) {
     943     1979051 :       int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
     944             :       int64_t Result;
     945     1979051 :       switch (getOpcode()) {
     946           0 :       default: llvm_unreachable("Bad opcode!");
     947      570888 :       case ADD: Result = LHSv +  RHSv; break;
     948       42251 :       case AND: Result = LHSv &  RHSv; break;
     949      790095 :       case OR: Result = LHSv | RHSv; break;
     950      322928 :       case SHL: Result = LHSv << RHSv; break;
     951           1 :       case SRA: Result = LHSv >> RHSv; break;
     952      252888 :       case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
     953             :       }
     954     1979051 :       return IntInit::get(Result);
     955             :     }
     956             :     break;
     957             :   }
     958             :   }
     959     1103194 :   return const_cast<BinOpInit *>(this);
     960             : }
     961             : 
     962    16621289 : Init *BinOpInit::resolveReferences(Resolver &R) const {
     963    16621289 :   Init *lhs = LHS->resolveReferences(R);
     964    16621289 :   Init *rhs = RHS->resolveReferences(R);
     965             : 
     966    16621289 :   if (LHS != lhs || RHS != rhs)
     967    10795533 :     return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
     968    21591066 :         ->Fold(R.getCurrentRecord());
     969     5825756 :   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     3560766 : ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
     996             :                   Init *RHS, RecTy *Type) {
     997     3560766 :   ID.AddInteger(Opcode);
     998     3560766 :   ID.AddPointer(LHS);
     999     3560766 :   ID.AddPointer(MHS);
    1000     3560766 :   ID.AddPointer(RHS);
    1001     3560766 :   ID.AddPointer(Type);
    1002     3560766 : }
    1003             : 
    1004     1281408 : TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
    1005             :                             RecTy *Type) {
    1006     1281621 :   static FoldingSet<TernOpInit> ThePool;
    1007             : 
    1008             :   FoldingSetNodeID ID;
    1009     1281408 :   ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
    1010             : 
    1011     1281408 :   void *IP = nullptr;
    1012     1042877 :   if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1013             :     return I;
    1014             : 
    1015             :   TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
    1016      238531 :   ThePool.InsertNode(I, IP);
    1017      238531 :   return I;
    1018             : }
    1019             : 
    1020     2279358 : void TernOpInit::Profile(FoldingSetNodeID &ID) const {
    1021     2279358 :   ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
    1022     2279358 : }
    1023             : 
    1024     1384189 : static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
    1025             :   MapResolver R(CurRec);
    1026             :   R.set(LHS, MHSe);
    1027     2768378 :   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      484956 : 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     2941524 :     for (Init *&Item : NewList) {
    1068     1244255 :       Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec);
    1069     1244255 :       if (NewItem != Item)
    1070     1113852 :         Item = NewItem;
    1071             :     }
    1072      906028 :     return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
    1073             :   }
    1074             : 
    1075             :   return nullptr;
    1076             : }
    1077             : 
    1078     1281408 : Init *TernOpInit::Fold(Record *CurRec) const {
    1079     1281408 :   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      484956 :   case FOREACH: {
    1124      484956 :     if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
    1125             :       return Result;
    1126             :     break;
    1127             :   }
    1128             : 
    1129      460392 :   case IF: {
    1130      460392 :     if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
    1131      460392 :                             LHS->convertInitializerTo(IntRecTy::get()))) {
    1132           0 :       if (LHSi->getValue())
    1133           0 :         return MHS;
    1134           0 :       return RHS;
    1135             :     }
    1136             :     break;
    1137             :   }
    1138             : 
    1139          15 :   case DAG: {
    1140          15 :     ListInit *MHSl = dyn_cast<ListInit>(MHS);
    1141          15 :     ListInit *RHSl = dyn_cast<ListInit>(RHS);
    1142          15 :     bool MHSok = MHSl || isa<UnsetInit>(MHS);
    1143          15 :     bool RHSok = RHSl || isa<UnsetInit>(RHS);
    1144             : 
    1145          15 :     if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
    1146             :       break; // Typically prevented by the parser, but might happen with template args
    1147             : 
    1148          15 :     if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
    1149             :       SmallVector<std::pair<Init *, StringInit *>, 8> Children;
    1150          10 :       unsigned Size = MHSl ? MHSl->size() : RHSl->size();
    1151          42 :       for (unsigned i = 0; i != Size; ++i) {
    1152          19 :         Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
    1153          21 :         Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
    1154          17 :         if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
    1155           1 :           return const_cast<TernOpInit *>(this);
    1156          16 :         Children.emplace_back(Node, dyn_cast<StringInit>(Name));
    1157             :       }
    1158           9 :       return DagInit::get(LHS, nullptr, Children);
    1159             :     }
    1160             :     break;
    1161             :   }
    1162             :   }
    1163             : 
    1164      517622 :   return const_cast<TernOpInit *>(this);
    1165             : }
    1166             : 
    1167    10826363 : Init *TernOpInit::resolveReferences(Resolver &R) const {
    1168    10826363 :   Init *lhs = LHS->resolveReferences(R);
    1169             : 
    1170    10826363 :   if (getOpcode() == IF && lhs != LHS) {
    1171     4587285 :     if (IntInit *Value = dyn_cast_or_null<IntInit>(
    1172     4587285 :                              lhs->convertInitializerTo(IntRecTy::get()))) {
    1173             :       // Short-circuit
    1174     4519980 :       if (Value->getValue())
    1175     2123297 :         return MHS->resolveReferences(R);
    1176     2396683 :       return RHS->resolveReferences(R);
    1177             :     }
    1178             :   }
    1179             : 
    1180     6306383 :   Init *mhs = MHS->resolveReferences(R);
    1181             :   Init *rhs;
    1182             : 
    1183     6306383 :   if (getOpcode() == FOREACH) {
    1184             :     ShadowResolver SR(R);
    1185             :     SR.addShadow(lhs);
    1186      854042 :     rhs = RHS->resolveReferences(SR);
    1187             :   } else {
    1188     5452341 :     rhs = RHS->resolveReferences(R);
    1189             :   }
    1190             : 
    1191     6306383 :   if (LHS != lhs || MHS != mhs || RHS != rhs)
    1192     1262531 :     return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
    1193     2525062 :         ->Fold(R.getCurrentRecord());
    1194     5043852 :   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      582079 : static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B,
    1212             :                               Init *Start, Init *List, Init *Expr,
    1213             :                               RecTy *Type) {
    1214      582079 :   ID.AddPointer(Start);
    1215      582079 :   ID.AddPointer(List);
    1216      582079 :   ID.AddPointer(A);
    1217      582079 :   ID.AddPointer(B);
    1218      582079 :   ID.AddPointer(Expr);
    1219      582079 :   ID.AddPointer(Type);
    1220      582079 : }
    1221             : 
    1222      227220 : FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
    1223             :                             Init *Expr, RecTy *Type) {
    1224      227414 :   static FoldingSet<FoldOpInit> ThePool;
    1225             : 
    1226             :   FoldingSetNodeID ID;
    1227      227220 :   ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
    1228             : 
    1229      227220 :   void *IP = nullptr;
    1230      186390 :   if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1231             :     return I;
    1232             : 
    1233             :   FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
    1234       40830 :   ThePool.InsertNode(I, IP);
    1235       40830 :   return I;
    1236             : }
    1237             : 
    1238      354859 : void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
    1239      354859 :   ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
    1240      354859 : }
    1241             : 
    1242      227220 : Init *FoldOpInit::Fold(Record *CurRec) const {
    1243      227220 :   if (ListInit *LI = dyn_cast<ListInit>(List)) {
    1244      226630 :     Init *Accum = Start;
    1245      894600 :     for (Init *Elt : *LI) {
    1246             :       MapResolver R(CurRec);
    1247      333985 :       R.set(A, Accum);
    1248      333985 :       R.set(B, Elt);
    1249      333985 :       Accum = Expr->resolveReferences(R);
    1250             :     }
    1251             :     return Accum;
    1252             :   }
    1253         590 :   return const_cast<FoldOpInit *>(this);
    1254             : }
    1255             : 
    1256      349355 : Init *FoldOpInit::resolveReferences(Resolver &R) const {
    1257      349355 :   Init *NewStart = Start->resolveReferences(R);
    1258      349355 :   Init *NewList = List->resolveReferences(R);
    1259             :   ShadowResolver SR(R);
    1260      349355 :   SR.addShadow(A);
    1261      349355 :   SR.addShadow(B);
    1262      349355 :   Init *NewExpr = Expr->resolveReferences(SR);
    1263             : 
    1264      349355 :   if (Start == NewStart && List == NewList && Expr == NewExpr)
    1265      122741 :     return const_cast<FoldOpInit *>(this);
    1266             : 
    1267      226614 :   return get(NewStart, NewList, A, B, NewExpr, getType())
    1268      453228 :       ->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     1624044 :   ID.AddPointer(CheckType);
    1285     1624044 :   ID.AddPointer(Expr);
    1286             : }
    1287             : 
    1288      797626 : IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
    1289      797817 :   static FoldingSet<IsAOpInit> ThePool;
    1290             : 
    1291             :   FoldingSetNodeID ID;
    1292             :   ProfileIsAOpInit(ID, CheckType, Expr);
    1293             : 
    1294      797626 :   void *IP = nullptr;
    1295      796100 :   if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1296             :     return I;
    1297             : 
    1298             :   IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
    1299        1526 :   ThePool.InsertNode(I, IP);
    1300        1526 :   return I;
    1301             : }
    1302             : 
    1303      826418 : void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
    1304      826418 :   ProfileIsAOpInit(ID, CheckType, Expr);
    1305      826418 : }
    1306             : 
    1307      797626 : Init *IsAOpInit::Fold() const {
    1308      797626 :   if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
    1309             :     // Is the expression type known to be (a subclass of) the desired type?
    1310      797626 :     if (TI->getType()->typeIsConvertibleTo(CheckType))
    1311      429402 :       return IntInit::get(1);
    1312             : 
    1313      736448 :     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      736448 :       if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
    1318      367652 :         return IntInit::get(0);
    1319             :     } else {
    1320             :       // We treat non-record types as not castable.
    1321           0 :       return IntInit::get(0);
    1322             :     }
    1323             :   }
    1324         572 :   return const_cast<IsAOpInit *>(this);
    1325             : }
    1326             : 
    1327     1158054 : Init *IsAOpInit::resolveReferences(Resolver &R) const {
    1328     1158054 :   Init *NewExpr = Expr->resolveReferences(R);
    1329     1158054 :   if (Expr != NewExpr)
    1330      797054 :     return get(CheckType, NewExpr)->Fold();
    1331      361000 :   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      280395 : RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
    1345      280395 :   if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
    1346      280395 :     for (Record *Rec : RecordType->getClasses()) {
    1347      280395 :       if (RecordVal *Field = Rec->getValue(FieldName))
    1348      280395 :         return Field->getType();
    1349             :     }
    1350             :   }
    1351             :   return nullptr;
    1352             : }
    1353             : 
    1354             : Init *
    1355      840512 : TypedInit::convertInitializerTo(RecTy *Ty) const {
    1356      840512 :   if (getType() == Ty || getType()->typeIsA(Ty))
    1357      347520 :     return const_cast<TypedInit *>(this);
    1358             : 
    1359     1321027 :   if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
    1360        7377 :       cast<BitsRecTy>(Ty)->getNumBits() == 1)
    1361       14754 :     return BitsInit::get({const_cast<TypedInit *>(this)});
    1362             : 
    1363             :   return nullptr;
    1364             : }
    1365             : 
    1366       45259 : Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
    1367       45259 :   BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
    1368             :   if (!T) return nullptr;  // Cannot subscript a non-bits variable.
    1369       45259 :   unsigned NumBits = T->getNumBits();
    1370             : 
    1371             :   SmallVector<Init *, 16> NewBits;
    1372       45259 :   NewBits.reserve(Bits.size());
    1373      372739 :   for (unsigned Bit : Bits) {
    1374      163740 :     if (Bit >= NumBits)
    1375             :       return nullptr;
    1376             : 
    1377      163740 :     NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
    1378             :   }
    1379       45259 :   return BitsInit::get(NewBits);
    1380             : }
    1381             : 
    1382   214255542 : Init *TypedInit::getCastTo(RecTy *Ty) const {
    1383             :   // Handle the common case quickly
    1384   214255542 :   if (getType() == Ty || getType()->typeIsA(Ty))
    1385   213623462 :     return const_cast<TypedInit *>(this);
    1386             : 
    1387      632080 :   if (Init *Converted = convertInitializerTo(Ty)) {
    1388             :     assert(!isa<TypedInit>(Converted) ||
    1389             :            cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
    1390             :     return Converted;
    1391             :   }
    1392             : 
    1393        4971 :   if (!getType()->typeIsConvertibleTo(Ty))
    1394             :     return nullptr;
    1395             : 
    1396             :   return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
    1397        4961 :       ->Fold(nullptr);
    1398             : }
    1399             : 
    1400         297 : Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
    1401         297 :   ListRecTy *T = dyn_cast<ListRecTy>(getType());
    1402             :   if (!T) return nullptr;  // Cannot subscript a non-list variable.
    1403             : 
    1404         297 :   if (Elements.size() == 1)
    1405         296 :     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     1089658 : VarInit *VarInit::get(Init *VN, RecTy *T) {
    1422             :   using Key = std::pair<RecTy *, Init *>;
    1423     1089996 :   static DenseMap<Key, VarInit*> ThePool;
    1424             : 
    1425     1089658 :   Key TheKey(std::make_pair(T, VN));
    1426             : 
    1427             :   VarInit *&I = ThePool[TheKey];
    1428     1089658 :   if (!I)
    1429      617217 :     I = new(Allocator) VarInit(VN, T);
    1430     1089658 :   return I;
    1431             : }
    1432             : 
    1433     6322968 : StringRef VarInit::getName() const {
    1434     6322968 :   StringInit *NameString = cast<StringInit>(getNameInit());
    1435     6322968 :   return NameString->getValue();
    1436             : }
    1437             : 
    1438    54959455 : Init *VarInit::getBit(unsigned Bit) const {
    1439    54959455 :   if (getType() == BitRecTy::get())
    1440    12927669 :     return const_cast<VarInit*>(this);
    1441    42031786 :   return VarBitInit::get(const_cast<VarInit*>(this), Bit);
    1442             : }
    1443             : 
    1444    86824248 : Init *VarInit::resolveReferences(Resolver &R) const {
    1445    86824248 :   if (Init *Val = R.resolve(VarName))
    1446             :     return Val;
    1447    44148078 :   return const_cast<VarInit *>(this);
    1448             : }
    1449             : 
    1450    68912424 : VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
    1451             :   using Key = std::pair<TypedInit *, unsigned>;
    1452    68912634 :   static DenseMap<Key, VarBitInit*> ThePool;
    1453             : 
    1454    68912424 :   Key TheKey(std::make_pair(T, B));
    1455             : 
    1456             :   VarBitInit *&I = ThePool[TheKey];
    1457    68912424 :   if (!I)
    1458      634191 :     I = new(Allocator) VarBitInit(T, B);
    1459    68912424 :   return I;
    1460             : }
    1461             : 
    1462         238 : std::string VarBitInit::getAsString() const {
    1463        1190 :   return TI->getAsString() + "{" + utostr(Bit) + "}";
    1464             : }
    1465             : 
    1466         922 : Init *VarBitInit::resolveReferences(Resolver &R) const {
    1467         922 :   Init *I = TI->resolveReferences(R);
    1468         922 :   if (TI != I)
    1469         616 :     return I->getBit(getBitNum());
    1470             : 
    1471         306 :   return const_cast<VarBitInit*>(this);
    1472             : }
    1473             : 
    1474         350 : VarListElementInit *VarListElementInit::get(TypedInit *T,
    1475             :                                             unsigned E) {
    1476             :   using Key = std::pair<TypedInit *, unsigned>;
    1477         549 :   static DenseMap<Key, VarListElementInit*> ThePool;
    1478             : 
    1479         350 :   Key TheKey(std::make_pair(T, E));
    1480             : 
    1481             :   VarListElementInit *&I = ThePool[TheKey];
    1482         685 :   if (!I) I = new(Allocator) VarListElementInit(T, E);
    1483         350 :   return I;
    1484             : }
    1485             : 
    1486          25 : std::string VarListElementInit::getAsString() const {
    1487         125 :   return TI->getAsString() + "[" + utostr(Element) + "]";
    1488             : }
    1489             : 
    1490       23218 : Init *VarListElementInit::resolveReferences(Resolver &R) const {
    1491       23218 :   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       15108 :     if (getElementNum() < List->size())
    1496       11013 :       return List->getElement(getElementNum());
    1497             :   }
    1498       12205 :   if (NewTI != TI && isa<TypedInit>(NewTI))
    1499          52 :     return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
    1500       12153 :   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      886582 : DefInit::DefInit(Record *D)
    1510     1773164 :     : TypedInit(IK_DefInit, D->getType()), Def(D) {}
    1511             : 
    1512     2281798 : DefInit *DefInit::get(Record *R) {
    1513     2281798 :   return R->getDefInit();
    1514             : }
    1515             : 
    1516       24987 : Init *DefInit::convertInitializerTo(RecTy *Ty) const {
    1517             :   if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
    1518       24987 :     if (getType()->typeIsConvertibleTo(RRT))
    1519       24987 :       return const_cast<DefInit *>(this);
    1520             :   return nullptr;
    1521             : }
    1522             : 
    1523     1388760 : RecTy *DefInit::getFieldType(StringInit *FieldName) const {
    1524     2777520 :   if (const RecordVal *RV = Def->getValue(FieldName))
    1525     1388760 :     return RV->getType();
    1526             :   return nullptr;
    1527             : }
    1528             : 
    1529     4071022 : std::string DefInit::getAsString() const {
    1530     8142044 :   return Def->getName();
    1531             : }
    1532             : 
    1533     7224524 : static void ProfileVarDefInit(FoldingSetNodeID &ID,
    1534             :                               Record *Class,
    1535             :                               ArrayRef<Init *> Args) {
    1536     7224524 :   ID.AddInteger(Args.size());
    1537     7224524 :   ID.AddPointer(Class);
    1538             : 
    1539    41789502 :   for (Init *I : Args)
    1540    17282489 :     ID.AddPointer(I);
    1541     7224524 : }
    1542             : 
    1543     2872305 : VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
    1544     2872539 :   static FoldingSet<VarDefInit> ThePool;
    1545             : 
    1546             :   FoldingSetNodeID ID;
    1547     2872305 :   ProfileVarDefInit(ID, Class, Args);
    1548             : 
    1549     2872305 :   void *IP = nullptr;
    1550     2595066 :   if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1551             :     return I;
    1552             : 
    1553             :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
    1554      277239 :                                  alignof(VarDefInit));
    1555      277239 :   VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
    1556             :   std::uninitialized_copy(Args.begin(), Args.end(),
    1557             :                           I->getTrailingObjects<Init *>());
    1558      277239 :   ThePool.InsertNode(I, IP);
    1559      277239 :   return I;
    1560             : }
    1561             : 
    1562     4352219 : void VarDefInit::Profile(FoldingSetNodeID &ID) const {
    1563     4352219 :   ProfileVarDefInit(ID, Class, args());
    1564     4352219 : }
    1565             : 
    1566     2267252 : DefInit *VarDefInit::instantiate() {
    1567     2267252 :   if (!Def) {
    1568      258114 :     RecordKeeper &Records = Class->getRecords();
    1569      258114 :     auto NewRecOwner = make_unique<Record>(Records.getNewAnonymousName(),
    1570      516228 :                                            Class->getLoc(), Records,
    1571      516228 :                                            /*IsAnonymous=*/true);
    1572             :     Record *NewRec = NewRecOwner.get();
    1573             : 
    1574             :     // Copy values from class to instance
    1575     5503762 :     for (const RecordVal &Val : Class->getValues())
    1576             :       NewRec->addValue(Val);
    1577             : 
    1578             :     // Substitute and resolve template arguments
    1579      258114 :     ArrayRef<Init *> TArgs = Class->getTemplateArgs();
    1580             :     MapResolver R(NewRec);
    1581             : 
    1582      973445 :     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
    1583      715331 :       if (i < args_size())
    1584      645227 :         R.set(TArgs[i], getArg(i));
    1585             :       else
    1586      140208 :         R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
    1587             : 
    1588      715331 :       NewRec->removeValue(TArgs[i]);
    1589             :     }
    1590             : 
    1591      258114 :     NewRec->resolveReferences(R);
    1592             : 
    1593             :     // Add superclasses.
    1594      258114 :     ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
    1595      555338 :     for (const auto &SCPair : SCs)
    1596      148612 :       NewRec->addSuperClass(SCPair.first, SCPair.second);
    1597             : 
    1598             :     NewRec->addSuperClass(Class,
    1599             :                           SMRange(Class->getLoc().back(),
    1600      258114 :                                   Class->getLoc().back()));
    1601             : 
    1602             :     // Resolve internal references and store in record keeper
    1603      258114 :     NewRec->resolveReferences();
    1604      258114 :     Records.addDef(std::move(NewRecOwner));
    1605             : 
    1606      258114 :     Def = DefInit::get(NewRec);
    1607             :   }
    1608             : 
    1609     2267252 :   return Def;
    1610             : }
    1611             : 
    1612     4252603 : Init *VarDefInit::resolveReferences(Resolver &R) const {
    1613             :   TrackUnresolvedResolver UR(&R);
    1614             :   bool Changed = false;
    1615             :   SmallVector<Init *, 8> NewArgs;
    1616     8505206 :   NewArgs.reserve(args_size());
    1617             : 
    1618    25282749 :   for (Init *Arg : args()) {
    1619    10515073 :     Init *NewArg = Arg->resolveReferences(UR);
    1620    10515073 :     NewArgs.push_back(NewArg);
    1621    10515073 :     Changed |= NewArg != Arg;
    1622             :   }
    1623             : 
    1624     4252603 :   if (Changed) {
    1625     2572746 :     auto New = VarDefInit::get(Class, NewArgs);
    1626     2572746 :     if (!UR.foundUnresolved())
    1627     2183210 :       return New->instantiate();
    1628             :     return New;
    1629             :   }
    1630     1679857 :   return const_cast<VarDefInit *>(this);
    1631             : }
    1632             : 
    1633      299559 : Init *VarDefInit::Fold() const {
    1634      299559 :   if (Def)
    1635             :     return Def;
    1636             : 
    1637             :   TrackUnresolvedResolver R;
    1638      463154 :   for (Init *Arg : args())
    1639      185748 :     Arg->resolveReferences(R);
    1640             : 
    1641       91658 :   if (!R.foundUnresolved())
    1642       84042 :     return const_cast<VarDefInit *>(this)->instantiate();
    1643        7616 :   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    14833865 : FieldInit *FieldInit::get(Init *R, StringInit *FN) {
    1658             :   using Key = std::pair<Init *, StringInit *>;
    1659    14834089 :   static DenseMap<Key, FieldInit*> ThePool;
    1660             : 
    1661    14833865 :   Key TheKey(std::make_pair(R, FN));
    1662             : 
    1663             :   FieldInit *&I = ThePool[TheKey];
    1664    16388847 :   if (!I) I = new(Allocator) FieldInit(R, FN);
    1665    14833865 :   return I;
    1666             : }
    1667             : 
    1668    18638136 : Init *FieldInit::getBit(unsigned Bit) const {
    1669    18638136 :   if (getType() == BitRecTy::get())
    1670        1635 :     return const_cast<FieldInit*>(this);
    1671    18636501 :   return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
    1672             : }
    1673             : 
    1674    26564622 : Init *FieldInit::resolveReferences(Resolver &R) const {
    1675    26564622 :   Init *NewRec = Rec->resolveReferences(R);
    1676    26564622 :   if (NewRec != Rec)
    1677    14719692 :     return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
    1678    11844930 :   return const_cast<FieldInit *>(this);
    1679             : }
    1680             : 
    1681    14833865 : Init *FieldInit::Fold(Record *CurRec) const {
    1682    14833865 :   if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
    1683    14166515 :     Record *Def = DI->getDef();
    1684    14166515 :     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    28333028 :     Init *FieldVal = Def->getValue(FieldName)->getValue();
    1690    14166514 :     if (FieldVal->isComplete())
    1691             :       return FieldVal;
    1692             :   }
    1693      667350 :   return const_cast<FieldInit *>(this);
    1694             : }
    1695             : 
    1696    10950427 : static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
    1697             :                            ArrayRef<Init *> ArgRange,
    1698             :                            ArrayRef<StringInit *> NameRange) {
    1699    10950427 :   ID.AddPointer(V);
    1700    10950427 :   ID.AddPointer(VN);
    1701             : 
    1702             :   ArrayRef<Init *>::iterator Arg = ArgRange.begin();
    1703    10950427 :   ArrayRef<StringInit *>::iterator Name = NameRange.begin();
    1704    32342116 :   while (Arg != ArgRange.end()) {
    1705             :     assert(Name != NameRange.end() && "Arg name underflow!");
    1706    21391689 :     ID.AddPointer(*Arg++);
    1707    21391689 :     ID.AddPointer(*Name++);
    1708             :   }
    1709             :   assert(Name == NameRange.end() && "Arg name overflow!");
    1710    10950427 : }
    1711             : 
    1712             : DagInit *
    1713     3616950 : DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
    1714             :              ArrayRef<StringInit *> NameRange) {
    1715     3617153 :   static FoldingSet<DagInit> ThePool;
    1716             : 
    1717             :   FoldingSetNodeID ID;
    1718     3616950 :   ProfileDagInit(ID, V, VN, ArgRange, NameRange);
    1719             : 
    1720     3616950 :   void *IP = nullptr;
    1721     1802417 :   if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
    1722             :     return I;
    1723             : 
    1724     1814533 :   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
    1725     1814533 :   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     1814533 :   ThePool.InsertNode(I, IP);
    1731     1814533 :   return I;
    1732             : }
    1733             : 
    1734             : DagInit *
    1735     1195653 : 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     4881959 :   for (const auto &Arg : args) {
    1741     1843153 :     Args.push_back(Arg.first);
    1742     1843153 :     Names.push_back(Arg.second);
    1743             :   }
    1744             : 
    1745     2391306 :   return DagInit::get(V, VN, Args, Names);
    1746             : }
    1747             : 
    1748     7333477 : void DagInit::Profile(FoldingSetNodeID &ID) const {
    1749     7333477 :   ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
    1750     7333477 : }
    1751             : 
    1752    11502210 : Init *DagInit::resolveReferences(Resolver &R) const {
    1753             :   SmallVector<Init*, 8> NewArgs;
    1754    23004420 :   NewArgs.reserve(arg_size());
    1755             :   bool ArgsChanged = false;
    1756    50919230 :   for (const Init *Arg : getArgs()) {
    1757    19708510 :     Init *NewArg = Arg->resolveReferences(R);
    1758    19708510 :     NewArgs.push_back(NewArg);
    1759    19708510 :     ArgsChanged |= NewArg != Arg;
    1760             :   }
    1761             : 
    1762    11502210 :   Init *Op = Val->resolveReferences(R);
    1763    11502210 :   if (Op != Val || ArgsChanged)
    1764     2361394 :     return DagInit::get(Op, ValName, NewArgs, getArgNames());
    1765             : 
    1766     9140816 :   return const_cast<DagInit *>(this);
    1767             : }
    1768             : 
    1769     4109078 : bool DagInit::isConcrete() const {
    1770     4109078 :   if (!Val->isConcrete())
    1771             :     return false;
    1772    19093252 :   for (const Init *Elt : getArgs()) {
    1773     7492087 :     if (!Elt->isConcrete())
    1774             :       return false;
    1775             :   }
    1776             :   return true;
    1777             : }
    1778             : 
    1779        4872 : std::string DagInit::getAsString() const {
    1780        9744 :   std::string Result = "(" + Val->getAsString();
    1781        4872 :   if (ValName)
    1782           3 :     Result += ":" + ValName->getAsUnquotedString();
    1783        4872 :   if (!arg_empty()) {
    1784       14586 :     Result += " " + getArg(0)->getAsString();
    1785       11860 :     if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
    1786       14099 :     for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
    1787       27711 :       Result += ", " + getArg(i)->getAsString();
    1788       21185 :       if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
    1789             :     }
    1790             :   }
    1791        9744 :   return Result + ")";
    1792             : }
    1793             : 
    1794             : //===----------------------------------------------------------------------===//
    1795             : //    Other implementations
    1796             : //===----------------------------------------------------------------------===//
    1797             : 
    1798      595336 : RecordVal::RecordVal(Init *N, RecTy *T, bool P)
    1799      595336 :   : Name(N), TyAndPrefix(T, P) {
    1800      595336 :   setValue(UnsetInit::get());
    1801             :   assert(Value && "Cannot create unset value for current type!");
    1802      595336 : }
    1803             : 
    1804     1420421 : StringRef RecordVal::getName() const {
    1805     1420421 :   return cast<StringInit>(getNameInit())->getValue();
    1806             : }
    1807             : 
    1808   225556610 : bool RecordVal::setValue(Init *V) {
    1809   225556610 :   if (V) {
    1810   451113220 :     Value = V->getCastTo(getType());
    1811   225556610 :     if (Value) {
    1812             :       assert(!isa<TypedInit>(Value) ||
    1813             :              cast<TypedInit>(Value)->getType()->typeIsA(getType()));
    1814             :       if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
    1815    22573198 :         if (!isa<BitsInit>(Value)) {
    1816             :           SmallVector<Init *, 64> Bits;
    1817      163212 :           Bits.reserve(BTy->getNumBits());
    1818     1293946 :           for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i)
    1819     1130734 :             Bits.push_back(Value->getBit(i));
    1820      163212 :           Value = BitsInit::get(Bits);
    1821             :         }
    1822             :       }
    1823             :     }
    1824   225556610 :     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        1830 : void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
    1835        1830 :   if (getPrefix()) OS << "field ";
    1836        3660 :   OS << *getType() << " " << getNameInitAsString();
    1837             : 
    1838        1830 :   if (getValue())
    1839        1821 :     OS << " = " << *getValue();
    1840             : 
    1841        1830 :   if (PrintSem) OS << ";\n";
    1842        1830 : }
    1843             : 
    1844             : unsigned Record::LastID = 0;
    1845             : 
    1846     3588169 : void Record::checkName() {
    1847             :   // Ensure the record name has string type.
    1848     3588169 :   const TypedInit *TypedName = cast<const TypedInit>(Name);
    1849     7176338 :   if (!isa<StringRecTy>(TypedName->getType()))
    1850           0 :     PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
    1851             :                                   "' is not a string!");
    1852     3588169 : }
    1853             : 
    1854      887092 : RecordRecTy *Record::getType() {
    1855             :   SmallVector<Record *, 4> DirectSCs;
    1856      887092 :   getDirectSuperClasses(DirectSCs);
    1857     1774184 :   return RecordRecTy::get(DirectSCs);
    1858             : }
    1859             : 
    1860     9495909 : DefInit *Record::getDefInit() {
    1861     9495909 :   if (!TheInit)
    1862      886582 :     TheInit = new(Allocator) DefInit(this);
    1863     9495909 :   return TheInit;
    1864             : }
    1865             : 
    1866     1048581 : void Record::setName(Init *NewName) {
    1867     1048581 :   Name = NewName;
    1868     1048581 :   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     1048581 : }
    1881             : 
    1882     1067373 : void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
    1883             :   ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
    1884     3862895 :   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     1397761 :     Record *SC = SCs.back().first;
    1888     1397761 :     SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
    1889     1397761 :     Classes.push_back(SC);
    1890             :   }
    1891     1067373 : }
    1892             : 
    1893     7757364 : void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
    1894   438210810 :   for (RecordVal &Value : Values) {
    1895   215226726 :     if (SkipVal == &Value) // Skip resolve the same field as the given one
    1896           0 :       continue;
    1897   215226726 :     if (Init *V = Value.getValue()) {
    1898   215226718 :       Init *VR = V->resolveReferences(R);
    1899   215226715 :       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     7757361 :   Init *OldName = getNameInit();
    1915     7757361 :   Init *NewName = Name->resolveReferences(R);
    1916     7757361 :   if (NewName != OldName) {
    1917             :     // Re-register with RecordKeeper.
    1918      846741 :     setName(NewName);
    1919             :   }
    1920     7757361 : }
    1921             : 
    1922     3140894 : void Record::resolveReferences() {
    1923     3140892 :   RecordResolver R(*this);
    1924             :   R.setFinal(true);
    1925     3140894 :   resolveReferences(R);
    1926     3140892 : }
    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         905 : raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
    1938         905 :   OS << R.getNameInitAsString();
    1939             : 
    1940             :   ArrayRef<Init *> TArgs = R.getTemplateArgs();
    1941         905 :   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         905 :   OS << " {";
    1955             :   ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
    1956         905 :   if (!SC.empty()) {
    1957         628 :     OS << "\t//";
    1958        2062 :     for (const auto &SuperPair : SC)
    1959        1434 :       OS << " " << SuperPair.first->getNameInitAsString();
    1960             :   }
    1961         905 :   OS << "\n";
    1962             : 
    1963        4565 :   for (const RecordVal &Val : R.getValues())
    1964        1833 :     if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
    1965           3 :       OS << Val;
    1966        4565 :   for (const RecordVal &Val : R.getValues())
    1967        3657 :     if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
    1968        1581 :       OS << Val;
    1969             : 
    1970         905 :   return OS << "}\n";
    1971             : }
    1972             : 
    1973      826472 : Init *Record::getValueInit(StringRef FieldName) const {
    1974             :   const RecordVal *R = getValue(FieldName);
    1975      826472 :   if (!R || !R->getValue())
    1976           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    1977             :       "' does not have a field named `" + FieldName + "'!\n");
    1978      826472 :   return R->getValue();
    1979             : }
    1980             : 
    1981     8747777 : StringRef Record::getValueAsString(StringRef FieldName) const {
    1982             :   const RecordVal *R = getValue(FieldName);
    1983     8747777 :   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      780664 : BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
    1997             :   const RecordVal *R = getValue(FieldName);
    1998      780664 :   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      780664 :     return BI;
    2004           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2005             :     FieldName + "' does not have a BitsInit initializer!");
    2006             : }
    2007             : 
    2008     5690480 : ListInit *Record::getValueAsListInit(StringRef FieldName) const {
    2009             :   const RecordVal *R = getValue(FieldName);
    2010     5690480 :   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     5690480 :     return LI;
    2016           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2017             :     FieldName + "' does not have a list initializer!");
    2018             : }
    2019             : 
    2020             : std::vector<Record*>
    2021     4145215 : Record::getValueAsListOfDefs(StringRef FieldName) const {
    2022     4145215 :   ListInit *List = getValueAsListInit(FieldName);
    2023             :   std::vector<Record*> Defs;
    2024    15662661 :   for (Init *I : List->getValues()) {
    2025             :     if (DefInit *DI = dyn_cast<DefInit>(I))
    2026    11517446 :       Defs.push_back(DI->getDef());
    2027             :     else
    2028           0 :       PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2029             :         FieldName + "' list is not entirely DefInit!");
    2030             :   }
    2031     4145215 :   return Defs;
    2032             : }
    2033             : 
    2034     3957395 : int64_t Record::getValueAsInt(StringRef FieldName) const {
    2035             :   const RecordVal *R = getValue(FieldName);
    2036     3957395 :   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     3957395 :     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       69392 : Record::getValueAsListOfInts(StringRef FieldName) const {
    2050       69392 :   ListInit *List = getValueAsListInit(FieldName);
    2051             :   std::vector<int64_t> Ints;
    2052      143746 :   for (Init *I : List->getValues()) {
    2053             :     if (IntInit *II = dyn_cast<IntInit>(I))
    2054       74354 :       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       69392 :   return Ints;
    2062             : }
    2063             : 
    2064             : std::vector<StringRef>
    2065       11049 : Record::getValueAsListOfStrings(StringRef FieldName) const {
    2066       11049 :   ListInit *List = getValueAsListInit(FieldName);
    2067             :   std::vector<StringRef> Strings;
    2068       34493 :   for (Init *I : List->getValues()) {
    2069             :     if (StringInit *SI = dyn_cast<StringInit>(I))
    2070       23444 :       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       11049 :   return Strings;
    2078             : }
    2079             : 
    2080    13374020 : Record *Record::getValueAsDef(StringRef FieldName) const {
    2081             :   const RecordVal *R = getValue(FieldName);
    2082    13374020 :   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    13374020 :     return DI->getDef();
    2088           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2089             :     FieldName + "' does not have a def initializer!");
    2090             : }
    2091             : 
    2092    34314501 : bool Record::getValueAsBit(StringRef FieldName) const {
    2093             :   const RecordVal *R = getValue(FieldName);
    2094    34314501 :   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    34314501 :     return BI->getValue();
    2100           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2101             :     FieldName + "' does not have a bit initializer!");
    2102             : }
    2103             : 
    2104     2406889 : bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
    2105             :   const RecordVal *R = getValue(FieldName);
    2106     2406889 :   if (!R || !R->getValue())
    2107           0 :     PrintFatalError(getLoc(), "Record `" + getName() +
    2108             :       "' does not have a field named `" + FieldName.str() + "'!\n");
    2109             : 
    2110     2406889 :   if (isa<UnsetInit>(R->getValue())) {
    2111     1763829 :     Unset = true;
    2112     1763829 :     return false;
    2113             :   }
    2114      643060 :   Unset = false;
    2115             :   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
    2116      643060 :     return BI->getValue();
    2117           0 :   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
    2118             :     FieldName + "' does not have a bit initializer!");
    2119             : }
    2120             : 
    2121     2281061 : DagInit *Record::getValueAsDag(StringRef FieldName) const {
    2122             :   const RecordVal *R = getValue(FieldName);
    2123     2281061 :   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     2281061 :     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 RecordKeeper::dump() const { errs() << *this; }
    2135             : #endif
    2136             : 
    2137          79 : raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
    2138          79 :   OS << "------------- Classes -----------------\n";
    2139         280 :   for (const auto &C : RK.getClasses())
    2140         402 :     OS << "class " << *C.second;
    2141             : 
    2142          79 :   OS << "------------- Defs -----------------\n";
    2143         783 :   for (const auto &D : RK.getDefs())
    2144        1408 :     OS << "def " << *D.second;
    2145          79 :   return OS;
    2146             : }
    2147             : 
    2148             : /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
    2149             : /// an identifier.
    2150      800581 : Init *RecordKeeper::getNewAnonymousName() {
    2151     3202324 :   return StringInit::get("anonymous_" + utostr(AnonCounter++));
    2152             : }
    2153             : 
    2154             : std::vector<Record *>
    2155        3258 : RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
    2156        3258 :   Record *Class = getClass(ClassName);
    2157        3258 :   if (!Class)
    2158           0 :     PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
    2159             : 
    2160             :   std::vector<Record*> Defs;
    2161    48631073 :   for (const auto &D : getDefs())
    2162    48627815 :     if (D.second->isSubClassOf(Class))
    2163     3892784 :       Defs.push_back(D.second.get());
    2164             : 
    2165        3258 :   return Defs;
    2166             : }
    2167             : 
    2168    63457848 : Init *MapResolver::resolve(Init *VarName) {
    2169    63457848 :   auto It = Map.find(VarName);
    2170    63457848 :   if (It == Map.end())
    2171             :     return nullptr;
    2172             : 
    2173    22059006 :   Init *I = It->second.V;
    2174             : 
    2175    39121204 :   if (!It->second.Resolved && Map.size() > 1) {
    2176             :     // Resolve mutual references among the mapped variables, but prevent
    2177             :     // infinite recursion.
    2178             :     Map.erase(It);
    2179    12722655 :     I = I->resolveReferences(*this);
    2180    12722655 :     Map[VarName] = {I, true};
    2181             :   }
    2182             : 
    2183             :   return I;
    2184             : }
    2185             : 
    2186    20618873 : Init *RecordResolver::resolve(Init *VarName) {
    2187    20618873 :   Init *Val = Cache.lookup(VarName);
    2188     4698127 :   if (Val)
    2189             :     return Val;
    2190             : 
    2191    19636130 :   for (Init *S : Stack) {
    2192     1857692 :     if (S == VarName)
    2193             :       return nullptr; // prevent infinite recursion
    2194             :   }
    2195             : 
    2196    31841492 :   if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
    2197    31841492 :     if (!isa<UnsetInit>(RV->getValue())) {
    2198             :       Val = RV->getValue();
    2199    15919037 :       Stack.push_back(VarName);
    2200    15919037 :       Val = Val->resolveReferences(*this);
    2201             :       Stack.pop_back();
    2202             :     }
    2203             :   }
    2204             : 
    2205    15920746 :   Cache[VarName] = Val;
    2206    15920746 :   return Val;
    2207             : }
    2208             : 
    2209    14047389 : Init *TrackUnresolvedResolver::resolve(Init *VarName) {
    2210             :   Init *I = nullptr;
    2211             : 
    2212    14047389 :   if (R) {
    2213    13984798 :     I = R->resolve(VarName);
    2214    13984798 :     if (I && !FoundUnresolved) {
    2215             :       // Do not recurse into the resolved initializer, as that would change
    2216             :       // the behavior of the resolver we're delegating, but do check to see
    2217             :       // if there are unresolved variables remaining.
    2218             :       TrackUnresolvedResolver Sub;
    2219     5936635 :       I->resolveReferences(Sub);
    2220     5936635 :       FoundUnresolved |= Sub.FoundUnresolved;
    2221             :     }
    2222             :   }
    2223             : 
    2224    13984798 :   if (!I)
    2225     7549174 :     FoundUnresolved = true;
    2226    14047389 :   return I;
    2227             : }
    2228             : 
    2229       14474 : Init *HasReferenceResolver::resolve(Init *VarName)
    2230             : {
    2231       14474 :   if (VarName == VarNameToTrack)
    2232       12620 :     Found = true;
    2233       14474 :   return nullptr;
    2234        1044 : }

Generated by: LCOV version 1.13