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