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