[cfe-dev] Matching Clang's AST nodes to the LLVM IR instructions they produced.

Matthew Heinsen Egan heinsm01 at student.uwa.edu.au
Mon Jan 16 18:30:55 PST 2012

Hi all,

I need to be able to determine, from an LLVM IR instruction, the
specific node in the AST that produced that instruction. For example,
if I had a C program containing the following:

  // int a, b, c
  (a * b) + c

Which produced the following:

  %0 = load i32* %a.addr, align 4
  %1 = load i32* %b.addr, align 4
  %mul = mul nsw i32 %0, %1
  %2 = load i32* %c.addr, align 4
  %add = add nsw i32 %mul, %2

I would like to be able to determine that %mul represented the value
of (a * b). The IR will be unoptimized, and I'm only interested in C
language programs at this stage.

What is the best way to approach this? I was thinking of modifying
codegen to add some metadata that mapped back to the AST (similar to
EmitDeclMetadata), but I imagine that having to maintain the
modifications would cause difficulty with keeping up-to-date with
Clang. Ideally I would like to be able to use some sort of plugin, or
extend codegen from outside of Clang's source tree, so that I can
avoid these issues and use a pure version of Clang. Any pointers on
how to achieve this would be greatly appreciated.

Thanks for you time,

More information about the cfe-dev mailing list