DeadArgumentElimination.cpp revision 296417
1193323Sed//===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This pass deletes dead arguments from internal functions.  Dead argument
11193323Sed// elimination removes arguments which are directly dead, as well as arguments
12193323Sed// only passed into function calls as dead arguments of other functions.  This
13193323Sed// pass also deletes dead return values in a similar way.
14193323Sed//
15193323Sed// This pass is often useful as a cleanup pass to run after aggressive
16193323Sed// interprocedural passes, which add possibly-dead arguments or return values.
17193323Sed//
18193323Sed//===----------------------------------------------------------------------===//
19193323Sed
20193323Sed#include "llvm/Transforms/IPO.h"
21249423Sdim#include "llvm/ADT/DenseMap.h"
22249423Sdim#include "llvm/ADT/SmallVector.h"
23249423Sdim#include "llvm/ADT/Statistic.h"
24249423Sdim#include "llvm/ADT/StringExtras.h"
25276479Sdim#include "llvm/IR/CallSite.h"
26249423Sdim#include "llvm/IR/CallingConv.h"
27249423Sdim#include "llvm/IR/Constant.h"
28276479Sdim#include "llvm/IR/DIBuilder.h"
29276479Sdim#include "llvm/IR/DebugInfo.h"
30249423Sdim#include "llvm/IR/DerivedTypes.h"
31249423Sdim#include "llvm/IR/Instructions.h"
32249423Sdim#include "llvm/IR/IntrinsicInst.h"
33249423Sdim#include "llvm/IR/LLVMContext.h"
34249423Sdim#include "llvm/IR/Module.h"
35193323Sed#include "llvm/Pass.h"
36193323Sed#include "llvm/Support/Debug.h"
37198090Srdivacky#include "llvm/Support/raw_ostream.h"
38296417Sdim#include "llvm/Transforms/Utils/BasicBlockUtils.h"
39193323Sed#include <map>
40193323Sed#include <set>
41276479Sdim#include <tuple>
42193323Sedusing namespace llvm;
43193323Sed
44276479Sdim#define DEBUG_TYPE "deadargelim"
45276479Sdim
46193323SedSTATISTIC(NumArgumentsEliminated, "Number of unread args removed");
47193323SedSTATISTIC(NumRetValsEliminated  , "Number of unused return values removed");
48218893SdimSTATISTIC(NumArgumentsReplacedWithUndef,
49218893Sdim          "Number of unread args replaced with undef");
50193323Sednamespace {
51193323Sed  /// DAE - The dead argument elimination pass.
52193323Sed  ///
53198892Srdivacky  class DAE : public ModulePass {
54193323Sed  public:
55193323Sed
56193323Sed    /// Struct that represents (part of) either a return value or a function
57193323Sed    /// argument.  Used so that arguments and return values can be used
58221345Sdim    /// interchangeably.
59193323Sed    struct RetOrArg {
60206083Srdivacky      RetOrArg(const Function *F, unsigned Idx, bool IsArg) : F(F), Idx(Idx),
61193323Sed               IsArg(IsArg) {}
62193323Sed      const Function *F;
63193323Sed      unsigned Idx;
64193323Sed      bool IsArg;
65193323Sed
66193323Sed      /// Make RetOrArg comparable, so we can put it into a map.
67193323Sed      bool operator<(const RetOrArg &O) const {
68276479Sdim        return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg);
69193323Sed      }
70193323Sed
71193323Sed      /// Make RetOrArg comparable, so we can easily iterate the multimap.
72193323Sed      bool operator==(const RetOrArg &O) const {
73193323Sed        return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
74193323Sed      }
75193323Sed
76193323Sed      std::string getDescription() const {
77288943Sdim        return (Twine(IsArg ? "Argument #" : "Return value #") + utostr(Idx) +
78288943Sdim                " of function " + F->getName()).str();
79193323Sed      }
80193323Sed    };
81193323Sed
82193323Sed    /// Liveness enum - During our initial pass over the program, we determine
83193323Sed    /// that things are either alive or maybe alive. We don't mark anything
84193323Sed    /// explicitly dead (even if we know they are), since anything not alive
85193323Sed    /// with no registered uses (in Uses) will never be marked alive and will
86193323Sed    /// thus become dead in the end.
87193323Sed    enum Liveness { Live, MaybeLive };
88193323Sed
89193323Sed    /// Convenience wrapper
90193323Sed    RetOrArg CreateRet(const Function *F, unsigned Idx) {
91193323Sed      return RetOrArg(F, Idx, false);
92193323Sed    }
93193323Sed    /// Convenience wrapper
94193323Sed    RetOrArg CreateArg(const Function *F, unsigned Idx) {
95193323Sed      return RetOrArg(F, Idx, true);
96193323Sed    }
97193323Sed
98193323Sed    typedef std::multimap<RetOrArg, RetOrArg> UseMap;
99193323Sed    /// This maps a return value or argument to any MaybeLive return values or
100193323Sed    /// arguments it uses. This allows the MaybeLive values to be marked live
101193323Sed    /// when any of its users is marked live.
102193323Sed    /// For example (indices are left out for clarity):
103193323Sed    ///  - Uses[ret F] = ret G
104193323Sed    ///    This means that F calls G, and F returns the value returned by G.
105193323Sed    ///  - Uses[arg F] = ret G
106193323Sed    ///    This means that some function calls G and passes its result as an
107193323Sed    ///    argument to F.
108193323Sed    ///  - Uses[ret F] = arg F
109193323Sed    ///    This means that F returns one of its own arguments.
110193323Sed    ///  - Uses[arg F] = arg G
111193323Sed    ///    This means that G calls F and passes one of its own (G's) arguments
112193323Sed    ///    directly to F.
113193323Sed    UseMap Uses;
114193323Sed
115193323Sed    typedef std::set<RetOrArg> LiveSet;
116193323Sed    typedef std::set<const Function*> LiveFuncSet;
117193323Sed
118193323Sed    /// This set contains all values that have been determined to be live.
119193323Sed    LiveSet LiveValues;
120193323Sed    /// This set contains all values that are cannot be changed in any way.
121193323Sed    LiveFuncSet LiveFunctions;
122193323Sed
123193323Sed    typedef SmallVector<RetOrArg, 5> UseVector;
124193323Sed
125210299Sed  protected:
126210299Sed    // DAH uses this to specify a different ID.
127212904Sdim    explicit DAE(char &ID) : ModulePass(ID) {}
128210299Sed
129193323Sed  public:
130193323Sed    static char ID; // Pass identification, replacement for typeid
131218893Sdim    DAE() : ModulePass(ID) {
132218893Sdim      initializeDAEPass(*PassRegistry::getPassRegistry());
133218893Sdim    }
134210299Sed
135276479Sdim    bool runOnModule(Module &M) override;
136193323Sed
137193323Sed    virtual bool ShouldHackArguments() const { return false; }
138193323Sed
139193323Sed  private:
140193323Sed    Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
141276479Sdim    Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses,
142288943Sdim                       unsigned RetValNum = -1U);
143206083Srdivacky    Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
144193323Sed
145206083Srdivacky    void SurveyFunction(const Function &F);
146193323Sed    void MarkValue(const RetOrArg &RA, Liveness L,
147193323Sed                   const UseVector &MaybeLiveUses);
148193323Sed    void MarkLive(const RetOrArg &RA);
149193323Sed    void MarkLive(const Function &F);
150193323Sed    void PropagateLiveness(const RetOrArg &RA);
151193323Sed    bool RemoveDeadStuffFromFunction(Function *F);
152193323Sed    bool DeleteDeadVarargs(Function &Fn);
153218893Sdim    bool RemoveDeadArgumentsFromCallers(Function &Fn);
154193323Sed  };
155193323Sed}
156193323Sed
157193323Sed
158193323Sedchar DAE::ID = 0;
159218893SdimINITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false)
160193323Sed
161193323Sednamespace {
162193323Sed  /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
163193323Sed  /// deletes arguments to functions which are external.  This is only for use
164193323Sed  /// by bugpoint.
165193323Sed  struct DAH : public DAE {
166193323Sed    static char ID;
167212904Sdim    DAH() : DAE(ID) {}
168210299Sed
169276479Sdim    bool ShouldHackArguments() const override { return true; }
170193323Sed  };
171193323Sed}
172193323Sed
173193323Sedchar DAH::ID = 0;
174212904SdimINITIALIZE_PASS(DAH, "deadarghaX0r",
175212904Sdim                "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)",
176218893Sdim                false, false)
177193323Sed
178193323Sed/// createDeadArgEliminationPass - This pass removes arguments from functions
179193323Sed/// which are not used by the body of the function.
180193323Sed///
181193323SedModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
182193323SedModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
183193323Sed
184193323Sed/// DeleteDeadVarargs - If this is an function that takes a ... list, and if
185193323Sed/// llvm.vastart is never called, the varargs list is dead for the function.
186193323Sedbool DAE::DeleteDeadVarargs(Function &Fn) {
187193323Sed  assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
188193323Sed  if (Fn.isDeclaration() || !Fn.hasLocalLinkage()) return false;
189193323Sed
190193323Sed  // Ensure that the function is only directly called.
191194178Sed  if (Fn.hasAddressTaken())
192194178Sed    return false;
193193323Sed
194296417Sdim  // Don't touch naked functions. The assembly might be using an argument, or
195296417Sdim  // otherwise rely on the frame layout in a way that this analysis will not
196296417Sdim  // see.
197296417Sdim  if (Fn.hasFnAttribute(Attribute::Naked)) {
198296417Sdim    return false;
199296417Sdim  }
200296417Sdim
201193323Sed  // Okay, we know we can transform this function if safe.  Scan its body
202280031Sdim  // looking for calls marked musttail or calls to llvm.vastart.
203193323Sed  for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
204193323Sed    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
205280031Sdim      CallInst *CI = dyn_cast<CallInst>(I);
206280031Sdim      if (!CI)
207280031Sdim        continue;
208280031Sdim      if (CI->isMustTailCall())
209280031Sdim        return false;
210280031Sdim      if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
211193323Sed        if (II->getIntrinsicID() == Intrinsic::vastart)
212193323Sed          return false;
213193323Sed      }
214193323Sed    }
215193323Sed  }
216193323Sed
217193323Sed  // If we get here, there are no calls to llvm.vastart in the function body,
218193323Sed  // remove the "..." and adjust all the calls.
219193323Sed
220193323Sed  // Start by computing a new prototype for the function, which is the same as
221193323Sed  // the old function, but doesn't have isVarArg set.
222226633Sdim  FunctionType *FTy = Fn.getFunctionType();
223206083Srdivacky
224224145Sdim  std::vector<Type*> Params(FTy->param_begin(), FTy->param_end());
225198090Srdivacky  FunctionType *NFTy = FunctionType::get(FTy->getReturnType(),
226198090Srdivacky                                                Params, false);
227193323Sed  unsigned NumArgs = Params.size();
228193323Sed
229193323Sed  // Create the new function body and insert it into the module...
230193323Sed  Function *NF = Function::Create(NFTy, Fn.getLinkage());
231193323Sed  NF->copyAttributesFrom(&Fn);
232296417Sdim  Fn.getParent()->getFunctionList().insert(Fn.getIterator(), NF);
233193323Sed  NF->takeName(&Fn);
234193323Sed
235193323Sed  // Loop over all of the callers of the function, transforming the call sites
236193323Sed  // to pass in a smaller number of arguments into the new function.
237193323Sed  //
238193323Sed  std::vector<Value*> Args;
239276479Sdim  for (Value::user_iterator I = Fn.user_begin(), E = Fn.user_end(); I != E; ) {
240261991Sdim    CallSite CS(*I++);
241261991Sdim    if (!CS)
242261991Sdim      continue;
243193323Sed    Instruction *Call = CS.getInstruction();
244193323Sed
245193323Sed    // Pass all the same arguments.
246212904Sdim    Args.assign(CS.arg_begin(), CS.arg_begin() + NumArgs);
247193323Sed
248193323Sed    // Drop any attributes that were on the vararg arguments.
249249423Sdim    AttributeSet PAL = CS.getAttributes();
250249423Sdim    if (!PAL.isEmpty() && PAL.getSlotIndex(PAL.getNumSlots() - 1) > NumArgs) {
251249423Sdim      SmallVector<AttributeSet, 8> AttributesVec;
252249423Sdim      for (unsigned i = 0; PAL.getSlotIndex(i) <= NumArgs; ++i)
253249423Sdim        AttributesVec.push_back(PAL.getSlotAttributes(i));
254249423Sdim      if (PAL.hasAttributes(AttributeSet::FunctionIndex))
255249423Sdim        AttributesVec.push_back(AttributeSet::get(Fn.getContext(),
256249423Sdim                                                  PAL.getFnAttributes()));
257249423Sdim      PAL = AttributeSet::get(Fn.getContext(), AttributesVec);
258193323Sed    }
259193323Sed
260193323Sed    Instruction *New;
261193323Sed    if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
262193323Sed      New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
263224145Sdim                               Args, "", Call);
264193323Sed      cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
265193323Sed      cast<InvokeInst>(New)->setAttributes(PAL);
266193323Sed    } else {
267224145Sdim      New = CallInst::Create(NF, Args, "", Call);
268193323Sed      cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
269193323Sed      cast<CallInst>(New)->setAttributes(PAL);
270193323Sed      if (cast<CallInst>(Call)->isTailCall())
271193323Sed        cast<CallInst>(New)->setTailCall();
272193323Sed    }
273212904Sdim    New->setDebugLoc(Call->getDebugLoc());
274207618Srdivacky
275193323Sed    Args.clear();
276193323Sed
277193323Sed    if (!Call->use_empty())
278193323Sed      Call->replaceAllUsesWith(New);
279193323Sed
280193323Sed    New->takeName(Call);
281193323Sed
282193323Sed    // Finally, remove the old call from the program, reducing the use-count of
283193323Sed    // F.
284193323Sed    Call->eraseFromParent();
285193323Sed  }
286193323Sed
287193323Sed  // Since we have now created the new function, splice the body of the old
288193323Sed  // function right into the new function, leaving the old rotting hulk of the
289193323Sed  // function empty.
290193323Sed  NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
291193323Sed
292221345Sdim  // Loop over the argument list, transferring uses of the old arguments over to
293221345Sdim  // the new arguments, also transferring over the names as well.  While we're at
294193323Sed  // it, remove the dead arguments from the DeadArguments list.
295193323Sed  //
296193323Sed  for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
297193323Sed       I2 = NF->arg_begin(); I != E; ++I, ++I2) {
298193323Sed    // Move the name and users over to the new version.
299296417Sdim    I->replaceAllUsesWith(&*I2);
300296417Sdim    I2->takeName(&*I);
301193323Sed  }
302193323Sed
303243830Sdim  // Patch the pointer to LLVM function in debug info descriptor.
304296417Sdim  NF->setSubprogram(Fn.getSubprogram());
305243830Sdim
306261991Sdim  // Fix up any BlockAddresses that refer to the function.
307261991Sdim  Fn.replaceAllUsesWith(ConstantExpr::getBitCast(NF, Fn.getType()));
308261991Sdim  // Delete the bitcast that we just created, so that NF does not
309261991Sdim  // appear to be address-taken.
310261991Sdim  NF->removeDeadConstantUsers();
311193323Sed  // Finally, nuke the old function.
312193323Sed  Fn.eraseFromParent();
313193323Sed  return true;
314193323Sed}
315193323Sed
316218893Sdim/// RemoveDeadArgumentsFromCallers - Checks if the given function has any
317218893Sdim/// arguments that are unused, and changes the caller parameters to be undefined
318218893Sdim/// instead.
319218893Sdimbool DAE::RemoveDeadArgumentsFromCallers(Function &Fn)
320218893Sdim{
321288943Sdim  // We cannot change the arguments if this TU does not define the function or
322288943Sdim  // if the linker may choose a function body from another TU, even if the
323288943Sdim  // nominal linkage indicates that other copies of the function have the same
324288943Sdim  // semantics. In the below example, the dead load from %p may not have been
325288943Sdim  // eliminated from the linker-chosen copy of f, so replacing %p with undef
326288943Sdim  // in callers may introduce undefined behavior.
327288943Sdim  //
328288943Sdim  // define linkonce_odr void @f(i32* %p) {
329288943Sdim  //   %v = load i32 %p
330288943Sdim  //   ret void
331288943Sdim  // }
332288943Sdim  if (!Fn.isStrongDefinitionForLinker())
333218893Sdim    return false;
334218893Sdim
335261991Sdim  // Functions with local linkage should already have been handled, except the
336261991Sdim  // fragile (variadic) ones which we can improve here.
337261991Sdim  if (Fn.hasLocalLinkage() && !Fn.getFunctionType()->isVarArg())
338218893Sdim    return false;
339218893Sdim
340296417Sdim  // Don't touch naked functions. The assembly might be using an argument, or
341296417Sdim  // otherwise rely on the frame layout in a way that this analysis will not
342296417Sdim  // see.
343296417Sdim  if (Fn.hasFnAttribute(Attribute::Naked))
344296417Sdim    return false;
345296417Sdim
346218893Sdim  if (Fn.use_empty())
347218893Sdim    return false;
348218893Sdim
349249423Sdim  SmallVector<unsigned, 8> UnusedArgs;
350296417Sdim  for (Argument &Arg : Fn.args()) {
351296417Sdim    if (Arg.use_empty() && !Arg.hasByValOrInAllocaAttr())
352296417Sdim      UnusedArgs.push_back(Arg.getArgNo());
353218893Sdim  }
354218893Sdim
355218893Sdim  if (UnusedArgs.empty())
356218893Sdim    return false;
357218893Sdim
358218893Sdim  bool Changed = false;
359218893Sdim
360276479Sdim  for (Use &U : Fn.uses()) {
361276479Sdim    CallSite CS(U.getUser());
362276479Sdim    if (!CS || !CS.isCallee(&U))
363218893Sdim      continue;
364218893Sdim
365218893Sdim    // Now go through all unused args and replace them with "undef".
366218893Sdim    for (unsigned I = 0, E = UnusedArgs.size(); I != E; ++I) {
367218893Sdim      unsigned ArgNo = UnusedArgs[I];
368218893Sdim
369218893Sdim      Value *Arg = CS.getArgument(ArgNo);
370218893Sdim      CS.setArgument(ArgNo, UndefValue::get(Arg->getType()));
371218893Sdim      ++NumArgumentsReplacedWithUndef;
372218893Sdim      Changed = true;
373218893Sdim    }
374218893Sdim  }
375218893Sdim
376218893Sdim  return Changed;
377218893Sdim}
378218893Sdim
379193323Sed/// Convenience function that returns the number of return values. It returns 0
380193323Sed/// for void functions and 1 for functions not returning a struct. It returns
381193323Sed/// the number of struct elements for functions returning a struct.
382193323Sedstatic unsigned NumRetVals(const Function *F) {
383288943Sdim  Type *RetTy = F->getReturnType();
384288943Sdim  if (RetTy->isVoidTy())
385193323Sed    return 0;
386288943Sdim  else if (StructType *STy = dyn_cast<StructType>(RetTy))
387193323Sed    return STy->getNumElements();
388288943Sdim  else if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy))
389288943Sdim    return ATy->getNumElements();
390193323Sed  else
391193323Sed    return 1;
392193323Sed}
393193323Sed
394288943Sdim/// Returns the sub-type a function will return at a given Idx. Should
395288943Sdim/// correspond to the result type of an ExtractValue instruction executed with
396288943Sdim/// just that one Idx (i.e. only top-level structure is considered).
397288943Sdimstatic Type *getRetComponentType(const Function *F, unsigned Idx) {
398288943Sdim  Type *RetTy = F->getReturnType();
399288943Sdim  assert(!RetTy->isVoidTy() && "void type has no subtype");
400288943Sdim
401288943Sdim  if (StructType *STy = dyn_cast<StructType>(RetTy))
402288943Sdim    return STy->getElementType(Idx);
403288943Sdim  else if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy))
404288943Sdim    return ATy->getElementType();
405288943Sdim  else
406288943Sdim    return RetTy;
407288943Sdim}
408288943Sdim
409193323Sed/// MarkIfNotLive - This checks Use for liveness in LiveValues. If Use is not
410193323Sed/// live, it adds Use to the MaybeLiveUses argument. Returns the determined
411193323Sed/// liveness of Use.
412193323SedDAE::Liveness DAE::MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses) {
413193323Sed  // We're live if our use or its Function is already marked as live.
414193323Sed  if (LiveFunctions.count(Use.F) || LiveValues.count(Use))
415193323Sed    return Live;
416193323Sed
417193323Sed  // We're maybe live otherwise, but remember that we must become live if
418193323Sed  // Use becomes live.
419193323Sed  MaybeLiveUses.push_back(Use);
420193323Sed  return MaybeLive;
421193323Sed}
422193323Sed
423193323Sed
424193323Sed/// SurveyUse - This looks at a single use of an argument or return value
425193323Sed/// and determines if it should be alive or not. Adds this use to MaybeLiveUses
426206083Srdivacky/// if it causes the used value to become MaybeLive.
427193323Sed///
428193323Sed/// RetValNum is the return value number to use when this use is used in a
429193323Sed/// return instruction. This is used in the recursion, you should always leave
430193323Sed/// it at 0.
431276479SdimDAE::Liveness DAE::SurveyUse(const Use *U,
432206083Srdivacky                             UseVector &MaybeLiveUses, unsigned RetValNum) {
433276479Sdim    const User *V = U->getUser();
434206083Srdivacky    if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) {
435193323Sed      // The value is returned from a function. It's only live when the
436193323Sed      // function's return value is live. We use RetValNum here, for the case
437193323Sed      // that U is really a use of an insertvalue instruction that uses the
438221345Sdim      // original Use.
439288943Sdim      const Function *F = RI->getParent()->getParent();
440288943Sdim      if (RetValNum != -1U) {
441288943Sdim        RetOrArg Use = CreateRet(F, RetValNum);
442288943Sdim        // We might be live, depending on the liveness of Use.
443288943Sdim        return MarkIfNotLive(Use, MaybeLiveUses);
444288943Sdim      } else {
445288943Sdim        DAE::Liveness Result = MaybeLive;
446288943Sdim        for (unsigned i = 0; i < NumRetVals(F); ++i) {
447288943Sdim          RetOrArg Use = CreateRet(F, i);
448288943Sdim          // We might be live, depending on the liveness of Use. If any
449288943Sdim          // sub-value is live, then the entire value is considered live. This
450288943Sdim          // is a conservative choice, and better tracking is possible.
451288943Sdim          DAE::Liveness SubResult = MarkIfNotLive(Use, MaybeLiveUses);
452288943Sdim          if (Result != Live)
453288943Sdim            Result = SubResult;
454288943Sdim        }
455288943Sdim        return Result;
456288943Sdim      }
457193323Sed    }
458206083Srdivacky    if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {
459276479Sdim      if (U->getOperandNo() != InsertValueInst::getAggregateOperandIndex()
460193323Sed          && IV->hasIndices())
461193323Sed        // The use we are examining is inserted into an aggregate. Our liveness
462193323Sed        // depends on all uses of that aggregate, but if it is used as a return
463193323Sed        // value, only index at which we were inserted counts.
464193323Sed        RetValNum = *IV->idx_begin();
465193323Sed
466193323Sed      // Note that if we are used as the aggregate operand to the insertvalue,
467193323Sed      // we don't change RetValNum, but do survey all our uses.
468193323Sed
469193323Sed      Liveness Result = MaybeLive;
470276479Sdim      for (const Use &UU : IV->uses()) {
471276479Sdim        Result = SurveyUse(&UU, MaybeLiveUses, RetValNum);
472193323Sed        if (Result == Live)
473193323Sed          break;
474193323Sed      }
475193323Sed      return Result;
476193323Sed    }
477206083Srdivacky
478288943Sdim    if (auto CS = ImmutableCallSite(V)) {
479206083Srdivacky      const Function *F = CS.getCalledFunction();
480193323Sed      if (F) {
481193323Sed        // Used in a direct call.
482206083Srdivacky
483296417Sdim        // The function argument is live if it is used as a bundle operand.
484296417Sdim        if (CS.isBundleOperand(U))
485296417Sdim          return Live;
486296417Sdim
487193323Sed        // Find the argument number. We know for sure that this use is an
488193323Sed        // argument, since if it was the function argument this would be an
489193323Sed        // indirect call and the we know can't be looking at a value of the
490193323Sed        // label type (for the invoke instruction).
491206083Srdivacky        unsigned ArgNo = CS.getArgumentNo(U);
492193323Sed
493193323Sed        if (ArgNo >= F->getFunctionType()->getNumParams())
494193323Sed          // The value is passed in through a vararg! Must be live.
495193323Sed          return Live;
496193323Sed
497206083Srdivacky        assert(CS.getArgument(ArgNo)
498276479Sdim               == CS->getOperand(U->getOperandNo())
499193323Sed               && "Argument is not where we expected it");
500193323Sed
501193323Sed        // Value passed to a normal call. It's only live when the corresponding
502193323Sed        // argument to the called function turns out live.
503193323Sed        RetOrArg Use = CreateArg(F, ArgNo);
504193323Sed        return MarkIfNotLive(Use, MaybeLiveUses);
505193323Sed      }
506193323Sed    }
507193323Sed    // Used in any other way? Value must be live.
508193323Sed    return Live;
509193323Sed}
510193323Sed
511193323Sed/// SurveyUses - This looks at all the uses of the given value
512193323Sed/// Returns the Liveness deduced from the uses of this value.
513193323Sed///
514193323Sed/// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If
515193323Sed/// the result is Live, MaybeLiveUses might be modified but its content should
516193323Sed/// be ignored (since it might not be complete).
517206083SrdivackyDAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) {
518193323Sed  // Assume it's dead (which will only hold if there are no uses at all..).
519193323Sed  Liveness Result = MaybeLive;
520193323Sed  // Check each use.
521276479Sdim  for (const Use &U : V->uses()) {
522276479Sdim    Result = SurveyUse(&U, MaybeLiveUses);
523193323Sed    if (Result == Live)
524193323Sed      break;
525193323Sed  }
526193323Sed  return Result;
527193323Sed}
528193323Sed
529193323Sed// SurveyFunction - This performs the initial survey of the specified function,
530193323Sed// checking out whether or not it uses any of its incoming arguments or whether
531193323Sed// any callers use the return value.  This fills in the LiveValues set and Uses
532193323Sed// map.
533193323Sed//
534193323Sed// We consider arguments of non-internal functions to be intrinsically alive as
535193323Sed// well as arguments to functions which have their "address taken".
536193323Sed//
537206083Srdivackyvoid DAE::SurveyFunction(const Function &F) {
538276479Sdim  // Functions with inalloca parameters are expecting args in a particular
539276479Sdim  // register and memory layout.
540276479Sdim  if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca)) {
541276479Sdim    MarkLive(F);
542276479Sdim    return;
543276479Sdim  }
544276479Sdim
545296417Sdim  // Don't touch naked functions. The assembly might be using an argument, or
546296417Sdim  // otherwise rely on the frame layout in a way that this analysis will not
547296417Sdim  // see.
548296417Sdim  if (F.hasFnAttribute(Attribute::Naked)) {
549296417Sdim    MarkLive(F);
550296417Sdim    return;
551296417Sdim  }
552296417Sdim
553193323Sed  unsigned RetCount = NumRetVals(&F);
554193323Sed  // Assume all return values are dead
555193323Sed  typedef SmallVector<Liveness, 5> RetVals;
556193323Sed  RetVals RetValLiveness(RetCount, MaybeLive);
557193323Sed
558193323Sed  typedef SmallVector<UseVector, 5> RetUses;
559193323Sed  // These vectors map each return value to the uses that make it MaybeLive, so
560193323Sed  // we can add those to the Uses map if the return value really turns out to be
561193323Sed  // MaybeLive. Initialized to a list of RetCount empty lists.
562193323Sed  RetUses MaybeLiveRetUses(RetCount);
563193323Sed
564206083Srdivacky  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
565206083Srdivacky    if (const ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
566193323Sed      if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType()
567193323Sed          != F.getFunctionType()->getReturnType()) {
568193323Sed        // We don't support old style multiple return values.
569193323Sed        MarkLive(F);
570193323Sed        return;
571193323Sed      }
572193323Sed
573193323Sed  if (!F.hasLocalLinkage() && (!ShouldHackArguments() || F.isIntrinsic())) {
574193323Sed    MarkLive(F);
575193323Sed    return;
576193323Sed  }
577193323Sed
578202375Srdivacky  DEBUG(dbgs() << "DAE - Inspecting callers for fn: " << F.getName() << "\n");
579193323Sed  // Keep track of the number of live retvals, so we can skip checks once all
580193323Sed  // of them turn out to be live.
581193323Sed  unsigned NumLiveRetVals = 0;
582193323Sed  // Loop all uses of the function.
583276479Sdim  for (const Use &U : F.uses()) {
584193323Sed    // If the function is PASSED IN as an argument, its address has been
585193323Sed    // taken.
586276479Sdim    ImmutableCallSite CS(U.getUser());
587276479Sdim    if (!CS || !CS.isCallee(&U)) {
588193323Sed      MarkLive(F);
589193323Sed      return;
590193323Sed    }
591193323Sed
592193323Sed    // If this use is anything other than a call site, the function is alive.
593206083Srdivacky    const Instruction *TheCall = CS.getInstruction();
594193323Sed    if (!TheCall) {   // Not a direct call site?
595193323Sed      MarkLive(F);
596193323Sed      return;
597193323Sed    }
598193323Sed
599193323Sed    // If we end up here, we are looking at a direct call to our function.
600193323Sed
601193323Sed    // Now, check how our return value(s) is/are used in this caller. Don't
602193323Sed    // bother checking return values if all of them are live already.
603288943Sdim    if (NumLiveRetVals == RetCount)
604288943Sdim      continue;
605288943Sdim
606288943Sdim    // Check all uses of the return value.
607288943Sdim    for (const Use &U : TheCall->uses()) {
608288943Sdim      if (ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(U.getUser())) {
609288943Sdim        // This use uses a part of our return value, survey the uses of
610288943Sdim        // that part and store the results for this index only.
611288943Sdim        unsigned Idx = *Ext->idx_begin();
612288943Sdim        if (RetValLiveness[Idx] != Live) {
613288943Sdim          RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]);
614288943Sdim          if (RetValLiveness[Idx] == Live)
615288943Sdim            NumLiveRetVals++;
616193323Sed        }
617193323Sed      } else {
618288943Sdim        // Used by something else than extractvalue. Survey, but assume that the
619288943Sdim        // result applies to all sub-values.
620288943Sdim        UseVector MaybeLiveAggregateUses;
621288943Sdim        if (SurveyUse(&U, MaybeLiveAggregateUses) == Live) {
622193323Sed          NumLiveRetVals = RetCount;
623288943Sdim          RetValLiveness.assign(RetCount, Live);
624288943Sdim          break;
625288943Sdim        } else {
626288943Sdim          for (unsigned i = 0; i != RetCount; ++i) {
627288943Sdim            if (RetValLiveness[i] != Live)
628288943Sdim              MaybeLiveRetUses[i].append(MaybeLiveAggregateUses.begin(),
629288943Sdim                                         MaybeLiveAggregateUses.end());
630288943Sdim          }
631288943Sdim        }
632193323Sed      }
633193323Sed    }
634193323Sed  }
635193323Sed
636193323Sed  // Now we've inspected all callers, record the liveness of our return values.
637193323Sed  for (unsigned i = 0; i != RetCount; ++i)
638193323Sed    MarkValue(CreateRet(&F, i), RetValLiveness[i], MaybeLiveRetUses[i]);
639193323Sed
640202375Srdivacky  DEBUG(dbgs() << "DAE - Inspecting args for fn: " << F.getName() << "\n");
641193323Sed
642193323Sed  // Now, check all of our arguments.
643193323Sed  unsigned i = 0;
644193323Sed  UseVector MaybeLiveArgUses;
645206083Srdivacky  for (Function::const_arg_iterator AI = F.arg_begin(),
646193323Sed       E = F.arg_end(); AI != E; ++AI, ++i) {
647261991Sdim    Liveness Result;
648261991Sdim    if (F.getFunctionType()->isVarArg()) {
649261991Sdim      // Variadic functions will already have a va_arg function expanded inside
650261991Sdim      // them, making them potentially very sensitive to ABI changes resulting
651261991Sdim      // from removing arguments entirely, so don't. For example AArch64 handles
652261991Sdim      // register and stack HFAs very differently, and this is reflected in the
653261991Sdim      // IR which has already been generated.
654261991Sdim      Result = Live;
655261991Sdim    } else {
656261991Sdim      // See what the effect of this use is (recording any uses that cause
657261991Sdim      // MaybeLive in MaybeLiveArgUses).
658296417Sdim      Result = SurveyUses(&*AI, MaybeLiveArgUses);
659261991Sdim    }
660261991Sdim
661193323Sed    // Mark the result.
662193323Sed    MarkValue(CreateArg(&F, i), Result, MaybeLiveArgUses);
663193323Sed    // Clear the vector again for the next iteration.
664193323Sed    MaybeLiveArgUses.clear();
665193323Sed  }
666193323Sed}
667193323Sed
668193323Sed/// MarkValue - This function marks the liveness of RA depending on L. If L is
669193323Sed/// MaybeLive, it also takes all uses in MaybeLiveUses and records them in Uses,
670193323Sed/// such that RA will be marked live if any use in MaybeLiveUses gets marked
671193323Sed/// live later on.
672193323Sedvoid DAE::MarkValue(const RetOrArg &RA, Liveness L,
673193323Sed                    const UseVector &MaybeLiveUses) {
674193323Sed  switch (L) {
675193323Sed    case Live: MarkLive(RA); break;
676193323Sed    case MaybeLive:
677193323Sed    {
678193323Sed      // Note any uses of this value, so this return value can be
679193323Sed      // marked live whenever one of the uses becomes live.
680193323Sed      for (UseVector::const_iterator UI = MaybeLiveUses.begin(),
681193323Sed           UE = MaybeLiveUses.end(); UI != UE; ++UI)
682193323Sed        Uses.insert(std::make_pair(*UI, RA));
683193323Sed      break;
684193323Sed    }
685193323Sed  }
686193323Sed}
687193323Sed
688193323Sed/// MarkLive - Mark the given Function as alive, meaning that it cannot be
689193323Sed/// changed in any way. Additionally,
690193323Sed/// mark any values that are used as this function's parameters or by its return
691193323Sed/// values (according to Uses) live as well.
692193323Sedvoid DAE::MarkLive(const Function &F) {
693202375Srdivacky  DEBUG(dbgs() << "DAE - Intrinsically live fn: " << F.getName() << "\n");
694208599Srdivacky  // Mark the function as live.
695208599Srdivacky  LiveFunctions.insert(&F);
696208599Srdivacky  // Mark all arguments as live.
697208599Srdivacky  for (unsigned i = 0, e = F.arg_size(); i != e; ++i)
698208599Srdivacky    PropagateLiveness(CreateArg(&F, i));
699208599Srdivacky  // Mark all return values as live.
700208599Srdivacky  for (unsigned i = 0, e = NumRetVals(&F); i != e; ++i)
701208599Srdivacky    PropagateLiveness(CreateRet(&F, i));
702193323Sed}
703193323Sed
704193323Sed/// MarkLive - Mark the given return value or argument as live. Additionally,
705193323Sed/// mark any values that are used by this value (according to Uses) live as
706193323Sed/// well.
707193323Sedvoid DAE::MarkLive(const RetOrArg &RA) {
708193323Sed  if (LiveFunctions.count(RA.F))
709193323Sed    return; // Function was already marked Live.
710193323Sed
711193323Sed  if (!LiveValues.insert(RA).second)
712193323Sed    return; // We were already marked Live.
713193323Sed
714202375Srdivacky  DEBUG(dbgs() << "DAE - Marking " << RA.getDescription() << " live\n");
715193323Sed  PropagateLiveness(RA);
716193323Sed}
717193323Sed
718193323Sed/// PropagateLiveness - Given that RA is a live value, propagate it's liveness
719193323Sed/// to any other values it uses (according to Uses).
720193323Sedvoid DAE::PropagateLiveness(const RetOrArg &RA) {
721193323Sed  // We don't use upper_bound (or equal_range) here, because our recursive call
722193323Sed  // to ourselves is likely to cause the upper_bound (which is the first value
723193323Sed  // not belonging to RA) to become erased and the iterator invalidated.
724193323Sed  UseMap::iterator Begin = Uses.lower_bound(RA);
725193323Sed  UseMap::iterator E = Uses.end();
726193323Sed  UseMap::iterator I;
727193323Sed  for (I = Begin; I != E && I->first == RA; ++I)
728193323Sed    MarkLive(I->second);
729193323Sed
730193323Sed  // Erase RA from the Uses map (from the lower bound to wherever we ended up
731193323Sed  // after the loop).
732193323Sed  Uses.erase(Begin, I);
733193323Sed}
734193323Sed
735193323Sed// RemoveDeadStuffFromFunction - Remove any arguments and return values from F
736193323Sed// that are not in LiveValues. Transform the function and all of the callees of
737193323Sed// the function to not have these arguments and return values.
738193323Sed//
739193323Sedbool DAE::RemoveDeadStuffFromFunction(Function *F) {
740193323Sed  // Don't modify fully live functions
741193323Sed  if (LiveFunctions.count(F))
742193323Sed    return false;
743193323Sed
744193323Sed  // Start by computing a new prototype for the function, which is the same as
745193323Sed  // the old function, but has fewer arguments and a different return type.
746226633Sdim  FunctionType *FTy = F->getFunctionType();
747224145Sdim  std::vector<Type*> Params;
748193323Sed
749261991Sdim  // Keep track of if we have a live 'returned' argument
750261991Sdim  bool HasLiveReturnedArg = false;
751261991Sdim
752193323Sed  // Set up to build a new list of parameter attributes.
753249423Sdim  SmallVector<AttributeSet, 8> AttributesVec;
754249423Sdim  const AttributeSet &PAL = F->getAttributes();
755193323Sed
756261991Sdim  // Remember which arguments are still alive.
757261991Sdim  SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
758261991Sdim  // Construct the new parameter list from non-dead arguments. Also construct
759261991Sdim  // a new set of parameter attributes to correspond. Skip the first parameter
760261991Sdim  // attribute, since that belongs to the return value.
761261991Sdim  unsigned i = 0;
762261991Sdim  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
763261991Sdim       I != E; ++I, ++i) {
764261991Sdim    RetOrArg Arg = CreateArg(F, i);
765261991Sdim    if (LiveValues.erase(Arg)) {
766261991Sdim      Params.push_back(I->getType());
767261991Sdim      ArgAlive[i] = true;
768261991Sdim
769261991Sdim      // Get the original parameter attributes (skipping the first one, that is
770261991Sdim      // for the return value.
771261991Sdim      if (PAL.hasAttributes(i + 1)) {
772261991Sdim        AttrBuilder B(PAL, i + 1);
773261991Sdim        if (B.contains(Attribute::Returned))
774261991Sdim          HasLiveReturnedArg = true;
775261991Sdim        AttributesVec.
776261991Sdim          push_back(AttributeSet::get(F->getContext(), Params.size(), B));
777261991Sdim      }
778261991Sdim    } else {
779261991Sdim      ++NumArgumentsEliminated;
780261991Sdim      DEBUG(dbgs() << "DAE - Removing argument " << i << " (" << I->getName()
781261991Sdim            << ") from " << F->getName() << "\n");
782261991Sdim    }
783261991Sdim  }
784261991Sdim
785193323Sed  // Find out the new return value.
786224145Sdim  Type *RetTy = FTy->getReturnType();
787276479Sdim  Type *NRetTy = nullptr;
788193323Sed  unsigned RetCount = NumRetVals(F);
789206083Srdivacky
790193323Sed  // -1 means unused, other numbers are the new index
791193323Sed  SmallVector<int, 5> NewRetIdxs(RetCount, -1);
792224145Sdim  std::vector<Type*> RetTypes;
793261991Sdim
794261991Sdim  // If there is a function with a live 'returned' argument but a dead return
795261991Sdim  // value, then there are two possible actions:
796261991Sdim  // 1) Eliminate the return value and take off the 'returned' attribute on the
797261991Sdim  //    argument.
798261991Sdim  // 2) Retain the 'returned' attribute and treat the return value (but not the
799261991Sdim  //    entire function) as live so that it is not eliminated.
800261991Sdim  //
801261991Sdim  // It's not clear in the general case which option is more profitable because,
802261991Sdim  // even in the absence of explicit uses of the return value, code generation
803261991Sdim  // is free to use the 'returned' attribute to do things like eliding
804261991Sdim  // save/restores of registers across calls. Whether or not this happens is
805261991Sdim  // target and ABI-specific as well as depending on the amount of register
806261991Sdim  // pressure, so there's no good way for an IR-level pass to figure this out.
807261991Sdim  //
808261991Sdim  // Fortunately, the only places where 'returned' is currently generated by
809261991Sdim  // the FE are places where 'returned' is basically free and almost always a
810261991Sdim  // performance win, so the second option can just be used always for now.
811261991Sdim  //
812261991Sdim  // This should be revisited if 'returned' is ever applied more liberally.
813261991Sdim  if (RetTy->isVoidTy() || HasLiveReturnedArg) {
814206083Srdivacky    NRetTy = RetTy;
815193323Sed  } else {
816288943Sdim    // Look at each of the original return values individually.
817288943Sdim    for (unsigned i = 0; i != RetCount; ++i) {
818288943Sdim      RetOrArg Ret = CreateRet(F, i);
819288943Sdim      if (LiveValues.erase(Ret)) {
820288943Sdim        RetTypes.push_back(getRetComponentType(F, i));
821288943Sdim        NewRetIdxs[i] = RetTypes.size() - 1;
822193323Sed      } else {
823193323Sed        ++NumRetValsEliminated;
824288943Sdim        DEBUG(dbgs() << "DAE - Removing return value " << i << " from "
825288943Sdim              << F->getName() << "\n");
826193323Sed      }
827288943Sdim    }
828288943Sdim    if (RetTypes.size() > 1) {
829288943Sdim      // More than one return type? Reduce it down to size.
830288943Sdim      if (StructType *STy = dyn_cast<StructType>(RetTy)) {
831288943Sdim        // Make the new struct packed if we used to return a packed struct
832288943Sdim        // already.
833288943Sdim        NRetTy = StructType::get(STy->getContext(), RetTypes, STy->isPacked());
834288943Sdim      } else {
835288943Sdim        assert(isa<ArrayType>(RetTy) && "unexpected multi-value return");
836288943Sdim        NRetTy = ArrayType::get(RetTypes[0], RetTypes.size());
837288943Sdim      }
838288943Sdim    } else if (RetTypes.size() == 1)
839193323Sed      // One return type? Just a simple value then, but only if we didn't use to
840193323Sed      // return a struct with that simple value before.
841193323Sed      NRetTy = RetTypes.front();
842193323Sed    else if (RetTypes.size() == 0)
843193323Sed      // No return types? Make it void, but only if we didn't use to return {}.
844198090Srdivacky      NRetTy = Type::getVoidTy(F->getContext());
845193323Sed  }
846193323Sed
847193323Sed  assert(NRetTy && "No new return type found?");
848193323Sed
849249423Sdim  // The existing function return attributes.
850249423Sdim  AttributeSet RAttrs = PAL.getRetAttributes();
851249423Sdim
852193323Sed  // Remove any incompatible attributes, but only if we removed all return
853193323Sed  // values. Otherwise, ensure that we don't have any conflicting attributes
854193323Sed  // here. Currently, this should not be possible, but special handling might be
855193323Sed  // required when new return value attributes are added.
856206083Srdivacky  if (NRetTy->isVoidTy())
857288943Sdim    RAttrs = RAttrs.removeAttributes(NRetTy->getContext(),
858288943Sdim                                     AttributeSet::ReturnIndex,
859288943Sdim                                     AttributeFuncs::typeIncompatible(NRetTy));
860193323Sed  else
861249423Sdim    assert(!AttrBuilder(RAttrs, AttributeSet::ReturnIndex).
862288943Sdim             overlaps(AttributeFuncs::typeIncompatible(NRetTy)) &&
863243830Sdim           "Return attributes no longer compatible?");
864193323Sed
865249423Sdim  if (RAttrs.hasAttributes(AttributeSet::ReturnIndex))
866249423Sdim    AttributesVec.push_back(AttributeSet::get(NRetTy->getContext(), RAttrs));
867193323Sed
868249423Sdim  if (PAL.hasAttributes(AttributeSet::FunctionIndex))
869249423Sdim    AttributesVec.push_back(AttributeSet::get(F->getContext(),
870249423Sdim                                              PAL.getFnAttributes()));
871193323Sed
872193323Sed  // Reconstruct the AttributesList based on the vector we constructed.
873249423Sdim  AttributeSet NewPAL = AttributeSet::get(F->getContext(), AttributesVec);
874193323Sed
875193323Sed  // Create the new function type based on the recomputed parameters.
876206083Srdivacky  FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
877193323Sed
878193323Sed  // No change?
879193323Sed  if (NFTy == FTy)
880193323Sed    return false;
881193323Sed
882193323Sed  // Create the new function body and insert it into the module...
883193323Sed  Function *NF = Function::Create(NFTy, F->getLinkage());
884193323Sed  NF->copyAttributesFrom(F);
885193323Sed  NF->setAttributes(NewPAL);
886193323Sed  // Insert the new function before the old function, so we won't be processing
887193323Sed  // it again.
888296417Sdim  F->getParent()->getFunctionList().insert(F->getIterator(), NF);
889193323Sed  NF->takeName(F);
890193323Sed
891193323Sed  // Loop over all of the callers of the function, transforming the call sites
892193323Sed  // to pass in a smaller number of arguments into the new function.
893193323Sed  //
894193323Sed  std::vector<Value*> Args;
895193323Sed  while (!F->use_empty()) {
896276479Sdim    CallSite CS(F->user_back());
897193323Sed    Instruction *Call = CS.getInstruction();
898193323Sed
899193323Sed    AttributesVec.clear();
900249423Sdim    const AttributeSet &CallPAL = CS.getAttributes();
901193323Sed
902193323Sed    // The call return attributes.
903249423Sdim    AttributeSet RAttrs = CallPAL.getRetAttributes();
904249423Sdim
905193323Sed    // Adjust in case the function was changed to return void.
906288943Sdim    RAttrs = RAttrs.removeAttributes(NRetTy->getContext(),
907288943Sdim                                     AttributeSet::ReturnIndex,
908288943Sdim                        AttributeFuncs::typeIncompatible(NF->getReturnType()));
909249423Sdim    if (RAttrs.hasAttributes(AttributeSet::ReturnIndex))
910249423Sdim      AttributesVec.push_back(AttributeSet::get(NF->getContext(), RAttrs));
911193323Sed
912193323Sed    // Declare these outside of the loops, so we can reuse them for the second
913193323Sed    // loop, which loops the varargs.
914193323Sed    CallSite::arg_iterator I = CS.arg_begin();
915193323Sed    unsigned i = 0;
916193323Sed    // Loop over those operands, corresponding to the normal arguments to the
917193323Sed    // original function, and add those that are still alive.
918193323Sed    for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i)
919193323Sed      if (ArgAlive[i]) {
920193323Sed        Args.push_back(*I);
921193323Sed        // Get original parameter attributes, but skip return attributes.
922249423Sdim        if (CallPAL.hasAttributes(i + 1)) {
923249423Sdim          AttrBuilder B(CallPAL, i + 1);
924261991Sdim          // If the return type has changed, then get rid of 'returned' on the
925261991Sdim          // call site. The alternative is to make all 'returned' attributes on
926261991Sdim          // call sites keep the return value alive just like 'returned'
927261991Sdim          // attributes on function declaration but it's less clearly a win
928261991Sdim          // and this is not an expected case anyway
929261991Sdim          if (NRetTy != RetTy && B.contains(Attribute::Returned))
930261991Sdim            B.removeAttribute(Attribute::Returned);
931249423Sdim          AttributesVec.
932249423Sdim            push_back(AttributeSet::get(F->getContext(), Args.size(), B));
933249423Sdim        }
934193323Sed      }
935193323Sed
936193323Sed    // Push any varargs arguments on the list. Don't forget their attributes.
937193323Sed    for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
938193323Sed      Args.push_back(*I);
939249423Sdim      if (CallPAL.hasAttributes(i + 1)) {
940249423Sdim        AttrBuilder B(CallPAL, i + 1);
941249423Sdim        AttributesVec.
942249423Sdim          push_back(AttributeSet::get(F->getContext(), Args.size(), B));
943249423Sdim      }
944193323Sed    }
945193323Sed
946249423Sdim    if (CallPAL.hasAttributes(AttributeSet::FunctionIndex))
947249423Sdim      AttributesVec.push_back(AttributeSet::get(Call->getContext(),
948249423Sdim                                                CallPAL.getFnAttributes()));
949193323Sed
950193323Sed    // Reconstruct the AttributesList based on the vector we constructed.
951249423Sdim    AttributeSet NewCallPAL = AttributeSet::get(F->getContext(), AttributesVec);
952193323Sed
953193323Sed    Instruction *New;
954193323Sed    if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
955193323Sed      New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
956296417Sdim                               Args, "", Call->getParent());
957193323Sed      cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
958193323Sed      cast<InvokeInst>(New)->setAttributes(NewCallPAL);
959193323Sed    } else {
960224145Sdim      New = CallInst::Create(NF, Args, "", Call);
961193323Sed      cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
962193323Sed      cast<CallInst>(New)->setAttributes(NewCallPAL);
963193323Sed      if (cast<CallInst>(Call)->isTailCall())
964193323Sed        cast<CallInst>(New)->setTailCall();
965193323Sed    }
966212904Sdim    New->setDebugLoc(Call->getDebugLoc());
967207618Srdivacky
968193323Sed    Args.clear();
969193323Sed
970193323Sed    if (!Call->use_empty()) {
971193323Sed      if (New->getType() == Call->getType()) {
972193323Sed        // Return type not changed? Just replace users then.
973193323Sed        Call->replaceAllUsesWith(New);
974193323Sed        New->takeName(Call);
975206083Srdivacky      } else if (New->getType()->isVoidTy()) {
976193323Sed        // Our return value has uses, but they will get removed later on.
977193323Sed        // Replace by null for now.
978218893Sdim        if (!Call->getType()->isX86_MMXTy())
979218893Sdim          Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
980193323Sed      } else {
981288943Sdim        assert((RetTy->isStructTy() || RetTy->isArrayTy()) &&
982193323Sed               "Return type changed, but not into a void. The old return type"
983288943Sdim               " must have been a struct or an array!");
984193323Sed        Instruction *InsertPt = Call;
985193323Sed        if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
986296417Sdim          BasicBlock *NewEdge = SplitEdge(New->getParent(), II->getNormalDest());
987296417Sdim          InsertPt = &*NewEdge->getFirstInsertionPt();
988193323Sed        }
989206083Srdivacky
990288943Sdim        // We used to return a struct or array. Instead of doing smart stuff
991288943Sdim        // with all the uses, we will just rebuild it using extract/insertvalue
992288943Sdim        // chaining and let instcombine clean that up.
993193323Sed        //
994193323Sed        // Start out building up our return value from undef
995198090Srdivacky        Value *RetVal = UndefValue::get(RetTy);
996193323Sed        for (unsigned i = 0; i != RetCount; ++i)
997193323Sed          if (NewRetIdxs[i] != -1) {
998193323Sed            Value *V;
999193323Sed            if (RetTypes.size() > 1)
1000193323Sed              // We are still returning a struct, so extract the value from our
1001193323Sed              // return value
1002193323Sed              V = ExtractValueInst::Create(New, NewRetIdxs[i], "newret",
1003193323Sed                                           InsertPt);
1004193323Sed            else
1005193323Sed              // We are now returning a single element, so just insert that
1006193323Sed              V = New;
1007193323Sed            // Insert the value at the old position
1008193323Sed            RetVal = InsertValueInst::Create(RetVal, V, i, "oldret", InsertPt);
1009193323Sed          }
1010193323Sed        // Now, replace all uses of the old call instruction with the return
1011193323Sed        // struct we built
1012193323Sed        Call->replaceAllUsesWith(RetVal);
1013193323Sed        New->takeName(Call);
1014193323Sed      }
1015193323Sed    }
1016193323Sed
1017193323Sed    // Finally, remove the old call from the program, reducing the use-count of
1018193323Sed    // F.
1019193323Sed    Call->eraseFromParent();
1020193323Sed  }
1021193323Sed
1022193323Sed  // Since we have now created the new function, splice the body of the old
1023193323Sed  // function right into the new function, leaving the old rotting hulk of the
1024193323Sed  // function empty.
1025193323Sed  NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
1026193323Sed
1027221345Sdim  // Loop over the argument list, transferring uses of the old arguments over to
1028221345Sdim  // the new arguments, also transferring over the names as well.
1029193323Sed  i = 0;
1030193323Sed  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
1031193323Sed       I2 = NF->arg_begin(); I != E; ++I, ++i)
1032193323Sed    if (ArgAlive[i]) {
1033193323Sed      // If this is a live argument, move the name and users over to the new
1034193323Sed      // version.
1035296417Sdim      I->replaceAllUsesWith(&*I2);
1036296417Sdim      I2->takeName(&*I);
1037193323Sed      ++I2;
1038193323Sed    } else {
1039193323Sed      // If this argument is dead, replace any uses of it with null constants
1040193323Sed      // (these are guaranteed to become unused later on).
1041218893Sdim      if (!I->getType()->isX86_MMXTy())
1042218893Sdim        I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
1043193323Sed    }
1044193323Sed
1045193323Sed  // If we change the return value of the function we must rewrite any return
1046193323Sed  // instructions.  Check this now.
1047193323Sed  if (F->getReturnType() != NF->getReturnType())
1048193323Sed    for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
1049193323Sed      if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
1050193323Sed        Value *RetVal;
1051193323Sed
1052208599Srdivacky        if (NFTy->getReturnType()->isVoidTy()) {
1053276479Sdim          RetVal = nullptr;
1054193323Sed        } else {
1055288943Sdim          assert(RetTy->isStructTy() || RetTy->isArrayTy());
1056288943Sdim          // The original return value was a struct or array, insert
1057193323Sed          // extractvalue/insertvalue chains to extract only the values we need
1058193323Sed          // to return and insert them into our new result.
1059193323Sed          // This does generate messy code, but we'll let it to instcombine to
1060193323Sed          // clean that up.
1061193323Sed          Value *OldRet = RI->getOperand(0);
1062193323Sed          // Start out building up our return value from undef
1063198090Srdivacky          RetVal = UndefValue::get(NRetTy);
1064193323Sed          for (unsigned i = 0; i != RetCount; ++i)
1065193323Sed            if (NewRetIdxs[i] != -1) {
1066193323Sed              ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i,
1067193323Sed                                                              "oldret", RI);
1068193323Sed              if (RetTypes.size() > 1) {
1069193323Sed                // We're still returning a struct, so reinsert the value into
1070193323Sed                // our new return value at the new index
1071193323Sed
1072193323Sed                RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i],
1073193323Sed                                                 "newret", RI);
1074193323Sed              } else {
1075193323Sed                // We are now only returning a simple value, so just return the
1076193323Sed                // extracted value.
1077193323Sed                RetVal = EV;
1078193323Sed              }
1079193323Sed            }
1080193323Sed        }
1081193323Sed        // Replace the return instruction with one returning the new return
1082193323Sed        // value (possibly 0 if we became void).
1083198090Srdivacky        ReturnInst::Create(F->getContext(), RetVal, RI);
1084193323Sed        BB->getInstList().erase(RI);
1085193323Sed      }
1086193323Sed
1087243830Sdim  // Patch the pointer to LLVM function in debug info descriptor.
1088296417Sdim  NF->setSubprogram(F->getSubprogram());
1089243830Sdim
1090193323Sed  // Now that the old function is dead, delete it.
1091193323Sed  F->eraseFromParent();
1092193323Sed
1093193323Sed  return true;
1094193323Sed}
1095193323Sed
1096193323Sedbool DAE::runOnModule(Module &M) {
1097193323Sed  bool Changed = false;
1098193323Sed
1099193323Sed  // First pass: Do a simple check to see if any functions can have their "..."
1100193323Sed  // removed.  We can do this if they never call va_start.  This loop cannot be
1101193323Sed  // fused with the next loop, because deleting a function invalidates
1102193323Sed  // information computed while surveying other functions.
1103202375Srdivacky  DEBUG(dbgs() << "DAE - Deleting dead varargs\n");
1104193323Sed  for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
1105193323Sed    Function &F = *I++;
1106193323Sed    if (F.getFunctionType()->isVarArg())
1107193323Sed      Changed |= DeleteDeadVarargs(F);
1108193323Sed  }
1109193323Sed
1110193323Sed  // Second phase:loop through the module, determining which arguments are live.
1111193323Sed  // We assume all arguments are dead unless proven otherwise (allowing us to
1112193323Sed  // determine that dead arguments passed into recursive functions are dead).
1113193323Sed  //
1114202375Srdivacky  DEBUG(dbgs() << "DAE - Determining liveness\n");
1115280031Sdim  for (auto &F : M)
1116280031Sdim    SurveyFunction(F);
1117206083Srdivacky
1118193323Sed  // Now, remove all dead arguments and return values from each function in
1119206083Srdivacky  // turn.
1120193323Sed  for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
1121206083Srdivacky    // Increment now, because the function will probably get removed (ie.
1122193323Sed    // replaced by a new one).
1123296417Sdim    Function *F = &*I++;
1124193323Sed    Changed |= RemoveDeadStuffFromFunction(F);
1125193323Sed  }
1126218893Sdim
1127218893Sdim  // Finally, look for any unused parameters in functions with non-local
1128218893Sdim  // linkage and replace the passed in parameters with undef.
1129280031Sdim  for (auto &F : M)
1130218893Sdim    Changed |= RemoveDeadArgumentsFromCallers(F);
1131218893Sdim
1132193323Sed  return Changed;
1133193323Sed}
1134