1 [committed] Fix target/17565: asms in delay slots
3 * From: Richard Sandiford <rsandifo at redhat dot com>
4 * To: gcc-patches at gcc dot gnu dot org
5 * Date: Mon, 20 Sep 2004 07:55:58 +0100
6 * Subject: [committed] Fix target/17565: asms in delay slots
8 The MIPS port was allowing asms to be put into delay slots if the
9 compiler guesses they are only one instruction long. This is wrong
10 because of the possibility of it containing macros.
12 The problem can be reproduced as an assembler warning
13 in the following testcase:
17 register int k asm ("$16") = n;
21 asm ("li %0,0x12345678" : "=r" (k));
26 because the multi-instruction asm statement goes into the delay
27 slot of the call to bar().
29 This is reduced from a much more serious linux problem. Linux is fond
30 of using empty asm statements, and since gcc estimates empty asms to be
31 one instruction long, they too might be put into delay slots. This
32 actually has the effect of putting the following instruction into the
33 delay slot instead. Since there's no assembler warning, the problem was
34 only detected as a run-time failure.
36 The fix is simple: set the asm value of "can_delay" to "no".
37 Tested on mipsisa64-elf, applied to mainline.
39 This problem goes back to at least 2.95, so it isn't technically a
40 regression. On the other hand, it's the kind of bug that could trigger
41 for different types of code in different releases, so I'm sure there's
42 a testcase that fails (say) in 3.4 and not in 2.95. Will probably ask
43 Mark for permission to backport to 3.4.
49 * config/mips/mips.md (define_asm_attributes): Set can_delay to no.
52 * gcc.target/mips/asm-1.c: New test.
54 Index: config/mips/mips.md
55 ===================================================================
56 RCS file: /cvs/gcc/gcc/gcc/config/mips/mips.md,v
57 retrieving revision 1.306
58 diff -c -p -F^\([(a-zA-Z0-9_]\|#define\) -r1.306 mips.md
59 *** gcc/gcc/config/mips/mips.md 13 Sep 2004 19:32:05 -0000 1.306
60 --- gcc/gcc/config/mips/mips.md 20 Sep 2004 06:52:31 -0000
61 *************** (define_attr "may_clobber_hilo" "no,yes"
64 ;; Describe a user's asm statement.
65 (define_asm_attributes
66 ! [(set_attr "type" "multi")])
68 ;; .........................
72 ;; Describe a user's asm statement.
73 (define_asm_attributes
74 ! [(set_attr "type" "multi")
75 ! (set_attr "can_delay" "no")])
77 ;; .........................
79 Index: testsuite/gcc.target/mips/asm-1.c
80 ===================================================================
81 RCS file: testsuite/gcc.target/mips/asm-1.c
82 diff -N testsuite/gcc.target/mips/asm-1.c
83 *** gcc/gcc/testsuite/gcc.target/mips/asm-1.c 1 Jan 1970 00:00:00 -0000
84 --- gcc/gcc/testsuite/gcc.target/mips/asm-1.c 20 Sep 2004 06:52:31 -0000
88 + /* PR target/17565. GCC used to put the asm into the delay slot
90 + /* { dg-do assemble } */
91 + /* { dg-options "-O" } */
94 + register int k asm ("$16") = n;
98 + asm ("li %0,0x12345678" : "=r" (k));