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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a useful analysis step to figure out what numbered
// slots values in a program will land in (keeping track of per plane
// information as required.
//
// This is used primarily for when writing a file to disk, either in bytecode
// or source format.
//
//===----------------------------------------------------------------------===//
#include "llvm/SlotCalculator.h"
#include "llvm/Analysis/ConstantsScanner.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/iOther.h"
#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include "Support/PostOrderIterator.h"
#include "Support/STLExtras.h"
#include <algorithm>
using namespace llvm;
#if 0
#define SC_DEBUG(X) std::cerr << X
#else
#define SC_DEBUG(X)
#endif
SlotCalculator::SlotCalculator(const Module *M, bool buildBytecodeInfo) {
BuildBytecodeInfo = buildBytecodeInfo;
TheModule = M;
// Preload table... Make sure that all of the primitive types are in the table
// and that their Primitive ID is equal to their slot #
//
SC_DEBUG("Inserting primitive types:\n");
for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
assert(Type::getPrimitiveType((Type::PrimitiveID)i));
insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true);
}
if (M == 0) return; // Empty table...
processModule();
}
SlotCalculator::SlotCalculator(const Function *M, bool buildBytecodeInfo) {
BuildBytecodeInfo = buildBytecodeInfo;
TheModule = M ? M->getParent() : 0;
// Preload table... Make sure that all of the primitive types are in the table
// and that their Primitive ID is equal to their slot #
//
SC_DEBUG("Inserting primitive types:\n");
for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
assert(Type::getPrimitiveType((Type::PrimitiveID)i));
insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true);
}
if (TheModule == 0) return; // Empty table...
processModule(); // Process module level stuff
incorporateFunction(M); // Start out in incorporated state
}
// processModule - Process all of the module level function declarations and
// types that are available.
//
void SlotCalculator::processModule() {
SC_DEBUG("begin processModule!\n");
// Add all of the global variables to the value table...
//
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I)
getOrCreateSlot(I);
// Scavenge the types out of the functions, then add the functions themselves
// to the value table...
//
for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
I != E; ++I)
getOrCreateSlot(I);
// Add all of the module level constants used as initializers
//
for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
I != E; ++I)
if (I->hasInitializer())
getOrCreateSlot(I->getInitializer());
// Now that all global constants have been added, rearrange constant planes
// that contain constant strings so that the strings occur at the start of the
// plane, not somewhere in the middle.
//
if (BuildBytecodeInfo) {
TypePlane &Types = Table[Type::TypeTyID];
for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
if (AT->getElementType() == Type::SByteTy ||
AT->getElementType() == Type::UByteTy) {
TypePlane &Plane = Table[plane];
unsigned FirstNonStringID = 0;
for (unsigned i = 0, e = Plane.size(); i != e; ++i)
if (cast<ConstantArray>(Plane[i])->isString()) {
// Check to see if we have to shuffle this string around. If not,
// don't do anything.
if (i != FirstNonStringID) {
// Swap the plane entries....
std::swap(Plane[i], Plane[FirstNonStringID]);
// Keep the NodeMap up to date.
NodeMap[Plane[i]] = i;
NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
}
++FirstNonStringID;
}
}
}
}
#if 0
// FIXME: Empirically, this causes the bytecode files to get BIGGER, because
// it explodes the operand size numbers to be bigger than can be handled
// compactly, which offsets the ~40% savings in constant sizes. Whoops.
// If we are emitting a bytecode file, scan all of the functions for their
// constants, which allows us to emit more compact modules. This is optional,
// and is just used to compactify the constants used by different functions
// together.
if (BuildBytecodeInfo) {
SC_DEBUG("Inserting function constants:\n");
for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
F != E; ++F)
for_each(constant_begin(F), constant_end(F),
bind_obj(this, &SlotCalculator::getOrCreateSlot));
}
#endif
// Insert constants that are named at module level into the slot pool so that
// the module symbol table can refer to them...
//
if (BuildBytecodeInfo) {
SC_DEBUG("Inserting SymbolTable values:\n");
processSymbolTable(&TheModule->getSymbolTable());
}
// Now that we have collected together all of the information relevant to the
// module, compactify the type table if it is particularly big and outputting
// a bytecode file. The basic problem we run into is that some programs have
// a large number of types, which causes the type field to overflow its size,
// which causes instructions to explode in size (particularly call
// instructions). To avoid this behavior, we "sort" the type table so that
// all non-value types are pushed to the end of the type table, giving nice
// low numbers to the types that can be used by instructions, thus reducing
// the amount of explodage we suffer.
if (BuildBytecodeInfo && Table[Type::TypeTyID].size() >= 64) {
// Scan through the type table moving value types to the start of the table.
TypePlane *Types = &Table[Type::TypeTyID];
unsigned FirstNonValueTypeID = 0;
for (unsigned i = 0, e = Types->size(); i != e; ++i)
if (cast<Type>((*Types)[i])->isFirstClassType() ||
cast<Type>((*Types)[i])->isPrimitiveType()) {
// Check to see if we have to shuffle this type around. If not, don't
// do anything.
if (i != FirstNonValueTypeID) {
assert(i != Type::TypeTyID && FirstNonValueTypeID != Type::TypeTyID &&
"Cannot move around the type plane!");
// Swap the type ID's.
std::swap((*Types)[i], (*Types)[FirstNonValueTypeID]);
// Keep the NodeMap up to date.
NodeMap[(*Types)[i]] = i;
NodeMap[(*Types)[FirstNonValueTypeID]] = FirstNonValueTypeID;
// When we move a type, make sure to move its value plane as needed.
if (Table.size() > FirstNonValueTypeID) {
if (Table.size() <= i) Table.resize(i+1);
std::swap(Table[i], Table[FirstNonValueTypeID]);
Types = &Table[Type::TypeTyID];
}
}
++FirstNonValueTypeID;
}
}
SC_DEBUG("end processModule!\n");
}
// processSymbolTable - Insert all of the values in the specified symbol table
// into the values table...
//
void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
for (SymbolTable::type_const_iterator TI = I->second.begin(),
TE = I->second.end(); TI != TE; ++TI)
getOrCreateSlot(TI->second);
}
void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
for (SymbolTable::type_const_iterator TI = I->second.begin(),
TE = I->second.end(); TI != TE; ++TI)
if (isa<Constant>(TI->second) || isa<Type>(TI->second))
getOrCreateSlot(TI->second);
}
void SlotCalculator::incorporateFunction(const Function *F)
|