Move CodeGenIntrinsic implementation to CodeGenTarget.cpp with the rest of
[oota-llvm.git] / utils / TableGen / IntrinsicEmitter.cpp
1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits information about intrinsic functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "IntrinsicEmitter.h"
15 #include "Record.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include <algorithm>
18 using namespace llvm;
19
20 //===----------------------------------------------------------------------===//
21 // IntrinsicEmitter Implementation
22 //===----------------------------------------------------------------------===//
23
24 void IntrinsicEmitter::run(std::ostream &OS) {
25   EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
26   
27   std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
28
29   // Emit the enum information.
30   EmitEnumInfo(Ints, OS);
31
32   // Emit the intrinsic ID -> name table.
33   EmitIntrinsicToNameTable(Ints, OS);
34   
35   // Emit the function name recognizer.
36   EmitFnNameRecognizer(Ints, OS);
37   
38   // Emit the intrinsic verifier.
39   EmitVerifier(Ints, OS);
40   
41   // Emit mod/ref info for each function.
42   EmitModRefInfo(Ints, OS);
43   
44   // Emit table of non-memory accessing intrinsics.
45   EmitNoMemoryInfo(Ints, OS);
46   
47   // Emit side effect info for each intrinsic.
48   EmitSideEffectInfo(Ints, OS);
49
50   // Emit a list of intrinsics with corresponding GCC builtins.
51   EmitGCCBuiltinList(Ints, OS);
52
53   // Emit code to translate GCC builtins into LLVM intrinsics.
54   EmitIntrinsicToGCCBuiltinMap(Ints, OS);
55 }
56
57 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
58                                     std::ostream &OS) {
59   OS << "// Enum values for Intrinsics.h\n";
60   OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
61   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
62     OS << "    " << Ints[i].EnumName;
63     OS << ((i != e-1) ? ", " : "  ");
64     OS << std::string(40-Ints[i].EnumName.size(), ' ') 
65       << "// " << Ints[i].Name << "\n";
66   }
67   OS << "#endif\n\n";
68 }
69
70 void IntrinsicEmitter::
71 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, 
72                      std::ostream &OS) {
73   // Build a function name -> intrinsic name mapping.
74   std::map<std::string, std::string> IntMapping;
75   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
76     IntMapping[Ints[i].Name] = Ints[i].EnumName;
77     
78   OS << "// Function name -> enum value recognizer code.\n";
79   OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
80   OS << "  switch (Name[5]) {\n";
81   OS << "  default: break;\n";
82   // Emit the intrinsics in sorted order.
83   char LastChar = 0;
84   for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
85        E = IntMapping.end(); I != E; ++I) {
86     if (I->first[5] != LastChar) {
87       LastChar = I->first[5];
88       OS << "  case '" << LastChar << "':\n";
89     }
90     
91     OS << "    if (Name == \"" << I->first << "\") return Intrinsic::"
92        << I->second << ";\n";
93   }
94   OS << "  }\n";
95   OS << "  // The 'llvm.' namespace is reserved!\n";
96   OS << "  assert(0 && \"Unknown LLVM intrinsic function!\");\n";
97   OS << "#endif\n\n";
98 }
99
100 void IntrinsicEmitter::
101 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints, 
102                          std::ostream &OS) {
103   std::vector<std::string> Names;
104   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
105     Names.push_back(Ints[i].Name);
106   std::sort(Names.begin(), Names.end());
107   
108   OS << "// Intrinsic ID to name table\n";
109   OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
110   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
111   for (unsigned i = 0, e = Names.size(); i != e; ++i)
112     OS << "  \"" << Names[i] << "\",\n";
113   OS << "#endif\n\n";
114 }
115
116 static void EmitTypeVerify(std::ostream &OS, const std::string &Val,
117                            Record *ArgType) {
118   OS << "    Assert1(" << Val << "->getTypeID() == "
119      << ArgType->getValueAsString("TypeVal") << ",\n"
120      << "            \"Illegal intrinsic type!\", IF);\n";
121
122   // If this is a packed type, check that the subtype and size are correct.
123   if (ArgType->isSubClassOf("LLVMPackedType")) {
124     Record *SubType = ArgType->getValueAsDef("ElTy");
125     OS << "    Assert1(cast<PackedType>(" << Val
126        << ")->getElementType()->getTypeID() == "
127        << SubType->getValueAsString("TypeVal") << ",\n"
128        << "            \"Illegal intrinsic type!\", IF);\n";
129     OS << "    Assert1(cast<PackedType>(" << Val << ")->getNumElements() == "
130        << ArgType->getValueAsInt("NumElts") << ",\n"
131        << "            \"Illegal intrinsic type!\", IF);\n";
132   }
133 }
134
135 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
136                                     std::ostream &OS) {
137   OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
138   OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
139   OS << "  switch (ID) {\n";
140   OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
141   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
142     OS << "  case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
143        << Ints[i].Name << "\n";
144     OS << "    Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
145        << ",\n"
146        << "            \"Illegal # arguments for intrinsic function!\", IF);\n";
147     EmitTypeVerify(OS, "FTy->getReturnType()", Ints[i].ArgTypeDefs[0]);
148     for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
149       EmitTypeVerify(OS, "FTy->getParamType(" + utostr(j-1) + ")",
150                      Ints[i].ArgTypeDefs[j]);
151     OS << "    break;\n";
152   }
153   OS << "  }\n";
154   OS << "#endif\n\n";
155 }
156
157 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
158                                       std::ostream &OS) {
159   OS << "// BasicAliasAnalysis code.\n";
160   OS << "#ifdef GET_MODREF_BEHAVIOR\n";
161   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
162     switch (Ints[i].ModRef) {
163     default: break;
164     case CodeGenIntrinsic::NoMem:
165       OS << "  NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
166       break;
167     case CodeGenIntrinsic::ReadArgMem:
168     case CodeGenIntrinsic::ReadMem:
169       OS << "  OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
170       break;
171     }
172   }
173   OS << "#endif\n\n";
174 }
175
176 void IntrinsicEmitter::
177 EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
178   OS << "// SelectionDAGIsel code.\n";
179   OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
180   OS << "  switch (IntrinsicID) {\n";
181   OS << "  default: break;\n";
182   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
183     switch (Ints[i].ModRef) {
184     default: break;
185     case CodeGenIntrinsic::NoMem:
186       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
187       break;
188     }
189   }
190   OS << "    return true; // These intrinsics have no side effects.\n";
191   OS << "  }\n";
192   OS << "#endif\n\n";
193 }
194
195 void IntrinsicEmitter::
196 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
197   OS << "// isInstructionTriviallyDead code.\n";
198   OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
199   OS << "  switch (F->getIntrinsicID()) {\n";
200   OS << "  default: break;\n";
201   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
202     switch (Ints[i].ModRef) {
203     default: break;
204     case CodeGenIntrinsic::NoMem:
205     case CodeGenIntrinsic::ReadArgMem:
206     case CodeGenIntrinsic::ReadMem:
207       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
208       break;
209     }
210   }
211   OS << "    return true; // These intrinsics have no side effects.\n";
212   OS << "  }\n";
213   OS << "#endif\n\n";
214 }
215
216 void IntrinsicEmitter::
217 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
218   OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
219   OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
220   OS << "  switch (F->getIntrinsicID()) {\n";
221   OS << "  default: BuiltinName = \"\"; break;\n";
222   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
223     if (!Ints[i].GCCBuiltinName.empty()) {
224       OS << "  case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
225          << Ints[i].GCCBuiltinName << "\"; break;\n";
226     }
227   }
228   OS << "  }\n";
229   OS << "#endif\n\n";
230 }
231
232 void IntrinsicEmitter::
233 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, 
234                              std::ostream &OS) {
235   typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
236   BIMTy BuiltinMap;
237   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
238     if (!Ints[i].GCCBuiltinName.empty()) {
239       std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
240                                               Ints[i].TargetPrefix);
241       if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
242         throw "Intrinsic '" + Ints[i].TheDef->getName() +
243               "': duplicate GCC builtin name!";
244     }
245   }
246   
247   OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
248   OS << "// This is used by the C front-end.  The GCC builtin name is passed\n";
249   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
250   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
251   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
252   OS << "  if (0);\n";
253   // Note: this could emit significantly better code if we cared.
254   for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
255     OS << "  else if (";
256     if (!I->first.second.empty()) {
257       // Emit this as a strcmp, so it can be constant folded by the FE.
258       OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
259          << "           ";
260     }
261     OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
262     OS << "    IntrinsicID = Intrinsic::" << I->second << ";\n";
263   }
264   OS << "  else\n";
265   OS << "    IntrinsicID = Intrinsic::not_intrinsic;\n";
266   OS << "#endif\n\n";
267 }