[llvm-dev] How to get the address of a global variable in LLVM?
Tim Northover via llvm-dev
llvm-dev at lists.llvm.org
Fri Apr 28 14:49:13 PDT 2017
On 28 April 2017 at 14:38, Dipanjan Das via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> I want to insert a call to `func` just before the store instruction with the
> address `x` of global variable `g` as parameter.
Your error is because the function you create is "void @func(i32)" but
you're trying to pass it an i32* (the address of the global). This
mismatch is spotted by LLVM's verifier and it quits.
So you have to make them match up somehow. IMO the best way to do this
would be to instead create "void @func(i8*)" (as Joel explained) and
then use IRBuilder::CreateBitCast on "po" to convert it from an i32*
to an i8*. Then you can pass that result to @func instead of "po".
Incidentally, the "i32 call void bitcast (void (i64)* @func to void
(i32)*)(i32* @a)" line in the output suggests you're being
inconsistent about how you call getOrCreateFunction for @func
elsewhere in your code: the first time it's told to expect an i64 so
when you getOrCreateFunction a second time LLVM "helpfully" inserts a
constant bitcast of its own. This is almost certainly not a good idea.
More information about the llvm-dev