Re: [RFC] Marking C++ new operator as malloc?

2007-09-13 Thread Andrew Pinski
On 9/7/07, Chris Lattner [EMAIL PROTECTED] wrote: It is unclear whether this is safe. Nothing in the standard AFAIK requires the operator new be implemented in terms of malloc, and users are allowed to override it. I was looking for something else in the standard and found this (5.3.4/7):

Re: [RFC] Marking C++ new operator as malloc?

2007-09-13 Thread Gabriel Dos Reis
Andrew Pinski [EMAIL PROTECTED] writes: | On 9/7/07, Chris Lattner [EMAIL PROTECTED] wrote: | | It is unclear whether this is safe. Nothing in the standard AFAIK | requires the operator new be implemented in terms of malloc, and | users are allowed to override it. | | I was looking for

Re: [RFC] Marking C++ new operator as malloc?

2007-09-13 Thread Andrew Pinski
On 13 Sep 2007 19:37:27 -0500, Gabriel Dos Reis [EMAIL PROTECTED] wrote: Do you believe that allocator is prohibited by the C++ standard? Yes just because it points to another object at that point. Now people do these tricks, I know but I guess most of them don't understand C++ that much to

Re: [RFC] Marking C++ new operator as malloc?

2007-09-13 Thread Gabriel Dos Reis
On Thu, 13 Sep 2007, Andrew Pinski wrote: | On 13 Sep 2007 19:37:27 -0500, Gabriel Dos Reis [EMAIL PROTECTED] wrote: | Do you believe that allocator is prohibited by the C++ standard? | | Yes just because it points to another object at that point. I'm afraid that is too dense for me. Please

Re: [RFC] Marking C++ new operator as malloc?

