diff options
author | Victor Hernandez <vhernandez@apple.com> | 2009-10-17 00:00:19 +0000 |
---|---|---|
committer | Victor Hernandez <vhernandez@apple.com> | 2009-10-17 00:00:19 +0000 |
commit | 13ad5aaaff8a446758b402fd5e9aea22f5bc5682 (patch) | |
tree | 02a18132aa0c90ad96e5aafeaea17439bb9cb5c2 /lib/VMCore/Instructions.cpp | |
parent | 8db42d2b1ce05e69b67f37c4e56a0f987a0c01d7 (diff) |
Autoupgrade malloc insts to malloc calls.
Update testcases that rely on malloc insts being present.
Also prematurely remove MallocInst handling from IndMemRemoval and RaiseAllocations to help pass tests in this incremental step.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84292 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/Instructions.cpp')
-rw-r--r-- | lib/VMCore/Instructions.cpp | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index f3d15cb2b8..4ff253a7db 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -462,7 +462,8 @@ static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) { static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd, const Type *IntPtrTy, const Type *AllocTy, - Value *ArraySize, const Twine &NameStr) { + Value *ArraySize, Function* MallocF, + const Twine &NameStr) { assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && "createMalloc needs either InsertBefore or InsertAtEnd"); @@ -499,27 +500,34 @@ static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd, BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; Module* M = BB->getParent()->getParent(); const Type *BPTy = Type::getInt8PtrTy(BB->getContext()); - // prototype malloc as "void *malloc(size_t)" - Constant *MallocF = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); - if (!cast<Function>(MallocF)->doesNotAlias(0)) - cast<Function>(MallocF)->setDoesNotAlias(0); + if (!MallocF) + // prototype malloc as "void *malloc(size_t)" + MallocF = cast<Function>(M->getOrInsertFunction("malloc", BPTy, + IntPtrTy, NULL)); + if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0); const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); CallInst *MCall = NULL; - Value *MCast = NULL; + Value *Result = NULL; if (InsertBefore) { MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore); - // Create a cast instruction to convert to the right type... - MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore); + Result = MCall; + if (Result->getType() != AllocPtrType) + // Create a cast instruction to convert to the right type... + Result = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore); } else { - MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertAtEnd); - // Create a cast instruction to convert to the right type... - MCast = new BitCastInst(MCall, AllocPtrType, NameStr); + MCall = CallInst::Create(MallocF, AllocSize, "malloccall"); + Result = MCall; + if (Result->getType() != AllocPtrType) { + InsertAtEnd->getInstList().push_back(MCall); + // Create a cast instruction to convert to the right type... + Result = new BitCastInst(MCall, AllocPtrType, NameStr); + } } MCall->setTailCall(); assert(MCall->getType() != Type::getVoidTy(BB->getContext()) && "Malloc has void return type"); - return MCast; + return Result; } /// CreateMalloc - Generate the IR for a call to malloc: @@ -531,7 +539,8 @@ static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd, Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy, const Type *AllocTy, Value *ArraySize, const Twine &Name) { - return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, ArraySize, Name); + return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, + ArraySize, NULL, Name); } /// CreateMalloc - Generate the IR for a call to malloc: @@ -544,8 +553,9 @@ Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy, /// responsibility of the caller. Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy, const Type *AllocTy, Value *ArraySize, - const Twine &Name) { - return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, ArraySize, Name); + Function* MallocF, const Twine &Name) { + return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, + ArraySize, MallocF, Name); } //===----------------------------------------------------------------------===// |