-//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
+//===-- 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 file 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.
+// Thus find places where the address of memory functions are taken and
+// construct bounce functions with direct calls of those functions.
//
//===----------------------------------------------------------------------===//
namespace {
class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
public:
+ static char ID; // Pass identification, replacement for typeid
+ IndMemRemPass() : ModulePass(&ID) {}
+
virtual bool runOnModule(Module &M);
};
- RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal");
} // end anonymous namespace
+char IndMemRemPass::ID = 0;
+static RegisterPass<IndMemRemPass>
+X("indmemrem","Indirect Malloc and Free Removal");
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
- //happen through intrinsics.
+ // In theory, all direct calls of malloc and free should be promoted
+ // to intrinsics. Therefore, 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
+ // happen through intrinsics.
bool changed = false;
if (Function* F = M.getFunction("free")) {
- assert(F->isDeclaration() && "free not external?");
- if (!F->use_empty()) {
- Function* FN = new Function(F->getFunctionType(),
- GlobalValue::LinkOnceLinkage,
- "free_llvm_bounce", &M);
- BasicBlock* bb = new BasicBlock("entry",FN);
- Instruction* R = new ReturnInst(bb);
+ if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
+ Function* FN = Function::Create(F->getFunctionType(),
+ GlobalValue::LinkOnceAnyLinkage,
+ "free_llvm_bounce", &M);
+ BasicBlock* bb = BasicBlock::Create(M.getContext(), "entry",FN);
+ Instruction* R = ReturnInst::Create(M.getContext(), bb);
new FreeInst(FN->arg_begin(), R);
++NumBounce;
NumBounceSites += F->getNumUses();
}
}
if (Function* F = M.getFunction("malloc")) {
- assert(F->isDeclaration() && "malloc not external?");
- if (!F->use_empty()) {
- Function* FN = new Function(F->getFunctionType(),
- GlobalValue::LinkOnceLinkage,
- "malloc_llvm_bounce", &M);
- BasicBlock* bb = new BasicBlock("entry",FN);
- Instruction* c = CastInst::createIntegerCast(
- FN->arg_begin(), Type::Int32Ty, false, "c", bb);
- Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
- new ReturnInst(a, bb);
+ if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
+ Function* FN = Function::Create(F->getFunctionType(),
+ GlobalValue::LinkOnceAnyLinkage,
+ "malloc_llvm_bounce", &M);
+ FN->setDoesNotAlias(0);
+ BasicBlock* bb = BasicBlock::Create(M.getContext(), "entry",FN);
+ Instruction* c = CastInst::CreateIntegerCast(
+ FN->arg_begin(), Type::getInt32Ty(M.getContext()), false, "c", bb);
+ Instruction* a = new MallocInst(Type::getInt8Ty(M.getContext()),
+ c, "m", bb);
+ ReturnInst::Create(M.getContext(), a, bb);
++NumBounce;
NumBounceSites += F->getNumUses();
F->replaceAllUsesWith(FN);