https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86919
Bug ID: 86919 Summary: strlen of a multidimensional array element with non-constant offset not folded Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- GCC 9 folds the first of the strlen() calls below where the first index into the array is a literal constant into an efficient expression but it misses the same opportunity in the second call where the index is a variable with a known value. (GCC 8folds neither.) $ cat f.c && gcc -O2 -S -Wall -Wextra -Wnull-dereference -fdump-tree-optimized=/dev/stdout f.c const char b[][5] = { "123", "1234" }; int f (int j) { return __builtin_strlen (&b[0][j]); // folded to 3 - i } int g (int j) { int i = 0; return __builtin_strlen (&b[i][j]); // not folded } ;; Function f (f, funcdef_no=0, decl_uid=1906, cgraph_uid=1, symbol_order=1) Removing basic block 5 f (int j) { unsigned int j.1_1; unsigned int _2; int iftmp.0_3; int iftmp.0_5; <bb 2> [local count: 1073741825]: if (j_4(D) <= 3) goto <bb 3>; [50.00%] else goto <bb 4>; [50.00%] <bb 3> [local count: 536870912]: j.1_1 = (unsigned int) j_4(D); _2 = 3 - j.1_1; iftmp.0_5 = (int) _2; <bb 4> [local count: 1073741825]: # iftmp.0_3 = PHI <iftmp.0_5(3), 0(2)> return iftmp.0_3; } ;; Function g (g, funcdef_no=1, decl_uid=1909, cgraph_uid=2, symbol_order=2) g (int j) { const char * _1; long unsigned int _2; int _5; sizetype _6; <bb 2> [local count: 1073741825]: _6 = (sizetype) j_3(D); _1 = &b + _6; _2 = __builtin_strlen (_1); _5 = (int) _2; return _5; }