Here's another strcmp optimization opportunity. When strlen() determines that two strings are of unequal length it's safe to assume they do not compare equal. Therefore, in the function below, the conditional with the the strcmp() call can be folded into false. $ cat c.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout c.c void g (const char *a, const char *b) { if (__builtin_strlen (a) == __builtin_strlen (b)) return; if (!__builtin_strcmp (a, b)) __builtin_abort (); } ;; Function g (g, funcdef_no=0, decl_uid=1892, cgraph_uid=0, symbol_order=0) g (const char * a, const char * b) { long unsigned int _1; long unsigned int _2; int _3; <bb 2> [local count: 10000]: _1 = __builtin_strlen (a_5(D)); _2 = __builtin_strlen (b_6(D)); if (_1 == _2) goto <bb 5>; [20.97%] else goto <bb 3>; [79.03%] <bb 3> [local count: 7903]: _3 = __builtin_strcmp (a_5(D), b_6(D)); if (_3 == 0) goto <bb 4>; [0.04%] else goto <bb 5>; [99.96%] <bb 4> [count: 0]: __builtin_abort (); <bb 5> [local count: 9997]: return; }
See also pr82950 for a related enhancement.
This is reasonable. I will add the support for this into my implementation for PR78809.
Qing is on it.
Note, this optimization is only valid when the result of the strcmp is used to compare with zero.
the patch has been put back into trunk as revision 261039: https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=261039
the change is in trunk, I am closing this bug.