<div dir="ltr"><div><div><div>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:<br><br></div><div>     size_t ret;<br></div>     for(int i = 0; i < LOOP_COUNT; i++)<br></div>        ret = sizeof("abcd");<br><br></div><div>Which I would expect to be optimsied into:<br><br></div><div>    size_t ret = 5; <br><br></div><div>and the loop disappear. <br><br></div><div>Well, the assignment of ret is indeed removed, but the loop remains (in LLVM-IR).<br><br></div><div>If I rewrite it to:<br><div><br>     size_t ret = sizeof("abcd");<br></div>     for(int i = 0; i < LOOP_COUNT; i++)<br>        ;<br><br></div><div>then the empty loop is removed.<br><br></div><div>I can't see a logical reason to keep "now empty" loops. Does anyone have a better idea than "it's a bug"? <br><br></div><div>The original code was comparing:<br><br></div><div>     for(int i = ... )<br></div><div>        ret = strlen("abcd");<br><br></div><div>and for this the loop DOES get optimised out, as does the sizeof loop on gcc.<br><br>--<br></div><div>Mats<br></div><div><br></div></div>