identifying a loop in LLVM CFG

1.9k Views Asked by At

I am writing a pass in llvm that would identify loop invariants and hoist those instructions who are using those invariants, above the loop body. But for that i need to know whether there is any back edge from one node to another. For e.g. I want to find whether there is a back edge from node N to node H, where node H dominates node N, that would help me identify a natural loop. How can i find whether there is any edge from one node to another in the CFG ? I could not found any class called CFG in LLVM from which i could gather this information.

2

There are 2 best solutions below

0
On

You can roll your own (by iterating over the successors of a basic block with succ_iterator/succ_begin/succ_end) or you can use LoopInfo.

0
On

If you call the loop-simplify pass it will guarantee that there is only a single back edge to the loop header. This is a transformation pass so the CFG is modified but it makes additional CFG hacking far simpler.