[cfe-dev] No loop optimisation?

mats petersson via cfe-dev cfe-dev at lists.llvm.org
Fri Nov 6 02:32:50 PST 2015


I saw a question on "why does clang do this" on Stack Overflow, and boiling
it down to simple code, the following is the code:

     size_t ret;
     for(int i = 0; i < LOOP_COUNT; i++)
        ret = sizeof("abcd");

Which I would expect to be optimsied into:

    size_t ret = 5;

and the loop disappear.

Well, the assignment of ret is indeed removed, but the loop remains (in
LLVM-IR).

If I rewrite it to:

     size_t ret = sizeof("abcd");
     for(int i = 0; i < LOOP_COUNT; i++)
        ;

then the empty loop is removed.

I can't see a logical reason to keep "now empty" loops. Does anyone have a
better idea than "it's a bug"?

The original code was comparing:

     for(int i = ... )
        ret = strlen("abcd");

and for this the loop DOES get optimised out, as does the sizeof loop on
gcc.

--
Mats
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151106/2c8c9fff/attachment.html>


More information about the cfe-dev mailing list