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