blob: 5c0939352477fe77207d02ab48781adb659cd1b1 [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
John Criswelld3032032003-10-20 20:20:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
10// This tablegen backend is responsible for emitting a description of the target
11// instruction set for the code generator.
12//
13//===----------------------------------------------------------------------===//
14
15#include "InstrSelectorEmitter.h"
16#include "CodeGenWrappers.h"
17#include "Record.h"
18#include "Support/Debug.h"
19#include "Support/StringExtras.h"
20#include <set>
21
Brian Gaeke960707c2003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000024NodeType::ArgResultTypes NodeType::Translate(Record *R) {
25 const std::string &Name = R->getName();
26 if (Name == "DNVT_any") return Any;
27 if (Name == "DNVT_void") return Void;
28 if (Name == "DNVT_val" ) return Val;
29 if (Name == "DNVT_arg0") return Arg0;
30 if (Name == "DNVT_arg1") return Arg1;
31 if (Name == "DNVT_ptr" ) return Ptr;
32 if (Name == "DNVT_i8" ) return I8;
33 throw "Unknown DagNodeValType '" + Name + "'!";
34}
35
36
37//===----------------------------------------------------------------------===//
38// TreePatternNode implementation
39//
40
41/// getValueRecord - Returns the value of this tree node as a record. For now
42/// we only allow DefInit's as our leaf values, so this is used.
43Record *TreePatternNode::getValueRecord() const {
44 DefInit *DI = dynamic_cast<DefInit*>(getValue());
45 assert(DI && "Instruction Selector does not yet support non-def leaves!");
46 return DI->getDef();
47}
48
49
50// updateNodeType - Set the node type of N to VT if VT contains information. If
51// N already contains a conflicting type, then throw an exception
52//
53bool TreePatternNode::updateNodeType(MVT::ValueType VT,
54 const std::string &RecName) {
55 if (VT == MVT::Other || getType() == VT) return false;
56 if (getType() == MVT::Other) {
57 setType(VT);
58 return true;
59 }
60
Chris Lattner95c13c92003-12-11 00:58:34 +000061 throw "Type inference contradiction found for pattern " + RecName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000062}
63
64/// InstantiateNonterminals - If this pattern refers to any nonterminals which
65/// are not themselves completely resolved, clone the nonterminal and resolve it
66/// with the using context we provide.
67///
68void TreePatternNode::InstantiateNonterminals(InstrSelectorEmitter &ISE) {
69 if (!isLeaf()) {
70 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
71 getChild(i)->InstantiateNonterminals(ISE);
72 return;
73 }
74
75 // If this is a leaf, it might be a reference to a nonterminal! Check now.
76 Record *R = getValueRecord();
77 if (R->isSubClassOf("Nonterminal")) {
78 Pattern *NT = ISE.getPattern(R);
79 if (!NT->isResolved()) {
80 // We found an unresolved nonterminal reference. Ask the ISE to clone
81 // it for us, then update our reference to the fresh, new, resolved,
82 // nonterminal.
83
84 Value = new DefInit(ISE.InstantiateNonterminal(NT, getType()));
85 }
86 }
87}
88
89
90/// clone - Make a copy of this tree and all of its children.
91///
92TreePatternNode *TreePatternNode::clone() const {
93 TreePatternNode *New;
94 if (isLeaf()) {
95 New = new TreePatternNode(Value);
96 } else {
97 std::vector<std::pair<TreePatternNode*, std::string> > CChildren;
98 CChildren.reserve(Children.size());
99 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
100 CChildren.push_back(std::make_pair(getChild(i)->clone(),getChildName(i)));
101 New = new TreePatternNode(Operator, CChildren);
102 }
103 New->setType(Type);
104 return New;
105}
106
107std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N) {
108 if (N.isLeaf())
109 return OS << N.getType() << ":" << *N.getValue();
110 OS << "(" << N.getType() << ":";
111 OS << N.getOperator()->getName();
112
113 if (N.getNumChildren() != 0) {
114 OS << " " << *N.getChild(0);
115 for (unsigned i = 1, e = N.getNumChildren(); i != e; ++i)
116 OS << ", " << *N.getChild(i);
117 }
118 return OS << ")";
119}
120
121void TreePatternNode::dump() const { std::cerr << *this; }
122
123//===----------------------------------------------------------------------===//
124// Pattern implementation
125//
126
127// Parse the specified DagInit into a TreePattern which we can use.
128//
129Pattern::Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
130 InstrSelectorEmitter &ise)
131 : PTy(pty), ResultNode(0), TheRecord(TheRec), ISE(ise) {
132
133 // First, parse the pattern...
134 Tree = ParseTreePattern(RawPat);
135
136 // Run the type-inference engine...
137 InferAllTypes();
138
139 if (PTy == Instruction || PTy == Expander) {
140 // Check to make sure there is not any unset types in the tree pattern...
141 if (!isResolved()) {
142 std::cerr << "In pattern: " << *Tree << "\n";
143 error("Could not infer all types!");
144 }
145
146 // Check to see if we have a top-level (set) of a register.
147 if (Tree->getOperator()->getName() == "set") {
148 assert(Tree->getNumChildren() == 2 && "Set with != 2 arguments?");
149 if (!Tree->getChild(0)->isLeaf())
150 error("Arg #0 of set should be a register or register class!");
151 ResultNode = Tree->getChild(0);
152 ResultName = Tree->getChildName(0);
153 Tree = Tree->getChild(1);
154 }
155 }
156
157 calculateArgs(Tree, "");
158}
159
160void Pattern::error(const std::string &Msg) const {
161 std::string M = "In ";
162 switch (PTy) {
163 case Nonterminal: M += "nonterminal "; break;
164 case Instruction: M += "instruction "; break;
165 case Expander : M += "expander "; break;
166 }
167 throw M + TheRecord->getName() + ": " + Msg;
168}
169
170/// calculateArgs - Compute the list of all of the arguments to this pattern,
171/// which are the non-void leaf nodes in this pattern.
172///
173void Pattern::calculateArgs(TreePatternNode *N, const std::string &Name) {
174 if (N->isLeaf() || N->getNumChildren() == 0) {
175 if (N->getType() != MVT::isVoid)
176 Args.push_back(std::make_pair(N, Name));
177 } else {
178 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
179 calculateArgs(N->getChild(i), N->getChildName(i));
180 }
181}
182
183/// getIntrinsicType - Check to see if the specified record has an intrinsic
184/// type which should be applied to it. This infer the type of register
185/// references from the register file information, for example.
186///
187MVT::ValueType Pattern::getIntrinsicType(Record *R) const {
188 // Check to see if this is a register or a register class...
189 if (R->isSubClassOf("RegisterClass"))
190 return getValueType(R->getValueAsDef("RegType"));
191 else if (R->isSubClassOf("Nonterminal"))
192 return ISE.ReadNonterminal(R)->getTree()->getType();
193 else if (R->isSubClassOf("Register")) {
194 std::cerr << "WARNING: Explicit registers not handled yet!\n";
195 return MVT::Other;
196 }
197
198 error("Unknown value used: " + R->getName());
199 return MVT::Other;
200}
201
202TreePatternNode *Pattern::ParseTreePattern(DagInit *Dag) {
203 Record *Operator = Dag->getNodeType();
204
205 if (Operator->isSubClassOf("ValueType")) {
206 // If the operator is a ValueType, then this must be "type cast" of a leaf
207 // node.
208 if (Dag->getNumArgs() != 1)
209 error("Type cast only valid for a leaf node!");
210
211 Init *Arg = Dag->getArg(0);
212 TreePatternNode *New;
213 if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) {
214 New = new TreePatternNode(DI);
215 // If it's a regclass or something else known, set the type.
216 New->setType(getIntrinsicType(DI->getDef()));
217 } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) {
218 New = ParseTreePattern(DI);
219 } else {
220 Arg->dump();
221 error("Unknown leaf value for tree pattern!");
222 return 0;
223 }
224
225 // Apply the type cast...
226 New->updateNodeType(getValueType(Operator), TheRecord->getName());
227 return New;
228 }
229
230 if (!ISE.getNodeTypes().count(Operator))
231 error("Unrecognized node '" + Operator->getName() + "'!");
232
233 std::vector<std::pair<TreePatternNode*, std::string> > Children;
234
235 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
236 Init *Arg = Dag->getArg(i);
237 if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) {
238 Children.push_back(std::make_pair(ParseTreePattern(DI),
239 Dag->getArgName(i)));
240 } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) {
241 Record *R = DefI->getDef();
242 // Direct reference to a leaf DagNode? Turn it into a DagNode if its own.
243 if (R->isSubClassOf("DagNode")) {
244 Dag->setArg(i, new DagInit(R,
245 std::vector<std::pair<Init*, std::string> >()));
246 --i; // Revisit this node...
247 } else {
248 Children.push_back(std::make_pair(new TreePatternNode(DefI),
249 Dag->getArgName(i)));
250 // If it's a regclass or something else known, set the type.
251 Children.back().first->setType(getIntrinsicType(R));
252 }
253 } else {
254 Arg->dump();
255 error("Unknown leaf value for tree pattern!");
256 }
257 }
258
259 return new TreePatternNode(Operator, Children);
260}
261
262void Pattern::InferAllTypes() {
263 bool MadeChange, AnyUnset;
264 do {
265 MadeChange = false;
266 AnyUnset = InferTypes(Tree, MadeChange);
267 } while ((AnyUnset || MadeChange) && !(AnyUnset && !MadeChange));
268 Resolved = !AnyUnset;
269}
270
271
272// InferTypes - Perform type inference on the tree, returning true if there
273// are any remaining untyped nodes and setting MadeChange if any changes were
274// made.
275bool Pattern::InferTypes(TreePatternNode *N, bool &MadeChange) {
276 if (N->isLeaf()) return N->getType() == MVT::Other;
277
278 bool AnyUnset = false;
279 Record *Operator = N->getOperator();
280 const NodeType &NT = ISE.getNodeType(Operator);
281
282 // Check to see if we can infer anything about the argument types from the
283 // return types...
284 if (N->getNumChildren() != NT.ArgTypes.size())
285 error("Incorrect number of children for " + Operator->getName() + " node!");
286
287 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
288 TreePatternNode *Child = N->getChild(i);
289 AnyUnset |= InferTypes(Child, MadeChange);
290
291 switch (NT.ArgTypes[i]) {
292 case NodeType::Any: break;
293 case NodeType::I8:
294 MadeChange |= Child->updateNodeType(MVT::i1, TheRecord->getName());
295 break;
296 case NodeType::Arg0:
297 MadeChange |= Child->updateNodeType(N->getChild(0)->getType(),
298 TheRecord->getName());
299 break;
300 case NodeType::Arg1:
301 MadeChange |= Child->updateNodeType(N->getChild(1)->getType(),
302 TheRecord->getName());
303 break;
304 case NodeType::Val:
305 if (Child->getType() == MVT::isVoid)
306 error("Inferred a void node in an illegal place!");
307 break;
308 case NodeType::Ptr:
309 MadeChange |= Child->updateNodeType(ISE.getTarget().getPointerType(),
310 TheRecord->getName());
311 break;
312 case NodeType::Void:
313 MadeChange |= Child->updateNodeType(MVT::isVoid, TheRecord->getName());
314 break;
315 default: assert(0 && "Invalid argument ArgType!");
316 }
317 }
318
319 // See if we can infer anything about the return type now...
320 switch (NT.ResultType) {
321 case NodeType::Any: break;
322 case NodeType::Void:
323 MadeChange |= N->updateNodeType(MVT::isVoid, TheRecord->getName());
324 break;
325 case NodeType::I8:
326 MadeChange |= N->updateNodeType(MVT::i1, TheRecord->getName());
327 break;
328 case NodeType::Arg0:
329 MadeChange |= N->updateNodeType(N->getChild(0)->getType(),
330 TheRecord->getName());
331 break;
332 case NodeType::Arg1:
333 MadeChange |= N->updateNodeType(N->getChild(1)->getType(),
334 TheRecord->getName());
335 break;
336 case NodeType::Ptr:
337 MadeChange |= N->updateNodeType(ISE.getTarget().getPointerType(),
338 TheRecord->getName());
339 break;
340 case NodeType::Val:
341 if (N->getType() == MVT::isVoid)
342 error("Inferred a void node in an illegal place!");
343 break;
344 default:
345 assert(0 && "Unhandled type constraint!");
346 break;
347 }
348
349 return AnyUnset | N->getType() == MVT::Other;
350}
351
352/// clone - This method is used to make an exact copy of the current pattern,
353/// then change the "TheRecord" instance variable to the specified record.
354///
355Pattern *Pattern::clone(Record *R) const {
356 assert(PTy == Nonterminal && "Can only clone nonterminals");
357 return new Pattern(Tree->clone(), R, Resolved, ISE);
358}
359
360
361
362std::ostream &operator<<(std::ostream &OS, const Pattern &P) {
363 switch (P.getPatternType()) {
364 case Pattern::Nonterminal: OS << "Nonterminal pattern "; break;
365 case Pattern::Instruction: OS << "Instruction pattern "; break;
366 case Pattern::Expander: OS << "Expander pattern "; break;
367 }
368
369 OS << P.getRecord()->getName() << ":\t";
370
371 if (Record *Result = P.getResult())
372 OS << Result->getName() << " = ";
373 OS << *P.getTree();
374
375 if (!P.isResolved())
376 OS << " [not completely resolved]";
377 return OS;
378}
379
380void Pattern::dump() const { std::cerr << *this; }
381
382
383
384/// getSlotName - If this is a leaf node, return the slot name that the operand
385/// will update.
386std::string Pattern::getSlotName() const {
387 if (getPatternType() == Pattern::Nonterminal) {
388 // Just use the nonterminal name, which will already include the type if
389 // it has been cloned.
390 return getRecord()->getName();
391 } else {
392 std::string SlotName;
393 if (getResult())
394 SlotName = getResult()->getName()+"_";
395 else
396 SlotName = "Void_";
397 return SlotName + getName(getTree()->getType());
398 }
399}
400
401/// getSlotName - If this is a leaf node, return the slot name that the
402/// operand will update.
403std::string Pattern::getSlotName(Record *R) {
404 if (R->isSubClassOf("Nonterminal")) {
405 // Just use the nonterminal name, which will already include the type if
406 // it has been cloned.
407 return R->getName();
408 } else if (R->isSubClassOf("RegisterClass")) {
409 MVT::ValueType Ty = getValueType(R->getValueAsDef("RegType"));
410 return R->getName() + "_" + getName(Ty);
411 } else {
412 assert(0 && "Don't know how to get a slot name for this!");
413 }
414 return "";
415}
416
417//===----------------------------------------------------------------------===//
418// PatternOrganizer implementation
419//
420
421/// addPattern - Add the specified pattern to the appropriate location in the
422/// collection.
423void PatternOrganizer::addPattern(Pattern *P) {
424 NodesForSlot &Nodes = AllPatterns[P->getSlotName()];
425 if (!P->getTree()->isLeaf())
426 Nodes[P->getTree()->getOperator()].push_back(P);
427 else {
428 // Right now we only support DefInit's with node types...
429 Nodes[P->getTree()->getValueRecord()].push_back(P);
430 }
431}
432
433
434
435//===----------------------------------------------------------------------===//
436// InstrSelectorEmitter implementation
437//
438
439/// ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
440/// turning them into the more accessible NodeTypes data structure.
441///
442void InstrSelectorEmitter::ReadNodeTypes() {
443 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("DagNode");
444 DEBUG(std::cerr << "Getting node types: ");
445 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
446 Record *Node = Nodes[i];
447
448 // Translate the return type...
449 NodeType::ArgResultTypes RetTy =
450 NodeType::Translate(Node->getValueAsDef("RetType"));
451
452 // Translate the arguments...
453 ListInit *Args = Node->getValueAsListInit("ArgTypes");
454 std::vector<NodeType::ArgResultTypes> ArgTypes;
455
456 for (unsigned a = 0, e = Args->getSize(); a != e; ++a) {
457 if (DefInit *DI = dynamic_cast<DefInit*>(Args->getElement(a)))
458 ArgTypes.push_back(NodeType::Translate(DI->getDef()));
459 else
460 throw "In node " + Node->getName() + ", argument is not a Def!";
461
462 if (a == 0 && ArgTypes.back() == NodeType::Arg0)
463 throw "In node " + Node->getName() + ", arg 0 cannot have type 'arg0'!";
464 if (a == 1 && ArgTypes.back() == NodeType::Arg1)
465 throw "In node " + Node->getName() + ", arg 1 cannot have type 'arg1'!";
466 }
467 if ((RetTy == NodeType::Arg0 && Args->getSize() == 0) ||
468 (RetTy == NodeType::Arg1 && Args->getSize() < 2))
469 throw "In node " + Node->getName() +
470 ", invalid return type for node with this many operands!";
471
472 // Add the node type mapping now...
473 NodeTypes[Node] = NodeType(RetTy, ArgTypes);
474 DEBUG(std::cerr << Node->getName() << ", ");
475 }
476 DEBUG(std::cerr << "DONE!\n");
477}
478
479Pattern *InstrSelectorEmitter::ReadNonterminal(Record *R) {
480 Pattern *&P = Patterns[R];
481 if (P) return P; // Don't reread it!
482
483 DagInit *DI = R->getValueAsDag("Pattern");
484 P = new Pattern(Pattern::Nonterminal, DI, R, *this);
485 DEBUG(std::cerr << "Parsed " << *P << "\n");
486 return P;
487}
488
489
490// ReadNonTerminals - Read in all nonterminals and incorporate them into our
491// pattern database.
492void InstrSelectorEmitter::ReadNonterminals() {
493 std::vector<Record*> NTs = Records.getAllDerivedDefinitions("Nonterminal");
494 for (unsigned i = 0, e = NTs.size(); i != e; ++i)
495 ReadNonterminal(NTs[i]);
496}
497
498
499/// ReadInstructionPatterns - Read in all subclasses of Instruction, and process
500/// those with a useful Pattern field.
501///
502void InstrSelectorEmitter::ReadInstructionPatterns() {
503 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
504 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
505 Record *Inst = Insts[i];
506 if (DagInit *DI = dynamic_cast<DagInit*>(Inst->getValueInit("Pattern"))) {
507 Patterns[Inst] = new Pattern(Pattern::Instruction, DI, Inst, *this);
508 DEBUG(std::cerr << "Parsed " << *Patterns[Inst] << "\n");
509 }
510 }
511}
512
513/// ReadExpanderPatterns - Read in all expander patterns...
514///
515void InstrSelectorEmitter::ReadExpanderPatterns() {
516 std::vector<Record*> Expanders = Records.getAllDerivedDefinitions("Expander");
517 for (unsigned i = 0, e = Expanders.size(); i != e; ++i) {
518 Record *Expander = Expanders[i];
519 DagInit *DI = Expander->getValueAsDag("Pattern");
520 Patterns[Expander] = new Pattern(Pattern::Expander, DI, Expander, *this);
521 DEBUG(std::cerr << "Parsed " << *Patterns[Expander] << "\n");
522 }
523}
524
525
526// InstantiateNonterminals - Instantiate any unresolved nonterminals with
527// information from the context that they are used in.
528//
529void InstrSelectorEmitter::InstantiateNonterminals() {
530 DEBUG(std::cerr << "Instantiating nonterminals:\n");
531 for (std::map<Record*, Pattern*>::iterator I = Patterns.begin(),
532 E = Patterns.end(); I != E; ++I)
533 if (I->second->isResolved())
534 I->second->InstantiateNonterminals();
535}
536
537/// InstantiateNonterminal - This method takes the nonterminal specified by
538/// NT, which should not be completely resolved, clones it, applies ResultTy
539/// to its root, then runs the type inference stuff on it. This should
540/// produce a newly resolved nonterminal, which we make a record for and
541/// return. To be extra fancy and efficient, this only makes one clone for
542/// each type it is instantiated with.
543Record *InstrSelectorEmitter::InstantiateNonterminal(Pattern *NT,
544 MVT::ValueType ResultTy) {
545 assert(!NT->isResolved() && "Nonterminal is already resolved!");
546
547 // Check to see if we have already instantiated this pair...
548 Record* &Slot = InstantiatedNTs[std::make_pair(NT, ResultTy)];
549 if (Slot) return Slot;
550
551 Record *New = new Record(NT->getRecord()->getName()+"_"+getName(ResultTy));
552
553 // Copy over the superclasses...
554 const std::vector<Record*> &SCs = NT->getRecord()->getSuperClasses();
555 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
556 New->addSuperClass(SCs[i]);
557
558 DEBUG(std::cerr << " Nonterminal '" << NT->getRecord()->getName()
559 << "' for type '" << getName(ResultTy) << "', producing '"
560 << New->getName() << "'\n");
561
562 // Copy the pattern...
563 Pattern *NewPat = NT->clone(New);
564
565 // Apply the type to the root...
566 NewPat->getTree()->updateNodeType(ResultTy, New->getName());
567
568 // Infer types...
569 NewPat->InferAllTypes();
570
571 // Make sure everything is good to go now...
572 if (!NewPat->isResolved())
573 NewPat->error("Instantiating nonterminal did not resolve all types!");
574
575 // Add the pattern to the patterns map, add the record to the RecordKeeper,
576 // return the new record.
577 Patterns[New] = NewPat;
578 Records.addDef(New);
579 return Slot = New;
580}
581
582// CalculateComputableValues - Fill in the ComputableValues map through
583// analysis of the patterns we are playing with.
584void InstrSelectorEmitter::CalculateComputableValues() {
585 // Loop over all of the patterns, adding them to the ComputableValues map
586 for (std::map<Record*, Pattern*>::iterator I = Patterns.begin(),
587 E = Patterns.end(); I != E; ++I)
588 if (I->second->isResolved()) {
589 // We don't want to add patterns like R32 = R32. This is a hack working
590 // around a special case of a general problem, but for now we explicitly
591 // forbid these patterns. They can never match anyway.
592 Pattern *P = I->second;
593 if (!P->getResult() || !P->getTree()->isLeaf() ||
594 P->getResult() != P->getTree()->getValueRecord())
595 ComputableValues.addPattern(P);
596 }
597}
598
599#if 0
600// MoveIdenticalPatterns - Given a tree pattern 'P', move all of the tree
601// patterns which have the same top-level structure as P from the 'From' list to
602// the 'To' list.
603static void MoveIdenticalPatterns(TreePatternNode *P,
604 std::vector<std::pair<Pattern*, TreePatternNode*> > &From,
605 std::vector<std::pair<Pattern*, TreePatternNode*> > &To) {
606 assert(!P->isLeaf() && "All leaves are identical!");
607
608 const std::vector<TreePatternNode*> &PChildren = P->getChildren();
609 for (unsigned i = 0; i != From.size(); ++i) {
610 TreePatternNode *N = From[i].second;
611 assert(P->getOperator() == N->getOperator() &&"Differing operators?");
612 assert(PChildren.size() == N->getChildren().size() &&
613 "Nodes with different arity??");
614 bool isDifferent = false;
615 for (unsigned c = 0, e = PChildren.size(); c != e; ++c) {
616 TreePatternNode *PC = PChildren[c];
617 TreePatternNode *NC = N->getChild(c);
618 if (PC->isLeaf() != NC->isLeaf()) {
619 isDifferent = true;
620 break;
621 }
622
623 if (!PC->isLeaf()) {
624 if (PC->getOperator() != NC->getOperator()) {
625 isDifferent = true;
626 break;
627 }
628 } else { // It's a leaf!
629 if (PC->getValueRecord() != NC->getValueRecord()) {
630 isDifferent = true;
631 break;
632 }
633 }
634 }
635 // If it's the same as the reference one, move it over now...
636 if (!isDifferent) {
637 To.push_back(std::make_pair(From[i].first, N));
638 From.erase(From.begin()+i);
639 --i; // Don't skip an entry...
640 }
641 }
642}
643#endif
644
645static std::string getNodeName(Record *R) {
646 RecordVal *RV = R->getValue("EnumName");
647 if (RV)
648 if (Init *I = RV->getValue())
649 if (StringInit *SI = dynamic_cast<StringInit*>(I))
650 return SI->getValue();
651 return R->getName();
652}
653
654
655static void EmitPatternPredicates(TreePatternNode *Tree,
656 const std::string &VarName, std::ostream &OS){
657 OS << " && " << VarName << "->getNodeType() == ISD::"
658 << getNodeName(Tree->getOperator());
659
660 for (unsigned c = 0, e = Tree->getNumChildren(); c != e; ++c)
661 if (!Tree->getChild(c)->isLeaf())
662 EmitPatternPredicates(Tree->getChild(c),
663 VarName + "->getUse(" + utostr(c)+")", OS);
664}
665
666static void EmitPatternCosts(TreePatternNode *Tree, const std::string &VarName,
667 std::ostream &OS) {
668 for (unsigned c = 0, e = Tree->getNumChildren(); c != e; ++c)
669 if (Tree->getChild(c)->isLeaf()) {
670 OS << " + Match_"
671 << Pattern::getSlotName(Tree->getChild(c)->getValueRecord()) << "("
672 << VarName << "->getUse(" << c << "))";
673 } else {
674 EmitPatternCosts(Tree->getChild(c),
675 VarName + "->getUse(" + utostr(c) + ")", OS);
676 }
677}
678
679
680// EmitMatchCosters - Given a list of patterns, which all have the same root
681// pattern operator, emit an efficient decision tree to decide which one to
682// pick. This is structured this way to avoid reevaluations of non-obvious
683// subexpressions.
684void InstrSelectorEmitter::EmitMatchCosters(std::ostream &OS,
685 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
686 const std::string &VarPrefix,
687 unsigned IndentAmt) {
688 assert(!Patterns.empty() && "No patterns to emit matchers for!");
689 std::string Indent(IndentAmt, ' ');
690
691 // Load all of the operands of the root node into scalars for fast access
692 const NodeType &ONT = getNodeType(Patterns[0].second->getOperator());
693 for (unsigned i = 0, e = ONT.ArgTypes.size(); i != e; ++i)
694 OS << Indent << "SelectionDAGNode *" << VarPrefix << "_Op" << i
695 << " = N->getUse(" << i << ");\n";
696
697 // Compute the costs of computing the various nonterminals/registers, which
698 // are directly used at this level.
699 OS << "\n" << Indent << "// Operand matching costs...\n";
700 std::set<std::string> ComputedValues; // Avoid duplicate computations...
701 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
702 TreePatternNode *NParent = Patterns[i].second;
703 for (unsigned c = 0, e = NParent->getNumChildren(); c != e; ++c) {
704 TreePatternNode *N = NParent->getChild(c);
705 if (N->isLeaf()) {
706 Record *VR = N->getValueRecord();
707 const std::string &LeafName = VR->getName();
708 std::string OpName = VarPrefix + "_Op" + utostr(c);
709 std::string ValName = OpName + "_" + LeafName + "_Cost";
710 if (!ComputedValues.count(ValName)) {
711 OS << Indent << "unsigned " << ValName << " = Match_"
712 << Pattern::getSlotName(VR) << "(" << OpName << ");\n";
713 ComputedValues.insert(ValName);
714 }
715 }
716 }
717 }
718 OS << "\n";
719
720
721 std::string LocCostName = VarPrefix + "_Cost";
722 OS << Indent << "unsigned " << LocCostName << "Min = ~0U >> 1;\n"
723 << Indent << "unsigned " << VarPrefix << "_PatternMin = NoMatchPattern;\n";
724
725#if 0
726 // Separate out all of the patterns into groups based on what their top-level
727 // signature looks like...
728 std::vector<std::pair<Pattern*, TreePatternNode*> > PatternsLeft(Patterns);
729 while (!PatternsLeft.empty()) {
730 // Process all of the patterns that have the same signature as the last
731 // element...
732 std::vector<std::pair<Pattern*, TreePatternNode*> > Group;
733 MoveIdenticalPatterns(PatternsLeft.back().second, PatternsLeft, Group);
734 assert(!Group.empty() && "Didn't at least pick the source pattern?");
735
736#if 0
737 OS << "PROCESSING GROUP:\n";
738 for (unsigned i = 0, e = Group.size(); i != e; ++i)
739 OS << " " << *Group[i].first << "\n";
740 OS << "\n\n";
741#endif
742
743 OS << Indent << "{ // ";
744
745 if (Group.size() != 1) {
746 OS << Group.size() << " size group...\n";
747 OS << Indent << " unsigned " << VarPrefix << "_Pattern = NoMatch;\n";
748 } else {
749 OS << *Group[0].first << "\n";
750 OS << Indent << " unsigned " << VarPrefix << "_Pattern = "
751 << Group[0].first->getRecord()->getName() << "_Pattern;\n";
752 }
753
754 OS << Indent << " unsigned " << LocCostName << " = ";
755 if (Group.size() == 1)
756 OS << "1;\n"; // Add inst cost if at individual rec
757 else
758 OS << "0;\n";
759
760 // Loop over all of the operands, adding in their costs...
761 TreePatternNode *N = Group[0].second;
762 const std::vector<TreePatternNode*> &Children = N->getChildren();
763
764 // If necessary, emit conditionals to check for the appropriate tree
765 // structure here...
766 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
767 TreePatternNode *C = Children[i];
768 if (C->isLeaf()) {
769 // We already calculated the cost for this leaf, add it in now...
770 OS << Indent << " " << LocCostName << " += "
771 << VarPrefix << "_Op" << utostr(i) << "_"
772 << C->getValueRecord()->getName() << "_Cost;\n";
773 } else {
774 // If it's not a leaf, we have to check to make sure that the current
775 // node has the appropriate structure, then recurse into it...
776 OS << Indent << " if (" << VarPrefix << "_Op" << i
777 << "->getNodeType() == ISD::" << getNodeName(C->getOperator())
778 << ") {\n";
779 std::vector<std::pair<Pattern*, TreePatternNode*> > SubPatterns;
780 for (unsigned n = 0, e = Group.size(); n != e; ++n)
781 SubPatterns.push_back(std::make_pair(Group[n].first,
782 Group[n].second->getChild(i)));
783 EmitMatchCosters(OS, SubPatterns, VarPrefix+"_Op"+utostr(i),
784 IndentAmt + 4);
785 OS << Indent << " }\n";
786 }
787 }
788
789 // If the cost for this match is less than the minimum computed cost so far,
790 // update the minimum cost and selected pattern.
791 OS << Indent << " if (" << LocCostName << " < " << LocCostName << "Min) { "
792 << LocCostName << "Min = " << LocCostName << "; " << VarPrefix
793 << "_PatternMin = " << VarPrefix << "_Pattern; }\n";
794
795 OS << Indent << "}\n";
796 }
797#endif
798
799 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
800 Pattern *P = Patterns[i].first;
801 TreePatternNode *PTree = P->getTree();
802 unsigned PatternCost = 1;
803
804 // Check to see if there are any non-leaf elements in the pattern. If so,
805 // we need to emit a predicate for this match.
806 bool AnyNonLeaf = false;
807 for (unsigned c = 0, e = PTree->getNumChildren(); c != e; ++c)
808 if (!PTree->getChild(c)->isLeaf()) {
809 AnyNonLeaf = true;
810 break;
811 }
812
813 if (!AnyNonLeaf) { // No predicate necessary, just output a scope...
814 OS << " {// " << *P << "\n";
815 } else {
816 // We need to emit a predicate to make sure the tree pattern matches, do
817 // so now...
818 OS << " if (1";
819 for (unsigned c = 0, e = PTree->getNumChildren(); c != e; ++c)
820 if (!PTree->getChild(c)->isLeaf())
821 EmitPatternPredicates(PTree->getChild(c),
822 VarPrefix + "_Op" + utostr(c), OS);
823
824 OS << ") {\n // " << *P << "\n";
825 }
826
827 OS << " unsigned PatCost = " << PatternCost;
828
829 for (unsigned c = 0, e = PTree->getNumChildren(); c != e; ++c)
830 if (PTree->getChild(c)->isLeaf()) {
831 OS << " + " << VarPrefix << "_Op" << c << "_"
832 << PTree->getChild(c)->getValueRecord()->getName() << "_Cost";
833 } else {
834 EmitPatternCosts(PTree->getChild(c), VarPrefix + "_Op" + utostr(c), OS);
835 }
836 OS << ";\n";
837 OS << " if (PatCost < MinCost) { MinCost = PatCost; Pattern = "
838 << P->getRecord()->getName() << "_Pattern; }\n"
839 << " }\n";
840 }
841}
842
843static void ReduceAllOperands(TreePatternNode *N, const std::string &Name,
844 std::vector<std::pair<TreePatternNode*, std::string> > &Operands,
845 std::ostream &OS) {
846 if (N->isLeaf()) {
847 // If this is a leaf, register or nonterminal reference...
848 std::string SlotName = Pattern::getSlotName(N->getValueRecord());
849 OS << " ReducedValue_" << SlotName << " *" << Name << "Val = Reduce_"
850 << SlotName << "(" << Name << ", MBB);\n";
851 Operands.push_back(std::make_pair(N, Name+"Val"));
852 } else if (N->getNumChildren() == 0) {
853 // This is a reference to a leaf tree node, like an immediate or frame
854 // index.
855 if (N->getType() != MVT::isVoid) {
856 std::string SlotName =
857 getNodeName(N->getOperator()) + "_" + getName(N->getType());
858 OS << " ReducedValue_" << SlotName << " *" << Name << "Val = "
859 << Name << "->getValue<ReducedValue_" << SlotName << ">(ISD::"
860 << SlotName << "_Slot);\n";
861 Operands.push_back(std::make_pair(N, Name+"Val"));
862 }
863 } else {
864 // Otherwise this is an interior node...
865 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
866 std::string ChildName = Name + "_Op" + utostr(i);
867 OS << " SelectionDAGNode *" << ChildName << " = " << Name
868 << "->getUse(" << i << ");\n";
869 ReduceAllOperands(N->getChild(i), ChildName, Operands, OS);
870 }
871 }
872}
873
874/// PrintExpanderOperand - Print out Arg as part of the instruction emission
875/// process for the expander pattern P. This argument may be referencing some
876/// values defined in P, or may just be physical register references or
877/// something like that. If PrintArg is true, we are printing out arguments to
878/// the BuildMI call. If it is false, we are printing the result register
879/// name.
880void InstrSelectorEmitter::PrintExpanderOperand(Init *Arg,
881 const std::string &NameVar,
882 TreePatternNode *ArgDeclNode,
883 Pattern *P, bool PrintArg,
884 std::ostream &OS) {
885 if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) {
886 Record *Arg = DI->getDef();
887 if (!ArgDeclNode->isLeaf() && ArgDeclNode->getNumChildren() != 0)
888 P->error("Expected leaf node as argument!");
889 Record *ArgDecl = ArgDeclNode->isLeaf() ? ArgDeclNode->getValueRecord() :
890 ArgDeclNode->getOperator();
891 if (Arg->isSubClassOf("Register")) {
892 // This is a physical register reference... make sure that the instruction
893 // requested a register!
894 if (!ArgDecl->isSubClassOf("RegisterClass"))
895 P->error("Argument mismatch for instruction pattern!");
896
897 // FIXME: This should check to see if the register is in the specified
898 // register class!
899 if (PrintArg) OS << ".addReg(";
900 OS << getQualifiedName(Arg);
901 if (PrintArg) OS << ")";
902 return;
903 } else if (Arg->isSubClassOf("RegisterClass")) {
904 // If this is a symbolic register class reference, we must be using a
905 // named value.
906 if (NameVar.empty()) P->error("Did not specify WHICH register to pass!");
907 if (Arg != ArgDecl) P->error("Instruction pattern mismatch!");
908
909 if (PrintArg) OS << ".addReg(";
910 OS << NameVar;
911 if (PrintArg) OS << ")";
912 return;
913 } else if (Arg->getName() == "frameidx") {
914 if (!PrintArg) P->error("Cannot define a new frameidx value!");
915 OS << ".addFrameIndex(" << NameVar << ")";
916 return;
917 } else if (Arg->getName() == "basicblock") {
918 if (!PrintArg) P->error("Cannot define a new basicblock value!");
919 OS << ".addMBB(" << NameVar << ")";
920 return;
921 }
922 P->error("Unknown operand type '" + Arg->getName() + "' to expander!");
923 } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
924 if (!NameVar.empty())
925 P->error("Illegal to specify a name for a constant initializer arg!");
926
927 // Hack this check to allow R32 values with 0 as the initializer for memory
928 // references... FIXME!
929 if (ArgDeclNode->isLeaf() && II->getValue() == 0 &&
930 ArgDeclNode->getValueRecord()->getName() == "R32") {
931 OS << ".addReg(0)";
932 } else {
933 if (ArgDeclNode->isLeaf() || ArgDeclNode->getOperator()->getName()!="imm")
934 P->error("Illegal immediate int value '" + itostr(II->getValue()) +
935 "' operand!");
936 OS << ".addZImm(" << II->getValue() << ")";
937 }
938 return;
939 }
940 P->error("Unknown operand type to expander!");
941}
942
943static std::string getArgName(Pattern *P, const std::string &ArgName,
944 const std::vector<std::pair<TreePatternNode*, std::string> > &Operands) {
945 assert(P->getNumArgs() == Operands.size() &&"Argument computation mismatch!");
946 if (ArgName.empty()) return "";
947
948 for (unsigned i = 0, e = P->getNumArgs(); i != e; ++i)
949 if (P->getArgName(i) == ArgName)
950 return Operands[i].second + "->Val";
951
952 if (ArgName == P->getResultName())
953 return "NewReg";
954 P->error("Pattern does not define a value named $" + ArgName + "!");
955 return "";
956}
957
958
959void InstrSelectorEmitter::run(std::ostream &OS) {
960 // Type-check all of the node types to ensure we "understand" them.
961 ReadNodeTypes();
962
963 // Read in all of the nonterminals, instructions, and expanders...
964 ReadNonterminals();
965 ReadInstructionPatterns();
966 ReadExpanderPatterns();
967
968 // Instantiate any unresolved nonterminals with information from the context
969 // that they are used in.
970 InstantiateNonterminals();
971
972 // Clear InstantiatedNTs, we don't need it anymore...
973 InstantiatedNTs.clear();
974
975 DEBUG(std::cerr << "Patterns acquired:\n");
976 for (std::map<Record*, Pattern*>::iterator I = Patterns.begin(),
977 E = Patterns.end(); I != E; ++I)
978 if (I->second->isResolved())
979 DEBUG(std::cerr << " " << *I->second << "\n");
980
981 CalculateComputableValues();
982
Brian Gaeke960707c2003-11-11 22:41:34 +0000983 OS << "#include \"llvm/CodeGen/MachineInstrBuilder.h\"\n";
984
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000985 EmitSourceFileHeader("Instruction Selector for the " + Target.getName() +
986 " target", OS);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000987
988 // Output the slot number enums...
989 OS << "\nenum { // Slot numbers...\n"
990 << " LastBuiltinSlot = ISD::NumBuiltinSlots-1, // Start numbering here\n";
991 for (PatternOrganizer::iterator I = ComputableValues.begin(),
992 E = ComputableValues.end(); I != E; ++I)
993 OS << " " << I->first << "_Slot,\n";
994 OS << " NumSlots\n};\n\n// Reduction value typedefs...\n";
995
996 // Output the reduction value typedefs...
997 for (PatternOrganizer::iterator I = ComputableValues.begin(),
998 E = ComputableValues.end(); I != E; ++I) {
999
1000 OS << "typedef ReducedValue<unsigned, " << I->first
1001 << "_Slot> ReducedValue_" << I->first << ";\n";
1002 }
1003
1004 // Output the pattern enums...
1005 OS << "\n\n"
1006 << "enum { // Patterns...\n"
1007 << " NotComputed = 0,\n"
1008 << " NoMatchPattern, \n";
1009 for (PatternOrganizer::iterator I = ComputableValues.begin(),
1010 E = ComputableValues.end(); I != E; ++I) {
1011 OS << " // " << I->first << " patterns...\n";
1012 for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(),
1013 E = I->second.end(); J != E; ++J)
1014 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
1015 OS << " " << J->second[i]->getRecord()->getName() << "_Pattern,\n";
1016 }
1017 OS << "};\n\n";
1018
1019 //===--------------------------------------------------------------------===//
1020 // Emit the class definition...
1021 //
1022 OS << "namespace {\n"
1023 << " class " << Target.getName() << "ISel {\n"
1024 << " SelectionDAG &DAG;\n"
1025 << " public:\n"
Brian Gaeke666aaf72004-01-24 09:23:46 +00001026 << " " << Target.getName () << "ISel(SelectionDAG &D) : DAG(D) {}\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001027 << " void generateCode();\n"
1028 << " private:\n"
1029 << " unsigned makeAnotherReg(const TargetRegisterClass *RC) {\n"
1030 << " return DAG.getMachineFunction().getSSARegMap()->createVirt"
1031 "ualRegister(RC);\n"
1032 << " }\n\n"
1033 << " // DAG matching methods for classes... all of these methods"
1034 " return the cost\n"
1035 << " // of producing a value of the specified class and type, which"
1036 " also gets\n"
1037 << " // added to the DAG node.\n";
1038
1039 // Output all of the matching prototypes for slots...
1040 for (PatternOrganizer::iterator I = ComputableValues.begin(),
1041 E = ComputableValues.end(); I != E; ++I)
1042 OS << " unsigned Match_" << I->first << "(SelectionDAGNode *N);\n";
1043 OS << "\n // DAG matching methods for DAG nodes...\n";
1044
1045 // Output all of the matching prototypes for slot/node pairs
1046 for (PatternOrganizer::iterator I = ComputableValues.begin(),
1047 E = ComputableValues.end(); I != E; ++I)
1048 for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(),
1049 E = I->second.end(); J != E; ++J)
1050 OS << " unsigned Match_" << I->first << "_" << getNodeName(J->first)
1051 << "(SelectionDAGNode *N);\n";
1052
1053 // Output all of the dag reduction methods prototypes...
1054 OS << "\n // DAG reduction methods...\n";
1055 for (PatternOrganizer::iterator I = ComputableValues.begin(),
1056 E = ComputableValues.end(); I != E; ++I)
1057 OS << " ReducedValue_" << I->first << " *Reduce_" << I->first
1058 << "(SelectionDAGNode *N,\n" << std::string(27+2*I->first.size(), ' ')
1059 << "MachineBasicBlock *MBB);\n";
1060 OS << " };\n}\n\n";
1061
1062 // Emit the generateCode entry-point...
Brian Gaeke666aaf72004-01-24 09:23:46 +00001063 OS << "void " << Target.getName () << "ISel::generateCode() {\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001064 << " SelectionDAGNode *Root = DAG.getRoot();\n"
1065 << " assert(Root->getValueType() == MVT::isVoid && "
1066 "\"Root of DAG produces value??\");\n\n"
1067 << " std::cerr << \"\\n\";\n"
1068 << " unsigned Cost = Match_Void_void(Root);\n"
1069 << " if (Cost >= ~0U >> 1) {\n"
1070 << " std::cerr << \"Match failed!\\n\";\n"
1071 << " Root->dump();\n"
1072 << " abort();\n"
1073 << " }\n\n"
1074 << " std::cerr << \"Total DAG Cost: \" << Cost << \"\\n\\n\";\n\n"
1075 << " Reduce_Void_void(Root, 0);\n"
1076 << "}\n\n"
1077 << "//===" << std::string(70, '-') << "===//\n"
1078 << "// Matching methods...\n"
1079 << "//\n\n";
1080
1081 //===--------------------------------------------------------------------===//
1082 // Emit all of the matcher methods...
1083 //
1084 for (PatternOrganizer::iterator I = ComputableValues.begin(),
1085 E = ComputableValues.end(); I != E; ++I) {
1086 const std::string &SlotName = I->first;
1087 OS << "unsigned " << Target.getName() << "ISel::Match_" << SlotName
1088 << "(SelectionDAGNode *N) {\n"
1089 << " assert(N->getValueType() == MVT::"
1090 << getEnumName((*I->second.begin()).second[0]->getTree()->getType())
1091 << ");\n" << " // If we already have a cost available for " << SlotName
1092 << " use it!\n"
1093 << " if (N->getPatternFor(" << SlotName << "_Slot))\n"
1094 << " return N->getCostFor(" << SlotName << "_Slot);\n\n"
1095 << " unsigned Cost;\n"
1096 << " switch (N->getNodeType()) {\n"
1097 << " default: Cost = ~0U >> 1; // Match failed\n"
1098 << " N->setPatternCostFor(" << SlotName << "_Slot, NoMatchPattern, Cost, NumSlots);\n"
1099 << " break;\n";
1100
1101 for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(),
1102 E = I->second.end(); J != E; ++J)
1103 if (!J->first->isSubClassOf("Nonterminal"))
1104 OS << " case ISD::" << getNodeName(J->first) << ":\tCost = Match_"
1105 << SlotName << "_" << getNodeName(J->first) << "(N); break;\n";
1106 OS << " }\n"; // End of the switch statement
1107
1108 // Emit any patterns which have a nonterminal leaf as the RHS. These may
1109 // match multiple root nodes, so they cannot be handled with the switch...
1110 for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(),
1111 E = I->second.end(); J != E; ++J)
1112 if (J->first->isSubClassOf("Nonterminal")) {
1113 OS << " unsigned " << J->first->getName() << "_Cost = Match_"
1114 << getNodeName(J->first) << "(N);\n"
1115 << " if (" << getNodeName(J->first) << "_Cost < Cost) Cost = "
1116 << getNodeName(J->first) << "_Cost;\n";
1117 }
1118
1119 OS << " return Cost;\n}\n\n";
1120
1121 for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(),
1122 E = I->second.end(); J != E; ++J) {
1123 Record *Operator = J->first;
1124 bool isNonterm = Operator->isSubClassOf("Nonterminal");
1125 if (!isNonterm) {
1126 OS << "unsigned " << Target.getName() << "ISel::Match_";
1127 if (!isNonterm) OS << SlotName << "_";
1128 OS << getNodeName(Operator) << "(SelectionDAGNode *N) {\n"
1129 << " unsigned Pattern = NoMatchPattern;\n"
1130 << " unsigned MinCost = ~0U >> 1;\n";
1131
1132 std::vector<std::pair<Pattern*, TreePatternNode*> > Patterns;
1133 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
1134 Patterns.push_back(std::make_pair(J->second[i],
1135 J->second[i]->getTree()));
1136 EmitMatchCosters(OS, Patterns, "N", 2);
1137
1138 OS << "\n N->setPatternCostFor(" << SlotName
1139 << "_Slot, Pattern, MinCost, NumSlots);\n"
1140 << " return MinCost;\n"
1141 << "}\n";
1142 }
1143 }
1144 }
1145
1146 //===--------------------------------------------------------------------===//
1147 // Emit all of the reducer methods...
1148 //
1149 OS << "\n\n//===" << std::string(70, '-') << "===//\n"
1150 << "// Reducer methods...\n"
1151 << "//\n";
1152
1153 for (PatternOrganizer::iterator I = ComputableValues.begin(),
1154 E = ComputableValues.end(); I != E; ++I) {
1155 const std::string &SlotName = I->first;
1156 OS << "ReducedValue_" << SlotName << " *" << Target.getName()
1157 << "ISel::Reduce_" << SlotName
1158 << "(SelectionDAGNode *N, MachineBasicBlock *MBB) {\n"
1159 << " ReducedValue_" << SlotName << " *Val = N->hasValue<ReducedValue_"
1160 << SlotName << ">(" << SlotName << "_Slot);\n"
1161 << " if (Val) return Val;\n"
1162 << " if (N->getBB()) MBB = N->getBB();\n\n"
1163 << " switch (N->getPatternFor(" << SlotName << "_Slot)) {\n";
1164
1165 // Loop over all of the patterns that can produce a value for this slot...
1166 PatternOrganizer::NodesForSlot &NodesForSlot = I->second;
1167 for (PatternOrganizer::NodesForSlot::iterator J = NodesForSlot.begin(),
1168 E = NodesForSlot.end(); J != E; ++J)
1169 for (unsigned i = 0, e = J->second.size(); i != e; ++i) {
1170 Pattern *P = J->second[i];
1171 OS << " case " << P->getRecord()->getName() << "_Pattern: {\n"
1172 << " // " << *P << "\n";
1173 // Loop over the operands, reducing them...
1174 std::vector<std::pair<TreePatternNode*, std::string> > Operands;
1175 ReduceAllOperands(P->getTree(), "N", Operands, OS);
1176
1177 // Now that we have reduced all of our operands, and have the values
1178 // that reduction produces, perform the reduction action for this
1179 // pattern.
1180 std::string Result;
1181
1182 // If the pattern produces a register result, generate a new register
1183 // now.
1184 if (Record *R = P->getResult()) {
1185 assert(R->isSubClassOf("RegisterClass") &&
1186 "Only handle register class results so far!");
1187 OS << " unsigned NewReg = makeAnotherReg(" << Target.getName()
1188 << "::" << R->getName() << "RegisterClass);\n";
1189 Result = "NewReg";
1190 DEBUG(OS << " std::cerr << \"%reg\" << NewReg << \" =\t\";\n");
1191 } else {
1192 DEBUG(OS << " std::cerr << \"\t\t\";\n");
1193 Result = "0";
1194 }
1195
1196 // Print out the pattern that matched...
1197 DEBUG(OS << " std::cerr << \" " << P->getRecord()->getName() <<'"');
1198 DEBUG(for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1199 if (Operands[i].first->isLeaf()) {
1200 Record *RV = Operands[i].first->getValueRecord();
1201 assert(RV->isSubClassOf("RegisterClass") &&
1202 "Only handles registers here so far!");
1203 OS << " << \" %reg\" << " << Operands[i].second
1204 << "->Val";
1205 } else {
1206 OS << " << ' ' << " << Operands[i].second
1207 << "->Val";
1208 });
1209 DEBUG(OS << " << \"\\n\";\n");
1210
1211 // Generate the reduction code appropriate to the particular type of
1212 // pattern that this is...
1213 switch (P->getPatternType()) {
1214 case Pattern::Instruction:
1215 // Instruction patterns just emit a single MachineInstr, using BuildMI
1216 OS << " BuildMI(MBB, " << Target.getName() << "::"
1217 << P->getRecord()->getName() << ", " << Operands.size();
1218 if (P->getResult()) OS << ", NewReg";
1219 OS << ")";
1220
1221 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
1222 TreePatternNode *Op = Operands[i].first;
1223 if (Op->isLeaf()) {
1224 Record *RV = Op->getValueRecord();
1225 assert(RV->isSubClassOf("RegisterClass") &&
1226 "Only handles registers here so far!");
1227 OS << ".addReg(" << Operands[i].second << "->Val)";
1228 } else if (Op->getOperator()->getName() == "imm") {
1229 OS << ".addZImm(" << Operands[i].second << "->Val)";
1230 } else if (Op->getOperator()->getName() == "basicblock") {
1231 OS << ".addMBB(" << Operands[i].second << "->Val)";
1232 } else {
1233 assert(0 && "Unknown value type!");
1234 }
1235 }
1236 OS << ";\n";
1237 break;
1238 case Pattern::Expander: {
1239 // Expander patterns emit one machine instr for each instruction in
1240 // the list of instructions expanded to.
1241 ListInit *Insts = P->getRecord()->getValueAsListInit("Result");
1242 for (unsigned IN = 0, e = Insts->getSize(); IN != e; ++IN) {
1243 DagInit *DIInst = dynamic_cast<DagInit*>(Insts->getElement(IN));
1244 if (!DIInst) P->error("Result list must contain instructions!");
1245 Record *InstRec = DIInst->getNodeType();
1246 Pattern *InstPat = getPattern(InstRec);
1247 if (!InstPat || InstPat->getPatternType() != Pattern::Instruction)
1248 P->error("Instruction list must contain Instruction patterns!");
1249
1250 bool hasResult = InstPat->getResult() != 0;
1251 if (InstPat->getNumArgs() != DIInst->getNumArgs()-hasResult) {
1252 P->error("Incorrect number of arguments specified for inst '" +
1253 InstPat->getRecord()->getName() + "' in result list!");
1254 }
1255
1256 // Start emission of the instruction...
1257 OS << " BuildMI(MBB, " << Target.getName() << "::"
1258 << InstRec->getName() << ", "
1259 << DIInst->getNumArgs()-hasResult;
1260 // Emit register result if necessary..
1261 if (hasResult) {
1262 std::string ArgNameVal =
1263 getArgName(P, DIInst->getArgName(0), Operands);
1264 PrintExpanderOperand(DIInst->getArg(0), ArgNameVal,
1265 InstPat->getResultNode(), P, false,
1266 OS << ", ");
1267 }
1268 OS << ")";
1269
1270 for (unsigned i = hasResult, e = DIInst->getNumArgs(); i != e; ++i){
1271 std::string ArgNameVal =
1272 getArgName(P, DIInst->getArgName(i), Operands);
1273
1274 PrintExpanderOperand(DIInst->getArg(i), ArgNameVal,
1275 InstPat->getArg(i-hasResult), P, true, OS);
1276 }
1277
1278 OS << ";\n";
1279 }
1280 break;
1281 }
1282 default:
1283 assert(0 && "Reduction of this type of pattern not implemented!");
1284 }
1285
1286 OS << " Val = new ReducedValue_" << SlotName << "(" << Result<<");\n"
1287 << " break;\n"
1288 << " }\n";
1289 }
1290
1291
1292 OS << " default: assert(0 && \"Unknown " << SlotName << " pattern!\");\n"
1293 << " }\n\n N->addValue(Val); // Do not ever recalculate this\n"
1294 << " return Val;\n}\n\n";
1295 }
Brian Gaeke960707c2003-11-11 22:41:34 +00001296 EmitSourceFileTail(OS);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001297}
1298
Brian Gaeke960707c2003-11-11 22:41:34 +00001299} // End llvm namespace