pass machinemoduleinfo down into getSymbolForDwarfGlobalReference,
[llvm/avr.git] / lib / Transforms / Utils / LowerAllocations.cpp
blobcc1f6ea5e50af4d3ed4f5ada0995d9e1e9cfabc0
1 //===- LowerAllocations.cpp - Reduce malloc & 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.
13 //===----------------------------------------------------------------------===//
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;
29 STATISTIC(NumLowered, "Number of allocations lowered");
31 namespace {
32 /// LowerAllocations - Turn malloc and free instructions into @malloc and
33 /// @free calls.
34 ///
35 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
36 Constant *FreeFunc; // Functions in the module we are processing
37 // Initialized by doInitialization
38 bool LowerMallocArgToInteger;
39 public:
40 static char ID; // Pass ID, replacement for typeid
41 explicit LowerAllocations(bool LowerToInt = false)
42 : BasicBlockPass(&ID), FreeFunc(0),
43 LowerMallocArgToInteger(LowerToInt) {}
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<TargetData>();
47 AU.setPreservesCFG();
49 // This is a cluster of orthogonal Transforms:
50 AU.addPreserved<UnifyFunctionExitNodes>();
51 AU.addPreservedID(PromoteMemoryToRegisterID);
52 AU.addPreservedID(LowerSwitchID);
53 AU.addPreservedID(LowerInvokePassID);
56 /// doPassInitialization - For the lower allocations pass, this ensures that
57 /// a module contains a declaration for a malloc and a free function.
58 ///
59 bool doInitialization(Module &M);
61 virtual bool doInitialization(Function &F) {
62 return doInitialization(*F.getParent());
65 /// runOnBasicBlock - This method does the actual work of converting
66 /// instructions over, assuming that the pass has already been initialized.
67 ///
68 bool runOnBasicBlock(BasicBlock &BB);
72 char LowerAllocations::ID = 0;
73 static RegisterPass<LowerAllocations>
74 X("lowerallocs", "Lower allocations from instructions to calls");
76 // Publically exposed interface to pass...
77 const PassInfo *const llvm::LowerAllocationsID = &X;
78 // createLowerAllocationsPass - Interface to this file...
79 Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
80 return new LowerAllocations(LowerMallocArgToInteger);
84 // doInitialization - For the lower allocations pass, this ensures that a
85 // module contains a declaration for a malloc and a free function.
87 // This function is always successful.
89 bool LowerAllocations::doInitialization(Module &M) {
90 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext()));
91 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
92 BPTy, (Type *)0);
93 return true;
96 // runOnBasicBlock - This method does the actual work of converting
97 // instructions over, assuming that the pass has already been initialized.
99 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
100 bool Changed = false;
101 assert(FreeFunc && "Pass not initialized!");
103 BasicBlock::InstListType &BBIL = BB.getInstList();
105 const TargetData &TD = getAnalysis<TargetData>();
106 const Type *IntPtrTy = TD.getIntPtrType(BB.getContext());
108 // Loop over all of the instructions, looking for malloc or free instructions
109 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
110 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
111 Value *MCast = CallInst::CreateMalloc(I, MI->getType(), IntPtrTy,
112 MI->getOperand(0));
114 // Replace all uses of the old malloc inst with the cast inst
115 MI->replaceAllUsesWith(MCast);
116 I = --BBIL.erase(I); // remove and delete the malloc instr...
117 Changed = true;
118 ++NumLowered;
119 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
120 Value *PtrCast =
121 new BitCastInst(FI->getOperand(0),
122 PointerType::getUnqual(Type::getInt8Ty(BB.getContext())), "", I);
124 // Insert a call to the free function...
125 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
127 // Delete the old free instruction
128 I = --BBIL.erase(I);
129 Changed = true;
130 ++NumLowered;
134 return Changed;