summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvan Cheng <evan.cheng@apple.com>2010-03-08 22:54:36 +0000
committerEvan Cheng <evan.cheng@apple.com>2010-03-08 22:54:36 +0000
commit4f2fd2d2be6a14f164bc5f7fbd9cdb4c9d615e08 (patch)
treed6849e8b42617b5a59a3f83a7d494b4f3fd0dfe0
parentee20b5f2362aa45d5cf2446ee76c80a640ad5839 (diff)
downloadbcm5719-llvm-4f2fd2d2be6a14f164bc5f7fbd9cdb4c9d615e08.tar.gz
bcm5719-llvm-4f2fd2d2be6a14f164bc5f7fbd9cdb4c9d615e08.zip
Re-commit 97860 with fix. getMallocAllocatedType may return null.
llvm-svn: 98000
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp10
-rw-r--r--llvm/test/Transforms/InstCombine/objsize.ll16
2 files changed, 26 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index e2b7d3d60db..0582210d407 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -337,6 +337,16 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
}
return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, AllocaSize));
}
+ } else if (CallInst *MI = extractMallocCall(Op1)) {
+ const Type* MallocType = getMallocAllocatedType(MI);
+ // Get alloca size.
+ if (MallocType && MallocType->isSized()) {
+ if (Value *NElems = getMallocArraySize(MI, TD, true)) {
+ if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
+ return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy,
+ (NElements->getZExtValue() * TD->getTypeAllocSize(MallocType))));
+ }
+ }
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) {
// Only handle constant GEPs here.
if (CE->getOpcode() != Instruction::GetElementPtr) break;
diff --git a/llvm/test/Transforms/InstCombine/objsize.ll b/llvm/test/Transforms/InstCombine/objsize.ll
index 57dc2fd0544..cd7b7c80679 100644
--- a/llvm/test/Transforms/InstCombine/objsize.ll
+++ b/llvm/test/Transforms/InstCombine/objsize.ll
@@ -118,8 +118,24 @@ entry:
ret i32 0
}
+@s = external global i8*
+
+define void @test5(i32 %n) nounwind ssp {
+; CHECK: @test5
+entry:
+ %0 = tail call noalias i8* @malloc(i32 20) nounwind
+ %1 = tail call i32 @llvm.objectsize.i32(i8* %0, i1 false)
+ %2 = load i8** @s, align 8
+; CHECK-NOT: @llvm.objectsize
+; CHECK: @__memcpy_chk(i8* %0, i8* %1, i32 10, i32 20)
+ %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 10, i32 %1) nounwind
+ ret void
+}
+
declare i8* @__memset_chk(i8*, i32, i64, i64) nounwind
+declare noalias i8* @malloc(i32) nounwind
+
declare i32 @llvm.objectsize.i32(i8*, i1) nounwind readonly
declare i64 @llvm.objectsize.i64(i8*, i1) nounwind readonly
OpenPOWER on IntegriCloud