[llvm-commits] [poolalloc] r128931 - /poolalloc/trunk/lib/AssistDS/FuncSpec.cpp
Arushi Aggarwal
aggarwa4 at illinois.edu
Tue Apr 5 14:29:37 PDT 2011
Author: aggarwa4
Date: Tue Apr 5 16:29:37 2011
New Revision: 128931
URL: http://llvm.org/viewvc/llvm-project?rev=128931&view=rev
Log:
Added comments.
Modified:
poolalloc/trunk/lib/AssistDS/FuncSpec.cpp
Modified: poolalloc/trunk/lib/AssistDS/FuncSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/FuncSpec.cpp?rev=128931&r1=128930&r2=128931&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/FuncSpec.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/FuncSpec.cpp Tue Apr 5 16:29:37 2011
@@ -27,6 +27,7 @@
using namespace llvm;
+// Pass statistics
STATISTIC(numCloned, "Number of Functions Cloned in FuncSpec");
STATISTIC(numReplaced, "Number of Calls Replaced");
@@ -35,6 +36,23 @@
public:
static char ID;
FuncSpec() : ModulePass(&ID) {}
+ //
+ // Method: runOnModule()
+ //
+ // Description:
+ // Entry point for this LLVM pass. Search for call sites, that take functions as arguments
+ // Clone those functions, and pass the clone.
+ //
+ // Inputs:
+ // M - A reference to the LLVM module to transform
+ //
+ // Outputs:
+ // M - The transformed LLVM module.
+ //
+ // Return value:
+ // true - The module was modified.
+ // false - The module was not modified.
+ //
bool runOnModule(Module& M) {
std::map<CallInst*, std::vector<std::pair<unsigned, Constant*> > > cloneSites;
std::map<std::pair<Function*, std::vector<std::pair<unsigned, Constant*> > >, Function* > toClone;
@@ -43,26 +61,36 @@
if (!I->isDeclaration() && !I->mayBeOverridden()) {
std::vector<unsigned> FPArgs;
for (Function::arg_iterator ii = I->arg_begin(), ee = I->arg_end();
- ii != ee; ++ii)
+ ii != ee; ++ii) {
+ // check if this function has a FunctionType(or a pointer to) argument
if (const PointerType* Ty = dyn_cast<PointerType>(ii->getType())) {
if (isa<FunctionType>(Ty->getElementType())) {
+ // Store the index of such an argument
FPArgs.push_back(ii->getArgNo());
DEBUG(errs() << "Eligible: " << I->getNameStr() << "\n");
}
} else if (isa<FunctionType>(ii->getType())) {
+ // Store the index of such an argument
FPArgs.push_back(ii->getArgNo());
DEBUG(errs() << "Eligible: " << I->getNameStr() << "\n");
}
+ }
+ // Now find all call sites that it is called from
for(Value::use_iterator ui = I->use_begin(), ue = I->use_end();
ui != ue; ++ui) {
if (CallInst* CI = dyn_cast<CallInst>(ui)) {
+ // Check that it is the called value (and not an argument)
if(CI->getCalledValue()->stripPointerCasts() == I) {
std::vector<std::pair<unsigned, Constant*> > Consts;
for (unsigned x = 0; x < FPArgs.size(); ++x)
if (Constant* C = dyn_cast<Constant>(ui->getOperand(FPArgs.at(x) + 1))) {
+ // If the argument passed, at any of the locations noted earlier
+ // is a constant function, store the pair
Consts.push_back(std::make_pair(FPArgs.at(x), C));
}
if (!Consts.empty()) {
+ // If at least one of the arguments is a constant function,
+ // we must clone the function.
cloneSites[CI] = Consts;
toClone[std::make_pair(I, Consts)] = 0;
}
@@ -74,14 +102,16 @@
numCloned += toClone.size();
for (std::map<std::pair<Function*, std::vector<std::pair<unsigned, Constant*> > >, Function* >::iterator I = toClone.begin(), E = toClone.end(); I != E; ++I) {
+ // Clone all the functions we need cloned
Function* DirectF = CloneFunction(I->first.first);
DirectF->setName(I->first.first->getNameStr() + "_SPEC");
DirectF->setLinkage(GlobalValue::InternalLinkage);
I->first.first->getParent()->getFunctionList().push_back(DirectF);
I->second = DirectF;
}
-
+
for (std::map<CallInst*, std::vector<std::pair<unsigned, Constant*> > >::iterator ii = cloneSites.begin(), ee = cloneSites.end(); ii != ee; ++ii) {
+ // Transform the call sites, to call the clones
ii->first->setOperand(0, toClone[std::make_pair(cast<Function>(ii->first->getOperand(0)), ii->second)]);
++numReplaced;
}
More information about the llvm-commits
mailing list