blob: f21e4f636822d5c6a91ed71ac2455a02034b944e [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//
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 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:
Devang Patel19974732007-05-03 01:11:54 +000035 static char ID; // Pass identifcation, 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 };
Devang Patel19974732007-05-03 01:11:54 +000040 char IndMemRemPass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000041 RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal");
Andrew Lenharth34331412006-04-10 19:26:09 +000042} // end anonymous namespace
43
44
Andrew Lenharth34331412006-04-10 19:26:09 +000045bool IndMemRemPass::runOnModule(Module &M) {
46 //in Theory, all direct calls of malloc and free should be promoted
47 //to intrinsics. Therefor, this goes through and finds where the
48 //address of free or malloc are taken and replaces those with bounce
49 //functions, ensuring that all malloc and free that might happen
Andrew Lenharth61e99c92006-04-13 13:43:31 +000050 //happen through intrinsics.
Andrew Lenharth34331412006-04-10 19:26:09 +000051 bool changed = false;
Reid Spencer688b0492007-02-05 21:19:13 +000052 if (Function* F = M.getFunction("free")) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000053 assert(F->isDeclaration() && "free not external?");
Andrew Lenharth61e99c92006-04-13 13:43:31 +000054 if (!F->use_empty()) {
Andrew Lenharth34331412006-04-10 19:26:09 +000055 Function* FN = new Function(F->getFunctionType(),
Anton Korobeynikovbed29462007-04-16 18:10:23 +000056 GlobalValue::LinkOnceLinkage,
57 "free_llvm_bounce", &M);
Andrew Lenharth34331412006-04-10 19:26:09 +000058 BasicBlock* bb = new BasicBlock("entry",FN);
59 Instruction* R = new ReturnInst(bb);
60 new FreeInst(FN->arg_begin(), R);
61 ++NumBounce;
62 NumBounceSites += F->getNumUses();
63 F->replaceAllUsesWith(FN);
64 changed = true;
65 }
66 }
Reid Spencer688b0492007-02-05 21:19:13 +000067 if (Function* F = M.getFunction("malloc")) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000068 assert(F->isDeclaration() && "malloc not external?");
Andrew Lenharth61e99c92006-04-13 13:43:31 +000069 if (!F->use_empty()) {
Andrew Lenharth34331412006-04-10 19:26:09 +000070 Function* FN = new Function(F->getFunctionType(),
Anton Korobeynikovbed29462007-04-16 18:10:23 +000071 GlobalValue::LinkOnceLinkage,
72 "malloc_llvm_bounce", &M);
Andrew Lenharth34331412006-04-10 19:26:09 +000073 BasicBlock* bb = new BasicBlock("entry",FN);
Reid Spencer7b06bd52006-12-13 00:50:17 +000074 Instruction* c = CastInst::createIntegerCast(
Reid Spencerc5b206b2006-12-31 05:48:39 +000075 FN->arg_begin(), Type::Int32Ty, false, "c", bb);
76 Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
Reid Spencer3ed469c2006-11-02 20:25:50 +000077 new ReturnInst(a, bb);
Andrew Lenharth34331412006-04-10 19:26:09 +000078 ++NumBounce;
79 NumBounceSites += F->getNumUses();
80 F->replaceAllUsesWith(FN);
81 changed = true;
82 }
83 }
84 return changed;
85}
86
87ModulePass *llvm::createIndMemRemPass() {
88 return new IndMemRemPass();
89}