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