Is there a C++ API that print ir::Value and all of the related producer instructions?

Hi,

I am debugging on an LLVM IR, and that needs to figure out what exactly some value is computed.
It is painful to read the IR and search the parent name one by one.

Is it possible to automatically print a value and all the related parent instructions?

for example:

  %60 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
  %61 = urem i32 %60, 16
  %62 = udiv i32 %60, 16
  %63 = urem i32 %62, 8
  %64 = mul i32 %63, 1
  %idx_0_011 = add i32 %64, 0

If I want to print %idx_0_011, then it will automatically print all the related instructions that help produce this value:

%60 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%62 = udiv i32 %60, 16
%63 = urem i32 %62, 8
%64 = mul i32 %63, 1
%idx_0_011 = add i32 %64, 0

Hi,

I am not aware of such a function. You can easily add it yourself, just by traversing the operands and the definitions:

void debugprint(Instruction *Inst) {
  for (auto &Op : Inst->operands()) {
    Value *V = &(*Op);
    if (Instruction *I = llvm::dyn_cast<Instruction>(V))
      debugprint(I);
  }
  Inst->dump();
}

Regards,
Kai