[llvm] r325630 - [MemoryBuiltins] Check nobuiltin status when identifying calls to free.
Philip Reames via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 21 14:20:07 PST 2018
On 02/21/2018 12:44 PM, Sam Clegg via llvm-commits wrote:
> Interestingly it doesn't elide the call if I return a non-const value.
> If I write `return &n+s;` instead it works as expected. So it seems
> as if it is assuming that because new returns a const value it can
> elide it, despite the fact that new contains other side effects (in
> this case the printf statement).
Probably not the actual reasoning, just FYI. If we can show we
returning a value derived from an alloca, that's UB. I'd phrase this as
simply a missed optimization. (i.e. an offset from the alloca is still,
by assumption, within the alloca and thus UB)
>
> On Wed, Feb 21, 2018 at 12:29 PM, Sam Clegg <sbc at google.com> wrote:
>> On Wed, Feb 21, 2018 at 2:46 AM, Benjamin Kramer <benny.kra at gmail.com> wrote:
>>> I'm relatively certain that the test is bad wrt the C++ standard. The
>>> compiler is allowed to elide new even if it is user-defined. The standard's
>>> escape hatch is calling "::operator new(size)" instead of using a new
>>> expression. Building with -fno-builtin should also work. Or feed the pointer
>>> into an __asm__ barrier to prevent the compiler from removing it.
>>>
>> Interesting. Why is it allowed to elide the call to new? I would have
>> thought that since the result of the new is used (its printed) the
>> call can't be elided. But I don't know the spec so if you are sure
>> this is allowed I can simply disable the tests that use this pattern.
>>
>> Would you have expected the change in question to have caused this to
>> start failing?
>>
>>
>>> On Wed, Feb 21, 2018 at 2:49 AM Sam Clegg <sbc at google.com> wrote:
>>>> We have some test code on the WebAssembly waterfall the broke as a
>>>> result of this. Perhaps the test is bad but I've distilled to the
>>>> following:
>>>>
>>>> ```
>>>> #include <stdio.h>
>>>> #include <stdlib.h>
>>>>
>>>> void* operator new(size_t s) {
>>>> static int n = 1;
>>>> printf("new %zu\n", s);
>>>> return &n;
>>>> }
>>>>
>>>> int main() {
>>>> int* foo = new int;
>>>> printf("%p\n", foo);
>>>> return 0;
>>>> }
>>>> ```
>>>>
>>>> Before this change the above program will always print "new ...".
>>>> After this change it only prints this O0. In O1 and O2 the call to
>>>> new is elided. Is this intended?
>>>>
>>>> On Tue, Feb 20, 2018 at 2:00 PM, Benjamin Kramer via llvm-commits
>>>> <llvm-commits at lists.llvm.org> wrote:
>>>>> Author: d0k
>>>>> Date: Tue Feb 20 14:00:33 2018
>>>>> New Revision: 325630
>>>>>
>>>>> URL: http://llvm.org/viewvc/llvm-project?rev=325630&view=rev
>>>>> Log:
>>>>> [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.
>>>>>
>>>>> Modified:
>>>>> llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
>>>>> llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll
>>>>>
>>>>> Modified: llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryBuiltins.cpp?rev=325630&r1=325629&r2=325630&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/Analysis/MemoryBuiltins.cpp (original)
>>>>> +++ llvm/trunk/lib/Analysis/MemoryBuiltins.cpp Tue Feb 20 14:00:33 2018
>>>>> @@ -112,10 +112,9 @@ static const Function *getCalledFunction
>>>>>
>>>>> 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(
>>>>>
>>>>> /// 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 V
>>>>> if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
>>>>> return nullptr;
>>>>>
>>>>> - return CI;
>>>>> + return dyn_cast<CallInst>(I);
>>>>> }
>>>>>
>>>>>
>>>>> //===----------------------------------------------------------------------===//
>>>>>
>>>>> Modified: llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll?rev=325630&r1=325629&r2=325630&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll
>>>>> (original)
>>>>> +++ llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll Tue Feb
>>>>> 20 14:00:33 2018
>>>>> @@ -146,7 +146,11 @@ lpad.i:
>>>>> }
>>>>>
>>>>> 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 at 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
>>>>> +}
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> llvm-commits mailing list
>>>>> llvm-commits at lists.llvm.org
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list