blob: 6b1f969e440e46fadf8722aa8c7e0b4386ee303a [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
Dan Gohmanae73dc12008-09-04 17:05:41 +000036 IndMemRemPass() : ModulePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000037
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) {
Nick Lewycky9d497912009-01-25 07:59:57 +000047 // In theory, all direct calls of malloc and free should be promoted
48 // to intrinsics. Therefore, 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
51 // 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")) {
Nuno Lopesb9baf312008-09-06 17:44:06 +000054 if (F->isDeclaration() && F->arg_size() == 1 && !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")) {
Nuno Lopesb9baf312008-09-06 17:44:06 +000068 if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
Gabor Greif051a9502008-04-06 20:25:17 +000069 Function* FN = Function::Create(F->getFunctionType(),
70 GlobalValue::LinkOnceLinkage,
71 "malloc_llvm_bounce", &M);
Nick Lewycky9d497912009-01-25 07:59:57 +000072 FN->setDoesNotAlias(0);
Gabor Greif051a9502008-04-06 20:25:17 +000073 BasicBlock* bb = BasicBlock::Create("entry",FN);
Gabor Greif7cbd8a32008-05-16 19:29:10 +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}