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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
//===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
//
// 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 pass deletes dead arguments from internal functions. Dead argument
// elimination removes arguments which are directly dead, as well as arguments
// only passed into function calls as dead arguments of other functions. This
// pass also deletes dead arguments in a similar way.
//
// This pass is often useful as a cleanup pass to run after aggressive
// interprocedural passes, which add possibly-dead arguments.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constant.h"
#include "llvm/iOther.h"
#include "llvm/iTerminators.h"
#include "llvm/Support/CallSite.h"
#include "Support/Debug.h"
#include "Support/Statistic.h"
#include "Support/iterator"
#include <set>
namespace {
Statistic<> NumArgumentsEliminated("deadargelim",
"Number of unread args removed");
Statistic<> NumRetValsEliminated("deadargelim",
"Number of unused return values removed");
/// DAE - The dead argument elimination pass.
///
class DAE : public Pass {
/// DeleteFromExternalFunctions - Bugpoint sets this flag to indicate that
/// it is safe to hack apart functions without internal linkage.
bool DeleteFromExternalFunctions;
/// Liveness enum - During our initial pass over the program, we determine
/// that things are either definately alive, definately dead, or in need of
/// interprocedural analysis (MaybeLive).
///
enum Liveness { Live, MaybeLive, Dead };
/// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain
/// all of the arguments in the program. The Dead set contains arguments
/// which are completely dead (never used in the function). The MaybeLive
/// set contains arguments which are only passed into other function calls,
/// thus may be live and may be dead. The Live set contains arguments which
/// are known to be alive.
///
std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments;
/// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the
/// functions in the program. The Dead set contains functions whose return
/// value is known to be dead. The MaybeLive set contains functions whose
/// return values are only used by return instructions, and the Live set
/// contains functions whose return values are used, functions that are
/// external, and functions that already return void.
///
std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal;
/// InstructionsToInspect - As we mark arguments and return values
/// MaybeLive, we keep track of which instructions could make the values
/// live here. Once the entire program has had the return value and
/// arguments analyzed, this set is scanned to promote the MaybeLive objects
/// to be Live if they really are used.
std::vector<Instruction*> InstructionsToInspect;
/// CallSites - Keep track of the call sites of functions that have
/// MaybeLive arguments or return values.
std::multimap<Function*, CallSite> CallSites;
public:
DAE(bool DFEF = false) : DeleteFromExternalFunctions(DFEF) {}
bool run(Module &M);
private:
Liveness getArgumentLiveness(const Argument &A);
bool isMaybeLiveArgumentNowLive(Argument *Arg);
void SurveyFunction(Function &Fn);
void MarkArgumentLive(Argument *Arg);
void MarkRetValLive(Function *F);
void MarkReturnInstArgumentLive(ReturnInst *RI);
void RemoveDeadArgumentsFromFunction(Function *F);
};
RegisterOpt<DAE> X("deadargelim", "Dead Argument Elimination");
}
/// createDeadArgEliminationPass - This pass removes arguments from functions
/// which are not used by the body of the function. If
/// DeleteFromExternalFunctions is true, the pass will modify functions that
/// have external linkage, which is not usually safe (this is used by bugpoint
/// to reduce testcases).
///
Pass *createDeadArgEliminationPass(bool DeleteFromExternalFunctions) {
return new DAE(DeleteFromExternalFunctions);
}
static inline bool CallPassesValueThoughVararg(Instruction *Call,
const Value *Arg) {
CallSite CS = CallSite::get(Call);
const Type *CalledValueTy = CS.getCalledValue()->getType();
const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType();
unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams();
for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
AI != CS.arg_end(); ++AI)
if (AI->get() == Arg)
return true;
return false;
}
// getArgumentLiveness - Inspect an argument, determining if is known Live
// (used in a computation), MaybeLive (only passed as an argument to a call), or
// Dead (not used).
DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
if (A.use_empty()) return Dead; // First check, directly dead?
// Scan through all of the uses, looking for non-argument passing uses.
for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
// Return instructions do not immediately effect liveness.
if (isa<ReturnInst>(*I))
continue;
CallSite CS = CallSite::get(const_cast<User*>(*I));
if (!CS.getInstruction()) {
// If its used by something that is not a call or invoke, it's alive!
return Live;
}
// If it's an indirect call, mark it alive...
Function *Callee = CS.getCalledFunction();
if (!Callee) return Live;
// Check to see if it's passed through a va_arg area: if so, we cannot
// remove it.
if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
return Live; // If passed through va_arg area, we cannot remove it
}
return MaybeLive; // It must be used, but only as argument to a function
}
// SurveyFunction - This performs the initial survey of the specified function,
// checking out whether or not it uses any of its incoming arguments or whether
// any callers use the return value. This fills in the
// (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
//
// We consider arguments of non-internal functions to be intrinsically alive as
// well as arguments to functions which have their "address taken".
//
void DAE::SurveyFunction(Function &F) {
bool FunctionIntrinsicallyLive = false;
Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
if (!F.hasInternalLinkage() && !DeleteFromExternalFunctions)
FunctionIntrinsicallyLive = true;
else
for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
// If this use is anything other than a call site, the function is alive.
CallSite CS = CallSite::get(*I);
Instruction *TheCall = CS.getInstruction();
if (!TheCall) { // Not a direct call site?
FunctionIntrinsicallyLive = true;
break;
}
// Check to see if the return value is used...
if (RetValLiveness != Live)
for (Value::use_iterator I = TheCall->use_begin(),
E = TheCall->use_end(); I != E; ++I)
if (isa<ReturnInst>(cast<Instruction>(*I))) {
RetValLiveness = MaybeLive;
} else if (isa<CallInst>(cast<Instruction>(*I)) ||
isa<InvokeInst>(cast<Instruction>(*I))) {
if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
!CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
RetValLiveness = Live;
break;
} else {
RetValLiveness = MaybeLive;
}
} else {
RetValLiveness = Live;
break;
}
// If the function is PASSED IN as an argument, its address has been taken
for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
AI != E; ++AI)
if (AI->get() == &F) {
FunctionIntrinsicallyLive = true;
break;
}
if (FunctionIntrinsicallyLive) break;
}
if (FunctionIntrinsicallyLive) {
DEBUG(std::cerr << " Intrinsically live fn: " << F.getName() << "
|