1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #include "CodeGenDAGPatterns.h" |
16 | #include "llvm/ADT/STLExtras.h" |
17 | #include "llvm/ADT/StringExtras.h" |
18 | #include "llvm/ADT/Twine.h" |
19 | #include "llvm/Support/Debug.h" |
20 | #include "llvm/Support/ErrorHandling.h" |
21 | #include "llvm/TableGen/Error.h" |
22 | #include "llvm/TableGen/Record.h" |
23 | #include <algorithm> |
24 | #include <cstdio> |
25 | #include <set> |
26 | using namespace llvm; |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | static inline bool isInteger(MVT::SimpleValueType VT) { |
33 | return EVT(VT).isInteger(); |
34 | } |
35 | static inline bool isFloatingPoint(MVT::SimpleValueType VT) { |
36 | return EVT(VT).isFloatingPoint(); |
37 | } |
38 | static inline bool isVector(MVT::SimpleValueType VT) { |
39 | return EVT(VT).isVector(); |
40 | } |
41 | static inline bool isScalar(MVT::SimpleValueType VT) { |
42 | return !EVT(VT).isVector(); |
43 | } |
44 | |
45 | EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) { |
46 | if (VT == MVT::iAny) |
47 | EnforceInteger(TP); |
48 | else if (VT == MVT::fAny) |
49 | EnforceFloatingPoint(TP); |
50 | else if (VT == MVT::vAny) |
51 | EnforceVector(TP); |
52 | else { |
53 | assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR ||((void)0) |
54 | VT == MVT::iPTRAny) && "Not a concrete type!")((void)0); |
55 | TypeVec.push_back(VT); |
56 | } |
57 | } |
58 | |
59 | |
60 | EEVT::TypeSet::TypeSet(ArrayRef<MVT::SimpleValueType> VTList) { |
61 | assert(!VTList.empty() && "empty list?")((void)0); |
62 | TypeVec.append(VTList.begin(), VTList.end()); |
63 | |
64 | if (!VTList.empty()) |
65 | assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny &&((void)0) |
66 | VTList[0] != MVT::fAny)((void)0); |
67 | |
68 | |
69 | array_pod_sort(TypeVec.begin(), TypeVec.end()); |
70 | assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end())((void)0); |
71 | } |
72 | |
73 | |
74 | |
75 | bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP, |
76 | bool (*Pred)(MVT::SimpleValueType), |
77 | const char *PredicateName) { |
78 | assert(isCompletelyUnknown())((void)0); |
79 | ArrayRef<MVT::SimpleValueType> LegalTypes = |
80 | TP.getDAGPatterns().getTargetInfo().getLegalValueTypes(); |
81 | |
82 | if (TP.hasError()) |
83 | return false; |
84 | |
85 | for (unsigned i = 0, e = LegalTypes.size(); i != e; ++i) |
86 | if (Pred == 0 || Pred(LegalTypes[i])) |
87 | TypeVec.push_back(LegalTypes[i]); |
88 | |
89 | |
90 | if (TypeVec.empty()) { |
91 | TP.error("Type inference contradiction found, no " + |
92 | std::string(PredicateName) + " types found"); |
93 | return false; |
94 | } |
95 | |
96 | if (TypeVec.size() == 1) return true; |
97 | |
98 | |
99 | array_pod_sort(TypeVec.begin(), TypeVec.end()); |
100 | TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end()); |
101 | |
102 | return true; |
103 | } |
104 | |
105 | |
106 | |
107 | bool EEVT::TypeSet::hasIntegerTypes() const { |
108 | for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) |
109 | if (isInteger(TypeVec[i])) |
110 | return true; |
111 | return false; |
112 | } |
113 | |
114 | |
115 | |
116 | bool EEVT::TypeSet::hasFloatingPointTypes() const { |
117 | for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) |
118 | if (isFloatingPoint(TypeVec[i])) |
119 | return true; |
120 | return false; |
121 | } |
122 | |
123 | |
124 | |
125 | bool EEVT::TypeSet::hasVectorTypes() const { |
126 | for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) |
127 | if (isVector(TypeVec[i])) |
128 | return true; |
129 | return false; |
130 | } |
131 | |
132 | |
133 | std::string EEVT::TypeSet::getName() const { |
134 | if (TypeVec.empty()) return "<empty>"; |
135 | |
136 | std::string Result; |
137 | |
138 | for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) { |
139 | std::string VTName = llvm::getEnumName(TypeVec[i]); |
140 | |
141 | if (VTName.substr(0,5) == "MVT::") |
142 | VTName = VTName.substr(5); |
143 | if (i) Result += ':'; |
144 | Result += VTName; |
145 | } |
146 | |
147 | if (TypeVec.size() == 1) |
148 | return Result; |
149 | return "{" + Result + "}"; |
150 | } |
151 | |
152 | |
153 | |
154 | |
155 | bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){ |
156 | if (InVT.isCompletelyUnknown() || *this == InVT || TP.hasError()) |
157 | return false; |
158 | |
159 | if (isCompletelyUnknown()) { |
160 | *this = InVT; |
161 | return true; |
162 | } |
163 | |
164 | assert(TypeVec.size() >= 1 && InVT.TypeVec.size() >= 1 && "No unknowns")((void)0); |
165 | |
166 | |
167 | switch (TypeVec[0]) { |
168 | default: break; |
169 | case MVT::iPTR: |
170 | case MVT::iPTRAny: |
171 | if (InVT.hasIntegerTypes()) { |
172 | EEVT::TypeSet InCopy(InVT); |
173 | InCopy.EnforceInteger(TP); |
174 | InCopy.EnforceScalar(TP); |
175 | |
176 | if (InCopy.isConcrete()) { |
177 | |
178 | TypeVec[0] = InVT.TypeVec[0]; |
179 | return true; |
180 | } |
181 | |
182 | |
183 | if (!InCopy.isCompletelyUnknown()) |
184 | return false; |
185 | } |
186 | break; |
187 | } |
188 | |
189 | |
190 | |
191 | if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && |
192 | hasIntegerTypes()) { |
193 | bool MadeChange = EnforceInteger(TP); |
194 | |
195 | |
196 | |
197 | if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && |
198 | TypeVec.size() != 1) { |
199 | TypeVec.resize(1); |
200 | TypeVec[0] = InVT.TypeVec[0]; |
201 | MadeChange = true; |
202 | } |
203 | |
204 | return MadeChange; |
205 | } |
206 | |
207 | |
208 | |
209 | bool MadeChange = false; |
210 | TypeSet InputSet(*this); |
211 | |
212 | for (unsigned i = 0; i != TypeVec.size(); ++i) { |
213 | bool InInVT = false; |
214 | for (unsigned j = 0, e = InVT.TypeVec.size(); j != e; ++j) |
215 | if (TypeVec[i] == InVT.TypeVec[j]) { |
216 | InInVT = true; |
217 | break; |
218 | } |
219 | |
220 | if (InInVT) continue; |
221 | TypeVec.erase(TypeVec.begin()+i--); |
222 | MadeChange = true; |
223 | } |
224 | |
225 | |
226 | if (!TypeVec.empty()) |
227 | return MadeChange; |
228 | |
229 | |
230 | TP.error("Type inference contradiction found, merging '" + |
231 | InVT.getName() + "' into '" + InputSet.getName() + "'"); |
232 | return false; |
233 | } |
234 | |
235 | |
236 | bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) { |
237 | if (TP.hasError()) |
238 | return false; |
239 | |
240 | if (TypeVec.empty()) |
241 | return FillWithPossibleTypes(TP, isInteger, "integer"); |
242 | if (!hasFloatingPointTypes()) |
243 | return false; |
244 | |
245 | TypeSet InputSet(*this); |
246 | |
247 | |
248 | for (unsigned i = 0; i != TypeVec.size(); ++i) |
249 | if (!isInteger(TypeVec[i])) |
250 | TypeVec.erase(TypeVec.begin()+i--); |
251 | |
252 | if (TypeVec.empty()) { |
253 | TP.error("Type inference contradiction found, '" + |
254 | InputSet.getName() + "' needs to be integer"); |
255 | return false; |
256 | } |
257 | return true; |
258 | } |
259 | |
260 | |
261 | bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) { |
262 | if (TP.hasError()) |
263 | return false; |
264 | |
265 | if (TypeVec.empty()) |
266 | return FillWithPossibleTypes(TP, isFloatingPoint, "floating point"); |
267 | |
268 | if (!hasIntegerTypes()) |
269 | return false; |
270 | |
271 | TypeSet InputSet(*this); |
272 | |
273 | |
274 | for (unsigned i = 0; i != TypeVec.size(); ++i) |
275 | if (!isFloatingPoint(TypeVec[i])) |
276 | TypeVec.erase(TypeVec.begin()+i--); |
277 | |
278 | if (TypeVec.empty()) { |
279 | TP.error("Type inference contradiction found, '" + |
280 | InputSet.getName() + "' needs to be floating point"); |
281 | return false; |
282 | } |
283 | return true; |
284 | } |
285 | |
286 | |
287 | bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) { |
288 | if (TP.hasError()) |
289 | return false; |
290 | |
291 | |
292 | if (TypeVec.empty()) |
293 | return FillWithPossibleTypes(TP, isScalar, "scalar"); |
294 | |
295 | if (!hasVectorTypes()) |
296 | return false; |
297 | |
298 | TypeSet InputSet(*this); |
299 | |
300 | |
301 | for (unsigned i = 0; i != TypeVec.size(); ++i) |
302 | if (!isScalar(TypeVec[i])) |
303 | TypeVec.erase(TypeVec.begin()+i--); |
304 | |
305 | if (TypeVec.empty()) { |
306 | TP.error("Type inference contradiction found, '" + |
307 | InputSet.getName() + "' needs to be scalar"); |
308 | return false; |
309 | } |
310 | return true; |
311 | } |
312 | |
313 | |
314 | bool EEVT::TypeSet::EnforceVector(TreePattern &TP) { |
315 | if (TP.hasError()) |
316 | return false; |
317 | |
318 | |
319 | if (TypeVec.empty()) |
320 | return FillWithPossibleTypes(TP, isVector, "vector"); |
321 | |
322 | TypeSet InputSet(*this); |
323 | bool MadeChange = false; |
324 | |
325 | |
326 | for (unsigned i = 0; i != TypeVec.size(); ++i) |
327 | if (!isVector(TypeVec[i])) { |
328 | TypeVec.erase(TypeVec.begin()+i--); |
329 | MadeChange = true; |
330 | } |
331 | |
332 | if (TypeVec.empty()) { |
333 | TP.error("Type inference contradiction found, '" + |
334 | InputSet.getName() + "' needs to be a vector"); |
335 | return false; |
336 | } |
337 | return MadeChange; |
338 | } |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) { |
345 | if (TP.hasError()) |
346 | return false; |
347 | |
348 | |
349 | bool MadeChange = false; |
350 | |
351 | if (isCompletelyUnknown()) |
352 | MadeChange = FillWithPossibleTypes(TP); |
353 | |
354 | if (Other.isCompletelyUnknown()) |
355 | MadeChange = Other.FillWithPossibleTypes(TP); |
356 | |
357 | |
358 | |
359 | if (!hasFloatingPointTypes()) |
360 | MadeChange |= Other.EnforceInteger(TP); |
361 | else if (!hasIntegerTypes()) |
362 | MadeChange |= Other.EnforceFloatingPoint(TP); |
363 | if (!Other.hasFloatingPointTypes()) |
364 | MadeChange |= EnforceInteger(TP); |
365 | else if (!Other.hasIntegerTypes()) |
366 | MadeChange |= EnforceFloatingPoint(TP); |
367 | |
368 | assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() &&((void)0) |
369 | "Should have a type list now")((void)0); |
370 | |
371 | |
372 | if (!hasVectorTypes()) |
373 | MadeChange |= Other.EnforceScalar(TP); |
374 | if (!hasVectorTypes()) |
375 | MadeChange |= EnforceScalar(TP); |
376 | |
377 | if (TypeVec.size() == 1 && Other.TypeVec.size() == 1) { |
378 | |
379 | |
380 | |
381 | assert(!(hasIntegerTypes() && hasFloatingPointTypes()) &&((void)0) |
382 | !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) &&((void)0) |
383 | "SDTCisOpSmallerThanOp does not handle mixed int/fp types!")((void)0); |
384 | |
385 | |
386 | |
387 | |
388 | EVT Type(TypeVec[0]); |
389 | EVT OtherType(Other.TypeVec[0]); |
390 | |
391 | if (hasVectorTypes() && Other.hasVectorTypes()) { |
392 | if (Type.getSizeInBits() >= OtherType.getSizeInBits()) |
393 | if (Type.getVectorElementType().getSizeInBits() |
394 | >= OtherType.getVectorElementType().getSizeInBits()) { |
395 | TP.error("Type inference contradiction found, '" + |
396 | getName() + "' element type not smaller than '" + |
397 | Other.getName() +"'!"); |
398 | return false; |
399 | } |
400 | } |
401 | else |
402 | |
403 | |
404 | if (Type.getSizeInBits() >= OtherType.getSizeInBits()) { |
405 | TP.error("Type inference contradiction found, '" + |
406 | getName() + "' is not smaller than '" + |
407 | Other.getName() +"'!"); |
408 | return false; |
409 | } |
410 | } |
411 | |
412 | |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | MVT::SimpleValueType SmallestInt = MVT::LAST_VALUETYPE; |
420 | for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) |
421 | if (isInteger(TypeVec[i])) { |
422 | SmallestInt = TypeVec[i]; |
423 | break; |
424 | } |
425 | for (unsigned i = 1, e = TypeVec.size(); i != e; ++i) |
426 | if (isInteger(TypeVec[i]) && TypeVec[i] < SmallestInt) |
427 | SmallestInt = TypeVec[i]; |
428 | |
429 | MVT::SimpleValueType SmallestFP = MVT::LAST_VALUETYPE; |
430 | for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) |
431 | if (isFloatingPoint(TypeVec[i])) { |
432 | SmallestFP = TypeVec[i]; |
433 | break; |
434 | } |
435 | for (unsigned i = 1, e = TypeVec.size(); i != e; ++i) |
436 | if (isFloatingPoint(TypeVec[i]) && TypeVec[i] < SmallestFP) |
437 | SmallestFP = TypeVec[i]; |
438 | |
439 | int OtherIntSize = 0; |
440 | int OtherFPSize = 0; |
441 | for (SmallVector<MVT::SimpleValueType, 2>::iterator TVI = |
442 | Other.TypeVec.begin(); |
443 | TVI != Other.TypeVec.end(); |
444 | ) { |
445 | if (isInteger(*TVI)) { |
446 | ++OtherIntSize; |
447 | if (*TVI == SmallestInt) { |
448 | TVI = Other.TypeVec.erase(TVI); |
449 | --OtherIntSize; |
450 | MadeChange = true; |
451 | continue; |
452 | } |
453 | } |
454 | else if (isFloatingPoint(*TVI)) { |
455 | ++OtherFPSize; |
456 | if (*TVI == SmallestFP) { |
457 | TVI = Other.TypeVec.erase(TVI); |
458 | --OtherFPSize; |
459 | MadeChange = true; |
460 | continue; |
461 | } |
462 | } |
463 | ++TVI; |
464 | } |
465 | |
466 | |
467 | |
468 | if ((Other.hasIntegerTypes() && OtherIntSize == 0) |
469 | || (Other.hasFloatingPointTypes() && OtherFPSize == 0)) { |
470 | TP.error("Type inference contradiction found, '" + |
471 | Other.getName() + "' has nothing larger than '" + getName() +"'!"); |
472 | return false; |
473 | } |
474 | |
475 | |
476 | |
477 | MVT::SimpleValueType LargestInt = MVT::Other; |
478 | for (unsigned i = 0, e = Other.TypeVec.size(); i != e; ++i) |
479 | if (isInteger(Other.TypeVec[i])) { |
480 | LargestInt = Other.TypeVec[i]; |
481 | break; |
482 | } |
483 | for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i) |
484 | if (isInteger(Other.TypeVec[i]) && Other.TypeVec[i] > LargestInt) |
485 | LargestInt = Other.TypeVec[i]; |
486 | |
487 | MVT::SimpleValueType LargestFP = MVT::Other; |
488 | for (unsigned i = 0, e = Other.TypeVec.size(); i != e; ++i) |
489 | if (isFloatingPoint(Other.TypeVec[i])) { |
490 | LargestFP = Other.TypeVec[i]; |
491 | break; |
492 | } |
493 | for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i) |
494 | if (isFloatingPoint(Other.TypeVec[i]) && Other.TypeVec[i] > LargestFP) |
495 | LargestFP = Other.TypeVec[i]; |
496 | |
497 | int IntSize = 0; |
498 | int FPSize = 0; |
499 | for (SmallVector<MVT::SimpleValueType, 2>::iterator TVI = |
500 | TypeVec.begin(); |
501 | TVI != TypeVec.end(); |
502 | ) { |
503 | if (isInteger(*TVI)) { |
504 | ++IntSize; |
505 | if (*TVI == LargestInt) { |
506 | TVI = TypeVec.erase(TVI); |
507 | --IntSize; |
508 | MadeChange = true; |
509 | continue; |
510 | } |
511 | } |
512 | else if (isFloatingPoint(*TVI)) { |
513 | ++FPSize; |
514 | if (*TVI == LargestFP) { |
515 | TVI = TypeVec.erase(TVI); |
516 | --FPSize; |
517 | MadeChange = true; |
518 | continue; |
519 | } |
520 | } |
521 | ++TVI; |
522 | } |
523 | |
524 | |
525 | |
526 | if ((hasIntegerTypes() && IntSize == 0) |
527 | || (hasFloatingPointTypes() && FPSize == 0)) { |
528 | TP.error("Type inference contradiction found, '" + |
529 | getName() + "' has nothing smaller than '" + Other.getName()+"'!"); |
530 | return false; |
531 | } |
532 | |
533 | return MadeChange; |
534 | } |
535 | |
536 | |
537 | |
538 | bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand, |
539 | TreePattern &TP) { |
540 | if (TP.hasError()) |
541 | return false; |
542 | |
543 | |
544 | bool MadeChange = false; |
545 | MadeChange |= EnforceVector(TP); |
546 | MadeChange |= VTOperand.EnforceScalar(TP); |
547 | |
548 | |
549 | if (isConcrete()) { |
550 | EVT IVT = getConcrete(); |
551 | IVT = IVT.getVectorElementType(); |
552 | return MadeChange | |
553 | VTOperand.MergeInTypeInfo(IVT.getSimpleVT().SimpleTy, TP); |
554 | } |
555 | |
556 | |
557 | |
558 | if (!VTOperand.isConcrete()) |
559 | return MadeChange; |
560 | |
561 | MVT::SimpleValueType VT = VTOperand.getConcrete(); |
562 | |
563 | TypeSet InputSet(*this); |
564 | |
565 | |
566 | for (unsigned i = 0; i != TypeVec.size(); ++i) { |
567 | assert(isVector(TypeVec[i]) && "EnforceVector didn't work")((void)0); |
568 | if (EVT(TypeVec[i]).getVectorElementType().getSimpleVT().SimpleTy != VT) { |
569 | TypeVec.erase(TypeVec.begin()+i--); |
570 | MadeChange = true; |
571 | } |
572 | } |
573 | |
574 | if (TypeVec.empty()) { |
575 | TP.error("Type inference contradiction found, forcing '" + |
576 | InputSet.getName() + "' to have a vector element"); |
577 | return false; |
578 | } |
579 | return MadeChange; |
580 | } |
581 | |
582 | |
583 | |
584 | bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand, |
585 | TreePattern &TP) { |
586 | |
587 | bool MadeChange = false; |
588 | MadeChange |= EnforceVector(TP); |
589 | MadeChange |= VTOperand.EnforceVector(TP); |
590 | |
591 | |
592 | MadeChange |= VTOperand.EnforceSmallerThan(*this, TP); |
593 | |
594 | |
595 | if (isConcrete()) { |
596 | EVT IVT = getConcrete(); |
597 | IVT = IVT.getVectorElementType(); |
598 | |
599 | EEVT::TypeSet EltTypeSet(IVT.getSimpleVT().SimpleTy, TP); |
600 | MadeChange |= VTOperand.EnforceVectorEltTypeIs(EltTypeSet, TP); |
601 | } else if (VTOperand.isConcrete()) { |
602 | EVT IVT = VTOperand.getConcrete(); |
603 | IVT = IVT.getVectorElementType(); |
604 | |
605 | EEVT::TypeSet EltTypeSet(IVT.getSimpleVT().SimpleTy, TP); |
606 | MadeChange |= EnforceVectorEltTypeIs(EltTypeSet, TP); |
607 | } |
608 | |
609 | return MadeChange; |
610 | } |
611 | |
612 | |
613 | |
614 | |
615 | |
616 | typedef std::map<std::string, int> DepVarMap; |
617 | |
618 | |
619 | typedef DepVarMap::const_iterator DepVarMap_citer; |
620 | |
621 | static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) { |
622 | if (N->isLeaf()) { |
623 | if (isa<DefInit>(N->getLeafValue())) |
624 | DepMap[N->getName()]++; |
625 | } else { |
626 | for (size_t i = 0, e = N->getNumChildren(); i != e; ++i) |
627 | FindDepVarsOf(N->getChild(i), DepMap); |
628 | } |
629 | } |
630 | |
631 | |
632 | static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) { |
633 | DepVarMap depcounts; |
634 | FindDepVarsOf(N, depcounts); |
635 | for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) { |
636 | if (i->second > 1) |
637 | DepVars.insert(i->first); |
638 | } |
639 | } |
640 | |
641 | #ifndef NDEBUG1 |
642 | |
643 | static void DumpDepVars(MultipleUseVarSet &DepVars) { |
644 | if (DepVars.empty()) { |
645 | DEBUG(errs() << "<empty set>")do { } while (0); |
646 | } else { |
647 | DEBUG(errs() << "[ ")do { } while (0); |
648 | for (MultipleUseVarSet::const_iterator i = DepVars.begin(), |
649 | e = DepVars.end(); i != e; ++i) { |
650 | DEBUG(errs() << (*i) << " ")do { } while (0); |
651 | } |
652 | DEBUG(errs() << "]")do { } while (0); |
653 | } |
654 | } |
655 | #endif |
656 | |
657 | |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) { |
664 | assert((getPredCode().empty() || getImmCode().empty()) &&((void)0) |
665 | ".td file corrupt: can't have a node predicate *and* an imm predicate")((void)0); |
666 | } |
667 | |
668 | std::string TreePredicateFn::getPredCode() const { |
669 | return PatFragRec->getRecord()->getValueAsString("PredicateCode"); |
670 | } |
671 | |
672 | std::string TreePredicateFn::getImmCode() const { |
673 | return PatFragRec->getRecord()->getValueAsString("ImmediateCode"); |
674 | } |
675 | |
676 | |
677 | |
678 | bool TreePredicateFn::isAlwaysTrue() const { |
679 | return getPredCode().empty() && getImmCode().empty(); |
680 | } |
681 | |
682 | |
683 | |
684 | std::string TreePredicateFn::getFnName() const { |
685 | return "Predicate_" + PatFragRec->getRecord()->getName(); |
686 | } |
687 | |
688 | |
689 | |
690 | |
691 | |
692 | std::string TreePredicateFn::getCodeToRunOnSDNode() const { |
693 | |
694 | std::string ImmCode = getImmCode(); |
695 | if (!ImmCode.empty()) { |
696 | std::string Result = |
697 | " int64_t Imm = cast<ConstantSDNode>(Node)->getSExtValue();\n"; |
698 | return Result + ImmCode; |
699 | } |
700 | |
701 | |
702 | assert(!getPredCode().empty() && "Don't have any predicate code!")((void)0); |
703 | std::string ClassName; |
704 | if (PatFragRec->getOnlyTree()->isLeaf()) |
705 | ClassName = "SDNode"; |
706 | else { |
707 | Record *Op = PatFragRec->getOnlyTree()->getOperator(); |
708 | ClassName = PatFragRec->getDAGPatterns().getSDNodeInfo(Op).getSDClassName(); |
709 | } |
710 | std::string Result; |
711 | if (ClassName == "SDNode") |
712 | Result = " SDNode *N = Node;\n"; |
713 | else |
714 | Result = " " + ClassName + "*N = cast<" + ClassName + ">(Node);\n"; |
715 | |
716 | return Result + getPredCode(); |
717 | } |
718 | |
719 | |
720 | |
721 | |
722 | |
723 | |
724 | |
725 | |
726 | |
727 | static unsigned getPatternSize(const TreePatternNode *P, |
728 | const CodeGenDAGPatterns &CGP) { |
729 | unsigned Size = 3; |
730 | |
731 | |
732 | if (P->isLeaf() && isa<IntInit>(P->getLeafValue())) |
733 | Size += 2; |
734 | |
735 | |
736 | |
737 | |
738 | |
739 | |
740 | const ComplexPattern *AM = P->getComplexPatternInfo(CGP); |
741 | if (AM) |
742 | Size += AM->getNumOperands() * 3; |
743 | |
744 | |
745 | |
746 | if (!P->getPredicateFns().empty()) |
747 | ++Size; |
748 | |
749 | |
750 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { |
751 | TreePatternNode *Child = P->getChild(i); |
752 | if (!Child->isLeaf() && Child->getNumTypes() && |
753 | Child->getType(0) != MVT::Other) |
754 | Size += getPatternSize(Child, CGP); |
755 | else if (Child->isLeaf()) { |
756 | if (isa<IntInit>(Child->getLeafValue())) |
757 | Size += 5; |
758 | else if (Child->getComplexPatternInfo(CGP)) |
759 | Size += getPatternSize(Child, CGP); |
760 | else if (!Child->getPredicateFns().empty()) |
761 | ++Size; |
762 | } |
763 | } |
764 | |
765 | return Size; |
766 | } |
767 | |
768 | |
769 | |
770 | unsigned PatternToMatch:: |
771 | getPatternComplexity(const CodeGenDAGPatterns &CGP) const { |
772 | return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity(); |
773 | } |
774 | |
775 | |
776 | |
777 | |
778 | |
779 | std::string PatternToMatch::getPredicateCheck() const { |
780 | std::string PredicateCheck; |
781 | for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { |
782 | if (DefInit *Pred = dyn_cast<DefInit>(Predicates->getElement(i))) { |
783 | Record *Def = Pred->getDef(); |
784 | if (!Def->isSubClassOf("Predicate")) { |
785 | #ifndef NDEBUG1 |
786 | Def->dump(); |
787 | #endif |
788 | llvm_unreachable("Unknown predicate type!")__builtin_unreachable(); |
789 | } |
790 | if (!PredicateCheck.empty()) |
791 | PredicateCheck += " && "; |
792 | PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; |
793 | } |
794 | } |
795 | |
796 | return PredicateCheck; |
797 | } |
798 | |
799 | |
800 | |
801 | |
802 | |
803 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
804 | OperandNo = R->getValueAsInt("OperandNum"); |
805 | |
806 | if (R->isSubClassOf("SDTCisVT")) { |
807 | ConstraintType = SDTCisVT; |
808 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
809 | if (x.SDTCisVT_Info.VT == MVT::isVoid) |
810 | PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT"); |
811 | |
812 | } else if (R->isSubClassOf("SDTCisPtrTy")) { |
813 | ConstraintType = SDTCisPtrTy; |
814 | } else if (R->isSubClassOf("SDTCisInt")) { |
815 | ConstraintType = SDTCisInt; |
816 | } else if (R->isSubClassOf("SDTCisFP")) { |
817 | ConstraintType = SDTCisFP; |
818 | } else if (R->isSubClassOf("SDTCisVec")) { |
819 | ConstraintType = SDTCisVec; |
820 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
821 | ConstraintType = SDTCisSameAs; |
822 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
823 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
824 | ConstraintType = SDTCisVTSmallerThanOp; |
825 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
826 | R->getValueAsInt("OtherOperandNum"); |
827 | } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { |
828 | ConstraintType = SDTCisOpSmallerThanOp; |
829 | x.SDTCisOpSmallerThanOp_Info.BigOperandNum = |
830 | R->getValueAsInt("BigOperandNum"); |
831 | } else if (R->isSubClassOf("SDTCisEltOfVec")) { |
832 | ConstraintType = SDTCisEltOfVec; |
833 | x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum"); |
834 | } else if (R->isSubClassOf("SDTCisSubVecOfVec")) { |
835 | ConstraintType = SDTCisSubVecOfVec; |
836 | x.SDTCisSubVecOfVec_Info.OtherOperandNum = |
837 | R->getValueAsInt("OtherOpNum"); |
838 | } else { |
839 | errs() << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
840 | exit(1); |
841 | } |
842 | } |
843 | |
844 | |
845 | |
846 | static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N, |
847 | const SDNodeInfo &NodeInfo, |
848 | unsigned &ResNo) { |
849 | unsigned NumResults = NodeInfo.getNumResults(); |
850 | if (OpNo < NumResults) { |
851 | ResNo = OpNo; |
852 | return N; |
853 | } |
854 | |
855 | OpNo -= NumResults; |
856 | |
857 | if (OpNo >= N->getNumChildren()) { |
858 | errs() << "Invalid operand number in type constraint " |
859 | << (OpNo+NumResults) << " "; |
860 | N->dump(); |
861 | errs() << '\n'; |
862 | exit(1); |
863 | } |
864 | |
865 | return N->getChild(OpNo); |
866 | } |
867 | |
868 | |
869 | |
870 | |
871 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
872 | const SDNodeInfo &NodeInfo, |
873 | TreePattern &TP) const { |
874 | if (TP.hasError()) |
875 | return false; |
876 | |
877 | unsigned ResNo = 0; |
878 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo); |
879 | |
880 | switch (ConstraintType) { |
881 | case SDTCisVT: |
882 | |
883 | return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP); |
884 | case SDTCisPtrTy: |
885 | |
886 | return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP); |
887 | case SDTCisInt: |
888 | |
889 | return NodeToApply->getExtType(ResNo).EnforceInteger(TP); |
890 | case SDTCisFP: |
891 | |
892 | return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP); |
893 | case SDTCisVec: |
894 | |
895 | return NodeToApply->getExtType(ResNo).EnforceVector(TP); |
896 | case SDTCisSameAs: { |
897 | unsigned OResNo = 0; |
898 | TreePatternNode *OtherNode = |
899 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo); |
900 | return NodeToApply->UpdateNodeType(OResNo, OtherNode->getExtType(ResNo),TP)| |
901 | OtherNode->UpdateNodeType(ResNo,NodeToApply->getExtType(OResNo),TP); |
902 | } |
903 | case SDTCisVTSmallerThanOp: { |
904 | |
905 | |
906 | if (!NodeToApply->isLeaf() || |
907 | !isa<DefInit>(NodeToApply->getLeafValue()) || |
908 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
909 | ->isSubClassOf("ValueType")) { |
910 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
911 | return false; |
912 | } |
913 | MVT::SimpleValueType VT = |
914 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
915 | |
916 | EEVT::TypeSet TypeListTmp(VT, TP); |
917 | |
918 | unsigned OResNo = 0; |
919 | TreePatternNode *OtherNode = |
920 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo, |
921 | OResNo); |
922 | |
923 | return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP); |
924 | } |
925 | case SDTCisOpSmallerThanOp: { |
926 | unsigned BResNo = 0; |
927 | TreePatternNode *BigOperand = |
928 | getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo, |
929 | BResNo); |
930 | return NodeToApply->getExtType(ResNo). |
931 | EnforceSmallerThan(BigOperand->getExtType(BResNo), TP); |
932 | } |
933 | case SDTCisEltOfVec: { |
934 | unsigned VResNo = 0; |
935 | TreePatternNode *VecOperand = |
936 | getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo, |
937 | VResNo); |
938 | |
939 | |
940 | |
941 | return VecOperand->getExtType(VResNo). |
942 | EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP); |
943 | } |
944 | case SDTCisSubVecOfVec: { |
945 | unsigned VResNo = 0; |
946 | TreePatternNode *BigVecOperand = |
947 | getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo, |
948 | VResNo); |
949 | |
950 | |
951 | |
952 | return BigVecOperand->getExtType(VResNo). |
953 | EnforceVectorSubVectorTypeIs(NodeToApply->getExtType(ResNo), TP); |
954 | } |
955 | } |
956 | llvm_unreachable("Invalid ConstraintType!")__builtin_unreachable(); |
957 | } |
958 | |
959 | |
960 | |
961 | |
962 | bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo, |
963 | Record *Operand, |
964 | TreePattern &TP) { |
965 | |
966 | |
967 | if (Operand->isSubClassOf("unknown_class")) |
968 | return false; |
969 | |
970 | |
971 | if (Operand->isSubClassOf("Operand")) |
972 | return UpdateNodeType(ResNo, getValueType(Operand->getValueAsDef("Type")), |
973 | TP); |
974 | |
975 | |
976 | if (Operand->isSubClassOf("PointerLikeRegClass")) |
977 | return UpdateNodeType(ResNo, MVT::iPTR, TP); |
978 | |
979 | |
980 | |
981 | Record *RC = 0; |
982 | if (Operand->isSubClassOf("RegisterClass")) |
983 | RC = Operand; |
984 | else if (Operand->isSubClassOf("RegisterOperand")) |
985 | RC = Operand->getValueAsDef("RegClass"); |
986 | |
987 | assert(RC && "Unknown operand type")((void)0); |
988 | CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo(); |
989 | return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP); |
990 | } |
991 | |
992 | |
993 | |
994 | |
995 | |
996 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
997 | EnumName = R->getValueAsString("Opcode"); |
998 | SDClassName = R->getValueAsString("SDClass"); |
999 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
1000 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
1001 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
1002 | |
1003 | |
1004 | Properties = 0; |
1005 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
1006 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) { |
1007 | if (PropList[i]->getName() == "SDNPCommutative") { |
1008 | Properties |= 1 << SDNPCommutative; |
1009 | } else if (PropList[i]->getName() == "SDNPAssociative") { |
1010 | Properties |= 1 << SDNPAssociative; |
1011 | } else if (PropList[i]->getName() == "SDNPHasChain") { |
1012 | Properties |= 1 << SDNPHasChain; |
1013 | } else if (PropList[i]->getName() == "SDNPOutGlue") { |
1014 | Properties |= 1 << SDNPOutGlue; |
1015 | } else if (PropList[i]->getName() == "SDNPInGlue") { |
1016 | Properties |= 1 << SDNPInGlue; |
1017 | } else if (PropList[i]->getName() == "SDNPOptInGlue") { |
1018 | Properties |= 1 << SDNPOptInGlue; |
1019 | } else if (PropList[i]->getName() == "SDNPMayStore") { |
1020 | Properties |= 1 << SDNPMayStore; |
1021 | } else if (PropList[i]->getName() == "SDNPMayLoad") { |
1022 | Properties |= 1 << SDNPMayLoad; |
1023 | } else if (PropList[i]->getName() == "SDNPSideEffect") { |
1024 | Properties |= 1 << SDNPSideEffect; |
1025 | } else if (PropList[i]->getName() == "SDNPMemOperand") { |
1026 | Properties |= 1 << SDNPMemOperand; |
1027 | } else if (PropList[i]->getName() == "SDNPVariadic") { |
1028 | Properties |= 1 << SDNPVariadic; |
1029 | } else { |
1030 | errs() << "Unknown SD Node property '" << PropList[i]->getName() |
1031 | << "' on node '" << R->getName() << "'!\n"; |
1032 | exit(1); |
1033 | } |
1034 | } |
1035 | |
1036 | |
1037 | |
1038 | std::vector<Record*> ConstraintList = |
1039 | TypeProfile->getValueAsListOfDefs("Constraints"); |
1040 | TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); |
1041 | } |
1042 | |
1043 | |
1044 | |
1045 | |
1046 | MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const { |
1047 | unsigned NumResults = getNumResults(); |
1048 | assert(NumResults <= 1 &&((void)0) |
1049 | "We only work with nodes with zero or one result so far!")((void)0); |
1050 | assert(ResNo == 0 && "Only handles single result nodes so far")((void)0); |
1051 | |
1052 | for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) { |
1053 | |
1054 | if (TypeConstraints[i].OperandNo >= NumResults) |
1055 | continue; |
1056 | |
1057 | switch (TypeConstraints[i].ConstraintType) { |
1058 | default: break; |
1059 | case SDTypeConstraint::SDTCisVT: |
1060 | return TypeConstraints[i].x.SDTCisVT_Info.VT; |
1061 | case SDTypeConstraint::SDTCisPtrTy: |
1062 | return MVT::iPTR; |
1063 | } |
1064 | } |
1065 | return MVT::Other; |
1066 | } |
1067 | |
1068 | |
1069 | |
1070 | |
1071 | |
1072 | TreePatternNode::~TreePatternNode() { |
1073 | #if 0 // FIXME: implement refcounted tree nodes! |
1074 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1075 | delete getChild(i); |
1076 | #endif |
1077 | } |
1078 | |
1079 | static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) { |
1080 | if (Operator->getName() == "set" || |
1081 | Operator->getName() == "implicit") |
1082 | return 0; |
1083 | |
1084 | if (Operator->isSubClassOf("Intrinsic")) |
1085 | return CDP.getIntrinsic(Operator).IS.RetVTs.size(); |
1086 | |
1087 | if (Operator->isSubClassOf("SDNode")) |
1088 | return CDP.getSDNodeInfo(Operator).getNumResults(); |
1089 | |
1090 | if (Operator->isSubClassOf("PatFrag")) { |
1091 | |
1092 | |
1093 | |
1094 | if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) |
1095 | return PFRec->getOnlyTree()->getNumTypes(); |
1096 | |
1097 | |
1098 | DagInit *Tree = Operator->getValueAsDag("Fragment"); |
1099 | Record *Op = 0; |
1100 | if (Tree) |
1101 | if (DefInit *DI = dyn_cast<DefInit>(Tree->getOperator())) |
1102 | Op = DI->getDef(); |
1103 | assert(Op && "Invalid Fragment")((void)0); |
1104 | return GetNumNodeResults(Op, CDP); |
1105 | } |
1106 | |
1107 | if (Operator->isSubClassOf("Instruction")) { |
1108 | CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator); |
1109 | |
1110 | |
1111 | unsigned NumDefsToAdd = InstInfo.Operands.NumDefs ? 1 : 0; |
1112 | |
1113 | |
1114 | if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other) |
1115 | ++NumDefsToAdd; |
1116 | return NumDefsToAdd; |
1117 | } |
1118 | |
1119 | if (Operator->isSubClassOf("SDNodeXForm")) |
1120 | return 1; |
1121 | |
1122 | Operator->dump(); |
1123 | errs() << "Unhandled node in GetNumNodeResults\n"; |
1124 | exit(1); |
1125 | } |
1126 | |
1127 | void TreePatternNode::print(raw_ostream &OS) const { |
1128 | if (isLeaf()) |
1129 | OS << *getLeafValue(); |
1130 | else |
1131 | OS << '(' << getOperator()->getName(); |
1132 | |
1133 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
1134 | OS << ':' << getExtType(i).getName(); |
1135 | |
1136 | if (!isLeaf()) { |
1137 | if (getNumChildren() != 0) { |
1138 | OS << " "; |
1139 | getChild(0)->print(OS); |
1140 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
1141 | OS << ", "; |
1142 | getChild(i)->print(OS); |
1143 | } |
1144 | } |
1145 | OS << ")"; |
1146 | } |
1147 | |
1148 | for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i) |
1149 | OS << "<<P:" << PredicateFns[i].getFnName() << ">>"; |
1150 | if (TransformFn) |
1151 | OS << "<<X:" << TransformFn->getName() << ">>"; |
1152 | if (!getName().empty()) |
1153 | OS << ":$" << getName(); |
1154 | |
1155 | } |
1156 | void TreePatternNode::dump() const { |
1157 | print(errs()); |
1158 | } |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, |
1168 | const MultipleUseVarSet &DepVars) const { |
1169 | if (N == this) return true; |
1170 | if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || |
1171 | getPredicateFns() != N->getPredicateFns() || |
1172 | getTransformFn() != N->getTransformFn()) |
1173 | return false; |
1174 | |
1175 | if (isLeaf()) { |
1176 | if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { |
1177 | if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) { |
1178 | return ((DI->getDef() == NDI->getDef()) |
1179 | && (DepVars.find(getName()) == DepVars.end() |
1180 | || getName() == N->getName())); |
1181 | } |
1182 | } |
1183 | return getLeafValue() == N->getLeafValue(); |
1184 | } |
1185 | |
1186 | if (N->getOperator() != getOperator() || |
1187 | N->getNumChildren() != getNumChildren()) return false; |
1188 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1189 | if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars)) |
1190 | return false; |
1191 | return true; |
1192 | } |
1193 | |
1194 | |
1195 | |
1196 | TreePatternNode *TreePatternNode::clone() const { |
1197 | TreePatternNode *New; |
1198 | if (isLeaf()) { |
1199 | New = new TreePatternNode(getLeafValue(), getNumTypes()); |
1200 | } else { |
1201 | std::vector<TreePatternNode*> CChildren; |
1202 | CChildren.reserve(Children.size()); |
1203 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1204 | CChildren.push_back(getChild(i)->clone()); |
1205 | New = new TreePatternNode(getOperator(), CChildren, getNumTypes()); |
1206 | } |
1207 | New->setName(getName()); |
1208 | New->Types = Types; |
1209 | New->setPredicateFns(getPredicateFns()); |
1210 | New->setTransformFn(getTransformFn()); |
1211 | return New; |
1212 | } |
1213 | |
1214 | |
1215 | void TreePatternNode::RemoveAllTypes() { |
1216 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
1217 | Types[i] = EEVT::TypeSet(); |
1218 | if (isLeaf()) return; |
1219 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1220 | getChild(i)->RemoveAllTypes(); |
1221 | } |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | void TreePatternNode:: |
1227 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
1228 | if (isLeaf()) return; |
1229 | |
1230 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
1231 | TreePatternNode *Child = getChild(i); |
1232 | if (Child->isLeaf()) { |
1233 | Init *Val = Child->getLeafValue(); |
1234 | if (isa<DefInit>(Val) && |
1235 | cast<DefInit>(Val)->getDef()->getName() == "node") { |
1236 | |
1237 | TreePatternNode *NewChild = ArgMap[Child->getName()]; |
1238 | assert(NewChild && "Couldn't find formal argument!")((void)0); |
1239 | assert((Child->getPredicateFns().empty() ||((void)0) |
1240 | NewChild->getPredicateFns() == Child->getPredicateFns()) &&((void)0) |
1241 | "Non-empty child predicate clobbered!")((void)0); |
1242 | setChild(i, NewChild); |
1243 | } |
1244 | } else { |
1245 | getChild(i)->SubstituteFormalArguments(ArgMap); |
1246 | } |
1247 | } |
1248 | } |
1249 | |
1250 | |
1251 | |
1252 | |
1253 | |
1254 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
1255 | if (TP.hasError()) |
1256 | return 0; |
1257 | |
1258 | if (isLeaf()) |
1259 | return this; |
1260 | Record *Op = getOperator(); |
1261 | |
1262 | if (!Op->isSubClassOf("PatFrag")) { |
1263 | |
1264 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
1265 | TreePatternNode *Child = getChild(i); |
1266 | TreePatternNode *NewChild = Child->InlinePatternFragments(TP); |
1267 | |
1268 | assert((Child->getPredicateFns().empty() ||((void)0) |
1269 | NewChild->getPredicateFns() == Child->getPredicateFns()) &&((void)0) |
1270 | "Non-empty child predicate clobbered!")((void)0); |
1271 | |
1272 | setChild(i, NewChild); |
1273 | } |
1274 | return this; |
1275 | } |
1276 | |
1277 | |
1278 | |
1279 | TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); |
1280 | |
1281 | |
1282 | if (Frag->getNumArgs() != Children.size()) { |
1283 | TP.error("'" + Op->getName() + "' fragment requires " + |
1284 | utostr(Frag->getNumArgs()) + " operands!"); |
1285 | return 0; |
1286 | } |
1287 | |
1288 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
1289 | |
1290 | TreePredicateFn PredFn(Frag); |
1291 | if (!PredFn.isAlwaysTrue()) |
1292 | FragTree->addPredicateFn(PredFn); |
1293 | |
1294 | |
1295 | if (Frag->getNumArgs()) { |
1296 | |
1297 | std::map<std::string, TreePatternNode*> ArgMap; |
1298 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
1299 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
1300 | |
1301 | FragTree->SubstituteFormalArguments(ArgMap); |
1302 | } |
1303 | |
1304 | FragTree->setName(getName()); |
1305 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
1306 | FragTree->UpdateNodeType(i, getExtType(i), TP); |
1307 | |
1308 | |
1309 | for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i) |
1310 | FragTree->addPredicateFn(getPredicateFns()[i]); |
1311 | |
1312 | |
1313 | |
1314 | |
1315 | |
1316 | |
1317 | return FragTree->InlinePatternFragments(TP); |
1318 | } |
1319 | |
1320 | |
1321 | |
1322 | |
1323 | |
1324 | static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo, |
1325 | bool NotRegisters, TreePattern &TP) { |
1326 | |
1327 | if (R->isSubClassOf("RegisterOperand")) { |
1328 | assert(ResNo == 0 && "Regoperand ref only has one result!")((void)0); |
1329 | if (NotRegisters) |
1330 | return EEVT::TypeSet(); |
1331 | Record *RegClass = R->getValueAsDef("RegClass"); |
1332 | const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); |
1333 | return EEVT::TypeSet(T.getRegisterClass(RegClass).getValueTypes()); |
1334 | } |
1335 | |
1336 | |
1337 | if (R->isSubClassOf("RegisterClass")) { |
1338 | assert(ResNo == 0 && "Regclass ref only has one result!")((void)0); |
1339 | if (NotRegisters) |
1340 | return EEVT::TypeSet(); |
1341 | const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); |
1342 | return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes()); |
1343 | } |
1344 | |
1345 | if (R->isSubClassOf("PatFrag")) { |
1346 | assert(ResNo == 0 && "FIXME: PatFrag with multiple results?")((void)0); |
1347 | |
1348 | return EEVT::TypeSet(); |
1349 | } |
1350 | |
1351 | if (R->isSubClassOf("Register")) { |
1352 | assert(ResNo == 0 && "Registers only produce one result!")((void)0); |
1353 | if (NotRegisters) |
1354 | return EEVT::TypeSet(); |
1355 | const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); |
1356 | return EEVT::TypeSet(T.getRegisterVTs(R)); |
1357 | } |
1358 | |
1359 | if (R->isSubClassOf("SubRegIndex")) { |
1360 | assert(ResNo == 0 && "SubRegisterIndices only produce one result!")((void)0); |
1361 | return EEVT::TypeSet(); |
1362 | } |
1363 | |
1364 | if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) { |
1365 | assert(ResNo == 0 && "This node only has one result!")((void)0); |
1366 | |
1367 | return EEVT::TypeSet(MVT::Other, TP); |
1368 | } |
1369 | |
1370 | if (R->isSubClassOf("ComplexPattern")) { |
1371 | assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?")((void)0); |
1372 | if (NotRegisters) |
1373 | return EEVT::TypeSet(); |
1374 | return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(), |
1375 | TP); |
1376 | } |
1377 | if (R->isSubClassOf("PointerLikeRegClass")) { |
1378 | assert(ResNo == 0 && "Regclass can only have one result!")((void)0); |
1379 | return EEVT::TypeSet(MVT::iPTR, TP); |
1380 | } |
1381 | |
1382 | if (R->getName() == "node" || R->getName() == "srcvalue" || |
1383 | R->getName() == "zero_reg") { |
1384 | |
1385 | return EEVT::TypeSet(); |
1386 | } |
1387 | |
1388 | TP.error("Unknown node flavor used in pattern: " + R->getName()); |
1389 | return EEVT::TypeSet(MVT::Other, TP); |
1390 | } |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | const CodeGenIntrinsic *TreePatternNode:: |
1396 | getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { |
1397 | if (getOperator() != CDP.get_intrinsic_void_sdnode() && |
1398 | getOperator() != CDP.get_intrinsic_w_chain_sdnode() && |
1399 | getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) |
1400 | return 0; |
1401 | |
1402 | unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue(); |
1403 | return &CDP.getIntrinsicInfo(IID); |
1404 | } |
1405 | |
1406 | |
1407 | |
1408 | const ComplexPattern * |
1409 | TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const { |
1410 | if (!isLeaf()) return 0; |
1411 | |
1412 | DefInit *DI = dyn_cast<DefInit>(getLeafValue()); |
1413 | if (DI && DI->getDef()->isSubClassOf("ComplexPattern")) |
1414 | return &CGP.getComplexPattern(DI->getDef()); |
1415 | return 0; |
1416 | } |
1417 | |
1418 | |
1419 | bool TreePatternNode::NodeHasProperty(SDNP Property, |
1420 | const CodeGenDAGPatterns &CGP) const { |
1421 | if (isLeaf()) { |
1422 | if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) |
1423 | return CP->hasProperty(Property); |
1424 | return false; |
1425 | } |
1426 | |
1427 | Record *Operator = getOperator(); |
1428 | if (!Operator->isSubClassOf("SDNode")) return false; |
1429 | |
1430 | return CGP.getSDNodeInfo(Operator).hasProperty(Property); |
1431 | } |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | bool TreePatternNode::TreeHasProperty(SDNP Property, |
1439 | const CodeGenDAGPatterns &CGP) const { |
1440 | if (NodeHasProperty(Property, CGP)) |
1441 | return true; |
1442 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1443 | if (getChild(i)->TreeHasProperty(Property, CGP)) |
1444 | return true; |
1445 | return false; |
1446 | } |
1447 | |
1448 | |
1449 | |
1450 | bool |
1451 | TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const { |
1452 | if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) |
1453 | return Int->isCommutative; |
1454 | return false; |
1455 | } |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | |
1461 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { |
1462 | if (TP.hasError()) |
1463 | return false; |
1464 | |
1465 | CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); |
1466 | if (isLeaf()) { |
1467 | if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { |
1468 | |
1469 | bool MadeChange = false; |
1470 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
1471 | MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i, |
1472 | NotRegisters, TP), TP); |
1473 | return MadeChange; |
1474 | } |
1475 | |
1476 | if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) { |
1477 | assert(Types.size() == 1 && "Invalid IntInit")((void)0); |
1478 | |
1479 | |
1480 | bool MadeChange = Types[0].EnforceInteger(TP); |
1481 | |
1482 | if (!Types[0].isConcrete()) |
1483 | return MadeChange; |
1484 | |
1485 | MVT::SimpleValueType VT = getType(0); |
1486 | if (VT == MVT::iPTR || VT == MVT::iPTRAny) |
1487 | return MadeChange; |
1488 | |
1489 | unsigned Size = EVT(VT).getSizeInBits(); |
1490 | |
1491 | if (Size >= 32) return MadeChange; |
1492 | |
1493 | |
1494 | |
1495 | int64_t SignBitAndAbove = II->getValue() >> (Size - 1); |
1496 | if (SignBitAndAbove == -1 || SignBitAndAbove == 0 || SignBitAndAbove == 1) |
1497 | return MadeChange; |
1498 | |
1499 | TP.error("Integer value '" + itostr(II->getValue()) + |
1500 | "' is out of range for type '" + getEnumName(getType(0)) + "'!"); |
1501 | return false; |
1502 | } |
1503 | return false; |
1504 | } |
1505 | |
1506 | |
1507 | if (getOperator()->getName() == "set") { |
1508 | assert(getNumTypes() == 0 && "Set doesn't produce a value")((void)0); |
1509 | assert(getNumChildren() >= 2 && "Missing RHS of a set?")((void)0); |
1510 | unsigned NC = getNumChildren(); |
1511 | |
1512 | TreePatternNode *SetVal = getChild(NC-1); |
1513 | bool MadeChange = SetVal->ApplyTypeConstraints(TP, NotRegisters); |
1514 | |
1515 | for (unsigned i = 0; i < NC-1; ++i) { |
1516 | TreePatternNode *Child = getChild(i); |
1517 | MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters); |
1518 | |
1519 | |
1520 | MadeChange |= Child->UpdateNodeType(0, SetVal->getExtType(i), TP); |
1521 | MadeChange |= SetVal->UpdateNodeType(i, Child->getExtType(0), TP); |
1522 | } |
1523 | return MadeChange; |
1524 | } |
1525 | |
1526 | if (getOperator()->getName() == "implicit") { |
1527 | assert(getNumTypes() == 0 && "Node doesn't produce a value")((void)0); |
1528 | |
1529 | bool MadeChange = false; |
1530 | for (unsigned i = 0; i < getNumChildren(); ++i) |
1531 | MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
1532 | return MadeChange; |
1533 | } |
1534 | |
1535 | if (getOperator()->getName() == "COPY_TO_REGCLASS") { |
1536 | bool MadeChange = false; |
1537 | MadeChange |= getChild(0)->ApplyTypeConstraints(TP, NotRegisters); |
1538 | MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters); |
1539 | |
1540 | assert(getChild(0)->getNumTypes() == 1 &&((void)0) |
1541 | getChild(1)->getNumTypes() == 1 && "Unhandled case")((void)0); |
1542 | |
1543 | |
1544 | |
1545 | |
1546 | if (!getChild(1)->hasTypeSet(0) && |
1547 | !getChild(1)->getExtType(0).isCompletelyUnknown()) { |
1548 | MVT::SimpleValueType RCVT = getChild(1)->getExtType(0).getTypeList()[0]; |
1549 | MadeChange |= getChild(1)->UpdateNodeType(0, RCVT, TP); |
1550 | } |
1551 | return MadeChange; |
1552 | } |
1553 | |
1554 | if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) { |
1555 | bool MadeChange = false; |
1556 | |
1557 | |
1558 | unsigned NumRetVTs = Int->IS.RetVTs.size(); |
1559 | unsigned NumParamVTs = Int->IS.ParamVTs.size(); |
1560 | |
1561 | for (unsigned i = 0, e = NumRetVTs; i != e; ++i) |
1562 | MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP); |
1563 | |
1564 | if (getNumChildren() != NumParamVTs + 1) { |
1565 | TP.error("Intrinsic '" + Int->Name + "' expects " + |
1566 | utostr(NumParamVTs) + " operands, not " + |
1567 | utostr(getNumChildren() - 1) + " operands!"); |
1568 | return false; |
1569 | } |
1570 | |
1571 | |
1572 | MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP); |
1573 | |
1574 | for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) { |
1575 | MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters); |
1576 | |
1577 | MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i]; |
1578 | assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case")((void)0); |
1579 | MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP); |
1580 | } |
1581 | return MadeChange; |
1582 | } |
1583 | |
1584 | if (getOperator()->isSubClassOf("SDNode")) { |
1585 | const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator()); |
1586 | |
1587 | |
1588 | if (NI.getNumOperands() >= 0 && |
1589 | getNumChildren() != (unsigned)NI.getNumOperands()) { |
1590 | TP.error(getOperator()->getName() + " node requires exactly " + |
1591 | itostr(NI.getNumOperands()) + " operands!"); |
1592 | return false; |
1593 | } |
1594 | |
1595 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
1596 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1597 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
1598 | return MadeChange; |
1599 | } |
1600 | |
1601 | if (getOperator()->isSubClassOf("Instruction")) { |
1602 | const DAGInstruction &Inst = CDP.getInstruction(getOperator()); |
1603 | CodeGenInstruction &InstInfo = |
1604 | CDP.getTargetInfo().getInstruction(getOperator()); |
1605 | |
1606 | bool MadeChange = false; |
1607 | |
1608 | |
1609 | |
1610 | |
1611 | unsigned NumResultsToAdd = InstInfo.Operands.NumDefs ? 1 : 0; |
1612 | for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo) |
1613 | MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP); |
1614 | |
1615 | |
1616 | |
1617 | if (!InstInfo.ImplicitDefs.empty()) { |
1618 | unsigned ResNo = NumResultsToAdd; |
1619 | |
1620 | |
1621 | |
1622 | MVT::SimpleValueType VT = |
1623 | InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()); |
1624 | |
1625 | if (VT != MVT::Other) |
1626 | MadeChange |= UpdateNodeType(ResNo, VT, TP); |
1627 | } |
1628 | |
1629 | |
1630 | |
1631 | if (getOperator()->getName() == "INSERT_SUBREG") { |
1632 | assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled")((void)0); |
1633 | MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP); |
1634 | MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP); |
1635 | } |
1636 | |
1637 | unsigned ChildNo = 0; |
1638 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { |
1639 | Record *OperandNode = Inst.getOperand(i); |
1640 | |
1641 | |
1642 | |
1643 | |
1644 | if (OperandNode->isSubClassOf("OperandWithDefaultOps") && |
1645 | !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) |
1646 | continue; |
1647 | |
1648 | |
1649 | if (ChildNo >= getNumChildren()) { |
1650 | TP.error("Instruction '" + getOperator()->getName() + |
1651 | "' expects more operands than were provided."); |
1652 | return false; |
1653 | } |
1654 | |
1655 | TreePatternNode *Child = getChild(ChildNo++); |
1656 | unsigned ChildResNo = 0; |
1657 | |
1658 | |
1659 | |
1660 | if (OperandNode->isSubClassOf("Operand")) { |
1661 | DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo"); |
1662 | if (unsigned NumArgs = MIOpInfo->getNumArgs()) { |
1663 | |
1664 | |
1665 | const ComplexPattern *AM = Child->getComplexPatternInfo(CDP); |
1666 | if (!AM || AM->getNumOperands() < NumArgs) { |
1667 | |
1668 | Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef(); |
1669 | MadeChange |= |
1670 | Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); |
1671 | |
1672 | |
1673 | for (unsigned Arg = 1; Arg < NumArgs; ++Arg) { |
1674 | if (ChildNo >= getNumChildren()) { |
1675 | TP.error("Instruction '" + getOperator()->getName() + |
1676 | "' expects more operands than were provided."); |
1677 | return false; |
1678 | } |
1679 | Child = getChild(ChildNo++); |
1680 | |
1681 | SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef(); |
1682 | MadeChange |= |
1683 | Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); |
1684 | } |
1685 | continue; |
1686 | } |
1687 | } |
1688 | } |
1689 | |
1690 | |
1691 | |
1692 | MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP); |
1693 | } |
1694 | |
1695 | if (ChildNo != getNumChildren()) { |
1696 | TP.error("Instruction '" + getOperator()->getName() + |
1697 | "' was provided too many operands!"); |
1698 | return false; |
1699 | } |
1700 | |
1701 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1702 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
1703 | return MadeChange; |
1704 | } |
1705 | |
1706 | assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!")((void)0); |
1707 | |
1708 | |
1709 | if (getNumChildren() != 1) { |
1710 | TP.error("Node transform '" + getOperator()->getName() + |
1711 | "' requires one operand!"); |
1712 | return false; |
1713 | } |
1714 | |
1715 | bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters); |
1716 | |
1717 | |
1718 | |
1719 | |
1720 | |
1721 | #if 0 |
1722 | if (!hasTypeSet() || !getChild(0)->hasTypeSet()) { |
1723 | bool MadeChange = UpdateNodeType(getChild(0)->getExtType(), TP); |
1724 | MadeChange |= getChild(0)->UpdateNodeType(getExtType(), TP); |
1725 | return MadeChange; |
1726 | } |
1727 | #endif |
1728 | return MadeChange; |
1729 | } |
1730 | |
1731 | |
1732 | |
1733 | static bool OnlyOnRHSOfCommutative(TreePatternNode *N) { |
1734 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") |
1735 | return true; |
1736 | if (N->isLeaf() && isa<IntInit>(N->getLeafValue())) |
1737 | return true; |
1738 | return false; |
1739 | } |
1740 | |
1741 | |
1742 | |
1743 | |
1744 | |
1745 | |
1746 | |
1747 | bool TreePatternNode::canPatternMatch(std::string &Reason, |
1748 | const CodeGenDAGPatterns &CDP) { |
1749 | if (isLeaf()) return true; |
1750 | |
1751 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
1752 | if (!getChild(i)->canPatternMatch(Reason, CDP)) |
1753 | return false; |
1754 | |
1755 | |
1756 | |
1757 | if (getOperator()->isSubClassOf("Intrinsic")) { |
1758 | |
1759 | return true; |
1760 | } |
1761 | |
1762 | |
1763 | |
1764 | const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); |
1765 | bool isCommIntrinsic = isCommutativeIntrinsic(CDP); |
1766 | if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { |
1767 | |
1768 | |
1769 | if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { |
1770 | bool Skip = isCommIntrinsic ? 1 : 0; |
1771 | for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i) |
1772 | if (OnlyOnRHSOfCommutative(getChild(i))) { |
1773 | Reason="Immediate value must be on the RHS of commutative operators!"; |
1774 | return false; |
1775 | } |
1776 | } |
1777 | } |
1778 | |
1779 | return true; |
1780 | } |
1781 | |
1782 | |
1783 | |
1784 | |
1785 | |
1786 | TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, |
1787 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), |
1788 | isInputPattern(isInput), HasError(false) { |
1789 | for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) |
1790 | Trees.push_back(ParseTreePattern(RawPat->getElement(i), "")); |
1791 | } |
1792 | |
1793 | TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, |
1794 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), |
1795 | isInputPattern(isInput), HasError(false) { |
1796 | Trees.push_back(ParseTreePattern(Pat, "")); |
1797 | } |
1798 | |
1799 | TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, |
1800 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), |
1801 | isInputPattern(isInput), HasError(false) { |
1802 | Trees.push_back(Pat); |
1803 | } |
1804 | |
1805 | void TreePattern::error(const std::string &Msg) { |
1806 | if (HasError) |
1807 | return; |
1808 | dump(); |
1809 | PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg); |
1810 | HasError = true; |
1811 | } |
1812 | |
1813 | void TreePattern::ComputeNamedNodes() { |
1814 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
1815 | ComputeNamedNodes(Trees[i]); |
1816 | } |
1817 | |
1818 | void TreePattern::ComputeNamedNodes(TreePatternNode *N) { |
1819 | if (!N->getName().empty()) |
1820 | NamedNodes[N->getName()].push_back(N); |
1821 | |
1822 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
1823 | ComputeNamedNodes(N->getChild(i)); |
1824 | } |
1825 | |
1826 | |
1827 | TreePatternNode *TreePattern::ParseTreePattern(Init *TheInit, StringRef OpName){ |
1828 | if (DefInit *DI = dyn_cast<DefInit>(TheInit)) { |
1829 | Record *R = DI->getDef(); |
1830 | |
1831 | |
1832 | |
1833 | |
1834 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) |
1835 | return ParseTreePattern( |
1836 | DagInit::get(DI, "", |
1837 | std::vector<std::pair<Init*, std::string> >()), |
1838 | OpName); |
1839 | |
1840 | |
1841 | TreePatternNode *Res = new TreePatternNode(DI, 1); |
1842 | if (R->getName() == "node" && !OpName.empty()) { |
1843 | if (OpName.empty()) |
1844 | error("'node' argument requires a name to match with operand list"); |
1845 | Args.push_back(OpName); |
1846 | } |
1847 | |
1848 | Res->setName(OpName); |
1849 | return Res; |
1850 | } |
1851 | |
1852 | if (IntInit *II = dyn_cast<IntInit>(TheInit)) { |
1853 | if (!OpName.empty()) |
1854 | error("Constant int argument should not have a name!"); |
1855 | return new TreePatternNode(II, 1); |
1856 | } |
1857 | |
1858 | if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) { |
1859 | |
1860 | Init *II = BI->convertInitializerTo(IntRecTy::get()); |
1861 | if (II == 0 || !isa<IntInit>(II)) |
1862 | error("Bits value must be constants!"); |
1863 | return ParseTreePattern(II, OpName); |
1864 | } |
1865 | |
1866 | DagInit *Dag = dyn_cast<DagInit>(TheInit); |
1867 | if (!Dag) { |
1868 | TheInit->dump(); |
1869 | error("Pattern has unexpected init kind!"); |
1870 | } |
1871 | DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator()); |
1872 | if (!OpDef) error("Pattern has unexpected operator type!"); |
1873 | Record *Operator = OpDef->getDef(); |
1874 | |
1875 | if (Operator->isSubClassOf("ValueType")) { |
1876 | |
1877 | |
1878 | if (Dag->getNumArgs() != 1) |
1879 | error("Type cast only takes one operand!"); |
1880 | |
1881 | TreePatternNode *New = ParseTreePattern(Dag->getArg(0), Dag->getArgName(0)); |
1882 | |
1883 | |
1884 | assert(New->getNumTypes() == 1 && "FIXME: Unhandled")((void)0); |
1885 | New->UpdateNodeType(0, getValueType(Operator), *this); |
1886 | |
1887 | if (!OpName.empty()) |
1888 | error("ValueType cast should not have a name!"); |
1889 | return New; |
1890 | } |
1891 | |
1892 | |
1893 | if (!Operator->isSubClassOf("PatFrag") && |
1894 | !Operator->isSubClassOf("SDNode") && |
1895 | !Operator->isSubClassOf("Instruction") && |
1896 | !Operator->isSubClassOf("SDNodeXForm") && |
1897 | !Operator->isSubClassOf("Intrinsic") && |
1898 | Operator->getName() != "set" && |
1899 | Operator->getName() != "implicit") |
1900 | error("Unrecognized node '" + Operator->getName() + "'!"); |
1901 | |
1902 | |
1903 | if (isInputPattern) { |
1904 | if (Operator->isSubClassOf("Instruction") || |
1905 | Operator->isSubClassOf("SDNodeXForm")) |
1906 | error("Cannot use '" + Operator->getName() + "' in an input pattern!"); |
1907 | } else { |
1908 | if (Operator->isSubClassOf("Intrinsic")) |
1909 | error("Cannot use '" + Operator->getName() + "' in an output pattern!"); |
1910 | |
1911 | if (Operator->isSubClassOf("SDNode") && |
1912 | Operator->getName() != "imm" && |
1913 | Operator->getName() != "fpimm" && |
1914 | Operator->getName() != "tglobaltlsaddr" && |
1915 | Operator->getName() != "tconstpool" && |
1916 | Operator->getName() != "tjumptable" && |
1917 | Operator->getName() != "tframeindex" && |
1918 | Operator->getName() != "texternalsym" && |
1919 | Operator->getName() != "tblockaddress" && |
1920 | Operator->getName() != "tglobaladdr" && |
1921 | Operator->getName() != "bb" && |
1922 | Operator->getName() != "vt") |
1923 | error("Cannot use '" + Operator->getName() + "' in an output pattern!"); |
1924 | } |
1925 | |
1926 | std::vector<TreePatternNode*> Children; |
1927 | |
1928 | |
1929 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) |
1930 | Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgName(i))); |
1931 | |
1932 | |
1933 | |
1934 | |
1935 | if (Operator->isSubClassOf("Intrinsic")) { |
1936 | const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator); |
1937 | unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1; |
1938 | |
1939 | |
1940 | |
1941 | if (Int.IS.RetVTs.empty()) |
1942 | Operator = getDAGPatterns().get_intrinsic_void_sdnode(); |
1943 | else if (Int.ModRef != CodeGenIntrinsic::NoMem) |
1944 | |
1945 | Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); |
1946 | else |
1947 | Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); |
1948 | |
1949 | TreePatternNode *IIDNode = new TreePatternNode(IntInit::get(IID), 1); |
1950 | Children.insert(Children.begin(), IIDNode); |
1951 | } |
1952 | |
1953 | unsigned NumResults = GetNumNodeResults(Operator, CDP); |
1954 | TreePatternNode *Result = new TreePatternNode(Operator, Children, NumResults); |
1955 | Result->setName(OpName); |
1956 | |
1957 | if (!Dag->getName().empty()) { |
1958 | assert(Result->getName().empty())((void)0); |
1959 | Result->setName(Dag->getName()); |
1960 | } |
1961 | return Result; |
1962 | } |
1963 | |
1964 | |
1965 | |
1966 | |
1967 | |
1968 | |
1969 | |
1970 | static bool SimplifyTree(TreePatternNode *&N) { |
1971 | if (N->isLeaf()) |
1972 | return false; |
1973 | |
1974 | |
1975 | |
1976 | if (N->getOperator()->getName() == "bitconvert" && |
1977 | N->getExtType(0).isConcrete() && |
1978 | N->getExtType(0) == N->getChild(0)->getExtType(0) && |
1979 | N->getName().empty()) { |
1980 | N = N->getChild(0); |
1981 | SimplifyTree(N); |
1982 | return true; |
1983 | } |
1984 | |
1985 | |
1986 | bool MadeChange = false; |
1987 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
1988 | TreePatternNode *Child = N->getChild(i); |
1989 | MadeChange |= SimplifyTree(Child); |
1990 | N->setChild(i, Child); |
1991 | } |
1992 | return MadeChange; |
1993 | } |
1994 | |
1995 | |
1996 | |
1997 | |
1998 | |
1999 | |
2000 | bool TreePattern:: |
2001 | InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) { |
2002 | if (NamedNodes.empty()) |
2003 | ComputeNamedNodes(); |
2004 | |
2005 | bool MadeChange = true; |
2006 | while (MadeChange) { |
2007 | MadeChange = false; |
2008 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
2009 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); |
2010 | MadeChange |= SimplifyTree(Trees[i]); |
2011 | } |
2012 | |
2013 | |
2014 | for (StringMap<SmallVector<TreePatternNode*,1> >::iterator |
2015 | I = NamedNodes.begin(), E = NamedNodes.end(); I != E; ++I) { |
2016 | SmallVectorImpl<TreePatternNode*> &Nodes = I->second; |
2017 | |
2018 | |
2019 | |
2020 | if (InNamedTypes) { |
2021 | |
2022 | assert(InNamedTypes->count(I->getKey()) &&((void)0) |
2023 | "Named node in output pattern but not input pattern?")((void)0); |
2024 | |
2025 | const SmallVectorImpl<TreePatternNode*> &InNodes = |
2026 | InNamedTypes->find(I->getKey())->second; |
2027 | |
2028 | |
2029 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
2030 | |
2031 | |
2032 | |
2033 | |
2034 | |
2035 | if (Nodes[i] == Trees[0] && Nodes[i]->isLeaf()) { |
2036 | DefInit *DI = dyn_cast<DefInit>(Nodes[i]->getLeafValue()); |
2037 | if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || |
2038 | DI->getDef()->isSubClassOf("RegisterOperand"))) |
2039 | continue; |
2040 | } |
2041 | |
2042 | assert(Nodes[i]->getNumTypes() == 1 &&((void)0) |
2043 | InNodes[0]->getNumTypes() == 1 &&((void)0) |
2044 | "FIXME: cannot name multiple result nodes yet")((void)0); |
2045 | MadeChange |= Nodes[i]->UpdateNodeType(0, InNodes[0]->getExtType(0), |
2046 | *this); |
2047 | } |
2048 | } |
2049 | |
2050 | |
2051 | |
2052 | if (I->second.size() > 1) { |
2053 | for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) { |
2054 | TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1]; |
2055 | assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&((void)0) |
2056 | "FIXME: cannot name multiple result nodes yet")((void)0); |
2057 | |
2058 | MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this); |
2059 | MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this); |
2060 | } |
2061 | } |
2062 | } |
2063 | } |
2064 | |
2065 | bool HasUnresolvedTypes = false; |
2066 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
2067 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
2068 | return !HasUnresolvedTypes; |
2069 | } |
2070 | |
2071 | void TreePattern::print(raw_ostream &OS) const { |
2072 | OS << getRecord()->getName(); |
2073 | if (!Args.empty()) { |
2074 | OS << "(" << Args[0]; |
2075 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
2076 | OS << ", " << Args[i]; |
2077 | OS << ")"; |
2078 | } |
2079 | OS << ": "; |
2080 | |
2081 | if (Trees.size() > 1) |
2082 | OS << "[\n"; |
2083 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
2084 | OS << "\t"; |
2085 | Trees[i]->print(OS); |
2086 | OS << "\n"; |
2087 | } |
2088 | |
2089 | if (Trees.size() > 1) |
2090 | OS << "]\n"; |
2091 | } |
2092 | |
2093 | void TreePattern::dump() const { print(errs()); } |
2094 | |
2095 | |
2096 | |
2097 | |
2098 | |
2099 | CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : |
2100 | Records(R), Target(R) { |
2101 | |
2102 | Intrinsics = LoadIntrinsics(Records, false); |
2103 | TgtIntrinsics = LoadIntrinsics(Records, true); |
2104 | ParseNodeInfo(); |
2105 | ParseNodeTransforms(); |
2106 | ParseComplexPatterns(); |
2107 | ParsePatternFragments(); |
2108 | ParseDefaultOperands(); |
2109 | ParseInstructions(); |
2110 | ParsePatterns(); |
2111 | |
2112 | |
2113 | |
2114 | GenerateVariants(); |
2115 | |
2116 | |
2117 | |
2118 | |
2119 | InferInstructionFlags(); |
2120 | |
2121 | |
2122 | VerifyInstructionFlags(); |
2123 | } |
2124 | |
2125 | CodeGenDAGPatterns::~CodeGenDAGPatterns() { |
2126 | for (pf_iterator I = PatternFragments.begin(), |
2127 | E = PatternFragments.end(); I != E; ++I) |
2128 | delete I->second; |
2129 | } |
2130 | |
2131 | |
2132 | Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const { |
2133 | Record *N = Records.getDef(Name); |
2134 | if (!N || !N->isSubClassOf("SDNode")) { |
2135 | errs() << "Error getting SDNode '" << Name << "'!\n"; |
2136 | exit(1); |
2137 | } |
2138 | return N; |
2139 | } |
2140 | |
2141 | |
2142 | void CodeGenDAGPatterns::ParseNodeInfo() { |
2143 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
2144 | while (!Nodes.empty()) { |
2145 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
2146 | Nodes.pop_back(); |
2147 | } |
2148 | |
2149 | |
2150 | intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); |
2151 | intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); |
2152 | intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); |
2153 | } |
2154 | |
2155 | |
2156 | |
2157 | void CodeGenDAGPatterns::ParseNodeTransforms() { |
2158 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
2159 | while (!Xforms.empty()) { |
2160 | Record *XFormNode = Xforms.back(); |
2161 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
2162 | std::string Code = XFormNode->getValueAsString("XFormFunction"); |
2163 | SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code))); |
2164 | |
2165 | Xforms.pop_back(); |
2166 | } |
2167 | } |
2168 | |
2169 | void CodeGenDAGPatterns::ParseComplexPatterns() { |
2170 | std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); |
2171 | while (!AMs.empty()) { |
2172 | ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); |
2173 | AMs.pop_back(); |
2174 | } |
2175 | } |
2176 | |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | void CodeGenDAGPatterns::ParsePatternFragments() { |
2184 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
2185 | |
2186 | |
2187 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
2188 | DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); |
2189 | TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this); |
2190 | PatternFragments[Fragments[i]] = P; |
2191 | |
2192 | |
2193 | std::vector<std::string> &Args = P->getArgList(); |
2194 | std::set<std::string> OperandsSet(Args.begin(), Args.end()); |
2195 | |
2196 | if (OperandsSet.count("")) |
2197 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
2198 | |
2199 | |
2200 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
2201 | DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator()); |
2202 | |
2203 | |
2204 | if (!OpsOp || |
2205 | (OpsOp->getDef()->getName() != "ops" && |
2206 | OpsOp->getDef()->getName() != "outs" && |
2207 | OpsOp->getDef()->getName() != "ins")) |
2208 | P->error("Operands list should start with '(ops ... '!"); |
2209 | |
2210 | |
2211 | Args.clear(); |
2212 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
2213 | if (!isa<DefInit>(OpsList->getArg(j)) || |
2214 | cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node") |
2215 | P->error("Operands list should all be 'node' values."); |
2216 | if (OpsList->getArgName(j).empty()) |
2217 | P->error("Operands list should have names for each operand!"); |
2218 | if (!OperandsSet.count(OpsList->getArgName(j))) |
2219 | P->error("'" + OpsList->getArgName(j) + |
2220 | "' does not occur in pattern or was multiply specified!"); |
2221 | OperandsSet.erase(OpsList->getArgName(j)); |
2222 | Args.push_back(OpsList->getArgName(j)); |
2223 | } |
2224 | |
2225 | if (!OperandsSet.empty()) |
2226 | P->error("Operands list does not contain an entry for operand '" + |
2227 | *OperandsSet.begin() + "'!"); |
2228 | |
2229 | |
2230 | |
2231 | TreePredicateFn PredFn(P); |
2232 | if (!PredFn.isAlwaysTrue()) |
2233 | P->getOnlyTree()->addPredicateFn(PredFn); |
2234 | |
2235 | |
2236 | |
2237 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
2238 | if (!getSDNodeTransform(Transform).second.empty()) |
2239 | P->getOnlyTree()->setTransformFn(Transform); |
2240 | } |
2241 | |
2242 | |
2243 | |
2244 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
2245 | TreePattern *ThePat = PatternFragments[Fragments[i]]; |
2246 | ThePat->InlinePatternFragments(); |
2247 | |
2248 | |
2249 | |
2250 | ThePat->InferAllTypes(); |
2251 | ThePat->resetError(); |
2252 | |
2253 | |
2254 | DEBUG(ThePat->dump())do { } while (0); |
2255 | } |
2256 | } |
2257 | |
2258 | void CodeGenDAGPatterns::ParseDefaultOperands() { |
2259 | std::vector<Record*> DefaultOps; |
2260 | DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps"); |
2261 | |
2262 | |
2263 | assert(!SDNodes.empty() && "No SDNodes parsed?")((void)0); |
2264 | Init *SomeSDNode = DefInit::get(SDNodes.begin()->first); |
2265 | |
2266 | for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) { |
2267 | DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps"); |
2268 | |
2269 | |
2270 | |
2271 | std::vector<std::pair<Init*, std::string> > Ops; |
2272 | for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) |
2273 | Ops.push_back(std::make_pair(DefaultInfo->getArg(op), |
2274 | DefaultInfo->getArgName(op))); |
2275 | DagInit *DI = DagInit::get(SomeSDNode, "", Ops); |
2276 | |
2277 | |
2278 | TreePattern P(DefaultOps[i], DI, false, *this); |
2279 | assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!")((void)0); |
2280 | |
2281 | |
2282 | DAGDefaultOperand DefaultOpInfo; |
2283 | |
2284 | TreePatternNode *T = P.getTree(0); |
2285 | for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) { |
2286 | TreePatternNode *TPN = T->getChild(op); |
2287 | while (TPN->ApplyTypeConstraints(P, false)) |
2288 | ; |
2289 | |
2290 | if (TPN->ContainsUnresolvedType()) { |
2291 | PrintFatalError("Value #" + utostr(i) + " of OperandWithDefaultOps '" + |
2292 | DefaultOps[i]->getName() +"' doesn't have a concrete type!"); |
2293 | } |
2294 | DefaultOpInfo.DefaultOps.push_back(TPN); |
2295 | } |
2296 | |
2297 | |
2298 | DefaultOperands[DefaultOps[i]] = DefaultOpInfo; |
2299 | } |
2300 | } |
2301 | |
2302 | |
2303 | |
2304 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
2305 | std::map<std::string, TreePatternNode*> &InstInputs) { |
2306 | |
2307 | if (Pat->getName().empty()) { |
2308 | if (Pat->isLeaf()) { |
2309 | DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); |
2310 | if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || |
2311 | DI->getDef()->isSubClassOf("RegisterOperand"))) |
2312 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
2313 | } |
2314 | return false; |
2315 | } |
2316 | |
2317 | Record *Rec; |
2318 | if (Pat->isLeaf()) { |
2319 | DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); |
2320 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
2321 | Rec = DI->getDef(); |
2322 | } else { |
2323 | Rec = Pat->getOperator(); |
2324 | } |
2325 | |
2326 | |
2327 | if (Rec->getName() == "srcvalue") |
2328 | return false; |
2329 | |
2330 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
2331 | if (!Slot) { |
2332 | Slot = Pat; |
2333 | return true; |
2334 | } |
2335 | Record *SlotRec; |
2336 | if (Slot->isLeaf()) { |
2337 | SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef(); |
2338 | } else { |
2339 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!")((void)0); |
2340 | SlotRec = Slot->getOperator(); |
2341 | } |
2342 | |
2343 | |
2344 | if (Rec != SlotRec) |
2345 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
2346 | if (Slot->getExtTypes() != Pat->getExtTypes()) |
2347 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
2348 | return true; |
2349 | } |
2350 | |
2351 | |
2352 | |
2353 | |
2354 | void CodeGenDAGPatterns:: |
2355 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
2356 | std::map<std::string, TreePatternNode*> &InstInputs, |
2357 | std::map<std::string, TreePatternNode*>&InstResults, |
2358 | std::vector<Record*> &InstImpResults) { |
2359 | if (Pat->isLeaf()) { |
2360 | bool isUse = HandleUse(I, Pat, InstInputs); |
2361 | if (!isUse && Pat->getTransformFn()) |
2362 | I->error("Cannot specify a transform function for a non-input value!"); |
2363 | return; |
2364 | } |
2365 | |
2366 | if (Pat->getOperator()->getName() == "implicit") { |
2367 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
2368 | TreePatternNode *Dest = Pat->getChild(i); |
2369 | if (!Dest->isLeaf()) |
2370 | I->error("implicitly defined value should be a register!"); |
2371 | |
2372 | DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); |
2373 | if (!Val || !Val->getDef()->isSubClassOf("Register")) |
2374 | I->error("implicitly defined value should be a register!"); |
2375 | InstImpResults.push_back(Val->getDef()); |
2376 | } |
2377 | return; |
2378 | } |
2379 | |
2380 | if (Pat->getOperator()->getName() != "set") { |
2381 | |
2382 | |
2383 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
2384 | if (Pat->getChild(i)->getNumTypes() == 0) |
2385 | I->error("Cannot have void nodes inside of patterns!"); |
2386 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, |
2387 | InstImpResults); |
2388 | } |
2389 | |
2390 | |
2391 | |
2392 | bool isUse = HandleUse(I, Pat, InstInputs); |
2393 | |
2394 | if (!isUse && Pat->getTransformFn()) |
2395 | I->error("Cannot specify a transform function for a non-input value!"); |
2396 | return; |
2397 | } |
2398 | |
2399 | |
2400 | if (Pat->getNumChildren() == 0) |
2401 | I->error("set requires operands!"); |
2402 | |
2403 | if (Pat->getTransformFn()) |
2404 | I->error("Cannot specify a transform function on a set node!"); |
2405 | |
2406 | |
2407 | unsigned NumDests = Pat->getNumChildren()-1; |
2408 | for (unsigned i = 0; i != NumDests; ++i) { |
2409 | TreePatternNode *Dest = Pat->getChild(i); |
2410 | if (!Dest->isLeaf()) |
2411 | I->error("set destination should be a register!"); |
2412 | |
2413 | DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); |
2414 | if (!Val) |
2415 | I->error("set destination should be a register!"); |
2416 | |
2417 | if (Val->getDef()->isSubClassOf("RegisterClass") || |
2418 | Val->getDef()->isSubClassOf("RegisterOperand") || |
2419 | Val->getDef()->isSubClassOf("PointerLikeRegClass")) { |
2420 | if (Dest->getName().empty()) |
2421 | I->error("set destination must have a name!"); |
2422 | if (InstResults.count(Dest->getName())) |
2423 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
2424 | InstResults[Dest->getName()] = Dest; |
2425 | } else if (Val->getDef()->isSubClassOf("Register")) { |
2426 | InstImpResults.push_back(Val->getDef()); |
2427 | } else { |
2428 | I->error("set destination should be a register!"); |
2429 | } |
2430 | } |
2431 | |
2432 | |
2433 | FindPatternInputsAndOutputs(I, Pat->getChild(NumDests), |
2434 | InstInputs, InstResults, InstImpResults); |
2435 | } |
2436 | |
2437 | |
2438 | |
2439 | |
2440 | |
2441 | class InstAnalyzer { |
2442 | const CodeGenDAGPatterns &CDP; |
2443 | public: |
2444 | bool hasSideEffects; |
2445 | bool mayStore; |
2446 | bool mayLoad; |
2447 | bool isBitcast; |
2448 | bool isVariadic; |
2449 | |
2450 | InstAnalyzer(const CodeGenDAGPatterns &cdp) |
2451 | : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false), |
2452 | isBitcast(false), isVariadic(false) {} |
2453 | |
2454 | void Analyze(const TreePattern *Pat) { |
2455 | |
2456 | AnalyzeNode(Pat->getTree(0)); |
2457 | } |
2458 | |
2459 | void Analyze(const PatternToMatch *Pat) { |
2460 | AnalyzeNode(Pat->getSrcPattern()); |
2461 | } |
2462 | |
2463 | private: |
2464 | bool IsNodeBitcast(const TreePatternNode *N) const { |
2465 | if (hasSideEffects || mayLoad || mayStore || isVariadic) |
2466 | return false; |
2467 | |
2468 | if (N->getNumChildren() != 2) |
2469 | return false; |
2470 | |
2471 | const TreePatternNode *N0 = N->getChild(0); |
2472 | if (!N0->isLeaf() || !isa<DefInit>(N0->getLeafValue())) |
2473 | return false; |
2474 | |
2475 | const TreePatternNode *N1 = N->getChild(1); |
2476 | if (N1->isLeaf()) |
2477 | return false; |
2478 | if (N1->getNumChildren() != 1 || !N1->getChild(0)->isLeaf()) |
2479 | return false; |
2480 | |
2481 | const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N1->getOperator()); |
2482 | if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1) |
2483 | return false; |
2484 | return OpInfo.getEnumName() == "ISD::BITCAST"; |
2485 | } |
2486 | |
2487 | public: |
2488 | void AnalyzeNode(const TreePatternNode *N) { |
2489 | if (N->isLeaf()) { |
2490 | if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) { |
2491 | Record *LeafRec = DI->getDef(); |
2492 | |
2493 | if (LeafRec->isSubClassOf("ComplexPattern")) { |
2494 | const ComplexPattern &CP = CDP.getComplexPattern(LeafRec); |
2495 | if (CP.hasProperty(SDNPMayStore)) mayStore = true; |
2496 | if (CP.hasProperty(SDNPMayLoad)) mayLoad = true; |
2497 | if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true; |
2498 | } |
2499 | } |
2500 | return; |
2501 | } |
2502 | |
2503 | |
2504 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
2505 | AnalyzeNode(N->getChild(i)); |
2506 | |
2507 | |
2508 | if (N->getOperator()->getName() == "set") { |
2509 | isBitcast = IsNodeBitcast(N); |
2510 | return; |
2511 | } |
2512 | |
2513 | |
2514 | const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator()); |
2515 | |
2516 | |
2517 | if (OpInfo.hasProperty(SDNPMayStore)) mayStore = true; |
2518 | if (OpInfo.hasProperty(SDNPMayLoad)) mayLoad = true; |
2519 | if (OpInfo.hasProperty(SDNPSideEffect)) hasSideEffects = true; |
2520 | if (OpInfo.hasProperty(SDNPVariadic)) isVariadic = true; |
2521 | |
2522 | if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) { |
2523 | |
2524 | if (IntInfo->ModRef >= CodeGenIntrinsic::ReadArgMem) |
2525 | mayLoad = true; |
2526 | |
2527 | if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteArgMem) |
2528 | mayStore = true; |
2529 | |
2530 | if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem) |
2531 | |
2532 | hasSideEffects = true; |
2533 | } |
2534 | } |
2535 | |
2536 | }; |
2537 | |
2538 | static bool InferFromPattern(CodeGenInstruction &InstInfo, |
2539 | const InstAnalyzer &PatInfo, |
2540 | Record *PatDef) { |
2541 | bool Error = false; |
2542 | |
2543 | |
2544 | if (InstInfo.hasUndefFlags()) |
2545 | InstInfo.InferredFrom = PatDef; |
2546 | |
2547 | |
2548 | if (InstInfo.hasSideEffects != PatInfo.hasSideEffects && |
2549 | !InstInfo.hasSideEffects_Unset) { |
2550 | |
2551 | |
2552 | |
2553 | if (!InstInfo.hasSideEffects) { |
2554 | Error = true; |
2555 | PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " + |
2556 | Twine(InstInfo.hasSideEffects)); |
2557 | } |
2558 | } |
2559 | |
2560 | if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) { |
2561 | Error = true; |
2562 | PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " + |
2563 | Twine(InstInfo.mayStore)); |
2564 | } |
2565 | |
2566 | if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) { |
2567 | |
2568 | |
2569 | if (!InstInfo.mayLoad) { |
2570 | Error = true; |
2571 | PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " + |
2572 | Twine(InstInfo.mayLoad)); |
2573 | } |
2574 | } |
2575 | |
2576 | |
2577 | InstInfo.hasSideEffects |= PatInfo.hasSideEffects; |
2578 | InstInfo.mayStore |= PatInfo.mayStore; |
2579 | InstInfo.mayLoad |= PatInfo.mayLoad; |
2580 | |
2581 | |
2582 | InstInfo.isBitcast |= PatInfo.isBitcast; |
2583 | |
2584 | |
2585 | |
2586 | |
2587 | |
2588 | |
2589 | return Error; |
2590 | } |
2591 | |
2592 | |
2593 | |
2594 | static bool hasNullFragReference(DagInit *DI) { |
2595 | DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator()); |
2596 | if (!OpDef) return false; |
2597 | Record *Operator = OpDef->getDef(); |
2598 | |
2599 | |
2600 | if (Operator->getName() == "null_frag") return true; |
2601 | |
2602 | for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) { |
2603 | DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i)); |
2604 | if (Arg && hasNullFragReference(Arg)) |
2605 | return true; |
2606 | } |
2607 | |
2608 | return false; |
2609 | } |
2610 | |
2611 | |
2612 | |
2613 | static bool hasNullFragReference(ListInit *LI) { |
2614 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) { |
2615 | DagInit *DI = dyn_cast<DagInit>(LI->getElement(i)); |
2616 | assert(DI && "non-dag in an instruction Pattern list?!")((void)0); |
2617 | if (hasNullFragReference(DI)) |
2618 | return true; |
2619 | } |
2620 | return false; |
2621 | } |
2622 | |
2623 | |
2624 | static void |
2625 | getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) { |
2626 | if (Tree->isLeaf()) |
2627 | return; |
2628 | if (Tree->getOperator()->isSubClassOf("Instruction")) |
2629 | Instrs.push_back(Tree->getOperator()); |
2630 | for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i) |
2631 | getInstructionsInTree(Tree->getChild(i), Instrs); |
2632 | } |
2633 | |
2634 | |
2635 | |
2636 | |
2637 | void CodeGenDAGPatterns::ParseInstructions() { |
2638 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
2639 | |
2640 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
2641 | ListInit *LI = 0; |
2642 | |
2643 | if (isa<ListInit>(Instrs[i]->getValueInit("Pattern"))) |
2644 | LI = Instrs[i]->getValueAsListInit("Pattern"); |
2645 | |
2646 | |
2647 | |
2648 | |
2649 | |
2650 | |
2651 | |
2652 | if (!LI || LI->getSize() == 0 || hasNullFragReference(LI)) { |
2653 | std::vector<Record*> Results; |
2654 | std::vector<Record*> Operands; |
2655 | |
2656 | CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); |
2657 | |
2658 | if (InstInfo.Operands.size() != 0) { |
2659 | if (InstInfo.Operands.NumDefs == 0) { |
2660 | |
2661 | for (unsigned j = 0, e = InstInfo.Operands.size(); j < e; ++j) |
2662 | Operands.push_back(InstInfo.Operands[j].Rec); |
2663 | } else { |
2664 | |
2665 | Results.push_back(InstInfo.Operands[0].Rec); |
2666 | |
2667 | |
2668 | for (unsigned j = 1, e = InstInfo.Operands.size(); j < e; ++j) |
2669 | Operands.push_back(InstInfo.Operands[j].Rec); |
2670 | } |
2671 | } |
2672 | |
2673 | |
2674 | std::vector<Record*> ImpResults; |
2675 | Instructions.insert(std::make_pair(Instrs[i], |
2676 | DAGInstruction(0, Results, Operands, ImpResults))); |
2677 | continue; |
2678 | } |
2679 | |
2680 | |
2681 | TreePattern *I = new TreePattern(Instrs[i], LI, true, *this); |
2682 | |
2683 | I->InlinePatternFragments(); |
2684 | |
2685 | |
2686 | |
2687 | if (!I->InferAllTypes()) |
2688 | I->error("Could not infer all types in pattern!"); |
2689 | |
2690 | |
2691 | |
2692 | std::map<std::string, TreePatternNode*> InstInputs; |
2693 | |
2694 | |
2695 | |
2696 | std::map<std::string, TreePatternNode*> InstResults; |
2697 | |
2698 | std::vector<Record*> InstImpResults; |
2699 | |
2700 | |
2701 | |
2702 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
2703 | TreePatternNode *Pat = I->getTree(j); |
2704 | if (Pat->getNumTypes() != 0) |
2705 | I->error("Top-level forms in instruction pattern should have" |
2706 | " void types"); |
2707 | |
2708 | |
2709 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, |
2710 | InstImpResults); |
2711 | } |
2712 | |
2713 | |
2714 | |
2715 | |
2716 | unsigned NumResults = InstResults.size(); |
2717 | |
2718 | |
2719 | assert(I->getArgList().empty() && "Args list should still be empty here!")((void)0); |
2720 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]); |
2721 | |
2722 | |
2723 | std::vector<Record*> Results; |
2724 | TreePatternNode *Res0Node = 0; |
2725 | for (unsigned i = 0; i != NumResults; ++i) { |
2726 | if (i == CGI.Operands.size()) |
2727 | I->error("'" + InstResults.begin()->first + |
2728 | "' set but does not appear in operand list!"); |
2729 | const std::string &OpName = CGI.Operands[i].Name; |
2730 | |
2731 | |
2732 | TreePatternNode *RNode = InstResults[OpName]; |
2733 | if (RNode == 0) |
2734 | I->error("Operand $" + OpName + " does not exist in operand list!"); |
2735 | |
2736 | if (i == 0) |
2737 | Res0Node = RNode; |
2738 | Record *R = cast<DefInit>(RNode->getLeafValue())->getDef(); |
2739 | if (R == 0) |
2740 | I->error("Operand $" + OpName + " should be a set destination: all " |
2741 | "outputs must occur before inputs in operand list!"); |
2742 | |
2743 | if (CGI.Operands[i].Rec != R) |
2744 | I->error("Operand $" + OpName + " class mismatch!"); |
2745 | |
2746 | |
2747 | Results.push_back(CGI.Operands[i].Rec); |
2748 | |
2749 | |
2750 | InstResults.erase(OpName); |
2751 | } |
2752 | |
2753 | |
2754 | |
2755 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
2756 | |
2757 | std::vector<TreePatternNode*> ResultNodeOperands; |
2758 | std::vector<Record*> Operands; |
2759 | for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) { |
2760 | CGIOperandList::OperandInfo &Op = CGI.Operands[i]; |
2761 | const std::string &OpName = Op.Name; |
2762 | if (OpName.empty()) |
2763 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
2764 | |
2765 | if (!InstInputsCheck.count(OpName)) { |
2766 | |
2767 | |
2768 | if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) { |
2769 | |
2770 | |
2771 | if (!getDefaultOperand(Op.Rec).DefaultOps.empty()) |
2772 | continue; |
2773 | } |
2774 | I->error("Operand $" + OpName + |
2775 | " does not appear in the instruction pattern"); |
2776 | } |
2777 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
2778 | InstInputsCheck.erase(OpName); |
2779 | |
2780 | if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) { |
2781 | Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); |
2782 | if (Op.Rec != InRec && !InRec->isSubClassOf("ComplexPattern")) |
2783 | I->error("Operand $" + OpName + "'s register class disagrees" |
2784 | " between the operand and pattern"); |
2785 | } |
2786 | Operands.push_back(Op.Rec); |
2787 | |
2788 | |
2789 | TreePatternNode *OpNode = InVal->clone(); |
2790 | |
2791 | |
2792 | OpNode->clearPredicateFns(); |
2793 | |
2794 | |
2795 | if (Record *Xform = OpNode->getTransformFn()) { |
2796 | OpNode->setTransformFn(0); |
2797 | std::vector<TreePatternNode*> Children; |
2798 | Children.push_back(OpNode); |
2799 | OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes()); |
2800 | } |
2801 | |
2802 | ResultNodeOperands.push_back(OpNode); |
2803 | } |
2804 | |
2805 | if (!InstInputsCheck.empty()) |
2806 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
2807 | " occurs in pattern but not in operands list!"); |
2808 | |
2809 | TreePatternNode *ResultPattern = |
2810 | new TreePatternNode(I->getRecord(), ResultNodeOperands, |
2811 | GetNumNodeResults(I->getRecord(), *this)); |
2812 | |
2813 | for (unsigned i = 0; i != NumResults; ++i) |
2814 | ResultPattern->setType(i, Res0Node->getExtType(i)); |
2815 | |
2816 | |
2817 | |
2818 | DAGInstruction TheInst(I, Results, Operands, InstImpResults); |
2819 | Instructions.insert(std::make_pair(I->getRecord(), TheInst)); |
2820 | |
2821 | |
2822 | |
2823 | |
2824 | TreePattern Temp(I->getRecord(), ResultPattern, false, *this); |
2825 | Temp.InferAllTypes(&I->getNamedNodesMap()); |
2826 | |
2827 | DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second; |
2828 | TheInsertedInst.setResultPattern(Temp.getOnlyTree()); |
2829 | |
2830 | DEBUG(I->dump())do { } while (0); |
2831 | } |
2832 | |
2833 | |
2834 | for (std::map<Record*, DAGInstruction, LessRecordByID>::iterator II = |
2835 | Instructions.begin(), |
2836 | E = Instructions.end(); II != E; ++II) { |
2837 | DAGInstruction &TheInst = II->second; |
2838 | TreePattern *I = TheInst.getPattern(); |
2839 | if (I == 0) continue; |
2840 | |
2841 | |
2842 | |
2843 | TreePatternNode *Pattern = I->getTree(0); |
2844 | TreePatternNode *SrcPattern; |
2845 | if (Pattern->getOperator()->getName() == "set") { |
2846 | SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone(); |
2847 | } else{ |
2848 | |
2849 | SrcPattern = Pattern; |
2850 | } |
2851 | |
2852 | Record *Instr = II->first; |
2853 | AddPatternToMatch(I, |
2854 | PatternToMatch(Instr, |
2855 | Instr->getValueAsListInit("Predicates"), |
2856 | SrcPattern, |
2857 | TheInst.getResultPattern(), |
2858 | TheInst.getImpResults(), |
2859 | Instr->getValueAsInt("AddedComplexity"), |
2860 | Instr->getID())); |
2861 | } |
2862 | } |
2863 | |
2864 | |
2865 | typedef std::pair<const TreePatternNode*, unsigned> NameRecord; |
2866 | |
2867 | static void FindNames(const TreePatternNode *P, |
2868 | std::map<std::string, NameRecord> &Names, |
2869 | TreePattern *PatternTop) { |
2870 | if (!P->getName().empty()) { |
2871 | NameRecord &Rec = Names[P->getName()]; |
2872 | |
2873 | if (Rec.second++ == 0) |
2874 | Rec.first = P; |
2875 | else if (Rec.first->getExtTypes() != P->getExtTypes()) |
2876 | PatternTop->error("repetition of value: $" + P->getName() + |
2877 | " where different uses have different types!"); |
2878 | } |
2879 | |
2880 | if (!P->isLeaf()) { |
2881 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
2882 | FindNames(P->getChild(i), Names, PatternTop); |
2883 | } |
2884 | } |
2885 | |
2886 | void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern, |
2887 | const PatternToMatch &PTM) { |
2888 | |
2889 | std::string Reason; |
2890 | if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) { |
2891 | PrintWarning(Pattern->getRecord()->getLoc(), |
2892 | Twine("Pattern can never match: ") + Reason); |
2893 | return; |
2894 | } |
2895 | |
2896 | |
2897 | |
2898 | if (const ComplexPattern *CP = |
2899 | PTM.getSrcPattern()->getComplexPatternInfo(*this)) |
2900 | if (CP->getRootNodes().empty()) |
2901 | Pattern->error("ComplexPattern at root must specify list of opcodes it" |
2902 | " could match"); |
2903 | |
2904 | |
2905 | |
2906 | |
2907 | std::map<std::string, NameRecord> SrcNames, DstNames; |
2908 | FindNames(PTM.getSrcPattern(), SrcNames, Pattern); |
2909 | FindNames(PTM.getDstPattern(), DstNames, Pattern); |
2910 | |
2911 | |
2912 | |
2913 | for (std::map<std::string, NameRecord>::iterator |
2914 | I = DstNames.begin(), E = DstNames.end(); I != E; ++I) { |
2915 | if (SrcNames[I->first].first == 0) |
2916 | Pattern->error("Pattern has input without matching name in output: $" + |
2917 | I->first); |
2918 | } |
2919 | |
2920 | |
2921 | |
2922 | for (std::map<std::string, NameRecord>::iterator |
2923 | I = SrcNames.begin(), E = SrcNames.end(); I != E; ++I) |
2924 | if (DstNames[I->first].first == 0 && SrcNames[I->first].second == 1) |
2925 | Pattern->error("Pattern has dead named input: $" + I->first); |
2926 | |
2927 | PatternsToMatch.push_back(PTM); |
2928 | } |
2929 | |
2930 | |
2931 | |
2932 | void CodeGenDAGPatterns::InferInstructionFlags() { |
2933 | const std::vector<const CodeGenInstruction*> &Instructions = |
2934 | Target.getInstructionsByEnumValue(); |
2935 | |
2936 | |
2937 | SmallVector<CodeGenInstruction*, 8> Revisit; |
2938 | unsigned Errors = 0; |
2939 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { |
2940 | CodeGenInstruction &InstInfo = |
2941 | const_cast<CodeGenInstruction &>(*Instructions[i]); |
2942 | |
2943 | |
2944 | |
2945 | if (InstInfo.neverHasSideEffects) { |
2946 | assert(!InstInfo.hasSideEffects)((void)0); |
2947 | InstInfo.hasSideEffects_Unset = false; |
2948 | } |
2949 | |
2950 | |
2951 | const TreePattern *Pattern = getInstruction(InstInfo.TheDef).getPattern(); |
2952 | if (!Pattern) { |
2953 | if (InstInfo.hasUndefFlags()) |
2954 | Revisit.push_back(&InstInfo); |
2955 | continue; |
2956 | } |
2957 | InstAnalyzer PatInfo(*this); |
2958 | PatInfo.Analyze(Pattern); |
2959 | Errors += InferFromPattern(InstInfo, PatInfo, InstInfo.TheDef); |
2960 | } |
2961 | |
2962 | |
2963 | |
2964 | for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) { |
2965 | const PatternToMatch &PTM = *I; |
2966 | |
2967 | |
2968 | |
2969 | SmallVector<Record*, 8> PatInstrs; |
2970 | getInstructionsInTree(PTM.getDstPattern(), PatInstrs); |
2971 | if (PatInstrs.size() != 1) |
2972 | continue; |
2973 | |
2974 | |
2975 | CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front()); |
2976 | |
2977 | |
2978 | if (InstInfo.InferredFrom) |
2979 | continue; |
2980 | |
2981 | InstAnalyzer PatInfo(*this); |
2982 | PatInfo.Analyze(&PTM); |
2983 | Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord()); |
2984 | } |
2985 | |
2986 | if (Errors) |
2987 | PrintFatalError("pattern conflicts"); |
2988 | |
2989 | |
2990 | if (Target.guessInstructionProperties()) { |
2991 | for (unsigned i = 0, e = Revisit.size(); i != e; ++i) { |
2992 | CodeGenInstruction &InstInfo = *Revisit[i]; |
2993 | if (InstInfo.InferredFrom) |
2994 | continue; |
2995 | |
2996 | |
2997 | if (InstInfo.hasSideEffects_Unset) |
2998 | InstInfo.hasSideEffects = true; |
2999 | } |
3000 | return; |
3001 | } |
3002 | |
3003 | |
3004 | for (unsigned i = 0, e = Revisit.size(); i != e; ++i) { |
3005 | CodeGenInstruction &InstInfo = *Revisit[i]; |
3006 | if (InstInfo.InferredFrom) |
3007 | continue; |
3008 | if (InstInfo.hasSideEffects_Unset) |
3009 | PrintError(InstInfo.TheDef->getLoc(), |
3010 | "Can't infer hasSideEffects from patterns"); |
3011 | if (InstInfo.mayStore_Unset) |
3012 | PrintError(InstInfo.TheDef->getLoc(), |
3013 | "Can't infer mayStore from patterns"); |
3014 | if (InstInfo.mayLoad_Unset) |
3015 | PrintError(InstInfo.TheDef->getLoc(), |
3016 | "Can't infer mayLoad from patterns"); |
3017 | } |
3018 | } |
3019 | |
3020 | |
3021 | |
3022 | void CodeGenDAGPatterns::VerifyInstructionFlags() { |
3023 | unsigned Errors = 0; |
3024 | for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) { |
3025 | const PatternToMatch &PTM = *I; |
3026 | SmallVector<Record*, 8> Instrs; |
3027 | getInstructionsInTree(PTM.getDstPattern(), Instrs); |
3028 | if (Instrs.empty()) |
3029 | continue; |
3030 | |
3031 | |
3032 | unsigned NumSideEffects = 0; |
3033 | unsigned NumStores = 0; |
3034 | unsigned NumLoads = 0; |
3035 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
3036 | const CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); |
3037 | NumSideEffects += InstInfo.hasSideEffects; |
3038 | NumStores += InstInfo.mayStore; |
3039 | NumLoads += InstInfo.mayLoad; |
3040 | } |
3041 | |
3042 | |
3043 | InstAnalyzer PatInfo(*this); |
3044 | PatInfo.Analyze(&PTM); |
3045 | |
3046 | |
3047 | SmallVector<std::string, 4> Msgs; |
3048 | |
3049 | |
3050 | |
3051 | if (PatInfo.hasSideEffects && !NumSideEffects) |
3052 | Msgs.push_back("pattern has side effects, but hasSideEffects isn't set"); |
3053 | |
3054 | |
3055 | |
3056 | if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores) |
3057 | Msgs.push_back("pattern may store, but mayStore isn't set"); |
3058 | |
3059 | |
3060 | if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads) |
3061 | Msgs.push_back("pattern may load, but mayLoad isn't set"); |
3062 | |
3063 | |
3064 | if (Msgs.empty()) |
3065 | continue; |
3066 | ++Errors; |
3067 | |
3068 | for (unsigned i = 0, e = Msgs.size(); i != e; ++i) |
3069 | PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msgs[i]) + " on the " + |
3070 | (Instrs.size() == 1 ? |
3071 | "instruction" : "output instructions")); |
3072 | |
3073 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
3074 | if (Instrs[i] != PTM.getSrcRecord()) |
3075 | PrintError(Instrs[i]->getLoc(), "defined here"); |
3076 | const CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); |
3077 | if (InstInfo.InferredFrom && |
3078 | InstInfo.InferredFrom != InstInfo.TheDef && |
3079 | InstInfo.InferredFrom != PTM.getSrcRecord()) |
3080 | PrintError(InstInfo.InferredFrom->getLoc(), "inferred from patttern"); |
3081 | } |
3082 | } |
3083 | if (Errors) |
3084 | PrintFatalError("Errors in DAG patterns"); |
3085 | } |
3086 | |
3087 | |
3088 | |
3089 | |
3090 | static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) { |
3091 | if (N->isLeaf()) |
3092 | return false; |
3093 | |
3094 | |
3095 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
3096 | if (ForceArbitraryInstResultType(N->getChild(i), TP)) |
3097 | return true; |
3098 | |
3099 | if (!N->getOperator()->isSubClassOf("Instruction")) |
3100 | return false; |
3101 | |
3102 | |
3103 | |
3104 | for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) { |
3105 | if (N->getExtType(i).isCompletelyUnknown() || N->getExtType(i).isConcrete()) |
3106 | continue; |
3107 | |
3108 | |
3109 | if (N->getExtType(i).MergeInTypeInfo(N->getExtType(i).getTypeList()[0], TP)) |
3110 | return true; |
3111 | } |
3112 | |
3113 | return false; |
3114 | } |
3115 | |
3116 | void CodeGenDAGPatterns::ParsePatterns() { |
3117 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
3118 | |
3119 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 1 | Assuming 'i' is not equal to 'e' | |
|
| 2 | | Loop condition is true. Entering loop body | |
|
3120 | Record *CurPattern = Patterns[i]; |
3121 | DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch"); |
3122 | |
3123 | |
3124 | if (hasNullFragReference(Tree)) |
| |
3125 | continue; |
3126 | |
3127 | TreePattern *Pattern = new TreePattern(CurPattern, Tree, true, *this); |
3128 | |
3129 | |
3130 | Pattern->InlinePatternFragments(); |
3131 | |
3132 | ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs"); |
3133 | if (LI->getSize() == 0) continue; |
| |
3134 | |
3135 | |
3136 | TreePattern *Result = new TreePattern(CurPattern, LI, false, *this); |
| |
3137 | |
3138 | |
3139 | Result->InlinePatternFragments(); |
3140 | |
3141 | if (Result->getNumTrees() != 1) |
| |
3142 | Result->error("Cannot handle instructions producing instructions " |
3143 | "with temporaries yet!"); |
3144 | |
3145 | bool IterateInference; |
3146 | bool InferredAllPatternTypes, InferredAllResultTypes; |
3147 | do { |
| 10 | | Loop condition is false. Exiting loop | |
|
3148 | |
3149 | |
3150 | InferredAllPatternTypes = |
3151 | Pattern->InferAllTypes(&Pattern->getNamedNodesMap()); |
3152 | |
3153 | |
3154 | |
3155 | InferredAllResultTypes = |
3156 | Result->InferAllTypes(&Pattern->getNamedNodesMap()); |
3157 | |
3158 | IterateInference = false; |
3159 | |
3160 | |
3161 | |
3162 | |
3163 | |
3164 | for (unsigned i = 0, e = std::min(Result->getTree(0)->getNumTypes(), |
| 8 | | Loop condition is false. Execution continues on line 3182 | |
|
3165 | Pattern->getTree(0)->getNumTypes()); |
3166 | i != e; ++i) { |
| 7 | | Assuming 'i' is equal to 'e' | |
|
3167 | IterateInference = Pattern->getTree(0)-> |
3168 | UpdateNodeType(i, Result->getTree(0)->getExtType(i), *Result); |
3169 | IterateInference |= Result->getTree(0)-> |
3170 | UpdateNodeType(i, Pattern->getTree(0)->getExtType(i), *Result); |
3171 | } |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | |
3177 | |
3178 | |
3179 | |
3180 | |
3181 | |
3182 | if (!IterateInference && InferredAllPatternTypes && |
| |
3183 | !InferredAllResultTypes) |
3184 | IterateInference = ForceArbitraryInstResultType(Result->getTree(0), |
3185 | *Result); |
3186 | } while (IterateInference); |
3187 | |
3188 | |
3189 | |
3190 | if (!InferredAllPatternTypes) |
| |
3191 | Pattern->error("Could not infer all types in pattern!"); |
3192 | if (!InferredAllResultTypes) { |
| |
3193 | Pattern->dump(); |
3194 | Result->error("Could not infer all types in pattern result!"); |
3195 | } |
3196 | |
3197 | |
3198 | std::map<std::string, TreePatternNode*> InstInputs; |
3199 | std::map<std::string, TreePatternNode*> InstResults; |
3200 | std::vector<Record*> InstImpResults; |
3201 | for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j) |
| 13 | | Assuming 'j' is equal to 'ee' | |
|
| 14 | | Loop condition is false. Execution continues on line 3207 | |
|
3202 | FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j), |
3203 | InstInputs, InstResults, |
3204 | InstImpResults); |
3205 | |
3206 | |
3207 | TreePatternNode *DstPattern = Result->getOnlyTree(); |
3208 | std::vector<TreePatternNode*> ResultNodeOperands; |
3209 | for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) { |
| 15 | | Assuming 'ii' is equal to 'ee' | |
|
| 16 | | Loop condition is false. Execution continues on line 3219 | |
|
3210 | TreePatternNode *OpNode = DstPattern->getChild(ii); |
3211 | if (Record *Xform = OpNode->getTransformFn()) { |
3212 | OpNode->setTransformFn(0); |
3213 | std::vector<TreePatternNode*> Children; |
3214 | Children.push_back(OpNode); |
3215 | OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes()); |
3216 | } |
3217 | ResultNodeOperands.push_back(OpNode); |
3218 | } |
3219 | DstPattern = Result->getOnlyTree(); |
3220 | if (!DstPattern->isLeaf()) |
| |
3221 | DstPattern = new TreePatternNode(DstPattern->getOperator(), |
3222 | ResultNodeOperands, |
3223 | DstPattern->getNumTypes()); |
3224 | |
3225 | for (unsigned i = 0, e = Result->getOnlyTree()->getNumTypes(); i != e; ++i) |
| 18 | | Assuming 'i' is equal to 'e' | |
|
| 19 | | Loop condition is false. Execution continues on line 3228 | |
|
3226 | DstPattern->setType(i, Result->getOnlyTree()->getExtType(i)); |
3227 | |
3228 | TreePattern Temp(Result->getRecord(), DstPattern, false, *this); |
| 20 | | Memory is never released; potential leak of memory pointed to by 'Result' |
|
3229 | Temp.InferAllTypes(); |
3230 | |
3231 | |
3232 | AddPatternToMatch(Pattern, |
3233 | PatternToMatch(CurPattern, |
3234 | CurPattern->getValueAsListInit("Predicates"), |
3235 | Pattern->getTree(0), |
3236 | Temp.getOnlyTree(), InstImpResults, |
3237 | CurPattern->getValueAsInt("AddedComplexity"), |
3238 | CurPattern->getID())); |
3239 | } |
3240 | } |
3241 | |
3242 | |
3243 | |
3244 | static void CombineChildVariants(TreePatternNode *Orig, |
3245 | const std::vector<std::vector<TreePatternNode*> > &ChildVariants, |
3246 | std::vector<TreePatternNode*> &OutVariants, |
3247 | CodeGenDAGPatterns &CDP, |
3248 | const MultipleUseVarSet &DepVars) { |
3249 | |
3250 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
3251 | if (ChildVariants[i].empty()) |
3252 | return; |
3253 | |
3254 | |
3255 | std::vector<unsigned> Idxs; |
3256 | Idxs.resize(ChildVariants.size()); |
3257 | bool NotDone; |
3258 | do { |
3259 | #ifndef NDEBUG1 |
3260 | DEBUG(if (!Idxs.empty()) {do { } while (0) |
3261 | errs() << Orig->getOperator()->getName() << ": Idxs = [ ";do { } while (0) |
3262 | for (unsigned i = 0; i < Idxs.size(); ++i) {do { } while (0) |
3263 | errs() << Idxs[i] << " ";do { } while (0) |
3264 | }do { } while (0) |
3265 | errs() << "]\n";do { } while (0) |
3266 | })do { } while (0); |
3267 | #endif |
3268 | |
3269 | std::vector<TreePatternNode*> NewChildren; |
3270 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
3271 | NewChildren.push_back(ChildVariants[i][Idxs[i]]); |
3272 | TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren, |
3273 | Orig->getNumTypes()); |
3274 | |
3275 | |
3276 | R->setName(Orig->getName()); |
3277 | R->setPredicateFns(Orig->getPredicateFns()); |
3278 | R->setTransformFn(Orig->getTransformFn()); |
3279 | for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i) |
3280 | R->setType(i, Orig->getExtType(i)); |
3281 | |
3282 | |
3283 | std::string ErrString; |
3284 | if (!R->canPatternMatch(ErrString, CDP)) { |
3285 | delete R; |
3286 | } else { |
3287 | bool AlreadyExists = false; |
3288 | |
3289 | |
3290 | |
3291 | |
3292 | |
3293 | for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) |
3294 | if (R->isIsomorphicTo(OutVariants[i], DepVars)) { |
3295 | AlreadyExists = true; |
3296 | break; |
3297 | } |
3298 | |
3299 | if (AlreadyExists) |
3300 | delete R; |
3301 | else |
3302 | OutVariants.push_back(R); |
3303 | } |
3304 | |
3305 | |
3306 | |
3307 | |
3308 | int IdxsIdx; |
3309 | for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) { |
3310 | if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size()) |
3311 | Idxs[IdxsIdx] = 0; |
3312 | else |
3313 | break; |
3314 | } |
3315 | NotDone = (IdxsIdx >= 0); |
3316 | } while (NotDone); |
3317 | } |
3318 | |
3319 | |
3320 | |
3321 | static void CombineChildVariants(TreePatternNode *Orig, |
3322 | const std::vector<TreePatternNode*> &LHS, |
3323 | const std::vector<TreePatternNode*> &RHS, |
3324 | std::vector<TreePatternNode*> &OutVariants, |
3325 | CodeGenDAGPatterns &CDP, |
3326 | const MultipleUseVarSet &DepVars) { |
3327 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
3328 | ChildVariants.push_back(LHS); |
3329 | ChildVariants.push_back(RHS); |
3330 | CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars); |
3331 | } |
3332 | |
3333 | |
3334 | static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, |
3335 | std::vector<TreePatternNode *> &Children) { |
3336 | assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!")((void)0); |
3337 | Record *Operator = N->getOperator(); |
3338 | |
3339 | |
3340 | if (!N->getName().empty() || !N->getPredicateFns().empty() || |
3341 | N->getTransformFn()) { |
3342 | Children.push_back(N); |
3343 | return; |
3344 | } |
3345 | |
3346 | if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) |
3347 | Children.push_back(N->getChild(0)); |
3348 | else |
3349 | GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); |
3350 | |
3351 | if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) |
3352 | Children.push_back(N->getChild(1)); |
3353 | else |
3354 | GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); |
3355 | } |
3356 | |
3357 | |
3358 | |
3359 | |
3360 | static void GenerateVariantsOf(TreePatternNode *N, |
3361 | std::vector<TreePatternNode*> &OutVariants, |
3362 | CodeGenDAGPatterns &CDP, |
3363 | const MultipleUseVarSet &DepVars) { |
3364 | |
3365 | if (N->isLeaf()) { |
3366 | OutVariants.push_back(N); |
3367 | return; |
3368 | } |
3369 | |
3370 | |
3371 | const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator()); |
3372 | |
3373 | |
3374 | if (NodeInfo.hasProperty(SDNPAssociative)) { |
3375 | |
3376 | std::vector<TreePatternNode*> MaximalChildren; |
3377 | GatherChildrenOfAssociativeOpcode(N, MaximalChildren); |
3378 | |
3379 | |
3380 | |
3381 | if (MaximalChildren.size() == 3) { |
3382 | |
3383 | std::vector<TreePatternNode*> AVariants, BVariants, CVariants; |
3384 | GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars); |
3385 | GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars); |
3386 | GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars); |
3387 | |
3388 | |
3389 | |
3390 | |
3391 | |
3392 | |
3393 | std::vector<TreePatternNode*> ABVariants; |
3394 | std::vector<TreePatternNode*> BAVariants; |
3395 | std::vector<TreePatternNode*> ACVariants; |
3396 | std::vector<TreePatternNode*> CAVariants; |
3397 | std::vector<TreePatternNode*> BCVariants; |
3398 | std::vector<TreePatternNode*> CBVariants; |
3399 | CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars); |
3400 | CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars); |
3401 | CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars); |
3402 | CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars); |
3403 | CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars); |
3404 | CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars); |
3405 | |
3406 | |
3407 | CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars); |
3408 | CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars); |
3409 | CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars); |
3410 | CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars); |
3411 | CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars); |
3412 | CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars); |
3413 | |
3414 | |
3415 | CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars); |
3416 | CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars); |
3417 | CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars); |
3418 | CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars); |
3419 | CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars); |
3420 | CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars); |
3421 | return; |
3422 | } |
3423 | } |
3424 | |
3425 | |
3426 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
3427 | ChildVariants.resize(N->getNumChildren()); |
3428 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
3429 | GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars); |
3430 | |
3431 | |
3432 | CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars); |
3433 | |
3434 | |
3435 | bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP); |
3436 | if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { |
3437 | assert((N->getNumChildren()==2 || isCommIntrinsic) &&((void)0) |
3438 | "Commutative but doesn't have 2 children!")((void)0); |
3439 | |
3440 | unsigned NC = 0; |
3441 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
3442 | TreePatternNode *Child = N->getChild(i); |
3443 | if (Child->isLeaf()) |
3444 | if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) { |
3445 | Record *RR = DI->getDef(); |
3446 | if (RR->isSubClassOf("Register")) |
3447 | continue; |
3448 | } |
3449 | NC++; |
3450 | } |
3451 | |
3452 | if (isCommIntrinsic) { |
3453 | |
3454 | |
3455 | |
3456 | assert(NC >= 3 &&((void)0) |
3457 | "Commutative intrinsic should have at least 3 childrean!")((void)0); |
3458 | std::vector<std::vector<TreePatternNode*> > Variants; |
3459 | Variants.push_back(ChildVariants[0]); |
3460 | Variants.push_back(ChildVariants[2]); |
3461 | Variants.push_back(ChildVariants[1]); |
3462 | for (unsigned i = 3; i != NC; ++i) |
3463 | Variants.push_back(ChildVariants[i]); |
3464 | CombineChildVariants(N, Variants, OutVariants, CDP, DepVars); |
3465 | } else if (NC == 2) |
3466 | CombineChildVariants(N, ChildVariants[1], ChildVariants[0], |
3467 | OutVariants, CDP, DepVars); |
3468 | } |
3469 | } |
3470 | |
3471 | |
3472 | |
3473 | |
3474 | void CodeGenDAGPatterns::GenerateVariants() { |
3475 | DEBUG(errs() << "Generating instruction variants.\n")do { } while (0); |
3476 | |
3477 | |
3478 | |
3479 | |
3480 | |
3481 | |
3482 | |
3483 | |
3484 | |
3485 | |
3486 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
3487 | MultipleUseVarSet DepVars; |
3488 | std::vector<TreePatternNode*> Variants; |
3489 | FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars); |
3490 | DEBUG(errs() << "Dependent/multiply used variables: ")do { } while (0); |
3491 | DEBUG(DumpDepVars(DepVars))do { } while (0); |
3492 | DEBUG(errs() << "\n")do { } while (0); |
3493 | GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this, |
3494 | DepVars); |
3495 | |
3496 | assert(!Variants.empty() && "Must create at least original variant!")((void)0); |
3497 | Variants.erase(Variants.begin()); |
3498 | |
3499 | if (Variants.empty()) |
3500 | continue; |
3501 | |
3502 | DEBUG(errs() << "FOUND VARIANTS OF: ";do { } while (0) |
3503 | PatternsToMatch[i].getSrcPattern()->dump();do { } while (0) |
3504 | errs() << "\n")do { } while (0); |
3505 | |
3506 | for (unsigned v = 0, e = Variants.size(); v != e; ++v) { |
3507 | TreePatternNode *Variant = Variants[v]; |
3508 | |
3509 | DEBUG(errs() << " VAR#" << v << ": ";do { } while (0) |
3510 | Variant->dump();do { } while (0) |
3511 | errs() << "\n")do { } while (0); |
3512 | |
3513 | |
3514 | bool AlreadyExists = false; |
3515 | for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { |
3516 | |
3517 | if (PatternsToMatch[i].getPredicates() != |
3518 | PatternsToMatch[p].getPredicates()) |
3519 | continue; |
3520 | |
3521 | if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(), |
3522 | DepVars)) { |
3523 | DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n")do { } while (0); |
3524 | AlreadyExists = true; |
3525 | break; |
3526 | } |
3527 | } |
3528 | |
3529 | if (AlreadyExists) continue; |
3530 | |
3531 | |
3532 | PatternsToMatch. |
3533 | push_back(PatternToMatch(PatternsToMatch[i].getSrcRecord(), |
3534 | PatternsToMatch[i].getPredicates(), |
3535 | Variant, PatternsToMatch[i].getDstPattern(), |
3536 | PatternsToMatch[i].getDstRegs(), |
3537 | PatternsToMatch[i].getAddedComplexity(), |
3538 | Record::getNewUID())); |
3539 | } |
3540 | |
3541 | DEBUG(errs() << "\n")do { } while (0); |
3542 | } |
3543 | } |