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
|
//===-- SSAUpdaterImpl.h - SSA Updater Implementation -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file provides a template that implements the core algorithm for the
// SSAUpdater and MachineSSAUpdater.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
#define LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ValueHandle.h"
namespace llvm {
class CastInst;
class PHINode;
template<typename T> class SSAUpdaterTraits;
template<typename UpdaterT>
class SSAUpdaterImpl {
private:
UpdaterT *Updater;
typedef SSAUpdaterTraits<UpdaterT> Traits;
typedef typename Traits::BlkT BlkT;
typedef typename Traits::ValT ValT;
typedef typename Traits::PhiT PhiT;
/// BBInfo - Per-basic block information used internally by SSAUpdaterImpl.
/// The predecessors of each block are cached here since pred_iterator is
/// slow and we need to iterate over the blocks at least a few times.
class BBInfo {
public:
BlkT *BB; // Back-pointer to the corresponding block.
ValT AvailableVal; // Value to use in this block.
BBInfo *DefBB; // Block that defines the available value.
int BlkNum; // Postorder number.
BBInfo *IDom; // Immediate dominator.
unsigned NumPreds; // Number of predecessor blocks.
BBInfo **Preds; // Array[NumPreds] of predecessor blocks.
PhiT *PHITag; // Marker for existing PHIs that match.
BBInfo(BlkT *ThisBB, ValT V)
: BB(ThisBB), AvailableVal(V), DefBB(V ? this : 0), BlkNum(0), IDom(0),
NumPreds(0), Preds(0), PHITag(0) { }
};
typedef DenseMap<BlkT*, ValT> AvailableValsTy;
AvailableValsTy *AvailableVals;
SmallVectorImpl<PhiT*> *InsertedPHIs;
typedef SmallVectorImpl<BBInfo*> BlockListTy;
typedef DenseMap<BlkT*, BBInfo*> BBMapTy;
BBMapTy BBMap;
BumpPtrAllocator Allocator;
public:
explicit SSAUpdaterImpl(UpdaterT *U, AvailableValsTy *A,
SmallVectorImpl<PhiT*> *Ins) :
Updater(U), AvailableVals(A), InsertedPHIs(Ins) { }
/// GetValue - Check to see if AvailableVals has an entry for the specified
/// BB and if so, return it. If not, construct SSA form by first
/// calculating the required placement of PHIs and then inserting new PHIs
/// where needed.
ValT GetValue(BlkT *BB) {
SmallVector<BBInfo*, 100> BlockList;
BBInfo *PseudoEntry = BuildBlockList(BB, &BlockList);
// Special case: bail out if BB is unreachable.
if (BlockList.size() == 0) {
ValT V = Traits::GetUndefVal(BB, Updater);
(*AvailableVals)[BB] = V;
return V;
}
FindDominators(&BlockList, PseudoEntry);
FindPHIPlacement(&BlockList);
FindAvailableVals(&BlockList);
return BBMap[BB]->DefBB->AvailableVal;
}
/// BuildBlockList - Starting from the specified basic block, traverse back
/// through its predecessors until reaching blocks with known values.
/// Create BBInfo structures for the blocks and append them to the block
/// list.
BBInfo *BuildBlockList(BlkT *BB, BlockListTy *BlockList) {
SmallVector<BBInfo*, 10> RootList;
SmallVector<BBInfo*, 64> WorkList;
BBInfo *Info = new (Allocator) BBInfo(BB, 0);
BBMap[BB] = Info;
WorkList.push_back(Info);
// Search backward from BB, creating BBInfos along the way and stopping
// when reaching blocks that define the value. Record those defining
// blocks on the RootList.
SmallVector<BlkT*, 10> Preds;
while (!WorkList.empty()) {
Info = WorkList.pop_back_val();
Preds.clear();
Traits::FindPredecessorBlocks(Info->BB, &Preds);
Info->NumPreds = Preds.size();
if (Info->NumPreds == 0)
Info->Preds = 0;
else
Info->Preds = static_cast<BBInfo**>
(Allocator.Allocate(Info->NumPreds * sizeof(BBInfo*),
AlignOf<BBInfo*>::Alignment));
for (unsigned p = 0; p != Info->NumPreds; ++p) {
BlkT *Pred = Preds[p];
// Check if BBMap already has a BBInfo for the predecessor block.
typename BBMapTy::value_type &BBMapBucket =
BBMap.FindAndConstruct(Pred);
if (BBMapBucket.second) {
Info->Preds[p] = BBMapBucket.second;
continue;
}
// Create a new BBInfo for the predecessor.
ValT PredVal = AvailableVals->lookup(Pred);
BBInfo *PredInfo = new (Allocator) BBInfo(Pred, PredVal);
BBMapBucket.second = PredInfo;
Info->Preds[p] = PredInfo;
if (PredInfo->AvailableVal) {
RootList.push_back(PredInfo);
continue;
}
WorkList.push_back(PredInfo);
}
}
// Now that we know what blocks are backwards-reachable from the starting
// block, do a forward depth-first traversal to assign postorder numbers
// to those blocks.
BBInfo *PseudoEntry = new (Allocator) BBInfo(0, 0);
unsigned BlkNum = 1;
// Initialize the worklist with the roots from the backward traversal.
while (!RootList.empty()) {
Info = RootList.pop_back_val();
Info->IDom = PseudoEntry;
Info->BlkNum = -1;
WorkList.push_back(Info);
}
while (!WorkList.empty()) {
Info = WorkList.back();
if (Info->BlkNum == -2) {
// All the successors have been handled; assign the postorder number.
Info->BlkNum = BlkNum++;
// If not a root, put it on the BlockList.
if (!Info->AvailableVal)
BlockList->push_back(Info);
WorkList.pop_back();
continue;
}
// Leave this entry on the worklist, but set its BlkNum to mark that its
// successors have been put on the worklist. When it returns to the top
// the list, after handling its successors, it will be assigned a
// number.
Info->BlkNum = -2;
// Add unvisited successors to the work list.
for (typename Traits::BlkSucc_iterator SI =
Traits::BlkSucc_begin(Info->BB),
E = Traits::BlkSucc_end(Info->BB); SI != E; ++SI) {
BBInfo *SuccInfo = BBMap[*SI];
if (!SuccInfo || SuccInfo->BlkNum)
continue;
SuccInfo->BlkNum = -1;
WorkList.push_back(SuccInfo);
}
}
PseudoEntry->BlkNum = BlkNum;
return PseudoEntry;
}
/// IntersectDominators - This is the dataflow lattice "meet" operation for
/// finding dominators. Given two basic blocks, it walks up the dominator
/// tree until it finds a common dominator of both. It uses the postorder
/// number of the blocks to determine how to do that.
BBInfo *IntersectDominators(BBInfo *Blk1, BBInfo *Blk2) {
while (Blk1 != Blk2) {
while (Blk1->BlkNum < Blk2->BlkNum) {
Blk1 = Blk1->IDom;
if (!Blk1)
return Blk2;
}
while (Blk2->BlkNum < Blk1->BlkNum) {
Blk2 = Blk2->IDom;
if (!Blk2)
return Blk1;
}
}
return Blk1;
}
/// FindDominators - Calculate the dominator tree for the subset of the CFG
/// corresponding to the basic blocks on the BlockList. This uses the
/// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey
/// and Kennedy, published in Software--Practice and Experience, 2001,
/// 4:1-10. Because the CFG subset does not include any edges leading into
/// blocks that define the value, the results are not the usual dominator
/// tree. The CFG subset has a single pseudo-entry node with edges to a set
/// of root nodes for blocks that define the value. The dominators for this
/// subset CFG are not the standard dominators but they are adequate for
/// placing PHIs within the subset CFG.
void FindDominators(BlockListTy *BlockList, BBInfo *PseudoEntry) {
bool Changed;
do {
Changed = false;
// Iterate over the list in reverse order, i.e., forward on CFG edges.
for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
E = BlockList->rend(); I != E; ++I) {
BBInfo *Info = *I;
BBInfo *NewIDom = 0
|