blob: 2f9c34d5cf6f7309dec8b357e2eca9b119b54ba7 [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 };
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()) {
Gabor Greif051a9502008-04-06 20:25:17 +000055 Function* FN = Function::Create(F->getFunctionType(),
56 GlobalValue::LinkOnceLinkage,
57 "free_llvm_bounce", &M);
58 BasicBlock* bb = BasicBlock::Create("entry",FN);
59 Instruction* R = ReturnInst::Create(bb);
Andrew Lenharth34331412006-04-10 19:26:09 +000060 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()) {
Gabor Greif051a9502008-04-06 20:25:17 +000070 Function* FN = Function::Create(F->getFunctionType(),
71 GlobalValue::LinkOnceLinkage,
72 "malloc_llvm_bounce", &M);
73 BasicBlock* bb = BasicBlock::Create("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);
Gabor Greif051a9502008-04-06 20:25:17 +000077 ReturnInst::Create(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}