How to call some transformation passes (LoopRotate and LoopUnroll) from my own pass


I wrote my own pass which needs to do some loop unrolling.

I can perform loop unrolling via opt:

opt -mem2reg -loops -loop-simplify -loop-rotate -lcssa -loop-unroll
-unroll-count=50 mytest.bc -o mytest.bc

This command works perfectly.

However, what I really want is to produce the **same behavior** but
pass which already calls other analysis and transformation passes in
its getAnalysisUsage method:

void MyPass::getAnalysisUsage(AnalysisUsage& AU) const {

However, I couldn't figure out how to call the LoopRotate and
LoopUnroll passes since I cannot use addRequiredID or addRequired for
these two transformations.

In Scalar.h, I found:

Pass *createLoopUnrollPass(int Threshold = -1, int Count = -1, int AllowPartial = -1);
Pass *createLoopRotatePass();

Is to call these methods the way to go? How?, can somebody show me an

P.S. I tried hard on Google but I couldn't find anything related.



Hi, Jorge.

When you put these "AU.addRequired<OtherPass>();", you are telling to
the pass manager that your pass will use the other passes.

You can actually call the other passes with get analysis, like this:

LoopInfo &li = getAnalysis<LoopInfo>();

I hope it helps.


Raphael Ernani

The create*Pass methods give you a Pass instance that you can add to a pass manager. If you're replacing "opt", you should look at the opt driver, opt.cpp to see how to do this. You can call addRequired<X> from your pass to force the PassManager to run X before your pass.

If you want to do these transformations within a single pass, then the transformations need to be exposed as a utility. llvm::UnrollLoop() currently is, but LoopRotation is not. LoopRotation could be exposed as a utility (or you could copy the relevant code), but you should try not to do that. The llvm strategy is that loop rotation should do the right thing when it runs before your pass. If loop rotation needs more information to what you want it to do, then you can create a some new analysis that loop rotation requires.

You may want to try browsing the online docs, such as...

Hope that helps.



I understand now much better.