Reputation: 4283
In the LLVM C Api, instructions are passed around by LLVMValueRef. How do I determine what instruction type (add, store, fence, whatever) an LLVMValueRef is, if it's an instruction at all?
Upvotes: 2
Views: 1380
Reputation: 1689
If anyone is still looking for this: in 2016 a LLVMGetValueKind
function was added: https://reviews.llvm.org/D18729.
Upvotes: 1
Reputation: 37167
There are LLVMIsA*
functions for each value subclass. They're maybe a bit hard to find because they're generated by macros. The declarations, in Core.h
, look like this:
#define LLVM_DECLARE_VALUE_CAST(name) \
LLVMValueRef LLVMIsA##name(LLVMValueRef Val);
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST)
And the definitions, in Core.cpp
, look like this:
#define LLVM_DEFINE_VALUE_CAST(name) \
LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
}
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
So for an LLVMValueRef V
should be able to write e.g. LLVMIsAStoreInst(V)
, which will return either the same value back or null. Intermediate subclasses also work, as in e.g. LLVMIsAInstruction(V)
, LLVMIsAConstant(V)
, etc.
Alternatively, for instructions in particular, you can use LLVMGetInstructionOpcode
, and compare the enum against the LLVMOpcode
values. It will also conveniently return 0
if the argument is not an instruction.
Incidentally (in response to your comment), I think the header files are often the best place to look for LLVM documentation, as the doxygen docs are sometimes confusing. For example Core.h
is pretty straightforward to read through, and this comment answers your question.
Upvotes: 0
Reputation: 618
It looks like there's LLVMTypeOf and then LLVMTypeKind functions to get a type enumeration from a value.
Upvotes: 0