diff options
| author | Benjamin Kramer <benny.kra@googlemail.com> | 2018-02-20 22:00:33 +0000 |
|---|---|---|
| committer | Benjamin Kramer <benny.kra@googlemail.com> | 2018-02-20 22:00:33 +0000 |
| commit | fd0630665b141d83a86b40cfd3e49be1dd80e005 (patch) | |
| tree | 7237e4c8ee798bad1d320305b724edbb2ab1fec4 | |
| parent | 3141ddc58d8479f4c4cf5d5d7b524b1e3e1a7ef1 (diff) | |
| download | bcm5719-llvm-fd0630665b141d83a86b40cfd3e49be1dd80e005.tar.gz bcm5719-llvm-fd0630665b141d83a86b40cfd3e49be1dd80e005.zip | |
[MemoryBuiltins] Check nobuiltin status when identifying calls to free.
This is usually not a problem because this code's main purpose is
eliminating unused new/delete pairs. We got deletes of nullptr or
nobuiltin deletes of builtin new wrong though.
llvm-svn: 325630
| -rw-r--r-- | llvm/lib/Analysis/MemoryBuiltins.cpp | 18 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/malloc-free-delete.ll | 22 |
2 files changed, 29 insertions, 11 deletions
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp index 24fedfed772..2aa389e059c 100644 --- a/llvm/lib/Analysis/MemoryBuiltins.cpp +++ b/llvm/lib/Analysis/MemoryBuiltins.cpp @@ -112,10 +112,9 @@ static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast IsNoBuiltin = CS.isNoBuiltin(); - const Function *Callee = CS.getCalledFunction(); - if (!Callee || !Callee->isDeclaration()) - return nullptr; - return Callee; + if (const Function *Callee = CS.getCalledFunction()) + return Callee; + return nullptr; } /// Returns the allocation data for the given value if it's either a call to a @@ -350,11 +349,10 @@ const CallInst *llvm::extractCallocCall(const Value *I, /// isFreeCall - Returns non-null if the value is a call to the builtin free() const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) { - const CallInst *CI = dyn_cast<CallInst>(I); - if (!CI || isa<IntrinsicInst>(CI)) - return nullptr; - Function *Callee = CI->getCalledFunction(); - if (Callee == nullptr) + bool IsNoBuiltinCall; + const Function *Callee = + getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall); + if (Callee == nullptr || IsNoBuiltinCall) return nullptr; StringRef FnName = Callee->getName(); @@ -400,7 +398,7 @@ const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) { if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext())) return nullptr; - return CI; + return dyn_cast<CallInst>(I); } //===----------------------------------------------------------------------===// diff --git a/llvm/test/Transforms/InstCombine/malloc-free-delete.ll b/llvm/test/Transforms/InstCombine/malloc-free-delete.ll index d2cf5d02fe3..77737fbc8f3 100644 --- a/llvm/test/Transforms/InstCombine/malloc-free-delete.ll +++ b/llvm/test/Transforms/InstCombine/malloc-free-delete.ll @@ -146,7 +146,11 @@ lpad.i: ; preds = %entry } declare i8* @_Znwm(i64) nobuiltin -declare i8* @_Znwj(i32) nobuiltin +define i8* @_Znwj(i32 %n) nobuiltin { + %z = zext i32 %n to i64 + call i8* @_Znwm(i64 %z) + ret i8* %m +} declare i8* @_Znam(i64) nobuiltin declare i8* @_Znaj(i32) nobuiltin declare void @_ZdlPv(i8*) nobuiltin @@ -197,3 +201,19 @@ define void @test9() { call void @"\01??3@YAXPEAX@Z"(i8* %new_long_long) builtin ret void } + +define void @test10() { +; CHECK-LABEL: @test10 +; CHECK: call void @_ZdlPv + call void @_ZdlPv(i8* null) + ret void +} + +define void @test11() { +; CHECK-LABEL: @test11 +; CHECK: call i8* @_Znwm +; CHECK: call void @_ZdlPv + %call = call i8* @_Znwm(i64 8) builtin + call void @_ZdlPv(i8* %call) + ret void +} |

