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