LLVM: The difference between “needs” and “user” in Value statements or classes

I am new to LLVM and have tested the Value and Instruction classes. I see that both of these classes have uses and user methods. What are the differences between the two? Also, regarding this post , can I use these methods to determine if the command is generating a value?

Tpx.

+5
source share
2 answers

I found this answer in the book Getting Started with Basic LLVM Libraries.

We still have not presented the most powerful aspect of LLVM IR (SSA form is included): Value and User interfaces; they make it easy to navigate use-def and def-use. In an LLRM IRL with internal memory, a class that inherits from Value means that it defines a result that others can use, while a user subclass means that this object uses one or more Value interfaces. A function and an Instruction are subclasses of both a value and a user, and BasicBlock is a subclass of just a value. To understand this, 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 your target network. For each Value class, you can also access its name through the getName () method. This simulates the fact that any LLVM value can have a separate identifier associated with it. For example,% add1 can identify the result of the addition, the BB1 ​​command can identify the base unit, and myfunc can identify the function. The value also has a powerful method called replaceAllUsesWith (Value *), which moves through all users of that value and replaces its other value. This is a good example of how the SSA form allows you to easily substitute instructions and write quick optimizations. You can view the full LLVM Value Class interface.

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

+5
source

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

When you call uses , you get a list of all Use instances containing a link from Value for each of the users of a 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 using:

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

It is usually sufficient to use users to get all the dependencies of a Value .

All values ​​used by Value are operands. This direction of the dependency is not included in the list of use of Value .

To the second question regarding instructions giving meaning: there is no guarantee that lack of use will result in it not producing meaning. Dead instruction can be meaningful and has no users. In addition, a non-producing entity may be used in metadata.

+6
source

Source: https://habr.com/ru/post/1242900/


All Articles