[llvm-dev] in an IR pass: access the instruction pointer/BB address

Marc via llvm-dev llvm-dev at lists.llvm.org
Thu Aug 8 13:36:18 PDT 2019

Hi Tim,

On 08.08.19 17:07, Tim Northover wrote:
> Hi Marc,
> On Thu, 8 Aug 2019 at 15:39, Marc via llvm-dev <llvm-dev at lists.llvm.org> wrote:
>> I want to access the current address of the basic block. It does not
>> need to be the exact address of the BB, a few instructions down of that
>> BB is fine as well.
> I think you want BlockAddress:
> https://llvm.org/doxygen/classllvm_1_1BlockAddress.html. It's an
> instance of Constant and therefore also Value, and doesn't need to be
> inserted as an instruction.

I thought this is a relative address within the function and not
complete address and thats why I dismissed it.

If it is an absolute value - how do I put that into an IRB.CreateStore()
as the value to write? My various attempts compiled the pass crashed
when using it ;)

>> basically the same that I could do with an "leaq (%rip), %rdx"
>> instruction on intel x64.
> That's slightly different, and I don't think there is an intrinsic for
> the current PC. You could use inline assembly to get it though, since
> you don't care about portability.

I thought about that too, but then stumbled into the next problem:
How would I get it into a free, unused register? That is kinda the great
stuff about the llvm IR, that it optimizes it as good as possible.

If I have to do "push rdx; lea (%rip), %rdx;" (do stuff) "pop rdx" that
a) needs unnecessary instructions and b) then I have the problem that I
have the value I want in rdx, but how would I access that register with
IR ... and writing the whole functionality in inline assembly is not as
effective as IR, and speed is essential ...

I would then rather do something like
and in that call set a global variable with GET_CALLER_PC, return and go
on ... at least this is portable and should have a similar speed as a
full inline assembly. (if that is that easy. I am not an llvm guru. by
far. rather mostly clueless ;) but its part of a bigger endeavor that
needs a small plugin in llvm)


Marc Heuse

PGP: AF3D 1D4C D810 F0BB 977D  3807 C7EE D0A0 6BE9 F573

More information about the llvm-dev mailing list