blob: a623135bc1e2d77b6f5bea035c56dc6cc89cbeec [file] [log] [blame]
Andrew Lenharth34331412006-04-10 19:26:09 +00001//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth34331412006-04-10 19:26:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass finds places where memory allocation functions may escape into
11// indirect land. Some transforms are much easier (aka possible) only if free
12// or malloc are not called indirectly.
13// Thus find places where the address of memory functions are taken and construct
14// bounce functions with direct calls of those functions.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner86453c52006-12-19 22:09:18 +000018#define DEBUG_TYPE "indmemrem"
Andrew Lenharth34331412006-04-10 19:26:09 +000019#include "llvm/Transforms/IPO.h"
20#include "llvm/Pass.h"
21#include "llvm/Module.h"
Andrew Lenharth34331412006-04-10 19:26:09 +000022#include "llvm/Instructions.h"
23#include "llvm/Type.h"
Reid Spencerc1030572007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Andrew Lenharth34331412006-04-10 19:26:09 +000025#include "llvm/ADT/Statistic.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000026#include "llvm/Support/Compiler.h"
Andrew Lenharth34331412006-04-10 19:26:09 +000027using namespace llvm;
28
Chris Lattner86453c52006-12-19 22:09:18 +000029STATISTIC(NumBounceSites, "Number of sites modified");
30STATISTIC(NumBounce , "Number of bounce functions created");
31
Andrew Lenharth34331412006-04-10 19:26:09 +000032namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000033 class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
Andrew Lenharth34331412006-04-10 19:26:09 +000034 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000035 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000036 IndMemRemPass() : ModulePass((intptr_t)&ID) {}
37
Andrew Lenharth34331412006-04-10 19:26:09 +000038 virtual bool runOnModule(Module &M);
39 };
Andrew Lenharth34331412006-04-10 19:26:09 +000040} // end anonymous namespace
41
Dan Gohman844731a2008-05-13 00:00:25 +000042char IndMemRemPass::ID = 0;
43static RegisterPass<IndMemRemPass>
44X("indmemrem","Indirect Malloc and Free Removal");
Andrew Lenharth34331412006-04-10 19:26:09 +000045
Andrew Lenharth34331412006-04-10 19:26:09 +000046bool IndMemRemPass::runOnModule(Module &M) {
47 //in Theory, all direct calls of malloc and free should be promoted
48 //to intrinsics. Therefor, this goes through and finds where the
49 //address of free or malloc are taken and replaces those with bounce
50 //functions, ensuring that all malloc and free that might happen
Andrew Lenharth61e99c92006-04-13 13:43:31 +000051 //happen through intrinsics.
Andrew Lenharth34331412006-04-10 19:26:09 +000052 bool changed = false;
Reid Spencer688b0492007-02-05 21:19:13 +000053 if (Function* F = M.getFunction("free")) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000054 assert(F->isDeclaration() && "free not external?");
Andrew Lenharth61e99c92006-04-13 13:43:31 +000055 if (!F->use_empty()) {
Gabor Greif051a9502008-04-06 20:25:17 +000056 Function* FN = Function::Create(F->getFunctionType(),
57 GlobalValue::LinkOnceLinkage,
58 "free_llvm_bounce", &M);
59 BasicBlock* bb = BasicBlock::Create("entry",FN);
60 Instruction* R = ReturnInst::Create(bb);
Andrew Lenharth34331412006-04-10 19:26:09 +000061 new FreeInst(FN->arg_begin(), R);
62 ++NumBounce;
63 NumBounceSites += F->getNumUses();
64 F->replaceAllUsesWith(FN);
65 changed = true;
66 }
67 }
Reid Spencer688b0492007-02-05 21:19:13 +000068 if (Function* F = M.getFunction("malloc")) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000069 assert(F->isDeclaration() && "malloc not external?");
Andrew Lenharth61e99c92006-04-13 13:43:31 +000070 if (!F->use_empty()) {
Gabor Greif051a9502008-04-06 20:25:17 +000071 Function* FN = Function::Create(F->getFunctionType(),
72 GlobalValue::LinkOnceLinkage,
73 "malloc_llvm_bounce", &M);
74 BasicBlock* bb = BasicBlock::Create("entry",FN);
Reid Spencer7b06bd52006-12-13 00:50:17 +000075 Instruction* c = CastInst::createIntegerCast(
Reid Spencerc5b206b2006-12-31 05:48:39 +000076 FN->arg_begin(), Type::Int32Ty, false, "c", bb);
77 Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
Gabor Greif051a9502008-04-06 20:25:17 +000078 ReturnInst::Create(a, bb);
Andrew Lenharth34331412006-04-10 19:26:09 +000079 ++NumBounce;
80 NumBounceSites += F->getNumUses();
81 F->replaceAllUsesWith(FN);
82 changed = true;
83 }
84 }
85 return changed;
86}
87
88ModulePass *llvm::createIndMemRemPass() {
89 return new IndMemRemPass();
90}