- // Print all declarations.
- for (std::set<std::pair<bool, std::string> >::iterator
- I = GeneratedDecl.begin(), E = GeneratedDecl.end(); I != E; ++I)
- if (I->first)
- OS << " SDNode *" << I->second << ";\n";
- else
- OS << " SDOperand " << I->second << "(0, 0);\n";
-
- // Loop through and reverse all of the CodeList vectors, as we will be
- // accessing them from their logical front, but accessing the end of a
- // vector is more efficient.
- for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
- CodeList &GeneratedCode = CodeForPatterns[i].second;
- std::reverse(GeneratedCode.begin(), GeneratedCode.end());
- }
-
- // Next, reverse the list of patterns itself for the same reason.
- std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
-
- // Emit all of the patterns now, grouped together to share code.
- EmitPatterns(CodeForPatterns, 2, OS);
-
- // If the last pattern has predicates (which could fail) emit code to catch
- // the case where nothing handles a pattern.
- if (mightNotMatch) {
- OS << " std::cerr << \"Cannot yet select: \";\n";
- if (OpcodeInfo.getEnumName() != "ISD::INTRINSIC_W_CHAIN" &&
- OpcodeInfo.getEnumName() != "ISD::INTRINSIC_WO_CHAIN" &&
- OpcodeInfo.getEnumName() != "ISD::INTRINSIC_VOID") {
- OS << " N.Val->dump(CurDAG);\n";
- } else {
- OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
- "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
- << " std::cerr << \"intrinsic %\"<< "
- "Intrinsic::getName((Intrinsic::ID)iid);\n";
+ // Factor target node emission code (emitted by EmitResultCode) into
+ // separate functions. Uniquing and share them among all instruction
+ // selection routines.
+ for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
+ CodeList &GeneratedCode = CodeForPatterns[i].second;
+ std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
+ std::vector<std::string> &TargetVTs = PatternVTs[i];
+ std::set<std::string> Decls = PatternDecls[i];
+ std::vector<std::string> AddedInits;
+ int CodeSize = (int)GeneratedCode.size();
+ int LastPred = -1;
+ for (int j = CodeSize-1; j >= 0; --j) {
+ if (LastPred == -1 && GeneratedCode[j].first == 1)
+ LastPred = j;
+ else if (LastPred != -1 && GeneratedCode[j].first == 2)
+ AddedInits.push_back(GeneratedCode[j].second);
+ }
+
+ std::string CalleeCode = "(const SDOperand &N";
+ std::string CallerCode = "(N";
+ for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
+ CalleeCode += ", unsigned Opc" + utostr(j);
+ CallerCode += ", " + TargetOpcodes[j];
+ }
+ for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
+ CalleeCode += ", MVT::ValueType VT" + utostr(j);
+ CallerCode += ", " + TargetVTs[j];
+ }
+ for (std::set<std::string>::iterator
+ I = Decls.begin(), E = Decls.end(); I != E; ++I) {
+ std::string Name = *I;
+ CalleeCode += ", SDOperand &" + Name;
+ CallerCode += ", " + Name;
+ }
+ CallerCode += ");";
+ CalleeCode += ") ";
+ // Prevent emission routines from being inlined to reduce selection
+ // routines stack frame sizes.
+ CalleeCode += "DISABLE_INLINE ";
+ CalleeCode += "{\n";
+
+ for (std::vector<std::string>::const_reverse_iterator
+ I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
+ CalleeCode += " " + *I + "\n";
+
+ for (int j = LastPred+1; j < CodeSize; ++j)
+ CalleeCode += " " + GeneratedCode[j].second + "\n";
+ for (int j = LastPred+1; j < CodeSize; ++j)
+ GeneratedCode.pop_back();
+ CalleeCode += "}\n";
+
+ // Uniquing the emission routines.
+ unsigned EmitFuncNum;
+ std::map<std::string, unsigned>::iterator EFI =
+ EmitFunctions.find(CalleeCode);
+ if (EFI != EmitFunctions.end()) {
+ EmitFuncNum = EFI->second;
+ } else {
+ EmitFuncNum = EmitFunctions.size();
+ EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
+ OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
+ }
+
+ // Replace the emission code within selection routines with calls to the
+ // emission functions.
+ CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
+ GeneratedCode.push_back(std::make_pair(false, CallerCode));
+ }
+
+ // Print function.
+ std::string OpVTStr = (OpVT != MVT::isVoid && OpVT != MVT::iPTR)
+ ? getEnumName(OpVT).substr(5) : "" ;
+ std::map<std::string, std::vector<std::string> >::iterator OpVTI =
+ OpcodeVTMap.find(OpName);
+ if (OpVTI == OpcodeVTMap.end()) {
+ std::vector<std::string> VTSet;
+ VTSet.push_back(OpVTStr);
+ OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
+ } else
+ OpVTI->second.push_back(OpVTStr);
+
+ OS << "SDNode *Select_" << OpName << (OpVTStr != "" ? "_" : "")
+ << OpVTStr << "(const SDOperand &N) {\n";
+
+ // Loop through and reverse all of the CodeList vectors, as we will be
+ // accessing them from their logical front, but accessing the end of a
+ // vector is more efficient.
+ for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
+ CodeList &GeneratedCode = CodeForPatterns[i].second;
+ std::reverse(GeneratedCode.begin(), GeneratedCode.end());
+ }
+
+ // Next, reverse the list of patterns itself for the same reason.
+ std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
+
+ // Emit all of the patterns now, grouped together to share code.
+ EmitPatterns(CodeForPatterns, 2, OS);
+
+ // If the last pattern has predicates (which could fail) emit code to
+ // catch the case where nothing handles a pattern.
+ if (mightNotMatch) {
+ OS << " std::cerr << \"Cannot yet select: \";\n";
+ if (OpcodeInfo.getEnumName() != "ISD::INTRINSIC_W_CHAIN" &&
+ OpcodeInfo.getEnumName() != "ISD::INTRINSIC_WO_CHAIN" &&
+ OpcodeInfo.getEnumName() != "ISD::INTRINSIC_VOID") {
+ OS << " N.Val->dump(CurDAG);\n";
+ } else {
+ OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
+ "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
+ << " std::cerr << \"intrinsic %\"<< "
+ "Intrinsic::getName((Intrinsic::ID)iid);\n";
+ }
+ OS << " std::cerr << '\\n';\n"
+ << " abort();\n"
+ << " return NULL;\n";