aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore/Instructions.cpp
diff options
context:
space:
mode:
authorEvan Cheng <evan.cheng@apple.com>2009-09-10 04:36:43 +0000
committerEvan Cheng <evan.cheng@apple.com>2009-09-10 04:36:43 +0000
commite5406467385df42184feed2f03fbdab5ac38c9f5 (patch)
treeb08e3c11348110692e65483dafca45b0489b12d4 /lib/VMCore/Instructions.cpp
parent5c6e5ae3c8ec8cc5df9f82b4c51d67105e941e5f (diff)
downloadexternal_llvm-e5406467385df42184feed2f03fbdab5ac38c9f5.zip
external_llvm-e5406467385df42184feed2f03fbdab5ac38c9f5.tar.gz
external_llvm-e5406467385df42184feed2f03fbdab5ac38c9f5.tar.bz2
Add malloc call utility functions. Patch by Victor Hernandez.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81426 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/Instructions.cpp')
-rw-r--r--lib/VMCore/Instructions.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp
index 9d8e047..a035871 100644
--- a/lib/VMCore/Instructions.cpp
+++ b/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