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