public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* optimized inlining of specific standard function calls
@ 2009-11-12  8:54 Enrico Weigelt
  2009-11-12 10:22 ` Andrew Haley
  0 siblings, 1 reply; 4+ messages in thread
From: Enrico Weigelt @ 2009-11-12  8:54 UTC (permalink / raw)
  To: gcc-help


Hi folks,


many specific cases of standard library function calls, eg. 

    snprintf (buf, sizeof(buf), "%s%s", a, b)

could be optimized to a few assembler lines, since in those 
cases the compiler knows exactly whats happening here.

Does gcc yet support such optimizations ? 

If not, how could it be implemented ?


cu
-- 
---------------------------------------------------------------------
 Enrico Weigelt    ==   metux IT service - http://www.metux.de/
---------------------------------------------------------------------
 Please visit the OpenSource QM Taskforce:
 	http://wiki.metux.de/public/OpenSource_QM_Taskforce
 Patches / Fixes for a lot dozens of packages in dozens of versions:
	http://patches.metux.de/
---------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: optimized inlining of specific standard function calls
  2009-11-12  8:54 optimized inlining of specific standard function calls Enrico Weigelt
@ 2009-11-12 10:22 ` Andrew Haley
  2009-11-12 15:47   ` Enrico Weigelt
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Haley @ 2009-11-12 10:22 UTC (permalink / raw)
  To: weigelt; +Cc: gcc-help

Enrico Weigelt wrote:

> many specific cases of standard library function calls, eg. 
> 
>     snprintf (buf, sizeof(buf), "%s%s", a, b)
> 
> could be optimized to a few assembler lines, since in those 
> cases the compiler knows exactly whats happening here.
> 
> Does gcc yet support such optimizations ? 

Yes.  For example, have a look at expand_builtin_fprintf() and
expand_builtin_strcpy() in
http://gcc.gnu.org/viewcvs/trunk/gcc/builtins.c?view=markup

You could do something similar by parsing a snprintf string and
converting it to expand_builtin_strcpy().

Andrew.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: optimized inlining of specific standard function calls
  2009-11-12 10:22 ` Andrew Haley
@ 2009-11-12 15:47   ` Enrico Weigelt
  2009-11-17  0:00     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Enrico Weigelt @ 2009-11-12 15:47 UTC (permalink / raw)
  To: gcc-help

* Andrew Haley <aph@redhat.com> wrote:

Hi,

> Yes.  For example, have a look at expand_builtin_fprintf() and
> expand_builtin_strcpy() in
> http://gcc.gnu.org/viewcvs/trunk/gcc/builtins.c?view=markup

Thx. I'm quite new to gcc internals, so i'd appreciate a bit 
more help. Is it possible to inject some C-code instead of
the original function call ?

For example:

    char foo[1024];
    snprintf(foo, sizeof(foo), "/tmp/%s%d.tmp", prefix, id);
    
Would inject something like:

    char foo[1024] = "/tmp/";
    {
	long  __bufmax;	// remaining space in buffer
	char* __bufptr;	// current buffer position

	// skip over the already initialized head
	__bufmax = sizeof(foo)-6;
	__bufptr = &foo[5];

	// the %s -> prefix part
	{
	    long __len__prefix = strlen(prefix);

	    // copy the parameter "prefix"
	    if (__bufmax > __len_prefix)
	    {
		memcpy(bufptr, prefix, __len_prefix);
		bufptr += __len_prefix;
		bufmax -= __len_prefix;
	    }
	    else
	    {
		memcpy(bufptr, prefix, __bufmax);
		goto __out_terminate;
	    }
	}
	
	// generate the %d -> id 
	{
	    struct __itoa_buf __fmt_d_id = __itoa_std(id);
	    if (__bufmax > __itoa_buf.len)
	    {
		memcpy(bufptr, __itoa_buf.buf, __itoa_buf.len);
		bufptr += __itoa_buf.len;
		bufmax -= __itoa_buf.len;
	    }
	    else
	    {
		memcpy(bufptr, __itao_buf.buf, __bufmax);
		goto __out_terminate;
	    }
	}

	// the suffix is short enough to be added char by char
	if (__bufmax)
	{
	    *__bufptr = '.';
	    __bufptr++;
	    __bufmax--;
	}
	else
	    goto __out_terminate;
	if (__bufmax)
	{
	    *__bufptr = 't';
	    __bufptr++;
	    __bufmax--;
	}
	else
	    goto __out_terminate;
	if (__bufmax)
	{
	    *__bufptr = 'm';
	    __bufptr++;
	    __bufmax--;
	}
	else
	    goto __out_terminate;
	if (__bufmax)
	{
	    *__bufptr = 'p';
	    __bufptr++;
	    __bufmax--;
	}

    __out_terminate:
	*bufptr = 0;
    }

And the whole thing goes through the compiler/optimizer again.


cu
-- 
---------------------------------------------------------------------
 Enrico Weigelt    ==   metux IT service - http://www.metux.de/
---------------------------------------------------------------------
 Please visit the OpenSource QM Taskforce:
 	http://wiki.metux.de/public/OpenSource_QM_Taskforce
 Patches / Fixes for a lot dozens of packages in dozens of versions:
	http://patches.metux.de/
---------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: optimized inlining of specific standard function calls
  2009-11-12 15:47   ` Enrico Weigelt
@ 2009-11-17  0:00     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2009-11-17  0:00 UTC (permalink / raw)
  To: weigelt; +Cc: gcc-help

Enrico Weigelt <weigelt@metux.de> writes:

> Thx. I'm quite new to gcc internals, so i'd appreciate a bit 
> more help. Is it possible to inject some C-code instead of
> the original function call ?

Not as such, no.  You can inject arbitrary code, but you have to do by
building GIMPLE structures.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-11-17  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-12  8:54 optimized inlining of specific standard function calls Enrico Weigelt
2009-11-12 10:22 ` Andrew Haley
2009-11-12 15:47   ` Enrico Weigelt
2009-11-17  0:00     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).