23

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.

Community
  • 1
  • 1
Carlos
  • 411
  • 1
  • 3
  • 11

2 Answers2

20

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 .

hexcoder
  • 1,218
  • 8
  • 13
Michael Haidl
  • 5,384
  • 25
  • 43
  • So, Uses gives all the instructions/values that the particular Value depends on and Users give all the instructions/values that depend on that particular value, right? – Carlos Feb 13 '16 at 22:57
  • user get the instructions, and uses get the particular values. When you wanna replace a 'variable '(value) with another, you must replace every use. – Jordy Baylac Feb 15 '16 at 17:19
  • 1
    Is it safe to assume that number of `users()` always equals to the number of `uses()`. – arrowd Feb 28 '18 at 14:41
  • 1
    @Carlos No, both Uses and Users get the Values that depend on the target value. Suppose you have a Value `a`. `a.users()` gives you the list of Values that use `a`. `a.uses()` returns the Use objects, on which you can call `getUser()` to get the value that uses `a`, or `get()` to get `a` itself. To get the list of Values that `a` uses, call `a.operands()`. – The Bic Pen Apr 07 '22 at 21:40
14

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.

yugr
  • 19,769
  • 3
  • 51
  • 96
Carlos
  • 411
  • 1
  • 3
  • 11
  • "Note that this represents the use-def chain" - I may be missing something but it seems that `op_begin`/`op_end` simply provide access to `User`'s operands whereas use-def chains [map use of variable to its definitions](https://en.wikipedia.org/wiki/Use-define_chain). – yugr May 23 '22 at 06:11