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