For PR1064:
[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, Record *ArgType) {
112   OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
113   // If this is an integer type, check the width is correct.
114   if (ArgType->isSubClassOf("LLVMIntegerType"))
115     OS << ArgType->getValueAsInt("Width") << ", ";
116
117   // If this is a packed type, check that the subtype and size are correct.
118   else if (ArgType->isSubClassOf("LLVMPackedType")) {
119     EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
120     OS << ArgType->getValueAsInt("NumElts") << ", ";
121   }
122 }
123
124 /// RecordListComparator - Provide a determinstic comparator for lists of
125 /// records.
126 namespace {
127   struct RecordListComparator {
128     bool operator()(const std::vector<Record*> &LHS,
129                     const std::vector<Record*> &RHS) const {
130       unsigned i = 0;
131       do {
132         if (i == RHS.size()) return false;  // RHS is shorter than LHS.
133         if (LHS[i] != RHS[i])
134           return LHS[i]->getName() < RHS[i]->getName();
135       } while (++i != LHS.size());
136       
137       return i != RHS.size();
138     }
139   };
140 }
141
142 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
143                                     std::ostream &OS) {
144   OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
145   OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
146   OS << "  switch (ID) {\n";
147   OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
148   
149   // This checking can emit a lot of very common code.  To reduce the amount of
150   // code that we emit, batch up cases that have identical types.  This avoids
151   // problems where GCC can run out of memory compiling Verifier.cpp.
152   typedef std::map<std::vector<Record*>, std::vector<unsigned>, 
153     RecordListComparator> MapTy;
154   MapTy UniqueArgInfos;
155   
156   // Compute the unique argument type info.
157   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
158     UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
159
160   // Loop through the array, emitting one comparison for each batch.
161   for (MapTy::iterator I = UniqueArgInfos.begin(),
162        E = UniqueArgInfos.end(); I != E; ++I) {
163     for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
164       OS << "  case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
165          << Ints[I->second[i]].Name << "\n";
166     }
167     
168     const std::vector<Record*> &ArgTypes = I->first;
169     OS << "    VerifyIntrinsicPrototype(IF, ";
170     for (unsigned j = 0; j != ArgTypes.size(); ++j)
171       EmitTypeVerify(OS, ArgTypes[j]);
172     OS << "-1);\n";
173     OS << "    break;\n";
174   }
175   OS << "  }\n";
176   OS << "#endif\n\n";
177 }
178
179 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
180                                       std::ostream &OS) {
181   OS << "// BasicAliasAnalysis code.\n";
182   OS << "#ifdef GET_MODREF_BEHAVIOR\n";
183   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
184     switch (Ints[i].ModRef) {
185     default: break;
186     case CodeGenIntrinsic::NoMem:
187       OS << "  NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
188       break;
189     case CodeGenIntrinsic::ReadArgMem:
190     case CodeGenIntrinsic::ReadMem:
191       OS << "  OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
192       break;
193     }
194   }
195   OS << "#endif\n\n";
196 }
197
198 void IntrinsicEmitter::
199 EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
200   OS << "// SelectionDAGIsel code.\n";
201   OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
202   OS << "  switch (IntrinsicID) {\n";
203   OS << "  default: break;\n";
204   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
205     switch (Ints[i].ModRef) {
206     default: break;
207     case CodeGenIntrinsic::NoMem:
208       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
209       break;
210     }
211   }
212   OS << "    return true; // These intrinsics have no side effects.\n";
213   OS << "  }\n";
214   OS << "#endif\n\n";
215 }
216
217 void IntrinsicEmitter::
218 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
219   OS << "// Return true if doesn't access or only reads memory.\n";
220   OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
221   OS << "  switch (IntrinsicID) {\n";
222   OS << "  default: break;\n";
223   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
224     switch (Ints[i].ModRef) {
225     default: break;
226     case CodeGenIntrinsic::NoMem:
227     case CodeGenIntrinsic::ReadArgMem:
228     case CodeGenIntrinsic::ReadMem:
229       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
230       break;
231     }
232   }
233   OS << "    return true; // These intrinsics have no side effects.\n";
234   OS << "  }\n";
235   OS << "#endif\n\n";
236 }
237
238 void IntrinsicEmitter::
239 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
240   OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
241   OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
242   OS << "  switch (F->getIntrinsicID()) {\n";
243   OS << "  default: BuiltinName = \"\"; break;\n";
244   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
245     if (!Ints[i].GCCBuiltinName.empty()) {
246       OS << "  case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
247          << Ints[i].GCCBuiltinName << "\"; break;\n";
248     }
249   }
250   OS << "  }\n";
251   OS << "#endif\n\n";
252 }
253
254 void IntrinsicEmitter::
255 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, 
256                              std::ostream &OS) {
257   typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
258   BIMTy BuiltinMap;
259   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
260     if (!Ints[i].GCCBuiltinName.empty()) {
261       std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
262                                               Ints[i].TargetPrefix);
263       if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
264         throw "Intrinsic '" + Ints[i].TheDef->getName() +
265               "': duplicate GCC builtin name!";
266     }
267   }
268   
269   OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
270   OS << "// This is used by the C front-end.  The GCC builtin name is passed\n";
271   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
272   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
273   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
274   OS << "  if (0);\n";
275   // Note: this could emit significantly better code if we cared.
276   for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
277     OS << "  else if (";
278     if (!I->first.second.empty()) {
279       // Emit this as a strcmp, so it can be constant folded by the FE.
280       OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
281          << "           ";
282     }
283     OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
284     OS << "    IntrinsicID = Intrinsic::" << I->second << ";\n";
285   }
286   OS << "  else\n";
287   OS << "    IntrinsicID = Intrinsic::not_intrinsic;\n";
288   OS << "#endif\n\n";
289 }