Consider the following code:
#include <string.h>void bar(char c);void foo(const char* restrict ss) { for (int i = 0; i < strlen(ss); ++i) { bar(*ss); }}
I would expect the strlen(ss)
to be hoisted out of the loop in these essentially ideal conditions; and yet - it isn't, neither by clang 5.0 nor by gcc 7.3 with maximum optimization (-O3
).
Why is this the case?
Note: Inspired by (my answer to) this question.