https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78888
Bug ID: 78888 Summary: toupper(x) can be assumed not to be in the range 'a' - 'z' Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The return value of the toupper function is guaranteed not to be in the range 'a' - 'z'. Similarly, the tolower return value is guaranteed not to be in the range 'A' - 'Z'. For non-EBCDIC character sets, it would be useful to set the range on the return value reflecting this constrained range. (The range is actually the intersection of the range [0, UCHAR_MAX] and the anti-range ~['a', 'z'], plus the value EOF). In addition, it would be useful to issue a warning if the return value is compared against a constant from its anti-range since such a comparison in always false (this might naturally fall out of the optimization). The following test case shows that GCC does not take advantage of this optimization (it doesn't remove the call to f()) or issue the warning. $ cat d.c && gcc -O2 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout d.c void f (void); void g (int x) { if (__builtin_toupper ((unsigned char)x) == 'a') f (); } ;; Function g (g, funcdef_no=0, decl_uid=1797, cgraph_uid=0, symbol_order=0) Removing basic block 5 g (int x) { int _1; int _6; <bb 2> [100.00%]: _6 = x_3(D) & 255; _1 = __builtin_toupper (_6); if (_1 == 97) goto <bb 3>; [22.95%] else goto <bb 4>; [77.05%] <bb 3> [22.95%]: f (); [tail call] <bb 4> [100.00%]: return; }