public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Liu Hao <lh_mouse@126.com>
To: Fredrik Noring <noring@nocrew.org>,
	Alexander Monakov <amonakov@ispras.ru>
Cc: gcc-help@gcc.gnu.org
Subject: Re: m68k: Simple loop compiles into boundless recursion with -O2
Date: Fri, 15 Jan 2021 10:23:13 +0800	[thread overview]
Message-ID: <3cc02e02-f193-fe54-6071-e56d3a9fa37e@126.com> (raw)
In-Reply-To: <X/8gK9JtEvr4jQtt@sx9>


[-- Attachment #1.1: Type: text/plain, Size: 1204 bytes --]

在 2021/1/14 上午12:30, Fredrik Noring 写道:
> Many thanks, Alexander,
> 
>> Please invoke objdump with -dr instead to see the relocations.
> 
> Indeed:
> 
>   1a:	4eb9 0000 0000 	jsr 0 <memset2>
> 			1c: R_68K_32	memset
> 
>> The relocation associated with this instruction should point to memset.
>> Most likely the compiler is optimizing your memset2 function to call
>> the standard function 'memset'.
>>
>> When implementing memset itself you need to pass -ffreestanding to GCC,
>> which will disable this optimization.
> 

I used to run into the same issue around CRT code on x86. Use of `-ffreestanding` disables a number
of optimizations, for example, the compiler cannot optimize

    int data[4];
    memset(&data, 0, sizeof(data));

to a series of store operations, but leave it as a function call, which is rather overkill.


The issue in the original post can be resolved by writing through a pointer to `volatile char` like
this:

    void *memset2(void *s, int c, unsigned int n)
    {
        volatile char *b = s;
        for (unsigned int i = 0; i < n; i++)
            b[i] = c;
        return s;
    }




-- 
Best regards,
LH_Mouse


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2021-01-15  2:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-13 16:01 Fredrik Noring
2021-01-13 16:09 ` Alexander Monakov
2021-01-13 16:30   ` Fredrik Noring
2021-01-13 19:20     ` Segher Boessenkool
2021-01-13 19:53       ` Fredrik Noring
2021-01-13 21:46         ` Segher Boessenkool
2021-01-13 21:54           ` AW: " Stefan Franke
2021-01-14 14:54           ` Fredrik Noring
2021-01-14 15:05             ` Alexander Monakov
2021-01-14 15:09               ` Alexander Monakov
2021-01-14 15:15                 ` AW: " Stefan Franke
2021-01-14 15:33                   ` Alexander Monakov
2021-01-14 15:56                     ` AW: " Stefan Franke
2021-01-15  2:23     ` Liu Hao [this message]
2021-01-15  6:23       ` Fredrik Noring
2021-01-13 16:23 ` AW: " Stefan Franke
2021-01-13 17:27   ` Fredrik Noring

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=3cc02e02-f193-fe54-6071-e56d3a9fa37e@126.com \
    --to=lh_mouse@126.com \
    --cc=amonakov@ispras.ru \
    --cc=gcc-help@gcc.gnu.org \
    --cc=noring@nocrew.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).