2006-04-10 19:26:09 +00:00
|
|
|
//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-04-10 19:26:09 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass finds places where memory allocation functions may escape into
|
|
|
|
// indirect land. Some transforms are much easier (aka possible) only if free
|
|
|
|
// or malloc are not called indirectly.
|
|
|
|
// Thus find places where the address of memory functions are taken and construct
|
|
|
|
// bounce functions with direct calls of those functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
#define DEBUG_TYPE "indmemrem"
|
2006-04-10 19:26:09 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Type.h"
|
2007-01-19 21:13:56 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2006-04-10 19:26:09 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-04-10 19:26:09 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumBounceSites, "Number of sites modified");
|
|
|
|
STATISTIC(NumBounce , "Number of bounce functions created");
|
2006-04-10 19:26:09 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
|
2006-04-10 19:26:09 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
IndMemRemPass() : ModulePass((intptr_t)&ID) {}
|
|
|
|
|
2006-04-10 19:26:09 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char IndMemRemPass::ID = 0;
|
|
|
|
static RegisterPass<IndMemRemPass>
|
|
|
|
X("indmemrem","Indirect Malloc and Free Removal");
|
2006-04-10 19:26:09 +00:00
|
|
|
|
|
|
|
bool IndMemRemPass::runOnModule(Module &M) {
|
|
|
|
//in Theory, all direct calls of malloc and free should be promoted
|
|
|
|
//to intrinsics. Therefor, this goes through and finds where the
|
|
|
|
//address of free or malloc are taken and replaces those with bounce
|
|
|
|
//functions, ensuring that all malloc and free that might happen
|
2006-04-13 13:43:31 +00:00
|
|
|
//happen through intrinsics.
|
2006-04-10 19:26:09 +00:00
|
|
|
bool changed = false;
|
2007-02-05 21:19:13 +00:00
|
|
|
if (Function* F = M.getFunction("free")) {
|
2007-01-30 20:08:39 +00:00
|
|
|
assert(F->isDeclaration() && "free not external?");
|
2006-04-13 13:43:31 +00:00
|
|
|
if (!F->use_empty()) {
|
2008-04-06 20:25:17 +00:00
|
|
|
Function* FN = Function::Create(F->getFunctionType(),
|
|
|
|
GlobalValue::LinkOnceLinkage,
|
|
|
|
"free_llvm_bounce", &M);
|
|
|
|
BasicBlock* bb = BasicBlock::Create("entry",FN);
|
|
|
|
Instruction* R = ReturnInst::Create(bb);
|
2006-04-10 19:26:09 +00:00
|
|
|
new FreeInst(FN->arg_begin(), R);
|
|
|
|
++NumBounce;
|
|
|
|
NumBounceSites += F->getNumUses();
|
|
|
|
F->replaceAllUsesWith(FN);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
2007-02-05 21:19:13 +00:00
|
|
|
if (Function* F = M.getFunction("malloc")) {
|
2007-01-30 20:08:39 +00:00
|
|
|
assert(F->isDeclaration() && "malloc not external?");
|
2006-04-13 13:43:31 +00:00
|
|
|
if (!F->use_empty()) {
|
2008-04-06 20:25:17 +00:00
|
|
|
Function* FN = Function::Create(F->getFunctionType(),
|
|
|
|
GlobalValue::LinkOnceLinkage,
|
|
|
|
"malloc_llvm_bounce", &M);
|
|
|
|
BasicBlock* bb = BasicBlock::Create("entry",FN);
|
2008-05-16 19:29:10 +00:00
|
|
|
Instruction* c = CastInst::CreateIntegerCast(
|
2006-12-31 05:48:39 +00:00
|
|
|
FN->arg_begin(), Type::Int32Ty, false, "c", bb);
|
|
|
|
Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
|
2008-04-06 20:25:17 +00:00
|
|
|
ReturnInst::Create(a, bb);
|
2006-04-10 19:26:09 +00:00
|
|
|
++NumBounce;
|
|
|
|
NumBounceSites += F->getNumUses();
|
|
|
|
F->replaceAllUsesWith(FN);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePass *llvm::createIndMemRemPass() {
|
|
|
|
return new IndMemRemPass();
|
|
|
|
}
|