Thank you for the explanation! I tried the following simple code: int test(int k) { int x = 0; for (int i = 0; i < k; ++i) x += i; return x; }
It was compiled (-O2) to something like: int test(int k) { if (k == 0) goto ret0; int x = 0; int i = 0; loop: x += i; i += 1; if (i != k) goto loop; return x; ret0: return 0; } Now I figure out why it doesn't make sense to reverse the branch (without profile feedback). 2015-01-13 20:44 GMT+08:00 Alexander Monakov <amona...@ispras.ru>: > Your measurement includes the conditional branches at the end of loop bodies. > When loops iterate, those branches are taken, and it doesn't make sense to > reverse them. > > HTH > Alexander