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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
//===-- AllocaManager.cpp -------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the AllocaManager class.
//
// The AllocaManager computes a frame layout, assigning every static alloca an
// offset. It does alloca liveness analysis in order to reuse stack memory,
// using lifetime intrinsics.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "allocamanager"
#include "AllocaManager.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Timer.h"
#include "llvm/ADT/Statistic.h"
using namespace llvm;
STATISTIC(NumAllocas, "Number of allocas eliminated");
// Return the size of the given alloca.
uint64_t AllocaManager::getSize(const AllocaInst *AI) {
assert(AI->isStaticAlloca());
return DL->getTypeAllocSize(AI->getAllocatedType()) *
cast<ConstantInt>(AI->getArraySize())->getValue().getZExtValue();
}
// Return the alignment of the given alloca.
unsigned AllocaManager::getAlignment(const AllocaInst *AI) {
assert(AI->isStaticAlloca());
return std::max(AI->getAlignment(),
DL->getABITypeAlignment(AI->getAllocatedType()));
}
AllocaManager::AllocaInfo AllocaManager::getInfo(const AllocaInst *AI) {
assert(AI->isStaticAlloca());
return AllocaInfo(AI, getSize(AI), getAlignment(AI));
}
// Given a lifetime_start or lifetime_end intrinsic, determine if it's
// describing a static alloc memory region suitable for our analysis. If so,
// return the alloca, otherwise return NULL.
const AllocaInst *
AllocaManager::getAllocaFromIntrinsic(const CallInst *CI) {
const IntrinsicInst *II = cast<IntrinsicInst>(CI);
assert(II->getIntrinsicID() == Intrinsic::lifetime_start ||
II->getIntrinsicID() == Intrinsic::lifetime_end);
// Lifetime intrinsics have a size as their first argument and a pointer as
// their second argument.
const Value *Size = II->getArgOperand(0);
const Value *Ptr = II->getArgOperand(1);
// Check to see if we can convert the size to a host integer. If we can't,
// it's probably not worth worrying about.
const ConstantInt *SizeCon = dyn_cast<ConstantInt>(Size);
if (!SizeCon) return NULL;
const APInt &SizeAP = SizeCon->getValue();
if (SizeAP.getActiveBits() > 64) return NULL;
uint64_t MarkedSize = SizeAP.getZExtValue();
// We're only interested if the pointer is a static alloca.
const AllocaInst *AI = dyn_cast<AllocaInst>(Ptr->stripPointerCasts());
if (!AI || !AI->isStaticAlloca()) return NULL;
// Make sure the size covers the alloca.
if (MarkedSize < getSize(AI)) return NULL;
return AI;
}
int AllocaManager::AllocaSort(const void *l, const void *r) {
const AllocaInfo *li = static_cast<const AllocaInfo *>(l);
const AllocaInfo *ri = static_cast<const AllocaInfo *>(r);
// Sort by alignment to minimize padding.
if (li->getAlignment() > ri->getAlignment()) return -1;
if (li->getAlignment() < ri->getAlignment()) return 1;
// Ensure a stable sort. We can do this because the pointers are
// pointing into the same array.
if (li > ri) return -1;
if (li < ri) return 1;
return 0;
}
// Collect allocas
void AllocaManager::collectMarkedAllocas() {
NamedRegionTimer Timer("Collect Marked Allocas", "AllocaManager",
TimePassesIsEnabled);
// Weird semantics: If an alloca *ever* appears in a lifetime start or end
// within the same function, its lifetime begins only at the explicit lifetime
// starts and ends only at the explicit lifetime ends and function exit
// points. Otherwise, its lifetime begins in the entry block and it is live
// everywhere.
//
// And so, instead of just walking the entry block to find all the static
// allocas, we walk the whole body to find the intrinsics so we can find the
// set of static allocas referenced in the intrinsics.
for (Function::const_iterator FI = F->begin(), FE = F->end();
FI != FE; ++FI) {
for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
BI != BE; ++BI) {
const CallInst *CI = dyn_cast<CallInst>(BI);
if (!CI) continue;
const Value *Callee = CI->getCalledValue();
if (Callee == LifetimeStart || Callee == LifetimeEnd) {
if (const AllocaInst *AI = getAllocaFromIntrinsic(CI)) {
Allocas.insert(std::make_pair(AI, 0));
}
}
}
}
// All that said, we still want the intrinsics in the order they appear in the
// block, so that we can represent later ones with earlier ones and skip
// worrying about dominance, so run through the entry block and index those
// allocas which we identified above.
AllocasByIndex.reserve(Allocas.size());
const BasicBlock *EntryBB = &F->getEntryBlock();
for (BasicBlock::const_iterator BI = EntryBB->begin(), BE = EntryBB->end();
BI != BE; ++BI) {
const AllocaInst *AI = dyn_cast<AllocaInst>(BI);
if (!AI || !AI->isStaticAlloca()) continue;
AllocaMap::iterator I = Allocas.find(AI);
if (I != Allocas.end()) {
I->second = AllocasByIndex.size();
AllocasByIndex.push_back(getInfo(AI));
}
}
assert(AllocasByIndex.size() == Allocas.size());
}
// Calculate the starting point from which inter-block liveness will be
// computed.
void AllocaManager::collectBlocks() {
NamedRegionTimer Timer("Collect Blocks", "AllocaManager",
TimePassesIsEnabled);
size_t AllocaCount = AllocasByIndex.size();
BitVector Seen(AllocaCount);
for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) {
const BasicBlock *BB = I;
BlockLifetimeInfo &BLI = BlockLiveness[BB];
BLI.Start.resize(AllocaCount);
BLI.End.resize(AllocaCount);
// Track which allocas we've seen. This is used because if a lifetime start
// is the first lifetime marker for an alloca in a block, the alloca is
// live-in.
Seen.reset();
// Walk the instructions and compute the Start and End sets.
for (BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
BI != BE; ++BI) {
const CallInst *CI = dyn_cast<CallInst>(BI);
if (!CI) continue;
const Value *Callee = CI->getCalledValue();
if (Callee == LifetimeStart) {
if (const AllocaInst *AI = getAllocaFromIntrinsic(CI)) {
AllocaMap::const_iterator MI = Allocas.find(AI);
if (MI != Allocas.end()) {
size_t AllocaIndex = MI->second;
if (!Seen.test(AllocaIndex)) {
BLI.Start.set(AllocaIndex);
}
BLI.End.reset(AllocaIndex);
Seen.set(AllocaIndex);
}
}
} else if (Callee == LifetimeEnd) {
if (const AllocaInst *AI = getAllocaFromIntrinsic(CI)) {
AllocaMap::const_iterator MI = Allocas.find(AI);
if (MI != Allocas.end()) {
size_t AllocaIndex = MI->second;
BLI.End.set(AllocaIndex);
Seen.set(AllocaIndex);
}
}
}
}
// Lifetimes that start in this block and do not end here are live-out.
BLI.LiveOut = BLI.Start;
BLI.LiveOut.reset(BLI.End);
if (BLI.LiveOut.any()) {
for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
SI != SE; ++SI) {
InterBlockWorklist.insert(*SI);
}
}
// Lifetimes that end in this block and do not start here are live-in.
// TODO: Is this actually true? What are the semantics of a standalone
// lifetime end? See also the code in computeInterBlockLiveness.
BLI.LiveIn = BLI.End;
BLI.LiveIn.reset(BLI.Start);
if (BLI.LiveIn.any()) {
for (const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
PI != PE; ++PI) {
InterBlockWorklist.insert(*PI);
}
}
}
}
// Compute the LiveIn and LiveOut sets for each block in F.
void AllocaManager::computeInterBlockLiveness() {
NamedRegionTimer Timer("Compute inter-block liveness", "AllocaManager",
TimePassesIsEnabled);
size_t AllocaCount = AllocasByIndex.size();
BitVector Temp(AllocaCount);
// This is currently using a very simple-minded bi-directional liveness
// propagation algorithm. Numerous opportunities for compile time
// speedups here.
while (!InterBlockWorklist.empty()) {
const BasicBlock *BB = InterBlockWorklist.pop_back_val();
BlockLifetimeInfo &BLI = BlockLiveness[BB];
// Compute the new live-in set.
for (const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
PI != PE; ++PI) {
Temp |= BlockLiveness[*PI].LiveOut;
}
// If it contains new live blocks, prepare to propagate them.
if (Temp.test(BLI.LiveIn)) {
BLI.LiveIn |= Temp;
Temp.reset(BLI.End);
BLI.LiveOut |= Temp;
for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
SI != SE; ++SI) {
InterBlockWorklist.insert(*SI);
}
}
Temp.reset();
// Compute the new live-out set.
for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
SI != SE; ++SI) {
Temp |= BlockLiveness[*SI].LiveIn;
}
// If it contains new live blocks, prepare to propagate them.
if (Temp.test(BLI.LiveOut)) {
// TODO: As above, what are the semantics of a standalone lifetime end?
BLI.LiveOut |= Temp;
Temp.reset(BLI.Start);
BLI.LiveIn |= Temp;
for (const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
PI != PE; ++PI) {
InterBlockWorklist.insert(*PI);
}
}
Temp.reset();
}
}
// Determine overlapping liveranges within blocks.
void AllocaManager::computeIntraBlockLiveness() {
NamedRegionTimer Timer("Compute intra-block liveness", "AllocaManager",
TimePassesIsEnabled);
size_t AllocaCount = AllocasByIndex.size();
BitVector Current(AllocaCount);
AllocaCompatibility.resize(AllocaCount, BitVector(AllocaCount, true));
for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) {
const BasicBlock *BB = I;
const BlockLifetimeInfo &BLI = BlockLiveness[BB];
Current = BLI.LiveIn;
for (int i = Current.find_first(); i >= 0; i = Current.find_next(i)) {
AllocaCompatibility[i].reset(Current);
}
for (BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
BI != BE; ++BI) {
const CallInst *CI = dyn_cast<CallInst>(BI);
if (!CI) continue;
const Value *Callee = CI->getCalledValue();
if (Callee == LifetimeStart) {
if (const AllocaInst *AI = getAllocaFromIntrinsic(CI)) {
size_t AIndex = Allocas[AI];
// We conflict with everything else that's currently live.
AllocaCompatibility[AIndex].reset(Current);
// Everything else that's currently live conflicts with us.
for (int i = Current.find_first(); i >= 0; i = Current.find_next(i)) {
AllocaCompatibility[i].reset(AIndex);
}
// We're now live.
Current.set(AIndex);
}
} else if (Callee == LifetimeEnd) {
if (const AllocaInst *AI = getAllocaFromIntrinsic(CI)) {
size_t AIndex = Allocas[AI];
// We're no longer live.
Current.reset(AIndex);
}
}
}
}
}
// Decide which allocas will represent which other allocas, and if so what their
// size and alignment will need to be.
void AllocaManager::computeRepresentatives() {
NamedRegionTimer Timer("Compute Representatives", "AllocaManager",
TimePassesIsEnabled);
for (size_t i = 0, e = AllocasByIndex.size(); i != e; ++i) {
// If we've already represented this alloca with another, don't visit it.
if (AllocasByIndex[i].isForwarded()) continue;
if (i > size_t(INT_MAX)) continue;
// Find compatible allocas. This is a simple greedy algorithm.
for (int j = int(i); ; ) {
assert(j >= int(i));
j = AllocaCompatibility[i].find_next(j);
assert(j != int(i));
if (j < 0) break;
if (!AllocaCompatibility[j][i]) continue;
DEBUG(dbgs() << "Allocas: "
"Representing "
<< AllocasByIndex[j].getInst()->getName() << " "
"with "
<< AllocasByIndex[i].getInst()->getName() << "\n");
++NumAllocas;
assert(!AllocasByIndex[j].isForwarded());
AllocasByIndex[i].mergeSize(AllocasByIndex[j].getSize());
AllocasByIndex[i].mergeAlignment(AllocasByIndex[j].getAlignment());
AllocasByIndex[j].forward(i);
AllocaCompatibility[i] &= AllocaCompatibility[j];
AllocaCompatibility[j].reset();
}
}
}
void AllocaManager::computeFrameOffsets() {
NamedRegionTimer Timer("Compute Frame Offsets", "AllocaManager",
TimePassesIsEnabled);
// Walk through the entry block and collect all the allocas, including the
// ones with no lifetime markers that we haven't looked at yet. We walk in
// reverse order so that we can set the representative allocas as those that
// dominate the others as we go.
const BasicBlock *EntryBB = &F->getEntryBlock();
for (BasicBlock::const_iterator BI = EntryBB->begin(), BE = EntryBB->end();
BI != BE; ++BI) {
const AllocaInst *AI = dyn_cast<AllocaInst>(BI);
if (!AI || !AI->isStaticAlloca()) continue;
AllocaMap::const_iterator I = Allocas.find(AI);
if (I != Allocas.end()) {
// An alloca with lifetime markers. Emit the record we've crafted for it,
// if we've chosen to keep it as a representative.
const AllocaInfo &Info = AllocasByIndex[I->second];
if (!Info.isForwarded()) {
SortedAllocas.push_back(Info);
}
} else {
// An alloca with no lifetime markers.
SortedAllocas.push_back(getInfo(AI));
}
}
// Sort the allocas to hopefully reduce padding.
array_pod_sort(SortedAllocas.begin(), SortedAllocas.end(), AllocaSort);
// Assign stack offsets.
uint64_t CurrentOffset = 0;
for (SmallVectorImpl<AllocaInfo>::const_iterator I = SortedAllocas.begin(),
E = SortedAllocas.end(); I != E; ++I) {
const AllocaInfo &Info = *I;
uint64_t NewOffset = RoundUpToAlignment(CurrentOffset, Info.getAlignment());
// For backwards compatibility, align every power-of-two multiple alloca to
// its greatest power-of-two factor, up to 8 bytes. In particular, cube2hash
// is known to depend on this.
// TODO: Consider disabling this and making people fix their code.
if (uint64_t Size = Info.getSize()) {
uint64_t P2 = uint64_t(1) << CountTrailingZeros_64(Size);
unsigned CompatAlign = unsigned(std::min(P2, uint64_t(8)));
NewOffset = RoundUpToAlignment(NewOffset, CompatAlign);
}
const AllocaInst *AI = Info.getInst();
StaticAllocas[AI] = StaticAllocation(AI, NewOffset);
CurrentOffset = NewOffset + Info.getSize();
}
// Add allocas that were represented by other allocas to the StaticAllocas map
// so that our clients can look them up.
for (unsigned i = 0, e = AllocasByIndex.size(); i != e; ++i) {
const AllocaInfo &Info = AllocasByIndex[i];
if (!Info.isForwarded()) continue;
size_t j = Info.getForwardedID();
assert(!AllocasByIndex[j].isForwarded());
StaticAllocaMap::const_iterator I =
StaticAllocas.find(AllocasByIndex[j].getInst());
assert(I != StaticAllocas.end());
std::pair<StaticAllocaMap::const_iterator, bool> Pair =
StaticAllocas.insert(std::make_pair(AllocasByIndex[i].getInst(),
I->second));
assert(Pair.second); (void)Pair;
}
// Record the final frame size. Keep the stack pointer 16-byte aligned.
FrameSize = CurrentOffset;
FrameSize = RoundUpToAlignment(FrameSize, 16);
DEBUG(dbgs() << "Allocas: "
"Statically allocated frame size is " << FrameSize << "\n");
}
AllocaManager::AllocaManager() {
}
void AllocaManager::analyze(const Function &Func, const DataLayout &Layout,
bool PerformColoring) {
NamedRegionTimer Timer("AllocaManager", TimePassesIsEnabled);
assert(Allocas.empty());
assert(AllocasByIndex.empty());
assert(AllocaCompatibility.empty());
assert(BlockLiveness.empty());
assert(StaticAllocas.empty());
assert(SortedAllocas.empty());
DL = &Layout;
F = &Func;
// Get the declarations for the lifetime intrinsics so we can quickly test to
// see if they are used at all, and for use later if they are.
const Module *M = F->getParent();
LifetimeStart = M->getFunction(Intrinsic::getName(Intrinsic::lifetime_start));
LifetimeEnd = M->getFunction(Intrinsic::getName(Intrinsic::lifetime_end));
// If we are optimizing and the module contains any lifetime intrinsics, run
// the alloca coloring algorithm.
if (PerformColoring &&
((LifetimeStart && !LifetimeStart->use_empty()) ||
(LifetimeEnd && !LifetimeEnd->use_empty()))) {
collectMarkedAllocas();
if (!AllocasByIndex.empty()) {
DEBUG(dbgs() << "Allocas: "
<< AllocasByIndex.size() << " marked allocas found\n");
collectBlocks();
computeInterBlockLiveness();
computeIntraBlockLiveness();
BlockLiveness.clear();
computeRepresentatives();
AllocaCompatibility.clear();
}
}
computeFrameOffsets();
SortedAllocas.clear();
Allocas.clear();
AllocasByIndex.clear();
}
void AllocaManager::clear() {
StaticAllocas.clear();
}
bool
AllocaManager::getFrameOffset(const AllocaInst *AI, uint64_t *Offset) const {
assert(AI->isStaticAlloca());
StaticAllocaMap::const_iterator I = StaticAllocas.find(AI);
assert(I != StaticAllocas.end());
*Offset = I->second.Offset;
return AI == I->second.Representative;
}
const AllocaInst *
AllocaManager::getRepresentative(const AllocaInst *AI) const {
assert(AI->isStaticAlloca());
StaticAllocaMap::const_iterator I = StaticAllocas.find(AI);
assert(I != StaticAllocas.end());
return I->second.Representative;
}
|