blob: c77b9ba096c2ff85016cf89549999955529d6c46 [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"
16using namespace llvm;
17
18//===----------------------------------------------------------------------===//
19// CodeGenIntrinsic Implementation
20//===----------------------------------------------------------------------===//
21
22std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
23 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
24 return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
25}
26
27CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
28 std::string DefName = R->getName();
Chris Lattner6448ee42006-03-09 22:30:49 +000029 ModRef = WriteMem;
Chris Lattner9e493cf2006-03-03 02:32:46 +000030
31 if (DefName.size() <= 4 ||
32 std::string(DefName.begin(), DefName.begin()+4) != "int_")
33 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
34 EnumName = std::string(DefName.begin()+4, DefName.end());
35
36 Name = R->getValueAsString("LLVMName");
37 if (Name == "") {
38 // If an explicit name isn't specified, derive one from the DefName.
39 Name = "llvm.";
40 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
41 if (EnumName[i] == '_')
42 Name += '.';
43 else
44 Name += EnumName[i];
45 }
Chris Lattnerf97a00e2006-03-09 22:05:04 +000046
47 // Parse the list of argument types.
48 ListInit *TypeList = R->getValueAsListInit("Types");
49 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
50 DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
51 assert(DI && "Invalid list type!");
52 Record *TyEl = DI->getDef();
53 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
54 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
55 }
56 if (ArgTypes.size() == 0)
57 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Chris Lattner6448ee42006-03-09 22:30:49 +000058
59 // Parse the intrinsic properties.
60 ListInit *PropList = R->getValueAsListInit("Properties");
61 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
62 DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
63 assert(DI && "Invalid list type!");
64 Record *Property = DI->getDef();
65 assert(Property->isSubClassOf("IntrinsicProperty") &&
66 "Expected a property!");
67
68 if (Property->getName() == "InstrNoMem")
69 ModRef = NoMem;
70 else if (Property->getName() == "InstrReadArgMem")
71 ModRef = ReadArgMem;
72 else if (Property->getName() == "IntrReadMem")
73 ModRef = ReadMem;
74 else if (Property->getName() == "InstrWriteArgMem")
75 ModRef = WriteArgMem;
76 else if (Property->getName() == "IntrWriteMem")
77 ModRef = WriteMem;
78 else
79 assert(0 && "Unknown property!");
80 }
Chris Lattner9e493cf2006-03-03 02:32:46 +000081}
82
83//===----------------------------------------------------------------------===//
84// IntrinsicEmitter Implementation
85//===----------------------------------------------------------------------===//
86
87void IntrinsicEmitter::run(std::ostream &OS) {
88 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
89
90 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
91
92 // Emit the enum information.
93 EmitEnumInfo(Ints, OS);
Chris Lattner9b843b22006-03-09 20:34:19 +000094
95 // Emit the function name recognizer.
96 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerf97a00e2006-03-09 22:05:04 +000097
98 // Emit the intrinsic verifier.
99 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +0000100
101 // Emit mod/ref info for each function.
102 EmitModRefInfo(Ints, OS);
Chris Lattner4e5f3592006-03-09 22:37:52 +0000103
104 // Emit side effect info for each function.
105 EmitSideEffectInfo(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +0000106}
107
108void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
109 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +0000110 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000111 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
112 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
113 OS << " " << Ints[i].EnumName;
114 OS << ((i != e-1) ? ", " : " ");
115 OS << std::string(40-Ints[i].EnumName.size(), ' ')
116 << "// " << Ints[i].Name << "\n";
117 }
118 OS << "#endif\n\n";
119}
Chris Lattner9b843b22006-03-09 20:34:19 +0000120
121void IntrinsicEmitter::
122EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
123 std::ostream &OS) {
124 // Build a function name -> intrinsic name mapping.
125 std::map<std::string, std::string> IntMapping;
126 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
127 IntMapping[Ints[i].Name] = Ints[i].EnumName;
128
129 OS << "// Function name -> enum value recognizer code.\n";
130 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
131 OS << " switch (Name[5]) {\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000132 OS << " default: break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000133 // Emit the intrinsics in sorted order.
134 char LastChar = 0;
135 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
136 E = IntMapping.end(); I != E; ++I) {
137 assert(I->first.size() > 5 && std::string(I->first.begin(),
138 I->first.begin()+5) == "llvm." &&
139 "Invalid intrinsic name!");
140 if (I->first[5] != LastChar) {
141 LastChar = I->first[5];
142 OS << " case '" << LastChar << "':\n";
143 }
144
145 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
146 << I->second << ";\n";
147 }
148 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000149 OS << " // The 'llvm.' namespace is reserved!\n";
150 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
151 OS << "#endif\n\n";
152}
153
154void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
155 std::ostream &OS) {
156 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
157 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
158 OS << " switch (ID) {\n";
159 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
160 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
161 OS << " case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
162 << Ints[i].Name << "\n";
163 OS << " Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
164 << ",\n"
165 << " \"Illegal # arguments for intrinsic function!\", IF);\n";
166 OS << " Assert1(FTy->getReturnType()->getTypeID() == "
167 << Ints[i].ArgTypes[0] << ",\n"
168 << " \"Illegal result type!\", IF);\n";
169 for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
170 OS << " Assert1(FTy->getParamType(" << j-1 << ")->getTypeID() == "
171 << Ints[i].ArgTypes[j] << ",\n"
172 << " \"Illegal result type!\", IF);\n";
173 OS << " break;\n";
174 }
175 OS << " }\n";
176 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000177}
178
Chris Lattner6448ee42006-03-09 22:30:49 +0000179void 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}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000197
198void IntrinsicEmitter::
199EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
200 OS << "// isInstructionTriviallyDead code.\n";
201 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
202 OS << " switch (F->getIntrinsicID()) {\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 case CodeGenIntrinsic::ReadArgMem:
209 case CodeGenIntrinsic::ReadMem:
210 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
211 break;
212 }
213 }
214 OS << " return true; // These intrinsics have no side effects.\n";
215 OS << " }\n";
216 OS << "#endif\n\n";
217
218}