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