1//===- DeadArgumentElimination.cpp - Eliminate dead arguments -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass deletes dead arguments from internal functions. Dead argument
10// elimination removes arguments which are directly dead, as well as arguments
11// only passed into function calls as dead arguments of other functions. This
12// pass also deletes dead return values in a similar way.
13//
14// This pass is often useful as a cleanup pass to run after aggressive
15// interprocedural passes, which add possibly-dead arguments or return values.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/OptimizationRemarkEmitter.h"
23#include "llvm/IR/Argument.h"
24#include "llvm/IR/AttributeMask.h"
25#include "llvm/IR/Attributes.h"
26#include "llvm/IR/BasicBlock.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DIBuilder.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/IRBuilder.h"
32#include "llvm/IR/InstrTypes.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/Intrinsics.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/NoFolder.h"
38#include "llvm/IR/PassManager.h"
39#include "llvm/IR/Type.h"
40#include "llvm/IR/Use.h"
41#include "llvm/IR/User.h"
42#include "llvm/IR/Value.h"
43#include "llvm/InitializePasses.h"
44#include "llvm/Pass.h"
45#include "llvm/Support/Casting.h"
46#include "llvm/Support/Debug.h"
47#include "llvm/Support/raw_ostream.h"
48#include "llvm/Transforms/IPO.h"
49#include "llvm/Transforms/Utils/BasicBlockUtils.h"
50#include <cassert>
51#include <utility>
52#include <vector>
53
54using namespace llvm;
55
56#define DEBUG_TYPE "deadargelim"
57
58STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
59STATISTIC(NumRetValsEliminated, "Number of unused return values removed");
60STATISTIC(NumArgumentsReplacedWithPoison,
61 "Number of unread args replaced with poison");
62
63namespace {
64
65/// The dead argument elimination pass.
66class DAE : public ModulePass {
67protected:
68 // DAH uses this to specify a different ID.
69 explicit DAE(char &ID) : ModulePass(ID) {}
70
71public:
72 static char ID; // Pass identification, replacement for typeid
73
74 DAE() : ModulePass(ID) {}
75
76 bool runOnModule(Module &M) override {
77 if (skipModule(M))
78 return false;
79 DeadArgumentEliminationPass DAEP;
80 ModuleAnalysisManager DummyMAM;
81 PreservedAnalyses PA = DAEP.run(M, DummyMAM);
82 return !PA.areAllPreserved();
83 }
84};
85
86} // end anonymous namespace
87
88char DAE::ID = 0;
89
90INITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false)
91
92/// This pass removes arguments from functions which are not used by the body of
93/// the function.
94ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
95
96/// If this is an function that takes a ... list, and if llvm.vastart is never
97/// called, the varargs list is dead for the function.
98bool DeadArgumentEliminationPass::deleteDeadVarargs(Function &F) {
99 assert(F.getFunctionType()->isVarArg() && "Function isn't varargs!");
100 if (F.isDeclaration() || !F.hasLocalLinkage())
101 return false;
102
103 // Ensure that the function is only directly called.
104 if (F.hasAddressTaken())
105 return false;
106
107 // Don't touch naked functions. The assembly might be using an argument, or
108 // otherwise rely on the frame layout in a way that this analysis will not
109 // see.
110 if (F.hasFnAttribute(Kind: Attribute::Naked)) {
111 return false;
112 }
113
114 // Okay, we know we can transform this function if safe. Scan its body
115 // looking for calls marked musttail or calls to llvm.vastart.
116 for (BasicBlock &BB : F) {
117 for (Instruction &I : BB) {
118 CallInst *CI = dyn_cast<CallInst>(Val: &I);
119 if (!CI)
120 continue;
121 if (CI->isMustTailCall())
122 return false;
123 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: CI)) {
124 if (II->getIntrinsicID() == Intrinsic::vastart)
125 return false;
126 }
127 }
128 }
129
130 // If we get here, there are no calls to llvm.vastart in the function body,
131 // remove the "..." and adjust all the calls.
132
133 // Start by computing a new prototype for the function, which is the same as
134 // the old function, but doesn't have isVarArg set.
135 FunctionType *FTy = F.getFunctionType();
136
137 std::vector<Type *> Params(FTy->param_begin(), FTy->param_end());
138 FunctionType *NFTy = FunctionType::get(Result: FTy->getReturnType(), Params, isVarArg: false);
139 unsigned NumArgs = Params.size();
140
141 // Create the new function body and insert it into the module...
142 Function *NF = Function::Create(Ty: NFTy, Linkage: F.getLinkage(), AddrSpace: F.getAddressSpace());
143 NF->copyAttributesFrom(Src: &F);
144 NF->setComdat(F.getComdat());
145 F.getParent()->getFunctionList().insert(where: F.getIterator(), New: NF);
146 NF->takeName(V: &F);
147
148 // Loop over all the callers of the function, transforming the call sites
149 // to pass in a smaller number of arguments into the new function.
150 //
151 std::vector<Value *> Args;
152 for (User *U : llvm::make_early_inc_range(Range: F.users())) {
153 CallBase *CB = dyn_cast<CallBase>(Val: U);
154 if (!CB)
155 continue;
156
157 // Pass all the same arguments.
158 Args.assign(first: CB->arg_begin(), last: CB->arg_begin() + NumArgs);
159
160 // Drop any attributes that were on the vararg arguments.
161 AttributeList PAL = CB->getAttributes();
162 if (!PAL.isEmpty()) {
163 SmallVector<AttributeSet, 8> ArgAttrs;
164 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
165 ArgAttrs.push_back(Elt: PAL.getParamAttrs(ArgNo));
166 PAL = AttributeList::get(C&: F.getContext(), FnAttrs: PAL.getFnAttrs(),
167 RetAttrs: PAL.getRetAttrs(), ArgAttrs);
168 }
169
170 SmallVector<OperandBundleDef, 1> OpBundles;
171 CB->getOperandBundlesAsDefs(Defs&: OpBundles);
172
173 CallBase *NewCB = nullptr;
174 if (InvokeInst *II = dyn_cast<InvokeInst>(Val: CB)) {
175 NewCB = InvokeInst::Create(Func: NF, IfNormal: II->getNormalDest(), IfException: II->getUnwindDest(),
176 Args, Bundles: OpBundles, NameStr: "", InsertBefore: CB->getIterator());
177 } else {
178 NewCB = CallInst::Create(Func: NF, Args, Bundles: OpBundles, NameStr: "", InsertBefore: CB->getIterator());
179 cast<CallInst>(Val: NewCB)->setTailCallKind(
180 cast<CallInst>(Val: CB)->getTailCallKind());
181 }
182 NewCB->setCallingConv(CB->getCallingConv());
183 NewCB->setAttributes(PAL);
184 NewCB->copyMetadata(SrcInst: *CB, WL: {LLVMContext::MD_prof, LLVMContext::MD_dbg});
185
186 Args.clear();
187
188 if (!CB->use_empty())
189 CB->replaceAllUsesWith(V: NewCB);
190
191 NewCB->takeName(V: CB);
192
193 // Finally, remove the old call from the program, reducing the use-count of
194 // F.
195 CB->eraseFromParent();
196 }
197
198 // Since we have now created the new function, splice the body of the old
199 // function right into the new function, leaving the old rotting hulk of the
200 // function empty.
201 NF->splice(ToIt: NF->begin(), FromF: &F);
202
203 // Loop over the argument list, transferring uses of the old arguments over to
204 // the new arguments, also transferring over the names as well. While we're
205 // at it, remove the dead arguments from the DeadArguments list.
206 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(),
207 I2 = NF->arg_begin();
208 I != E; ++I, ++I2) {
209 // Move the name and users over to the new version.
210 I->replaceAllUsesWith(V: &*I2);
211 I2->takeName(V: &*I);
212 }
213
214 // Clone metadata from the old function, including debug info descriptor.
215 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
216 F.getAllMetadata(MDs);
217 for (auto [KindID, Node] : MDs)
218 NF->addMetadata(KindID, MD&: *Node);
219
220 // Fix up any BlockAddresses that refer to the function.
221 F.replaceAllUsesWith(V: NF);
222 // Delete the bitcast that we just created, so that NF does not
223 // appear to be address-taken.
224 NF->removeDeadConstantUsers();
225 // Finally, nuke the old function.
226 F.eraseFromParent();
227 return true;
228}
229
230/// Checks if the given function has any arguments that are unused, and changes
231/// the caller parameters to be poison instead.
232bool DeadArgumentEliminationPass::removeDeadArgumentsFromCallers(Function &F) {
233 // We cannot change the arguments if this TU does not define the function or
234 // if the linker may choose a function body from another TU, even if the
235 // nominal linkage indicates that other copies of the function have the same
236 // semantics. In the below example, the dead load from %p may not have been
237 // eliminated from the linker-chosen copy of f, so replacing %p with poison
238 // in callers may introduce undefined behavior.
239 //
240 // define linkonce_odr void @f(i32* %p) {
241 // %v = load i32 %p
242 // ret void
243 // }
244 if (!F.hasExactDefinition())
245 return false;
246
247 // Functions with local linkage should already have been handled, except if
248 // they are fully alive (e.g., called indirectly) and except for the fragile
249 // (variadic) ones. In these cases, we may still be able to improve their
250 // statically known call sites.
251 if ((F.hasLocalLinkage() && !FrozenFunctions.count(x: &F)) &&
252 !F.getFunctionType()->isVarArg())
253 return false;
254
255 // Don't touch naked functions. The assembly might be using an argument, or
256 // otherwise rely on the frame layout in a way that this analysis will not
257 // see.
258 if (F.hasFnAttribute(Kind: Attribute::Naked))
259 return false;
260
261 if (F.use_empty())
262 return false;
263
264 SmallVector<unsigned, 8> UnusedArgs;
265 bool Changed = false;
266
267 AttributeMask UBImplyingAttributes =
268 AttributeFuncs::getUBImplyingAttributes();
269 for (Argument &Arg : F.args()) {
270 if (!Arg.hasSwiftErrorAttr() && Arg.use_empty() &&
271 !Arg.hasPassPointeeByValueCopyAttr()) {
272 if (Arg.isUsedByMetadata()) {
273 Arg.replaceAllUsesWith(V: PoisonValue::get(T: Arg.getType()));
274 Changed = true;
275 }
276 UnusedArgs.push_back(Elt: Arg.getArgNo());
277 F.removeParamAttrs(ArgNo: Arg.getArgNo(), Attrs: UBImplyingAttributes);
278 }
279 }
280
281 if (UnusedArgs.empty())
282 return false;
283
284 for (Use &U : F.uses()) {
285 CallBase *CB = dyn_cast<CallBase>(Val: U.getUser());
286 if (!CB || !CB->isCallee(U: &U) ||
287 CB->getFunctionType() != F.getFunctionType())
288 continue;
289
290 // Now go through all unused args and replace them with poison.
291 for (unsigned ArgNo : UnusedArgs) {
292 Value *Arg = CB->getArgOperand(i: ArgNo);
293 CB->setArgOperand(i: ArgNo, v: PoisonValue::get(T: Arg->getType()));
294 CB->removeParamAttrs(ArgNo, AttrsToRemove: UBImplyingAttributes);
295
296 ++NumArgumentsReplacedWithPoison;
297 Changed = true;
298 }
299 }
300
301 return Changed;
302}
303
304/// Convenience function that returns the number of return values. It returns 0
305/// for void functions and 1 for functions not returning a struct. It returns
306/// the number of struct elements for functions returning a struct.
307static unsigned numRetVals(const Function *F) {
308 Type *RetTy = F->getReturnType();
309 if (RetTy->isVoidTy())
310 return 0;
311 if (StructType *STy = dyn_cast<StructType>(Val: RetTy))
312 return STy->getNumElements();
313 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: RetTy))
314 return ATy->getNumElements();
315 return 1;
316}
317
318/// Returns the sub-type a function will return at a given Idx. Should
319/// correspond to the result type of an ExtractValue instruction executed with
320/// just that one Idx (i.e. only top-level structure is considered).
321static Type *getRetComponentType(const Function *F, unsigned Idx) {
322 Type *RetTy = F->getReturnType();
323 assert(!RetTy->isVoidTy() && "void type has no subtype");
324
325 if (StructType *STy = dyn_cast<StructType>(Val: RetTy))
326 return STy->getElementType(N: Idx);
327 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: RetTy))
328 return ATy->getElementType();
329 return RetTy;
330}
331
332/// Checks Use for liveness in LiveValues. If Use is not live, it adds Use to
333/// the MaybeLiveUses argument. Returns the determined liveness of Use.
334DeadArgumentEliminationPass::Liveness
335DeadArgumentEliminationPass::markIfNotLive(RetOrArg Use,
336 UseVector &MaybeLiveUses) {
337 // We're live if our use or its Function is already marked as live.
338 if (isLive(RA: Use))
339 return Live;
340
341 // We're maybe live otherwise, but remember that we must become live if
342 // Use becomes live.
343 MaybeLiveUses.push_back(Elt: Use);
344 return MaybeLive;
345}
346
347/// Looks at a single use of an argument or return value and determines if it
348/// should be alive or not. Adds this use to MaybeLiveUses if it causes the
349/// used value to become MaybeLive.
350///
351/// RetValNum is the return value number to use when this use is used in a
352/// return instruction. This is used in the recursion, you should always leave
353/// it at 0.
354DeadArgumentEliminationPass::Liveness
355DeadArgumentEliminationPass::surveyUse(const Use *U, UseVector &MaybeLiveUses,
356 unsigned RetValNum) {
357 const User *V = U->getUser();
358 if (const ReturnInst *RI = dyn_cast<ReturnInst>(Val: V)) {
359 // The value is returned from a function. It's only live when the
360 // function's return value is live. We use RetValNum here, for the case
361 // that U is really a use of an insertvalue instruction that uses the
362 // original Use.
363 const Function *F = RI->getParent()->getParent();
364 if (RetValNum != -1U) {
365 RetOrArg Use = createRet(F, Idx: RetValNum);
366 // We might be live, depending on the liveness of Use.
367 return markIfNotLive(Use, MaybeLiveUses);
368 }
369
370 DeadArgumentEliminationPass::Liveness Result = MaybeLive;
371 for (unsigned Ri = 0; Ri < numRetVals(F); ++Ri) {
372 RetOrArg Use = createRet(F, Idx: Ri);
373 // We might be live, depending on the liveness of Use. If any
374 // sub-value is live, then the entire value is considered live. This
375 // is a conservative choice, and better tracking is possible.
376 DeadArgumentEliminationPass::Liveness SubResult =
377 markIfNotLive(Use, MaybeLiveUses);
378 if (Result != Live)
379 Result = SubResult;
380 }
381 return Result;
382 }
383
384 if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(Val: V)) {
385 if (U->getOperandNo() != InsertValueInst::getAggregateOperandIndex() &&
386 IV->hasIndices())
387 // The use we are examining is inserted into an aggregate. Our liveness
388 // depends on all uses of that aggregate, but if it is used as a return
389 // value, only index at which we were inserted counts.
390 RetValNum = *IV->idx_begin();
391
392 // Note that if we are used as the aggregate operand to the insertvalue,
393 // we don't change RetValNum, but do survey all our uses.
394
395 Liveness Result = MaybeLive;
396 for (const Use &UU : IV->uses()) {
397 Result = surveyUse(U: &UU, MaybeLiveUses, RetValNum);
398 if (Result == Live)
399 break;
400 }
401 return Result;
402 }
403
404 if (const auto *CB = dyn_cast<CallBase>(Val: V)) {
405 const Function *F = CB->getCalledFunction();
406 if (F) {
407 // Used in a direct call.
408
409 // The function argument is live if it is used as a bundle operand.
410 if (CB->isBundleOperand(U))
411 return Live;
412
413 // Find the argument number. We know for sure that this use is an
414 // argument, since if it was the function argument this would be an
415 // indirect call and that we know can't be looking at a value of the
416 // label type (for the invoke instruction).
417 unsigned ArgNo = CB->getArgOperandNo(U);
418
419 if (ArgNo >= F->getFunctionType()->getNumParams())
420 // The value is passed in through a vararg! Must be live.
421 return Live;
422
423 assert(CB->getArgOperand(ArgNo) == CB->getOperand(U->getOperandNo()) &&
424 "Argument is not where we expected it");
425
426 // Value passed to a normal call. It's only live when the corresponding
427 // argument to the called function turns out live.
428 RetOrArg Use = createArg(F, Idx: ArgNo);
429 return markIfNotLive(Use, MaybeLiveUses);
430 }
431 }
432 // Used in any other way? Value must be live.
433 return Live;
434}
435
436/// Looks at all the uses of the given value
437/// Returns the Liveness deduced from the uses of this value.
438///
439/// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If
440/// the result is Live, MaybeLiveUses might be modified but its content should
441/// be ignored (since it might not be complete).
442DeadArgumentEliminationPass::Liveness
443DeadArgumentEliminationPass::surveyUses(const Value *V,
444 UseVector &MaybeLiveUses) {
445 // Assume it's dead (which will only hold if there are no uses at all..).
446 Liveness Result = MaybeLive;
447 // Check each use.
448 for (const Use &U : V->uses()) {
449 Result = surveyUse(U: &U, MaybeLiveUses);
450 if (Result == Live)
451 break;
452 }
453 return Result;
454}
455
456/// Performs the initial survey of the specified function, checking out whether
457/// it uses any of its incoming arguments or whether any callers use the return
458/// value. This fills in the LiveValues set and Uses map.
459///
460/// We consider arguments of non-internal functions to be intrinsically alive as
461/// well as arguments to functions which have their "address taken".
462void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
463 // Can only change function signature for functions with local linkage.
464 if (!F.hasLocalLinkage()) {
465 markFrozen(F);
466 return;
467 }
468
469 // Functions with inalloca/preallocated parameters are expecting args in a
470 // particular register and memory layout.
471 if (F.getAttributes().hasAttrSomewhere(Kind: Attribute::InAlloca) ||
472 F.getAttributes().hasAttrSomewhere(Kind: Attribute::Preallocated)) {
473 markFrozen(F);
474 return;
475 }
476
477 // Don't touch naked functions. The assembly might be using an argument, or
478 // otherwise rely on the frame layout in a way that this analysis will not
479 // see.
480 if (F.hasFnAttribute(Kind: Attribute::Naked)) {
481 markFrozen(F);
482 return;
483 }
484
485 // Ensure function definition is available for interprocedural analysis.
486 if (!F.isDefinitionExact()) {
487 markFrozen(F);
488 return;
489 }
490
491 unsigned RetCount = numRetVals(F: &F);
492
493 // Assume all return values are dead
494 using RetVals = SmallVector<Liveness, 5>;
495
496 RetVals RetValLiveness(RetCount, MaybeLive);
497
498 using RetUses = SmallVector<UseVector, 5>;
499
500 // These vectors map each return value to the uses that make it MaybeLive, so
501 // we can add those to the Uses map if the return value really turns out to be
502 // MaybeLive. Initialized to a list of RetCount empty lists.
503 RetUses MaybeLiveRetUses(RetCount);
504
505 for (const BasicBlock &BB : F) {
506 if (BB.getTerminatingMustTailCall()) {
507 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - " << F.getName()
508 << " has musttail calls\n");
509 if (markFnOrRetTyFrozenOnMusttail(F))
510 return;
511 }
512 }
513
514 LLVM_DEBUG(
515 dbgs() << "DeadArgumentEliminationPass - Inspecting callers for fn: "
516 << F.getName() << "\n");
517 // Keep track of the number of live retvals, so we can skip checks once all
518 // of them turn out to be live.
519 unsigned NumLiveRetVals = 0;
520
521 // Loop all uses of the function.
522 for (const Use &U : F.uses()) {
523 // If the function is PASSED IN as an argument, its address has been
524 // taken.
525 const auto *CB = dyn_cast<CallBase>(Val: U.getUser());
526 if (!CB || !CB->isCallee(U: &U) ||
527 CB->getFunctionType() != F.getFunctionType()) {
528 markFrozen(F);
529 return;
530 }
531
532 if (CB->isMustTailCall()) {
533 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - " << F.getName()
534 << " has musttail callers\n");
535 if (markFnOrRetTyFrozenOnMusttail(F))
536 return;
537 }
538
539 // If we end up here, we are looking at a direct call to our function.
540
541 // Now, check how our return value(s) is/are used in this caller. Don't
542 // bother checking return values if all of them are live already.
543 if (NumLiveRetVals == RetCount)
544 continue;
545
546 // Check all uses of the return value.
547 for (const Use &UU : CB->uses()) {
548 if (ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(Val: UU.getUser())) {
549 // This use uses a part of our return value, survey the uses of
550 // that part and store the results for this index only.
551 unsigned Idx = *Ext->idx_begin();
552 if (RetValLiveness[Idx] != Live) {
553 RetValLiveness[Idx] = surveyUses(V: Ext, MaybeLiveUses&: MaybeLiveRetUses[Idx]);
554 if (RetValLiveness[Idx] == Live)
555 NumLiveRetVals++;
556 }
557 } else {
558 // Used by something else than extractvalue. Survey, but assume that the
559 // result applies to all sub-values.
560 UseVector MaybeLiveAggregateUses;
561 if (surveyUse(U: &UU, MaybeLiveUses&: MaybeLiveAggregateUses) == Live) {
562 NumLiveRetVals = RetCount;
563 RetValLiveness.assign(NumElts: RetCount, Elt: Live);
564 break;
565 }
566
567 for (unsigned Ri = 0; Ri != RetCount; ++Ri) {
568 if (RetValLiveness[Ri] != Live)
569 MaybeLiveRetUses[Ri].append(in_start: MaybeLiveAggregateUses.begin(),
570 in_end: MaybeLiveAggregateUses.end());
571 }
572 }
573 }
574 }
575
576 // Now we've inspected all callers, record the liveness of our return values.
577 for (unsigned Ri = 0; Ri != RetCount; ++Ri)
578 markValue(RA: createRet(F: &F, Idx: Ri), L: RetValLiveness[Ri], MaybeLiveUses: MaybeLiveRetUses[Ri]);
579
580 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting args for fn: "
581 << F.getName() << "\n");
582
583 // Now, check all of our arguments.
584 unsigned ArgI = 0;
585 UseVector MaybeLiveArgUses;
586 for (Function::const_arg_iterator AI = F.arg_begin(), E = F.arg_end();
587 AI != E; ++AI, ++ArgI) {
588 Liveness Result;
589 if (F.getFunctionType()->isVarArg()) {
590 // Variadic functions will already have a va_arg function expanded inside
591 // them, making them potentially very sensitive to ABI changes resulting
592 // from removing arguments entirely, so don't. For example AArch64 handles
593 // register and stack HFAs very differently, and this is reflected in the
594 // IR which has already been generated.
595 Result = Live;
596 } else {
597 // See what the effect of this use is (recording any uses that cause
598 // MaybeLive in MaybeLiveArgUses).
599 Result = surveyUses(V: &*AI, MaybeLiveUses&: MaybeLiveArgUses);
600 }
601
602 // Mark the result.
603 markValue(RA: createArg(F: &F, Idx: ArgI), L: Result, MaybeLiveUses: MaybeLiveArgUses);
604 // Clear the vector again for the next iteration.
605 MaybeLiveArgUses.clear();
606 }
607}
608
609/// Marks the liveness of RA depending on L. If L is MaybeLive, it also takes
610/// all uses in MaybeLiveUses and records them in Uses, such that RA will be
611/// marked live if any use in MaybeLiveUses gets marked live later on.
612void DeadArgumentEliminationPass::markValue(const RetOrArg &RA, Liveness L,
613 const UseVector &MaybeLiveUses) {
614 switch (L) {
615 case Live:
616 markLive(RA);
617 break;
618 case MaybeLive:
619 assert(!isLive(RA) && "Use is already live!");
620 for (const auto &MaybeLiveUse : MaybeLiveUses) {
621 if (isLive(RA: MaybeLiveUse)) {
622 // A use is live, so this value is live.
623 markLive(RA);
624 break;
625 }
626 // Note any uses of this value, so this value can be
627 // marked live whenever one of the uses becomes live.
628 Uses.emplace(args: MaybeLiveUse, args: RA);
629 }
630 break;
631 }
632}
633
634/// Return true if we freeze the whole function.
635/// If the calling convention is not swifttailcc or tailcc, the caller and
636/// callee of musttail must have exactly the same signature. Otherwise we
637/// only needs to guarantee they have the same return type.
638bool DeadArgumentEliminationPass::markFnOrRetTyFrozenOnMusttail(
639 const Function &F) {
640 if (F.getCallingConv() != CallingConv::SwiftTail ||
641 F.getCallingConv() != CallingConv::Tail) {
642 markFrozen(F);
643 return true;
644 } else {
645 markRetTyFrozen(F);
646 return false;
647 }
648}
649
650/// Mark the given Function as alive, meaning that it cannot be changed in any
651/// way. Additionally, mark any values that are used as this function's
652/// parameters or by its return values (according to Uses) live as well.
653void DeadArgumentEliminationPass::markFrozen(const Function &F) {
654 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - frozen fn: "
655 << F.getName() << "\n");
656 // Mark the function as frozen.
657 FrozenFunctions.insert(x: &F);
658 // Mark all arguments as live.
659 for (unsigned ArgI = 0, E = F.arg_size(); ArgI != E; ++ArgI)
660 propagateLiveness(RA: createArg(F: &F, Idx: ArgI));
661 // Mark all return values as live.
662 for (unsigned Ri = 0, E = numRetVals(F: &F); Ri != E; ++Ri)
663 propagateLiveness(RA: createRet(F: &F, Idx: Ri));
664}
665
666void DeadArgumentEliminationPass::markRetTyFrozen(const Function &F) {
667 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - frozen return type fn: "
668 << F.getName() << "\n");
669 FrozenRetTyFunctions.insert(x: &F);
670}
671
672/// Mark the given return value or argument as live. Additionally, mark any
673/// values that are used by this value (according to Uses) live as well.
674void DeadArgumentEliminationPass::markLive(const RetOrArg &RA) {
675 if (isLive(RA))
676 return; // Already marked Live.
677
678 LiveValues.insert(x: RA);
679
680 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Marking "
681 << RA.getDescription() << " live\n");
682 propagateLiveness(RA);
683}
684
685bool DeadArgumentEliminationPass::isLive(const RetOrArg &RA) {
686 return FrozenFunctions.count(x: RA.F) || LiveValues.count(x: RA);
687}
688
689/// Given that RA is a live value, propagate it's liveness to any other values
690/// it uses (according to Uses).
691void DeadArgumentEliminationPass::propagateLiveness(const RetOrArg &RA) {
692 // We don't use upper_bound (or equal_range) here, because our recursive call
693 // to ourselves is likely to cause the upper_bound (which is the first value
694 // not belonging to RA) to become erased and the iterator invalidated.
695 UseMap::iterator Begin = Uses.lower_bound(x: RA);
696 UseMap::iterator E = Uses.end();
697 UseMap::iterator I;
698 for (I = Begin; I != E && I->first == RA; ++I)
699 markLive(RA: I->second);
700
701 // Erase RA from the Uses map (from the lower bound to wherever we ended up
702 // after the loop).
703 Uses.erase(first: Begin, last: I);
704}
705
706/// Remove any arguments and return values from F that are not in LiveValues.
707/// Transform the function and all the callees of the function to not have these
708/// arguments and return values.
709bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
710 // Don't modify frozen functions
711 if (FrozenFunctions.count(x: F))
712 return false;
713
714 // Start by computing a new prototype for the function, which is the same as
715 // the old function, but has fewer arguments and a different return type.
716 FunctionType *FTy = F->getFunctionType();
717 std::vector<Type *> Params;
718
719 // Keep track of if we have a live 'returned' argument
720 bool HasLiveReturnedArg = false;
721
722 // Set up to build a new list of parameter attributes.
723 SmallVector<AttributeSet, 8> ArgAttrVec;
724 const AttributeList &PAL = F->getAttributes();
725 OptimizationRemarkEmitter ORE(F);
726
727 // Remember which arguments are still alive.
728 SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
729 // Construct the new parameter list from non-dead arguments. Also construct
730 // a new set of parameter attributes to correspond. Skip the first parameter
731 // attribute, since that belongs to the return value.
732 unsigned ArgI = 0;
733 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
734 ++I, ++ArgI) {
735 RetOrArg Arg = createArg(F, Idx: ArgI);
736 if (LiveValues.erase(x: Arg)) {
737 Params.push_back(x: I->getType());
738 ArgAlive[ArgI] = true;
739 ArgAttrVec.push_back(Elt: PAL.getParamAttrs(ArgNo: ArgI));
740 HasLiveReturnedArg |= PAL.hasParamAttr(ArgNo: ArgI, Kind: Attribute::Returned);
741 } else {
742 ++NumArgumentsEliminated;
743
744 ORE.emit(RemarkBuilder: [&]() {
745 return OptimizationRemark(DEBUG_TYPE, "ArgumentRemoved", F)
746 << "eliminating argument " << ore::NV("ArgName", I->getName())
747 << "(" << ore::NV("ArgIndex", ArgI) << ")";
748 });
749 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument "
750 << ArgI << " (" << I->getName() << ") from "
751 << F->getName() << "\n");
752 }
753 }
754
755 // Find out the new return value.
756 Type *RetTy = FTy->getReturnType();
757 Type *NRetTy = nullptr;
758 unsigned RetCount = numRetVals(F);
759
760 // -1 means unused, other numbers are the new index
761 SmallVector<int, 5> NewRetIdxs(RetCount, -1);
762 std::vector<Type *> RetTypes;
763
764 // If there is a function with a live 'returned' argument but a dead return
765 // value, then there are two possible actions:
766 // 1) Eliminate the return value and take off the 'returned' attribute on the
767 // argument.
768 // 2) Retain the 'returned' attribute and treat the return value (but not the
769 // entire function) as live so that it is not eliminated.
770 //
771 // It's not clear in the general case which option is more profitable because,
772 // even in the absence of explicit uses of the return value, code generation
773 // is free to use the 'returned' attribute to do things like eliding
774 // save/restores of registers across calls. Whether this happens is target and
775 // ABI-specific as well as depending on the amount of register pressure, so
776 // there's no good way for an IR-level pass to figure this out.
777 //
778 // Fortunately, the only places where 'returned' is currently generated by
779 // the FE are places where 'returned' is basically free and almost always a
780 // performance win, so the second option can just be used always for now.
781 //
782 // This should be revisited if 'returned' is ever applied more liberally.
783 if (RetTy->isVoidTy() || HasLiveReturnedArg ||
784 FrozenRetTyFunctions.count(x: F)) {
785 NRetTy = RetTy;
786 } else {
787 // Look at each of the original return values individually.
788 for (unsigned Ri = 0; Ri != RetCount; ++Ri) {
789 RetOrArg Ret = createRet(F, Idx: Ri);
790 if (LiveValues.erase(x: Ret)) {
791 RetTypes.push_back(x: getRetComponentType(F, Idx: Ri));
792 NewRetIdxs[Ri] = RetTypes.size() - 1;
793 } else {
794 ++NumRetValsEliminated;
795
796 ORE.emit(RemarkBuilder: [&]() {
797 return OptimizationRemark(DEBUG_TYPE, "ReturnValueRemoved", F)
798 << "removing return value " << std::to_string(val: Ri);
799 });
800 LLVM_DEBUG(
801 dbgs() << "DeadArgumentEliminationPass - Removing return value "
802 << Ri << " from " << F->getName() << "\n");
803 }
804 }
805 if (RetTypes.size() > 1) {
806 // More than one return type? Reduce it down to size.
807 if (StructType *STy = dyn_cast<StructType>(Val: RetTy)) {
808 // Make the new struct packed if we used to return a packed struct
809 // already.
810 NRetTy = StructType::get(Context&: STy->getContext(), Elements: RetTypes, isPacked: STy->isPacked());
811 } else {
812 assert(isa<ArrayType>(RetTy) && "unexpected multi-value return");
813 NRetTy = ArrayType::get(ElementType: RetTypes[0], NumElements: RetTypes.size());
814 }
815 } else if (RetTypes.size() == 1)
816 // One return type? Just a simple value then, but only if we didn't use to
817 // return a struct with that simple value before.
818 NRetTy = RetTypes.front();
819 else if (RetTypes.empty())
820 // No return types? Make it void, but only if we didn't use to return {}.
821 NRetTy = Type::getVoidTy(C&: F->getContext());
822 }
823
824 assert(NRetTy && "No new return type found?");
825
826 // The existing function return attributes.
827 AttrBuilder RAttrs(F->getContext(), PAL.getRetAttrs());
828
829 // Remove any incompatible attributes, but only if we removed all return
830 // values. Otherwise, ensure that we don't have any conflicting attributes
831 // here. Currently, this should not be possible, but special handling might be
832 // required when new return value attributes are added.
833 if (NRetTy->isVoidTy())
834 RAttrs.remove(AM: AttributeFuncs::typeIncompatible(Ty: NRetTy, AS: PAL.getRetAttrs()));
835 else
836 assert(!RAttrs.overlaps(
837 AttributeFuncs::typeIncompatible(NRetTy, PAL.getRetAttrs())) &&
838 "Return attributes no longer compatible?");
839
840 AttributeSet RetAttrs = AttributeSet::get(C&: F->getContext(), B: RAttrs);
841
842 // Strip allocsize attributes. They might refer to the deleted arguments.
843 AttributeSet FnAttrs =
844 PAL.getFnAttrs().removeAttribute(C&: F->getContext(), Kind: Attribute::AllocSize);
845
846 // Reconstruct the AttributesList based on the vector we constructed.
847 assert(ArgAttrVec.size() == Params.size());
848 AttributeList NewPAL =
849 AttributeList::get(C&: F->getContext(), FnAttrs, RetAttrs, ArgAttrs: ArgAttrVec);
850
851 // Create the new function type based on the recomputed parameters.
852 FunctionType *NFTy = FunctionType::get(Result: NRetTy, Params, isVarArg: FTy->isVarArg());
853
854 // No change?
855 if (NFTy == FTy)
856 return false;
857
858 // Create the new function body and insert it into the module...
859 Function *NF = Function::Create(Ty: NFTy, Linkage: F->getLinkage(), AddrSpace: F->getAddressSpace());
860 NF->copyAttributesFrom(Src: F);
861 NF->setComdat(F->getComdat());
862 NF->setAttributes(NewPAL);
863 // Insert the new function before the old function, so we won't be processing
864 // it again.
865 F->getParent()->getFunctionList().insert(where: F->getIterator(), New: NF);
866 NF->takeName(V: F);
867
868 // Loop over all the callers of the function, transforming the call sites to
869 // pass in a smaller number of arguments into the new function.
870 std::vector<Value *> Args;
871 while (!F->use_empty()) {
872 CallBase &CB = cast<CallBase>(Val&: *F->user_back());
873
874 ArgAttrVec.clear();
875 const AttributeList &CallPAL = CB.getAttributes();
876
877 // Adjust the call return attributes in case the function was changed to
878 // return void.
879 AttrBuilder RAttrs(F->getContext(), CallPAL.getRetAttrs());
880 RAttrs.remove(
881 AM: AttributeFuncs::typeIncompatible(Ty: NRetTy, AS: CallPAL.getRetAttrs()));
882 AttributeSet RetAttrs = AttributeSet::get(C&: F->getContext(), B: RAttrs);
883
884 // Declare these outside of the loops, so we can reuse them for the second
885 // loop, which loops the varargs.
886 auto *I = CB.arg_begin();
887 unsigned Pi = 0;
888 // Loop over those operands, corresponding to the normal arguments to the
889 // original function, and add those that are still alive.
890 for (unsigned E = FTy->getNumParams(); Pi != E; ++I, ++Pi)
891 if (ArgAlive[Pi]) {
892 Args.push_back(x: *I);
893 // Get original parameter attributes, but skip return attributes.
894 AttributeSet Attrs = CallPAL.getParamAttrs(ArgNo: Pi);
895 if (NRetTy != RetTy && Attrs.hasAttribute(Kind: Attribute::Returned)) {
896 // If the return type has changed, then get rid of 'returned' on the
897 // call site. The alternative is to make all 'returned' attributes on
898 // call sites keep the return value alive just like 'returned'
899 // attributes on function declaration, but it's less clearly a win and
900 // this is not an expected case anyway
901 ArgAttrVec.push_back(Elt: AttributeSet::get(
902 C&: F->getContext(), B: AttrBuilder(F->getContext(), Attrs)
903 .removeAttribute(Val: Attribute::Returned)));
904 } else {
905 // Otherwise, use the original attributes.
906 ArgAttrVec.push_back(Elt: Attrs);
907 }
908 }
909
910 // Push any varargs arguments on the list. Don't forget their attributes.
911 for (auto *E = CB.arg_end(); I != E; ++I, ++Pi) {
912 Args.push_back(x: *I);
913 ArgAttrVec.push_back(Elt: CallPAL.getParamAttrs(ArgNo: Pi));
914 }
915
916 // Reconstruct the AttributesList based on the vector we constructed.
917 assert(ArgAttrVec.size() == Args.size());
918
919 // Again, be sure to remove any allocsize attributes, since their indices
920 // may now be incorrect.
921 AttributeSet FnAttrs = CallPAL.getFnAttrs().removeAttribute(
922 C&: F->getContext(), Kind: Attribute::AllocSize);
923
924 AttributeList NewCallPAL =
925 AttributeList::get(C&: F->getContext(), FnAttrs, RetAttrs, ArgAttrs: ArgAttrVec);
926
927 SmallVector<OperandBundleDef, 1> OpBundles;
928 CB.getOperandBundlesAsDefs(Defs&: OpBundles);
929
930 CallBase *NewCB = nullptr;
931 if (InvokeInst *II = dyn_cast<InvokeInst>(Val: &CB)) {
932 NewCB = InvokeInst::Create(Func: NF, IfNormal: II->getNormalDest(), IfException: II->getUnwindDest(),
933 Args, Bundles: OpBundles, NameStr: "", InsertBefore: CB.getParent());
934 } else {
935 NewCB = CallInst::Create(Ty: NFTy, Func: NF, Args, Bundles: OpBundles, NameStr: "", InsertBefore: CB.getIterator());
936 cast<CallInst>(Val: NewCB)->setTailCallKind(
937 cast<CallInst>(Val: &CB)->getTailCallKind());
938 }
939 NewCB->setCallingConv(CB.getCallingConv());
940 NewCB->setAttributes(NewCallPAL);
941 NewCB->copyMetadata(SrcInst: CB, WL: {LLVMContext::MD_prof, LLVMContext::MD_dbg});
942 Args.clear();
943 ArgAttrVec.clear();
944
945 if (!CB.use_empty() || CB.isUsedByMetadata()) {
946 if (NewCB->getType() == CB.getType()) {
947 // Return type not changed? Just replace users then.
948 CB.replaceAllUsesWith(V: NewCB);
949 NewCB->takeName(V: &CB);
950 } else if (NewCB->getType()->isVoidTy()) {
951 // If the return value is dead, replace any uses of it with poison
952 // (any non-debug value uses will get removed later on).
953 CB.replaceAllUsesWith(V: PoisonValue::get(T: CB.getType()));
954 } else {
955 assert((RetTy->isStructTy() || RetTy->isArrayTy()) &&
956 "Return type changed, but not into a void. The old return type"
957 " must have been a struct or an array!");
958 Instruction *InsertPt = &CB;
959 if (InvokeInst *II = dyn_cast<InvokeInst>(Val: &CB)) {
960 BasicBlock *NewEdge =
961 SplitEdge(From: NewCB->getParent(), To: II->getNormalDest());
962 InsertPt = &*NewEdge->getFirstInsertionPt();
963 }
964
965 // We used to return a struct or array. Instead of doing smart stuff
966 // with all the uses, we will just rebuild it using extract/insertvalue
967 // chaining and let instcombine clean that up.
968 //
969 // Start out building up our return value from poison
970 Value *RetVal = PoisonValue::get(T: RetTy);
971 for (unsigned Ri = 0; Ri != RetCount; ++Ri)
972 if (NewRetIdxs[Ri] != -1) {
973 Value *V;
974 IRBuilder<NoFolder> IRB(InsertPt);
975 if (RetTypes.size() > 1)
976 // We are still returning a struct, so extract the value from our
977 // return value
978 V = IRB.CreateExtractValue(Agg: NewCB, Idxs: NewRetIdxs[Ri], Name: "newret");
979 else
980 // We are now returning a single element, so just insert that
981 V = NewCB;
982 // Insert the value at the old position
983 RetVal = IRB.CreateInsertValue(Agg: RetVal, Val: V, Idxs: Ri, Name: "oldret");
984 }
985 // Now, replace all uses of the old call instruction with the return
986 // struct we built
987 CB.replaceAllUsesWith(V: RetVal);
988 NewCB->takeName(V: &CB);
989 }
990 }
991
992 // Finally, remove the old call from the program, reducing the use-count of
993 // F.
994 CB.eraseFromParent();
995 }
996
997 // Since we have now created the new function, splice the body of the old
998 // function right into the new function, leaving the old rotting hulk of the
999 // function empty.
1000 NF->splice(ToIt: NF->begin(), FromF: F);
1001
1002 // Loop over the argument list, transferring uses of the old arguments over to
1003 // the new arguments, also transferring over the names as well.
1004 ArgI = 0;
1005 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
1006 I2 = NF->arg_begin();
1007 I != E; ++I, ++ArgI)
1008 if (ArgAlive[ArgI]) {
1009 // If this is a live argument, move the name and users over to the new
1010 // version.
1011 I->replaceAllUsesWith(V: &*I2);
1012 I2->takeName(V: &*I);
1013 ++I2;
1014 } else {
1015 // If this argument is dead, replace any uses of it with poison
1016 // (any non-debug value uses will get removed later on).
1017 I->replaceAllUsesWith(V: PoisonValue::get(T: I->getType()));
1018 }
1019
1020 // If we change the return value of the function we must rewrite any return
1021 // instructions. Check this now.
1022 if (F->getReturnType() != NF->getReturnType())
1023 for (BasicBlock &BB : *NF)
1024 if (ReturnInst *RI = dyn_cast<ReturnInst>(Val: BB.getTerminator())) {
1025 IRBuilder<NoFolder> IRB(RI);
1026 Value *RetVal = nullptr;
1027
1028 if (!NFTy->getReturnType()->isVoidTy()) {
1029 assert(RetTy->isStructTy() || RetTy->isArrayTy());
1030 // The original return value was a struct or array, insert
1031 // extractvalue/insertvalue chains to extract only the values we need
1032 // to return and insert them into our new result.
1033 // This does generate messy code, but we'll let it to instcombine to
1034 // clean that up.
1035 Value *OldRet = RI->getOperand(i_nocapture: 0);
1036 // Start out building up our return value from poison
1037 RetVal = PoisonValue::get(T: NRetTy);
1038 for (unsigned RetI = 0; RetI != RetCount; ++RetI)
1039 if (NewRetIdxs[RetI] != -1) {
1040 Value *EV = IRB.CreateExtractValue(Agg: OldRet, Idxs: RetI, Name: "oldret");
1041
1042 if (RetTypes.size() > 1) {
1043 // We're still returning a struct, so reinsert the value into
1044 // our new return value at the new index
1045
1046 RetVal = IRB.CreateInsertValue(Agg: RetVal, Val: EV, Idxs: NewRetIdxs[RetI],
1047 Name: "newret");
1048 } else {
1049 // We are now only returning a simple value, so just return the
1050 // extracted value.
1051 RetVal = EV;
1052 }
1053 }
1054 }
1055 // Replace the return instruction with one returning the new return
1056 // value (possibly 0 if we became void).
1057 auto *NewRet =
1058 ReturnInst::Create(C&: F->getContext(), retVal: RetVal, InsertBefore: RI->getIterator());
1059 NewRet->setDebugLoc(RI->getDebugLoc());
1060 RI->eraseFromParent();
1061 }
1062
1063 // Clone metadata from the old function, including debug info descriptor.
1064 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
1065 F->getAllMetadata(MDs);
1066 for (auto [KindID, Node] : MDs)
1067 NF->addMetadata(KindID, MD&: *Node);
1068
1069 // If either the return value(s) or argument(s) are removed, then probably the
1070 // function does not follow standard calling conventions anymore. Hence, add
1071 // DW_CC_nocall to DISubroutineType to inform debugger that it may not be safe
1072 // to call this function or try to interpret the return value.
1073 if (NFTy != FTy && NF->getSubprogram()) {
1074 DISubprogram *SP = NF->getSubprogram();
1075 auto Temp = SP->getType()->cloneWithCC(CC: llvm::dwarf::DW_CC_nocall);
1076 SP->replaceType(Ty: MDNode::replaceWithPermanent(N: std::move(Temp)));
1077 }
1078
1079 // Now that the old function is dead, delete it.
1080 F->eraseFromParent();
1081
1082 return true;
1083}
1084
1085PreservedAnalyses DeadArgumentEliminationPass::run(Module &M,
1086 ModuleAnalysisManager &) {
1087 bool Changed = false;
1088
1089 // First pass: Do a simple check to see if any functions can have their "..."
1090 // removed. We can do this if they never call va_start. This loop cannot be
1091 // fused with the next loop, because deleting a function invalidates
1092 // information computed while surveying other functions.
1093 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Deleting dead varargs\n");
1094 for (Function &F : llvm::make_early_inc_range(Range&: M))
1095 if (F.getFunctionType()->isVarArg())
1096 Changed |= deleteDeadVarargs(F);
1097
1098 // Second phase: Loop through the module, determining which arguments are
1099 // live. We assume all arguments are dead unless proven otherwise (allowing us
1100 // to determine that dead arguments passed into recursive functions are dead).
1101 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Determining liveness\n");
1102 for (auto &F : M)
1103 surveyFunction(F);
1104
1105 // Now, remove all dead arguments and return values from each function in
1106 // turn. We use make_early_inc_range here because functions will probably get
1107 // removed (i.e. replaced by new ones).
1108 for (Function &F : llvm::make_early_inc_range(Range&: M))
1109 Changed |= removeDeadStuffFromFunction(F: &F);
1110
1111 // Finally, look for any unused parameters in functions with non-local
1112 // linkage and replace the passed in parameters with poison.
1113 for (auto &F : M)
1114 Changed |= removeDeadArgumentsFromCallers(F);
1115
1116 if (!Changed)
1117 return PreservedAnalyses::all();
1118 return PreservedAnalyses::none();
1119}
1120