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