public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Optimizing
@ 2011-01-30 20:18 Magnus Fromreide
  2011-01-30 20:24 ` Optimizing Kalle Olavi Niemitalo
  0 siblings, 1 reply; 4+ messages in thread
From: Magnus Fromreide @ 2011-01-30 20:18 UTC (permalink / raw)
  To: gcc-help

Consider the following code:

----
#define SRC "Big blob of text goes here"

extern int foo(int, const void*, int);

void f1(void) {
        foo(1, SRC, sizeof(SRC) - 1);
}

void f2(void) {
        char buf[sizeof(SRC) - 1];
        __builtin_memcpy(buf, SRC, sizeof(SRC)  - 1); 
        foo(1, buf, sizeof(SRC) - 1);
}
----

Is it a valid optimization for the compiler to generate the same
instructions for f2 as it generates for f1?

Would it be hard to make GCC do it?

/MF

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

* Re: Optimizing
  2011-01-30 20:18 Optimizing Magnus Fromreide
@ 2011-01-30 20:24 ` Kalle Olavi Niemitalo
  2011-01-30 20:55   ` Optimizing Enrico Weigelt
  0 siblings, 1 reply; 4+ messages in thread
From: Kalle Olavi Niemitalo @ 2011-01-30 20:24 UTC (permalink / raw)
  To: Magnus Fromreide; +Cc: gcc-help

Magnus Fromreide <magfr@lysator.liu.se> writes:

> Is it a valid optimization for the compiler to generate the same
> instructions for f2 as it generates for f1?

It isn't.

(1) foo could modify the data:

int foo(int writable, const void *p, int size)
{
        if (writable) {
                memset((void *) p, 0, size);
	}
        return 0;
}

Although the second parameter is const void *, the language still
allows the function to modify the object to which that points,
provided that the object was not defined as const.  This is okay
with f2 but not with f1.

(2) In f2, buf must have a different address at each level of recursion:

int foo(int flag, const void *p, int size)
{
        static const void *outer = NULL;
        if (outer == NULL) {
		outer = p;
        	f2();
                outer = NULL;
	} else {
		assert(p != outer);
	}
        return 0;
}

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

* Re: Optimizing
  2011-01-30 20:24 ` Optimizing Kalle Olavi Niemitalo
@ 2011-01-30 20:55   ` Enrico Weigelt
  2011-01-30 21:02     ` Optimizing Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Enrico Weigelt @ 2011-01-30 20:55 UTC (permalink / raw)
  To: gcc-help

* Kalle Olavi Niemitalo <kon@iki.fi> wrote:

> Although the second parameter is const void *, the language still
> allows the function to modify the object to which that points,
> provided that the object was not defined as const. 

Doesn't that defeat the whole purpose of const ?


BTW: let me add another question:


    const std::string str;

    void one(const std::string s)
    {
	str = s;
    }
    
    void two(const char* s)
    {
	one(s);
    }

    void three()
    {
	two("hello world");
    }


What actually happens here under the hood ? Is this reliable ?
(lifetime of the string object)


cu
-- 
----------------------------------------------------------------------
 Enrico Weigelt, metux IT service -- http://www.metux.de/

 phone:  +49 36207 519931  email: weigelt@metux.de
 mobile: +49 151 27565287  icq:   210169427         skype: nekrad666
----------------------------------------------------------------------
 Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------

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

* Re: Optimizing
  2011-01-30 20:55   ` Optimizing Enrico Weigelt
@ 2011-01-30 21:02     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2011-01-30 21:02 UTC (permalink / raw)
  To: weigelt, gcc-help

On 30 January 2011 20:40, Enrico Weigelt wrote:
> * Kalle Olavi Niemitalo <kon@iki.fi> wrote:
>
>> Although the second parameter is const void *, the language still
>> allows the function to modify the object to which that points,
>> provided that the object was not defined as const.
>
> Doesn't that defeat the whole purpose of const ?

No. http://www.gotw.ca/gotw/081.htm


> BTW: let me add another question:
>
>
>    const std::string str;
>
>    void one(const std::string s)
>    {
>        str = s;
>    }
>
>    void two(const char* s)
>    {
>        one(s);
>    }
>
>    void three()
>    {
>        two("hello world");
>    }
>
>
> What actually happens here under the hood ? Is this reliable ?
> (lifetime of the string object)

It won't compile because you assign to a const, so I don't understand
the question.

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

end of thread, other threads:[~2011-01-30 21:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-30 20:18 Optimizing Magnus Fromreide
2011-01-30 20:24 ` Optimizing Kalle Olavi Niemitalo
2011-01-30 20:55   ` Optimizing Enrico Weigelt
2011-01-30 21:02     ` Optimizing Jonathan Wakely

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).