LLVM  8.0.1
Record.cpp
Go to the documentation of this file.
1 //===- Record.cpp - Record implementation ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the tablegen record classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/SMLoc.h"
29 #include "llvm/TableGen/Error.h"
30 #include "llvm/TableGen/Record.h"
31 #include <cassert>
32 #include <cstdint>
33 #include <memory>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 using namespace llvm;
39 
41 
42 //===----------------------------------------------------------------------===//
43 // Type implementations
44 //===----------------------------------------------------------------------===//
45 
46 BitRecTy BitRecTy::Shared;
47 CodeRecTy CodeRecTy::Shared;
48 IntRecTy IntRecTy::Shared;
49 StringRecTy StringRecTy::Shared;
50 DagRecTy DagRecTy::Shared;
51 
52 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
53 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
54 #endif
55 
57  if (!ListTy)
58  ListTy = new(Allocator) ListRecTy(this);
59  return ListTy;
60 }
61 
62 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
63  assert(RHS && "NULL pointer");
64  return Kind == RHS->getRecTyKind();
65 }
66 
67 bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
68 
69 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
71  return true;
72  if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
73  return BitsTy->getNumBits() == 1;
74  return false;
75 }
76 
77 BitsRecTy *BitsRecTy::get(unsigned Sz) {
78  static std::vector<BitsRecTy*> Shared;
79  if (Sz >= Shared.size())
80  Shared.resize(Sz + 1);
81  BitsRecTy *&Ty = Shared[Sz];
82  if (!Ty)
83  Ty = new(Allocator) BitsRecTy(Sz);
84  return Ty;
85 }
86 
87 std::string BitsRecTy::getAsString() const {
88  return "bits<" + utostr(Size) + ">";
89 }
90 
91 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
92  if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
93  return cast<BitsRecTy>(RHS)->Size == Size;
94  RecTyKind kind = RHS->getRecTyKind();
95  return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
96 }
97 
98 bool BitsRecTy::typeIsA(const RecTy *RHS) const {
99  if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
100  return RHSb->Size == Size;
101  return false;
102 }
103 
104 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
105  RecTyKind kind = RHS->getRecTyKind();
106  return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
107 }
108 
109 bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
110  RecTyKind Kind = RHS->getRecTyKind();
111  return Kind == CodeRecTyKind || Kind == StringRecTyKind;
112 }
113 
114 std::string StringRecTy::getAsString() const {
115  return "string";
116 }
117 
118 bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
119  RecTyKind Kind = RHS->getRecTyKind();
120  return Kind == StringRecTyKind || Kind == CodeRecTyKind;
121 }
122 
123 std::string ListRecTy::getAsString() const {
124  return "list<" + Ty->getAsString() + ">";
125 }
126 
127 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
128  if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
129  return Ty->typeIsConvertibleTo(ListTy->getElementType());
130  return false;
131 }
132 
133 bool ListRecTy::typeIsA(const RecTy *RHS) const {
134  if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
135  return getElementType()->typeIsA(RHSl->getElementType());
136  return false;
137 }
138 
139 std::string DagRecTy::getAsString() const {
140  return "dag";
141 }
142 
144  ArrayRef<Record *> Classes) {
145  ID.AddInteger(Classes.size());
146  for (Record *R : Classes)
147  ID.AddPointer(R);
148 }
149 
151  if (UnsortedClasses.empty()) {
152  static RecordRecTy AnyRecord(0);
153  return &AnyRecord;
154  }
155 
156  FoldingSet<RecordRecTy> &ThePool =
157  UnsortedClasses[0]->getRecords().RecordTypePool;
158 
159  SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
160  UnsortedClasses.end());
161  llvm::sort(Classes, [](Record *LHS, Record *RHS) {
162  return LHS->getNameInitAsString() < RHS->getNameInitAsString();
163  });
164 
166  ProfileRecordRecTy(ID, Classes);
167 
168  void *IP = nullptr;
169  if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
170  return Ty;
171 
172 #ifndef NDEBUG
173  // Check for redundancy.
174  for (unsigned i = 0; i < Classes.size(); ++i) {
175  for (unsigned j = 0; j < Classes.size(); ++j) {
176  assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
177  }
178  assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
179  }
180 #endif
181 
182  void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()),
183  alignof(RecordRecTy));
184  RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
185  std::uninitialized_copy(Classes.begin(), Classes.end(),
186  Ty->getTrailingObjects<Record *>());
187  ThePool.InsertNode(Ty, IP);
188  return Ty;
189 }
190 
192  ProfileRecordRecTy(ID, getClasses());
193 }
194 
195 std::string RecordRecTy::getAsString() const {
196  if (NumClasses == 1)
197  return getClasses()[0]->getNameInitAsString();
198 
199  std::string Str = "{";
200  bool First = true;
201  for (Record *R : getClasses()) {
202  if (!First)
203  Str += ", ";
204  First = false;
205  Str += R->getNameInitAsString();
206  }
207  Str += "}";
208  return Str;
209 }
210 
211 bool RecordRecTy::isSubClassOf(Record *Class) const {
212  return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
213  return MySuperClass == Class ||
214  MySuperClass->isSubClassOf(Class);
215  });
216 }
217 
218 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
219  if (this == RHS)
220  return true;
221 
222  const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
223  if (!RTy)
224  return false;
225 
226  return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
227  return isSubClassOf(TargetClass);
228  });
229 }
230 
231 bool RecordRecTy::typeIsA(const RecTy *RHS) const {
232  return typeIsConvertibleTo(RHS);
233 }
234 
236  SmallVector<Record *, 4> CommonSuperClasses;
238 
239  Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end());
240 
241  while (!Stack.empty()) {
242  Record *R = Stack.back();
243  Stack.pop_back();
244 
245  if (T2->isSubClassOf(R)) {
246  CommonSuperClasses.push_back(R);
247  } else {
248  R->getDirectSuperClasses(Stack);
249  }
250  }
251 
252  return RecordRecTy::get(CommonSuperClasses);
253 }
254 
256  if (T1 == T2)
257  return T1;
258 
259  if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
260  if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
261  return resolveRecordTypes(RecTy1, RecTy2);
262  }
263 
264  if (T1->typeIsConvertibleTo(T2))
265  return T2;
266  if (T2->typeIsConvertibleTo(T1))
267  return T1;
268 
269  if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
270  if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
271  RecTy* NewType = resolveTypes(ListTy1->getElementType(),
272  ListTy2->getElementType());
273  if (NewType)
274  return NewType->getListTy();
275  }
276  }
277 
278  return nullptr;
279 }
280 
281 //===----------------------------------------------------------------------===//
282 // Initializer implementations
283 //===----------------------------------------------------------------------===//
284 
285 void Init::anchor() {}
286 
287 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
288 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
289 #endif
290 
292  static UnsetInit TheInit;
293  return &TheInit;
294 }
295 
297  return const_cast<UnsetInit *>(this);
298 }
299 
301  return const_cast<UnsetInit *>(this);
302 }
303 
305  static BitInit True(true);
306  static BitInit False(false);
307 
308  return V ? &True : &False;
309 }
310 
312  if (isa<BitRecTy>(Ty))
313  return const_cast<BitInit *>(this);
314 
315  if (isa<IntRecTy>(Ty))
316  return IntInit::get(getValue());
317 
318  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
319  // Can only convert single bit.
320  if (BRT->getNumBits() == 1)
321  return BitsInit::get(const_cast<BitInit *>(this));
322  }
323 
324  return nullptr;
325 }
326 
327 static void
329  ID.AddInteger(Range.size());
330 
331  for (Init *I : Range)
332  ID.AddPointer(I);
333 }
334 
336  static FoldingSet<BitsInit> ThePool;
337 
339  ProfileBitsInit(ID, Range);
340 
341  void *IP = nullptr;
342  if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
343  return I;
344 
345  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
346  alignof(BitsInit));
347  BitsInit *I = new(Mem) BitsInit(Range.size());
348  std::uninitialized_copy(Range.begin(), Range.end(),
349  I->getTrailingObjects<Init *>());
350  ThePool.InsertNode(I, IP);
351  return I;
352 }
353 
355  ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
356 }
357 
359  if (isa<BitRecTy>(Ty)) {
360  if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
361  return getBit(0);
362  }
363 
364  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
365  // If the number of bits is right, return it. Otherwise we need to expand
366  // or truncate.
367  if (getNumBits() != BRT->getNumBits()) return nullptr;
368  return const_cast<BitsInit *>(this);
369  }
370 
371  if (isa<IntRecTy>(Ty)) {
372  int64_t Result = 0;
373  for (unsigned i = 0, e = getNumBits(); i != e; ++i)
374  if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
375  Result |= static_cast<int64_t>(Bit->getValue()) << i;
376  else
377  return nullptr;
378  return IntInit::get(Result);
379  }
380 
381  return nullptr;
382 }
383 
384 Init *
386  SmallVector<Init *, 16> NewBits(Bits.size());
387 
388  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
389  if (Bits[i] >= getNumBits())
390  return nullptr;
391  NewBits[i] = getBit(Bits[i]);
392  }
393  return BitsInit::get(NewBits);
394 }
395 
396 bool BitsInit::isConcrete() const {
397  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
398  if (!getBit(i)->isConcrete())
399  return false;
400  }
401  return true;
402 }
403 
404 std::string BitsInit::getAsString() const {
405  std::string Result = "{ ";
406  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
407  if (i) Result += ", ";
408  if (Init *Bit = getBit(e-i-1))
409  Result += Bit->getAsString();
410  else
411  Result += "*";
412  }
413  return Result + " }";
414 }
415 
416 // resolveReferences - If there are any field references that refer to fields
417 // that have been filled in, we can propagate the values now.
419  bool Changed = false;
420  SmallVector<Init *, 16> NewBits(getNumBits());
421 
422  Init *CachedBitVarRef = nullptr;
423  Init *CachedBitVarResolved = nullptr;
424 
425  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
426  Init *CurBit = getBit(i);
427  Init *NewBit = CurBit;
428 
429  if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
430  if (CurBitVar->getBitVar() != CachedBitVarRef) {
431  CachedBitVarRef = CurBitVar->getBitVar();
432  CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
433  }
434 
435  NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
436  } else {
437  // getBit(0) implicitly converts int and bits<1> values to bit.
438  NewBit = CurBit->resolveReferences(R)->getBit(0);
439  }
440 
441  if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
442  NewBit = CurBit;
443  NewBits[i] = NewBit;
444  Changed |= CurBit != NewBit;
445  }
446 
447  if (Changed)
448  return BitsInit::get(NewBits);
449 
450  return const_cast<BitsInit *>(this);
451 }
452 
453 IntInit *IntInit::get(int64_t V) {
454  static DenseMap<int64_t, IntInit*> ThePool;
455 
456  IntInit *&I = ThePool[V];
457  if (!I) I = new(Allocator) IntInit(V);
458  return I;
459 }
460 
461 std::string IntInit::getAsString() const {
462  return itostr(Value);
463 }
464 
465 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
466  // For example, with NumBits == 4, we permit Values from [-7 .. 15].
467  return (NumBits >= sizeof(Value) * 8) ||
468  (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
469 }
470 
472  if (isa<IntRecTy>(Ty))
473  return const_cast<IntInit *>(this);
474 
475  if (isa<BitRecTy>(Ty)) {
476  int64_t Val = getValue();
477  if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
478  return BitInit::get(Val != 0);
479  }
480 
481  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
482  int64_t Value = getValue();
483  // Make sure this bitfield is large enough to hold the integer value.
484  if (!canFitInBitfield(Value, BRT->getNumBits()))
485  return nullptr;
486 
487  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
488  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
489  NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0));
490 
491  return BitsInit::get(NewBits);
492  }
493 
494  return nullptr;
495 }
496 
497 Init *
499  SmallVector<Init *, 16> NewBits(Bits.size());
500 
501  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
502  if (Bits[i] >= 64)
503  return nullptr;
504 
505  NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
506  }
507  return BitsInit::get(NewBits);
508 }
509 
511  static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
512 
513  auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
514  if (!Entry.second)
515  Entry.second = new(Allocator) CodeInit(Entry.getKey());
516  return Entry.second;
517 }
518 
520  static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
521 
522  auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
523  if (!Entry.second)
524  Entry.second = new(Allocator) StringInit(Entry.getKey());
525  return Entry.second;
526 }
527 
529  if (isa<StringRecTy>(Ty))
530  return const_cast<StringInit *>(this);
531  if (isa<CodeRecTy>(Ty))
532  return CodeInit::get(getValue());
533 
534  return nullptr;
535 }
536 
538  if (isa<CodeRecTy>(Ty))
539  return const_cast<CodeInit *>(this);
540  if (isa<StringRecTy>(Ty))
541  return StringInit::get(getValue());
542 
543  return nullptr;
544 }
545 
547  ArrayRef<Init *> Range,
548  RecTy *EltTy) {
549  ID.AddInteger(Range.size());
550  ID.AddPointer(EltTy);
551 
552  for (Init *I : Range)
553  ID.AddPointer(I);
554 }
555 
557  static FoldingSet<ListInit> ThePool;
558 
560  ProfileListInit(ID, Range, EltTy);
561 
562  void *IP = nullptr;
563  if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
564  return I;
565 
566  assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
567  cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
568 
569  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
570  alignof(ListInit));
571  ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
572  std::uninitialized_copy(Range.begin(), Range.end(),
573  I->getTrailingObjects<Init *>());
574  ThePool.InsertNode(I, IP);
575  return I;
576 }
577 
579  RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
580 
581  ProfileListInit(ID, getValues(), EltTy);
582 }
583 
585  if (getType() == Ty)
586  return const_cast<ListInit*>(this);
587 
588  if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
589  SmallVector<Init*, 8> Elements;
590  Elements.reserve(getValues().size());
591 
592  // Verify that all of the elements of the list are subclasses of the
593  // appropriate class!
594  bool Changed = false;
595  RecTy *ElementType = LRT->getElementType();
596  for (Init *I : getValues())
597  if (Init *CI = I->convertInitializerTo(ElementType)) {
598  Elements.push_back(CI);
599  if (CI != I)
600  Changed = true;
601  } else
602  return nullptr;
603 
604  if (!Changed)
605  return const_cast<ListInit*>(this);
606  return ListInit::get(Elements, ElementType);
607  }
608 
609  return nullptr;
610 }
611 
614  Vals.reserve(Elements.size());
615  for (unsigned Element : Elements) {
616  if (Element >= size())
617  return nullptr;
618  Vals.push_back(getElement(Element));
619  }
620  return ListInit::get(Vals, getElementType());
621 }
622 
624  assert(i < NumValues && "List element index out of range!");
625  DefInit *DI = dyn_cast<DefInit>(getElement(i));
626  if (!DI)
627  PrintFatalError("Expected record in list!");
628  return DI->getDef();
629 }
630 
632  SmallVector<Init*, 8> Resolved;
633  Resolved.reserve(size());
634  bool Changed = false;
635 
636  for (Init *CurElt : getValues()) {
637  Init *E = CurElt->resolveReferences(R);
638  Changed |= E != CurElt;
639  Resolved.push_back(E);
640  }
641 
642  if (Changed)
643  return ListInit::get(Resolved, getElementType());
644  return const_cast<ListInit *>(this);
645 }
646 
647 bool ListInit::isConcrete() const {
648  for (Init *Element : *this) {
649  if (!Element->isConcrete())
650  return false;
651  }
652  return true;
653 }
654 
655 std::string ListInit::getAsString() const {
656  std::string Result = "[";
657  const char *sep = "";
658  for (Init *Element : *this) {
659  Result += sep;
660  sep = ", ";
661  Result += Element->getAsString();
662  }
663  return Result + "]";
664 }
665 
666 Init *OpInit::getBit(unsigned Bit) const {
667  if (getType() == BitRecTy::get())
668  return const_cast<OpInit*>(this);
669  return VarBitInit::get(const_cast<OpInit*>(this), Bit);
670 }
671 
672 static void
674  ID.AddInteger(Opcode);
675  ID.AddPointer(Op);
676  ID.AddPointer(Type);
677 }
678 
680  static FoldingSet<UnOpInit> ThePool;
681 
683  ProfileUnOpInit(ID, Opc, LHS, Type);
684 
685  void *IP = nullptr;
686  if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
687  return I;
688 
689  UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
690  ThePool.InsertNode(I, IP);
691  return I;
692 }
693 
695  ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
696 }
697 
698 Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
699  switch (getOpcode()) {
700  case CAST:
701  if (isa<StringRecTy>(getType())) {
702  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
703  return LHSs;
704 
705  if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
706  return StringInit::get(LHSd->getAsString());
707 
708  if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
709  return StringInit::get(LHSi->getAsString());
710  } else if (isa<RecordRecTy>(getType())) {
711  if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
712  if (!CurRec && !IsFinal)
713  break;
714  assert(CurRec && "NULL pointer");
715  Record *D;
716 
717  // Self-references are allowed, but their resolution is delayed until
718  // the final resolve to ensure that we get the correct type for them.
719  if (Name == CurRec->getNameInit()) {
720  if (!IsFinal)
721  break;
722  D = CurRec;
723  } else {
724  D = CurRec->getRecords().getDef(Name->getValue());
725  if (!D) {
726  if (IsFinal)
727  PrintFatalError(CurRec->getLoc(),
728  Twine("Undefined reference to record: '") +
729  Name->getValue() + "'\n");
730  break;
731  }
732  }
733 
734  DefInit *DI = DefInit::get(D);
735  if (!DI->getType()->typeIsA(getType())) {
736  PrintFatalError(CurRec->getLoc(),
737  Twine("Expected type '") +
738  getType()->getAsString() + "', got '" +
739  DI->getType()->getAsString() + "' in: " +
740  getAsString() + "\n");
741  }
742  return DI;
743  }
744  }
745 
746  if (Init *NewInit = LHS->convertInitializerTo(getType()))
747  return NewInit;
748  break;
749 
750  case HEAD:
751  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
752  assert(!LHSl->empty() && "Empty list in head");
753  return LHSl->getElement(0);
754  }
755  break;
756 
757  case TAIL:
758  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
759  assert(!LHSl->empty() && "Empty list in tail");
760  // Note the +1. We can't just pass the result of getValues()
761  // directly.
762  return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
763  }
764  break;
765 
766  case SIZE:
767  if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
768  return IntInit::get(LHSl->size());
769  break;
770 
771  case EMPTY:
772  if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
773  return IntInit::get(LHSl->empty());
774  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
775  return IntInit::get(LHSs->getValue().empty());
776  break;
777  }
778  return const_cast<UnOpInit *>(this);
779 }
780 
782  Init *lhs = LHS->resolveReferences(R);
783 
784  if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
785  return (UnOpInit::get(getOpcode(), lhs, getType()))
786  ->Fold(R.getCurrentRecord(), R.isFinal());
787  return const_cast<UnOpInit *>(this);
788 }
789 
790 std::string UnOpInit::getAsString() const {
791  std::string Result;
792  switch (getOpcode()) {
793  case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
794  case HEAD: Result = "!head"; break;
795  case TAIL: Result = "!tail"; break;
796  case SIZE: Result = "!size"; break;
797  case EMPTY: Result = "!empty"; break;
798  }
799  return Result + "(" + LHS->getAsString() + ")";
800 }
801 
802 static void
803 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
804  RecTy *Type) {
805  ID.AddInteger(Opcode);
806  ID.AddPointer(LHS);
807  ID.AddPointer(RHS);
808  ID.AddPointer(Type);
809 }
810 
812  Init *RHS, RecTy *Type) {
813  static FoldingSet<BinOpInit> ThePool;
814 
816  ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
817 
818  void *IP = nullptr;
819  if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
820  return I;
821 
822  BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
823  ThePool.InsertNode(I, IP);
824  return I;
825 }
826 
828  ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
829 }
830 
832  const StringInit *I1) {
834  Concat.append(I1->getValue());
835  return StringInit::get(Concat);
836 }
837 
839  // Shortcut for the common case of concatenating two strings.
840  if (const StringInit *I0s = dyn_cast<StringInit>(I0))
841  if (const StringInit *I1s = dyn_cast<StringInit>(I1))
842  return ConcatStringInits(I0s, I1s);
844 }
845 
846 Init *BinOpInit::Fold(Record *CurRec) const {
847  switch (getOpcode()) {
848  case CONCAT: {
849  DagInit *LHSs = dyn_cast<DagInit>(LHS);
850  DagInit *RHSs = dyn_cast<DagInit>(RHS);
851  if (LHSs && RHSs) {
852  DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
853  DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
854  if (!LOp || !ROp)
855  break;
856  if (LOp->getDef() != ROp->getDef()) {
857  PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
858  LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
859  "'");
860  }
863  for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
864  Args.push_back(LHSs->getArg(i));
865  ArgNames.push_back(LHSs->getArgName(i));
866  }
867  for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
868  Args.push_back(RHSs->getArg(i));
869  ArgNames.push_back(RHSs->getArgName(i));
870  }
871  return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
872  }
873  break;
874  }
875  case LISTCONCAT: {
876  ListInit *LHSs = dyn_cast<ListInit>(LHS);
877  ListInit *RHSs = dyn_cast<ListInit>(RHS);
878  if (LHSs && RHSs) {
880  Args.insert(Args.end(), LHSs->begin(), LHSs->end());
881  Args.insert(Args.end(), RHSs->begin(), RHSs->end());
882  return ListInit::get(Args, LHSs->getElementType());
883  }
884  break;
885  }
886  case STRCONCAT: {
887  StringInit *LHSs = dyn_cast<StringInit>(LHS);
888  StringInit *RHSs = dyn_cast<StringInit>(RHS);
889  if (LHSs && RHSs)
890  return ConcatStringInits(LHSs, RHSs);
891  break;
892  }
893  case EQ:
894  case NE:
895  case LE:
896  case LT:
897  case GE:
898  case GT: {
899  // try to fold eq comparison for 'bit' and 'int', otherwise fallback
900  // to string objects.
901  IntInit *L =
902  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
903  IntInit *R =
904  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
905 
906  if (L && R) {
907  bool Result;
908  switch (getOpcode()) {
909  case EQ: Result = L->getValue() == R->getValue(); break;
910  case NE: Result = L->getValue() != R->getValue(); break;
911  case LE: Result = L->getValue() <= R->getValue(); break;
912  case LT: Result = L->getValue() < R->getValue(); break;
913  case GE: Result = L->getValue() >= R->getValue(); break;
914  case GT: Result = L->getValue() > R->getValue(); break;
915  default: llvm_unreachable("unhandled comparison");
916  }
917  return BitInit::get(Result);
918  }
919 
920  if (getOpcode() == EQ || getOpcode() == NE) {
921  StringInit *LHSs = dyn_cast<StringInit>(LHS);
922  StringInit *RHSs = dyn_cast<StringInit>(RHS);
923 
924  // Make sure we've resolved
925  if (LHSs && RHSs) {
926  bool Equal = LHSs->getValue() == RHSs->getValue();
927  return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
928  }
929  }
930 
931  break;
932  }
933  case ADD:
934  case AND:
935  case OR:
936  case SHL:
937  case SRA:
938  case SRL: {
939  IntInit *LHSi =
940  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
941  IntInit *RHSi =
942  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
943  if (LHSi && RHSi) {
944  int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
945  int64_t Result;
946  switch (getOpcode()) {
947  default: llvm_unreachable("Bad opcode!");
948  case ADD: Result = LHSv + RHSv; break;
949  case AND: Result = LHSv & RHSv; break;
950  case OR: Result = LHSv | RHSv; break;
951  case SHL: Result = LHSv << RHSv; break;
952  case SRA: Result = LHSv >> RHSv; break;
953  case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
954  }
955  return IntInit::get(Result);
956  }
957  break;
958  }
959  }
960  return const_cast<BinOpInit *>(this);
961 }
962 
964  Init *lhs = LHS->resolveReferences(R);
965  Init *rhs = RHS->resolveReferences(R);
966 
967  if (LHS != lhs || RHS != rhs)
968  return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
969  ->Fold(R.getCurrentRecord());
970  return const_cast<BinOpInit *>(this);
971 }
972 
973 std::string BinOpInit::getAsString() const {
974  std::string Result;
975  switch (getOpcode()) {
976  case CONCAT: Result = "!con"; break;
977  case ADD: Result = "!add"; break;
978  case AND: Result = "!and"; break;
979  case OR: Result = "!or"; break;
980  case SHL: Result = "!shl"; break;
981  case SRA: Result = "!sra"; break;
982  case SRL: Result = "!srl"; break;
983  case EQ: Result = "!eq"; break;
984  case NE: Result = "!ne"; break;
985  case LE: Result = "!le"; break;
986  case LT: Result = "!lt"; break;
987  case GE: Result = "!ge"; break;
988  case GT: Result = "!gt"; break;
989  case LISTCONCAT: Result = "!listconcat"; break;
990  case STRCONCAT: Result = "!strconcat"; break;
991  }
992  return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
993 }
994 
995 static void
996 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
997  Init *RHS, RecTy *Type) {
998  ID.AddInteger(Opcode);
999  ID.AddPointer(LHS);
1000  ID.AddPointer(MHS);
1001  ID.AddPointer(RHS);
1002  ID.AddPointer(Type);
1003 }
1004 
1006  RecTy *Type) {
1007  static FoldingSet<TernOpInit> ThePool;
1008 
1010  ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
1011 
1012  void *IP = nullptr;
1013  if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1014  return I;
1015 
1016  TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
1017  ThePool.InsertNode(I, IP);
1018  return I;
1019 }
1020 
1022  ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1023 }
1024 
1025 static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
1026  MapResolver R(CurRec);
1027  R.set(LHS, MHSe);
1028  return RHS->resolveReferences(R);
1029 }
1030 
1031 static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
1032  Record *CurRec) {
1033  bool Change = false;
1034  Init *Val = ForeachApply(LHS, MHSd->getOperator(), RHS, CurRec);
1035  if (Val != MHSd->getOperator())
1036  Change = true;
1037 
1039  for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1040  Init *Arg = MHSd->getArg(i);
1041  Init *NewArg;
1042  StringInit *ArgName = MHSd->getArgName(i);
1043 
1044  if (DagInit *Argd = dyn_cast<DagInit>(Arg))
1045  NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
1046  else
1047  NewArg = ForeachApply(LHS, Arg, RHS, CurRec);
1048 
1049  NewArgs.push_back(std::make_pair(NewArg, ArgName));
1050  if (Arg != NewArg)
1051  Change = true;
1052  }
1053 
1054  if (Change)
1055  return DagInit::get(Val, nullptr, NewArgs);
1056  return MHSd;
1057 }
1058 
1059 // Applies RHS to all elements of MHS, using LHS as a temp variable.
1060 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1061  Record *CurRec) {
1062  if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
1063  return ForeachDagApply(LHS, MHSd, RHS, CurRec);
1064 
1065  if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
1066  SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1067 
1068  for (Init *&Item : NewList) {
1069  Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec);
1070  if (NewItem != Item)
1071  Item = NewItem;
1072  }
1073  return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1074  }
1075 
1076  return nullptr;
1077 }
1078 
1079 Init *TernOpInit::Fold(Record *CurRec) const {
1080  switch (getOpcode()) {
1081  case SUBST: {
1082  DefInit *LHSd = dyn_cast<DefInit>(LHS);
1083  VarInit *LHSv = dyn_cast<VarInit>(LHS);
1084  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1085 
1086  DefInit *MHSd = dyn_cast<DefInit>(MHS);
1087  VarInit *MHSv = dyn_cast<VarInit>(MHS);
1088  StringInit *MHSs = dyn_cast<StringInit>(MHS);
1089 
1090  DefInit *RHSd = dyn_cast<DefInit>(RHS);
1091  VarInit *RHSv = dyn_cast<VarInit>(RHS);
1092  StringInit *RHSs = dyn_cast<StringInit>(RHS);
1093 
1094  if (LHSd && MHSd && RHSd) {
1095  Record *Val = RHSd->getDef();
1096  if (LHSd->getAsString() == RHSd->getAsString())
1097  Val = MHSd->getDef();
1098  return DefInit::get(Val);
1099  }
1100  if (LHSv && MHSv && RHSv) {
1101  std::string Val = RHSv->getName();
1102  if (LHSv->getAsString() == RHSv->getAsString())
1103  Val = MHSv->getName();
1104  return VarInit::get(Val, getType());
1105  }
1106  if (LHSs && MHSs && RHSs) {
1107  std::string Val = RHSs->getValue();
1108 
1109  std::string::size_type found;
1110  std::string::size_type idx = 0;
1111  while (true) {
1112  found = Val.find(LHSs->getValue(), idx);
1113  if (found == std::string::npos)
1114  break;
1115  Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1116  idx = found + MHSs->getValue().size();
1117  }
1118 
1119  return StringInit::get(Val);
1120  }
1121  break;
1122  }
1123 
1124  case FOREACH: {
1125  if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
1126  return Result;
1127  break;
1128  }
1129 
1130  case IF: {
1131  if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
1132  LHS->convertInitializerTo(IntRecTy::get()))) {
1133  if (LHSi->getValue())
1134  return MHS;
1135  return RHS;
1136  }
1137  break;
1138  }
1139 
1140  case DAG: {
1141  ListInit *MHSl = dyn_cast<ListInit>(MHS);
1142  ListInit *RHSl = dyn_cast<ListInit>(RHS);
1143  bool MHSok = MHSl || isa<UnsetInit>(MHS);
1144  bool RHSok = RHSl || isa<UnsetInit>(RHS);
1145 
1146  if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
1147  break; // Typically prevented by the parser, but might happen with template args
1148 
1149  if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
1151  unsigned Size = MHSl ? MHSl->size() : RHSl->size();
1152  for (unsigned i = 0; i != Size; ++i) {
1153  Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
1154  Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
1155  if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
1156  return const_cast<TernOpInit *>(this);
1157  Children.emplace_back(Node, dyn_cast<StringInit>(Name));
1158  }
1159  return DagInit::get(LHS, nullptr, Children);
1160  }
1161  break;
1162  }
1163  }
1164 
1165  return const_cast<TernOpInit *>(this);
1166 }
1167 
1169  Init *lhs = LHS->resolveReferences(R);
1170 
1171  if (getOpcode() == IF && lhs != LHS) {
1172  if (IntInit *Value = dyn_cast_or_null<IntInit>(
1174  // Short-circuit
1175  if (Value->getValue())
1176  return MHS->resolveReferences(R);
1177  return RHS->resolveReferences(R);
1178  }
1179  }
1180 
1181  Init *mhs = MHS->resolveReferences(R);
1182  Init *rhs;
1183 
1184  if (getOpcode() == FOREACH) {
1185  ShadowResolver SR(R);
1186  SR.addShadow(lhs);
1187  rhs = RHS->resolveReferences(SR);
1188  } else {
1189  rhs = RHS->resolveReferences(R);
1190  }
1191 
1192  if (LHS != lhs || MHS != mhs || RHS != rhs)
1193  return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
1194  ->Fold(R.getCurrentRecord());
1195  return const_cast<TernOpInit *>(this);
1196 }
1197 
1198 std::string TernOpInit::getAsString() const {
1199  std::string Result;
1200  bool UnquotedLHS = false;
1201  switch (getOpcode()) {
1202  case SUBST: Result = "!subst"; break;
1203  case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
1204  case IF: Result = "!if"; break;
1205  case DAG: Result = "!dag"; break;
1206  }
1207  return (Result + "(" +
1208  (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
1209  ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
1210 }
1211 
1213  Init *Start, Init *List, Init *Expr,
1214  RecTy *Type) {
1215  ID.AddPointer(Start);
1216  ID.AddPointer(List);
1217  ID.AddPointer(A);
1218  ID.AddPointer(B);
1219  ID.AddPointer(Expr);
1220  ID.AddPointer(Type);
1221 }
1222 
1224  Init *Expr, RecTy *Type) {
1225  static FoldingSet<FoldOpInit> ThePool;
1226 
1228  ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
1229 
1230  void *IP = nullptr;
1231  if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1232  return I;
1233 
1234  FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
1235  ThePool.InsertNode(I, IP);
1236  return I;
1237 }
1238 
1240  ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
1241 }
1242 
1243 Init *FoldOpInit::Fold(Record *CurRec) const {
1244  if (ListInit *LI = dyn_cast<ListInit>(List)) {
1245  Init *Accum = Start;
1246  for (Init *Elt : *LI) {
1247  MapResolver R(CurRec);
1248  R.set(A, Accum);
1249  R.set(B, Elt);
1250  Accum = Expr->resolveReferences(R);
1251  }
1252  return Accum;
1253  }
1254  return const_cast<FoldOpInit *>(this);
1255 }
1256 
1258  Init *NewStart = Start->resolveReferences(R);
1259  Init *NewList = List->resolveReferences(R);
1260  ShadowResolver SR(R);
1261  SR.addShadow(A);
1262  SR.addShadow(B);
1263  Init *NewExpr = Expr->resolveReferences(SR);
1264 
1265  if (Start == NewStart && List == NewList && Expr == NewExpr)
1266  return const_cast<FoldOpInit *>(this);
1267 
1268  return get(NewStart, NewList, A, B, NewExpr, getType())
1269  ->Fold(R.getCurrentRecord());
1270 }
1271 
1272 Init *FoldOpInit::getBit(unsigned Bit) const {
1273  return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
1274 }
1275 
1276 std::string FoldOpInit::getAsString() const {
1277  return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
1278  ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
1279  ", " + Expr->getAsString() + ")")
1280  .str();
1281 }
1282 
1284  Init *Expr) {
1285  ID.AddPointer(CheckType);
1286  ID.AddPointer(Expr);
1287 }
1288 
1290  static FoldingSet<IsAOpInit> ThePool;
1291 
1293  ProfileIsAOpInit(ID, CheckType, Expr);
1294 
1295  void *IP = nullptr;
1296  if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1297  return I;
1298 
1299  IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
1300  ThePool.InsertNode(I, IP);
1301  return I;
1302 }
1303 
1305  ProfileIsAOpInit(ID, CheckType, Expr);
1306 }
1307 
1309  if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
1310  // Is the expression type known to be (a subclass of) the desired type?
1311  if (TI->getType()->typeIsConvertibleTo(CheckType))
1312  return IntInit::get(1);
1313 
1314  if (isa<RecordRecTy>(CheckType)) {
1315  // If the target type is not a subclass of the expression type, or if
1316  // the expression has fully resolved to a record, we know that it can't
1317  // be of the required type.
1318  if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
1319  return IntInit::get(0);
1320  } else {
1321  // We treat non-record types as not castable.
1322  return IntInit::get(0);
1323  }
1324  }
1325  return const_cast<IsAOpInit *>(this);
1326 }
1327 
1329  Init *NewExpr = Expr->resolveReferences(R);
1330  if (Expr != NewExpr)
1331  return get(CheckType, NewExpr)->Fold();
1332  return const_cast<IsAOpInit *>(this);
1333 }
1334 
1335 Init *IsAOpInit::getBit(unsigned Bit) const {
1336  return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
1337 }
1338 
1339 std::string IsAOpInit::getAsString() const {
1340  return (Twine("!isa<") + CheckType->getAsString() + ">(" +
1341  Expr->getAsString() + ")")
1342  .str();
1343 }
1344 
1346  if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
1347  for (Record *Rec : RecordType->getClasses()) {
1348  if (RecordVal *Field = Rec->getValue(FieldName))
1349  return Field->getType();
1350  }
1351  }
1352  return nullptr;
1353 }
1354 
1355 Init *
1357  if (getType() == Ty || getType()->typeIsA(Ty))
1358  return const_cast<TypedInit *>(this);
1359 
1360  if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
1361  cast<BitsRecTy>(Ty)->getNumBits() == 1)
1362  return BitsInit::get({const_cast<TypedInit *>(this)});
1363 
1364  return nullptr;
1365 }
1366 
1369  if (!T) return nullptr; // Cannot subscript a non-bits variable.
1370  unsigned NumBits = T->getNumBits();
1371 
1372  SmallVector<Init *, 16> NewBits;
1373  NewBits.reserve(Bits.size());
1374  for (unsigned Bit : Bits) {
1375  if (Bit >= NumBits)
1376  return nullptr;
1377 
1378  NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1379  }
1380  return BitsInit::get(NewBits);
1381 }
1382 
1384  // Handle the common case quickly
1385  if (getType() == Ty || getType()->typeIsA(Ty))
1386  return const_cast<TypedInit *>(this);
1387 
1388  if (Init *Converted = convertInitializerTo(Ty)) {
1389  assert(!isa<TypedInit>(Converted) ||
1390  cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
1391  return Converted;
1392  }
1393 
1394  if (!getType()->typeIsConvertibleTo(Ty))
1395  return nullptr;
1396 
1397  return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
1398  ->Fold(nullptr);
1399 }
1400 
1403  if (!T) return nullptr; // Cannot subscript a non-list variable.
1404 
1405  if (Elements.size() == 1)
1406  return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1407 
1408  SmallVector<Init*, 8> ListInits;
1409  ListInits.reserve(Elements.size());
1410  for (unsigned Element : Elements)
1411  ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1412  Element));
1413  return ListInit::get(ListInits, T->getElementType());
1414 }
1415 
1416 
1418  Init *Value = StringInit::get(VN);
1419  return VarInit::get(Value, T);
1420 }
1421 
1423  using Key = std::pair<RecTy *, Init *>;
1424  static DenseMap<Key, VarInit*> ThePool;
1425 
1426  Key TheKey(std::make_pair(T, VN));
1427 
1428  VarInit *&I = ThePool[TheKey];
1429  if (!I)
1430  I = new(Allocator) VarInit(VN, T);
1431  return I;
1432 }
1433 
1435  StringInit *NameString = cast<StringInit>(getNameInit());
1436  return NameString->getValue();
1437 }
1438 
1439 Init *VarInit::getBit(unsigned Bit) const {
1440  if (getType() == BitRecTy::get())
1441  return const_cast<VarInit*>(this);
1442  return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1443 }
1444 
1446  if (Init *Val = R.resolve(VarName))
1447  return Val;
1448  return const_cast<VarInit *>(this);
1449 }
1450 
1452  using Key = std::pair<TypedInit *, unsigned>;
1453  static DenseMap<Key, VarBitInit*> ThePool;
1454 
1455  Key TheKey(std::make_pair(T, B));
1456 
1457  VarBitInit *&I = ThePool[TheKey];
1458  if (!I)
1459  I = new(Allocator) VarBitInit(T, B);
1460  return I;
1461 }
1462 
1463 std::string VarBitInit::getAsString() const {
1464  return TI->getAsString() + "{" + utostr(Bit) + "}";
1465 }
1466 
1468  Init *I = TI->resolveReferences(R);
1469  if (TI != I)
1470  return I->getBit(getBitNum());
1471 
1472  return const_cast<VarBitInit*>(this);
1473 }
1474 
1476  unsigned E) {
1477  using Key = std::pair<TypedInit *, unsigned>;
1478  static DenseMap<Key, VarListElementInit*> ThePool;
1479 
1480  Key TheKey(std::make_pair(T, E));
1481 
1482  VarListElementInit *&I = ThePool[TheKey];
1483  if (!I) I = new(Allocator) VarListElementInit(T, E);
1484  return I;
1485 }
1486 
1487 std::string VarListElementInit::getAsString() const {
1488  return TI->getAsString() + "[" + utostr(Element) + "]";
1489 }
1490 
1492  Init *NewTI = TI->resolveReferences(R);
1493  if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1494  // Leave out-of-bounds array references as-is. This can happen without
1495  // being an error, e.g. in the untaken "branch" of an !if expression.
1496  if (getElementNum() < List->size())
1497  return List->getElement(getElementNum());
1498  }
1499  if (NewTI != TI && isa<TypedInit>(NewTI))
1500  return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1501  return const_cast<VarListElementInit *>(this);
1502 }
1503 
1505  if (getType() == BitRecTy::get())
1506  return const_cast<VarListElementInit*>(this);
1507  return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1508 }
1509 
1510 DefInit::DefInit(Record *D)
1511  : TypedInit(IK_DefInit, D->getType()), Def(D) {}
1512 
1514  return R->getDefInit();
1515 }
1516 
1518  if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1519  if (getType()->typeIsConvertibleTo(RRT))
1520  return const_cast<DefInit *>(this);
1521  return nullptr;
1522 }
1523 
1525  if (const RecordVal *RV = Def->getValue(FieldName))
1526  return RV->getType();
1527  return nullptr;
1528 }
1529 
1530 std::string DefInit::getAsString() const {
1531  return Def->getName();
1532 }
1533 
1535  Record *Class,
1537  ID.AddInteger(Args.size());
1538  ID.AddPointer(Class);
1539 
1540  for (Init *I : Args)
1541  ID.AddPointer(I);
1542 }
1543 
1545  static FoldingSet<VarDefInit> ThePool;
1546 
1548  ProfileVarDefInit(ID, Class, Args);
1549 
1550  void *IP = nullptr;
1551  if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1552  return I;
1553 
1554  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
1555  alignof(VarDefInit));
1556  VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
1557  std::uninitialized_copy(Args.begin(), Args.end(),
1558  I->getTrailingObjects<Init *>());
1559  ThePool.InsertNode(I, IP);
1560  return I;
1561 }
1562 
1564  ProfileVarDefInit(ID, Class, args());
1565 }
1566 
1567 DefInit *VarDefInit::instantiate() {
1568  if (!Def) {
1569  RecordKeeper &Records = Class->getRecords();
1570  auto NewRecOwner = make_unique<Record>(Records.getNewAnonymousName(),
1571  Class->getLoc(), Records,
1572  /*IsAnonymous=*/true);
1573  Record *NewRec = NewRecOwner.get();
1574 
1575  // Copy values from class to instance
1576  for (const RecordVal &Val : Class->getValues())
1577  NewRec->addValue(Val);
1578 
1579  // Substitute and resolve template arguments
1580  ArrayRef<Init *> TArgs = Class->getTemplateArgs();
1581  MapResolver R(NewRec);
1582 
1583  for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1584  if (i < args_size())
1585  R.set(TArgs[i], getArg(i));
1586  else
1587  R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
1588 
1589  NewRec->removeValue(TArgs[i]);
1590  }
1591 
1592  NewRec->resolveReferences(R);
1593 
1594  // Add superclasses.
1595  ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
1596  for (const auto &SCPair : SCs)
1597  NewRec->addSuperClass(SCPair.first, SCPair.second);
1598 
1599  NewRec->addSuperClass(Class,
1600  SMRange(Class->getLoc().back(),
1601  Class->getLoc().back()));
1602 
1603  // Resolve internal references and store in record keeper
1604  NewRec->resolveReferences();
1605  Records.addDef(std::move(NewRecOwner));
1606 
1607  Def = DefInit::get(NewRec);
1608  }
1609 
1610  return Def;
1611 }
1612 
1614  TrackUnresolvedResolver UR(&R);
1615  bool Changed = false;
1616  SmallVector<Init *, 8> NewArgs;
1617  NewArgs.reserve(args_size());
1618 
1619  for (Init *Arg : args()) {
1620  Init *NewArg = Arg->resolveReferences(UR);
1621  NewArgs.push_back(NewArg);
1622  Changed |= NewArg != Arg;
1623  }
1624 
1625  if (Changed) {
1626  auto New = VarDefInit::get(Class, NewArgs);
1627  if (!UR.foundUnresolved())
1628  return New->instantiate();
1629  return New;
1630  }
1631  return const_cast<VarDefInit *>(this);
1632 }
1633 
1635  if (Def)
1636  return Def;
1637 
1639  for (Init *Arg : args())
1640  Arg->resolveReferences(R);
1641 
1642  if (!R.foundUnresolved())
1643  return const_cast<VarDefInit *>(this)->instantiate();
1644  return const_cast<VarDefInit *>(this);
1645 }
1646 
1647 std::string VarDefInit::getAsString() const {
1648  std::string Result = Class->getNameInitAsString() + "<";
1649  const char *sep = "";
1650  for (Init *Arg : args()) {
1651  Result += sep;
1652  sep = ", ";
1653  Result += Arg->getAsString();
1654  }
1655  return Result + ">";
1656 }
1657 
1659  using Key = std::pair<Init *, StringInit *>;
1660  static DenseMap<Key, FieldInit*> ThePool;
1661 
1662  Key TheKey(std::make_pair(R, FN));
1663 
1664  FieldInit *&I = ThePool[TheKey];
1665  if (!I) I = new(Allocator) FieldInit(R, FN);
1666  return I;
1667 }
1668 
1669 Init *FieldInit::getBit(unsigned Bit) const {
1670  if (getType() == BitRecTy::get())
1671  return const_cast<FieldInit*>(this);
1672  return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1673 }
1674 
1676  Init *NewRec = Rec->resolveReferences(R);
1677  if (NewRec != Rec)
1678  return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
1679  return const_cast<FieldInit *>(this);
1680 }
1681 
1682 Init *FieldInit::Fold(Record *CurRec) const {
1683  if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1684  Record *Def = DI->getDef();
1685  if (Def == CurRec)
1686  PrintFatalError(CurRec->getLoc(),
1687  Twine("Attempting to access field '") +
1688  FieldName->getAsUnquotedString() + "' of '" +
1689  Rec->getAsString() + "' is a forbidden self-reference");
1690  Init *FieldVal = Def->getValue(FieldName)->getValue();
1691  if (FieldVal->isComplete())
1692  return FieldVal;
1693  }
1694  return const_cast<FieldInit *>(this);
1695 }
1696 
1698  ArrayRef<Init *> ArgRange,
1699  ArrayRef<StringInit *> NameRange) {
1700  ID.AddPointer(V);
1701  ID.AddPointer(VN);
1702 
1703  ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1705  while (Arg != ArgRange.end()) {
1706  assert(Name != NameRange.end() && "Arg name underflow!");
1707  ID.AddPointer(*Arg++);
1708  ID.AddPointer(*Name++);
1709  }
1710  assert(Name == NameRange.end() && "Arg name overflow!");
1711 }
1712 
1713 DagInit *
1715  ArrayRef<StringInit *> NameRange) {
1716  static FoldingSet<DagInit> ThePool;
1717 
1719  ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1720 
1721  void *IP = nullptr;
1722  if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1723  return I;
1724 
1725  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1726  DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1727  std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1728  I->getTrailingObjects<Init *>());
1729  std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1730  I->getTrailingObjects<StringInit *>());
1731  ThePool.InsertNode(I, IP);
1732  return I;
1733 }
1734 
1735 DagInit *
1737  ArrayRef<std::pair<Init*, StringInit*>> args) {
1740 
1741  for (const auto &Arg : args) {
1742  Args.push_back(Arg.first);
1743  Names.push_back(Arg.second);
1744  }
1745 
1746  return DagInit::get(V, VN, Args, Names);
1747 }
1748 
1750  ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1751 }
1752 
1754  SmallVector<Init*, 8> NewArgs;
1755  NewArgs.reserve(arg_size());
1756  bool ArgsChanged = false;
1757  for (const Init *Arg : getArgs()) {
1758  Init *NewArg = Arg->resolveReferences(R);
1759  NewArgs.push_back(NewArg);
1760  ArgsChanged |= NewArg != Arg;
1761  }
1762 
1763  Init *Op = Val->resolveReferences(R);
1764  if (Op != Val || ArgsChanged)
1765  return DagInit::get(Op, ValName, NewArgs, getArgNames());
1766 
1767  return const_cast<DagInit *>(this);
1768 }
1769 
1770 bool DagInit::isConcrete() const {
1771  if (!Val->isConcrete())
1772  return false;
1773  for (const Init *Elt : getArgs()) {
1774  if (!Elt->isConcrete())
1775  return false;
1776  }
1777  return true;
1778 }
1779 
1780 std::string DagInit::getAsString() const {
1781  std::string Result = "(" + Val->getAsString();
1782  if (ValName)
1783  Result += ":" + ValName->getAsUnquotedString();
1784  if (!arg_empty()) {
1785  Result += " " + getArg(0)->getAsString();
1786  if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1787  for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1788  Result += ", " + getArg(i)->getAsString();
1789  if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1790  }
1791  }
1792  return Result + ")";
1793 }
1794 
1795 //===----------------------------------------------------------------------===//
1796 // Other implementations
1797 //===----------------------------------------------------------------------===//
1798 
1800  : Name(N), TyAndPrefix(T, P) {
1802  assert(Value && "Cannot create unset value for current type!");
1803 }
1804 
1806  return cast<StringInit>(getNameInit())->getValue();
1807 }
1808 
1810  if (V) {
1811  Value = V->getCastTo(getType());
1812  if (Value) {
1813  assert(!isa<TypedInit>(Value) ||
1814  cast<TypedInit>(Value)->getType()->typeIsA(getType()));
1815  if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
1816  if (!isa<BitsInit>(Value)) {
1818  Bits.reserve(BTy->getNumBits());
1819  for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i)
1820  Bits.push_back(Value->getBit(i));
1821  Value = BitsInit::get(Bits);
1822  }
1823  }
1824  }
1825  return Value == nullptr;
1826  }
1827  Value = nullptr;
1828  return false;
1829 }
1830 
1831 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1832 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1833 #endif
1834 
1835 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1836  if (getPrefix()) OS << "field ";
1837  OS << *getType() << " " << getNameInitAsString();
1838 
1839  if (getValue())
1840  OS << " = " << *getValue();
1841 
1842  if (PrintSem) OS << ";\n";
1843 }
1844 
1845 unsigned Record::LastID = 0;
1846 
1847 void Record::checkName() {
1848  // Ensure the record name has string type.
1849  const TypedInit *TypedName = cast<const TypedInit>(Name);
1850  if (!isa<StringRecTy>(TypedName->getType()))
1851  PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
1852  "' is not a string!");
1853 }
1854 
1856  SmallVector<Record *, 4> DirectSCs;
1857  getDirectSuperClasses(DirectSCs);
1858  return RecordRecTy::get(DirectSCs);
1859 }
1860 
1862  if (!TheInit)
1863  TheInit = new(Allocator) DefInit(this);
1864  return TheInit;
1865 }
1866 
1867 void Record::setName(Init *NewName) {
1868  Name = NewName;
1869  checkName();
1870  // DO NOT resolve record values to the name at this point because
1871  // there might be default values for arguments of this def. Those
1872  // arguments might not have been resolved yet so we don't want to
1873  // prematurely assume values for those arguments were not passed to
1874  // this def.
1875  //
1876  // Nonetheless, it may be that some of this Record's values
1877  // reference the record name. Indeed, the reason for having the
1878  // record name be an Init is to provide this flexibility. The extra
1879  // resolve steps after completely instantiating defs takes care of
1880  // this. See TGParser::ParseDef and TGParser::ParseDefm.
1881 }
1882 
1884  ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
1885  while (!SCs.empty()) {
1886  // Superclasses are in reverse preorder, so 'back' is a direct superclass,
1887  // and its transitive superclasses are directly preceding it.
1888  Record *SC = SCs.back().first;
1889  SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
1890  Classes.push_back(SC);
1891  }
1892 }
1893 
1895  for (RecordVal &Value : Values) {
1896  if (SkipVal == &Value) // Skip resolve the same field as the given one
1897  continue;
1898  if (Init *V = Value.getValue()) {
1899  Init *VR = V->resolveReferences(R);
1900  if (Value.setValue(VR)) {
1901  std::string Type;
1902  if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
1903  Type =
1904  (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
1905  PrintFatalError(getLoc(), Twine("Invalid value ") + Type +
1906  "is found when setting '" +
1907  Value.getNameInitAsString() +
1908  "' of type '" +
1909  Value.getType()->getAsString() +
1910  "' after resolving references: " +
1911  VR->getAsUnquotedString() + "\n");
1912  }
1913  }
1914  }
1915  Init *OldName = getNameInit();
1916  Init *NewName = Name->resolveReferences(R);
1917  if (NewName != OldName) {
1918  // Re-register with RecordKeeper.
1919  setName(NewName);
1920  }
1921 }
1922 
1924  RecordResolver R(*this);
1925  R.setFinal(true);
1926  resolveReferences(R);
1927 }
1928 
1930  RecordValResolver R(*this, RV);
1931  resolveReferences(R, RV);
1932 }
1933 
1934 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1935 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1936 #endif
1937 
1939  OS << R.getNameInitAsString();
1940 
1941  ArrayRef<Init *> TArgs = R.getTemplateArgs();
1942  if (!TArgs.empty()) {
1943  OS << "<";
1944  bool NeedComma = false;
1945  for (const Init *TA : TArgs) {
1946  if (NeedComma) OS << ", ";
1947  NeedComma = true;
1948  const RecordVal *RV = R.getValue(TA);
1949  assert(RV && "Template argument record not found??");
1950  RV->print(OS, false);
1951  }
1952  OS << ">";
1953  }
1954 
1955  OS << " {";
1957  if (!SC.empty()) {
1958  OS << "\t//";
1959  for (const auto &SuperPair : SC)
1960  OS << " " << SuperPair.first->getNameInitAsString();
1961  }
1962  OS << "\n";
1963 
1964  for (const RecordVal &Val : R.getValues())
1965  if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1966  OS << Val;
1967  for (const RecordVal &Val : R.getValues())
1968  if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1969  OS << Val;
1970 
1971  return OS << "}\n";
1972 }
1973 
1975  const RecordVal *R = getValue(FieldName);
1976  if (!R || !R->getValue())
1977  PrintFatalError(getLoc(), "Record `" + getName() +
1978  "' does not have a field named `" + FieldName + "'!\n");
1979  return R->getValue();
1980 }
1981 
1983  const RecordVal *R = getValue(FieldName);
1984  if (!R || !R->getValue())
1985  PrintFatalError(getLoc(), "Record `" + getName() +
1986  "' does not have a field named `" + FieldName + "'!\n");
1987 
1988  if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1989  return SI->getValue();
1990  if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1991  return CI->getValue();
1992 
1993  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1994  FieldName + "' does not have a string initializer!");
1995 }
1996 
1998  const RecordVal *R = getValue(FieldName);
1999  if (!R || !R->getValue())
2000  PrintFatalError(getLoc(), "Record `" + getName() +
2001  "' does not have a field named `" + FieldName + "'!\n");
2002 
2003  if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
2004  return BI;
2005  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2006  FieldName + "' does not have a BitsInit initializer!");
2007 }
2008 
2010  const RecordVal *R = getValue(FieldName);
2011  if (!R || !R->getValue())
2012  PrintFatalError(getLoc(), "Record `" + getName() +
2013  "' does not have a field named `" + FieldName + "'!\n");
2014 
2015  if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
2016  return LI;
2017  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2018  FieldName + "' does not have a list initializer!");
2019 }
2020 
2021 std::vector<Record*>
2023  ListInit *List = getValueAsListInit(FieldName);
2024  std::vector<Record*> Defs;
2025  for (Init *I : List->getValues()) {
2026  if (DefInit *DI = dyn_cast<DefInit>(I))
2027  Defs.push_back(DI->getDef());
2028  else
2029  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2030  FieldName + "' list is not entirely DefInit!");
2031  }
2032  return Defs;
2033 }
2034 
2035 int64_t Record::getValueAsInt(StringRef FieldName) const {
2036  const RecordVal *R = getValue(FieldName);
2037  if (!R || !R->getValue())
2038  PrintFatalError(getLoc(), "Record `" + getName() +
2039  "' does not have a field named `" + FieldName + "'!\n");
2040 
2041  if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
2042  return II->getValue();
2043  PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2044  FieldName +
2045  "' does not have an int initializer: " +
2046  R->getValue()->getAsString());
2047 }
2048 
2049 std::vector<int64_t>
2051  ListInit *List = getValueAsListInit(FieldName);
2052  std::vector<int64_t> Ints;
2053  for (Init *I : List->getValues()) {
2054  if (IntInit *II = dyn_cast<IntInit>(I))
2055  Ints.push_back(II->getValue());
2056  else
2057  PrintFatalError(getLoc(),
2058  Twine("Record `") + getName() + "', field `" + FieldName +
2059  "' does not have a list of ints initializer: " +
2060  I->getAsString());
2061  }
2062  return Ints;
2063 }
2064 
2065 std::vector<StringRef>
2067  ListInit *List = getValueAsListInit(FieldName);
2068  std::vector<StringRef> Strings;
2069  for (Init *I : List->getValues()) {
2070  if (StringInit *SI = dyn_cast<StringInit>(I))
2071  Strings.push_back(SI->getValue());
2072  else
2073  PrintFatalError(getLoc(),
2074  Twine("Record `") + getName() + "', field `" + FieldName +
2075  "' does not have a list of strings initializer: " +
2076  I->getAsString());
2077  }
2078  return Strings;
2079 }
2080 
2082  const RecordVal *R = getValue(FieldName);
2083  if (!R || !R->getValue())
2084  PrintFatalError(getLoc(), "Record `" + getName() +
2085  "' does not have a field named `" + FieldName + "'!\n");
2086 
2087  if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2088  return DI->getDef();
2089  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2090  FieldName + "' does not have a def initializer!");
2091 }
2092 
2093 bool Record::getValueAsBit(StringRef FieldName) const {
2094  const RecordVal *R = getValue(FieldName);
2095  if (!R || !R->getValue())
2096  PrintFatalError(getLoc(), "Record `" + getName() +
2097  "' does not have a field named `" + FieldName + "'!\n");
2098 
2099  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2100  return BI->getValue();
2101  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2102  FieldName + "' does not have a bit initializer!");
2103 }
2104 
2105 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
2106  const RecordVal *R = getValue(FieldName);
2107  if (!R || !R->getValue())
2108  PrintFatalError(getLoc(), "Record `" + getName() +
2109  "' does not have a field named `" + FieldName.str() + "'!\n");
2110 
2111  if (isa<UnsetInit>(R->getValue())) {
2112  Unset = true;
2113  return false;
2114  }
2115  Unset = false;
2116  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2117  return BI->getValue();
2118  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2119  FieldName + "' does not have a bit initializer!");
2120 }
2121 
2123  const RecordVal *R = getValue(FieldName);
2124  if (!R || !R->getValue())
2125  PrintFatalError(getLoc(), "Record `" + getName() +
2126  "' does not have a field named `" + FieldName + "'!\n");
2127 
2128  if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
2129  return DI;
2130  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2131  FieldName + "' does not have a dag initializer!");
2132 }
2133 
2134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2135 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
2136 #endif
2137 
2139  OS << "------------- Classes -----------------\n";
2140  for (const auto &C : RK.getClasses())
2141  OS << "class " << *C.second;
2142 
2143  OS << "------------- Defs -----------------\n";
2144  for (const auto &D : RK.getDefs())
2145  OS << "def " << *D.second;
2146  return OS;
2147 }
2148 
2149 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2150 /// an identifier.
2152  return StringInit::get("anonymous_" + utostr(AnonCounter++));
2153 }
2154 
2155 std::vector<Record *>
2157  Record *Class = getClass(ClassName);
2158  if (!Class)
2159  PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
2160 
2161  std::vector<Record*> Defs;
2162  for (const auto &D : getDefs())
2163  if (D.second->isSubClassOf(Class))
2164  Defs.push_back(D.second.get());
2165 
2166  return Defs;
2167 }
2168 
2170  auto It = Map.find(VarName);
2171  if (It == Map.end())
2172  return nullptr;
2173 
2174  Init *I = It->second.V;
2175 
2176  if (!It->second.Resolved && Map.size() > 1) {
2177  // Resolve mutual references among the mapped variables, but prevent
2178  // infinite recursion.
2179  Map.erase(It);
2180  I = I->resolveReferences(*this);
2181  Map[VarName] = {I, true};
2182  }
2183 
2184  return I;
2185 }
2186 
2188  Init *Val = Cache.lookup(VarName);
2189  if (Val)
2190  return Val;
2191 
2192  for (Init *S : Stack) {
2193  if (S == VarName)
2194  return nullptr; // prevent infinite recursion
2195  }
2196 
2197  if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
2198  if (!isa<UnsetInit>(RV->getValue())) {
2199  Val = RV->getValue();
2200  Stack.push_back(VarName);
2201  Val = Val->resolveReferences(*this);
2202  Stack.pop_back();
2203  }
2204  }
2205 
2206  Cache[VarName] = Val;
2207  return Val;
2208 }
2209 
2211  Init *I = nullptr;
2212 
2213  if (R) {
2214  I = R->resolve(VarName);
2215  if (I && !FoundUnresolved) {
2216  // Do not recurse into the resolved initializer, as that would change
2217  // the behavior of the resolver we're delegating, but do check to see
2218  // if there are unresolved variables remaining.
2220  I->resolveReferences(Sub);
2221  FoundUnresolved |= Sub.FoundUnresolved;
2222  }
2223  }
2224 
2225  if (!I)
2226  FoundUnresolved = true;
2227  return I;
2228 }
2229 
2231 {
2232  if (VarName == VarNameToTrack)
2233  Found = true;
2234  return nullptr;
2235 }
Init * Fold() const
Definition: Record.cpp:1308
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:811
uint64_t CallInst * C
Represents a range in source code.
Definition: SMLoc.h:49
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:52
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:358
StringRef getName() const
Definition: Record.cpp:1805
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:781
RecTyKind
Subclass discriminator (for dyn_cast<> et al.)
Definition: Record.h:58
void print(raw_ostream &OS) const
Definition: Record.h:80
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1335
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
StringRef getName() const
Definition: Record.cpp:1434
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:2081
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
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:1997
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:158
Init * getValue() const
Definition: Record.h:1324
This class represents lattice values for constants.
Definition: AllocatorList.h:24
X.Y - Represent a reference to a subfield of a variable.
Definition: Record.h:1178
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:498
virtual std::string getAsUnquotedString() const
Convert this value to a string form, without adding quote markers.
Definition: Record.h:367
Record * getCurrentRecord() const
Definition: Record.h:1782
bool foundUnresolved() const
Definition: Record.h:1883
[AL, AH, CL] - Represent a list of defs
Definition: Record.h:659
iterator begin() const
Definition: ArrayRef.h:137
&#39;7&#39; - Represent an initialization by a literal integer value.
Definition: Record.h:564
static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B, Init *Start, Init *List, Init *Expr, RecTy *Type)
Definition: Record.cpp:1212
&#39;list<Ty>&#39; - Represent a list of values, all of which must be of the specified type.
Definition: Record.h:196
void getDirectSuperClasses(SmallVectorImpl< Record *> &Classes) const
Append the direct super classes of this record to Classes.
Definition: Record.cpp:1883
!op (X, Y) - Combine two inits.
Definition: Record.h:799
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1504
bool typeIsA(const RecTy *RHS) const override
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:98
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:578
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:2122
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1647
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:311
AL - Represent a reference to a &#39;def&#39; in the description.
Definition: Record.h:1092
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:1445
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2187
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1272
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.h:445
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
Definition: Record.cpp:679
void addDef(std::unique_ptr< Record > R)
Definition: Record.h:1635
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:1367
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1524
Init * getCastTo(RecTy *Ty) const override
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
Definition: Record.cpp:1383
Init * Fold(Record *CurRec, bool IsFinal=false) const
Definition: Record.cpp:698
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1463
static Init * getStrConcat(Init *lhs, Init *rhs)
Definition: Record.cpp:838
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:1186
static VarInit * get(StringRef VN, RecTy *T)
Definition: Record.cpp:1417
static uint32_t Concat[]
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:2009
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:2035
Resolve all references to a specific RecordVal.
Definition: Record.h:1839
const_record_iterator classes_begin() const
Definition: Record.h:269
static BumpPtrAllocator Allocator
Definition: Record.cpp:40
void reserve(size_type N)
Definition: SmallVector.h:376
virtual Init * getCastTo(RecTy *Ty) const =0
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
Resolve all variables from a record except for unset variables.
Definition: Record.h:1822
bool typeIsA(const RecTy *RHS) const override
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:231
&#39;{ a, b, c }&#39; - Represents an initializer for a BitsRecTy value.
Definition: Record.h:513
Record * getElementAsRecord(unsigned i) const
Definition: Record.cpp:623
std::string getAsString() const override
Definition: Record.cpp:123
std::string getNameInitAsString() const
Definition: Record.h:1318
Init * Fold() const
Definition: Record.cpp:1634
void dump() const
Debugging method that may be called through a debugger, just invokes print on stderr.
Definition: Record.cpp:288
static BitRecTy * get()
Definition: Record.h:111
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
static IntInit * get(int64_t V)
Definition: Record.cpp:453
static BitsInit * get(ArrayRef< Init *> Range)
Definition: Record.cpp:335
(Optionally) delegate resolving to a sub-resolver, and keep track whether there were unresolved refer...
Definition: Record.h:1875
bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const
This method looks up the specified field and returns its value as a bit.
Definition: Record.cpp:2105
Init * getArg(unsigned Num) const
Definition: Record.h:1253
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
Definition: Record.h:1419
&#39;?&#39; - Represents an uninitialized value
Definition: Record.h:457
&#39;true&#39;/&#39;false&#39; - Represent a concrete initializer for a bit.
Definition: Record.h:483
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:385
void dump() const
Definition: Record.cpp:1832
amdgpu Simplify well known AMD library false Value Value const Twine & Name
static Init * ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec)
Definition: Record.cpp:1025
static BitsRecTy * get(unsigned Sz)
Definition: Record.cpp:77
Record * getDef() const
Definition: Record.h:1111
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This is the common super-class of types that have a specific, explicit, type.
Definition: Record.h:425
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
static RecordRecTy * resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2)
Definition: Record.cpp:235
Shift and rotation operations.
Definition: ISDOpcodes.h:410
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:91
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:354
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:666
RecTy * getElementType() const
Definition: Record.h:209
static StringRecTy * get()
Definition: Record.h:187
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:827
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:197
ArrayRef< Init * > getTemplateArgs() const
Definition: Record.h:1413
std::string getAsString() const override
Definition: Record.cpp:195
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
std::string getAsString() const override
Definition: Record.cpp:114
List[4] - Represent access to one element of a var or field.
Definition: Record.h:1060
&#39;[classname]&#39; - Type of record values that have zero or more superclasses.
Definition: Record.h:238
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:528
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:461
std::vector< Record * > getValueAsListOfDefs(StringRef FieldName) const
This method looks up the specified field and returns its value as a vector of records, throwing an exception if the field does not exist or if the value is not the right type.
Definition: Record.cpp:2022
void AddInteger(signed I)
Definition: FoldingSet.cpp:61
!foldl (a, b, expr, start, lst) - Fold over a list.
Definition: Record.h:916
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:1974
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
void print(raw_ostream &OS, bool PrintSem=true) const
Definition: Record.cpp:1835
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:790
const_iterator end() const
Definition: Record.h:713
virtual Init * resolve(Init *VarName)=0
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
StringInit * getArgName(unsigned Num) const
Definition: Record.h:1258
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:452
Key
PAL metadata keys.
ArrayRef< SMLoc > getLoc() const
Definition: Record.h:1402
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
&#39;bits<n>&#39; - Represent a fixed number of bits
Definition: Record.h:119
ArrayRef< Record * > getClasses() const
Definition: Record.h:263
std::string itostr(int64_t X)
Definition: StringExtras.h:239
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:191
bool isTemplateArg(Init *Name) const
Definition: Record.h:1426
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:1168
RecTy * getType() const
Definition: Record.h:1323
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:201
bool isSubClassOf(const Record *R) const
Definition: Record.h:1473
ArrayRef< Init * > getValues() const
Definition: Record.h:708
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:655
bool isFinal() const
Definition: Record.h:1796
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
unsigned getNumBits() const
Definition: Record.h:131
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:1491
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:118
#define EQ(a, b)
Definition: regexec.c:112
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2169
virtual Init * getBit(unsigned Bit) const =0
This method is used to return the initializer for the specified bit.
ArrayRef< RecordVal > getValues() const
Definition: Record.h:1417
const_record_iterator classes_end() const
Definition: Record.h:270
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:612
&#39;code&#39; - Represent a code fragment
Definition: Record.h:141
void resolveReferencesTo(const RecordVal *RV)
If anything in this record refers to RV, replace the reference to RV with the RHS of RV...
Definition: Record.cpp:1929
bool isSubClassOf(Record *Class) const
Definition: Record.cpp:211
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:537
virtual Init * convertInitializerTo(RecTy *Ty) const =0
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
void addShadow(Init *Key)
Definition: Record.h:1864
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:584
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:104
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:306
Base class for operators.
Definition: Record.h:725
#define P(N)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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:1467
RecTy * getType() const
Definition: Record.h:441
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:218
Init * getElement(unsigned i) const
Definition: Record.h:684
Resolve arbitrary mappings.
Definition: Record.h:1802
void dump() const
Definition: Record.cpp:1935
Init * getNameInit() const
Definition: Record.h:1316
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
virtual bool isConcrete() const
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.h:356
void resolveReferences()
If there are any field references that refer to fields that have been filled in, we can propagate the...
Definition: Record.cpp:1923
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void set(Init *Key, Init *Value)
Definition: Record.h:1816
"foo" - Represent an initialization by a string value.
Definition: Record.h:594
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1345
static void ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:996
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:215
void dump() const
Definition: Record.cpp:2135
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1774
static void ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type)
Definition: Record.cpp:673
static ManagedStatic< OptionRegistry > OR
Definition: Options.cpp:31
const T * getTrailingObjects() const
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:109
!op (X) - Transform an init.
Definition: Record.h:750
Init * getCastTo(RecTy *Ty) const override
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
Definition: Record.cpp:296
const std::string getNameInitAsString() const
Definition: Record.h:1396
static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
Definition: Record.cpp:1697
bool getPrefix() const
Definition: Record.h:1322
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
Definition: Record.cpp:1714
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:1675
void dump() const
Definition: Record.cpp:53
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:1193
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:1982
int64_t getValue() const
Definition: Record.h:580
&#39;string&#39; - Represent an string value
Definition: Record.h:177
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:1613
static BitInit * get(bool V)
Definition: Record.cpp:304
static bool canFitInBitfield(int64_t Value, unsigned NumBits)
Definition: Record.cpp:465
bool setValue(Init *V)
Definition: Record.cpp:1809
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1530
static CodeInit * get(StringRef)
Definition: Record.cpp:510
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:418
static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:803
DefInit * getDefInit()
get the corresponding DefInit.
Definition: Record.cpp:1861
static wasm::ValType getType(const TargetRegisterClass *RC)
static Init * ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, Record *CurRec)
Definition: Record.cpp:1060
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:474
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1780
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::vector< int64_t > getValueAsListOfInts(StringRef FieldName) const
This method looks up the specified field and returns its value as a vector of integers, throwing an exception if the field does not exist or if the value is not the right type.
Definition: Record.cpp:2050
static RecordRecTy * get(ArrayRef< Record *> Classes)
Get the record type with the given non-redundant list of superclasses.
Definition: Record.cpp:150
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1116
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:1356
std::vector< Record * > getAllDerivedDefinitions(StringRef ClassName) const
This method returns all concrete definitions that derive from the specified class name...
Definition: Record.cpp:2156
Delegate resolving to a sub-resolver, but shadow some variable names.
Definition: Record.h:1854
void setFinal(bool Final)
Definition: Record.h:1798
std::vector< StringRef > getValueAsListOfStrings(StringRef FieldName) const
This method looks up the specified field and returns its value as a vector of strings, throwing an exception if the field does not exist or if the value is not the right type.
Definition: Record.cpp:2066
static FieldInit * get(Init *R, StringInit *FN)
Definition: Record.cpp:1658
virtual Init * resolveReferences(Resolver &R) const
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.h:410
Init * Fold(Record *CurRec) const
Definition: Record.cpp:846
std::string getAsString() const override
Definition: Record.cpp:87
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:1401
static StringInit * ConcatStringInits(const StringInit *I0, const StringInit *I1)
Definition: Record.cpp:831
bool typeIsA(const RecTy *RHS) const override
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:133
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1167
virtual bool typeIsA(const RecTy *RHS) const
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:67
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2210
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1432
const RecordMap & getClasses() const
Definition: Record.h:1608
Init * getOperator() const
Definition: Record.h:1243
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
static VarBitInit * get(TypedInit *T, unsigned B)
Definition: Record.cpp:1451
static StringInit * get(StringRef)
Definition: Record.cpp:519
RecordKeeper & getRecords() const
Definition: Record.h:1517
iterator end() const
Definition: ArrayRef.h:138
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:404
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:1257
CHAIN = SC CHAIN, Imm128 - System call.
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1243
unsigned getNumArgs() const
Definition: Record.h:1251
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:1517
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
RecTy * getElementType() const
Definition: Record.h:688
static DefInit * get(Record *)
Definition: Record.cpp:1513
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:194
classname<targs...> - Represent an uninstantiated anonymous class instantiation.
Definition: Record.h:1127
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:224
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:471
static VarDefInit * get(Record *Class, ArrayRef< Init *> Args)
Definition: Record.cpp:1544
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:366
static IsAOpInit * get(RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1289
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
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:963
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2230
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:631
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
Definition: Error.cpp:67
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1563
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:694
!isa<type>(expr) - Dynamically determine the type of an expression.
Definition: Record.h:953
RecordRecTy * getType()
Definition: Record.cpp:1855
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:478
static FoldOpInit * get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
Definition: Record.cpp:1223
static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1283
&#39;int&#39; - Represent an integer value of no particular size
Definition: Record.h:159
amdgpu Simplify well known AMD library false Value Value * Arg
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1439
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:2093
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:973
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1749
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:127
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:387
Init * getNameInit() const
Definition: Record.h:1392
void emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:652
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:396
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:647
static Init * ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS, Record *CurRec)
Definition: Record.cpp:1031
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
static UnsetInit * get()
Definition: Record.cpp:291
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1079
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:69
void addValue(const RecordVal &RV)
Definition: Record.h:1455
const NodeList & List
Definition: RDFGraph.cpp:210
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:1005
const_iterator begin() const
Definition: Record.h:712
static VarListElementInit * get(TypedInit *T, unsigned E)
Definition: Record.cpp:1475
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition: Record.cpp:255
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:1328
static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class, ArrayRef< Init *> Args)
Definition: Record.cpp:1534
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1239
Opcode{0} - Represent access to one bit of a variable or field.
Definition: Record.h:1023
&#39;Opcode&#39; - Represent a reference to an entire variable object.
Definition: Record.h:986
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1304
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1198
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:1753
std::string getAsString() const override
Definition: Record.cpp:139
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
uint32_t Size
Definition: Profile.cpp:47
&#39;bit&#39; - Represent a single bit
Definition: Record.h:101
!op (X, Y, Z) - Combine two inits.
Definition: Record.h:854
static void ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef< Init *> Range)
Definition: Record.cpp:328
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2039
static ListInit * get(ArrayRef< Init *> Range, RecTy *EltTy)
Definition: Record.cpp:556
virtual bool keepUnsetBits() const
Definition: Record.h:1791
RecTyKind getRecTyKind() const
Definition: Record.h:77
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:62
StringRef getValue() const
Definition: Record.h:610
&#39;dag&#39; - Represent a dag fragment
Definition: Record.h:219
Record * getDef(StringRef Name) const
Definition: Record.h:1616
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, const TargetLowering *TLI, const DataLayout &DL)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1669
LLVM Value Representation.
Definition: Value.h:73
(v a, b) - Represent a DAG tree value.
Definition: Record.h:1213
virtual std::string getAsString() const =0
Convert this value to a string form.
size_t size() const
Definition: Record.h:715
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:1770
static void ProfileListInit(FoldingSetNodeID &ID, ArrayRef< Init *> Range, RecTy *EltTy)
Definition: Record.cpp:546
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
void setName(Init *Name)
Definition: Record.cpp:1867
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1021
static void ProfileRecordRecTy(FoldingSetNodeID &ID, ArrayRef< Record *> Classes)
Definition: Record.cpp:143
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1339
virtual std::string getAsString() const =0
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:300
ListRecTy * getListTy()
Returns the type representing list<this>.
Definition: Record.cpp:56
Init * getNewAnonymousName()
GetNewAnonymousName - Generate a unique anonymous name that can be used as an identifier.
Definition: Record.cpp:2151
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
const RecordMap & getDefs() const
Definition: Record.h:1609
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1276
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1682
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
#define T1
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1487
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
static IntRecTy * get()
Definition: Record.h:169
RecordVal(Init *N, RecTy *T, bool P)
Definition: Record.cpp:1799