Hyphenate `target-dependent'
[oota-llvm.git] / lib / Transforms / Utils / LowerAllocations.cpp
1 //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
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 // 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 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/iMemory.h"
19 #include "llvm/iOther.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Target/TargetData.h"
23 #include "Support/Statistic.h"
24 using namespace llvm;
25
26 namespace {
27   Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
28
29   /// LowerAllocations - Turn malloc and free instructions into %malloc and
30   /// %free calls.
31   ///
32   class LowerAllocations : public BasicBlockPass {
33     Function *MallocFunc;   // Functions in the module we are processing
34     Function *FreeFunc;     // Initialized by doInitialization
35   public:
36     LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
37
38     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39       AU.addRequired<TargetData>();
40     }
41
42     /// doPassInitialization - For the lower allocations pass, this ensures that
43     /// a module contains a declaration for a malloc and a free function.
44     ///
45     bool doInitialization(Module &M);
46     
47     /// runOnBasicBlock - This method does the actual work of converting
48     /// instructions over, assuming that the pass has already been initialized.
49     ///
50     bool runOnBasicBlock(BasicBlock &BB);
51   };
52
53   RegisterOpt<LowerAllocations>
54   X("lowerallocs", "Lower allocations from instructions to calls");
55 }
56
57 // createLowerAllocationsPass - Interface to this file...
58 FunctionPass *llvm::createLowerAllocationsPass() {
59   return new LowerAllocations();
60 }
61
62
63 // doInitialization - For the lower allocations pass, this ensures that a
64 // module contains a declaration for a malloc and a free function.
65 //
66 // This function is always successful.
67 //
68 bool LowerAllocations::doInitialization(Module &M) {
69   const Type *SBPTy = PointerType::get(Type::SByteTy);
70   MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
71   FreeFunc   = M.getOrInsertFunction("free"  , Type::VoidTy, SBPTy, 0);
72
73   return true;
74 }
75
76 // runOnBasicBlock - This method does the actual work of converting
77 // instructions over, assuming that the pass has already been initialized.
78 //
79 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
80   bool Changed = false;
81   assert(MallocFunc && FreeFunc && "Pass not initialized!");
82
83   BasicBlock::InstListType &BBIL = BB.getInstList();
84   TargetData &DataLayout = getAnalysis<TargetData>();
85
86   // Loop over all of the instructions, looking for malloc or free instructions
87   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
88     if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
89       const Type *AllocTy = MI->getType()->getElementType();
90       
91       // Get the number of bytes to be allocated for one element of the
92       // requested type...
93       unsigned Size = DataLayout.getTypeSize(AllocTy);
94       
95       // malloc(type) becomes sbyte *malloc(constint)
96       Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
97       if (MI->getNumOperands() && Size == 1) {
98         MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
99       } else if (MI->getNumOperands()) {
100         // Multiply it by the array size if necessary...
101         MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
102                                            MallocArg, "", I);
103       }
104       
105       // Create the call to Malloc...
106       CallInst *MCall = new CallInst(MallocFunc,
107                                      std::vector<Value*>(1, MallocArg), "", I);
108       
109       // Create a cast instruction to convert to the right type...
110       CastInst *MCast = new CastInst(MCall, MI->getType(), "", I);
111       
112       // Replace all uses of the old malloc inst with the cast inst
113       MI->replaceAllUsesWith(MCast);
114       I = --BBIL.erase(I);         // remove and delete the malloc instr...
115       Changed = true;
116       ++NumLowered;
117     } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
118       // Cast the argument to free into a ubyte*...
119       CastInst *MCast = new CastInst(FI->getOperand(0), 
120                                      PointerType::get(Type::SByteTy), "", I);
121       
122       // Insert a call to the free function...
123       CallInst *FCall = new CallInst(FreeFunc, std::vector<Value*>(1, MCast),
124                                      "", I);
125       
126       // Delete the old free instruction
127       I = --BBIL.erase(I);
128       Changed = true;
129       ++NumLowered;
130     }
131   }
132
133   return Changed;
134 }
135