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