LLVM 23.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
49/// This class represents the internal implementation of the RecordKeeper.
50/// It contains all of the contextual static state of the Record classes. It is
51/// kept out-of-line to simplify dependencies, and also make it easier for
52/// internal classes to access the uniquer state of the keeper.
60
62 std::vector<BitsRecTy *> SharedBitsRecTys;
67
72
75 std::map<int64_t, IntInit *> TheIntInitPool;
95
96 unsigned AnonCounter;
97 unsigned LastRecordID;
98
99 void dumpAllocationStats(raw_ostream &OS) const;
100};
101
103 // Dump memory allocation related stats.
104 OS << "TheArgumentInitPool size = " << TheArgumentInitPool.size() << '\n';
105 OS << "TheBitsInitPool size = " << TheBitsInitPool.size() << '\n';
106 OS << "TheIntInitPool size = " << TheIntInitPool.size() << '\n';
107 OS << "StringInitStringPool size = " << StringInitStringPool.size() << '\n';
108 OS << "StringInitCodePool size = " << StringInitCodePool.size() << '\n';
109 OS << "TheListInitPool size = " << TheListInitPool.size() << '\n';
110 OS << "TheUnOpInitPool size = " << TheUnOpInitPool.size() << '\n';
111 OS << "TheBinOpInitPool size = " << TheBinOpInitPool.size() << '\n';
112 OS << "TheTernOpInitPool size = " << TheTernOpInitPool.size() << '\n';
113 OS << "TheFoldOpInitPool size = " << TheFoldOpInitPool.size() << '\n';
114 OS << "TheIsAOpInitPool size = " << TheIsAOpInitPool.size() << '\n';
115 OS << "TheExistsOpInitPool size = " << TheExistsOpInitPool.size() << '\n';
116 OS << "TheCondOpInitPool size = " << TheCondOpInitPool.size() << '\n';
117 OS << "TheDagInitPool size = " << TheDagInitPool.size() << '\n';
118 OS << "RecordTypePool size = " << RecordTypePool.size() << '\n';
119 OS << "TheVarInitPool size = " << TheVarInitPool.size() << '\n';
120 OS << "TheVarBitInitPool size = " << TheVarBitInitPool.size() << '\n';
121 OS << "TheVarDefInitPool size = " << TheVarDefInitPool.size() << '\n';
122 OS << "TheFieldInitPool size = " << TheFieldInitPool.size() << '\n';
123 OS << "Bytes allocated = " << Allocator.getBytesAllocated() << '\n';
124 OS << "Total allocator memory = " << Allocator.getTotalMemory() << "\n\n";
125
126 OS << "Number of records instantiated = " << LastRecordID << '\n';
127 OS << "Number of anonymous records = " << AnonCounter << '\n';
128}
129
130//===----------------------------------------------------------------------===//
131// Type implementations
132//===----------------------------------------------------------------------===//
133
134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
136#endif
137
139 if (!ListTy)
140 ListTy = new (RK.getImpl().Allocator) ListRecTy(this);
141 return ListTy;
142}
143
144bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
145 assert(RHS && "NULL pointer");
146 return Kind == RHS->getRecTyKind();
147}
148
149bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
150
151const BitRecTy *BitRecTy::get(RecordKeeper &RK) {
152 return &RK.getImpl().SharedBitRecTy;
153}
154
156 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
157 return true;
158 if (const auto *BitsTy = dyn_cast<BitsRecTy>(RHS))
159 return BitsTy->getNumBits() == 1;
160 return false;
161}
162
163const BitsRecTy *BitsRecTy::get(RecordKeeper &RK, unsigned Sz) {
164 detail::RecordKeeperImpl &RKImpl = RK.getImpl();
165 if (Sz >= RKImpl.SharedBitsRecTys.size())
166 RKImpl.SharedBitsRecTys.resize(Sz + 1);
167 BitsRecTy *&Ty = RKImpl.SharedBitsRecTys[Sz];
168 if (!Ty)
169 Ty = new (RKImpl.Allocator) BitsRecTy(RK, Sz);
170 return Ty;
171}
172
173std::string BitsRecTy::getAsString() const {
174 return "bits<" + utostr(Size) + ">";
175}
176
177bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
178 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
179 return cast<BitsRecTy>(RHS)->Size == Size;
180 RecTyKind kind = RHS->getRecTyKind();
181 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
182}
183
184const IntRecTy *IntRecTy::get(RecordKeeper &RK) {
185 return &RK.getImpl().SharedIntRecTy;
186}
187
188bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
189 RecTyKind kind = RHS->getRecTyKind();
190 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
191}
192
193const StringRecTy *StringRecTy::get(RecordKeeper &RK) {
194 return &RK.getImpl().SharedStringRecTy;
195}
196
197std::string StringRecTy::getAsString() const {
198 return "string";
199}
200
202 RecTyKind Kind = RHS->getRecTyKind();
203 return Kind == StringRecTyKind;
204}
205
206std::string ListRecTy::getAsString() const {
207 return "list<" + ElementTy->getAsString() + ">";
208}
209
210bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
211 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
212 return ElementTy->typeIsConvertibleTo(ListTy->getElementType());
213 return false;
214}
215
216bool ListRecTy::typeIsA(const RecTy *RHS) const {
217 if (const auto *RHSl = dyn_cast<ListRecTy>(RHS))
218 return getElementType()->typeIsA(RHSl->getElementType());
219 return false;
220}
221
222const DagRecTy *DagRecTy::get(RecordKeeper &RK) {
223 return &RK.getImpl().SharedDagRecTy;
224}
225
226std::string DagRecTy::getAsString() const {
227 return "dag";
228}
229
231 ArrayRef<const Record *> Classes) {
232 ID.AddInteger(Classes.size());
233 for (const Record *R : Classes)
234 ID.AddPointer(R);
235}
236
237RecordRecTy::RecordRecTy(RecordKeeper &RK, ArrayRef<const Record *> Classes)
238 : RecTy(RecordRecTyKind, RK), NumClasses(Classes.size()) {
239 llvm::uninitialized_copy(Classes, getTrailingObjects());
240}
241
242const RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
243 ArrayRef<const Record *> UnsortedClasses) {
244 detail::RecordKeeperImpl &RKImpl = RK.getImpl();
245 if (UnsortedClasses.empty())
246 return &RKImpl.AnyRecord;
247
248 FoldingSet<RecordRecTy> &ThePool = RKImpl.RecordTypePool;
249
250 SmallVector<const Record *, 4> Classes(UnsortedClasses);
251 llvm::sort(Classes, [](const Record *LHS, const Record *RHS) {
252 return LHS->getNameInitAsString() < RHS->getNameInitAsString();
253 });
254
256 ProfileRecordRecTy(ID, Classes);
257
258 void *IP = nullptr;
259 if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
260 return Ty;
261
262#ifndef NDEBUG
263 // Check for redundancy.
264 for (unsigned i = 0; i < Classes.size(); ++i) {
265 for (unsigned j = 0; j < Classes.size(); ++j) {
266 assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
267 }
268 assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
269 }
270#endif
271
272 void *Mem = RKImpl.Allocator.Allocate(
273 totalSizeToAlloc<const Record *>(Classes.size()), alignof(RecordRecTy));
274 RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes);
275 ThePool.InsertNode(Ty, IP);
276 return Ty;
277}
278
279const RecordRecTy *RecordRecTy::get(const Record *Class) {
280 assert(Class && "unexpected null class");
281 return get(Class->getRecords(), {Class});
282}
283
287
288std::string RecordRecTy::getAsString() const {
289 if (NumClasses == 1)
290 return getClasses()[0]->getNameInitAsString();
291
292 std::string Str = "{";
293 ListSeparator LS;
294 for (const Record *R : getClasses()) {
295 Str += LS;
296 Str += R->getNameInitAsString();
297 }
298 Str += "}";
299 return Str;
300}
301
302bool RecordRecTy::isSubClassOf(const Record *Class) const {
303 return llvm::any_of(getClasses(), [Class](const Record *MySuperClass) {
304 return MySuperClass == Class || MySuperClass->isSubClassOf(Class);
305 });
306}
307
309 if (this == RHS)
310 return true;
311
312 const auto *RTy = dyn_cast<RecordRecTy>(RHS);
313 if (!RTy)
314 return false;
315
316 return llvm::all_of(RTy->getClasses(), [this](const Record *TargetClass) {
317 return isSubClassOf(TargetClass);
318 });
319}
320
321bool RecordRecTy::typeIsA(const RecTy *RHS) const {
322 return typeIsConvertibleTo(RHS);
323}
324
326 const RecordRecTy *T2) {
327 SmallVector<const Record *, 4> CommonSuperClasses;
328 SmallVector<const Record *, 4> Stack(T1->getClasses());
329
330 while (!Stack.empty()) {
331 const Record *R = Stack.pop_back_val();
332
333 if (T2->isSubClassOf(R))
334 CommonSuperClasses.push_back(R);
335 else
336 llvm::append_range(Stack, make_first_range(R->getDirectSuperClasses()));
337 }
338
339 return RecordRecTy::get(T1->getRecordKeeper(), CommonSuperClasses);
340}
341
342const RecTy *llvm::resolveTypes(const RecTy *T1, const RecTy *T2) {
343 if (T1 == T2)
344 return T1;
345
346 if (const auto *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
347 if (const auto *RecTy2 = dyn_cast<RecordRecTy>(T2))
348 return resolveRecordTypes(RecTy1, RecTy2);
349 }
350
351 assert(T1 != nullptr && "Invalid record type");
352 if (T1->typeIsConvertibleTo(T2))
353 return T2;
354
355 assert(T2 != nullptr && "Invalid record type");
356 if (T2->typeIsConvertibleTo(T1))
357 return T1;
358
359 if (const auto *ListTy1 = dyn_cast<ListRecTy>(T1)) {
360 if (const auto *ListTy2 = dyn_cast<ListRecTy>(T2)) {
361 const RecTy *NewType =
362 resolveTypes(ListTy1->getElementType(), ListTy2->getElementType());
363 if (NewType)
364 return NewType->getListTy();
365 }
366 }
367
368 return nullptr;
369}
370
371//===----------------------------------------------------------------------===//
372// Initializer implementations
373//===----------------------------------------------------------------------===//
374
375void Init::anchor() {}
376
377#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
378LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
379#endif
380
382 if (auto *TyInit = dyn_cast<TypedInit>(this))
383 return TyInit->getType()->getRecordKeeper();
384 if (auto *ArgInit = dyn_cast<ArgumentInit>(this))
385 return ArgInit->getRecordKeeper();
386 return cast<UnsetInit>(this)->getRecordKeeper();
387}
388
390 return &RK.getImpl().TheUnsetInit;
391}
392
393const Init *UnsetInit::getCastTo(const RecTy *Ty) const { return this; }
394
396 return this;
397}
398
400 ArgAuxType Aux) {
401 auto I = Aux.index();
402 ID.AddInteger(I);
404 ID.AddInteger(std::get<ArgumentInit::Positional>(Aux));
405 if (I == ArgumentInit::Named)
406 ID.AddPointer(std::get<ArgumentInit::Named>(Aux));
407 ID.AddPointer(Value);
408}
409
411 ProfileArgumentInit(ID, Value, Aux);
412}
413
416 ProfileArgumentInit(ID, Value, Aux);
417
418 RecordKeeper &RK = Value->getRecordKeeper();
419 detail::RecordKeeperImpl &RKImpl = RK.getImpl();
420 void *IP = nullptr;
421 if (const ArgumentInit *I =
423 return I;
424
425 ArgumentInit *I = new (RKImpl.Allocator) ArgumentInit(Value, Aux);
426 RKImpl.TheArgumentInitPool.InsertNode(I, IP);
427 return I;
428}
429
431 const Init *NewValue = Value->resolveReferences(R);
432 if (NewValue != Value)
433 return cloneWithValue(NewValue);
434
435 return this;
436}
437
438BitInit *BitInit::get(RecordKeeper &RK, bool V) {
439 return V ? &RK.getImpl().TrueBitInit : &RK.getImpl().FalseBitInit;
440}
441
442const Init *BitInit::convertInitializerTo(const RecTy *Ty) const {
443 if (isa<BitRecTy>(Ty))
444 return this;
445
446 if (isa<IntRecTy>(Ty))
448
449 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
450 // Can only convert single bit.
451 if (BRT->getNumBits() == 1)
452 return BitsInit::get(getRecordKeeper(), this);
453 }
454
455 return nullptr;
456}
457
460 ID.AddInteger(Range.size());
461
462 for (const Init *I : Range)
463 ID.AddPointer(I);
464}
465
466BitsInit::BitsInit(RecordKeeper &RK, ArrayRef<const Init *> Bits)
467 : TypedInit(IK_BitsInit, BitsRecTy::get(RK, Bits.size())),
468 NumBits(Bits.size()) {
469 llvm::uninitialized_copy(Bits, getTrailingObjects());
470}
471
474 ProfileBitsInit(ID, Bits);
475
476 detail::RecordKeeperImpl &RKImpl = RK.getImpl();
477 void *IP = nullptr;
478 if (BitsInit *I = RKImpl.TheBitsInitPool.FindNodeOrInsertPos(ID, IP))
479 return I;
480
481 void *Mem = RKImpl.Allocator.Allocate(
482 totalSizeToAlloc<const Init *>(Bits.size()), alignof(BitsInit));
483 BitsInit *I = new (Mem) BitsInit(RK, Bits);
484 RKImpl.TheBitsInitPool.InsertNode(I, IP);
485 return I;
486}
487
491
493 if (isa<BitRecTy>(Ty)) {
494 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
495 return getBit(0);
496 }
497
498 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
499 // If the number of bits is right, return it. Otherwise we need to expand
500 // or truncate.
501 if (getNumBits() != BRT->getNumBits()) return nullptr;
502 return this;
503 }
504
505 if (isa<IntRecTy>(Ty)) {
506 std::optional<int64_t> Result = convertInitializerToInt();
507 if (Result)
508 return IntInit::get(getRecordKeeper(), *Result);
509 }
510
511 return nullptr;
512}
513
514std::optional<int64_t> BitsInit::convertInitializerToInt() const {
515 int64_t Result = 0;
516 for (auto [Idx, InitV] : enumerate(getBits()))
517 if (auto *Bit = dyn_cast<BitInit>(InitV))
518 Result |= static_cast<int64_t>(Bit->getValue()) << Idx;
519 else
520 return std::nullopt;
521 return Result;
522}
523
525 uint64_t Result = 0;
526 for (auto [Idx, InitV] : enumerate(getBits()))
527 if (auto *Bit = dyn_cast<BitInit>(InitV))
528 Result |= static_cast<int64_t>(Bit->getValue()) << Idx;
529 return Result;
530}
531
532const Init *
534 SmallVector<const Init *, 16> NewBits(Bits.size());
535
536 for (auto [Bit, NewBit] : zip_equal(Bits, NewBits)) {
537 if (Bit >= getNumBits())
538 return nullptr;
539 NewBit = getBit(Bit);
540 }
541 return BitsInit::get(getRecordKeeper(), NewBits);
542}
543
545 return all_of(getBits(), [](const Init *Bit) { return Bit->isComplete(); });
546}
548 return all_of(getBits(), [](const Init *Bit) { return !Bit->isComplete(); });
549}
551 return all_of(getBits(), [](const Init *Bit) { return Bit->isConcrete(); });
552}
553
554std::string BitsInit::getAsString() const {
555 std::string Result = "{ ";
556 ListSeparator LS;
557 for (const Init *Bit : reverse(getBits())) {
558 Result += LS;
559 if (Bit)
560 Result += Bit->getAsString();
561 else
562 Result += "*";
563 }
564 return Result + " }";
565}
566
567// resolveReferences - If there are any field references that refer to fields
568// that have been filled in, we can propagate the values now.
570 bool Changed = false;
572
573 const Init *CachedBitVarRef = nullptr;
574 const Init *CachedBitVarResolved = nullptr;
575
576 for (auto [CurBit, NewBit] : zip_equal(getBits(), NewBits)) {
577 NewBit = CurBit;
578
579 if (const auto *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
580 if (CurBitVar->getBitVar() != CachedBitVarRef) {
581 CachedBitVarRef = CurBitVar->getBitVar();
582 CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
583 }
584 assert(CachedBitVarResolved && "Unresolved bitvar reference");
585 NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
586 } else {
587 // getBit(0) implicitly converts int and bits<1> values to bit.
588 NewBit = CurBit->resolveReferences(R)->getBit(0);
589 }
590
591 if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
592 NewBit = CurBit;
593 Changed |= CurBit != NewBit;
594 }
595
596 if (Changed)
597 return BitsInit::get(getRecordKeeper(), NewBits);
598
599 return this;
600}
601
602IntInit *IntInit::get(RecordKeeper &RK, int64_t V) {
603 IntInit *&I = RK.getImpl().TheIntInitPool[V];
604 if (!I)
605 I = new (RK.getImpl().Allocator) IntInit(RK, V);
606 return I;
607}
608
609std::string IntInit::getAsString() const {
610 return itostr(Value);
611}
612
613static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
614 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
615 return (NumBits >= sizeof(Value) * 8) ||
616 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
617}
618
619const Init *IntInit::convertInitializerTo(const RecTy *Ty) const {
620 if (isa<IntRecTy>(Ty))
621 return this;
622
623 if (isa<BitRecTy>(Ty)) {
624 int64_t Val = getValue();
625 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
626 return BitInit::get(getRecordKeeper(), Val != 0);
627 }
628
629 if (const auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
630 int64_t Value = getValue();
631 // Make sure this bitfield is large enough to hold the integer value.
632 if (!canFitInBitfield(Value, BRT->getNumBits()))
633 return nullptr;
634
635 SmallVector<const Init *, 16> NewBits(BRT->getNumBits());
636 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
637 NewBits[i] =
638 BitInit::get(getRecordKeeper(), Value & ((i < 64) ? (1LL << i) : 0));
639
640 return BitsInit::get(getRecordKeeper(), NewBits);
641 }
642
643 return nullptr;
644}
645
647 SmallVector<const Init *, 16> NewBits(Bits.size());
648
649 for (auto [Bit, NewBit] : zip_equal(Bits, NewBits)) {
650 if (Bit >= 64)
651 return nullptr;
652
653 NewBit = BitInit::get(getRecordKeeper(), Value & (INT64_C(1) << Bit));
654 }
655 return BitsInit::get(getRecordKeeper(), NewBits);
656}
657
658AnonymousNameInit *AnonymousNameInit::get(RecordKeeper &RK, unsigned V) {
659 return new (RK.getImpl().Allocator) AnonymousNameInit(RK, V);
660}
661
665
667 return "anonymous_" + utostr(Value);
668}
669
671 auto *Old = this;
672 auto *New = R.resolve(Old);
673 New = New ? New : Old;
674 if (R.isFinal())
675 if (const auto *Anonymous = dyn_cast<AnonymousNameInit>(New))
676 return Anonymous->getNameInit();
677 return New;
678}
679
680const StringInit *StringInit::get(RecordKeeper &RK, StringRef V,
681 StringFormat Fmt) {
682 detail::RecordKeeperImpl &RKImpl = RK.getImpl();
683 auto &InitMap = Fmt == SF_String ? RKImpl.StringInitStringPool
684 : RKImpl.StringInitCodePool;
685 auto &Entry = *InitMap.try_emplace(V, nullptr).first;
686 if (!Entry.second)
687 Entry.second = new (RKImpl.Allocator) StringInit(RK, Entry.getKey(), Fmt);
688 return Entry.second;
689}
690
692 if (isa<StringRecTy>(Ty))
693 return this;
694
695 return nullptr;
696}
697
699 ArrayRef<const Init *> Elements,
700 const RecTy *EltTy) {
701 ID.AddInteger(Elements.size());
702 ID.AddPointer(EltTy);
703
704 for (const Init *E : Elements)
705 ID.AddPointer(E);
706}
707
708ListInit::ListInit(ArrayRef<const Init *> Elements, const RecTy *EltTy)
709 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
710 NumElements(Elements.size()) {
711 llvm::uninitialized_copy(Elements, getTrailingObjects());
712}
713
714const ListInit *ListInit::get(ArrayRef<const Init *> Elements,
715 const RecTy *EltTy) {
717 ProfileListInit(ID, Elements, EltTy);
718
720 void *IP = nullptr;
721 if (const ListInit *I = RK.TheListInitPool.FindNodeOrInsertPos(ID, IP))
722 return I;
723
724 assert(Elements.empty() || !isa<TypedInit>(Elements[0]) ||
725 cast<TypedInit>(Elements[0])->getType()->typeIsConvertibleTo(EltTy));
726
727 void *Mem = RK.Allocator.Allocate(
728 totalSizeToAlloc<const Init *>(Elements.size()), alignof(ListInit));
729 ListInit *I = new (Mem) ListInit(Elements, EltTy);
731 return I;
732}
733
735 const RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
736 ProfileListInit(ID, getElements(), EltTy);
737}
738
740 if (getType() == Ty)
741 return this;
742
743 if (const auto *LRT = dyn_cast<ListRecTy>(Ty)) {
745 Elements.reserve(size());
746
747 // Verify that all of the elements of the list are subclasses of the
748 // appropriate class!
749 bool Changed = false;
750 const RecTy *ElementType = LRT->getElementType();
751 for (const Init *I : getElements())
752 if (const Init *CI = I->convertInitializerTo(ElementType)) {
753 Elements.push_back(CI);
754 if (CI != I)
755 Changed = true;
756 } else {
757 return nullptr;
758 }
759
760 if (!Changed)
761 return this;
762 return ListInit::get(Elements, ElementType);
763 }
764
765 return nullptr;
766}
767
768const Record *ListInit::getElementAsRecord(unsigned Idx) const {
769 const auto *DI = dyn_cast<DefInit>(getElement(Idx));
770 if (!DI)
771 PrintFatalError("expected record type for the element with index " +
772 Twine(Idx) + " in list " + getAsString());
773 return DI->getDef();
774}
775
778 Resolved.reserve(size());
779 bool Changed = false;
780
781 for (const Init *CurElt : getElements()) {
782 const Init *E = CurElt->resolveReferences(R);
783 Changed |= E != CurElt;
784 Resolved.push_back(E);
785 }
786
787 if (Changed)
788 return ListInit::get(Resolved, getElementType());
789 return this;
790}
791
793 return all_of(*this,
794 [](const Init *Element) { return Element->isComplete(); });
795}
796
798 return all_of(*this,
799 [](const Init *Element) { return Element->isConcrete(); });
800}
801
802std::string ListInit::getAsString() const {
803 std::string Result = "[";
804 ListSeparator LS;
805 for (const Init *Element : *this) {
806 Result += LS;
807 Result += Element->getAsString();
808 }
809 return Result + "]";
810}
811
812const Init *OpInit::getBit(unsigned Bit) const {
814 return this;
815 return VarBitInit::get(this, Bit);
816}
817
818static void ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode,
819 const Init *Op, const RecTy *Type) {
820 ID.AddInteger(Opcode);
821 ID.AddPointer(Op);
822 ID.AddPointer(Type);
823}
824
825const UnOpInit *UnOpInit::get(UnaryOp Opc, const Init *LHS, const RecTy *Type) {
827 ProfileUnOpInit(ID, Opc, LHS, Type);
828
829 detail::RecordKeeperImpl &RK = Type->getRecordKeeper().getImpl();
830 void *IP = nullptr;
831 if (const UnOpInit *I = RK.TheUnOpInitPool.FindNodeOrInsertPos(ID, IP))
832 return I;
833
834 UnOpInit *I = new (RK.Allocator) UnOpInit(Opc, LHS, Type);
836 return I;
837}
838
842
843const Init *UnOpInit::Fold(const Record *CurRec, bool IsFinal) const {
845 switch (getOpcode()) {
846 case REPR:
847 if (LHS->isConcrete()) {
848 // If it is a Record, print the full content.
849 if (const auto *Def = dyn_cast<DefInit>(LHS)) {
850 std::string S;
851 raw_string_ostream OS(S);
852 OS << *Def->getDef();
853 return StringInit::get(RK, S);
854 } else {
855 // Otherwise, print the value of the variable.
856 //
857 // NOTE: we could recursively !repr the elements of a list,
858 // but that could produce a lot of output when printing a
859 // defset.
860 return StringInit::get(RK, LHS->getAsString());
861 }
862 }
863 break;
864 case TOLOWER:
865 if (const auto *LHSs = dyn_cast<StringInit>(LHS))
866 return StringInit::get(RK, LHSs->getValue().lower());
867 break;
868 case TOUPPER:
869 if (const auto *LHSs = dyn_cast<StringInit>(LHS))
870 return StringInit::get(RK, LHSs->getValue().upper());
871 break;
872 case CAST:
873 if (isa<StringRecTy>(getType())) {
874 if (const auto *LHSs = dyn_cast<StringInit>(LHS))
875 return LHSs;
876
877 if (const auto *LHSd = dyn_cast<DefInit>(LHS))
878 return StringInit::get(RK, LHSd->getAsString());
879
880 if (const auto *LHSi = dyn_cast_or_null<IntInit>(
881 LHS->convertInitializerTo(IntRecTy::get(RK))))
882 return StringInit::get(RK, LHSi->getAsString());
883
884 } else if (isa<RecordRecTy>(getType())) {
885 if (const auto *Name = dyn_cast<StringInit>(LHS)) {
886 const Record *D = RK.getDef(Name->getValue());
887 if (!D && CurRec) {
888 // Self-references are allowed, but their resolution is delayed until
889 // the final resolve to ensure that we get the correct type for them.
890 auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
891 if (Name == CurRec->getNameInit() ||
892 (Anonymous && Name == Anonymous->getNameInit())) {
893 if (!IsFinal)
894 break;
895 D = CurRec;
896 }
897 }
898
899 auto PrintFatalErrorHelper = [CurRec](const Twine &T) {
900 if (CurRec)
901 PrintFatalError(CurRec->getLoc(), T);
902 else
904 };
905
906 if (!D) {
907 if (IsFinal) {
908 PrintFatalErrorHelper(Twine("Undefined reference to record: '") +
909 Name->getValue() + "'\n");
910 }
911 break;
912 }
913
914 DefInit *DI = D->getDefInit();
915 if (!DI->getType()->typeIsA(getType())) {
916 PrintFatalErrorHelper(Twine("Expected type '") +
917 getType()->getAsString() + "', got '" +
918 DI->getType()->getAsString() + "' in: " +
919 getAsString() + "\n");
920 }
921 return DI;
922 }
923 }
924
925 if (const Init *NewInit = LHS->convertInitializerTo(getType()))
926 return NewInit;
927 break;
928
929 case INITIALIZED:
930 if (isa<UnsetInit>(LHS))
931 return IntInit::get(RK, 0);
932 if (LHS->isConcrete())
933 return IntInit::get(RK, 1);
934 break;
935
936 case NOT:
937 if (const auto *LHSi = dyn_cast_or_null<IntInit>(
938 LHS->convertInitializerTo(IntRecTy::get(RK))))
939 return IntInit::get(RK, LHSi->getValue() ? 0 : 1);
940 break;
941
942 case HEAD:
943 if (const auto *LHSl = dyn_cast<ListInit>(LHS)) {
944 assert(!LHSl->empty() && "Empty list in head");
945 return LHSl->getElement(0);
946 }
947 break;
948
949 case TAIL:
950 if (const auto *LHSl = dyn_cast<ListInit>(LHS)) {
951 assert(!LHSl->empty() && "Empty list in tail");
952 // Note the slice(1). We can't just pass the result of getElements()
953 // directly.
954 return ListInit::get(LHSl->getElements().slice(1),
955 LHSl->getElementType());
956 }
957 break;
958
959 case SIZE:
960 if (const auto *LHSl = dyn_cast<ListInit>(LHS))
961 return IntInit::get(RK, LHSl->size());
962 if (const auto *LHSd = dyn_cast<DagInit>(LHS))
963 return IntInit::get(RK, LHSd->arg_size());
964 if (const auto *LHSs = dyn_cast<StringInit>(LHS))
965 return IntInit::get(RK, LHSs->getValue().size());
966 break;
967
968 case EMPTY:
969 if (const auto *LHSl = dyn_cast<ListInit>(LHS))
970 return IntInit::get(RK, LHSl->empty());
971 if (const auto *LHSd = dyn_cast<DagInit>(LHS))
972 return IntInit::get(RK, LHSd->arg_empty());
973 if (const auto *LHSs = dyn_cast<StringInit>(LHS))
974 return IntInit::get(RK, LHSs->getValue().empty());
975 break;
976
977 case GETDAGOP:
978 if (const auto *Dag = dyn_cast<DagInit>(LHS)) {
979 // TI is not necessarily a def due to the late resolution in multiclasses,
980 // but has to be a TypedInit.
981 auto *TI = cast<TypedInit>(Dag->getOperator());
982 if (!TI->getType()->typeIsA(getType())) {
983 PrintFatalError(CurRec->getLoc(),
984 Twine("Expected type '") + getType()->getAsString() +
985 "', got '" + TI->getType()->getAsString() +
986 "' in: " + getAsString() + "\n");
987 } else {
988 return Dag->getOperator();
989 }
990 }
991 break;
992
993 case GETDAGOPNAME:
994 if (const auto *Dag = dyn_cast<DagInit>(LHS)) {
995 return Dag->getName();
996 }
997 break;
998
999 case LOG2:
1000 if (const auto *LHSi = dyn_cast_or_null<IntInit>(
1001 LHS->convertInitializerTo(IntRecTy::get(RK)))) {
1002 int64_t LHSv = LHSi->getValue();
1003 if (LHSv <= 0) {
1004 PrintFatalError(CurRec->getLoc(),
1005 "Illegal operation: logtwo is undefined "
1006 "on arguments less than or equal to 0");
1007 } else {
1008 uint64_t Log = Log2_64(LHSv);
1009 assert(Log <= INT64_MAX &&
1010 "Log of an int64_t must be smaller than INT64_MAX");
1011 return IntInit::get(RK, static_cast<int64_t>(Log));
1012 }
1013 }
1014 break;
1015
1016 case LISTFLATTEN:
1017 if (const auto *LHSList = dyn_cast<ListInit>(LHS)) {
1018 const auto *InnerListTy = dyn_cast<ListRecTy>(LHSList->getElementType());
1019 // list of non-lists, !listflatten() is a NOP.
1020 if (!InnerListTy)
1021 return LHS;
1022
1023 auto Flatten =
1024 [](const ListInit *List) -> std::optional<std::vector<const Init *>> {
1025 std::vector<const Init *> Flattened;
1026 // Concatenate elements of all the inner lists.
1027 for (const Init *InnerInit : List->getElements()) {
1028 const auto *InnerList = dyn_cast<ListInit>(InnerInit);
1029 if (!InnerList)
1030 return std::nullopt;
1031 llvm::append_range(Flattened, InnerList->getElements());
1032 };
1033 return Flattened;
1034 };
1035
1036 auto Flattened = Flatten(LHSList);
1037 if (Flattened)
1038 return ListInit::get(*Flattened, InnerListTy->getElementType());
1039 }
1040 break;
1041 }
1042 return this;
1043}
1044
1046 const Init *lhs = LHS->resolveReferences(R);
1047
1048 if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
1049 return (UnOpInit::get(getOpcode(), lhs, getType()))
1050 ->Fold(R.getCurrentRecord(), R.isFinal());
1051 return this;
1052}
1053
1054std::string UnOpInit::getAsString() const {
1055 std::string Result;
1056 switch (getOpcode()) {
1057 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
1058 case NOT: Result = "!not"; break;
1059 case HEAD: Result = "!head"; break;
1060 case TAIL: Result = "!tail"; break;
1061 case SIZE: Result = "!size"; break;
1062 case EMPTY: Result = "!empty"; break;
1063 case GETDAGOP: Result = "!getdagop"; break;
1064 case GETDAGOPNAME:
1065 Result = "!getdagopname";
1066 break;
1067 case LOG2 : Result = "!logtwo"; break;
1068 case LISTFLATTEN:
1069 Result = "!listflatten";
1070 break;
1071 case REPR:
1072 Result = "!repr";
1073 break;
1074 case TOLOWER:
1075 Result = "!tolower";
1076 break;
1077 case TOUPPER:
1078 Result = "!toupper";
1079 break;
1080 case INITIALIZED:
1081 Result = "!initialized";
1082 break;
1083 }
1084 return Result + "(" + LHS->getAsString() + ")";
1085}
1086
1087static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode,
1088 const Init *LHS, const Init *RHS,
1089 const RecTy *Type) {
1090 ID.AddInteger(Opcode);
1091 ID.AddPointer(LHS);
1092 ID.AddPointer(RHS);
1093 ID.AddPointer(Type);
1094}
1095
1096const BinOpInit *BinOpInit::get(BinaryOp Opc, const Init *LHS, const Init *RHS,
1097 const RecTy *Type) {
1099 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
1100
1101 detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl();
1102 void *IP = nullptr;
1103 if (const BinOpInit *I = RK.TheBinOpInitPool.FindNodeOrInsertPos(ID, IP))
1104 return I;
1105
1106 BinOpInit *I = new (RK.Allocator) BinOpInit(Opc, LHS, RHS, Type);
1108 return I;
1109}
1110
1114
1116 const StringInit *I1) {
1118 Concat.append(I1->getValue());
1119 return StringInit::get(
1120 I0->getRecordKeeper(), Concat,
1121 StringInit::determineFormat(I0->getFormat(), I1->getFormat()));
1122}
1123
1124static const StringInit *interleaveStringList(const ListInit *List,
1125 const StringInit *Delim) {
1126 if (List->size() == 0)
1127 return StringInit::get(List->getRecordKeeper(), "");
1128 const auto *Element = dyn_cast<StringInit>(List->getElement(0));
1129 if (!Element)
1130 return nullptr;
1131 SmallString<80> Result(Element->getValue());
1133
1134 for (const Init *Elem : List->getElements().drop_front()) {
1135 Result.append(Delim->getValue());
1136 const auto *Element = dyn_cast<StringInit>(Elem);
1137 if (!Element)
1138 return nullptr;
1139 Result.append(Element->getValue());
1140 Fmt = StringInit::determineFormat(Fmt, Element->getFormat());
1141 }
1142 return StringInit::get(List->getRecordKeeper(), Result, Fmt);
1143}
1144
1145static const StringInit *interleaveIntList(const ListInit *List,
1146 const StringInit *Delim) {
1147 RecordKeeper &RK = List->getRecordKeeper();
1148 if (List->size() == 0)
1149 return StringInit::get(RK, "");
1150 const auto *Element = dyn_cast_or_null<IntInit>(
1151 List->getElement(0)->convertInitializerTo(IntRecTy::get(RK)));
1152 if (!Element)
1153 return nullptr;
1154 SmallString<80> Result(Element->getAsString());
1155
1156 for (const Init *Elem : List->getElements().drop_front()) {
1157 Result.append(Delim->getValue());
1158 const auto *Element = dyn_cast_or_null<IntInit>(
1159 Elem->convertInitializerTo(IntRecTy::get(RK)));
1160 if (!Element)
1161 return nullptr;
1162 Result.append(Element->getAsString());
1163 }
1164 return StringInit::get(RK, Result);
1165}
1166
1167const Init *BinOpInit::getStrConcat(const Init *I0, const Init *I1) {
1168 // Shortcut for the common case of concatenating two strings.
1169 if (const auto *I0s = dyn_cast<StringInit>(I0))
1170 if (const auto *I1s = dyn_cast<StringInit>(I1))
1171 return ConcatStringInits(I0s, I1s);
1172 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1,
1174}
1175
1177 const ListInit *RHS) {
1179 llvm::append_range(Args, *LHS);
1180 llvm::append_range(Args, *RHS);
1181 return ListInit::get(Args, LHS->getElementType());
1182}
1183
1184const Init *BinOpInit::getListConcat(const TypedInit *LHS, const Init *RHS) {
1185 assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list");
1186
1187 // Shortcut for the common case of concatenating two lists.
1188 if (const auto *LHSList = dyn_cast<ListInit>(LHS))
1189 if (const auto *RHSList = dyn_cast<ListInit>(RHS))
1190 return ConcatListInits(LHSList, RHSList);
1191 return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType());
1192}
1193
1194std::optional<bool> BinOpInit::CompareInit(unsigned Opc, const Init *LHS,
1195 const Init *RHS) const {
1196 // First see if we have two bit, bits, or int.
1197 const auto *LHSi = dyn_cast_or_null<IntInit>(
1198 LHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1199 const auto *RHSi = dyn_cast_or_null<IntInit>(
1200 RHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1201
1202 if (LHSi && RHSi) {
1203 bool Result;
1204 switch (Opc) {
1205 case EQ:
1206 Result = LHSi->getValue() == RHSi->getValue();
1207 break;
1208 case NE:
1209 Result = LHSi->getValue() != RHSi->getValue();
1210 break;
1211 case LE:
1212 Result = LHSi->getValue() <= RHSi->getValue();
1213 break;
1214 case LT:
1215 Result = LHSi->getValue() < RHSi->getValue();
1216 break;
1217 case GE:
1218 Result = LHSi->getValue() >= RHSi->getValue();
1219 break;
1220 case GT:
1221 Result = LHSi->getValue() > RHSi->getValue();
1222 break;
1223 default:
1224 llvm_unreachable("unhandled comparison");
1225 }
1226 return Result;
1227 }
1228
1229 // Next try strings.
1230 const auto *LHSs = dyn_cast<StringInit>(LHS);
1231 const auto *RHSs = dyn_cast<StringInit>(RHS);
1232
1233 if (LHSs && RHSs) {
1234 bool Result;
1235 switch (Opc) {
1236 case EQ:
1237 Result = LHSs->getValue() == RHSs->getValue();
1238 break;
1239 case NE:
1240 Result = LHSs->getValue() != RHSs->getValue();
1241 break;
1242 case LE:
1243 Result = LHSs->getValue() <= RHSs->getValue();
1244 break;
1245 case LT:
1246 Result = LHSs->getValue() < RHSs->getValue();
1247 break;
1248 case GE:
1249 Result = LHSs->getValue() >= RHSs->getValue();
1250 break;
1251 case GT:
1252 Result = LHSs->getValue() > RHSs->getValue();
1253 break;
1254 default:
1255 llvm_unreachable("unhandled comparison");
1256 }
1257 return Result;
1258 }
1259
1260 // Finally, !eq and !ne can be used with records.
1261 if (Opc == EQ || Opc == NE) {
1262 const auto *LHSd = dyn_cast<DefInit>(LHS);
1263 const auto *RHSd = dyn_cast<DefInit>(RHS);
1264 if (LHSd && RHSd)
1265 return (Opc == EQ) ? LHSd == RHSd : LHSd != RHSd;
1266 }
1267
1268 return std::nullopt;
1269}
1270
1271static std::optional<unsigned>
1272getDagArgNoByKey(const DagInit *Dag, const Init *Key, std::string &Error) {
1273 // Accessor by index
1274 if (const auto *Idx = dyn_cast<IntInit>(Key)) {
1275 int64_t Pos = Idx->getValue();
1276 if (Pos < 0) {
1277 // The index is negative.
1278 Error =
1279 (Twine("index ") + std::to_string(Pos) + Twine(" is negative")).str();
1280 return std::nullopt;
1281 }
1282 if (Pos >= Dag->getNumArgs()) {
1283 // The index is out-of-range.
1284 Error = (Twine("index ") + std::to_string(Pos) +
1285 " is out of range (dag has " +
1286 std::to_string(Dag->getNumArgs()) + " arguments)")
1287 .str();
1288 return std::nullopt;
1289 }
1290 return Pos;
1291 }
1293 // Accessor by name
1294 const auto *Name = dyn_cast<StringInit>(Key);
1295 auto ArgNo = Dag->getArgNo(Name->getValue());
1296 if (!ArgNo) {
1297 // The key is not found.
1298 Error = (Twine("key '") + Name->getValue() + Twine("' is not found")).str();
1299 return std::nullopt;
1300 }
1301 return *ArgNo;
1302}
1303
1304const Init *BinOpInit::Fold(const Record *CurRec) const {
1305 switch (getOpcode()) {
1306 case CONCAT: {
1307 const auto *LHSs = dyn_cast<DagInit>(LHS);
1308 const auto *RHSs = dyn_cast<DagInit>(RHS);
1309 if (LHSs && RHSs) {
1310 const auto *LOp = dyn_cast<DefInit>(LHSs->getOperator());
1311 const auto *ROp = dyn_cast<DefInit>(RHSs->getOperator());
1312 if ((!LOp && !isa<UnsetInit>(LHSs->getOperator())) ||
1313 (!ROp && !isa<UnsetInit>(RHSs->getOperator())))
1314 break;
1315 if (LOp && ROp && LOp->getDef() != ROp->getDef()) {
1316 PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
1317 LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
1318 "'");
1319 }
1320 const Init *Op = LOp ? LOp : ROp;
1321 if (!Op)
1323
1325 llvm::append_range(Args, LHSs->getArgAndNames());
1326 llvm::append_range(Args, RHSs->getArgAndNames());
1327 // Use the name of the LHS DAG if it's set, otherwise the name of the RHS.
1328 const auto *NameInit = LHSs->getName();
1329 if (!NameInit)
1330 NameInit = RHSs->getName();
1331 return DagInit::get(Op, NameInit, Args);
1332 }
1333 break;
1334 }
1335 case MATCH: {
1336 const auto *StrInit = dyn_cast<StringInit>(LHS);
1337 if (!StrInit)
1338 return this;
1339
1340 const auto *RegexInit = dyn_cast<StringInit>(RHS);
1341 if (!RegexInit)
1342 return this;
1343
1344 StringRef RegexStr = RegexInit->getValue();
1345 llvm::Regex Matcher(RegexStr);
1346 if (!Matcher.isValid())
1347 PrintFatalError(Twine("invalid regex '") + RegexStr + Twine("'"));
1348
1349 return BitInit::get(LHS->getRecordKeeper(),
1350 Matcher.match(StrInit->getValue()));
1351 }
1352 case LISTCONCAT: {
1353 const auto *LHSs = dyn_cast<ListInit>(LHS);
1354 const auto *RHSs = dyn_cast<ListInit>(RHS);
1355 if (LHSs && RHSs) {
1357 llvm::append_range(Args, *LHSs);
1358 llvm::append_range(Args, *RHSs);
1359 return ListInit::get(Args, LHSs->getElementType());
1360 }
1361 break;
1362 }
1363 case LISTSPLAT: {
1364 const auto *Value = dyn_cast<TypedInit>(LHS);
1365 const auto *Count = dyn_cast<IntInit>(RHS);
1366 if (Value && Count) {
1367 if (Count->getValue() < 0)
1368 PrintFatalError(Twine("!listsplat count ") + Count->getAsString() +
1369 " is negative");
1370 SmallVector<const Init *, 8> Args(Count->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);
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);
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);
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 =
2197 return I;
2198
2199 ExistsOpInit *I = new (RK.Allocator) ExistsOpInit(CheckType, Expr);
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 =
2272 return I;
2273
2274 InstancesOpInit *I = new (RK.Allocator) InstancesOpInit(Type, Regex);
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);
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);
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:661
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:57
#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:458
static bool canFitInBitfield(int64_t Value, unsigned NumBits)
Definition Record.cpp:613
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:698
static std::optional< unsigned > getDagArgNoByKey(const DagInit *Dag, const Init *Key, std::string &Error)
Definition Record.cpp:1272
static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, const Init *LHS, const Init *RHS, const RecTy *Type)
Definition Record.cpp:1087
static const StringInit * ConcatStringInits(const StringInit *I0, const StringInit *I1)
Definition Record.cpp:1115
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:1176
static const StringInit * interleaveStringList(const ListInit *List, const StringInit *Delim)
Definition Record.cpp:1124
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:818
static void ProfileArgumentInit(FoldingSetNodeID &ID, const Init *Value, ArgAuxType Aux)
Definition Record.cpp:399
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:325
static void ProfileRecordRecTy(FoldingSetNodeID &ID, ArrayRef< const Record * > Classes)
Definition Record.cpp:230
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:1145
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:658
const StringInit * getNameInit() const
Definition Record.cpp:662
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:670
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:666
const ArgumentInit * cloneWithValue(const Init *Value) const
Definition Record.h:528
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:410
static const ArgumentInit * get(const Init *Value, ArgAuxType Aux)
Definition Record.cpp:414
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:430
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
static const BinOpInit * get(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
Definition Record.cpp:1096
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:1111
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:1167
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:1194
const Init * getLHS() const
Definition Record.h:942
static const Init * getListConcat(const TypedInit *lhs, const Init *rhs)
Definition Record.cpp:1184
const Init * Fold(const Record *CurRec) const
Definition Record.cpp:1304
'true'/'false' - Represent a concrete initializer for a bit.
Definition Record.h:556
static BitInit * get(RecordKeeper &RK, bool V)
Definition Record.cpp:438
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:442
'bit' - Represent a single bit
Definition Record.h:113
static const BitRecTy * get(RecordKeeper &RK)
Definition Record.cpp:151
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:155
'{ a, b, c }' - Represents an initializer for a BitsRecTy value.
Definition Record.h:591
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:488
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:554
bool isComplete() const override
Is this a complete value with no unset (uninitialized) subvalues?
Definition Record.cpp:544
unsigned getNumBits() const
Definition Record.h:612
std::optional< int64_t > convertInitializerToInt() const
Definition Record.cpp:514
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:533
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:569
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:492
ArrayRef< const Init * > getBits() const
Definition Record.h:629
uint64_t convertKnownBitsToInt() const
Definition Record.cpp:524
bool allInComplete() const
Definition Record.cpp:547
static BitsInit * get(RecordKeeper &RK, ArrayRef< const Init * > Range)
Definition Record.cpp:472
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations?
Definition Record.cpp:550
'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:177
static const BitsRecTy * get(RecordKeeper &RK, unsigned Sz)
Definition Record.cpp:163
std::string getAsString() const override
Definition Record.cpp:173
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:226
static const DagRecTy * get(RecordKeeper &RK)
Definition Record.cpp:222
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:512
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition FoldingSet.h:504
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition FoldingSet.h:535
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:378
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:381
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:602
const Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This function is used to implement the bit range selection operator.
Definition Record.cpp:646
int64_t getValue() const
Definition Record.h:651
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:609
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:619
'int' - Represent an integer value of no particular size
Definition Record.h:152
static const IntRecTy * get(RecordKeeper &RK)
Definition Record.cpp:184
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:188
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:802
const RecTy * getElementType() const
Definition Record.h:784
static const ListInit * get(ArrayRef< const Init * > Range, const RecTy *EltTy)
Definition Record.cpp:714
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations?
Definition Record.cpp:797
bool isComplete() const override
Is this a complete value with no unset (uninitialized) subvalues?
Definition Record.cpp:792
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:776
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:739
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:734
const Record * getElementAsRecord(unsigned Idx) const
Definition Record.cpp:768
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:216
std::string getAsString() const override
Definition Record.cpp:206
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:210
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:812
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:149
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:144
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:135
const ListRecTy * getListTy() const
Returns the type representing list<thistype>.
Definition Record.cpp:138
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:308
bool isSubClassOf(const Record *Class) const
Definition Record.cpp:302
ArrayRef< const Record * > getClasses() const
Definition Record.h:261
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:284
friend class Record
Definition Record.h:236
std::string getAsString() const override
Definition Record.cpp:288
bool typeIsA(const RecTy *RHS) const override
Return true if 'this' type is equal to or a subtype of RHS.
Definition Record.cpp:321
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:242
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
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
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:22
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:680
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:691
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition StringMap.h:381
'string' - Represent an string value
Definition Record.h:170
std::string getAsString() const override
Definition Record.cpp:197
static const StringRecTy * get(RecordKeeper &RK)
Definition Record.cpp:193
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:201
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:222
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:46
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:825
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:839
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:1045
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:1054
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
Definition Record.cpp:843
'?' - 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:393
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:395
static UnsetInit * get(RecordKeeper &RK)
Get the singleton unset Init.
Definition Record.cpp:389
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:532
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:831
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:1739
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
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:1669
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:841
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:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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:2208
std::string utostr(uint64_t X, bool isNeg=false)
auto uninitialized_copy(R &&Src, IterTy Dst)
Definition STLExtras.h:2111
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:337
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:753
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:1746
void PrintWarning(const Twine &Msg)
Definition Error.cpp:90
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1399
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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:547
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:1409
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:559
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:1772
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition Record.cpp:342
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
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:870
#define N
This class represents the internal implementation of the RecordKeeper.
Definition Record.cpp:53
StringMap< const StringInit *, BumpPtrAllocator & > StringInitCodePool
Definition Record.cpp:77
StringRecTy SharedStringRecTy
Definition Record.cpp:65
FoldingSet< ListInit > TheListInitPool
Definition Record.cpp:78
FoldingSet< BitsInit > TheBitsInitPool
Definition Record.cpp:74
BumpPtrAllocator Allocator
Definition Record.cpp:61
std::map< int64_t, IntInit * > TheIntInitPool
Definition Record.cpp:75
FoldingSet< ArgumentInit > TheArgumentInitPool
Definition Record.cpp:73
RecordRecTy AnyRecord
Definition Record.cpp:68
FoldingSet< UnOpInit > TheUnOpInitPool
Definition Record.cpp:79
DenseMap< std::pair< const Init *, const StringInit * >, FieldInit * > TheFieldInitPool
Definition Record.cpp:91
std::vector< BitsRecTy * > SharedBitsRecTys
Definition Record.cpp:62
FoldingSet< RecordRecTy > RecordTypePool
Definition Record.cpp:94
FoldingSet< VarDefInit > TheVarDefInitPool
Definition Record.cpp:89
RecordKeeperImpl(RecordKeeper &RK)
Definition Record.cpp:54
StringMap< const StringInit *, BumpPtrAllocator & > StringInitStringPool
Definition Record.cpp:76
FoldingSet< TernOpInit > TheTernOpInitPool
Definition Record.cpp:81
FoldingSet< InstancesOpInit > TheInstancesOpInitPool
Definition Record.cpp:85
void dumpAllocationStats(raw_ostream &OS) const
Definition Record.cpp:102
FoldingSet< ExistsOpInit > TheExistsOpInitPool
Definition Record.cpp:84
DenseMap< std::pair< const RecTy *, const Init * >, VarInit * > TheVarInitPool
Definition Record.cpp:86
FoldingSet< IsAOpInit > TheIsAOpInitPool
Definition Record.cpp:83
FoldingSet< DagInit > TheDagInitPool
Definition Record.cpp:93
DenseMap< std::pair< const TypedInit *, unsigned >, VarBitInit * > TheVarBitInitPool
Definition Record.cpp:88
FoldingSet< CondOpInit > TheCondOpInitPool
Definition Record.cpp:92
FoldingSet< BinOpInit > TheBinOpInitPool
Definition Record.cpp:80
FoldingSet< FoldOpInit > TheFoldOpInitPool
Definition Record.cpp:82
Sorting predicate to sort record pointers by name.
Definition Record.h:2089