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