2003-06-18 06:21:05 +08:00
|
|
|
//===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-06-18 06:21:05 +08:00
|
|
|
//
|
|
|
|
// This pass deletes dead arguments from internal functions. Dead argument
|
|
|
|
// elimination removes arguments which are directly dead, as well as arguments
|
2003-10-23 11:48:17 +08:00
|
|
|
// only passed into function calls as dead arguments of other functions. This
|
2008-07-10 18:24:08 +08:00
|
|
|
// pass also deletes dead return values in a similar way.
|
2003-06-18 06:21:05 +08:00
|
|
|
//
|
|
|
|
// This pass is often useful as a cleanup pass to run after aggressive
|
2008-07-10 18:24:08 +08:00
|
|
|
// interprocedural passes, which add possibly-dead arguments or return values.
|
2003-06-18 06:21:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-03-04 19:01:28 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
2014-03-06 08:22:06 +08:00
|
|
|
#include "llvm/IR/DIBuilder.h"
|
2014-03-06 08:46:21 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2006-06-28 05:05:04 +08:00
|
|
|
#include "llvm/Pass.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 08:23:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-03-22 07:51:57 +08:00
|
|
|
#include <map>
|
2003-06-18 06:21:05 +08:00
|
|
|
#include <set>
|
2014-04-30 15:21:01 +08:00
|
|
|
#include <tuple>
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "deadargelim"
|
|
|
|
|
2013-02-02 08:25:26 +08:00
|
|
|
STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
|
|
|
|
STATISTIC(NumRetValsEliminated , "Number of unused return values removed");
|
2011-01-17 05:25:33 +08:00
|
|
|
STATISTIC(NumArgumentsReplacedWithUndef,
|
|
|
|
"Number of unread args replaced with undef");
|
2006-12-20 06:09:18 +08:00
|
|
|
namespace {
|
2003-10-23 11:48:17 +08:00
|
|
|
/// DAE - The dead argument elimination pass.
|
|
|
|
///
|
2009-10-25 14:33:48 +08:00
|
|
|
class DAE : public ModulePass {
|
2008-07-10 18:24:08 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
/// Struct that represents (part of) either a return value or a function
|
|
|
|
/// argument. Used so that arguments and return values can be used
|
2011-04-15 13:18:47 +08:00
|
|
|
/// interchangeably.
|
2008-07-10 18:24:08 +08:00
|
|
|
struct RetOrArg {
|
2010-04-01 15:34:00 +08:00
|
|
|
RetOrArg(const Function *F, unsigned Idx, bool IsArg) : F(F), Idx(Idx),
|
2008-07-10 18:24:08 +08:00
|
|
|
IsArg(IsArg) {}
|
|
|
|
const Function *F;
|
2008-07-15 22:39:36 +08:00
|
|
|
unsigned Idx;
|
2008-07-10 18:24:08 +08:00
|
|
|
bool IsArg;
|
|
|
|
|
|
|
|
/// Make RetOrArg comparable, so we can put it into a map.
|
|
|
|
bool operator<(const RetOrArg &O) const {
|
2014-03-04 03:58:30 +08:00
|
|
|
return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg);
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Make RetOrArg comparable, so we can easily iterate the multimap.
|
|
|
|
bool operator==(const RetOrArg &O) const {
|
|
|
|
return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:56:49 +08:00
|
|
|
std::string getDescription() const {
|
2015-03-28 01:51:30 +08:00
|
|
|
return (Twine(IsArg ? "Argument #" : "Return value #") + utostr(Idx) +
|
|
|
|
" of function " + F->getName()).str();
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
/// Liveness enum - During our initial pass over the program, we determine
|
2008-07-10 18:24:08 +08:00
|
|
|
/// that things are either alive or maybe alive. We don't mark anything
|
|
|
|
/// explicitly dead (even if we know they are), since anything not alive
|
|
|
|
/// with no registered uses (in Uses) will never be marked alive and will
|
|
|
|
/// thus become dead in the end.
|
|
|
|
enum Liveness { Live, MaybeLive };
|
|
|
|
|
|
|
|
/// Convenience wrapper
|
2008-07-15 22:39:36 +08:00
|
|
|
RetOrArg CreateRet(const Function *F, unsigned Idx) {
|
2008-07-10 18:24:08 +08:00
|
|
|
return RetOrArg(F, Idx, false);
|
|
|
|
}
|
|
|
|
/// Convenience wrapper
|
2008-07-15 22:39:36 +08:00
|
|
|
RetOrArg CreateArg(const Function *F, unsigned Idx) {
|
2008-07-10 18:24:08 +08:00
|
|
|
return RetOrArg(F, Idx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::multimap<RetOrArg, RetOrArg> UseMap;
|
|
|
|
/// This maps a return value or argument to any MaybeLive return values or
|
|
|
|
/// arguments it uses. This allows the MaybeLive values to be marked live
|
|
|
|
/// when any of its users is marked live.
|
|
|
|
/// For example (indices are left out for clarity):
|
|
|
|
/// - Uses[ret F] = ret G
|
|
|
|
/// This means that F calls G, and F returns the value returned by G.
|
|
|
|
/// - Uses[arg F] = ret G
|
|
|
|
/// This means that some function calls G and passes its result as an
|
|
|
|
/// argument to F.
|
|
|
|
/// - Uses[ret F] = arg F
|
|
|
|
/// This means that F returns one of its own arguments.
|
|
|
|
/// - Uses[arg F] = arg G
|
|
|
|
/// This means that G calls F and passes one of its own (G's) arguments
|
|
|
|
/// directly to F.
|
|
|
|
UseMap Uses;
|
|
|
|
|
|
|
|
typedef std::set<RetOrArg> LiveSet;
|
2008-07-15 17:11:16 +08:00
|
|
|
typedef std::set<const Function*> LiveFuncSet;
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
/// This set contains all values that have been determined to be live.
|
|
|
|
LiveSet LiveValues;
|
2008-07-15 17:11:16 +08:00
|
|
|
/// This set contains all values that are cannot be changed in any way.
|
|
|
|
LiveFuncSet LiveFunctions;
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
typedef SmallVector<RetOrArg, 5> UseVector;
|
2003-10-23 11:48:17 +08:00
|
|
|
|
2012-10-09 16:13:15 +08:00
|
|
|
// Map each LLVM function to corresponding metadata with debug info. If
|
|
|
|
// the function is replaced with another one, we should patch the pointer
|
|
|
|
// to LLVM function in metadata.
|
|
|
|
// As the code generation for module is finished (and DIBuilder is
|
|
|
|
// finalized) we assume that subprogram descriptors won't be changed, and
|
|
|
|
// they are stored in map for short duration anyway.
|
2015-04-30 00:38:44 +08:00
|
|
|
DenseMap<const Function *, DISubprogram *> FunctionDIs;
|
2012-10-09 16:13:15 +08:00
|
|
|
|
2010-06-08 04:20:33 +08:00
|
|
|
protected:
|
|
|
|
// DAH uses this to specify a different ID.
|
2010-08-07 02:33:48 +08:00
|
|
|
explicit DAE(char &ID) : ModulePass(ID) {}
|
2010-06-08 04:20:33 +08:00
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
public:
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
DAE() : ModulePass(ID) {
|
|
|
|
initializeDAEPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-06-08 04:20:33 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2003-06-25 12:12:49 +08:00
|
|
|
|
2003-11-06 05:43:02 +08:00
|
|
|
virtual bool ShouldHackArguments() const { return false; }
|
|
|
|
|
2003-06-25 12:12:49 +08:00
|
|
|
private:
|
2008-07-10 18:24:08 +08:00
|
|
|
Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
|
2014-03-09 11:16:01 +08:00
|
|
|
Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses,
|
2015-02-11 03:49:18 +08:00
|
|
|
unsigned RetValNum = -1U);
|
2010-04-01 16:21:08 +08:00
|
|
|
Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2010-04-01 16:21:08 +08:00
|
|
|
void SurveyFunction(const Function &F);
|
2008-07-10 18:24:08 +08:00
|
|
|
void MarkValue(const RetOrArg &RA, Liveness L,
|
|
|
|
const UseVector &MaybeLiveUses);
|
2008-07-15 16:56:49 +08:00
|
|
|
void MarkLive(const RetOrArg &RA);
|
2008-07-15 16:45:12 +08:00
|
|
|
void MarkLive(const Function &F);
|
2008-07-15 17:00:17 +08:00
|
|
|
void PropagateLiveness(const RetOrArg &RA);
|
2008-07-10 18:24:08 +08:00
|
|
|
bool RemoveDeadStuffFromFunction(Function *F);
|
2006-09-18 15:02:31 +08:00
|
|
|
bool DeleteDeadVarargs(Function &Fn);
|
2011-01-17 05:25:33 +08:00
|
|
|
bool RemoveDeadArgumentsFromCallers(Function &Fn);
|
2003-06-18 06:21:05 +08:00
|
|
|
};
|
2008-05-13 08:00:25 +08:00
|
|
|
}
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char DAE::ID = 0;
|
2010-10-08 06:25:06 +08:00
|
|
|
INITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false)
|
2003-11-06 05:43:02 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
namespace {
|
2003-11-06 05:43:02 +08:00
|
|
|
/// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
|
|
|
|
/// deletes arguments to functions which are external. This is only for use
|
|
|
|
/// by bugpoint.
|
|
|
|
struct DAH : public DAE {
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID;
|
2010-08-07 02:33:48 +08:00
|
|
|
DAH() : DAE(ID) {}
|
2010-06-08 04:20:33 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool ShouldHackArguments() const override { return true; }
|
2003-11-06 05:43:02 +08:00
|
|
|
};
|
2003-06-18 06:21:05 +08:00
|
|
|
}
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char DAH::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(DAH, "deadarghaX0r",
|
|
|
|
"Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)",
|
2010-10-08 06:25:06 +08:00
|
|
|
false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2003-06-25 12:12:49 +08:00
|
|
|
/// createDeadArgEliminationPass - This pass removes arguments from functions
|
2003-11-06 05:43:02 +08:00
|
|
|
/// which are not used by the body of the function.
|
2003-06-25 12:12:49 +08:00
|
|
|
///
|
2004-09-20 12:48:05 +08:00
|
|
|
ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
|
|
|
|
ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
|
2003-06-18 06:21:05 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
/// DeleteDeadVarargs - If this is an function that takes a ... list, and if
|
|
|
|
/// llvm.vastart is never called, the varargs list is dead for the function.
|
|
|
|
bool DAE::DeleteDeadVarargs(Function &Fn) {
|
|
|
|
assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
|
2009-01-16 04:18:42 +08:00
|
|
|
if (Fn.isDeclaration() || !Fn.hasLocalLinkage()) return false;
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Ensure that the function is only directly called.
|
2009-06-10 16:41:11 +08:00
|
|
|
if (Fn.hasAddressTaken())
|
|
|
|
return false;
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Okay, we know we can transform this function if safe. Scan its body
|
2014-08-26 08:59:51 +08:00
|
|
|
// looking for calls marked musttail or calls to llvm.vastart.
|
2006-09-18 15:02:31 +08:00
|
|
|
for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
|
2014-08-26 08:59:51 +08:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
if (!CI)
|
|
|
|
continue;
|
|
|
|
if (CI->isMustTailCall())
|
|
|
|
return false;
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
|
2006-09-18 15:02:31 +08:00
|
|
|
if (II->getIntrinsicID() == Intrinsic::vastart)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// If we get here, there are no calls to llvm.vastart in the function body,
|
|
|
|
// remove the "..." and adjust all the calls.
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Start by computing a new prototype for the function, which is the same as
|
2008-07-10 18:24:08 +08:00
|
|
|
// the old function, but doesn't have isVarArg set.
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *FTy = Fn.getFunctionType();
|
2010-03-24 19:58:07 +08:00
|
|
|
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> Params(FTy->param_begin(), FTy->param_end());
|
2013-02-02 08:25:26 +08:00
|
|
|
FunctionType *NFTy = FunctionType::get(FTy->getReturnType(),
|
|
|
|
Params, false);
|
2006-09-18 15:02:31 +08:00
|
|
|
unsigned NumArgs = Params.size();
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Create the new function body and insert it into the module...
|
2008-04-07 04:25:17 +08:00
|
|
|
Function *NF = Function::Create(NFTy, Fn.getLinkage());
|
2008-05-27 03:58:59 +08:00
|
|
|
NF->copyAttributesFrom(&Fn);
|
2006-09-18 15:02:31 +08:00
|
|
|
Fn.getParent()->getFunctionList().insert(&Fn, NF);
|
2007-02-11 09:08:35 +08:00
|
|
|
NF->takeName(&Fn);
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Loop over all of the callers of the function, transforming the call sites
|
|
|
|
// to pass in a smaller number of arguments into the new function.
|
|
|
|
//
|
|
|
|
std::vector<Value*> Args;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator I = Fn.user_begin(), E = Fn.user_end(); I != E; ) {
|
2013-06-14 03:51:17 +08:00
|
|
|
CallSite CS(*I++);
|
|
|
|
if (!CS)
|
|
|
|
continue;
|
2006-09-18 15:02:31 +08:00
|
|
|
Instruction *Call = CS.getInstruction();
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2007-10-19 02:49:29 +08:00
|
|
|
// Pass all the same arguments.
|
2010-07-29 06:50:26 +08:00
|
|
|
Args.assign(CS.arg_begin(), CS.arg_begin() + NumArgs);
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2008-01-12 07:13:45 +08:00
|
|
|
// Drop any attributes that were on the vararg arguments.
|
2012-12-08 07:16:57 +08:00
|
|
|
AttributeSet PAL = CS.getAttributes();
|
2013-01-26 05:46:52 +08:00
|
|
|
if (!PAL.isEmpty() && PAL.getSlotIndex(PAL.getNumSlots() - 1) > NumArgs) {
|
2013-01-26 07:09:36 +08:00
|
|
|
SmallVector<AttributeSet, 8> AttributesVec;
|
2013-01-26 05:46:52 +08:00
|
|
|
for (unsigned i = 0; PAL.getSlotIndex(i) <= NumArgs; ++i)
|
2013-01-26 07:09:36 +08:00
|
|
|
AttributesVec.push_back(PAL.getSlotAttributes(i));
|
2013-01-19 05:11:39 +08:00
|
|
|
if (PAL.hasAttributes(AttributeSet::FunctionIndex))
|
2013-01-27 09:44:34 +08:00
|
|
|
AttributesVec.push_back(AttributeSet::get(Fn.getContext(),
|
|
|
|
PAL.getFnAttributes()));
|
2012-12-08 07:16:57 +08:00
|
|
|
PAL = AttributeSet::get(Fn.getContext(), AttributesVec);
|
2008-01-12 07:13:45 +08:00
|
|
|
}
|
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
Instruction *New;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
|
2008-04-07 04:25:17 +08:00
|
|
|
New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
|
2011-07-15 16:37:34 +08:00
|
|
|
Args, "", Call);
|
2006-09-18 15:02:31 +08:00
|
|
|
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-09-26 05:00:45 +08:00
|
|
|
cast<InvokeInst>(New)->setAttributes(PAL);
|
2006-09-18 15:02:31 +08:00
|
|
|
} else {
|
2011-07-15 16:37:34 +08:00
|
|
|
New = CallInst::Create(NF, Args, "", Call);
|
2006-09-18 15:02:31 +08:00
|
|
|
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-09-26 05:00:45 +08:00
|
|
|
cast<CallInst>(New)->setAttributes(PAL);
|
2006-09-18 15:02:31 +08:00
|
|
|
if (cast<CallInst>(Call)->isTailCall())
|
|
|
|
cast<CallInst>(New)->setTailCall();
|
|
|
|
}
|
2010-07-21 04:09:07 +08:00
|
|
|
New->setDebugLoc(Call->getDebugLoc());
|
2010-05-01 04:23:54 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
Args.clear();
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
if (!Call->use_empty())
|
2007-10-19 02:49:29 +08:00
|
|
|
Call->replaceAllUsesWith(New);
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2007-02-11 09:08:35 +08:00
|
|
|
New->takeName(Call);
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Finally, remove the old call from the program, reducing the use-count of
|
|
|
|
// F.
|
2007-10-19 02:49:29 +08:00
|
|
|
Call->eraseFromParent();
|
2006-09-18 15:02:31 +08:00
|
|
|
}
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2006-09-18 15:02:31 +08:00
|
|
|
// Since we have now created the new function, splice the body of the old
|
|
|
|
// function right into the new function, leaving the old rotting hulk of the
|
|
|
|
// function empty.
|
|
|
|
NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2011-04-15 13:18:47 +08:00
|
|
|
// Loop over the argument list, transferring uses of the old arguments over to
|
|
|
|
// the new arguments, also transferring over the names as well. While we're at
|
2006-09-18 15:02:31 +08:00
|
|
|
// it, remove the dead arguments from the DeadArguments list.
|
|
|
|
//
|
|
|
|
for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
|
|
|
|
I2 = NF->arg_begin(); I != E; ++I, ++I2) {
|
|
|
|
// Move the name and users over to the new version.
|
|
|
|
I->replaceAllUsesWith(I2);
|
2007-02-11 09:08:35 +08:00
|
|
|
I2->takeName(I);
|
2006-09-18 15:02:31 +08:00
|
|
|
}
|
2007-12-22 03:16:16 +08:00
|
|
|
|
2012-10-09 16:13:15 +08:00
|
|
|
// Patch the pointer to LLVM function in debug info descriptor.
|
2014-07-03 02:30:05 +08:00
|
|
|
auto DI = FunctionDIs.find(&Fn);
|
DebugInfo+DeadArgElimination: Ensure llvm::Function*s from debug info are updated even when DAE removes both varargs and non-varargs arguments on the same function.
After some stellar (& inspired) help from Reid Kleckner providing a test
case for some rather unstable undefined behavior showing up as
assertions produced by r214761, I was able to fix this issue in DAE
involving the application of both varargs removal, followed by normal
argument removal.
Indeed I introduced this same bug into ArgumentPromotion (r212128) by
copying the code from DAE, and when I fixed the bug in ArgPromo
(r213805) and commented in that patch that I didn't need to address the
same issue in DAE because it was a single pass. Turns out it's two pass,
one for the varargs and one for the normal arguments, so the same fix is
needed (at least during varargs removal). So here it is.
(the observable/net effect of this bug, even when it didn't result in
assertion failure, is that debug info would describe the DAE'd function
in the abstract, but wouldn't provide high/low_pc, variable locations,
line table, etc (it would appear as though the function had been
entirely optimized away), see the original PR14016 for details of the
general problem)
I'm not recommitting the assertion just yet, as there's been another
regression of it since I last tried. It might just be a few test cases
weren't adequately updated after Adrian or Duncan's recent schema
changes.
llvm-svn: 219210
2014-10-07 23:10:23 +08:00
|
|
|
if (DI != FunctionDIs.end()) {
|
2015-04-30 00:38:44 +08:00
|
|
|
DISubprogram *SP = DI->second;
|
2015-04-14 11:40:37 +08:00
|
|
|
SP->replaceFunction(NF);
|
DebugInfo+DeadArgElimination: Ensure llvm::Function*s from debug info are updated even when DAE removes both varargs and non-varargs arguments on the same function.
After some stellar (& inspired) help from Reid Kleckner providing a test
case for some rather unstable undefined behavior showing up as
assertions produced by r214761, I was able to fix this issue in DAE
involving the application of both varargs removal, followed by normal
argument removal.
Indeed I introduced this same bug into ArgumentPromotion (r212128) by
copying the code from DAE, and when I fixed the bug in ArgPromo
(r213805) and commented in that patch that I didn't need to address the
same issue in DAE because it was a single pass. Turns out it's two pass,
one for the varargs and one for the normal arguments, so the same fix is
needed (at least during varargs removal). So here it is.
(the observable/net effect of this bug, even when it didn't result in
assertion failure, is that debug info would describe the DAE'd function
in the abstract, but wouldn't provide high/low_pc, variable locations,
line table, etc (it would appear as though the function had been
entirely optimized away), see the original PR14016 for details of the
general problem)
I'm not recommitting the assertion just yet, as there's been another
regression of it since I last tried. It might just be a few test cases
weren't adequately updated after Adrian or Duncan's recent schema
changes.
llvm-svn: 219210
2014-10-07 23:10:23 +08:00
|
|
|
// Ensure the map is updated so it can be reused on non-varargs argument
|
|
|
|
// eliminations of the same function.
|
|
|
|
FunctionDIs.erase(DI);
|
|
|
|
FunctionDIs[NF] = SP;
|
|
|
|
}
|
2012-10-09 16:13:15 +08:00
|
|
|
|
2013-06-14 03:51:17 +08:00
|
|
|
// Fix up any BlockAddresses that refer to the function.
|
|
|
|
Fn.replaceAllUsesWith(ConstantExpr::getBitCast(NF, Fn.getType()));
|
|
|
|
// Delete the bitcast that we just created, so that NF does not
|
|
|
|
// appear to be address-taken.
|
|
|
|
NF->removeDeadConstantUsers();
|
2006-09-18 15:02:31 +08:00
|
|
|
// Finally, nuke the old function.
|
|
|
|
Fn.eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-17 05:25:33 +08:00
|
|
|
/// RemoveDeadArgumentsFromCallers - Checks if the given function has any
|
|
|
|
/// arguments that are unused, and changes the caller parameters to be undefined
|
|
|
|
/// instead.
|
|
|
|
bool DAE::RemoveDeadArgumentsFromCallers(Function &Fn)
|
|
|
|
{
|
2011-03-01 08:33:47 +08:00
|
|
|
if (Fn.isDeclaration() || Fn.mayBeOverridden())
|
2011-01-17 05:25:33 +08:00
|
|
|
return false;
|
|
|
|
|
2013-06-09 10:17:27 +08:00
|
|
|
// Functions with local linkage should already have been handled, except the
|
|
|
|
// fragile (variadic) ones which we can improve here.
|
|
|
|
if (Fn.hasLocalLinkage() && !Fn.getFunctionType()->isVarArg())
|
2011-01-17 05:25:33 +08:00
|
|
|
return false;
|
|
|
|
|
2013-10-10 01:21:44 +08:00
|
|
|
// If a function seen at compile time is not necessarily the one linked to
|
|
|
|
// the binary being built, it is illegal to change the actual arguments
|
2013-10-22 07:14:06 +08:00
|
|
|
// passed to it. These functions can be captured by isWeakForLinker().
|
2013-10-10 01:21:44 +08:00
|
|
|
// *NOTE* that mayBeOverridden() is insufficient for this purpose as it
|
2013-10-22 07:14:06 +08:00
|
|
|
// doesn't include linkage types like AvailableExternallyLinkage and
|
2013-10-10 01:21:44 +08:00
|
|
|
// LinkOnceODRLinkage. Take link_odr* as an example, it indicates a set of
|
|
|
|
// *EQUIVALENT* globals that can be merged at link-time. However, the
|
|
|
|
// semantic of *EQUIVALENT*-functions includes parameters. Changing
|
2013-10-22 07:14:06 +08:00
|
|
|
// parameters breaks this assumption.
|
2013-10-10 01:21:44 +08:00
|
|
|
//
|
|
|
|
if (Fn.isWeakForLinker())
|
|
|
|
return false;
|
|
|
|
|
2011-01-17 05:25:33 +08:00
|
|
|
if (Fn.use_empty())
|
|
|
|
return false;
|
|
|
|
|
2013-01-14 00:01:15 +08:00
|
|
|
SmallVector<unsigned, 8> UnusedArgs;
|
2011-01-17 05:25:33 +08:00
|
|
|
for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Argument *Arg = I;
|
|
|
|
|
2014-01-28 10:38:36 +08:00
|
|
|
if (Arg->use_empty() && !Arg->hasByValOrInAllocaAttr())
|
2011-01-17 05:25:33 +08:00
|
|
|
UnusedArgs.push_back(Arg->getArgNo());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UnusedArgs.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Use &U : Fn.uses()) {
|
|
|
|
CallSite CS(U.getUser());
|
|
|
|
if (!CS || !CS.isCallee(&U))
|
2011-01-17 05:25:33 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Now go through all unused args and replace them with "undef".
|
|
|
|
for (unsigned I = 0, E = UnusedArgs.size(); I != E; ++I) {
|
|
|
|
unsigned ArgNo = UnusedArgs[I];
|
|
|
|
|
|
|
|
Value *Arg = CS.getArgument(ArgNo);
|
|
|
|
CS.setArgument(ArgNo, UndefValue::get(Arg->getType()));
|
|
|
|
++NumArgumentsReplacedWithUndef;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2008-07-15 22:39:36 +08:00
|
|
|
/// Convenience function that returns the number of return values. It returns 0
|
|
|
|
/// for void functions and 1 for functions not returning a struct. It returns
|
|
|
|
/// the number of struct elements for functions returning a struct.
|
|
|
|
static unsigned NumRetVals(const Function *F) {
|
2015-02-09 09:21:00 +08:00
|
|
|
Type *RetTy = F->getReturnType();
|
|
|
|
if (RetTy->isVoidTy())
|
2008-07-15 22:39:36 +08:00
|
|
|
return 0;
|
2015-02-09 09:21:00 +08:00
|
|
|
else if (StructType *STy = dyn_cast<StructType>(RetTy))
|
2008-07-15 22:39:36 +08:00
|
|
|
return STy->getNumElements();
|
2015-02-09 09:21:00 +08:00
|
|
|
else if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy))
|
|
|
|
return ATy->getNumElements();
|
2008-07-15 22:39:36 +08:00
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-09 09:21:00 +08:00
|
|
|
/// Returns the sub-type a function will return at a given Idx. Should
|
|
|
|
/// correspond to the result type of an ExtractValue instruction executed with
|
|
|
|
/// just that one Idx (i.e. only top-level structure is considered).
|
|
|
|
static Type *getRetComponentType(const Function *F, unsigned Idx) {
|
|
|
|
Type *RetTy = F->getReturnType();
|
|
|
|
assert(!RetTy->isVoidTy() && "void type has no subtype");
|
|
|
|
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(RetTy))
|
|
|
|
return STy->getElementType(Idx);
|
|
|
|
else if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy))
|
|
|
|
return ATy->getElementType();
|
|
|
|
else
|
|
|
|
return RetTy;
|
|
|
|
}
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
/// MarkIfNotLive - This checks Use for liveness in LiveValues. If Use is not
|
|
|
|
/// live, it adds Use to the MaybeLiveUses argument. Returns the determined
|
|
|
|
/// liveness of Use.
|
|
|
|
DAE::Liveness DAE::MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses) {
|
2008-07-15 17:11:16 +08:00
|
|
|
// We're live if our use or its Function is already marked as live.
|
|
|
|
if (LiveFunctions.count(Use.F) || LiveValues.count(Use))
|
2008-06-25 00:30:26 +08:00
|
|
|
return Live;
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
// We're maybe live otherwise, but remember that we must become live if
|
|
|
|
// Use becomes live.
|
|
|
|
MaybeLiveUses.push_back(Use);
|
|
|
|
return MaybeLive;
|
|
|
|
}
|
|
|
|
|
2008-07-15 22:39:36 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
/// SurveyUse - This looks at a single use of an argument or return value
|
|
|
|
/// and determines if it should be alive or not. Adds this use to MaybeLiveUses
|
2010-04-01 15:34:00 +08:00
|
|
|
/// if it causes the used value to become MaybeLive.
|
2008-07-10 18:24:08 +08:00
|
|
|
///
|
|
|
|
/// RetValNum is the return value number to use when this use is used in a
|
|
|
|
/// return instruction. This is used in the recursion, you should always leave
|
2008-07-15 22:39:36 +08:00
|
|
|
/// it at 0.
|
2014-03-09 11:16:01 +08:00
|
|
|
DAE::Liveness DAE::SurveyUse(const Use *U,
|
2010-04-01 16:21:08 +08:00
|
|
|
UseVector &MaybeLiveUses, unsigned RetValNum) {
|
2014-03-09 11:16:01 +08:00
|
|
|
const User *V = U->getUser();
|
2010-04-01 16:21:08 +08:00
|
|
|
if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) {
|
2008-07-10 18:24:08 +08:00
|
|
|
// The value is returned from a function. It's only live when the
|
|
|
|
// function's return value is live. We use RetValNum here, for the case
|
|
|
|
// that U is really a use of an insertvalue instruction that uses the
|
2011-04-15 13:18:47 +08:00
|
|
|
// original Use.
|
2015-02-11 03:49:18 +08:00
|
|
|
const Function *F = RI->getParent()->getParent();
|
|
|
|
if (RetValNum != -1U) {
|
|
|
|
RetOrArg Use = CreateRet(F, RetValNum);
|
|
|
|
// We might be live, depending on the liveness of Use.
|
|
|
|
return MarkIfNotLive(Use, MaybeLiveUses);
|
|
|
|
} else {
|
2015-02-12 07:13:11 +08:00
|
|
|
DAE::Liveness Result = MaybeLive;
|
2015-02-11 03:49:18 +08:00
|
|
|
for (unsigned i = 0; i < NumRetVals(F); ++i) {
|
|
|
|
RetOrArg Use = CreateRet(F, i);
|
2015-02-12 07:13:11 +08:00
|
|
|
// We might be live, depending on the liveness of Use. If any
|
|
|
|
// sub-value is live, then the entire value is considered live. This
|
|
|
|
// is a conservative choice, and better tracking is possible.
|
|
|
|
DAE::Liveness SubResult = MarkIfNotLive(Use, MaybeLiveUses);
|
|
|
|
if (Result != Live)
|
|
|
|
Result = SubResult;
|
2015-02-11 03:49:18 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
2010-04-01 16:21:08 +08:00
|
|
|
if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {
|
2014-03-09 11:16:01 +08:00
|
|
|
if (U->getOperandNo() != InsertValueInst::getAggregateOperandIndex()
|
2008-07-10 18:24:08 +08:00
|
|
|
&& IV->hasIndices())
|
|
|
|
// The use we are examining is inserted into an aggregate. Our liveness
|
|
|
|
// depends on all uses of that aggregate, but if it is used as a return
|
|
|
|
// value, only index at which we were inserted counts.
|
|
|
|
RetValNum = *IV->idx_begin();
|
|
|
|
|
|
|
|
// Note that if we are used as the aggregate operand to the insertvalue,
|
|
|
|
// we don't change RetValNum, but do survey all our uses.
|
|
|
|
|
|
|
|
Liveness Result = MaybeLive;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const Use &UU : IV->uses()) {
|
|
|
|
Result = SurveyUse(&UU, MaybeLiveUses, RetValNum);
|
2008-07-10 18:24:08 +08:00
|
|
|
if (Result == Live)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2010-04-01 16:21:08 +08:00
|
|
|
|
2015-04-10 22:50:08 +08:00
|
|
|
if (auto CS = ImmutableCallSite(V)) {
|
2010-04-01 16:21:08 +08:00
|
|
|
const Function *F = CS.getCalledFunction();
|
2008-07-10 18:24:08 +08:00
|
|
|
if (F) {
|
|
|
|
// Used in a direct call.
|
2010-03-24 19:58:07 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Find the argument number. We know for sure that this use is an
|
|
|
|
// argument, since if it was the function argument this would be an
|
|
|
|
// indirect call and the we know can't be looking at a value of the
|
|
|
|
// label type (for the invoke instruction).
|
2010-03-24 21:21:49 +08:00
|
|
|
unsigned ArgNo = CS.getArgumentNo(U);
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
if (ArgNo >= F->getFunctionType()->getNumParams())
|
|
|
|
// The value is passed in through a vararg! Must be live.
|
|
|
|
return Live;
|
|
|
|
|
2010-03-24 19:58:07 +08:00
|
|
|
assert(CS.getArgument(ArgNo)
|
2014-03-09 11:16:01 +08:00
|
|
|
== CS->getOperand(U->getOperandNo())
|
2008-07-10 18:24:08 +08:00
|
|
|
&& "Argument is not where we expected it");
|
|
|
|
|
|
|
|
// Value passed to a normal call. It's only live when the corresponding
|
|
|
|
// argument to the called function turns out live.
|
|
|
|
RetOrArg Use = CreateArg(F, ArgNo);
|
|
|
|
return MarkIfNotLive(Use, MaybeLiveUses);
|
|
|
|
}
|
2008-06-26 02:10:09 +08:00
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
// Used in any other way? Value must be live.
|
|
|
|
return Live;
|
2008-06-25 00:30:26 +08:00
|
|
|
}
|
2008-06-21 08:31:44 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
/// SurveyUses - This looks at all the uses of the given value
|
|
|
|
/// Returns the Liveness deduced from the uses of this value.
|
|
|
|
///
|
|
|
|
/// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If
|
|
|
|
/// the result is Live, MaybeLiveUses might be modified but its content should
|
|
|
|
/// be ignored (since it might not be complete).
|
2010-04-01 16:21:08 +08:00
|
|
|
DAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) {
|
2008-07-10 18:24:08 +08:00
|
|
|
// Assume it's dead (which will only hold if there are no uses at all..).
|
|
|
|
Liveness Result = MaybeLive;
|
|
|
|
// Check each use.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const Use &U : V->uses()) {
|
|
|
|
Result = SurveyUse(&U, MaybeLiveUses);
|
2008-07-10 18:24:08 +08:00
|
|
|
if (Result == Live)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2008-06-26 02:10:09 +08:00
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
// SurveyFunction - This performs the initial survey of the specified function,
|
|
|
|
// checking out whether or not it uses any of its incoming arguments or whether
|
2008-07-10 18:24:08 +08:00
|
|
|
// any callers use the return value. This fills in the LiveValues set and Uses
|
|
|
|
// map.
|
2003-10-23 11:48:17 +08:00
|
|
|
//
|
2010-01-24 04:32:12 +08:00
|
|
|
// We consider arguments of non-internal functions to be intrinsically alive as
|
2013-02-02 08:25:26 +08:00
|
|
|
// well as arguments to functions which have their "address taken".
|
2003-06-18 06:21:05 +08:00
|
|
|
//
|
2010-04-01 16:21:08 +08:00
|
|
|
void DAE::SurveyFunction(const Function &F) {
|
2014-02-04 04:42:49 +08:00
|
|
|
// Functions with inalloca parameters are expecting args in a particular
|
|
|
|
// register and memory layout.
|
|
|
|
if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca)) {
|
|
|
|
MarkLive(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-15 22:39:36 +08:00
|
|
|
unsigned RetCount = NumRetVals(&F);
|
2008-07-10 18:24:08 +08:00
|
|
|
// Assume all return values are dead
|
|
|
|
typedef SmallVector<Liveness, 5> RetVals;
|
2008-07-15 22:39:36 +08:00
|
|
|
RetVals RetValLiveness(RetCount, MaybeLive);
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
typedef SmallVector<UseVector, 5> RetUses;
|
|
|
|
// These vectors map each return value to the uses that make it MaybeLive, so
|
|
|
|
// we can add those to the Uses map if the return value really turns out to be
|
2008-07-15 22:39:36 +08:00
|
|
|
// MaybeLive. Initialized to a list of RetCount empty lists.
|
|
|
|
RetUses MaybeLiveRetUses(RetCount);
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2010-04-01 16:21:08 +08:00
|
|
|
for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (const ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
|
2008-07-10 18:24:08 +08:00
|
|
|
if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType()
|
|
|
|
!= F.getFunctionType()->getReturnType()) {
|
|
|
|
// We don't support old style multiple return values.
|
2008-07-15 16:45:12 +08:00
|
|
|
MarkLive(F);
|
|
|
|
return;
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
2008-06-19 01:32:16 +08:00
|
|
|
|
2013-02-02 08:25:26 +08:00
|
|
|
if (!F.hasLocalLinkage() && (!ShouldHackArguments() || F.isIntrinsic())) {
|
2008-07-15 16:45:12 +08:00
|
|
|
MarkLive(F);
|
|
|
|
return;
|
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2010-01-05 09:28:29 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Inspecting callers for fn: " << F.getName() << "\n");
|
2008-07-15 22:39:36 +08:00
|
|
|
// Keep track of the number of live retvals, so we can skip checks once all
|
|
|
|
// of them turn out to be live.
|
|
|
|
unsigned NumLiveRetVals = 0;
|
2008-07-15 16:47:32 +08:00
|
|
|
// Loop all uses of the function.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const Use &U : F.uses()) {
|
2008-07-15 16:47:32 +08:00
|
|
|
// If the function is PASSED IN as an argument, its address has been
|
|
|
|
// taken.
|
2014-03-09 11:16:01 +08:00
|
|
|
ImmutableCallSite CS(U.getUser());
|
|
|
|
if (!CS || !CS.isCallee(&U)) {
|
2008-07-15 16:47:32 +08:00
|
|
|
MarkLive(F);
|
|
|
|
return;
|
|
|
|
}
|
2008-06-05 16:34:25 +08:00
|
|
|
|
2008-07-15 16:47:32 +08:00
|
|
|
// If this use is anything other than a call site, the function is alive.
|
2010-04-01 16:21:08 +08:00
|
|
|
const Instruction *TheCall = CS.getInstruction();
|
2008-07-15 16:47:32 +08:00
|
|
|
if (!TheCall) { // Not a direct call site?
|
|
|
|
MarkLive(F);
|
|
|
|
return;
|
|
|
|
}
|
2008-06-20 23:34:07 +08:00
|
|
|
|
2008-07-15 16:47:32 +08:00
|
|
|
// If we end up here, we are looking at a direct call to our function.
|
|
|
|
|
|
|
|
// Now, check how our return value(s) is/are used in this caller. Don't
|
2008-07-15 22:39:36 +08:00
|
|
|
// bother checking return values if all of them are live already.
|
2015-02-09 09:20:53 +08:00
|
|
|
if (NumLiveRetVals == RetCount)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check all uses of the return value.
|
|
|
|
for (const Use &U : TheCall->uses()) {
|
|
|
|
if (ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(U.getUser())) {
|
|
|
|
// This use uses a part of our return value, survey the uses of
|
|
|
|
// that part and store the results for this index only.
|
|
|
|
unsigned Idx = *Ext->idx_begin();
|
|
|
|
if (RetValLiveness[Idx] != Live) {
|
|
|
|
RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]);
|
|
|
|
if (RetValLiveness[Idx] == Live)
|
|
|
|
NumLiveRetVals++;
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
2008-07-15 22:39:36 +08:00
|
|
|
} else {
|
2015-02-09 09:20:53 +08:00
|
|
|
// Used by something else than extractvalue. Survey, but assume that the
|
|
|
|
// result applies to all sub-values.
|
|
|
|
UseVector MaybeLiveAggregateUses;
|
|
|
|
if (SurveyUse(&U, MaybeLiveAggregateUses) == Live) {
|
2008-07-15 22:39:36 +08:00
|
|
|
NumLiveRetVals = RetCount;
|
2015-02-09 09:20:53 +08:00
|
|
|
RetValLiveness.assign(RetCount, Live);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i != RetCount; ++i) {
|
|
|
|
if (RetValLiveness[i] != Live)
|
|
|
|
MaybeLiveRetUses[i].append(MaybeLiveAggregateUses.begin(),
|
|
|
|
MaybeLiveAggregateUses.end());
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
2003-10-23 11:48:17 +08:00
|
|
|
}
|
2008-07-15 16:47:32 +08:00
|
|
|
}
|
2008-06-20 23:34:07 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Now we've inspected all callers, record the liveness of our return values.
|
2008-07-15 22:39:36 +08:00
|
|
|
for (unsigned i = 0; i != RetCount; ++i)
|
|
|
|
MarkValue(CreateRet(&F, i), RetValLiveness[i], MaybeLiveRetUses[i]);
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2010-01-05 09:28:29 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Inspecting args for fn: " << F.getName() << "\n");
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
// Now, check all of our arguments.
|
|
|
|
unsigned i = 0;
|
|
|
|
UseVector MaybeLiveArgUses;
|
2010-04-01 16:21:08 +08:00
|
|
|
for (Function::const_arg_iterator AI = F.arg_begin(),
|
2008-07-10 18:24:08 +08:00
|
|
|
E = F.arg_end(); AI != E; ++AI, ++i) {
|
2013-06-09 10:17:27 +08:00
|
|
|
Liveness Result;
|
|
|
|
if (F.getFunctionType()->isVarArg()) {
|
|
|
|
// Variadic functions will already have a va_arg function expanded inside
|
|
|
|
// them, making them potentially very sensitive to ABI changes resulting
|
|
|
|
// from removing arguments entirely, so don't. For example AArch64 handles
|
|
|
|
// register and stack HFAs very differently, and this is reflected in the
|
|
|
|
// IR which has already been generated.
|
|
|
|
Result = Live;
|
|
|
|
} else {
|
|
|
|
// See what the effect of this use is (recording any uses that cause
|
|
|
|
// MaybeLive in MaybeLiveArgUses).
|
|
|
|
Result = SurveyUses(AI, MaybeLiveArgUses);
|
|
|
|
}
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Mark the result.
|
|
|
|
MarkValue(CreateArg(&F, i), Result, MaybeLiveArgUses);
|
|
|
|
// Clear the vector again for the next iteration.
|
|
|
|
MaybeLiveArgUses.clear();
|
2003-06-18 06:21:05 +08:00
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
2008-06-19 01:32:16 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
/// MarkValue - This function marks the liveness of RA depending on L. If L is
|
|
|
|
/// MaybeLive, it also takes all uses in MaybeLiveUses and records them in Uses,
|
|
|
|
/// such that RA will be marked live if any use in MaybeLiveUses gets marked
|
|
|
|
/// live later on.
|
|
|
|
void DAE::MarkValue(const RetOrArg &RA, Liveness L,
|
|
|
|
const UseVector &MaybeLiveUses) {
|
|
|
|
switch (L) {
|
|
|
|
case Live: MarkLive(RA); break;
|
2008-06-25 00:30:26 +08:00
|
|
|
case MaybeLive:
|
2008-07-10 18:24:08 +08:00
|
|
|
{
|
|
|
|
// Note any uses of this value, so this return value can be
|
|
|
|
// marked live whenever one of the uses becomes live.
|
|
|
|
for (UseVector::const_iterator UI = MaybeLiveUses.begin(),
|
|
|
|
UE = MaybeLiveUses.end(); UI != UE; ++UI)
|
|
|
|
Uses.insert(std::make_pair(*UI, RA));
|
2008-06-25 00:30:26 +08:00
|
|
|
break;
|
2008-06-21 08:31:44 +08:00
|
|
|
}
|
2008-06-26 02:10:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:45:12 +08:00
|
|
|
/// MarkLive - Mark the given Function as alive, meaning that it cannot be
|
|
|
|
/// changed in any way. Additionally,
|
|
|
|
/// mark any values that are used as this function's parameters or by its return
|
|
|
|
/// values (according to Uses) live as well.
|
|
|
|
void DAE::MarkLive(const Function &F) {
|
2010-01-05 09:28:29 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Intrinsically live fn: " << F.getName() << "\n");
|
2010-05-15 11:41:58 +08:00
|
|
|
// Mark the function as live.
|
|
|
|
LiveFunctions.insert(&F);
|
|
|
|
// Mark all arguments as live.
|
|
|
|
for (unsigned i = 0, e = F.arg_size(); i != e; ++i)
|
|
|
|
PropagateLiveness(CreateArg(&F, i));
|
|
|
|
// Mark all return values as live.
|
|
|
|
for (unsigned i = 0, e = NumRetVals(&F); i != e; ++i)
|
|
|
|
PropagateLiveness(CreateRet(&F, i));
|
2008-07-15 16:45:12 +08:00
|
|
|
}
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
/// MarkLive - Mark the given return value or argument as live. Additionally,
|
|
|
|
/// mark any values that are used by this value (according to Uses) live as
|
|
|
|
/// well.
|
2008-07-15 16:56:49 +08:00
|
|
|
void DAE::MarkLive(const RetOrArg &RA) {
|
2008-07-15 17:11:16 +08:00
|
|
|
if (LiveFunctions.count(RA.F))
|
|
|
|
return; // Function was already marked Live.
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
if (!LiveValues.insert(RA).second)
|
|
|
|
return; // We were already marked Live.
|
2008-06-26 02:10:09 +08:00
|
|
|
|
2010-01-05 09:28:29 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Marking " << RA.getDescription() << " live\n");
|
2008-07-15 17:00:17 +08:00
|
|
|
PropagateLiveness(RA);
|
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2008-07-15 17:00:17 +08:00
|
|
|
/// PropagateLiveness - Given that RA is a live value, propagate it's liveness
|
|
|
|
/// to any other values it uses (according to Uses).
|
|
|
|
void DAE::PropagateLiveness(const RetOrArg &RA) {
|
2008-07-10 18:24:08 +08:00
|
|
|
// We don't use upper_bound (or equal_range) here, because our recursive call
|
|
|
|
// to ourselves is likely to cause the upper_bound (which is the first value
|
|
|
|
// not belonging to RA) to become erased and the iterator invalidated.
|
|
|
|
UseMap::iterator Begin = Uses.lower_bound(RA);
|
|
|
|
UseMap::iterator E = Uses.end();
|
|
|
|
UseMap::iterator I;
|
|
|
|
for (I = Begin; I != E && I->first == RA; ++I)
|
|
|
|
MarkLive(I->second);
|
|
|
|
|
|
|
|
// Erase RA from the Uses map (from the lower bound to wherever we ended up
|
|
|
|
// after the loop).
|
|
|
|
Uses.erase(Begin, I);
|
2008-06-21 08:31:44 +08:00
|
|
|
}
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// RemoveDeadStuffFromFunction - Remove any arguments and return values from F
|
|
|
|
// that are not in LiveValues. Transform the function and all of the callees of
|
|
|
|
// the function to not have these arguments and return values.
|
2003-06-18 06:21:05 +08:00
|
|
|
//
|
2008-07-10 18:24:08 +08:00
|
|
|
bool DAE::RemoveDeadStuffFromFunction(Function *F) {
|
2008-07-15 17:11:16 +08:00
|
|
|
// Don't modify fully live functions
|
|
|
|
if (LiveFunctions.count(F))
|
2008-07-10 18:24:08 +08:00
|
|
|
return false;
|
|
|
|
|
2003-06-18 06:21:05 +08:00
|
|
|
// Start by computing a new prototype for the function, which is the same as
|
2008-07-10 18:24:08 +08:00
|
|
|
// the old function, but has fewer arguments and a different return type.
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *FTy = F->getFunctionType();
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> Params;
|
2003-06-18 06:21:05 +08:00
|
|
|
|
2013-07-01 04:26:21 +08:00
|
|
|
// Keep track of if we have a live 'returned' argument
|
|
|
|
bool HasLiveReturnedArg = false;
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Set up to build a new list of parameter attributes.
|
2013-01-27 09:44:34 +08:00
|
|
|
SmallVector<AttributeSet, 8> AttributesVec;
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet &PAL = F->getAttributes();
|
2007-11-27 21:23:08 +08:00
|
|
|
|
2013-07-01 04:26:21 +08:00
|
|
|
// Remember which arguments are still alive.
|
|
|
|
SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
|
|
|
|
// Construct the new parameter list from non-dead arguments. Also construct
|
|
|
|
// a new set of parameter attributes to correspond. Skip the first parameter
|
|
|
|
// attribute, since that belongs to the return value.
|
|
|
|
unsigned i = 0;
|
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I, ++i) {
|
|
|
|
RetOrArg Arg = CreateArg(F, i);
|
|
|
|
if (LiveValues.erase(Arg)) {
|
|
|
|
Params.push_back(I->getType());
|
|
|
|
ArgAlive[i] = true;
|
|
|
|
|
|
|
|
// Get the original parameter attributes (skipping the first one, that is
|
|
|
|
// for the return value.
|
|
|
|
if (PAL.hasAttributes(i + 1)) {
|
|
|
|
AttrBuilder B(PAL, i + 1);
|
|
|
|
if (B.contains(Attribute::Returned))
|
|
|
|
HasLiveReturnedArg = true;
|
|
|
|
AttributesVec.
|
|
|
|
push_back(AttributeSet::get(F->getContext(), Params.size(), B));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++NumArgumentsEliminated;
|
|
|
|
DEBUG(dbgs() << "DAE - Removing argument " << i << " (" << I->getName()
|
|
|
|
<< ") from " << F->getName() << "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Find out the new return value.
|
2011-07-12 22:06:48 +08:00
|
|
|
Type *RetTy = FTy->getReturnType();
|
2014-04-25 13:29:35 +08:00
|
|
|
Type *NRetTy = nullptr;
|
2008-07-15 22:39:36 +08:00
|
|
|
unsigned RetCount = NumRetVals(F);
|
2010-03-24 19:58:07 +08:00
|
|
|
|
2008-07-15 22:39:36 +08:00
|
|
|
// -1 means unused, other numbers are the new index
|
|
|
|
SmallVector<int, 5> NewRetIdxs(RetCount, -1);
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> RetTypes;
|
2013-07-01 04:26:21 +08:00
|
|
|
|
|
|
|
// If there is a function with a live 'returned' argument but a dead return
|
|
|
|
// value, then there are two possible actions:
|
|
|
|
// 1) Eliminate the return value and take off the 'returned' attribute on the
|
|
|
|
// argument.
|
|
|
|
// 2) Retain the 'returned' attribute and treat the return value (but not the
|
|
|
|
// entire function) as live so that it is not eliminated.
|
|
|
|
//
|
|
|
|
// It's not clear in the general case which option is more profitable because,
|
|
|
|
// even in the absence of explicit uses of the return value, code generation
|
|
|
|
// is free to use the 'returned' attribute to do things like eliding
|
|
|
|
// save/restores of registers across calls. Whether or not this happens is
|
|
|
|
// target and ABI-specific as well as depending on the amount of register
|
|
|
|
// pressure, so there's no good way for an IR-level pass to figure this out.
|
|
|
|
//
|
|
|
|
// Fortunately, the only places where 'returned' is currently generated by
|
|
|
|
// the FE are places where 'returned' is basically free and almost always a
|
|
|
|
// performance win, so the second option can just be used always for now.
|
|
|
|
//
|
|
|
|
// This should be revisited if 'returned' is ever applied more liberally.
|
|
|
|
if (RetTy->isVoidTy() || HasLiveReturnedArg) {
|
2010-04-01 15:34:00 +08:00
|
|
|
NRetTy = RetTy;
|
2008-07-10 18:24:08 +08:00
|
|
|
} else {
|
2015-02-09 09:21:00 +08:00
|
|
|
// Look at each of the original return values individually.
|
|
|
|
for (unsigned i = 0; i != RetCount; ++i) {
|
|
|
|
RetOrArg Ret = CreateRet(F, i);
|
|
|
|
if (LiveValues.erase(Ret)) {
|
|
|
|
RetTypes.push_back(getRetComponentType(F, i));
|
|
|
|
NewRetIdxs[i] = RetTypes.size() - 1;
|
2008-07-15 22:39:36 +08:00
|
|
|
} else {
|
|
|
|
++NumRetValsEliminated;
|
2015-02-09 09:21:00 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Removing return value " << i << " from "
|
|
|
|
<< F->getName() << "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (RetTypes.size() > 1) {
|
|
|
|
// More than one return type? Reduce it down to size.
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(RetTy)) {
|
|
|
|
// Make the new struct packed if we used to return a packed struct
|
|
|
|
// already.
|
|
|
|
NRetTy = StructType::get(STy->getContext(), RetTypes, STy->isPacked());
|
|
|
|
} else {
|
|
|
|
assert(isa<ArrayType>(RetTy) && "unexpected multi-value return");
|
|
|
|
NRetTy = ArrayType::get(RetTypes[0], RetTypes.size());
|
2008-07-15 22:39:36 +08:00
|
|
|
}
|
2015-02-09 09:21:00 +08:00
|
|
|
} else if (RetTypes.size() == 1)
|
2008-07-10 18:24:08 +08:00
|
|
|
// One return type? Just a simple value then, but only if we didn't use to
|
|
|
|
// return a struct with that simple value before.
|
|
|
|
NRetTy = RetTypes.front();
|
|
|
|
else if (RetTypes.size() == 0)
|
2008-07-15 22:39:36 +08:00
|
|
|
// No return types? Make it void, but only if we didn't use to return {}.
|
2009-08-14 05:58:54 +08:00
|
|
|
NRetTy = Type::getVoidTy(F->getContext());
|
2007-12-22 03:16:16 +08:00
|
|
|
}
|
2008-06-20 23:34:07 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
assert(NRetTy && "No new return type found?");
|
|
|
|
|
2013-01-22 06:44:49 +08:00
|
|
|
// The existing function return attributes.
|
|
|
|
AttributeSet RAttrs = PAL.getRetAttributes();
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Remove any incompatible attributes, but only if we removed all return
|
|
|
|
// values. Otherwise, ensure that we don't have any conflicting attributes
|
|
|
|
// here. Currently, this should not be possible, but special handling might be
|
|
|
|
// required when new return value attributes are added.
|
2010-04-01 15:34:00 +08:00
|
|
|
if (NRetTy->isVoidTy())
|
2015-05-07 07:19:56 +08:00
|
|
|
RAttrs = RAttrs.removeAttributes(NRetTy->getContext(),
|
|
|
|
AttributeSet::ReturnIndex,
|
|
|
|
AttributeFuncs::typeIncompatible(NRetTy));
|
2008-07-10 18:24:08 +08:00
|
|
|
else
|
2013-01-22 06:44:49 +08:00
|
|
|
assert(!AttrBuilder(RAttrs, AttributeSet::ReturnIndex).
|
2015-05-07 07:19:56 +08:00
|
|
|
overlaps(AttributeFuncs::typeIncompatible(NRetTy)) &&
|
2012-10-14 15:52:48 +08:00
|
|
|
"Return attributes no longer compatible?");
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2013-01-22 06:44:49 +08:00
|
|
|
if (RAttrs.hasAttributes(AttributeSet::ReturnIndex))
|
2013-01-27 09:44:34 +08:00
|
|
|
AttributesVec.push_back(AttributeSet::get(NRetTy->getContext(), RAttrs));
|
2008-06-20 23:34:07 +08:00
|
|
|
|
2013-01-22 05:57:28 +08:00
|
|
|
if (PAL.hasAttributes(AttributeSet::FunctionIndex))
|
2013-01-27 09:44:34 +08:00
|
|
|
AttributesVec.push_back(AttributeSet::get(F->getContext(),
|
|
|
|
PAL.getFnAttributes()));
|
2008-09-27 06:53:05 +08:00
|
|
|
|
2008-09-26 05:00:45 +08:00
|
|
|
// Reconstruct the AttributesList based on the vector we constructed.
|
2012-12-08 07:16:57 +08:00
|
|
|
AttributeSet NewPAL = AttributeSet::get(F->getContext(), AttributesVec);
|
2003-10-23 11:48:17 +08:00
|
|
|
|
2007-11-27 21:23:08 +08:00
|
|
|
// Create the new function type based on the recomputed parameters.
|
2010-04-01 15:34:00 +08:00
|
|
|
FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
// No change?
|
|
|
|
if (NFTy == FTy)
|
|
|
|
return false;
|
|
|
|
|
2003-06-18 06:21:05 +08:00
|
|
|
// Create the new function body and insert it into the module...
|
2008-04-07 04:25:17 +08:00
|
|
|
Function *NF = Function::Create(NFTy, F->getLinkage());
|
2008-05-27 03:58:59 +08:00
|
|
|
NF->copyAttributesFrom(F);
|
2008-09-26 05:00:45 +08:00
|
|
|
NF->setAttributes(NewPAL);
|
2008-07-10 18:24:08 +08:00
|
|
|
// Insert the new function before the old function, so we won't be processing
|
|
|
|
// it again.
|
2003-06-18 06:21:05 +08:00
|
|
|
F->getParent()->getFunctionList().insert(F, NF);
|
2007-02-11 09:08:35 +08:00
|
|
|
NF->takeName(F);
|
2003-06-18 06:21:05 +08:00
|
|
|
|
|
|
|
// Loop over all of the callers of the function, transforming the call sites
|
|
|
|
// to pass in a smaller number of arguments into the new function.
|
|
|
|
//
|
2003-10-23 11:48:17 +08:00
|
|
|
std::vector<Value*> Args;
|
2003-06-18 06:21:05 +08:00
|
|
|
while (!F->use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
CallSite CS(F->user_back());
|
2003-06-18 06:21:05 +08:00
|
|
|
Instruction *Call = CS.getInstruction();
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2008-09-26 05:00:45 +08:00
|
|
|
AttributesVec.clear();
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet &CallPAL = CS.getAttributes();
|
2007-12-22 03:16:16 +08:00
|
|
|
|
|
|
|
// The call return attributes.
|
2013-01-22 06:44:49 +08:00
|
|
|
AttributeSet RAttrs = CallPAL.getRetAttributes();
|
2013-01-22 05:57:28 +08:00
|
|
|
|
2007-12-22 03:16:16 +08:00
|
|
|
// Adjust in case the function was changed to return void.
|
2015-05-07 07:19:56 +08:00
|
|
|
RAttrs = RAttrs.removeAttributes(NRetTy->getContext(),
|
|
|
|
AttributeSet::ReturnIndex,
|
|
|
|
AttributeFuncs::typeIncompatible(NF->getReturnType()));
|
2013-01-22 06:44:49 +08:00
|
|
|
if (RAttrs.hasAttributes(AttributeSet::ReturnIndex))
|
2013-01-27 09:44:34 +08:00
|
|
|
AttributesVec.push_back(AttributeSet::get(NF->getContext(), RAttrs));
|
2003-10-23 11:48:17 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Declare these outside of the loops, so we can reuse them for the second
|
|
|
|
// loop, which loops the varargs.
|
|
|
|
CallSite::arg_iterator I = CS.arg_begin();
|
|
|
|
unsigned i = 0;
|
|
|
|
// Loop over those operands, corresponding to the normal arguments to the
|
|
|
|
// original function, and add those that are still alive.
|
|
|
|
for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i)
|
|
|
|
if (ArgAlive[i]) {
|
|
|
|
Args.push_back(*I);
|
|
|
|
// Get original parameter attributes, but skip return attributes.
|
2013-01-23 14:14:59 +08:00
|
|
|
if (CallPAL.hasAttributes(i + 1)) {
|
2013-01-27 09:44:34 +08:00
|
|
|
AttrBuilder B(CallPAL, i + 1);
|
2013-07-01 04:26:21 +08:00
|
|
|
// If the return type has changed, then get rid of 'returned' on the
|
|
|
|
// call site. The alternative is to make all 'returned' attributes on
|
|
|
|
// call sites keep the return value alive just like 'returned'
|
|
|
|
// attributes on function declaration but it's less clearly a win
|
|
|
|
// and this is not an expected case anyway
|
|
|
|
if (NRetTy != RetTy && B.contains(Attribute::Returned))
|
|
|
|
B.removeAttribute(Attribute::Returned);
|
2013-01-23 14:14:59 +08:00
|
|
|
AttributesVec.
|
2013-01-27 09:44:34 +08:00
|
|
|
push_back(AttributeSet::get(F->getContext(), Args.size(), B));
|
2013-01-23 14:14:59 +08:00
|
|
|
}
|
2007-12-22 03:16:16 +08:00
|
|
|
}
|
|
|
|
|
2008-01-17 12:18:54 +08:00
|
|
|
// Push any varargs arguments on the list. Don't forget their attributes.
|
2008-07-10 18:24:08 +08:00
|
|
|
for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
|
|
|
|
Args.push_back(*I);
|
2013-01-23 14:14:59 +08:00
|
|
|
if (CallPAL.hasAttributes(i + 1)) {
|
2013-01-27 09:44:34 +08:00
|
|
|
AttrBuilder B(CallPAL, i + 1);
|
2013-01-23 14:14:59 +08:00
|
|
|
AttributesVec.
|
2013-01-27 09:44:34 +08:00
|
|
|
push_back(AttributeSet::get(F->getContext(), Args.size(), B));
|
2013-01-23 14:14:59 +08:00
|
|
|
}
|
2008-01-17 12:18:54 +08:00
|
|
|
}
|
|
|
|
|
2013-01-22 05:57:28 +08:00
|
|
|
if (CallPAL.hasAttributes(AttributeSet::FunctionIndex))
|
2013-01-27 09:44:34 +08:00
|
|
|
AttributesVec.push_back(AttributeSet::get(Call->getContext(),
|
|
|
|
CallPAL.getFnAttributes()));
|
2008-09-27 06:53:05 +08:00
|
|
|
|
2008-09-26 05:00:45 +08:00
|
|
|
// Reconstruct the AttributesList based on the vector we constructed.
|
2012-12-08 07:16:57 +08:00
|
|
|
AttributeSet NewCallPAL = AttributeSet::get(F->getContext(), AttributesVec);
|
2003-10-24 01:44:53 +08:00
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
Instruction *New;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
|
2008-04-07 04:25:17 +08:00
|
|
|
New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
|
2011-07-15 16:37:34 +08:00
|
|
|
Args, "", Call);
|
2005-05-09 09:05:50 +08:00
|
|
|
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-09-26 05:00:45 +08:00
|
|
|
cast<InvokeInst>(New)->setAttributes(NewCallPAL);
|
2003-10-23 11:48:17 +08:00
|
|
|
} else {
|
2011-07-15 16:37:34 +08:00
|
|
|
New = CallInst::Create(NF, Args, "", Call);
|
2005-05-09 09:05:50 +08:00
|
|
|
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-09-26 05:00:45 +08:00
|
|
|
cast<CallInst>(New)->setAttributes(NewCallPAL);
|
2005-05-06 14:46:58 +08:00
|
|
|
if (cast<CallInst>(Call)->isTailCall())
|
|
|
|
cast<CallInst>(New)->setTailCall();
|
2003-10-23 11:48:17 +08:00
|
|
|
}
|
2010-07-21 04:09:07 +08:00
|
|
|
New->setDebugLoc(Call->getDebugLoc());
|
2010-05-01 04:23:54 +08:00
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
Args.clear();
|
|
|
|
|
|
|
|
if (!Call->use_empty()) {
|
2008-07-10 18:24:08 +08:00
|
|
|
if (New->getType() == Call->getType()) {
|
|
|
|
// Return type not changed? Just replace users then.
|
2008-06-26 02:10:09 +08:00
|
|
|
Call->replaceAllUsesWith(New);
|
2008-06-25 00:30:26 +08:00
|
|
|
New->takeName(Call);
|
2010-04-01 15:34:00 +08:00
|
|
|
} else if (New->getType()->isVoidTy()) {
|
2008-07-10 18:24:08 +08:00
|
|
|
// Our return value has uses, but they will get removed later on.
|
|
|
|
// Replace by null for now.
|
2010-10-01 07:57:10 +08:00
|
|
|
if (!Call->getType()->isX86_MMXTy())
|
|
|
|
Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
|
2008-07-10 18:24:08 +08:00
|
|
|
} else {
|
2015-02-09 09:21:00 +08:00
|
|
|
assert((RetTy->isStructTy() || RetTy->isArrayTy()) &&
|
2009-03-18 08:31:45 +08:00
|
|
|
"Return type changed, but not into a void. The old return type"
|
2015-02-09 09:21:00 +08:00
|
|
|
" must have been a struct or an array!");
|
2009-03-18 08:31:45 +08:00
|
|
|
Instruction *InsertPt = Call;
|
2009-03-19 00:23:56 +08:00
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
|
2009-03-19 00:48:45 +08:00
|
|
|
BasicBlock::iterator IP = II->getNormalDest()->begin();
|
|
|
|
while (isa<PHINode>(IP)) ++IP;
|
|
|
|
InsertPt = IP;
|
2009-03-19 00:23:56 +08:00
|
|
|
}
|
2010-03-24 19:58:07 +08:00
|
|
|
|
2015-02-09 09:21:00 +08:00
|
|
|
// We used to return a struct or array. Instead of doing smart stuff
|
|
|
|
// with all the uses, we will just rebuild it using extract/insertvalue
|
|
|
|
// chaining and let instcombine clean that up.
|
2008-07-15 22:03:10 +08:00
|
|
|
//
|
|
|
|
// Start out building up our return value from undef
|
2009-07-31 07:03:37 +08:00
|
|
|
Value *RetVal = UndefValue::get(RetTy);
|
2008-07-15 22:39:36 +08:00
|
|
|
for (unsigned i = 0; i != RetCount; ++i)
|
2008-07-15 22:03:10 +08:00
|
|
|
if (NewRetIdxs[i] != -1) {
|
|
|
|
Value *V;
|
|
|
|
if (RetTypes.size() > 1)
|
|
|
|
// We are still returning a struct, so extract the value from our
|
|
|
|
// return value
|
2009-03-18 08:31:45 +08:00
|
|
|
V = ExtractValueInst::Create(New, NewRetIdxs[i], "newret",
|
|
|
|
InsertPt);
|
2008-07-15 22:03:10 +08:00
|
|
|
else
|
|
|
|
// We are now returning a single element, so just insert that
|
|
|
|
V = New;
|
|
|
|
// Insert the value at the old position
|
2009-03-18 08:31:45 +08:00
|
|
|
RetVal = InsertValueInst::Create(RetVal, V, i, "oldret", InsertPt);
|
2008-07-10 18:24:08 +08:00
|
|
|
}
|
2008-07-15 22:03:10 +08:00
|
|
|
// Now, replace all uses of the old call instruction with the return
|
|
|
|
// struct we built
|
|
|
|
Call->replaceAllUsesWith(RetVal);
|
2008-07-10 18:24:08 +08:00
|
|
|
New->takeName(Call);
|
2008-06-20 23:34:07 +08:00
|
|
|
}
|
2003-10-23 11:48:17 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
// Finally, remove the old call from the program, reducing the use-count of
|
|
|
|
// F.
|
2008-05-30 20:35:46 +08:00
|
|
|
Call->eraseFromParent();
|
2003-06-18 06:21:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since we have now created the new function, splice the body of the old
|
|
|
|
// function right into the new function, leaving the old rotting hulk of the
|
|
|
|
// function empty.
|
|
|
|
NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
|
|
|
|
|
2011-04-15 13:18:47 +08:00
|
|
|
// Loop over the argument list, transferring uses of the old arguments over to
|
|
|
|
// the new arguments, also transferring over the names as well.
|
2008-07-10 18:24:08 +08:00
|
|
|
i = 0;
|
2005-05-06 13:34:40 +08:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
|
2008-07-10 18:24:08 +08:00
|
|
|
I2 = NF->arg_begin(); I != E; ++I, ++i)
|
|
|
|
if (ArgAlive[i]) {
|
2003-06-18 06:21:05 +08:00
|
|
|
// If this is a live argument, move the name and users over to the new
|
|
|
|
// version.
|
|
|
|
I->replaceAllUsesWith(I2);
|
2007-02-11 09:08:35 +08:00
|
|
|
I2->takeName(I);
|
2003-06-18 06:21:05 +08:00
|
|
|
++I2;
|
|
|
|
} else {
|
|
|
|
// If this argument is dead, replace any uses of it with null constants
|
2008-07-10 18:24:08 +08:00
|
|
|
// (these are guaranteed to become unused later on).
|
2010-10-01 07:57:10 +08:00
|
|
|
if (!I->getType()->isX86_MMXTy())
|
|
|
|
I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
|
2003-06-18 06:21:05 +08:00
|
|
|
}
|
|
|
|
|
2003-10-23 11:48:17 +08:00
|
|
|
// If we change the return value of the function we must rewrite any return
|
|
|
|
// instructions. Check this now.
|
|
|
|
if (F->getReturnType() != NF->getReturnType())
|
|
|
|
for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
|
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
2008-07-10 18:24:08 +08:00
|
|
|
Value *RetVal;
|
|
|
|
|
2010-05-15 11:41:58 +08:00
|
|
|
if (NFTy->getReturnType()->isVoidTy()) {
|
2014-04-25 13:29:35 +08:00
|
|
|
RetVal = nullptr;
|
2008-07-10 18:24:08 +08:00
|
|
|
} else {
|
2015-02-09 09:21:00 +08:00
|
|
|
assert(RetTy->isStructTy() || RetTy->isArrayTy());
|
|
|
|
// The original return value was a struct or array, insert
|
2008-07-10 18:24:08 +08:00
|
|
|
// extractvalue/insertvalue chains to extract only the values we need
|
|
|
|
// to return and insert them into our new result.
|
|
|
|
// This does generate messy code, but we'll let it to instcombine to
|
|
|
|
// clean that up.
|
|
|
|
Value *OldRet = RI->getOperand(0);
|
|
|
|
// Start out building up our return value from undef
|
2009-07-31 07:03:37 +08:00
|
|
|
RetVal = UndefValue::get(NRetTy);
|
2008-07-15 22:39:36 +08:00
|
|
|
for (unsigned i = 0; i != RetCount; ++i)
|
2008-07-10 18:24:08 +08:00
|
|
|
if (NewRetIdxs[i] != -1) {
|
|
|
|
ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i,
|
|
|
|
"oldret", RI);
|
|
|
|
if (RetTypes.size() > 1) {
|
|
|
|
// We're still returning a struct, so reinsert the value into
|
|
|
|
// our new return value at the new index
|
|
|
|
|
|
|
|
RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i],
|
|
|
|
"newret", RI);
|
|
|
|
} else {
|
|
|
|
// We are now only returning a simple value, so just return the
|
|
|
|
// extracted value.
|
|
|
|
RetVal = EV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Replace the return instruction with one returning the new return
|
|
|
|
// value (possibly 0 if we became void).
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(F->getContext(), RetVal, RI);
|
2003-10-23 11:48:17 +08:00
|
|
|
BB->getInstList().erase(RI);
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:13:15 +08:00
|
|
|
// Patch the pointer to LLVM function in debug info descriptor.
|
2014-07-03 02:30:05 +08:00
|
|
|
auto DI = FunctionDIs.find(F);
|
2012-10-09 16:13:15 +08:00
|
|
|
if (DI != FunctionDIs.end())
|
2015-04-14 11:40:37 +08:00
|
|
|
DI->second->replaceFunction(NF);
|
2012-10-09 16:13:15 +08:00
|
|
|
|
2003-06-18 06:21:05 +08:00
|
|
|
// Now that the old function is dead, delete it.
|
2008-05-30 20:35:46 +08:00
|
|
|
F->eraseFromParent();
|
2008-07-10 18:24:08 +08:00
|
|
|
|
|
|
|
return true;
|
2003-06-18 06:21:05 +08:00
|
|
|
}
|
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
bool DAE::runOnModule(Module &M) {
|
2007-11-15 14:10:55 +08:00
|
|
|
bool Changed = false;
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2012-10-09 16:13:15 +08:00
|
|
|
// Collect debug info descriptors for functions.
|
2014-07-02 04:05:26 +08:00
|
|
|
FunctionDIs = makeSubprogramMap(M);
|
2012-10-09 16:13:15 +08:00
|
|
|
|
2007-11-15 14:10:55 +08:00
|
|
|
// First pass: Do a simple check to see if any functions can have their "..."
|
|
|
|
// removed. We can do this if they never call va_start. This loop cannot be
|
|
|
|
// fused with the next loop, because deleting a function invalidates
|
|
|
|
// information computed while surveying other functions.
|
2010-01-05 09:28:29 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Deleting dead varargs\n");
|
2006-09-18 15:02:31 +08:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
|
|
|
|
Function &F = *I++;
|
|
|
|
if (F.getFunctionType()->isVarArg())
|
2007-11-15 14:10:55 +08:00
|
|
|
Changed |= DeleteDeadVarargs(F);
|
2006-09-18 15:02:31 +08:00
|
|
|
}
|
2008-07-10 18:24:08 +08:00
|
|
|
|
2007-11-15 14:10:55 +08:00
|
|
|
// Second phase:loop through the module, determining which arguments are live.
|
|
|
|
// We assume all arguments are dead unless proven otherwise (allowing us to
|
|
|
|
// determine that dead arguments passed into recursive functions are dead).
|
|
|
|
//
|
2010-01-05 09:28:29 +08:00
|
|
|
DEBUG(dbgs() << "DAE - Determining liveness\n");
|
2014-10-07 06:59:29 +08:00
|
|
|
for (auto &F : M)
|
|
|
|
SurveyFunction(F);
|
2010-03-24 19:58:07 +08:00
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
// Now, remove all dead arguments and return values from each function in
|
2010-04-01 15:34:00 +08:00
|
|
|
// turn.
|
2008-07-10 18:24:08 +08:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
|
2010-04-01 15:34:00 +08:00
|
|
|
// Increment now, because the function will probably get removed (ie.
|
2008-07-10 18:24:08 +08:00
|
|
|
// replaced by a new one).
|
|
|
|
Function *F = I++;
|
2013-02-02 08:25:26 +08:00
|
|
|
Changed |= RemoveDeadStuffFromFunction(F);
|
2008-06-26 02:10:09 +08:00
|
|
|
}
|
2011-01-17 05:25:33 +08:00
|
|
|
|
|
|
|
// Finally, look for any unused parameters in functions with non-local
|
|
|
|
// linkage and replace the passed in parameters with undef.
|
2014-10-07 06:59:29 +08:00
|
|
|
for (auto &F : M)
|
2011-01-17 05:25:33 +08:00
|
|
|
Changed |= RemoveDeadArgumentsFromCallers(F);
|
|
|
|
|
2008-07-10 18:24:08 +08:00
|
|
|
return Changed;
|
2003-06-18 06:21:05 +08:00
|
|
|
}
|