How to use replaceUsesWithIf

I am very new to LLVM. I illustrate what I want to achieve visually below.

Initially, Inst A is used by other Inst (B, C, D, etc). Now, I want to create a new Inst (A’), which uses A still. However, all other Inst now will use Inst A’ instead. After going through documentations, I find replaceUsesWithIf() function, but I can hardly find any example about how to use it, especially on how to insert the conditional as function_ref.

   A
  /|\
 / | \
B  C  D ...  // Before


   A
   |
   |
   A'
  /|\
 / | \
B  C  D ... // After

(Inst A).replaceUsesWithIf((Inst A'), ...);

Hello,

For your case it would be something like:

A1 = /* ... create insn and pass A as operand ... */
A->replaceUsesWithIf(A1, [=](Use *U) { return U.getUser() != A1; })

In general for such utility functions I recommend to grep through LLVM code base to get a feeling about usage patterns.

Disclaimer: I’m a beginner myself, so there might be some utility function I’m not aware of.

Also, be careful not to modify uses or instruction lists while iterating over those lists as it might invalidate the iterator. And there are no asserts that would tell you about it… I collect instructions I need to modify in some kind of a container and apply modifications afterwards.