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