Integer ID for LLVM::Instruction*?

Hi,

I wonder whether we can easily retrieve an LLVM instruction ID that uniquely identifies the instruction. In my case, I need to avoid using llvm::Instruction* directly. Given an ‘inst’ of type llvm::instruction*, is there some readily usable method as simple as “int id = inst->id( )”? Thanks.

What’s wrong with the pointer address?

—Owen

intptr_t id = static_cast<intptr_t>(inst);

?

Thanks for asking. In my case, I need to avoid llvm:;instruction* as the identifier because the ID is to be passed to a callback function which only accepts primitive input values. Or, just imagine that you do not want the program segment that uses the identifier to be linked with LLVM.

Thanks. I did not know this simple mechanism, shame…

Is it safe? Can I then pass the intptr_t to int or long int? In my case, the ID has to be passed to a call back function accepting only primitive typed parameters.

Thanks. I did not know this simple mechanism, shame...

Is it safe? Can I then pass the intptr_t to int or long int?

Depends how big a pointer is on your system. But you should be able to
roundtrip it through other integers as safely as you could any other
integer (ie: so long as the other integer type is the same size (and
signedness) or bigger, etc)

Thanks for asking. In my case, I need to avoid llvm:;instruction* as the
identifier because the ID is to be passed to a callback function which only
accepts primitive input values. Or, just imagine that you do not want the
program segment that uses the identifier to be linked with LLVM.

The somewhat more common way to do this would be with 'void*' but
intptr_t's good too, depending on needs, etc.

Thanks. I did not know this simple mechanism, shame...

Is it safe? Can I then pass the intptr_t to int or long int? In my case,

Only when sizeof(intptr_t) is the same or smaller than the type you're shoving it into... and that's a platform dependent question, so by doing that you're introducing a potential portability problem in your code.

What callback are you having trouble with?

Jon

David,

As you suggested, I try to compile the following test code:

include “llvm/IR/Instruction.h”
#include “stdint.h”
extern void callback(intptr_t);
void foo(){
llvm::Instruction* i;
intptr_t zzzz=static_cast<intptr_t>(i);
callback(zzzz);
}

but got this compilation error:

‘intptr_t’ (aka ‘long’) is not allowed
intptr_t zzzz=static_cast<intptr_t>(i);
*^~~~~~~~~~~~~~~~~~~~~~~~*
1 warning and 1 error generated.

For information, my OS is a Ubuntu 14.04 (VM), and my command line used to compile the program is

clang prog.cpp -c -fno-rtti -std=c++11 llvm-config --cxxflags -o prog.cpp.bc

Any idea on why the casting does not work? Thanks in advance.

I just tried with reinterpret-cast instead of static_cast. It seems working. I hope it is safe.

I just tried with reinterpret-cast instead of static_cast. It seems
working. I hope it is safe.

Yep, my mistake. reinterpret_cast is the right tool for this conversion.