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