2006-04-10 19:26:09 +00:00
|
|
|
//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
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 {
|
2006-04-10 19:26:09 +00:00
|
|
|
class IndMemRemPass : public ModulePass {
|
|
|
|
public:
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
};
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal");
|
2006-04-10 19:26:09 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (Function* F = M.getNamedFunction("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()) {
|
2006-04-10 19:26:09 +00:00
|
|
|
Function* FN = new Function(F->getFunctionType(),
|
|
|
|
GlobalValue::LinkOnceLinkage,
|
|
|
|
"free_llvm_bounce", &M);
|
|
|
|
BasicBlock* bb = new BasicBlock("entry",FN);
|
|
|
|
Instruction* R = new ReturnInst(bb);
|
|
|
|
new FreeInst(FN->arg_begin(), R);
|
|
|
|
++NumBounce;
|
|
|
|
NumBounceSites += F->getNumUses();
|
|
|
|
F->replaceAllUsesWith(FN);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Function* F = M.getNamedFunction("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()) {
|
2006-04-10 19:26:09 +00:00
|
|
|
Function* FN = new Function(F->getFunctionType(),
|
|
|
|
GlobalValue::LinkOnceLinkage,
|
|
|
|
"malloc_llvm_bounce", &M);
|
|
|
|
BasicBlock* bb = new BasicBlock("entry",FN);
|
2006-12-13 00:50:17 +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);
|
2006-11-02 20:25:50 +00:00
|
|
|
new ReturnInst(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();
|
|
|
|
}
|