Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM: difference between "uses" and "user" in Instruction or Value classes

Tags:

llvm

llvm-ir

I am new in LLVM and have checked Value and Instruction classes. I see that both of these classes have the methods uses and user. What are the differences between them? Also, regarding this post, can I use these methods to determine if an instruction produces a value?

tnx.

like image 252
Carlos Avatar asked Feb 12 '16 18:02

Carlos


2 Answers

Since Instruction is derived from Value it inherits both functions users and uses. The difference is that a user of Value has the Value as one of its operands.

When you are calling uses you get a list of all Use instances holding a reference from the Value to each of the users of the particular Value. Calling users gives you a list of User directly. The following code shows how to use users and uses.

for(auto U : V->users()){  // U is of type User*
     if (auto I = dyn_cast<Instruction>(U)){
        // an instruction uses V
     }
}

You can see users as a shortcut because you can do the same with uses:

for(auto U : V->uses()){  // U is of type Use*
     if (auto I = dyn_cast<Instruction>(U.getUser())){
        // an instruction uses V
     }
}

Commonly it is enough to use users to get all dependencies of a Value.

All Values used by a Value are the operands. This direction of dependency is not part of a Value's use list.

To the second question regarding instructions producing a value: there is no guarantee that the absence of uses results from not producing a value. A dead instruction can produce a value and has no users. Additionally, an instruction not producing a value can be used by metadata .

like image 108
Michael Haidl Avatar answered Sep 28 '22 06:09

Michael Haidl


I have found this answer in "Getting Started with LLVM Core Libraries" book.

We have still not presented the most powerful aspect of the LLVM IR (enabled by the SSA form): the Value and User interfaces; these allow you to easily navigate the use-def and def-use chains. In the LLVM in-memory IR, a class that inherits from Value means that it defines a result that can be used by others, whereas a subclass of User means that this entity uses one or more Value interfaces. Function and Instruction are subclasses of both Value and User, while BasicBlock is a subclass of just Value. To understand this, let's analyze these two classes in depth:

• The Value class defines the use_begin() and use_end() methods to allow you to iterate through Users, offering an easy way to access its def-use chain. For every Value class, you can also access its name through the getName() method. This models the fact that any LLVM value can have a distinct identifier associated with it. For example, %add1 can identify the result of an add instruction, BB1 can identify a basic block, and myfunc can identify a function. Value also has a powerful method called replaceAllUsesWith(Value *), which navigates through all of the users of this value and replaces it with some other value. This is a good example of how the SSA form allows you to easily substitute instructions and write fast optimizations. You can view the full interface at LLVM Value Class.

• The User class has the op_begin() and op_end() methods that allows you to quickly access all of the Value interfaces that it uses. Note that this represents the use-def chain. You can also use a helper method called replaceUsesOfWith(Value *From, Value *To) to replace any of its used values. You can view the full interface at LLVM User Class.

like image 26
Carlos Avatar answered Sep 28 '22 07:09

Carlos