#include "llvm/ADT/Statistic.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Support/CallSite.h"
-#include "llvm/Support/Compiler.h"
#include "llvm/ADT/DenseSet.h"
#include <map>
using namespace llvm;
static const double ConstValPercent = .1;
namespace {
- class VISIBILITY_HIDDEN PartSpec : public ModulePass {
+ class PartSpec : public ModulePass {
void scanForInterest(Function&, SmallVector<int, 6>&);
int scanDistribution(Function&, int, std::map<Constant*, int>&);
public :
DenseSet<unsigned> deleted;
for (DenseMap<const Value*, Value*>::iterator
repb = replacements.begin(), repe = replacements.end();
- repb != repe; ++ repb)
+ repb != repe; ++repb)
deleted.insert(cast<Argument>(repb->first)->getArgNo());
Function* NF = CloneFunction(F, replacements);
for (Value::use_iterator ii = F->use_begin(), ee = F->use_end();
ii != ee; ) {
- Value::use_iterator i = ii;;
+ Value::use_iterator i = ii;
++ii;
if (isa<CallInst>(i) || isa<InvokeInst>(i)) {
CallSite CS(cast<Instruction>(i));
if (!deleted.count(x))
args.push_back(CS.getArgument(x));
Value* NCall;
- if (isa<CallInst>(i))
+ if (CallInst *CI = dyn_cast<CallInst>(i)) {
NCall = CallInst::Create(NF, args.begin(), args.end(),
- CS.getInstruction()->getName(),
- CS.getInstruction());
- else
- NCall = InvokeInst::Create(NF, cast<InvokeInst>(i)->getNormalDest(),
- cast<InvokeInst>(i)->getUnwindDest(),
+ CI->getName(), CI);
+ cast<CallInst>(NCall)->setTailCall(CI->isTailCall());
+ cast<CallInst>(NCall)->setCallingConv(CI->getCallingConv());
+ } else {
+ InvokeInst *II = cast<InvokeInst>(i);
+ NCall = InvokeInst::Create(NF, II->getNormalDest(),
+ II->getUnwindDest(),
args.begin(), args.end(),
- CS.getInstruction()->getName(),
- CS.getInstruction());
+ II->getName(), II);
+ cast<InvokeInst>(NCall)->setCallingConv(II->getCallingConv());
+ }
CS.getInstruction()->replaceAllUsesWith(NCall);
CS.getInstruction()->eraseFromParent();
}
bool Changed = false;
for (Module::iterator I = M.begin(); I != M.end(); ++I) {
Function &F = *I;
- if (F.isDeclaration()) continue;
+ if (F.isDeclaration() || F.mayBeOverridden()) continue;
SmallVector<int, 6> interestingArgs;
scanForInterest(F, interestingArgs);
}
}
+/// scanDistribution - Construct a histogram of constants for arg of F at arg.
int PartSpec::scanDistribution(Function& F, int arg,
std::map<Constant*, int>& dist) {
bool hasIndirect = false;
int total = 0;
for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
ii != ee; ++ii)
- if (CallInst* CI = dyn_cast<CallInst>(ii)) {
- ++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))];
+ if ((isa<CallInst>(ii) || isa<InvokeInst>(ii))
+ && ii->getOperand(0) == &F) {
+ ++dist[dyn_cast<Constant>(ii->getOperand(arg + 1))];
++total;
} else
hasIndirect = true;