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