<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/150844>150844</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Optimization when 'while' loop can be converted to 'do-while'
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Explorer09
      </td>
    </tr>
</table>

<pre>
    This missed optimization issue is more like a feature request than a bug.

When the body of a loop contains only counter increments and the loop condition is determined externally, a `while` loop construct can sometimes be converted to a `do`-`while`.

Clang can do the optimization when the loop counter change is addition or subtraction. Now I wish the optimization can also be performed when the counter change is multiplication (by a constant) or bit shift (shift left or shift right by a constant).

(I'm not requesting for general division cases except for division by a power of two.)

Example code:

```c
#include <stdbool.h>
#include <stdint.h>

extern bool cond(uint32_t x);

uint32_t func2_a(uint32_t x) {
    uint32_t i = 10;
    while (cond(i)) {
        i *= 2;
    }
    return i;
}

uint32_t func2_b(uint32_t x) {
 uint32_t i = 5;
    do {
        i *= 2;
    } while (cond(i));
 return i;
}
```

`func2_a` and `func2_b` should be equivalent. By turning `func2_a` to `func2_b` it can usually save a `jmp` instruction.

[Related GCC bug report](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121258)

The example code can be tested in Compiler Explorer ([link](https://godbolt.org/z/Kqnr1o8zh)). 

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVVFvozgQ_jXOy6iIGAjJAw9p2pxWJ91Jp5XusTJ4AO8am9rjpO2vPxlIuk270kWRAM98830zHo-F96oziBUr7lnxsBKBeuuqx5dRW4cu3a1qK1-r773yMCjvUYIdSQ3qTZCyBpT3ASEarUPQ6ieCgBYFBYfg8DmgJ6BeGBBQhy5h6Z6l-397NEA9QgwOtgUB2toRGmtIKOPBGv0KjQ2G0IEyjcMBDXkQRk64i7dUiwqQSOgGZVACvhA6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