https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86396
Bug ID: 86396 Summary: fold calls to strtod() into constants where possible Product: gcc Version: 8.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: --- The canonical C interface to obtain the values of infinity and NaN are the strtod(), strtof(), and strtold() family of functions. The C standard defines other functions such as atof(), nan() or fscanf() (and macros such as INFINITY and NAN) but they are all specified in terms of strtod() et alia. GCC provides __builtin_nan(const char*) and __builtin_inf(), but as the test case below shows it doesn't make use of these built-ins to fold calls to strtod(). It would be a worthwhile improvement to add this feature, not just for infinity and NaN, but for all other constant arguments. For example, there is no reason why the call atof("3.14") or any other call with a constant string that is representable in the return type couldn't be folded into the corresponding constant. $ cat c.c && gcc -O2 -S -fdump-tree-optimized=/dev/stdout c.c extern double atof (const char*); extern double strtod (const char*, char **); extern double nan (const char*); double f0 (void) { return atof ("nan"); // not folded but could be } double f1 (void) { return nan ("0"); // folded into NaN } double f2 (void) { return strtod ("inf", 0); // not folded but could be } double f3 (void) { return strtod ("nan", 0); // not folded but could be } ;; Function f0 (f0, funcdef_no=0, decl_uid=1905, cgraph_uid=1, symbol_order=0) f0 () { double _3; <bb 2> [local count: 1073741825]: _3 = atof ("nan"); [tail call] return _3; } ;; Function f1 (f1, funcdef_no=1, decl_uid=1908, cgraph_uid=2, symbol_order=1) f1 () { <bb 2> [local count: 1073741825]: return Nan; } ;; Function f2 (f2, funcdef_no=2, decl_uid=1911, cgraph_uid=3, symbol_order=2) f2 () { double _3; <bb 2> [local count: 1073741825]: _3 = strtod ("inf", 0B); [tail call] return _3; } ;; Function f3 (f3, funcdef_no=3, decl_uid=1914, cgraph_uid=4, symbol_order=3) f3 () { double _3; <bb 2> [local count: 1073741825]: _3 = strtod ("nan", 0B); [tail call] return _3; }