blob: eeddad71a9795e66d97305ba2cf9f6f5ae4aed8f [file] [log] [blame]
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +00001//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Lattner1631bcb2006-12-19 22:09:18 +000018#define DEBUG_TYPE "indmemrem"
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000019#include "llvm/Transforms/IPO.h"
20#include "llvm/Pass.h"
21#include "llvm/Module.h"
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000022#include "llvm/Instructions.h"
23#include "llvm/Type.h"
Reid Spencera94d3942007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000025#include "llvm/ADT/Statistic.h"
Reid Spencer557ab152007-02-05 23:32:05 +000026#include "llvm/Support/Compiler.h"
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000027using namespace llvm;
28
Chris Lattner1631bcb2006-12-19 22:09:18 +000029STATISTIC(NumBounceSites, "Number of sites modified");
30STATISTIC(NumBounce , "Number of bounce functions created");
31
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000032namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000033 class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000034 public:
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000035 virtual bool runOnModule(Module &M);
36 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000037 RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal");
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000038} // end anonymous namespace
39
40
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000041bool IndMemRemPass::runOnModule(Module &M) {
42 //in Theory, all direct calls of malloc and free should be promoted
43 //to intrinsics. Therefor, this goes through and finds where the
44 //address of free or malloc are taken and replaces those with bounce
45 //functions, ensuring that all malloc and free that might happen
Andrew Lenharth92cf71f2006-04-13 13:43:31 +000046 //happen through intrinsics.
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000047 bool changed = false;
Reid Spencer1241d6d2007-02-05 21:19:13 +000048 if (Function* F = M.getFunction("free")) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000049 assert(F->isDeclaration() && "free not external?");
Andrew Lenharth92cf71f2006-04-13 13:43:31 +000050 if (!F->use_empty()) {
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000051 Function* FN = new Function(F->getFunctionType(),
Anton Korobeynikovfb801512007-04-16 18:10:23 +000052 GlobalValue::LinkOnceLinkage,
53 "free_llvm_bounce", &M);
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000054 BasicBlock* bb = new BasicBlock("entry",FN);
55 Instruction* R = new ReturnInst(bb);
56 new FreeInst(FN->arg_begin(), R);
57 ++NumBounce;
58 NumBounceSites += F->getNumUses();
59 F->replaceAllUsesWith(FN);
60 changed = true;
61 }
62 }
Reid Spencer1241d6d2007-02-05 21:19:13 +000063 if (Function* F = M.getFunction("malloc")) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000064 assert(F->isDeclaration() && "malloc not external?");
Andrew Lenharth92cf71f2006-04-13 13:43:31 +000065 if (!F->use_empty()) {
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000066 Function* FN = new Function(F->getFunctionType(),
Anton Korobeynikovfb801512007-04-16 18:10:23 +000067 GlobalValue::LinkOnceLinkage,
68 "malloc_llvm_bounce", &M);
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000069 BasicBlock* bb = new BasicBlock("entry",FN);
Reid Spencerbfe26ff2006-12-13 00:50:17 +000070 Instruction* c = CastInst::createIntegerCast(
Reid Spencerc635f472006-12-31 05:48:39 +000071 FN->arg_begin(), Type::Int32Ty, false, "c", bb);
72 Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
Reid Spencerde46e482006-11-02 20:25:50 +000073 new ReturnInst(a, bb);
Andrew Lenhartha9cdcca2006-04-10 19:26:09 +000074 ++NumBounce;
75 NumBounceSites += F->getNumUses();
76 F->replaceAllUsesWith(FN);
77 changed = true;
78 }
79 }
80 return changed;
81}
82
83ModulePass *llvm::createIndMemRemPass() {
84 return new IndMemRemPass();
85}