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