Difference between “uses” and “user”

Hi everyone,

Instruction and Value classes have “Uses” and “User”. What is the difference between them?

I think that “Uses” gives all the instructions/values that a particular Value depends on and “User” gives all the instructions/values that depend on that particular value. Is that right?

Best,

Carlo

Given as example the instruction %add in

%add = add i32 %1, 1
[...]
%sub = sub i32 %add, 1

You can think of an llvm::Use as a tuple (Instruction,ArgumentNo)
Take for example the first argument of the add: (%add,0)
Use::getUser() returns that Instruction (%add)
Use::getOperandNo() returns that ArgumentNo (0)
Use::get() returns the argument itself (%1); llvm::Use operator
overloads may make the llvm::Use appear like it is the argument's
llvm::Value itself

add->operands() enumerates the llvm::Use(s), i.e. (%add,0) for the
first argument (which is "%1") and (%add,1) for the second (which is
"1")
add->uses() enumerates where %add is used, i.e. (%sub,0) in this example.
add->users() enumerates only the instructions which use %add, i.e.
%sub in this example (without specifying the ArgumentNo)

llvm::User is a base class of llvm::Instruction that implements the
argument list mechanism (Lists of llvm::Value(s)).

Hope that helps any corrections/additions are welcome.

Michael

So, according to your explanation, do you think that it is possible to get the value produced by an instruction? something like the following (roughly):

for(auto operand : instruction->getOperandList())

if( operand → getUser == instruction )
return true;

else

continue;

Best,

Carlo

An Instruction /is/ its value - they aren’t distinct things.

The loop you’ve written appears tautological - iterating a User’s Uses and examining those Uses Users should produce the User you’re iterating over in all cases. If there was a Use in a User’s Uses that wasn’t a Use by that User, that would seem to be broken as far as I understand.