//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file defines the RaiseAllocations pass which convert malloc and free
// RaiseAllocations - Turn %malloc and %free calls into the appropriate
// instruction.
//
- class RaiseAllocations : public Pass {
+ class RaiseAllocations : public ModulePass {
Function *MallocFunc; // Functions in the module we are processing
Function *FreeFunc; // Initialized by doPassInitializationVirt
public:
RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
-
+
// doPassInitialization - For the raise allocations pass, this finds a
// declaration for malloc and free if they exist.
//
void doInitialization(Module &M);
-
+
// run - This method does the actual work of converting instructions over.
//
- bool run(Module &M);
+ bool runOnModule(Module &M);
};
-
+
RegisterOpt<RaiseAllocations>
X("raiseallocs", "Raise allocations from calls to instructions");
} // end anonymous namespace
// createRaiseAllocationsPass - The interface to this file...
-Pass *llvm::createRaiseAllocationsPass() {
+ModulePass *llvm::createRaiseAllocationsPass() {
return new RaiseAllocations();
}
// run - Transform calls into instructions...
//
-bool RaiseAllocations::run(Module &M) {
+bool RaiseAllocations::runOnModule(Module &M) {
// Find the malloc/free prototypes...
doInitialization(M);
(CS.getCalledFunction() == MallocFunc ||
std::find(EqPointers.begin(), EqPointers.end(),
CS.getCalledValue()) != EqPointers.end())) {
-
+
Value *Source = *CS.arg_begin();
-
+
// If no prototype was provided for malloc, we may need to cast the
// source size.
if (Source->getType() != Type::UIntTy)
Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
-
+
std::string Name(I->getName()); I->setName("");
MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
I->replaceAllUsesWith(MI);
(CS.getCalledFunction() == FreeFunc ||
std::find(EqPointers.begin(), EqPointers.end(),
CS.getCalledValue()) != EqPointers.end())) {
-
+
// If no prototype was provided for free, we may need to cast the
// source pointer. This should be really uncommon, but it's necessary
- // just in case we are dealing with wierd code like this:
+ // just in case we are dealing with weird code like this:
// free((long)ptr);
//
Value *Source = *CS.arg_begin();
new BranchInst(II->getNormalDest(), I);
// Delete the old call site
- I->getParent()->getInstList().erase(I);
+ if (I->getType() != Type::VoidTy)
+ I->replaceAllUsesWith(UndefValue::get(I->getType()));
+ I->eraseFromParent();
Changed = true;
++NumRaised;
}