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