diff options
Diffstat (limited to 'llvm/lib/VMCore/Instructions.cpp')
-rw-r--r-- | llvm/lib/VMCore/Instructions.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/Instructions.cpp b/llvm/lib/VMCore/Instructions.cpp index 9d8e0477eed..a03587107cc 100644 --- a/llvm/lib/VMCore/Instructions.cpp +++ b/llvm/lib/VMCore/Instructions.cpp @@ -16,6 +16,7 @@ #include "llvm/DerivedTypes.h" #include "llvm/Function.h" #include "llvm/Instructions.h" +#include "llvm/Module.h" #include "llvm/Operator.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Support/ErrorHandling.h" @@ -440,6 +441,116 @@ bool CallInst::paramHasAttr(unsigned i, Attributes attr) const { return false; } +/// IsConstantOne - Return true only if val is constant int 1 +static bool IsConstantOne(Value *val) { + assert(val && "IsConstantOne does not work with NULL val"); + return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); +} + +static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) { + if (!Amt) + Amt = ConstantInt::get(IntPtrTy, 1); + else { + assert(!isa<BasicBlock>(Amt) && + "Passed basic block into malloc size parameter! Use other ctor"); + assert(Amt->getType() == IntPtrTy && + "Malloc array size is not an intptr!"); + } + return Amt; +} + +static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd, + const Type *AllocTy, const Type *IntPtrTy, + Value *ArraySize, const Twine &NameStr) { + assert((!InsertBefore && InsertAtEnd || InsertBefore && !InsertAtEnd) && + "createMalloc needs only InsertBefore or InsertAtEnd"); + const PointerType *AllocPtrType = dyn_cast<PointerType>(AllocTy); + assert(AllocPtrType && "CreateMalloc passed a non-pointer allocation type"); + + ArraySize = checkArraySize(ArraySize, IntPtrTy); + + // malloc(type) becomes i8 *malloc(size) + Value *AllocSize = ConstantExpr::getSizeOf(AllocPtrType->getElementType()); + AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize), + IntPtrTy); + if (!IsConstantOne(ArraySize)) + if (IsConstantOne(AllocSize)) { + AllocSize = ArraySize; // Operand * 1 = Operand + } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { + Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, + false /*ZExt*/); + // Malloc arg is constant product of type size and array size + AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); + } else { + Value *Scale = ArraySize; + if (Scale->getType() != IntPtrTy) + if (InsertBefore) + Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/, + "", InsertBefore); + else + Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/, + "", InsertAtEnd); + // Multiply type size by the array size... + if (InsertBefore) + AllocSize = BinaryOperator::CreateMul(Scale, AllocSize, + "", InsertBefore); + else + AllocSize = BinaryOperator::CreateMul(Scale, AllocSize, + "", InsertAtEnd); + } + + // Create the call to Malloc. + BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; + Module* M = BB->getParent()->getParent(); + const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext())); + // prototype malloc as "void *malloc(size_t)" + Constant *MallocFunc = M->getOrInsertFunction("malloc", BPTy, + IntPtrTy, NULL); + CallInst *MCall = NULL; + if (InsertBefore) + MCall = CallInst::Create(MallocFunc, AllocSize, NameStr, InsertBefore); + else + MCall = CallInst::Create(MallocFunc, AllocSize, NameStr, InsertAtEnd); + MCall->setTailCall(); + + // Create a cast instruction to convert to the right type... + const Type* VoidT = Type::getVoidTy(BB->getContext()); + assert(MCall->getType() != VoidT && "Malloc has void return type"); + Value *MCast; + if (InsertBefore) + MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore); + else + MCast = new BitCastInst(MCall, AllocPtrType, NameStr); + return MCast; +} + +/// CreateMalloc - Generate the IR for a call to malloc: +/// 1. Compute the malloc call's argument as the specified type's size, +/// possibly multiplied by the array size if the array size is not +/// constant 1. +/// 2. Call malloc with that argument. +/// 3. Bitcast the result of the malloc call to the specified type. +Value *CallInst::CreateMalloc(Instruction *InsertBefore, + const Type *AllocTy, const Type *IntPtrTy, + Value *ArraySize, const Twine &NameStr) { + return createMalloc(InsertBefore, NULL, AllocTy, + IntPtrTy, ArraySize, NameStr); +} + +/// CreateMalloc - Generate the IR for a call to malloc: +/// 1. Compute the malloc call's argument as the specified type's size, +/// possibly multiplied by the array size if the array size is not +/// constant 1. +/// 2. Call malloc with that argument. +/// 3. Bitcast the result of the malloc call to the specified type. +/// Note: This function does not add the bitcast to the basic block, that is the +/// responsibility of the caller. +Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, + const Type *AllocTy, const Type *IntPtrTy, + Value *ArraySize, const Twine &NameStr) { + return createMalloc(NULL, InsertAtEnd, AllocTy, + IntPtrTy, ArraySize, NameStr); +} //===----------------------------------------------------------------------===// // InvokeInst Implementation |