[llvm-dev] Value

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 20 07:29:01 PDT 2017

On 20 July 2017 at 07:02, Anastasiya Ruzhanskaya via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Thank you! I wanted to use the right part of the instruction ,
>  %a = alloca i32, align 4 - %a here , but I don't quite understand the
> difference between Instruction object and Value object of a,

Instruction is a subclass of Value so if you know your Value was
defined by an instruction you can just use
"cast<Instruction>(MyValue)" to access the instruction methods (more
usually you cast to a particular kind of instruction).

But be careful of Values that aren't defined by instructions:

    @globals_arent_instructions = extern global i32
    define void @foo(i32 %not_an_Instruction) {
      %rhs_instruction = load i32, i32* @globals_arent_instructions
      %sum_instruction = add i32 %not_an_instruction, %rhs_instruction
      %res_instruction = add i32 %sum_instruction, 1
      ret i32 %res_instruction

So generally you don't want to use "cast", which asserts when the cast
is invalid. Most places use "dyn_cast" instead, which returns nullptr
when invalid; then you can check that return value.

If you look at the hierarchy diagram on
https://llvm.org/doxygen/classllvm_1_1Value.html, you can see the full
range of Values that aren't Instructions. Depending on what you're
doing you may be able to simply ignore those cases. The most common
ones that can crop up when you're not expecting them are Constants and
Arguments (as in my example above), the others tend to be obvious from
context (it's obvious a branch will have a BasicBlock destination for



More information about the llvm-dev mailing list