// CalculateComputableValues - Fill in the ComputableValues map through // analysis of the patterns we are playing with. void InstrSelectorEmitter::CalculateComputableValues() { // Loop over all of the patterns, adding them to the ComputableValues map for (std::map<Record*, Pattern*>::iterator I = Patterns.begin(), E = Patterns.end(); I != E; ++I) if (I->second->isResolved()) { // We don't want to add patterns like R32 = R32. This is a hack working // around a special case of a general problem, but for now we explicitly // forbid these patterns. They can never match anyway. Pattern *P = I->second; if (!P->getResult() || !P->getTree()->isLeaf() || P->getResult() != P->getTree()->getValueRecord()) ComputableValues.addPattern(P); } }
void InstrSelectorEmitter::run(std::ostream &OS) { // Type-check all of the node types to ensure we "understand" them. ReadNodeTypes(); // Read in all of the nonterminals, instructions, and expanders... ReadNonterminals(); ReadInstructionPatterns(); ReadExpanderPatterns(); // Instantiate any unresolved nonterminals with information from the context // that they are used in. InstantiateNonterminals(); // Clear InstantiatedNTs, we don't need it anymore... InstantiatedNTs.clear(); DEBUG(std::cerr << "Patterns acquired:\n"); for (std::map<Record*, Pattern*>::iterator I = Patterns.begin(), E = Patterns.end(); I != E; ++I) if (I->second->isResolved()) DEBUG(std::cerr << " " << *I->second << "\n"); CalculateComputableValues(); OS << "#include \"llvm/CodeGen/MachineInstrBuilder.h\"\n"; EmitSourceFileHeader("Instruction Selector for the " + Target.getName() + " target", OS); // Output the slot number enums... OS << "\nenum { // Slot numbers...\n" << " LastBuiltinSlot = ISD::NumBuiltinSlots-1, // Start numbering here\n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) OS << " " << I->first << "_Slot,\n"; OS << " NumSlots\n};\n\n// Reduction value typedefs...\n"; // Output the reduction value typedefs... for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { OS << "typedef ReducedValue<unsigned, " << I->first << "_Slot> ReducedValue_" << I->first << ";\n"; } // Output the pattern enums... OS << "\n\n" << "enum { // Patterns...\n" << " NotComputed = 0,\n" << " NoMatchPattern, \n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { OS << " // " << I->first << " patterns...\n"; for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) for (unsigned i = 0, e = J->second.size(); i != e; ++i) OS << " " << J->second[i]->getRecord()->getName() << "_Pattern,\n"; } OS << "};\n\n"; //===--------------------------------------------------------------------===// // Emit the class definition... // OS << "namespace {\n" << " class " << Target.getName() << "ISel {\n" << " SelectionDAG &DAG;\n" << " public:\n" << " " << Target.getName () << "ISel(SelectionDAG &D) : DAG(D) {}\n" << " void generateCode();\n" << " private:\n" << " unsigned makeAnotherReg(const TargetRegisterClass *RC) {\n" << " return DAG.getMachineFunction().getSSARegMap()->createVirt" "ualRegister(RC);\n" << " }\n\n" << " // DAG matching methods for classes... all of these methods" " return the cost\n" << " // of producing a value of the specified class and type, which" " also gets\n" << " // added to the DAG node.\n"; // Output all of the matching prototypes for slots... for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) OS << " unsigned Match_" << I->first << "(SelectionDAGNode *N);\n"; OS << "\n // DAG matching methods for DAG nodes...\n"; // Output all of the matching prototypes for slot/node pairs for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) OS << " unsigned Match_" << I->first << "_" << getNodeName(J->first) << "(SelectionDAGNode *N);\n"; // Output all of the dag reduction methods prototypes... OS << "\n // DAG reduction methods...\n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) OS << " ReducedValue_" << I->first << " *Reduce_" << I->first << "(SelectionDAGNode *N,\n" << std::string(27+2*I->first.size(), ' ') << "MachineBasicBlock *MBB);\n"; OS << " };\n}\n\n"; // Emit the generateCode entry-point... OS << "void " << Target.getName () << "ISel::generateCode() {\n" << " SelectionDAGNode *Root = DAG.getRoot();\n" << " assert(Root->getValueType() == MVT::isVoid && " "\"Root of DAG produces value??\");\n\n" << " std::cerr << \"\\n\";\n" << " unsigned Cost = Match_Void_void(Root);\n" << " if (Cost >= ~0U >> 1) {\n" << " std::cerr << \"Match failed!\\n\";\n" << " Root->dump();\n" << " abort();\n" << " }\n\n" << " std::cerr << \"Total DAG Cost: \" << Cost << \"\\n\\n\";\n\n" << " Reduce_Void_void(Root, 0);\n" << "}\n\n" << "//===" << std::string(70, '-') << "===//\n" << "// Matching methods...\n" << "//\n\n"; //===--------------------------------------------------------------------===// // Emit all of the matcher methods... // for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { const std::string &SlotName = I->first; OS << "unsigned " << Target.getName() << "ISel::Match_" << SlotName << "(SelectionDAGNode *N) {\n" << " assert(N->getValueType() == MVT::" << getEnumName((*I->second.begin()).second[0]->getTree()->getType()) << ");\n" << " // If we already have a cost available for " << SlotName << " use it!\n" << " if (N->getPatternFor(" << SlotName << "_Slot))\n" << " return N->getCostFor(" << SlotName << "_Slot);\n\n" << " unsigned Cost;\n" << " switch (N->getNodeType()) {\n" << " default: Cost = ~0U >> 1; // Match failed\n" << " N->setPatternCostFor(" << SlotName << "_Slot, NoMatchPattern, Cost, NumSlots);\n" << " break;\n"; for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) if (!J->first->isSubClassOf("Nonterminal")) OS << " case ISD::" << getNodeName(J->first) << ":\tCost = Match_" << SlotName << "_" << getNodeName(J->first) << "(N); break;\n"; OS << " }\n"; // End of the switch statement // Emit any patterns which have a nonterminal leaf as the RHS. These may // match multiple root nodes, so they cannot be handled with the switch... for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) if (J->first->isSubClassOf("Nonterminal")) { OS << " unsigned " << J->first->getName() << "_Cost = Match_" << getNodeName(J->first) << "(N);\n" << " if (" << getNodeName(J->first) << "_Cost < Cost) Cost = " << getNodeName(J->first) << "_Cost;\n"; } OS << " return Cost;\n}\n\n"; for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) { Record *Operator = J->first; bool isNonterm = Operator->isSubClassOf("Nonterminal"); if (!isNonterm) { OS << "unsigned " << Target.getName() << "ISel::Match_"; if (!isNonterm) OS << SlotName << "_"; OS << getNodeName(Operator) << "(SelectionDAGNode *N) {\n" << " unsigned Pattern = NoMatchPattern;\n" << " unsigned MinCost = ~0U >> 1;\n"; std::vector<std::pair<Pattern*, TreePatternNode*> > Patterns; for (unsigned i = 0, e = J->second.size(); i != e; ++i) Patterns.push_back(std::make_pair(J->second[i], J->second[i]->getTree())); EmitMatchCosters(OS, Patterns, "N", 2); OS << "\n N->setPatternCostFor(" << SlotName << "_Slot, Pattern, MinCost, NumSlots);\n" << " return MinCost;\n" << "}\n"; } } } //===--------------------------------------------------------------------===// // Emit all of the reducer methods... // OS << "\n\n//===" << std::string(70, '-') << "===//\n" << "// Reducer methods...\n" << "//\n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { const std::string &SlotName = I->first; OS << "ReducedValue_" << SlotName << " *" << Target.getName() << "ISel::Reduce_" << SlotName << "(SelectionDAGNode *N, MachineBasicBlock *MBB) {\n" << " ReducedValue_" << SlotName << " *Val = N->hasValue<ReducedValue_" << SlotName << ">(" << SlotName << "_Slot);\n" << " if (Val) return Val;\n" << " if (N->getBB()) MBB = N->getBB();\n\n" << " switch (N->getPatternFor(" << SlotName << "_Slot)) {\n"; // Loop over all of the patterns that can produce a value for this slot... PatternOrganizer::NodesForSlot &NodesForSlot = I->second; for (PatternOrganizer::NodesForSlot::iterator J = NodesForSlot.begin(), E = NodesForSlot.end(); J != E; ++J) for (unsigned i = 0, e = J->second.size(); i != e; ++i) { Pattern *P = J->second[i]; OS << " case " << P->getRecord()->getName() << "_Pattern: {\n" << " // " << *P << "\n"; // Loop over the operands, reducing them... std::vector<std::pair<TreePatternNode*, std::string> > Operands; ReduceAllOperands(P->getTree(), "N", Operands, OS); // Now that we have reduced all of our operands, and have the values // that reduction produces, perform the reduction action for this // pattern. std::string Result; // If the pattern produces a register result, generate a new register // now. if (Record *R = P->getResult()) { assert(R->isSubClassOf("RegisterClass") && "Only handle register class results so far!"); OS << " unsigned NewReg = makeAnotherReg(" << Target.getName() << "::" << R->getName() << "RegisterClass);\n"; Result = "NewReg"; DEBUG(OS << " std::cerr << \"%reg\" << NewReg << \" =\t\";\n"); } else { DEBUG(OS << " std::cerr << \"\t\t\";\n"); Result = "0"; } // Print out the pattern that matched... DEBUG(OS << " std::cerr << \" " << P->getRecord()->getName() <<'"'); DEBUG(for (unsigned i = 0, e = Operands.size(); i != e; ++i) if (Operands[i].first->isLeaf()) { Record *RV = Operands[i].first->getValueRecord(); assert(RV->isSubClassOf("RegisterClass") && "Only handles registers here so far!"); OS << " << \" %reg\" << " << Operands[i].second << "->Val"; } else { OS << " << ' ' << " << Operands[i].second << "->Val"; }); DEBUG(OS << " << \"\\n\";\n"); // Generate the reduction code appropriate to the particular type of // pattern that this is... switch (P->getPatternType()) { case Pattern::Instruction: // Instruction patterns just emit a single MachineInstr, using BuildMI OS << " BuildMI(MBB, " << Target.getName() << "::" << P->getRecord()->getName() << ", " << Operands.size(); if (P->getResult()) OS << ", NewReg"; OS << ")"; for (unsigned i = 0, e = Operands.size(); i != e; ++i) { TreePatternNode *Op = Operands[i].first; if (Op->isLeaf()) { Record *RV = Op->getValueRecord(); assert(RV->isSubClassOf("RegisterClass") && "Only handles registers here so far!"); OS << ".addReg(" << Operands[i].second << "->Val)"; } else if (Op->getOperator()->getName() == "imm") { OS << ".addZImm(" << Operands[i].second << "->Val)"; } else if (Op->getOperator()->getName() == "basicblock") { OS << ".addMBB(" << Operands[i].second << "->Val)"; } else { assert(0 && "Unknown value type!"); } } OS << ";\n"; break; case Pattern::Expander: { // Expander patterns emit one machine instr for each instruction in // the list of instructions expanded to. ListInit *Insts = P->getRecord()->getValueAsListInit("Result"); for (unsigned IN = 0, e = Insts->getSize(); IN != e; ++IN) { DagInit *DIInst = dynamic_cast<DagInit*>(Insts->getElement(IN)); if (!DIInst) P->error("Result list must contain instructions!"); Record *InstRec = DIInst->getNodeType(); Pattern *InstPat = getPattern(InstRec); if (!InstPat || InstPat->getPatternType() != Pattern::Instruction) P->error("Instruction list must contain Instruction patterns!"); bool hasResult = InstPat->getResult() != 0; if (InstPat->getNumArgs() != DIInst->getNumArgs()-hasResult) { P->error("Incorrect number of arguments specified for inst '" + InstPat->getRecord()->getName() + "' in result list!"); } // Start emission of the instruction... OS << " BuildMI(MBB, " << Target.getName() << "::" << InstRec->getName() << ", " << DIInst->getNumArgs()-hasResult; // Emit register result if necessary.. if (hasResult) { std::string ArgNameVal = getArgName(P, DIInst->getArgName(0), Operands); PrintExpanderOperand(DIInst->getArg(0), ArgNameVal, InstPat->getResultNode(), P, false, OS << ", "); } OS << ")"; for (unsigned i = hasResult, e = DIInst->getNumArgs(); i != e; ++i){ std::string ArgNameVal = getArgName(P, DIInst->getArgName(i), Operands); PrintExpanderOperand(DIInst->getArg(i), ArgNameVal, InstPat->getArg(i-hasResult), P, true, OS); } OS << ";\n"; } break; } default: assert(0 && "Reduction of this type of pattern not implemented!"); } OS << " Val = new ReducedValue_" << SlotName << "(" << Result<<");\n" << " break;\n" << " }\n"; } OS << " default: assert(0 && \"Unknown " << SlotName << " pattern!\");\n" << " }\n\n N->addValue(Val); // Do not ever recalculate this\n" << " return Val;\n}\n\n"; }