From: Denis Vlasenko <vda@ilport.com.ua>
To: Arjan van de Ven <arjan@infradead.org>
Cc: Pekka J Enberg <penberg@cs.Helsinki.FI>,
Adrian Bunk <bunk@stusta.de>, Andrew Morton <akpm@osdl.org>,
linux-kernel@vger.kernel.org
Subject: Re: [-mm patch] make kcalloc() a static inline
Date: Mon, 15 Aug 2005 15:17:52 +0300 [thread overview]
Message-ID: <200508151517.52171.vda@ilport.com.ua> (raw)
In-Reply-To: <1124098918.3228.25.camel@laptopd505.fenrus.org>
On Monday 15 August 2005 12:41, Arjan van de Ven wrote:
> On Mon, 2005-08-15 at 12:33 +0300, Denis Vlasenko wrote:
>
> > gcc can optimize that away with non-const n?! I don't think so.
>
> due to the wonders of "value range propagation" it actually can, if the
> check is done sufficiently before...
>
> gcc keeps track of the range a variable can have at each point, so if
> the check is done before, the "possible range" will be such that the
> divide should be optimizable.
>
> (Note: this is a relatively new feature in gcc though)
# gcc -v
Using built-in specs.
Target: i386-pc-linux-gnu
Configured with: ../gcc-4.0.0.src/configure --prefix=/usr/app/gcc-4.0.0 --exec-prefix=/usr/app/gcc-4.0.0 --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/app/gcc-4.0.0/libexec --datadir=/usr/app/gcc-4.0.0/share --sysconfdir=/etc --sharedstatedir=/usr/app/gcc-4.0.0/var/com --localstatedir=/usr/app/gcc-4.0.0/var --libdir=/usr/lib --includedir=/usr/include --infodir=/usr/info --mandir=/usr/man --with-slibdir=/usr/app/gcc-4.0.0/lib --with-local-prefix=/usr/local --with-gxx-include-dir=/usr/app/gcc-4.0.0/include/g++-v3 --enable-languages=c,c++ --with-system-zlib --disable-nls --enable-threads=posix i386-pc-linux-gnu
Thread model: posix
gcc version 4.0.0
# cat t.c
#include <stdlib.h>
#include <limits.h>
void *kzalloc(size_t size, unsigned int flags);
static inline void *kcalloc(size_t n, size_t size, unsigned int flags)
{
if (n != 0 && size > INT_MAX / n)
return NULL;
return kzalloc(n * size, flags);
}
void* f(int n, int sz)
{
if (sz<1000) return kcalloc(n, sz, 1);
return NULL;
}
# gcc -O2 -S -mcpu=i386 t.c
`-mcpu=' is deprecated. Use `-mtune=' or '-march=' instead.
# cat t.s
.file "t.c"
.text
.p2align 2,,3
.globl f
.type f, @function
f:
pushl %ebp
movl %esp, %ebp
pushl %ebx
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
cmpl $999, %ecx
jg .L2
testl %ebx, %ebx
jne .L9
.L4:
movl $1, 12(%ebp)
imull %ebx, %ecx
movl %ecx, 8(%ebp)
popl %ebx
leave
jmp kzalloc
.p2align 2,,3
.L9:
movl $2147483647, %eax
xorl %edx, %edx
divl %ebx
cmpl %eax, %ecx
jbe .L4
.p2align 2,,3
.L2:
xorl %eax, %eax
popl %ebx
leave
ret
.size f, .-f
.ident "GCC: (GNU) 4.0.0"
.section .note.GNU-stack,"",@progbits
Seems like that optimization is not helping.
Do you have better example?
--
vda
next prev parent reply other threads:[~2005-08-15 12:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-08-08 22:38 Adrian Bunk
2005-08-09 5:09 ` Pekka J Enberg
2005-08-15 8:06 ` [-mm patch] " Denis Vlasenko
2005-08-15 8:14 ` Arjan van de Ven
2005-08-15 8:20 ` Denis Vlasenko
2005-08-15 8:28 ` Pekka J Enberg
2005-08-15 9:33 ` Denis Vlasenko
2005-08-15 9:41 ` Arjan van de Ven
2005-08-15 12:17 ` Denis Vlasenko [this message]
2005-08-15 12:25 ` Arjan van de Ven
2005-08-15 13:06 ` Pekka J Enberg
2005-08-15 13:51 ` Alan Cox
2005-08-15 16:30 ` Adrian Bunk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200508151517.52171.vda@ilport.com.ua \
--to=vda@ilport.com.ua \
--cc=akpm@osdl.org \
--cc=arjan@infradead.org \
--cc=bunk@stusta.de \
--cc=linux-kernel@vger.kernel.org \
--cc=penberg@cs.Helsinki.FI \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome