This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc attributes and realloc
Daniel Berlin <dberlin@dberlin.org> writes:
> On Jan 2, 2004, at 11:58 AM, Joseph S. Myers wrote:
>> (And quite what does the malloc
>> attribute do at present, given the lack of points-to analysis in GCC?)
>>
>>
> Absolutely nothing, AFAIK. :)
> I'm dreading the __attribute__ (malloc) related bugs that will happen
> if I include this optimization in tree-ssa's PTA.
Are you referring to what __attribute__ (malloc) does for the tree
optimizations on the tree-ssa branch?
For RTL optimizations __attribute__ (malloc) does what the
documentation says:
The `malloc' attribute is used to tell the compiler that a function
may be treated as if it were the malloc function. The compiler
assumes that calls to malloc result in a pointers that cannot
alias anything. This will often improve optimization.
See the difference in the generated code for "foo" and "bar":
extern void *myalloc_withattribute (unsigned int size) __attribute__((malloc));
extern void *myalloc_no_attribute (unsigned int size);
extern int link_error (void);
struct str {
int *aaaa;
int *bbbb;
};
int
foo (unsigned int SZ, struct str *test)
{
int *aaaa;
int *bbbb;
aaaa = myalloc_withattribute (SZ*sizeof(int));
bbbb = myalloc_withattribute (SZ*sizeof(int));
aaaa[0] = 1;
bbbb[0] = 1;
/* aaaa and bbbb do not alias, so the comparisons can be resolved at
compile time */
if (aaaa[0] != 1)
return link_error ();
if (bbbb[0] != 1)
return link_error ();
test->aaaa = aaaa;
test->bbbb = bbbb;
return 0;
}
int
bar (unsigned int SZ, struct str *test)
{
int *aaaa;
int *bbbb;
aaaa = myalloc_no_attribute (SZ*sizeof(int));
bbbb = myalloc_no_attribute (SZ*sizeof(int));
aaaa[0] = 1;
bbbb[0] = 1;
/* bbbb might alias aaaa, the comparison cannot be resolved at
compile time */
if (aaaa[0] != 1)
return link_error ();
if (bbbb[0] != 1)
return link_error ();
test->aaaa = aaaa;
test->bbbb = bbbb;
return 0;
}
The generated x86 assembly:
(compiled with gcc -O2 -fomit-frame-pointer)
foo:
subl $28, %esp
movl %ebx, 16(%esp)
movl 32(%esp), %ebx
movl %edi, 24(%esp)
movl 36(%esp), %edi
sall $2, %ebx
movl %esi, 20(%esp)
movl %ebx, (%esp)
call myalloc_withattribute
movl %ebx, (%esp)
movl %eax, %esi
call myalloc_withattribute
movl %eax, 4(%edi)
movl %esi, (%edi)
movl $1, (%esi)
movl $1, (%eax)
movl 16(%esp), %ebx
xorl %eax, %eax
movl 20(%esp), %esi
movl 24(%esp), %edi
addl $28, %esp
ret
bar:
subl $28, %esp
movl %ebx, 16(%esp)
movl 32(%esp), %ebx
movl %esi, 20(%esp)
movl %edi, 24(%esp)
sall $2, %ebx
movl 36(%esp), %edi
movl %ebx, (%esp)
call myalloc_no_attribute
movl %ebx, (%esp)
movl %eax, %esi
call myalloc_no_attribute
movl $1, (%esi)
movl $1, (%eax)
cmpl $1, (%esi)
je .L5
movl 16(%esp), %ebx
movl 20(%esp), %esi
movl 24(%esp), %edi
addl $28, %esp
jmp link_error
.p2align 4,,7
.L5:
movl %eax, 4(%edi)
xorl %eax, %eax
movl %esi, (%edi)
movl 16(%esp), %ebx
movl 20(%esp), %esi
movl 24(%esp), %edi
addl $28, %esp
ret