get LPPassManager to use it in llvm::CloneLoop

Hi,

I try to write a FunctionPass that, among other tasks, has to clone some loops from the current function.
How can I obtain the LPPassManager in order to use the CloneLoop function.
In a LoopPass this is a parameter for the runOnLoop, but how can I obtain it in a FunctionPass?
I tried simply by creating a new instance :

ValueMap<const Value , Value > VMap;
LoopInfo *LI = &getAnalysis();
LPPassManager *LPM = new llvm::LPPassManager(1);

Loop* nL = llvm::CloneLoop(L, LPM, LI, VMap, this);

but it segfaults in CloneLoop when trying LPM->insertLoop(…).

Does the CloneLoop function include both loops in the CFG and re-create the use map between the clones?
Also does it update the phi nodes to use either the original or the cloned values?

Thank you for your help,

Alexandra

Hi,

I try to write a FunctionPass that, among other tasks, has to clone some
loops from the current function.
How can I obtain the LPPassManager in order to use the CloneLoop function.
In a LoopPass this is a parameter for the runOnLoop, but how can I obtain it
in a FunctionPass?
I tried simply by creating a new instance :

    ValueMap&lt;const Value \*, Value\* &gt; VMap;
    LoopInfo \*LI = &amp;getAnalysis&lt;LoopInfo&gt;\(\);
    LPPassManager \*LPM = new llvm::LPPassManager\(1\);

    Loop\* nL = llvm::CloneLoop\(L, LPM, LI, VMap, this\);

but it segfaults in CloneLoop when trying LPM->insertLoop(..).

CloneLoop isn't fundamentally dependent on a LPPassManager, but it
looks like you have to pass one in at the moment; you might need to
mess with the implementation of llvm::CloneLoop (in CloneLoop.cpp) a
bit so that passing in null for the LPM argument works correctly.

Does the CloneLoop function include both loops in the CFG and re-create the
use map between the clones?
Also does it update the phi nodes to use either the original or the cloned
values?

CloneLoop essentially puts in a copy of the loop, and doesn't mess
with anything outside of the loop. You have to fix up everything else
to fit together properly yourself. Note that the "LCSSA" pass helps a
lot here, but I don't recall if requiring it from a FunctionPass works
properly.

-Eli

Thanks , your suggestion was welcome and CloneLoop works without passing the LPPassManager.

However, I reached another problem. When the loop to be cloned has some subloops, the subloops are not properly cloned. Some clones of the clones are created and the CFG between the cloned basic blocks of the subloops is not correctly built. There are clones like for.body.clone, for.body.clone1, for.body.clone2, … for.body.clone26 without any predecessor and for.body.clone27, for.body.clone28 … which are included in the CFG.

To be sure I did not mess up the code of CloneLoop I checked also the original version of the function (which requires LPPassManager as parameter), but the output is the same. How can I use the CloneLoop function to clone a loop nest?

Thank you.
Alexandra

I would like to point you to a code example, but then I realized
nothing in the LLVM codebase actually uses CloneLoop. So I'm not
surprised it's broken... sorry about that.

For an actually working piece of code that clones a loop, try
LoopUnswitch::UnswitchNontrivialCondition in
lib/Transforms/Scalar/LoopUnswitch.cpp.

-Eli

Hi Eli,

I would like to point you to a code example, but then I realized
nothing in the LLVM codebase actually uses CloneLoop. So I'm not
surprised it's broken... sorry about that.

maybe CloneLoop should be removed then?

Ciao, Duncan.

r133078 .

-Eli