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

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Thu Aug 8 22:54:12 PDT 2019

On Thu, 8 Aug 2019 at 21:34, Marc <mh at mh-sec.de> wrote:
> I thought this is a relative address within the function and not
> complete address and thats why I dismissed it.

It's the complete address. The way it's written includes the function
because the block name might be duplicated in multiple functions.

> 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 ;)

It should just be something like "IRB.CreateStore(BlockAddress::get(F,
BB), Ptr)". Could you show us some simple IR that crashes? Or the C++
code and assertion failure if it was a compile-time issue.

> >> 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 ...

Inline assembly lets you specify input and output values. In IR you'd
write something like

    %pc = call i64 asm "leaq (%rip), $0", "=r"()

which you'd create by calling an InlineAsm object. The two strings map
to the operands of InlineAsm::get in the natural way.



More information about the llvm-dev mailing list