auto f(char c) { return strchr("123", c); } This can be optimized to `return memchr("123", c, 4)`. This transformation is done by LLVM, but not by GCC.
For -Os that is an undesirable change because it makes the code larger. But otherwise yes, seems useful.
Confirmed. Though for small strings it might even make sense to inline it, eg.: auto f(char c) { return strchr("123", c); } to auto f(char c) { auto t = "123"; int t1; switch (c) { case '1': t1 = 1; case '2': t1 = 2; case '3': t1 = 3; default: t = 0; return t; } return t+t1; }
(In reply to Andrew Pinski from comment #2) > Confirmed. > auto f(char c) > { > auto t = "123"; > int t1; > switch (c) > { > case '1': > t1 = 1; > case '2': > t1 = 2; > case '3': > t1 = 3; > default: > t = 0; > return t; > } > return t+t1; > } I missed '\0' (and break's): auto f(char c) { auto t = "123"; int t1; switch (c) { case '1': t1 = 1; break; case '2': t1 = 2; break; case '3': t1 = 3; break; case '\0': t1 = 4; break; default: t = 0; return t; } return t+t1; }
Created attachment 53157 [details] A patch