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