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