https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71315
prathamesh3492 at gcc dot gnu.org changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |prathamesh3492 at gcc dot gnu.org --- Comment #3 from prathamesh3492 at gcc dot gnu.org --- Hi, As of r238513, gcc folds call to strlen into constant. void f(unsigned x) void g() { char s[] = "hello"; f (__builtin_strlen (s + 1)); } optimized dump shows: ;; Function g (g, funcdef_no=0, decl_uid=1753, cgraph_uid=0, symbol_order=0) Optimizing: _1 = __builtin_strlen (&MEM[(void *)&s + 1B]); into: _1 = 4; g () { char s[6]; long unsigned int _1; unsigned int _2; <bb 2>: s = "hello"; _1 = 4; _2 = (unsigned int) _1; f (_2); s ={v} {CLOBBER}; return; } However there seems to be another issue, if there are repeated calls to strlen() with same arg, only the first call is folded. For example consider following case: void f (unsigned); void g (void) { char s[] = "hello"; f (__builtin_strlen (s)); f (__builtin_strlen (s)); } Only the first call to __builtin_strlen() is folded to 5, the second one isn't. Optimized dump shows: ;; Function g (g, funcdef_no=0, decl_uid=1753, cgraph_uid=0, symbol_order=0) Optimizing: _1 = __builtin_strlen (&s); into: _1 = 5; g () { char s[6]; long unsigned int _1; unsigned int _2; long unsigned int _3; unsigned int _4; <bb 2>: s = "hello"; _1 = 5; _2 = (unsigned int) _1; f (_2); _3 = __builtin_strlen (&s); _4 = (unsigned int) _3; f (_4); s ={v} {CLOBBER}; return; } clang seems to fold both the calls to 5. Thanks, Prathamesh