blob: 502c293da1d8448ea25e8080de89eaad1439715d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
18#define DEBUG_TYPE "indmemrem"
19#include "llvm/Transforms/IPO.h"
20#include "llvm/Pass.h"
21#include "llvm/Module.h"
22#include "llvm/Instructions.h"
23#include "llvm/Type.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/ADT/Statistic.h"
26#include "llvm/Support/Compiler.h"
27using namespace llvm;
28
29STATISTIC(NumBounceSites, "Number of sites modified");
30STATISTIC(NumBounce , "Number of bounce functions created");
31
32namespace {
33 class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
34 public:
35 static char ID; // Pass identification, replacement for typeid
36 IndMemRemPass() : ModulePass((intptr_t)&ID) {}
37
38 virtual bool runOnModule(Module &M);
39 };
40 char IndMemRemPass::ID = 0;
41 RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal");
42} // end anonymous namespace
43
44
45bool 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
50 //happen through intrinsics.
51 bool changed = false;
52 if (Function* F = M.getFunction("free")) {
53 assert(F->isDeclaration() && "free not external?");
54 if (!F->use_empty()) {
55 Function* FN = new Function(F->getFunctionType(),
56 GlobalValue::LinkOnceLinkage,
57 "free_llvm_bounce", &M);
58 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 }
67 if (Function* F = M.getFunction("malloc")) {
68 assert(F->isDeclaration() && "malloc not external?");
69 if (!F->use_empty()) {
70 Function* FN = new Function(F->getFunctionType(),
71 GlobalValue::LinkOnceLinkage,
72 "malloc_llvm_bounce", &M);
73 BasicBlock* bb = new BasicBlock("entry",FN);
74 Instruction* c = CastInst::createIntegerCast(
75 FN->arg_begin(), Type::Int32Ty, false, "c", bb);
76 Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
77 new ReturnInst(a, bb);
78 ++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}