Richard Guenther - Re: [PATCH]: add folding of builtin modf (original) (raw)
This is the mail archive of the gcc-patches@gcc.gnu.orgmailing list for the GCC project.
On 2/24/07, Kaveh R. GHAZI ghazi@caip.rutgers.edu wrote:
On 2/24/07, Kaveh R. GHAZI ghazi@caip.rutgers.edu wrote:
This patch adds folding of builtin modf when the first argument is a constant.
Tested on sparc-sun-solaris2.10, no regressions.
Okay for mainline?
- /* Proceed if a valid pointer type was passed in. */
- if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
I wonder what happens for
double foo(double x) { const double y; return modf (x, &y); }
as the TYPE_MAIN_VARIANT is the same for double and const double, but you certainly cannot write to y. Richard.
Hi Richard,
My patch only affects codegen when x is a constant. Presumably you meant something like this:
double foo(void) { const double y; return modf (5.25, &y); }
In the above case, unpatched mainline issues a diagnostic but happily generates code like so with -fdump-tree-original:
modf.c: In function 'foo': modf.c:5: warning: implicit declaration of function 'modf' modf.c:5: warning: incompatible implicit declaration of built-in function 'modf' modf.c:5: warning: passing argument 2 of 'modf' discards qualifiers from pointer target type
;; Function foo (foo) ;; enabled by -tree-original { const double y; const double y; return modf (5.25e+0, (double *) &y); }
With my patch we get:
modf.c: In function 'foo': modf.c:5: warning: implicit declaration of function 'modf' modf.c:5: warning: incompatible implicit declaration of built-in function 'modf' modf.c:5: warning: passing argument 2 of 'modf' discards qualifiers from pointer target type
;; Function foo (foo) ;; enabled by -tree-original { const double y; const double y; return *(double *) &y = 5.0e+0;, 2.5e-1; }
I.e. same warnings, but folded modf. The modf builtin's prototype is causing gcc to cast away y's const-ness (even though the testcase contained no modf prototype). Both cases warn correctly, but compile (without an ICE or any problems).
They also both run fine on my sparc box, but I guess that depends on what 'y' is and how your OS handles it. I'm sure I could create something that crashes at runtime, but so would the original modf call.
Note, builtin frexp and sincos also use similar code to transform their stores into their pointer arg(s). So if any change is necessary I'll apply to those as well. But I don't see anything so far that causes me to think my code won't work.
So is the original modf patch okay for mainline?
Yes, thanks. I was just worried we ICE somehow on the write.