2007-09-12 Thread Richard Guenther
On 11 Sep 2007 18:14:21 -0700, Ian Lance Taylor [EMAIL PROTECTED] wrote: Richard Guenther [EMAIL PROTECTED] writes: On 9/9/07, Richard Guenther [EMAIL PROTECTED] wrote: Which brings back the fact that you cannot implement malloc in C (you certainly remember the discussions about C++

Re: [RFC] Marking C++ new operator as malloc?

2007-09-12 Thread Ian Lance Taylor
Richard Guenther [EMAIL PROTECTED] writes: CHANGE_DYNAMIC_TYPE_EXPR has the target type, of course. So perhaps we need __attribute__ ((change_dynamic_type))? Or actually of course __attribute__ ((malloc)) is fine but we could throw in a CHANGE_DYNAMIC_TYPE_EXPR after any call to such a

Re: [RFC] Marking C++ new operator as malloc?

2007-09-12 Thread Richard Guenther
On 12 Sep 2007 08:13:31 -0700, Ian Lance Taylor [EMAIL PROTECTED] wrote: Richard Guenther [EMAIL PROTECTED] writes: CHANGE_DYNAMIC_TYPE_EXPR has the target type, of course. So perhaps we need __attribute__ ((change_dynamic_type))? Or actually of course __attribute__ ((malloc)) is

Re: [RFC] Marking C++ new operator as malloc?

2007-09-11 Thread Richard Guenther
On 9/11/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: Well, we have that now: int *q = new int; delete q; int *p = new int; delete p; if (q != p) cout different; we cannot optimize the test to be always true. The point is that alias

Re: [RFC] Marking C++ new operator as malloc?

2007-09-11 Thread Gabriel Dos Reis
Richard Guenther [EMAIL PROTECTED] writes: [...] | I think I'm getting confused. Perhaps you could sum up in a single | email the argument for why putting this attribute in our standard | headers is safe, even in view of possible replacement in user programs? | | My argument goes like We

Re: [RFC] Marking C++ new operator as malloc?

2007-09-11 Thread Richard Guenther
On 11 Sep 2007 07:24:44 -0500, Gabriel Dos Reis [EMAIL PROTECTED] wrote: Richard Guenther [EMAIL PROTECTED] writes: [...] | I think I'm getting confused. Perhaps you could sum up in a single | email the argument for why putting this attribute in our standard | headers is safe, even in

Re: [RFC] Marking C++ new operator as malloc?

2007-09-11 Thread Mark Mitchell
Richard Guenther wrote: So, for something like: char *pool; void *operator new[] (size_t s) { /* return memory from pool */ } ... pool = new char[1024]; char *c = new char[16]; IIUC, your claim is that if pool and c now have the same value, then any indirection through pool is

Re: [RFC] Marking C++ new operator as malloc?

2007-09-11 Thread Ian Lance Taylor
Richard Guenther [EMAIL PROTECTED] writes: On 9/9/07, Richard Guenther [EMAIL PROTECTED] wrote: Which brings back the fact that you cannot implement malloc in C (you certainly remember the discussions about C++ placement new handling wrt aliasing). To re-use the machinery we invented

Re: [RFC] Marking C++ new operator as malloc?

2007-09-10 Thread Richard Guenther
On 9/10/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: What is an example program in that meets the requirements I gave above -- i.e., allows the compiler to prove that two same-typed pointers do not alias (whether by the compiler's cleverness, use of restrict, or

Re: [RFC] Marking C++ new operator as malloc?

2007-09-10 Thread Martin Jambor
Hi, On Sun, Sep 09, 2007 at 12:56:25PM -0700, Mark Mitchell wrote: For a particular implementation of operator new (such as the one in libstdc++), you can of course make it safe in the same way as malloc; hide the implementation somewhere the rest of the program can't see it (modulo LTO).

Re: [RFC] Marking C++ new operator as malloc?

2007-09-10 Thread Tom Tromey
Mark == Mark Mitchell [EMAIL PROTECTED] writes: Mark If the compiler treats: Mark int *p = new int; Mark any differently from: Mark int r = *new int; Mark then we're missing an optimization opportunity. AFAIK we don't have a way to mark a function as cannot return NULL. This is PR 20318.

Re: [RFC] Marking C++ new operator as malloc?

2007-09-10 Thread Daniel Berlin
On 9/10/07, Tom Tromey [EMAIL PROTECTED] wrote: Mark == Mark Mitchell [EMAIL PROTECTED] writes: Mark If the compiler treats: Mark int *p = new int; Mark any differently from: Mark int r = *new int; Mark then we're missing an optimization opportunity. AFAIK we don't have a way to mark

Re: [RFC] Marking C++ new operator as malloc?

2007-09-10 Thread Mark Mitchell
Richard Guenther wrote: Well, we have that now: int *q = new int; delete q; int *p = new int; delete p; if (q != p) cout different; we cannot optimize the test to be always true. The point is that alias analysis tells us something about accesses to *q and *p, but

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Ross Smith
Gabriel Dos Reis wrote: Joe Buck [EMAIL PROTECTED] writes: | On Sat, Sep 08, 2007 at 04:33:50PM -0500, Gabriel Dos Reis wrote: | Richard Guenther [EMAIL PROTECTED] writes: | | | On 9/8/07, Chris Lattner [EMAIL PROTECTED] wrote: | | I understand, but allowing users to override new means

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Ross Smith [EMAIL PROTECTED] wrote: Gabriel Dos Reis wrote: Joe Buck [EMAIL PROTECTED] writes: | On Sat, Sep 08, 2007 at 04:33:50PM -0500, Gabriel Dos Reis wrote: | Richard Guenther [EMAIL PROTECTED] writes: | | | On 9/8/07, Chris Lattner [EMAIL PROTECTED] wrote: | |

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Joe Buck
On Mon, Sep 10, 2007 at 05:33:24AM +1200, Ross Smith wrote: [...] If the request succeeds, the value returned shall be a nonnull pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. That's not

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: Probably the most common use of a custom new is to allocate memory from a user-controlled pool instead of the standard free store. Somewhere in the program there will be a pointer to the complete pool, which aliases every pointer returned by that version of new. Any

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Joe Buck
On Sun, Sep 09, 2007 at 12:24:13PM -0700, Mark Mitchell wrote: The term cannot alias is not fully defined. It could mean cannot have the same value as any other pointer of the same type. It could mean cannot have the same value as any other pointer, when both pointers are cast to `void *'.

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: Probably the most common use of a custom new is to allocate memory from a user-controlled pool instead of the standard free store. Somewhere in the program there will be a pointer to the complete pool, which aliases

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Joe Buck wrote: In the case of malloc, and assuming that all of the machinery for malloc is hidden away in some piece of the program we're not talking about, all three definitions apply. Each pointer returned by malloc is an island unto itself; there's no conforming way to get there except

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: Joe Buck wrote: In the case of malloc, and assuming that all of the machinery for malloc is hidden away in some piece of the program we're not talking about, all three definitions apply. Each pointer returned by malloc is an island unto

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Richard Guenther [EMAIL PROTECTED] wrote: Which brings back the fact that you cannot implement malloc in C (you certainly remember the discussions about C++ placement new handling wrt aliasing). To re-use the machinery we invented there we would need to place a

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: char *pool; void set_pool(char *p) { pool = p; } void *operator new(size_t s) { // return stuff from pool. } bool f() { char *p = new char[1024]; set_pool (p); char *i = new char; return (p == i); } In other words, pointers from

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: char *pool; void set_pool(char *p) { pool = p; } void *operator new(size_t s) { // return stuff from pool. } bool f() { char *p = new char[1024]; set_pool (p); char *i = new char;

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: I don't know of any place we would use such information. At least int *p = new int; int *q = new int; if (p == q) cannot be simplified as both pointers may be NULL? They cannot be NULL; new-expressions throw an exception if the allocation fails. (Of

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: I don't know of any place we would use such information. At least int *p = new int; int *q = new int; if (p == q) cannot be simplified as both pointers may be NULL? They cannot be NULL;

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Richard Guenther [EMAIL PROTECTED] wrote: On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: I don't know of any place we would use such information. At least int *p = new int; int *q = new int; if (p == q) cannot be simplified as

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: We should optimize away things like: int *p = new int; if (!p) cerr Could not allocate memory\n; We don't I believe. That's a missed-optimization, and independent of any attributes on operator new. It's a property of the new *expression*, not of the

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: But, I don't think that even the C meaning is safe in C++ for use with the library declaration of new. I'm also somewhat skeptical of the idea that we will never do any optimization on pointer comparisons. What design principle in the

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: A better one: int *p = new int; foo(p); int *q = new int; if foo deletes p then q may be equal to p. But alias analysis still (validly!) says they don't alias. Yes, I agree that this is safe. (Either p is still valid and therefore different from q, or it

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: But, I don't think that even the C meaning is safe in C++ for use with the library declaration of new. I'm also somewhat skeptical of the idea that we will never do any optimization on pointer comparisons. What design

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Gabriel Dos Reis
Mark Mitchell [EMAIL PROTECTED] writes: [...] | This seems like a useful optimization to me, and I understand that it | will work 99.99% of the time Except for people writing allocators :-) -- Gaby

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Gabriel Dos Reis
Richard Guenther [EMAIL PROTECTED] writes: [...] | It's worse than that: | |char *pool; |void set_pool(char *p) { pool = p; } |void *operator new(size_t s) { // return stuff from pool. } | |bool f() { | char *p = new char[1024]; | set_pool (p); | char *i =

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Gabriel Dos Reis
Richard Guenther [EMAIL PROTECTED] writes: [...] | I don't know of any place we would use such information. At least | | int *p = new int; | int *q = new int; | if (p == q) | | cannot be simplified as both pointers may be NULL? The above does not use the no-throw operator new, so

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Richard Guenther
On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: Richard Guenther wrote: On 9/9/07, Mark Mitchell [EMAIL PROTECTED] wrote: But, I don't think that even the C meaning is safe in C++ for use with the library declaration of new. I'm also somewhat skeptical of the idea that we will never do

Re: [RFC] Marking C++ new operator as malloc?

2007-09-09 Thread Mark Mitchell
Richard Guenther wrote: What is an example program in that meets the requirements I gave above -- i.e., allows the compiler to prove that two same-typed pointers do not alias (whether by the compiler's cleverness, use of restrict, or whatever), but where the compiler must still assume that

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread H.J. Lu
On Sat, Sep 08, 2007 at 12:57:13AM -0500, Gabriel Dos Reis wrote: Joe Buck [EMAIL PROTECTED] writes: | On Sep 7, 2007, at 1:53 PM, Martin Jambor wrote: | [ giving operator new the malloc property ] | | On Fri, Sep 07, 2007 at 06:30:33PM -0700, Chris Lattner wrote: | It is unclear

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Chris Lattner
I understand, but allowing users to override new means that the actual implementation may not honor the aliasing guarantees of attribute malloc. -Chris http://nondot.org/sabre http://llvm.org On Sep 7, 2007, at 10:12 PM, Joe Buck [EMAIL PROTECTED] wrote: On Sep 7, 2007, at 1:53 PM,

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Basile STARYNKEVITCH
Chris Lattner wrote: I understand, but allowing users to override new means that the actual implementation may not honor the aliasing guarantees of attribute malloc. -Chris On Sep 7, 2007, at 10:12 PM, Joe Buck [EMAIL PROTECTED] wrote: On Sep 7, 2007, at 1:53 PM, Martin Jambor wrote: [

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Richard Guenther
On 9/8/07, Chris Lattner [EMAIL PROTECTED] wrote: I understand, but allowing users to override new means that the actual implementation may not honor the aliasing guarantees of attribute malloc. Well, you can argue that all hell breaks lose if you do so. A sane ::new is required for almost

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Gabriel Dos Reis
Richard Guenther [EMAIL PROTECTED] writes: | On 9/8/07, Chris Lattner [EMAIL PROTECTED] wrote: | I understand, but allowing users to override new means that the actual | implementation may not honor the aliasing guarantees of attribute | malloc. | | Well, you can argue that all hell breaks

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Joe Buck
On Sat, Sep 08, 2007 at 04:33:50PM -0500, Gabriel Dos Reis wrote: Richard Guenther [EMAIL PROTECTED] writes: | On 9/8/07, Chris Lattner [EMAIL PROTECTED] wrote: | I understand, but allowing users to override new means that the actual | implementation may not honor the aliasing guarantees

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Joe Buck
On Sat, Sep 08, 2007 at 10:16:41PM +0200, Basile STARYNKEVITCH wrote: Maybe it could make sense to give the malloc attribute only to ::operator new but not to other new-s, in particular not to the placement new? It would be completely wrong to give the attribute to placement new, as the

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Martin Jambor
Hi, On Sat, Sep 08, 2007 at 10:16:41PM +0200, Basile STARYNKEVITCH wrote: Chris Lattner wrote: I understand, but allowing users to override new means that the actual implementation may not honor the aliasing guarantees of attribute malloc. -Chris Maybe it could make sense to give the

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Martin Jambor
Hi, On Fri, Sep 07, 2007 at 06:30:33PM -0700, Chris Lattner wrote: On Sep 7, 2007, at 1:53 PM, Martin Jambor wrote: when trying to analyse dynamically allocated objects in C++, I came across the need to identify results of the new operator (at least the non-overridden standard one) as

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Martin Jambor
Hi, On Sat, Sep 08, 2007 at 07:45:32AM -0700, H.J. Lu wrote: On Sat, Sep 08, 2007 at 12:57:13AM -0500, Gabriel Dos Reis wrote: I seem to remember we had had this discussion before -- in connection with optimizations related to 'malloc' attribute -- and decided not to apply the attribute

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Gabriel Dos Reis
Joe Buck [EMAIL PROTECTED] writes: | On Sat, Sep 08, 2007 at 04:33:50PM -0500, Gabriel Dos Reis wrote: | Richard Guenther [EMAIL PROTECTED] writes: | | | On 9/8/07, Chris Lattner [EMAIL PROTECTED] wrote: | | I understand, but allowing users to override new means that the actual | |

Re: [RFC] Marking C++ new operator as malloc?

2007-09-08 Thread Gabriel Dos Reis
Martin Jambor [EMAIL PROTECTED] writes: [...] | The reason why I believe the patch is safe not only in practice but | also in principle However, I do not believe it is sufficient. The reason being that if the user does not include a header that pulls in that declaration, then that

[RFC] Marking C++ new operator as malloc?

2007-09-07 Thread Martin Jambor
Hi, when trying to analyse dynamically allocated objects in C++, I came across the need to identify results of the new operator (at least the non-overridden standard one) as malloc-allocated. The cleanest approach would probably be to mark the new operator function with the malloc

Re: [RFC] Marking C++ new operator as malloc?

2007-09-07 Thread Kaveh R. GHAZI
On Fri, 7 Sep 2007, Martin Jambor wrote: Index: libstdc++-v3/libsupc++/new === --- libstdc++-v3/libsupc++/new(revision 128207) +++ libstdc++-v3/libsupc++/new(working copy) @@ -92,7 +92,8 @@ * Placement new

Re: [RFC] Marking C++ new operator as malloc?

2007-09-07 Thread Chris Lattner
On Sep 7, 2007, at 1:53 PM, Martin Jambor wrote: Hi, when trying to analyse dynamically allocated objects in C++, I came across the need to identify results of the new operator (at least the non-overridden standard one) as malloc-allocated. The cleanest approach would probably be

Re: [RFC] Marking C++ new operator as malloc?

2007-09-07 Thread Joe Buck
On Sep 7, 2007, at 1:53 PM, Martin Jambor wrote: [ giving operator new the malloc property ] On Fri, Sep 07, 2007 at 06:30:33PM -0700, Chris Lattner wrote: It is unclear whether this is safe. Nothing in the standard AFAIK requires the operator new be implemented in terms of malloc, and

Re: [RFC] Marking C++ new operator as malloc?

2007-09-07 Thread Gabriel Dos Reis
Joe Buck [EMAIL PROTECTED] writes: | On Sep 7, 2007, at 1:53 PM, Martin Jambor wrote: | [ giving operator new the malloc property ] | | On Fri, Sep 07, 2007 at 06:30:33PM -0700, Chris Lattner wrote: | It is unclear whether this is safe. Nothing in the standard AFAIK | requires the operator