llvm.org GIT mirror llvm / 9911901
Allow separation of declarations and definitions in <Target>ISelDAGToDAG.inc This patch adds the ability to include the member function declarations in the instruction selector class separately from the member bodies. Defining GET_DAGISEL_DECL macro to any value will only include the member declarations. To include bodies, define GET_DAGISEL_BODY macro to be the selector class name. Example: class FooDAGToDAGISel : public SelectionDAGISel { // Pull in declarations only. #define GET_DAGISEL_DECL #include "FooISelDAGToDAG.inc" }; // Include the function bodies (with names qualified with the provided // class name). #define GET_DAGISEL_BODY FooDAGToDAGISel #include "FooISelDAGToDAG.inc" When neither of the two macros are defined, the function bodies are emitted inline (in the same way as before this patch). Differential Revision: https://reviews.llvm.org/D39596 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317903 91177308-0d34-0410-b5e6-96231b3b80d8 Krzysztof Parzyszek 1 year, 11 months ago
2 changed file(s) with 114 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
126126 << "// *** instruction selector class. These functions are really "
127127 << "methods.\n\n";
128128
129 OS << "// If GET_DAGISEL_DECL is #defined with any value, only function\n"
130 "// declarations will be included when this file is included.\n"
131 "// If GET_DAGISEL_BODY is #defined, its value should be the name of\n"
132 "// the instruction selector class. Function bodies will be emitted\n"
133 "// and each function's name will be qualified with the name of the\n"
134 "// class.\n"
135 "//\n"
136 "// When neither of the GET_DAGISEL* macros is defined, the functions\n"
137 "// are emitted inline.\n\n";
138
129139 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
130140 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
131141 E = CGP.ptm_end(); I != E; ++I) {
207207 return str;
208208 }
209209
210 static void BeginEmitFunction(raw_ostream &OS, StringRef RetType,
211 StringRef Decl, bool AddOverride) {
212 OS << "#ifdef GET_DAGISEL_DECL\n";
213 OS << RetType << ' ' << Decl;
214 if (AddOverride)
215 OS << " override";
216 OS << ";\n"
217 "#endif\n"
218 "#if defined(GET_DAGISEL_BODY) || DAGISEL_INLINE\n";
219 OS << RetType << " DAGISEL_CLASS_COLONCOLON " << Decl << "\n";
220 if (AddOverride) {
221 OS << "#if DAGISEL_INLINE\n"
222 " override\n"
223 "#endif\n";
224 }
225 }
226
227 static void EndEmitFunction(raw_ostream &OS) {
228 OS << "#endif // GET_DAGISEL_BODY\n\n";
229 }
230
210231 void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
211232
212233 assert(isUInt<16>(VecPatterns.size()) &&
213234 "Using only 16 bits to encode offset into Pattern Table");
214235 assert(VecPatterns.size() == VecIncludeStrings.size() &&
215236 "The sizes of Pattern and include vectors should be the same");
216 OS << "StringRef getPatternForIndex(unsigned Index) override {\n";
237
238 BeginEmitFunction(OS, "StringRef", "getPatternForIndex(unsigned Index)",
239 true/*AddOverride*/);
240 OS << "{\n";
217241 OS << "static const char * PATTERN_MATCH_TABLE[] = {\n";
218242
219243 for (const auto &It : VecPatterns) {
223247 OS << "\n};";
224248 OS << "\nreturn StringRef(PATTERN_MATCH_TABLE[Index]);";
225249 OS << "\n}";
226
227 OS << "\nStringRef getIncludePathForIndex(unsigned Index) override {\n";
250 EndEmitFunction(OS);
251
252 BeginEmitFunction(OS, "StringRef", "getIncludePathForIndex(unsigned Index)",
253 true/*AddOverride*/);
254 OS << "{\n";
228255 OS << "static const char * INCLUDE_PATH_TABLE[] = {\n";
229256
230257 for (const auto &It : VecIncludeStrings) {
234261 OS << "\n};";
235262 OS << "\nreturn StringRef(INCLUDE_PATH_TABLE[Index]);";
236263 OS << "\n}";
264 EndEmitFunction(OS);
237265 }
238266
239267 /// EmitMatcher - Emit bytes for the specified matcher and return
754782 void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
755783 // Emit pattern predicates.
756784 if (!PatternPredicates.empty()) {
757 OS << "bool CheckPatternPredicate(unsigned PredNo) const override {\n";
785 BeginEmitFunction(OS, "bool",
786 "CheckPatternPredicate(unsigned PredNo) const", true/*AddOverride*/);
787 OS << "{\n";
758788 OS << " switch (PredNo) {\n";
759789 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
760790 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
761791 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
762792 OS << " }\n";
763 OS << "}\n\n";
793 OS << "}\n";
794 EndEmitFunction(OS);
764795 }
765796
766797 // Emit Node predicates.
767798 if (!NodePredicates.empty()) {
768 OS << "bool CheckNodePredicate(SDNode *Node,\n";
769 OS << " unsigned PredNo) const override {\n";
799 BeginEmitFunction(OS, "bool",
800 "CheckNodePredicate(SDNode *Node, unsigned PredNo) const",
801 true/*AddOverride*/);
802 OS << "{\n";
770803 OS << " switch (PredNo) {\n";
771804 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
772805 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
782815 OS << PredFn.getCodeToRunOnSDNode() << "\n }\n";
783816 }
784817 OS << " }\n";
785 OS << "}\n\n";
818 OS << "}\n";
819 EndEmitFunction(OS);
786820 }
787821
788822 // Emit CompletePattern matchers.
789823 // FIXME: This should be const.
790824 if (!ComplexPatterns.empty()) {
791 OS << "bool CheckComplexPattern(SDNode *Root, SDNode *Parent,\n";
792 OS << " SDValue N, unsigned PatternNo,\n";
793 OS << " SmallVectorImpl > &Result) override {\n";
825 BeginEmitFunction(OS, "bool",
826 "CheckComplexPattern(SDNode *Root, SDNode *Parent,\n"
827 " SDValue N, unsigned PatternNo,\n"
828 " SmallVectorImpl> &Result)",
829 true/*AddOverride*/);
830 OS << "{\n";
794831 OS << " unsigned NextRes = Result.size();\n";
795832 OS << " switch (PatternNo) {\n";
796833 OS << " default: llvm_unreachable(\"Invalid pattern # in table?\");\n";
834871 }
835872 }
836873 OS << " }\n";
837 OS << "}\n\n";
874 OS << "}\n";
875 EndEmitFunction(OS);
838876 }
839877
840878
841879 // Emit SDNodeXForm handlers.
842880 // FIXME: This should be const.
843881 if (!NodeXForms.empty()) {
844 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) override {\n";
882 BeginEmitFunction(OS, "SDValue",
883 "RunSDNodeXForm(SDValue V, unsigned XFormNo)", true/*AddOverride*/);
884 OS << "{\n";
845885 OS << " switch (XFormNo) {\n";
846886 OS << " default: llvm_unreachable(\"Invalid xform # in table?\");\n";
847887
867907 OS << Code << "\n }\n";
868908 }
869909 OS << " }\n";
870 OS << "}\n\n";
910 OS << "}\n";
911 EndEmitFunction(OS);
871912 }
872913 }
873914
957998 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
958999 const CodeGenDAGPatterns &CGP,
9591000 raw_ostream &OS) {
960 OS << "// The main instruction selector code.\n";
961 OS << "void SelectCode(SDNode *N) {\n";
962
1001 OS << "#if defined(GET_DAGISEL_DECL) && defined(GET_DAGISEL_BODY)\n";
1002 OS << "#error GET_DAGISEL_DECL and GET_DAGISEL_BODY cannot be both defined, ";
1003 OS << "undef both for inline definitions\n";
1004 OS << "#endif\n\n";
1005
1006 // Emit a check for omitted class name.
1007 OS << "#ifdef GET_DAGISEL_BODY\n";
1008 OS << "#define LOCAL_DAGISEL_STRINGIZE(X) LOCAL_DAGISEL_STRINGIZE_(X)\n";
1009 OS << "#define LOCAL_DAGISEL_STRINGIZE_(X) #X\n";
1010 OS << "static_assert(sizeof(LOCAL_DAGISEL_STRINGIZE(GET_DAGISEL_BODY)) > 1,"
1011 "\n";
1012 OS << " \"GET_DAGISEL_BODY is empty: it should be defined with the class "
1013 "name\");\n";
1014 OS << "#undef LOCAL_DAGISEL_STRINGIZE_\n";
1015 OS << "#undef LOCAL_DAGISEL_STRINGIZE\n";
1016 OS << "#endif\n\n";
1017
1018 OS << "#if !defined(GET_DAGISEL_DECL) && !defined(GET_DAGISEL_BODY)\n";
1019 OS << "#define DAGISEL_INLINE 1\n";
1020 OS << "#else\n";
1021 OS << "#define DAGISEL_INLINE 0\n";
1022 OS << "#endif\n\n";
1023
1024 OS << "#if !DAGISEL_INLINE\n";
1025 OS << "#define DAGISEL_CLASS_COLONCOLON GET_DAGISEL_BODY ::\n";
1026 OS << "#else\n";
1027 OS << "#define DAGISEL_CLASS_COLONCOLON\n";
1028 OS << "#endif\n\n";
1029
1030 BeginEmitFunction(OS, "void", "SelectCode(SDNode *N)", false/*AddOverride*/);
9631031 MatcherTableEmitter MatcherEmitter(CGP);
9641032
1033 OS << "{\n";
9651034 OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
9661035 OS << " // this.\n";
9671036 OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
9731042
9741043 OS << " #undef TARGET_VAL\n";
9751044 OS << " SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n";
976 OS << "}\n\n";
1045 OS << "}\n";
1046 EndEmitFunction(OS);
9771047
9781048 // Next up, emit the function for node and pattern predicates:
9791049 MatcherEmitter.EmitPredicateFunctions(OS);
9801050
9811051 if (InstrumentCoverage)
9821052 MatcherEmitter.EmitPatternMatchTable(OS);
983 }
1053
1054 // Clean up the preprocessor macros.
1055 OS << "\n";
1056 OS << "#ifdef DAGISEL_INLINE\n";
1057 OS << "#undef DAGISEL_INLINE\n";
1058 OS << "#endif\n";
1059 OS << "#ifdef DAGISEL_CLASS_COLONCOLON\n";
1060 OS << "#undef DAGISEL_CLASS_COLONCOLON\n";
1061 OS << "#endif\n";
1062 OS << "#ifdef GET_DAGISEL_DECL\n";
1063 OS << "#undef GET_DAGISEL_DECL\n";
1064 OS << "#endif\n";
1065 OS << "#ifdef GET_DAGISEL_BODY\n";
1066 OS << "#undef GET_DAGISEL_BODY\n";
1067 OS << "#endif\n";
1068 }