9c9113daa93a4a202a7814e957f5be23af35f483
[oota-llvm.git] / lib / Transforms / Utils / LowerAllocations.cpp
1 //===- LowerAllocations.cpp - Reduce free insts to calls ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The LowerAllocations transformation is a target-dependent tranformation
11 // because it depends on the size of data types and alignment constraints.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "lowerallocs"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
18 #include "llvm/Module.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Constants.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Pass.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Support/Compiler.h"
27 using namespace llvm;
28
29 STATISTIC(NumLowered, "Number of allocations lowered");
30
31 namespace {
32   /// LowerAllocations - Turn free instructions into @free calls.
33   ///
34   class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
35     Constant *FreeFunc;   // Functions in the module we are processing
36                           // Initialized by doInitialization
37   public:
38     static char ID; // Pass ID, replacement for typeid
39     explicit LowerAllocations()
40       : BasicBlockPass(&ID), FreeFunc(0) {}
41
42     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43       AU.addRequired<TargetData>();
44       AU.setPreservesCFG();
45
46       // This is a cluster of orthogonal Transforms:
47       AU.addPreserved<UnifyFunctionExitNodes>();
48       AU.addPreservedID(PromoteMemoryToRegisterID);
49       AU.addPreservedID(LowerSwitchID);
50       AU.addPreservedID(LowerInvokePassID);
51     }
52
53     /// doPassInitialization - For the lower allocations pass, this ensures that
54     /// a module contains a declaration for a free function.
55     ///
56     bool doInitialization(Module &M);
57
58     virtual bool doInitialization(Function &F) {
59       return doInitialization(*F.getParent());
60     }
61
62     /// runOnBasicBlock - This method does the actual work of converting
63     /// instructions over, assuming that the pass has already been initialized.
64     ///
65     bool runOnBasicBlock(BasicBlock &BB);
66   };
67 }
68
69 char LowerAllocations::ID = 0;
70 static RegisterPass<LowerAllocations>
71 X("lowerallocs", "Lower allocations from instructions to calls");
72
73 // Publically exposed interface to pass...
74 const PassInfo *const llvm::LowerAllocationsID = &X;
75 // createLowerAllocationsPass - Interface to this file...
76 Pass *llvm::createLowerAllocationsPass() {
77   return new LowerAllocations();
78 }
79
80
81 // doInitialization - For the lower allocations pass, this ensures that a
82 // module contains a declaration for a free function.
83 //
84 // This function is always successful.
85 //
86 bool LowerAllocations::doInitialization(Module &M) {
87   const Type *BPTy = Type::getInt8PtrTy(M.getContext());
88   FreeFunc = M.getOrInsertFunction("free"  , Type::getVoidTy(M.getContext()),
89                                    BPTy, (Type *)0);
90   return true;
91 }
92
93 // runOnBasicBlock - This method does the actual work of converting
94 // instructions over, assuming that the pass has already been initialized.
95 //
96 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
97   bool Changed = false;
98   assert(FreeFunc && "Pass not initialized!");
99
100   BasicBlock::InstListType &BBIL = BB.getInstList();
101
102   // Loop over all of the instructions, looking for free instructions
103   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
104     if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
105       Value *PtrCast = 
106         new BitCastInst(FI->getOperand(0),
107                Type::getInt8PtrTy(BB.getContext()), "", I);
108
109       // Insert a call to the free function...
110       CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
111
112       // Delete the old free instruction
113       I = --BBIL.erase(I);
114       Changed = true;
115       ++NumLowered;
116     }
117   }
118
119   return Changed;
120 }
121