1 //===- Record.cpp - Record implementation ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implement the tablegen record classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/TableGen/Record.h"
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 <cassert>
31 #include <cstdint>
32 #include <map>
33 #include <memory>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "tblgen-records"
41 
42 //===----------------------------------------------------------------------===//
43 //    Context
44 //===----------------------------------------------------------------------===//
45 
46 namespace llvm {
47 namespace detail {
48 /// This class represents the internal implementation of the RecordKeeper.
49 /// It contains all of the contextual static state of the Record classes. It is
50 /// kept out-of-line to simplify dependencies, and also make it easier for
51 /// internal classes to access the uniquer state of the keeper.
52 struct RecordKeeperImpl {
53   RecordKeeperImpl(RecordKeeper &RK)
54       : SharedBitRecTy(RK), SharedIntRecTy(RK), SharedStringRecTy(RK),
55         SharedDagRecTy(RK), AnyRecord(RK, 0), TheUnsetInit(RK),
56         TrueBitInit(true, &SharedBitRecTy),
57         FalseBitInit(false, &SharedBitRecTy), StringInitStringPool(Allocator),
58         StringInitCodePool(Allocator), AnonCounter(0), LastRecordID(0) {}
59 
60   BumpPtrAllocator Allocator;
61   std::vector<BitsRecTy *> SharedBitsRecTys;
62   BitRecTy SharedBitRecTy;
63   IntRecTy SharedIntRecTy;
64   StringRecTy SharedStringRecTy;
65   DagRecTy SharedDagRecTy;
66 
67   RecordRecTy AnyRecord;
68   UnsetInit TheUnsetInit;
69   BitInit TrueBitInit;
70   BitInit FalseBitInit;
71 
72   FoldingSet<BitsInit> TheBitsInitPool;
73   std::map<int64_t, IntInit *> TheIntInitPool;
74   StringMap<StringInit *, BumpPtrAllocator &> StringInitStringPool;
75   StringMap<StringInit *, BumpPtrAllocator &> StringInitCodePool;
76   FoldingSet<ListInit> TheListInitPool;
77   FoldingSet<UnOpInit> TheUnOpInitPool;
78   FoldingSet<BinOpInit> TheBinOpInitPool;
79   FoldingSet<TernOpInit> TheTernOpInitPool;
80   FoldingSet<FoldOpInit> TheFoldOpInitPool;
81   FoldingSet<IsAOpInit> TheIsAOpInitPool;
82   DenseMap<std::pair<RecTy *, Init *>, VarInit *> TheVarInitPool;
83   DenseMap<std::pair<TypedInit *, unsigned>, VarBitInit *> TheVarBitInitPool;
84   DenseMap<std::pair<TypedInit *, unsigned>, VarListElementInit *>
85       TheVarListElementInitPool;
86   FoldingSet<VarDefInit> TheVarDefInitPool;
87   DenseMap<std::pair<Init *, StringInit *>, FieldInit *> TheFieldInitPool;
88   FoldingSet<CondOpInit> TheCondOpInitPool;
89   FoldingSet<DagInit> TheDagInitPool;
90   FoldingSet<RecordRecTy> RecordTypePool;
91 
92   unsigned AnonCounter;
93   unsigned LastRecordID;
94 };
95 } // namespace detail
96 } // namespace llvm
97 
98 //===----------------------------------------------------------------------===//
99 //    Type implementations
100 //===----------------------------------------------------------------------===//
101 
102 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
103 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
104 #endif
105 
106 ListRecTy *RecTy::getListTy() {
107   if (!ListTy)
108     ListTy = new (RK.getImpl().Allocator) ListRecTy(this);
109   return ListTy;
110 }
111 
112 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
113   assert(RHS && "NULL pointer");
114   return Kind == RHS->getRecTyKind();
115 }
116 
117 bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
118 
119 BitRecTy *BitRecTy::get(RecordKeeper &RK) {
120   return &RK.getImpl().SharedBitRecTy;
121 }
122 
123 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
124   if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
125     return true;
126   if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
127     return BitsTy->getNumBits() == 1;
128   return false;
129 }
130 
131 BitsRecTy *BitsRecTy::get(RecordKeeper &RK, unsigned Sz) {
132   detail::RecordKeeperImpl &RKImpl = RK.getImpl();
133   if (Sz >= RKImpl.SharedBitsRecTys.size())
134     RKImpl.SharedBitsRecTys.resize(Sz + 1);
135   BitsRecTy *&Ty = RKImpl.SharedBitsRecTys[Sz];
136   if (!Ty)
137     Ty = new (RKImpl.Allocator) BitsRecTy(RK, Sz);
138   return Ty;
139 }
140 
141 std::string BitsRecTy::getAsString() const {
142   return "bits<" + utostr(Size) + ">";
143 }
144 
145 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
146   if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
147     return cast<BitsRecTy>(RHS)->Size == Size;
148   RecTyKind kind = RHS->getRecTyKind();
149   return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
150 }
151 
152 bool BitsRecTy::typeIsA(const RecTy *RHS) const {
153   if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
154     return RHSb->Size == Size;
155   return false;
156 }
157 
158 IntRecTy *IntRecTy::get(RecordKeeper &RK) {
159   return &RK.getImpl().SharedIntRecTy;
160 }
161 
162 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
163   RecTyKind kind = RHS->getRecTyKind();
164   return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
165 }
166 
167 StringRecTy *StringRecTy::get(RecordKeeper &RK) {
168   return &RK.getImpl().SharedStringRecTy;
169 }
170 
171 std::string StringRecTy::getAsString() const {
172   return "string";
173 }
174 
175 bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
176   RecTyKind Kind = RHS->getRecTyKind();
177   return Kind == StringRecTyKind;
178 }
179 
180 std::string ListRecTy::getAsString() const {
181   return "list<" + ElementTy->getAsString() + ">";
182 }
183 
184 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
185   if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
186     return ElementTy->typeIsConvertibleTo(ListTy->getElementType());
187   return false;
188 }
189 
190 bool ListRecTy::typeIsA(const RecTy *RHS) const {
191   if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
192     return getElementType()->typeIsA(RHSl->getElementType());
193   return false;
194 }
195 
196 DagRecTy *DagRecTy::get(RecordKeeper &RK) {
197   return &RK.getImpl().SharedDagRecTy;
198 }
199 
200 std::string DagRecTy::getAsString() const {
201   return "dag";
202 }
203 
204 static void ProfileRecordRecTy(FoldingSetNodeID &ID,
205                                ArrayRef<Record *> Classes) {
206   ID.AddInteger(Classes.size());
207   for (Record *R : Classes)
208     ID.AddPointer(R);
209 }
210 
211 RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
212                               ArrayRef<Record *> UnsortedClasses) {
213   detail::RecordKeeperImpl &RKImpl = RK.getImpl();
214   if (UnsortedClasses.empty())
215     return &RKImpl.AnyRecord;
216 
217   FoldingSet<RecordRecTy> &ThePool = RKImpl.RecordTypePool;
218 
219   SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
220                                    UnsortedClasses.end());
221   llvm::sort(Classes, [](Record *LHS, Record *RHS) {
222     return LHS->getNameInitAsString() < RHS->getNameInitAsString();
223   });
224 
225   FoldingSetNodeID ID;
226   ProfileRecordRecTy(ID, Classes);
227 
228   void *IP = nullptr;
229   if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
230     return Ty;
231 
232 #ifndef NDEBUG
233   // Check for redundancy.
234   for (unsigned i = 0; i < Classes.size(); ++i) {
235     for (unsigned j = 0; j < Classes.size(); ++j) {
236       assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
237     }
238     assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
239   }
240 #endif
241 
242   void *Mem = RKImpl.Allocator.Allocate(
243       totalSizeToAlloc<Record *>(Classes.size()), alignof(RecordRecTy));
244   RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes.size());
245   std::uninitialized_copy(Classes.begin(), Classes.end(),
246                           Ty->getTrailingObjects<Record *>());
247   ThePool.InsertNode(Ty, IP);
248   return Ty;
249 }
250 RecordRecTy *RecordRecTy::get(Record *Class) {
251   assert(Class && "unexpected null class");
252   return get(Class->getRecords(), Class);
253 }
254 
255 void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
256   ProfileRecordRecTy(ID, getClasses());
257 }
258 
259 std::string RecordRecTy::getAsString() const {
260   if (NumClasses == 1)
261     return getClasses()[0]->getNameInitAsString();
262 
263   std::string Str = "{";
264   bool First = true;
265   for (Record *R : getClasses()) {
266     if (!First)
267       Str += ", ";
268     First = false;
269     Str += R->getNameInitAsString();
270   }
271   Str += "}";
272   return Str;
273 }
274 
275 bool RecordRecTy::isSubClassOf(Record *Class) const {
276   return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
277                                       return MySuperClass == Class ||
278                                              MySuperClass->isSubClassOf(Class);
279                                     });
280 }
281 
282 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
283   if (this == RHS)
284     return true;
285 
286   const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
287   if (!RTy)
288     return false;
289 
290   return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
291                                            return isSubClassOf(TargetClass);
292                                          });
293 }
294 
295 bool RecordRecTy::typeIsA(const RecTy *RHS) const {
296   return typeIsConvertibleTo(RHS);
297 }
298 
299 static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
300   SmallVector<Record *, 4> CommonSuperClasses;
301   SmallVector<Record *, 4> Stack(T1->classes_begin(), T1->classes_end());
302 
303   while (!Stack.empty()) {
304     Record *R = Stack.pop_back_val();
305 
306     if (T2->isSubClassOf(R)) {
307       CommonSuperClasses.push_back(R);
308     } else {
309       R->getDirectSuperClasses(Stack);
310     }
311   }
312 
313   return RecordRecTy::get(T1->getRecordKeeper(), CommonSuperClasses);
314 }
315 
316 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
317   if (T1 == T2)
318     return T1;
319 
320   if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
321     if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
322       return resolveRecordTypes(RecTy1, RecTy2);
323   }
324 
325   if (T1->typeIsConvertibleTo(T2))
326     return T2;
327   if (T2->typeIsConvertibleTo(T1))
328     return T1;
329 
330   if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
331     if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
332       RecTy* NewType = resolveTypes(ListTy1->getElementType(),
333                                     ListTy2->getElementType());
334       if (NewType)
335         return NewType->getListTy();
336     }
337   }
338 
339   return nullptr;
340 }
341 
342 //===----------------------------------------------------------------------===//
343 //    Initializer implementations
344 //===----------------------------------------------------------------------===//
345 
346 void Init::anchor() {}
347 
348 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
349 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
350 #endif
351 
352 RecordKeeper &Init::getRecordKeeper() const {
353   if (auto *TyInit = dyn_cast<TypedInit>(this))
354     return TyInit->getType()->getRecordKeeper();
355   return cast<UnsetInit>(this)->getRecordKeeper();
356 }
357 
358 UnsetInit *UnsetInit::get(RecordKeeper &RK) {
359   return &RK.getImpl().TheUnsetInit;
360 }
361 
362 Init *UnsetInit::getCastTo(RecTy *Ty) const {
363   return const_cast<UnsetInit *>(this);
364 }
365 
366 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
367   return const_cast<UnsetInit *>(this);
368 }
369 
370 BitInit *BitInit::get(RecordKeeper &RK, bool V) {
371   return V ? &RK.getImpl().TrueBitInit : &RK.getImpl().FalseBitInit;
372 }
373 
374 Init *BitInit::convertInitializerTo(RecTy *Ty) const {
375   if (isa<BitRecTy>(Ty))
376     return const_cast<BitInit *>(this);
377 
378   if (isa<IntRecTy>(Ty))
379     return IntInit::get(getRecordKeeper(), getValue());
380 
381   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
382     // Can only convert single bit.
383     if (BRT->getNumBits() == 1)
384       return BitsInit::get(getRecordKeeper(), const_cast<BitInit *>(this));
385   }
386 
387   return nullptr;
388 }
389 
390 static void
391 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
392   ID.AddInteger(Range.size());
393 
394   for (Init *I : Range)
395     ID.AddPointer(I);
396 }
397 
398 BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<Init *> Range) {
399   FoldingSetNodeID ID;
400   ProfileBitsInit(ID, Range);
401 
402   detail::RecordKeeperImpl &RKImpl = RK.getImpl();
403   void *IP = nullptr;
404   if (BitsInit *I = RKImpl.TheBitsInitPool.FindNodeOrInsertPos(ID, IP))
405     return I;
406 
407   void *Mem = RKImpl.Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
408                                         alignof(BitsInit));
409   BitsInit *I = new (Mem) BitsInit(RK, Range.size());
410   std::uninitialized_copy(Range.begin(), Range.end(),
411                           I->getTrailingObjects<Init *>());
412   RKImpl.TheBitsInitPool.InsertNode(I, IP);
413   return I;
414 }
415 
416 void BitsInit::Profile(FoldingSetNodeID &ID) const {
417   ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
418 }
419 
420 Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
421   if (isa<BitRecTy>(Ty)) {
422     if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
423     return getBit(0);
424   }
425 
426   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
427     // If the number of bits is right, return it.  Otherwise we need to expand
428     // or truncate.
429     if (getNumBits() != BRT->getNumBits()) return nullptr;
430     return const_cast<BitsInit *>(this);
431   }
432 
433   if (isa<IntRecTy>(Ty)) {
434     int64_t Result = 0;
435     for (unsigned i = 0, e = getNumBits(); i != e; ++i)
436       if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
437         Result |= static_cast<int64_t>(Bit->getValue()) << i;
438       else
439         return nullptr;
440     return IntInit::get(getRecordKeeper(), Result);
441   }
442 
443   return nullptr;
444 }
445 
446 Init *
447 BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
448   SmallVector<Init *, 16> NewBits(Bits.size());
449 
450   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
451     if (Bits[i] >= getNumBits())
452       return nullptr;
453     NewBits[i] = getBit(Bits[i]);
454   }
455   return BitsInit::get(getRecordKeeper(), NewBits);
456 }
457 
458 bool BitsInit::isConcrete() const {
459   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
460     if (!getBit(i)->isConcrete())
461       return false;
462   }
463   return true;
464 }
465 
466 std::string BitsInit::getAsString() const {
467   std::string Result = "{ ";
468   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
469     if (i) Result += ", ";
470     if (Init *Bit = getBit(e-i-1))
471       Result += Bit->getAsString();
472     else
473       Result += "*";
474   }
475   return Result + " }";
476 }
477 
478 // resolveReferences - If there are any field references that refer to fields
479 // that have been filled in, we can propagate the values now.
480 Init *BitsInit::resolveReferences(Resolver &R) const {
481   bool Changed = false;
482   SmallVector<Init *, 16> NewBits(getNumBits());
483 
484   Init *CachedBitVarRef = nullptr;
485   Init *CachedBitVarResolved = nullptr;
486 
487   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
488     Init *CurBit = getBit(i);
489     Init *NewBit = CurBit;
490 
491     if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
492       if (CurBitVar->getBitVar() != CachedBitVarRef) {
493         CachedBitVarRef = CurBitVar->getBitVar();
494         CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
495       }
496       assert(CachedBitVarResolved && "Unresolved bitvar reference");
497       NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
498     } else {
499       // getBit(0) implicitly converts int and bits<1> values to bit.
500       NewBit = CurBit->resolveReferences(R)->getBit(0);
501     }
502 
503     if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
504       NewBit = CurBit;
505     NewBits[i] = NewBit;
506     Changed |= CurBit != NewBit;
507   }
508 
509   if (Changed)
510     return BitsInit::get(getRecordKeeper(), NewBits);
511 
512   return const_cast<BitsInit *>(this);
513 }
514 
515 IntInit *IntInit::get(RecordKeeper &RK, int64_t V) {
516   IntInit *&I = RK.getImpl().TheIntInitPool[V];
517   if (!I)
518     I = new (RK.getImpl().Allocator) IntInit(RK, V);
519   return I;
520 }
521 
522 std::string IntInit::getAsString() const {
523   return itostr(Value);
524 }
525 
526 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
527   // For example, with NumBits == 4, we permit Values from [-7 .. 15].
528   return (NumBits >= sizeof(Value) * 8) ||
529          (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
530 }
531 
532 Init *IntInit::convertInitializerTo(RecTy *Ty) const {
533   if (isa<IntRecTy>(Ty))
534     return const_cast<IntInit *>(this);
535 
536   if (isa<BitRecTy>(Ty)) {
537     int64_t Val = getValue();
538     if (Val != 0 && Val != 1) return nullptr;  // Only accept 0 or 1 for a bit!
539     return BitInit::get(getRecordKeeper(), Val != 0);
540   }
541 
542   if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
543     int64_t Value = getValue();
544     // Make sure this bitfield is large enough to hold the integer value.
545     if (!canFitInBitfield(Value, BRT->getNumBits()))
546       return nullptr;
547 
548     SmallVector<Init *, 16> NewBits(BRT->getNumBits());
549     for (unsigned i = 0; i != BRT->getNumBits(); ++i)
550       NewBits[i] =
551           BitInit::get(getRecordKeeper(), Value & ((i < 64) ? (1LL << i) : 0));
552 
553     return BitsInit::get(getRecordKeeper(), NewBits);
554   }
555 
556   return nullptr;
557 }
558 
559 Init *
560 IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
561   SmallVector<Init *, 16> NewBits(Bits.size());
562 
563   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
564     if (Bits[i] >= 64)
565       return nullptr;
566 
567     NewBits[i] =
568         BitInit::get(getRecordKeeper(), Value & (INT64_C(1) << Bits[i]));
569   }
570   return BitsInit::get(getRecordKeeper(), NewBits);
571 }
572 
573 AnonymousNameInit *AnonymousNameInit::get(RecordKeeper &RK, unsigned V) {
574   return new (RK.getImpl().Allocator) AnonymousNameInit(RK, V);
575 }
576 
577 StringInit *AnonymousNameInit::getNameInit() const {
578   return StringInit::get(getRecordKeeper(), getAsString());
579 }
580 
581 std::string AnonymousNameInit::getAsString() const {
582   return "anonymous_" + utostr(Value);
583 }
584 
585 Init *AnonymousNameInit::resolveReferences(Resolver &R) const {
586   auto *Old = const_cast<Init *>(static_cast<const Init *>(this));
587   auto *New = R.resolve(Old);
588   New = New ? New : Old;
589   if (R.isFinal())
590     if (auto *Anonymous = dyn_cast<AnonymousNameInit>(New))
591       return Anonymous->getNameInit();
592   return New;
593 }
594 
595 StringInit *StringInit::get(RecordKeeper &RK, StringRef V, StringFormat Fmt) {
596   detail::RecordKeeperImpl &RKImpl = RK.getImpl();
597   auto &InitMap = Fmt == SF_String ? RKImpl.StringInitStringPool
598                                    : RKImpl.StringInitCodePool;
599   auto &Entry = *InitMap.insert(std::make_pair(V, nullptr)).first;
600   if (!Entry.second)
601     Entry.second = new (RKImpl.Allocator) StringInit(RK, Entry.getKey(), Fmt);
602   return Entry.second;
603 }
604 
605 Init *StringInit::convertInitializerTo(RecTy *Ty) const {
606   if (isa<StringRecTy>(Ty))
607     return const_cast<StringInit *>(this);
608 
609   return nullptr;
610 }
611 
612 static void ProfileListInit(FoldingSetNodeID &ID,
613                             ArrayRef<Init *> Range,
614                             RecTy *EltTy) {
615   ID.AddInteger(Range.size());
616   ID.AddPointer(EltTy);
617 
618   for (Init *I : Range)
619     ID.AddPointer(I);
620 }
621 
622 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
623   FoldingSetNodeID ID;
624   ProfileListInit(ID, Range, EltTy);
625 
626   detail::RecordKeeperImpl &RK = EltTy->getRecordKeeper().getImpl();
627   void *IP = nullptr;
628   if (ListInit *I = RK.TheListInitPool.FindNodeOrInsertPos(ID, IP))
629     return I;
630 
631   assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
632          cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
633 
634   void *Mem = RK.Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
635                                     alignof(ListInit));
636   ListInit *I = new (Mem) ListInit(Range.size(), EltTy);
637   std::uninitialized_copy(Range.begin(), Range.end(),
638                           I->getTrailingObjects<Init *>());
639   RK.TheListInitPool.InsertNode(I, IP);
640   return I;
641 }
642 
643 void ListInit::Profile(FoldingSetNodeID &ID) const {
644   RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
645 
646   ProfileListInit(ID, getValues(), EltTy);
647 }
648 
649 Init *ListInit::convertInitializerTo(RecTy *Ty) const {
650   if (getType() == Ty)
651     return const_cast<ListInit*>(this);
652 
653   if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
654     SmallVector<Init*, 8> Elements;
655     Elements.reserve(getValues().size());
656 
657     // Verify that all of the elements of the list are subclasses of the
658     // appropriate class!
659     bool Changed = false;
660     RecTy *ElementType = LRT->getElementType();
661     for (Init *I : getValues())
662       if (Init *CI = I->convertInitializerTo(ElementType)) {
663         Elements.push_back(CI);
664         if (CI != I)
665           Changed = true;
666       } else
667         return nullptr;
668 
669     if (!Changed)
670       return const_cast<ListInit*>(this);
671     return ListInit::get(Elements, ElementType);
672   }
673 
674   return nullptr;
675 }
676 
677 Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
678   if (Elements.size() == 1) {
679     if (Elements[0] >= size())
680       return nullptr;
681     return getElement(Elements[0]);
682   }
683 
684   SmallVector<Init*, 8> Vals;
685   Vals.reserve(Elements.size());
686   for (unsigned Element : Elements) {
687     if (Element >= size())
688       return nullptr;
689     Vals.push_back(getElement(Element));
690   }
691   return ListInit::get(Vals, getElementType());
692 }
693 
694 Record *ListInit::getElementAsRecord(unsigned i) const {
695   assert(i < NumValues && "List element index out of range!");
696   DefInit *DI = dyn_cast<DefInit>(getElement(i));
697   if (!DI)
698     PrintFatalError("Expected record in list!");
699   return DI->getDef();
700 }
701 
702 Init *ListInit::resolveReferences(Resolver &R) const {
703   SmallVector<Init*, 8> Resolved;
704   Resolved.reserve(size());
705   bool Changed = false;
706 
707   for (Init *CurElt : getValues()) {
708     Init *E = CurElt->resolveReferences(R);
709     Changed |= E != CurElt;
710     Resolved.push_back(E);
711   }
712 
713   if (Changed)
714     return ListInit::get(Resolved, getElementType());
715   return const_cast<ListInit *>(this);
716 }
717 
718 bool ListInit::isComplete() const {
719   for (Init *Element : *this) {
720     if (!Element->isComplete())
721       return false;
722   }
723   return true;
724 }
725 
726 bool ListInit::isConcrete() const {
727   for (Init *Element : *this) {
728     if (!Element->isConcrete())
729       return false;
730   }
731   return true;
732 }
733 
734 std::string ListInit::getAsString() const {
735   std::string Result = "[";
736   const char *sep = "";
737   for (Init *Element : *this) {
738     Result += sep;
739     sep = ", ";
740     Result += Element->getAsString();
741   }
742   return Result + "]";
743 }
744 
745 Init *OpInit::getBit(unsigned Bit) const {
746   if (getType() == BitRecTy::get(getRecordKeeper()))
747     return const_cast<OpInit*>(this);
748   return VarBitInit::get(const_cast<OpInit*>(this), Bit);
749 }
750 
751 static void
752 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
753   ID.AddInteger(Opcode);
754   ID.AddPointer(Op);
755   ID.AddPointer(Type);
756 }
757 
758 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
759   FoldingSetNodeID ID;
760   ProfileUnOpInit(ID, Opc, LHS, Type);
761 
762   detail::RecordKeeperImpl &RK = Type->getRecordKeeper().getImpl();
763   void *IP = nullptr;
764   if (UnOpInit *I = RK.TheUnOpInitPool.FindNodeOrInsertPos(ID, IP))
765     return I;
766 
767   UnOpInit *I = new (RK.Allocator) UnOpInit(Opc, LHS, Type);
768   RK.TheUnOpInitPool.InsertNode(I, IP);
769   return I;
770 }
771 
772 void UnOpInit::Profile(FoldingSetNodeID &ID) const {
773   ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
774 }
775 
776 Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
777   RecordKeeper &RK = getRecordKeeper();
778   switch (getOpcode()) {
779   case CAST:
780     if (isa<StringRecTy>(getType())) {
781       if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
782         return LHSs;
783 
784       if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
785         return StringInit::get(RK, LHSd->getAsString());
786 
787       if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
788               LHS->convertInitializerTo(IntRecTy::get(RK))))
789         return StringInit::get(RK, LHSi->getAsString());
790 
791     } else if (isa<RecordRecTy>(getType())) {
792       if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
793         if (!CurRec && !IsFinal)
794           break;
795         assert(CurRec && "NULL pointer");
796         Record *D;
797 
798         // Self-references are allowed, but their resolution is delayed until
799         // the final resolve to ensure that we get the correct type for them.
800         auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
801         if (Name == CurRec->getNameInit() ||
802             (Anonymous && Name == Anonymous->getNameInit())) {
803           if (!IsFinal)
804             break;
805           D = CurRec;
806         } else {
807           D = CurRec->getRecords().getDef(Name->getValue());
808           if (!D) {
809             if (IsFinal)
810               PrintFatalError(CurRec->getLoc(),
811                               Twine("Undefined reference to record: '") +
812                               Name->getValue() + "'\n");
813             break;
814           }
815         }
816 
817         DefInit *DI = DefInit::get(D);
818         if (!DI->getType()->typeIsA(getType())) {
819           PrintFatalError(CurRec->getLoc(),
820                           Twine("Expected type '") +
821                           getType()->getAsString() + "', got '" +
822                           DI->getType()->getAsString() + "' in: " +
823                           getAsString() + "\n");
824         }
825         return DI;
826       }
827     }
828 
829     if (Init *NewInit = LHS->convertInitializerTo(getType()))
830       return NewInit;
831     break;
832 
833   case NOT:
834     if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
835             LHS->convertInitializerTo(IntRecTy::get(RK))))
836       return IntInit::get(RK, LHSi->getValue() ? 0 : 1);
837     break;
838 
839   case HEAD:
840     if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
841       assert(!LHSl->empty() && "Empty list in head");
842       return LHSl->getElement(0);
843     }
844     break;
845 
846   case TAIL:
847     if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
848       assert(!LHSl->empty() && "Empty list in tail");
849       // Note the +1.  We can't just pass the result of getValues()
850       // directly.
851       return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
852     }
853     break;
854 
855   case SIZE:
856     if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
857       return IntInit::get(RK, LHSl->size());
858     if (DagInit *LHSd = dyn_cast<DagInit>(LHS))
859       return IntInit::get(RK, LHSd->arg_size());
860     if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
861       return IntInit::get(RK, LHSs->getValue().size());
862     break;
863 
864   case EMPTY:
865     if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
866       return IntInit::get(RK, LHSl->empty());
867     if (DagInit *LHSd = dyn_cast<DagInit>(LHS))
868       return IntInit::get(RK, LHSd->arg_empty());
869     if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
870       return IntInit::get(RK, LHSs->getValue().empty());
871     break;
872 
873   case GETDAGOP:
874     if (DagInit *Dag = dyn_cast<DagInit>(LHS)) {
875       DefInit *DI = DefInit::get(Dag->getOperatorAsDef({}));
876       if (!DI->getType()->typeIsA(getType())) {
877         PrintFatalError(CurRec->getLoc(),
878                         Twine("Expected type '") +
879                         getType()->getAsString() + "', got '" +
880                         DI->getType()->getAsString() + "' in: " +
881                         getAsString() + "\n");
882       } else {
883         return DI;
884       }
885     }
886     break;
887   }
888   return const_cast<UnOpInit *>(this);
889 }
890 
891 Init *UnOpInit::resolveReferences(Resolver &R) const {
892   Init *lhs = LHS->resolveReferences(R);
893 
894   if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
895     return (UnOpInit::get(getOpcode(), lhs, getType()))
896         ->Fold(R.getCurrentRecord(), R.isFinal());
897   return const_cast<UnOpInit *>(this);
898 }
899 
900 std::string UnOpInit::getAsString() const {
901   std::string Result;
902   switch (getOpcode()) {
903   case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
904   case NOT: Result = "!not"; break;
905   case HEAD: Result = "!head"; break;
906   case TAIL: Result = "!tail"; break;
907   case SIZE: Result = "!size"; break;
908   case EMPTY: Result = "!empty"; break;
909   case GETDAGOP: Result = "!getdagop"; break;
910   }
911   return Result + "(" + LHS->getAsString() + ")";
912 }
913 
914 static void
915 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
916                  RecTy *Type) {
917   ID.AddInteger(Opcode);
918   ID.AddPointer(LHS);
919   ID.AddPointer(RHS);
920   ID.AddPointer(Type);
921 }
922 
923 BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, Init *RHS, RecTy *Type) {
924   FoldingSetNodeID ID;
925   ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
926 
927   detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl();
928   void *IP = nullptr;
929   if (BinOpInit *I = RK.TheBinOpInitPool.FindNodeOrInsertPos(ID, IP))
930     return I;
931 
932   BinOpInit *I = new (RK.Allocator) BinOpInit(Opc, LHS, RHS, Type);
933   RK.TheBinOpInitPool.InsertNode(I, IP);
934   return I;
935 }
936 
937 void BinOpInit::Profile(FoldingSetNodeID &ID) const {
938   ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
939 }
940 
941 static StringInit *ConcatStringInits(const StringInit *I0,
942                                      const StringInit *I1) {
943   SmallString<80> Concat(I0->getValue());
944   Concat.append(I1->getValue());
945   return StringInit::get(
946       I0->getRecordKeeper(), Concat,
947       StringInit::determineFormat(I0->getFormat(), I1->getFormat()));
948 }
949 
950 static StringInit *interleaveStringList(const ListInit *List,
951                                         const StringInit *Delim) {
952   if (List->size() == 0)
953     return StringInit::get(List->getRecordKeeper(), "");
954   StringInit *Element = dyn_cast<StringInit>(List->getElement(0));
955   if (!Element)
956     return nullptr;
957   SmallString<80> Result(Element->getValue());
958   StringInit::StringFormat Fmt = StringInit::SF_String;
959 
960   for (unsigned I = 1, E = List->size(); I < E; ++I) {
961     Result.append(Delim->getValue());
962     StringInit *Element = dyn_cast<StringInit>(List->getElement(I));
963     if (!Element)
964       return nullptr;
965     Result.append(Element->getValue());
966     Fmt = StringInit::determineFormat(Fmt, Element->getFormat());
967   }
968   return StringInit::get(List->getRecordKeeper(), Result, Fmt);
969 }
970 
971 static StringInit *interleaveIntList(const ListInit *List,
972                                      const StringInit *Delim) {
973   RecordKeeper &RK = List->getRecordKeeper();
974   if (List->size() == 0)
975     return StringInit::get(RK, "");
976   IntInit *Element = dyn_cast_or_null<IntInit>(
977       List->getElement(0)->convertInitializerTo(IntRecTy::get(RK)));
978   if (!Element)
979     return nullptr;
980   SmallString<80> Result(Element->getAsString());
981 
982   for (unsigned I = 1, E = List->size(); I < E; ++I) {
983     Result.append(Delim->getValue());
984     IntInit *Element = dyn_cast_or_null<IntInit>(
985         List->getElement(I)->convertInitializerTo(IntRecTy::get(RK)));
986     if (!Element)
987       return nullptr;
988     Result.append(Element->getAsString());
989   }
990   return StringInit::get(RK, Result);
991 }
992 
993 Init *BinOpInit::getStrConcat(Init *I0, Init *I1) {
994   // Shortcut for the common case of concatenating two strings.
995   if (const StringInit *I0s = dyn_cast<StringInit>(I0))
996     if (const StringInit *I1s = dyn_cast<StringInit>(I1))
997       return ConcatStringInits(I0s, I1s);
998   return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1,
999                         StringRecTy::get(I0->getRecordKeeper()));
1000 }
1001 
1002 static ListInit *ConcatListInits(const ListInit *LHS,
1003                                  const ListInit *RHS) {
1004   SmallVector<Init *, 8> Args;
1005   llvm::append_range(Args, *LHS);
1006   llvm::append_range(Args, *RHS);
1007   return ListInit::get(Args, LHS->getElementType());
1008 }
1009 
1010 Init *BinOpInit::getListConcat(TypedInit *LHS, Init *RHS) {
1011   assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list");
1012 
1013   // Shortcut for the common case of concatenating two lists.
1014    if (const ListInit *LHSList = dyn_cast<ListInit>(LHS))
1015      if (const ListInit *RHSList = dyn_cast<ListInit>(RHS))
1016        return ConcatListInits(LHSList, RHSList);
1017    return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType());
1018 }
1019 
1020 Init *BinOpInit::Fold(Record *CurRec) const {
1021   switch (getOpcode()) {
1022   case CONCAT: {
1023     DagInit *LHSs = dyn_cast<DagInit>(LHS);
1024     DagInit *RHSs = dyn_cast<DagInit>(RHS);
1025     if (LHSs && RHSs) {
1026       DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
1027       DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
1028       if ((!LOp && !isa<UnsetInit>(LHSs->getOperator())) ||
1029           (!ROp && !isa<UnsetInit>(RHSs->getOperator())))
1030         break;
1031       if (LOp && ROp && LOp->getDef() != ROp->getDef()) {
1032         PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
1033                         LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
1034                         "'");
1035       }
1036       Init *Op = LOp ? LOp : ROp;
1037       if (!Op)
1038         Op = UnsetInit::get(getRecordKeeper());
1039 
1040       SmallVector<Init*, 8> Args;
1041       SmallVector<StringInit*, 8> ArgNames;
1042       for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
1043         Args.push_back(LHSs->getArg(i));
1044         ArgNames.push_back(LHSs->getArgName(i));
1045       }
1046       for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
1047         Args.push_back(RHSs->getArg(i));
1048         ArgNames.push_back(RHSs->getArgName(i));
1049       }
1050       return DagInit::get(Op, nullptr, Args, ArgNames);
1051     }
1052     break;
1053   }
1054   case LISTCONCAT: {
1055     ListInit *LHSs = dyn_cast<ListInit>(LHS);
1056     ListInit *RHSs = dyn_cast<ListInit>(RHS);
1057     if (LHSs && RHSs) {
1058       SmallVector<Init *, 8> Args;
1059       llvm::append_range(Args, *LHSs);
1060       llvm::append_range(Args, *RHSs);
1061       return ListInit::get(Args, LHSs->getElementType());
1062     }
1063     break;
1064   }
1065   case LISTSPLAT: {
1066     TypedInit *Value = dyn_cast<TypedInit>(LHS);
1067     IntInit *Size = dyn_cast<IntInit>(RHS);
1068     if (Value && Size) {
1069       SmallVector<Init *, 8> Args(Size->getValue(), Value);
1070       return ListInit::get(Args, Value->getType());
1071     }
1072     break;
1073   }
1074   case STRCONCAT: {
1075     StringInit *LHSs = dyn_cast<StringInit>(LHS);
1076     StringInit *RHSs = dyn_cast<StringInit>(RHS);
1077     if (LHSs && RHSs)
1078       return ConcatStringInits(LHSs, RHSs);
1079     break;
1080   }
1081   case INTERLEAVE: {
1082     ListInit *List = dyn_cast<ListInit>(LHS);
1083     StringInit *Delim = dyn_cast<StringInit>(RHS);
1084     if (List && Delim) {
1085       StringInit *Result;
1086       if (isa<StringRecTy>(List->getElementType()))
1087         Result = interleaveStringList(List, Delim);
1088       else
1089         Result = interleaveIntList(List, Delim);
1090       if (Result)
1091         return Result;
1092     }
1093     break;
1094   }
1095   case EQ:
1096   case NE:
1097   case LE:
1098   case LT:
1099   case GE:
1100   case GT: {
1101     // First see if we have two bit, bits, or int.
1102     IntInit *LHSi = dyn_cast_or_null<IntInit>(
1103         LHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1104     IntInit *RHSi = dyn_cast_or_null<IntInit>(
1105         RHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1106 
1107     if (LHSi && RHSi) {
1108       bool Result;
1109       switch (getOpcode()) {
1110       case EQ: Result = LHSi->getValue() == RHSi->getValue(); break;
1111       case NE: Result = LHSi->getValue() != RHSi->getValue(); break;
1112       case LE: Result = LHSi->getValue() <= RHSi->getValue(); break;
1113       case LT: Result = LHSi->getValue() <  RHSi->getValue(); break;
1114       case GE: Result = LHSi->getValue() >= RHSi->getValue(); break;
1115       case GT: Result = LHSi->getValue() >  RHSi->getValue(); break;
1116       default: llvm_unreachable("unhandled comparison");
1117       }
1118       return BitInit::get(getRecordKeeper(), Result);
1119     }
1120 
1121     // Next try strings.
1122     StringInit *LHSs = dyn_cast<StringInit>(LHS);
1123     StringInit *RHSs = dyn_cast<StringInit>(RHS);
1124 
1125     if (LHSs && RHSs) {
1126       bool Result;
1127       switch (getOpcode()) {
1128       case EQ: Result = LHSs->getValue() == RHSs->getValue(); break;
1129       case NE: Result = LHSs->getValue() != RHSs->getValue(); break;
1130       case LE: Result = LHSs->getValue() <= RHSs->getValue(); break;
1131       case LT: Result = LHSs->getValue() <  RHSs->getValue(); break;
1132       case GE: Result = LHSs->getValue() >= RHSs->getValue(); break;
1133       case GT: Result = LHSs->getValue() >  RHSs->getValue(); break;
1134       default: llvm_unreachable("unhandled comparison");
1135       }
1136       return BitInit::get(getRecordKeeper(), Result);
1137     }
1138 
1139     // Finally, !eq and !ne can be used with records.
1140     if (getOpcode() == EQ || getOpcode() == NE) {
1141       DefInit *LHSd = dyn_cast<DefInit>(LHS);
1142       DefInit *RHSd = dyn_cast<DefInit>(RHS);
1143       if (LHSd && RHSd)
1144         return BitInit::get(getRecordKeeper(),
1145                             (getOpcode() == EQ) ? LHSd == RHSd : LHSd != RHSd);
1146     }
1147 
1148     break;
1149   }
1150   case SETDAGOP: {
1151     DagInit *Dag = dyn_cast<DagInit>(LHS);
1152     DefInit *Op = dyn_cast<DefInit>(RHS);
1153     if (Dag && Op) {
1154       SmallVector<Init*, 8> Args;
1155       SmallVector<StringInit*, 8> ArgNames;
1156       for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
1157         Args.push_back(Dag->getArg(i));
1158         ArgNames.push_back(Dag->getArgName(i));
1159       }
1160       return DagInit::get(Op, nullptr, Args, ArgNames);
1161     }
1162     break;
1163   }
1164   case ADD:
1165   case SUB:
1166   case MUL:
1167   case AND:
1168   case OR:
1169   case XOR:
1170   case SHL:
1171   case SRA:
1172   case SRL: {
1173     IntInit *LHSi = dyn_cast_or_null<IntInit>(
1174         LHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1175     IntInit *RHSi = dyn_cast_or_null<IntInit>(
1176         RHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1177     if (LHSi && RHSi) {
1178       int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
1179       int64_t Result;
1180       switch (getOpcode()) {
1181       default: llvm_unreachable("Bad opcode!");
1182       case ADD: Result = LHSv + RHSv; break;
1183       case SUB: Result = LHSv - RHSv; break;
1184       case MUL: Result = LHSv * RHSv; break;
1185       case AND: Result = LHSv & RHSv; break;
1186       case OR:  Result = LHSv | RHSv; break;
1187       case XOR: Result = LHSv ^ RHSv; break;
1188       case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break;
1189       case SRA: Result = LHSv >> RHSv; break;
1190       case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
1191       }
1192       return IntInit::get(getRecordKeeper(), Result);
1193     }
1194     break;
1195   }
1196   }
1197   return const_cast<BinOpInit *>(this);
1198 }
1199 
1200 Init *BinOpInit::resolveReferences(Resolver &R) const {
1201   Init *lhs = LHS->resolveReferences(R);
1202   Init *rhs = RHS->resolveReferences(R);
1203 
1204   if (LHS != lhs || RHS != rhs)
1205     return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
1206         ->Fold(R.getCurrentRecord());
1207   return const_cast<BinOpInit *>(this);
1208 }
1209 
1210 std::string BinOpInit::getAsString() const {
1211   std::string Result;
1212   switch (getOpcode()) {
1213   case CONCAT: Result = "!con"; break;
1214   case ADD: Result = "!add"; break;
1215   case SUB: Result = "!sub"; break;
1216   case MUL: Result = "!mul"; break;
1217   case AND: Result = "!and"; break;
1218   case OR: Result = "!or"; break;
1219   case XOR: Result = "!xor"; break;
1220   case SHL: Result = "!shl"; break;
1221   case SRA: Result = "!sra"; break;
1222   case SRL: Result = "!srl"; break;
1223   case EQ: Result = "!eq"; break;
1224   case NE: Result = "!ne"; break;
1225   case LE: Result = "!le"; break;
1226   case LT: Result = "!lt"; break;
1227   case GE: Result = "!ge"; break;
1228   case GT: Result = "!gt"; break;
1229   case LISTCONCAT: Result = "!listconcat"; break;
1230   case LISTSPLAT: Result = "!listsplat"; break;
1231   case STRCONCAT: Result = "!strconcat"; break;
1232   case INTERLEAVE: Result = "!interleave"; break;
1233   case SETDAGOP: Result = "!setdagop"; break;
1234   }
1235   return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
1236 }
1237 
1238 static void
1239 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
1240                   Init *RHS, RecTy *Type) {
1241   ID.AddInteger(Opcode);
1242   ID.AddPointer(LHS);
1243   ID.AddPointer(MHS);
1244   ID.AddPointer(RHS);
1245   ID.AddPointer(Type);
1246 }
1247 
1248 TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
1249                             RecTy *Type) {
1250   FoldingSetNodeID ID;
1251   ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
1252 
1253   detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl();
1254   void *IP = nullptr;
1255   if (TernOpInit *I = RK.TheTernOpInitPool.FindNodeOrInsertPos(ID, IP))
1256     return I;
1257 
1258   TernOpInit *I = new (RK.Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
1259   RK.TheTernOpInitPool.InsertNode(I, IP);
1260   return I;
1261 }
1262 
1263 void TernOpInit::Profile(FoldingSetNodeID &ID) const {
1264   ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1265 }
1266 
1267 static Init *ItemApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
1268   MapResolver R(CurRec);
1269   R.set(LHS, MHSe);
1270   return RHS->resolveReferences(R);
1271 }
1272 
1273 static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
1274                              Record *CurRec) {
1275   bool Change = false;
1276   Init *Val = ItemApply(LHS, MHSd->getOperator(), RHS, CurRec);
1277   if (Val != MHSd->getOperator())
1278     Change = true;
1279 
1280   SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs;
1281   for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1282     Init *Arg = MHSd->getArg(i);
1283     Init *NewArg;
1284     StringInit *ArgName = MHSd->getArgName(i);
1285 
1286     if (DagInit *Argd = dyn_cast<DagInit>(Arg))
1287       NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
1288     else
1289       NewArg = ItemApply(LHS, Arg, RHS, CurRec);
1290 
1291     NewArgs.push_back(std::make_pair(NewArg, ArgName));
1292     if (Arg != NewArg)
1293       Change = true;
1294   }
1295 
1296   if (Change)
1297     return DagInit::get(Val, nullptr, NewArgs);
1298   return MHSd;
1299 }
1300 
1301 // Applies RHS to all elements of MHS, using LHS as a temp variable.
1302 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1303                            Record *CurRec) {
1304   if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
1305     return ForeachDagApply(LHS, MHSd, RHS, CurRec);
1306 
1307   if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
1308     SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1309 
1310     for (Init *&Item : NewList) {
1311       Init *NewItem = ItemApply(LHS, Item, RHS, CurRec);
1312       if (NewItem != Item)
1313         Item = NewItem;
1314     }
1315     return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1316   }
1317 
1318   return nullptr;
1319 }
1320 
1321 // Evaluates RHS for all elements of MHS, using LHS as a temp variable.
1322 // Creates a new list with the elements that evaluated to true.
1323 static Init *FilterHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1324                           Record *CurRec) {
1325   if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
1326     SmallVector<Init *, 8> NewList;
1327 
1328     for (Init *Item : MHSl->getValues()) {
1329       Init *Include = ItemApply(LHS, Item, RHS, CurRec);
1330       if (!Include)
1331         return nullptr;
1332       if (IntInit *IncludeInt =
1333               dyn_cast_or_null<IntInit>(Include->convertInitializerTo(
1334                   IntRecTy::get(LHS->getRecordKeeper())))) {
1335         if (IncludeInt->getValue())
1336           NewList.push_back(Item);
1337       } else {
1338         return nullptr;
1339       }
1340     }
1341     return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1342   }
1343 
1344   return nullptr;
1345 }
1346 
1347 Init *TernOpInit::Fold(Record *CurRec) const {
1348   RecordKeeper &RK = getRecordKeeper();
1349   switch (getOpcode()) {
1350   case SUBST: {
1351     DefInit *LHSd = dyn_cast<DefInit>(LHS);
1352     VarInit *LHSv = dyn_cast<VarInit>(LHS);
1353     StringInit *LHSs = dyn_cast<StringInit>(LHS);
1354 
1355     DefInit *MHSd = dyn_cast<DefInit>(MHS);
1356     VarInit *MHSv = dyn_cast<VarInit>(MHS);
1357     StringInit *MHSs = dyn_cast<StringInit>(MHS);
1358 
1359     DefInit *RHSd = dyn_cast<DefInit>(RHS);
1360     VarInit *RHSv = dyn_cast<VarInit>(RHS);
1361     StringInit *RHSs = dyn_cast<StringInit>(RHS);
1362 
1363     if (LHSd && MHSd && RHSd) {
1364       Record *Val = RHSd->getDef();
1365       if (LHSd->getAsString() == RHSd->getAsString())
1366         Val = MHSd->getDef();
1367       return DefInit::get(Val);
1368     }
1369     if (LHSv && MHSv && RHSv) {
1370       std::string Val = std::string(RHSv->getName());
1371       if (LHSv->getAsString() == RHSv->getAsString())
1372         Val = std::string(MHSv->getName());
1373       return VarInit::get(Val, getType());
1374     }
1375     if (LHSs && MHSs && RHSs) {
1376       std::string Val = std::string(RHSs->getValue());
1377 
1378       std::string::size_type found;
1379       std::string::size_type idx = 0;
1380       while (true) {
1381         found = Val.find(std::string(LHSs->getValue()), idx);
1382         if (found == std::string::npos)
1383           break;
1384         Val.replace(found, LHSs->getValue().size(),
1385                     std::string(MHSs->getValue()));
1386         idx = found + MHSs->getValue().size();
1387       }
1388 
1389       return StringInit::get(RK, Val);
1390     }
1391     break;
1392   }
1393 
1394   case FOREACH: {
1395     if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
1396       return Result;
1397     break;
1398   }
1399 
1400   case FILTER: {
1401     if (Init *Result = FilterHelper(LHS, MHS, RHS, getType(), CurRec))
1402       return Result;
1403     break;
1404   }
1405 
1406   case IF: {
1407     if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
1408             LHS->convertInitializerTo(IntRecTy::get(RK)))) {
1409       if (LHSi->getValue())
1410         return MHS;
1411       return RHS;
1412     }
1413     break;
1414   }
1415 
1416   case DAG: {
1417     ListInit *MHSl = dyn_cast<ListInit>(MHS);
1418     ListInit *RHSl = dyn_cast<ListInit>(RHS);
1419     bool MHSok = MHSl || isa<UnsetInit>(MHS);
1420     bool RHSok = RHSl || isa<UnsetInit>(RHS);
1421 
1422     if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
1423       break; // Typically prevented by the parser, but might happen with template args
1424 
1425     if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
1426       SmallVector<std::pair<Init *, StringInit *>, 8> Children;
1427       unsigned Size = MHSl ? MHSl->size() : RHSl->size();
1428       for (unsigned i = 0; i != Size; ++i) {
1429         Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get(RK);
1430         Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get(RK);
1431         if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
1432           return const_cast<TernOpInit *>(this);
1433         Children.emplace_back(Node, dyn_cast<StringInit>(Name));
1434       }
1435       return DagInit::get(LHS, nullptr, Children);
1436     }
1437     break;
1438   }
1439 
1440   case SUBSTR: {
1441     StringInit *LHSs = dyn_cast<StringInit>(LHS);
1442     IntInit *MHSi = dyn_cast<IntInit>(MHS);
1443     IntInit *RHSi = dyn_cast<IntInit>(RHS);
1444     if (LHSs && MHSi && RHSi) {
1445       int64_t StringSize = LHSs->getValue().size();
1446       int64_t Start = MHSi->getValue();
1447       int64_t Length = RHSi->getValue();
1448       if (Start < 0 || Start > StringSize)
1449         PrintError(CurRec->getLoc(),
1450                    Twine("!substr start position is out of range 0...") +
1451                        std::to_string(StringSize) + ": " +
1452                        std::to_string(Start));
1453       if (Length < 0)
1454         PrintError(CurRec->getLoc(), "!substr length must be nonnegative");
1455       return StringInit::get(RK, LHSs->getValue().substr(Start, Length),
1456                              LHSs->getFormat());
1457     }
1458     break;
1459   }
1460 
1461   case FIND: {
1462     StringInit *LHSs = dyn_cast<StringInit>(LHS);
1463     StringInit *MHSs = dyn_cast<StringInit>(MHS);
1464     IntInit *RHSi = dyn_cast<IntInit>(RHS);
1465     if (LHSs && MHSs && RHSi) {
1466       int64_t SourceSize = LHSs->getValue().size();
1467       int64_t Start = RHSi->getValue();
1468       if (Start < 0 || Start > SourceSize)
1469         PrintError(CurRec->getLoc(),
1470                    Twine("!find start position is out of range 0...") +
1471                        std::to_string(SourceSize) + ": " +
1472                        std::to_string(Start));
1473       auto I = LHSs->getValue().find(MHSs->getValue(), Start);
1474       if (I == std::string::npos)
1475         return IntInit::get(RK, -1);
1476       return IntInit::get(RK, I);
1477     }
1478     break;
1479   }
1480   }
1481 
1482   return const_cast<TernOpInit *>(this);
1483 }
1484 
1485 Init *TernOpInit::resolveReferences(Resolver &R) const {
1486   Init *lhs = LHS->resolveReferences(R);
1487 
1488   if (getOpcode() == IF && lhs != LHS) {
1489     if (IntInit *Value = dyn_cast_or_null<IntInit>(
1490             lhs->convertInitializerTo(IntRecTy::get(getRecordKeeper())))) {
1491       // Short-circuit
1492       if (Value->getValue())
1493         return MHS->resolveReferences(R);
1494       return RHS->resolveReferences(R);
1495     }
1496   }
1497 
1498   Init *mhs = MHS->resolveReferences(R);
1499   Init *rhs;
1500 
1501   if (getOpcode() == FOREACH || getOpcode() == FILTER) {
1502     ShadowResolver SR(R);
1503     SR.addShadow(lhs);
1504     rhs = RHS->resolveReferences(SR);
1505   } else {
1506     rhs = RHS->resolveReferences(R);
1507   }
1508 
1509   if (LHS != lhs || MHS != mhs || RHS != rhs)
1510     return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
1511         ->Fold(R.getCurrentRecord());
1512   return const_cast<TernOpInit *>(this);
1513 }
1514 
1515 std::string TernOpInit::getAsString() const {
1516   std::string Result;
1517   bool UnquotedLHS = false;
1518   switch (getOpcode()) {
1519   case DAG: Result = "!dag"; break;
1520   case FILTER: Result = "!filter"; UnquotedLHS = true; break;
1521   case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
1522   case IF: Result = "!if"; break;
1523   case SUBST: Result = "!subst"; break;
1524   case SUBSTR: Result = "!substr"; break;
1525   case FIND: Result = "!find"; break;
1526   }
1527   return (Result + "(" +
1528           (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
1529           ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
1530 }
1531 
1532 static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *Start, Init *List,
1533                               Init *A, Init *B, Init *Expr, RecTy *Type) {
1534   ID.AddPointer(Start);
1535   ID.AddPointer(List);
1536   ID.AddPointer(A);
1537   ID.AddPointer(B);
1538   ID.AddPointer(Expr);
1539   ID.AddPointer(Type);
1540 }
1541 
1542 FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
1543                             Init *Expr, RecTy *Type) {
1544   FoldingSetNodeID ID;
1545   ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
1546 
1547   detail::RecordKeeperImpl &RK = Start->getRecordKeeper().getImpl();
1548   void *IP = nullptr;
1549   if (FoldOpInit *I = RK.TheFoldOpInitPool.FindNodeOrInsertPos(ID, IP))
1550     return I;
1551 
1552   FoldOpInit *I = new (RK.Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
1553   RK.TheFoldOpInitPool.InsertNode(I, IP);
1554   return I;
1555 }
1556 
1557 void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
1558   ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
1559 }
1560 
1561 Init *FoldOpInit::Fold(Record *CurRec) const {
1562   if (ListInit *LI = dyn_cast<ListInit>(List)) {
1563     Init *Accum = Start;
1564     for (Init *Elt : *LI) {
1565       MapResolver R(CurRec);
1566       R.set(A, Accum);
1567       R.set(B, Elt);
1568       Accum = Expr->resolveReferences(R);
1569     }
1570     return Accum;
1571   }
1572   return const_cast<FoldOpInit *>(this);
1573 }
1574 
1575 Init *FoldOpInit::resolveReferences(Resolver &R) const {
1576   Init *NewStart = Start->resolveReferences(R);
1577   Init *NewList = List->resolveReferences(R);
1578   ShadowResolver SR(R);
1579   SR.addShadow(A);
1580   SR.addShadow(B);
1581   Init *NewExpr = Expr->resolveReferences(SR);
1582 
1583   if (Start == NewStart && List == NewList && Expr == NewExpr)
1584     return const_cast<FoldOpInit *>(this);
1585 
1586   return get(NewStart, NewList, A, B, NewExpr, getType())
1587       ->Fold(R.getCurrentRecord());
1588 }
1589 
1590 Init *FoldOpInit::getBit(unsigned Bit) const {
1591   return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
1592 }
1593 
1594 std::string FoldOpInit::getAsString() const {
1595   return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
1596           ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
1597           ", " + Expr->getAsString() + ")")
1598       .str();
1599 }
1600 
1601 static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType,
1602                              Init *Expr) {
1603   ID.AddPointer(CheckType);
1604   ID.AddPointer(Expr);
1605 }
1606 
1607 IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
1608 
1609   FoldingSetNodeID ID;
1610   ProfileIsAOpInit(ID, CheckType, Expr);
1611 
1612   detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl();
1613   void *IP = nullptr;
1614   if (IsAOpInit *I = RK.TheIsAOpInitPool.FindNodeOrInsertPos(ID, IP))
1615     return I;
1616 
1617   IsAOpInit *I = new (RK.Allocator) IsAOpInit(CheckType, Expr);
1618   RK.TheIsAOpInitPool.InsertNode(I, IP);
1619   return I;
1620 }
1621 
1622 void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
1623   ProfileIsAOpInit(ID, CheckType, Expr);
1624 }
1625 
1626 Init *IsAOpInit::Fold() const {
1627   if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
1628     // Is the expression type known to be (a subclass of) the desired type?
1629     if (TI->getType()->typeIsConvertibleTo(CheckType))
1630       return IntInit::get(getRecordKeeper(), 1);
1631 
1632     if (isa<RecordRecTy>(CheckType)) {
1633       // If the target type is not a subclass of the expression type, or if
1634       // the expression has fully resolved to a record, we know that it can't
1635       // be of the required type.
1636       if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
1637         return IntInit::get(getRecordKeeper(), 0);
1638     } else {
1639       // We treat non-record types as not castable.
1640       return IntInit::get(getRecordKeeper(), 0);
1641     }
1642   }
1643   return const_cast<IsAOpInit *>(this);
1644 }
1645 
1646 Init *IsAOpInit::resolveReferences(Resolver &R) const {
1647   Init *NewExpr = Expr->resolveReferences(R);
1648   if (Expr != NewExpr)
1649     return get(CheckType, NewExpr)->Fold();
1650   return const_cast<IsAOpInit *>(this);
1651 }
1652 
1653 Init *IsAOpInit::getBit(unsigned Bit) const {
1654   return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
1655 }
1656 
1657 std::string IsAOpInit::getAsString() const {
1658   return (Twine("!isa<") + CheckType->getAsString() + ">(" +
1659           Expr->getAsString() + ")")
1660       .str();
1661 }
1662 
1663 RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1664   if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
1665     for (Record *Rec : RecordType->getClasses()) {
1666       if (RecordVal *Field = Rec->getValue(FieldName))
1667         return Field->getType();
1668     }
1669   }
1670   return nullptr;
1671 }
1672 
1673 Init *
1674 TypedInit::convertInitializerTo(RecTy *Ty) const {
1675   if (getType() == Ty || getType()->typeIsA(Ty))
1676     return const_cast<TypedInit *>(this);
1677 
1678   if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
1679       cast<BitsRecTy>(Ty)->getNumBits() == 1)
1680     return BitsInit::get(getRecordKeeper(), {const_cast<TypedInit *>(this)});
1681 
1682   return nullptr;
1683 }
1684 
1685 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1686   BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1687   if (!T) return nullptr;  // Cannot subscript a non-bits variable.
1688   unsigned NumBits = T->getNumBits();
1689 
1690   SmallVector<Init *, 16> NewBits;
1691   NewBits.reserve(Bits.size());
1692   for (unsigned Bit : Bits) {
1693     if (Bit >= NumBits)
1694       return nullptr;
1695 
1696     NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1697   }
1698   return BitsInit::get(getRecordKeeper(), NewBits);
1699 }
1700 
1701 Init *TypedInit::getCastTo(RecTy *Ty) const {
1702   // Handle the common case quickly
1703   if (getType() == Ty || getType()->typeIsA(Ty))
1704     return const_cast<TypedInit *>(this);
1705 
1706   if (Init *Converted = convertInitializerTo(Ty)) {
1707     assert(!isa<TypedInit>(Converted) ||
1708            cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
1709     return Converted;
1710   }
1711 
1712   if (!getType()->typeIsConvertibleTo(Ty))
1713     return nullptr;
1714 
1715   return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
1716       ->Fold(nullptr);
1717 }
1718 
1719 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1720   ListRecTy *T = dyn_cast<ListRecTy>(getType());
1721   if (!T) return nullptr;  // Cannot subscript a non-list variable.
1722 
1723   if (Elements.size() == 1)
1724     return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1725 
1726   SmallVector<Init*, 8> ListInits;
1727   ListInits.reserve(Elements.size());
1728   for (unsigned Element : Elements)
1729     ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1730                                                 Element));
1731   return ListInit::get(ListInits, T->getElementType());
1732 }
1733 
1734 
1735 VarInit *VarInit::get(StringRef VN, RecTy *T) {
1736   Init *Value = StringInit::get(T->getRecordKeeper(), VN);
1737   return VarInit::get(Value, T);
1738 }
1739 
1740 VarInit *VarInit::get(Init *VN, RecTy *T) {
1741   detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl();
1742   VarInit *&I = RK.TheVarInitPool[std::make_pair(T, VN)];
1743   if (!I)
1744     I = new (RK.Allocator) VarInit(VN, T);
1745   return I;
1746 }
1747 
1748 StringRef VarInit::getName() const {
1749   StringInit *NameString = cast<StringInit>(getNameInit());
1750   return NameString->getValue();
1751 }
1752 
1753 Init *VarInit::getBit(unsigned Bit) const {
1754   if (getType() == BitRecTy::get(getRecordKeeper()))
1755     return const_cast<VarInit*>(this);
1756   return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1757 }
1758 
1759 Init *VarInit::resolveReferences(Resolver &R) const {
1760   if (Init *Val = R.resolve(VarName))
1761     return Val;
1762   return const_cast<VarInit *>(this);
1763 }
1764 
1765 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1766   detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl();
1767   VarBitInit *&I = RK.TheVarBitInitPool[std::make_pair(T, B)];
1768   if (!I)
1769     I = new (RK.Allocator) VarBitInit(T, B);
1770   return I;
1771 }
1772 
1773 std::string VarBitInit::getAsString() const {
1774   return TI->getAsString() + "{" + utostr(Bit) + "}";
1775 }
1776 
1777 Init *VarBitInit::resolveReferences(Resolver &R) const {
1778   Init *I = TI->resolveReferences(R);
1779   if (TI != I)
1780     return I->getBit(getBitNum());
1781 
1782   return const_cast<VarBitInit*>(this);
1783 }
1784 
1785 VarListElementInit *VarListElementInit::get(TypedInit *T, unsigned E) {
1786   detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl();
1787   VarListElementInit *&I = RK.TheVarListElementInitPool[std::make_pair(T, E)];
1788   if (!I)
1789     I = new (RK.Allocator) VarListElementInit(T, E);
1790   return I;
1791 }
1792 
1793 std::string VarListElementInit::getAsString() const {
1794   return TI->getAsString() + "[" + utostr(Element) + "]";
1795 }
1796 
1797 Init *VarListElementInit::resolveReferences(Resolver &R) const {
1798   Init *NewTI = TI->resolveReferences(R);
1799   if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1800     // Leave out-of-bounds array references as-is. This can happen without
1801     // being an error, e.g. in the untaken "branch" of an !if expression.
1802     if (getElementNum() < List->size())
1803       return List->getElement(getElementNum());
1804   }
1805   if (NewTI != TI && isa<TypedInit>(NewTI))
1806     return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1807   return const_cast<VarListElementInit *>(this);
1808 }
1809 
1810 Init *VarListElementInit::getBit(unsigned Bit) const {
1811   if (getType() == BitRecTy::get(getRecordKeeper()))
1812     return const_cast<VarListElementInit*>(this);
1813   return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1814 }
1815 
1816 DefInit::DefInit(Record *D)
1817     : TypedInit(IK_DefInit, D->getType()), Def(D) {}
1818 
1819 DefInit *DefInit::get(Record *R) {
1820   return R->getDefInit();
1821 }
1822 
1823 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1824   if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1825     if (getType()->typeIsConvertibleTo(RRT))
1826       return const_cast<DefInit *>(this);
1827   return nullptr;
1828 }
1829 
1830 RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1831   if (const RecordVal *RV = Def->getValue(FieldName))
1832     return RV->getType();
1833   return nullptr;
1834 }
1835 
1836 std::string DefInit::getAsString() const { return std::string(Def->getName()); }
1837 
1838 static void ProfileVarDefInit(FoldingSetNodeID &ID,
1839                               Record *Class,
1840                               ArrayRef<Init *> Args) {
1841   ID.AddInteger(Args.size());
1842   ID.AddPointer(Class);
1843 
1844   for (Init *I : Args)
1845     ID.AddPointer(I);
1846 }
1847 
1848 VarDefInit::VarDefInit(Record *Class, unsigned N)
1849     : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class),
1850       NumArgs(N) {}
1851 
1852 VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
1853   FoldingSetNodeID ID;
1854   ProfileVarDefInit(ID, Class, Args);
1855 
1856   detail::RecordKeeperImpl &RK = Class->getRecords().getImpl();
1857   void *IP = nullptr;
1858   if (VarDefInit *I = RK.TheVarDefInitPool.FindNodeOrInsertPos(ID, IP))
1859     return I;
1860 
1861   void *Mem = RK.Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
1862                                     alignof(VarDefInit));
1863   VarDefInit *I = new (Mem) VarDefInit(Class, Args.size());
1864   std::uninitialized_copy(Args.begin(), Args.end(),
1865                           I->getTrailingObjects<Init *>());
1866   RK.TheVarDefInitPool.InsertNode(I, IP);
1867   return I;
1868 }
1869 
1870 void VarDefInit::Profile(FoldingSetNodeID &ID) const {
1871   ProfileVarDefInit(ID, Class, args());
1872 }
1873 
1874 DefInit *VarDefInit::instantiate() {
1875   if (!Def) {
1876     RecordKeeper &Records = Class->getRecords();
1877     auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
1878                                            Class->getLoc(), Records,
1879                                            /*IsAnonymous=*/true);
1880     Record *NewRec = NewRecOwner.get();
1881 
1882     // Copy values from class to instance
1883     for (const RecordVal &Val : Class->getValues())
1884       NewRec->addValue(Val);
1885 
1886     // Copy assertions from class to instance.
1887     NewRec->appendAssertions(Class);
1888 
1889     // Substitute and resolve template arguments
1890     ArrayRef<Init *> TArgs = Class->getTemplateArgs();
1891     MapResolver R(NewRec);
1892 
1893     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1894       if (i < args_size())
1895         R.set(TArgs[i], getArg(i));
1896       else
1897         R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
1898 
1899       NewRec->removeValue(TArgs[i]);
1900     }
1901 
1902     NewRec->resolveReferences(R);
1903 
1904     // Add superclasses.
1905     ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
1906     for (const auto &SCPair : SCs)
1907       NewRec->addSuperClass(SCPair.first, SCPair.second);
1908 
1909     NewRec->addSuperClass(Class,
1910                           SMRange(Class->getLoc().back(),
1911                                   Class->getLoc().back()));
1912 
1913     // Resolve internal references and store in record keeper
1914     NewRec->resolveReferences();
1915     Records.addDef(std::move(NewRecOwner));
1916 
1917     // Check the assertions.
1918     NewRec->checkRecordAssertions();
1919 
1920     Def = DefInit::get(NewRec);
1921   }
1922 
1923   return Def;
1924 }
1925 
1926 Init *VarDefInit::resolveReferences(Resolver &R) const {
1927   TrackUnresolvedResolver UR(&R);
1928   bool Changed = false;
1929   SmallVector<Init *, 8> NewArgs;
1930   NewArgs.reserve(args_size());
1931 
1932   for (Init *Arg : args()) {
1933     Init *NewArg = Arg->resolveReferences(UR);
1934     NewArgs.push_back(NewArg);
1935     Changed |= NewArg != Arg;
1936   }
1937 
1938   if (Changed) {
1939     auto New = VarDefInit::get(Class, NewArgs);
1940     if (!UR.foundUnresolved())
1941       return New->instantiate();
1942     return New;
1943   }
1944   return const_cast<VarDefInit *>(this);
1945 }
1946 
1947 Init *VarDefInit::Fold() const {
1948   if (Def)
1949     return Def;
1950 
1951   TrackUnresolvedResolver R;
1952   for (Init *Arg : args())
1953     Arg->resolveReferences(R);
1954 
1955   if (!R.foundUnresolved())
1956     return const_cast<VarDefInit *>(this)->instantiate();
1957   return const_cast<VarDefInit *>(this);
1958 }
1959 
1960 std::string VarDefInit::getAsString() const {
1961   std::string Result = Class->getNameInitAsString() + "<";
1962   const char *sep = "";
1963   for (Init *Arg : args()) {
1964     Result += sep;
1965     sep = ", ";
1966     Result += Arg->getAsString();
1967   }
1968   return Result + ">";
1969 }
1970 
1971 FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1972   detail::RecordKeeperImpl &RK = R->getRecordKeeper().getImpl();
1973   FieldInit *&I = RK.TheFieldInitPool[std::make_pair(R, FN)];
1974   if (!I)
1975     I = new (RK.Allocator) FieldInit(R, FN);
1976   return I;
1977 }
1978 
1979 Init *FieldInit::getBit(unsigned Bit) const {
1980   if (getType() == BitRecTy::get(getRecordKeeper()))
1981     return const_cast<FieldInit*>(this);
1982   return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1983 }
1984 
1985 Init *FieldInit::resolveReferences(Resolver &R) const {
1986   Init *NewRec = Rec->resolveReferences(R);
1987   if (NewRec != Rec)
1988     return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
1989   return const_cast<FieldInit *>(this);
1990 }
1991 
1992 Init *FieldInit::Fold(Record *CurRec) const {
1993   if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1994     Record *Def = DI->getDef();
1995     if (Def == CurRec)
1996       PrintFatalError(CurRec->getLoc(),
1997                       Twine("Attempting to access field '") +
1998                       FieldName->getAsUnquotedString() + "' of '" +
1999                       Rec->getAsString() + "' is a forbidden self-reference");
2000     Init *FieldVal = Def->getValue(FieldName)->getValue();
2001     if (FieldVal->isConcrete())
2002       return FieldVal;
2003   }
2004   return const_cast<FieldInit *>(this);
2005 }
2006 
2007 bool FieldInit::isConcrete() const {
2008   if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
2009     Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue();
2010     return FieldVal->isConcrete();
2011   }
2012   return false;
2013 }
2014 
2015 static void ProfileCondOpInit(FoldingSetNodeID &ID,
2016                              ArrayRef<Init *> CondRange,
2017                              ArrayRef<Init *> ValRange,
2018                              const RecTy *ValType) {
2019   assert(CondRange.size() == ValRange.size() &&
2020          "Number of conditions and values must match!");
2021   ID.AddPointer(ValType);
2022   ArrayRef<Init *>::iterator Case = CondRange.begin();
2023   ArrayRef<Init *>::iterator Val = ValRange.begin();
2024 
2025   while (Case != CondRange.end()) {
2026     ID.AddPointer(*Case++);
2027     ID.AddPointer(*Val++);
2028   }
2029 }
2030 
2031 void CondOpInit::Profile(FoldingSetNodeID &ID) const {
2032   ProfileCondOpInit(ID,
2033       makeArrayRef(getTrailingObjects<Init *>(), NumConds),
2034       makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds),
2035       ValType);
2036 }
2037 
2038 CondOpInit *CondOpInit::get(ArrayRef<Init *> CondRange,
2039                             ArrayRef<Init *> ValRange, RecTy *Ty) {
2040   assert(CondRange.size() == ValRange.size() &&
2041          "Number of conditions and values must match!");
2042 
2043   FoldingSetNodeID ID;
2044   ProfileCondOpInit(ID, CondRange, ValRange, Ty);
2045 
2046   detail::RecordKeeperImpl &RK = Ty->getRecordKeeper().getImpl();
2047   void *IP = nullptr;
2048   if (CondOpInit *I = RK.TheCondOpInitPool.FindNodeOrInsertPos(ID, IP))
2049     return I;
2050 
2051   void *Mem = RK.Allocator.Allocate(
2052       totalSizeToAlloc<Init *>(2 * CondRange.size()), alignof(BitsInit));
2053   CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty);
2054 
2055   std::uninitialized_copy(CondRange.begin(), CondRange.end(),
2056                           I->getTrailingObjects<Init *>());
2057   std::uninitialized_copy(ValRange.begin(), ValRange.end(),
2058                           I->getTrailingObjects<Init *>()+CondRange.size());
2059   RK.TheCondOpInitPool.InsertNode(I, IP);
2060   return I;
2061 }
2062 
2063 Init *CondOpInit::resolveReferences(Resolver &R) const {
2064   SmallVector<Init*, 4> NewConds;
2065   bool Changed = false;
2066   for (const Init *Case : getConds()) {
2067     Init *NewCase = Case->resolveReferences(R);
2068     NewConds.push_back(NewCase);
2069     Changed |= NewCase != Case;
2070   }
2071 
2072   SmallVector<Init*, 4> NewVals;
2073   for (const Init *Val : getVals()) {
2074     Init *NewVal = Val->resolveReferences(R);
2075     NewVals.push_back(NewVal);
2076     Changed |= NewVal != Val;
2077   }
2078 
2079   if (Changed)
2080     return (CondOpInit::get(NewConds, NewVals,
2081             getValType()))->Fold(R.getCurrentRecord());
2082 
2083   return const_cast<CondOpInit *>(this);
2084 }
2085 
2086 Init *CondOpInit::Fold(Record *CurRec) const {
2087   RecordKeeper &RK = getRecordKeeper();
2088   for ( unsigned i = 0; i < NumConds; ++i) {
2089     Init *Cond = getCond(i);
2090     Init *Val = getVal(i);
2091 
2092     if (IntInit *CondI = dyn_cast_or_null<IntInit>(
2093             Cond->convertInitializerTo(IntRecTy::get(RK)))) {
2094       if (CondI->getValue())
2095         return Val->convertInitializerTo(getValType());
2096     } else {
2097       return const_cast<CondOpInit *>(this);
2098     }
2099   }
2100 
2101   PrintFatalError(CurRec->getLoc(),
2102                   CurRec->getName() +
2103                   " does not have any true condition in:" +
2104                   this->getAsString());
2105   return nullptr;
2106 }
2107 
2108 bool CondOpInit::isConcrete() const {
2109   for (const Init *Case : getConds())
2110     if (!Case->isConcrete())
2111       return false;
2112 
2113   for (const Init *Val : getVals())
2114     if (!Val->isConcrete())
2115       return false;
2116 
2117   return true;
2118 }
2119 
2120 bool CondOpInit::isComplete() const {
2121   for (const Init *Case : getConds())
2122     if (!Case->isComplete())
2123       return false;
2124 
2125   for (const Init *Val : getVals())
2126     if (!Val->isConcrete())
2127       return false;
2128 
2129   return true;
2130 }
2131 
2132 std::string CondOpInit::getAsString() const {
2133   std::string Result = "!cond(";
2134   for (unsigned i = 0; i < getNumConds(); i++) {
2135     Result += getCond(i)->getAsString() + ": ";
2136     Result += getVal(i)->getAsString();
2137     if (i != getNumConds()-1)
2138       Result += ", ";
2139   }
2140   return Result + ")";
2141 }
2142 
2143 Init *CondOpInit::getBit(unsigned Bit) const {
2144   return VarBitInit::get(const_cast<CondOpInit *>(this), Bit);
2145 }
2146 
2147 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
2148                            ArrayRef<Init *> ArgRange,
2149                            ArrayRef<StringInit *> NameRange) {
2150   ID.AddPointer(V);
2151   ID.AddPointer(VN);
2152 
2153   ArrayRef<Init *>::iterator Arg = ArgRange.begin();
2154   ArrayRef<StringInit *>::iterator Name = NameRange.begin();
2155   while (Arg != ArgRange.end()) {
2156     assert(Name != NameRange.end() && "Arg name underflow!");
2157     ID.AddPointer(*Arg++);
2158     ID.AddPointer(*Name++);
2159   }
2160   assert(Name == NameRange.end() && "Arg name overflow!");
2161 }
2162 
2163 DagInit *DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
2164                       ArrayRef<StringInit *> NameRange) {
2165   FoldingSetNodeID ID;
2166   ProfileDagInit(ID, V, VN, ArgRange, NameRange);
2167 
2168   detail::RecordKeeperImpl &RK = V->getRecordKeeper().getImpl();
2169   void *IP = nullptr;
2170   if (DagInit *I = RK.TheDagInitPool.FindNodeOrInsertPos(ID, IP))
2171     return I;
2172 
2173   void *Mem = RK.Allocator.Allocate(
2174       totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()),
2175       alignof(BitsInit));
2176   DagInit *I = new (Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
2177   std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
2178                           I->getTrailingObjects<Init *>());
2179   std::uninitialized_copy(NameRange.begin(), NameRange.end(),
2180                           I->getTrailingObjects<StringInit *>());
2181   RK.TheDagInitPool.InsertNode(I, IP);
2182   return I;
2183 }
2184 
2185 DagInit *
2186 DagInit::get(Init *V, StringInit *VN,
2187              ArrayRef<std::pair<Init*, StringInit*>> args) {
2188   SmallVector<Init *, 8> Args;
2189   SmallVector<StringInit *, 8> Names;
2190 
2191   for (const auto &Arg : args) {
2192     Args.push_back(Arg.first);
2193     Names.push_back(Arg.second);
2194   }
2195 
2196   return DagInit::get(V, VN, Args, Names);
2197 }
2198 
2199 void DagInit::Profile(FoldingSetNodeID &ID) const {
2200   ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
2201 }
2202 
2203 Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
2204   if (DefInit *DefI = dyn_cast<DefInit>(Val))
2205     return DefI->getDef();
2206   PrintFatalError(Loc, "Expected record as operator");
2207   return nullptr;
2208 }
2209 
2210 Init *DagInit::resolveReferences(Resolver &R) const {
2211   SmallVector<Init*, 8> NewArgs;
2212   NewArgs.reserve(arg_size());
2213   bool ArgsChanged = false;
2214   for (const Init *Arg : getArgs()) {
2215     Init *NewArg = Arg->resolveReferences(R);
2216     NewArgs.push_back(NewArg);
2217     ArgsChanged |= NewArg != Arg;
2218   }
2219 
2220   Init *Op = Val->resolveReferences(R);
2221   if (Op != Val || ArgsChanged)
2222     return DagInit::get(Op, ValName, NewArgs, getArgNames());
2223 
2224   return const_cast<DagInit *>(this);
2225 }
2226 
2227 bool DagInit::isConcrete() const {
2228   if (!Val->isConcrete())
2229     return false;
2230   for (const Init *Elt : getArgs()) {
2231     if (!Elt->isConcrete())
2232       return false;
2233   }
2234   return true;
2235 }
2236 
2237 std::string DagInit::getAsString() const {
2238   std::string Result = "(" + Val->getAsString();
2239   if (ValName)
2240     Result += ":" + ValName->getAsUnquotedString();
2241   if (!arg_empty()) {
2242     Result += " " + getArg(0)->getAsString();
2243     if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
2244     for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
2245       Result += ", " + getArg(i)->getAsString();
2246       if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
2247     }
2248   }
2249   return Result + ")";
2250 }
2251 
2252 //===----------------------------------------------------------------------===//
2253 //    Other implementations
2254 //===----------------------------------------------------------------------===//
2255 
2256 RecordVal::RecordVal(Init *N, RecTy *T, FieldKind K)
2257     : Name(N), TyAndKind(T, K) {
2258   setValue(UnsetInit::get(N->getRecordKeeper()));
2259   assert(Value && "Cannot create unset value for current type!");
2260 }
2261 
2262 // This constructor accepts the same arguments as the above, but also
2263 // a source location.
2264 RecordVal::RecordVal(Init *N, SMLoc Loc, RecTy *T, FieldKind K)
2265     : Name(N), Loc(Loc), TyAndKind(T, K) {
2266   setValue(UnsetInit::get(N->getRecordKeeper()));
2267   assert(Value && "Cannot create unset value for current type!");
2268 }
2269 
2270 StringRef RecordVal::getName() const {
2271   return cast<StringInit>(getNameInit())->getValue();
2272 }
2273 
2274 std::string RecordVal::getPrintType() const {
2275   if (getType() == StringRecTy::get(getRecordKeeper())) {
2276     if (auto *StrInit = dyn_cast<StringInit>(Value)) {
2277       if (StrInit->hasCodeFormat())
2278         return "code";
2279       else
2280         return "string";
2281     } else {
2282       return "string";
2283     }
2284   } else {
2285     return TyAndKind.getPointer()->getAsString();
2286   }
2287 }
2288 
2289 bool RecordVal::setValue(Init *V) {
2290   if (V) {
2291     Value = V->getCastTo(getType());
2292     if (Value) {
2293       assert(!isa<TypedInit>(Value) ||
2294              cast<TypedInit>(Value)->getType()->typeIsA(getType()));
2295       if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
2296         if (!isa<BitsInit>(Value)) {
2297           SmallVector<Init *, 64> Bits;
2298           Bits.reserve(BTy->getNumBits());
2299           for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
2300             Bits.push_back(Value->getBit(I));
2301           Value = BitsInit::get(V->getRecordKeeper(), Bits);
2302         }
2303       }
2304     }
2305     return Value == nullptr;
2306   }
2307   Value = nullptr;
2308   return false;
2309 }
2310 
2311 // This version of setValue takes a source location and resets the
2312 // location in the RecordVal.
2313 bool RecordVal::setValue(Init *V, SMLoc NewLoc) {
2314   Loc = NewLoc;
2315   if (V) {
2316     Value = V->getCastTo(getType());
2317     if (Value) {
2318       assert(!isa<TypedInit>(Value) ||
2319              cast<TypedInit>(Value)->getType()->typeIsA(getType()));
2320       if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
2321         if (!isa<BitsInit>(Value)) {
2322           SmallVector<Init *, 64> Bits;
2323           Bits.reserve(BTy->getNumBits());
2324           for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
2325             Bits.push_back(Value->getBit(I));
2326           Value = BitsInit::get(getRecordKeeper(), Bits);
2327         }
2328       }
2329     }
2330     return Value == nullptr;
2331   }
2332   Value = nullptr;
2333   return false;
2334 }
2335 
2336 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2337 #include "llvm/TableGen/Record.h"
2338 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
2339 #endif
2340 
2341 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
2342   if (isNonconcreteOK()) OS << "field ";
2343   OS << getPrintType() << " " << getNameInitAsString();
2344 
2345   if (getValue())
2346     OS << " = " << *getValue();
2347 
2348   if (PrintSem) OS << ";\n";
2349 }
2350 
2351 void Record::checkName() {
2352   // Ensure the record name has string type.
2353   const TypedInit *TypedName = cast<const TypedInit>(Name);
2354   if (!isa<StringRecTy>(TypedName->getType()))
2355     PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
2356                                   "' is not a string!");
2357 }
2358 
2359 RecordRecTy *Record::getType() {
2360   SmallVector<Record *, 4> DirectSCs;
2361   getDirectSuperClasses(DirectSCs);
2362   return RecordRecTy::get(TrackedRecords, DirectSCs);
2363 }
2364 
2365 DefInit *Record::getDefInit() {
2366   if (!CorrespondingDefInit) {
2367     CorrespondingDefInit =
2368         new (TrackedRecords.getImpl().Allocator) DefInit(this);
2369   }
2370   return CorrespondingDefInit;
2371 }
2372 
2373 unsigned Record::getNewUID(RecordKeeper &RK) {
2374   return RK.getImpl().LastRecordID++;
2375 }
2376 
2377 void Record::setName(Init *NewName) {
2378   Name = NewName;
2379   checkName();
2380   // DO NOT resolve record values to the name at this point because
2381   // there might be default values for arguments of this def.  Those
2382   // arguments might not have been resolved yet so we don't want to
2383   // prematurely assume values for those arguments were not passed to
2384   // this def.
2385   //
2386   // Nonetheless, it may be that some of this Record's values
2387   // reference the record name.  Indeed, the reason for having the
2388   // record name be an Init is to provide this flexibility.  The extra
2389   // resolve steps after completely instantiating defs takes care of
2390   // this.  See TGParser::ParseDef and TGParser::ParseDefm.
2391 }
2392 
2393 // NOTE for the next two functions:
2394 // Superclasses are in post-order, so the final one is a direct
2395 // superclass. All of its transitive superclases immediately precede it,
2396 // so we can step through the direct superclasses in reverse order.
2397 
2398 bool Record::hasDirectSuperClass(const Record *Superclass) const {
2399   ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
2400 
2401   for (int I = SCs.size() - 1; I >= 0; --I) {
2402     const Record *SC = SCs[I].first;
2403     if (SC == Superclass)
2404       return true;
2405     I -= SC->getSuperClasses().size();
2406   }
2407 
2408   return false;
2409 }
2410 
2411 void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
2412   ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
2413 
2414   while (!SCs.empty()) {
2415     Record *SC = SCs.back().first;
2416     SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
2417     Classes.push_back(SC);
2418   }
2419 }
2420 
2421 void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
2422   Init *OldName = getNameInit();
2423   Init *NewName = Name->resolveReferences(R);
2424   if (NewName != OldName) {
2425     // Re-register with RecordKeeper.
2426     setName(NewName);
2427   }
2428 
2429   // Resolve the field values.
2430   for (RecordVal &Value : Values) {
2431     if (SkipVal == &Value) // Skip resolve the same field as the given one
2432       continue;
2433     if (Init *V = Value.getValue()) {
2434       Init *VR = V->resolveReferences(R);
2435       if (Value.setValue(VR)) {
2436         std::string Type;
2437         if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
2438           Type =
2439               (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
2440         PrintFatalError(
2441             getLoc(),
2442             Twine("Invalid value ") + Type + "found when setting field '" +
2443                 Value.getNameInitAsString() + "' of type '" +
2444                 Value.getType()->getAsString() +
2445                 "' after resolving references: " + VR->getAsUnquotedString() +
2446                 "\n");
2447       }
2448     }
2449   }
2450 
2451   // Resolve the assertion expressions.
2452   for (auto &Assertion : Assertions) {
2453     Init *Value = Assertion.Condition->resolveReferences(R);
2454     Assertion.Condition = Value;
2455     Value = Assertion.Message->resolveReferences(R);
2456     Assertion.Message = Value;
2457   }
2458 }
2459 
2460 void Record::resolveReferences(Init *NewName) {
2461   RecordResolver R(*this);
2462   R.setName(NewName);
2463   R.setFinal(true);
2464   resolveReferences(R);
2465 }
2466 
2467 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2468 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
2469 #endif
2470 
2471 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
2472   OS << R.getNameInitAsString();
2473 
2474   ArrayRef<Init *> TArgs = R.getTemplateArgs();
2475   if (!TArgs.empty()) {
2476     OS << "<";
2477     bool NeedComma = false;
2478     for (const Init *TA : TArgs) {
2479       if (NeedComma) OS << ", ";
2480       NeedComma = true;
2481       const RecordVal *RV = R.getValue(TA);
2482       assert(RV && "Template argument record not found??");
2483       RV->print(OS, false);
2484     }
2485     OS << ">";
2486   }
2487 
2488   OS << " {";
2489   ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
2490   if (!SC.empty()) {
2491     OS << "\t//";
2492     for (const auto &SuperPair : SC)
2493       OS << " " << SuperPair.first->getNameInitAsString();
2494   }
2495   OS << "\n";
2496 
2497   for (const RecordVal &Val : R.getValues())
2498     if (Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit()))
2499       OS << Val;
2500   for (const RecordVal &Val : R.getValues())
2501     if (!Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit()))
2502       OS << Val;
2503 
2504   return OS << "}\n";
2505 }
2506 
2507 SMLoc Record::getFieldLoc(StringRef FieldName) const {
2508   const RecordVal *R = getValue(FieldName);
2509   if (!R)
2510     PrintFatalError(getLoc(), "Record `" + getName() +
2511       "' does not have a field named `" + FieldName + "'!\n");
2512   return R->getLoc();
2513 }
2514 
2515 Init *Record::getValueInit(StringRef FieldName) const {
2516   const RecordVal *R = getValue(FieldName);
2517   if (!R || !R->getValue())
2518     PrintFatalError(getLoc(), "Record `" + getName() +
2519       "' does not have a field named `" + FieldName + "'!\n");
2520   return R->getValue();
2521 }
2522 
2523 StringRef Record::getValueAsString(StringRef FieldName) const {
2524   llvm::Optional<StringRef> S = getValueAsOptionalString(FieldName);
2525   if (!S.hasValue())
2526     PrintFatalError(getLoc(), "Record `" + getName() +
2527       "' does not have a field named `" + FieldName + "'!\n");
2528   return S.getValue();
2529 }
2530 
2531 llvm::Optional<StringRef>
2532 Record::getValueAsOptionalString(StringRef FieldName) const {
2533   const RecordVal *R = getValue(FieldName);
2534   if (!R || !R->getValue())
2535     return llvm::Optional<StringRef>();
2536   if (isa<UnsetInit>(R->getValue()))
2537     return llvm::Optional<StringRef>();
2538 
2539   if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
2540     return SI->getValue();
2541 
2542   PrintFatalError(getLoc(),
2543                   "Record `" + getName() + "', ` field `" + FieldName +
2544                       "' exists but does not have a string initializer!");
2545 }
2546 
2547 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
2548   const RecordVal *R = getValue(FieldName);
2549   if (!R || !R->getValue())
2550     PrintFatalError(getLoc(), "Record `" + getName() +
2551       "' does not have a field named `" + FieldName + "'!\n");
2552 
2553   if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
2554     return BI;
2555   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
2556                                 "' exists but does not have a bits value");
2557 }
2558 
2559 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
2560   const RecordVal *R = getValue(FieldName);
2561   if (!R || !R->getValue())
2562     PrintFatalError(getLoc(), "Record `" + getName() +
2563       "' does not have a field named `" + FieldName + "'!\n");
2564 
2565   if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
2566     return LI;
2567   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
2568                                 "' exists but does not have a list value");
2569 }
2570 
2571 std::vector<Record*>
2572 Record::getValueAsListOfDefs(StringRef FieldName) const {
2573   ListInit *List = getValueAsListInit(FieldName);
2574   std::vector<Record*> Defs;
2575   for (Init *I : List->getValues()) {
2576     if (DefInit *DI = dyn_cast<DefInit>(I))
2577       Defs.push_back(DI->getDef());
2578     else
2579       PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2580         FieldName + "' list is not entirely DefInit!");
2581   }
2582   return Defs;
2583 }
2584 
2585 int64_t Record::getValueAsInt(StringRef FieldName) const {
2586   const RecordVal *R = getValue(FieldName);
2587   if (!R || !R->getValue())
2588     PrintFatalError(getLoc(), "Record `" + getName() +
2589       "' does not have a field named `" + FieldName + "'!\n");
2590 
2591   if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
2592     return II->getValue();
2593   PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2594                                 FieldName +
2595                                 "' exists but does not have an int value: " +
2596                                 R->getValue()->getAsString());
2597 }
2598 
2599 std::vector<int64_t>
2600 Record::getValueAsListOfInts(StringRef FieldName) const {
2601   ListInit *List = getValueAsListInit(FieldName);
2602   std::vector<int64_t> Ints;
2603   for (Init *I : List->getValues()) {
2604     if (IntInit *II = dyn_cast<IntInit>(I))
2605       Ints.push_back(II->getValue());
2606     else
2607       PrintFatalError(getLoc(),
2608                       Twine("Record `") + getName() + "', field `" + FieldName +
2609                           "' exists but does not have a list of ints value: " +
2610                           I->getAsString());
2611   }
2612   return Ints;
2613 }
2614 
2615 std::vector<StringRef>
2616 Record::getValueAsListOfStrings(StringRef FieldName) const {
2617   ListInit *List = getValueAsListInit(FieldName);
2618   std::vector<StringRef> Strings;
2619   for (Init *I : List->getValues()) {
2620     if (StringInit *SI = dyn_cast<StringInit>(I))
2621       Strings.push_back(SI->getValue());
2622     else
2623       PrintFatalError(getLoc(),
2624                       Twine("Record `") + getName() + "', field `" + FieldName +
2625                           "' exists but does not have a list of strings value: " +
2626                           I->getAsString());
2627   }
2628   return Strings;
2629 }
2630 
2631 Record *Record::getValueAsDef(StringRef FieldName) const {
2632   const RecordVal *R = getValue(FieldName);
2633   if (!R || !R->getValue())
2634     PrintFatalError(getLoc(), "Record `" + getName() +
2635       "' does not have a field named `" + FieldName + "'!\n");
2636 
2637   if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2638     return DI->getDef();
2639   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2640     FieldName + "' does not have a def initializer!");
2641 }
2642 
2643 Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
2644   const RecordVal *R = getValue(FieldName);
2645   if (!R || !R->getValue())
2646     PrintFatalError(getLoc(), "Record `" + getName() +
2647       "' does not have a field named `" + FieldName + "'!\n");
2648 
2649   if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2650     return DI->getDef();
2651   if (isa<UnsetInit>(R->getValue()))
2652     return nullptr;
2653   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2654     FieldName + "' does not have either a def initializer or '?'!");
2655 }
2656 
2657 
2658 bool Record::getValueAsBit(StringRef FieldName) const {
2659   const RecordVal *R = getValue(FieldName);
2660   if (!R || !R->getValue())
2661     PrintFatalError(getLoc(), "Record `" + getName() +
2662       "' does not have a field named `" + FieldName + "'!\n");
2663 
2664   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2665     return BI->getValue();
2666   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2667     FieldName + "' does not have a bit initializer!");
2668 }
2669 
2670 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
2671   const RecordVal *R = getValue(FieldName);
2672   if (!R || !R->getValue())
2673     PrintFatalError(getLoc(), "Record `" + getName() +
2674       "' does not have a field named `" + FieldName.str() + "'!\n");
2675 
2676   if (isa<UnsetInit>(R->getValue())) {
2677     Unset = true;
2678     return false;
2679   }
2680   Unset = false;
2681   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2682     return BI->getValue();
2683   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2684     FieldName + "' does not have a bit initializer!");
2685 }
2686 
2687 DagInit *Record::getValueAsDag(StringRef FieldName) const {
2688   const RecordVal *R = getValue(FieldName);
2689   if (!R || !R->getValue())
2690     PrintFatalError(getLoc(), "Record `" + getName() +
2691       "' does not have a field named `" + FieldName + "'!\n");
2692 
2693   if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
2694     return DI;
2695   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2696     FieldName + "' does not have a dag initializer!");
2697 }
2698 
2699 // Check all record assertions: For each one, resolve the condition
2700 // and message, then call CheckAssert().
2701 // Note: The condition and message are probably already resolved,
2702 //       but resolving again allows calls before records are resolved.
2703 void Record::checkRecordAssertions() {
2704   RecordResolver R(*this);
2705   R.setFinal(true);
2706 
2707   for (const auto &Assertion : getAssertions()) {
2708     Init *Condition = Assertion.Condition->resolveReferences(R);
2709     Init *Message = Assertion.Message->resolveReferences(R);
2710     CheckAssert(Assertion.Loc, Condition, Message);
2711   }
2712 }
2713 
2714 // Report a warning if the record has unused template arguments.
2715 void Record::checkUnusedTemplateArgs() {
2716   for (const Init *TA : getTemplateArgs()) {
2717     const RecordVal *Arg = getValue(TA);
2718     if (!Arg->isUsed())
2719       PrintWarning(Arg->getLoc(),
2720                    "unused template argument: " + Twine(Arg->getName()));
2721   }
2722 }
2723 
2724 RecordKeeper::RecordKeeper()
2725     : Impl(std::make_unique<detail::RecordKeeperImpl>(*this)) {}
2726 RecordKeeper::~RecordKeeper() = default;
2727 
2728 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2729 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
2730 #endif
2731 
2732 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
2733   OS << "------------- Classes -----------------\n";
2734   for (const auto &C : RK.getClasses())
2735     OS << "class " << *C.second;
2736 
2737   OS << "------------- Defs -----------------\n";
2738   for (const auto &D : RK.getDefs())
2739     OS << "def " << *D.second;
2740   return OS;
2741 }
2742 
2743 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2744 /// an identifier.
2745 Init *RecordKeeper::getNewAnonymousName() {
2746   return AnonymousNameInit::get(*this, getImpl().AnonCounter++);
2747 }
2748 
2749 // These functions implement the phase timing facility. Starting a timer
2750 // when one is already running stops the running one.
2751 
2752 void RecordKeeper::startTimer(StringRef Name) {
2753   if (TimingGroup) {
2754     if (LastTimer && LastTimer->isRunning()) {
2755       LastTimer->stopTimer();
2756       if (BackendTimer) {
2757         LastTimer->clear();
2758         BackendTimer = false;
2759       }
2760     }
2761 
2762     LastTimer = new Timer("", Name, *TimingGroup);
2763     LastTimer->startTimer();
2764   }
2765 }
2766 
2767 void RecordKeeper::stopTimer() {
2768   if (TimingGroup) {
2769     assert(LastTimer && "No phase timer was started");
2770     LastTimer->stopTimer();
2771   }
2772 }
2773 
2774 void RecordKeeper::startBackendTimer(StringRef Name) {
2775   if (TimingGroup) {
2776     startTimer(Name);
2777     BackendTimer = true;
2778   }
2779 }
2780 
2781 void RecordKeeper::stopBackendTimer() {
2782   if (TimingGroup) {
2783     if (BackendTimer) {
2784       stopTimer();
2785       BackendTimer = false;
2786     }
2787   }
2788 }
2789 
2790 std::vector<Record *>
2791 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
2792   // We cache the record vectors for single classes. Many backends request
2793   // the same vectors multiple times.
2794   auto Pair = ClassRecordsMap.try_emplace(ClassName);
2795   if (Pair.second)
2796     Pair.first->second = getAllDerivedDefinitions(makeArrayRef(ClassName));
2797 
2798   return Pair.first->second;
2799 }
2800 
2801 std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
2802     ArrayRef<StringRef> ClassNames) const {
2803   SmallVector<Record *, 2> ClassRecs;
2804   std::vector<Record *> Defs;
2805 
2806   assert(ClassNames.size() > 0 && "At least one class must be passed.");
2807   for (const auto &ClassName : ClassNames) {
2808     Record *Class = getClass(ClassName);
2809     if (!Class)
2810       PrintFatalError("The class '" + ClassName + "' is not defined\n");
2811     ClassRecs.push_back(Class);
2812   }
2813 
2814   for (const auto &OneDef : getDefs()) {
2815     if (all_of(ClassRecs, [&OneDef](const Record *Class) {
2816                             return OneDef.second->isSubClassOf(Class);
2817                           }))
2818       Defs.push_back(OneDef.second.get());
2819   }
2820 
2821   return Defs;
2822 }
2823 
2824 std::vector<Record *>
2825 RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) const {
2826   return getClass(ClassName) ? getAllDerivedDefinitions(ClassName)
2827                              : std::vector<Record *>();
2828 }
2829 
2830 Init *MapResolver::resolve(Init *VarName) {
2831   auto It = Map.find(VarName);
2832   if (It == Map.end())
2833     return nullptr;
2834 
2835   Init *I = It->second.V;
2836 
2837   if (!It->second.Resolved && Map.size() > 1) {
2838     // Resolve mutual references among the mapped variables, but prevent
2839     // infinite recursion.
2840     Map.erase(It);
2841     I = I->resolveReferences(*this);
2842     Map[VarName] = {I, true};
2843   }
2844 
2845   return I;
2846 }
2847 
2848 Init *RecordResolver::resolve(Init *VarName) {
2849   Init *Val = Cache.lookup(VarName);
2850   if (Val)
2851     return Val;
2852 
2853   if (llvm::is_contained(Stack, VarName))
2854     return nullptr; // prevent infinite recursion
2855 
2856   if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
2857     if (!isa<UnsetInit>(RV->getValue())) {
2858       Val = RV->getValue();
2859       Stack.push_back(VarName);
2860       Val = Val->resolveReferences(*this);
2861       Stack.pop_back();
2862     }
2863   } else if (Name && VarName == getCurrentRecord()->getNameInit()) {
2864     Stack.push_back(VarName);
2865     Val = Name->resolveReferences(*this);
2866     Stack.pop_back();
2867   }
2868 
2869   Cache[VarName] = Val;
2870   return Val;
2871 }
2872 
2873 Init *TrackUnresolvedResolver::resolve(Init *VarName) {
2874   Init *I = nullptr;
2875 
2876   if (R) {
2877     I = R->resolve(VarName);
2878     if (I && !FoundUnresolved) {
2879       // Do not recurse into the resolved initializer, as that would change
2880       // the behavior of the resolver we're delegating, but do check to see
2881       // if there are unresolved variables remaining.
2882       TrackUnresolvedResolver Sub;
2883       I->resolveReferences(Sub);
2884       FoundUnresolved |= Sub.FoundUnresolved;
2885     }
2886   }
2887 
2888   if (!I)
2889     FoundUnresolved = true;
2890   return I;
2891 }
2892 
2893 Init *HasReferenceResolver::resolve(Init *VarName)
2894 {
2895   if (VarName == VarNameToTrack)
2896     Found = true;
2897   return nullptr;
2898 }
2899