Line data Source code
1 : //===- Record.cpp - Record implementation ---------------------------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // Implement the tablegen record classes.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "llvm/ADT/ArrayRef.h"
15 : #include "llvm/ADT/DenseMap.h"
16 : #include "llvm/ADT/FoldingSet.h"
17 : #include "llvm/ADT/SmallString.h"
18 : #include "llvm/ADT/SmallVector.h"
19 : #include "llvm/ADT/StringExtras.h"
20 : #include "llvm/ADT/StringMap.h"
21 : #include "llvm/ADT/StringRef.h"
22 : #include "llvm/Config/llvm-config.h"
23 : #include "llvm/Support/Allocator.h"
24 : #include "llvm/Support/Casting.h"
25 : #include "llvm/Support/Compiler.h"
26 : #include "llvm/Support/ErrorHandling.h"
27 : #include "llvm/Support/SMLoc.h"
28 : #include "llvm/Support/raw_ostream.h"
29 : #include "llvm/TableGen/Error.h"
30 : #include "llvm/TableGen/Record.h"
31 : #include <cassert>
32 : #include <cstdint>
33 : #include <memory>
34 : #include <string>
35 : #include <utility>
36 : #include <vector>
37 :
38 : using namespace llvm;
39 :
40 : static BumpPtrAllocator Allocator;
41 :
42 : //===----------------------------------------------------------------------===//
43 : // Type implementations
44 : //===----------------------------------------------------------------------===//
45 :
46 : BitRecTy BitRecTy::Shared;
47 : CodeRecTy CodeRecTy::Shared;
48 : IntRecTy IntRecTy::Shared;
49 : StringRecTy StringRecTy::Shared;
50 : DagRecTy DagRecTy::Shared;
51 :
52 : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
53 : LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
54 : #endif
55 :
56 1270742 : ListRecTy *RecTy::getListTy() {
57 1270742 : if (!ListTy)
58 14170 : ListTy = new(Allocator) ListRecTy(this);
59 1270742 : return ListTy;
60 : }
61 :
62 62633 : bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
63 : assert(RHS && "NULL pointer");
64 62633 : return Kind == RHS->getRecTyKind();
65 : }
66 :
67 1107380 : bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
68 :
69 2728 : bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
70 2728 : if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
71 : return true;
72 : if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
73 15 : return BitsTy->getNumBits() == 1;
74 : return false;
75 : }
76 :
77 1130458 : BitsRecTy *BitsRecTy::get(unsigned Sz) {
78 1130458 : static std::vector<BitsRecTy*> Shared;
79 2260916 : if (Sz >= Shared.size())
80 603 : Shared.resize(Sz + 1);
81 1130458 : BitsRecTy *&Ty = Shared[Sz];
82 1130458 : if (!Ty)
83 2954 : Ty = new(Allocator) BitsRecTy(Sz);
84 1130458 : return Ty;
85 : }
86 :
87 250 : std::string BitsRecTy::getAsString() const {
88 500 : return "bits<" + utostr(Size) + ">";
89 : }
90 :
91 3356 : bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
92 3356 : if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
93 11 : return cast<BitsRecTy>(RHS)->Size == Size;
94 3345 : RecTyKind kind = RHS->getRecTyKind();
95 3345 : return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
96 : }
97 :
98 27810 : bool BitsRecTy::typeIsA(const RecTy *RHS) const {
99 : if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
100 8 : return RHSb->Size == Size;
101 : return false;
102 : }
103 :
104 362022 : bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
105 362022 : RecTyKind kind = RHS->getRecTyKind();
106 362022 : return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
107 : }
108 :
109 143 : bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
110 143 : RecTyKind Kind = RHS->getRecTyKind();
111 143 : return Kind == CodeRecTyKind || Kind == StringRecTyKind;
112 : }
113 :
114 516 : std::string StringRecTy::getAsString() const {
115 516 : return "string";
116 : }
117 :
118 59850 : bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
119 59850 : RecTyKind Kind = RHS->getRecTyKind();
120 59850 : return Kind == StringRecTyKind || Kind == CodeRecTyKind;
121 : }
122 :
123 248 : std::string ListRecTy::getAsString() const {
124 496 : return "list<" + Ty->getAsString() + ">";
125 : }
126 :
127 164410 : bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
128 : if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
129 164410 : return Ty->typeIsConvertibleTo(ListTy->getElementType());
130 : return false;
131 : }
132 :
133 3154909 : bool ListRecTy::typeIsA(const RecTy *RHS) const {
134 : if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
135 3154909 : return getElementType()->typeIsA(RHSl->getElementType());
136 : return false;
137 : }
138 :
139 178 : std::string DagRecTy::getAsString() const {
140 178 : return "dag";
141 : }
142 :
143 3844850 : static void ProfileRecordRecTy(FoldingSetNodeID &ID,
144 : ArrayRef<Record *> Classes) {
145 3844850 : ID.AddInteger(Classes.size());
146 8858871 : for (Record *R : Classes)
147 5014021 : ID.AddPointer(R);
148 3844850 : }
149 :
150 1651259 : RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) {
151 1651259 : if (UnsortedClasses.empty()) {
152 2506 : static RecordRecTy AnyRecord(0);
153 2506 : return &AnyRecord;
154 : }
155 :
156 : FoldingSet<RecordRecTy> &ThePool =
157 1648753 : UnsortedClasses[0]->getRecords().RecordTypePool;
158 :
159 : SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
160 : UnsortedClasses.end());
161 : llvm::sort(Classes, [](Record *LHS, Record *RHS) {
162 : return LHS->getNameInitAsString() < RHS->getNameInitAsString();
163 : });
164 :
165 : FoldingSetNodeID ID;
166 1648753 : ProfileRecordRecTy(ID, Classes);
167 :
168 1648753 : void *IP = nullptr;
169 1580013 : if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
170 : return Ty;
171 :
172 : #ifndef NDEBUG
173 : // Check for redundancy.
174 : for (unsigned i = 0; i < Classes.size(); ++i) {
175 : for (unsigned j = 0; j < Classes.size(); ++j) {
176 : assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
177 : }
178 : assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
179 : }
180 : #endif
181 :
182 137480 : void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()),
183 : alignof(RecordRecTy));
184 68740 : RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
185 : std::uninitialized_copy(Classes.begin(), Classes.end(),
186 : Ty->getTrailingObjects<Record *>());
187 68740 : ThePool.InsertNode(Ty, IP);
188 68740 : return Ty;
189 : }
190 :
191 2196097 : void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
192 2196097 : ProfileRecordRecTy(ID, getClasses());
193 2196097 : }
194 :
195 127 : std::string RecordRecTy::getAsString() const {
196 127 : if (NumClasses == 1)
197 125 : return getClasses()[0]->getNameInitAsString();
198 :
199 2 : std::string Str = "{";
200 : bool First = true;
201 2 : for (Record *R : getClasses()) {
202 0 : if (!First)
203 : Str += ", ";
204 : First = false;
205 0 : Str += R->getNameInitAsString();
206 : }
207 : Str += "}";
208 : return Str;
209 : }
210 :
211 7704059 : bool RecordRecTy::isSubClassOf(Record *Class) const {
212 : return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
213 0 : return MySuperClass == Class ||
214 : MySuperClass->isSubClassOf(Class);
215 7704059 : });
216 : }
217 :
218 10636093 : bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
219 10636093 : if (this == RHS)
220 : return true;
221 :
222 : const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
223 : if (!RTy)
224 : return false;
225 :
226 : return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
227 0 : return isSubClassOf(TargetClass);
228 : });
229 : }
230 :
231 6741791 : bool RecordRecTy::typeIsA(const RecTy *RHS) const {
232 6741791 : return typeIsConvertibleTo(RHS);
233 : }
234 :
235 235713 : static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
236 : SmallVector<Record *, 4> CommonSuperClasses;
237 : SmallVector<Record *, 4> Stack;
238 :
239 235713 : Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end());
240 :
241 667497 : while (!Stack.empty()) {
242 431784 : Record *R = Stack.back();
243 : Stack.pop_back();
244 :
245 431784 : if (T2->isSubClassOf(R)) {
246 240897 : CommonSuperClasses.push_back(R);
247 : } else {
248 190887 : R->getDirectSuperClasses(Stack);
249 : }
250 : }
251 :
252 235713 : return RecordRecTy::get(CommonSuperClasses);
253 : }
254 :
255 2190248 : RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
256 2190248 : if (T1 == T2)
257 : return T1;
258 :
259 : if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
260 : if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
261 235713 : return resolveRecordTypes(RecTy1, RecTy2);
262 : }
263 :
264 4416 : if (T1->typeIsConvertibleTo(T2))
265 : return T2;
266 602 : if (T2->typeIsConvertibleTo(T1))
267 : return T1;
268 :
269 : if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
270 : if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
271 0 : RecTy* NewType = resolveTypes(ListTy1->getElementType(),
272 : ListTy2->getElementType());
273 0 : if (NewType)
274 0 : return NewType->getListTy();
275 : }
276 : }
277 :
278 : return nullptr;
279 : }
280 :
281 : //===----------------------------------------------------------------------===//
282 : // Initializer implementations
283 : //===----------------------------------------------------------------------===//
284 :
285 0 : void Init::anchor() {}
286 :
287 : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
288 : LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
289 : #endif
290 :
291 1032694 : UnsetInit *UnsetInit::get() {
292 1032694 : static UnsetInit TheInit;
293 1032694 : return &TheInit;
294 : }
295 :
296 11829417 : Init *UnsetInit::getCastTo(RecTy *Ty) const {
297 11829417 : return const_cast<UnsetInit *>(this);
298 : }
299 :
300 23820 : Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
301 23820 : return const_cast<UnsetInit *>(this);
302 : }
303 :
304 12112811 : BitInit *BitInit::get(bool V) {
305 12112811 : static BitInit True(true);
306 12112811 : static BitInit False(false);
307 :
308 12112811 : return V ? &True : &False;
309 : }
310 :
311 3133206 : Init *BitInit::convertInitializerTo(RecTy *Ty) const {
312 3133206 : if (isa<BitRecTy>(Ty))
313 2175 : return const_cast<BitInit *>(this);
314 :
315 3131031 : if (isa<IntRecTy>(Ty))
316 3131031 : return IntInit::get(getValue());
317 :
318 : if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
319 : // Can only convert single bit.
320 0 : if (BRT->getNumBits() == 1)
321 0 : return BitsInit::get(const_cast<BitInit *>(this));
322 : }
323 :
324 : return nullptr;
325 : }
326 :
327 : static void
328 22010063 : ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
329 22010063 : ID.AddInteger(Range.size());
330 :
331 505646599 : for (Init *I : Range)
332 483636536 : ID.AddPointer(I);
333 22010063 : }
334 :
335 7683084 : BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
336 7683299 : static FoldingSet<BitsInit> ThePool;
337 :
338 : FoldingSetNodeID ID;
339 7683084 : ProfileBitsInit(ID, Range);
340 :
341 7683084 : void *IP = nullptr;
342 6880109 : if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
343 : return I;
344 :
345 802975 : void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
346 : alignof(BitsInit));
347 802975 : BitsInit *I = new(Mem) BitsInit(Range.size());
348 : std::uninitialized_copy(Range.begin(), Range.end(),
349 : I->getTrailingObjects<Init *>());
350 802975 : ThePool.InsertNode(I, IP);
351 802975 : return I;
352 : }
353 :
354 14326979 : void BitsInit::Profile(FoldingSetNodeID &ID) const {
355 28653958 : ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
356 14326979 : }
357 :
358 2613358 : Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
359 2613358 : if (isa<BitRecTy>(Ty)) {
360 1393 : if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
361 1393 : return getBit(0);
362 : }
363 :
364 : if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
365 : // If the number of bits is right, return it. Otherwise we need to expand
366 : // or truncate.
367 147818 : if (getNumBits() != BRT->getNumBits()) return nullptr;
368 147810 : return const_cast<BitsInit *>(this);
369 : }
370 :
371 2464147 : if (isa<IntRecTy>(Ty)) {
372 : int64_t Result = 0;
373 7848671 : for (unsigned i = 0, e = getNumBits(); i != e; ++i)
374 : if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
375 5384524 : Result |= static_cast<int64_t>(Bit->getValue()) << i;
376 : else
377 : return nullptr;
378 2462792 : return IntInit::get(Result);
379 : }
380 :
381 : return nullptr;
382 : }
383 :
384 : Init *
385 0 : BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
386 0 : SmallVector<Init *, 16> NewBits(Bits.size());
387 :
388 0 : for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
389 0 : if (Bits[i] >= getNumBits())
390 : return nullptr;
391 0 : NewBits[i] = getBit(Bits[i]);
392 : }
393 0 : return BitsInit::get(NewBits);
394 : }
395 :
396 12187 : bool BitsInit::isConcrete() const {
397 67586 : for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
398 55399 : if (!getBit(i)->isConcrete())
399 : return false;
400 : }
401 : return true;
402 : }
403 :
404 1150 : std::string BitsInit::getAsString() const {
405 1150 : std::string Result = "{ ";
406 6122 : for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
407 4972 : if (i) Result += ", ";
408 9944 : if (Init *Bit = getBit(e-i-1))
409 9944 : Result += Bit->getAsString();
410 : else
411 : Result += "*";
412 : }
413 1150 : return Result + " }";
414 : }
415 :
416 : // resolveReferences - If there are any field references that refer to fields
417 : // that have been filled in, we can propagate the values now.
418 31139611 : Init *BitsInit::resolveReferences(Resolver &R) const {
419 : bool Changed = false;
420 31139611 : SmallVector<Init *, 16> NewBits(getNumBits());
421 :
422 : Init *CachedBitVarRef = nullptr;
423 : Init *CachedBitVarResolved = nullptr;
424 :
425 322580399 : for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
426 : Init *CurBit = getBit(i);
427 : Init *NewBit = CurBit;
428 :
429 : if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
430 115068335 : if (CurBitVar->getBitVar() != CachedBitVarRef) {
431 : CachedBitVarRef = CurBitVar->getBitVar();
432 29286976 : CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
433 : }
434 :
435 115068335 : NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
436 : } else {
437 : // getBit(0) implicitly converts int and bits<1> values to bit.
438 176372453 : NewBit = CurBit->resolveReferences(R)->getBit(0);
439 : }
440 :
441 291440788 : if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
442 : NewBit = CurBit;
443 291440788 : NewBits[i] = NewBit;
444 291440788 : Changed |= CurBit != NewBit;
445 : }
446 :
447 31139611 : if (Changed)
448 5844714 : return BitsInit::get(NewBits);
449 :
450 25294897 : return const_cast<BitsInit *>(this);
451 : }
452 :
453 9961560 : IntInit *IntInit::get(int64_t V) {
454 9961883 : static DenseMap<int64_t, IntInit*> ThePool;
455 :
456 : IntInit *&I = ThePool[V];
457 10101302 : if (!I) I = new(Allocator) IntInit(V);
458 9961560 : return I;
459 : }
460 :
461 4497415 : std::string IntInit::getAsString() const {
462 4497415 : return itostr(Value);
463 : }
464 :
465 : static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
466 : // For example, with NumBits == 4, we permit Values from [-7 .. 15].
467 1135277 : return (NumBits >= sizeof(Value) * 8) ||
468 1135277 : (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
469 : }
470 :
471 6189485 : Init *IntInit::convertInitializerTo(RecTy *Ty) const {
472 6189485 : if (isa<IntRecTy>(Ty))
473 4356500 : return const_cast<IntInit *>(this);
474 :
475 1832985 : if (isa<BitRecTy>(Ty)) {
476 697429 : int64_t Val = getValue();
477 697429 : if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
478 697426 : return BitInit::get(Val != 0);
479 : }
480 :
481 : if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
482 1135556 : int64_t Value = getValue();
483 : // Make sure this bitfield is large enough to hold the integer value.
484 1135556 : if (!canFitInBitfield(Value, BRT->getNumBits()))
485 : return nullptr;
486 :
487 1135553 : SmallVector<Init *, 16> NewBits(BRT->getNumBits());
488 8284169 : for (unsigned i = 0; i != BRT->getNumBits(); ++i)
489 7148616 : NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0));
490 :
491 1135553 : return BitsInit::get(NewBits);
492 : }
493 :
494 : return nullptr;
495 : }
496 :
497 : Init *
498 0 : IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
499 0 : SmallVector<Init *, 16> NewBits(Bits.size());
500 :
501 0 : for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
502 0 : if (Bits[i] >= 64)
503 : return nullptr;
504 :
505 0 : NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
506 : }
507 0 : return BitsInit::get(NewBits);
508 : }
509 :
510 21770 : CodeInit *CodeInit::get(StringRef V) {
511 21770 : static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
512 :
513 21770 : auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
514 21770 : if (!Entry.second)
515 17475 : Entry.second = new(Allocator) CodeInit(Entry.getKey());
516 21770 : return Entry.second;
517 : }
518 :
519 123607674 : StringInit *StringInit::get(StringRef V) {
520 123607674 : static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
521 :
522 123607674 : auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
523 123607674 : if (!Entry.second)
524 7922508 : Entry.second = new(Allocator) StringInit(Entry.getKey());
525 123607674 : return Entry.second;
526 : }
527 :
528 436847 : Init *StringInit::convertInitializerTo(RecTy *Ty) const {
529 436847 : if (isa<StringRecTy>(Ty))
530 0 : return const_cast<StringInit *>(this);
531 436847 : if (isa<CodeRecTy>(Ty))
532 1040 : return CodeInit::get(getValue());
533 :
534 : return nullptr;
535 : }
536 :
537 60 : Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
538 60 : if (isa<CodeRecTy>(Ty))
539 0 : return const_cast<CodeInit *>(this);
540 60 : if (isa<StringRecTy>(Ty))
541 60 : return StringInit::get(getValue());
542 :
543 : return nullptr;
544 : }
545 :
546 18098855 : static void ProfileListInit(FoldingSetNodeID &ID,
547 : ArrayRef<Init *> Range,
548 : RecTy *EltTy) {
549 18098855 : ID.AddInteger(Range.size());
550 18098855 : ID.AddPointer(EltTy);
551 :
552 62914202 : for (Init *I : Range)
553 44815347 : ID.AddPointer(I);
554 18098855 : }
555 :
556 6388959 : ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
557 6389242 : static FoldingSet<ListInit> ThePool;
558 :
559 : FoldingSetNodeID ID;
560 6388959 : ProfileListInit(ID, Range, EltTy);
561 :
562 6388959 : void *IP = nullptr;
563 5183701 : if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
564 : return I;
565 :
566 : assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
567 : cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
568 :
569 1205258 : void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
570 : alignof(ListInit));
571 1205258 : ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
572 : std::uninitialized_copy(Range.begin(), Range.end(),
573 : I->getTrailingObjects<Init *>());
574 1205258 : ThePool.InsertNode(I, IP);
575 1205258 : return I;
576 : }
577 :
578 11709896 : void ListInit::Profile(FoldingSetNodeID &ID) const {
579 11709896 : RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
580 :
581 11709896 : ProfileListInit(ID, getValues(), EltTy);
582 11709896 : }
583 :
584 26 : Init *ListInit::convertInitializerTo(RecTy *Ty) const {
585 26 : if (getType() == Ty)
586 0 : return const_cast<ListInit*>(this);
587 :
588 : if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
589 : SmallVector<Init*, 8> Elements;
590 : Elements.reserve(getValues().size());
591 :
592 : // Verify that all of the elements of the list are subclasses of the
593 : // appropriate class!
594 : bool Changed = false;
595 26 : RecTy *ElementType = LRT->getElementType();
596 91 : for (Init *I : getValues())
597 65 : if (Init *CI = I->convertInitializerTo(ElementType)) {
598 65 : Elements.push_back(CI);
599 65 : if (CI != I)
600 : Changed = true;
601 : } else
602 0 : return nullptr;
603 :
604 26 : if (!Changed)
605 0 : return const_cast<ListInit*>(this);
606 26 : return ListInit::get(Elements, ElementType);
607 : }
608 :
609 : return nullptr;
610 : }
611 :
612 1118 : Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
613 : SmallVector<Init*, 8> Vals;
614 : Vals.reserve(Elements.size());
615 11930 : for (unsigned Element : Elements) {
616 10812 : if (Element >= size())
617 : return nullptr;
618 10812 : Vals.push_back(getElement(Element));
619 : }
620 1118 : return ListInit::get(Vals, getElementType());
621 : }
622 :
623 2014601 : Record *ListInit::getElementAsRecord(unsigned i) const {
624 : assert(i < NumValues && "List element index out of range!");
625 : DefInit *DI = dyn_cast<DefInit>(getElement(i));
626 : if (!DI)
627 0 : PrintFatalError("Expected record in list!");
628 2014601 : return DI->getDef();
629 : }
630 :
631 29709601 : Init *ListInit::resolveReferences(Resolver &R) const {
632 : SmallVector<Init*, 8> Resolved;
633 29709601 : Resolved.reserve(size());
634 : bool Changed = false;
635 :
636 62134344 : for (Init *CurElt : getValues()) {
637 32424743 : Init *E = CurElt->resolveReferences(R);
638 32424743 : Changed |= E != CurElt;
639 32424743 : Resolved.push_back(E);
640 : }
641 :
642 29709601 : if (Changed)
643 1496999 : return ListInit::get(Resolved, getElementType());
644 28212602 : return const_cast<ListInit *>(this);
645 : }
646 :
647 10223267 : bool ListInit::isConcrete() const {
648 20782060 : for (Init *Element : *this) {
649 10558793 : if (!Element->isConcrete())
650 : return false;
651 : }
652 : return true;
653 : }
654 :
655 131 : std::string ListInit::getAsString() const {
656 131 : std::string Result = "[";
657 : const char *sep = "";
658 425 : for (Init *Element : *this) {
659 : Result += sep;
660 : sep = ", ";
661 588 : Result += Element->getAsString();
662 : }
663 131 : return Result + "]";
664 : }
665 :
666 8103102 : Init *OpInit::getBit(unsigned Bit) const {
667 8103102 : if (getType() == BitRecTy::get())
668 211 : return const_cast<OpInit*>(this);
669 8102891 : return VarBitInit::get(const_cast<OpInit*>(this), Bit);
670 : }
671 :
672 : static void
673 10116671 : ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
674 10116671 : ID.AddInteger(Opcode);
675 10116671 : ID.AddPointer(Op);
676 10116671 : ID.AddPointer(Type);
677 10116671 : }
678 :
679 3967195 : UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
680 3967433 : static FoldingSet<UnOpInit> ThePool;
681 :
682 : FoldingSetNodeID ID;
683 3967195 : ProfileUnOpInit(ID, Opc, LHS, Type);
684 :
685 3967195 : void *IP = nullptr;
686 3567557 : if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
687 : return I;
688 :
689 : UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
690 399638 : ThePool.InsertNode(I, IP);
691 399638 : return I;
692 : }
693 :
694 6149476 : void UnOpInit::Profile(FoldingSetNodeID &ID) const {
695 6149476 : ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
696 6149476 : }
697 :
698 3966685 : Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
699 3966685 : switch (getOpcode()) {
700 3724032 : case CAST:
701 7448064 : if (isa<StringRecTy>(getType())) {
702 254103 : if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
703 : return LHSs;
704 :
705 : if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
706 69220 : return StringInit::get(LHSd->getAsString());
707 :
708 : if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
709 166267 : return StringInit::get(LHSi->getAsString());
710 3469929 : } else if (isa<RecordRecTy>(getType())) {
711 2001049 : if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
712 1896262 : if (!CurRec && !IsFinal)
713 : break;
714 : assert(CurRec && "NULL pointer");
715 : Record *D;
716 :
717 : // Self-references are allowed, but their resolution is delayed until
718 : // the final resolve to ensure that we get the correct type for them.
719 1896260 : if (Name == CurRec->getNameInit()) {
720 109838 : if (!IsFinal)
721 : break;
722 : D = CurRec;
723 : } else {
724 1786422 : D = CurRec->getRecords().getDef(Name->getValue());
725 1786422 : if (!D) {
726 234737 : if (IsFinal)
727 1 : PrintFatalError(CurRec->getLoc(),
728 : Twine("Undefined reference to record: '") +
729 1 : Name->getValue() + "'\n");
730 : break;
731 : }
732 : }
733 :
734 1659969 : DefInit *DI = DefInit::get(D);
735 1659969 : if (!DI->getType()->typeIsA(getType())) {
736 1 : PrintFatalError(CurRec->getLoc(),
737 : Twine("Expected type '") +
738 1 : getType()->getAsString() + "', got '" +
739 1 : DI->getType()->getAsString() + "' in: " +
740 1 : getAsString() + "\n");
741 : }
742 : return DI;
743 : }
744 : }
745 :
746 1595853 : if (Init *NewInit = LHS->convertInitializerTo(getType()))
747 120263 : return NewInit;
748 : break;
749 :
750 2472 : case HEAD:
751 2472 : if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
752 : assert(!LHSl->empty() && "Empty list in head");
753 2233 : return LHSl->getElement(0);
754 : }
755 : break;
756 :
757 4472 : case TAIL:
758 4472 : if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
759 : assert(!LHSl->empty() && "Empty list in tail");
760 : // Note the +1. We can't just pass the result of getValues()
761 : // directly.
762 4210 : return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
763 : }
764 : break;
765 :
766 199815 : case SIZE:
767 199815 : if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
768 180523 : return IntInit::get(LHSl->size());
769 : break;
770 :
771 35894 : case EMPTY:
772 35894 : if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
773 71066 : return IntInit::get(LHSl->empty());
774 : if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
775 0 : return IntInit::get(LHSs->getValue().empty());
776 : break;
777 : }
778 376709 : return const_cast<UnOpInit *>(this);
779 : }
780 :
781 5695981 : Init *UnOpInit::resolveReferences(Resolver &R) const {
782 5695981 : Init *lhs = LHS->resolveReferences(R);
783 :
784 5695981 : if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
785 3923327 : return (UnOpInit::get(getOpcode(), lhs, getType()))
786 3923327 : ->Fold(R.getCurrentRecord(), R.isFinal());
787 1772654 : return const_cast<UnOpInit *>(this);
788 : }
789 :
790 82 : std::string UnOpInit::getAsString() const {
791 : std::string Result;
792 82 : switch (getOpcode()) {
793 136 : case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
794 : case HEAD: Result = "!head"; break;
795 : case TAIL: Result = "!tail"; break;
796 : case SIZE: Result = "!size"; break;
797 : case EMPTY: Result = "!empty"; break;
798 : }
799 165 : return Result + "(" + LHS->getAsString() + ")";
800 : }
801 :
802 : static void
803 29247772 : ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
804 : RecTy *Type) {
805 29247772 : ID.AddInteger(Opcode);
806 29247772 : ID.AddPointer(LHS);
807 29247772 : ID.AddPointer(RHS);
808 29247772 : ID.AddPointer(Type);
809 29247772 : }
810 :
811 11146454 : BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
812 : Init *RHS, RecTy *Type) {
813 11146702 : static FoldingSet<BinOpInit> ThePool;
814 :
815 : FoldingSetNodeID ID;
816 11146454 : ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
817 :
818 11146454 : void *IP = nullptr;
819 8349158 : if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
820 : return I;
821 :
822 : BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
823 2797296 : ThePool.InsertNode(I, IP);
824 2797296 : return I;
825 : }
826 :
827 18101318 : void BinOpInit::Profile(FoldingSetNodeID &ID) const {
828 18101318 : ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
829 18101318 : }
830 :
831 17578841 : static StringInit *ConcatStringInits(const StringInit *I0,
832 : const StringInit *I1) {
833 : SmallString<80> Concat(I0->getValue());
834 : Concat.append(I1->getValue());
835 17578841 : return StringInit::get(Concat);
836 : }
837 :
838 12195650 : Init *BinOpInit::getStrConcat(Init *I0, Init *I1) {
839 : // Shortcut for the common case of concatenating two strings.
840 : if (const StringInit *I0s = dyn_cast<StringInit>(I0))
841 : if (const StringInit *I1s = dyn_cast<StringInit>(I1))
842 12035005 : return ConcatStringInits(I0s, I1s);
843 160645 : return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
844 : }
845 :
846 10985809 : Init *BinOpInit::Fold(Record *CurRec) const {
847 10985809 : switch (getOpcode()) {
848 75213 : case CONCAT: {
849 75213 : DagInit *LHSs = dyn_cast<DagInit>(LHS);
850 75213 : DagInit *RHSs = dyn_cast<DagInit>(RHS);
851 75213 : if (LHSs && RHSs) {
852 60114 : DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
853 60114 : DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
854 60114 : if (!LOp || !ROp)
855 : break;
856 60113 : if (LOp->getDef() != ROp->getDef()) {
857 0 : PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
858 0 : LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
859 : "'");
860 : }
861 : SmallVector<Init*, 8> Args;
862 : SmallVector<StringInit*, 8> ArgNames;
863 385610 : for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
864 325497 : Args.push_back(LHSs->getArg(i));
865 325497 : ArgNames.push_back(LHSs->getArgName(i));
866 : }
867 138551 : for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
868 78438 : Args.push_back(RHSs->getArg(i));
869 78438 : ArgNames.push_back(RHSs->getArgName(i));
870 : }
871 60113 : return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
872 : }
873 : break;
874 : }
875 1583015 : case LISTCONCAT: {
876 1583015 : ListInit *LHSs = dyn_cast<ListInit>(LHS);
877 1583015 : ListInit *RHSs = dyn_cast<ListInit>(RHS);
878 1583015 : if (LHSs && RHSs) {
879 : SmallVector<Init *, 8> Args;
880 1405739 : Args.insert(Args.end(), LHSs->begin(), LHSs->end());
881 1405739 : Args.insert(Args.end(), RHSs->begin(), RHSs->end());
882 1405739 : return ListInit::get(Args, LHSs->getElementType());
883 : }
884 : break;
885 : }
886 6360939 : case STRCONCAT: {
887 6360939 : StringInit *LHSs = dyn_cast<StringInit>(LHS);
888 6360939 : StringInit *RHSs = dyn_cast<StringInit>(RHS);
889 6360939 : if (LHSs && RHSs)
890 5543836 : return ConcatStringInits(LHSs, RHSs);
891 : break;
892 : }
893 880061 : case EQ:
894 : case NE:
895 : case LE:
896 : case LT:
897 : case GE:
898 : case GT: {
899 : // try to fold eq comparison for 'bit' and 'int', otherwise fallback
900 : // to string objects.
901 : IntInit *L =
902 880061 : dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
903 : IntInit *R =
904 880061 : dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
905 :
906 880061 : if (L && R) {
907 : bool Result;
908 641893 : switch (getOpcode()) {
909 628391 : case EQ: Result = L->getValue() == R->getValue(); break;
910 696 : case NE: Result = L->getValue() != R->getValue(); break;
911 12784 : case LE: Result = L->getValue() <= R->getValue(); break;
912 14 : case LT: Result = L->getValue() < R->getValue(); break;
913 4 : case GE: Result = L->getValue() >= R->getValue(); break;
914 4 : case GT: Result = L->getValue() > R->getValue(); break;
915 0 : default: llvm_unreachable("unhandled comparison");
916 : }
917 641893 : return BitInit::get(Result);
918 : }
919 :
920 238168 : if (getOpcode() == EQ || getOpcode() == NE) {
921 235564 : StringInit *LHSs = dyn_cast<StringInit>(LHS);
922 235564 : StringInit *RHSs = dyn_cast<StringInit>(RHS);
923 :
924 : // Make sure we've resolved
925 235564 : if (LHSs && RHSs) {
926 : bool Equal = LHSs->getValue() == RHSs->getValue();
927 208875 : return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
928 : }
929 : }
930 :
931 : break;
932 : }
933 2086581 : case ADD:
934 : case AND:
935 : case OR:
936 : case SHL:
937 : case SRA:
938 : case SRL: {
939 : IntInit *LHSi =
940 2086581 : dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
941 : IntInit *RHSi =
942 2086581 : dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
943 2086581 : if (LHSi && RHSi) {
944 2004870 : int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
945 : int64_t Result;
946 2004870 : switch (getOpcode()) {
947 0 : default: llvm_unreachable("Bad opcode!");
948 581115 : case ADD: Result = LHSv + RHSv; break;
949 45591 : case AND: Result = LHSv & RHSv; break;
950 801519 : case OR: Result = LHSv | RHSv; break;
951 323756 : case SHL: Result = LHSv << RHSv; break;
952 1 : case SRA: Result = LHSv >> RHSv; break;
953 252888 : case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
954 : }
955 2004870 : return IntInit::get(Result);
956 : }
957 : break;
958 : }
959 : }
960 1120483 : return const_cast<BinOpInit *>(this);
961 : }
962 :
963 16829246 : Init *BinOpInit::resolveReferences(Resolver &R) const {
964 16829246 : Init *lhs = LHS->resolveReferences(R);
965 16829246 : Init *rhs = RHS->resolveReferences(R);
966 :
967 16829246 : if (LHS != lhs || RHS != rhs)
968 10941078 : return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
969 10941078 : ->Fold(R.getCurrentRecord());
970 5888168 : return const_cast<BinOpInit *>(this);
971 : }
972 :
973 103 : std::string BinOpInit::getAsString() const {
974 : std::string Result;
975 103 : switch (getOpcode()) {
976 : case CONCAT: Result = "!con"; break;
977 : case ADD: Result = "!add"; break;
978 : case AND: Result = "!and"; break;
979 : case OR: Result = "!or"; break;
980 : case SHL: Result = "!shl"; break;
981 : case SRA: Result = "!sra"; break;
982 : case SRL: Result = "!srl"; break;
983 : case EQ: Result = "!eq"; break;
984 : case NE: Result = "!ne"; break;
985 : case LE: Result = "!le"; break;
986 : case LT: Result = "!lt"; break;
987 : case GE: Result = "!ge"; break;
988 : case GT: Result = "!gt"; break;
989 : case LISTCONCAT: Result = "!listconcat"; break;
990 : case STRCONCAT: Result = "!strconcat"; break;
991 : }
992 216 : return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
993 : }
994 :
995 : static void
996 3604587 : ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
997 : Init *RHS, RecTy *Type) {
998 3604587 : ID.AddInteger(Opcode);
999 3604587 : ID.AddPointer(LHS);
1000 3604587 : ID.AddPointer(MHS);
1001 3604587 : ID.AddPointer(RHS);
1002 3604587 : ID.AddPointer(Type);
1003 3604587 : }
1004 :
1005 1296017 : TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
1006 : RecTy *Type) {
1007 1296233 : static FoldingSet<TernOpInit> ThePool;
1008 :
1009 : FoldingSetNodeID ID;
1010 1296017 : ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
1011 :
1012 1296017 : void *IP = nullptr;
1013 1054278 : if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1014 : return I;
1015 :
1016 : TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
1017 241739 : ThePool.InsertNode(I, IP);
1018 241739 : return I;
1019 : }
1020 :
1021 2308570 : void TernOpInit::Profile(FoldingSetNodeID &ID) const {
1022 2308570 : ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1023 2308570 : }
1024 :
1025 1398305 : static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
1026 : MapResolver R(CurRec);
1027 1398305 : R.set(LHS, MHSe);
1028 1398305 : return RHS->resolveReferences(R);
1029 : }
1030 :
1031 21450 : static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
1032 : Record *CurRec) {
1033 : bool Change = false;
1034 21450 : Init *Val = ForeachApply(LHS, MHSd->getOperator(), RHS, CurRec);
1035 21450 : if (Val != MHSd->getOperator())
1036 : Change = true;
1037 :
1038 : SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs;
1039 140998 : for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1040 : Init *Arg = MHSd->getArg(i);
1041 : Init *NewArg;
1042 : StringInit *ArgName = MHSd->getArgName(i);
1043 :
1044 : if (DagInit *Argd = dyn_cast<DagInit>(Arg))
1045 14 : NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
1046 : else
1047 119534 : NewArg = ForeachApply(LHS, Arg, RHS, CurRec);
1048 :
1049 119548 : NewArgs.push_back(std::make_pair(NewArg, ArgName));
1050 119548 : if (Arg != NewArg)
1051 : Change = true;
1052 : }
1053 :
1054 21450 : if (Change)
1055 21450 : return DagInit::get(Val, nullptr, NewArgs);
1056 : return MHSd;
1057 : }
1058 :
1059 : // Applies RHS to all elements of MHS, using LHS as a temp variable.
1060 490282 : static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1061 : Record *CurRec) {
1062 : if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
1063 21436 : return ForeachDagApply(LHS, MHSd, RHS, CurRec);
1064 :
1065 : if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
1066 : SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1067 :
1068 1715097 : for (Init *&Item : NewList) {
1069 1257321 : Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec);
1070 1257321 : if (NewItem != Item)
1071 1125552 : Item = NewItem;
1072 : }
1073 915552 : return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1074 : }
1075 :
1076 : return nullptr;
1077 : }
1078 :
1079 1296017 : Init *TernOpInit::Fold(Record *CurRec) const {
1080 1296017 : switch (getOpcode()) {
1081 340530 : case SUBST: {
1082 340530 : DefInit *LHSd = dyn_cast<DefInit>(LHS);
1083 : VarInit *LHSv = dyn_cast<VarInit>(LHS);
1084 : StringInit *LHSs = dyn_cast<StringInit>(LHS);
1085 :
1086 340530 : DefInit *MHSd = dyn_cast<DefInit>(MHS);
1087 : VarInit *MHSv = dyn_cast<VarInit>(MHS);
1088 : StringInit *MHSs = dyn_cast<StringInit>(MHS);
1089 :
1090 340530 : DefInit *RHSd = dyn_cast<DefInit>(RHS);
1091 : VarInit *RHSv = dyn_cast<VarInit>(RHS);
1092 : StringInit *RHSs = dyn_cast<StringInit>(RHS);
1093 :
1094 340530 : if (LHSd && MHSd && RHSd) {
1095 288624 : Record *Val = RHSd->getDef();
1096 321024 : if (LHSd->getAsString() == RHSd->getAsString())
1097 28591 : Val = MHSd->getDef();
1098 288624 : return DefInit::get(Val);
1099 : }
1100 51906 : if (LHSv && MHSv && RHSv) {
1101 0 : std::string Val = RHSv->getName();
1102 0 : if (LHSv->getAsString() == RHSv->getAsString())
1103 0 : Val = MHSv->getName();
1104 0 : return VarInit::get(Val, getType());
1105 : }
1106 51906 : if (LHSs && MHSs && RHSs) {
1107 : std::string Val = RHSs->getValue();
1108 :
1109 : std::string::size_type found;
1110 : std::string::size_type idx = 0;
1111 : while (true) {
1112 9154 : found = Val.find(LHSs->getValue(), idx);
1113 6503 : if (found == std::string::npos)
1114 : break;
1115 2651 : Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1116 2651 : idx = found + MHSs->getValue().size();
1117 : }
1118 :
1119 3852 : return StringInit::get(Val);
1120 : }
1121 : break;
1122 : }
1123 :
1124 490282 : case FOREACH: {
1125 490282 : if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
1126 : return Result;
1127 : break;
1128 : }
1129 :
1130 465190 : case IF: {
1131 465190 : if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
1132 465190 : LHS->convertInitializerTo(IntRecTy::get()))) {
1133 0 : if (LHSi->getValue())
1134 0 : return MHS;
1135 0 : return RHS;
1136 : }
1137 : break;
1138 : }
1139 :
1140 15 : case DAG: {
1141 15 : ListInit *MHSl = dyn_cast<ListInit>(MHS);
1142 15 : ListInit *RHSl = dyn_cast<ListInit>(RHS);
1143 15 : bool MHSok = MHSl || isa<UnsetInit>(MHS);
1144 15 : bool RHSok = RHSl || isa<UnsetInit>(RHS);
1145 :
1146 15 : if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
1147 : break; // Typically prevented by the parser, but might happen with template args
1148 :
1149 15 : if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
1150 : SmallVector<std::pair<Init *, StringInit *>, 8> Children;
1151 10 : unsigned Size = MHSl ? MHSl->size() : RHSl->size();
1152 26 : for (unsigned i = 0; i != Size; ++i) {
1153 17 : Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
1154 17 : Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
1155 17 : if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
1156 1 : return const_cast<TernOpInit *>(this);
1157 16 : Children.emplace_back(Node, dyn_cast<StringInit>(Name));
1158 : }
1159 9 : return DagInit::get(LHS, nullptr, Children);
1160 : }
1161 : break;
1162 : }
1163 : }
1164 :
1165 524319 : return const_cast<TernOpInit *>(this);
1166 : }
1167 :
1168 10868543 : Init *TernOpInit::resolveReferences(Resolver &R) const {
1169 10868543 : Init *lhs = LHS->resolveReferences(R);
1170 :
1171 10868543 : if (getOpcode() == IF && lhs != LHS) {
1172 4605707 : if (IntInit *Value = dyn_cast_or_null<IntInit>(
1173 4605707 : lhs->convertInitializerTo(IntRecTy::get()))) {
1174 : // Short-circuit
1175 4538282 : if (Value->getValue())
1176 2132287 : return MHS->resolveReferences(R);
1177 2405995 : return RHS->resolveReferences(R);
1178 : }
1179 : }
1180 :
1181 6330261 : Init *mhs = MHS->resolveReferences(R);
1182 : Init *rhs;
1183 :
1184 6330261 : if (getOpcode() == FOREACH) {
1185 : ShadowResolver SR(R);
1186 863046 : SR.addShadow(lhs);
1187 863046 : rhs = RHS->resolveReferences(SR);
1188 : } else {
1189 5467215 : rhs = RHS->resolveReferences(R);
1190 : }
1191 :
1192 6330261 : if (LHS != lhs || MHS != mhs || RHS != rhs)
1193 1276673 : return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
1194 1276673 : ->Fold(R.getCurrentRecord());
1195 5053588 : return const_cast<TernOpInit *>(this);
1196 : }
1197 :
1198 109 : std::string TernOpInit::getAsString() const {
1199 : std::string Result;
1200 : bool UnquotedLHS = false;
1201 109 : switch (getOpcode()) {
1202 : case SUBST: Result = "!subst"; break;
1203 24 : case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
1204 : case IF: Result = "!if"; break;
1205 : case DAG: Result = "!dag"; break;
1206 : }
1207 218 : return (Result + "(" +
1208 436 : (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
1209 485 : ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
1210 : }
1211 :
1212 587842 : static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B,
1213 : Init *Start, Init *List, Init *Expr,
1214 : RecTy *Type) {
1215 587842 : ID.AddPointer(Start);
1216 587842 : ID.AddPointer(List);
1217 587842 : ID.AddPointer(A);
1218 587842 : ID.AddPointer(B);
1219 587842 : ID.AddPointer(Expr);
1220 587842 : ID.AddPointer(Type);
1221 587842 : }
1222 :
1223 229638 : FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
1224 : Init *Expr, RecTy *Type) {
1225 229834 : static FoldingSet<FoldOpInit> ThePool;
1226 :
1227 : FoldingSetNodeID ID;
1228 229638 : ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
1229 :
1230 229638 : void *IP = nullptr;
1231 188352 : if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1232 : return I;
1233 :
1234 : FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
1235 41286 : ThePool.InsertNode(I, IP);
1236 41286 : return I;
1237 : }
1238 :
1239 358204 : void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
1240 358204 : ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
1241 358204 : }
1242 :
1243 229638 : Init *FoldOpInit::Fold(Record *CurRec) const {
1244 229638 : if (ListInit *LI = dyn_cast<ListInit>(List)) {
1245 229042 : Init *Accum = Start;
1246 566700 : for (Init *Elt : *LI) {
1247 : MapResolver R(CurRec);
1248 337658 : R.set(A, Accum);
1249 337658 : R.set(B, Elt);
1250 337658 : Accum = Expr->resolveReferences(R);
1251 : }
1252 229042 : return Accum;
1253 : }
1254 596 : return const_cast<FoldOpInit *>(this);
1255 : }
1256 :
1257 353019 : Init *FoldOpInit::resolveReferences(Resolver &R) const {
1258 353019 : Init *NewStart = Start->resolveReferences(R);
1259 353019 : Init *NewList = List->resolveReferences(R);
1260 : ShadowResolver SR(R);
1261 353019 : SR.addShadow(A);
1262 353019 : SR.addShadow(B);
1263 353019 : Init *NewExpr = Expr->resolveReferences(SR);
1264 :
1265 353019 : if (Start == NewStart && List == NewList && Expr == NewExpr)
1266 124033 : return const_cast<FoldOpInit *>(this);
1267 :
1268 228986 : return get(NewStart, NewList, A, B, NewExpr, getType())
1269 228986 : ->Fold(R.getCurrentRecord());
1270 : }
1271 :
1272 0 : Init *FoldOpInit::getBit(unsigned Bit) const {
1273 0 : return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
1274 : }
1275 :
1276 4 : std::string FoldOpInit::getAsString() const {
1277 8 : return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
1278 8 : ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
1279 12 : ", " + Expr->getAsString() + ")")
1280 4 : .str();
1281 : }
1282 :
1283 : static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType,
1284 : Init *Expr) {
1285 806082 : ID.AddPointer(CheckType);
1286 1630009 : ID.AddPointer(Expr);
1287 : }
1288 :
1289 806082 : IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
1290 806275 : static FoldingSet<IsAOpInit> ThePool;
1291 :
1292 : FoldingSetNodeID ID;
1293 : ProfileIsAOpInit(ID, CheckType, Expr);
1294 :
1295 806082 : void *IP = nullptr;
1296 804480 : if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1297 : return I;
1298 :
1299 : IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
1300 1602 : ThePool.InsertNode(I, IP);
1301 1602 : return I;
1302 : }
1303 :
1304 823927 : void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
1305 823927 : ProfileIsAOpInit(ID, CheckType, Expr);
1306 823927 : }
1307 :
1308 806082 : Init *IsAOpInit::Fold() const {
1309 806082 : if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
1310 : // Is the expression type known to be (a subclass of) the desired type?
1311 806082 : if (TI->getType()->typeIsConvertibleTo(CheckType))
1312 433967 : return IntInit::get(1);
1313 :
1314 744230 : if (isa<RecordRecTy>(CheckType)) {
1315 : // If the target type is not a subclass of the expression type, or if
1316 : // the expression has fully resolved to a record, we know that it can't
1317 : // be of the required type.
1318 372115 : if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
1319 371522 : return IntInit::get(0);
1320 : } else {
1321 : // We treat non-record types as not castable.
1322 0 : return IntInit::get(0);
1323 : }
1324 : }
1325 593 : return const_cast<IsAOpInit *>(this);
1326 : }
1327 :
1328 1170289 : Init *IsAOpInit::resolveReferences(Resolver &R) const {
1329 1170289 : Init *NewExpr = Expr->resolveReferences(R);
1330 1170289 : if (Expr != NewExpr)
1331 805489 : return get(CheckType, NewExpr)->Fold();
1332 364800 : return const_cast<IsAOpInit *>(this);
1333 : }
1334 :
1335 0 : Init *IsAOpInit::getBit(unsigned Bit) const {
1336 0 : return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
1337 : }
1338 :
1339 2 : std::string IsAOpInit::getAsString() const {
1340 2 : return (Twine("!isa<") + CheckType->getAsString() + ">(" +
1341 4 : Expr->getAsString() + ")")
1342 2 : .str();
1343 : }
1344 :
1345 293961 : RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1346 293961 : if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
1347 293961 : for (Record *Rec : RecordType->getClasses()) {
1348 293961 : if (RecordVal *Field = Rec->getValue(FieldName))
1349 293961 : return Field->getType();
1350 : }
1351 : }
1352 : return nullptr;
1353 : }
1354 :
1355 : Init *
1356 852580 : TypedInit::convertInitializerTo(RecTy *Ty) const {
1357 852580 : if (getType() == Ty || getType()->typeIsA(Ty))
1358 352379 : return const_cast<TypedInit *>(this);
1359 :
1360 1000402 : if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
1361 7689 : cast<BitsRecTy>(Ty)->getNumBits() == 1)
1362 15378 : return BitsInit::get({const_cast<TypedInit *>(this)});
1363 :
1364 : return nullptr;
1365 : }
1366 :
1367 45863 : Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1368 45863 : BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1369 : if (!T) return nullptr; // Cannot subscript a non-bits variable.
1370 45863 : unsigned NumBits = T->getNumBits();
1371 :
1372 : SmallVector<Init *, 16> NewBits;
1373 : NewBits.reserve(Bits.size());
1374 210839 : for (unsigned Bit : Bits) {
1375 164976 : if (Bit >= NumBits)
1376 : return nullptr;
1377 :
1378 164976 : NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1379 : }
1380 45863 : return BitsInit::get(NewBits);
1381 : }
1382 :
1383 221488673 : Init *TypedInit::getCastTo(RecTy *Ty) const {
1384 : // Handle the common case quickly
1385 221488673 : if (getType() == Ty || getType()->typeIsA(Ty))
1386 220845536 : return const_cast<TypedInit *>(this);
1387 :
1388 643137 : if (Init *Converted = convertInitializerTo(Ty)) {
1389 : assert(!isa<TypedInit>(Converted) ||
1390 : cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
1391 : return Converted;
1392 : }
1393 :
1394 5037 : if (!getType()->typeIsConvertibleTo(Ty))
1395 : return nullptr;
1396 :
1397 : return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
1398 5027 : ->Fold(nullptr);
1399 : }
1400 :
1401 349 : Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1402 349 : ListRecTy *T = dyn_cast<ListRecTy>(getType());
1403 : if (!T) return nullptr; // Cannot subscript a non-list variable.
1404 :
1405 349 : if (Elements.size() == 1)
1406 348 : return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1407 :
1408 : SmallVector<Init*, 8> ListInits;
1409 : ListInits.reserve(Elements.size());
1410 3 : for (unsigned Element : Elements)
1411 2 : ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1412 : Element));
1413 2 : return ListInit::get(ListInits, T->getElementType());
1414 : }
1415 :
1416 :
1417 0 : VarInit *VarInit::get(StringRef VN, RecTy *T) {
1418 0 : Init *Value = StringInit::get(VN);
1419 0 : return VarInit::get(Value, T);
1420 : }
1421 :
1422 1137021 : VarInit *VarInit::get(Init *VN, RecTy *T) {
1423 : using Key = std::pair<RecTy *, Init *>;
1424 1137363 : static DenseMap<Key, VarInit*> ThePool;
1425 :
1426 1137021 : Key TheKey(std::make_pair(T, VN));
1427 :
1428 : VarInit *&I = ThePool[TheKey];
1429 1137021 : if (!I)
1430 641220 : I = new(Allocator) VarInit(VN, T);
1431 1137021 : return I;
1432 : }
1433 :
1434 6432133 : StringRef VarInit::getName() const {
1435 6432133 : StringInit *NameString = cast<StringInit>(getNameInit());
1436 6432133 : return NameString->getValue();
1437 : }
1438 :
1439 55521114 : Init *VarInit::getBit(unsigned Bit) const {
1440 55521114 : if (getType() == BitRecTy::get())
1441 12931201 : return const_cast<VarInit*>(this);
1442 42589913 : return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1443 : }
1444 :
1445 87920162 : Init *VarInit::resolveReferences(Resolver &R) const {
1446 87920162 : if (Init *Val = R.resolve(VarName))
1447 : return Val;
1448 44589640 : return const_cast<VarInit *>(this);
1449 : }
1450 :
1451 69899877 : VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1452 : using Key = std::pair<TypedInit *, unsigned>;
1453 69900090 : static DenseMap<Key, VarBitInit*> ThePool;
1454 :
1455 69899877 : Key TheKey(std::make_pair(T, B));
1456 :
1457 : VarBitInit *&I = ThePool[TheKey];
1458 69899877 : if (!I)
1459 649904 : I = new(Allocator) VarBitInit(T, B);
1460 69899877 : return I;
1461 : }
1462 :
1463 240 : std::string VarBitInit::getAsString() const {
1464 480 : return TI->getAsString() + "{" + utostr(Bit) + "}";
1465 : }
1466 :
1467 922 : Init *VarBitInit::resolveReferences(Resolver &R) const {
1468 922 : Init *I = TI->resolveReferences(R);
1469 922 : if (TI != I)
1470 616 : return I->getBit(getBitNum());
1471 :
1472 306 : return const_cast<VarBitInit*>(this);
1473 : }
1474 :
1475 402 : VarListElementInit *VarListElementInit::get(TypedInit *T,
1476 : unsigned E) {
1477 : using Key = std::pair<TypedInit *, unsigned>;
1478 603 : static DenseMap<Key, VarListElementInit*> ThePool;
1479 :
1480 402 : Key TheKey(std::make_pair(T, E));
1481 :
1482 : VarListElementInit *&I = ThePool[TheKey];
1483 789 : if (!I) I = new(Allocator) VarListElementInit(T, E);
1484 402 : return I;
1485 : }
1486 :
1487 25 : std::string VarListElementInit::getAsString() const {
1488 50 : return TI->getAsString() + "[" + utostr(Element) + "]";
1489 : }
1490 :
1491 24478 : Init *VarListElementInit::resolveReferences(Resolver &R) const {
1492 24478 : Init *NewTI = TI->resolveReferences(R);
1493 : if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1494 : // Leave out-of-bounds array references as-is. This can happen without
1495 : // being an error, e.g. in the untaken "branch" of an !if expression.
1496 15836 : if (getElementNum() < List->size())
1497 11741 : return List->getElement(getElementNum());
1498 : }
1499 12737 : if (NewTI != TI && isa<TypedInit>(NewTI))
1500 52 : return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1501 12685 : return const_cast<VarListElementInit *>(this);
1502 : }
1503 :
1504 0 : Init *VarListElementInit::getBit(unsigned Bit) const {
1505 0 : if (getType() == BitRecTy::get())
1506 0 : return const_cast<VarListElementInit*>(this);
1507 0 : return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1508 : }
1509 :
1510 914693 : DefInit::DefInit(Record *D)
1511 914693 : : TypedInit(IK_DefInit, D->getType()), Def(D) {}
1512 :
1513 2300104 : DefInit *DefInit::get(Record *R) {
1514 2300104 : return R->getDefInit();
1515 : }
1516 :
1517 26210 : Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1518 : if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1519 26210 : if (getType()->typeIsConvertibleTo(RRT))
1520 26210 : return const_cast<DefInit *>(this);
1521 : return nullptr;
1522 : }
1523 :
1524 1403523 : RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1525 2807046 : if (const RecordVal *RV = Def->getValue(FieldName))
1526 1403523 : return RV->getType();
1527 : return nullptr;
1528 : }
1529 :
1530 7272105 : std::string DefInit::getAsString() const {
1531 7272105 : return Def->getName();
1532 : }
1533 :
1534 7336402 : static void ProfileVarDefInit(FoldingSetNodeID &ID,
1535 : Record *Class,
1536 : ArrayRef<Init *> Args) {
1537 7336402 : ID.AddInteger(Args.size());
1538 7336402 : ID.AddPointer(Class);
1539 :
1540 24852198 : for (Init *I : Args)
1541 17515796 : ID.AddPointer(I);
1542 7336402 : }
1543 :
1544 2920531 : VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
1545 2920768 : static FoldingSet<VarDefInit> ThePool;
1546 :
1547 : FoldingSetNodeID ID;
1548 2920531 : ProfileVarDefInit(ID, Class, Args);
1549 :
1550 2920531 : void *IP = nullptr;
1551 2635452 : if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1552 : return I;
1553 :
1554 285079 : void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
1555 : alignof(VarDefInit));
1556 285079 : VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
1557 : std::uninitialized_copy(Args.begin(), Args.end(),
1558 : I->getTrailingObjects<Init *>());
1559 285079 : ThePool.InsertNode(I, IP);
1560 285079 : return I;
1561 : }
1562 :
1563 4415871 : void VarDefInit::Profile(FoldingSetNodeID &ID) const {
1564 4415871 : ProfileVarDefInit(ID, Class, args());
1565 4415871 : }
1566 :
1567 2301390 : DefInit *VarDefInit::instantiate() {
1568 2301390 : if (!Def) {
1569 265165 : RecordKeeper &Records = Class->getRecords();
1570 265165 : auto NewRecOwner = make_unique<Record>(Records.getNewAnonymousName(),
1571 530330 : Class->getLoc(), Records,
1572 530330 : /*IsAnonymous=*/true);
1573 : Record *NewRec = NewRecOwner.get();
1574 :
1575 : // Copy values from class to instance
1576 2827424 : for (const RecordVal &Val : Class->getValues())
1577 : NewRec->addValue(Val);
1578 :
1579 : // Substitute and resolve template arguments
1580 265165 : ArrayRef<Init *> TArgs = Class->getTemplateArgs();
1581 : MapResolver R(NewRec);
1582 :
1583 996950 : for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1584 731785 : if (i < args_size())
1585 660161 : R.set(TArgs[i], getArg(i));
1586 : else
1587 143248 : R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
1588 :
1589 731785 : NewRec->removeValue(TArgs[i]);
1590 : }
1591 :
1592 265165 : NewRec->resolveReferences(R);
1593 :
1594 : // Add superclasses.
1595 265165 : ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
1596 421372 : for (const auto &SCPair : SCs)
1597 156207 : NewRec->addSuperClass(SCPair.first, SCPair.second);
1598 :
1599 : NewRec->addSuperClass(Class,
1600 : SMRange(Class->getLoc().back(),
1601 265165 : Class->getLoc().back()));
1602 :
1603 : // Resolve internal references and store in record keeper
1604 265165 : NewRec->resolveReferences();
1605 265165 : Records.addDef(std::move(NewRecOwner));
1606 :
1607 265165 : Def = DefInit::get(NewRec);
1608 : }
1609 :
1610 2301390 : return Def;
1611 : }
1612 :
1613 4299486 : Init *VarDefInit::resolveReferences(Resolver &R) const {
1614 : TrackUnresolvedResolver UR(&R);
1615 : bool Changed = false;
1616 : SmallVector<Init *, 8> NewArgs;
1617 4299486 : NewArgs.reserve(args_size());
1618 :
1619 14919871 : for (Init *Arg : args()) {
1620 10620385 : Init *NewArg = Arg->resolveReferences(UR);
1621 10620385 : NewArgs.push_back(NewArg);
1622 10620385 : Changed |= NewArg != Arg;
1623 : }
1624 :
1625 4299486 : if (Changed) {
1626 2609114 : auto New = VarDefInit::get(Class, NewArgs);
1627 2609114 : if (!UR.foundUnresolved())
1628 2215177 : return New->instantiate();
1629 : return New;
1630 : }
1631 1690372 : return const_cast<VarDefInit *>(this);
1632 : }
1633 :
1634 311417 : Init *VarDefInit::Fold() const {
1635 311417 : if (Def)
1636 : return Def;
1637 :
1638 : TrackUnresolvedResolver R;
1639 286406 : for (Init *Arg : args())
1640 191418 : Arg->resolveReferences(R);
1641 :
1642 94988 : if (!R.foundUnresolved())
1643 86213 : return const_cast<VarDefInit *>(this)->instantiate();
1644 8775 : return const_cast<VarDefInit *>(this);
1645 : }
1646 :
1647 8 : std::string VarDefInit::getAsString() const {
1648 16 : std::string Result = Class->getNameInitAsString() + "<";
1649 : const char *sep = "";
1650 18 : for (Init *Arg : args()) {
1651 : Result += sep;
1652 : sep = ", ";
1653 20 : Result += Arg->getAsString();
1654 : }
1655 8 : return Result + ">";
1656 : }
1657 :
1658 15228741 : FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1659 : using Key = std::pair<Init *, StringInit *>;
1660 15228968 : static DenseMap<Key, FieldInit*> ThePool;
1661 :
1662 15228741 : Key TheKey(std::make_pair(R, FN));
1663 :
1664 : FieldInit *&I = ThePool[TheKey];
1665 16802602 : if (!I) I = new(Allocator) FieldInit(R, FN);
1666 15228741 : return I;
1667 : }
1668 :
1669 19043732 : Init *FieldInit::getBit(unsigned Bit) const {
1670 19043732 : if (getType() == BitRecTy::get())
1671 1635 : return const_cast<FieldInit*>(this);
1672 19042097 : return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1673 : }
1674 :
1675 27257073 : Init *FieldInit::resolveReferences(Resolver &R) const {
1676 27257073 : Init *NewRec = Rec->resolveReferences(R);
1677 27257073 : if (NewRec != Rec)
1678 15105118 : return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
1679 12151955 : return const_cast<FieldInit *>(this);
1680 : }
1681 :
1682 15228741 : Init *FieldInit::Fold(Record *CurRec) const {
1683 15228741 : if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1684 14526567 : Record *Def = DI->getDef();
1685 14526567 : if (Def == CurRec)
1686 1 : PrintFatalError(CurRec->getLoc(),
1687 : Twine("Attempting to access field '") +
1688 1 : FieldName->getAsUnquotedString() + "' of '" +
1689 1 : Rec->getAsString() + "' is a forbidden self-reference");
1690 14526566 : Init *FieldVal = Def->getValue(FieldName)->getValue();
1691 14526566 : if (FieldVal->isComplete())
1692 : return FieldVal;
1693 : }
1694 702174 : return const_cast<FieldInit *>(this);
1695 : }
1696 :
1697 0 : static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
1698 : ArrayRef<Init *> ArgRange,
1699 : ArrayRef<StringInit *> NameRange) {
1700 0 : ID.AddPointer(V);
1701 0 : ID.AddPointer(VN);
1702 :
1703 : ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1704 0 : ArrayRef<StringInit *>::iterator Name = NameRange.begin();
1705 0 : while (Arg != ArgRange.end()) {
1706 : assert(Name != NameRange.end() && "Arg name underflow!");
1707 0 : ID.AddPointer(*Arg++);
1708 0 : ID.AddPointer(*Name++);
1709 : }
1710 : assert(Name == NameRange.end() && "Arg name overflow!");
1711 0 : }
1712 :
1713 : DagInit *
1714 3709700 : DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1715 : ArrayRef<StringInit *> NameRange) {
1716 3709906 : static FoldingSet<DagInit> ThePool;
1717 :
1718 : FoldingSetNodeID ID;
1719 3709700 : ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1720 :
1721 3709700 : void *IP = nullptr;
1722 1845930 : if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1723 : return I;
1724 :
1725 1863770 : void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1726 1863770 : DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1727 : std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1728 : I->getTrailingObjects<Init *>());
1729 : std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1730 : I->getTrailingObjects<StringInit *>());
1731 1863770 : ThePool.InsertNode(I, IP);
1732 1863770 : return I;
1733 : }
1734 :
1735 : DagInit *
1736 1224311 : DagInit::get(Init *V, StringInit *VN,
1737 : ArrayRef<std::pair<Init*, StringInit*>> args) {
1738 : SmallVector<Init *, 8> Args;
1739 : SmallVector<StringInit *, 8> Names;
1740 :
1741 3108907 : for (const auto &Arg : args) {
1742 1884596 : Args.push_back(Arg.first);
1743 1884596 : Names.push_back(Arg.second);
1744 : }
1745 :
1746 1224311 : return DagInit::get(V, VN, Args, Names);
1747 : }
1748 :
1749 7504050 : void DagInit::Profile(FoldingSetNodeID &ID) const {
1750 7504050 : ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1751 7504050 : }
1752 :
1753 11784875 : Init *DagInit::resolveReferences(Resolver &R) const {
1754 : SmallVector<Init*, 8> NewArgs;
1755 11784875 : NewArgs.reserve(arg_size());
1756 : bool ArgsChanged = false;
1757 31949727 : for (const Init *Arg : getArgs()) {
1758 20164852 : Init *NewArg = Arg->resolveReferences(R);
1759 20164852 : NewArgs.push_back(NewArg);
1760 20164852 : ArgsChanged |= NewArg != Arg;
1761 : }
1762 :
1763 11784875 : Init *Op = Val->resolveReferences(R);
1764 11784875 : if (Op != Val || ArgsChanged)
1765 2425276 : return DagInit::get(Op, ValName, NewArgs, getArgNames());
1766 :
1767 9359599 : return const_cast<DagInit *>(this);
1768 : }
1769 :
1770 4229517 : bool DagInit::isConcrete() const {
1771 4229517 : if (!Val->isConcrete())
1772 : return false;
1773 11916460 : for (const Init *Elt : getArgs()) {
1774 7686943 : if (!Elt->isConcrete())
1775 : return false;
1776 : }
1777 : return true;
1778 : }
1779 :
1780 4929 : std::string DagInit::getAsString() const {
1781 4929 : std::string Result = "(" + Val->getAsString();
1782 4929 : if (ValName)
1783 2 : Result += ":" + ValName->getAsUnquotedString();
1784 4929 : if (!arg_empty()) {
1785 9838 : Result += " " + getArg(0)->getAsString();
1786 8464 : if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1787 14241 : for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1788 18644 : Result += ", " + getArg(i)->getAsString();
1789 15335 : if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1790 : }
1791 : }
1792 4929 : return Result + ")";
1793 : }
1794 :
1795 : //===----------------------------------------------------------------------===//
1796 : // Other implementations
1797 : //===----------------------------------------------------------------------===//
1798 :
1799 620007 : RecordVal::RecordVal(Init *N, RecTy *T, bool P)
1800 620007 : : Name(N), TyAndPrefix(T, P) {
1801 620007 : setValue(UnsetInit::get());
1802 : assert(Value && "Cannot create unset value for current type!");
1803 620007 : }
1804 :
1805 1487002 : StringRef RecordVal::getName() const {
1806 1487002 : return cast<StringInit>(getNameInit())->getValue();
1807 : }
1808 :
1809 233078435 : bool RecordVal::setValue(Init *V) {
1810 233078435 : if (V) {
1811 466156870 : Value = V->getCastTo(getType());
1812 233078435 : if (Value) {
1813 : assert(!isa<TypedInit>(Value) ||
1814 : cast<TypedInit>(Value)->getType()->typeIsA(getType()));
1815 : if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
1816 22759395 : if (!isa<BitsInit>(Value)) {
1817 : SmallVector<Init *, 64> Bits;
1818 169138 : Bits.reserve(BTy->getNumBits());
1819 1327260 : for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i)
1820 1158122 : Bits.push_back(Value->getBit(i));
1821 169138 : Value = BitsInit::get(Bits);
1822 : }
1823 : }
1824 : }
1825 233078435 : return Value == nullptr;
1826 : }
1827 0 : Value = nullptr;
1828 0 : return false;
1829 : }
1830 :
1831 : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1832 : LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1833 : #endif
1834 :
1835 1838 : void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1836 1838 : if (getPrefix()) OS << "field ";
1837 1838 : OS << *getType() << " " << getNameInitAsString();
1838 :
1839 1838 : if (getValue())
1840 1829 : OS << " = " << *getValue();
1841 :
1842 1838 : if (PrintSem) OS << ";\n";
1843 1838 : }
1844 :
1845 : unsigned Record::LastID = 0;
1846 :
1847 3669987 : void Record::checkName() {
1848 : // Ensure the record name has string type.
1849 3669987 : const TypedInit *TypedName = cast<const TypedInit>(Name);
1850 7339974 : if (!isa<StringRecTy>(TypedName->getType()))
1851 0 : PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
1852 : "' is not a string!");
1853 3669987 : }
1854 :
1855 915203 : RecordRecTy *Record::getType() {
1856 : SmallVector<Record *, 4> DirectSCs;
1857 915203 : getDirectSuperClasses(DirectSCs);
1858 915203 : return RecordRecTy::get(DirectSCs);
1859 : }
1860 :
1861 9665551 : DefInit *Record::getDefInit() {
1862 9665551 : if (!TheInit)
1863 914693 : TheInit = new(Allocator) DefInit(this);
1864 9665551 : return TheInit;
1865 : }
1866 :
1867 1080297 : void Record::setName(Init *NewName) {
1868 1080297 : Name = NewName;
1869 1080297 : checkName();
1870 : // DO NOT resolve record values to the name at this point because
1871 : // there might be default values for arguments of this def. Those
1872 : // arguments might not have been resolved yet so we don't want to
1873 : // prematurely assume values for those arguments were not passed to
1874 : // this def.
1875 : //
1876 : // Nonetheless, it may be that some of this Record's values
1877 : // reference the record name. Indeed, the reason for having the
1878 : // record name be an Init is to provide this flexibility. The extra
1879 : // resolve steps after completely instantiating defs takes care of
1880 : // this. See TGParser::ParseDef and TGParser::ParseDefm.
1881 1080297 : }
1882 :
1883 1106090 : void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
1884 : ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
1885 2558651 : while (!SCs.empty()) {
1886 : // Superclasses are in reverse preorder, so 'back' is a direct superclass,
1887 : // and its transitive superclasses are directly preceding it.
1888 1452561 : Record *SC = SCs.back().first;
1889 1452561 : SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
1890 1452561 : Classes.push_back(SC);
1891 : }
1892 1106090 : }
1893 :
1894 7919753 : void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
1895 230466568 : for (RecordVal &Value : Values) {
1896 222546818 : if (SkipVal == &Value) // Skip resolve the same field as the given one
1897 : continue;
1898 222546818 : if (Init *V = Value.getValue()) {
1899 222546810 : Init *VR = V->resolveReferences(R);
1900 222546807 : if (Value.setValue(VR)) {
1901 : std::string Type;
1902 : if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
1903 : Type =
1904 0 : (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
1905 0 : PrintFatalError(getLoc(), Twine("Invalid value ") + Type +
1906 : "is found when setting '" +
1907 : Value.getNameInitAsString() +
1908 : "' of type '" +
1909 0 : Value.getType()->getAsString() +
1910 : "' after resolving references: " +
1911 0 : VR->getAsUnquotedString() + "\n");
1912 : }
1913 : }
1914 : }
1915 7919750 : Init *OldName = getNameInit();
1916 7919750 : Init *NewName = Name->resolveReferences(R);
1917 7919750 : if (NewName != OldName) {
1918 : // Re-register with RecordKeeper.
1919 863893 : setName(NewName);
1920 : }
1921 7919750 : }
1922 :
1923 3209586 : void Record::resolveReferences() {
1924 3209584 : RecordResolver R(*this);
1925 : R.setFinal(true);
1926 3209586 : resolveReferences(R);
1927 3209584 : }
1928 :
1929 0 : void Record::resolveReferencesTo(const RecordVal *RV) {
1930 : RecordValResolver R(*this, RV);
1931 0 : resolveReferences(R, RV);
1932 0 : }
1933 :
1934 : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1935 : LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1936 : #endif
1937 :
1938 912 : raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1939 912 : OS << R.getNameInitAsString();
1940 :
1941 : ArrayRef<Init *> TArgs = R.getTemplateArgs();
1942 912 : if (!TArgs.empty()) {
1943 171 : OS << "<";
1944 : bool NeedComma = false;
1945 419 : for (const Init *TA : TArgs) {
1946 248 : if (NeedComma) OS << ", ";
1947 : NeedComma = true;
1948 : const RecordVal *RV = R.getValue(TA);
1949 : assert(RV && "Template argument record not found??");
1950 248 : RV->print(OS, false);
1951 : }
1952 171 : OS << ">";
1953 : }
1954 :
1955 912 : OS << " {";
1956 : ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
1957 912 : if (!SC.empty()) {
1958 632 : OS << "\t//";
1959 1353 : for (const auto &SuperPair : SC)
1960 1442 : OS << " " << SuperPair.first->getNameInitAsString();
1961 : }
1962 912 : OS << "\n";
1963 :
1964 2750 : for (const RecordVal &Val : R.getValues())
1965 1841 : if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1966 3 : OS << Val;
1967 2750 : for (const RecordVal &Val : R.getValues())
1968 3673 : if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1969 1587 : OS << Val;
1970 :
1971 912 : return OS << "}\n";
1972 : }
1973 :
1974 837989 : Init *Record::getValueInit(StringRef FieldName) const {
1975 837989 : const RecordVal *R = getValue(FieldName);
1976 837989 : if (!R || !R->getValue())
1977 0 : PrintFatalError(getLoc(), "Record `" + getName() +
1978 : "' does not have a field named `" + FieldName + "'!\n");
1979 837989 : return R->getValue();
1980 : }
1981 :
1982 8981556 : StringRef Record::getValueAsString(StringRef FieldName) const {
1983 8981556 : const RecordVal *R = getValue(FieldName);
1984 8981556 : if (!R || !R->getValue())
1985 0 : PrintFatalError(getLoc(), "Record `" + getName() +
1986 : "' does not have a field named `" + FieldName + "'!\n");
1987 :
1988 : if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1989 8685076 : return SI->getValue();
1990 : if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1991 296480 : return CI->getValue();
1992 :
1993 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1994 : FieldName + "' does not have a string initializer!");
1995 : }
1996 :
1997 804412 : BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1998 804412 : const RecordVal *R = getValue(FieldName);
1999 804412 : if (!R || !R->getValue())
2000 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2001 : "' does not have a field named `" + FieldName + "'!\n");
2002 :
2003 : if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
2004 804412 : return BI;
2005 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2006 : FieldName + "' does not have a BitsInit initializer!");
2007 : }
2008 :
2009 5787829 : ListInit *Record::getValueAsListInit(StringRef FieldName) const {
2010 5787829 : const RecordVal *R = getValue(FieldName);
2011 5787829 : if (!R || !R->getValue())
2012 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2013 : "' does not have a field named `" + FieldName + "'!\n");
2014 :
2015 : if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
2016 5787829 : return LI;
2017 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2018 : FieldName + "' does not have a list initializer!");
2019 : }
2020 :
2021 : std::vector<Record*>
2022 4172028 : Record::getValueAsListOfDefs(StringRef FieldName) const {
2023 4172028 : ListInit *List = getValueAsListInit(FieldName);
2024 : std::vector<Record*> Defs;
2025 9575187 : for (Init *I : List->getValues()) {
2026 : if (DefInit *DI = dyn_cast<DefInit>(I))
2027 5403159 : Defs.push_back(DI->getDef());
2028 : else
2029 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2030 : FieldName + "' list is not entirely DefInit!");
2031 : }
2032 4172028 : return Defs;
2033 : }
2034 :
2035 3905828 : int64_t Record::getValueAsInt(StringRef FieldName) const {
2036 3905828 : const RecordVal *R = getValue(FieldName);
2037 3905828 : if (!R || !R->getValue())
2038 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2039 : "' does not have a field named `" + FieldName + "'!\n");
2040 :
2041 : if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
2042 3905828 : return II->getValue();
2043 0 : PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2044 : FieldName +
2045 : "' does not have an int initializer: " +
2046 0 : R->getValue()->getAsString());
2047 : }
2048 :
2049 : std::vector<int64_t>
2050 72845 : Record::getValueAsListOfInts(StringRef FieldName) const {
2051 72845 : ListInit *List = getValueAsListInit(FieldName);
2052 : std::vector<int64_t> Ints;
2053 111496 : for (Init *I : List->getValues()) {
2054 : if (IntInit *II = dyn_cast<IntInit>(I))
2055 38651 : Ints.push_back(II->getValue());
2056 : else
2057 0 : PrintFatalError(getLoc(),
2058 0 : Twine("Record `") + getName() + "', field `" + FieldName +
2059 : "' does not have a list of ints initializer: " +
2060 0 : I->getAsString());
2061 : }
2062 72845 : return Ints;
2063 : }
2064 :
2065 : std::vector<StringRef>
2066 11384 : Record::getValueAsListOfStrings(StringRef FieldName) const {
2067 11384 : ListInit *List = getValueAsListInit(FieldName);
2068 : std::vector<StringRef> Strings;
2069 23522 : for (Init *I : List->getValues()) {
2070 : if (StringInit *SI = dyn_cast<StringInit>(I))
2071 12138 : Strings.push_back(SI->getValue());
2072 : else
2073 0 : PrintFatalError(getLoc(),
2074 0 : Twine("Record `") + getName() + "', field `" + FieldName +
2075 : "' does not have a list of strings initializer: " +
2076 0 : I->getAsString());
2077 : }
2078 11384 : return Strings;
2079 : }
2080 :
2081 15789114 : Record *Record::getValueAsDef(StringRef FieldName) const {
2082 15789114 : const RecordVal *R = getValue(FieldName);
2083 15789114 : if (!R || !R->getValue())
2084 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2085 : "' does not have a field named `" + FieldName + "'!\n");
2086 :
2087 : if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2088 15789114 : return DI->getDef();
2089 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2090 : FieldName + "' does not have a def initializer!");
2091 : }
2092 :
2093 36213795 : bool Record::getValueAsBit(StringRef FieldName) const {
2094 36213795 : const RecordVal *R = getValue(FieldName);
2095 36213795 : if (!R || !R->getValue())
2096 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2097 : "' does not have a field named `" + FieldName + "'!\n");
2098 :
2099 : if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2100 36213795 : return BI->getValue();
2101 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2102 : FieldName + "' does not have a bit initializer!");
2103 : }
2104 :
2105 2450045 : bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
2106 2450045 : const RecordVal *R = getValue(FieldName);
2107 2450045 : if (!R || !R->getValue())
2108 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2109 0 : "' does not have a field named `" + FieldName.str() + "'!\n");
2110 :
2111 2450045 : if (isa<UnsetInit>(R->getValue())) {
2112 1815631 : Unset = true;
2113 1815631 : return false;
2114 : }
2115 634414 : Unset = false;
2116 : if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2117 634414 : return BI->getValue();
2118 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2119 : FieldName + "' does not have a bit initializer!");
2120 : }
2121 :
2122 2421303 : DagInit *Record::getValueAsDag(StringRef FieldName) const {
2123 2421303 : const RecordVal *R = getValue(FieldName);
2124 2421303 : if (!R || !R->getValue())
2125 0 : PrintFatalError(getLoc(), "Record `" + getName() +
2126 : "' does not have a field named `" + FieldName + "'!\n");
2127 :
2128 : if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
2129 2421303 : return DI;
2130 0 : PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2131 : FieldName + "' does not have a dag initializer!");
2132 : }
2133 :
2134 : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2135 : LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
2136 : #endif
2137 :
2138 80 : raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
2139 80 : OS << "------------- Classes -----------------\n";
2140 283 : for (const auto &C : RK.getClasses())
2141 203 : OS << "class " << *C.second;
2142 :
2143 80 : OS << "------------- Defs -----------------\n";
2144 789 : for (const auto &D : RK.getDefs())
2145 709 : OS << "def " << *D.second;
2146 80 : return OS;
2147 : }
2148 :
2149 : /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2150 : /// an identifier.
2151 830948 : Init *RecordKeeper::getNewAnonymousName() {
2152 830948 : return StringInit::get("anonymous_" + utostr(AnonCounter++));
2153 : }
2154 :
2155 : std::vector<Record *>
2156 3474 : RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
2157 3474 : Record *Class = getClass(ClassName);
2158 3474 : if (!Class)
2159 0 : PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
2160 :
2161 : std::vector<Record*> Defs;
2162 52488416 : for (const auto &D : getDefs())
2163 52484942 : if (D.second->isSubClassOf(Class))
2164 1993753 : Defs.push_back(D.second.get());
2165 :
2166 3474 : return Defs;
2167 : }
2168 :
2169 64310960 : Init *MapResolver::resolve(Init *VarName) {
2170 64310960 : auto It = Map.find(VarName);
2171 64310960 : if (It == Map.end())
2172 : return nullptr;
2173 :
2174 22500956 : Init *I = It->second.V;
2175 :
2176 22500956 : if (!It->second.Resolved && Map.size() > 1) {
2177 : // Resolve mutual references among the mapped variables, but prevent
2178 : // infinite recursion.
2179 : Map.erase(It);
2180 12964260 : I = I->resolveReferences(*this);
2181 12964260 : Map[VarName] = {I, true};
2182 : }
2183 :
2184 : return I;
2185 : }
2186 :
2187 20831560 : Init *RecordResolver::resolve(Init *VarName) {
2188 36943767 : Init *Val = Cache.lookup(VarName);
2189 4719353 : if (Val)
2190 : return Val;
2191 :
2192 17967385 : for (Init *S : Stack) {
2193 1855177 : if (S == VarName)
2194 : return nullptr; // prevent infinite recursion
2195 : }
2196 :
2197 32224416 : if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
2198 32224416 : if (!isa<UnsetInit>(RV->getValue())) {
2199 : Val = RV->getValue();
2200 16110214 : Stack.push_back(VarName);
2201 16110214 : Val = Val->resolveReferences(*this);
2202 : Stack.pop_back();
2203 : }
2204 : }
2205 :
2206 16112208 : Cache[VarName] = Val;
2207 16112208 : return Val;
2208 : }
2209 :
2210 14176062 : Init *TrackUnresolvedResolver::resolve(Init *VarName) {
2211 : Init *I = nullptr;
2212 :
2213 14176062 : if (R) {
2214 14111622 : I = R->resolve(VarName);
2215 14111622 : if (I && !FoundUnresolved) {
2216 : // Do not recurse into the resolved initializer, as that would change
2217 : // the behavior of the resolver we're delegating, but do check to see
2218 : // if there are unresolved variables remaining.
2219 : TrackUnresolvedResolver Sub;
2220 6013373 : I->resolveReferences(Sub);
2221 6013373 : FoundUnresolved |= Sub.FoundUnresolved;
2222 : }
2223 : }
2224 :
2225 14111622 : if (!I)
2226 7594318 : FoundUnresolved = true;
2227 14176062 : return I;
2228 : }
2229 :
2230 14656 : Init *HasReferenceResolver::resolve(Init *VarName)
2231 : {
2232 14656 : if (VarName == VarNameToTrack)
2233 12590 : Found = true;
2234 14656 : return nullptr;
2235 : }
|