DAGISelEmitter.cpp (193323) | DAGISelEmitter.cpp (195340) |
---|---|
1//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 4 unchanged lines hidden (view full) --- 13 14#include "DAGISelEmitter.h" 15#include "Record.h" 16#include "llvm/ADT/StringExtras.h" 17#include "llvm/Support/CommandLine.h" 18#include "llvm/Support/Debug.h" 19#include "llvm/Support/MathExtras.h" 20#include "llvm/Support/Debug.h" | 1//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 4 unchanged lines hidden (view full) --- 13 14#include "DAGISelEmitter.h" 15#include "Record.h" 16#include "llvm/ADT/StringExtras.h" 17#include "llvm/Support/CommandLine.h" 18#include "llvm/Support/Debug.h" 19#include "llvm/Support/MathExtras.h" 20#include "llvm/Support/Debug.h" |
21#include "llvm/Support/Streams.h" | |
22#include <algorithm> 23#include <deque> | 21#include <algorithm> 22#include <deque> |
23#include <iostream> |
|
24using namespace llvm; 25 26namespace { 27 cl::opt<bool> 28 GenDebug("gen-debug", cl::desc("Generate debug code"), 29 cl::init(false)); 30} 31 --- 232 unchanged lines hidden (view full) --- 264 } 265 } 266 return HasChain; 267} 268 269//===----------------------------------------------------------------------===// 270// Node Transformation emitter implementation. 271// | 24using namespace llvm; 25 26namespace { 27 cl::opt<bool> 28 GenDebug("gen-debug", cl::desc("Generate debug code"), 29 cl::init(false)); 30} 31 --- 232 unchanged lines hidden (view full) --- 264 } 265 } 266 return HasChain; 267} 268 269//===----------------------------------------------------------------------===// 270// Node Transformation emitter implementation. 271// |
272void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) { | 272void DAGISelEmitter::EmitNodeTransforms(raw_ostream &OS) { |
273 // Walk the pattern fragments, adding them to a map, which sorts them by 274 // name. 275 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy; 276 NXsByNameTy NXsByName; 277 278 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end(); 279 I != E; ++I) 280 NXsByName.insert(std::make_pair(I->first->getName(), I->second)); --- 17 unchanged lines hidden (view full) --- 298 OS << Code << "\n}\n"; 299 } 300} 301 302//===----------------------------------------------------------------------===// 303// Predicate emitter implementation. 304// 305 | 273 // Walk the pattern fragments, adding them to a map, which sorts them by 274 // name. 275 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy; 276 NXsByNameTy NXsByName; 277 278 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end(); 279 I != E; ++I) 280 NXsByName.insert(std::make_pair(I->first->getName(), I->second)); --- 17 unchanged lines hidden (view full) --- 298 OS << Code << "\n}\n"; 299 } 300} 301 302//===----------------------------------------------------------------------===// 303// Predicate emitter implementation. 304// 305 |
306void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) { | 306void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) { |
307 OS << "\n// Predicate functions.\n"; 308 309 // Walk the pattern fragments, adding them to a map, which sorts them by 310 // name. 311 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy; 312 PFsByNameTy PFsByName; 313 314 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end(); --- 431 unchanged lines hidden (view full) --- 746 ")->getVT() == MVT::" + LeafRec->getName()); 747 } else if (LeafRec->isSubClassOf("CondCode")) { 748 // Make sure this is the specified cond code. 749 emitCheck("cast<CondCodeSDNode>(" + RootName + 750 ")->get() == ISD::" + LeafRec->getName()); 751 } else { 752#ifndef NDEBUG 753 Child->dump(); | 307 OS << "\n// Predicate functions.\n"; 308 309 // Walk the pattern fragments, adding them to a map, which sorts them by 310 // name. 311 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy; 312 PFsByNameTy PFsByName; 313 314 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end(); --- 431 unchanged lines hidden (view full) --- 746 ")->getVT() == MVT::" + LeafRec->getName()); 747 } else if (LeafRec->isSubClassOf("CondCode")) { 748 // Make sure this is the specified cond code. 749 emitCheck("cast<CondCodeSDNode>(" + RootName + 750 ")->get() == ISD::" + LeafRec->getName()); 751 } else { 752#ifndef NDEBUG 753 Child->dump(); |
754 cerr << " "; | 754 errs() << " "; |
755#endif 756 assert(0 && "Unknown leaf type!"); 757 } 758 759 // If there are node predicates for this, emit the calls. 760 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i) 761 emitCheck(Child->getPredicateFns()[i] + "(" + RootName + 762 ".getNode())"); --- 27 unchanged lines hidden (view full) --- 790 // List of arguments of getTargetNode() or SelectNodeTo(). 791 std::vector<std::string> NodeOps; 792 // This is something selected from the pattern we matched. 793 if (!N->getName().empty()) { 794 const std::string &VarName = N->getName(); 795 std::string Val = VariableMap[VarName]; 796 bool ModifiedVal = false; 797 if (Val.empty()) { | 755#endif 756 assert(0 && "Unknown leaf type!"); 757 } 758 759 // If there are node predicates for this, emit the calls. 760 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i) 761 emitCheck(Child->getPredicateFns()[i] + "(" + RootName + 762 ".getNode())"); --- 27 unchanged lines hidden (view full) --- 790 // List of arguments of getTargetNode() or SelectNodeTo(). 791 std::vector<std::string> NodeOps; 792 // This is something selected from the pattern we matched. 793 if (!N->getName().empty()) { 794 const std::string &VarName = N->getName(); 795 std::string Val = VariableMap[VarName]; 796 bool ModifiedVal = false; 797 if (Val.empty()) { |
798 cerr << "Variable '" << VarName << " referenced but not defined " | 798 errs() << "Variable '" << VarName << " referenced but not defined " |
799 << "and not caught earlier!\n"; 800 abort(); 801 } 802 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') { 803 // Already selected this operand, just return the tmpval. 804 NodeOps.push_back(Val); 805 return NodeOps; 806 } 807 808 const ComplexPattern *CP; 809 unsigned ResNo = TmpNo++; 810 if (!N->isLeaf() && N->getOperator()->getName() == "imm") { 811 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); 812 std::string CastType; 813 std::string TmpVar = "Tmp" + utostr(ResNo); 814 switch (N->getTypeNum(0)) { 815 default: | 799 << "and not caught earlier!\n"; 800 abort(); 801 } 802 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') { 803 // Already selected this operand, just return the tmpval. 804 NodeOps.push_back(Val); 805 return NodeOps; 806 } 807 808 const ComplexPattern *CP; 809 unsigned ResNo = TmpNo++; 810 if (!N->isLeaf() && N->getOperator()->getName() == "imm") { 811 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); 812 std::string CastType; 813 std::string TmpVar = "Tmp" + utostr(ResNo); 814 switch (N->getTypeNum(0)) { 815 default: |
816 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0)) | 816 errs() << "Cannot handle " << getEnumName(N->getTypeNum(0)) |
817 << " type as an immediate constant. Aborting\n"; 818 abort(); 819 case MVT::i1: CastType = "bool"; break; 820 case MVT::i8: CastType = "unsigned char"; break; 821 case MVT::i16: CastType = "unsigned short"; break; 822 case MVT::i32: CastType = "unsigned"; break; 823 case MVT::i64: CastType = "uint64_t"; break; 824 } --- 521 unchanged lines hidden (view full) --- 1346 + "(" + Ops.back() + ".getNode());"); 1347 NodeOps.push_back("Tmp" + utostr(ResNo)); 1348 if (isRoot) 1349 emitCode("return Tmp" + utostr(ResNo) + ".getNode();"); 1350 return NodeOps; 1351 } 1352 1353 N->dump(); | 817 << " type as an immediate constant. Aborting\n"; 818 abort(); 819 case MVT::i1: CastType = "bool"; break; 820 case MVT::i8: CastType = "unsigned char"; break; 821 case MVT::i16: CastType = "unsigned short"; break; 822 case MVT::i32: CastType = "unsigned"; break; 823 case MVT::i64: CastType = "uint64_t"; break; 824 } --- 521 unchanged lines hidden (view full) --- 1346 + "(" + Ops.back() + ".getNode());"); 1347 NodeOps.push_back("Tmp" + utostr(ResNo)); 1348 if (isRoot) 1349 emitCode("return Tmp" + utostr(ResNo) + ".getNode();"); 1350 return NodeOps; 1351 } 1352 1353 N->dump(); |
1354 cerr << "\n"; | 1354 errs() << "\n"; |
1355 throw std::string("Unknown node in result pattern!"); 1356 } 1357 1358 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat' 1359 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that 1360 /// 'Pat' may be missing types. If we find an unresolved type to add a check 1361 /// for, this returns true otherwise false if Pat has all types. 1362 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other, --- 169 unchanged lines hidden (view full) --- 1532 return ErasedPatterns; 1533} 1534 1535/// EmitPatterns - Emit code for at least one pattern, but try to group common 1536/// code together between the patterns. 1537void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*, 1538 std::vector<std::pair<unsigned, std::string> > > > 1539 &Patterns, unsigned Indent, | 1355 throw std::string("Unknown node in result pattern!"); 1356 } 1357 1358 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat' 1359 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that 1360 /// 'Pat' may be missing types. If we find an unresolved type to add a check 1361 /// for, this returns true otherwise false if Pat has all types. 1362 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other, --- 169 unchanged lines hidden (view full) --- 1532 return ErasedPatterns; 1533} 1534 1535/// EmitPatterns - Emit code for at least one pattern, but try to group common 1536/// code together between the patterns. 1537void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*, 1538 std::vector<std::pair<unsigned, std::string> > > > 1539 &Patterns, unsigned Indent, |
1540 std::ostream &OS) { | 1540 raw_ostream &OS) { |
1541 typedef std::pair<unsigned, std::string> CodeLine; 1542 typedef std::vector<CodeLine> CodeList; 1543 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList; 1544 1545 if (Patterns.empty()) return; 1546 1547 // Figure out how many patterns share the next code line. Explicitly copy 1548 // FirstCodeLine so that we don't invalidate a reference when changing --- 98 unchanged lines hidden (view full) --- 1647 1648static std::string getLegalCName(std::string OpName) { 1649 std::string::size_type pos = OpName.find("::"); 1650 if (pos != std::string::npos) 1651 OpName.replace(pos, 2, "_"); 1652 return OpName; 1653} 1654 | 1541 typedef std::pair<unsigned, std::string> CodeLine; 1542 typedef std::vector<CodeLine> CodeList; 1543 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList; 1544 1545 if (Patterns.empty()) return; 1546 1547 // Figure out how many patterns share the next code line. Explicitly copy 1548 // FirstCodeLine so that we don't invalidate a reference when changing --- 98 unchanged lines hidden (view full) --- 1647 1648static std::string getLegalCName(std::string OpName) { 1649 std::string::size_type pos = OpName.find("::"); 1650 if (pos != std::string::npos) 1651 OpName.replace(pos, 2, "_"); 1652 return OpName; 1653} 1654 |
1655void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { | 1655void DAGISelEmitter::EmitInstructionSelector(raw_ostream &OS) { |
1656 const CodeGenTarget &Target = CGP.getTargetInfo(); 1657 1658 // Get the namespace to insert instructions into. 1659 std::string InstNS = Target.getInstNamespace(); 1660 if (!InstNS.empty()) InstNS += "::"; 1661 1662 // Group the patterns by their top-level opcodes. 1663 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode; --- 15 unchanged lines hidden (view full) --- 1679 } else if ((CP = NodeGetComplexPattern(Node, CGP))) { 1680 std::vector<Record*> OpNodes = CP->getRootNodes(); 1681 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) { 1682 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)] 1683 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(), 1684 &Pattern); 1685 } 1686 } else { | 1656 const CodeGenTarget &Target = CGP.getTargetInfo(); 1657 1658 // Get the namespace to insert instructions into. 1659 std::string InstNS = Target.getInstNamespace(); 1660 if (!InstNS.empty()) InstNS += "::"; 1661 1662 // Group the patterns by their top-level opcodes. 1663 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode; --- 15 unchanged lines hidden (view full) --- 1679 } else if ((CP = NodeGetComplexPattern(Node, CGP))) { 1680 std::vector<Record*> OpNodes = CP->getRootNodes(); 1681 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) { 1682 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)] 1683 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(), 1684 &Pattern); 1685 } 1686 } else { |
1687 cerr << "Unrecognized opcode '"; | 1687 errs() << "Unrecognized opcode '"; |
1688 Node->dump(); | 1688 Node->dump(); |
1689 cerr << "' on tree pattern '"; 1690 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n"; | 1689 errs() << "' on tree pattern '"; 1690 errs() << Pattern.getDstPattern()->getOperator()->getName() << "'!\n"; |
1691 exit(1); 1692 } 1693 } 1694 } 1695 1696 // For each opcode, there might be multiple select functions, one per 1697 // ValueType of the node (or its first operand if it doesn't produce a 1698 // non-chain result. --- 180 unchanged lines hidden (view full) --- 1879 mightNotMatch = true; 1880 break; 1881 } 1882 } 1883 1884 // If this pattern definitely matches, and if it isn't the last one, the 1885 // patterns after it CANNOT ever match. Error out. 1886 if (mightNotMatch == false && i != CodeForPatterns.size()-1) { | 1691 exit(1); 1692 } 1693 } 1694 } 1695 1696 // For each opcode, there might be multiple select functions, one per 1697 // ValueType of the node (or its first operand if it doesn't produce a 1698 // non-chain result. --- 180 unchanged lines hidden (view full) --- 1879 mightNotMatch = true; 1880 break; 1881 } 1882 } 1883 1884 // If this pattern definitely matches, and if it isn't the last one, the 1885 // patterns after it CANNOT ever match. Error out. 1886 if (mightNotMatch == false && i != CodeForPatterns.size()-1) { |
1887 cerr << "Pattern '"; 1888 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream()); 1889 cerr << "' is impossible to select!\n"; | 1887 errs() << "Pattern '"; 1888 CodeForPatterns[i].first->getSrcPattern()->print(errs()); 1889 errs() << "' is impossible to select!\n"; |
1890 exit(1); 1891 } 1892 } 1893 1894 // Loop through and reverse all of the CodeList vectors, as we will be 1895 // accessing them from their logical front, but accessing the end of a 1896 // vector is more efficient. 1897 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { --- 197 unchanged lines hidden (view full) --- 2095 << "N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();\n" 2096 << " cerr << \"intrinsic %\"<< " 2097 << "Intrinsic::getName((Intrinsic::ID)iid);\n" 2098 << " cerr << '\\n';\n" 2099 << " abort();\n" 2100 << "}\n\n"; 2101} 2102 | 1890 exit(1); 1891 } 1892 } 1893 1894 // Loop through and reverse all of the CodeList vectors, as we will be 1895 // accessing them from their logical front, but accessing the end of a 1896 // vector is more efficient. 1897 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { --- 197 unchanged lines hidden (view full) --- 2095 << "N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();\n" 2096 << " cerr << \"intrinsic %\"<< " 2097 << "Intrinsic::getName((Intrinsic::ID)iid);\n" 2098 << " cerr << '\\n';\n" 2099 << " abort();\n" 2100 << "}\n\n"; 2101} 2102 |
2103void DAGISelEmitter::run(std::ostream &OS) { | 2103void DAGISelEmitter::run(raw_ostream &OS) { |
2104 EmitSourceFileHeader("DAG Instruction Selector for the " + 2105 CGP.getTargetInfo().getName() + " target", OS); 2106 2107 OS << "// *** NOTE: This file is #included into the middle of the target\n" 2108 << "// *** instruction selector class. These functions are really " 2109 << "methods.\n\n"; 2110 2111 OS << "// Include standard, target-independent definitions and methods used\n" --- 20 unchanged lines hidden --- | 2104 EmitSourceFileHeader("DAG Instruction Selector for the " + 2105 CGP.getTargetInfo().getName() + " target", OS); 2106 2107 OS << "// *** NOTE: This file is #included into the middle of the target\n" 2108 << "// *** instruction selector class. These functions are really " 2109 << "methods.\n\n"; 2110 2111 OS << "// Include standard, target-independent definitions and methods used\n" --- 20 unchanged lines hidden --- |