]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/m68k/m68k.c
re PR tree-optimization/44667 (ICE: verify_stmts failed: non-trivial conversion at...
[thirdparty/gcc.git] / gcc / config / m68k / m68k.c
CommitLineData
79e68feb 1/* Subroutines for insn-output.c for Motorola 68000 family.
8636be86 2 Copyright (C) 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
7b0f476d 3 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4592bdcb 4 Free Software Foundation, Inc.
79e68feb 5
7ec022b2 6This file is part of GCC.
79e68feb 7
7ec022b2 8GCC is free software; you can redistribute it and/or modify
79e68feb 9it under the terms of the GNU General Public License as published by
2f83c7d6 10the Free Software Foundation; either version 3, or (at your option)
79e68feb
RS
11any later version.
12
7ec022b2 13GCC is distributed in the hope that it will be useful,
79e68feb
RS
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
2f83c7d6
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
79e68feb 21
79e68feb 22#include "config.h"
f5220a5d 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
da932f04 26#include "tree.h"
79e68feb 27#include "rtl.h"
49ad7cfa 28#include "function.h"
79e68feb
RS
29#include "regs.h"
30#include "hard-reg-set.h"
79e68feb
RS
31#include "insn-config.h"
32#include "conditions.h"
79e68feb
RS
33#include "output.h"
34#include "insn-attr.h"
1d8eaa6b 35#include "recog.h"
f5220a5d 36#include "toplev.h"
6d5f49b2
RH
37#include "expr.h"
38#include "reload.h"
5505f548 39#include "tm_p.h"
672a6f42
NB
40#include "target.h"
41#include "target-def.h"
2cc07db4 42#include "debug.h"
79e68feb 43#include "flags.h"
6fb5fa3c 44#include "df.h"
b8c96320
MK
45/* ??? Need to add a dependency between m68k.o and sched-int.h. */
46#include "sched-int.h"
47#include "insn-codes.h"
75df395f 48#include "ggc.h"
79e68feb 49
a4e9467d
RZ
50enum reg_class regno_reg_class[] =
51{
52 DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
53 DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
54 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
55 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
56 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
57 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
58 ADDR_REGS
59};
60
61
a40ed0f3
KH
62/* The minimum number of integer registers that we want to save with the
63 movem instruction. Using two movel instructions instead of a single
64 moveml is about 15% faster for the 68020 and 68030 at no expense in
65 code size. */
66#define MIN_MOVEM_REGS 3
67
68/* The minimum number of floating point registers that we want to save
69 with the fmovem instruction. */
70#define MIN_FMOVEM_REGS 1
71
ff482c8d 72/* Structure describing stack frame layout. */
3d74bc09
BI
73struct m68k_frame
74{
75 /* Stack pointer to frame pointer offset. */
48ed72a4 76 HOST_WIDE_INT offset;
3d74bc09
BI
77
78 /* Offset of FPU registers. */
79 HOST_WIDE_INT foffset;
80
81 /* Frame size in bytes (rounded up). */
48ed72a4 82 HOST_WIDE_INT size;
3d74bc09
BI
83
84 /* Data and address register. */
48ed72a4
PB
85 int reg_no;
86 unsigned int reg_mask;
3d74bc09
BI
87
88 /* FPU registers. */
48ed72a4
PB
89 int fpu_no;
90 unsigned int fpu_mask;
3d74bc09
BI
91
92 /* Offsets relative to ARG_POINTER. */
48ed72a4
PB
93 HOST_WIDE_INT frame_pointer_offset;
94 HOST_WIDE_INT stack_pointer_offset;
3d74bc09
BI
95
96 /* Function which the above information refers to. */
97 int funcdef_no;
48ed72a4
PB
98};
99
3d74bc09
BI
100/* Current frame information calculated by m68k_compute_frame_layout(). */
101static struct m68k_frame current_frame;
102
fc2241eb
RS
103/* Structure describing an m68k address.
104
105 If CODE is UNKNOWN, the address is BASE + INDEX * SCALE + OFFSET,
106 with null fields evaluating to 0. Here:
107
108 - BASE satisfies m68k_legitimate_base_reg_p
109 - INDEX satisfies m68k_legitimate_index_reg_p
110 - OFFSET satisfies m68k_legitimate_constant_address_p
111
112 INDEX is either HImode or SImode. The other fields are SImode.
113
114 If CODE is PRE_DEC, the address is -(BASE). If CODE is POST_INC,
115 the address is (BASE)+. */
116struct m68k_address {
117 enum rtx_code code;
118 rtx base;
119 rtx index;
120 rtx offset;
121 int scale;
122};
123
b8c96320 124static int m68k_sched_adjust_cost (rtx, rtx, rtx, int);
96fcacb7 125static int m68k_sched_issue_rate (void);
b8c96320
MK
126static int m68k_sched_variable_issue (FILE *, int, rtx, int);
127static void m68k_sched_md_init_global (FILE *, int, int);
128static void m68k_sched_md_finish_global (FILE *, int);
129static void m68k_sched_md_init (FILE *, int, int);
130static void m68k_sched_dfa_pre_advance_cycle (void);
131static void m68k_sched_dfa_post_advance_cycle (void);
96fcacb7 132static int m68k_sched_first_cycle_multipass_dfa_lookahead (void);
b8c96320 133
7b5cbb57 134static bool m68k_can_eliminate (const int, const int);
c6c3dba9 135static bool m68k_legitimate_address_p (enum machine_mode, rtx, bool);
4af06170 136static bool m68k_handle_option (size_t, const char *, int);
8a4a2253
BI
137static rtx find_addr_reg (rtx);
138static const char *singlemove_string (rtx *);
8a4a2253
BI
139static void m68k_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
140 HOST_WIDE_INT, tree);
8636be86 141static rtx m68k_struct_value_rtx (tree, int);
48ed72a4
PB
142static tree m68k_handle_fndecl_attribute (tree *node, tree name,
143 tree args, int flags,
144 bool *no_add_attrs);
3d74bc09 145static void m68k_compute_frame_layout (void);
48ed72a4 146static bool m68k_save_reg (unsigned int regno, bool interrupt_handler);
f7e70894 147static bool m68k_ok_for_sibcall_p (tree, tree);
75df395f 148static bool m68k_tls_symbol_p (rtx);
506d7b68 149static rtx m68k_legitimize_address (rtx, rtx, enum machine_mode);
f40751dd 150static bool m68k_rtx_costs (rtx, int, int, int *, bool);
1c445f03 151#if M68K_HONOR_TARGET_STRICT_ALIGNMENT
511e41e5 152static bool m68k_return_in_memory (const_tree, const_tree);
1c445f03 153#endif
75df395f 154static void m68k_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
e0601576 155static void m68k_trampoline_init (rtx, tree, rtx);
7b0f476d 156static rtx m68k_delegitimize_address (rtx);
79e68feb
RS
157\f
158
a2ef3db7 159/* Specify the identification number of the library being built */
4af06170 160const char *m68k_library_id_string = "_current_shared_library_a5_offset_";
672a6f42
NB
161\f
162/* Initialize the GCC target structure. */
301d03af
RS
163
164#if INT_OP_GROUP == INT_OP_DOT_WORD
165#undef TARGET_ASM_ALIGNED_HI_OP
166#define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
167#endif
168
169#if INT_OP_GROUP == INT_OP_NO_DOT
170#undef TARGET_ASM_BYTE_OP
171#define TARGET_ASM_BYTE_OP "\tbyte\t"
172#undef TARGET_ASM_ALIGNED_HI_OP
173#define TARGET_ASM_ALIGNED_HI_OP "\tshort\t"
174#undef TARGET_ASM_ALIGNED_SI_OP
175#define TARGET_ASM_ALIGNED_SI_OP "\tlong\t"
176#endif
177
178#if INT_OP_GROUP == INT_OP_DC
179#undef TARGET_ASM_BYTE_OP
180#define TARGET_ASM_BYTE_OP "\tdc.b\t"
181#undef TARGET_ASM_ALIGNED_HI_OP
182#define TARGET_ASM_ALIGNED_HI_OP "\tdc.w\t"
183#undef TARGET_ASM_ALIGNED_SI_OP
184#define TARGET_ASM_ALIGNED_SI_OP "\tdc.l\t"
185#endif
186
187#undef TARGET_ASM_UNALIGNED_HI_OP
188#define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
189#undef TARGET_ASM_UNALIGNED_SI_OP
190#define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
191
c590b625
RH
192#undef TARGET_ASM_OUTPUT_MI_THUNK
193#define TARGET_ASM_OUTPUT_MI_THUNK m68k_output_mi_thunk
bdabc150 194#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3101faab 195#define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
c590b625 196
1bc7c5b6
ZW
197#undef TARGET_ASM_FILE_START_APP_OFF
198#define TARGET_ASM_FILE_START_APP_OFF true
199
506d7b68
PB
200#undef TARGET_LEGITIMIZE_ADDRESS
201#define TARGET_LEGITIMIZE_ADDRESS m68k_legitimize_address
202
b8c96320
MK
203#undef TARGET_SCHED_ADJUST_COST
204#define TARGET_SCHED_ADJUST_COST m68k_sched_adjust_cost
205
96fcacb7
MK
206#undef TARGET_SCHED_ISSUE_RATE
207#define TARGET_SCHED_ISSUE_RATE m68k_sched_issue_rate
208
b8c96320
MK
209#undef TARGET_SCHED_VARIABLE_ISSUE
210#define TARGET_SCHED_VARIABLE_ISSUE m68k_sched_variable_issue
211
212#undef TARGET_SCHED_INIT_GLOBAL
213#define TARGET_SCHED_INIT_GLOBAL m68k_sched_md_init_global
214
215#undef TARGET_SCHED_FINISH_GLOBAL
216#define TARGET_SCHED_FINISH_GLOBAL m68k_sched_md_finish_global
217
218#undef TARGET_SCHED_INIT
219#define TARGET_SCHED_INIT m68k_sched_md_init
220
221#undef TARGET_SCHED_DFA_PRE_ADVANCE_CYCLE
222#define TARGET_SCHED_DFA_PRE_ADVANCE_CYCLE m68k_sched_dfa_pre_advance_cycle
223
224#undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
225#define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE m68k_sched_dfa_post_advance_cycle
226
96fcacb7
MK
227#undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
228#define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
229 m68k_sched_first_cycle_multipass_dfa_lookahead
230
4af06170
RS
231#undef TARGET_HANDLE_OPTION
232#define TARGET_HANDLE_OPTION m68k_handle_option
233
3c50106f
RH
234#undef TARGET_RTX_COSTS
235#define TARGET_RTX_COSTS m68k_rtx_costs
236
48ed72a4
PB
237#undef TARGET_ATTRIBUTE_TABLE
238#define TARGET_ATTRIBUTE_TABLE m68k_attribute_table
239
8636be86 240#undef TARGET_PROMOTE_PROTOTYPES
586de218 241#define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
8636be86
KH
242
243#undef TARGET_STRUCT_VALUE_RTX
244#define TARGET_STRUCT_VALUE_RTX m68k_struct_value_rtx
245
7ffb5e78
RS
246#undef TARGET_CANNOT_FORCE_CONST_MEM
247#define TARGET_CANNOT_FORCE_CONST_MEM m68k_illegitimate_symbolic_constant_p
248
f7e70894
RS
249#undef TARGET_FUNCTION_OK_FOR_SIBCALL
250#define TARGET_FUNCTION_OK_FOR_SIBCALL m68k_ok_for_sibcall_p
251
1c445f03
NS
252#if M68K_HONOR_TARGET_STRICT_ALIGNMENT
253#undef TARGET_RETURN_IN_MEMORY
254#define TARGET_RETURN_IN_MEMORY m68k_return_in_memory
255#endif
256
75df395f
MK
257#ifdef HAVE_AS_TLS
258#undef TARGET_HAVE_TLS
259#define TARGET_HAVE_TLS (true)
260
261#undef TARGET_ASM_OUTPUT_DWARF_DTPREL
262#define TARGET_ASM_OUTPUT_DWARF_DTPREL m68k_output_dwarf_dtprel
263#endif
264
c6c3dba9
PB
265#undef TARGET_LEGITIMATE_ADDRESS_P
266#define TARGET_LEGITIMATE_ADDRESS_P m68k_legitimate_address_p
267
7b5cbb57
AS
268#undef TARGET_CAN_ELIMINATE
269#define TARGET_CAN_ELIMINATE m68k_can_eliminate
270
e0601576
RH
271#undef TARGET_TRAMPOLINE_INIT
272#define TARGET_TRAMPOLINE_INIT m68k_trampoline_init
273
7b0f476d
AS
274#undef TARGET_DELEGITIMIZE_ADDRESS
275#define TARGET_DELEGITIMIZE_ADDRESS m68k_delegitimize_address
276
48ed72a4
PB
277static const struct attribute_spec m68k_attribute_table[] =
278{
279 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2bccb817 280 { "interrupt", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
48ed72a4 281 { "interrupt_handler", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
a4242737 282 { "interrupt_thread", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
48ed72a4
PB
283 { NULL, 0, 0, false, false, false, NULL }
284};
285
f6897b10 286struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 287\f
900ec02d
JB
288/* Base flags for 68k ISAs. */
289#define FL_FOR_isa_00 FL_ISA_68000
290#define FL_FOR_isa_10 (FL_FOR_isa_00 | FL_ISA_68010)
291/* FL_68881 controls the default setting of -m68881. gcc has traditionally
292 generated 68881 code for 68020 and 68030 targets unless explicitly told
293 not to. */
294#define FL_FOR_isa_20 (FL_FOR_isa_10 | FL_ISA_68020 \
295 | FL_BITFIELD | FL_68881)
296#define FL_FOR_isa_40 (FL_FOR_isa_20 | FL_ISA_68040)
297#define FL_FOR_isa_cpu32 (FL_FOR_isa_10 | FL_ISA_68020)
298
299/* Base flags for ColdFire ISAs. */
300#define FL_FOR_isa_a (FL_COLDFIRE | FL_ISA_A)
301#define FL_FOR_isa_aplus (FL_FOR_isa_a | FL_ISA_APLUS | FL_CF_USP)
302/* Note ISA_B doesn't necessarily include USP (user stack pointer) support. */
303#define FL_FOR_isa_b (FL_FOR_isa_a | FL_ISA_B | FL_CF_HWDIV)
4e2b26aa 304/* ISA_C is not upwardly compatible with ISA_B. */
8c5c99dc 305#define FL_FOR_isa_c (FL_FOR_isa_a | FL_ISA_C | FL_CF_USP)
900ec02d
JB
306
307enum m68k_isa
308{
309 /* Traditional 68000 instruction sets. */
310 isa_00,
311 isa_10,
312 isa_20,
313 isa_40,
314 isa_cpu32,
315 /* ColdFire instruction set variants. */
316 isa_a,
317 isa_aplus,
318 isa_b,
319 isa_c,
320 isa_max
321};
322
323/* Information about one of the -march, -mcpu or -mtune arguments. */
324struct m68k_target_selection
325{
326 /* The argument being described. */
327 const char *name;
328
329 /* For -mcpu, this is the device selected by the option.
330 For -mtune and -march, it is a representative device
331 for the microarchitecture or ISA respectively. */
332 enum target_device device;
333
334 /* The M68K_DEVICE fields associated with DEVICE. See the comment
335 in m68k-devices.def for details. FAMILY is only valid for -mcpu. */
336 const char *family;
337 enum uarch_type microarch;
338 enum m68k_isa isa;
339 unsigned long flags;
340};
341
342/* A list of all devices in m68k-devices.def. Used for -mcpu selection. */
343static const struct m68k_target_selection all_devices[] =
344{
345#define M68K_DEVICE(NAME,ENUM_VALUE,FAMILY,MULTILIB,MICROARCH,ISA,FLAGS) \
346 { NAME, ENUM_VALUE, FAMILY, u##MICROARCH, ISA, FLAGS | FL_FOR_##ISA },
347#include "m68k-devices.def"
348#undef M68K_DEVICE
349 { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
350};
351
352/* A list of all ISAs, mapping each one to a representative device.
353 Used for -march selection. */
354static const struct m68k_target_selection all_isas[] =
355{
356 { "68000", m68000, NULL, u68000, isa_00, FL_FOR_isa_00 },
357 { "68010", m68010, NULL, u68010, isa_10, FL_FOR_isa_10 },
358 { "68020", m68020, NULL, u68020, isa_20, FL_FOR_isa_20 },
359 { "68030", m68030, NULL, u68030, isa_20, FL_FOR_isa_20 },
360 { "68040", m68040, NULL, u68040, isa_40, FL_FOR_isa_40 },
361 { "68060", m68060, NULL, u68060, isa_40, FL_FOR_isa_40 },
362 { "cpu32", cpu32, NULL, ucpu32, isa_20, FL_FOR_isa_cpu32 },
363 { "isaa", mcf5206e, NULL, ucfv2, isa_a, (FL_FOR_isa_a
364 | FL_CF_HWDIV) },
365 { "isaaplus", mcf5271, NULL, ucfv2, isa_aplus, (FL_FOR_isa_aplus
366 | FL_CF_HWDIV) },
367 { "isab", mcf5407, NULL, ucfv4, isa_b, FL_FOR_isa_b },
8c5c99dc
MK
368 { "isac", unk_device, NULL, ucfv4, isa_c, (FL_FOR_isa_c
369 | FL_CF_HWDIV) },
900ec02d
JB
370 { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
371};
372
373/* A list of all microarchitectures, mapping each one to a representative
374 device. Used for -mtune selection. */
375static const struct m68k_target_selection all_microarchs[] =
376{
377 { "68000", m68000, NULL, u68000, isa_00, FL_FOR_isa_00 },
378 { "68010", m68010, NULL, u68010, isa_10, FL_FOR_isa_10 },
379 { "68020", m68020, NULL, u68020, isa_20, FL_FOR_isa_20 },
380 { "68020-40", m68020, NULL, u68020_40, isa_20, FL_FOR_isa_20 },
381 { "68020-60", m68020, NULL, u68020_60, isa_20, FL_FOR_isa_20 },
382 { "68030", m68030, NULL, u68030, isa_20, FL_FOR_isa_20 },
383 { "68040", m68040, NULL, u68040, isa_40, FL_FOR_isa_40 },
384 { "68060", m68060, NULL, u68060, isa_40, FL_FOR_isa_40 },
385 { "cpu32", cpu32, NULL, ucpu32, isa_20, FL_FOR_isa_cpu32 },
8c5c99dc 386 { "cfv1", mcf51qe, NULL, ucfv1, isa_c, FL_FOR_isa_c },
900ec02d
JB
387 { "cfv2", mcf5206, NULL, ucfv2, isa_a, FL_FOR_isa_a },
388 { "cfv3", mcf5307, NULL, ucfv3, isa_a, (FL_FOR_isa_a
389 | FL_CF_HWDIV) },
390 { "cfv4", mcf5407, NULL, ucfv4, isa_b, FL_FOR_isa_b },
391 { "cfv4e", mcf547x, NULL, ucfv4e, isa_b, (FL_FOR_isa_b
392 | FL_CF_USP
393 | FL_CF_EMAC
394 | FL_CF_FPU) },
395 { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
396};
397\f
398/* The entries associated with the -mcpu, -march and -mtune settings,
399 or null for options that have not been used. */
400const struct m68k_target_selection *m68k_cpu_entry;
401const struct m68k_target_selection *m68k_arch_entry;
402const struct m68k_target_selection *m68k_tune_entry;
403
404/* Which CPU we are generating code for. */
405enum target_device m68k_cpu;
406
407/* Which microarchitecture to tune for. */
408enum uarch_type m68k_tune;
409
410/* Which FPU to use. */
411enum fpu_type m68k_fpu;
4af06170 412
900ec02d
JB
413/* The set of FL_* flags that apply to the target processor. */
414unsigned int m68k_cpu_flags;
29ca003a 415
03b3e271
KH
416/* The set of FL_* flags that apply to the processor to be tuned for. */
417unsigned int m68k_tune_flags;
418
29ca003a
RS
419/* Asm templates for calling or jumping to an arbitrary symbolic address,
420 or NULL if such calls or jumps are not supported. The address is held
421 in operand 0. */
422const char *m68k_symbolic_call;
423const char *m68k_symbolic_jump;
c47b0cb4
MK
424
425/* Enum variable that corresponds to m68k_symbolic_call values. */
426enum M68K_SYMBOLIC_CALL m68k_symbolic_call_var;
427
900ec02d
JB
428\f
429/* See whether TABLE has an entry with name NAME. Return true and
430 store the entry in *ENTRY if so, otherwise return false and
431 leave *ENTRY alone. */
432
433static bool
434m68k_find_selection (const struct m68k_target_selection **entry,
435 const struct m68k_target_selection *table,
436 const char *name)
437{
438 size_t i;
439
440 for (i = 0; table[i].name; i++)
441 if (strcmp (table[i].name, name) == 0)
442 {
443 *entry = table + i;
444 return true;
445 }
446 return false;
447}
4af06170
RS
448
449/* Implement TARGET_HANDLE_OPTION. */
450
451static bool
452m68k_handle_option (size_t code, const char *arg, int value)
453{
454 switch (code)
455 {
900ec02d
JB
456 case OPT_march_:
457 return m68k_find_selection (&m68k_arch_entry, all_isas, arg);
458
459 case OPT_mcpu_:
460 return m68k_find_selection (&m68k_cpu_entry, all_devices, arg);
461
462 case OPT_mtune_:
463 return m68k_find_selection (&m68k_tune_entry, all_microarchs, arg);
464
4af06170 465 case OPT_m5200:
900ec02d 466 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5206");
4af06170
RS
467
468 case OPT_m5206e:
900ec02d 469 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5206e");
4af06170
RS
470
471 case OPT_m528x:
900ec02d 472 return m68k_find_selection (&m68k_cpu_entry, all_devices, "528x");
4af06170
RS
473
474 case OPT_m5307:
900ec02d 475 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5307");
4af06170
RS
476
477 case OPT_m5407:
900ec02d 478 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5407");
4af06170 479
dcc21c4c 480 case OPT_mcfv4e:
900ec02d 481 return m68k_find_selection (&m68k_cpu_entry, all_devices, "547x");
dcc21c4c 482
4af06170
RS
483 case OPT_m68000:
484 case OPT_mc68000:
900ec02d 485 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68000");
4af06170 486
3197c489 487 case OPT_m68010:
900ec02d 488 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68010");
3197c489 489
4af06170
RS
490 case OPT_m68020:
491 case OPT_mc68020:
900ec02d 492 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68020");
4af06170
RS
493
494 case OPT_m68020_40:
900ec02d
JB
495 return (m68k_find_selection (&m68k_tune_entry, all_microarchs,
496 "68020-40")
497 && m68k_find_selection (&m68k_cpu_entry, all_devices, "68020"));
4af06170
RS
498
499 case OPT_m68020_60:
900ec02d
JB
500 return (m68k_find_selection (&m68k_tune_entry, all_microarchs,
501 "68020-60")
502 && m68k_find_selection (&m68k_cpu_entry, all_devices, "68020"));
4af06170
RS
503
504 case OPT_m68030:
900ec02d 505 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68030");
4af06170
RS
506
507 case OPT_m68040:
900ec02d 508 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68040");
4af06170
RS
509
510 case OPT_m68060:
900ec02d 511 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68060");
4af06170
RS
512
513 case OPT_m68302:
900ec02d 514 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68302");
4af06170
RS
515
516 case OPT_m68332:
517 case OPT_mcpu32:
900ec02d 518 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68332");
4af06170
RS
519
520 case OPT_mshared_library_id_:
521 if (value > MAX_LIBRARY_ID)
522 error ("-mshared-library-id=%s is not between 0 and %d",
523 arg, MAX_LIBRARY_ID);
524 else
5ead67f6
KG
525 {
526 char *tmp;
527 asprintf (&tmp, "%d", (value * -4) - 4);
528 m68k_library_id_string = tmp;
529 }
4af06170
RS
530 return true;
531
532 default:
533 return true;
534 }
535}
536
ef1dbfb0
RK
537/* Sometimes certain combinations of command options do not make
538 sense on a particular target machine. You can define a macro
539 `OVERRIDE_OPTIONS' to take account of this. This macro, if
540 defined, is executed once just after all the command options have
541 been parsed.
542
543 Don't use this macro to turn on various extra optimizations for
544 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
545
546void
8a4a2253 547override_options (void)
ef1dbfb0 548{
900ec02d
JB
549 const struct m68k_target_selection *entry;
550 unsigned long target_mask;
551
552 /* User can choose:
553
554 -mcpu=
555 -march=
556 -mtune=
557
558 -march=ARCH should generate code that runs any processor
559 implementing architecture ARCH. -mcpu=CPU should override -march
560 and should generate code that runs on processor CPU, making free
561 use of any instructions that CPU understands. -mtune=UARCH applies
9f5ed61a 562 on top of -mcpu or -march and optimizes the code for UARCH. It does
900ec02d
JB
563 not change the target architecture. */
564 if (m68k_cpu_entry)
565 {
566 /* Complain if the -march setting is for a different microarchitecture,
567 or includes flags that the -mcpu setting doesn't. */
568 if (m68k_arch_entry
569 && (m68k_arch_entry->microarch != m68k_cpu_entry->microarch
570 || (m68k_arch_entry->flags & ~m68k_cpu_entry->flags) != 0))
571 warning (0, "-mcpu=%s conflicts with -march=%s",
572 m68k_cpu_entry->name, m68k_arch_entry->name);
573
574 entry = m68k_cpu_entry;
575 }
576 else
577 entry = m68k_arch_entry;
578
579 if (!entry)
580 entry = all_devices + TARGET_CPU_DEFAULT;
581
582 m68k_cpu_flags = entry->flags;
583
584 /* Use the architecture setting to derive default values for
585 certain flags. */
586 target_mask = 0;
8785d88c
KH
587
588 /* ColdFire is lenient about alignment. */
589 if (!TARGET_COLDFIRE)
590 target_mask |= MASK_STRICT_ALIGNMENT;
591
900ec02d
JB
592 if ((m68k_cpu_flags & FL_BITFIELD) != 0)
593 target_mask |= MASK_BITFIELD;
594 if ((m68k_cpu_flags & FL_CF_HWDIV) != 0)
595 target_mask |= MASK_CF_HWDIV;
596 if ((m68k_cpu_flags & (FL_68881 | FL_CF_FPU)) != 0)
597 target_mask |= MASK_HARD_FLOAT;
598 target_flags |= target_mask & ~target_flags_explicit;
599
600 /* Set the directly-usable versions of the -mcpu and -mtune settings. */
601 m68k_cpu = entry->device;
602 if (m68k_tune_entry)
03b3e271
KH
603 {
604 m68k_tune = m68k_tune_entry->microarch;
605 m68k_tune_flags = m68k_tune_entry->flags;
606 }
900ec02d
JB
607#ifdef M68K_DEFAULT_TUNE
608 else if (!m68k_cpu_entry && !m68k_arch_entry)
03b3e271
KH
609 {
610 enum target_device dev;
611 dev = all_microarchs[M68K_DEFAULT_TUNE].device;
612 m68k_tune_flags = all_devices[dev]->flags;
613 }
900ec02d
JB
614#endif
615 else
03b3e271
KH
616 {
617 m68k_tune = entry->microarch;
618 m68k_tune_flags = entry->flags;
619 }
900ec02d
JB
620
621 /* Set the type of FPU. */
622 m68k_fpu = (!TARGET_HARD_FLOAT ? FPUTYPE_NONE
623 : (m68k_cpu_flags & FL_COLDFIRE) != 0 ? FPUTYPE_COLDFIRE
624 : FPUTYPE_68881);
625
a2ef3db7
BI
626 /* Sanity check to ensure that msep-data and mid-sahred-library are not
627 * both specified together. Doing so simply doesn't make sense.
628 */
629 if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
630 error ("cannot specify both -msep-data and -mid-shared-library");
631
632 /* If we're generating code for a separate A5 relative data segment,
633 * we've got to enable -fPIC as well. This might be relaxable to
634 * -fpic but it hasn't been tested properly.
635 */
636 if (TARGET_SEP_DATA || TARGET_ID_SHARED_LIBRARY)
637 flag_pic = 2;
638
abe92a04
RS
639 /* -mpcrel -fPIC uses 32-bit pc-relative displacements. Raise an
640 error if the target does not support them. */
641 if (TARGET_PCREL && !TARGET_68020 && flag_pic == 2)
642 error ("-mpcrel -fPIC is not currently supported on selected cpu");
adf2ac37
RH
643
644 /* ??? A historic way of turning on pic, or is this intended to
645 be an embedded thing that doesn't have the same name binding
646 significance that it does on hosted ELF systems? */
647 if (TARGET_PCREL && flag_pic == 0)
648 flag_pic = 1;
649
29ca003a
RS
650 if (!flag_pic)
651 {
c47b0cb4
MK
652 m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_JSR;
653
29ca003a 654 m68k_symbolic_jump = "jra %a0";
29ca003a
RS
655 }
656 else if (TARGET_ID_SHARED_LIBRARY)
657 /* All addresses must be loaded from the GOT. */
658 ;
4e2b26aa 659 else if (TARGET_68020 || TARGET_ISAB || TARGET_ISAC)
29ca003a
RS
660 {
661 if (TARGET_PCREL)
c47b0cb4 662 m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_BSR_C;
4e2b26aa 663 else
c47b0cb4
MK
664 m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_BSR_P;
665
4e2b26aa
NS
666 if (TARGET_ISAC)
667 /* No unconditional long branch */;
668 else if (TARGET_PCREL)
da398bb5 669 m68k_symbolic_jump = "bra%.l %c0";
29ca003a 670 else
da398bb5 671 m68k_symbolic_jump = "bra%.l %p0";
29ca003a
RS
672 /* Turn off function cse if we are doing PIC. We always want
673 function call to be done as `bsr foo@PLTPC'. */
674 /* ??? It's traditional to do this for -mpcrel too, but it isn't
675 clear how intentional that is. */
676 flag_no_function_cse = 1;
677 }
adf2ac37 678
c47b0cb4
MK
679 switch (m68k_symbolic_call_var)
680 {
681 case M68K_SYMBOLIC_CALL_JSR:
c47b0cb4 682 m68k_symbolic_call = "jsr %a0";
c47b0cb4
MK
683 break;
684
685 case M68K_SYMBOLIC_CALL_BSR_C:
da398bb5 686 m68k_symbolic_call = "bsr%.l %c0";
c47b0cb4
MK
687 break;
688
689 case M68K_SYMBOLIC_CALL_BSR_P:
da398bb5 690 m68k_symbolic_call = "bsr%.l %p0";
c47b0cb4
MK
691 break;
692
693 case M68K_SYMBOLIC_CALL_NONE:
694 gcc_assert (m68k_symbolic_call == NULL);
695 break;
696
697 default:
698 gcc_unreachable ();
699 }
700
aaca7021
RZ
701#ifndef ASM_OUTPUT_ALIGN_WITH_NOP
702 if (align_labels > 2)
703 {
704 warning (0, "-falign-labels=%d is not supported", align_labels);
705 align_labels = 0;
706 }
707 if (align_loops > 2)
708 {
709 warning (0, "-falign-loops=%d is not supported", align_loops);
710 align_loops = 0;
711 }
712#endif
713
adf2ac37 714 SUBTARGET_OVERRIDE_OPTIONS;
c47b0cb4
MK
715
716 /* Setup scheduling options. */
826fadba
MK
717 if (TUNE_CFV1)
718 m68k_sched_cpu = CPU_CFV1;
719 else if (TUNE_CFV2)
720 m68k_sched_cpu = CPU_CFV2;
721 else if (TUNE_CFV3)
722 m68k_sched_cpu = CPU_CFV3;
96fcacb7
MK
723 else if (TUNE_CFV4)
724 m68k_sched_cpu = CPU_CFV4;
c47b0cb4
MK
725 else
726 {
727 m68k_sched_cpu = CPU_UNKNOWN;
728 flag_schedule_insns = 0;
729 flag_schedule_insns_after_reload = 0;
730 flag_modulo_sched = 0;
731 }
826fadba
MK
732
733 if (m68k_sched_cpu != CPU_UNKNOWN)
734 {
735 if ((m68k_cpu_flags & (FL_CF_EMAC | FL_CF_EMAC_B)) != 0)
736 m68k_sched_mac = MAC_CF_EMAC;
737 else if ((m68k_cpu_flags & FL_CF_MAC) != 0)
738 m68k_sched_mac = MAC_CF_MAC;
739 else
740 m68k_sched_mac = MAC_NO;
741 }
ef1dbfb0 742}
7eb4f044
NS
743
744/* Generate a macro of the form __mPREFIX_cpu_NAME, where PREFIX is the
745 given argument and NAME is the argument passed to -mcpu. Return NULL
746 if -mcpu was not passed. */
747
748const char *
749m68k_cpp_cpu_ident (const char *prefix)
750{
751 if (!m68k_cpu_entry)
752 return NULL;
753 return concat ("__m", prefix, "_cpu_", m68k_cpu_entry->name, NULL);
754}
755
756/* Generate a macro of the form __mPREFIX_family_NAME, where PREFIX is the
757 given argument and NAME is the name of the representative device for
758 the -mcpu argument's family. Return NULL if -mcpu was not passed. */
759
760const char *
761m68k_cpp_cpu_family (const char *prefix)
762{
763 if (!m68k_cpu_entry)
764 return NULL;
765 return concat ("__m", prefix, "_family_", m68k_cpu_entry->family, NULL);
766}
79e68feb 767\f
2bccb817
KH
768/* Return m68k_fk_interrupt_handler if FUNC has an "interrupt" or
769 "interrupt_handler" attribute and interrupt_thread if FUNC has an
770 "interrupt_thread" attribute. Otherwise, return
771 m68k_fk_normal_function. */
a4242737
KH
772
773enum m68k_function_kind
774m68k_get_function_kind (tree func)
48ed72a4
PB
775{
776 tree a;
777
fa157b28
NS
778 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
779
2bccb817
KH
780 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
781 if (a != NULL_TREE)
782 return m68k_fk_interrupt_handler;
783
48ed72a4 784 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
a4242737
KH
785 if (a != NULL_TREE)
786 return m68k_fk_interrupt_handler;
787
788 a = lookup_attribute ("interrupt_thread", DECL_ATTRIBUTES (func));
789 if (a != NULL_TREE)
790 return m68k_fk_interrupt_thread;
791
792 return m68k_fk_normal_function;
48ed72a4
PB
793}
794
795/* Handle an attribute requiring a FUNCTION_DECL; arguments as in
796 struct attribute_spec.handler. */
797static tree
798m68k_handle_fndecl_attribute (tree *node, tree name,
799 tree args ATTRIBUTE_UNUSED,
800 int flags ATTRIBUTE_UNUSED,
801 bool *no_add_attrs)
802{
803 if (TREE_CODE (*node) != FUNCTION_DECL)
804 {
29d08eba
JM
805 warning (OPT_Wattributes, "%qE attribute only applies to functions",
806 name);
48ed72a4
PB
807 *no_add_attrs = true;
808 }
809
a4242737
KH
810 if (m68k_get_function_kind (*node) != m68k_fk_normal_function)
811 {
812 error ("multiple interrupt attributes not allowed");
813 *no_add_attrs = true;
814 }
815
816 if (!TARGET_FIDOA
817 && !strcmp (IDENTIFIER_POINTER (name), "interrupt_thread"))
818 {
819 error ("interrupt_thread is available only on fido");
820 *no_add_attrs = true;
821 }
822
48ed72a4
PB
823 return NULL_TREE;
824}
860c4900
BI
825
826static void
3d74bc09 827m68k_compute_frame_layout (void)
860c4900
BI
828{
829 int regno, saved;
a40ed0f3 830 unsigned int mask;
a4242737
KH
831 enum m68k_function_kind func_kind =
832 m68k_get_function_kind (current_function_decl);
833 bool interrupt_handler = func_kind == m68k_fk_interrupt_handler;
834 bool interrupt_thread = func_kind == m68k_fk_interrupt_thread;
860c4900 835
3d74bc09
BI
836 /* Only compute the frame once per function.
837 Don't cache information until reload has been completed. */
838 if (current_frame.funcdef_no == current_function_funcdef_no
839 && reload_completed)
840 return;
841
842 current_frame.size = (get_frame_size () + 3) & -4;
860c4900 843
a40ed0f3 844 mask = saved = 0;
a4242737
KH
845
846 /* Interrupt thread does not need to save any register. */
847 if (!interrupt_thread)
848 for (regno = 0; regno < 16; regno++)
849 if (m68k_save_reg (regno, interrupt_handler))
850 {
851 mask |= 1 << (regno - D0_REG);
852 saved++;
853 }
3d74bc09
BI
854 current_frame.offset = saved * 4;
855 current_frame.reg_no = saved;
856 current_frame.reg_mask = mask;
860c4900 857
57047680 858 current_frame.foffset = 0;
a40ed0f3 859 mask = saved = 0;
dcc21c4c 860 if (TARGET_HARD_FLOAT)
860c4900 861 {
a4242737
KH
862 /* Interrupt thread does not need to save any register. */
863 if (!interrupt_thread)
864 for (regno = 16; regno < 24; regno++)
865 if (m68k_save_reg (regno, interrupt_handler))
866 {
867 mask |= 1 << (regno - FP0_REG);
868 saved++;
869 }
dcc21c4c 870 current_frame.foffset = saved * TARGET_FP_REG_SIZE;
3d74bc09 871 current_frame.offset += current_frame.foffset;
860c4900 872 }
57047680
GN
873 current_frame.fpu_no = saved;
874 current_frame.fpu_mask = mask;
3d74bc09
BI
875
876 /* Remember what function this frame refers to. */
877 current_frame.funcdef_no = current_function_funcdef_no;
860c4900
BI
878}
879
7b5cbb57
AS
880/* Worker function for TARGET_CAN_ELIMINATE. */
881
882bool
883m68k_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
884{
885 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
886}
887
860c4900
BI
888HOST_WIDE_INT
889m68k_initial_elimination_offset (int from, int to)
890{
42b67c06
PB
891 int argptr_offset;
892 /* The arg pointer points 8 bytes before the start of the arguments,
893 as defined by FIRST_PARM_OFFSET. This makes it coincident with the
894 frame pointer in most frames. */
895 argptr_offset = frame_pointer_needed ? 0 : UNITS_PER_WORD;
860c4900 896 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
42b67c06 897 return argptr_offset;
860c4900 898
3d74bc09 899 m68k_compute_frame_layout ();
860c4900 900
4761e388
NS
901 gcc_assert (to == STACK_POINTER_REGNUM);
902 switch (from)
903 {
a0a7fbc9 904 case ARG_POINTER_REGNUM:
42b67c06 905 return current_frame.offset + current_frame.size - argptr_offset;
4761e388
NS
906 case FRAME_POINTER_REGNUM:
907 return current_frame.offset + current_frame.size;
908 default:
909 gcc_unreachable ();
910 }
860c4900
BI
911}
912
97c55091
GN
913/* Refer to the array `regs_ever_live' to determine which registers
914 to save; `regs_ever_live[I]' is nonzero if register number I
915 is ever used in the function. This function is responsible for
916 knowing which registers should not be saved even if used.
917 Return true if we need to save REGNO. */
918
48ed72a4
PB
919static bool
920m68k_save_reg (unsigned int regno, bool interrupt_handler)
2cff4a6e 921{
4ab870f5 922 if (flag_pic && regno == PIC_REG)
b86ba8a3 923 {
e3b5732b 924 if (crtl->saves_all_registers)
afcb440c 925 return true;
e3b5732b 926 if (crtl->uses_pic_offset_table)
b86ba8a3 927 return true;
6357eb0d
RS
928 /* Reload may introduce constant pool references into a function
929 that thitherto didn't need a PIC register. Note that the test
930 above will not catch that case because we will only set
e3b5732b 931 crtl->uses_pic_offset_table when emitting
6357eb0d 932 the address reloads. */
e3b5732b 933 if (crtl->uses_const_pool)
6357eb0d 934 return true;
b86ba8a3 935 }
2cff4a6e 936
e3b5732b 937 if (crtl->calls_eh_return)
2cff4a6e
AS
938 {
939 unsigned int i;
940 for (i = 0; ; i++)
941 {
942 unsigned int test = EH_RETURN_DATA_REGNO (i);
943 if (test == INVALID_REGNUM)
944 break;
945 if (test == regno)
48ed72a4 946 return true;
2cff4a6e
AS
947 }
948 }
949
48ed72a4
PB
950 /* Fixed regs we never touch. */
951 if (fixed_regs[regno])
952 return false;
953
954 /* The frame pointer (if it is such) is handled specially. */
955 if (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)
956 return false;
957
958 /* Interrupt handlers must also save call_used_regs
959 if they are live or when calling nested functions. */
960 if (interrupt_handler)
a0a7fbc9 961 {
6fb5fa3c 962 if (df_regs_ever_live_p (regno))
a0a7fbc9 963 return true;
48ed72a4 964
a0a7fbc9
AS
965 if (!current_function_is_leaf && call_used_regs[regno])
966 return true;
967 }
48ed72a4
PB
968
969 /* Never need to save registers that aren't touched. */
6fb5fa3c 970 if (!df_regs_ever_live_p (regno))
48ed72a4
PB
971 return false;
972
b2e08ed4 973 /* Otherwise save everything that isn't call-clobbered. */
48ed72a4 974 return !call_used_regs[regno];
2cff4a6e
AS
975}
976
a40ed0f3
KH
977/* Emit RTL for a MOVEM or FMOVEM instruction. BASE + OFFSET represents
978 the lowest memory address. COUNT is the number of registers to be
979 moved, with register REGNO + I being moved if bit I of MASK is set.
980 STORE_P specifies the direction of the move and ADJUST_STACK_P says
981 whether or not this is pre-decrement (if STORE_P) or post-increment
982 (if !STORE_P) operation. */
983
984static rtx
985m68k_emit_movem (rtx base, HOST_WIDE_INT offset,
986 unsigned int count, unsigned int regno,
987 unsigned int mask, bool store_p, bool adjust_stack_p)
988{
989 int i;
990 rtx body, addr, src, operands[2];
991 enum machine_mode mode;
992
993 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (adjust_stack_p + count));
994 mode = reg_raw_mode[regno];
995 i = 0;
996
997 if (adjust_stack_p)
998 {
999 src = plus_constant (base, (count
1000 * GET_MODE_SIZE (mode)
1001 * (HOST_WIDE_INT) (store_p ? -1 : 1)));
1002 XVECEXP (body, 0, i++) = gen_rtx_SET (VOIDmode, base, src);
1003 }
1004
1005 for (; mask != 0; mask >>= 1, regno++)
1006 if (mask & 1)
1007 {
1008 addr = plus_constant (base, offset);
1009 operands[!store_p] = gen_frame_mem (mode, addr);
1010 operands[store_p] = gen_rtx_REG (mode, regno);
1011 XVECEXP (body, 0, i++)
1012 = gen_rtx_SET (VOIDmode, operands[0], operands[1]);
1013 offset += GET_MODE_SIZE (mode);
1014 }
1015 gcc_assert (i == XVECLEN (body, 0));
1016
1017 return emit_insn (body);
1018}
1019
1020/* Make INSN a frame-related instruction. */
79e68feb 1021
08c148a8 1022static void
a40ed0f3
KH
1023m68k_set_frame_related (rtx insn)
1024{
1025 rtx body;
1026 int i;
1027
1028 RTX_FRAME_RELATED_P (insn) = 1;
1029 body = PATTERN (insn);
1030 if (GET_CODE (body) == PARALLEL)
1031 for (i = 0; i < XVECLEN (body, 0); i++)
1032 RTX_FRAME_RELATED_P (XVECEXP (body, 0, i)) = 1;
1033}
1034
1035/* Emit RTL for the "prologue" define_expand. */
1036
1037void
1038m68k_expand_prologue (void)
79e68feb 1039{
860c4900 1040 HOST_WIDE_INT fsize_with_regs;
a40ed0f3 1041 rtx limit, src, dest, insn;
3d74bc09 1042
a40ed0f3 1043 m68k_compute_frame_layout ();
3d74bc09 1044
a157febd
GK
1045 /* If the stack limit is a symbol, we can check it here,
1046 before actually allocating the space. */
e3b5732b 1047 if (crtl->limit_stack
a157febd 1048 && GET_CODE (stack_limit_rtx) == SYMBOL_REF)
a40ed0f3
KH
1049 {
1050 limit = plus_constant (stack_limit_rtx, current_frame.size + 4);
1051 if (!LEGITIMATE_CONSTANT_P (limit))
1052 {
1053 emit_move_insn (gen_rtx_REG (Pmode, D0_REG), limit);
1054 limit = gen_rtx_REG (Pmode, D0_REG);
1055 }
f90b7a5a
PB
1056 emit_insn (gen_ctrapsi4 (gen_rtx_LTU (VOIDmode,
1057 stack_pointer_rtx, limit),
1058 stack_pointer_rtx, limit,
1059 const1_rtx));
a40ed0f3 1060 }
79e68feb 1061
a89e3f21 1062 fsize_with_regs = current_frame.size;
dcc21c4c
PB
1063 if (TARGET_COLDFIRE)
1064 {
a40ed0f3
KH
1065 /* ColdFire's move multiple instructions do not allow pre-decrement
1066 addressing. Add the size of movem saves to the initial stack
1067 allocation instead. */
1068 if (current_frame.reg_no >= MIN_MOVEM_REGS)
1069 fsize_with_regs += current_frame.reg_no * GET_MODE_SIZE (SImode);
1070 if (current_frame.fpu_no >= MIN_FMOVEM_REGS)
1071 fsize_with_regs += current_frame.fpu_no * GET_MODE_SIZE (DFmode);
dcc21c4c 1072 }
860c4900 1073
79e68feb
RS
1074 if (frame_pointer_needed)
1075 {
a40ed0f3 1076 if (fsize_with_regs == 0 && TUNE_68040)
79e68feb 1077 {
a40ed0f3
KH
1078 /* On the 68040, two separate moves are faster than link.w 0. */
1079 dest = gen_frame_mem (Pmode,
1080 gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx));
1081 m68k_set_frame_related (emit_move_insn (dest, frame_pointer_rtx));
1082 m68k_set_frame_related (emit_move_insn (frame_pointer_rtx,
1083 stack_pointer_rtx));
79e68feb 1084 }
a40ed0f3
KH
1085 else if (fsize_with_regs < 0x8000 || TARGET_68020)
1086 m68k_set_frame_related
1087 (emit_insn (gen_link (frame_pointer_rtx,
1088 GEN_INT (-4 - fsize_with_regs))));
d9e88af0 1089 else
a40ed0f3
KH
1090 {
1091 m68k_set_frame_related
1092 (emit_insn (gen_link (frame_pointer_rtx, GEN_INT (-4))));
1093 m68k_set_frame_related
1094 (emit_insn (gen_addsi3 (stack_pointer_rtx,
1095 stack_pointer_rtx,
1096 GEN_INT (-fsize_with_regs))));
1097 }
96fcacb7
MK
1098
1099 /* If the frame pointer is needed, emit a special barrier that
1100 will prevent the scheduler from moving stores to the frame
1101 before the stack adjustment. */
1102 emit_insn (gen_stack_tie (stack_pointer_rtx, frame_pointer_rtx));
d9e88af0 1103 }
a40ed0f3
KH
1104 else if (fsize_with_regs != 0)
1105 m68k_set_frame_related
1106 (emit_insn (gen_addsi3 (stack_pointer_rtx,
1107 stack_pointer_rtx,
1108 GEN_INT (-fsize_with_regs))));
860c4900 1109
57047680 1110 if (current_frame.fpu_mask)
79e68feb 1111 {
a40ed0f3 1112 gcc_assert (current_frame.fpu_no >= MIN_FMOVEM_REGS);
dcc21c4c 1113 if (TARGET_68881)
a40ed0f3
KH
1114 m68k_set_frame_related
1115 (m68k_emit_movem (stack_pointer_rtx,
1116 current_frame.fpu_no * -GET_MODE_SIZE (XFmode),
1117 current_frame.fpu_no, FP0_REG,
1118 current_frame.fpu_mask, true, true));
dcc21c4c
PB
1119 else
1120 {
1121 int offset;
1122
a40ed0f3
KH
1123 /* If we're using moveml to save the integer registers,
1124 the stack pointer will point to the bottom of the moveml
1125 save area. Find the stack offset of the first FP register. */
1126 if (current_frame.reg_no < MIN_MOVEM_REGS)
dcc21c4c
PB
1127 offset = 0;
1128 else
a40ed0f3
KH
1129 offset = current_frame.reg_no * GET_MODE_SIZE (SImode);
1130 m68k_set_frame_related
1131 (m68k_emit_movem (stack_pointer_rtx, offset,
1132 current_frame.fpu_no, FP0_REG,
1133 current_frame.fpu_mask, true, false));
f277471f 1134 }
79e68feb 1135 }
99df2465 1136
01bbf777 1137 /* If the stack limit is not a symbol, check it here.
a157febd 1138 This has the disadvantage that it may be too late... */
e3b5732b 1139 if (crtl->limit_stack)
a157febd
GK
1140 {
1141 if (REG_P (stack_limit_rtx))
f90b7a5a
PB
1142 emit_insn (gen_ctrapsi4 (gen_rtx_LTU (VOIDmode, stack_pointer_rtx,
1143 stack_limit_rtx),
1144 stack_pointer_rtx, stack_limit_rtx,
1145 const1_rtx));
1146
a157febd 1147 else if (GET_CODE (stack_limit_rtx) != SYMBOL_REF)
d4ee4d25 1148 warning (0, "stack limit expression is not supported");
a157febd 1149 }
01bbf777 1150
a40ed0f3 1151 if (current_frame.reg_no < MIN_MOVEM_REGS)
79e68feb 1152 {
a40ed0f3 1153 /* Store each register separately in the same order moveml does. */
79e68feb
RS
1154 int i;
1155
a40ed0f3
KH
1156 for (i = 16; i-- > 0; )
1157 if (current_frame.reg_mask & (1 << i))
078e983e 1158 {
a40ed0f3
KH
1159 src = gen_rtx_REG (SImode, D0_REG + i);
1160 dest = gen_frame_mem (SImode,
1161 gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx));
1162 m68k_set_frame_related (emit_insn (gen_movsi (dest, src)));
078e983e 1163 }
79e68feb 1164 }
a40ed0f3 1165 else
79e68feb 1166 {
9425fb04 1167 if (TARGET_COLDFIRE)
a40ed0f3
KH
1168 /* The required register save space has already been allocated.
1169 The first register should be stored at (%sp). */
1170 m68k_set_frame_related
1171 (m68k_emit_movem (stack_pointer_rtx, 0,
1172 current_frame.reg_no, D0_REG,
1173 current_frame.reg_mask, true, false));
afaff477 1174 else
a40ed0f3
KH
1175 m68k_set_frame_related
1176 (m68k_emit_movem (stack_pointer_rtx,
1177 current_frame.reg_no * -GET_MODE_SIZE (SImode),
1178 current_frame.reg_no, D0_REG,
1179 current_frame.reg_mask, true, true));
79e68feb 1180 }
a40ed0f3 1181
75df395f 1182 if (!TARGET_SEP_DATA
e3b5732b 1183 && crtl->uses_pic_offset_table)
6fb5fa3c 1184 insn = emit_insn (gen_load_got (pic_offset_table_rtx));
79e68feb
RS
1185}
1186\f
413ac1b2
RS
1187/* Return true if a simple (return) instruction is sufficient for this
1188 instruction (i.e. if no epilogue is needed). */
79e68feb 1189
3d74bc09 1190bool
a2bda628 1191m68k_use_return_insn (void)
79e68feb 1192{
79e68feb 1193 if (!reload_completed || frame_pointer_needed || get_frame_size () != 0)
3d74bc09 1194 return false;
125ed86f 1195
a0a7fbc9 1196 m68k_compute_frame_layout ();
413ac1b2 1197 return current_frame.offset == 0;
79e68feb
RS
1198}
1199
f7e70894
RS
1200/* Emit RTL for the "epilogue" or "sibcall_epilogue" define_expand;
1201 SIBCALL_P says which.
79e68feb
RS
1202
1203 The function epilogue should not depend on the current stack pointer!
1204 It should use the frame pointer only, if there is a frame pointer.
1205 This is mandatory because of alloca; we also take advantage of it to
1206 omit stack adjustments before returning. */
1207
a40ed0f3 1208void
f7e70894 1209m68k_expand_epilogue (bool sibcall_p)
08c148a8 1210{
3d74bc09 1211 HOST_WIDE_INT fsize, fsize_with_regs;
a40ed0f3 1212 bool big, restore_from_sp;
3d74bc09 1213
a0a7fbc9 1214 m68k_compute_frame_layout ();
3d74bc09 1215
3d74bc09 1216 fsize = current_frame.size;
a40ed0f3
KH
1217 big = false;
1218 restore_from_sp = false;
3d74bc09 1219
a40ed0f3 1220 /* FIXME : current_function_is_leaf below is too strong.
c67ddce5 1221 What we really need to know there is if there could be pending
7a1929e1 1222 stack adjustment needed at that point. */
a40ed0f3 1223 restore_from_sp = (!frame_pointer_needed
e3b5732b 1224 || (!cfun->calls_alloca
a40ed0f3 1225 && current_function_is_leaf));
860c4900
BI
1226
1227 /* fsize_with_regs is the size we need to adjust the sp when
97c55091 1228 popping the frame. */
860c4900 1229 fsize_with_regs = fsize;
dcc21c4c
PB
1230 if (TARGET_COLDFIRE && restore_from_sp)
1231 {
a40ed0f3
KH
1232 /* ColdFire's move multiple instructions do not allow post-increment
1233 addressing. Add the size of movem loads to the final deallocation
1234 instead. */
1235 if (current_frame.reg_no >= MIN_MOVEM_REGS)
1236 fsize_with_regs += current_frame.reg_no * GET_MODE_SIZE (SImode);
1237 if (current_frame.fpu_no >= MIN_FMOVEM_REGS)
1238 fsize_with_regs += current_frame.fpu_no * GET_MODE_SIZE (DFmode);
dcc21c4c 1239 }
860c4900 1240
3d74bc09 1241 if (current_frame.offset + fsize >= 0x8000
a40ed0f3 1242 && !restore_from_sp
3d74bc09 1243 && (current_frame.reg_mask || current_frame.fpu_mask))
79e68feb 1244 {
a40ed0f3
KH
1245 if (TARGET_COLDFIRE
1246 && (current_frame.reg_no >= MIN_MOVEM_REGS
1247 || current_frame.fpu_no >= MIN_FMOVEM_REGS))
1248 {
1249 /* ColdFire's move multiple instructions do not support the
1250 (d8,Ax,Xi) addressing mode, so we're as well using a normal
1251 stack-based restore. */
1252 emit_move_insn (gen_rtx_REG (Pmode, A1_REG),
1253 GEN_INT (-(current_frame.offset + fsize)));
1254 emit_insn (gen_addsi3 (stack_pointer_rtx,
1255 gen_rtx_REG (Pmode, A1_REG),
1256 frame_pointer_rtx));
1257 restore_from_sp = true;
1258 }
1259 else
1260 {
1261 emit_move_insn (gen_rtx_REG (Pmode, A1_REG), GEN_INT (-fsize));
1262 fsize = 0;
1263 big = true;
1264 }
79e68feb 1265 }
79e68feb 1266
a40ed0f3
KH
1267 if (current_frame.reg_no < MIN_MOVEM_REGS)
1268 {
1269 /* Restore each register separately in the same order moveml does. */
79e68feb 1270 int i;
a40ed0f3 1271 HOST_WIDE_INT offset;
79e68feb 1272
a40ed0f3 1273 offset = current_frame.offset + fsize;
3d74bc09
BI
1274 for (i = 0; i < 16; i++)
1275 if (current_frame.reg_mask & (1 << i))
79e68feb 1276 {
a40ed0f3
KH
1277 rtx addr;
1278
1279 if (big)
79e68feb 1280 {
a40ed0f3
KH
1281 /* Generate the address -OFFSET(%fp,%a1.l). */
1282 addr = gen_rtx_REG (Pmode, A1_REG);
1283 addr = gen_rtx_PLUS (Pmode, addr, frame_pointer_rtx);
1284 addr = plus_constant (addr, -offset);
79e68feb 1285 }
a40ed0f3
KH
1286 else if (restore_from_sp)
1287 addr = gen_rtx_POST_INC (Pmode, stack_pointer_rtx);
1288 else
1289 addr = plus_constant (frame_pointer_rtx, -offset);
1290 emit_move_insn (gen_rtx_REG (SImode, D0_REG + i),
1291 gen_frame_mem (SImode, addr));
1292 offset -= GET_MODE_SIZE (SImode);
1293 }
79e68feb 1294 }
3d74bc09 1295 else if (current_frame.reg_mask)
79e68feb 1296 {
a40ed0f3
KH
1297 if (big)
1298 m68k_emit_movem (gen_rtx_PLUS (Pmode,
1299 gen_rtx_REG (Pmode, A1_REG),
1300 frame_pointer_rtx),
1301 -(current_frame.offset + fsize),
1302 current_frame.reg_no, D0_REG,
1303 current_frame.reg_mask, false, false);
1304 else if (restore_from_sp)
1305 m68k_emit_movem (stack_pointer_rtx, 0,
1306 current_frame.reg_no, D0_REG,
1307 current_frame.reg_mask, false,
1308 !TARGET_COLDFIRE);
1309 else
1310 m68k_emit_movem (frame_pointer_rtx,
1311 -(current_frame.offset + fsize),
1312 current_frame.reg_no, D0_REG,
1313 current_frame.reg_mask, false, false);
79e68feb 1314 }
a40ed0f3
KH
1315
1316 if (current_frame.fpu_no > 0)
79e68feb
RS
1317 {
1318 if (big)
a40ed0f3
KH
1319 m68k_emit_movem (gen_rtx_PLUS (Pmode,
1320 gen_rtx_REG (Pmode, A1_REG),
1321 frame_pointer_rtx),
1322 -(current_frame.foffset + fsize),
1323 current_frame.fpu_no, FP0_REG,
1324 current_frame.fpu_mask, false, false);
6910dd70 1325 else if (restore_from_sp)
79e68feb 1326 {
dcc21c4c
PB
1327 if (TARGET_COLDFIRE)
1328 {
1329 int offset;
1330
a40ed0f3
KH
1331 /* If we used moveml to restore the integer registers, the
1332 stack pointer will still point to the bottom of the moveml
1333 save area. Find the stack offset of the first FP
1334 register. */
1335 if (current_frame.reg_no < MIN_MOVEM_REGS)
dcc21c4c
PB
1336 offset = 0;
1337 else
a40ed0f3
KH
1338 offset = current_frame.reg_no * GET_MODE_SIZE (SImode);
1339 m68k_emit_movem (stack_pointer_rtx, offset,
1340 current_frame.fpu_no, FP0_REG,
1341 current_frame.fpu_mask, false, false);
dcc21c4c 1342 }
884b74f0 1343 else
a40ed0f3
KH
1344 m68k_emit_movem (stack_pointer_rtx, 0,
1345 current_frame.fpu_no, FP0_REG,
1346 current_frame.fpu_mask, false, true);
79e68feb
RS
1347 }
1348 else
a40ed0f3
KH
1349 m68k_emit_movem (frame_pointer_rtx,
1350 -(current_frame.foffset + fsize),
1351 current_frame.fpu_no, FP0_REG,
1352 current_frame.fpu_mask, false, false);
79e68feb 1353 }
a40ed0f3 1354
79e68feb 1355 if (frame_pointer_needed)
a40ed0f3 1356 emit_insn (gen_unlink (frame_pointer_rtx));
860c4900 1357 else if (fsize_with_regs)
a40ed0f3
KH
1358 emit_insn (gen_addsi3 (stack_pointer_rtx,
1359 stack_pointer_rtx,
1360 GEN_INT (fsize_with_regs)));
1361
e3b5732b 1362 if (crtl->calls_eh_return)
a40ed0f3
KH
1363 emit_insn (gen_addsi3 (stack_pointer_rtx,
1364 stack_pointer_rtx,
1365 EH_RETURN_STACKADJ_RTX));
1366
f7e70894 1367 if (!sibcall_p)
49570723 1368 emit_jump_insn (gen_rtx_RETURN (VOIDmode));
79e68feb
RS
1369}
1370\f
8a4a2253 1371/* Return true if X is a valid comparison operator for the dbcc
64a184e9
RS
1372 instruction.
1373
1374 Note it rejects floating point comparison operators.
1375 (In the future we could use Fdbcc).
1376
1377 It also rejects some comparisons when CC_NO_OVERFLOW is set. */
1378
1379int
41b6a5e2 1380valid_dbcc_comparison_p_2 (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
64a184e9 1381{
64a184e9
RS
1382 switch (GET_CODE (x))
1383 {
64a184e9
RS
1384 case EQ: case NE: case GTU: case LTU:
1385 case GEU: case LEU:
1386 return 1;
1387
1388 /* Reject some when CC_NO_OVERFLOW is set. This may be over
1389 conservative */
1390 case GT: case LT: case GE: case LE:
1391 return ! (cc_prev_status.flags & CC_NO_OVERFLOW);
1392 default:
1393 return 0;
1394 }
1395}
1396
a0ab749a 1397/* Return nonzero if flags are currently in the 68881 flag register. */
6a0f85e3 1398int
8a4a2253 1399flags_in_68881 (void)
6a0f85e3
TG
1400{
1401 /* We could add support for these in the future */
1402 return cc_status.flags & CC_IN_68881;
1403}
1404
db5e2d51
MK
1405/* Return true if PARALLEL contains register REGNO. */
1406static bool
1407m68k_reg_present_p (const_rtx parallel, unsigned int regno)
1408{
1409 int i;
1410
1411 if (REG_P (parallel) && REGNO (parallel) == regno)
1412 return true;
1413
1414 if (GET_CODE (parallel) != PARALLEL)
1415 return false;
1416
1417 for (i = 0; i < XVECLEN (parallel, 0); ++i)
1418 {
1419 const_rtx x;
1420
1421 x = XEXP (XVECEXP (parallel, 0, i), 0);
1422 if (REG_P (x) && REGNO (x) == regno)
1423 return true;
1424 }
1425
1426 return false;
1427}
1428
fa157b28 1429/* Implement TARGET_FUNCTION_OK_FOR_SIBCALL_P. */
f7e70894
RS
1430
1431static bool
fa157b28 1432m68k_ok_for_sibcall_p (tree decl, tree exp)
f7e70894 1433{
fa157b28
NS
1434 enum m68k_function_kind kind;
1435
1436 /* We cannot use sibcalls for nested functions because we use the
1437 static chain register for indirect calls. */
1438 if (CALL_EXPR_STATIC_CHAIN (exp))
1439 return false;
1440
db5e2d51
MK
1441 if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
1442 {
1443 /* Check that the return value locations are the same. For
1444 example that we aren't returning a value from the sibling in
1445 a D0 register but then need to transfer it to a A0 register. */
1446 rtx cfun_value;
1447 rtx call_value;
1448
1449 cfun_value = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (cfun->decl)),
1450 cfun->decl);
1451 call_value = FUNCTION_VALUE (TREE_TYPE (exp), decl);
1452
1453 /* Check that the values are equal or that the result the callee
1454 function returns is superset of what the current function returns. */
1455 if (!(rtx_equal_p (cfun_value, call_value)
1456 || (REG_P (cfun_value)
1457 && m68k_reg_present_p (call_value, REGNO (cfun_value)))))
1458 return false;
1459 }
1460
fa157b28
NS
1461 kind = m68k_get_function_kind (current_function_decl);
1462 if (kind == m68k_fk_normal_function)
1463 /* We can always sibcall from a normal function, because it's
1464 undefined if it is calling an interrupt function. */
1465 return true;
1466
1467 /* Otherwise we can only sibcall if the function kind is known to be
1468 the same. */
1469 if (decl && m68k_get_function_kind (decl) == kind)
1470 return true;
1471
1472 return false;
f7e70894
RS
1473}
1474
29ca003a
RS
1475/* Convert X to a legitimate function call memory reference and return the
1476 result. */
a2ef3db7 1477
29ca003a
RS
1478rtx
1479m68k_legitimize_call_address (rtx x)
1480{
1481 gcc_assert (MEM_P (x));
1482 if (call_operand (XEXP (x, 0), VOIDmode))
1483 return x;
1484 return replace_equiv_address (x, force_reg (Pmode, XEXP (x, 0)));
a2ef3db7
BI
1485}
1486
f7e70894
RS
1487/* Likewise for sibling calls. */
1488
1489rtx
1490m68k_legitimize_sibcall_address (rtx x)
1491{
1492 gcc_assert (MEM_P (x));
1493 if (sibcall_operand (XEXP (x, 0), VOIDmode))
1494 return x;
1495
1496 emit_move_insn (gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM), XEXP (x, 0));
1497 return replace_equiv_address (x, gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM));
1498}
1499
506d7b68
PB
1500/* Convert X to a legitimate address and return it if successful. Otherwise
1501 return X.
1502
1503 For the 68000, we handle X+REG by loading X into a register R and
1504 using R+REG. R will go in an address reg and indexing will be used.
1505 However, if REG is a broken-out memory address or multiplication,
1506 nothing needs to be done because REG can certainly go in an address reg. */
1507
ab7256e4 1508static rtx
506d7b68
PB
1509m68k_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
1510{
75df395f
MK
1511 if (m68k_tls_symbol_p (x))
1512 return m68k_legitimize_tls_address (x);
1513
506d7b68
PB
1514 if (GET_CODE (x) == PLUS)
1515 {
1516 int ch = (x) != (oldx);
1517 int copied = 0;
1518
1519#define COPY_ONCE(Y) if (!copied) { Y = copy_rtx (Y); copied = ch = 1; }
1520
1521 if (GET_CODE (XEXP (x, 0)) == MULT)
1522 {
1523 COPY_ONCE (x);
1524 XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
1525 }
1526 if (GET_CODE (XEXP (x, 1)) == MULT)
1527 {
1528 COPY_ONCE (x);
1529 XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
1530 }
1531 if (ch)
1532 {
1533 if (GET_CODE (XEXP (x, 1)) == REG
1534 && GET_CODE (XEXP (x, 0)) == REG)
1535 {
1536 if (TARGET_COLDFIRE_FPU && GET_MODE_CLASS (mode) == MODE_FLOAT)
1537 {
1538 COPY_ONCE (x);
1539 x = force_operand (x, 0);
1540 }
1541 return x;
1542 }
1543 if (memory_address_p (mode, x))
1544 return x;
1545 }
1546 if (GET_CODE (XEXP (x, 0)) == REG
1547 || (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
1548 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
1549 && GET_MODE (XEXP (XEXP (x, 0), 0)) == HImode))
1550 {
1551 rtx temp = gen_reg_rtx (Pmode);
1552 rtx val = force_operand (XEXP (x, 1), 0);
1553 emit_move_insn (temp, val);
1554 COPY_ONCE (x);
1555 XEXP (x, 1) = temp;
1556 if (TARGET_COLDFIRE_FPU && GET_MODE_CLASS (mode) == MODE_FLOAT
1557 && GET_CODE (XEXP (x, 0)) == REG)
1558 x = force_operand (x, 0);
1559 }
1560 else if (GET_CODE (XEXP (x, 1)) == REG
1561 || (GET_CODE (XEXP (x, 1)) == SIGN_EXTEND
1562 && GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
1563 && GET_MODE (XEXP (XEXP (x, 1), 0)) == HImode))
1564 {
1565 rtx temp = gen_reg_rtx (Pmode);
1566 rtx val = force_operand (XEXP (x, 0), 0);
1567 emit_move_insn (temp, val);
1568 COPY_ONCE (x);
1569 XEXP (x, 0) = temp;
1570 if (TARGET_COLDFIRE_FPU && GET_MODE_CLASS (mode) == MODE_FLOAT
1571 && GET_CODE (XEXP (x, 1)) == REG)
1572 x = force_operand (x, 0);
1573 }
1574 }
1575
1576 return x;
1577}
1578
1579
64a184e9
RS
1580/* Output a dbCC; jCC sequence. Note we do not handle the
1581 floating point version of this sequence (Fdbcc). We also
1582 do not handle alternative conditions when CC_NO_OVERFLOW is
6a0f85e3
TG
1583 set. It is assumed that valid_dbcc_comparison_p and flags_in_68881 will
1584 kick those out before we get here. */
64a184e9 1585
1d8eaa6b 1586void
8a4a2253 1587output_dbcc_and_branch (rtx *operands)
64a184e9 1588{
64a184e9
RS
1589 switch (GET_CODE (operands[3]))
1590 {
1591 case EQ:
da398bb5 1592 output_asm_insn ("dbeq %0,%l1\n\tjeq %l2", operands);
e6d98cb0 1593 break;
64a184e9
RS
1594
1595 case NE:
da398bb5 1596 output_asm_insn ("dbne %0,%l1\n\tjne %l2", operands);
e6d98cb0 1597 break;
64a184e9
RS
1598
1599 case GT:
da398bb5 1600 output_asm_insn ("dbgt %0,%l1\n\tjgt %l2", operands);
e6d98cb0 1601 break;
64a184e9
RS
1602
1603 case GTU:
da398bb5 1604 output_asm_insn ("dbhi %0,%l1\n\tjhi %l2", operands);
e6d98cb0 1605 break;
64a184e9
RS
1606
1607 case LT:
da398bb5 1608 output_asm_insn ("dblt %0,%l1\n\tjlt %l2", operands);
e6d98cb0 1609 break;
64a184e9
RS
1610
1611 case LTU:
da398bb5 1612 output_asm_insn ("dbcs %0,%l1\n\tjcs %l2", operands);
e6d98cb0 1613 break;
64a184e9
RS
1614
1615 case GE:
da398bb5 1616 output_asm_insn ("dbge %0,%l1\n\tjge %l2", operands);
e6d98cb0 1617 break;
64a184e9
RS
1618
1619 case GEU:
da398bb5 1620 output_asm_insn ("dbcc %0,%l1\n\tjcc %l2", operands);
e6d98cb0 1621 break;
64a184e9
RS
1622
1623 case LE:
da398bb5 1624 output_asm_insn ("dble %0,%l1\n\tjle %l2", operands);
e6d98cb0 1625 break;
64a184e9
RS
1626
1627 case LEU:
da398bb5 1628 output_asm_insn ("dbls %0,%l1\n\tjls %l2", operands);
e6d98cb0 1629 break;
64a184e9
RS
1630
1631 default:
4761e388 1632 gcc_unreachable ();
64a184e9
RS
1633 }
1634
1635 /* If the decrement is to be done in SImode, then we have
7a1929e1 1636 to compensate for the fact that dbcc decrements in HImode. */
64a184e9
RS
1637 switch (GET_MODE (operands[0]))
1638 {
1639 case SImode:
da398bb5 1640 output_asm_insn ("clr%.w %0\n\tsubq%.l #1,%0\n\tjpl %l1", operands);
64a184e9
RS
1641 break;
1642
1643 case HImode:
1644 break;
1645
1646 default:
4761e388 1647 gcc_unreachable ();
64a184e9
RS
1648 }
1649}
1650
5505f548 1651const char *
4761e388 1652output_scc_di (rtx op, rtx operand1, rtx operand2, rtx dest)
c59c3b1c
RK
1653{
1654 rtx loperands[7];
d9832fd2 1655 enum rtx_code op_code = GET_CODE (op);
c59c3b1c 1656
f710504c 1657 /* This does not produce a useful cc. */
906a2d3c
RK
1658 CC_STATUS_INIT;
1659
d9832fd2
RK
1660 /* The m68k cmp.l instruction requires operand1 to be a reg as used
1661 below. Swap the operands and change the op if these requirements
1662 are not fulfilled. */
1663 if (GET_CODE (operand2) == REG && GET_CODE (operand1) != REG)
1664 {
1665 rtx tmp = operand1;
1666
1667 operand1 = operand2;
1668 operand2 = tmp;
1669 op_code = swap_condition (op_code);
1670 }
c59c3b1c
RK
1671 loperands[0] = operand1;
1672 if (GET_CODE (operand1) == REG)
1d8eaa6b 1673 loperands[1] = gen_rtx_REG (SImode, REGNO (operand1) + 1);
c59c3b1c 1674 else
b72f00af 1675 loperands[1] = adjust_address (operand1, SImode, 4);
c59c3b1c
RK
1676 if (operand2 != const0_rtx)
1677 {
1678 loperands[2] = operand2;
1679 if (GET_CODE (operand2) == REG)
1d8eaa6b 1680 loperands[3] = gen_rtx_REG (SImode, REGNO (operand2) + 1);
c59c3b1c 1681 else
b72f00af 1682 loperands[3] = adjust_address (operand2, SImode, 4);
c59c3b1c 1683 }
428511bb 1684 loperands[4] = gen_label_rtx ();
c59c3b1c 1685 if (operand2 != const0_rtx)
da398bb5 1686 output_asm_insn ("cmp%.l %2,%0\n\tjne %l4\n\tcmp%.l %3,%1", loperands);
392582fa 1687 else
4a8c52e0 1688 {
9425fb04 1689 if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[0]))
4a8c52e0
AS
1690 output_asm_insn ("tst%.l %0", loperands);
1691 else
a0a7fbc9 1692 output_asm_insn ("cmp%.w #0,%0", loperands);
4a8c52e0 1693
da398bb5 1694 output_asm_insn ("jne %l4", loperands);
4a8c52e0 1695
9425fb04 1696 if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[1]))
4a8c52e0
AS
1697 output_asm_insn ("tst%.l %1", loperands);
1698 else
3b4b85c9 1699 output_asm_insn ("cmp%.w #0,%1", loperands);
4a8c52e0
AS
1700 }
1701
c59c3b1c 1702 loperands[5] = dest;
3b4b85c9 1703
d9832fd2 1704 switch (op_code)
c59c3b1c
RK
1705 {
1706 case EQ:
4977bab6 1707 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1708 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c
RK
1709 output_asm_insn ("seq %5", loperands);
1710 break;
1711
1712 case NE:
4977bab6 1713 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1714 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c
RK
1715 output_asm_insn ("sne %5", loperands);
1716 break;
1717
1718 case GT:
428511bb 1719 loperands[6] = gen_label_rtx ();
da398bb5 1720 output_asm_insn ("shi %5\n\tjra %l6", loperands);
4977bab6 1721 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1722 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c 1723 output_asm_insn ("sgt %5", loperands);
4977bab6 1724 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1725 CODE_LABEL_NUMBER (loperands[6]));
c59c3b1c
RK
1726 break;
1727
1728 case GTU:
4977bab6 1729 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1730 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c
RK
1731 output_asm_insn ("shi %5", loperands);
1732 break;
1733
1734 case LT:
428511bb 1735 loperands[6] = gen_label_rtx ();
da398bb5 1736 output_asm_insn ("scs %5\n\tjra %l6", loperands);
4977bab6 1737 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1738 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c 1739 output_asm_insn ("slt %5", loperands);
4977bab6 1740 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1741 CODE_LABEL_NUMBER (loperands[6]));
c59c3b1c
RK
1742 break;
1743
1744 case LTU:
4977bab6 1745 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1746 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c
RK
1747 output_asm_insn ("scs %5", loperands);
1748 break;
1749
1750 case GE:
428511bb 1751 loperands[6] = gen_label_rtx ();
da398bb5 1752 output_asm_insn ("scc %5\n\tjra %l6", loperands);
4977bab6 1753 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1754 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c 1755 output_asm_insn ("sge %5", loperands);
4977bab6 1756 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1757 CODE_LABEL_NUMBER (loperands[6]));
c59c3b1c
RK
1758 break;
1759
1760 case GEU:
4977bab6 1761 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1762 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c
RK
1763 output_asm_insn ("scc %5", loperands);
1764 break;
1765
1766 case LE:
428511bb 1767 loperands[6] = gen_label_rtx ();
da398bb5 1768 output_asm_insn ("sls %5\n\tjra %l6", loperands);
4977bab6 1769 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1770 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c 1771 output_asm_insn ("sle %5", loperands);
4977bab6 1772 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1773 CODE_LABEL_NUMBER (loperands[6]));
c59c3b1c
RK
1774 break;
1775
1776 case LEU:
4977bab6 1777 (*targetm.asm_out.internal_label) (asm_out_file, "L",
a0a7fbc9 1778 CODE_LABEL_NUMBER (loperands[4]));
c59c3b1c
RK
1779 output_asm_insn ("sls %5", loperands);
1780 break;
1781
1782 default:
4761e388 1783 gcc_unreachable ();
c59c3b1c
RK
1784 }
1785 return "";
1786}
1787
5505f548 1788const char *
8a4a2253 1789output_btst (rtx *operands, rtx countop, rtx dataop, rtx insn, int signpos)
79e68feb
RS
1790{
1791 operands[0] = countop;
1792 operands[1] = dataop;
1793
1794 if (GET_CODE (countop) == CONST_INT)
1795 {
1796 register int count = INTVAL (countop);
1797 /* If COUNT is bigger than size of storage unit in use,
1798 advance to the containing unit of same size. */
1799 if (count > signpos)
1800 {
1801 int offset = (count & ~signpos) / 8;
1802 count = count & signpos;
b72f00af 1803 operands[1] = dataop = adjust_address (dataop, QImode, offset);
79e68feb
RS
1804 }
1805 if (count == signpos)
1806 cc_status.flags = CC_NOT_POSITIVE | CC_Z_IN_NOT_N;
1807 else
1808 cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N;
1809
1810 /* These three statements used to use next_insns_test_no...
1811 but it appears that this should do the same job. */
1812 if (count == 31
1813 && next_insn_tests_no_inequality (insn))
1814 return "tst%.l %1";
1815 if (count == 15
1816 && next_insn_tests_no_inequality (insn))
1817 return "tst%.w %1";
1818 if (count == 7
1819 && next_insn_tests_no_inequality (insn))
1820 return "tst%.b %1";
5083912d
PDM
1821 /* Try to use `movew to ccr' followed by the appropriate branch insn.
1822 On some m68k variants unfortunately that's slower than btst.
1823 On 68000 and higher, that should also work for all HImode operands. */
1824 if (TUNE_CPU32 || TARGET_COLDFIRE || optimize_size)
1825 {
1826 if (count == 3 && DATA_REG_P (operands[1])
1827 && next_insn_tests_no_inequality (insn))
1828 {
1829 cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N | CC_NO_OVERFLOW;
1830 return "move%.w %1,%%ccr";
1831 }
1832 if (count == 2 && DATA_REG_P (operands[1])
1833 && next_insn_tests_no_inequality (insn))
1834 {
1835 cc_status.flags = CC_NOT_NEGATIVE | CC_INVERTED | CC_NO_OVERFLOW;
1836 return "move%.w %1,%%ccr";
1837 }
1838 /* count == 1 followed by bvc/bvs and
1839 count == 0 followed by bcc/bcs are also possible, but need
1840 m68k-specific CC_Z_IN_NOT_V and CC_Z_IN_NOT_C flags. */
1841 }
79e68feb
RS
1842
1843 cc_status.flags = CC_NOT_NEGATIVE;
1844 }
1845 return "btst %0,%1";
1846}
79e68feb 1847\f
fc2241eb
RS
1848/* Return true if X is a legitimate base register. STRICT_P says
1849 whether we need strict checking. */
1850
1851bool
1852m68k_legitimate_base_reg_p (rtx x, bool strict_p)
1853{
1854 /* Allow SUBREG everywhere we allow REG. This results in better code. */
1855 if (!strict_p && GET_CODE (x) == SUBREG)
1856 x = SUBREG_REG (x);
1857
1858 return (REG_P (x)
1859 && (strict_p
1860 ? REGNO_OK_FOR_BASE_P (REGNO (x))
bf32249e 1861 : REGNO_OK_FOR_BASE_NONSTRICT_P (REGNO (x))));
fc2241eb
RS
1862}
1863
1864/* Return true if X is a legitimate index register. STRICT_P says
1865 whether we need strict checking. */
1866
1867bool
1868m68k_legitimate_index_reg_p (rtx x, bool strict_p)
1869{
1870 if (!strict_p && GET_CODE (x) == SUBREG)
1871 x = SUBREG_REG (x);
1872
1873 return (REG_P (x)
1874 && (strict_p
1875 ? REGNO_OK_FOR_INDEX_P (REGNO (x))
bf32249e 1876 : REGNO_OK_FOR_INDEX_NONSTRICT_P (REGNO (x))));
fc2241eb
RS
1877}
1878
1879/* Return true if X is a legitimate index expression for a (d8,An,Xn) or
1880 (bd,An,Xn) addressing mode. Fill in the INDEX and SCALE fields of
1881 ADDRESS if so. STRICT_P says whether we need strict checking. */
1882
1883static bool
1884m68k_decompose_index (rtx x, bool strict_p, struct m68k_address *address)
1885{
1886 int scale;
1887
1888 /* Check for a scale factor. */
1889 scale = 1;
1890 if ((TARGET_68020 || TARGET_COLDFIRE)
1891 && GET_CODE (x) == MULT
1892 && GET_CODE (XEXP (x, 1)) == CONST_INT
1893 && (INTVAL (XEXP (x, 1)) == 2
1894 || INTVAL (XEXP (x, 1)) == 4
1895 || (INTVAL (XEXP (x, 1)) == 8
1896 && (TARGET_COLDFIRE_FPU || !TARGET_COLDFIRE))))
1897 {
1898 scale = INTVAL (XEXP (x, 1));
1899 x = XEXP (x, 0);
1900 }
1901
1902 /* Check for a word extension. */
1903 if (!TARGET_COLDFIRE
1904 && GET_CODE (x) == SIGN_EXTEND
1905 && GET_MODE (XEXP (x, 0)) == HImode)
1906 x = XEXP (x, 0);
1907
1908 if (m68k_legitimate_index_reg_p (x, strict_p))
1909 {
1910 address->scale = scale;
1911 address->index = x;
1912 return true;
1913 }
1914
1915 return false;
1916}
1917
7ffb5e78
RS
1918/* Return true if X is an illegitimate symbolic constant. */
1919
1920bool
1921m68k_illegitimate_symbolic_constant_p (rtx x)
1922{
1923 rtx base, offset;
1924
1925 if (M68K_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
1926 {
1927 split_const (x, &base, &offset);
1928 if (GET_CODE (base) == SYMBOL_REF
1929 && !offset_within_block_p (base, INTVAL (offset)))
1930 return true;
1931 }
75df395f 1932 return m68k_tls_reference_p (x, false);
7ffb5e78
RS
1933}
1934
fc2241eb
RS
1935/* Return true if X is a legitimate constant address that can reach
1936 bytes in the range [X, X + REACH). STRICT_P says whether we need
1937 strict checking. */
1938
1939static bool
1940m68k_legitimate_constant_address_p (rtx x, unsigned int reach, bool strict_p)
1941{
1942 rtx base, offset;
1943
1944 if (!CONSTANT_ADDRESS_P (x))
1945 return false;
1946
1947 if (flag_pic
1948 && !(strict_p && TARGET_PCREL)
1949 && symbolic_operand (x, VOIDmode))
1950 return false;
1951
1952 if (M68K_OFFSETS_MUST_BE_WITHIN_SECTIONS_P && reach > 1)
1953 {
1954 split_const (x, &base, &offset);
1955 if (GET_CODE (base) == SYMBOL_REF
1956 && !offset_within_block_p (base, INTVAL (offset) + reach - 1))
1957 return false;
1958 }
1959
75df395f 1960 return !m68k_tls_reference_p (x, false);
fc2241eb
RS
1961}
1962
1963/* Return true if X is a LABEL_REF for a jump table. Assume that unplaced
1964 labels will become jump tables. */
1965
1966static bool
1967m68k_jump_table_ref_p (rtx x)
1968{
1969 if (GET_CODE (x) != LABEL_REF)
1970 return false;
1971
1972 x = XEXP (x, 0);
1973 if (!NEXT_INSN (x) && !PREV_INSN (x))
1974 return true;
1975
1976 x = next_nonnote_insn (x);
1977 return x && JUMP_TABLE_DATA_P (x);
1978}
1979
1980/* Return true if X is a legitimate address for values of mode MODE.
1981 STRICT_P says whether strict checking is needed. If the address
1982 is valid, describe its components in *ADDRESS. */
1983
1984static bool
1985m68k_decompose_address (enum machine_mode mode, rtx x,
1986 bool strict_p, struct m68k_address *address)
1987{
1988 unsigned int reach;
1989
1990 memset (address, 0, sizeof (*address));
1991
1992 if (mode == BLKmode)
1993 reach = 1;
1994 else
1995 reach = GET_MODE_SIZE (mode);
1996
1997 /* Check for (An) (mode 2). */
1998 if (m68k_legitimate_base_reg_p (x, strict_p))
1999 {
2000 address->base = x;
2001 return true;
2002 }
2003
2004 /* Check for -(An) and (An)+ (modes 3 and 4). */
2005 if ((GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_INC)
2006 && m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p))
2007 {
2008 address->code = GET_CODE (x);
2009 address->base = XEXP (x, 0);
2010 return true;
2011 }
2012
2013 /* Check for (d16,An) (mode 5). */
2014 if (GET_CODE (x) == PLUS
2015 && GET_CODE (XEXP (x, 1)) == CONST_INT
2016 && IN_RANGE (INTVAL (XEXP (x, 1)), -0x8000, 0x8000 - reach)
2017 && m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p))
2018 {
2019 address->base = XEXP (x, 0);
2020 address->offset = XEXP (x, 1);
2021 return true;
2022 }
2023
2024 /* Check for GOT loads. These are (bd,An,Xn) addresses if
2025 TARGET_68020 && flag_pic == 2, otherwise they are (d16,An)
2026 addresses. */
75df395f
MK
2027 if (GET_CODE (x) == PLUS
2028 && XEXP (x, 0) == pic_offset_table_rtx)
fc2241eb 2029 {
75df395f
MK
2030 /* As we are processing a PLUS, do not unwrap RELOC32 symbols --
2031 they are invalid in this context. */
2032 if (m68k_unwrap_symbol (XEXP (x, 1), false) != XEXP (x, 1))
2033 {
2034 address->base = XEXP (x, 0);
2035 address->offset = XEXP (x, 1);
2036 return true;
2037 }
fc2241eb
RS
2038 }
2039
2040 /* The ColdFire FPU only accepts addressing modes 2-5. */
2041 if (TARGET_COLDFIRE_FPU && GET_MODE_CLASS (mode) == MODE_FLOAT)
2042 return false;
2043
2044 /* Check for (xxx).w and (xxx).l. Also, in the TARGET_PCREL case,
2045 check for (d16,PC) or (bd,PC,Xn) with a suppressed index register.
2046 All these modes are variations of mode 7. */
2047 if (m68k_legitimate_constant_address_p (x, reach, strict_p))
2048 {
2049 address->offset = x;
2050 return true;
2051 }
2052
2053 /* Check for (d8,PC,Xn), a mode 7 form. This case is needed for
2054 tablejumps.
2055
2056 ??? do_tablejump creates these addresses before placing the target
2057 label, so we have to assume that unplaced labels are jump table
2058 references. It seems unlikely that we would ever generate indexed
2059 accesses to unplaced labels in other cases. */
2060 if (GET_CODE (x) == PLUS
2061 && m68k_jump_table_ref_p (XEXP (x, 1))
2062 && m68k_decompose_index (XEXP (x, 0), strict_p, address))
2063 {
2064 address->offset = XEXP (x, 1);
2065 return true;
2066 }
2067
2068 /* Everything hereafter deals with (d8,An,Xn.SIZE*SCALE) or
2069 (bd,An,Xn.SIZE*SCALE) addresses. */
2070
2071 if (TARGET_68020)
2072 {
2073 /* Check for a nonzero base displacement. */
2074 if (GET_CODE (x) == PLUS
2075 && m68k_legitimate_constant_address_p (XEXP (x, 1), reach, strict_p))
2076 {
2077 address->offset = XEXP (x, 1);
2078 x = XEXP (x, 0);
2079 }
2080
2081 /* Check for a suppressed index register. */
2082 if (m68k_legitimate_base_reg_p (x, strict_p))
2083 {
2084 address->base = x;
2085 return true;
2086 }
2087
2088 /* Check for a suppressed base register. Do not allow this case
2089 for non-symbolic offsets as it effectively gives gcc freedom
2090 to treat data registers as base registers, which can generate
2091 worse code. */
2092 if (address->offset
2093 && symbolic_operand (address->offset, VOIDmode)
2094 && m68k_decompose_index (x, strict_p, address))
2095 return true;
2096 }
2097 else
2098 {
2099 /* Check for a nonzero base displacement. */
2100 if (GET_CODE (x) == PLUS
2101 && GET_CODE (XEXP (x, 1)) == CONST_INT
2102 && IN_RANGE (INTVAL (XEXP (x, 1)), -0x80, 0x80 - reach))
2103 {
2104 address->offset = XEXP (x, 1);
2105 x = XEXP (x, 0);
2106 }
2107 }
2108
2109 /* We now expect the sum of a base and an index. */
2110 if (GET_CODE (x) == PLUS)
2111 {
2112 if (m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p)
2113 && m68k_decompose_index (XEXP (x, 1), strict_p, address))
2114 {
2115 address->base = XEXP (x, 0);
2116 return true;
2117 }
2118
2119 if (m68k_legitimate_base_reg_p (XEXP (x, 1), strict_p)
2120 && m68k_decompose_index (XEXP (x, 0), strict_p, address))
2121 {
2122 address->base = XEXP (x, 1);
2123 return true;
2124 }
2125 }
2126 return false;
2127}
2128
2129/* Return true if X is a legitimate address for values of mode MODE.
2130 STRICT_P says whether strict checking is needed. */
2131
2132bool
2133m68k_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2134{
2135 struct m68k_address address;
2136
2137 return m68k_decompose_address (mode, x, strict_p, &address);
2138}
2139
2140/* Return true if X is a memory, describing its address in ADDRESS if so.
2141 Apply strict checking if called during or after reload. */
2142
2143static bool
2144m68k_legitimate_mem_p (rtx x, struct m68k_address *address)
2145{
2146 return (MEM_P (x)
2147 && m68k_decompose_address (GET_MODE (x), XEXP (x, 0),
2148 reload_in_progress || reload_completed,
2149 address));
2150}
2151
2152/* Return true if X matches the 'Q' constraint. It must be a memory
2153 with a base address and no constant offset or index. */
2154
2155bool
2156m68k_matches_q_p (rtx x)
2157{
2158 struct m68k_address address;
2159
2160 return (m68k_legitimate_mem_p (x, &address)
2161 && address.code == UNKNOWN
2162 && address.base
2163 && !address.offset
2164 && !address.index);
2165}
2166
2167/* Return true if X matches the 'U' constraint. It must be a base address
2168 with a constant offset and no index. */
2169
2170bool
2171m68k_matches_u_p (rtx x)
2172{
2173 struct m68k_address address;
2174
2175 return (m68k_legitimate_mem_p (x, &address)
2176 && address.code == UNKNOWN
2177 && address.base
2178 && address.offset
2179 && !address.index);
2180}
2181
75df395f
MK
2182/* Return GOT pointer. */
2183
2184static rtx
2185m68k_get_gp (void)
2186{
2187 if (pic_offset_table_rtx == NULL_RTX)
2188 pic_offset_table_rtx = gen_rtx_REG (Pmode, PIC_REG);
2189
2190 crtl->uses_pic_offset_table = 1;
2191
2192 return pic_offset_table_rtx;
2193}
2194
2195/* M68K relocations, used to distinguish GOT and TLS relocations in UNSPEC
2196 wrappers. */
2197enum m68k_reloc { RELOC_GOT, RELOC_TLSGD, RELOC_TLSLDM, RELOC_TLSLDO,
2198 RELOC_TLSIE, RELOC_TLSLE };
2199
2200#define TLS_RELOC_P(RELOC) ((RELOC) != RELOC_GOT)
2201
2202/* Wrap symbol X into unspec representing relocation RELOC.
2203 BASE_REG - register that should be added to the result.
2204 TEMP_REG - if non-null, temporary register. */
2205
2206static rtx
2207m68k_wrap_symbol (rtx x, enum m68k_reloc reloc, rtx base_reg, rtx temp_reg)
2208{
2209 bool use_x_p;
2210
2211 use_x_p = (base_reg == pic_offset_table_rtx) ? TARGET_XGOT : TARGET_XTLS;
2212
2213 if (TARGET_COLDFIRE && use_x_p)
2214 /* When compiling with -mx{got, tls} switch the code will look like this:
2215
2216 move.l <X>@<RELOC>,<TEMP_REG>
2217 add.l <BASE_REG>,<TEMP_REG> */
2218 {
2219 /* Wrap X in UNSPEC_??? to tip m68k_output_addr_const_extra
2220 to put @RELOC after reference. */
2221 x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (reloc)),
2222 UNSPEC_RELOC32);
2223 x = gen_rtx_CONST (Pmode, x);
2224
2225 if (temp_reg == NULL)
2226 {
2227 gcc_assert (can_create_pseudo_p ());
2228 temp_reg = gen_reg_rtx (Pmode);
2229 }
2230
2231 emit_move_insn (temp_reg, x);
2232 emit_insn (gen_addsi3 (temp_reg, temp_reg, base_reg));
2233 x = temp_reg;
2234 }
2235 else
2236 {
2237 x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (reloc)),
2238 UNSPEC_RELOC16);
2239 x = gen_rtx_CONST (Pmode, x);
2240
2241 x = gen_rtx_PLUS (Pmode, base_reg, x);
2242 }
2243
2244 return x;
2245}
2246
2247/* Helper for m68k_unwrap_symbol.
2248 Also, if unwrapping was successful (that is if (ORIG != <return value>)),
2249 sets *RELOC_PTR to relocation type for the symbol. */
2250
2251static rtx
2252m68k_unwrap_symbol_1 (rtx orig, bool unwrap_reloc32_p,
2253 enum m68k_reloc *reloc_ptr)
2254{
2255 if (GET_CODE (orig) == CONST)
2256 {
2257 rtx x;
2258 enum m68k_reloc dummy;
2259
2260 x = XEXP (orig, 0);
2261
2262 if (reloc_ptr == NULL)
2263 reloc_ptr = &dummy;
2264
2265 /* Handle an addend. */
2266 if ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
2267 && CONST_INT_P (XEXP (x, 1)))
2268 x = XEXP (x, 0);
2269
2270 if (GET_CODE (x) == UNSPEC)
2271 {
2272 switch (XINT (x, 1))
2273 {
2274 case UNSPEC_RELOC16:
2275 orig = XVECEXP (x, 0, 0);
2276 *reloc_ptr = (enum m68k_reloc) INTVAL (XVECEXP (x, 0, 1));
2277 break;
2278
2279 case UNSPEC_RELOC32:
2280 if (unwrap_reloc32_p)
2281 {
2282 orig = XVECEXP (x, 0, 0);
2283 *reloc_ptr = (enum m68k_reloc) INTVAL (XVECEXP (x, 0, 1));
2284 }
2285 break;
2286
2287 default:
2288 break;
2289 }
2290 }
2291 }
2292
2293 return orig;
2294}
2295
2296/* Unwrap symbol from UNSPEC_RELOC16 and, if unwrap_reloc32_p,
2297 UNSPEC_RELOC32 wrappers. */
2298
2299rtx
2300m68k_unwrap_symbol (rtx orig, bool unwrap_reloc32_p)
2301{
2302 return m68k_unwrap_symbol_1 (orig, unwrap_reloc32_p, NULL);
2303}
2304
2305/* Helper for m68k_final_prescan_insn. */
2306
2307static int
2308m68k_final_prescan_insn_1 (rtx *x_ptr, void *data ATTRIBUTE_UNUSED)
2309{
2310 rtx x = *x_ptr;
2311
2312 if (m68k_unwrap_symbol (x, true) != x)
2313 /* For rationale of the below, see comment in m68k_final_prescan_insn. */
2314 {
2315 rtx plus;
2316
2317 gcc_assert (GET_CODE (x) == CONST);
2318 plus = XEXP (x, 0);
2319
2320 if (GET_CODE (plus) == PLUS || GET_CODE (plus) == MINUS)
2321 {
2322 rtx unspec;
2323 rtx addend;
2324
2325 unspec = XEXP (plus, 0);
2326 gcc_assert (GET_CODE (unspec) == UNSPEC);
2327 addend = XEXP (plus, 1);
2328 gcc_assert (CONST_INT_P (addend));
2329
2330 /* We now have all the pieces, rearrange them. */
2331
2332 /* Move symbol to plus. */
2333 XEXP (plus, 0) = XVECEXP (unspec, 0, 0);
2334
2335 /* Move plus inside unspec. */
2336 XVECEXP (unspec, 0, 0) = plus;
2337
2338 /* Move unspec to top level of const. */
2339 XEXP (x, 0) = unspec;
2340 }
2341
2342 return -1;
2343 }
2344
2345 return 0;
2346}
2347
2348/* Prescan insn before outputing assembler for it. */
2349
2350void
2351m68k_final_prescan_insn (rtx insn ATTRIBUTE_UNUSED,
2352 rtx *operands, int n_operands)
2353{
2354 int i;
2355
2356 /* Combine and, possibly, other optimizations may do good job
2357 converting
2358 (const (unspec [(symbol)]))
2359 into
2360 (const (plus (unspec [(symbol)])
2361 (const_int N))).
2362 The problem with this is emitting @TLS or @GOT decorations.
2363 The decoration is emitted when processing (unspec), so the
2364 result would be "#symbol@TLSLE+N" instead of "#symbol+N@TLSLE".
2365
2366 It seems that the easiest solution to this is to convert such
2367 operands to
2368 (const (unspec [(plus (symbol)
2369 (const_int N))])).
2370 Note, that the top level of operand remains intact, so we don't have
2371 to patch up anything outside of the operand. */
2372
2373 for (i = 0; i < n_operands; ++i)
2374 {
2375 rtx op;
2376
2377 op = operands[i];
2378
2379 for_each_rtx (&op, m68k_final_prescan_insn_1, NULL);
2380 }
2381}
2382
2383/* Move X to a register and add REG_EQUAL note pointing to ORIG.
2384 If REG is non-null, use it; generate new pseudo otherwise. */
2385
2386static rtx
2387m68k_move_to_reg (rtx x, rtx orig, rtx reg)
2388{
2389 rtx insn;
2390
2391 if (reg == NULL_RTX)
2392 {
2393 gcc_assert (can_create_pseudo_p ());
2394 reg = gen_reg_rtx (Pmode);
2395 }
2396
2397 insn = emit_move_insn (reg, x);
2398 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2399 by loop. */
2400 set_unique_reg_note (insn, REG_EQUAL, orig);
2401
2402 return reg;
2403}
2404
2405/* Does the same as m68k_wrap_symbol, but returns a memory reference to
2406 GOT slot. */
2407
2408static rtx
2409m68k_wrap_symbol_into_got_ref (rtx x, enum m68k_reloc reloc, rtx temp_reg)
2410{
2411 x = m68k_wrap_symbol (x, reloc, m68k_get_gp (), temp_reg);
2412
2413 x = gen_rtx_MEM (Pmode, x);
2414 MEM_READONLY_P (x) = 1;
2415
2416 return x;
2417}
2418
79e68feb
RS
2419/* Legitimize PIC addresses. If the address is already
2420 position-independent, we return ORIG. Newly generated
2421 position-independent addresses go to REG. If we need more
2422 than one register, we lose.
2423
2424 An address is legitimized by making an indirect reference
2425 through the Global Offset Table with the name of the symbol
2426 used as an offset.
2427
2428 The assembler and linker are responsible for placing the
2429 address of the symbol in the GOT. The function prologue
2430 is responsible for initializing a5 to the starting address
2431 of the GOT.
2432
2433 The assembler is also responsible for translating a symbol name
2434 into a constant displacement from the start of the GOT.
2435
2436 A quick example may make things a little clearer:
2437
2438 When not generating PIC code to store the value 12345 into _foo
2439 we would generate the following code:
2440
2441 movel #12345, _foo
2442
2443 When generating PIC two transformations are made. First, the compiler
2444 loads the address of foo into a register. So the first transformation makes:
2445
2446 lea _foo, a0
2447 movel #12345, a0@
2448
2449 The code in movsi will intercept the lea instruction and call this
2450 routine which will transform the instructions into:
2451
2452 movel a5@(_foo:w), a0
2453 movel #12345, a0@
2454
2455
2456 That (in a nutshell) is how *all* symbol and label references are
2457 handled. */
2458
2459rtx
8a4a2253
BI
2460legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
2461 rtx reg)
79e68feb
RS
2462{
2463 rtx pic_ref = orig;
2464
2465 /* First handle a simple SYMBOL_REF or LABEL_REF */
2466 if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
2467 {
4761e388 2468 gcc_assert (reg);
79e68feb 2469
75df395f
MK
2470 pic_ref = m68k_wrap_symbol_into_got_ref (orig, RELOC_GOT, reg);
2471 pic_ref = m68k_move_to_reg (pic_ref, orig, reg);
79e68feb
RS
2472 }
2473 else if (GET_CODE (orig) == CONST)
2474 {
1d8eaa6b 2475 rtx base;
79e68feb 2476
b2e08ed4 2477 /* Make sure this has not already been legitimized. */
75df395f 2478 if (m68k_unwrap_symbol (orig, true) != orig)
79e68feb
RS
2479 return orig;
2480
4761e388 2481 gcc_assert (reg);
79e68feb
RS
2482
2483 /* legitimize both operands of the PLUS */
4761e388
NS
2484 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
2485
2486 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2487 orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2488 base == reg ? 0 : reg);
79e68feb
RS
2489
2490 if (GET_CODE (orig) == CONST_INT)
75df395f
MK
2491 pic_ref = plus_constant (base, INTVAL (orig));
2492 else
2493 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
79e68feb 2494 }
75df395f 2495
79e68feb
RS
2496 return pic_ref;
2497}
2498
75df395f
MK
2499/* The __tls_get_addr symbol. */
2500static GTY(()) rtx m68k_tls_get_addr;
2501
2502/* Return SYMBOL_REF for __tls_get_addr. */
2503
2504static rtx
2505m68k_get_tls_get_addr (void)
2506{
2507 if (m68k_tls_get_addr == NULL_RTX)
2508 m68k_tls_get_addr = init_one_libfunc ("__tls_get_addr");
2509
2510 return m68k_tls_get_addr;
2511}
2512
2513/* Return libcall result in A0 instead of usual D0. */
2514static bool m68k_libcall_value_in_a0_p = false;
2515
2516/* Emit instruction sequence that calls __tls_get_addr. X is
2517 the TLS symbol we are referencing and RELOC is the symbol type to use
2518 (either TLSGD or TLSLDM). EQV is the REG_EQUAL note for the sequence
2519 emitted. A pseudo register with result of __tls_get_addr call is
2520 returned. */
2521
2522static rtx
2523m68k_call_tls_get_addr (rtx x, rtx eqv, enum m68k_reloc reloc)
2524{
2525 rtx a0;
2526 rtx insns;
2527 rtx dest;
2528
2529 /* Emit the call sequence. */
2530 start_sequence ();
2531
2532 /* FIXME: Unfortunately, emit_library_call_value does not
2533 consider (plus (%a5) (const (unspec))) to be a good enough
2534 operand for push, so it forces it into a register. The bad
2535 thing about this is that combiner, due to copy propagation and other
2536 optimizations, sometimes can not later fix this. As a consequence,
2537 additional register may be allocated resulting in a spill.
2538 For reference, see args processing loops in
2539 calls.c:emit_library_call_value_1.
2540 For testcase, see gcc.target/m68k/tls-{gd, ld}.c */
2541 x = m68k_wrap_symbol (x, reloc, m68k_get_gp (), NULL_RTX);
2542
2543 /* __tls_get_addr() is not a libcall, but emitting a libcall_value
2544 is the simpliest way of generating a call. The difference between
2545 __tls_get_addr() and libcall is that the result is returned in D0
2546 instead of A0. To workaround this, we use m68k_libcall_value_in_a0_p
2547 which temporarily switches returning the result to A0. */
2548
2549 m68k_libcall_value_in_a0_p = true;
2550 a0 = emit_library_call_value (m68k_get_tls_get_addr (), NULL_RTX, LCT_PURE,
2551 Pmode, 1, x, Pmode);
2552 m68k_libcall_value_in_a0_p = false;
2553
2554 insns = get_insns ();
2555 end_sequence ();
2556
2557 gcc_assert (can_create_pseudo_p ());
2558 dest = gen_reg_rtx (Pmode);
2559 emit_libcall_block (insns, dest, a0, eqv);
2560
2561 return dest;
2562}
2563
2564/* The __tls_get_addr symbol. */
2565static GTY(()) rtx m68k_read_tp;
2566
2567/* Return SYMBOL_REF for __m68k_read_tp. */
2568
2569static rtx
2570m68k_get_m68k_read_tp (void)
2571{
2572 if (m68k_read_tp == NULL_RTX)
2573 m68k_read_tp = init_one_libfunc ("__m68k_read_tp");
2574
2575 return m68k_read_tp;
2576}
2577
2578/* Emit instruction sequence that calls __m68k_read_tp.
2579 A pseudo register with result of __m68k_read_tp call is returned. */
2580
2581static rtx
2582m68k_call_m68k_read_tp (void)
2583{
2584 rtx a0;
2585 rtx eqv;
2586 rtx insns;
2587 rtx dest;
2588
2589 start_sequence ();
2590
2591 /* __m68k_read_tp() is not a libcall, but emitting a libcall_value
2592 is the simpliest way of generating a call. The difference between
2593 __m68k_read_tp() and libcall is that the result is returned in D0
2594 instead of A0. To workaround this, we use m68k_libcall_value_in_a0_p
2595 which temporarily switches returning the result to A0. */
2596
2597 /* Emit the call sequence. */
2598 m68k_libcall_value_in_a0_p = true;
2599 a0 = emit_library_call_value (m68k_get_m68k_read_tp (), NULL_RTX, LCT_PURE,
2600 Pmode, 0);
2601 m68k_libcall_value_in_a0_p = false;
2602 insns = get_insns ();
2603 end_sequence ();
2604
2605 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2606 share the m68k_read_tp result with other IE/LE model accesses. */
2607 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx), UNSPEC_RELOC32);
2608
2609 gcc_assert (can_create_pseudo_p ());
2610 dest = gen_reg_rtx (Pmode);
2611 emit_libcall_block (insns, dest, a0, eqv);
2612
2613 return dest;
2614}
2615
2616/* Return a legitimized address for accessing TLS SYMBOL_REF X.
2617 For explanations on instructions sequences see TLS/NPTL ABI for m68k and
2618 ColdFire. */
2619
2620rtx
2621m68k_legitimize_tls_address (rtx orig)
2622{
2623 switch (SYMBOL_REF_TLS_MODEL (orig))
2624 {
2625 case TLS_MODEL_GLOBAL_DYNAMIC:
2626 orig = m68k_call_tls_get_addr (orig, orig, RELOC_TLSGD);
2627 break;
2628
2629 case TLS_MODEL_LOCAL_DYNAMIC:
2630 {
2631 rtx eqv;
2632 rtx a0;
2633 rtx x;
2634
2635 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2636 share the LDM result with other LD model accesses. */
2637 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2638 UNSPEC_RELOC32);
2639
2640 a0 = m68k_call_tls_get_addr (orig, eqv, RELOC_TLSLDM);
2641
2642 x = m68k_wrap_symbol (orig, RELOC_TLSLDO, a0, NULL_RTX);
2643
2644 if (can_create_pseudo_p ())
2645 x = m68k_move_to_reg (x, orig, NULL_RTX);
2646
2647 orig = x;
2648 break;
2649 }
2650
2651 case TLS_MODEL_INITIAL_EXEC:
2652 {
2653 rtx a0;
2654 rtx x;
2655
2656 a0 = m68k_call_m68k_read_tp ();
2657
2658 x = m68k_wrap_symbol_into_got_ref (orig, RELOC_TLSIE, NULL_RTX);
2659 x = gen_rtx_PLUS (Pmode, x, a0);
2660
2661 if (can_create_pseudo_p ())
2662 x = m68k_move_to_reg (x, orig, NULL_RTX);
2663
2664 orig = x;
2665 break;
2666 }
2667
2668 case TLS_MODEL_LOCAL_EXEC:
2669 {
2670 rtx a0;
2671 rtx x;
2672
2673 a0 = m68k_call_m68k_read_tp ();
2674
2675 x = m68k_wrap_symbol (orig, RELOC_TLSLE, a0, NULL_RTX);
2676
2677 if (can_create_pseudo_p ())
2678 x = m68k_move_to_reg (x, orig, NULL_RTX);
2679
2680 orig = x;
2681 break;
2682 }
2683
2684 default:
2685 gcc_unreachable ();
2686 }
2687
2688 return orig;
2689}
2690
2691/* Return true if X is a TLS symbol. */
2692
2693static bool
2694m68k_tls_symbol_p (rtx x)
2695{
2696 if (!TARGET_HAVE_TLS)
2697 return false;
2698
2699 if (GET_CODE (x) != SYMBOL_REF)
2700 return false;
2701
2702 return SYMBOL_REF_TLS_MODEL (x) != 0;
2703}
2704
2705/* Helper for m68k_tls_referenced_p. */
2706
2707static int
2708m68k_tls_reference_p_1 (rtx *x_ptr, void *data ATTRIBUTE_UNUSED)
2709{
2710 /* Note: this is not the same as m68k_tls_symbol_p. */
2711 if (GET_CODE (*x_ptr) == SYMBOL_REF)
2712 return SYMBOL_REF_TLS_MODEL (*x_ptr) != 0 ? 1 : 0;
2713
2714 /* Don't recurse into legitimate TLS references. */
2715 if (m68k_tls_reference_p (*x_ptr, true))
2716 return -1;
2717
2718 return 0;
2719}
2720
2721/* If !LEGITIMATE_P, return true if X is a TLS symbol reference,
2722 though illegitimate one.
2723 If LEGITIMATE_P, return true if X is a legitimate TLS symbol reference. */
2724
2725bool
2726m68k_tls_reference_p (rtx x, bool legitimate_p)
2727{
2728 if (!TARGET_HAVE_TLS)
2729 return false;
2730
2731 if (!legitimate_p)
2732 return for_each_rtx (&x, m68k_tls_reference_p_1, NULL) == 1 ? true : false;
2733 else
2734 {
2735 enum m68k_reloc reloc = RELOC_GOT;
2736
2737 return (m68k_unwrap_symbol_1 (x, true, &reloc) != x
2738 && TLS_RELOC_P (reloc));
2739 }
2740}
2741
79e68feb 2742\f
0ce6f9fb 2743
a0a7fbc9 2744#define USE_MOVQ(i) ((unsigned) ((i) + 128) <= 255)
0ce6f9fb 2745
bda2a571
RS
2746/* Return the type of move that should be used for integer I. */
2747
c47b0cb4
MK
2748M68K_CONST_METHOD
2749m68k_const_method (HOST_WIDE_INT i)
0ce6f9fb 2750{
0ce6f9fb
RK
2751 unsigned u;
2752
6910dd70 2753 if (USE_MOVQ (i))
0ce6f9fb 2754 return MOVQ;
24092242 2755
c16eadc7 2756 /* The ColdFire doesn't have byte or word operations. */
97c55091 2757 /* FIXME: This may not be useful for the m68060 either. */
85dbf7e2 2758 if (!TARGET_COLDFIRE)
24092242
RK
2759 {
2760 /* if -256 < N < 256 but N is not in range for a moveq
7a1929e1 2761 N^ff will be, so use moveq #N^ff, dreg; not.b dreg. */
24092242
RK
2762 if (USE_MOVQ (i ^ 0xff))
2763 return NOTB;
2764 /* Likewise, try with not.w */
2765 if (USE_MOVQ (i ^ 0xffff))
2766 return NOTW;
2767 /* This is the only value where neg.w is useful */
2768 if (i == -65408)
2769 return NEGW;
24092242 2770 }
28bad6d1 2771
5e04daf3
PB
2772 /* Try also with swap. */
2773 u = i;
2774 if (USE_MOVQ ((u >> 16) | (u << 16)))
2775 return SWAP;
2776
986e74d5 2777 if (TARGET_ISAB)
28bad6d1 2778 {
72edf146 2779 /* Try using MVZ/MVS with an immediate value to load constants. */
28bad6d1
PB
2780 if (i >= 0 && i <= 65535)
2781 return MVZ;
2782 if (i >= -32768 && i <= 32767)
2783 return MVS;
2784 }
2785
0ce6f9fb
RK
2786 /* Otherwise, use move.l */
2787 return MOVL;
2788}
2789
bda2a571
RS
2790/* Return the cost of moving constant I into a data register. */
2791
3c50106f 2792static int
bda2a571 2793const_int_cost (HOST_WIDE_INT i)
0ce6f9fb 2794{
c47b0cb4 2795 switch (m68k_const_method (i))
0ce6f9fb 2796 {
a0a7fbc9
AS
2797 case MOVQ:
2798 /* Constants between -128 and 127 are cheap due to moveq. */
2799 return 0;
2800 case MVZ:
2801 case MVS:
2802 case NOTB:
2803 case NOTW:
2804 case NEGW:
2805 case SWAP:
2806 /* Constants easily generated by moveq + not.b/not.w/neg.w/swap. */
2807 return 1;
2808 case MOVL:
2809 return 2;
2810 default:
2811 gcc_unreachable ();
0ce6f9fb
RK
2812 }
2813}
2814
3c50106f 2815static bool
f40751dd
JH
2816m68k_rtx_costs (rtx x, int code, int outer_code, int *total,
2817 bool speed ATTRIBUTE_UNUSED)
3c50106f
RH
2818{
2819 switch (code)
2820 {
2821 case CONST_INT:
2822 /* Constant zero is super cheap due to clr instruction. */
2823 if (x == const0_rtx)
2824 *total = 0;
2825 else
bda2a571 2826 *total = const_int_cost (INTVAL (x));
3c50106f
RH
2827 return true;
2828
2829 case CONST:
2830 case LABEL_REF:
2831 case SYMBOL_REF:
2832 *total = 3;
2833 return true;
2834
2835 case CONST_DOUBLE:
2836 /* Make 0.0 cheaper than other floating constants to
2837 encourage creating tstsf and tstdf insns. */
2838 if (outer_code == COMPARE
2839 && (x == CONST0_RTX (SFmode) || x == CONST0_RTX (DFmode)))
2840 *total = 4;
2841 else
2842 *total = 5;
2843 return true;
2844
2845 /* These are vaguely right for a 68020. */
2846 /* The costs for long multiply have been adjusted to work properly
2847 in synth_mult on the 68020, relative to an average of the time
2848 for add and the time for shift, taking away a little more because
2849 sometimes move insns are needed. */
a0a7fbc9
AS
2850 /* div?.w is relatively cheaper on 68000 counted in COSTS_N_INSNS
2851 terms. */
fe95f2f7
JB
2852#define MULL_COST \
2853 (TUNE_68060 ? 2 \
2854 : TUNE_68040 ? 5 \
03b3e271
KH
2855 : (TUNE_CFV2 && TUNE_EMAC) ? 3 \
2856 : (TUNE_CFV2 && TUNE_MAC) ? 4 \
2857 : TUNE_CFV2 ? 8 \
fe95f2f7
JB
2858 : TARGET_COLDFIRE ? 3 : 13)
2859
2860#define MULW_COST \
2861 (TUNE_68060 ? 2 \
2862 : TUNE_68040 ? 3 \
03b3e271
KH
2863 : TUNE_68000_10 ? 5 \
2864 : (TUNE_CFV2 && TUNE_EMAC) ? 3 \
2865 : (TUNE_CFV2 && TUNE_MAC) ? 2 \
2866 : TUNE_CFV2 ? 8 \
fe95f2f7
JB
2867 : TARGET_COLDFIRE ? 2 : 8)
2868
2869#define DIVW_COST \
2870 (TARGET_CF_HWDIV ? 11 \
2871 : TUNE_68000_10 || TARGET_COLDFIRE ? 12 : 27)
3c50106f
RH
2872
2873 case PLUS:
2874 /* An lea costs about three times as much as a simple add. */
2875 if (GET_MODE (x) == SImode
2876 && GET_CODE (XEXP (x, 1)) == REG
2877 && GET_CODE (XEXP (x, 0)) == MULT
2878 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2879 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2880 && (INTVAL (XEXP (XEXP (x, 0), 1)) == 2
2881 || INTVAL (XEXP (XEXP (x, 0), 1)) == 4
2882 || INTVAL (XEXP (XEXP (x, 0), 1)) == 8))
eb849993
BI
2883 {
2884 /* lea an@(dx:l:i),am */
2885 *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 2 : 3);
2886 return true;
2887 }
3c50106f
RH
2888 return false;
2889
2890 case ASHIFT:
2891 case ASHIFTRT:
2892 case LSHIFTRT:
fe95f2f7 2893 if (TUNE_68060)
3c50106f
RH
2894 {
2895 *total = COSTS_N_INSNS(1);
2896 return true;
2897 }
fe95f2f7 2898 if (TUNE_68000_10)
3c50106f
RH
2899 {
2900 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2901 {
2902 if (INTVAL (XEXP (x, 1)) < 16)
2903 *total = COSTS_N_INSNS (2) + INTVAL (XEXP (x, 1)) / 2;
2904 else
2905 /* We're using clrw + swap for these cases. */
2906 *total = COSTS_N_INSNS (4) + (INTVAL (XEXP (x, 1)) - 16) / 2;
2907 }
2908 else
a0a7fbc9 2909 *total = COSTS_N_INSNS (10); /* Worst case. */
3c50106f
RH
2910 return true;
2911 }
2912 /* A shift by a big integer takes an extra instruction. */
2913 if (GET_CODE (XEXP (x, 1)) == CONST_INT
2914 && (INTVAL (XEXP (x, 1)) == 16))
2915 {
2916 *total = COSTS_N_INSNS (2); /* clrw;swap */
2917 return true;
2918 }
2919 if (GET_CODE (XEXP (x, 1)) == CONST_INT
2920 && !(INTVAL (XEXP (x, 1)) > 0
2921 && INTVAL (XEXP (x, 1)) <= 8))
2922 {
eb849993 2923 *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 1 : 3); /* lsr #i,dn */
3c50106f
RH
2924 return true;
2925 }
2926 return false;
2927
2928 case MULT:
2929 if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2930 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
2931 && GET_MODE (x) == SImode)
2932 *total = COSTS_N_INSNS (MULW_COST);
2933 else if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
2934 *total = COSTS_N_INSNS (MULW_COST);
2935 else
2936 *total = COSTS_N_INSNS (MULL_COST);
2937 return true;
2938
2939 case DIV:
2940 case UDIV:
2941 case MOD:
2942 case UMOD:
2943 if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
2944 *total = COSTS_N_INSNS (DIVW_COST); /* div.w */
eb849993
BI
2945 else if (TARGET_CF_HWDIV)
2946 *total = COSTS_N_INSNS (18);
3c50106f
RH
2947 else
2948 *total = COSTS_N_INSNS (43); /* div.l */
2949 return true;
2950
f90b7a5a
PB
2951 case ZERO_EXTRACT:
2952 if (outer_code == COMPARE)
2953 *total = 0;
2954 return false;
2955
3c50106f
RH
2956 default:
2957 return false;
2958 }
2959}
2960
88512ba0 2961/* Return an instruction to move CONST_INT OPERANDS[1] into data register
bda2a571
RS
2962 OPERANDS[0]. */
2963
2964static const char *
8a4a2253 2965output_move_const_into_data_reg (rtx *operands)
0ce6f9fb 2966{
bda2a571 2967 HOST_WIDE_INT i;
0ce6f9fb
RK
2968
2969 i = INTVAL (operands[1]);
c47b0cb4 2970 switch (m68k_const_method (i))
0ce6f9fb 2971 {
28bad6d1 2972 case MVZ:
28bad6d1 2973 return "mvzw %1,%0";
1cbae84f
PB
2974 case MVS:
2975 return "mvsw %1,%0";
a0a7fbc9 2976 case MOVQ:
0ce6f9fb 2977 return "moveq %1,%0";
a0a7fbc9 2978 case NOTB:
66e07510 2979 CC_STATUS_INIT;
1d8eaa6b 2980 operands[1] = GEN_INT (i ^ 0xff);
0ce6f9fb 2981 return "moveq %1,%0\n\tnot%.b %0";
a0a7fbc9 2982 case NOTW:
66e07510 2983 CC_STATUS_INIT;
1d8eaa6b 2984 operands[1] = GEN_INT (i ^ 0xffff);
0ce6f9fb 2985 return "moveq %1,%0\n\tnot%.w %0";
a0a7fbc9 2986 case NEGW:
66e07510 2987 CC_STATUS_INIT;
3b4b85c9 2988 return "moveq #-128,%0\n\tneg%.w %0";
a0a7fbc9 2989 case SWAP:
0ce6f9fb
RK
2990 {
2991 unsigned u = i;
2992
1d8eaa6b 2993 operands[1] = GEN_INT ((u << 16) | (u >> 16));
0ce6f9fb 2994 return "moveq %1,%0\n\tswap %0";
0ce6f9fb 2995 }
a0a7fbc9 2996 case MOVL:
bda2a571 2997 return "move%.l %1,%0";
a0a7fbc9 2998 default:
bda2a571 2999 gcc_unreachable ();
0ce6f9fb
RK
3000 }
3001}
3002
bda2a571 3003/* Return true if I can be handled by ISA B's mov3q instruction. */
5e04daf3 3004
bda2a571
RS
3005bool
3006valid_mov3q_const (HOST_WIDE_INT i)
3007{
3008 return TARGET_ISAB && (i == -1 || IN_RANGE (i, 1, 7));
5e04daf3
PB
3009}
3010
bda2a571
RS
3011/* Return an instruction to move CONST_INT OPERANDS[1] into OPERANDS[0].
3012 I is the value of OPERANDS[1]. */
5e04daf3 3013
bda2a571 3014static const char *
8a4a2253 3015output_move_simode_const (rtx *operands)
02ed0c07 3016{
bda2a571
RS
3017 rtx dest;
3018 HOST_WIDE_INT src;
3019
3020 dest = operands[0];
3021 src = INTVAL (operands[1]);
3022 if (src == 0
3023 && (DATA_REG_P (dest) || MEM_P (dest))
3197c489
RS
3024 /* clr insns on 68000 read before writing. */
3025 && ((TARGET_68010 || TARGET_COLDFIRE)
bda2a571 3026 || !(MEM_P (dest) && MEM_VOLATILE_P (dest))))
02ed0c07 3027 return "clr%.l %0";
bda2a571 3028 else if (GET_MODE (dest) == SImode && valid_mov3q_const (src))
a0a7fbc9 3029 return "mov3q%.l %1,%0";
bda2a571 3030 else if (src == 0 && ADDRESS_REG_P (dest))
38198304 3031 return "sub%.l %0,%0";
bda2a571 3032 else if (DATA_REG_P (dest))
02ed0c07 3033 return output_move_const_into_data_reg (operands);
bda2a571 3034 else if (ADDRESS_REG_P (dest) && IN_RANGE (src, -0x8000, 0x7fff))
5e04daf3 3035 {
bda2a571 3036 if (valid_mov3q_const (src))
5e04daf3
PB
3037 return "mov3q%.l %1,%0";
3038 return "move%.w %1,%0";
3039 }
bda2a571
RS
3040 else if (MEM_P (dest)
3041 && GET_CODE (XEXP (dest, 0)) == PRE_DEC
3042 && REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
3043 && IN_RANGE (src, -0x8000, 0x7fff))
5e04daf3 3044 {
bda2a571 3045 if (valid_mov3q_const (src))
5e04daf3
PB
3046 return "mov3q%.l %1,%-";
3047 return "pea %a1";
3048 }
02ed0c07
RK
3049 return "move%.l %1,%0";
3050}
3051
5505f548 3052const char *
8a4a2253 3053output_move_simode (rtx *operands)
f4e80198
RK
3054{
3055 if (GET_CODE (operands[1]) == CONST_INT)
3056 return output_move_simode_const (operands);
3057 else if ((GET_CODE (operands[1]) == SYMBOL_REF
3058 || GET_CODE (operands[1]) == CONST)
3059 && push_operand (operands[0], SImode))
3060 return "pea %a1";
3061 else if ((GET_CODE (operands[1]) == SYMBOL_REF
3062 || GET_CODE (operands[1]) == CONST)
3063 && ADDRESS_REG_P (operands[0]))
3064 return "lea %a1,%0";
3065 return "move%.l %1,%0";
3066}
3067
5505f548 3068const char *
8a4a2253 3069output_move_himode (rtx *operands)
f4e80198
RK
3070{
3071 if (GET_CODE (operands[1]) == CONST_INT)
3072 {
3073 if (operands[1] == const0_rtx
3074 && (DATA_REG_P (operands[0])
3075 || GET_CODE (operands[0]) == MEM)
3197c489
RS
3076 /* clr insns on 68000 read before writing. */
3077 && ((TARGET_68010 || TARGET_COLDFIRE)
f4e80198
RK
3078 || !(GET_CODE (operands[0]) == MEM
3079 && MEM_VOLATILE_P (operands[0]))))
3080 return "clr%.w %0";
38198304
AS
3081 else if (operands[1] == const0_rtx
3082 && ADDRESS_REG_P (operands[0]))
3083 return "sub%.l %0,%0";
f4e80198
RK
3084 else if (DATA_REG_P (operands[0])
3085 && INTVAL (operands[1]) < 128
3086 && INTVAL (operands[1]) >= -128)
a0a7fbc9 3087 return "moveq %1,%0";
f4e80198
RK
3088 else if (INTVAL (operands[1]) < 0x8000
3089 && INTVAL (operands[1]) >= -0x8000)
3090 return "move%.w %1,%0";
3091 }
3092 else if (CONSTANT_P (operands[1]))
3093 return "move%.l %1,%0";
f4e80198
RK
3094 return "move%.w %1,%0";
3095}
3096
5505f548 3097const char *
8a4a2253 3098output_move_qimode (rtx *operands)
f4e80198 3099{
102701ff 3100 /* 68k family always modifies the stack pointer by at least 2, even for
c16eadc7 3101 byte pushes. The 5200 (ColdFire) does not do this. */
4761e388 3102
a0a7fbc9 3103 /* This case is generated by pushqi1 pattern now. */
4761e388
NS
3104 gcc_assert (!(GET_CODE (operands[0]) == MEM
3105 && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
3106 && XEXP (XEXP (operands[0], 0), 0) == stack_pointer_rtx
3107 && ! ADDRESS_REG_P (operands[1])
3108 && ! TARGET_COLDFIRE));
f4e80198 3109
3197c489 3110 /* clr and st insns on 68000 read before writing. */
f4e80198 3111 if (!ADDRESS_REG_P (operands[0])
3197c489 3112 && ((TARGET_68010 || TARGET_COLDFIRE)
f4e80198
RK
3113 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
3114 {
3115 if (operands[1] == const0_rtx)
3116 return "clr%.b %0";
9425fb04 3117 if ((!TARGET_COLDFIRE || DATA_REG_P (operands[0]))
f4e80198
RK
3118 && GET_CODE (operands[1]) == CONST_INT
3119 && (INTVAL (operands[1]) & 255) == 255)
3120 {
3121 CC_STATUS_INIT;
3122 return "st %0";
3123 }
3124 }
3125 if (GET_CODE (operands[1]) == CONST_INT
3126 && DATA_REG_P (operands[0])
3127 && INTVAL (operands[1]) < 128
3128 && INTVAL (operands[1]) >= -128)
a0a7fbc9 3129 return "moveq %1,%0";
38198304
AS
3130 if (operands[1] == const0_rtx && ADDRESS_REG_P (operands[0]))
3131 return "sub%.l %0,%0";
f4e80198
RK
3132 if (GET_CODE (operands[1]) != CONST_INT && CONSTANT_P (operands[1]))
3133 return "move%.l %1,%0";
c16eadc7 3134 /* 68k family (including the 5200 ColdFire) does not support byte moves to
37834fc8
JL
3135 from address registers. */
3136 if (ADDRESS_REG_P (operands[0]) || ADDRESS_REG_P (operands[1]))
f4e80198
RK
3137 return "move%.w %1,%0";
3138 return "move%.b %1,%0";
3139}
3140
5505f548 3141const char *
8a4a2253 3142output_move_stricthi (rtx *operands)
9b55bf04
RK
3143{
3144 if (operands[1] == const0_rtx
3197c489
RS
3145 /* clr insns on 68000 read before writing. */
3146 && ((TARGET_68010 || TARGET_COLDFIRE)
9b55bf04
RK
3147 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
3148 return "clr%.w %0";
3149 return "move%.w %1,%0";
3150}
3151
5505f548 3152const char *
8a4a2253 3153output_move_strictqi (rtx *operands)
9b55bf04
RK
3154{
3155 if (operands[1] == const0_rtx
3197c489
RS
3156 /* clr insns on 68000 read before writing. */
3157 && ((TARGET_68010 || TARGET_COLDFIRE)
9b55bf04
RK
3158 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
3159 return "clr%.b %0";
3160 return "move%.b %1,%0";
3161}
3162
79e68feb
RS
3163/* Return the best assembler insn template
3164 for moving operands[1] into operands[0] as a fullword. */
3165
5505f548 3166static const char *
8a4a2253 3167singlemove_string (rtx *operands)
79e68feb 3168{
02ed0c07
RK
3169 if (GET_CODE (operands[1]) == CONST_INT)
3170 return output_move_simode_const (operands);
3171 return "move%.l %1,%0";
79e68feb
RS
3172}
3173
2505bc97 3174
c47b0cb4
MK
3175/* Output assembler or rtl code to perform a doubleword move insn
3176 with operands OPERANDS.
3177 Pointers to 3 helper functions should be specified:
3178 HANDLE_REG_ADJUST to adjust a register by a small value,
3179 HANDLE_COMPADR to compute an address and
3180 HANDLE_MOVSI to move 4 bytes. */
79e68feb 3181
c47b0cb4
MK
3182static void
3183handle_move_double (rtx operands[2],
3184 void (*handle_reg_adjust) (rtx, int),
3185 void (*handle_compadr) (rtx [2]),
3186 void (*handle_movsi) (rtx [2]))
79e68feb 3187{
2505bc97
RS
3188 enum
3189 {
3190 REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP
3191 } optype0, optype1;
79e68feb 3192 rtx latehalf[2];
2505bc97 3193 rtx middlehalf[2];
7f98eeb6 3194 rtx xops[2];
79e68feb 3195 rtx addreg0 = 0, addreg1 = 0;
7f98eeb6 3196 int dest_overlapped_low = 0;
184916bc 3197 int size = GET_MODE_SIZE (GET_MODE (operands[0]));
2505bc97
RS
3198
3199 middlehalf[0] = 0;
3200 middlehalf[1] = 0;
79e68feb
RS
3201
3202 /* First classify both operands. */
3203
3204 if (REG_P (operands[0]))
3205 optype0 = REGOP;
3206 else if (offsettable_memref_p (operands[0]))
3207 optype0 = OFFSOP;
3208 else if (GET_CODE (XEXP (operands[0], 0)) == POST_INC)
3209 optype0 = POPOP;
3210 else if (GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
3211 optype0 = PUSHOP;
3212 else if (GET_CODE (operands[0]) == MEM)
3213 optype0 = MEMOP;
3214 else
3215 optype0 = RNDOP;
3216
3217 if (REG_P (operands[1]))
3218 optype1 = REGOP;
3219 else if (CONSTANT_P (operands[1]))
3220 optype1 = CNSTOP;
3221 else if (offsettable_memref_p (operands[1]))
3222 optype1 = OFFSOP;
3223 else if (GET_CODE (XEXP (operands[1], 0)) == POST_INC)
3224 optype1 = POPOP;
3225 else if (GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
3226 optype1 = PUSHOP;
3227 else if (GET_CODE (operands[1]) == MEM)
3228 optype1 = MEMOP;
3229 else
3230 optype1 = RNDOP;
3231
4761e388
NS
3232 /* Check for the cases that the operand constraints are not supposed
3233 to allow to happen. Generating code for these cases is
3234 painful. */
3235 gcc_assert (optype0 != RNDOP && optype1 != RNDOP);
79e68feb
RS
3236
3237 /* If one operand is decrementing and one is incrementing
3238 decrement the former register explicitly
3239 and change that operand into ordinary indexing. */
3240
3241 if (optype0 == PUSHOP && optype1 == POPOP)
3242 {
3243 operands[0] = XEXP (XEXP (operands[0], 0), 0);
c47b0cb4
MK
3244
3245 handle_reg_adjust (operands[0], -size);
3246
2505bc97 3247 if (GET_MODE (operands[1]) == XFmode)
1d8eaa6b 3248 operands[0] = gen_rtx_MEM (XFmode, operands[0]);
2505bc97 3249 else if (GET_MODE (operands[0]) == DFmode)
1d8eaa6b 3250 operands[0] = gen_rtx_MEM (DFmode, operands[0]);
2505bc97 3251 else
1d8eaa6b 3252 operands[0] = gen_rtx_MEM (DImode, operands[0]);
79e68feb
RS
3253 optype0 = OFFSOP;
3254 }
3255 if (optype0 == POPOP && optype1 == PUSHOP)
3256 {
3257 operands[1] = XEXP (XEXP (operands[1], 0), 0);
c47b0cb4
MK
3258
3259 handle_reg_adjust (operands[1], -size);
3260
2505bc97 3261 if (GET_MODE (operands[1]) == XFmode)
1d8eaa6b 3262 operands[1] = gen_rtx_MEM (XFmode, operands[1]);
2505bc97 3263 else if (GET_MODE (operands[1]) == DFmode)
1d8eaa6b 3264 operands[1] = gen_rtx_MEM (DFmode, operands[1]);
2505bc97 3265 else
1d8eaa6b 3266 operands[1] = gen_rtx_MEM (DImode, operands[1]);
79e68feb
RS
3267 optype1 = OFFSOP;
3268 }
3269
3270 /* If an operand is an unoffsettable memory ref, find a register
3271 we can increment temporarily to make it refer to the second word. */
3272
3273 if (optype0 == MEMOP)
3274 addreg0 = find_addr_reg (XEXP (operands[0], 0));
3275
3276 if (optype1 == MEMOP)
3277 addreg1 = find_addr_reg (XEXP (operands[1], 0));
3278
3279 /* Ok, we can do one word at a time.
3280 Normally we do the low-numbered word first,
3281 but if either operand is autodecrementing then we
3282 do the high-numbered word first.
3283
3284 In either case, set up in LATEHALF the operands to use
3285 for the high-numbered word and in some cases alter the
3286 operands in OPERANDS to be suitable for the low-numbered word. */
3287
2505bc97
RS
3288 if (size == 12)
3289 {
3290 if (optype0 == REGOP)
3291 {
1d8eaa6b
AS
3292 latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 2);
3293 middlehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2505bc97
RS
3294 }
3295 else if (optype0 == OFFSOP)
3296 {
b72f00af
RK
3297 middlehalf[0] = adjust_address (operands[0], SImode, 4);
3298 latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2505bc97
RS
3299 }
3300 else
3301 {
c47b0cb4
MK
3302 middlehalf[0] = adjust_address (operands[0], SImode, 0);
3303 latehalf[0] = adjust_address (operands[0], SImode, 0);
2505bc97
RS
3304 }
3305
3306 if (optype1 == REGOP)
3307 {
1d8eaa6b
AS
3308 latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 2);
3309 middlehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2505bc97
RS
3310 }
3311 else if (optype1 == OFFSOP)
3312 {
b72f00af
RK
3313 middlehalf[1] = adjust_address (operands[1], SImode, 4);
3314 latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2505bc97
RS
3315 }
3316 else if (optype1 == CNSTOP)
3317 {
3318 if (GET_CODE (operands[1]) == CONST_DOUBLE)
3319 {
3320 REAL_VALUE_TYPE r;
3321 long l[3];
3322
3323 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
3324 REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
3325 operands[1] = GEN_INT (l[0]);
3326 middlehalf[1] = GEN_INT (l[1]);
3327 latehalf[1] = GEN_INT (l[2]);
3328 }
4761e388 3329 else
2505bc97 3330 {
4761e388
NS
3331 /* No non-CONST_DOUBLE constant should ever appear
3332 here. */
3333 gcc_assert (!CONSTANT_P (operands[1]));
2505bc97
RS
3334 }
3335 }
3336 else
3337 {
c47b0cb4
MK
3338 middlehalf[1] = adjust_address (operands[1], SImode, 0);
3339 latehalf[1] = adjust_address (operands[1], SImode, 0);
2505bc97
RS
3340 }
3341 }
79e68feb 3342 else
2505bc97
RS
3343 /* size is not 12: */
3344 {
3345 if (optype0 == REGOP)
1d8eaa6b 3346 latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2505bc97 3347 else if (optype0 == OFFSOP)
b72f00af 3348 latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2505bc97 3349 else
c47b0cb4 3350 latehalf[0] = adjust_address (operands[0], SImode, 0);
2505bc97
RS
3351
3352 if (optype1 == REGOP)
1d8eaa6b 3353 latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2505bc97 3354 else if (optype1 == OFFSOP)
b72f00af 3355 latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2505bc97
RS
3356 else if (optype1 == CNSTOP)
3357 split_double (operands[1], &operands[1], &latehalf[1]);
3358 else
c47b0cb4 3359 latehalf[1] = adjust_address (operands[1], SImode, 0);
2505bc97 3360 }
79e68feb
RS
3361
3362 /* If insn is effectively movd N(sp),-(sp) then we will do the
3363 high word first. We should use the adjusted operand 1 (which is N+4(sp))
3364 for the low word as well, to compensate for the first decrement of sp. */
3365 if (optype0 == PUSHOP
3366 && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
3367 && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
c88aeaf8 3368 operands[1] = middlehalf[1] = latehalf[1];
79e68feb 3369
7f98eeb6
RS
3370 /* For (set (reg:DI N) (mem:DI ... (reg:SI N) ...)),
3371 if the upper part of reg N does not appear in the MEM, arrange to
3372 emit the move late-half first. Otherwise, compute the MEM address
3373 into the upper part of N and use that as a pointer to the memory
3374 operand. */
3375 if (optype0 == REGOP
3376 && (optype1 == OFFSOP || optype1 == MEMOP))
3377 {
1d8eaa6b 3378 rtx testlow = gen_rtx_REG (SImode, REGNO (operands[0]));
3a58400f
RS
3379
3380 if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
d7e8d581 3381 && reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
7f98eeb6
RS
3382 {
3383 /* If both halves of dest are used in the src memory address,
3a58400f
RS
3384 compute the address into latehalf of dest.
3385 Note that this can't happen if the dest is two data regs. */
4761e388 3386 compadr:
7f98eeb6
RS
3387 xops[0] = latehalf[0];
3388 xops[1] = XEXP (operands[1], 0);
c47b0cb4
MK
3389
3390 handle_compadr (xops);
3391 if (GET_MODE (operands[1]) == XFmode)
7f98eeb6 3392 {
1d8eaa6b 3393 operands[1] = gen_rtx_MEM (XFmode, latehalf[0]);
b72f00af
RK
3394 middlehalf[1] = adjust_address (operands[1], DImode, size - 8);
3395 latehalf[1] = adjust_address (operands[1], DImode, size - 4);
7f98eeb6
RS
3396 }
3397 else
3398 {
1d8eaa6b 3399 operands[1] = gen_rtx_MEM (DImode, latehalf[0]);
b72f00af 3400 latehalf[1] = adjust_address (operands[1], DImode, size - 4);
7f98eeb6
RS
3401 }
3402 }
3403 else if (size == 12
d7e8d581
RS
3404 && reg_overlap_mentioned_p (middlehalf[0],
3405 XEXP (operands[1], 0)))
7f98eeb6 3406 {
3a58400f
RS
3407 /* Check for two regs used by both source and dest.
3408 Note that this can't happen if the dest is all data regs.
3409 It can happen if the dest is d6, d7, a0.
3410 But in that case, latehalf is an addr reg, so
3411 the code at compadr does ok. */
3412
3413 if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
d7e8d581
RS
3414 || reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
3415 goto compadr;
7f98eeb6
RS
3416
3417 /* JRV says this can't happen: */
4761e388 3418 gcc_assert (!addreg0 && !addreg1);
7f98eeb6 3419
7a1929e1 3420 /* Only the middle reg conflicts; simply put it last. */
c47b0cb4
MK
3421 handle_movsi (operands);
3422 handle_movsi (latehalf);
3423 handle_movsi (middlehalf);
3424
3425 return;
7f98eeb6 3426 }
2fb8a81d 3427 else if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0)))
7f98eeb6
RS
3428 /* If the low half of dest is mentioned in the source memory
3429 address, the arrange to emit the move late half first. */
3430 dest_overlapped_low = 1;
3431 }
3432
79e68feb
RS
3433 /* If one or both operands autodecrementing,
3434 do the two words, high-numbered first. */
3435
3436 /* Likewise, the first move would clobber the source of the second one,
3437 do them in the other order. This happens only for registers;
3438 such overlap can't happen in memory unless the user explicitly
3439 sets it up, and that is an undefined circumstance. */
3440
3441 if (optype0 == PUSHOP || optype1 == PUSHOP
3442 || (optype0 == REGOP && optype1 == REGOP
2505bc97 3443 && ((middlehalf[1] && REGNO (operands[0]) == REGNO (middlehalf[1]))
7f98eeb6
RS
3444 || REGNO (operands[0]) == REGNO (latehalf[1])))
3445 || dest_overlapped_low)
79e68feb
RS
3446 {
3447 /* Make any unoffsettable addresses point at high-numbered word. */
3448 if (addreg0)
c47b0cb4 3449 handle_reg_adjust (addreg0, size - 4);
79e68feb 3450 if (addreg1)
c47b0cb4 3451 handle_reg_adjust (addreg1, size - 4);
79e68feb
RS
3452
3453 /* Do that word. */
c47b0cb4 3454 handle_movsi (latehalf);
79e68feb
RS
3455
3456 /* Undo the adds we just did. */
3457 if (addreg0)
c47b0cb4 3458 handle_reg_adjust (addreg0, -4);
79e68feb 3459 if (addreg1)
c47b0cb4 3460 handle_reg_adjust (addreg1, -4);
79e68feb 3461
2505bc97
RS
3462 if (size == 12)
3463 {
c47b0cb4
MK
3464 handle_movsi (middlehalf);
3465
2505bc97 3466 if (addreg0)
c47b0cb4 3467 handle_reg_adjust (addreg0, -4);
2505bc97 3468 if (addreg1)
c47b0cb4 3469 handle_reg_adjust (addreg1, -4);
2505bc97
RS
3470 }
3471
79e68feb 3472 /* Do low-numbered word. */
c47b0cb4
MK
3473
3474 handle_movsi (operands);
3475 return;
79e68feb
RS
3476 }
3477
3478 /* Normal case: do the two words, low-numbered first. */
3479
c47b0cb4 3480 handle_movsi (operands);
79e68feb 3481
2505bc97
RS
3482 /* Do the middle one of the three words for long double */
3483 if (size == 12)
3484 {
3485 if (addreg0)
c47b0cb4 3486 handle_reg_adjust (addreg0, 4);
2505bc97 3487 if (addreg1)
c47b0cb4 3488 handle_reg_adjust (addreg1, 4);
2505bc97 3489
c47b0cb4 3490 handle_movsi (middlehalf);
2505bc97
RS
3491 }
3492
79e68feb
RS
3493 /* Make any unoffsettable addresses point at high-numbered word. */
3494 if (addreg0)
c47b0cb4 3495 handle_reg_adjust (addreg0, 4);
79e68feb 3496 if (addreg1)
c47b0cb4 3497 handle_reg_adjust (addreg1, 4);
79e68feb
RS
3498
3499 /* Do that word. */
c47b0cb4 3500 handle_movsi (latehalf);
79e68feb
RS
3501
3502 /* Undo the adds we just did. */
3503 if (addreg0)
c47b0cb4
MK
3504 handle_reg_adjust (addreg0, -(size - 4));
3505 if (addreg1)
3506 handle_reg_adjust (addreg1, -(size - 4));
3507
3508 return;
3509}
3510
3511/* Output assembler code to adjust REG by N. */
3512static void
3513output_reg_adjust (rtx reg, int n)
3514{
3515 const char *s;
3516
3517 gcc_assert (GET_MODE (reg) == SImode
3518 && -12 <= n && n != 0 && n <= 12);
3519
3520 switch (n)
2505bc97 3521 {
c47b0cb4
MK
3522 case 12:
3523 s = "add%.l #12,%0";
3524 break;
3525
3526 case 8:
3527 s = "addq%.l #8,%0";
3528 break;
3529
3530 case 4:
3531 s = "addq%.l #4,%0";
3532 break;
3533
3534 case -12:
3535 s = "sub%.l #12,%0";
3536 break;
3537
3538 case -8:
3539 s = "subq%.l #8,%0";
3540 break;
3541
3542 case -4:
3543 s = "subq%.l #4,%0";
3544 break;
3545
3546 default:
3547 gcc_unreachable ();
3548 s = NULL;
2505bc97 3549 }
c47b0cb4
MK
3550
3551 output_asm_insn (s, &reg);
3552}
3553
3554/* Emit rtl code to adjust REG by N. */
3555static void
3556emit_reg_adjust (rtx reg1, int n)
3557{
3558 rtx reg2;
3559
3560 gcc_assert (GET_MODE (reg1) == SImode
3561 && -12 <= n && n != 0 && n <= 12);
3562
3563 reg1 = copy_rtx (reg1);
3564 reg2 = copy_rtx (reg1);
3565
3566 if (n < 0)
3567 emit_insn (gen_subsi3 (reg1, reg2, GEN_INT (-n)));
3568 else if (n > 0)
3569 emit_insn (gen_addsi3 (reg1, reg2, GEN_INT (n)));
3570 else
3571 gcc_unreachable ();
3572}
3573
3574/* Output assembler to load address OPERANDS[0] to register OPERANDS[1]. */
3575static void
3576output_compadr (rtx operands[2])
3577{
3578 output_asm_insn ("lea %a1,%0", operands);
3579}
3580
3581/* Output the best assembler insn for moving operands[1] into operands[0]
3582 as a fullword. */
3583static void
3584output_movsi (rtx operands[2])
3585{
3586 output_asm_insn (singlemove_string (operands), operands);
3587}
3588
3589/* Copy OP and change its mode to MODE. */
3590static rtx
3591copy_operand (rtx op, enum machine_mode mode)
3592{
3593 /* ??? This looks really ugly. There must be a better way
3594 to change a mode on the operand. */
3595 if (GET_MODE (op) != VOIDmode)
2505bc97 3596 {
c47b0cb4
MK
3597 if (REG_P (op))
3598 op = gen_rtx_REG (mode, REGNO (op));
2505bc97 3599 else
c47b0cb4
MK
3600 {
3601 op = copy_rtx (op);
3602 PUT_MODE (op, mode);
3603 }
2505bc97 3604 }
79e68feb 3605
c47b0cb4
MK
3606 return op;
3607}
3608
3609/* Emit rtl code for moving operands[1] into operands[0] as a fullword. */
3610static void
3611emit_movsi (rtx operands[2])
3612{
3613 operands[0] = copy_operand (operands[0], SImode);
3614 operands[1] = copy_operand (operands[1], SImode);
3615
3616 emit_insn (gen_movsi (operands[0], operands[1]));
3617}
3618
3619/* Output assembler code to perform a doubleword move insn
3620 with operands OPERANDS. */
3621const char *
3622output_move_double (rtx *operands)
3623{
3624 handle_move_double (operands,
3625 output_reg_adjust, output_compadr, output_movsi);
3626
79e68feb
RS
3627 return "";
3628}
3629
c47b0cb4
MK
3630/* Output rtl code to perform a doubleword move insn
3631 with operands OPERANDS. */
3632void
3633m68k_emit_move_double (rtx operands[2])
3634{
3635 handle_move_double (operands, emit_reg_adjust, emit_movsi, emit_movsi);
3636}
dcc21c4c
PB
3637
3638/* Ensure mode of ORIG, a REG rtx, is MODE. Returns either ORIG or a
3639 new rtx with the correct mode. */
3640
3641static rtx
3642force_mode (enum machine_mode mode, rtx orig)
3643{
3644 if (mode == GET_MODE (orig))
3645 return orig;
3646
3647 if (REGNO (orig) >= FIRST_PSEUDO_REGISTER)
3648 abort ();
3649
3650 return gen_rtx_REG (mode, REGNO (orig));
3651}
3652
3653static int
3654fp_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3655{
3656 return reg_renumber && FP_REG_P (op);
3657}
3658
3659/* Emit insns to move operands[1] into operands[0].
3660
3661 Return 1 if we have written out everything that needs to be done to
3662 do the move. Otherwise, return 0 and the caller will emit the move
3663 normally.
3664
3665 Note SCRATCH_REG may not be in the proper mode depending on how it
c0220ea4 3666 will be used. This routine is responsible for creating a new copy
dcc21c4c
PB
3667 of SCRATCH_REG in the proper mode. */
3668
3669int
3670emit_move_sequence (rtx *operands, enum machine_mode mode, rtx scratch_reg)
3671{
3672 register rtx operand0 = operands[0];
3673 register rtx operand1 = operands[1];
3674 register rtx tem;
3675
3676 if (scratch_reg
3677 && reload_in_progress && GET_CODE (operand0) == REG
3678 && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
3679 operand0 = reg_equiv_mem[REGNO (operand0)];
3680 else if (scratch_reg
3681 && reload_in_progress && GET_CODE (operand0) == SUBREG
3682 && GET_CODE (SUBREG_REG (operand0)) == REG
3683 && REGNO (SUBREG_REG (operand0)) >= FIRST_PSEUDO_REGISTER)
3684 {
3685 /* We must not alter SUBREG_BYTE (operand0) since that would confuse
3686 the code which tracks sets/uses for delete_output_reload. */
3687 rtx temp = gen_rtx_SUBREG (GET_MODE (operand0),
3688 reg_equiv_mem [REGNO (SUBREG_REG (operand0))],
3689 SUBREG_BYTE (operand0));
3690 operand0 = alter_subreg (&temp);
3691 }
3692
3693 if (scratch_reg
3694 && reload_in_progress && GET_CODE (operand1) == REG
3695 && REGNO (operand1) >= FIRST_PSEUDO_REGISTER)
3696 operand1 = reg_equiv_mem[REGNO (operand1)];
3697 else if (scratch_reg
3698 && reload_in_progress && GET_CODE (operand1) == SUBREG
3699 && GET_CODE (SUBREG_REG (operand1)) == REG
3700 && REGNO (SUBREG_REG (operand1)) >= FIRST_PSEUDO_REGISTER)
3701 {
3702 /* We must not alter SUBREG_BYTE (operand0) since that would confuse
3703 the code which tracks sets/uses for delete_output_reload. */
3704 rtx temp = gen_rtx_SUBREG (GET_MODE (operand1),
3705 reg_equiv_mem [REGNO (SUBREG_REG (operand1))],
3706 SUBREG_BYTE (operand1));
3707 operand1 = alter_subreg (&temp);
3708 }
3709
3710 if (scratch_reg && reload_in_progress && GET_CODE (operand0) == MEM
3711 && ((tem = find_replacement (&XEXP (operand0, 0)))
3712 != XEXP (operand0, 0)))
3713 operand0 = gen_rtx_MEM (GET_MODE (operand0), tem);
3714 if (scratch_reg && reload_in_progress && GET_CODE (operand1) == MEM
3715 && ((tem = find_replacement (&XEXP (operand1, 0)))
3716 != XEXP (operand1, 0)))
3717 operand1 = gen_rtx_MEM (GET_MODE (operand1), tem);
3718
3719 /* Handle secondary reloads for loads/stores of FP registers where
3720 the address is symbolic by using the scratch register */
3721 if (fp_reg_operand (operand0, mode)
3722 && ((GET_CODE (operand1) == MEM
3723 && ! memory_address_p (DFmode, XEXP (operand1, 0)))
3724 || ((GET_CODE (operand1) == SUBREG
3725 && GET_CODE (XEXP (operand1, 0)) == MEM
3726 && !memory_address_p (DFmode, XEXP (XEXP (operand1, 0), 0)))))
3727 && scratch_reg)
3728 {
3729 if (GET_CODE (operand1) == SUBREG)
3730 operand1 = XEXP (operand1, 0);
3731
3732 /* SCRATCH_REG will hold an address. We want
3733 it in SImode regardless of what mode it was originally given
3734 to us. */
3735 scratch_reg = force_mode (SImode, scratch_reg);
3736
3737 /* D might not fit in 14 bits either; for such cases load D into
3738 scratch reg. */
3739 if (!memory_address_p (Pmode, XEXP (operand1, 0)))
3740 {
3741 emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
3742 emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1, 0)),
3743 Pmode,
3744 XEXP (XEXP (operand1, 0), 0),
3745 scratch_reg));
3746 }
3747 else
3748 emit_move_insn (scratch_reg, XEXP (operand1, 0));
3749 emit_insn (gen_rtx_SET (VOIDmode, operand0,
3750 gen_rtx_MEM (mode, scratch_reg)));
3751 return 1;
3752 }
3753 else if (fp_reg_operand (operand1, mode)
3754 && ((GET_CODE (operand0) == MEM
3755 && ! memory_address_p (DFmode, XEXP (operand0, 0)))
3756 || ((GET_CODE (operand0) == SUBREG)
3757 && GET_CODE (XEXP (operand0, 0)) == MEM
3758 && !memory_address_p (DFmode, XEXP (XEXP (operand0, 0), 0))))
3759 && scratch_reg)
3760 {
3761 if (GET_CODE (operand0) == SUBREG)
3762 operand0 = XEXP (operand0, 0);
3763
3764 /* SCRATCH_REG will hold an address and maybe the actual data. We want
3765 it in SIMODE regardless of what mode it was originally given
3766 to us. */
3767 scratch_reg = force_mode (SImode, scratch_reg);
3768
3769 /* D might not fit in 14 bits either; for such cases load D into
3770 scratch reg. */
3771 if (!memory_address_p (Pmode, XEXP (operand0, 0)))
3772 {
3773 emit_move_insn (scratch_reg, XEXP (XEXP (operand0, 0), 1));
3774 emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand0,
3775 0)),
3776 Pmode,
3777 XEXP (XEXP (operand0, 0),
3778 0),
3779 scratch_reg));
3780 }
3781 else
3782 emit_move_insn (scratch_reg, XEXP (operand0, 0));
3783 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_MEM (mode, scratch_reg),
3784 operand1));
3785 return 1;
3786 }
3787 /* Handle secondary reloads for loads of FP registers from constant
3788 expressions by forcing the constant into memory.
3789
3790 use scratch_reg to hold the address of the memory location.
3791
3792 The proper fix is to change PREFERRED_RELOAD_CLASS to return
3793 NO_REGS when presented with a const_int and an register class
3794 containing only FP registers. Doing so unfortunately creates
3795 more problems than it solves. Fix this for 2.5. */
3796 else if (fp_reg_operand (operand0, mode)
3797 && CONSTANT_P (operand1)
3798 && scratch_reg)
3799 {
3800 rtx xoperands[2];
3801
3802 /* SCRATCH_REG will hold an address and maybe the actual data. We want
3803 it in SIMODE regardless of what mode it was originally given
3804 to us. */
3805 scratch_reg = force_mode (SImode, scratch_reg);
3806
3807 /* Force the constant into memory and put the address of the
3808 memory location into scratch_reg. */
3809 xoperands[0] = scratch_reg;
3810 xoperands[1] = XEXP (force_const_mem (mode, operand1), 0);
3811 emit_insn (gen_rtx_SET (mode, scratch_reg, xoperands[1]));
3812
3813 /* Now load the destination register. */
3814 emit_insn (gen_rtx_SET (mode, operand0,
3815 gen_rtx_MEM (mode, scratch_reg)));
3816 return 1;
3817 }
3818
3819 /* Now have insn-emit do whatever it normally does. */
3820 return 0;
3821}
3822
01e304f8
RZ
3823/* Split one or more DImode RTL references into pairs of SImode
3824 references. The RTL can be REG, offsettable MEM, integer constant, or
3825 CONST_DOUBLE. "operands" is a pointer to an array of DImode RTL to
3826 split and "num" is its length. lo_half and hi_half are output arrays
3827 that parallel "operands". */
3828
3829void
3830split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
3831{
3832 while (num--)
3833 {
3834 rtx op = operands[num];
3835
3836 /* simplify_subreg refuses to split volatile memory addresses,
3837 but we still have to handle it. */
3838 if (GET_CODE (op) == MEM)
3839 {
3840 lo_half[num] = adjust_address (op, SImode, 4);
3841 hi_half[num] = adjust_address (op, SImode, 0);
3842 }
3843 else
3844 {
3845 lo_half[num] = simplify_gen_subreg (SImode, op,
3846 GET_MODE (op) == VOIDmode
3847 ? DImode : GET_MODE (op), 4);
3848 hi_half[num] = simplify_gen_subreg (SImode, op,
3849 GET_MODE (op) == VOIDmode
3850 ? DImode : GET_MODE (op), 0);
3851 }
3852 }
3853}
3854
a40ed0f3
KH
3855/* Split X into a base and a constant offset, storing them in *BASE
3856 and *OFFSET respectively. */
3857
3858static void
3859m68k_split_offset (rtx x, rtx *base, HOST_WIDE_INT *offset)
3860{
3861 *offset = 0;
3862 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
3863 {
3864 *offset += INTVAL (XEXP (x, 1));
3865 x = XEXP (x, 0);
3866 }
3867 *base = x;
3868}
3869
3870/* Return true if PATTERN is a PARALLEL suitable for a movem or fmovem
3871 instruction. STORE_P says whether the move is a load or store.
3872
3873 If the instruction uses post-increment or pre-decrement addressing,
3874 AUTOMOD_BASE is the base register and AUTOMOD_OFFSET is the total
3875 adjustment. This adjustment will be made by the first element of
3876 PARALLEL, with the loads or stores starting at element 1. If the
3877 instruction does not use post-increment or pre-decrement addressing,
3878 AUTOMOD_BASE is null, AUTOMOD_OFFSET is 0, and the loads or stores
3879 start at element 0. */
3880
3881bool
3882m68k_movem_pattern_p (rtx pattern, rtx automod_base,
3883 HOST_WIDE_INT automod_offset, bool store_p)
3884{
3885 rtx base, mem_base, set, mem, reg, last_reg;
3886 HOST_WIDE_INT offset, mem_offset;
3887 int i, first, len;
3888 enum reg_class rclass;
3889
3890 len = XVECLEN (pattern, 0);
3891 first = (automod_base != NULL);
3892
3893 if (automod_base)
3894 {
3895 /* Stores must be pre-decrement and loads must be post-increment. */
3896 if (store_p != (automod_offset < 0))
3897 return false;
3898
3899 /* Work out the base and offset for lowest memory location. */
3900 base = automod_base;
3901 offset = (automod_offset < 0 ? automod_offset : 0);
3902 }
3903 else
3904 {
3905 /* Allow any valid base and offset in the first access. */
3906 base = NULL;
3907 offset = 0;
3908 }
3909
3910 last_reg = NULL;
3911 rclass = NO_REGS;
3912 for (i = first; i < len; i++)
3913 {
3914 /* We need a plain SET. */
3915 set = XVECEXP (pattern, 0, i);
3916 if (GET_CODE (set) != SET)
3917 return false;
3918
3919 /* Check that we have a memory location... */
3920 mem = XEXP (set, !store_p);
3921 if (!MEM_P (mem) || !memory_operand (mem, VOIDmode))
3922 return false;
3923
3924 /* ...with the right address. */
3925 if (base == NULL)
3926 {
3927 m68k_split_offset (XEXP (mem, 0), &base, &offset);
3928 /* The ColdFire instruction only allows (An) and (d16,An) modes.
3929 There are no mode restrictions for 680x0 besides the
3930 automodification rules enforced above. */
3931 if (TARGET_COLDFIRE
3932 && !m68k_legitimate_base_reg_p (base, reload_completed))
3933 return false;
3934 }
3935 else
3936 {
3937 m68k_split_offset (XEXP (mem, 0), &mem_base, &mem_offset);
3938 if (!rtx_equal_p (base, mem_base) || offset != mem_offset)
3939 return false;
3940 }
3941
3942 /* Check that we have a register of the required mode and class. */
3943 reg = XEXP (set, store_p);
3944 if (!REG_P (reg)
3945 || !HARD_REGISTER_P (reg)
3946 || GET_MODE (reg) != reg_raw_mode[REGNO (reg)])
3947 return false;
3948
3949 if (last_reg)
3950 {
3951 /* The register must belong to RCLASS and have a higher number
3952 than the register in the previous SET. */
3953 if (!TEST_HARD_REG_BIT (reg_class_contents[rclass], REGNO (reg))
3954 || REGNO (last_reg) >= REGNO (reg))
3955 return false;
3956 }
3957 else
3958 {
3959 /* Work out which register class we need. */
3960 if (INT_REGNO_P (REGNO (reg)))
3961 rclass = GENERAL_REGS;
3962 else if (FP_REGNO_P (REGNO (reg)))
3963 rclass = FP_REGS;
3964 else
3965 return false;
3966 }
3967
3968 last_reg = reg;
3969 offset += GET_MODE_SIZE (GET_MODE (reg));
3970 }
3971
3972 /* If we have an automodification, check whether the final offset is OK. */
3973 if (automod_base && offset != (automod_offset < 0 ? 0 : automod_offset))
3974 return false;
3975
3976 /* Reject unprofitable cases. */
3977 if (len < first + (rclass == FP_REGS ? MIN_FMOVEM_REGS : MIN_MOVEM_REGS))
3978 return false;
3979
3980 return true;
3981}
3982
3983/* Return the assembly code template for a movem or fmovem instruction
3984 whose pattern is given by PATTERN. Store the template's operands
3985 in OPERANDS.
3986
3987 If the instruction uses post-increment or pre-decrement addressing,
3988 AUTOMOD_OFFSET is the total adjustment, otherwise it is 0. STORE_P
3989 is true if this is a store instruction. */
3990
3991const char *
3992m68k_output_movem (rtx *operands, rtx pattern,
3993 HOST_WIDE_INT automod_offset, bool store_p)
3994{
3995 unsigned int mask;
3996 int i, first;
3997
3998 gcc_assert (GET_CODE (pattern) == PARALLEL);
3999 mask = 0;
4000 first = (automod_offset != 0);
4001 for (i = first; i < XVECLEN (pattern, 0); i++)
4002 {
4003 /* When using movem with pre-decrement addressing, register X + D0_REG
4004 is controlled by bit 15 - X. For all other addressing modes,
4005 register X + D0_REG is controlled by bit X. Confusingly, the
4006 register mask for fmovem is in the opposite order to that for
4007 movem. */
4008 unsigned int regno;
4009
4010 gcc_assert (MEM_P (XEXP (XVECEXP (pattern, 0, i), !store_p)));
4011 gcc_assert (REG_P (XEXP (XVECEXP (pattern, 0, i), store_p)));
4012 regno = REGNO (XEXP (XVECEXP (pattern, 0, i), store_p));
4013 if (automod_offset < 0)
4014 {
4015 if (FP_REGNO_P (regno))
4016 mask |= 1 << (regno - FP0_REG);
4017 else
4018 mask |= 1 << (15 - (regno - D0_REG));
4019 }
4020 else
4021 {
4022 if (FP_REGNO_P (regno))
4023 mask |= 1 << (7 - (regno - FP0_REG));
4024 else
4025 mask |= 1 << (regno - D0_REG);
4026 }
4027 }
4028 CC_STATUS_INIT;
4029
4030 if (automod_offset == 0)
4031 operands[0] = XEXP (XEXP (XVECEXP (pattern, 0, first), !store_p), 0);
4032 else if (automod_offset < 0)
4033 operands[0] = gen_rtx_PRE_DEC (Pmode, SET_DEST (XVECEXP (pattern, 0, 0)));
4034 else
4035 operands[0] = gen_rtx_POST_INC (Pmode, SET_DEST (XVECEXP (pattern, 0, 0)));
4036 operands[1] = GEN_INT (mask);
4037 if (FP_REGNO_P (REGNO (XEXP (XVECEXP (pattern, 0, first), store_p))))
4038 {
4039 if (store_p)
1fae2d80 4040 return "fmovem %1,%a0";
a40ed0f3 4041 else
1fae2d80 4042 return "fmovem %a0,%1";
a40ed0f3
KH
4043 }
4044 else
4045 {
4046 if (store_p)
1fae2d80 4047 return "movem%.l %1,%a0";
a40ed0f3 4048 else
1fae2d80 4049 return "movem%.l %a0,%1";
a40ed0f3
KH
4050 }
4051}
4052
79e68feb
RS
4053/* Return a REG that occurs in ADDR with coefficient 1.
4054 ADDR can be effectively incremented by incrementing REG. */
4055
4056static rtx
8a4a2253 4057find_addr_reg (rtx addr)
79e68feb
RS
4058{
4059 while (GET_CODE (addr) == PLUS)
4060 {
4061 if (GET_CODE (XEXP (addr, 0)) == REG)
4062 addr = XEXP (addr, 0);
4063 else if (GET_CODE (XEXP (addr, 1)) == REG)
4064 addr = XEXP (addr, 1);
4065 else if (CONSTANT_P (XEXP (addr, 0)))
4066 addr = XEXP (addr, 1);
4067 else if (CONSTANT_P (XEXP (addr, 1)))
4068 addr = XEXP (addr, 0);
4069 else
4761e388 4070 gcc_unreachable ();
79e68feb 4071 }
4761e388
NS
4072 gcc_assert (GET_CODE (addr) == REG);
4073 return addr;
79e68feb 4074}
9ee3c687 4075
c16eadc7 4076/* Output assembler code to perform a 32-bit 3-operand add. */
9ee3c687 4077
5505f548 4078const char *
8a4a2253 4079output_addsi3 (rtx *operands)
9ee3c687
JW
4080{
4081 if (! operands_match_p (operands[0], operands[1]))
4082 {
4083 if (!ADDRESS_REG_P (operands[1]))
4084 {
4085 rtx tmp = operands[1];
4086
4087 operands[1] = operands[2];
4088 operands[2] = tmp;
4089 }
4090
4091 /* These insns can result from reloads to access
4092 stack slots over 64k from the frame pointer. */
4093 if (GET_CODE (operands[2]) == CONST_INT
218d5a87 4094 && (INTVAL (operands[2]) < -32768 || INTVAL (operands[2]) > 32767))
8c61b6c1 4095 return "move%.l %2,%0\n\tadd%.l %1,%0";
9ee3c687 4096 if (GET_CODE (operands[2]) == REG)
4b3d1177
KH
4097 return MOTOROLA ? "lea (%1,%2.l),%0" : "lea %1@(0,%2:l),%0";
4098 return MOTOROLA ? "lea (%c2,%1),%0" : "lea %1@(%c2),%0";
9ee3c687
JW
4099 }
4100 if (GET_CODE (operands[2]) == CONST_INT)
4101 {
9ee3c687
JW
4102 if (INTVAL (operands[2]) > 0
4103 && INTVAL (operands[2]) <= 8)
4104 return "addq%.l %2,%0";
4105 if (INTVAL (operands[2]) < 0
4106 && INTVAL (operands[2]) >= -8)
4107 {
c5c76735 4108 operands[2] = GEN_INT (- INTVAL (operands[2]));
9ee3c687
JW
4109 return "subq%.l %2,%0";
4110 }
4111 /* On the CPU32 it is faster to use two addql instructions to
4112 add a small integer (8 < N <= 16) to a register.
7a1929e1 4113 Likewise for subql. */
fe95f2f7 4114 if (TUNE_CPU32 && REG_P (operands[0]))
9ee3c687
JW
4115 {
4116 if (INTVAL (operands[2]) > 8
4117 && INTVAL (operands[2]) <= 16)
4118 {
1d8eaa6b 4119 operands[2] = GEN_INT (INTVAL (operands[2]) - 8);
3b4b85c9 4120 return "addq%.l #8,%0\n\taddq%.l %2,%0";
9ee3c687
JW
4121 }
4122 if (INTVAL (operands[2]) < -8
4123 && INTVAL (operands[2]) >= -16)
4124 {
c5c76735 4125 operands[2] = GEN_INT (- INTVAL (operands[2]) - 8);
3b4b85c9 4126 return "subq%.l #8,%0\n\tsubq%.l %2,%0";
9ee3c687
JW
4127 }
4128 }
9ee3c687
JW
4129 if (ADDRESS_REG_P (operands[0])
4130 && INTVAL (operands[2]) >= -0x8000
4131 && INTVAL (operands[2]) < 0x8000)
4132 {
fe95f2f7 4133 if (TUNE_68040)
9ee3c687
JW
4134 return "add%.w %2,%0";
4135 else
4b3d1177 4136 return MOTOROLA ? "lea (%c2,%0),%0" : "lea %0@(%c2),%0";
9ee3c687
JW
4137 }
4138 }
4139 return "add%.l %2,%0";
4140}
79e68feb
RS
4141\f
4142/* Store in cc_status the expressions that the condition codes will
4143 describe after execution of an instruction whose pattern is EXP.
4144 Do not alter them if the instruction would not alter the cc's. */
4145
4146/* On the 68000, all the insns to store in an address register fail to
4147 set the cc's. However, in some cases these instructions can make it
4148 possibly invalid to use the saved cc's. In those cases we clear out
4149 some or all of the saved cc's so they won't be used. */
4150
1d8eaa6b 4151void
8a4a2253 4152notice_update_cc (rtx exp, rtx insn)
79e68feb 4153{
1a8965c4 4154 if (GET_CODE (exp) == SET)
79e68feb
RS
4155 {
4156 if (GET_CODE (SET_SRC (exp)) == CALL)
a0a7fbc9 4157 CC_STATUS_INIT;
79e68feb
RS
4158 else if (ADDRESS_REG_P (SET_DEST (exp)))
4159 {
f5963e61 4160 if (cc_status.value1 && modified_in_p (cc_status.value1, insn))
79e68feb 4161 cc_status.value1 = 0;
f5963e61 4162 if (cc_status.value2 && modified_in_p (cc_status.value2, insn))
79e68feb
RS
4163 cc_status.value2 = 0;
4164 }
f6ab62e8
RS
4165 /* fmoves to memory or data registers do not set the condition
4166 codes. Normal moves _do_ set the condition codes, but not in
4167 a way that is appropriate for comparison with 0, because -0.0
4168 would be treated as a negative nonzero number. Note that it
88512ba0 4169 isn't appropriate to conditionalize this restriction on
f6ab62e8
RS
4170 HONOR_SIGNED_ZEROS because that macro merely indicates whether
4171 we care about the difference between -0.0 and +0.0. */
79e68feb
RS
4172 else if (!FP_REG_P (SET_DEST (exp))
4173 && SET_DEST (exp) != cc0_rtx
4174 && (FP_REG_P (SET_SRC (exp))
4175 || GET_CODE (SET_SRC (exp)) == FIX
f6ab62e8 4176 || FLOAT_MODE_P (GET_MODE (SET_DEST (exp)))))
a0a7fbc9 4177 CC_STATUS_INIT;
79e68feb
RS
4178 /* A pair of move insns doesn't produce a useful overall cc. */
4179 else if (!FP_REG_P (SET_DEST (exp))
4180 && !FP_REG_P (SET_SRC (exp))
4181 && GET_MODE_SIZE (GET_MODE (SET_SRC (exp))) > 4
4182 && (GET_CODE (SET_SRC (exp)) == REG
4183 || GET_CODE (SET_SRC (exp)) == MEM
4184 || GET_CODE (SET_SRC (exp)) == CONST_DOUBLE))
a0a7fbc9 4185 CC_STATUS_INIT;
e1dff52a 4186 else if (SET_DEST (exp) != pc_rtx)
79e68feb
RS
4187 {
4188 cc_status.flags = 0;
e1dff52a
KH
4189 cc_status.value1 = SET_DEST (exp);
4190 cc_status.value2 = SET_SRC (exp);
79e68feb
RS
4191 }
4192 }
4193 else if (GET_CODE (exp) == PARALLEL
4194 && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
4195 {
e1dff52a
KH
4196 rtx dest = SET_DEST (XVECEXP (exp, 0, 0));
4197 rtx src = SET_SRC (XVECEXP (exp, 0, 0));
4198
4199 if (ADDRESS_REG_P (dest))
79e68feb 4200 CC_STATUS_INIT;
e1dff52a 4201 else if (dest != pc_rtx)
79e68feb
RS
4202 {
4203 cc_status.flags = 0;
e1dff52a
KH
4204 cc_status.value1 = dest;
4205 cc_status.value2 = src;
79e68feb
RS
4206 }
4207 }
4208 else
4209 CC_STATUS_INIT;
4210 if (cc_status.value2 != 0
4211 && ADDRESS_REG_P (cc_status.value2)
4212 && GET_MODE (cc_status.value2) == QImode)
4213 CC_STATUS_INIT;
1a8965c4 4214 if (cc_status.value2 != 0)
79e68feb
RS
4215 switch (GET_CODE (cc_status.value2))
4216 {
996a5f59 4217 case ASHIFT: case ASHIFTRT: case LSHIFTRT:
79e68feb 4218 case ROTATE: case ROTATERT:
a126dc3a
RH
4219 /* These instructions always clear the overflow bit, and set
4220 the carry to the bit shifted out. */
1afac9a6 4221 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
a126dc3a
RH
4222 break;
4223
4224 case PLUS: case MINUS: case MULT:
4225 case DIV: case UDIV: case MOD: case UMOD: case NEG:
79e68feb
RS
4226 if (GET_MODE (cc_status.value2) != VOIDmode)
4227 cc_status.flags |= CC_NO_OVERFLOW;
4228 break;
4229 case ZERO_EXTEND:
4230 /* (SET r1 (ZERO_EXTEND r2)) on this machine
4231 ends with a move insn moving r2 in r2's mode.
4232 Thus, the cc's are set for r2.
7a1929e1 4233 This can set N bit spuriously. */
79e68feb 4234 cc_status.flags |= CC_NOT_NEGATIVE;
1d8eaa6b
AS
4235
4236 default:
4237 break;
79e68feb
RS
4238 }
4239 if (cc_status.value1 && GET_CODE (cc_status.value1) == REG
4240 && cc_status.value2
4241 && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
4242 cc_status.value2 = 0;
4243 if (((cc_status.value1 && FP_REG_P (cc_status.value1))
1a8965c4 4244 || (cc_status.value2 && FP_REG_P (cc_status.value2))))
79e68feb 4245 cc_status.flags = CC_IN_68881;
67595cbb
RZ
4246 if (cc_status.value2 && GET_CODE (cc_status.value2) == COMPARE
4247 && GET_MODE_CLASS (GET_MODE (XEXP (cc_status.value2, 0))) == MODE_FLOAT)
4248 {
4249 cc_status.flags = CC_IN_68881;
4250 if (!FP_REG_P (XEXP (cc_status.value2, 0)))
4251 cc_status.flags |= CC_REVERSED;
4252 }
79e68feb
RS
4253}
4254\f
5505f548 4255const char *
8a4a2253 4256output_move_const_double (rtx *operands)
79e68feb 4257{
1a8965c4 4258 int code = standard_68881_constant_p (operands[1]);
79e68feb 4259
1a8965c4 4260 if (code != 0)
79e68feb 4261 {
1a8965c4 4262 static char buf[40];
79e68feb 4263
3b4b85c9 4264 sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
1a8965c4 4265 return buf;
79e68feb 4266 }
1a8965c4 4267 return "fmove%.d %1,%0";
79e68feb
RS
4268}
4269
5505f548 4270const char *
8a4a2253 4271output_move_const_single (rtx *operands)
79e68feb 4272{
1a8965c4 4273 int code = standard_68881_constant_p (operands[1]);
79e68feb 4274
1a8965c4 4275 if (code != 0)
79e68feb 4276 {
1a8965c4 4277 static char buf[40];
79e68feb 4278
3b4b85c9 4279 sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
1a8965c4 4280 return buf;
79e68feb 4281 }
1a8965c4 4282 return "fmove%.s %f1,%0";
79e68feb
RS
4283}
4284
4285/* Return nonzero if X, a CONST_DOUBLE, has a value that we can get
4286 from the "fmovecr" instruction.
4287 The value, anded with 0xff, gives the code to use in fmovecr
4288 to get the desired constant. */
4289
7a1929e1 4290/* This code has been fixed for cross-compilation. */
c1cfb2ae
RS
4291
4292static int inited_68881_table = 0;
4293
5505f548 4294static const char *const strings_68881[7] = {
c1cfb2ae
RS
4295 "0.0",
4296 "1.0",
4297 "10.0",
4298 "100.0",
4299 "10000.0",
4300 "1e8",
4301 "1e16"
a0a7fbc9 4302};
c1cfb2ae 4303
8b60264b 4304static const int codes_68881[7] = {
c1cfb2ae
RS
4305 0x0f,
4306 0x32,
4307 0x33,
4308 0x34,
4309 0x35,
4310 0x36,
4311 0x37
a0a7fbc9 4312};
c1cfb2ae
RS
4313
4314REAL_VALUE_TYPE values_68881[7];
4315
4316/* Set up values_68881 array by converting the decimal values
7a1929e1 4317 strings_68881 to binary. */
c1cfb2ae
RS
4318
4319void
8a4a2253 4320init_68881_table (void)
c1cfb2ae
RS
4321{
4322 int i;
4323 REAL_VALUE_TYPE r;
4324 enum machine_mode mode;
4325
16d82c3c 4326 mode = SFmode;
c1cfb2ae
RS
4327 for (i = 0; i < 7; i++)
4328 {
4329 if (i == 6)
16d82c3c 4330 mode = DFmode;
c1cfb2ae
RS
4331 r = REAL_VALUE_ATOF (strings_68881[i], mode);
4332 values_68881[i] = r;
4333 }
4334 inited_68881_table = 1;
4335}
79e68feb
RS
4336
4337int
8a4a2253 4338standard_68881_constant_p (rtx x)
79e68feb 4339{
c1cfb2ae
RS
4340 REAL_VALUE_TYPE r;
4341 int i;
79e68feb 4342
e18db50d 4343 /* fmovecr must be emulated on the 68040 and 68060, so it shouldn't be
7a1929e1 4344 used at all on those chips. */
9cf106c8 4345 if (TUNE_68040_60)
79e68feb
RS
4346 return 0;
4347
c1cfb2ae
RS
4348 if (! inited_68881_table)
4349 init_68881_table ();
4350
4351 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4352
64c0b414
AS
4353 /* Use REAL_VALUES_IDENTICAL instead of REAL_VALUES_EQUAL so that -0.0
4354 is rejected. */
c1cfb2ae
RS
4355 for (i = 0; i < 6; i++)
4356 {
64c0b414 4357 if (REAL_VALUES_IDENTICAL (r, values_68881[i]))
c1cfb2ae
RS
4358 return (codes_68881[i]);
4359 }
4360
79e68feb
RS
4361 if (GET_MODE (x) == SFmode)
4362 return 0;
c1cfb2ae
RS
4363
4364 if (REAL_VALUES_EQUAL (r, values_68881[6]))
4365 return (codes_68881[6]);
4366
79e68feb
RS
4367 /* larger powers of ten in the constants ram are not used
4368 because they are not equal to a `double' C constant. */
4369 return 0;
4370}
4371
4372/* If X is a floating-point constant, return the logarithm of X base 2,
4373 or 0 if X is not a power of 2. */
4374
4375int
8a4a2253 4376floating_exact_log2 (rtx x)
79e68feb 4377{
c1cfb2ae 4378 REAL_VALUE_TYPE r, r1;
eaff3bf8 4379 int exp;
79e68feb 4380
c1cfb2ae 4381 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
79e68feb 4382
eaff3bf8 4383 if (REAL_VALUES_LESS (r, dconst1))
79e68feb
RS
4384 return 0;
4385
eaff3bf8 4386 exp = real_exponent (&r);
6ef9a246 4387 real_2expN (&r1, exp, DFmode);
eaff3bf8
RH
4388 if (REAL_VALUES_EQUAL (r1, r))
4389 return exp;
4390
79e68feb
RS
4391 return 0;
4392}
4393\f
79e68feb
RS
4394/* A C compound statement to output to stdio stream STREAM the
4395 assembler syntax for an instruction operand X. X is an RTL
4396 expression.
4397
4398 CODE is a value that can be used to specify one of several ways
4399 of printing the operand. It is used when identical operands
4400 must be printed differently depending on the context. CODE
4401 comes from the `%' specification that was used to request
4402 printing of the operand. If the specification was just `%DIGIT'
4403 then CODE is 0; if the specification was `%LTR DIGIT' then CODE
4404 is the ASCII code for LTR.
4405
4406 If X is a register, this macro should print the register's name.
4407 The names can be found in an array `reg_names' whose type is
4408 `char *[]'. `reg_names' is initialized from `REGISTER_NAMES'.
4409
4410 When the machine description has a specification `%PUNCT' (a `%'
4411 followed by a punctuation character), this macro is called with
4412 a null pointer for X and the punctuation character for CODE.
4413
4414 The m68k specific codes are:
4415
4416 '.' for dot needed in Motorola-style opcode names.
4417 '-' for an operand pushing on the stack:
4418 sp@-, -(sp) or -(%sp) depending on the style of syntax.
4419 '+' for an operand pushing on the stack:
4420 sp@+, (sp)+ or (%sp)+ depending on the style of syntax.
4421 '@' for a reference to the top word on the stack:
4422 sp@, (sp) or (%sp) depending on the style of syntax.
4423 '#' for an immediate operand prefix (# in MIT and Motorola syntax
5ee084df 4424 but & in SGS syntax).
79e68feb
RS
4425 '!' for the cc register (used in an `and to cc' insn).
4426 '$' for the letter `s' in an op code, but only on the 68040.
4427 '&' for the letter `d' in an op code, but only on the 68040.
2ac5f14a 4428 '/' for register prefix needed by longlong.h.
a40ed0f3 4429 '?' for m68k_library_id_string
79e68feb
RS
4430
4431 'b' for byte insn (no effect, on the Sun; this is for the ISI).
4432 'd' to force memory addressing to be absolute, not relative.
4433 'f' for float insn (print a CONST_DOUBLE as a float rather than in hex)
79e68feb
RS
4434 'x' for float insn (print a CONST_DOUBLE as a float rather than in hex),
4435 or print pair of registers as rx:ry.
29ca003a
RS
4436 'p' print an address with @PLTPC attached, but only if the operand
4437 is not locally-bound. */
79e68feb
RS
4438
4439void
8a4a2253 4440print_operand (FILE *file, rtx op, int letter)
79e68feb 4441{
79e68feb
RS
4442 if (letter == '.')
4443 {
e6d98cb0
BI
4444 if (MOTOROLA)
4445 fprintf (file, ".");
79e68feb
RS
4446 }
4447 else if (letter == '#')
e6d98cb0 4448 asm_fprintf (file, "%I");
79e68feb 4449 else if (letter == '-')
4b3d1177 4450 asm_fprintf (file, MOTOROLA ? "-(%Rsp)" : "%Rsp@-");
79e68feb 4451 else if (letter == '+')
4b3d1177 4452 asm_fprintf (file, MOTOROLA ? "(%Rsp)+" : "%Rsp@+");
79e68feb 4453 else if (letter == '@')
4b3d1177 4454 asm_fprintf (file, MOTOROLA ? "(%Rsp)" : "%Rsp@");
79e68feb 4455 else if (letter == '!')
e6d98cb0 4456 asm_fprintf (file, "%Rfpcr");
79e68feb
RS
4457 else if (letter == '$')
4458 {
b101567e 4459 if (TARGET_68040)
e6d98cb0 4460 fprintf (file, "s");
79e68feb
RS
4461 }
4462 else if (letter == '&')
4463 {
b101567e 4464 if (TARGET_68040)
e6d98cb0 4465 fprintf (file, "d");
79e68feb 4466 }
2ac5f14a 4467 else if (letter == '/')
e6d98cb0 4468 asm_fprintf (file, "%R");
a40ed0f3
KH
4469 else if (letter == '?')
4470 asm_fprintf (file, m68k_library_id_string);
29ca003a 4471 else if (letter == 'p')
2c8ec431 4472 {
29ca003a
RS
4473 output_addr_const (file, op);
4474 if (!(GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op)))
4475 fprintf (file, "@PLTPC");
2c8ec431 4476 }
79e68feb
RS
4477 else if (GET_CODE (op) == REG)
4478 {
1a8965c4
AS
4479 if (letter == 'R')
4480 /* Print out the second register name of a register pair.
4481 I.e., R (6) => 7. */
01bbf777 4482 fputs (M68K_REGNAME(REGNO (op) + 1), file);
79e68feb 4483 else
01bbf777 4484 fputs (M68K_REGNAME(REGNO (op)), file);
79e68feb
RS
4485 }
4486 else if (GET_CODE (op) == MEM)
4487 {
4488 output_address (XEXP (op, 0));
4489 if (letter == 'd' && ! TARGET_68020
4490 && CONSTANT_ADDRESS_P (XEXP (op, 0))
4491 && !(GET_CODE (XEXP (op, 0)) == CONST_INT
4492 && INTVAL (XEXP (op, 0)) < 0x8000
4493 && INTVAL (XEXP (op, 0)) >= -0x8000))
4b3d1177 4494 fprintf (file, MOTOROLA ? ".l" : ":l");
79e68feb 4495 }
79e68feb
RS
4496 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == SFmode)
4497 {
c1cfb2ae 4498 REAL_VALUE_TYPE r;
6ae89ea8 4499 long l;
c1cfb2ae 4500 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6ae89ea8 4501 REAL_VALUE_TO_TARGET_SINGLE (r, l);
429ce992 4502 asm_fprintf (file, "%I0x%lx", l & 0xFFFFFFFF);
c1cfb2ae
RS
4503 }
4504 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == XFmode)
4505 {
4506 REAL_VALUE_TYPE r;
6ae89ea8 4507 long l[3];
c1cfb2ae 4508 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6ae89ea8 4509 REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
429ce992
AS
4510 asm_fprintf (file, "%I0x%lx%08lx%08lx", l[0] & 0xFFFFFFFF,
4511 l[1] & 0xFFFFFFFF, l[2] & 0xFFFFFFFF);
79e68feb 4512 }
e2c0a924 4513 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == DFmode)
79e68feb 4514 {
c1cfb2ae 4515 REAL_VALUE_TYPE r;
6ae89ea8 4516 long l[2];
c1cfb2ae 4517 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6ae89ea8 4518 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
429ce992 4519 asm_fprintf (file, "%I0x%lx%08lx", l[0] & 0xFFFFFFFF, l[1] & 0xFFFFFFFF);
79e68feb
RS
4520 }
4521 else
4522 {
2c8ec431
DL
4523 /* Use `print_operand_address' instead of `output_addr_const'
4524 to ensure that we print relevant PIC stuff. */
1f85a612 4525 asm_fprintf (file, "%I");
2c8ec431
DL
4526 if (TARGET_PCREL
4527 && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST))
4528 print_operand_address (file, op);
4529 else
4530 output_addr_const (file, op);
79e68feb
RS
4531 }
4532}
4533
75df395f
MK
4534/* Return string for TLS relocation RELOC. */
4535
4536static const char *
4537m68k_get_reloc_decoration (enum m68k_reloc reloc)
4538{
4539 /* To my knowledge, !MOTOROLA assemblers don't support TLS. */
4540 gcc_assert (MOTOROLA || reloc == RELOC_GOT);
4541
4542 switch (reloc)
4543 {
4544 case RELOC_GOT:
4545 if (MOTOROLA)
4546 {
4547 if (flag_pic == 1 && TARGET_68020)
4548 return "@GOT.w";
4549 else
4550 return "@GOT";
4551 }
4552 else
4553 {
4554 if (TARGET_68020)
4555 {
4556 switch (flag_pic)
4557 {
4558 case 1:
4559 return ":w";
4560 case 2:
4561 return ":l";
4562 default:
4563 return "";
4564 }
4565 }
4566 }
4567
4568 case RELOC_TLSGD:
4569 return "@TLSGD";
4570
4571 case RELOC_TLSLDM:
4572 return "@TLSLDM";
4573
4574 case RELOC_TLSLDO:
4575 return "@TLSLDO";
4576
4577 case RELOC_TLSIE:
4578 return "@TLSIE";
4579
4580 case RELOC_TLSLE:
4581 return "@TLSLE";
4582
4583 default:
4584 gcc_unreachable ();
4585 }
4586}
4587
884316ff
JM
4588/* m68k implementation of OUTPUT_ADDR_CONST_EXTRA. */
4589
4590bool
4591m68k_output_addr_const_extra (FILE *file, rtx x)
4592{
75df395f
MK
4593 if (GET_CODE (x) == UNSPEC)
4594 {
4595 switch (XINT (x, 1))
4596 {
4597 case UNSPEC_RELOC16:
4598 case UNSPEC_RELOC32:
4599 output_addr_const (file, XVECEXP (x, 0, 0));
f878882b
AS
4600 fputs (m68k_get_reloc_decoration
4601 ((enum m68k_reloc) INTVAL (XVECEXP (x, 0, 1))), file);
75df395f 4602 return true;
884316ff 4603
75df395f
MK
4604 default:
4605 break;
4606 }
4607 }
4608
4609 return false;
4610}
4611
4612/* M68K implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL. */
4613
4614static void
4615m68k_output_dwarf_dtprel (FILE *file, int size, rtx x)
4616{
4617 gcc_assert (size == 4);
4618 fputs ("\t.long\t", file);
4619 output_addr_const (file, x);
4620 fputs ("@TLSLDO+0x8000", file);
884316ff
JM
4621}
4622
7b0f476d
AS
4623/* In the name of slightly smaller debug output, and to cater to
4624 general assembler lossage, recognize various UNSPEC sequences
4625 and turn them back into a direct symbol reference. */
4626
4627static rtx
33d67485 4628m68k_delegitimize_address (rtx orig_x)
7b0f476d 4629{
33d67485 4630 rtx x, y;
7b0f476d
AS
4631 rtx addend = NULL_RTX;
4632 rtx result;
4633
33d67485
AS
4634 orig_x = delegitimize_mem_from_attrs (orig_x);
4635 if (! MEM_P (orig_x))
4636 return orig_x;
4637
4638 x = XEXP (orig_x, 0);
7b0f476d
AS
4639
4640 if (GET_CODE (x) == PLUS
4641 && GET_CODE (XEXP (x, 1)) == CONST
4642 && REG_P (XEXP (x, 0))
4643 && REGNO (XEXP (x, 0)) == PIC_REG)
4644 {
4645 y = x = XEXP (XEXP (x, 1), 0);
4646
4647 /* Handle an addend. */
4648 if ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
4649 && CONST_INT_P (XEXP (x, 1)))
4650 {
4651 addend = XEXP (x, 1);
4652 x = XEXP (x, 0);
4653 }
4654
4655 if (GET_CODE (x) == UNSPEC
4656 && (XINT (x, 1) == UNSPEC_RELOC16
4657 || XINT (x, 1) == UNSPEC_RELOC32))
4658 {
4659 result = XVECEXP (x, 0, 0);
4660 if (addend)
4661 {
4662 if (GET_CODE (y) == PLUS)
4663 result = gen_rtx_PLUS (Pmode, result, addend);
4664 else
4665 result = gen_rtx_MINUS (Pmode, result, addend);
4666 result = gen_rtx_CONST (Pmode, result);
4667 }
4668 return result;
4669 }
4670 }
4671
4672 return orig_x;
4673}
4674
79e68feb
RS
4675\f
4676/* A C compound statement to output to stdio stream STREAM the
4677 assembler syntax for an instruction operand that is a memory
4678 reference whose address is ADDR. ADDR is an RTL expression.
4679
4680 Note that this contains a kludge that knows that the only reason
4681 we have an address (plus (label_ref...) (reg...)) when not generating
4682 PIC code is in the insn before a tablejump, and we know that m68k.md
4683 generates a label LInnn: on such an insn.
4684
4685 It is possible for PIC to generate a (plus (label_ref...) (reg...))
4686 and we handle that just like we would a (plus (symbol_ref...) (reg...)).
4687
79e68feb
RS
4688 This routine is responsible for distinguishing between -fpic and -fPIC
4689 style relocations in an address. When generating -fpic code the
112cdef5
KH
4690 offset is output in word mode (e.g. movel a5@(_foo:w), a0). When generating
4691 -fPIC code the offset is output in long mode (e.g. movel a5@(_foo:l), a0) */
79e68feb
RS
4692
4693void
8a4a2253 4694print_operand_address (FILE *file, rtx addr)
79e68feb 4695{
fc2241eb
RS
4696 struct m68k_address address;
4697
4698 if (!m68k_decompose_address (QImode, addr, true, &address))
4699 gcc_unreachable ();
4700
4701 if (address.code == PRE_DEC)
4b3d1177
KH
4702 fprintf (file, MOTOROLA ? "-(%s)" : "%s@-",
4703 M68K_REGNAME (REGNO (address.base)));
fc2241eb 4704 else if (address.code == POST_INC)
4b3d1177
KH
4705 fprintf (file, MOTOROLA ? "(%s)+" : "%s@+",
4706 M68K_REGNAME (REGNO (address.base)));
fc2241eb
RS
4707 else if (!address.base && !address.index)
4708 {
4709 /* A constant address. */
4710 gcc_assert (address.offset == addr);
4711 if (GET_CODE (addr) == CONST_INT)
4712 {
4713 /* (xxx).w or (xxx).l. */
4714 if (IN_RANGE (INTVAL (addr), -0x8000, 0x7fff))
4b3d1177 4715 fprintf (file, MOTOROLA ? "%d.w" : "%d:w", (int) INTVAL (addr));
a0a7fbc9 4716 else
fc2241eb 4717 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
a0a7fbc9 4718 }
fc2241eb 4719 else if (TARGET_PCREL)
a0a7fbc9 4720 {
fc2241eb
RS
4721 /* (d16,PC) or (bd,PC,Xn) (with suppressed index register). */
4722 fputc ('(', file);
4723 output_addr_const (file, addr);
4724 asm_fprintf (file, flag_pic == 1 ? ":w,%Rpc)" : ":l,%Rpc)");
a0a7fbc9 4725 }
fc2241eb 4726 else
a0a7fbc9 4727 {
fc2241eb
RS
4728 /* (xxx).l. We need a special case for SYMBOL_REF if the symbol
4729 name ends in `.<letter>', as the last 2 characters can be
4730 mistaken as a size suffix. Put the name in parentheses. */
4731 if (GET_CODE (addr) == SYMBOL_REF
4732 && strlen (XSTR (addr, 0)) > 2
4733 && XSTR (addr, 0)[strlen (XSTR (addr, 0)) - 2] == '.')
a0a7fbc9 4734 {
fc2241eb
RS
4735 putc ('(', file);
4736 output_addr_const (file, addr);
4737 putc (')', file);
a0a7fbc9
AS
4738 }
4739 else
fc2241eb 4740 output_addr_const (file, addr);
a0a7fbc9 4741 }
fc2241eb
RS
4742 }
4743 else
4744 {
4745 int labelno;
4746
4747 /* If ADDR is a (d8,pc,Xn) address, this is the number of the
44c7bd63 4748 label being accessed, otherwise it is -1. */
fc2241eb
RS
4749 labelno = (address.offset
4750 && !address.base
4751 && GET_CODE (address.offset) == LABEL_REF
4752 ? CODE_LABEL_NUMBER (XEXP (address.offset, 0))
4753 : -1);
4754 if (MOTOROLA)
a0a7fbc9 4755 {
fc2241eb
RS
4756 /* Print the "offset(base" component. */
4757 if (labelno >= 0)
e59d83aa 4758 asm_fprintf (file, "%LL%d(%Rpc,", labelno);
fc2241eb 4759 else
a0a7fbc9 4760 {
fc2241eb 4761 if (address.offset)
75df395f
MK
4762 output_addr_const (file, address.offset);
4763
fc2241eb
RS
4764 putc ('(', file);
4765 if (address.base)
4766 fputs (M68K_REGNAME (REGNO (address.base)), file);
a0a7fbc9 4767 }
fc2241eb
RS
4768 /* Print the ",index" component, if any. */
4769 if (address.index)
a0a7fbc9 4770 {
fc2241eb
RS
4771 if (address.base)
4772 putc (',', file);
4773 fprintf (file, "%s.%c",
4774 M68K_REGNAME (REGNO (address.index)),
4775 GET_MODE (address.index) == HImode ? 'w' : 'l');
4776 if (address.scale != 1)
4777 fprintf (file, "*%d", address.scale);
a0a7fbc9 4778 }
a0a7fbc9 4779 putc (')', file);
a0a7fbc9 4780 }
fc2241eb 4781 else /* !MOTOROLA */
a0a7fbc9 4782 {
fc2241eb
RS
4783 if (!address.offset && !address.index)
4784 fprintf (file, "%s@", M68K_REGNAME (REGNO (address.base)));
a0a7fbc9 4785 else
a0a7fbc9 4786 {
fc2241eb
RS
4787 /* Print the "base@(offset" component. */
4788 if (labelno >= 0)
e59d83aa 4789 asm_fprintf (file, "%Rpc@(%LL%d", labelno);
fc2241eb
RS
4790 else
4791 {
4792 if (address.base)
4793 fputs (M68K_REGNAME (REGNO (address.base)), file);
4794 fprintf (file, "@(");
4795 if (address.offset)
75df395f 4796 output_addr_const (file, address.offset);
fc2241eb
RS
4797 }
4798 /* Print the ",index" component, if any. */
4799 if (address.index)
4800 {
4801 fprintf (file, ",%s:%c",
4802 M68K_REGNAME (REGNO (address.index)),
4803 GET_MODE (address.index) == HImode ? 'w' : 'l');
4804 if (address.scale != 1)
4805 fprintf (file, ":%d", address.scale);
4806 }
a0a7fbc9
AS
4807 putc (')', file);
4808 }
a0a7fbc9 4809 }
79e68feb
RS
4810 }
4811}
af13f02d
JW
4812\f
4813/* Check for cases where a clr insns can be omitted from code using
4814 strict_low_part sets. For example, the second clrl here is not needed:
4815 clrl d0; movw a0@+,d0; use d0; clrl d0; movw a0@+; use d0; ...
4816
4817 MODE is the mode of this STRICT_LOW_PART set. FIRST_INSN is the clear
4818 insn we are checking for redundancy. TARGET is the register set by the
4819 clear insn. */
4820
8a4a2253
BI
4821bool
4822strict_low_part_peephole_ok (enum machine_mode mode, rtx first_insn,
4823 rtx target)
af13f02d 4824{
39250081 4825 rtx p = first_insn;
af13f02d 4826
39250081 4827 while ((p = PREV_INSN (p)))
af13f02d 4828 {
39250081
RZ
4829 if (NOTE_INSN_BASIC_BLOCK_P (p))
4830 return false;
4831
4832 if (NOTE_P (p))
4833 continue;
4834
af13f02d 4835 /* If it isn't an insn, then give up. */
39250081 4836 if (!INSN_P (p))
8a4a2253 4837 return false;
af13f02d
JW
4838
4839 if (reg_set_p (target, p))
4840 {
4841 rtx set = single_set (p);
4842 rtx dest;
4843
4844 /* If it isn't an easy to recognize insn, then give up. */
4845 if (! set)
8a4a2253 4846 return false;
af13f02d
JW
4847
4848 dest = SET_DEST (set);
4849
4850 /* If this sets the entire target register to zero, then our
4851 first_insn is redundant. */
4852 if (rtx_equal_p (dest, target)
4853 && SET_SRC (set) == const0_rtx)
8a4a2253 4854 return true;
af13f02d
JW
4855 else if (GET_CODE (dest) == STRICT_LOW_PART
4856 && GET_CODE (XEXP (dest, 0)) == REG
4857 && REGNO (XEXP (dest, 0)) == REGNO (target)
4858 && (GET_MODE_SIZE (GET_MODE (XEXP (dest, 0)))
4859 <= GET_MODE_SIZE (mode)))
4860 /* This is a strict low part set which modifies less than
4861 we are using, so it is safe. */
4862 ;
4863 else
8a4a2253 4864 return false;
af13f02d 4865 }
af13f02d
JW
4866 }
4867
8a4a2253 4868 return false;
af13f02d 4869}
67cd4f83 4870
2c8ec431
DL
4871/* Operand predicates for implementing asymmetric pc-relative addressing
4872 on m68k. The m68k supports pc-relative addressing (mode 7, register 2)
dab66575 4873 when used as a source operand, but not as a destination operand.
2c8ec431
DL
4874
4875 We model this by restricting the meaning of the basic predicates
4876 (general_operand, memory_operand, etc) to forbid the use of this
4877 addressing mode, and then define the following predicates that permit
4878 this addressing mode. These predicates can then be used for the
4879 source operands of the appropriate instructions.
4880
4881 n.b. While it is theoretically possible to change all machine patterns
4882 to use this addressing more where permitted by the architecture,
4883 it has only been implemented for "common" cases: SImode, HImode, and
4884 QImode operands, and only for the principle operations that would
4885 require this addressing mode: data movement and simple integer operations.
4886
4887 In parallel with these new predicates, two new constraint letters
4888 were defined: 'S' and 'T'. 'S' is the -mpcrel analog of 'm'.
4889 'T' replaces 's' in the non-pcrel case. It is a no-op in the pcrel case.
4890 In the pcrel case 's' is only valid in combination with 'a' registers.
4891 See addsi3, subsi3, cmpsi, and movsi patterns for a better understanding
4892 of how these constraints are used.
4893
4894 The use of these predicates is strictly optional, though patterns that
4895 don't will cause an extra reload register to be allocated where one
4896 was not necessary:
4897
4898 lea (abc:w,%pc),%a0 ; need to reload address
4899 moveq &1,%d1 ; since write to pc-relative space
4900 movel %d1,%a0@ ; is not allowed
4901 ...
4902 lea (abc:w,%pc),%a1 ; no need to reload address here
4903 movel %a1@,%d0 ; since "movel (abc:w,%pc),%d0" is ok
4904
4905 For more info, consult tiemann@cygnus.com.
4906
4907
4908 All of the ugliness with predicates and constraints is due to the
4909 simple fact that the m68k does not allow a pc-relative addressing
4910 mode as a destination. gcc does not distinguish between source and
4911 destination addresses. Hence, if we claim that pc-relative address
331d9186 4912 modes are valid, e.g. TARGET_LEGITIMATE_ADDRESS_P accepts them, then we
2c8ec431
DL
4913 end up with invalid code. To get around this problem, we left
4914 pc-relative modes as invalid addresses, and then added special
4915 predicates and constraints to accept them.
4916
4917 A cleaner way to handle this is to modify gcc to distinguish
4918 between source and destination addresses. We can then say that
4919 pc-relative is a valid source address but not a valid destination
4920 address, and hopefully avoid a lot of the predicate and constraint
4921 hackery. Unfortunately, this would be a pretty big change. It would
4922 be a useful change for a number of ports, but there aren't any current
4923 plans to undertake this.
4924
4925 ***************************************************************************/
4926
4927
5505f548 4928const char *
8a4a2253 4929output_andsi3 (rtx *operands)
29ae8a3c
RK
4930{
4931 int logval;
4932 if (GET_CODE (operands[2]) == CONST_INT
25c99d8f 4933 && (INTVAL (operands[2]) | 0xffff) == -1
29ae8a3c
RK
4934 && (DATA_REG_P (operands[0])
4935 || offsettable_memref_p (operands[0]))
9425fb04 4936 && !TARGET_COLDFIRE)
29ae8a3c
RK
4937 {
4938 if (GET_CODE (operands[0]) != REG)
b72f00af 4939 operands[0] = adjust_address (operands[0], HImode, 2);
1d8eaa6b 4940 operands[2] = GEN_INT (INTVAL (operands[2]) & 0xffff);
29ae8a3c
RK
4941 /* Do not delete a following tstl %0 insn; that would be incorrect. */
4942 CC_STATUS_INIT;
4943 if (operands[2] == const0_rtx)
4944 return "clr%.w %0";
4945 return "and%.w %2,%0";
4946 }
4947 if (GET_CODE (operands[2]) == CONST_INT
c4406f74 4948 && (logval = exact_log2 (~ INTVAL (operands[2]) & 0xffffffff)) >= 0
29ae8a3c
RK
4949 && (DATA_REG_P (operands[0])
4950 || offsettable_memref_p (operands[0])))
4951 {
4952 if (DATA_REG_P (operands[0]))
a0a7fbc9 4953 operands[1] = GEN_INT (logval);
29ae8a3c
RK
4954 else
4955 {
b72f00af 4956 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
1d8eaa6b 4957 operands[1] = GEN_INT (logval % 8);
29ae8a3c
RK
4958 }
4959 /* This does not set condition codes in a standard way. */
4960 CC_STATUS_INIT;
4961 return "bclr %1,%0";
4962 }
4963 return "and%.l %2,%0";
4964}
4965
5505f548 4966const char *
8a4a2253 4967output_iorsi3 (rtx *operands)
29ae8a3c
RK
4968{
4969 register int logval;
4970 if (GET_CODE (operands[2]) == CONST_INT
4971 && INTVAL (operands[2]) >> 16 == 0
4972 && (DATA_REG_P (operands[0])
4973 || offsettable_memref_p (operands[0]))
9425fb04 4974 && !TARGET_COLDFIRE)
29ae8a3c
RK
4975 {
4976 if (GET_CODE (operands[0]) != REG)
b72f00af 4977 operands[0] = adjust_address (operands[0], HImode, 2);
29ae8a3c
RK
4978 /* Do not delete a following tstl %0 insn; that would be incorrect. */
4979 CC_STATUS_INIT;
4980 if (INTVAL (operands[2]) == 0xffff)
4981 return "mov%.w %2,%0";
4982 return "or%.w %2,%0";
4983 }
4984 if (GET_CODE (operands[2]) == CONST_INT
c4406f74 4985 && (logval = exact_log2 (INTVAL (operands[2]) & 0xffffffff)) >= 0
29ae8a3c
RK
4986 && (DATA_REG_P (operands[0])
4987 || offsettable_memref_p (operands[0])))
4988 {
4989 if (DATA_REG_P (operands[0]))
b72f00af 4990 operands[1] = GEN_INT (logval);
29ae8a3c
RK
4991 else
4992 {
b72f00af 4993 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
1d8eaa6b 4994 operands[1] = GEN_INT (logval % 8);
29ae8a3c
RK
4995 }
4996 CC_STATUS_INIT;
4997 return "bset %1,%0";
4998 }
4999 return "or%.l %2,%0";
5000}
5001
5505f548 5002const char *
8a4a2253 5003output_xorsi3 (rtx *operands)
29ae8a3c
RK
5004{
5005 register int logval;
5006 if (GET_CODE (operands[2]) == CONST_INT
5007 && INTVAL (operands[2]) >> 16 == 0
5008 && (offsettable_memref_p (operands[0]) || DATA_REG_P (operands[0]))
9425fb04 5009 && !TARGET_COLDFIRE)
29ae8a3c
RK
5010 {
5011 if (! DATA_REG_P (operands[0]))
b72f00af 5012 operands[0] = adjust_address (operands[0], HImode, 2);
29ae8a3c
RK
5013 /* Do not delete a following tstl %0 insn; that would be incorrect. */
5014 CC_STATUS_INIT;
5015 if (INTVAL (operands[2]) == 0xffff)
5016 return "not%.w %0";
5017 return "eor%.w %2,%0";
5018 }
5019 if (GET_CODE (operands[2]) == CONST_INT
c4406f74 5020 && (logval = exact_log2 (INTVAL (operands[2]) & 0xffffffff)) >= 0
29ae8a3c
RK
5021 && (DATA_REG_P (operands[0])
5022 || offsettable_memref_p (operands[0])))
5023 {
5024 if (DATA_REG_P (operands[0]))
b72f00af 5025 operands[1] = GEN_INT (logval);
29ae8a3c
RK
5026 else
5027 {
b72f00af 5028 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
1d8eaa6b 5029 operands[1] = GEN_INT (logval % 8);
29ae8a3c
RK
5030 }
5031 CC_STATUS_INIT;
5032 return "bchg %1,%0";
5033 }
5034 return "eor%.l %2,%0";
5035}
7c262518 5036
29ca003a
RS
5037/* Return the instruction that should be used for a call to address X,
5038 which is known to be in operand 0. */
5039
5040const char *
5041output_call (rtx x)
5042{
5043 if (symbolic_operand (x, VOIDmode))
5044 return m68k_symbolic_call;
5045 else
5046 return "jsr %a0";
5047}
5048
f7e70894
RS
5049/* Likewise sibling calls. */
5050
5051const char *
5052output_sibcall (rtx x)
5053{
5054 if (symbolic_operand (x, VOIDmode))
5055 return m68k_symbolic_jump;
5056 else
5057 return "jmp %a0";
5058}
5059
c590b625 5060static void
8a4a2253 5061m68k_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
4ab870f5 5062 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
8a4a2253 5063 tree function)
483ab821 5064{
e0601576
RH
5065 rtx this_slot, offset, addr, mem, insn, tmp;
5066
5067 /* Avoid clobbering the struct value reg by using the
5068 static chain reg as a temporary. */
5069 tmp = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
4ab870f5
RS
5070
5071 /* Pretend to be a post-reload pass while generating rtl. */
4ab870f5 5072 reload_completed = 1;
4ab870f5
RS
5073
5074 /* The "this" pointer is stored at 4(%sp). */
5075 this_slot = gen_rtx_MEM (Pmode, plus_constant (stack_pointer_rtx, 4));
5076
5077 /* Add DELTA to THIS. */
5078 if (delta != 0)
5050d266 5079 {
4ab870f5
RS
5080 /* Make the offset a legitimate operand for memory addition. */
5081 offset = GEN_INT (delta);
5082 if ((delta < -8 || delta > 8)
5083 && (TARGET_COLDFIRE || USE_MOVQ (delta)))
5084 {
5085 emit_move_insn (gen_rtx_REG (Pmode, D0_REG), offset);
5086 offset = gen_rtx_REG (Pmode, D0_REG);
5087 }
5088 emit_insn (gen_add3_insn (copy_rtx (this_slot),
5089 copy_rtx (this_slot), offset));
5050d266 5090 }
c590b625 5091
4ab870f5
RS
5092 /* If needed, add *(*THIS + VCALL_OFFSET) to THIS. */
5093 if (vcall_offset != 0)
5094 {
5095 /* Set the static chain register to *THIS. */
e0601576
RH
5096 emit_move_insn (tmp, this_slot);
5097 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
4ab870f5
RS
5098
5099 /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET. */
e0601576 5100 addr = plus_constant (tmp, vcall_offset);
4ab870f5
RS
5101 if (!m68k_legitimate_address_p (Pmode, addr, true))
5102 {
e0601576
RH
5103 emit_insn (gen_rtx_SET (VOIDmode, tmp, addr));
5104 addr = tmp;
4ab870f5 5105 }
c590b625 5106
4ab870f5
RS
5107 /* Load the offset into %d0 and add it to THIS. */
5108 emit_move_insn (gen_rtx_REG (Pmode, D0_REG),
5109 gen_rtx_MEM (Pmode, addr));
5110 emit_insn (gen_add3_insn (copy_rtx (this_slot),
5111 copy_rtx (this_slot),
5112 gen_rtx_REG (Pmode, D0_REG)));
5113 }
29ca003a 5114
4ab870f5
RS
5115 /* Jump to the target function. Use a sibcall if direct jumps are
5116 allowed, otherwise load the address into a register first. */
5117 mem = DECL_RTL (function);
5118 if (!sibcall_operand (XEXP (mem, 0), VOIDmode))
5119 {
5120 gcc_assert (flag_pic);
c590b625 5121
4ab870f5
RS
5122 if (!TARGET_SEP_DATA)
5123 {
5124 /* Use the static chain register as a temporary (call-clobbered)
5125 GOT pointer for this function. We can use the static chain
5126 register because it isn't live on entry to the thunk. */
6fb5fa3c 5127 SET_REGNO (pic_offset_table_rtx, STATIC_CHAIN_REGNUM);
4ab870f5
RS
5128 emit_insn (gen_load_got (pic_offset_table_rtx));
5129 }
e0601576
RH
5130 legitimize_pic_address (XEXP (mem, 0), Pmode, tmp);
5131 mem = replace_equiv_address (mem, tmp);
4ab870f5
RS
5132 }
5133 insn = emit_call_insn (gen_sibcall (mem, const0_rtx));
5134 SIBLING_CALL_P (insn) = 1;
5135
5136 /* Run just enough of rest_of_compilation. */
5137 insn = get_insns ();
5138 split_all_insns_noflow ();
5139 final_start_function (insn, file, 1);
5140 final (insn, file, 1);
5141 final_end_function ();
5142
5143 /* Clean up the vars set above. */
5144 reload_completed = 0;
4ab870f5
RS
5145
5146 /* Restore the original PIC register. */
5147 if (flag_pic)
6fb5fa3c 5148 SET_REGNO (pic_offset_table_rtx, PIC_REG);
483ab821 5149}
8636be86
KH
5150
5151/* Worker function for TARGET_STRUCT_VALUE_RTX. */
5152
5153static rtx
5154m68k_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
5155 int incoming ATTRIBUTE_UNUSED)
5156{
5157 return gen_rtx_REG (Pmode, M68K_STRUCT_VALUE_REGNUM);
5158}
cfca21cb
PB
5159
5160/* Return nonzero if register old_reg can be renamed to register new_reg. */
5161int
5162m68k_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5163 unsigned int new_reg)
5164{
5165
5166 /* Interrupt functions can only use registers that have already been
5167 saved by the prologue, even if they would normally be
5168 call-clobbered. */
5169
a4242737
KH
5170 if ((m68k_get_function_kind (current_function_decl)
5171 == m68k_fk_interrupt_handler)
6fb5fa3c 5172 && !df_regs_ever_live_p (new_reg))
cfca21cb
PB
5173 return 0;
5174
5175 return 1;
5176}
70028b61 5177
ffa2596e
RS
5178/* Value is true if hard register REGNO can hold a value of machine-mode
5179 MODE. On the 68000, we let the cpu registers can hold any mode, but
5180 restrict the 68881 registers to floating-point modes. */
5181
70028b61
PB
5182bool
5183m68k_regno_mode_ok (int regno, enum machine_mode mode)
5184{
36e04090 5185 if (DATA_REGNO_P (regno))
70028b61 5186 {
a0a7fbc9
AS
5187 /* Data Registers, can hold aggregate if fits in. */
5188 if (regno + GET_MODE_SIZE (mode) / 4 <= 8)
5189 return true;
70028b61 5190 }
36e04090 5191 else if (ADDRESS_REGNO_P (regno))
70028b61 5192 {
a0a7fbc9
AS
5193 if (regno + GET_MODE_SIZE (mode) / 4 <= 16)
5194 return true;
70028b61 5195 }
36e04090 5196 else if (FP_REGNO_P (regno))
70028b61
PB
5197 {
5198 /* FPU registers, hold float or complex float of long double or
a0a7fbc9
AS
5199 smaller. */
5200 if ((GET_MODE_CLASS (mode) == MODE_FLOAT
5201 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
dcc21c4c 5202 && GET_MODE_UNIT_SIZE (mode) <= TARGET_FP_REG_SIZE)
a0a7fbc9 5203 return true;
70028b61
PB
5204 }
5205 return false;
5206}
dcc21c4c 5207
ffa2596e
RS
5208/* Implement SECONDARY_RELOAD_CLASS. */
5209
5210enum reg_class
5211m68k_secondary_reload_class (enum reg_class rclass,
5212 enum machine_mode mode, rtx x)
5213{
5214 int regno;
5215
5216 regno = true_regnum (x);
5217
5218 /* If one operand of a movqi is an address register, the other
5219 operand must be a general register or constant. Other types
5220 of operand must be reloaded through a data register. */
5221 if (GET_MODE_SIZE (mode) == 1
5222 && reg_classes_intersect_p (rclass, ADDR_REGS)
5223 && !(INT_REGNO_P (regno) || CONSTANT_P (x)))
5224 return DATA_REGS;
5225
5226 /* PC-relative addresses must be loaded into an address register first. */
5227 if (TARGET_PCREL
5228 && !reg_class_subset_p (rclass, ADDR_REGS)
5229 && symbolic_operand (x, VOIDmode))
5230 return ADDR_REGS;
5231
5232 return NO_REGS;
5233}
5234
5235/* Implement PREFERRED_RELOAD_CLASS. */
5236
5237enum reg_class
5238m68k_preferred_reload_class (rtx x, enum reg_class rclass)
5239{
5240 enum reg_class secondary_class;
5241
5242 /* If RCLASS might need a secondary reload, try restricting it to
5243 a class that doesn't. */
5244 secondary_class = m68k_secondary_reload_class (rclass, GET_MODE (x), x);
5245 if (secondary_class != NO_REGS
5246 && reg_class_subset_p (secondary_class, rclass))
5247 return secondary_class;
5248
5249 /* Prefer to use moveq for in-range constants. */
5250 if (GET_CODE (x) == CONST_INT
5251 && reg_class_subset_p (DATA_REGS, rclass)
5252 && IN_RANGE (INTVAL (x), -0x80, 0x7f))
5253 return DATA_REGS;
5254
5255 /* ??? Do we really need this now? */
5256 if (GET_CODE (x) == CONST_DOUBLE
5257 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
5258 {
5259 if (TARGET_HARD_FLOAT && reg_class_subset_p (FP_REGS, rclass))
5260 return FP_REGS;
5261
5262 return NO_REGS;
5263 }
5264
5265 return rclass;
5266}
5267
dcc21c4c
PB
5268/* Return floating point values in a 68881 register. This makes 68881 code
5269 a little bit faster. It also makes -msoft-float code incompatible with
5270 hard-float code, so people have to be careful not to mix the two.
c0220ea4 5271 For ColdFire it was decided the ABI incompatibility is undesirable.
dcc21c4c
PB
5272 If there is need for a hard-float ABI it is probably worth doing it
5273 properly and also passing function arguments in FP registers. */
5274rtx
5275m68k_libcall_value (enum machine_mode mode)
5276{
5277 switch (mode) {
5278 case SFmode:
5279 case DFmode:
5280 case XFmode:
5281 if (TARGET_68881)
8d989403 5282 return gen_rtx_REG (mode, FP0_REG);
dcc21c4c
PB
5283 break;
5284 default:
5285 break;
5286 }
75df395f
MK
5287
5288 return gen_rtx_REG (mode, m68k_libcall_value_in_a0_p ? A0_REG : D0_REG);
dcc21c4c
PB
5289}
5290
db5e2d51
MK
5291/* Location in which function value is returned.
5292 NOTE: Due to differences in ABIs, don't call this function directly,
5293 use FUNCTION_VALUE instead. */
dcc21c4c 5294rtx
586de218 5295m68k_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED)
dcc21c4c
PB
5296{
5297 enum machine_mode mode;
5298
5299 mode = TYPE_MODE (valtype);
5300 switch (mode) {
5301 case SFmode:
5302 case DFmode:
5303 case XFmode:
5304 if (TARGET_68881)
8d989403 5305 return gen_rtx_REG (mode, FP0_REG);
dcc21c4c
PB
5306 break;
5307 default:
5308 break;
5309 }
5310
576c9028
KH
5311 /* If the function returns a pointer, push that into %a0. */
5312 if (func && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (func))))
5313 /* For compatibility with the large body of existing code which
5314 does not always properly declare external functions returning
5315 pointer types, the m68k/SVR4 convention is to copy the value
5316 returned for pointer functions from a0 to d0 in the function
5317 epilogue, so that callers that have neglected to properly
5318 declare the callee can still find the correct return value in
5319 d0. */
5320 return gen_rtx_PARALLEL
5321 (mode,
5322 gen_rtvec (2,
5323 gen_rtx_EXPR_LIST (VOIDmode,
5324 gen_rtx_REG (mode, A0_REG),
5325 const0_rtx),
5326 gen_rtx_EXPR_LIST (VOIDmode,
5327 gen_rtx_REG (mode, D0_REG),
5328 const0_rtx)));
5329 else if (POINTER_TYPE_P (valtype))
5330 return gen_rtx_REG (mode, A0_REG);
dcc21c4c 5331 else
576c9028 5332 return gen_rtx_REG (mode, D0_REG);
dcc21c4c 5333}
1c445f03
NS
5334
5335/* Worker function for TARGET_RETURN_IN_MEMORY. */
5336#if M68K_HONOR_TARGET_STRICT_ALIGNMENT
5337static bool
511e41e5 5338m68k_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1c445f03
NS
5339{
5340 enum machine_mode mode = TYPE_MODE (type);
5341
5342 if (mode == BLKmode)
5343 return true;
5344
5345 /* If TYPE's known alignment is less than the alignment of MODE that
5346 would contain the structure, then return in memory. We need to
5347 do so to maintain the compatibility between code compiled with
5348 -mstrict-align and that compiled with -mno-strict-align. */
5349 if (AGGREGATE_TYPE_P (type)
5350 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (mode))
5351 return true;
5352
5353 return false;
5354}
5355#endif
c47b0cb4
MK
5356
5357/* CPU to schedule the program for. */
5358enum attr_cpu m68k_sched_cpu;
5359
826fadba
MK
5360/* MAC to schedule the program for. */
5361enum attr_mac m68k_sched_mac;
5362
c47b0cb4
MK
5363/* Operand type. */
5364enum attr_op_type
5365 {
5366 /* No operand. */
5367 OP_TYPE_NONE,
5368
96fcacb7
MK
5369 /* Integer register. */
5370 OP_TYPE_RN,
5371
5372 /* FP register. */
5373 OP_TYPE_FPN,
c47b0cb4
MK
5374
5375 /* Implicit mem reference (e.g. stack). */
5376 OP_TYPE_MEM1,
5377
5378 /* Memory without offset or indexing. EA modes 2, 3 and 4. */
5379 OP_TYPE_MEM234,
5380
5381 /* Memory with offset but without indexing. EA mode 5. */
5382 OP_TYPE_MEM5,
5383
5384 /* Memory with indexing. EA mode 6. */
5385 OP_TYPE_MEM6,
5386
5387 /* Memory referenced by absolute address. EA mode 7. */
5388 OP_TYPE_MEM7,
5389
5390 /* Immediate operand that doesn't require extension word. */
5391 OP_TYPE_IMM_Q,
5392
5393 /* Immediate 16 bit operand. */
5394 OP_TYPE_IMM_W,
5395
5396 /* Immediate 32 bit operand. */
5397 OP_TYPE_IMM_L
5398 };
5399
c47b0cb4
MK
5400/* Return type of memory ADDR_RTX refers to. */
5401static enum attr_op_type
5402sched_address_type (enum machine_mode mode, rtx addr_rtx)
5403{
5404 struct m68k_address address;
5405
96fcacb7
MK
5406 if (symbolic_operand (addr_rtx, VOIDmode))
5407 return OP_TYPE_MEM7;
5408
c47b0cb4
MK
5409 if (!m68k_decompose_address (mode, addr_rtx,
5410 reload_completed, &address))
5411 {
96fcacb7 5412 gcc_assert (!reload_completed);
c47b0cb4
MK
5413 /* Reload will likely fix the address to be in the register. */
5414 return OP_TYPE_MEM234;
5415 }
5416
5417 if (address.scale != 0)
5418 return OP_TYPE_MEM6;
5419
5420 if (address.base != NULL_RTX)
5421 {
5422 if (address.offset == NULL_RTX)
5423 return OP_TYPE_MEM234;
5424
5425 return OP_TYPE_MEM5;
5426 }
5427
5428 gcc_assert (address.offset != NULL_RTX);
5429
5430 return OP_TYPE_MEM7;
5431}
5432
96fcacb7
MK
5433/* Return X or Y (depending on OPX_P) operand of INSN. */
5434static rtx
5435sched_get_operand (rtx insn, bool opx_p)
5436{
5437 int i;
5438
5439 if (recog_memoized (insn) < 0)
5440 gcc_unreachable ();
5441
5442 extract_constrain_insn_cached (insn);
5443
5444 if (opx_p)
5445 i = get_attr_opx (insn);
5446 else
5447 i = get_attr_opy (insn);
5448
5449 if (i >= recog_data.n_operands)
5450 return NULL;
5451
5452 return recog_data.operand[i];
5453}
5454
5455/* Return type of INSN's operand X (if OPX_P) or operand Y (if !OPX_P).
5456 If ADDRESS_P is true, return type of memory location operand refers to. */
c47b0cb4 5457static enum attr_op_type
96fcacb7 5458sched_attr_op_type (rtx insn, bool opx_p, bool address_p)
c47b0cb4 5459{
96fcacb7
MK
5460 rtx op;
5461
5462 op = sched_get_operand (insn, opx_p);
5463
5464 if (op == NULL)
5465 {
5466 gcc_assert (!reload_completed);
5467 return OP_TYPE_RN;
5468 }
c47b0cb4
MK
5469
5470 if (address_p)
5471 return sched_address_type (QImode, op);
5472
5473 if (memory_operand (op, VOIDmode))
5474 return sched_address_type (GET_MODE (op), XEXP (op, 0));
5475
5476 if (register_operand (op, VOIDmode))
96fcacb7
MK
5477 {
5478 if ((!reload_completed && FLOAT_MODE_P (GET_MODE (op)))
5479 || (reload_completed && FP_REG_P (op)))
5480 return OP_TYPE_FPN;
5481
5482 return OP_TYPE_RN;
5483 }
c47b0cb4
MK
5484
5485 if (GET_CODE (op) == CONST_INT)
5486 {
96fcacb7
MK
5487 int ival;
5488
5489 ival = INTVAL (op);
5490
5491 /* Check for quick constants. */
5492 switch (get_attr_type (insn))
5493 {
5494 case TYPE_ALUQ_L:
5495 if (IN_RANGE (ival, 1, 8) || IN_RANGE (ival, -8, -1))
5496 return OP_TYPE_IMM_Q;
5497
5498 gcc_assert (!reload_completed);
5499 break;
5500
5501 case TYPE_MOVEQ_L:
5502 if (USE_MOVQ (ival))
5503 return OP_TYPE_IMM_Q;
5504
5505 gcc_assert (!reload_completed);
5506 break;
5507
5508 case TYPE_MOV3Q_L:
5509 if (valid_mov3q_const (ival))
5510 return OP_TYPE_IMM_Q;
5511
5512 gcc_assert (!reload_completed);
5513 break;
5514
5515 default:
5516 break;
5517 }
5518
5519 if (IN_RANGE (ival, -0x8000, 0x7fff))
c47b0cb4
MK
5520 return OP_TYPE_IMM_W;
5521
5522 return OP_TYPE_IMM_L;
5523 }
5524
5525 if (GET_CODE (op) == CONST_DOUBLE)
5526 {
5527 switch (GET_MODE (op))
5528 {
5529 case SFmode:
5530 return OP_TYPE_IMM_W;
5531
5532 case VOIDmode:
5533 case DFmode:
5534 return OP_TYPE_IMM_L;
5535
5536 default:
5537 gcc_unreachable ();
5538 }
5539 }
5540
00b2ef14
MK
5541 if (GET_CODE (op) == CONST
5542 || symbolic_operand (op, VOIDmode)
c47b0cb4
MK
5543 || LABEL_P (op))
5544 {
5545 switch (GET_MODE (op))
5546 {
5547 case QImode:
5548 return OP_TYPE_IMM_Q;
5549
5550 case HImode:
5551 return OP_TYPE_IMM_W;
5552
5553 case SImode:
5554 return OP_TYPE_IMM_L;
5555
5556 default:
75df395f
MK
5557 if (symbolic_operand (m68k_unwrap_symbol (op, false), VOIDmode))
5558 /* Just a guess. */
c47b0cb4
MK
5559 return OP_TYPE_IMM_W;
5560
5561 return OP_TYPE_IMM_L;
5562 }
5563 }
5564
96fcacb7 5565 gcc_assert (!reload_completed);
c47b0cb4 5566
96fcacb7
MK
5567 if (FLOAT_MODE_P (GET_MODE (op)))
5568 return OP_TYPE_FPN;
c47b0cb4 5569
96fcacb7 5570 return OP_TYPE_RN;
c47b0cb4
MK
5571}
5572
5573/* Implement opx_type attribute.
5574 Return type of INSN's operand X.
5575 If ADDRESS_P is true, return type of memory location operand refers to. */
5576enum attr_opx_type
5577m68k_sched_attr_opx_type (rtx insn, int address_p)
5578{
c47b0cb4
MK
5579 switch (sched_attr_op_type (insn, true, address_p != 0))
5580 {
96fcacb7
MK
5581 case OP_TYPE_RN:
5582 return OPX_TYPE_RN;
5583
5584 case OP_TYPE_FPN:
5585 return OPX_TYPE_FPN;
c47b0cb4
MK
5586
5587 case OP_TYPE_MEM1:
5588 return OPX_TYPE_MEM1;
5589
5590 case OP_TYPE_MEM234:
5591 return OPX_TYPE_MEM234;
5592
5593 case OP_TYPE_MEM5:
5594 return OPX_TYPE_MEM5;
5595
5596 case OP_TYPE_MEM6:
5597 return OPX_TYPE_MEM6;
5598
5599 case OP_TYPE_MEM7:
5600 return OPX_TYPE_MEM7;
5601
5602 case OP_TYPE_IMM_Q:
5603 return OPX_TYPE_IMM_Q;
5604
5605 case OP_TYPE_IMM_W:
5606 return OPX_TYPE_IMM_W;
5607
5608 case OP_TYPE_IMM_L:
5609 return OPX_TYPE_IMM_L;
5610
5611 default:
5612 gcc_unreachable ();
c47b0cb4
MK
5613 }
5614}
5615
5616/* Implement opy_type attribute.
5617 Return type of INSN's operand Y.
5618 If ADDRESS_P is true, return type of memory location operand refers to. */
5619enum attr_opy_type
5620m68k_sched_attr_opy_type (rtx insn, int address_p)
5621{
c47b0cb4
MK
5622 switch (sched_attr_op_type (insn, false, address_p != 0))
5623 {
96fcacb7
MK
5624 case OP_TYPE_RN:
5625 return OPY_TYPE_RN;
5626
5627 case OP_TYPE_FPN:
5628 return OPY_TYPE_FPN;
c47b0cb4
MK
5629
5630 case OP_TYPE_MEM1:
5631 return OPY_TYPE_MEM1;
5632
5633 case OP_TYPE_MEM234:
5634 return OPY_TYPE_MEM234;
5635
5636 case OP_TYPE_MEM5:
5637 return OPY_TYPE_MEM5;
5638
5639 case OP_TYPE_MEM6:
5640 return OPY_TYPE_MEM6;
5641
5642 case OP_TYPE_MEM7:
5643 return OPY_TYPE_MEM7;
5644
5645 case OP_TYPE_IMM_Q:
5646 return OPY_TYPE_IMM_Q;
5647
5648 case OP_TYPE_IMM_W:
5649 return OPY_TYPE_IMM_W;
5650
5651 case OP_TYPE_IMM_L:
5652 return OPY_TYPE_IMM_L;
5653
5654 default:
5655 gcc_unreachable ();
c47b0cb4
MK
5656 }
5657}
5658
96fcacb7
MK
5659/* Return size of INSN as int. */
5660static int
5661sched_get_attr_size_int (rtx insn)
c47b0cb4
MK
5662{
5663 int size;
5664
96fcacb7 5665 switch (get_attr_type (insn))
c47b0cb4 5666 {
96fcacb7
MK
5667 case TYPE_IGNORE:
5668 /* There should be no references to m68k_sched_attr_size for 'ignore'
5669 instructions. */
5670 gcc_unreachable ();
5671 return 0;
5672
5673 case TYPE_MUL_L:
c47b0cb4
MK
5674 size = 2;
5675 break;
5676
5677 default:
5678 size = 1;
5679 break;
5680 }
5681
5682 switch (get_attr_opx_type (insn))
5683 {
5684 case OPX_TYPE_NONE:
96fcacb7
MK
5685 case OPX_TYPE_RN:
5686 case OPX_TYPE_FPN:
c47b0cb4
MK
5687 case OPX_TYPE_MEM1:
5688 case OPX_TYPE_MEM234:
5689 case OPY_TYPE_IMM_Q:
5690 break;
5691
5692 case OPX_TYPE_MEM5:
5693 case OPX_TYPE_MEM6:
5694 /* Here we assume that most absolute references are short. */
5695 case OPX_TYPE_MEM7:
5696 case OPY_TYPE_IMM_W:
5697 ++size;
5698 break;
5699
5700 case OPY_TYPE_IMM_L:
5701 size += 2;
5702 break;
5703
5704 default:
5705 gcc_unreachable ();
5706 }
5707
5708 switch (get_attr_opy_type (insn))
5709 {
5710 case OPY_TYPE_NONE:
96fcacb7
MK
5711 case OPY_TYPE_RN:
5712 case OPY_TYPE_FPN:
c47b0cb4
MK
5713 case OPY_TYPE_MEM1:
5714 case OPY_TYPE_MEM234:
5715 case OPY_TYPE_IMM_Q:
5716 break;
5717
5718 case OPY_TYPE_MEM5:
5719 case OPY_TYPE_MEM6:
5720 /* Here we assume that most absolute references are short. */
5721 case OPY_TYPE_MEM7:
5722 case OPY_TYPE_IMM_W:
5723 ++size;
5724 break;
5725
5726 case OPY_TYPE_IMM_L:
5727 size += 2;
5728 break;
5729
5730 default:
5731 gcc_unreachable ();
5732 }
5733
5734 if (size > 3)
5735 {
96fcacb7 5736 gcc_assert (!reload_completed);
c47b0cb4
MK
5737
5738 size = 3;
5739 }
5740
5741 return size;
5742}
5743
96fcacb7
MK
5744/* Return size of INSN as attribute enum value. */
5745enum attr_size
5746m68k_sched_attr_size (rtx insn)
5747{
5748 switch (sched_get_attr_size_int (insn))
5749 {
5750 case 1:
5751 return SIZE_1;
5752
5753 case 2:
5754 return SIZE_2;
5755
5756 case 3:
5757 return SIZE_3;
5758
5759 default:
5760 gcc_unreachable ();
96fcacb7
MK
5761 }
5762}
5763
5764/* Return operand X or Y (depending on OPX_P) of INSN,
5765 if it is a MEM, or NULL overwise. */
5766static enum attr_op_type
5767sched_get_opxy_mem_type (rtx insn, bool opx_p)
5768{
5769 if (opx_p)
5770 {
5771 switch (get_attr_opx_type (insn))
5772 {
5773 case OPX_TYPE_NONE:
5774 case OPX_TYPE_RN:
5775 case OPX_TYPE_FPN:
5776 case OPX_TYPE_IMM_Q:
5777 case OPX_TYPE_IMM_W:
5778 case OPX_TYPE_IMM_L:
5779 return OP_TYPE_RN;
5780
5781 case OPX_TYPE_MEM1:
5782 case OPX_TYPE_MEM234:
5783 case OPX_TYPE_MEM5:
5784 case OPX_TYPE_MEM7:
5785 return OP_TYPE_MEM1;
5786
5787 case OPX_TYPE_MEM6:
5788 return OP_TYPE_MEM6;
5789
5790 default:
5791 gcc_unreachable ();
96fcacb7
MK
5792 }
5793 }
5794 else
5795 {
5796 switch (get_attr_opy_type (insn))
5797 {
5798 case OPY_TYPE_NONE:
5799 case OPY_TYPE_RN:
5800 case OPY_TYPE_FPN:
5801 case OPY_TYPE_IMM_Q:
5802 case OPY_TYPE_IMM_W:
5803 case OPY_TYPE_IMM_L:
5804 return OP_TYPE_RN;
5805
5806 case OPY_TYPE_MEM1:
5807 case OPY_TYPE_MEM234:
5808 case OPY_TYPE_MEM5:
5809 case OPY_TYPE_MEM7:
5810 return OP_TYPE_MEM1;
5811
5812 case OPY_TYPE_MEM6:
5813 return OP_TYPE_MEM6;
5814
5815 default:
5816 gcc_unreachable ();
96fcacb7
MK
5817 }
5818 }
5819}
5820
c47b0cb4
MK
5821/* Implement op_mem attribute. */
5822enum attr_op_mem
5823m68k_sched_attr_op_mem (rtx insn)
5824{
96fcacb7
MK
5825 enum attr_op_type opx;
5826 enum attr_op_type opy;
c47b0cb4 5827
96fcacb7
MK
5828 opx = sched_get_opxy_mem_type (insn, true);
5829 opy = sched_get_opxy_mem_type (insn, false);
c47b0cb4 5830
96fcacb7 5831 if (opy == OP_TYPE_RN && opx == OP_TYPE_RN)
c47b0cb4
MK
5832 return OP_MEM_00;
5833
96fcacb7 5834 if (opy == OP_TYPE_RN && opx == OP_TYPE_MEM1)
c47b0cb4
MK
5835 {
5836 switch (get_attr_opx_access (insn))
5837 {
5838 case OPX_ACCESS_R:
5839 return OP_MEM_10;
5840
5841 case OPX_ACCESS_W:
5842 return OP_MEM_01;
5843
5844 case OPX_ACCESS_RW:
5845 return OP_MEM_11;
5846
5847 default:
96fcacb7 5848 gcc_unreachable ();
c47b0cb4
MK
5849 }
5850 }
5851
96fcacb7 5852 if (opy == OP_TYPE_RN && opx == OP_TYPE_MEM6)
c47b0cb4
MK
5853 {
5854 switch (get_attr_opx_access (insn))
5855 {
5856 case OPX_ACCESS_R:
5857 return OP_MEM_I0;
5858
5859 case OPX_ACCESS_W:
5860 return OP_MEM_0I;
5861
5862 case OPX_ACCESS_RW:
5863 return OP_MEM_I1;
5864
5865 default:
96fcacb7 5866 gcc_unreachable ();
c47b0cb4
MK
5867 }
5868 }
5869
96fcacb7 5870 if (opy == OP_TYPE_MEM1 && opx == OP_TYPE_RN)
c47b0cb4
MK
5871 return OP_MEM_10;
5872
96fcacb7 5873 if (opy == OP_TYPE_MEM1 && opx == OP_TYPE_MEM1)
c47b0cb4
MK
5874 {
5875 switch (get_attr_opx_access (insn))
5876 {
5877 case OPX_ACCESS_W:
5878 return OP_MEM_11;
5879
5880 default:
96fcacb7
MK
5881 gcc_assert (!reload_completed);
5882 return OP_MEM_11;
c47b0cb4
MK
5883 }
5884 }
5885
96fcacb7 5886 if (opy == OP_TYPE_MEM1 && opx == OP_TYPE_MEM6)
c47b0cb4
MK
5887 {
5888 switch (get_attr_opx_access (insn))
5889 {
5890 case OPX_ACCESS_W:
5891 return OP_MEM_1I;
5892
5893 default:
96fcacb7
MK
5894 gcc_assert (!reload_completed);
5895 return OP_MEM_1I;
c47b0cb4
MK
5896 }
5897 }
5898
96fcacb7 5899 if (opy == OP_TYPE_MEM6 && opx == OP_TYPE_RN)
c47b0cb4
MK
5900 return OP_MEM_I0;
5901
96fcacb7 5902 if (opy == OP_TYPE_MEM6 && opx == OP_TYPE_MEM1)
c47b0cb4
MK
5903 {
5904 switch (get_attr_opx_access (insn))
5905 {
5906 case OPX_ACCESS_W:
5907 return OP_MEM_I1;
5908
5909 default:
96fcacb7
MK
5910 gcc_assert (!reload_completed);
5911 return OP_MEM_I1;
c47b0cb4
MK
5912 }
5913 }
5914
96fcacb7
MK
5915 gcc_assert (opy == OP_TYPE_MEM6 && opx == OP_TYPE_MEM6);
5916 gcc_assert (!reload_completed);
5917 return OP_MEM_I1;
c47b0cb4
MK
5918}
5919
5920/* Jump instructions types. Indexed by INSN_UID.
5921 The same rtl insn can be expanded into different asm instructions
5922 depending on the cc0_status. To properly determine type of jump
5923 instructions we scan instruction stream and map jumps types to this
5924 array. */
5925static enum attr_type *sched_branch_type;
5926
5927/* Return the type of the jump insn. */
5928enum attr_type
5929m68k_sched_branch_type (rtx insn)
5930{
5931 enum attr_type type;
5932
5933 type = sched_branch_type[INSN_UID (insn)];
5934
5935 gcc_assert (type != 0);
5936
5937 return type;
5938}
b8c96320 5939
96fcacb7
MK
5940/* Data for ColdFire V4 index bypass.
5941 Producer modifies register that is used as index in consumer with
5942 specified scale. */
5943static struct
b8c96320 5944{
96fcacb7
MK
5945 /* Producer instruction. */
5946 rtx pro;
826fadba 5947
96fcacb7
MK
5948 /* Consumer instruction. */
5949 rtx con;
b8c96320 5950
96fcacb7
MK
5951 /* Scale of indexed memory access within consumer.
5952 Or zero if bypass should not be effective at the moment. */
5953 int scale;
5954} sched_cfv4_bypass_data;
b8c96320
MK
5955
5956/* An empty state that is used in m68k_sched_adjust_cost. */
5957static state_t sched_adjust_cost_state;
5958
5959/* Implement adjust_cost scheduler hook.
5960 Return adjusted COST of dependency LINK between DEF_INSN and INSN. */
5961static int
5962m68k_sched_adjust_cost (rtx insn, rtx link ATTRIBUTE_UNUSED, rtx def_insn,
5963 int cost)
5964{
5965 int delay;
5966
5967 if (recog_memoized (def_insn) < 0
5968 || recog_memoized (insn) < 0)
5969 return cost;
5970
96fcacb7
MK
5971 if (sched_cfv4_bypass_data.scale == 1)
5972 /* Handle ColdFire V4 bypass for indexed address with 1x scale. */
5973 {
5974 /* haifa-sched.c: insn_cost () calls bypass_p () just before
5975 targetm.sched.adjust_cost (). Hence, we can be relatively sure
5976 that the data in sched_cfv4_bypass_data is up to date. */
5977 gcc_assert (sched_cfv4_bypass_data.pro == def_insn
5978 && sched_cfv4_bypass_data.con == insn);
5979
5980 if (cost < 3)
5981 cost = 3;
5982
5983 sched_cfv4_bypass_data.pro = NULL;
5984 sched_cfv4_bypass_data.con = NULL;
5985 sched_cfv4_bypass_data.scale = 0;
5986 }
5987 else
5988 gcc_assert (sched_cfv4_bypass_data.pro == NULL
5989 && sched_cfv4_bypass_data.con == NULL
5990 && sched_cfv4_bypass_data.scale == 0);
5991
b8c96320
MK
5992 /* Don't try to issue INSN earlier than DFA permits.
5993 This is especially useful for instructions that write to memory,
5994 as their true dependence (default) latency is better to be set to 0
5995 to workaround alias analysis limitations.
5996 This is, in fact, a machine independent tweak, so, probably,
5997 it should be moved to haifa-sched.c: insn_cost (). */
b8c96320
MK
5998 delay = min_insn_conflict_delay (sched_adjust_cost_state, def_insn, insn);
5999 if (delay > cost)
6000 cost = delay;
6001
6002 return cost;
6003}
6004
96fcacb7
MK
6005/* Return maximal number of insns that can be scheduled on a single cycle. */
6006static int
6007m68k_sched_issue_rate (void)
6008{
6009 switch (m68k_sched_cpu)
6010 {
6011 case CPU_CFV1:
6012 case CPU_CFV2:
6013 case CPU_CFV3:
6014 return 1;
6015
6016 case CPU_CFV4:
6017 return 2;
6018
6019 default:
6020 gcc_unreachable ();
6021 return 0;
6022 }
6023}
6024
826fadba
MK
6025/* Maximal length of instruction for current CPU.
6026 E.g. it is 3 for any ColdFire core. */
6027static int max_insn_size;
6028
6029/* Data to model instruction buffer of CPU. */
6030struct _sched_ib
6031{
96fcacb7
MK
6032 /* True if instruction buffer model is modeled for current CPU. */
6033 bool enabled_p;
6034
826fadba
MK
6035 /* Size of the instruction buffer in words. */
6036 int size;
6037
6038 /* Number of filled words in the instruction buffer. */
6039 int filled;
6040
6041 /* Additional information about instruction buffer for CPUs that have
6042 a buffer of instruction records, rather then a plain buffer
6043 of instruction words. */
6044 struct _sched_ib_records
6045 {
6046 /* Size of buffer in records. */
6047 int n_insns;
b8c96320 6048
826fadba
MK
6049 /* Array to hold data on adjustements made to the size of the buffer. */
6050 int *adjust;
b8c96320 6051
826fadba
MK
6052 /* Index of the above array. */
6053 int adjust_index;
6054 } records;
6055
6056 /* An insn that reserves (marks empty) one word in the instruction buffer. */
6057 rtx insn;
6058};
6059
6060static struct _sched_ib sched_ib;
b8c96320
MK
6061
6062/* ID of memory unit. */
6063static int sched_mem_unit_code;
6064
6065/* Implementation of the targetm.sched.variable_issue () hook.
6066 It is called after INSN was issued. It returns the number of insns
6067 that can possibly get scheduled on the current cycle.
6068 It is used here to determine the effect of INSN on the instruction
6069 buffer. */
6070static int
6071m68k_sched_variable_issue (FILE *sched_dump ATTRIBUTE_UNUSED,
6072 int sched_verbose ATTRIBUTE_UNUSED,
6073 rtx insn, int can_issue_more)
6074{
6075 int insn_size;
6076
96fcacb7 6077 if (recog_memoized (insn) >= 0 && get_attr_type (insn) != TYPE_IGNORE)
b8c96320 6078 {
826fadba
MK
6079 switch (m68k_sched_cpu)
6080 {
6081 case CPU_CFV1:
6082 case CPU_CFV2:
96fcacb7 6083 insn_size = sched_get_attr_size_int (insn);
826fadba
MK
6084 break;
6085
6086 case CPU_CFV3:
96fcacb7 6087 insn_size = sched_get_attr_size_int (insn);
826fadba
MK
6088
6089 /* ColdFire V3 and V4 cores have instruction buffers that can
6090 accumulate up to 8 instructions regardless of instructions'
6091 sizes. So we should take care not to "prefetch" 24 one-word
6092 or 12 two-words instructions.
6093 To model this behavior we temporarily decrease size of the
6094 buffer by (max_insn_size - insn_size) for next 7 instructions. */
6095 {
6096 int adjust;
6097
6098 adjust = max_insn_size - insn_size;
6099 sched_ib.size -= adjust;
6100
6101 if (sched_ib.filled > sched_ib.size)
6102 sched_ib.filled = sched_ib.size;
6103
6104 sched_ib.records.adjust[sched_ib.records.adjust_index] = adjust;
6105 }
6106
6107 ++sched_ib.records.adjust_index;
6108 if (sched_ib.records.adjust_index == sched_ib.records.n_insns)
6109 sched_ib.records.adjust_index = 0;
6110
6111 /* Undo adjustement we did 7 instructions ago. */
6112 sched_ib.size
6113 += sched_ib.records.adjust[sched_ib.records.adjust_index];
6114
6115 break;
b8c96320 6116
96fcacb7
MK
6117 case CPU_CFV4:
6118 gcc_assert (!sched_ib.enabled_p);
6119 insn_size = 0;
6120 break;
6121
826fadba
MK
6122 default:
6123 gcc_unreachable ();
6124 }
b8c96320 6125
826fadba 6126 gcc_assert (insn_size <= sched_ib.filled);
b8c96320
MK
6127 --can_issue_more;
6128 }
6129 else if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6130 || asm_noperands (PATTERN (insn)) >= 0)
826fadba 6131 insn_size = sched_ib.filled;
b8c96320
MK
6132 else
6133 insn_size = 0;
6134
826fadba 6135 sched_ib.filled -= insn_size;
b8c96320
MK
6136
6137 return can_issue_more;
6138}
6139
96fcacb7
MK
6140/* Return how many instructions should scheduler lookahead to choose the
6141 best one. */
6142static int
6143m68k_sched_first_cycle_multipass_dfa_lookahead (void)
b8c96320 6144{
96fcacb7 6145 return m68k_sched_issue_rate () - 1;
b8c96320
MK
6146}
6147
6148/* Implementation of targetm.sched.md_init_global () hook.
6149 It is invoked once per scheduling pass and is used here
6150 to initialize scheduler constants. */
6151static void
6152m68k_sched_md_init_global (FILE *sched_dump ATTRIBUTE_UNUSED,
6153 int sched_verbose ATTRIBUTE_UNUSED,
6154 int n_insns ATTRIBUTE_UNUSED)
6155{
6156 /* Init branch types. */
6157 {
6158 rtx insn;
6159
5ead67f6 6160 sched_branch_type = XCNEWVEC (enum attr_type, get_max_uid () + 1);
b8c96320
MK
6161
6162 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
6163 {
6164 if (JUMP_P (insn))
6165 /* !!! FIXME: Implement real scan here. */
6166 sched_branch_type[INSN_UID (insn)] = TYPE_BCC;
6167 }
6168 }
6169
96fcacb7
MK
6170#ifdef ENABLE_CHECKING
6171 /* Check that all instructions have DFA reservations and
6172 that all instructions can be issued from a clean state. */
6173 {
6174 rtx insn;
6175 state_t state;
b8c96320 6176
96fcacb7 6177 state = alloca (state_size ());
b8c96320 6178
96fcacb7
MK
6179 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
6180 {
6181 if (INSN_P (insn) && recog_memoized (insn) >= 0)
6182 {
6183 gcc_assert (insn_has_dfa_reservation_p (insn));
b8c96320 6184
96fcacb7
MK
6185 state_reset (state);
6186 if (state_transition (state, insn) >= 0)
6187 gcc_unreachable ();
6188 }
6189 }
6190 }
6191#endif
b8c96320
MK
6192
6193 /* Setup target cpu. */
96fcacb7
MK
6194
6195 /* ColdFire V4 has a set of features to keep its instruction buffer full
6196 (e.g., a separate memory bus for instructions) and, hence, we do not model
6197 buffer for this CPU. */
6198 sched_ib.enabled_p = (m68k_sched_cpu != CPU_CFV4);
6199
b8c96320
MK
6200 switch (m68k_sched_cpu)
6201 {
96fcacb7
MK
6202 case CPU_CFV4:
6203 sched_ib.filled = 0;
6204
6205 /* FALLTHRU */
6206
826fadba
MK
6207 case CPU_CFV1:
6208 case CPU_CFV2:
6209 max_insn_size = 3;
6210 sched_ib.records.n_insns = 0;
6211 sched_ib.records.adjust = NULL;
6212 break;
6213
6214 case CPU_CFV3:
6215 max_insn_size = 3;
6216 sched_ib.records.n_insns = 8;
5ead67f6 6217 sched_ib.records.adjust = XNEWVEC (int, sched_ib.records.n_insns);
b8c96320
MK
6218 break;
6219
6220 default:
6221 gcc_unreachable ();
6222 }
6223
826fadba
MK
6224 sched_mem_unit_code = get_cpu_unit_code ("cf_mem1");
6225
b8c96320
MK
6226 sched_adjust_cost_state = xmalloc (state_size ());
6227 state_reset (sched_adjust_cost_state);
6228
6229 start_sequence ();
6230 emit_insn (gen_ib ());
826fadba 6231 sched_ib.insn = get_insns ();
b8c96320
MK
6232 end_sequence ();
6233}
6234
6235/* Scheduling pass is now finished. Free/reset static variables. */
6236static void
6237m68k_sched_md_finish_global (FILE *dump ATTRIBUTE_UNUSED,
6238 int verbose ATTRIBUTE_UNUSED)
6239{
826fadba 6240 sched_ib.insn = NULL;
b8c96320
MK
6241
6242 free (sched_adjust_cost_state);
6243 sched_adjust_cost_state = NULL;
6244
6245 sched_mem_unit_code = 0;
826fadba
MK
6246
6247 free (sched_ib.records.adjust);
6248 sched_ib.records.adjust = NULL;
6249 sched_ib.records.n_insns = 0;
6250 max_insn_size = 0;
b8c96320
MK
6251
6252 free (sched_branch_type);
6253 sched_branch_type = NULL;
6254}
6255
6256/* Implementation of targetm.sched.md_init () hook.
6257 It is invoked each time scheduler starts on the new block (basic block or
6258 extended basic block). */
6259static void
6260m68k_sched_md_init (FILE *sched_dump ATTRIBUTE_UNUSED,
6261 int sched_verbose ATTRIBUTE_UNUSED,
6262 int n_insns ATTRIBUTE_UNUSED)
6263{
826fadba
MK
6264 switch (m68k_sched_cpu)
6265 {
6266 case CPU_CFV1:
6267 case CPU_CFV2:
6268 sched_ib.size = 6;
6269 break;
6270
6271 case CPU_CFV3:
6272 sched_ib.size = sched_ib.records.n_insns * max_insn_size;
6273
6274 memset (sched_ib.records.adjust, 0,
6275 sched_ib.records.n_insns * sizeof (*sched_ib.records.adjust));
6276 sched_ib.records.adjust_index = 0;
6277 break;
6278
96fcacb7
MK
6279 case CPU_CFV4:
6280 gcc_assert (!sched_ib.enabled_p);
6281 sched_ib.size = 0;
6282 break;
6283
826fadba
MK
6284 default:
6285 gcc_unreachable ();
6286 }
6287
96fcacb7
MK
6288 if (sched_ib.enabled_p)
6289 /* haifa-sched.c: schedule_block () calls advance_cycle () just before
6290 the first cycle. Workaround that. */
6291 sched_ib.filled = -2;
b8c96320
MK
6292}
6293
6294/* Implementation of targetm.sched.dfa_pre_advance_cycle () hook.
6295 It is invoked just before current cycle finishes and is used here
6296 to track if instruction buffer got its two words this cycle. */
6297static void
6298m68k_sched_dfa_pre_advance_cycle (void)
6299{
96fcacb7
MK
6300 if (!sched_ib.enabled_p)
6301 return;
6302
b8c96320
MK
6303 if (!cpu_unit_reservation_p (curr_state, sched_mem_unit_code))
6304 {
826fadba 6305 sched_ib.filled += 2;
b8c96320 6306
826fadba
MK
6307 if (sched_ib.filled > sched_ib.size)
6308 sched_ib.filled = sched_ib.size;
b8c96320
MK
6309 }
6310}
6311
6312/* Implementation of targetm.sched.dfa_post_advance_cycle () hook.
6313 It is invoked just after new cycle begins and is used here
6314 to setup number of filled words in the instruction buffer so that
6315 instructions which won't have all their words prefetched would be
6316 stalled for a cycle. */
6317static void
6318m68k_sched_dfa_post_advance_cycle (void)
6319{
6320 int i;
b8c96320 6321
96fcacb7
MK
6322 if (!sched_ib.enabled_p)
6323 return;
6324
b8c96320
MK
6325 /* Setup number of prefetched instruction words in the instruction
6326 buffer. */
826fadba
MK
6327 i = max_insn_size - sched_ib.filled;
6328
6329 while (--i >= 0)
b8c96320 6330 {
826fadba 6331 if (state_transition (curr_state, sched_ib.insn) >= 0)
b8c96320
MK
6332 gcc_unreachable ();
6333 }
6334}
96fcacb7
MK
6335
6336/* Return X or Y (depending on OPX_P) operand of INSN,
6337 if it is an integer register, or NULL overwise. */
6338static rtx
6339sched_get_reg_operand (rtx insn, bool opx_p)
6340{
6341 rtx op = NULL;
6342
6343 if (opx_p)
6344 {
6345 if (get_attr_opx_type (insn) == OPX_TYPE_RN)
6346 {
6347 op = sched_get_operand (insn, true);
6348 gcc_assert (op != NULL);
6349
6350 if (!reload_completed && !REG_P (op))
6351 return NULL;
6352 }
6353 }
6354 else
6355 {
6356 if (get_attr_opy_type (insn) == OPY_TYPE_RN)
6357 {
6358 op = sched_get_operand (insn, false);
6359 gcc_assert (op != NULL);
6360
6361 if (!reload_completed && !REG_P (op))
6362 return NULL;
6363 }
6364 }
6365
6366 return op;
6367}
6368
6369/* Return true, if X or Y (depending on OPX_P) operand of INSN
6370 is a MEM. */
6371static bool
6372sched_mem_operand_p (rtx insn, bool opx_p)
6373{
6374 switch (sched_get_opxy_mem_type (insn, opx_p))
6375 {
6376 case OP_TYPE_MEM1:
6377 case OP_TYPE_MEM6:
6378 return true;
6379
6380 default:
6381 return false;
6382 }
6383}
6384
6385/* Return X or Y (depending on OPX_P) operand of INSN,
6386 if it is a MEM, or NULL overwise. */
6387static rtx
6388sched_get_mem_operand (rtx insn, bool must_read_p, bool must_write_p)
6389{
6390 bool opx_p;
6391 bool opy_p;
6392
6393 opx_p = false;
6394 opy_p = false;
6395
6396 if (must_read_p)
6397 {
6398 opx_p = true;
6399 opy_p = true;
6400 }
6401
6402 if (must_write_p)
6403 {
6404 opx_p = true;
6405 opy_p = false;
6406 }
6407
6408 if (opy_p && sched_mem_operand_p (insn, false))
6409 return sched_get_operand (insn, false);
6410
6411 if (opx_p && sched_mem_operand_p (insn, true))
6412 return sched_get_operand (insn, true);
6413
6414 gcc_unreachable ();
6415 return NULL;
6416}
6417
6418/* Return non-zero if PRO modifies register used as part of
6419 address in CON. */
6420int
6421m68k_sched_address_bypass_p (rtx pro, rtx con)
6422{
6423 rtx pro_x;
6424 rtx con_mem_read;
6425
6426 pro_x = sched_get_reg_operand (pro, true);
6427 if (pro_x == NULL)
6428 return 0;
6429
6430 con_mem_read = sched_get_mem_operand (con, true, false);
6431 gcc_assert (con_mem_read != NULL);
6432
6433 if (reg_mentioned_p (pro_x, con_mem_read))
6434 return 1;
6435
6436 return 0;
6437}
6438
6439/* Helper function for m68k_sched_indexed_address_bypass_p.
6440 if PRO modifies register used as index in CON,
6441 return scale of indexed memory access in CON. Return zero overwise. */
6442static int
6443sched_get_indexed_address_scale (rtx pro, rtx con)
6444{
6445 rtx reg;
6446 rtx mem;
6447 struct m68k_address address;
6448
6449 reg = sched_get_reg_operand (pro, true);
6450 if (reg == NULL)
6451 return 0;
6452
6453 mem = sched_get_mem_operand (con, true, false);
6454 gcc_assert (mem != NULL && MEM_P (mem));
6455
6456 if (!m68k_decompose_address (GET_MODE (mem), XEXP (mem, 0), reload_completed,
6457 &address))
6458 gcc_unreachable ();
6459
6460 if (REGNO (reg) == REGNO (address.index))
6461 {
6462 gcc_assert (address.scale != 0);
6463 return address.scale;
6464 }
6465
6466 return 0;
6467}
6468
6469/* Return non-zero if PRO modifies register used
6470 as index with scale 2 or 4 in CON. */
6471int
6472m68k_sched_indexed_address_bypass_p (rtx pro, rtx con)
6473{
6474 gcc_assert (sched_cfv4_bypass_data.pro == NULL
6475 && sched_cfv4_bypass_data.con == NULL
6476 && sched_cfv4_bypass_data.scale == 0);
6477
6478 switch (sched_get_indexed_address_scale (pro, con))
6479 {
6480 case 1:
6481 /* We can't have a variable latency bypass, so
6482 remember to adjust the insn cost in adjust_cost hook. */
6483 sched_cfv4_bypass_data.pro = pro;
6484 sched_cfv4_bypass_data.con = con;
6485 sched_cfv4_bypass_data.scale = 1;
6486 return 0;
6487
6488 case 2:
6489 case 4:
6490 return 1;
6491
6492 default:
6493 return 0;
6494 }
6495}
75df395f 6496
e0601576
RH
6497/* We generate a two-instructions program at M_TRAMP :
6498 movea.l &CHAIN_VALUE,%a0
6499 jmp FNADDR
6500 where %a0 can be modified by changing STATIC_CHAIN_REGNUM. */
6501
6502static void
6503m68k_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
6504{
6505 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
6506 rtx mem;
6507
6508 gcc_assert (ADDRESS_REGNO_P (STATIC_CHAIN_REGNUM));
6509
6510 mem = adjust_address (m_tramp, HImode, 0);
6511 emit_move_insn (mem, GEN_INT(0x207C + ((STATIC_CHAIN_REGNUM-8) << 9)));
6512 mem = adjust_address (m_tramp, SImode, 2);
6513 emit_move_insn (mem, chain_value);
6514
6515 mem = adjust_address (m_tramp, HImode, 6);
6516 emit_move_insn (mem, GEN_INT(0x4EF9));
6517 mem = adjust_address (m_tramp, SImode, 8);
6518 emit_move_insn (mem, fnaddr);
6519
6520 FINALIZE_TRAMPOLINE (XEXP (m_tramp, 0));
6521}
6522
75df395f 6523#include "gt-m68k.h"