emit the __builtin -> intrinsic map with StringMatcher instead of a
[oota-llvm.git] / utils / TableGen / IntrinsicEmitter.cpp
1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
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 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits information about intrinsic functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "CodeGenTarget.h"
15 #include "IntrinsicEmitter.h"
16 #include "Record.h"
17 #include "StringMatcher.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include <algorithm>
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 // IntrinsicEmitter Implementation
24 //===----------------------------------------------------------------------===//
25
26 void IntrinsicEmitter::run(raw_ostream &OS) {
27   EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
28   
29   std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
30   
31   if (TargetOnly && !Ints.empty())
32     TargetPrefix = Ints[0].TargetPrefix;
33
34   EmitPrefix(OS);
35
36   // Emit the enum information.
37   EmitEnumInfo(Ints, OS);
38
39   // Emit the intrinsic ID -> name table.
40   EmitIntrinsicToNameTable(Ints, OS);
41
42   // Emit the intrinsic ID -> overload table.
43   EmitIntrinsicToOverloadTable(Ints, OS);
44
45   // Emit the function name recognizer.
46   EmitFnNameRecognizer(Ints, OS);
47   
48   // Emit the intrinsic verifier.
49   EmitVerifier(Ints, OS);
50   
51   // Emit the intrinsic declaration generator.
52   EmitGenerator(Ints, OS);
53   
54   // Emit the intrinsic parameter attributes.
55   EmitAttributes(Ints, OS);
56
57   // Emit intrinsic alias analysis mod/ref behavior.
58   EmitModRefBehavior(Ints, OS);
59
60   // Emit a list of intrinsics with corresponding GCC builtins.
61   EmitGCCBuiltinList(Ints, OS);
62
63   // Emit code to translate GCC builtins into LLVM intrinsics.
64   EmitIntrinsicToGCCBuiltinMap(Ints, OS);
65
66   EmitSuffix(OS);
67 }
68
69 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
70   OS << "// VisualStudio defines setjmp as _setjmp\n"
71         "#if defined(_MSC_VER) && defined(setjmp)\n"
72         "#define setjmp_undefined_for_visual_studio\n"
73         "#undef setjmp\n"
74         "#endif\n\n";
75 }
76
77 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
78   OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_visual_studio)\n"
79         "// let's return it to _setjmp state\n"
80         "#define setjmp _setjmp\n"
81         "#endif\n\n";
82 }
83
84 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
85                                     raw_ostream &OS) {
86   OS << "// Enum values for Intrinsics.h\n";
87   OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
88   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
89     OS << "    " << Ints[i].EnumName;
90     OS << ((i != e-1) ? ", " : "  ");
91     OS << std::string(40-Ints[i].EnumName.size(), ' ') 
92       << "// " << Ints[i].Name << "\n";
93   }
94   OS << "#endif\n\n";
95 }
96
97 void IntrinsicEmitter::
98 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, 
99                      raw_ostream &OS) {
100   // Build a function name -> intrinsic name mapping.
101   std::map<std::string, unsigned> IntMapping;
102   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
103     IntMapping[Ints[i].Name.substr(5)] = i;
104     
105   OS << "// Function name -> enum value recognizer code.\n";
106   OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
107   OS << "  Name += 5; Len -= 5;  // Skip over 'llvm.'\n";
108   OS << "  switch (*Name) {      // Dispatch on first letter.\n";
109   OS << "  default:\n";
110   // Emit the intrinsics in sorted order.
111   char LastChar = 0;
112   for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
113        E = IntMapping.end(); I != E; ++I) {
114     if (I->first[0] != LastChar) {
115       LastChar = I->first[0];
116       OS << "    break;\n";
117       OS << "  case '" << LastChar << "':\n";
118     }
119     
120     // For overloaded intrinsics, only the prefix needs to match
121     std::string TheStr = I->first;
122     if (Ints[I->second].isOverloaded) {
123       TheStr += '.';  // Require "bswap." instead of bswap.
124       OS << "    if (Len > " << I->first.size();
125     } else {
126       OS << "    if (Len == " << I->first.size();
127     }
128       
129     OS << " && !memcmp(Name, \"" << TheStr << "\", "
130        << TheStr.size() << ")) return " << TargetPrefix << "Intrinsic::"
131        << Ints[I->second].EnumName << ";\n";
132   }
133   OS << "  }\n";
134   OS << "#endif\n\n";
135 }
136
137 void IntrinsicEmitter::
138 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints, 
139                          raw_ostream &OS) {
140   OS << "// Intrinsic ID to name table\n";
141   OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
142   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
143   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
144     OS << "  \"" << Ints[i].Name << "\",\n";
145   OS << "#endif\n\n";
146 }
147
148 void IntrinsicEmitter::
149 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints, 
150                          raw_ostream &OS) {
151   OS << "// Intrinsic ID to overload table\n";
152   OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
153   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
154   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
155     OS << "  ";
156     if (Ints[i].isOverloaded)
157       OS << "true";
158     else
159       OS << "false";
160     OS << ",\n";
161   }
162   OS << "#endif\n\n";
163 }
164
165 static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
166   if (EVT(VT).isInteger()) {
167     unsigned BitWidth = EVT(VT).getSizeInBits();
168     OS << "IntegerType::get(Context, " << BitWidth << ")";
169   } else if (VT == MVT::Other) {
170     // MVT::OtherVT is used to mean the empty struct type here.
171     OS << "StructType::get(Context)";
172   } else if (VT == MVT::f32) {
173     OS << "Type::getFloatTy(Context)";
174   } else if (VT == MVT::f64) {
175     OS << "Type::getDoubleTy(Context)";
176   } else if (VT == MVT::f80) {
177     OS << "Type::getX86_FP80Ty(Context)";
178   } else if (VT == MVT::f128) {
179     OS << "Type::getFP128Ty(Context)";
180   } else if (VT == MVT::ppcf128) {
181     OS << "Type::getPPC_FP128Ty(Context)";
182   } else if (VT == MVT::isVoid) {
183     OS << "Type::getVoidTy(Context)";
184   } else if (VT == MVT::Metadata) {
185     OS << "Type::getMetadataTy(Context)";
186   } else {
187     assert(false && "Unsupported ValueType!");
188   }
189 }
190
191 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
192                              unsigned &ArgNo);
193
194 static void EmitTypeGenerate(raw_ostream &OS,
195                              const std::vector<Record*> &ArgTypes,
196                              unsigned &ArgNo) {
197   if (ArgTypes.empty())
198     return EmitTypeForValueType(OS, MVT::isVoid);
199   
200   if (ArgTypes.size() == 1)
201     return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
202
203   OS << "StructType::get(Context, ";
204
205   for (std::vector<Record*>::const_iterator
206          I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
207     EmitTypeGenerate(OS, *I, ArgNo);
208     OS << ", ";
209   }
210
211   OS << " NULL)";
212 }
213
214 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
215                              unsigned &ArgNo) {
216   MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
217
218   if (ArgType->isSubClassOf("LLVMMatchType")) {
219     unsigned Number = ArgType->getValueAsInt("Number");
220     assert(Number < ArgNo && "Invalid matching number!");
221     if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
222       OS << "VectorType::getExtendedElementVectorType"
223          << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
224     else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
225       OS << "VectorType::getTruncatedElementVectorType"
226          << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
227     else
228       OS << "Tys[" << Number << "]";
229   } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
230     // NOTE: The ArgNo variable here is not the absolute argument number, it is
231     // the index of the "arbitrary" type in the Tys array passed to the
232     // Intrinsic::getDeclaration function. Consequently, we only want to
233     // increment it when we actually hit an overloaded type. Getting this wrong
234     // leads to very subtle bugs!
235     OS << "Tys[" << ArgNo++ << "]";
236   } else if (EVT(VT).isVector()) {
237     EVT VVT = VT;
238     OS << "VectorType::get(";
239     EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
240     OS << ", " << VVT.getVectorNumElements() << ")";
241   } else if (VT == MVT::iPTR) {
242     OS << "PointerType::getUnqual(";
243     EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
244     OS << ")";
245   } else if (VT == MVT::iPTRAny) {
246     // Make sure the user has passed us an argument type to overload. If not,
247     // treat it as an ordinary (not overloaded) intrinsic.
248     OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo 
249     << "] : PointerType::getUnqual(";
250     EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
251     OS << ")";
252     ++ArgNo;
253   } else if (VT == MVT::isVoid) {
254     if (ArgNo == 0)
255       OS << "Type::getVoidTy(Context)";
256     else
257       // MVT::isVoid is used to mean varargs here.
258       OS << "...";
259   } else {
260     EmitTypeForValueType(OS, VT);
261   }
262 }
263
264 /// RecordListComparator - Provide a deterministic comparator for lists of
265 /// records.
266 namespace {
267   typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
268   struct RecordListComparator {
269     bool operator()(const RecPair &LHS,
270                     const RecPair &RHS) const {
271       unsigned i = 0;
272       const std::vector<Record*> *LHSVec = &LHS.first;
273       const std::vector<Record*> *RHSVec = &RHS.first;
274       unsigned RHSSize = RHSVec->size();
275       unsigned LHSSize = LHSVec->size();
276
277       for (; i != LHSSize; ++i) {
278         if (i == RHSSize) return false;  // RHS is shorter than LHS.
279         if ((*LHSVec)[i] != (*RHSVec)[i])
280           return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
281       }
282
283       if (i != RHSSize) return true;
284
285       i = 0;
286       LHSVec = &LHS.second;
287       RHSVec = &RHS.second;
288       RHSSize = RHSVec->size();
289       LHSSize = LHSVec->size();
290
291       for (i = 0; i != LHSSize; ++i) {
292         if (i == RHSSize) return false;  // RHS is shorter than LHS.
293         if ((*LHSVec)[i] != (*RHSVec)[i])
294           return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
295       }
296
297       return i != RHSSize;
298     }
299   };
300 }
301
302 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
303                                     raw_ostream &OS) {
304   OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
305   OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
306   OS << "  switch (ID) {\n";
307   OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
308   
309   // This checking can emit a lot of very common code.  To reduce the amount of
310   // code that we emit, batch up cases that have identical types.  This avoids
311   // problems where GCC can run out of memory compiling Verifier.cpp.
312   typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
313   MapTy UniqueArgInfos;
314   
315   // Compute the unique argument type info.
316   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
317     UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
318                              Ints[i].IS.ParamTypeDefs)].push_back(i);
319
320   // Loop through the array, emitting one comparison for each batch.
321   for (MapTy::iterator I = UniqueArgInfos.begin(),
322        E = UniqueArgInfos.end(); I != E; ++I) {
323     for (unsigned i = 0, e = I->second.size(); i != e; ++i)
324       OS << "  case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
325          << Ints[I->second[i]].Name << "\n";
326     
327     const RecPair &ArgTypes = I->first;
328     const std::vector<Record*> &RetTys = ArgTypes.first;
329     const std::vector<Record*> &ParamTys = ArgTypes.second;
330     std::vector<unsigned> OverloadedTypeIndices;
331
332     OS << "    VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
333        << ParamTys.size();
334
335     // Emit return types.
336     for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
337       Record *ArgType = RetTys[j];
338       OS << ", ";
339
340       if (ArgType->isSubClassOf("LLVMMatchType")) {
341         unsigned Number = ArgType->getValueAsInt("Number");
342         assert(Number < OverloadedTypeIndices.size() &&
343                "Invalid matching number!");
344         Number = OverloadedTypeIndices[Number];
345         if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
346           OS << "~(ExtendedElementVectorType | " << Number << ")";
347         else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
348           OS << "~(TruncatedElementVectorType | " << Number << ")";
349         else
350           OS << "~" << Number;
351       } else {
352         MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
353         OS << getEnumName(VT);
354
355         if (EVT(VT).isOverloaded())
356           OverloadedTypeIndices.push_back(j);
357
358         if (VT == MVT::isVoid && j != 0 && j != je - 1)
359           throw "Var arg type not last argument";
360       }
361     }
362
363     // Emit the parameter types.
364     for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
365       Record *ArgType = ParamTys[j];
366       OS << ", ";
367
368       if (ArgType->isSubClassOf("LLVMMatchType")) {
369         unsigned Number = ArgType->getValueAsInt("Number");
370         assert(Number < OverloadedTypeIndices.size() &&
371                "Invalid matching number!");
372         Number = OverloadedTypeIndices[Number];
373         if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
374           OS << "~(ExtendedElementVectorType | " << Number << ")";
375         else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
376           OS << "~(TruncatedElementVectorType | " << Number << ")";
377         else
378           OS << "~" << Number;
379       } else {
380         MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
381         OS << getEnumName(VT);
382
383         if (EVT(VT).isOverloaded())
384           OverloadedTypeIndices.push_back(j + RetTys.size());
385
386         if (VT == MVT::isVoid && j != 0 && j != je - 1)
387           throw "Var arg type not last argument";
388       }
389     }
390       
391     OS << ");\n";
392     OS << "    break;\n";
393   }
394   OS << "  }\n";
395   OS << "#endif\n\n";
396 }
397
398 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, 
399                                      raw_ostream &OS) {
400   OS << "// Code for generating Intrinsic function declarations.\n";
401   OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
402   OS << "  switch (id) {\n";
403   OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
404   
405   // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
406   // types.
407   typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
408   MapTy UniqueArgInfos;
409   
410   // Compute the unique argument type info.
411   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
412     UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
413                              Ints[i].IS.ParamTypeDefs)].push_back(i);
414
415   // Loop through the array, emitting one generator for each batch.
416   std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
417   
418   for (MapTy::iterator I = UniqueArgInfos.begin(),
419        E = UniqueArgInfos.end(); I != E; ++I) {
420     for (unsigned i = 0, e = I->second.size(); i != e; ++i)
421       OS << "  case " << IntrinsicStr << Ints[I->second[i]].EnumName 
422          << ":\t\t// " << Ints[I->second[i]].Name << "\n";
423     
424     const RecPair &ArgTypes = I->first;
425     const std::vector<Record*> &RetTys = ArgTypes.first;
426     const std::vector<Record*> &ParamTys = ArgTypes.second;
427
428     unsigned N = ParamTys.size();
429
430     if (N > 1 &&
431         getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
432       OS << "    IsVarArg = true;\n";
433       --N;
434     }
435
436     unsigned ArgNo = 0;
437     OS << "    ResultTy = ";
438     EmitTypeGenerate(OS, RetTys, ArgNo);
439     OS << ";\n";
440     
441     for (unsigned j = 0; j != N; ++j) {
442       OS << "    ArgTys.push_back(";
443       EmitTypeGenerate(OS, ParamTys[j], ArgNo);
444       OS << ");\n";
445     }
446
447     OS << "    break;\n";
448   }
449
450   OS << "  }\n";
451   OS << "#endif\n\n";
452 }
453
454 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
455 void IntrinsicEmitter::
456 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
457   OS << "// Add parameter attributes that are not common to all intrinsics.\n";
458   OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
459   if (TargetOnly)
460     OS << "static AttrListPtr getAttributes(" << TargetPrefix 
461        << "Intrinsic::ID id) {";
462   else
463     OS << "AttrListPtr Intrinsic::getAttributes(ID id) {";
464   OS << "  // No intrinsic can throw exceptions.\n";
465   OS << "  Attributes Attr = Attribute::NoUnwind;\n";
466   OS << "  switch (id) {\n";
467   OS << "  default: break;\n";
468   unsigned MaxArgAttrs = 0;
469   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
470     MaxArgAttrs =
471       std::max(MaxArgAttrs, unsigned(Ints[i].ArgumentAttributes.size()));
472     switch (Ints[i].ModRef) {
473     default: break;
474     case CodeGenIntrinsic::NoMem:
475       OS << "  case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName 
476          << ":\n";
477       break;
478     }
479   }
480   OS << "    Attr |= Attribute::ReadNone; // These do not access memory.\n";
481   OS << "    break;\n";
482   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
483     switch (Ints[i].ModRef) {
484     default: break;
485     case CodeGenIntrinsic::ReadArgMem:
486     case CodeGenIntrinsic::ReadMem:
487       OS << "  case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName 
488          << ":\n";
489       break;
490     }
491   }
492   OS << "    Attr |= Attribute::ReadOnly; // These do not write memory.\n";
493   OS << "    break;\n";
494   OS << "  }\n";
495   OS << "  AttributeWithIndex AWI[" << MaxArgAttrs+1 << "];\n";
496   OS << "  unsigned NumAttrs = 0;\n";
497   OS << "  switch (id) {\n";
498   OS << "  default: break;\n";
499   
500   // Add argument attributes for any intrinsics that have them.
501   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
502     if (Ints[i].ArgumentAttributes.empty()) continue;
503     
504     OS << "  case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName 
505        << ":\n";
506
507     std::vector<std::pair<unsigned, CodeGenIntrinsic::ArgAttribute> > ArgAttrs =
508       Ints[i].ArgumentAttributes;
509     // Sort by argument index.
510     std::sort(ArgAttrs.begin(), ArgAttrs.end());
511
512     unsigned NumArgsWithAttrs = 0;
513
514     while (!ArgAttrs.empty()) {
515       unsigned ArgNo = ArgAttrs[0].first;
516       
517       OS << "    AWI[" << NumArgsWithAttrs++ << "] = AttributeWithIndex::get("
518          << ArgNo+1 << ", 0";
519
520       while (!ArgAttrs.empty() && ArgAttrs[0].first == ArgNo) {
521         switch (ArgAttrs[0].second) {
522         default: assert(0 && "Unknown arg attribute");
523         case CodeGenIntrinsic::NoCapture:
524           OS << "|Attribute::NoCapture";
525           break;
526         }
527         ArgAttrs.erase(ArgAttrs.begin());
528       }
529       OS << ");\n";
530     }
531     
532     OS << "    NumAttrs = " << NumArgsWithAttrs << ";\n";
533     OS << "    break;\n";
534   }
535   
536   OS << "  }\n";
537   OS << "  AWI[NumAttrs] = AttributeWithIndex::get(~0, Attr);\n";
538   OS << "  return AttrListPtr::get(AWI, NumAttrs+1);\n";
539   OS << "}\n";
540   OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
541 }
542
543 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
544 void IntrinsicEmitter::
545 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
546   OS << "// Determine intrinsic alias analysis mod/ref behavior.\n";
547   OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n";
548   OS << "switch (iid) {\n";
549   OS << "default:\n    return UnknownModRefBehavior;\n";
550   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
551     if (Ints[i].ModRef == CodeGenIntrinsic::ReadWriteMem)
552       continue;
553     OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
554       << ":\n";
555     switch (Ints[i].ModRef) {
556     default:
557       assert(false && "Unknown Mod/Ref type!");
558     case CodeGenIntrinsic::NoMem:
559       OS << "  return DoesNotAccessMemory;\n";
560       break;
561     case CodeGenIntrinsic::ReadArgMem:
562     case CodeGenIntrinsic::ReadMem:
563       OS << "  return OnlyReadsMemory;\n";
564       break;
565     case CodeGenIntrinsic::ReadWriteArgMem:
566       OS << "  return AccessesArguments;\n";
567       break;
568     }
569   }
570   OS << "}\n";
571   OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
572 }
573
574 void IntrinsicEmitter::
575 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
576   OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
577   OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
578   OS << "  switch (F->getIntrinsicID()) {\n";
579   OS << "  default: BuiltinName = \"\"; break;\n";
580   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
581     if (!Ints[i].GCCBuiltinName.empty()) {
582       OS << "  case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
583          << Ints[i].GCCBuiltinName << "\"; break;\n";
584     }
585   }
586   OS << "  }\n";
587   OS << "#endif\n\n";
588 }
589
590 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
591 /// same target, and we already checked it.
592 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
593                                const std::string &TargetPrefix,
594                                raw_ostream &OS) {
595   
596   std::vector<StringMatcher::StringPair> Results;
597   
598   for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
599        E = BIM.end(); I != E; ++I) {
600     std::string ResultCode =
601     "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
602     Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
603   }
604
605   StringMatcher("BuiltinName", Results, OS).Emit();
606 }
607
608         
609 void IntrinsicEmitter::
610 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, 
611                              raw_ostream &OS) {
612   typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
613   BIMTy BuiltinMap;
614   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
615     if (!Ints[i].GCCBuiltinName.empty()) {
616       // Get the map for this target prefix.
617       std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
618       
619       if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
620                                      Ints[i].EnumName)).second)
621         throw "Intrinsic '" + Ints[i].TheDef->getName() +
622               "': duplicate GCC builtin name!";
623     }
624   }
625   
626   OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
627   OS << "// This is used by the C front-end.  The GCC builtin name is passed\n";
628   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
629   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
630   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
631   
632   if (TargetOnly) {
633     OS << "static " << TargetPrefix << "Intrinsic::ID "
634        << "getIntrinsicForGCCBuiltin(const char "
635        << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
636   } else {
637     OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
638        << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
639   }
640   
641   OS << "  StringRef BuiltinName(BuiltinNameStr);\n";
642   OS << "  StringRef TargetPrefix(TargetPrefixStr);\n\n";
643   
644   // Note: this could emit significantly better code if we cared.
645   for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
646     OS << "  ";
647     if (!I->first.empty())
648       OS << "if (TargetPrefix == \"" << I->first << "\") ";
649     else
650       OS << "/* Target Independent Builtins */ ";
651     OS << "{\n";
652
653     // Emit the comparisons for this target prefix.
654     EmitTargetBuiltins(I->second, TargetPrefix, OS);
655     OS << "  }\n";
656   }
657   OS << "  return ";
658   if (!TargetPrefix.empty())
659     OS << "(" << TargetPrefix << "Intrinsic::ID)";
660   OS << "Intrinsic::not_intrinsic;\n";
661   OS << "}\n";
662   OS << "#endif\n\n";
663 }