#include "llvm/Support/CallSite.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Compiler.h"
#include <set>
using namespace llvm;
-namespace {
- Statistic NumArgumentsEliminated("deadargelim",
- "Number of unread args removed");
- Statistic NumRetValsEliminated("deadargelim",
- "Number of unused return values removed");
+STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
+STATISTIC(NumRetValsEliminated , "Number of unused return values removed");
+namespace {
/// DAE - The dead argument elimination pass.
///
- class DAE : public ModulePass {
+ class VISIBILITY_HIDDEN DAE : public ModulePass {
/// Liveness enum - During our initial pass over the program, we determine
/// that things are either definately alive, definately dead, or in need of
/// interprocedural analysis (MaybeLive).
std::multimap<Function*, CallSite> CallSites;
public:
+ static char ID; // Pass identifcation, replacement for typeid
+ DAE() : ModulePass((intptr_t)&ID) {}
bool runOnModule(Module &M);
virtual bool ShouldHackArguments() const { return false; }
void RemoveDeadArgumentsFromFunction(Function *F);
};
+ char DAE::ID = 0;
RegisterPass<DAE> X("deadargelim", "Dead Argument Elimination");
/// 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 {
+ static char ID;
virtual bool ShouldHackArguments() const { return true; }
};
+ char DAH::ID = 0;
RegisterPass<DAH> Y("deadarghaX0r",
"Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
}
/// 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!");
- if (Fn.isExternal() || !Fn.hasInternalLinkage()) return false;
+ if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false;
// Ensure that the function is only directly called.
for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) {
unsigned NumArgs = Params.size();
// Create the new function body and insert it into the module...
- Function *NF = new Function(NFTy, Fn.getLinkage(), Fn.getName());
+ Function *NF = new Function(NFTy, Fn.getLinkage());
NF->setCallingConv(Fn.getCallingConv());
Fn.getParent()->getFunctionList().insert(&Fn, NF);
+ NF->takeName(&Fn);
// 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.
Instruction *New;
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
- Args, "", Call);
+ &Args[0], Args.size(), "", Call);
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
} else {
- New = new CallInst(NF, Args, "", Call);
+ New = new CallInst(NF, &Args[0], Args.size(), "", Call);
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
if (cast<CallInst>(Call)->isTailCall())
cast<CallInst>(New)->setTailCall();
if (!Call->use_empty())
Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
- if (Call->hasName()) {
- std::string Name = Call->getName();
- Call->setName("");
- New->setName(Name);
- }
+ New->takeName(Call);
// Finally, remove the old call from the program, reducing the use-count of
// F.
I2 = NF->arg_begin(); I != E; ++I, ++I2) {
// Move the name and users over to the new version.
I->replaceAllUsesWith(I2);
- I2->setName(I->getName());
+ I2->takeName(I);
}
// Finally, nuke the old function.
// (used in a computation), MaybeLive (only passed as an argument to a call), or
// Dead (not used).
DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
- // If this is the return value of a csret function, it's not really dead.
- if (A.getParent()->getCallingConv() == CallingConv::CSRet &&
- &*A.getParent()->arg_begin() == &A)
+ const FunctionType *FTy = A.getParent()->getFunctionType();
+
+ // If this is the return value of a struct function, it's not really dead.
+ if (FTy->isStructReturn() && &*A.getParent()->arg_begin() == &A)
return Live;
if (A.use_empty()) // First check, directly dead?
// Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
// have zero fixed arguments.
//
- // FIXME: once this bug is fixed in the CWriter, this hack should be removed.
- //
bool ExtraArgHack = false;
if (Params.empty() && FTy->isVarArg()) {
ExtraArgHack = true;
- Params.push_back(Type::IntTy);
+ Params.push_back(Type::Int32Ty);
}
FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
// Create the new function body and insert it into the module...
- Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
+ Function *NF = new Function(NFTy, F->getLinkage());
NF->setCallingConv(F->getCallingConv());
F->getParent()->getFunctionList().insert(F, NF);
+ NF->takeName(F);
// 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.
Args.push_back(*AI);
if (ExtraArgHack)
- Args.push_back(Constant::getNullValue(Type::IntTy));
+ Args.push_back(UndefValue::get(Type::Int32Ty));
// Push any varargs arguments on the list
for (; AI != CS.arg_end(); ++AI)
Instruction *New;
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
- Args, "", Call);
+ &Args[0], Args.size(), "", Call);
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
} else {
- New = new CallInst(NF, Args, "", Call);
+ New = new CallInst(NF, &Args[0], Args.size(), "", Call);
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
if (cast<CallInst>(Call)->isTailCall())
cast<CallInst>(New)->setTailCall();
Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
else {
Call->replaceAllUsesWith(New);
- std::string Name = Call->getName();
- Call->setName("");
- New->setName(Name);
+ New->takeName(Call);
}
}
// If this is a live argument, move the name and users over to the new
// version.
I->replaceAllUsesWith(I2);
- I2->setName(I->getName());
+ I2->takeName(I);
++I2;
} else {
// If this argument is dead, replace any uses of it with null constants