<div dir="ltr">In general, no, there is no way in LLVM IR to get the return address of a single function call, which appears to be what you want. The compiler is free to insert instructions at the end of the basic block and into the beginning of the next block, so yes, the BlockAddress is always exact, but it doesn't seem to be quite what you want. Something else that would break your invariant, for example, is if the register allocator decided to spill the return value in RAX right after the call, which is pretty typical.<div><br></div><div>There are several existing LLVM features that record function return addresses, but it is not implementable in LLVM IR. For example, CodeView debug info records heap allocation call sites. You can see this in the assembly in this example, see the labels Ltmp3 and Ltmp5 etc at the return addresses:</div><div><br></div><div>$ cat t.cpp<br>struct Foo {<br>  int x, y;<br>};<br>__declspec(allocator) Foo *newFoo();<br>void bar(Foo **foos) {<br>  foos[0] = newFoo();<br>  foos[1] = newFoo();<br>  foos[2] = newFoo();<br>  foos[3] = newFoo();<br>}<br><br>$ clang -cc1 -gcodeview  -masm-verbose -debug-info-kind=limited -triple x86_64-windows-msvc -fms-extensions -S t.cpp  -o - | grep -A4 'S_HEAP\|call.*newFoo'<br>        callq   "?newFoo@@YAPEAUFoo@@XZ"<br>.Ltmp1:<br>        movq    32(%rsp), %rcx<br>        movq    %rax, (%rcx)<br>.Ltmp2:<br>--<br>        callq   "?newFoo@@YAPEAUFoo@@XZ"<br>.Ltmp3:<br>        movq    32(%rsp), %rcx<br>        movq    %rax, 8(%rcx)<br>.Ltmp4:<br>--<br>        callq   "?newFoo@@YAPEAUFoo@@XZ"<br>.Ltmp5:<br>        movq    32(%rsp), %rcx<br>        movq    %rax, 16(%rcx)<br>.Ltmp6:<br>--<br>        callq   "?newFoo@@YAPEAUFoo@@XZ"<br>.Ltmp7:<br>        movq    32(%rsp), %rcx<br>        movq    %rax, 24(%rcx)<br>        .cv_loc 0 1 10 0                # t.cpp:10:0<br>--<br>        .short  4446                    # Record kind: S_HEAPALLOCSITE<br>        .secrel32       .Ltmp0          # Call site offset<br>        .secidx .Ltmp0                  # Call site section index<br>        .short  .Ltmp1-.Ltmp0           # Call instruction length<br>        .long   4096                    # Type index<br>--<br>        .short  4446                    # Record kind: S_HEAPALLOCSITE<br>        .secrel32       .Ltmp2          # Call site offset<br>        .secidx .Ltmp2                  # Call site section index<br>        .short  .Ltmp3-.Ltmp2           # Call instruction length<br>        .long   4096                    # Type index<br>--<br>        .short  4446                    # Record kind: S_HEAPALLOCSITE<br>        .secrel32       .Ltmp4          # Call site offset<br>        .secidx .Ltmp4                  # Call site section index<br>        .short  .Ltmp5-.Ltmp4           # Call instruction length<br>        .long   4096                    # Type index<br>--<br>        .short  4446                    # Record kind: S_HEAPALLOCSITE<br>        .secrel32       .Ltmp6          # Call site offset<br>        .secidx .Ltmp6                  # Call site section index<br>        .short  .Ltmp7-.Ltmp6           # Call instruction length<br>        .long   4096                    # Type index<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 28, 2020 at 5:57 AM PenYiWang via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi <div><br><div>I use BlockAddress to get the address of BasicBlock , </div><div><br></div><div>and I use GlobalVariable 's getInitializer() </div><div><br></div><div>to pass the address of BasicBlock to the global variable of my own program</div><div><br></div><div>and then I print it out.</div><div><br></div><div>But , I found that  BlockAddress is not always correct.</div><div><br></div><div>For example, some function's rsp (stack pointer) or other register is maintained by caller,</div><div><br></div><div>so  it would be like: </div><div><a href="https://i.imgur.com/Rwuy5ju.png" target="_blank">https://i.imgur.com/Rwuy5ju.png</a></div><div>  0x42c37a:      e8 c1 7a 00 00     call   433e40 <retrieve_url><br>  0x42c37f: 48 83 c4 20                add    rsp,0x20<br>  0x42c383: eb 00                    jmp    42c385 <main+0x16b5><br></div><div><br></div><div>What I want is the basic block which is "excatly" after the function call , 



0x42c37f



</div><div><br></div><div>I want BlockAddress give me 



0x42c37f.</div><div><br></div><div>But actually, the output my program print  out is  

0x42c383.</div><div><br></div><div>I guess "add    rsp,0x20" is seen as within the basic block  of the function call.</div><div><br></div><div>Maybe reset the rsp (stack pointer) is part of the function call.</div><div><br></div><div>Can I say there is bug in BlockAddress ?</div><div><br></div><div>Or there is some bug in LLVM's backend?</div><div><br></div><div>How to solve this problem?</div><div><br></div><div>Force clang/llvm not to use caller-saved convention or something like that ?</div><div><br></div><div>Thanks</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>