blob: 9396bdfe6e5697c96ecc5b887d299b674f9bfd2c [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- 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"
Chris Lattner18faf5d2006-03-13 22:38:57 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000017using namespace llvm;
18
19//===----------------------------------------------------------------------===//
20// CodeGenIntrinsic Implementation
21//===----------------------------------------------------------------------===//
22
23std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
24 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
25 return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
26}
27
28CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
29 std::string DefName = R->getName();
Chris Lattner6448ee42006-03-09 22:30:49 +000030 ModRef = WriteMem;
Chris Lattner9e493cf2006-03-03 02:32:46 +000031
32 if (DefName.size() <= 4 ||
33 std::string(DefName.begin(), DefName.begin()+4) != "int_")
34 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
35 EnumName = std::string(DefName.begin()+4, DefName.end());
36
37 Name = R->getValueAsString("LLVMName");
38 if (Name == "") {
39 // If an explicit name isn't specified, derive one from the DefName.
40 Name = "llvm.";
41 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
42 if (EnumName[i] == '_')
43 Name += '.';
44 else
45 Name += EnumName[i];
46 }
Chris Lattnerf97a00e2006-03-09 22:05:04 +000047
48 // Parse the list of argument types.
49 ListInit *TypeList = R->getValueAsListInit("Types");
50 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
51 DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
52 assert(DI && "Invalid list type!");
53 Record *TyEl = DI->getDef();
54 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
55 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
Chris Lattner18faf5d2006-03-13 22:38:57 +000056 ArgTypeDefs.push_back(TyEl);
Chris Lattnerf97a00e2006-03-09 22:05:04 +000057 }
58 if (ArgTypes.size() == 0)
59 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Chris Lattner6448ee42006-03-09 22:30:49 +000060
61 // Parse the intrinsic properties.
62 ListInit *PropList = R->getValueAsListInit("Properties");
63 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
64 DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
65 assert(DI && "Invalid list type!");
66 Record *Property = DI->getDef();
67 assert(Property->isSubClassOf("IntrinsicProperty") &&
68 "Expected a property!");
69
70 if (Property->getName() == "InstrNoMem")
71 ModRef = NoMem;
72 else if (Property->getName() == "InstrReadArgMem")
73 ModRef = ReadArgMem;
74 else if (Property->getName() == "IntrReadMem")
75 ModRef = ReadMem;
76 else if (Property->getName() == "InstrWriteArgMem")
77 ModRef = WriteArgMem;
78 else if (Property->getName() == "IntrWriteMem")
79 ModRef = WriteMem;
80 else
81 assert(0 && "Unknown property!");
82 }
Chris Lattner9e493cf2006-03-03 02:32:46 +000083}
84
85//===----------------------------------------------------------------------===//
86// IntrinsicEmitter Implementation
87//===----------------------------------------------------------------------===//
88
89void IntrinsicEmitter::run(std::ostream &OS) {
90 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
91
92 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
93
94 // Emit the enum information.
95 EmitEnumInfo(Ints, OS);
Chris Lattner9b843b22006-03-09 20:34:19 +000096
97 // Emit the function name recognizer.
98 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerf97a00e2006-03-09 22:05:04 +000099
100 // Emit the intrinsic verifier.
101 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +0000102
103 // Emit mod/ref info for each function.
104 EmitModRefInfo(Ints, OS);
Chris Lattner4e5f3592006-03-09 22:37:52 +0000105
106 // Emit side effect info for each function.
107 EmitSideEffectInfo(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +0000108}
109
110void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
111 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +0000112 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000113 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
114 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
115 OS << " " << Ints[i].EnumName;
116 OS << ((i != e-1) ? ", " : " ");
117 OS << std::string(40-Ints[i].EnumName.size(), ' ')
118 << "// " << Ints[i].Name << "\n";
119 }
120 OS << "#endif\n\n";
121}
Chris Lattner9b843b22006-03-09 20:34:19 +0000122
123void IntrinsicEmitter::
124EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
125 std::ostream &OS) {
126 // Build a function name -> intrinsic name mapping.
127 std::map<std::string, std::string> IntMapping;
128 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
129 IntMapping[Ints[i].Name] = Ints[i].EnumName;
130
131 OS << "// Function name -> enum value recognizer code.\n";
132 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
133 OS << " switch (Name[5]) {\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000134 OS << " default: break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000135 // Emit the intrinsics in sorted order.
136 char LastChar = 0;
137 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
138 E = IntMapping.end(); I != E; ++I) {
139 assert(I->first.size() > 5 && std::string(I->first.begin(),
140 I->first.begin()+5) == "llvm." &&
141 "Invalid intrinsic name!");
142 if (I->first[5] != LastChar) {
143 LastChar = I->first[5];
144 OS << " case '" << LastChar << "':\n";
145 }
146
147 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
148 << I->second << ";\n";
149 }
150 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000151 OS << " // The 'llvm.' namespace is reserved!\n";
152 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
153 OS << "#endif\n\n";
154}
155
Chris Lattner18faf5d2006-03-13 22:38:57 +0000156static void EmitTypeVerify(std::ostream &OS, const std::string &Val,
157 Record *ArgType) {
158 OS << " Assert1(" << Val << "->getTypeID() == "
159 << ArgType->getValueAsString("TypeVal") << ",\n"
160 << " \"Illegal intrinsic type!\", IF);\n";
161
162 // If this is a packed type, check that the subtype and size are correct.
163 if (ArgType->isSubClassOf("LLVMPackedType")) {
164 Record *SubType = ArgType->getValueAsDef("ElTy");
165 OS << " Assert1(cast<PackedType>(" << Val
166 << ")->getElementType()->getTypeID() == "
167 << SubType->getValueAsString("TypeVal") << ",\n"
168 << " \"Illegal intrinsic type!\", IF);\n";
169 OS << " Assert1(cast<PackedType>(" << Val << ")->getNumElements() == "
170 << ArgType->getValueAsInt("NumElts") << ",\n"
171 << " \"Illegal intrinsic type!\", IF);\n";
172 }
173}
174
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000175void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
176 std::ostream &OS) {
177 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
178 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
179 OS << " switch (ID) {\n";
180 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
181 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
182 OS << " case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
183 << Ints[i].Name << "\n";
184 OS << " Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
185 << ",\n"
186 << " \"Illegal # arguments for intrinsic function!\", IF);\n";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000187 EmitTypeVerify(OS, "FTy->getReturnType()", Ints[i].ArgTypeDefs[0]);
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000188 for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
Chris Lattner18faf5d2006-03-13 22:38:57 +0000189 EmitTypeVerify(OS, "FTy->getParamType(" + utostr(j-1) + ")",
190 Ints[i].ArgTypeDefs[j]);
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000191 OS << " break;\n";
192 }
193 OS << " }\n";
194 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000195}
196
Chris Lattner6448ee42006-03-09 22:30:49 +0000197void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
198 std::ostream &OS) {
199 OS << "// BasicAliasAnalysis code.\n";
200 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
201 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
202 switch (Ints[i].ModRef) {
203 default: break;
204 case CodeGenIntrinsic::NoMem:
205 OS << " NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
206 break;
207 case CodeGenIntrinsic::ReadArgMem:
208 case CodeGenIntrinsic::ReadMem:
209 OS << " OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
210 break;
211 }
212 }
213 OS << "#endif\n\n";
214}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000215
216void IntrinsicEmitter::
217EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
218 OS << "// isInstructionTriviallyDead code.\n";
219 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
220 OS << " switch (F->getIntrinsicID()) {\n";
221 OS << " default: break;\n";
222 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
223 switch (Ints[i].ModRef) {
224 default: break;
225 case CodeGenIntrinsic::NoMem:
226 case CodeGenIntrinsic::ReadArgMem:
227 case CodeGenIntrinsic::ReadMem:
228 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
229 break;
230 }
231 }
232 OS << " return true; // These intrinsics have no side effects.\n";
233 OS << " }\n";
234 OS << "#endif\n\n";
235
236}