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