From 240d42d18559a1f75558951ada556baaf289c7f0 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 6 Jan 2011 13:11:05 +0000 Subject: [PATCH] InstCombine: If we call llvm.objectsize on a malloc call we can replace it with the size passed to malloc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122959 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/README.txt | 23 ------------------- .../InstCombine/InstCombineCalls.cpp | 6 ++++- test/Transforms/InstCombine/objsize.ll | 16 +++++++++++++ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/Target/README.txt b/lib/Target/README.txt index 8ed8c598dde..d83cd8bb963 100644 --- a/lib/Target/README.txt +++ b/lib/Target/README.txt @@ -2020,29 +2020,6 @@ define i1 @g(i32 a) nounwind readnone { //===---------------------------------------------------------------------===// -This code can be seen in viterbi: - - %64 = call noalias i8* @malloc(i64 %62) nounwind -... - %67 = call i64 @llvm.objectsize.i64(i8* %64, i1 false) nounwind - %68 = call i8* @__memset_chk(i8* %64, i32 0, i64 %62, i64 %67) nounwind - -llvm.objectsize.i64 should be taught about malloc/calloc, allowing it to -fold to %62. This is a security win (overflows of malloc will get caught) -and also a performance win by exposing more memsets to the optimizer. - -This occurs several times in viterbi. - -Stuff like this occurs in drystone: - - %call5 = call i8* @malloc(i32 48) optsize - %5 = getelementptr inbounds i8* %call5, i32 16 - %6 = call i32 @llvm.objectsize.i32(i8* %5, i1 false) - -We should be able to constant fold that. - -//===---------------------------------------------------------------------===// - This code (from Benchmarks/Dhrystone/dry.c): define i32 @Func1(i32, i32) nounwind readnone optsize ssp { diff --git a/lib/Transforms/InstCombine/InstCombineCalls.cpp b/lib/Transforms/InstCombine/InstCombineCalls.cpp index 7068cb6669e..603a01075a5 100644 --- a/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -298,12 +298,16 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { } } } else if (CallInst *MI = extractMallocCall(Op1)) { - // Get alloca size. + // Get allocation size. const Type* MallocType = getMallocAllocatedType(MI); if (MallocType && MallocType->isSized()) if (Value *NElems = getMallocArraySize(MI, TD, true)) if (ConstantInt *NElements = dyn_cast(NElems)) Size = NElements->getZExtValue() * TD->getTypeAllocSize(MallocType); + + // If there is no offset we can just return the size passed to malloc. + if (Offset == 0) + return ReplaceInstUsesWith(CI, MI->getArgOperand(0)); } // Do not return "I don't know" here. Later optimization passes could diff --git a/test/Transforms/InstCombine/objsize.ll b/test/Transforms/InstCombine/objsize.ll index 043525b7555..9950e5f4cc6 100644 --- a/test/Transforms/InstCombine/objsize.ll +++ b/test/Transforms/InstCombine/objsize.ll @@ -160,3 +160,19 @@ define i32 @test7() { ret i32 %objsize } +define i32 @test8(i32 %x) { +; CHECK: @test8 + %alloc = call noalias i8* @malloc(i32 %x) nounwind + %objsize = call i32 @llvm.objectsize.i32(i8* %alloc, i1 false) nounwind readonly +; CHECK-NEXT: ret i32 %x + ret i32 %objsize +} + +define i32 @test9(i32 %x) { +; CHECK: @test9 + %alloc = call noalias i8* @malloc(i32 %x) nounwind + %gep = getelementptr inbounds i8* %alloc, i32 16 + %objsize = call i32 @llvm.objectsize.i32(i8* %gep, i1 false) nounwind readonly +; CHECK-NOT: ret i32 %x + ret i32 %objsize +}