Separate LoopVectorize LLVM pass

     I am trying to create my own LoopVectorize.cpp pass as a separate pass from the LLVM trunk, as described in Did anybody try something like this?
     I added close to the end of the .cpp file:
     /* this line seems to be required - it allows to run this pass
        as an embedded pass by giving opt -my-loop-vectorize */
     static RegisterPass<LoopVectorize> Z("my-loop-vectorize",
                                     "Write comments in source file.");

     Note that I did NOT register my new pass in llvm/lib/Transforms/IPO/PassManagerBuilder.cpp - the original LoopVectorize module is registered there.

     However, when I give:
       opt -debug -load -my-loop-vectorize test.c
      it does not vectorize a loop that was normally vectorized by the original LoopVectorize module in LLVM. My module gives:
         <<loop not vectorized: loop control flow is not understood by vectorizer
         LV: Not vectorizing: Cannot prove legality.>>

     So I tried to explicitly run a few more passes before my new LoopVectorize to help it (I think the most important one is -loop-simplify, which canonicalizes the loop):
       opt -debug -loop-simplify -mem2reg -lcssa -load -my-loop-vectorize test.c
     However, I do not see any line in the output of opt starting with "LoopSimplify" (when I have success, when I give opt -O3, I get "LoopSimplify: Creating pre-header for.body.preheader; LoopSimplify: Creating dedicated exit block for.end.loopexit" before LoopVectorize) and my LoopVectorize still does not vectorize the loop.

     Can anybody help me run explicitly the LoopSimplify pass (before my LoopVectorize pass)?

   Thank you. And wish you happy Easter holidays!

     I managed to find the reason why my own LoopVectorize pass does not vectorize.
     Actually, the loop-simplify pass was getting executed, but because it did not run the passes loop-rotate and licm before it did not generate a proper loop preheader and the loops were not valid for vectorization.

     To make my own LoopVectorize pass vectorize I got inspired from the arguments displayed on stdout of:
       opt -debug-pass=Arguments -O3 ...
     and looked at what other passes are being executed before and after -loop-vectorize .

     So I chose the following:
       opt -mem2reg -loop-rotate -licm -loop-unswitch -simplifycfg -domtree -basicaa -aa -instcombine -loops -loop-simplify \
           -force-vector-width=128 -load -my-loop-vectorize \
           -loop-simplify -scalar-evolution -aa -loop-accesses -loop-load-elim -basicaa -aa -instcombine -scalar-evolution -demanded-bits -slp-vectorizer

   Best regards,

     I come back to this thread because I still have issues with my separate LoopVectorize (same code, but without relying on PassManagerBuilder and -O3) LLVM pass.
     More exactly, my LoopVectorize pass, when given the following reduction code:
       short SumReduce(short *C, short N) {
         short sum = 0;
         for (short i = 0; i < N; ++i) {
           sum += C[i];
         return sum;
     I get on the standard error of opt:
       LV: Checking a loop in "SumReduce" from test.c:15:3
       LV: Loop hints: force=? width=128 unroll=0
       LV: Found a loop: for.body
       LoopVectorize::canVectorizeInstrs(): PhiScev = {0,+,1}<nuw><nsw><%for.body>
       LoopVectorize::canVectorizeInstrs(): AR = {0,+,1}<nuw><nsw><%for.body>
       LV: Found an induction variable.
       LoopVectorize::canVectorizeInstrs(): PhiScev = %sum.02
       LV: PHI is not a poly recurrence.
       remark: <unknown>:0:0: loop not vectorized: value that could not be identified as reduction is used outside the loop
       LV: Found an unidentified PHI. %sum.02 = phi i32 [ %add, %for.body ], [ 0, %for.body.preheader ]
       LV: Can't vectorize the instructions or CFG
       LV: Not vectorizing: Cannot prove legality.
       Exiting LoopVectorize::runOnFunction()

     Does anybody know what pass would be helpful for this issue (e.g. "PHI is not a poly recurrence."), besides the passes presented below already.?

   Thank you very much,