[llvm-dev] Question concerning llvm::BlockAddress class
Pei Wang via llvm-dev
llvm-dev at lists.llvm.org
Thu Apr 19 10:54:52 PDT 2018
I personally have used blockaddress as virtual addresses of basic blocks without problems, as long as you store them as globals and retrieve the values from memory. Below is an example.
int foo(int n) {
const void *targets[] = { &&BB1, &&BB2, &&BB3 };
goto *targets[n % 3];
BB1:
return 1;
BB2:
return 2;
BB3:
return 3;
}
In the compiled bit code you will find that the "targets" array is defined as follows.
@foo.targets = private unnamed_addr constant [3 x i8*] [i8* blockaddress(@foo, %11), i8* blockaddress(@foo, %12), i8* blockaddress(@foo, %13)], align 16
You can read the addresses through the global variable @foo.targets.
Pei
On 4/16/18, 4:36 PM, "serge guelton via llvm-dev" <llvm-dev at lists.llvm.org> wrote:
On Mon, Apr 16, 2018 at 04:14:03PM -0400, Brenda So via llvm-dev wrote:
> Hi all,
>
> I have a question concerning block address class in LLVM. I am currently
> working on a project where I need to obtain and manipulate basic block virtual
> addresses. I was searching the web and found the llvm::BlockAddress class (
> http://llvm.org/doxygen/classllvm_1_1BlockAddress.html). With this class I was
> able to obtain a printout like this:
>
> i8* blockaddress(@func_name, %bb_label)
>
> How do I obtain the virtual memory address from the Block address class? Can I
> even do so?
>
> Moreover, I thought that one can only obtain the virtual memory address after
> linking the object files together. So how would the BlockAddress class help me
> when it's working on the IR level? If BlockAddress class is not the way to go,
> is there another api function that I can use to obtain the addresses?
Hi Brenda,
you can bitcast a BlockAdress to an intptr and then manipulate it as you
want. Unfortunately, there' is no guarentee that you can do anything
appart jumping to this adress from the same function, as described in
the langref http://llvm.org/docs/LangRef.html#addresses-of-basic-blocks
This value only has defined behavior when used as an operand to the
‘indirectbr‘ instruction, or for comparisons against null. Pointer
equality tests between labels addresses results in undefined
behavior — though, again, comparison against null is ok, and no
label is equal to the null pointer. This may be passed around as an
opaque pointer sized value as long as the bits are not inspected.
A glimpse of hope though:
Finally, some targets may provide defined semantics when using the
value as the operand to an inline assembly, but that is target
specific.
Hope it helps,
Serge
More information about the llvm-dev
mailing list