Value

Hello,

I am trying to write my own pass for some purposes, and found out that there is no way to work with such variables as %1 and so on. How they are identifiable? Only by the Value object, without any possibility for visual representation as for example %a, %b values?

Hi Anastasiya,

What do you mean ‘identifiable’? You have Module, Function, BasicBlock etc. You can access Values and Users. They form a data-flow graph. You can store pointers to Values and Users if you wish. If you want a name for any Value there is ValueSymbolTable (http://www.llvm.org/docs/ProgrammersManual.html#advanced-topics) which provides a symbol table that the Function and Module classes use for naming value definitions. You should take into account that some Values might not have names and passes can change names of Values.

Thanks,

Evgeny Astigevich

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, which is used further , and in this case :
%1 = alloca i32, align 4 - I also wanted to use %1 and in this case the only possibility is Instruction object.

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
example).

Cheers.

Tim.

You think in terms of the textual representation of IR and are trying to work on IR in these terms.

See:

http://www.llvm.org/docs/ProgrammersManual.html#coreclasses

http://llvm.org/doxygen/classllvm_1_1Value.html

In the LLVM representation Value is a thing which can be used as an operand to instructions. In API there is no such thing as the named right part. An instruction itself is a value which is produced as result of execution of the instruction. Look at the example:

Instr 1: add i32 1, 2

Instr 2: add i32 , 10

We have a def-use connection between Instr 2 and Instr 1. We don’t need names. We can traverse def-use connection in this way (http://www.llvm.org/docs/ProgrammersManual.html#helpful-hints-for-common-operations):

Instruction *pi = ...;

for (Use &U : pi->operands()) {
  Value *v = U.get();
  // ...
}

Your case:

%a = alloca i32, align 4 - %a here

, but I don’t quite understand the difference between Instruction object and Value object of a, which is used further , and in this case :

Instr1: alloca i32, align 4

If there are users of Instr1 then can access them calling user_begin() on Instr1. You can access Instr1 from users as in the example above. Of course you need to keep the pointer to Instr1 somewhere to check that it’s used.

%1 = alloca i32, align 4 - I also wanted to use %1 and in this case the only possibility is Instruction object.

You traverse basic blocks and process the instructions. Processing can include any type of actions: analysis, optimization etc.

So what is your use case?

-Evgeny

I am performing the bit analysis, so actually I want to propagate information about bits from operands to the instruction (the result of instruction), and every time I access the Instruction first , than get the operands, then access to the next instruction.
That was the only problem, that I keep a map from Instruction to bit info and was confused, is Instruction, that I am putting to map, is the same object as the Value, that will be used later,
as here %a = alloca i32, align 4 …%b= mul %a, 2. Now seems to be, that yes.

Yep, that's right.

Tim.

Thank you for the answers! I have only one question more :

all Instructions and Values have bit width. I can access the bit width only in ConstantInt, however it logically should be possible in any other ( store has a width, load also ans so on, as the first parameter). Is it really possible? In all docs I find this bit info only in APInt class

Ok, I have already figured out, the Type information helps.