[llvm-dev] getting providers of a llvm instruction

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Mon Jan 17 01:40:20 PST 2022


Hi David,

On Sun, 16 Jan 2022 at 21:06, David Livshin via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Analyzing llvm IR file ( .bc or .ll ) for every instruction it is possible, using the provided functions, to determine the list of instructions that consume it result.

Yes, this information is tracked pretty efficiently by the LLVM data
structures. You can iterate using Value::users (Instruction inherits
from Value). So something like:

    for (auto &U : MyInst->users()) {
      [...]
    }

ought to work.

> What about the other way around – are there llvm functions that allow for every instruction ( or better – for every operand of an instruction ) to determine the list of instructions that provide it with the necessary resources.

I think the confusingly named `Value::uses` gives you this information:

    for (auto &U : MyInst->uses()) {
      // U is the graph edge between the two values, U.get() is the
Value actually being used.
      Value *V = U.get();
      [...]
    }

Cheers.

Tim.


More information about the llvm-dev mailing list