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