1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
//===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This tablegen backend is responsible for emitting descriptions of the calling
// conventions supported by this target.
//
//===----------------------------------------------------------------------===//
#include "Common/CodeGenRegisters.h"
#include "Common/CodeGenTarget.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/InterleavedRange.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TGTimer.h"
#include "llvm/TableGen/TableGenBackend.h"
#include <deque>
#include <set>
using namespace llvm;
namespace {
class CallingConvEmitter {
const RecordKeeper &Records;
const CodeGenTarget Target;
unsigned Counter = 0u;
std::string CurrentAction;
bool SwiftAction = false;
std::map<std::string, std::set<std::string>> AssignedRegsMap;
std::map<std::string, std::set<std::string>> AssignedSwiftRegsMap;
std::map<std::string, std::set<std::string>> DelegateToMap;
public:
explicit CallingConvEmitter(const RecordKeeper &R) : Records(R), Target(R) {
for (const CodeGenRegister &Reg : Target.getRegBank().getRegisters())
RegistersByDefName.try_emplace(Reg.getName(), &Reg);
}
void run(raw_ostream &O);
private:
void emitCallingConv(const Record *CC, raw_ostream &O);
void emitAction(const Record *Action, indent Indent, raw_ostream &O);
void emitArgRegisterLists(raw_ostream &O);
StringMap<const CodeGenRegister *> RegistersByDefName;
std::string getQualifiedRegisterName(const Init *I);
};
} // End anonymous namespace
void CallingConvEmitter::run(raw_ostream &O) {
emitSourceFileHeader("Calling Convention Implementation Fragment", O);
ArrayRef<const Record *> CCs =
Records.getAllDerivedDefinitions("CallingConv");
// Emit prototypes for all of the non-custom CC's so that they can forward ref
// each other.
Records.getTimer().startTimer("Emit prototypes");
O << "#ifndef GET_CC_REGISTER_LISTS\n\n";
for (const Record *CC : CCs) {
if (!CC->getValueAsBit("Custom")) {
unsigned Pad = CC->getName().size();
if (CC->getValueAsBit("Entry")) {
O << "bool llvm::";
Pad += 12;
} else {
O << "static bool ";
Pad += 13;
}
O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
<< std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
<< std::string(Pad, ' ')
<< "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
}
}
// Emit each non-custom calling convention description in full.
Records.getTimer().startTimer("Emit full descriptions");
for (const Record *CC : CCs) {
if (!CC->getValueAsBit("Custom")) {
emitCallingConv(CC, O);
}
}
emitArgRegisterLists(O);
O << "\n#endif // CC_REGISTER_LIST\n";
}
void CallingConvEmitter::emitCallingConv(const Record *CC, raw_ostream &O) {
const ListInit *CCActions = CC->getValueAsListInit("Actions");
Counter = 0;
CurrentAction = CC->getName().str();
// Call upon the creation of a map entry from the void!
// We want an entry in AssignedRegsMap for every action, even if that
// entry is empty.
AssignedRegsMap[CurrentAction] = {};
O << "\n\n";
unsigned Pad = CurrentAction.size();
if (CC->getValueAsBit("Entry")) {
O << "bool llvm::";
Pad += 12;
} else {
O << "static bool ";
Pad += 13;
}
O << CurrentAction << "(unsigned ValNo, MVT ValVT,\n"
<< std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
<< std::string(Pad, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
// Emit all of the actions, in order.
for (unsigned I = 0, E = CCActions->size(); I != E; ++I) {
const Record *Action = CCActions->getElementAsRecord(I);
SwiftAction =
llvm::any_of(Action->getSuperClasses(), [](const Record *Class) {
std::string Name = Class->getNameInitAsString();
return StringRef(Name).starts_with("CCIfSwift");
});
O << "\n";
emitAction(Action, indent(2), O);
}
O << "\n return true; // CC didn't match.\n";
O << "}\n";
}
// Return the name of the specified Init (DefInit or StringInit), with a
// namespace qualifier if the corresponding record contains one.
std::string CallingConvEmitter::getQualifiedRegisterName(const Init *I) {
if (const auto *DI = dyn_cast<DefInit>(I))
return getQualifiedName(DI->getDef());
const auto *SI = cast<StringInit>(I);
if (const CodeGenRegister *CGR = RegistersByDefName.lookup(SI->getValue()))
return getQualifiedName(CGR->TheDef);
PrintFatalError("register not defined: " + SI->getAsString());
return "";
}
void CallingConvEmitter::emitAction(const Record *Action, indent Indent,
raw_ostream &O) {
auto EmitRegList = [&](const ListInit *RL, const StringRef RLName) {
O << Indent << "static const MCPhysReg " << RLName << "[] = {\n";
O << Indent << " ";
ListSeparator LS;
for (const Init *V : RL->getElements())
O << LS << getQualifiedRegisterName(V);
O << "\n" << Indent << "};\n";
};
auto EmitAllocateReg = [&](ArrayRef<const ListInit *> RegLists,
ArrayRef<std::string> RLNames) {
SmallVector<std::string> Parms;
if (RegLists[0]->size() == 1) {
for (const ListInit *LI : RegLists)
Parms.push_back(getQualifiedRegisterName(LI->getElement(0)));
} else {
for (const std::string &S : RLNames)
Parms.push_back(S + utostr(++Counter));
for (const auto [Idx, LI] : enumerate(RegLists))
EmitRegList(LI, Parms[Idx]);
}
O << formatv("{0}if (MCRegister Reg = State.AllocateReg({1})) {{\n", Indent,
make_range(Parms.begin(), Parms.end()));
O << Indent << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
<< "Reg, LocVT, LocInfo));\n";
};
auto EmitAllocateStack = [&](bool EmitOffset = false) {
int Size = Action->getValueAsInt("Size");
int Align = Action->getValueAsInt("Align");
if (EmitOffset)
O << Indent << "int64_t Offset" << ++Counter << " = ";
else
O << Indent << " (void)";
O << "State.AllocateStack(";
const char *Fmt = " State.getMachineFunction().getDataLayout()."
"{0}(EVT(LocVT).getTypeForEVT(State.getContext()))";
if (Size)
O << Size << ", ";
else
O << "\n" << Indent << formatv(Fmt, "getTypeAllocSize") << ", ";
if (Align)
O << "Align(" << Align << ")";
else
O << "\n" << Indent << formatv(Fmt, "getABITypeAlign");
O << ");\n";
};
if (Action->isSubClassOf("CCPredicateAction")) {
O << Indent << "if (";
if (Action->isSubClassOf("CCIfType")) {
const ListInit *VTs = Action->getValueAsListInit("VTs");
for (unsigned I = 0, E = VTs->size(); I != E; ++I) {
const Record *VT = VTs->getElementAsRecord(I);
if (I != 0)
O << " ||\n " << Indent;
O << "LocVT == " << getEnumName(getValueType(VT));
}
} else if (Action->isSubClassOf("CCIf")) {
O << Action->getValueAsString("Predicate");
} else {
errs() << *Action;
PrintFatalError(Action->getLoc(), "Unknown CCPredicateAction!");
}
O << ") {\n";
emitAction(Action->getValueAsDef("SubAction"), Indent + 2, O);
O << Indent << "}\n";
return;
}
if (Action->isSubClassOf("CCDelegateTo")) {
const Record *CC = Action->getValueAsDef("CC");
O << Indent << "if (!" << CC->getName()
<< "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
<< Indent + 2 << "return false;\n";
DelegateToMap[CurrentAction].insert(CC->getName().str());
} else if (Action->isSubClassOf("CCAssignToReg") ||
Action->isSubClassOf("CCAssignToRegTuple") ||
Action->isSubClassOf("CCAssignToRegAndStack")) {
const ListInit *RegList = Action->getValueAsListInit("RegList");
for (unsigned I = 0, E = RegList->size(); I != E; ++I) {
std::string Name = getQualifiedRegisterName(RegList->getElement(I));
if (SwiftAction)
AssignedSwiftRegsMap[CurrentAction].insert(std::move(Name));
else
AssignedRegsMap[CurrentAction].insert(std::move(Name));
}
EmitAllocateReg({RegList}, {"RegList"});
if (Action->isSubClassOf("CCAssignToRegAndStack"))
EmitAllocateStack();
O << Indent << " return false;\n";
O << Indent << "}\n";
} else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
const ListInit *RegList = Action->getValueAsListInit("RegList");
const ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
PrintFatalError(Action->getLoc(),
"Invalid length of list of shadowed registers");
EmitAllocateReg({RegList, ShadowRegList}, {"RegList", "RegList"});
O << Indent << " return false;\n";
O << Indent << "}\n";
} else if (Action->isSubClassOf("CCAssignToStack")) {
EmitAllocateStack(/*EmitOffset=*/true);
O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
<< Counter << ", LocVT, LocInfo));\n";
O << Indent << "return false;\n";
} else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
int Size = Action->getValueAsInt("Size");
int Align = Action->getValueAsInt("Align");
const ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
unsigned ShadowRegListNumber = ++Counter;
EmitRegList(ShadowRegList, "ShadowRegList" + utostr(ShadowRegListNumber));
O << Indent << "int64_t Offset" << ++Counter << " = State.AllocateStack("
<< Size << ", Align(" << Align << "), "
<< "ShadowRegList" << ShadowRegListNumber << ");\n";
O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
<< Counter << ", LocVT, LocInfo));\n";
O << Indent << "return false;\n";
} else if (Action->isSubClassOf("CCPromoteToType")) {
const Record *DestTy = Action->getValueAsDef("DestTy");
MVT::SimpleValueType DestVT = getValueType(DestTy);
O << Indent << "LocVT = " << getEnumName(DestVT) << ";\n";
if (MVT(DestVT).isFloatingPoint()) {
O << Indent << "LocInfo = CCValAssign::FPExt;\n";
} else {
O << Indent << "if (ArgFlags.isSExt())\n"
<< Indent << " LocInfo = CCValAssign::SExt;\n"
<< Indent << "else if (ArgFlags.isZExt())\n"
<< Indent << " LocInfo = CCValAssign::ZExt;\n"
<< Indent << "else\n"
<< Indent << " LocInfo = CCValAssign::AExt;\n";
}
} else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
const Record *DestTy = Action->getValueAsDef("DestTy");
MVT::SimpleValueType DestVT = getValueType(DestTy);
O << Indent << "LocVT = " << getEnumName(DestVT) << ";\n";
if (MVT(DestVT).isFloatingPoint()) {
PrintFatalError(Action->getLoc(),
"CCPromoteToUpperBitsInType does not handle floating "
"point");
} else {
O << Indent << "if (ArgFlags.isSExt())\n"
<< Indent << " LocInfo = CCValAssign::SExtUpper;\n"
<< Indent << "else if (ArgFlags.isZExt())\n"
<< Indent << " LocInfo = CCValAssign::ZExtUpper;\n"
<< Indent << "else\n"
<< Indent << " LocInfo = CCValAssign::AExtUpper;\n";
}
} else if (Action->isSubClassOf("CCBitConvertToType")) {
const Record *DestTy = Action->getValueAsDef("DestTy");
O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
O << Indent << "LocInfo = CCValAssign::BCvt;\n";
} else if (Action->isSubClassOf("CCTruncToType")) {
const Record *DestTy = Action->getValueAsDef("DestTy");
O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
O << Indent << "LocInfo = CCValAssign::Trunc;\n";
} else if (Action->isSubClassOf("CCPassIndirect")) {
const Record *DestTy = Action->getValueAsDef("DestTy");
O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
O << Indent << "LocInfo = CCValAssign::Indirect;\n";
} else if (Action->isSubClassOf("CCPassByVal")) {
int Size = Action->getValueAsInt("Size");
int Align = Action->getValueAsInt("Align");
O << Indent << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " << Size
<< ", Align(" << Align << "), ArgFlags);\n";
O << Indent << "return false;\n";
} else if (Action->isSubClassOf("CCCustom")) {
O << Indent << "if (" << Action->getValueAsString("FuncName")
<< "(ValNo, ValVT, "
<< "LocVT, LocInfo, ArgFlags, State))\n";
O << Indent << " return false;\n";
} else {
errs() << *Action;
PrintFatalError(Action->getLoc(), "Unknown CCAction!");
}
}
void CallingConvEmitter::emitArgRegisterLists(raw_ostream &O) {
// Transitively merge all delegated CCs into AssignedRegsMap.
using EntryTy = std::pair<std::string, std::set<std::string>>;
bool Redo;
do {
Redo = false;
std::deque<EntryTy> Worklist(DelegateToMap.begin(), DelegateToMap.end());
while (!Worklist.empty()) {
EntryTy Entry = Worklist.front();
Worklist.pop_front();
const std::string &CCName = Entry.first;
std::set<std::string> &Registers = Entry.second;
if (!Registers.empty())
continue;
for (auto &InnerEntry : Worklist) {
const std::string &InnerCCName = InnerEntry.first;
std::set<std::string> &InnerRegisters = InnerEntry.second;
auto It = InnerRegisters.find(CCName);
if (It != InnerRegisters.end()) {
const auto &Src = AssignedRegsMap[CCName];
AssignedRegsMap[InnerCCName].insert(Src.begin(), Src.end());
InnerRegisters.erase(It);
}
}
DelegateToMap.erase(CCName);
Redo = true;
}
} while (Redo);
if (AssignedRegsMap.empty())
return;
O << "\n#else\n\n";
for (const auto &[RegName, Registers] : AssignedRegsMap) {
if (RegName.empty())
continue;
O << "const MCRegister " << RegName << "_ArgRegs[] = { ";
if (Registers.empty())
O << "0";
else
O << llvm::interleaved(Registers);
O << " };\n";
}
if (AssignedSwiftRegsMap.empty())
return;
O << "\n// Registers used by Swift.\n";
for (const auto &[RegName, Registers] : AssignedSwiftRegsMap)
O << "const MCRegister " << RegName << "_Swift_ArgRegs[] = { "
<< llvm::interleaved(Registers) << " };\n";
}
static TableGen::Emitter::OptClass<CallingConvEmitter>
X("gen-callingconv", "Generate calling convention descriptions");
|