LLVM  4.0.0
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/TableGen/Record.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/Compiler.h"
24 #include "llvm/TableGen/Error.h"
25 #include <cassert>
26 #include <cstdint>
27 #include <new>
28 
29 using namespace llvm;
30 
32 
33 //===----------------------------------------------------------------------===//
34 // Type implementations
35 //===----------------------------------------------------------------------===//
36 
37 BitRecTy BitRecTy::Shared;
38 CodeRecTy CodeRecTy::Shared;
39 IntRecTy IntRecTy::Shared;
40 StringRecTy StringRecTy::Shared;
41 DagRecTy DagRecTy::Shared;
42 
43 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
44 
46  if (!ListTy)
47  ListTy = new(Allocator) ListRecTy(this);
48  return ListTy;
49 }
50 
51 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
52  assert(RHS && "NULL pointer");
53  return Kind == RHS->getRecTyKind();
54 }
55 
56 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
58  return true;
59  if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
60  return BitsTy->getNumBits() == 1;
61  return false;
62 }
63 
64 BitsRecTy *BitsRecTy::get(unsigned Sz) {
65  static std::vector<BitsRecTy*> Shared;
66  if (Sz >= Shared.size())
67  Shared.resize(Sz + 1);
68  BitsRecTy *&Ty = Shared[Sz];
69  if (!Ty)
70  Ty = new(Allocator) BitsRecTy(Sz);
71  return Ty;
72 }
73 
74 std::string BitsRecTy::getAsString() const {
75  return "bits<" + utostr(Size) + ">";
76 }
77 
78 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
79  if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
80  return cast<BitsRecTy>(RHS)->Size == Size;
81  RecTyKind kind = RHS->getRecTyKind();
82  return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
83 }
84 
85 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
86  RecTyKind kind = RHS->getRecTyKind();
87  return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
88 }
89 
90 std::string StringRecTy::getAsString() const {
91  return "string";
92 }
93 
94 std::string ListRecTy::getAsString() const {
95  return "list<" + Ty->getAsString() + ">";
96 }
97 
98 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
99  if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
100  return Ty->typeIsConvertibleTo(ListTy->getElementType());
101  return false;
102 }
103 
104 std::string DagRecTy::getAsString() const {
105  return "dag";
106 }
107 
109  return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
110 }
111 
112 std::string RecordRecTy::getAsString() const {
113  return Rec->getName();
114 }
115 
116 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
117  const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
118  if (!RTy)
119  return false;
120 
121  if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
122  return true;
123 
124  for (const auto &SCPair : RTy->getRecord()->getSuperClasses())
125  if (Rec->isSubClassOf(SCPair.first))
126  return true;
127 
128  return false;
129 }
130 
132  if (T1->typeIsConvertibleTo(T2))
133  return T2;
134  if (T2->typeIsConvertibleTo(T1))
135  return T1;
136 
137  // If one is a Record type, check superclasses
138  if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
139  // See if T2 inherits from a type T1 also inherits from
140  for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) {
141  RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first);
142  RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
143  if (NewType1)
144  return NewType1;
145  }
146  }
147  if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
148  // See if T1 inherits from a type T2 also inherits from
149  for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) {
150  RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first);
151  RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
152  if (NewType2)
153  return NewType2;
154  }
155  }
156  return nullptr;
157 }
158 
159 //===----------------------------------------------------------------------===//
160 // Initializer implementations
161 //===----------------------------------------------------------------------===//
162 
163 void Init::anchor() { }
164 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
165 
167  static UnsetInit TheInit;
168  return &TheInit;
169 }
170 
172  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
173  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
174 
175  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
176  NewBits[i] = UnsetInit::get();
177 
178  return BitsInit::get(NewBits);
179  }
180 
181  // All other types can just be returned.
182  return const_cast<UnsetInit *>(this);
183 }
184 
186  static BitInit True(true);
187  static BitInit False(false);
188 
189  return V ? &True : &False;
190 }
191 
193  if (isa<BitRecTy>(Ty))
194  return const_cast<BitInit *>(this);
195 
196  if (isa<IntRecTy>(Ty))
197  return IntInit::get(getValue());
198 
199  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
200  // Can only convert single bit.
201  if (BRT->getNumBits() == 1)
202  return BitsInit::get(const_cast<BitInit *>(this));
203  }
204 
205  return nullptr;
206 }
207 
208 static void
210  ID.AddInteger(Range.size());
211 
212  for (Init *I : Range)
213  ID.AddPointer(I);
214 }
215 
217  static FoldingSet<BitsInit> ThePool;
218  static std::vector<BitsInit*> TheActualPool;
219 
221  ProfileBitsInit(ID, Range);
222 
223  void *IP = nullptr;
224  if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
225  return I;
226 
227  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
228  alignof(BitsInit));
229  BitsInit *I = new(Mem) BitsInit(Range.size());
230  std::uninitialized_copy(Range.begin(), Range.end(),
231  I->getTrailingObjects<Init *>());
232  ThePool.InsertNode(I, IP);
233  TheActualPool.push_back(I);
234  return I;
235 }
236 
238  ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
239 }
240 
242  if (isa<BitRecTy>(Ty)) {
243  if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
244  return getBit(0);
245  }
246 
247  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
248  // If the number of bits is right, return it. Otherwise we need to expand
249  // or truncate.
250  if (getNumBits() != BRT->getNumBits()) return nullptr;
251  return const_cast<BitsInit *>(this);
252  }
253 
254  if (isa<IntRecTy>(Ty)) {
255  int64_t Result = 0;
256  for (unsigned i = 0, e = getNumBits(); i != e; ++i)
257  if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
258  Result |= static_cast<int64_t>(Bit->getValue()) << i;
259  else
260  return nullptr;
261  return IntInit::get(Result);
262  }
263 
264  return nullptr;
265 }
266 
267 Init *
269  SmallVector<Init *, 16> NewBits(Bits.size());
270 
271  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
272  if (Bits[i] >= getNumBits())
273  return nullptr;
274  NewBits[i] = getBit(Bits[i]);
275  }
276  return BitsInit::get(NewBits);
277 }
278 
279 std::string BitsInit::getAsString() const {
280  std::string Result = "{ ";
281  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
282  if (i) Result += ", ";
283  if (Init *Bit = getBit(e-i-1))
284  Result += Bit->getAsString();
285  else
286  Result += "*";
287  }
288  return Result + " }";
289 }
290 
291 // Fix bit initializer to preserve the behavior that bit reference from a unset
292 // bits initializer will resolve into VarBitInit to keep the field name and bit
293 // number used in targets with fixed insn length.
294 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
295  if (RV || !isa<UnsetInit>(After))
296  return After;
297  return Before;
298 }
299 
300 // resolveReferences - If there are any field references that refer to fields
301 // that have been filled in, we can propagate the values now.
302 //
304  bool Changed = false;
306 
307  Init *CachedInit = nullptr;
308  Init *CachedBitVar = nullptr;
309  bool CachedBitVarChanged = false;
310 
311  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
312  Init *CurBit = getBit(i);
313  Init *CurBitVar = CurBit->getBitVar();
314 
315  NewBits[i] = CurBit;
316 
317  if (CurBitVar == CachedBitVar) {
318  if (CachedBitVarChanged) {
319  Init *Bit = CachedInit->getBit(CurBit->getBitNum());
320  NewBits[i] = fixBitInit(RV, CurBit, Bit);
321  }
322  continue;
323  }
324  CachedBitVar = CurBitVar;
325  CachedBitVarChanged = false;
326 
327  Init *B;
328  do {
329  B = CurBitVar;
330  CurBitVar = CurBitVar->resolveReferences(R, RV);
331  CachedBitVarChanged |= B != CurBitVar;
332  Changed |= B != CurBitVar;
333  } while (B != CurBitVar);
334  CachedInit = CurBitVar;
335 
336  if (CachedBitVarChanged) {
337  Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
338  NewBits[i] = fixBitInit(RV, CurBit, Bit);
339  }
340  }
341 
342  if (Changed)
343  return BitsInit::get(NewBits);
344 
345  return const_cast<BitsInit *>(this);
346 }
347 
348 IntInit *IntInit::get(int64_t V) {
349  static DenseMap<int64_t, IntInit*> ThePool;
350 
351  IntInit *&I = ThePool[V];
352  if (!I) I = new(Allocator) IntInit(V);
353  return I;
354 }
355 
356 std::string IntInit::getAsString() const {
357  return itostr(Value);
358 }
359 
360 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
361  // For example, with NumBits == 4, we permit Values from [-7 .. 15].
362  return (NumBits >= sizeof(Value) * 8) ||
363  (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
364 }
365 
367  if (isa<IntRecTy>(Ty))
368  return const_cast<IntInit *>(this);
369 
370  if (isa<BitRecTy>(Ty)) {
371  int64_t Val = getValue();
372  if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
373  return BitInit::get(Val != 0);
374  }
375 
376  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
377  int64_t Value = getValue();
378  // Make sure this bitfield is large enough to hold the integer value.
379  if (!canFitInBitfield(Value, BRT->getNumBits()))
380  return nullptr;
381 
382  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
383  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
384  NewBits[i] = BitInit::get(Value & (1LL << i));
385 
386  return BitsInit::get(NewBits);
387  }
388 
389  return nullptr;
390 }
391 
392 Init *
394  SmallVector<Init *, 16> NewBits(Bits.size());
395 
396  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
397  if (Bits[i] >= 64)
398  return nullptr;
399 
400  NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
401  }
402  return BitsInit::get(NewBits);
403 }
404 
406  static DenseMap<StringRef, CodeInit*> ThePool;
407 
408  auto I = ThePool.insert(std::make_pair(V, nullptr));
409  if (I.second) {
410  StringRef VCopy = V.copy(Allocator);
411  I.first->first = VCopy;
412  I.first->second = new(Allocator) CodeInit(VCopy);
413  }
414  return I.first->second;
415 }
416 
418  static DenseMap<StringRef, StringInit*> ThePool;
419 
420  auto I = ThePool.insert(std::make_pair(V, nullptr));
421  if (I.second) {
422  StringRef VCopy = V.copy(Allocator);
423  I.first->first = VCopy;
424  I.first->second = new(Allocator) StringInit(VCopy);
425  }
426  return I.first->second;
427 }
428 
430  if (isa<StringRecTy>(Ty))
431  return const_cast<StringInit *>(this);
432 
433  return nullptr;
434 }
435 
437  if (isa<CodeRecTy>(Ty))
438  return const_cast<CodeInit *>(this);
439 
440  return nullptr;
441 }
442 
444  ArrayRef<Init *> Range,
445  RecTy *EltTy) {
446  ID.AddInteger(Range.size());
447  ID.AddPointer(EltTy);
448 
449  for (Init *I : Range)
450  ID.AddPointer(I);
451 }
452 
454  static FoldingSet<ListInit> ThePool;
455  static std::vector<ListInit*> TheActualPool;
456 
458  ProfileListInit(ID, Range, EltTy);
459 
460  void *IP = nullptr;
461  if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
462  return I;
463 
464  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
465  alignof(ListInit));
466  ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
467  std::uninitialized_copy(Range.begin(), Range.end(),
468  I->getTrailingObjects<Init *>());
469  ThePool.InsertNode(I, IP);
470  TheActualPool.push_back(I);
471  return I;
472 }
473 
475  RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
476 
477  ProfileListInit(ID, getValues(), EltTy);
478 }
479 
481  if (getType() == Ty)
482  return const_cast<ListInit*>(this);
483 
484  if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
485  SmallVector<Init*, 8> Elements;
486  Elements.reserve(getValues().size());
487 
488  // Verify that all of the elements of the list are subclasses of the
489  // appropriate class!
490  bool Changed = false;
491  RecTy *ElementType = LRT->getElementType();
492  for (Init *I : getValues())
493  if (Init *CI = I->convertInitializerTo(ElementType)) {
494  Elements.push_back(CI);
495  if (CI != I)
496  Changed = true;
497  } else
498  return nullptr;
499 
500  if (!Changed)
501  return const_cast<ListInit*>(this);
502  return ListInit::get(Elements, Ty);
503  }
504 
505  return nullptr;
506 }
507 
510  Vals.reserve(Elements.size());
511  for (unsigned Element : Elements) {
512  if (Element >= size())
513  return nullptr;
514  Vals.push_back(getElement(Element));
515  }
516  return ListInit::get(Vals, getType());
517 }
518 
520  assert(i < NumValues && "List element index out of range!");
521  DefInit *DI = dyn_cast<DefInit>(getElement(i));
522  if (!DI)
523  PrintFatalError("Expected record in list!");
524  return DI->getDef();
525 }
526 
528  SmallVector<Init*, 8> Resolved;
529  Resolved.reserve(size());
530  bool Changed = false;
531 
532  for (Init *CurElt : getValues()) {
533  Init *E;
534 
535  do {
536  E = CurElt;
537  CurElt = CurElt->resolveReferences(R, RV);
538  Changed |= E != CurElt;
539  } while (E != CurElt);
540  Resolved.push_back(E);
541  }
542 
543  if (Changed)
544  return ListInit::get(Resolved, getType());
545  return const_cast<ListInit *>(this);
546 }
547 
549  unsigned Elt) const {
550  if (Elt >= size())
551  return nullptr; // Out of range reference.
552  Init *E = getElement(Elt);
553  // If the element is set to some value, or if we are resolving a reference
554  // to a specific variable and that variable is explicitly unset, then
555  // replace the VarListElementInit with it.
556  if (IRV || !isa<UnsetInit>(E))
557  return E;
558  return nullptr;
559 }
560 
561 std::string ListInit::getAsString() const {
562  std::string Result = "[";
563  const char *sep = "";
564  for (Init *Element : *this) {
565  Result += sep;
566  sep = ", ";
567  Result += Element->getAsString();
568  }
569  return Result + "]";
570 }
571 
573  unsigned Elt) const {
574  Init *Resolved = resolveReferences(R, IRV);
575  OpInit *OResolved = dyn_cast<OpInit>(Resolved);
576  if (OResolved) {
577  Resolved = OResolved->Fold(&R, nullptr);
578  }
579 
580  if (Resolved != this) {
581  TypedInit *Typed = cast<TypedInit>(Resolved);
582  if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
583  return New;
584  return VarListElementInit::get(Typed, Elt);
585  }
586 
587  return nullptr;
588 }
589 
590 Init *OpInit::getBit(unsigned Bit) const {
591  if (getType() == BitRecTy::get())
592  return const_cast<OpInit*>(this);
593  return VarBitInit::get(const_cast<OpInit*>(this), Bit);
594 }
595 
596 static void
598  ID.AddInteger(Opcode);
599  ID.AddPointer(Op);
600  ID.AddPointer(Type);
601 }
602 
604  static FoldingSet<UnOpInit> ThePool;
605  static std::vector<UnOpInit*> TheActualPool;
606 
608  ProfileUnOpInit(ID, Opc, LHS, Type);
609 
610  void *IP = nullptr;
611  if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
612  return I;
613 
614  UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
615  ThePool.InsertNode(I, IP);
616  TheActualPool.push_back(I);
617  return I;
618 }
619 
622 }
623 
624 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
625  switch (getOpcode()) {
626  case CAST: {
627  if (isa<StringRecTy>(getType())) {
628  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
629  return LHSs;
630 
631  if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
632  return StringInit::get(LHSd->getAsString());
633 
634  if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
635  return StringInit::get(LHSi->getAsString());
636  } else {
637  if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
638  // From TGParser::ParseIDValue
639  if (CurRec) {
640  if (const RecordVal *RV = CurRec->getValue(Name)) {
641  if (RV->getType() != getType())
642  PrintFatalError("type mismatch in cast");
643  return VarInit::get(Name, RV->getType());
644  }
645 
646  Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
647  ":");
648 
649  if (CurRec->isTemplateArg(TemplateArgName)) {
650  const RecordVal *RV = CurRec->getValue(TemplateArgName);
651  assert(RV && "Template arg doesn't exist??");
652 
653  if (RV->getType() != getType())
654  PrintFatalError("type mismatch in cast");
655 
656  return VarInit::get(TemplateArgName, RV->getType());
657  }
658  }
659 
660  if (CurMultiClass) {
661  Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
662  "::");
663 
664  if (CurMultiClass->Rec.isTemplateArg(MCName)) {
665  const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
666  assert(RV && "Template arg doesn't exist??");
667 
668  if (RV->getType() != getType())
669  PrintFatalError("type mismatch in cast");
670 
671  return VarInit::get(MCName, RV->getType());
672  }
673  }
674  assert(CurRec && "NULL pointer");
675  if (Record *D = (CurRec->getRecords()).getDef(Name->getValue()))
676  return DefInit::get(D);
677 
678  PrintFatalError(CurRec->getLoc(),
679  "Undefined reference:'" + Name->getValue() + "'\n");
680  }
681 
682  if (isa<IntRecTy>(getType())) {
683  if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
684  if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
685  return NewInit;
686  break;
687  }
688  }
689  }
690  break;
691  }
692  case HEAD: {
693  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
694  assert(!LHSl->empty() && "Empty list in head");
695  return LHSl->getElement(0);
696  }
697  break;
698  }
699  case TAIL: {
700  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
701  assert(!LHSl->empty() && "Empty list in tail");
702  // Note the +1. We can't just pass the result of getValues()
703  // directly.
704  return ListInit::get(LHSl->getValues().slice(1), LHSl->getType());
705  }
706  break;
707  }
708  case EMPTY: {
709  if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
710  return IntInit::get(LHSl->empty());
711  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
712  return IntInit::get(LHSs->getValue().empty());
713 
714  break;
715  }
716  }
717  return const_cast<UnOpInit *>(this);
718 }
719 
721  Init *lhs = LHS->resolveReferences(R, RV);
722 
723  if (LHS != lhs)
724  return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
725  return Fold(&R, nullptr);
726 }
727 
728 std::string UnOpInit::getAsString() const {
729  std::string Result;
730  switch (getOpcode()) {
731  case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
732  case HEAD: Result = "!head"; break;
733  case TAIL: Result = "!tail"; break;
734  case EMPTY: Result = "!empty"; break;
735  }
736  return Result + "(" + LHS->getAsString() + ")";
737 }
738 
739 static void
740 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
741  RecTy *Type) {
742  ID.AddInteger(Opcode);
743  ID.AddPointer(LHS);
744  ID.AddPointer(RHS);
745  ID.AddPointer(Type);
746 }
747 
749  Init *RHS, RecTy *Type) {
750  static FoldingSet<BinOpInit> ThePool;
751  static std::vector<BinOpInit*> TheActualPool;
752 
754  ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
755 
756  void *IP = nullptr;
757  if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
758  return I;
759 
760  BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
761  ThePool.InsertNode(I, IP);
762  TheActualPool.push_back(I);
763  return I;
764 }
765 
768 }
769 
771  const StringInit *I1) {
772  SmallString<80> Concat(I0->getValue());
773  Concat.append(I1->getValue());
774  return StringInit::get(Concat);
775 }
776 
777 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
778  switch (getOpcode()) {
779  case CONCAT: {
780  DagInit *LHSs = dyn_cast<DagInit>(LHS);
781  DagInit *RHSs = dyn_cast<DagInit>(RHS);
782  if (LHSs && RHSs) {
783  DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
784  DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
785  if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
786  PrintFatalError("Concated Dag operators do not match!");
789  for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
790  Args.push_back(LHSs->getArg(i));
791  ArgNames.push_back(LHSs->getArgName(i));
792  }
793  for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
794  Args.push_back(RHSs->getArg(i));
795  ArgNames.push_back(RHSs->getArgName(i));
796  }
797  return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
798  }
799  break;
800  }
801  case LISTCONCAT: {
802  ListInit *LHSs = dyn_cast<ListInit>(LHS);
803  ListInit *RHSs = dyn_cast<ListInit>(RHS);
804  if (LHSs && RHSs) {
806  Args.insert(Args.end(), LHSs->begin(), LHSs->end());
807  Args.insert(Args.end(), RHSs->begin(), RHSs->end());
808  return ListInit::get(
809  Args, cast<ListRecTy>(LHSs->getType())->getElementType());
810  }
811  break;
812  }
813  case STRCONCAT: {
814  StringInit *LHSs = dyn_cast<StringInit>(LHS);
815  StringInit *RHSs = dyn_cast<StringInit>(RHS);
816  if (LHSs && RHSs)
817  return ConcatStringInits(LHSs, RHSs);
818  break;
819  }
820  case EQ: {
821  // try to fold eq comparison for 'bit' and 'int', otherwise fallback
822  // to string objects.
823  IntInit *L =
824  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
825  IntInit *R =
826  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
827 
828  if (L && R)
829  return IntInit::get(L->getValue() == R->getValue());
830 
831  StringInit *LHSs = dyn_cast<StringInit>(LHS);
832  StringInit *RHSs = dyn_cast<StringInit>(RHS);
833 
834  // Make sure we've resolved
835  if (LHSs && RHSs)
836  return IntInit::get(LHSs->getValue() == RHSs->getValue());
837 
838  break;
839  }
840  case ADD:
841  case AND:
842  case OR:
843  case SHL:
844  case SRA:
845  case SRL: {
846  IntInit *LHSi =
847  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
848  IntInit *RHSi =
849  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
850  if (LHSi && RHSi) {
851  int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
852  int64_t Result;
853  switch (getOpcode()) {
854  default: llvm_unreachable("Bad opcode!");
855  case ADD: Result = LHSv + RHSv; break;
856  case AND: Result = LHSv & RHSv; break;
857  case OR: Result = LHSv | RHSv; break;
858  case SHL: Result = LHSv << RHSv; break;
859  case SRA: Result = LHSv >> RHSv; break;
860  case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
861  }
862  return IntInit::get(Result);
863  }
864  break;
865  }
866  }
867  return const_cast<BinOpInit *>(this);
868 }
869 
871  Init *lhs = LHS->resolveReferences(R, RV);
872  Init *rhs = RHS->resolveReferences(R, RV);
873 
874  if (LHS != lhs || RHS != rhs)
875  return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
876  return Fold(&R, nullptr);
877 }
878 
879 std::string BinOpInit::getAsString() const {
880  std::string Result;
881  switch (getOpcode()) {
882  case CONCAT: Result = "!con"; break;
883  case ADD: Result = "!add"; break;
884  case AND: Result = "!and"; break;
885  case OR: Result = "!or"; break;
886  case SHL: Result = "!shl"; break;
887  case SRA: Result = "!sra"; break;
888  case SRL: Result = "!srl"; break;
889  case EQ: Result = "!eq"; break;
890  case LISTCONCAT: Result = "!listconcat"; break;
891  case STRCONCAT: Result = "!strconcat"; break;
892  }
893  return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
894 }
895 
896 static void
897 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
898  Init *RHS, RecTy *Type) {
899  ID.AddInteger(Opcode);
900  ID.AddPointer(LHS);
901  ID.AddPointer(MHS);
902  ID.AddPointer(RHS);
903  ID.AddPointer(Type);
904 }
905 
907  RecTy *Type) {
908  static FoldingSet<TernOpInit> ThePool;
909  static std::vector<TernOpInit*> TheActualPool;
910 
912  ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
913 
914  void *IP = nullptr;
915  if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
916  return I;
917 
918  TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
919  ThePool.InsertNode(I, IP);
920  TheActualPool.push_back(I);
921  return I;
922 }
923 
926 }
927 
928 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
929  Record *CurRec, MultiClass *CurMultiClass);
930 
931 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
932  RecTy *Type, Record *CurRec,
933  MultiClass *CurMultiClass) {
934  // If this is a dag, recurse
935  if (auto *TArg = dyn_cast<TypedInit>(Arg))
936  if (isa<DagRecTy>(TArg->getType()))
937  return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass);
938 
939  SmallVector<Init *, 8> NewOperands;
940  NewOperands.reserve(RHSo->getNumOperands());
941  for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) {
942  if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
943  if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
944  Type, CurRec, CurMultiClass))
945  NewOperands.push_back(Result);
946  else
947  NewOperands.push_back(Arg);
948  } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
949  NewOperands.push_back(Arg);
950  } else {
951  NewOperands.push_back(RHSo->getOperand(i));
952  }
953  }
954 
955  // Now run the operator and use its result as the new leaf
956  const OpInit *NewOp = RHSo->clone(NewOperands);
957  Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
958  return (NewVal != NewOp) ? NewVal : nullptr;
959 }
960 
961 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
962  Record *CurRec, MultiClass *CurMultiClass) {
963 
964  OpInit *RHSo = dyn_cast<OpInit>(RHS);
965 
966  if (!RHSo)
967  PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
968 
969  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
970 
971  if (!LHSt)
972  PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
973 
974  DagInit *MHSd = dyn_cast<DagInit>(MHS);
975  if (MHSd && isa<DagRecTy>(Type)) {
976  Init *Val = MHSd->getOperator();
977  if (Init *Result = EvaluateOperation(RHSo, LHS, Val,
978  Type, CurRec, CurMultiClass))
979  Val = Result;
980 
982  for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
983  Init *Arg = MHSd->getArg(i);
984  StringInit *ArgName = MHSd->getArgName(i);
985 
986  // Process args
987  if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
988  CurRec, CurMultiClass))
989  Arg = Result;
990 
991  // TODO: Process arg names
992  args.push_back(std::make_pair(Arg, ArgName));
993  }
994 
995  return DagInit::get(Val, nullptr, args);
996  }
997 
998  ListInit *MHSl = dyn_cast<ListInit>(MHS);
999  if (MHSl && isa<ListRecTy>(Type)) {
1000  SmallVector<Init *, 8> NewOperands;
1001  SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1002 
1003  for (Init *&Item : NewList) {
1004  NewOperands.clear();
1005  for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
1006  // First, replace the foreach variable with the list item
1007  if (LHS->getAsString() == RHSo->getOperand(i)->getAsString())
1008  NewOperands.push_back(Item);
1009  else
1010  NewOperands.push_back(RHSo->getOperand(i));
1011  }
1012 
1013  // Now run the operator and use its result as the new list item
1014  const OpInit *NewOp = RHSo->clone(NewOperands);
1015  Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1016  if (NewItem != NewOp)
1017  Item = NewItem;
1018  }
1019  return ListInit::get(NewList, MHSl->getType());
1020  }
1021  return nullptr;
1022 }
1023 
1024 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1025  switch (getOpcode()) {
1026  case SUBST: {
1027  DefInit *LHSd = dyn_cast<DefInit>(LHS);
1028  VarInit *LHSv = dyn_cast<VarInit>(LHS);
1029  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1030 
1031  DefInit *MHSd = dyn_cast<DefInit>(MHS);
1032  VarInit *MHSv = dyn_cast<VarInit>(MHS);
1033  StringInit *MHSs = dyn_cast<StringInit>(MHS);
1034 
1035  DefInit *RHSd = dyn_cast<DefInit>(RHS);
1036  VarInit *RHSv = dyn_cast<VarInit>(RHS);
1037  StringInit *RHSs = dyn_cast<StringInit>(RHS);
1038 
1039  if (LHSd && MHSd && RHSd) {
1040  Record *Val = RHSd->getDef();
1041  if (LHSd->getAsString() == RHSd->getAsString())
1042  Val = MHSd->getDef();
1043  return DefInit::get(Val);
1044  }
1045  if (LHSv && MHSv && RHSv) {
1046  std::string Val = RHSv->getName();
1047  if (LHSv->getAsString() == RHSv->getAsString())
1048  Val = MHSv->getName();
1049  return VarInit::get(Val, getType());
1050  }
1051  if (LHSs && MHSs && RHSs) {
1052  std::string Val = RHSs->getValue();
1053 
1054  std::string::size_type found;
1055  std::string::size_type idx = 0;
1056  while (true) {
1057  found = Val.find(LHSs->getValue(), idx);
1058  if (found == std::string::npos)
1059  break;
1060  Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1061  idx = found + MHSs->getValue().size();
1062  }
1063 
1064  return StringInit::get(Val);
1065  }
1066  break;
1067  }
1068 
1069  case FOREACH: {
1070  if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1071  CurRec, CurMultiClass))
1072  return Result;
1073  break;
1074  }
1075 
1076  case IF: {
1077  IntInit *LHSi = dyn_cast<IntInit>(LHS);
1078  if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1079  LHSi = dyn_cast<IntInit>(I);
1080  if (LHSi) {
1081  if (LHSi->getValue())
1082  return MHS;
1083  return RHS;
1084  }
1085  break;
1086  }
1087  }
1088 
1089  return const_cast<TernOpInit *>(this);
1090 }
1091 
1093  const RecordVal *RV) const {
1094  Init *lhs = LHS->resolveReferences(R, RV);
1095 
1096  if (getOpcode() == IF && lhs != LHS) {
1097  IntInit *Value = dyn_cast<IntInit>(lhs);
1098  if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1099  Value = dyn_cast<IntInit>(I);
1100  if (Value) {
1101  // Short-circuit
1102  if (Value->getValue()) {
1103  Init *mhs = MHS->resolveReferences(R, RV);
1104  return (TernOpInit::get(getOpcode(), lhs, mhs,
1105  RHS, getType()))->Fold(&R, nullptr);
1106  }
1107  Init *rhs = RHS->resolveReferences(R, RV);
1108  return (TernOpInit::get(getOpcode(), lhs, MHS,
1109  rhs, getType()))->Fold(&R, nullptr);
1110  }
1111  }
1112 
1113  Init *mhs = MHS->resolveReferences(R, RV);
1114  Init *rhs = RHS->resolveReferences(R, RV);
1115 
1116  if (LHS != lhs || MHS != mhs || RHS != rhs)
1117  return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1118  getType()))->Fold(&R, nullptr);
1119  return Fold(&R, nullptr);
1120 }
1121 
1122 std::string TernOpInit::getAsString() const {
1123  std::string Result;
1124  switch (getOpcode()) {
1125  case SUBST: Result = "!subst"; break;
1126  case FOREACH: Result = "!foreach"; break;
1127  case IF: Result = "!if"; break;
1128  }
1129  return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1130  RHS->getAsString() + ")";
1131 }
1132 
1134  if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1135  if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1136  return Field->getType();
1137  return nullptr;
1138 }
1139 
1140 Init *
1142  if (isa<IntRecTy>(Ty)) {
1143  if (getType()->typeIsConvertibleTo(Ty))
1144  return const_cast<TypedInit *>(this);
1145  return nullptr;
1146  }
1147 
1148  if (isa<StringRecTy>(Ty)) {
1149  if (isa<StringRecTy>(getType()))
1150  return const_cast<TypedInit *>(this);
1151  return nullptr;
1152  }
1153 
1154  if (isa<CodeRecTy>(Ty)) {
1155  if (isa<CodeRecTy>(getType()))
1156  return const_cast<TypedInit *>(this);
1157  return nullptr;
1158  }
1159 
1160  if (isa<BitRecTy>(Ty)) {
1161  // Accept variable if it is already of bit type!
1162  if (isa<BitRecTy>(getType()))
1163  return const_cast<TypedInit *>(this);
1164  if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1165  // Accept only bits<1> expression.
1166  if (BitsTy->getNumBits() == 1)
1167  return const_cast<TypedInit *>(this);
1168  return nullptr;
1169  }
1170  // Ternary !if can be converted to bit, but only if both sides are
1171  // convertible to a bit.
1172  if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1173  if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1174  TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1175  TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1176  return const_cast<TypedInit *>(this);
1177  return nullptr;
1178  }
1179  return nullptr;
1180  }
1181 
1182  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1183  if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1184  return BitsInit::get(const_cast<TypedInit *>(this));
1185 
1186  if (getType()->typeIsConvertibleTo(BRT)) {
1187  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1188 
1189  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1190  NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1191  return BitsInit::get(NewBits);
1192  }
1193 
1194  return nullptr;
1195  }
1196 
1197  if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1198  if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1199  if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1200  return const_cast<TypedInit *>(this);
1201  return nullptr;
1202  }
1203 
1204  if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1205  if (getType()->typeIsConvertibleTo(DRT))
1206  return const_cast<TypedInit *>(this);
1207  return nullptr;
1208  }
1209 
1210  if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1211  // Ensure that this is compatible with Rec.
1212  if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1213  if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1214  DRRT->getRecord() == SRRT->getRecord())
1215  return const_cast<TypedInit *>(this);
1216  return nullptr;
1217  }
1218 
1219  return nullptr;
1220 }
1221 
1224  if (!T) return nullptr; // Cannot subscript a non-bits variable.
1225  unsigned NumBits = T->getNumBits();
1226 
1227  SmallVector<Init *, 16> NewBits;
1228  NewBits.reserve(Bits.size());
1229  for (unsigned Bit : Bits) {
1230  if (Bit >= NumBits)
1231  return nullptr;
1232 
1233  NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1234  }
1235  return BitsInit::get(NewBits);
1236 }
1237 
1240  if (!T) return nullptr; // Cannot subscript a non-list variable.
1241 
1242  if (Elements.size() == 1)
1243  return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1244 
1245  SmallVector<Init*, 8> ListInits;
1246  ListInits.reserve(Elements.size());
1247  for (unsigned Element : Elements)
1248  ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1249  Element));
1250  return ListInit::get(ListInits, T);
1251 }
1252 
1253 
1255  Init *Value = StringInit::get(VN);
1256  return VarInit::get(Value, T);
1257 }
1258 
1260  typedef std::pair<RecTy *, Init *> Key;
1261  static DenseMap<Key, VarInit*> ThePool;
1262 
1263  Key TheKey(std::make_pair(T, VN));
1264 
1265  VarInit *&I = ThePool[TheKey];
1266  if (!I)
1267  I = new(Allocator) VarInit(VN, T);
1268  return I;
1269 }
1270 
1272  StringInit *NameString = cast<StringInit>(getNameInit());
1273  return NameString->getValue();
1274 }
1275 
1276 Init *VarInit::getBit(unsigned Bit) const {
1277  if (getType() == BitRecTy::get())
1278  return const_cast<VarInit*>(this);
1279  return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1280 }
1281 
1283  const RecordVal *IRV,
1284  unsigned Elt) const {
1285  if (R.isTemplateArg(getNameInit())) return nullptr;
1286  if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
1287 
1288  RecordVal *RV = R.getValue(getNameInit());
1289  assert(RV && "Reference to a non-existent variable?");
1290  ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1291  if (!LI)
1292  return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
1293 
1294  if (Elt >= LI->size())
1295  return nullptr; // Out of range reference.
1296  Init *E = LI->getElement(Elt);
1297  // If the element is set to some value, or if we are resolving a reference
1298  // to a specific variable and that variable is explicitly unset, then
1299  // replace the VarListElementInit with it.
1300  if (IRV || !isa<UnsetInit>(E))
1301  return E;
1302  return nullptr;
1303 }
1304 
1306  if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1307  if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1308  return RV->getType();
1309  return nullptr;
1310 }
1311 
1313  StringInit *FieldName) const {
1314  if (isa<RecordRecTy>(getType()))
1315  if (const RecordVal *Val = R.getValue(VarName)) {
1316  if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1317  return nullptr;
1318  Init *TheInit = Val->getValue();
1319  assert(TheInit != this && "Infinite loop detected!");
1320  if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1321  return I;
1322  return nullptr;
1323  }
1324  return nullptr;
1325 }
1326 
1328  if (RecordVal *Val = R.getValue(VarName))
1329  if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1330  return Val->getValue();
1331  return const_cast<VarInit *>(this);
1332 }
1333 
1335  typedef std::pair<TypedInit *, unsigned> Key;
1336  static DenseMap<Key, VarBitInit*> ThePool;
1337 
1338  Key TheKey(std::make_pair(T, B));
1339 
1340  VarBitInit *&I = ThePool[TheKey];
1341  if (!I)
1342  I = new(Allocator) VarBitInit(T, B);
1343  return I;
1344 }
1345 
1347  if (isa<BitRecTy>(Ty))
1348  return const_cast<VarBitInit *>(this);
1349 
1350  return nullptr;
1351 }
1352 
1353 std::string VarBitInit::getAsString() const {
1354  return TI->getAsString() + "{" + utostr(Bit) + "}";
1355 }
1356 
1358  Init *I = TI->resolveReferences(R, RV);
1359  if (TI != I)
1360  return I->getBit(getBitNum());
1361 
1362  return const_cast<VarBitInit*>(this);
1363 }
1364 
1366  unsigned E) {
1367  typedef std::pair<TypedInit *, unsigned> Key;
1368  static DenseMap<Key, VarListElementInit*> ThePool;
1369 
1370  Key TheKey(std::make_pair(T, E));
1371 
1372  VarListElementInit *&I = ThePool[TheKey];
1373  if (!I) I = new(Allocator) VarListElementInit(T, E);
1374  return I;
1375 }
1376 
1377 std::string VarListElementInit::getAsString() const {
1378  return TI->getAsString() + "[" + utostr(Element) + "]";
1379 }
1380 
1381 Init *
1384  getElementNum()))
1385  return I;
1386  return const_cast<VarListElementInit *>(this);
1387 }
1388 
1390  if (getType() == BitRecTy::get())
1391  return const_cast<VarListElementInit*>(this);
1392  return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1393 }
1394 
1396  const RecordVal *RV,
1397  unsigned Elt) const {
1398  if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
1399  if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1400  if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
1401  return Result2;
1402  return VarListElementInit::get(TInit, Elt);
1403  }
1404  return Result;
1405  }
1406 
1407  return nullptr;
1408 }
1409 
1411  return R->getDefInit();
1412 }
1413 
1415  if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1416  if (getDef()->isSubClassOf(RRT->getRecord()))
1417  return const_cast<DefInit *>(this);
1418  return nullptr;
1419 }
1420 
1422  if (const RecordVal *RV = Def->getValue(FieldName))
1423  return RV->getType();
1424  return nullptr;
1425 }
1426 
1428  StringInit *FieldName) const {
1429  return Def->getValue(FieldName)->getValue();
1430 }
1431 
1432 std::string DefInit::getAsString() const {
1433  return Def->getName();
1434 }
1435 
1437  typedef std::pair<Init *, StringInit *> Key;
1438  static DenseMap<Key, FieldInit*> ThePool;
1439 
1440  Key TheKey(std::make_pair(R, FN));
1441 
1442  FieldInit *&I = ThePool[TheKey];
1443  if (!I) I = new(Allocator) FieldInit(R, FN);
1444  return I;
1445 }
1446 
1447 Init *FieldInit::getBit(unsigned Bit) const {
1448  if (getType() == BitRecTy::get())
1449  return const_cast<FieldInit*>(this);
1450  return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1451 }
1452 
1454  unsigned Elt) const {
1455  if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1456  if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1457  if (Elt >= LI->size()) return nullptr;
1458  Init *E = LI->getElement(Elt);
1459 
1460  // If the element is set to some value, or if we are resolving a
1461  // reference to a specific variable and that variable is explicitly
1462  // unset, then replace the VarListElementInit with it.
1463  if (RV || !isa<UnsetInit>(E))
1464  return E;
1465  }
1466  return nullptr;
1467 }
1468 
1470  Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1471 
1472  if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
1473  Init *BVR = BitsVal->resolveReferences(R, RV);
1474  return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1475  }
1476 
1477  if (NewRec != Rec)
1478  return FieldInit::get(NewRec, FieldName);
1479  return const_cast<FieldInit *>(this);
1480 }
1481 
1483  ArrayRef<Init *> ArgRange,
1484  ArrayRef<StringInit *> NameRange) {
1485  ID.AddPointer(V);
1486  ID.AddPointer(VN);
1487 
1488  ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1490  while (Arg != ArgRange.end()) {
1491  assert(Name != NameRange.end() && "Arg name underflow!");
1492  ID.AddPointer(*Arg++);
1493  ID.AddPointer(*Name++);
1494  }
1495  assert(Name == NameRange.end() && "Arg name overflow!");
1496 }
1497 
1498 DagInit *
1500  ArrayRef<StringInit *> NameRange) {
1501  static FoldingSet<DagInit> ThePool;
1502  static std::vector<DagInit*> TheActualPool;
1503 
1505  ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1506 
1507  void *IP = nullptr;
1508  if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1509  return I;
1510 
1511  DagInit *I = new(Allocator) DagInit(V, VN, ArgRange, NameRange);
1512  ThePool.InsertNode(I, IP);
1513  TheActualPool.push_back(I);
1514  return I;
1515 }
1516 
1517 DagInit *
1519  ArrayRef<std::pair<Init*, StringInit*>> args) {
1522 
1523  for (const auto &Arg : args) {
1524  Args.push_back(Arg.first);
1525  Names.push_back(Arg.second);
1526  }
1527 
1528  return DagInit::get(V, VN, Args, Names);
1529 }
1530 
1532  ProfileDagInit(ID, Val, ValName, Args, ArgNames);
1533 }
1534 
1536  if (isa<DagRecTy>(Ty))
1537  return const_cast<DagInit *>(this);
1538 
1539  return nullptr;
1540 }
1541 
1543  SmallVector<Init*, 8> NewArgs;
1544  NewArgs.reserve(Args.size());
1545  bool ArgsChanged = false;
1546  for (const Init *Arg : Args) {
1547  Init *NewArg = Arg->resolveReferences(R, RV);
1548  NewArgs.push_back(NewArg);
1549  ArgsChanged |= NewArg != Arg;
1550  }
1551 
1552  Init *Op = Val->resolveReferences(R, RV);
1553  if (Op != Val || ArgsChanged)
1554  return DagInit::get(Op, ValName, NewArgs, ArgNames);
1555 
1556  return const_cast<DagInit *>(this);
1557 }
1558 
1559 std::string DagInit::getAsString() const {
1560  std::string Result = "(" + Val->getAsString();
1561  if (ValName)
1562  Result += ":" + ValName->getAsUnquotedString();
1563  if (!Args.empty()) {
1564  Result += " " + Args[0]->getAsString();
1565  if (ArgNames[0]) Result += ":$" + ArgNames[0]->getAsUnquotedString();
1566  for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1567  Result += ", " + Args[i]->getAsString();
1568  if (ArgNames[i]) Result += ":$" + ArgNames[i]->getAsUnquotedString();
1569  }
1570  }
1571  return Result + ")";
1572 }
1573 
1574 //===----------------------------------------------------------------------===//
1575 // Other implementations
1576 //===----------------------------------------------------------------------===//
1577 
1579  : Name(N), TyAndPrefix(T, P) {
1581  assert(Value && "Cannot create unset value for current type!");
1582 }
1583 
1585  : Name(StringInit::get(N)), TyAndPrefix(T, P) {
1587  assert(Value && "Cannot create unset value for current type!");
1588 }
1589 
1591  return cast<StringInit>(getNameInit())->getValue();
1592 }
1593 
1594 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1595 
1596 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1597  if (getPrefix()) OS << "field ";
1598  OS << *getType() << " " << getNameInitAsString();
1599 
1600  if (getValue())
1601  OS << " = " << *getValue();
1602 
1603  if (PrintSem) OS << ";\n";
1604 }
1605 
1606 unsigned Record::LastID = 0;
1607 
1608 void Record::init() {
1609  checkName();
1610 
1611  // Every record potentially has a def at the top. This value is
1612  // replaced with the top-level def name at instantiation time.
1613  RecordVal DN("NAME", StringRecTy::get(), false);
1614  addValue(DN);
1615 }
1616 
1617 void Record::checkName() {
1618  // Ensure the record name has string type.
1619  const TypedInit *TypedName = cast<const TypedInit>(Name);
1620  if (!isa<StringRecTy>(TypedName->getType()))
1621  PrintFatalError(getLoc(), "Record name is not a string!");
1622 }
1623 
1625  if (!TheInit)
1626  TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this));
1627  return TheInit;
1628 }
1629 
1631  return cast<StringInit>(Name)->getValue();
1632 }
1633 
1634 void Record::setName(Init *NewName) {
1635  Name = NewName;
1636  checkName();
1637  // DO NOT resolve record values to the name at this point because
1638  // there might be default values for arguments of this def. Those
1639  // arguments might not have been resolved yet so we don't want to
1640  // prematurely assume values for those arguments were not passed to
1641  // this def.
1642  //
1643  // Nonetheless, it may be that some of this Record's values
1644  // reference the record name. Indeed, the reason for having the
1645  // record name be an Init is to provide this flexibility. The extra
1646  // resolve steps after completely instantiating defs takes care of
1647  // this. See TGParser::ParseDef and TGParser::ParseDefm.
1648 }
1649 
1651  setName(StringInit::get(Name));
1652 }
1653 
1655  for (RecordVal &Value : Values) {
1656  if (RV == &Value) // Skip resolve the same field as the given one
1657  continue;
1658  if (Init *V = Value.getValue())
1659  if (Value.setValue(V->resolveReferences(*this, RV)))
1660  PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1661  Value.getNameInitAsString() +
1662  "' after resolving references" +
1663  (RV ? " against '" + RV->getNameInitAsString() +
1664  "' of (" + RV->getValue()->getAsUnquotedString() +
1665  ")"
1666  : "") + "\n");
1667  }
1668  Init *OldName = getNameInit();
1669  Init *NewName = Name->resolveReferences(*this, RV);
1670  if (NewName != OldName) {
1671  // Re-register with RecordKeeper.
1672  setName(NewName);
1673  }
1674 }
1675 
1676 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1677 
1679  OS << R.getNameInitAsString();
1680 
1681  ArrayRef<Init *> TArgs = R.getTemplateArgs();
1682  if (!TArgs.empty()) {
1683  OS << "<";
1684  bool NeedComma = false;
1685  for (const Init *TA : TArgs) {
1686  if (NeedComma) OS << ", ";
1687  NeedComma = true;
1688  const RecordVal *RV = R.getValue(TA);
1689  assert(RV && "Template argument record not found??");
1690  RV->print(OS, false);
1691  }
1692  OS << ">";
1693  }
1694 
1695  OS << " {";
1697  if (!SC.empty()) {
1698  OS << "\t//";
1699  for (const auto &SuperPair : SC)
1700  OS << " " << SuperPair.first->getNameInitAsString();
1701  }
1702  OS << "\n";
1703 
1704  for (const RecordVal &Val : R.getValues())
1705  if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1706  OS << Val;
1707  for (const RecordVal &Val : R.getValues())
1708  if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1709  OS << Val;
1710 
1711  return OS << "}\n";
1712 }
1713 
1715  const RecordVal *R = getValue(FieldName);
1716  if (!R || !R->getValue())
1717  PrintFatalError(getLoc(), "Record `" + getName() +
1718  "' does not have a field named `" + FieldName + "'!\n");
1719  return R->getValue();
1720 }
1721 
1722 std::string Record::getValueAsString(StringRef FieldName) const {
1723  const RecordVal *R = getValue(FieldName);
1724  if (!R || !R->getValue())
1725  PrintFatalError(getLoc(), "Record `" + getName() +
1726  "' does not have a field named `" + FieldName + "'!\n");
1727 
1728  if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1729  return SI->getValue();
1730  if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1731  return CI->getValue();
1732 
1733  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1734  FieldName + "' does not have a string initializer!");
1735 }
1736 
1738  const RecordVal *R = getValue(FieldName);
1739  if (!R || !R->getValue())
1740  PrintFatalError(getLoc(), "Record `" + getName() +
1741  "' does not have a field named `" + FieldName + "'!\n");
1742 
1743  if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1744  return BI;
1745  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1746  FieldName + "' does not have a BitsInit initializer!");
1747 }
1748 
1750  const RecordVal *R = getValue(FieldName);
1751  if (!R || !R->getValue())
1752  PrintFatalError(getLoc(), "Record `" + getName() +
1753  "' does not have a field named `" + FieldName + "'!\n");
1754 
1755  if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1756  return LI;
1757  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1758  FieldName + "' does not have a list initializer!");
1759 }
1760 
1761 std::vector<Record*>
1763  ListInit *List = getValueAsListInit(FieldName);
1764  std::vector<Record*> Defs;
1765  for (Init *I : List->getValues()) {
1766  if (DefInit *DI = dyn_cast<DefInit>(I))
1767  Defs.push_back(DI->getDef());
1768  else
1769  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1770  FieldName + "' list is not entirely DefInit!");
1771  }
1772  return Defs;
1773 }
1774 
1775 int64_t Record::getValueAsInt(StringRef FieldName) const {
1776  const RecordVal *R = getValue(FieldName);
1777  if (!R || !R->getValue())
1778  PrintFatalError(getLoc(), "Record `" + getName() +
1779  "' does not have a field named `" + FieldName + "'!\n");
1780 
1781  if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1782  return II->getValue();
1783  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1784  FieldName + "' does not have an int initializer!");
1785 }
1786 
1787 std::vector<int64_t>
1789  ListInit *List = getValueAsListInit(FieldName);
1790  std::vector<int64_t> Ints;
1791  for (Init *I : List->getValues()) {
1792  if (IntInit *II = dyn_cast<IntInit>(I))
1793  Ints.push_back(II->getValue());
1794  else
1795  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1796  FieldName + "' does not have a list of ints initializer!");
1797  }
1798  return Ints;
1799 }
1800 
1801 std::vector<std::string>
1803  ListInit *List = getValueAsListInit(FieldName);
1804  std::vector<std::string> Strings;
1805  for (Init *I : List->getValues()) {
1806  if (StringInit *SI = dyn_cast<StringInit>(I))
1807  Strings.push_back(SI->getValue());
1808  else
1809  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1810  FieldName + "' does not have a list of strings initializer!");
1811  }
1812  return Strings;
1813 }
1814 
1816  const RecordVal *R = getValue(FieldName);
1817  if (!R || !R->getValue())
1818  PrintFatalError(getLoc(), "Record `" + getName() +
1819  "' does not have a field named `" + FieldName + "'!\n");
1820 
1821  if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1822  return DI->getDef();
1823  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1824  FieldName + "' does not have a def initializer!");
1825 }
1826 
1827 bool Record::getValueAsBit(StringRef FieldName) const {
1828  const RecordVal *R = getValue(FieldName);
1829  if (!R || !R->getValue())
1830  PrintFatalError(getLoc(), "Record `" + getName() +
1831  "' does not have a field named `" + FieldName + "'!\n");
1832 
1833  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1834  return BI->getValue();
1835  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1836  FieldName + "' does not have a bit initializer!");
1837 }
1838 
1839 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1840  const RecordVal *R = getValue(FieldName);
1841  if (!R || !R->getValue())
1842  PrintFatalError(getLoc(), "Record `" + getName() +
1843  "' does not have a field named `" + FieldName.str() + "'!\n");
1844 
1845  if (isa<UnsetInit>(R->getValue())) {
1846  Unset = true;
1847  return false;
1848  }
1849  Unset = false;
1850  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1851  return BI->getValue();
1852  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1853  FieldName + "' does not have a bit initializer!");
1854 }
1855 
1857  const RecordVal *R = getValue(FieldName);
1858  if (!R || !R->getValue())
1859  PrintFatalError(getLoc(), "Record `" + getName() +
1860  "' does not have a field named `" + FieldName + "'!\n");
1861 
1862  if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1863  return DI;
1864  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1865  FieldName + "' does not have a dag initializer!");
1866 }
1867 
1869  errs() << "Record:\n";
1870  Rec.dump();
1871 
1872  errs() << "Defs:\n";
1873  for (const auto &Proto : DefPrototypes)
1874  Proto->dump();
1875 }
1876 
1877 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
1878 
1880  OS << "------------- Classes -----------------\n";
1881  for (const auto &C : RK.getClasses())
1882  OS << "class " << *C.second;
1883 
1884  OS << "------------- Defs -----------------\n";
1885  for (const auto &D : RK.getDefs())
1886  OS << "def " << *D.second;
1887  return OS;
1888 }
1889 
1890 std::vector<Record *>
1892  Record *Class = getClass(ClassName);
1893  if (!Class)
1894  PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1895 
1896  std::vector<Record*> Defs;
1897  for (const auto &D : getDefs())
1898  if (D.second->isSubClassOf(Class))
1899  Defs.push_back(D.second.get());
1900 
1901  return Defs;
1902 }
1903 
1904 static Init *GetStrConcat(Init *I0, Init *I1) {
1905  // Shortcut for the common case of concatenating two strings.
1906  if (const StringInit *I0s = dyn_cast<StringInit>(I0))
1907  if (const StringInit *I1s = dyn_cast<StringInit>(I1))
1908  return ConcatStringInits(I0s, I1s);
1910 }
1911 
1912 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1913  Init *Name, StringRef Scoper) {
1914  Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
1915  NewName = GetStrConcat(NewName, Name);
1916  if (CurMultiClass && Scoper != "::") {
1917  Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(),
1918  StringInit::get("::"));
1919  NewName = GetStrConcat(Prefix, NewName);
1920  }
1921 
1922  if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
1923  NewName = BinOp->Fold(&CurRec, CurMultiClass);
1924  return NewName;
1925 }
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1327
MachineLoop * L
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:748
static Init * GetStrConcat(Init *I0, Init *I1)
Definition: Record.cpp:1904
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:52
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:241
void print(raw_ostream &OS) const
Definition: Record.h:76
Init * resolveReferences(Record &R, const RecordVal *RV) 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:527
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1469
RecTyKind
Subclass discriminator (for dyn_cast<> et al.)
Definition: Record.h:54
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
virtual bool isComplete() const
This virtual method should be overridden by values that may not be completely specified yet...
Definition: Record.h:319
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:237
unsigned getNumBits() const
Definition: Record.h:519
unsigned getNumArgs() const
Definition: Record.h:1176
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:1827
size_t i
X.Y - Represent a reference to a subfield of a variable.
Definition: Record.h:1105
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
void dump() const
Definition: Record.cpp:1676
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:393
bool getPrefix() const
Definition: Record.h:1240
[AL, AH, CL] - Represent a list of defs
Definition: Record.h:673
'7' - Represent an initialization by a literal integer value.
Definition: Record.h:556
'list<Ty>' - Represent a list of values, all of which must be of the specified type.
Definition: Record.h:189
!op (X, Y) - Combine two inits.
Definition: Record.h:823
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1389
Record * getElementAsRecord(unsigned i) const
Definition: Record.cpp:519
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:51
Init * resolveReferences(Record &R, const RecordVal *RV) 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:720
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:192
AL - Represent a reference to a 'def' in the description.
Definition: Record.h:1061
StringRef getName() const
Definition: Record.cpp:1271
std::string getNameInitAsString() const
Definition: Record.h:1236
iterator end() const
Definition: ArrayRef.h:130
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
Definition: Record.cpp:603
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:548
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:1222
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:1714
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1421
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:572
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1092
RecTy * getType() const
Definition: Record.h:420
StringInit * getArgName(unsigned Num) const
Definition: Record.h:1181
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1353
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
Definition: Record.cpp:1024
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:474
BinaryOp getOpcode() const
Definition: Record.h:863
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:1815
static VarInit * get(StringRef VN, RecTy *T)
Definition: Record.cpp:1254
Init * getElement(unsigned i) const
Definition: Record.h:698
void dump() const
Definition: Record.cpp:1594
void InsertNode(Node *N, void *InsertPos)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition: FoldingSet.cpp:345
static BumpPtrAllocator Allocator
Definition: Record.cpp:31
void reserve(size_type N)
Definition: SmallVector.h:377
std::string 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:1722
Init * getOperator() const
Definition: Record.h:1169
'{ a, b, c }' - Represents an initializer for a BitsRecTy value.
Definition: Record.h:497
std::string getAsString() const override
Definition: Record.cpp:94
virtual std::string getAsUnquotedString() const
Convert this value to a string form, without adding quote markers.
Definition: Record.h:330
static BitRecTy * get()
Definition: Record.h:104
ArrayRef< Init * > getTemplateArgs() const
Definition: Record.h:1341
void print(raw_ostream &OS) const
Print out this value.
Definition: Record.h:322
static IntInit * get(int64_t V)
Definition: Record.cpp:348
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:172
'?' - Represents an uninitialized value
Definition: Record.h:442
'true'/'false' - Represent a concrete initializer for a bit.
Definition: Record.h:467
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:268
StringRef getName() const
Definition: Record.cpp:1590
Init * getValue() const
Definition: Record.h:1242
static BitsRecTy * get(unsigned Sz)
Definition: Record.cpp:64
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const =0
This method is used to implement VarListElementInit::resolveReferences.
This is the common super-class of types that have a specific, explicit, type.
Definition: Record.h:404
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:440
static void ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef< Init * > Range)
Definition: Record.cpp:209
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:78
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:590
static StringRecTy * get()
Definition: Record.h:181
Init * getLHS() const
Definition: Record.h:864
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init * > ArgRange, ArrayRef< StringInit * > NameRange)
Definition: Record.cpp:1499
Init * getOperand() const
Definition: Record.h:810
std::string getAsString() const override
Definition: Record.cpp:112
std::string getAsString() const override
Definition: Record.cpp:90
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
List[4] - Represent access to one element of a var or field.
Definition: Record.h:1022
'[classname]' - Represent an instance of a class, such as: (R32 X = EAX).
Definition: Record.h:229
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:429
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:356
StringRef getName() const
Definition: Record.cpp:1630
void AddInteger(signed I)
Definition: FoldingSet.cpp:61
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1357
static Init * ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass)
Definition: Record.cpp:961
int64_t getValue() const
Definition: Record.h:572
virtual Init * getFieldInit(Record &R, const RecordVal *RV, StringInit *FieldName) const
This method complements getFieldType to return the initializer for the specified field.
Definition: Record.h:370
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:728
virtual unsigned getNumOperands() const =0
void print(raw_ostream &OS, bool PrintSem=true) const
Definition: Record.cpp:1596
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:1737
const_iterator begin() const
Definition: Record.h:722
const T * getTrailingObjects() const
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
RecTy * getType() const
Definition: Record.h:1241
'bits<n>' - Represent a fixed number of bits
Definition: Record.h:113
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:1749
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1542
const std::string getNameInitAsString() const
Definition: Record.h:1329
Init * getArg(unsigned Num) const
Definition: Record.h:1177
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1531
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:561
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:79
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
Init * getRHS() const
Definition: Record.h:865
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
virtual unsigned getBitNum() const
This method is used to retrieve the bit number of a bit reference.
Definition: Record.h:394
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
Definition: Record.cpp:624
virtual Init * getBit(unsigned Bit) const =0
This method is used to return the initializer for the specified bit.
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:1856
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:1395
virtual Init * getOperand(unsigned i) const =0
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
std::vector< std::string > 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:1802
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:508
bool getValue() const
Definition: Record.h:482
'code' - Represent a code fragment
Definition: Record.h:134
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
static BitsInit * get(ArrayRef< Init * > Range)
Definition: Record.cpp:216
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:1654
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:436
std::vector< Record * > getAllDerivedDefinitions(StringRef ClassName) const
This method returns all concrete definitions that derive from the specified class name...
Definition: Record.cpp:1891
virtual Init * convertInitializerTo(RecTy *Ty) const =0
This virtual function converts to the appropriate Init based on the passed in type.
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:480
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:85
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:316
void dump() const
Debugging method that may be called through a debugger, just invokes print on stderr.
Definition: Record.cpp:164
Base class for operators.
Definition: Record.h:741
#define P(N)
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1535
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:924
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:116
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
virtual OpInit * clone(ArrayRef< Init * > Operands) const =0
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:1282
"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:1133
RecordVector DefPrototypes
Definition: Record.h:1553
static void ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:897
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:766
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:212
static void ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type)
Definition: Record.cpp:597
!op (X) - Transform an init.
Definition: Record.h:773
TypedInit * getVariable() const
Definition: Record.h:1044
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1346
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.h:481
Init * getLHS() const
Definition: Record.h:922
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1361
iterator begin() const
Definition: ArrayRef.h:129
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
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:1788
'string' - Represent an string value
Definition: Record.h:170
RecTyKind getRecTyKind() const
Definition: Record.h:73
static BitInit * get(bool V)
Definition: Record.cpp:185
static bool canFitInBitfield(int64_t Value, unsigned NumBits)
Definition: Record.cpp:360
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:136
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1432
static CodeInit * get(StringRef)
Definition: Record.cpp:405
static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:740
ArrayRef< Init * > getValues() const
Definition: Record.h:718
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:225
Record * getDef() const
Definition: Record.h:1080
DefInit * getDefInit()
get the corresponding DefInit.
Definition: Record.cpp:1624
static std::string itostr(int64_t X)
Definition: StringExtras.h:95
size_t size() const
Definition: Record.h:725
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:1762
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:419
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1559
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.h:380
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:1775
const RecordMap & getClasses() const
Definition: Record.h:1566
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1141
virtual Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const =0
static FieldInit * get(Init *R, StringInit *FN)
Definition: Record.cpp:1436
static Init * EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass)
Definition: Record.cpp:931
uint64_t * Vals
std::string getAsString() const override
Definition: Record.cpp:74
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:1238
static StringInit * ConcatStringInits(const StringInit *I0, const StringInit *I1)
Definition: Record.cpp:770
void dump() const
Definition: Record.cpp:1868
ArrayRef< SMLoc > getLoc() const
Definition: Record.h:1336
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
static VarBitInit * get(TypedInit *T, unsigned B)
Definition: Record.cpp:1334
static StringInit * get(StringRef)
Definition: Record.cpp:417
Init * QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, StringRef Scoper)
Return an Init with a qualifier prefix referring to CurRec's name.
Definition: Record.cpp:1912
static RecordRecTy * get(Record *R)
Definition: Record.cpp:108
Init * getMHS() const
Definition: Record.h:923
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:279
CHAIN = SC CHAIN, Imm128 - System call.
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1414
Init * getNameInit() const
Definition: Record.h:1325
const_iterator end() const
Definition: Record.h:723
Init * getNameInit() const
Definition: Record.h:1234
static DefInit * get(Record *)
Definition: Record.cpp:1410
Record * getClass(StringRef Name) const
Definition: Record.h:1569
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:366
Init * getFieldInit(Record &R, const RecordVal *RV, StringInit *FieldName) const override
This method complements getFieldType to return the initializer for the specified field.
Definition: Record.cpp:1427
Init * resolveReferences(Record &R, const RecordVal *RV) 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:870
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
unsigned getElementNum() const
Definition: Record.h:1045
static void ProfileListInit(FoldingSetNodeID &ID, ArrayRef< Init * > Range, RecTy *EltTy)
Definition: Record.cpp:443
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
bool isTemplateArg(Init *Name) const
Definition: Record.h:1351
const RecordMap & getDefs() const
Definition: Record.h:1567
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:464
LLVM_NODISCARD StringRef copy(Allocator &A) const
Definition: StringRef.h:153
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.h:548
'int' - Represent an integer value of no particular size
Definition: Record.h:151
static Init * fixBitInit(const RecordVal *RV, Init *Before, Init *After)
Definition: Record.cpp:294
ArrayRef< RecordVal > getValues() const
Definition: Record.h:1345
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:1453
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1276
RecordKeeper & getRecords() const
Definition: Record.h:1449
static MachineInstr * getDef(unsigned Reg, const MachineRegisterInfo *MRI)
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:879
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:98
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
unsigned getNumBits() const
Definition: Record.h:125
virtual Init * getBitVar() const
This method is used to retrieve the initializer for bit reference.
Definition: Record.h:390
bool isSubClassOf(const Record *R) const
Definition: Record.h:1415
static UnsetInit * get()
Definition: Record.cpp:166
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:56
void addValue(const RecordVal &RV)
Definition: Record.h:1390
const NodeList & List
Definition: RDFGraph.cpp:205
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:906
static VarListElementInit * get(TypedInit *T, unsigned E)
Definition: Record.cpp:1365
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition: Record.cpp:131
Opcode{0} - Represent access to one bit of a variable or field.
Definition: Record.h:984
'Opcode' - Represent a reference to an entire variable object.
Definition: Record.h:939
static ListInit * get(ArrayRef< Init * > Range, RecTy *EltTy)
Definition: Record.cpp:453
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1122
std::string getAsString() const override
Definition: Record.cpp:104
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:287
UnaryOp getOpcode() const
Definition: Record.h:809
'bit' - Represent a single bit
Definition: Record.h:94
!op (X, Y, Z) - Combine two inits.
Definition: Record.h:878
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
Definition: Record.cpp:777
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1726
Record * getRecord() const
Definition: Record.h:243
bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const
This method looks up the specified field and returns its value as a bit.
Definition: Record.cpp:1839
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1382
void dump() const
Definition: Record.cpp:1877
'dag' - Represent a dag fragment
Definition: Record.h:211
Init * resolveReferences(Record &R, const RecordVal *RV) 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:303
static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, ArrayRef< Init * > ArgRange, ArrayRef< StringInit * > NameRange)
Definition: Record.cpp:1482
Init * getFieldInit(Record &R, const RecordVal *RV, StringInit *FieldName) const override
This method complements getFieldType to return the initializer for the specified field.
Definition: Record.cpp:1312
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:1447
StringRef getValue() const
Definition: Record.h:610
Init * getNameInit() const
Definition: Record.h:957
LLVM Value Representation.
Definition: Value.h:71
(v a, b) - Represent a DAG tree value.
Definition: Record.h:1140
virtual std::string getAsString() const =0
Convert this value to a string form.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
TernaryOp getOpcode() const
Definition: Record.h:921
void dump() const
Definition: Record.cpp:43
void setName(Init *Name)
Definition: Record.cpp:1634
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
unsigned getBitNum() const override
This method is used to retrieve the bit number of a bit reference.
Definition: Record.h:1009
std::string getAsUnquotedString() const override
Convert this value to a string form, without adding quote markers.
Definition: Record.h:616
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:620
Init * getRHS() const
Definition: Record.h:924
virtual std::string getAsString() const =0
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:171
ListRecTy * getListTy()
Returns the type representing list<this>.
Definition: Record.cpp:45
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
Definition: Record.h:1347
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1305
#define T1
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1377
static IntRecTy * get()
Definition: Record.h:161
RecordVal(Init *N, RecTy *T, bool P)
Definition: Record.cpp:1578