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