llvm return value propagation & asm

Hi, I'm trying to have a pure asm function (non inlined) that returns it's own value to the caller.

; Function Attrs: naked noinline optnone
define i32 @callcatch(i32, i32) #3 !dbg !10103 {
BasicBlock8472:
   call void asm "\0D\0Apushl %ebp\0D\0Amovl 8(%esp),%eax\0D\0Amovl 12(%esp), %ebp\0D\0Acalll *%eax\0D\0Apopl %ebp\0D\0Aretl\0D\0A", ""(), !dbg !10104, !srcloc !10106 // this returns in eax
   ret i32 0, !dbg !10104
}

; Function Attrs: naked noinline optnone
define void @jumptocont(i32, i32, i32) #3 !dbg !10107 {
BasicBlock8473:
   call void asm "\0D\0A movl 12(%esp), %ebp\0D\0A movl 4(%esp), %eax\0D\0A movl 8(%esp), %esp\0D\0A jmpl *%eax\0D\0A", ""(), !dbg !10108, !srcloc !10110
   ret void, !dbg !10108
}

The calling code is like;
   %530 = call i32 @callcatch(i32 %528, i32 %529) #3, !dbg !7648
   store i32 %530, i32* %20, !dbg !7648
   %531 = load i32, i32* %20, !dbg !7630
...
   call void @jumptocont(i32 %531, i32 %532, i32 %533) #3, !dbg

However the return constant propagation code from

http://llvm.org/docs/doxygen/html/IPConstantPropagation_8cpp_source.html

finds the ret i32 0 and ends up calling jumptocont with i32 0 as a first parameter, which wasn't what I wanted. Ending up like:

  %189 = tail call i32 @callcatchn(i32 %188, i32 %12) #10, !dbg !7019
   tail call void @jumptocont(i32 0, i32 %14, i32 %12) #10, !dbg !7022

I tried unreachable in the callcatch which would make the caller code turn into unreachable too. Is there a workaround for this, or a better way to do this?

We need to disable most forms of IPO on naked functions. We already do this in DeadArgElimination.cpp. We need to do it in more places.

+1. Carlo, can you please open a bug?

Done

https://llvm.org/bugs/show_bug.cgi?id=31811

We need to disable most forms of IPO on naked functions. We already do
this
in DeadArgElimination.cpp. We need to do it in more places.

+1. Carlo, can you please open a bug?

Done

https://llvm.org/bugs/show_bug.cgi?id=31811

I committed a fix (r293727), this should be enough for IPSCCP.
IPConstprop still needs to learn about the `naked` attribute, but
that's not part of the default pipeline.
If this really matters to you (and you use 4.0), you may consider
asking for a backport, but maybe it's too late (and given it's not a
regression maybe not really justifiable).

commit dcf8ccaf1afac34fc8fe0f9d7a2131b84a710ba6
Author: davide <davide@91177308-0d34-0410-b5e6-96231b3b80d8>

    [IPSCCP] Teach how to not propagate return values of naked functions.

    Differential Revision: https://reviews.llvm.org/D29360

    git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293727
91177308-0d34-0410-b5e6-96231b3b80d8