From: Jakub Jelinek <jakub@redhat.com>
To: Denis Vlasenko <vda@ilport.com.ua>
Cc: linux-kernel@vger.kernel.org, gcc@gcc.gnu.org
Subject: Re: memcpy(a,b,CONST) is not inlined by gcc 3.4.1 in Linux kernel
Date: Tue, 29 Mar 2005 17:13:05 +0200 [thread overview]
Message-ID: <20050329151305.GQ4961@sunsite.mff.cuni.cz> (raw)
In-Reply-To: <200503291737.06356.vda@ilport.com.ua>
On Tue, Mar 29, 2005 at 05:37:06PM +0300, Denis Vlasenko wrote:
> typedef unsigned int size_t;
>
> static inline void * __memcpy(void * to, const void * from, size_t n)
> {
> int d0, d1, d2;
> __asm__ __volatile__(
> "rep ; movsl\n\t"
> "testb $2,%b4\n\t"
> "je 1f\n\t"
> "movsw\n"
> "1:\ttestb $1,%b4\n\t"
> "je 2f\n\t"
> "movsb\n"
> "2:"
> : "=&c" (d0), "=&D" (d1), "=&S" (d2)
> :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
> : "memory");
> return (to);
> }
>
> /*
> * This looks horribly ugly, but the compiler can optimize it totally,
> * as the count is constant.
> */
> static inline void * __constant_memcpy(void * to, const void * from, size_t n)
> {
> if (n <= 128)
> return __builtin_memcpy(to, from, n);
>
> #define COMMON(x) \
> __asm__ __volatile__( \
> "rep ; movsl" \
> x \
> : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
> : "0" (n/4),"1" ((long) to),"2" ((long) from) \
> : "memory");
> {
> int d0, d1, d2;
> switch (n % 4) {
> case 0: COMMON(""); return to;
> case 1: COMMON("\n\tmovsb"); return to;
> case 2: COMMON("\n\tmovsw"); return to;
> default: COMMON("\n\tmovsw\n\tmovsb"); return to;
> }
> }
>
> #undef COMMON
> }
>
> #define memcpy(t, f, n) \
> (__builtin_constant_p(n) ? \
> __constant_memcpy((t),(f),(n)) : \
> __memcpy((t),(f),(n)))
>
> int f3(char *a, char *b) { memcpy(a,b,3); }
The problem is that in GCC < 4.0 there is no constant propagation
pass before expanding builtin functions, so the __builtin_memcpy
call above sees a variable rather than a constant.
Either use GCC 4.0+, where this works just fine, or move the
n <= 128 case into the macro:
#define memcpy(t, f, n) \
(__builtin_constant_p(n) ? \
((n) <= 128 ? __builtin_memcpy(t,f,n) : __constant_memcpy(t,f,n) : \
__memcpy(t,f,n))
Jakub
next prev parent reply other threads:[~2005-03-29 15:17 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-03-29 14:37 Denis Vlasenko
2005-03-29 15:06 ` Richard Guenther
2005-03-29 15:08 ` Nathan Sidwell
2005-03-29 15:13 ` Jakub Jelinek [this message]
2005-03-29 15:42 ` Andrew Pinski
2005-03-30 2:27 ` Gerold Jury
2005-03-30 6:15 ` Denis Vlasenko
2005-04-01 21:43 ` Jan Hubicka
2005-04-02 12:18 ` Denis Vlasenko
2005-04-02 12:26 ` Denis Vlasenko
2005-04-05 16:34 ` [BUG mm] "fixed" i386 memcpy inlining buggy Christophe Saout
2005-04-06 10:14 ` Denis Vlasenko
2005-04-06 11:05 ` Dave Korn
2005-04-06 11:13 ` Dave Korn
2005-04-06 11:53 ` Dave Korn
2005-04-06 11:56 ` Dave Korn
2005-04-06 13:18 ` Richard B. Johnson
2005-04-06 14:16 ` Denis Vlasenko
2005-04-06 12:05 ` Christophe Saout
2005-04-06 12:36 ` Andrew Haley
2005-04-06 15:18 ` Paolo Bonzini
2005-04-06 16:11 ` Denis Vlasenko
2005-03-29 20:22 ` [PATCH] fix i386 memcpy Denis Vlasenko
2005-03-29 20:24 ` Denis Vlasenko
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=20050329151305.GQ4961@sunsite.mff.cuni.cz \
--to=jakub@redhat.com \
--cc=gcc@gcc.gnu.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vda@ilport.com.ua \
/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
all inboxes | Powered by JetHome®