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   case SUBSTR: {
1330     StringInit *LHSs = dyn_cast<StringInit>(LHS);
1331     IntInit *MHSi = dyn_cast<IntInit>(MHS);
1332     IntInit *RHSi = dyn_cast<IntInit>(RHS);
1333     if (LHSs && MHSi && RHSi) {
1334       int64_t StringSize = LHSs->getValue().size();
1335       int64_t Start = MHSi->getValue();
1336       int64_t Length = RHSi->getValue();
1337       if (Start < 0 || Start > StringSize)
1338         PrintError(CurRec->getLoc(),
1339                    Twine("!substr start position is out of range 0...") +
1340                        std::to_string(StringSize) + ": " +
1341                        std::to_string(Start));
1342       if (Length < 0)
1343         PrintError(CurRec->getLoc(), "!substr length must be nonnegative");
1344       return StringInit::get(LHSs->getValue().substr(Start, Length),
1345                              LHSs->getFormat());
1346     }
1347     break;
1348   }
1349   }
1350 
1351   return const_cast<TernOpInit *>(this);
1352 }
1353 
1354 Init *TernOpInit::resolveReferences(Resolver &R) const {
1355   Init *lhs = LHS->resolveReferences(R);
1356 
1357   if (getOpcode() == IF && lhs != LHS) {
1358     if (IntInit *Value = dyn_cast_or_null<IntInit>(
1359                              lhs->convertInitializerTo(IntRecTy::get()))) {
1360       // Short-circuit
1361       if (Value->getValue())
1362         return MHS->resolveReferences(R);
1363       return RHS->resolveReferences(R);
1364     }
1365   }
1366 
1367   Init *mhs = MHS->resolveReferences(R);
1368   Init *rhs;
1369 
1370   if (getOpcode() == FOREACH || getOpcode() == FILTER) {
1371     ShadowResolver SR(R);
1372     SR.addShadow(lhs);
1373     rhs = RHS->resolveReferences(SR);
1374   } else {
1375     rhs = RHS->resolveReferences(R);
1376   }
1377 
1378   if (LHS != lhs || MHS != mhs || RHS != rhs)
1379     return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
1380         ->Fold(R.getCurrentRecord());
1381   return const_cast<TernOpInit *>(this);
1382 }
1383 
1384 std::string TernOpInit::getAsString() const {
1385   std::string Result;
1386   bool UnquotedLHS = false;
1387   switch (getOpcode()) {
1388   case DAG: Result = "!dag"; break;
1389   case FILTER: Result = "!filter"; UnquotedLHS = true; break;
1390   case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
1391   case IF: Result = "!if"; break;
1392   case SUBST: Result = "!subst"; break;
1393   case SUBSTR: Result = "!substr"; break;
1394   }
1395   return (Result + "(" +
1396           (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
1397           ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
1398 }
1399 
1400 static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B,
1401                               Init *Start, Init *List, Init *Expr,
1402                               RecTy *Type) {
1403   ID.AddPointer(Start);
1404   ID.AddPointer(List);
1405   ID.AddPointer(A);
1406   ID.AddPointer(B);
1407   ID.AddPointer(Expr);
1408   ID.AddPointer(Type);
1409 }
1410 
1411 FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
1412                             Init *Expr, RecTy *Type) {
1413   static FoldingSet<FoldOpInit> ThePool;
1414 
1415   FoldingSetNodeID ID;
1416   ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
1417 
1418   void *IP = nullptr;
1419   if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1420     return I;
1421 
1422   FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
1423   ThePool.InsertNode(I, IP);
1424   return I;
1425 }
1426 
1427 void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
1428   ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
1429 }
1430 
1431 Init *FoldOpInit::Fold(Record *CurRec) const {
1432   if (ListInit *LI = dyn_cast<ListInit>(List)) {
1433     Init *Accum = Start;
1434     for (Init *Elt : *LI) {
1435       MapResolver R(CurRec);
1436       R.set(A, Accum);
1437       R.set(B, Elt);
1438       Accum = Expr->resolveReferences(R);
1439     }
1440     return Accum;
1441   }
1442   return const_cast<FoldOpInit *>(this);
1443 }
1444 
1445 Init *FoldOpInit::resolveReferences(Resolver &R) const {
1446   Init *NewStart = Start->resolveReferences(R);
1447   Init *NewList = List->resolveReferences(R);
1448   ShadowResolver SR(R);
1449   SR.addShadow(A);
1450   SR.addShadow(B);
1451   Init *NewExpr = Expr->resolveReferences(SR);
1452 
1453   if (Start == NewStart && List == NewList && Expr == NewExpr)
1454     return const_cast<FoldOpInit *>(this);
1455 
1456   return get(NewStart, NewList, A, B, NewExpr, getType())
1457       ->Fold(R.getCurrentRecord());
1458 }
1459 
1460 Init *FoldOpInit::getBit(unsigned Bit) const {
1461   return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
1462 }
1463 
1464 std::string FoldOpInit::getAsString() const {
1465   return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
1466           ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
1467           ", " + Expr->getAsString() + ")")
1468       .str();
1469 }
1470 
1471 static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType,
1472                              Init *Expr) {
1473   ID.AddPointer(CheckType);
1474   ID.AddPointer(Expr);
1475 }
1476 
1477 IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
1478   static FoldingSet<IsAOpInit> ThePool;
1479 
1480   FoldingSetNodeID ID;
1481   ProfileIsAOpInit(ID, CheckType, Expr);
1482 
1483   void *IP = nullptr;
1484   if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1485     return I;
1486 
1487   IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
1488   ThePool.InsertNode(I, IP);
1489   return I;
1490 }
1491 
1492 void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
1493   ProfileIsAOpInit(ID, CheckType, Expr);
1494 }
1495 
1496 Init *IsAOpInit::Fold() const {
1497   if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
1498     // Is the expression type known to be (a subclass of) the desired type?
1499     if (TI->getType()->typeIsConvertibleTo(CheckType))
1500       return IntInit::get(1);
1501 
1502     if (isa<RecordRecTy>(CheckType)) {
1503       // If the target type is not a subclass of the expression type, or if
1504       // the expression has fully resolved to a record, we know that it can't
1505       // be of the required type.
1506       if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
1507         return IntInit::get(0);
1508     } else {
1509       // We treat non-record types as not castable.
1510       return IntInit::get(0);
1511     }
1512   }
1513   return const_cast<IsAOpInit *>(this);
1514 }
1515 
1516 Init *IsAOpInit::resolveReferences(Resolver &R) const {
1517   Init *NewExpr = Expr->resolveReferences(R);
1518   if (Expr != NewExpr)
1519     return get(CheckType, NewExpr)->Fold();
1520   return const_cast<IsAOpInit *>(this);
1521 }
1522 
1523 Init *IsAOpInit::getBit(unsigned Bit) const {
1524   return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
1525 }
1526 
1527 std::string IsAOpInit::getAsString() const {
1528   return (Twine("!isa<") + CheckType->getAsString() + ">(" +
1529           Expr->getAsString() + ")")
1530       .str();
1531 }
1532 
1533 RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1534   if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
1535     for (Record *Rec : RecordType->getClasses()) {
1536       if (RecordVal *Field = Rec->getValue(FieldName))
1537         return Field->getType();
1538     }
1539   }
1540   return nullptr;
1541 }
1542 
1543 Init *
1544 TypedInit::convertInitializerTo(RecTy *Ty) const {
1545   if (getType() == Ty || getType()->typeIsA(Ty))
1546     return const_cast<TypedInit *>(this);
1547 
1548   if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
1549       cast<BitsRecTy>(Ty)->getNumBits() == 1)
1550     return BitsInit::get({const_cast<TypedInit *>(this)});
1551 
1552   return nullptr;
1553 }
1554 
1555 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1556   BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1557   if (!T) return nullptr;  // Cannot subscript a non-bits variable.
1558   unsigned NumBits = T->getNumBits();
1559 
1560   SmallVector<Init *, 16> NewBits;
1561   NewBits.reserve(Bits.size());
1562   for (unsigned Bit : Bits) {
1563     if (Bit >= NumBits)
1564       return nullptr;
1565 
1566     NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1567   }
1568   return BitsInit::get(NewBits);
1569 }
1570 
1571 Init *TypedInit::getCastTo(RecTy *Ty) const {
1572   // Handle the common case quickly
1573   if (getType() == Ty || getType()->typeIsA(Ty))
1574     return const_cast<TypedInit *>(this);
1575 
1576   if (Init *Converted = convertInitializerTo(Ty)) {
1577     assert(!isa<TypedInit>(Converted) ||
1578            cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
1579     return Converted;
1580   }
1581 
1582   if (!getType()->typeIsConvertibleTo(Ty))
1583     return nullptr;
1584 
1585   return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
1586       ->Fold(nullptr);
1587 }
1588 
1589 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1590   ListRecTy *T = dyn_cast<ListRecTy>(getType());
1591   if (!T) return nullptr;  // Cannot subscript a non-list variable.
1592 
1593   if (Elements.size() == 1)
1594     return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1595 
1596   SmallVector<Init*, 8> ListInits;
1597   ListInits.reserve(Elements.size());
1598   for (unsigned Element : Elements)
1599     ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1600                                                 Element));
1601   return ListInit::get(ListInits, T->getElementType());
1602 }
1603 
1604 
1605 VarInit *VarInit::get(StringRef VN, RecTy *T) {
1606   Init *Value = StringInit::get(VN);
1607   return VarInit::get(Value, T);
1608 }
1609 
1610 VarInit *VarInit::get(Init *VN, RecTy *T) {
1611   using Key = std::pair<RecTy *, Init *>;
1612   static DenseMap<Key, VarInit*> ThePool;
1613 
1614   Key TheKey(std::make_pair(T, VN));
1615 
1616   VarInit *&I = ThePool[TheKey];
1617   if (!I)
1618     I = new(Allocator) VarInit(VN, T);
1619   return I;
1620 }
1621 
1622 StringRef VarInit::getName() const {
1623   StringInit *NameString = cast<StringInit>(getNameInit());
1624   return NameString->getValue();
1625 }
1626 
1627 Init *VarInit::getBit(unsigned Bit) const {
1628   if (getType() == BitRecTy::get())
1629     return const_cast<VarInit*>(this);
1630   return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1631 }
1632 
1633 Init *VarInit::resolveReferences(Resolver &R) const {
1634   if (Init *Val = R.resolve(VarName))
1635     return Val;
1636   return const_cast<VarInit *>(this);
1637 }
1638 
1639 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1640   using Key = std::pair<TypedInit *, unsigned>;
1641   static DenseMap<Key, VarBitInit*> ThePool;
1642 
1643   Key TheKey(std::make_pair(T, B));
1644 
1645   VarBitInit *&I = ThePool[TheKey];
1646   if (!I)
1647     I = new(Allocator) VarBitInit(T, B);
1648   return I;
1649 }
1650 
1651 std::string VarBitInit::getAsString() const {
1652   return TI->getAsString() + "{" + utostr(Bit) + "}";
1653 }
1654 
1655 Init *VarBitInit::resolveReferences(Resolver &R) const {
1656   Init *I = TI->resolveReferences(R);
1657   if (TI != I)
1658     return I->getBit(getBitNum());
1659 
1660   return const_cast<VarBitInit*>(this);
1661 }
1662 
1663 VarListElementInit *VarListElementInit::get(TypedInit *T,
1664                                             unsigned E) {
1665   using Key = std::pair<TypedInit *, unsigned>;
1666   static DenseMap<Key, VarListElementInit*> ThePool;
1667 
1668   Key TheKey(std::make_pair(T, E));
1669 
1670   VarListElementInit *&I = ThePool[TheKey];
1671   if (!I) I = new(Allocator) VarListElementInit(T, E);
1672   return I;
1673 }
1674 
1675 std::string VarListElementInit::getAsString() const {
1676   return TI->getAsString() + "[" + utostr(Element) + "]";
1677 }
1678 
1679 Init *VarListElementInit::resolveReferences(Resolver &R) const {
1680   Init *NewTI = TI->resolveReferences(R);
1681   if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1682     // Leave out-of-bounds array references as-is. This can happen without
1683     // being an error, e.g. in the untaken "branch" of an !if expression.
1684     if (getElementNum() < List->size())
1685       return List->getElement(getElementNum());
1686   }
1687   if (NewTI != TI && isa<TypedInit>(NewTI))
1688     return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1689   return const_cast<VarListElementInit *>(this);
1690 }
1691 
1692 Init *VarListElementInit::getBit(unsigned Bit) const {
1693   if (getType() == BitRecTy::get())
1694     return const_cast<VarListElementInit*>(this);
1695   return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1696 }
1697 
1698 DefInit::DefInit(Record *D)
1699     : TypedInit(IK_DefInit, D->getType()), Def(D) {}
1700 
1701 DefInit *DefInit::get(Record *R) {
1702   return R->getDefInit();
1703 }
1704 
1705 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1706   if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1707     if (getType()->typeIsConvertibleTo(RRT))
1708       return const_cast<DefInit *>(this);
1709   return nullptr;
1710 }
1711 
1712 RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1713   if (const RecordVal *RV = Def->getValue(FieldName))
1714     return RV->getType();
1715   return nullptr;
1716 }
1717 
1718 std::string DefInit::getAsString() const { return std::string(Def->getName()); }
1719 
1720 static void ProfileVarDefInit(FoldingSetNodeID &ID,
1721                               Record *Class,
1722                               ArrayRef<Init *> Args) {
1723   ID.AddInteger(Args.size());
1724   ID.AddPointer(Class);
1725 
1726   for (Init *I : Args)
1727     ID.AddPointer(I);
1728 }
1729 
1730 VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
1731   static FoldingSet<VarDefInit> ThePool;
1732 
1733   FoldingSetNodeID ID;
1734   ProfileVarDefInit(ID, Class, Args);
1735 
1736   void *IP = nullptr;
1737   if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1738     return I;
1739 
1740   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
1741                                  alignof(VarDefInit));
1742   VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
1743   std::uninitialized_copy(Args.begin(), Args.end(),
1744                           I->getTrailingObjects<Init *>());
1745   ThePool.InsertNode(I, IP);
1746   return I;
1747 }
1748 
1749 void VarDefInit::Profile(FoldingSetNodeID &ID) const {
1750   ProfileVarDefInit(ID, Class, args());
1751 }
1752 
1753 DefInit *VarDefInit::instantiate() {
1754   if (!Def) {
1755     RecordKeeper &Records = Class->getRecords();
1756     auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
1757                                            Class->getLoc(), Records,
1758                                            /*IsAnonymous=*/true);
1759     Record *NewRec = NewRecOwner.get();
1760 
1761     // Copy values from class to instance
1762     for (const RecordVal &Val : Class->getValues())
1763       NewRec->addValue(Val);
1764 
1765     // Substitute and resolve template arguments
1766     ArrayRef<Init *> TArgs = Class->getTemplateArgs();
1767     MapResolver R(NewRec);
1768 
1769     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1770       if (i < args_size())
1771         R.set(TArgs[i], getArg(i));
1772       else
1773         R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
1774 
1775       NewRec->removeValue(TArgs[i]);
1776     }
1777 
1778     NewRec->resolveReferences(R);
1779 
1780     // Add superclasses.
1781     ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
1782     for (const auto &SCPair : SCs)
1783       NewRec->addSuperClass(SCPair.first, SCPair.second);
1784 
1785     NewRec->addSuperClass(Class,
1786                           SMRange(Class->getLoc().back(),
1787                                   Class->getLoc().back()));
1788 
1789     // Resolve internal references and store in record keeper
1790     NewRec->resolveReferences();
1791     Records.addDef(std::move(NewRecOwner));
1792 
1793     Def = DefInit::get(NewRec);
1794   }
1795 
1796   return Def;
1797 }
1798 
1799 Init *VarDefInit::resolveReferences(Resolver &R) const {
1800   TrackUnresolvedResolver UR(&R);
1801   bool Changed = false;
1802   SmallVector<Init *, 8> NewArgs;
1803   NewArgs.reserve(args_size());
1804 
1805   for (Init *Arg : args()) {
1806     Init *NewArg = Arg->resolveReferences(UR);
1807     NewArgs.push_back(NewArg);
1808     Changed |= NewArg != Arg;
1809   }
1810 
1811   if (Changed) {
1812     auto New = VarDefInit::get(Class, NewArgs);
1813     if (!UR.foundUnresolved())
1814       return New->instantiate();
1815     return New;
1816   }
1817   return const_cast<VarDefInit *>(this);
1818 }
1819 
1820 Init *VarDefInit::Fold() const {
1821   if (Def)
1822     return Def;
1823 
1824   TrackUnresolvedResolver R;
1825   for (Init *Arg : args())
1826     Arg->resolveReferences(R);
1827 
1828   if (!R.foundUnresolved())
1829     return const_cast<VarDefInit *>(this)->instantiate();
1830   return const_cast<VarDefInit *>(this);
1831 }
1832 
1833 std::string VarDefInit::getAsString() const {
1834   std::string Result = Class->getNameInitAsString() + "<";
1835   const char *sep = "";
1836   for (Init *Arg : args()) {
1837     Result += sep;
1838     sep = ", ";
1839     Result += Arg->getAsString();
1840   }
1841   return Result + ">";
1842 }
1843 
1844 FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1845   using Key = std::pair<Init *, StringInit *>;
1846   static DenseMap<Key, FieldInit*> ThePool;
1847 
1848   Key TheKey(std::make_pair(R, FN));
1849 
1850   FieldInit *&I = ThePool[TheKey];
1851   if (!I) I = new(Allocator) FieldInit(R, FN);
1852   return I;
1853 }
1854 
1855 Init *FieldInit::getBit(unsigned Bit) const {
1856   if (getType() == BitRecTy::get())
1857     return const_cast<FieldInit*>(this);
1858   return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1859 }
1860 
1861 Init *FieldInit::resolveReferences(Resolver &R) const {
1862   Init *NewRec = Rec->resolveReferences(R);
1863   if (NewRec != Rec)
1864     return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
1865   return const_cast<FieldInit *>(this);
1866 }
1867 
1868 Init *FieldInit::Fold(Record *CurRec) const {
1869   if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1870     Record *Def = DI->getDef();
1871     if (Def == CurRec)
1872       PrintFatalError(CurRec->getLoc(),
1873                       Twine("Attempting to access field '") +
1874                       FieldName->getAsUnquotedString() + "' of '" +
1875                       Rec->getAsString() + "' is a forbidden self-reference");
1876     Init *FieldVal = Def->getValue(FieldName)->getValue();
1877     if (FieldVal->isComplete())
1878       return FieldVal;
1879   }
1880   return const_cast<FieldInit *>(this);
1881 }
1882 
1883 bool FieldInit::isConcrete() const {
1884   if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1885     Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue();
1886     return FieldVal->isConcrete();
1887   }
1888   return false;
1889 }
1890 
1891 static void ProfileCondOpInit(FoldingSetNodeID &ID,
1892                              ArrayRef<Init *> CondRange,
1893                              ArrayRef<Init *> ValRange,
1894                              const RecTy *ValType) {
1895   assert(CondRange.size() == ValRange.size() &&
1896          "Number of conditions and values must match!");
1897   ID.AddPointer(ValType);
1898   ArrayRef<Init *>::iterator Case = CondRange.begin();
1899   ArrayRef<Init *>::iterator Val = ValRange.begin();
1900 
1901   while (Case != CondRange.end()) {
1902     ID.AddPointer(*Case++);
1903     ID.AddPointer(*Val++);
1904   }
1905 }
1906 
1907 void CondOpInit::Profile(FoldingSetNodeID &ID) const {
1908   ProfileCondOpInit(ID,
1909       makeArrayRef(getTrailingObjects<Init *>(), NumConds),
1910       makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds),
1911       ValType);
1912 }
1913 
1914 CondOpInit *
1915 CondOpInit::get(ArrayRef<Init *> CondRange,
1916                 ArrayRef<Init *> ValRange, RecTy *Ty) {
1917   assert(CondRange.size() == ValRange.size() &&
1918          "Number of conditions and values must match!");
1919 
1920   static FoldingSet<CondOpInit> ThePool;
1921   FoldingSetNodeID ID;
1922   ProfileCondOpInit(ID, CondRange, ValRange, Ty);
1923 
1924   void *IP = nullptr;
1925   if (CondOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1926     return I;
1927 
1928   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(2*CondRange.size()),
1929                                  alignof(BitsInit));
1930   CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty);
1931 
1932   std::uninitialized_copy(CondRange.begin(), CondRange.end(),
1933                           I->getTrailingObjects<Init *>());
1934   std::uninitialized_copy(ValRange.begin(), ValRange.end(),
1935                           I->getTrailingObjects<Init *>()+CondRange.size());
1936   ThePool.InsertNode(I, IP);
1937   return I;
1938 }
1939 
1940 Init *CondOpInit::resolveReferences(Resolver &R) const {
1941   SmallVector<Init*, 4> NewConds;
1942   bool Changed = false;
1943   for (const Init *Case : getConds()) {
1944     Init *NewCase = Case->resolveReferences(R);
1945     NewConds.push_back(NewCase);
1946     Changed |= NewCase != Case;
1947   }
1948 
1949   SmallVector<Init*, 4> NewVals;
1950   for (const Init *Val : getVals()) {
1951     Init *NewVal = Val->resolveReferences(R);
1952     NewVals.push_back(NewVal);
1953     Changed |= NewVal != Val;
1954   }
1955 
1956   if (Changed)
1957     return (CondOpInit::get(NewConds, NewVals,
1958             getValType()))->Fold(R.getCurrentRecord());
1959 
1960   return const_cast<CondOpInit *>(this);
1961 }
1962 
1963 Init *CondOpInit::Fold(Record *CurRec) const {
1964   for ( unsigned i = 0; i < NumConds; ++i) {
1965     Init *Cond = getCond(i);
1966     Init *Val = getVal(i);
1967 
1968     if (IntInit *CondI = dyn_cast_or_null<IntInit>(
1969             Cond->convertInitializerTo(IntRecTy::get()))) {
1970       if (CondI->getValue())
1971         return Val->convertInitializerTo(getValType());
1972     } else
1973      return const_cast<CondOpInit *>(this);
1974   }
1975 
1976   PrintFatalError(CurRec->getLoc(),
1977                   CurRec->getName() +
1978                   " does not have any true condition in:" +
1979                   this->getAsString());
1980   return nullptr;
1981 }
1982 
1983 bool CondOpInit::isConcrete() const {
1984   for (const Init *Case : getConds())
1985     if (!Case->isConcrete())
1986       return false;
1987 
1988   for (const Init *Val : getVals())
1989     if (!Val->isConcrete())
1990       return false;
1991 
1992   return true;
1993 }
1994 
1995 bool CondOpInit::isComplete() const {
1996   for (const Init *Case : getConds())
1997     if (!Case->isComplete())
1998       return false;
1999 
2000   for (const Init *Val : getVals())
2001     if (!Val->isConcrete())
2002       return false;
2003 
2004   return true;
2005 }
2006 
2007 std::string CondOpInit::getAsString() const {
2008   std::string Result = "!cond(";
2009   for (unsigned i = 0; i < getNumConds(); i++) {
2010     Result += getCond(i)->getAsString() + ": ";
2011     Result += getVal(i)->getAsString();
2012     if (i != getNumConds()-1)
2013       Result += ", ";
2014   }
2015   return Result + ")";
2016 }
2017 
2018 Init *CondOpInit::getBit(unsigned Bit) const {
2019   return VarBitInit::get(const_cast<CondOpInit *>(this), Bit);
2020 }
2021 
2022 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
2023                            ArrayRef<Init *> ArgRange,
2024                            ArrayRef<StringInit *> NameRange) {
2025   ID.AddPointer(V);
2026   ID.AddPointer(VN);
2027 
2028   ArrayRef<Init *>::iterator Arg = ArgRange.begin();
2029   ArrayRef<StringInit *>::iterator Name = NameRange.begin();
2030   while (Arg != ArgRange.end()) {
2031     assert(Name != NameRange.end() && "Arg name underflow!");
2032     ID.AddPointer(*Arg++);
2033     ID.AddPointer(*Name++);
2034   }
2035   assert(Name == NameRange.end() && "Arg name overflow!");
2036 }
2037 
2038 DagInit *
2039 DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
2040              ArrayRef<StringInit *> NameRange) {
2041   static FoldingSet<DagInit> ThePool;
2042 
2043   FoldingSetNodeID ID;
2044   ProfileDagInit(ID, V, VN, ArgRange, NameRange);
2045 
2046   void *IP = nullptr;
2047   if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
2048     return I;
2049 
2050   void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
2051   DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
2052   std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
2053                           I->getTrailingObjects<Init *>());
2054   std::uninitialized_copy(NameRange.begin(), NameRange.end(),
2055                           I->getTrailingObjects<StringInit *>());
2056   ThePool.InsertNode(I, IP);
2057   return I;
2058 }
2059 
2060 DagInit *
2061 DagInit::get(Init *V, StringInit *VN,
2062              ArrayRef<std::pair<Init*, StringInit*>> args) {
2063   SmallVector<Init *, 8> Args;
2064   SmallVector<StringInit *, 8> Names;
2065 
2066   for (const auto &Arg : args) {
2067     Args.push_back(Arg.first);
2068     Names.push_back(Arg.second);
2069   }
2070 
2071   return DagInit::get(V, VN, Args, Names);
2072 }
2073 
2074 void DagInit::Profile(FoldingSetNodeID &ID) const {
2075   ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
2076 }
2077 
2078 Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
2079   if (DefInit *DefI = dyn_cast<DefInit>(Val))
2080     return DefI->getDef();
2081   PrintFatalError(Loc, "Expected record as operator");
2082   return nullptr;
2083 }
2084 
2085 Init *DagInit::resolveReferences(Resolver &R) const {
2086   SmallVector<Init*, 8> NewArgs;
2087   NewArgs.reserve(arg_size());
2088   bool ArgsChanged = false;
2089   for (const Init *Arg : getArgs()) {
2090     Init *NewArg = Arg->resolveReferences(R);
2091     NewArgs.push_back(NewArg);
2092     ArgsChanged |= NewArg != Arg;
2093   }
2094 
2095   Init *Op = Val->resolveReferences(R);
2096   if (Op != Val || ArgsChanged)
2097     return DagInit::get(Op, ValName, NewArgs, getArgNames());
2098 
2099   return const_cast<DagInit *>(this);
2100 }
2101 
2102 bool DagInit::isConcrete() const {
2103   if (!Val->isConcrete())
2104     return false;
2105   for (const Init *Elt : getArgs()) {
2106     if (!Elt->isConcrete())
2107       return false;
2108   }
2109   return true;
2110 }
2111 
2112 std::string DagInit::getAsString() const {
2113   std::string Result = "(" + Val->getAsString();
2114   if (ValName)
2115     Result += ":" + ValName->getAsUnquotedString();
2116   if (!arg_empty()) {
2117     Result += " " + getArg(0)->getAsString();
2118     if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
2119     for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
2120       Result += ", " + getArg(i)->getAsString();
2121       if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
2122     }
2123   }
2124   return Result + ")";
2125 }
2126 
2127 //===----------------------------------------------------------------------===//
2128 //    Other implementations
2129 //===----------------------------------------------------------------------===//
2130 
2131 RecordVal::RecordVal(Init *N, RecTy *T, bool P)
2132   : Name(N), TyAndPrefix(T, P) {
2133   setValue(UnsetInit::get());
2134   assert(Value && "Cannot create unset value for current type!");
2135 }
2136 
2137 // This constructor accepts the same arguments as the above, but also
2138 // a source location.
2139 RecordVal::RecordVal(Init *N, SMLoc Loc, RecTy *T, bool P)
2140     : Name(N), Loc(Loc), TyAndPrefix(T, P) {
2141   setValue(UnsetInit::get());
2142   assert(Value && "Cannot create unset value for current type!");
2143 }
2144 
2145 StringRef RecordVal::getName() const {
2146   return cast<StringInit>(getNameInit())->getValue();
2147 }
2148 
2149 std::string RecordVal::getPrintType() const {
2150   if (getType() == StringRecTy::get()) {
2151     if (auto *StrInit = dyn_cast<StringInit>(Value)) {
2152       if (StrInit->hasCodeFormat())
2153         return "code";
2154       else
2155         return "string";
2156     } else {
2157       return "string";
2158     }
2159   } else {
2160     return TyAndPrefix.getPointer()->getAsString();
2161   }
2162 }
2163 
2164 bool RecordVal::setValue(Init *V) {
2165   if (V) {
2166     Value = V->getCastTo(getType());
2167     if (Value) {
2168       assert(!isa<TypedInit>(Value) ||
2169              cast<TypedInit>(Value)->getType()->typeIsA(getType()));
2170       if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
2171         if (!isa<BitsInit>(Value)) {
2172           SmallVector<Init *, 64> Bits;
2173           Bits.reserve(BTy->getNumBits());
2174           for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
2175             Bits.push_back(Value->getBit(I));
2176           Value = BitsInit::get(Bits);
2177         }
2178       }
2179     }
2180     return Value == nullptr;
2181   }
2182   Value = nullptr;
2183   return false;
2184 }
2185 
2186 // This version of setValue takes a source location and resets the
2187 // location in the RecordVal.
2188 bool RecordVal::setValue(Init *V, SMLoc NewLoc) {
2189   Loc = NewLoc;
2190   if (V) {
2191     Value = V->getCastTo(getType());
2192     if (Value) {
2193       assert(!isa<TypedInit>(Value) ||
2194              cast<TypedInit>(Value)->getType()->typeIsA(getType()));
2195       if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
2196         if (!isa<BitsInit>(Value)) {
2197           SmallVector<Init *, 64> Bits;
2198           Bits.reserve(BTy->getNumBits());
2199           for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
2200             Bits.push_back(Value->getBit(I));
2201           Value = BitsInit::get(Bits);
2202         }
2203       }
2204     }
2205     return Value == nullptr;
2206   }
2207   Value = nullptr;
2208   return false;
2209 }
2210 
2211 #include "llvm/TableGen/Record.h"
2212 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2213 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
2214 #endif
2215 
2216 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
2217   if (getPrefix()) OS << "field ";
2218   OS << getPrintType() << " " << getNameInitAsString();
2219 
2220   if (getValue())
2221     OS << " = " << *getValue();
2222 
2223   if (PrintSem) OS << ";\n";
2224 }
2225 
2226 unsigned Record::LastID = 0;
2227 
2228 void Record::checkName() {
2229   // Ensure the record name has string type.
2230   const TypedInit *TypedName = cast<const TypedInit>(Name);
2231   if (!isa<StringRecTy>(TypedName->getType()))
2232     PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
2233                                   "' is not a string!");
2234 }
2235 
2236 RecordRecTy *Record::getType() {
2237   SmallVector<Record *, 4> DirectSCs;
2238   getDirectSuperClasses(DirectSCs);
2239   return RecordRecTy::get(DirectSCs);
2240 }
2241 
2242 DefInit *Record::getDefInit() {
2243   if (!CorrespondingDefInit)
2244     CorrespondingDefInit = new (Allocator) DefInit(this);
2245   return CorrespondingDefInit;
2246 }
2247 
2248 void Record::setName(Init *NewName) {
2249   Name = NewName;
2250   checkName();
2251   // DO NOT resolve record values to the name at this point because
2252   // there might be default values for arguments of this def.  Those
2253   // arguments might not have been resolved yet so we don't want to
2254   // prematurely assume values for those arguments were not passed to
2255   // this def.
2256   //
2257   // Nonetheless, it may be that some of this Record's values
2258   // reference the record name.  Indeed, the reason for having the
2259   // record name be an Init is to provide this flexibility.  The extra
2260   // resolve steps after completely instantiating defs takes care of
2261   // this.  See TGParser::ParseDef and TGParser::ParseDefm.
2262 }
2263 
2264 // NOTE for the next two functions:
2265 // Superclasses are in post-order, so the final one is a direct
2266 // superclass. All of its transitive superclases immediately precede it,
2267 // so we can step through the direct superclasses in reverse order.
2268 
2269 bool Record::hasDirectSuperClass(const Record *Superclass) const {
2270   ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
2271 
2272   for (int I = SCs.size() - 1; I >= 0; --I) {
2273     const Record *SC = SCs[I].first;
2274     if (SC == Superclass)
2275       return true;
2276     I -= SC->getSuperClasses().size();
2277   }
2278 
2279   return false;
2280 }
2281 
2282 void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
2283   ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
2284 
2285   while (!SCs.empty()) {
2286     Record *SC = SCs.back().first;
2287     SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
2288     Classes.push_back(SC);
2289   }
2290 }
2291 
2292 void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
2293   for (RecordVal &Value : Values) {
2294     if (SkipVal == &Value) // Skip resolve the same field as the given one
2295       continue;
2296     if (Init *V = Value.getValue()) {
2297       Init *VR = V->resolveReferences(R);
2298       if (Value.setValue(VR)) {
2299         std::string Type;
2300         if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
2301           Type =
2302               (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
2303         PrintFatalError(getLoc(), Twine("Invalid value ") + Type +
2304                                       "is found when setting '" +
2305                                       Value.getNameInitAsString() +
2306                                       "' of type '" +
2307                                       Value.getType()->getAsString() +
2308                                       "' after resolving references: " +
2309                                       VR->getAsUnquotedString() + "\n");
2310       }
2311     }
2312   }
2313   Init *OldName = getNameInit();
2314   Init *NewName = Name->resolveReferences(R);
2315   if (NewName != OldName) {
2316     // Re-register with RecordKeeper.
2317     setName(NewName);
2318   }
2319 }
2320 
2321 void Record::resolveReferences() {
2322   RecordResolver R(*this);
2323   R.setFinal(true);
2324   resolveReferences(R);
2325 }
2326 
2327 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2328 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
2329 #endif
2330 
2331 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
2332   OS << R.getNameInitAsString();
2333 
2334   ArrayRef<Init *> TArgs = R.getTemplateArgs();
2335   if (!TArgs.empty()) {
2336     OS << "<";
2337     bool NeedComma = false;
2338     for (const Init *TA : TArgs) {
2339       if (NeedComma) OS << ", ";
2340       NeedComma = true;
2341       const RecordVal *RV = R.getValue(TA);
2342       assert(RV && "Template argument record not found??");
2343       RV->print(OS, false);
2344     }
2345     OS << ">";
2346   }
2347 
2348   OS << " {";
2349   ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
2350   if (!SC.empty()) {
2351     OS << "\t//";
2352     for (const auto &SuperPair : SC)
2353       OS << " " << SuperPair.first->getNameInitAsString();
2354   }
2355   OS << "\n";
2356 
2357   for (const RecordVal &Val : R.getValues())
2358     if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
2359       OS << Val;
2360   for (const RecordVal &Val : R.getValues())
2361     if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
2362       OS << Val;
2363 
2364   return OS << "}\n";
2365 }
2366 
2367 SMLoc Record::getFieldLoc(StringRef FieldName) const {
2368   const RecordVal *R = getValue(FieldName);
2369   if (!R)
2370     PrintFatalError(getLoc(), "Record `" + getName() +
2371       "' does not have a field named `" + FieldName + "'!\n");
2372   return R->getLoc();
2373 }
2374 
2375 Init *Record::getValueInit(StringRef FieldName) const {
2376   const RecordVal *R = getValue(FieldName);
2377   if (!R || !R->getValue())
2378     PrintFatalError(getLoc(), "Record `" + getName() +
2379       "' does not have a field named `" + FieldName + "'!\n");
2380   return R->getValue();
2381 }
2382 
2383 StringRef Record::getValueAsString(StringRef FieldName) const {
2384   llvm::Optional<StringRef> S = getValueAsOptionalString(FieldName);
2385   if (!S.hasValue())
2386     PrintFatalError(getLoc(), "Record `" + getName() +
2387       "' does not have a field named `" + FieldName + "'!\n");
2388   return S.getValue();
2389 }
2390 
2391 llvm::Optional<StringRef>
2392 Record::getValueAsOptionalString(StringRef FieldName) const {
2393   const RecordVal *R = getValue(FieldName);
2394   if (!R || !R->getValue())
2395     return llvm::Optional<StringRef>();
2396   if (isa<UnsetInit>(R->getValue()))
2397     return llvm::Optional<StringRef>();
2398 
2399   if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
2400     return SI->getValue();
2401 
2402   PrintFatalError(getLoc(),
2403                   "Record `" + getName() + "', ` field `" + FieldName +
2404                       "' exists but does not have a string initializer!");
2405 }
2406 
2407 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
2408   const RecordVal *R = getValue(FieldName);
2409   if (!R || !R->getValue())
2410     PrintFatalError(getLoc(), "Record `" + getName() +
2411       "' does not have a field named `" + FieldName + "'!\n");
2412 
2413   if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
2414     return BI;
2415   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
2416                                 "' exists but does not have a bits value");
2417 }
2418 
2419 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
2420   const RecordVal *R = getValue(FieldName);
2421   if (!R || !R->getValue())
2422     PrintFatalError(getLoc(), "Record `" + getName() +
2423       "' does not have a field named `" + FieldName + "'!\n");
2424 
2425   if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
2426     return LI;
2427   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
2428                                 "' exists but does not have a list value");
2429 }
2430 
2431 std::vector<Record*>
2432 Record::getValueAsListOfDefs(StringRef FieldName) const {
2433   ListInit *List = getValueAsListInit(FieldName);
2434   std::vector<Record*> Defs;
2435   for (Init *I : List->getValues()) {
2436     if (DefInit *DI = dyn_cast<DefInit>(I))
2437       Defs.push_back(DI->getDef());
2438     else
2439       PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2440         FieldName + "' list is not entirely DefInit!");
2441   }
2442   return Defs;
2443 }
2444 
2445 int64_t Record::getValueAsInt(StringRef FieldName) const {
2446   const RecordVal *R = getValue(FieldName);
2447   if (!R || !R->getValue())
2448     PrintFatalError(getLoc(), "Record `" + getName() +
2449       "' does not have a field named `" + FieldName + "'!\n");
2450 
2451   if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
2452     return II->getValue();
2453   PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2454                                 FieldName +
2455                                 "' exists but does not have an int value: " +
2456                                 R->getValue()->getAsString());
2457 }
2458 
2459 std::vector<int64_t>
2460 Record::getValueAsListOfInts(StringRef FieldName) const {
2461   ListInit *List = getValueAsListInit(FieldName);
2462   std::vector<int64_t> Ints;
2463   for (Init *I : List->getValues()) {
2464     if (IntInit *II = dyn_cast<IntInit>(I))
2465       Ints.push_back(II->getValue());
2466     else
2467       PrintFatalError(getLoc(),
2468                       Twine("Record `") + getName() + "', field `" + FieldName +
2469                           "' exists but does not have a list of ints value: " +
2470                           I->getAsString());
2471   }
2472   return Ints;
2473 }
2474 
2475 std::vector<StringRef>
2476 Record::getValueAsListOfStrings(StringRef FieldName) const {
2477   ListInit *List = getValueAsListInit(FieldName);
2478   std::vector<StringRef> Strings;
2479   for (Init *I : List->getValues()) {
2480     if (StringInit *SI = dyn_cast<StringInit>(I))
2481       Strings.push_back(SI->getValue());
2482     else
2483       PrintFatalError(getLoc(),
2484                       Twine("Record `") + getName() + "', field `" + FieldName +
2485                           "' exists but does not have a list of strings value: " +
2486                           I->getAsString());
2487   }
2488   return Strings;
2489 }
2490 
2491 Record *Record::getValueAsDef(StringRef FieldName) const {
2492   const RecordVal *R = getValue(FieldName);
2493   if (!R || !R->getValue())
2494     PrintFatalError(getLoc(), "Record `" + getName() +
2495       "' does not have a field named `" + FieldName + "'!\n");
2496 
2497   if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2498     return DI->getDef();
2499   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2500     FieldName + "' does not have a def initializer!");
2501 }
2502 
2503 Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
2504   const RecordVal *R = getValue(FieldName);
2505   if (!R || !R->getValue())
2506     PrintFatalError(getLoc(), "Record `" + getName() +
2507       "' does not have a field named `" + FieldName + "'!\n");
2508 
2509   if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2510     return DI->getDef();
2511   if (isa<UnsetInit>(R->getValue()))
2512     return nullptr;
2513   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2514     FieldName + "' does not have either a def initializer or '?'!");
2515 }
2516 
2517 
2518 bool Record::getValueAsBit(StringRef FieldName) const {
2519   const RecordVal *R = getValue(FieldName);
2520   if (!R || !R->getValue())
2521     PrintFatalError(getLoc(), "Record `" + getName() +
2522       "' does not have a field named `" + FieldName + "'!\n");
2523 
2524   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2525     return BI->getValue();
2526   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2527     FieldName + "' does not have a bit initializer!");
2528 }
2529 
2530 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
2531   const RecordVal *R = getValue(FieldName);
2532   if (!R || !R->getValue())
2533     PrintFatalError(getLoc(), "Record `" + getName() +
2534       "' does not have a field named `" + FieldName.str() + "'!\n");
2535 
2536   if (isa<UnsetInit>(R->getValue())) {
2537     Unset = true;
2538     return false;
2539   }
2540   Unset = false;
2541   if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2542     return BI->getValue();
2543   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2544     FieldName + "' does not have a bit initializer!");
2545 }
2546 
2547 DagInit *Record::getValueAsDag(StringRef FieldName) const {
2548   const RecordVal *R = getValue(FieldName);
2549   if (!R || !R->getValue())
2550     PrintFatalError(getLoc(), "Record `" + getName() +
2551       "' does not have a field named `" + FieldName + "'!\n");
2552 
2553   if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
2554     return DI;
2555   PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2556     FieldName + "' does not have a dag initializer!");
2557 }
2558 
2559 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2560 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
2561 #endif
2562 
2563 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
2564   OS << "------------- Classes -----------------\n";
2565   for (const auto &C : RK.getClasses())
2566     OS << "class " << *C.second;
2567 
2568   OS << "------------- Defs -----------------\n";
2569   for (const auto &D : RK.getDefs())
2570     OS << "def " << *D.second;
2571   return OS;
2572 }
2573 
2574 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2575 /// an identifier.
2576 Init *RecordKeeper::getNewAnonymousName() {
2577   return StringInit::get("anonymous_" + utostr(AnonCounter++));
2578 }
2579 
2580 // These functions implement the phase timing facility. Starting a timer
2581 // when one is already running stops the running one.
2582 
2583 void RecordKeeper::startTimer(StringRef Name) {
2584   if (TimingGroup) {
2585     if (LastTimer && LastTimer->isRunning()) {
2586       LastTimer->stopTimer();
2587       if (BackendTimer) {
2588         LastTimer->clear();
2589         BackendTimer = false;
2590       }
2591     }
2592 
2593     LastTimer = new Timer("", Name, *TimingGroup);
2594     LastTimer->startTimer();
2595   }
2596 }
2597 
2598 void RecordKeeper::stopTimer() {
2599   if (TimingGroup) {
2600     assert(LastTimer && "No phase timer was started");
2601     LastTimer->stopTimer();
2602   }
2603 }
2604 
2605 void RecordKeeper::startBackendTimer(StringRef Name) {
2606   if (TimingGroup) {
2607     startTimer(Name);
2608     BackendTimer = true;
2609   }
2610 }
2611 
2612 void RecordKeeper::stopBackendTimer() {
2613   if (TimingGroup) {
2614     if (BackendTimer) {
2615       stopTimer();
2616       BackendTimer = false;
2617     }
2618   }
2619 }
2620 
2621 // We cache the record vectors for single classes. Many backends request
2622 // the same vectors multiple times.
2623 std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
2624     StringRef ClassName) const {
2625 
2626   auto Pair = ClassRecordsMap.try_emplace(ClassName);
2627   if (Pair.second)
2628     Pair.first->second = getAllDerivedDefinitions(makeArrayRef(ClassName));
2629 
2630   return Pair.first->second;
2631 }
2632 
2633 std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
2634     ArrayRef<StringRef> ClassNames) const {
2635   SmallVector<Record *, 2> ClassRecs;
2636   std::vector<Record *> Defs;
2637 
2638   assert(ClassNames.size() > 0 && "At least one class must be passed.");
2639   for (const auto &ClassName : ClassNames) {
2640     Record *Class = getClass(ClassName);
2641     if (!Class)
2642       PrintFatalError("The class '" + ClassName + "' is not defined\n");
2643     ClassRecs.push_back(Class);
2644   }
2645 
2646   for (const auto &OneDef : getDefs()) {
2647     if (all_of(ClassRecs, [&OneDef](const Record *Class) {
2648                             return OneDef.second->isSubClassOf(Class);
2649                           }))
2650       Defs.push_back(OneDef.second.get());
2651   }
2652 
2653   return Defs;
2654 }
2655 
2656 Init *MapResolver::resolve(Init *VarName) {
2657   auto It = Map.find(VarName);
2658   if (It == Map.end())
2659     return nullptr;
2660 
2661   Init *I = It->second.V;
2662 
2663   if (!It->second.Resolved && Map.size() > 1) {
2664     // Resolve mutual references among the mapped variables, but prevent
2665     // infinite recursion.
2666     Map.erase(It);
2667     I = I->resolveReferences(*this);
2668     Map[VarName] = {I, true};
2669   }
2670 
2671   return I;
2672 }
2673 
2674 Init *RecordResolver::resolve(Init *VarName) {
2675   Init *Val = Cache.lookup(VarName);
2676   if (Val)
2677     return Val;
2678 
2679   for (Init *S : Stack) {
2680     if (S == VarName)
2681       return nullptr; // prevent infinite recursion
2682   }
2683 
2684   if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
2685     if (!isa<UnsetInit>(RV->getValue())) {
2686       Val = RV->getValue();
2687       Stack.push_back(VarName);
2688       Val = Val->resolveReferences(*this);
2689       Stack.pop_back();
2690     }
2691   }
2692 
2693   Cache[VarName] = Val;
2694   return Val;
2695 }
2696 
2697 Init *TrackUnresolvedResolver::resolve(Init *VarName) {
2698   Init *I = nullptr;
2699 
2700   if (R) {
2701     I = R->resolve(VarName);
2702     if (I && !FoundUnresolved) {
2703       // Do not recurse into the resolved initializer, as that would change
2704       // the behavior of the resolver we're delegating, but do check to see
2705       // if there are unresolved variables remaining.
2706       TrackUnresolvedResolver Sub;
2707       I->resolveReferences(Sub);
2708       FoundUnresolved |= Sub.FoundUnresolved;
2709     }
2710   }
2711 
2712   if (!I)
2713     FoundUnresolved = true;
2714   return I;
2715 }
2716 
2717 Init *HasReferenceResolver::resolve(Init *VarName)
2718 {
2719   if (VarName == VarNameToTrack)
2720     Found = true;
2721   return nullptr;
2722 }
2723