I have a LLVM bitcode file and I'm running a loop pass on it. Every time I arrive at a loop ("runOnLoop"), I extract several pieces of information about the loop body (i.e. the number of instructions in the body) and print it. However, I need a way to associate this information with a specific loop - in other words, I need to print the "name" of the loop the information was extracted from.
1 Answer
I'm not sure what you mean by "name", but one way is to print debugging information (line number/column) associated with the loop latch block or something similar.
Another way is to use metadata to uniquely identify each loop and associate the extracted information with that identifier.
I had a similar need too, so I created a pass for that. Please note that this approach is sensitive to compiler optimizations and it does not preserve the ID when that happens (e.g. if a function that contains a loop is inlined). For best results (closer to the source) use it over IR that has been compiled with -O0. Further, optimizations can be applied afterwards, when you're done with your information gathering.
However, for something simple, I'd go with the first approach.