-//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
+//===- 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 is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
STATISTIC(NumRaised, "Number of allocations raised");
namespace {
- // RaiseAllocations - Turn %malloc and %free calls into the appropriate
+ // RaiseAllocations - Turn @malloc and @free calls into the appropriate
// instruction.
//
class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
Function *MallocFunc; // Functions in the module we are processing
Function *FreeFunc; // Initialized by doPassInitializationVirt
public:
- RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
+ static char ID; // Pass identification, replacement for typeid
+ RaiseAllocations()
+ : ModulePass((intptr_t)&ID), MallocFunc(0), FreeFunc(0) {}
// doPassInitialization - For the raise allocations pass, this finds a
// declaration for malloc and free if they exist.
bool runOnModule(Module &M);
};
+ char RaiseAllocations::ID = 0;
RegisterPass<RaiseAllocations>
X("raiseallocs", "Raise allocations from calls to instructions");
} // end anonymous namespace
// free functions. If this is the case, grab the method pointers that the
// module is using.
//
-// Lookup %malloc and %free in the symbol table, for later use. If they don't
+// Lookup @malloc and @free in the symbol table, for later use. If they don't
// exist, or are not external, we do not worry about converting calls to that
// function into the appropriate instruction.
//
// Get the expected prototype for malloc
const FunctionType *Malloc1Type =
- FunctionType::get(PointerType::get(Type::Int8Ty),
+ FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
std::vector<const Type*>(1, Type::Int64Ty), false);
// Chck to see if we got the expected malloc
// Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
// This handles the common declaration of: 'void *malloc(unsigned);'
const FunctionType *Malloc2Type =
- FunctionType::get(PointerType::get(Type::Int8Ty),
+ FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
std::vector<const Type*>(1, Type::Int32Ty), false);
if (TyWeHave != Malloc2Type) {
// Check to see if the prototype is missing, giving us
// sbyte*(...) * malloc
// This handles the common declaration of: 'void *malloc();'
const FunctionType *Malloc3Type =
- FunctionType::get(PointerType::get(Type::Int8Ty),
+ FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
std::vector<const Type*>(), true);
if (TyWeHave != Malloc3Type)
// Give up
// Get the expected prototype for void free(i8*)
const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
- std::vector<const Type*>(1, PointerType::get(Type::Int8Ty)), false);
+ std::vector<const Type*>(1, PointerType::getUnqual(Type::Int8Ty)), false);
if (TyWeHave != Free1Type) {
// Check to see if the prototype was forgotten, giving us
if (Instruction *I = dyn_cast<Instruction>(U)) {
CallSite CS = CallSite::get(I);
- if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
+ if (CS.getInstruction() && !CS.arg_empty() &&
(CS.getCalledFunction() == MallocFunc ||
std::find(EqPointers.begin(), EqPointers.end(),
CS.getCalledValue()) != EqPointers.end())) {
// If the old instruction was an invoke, add an unconditional branch
// before the invoke, which will become the new terminator.
if (InvokeInst *II = dyn_cast<InvokeInst>(I))
- new BranchInst(II->getNormalDest(), I);
+ BranchInst::Create(II->getNormalDest(), I);
// Delete the old call site
MI->getParent()->getInstList().erase(I);
Users.pop_back();
if (Instruction *I = dyn_cast<Instruction>(U)) {
+ if (isa<InvokeInst>(I))
+ continue;
CallSite CS = CallSite::get(I);
- if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
+ if (CS.getInstruction() && !CS.arg_empty() &&
(CS.getCalledFunction() == FreeFunc ||
std::find(EqPointers.begin(), EqPointers.end(),
CS.getCalledValue()) != EqPointers.end())) {
//
Value *Source = *CS.arg_begin();
if (!isa<PointerType>(Source->getType()))
- Source = new IntToPtrInst(Source, PointerType::get(Type::Int8Ty),
+ Source = new IntToPtrInst(Source,
+ PointerType::getUnqual(Type::Int8Ty),
"FreePtrCast", I);
new FreeInst(Source, I);
// If the old instruction was an invoke, add an unconditional branch
// before the invoke, which will become the new terminator.
if (InvokeInst *II = dyn_cast<InvokeInst>(I))
- new BranchInst(II->getNormalDest(), I);
+ BranchInst::Create(II->getNormalDest(), I);
// Delete the old call site
if (I->getType() != Type::VoidTy)