https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91996
Bug ID: 91996 Summary: fold non-constant strlen relational expressions 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: --- Now that the strlen pass is integrated with EVRP it's become possible to make use of the EVRP ranges to fold even some relational expressions involving non-constant strlen values. For example, in the test case below, since a's length is known to be at least 7, copies of its first N characters must be at least as long as MIN (N, strlen (a)]. $ cat b.c && gcc -S -O2 -Wall -fdump-tree-optimized=/dev/stdout b.c extern char a[32], b[32]; void f (void) { if (__builtin_strlen (a) < 7) return; __builtin_memcpy (b, a, sizeof b); if (__builtin_strlen (b) < 7) // can be folded to false __builtin_abort (); } ;; Function f (f, funcdef_no=0, decl_uid=1926, cgraph_uid=1, symbol_order=0) Removing basic block 6 Removing basic block 7 f () { long unsigned int _1; long unsigned int _2; <bb 2> [local count: 1073741824]: _1 = __builtin_strlen (&a); if (_1 <= 6) goto <bb 5>; [34.00%] else goto <bb 3>; [66.00%] <bb 3> [local count: 708669605]: MEM <unsigned char[32]> [(char * {ref-all})&b] = MEM <unsigned char[32]> [(char * {ref-all})&a]; _2 = __builtin_strlen (&b); if (_2 <= 6) goto <bb 4>; [0.00%] else goto <bb 5>; [100.00%] <bb 4> [count: 0]: __builtin_abort (); <bb 5> [local count: 1073741829]: return; }