From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ste-pvt-msa2.bahnhof.se (ste-pvt-msa2.bahnhof.se [213.80.101.71]) by sourceware.org (Postfix) with ESMTPS id 487FB385783D for ; Wed, 13 Jan 2021 16:01:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 487FB385783D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nocrew.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=noring@nocrew.org Received: from localhost (localhost [127.0.0.1]) by ste-pvt-msa2.bahnhof.se (Postfix) with ESMTP id 6269A3F61C for ; Wed, 13 Jan 2021 17:01:27 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at bahnhof.se X-Spam-Score: -1.901 X-Spam-Level: X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=no autolearn_force=no version=3.4.2 Received: from ste-pvt-msa2.bahnhof.se ([127.0.0.1]) by localhost (ste-ftg-msa2.bahnhof.se [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3Wq8iolJDuJr for ; Wed, 13 Jan 2021 17:01:26 +0100 (CET) Received: by ste-pvt-msa2.bahnhof.se (Postfix) with ESMTPA id CC12E3F433 for ; Wed, 13 Jan 2021 17:01:26 +0100 (CET) Date: Wed, 13 Jan 2021 17:01:37 +0100 From: Fredrik Noring To: gcc-help@gcc.gnu.org Subject: m68k: Simple loop compiles into boundless recursion with -O2 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jan 2021 16:01:50 -0000 Hi, Compiler used is GCC m68k-elf version 10.2.0. A variant of the classic memset void *memset2(void *s, int c, unsigned int n) { char *b = s; for (unsigned int i = 0; i < n; i++) b[i] = c; return s; } compiles into boundless recursion with O2 optimisation and the m68k-elf target. This will, of course, exhaust the stack and crash badly. The commands m68k-elf-gcc -O2 -march=68000 -c -o memset2.o memset2.c m68k-elf-objdump -d memset2.o produce 00000000 : 0: 2f02 movel %d2,%sp@- 2: 242f 0008 movel %sp@(8),%d2 6: 202f 0010 movel %sp@(16),%d0 a: 6718 beqs 24 c: 2f00 movel %d0,%sp@- e: 102f 0013 moveb %sp@(19),%d0 12: 4880 extw %d0 14: 3040 moveaw %d0,%a0 16: 2f08 movel %a0,%sp@- 18: 2f02 movel %d2,%sp@- 1a: 4eb9 0000 0000 jsr 0 /* <<<--- recursion */ 20: 4fef 000c lea %sp@(12),%sp 24: 2002 movel %d2,%d0 26: 241f movel %sp@+,%d2 28: 4e75 rts O1 optimisation is more reasonable, as it instead produces 00000000 : 0: 2f02 movel %d2,%sp@- 2: 202f 0008 movel %sp@(8),%d0 6: 242f 000c movel %sp@(12),%d2 a: 4aaf 0010 tstl %sp@(16) e: 670e beqs 1e 10: 2040 moveal %d0,%a0 12: 222f 0010 movel %sp@(16),%d1 16: d280 addl %d0,%d1 18: 10c2 moveb %d2,%a0@+ 1a: b288 cmpl %a0,%d1 1c: 66fa bnes 18 1e: 241f movel %sp@+,%d2 20: 4e75 rts The machine code with O2 looks like a plain compiler bug to me. What to do? Fredrik