1 //===- LowerAllocations.cpp - Reduce free insts to calls ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
29 STATISTIC(NumLowered, "Number of allocations lowered");
32 /// LowerAllocations - Turn free instructions into @free calls.
34 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
35 Constant *FreeFunc; // Functions in the module we are processing
36 // Initialized by doInitialization
38 static char ID; // Pass ID, replacement for typeid
39 explicit LowerAllocations()
40 : BasicBlockPass(&ID), FreeFunc(0) {}
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<TargetData>();
46 // This is a cluster of orthogonal Transforms:
47 AU.addPreserved<UnifyFunctionExitNodes>();
48 AU.addPreservedID(PromoteMemoryToRegisterID);
49 AU.addPreservedID(LowerSwitchID);
50 AU.addPreservedID(LowerInvokePassID);
53 /// doPassInitialization - For the lower allocations pass, this ensures that
54 /// a module contains a declaration for a free function.
56 bool doInitialization(Module &M);
58 virtual bool doInitialization(Function &F) {
59 return doInitialization(*F.getParent());
62 /// runOnBasicBlock - This method does the actual work of converting
63 /// instructions over, assuming that the pass has already been initialized.
65 bool runOnBasicBlock(BasicBlock &BB);
69 char LowerAllocations::ID = 0;
70 static RegisterPass<LowerAllocations>
71 X("lowerallocs", "Lower allocations from instructions to calls");
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();
81 // doInitialization - For the lower allocations pass, this ensures that a
82 // module contains a declaration for a free function.
84 // This function is always successful.
86 bool LowerAllocations::doInitialization(Module &M) {
87 const Type *BPTy = Type::getInt8PtrTy(M.getContext());
88 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
93 // runOnBasicBlock - This method does the actual work of converting
94 // instructions over, assuming that the pass has already been initialized.
96 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
98 assert(FreeFunc && "Pass not initialized!");
100 BasicBlock::InstListType &BBIL = BB.getInstList();
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)) {
106 new BitCastInst(FI->getOperand(0),
107 Type::getInt8PtrTy(BB.getContext()), "", I);
109 // Insert a call to the free function...
110 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
112 // Delete the old free instruction