[llvm-commits] [llvm] r156102 - in /llvm/trunk: include/llvm/Analysis/MemoryBuiltins.h lib/Analysis/MemoryBuiltins.cpp lib/Transforms/InstCombine/InstCombineCalls.cpp test/Transforms/InstCombine/objsize.ll
Nuno Lopes
nunoplopes at sapo.pt
Thu May 3 14:19:58 PDT 2012
Author: nlopes
Date: Thu May 3 16:19:58 2012
New Revision: 156102
URL: http://llvm.org/viewvc/llvm-project?rev=156102&view=rev
Log:
add support for calloc to objectsize lowering
Modified:
llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h
llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/trunk/test/Transforms/InstCombine/objsize.ll
Modified: llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h?rev=156102&r1=156101&r2=156102&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h (original)
+++ llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h Thu May 3 16:19:58 2012
@@ -68,6 +68,17 @@
Value *getMallocArraySize(CallInst *CI, const TargetData *TD,
bool LookThroughSExt = false);
+
+//===----------------------------------------------------------------------===//
+// calloc Call Utility Functions.
+//
+
+/// extractCallocCall - Returns the corresponding CallInst if the instruction
+/// is a calloc call.
+const CallInst *extractCallocCall(const Value *I);
+CallInst *extractCallocCall(Value *I);
+
+
//===----------------------------------------------------------------------===//
// free Call Utility Functions.
//
Modified: llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryBuiltins.cpp?rev=156102&r1=156101&r2=156102&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemoryBuiltins.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryBuiltins.cpp Thu May 3 16:19:58 2012
@@ -180,6 +180,46 @@
return computeArraySize(CI, TD, LookThroughSExt);
}
+
+//===----------------------------------------------------------------------===//
+// clloc Call Utility Functions.
+//
+
+static bool isCallocCall(const CallInst *CI) {
+ if (!CI)
+ return false;
+
+ Function *Callee = CI->getCalledFunction();
+ if (Callee == 0 || !Callee->isDeclaration())
+ return false;
+ if (Callee->getName() != "calloc")
+ return false;
+
+ // Check malloc prototype.
+ // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
+ // attribute will exist.
+ FunctionType *FTy = Callee->getFunctionType();
+ return FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
+ FTy->getNumParams() == 2 &&
+ ((FTy->getParamType(0)->isIntegerTy(32) &&
+ FTy->getParamType(1)->isIntegerTy(32)) ||
+ (FTy->getParamType(0)->isIntegerTy(64) &&
+ FTy->getParamType(1)->isIntegerTy(64)));
+}
+
+/// extractCallocCall - Returns the corresponding CallInst if the instruction
+/// is a calloc call.
+const CallInst *llvm::extractCallocCall(const Value *I) {
+ const CallInst *CI = dyn_cast<CallInst>(I);
+ return isCallocCall(CI) ? CI : 0;
+}
+
+CallInst *llvm::extractCallocCall(Value *I) {
+ CallInst *CI = dyn_cast<CallInst>(I);
+ return isCallocCall(CI) ? CI : 0;
+}
+
+
//===----------------------------------------------------------------------===//
// free Call Utility Functions.
//
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=156102&r1=156101&r2=156102&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Thu May 3 16:19:58 2012
@@ -300,11 +300,23 @@
}
} else if (CallInst *MI = extractMallocCall(Op1)) {
// Get allocation size.
- Type* MallocType = getMallocAllocatedType(MI);
- if (MallocType && MallocType->isSized())
- if (Value *NElems = getMallocArraySize(MI, TD, true))
- if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
- Size = NElements->getZExtValue() * TD->getTypeAllocSize(MallocType);
+ Value *Arg = MI->getArgOperand(0);
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
+ Size = CI->getZExtValue();
+
+ } else if (CallInst *MI = extractCallocCall(Op1)) {
+ // Get allocation size.
+ Value *Arg1 = MI->getArgOperand(0);
+ Value *Arg2 = MI->getArgOperand(1);
+ if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Arg1))
+ if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Arg2)) {
+ bool overflow;
+ APInt SizeAP = CI1->getValue().umul_ov(CI2->getValue(), overflow);
+ if (!overflow)
+ Size = SizeAP.getZExtValue();
+ else
+ return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, DontKnow));
+ }
}
// Do not return "I don't know" here. Later optimization passes could
Modified: llvm/trunk/test/Transforms/InstCombine/objsize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/objsize.ll?rev=156102&r1=156101&r2=156102&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/objsize.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/objsize.ll Thu May 3 16:19:58 2012
@@ -158,3 +158,23 @@
ret i32 %objsize
}
+declare noalias i8* @calloc(i32, i32) nounwind
+
+define i32 @test8() {
+; CHECK: @test8
+ %alloc = call noalias i8* @calloc(i32 5, i32 7) nounwind
+ %gep = getelementptr inbounds i8* %alloc, i32 5
+ %objsize = call i32 @llvm.objectsize.i32(i8* %gep, i1 false) nounwind readonly
+; CHECK: ret i32 30
+ ret i32 %objsize
+}
+
+; test for overflow in calloc
+define i32 @test9() {
+; CHECK: @test9
+ %alloc = call noalias i8* @calloc(i32 100000000, i32 100000000) nounwind
+ %gep = getelementptr inbounds i8* %alloc, i32 2
+ %objsize = call i32 @llvm.objectsize.i32(i8* %gep, i1 true) nounwind readonly
+; CHECK: ret i32 0
+ ret i32 %objsize
+}
More information about the llvm-commits
mailing list