llvm.org GIT mirror llvm / 17246b1
[TableGen] Remove all the variations of RecTy::convertValue and just handle the conversions in convertInitializerTo directly. This saves a bunch of vtable entries. NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238646 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 5 years ago
2 changed file(s) with 232 addition(s) and 284 deletion(s). Raw diff Collapse all Expand all
9999 /// getListTy - Returns the type representing list.
100100 ListRecTy *getListTy();
101101
102 public: // These methods should only be called from subclasses of Init
103 virtual Init *convertValue( UnsetInit *UI) { return nullptr; }
104 virtual Init *convertValue( BitInit *BI) { return nullptr; }
105 virtual Init *convertValue( BitsInit *BI) { return nullptr; }
106 virtual Init *convertValue( IntInit *II) { return nullptr; }
107 virtual Init *convertValue(StringInit *SI) { return nullptr; }
108 virtual Init *convertValue( ListInit *LI) { return nullptr; }
109 virtual Init *convertValue(VarBitInit *VB) { return nullptr; }
110 virtual Init *convertValue( DefInit *DI) { return nullptr; }
111 virtual Init *convertValue( DagInit *DI) { return nullptr; }
112 virtual Init *convertValue( TypedInit *TI) { return nullptr; }
113 virtual Init *convertValue( VarInit *VI) {
114 return convertValue((TypedInit*)VI);
115 }
116 virtual Init *convertValue( FieldInit *FI) {
117 return convertValue((TypedInit*)FI);
118 }
119
120 public:
121102 virtual bool baseClassOf(const RecTy*) const;
122103 };
123104
139120
140121 static BitRecTy *get() { return &Shared; }
141122
142 using RecTy::convertValue;
143 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
144 Init *convertValue( BitInit *BI) override { return (Init*)BI; }
145 Init *convertValue( BitsInit *BI) override;
146 Init *convertValue( IntInit *II) override;
147 Init *convertValue(VarBitInit *VB) override { return (Init*)VB; }
148 Init *convertValue( TypedInit *TI) override;
149
150123 std::string getAsString() const override { return "bit"; }
151124
152125 bool typeIsConvertibleTo(const RecTy *RHS) const override {
170143
171144 unsigned getNumBits() const { return Size; }
172145
173 using RecTy::convertValue;
174 Init *convertValue( UnsetInit *UI) override;
175 Init *convertValue( BitInit *UI) override;
176 Init *convertValue( BitsInit *BI) override;
177 Init *convertValue( IntInit *II) override;
178 Init *convertValue( TypedInit *TI) override;
179
180146 std::string getAsString() const override;
181147
182148 bool typeIsConvertibleTo(const RecTy *RHS) const override {
198164
199165 static IntRecTy *get() { return &Shared; }
200166
201 using RecTy::convertValue;
202 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
203 Init *convertValue( BitInit *BI) override;
204 Init *convertValue( BitsInit *BI) override;
205 Init *convertValue( IntInit *II) override { return (Init*)II; }
206 Init *convertValue( TypedInit *TI) override;
207
208167 std::string getAsString() const override { return "int"; }
209168
210169 bool typeIsConvertibleTo(const RecTy *RHS) const override {
220179 static StringRecTy Shared;
221180 StringRecTy() : RecTy(StringRecTyKind) {}
222181
182 void anchor() override;
223183 public:
224184 static bool classof(const RecTy *RT) {
225185 return RT->getRecTyKind() == StringRecTyKind;
226186 }
227187
228188 static StringRecTy *get() { return &Shared; }
229
230 using RecTy::convertValue;
231 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
232 Init *convertValue(StringInit *SI) override { return (Init*)SI; }
233 Init *convertValue( TypedInit *TI) override;
234189
235190 std::string getAsString() const override { return "string"; }
236191
255210 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
256211 RecTy *getElementType() const { return Ty; }
257212
258 using RecTy::convertValue;
259 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
260 Init *convertValue( ListInit *LI) override;
261 Init *convertValue( TypedInit *TI) override;
262
263213 std::string getAsString() const override;
264214
265215 bool typeIsConvertibleTo(const RecTy *RHS) const override {
275225 static DagRecTy Shared;
276226 DagRecTy() : RecTy(DagRecTyKind) {}
277227
228 void anchor() override;
278229 public:
279230 static bool classof(const RecTy *RT) {
280231 return RT->getRecTyKind() == DagRecTyKind;
281232 }
282233
283234 static DagRecTy *get() { return &Shared; }
284
285 using RecTy::convertValue;
286 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
287 Init *convertValue( DagInit *DI) override { return (Init*)DI; }
288 Init *convertValue( TypedInit *TI) override;
289235
290236 std::string getAsString() const override { return "dag"; }
291237
310256 static RecordRecTy *get(Record *R);
311257
312258 Record *getRecord() const { return Rec; }
313
314 using RecTy::convertValue;
315 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
316 Init *convertValue( DefInit *DI) override;
317 Init *convertValue( TypedInit *TI) override;
318259
319260 std::string getAsString() const override;
320261
404345 /// invokes print on stderr.
405346 void dump() const;
406347
407 /// convertInitializerTo - This virtual function is a simple call-back
408 /// function that should be overridden to call the appropriate
409 /// RecTy::convertValue method.
410 ///
348 /// convertInitializerTo - This virtual function converts to the appropriate
349 /// Init based on the passed in type.
411350 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
412351
413352 /// convertInitializerBitRange - This method is used to implement the bitrange
497436 }
498437 RecTy *getType() const { return Ty; }
499438
439 Init *convertInitializerTo(RecTy *Ty) const override;
440
500441 Init *
501442 convertInitializerBitRange(const std::vector &Bits) const override;
502443 Init *
521462 UnsetInit() : Init(IK_UnsetInit) {}
522463 UnsetInit(const UnsetInit &) = delete;
523464 UnsetInit &operator=(const UnsetInit &Other) = delete;
524 void anchor() override;
525465
526466 public:
527467 static bool classof(const Init *I) {
529469 }
530470 static UnsetInit *get();
531471
532 Init *convertInitializerTo(RecTy *Ty) const override {
533 return Ty->convertValue(const_cast(this));
534 }
472 Init *convertInitializerTo(RecTy *Ty) const override;
535473
536474 Init *getBit(unsigned Bit) const override {
537475 return const_cast(this);
549487 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
550488 BitInit(const BitInit &Other) = delete;
551489 BitInit &operator=(BitInit &Other) = delete;
552 void anchor() override;
553490
554491 public:
555492 static bool classof(const Init *I) {
559496
560497 bool getValue() const { return Value; }
561498
562 Init *convertInitializerTo(RecTy *Ty) const override {
563 return Ty->convertValue(const_cast(this));
564 }
499 Init *convertInitializerTo(RecTy *Ty) const override;
565500
566501 Init *getBit(unsigned Bit) const override {
567502 assert(Bit < 1 && "Bit index out of range!");
594529
595530 unsigned getNumBits() const { return Bits.size(); }
596531
597 Init *convertInitializerTo(RecTy *Ty) const override {
598 return Ty->convertValue(const_cast(this));
599 }
532 Init *convertInitializerTo(RecTy *Ty) const override;
600533 Init *
601534 convertInitializerBitRange(const std::vector &Bits) const override;
602535
647580
648581 int64_t getValue() const { return Value; }
649582
650 Init *convertInitializerTo(RecTy *Ty) const override {
651 return Ty->convertValue(const_cast(this));
652 }
583 Init *convertInitializerTo(RecTy *Ty) const override;
653584 Init *
654585 convertInitializerBitRange(const std::vector &Bits) const override;
655586
678609
679610 StringInit(const StringInit &Other) = delete;
680611 StringInit &operator=(const StringInit &Other) = delete;
681 void anchor() override;
682612
683613 public:
684614 static bool classof(const Init *I) {
688618
689619 const std::string &getValue() const { return Value; }
690620
691 Init *convertInitializerTo(RecTy *Ty) const override {
692 return Ty->convertValue(const_cast(this));
693 }
621 Init *convertInitializerTo(RecTy *Ty) const override;
694622
695623 std::string getAsString() const override { return "\"" + Value + "\""; }
696624 std::string getAsUnquotedString() const override { return Value; }
743671 Init *
744672 convertInitListSlice(const std::vector &Elements) const override;
745673
746 Init *convertInitializerTo(RecTy *Ty) const override {
747 return Ty->convertValue(const_cast(this));
748 }
674 Init *convertInitializerTo(RecTy *Ty) const override;
749675
750676 /// resolveReferences - This method is used by classes that refer to other
751677 /// variables which may not be defined at the time they expression is formed.
797723 // Fold - If possible, fold this to a simpler init. Return this if not
798724 // possible to fold.
799725 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
800
801 Init *convertInitializerTo(RecTy *Ty) const override {
802 return Ty->convertValue(const_cast(this));
803 }
804726
805727 Init *resolveListElementReference(Record &R, const RecordVal *RV,
806728 unsigned Elt) const override;
990912 static VarInit *get(const std::string &VN, RecTy *T);
991913 static VarInit *get(Init *VN, RecTy *T);
992914
993 Init *convertInitializerTo(RecTy *Ty) const override {
994 return Ty->convertValue(const_cast(this));
995 }
996
997915 const std::string &getName() const;
998916 Init *getNameInit() const { return VarName; }
999917 std::string getNameInitAsString() const {
1042960 }
1043961 static VarBitInit *get(TypedInit *T, unsigned B);
1044962
1045 Init *convertInitializerTo(RecTy *Ty) const override {
1046 return Ty->convertValue(const_cast(this));
1047 }
963 Init *convertInitializerTo(RecTy *Ty) const override;
1048964
1049965 Init *getBitVar() const override { return TI; }
1050966 unsigned getBitNum() const override { return Bit; }
1081997 }
1082998 static VarListElementInit *get(TypedInit *T, unsigned E);
1083999
1084 Init *convertInitializerTo(RecTy *Ty) const override {
1085 return Ty->convertValue(const_cast(this));
1086 }
1087
10881000 TypedInit *getVariable() const { return TI; }
10891001 unsigned getElementNum() const { return Element; }
10901002
11171029 }
11181030 static DefInit *get(Record*);
11191031
1120 Init *convertInitializerTo(RecTy *Ty) const override {
1121 return Ty->convertValue(const_cast(this));
1122 }
1032 Init *convertInitializerTo(RecTy *Ty) const override;
11231033
11241034 Record *getDef() const { return Def; }
11251035
11651075 static FieldInit *get(Init *R, const std::string &FN);
11661076 static FieldInit *get(Init *R, const Init *FN);
11671077
1168 Init *convertInitializerTo(RecTy *Ty) const override {
1169 return Ty->convertValue(const_cast(this));
1170 }
1171
11721078 Init *getBit(unsigned Bit) const override;
11731079
11741080 Init *resolveListElementReference(Record &R, const RecordVal *RV,
12141120
12151121 void Profile(FoldingSetNodeID &ID) const;
12161122
1217 Init *convertInitializerTo(RecTy *Ty) const override {
1218 return Ty->convertValue(const_cast(this));
1219 }
1123 Init *convertInitializerTo(RecTy *Ty) const override;
12201124
12211125 Init *getOperator() const { return Val; }
12221126
8888 void RecTy::anchor() { }
8989 void RecTy::dump() const { print(errs()); }
9090
91 void StringRecTy::anchor() { }
92 void DagRecTy::anchor() { }
93
9194 ListRecTy *RecTy::getListTy() {
9295 if (!ListTy)
9396 ListTy.reset(new ListRecTy(this));
97100 bool RecTy::baseClassOf(const RecTy *RHS) const {
98101 assert (RHS && "NULL pointer");
99102 return Kind == RHS->getRecTyKind();
100 }
101
102 Init *BitRecTy::convertValue(BitsInit *BI) {
103 if (BI->getNumBits() != 1) return nullptr; // Only accept if just one bit!
104 return BI->getBit(0);
105 }
106
107 Init *BitRecTy::convertValue(IntInit *II) {
108 int64_t Val = II->getValue();
109 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
110
111 return BitInit::get(Val != 0);
112 }
113
114 Init *BitRecTy::convertValue(TypedInit *TI) {
115 RecTy *Ty = TI->getType();
116 if (isa(Ty))
117 return TI; // Accept variable if it is already of bit type!
118 if (auto *BitsTy = dyn_cast(Ty))
119 // Accept only bits<1> expression.
120 return BitsTy->getNumBits() == 1 ? TI : nullptr;
121 // Ternary !if can be converted to bit, but only if both sides are
122 // convertible to a bit.
123 if (TernOpInit *TOI = dyn_cast(TI)) {
124 if (TOI->getOpcode() != TernOpInit::TernaryOp::IF)
125 return nullptr;
126 if (!TOI->getMHS()->convertInitializerTo(BitRecTy::get()) ||
127 !TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
128 return nullptr;
129 return TOI;
130 }
131 return nullptr;
132103 }
133104
134105 bool BitRecTy::baseClassOf(const RecTy *RHS) const{
153124 return "bits<" + utostr(Size) + ">";
154125 }
155126
156 Init *BitsRecTy::convertValue(UnsetInit *UI) {
157 SmallVector NewBits(Size);
158
159 for (unsigned i = 0; i != Size; ++i)
160 NewBits[i] = UnsetInit::get();
161
162 return BitsInit::get(NewBits);
163 }
164
165 Init *BitsRecTy::convertValue(BitInit *BI) {
166 if (Size != 1) return nullptr; // Can only convert single bit.
167 return BitsInit::get(BI);
168 }
169
170 /// canFitInBitfield - Return true if the number of bits is large enough to hold
171 /// the integer value.
172 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
173 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
174 return (NumBits >= sizeof(Value) * 8) ||
175 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
176 }
177
178 /// convertValue from Int initializer to bits type: Split the integer up into the
179 /// appropriate bits.
180 ///
181 Init *BitsRecTy::convertValue(IntInit *II) {
182 int64_t Value = II->getValue();
183 // Make sure this bitfield is large enough to hold the integer value.
184 if (!canFitInBitfield(Value, Size))
185 return nullptr;
186
187 SmallVector NewBits(Size);
188
189 for (unsigned i = 0; i != Size; ++i)
190 NewBits[i] = BitInit::get(Value & (1LL << i));
191
192 return BitsInit::get(NewBits);
193 }
194
195 Init *BitsRecTy::convertValue(BitsInit *BI) {
196 // If the number of bits is right, return it. Otherwise we need to expand or
197 // truncate.
198 if (BI->getNumBits() == Size) return BI;
199 return nullptr;
200 }
201
202 Init *BitsRecTy::convertValue(TypedInit *TI) {
203 if (Size == 1 && isa(TI->getType()))
204 return BitsInit::get(TI);
205
206 if (TI->getType()->typeIsConvertibleTo(this)) {
207 SmallVector NewBits(Size);
208
209 for (unsigned i = 0; i != Size; ++i)
210 NewBits[i] = VarBitInit::get(TI, i);
211 return BitsInit::get(NewBits);
212 }
213
214 return nullptr;
215 }
216
217127 bool BitsRecTy::baseClassOf(const RecTy *RHS) const{
218128 if (RecTy::baseClassOf(RHS)) //argument and the receiver are the same type
219129 return cast(RHS)->Size == Size;
221131 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
222132 }
223133
224 Init *IntRecTy::convertValue(BitInit *BI) {
225 return IntInit::get(BI->getValue());
226 }
227
228 Init *IntRecTy::convertValue(BitsInit *BI) {
229 int64_t Result = 0;
230 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
231 if (BitInit *Bit = dyn_cast(BI->getBit(i)))
232 Result |= static_cast(Bit->getValue()) << i;
233 else
234 return nullptr;
235 return IntInit::get(Result);
236 }
237
238 Init *IntRecTy::convertValue(TypedInit *TI) {
239 if (TI->getType()->typeIsConvertibleTo(this))
240 return TI; // Accept variable if already of the right type!
241 return nullptr;
242 }
243
244134 bool IntRecTy::baseClassOf(const RecTy *RHS) const{
245135 RecTyKind kind = RHS->getRecTyKind();
246136 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
247137 }
248138
249139
250 Init *StringRecTy::convertValue(TypedInit *TI) {
251 if (isa(TI->getType()))
252 return TI; // Accept variable if already of the right type!
253 return nullptr;
254 }
255
256140 std::string ListRecTy::getAsString() const {
257141 return "list<" + Ty->getAsString() + ">";
258 }
259
260 Init *ListRecTy::convertValue(ListInit *LI) {
261 std::vector Elements;
262
263 // Verify that all of the elements of the list are subclasses of the
264 // appropriate class!
265 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
266 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
267 Elements.push_back(CI);
268 else
269 return nullptr;
270
271 if (!isa(LI->getType()))
272 return nullptr;
273
274 return ListInit::get(Elements, this);
275 }
276
277 Init *ListRecTy::convertValue(TypedInit *TI) {
278 // Ensure that TI is compatible with our class.
279 if (ListRecTy *LRT = dyn_cast(TI->getType()))
280 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
281 return TI;
282 return nullptr;
283142 }
284143
285144 bool ListRecTy::baseClassOf(const RecTy *RHS) const{
288147 return false;
289148 }
290149
291 Init *DagRecTy::convertValue(TypedInit *TI) {
292 if (TI->getType()->typeIsConvertibleTo(this))
293 return TI;
294 return nullptr;
295 }
296
297150 RecordRecTy *RecordRecTy::get(Record *R) {
298151 return dyn_cast(R->getDefInit()->getType());
299152 }
300153
301154 std::string RecordRecTy::getAsString() const {
302155 return Rec->getName();
303 }
304
305 Init *RecordRecTy::convertValue(DefInit *DI) {
306 // Ensure that DI is a subclass of Rec.
307 if (!DI->getDef()->isSubClassOf(Rec))
308 return nullptr;
309 return DI;
310 }
311
312 Init *RecordRecTy::convertValue(TypedInit *TI) {
313 // Ensure that TI is compatible with Rec.
314 if (RecordRecTy *RRT = dyn_cast(TI->getType()))
315 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
316 RRT->getRecord() == getRecord())
317 return TI;
318 return nullptr;
319156 }
320157
321158 bool RecordRecTy::baseClassOf(const RecTy *RHS) const{
373210 void Init::anchor() { }
374211 void Init::dump() const { return print(errs()); }
375212
376 void UnsetInit::anchor() { }
377
378213 UnsetInit *UnsetInit::get() {
379214 static UnsetInit TheInit;
380215 return &TheInit;
381216 }
382217
383 void BitInit::anchor() { }
218 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
219 if (auto *BRT = dyn_cast(Ty)) {
220 SmallVector NewBits(BRT->getNumBits());
221
222 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
223 NewBits[i] = UnsetInit::get();
224
225 return BitsInit::get(NewBits);
226 }
227
228 // All other types can just be returned.
229 return const_cast(this);
230 }
384231
385232 BitInit *BitInit::get(bool V) {
386233 static BitInit True(true);
387234 static BitInit False(false);
388235
389236 return V ? &True : &False;
237 }
238
239 Init *BitInit::convertInitializerTo(RecTy *Ty) const {
240 if (isa(Ty))
241 return const_cast(this);
242
243 if (isa(Ty))
244 return IntInit::get(getValue());
245
246 if (auto *BRT = dyn_cast(Ty)) {
247 // Can only convert single bit.
248 if (BRT->getNumBits() == 1)
249 return BitsInit::get(const_cast(this));
250 }
251
252 return nullptr;
390253 }
391254
392255 static void
416279
417280 void BitsInit::Profile(FoldingSetNodeID &ID) const {
418281 ProfileBitsInit(ID, Bits);
282 }
283
284 Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
285 if (isa(Ty)) {
286 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
287 return getBit(0);
288 }
289
290 if (auto *BRT = dyn_cast(Ty)) {
291 // If the number of bits is right, return it. Otherwise we need to expand
292 // or truncate.
293 if (getNumBits() != BRT->getNumBits()) return nullptr;
294 return const_cast(this);
295 }
296
297 if (isa(Ty)) {
298 int64_t Result = 0;
299 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
300 if (auto *Bit = dyn_cast(getBit(i)))
301 Result |= static_cast(Bit->getValue()) << i;
302 else
303 return nullptr;
304 return IntInit::get(Result);
305 }
306
307 return nullptr;
419308 }
420309
421310 Init *
511400 return itostr(Value);
512401 }
513402
403 /// canFitInBitfield - Return true if the number of bits is large enough to hold
404 /// the integer value.
405 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
406 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
407 return (NumBits >= sizeof(Value) * 8) ||
408 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
409 }
410
411 Init *IntInit::convertInitializerTo(RecTy *Ty) const {
412 if (isa(Ty))
413 return const_cast(this);
414
415 if (isa(Ty)) {
416 int64_t Val = getValue();
417 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
418 return BitInit::get(Val != 0);
419 }
420
421 if (auto *BRT = dyn_cast(Ty)) {
422 int64_t Value = getValue();
423 // Make sure this bitfield is large enough to hold the integer value.
424 if (!canFitInBitfield(Value, BRT->getNumBits()))
425 return nullptr;
426
427 SmallVector NewBits(BRT->getNumBits());
428 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
429 NewBits[i] = BitInit::get(Value & (1LL << i));
430
431 return BitsInit::get(NewBits);
432 }
433
434 return nullptr;
435 }
436
514437 Init *
515438 IntInit::convertInitializerBitRange(const std::vector &Bits) const {
516439 SmallVector NewBits(Bits.size());
524447 return BitsInit::get(NewBits);
525448 }
526449
527 void StringInit::anchor() { }
528
529450 StringInit *StringInit::get(StringRef V) {
530451 static StringMap> ThePool;
531452
532453 std::unique_ptr &I = ThePool[V];
533454 if (!I) I.reset(new StringInit(V));
534455 return I.get();
456 }
457
458 Init *StringInit::convertInitializerTo(RecTy *Ty) const {
459 if (isa(Ty))
460 return const_cast(this);
461
462 return nullptr;
535463 }
536464
537465 static void ProfileListInit(FoldingSetNodeID &ID,
565493 RecTy *EltTy = cast(getType())->getElementType();
566494
567495 ProfileListInit(ID, Values, EltTy);
496 }
497
498 Init *ListInit::convertInitializerTo(RecTy *Ty) const {
499 if (auto *LRT = dyn_cast(Ty)) {
500 std::vector Elements;
501
502 // Verify that all of the elements of the list are subclasses of the
503 // appropriate class!
504 for (unsigned i = 0, e = getSize(); i != e; ++i)
505 if (Init *CI = getElement(i)->convertInitializerTo(LRT->getElementType()))
506 Elements.push_back(CI);
507 else
508 return nullptr;
509
510 if (isa(getType()))
511 return ListInit::get(Elements, Ty);
512 }
513
514 return nullptr;
568515 }
569516
570517 Init *
11471094 }
11481095
11491096 Init *
1097 TypedInit::convertInitializerTo(RecTy *Ty) const {
1098 if (isa(Ty)) {
1099 if (getType()->typeIsConvertibleTo(Ty))
1100 return const_cast(this);
1101 return nullptr;
1102 }
1103
1104 if (isa(Ty)) {
1105 if (isa(getType()))
1106 return const_cast(this);
1107 return nullptr;
1108 }
1109
1110 if (isa(Ty)) {
1111 // Accept variable if it is already of bit type!
1112 if (isa(getType()))
1113 return const_cast(this);
1114 if (auto *BitsTy = dyn_cast(getType())) {
1115 // Accept only bits<1> expression.
1116 if (BitsTy->getNumBits() == 1)
1117 return const_cast(this);
1118 return nullptr;
1119 }
1120 // Ternary !if can be converted to bit, but only if both sides are
1121 // convertible to a bit.
1122 if (const auto *TOI = dyn_cast(this)) {
1123 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1124 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1125 TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1126 return const_cast(this);
1127 return nullptr;
1128 }
1129 return nullptr;
1130 }
1131
1132 if (auto *BRT = dyn_cast(Ty)) {
1133 if (BRT->getNumBits() == 1 && isa(getType()))
1134 return BitsInit::get(const_cast(this));
1135
1136 if (getType()->typeIsConvertibleTo(BRT)) {
1137 SmallVector NewBits(BRT->getNumBits());
1138
1139 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1140 NewBits[i] = VarBitInit::get(const_cast(this), i);
1141 return BitsInit::get(NewBits);
1142 }
1143
1144 return nullptr;
1145 }
1146
1147 if (auto *DLRT = dyn_cast(Ty)) {
1148 if (auto *SLRT = dyn_cast(getType()))
1149 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1150 return const_cast(this);
1151 return nullptr;
1152 }
1153
1154 if (auto *DRT = dyn_cast(Ty)) {
1155 if (getType()->typeIsConvertibleTo(DRT))
1156 return const_cast(this);
1157 return nullptr;
1158 }
1159
1160 if (auto *SRRT = dyn_cast(Ty)) {
1161 // Ensure that this is compatible with Rec.
1162 if (RecordRecTy *DRRT = dyn_cast(getType()))
1163 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1164 DRRT->getRecord() == SRRT->getRecord())
1165 return const_cast(this);
1166 return nullptr;
1167 }
1168
1169 return nullptr;
1170 }
1171
1172 Init *
11501173 TypedInit::convertInitializerBitRange(const std::vector &Bits) const {
11511174 BitsRecTy *T = dyn_cast(getType());
11521175 if (!T) return nullptr; // Cannot subscript a non-bits variable.
12751298 return I.get();
12761299 }
12771300
1301 Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
1302 if (isa(Ty))
1303 return const_cast(this);
1304
1305 return nullptr;
1306 }
1307
12781308 std::string VarBitInit::getAsString() const {
12791309 return TI->getAsString() + "{" + utostr(Bit) + "}";
12801310 }
13361366 return R->getDefInit();
13371367 }
13381368
1369 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1370 if (auto *RRT = dyn_cast(Ty))
1371 if (getDef()->isSubClassOf(RRT->getRecord()))
1372 return const_cast(this);
1373 return nullptr;
1374 }
1375
13391376 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
13401377 if (const RecordVal *RV = Def->getValue(FieldName))
13411378 return RV->getType();
14501487
14511488 void DagInit::Profile(FoldingSetNodeID &ID) const {
14521489 ProfileDagInit(ID, Val, ValName, Args, ArgNames);
1490 }
1491
1492 Init *DagInit::convertInitializerTo(RecTy *Ty) const {
1493 if (isa(Ty))
1494 return const_cast(this);
1495
1496 return nullptr;
14531497 }
14541498
14551499 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
14881532
14891533 RecordVal::RecordVal(Init *N, RecTy *T, unsigned P)
14901534 : Name(N), Ty(T), Prefix(P) {
1491 Value = Ty->convertValue(UnsetInit::get());
1535 Value = UnsetInit::get()->convertInitializerTo(Ty);
14921536 assert(Value && "Cannot create unset value for current type!");
14931537 }
14941538
14951539 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
14961540 : Name(StringInit::get(N)), Ty(T), Prefix(P) {
1497 Value = Ty->convertValue(UnsetInit::get());
1541 Value = UnsetInit::get()->convertInitializerTo(Ty);
14981542 assert(Value && "Cannot create unset value for current type!");
14991543 }
15001544