Index: llvm/docs/LoopTerminology.rst =================================================================== --- llvm/docs/LoopTerminology.rst +++ llvm/docs/LoopTerminology.rst @@ -155,4 +155,44 @@ "More Canonical" Loops ====================== -TBD +Rotated Loops +------------- + +Loops are rotated by the loop-rotate pass. The purpose +of this transformation is to convert loops into +do/while style loops. Example: + +.. code-block:: C + + for (int i = 0; i < n; i += 1) + Stmt(i) + +is transformed to: + +.. code-block:: C + + int i = 0; + do { + Stmt(i); + i += 1; + } while (i < n); + +**Warning**: This transformation is valid only if the compiler +can prove that the loop body will be executed at least once. Otherwise, +it has to insert a guard which will test it at runtime. In the example +above, that would be: + +.. code-block:: C + + int i = 0; + if (n > 0) { + do { + Stmt(i); + i += 1; + } while (i < n); + } + +This transformation canonicalizes the loop latch to have +a single successor, which implies that the loop latch +is also an exiting block. It is done by the `loop-rotate` +pass.