How to write optimizer loop

I've written an optimization loop, with the following form:

     PassManager lpm;


     const int maxit = 100;
     int it = 0;
     bool changed = true;
     while (changed && it < maxit)
         changed =*myModule);

Aside from the possibility that the optimizations don't converge (handled by the "maxit" variable), this code is erroneous since the Loop Deletion pass incurs LCSSA and loop-simplify, which will likely always modify code that has been simplified via CFGSimplification.

Is there a recommended method to write an optimization loop that correctly detects when an iteration has made changes to the module? I'm now thinking that it will be necessary to compare the new and previous module, possibly with a hash function.

Also, how could I embed this loop into another PassManager so that it doesn't need to recompute stuff such as DominatorTree?