3aa887693a2aceac38104b7a9a6490db0476c291
[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   OS << "// Intrinsic ID to name table\n";
104   OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
105   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
106   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
107     OS << "  \"" << Ints[i].Name << "\",\n";
108   OS << "#endif\n\n";
109 }
110
111 static void EmitTypeVerify(std::ostream &OS, const std::string &Val,
112                            Record *ArgType) {
113   OS << "    Assert1(" << Val << "->getTypeID() == "
114      << ArgType->getValueAsString("TypeVal") << ",\n"
115      << "            \"Illegal intrinsic type!\", IF);\n";
116
117   // If this is a packed type, check that the subtype and size are correct.
118   if (ArgType->isSubClassOf("LLVMPackedType")) {
119     Record *SubType = ArgType->getValueAsDef("ElTy");
120     OS << "    Assert1(cast<PackedType>(" << Val
121        << ")->getElementType()->getTypeID() == "
122        << SubType->getValueAsString("TypeVal") << ",\n"
123        << "            \"Illegal intrinsic type!\", IF);\n";
124     OS << "    Assert1(cast<PackedType>(" << Val << ")->getNumElements() == "
125        << ArgType->getValueAsInt("NumElts") << ",\n"
126        << "            \"Illegal intrinsic type!\", IF);\n";
127   }
128 }
129
130 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
131                                     std::ostream &OS) {
132   OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
133   OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
134   OS << "  switch (ID) {\n";
135   OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
136   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
137     OS << "  case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
138        << Ints[i].Name << "\n";
139     OS << "    Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
140        << ",\n"
141        << "            \"Illegal # arguments for intrinsic function!\", IF);\n";
142     EmitTypeVerify(OS, "FTy->getReturnType()", Ints[i].ArgTypeDefs[0]);
143     for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
144       EmitTypeVerify(OS, "FTy->getParamType(" + utostr(j-1) + ")",
145                      Ints[i].ArgTypeDefs[j]);
146     OS << "    break;\n";
147   }
148   OS << "  }\n";
149   OS << "#endif\n\n";
150 }
151
152 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
153                                       std::ostream &OS) {
154   OS << "// BasicAliasAnalysis code.\n";
155   OS << "#ifdef GET_MODREF_BEHAVIOR\n";
156   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
157     switch (Ints[i].ModRef) {
158     default: break;
159     case CodeGenIntrinsic::NoMem:
160       OS << "  NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
161       break;
162     case CodeGenIntrinsic::ReadArgMem:
163     case CodeGenIntrinsic::ReadMem:
164       OS << "  OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
165       break;
166     }
167   }
168   OS << "#endif\n\n";
169 }
170
171 void IntrinsicEmitter::
172 EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
173   OS << "// SelectionDAGIsel code.\n";
174   OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
175   OS << "  switch (IntrinsicID) {\n";
176   OS << "  default: break;\n";
177   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
178     switch (Ints[i].ModRef) {
179     default: break;
180     case CodeGenIntrinsic::NoMem:
181       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
182       break;
183     }
184   }
185   OS << "    return true; // These intrinsics have no side effects.\n";
186   OS << "  }\n";
187   OS << "#endif\n\n";
188 }
189
190 void IntrinsicEmitter::
191 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
192   OS << "// isInstructionTriviallyDead code.\n";
193   OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
194   OS << "  switch (F->getIntrinsicID()) {\n";
195   OS << "  default: break;\n";
196   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
197     switch (Ints[i].ModRef) {
198     default: break;
199     case CodeGenIntrinsic::NoMem:
200     case CodeGenIntrinsic::ReadArgMem:
201     case CodeGenIntrinsic::ReadMem:
202       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
203       break;
204     }
205   }
206   OS << "    return true; // These intrinsics have no side effects.\n";
207   OS << "  }\n";
208   OS << "#endif\n\n";
209 }
210
211 void IntrinsicEmitter::
212 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
213   OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
214   OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
215   OS << "  switch (F->getIntrinsicID()) {\n";
216   OS << "  default: BuiltinName = \"\"; break;\n";
217   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
218     if (!Ints[i].GCCBuiltinName.empty()) {
219       OS << "  case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
220          << Ints[i].GCCBuiltinName << "\"; break;\n";
221     }
222   }
223   OS << "  }\n";
224   OS << "#endif\n\n";
225 }
226
227 void IntrinsicEmitter::
228 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, 
229                              std::ostream &OS) {
230   typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
231   BIMTy BuiltinMap;
232   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
233     if (!Ints[i].GCCBuiltinName.empty()) {
234       std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
235                                               Ints[i].TargetPrefix);
236       if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
237         throw "Intrinsic '" + Ints[i].TheDef->getName() +
238               "': duplicate GCC builtin name!";
239     }
240   }
241   
242   OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
243   OS << "// This is used by the C front-end.  The GCC builtin name is passed\n";
244   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
245   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
246   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
247   OS << "  if (0);\n";
248   // Note: this could emit significantly better code if we cared.
249   for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
250     OS << "  else if (";
251     if (!I->first.second.empty()) {
252       // Emit this as a strcmp, so it can be constant folded by the FE.
253       OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
254          << "           ";
255     }
256     OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
257     OS << "    IntrinsicID = Intrinsic::" << I->second << ";\n";
258   }
259   OS << "  else\n";
260   OS << "    IntrinsicID = Intrinsic::not_intrinsic;\n";
261   OS << "#endif\n\n";
262 }