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 *Count = dyn_cast<IntInit>(RHS);
1367 if (Value && Count) {
1368 if (Count->getValue() < 0)
1369 PrintFatalError(Twine("!listsplat count ") + Count->getAsString() +
1370 " is negative");
1371 SmallVector<const Init *, 8> Args(Count->getValue(), Value);
1372 return ListInit::get(Args, Value->getType());
1373 }
1374 break;
1375 }
1376 case LISTREMOVE: {
1377 const auto *LHSs = dyn_cast<ListInit>(LHS);
1378 const auto *RHSs = dyn_cast<ListInit>(RHS);
1379 if (LHSs && RHSs) {
1381 for (const Init *EltLHS : *LHSs) {
1382 bool Found = false;
1383 for (const Init *EltRHS : *RHSs) {
1384 if (std::optional<bool> Result = CompareInit(EQ, EltLHS, EltRHS)) {
1385 if (*Result) {
1386 Found = true;
1387 break;
1388 }
1389 }
1390 }
1391 if (!Found)
1392 Args.push_back(EltLHS);
1393 }
1394 return ListInit::get(Args, LHSs->getElementType());
1395 }
1396 break;
1397 }
1398 case LISTELEM: {
1399 const auto *TheList = dyn_cast<ListInit>(LHS);
1400 const auto *Idx = dyn_cast<IntInit>(RHS);
1401 if (!TheList || !Idx)
1402 break;
1403 auto i = Idx->getValue();
1404 if (i < 0 || i >= (ssize_t)TheList->size())
1405 break;
1406 return TheList->getElement(i);
1407 }
1408 case LISTSLICE: {
1409 const auto *TheList = dyn_cast<ListInit>(LHS);
1410 const auto *SliceIdxs = dyn_cast<ListInit>(RHS);
1411 if (!TheList || !SliceIdxs)
1412 break;
1414 Args.reserve(SliceIdxs->size());
1415 for (auto *I : *SliceIdxs) {
1416 auto *II = dyn_cast<IntInit>(I);
1417 if (!II)
1418 goto unresolved;
1419 auto i = II->getValue();
1420 if (i < 0 || i >= (ssize_t)TheList->size())
1421 goto unresolved;
1422 Args.push_back(TheList->getElement(i));
1423 }
1424 return ListInit::get(Args, TheList->getElementType());
1425 }
1426 case RANGEC: {
1427 const auto *LHSi = dyn_cast<IntInit>(LHS);
1428 const auto *RHSi = dyn_cast<IntInit>(RHS);
1429 if (!LHSi || !RHSi)
1430 break;
1431
1432 int64_t Start = LHSi->getValue();
1433 int64_t End = RHSi->getValue();
1435 if (getOpcode() == RANGEC) {
1436 // Closed interval
1437 if (Start <= End) {
1438 // Ascending order
1439 Args.reserve(End - Start + 1);
1440 for (auto i = Start; i <= End; ++i)
1441 Args.push_back(IntInit::get(getRecordKeeper(), i));
1442 } else {
1443 // Descending order
1444 Args.reserve(Start - End + 1);
1445 for (auto i = Start; i >= End; --i)
1446 Args.push_back(IntInit::get(getRecordKeeper(), i));
1447 }
1448 } else if (Start < End) {
1449 // Half-open interval (excludes `End`)
1450 Args.reserve(End - Start);
1451 for (auto i = Start; i < End; ++i)
1452 Args.push_back(IntInit::get(getRecordKeeper(), i));
1453 } else {
1454 // Empty set
1455 }
1456 return ListInit::get(Args, LHSi->getType());
1457 }
1458 case STRCONCAT: {
1459 const auto *LHSs = dyn_cast<StringInit>(LHS);
1460 const auto *RHSs = dyn_cast<StringInit>(RHS);
1461 if (LHSs && RHSs)
1462 return ConcatStringInits(LHSs, RHSs);
1463 break;
1464 }
1465 case INTERLEAVE: {
1466 const auto *List = dyn_cast<ListInit>(LHS);
1467 const auto *Delim = dyn_cast<StringInit>(RHS);
1468 if (List && Delim) {
1469 const StringInit *Result;
1470 if (isa<StringRecTy>(List->getElementType()))
1471 Result = interleaveStringList(List, Delim);
1472 else
1473 Result = interleaveIntList(List, Delim);
1474 if (Result)
1475 return Result;
1476 }
1477 break;
1478 }
1479 case EQ:
1480 case NE:
1481 case LE:
1482 case LT:
1483 case GE:
1484 case GT: {
1485 if (std::optional<bool> Result = CompareInit(getOpcode(), LHS, RHS))
1486 return BitInit::get(getRecordKeeper(), *Result);
1487 break;
1488 }
1489 case GETDAGARG: {
1490 const auto *Dag = dyn_cast<DagInit>(LHS);
1491 if (Dag && isa<IntInit, StringInit>(RHS)) {
1492 std::string Error;
1493 auto ArgNo = getDagArgNoByKey(Dag, RHS, Error);
1494 if (!ArgNo)
1495 PrintFatalError(CurRec->getLoc(), "!getdagarg " + Error);
1496
1497 assert(*ArgNo < Dag->getNumArgs());
1498
1499 const Init *Arg = Dag->getArg(*ArgNo);
1500 if (const auto *TI = dyn_cast<TypedInit>(Arg))
1501 if (!TI->getType()->typeIsConvertibleTo(getType()))
1502 return UnsetInit::get(Dag->getRecordKeeper());
1503 return Arg;
1504 }
1505 break;
1506 }
1507 case GETDAGNAME: {
1508 const auto *Dag = dyn_cast<DagInit>(LHS);
1509 const auto *Idx = dyn_cast<IntInit>(RHS);
1510 if (Dag && Idx) {
1511 int64_t Pos = Idx->getValue();
1512 if (Pos < 0 || Pos >= Dag->getNumArgs()) {
1513 // The index is out-of-range.
1514 PrintError(CurRec->getLoc(),
1515 Twine("!getdagname index is out of range 0...") +
1516 std::to_string(Dag->getNumArgs() - 1) + ": " +
1517 std::to_string(Pos));
1518 }
1519 const Init *ArgName = Dag->getArgName(Pos);
1520 if (!ArgName)
1522 return ArgName;
1523 }
1524 break;
1525 }
1526 case SETDAGOP: {
1527 const auto *Dag = dyn_cast<DagInit>(LHS);
1528 const auto *Op = dyn_cast<DefInit>(RHS);
1529 if (Dag && Op)
1530 return DagInit::get(Op, Dag->getArgs(), Dag->getArgNames());
1531 break;
1532 }
1533 case SETDAGOPNAME: {
1534 const auto *Dag = dyn_cast<DagInit>(LHS);
1535 const auto *Op = dyn_cast<StringInit>(RHS);
1536 if (Dag && Op)
1537 return DagInit::get(Dag->getOperator(), Op, Dag->getArgs(),
1538 Dag->getArgNames());
1539 break;
1540 }
1541 case ADD:
1542 case SUB:
1543 case MUL:
1544 case DIV:
1545 case AND:
1546 case OR:
1547 case XOR:
1548 case SHL:
1549 case SRA:
1550 case SRL: {
1551 const auto *LHSi = dyn_cast_or_null<IntInit>(
1552 LHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1553 const auto *RHSi = dyn_cast_or_null<IntInit>(
1554 RHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())));
1555 if (LHSi && RHSi) {
1556 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
1557 int64_t Result;
1558 switch (getOpcode()) {
1559 default: llvm_unreachable("Bad opcode!");
1560 case ADD: Result = LHSv + RHSv; break;
1561 case SUB: Result = LHSv - RHSv; break;
1562 case MUL: Result = LHSv * RHSv; break;
1563 case DIV:
1564 if (RHSv == 0)
1565 PrintFatalError(CurRec->getLoc(),
1566 "Illegal operation: division by zero");
1567 else if (LHSv == INT64_MIN && RHSv == -1)
1568 PrintFatalError(CurRec->getLoc(),
1569 "Illegal operation: INT64_MIN / -1");
1570 else
1571 Result = LHSv / RHSv;
1572 break;
1573 case AND: Result = LHSv & RHSv; break;
1574 case OR: Result = LHSv | RHSv; break;
1575 case XOR: Result = LHSv ^ RHSv; break;
1576 case SHL:
1577 if (RHSv < 0 || RHSv >= 64)
1578 PrintFatalError(CurRec->getLoc(),
1579 "Illegal operation: out of bounds shift");
1580 Result = (uint64_t)LHSv << (uint64_t)RHSv;
1581 break;
1582 case SRA:
1583 if (RHSv < 0 || RHSv >= 64)
1584 PrintFatalError(CurRec->getLoc(),
1585 "Illegal operation: out of bounds shift");
1586 Result = LHSv >> (uint64_t)RHSv;
1587 break;
1588 case SRL:
1589 if (RHSv < 0 || RHSv >= 64)
1590 PrintFatalError(CurRec->getLoc(),
1591 "Illegal operation: out of bounds shift");
1592 Result = (uint64_t)LHSv >> (uint64_t)RHSv;
1593 break;
1594 }
1595 return IntInit::get(getRecordKeeper(), Result);
1596 }
1597 break;
1598 }
1599 }
1600unresolved:
1601 return this;
1602}
1603
1605 const Init *NewLHS = LHS->resolveReferences(R);
1606
1607 unsigned Opc = getOpcode();
1608 if (Opc == AND || Opc == OR) {
1609 // Short-circuit. Regardless whether this is a logical or bitwise
1610 // AND/OR.
1611 // Ideally we could also short-circuit `!or(true, ...)`, but it's
1612 // difficult to do it right without knowing if rest of the operands
1613 // are all `bit` or not. Therefore, we're only implementing a relatively
1614 // limited version of short-circuit against all ones (`true` is casted
1615 // to 1 rather than all ones before we evaluate `!or`).
1616 if (const auto *LHSi = dyn_cast_or_null<IntInit>(
1618 if ((Opc == AND && !LHSi->getValue()) ||
1619 (Opc == OR && LHSi->getValue() == -1))
1620 return LHSi;
1621 }
1622 }
1623
1624 const Init *NewRHS = RHS->resolveReferences(R);
1625
1626 if (LHS != NewLHS || RHS != NewRHS)
1627 return (BinOpInit::get(getOpcode(), NewLHS, NewRHS, getType()))
1628 ->Fold(R.getCurrentRecord());
1629 return this;
1630}
1631
1632std::string BinOpInit::getAsString() const {
1633 std::string Result;
1634 switch (getOpcode()) {
1635 case LISTELEM:
1636 case LISTSLICE:
1637 return LHS->getAsString() + "[" + RHS->getAsString() + "]";
1638 case RANGEC:
1639 return LHS->getAsString() + "..." + RHS->getAsString();
1640 case CONCAT: Result = "!con"; break;
1641 case MATCH:
1642 Result = "!match";
1643 break;
1644 case ADD: Result = "!add"; break;
1645 case SUB: Result = "!sub"; break;
1646 case MUL: Result = "!mul"; break;
1647 case DIV: Result = "!div"; break;
1648 case AND: Result = "!and"; break;
1649 case OR: Result = "!or"; break;
1650 case XOR: Result = "!xor"; break;
1651 case SHL: Result = "!shl"; break;
1652 case SRA: Result = "!sra"; break;
1653 case SRL: Result = "!srl"; break;
1654 case EQ: Result = "!eq"; break;
1655 case NE: Result = "!ne"; break;
1656 case LE: Result = "!le"; break;
1657 case LT: Result = "!lt"; break;
1658 case GE: Result = "!ge"; break;
1659 case GT: Result = "!gt"; break;
1660 case LISTCONCAT: Result = "!listconcat"; break;
1661 case LISTSPLAT: Result = "!listsplat"; break;
1662 case LISTREMOVE:
1663 Result = "!listremove";
1664 break;
1665 case STRCONCAT: Result = "!strconcat"; break;
1666 case INTERLEAVE: Result = "!interleave"; break;
1667 case SETDAGOP: Result = "!setdagop"; break;
1668 case SETDAGOPNAME:
1669 Result = "!setdagopname";
1670 break;
1671 case GETDAGARG:
1672 Result = "!getdagarg<" + getType()->getAsString() + ">";
1673 break;
1674 case GETDAGNAME:
1675 Result = "!getdagname";
1676 break;
1677 }
1678 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
1679}
1680
1681static void ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode,
1682 const Init *LHS, const Init *MHS, const Init *RHS,
1683 const RecTy *Type) {
1684 ID.AddInteger(Opcode);
1685 ID.AddPointer(LHS);
1686 ID.AddPointer(MHS);
1687 ID.AddPointer(RHS);
1688 ID.AddPointer(Type);
1689}
1690
1691const TernOpInit *TernOpInit::get(TernaryOp Opc, const Init *LHS,
1692 const Init *MHS, const Init *RHS,
1693 const RecTy *Type) {
1695 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
1696
1697 detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl();
1698 void *IP = nullptr;
1699 if (TernOpInit *I = RK.TheTernOpInitPool.FindNodeOrInsertPos(ID, IP))
1700 return I;
1701
1702 TernOpInit *I = new (RK.Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
1703 RK.TheTernOpInitPool.InsertNode(I, IP);
1704 return I;
1705}
1706
1710
1711static const Init *ItemApply(const Init *LHS, const Init *MHSe, const Init *RHS,
1712 const Record *CurRec) {
1713 MapResolver R(CurRec);
1714 R.set(LHS, MHSe);
1715 return RHS->resolveReferences(R);
1716}
1717
1718static const Init *ForeachDagApply(const Init *LHS, const DagInit *MHSd,
1719 const Init *RHS, const Record *CurRec) {
1720 bool Change = false;
1721 const Init *Val = ItemApply(LHS, MHSd->getOperator(), RHS, CurRec);
1722 if (Val != MHSd->getOperator())
1723 Change = true;
1724
1726 for (auto [Arg, ArgName] : MHSd->getArgAndNames()) {
1727 const Init *NewArg;
1728
1729 if (const auto *Argd = dyn_cast<DagInit>(Arg))
1730 NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
1731 else
1732 NewArg = ItemApply(LHS, Arg, RHS, CurRec);
1733
1734 NewArgs.emplace_back(NewArg, ArgName);
1735 if (Arg != NewArg)
1736 Change = true;
1737 }
1738
1739 if (Change)
1740 return DagInit::get(Val, MHSd->getName(), NewArgs);
1741 return MHSd;
1742}
1743
1744// Applies RHS to all elements of MHS, using LHS as a temp variable.
1745static const Init *ForeachHelper(const Init *LHS, const Init *MHS,
1746 const Init *RHS, const RecTy *Type,
1747 const Record *CurRec) {
1748 if (const auto *MHSd = dyn_cast<DagInit>(MHS))
1749 return ForeachDagApply(LHS, MHSd, RHS, CurRec);
1750
1751 if (const auto *MHSl = dyn_cast<ListInit>(MHS)) {
1752 SmallVector<const Init *, 8> NewList(MHSl->begin(), MHSl->end());
1753
1754 for (const Init *&Item : NewList) {
1755 const Init *NewItem = ItemApply(LHS, Item, RHS, CurRec);
1756 if (NewItem != Item)
1757 Item = NewItem;
1758 }
1759 return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1760 }
1761
1762 return nullptr;
1763}
1764
1765// Evaluates RHS for all elements of MHS, using LHS as a temp variable.
1766// Creates a new list with the elements that evaluated to true.
1767static const Init *FilterHelper(const Init *LHS, const Init *MHS,
1768 const Init *RHS, const RecTy *Type,
1769 const Record *CurRec) {
1770 if (const auto *MHSl = dyn_cast<ListInit>(MHS)) {
1772
1773 for (const Init *Item : MHSl->getElements()) {
1774 const Init *Include = ItemApply(LHS, Item, RHS, CurRec);
1775 if (!Include)
1776 return nullptr;
1777 if (const auto *IncludeInt =
1778 dyn_cast_or_null<IntInit>(Include->convertInitializerTo(
1779 IntRecTy::get(LHS->getRecordKeeper())))) {
1780 if (IncludeInt->getValue())
1781 NewList.push_back(Item);
1782 } else {
1783 return nullptr;
1784 }
1785 }
1786 return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1787 }
1788
1789 return nullptr;
1790}
1791
1792const Init *TernOpInit::Fold(const Record *CurRec) const {
1794 switch (getOpcode()) {
1795 case SUBST: {
1796 const auto *LHSd = dyn_cast<DefInit>(LHS);
1797 const auto *LHSv = dyn_cast<VarInit>(LHS);
1798 const auto *LHSs = dyn_cast<StringInit>(LHS);
1799
1800 const auto *MHSd = dyn_cast<DefInit>(MHS);
1801 const auto *MHSv = dyn_cast<VarInit>(MHS);
1802 const auto *MHSs = dyn_cast<StringInit>(MHS);
1803
1804 const auto *RHSd = dyn_cast<DefInit>(RHS);
1805 const auto *RHSv = dyn_cast<VarInit>(RHS);
1806 const auto *RHSs = dyn_cast<StringInit>(RHS);
1807
1808 if (LHSd && MHSd && RHSd) {
1809 const Record *Val = RHSd->getDef();
1810 if (LHSd->getAsString() == RHSd->getAsString())
1811 Val = MHSd->getDef();
1812 return Val->getDefInit();
1813 }
1814 if (LHSv && MHSv && RHSv) {
1815 std::string Val = RHSv->getName().str();
1816 if (LHSv->getAsString() == RHSv->getAsString())
1817 Val = MHSv->getName().str();
1818 return VarInit::get(Val, getType());
1819 }
1820 if (LHSs && MHSs && RHSs) {
1821 std::string Val = RHSs->getValue().str();
1822
1823 std::string::size_type Idx = 0;
1824 while (true) {
1825 std::string::size_type Found = Val.find(LHSs->getValue(), Idx);
1826 if (Found == std::string::npos)
1827 break;
1828 Val.replace(Found, LHSs->getValue().size(), MHSs->getValue().str());
1829 Idx = Found + MHSs->getValue().size();
1830 }
1831
1832 return StringInit::get(RK, Val);
1833 }
1834 break;
1835 }
1836
1837 case FOREACH: {
1838 if (const Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
1839 return Result;
1840 break;
1841 }
1842
1843 case FILTER: {
1844 if (const Init *Result = FilterHelper(LHS, MHS, RHS, getType(), CurRec))
1845 return Result;
1846 break;
1847 }
1848
1849 case IF: {
1850 if (const auto *LHSi = dyn_cast_or_null<IntInit>(
1851 LHS->convertInitializerTo(IntRecTy::get(RK)))) {
1852 if (LHSi->getValue())
1853 return MHS;
1854 return RHS;
1855 }
1856 break;
1857 }
1858
1859 case DAG: {
1860 const auto *MHSl = dyn_cast<ListInit>(MHS);
1861 const auto *RHSl = dyn_cast<ListInit>(RHS);
1862 bool MHSok = MHSl || isa<UnsetInit>(MHS);
1863 bool RHSok = RHSl || isa<UnsetInit>(RHS);
1864
1865 if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
1866 break; // Typically prevented by the parser, but might happen with template args
1867
1868 if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
1870 unsigned Size = MHSl ? MHSl->size() : RHSl->size();
1871 for (unsigned i = 0; i != Size; ++i) {
1872 const Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get(RK);
1873 const Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get(RK);
1874 if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
1875 return this;
1876 Children.emplace_back(Node, dyn_cast<StringInit>(Name));
1877 }
1878 return DagInit::get(LHS, Children);
1879 }
1880 break;
1881 }
1882
1883 case RANGE: {
1884 const auto *LHSi = dyn_cast<IntInit>(LHS);
1885 const auto *MHSi = dyn_cast<IntInit>(MHS);
1886 const auto *RHSi = dyn_cast<IntInit>(RHS);
1887 if (!LHSi || !MHSi || !RHSi)
1888 break;
1889
1890 auto Start = LHSi->getValue();
1891 auto End = MHSi->getValue();
1892 auto Step = RHSi->getValue();
1893 if (Step == 0)
1894 PrintError(CurRec->getLoc(), "Step of !range can't be 0");
1895
1897 if (Start < End && Step > 0) {
1898 Args.reserve((End - Start) / Step);
1899 for (auto I = Start; I < End; I += Step)
1900 Args.push_back(IntInit::get(getRecordKeeper(), I));
1901 } else if (Start > End && Step < 0) {
1902 Args.reserve((Start - End) / -Step);
1903 for (auto I = Start; I > End; I += Step)
1904 Args.push_back(IntInit::get(getRecordKeeper(), I));
1905 } else {
1906 // Empty set
1907 }
1908 return ListInit::get(Args, LHSi->getType());
1909 }
1910
1911 case SUBSTR: {
1912 const auto *LHSs = dyn_cast<StringInit>(LHS);
1913 const auto *MHSi = dyn_cast<IntInit>(MHS);
1914 const auto *RHSi = dyn_cast<IntInit>(RHS);
1915 if (LHSs && MHSi && RHSi) {
1916 int64_t StringSize = LHSs->getValue().size();
1917 int64_t Start = MHSi->getValue();
1918 int64_t Length = RHSi->getValue();
1919 if (Start < 0 || Start > StringSize)
1920 PrintError(CurRec->getLoc(),
1921 Twine("!substr start position is out of range 0...") +
1922 std::to_string(StringSize) + ": " +
1923 std::to_string(Start));
1924 if (Length < 0)
1925 PrintError(CurRec->getLoc(), "!substr length must be nonnegative");
1926 return StringInit::get(RK, LHSs->getValue().substr(Start, Length),
1927 LHSs->getFormat());
1928 }
1929 break;
1930 }
1931
1932 case FIND: {
1933 const auto *LHSs = dyn_cast<StringInit>(LHS);
1934 const auto *MHSs = dyn_cast<StringInit>(MHS);
1935 const auto *RHSi = dyn_cast<IntInit>(RHS);
1936 if (LHSs && MHSs && RHSi) {
1937 int64_t SourceSize = LHSs->getValue().size();
1938 int64_t Start = RHSi->getValue();
1939 if (Start < 0 || Start > SourceSize)
1940 PrintError(CurRec->getLoc(),
1941 Twine("!find start position is out of range 0...") +
1942 std::to_string(SourceSize) + ": " +
1943 std::to_string(Start));
1944 auto I = LHSs->getValue().find(MHSs->getValue(), Start);
1945 if (I == std::string::npos)
1946 return IntInit::get(RK, -1);
1947 return IntInit::get(RK, I);
1948 }
1949 break;
1950 }
1951
1952 case SETDAGARG: {
1953 const auto *Dag = dyn_cast<DagInit>(LHS);
1954 if (Dag && isa<IntInit, StringInit>(MHS)) {
1955 std::string Error;
1956 auto ArgNo = getDagArgNoByKey(Dag, MHS, Error);
1957 if (!ArgNo)
1958 PrintFatalError(CurRec->getLoc(), "!setdagarg " + Error);
1959
1960 assert(*ArgNo < Dag->getNumArgs());
1961
1962 SmallVector<const Init *, 8> Args(Dag->getArgs());
1963 Args[*ArgNo] = RHS;
1964 return DagInit::get(Dag->getOperator(), Dag->getName(), Args,
1965 Dag->getArgNames());
1966 }
1967 break;
1968 }
1969
1970 case SETDAGNAME: {
1971 const auto *Dag = dyn_cast<DagInit>(LHS);
1972 if (Dag && isa<IntInit, StringInit>(MHS)) {
1973 std::string Error;
1974 auto ArgNo = getDagArgNoByKey(Dag, MHS, Error);
1975 if (!ArgNo)
1976 PrintFatalError(CurRec->getLoc(), "!setdagname " + Error);
1977
1978 assert(*ArgNo < Dag->getNumArgs());
1979
1980 SmallVector<const StringInit *, 8> Names(Dag->getArgNames());
1981 Names[*ArgNo] = dyn_cast<StringInit>(RHS);
1982 return DagInit::get(Dag->getOperator(), Dag->getName(), Dag->getArgs(),
1983 Names);
1984 }
1985 break;
1986 }
1987 }
1988
1989 return this;
1990}
1991
1993 const Init *lhs = LHS->resolveReferences(R);
1994
1995 if (getOpcode() == IF && lhs != LHS) {
1996 if (const auto *Value = dyn_cast_or_null<IntInit>(
1998 // Short-circuit
1999 if (Value->getValue())
2000 return MHS->resolveReferences(R);
2001 return RHS->resolveReferences(R);
2002 }
2003 }
2004
2005 const Init *mhs = MHS->resolveReferences(R);
2006 const Init *rhs;
2007
2008 if (getOpcode() == FOREACH || getOpcode() == FILTER) {
2009 ShadowResolver SR(R);
2010 SR.addShadow(lhs);
2011 rhs = RHS->resolveReferences(SR);
2012 } else {
2013 rhs = RHS->resolveReferences(R);
2014 }
2015
2016 if (LHS != lhs || MHS != mhs || RHS != rhs)
2017 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
2018 ->Fold(R.getCurrentRecord());
2019 return this;
2020}
2021
2022std::string TernOpInit::getAsString() const {
2023 std::string Result;
2024 bool UnquotedLHS = false;
2025 switch (getOpcode()) {
2026 case DAG: Result = "!dag"; break;
2027 case FILTER: Result = "!filter"; UnquotedLHS = true; break;
2028 case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
2029 case IF: Result = "!if"; break;
2030 case RANGE:
2031 Result = "!range";
2032 break;
2033 case SUBST: Result = "!subst"; break;
2034 case SUBSTR: Result = "!substr"; break;
2035 case FIND: Result = "!find"; break;
2036 case SETDAGARG:
2037 Result = "!setdagarg";
2038 break;
2039 case SETDAGNAME:
2040 Result = "!setdagname";
2041 break;
2042 }
2043 return (Result + "(" +
2044 (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
2045 ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
2046}
2047
2048static void ProfileFoldOpInit(FoldingSetNodeID &ID, const Init *Start,
2049 const Init *List, const Init *A, const Init *B,
2050 const Init *Expr, const RecTy *Type) {
2051 ID.AddPointer(Start);
2052 ID.AddPointer(List);
2053 ID.AddPointer(A);
2054 ID.AddPointer(B);
2055 ID.AddPointer(Expr);
2056 ID.AddPointer(Type);
2057}
2058
2059const FoldOpInit *FoldOpInit::get(const Init *Start, const Init *List,
2060 const Init *A, const Init *B,
2061 const Init *Expr, const RecTy *Type) {
2063 ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
2064
2065 detail::RecordKeeperImpl &RK = Start->getRecordKeeper().getImpl();
2066 void *IP = nullptr;
2067 if (const FoldOpInit *I = RK.TheFoldOpInitPool.FindNodeOrInsertPos(ID, IP))
2068 return I;
2069
2070 FoldOpInit *I = new (RK.Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
2071 RK.TheFoldOpInitPool.InsertNode(I, IP);
2072 return I;
2073}
2074
2076 ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
2077}
2078
2079const Init *FoldOpInit::Fold(const Record *CurRec) const {
2080 if (const auto *LI = dyn_cast<ListInit>(List)) {
2081 const Init *Accum = Start;
2082 for (const Init *Elt : *LI) {
2083 MapResolver R(CurRec);
2084 R.set(A, Accum);
2085 R.set(B, Elt);
2086 Accum = Expr->resolveReferences(R);
2087 }
2088 return Accum;
2089 }
2090 return this;
2091}
2092
2094 const Init *NewStart = Start->resolveReferences(R);
2095 const Init *NewList = List->resolveReferences(R);
2096 ShadowResolver SR(R);
2097 SR.addShadow(A);
2098 SR.addShadow(B);
2099 const Init *NewExpr = Expr->resolveReferences(SR);
2100
2101 if (Start == NewStart && List == NewList && Expr == NewExpr)
2102 return this;
2103
2104 return get(NewStart, NewList, A, B, NewExpr, getType())
2105 ->Fold(R.getCurrentRecord());
2106}
2107
2108const Init *FoldOpInit::getBit(unsigned Bit) const {
2109 return VarBitInit::get(this, Bit);
2110}
2111
2112std::string FoldOpInit::getAsString() const {
2113 return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
2114 ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
2115 ", " + Expr->getAsString() + ")")
2116 .str();
2117}
2118
2120 const Init *Expr) {
2121 ID.AddPointer(CheckType);
2122 ID.AddPointer(Expr);
2123}
2124
2125const IsAOpInit *IsAOpInit::get(const RecTy *CheckType, const Init *Expr) {
2126
2128 ProfileIsAOpInit(ID, CheckType, Expr);
2129
2130 detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl();
2131 void *IP = nullptr;
2132 if (const IsAOpInit *I = RK.TheIsAOpInitPool.FindNodeOrInsertPos(ID, IP))
2133 return I;
2134
2135 IsAOpInit *I = new (RK.Allocator) IsAOpInit(CheckType, Expr);
2136 RK.TheIsAOpInitPool.InsertNode(I, IP);
2137 return I;
2138}
2139
2141 ProfileIsAOpInit(ID, CheckType, Expr);
2142}
2143
2144const Init *IsAOpInit::Fold() const {
2145 if (const auto *TI = dyn_cast<TypedInit>(Expr)) {
2146 // Is the expression type known to be (a subclass of) the desired type?
2147 if (TI->getType()->typeIsConvertibleTo(CheckType))
2148 return IntInit::get(getRecordKeeper(), 1);
2149
2150 if (isa<RecordRecTy>(CheckType)) {
2151 // If the target type is not a subclass of the expression type once the
2152 // expression has been made concrete, or if the expression has fully
2153 // resolved to a record, we know that it can't be of the required type.
2154 if ((!CheckType->typeIsConvertibleTo(TI->getType()) &&
2155 Expr->isConcrete()) ||
2156 isa<DefInit>(Expr))
2157 return IntInit::get(getRecordKeeper(), 0);
2158 } else {
2159 // We treat non-record types as not castable.
2160 return IntInit::get(getRecordKeeper(), 0);
2161 }
2162 }
2163 return this;
2164}
2165
2167 const Init *NewExpr = Expr->resolveReferences(R);
2168 if (Expr != NewExpr)
2169 return get(CheckType, NewExpr)->Fold();
2170 return this;
2171}
2172
2173const Init *IsAOpInit::getBit(unsigned Bit) const {
2174 return VarBitInit::get(this, Bit);
2175}
2176
2177std::string IsAOpInit::getAsString() const {
2178 return (Twine("!isa<") + CheckType->getAsString() + ">(" +
2179 Expr->getAsString() + ")")
2180 .str();
2181}
2182
2184 const Init *Expr) {
2185 ID.AddPointer(CheckType);
2186 ID.AddPointer(Expr);
2187}
2188
2189const ExistsOpInit *ExistsOpInit::get(const RecTy *CheckType,
2190 const Init *Expr) {
2192 ProfileExistsOpInit(ID, CheckType, Expr);
2193
2194 detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl();
2195 void *IP = nullptr;
2196 if (const ExistsOpInit *I =
2197 RK.TheExistsOpInitPool.FindNodeOrInsertPos(ID, IP))
2198 return I;
2199
2200 ExistsOpInit *I = new (RK.Allocator) ExistsOpInit(CheckType, Expr);
2201 RK.TheExistsOpInitPool.InsertNode(I, IP);
2202 return I;
2203}
2204
2206 ProfileExistsOpInit(ID, CheckType, Expr);
2207}
2208
2209const Init *ExistsOpInit::Fold(const Record *CurRec, bool IsFinal) const {
2210 if (const auto *Name = dyn_cast<StringInit>(Expr)) {
2211 // Look up all defined records to see if we can find one.
2212 const Record *D = CheckType->getRecordKeeper().getDef(Name->getValue());
2213 if (D) {
2214 // Check if types are compatible.
2216 D->getDefInit()->getType()->typeIsA(CheckType));
2217 }
2218
2219 if (CurRec) {
2220 // Self-references are allowed, but their resolution is delayed until
2221 // the final resolve to ensure that we get the correct type for them.
2222 auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
2223 if (Name == CurRec->getNameInit() ||
2224 (Anonymous && Name == Anonymous->getNameInit())) {
2225 if (!IsFinal)
2226 return this;
2227
2228 // No doubt that there exists a record, so we should check if types are
2229 // compatible.
2231 CurRec->getType()->typeIsA(CheckType));
2232 }
2233 }
2234
2235 if (IsFinal)
2236 return IntInit::get(getRecordKeeper(), 0);
2237 }
2238 return this;
2239}
2240
2242 const Init *NewExpr = Expr->resolveReferences(R);
2243 if (Expr != NewExpr || R.isFinal())
2244 return get(CheckType, NewExpr)->Fold(R.getCurrentRecord(), R.isFinal());
2245 return this;
2246}
2247
2248const Init *ExistsOpInit::getBit(unsigned Bit) const {
2249 return VarBitInit::get(this, Bit);
2250}
2251
2252std::string ExistsOpInit::getAsString() const {
2253 return (Twine("!exists<") + CheckType->getAsString() + ">(" +
2254 Expr->getAsString() + ")")
2255 .str();
2256}
2257
2259 const Init *Regex) {
2260 ID.AddPointer(Type);
2261 ID.AddPointer(Regex);
2262}
2263
2264const InstancesOpInit *InstancesOpInit::get(const RecTy *Type,
2265 const Init *Regex) {
2267 ProfileInstancesOpInit(ID, Type, Regex);
2268
2269 detail::RecordKeeperImpl &RK = Regex->getRecordKeeper().getImpl();
2270 void *IP = nullptr;
2271 if (const InstancesOpInit *I =
2272 RK.TheInstancesOpInitPool.FindNodeOrInsertPos(ID, IP))
2273 return I;
2274
2275 InstancesOpInit *I = new (RK.Allocator) InstancesOpInit(Type, Regex);
2276 RK.TheInstancesOpInitPool.InsertNode(I, IP);
2277 return I;
2278}
2279
2283
2284const Init *InstancesOpInit::Fold(const Record *CurRec, bool IsFinal) const {
2285 if (CurRec && !IsFinal)
2286 return this;
2287
2288 const auto *RegexInit = dyn_cast<StringInit>(Regex);
2289 if (!RegexInit)
2290 return this;
2291
2292 StringRef RegexStr = RegexInit->getValue();
2293 llvm::Regex Matcher(RegexStr);
2294 if (!Matcher.isValid())
2295 PrintFatalError(Twine("invalid regex '") + RegexStr + Twine("'"));
2296
2297 const RecordKeeper &RK = Type->getRecordKeeper();
2298 SmallVector<Init *, 8> Selected;
2299 for (auto &Def : RK.getAllDerivedDefinitionsIfDefined(Type->getAsString()))
2300 if (Matcher.match(Def->getName()))
2301 Selected.push_back(Def->getDefInit());
2302
2303 return ListInit::get(Selected, Type);
2304}
2305
2307 const Init *NewRegex = Regex->resolveReferences(R);
2308 if (Regex != NewRegex || R.isFinal())
2309 return get(Type, NewRegex)->Fold(R.getCurrentRecord(), R.isFinal());
2310 return this;
2311}
2312
2313const Init *InstancesOpInit::getBit(unsigned Bit) const {
2314 return VarBitInit::get(this, Bit);
2315}
2316
2317std::string InstancesOpInit::getAsString() const {
2318 return "!instances<" + Type->getAsString() + ">(" + Regex->getAsString() +
2319 ")";
2320}
2321
2322const RecTy *TypedInit::getFieldType(const StringInit *FieldName) const {
2323 if (const auto *RecordType = dyn_cast<RecordRecTy>(getType())) {
2324 for (const Record *Rec : RecordType->getClasses()) {
2325 if (const RecordVal *Field = Rec->getValue(FieldName))
2326 return Field->getType();
2327 }
2328 }
2329 return nullptr;
2330}
2331
2333 if (getType() == Ty || getType()->typeIsA(Ty))
2334 return this;
2335
2336 if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
2337 cast<BitsRecTy>(Ty)->getNumBits() == 1)
2338 return BitsInit::get(getRecordKeeper(), {this});
2339
2340 return nullptr;
2341}
2342
2343const Init *
2345 const auto *T = dyn_cast<BitsRecTy>(getType());
2346 if (!T) return nullptr; // Cannot subscript a non-bits variable.
2347 unsigned NumBits = T->getNumBits();
2348
2350 NewBits.reserve(Bits.size());
2351 for (unsigned Bit : Bits) {
2352 if (Bit >= NumBits)
2353 return nullptr;
2354
2355 NewBits.push_back(VarBitInit::get(this, Bit));
2356 }
2357 return BitsInit::get(getRecordKeeper(), NewBits);
2358}
2359
2360const Init *TypedInit::getCastTo(const RecTy *Ty) const {
2361 // Handle the common case quickly
2362 if (getType() == Ty || getType()->typeIsA(Ty))
2363 return this;
2364
2365 if (const Init *Converted = convertInitializerTo(Ty)) {
2366 assert(!isa<TypedInit>(Converted) ||
2367 cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
2368 return Converted;
2369 }
2370
2371 if (!getType()->typeIsConvertibleTo(Ty))
2372 return nullptr;
2373
2374 return UnOpInit::get(UnOpInit::CAST, this, Ty)->Fold(nullptr);
2375}
2376
2377const VarInit *VarInit::get(StringRef VN, const RecTy *T) {
2378 const Init *Value = StringInit::get(T->getRecordKeeper(), VN);
2379 return VarInit::get(Value, T);
2380}
2381
2382const VarInit *VarInit::get(const Init *VN, const RecTy *T) {
2383 detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl();
2384 VarInit *&I = RK.TheVarInitPool[{T, VN}];
2385 if (!I)
2386 I = new (RK.Allocator) VarInit(VN, T);
2387 return I;
2388}
2389
2391 const auto *NameString = cast<StringInit>(getNameInit());
2392 return NameString->getValue();
2393}
2394
2395const Init *VarInit::getBit(unsigned Bit) const {
2397 return this;
2398 return VarBitInit::get(this, Bit);
2399}
2400
2402 if (const Init *Val = R.resolve(VarName))
2403 return Val;
2404 return this;
2405}
2406
2407const VarBitInit *VarBitInit::get(const TypedInit *T, unsigned B) {
2408 detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl();
2409 VarBitInit *&I = RK.TheVarBitInitPool[{T, B}];
2410 if (!I)
2411 I = new (RK.Allocator) VarBitInit(T, B);
2412 return I;
2413}
2414
2415std::string VarBitInit::getAsString() const {
2416 return TI->getAsString() + "{" + utostr(Bit) + "}";
2417}
2418
2420 const Init *I = TI->resolveReferences(R);
2421 if (TI != I)
2422 return I->getBit(getBitNum());
2423
2424 return this;
2425}
2426
2427DefInit::DefInit(const Record *D)
2428 : TypedInit(IK_DefInit, D->getType()), Def(D) {}
2429
2431 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
2432 if (getType()->typeIsConvertibleTo(RRT))
2433 return this;
2434 return nullptr;
2435}
2436
2437const RecTy *DefInit::getFieldType(const StringInit *FieldName) const {
2438 if (const RecordVal *RV = Def->getValue(FieldName))
2439 return RV->getType();
2440 return nullptr;
2441}
2442
2443std::string DefInit::getAsString() const { return Def->getName().str(); }
2444
2445static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class,
2447 ID.AddInteger(Args.size());
2448 ID.AddPointer(Class);
2449
2450 for (const Init *I : Args)
2451 ID.AddPointer(I);
2452}
2453
2454VarDefInit::VarDefInit(SMLoc Loc, const Record *Class,
2456 : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class),
2457 NumArgs(Args.size()) {
2458 llvm::uninitialized_copy(Args, getTrailingObjects());
2459}
2460
2461const VarDefInit *VarDefInit::get(SMLoc Loc, const Record *Class,
2464 ProfileVarDefInit(ID, Class, Args);
2465
2466 detail::RecordKeeperImpl &RK = Class->getRecords().getImpl();
2467 void *IP = nullptr;
2468 if (const VarDefInit *I = RK.TheVarDefInitPool.FindNodeOrInsertPos(ID, IP))
2469 return I;
2470
2471 void *Mem = RK.Allocator.Allocate(
2472 totalSizeToAlloc<const ArgumentInit *>(Args.size()), alignof(VarDefInit));
2473 VarDefInit *I = new (Mem) VarDefInit(Loc, Class, Args);
2474 RK.TheVarDefInitPool.InsertNode(I, IP);
2475 return I;
2476}
2477
2479 ProfileVarDefInit(ID, Class, args());
2480}
2481
2482const DefInit *VarDefInit::instantiate() {
2483 if (Def)
2484 return Def;
2485
2486 RecordKeeper &Records = Class->getRecords();
2487 auto NewRecOwner = std::make_unique<Record>(
2488 Records.getNewAnonymousName(), Loc, Records, Record::RK_AnonymousDef);
2489 Record *NewRec = NewRecOwner.get();
2490
2491 // Copy values from class to instance
2492 for (const RecordVal &Val : Class->getValues())
2493 NewRec->addValue(Val);
2494
2495 // Copy assertions from class to instance.
2496 NewRec->appendAssertions(Class);
2497
2498 // Copy dumps from class to instance.
2499 NewRec->appendDumps(Class);
2500
2501 // Substitute and resolve template arguments
2502 ArrayRef<const Init *> TArgs = Class->getTemplateArgs();
2503 MapResolver R(NewRec);
2504
2505 for (const Init *Arg : TArgs) {
2506 R.set(Arg, NewRec->getValue(Arg)->getValue());
2507 NewRec->removeValue(Arg);
2508 }
2509
2510 for (auto *Arg : args()) {
2511 if (Arg->isPositional())
2512 R.set(TArgs[Arg->getIndex()], Arg->getValue());
2513 if (Arg->isNamed())
2514 R.set(Arg->getName(), Arg->getValue());
2515 }
2516
2517 NewRec->resolveReferences(R);
2518
2519 // Add superclass.
2520 NewRec->addDirectSuperClass(
2521 Class, SMRange(Class->getLoc().back(), Class->getLoc().back()));
2522
2523 // Resolve internal references and store in record keeper
2524 NewRec->resolveReferences();
2525 Records.addDef(std::move(NewRecOwner));
2526
2527 // Check the assertions.
2528 NewRec->checkRecordAssertions();
2529
2530 // Check the assertions.
2531 NewRec->emitRecordDumps();
2532
2533 return Def = NewRec->getDefInit();
2534}
2535
2538 bool Changed = false;
2540 NewArgs.reserve(args_size());
2541
2542 for (const ArgumentInit *Arg : args()) {
2543 const auto *NewArg = cast<ArgumentInit>(Arg->resolveReferences(UR));
2544 NewArgs.push_back(NewArg);
2545 Changed |= NewArg != Arg;
2546 }
2547
2548 if (Changed) {
2549 auto *New = VarDefInit::get(Loc, Class, NewArgs);
2550 if (!UR.foundUnresolved())
2551 return const_cast<VarDefInit *>(New)->instantiate();
2552 return New;
2553 }
2554 return this;
2555}
2556
2557const Init *VarDefInit::Fold() const {
2558 if (Def)
2559 return Def;
2560
2562 for (const Init *Arg : args())
2563 Arg->resolveReferences(R);
2564
2565 if (!R.foundUnresolved())
2566 return const_cast<VarDefInit *>(this)->instantiate();
2567 return this;
2568}
2569
2570std::string VarDefInit::getAsString() const {
2571 std::string Result = Class->getNameInitAsString() + "<";
2572 ListSeparator LS;
2573 for (const Init *Arg : args()) {
2574 Result += LS;
2575 Result += Arg->getAsString();
2576 }
2577 return Result + ">";
2578}
2579
2580const FieldInit *FieldInit::get(const Init *R, const StringInit *FN) {
2581 detail::RecordKeeperImpl &RK = R->getRecordKeeper().getImpl();
2582 FieldInit *&I = RK.TheFieldInitPool[{R, FN}];
2583 if (!I)
2584 I = new (RK.Allocator) FieldInit(R, FN);
2585 return I;
2586}
2587
2588const Init *FieldInit::getBit(unsigned Bit) const {
2590 return this;
2591 return VarBitInit::get(this, Bit);
2592}
2593
2595 const Init *NewRec = Rec->resolveReferences(R);
2596 if (NewRec != Rec)
2597 return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
2598 return this;
2599}
2600
2601const Init *FieldInit::Fold(const Record *CurRec) const {
2602 if (const auto *DI = dyn_cast<DefInit>(Rec)) {
2603 const Record *Def = DI->getDef();
2604 if (Def == CurRec)
2605 PrintFatalError(CurRec->getLoc(),
2606 Twine("Attempting to access field '") +
2607 FieldName->getAsUnquotedString() + "' of '" +
2608 Rec->getAsString() + "' is a forbidden self-reference");
2609 const Init *FieldVal = Def->getValue(FieldName)->getValue();
2610 if (FieldVal->isConcrete())
2611 return FieldVal;
2612 }
2613 return this;
2614}
2615
2617 if (const auto *DI = dyn_cast<DefInit>(Rec)) {
2618 const Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue();
2619 return FieldVal->isConcrete();
2620 }
2621 return false;
2622}
2623
2627 const RecTy *ValType) {
2628 assert(Conds.size() == Vals.size() &&
2629 "Number of conditions and values must match!");
2630 ID.AddPointer(ValType);
2631
2632 for (const auto &[Cond, Val] : zip(Conds, Vals)) {
2633 ID.AddPointer(Cond);
2634 ID.AddPointer(Val);
2635 }
2636}
2637
2638CondOpInit::CondOpInit(ArrayRef<const Init *> Conds,
2639 ArrayRef<const Init *> Values, const RecTy *Type)
2640 : TypedInit(IK_CondOpInit, Type), NumConds(Conds.size()), ValType(Type) {
2641 const Init **TrailingObjects = getTrailingObjects();
2643 llvm::uninitialized_copy(Values, TrailingObjects + NumConds);
2644}
2645
2649
2652 const RecTy *Ty) {
2653 assert(Conds.size() == Values.size() &&
2654 "Number of conditions and values must match!");
2655
2657 ProfileCondOpInit(ID, Conds, Values, Ty);
2658
2659 detail::RecordKeeperImpl &RK = Ty->getRecordKeeper().getImpl();
2660 void *IP = nullptr;
2661 if (const CondOpInit *I = RK.TheCondOpInitPool.FindNodeOrInsertPos(ID, IP))
2662 return I;
2663
2664 void *Mem = RK.Allocator.Allocate(
2665 totalSizeToAlloc<const Init *>(2 * Conds.size()), alignof(CondOpInit));
2666 CondOpInit *I = new (Mem) CondOpInit(Conds, Values, Ty);
2667 RK.TheCondOpInitPool.InsertNode(I, IP);
2668 return I;
2669}
2670
2674
2675 bool Changed = false;
2676 for (auto [Cond, Val] : getCondAndVals()) {
2677 const Init *NewCond = Cond->resolveReferences(R);
2678 NewConds.push_back(NewCond);
2679 Changed |= NewCond != Cond;
2680
2681 const Init *NewVal = Val->resolveReferences(R);
2682 NewVals.push_back(NewVal);
2683 Changed |= NewVal != Val;
2684 }
2685
2686 if (Changed)
2687 return (CondOpInit::get(NewConds, NewVals,
2688 getValType()))->Fold(R.getCurrentRecord());
2689
2690 return this;
2691}
2692
2693const Init *CondOpInit::Fold(const Record *CurRec) const {
2695 for (auto [Cond, Val] : getCondAndVals()) {
2696 if (const auto *CondI = dyn_cast_or_null<IntInit>(
2697 Cond->convertInitializerTo(IntRecTy::get(RK)))) {
2698 if (CondI->getValue())
2699 return Val->convertInitializerTo(getValType());
2700 } else {
2701 return this;
2702 }
2703 }
2704
2705 PrintFatalError(CurRec->getLoc(),
2706 CurRec->getNameInitAsString() +
2707 " does not have any true condition in:" +
2708 this->getAsString());
2709 return nullptr;
2710}
2711
2713 return all_of(getCondAndVals(), [](const auto &Pair) {
2714 return std::get<0>(Pair)->isConcrete() && std::get<1>(Pair)->isConcrete();
2715 });
2716}
2717
2719 return all_of(getCondAndVals(), [](const auto &Pair) {
2720 return std::get<0>(Pair)->isComplete() && std::get<1>(Pair)->isComplete();
2721 });
2722}
2723
2724std::string CondOpInit::getAsString() const {
2725 std::string Result = "!cond(";
2726 ListSeparator LS;
2727 for (auto [Cond, Val] : getCondAndVals()) {
2728 Result += LS;
2729 Result += Cond->getAsString() + ": ";
2730 Result += Val->getAsString();
2731 }
2732 return Result + ")";
2733}
2734
2735const Init *CondOpInit::getBit(unsigned Bit) const {
2736 return VarBitInit::get(this, Bit);
2737}
2738
2740 const StringInit *VN, ArrayRef<const Init *> Args,
2742 ID.AddPointer(V);
2743 ID.AddPointer(VN);
2744
2745 for (auto [Arg, Name] : zip_equal(Args, ArgNames)) {
2746 ID.AddPointer(Arg);
2747 ID.AddPointer(Name);
2748 }
2749}
2750
2751DagInit::DagInit(const Init *V, const StringInit *VN,
2754 : TypedInit(IK_DagInit, DagRecTy::get(V->getRecordKeeper())), Val(V),
2755 ValName(VN), NumArgs(Args.size()) {
2756 llvm::uninitialized_copy(Args, getTrailingObjects<const Init *>());
2757 llvm::uninitialized_copy(ArgNames, getTrailingObjects<const StringInit *>());
2758}
2759
2760const DagInit *DagInit::get(const Init *V, const StringInit *VN,
2763 assert(Args.size() == ArgNames.size() &&
2764 "Number of DAG args and arg names must match!");
2765
2767 ProfileDagInit(ID, V, VN, Args, ArgNames);
2768
2769 detail::RecordKeeperImpl &RK = V->getRecordKeeper().getImpl();
2770 void *IP = nullptr;
2771 if (const DagInit *I = RK.TheDagInitPool.FindNodeOrInsertPos(ID, IP))
2772 return I;
2773
2774 void *Mem =
2776 Args.size(), ArgNames.size()),
2777 alignof(DagInit));
2778 DagInit *I = new (Mem) DagInit(V, VN, Args, ArgNames);
2779 RK.TheDagInitPool.InsertNode(I, IP);
2780 return I;
2781}
2782
2783const DagInit *DagInit::get(
2784 const Init *V, const StringInit *VN,
2785 ArrayRef<std::pair<const Init *, const StringInit *>> ArgAndNames) {
2788 return DagInit::get(V, VN, Args, Names);
2789}
2790
2792 ProfileDagInit(ID, Val, ValName, getArgs(), getArgNames());
2793}
2794
2796 if (const auto *DefI = dyn_cast<DefInit>(Val))
2797 return DefI->getDef();
2798 PrintFatalError(Loc, "Expected record as operator");
2799 return nullptr;
2800}
2801
2802std::optional<unsigned> DagInit::getArgNo(StringRef Name) const {
2804 auto It = llvm::find_if(ArgNames, [Name](const StringInit *ArgName) {
2805 return ArgName && ArgName->getValue() == Name;
2806 });
2807 if (It == ArgNames.end())
2808 return std::nullopt;
2809 return std::distance(ArgNames.begin(), It);
2810}
2811
2814 NewArgs.reserve(arg_size());
2815 bool ArgsChanged = false;
2816 for (const Init *Arg : getArgs()) {
2817 const Init *NewArg = Arg->resolveReferences(R);
2818 NewArgs.push_back(NewArg);
2819 ArgsChanged |= NewArg != Arg;
2820 }
2821
2822 const Init *Op = Val->resolveReferences(R);
2823 if (Op != Val || ArgsChanged)
2824 return DagInit::get(Op, ValName, NewArgs, getArgNames());
2825
2826 return this;
2827}
2828
2830 if (!Val->isConcrete())
2831 return false;
2832 return all_of(getArgs(), [](const Init *Elt) { return Elt->isConcrete(); });
2833}
2834
2835std::string DagInit::getAsString() const {
2836 std::string Result = "(" + Val->getAsString();
2837 if (ValName)
2838 Result += ":$" + ValName->getAsUnquotedString();
2839 if (!arg_empty()) {
2840 Result += " ";
2841 ListSeparator LS;
2842 for (auto [Arg, Name] : getArgAndNames()) {
2843 Result += LS;
2844 Result += Arg->getAsString();
2845 if (Name)
2846 Result += ":$" + Name->getAsUnquotedString();
2847 }
2848 }
2849 return Result + ")";
2850}
2851
2852//===----------------------------------------------------------------------===//
2853// Other implementations
2854//===----------------------------------------------------------------------===//
2855
2857 : Name(N), TyAndKind(T, K) {
2858 setValue(UnsetInit::get(N->getRecordKeeper()));
2859 assert(Value && "Cannot create unset value for current type!");
2860}
2861
2862// This constructor accepts the same arguments as the above, but also
2863// a source location.
2865 : Name(N), Loc(Loc), TyAndKind(T, K) {
2866 setValue(UnsetInit::get(N->getRecordKeeper()));
2867 assert(Value && "Cannot create unset value for current type!");
2868}
2869
2871 return cast<StringInit>(getNameInit())->getValue();
2872}
2873
2874std::string RecordVal::getPrintType() const {
2876 if (const auto *StrInit = dyn_cast<StringInit>(Value)) {
2877 if (StrInit->hasCodeFormat())
2878 return "code";
2879 else
2880 return "string";
2881 } else {
2882 return "string";
2883 }
2884 } else {
2885 return TyAndKind.getPointer()->getAsString();
2886 }
2887}
2888
2890 if (!V) {
2891 Value = nullptr;
2892 return false;
2893 }
2894
2895 Value = V->getCastTo(getType());
2896 if (!Value)
2897 return true;
2898
2899 assert(!isa<TypedInit>(Value) ||
2900 cast<TypedInit>(Value)->getType()->typeIsA(getType()));
2901 if (const auto *BTy = dyn_cast<BitsRecTy>(getType())) {
2902 if (isa<BitsInit>(Value))
2903 return false;
2904 SmallVector<const Init *, 64> Bits(BTy->getNumBits());
2905 for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
2906 Bits[I] = Value->getBit(I);
2907 Value = BitsInit::get(V->getRecordKeeper(), Bits);
2908 }
2909
2910 return false;
2911}
2912
2913// This version of setValue takes a source location and resets the
2914// location in the RecordVal.
2915bool RecordVal::setValue(const Init *V, SMLoc NewLoc) {
2916 Loc = NewLoc;
2917 return setValue(V);
2918}
2919
2920#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2921LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
2922#endif
2923
2924void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
2925 if (isNonconcreteOK()) OS << "field ";
2926 OS << getPrintType() << " " << getNameInitAsString();
2927
2928 if (getValue())
2929 OS << " = " << *getValue();
2930
2931 if (PrintSem) OS << ";\n";
2932}
2933
2935 assert(Locs.size() == 1);
2936 ForwardDeclarationLocs.push_back(Locs.front());
2937
2938 Locs.clear();
2939 Locs.push_back(Loc);
2940}
2941
2942void Record::checkName() {
2943 // Ensure the record name has string type.
2944 const auto *TypedName = cast<const TypedInit>(Name);
2945 if (!isa<StringRecTy>(TypedName->getType()))
2946 PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
2947 "' is not a string!");
2948}
2949
2953 return RecordRecTy::get(TrackedRecords, DirectSCs);
2954}
2955
2957 if (!CorrespondingDefInit) {
2958 CorrespondingDefInit =
2959 new (TrackedRecords.getImpl().Allocator) DefInit(this);
2960 }
2961 return CorrespondingDefInit;
2962}
2963
2965 return RK.getImpl().LastRecordID++;
2966}
2967
2968void Record::setName(const Init *NewName) {
2969 Name = NewName;
2970 checkName();
2971 // DO NOT resolve record values to the name at this point because
2972 // there might be default values for arguments of this def. Those
2973 // arguments might not have been resolved yet so we don't want to
2974 // prematurely assume values for those arguments were not passed to
2975 // this def.
2976 //
2977 // Nonetheless, it may be that some of this Record's values
2978 // reference the record name. Indeed, the reason for having the
2979 // record name be an Init is to provide this flexibility. The extra
2980 // resolve steps after completely instantiating defs takes care of
2981 // this. See TGParser::ParseDef and TGParser::ParseDefm.
2982}
2983
2985 const Init *OldName = getNameInit();
2986 const Init *NewName = Name->resolveReferences(R);
2987 if (NewName != OldName) {
2988 // Re-register with RecordKeeper.
2989 setName(NewName);
2990 }
2991
2992 // Resolve the field values.
2993 for (RecordVal &Value : Values) {
2994 if (SkipVal == &Value) // Skip resolve the same field as the given one
2995 continue;
2996 if (const Init *V = Value.getValue()) {
2997 const Init *VR = V->resolveReferences(R);
2998 if (Value.setValue(VR)) {
2999 std::string Type;
3000 if (const auto *VRT = dyn_cast<TypedInit>(VR))
3001 Type =
3002 (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
3004 getLoc(),
3005 Twine("Invalid value ") + Type + "found when setting field '" +
3006 Value.getNameInitAsString() + "' of type '" +
3007 Value.getType()->getAsString() +
3008 "' after resolving references: " + VR->getAsUnquotedString() +
3009 "\n");
3010 }
3011 }
3012 }
3013
3014 // Resolve the assertion expressions.
3015 for (AssertionInfo &Assertion : Assertions) {
3016 const Init *Value = Assertion.Condition->resolveReferences(R);
3017 Assertion.Condition = Value;
3018 Value = Assertion.Message->resolveReferences(R);
3019 Assertion.Message = Value;
3020 }
3021 // Resolve the dump expressions.
3022 for (DumpInfo &Dump : Dumps) {
3023 const Init *Value = Dump.Message->resolveReferences(R);
3024 Dump.Message = Value;
3025 }
3026}
3027
3028void Record::resolveReferences(const Init *NewName) {
3029 RecordResolver R(*this);
3030 R.setName(NewName);
3031 R.setFinal(true);
3033}
3034
3035#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3036LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
3037#endif
3038
3040 OS << R.getNameInitAsString();
3041
3042 ArrayRef<const Init *> TArgs = R.getTemplateArgs();
3043 if (!TArgs.empty()) {
3044 OS << "<";
3045 ListSeparator LS;
3046 for (const Init *TA : TArgs) {
3047 const RecordVal *RV = R.getValue(TA);
3048 assert(RV && "Template argument record not found??");
3049 OS << LS;
3050 RV->print(OS, false);
3051 }
3052 OS << ">";
3053 }
3054
3055 OS << " {";
3056 std::vector<const Record *> SCs = R.getSuperClasses();
3057 if (!SCs.empty()) {
3058 OS << "\t//";
3059 for (const Record *SC : SCs)
3060 OS << " " << SC->getNameInitAsString();
3061 }
3062 OS << "\n";
3063
3064 for (const RecordVal &Val : R.getValues())
3065 if (Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit()))
3066 OS << Val;
3067 for (const RecordVal &Val : R.getValues())
3068 if (!Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit()))
3069 OS << Val;
3070
3071 return OS << "}\n";
3072}
3073
3075 const RecordVal *R = getValue(FieldName);
3076 if (!R)
3077 PrintFatalError(getLoc(), "Record `" + getName() +
3078 "' does not have a field named `" + FieldName + "'!\n");
3079 return R->getLoc();
3080}
3081
3082const Init *Record::getValueInit(StringRef FieldName) const {
3083 const RecordVal *R = getValue(FieldName);
3084 if (!R || !R->getValue())
3085 PrintFatalError(getLoc(), "Record `" + getName() +
3086 "' does not have a field named `" + FieldName + "'!\n");
3087 return R->getValue();
3088}
3089
3091 const Init *I = getValueInit(FieldName);
3092 if (const auto *SI = dyn_cast<StringInit>(I))
3093 return SI->getValue();
3094 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
3095 "' exists but does not have a string value");
3096}
3097
3098std::optional<StringRef>
3100 const RecordVal *R = getValue(FieldName);
3101 if (!R || !R->getValue())
3102 return std::nullopt;
3103 if (isa<UnsetInit>(R->getValue()))
3104 return std::nullopt;
3105
3106 if (const auto *SI = dyn_cast<StringInit>(R->getValue()))
3107 return SI->getValue();
3108
3110 "Record `" + getName() + "', ` field `" + FieldName +
3111 "' exists but does not have a string initializer!");
3112}
3113
3115 const Init *I = getValueInit(FieldName);
3116 if (const auto *BI = dyn_cast<BitsInit>(I))
3117 return BI;
3118 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
3119 "' exists but does not have a bits value");
3120}
3121
3123 const Init *I = getValueInit(FieldName);
3124 if (const auto *LI = dyn_cast<ListInit>(I))
3125 return LI;
3126 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
3127 "' exists but does not have a list value");
3128}
3129
3130std::vector<const Record *>
3132 const ListInit *List = getValueAsListInit(FieldName);
3133 std::vector<const Record *> Defs;
3134 for (const Init *I : List->getElements()) {
3135 if (const auto *DI = dyn_cast<DefInit>(I))
3136 Defs.push_back(DI->getDef());
3137 else
3138 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3139 FieldName +
3140 "' list is not entirely DefInit!");
3141 }
3142 return Defs;
3143}
3144
3145int64_t Record::getValueAsInt(StringRef FieldName) const {
3146 const Init *I = getValueInit(FieldName);
3147 if (const auto *II = dyn_cast<IntInit>(I))
3148 return II->getValue();
3150 getLoc(),
3151 Twine("Record `") + getName() + "', field `" + FieldName +
3152 "' exists but does not have an int value: " + I->getAsString());
3153}
3154
3155std::vector<int64_t>
3157 const ListInit *List = getValueAsListInit(FieldName);
3158 std::vector<int64_t> Ints;
3159 for (const Init *I : List->getElements()) {
3160 if (const auto *II = dyn_cast<IntInit>(I))
3161 Ints.push_back(II->getValue());
3162 else
3164 Twine("Record `") + getName() + "', field `" + FieldName +
3165 "' exists but does not have a list of ints value: " +
3166 I->getAsString());
3167 }
3168 return Ints;
3169}
3170
3171std::vector<StringRef>
3173 const ListInit *List = getValueAsListInit(FieldName);
3174 std::vector<StringRef> Strings;
3175 for (const Init *I : List->getElements()) {
3176 if (const auto *SI = dyn_cast<StringInit>(I))
3177 Strings.push_back(SI->getValue());
3178 else
3180 Twine("Record `") + getName() + "', field `" + FieldName +
3181 "' exists but does not have a list of strings value: " +
3182 I->getAsString());
3183 }
3184 return Strings;
3185}
3186
3187const Record *Record::getValueAsDef(StringRef FieldName) const {
3188 const Init *I = getValueInit(FieldName);
3189 if (const auto *DI = dyn_cast<DefInit>(I))
3190 return DI->getDef();
3191 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3192 FieldName + "' does not have a def initializer!");
3193}
3194
3196 const Init *I = getValueInit(FieldName);
3197 if (const auto *DI = dyn_cast<DefInit>(I))
3198 return DI->getDef();
3199 if (isa<UnsetInit>(I))
3200 return nullptr;
3201 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3202 FieldName + "' does not have either a def initializer or '?'!");
3203}
3204
3205bool Record::getValueAsBit(StringRef FieldName) const {
3206 const Init *I = getValueInit(FieldName);
3207 if (const auto *BI = dyn_cast<BitInit>(I))
3208 return BI->getValue();
3209 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3210 FieldName + "' does not have a bit initializer!");
3211}
3212
3213bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
3214 const Init *I = getValueInit(FieldName);
3215 if (isa<UnsetInit>(I)) {
3216 Unset = true;
3217 return false;
3218 }
3219 Unset = false;
3220 if (const auto *BI = dyn_cast<BitInit>(I))
3221 return BI->getValue();
3222 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3223 FieldName + "' does not have a bit initializer!");
3224}
3225
3226const DagInit *Record::getValueAsDag(StringRef FieldName) const {
3227 const Init *I = getValueInit(FieldName);
3228 if (const auto *DI = dyn_cast<DagInit>(I))
3229 return DI;
3230 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3231 FieldName + "' does not have a dag initializer!");
3232}
3233
3234// Check all record assertions: For each one, resolve the condition
3235// and message, then call CheckAssert().
3236// Note: The condition and message are probably already resolved,
3237// but resolving again allows calls before records are resolved.
3239 RecordResolver R(*this);
3240 R.setFinal(true);
3241
3242 bool AnyFailed = false;
3243 for (const auto &Assertion : getAssertions()) {
3244 const Init *Condition = Assertion.Condition->resolveReferences(R);
3245 const Init *Message = Assertion.Message->resolveReferences(R);
3246 AnyFailed |= CheckAssert(Assertion.Loc, Condition, Message);
3247 }
3248
3249 if (!AnyFailed)
3250 return;
3251
3252 // If any of the record assertions failed, print some context that will
3253 // help see where the record that caused these assert failures is defined.
3254 PrintError(this, "assertion failed in this record");
3255}
3256
3258 RecordResolver R(*this);
3259 R.setFinal(true);
3260
3261 for (const DumpInfo &Dump : getDumps()) {
3262 const Init *Message = Dump.Message->resolveReferences(R);
3263 dumpMessage(Dump.Loc, Message);
3264 }
3265}
3266
3267// Report a warning if the record has unused template arguments.
3269 for (const Init *TA : getTemplateArgs()) {
3270 const RecordVal *Arg = getValue(TA);
3271 if (!Arg->isUsed())
3272 PrintWarning(Arg->getLoc(),
3273 "unused template argument: " + Twine(Arg->getName()));
3274 }
3275}
3276
3278 : Impl(std::make_unique<detail::RecordKeeperImpl>(*this)),
3279 Timer(std::make_unique<TGTimer>()) {}
3280
3281RecordKeeper::~RecordKeeper() = default;
3282
3283#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3284LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
3285#endif
3286
3288 OS << "------------- Classes -----------------\n";
3289 for (const auto &[_, C] : RK.getClasses())
3290 OS << "class " << *C;
3291
3292 OS << "------------- Defs -----------------\n";
3293 for (const auto &[_, D] : RK.getDefs())
3294 OS << "def " << *D;
3295 return OS;
3296}
3297
3298/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
3299/// an identifier.
3301 return AnonymousNameInit::get(*this, getImpl().AnonCounter++);
3302}
3303
3306 // We cache the record vectors for single classes. Many backends request
3307 // the same vectors multiple times.
3308 auto [Iter, Inserted] = Cache.try_emplace(ClassName.str());
3309 if (Inserted)
3310 Iter->second = getAllDerivedDefinitions(ArrayRef(ClassName));
3311 return Iter->second;
3312}
3313
3314std::vector<const Record *>
3317 std::vector<const Record *> Defs;
3318
3319 assert(ClassNames.size() > 0 && "At least one class must be passed.");
3320 for (StringRef ClassName : ClassNames) {
3321 const Record *Class = getClass(ClassName);
3322 if (!Class)
3323 PrintFatalError("The class '" + ClassName + "' is not defined\n");
3324 ClassRecs.push_back(Class);
3325 }
3326
3327 for (const auto &OneDef : getDefs()) {
3328 if (all_of(ClassRecs, [&OneDef](const Record *Class) {
3329 return OneDef.second->isSubClassOf(Class);
3330 }))
3331 Defs.push_back(OneDef.second.get());
3332 }
3333 llvm::sort(Defs, LessRecord());
3334 return Defs;
3335}
3336
3339 if (getClass(ClassName))
3340 return getAllDerivedDefinitions(ClassName);
3341 return Cache[""];
3342}
3343
3345 Impl->dumpAllocationStats(OS);
3346}
3347
3348const Init *MapResolver::resolve(const Init *VarName) {
3349 auto It = Map.find(VarName);
3350 if (It == Map.end())
3351 return nullptr;
3352
3353 const Init *I = It->second.V;
3354
3355 if (!It->second.Resolved && Map.size() > 1) {
3356 // Resolve mutual references among the mapped variables, but prevent
3357 // infinite recursion.
3358 Map.erase(It);
3359 I = I->resolveReferences(*this);
3360 Map[VarName] = {I, true};
3361 }
3362
3363 return I;
3364}
3365
3366const Init *RecordResolver::resolve(const Init *VarName) {
3367 const Init *Val = Cache.lookup(VarName);
3368 if (Val)
3369 return Val;
3370
3371 if (llvm::is_contained(Stack, VarName))
3372 return nullptr; // prevent infinite recursion
3373
3374 if (const RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
3375 if (!isa<UnsetInit>(RV->getValue())) {
3376 Val = RV->getValue();
3377 Stack.push_back(VarName);
3378 Val = Val->resolveReferences(*this);
3379 Stack.pop_back();
3380 }
3381 } else if (Name && VarName == getCurrentRecord()->getNameInit()) {
3382 Stack.push_back(VarName);
3383 Val = Name->resolveReferences(*this);
3384 Stack.pop_back();
3385 }
3386
3387 Cache[VarName] = Val;
3388 return Val;
3389}
3390
3392 const Init *I = nullptr;
3393
3394 if (R) {
3395 I = R->resolve(VarName);
3396 if (I && !FoundUnresolved) {
3397 // Do not recurse into the resolved initializer, as that would change
3398 // the behavior of the resolver we're delegating, but do check to see
3399 // if there are unresolved variables remaining.
3401 I->resolveReferences(Sub);
3402 FoundUnresolved |= Sub.FoundUnresolved;
3403 }
3404 }
3405
3406 if (!I)
3407 FoundUnresolved = true;
3408 return I;
3409}
3410
3412 if (VarName == VarNameToTrack)
3413 Found = true;
3414 return nullptr;
3415}
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:2624
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:1681
static void ProfileExistsOpInit(FoldingSetNodeID &ID, const RecTy *CheckType, const Init *Expr)
Definition Record.cpp:2183
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:2739
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:2048
static void ProfileInstancesOpInit(FoldingSetNodeID &ID, const RecTy *Type, const Init *Regex)
Definition Record.cpp:2258
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:1718
static const Init * FilterHelper(const Init *LHS, const Init *MHS, const Init *RHS, const RecTy *Type, const Record *CurRec)
Definition Record.cpp:1767
static const Init * ItemApply(const Init *LHS, const Init *MHSe, const Init *RHS, const Record *CurRec)
Definition Record.cpp:1711
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:1745
static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class, ArrayRef< const ArgumentInit * > Args)
Definition Record.cpp:2445
static void ProfileIsAOpInit(FoldingSetNodeID &ID, const RecTy *CheckType, const Init *Expr)
Definition Record.cpp:2119
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:132
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
iterator begin() const
Definition ArrayRef.h:131
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:138
static const BinOpInit * get(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
Definition Record.cpp: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:1604
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:1632
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:2693
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:2671
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2735
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations?
Definition Record.cpp:2712
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:2646
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2724
static const CondOpInit * get(ArrayRef< const Init * > Conds, ArrayRef< const Init * > Values, const RecTy *Type)
Definition Record.cpp:2650
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:2718
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:2829
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:2802
const StringInit * getName() const
Definition Record.h:1472
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:2791
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:2812
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:2760
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:2795
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:2835
'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:2443
const RecTy * getFieldType(const StringInit *FieldName) const override
This function is used to implement the FieldInit class.
Definition Record.cpp:2437
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:2430
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:2205
static const ExistsOpInit * get(const RecTy *CheckType, const Init *Expr)
Definition Record.cpp:2189
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2252
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:2241
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
Definition Record.cpp:2209
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2248
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:2601
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2588
static const FieldInit * get(const Init *R, const StringInit *FN)
Definition Record.cpp:2580
const Init * resolveReferences(Resolver &R) const override
This function is used by classes that refer to other variables which may not be defined at the time t...
Definition Record.cpp:2594
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations?
Definition Record.cpp:2616
const Init * Fold(const Record *CurRec) const
Definition Record.cpp:2079
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2112
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:2059
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2108
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:2093
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:2075
void InsertNode(T *N, void *InsertPos)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition FoldingSet.h:512
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition FoldingSet.h:504
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:330
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition FoldingSet.h:535
const Init * resolve(const Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit),...
Definition Record.cpp:3411
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:2280
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
Definition Record.cpp:2284
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2313
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:2306
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2317
static const InstancesOpInit * get(const RecTy *Type, const Init *Regex)
Definition Record.cpp:2264
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:2125
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:2140
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:2166
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2177
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2173
const Init * Fold() const
Definition Record.cpp:2144
[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:3348
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:3300
const RecordMap & getDefs() const
Get the map of records (defs).
Definition Record.h:1995
void dump() const
Definition Record.cpp:3284
detail::RecordKeeperImpl & getImpl()
Return the internal implementation of the RecordKeeper.
Definition Record.h:1986
void dumpAllocationStats(raw_ostream &OS) const
Definition Record.cpp:3344
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:3338
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:3305
'[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:3366
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:2889
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:2921
StringRef getName() const
Get the name of the field as a StringRef.
Definition Record.cpp:2870
void print(raw_ostream &OS, bool PrintSem=true) const
Print the value to an output stream, possibly with a semicolon.
Definition Record.cpp:2924
RecordVal(const Init *N, const RecTy *T, FieldKind K)
Definition Record.cpp:2856
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:2874
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:3156
const RecordRecTy * getType() const
Definition Record.cpp:2950
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:3082
bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const
This method looks up the specified field and returns its value as a bit.
Definition Record.cpp:3213
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:3205
@ RK_AnonymousDef
Definition Record.h:1651
static unsigned getNewUID(RecordKeeper &RK)
Definition Record.cpp:2964
ArrayRef< SMLoc > getLoc() const
Definition Record.h:1720
void checkUnusedTemplateArgs()
Definition Record.cpp:3268
void emitRecordDumps()
Definition Record.cpp:3257
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:3131
ArrayRef< AssertionInfo > getAssertions() const
Definition Record.h:1752
std::string getNameInitAsString() const
Definition Record.h:1714
void dump() const
Definition Record.cpp:3036
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:3187
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:3226
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:3172
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:3195
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:2968
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:3122
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:2956
SMLoc getFieldLoc(StringRef FieldName) const
Return the source location for the named field.
Definition Record.cpp:3074
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:3028
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:3099
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:2934
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:3114
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:3145
void checkRecordAssertions()
Definition Record.cpp:3238
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:3090
LLVM_ABI bool isValid(std::string &Error) const
isValid - returns the error encountered during regex compilation, if any.
Definition Regex.cpp:69
LLVM_ABI bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition Regex.cpp:83
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition Record.h:2199
const Record * getCurrentRecord() const
Definition Record.h:2207
Represents a location in source code.
Definition SMLoc.h:22
Delegate resolving to a sub-resolver, but shadow some variable names.
Definition Record.h:2269
void addShadow(const Init *Key)
Definition Record.h:2279
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
"foo" - Represent an initialization by a string value.
Definition Record.h:696
static const StringInit * get(RecordKeeper &RK, StringRef, StringFormat Fmt=SF_String)
Definition Record.cpp: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:1792
const Init * getLHS() const
Definition Record.h:994
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:1707
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:1691
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2022
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:1992
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:3391
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:2322
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:2344
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:2360
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:2332
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:2407
unsigned getBitNum() const
Definition Record.h:1282
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2415
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:2419
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:2461
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:2536
const Init * Fold() const
Definition Record.cpp:2557
void Profile(FoldingSetNodeID &ID) const
Definition Record.cpp:2478
std::string getAsString() const override
Convert this value to a literal form.
Definition Record.cpp:2570
'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:2377
const Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition Record.cpp:2395
StringRef getName() const
Definition Record.cpp:2390
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:2401
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
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
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:829
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:839
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2472
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void PrintFatalError(const Twine &Msg)
Definition Error.cpp:132
void PrintError(const Twine &Msg)
Definition Error.cpp:104
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
std::string utostr(uint64_t X, bool isNeg=false)
auto uninitialized_copy(R &&Src, IterTy Dst)
Definition STLExtras.h:2053
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
Definition Error.cpp:163
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
void PrintWarning(const Twine &Msg)
Definition Error.cpp:90
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1397
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
Definition STLExtras.h:1407
void dumpMessage(SMLoc Loc, const Init *Message)
Definition Error.cpp:181
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1758
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition Record.cpp: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