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
|
2008-09-04 17:05:41 +00:00
|
|
|
IndMemRemPass() : ModulePass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
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) {
|
2009-01-25 07:59:57 +00:00
|
|
|
// 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.
|
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")) {
|
2008-09-06 17:44:06 +00:00
|
|
|
if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
|
2008-04-06 20:25:17 +00:00
|
|
|
Function* FN = Function::Create(F->getFunctionType(),
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
GlobalValue::LinkOnceAnyLinkage,
|
2008-04-06 20:25:17 +00:00
|
|
|
"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")) {
|
2008-09-06 17:44:06 +00:00
|
|
|
if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
|
2008-04-06 20:25:17 +00:00
|
|
|
Function* FN = Function::Create(F->getFunctionType(),
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
GlobalValue::LinkOnceAnyLinkage,
|
2008-04-06 20:25:17 +00:00
|
|
|
"malloc_llvm_bounce", &M);
|
2009-01-25 07:59:57 +00:00
|
|
|
FN->setDoesNotAlias(0);
|
2008-04-06 20:25:17 +00:00
|
|
|
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();
|
|
|
|
}
|