WouldBeNice/Optimisation: const variable-parameter-list for printf

Paolo Bonzini bonzini@gnu.org
Wed Dec 21 16:08:00 GMT 2005


Etienne Lorrain wrote:
>  Hello,
> 
>  A lot of people (me too) write this kind of code:
> 
>  struct param1_str *param1;
>  struct param2_str *param2;
>  struct param3_str *param3;
> 
>  error = treat_alpha (param1, param2, param3);
>  if (error)
>     printf ("treat_alpha failed error %d, param1 = %p, "
>             "param2 = %p, param3 = %p",
>              error, param1, param2, param3);
>  error = treat_beta (param1, param2, param3);
>  if (error)
>     printf ("treat_beta failed error %d, param1 = %p, "
>             "param2 = %p, param3 = %p",
>              error, param1, param2, param3);
> 
>  The printf() is only there for debug purposes, sometimes
> it is msglog() or even sprintf().
> 
>  This construct cannot be optimised efficiently by the
> compiler because the variable list of parameters of
> printf() is not typed - so no "const" attribute, and even
> a simple:
>   printf ("error %d\n", error);
> need to flush every registers to memory before the call and
> reload everything after the call.

In general this is not possible because of %n and because uppercase 
specifiers have undefined results (and can indeed be attached to 
arbitrary code using glibc).  It may be possible to use the existing 
machinery to analyze format strings, and do the optimization you want in 
the case of constant format strings that don't mention uppercase 
specifiers or %n, but I am not knowledgeable enough to tell you if it is 
easy to do so or not.

Paolo



More information about the Gcc mailing list