Merge with trank @ 137446
[official-gcc.git] / gcc / config / m68k / m68k.c
blob046132088bc7d1038e4219ca1d7b1348f4cbf717
1 /* Subroutines for insn-output.c for Motorola 68000 family.
2 Copyright (C) 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "function.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "recog.h"
37 #include "toplev.h"
38 #include "expr.h"
39 #include "reload.h"
40 #include "tm_p.h"
41 #include "target.h"
42 #include "target-def.h"
43 #include "debug.h"
44 #include "flags.h"
45 #include "df.h"
46 /* ??? Need to add a dependency between m68k.o and sched-int.h. */
47 #include "sched-int.h"
48 #include "insn-codes.h"
50 enum reg_class regno_reg_class[] =
52 DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
53 DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
54 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
55 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
56 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
57 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
58 ADDR_REGS
62 /* The minimum number of integer registers that we want to save with the
63 movem instruction. Using two movel instructions instead of a single
64 moveml is about 15% faster for the 68020 and 68030 at no expense in
65 code size. */
66 #define MIN_MOVEM_REGS 3
68 /* The minimum number of floating point registers that we want to save
69 with the fmovem instruction. */
70 #define MIN_FMOVEM_REGS 1
72 /* Structure describing stack frame layout. */
73 struct m68k_frame
75 /* Stack pointer to frame pointer offset. */
76 HOST_WIDE_INT offset;
78 /* Offset of FPU registers. */
79 HOST_WIDE_INT foffset;
81 /* Frame size in bytes (rounded up). */
82 HOST_WIDE_INT size;
84 /* Data and address register. */
85 int reg_no;
86 unsigned int reg_mask;
88 /* FPU registers. */
89 int fpu_no;
90 unsigned int fpu_mask;
92 /* Offsets relative to ARG_POINTER. */
93 HOST_WIDE_INT frame_pointer_offset;
94 HOST_WIDE_INT stack_pointer_offset;
96 /* Function which the above information refers to. */
97 int funcdef_no;
100 /* Current frame information calculated by m68k_compute_frame_layout(). */
101 static struct m68k_frame current_frame;
103 /* Structure describing an m68k address.
105 If CODE is UNKNOWN, the address is BASE + INDEX * SCALE + OFFSET,
106 with null fields evaluating to 0. Here:
108 - BASE satisfies m68k_legitimate_base_reg_p
109 - INDEX satisfies m68k_legitimate_index_reg_p
110 - OFFSET satisfies m68k_legitimate_constant_address_p
112 INDEX is either HImode or SImode. The other fields are SImode.
114 If CODE is PRE_DEC, the address is -(BASE). If CODE is POST_INC,
115 the address is (BASE)+. */
116 struct m68k_address {
117 enum rtx_code code;
118 rtx base;
119 rtx index;
120 rtx offset;
121 int scale;
124 static int m68k_sched_adjust_cost (rtx, rtx, rtx, int);
125 static int m68k_sched_issue_rate (void);
126 static int m68k_sched_variable_issue (FILE *, int, rtx, int);
127 static void m68k_sched_md_init_global (FILE *, int, int);
128 static void m68k_sched_md_finish_global (FILE *, int);
129 static void m68k_sched_md_init (FILE *, int, int);
130 static void m68k_sched_dfa_pre_advance_cycle (void);
131 static void m68k_sched_dfa_post_advance_cycle (void);
132 static int m68k_sched_first_cycle_multipass_dfa_lookahead (void);
134 static bool m68k_handle_option (size_t, const char *, int);
135 static rtx find_addr_reg (rtx);
136 static const char *singlemove_string (rtx *);
137 #ifdef M68K_TARGET_COFF
138 static void m68k_coff_asm_named_section (const char *, unsigned int, tree);
139 #endif /* M68K_TARGET_COFF */
140 static void m68k_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
141 HOST_WIDE_INT, tree);
142 static rtx m68k_struct_value_rtx (tree, int);
143 static tree m68k_handle_fndecl_attribute (tree *node, tree name,
144 tree args, int flags,
145 bool *no_add_attrs);
146 static void m68k_compute_frame_layout (void);
147 static bool m68k_save_reg (unsigned int regno, bool interrupt_handler);
148 static bool m68k_ok_for_sibcall_p (tree, tree);
149 static bool m68k_rtx_costs (rtx, int, int, int *);
150 #if M68K_HONOR_TARGET_STRICT_ALIGNMENT
151 static bool m68k_return_in_memory (tree, tree);
152 #endif
155 /* Specify the identification number of the library being built */
156 const char *m68k_library_id_string = "_current_shared_library_a5_offset_";
158 /* Nonzero if the last compare/test insn had FP operands. The
159 sCC expanders peek at this to determine what to do for the
160 68060, which has no fsCC instructions. */
161 int m68k_last_compare_had_fp_operands;
163 /* Initialize the GCC target structure. */
165 #if INT_OP_GROUP == INT_OP_DOT_WORD
166 #undef TARGET_ASM_ALIGNED_HI_OP
167 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
168 #endif
170 #if INT_OP_GROUP == INT_OP_NO_DOT
171 #undef TARGET_ASM_BYTE_OP
172 #define TARGET_ASM_BYTE_OP "\tbyte\t"
173 #undef TARGET_ASM_ALIGNED_HI_OP
174 #define TARGET_ASM_ALIGNED_HI_OP "\tshort\t"
175 #undef TARGET_ASM_ALIGNED_SI_OP
176 #define TARGET_ASM_ALIGNED_SI_OP "\tlong\t"
177 #endif
179 #if INT_OP_GROUP == INT_OP_DC
180 #undef TARGET_ASM_BYTE_OP
181 #define TARGET_ASM_BYTE_OP "\tdc.b\t"
182 #undef TARGET_ASM_ALIGNED_HI_OP
183 #define TARGET_ASM_ALIGNED_HI_OP "\tdc.w\t"
184 #undef TARGET_ASM_ALIGNED_SI_OP
185 #define TARGET_ASM_ALIGNED_SI_OP "\tdc.l\t"
186 #endif
188 #undef TARGET_ASM_UNALIGNED_HI_OP
189 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
190 #undef TARGET_ASM_UNALIGNED_SI_OP
191 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
193 #undef TARGET_ASM_OUTPUT_MI_THUNK
194 #define TARGET_ASM_OUTPUT_MI_THUNK m68k_output_mi_thunk
195 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
196 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
198 #undef TARGET_ASM_FILE_START_APP_OFF
199 #define TARGET_ASM_FILE_START_APP_OFF true
201 #undef TARGET_SCHED_ADJUST_COST
202 #define TARGET_SCHED_ADJUST_COST m68k_sched_adjust_cost
204 #undef TARGET_SCHED_ISSUE_RATE
205 #define TARGET_SCHED_ISSUE_RATE m68k_sched_issue_rate
207 #undef TARGET_SCHED_VARIABLE_ISSUE
208 #define TARGET_SCHED_VARIABLE_ISSUE m68k_sched_variable_issue
210 #undef TARGET_SCHED_INIT_GLOBAL
211 #define TARGET_SCHED_INIT_GLOBAL m68k_sched_md_init_global
213 #undef TARGET_SCHED_FINISH_GLOBAL
214 #define TARGET_SCHED_FINISH_GLOBAL m68k_sched_md_finish_global
216 #undef TARGET_SCHED_INIT
217 #define TARGET_SCHED_INIT m68k_sched_md_init
219 #undef TARGET_SCHED_DFA_PRE_ADVANCE_CYCLE
220 #define TARGET_SCHED_DFA_PRE_ADVANCE_CYCLE m68k_sched_dfa_pre_advance_cycle
222 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
223 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE m68k_sched_dfa_post_advance_cycle
225 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
226 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
227 m68k_sched_first_cycle_multipass_dfa_lookahead
229 #undef TARGET_HANDLE_OPTION
230 #define TARGET_HANDLE_OPTION m68k_handle_option
232 #undef TARGET_RTX_COSTS
233 #define TARGET_RTX_COSTS m68k_rtx_costs
235 #undef TARGET_ATTRIBUTE_TABLE
236 #define TARGET_ATTRIBUTE_TABLE m68k_attribute_table
238 #undef TARGET_PROMOTE_PROTOTYPES
239 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
241 #undef TARGET_STRUCT_VALUE_RTX
242 #define TARGET_STRUCT_VALUE_RTX m68k_struct_value_rtx
244 #undef TARGET_CANNOT_FORCE_CONST_MEM
245 #define TARGET_CANNOT_FORCE_CONST_MEM m68k_illegitimate_symbolic_constant_p
247 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
248 #define TARGET_FUNCTION_OK_FOR_SIBCALL m68k_ok_for_sibcall_p
250 #if M68K_HONOR_TARGET_STRICT_ALIGNMENT
251 #undef TARGET_RETURN_IN_MEMORY
252 #define TARGET_RETURN_IN_MEMORY m68k_return_in_memory
253 #endif
255 static const struct attribute_spec m68k_attribute_table[] =
257 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
258 { "interrupt", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
259 { "interrupt_handler", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
260 { "interrupt_thread", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
261 { NULL, 0, 0, false, false, false, NULL }
264 struct gcc_target targetm = TARGET_INITIALIZER;
266 /* Base flags for 68k ISAs. */
267 #define FL_FOR_isa_00 FL_ISA_68000
268 #define FL_FOR_isa_10 (FL_FOR_isa_00 | FL_ISA_68010)
269 /* FL_68881 controls the default setting of -m68881. gcc has traditionally
270 generated 68881 code for 68020 and 68030 targets unless explicitly told
271 not to. */
272 #define FL_FOR_isa_20 (FL_FOR_isa_10 | FL_ISA_68020 \
273 | FL_BITFIELD | FL_68881)
274 #define FL_FOR_isa_40 (FL_FOR_isa_20 | FL_ISA_68040)
275 #define FL_FOR_isa_cpu32 (FL_FOR_isa_10 | FL_ISA_68020)
277 /* Base flags for ColdFire ISAs. */
278 #define FL_FOR_isa_a (FL_COLDFIRE | FL_ISA_A)
279 #define FL_FOR_isa_aplus (FL_FOR_isa_a | FL_ISA_APLUS | FL_CF_USP)
280 /* Note ISA_B doesn't necessarily include USP (user stack pointer) support. */
281 #define FL_FOR_isa_b (FL_FOR_isa_a | FL_ISA_B | FL_CF_HWDIV)
282 /* ISA_C is not upwardly compatible with ISA_B. */
283 #define FL_FOR_isa_c (FL_FOR_isa_a | FL_ISA_C | FL_CF_USP)
285 enum m68k_isa
287 /* Traditional 68000 instruction sets. */
288 isa_00,
289 isa_10,
290 isa_20,
291 isa_40,
292 isa_cpu32,
293 /* ColdFire instruction set variants. */
294 isa_a,
295 isa_aplus,
296 isa_b,
297 isa_c,
298 isa_max
301 /* Information about one of the -march, -mcpu or -mtune arguments. */
302 struct m68k_target_selection
304 /* The argument being described. */
305 const char *name;
307 /* For -mcpu, this is the device selected by the option.
308 For -mtune and -march, it is a representative device
309 for the microarchitecture or ISA respectively. */
310 enum target_device device;
312 /* The M68K_DEVICE fields associated with DEVICE. See the comment
313 in m68k-devices.def for details. FAMILY is only valid for -mcpu. */
314 const char *family;
315 enum uarch_type microarch;
316 enum m68k_isa isa;
317 unsigned long flags;
320 /* A list of all devices in m68k-devices.def. Used for -mcpu selection. */
321 static const struct m68k_target_selection all_devices[] =
323 #define M68K_DEVICE(NAME,ENUM_VALUE,FAMILY,MULTILIB,MICROARCH,ISA,FLAGS) \
324 { NAME, ENUM_VALUE, FAMILY, u##MICROARCH, ISA, FLAGS | FL_FOR_##ISA },
325 #include "m68k-devices.def"
326 #undef M68K_DEVICE
327 { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
330 /* A list of all ISAs, mapping each one to a representative device.
331 Used for -march selection. */
332 static const struct m68k_target_selection all_isas[] =
334 { "68000", m68000, NULL, u68000, isa_00, FL_FOR_isa_00 },
335 { "68010", m68010, NULL, u68010, isa_10, FL_FOR_isa_10 },
336 { "68020", m68020, NULL, u68020, isa_20, FL_FOR_isa_20 },
337 { "68030", m68030, NULL, u68030, isa_20, FL_FOR_isa_20 },
338 { "68040", m68040, NULL, u68040, isa_40, FL_FOR_isa_40 },
339 { "68060", m68060, NULL, u68060, isa_40, FL_FOR_isa_40 },
340 { "cpu32", cpu32, NULL, ucpu32, isa_20, FL_FOR_isa_cpu32 },
341 { "isaa", mcf5206e, NULL, ucfv2, isa_a, (FL_FOR_isa_a
342 | FL_CF_HWDIV) },
343 { "isaaplus", mcf5271, NULL, ucfv2, isa_aplus, (FL_FOR_isa_aplus
344 | FL_CF_HWDIV) },
345 { "isab", mcf5407, NULL, ucfv4, isa_b, FL_FOR_isa_b },
346 { "isac", unk_device, NULL, ucfv4, isa_c, (FL_FOR_isa_c
347 | FL_CF_HWDIV) },
348 { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
351 /* A list of all microarchitectures, mapping each one to a representative
352 device. Used for -mtune selection. */
353 static const struct m68k_target_selection all_microarchs[] =
355 { "68000", m68000, NULL, u68000, isa_00, FL_FOR_isa_00 },
356 { "68010", m68010, NULL, u68010, isa_10, FL_FOR_isa_10 },
357 { "68020", m68020, NULL, u68020, isa_20, FL_FOR_isa_20 },
358 { "68020-40", m68020, NULL, u68020_40, isa_20, FL_FOR_isa_20 },
359 { "68020-60", m68020, NULL, u68020_60, isa_20, FL_FOR_isa_20 },
360 { "68030", m68030, NULL, u68030, isa_20, FL_FOR_isa_20 },
361 { "68040", m68040, NULL, u68040, isa_40, FL_FOR_isa_40 },
362 { "68060", m68060, NULL, u68060, isa_40, FL_FOR_isa_40 },
363 { "cpu32", cpu32, NULL, ucpu32, isa_20, FL_FOR_isa_cpu32 },
364 { "cfv1", mcf51qe, NULL, ucfv1, isa_c, FL_FOR_isa_c },
365 { "cfv2", mcf5206, NULL, ucfv2, isa_a, FL_FOR_isa_a },
366 { "cfv3", mcf5307, NULL, ucfv3, isa_a, (FL_FOR_isa_a
367 | FL_CF_HWDIV) },
368 { "cfv4", mcf5407, NULL, ucfv4, isa_b, FL_FOR_isa_b },
369 { "cfv4e", mcf547x, NULL, ucfv4e, isa_b, (FL_FOR_isa_b
370 | FL_CF_USP
371 | FL_CF_EMAC
372 | FL_CF_FPU) },
373 { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
376 /* The entries associated with the -mcpu, -march and -mtune settings,
377 or null for options that have not been used. */
378 const struct m68k_target_selection *m68k_cpu_entry;
379 const struct m68k_target_selection *m68k_arch_entry;
380 const struct m68k_target_selection *m68k_tune_entry;
382 /* Which CPU we are generating code for. */
383 enum target_device m68k_cpu;
385 /* Which microarchitecture to tune for. */
386 enum uarch_type m68k_tune;
388 /* Which FPU to use. */
389 enum fpu_type m68k_fpu;
391 /* The set of FL_* flags that apply to the target processor. */
392 unsigned int m68k_cpu_flags;
394 /* The set of FL_* flags that apply to the processor to be tuned for. */
395 unsigned int m68k_tune_flags;
397 /* Asm templates for calling or jumping to an arbitrary symbolic address,
398 or NULL if such calls or jumps are not supported. The address is held
399 in operand 0. */
400 const char *m68k_symbolic_call;
401 const char *m68k_symbolic_jump;
403 /* Enum variable that corresponds to m68k_symbolic_call values. */
404 enum M68K_SYMBOLIC_CALL m68k_symbolic_call_var;
407 /* See whether TABLE has an entry with name NAME. Return true and
408 store the entry in *ENTRY if so, otherwise return false and
409 leave *ENTRY alone. */
411 static bool
412 m68k_find_selection (const struct m68k_target_selection **entry,
413 const struct m68k_target_selection *table,
414 const char *name)
416 size_t i;
418 for (i = 0; table[i].name; i++)
419 if (strcmp (table[i].name, name) == 0)
421 *entry = table + i;
422 return true;
424 return false;
427 /* Implement TARGET_HANDLE_OPTION. */
429 static bool
430 m68k_handle_option (size_t code, const char *arg, int value)
432 switch (code)
434 case OPT_march_:
435 return m68k_find_selection (&m68k_arch_entry, all_isas, arg);
437 case OPT_mcpu_:
438 return m68k_find_selection (&m68k_cpu_entry, all_devices, arg);
440 case OPT_mtune_:
441 return m68k_find_selection (&m68k_tune_entry, all_microarchs, arg);
443 case OPT_m5200:
444 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5206");
446 case OPT_m5206e:
447 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5206e");
449 case OPT_m528x:
450 return m68k_find_selection (&m68k_cpu_entry, all_devices, "528x");
452 case OPT_m5307:
453 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5307");
455 case OPT_m5407:
456 return m68k_find_selection (&m68k_cpu_entry, all_devices, "5407");
458 case OPT_mcfv4e:
459 return m68k_find_selection (&m68k_cpu_entry, all_devices, "547x");
461 case OPT_m68000:
462 case OPT_mc68000:
463 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68000");
465 case OPT_m68010:
466 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68010");
468 case OPT_m68020:
469 case OPT_mc68020:
470 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68020");
472 case OPT_m68020_40:
473 return (m68k_find_selection (&m68k_tune_entry, all_microarchs,
474 "68020-40")
475 && m68k_find_selection (&m68k_cpu_entry, all_devices, "68020"));
477 case OPT_m68020_60:
478 return (m68k_find_selection (&m68k_tune_entry, all_microarchs,
479 "68020-60")
480 && m68k_find_selection (&m68k_cpu_entry, all_devices, "68020"));
482 case OPT_m68030:
483 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68030");
485 case OPT_m68040:
486 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68040");
488 case OPT_m68060:
489 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68060");
491 case OPT_m68302:
492 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68302");
494 case OPT_m68332:
495 case OPT_mcpu32:
496 return m68k_find_selection (&m68k_cpu_entry, all_devices, "68332");
498 case OPT_mshared_library_id_:
499 if (value > MAX_LIBRARY_ID)
500 error ("-mshared-library-id=%s is not between 0 and %d",
501 arg, MAX_LIBRARY_ID);
502 else
504 char *tmp;
505 asprintf (&tmp, "%d", (value * -4) - 4);
506 m68k_library_id_string = tmp;
508 return true;
510 default:
511 return true;
515 /* Sometimes certain combinations of command options do not make
516 sense on a particular target machine. You can define a macro
517 `OVERRIDE_OPTIONS' to take account of this. This macro, if
518 defined, is executed once just after all the command options have
519 been parsed.
521 Don't use this macro to turn on various extra optimizations for
522 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
524 void
525 override_options (void)
527 const struct m68k_target_selection *entry;
528 unsigned long target_mask;
530 /* User can choose:
532 -mcpu=
533 -march=
534 -mtune=
536 -march=ARCH should generate code that runs any processor
537 implementing architecture ARCH. -mcpu=CPU should override -march
538 and should generate code that runs on processor CPU, making free
539 use of any instructions that CPU understands. -mtune=UARCH applies
540 on top of -mcpu or -march and optimizes the code for UARCH. It does
541 not change the target architecture. */
542 if (m68k_cpu_entry)
544 /* Complain if the -march setting is for a different microarchitecture,
545 or includes flags that the -mcpu setting doesn't. */
546 if (m68k_arch_entry
547 && (m68k_arch_entry->microarch != m68k_cpu_entry->microarch
548 || (m68k_arch_entry->flags & ~m68k_cpu_entry->flags) != 0))
549 warning (0, "-mcpu=%s conflicts with -march=%s",
550 m68k_cpu_entry->name, m68k_arch_entry->name);
552 entry = m68k_cpu_entry;
554 else
555 entry = m68k_arch_entry;
557 if (!entry)
558 entry = all_devices + TARGET_CPU_DEFAULT;
560 m68k_cpu_flags = entry->flags;
562 /* Use the architecture setting to derive default values for
563 certain flags. */
564 target_mask = 0;
566 /* ColdFire is lenient about alignment. */
567 if (!TARGET_COLDFIRE)
568 target_mask |= MASK_STRICT_ALIGNMENT;
570 if ((m68k_cpu_flags & FL_BITFIELD) != 0)
571 target_mask |= MASK_BITFIELD;
572 if ((m68k_cpu_flags & FL_CF_HWDIV) != 0)
573 target_mask |= MASK_CF_HWDIV;
574 if ((m68k_cpu_flags & (FL_68881 | FL_CF_FPU)) != 0)
575 target_mask |= MASK_HARD_FLOAT;
576 target_flags |= target_mask & ~target_flags_explicit;
578 /* Set the directly-usable versions of the -mcpu and -mtune settings. */
579 m68k_cpu = entry->device;
580 if (m68k_tune_entry)
582 m68k_tune = m68k_tune_entry->microarch;
583 m68k_tune_flags = m68k_tune_entry->flags;
585 #ifdef M68K_DEFAULT_TUNE
586 else if (!m68k_cpu_entry && !m68k_arch_entry)
588 enum target_device dev;
589 dev = all_microarchs[M68K_DEFAULT_TUNE].device;
590 m68k_tune_flags = all_devices[dev]->flags;
592 #endif
593 else
595 m68k_tune = entry->microarch;
596 m68k_tune_flags = entry->flags;
599 /* Set the type of FPU. */
600 m68k_fpu = (!TARGET_HARD_FLOAT ? FPUTYPE_NONE
601 : (m68k_cpu_flags & FL_COLDFIRE) != 0 ? FPUTYPE_COLDFIRE
602 : FPUTYPE_68881);
604 /* Sanity check to ensure that msep-data and mid-sahred-library are not
605 * both specified together. Doing so simply doesn't make sense.
607 if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
608 error ("cannot specify both -msep-data and -mid-shared-library");
610 /* If we're generating code for a separate A5 relative data segment,
611 * we've got to enable -fPIC as well. This might be relaxable to
612 * -fpic but it hasn't been tested properly.
614 if (TARGET_SEP_DATA || TARGET_ID_SHARED_LIBRARY)
615 flag_pic = 2;
617 /* -mpcrel -fPIC uses 32-bit pc-relative displacements. Raise an
618 error if the target does not support them. */
619 if (TARGET_PCREL && !TARGET_68020 && flag_pic == 2)
620 error ("-mpcrel -fPIC is not currently supported on selected cpu");
622 /* ??? A historic way of turning on pic, or is this intended to
623 be an embedded thing that doesn't have the same name binding
624 significance that it does on hosted ELF systems? */
625 if (TARGET_PCREL && flag_pic == 0)
626 flag_pic = 1;
628 if (!flag_pic)
630 m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_JSR;
632 m68k_symbolic_jump = "jra %a0";
634 else if (TARGET_ID_SHARED_LIBRARY)
635 /* All addresses must be loaded from the GOT. */
637 else if (TARGET_68020 || TARGET_ISAB || TARGET_ISAC)
639 if (TARGET_PCREL)
640 m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_BSR_C;
641 else
642 m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_BSR_P;
644 if (TARGET_ISAC)
645 /* No unconditional long branch */;
646 else if (TARGET_PCREL)
647 m68k_symbolic_jump = "bra%.l %c0";
648 else
649 m68k_symbolic_jump = "bra%.l %p0";
650 /* Turn off function cse if we are doing PIC. We always want
651 function call to be done as `bsr foo@PLTPC'. */
652 /* ??? It's traditional to do this for -mpcrel too, but it isn't
653 clear how intentional that is. */
654 flag_no_function_cse = 1;
657 switch (m68k_symbolic_call_var)
659 case M68K_SYMBOLIC_CALL_JSR:
660 m68k_symbolic_call = "jsr %a0";
661 break;
663 case M68K_SYMBOLIC_CALL_BSR_C:
664 m68k_symbolic_call = "bsr%.l %c0";
665 break;
667 case M68K_SYMBOLIC_CALL_BSR_P:
668 m68k_symbolic_call = "bsr%.l %p0";
669 break;
671 case M68K_SYMBOLIC_CALL_NONE:
672 gcc_assert (m68k_symbolic_call == NULL);
673 break;
675 default:
676 gcc_unreachable ();
679 #ifndef ASM_OUTPUT_ALIGN_WITH_NOP
680 if (align_labels > 2)
682 warning (0, "-falign-labels=%d is not supported", align_labels);
683 align_labels = 0;
685 if (align_loops > 2)
687 warning (0, "-falign-loops=%d is not supported", align_loops);
688 align_loops = 0;
690 #endif
692 SUBTARGET_OVERRIDE_OPTIONS;
694 /* Setup scheduling options. */
695 if (TUNE_CFV1)
696 m68k_sched_cpu = CPU_CFV1;
697 else if (TUNE_CFV2)
698 m68k_sched_cpu = CPU_CFV2;
699 else if (TUNE_CFV3)
700 m68k_sched_cpu = CPU_CFV3;
701 else if (TUNE_CFV4)
702 m68k_sched_cpu = CPU_CFV4;
703 else
705 m68k_sched_cpu = CPU_UNKNOWN;
706 flag_schedule_insns = 0;
707 flag_schedule_insns_after_reload = 0;
708 flag_modulo_sched = 0;
711 if (m68k_sched_cpu != CPU_UNKNOWN)
713 if ((m68k_cpu_flags & (FL_CF_EMAC | FL_CF_EMAC_B)) != 0)
714 m68k_sched_mac = MAC_CF_EMAC;
715 else if ((m68k_cpu_flags & FL_CF_MAC) != 0)
716 m68k_sched_mac = MAC_CF_MAC;
717 else
718 m68k_sched_mac = MAC_NO;
722 /* Generate a macro of the form __mPREFIX_cpu_NAME, where PREFIX is the
723 given argument and NAME is the argument passed to -mcpu. Return NULL
724 if -mcpu was not passed. */
726 const char *
727 m68k_cpp_cpu_ident (const char *prefix)
729 if (!m68k_cpu_entry)
730 return NULL;
731 return concat ("__m", prefix, "_cpu_", m68k_cpu_entry->name, NULL);
734 /* Generate a macro of the form __mPREFIX_family_NAME, where PREFIX is the
735 given argument and NAME is the name of the representative device for
736 the -mcpu argument's family. Return NULL if -mcpu was not passed. */
738 const char *
739 m68k_cpp_cpu_family (const char *prefix)
741 if (!m68k_cpu_entry)
742 return NULL;
743 return concat ("__m", prefix, "_family_", m68k_cpu_entry->family, NULL);
746 /* Return m68k_fk_interrupt_handler if FUNC has an "interrupt" or
747 "interrupt_handler" attribute and interrupt_thread if FUNC has an
748 "interrupt_thread" attribute. Otherwise, return
749 m68k_fk_normal_function. */
751 enum m68k_function_kind
752 m68k_get_function_kind (tree func)
754 tree a;
756 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
758 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
759 if (a != NULL_TREE)
760 return m68k_fk_interrupt_handler;
762 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
763 if (a != NULL_TREE)
764 return m68k_fk_interrupt_handler;
766 a = lookup_attribute ("interrupt_thread", DECL_ATTRIBUTES (func));
767 if (a != NULL_TREE)
768 return m68k_fk_interrupt_thread;
770 return m68k_fk_normal_function;
773 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
774 struct attribute_spec.handler. */
775 static tree
776 m68k_handle_fndecl_attribute (tree *node, tree name,
777 tree args ATTRIBUTE_UNUSED,
778 int flags ATTRIBUTE_UNUSED,
779 bool *no_add_attrs)
781 if (TREE_CODE (*node) != FUNCTION_DECL)
783 warning (OPT_Wattributes, "%qs attribute only applies to functions",
784 IDENTIFIER_POINTER (name));
785 *no_add_attrs = true;
788 if (m68k_get_function_kind (*node) != m68k_fk_normal_function)
790 error ("multiple interrupt attributes not allowed");
791 *no_add_attrs = true;
794 if (!TARGET_FIDOA
795 && !strcmp (IDENTIFIER_POINTER (name), "interrupt_thread"))
797 error ("interrupt_thread is available only on fido");
798 *no_add_attrs = true;
801 return NULL_TREE;
804 static void
805 m68k_compute_frame_layout (void)
807 int regno, saved;
808 unsigned int mask;
809 enum m68k_function_kind func_kind =
810 m68k_get_function_kind (current_function_decl);
811 bool interrupt_handler = func_kind == m68k_fk_interrupt_handler;
812 bool interrupt_thread = func_kind == m68k_fk_interrupt_thread;
814 /* Only compute the frame once per function.
815 Don't cache information until reload has been completed. */
816 if (current_frame.funcdef_no == current_function_funcdef_no
817 && reload_completed)
818 return;
820 current_frame.size = (get_frame_size () + 3) & -4;
822 mask = saved = 0;
824 /* Interrupt thread does not need to save any register. */
825 if (!interrupt_thread)
826 for (regno = 0; regno < 16; regno++)
827 if (m68k_save_reg (regno, interrupt_handler))
829 mask |= 1 << (regno - D0_REG);
830 saved++;
832 current_frame.offset = saved * 4;
833 current_frame.reg_no = saved;
834 current_frame.reg_mask = mask;
836 current_frame.foffset = 0;
837 mask = saved = 0;
838 if (TARGET_HARD_FLOAT)
840 /* Interrupt thread does not need to save any register. */
841 if (!interrupt_thread)
842 for (regno = 16; regno < 24; regno++)
843 if (m68k_save_reg (regno, interrupt_handler))
845 mask |= 1 << (regno - FP0_REG);
846 saved++;
848 current_frame.foffset = saved * TARGET_FP_REG_SIZE;
849 current_frame.offset += current_frame.foffset;
851 current_frame.fpu_no = saved;
852 current_frame.fpu_mask = mask;
854 /* Remember what function this frame refers to. */
855 current_frame.funcdef_no = current_function_funcdef_no;
858 HOST_WIDE_INT
859 m68k_initial_elimination_offset (int from, int to)
861 int argptr_offset;
862 /* The arg pointer points 8 bytes before the start of the arguments,
863 as defined by FIRST_PARM_OFFSET. This makes it coincident with the
864 frame pointer in most frames. */
865 argptr_offset = frame_pointer_needed ? 0 : UNITS_PER_WORD;
866 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
867 return argptr_offset;
869 m68k_compute_frame_layout ();
871 gcc_assert (to == STACK_POINTER_REGNUM);
872 switch (from)
874 case ARG_POINTER_REGNUM:
875 return current_frame.offset + current_frame.size - argptr_offset;
876 case FRAME_POINTER_REGNUM:
877 return current_frame.offset + current_frame.size;
878 default:
879 gcc_unreachable ();
883 /* Refer to the array `regs_ever_live' to determine which registers
884 to save; `regs_ever_live[I]' is nonzero if register number I
885 is ever used in the function. This function is responsible for
886 knowing which registers should not be saved even if used.
887 Return true if we need to save REGNO. */
889 static bool
890 m68k_save_reg (unsigned int regno, bool interrupt_handler)
892 if (flag_pic && regno == PIC_REG)
894 if (crtl->saves_all_registers)
895 return true;
896 if (crtl->uses_pic_offset_table)
897 return true;
898 /* Reload may introduce constant pool references into a function
899 that thitherto didn't need a PIC register. Note that the test
900 above will not catch that case because we will only set
901 crtl->uses_pic_offset_table when emitting
902 the address reloads. */
903 if (crtl->uses_const_pool)
904 return true;
907 if (crtl->calls_eh_return)
909 unsigned int i;
910 for (i = 0; ; i++)
912 unsigned int test = EH_RETURN_DATA_REGNO (i);
913 if (test == INVALID_REGNUM)
914 break;
915 if (test == regno)
916 return true;
920 /* Fixed regs we never touch. */
921 if (fixed_regs[regno])
922 return false;
924 /* The frame pointer (if it is such) is handled specially. */
925 if (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)
926 return false;
928 /* Interrupt handlers must also save call_used_regs
929 if they are live or when calling nested functions. */
930 if (interrupt_handler)
932 if (df_regs_ever_live_p (regno))
933 return true;
935 if (!current_function_is_leaf && call_used_regs[regno])
936 return true;
939 /* Never need to save registers that aren't touched. */
940 if (!df_regs_ever_live_p (regno))
941 return false;
943 /* Otherwise save everything that isn't call-clobbered. */
944 return !call_used_regs[regno];
947 /* Emit RTL for a MOVEM or FMOVEM instruction. BASE + OFFSET represents
948 the lowest memory address. COUNT is the number of registers to be
949 moved, with register REGNO + I being moved if bit I of MASK is set.
950 STORE_P specifies the direction of the move and ADJUST_STACK_P says
951 whether or not this is pre-decrement (if STORE_P) or post-increment
952 (if !STORE_P) operation. */
954 static rtx
955 m68k_emit_movem (rtx base, HOST_WIDE_INT offset,
956 unsigned int count, unsigned int regno,
957 unsigned int mask, bool store_p, bool adjust_stack_p)
959 int i;
960 rtx body, addr, src, operands[2];
961 enum machine_mode mode;
963 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (adjust_stack_p + count));
964 mode = reg_raw_mode[regno];
965 i = 0;
967 if (adjust_stack_p)
969 src = plus_constant (base, (count
970 * GET_MODE_SIZE (mode)
971 * (HOST_WIDE_INT) (store_p ? -1 : 1)));
972 XVECEXP (body, 0, i++) = gen_rtx_SET (VOIDmode, base, src);
975 for (; mask != 0; mask >>= 1, regno++)
976 if (mask & 1)
978 addr = plus_constant (base, offset);
979 operands[!store_p] = gen_frame_mem (mode, addr);
980 operands[store_p] = gen_rtx_REG (mode, regno);
981 XVECEXP (body, 0, i++)
982 = gen_rtx_SET (VOIDmode, operands[0], operands[1]);
983 offset += GET_MODE_SIZE (mode);
985 gcc_assert (i == XVECLEN (body, 0));
987 return emit_insn (body);
990 /* Make INSN a frame-related instruction. */
992 static void
993 m68k_set_frame_related (rtx insn)
995 rtx body;
996 int i;
998 RTX_FRAME_RELATED_P (insn) = 1;
999 body = PATTERN (insn);
1000 if (GET_CODE (body) == PARALLEL)
1001 for (i = 0; i < XVECLEN (body, 0); i++)
1002 RTX_FRAME_RELATED_P (XVECEXP (body, 0, i)) = 1;
1005 /* Emit RTL for the "prologue" define_expand. */
1007 void
1008 m68k_expand_prologue (void)
1010 HOST_WIDE_INT fsize_with_regs;
1011 rtx limit, src, dest, insn;
1013 m68k_compute_frame_layout ();
1015 /* If the stack limit is a symbol, we can check it here,
1016 before actually allocating the space. */
1017 if (crtl->limit_stack
1018 && GET_CODE (stack_limit_rtx) == SYMBOL_REF)
1020 limit = plus_constant (stack_limit_rtx, current_frame.size + 4);
1021 if (!LEGITIMATE_CONSTANT_P (limit))
1023 emit_move_insn (gen_rtx_REG (Pmode, D0_REG), limit);
1024 limit = gen_rtx_REG (Pmode, D0_REG);
1026 emit_insn (gen_cmpsi (stack_pointer_rtx, limit));
1027 emit_insn (gen_conditional_trap (gen_rtx_LTU (VOIDmode,
1028 cc0_rtx, const0_rtx),
1029 const1_rtx));
1032 fsize_with_regs = current_frame.size;
1033 if (TARGET_COLDFIRE)
1035 /* ColdFire's move multiple instructions do not allow pre-decrement
1036 addressing. Add the size of movem saves to the initial stack
1037 allocation instead. */
1038 if (current_frame.reg_no >= MIN_MOVEM_REGS)
1039 fsize_with_regs += current_frame.reg_no * GET_MODE_SIZE (SImode);
1040 if (current_frame.fpu_no >= MIN_FMOVEM_REGS)
1041 fsize_with_regs += current_frame.fpu_no * GET_MODE_SIZE (DFmode);
1044 if (frame_pointer_needed)
1046 if (fsize_with_regs == 0 && TUNE_68040)
1048 /* On the 68040, two separate moves are faster than link.w 0. */
1049 dest = gen_frame_mem (Pmode,
1050 gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx));
1051 m68k_set_frame_related (emit_move_insn (dest, frame_pointer_rtx));
1052 m68k_set_frame_related (emit_move_insn (frame_pointer_rtx,
1053 stack_pointer_rtx));
1055 else if (fsize_with_regs < 0x8000 || TARGET_68020)
1056 m68k_set_frame_related
1057 (emit_insn (gen_link (frame_pointer_rtx,
1058 GEN_INT (-4 - fsize_with_regs))));
1059 else
1061 m68k_set_frame_related
1062 (emit_insn (gen_link (frame_pointer_rtx, GEN_INT (-4))));
1063 m68k_set_frame_related
1064 (emit_insn (gen_addsi3 (stack_pointer_rtx,
1065 stack_pointer_rtx,
1066 GEN_INT (-fsize_with_regs))));
1069 /* If the frame pointer is needed, emit a special barrier that
1070 will prevent the scheduler from moving stores to the frame
1071 before the stack adjustment. */
1072 emit_insn (gen_stack_tie (stack_pointer_rtx, frame_pointer_rtx));
1074 else if (fsize_with_regs != 0)
1075 m68k_set_frame_related
1076 (emit_insn (gen_addsi3 (stack_pointer_rtx,
1077 stack_pointer_rtx,
1078 GEN_INT (-fsize_with_regs))));
1080 if (current_frame.fpu_mask)
1082 gcc_assert (current_frame.fpu_no >= MIN_FMOVEM_REGS);
1083 if (TARGET_68881)
1084 m68k_set_frame_related
1085 (m68k_emit_movem (stack_pointer_rtx,
1086 current_frame.fpu_no * -GET_MODE_SIZE (XFmode),
1087 current_frame.fpu_no, FP0_REG,
1088 current_frame.fpu_mask, true, true));
1089 else
1091 int offset;
1093 /* If we're using moveml to save the integer registers,
1094 the stack pointer will point to the bottom of the moveml
1095 save area. Find the stack offset of the first FP register. */
1096 if (current_frame.reg_no < MIN_MOVEM_REGS)
1097 offset = 0;
1098 else
1099 offset = current_frame.reg_no * GET_MODE_SIZE (SImode);
1100 m68k_set_frame_related
1101 (m68k_emit_movem (stack_pointer_rtx, offset,
1102 current_frame.fpu_no, FP0_REG,
1103 current_frame.fpu_mask, true, false));
1107 /* If the stack limit is not a symbol, check it here.
1108 This has the disadvantage that it may be too late... */
1109 if (crtl->limit_stack)
1111 if (REG_P (stack_limit_rtx))
1113 emit_insn (gen_cmpsi (stack_pointer_rtx, stack_limit_rtx));
1114 emit_insn (gen_conditional_trap (gen_rtx_LTU (VOIDmode,
1115 cc0_rtx, const0_rtx),
1116 const1_rtx));
1118 else if (GET_CODE (stack_limit_rtx) != SYMBOL_REF)
1119 warning (0, "stack limit expression is not supported");
1122 if (current_frame.reg_no < MIN_MOVEM_REGS)
1124 /* Store each register separately in the same order moveml does. */
1125 int i;
1127 for (i = 16; i-- > 0; )
1128 if (current_frame.reg_mask & (1 << i))
1130 src = gen_rtx_REG (SImode, D0_REG + i);
1131 dest = gen_frame_mem (SImode,
1132 gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx));
1133 m68k_set_frame_related (emit_insn (gen_movsi (dest, src)));
1136 else
1138 if (TARGET_COLDFIRE)
1139 /* The required register save space has already been allocated.
1140 The first register should be stored at (%sp). */
1141 m68k_set_frame_related
1142 (m68k_emit_movem (stack_pointer_rtx, 0,
1143 current_frame.reg_no, D0_REG,
1144 current_frame.reg_mask, true, false));
1145 else
1146 m68k_set_frame_related
1147 (m68k_emit_movem (stack_pointer_rtx,
1148 current_frame.reg_no * -GET_MODE_SIZE (SImode),
1149 current_frame.reg_no, D0_REG,
1150 current_frame.reg_mask, true, true));
1153 if (flag_pic
1154 && !TARGET_SEP_DATA
1155 && crtl->uses_pic_offset_table)
1156 insn = emit_insn (gen_load_got (pic_offset_table_rtx));
1159 /* Return true if a simple (return) instruction is sufficient for this
1160 instruction (i.e. if no epilogue is needed). */
1162 bool
1163 m68k_use_return_insn (void)
1165 if (!reload_completed || frame_pointer_needed || get_frame_size () != 0)
1166 return false;
1168 m68k_compute_frame_layout ();
1169 return current_frame.offset == 0;
1172 /* Emit RTL for the "epilogue" or "sibcall_epilogue" define_expand;
1173 SIBCALL_P says which.
1175 The function epilogue should not depend on the current stack pointer!
1176 It should use the frame pointer only, if there is a frame pointer.
1177 This is mandatory because of alloca; we also take advantage of it to
1178 omit stack adjustments before returning. */
1180 void
1181 m68k_expand_epilogue (bool sibcall_p)
1183 HOST_WIDE_INT fsize, fsize_with_regs;
1184 bool big, restore_from_sp;
1186 m68k_compute_frame_layout ();
1188 fsize = current_frame.size;
1189 big = false;
1190 restore_from_sp = false;
1192 /* FIXME : current_function_is_leaf below is too strong.
1193 What we really need to know there is if there could be pending
1194 stack adjustment needed at that point. */
1195 restore_from_sp = (!frame_pointer_needed
1196 || (!cfun->calls_alloca
1197 && current_function_is_leaf));
1199 /* fsize_with_regs is the size we need to adjust the sp when
1200 popping the frame. */
1201 fsize_with_regs = fsize;
1202 if (TARGET_COLDFIRE && restore_from_sp)
1204 /* ColdFire's move multiple instructions do not allow post-increment
1205 addressing. Add the size of movem loads to the final deallocation
1206 instead. */
1207 if (current_frame.reg_no >= MIN_MOVEM_REGS)
1208 fsize_with_regs += current_frame.reg_no * GET_MODE_SIZE (SImode);
1209 if (current_frame.fpu_no >= MIN_FMOVEM_REGS)
1210 fsize_with_regs += current_frame.fpu_no * GET_MODE_SIZE (DFmode);
1213 if (current_frame.offset + fsize >= 0x8000
1214 && !restore_from_sp
1215 && (current_frame.reg_mask || current_frame.fpu_mask))
1217 if (TARGET_COLDFIRE
1218 && (current_frame.reg_no >= MIN_MOVEM_REGS
1219 || current_frame.fpu_no >= MIN_FMOVEM_REGS))
1221 /* ColdFire's move multiple instructions do not support the
1222 (d8,Ax,Xi) addressing mode, so we're as well using a normal
1223 stack-based restore. */
1224 emit_move_insn (gen_rtx_REG (Pmode, A1_REG),
1225 GEN_INT (-(current_frame.offset + fsize)));
1226 emit_insn (gen_addsi3 (stack_pointer_rtx,
1227 gen_rtx_REG (Pmode, A1_REG),
1228 frame_pointer_rtx));
1229 restore_from_sp = true;
1231 else
1233 emit_move_insn (gen_rtx_REG (Pmode, A1_REG), GEN_INT (-fsize));
1234 fsize = 0;
1235 big = true;
1239 if (current_frame.reg_no < MIN_MOVEM_REGS)
1241 /* Restore each register separately in the same order moveml does. */
1242 int i;
1243 HOST_WIDE_INT offset;
1245 offset = current_frame.offset + fsize;
1246 for (i = 0; i < 16; i++)
1247 if (current_frame.reg_mask & (1 << i))
1249 rtx addr;
1251 if (big)
1253 /* Generate the address -OFFSET(%fp,%a1.l). */
1254 addr = gen_rtx_REG (Pmode, A1_REG);
1255 addr = gen_rtx_PLUS (Pmode, addr, frame_pointer_rtx);
1256 addr = plus_constant (addr, -offset);
1258 else if (restore_from_sp)
1259 addr = gen_rtx_POST_INC (Pmode, stack_pointer_rtx);
1260 else
1261 addr = plus_constant (frame_pointer_rtx, -offset);
1262 emit_move_insn (gen_rtx_REG (SImode, D0_REG + i),
1263 gen_frame_mem (SImode, addr));
1264 offset -= GET_MODE_SIZE (SImode);
1267 else if (current_frame.reg_mask)
1269 if (big)
1270 m68k_emit_movem (gen_rtx_PLUS (Pmode,
1271 gen_rtx_REG (Pmode, A1_REG),
1272 frame_pointer_rtx),
1273 -(current_frame.offset + fsize),
1274 current_frame.reg_no, D0_REG,
1275 current_frame.reg_mask, false, false);
1276 else if (restore_from_sp)
1277 m68k_emit_movem (stack_pointer_rtx, 0,
1278 current_frame.reg_no, D0_REG,
1279 current_frame.reg_mask, false,
1280 !TARGET_COLDFIRE);
1281 else
1282 m68k_emit_movem (frame_pointer_rtx,
1283 -(current_frame.offset + fsize),
1284 current_frame.reg_no, D0_REG,
1285 current_frame.reg_mask, false, false);
1288 if (current_frame.fpu_no > 0)
1290 if (big)
1291 m68k_emit_movem (gen_rtx_PLUS (Pmode,
1292 gen_rtx_REG (Pmode, A1_REG),
1293 frame_pointer_rtx),
1294 -(current_frame.foffset + fsize),
1295 current_frame.fpu_no, FP0_REG,
1296 current_frame.fpu_mask, false, false);
1297 else if (restore_from_sp)
1299 if (TARGET_COLDFIRE)
1301 int offset;
1303 /* If we used moveml to restore the integer registers, the
1304 stack pointer will still point to the bottom of the moveml
1305 save area. Find the stack offset of the first FP
1306 register. */
1307 if (current_frame.reg_no < MIN_MOVEM_REGS)
1308 offset = 0;
1309 else
1310 offset = current_frame.reg_no * GET_MODE_SIZE (SImode);
1311 m68k_emit_movem (stack_pointer_rtx, offset,
1312 current_frame.fpu_no, FP0_REG,
1313 current_frame.fpu_mask, false, false);
1315 else
1316 m68k_emit_movem (stack_pointer_rtx, 0,
1317 current_frame.fpu_no, FP0_REG,
1318 current_frame.fpu_mask, false, true);
1320 else
1321 m68k_emit_movem (frame_pointer_rtx,
1322 -(current_frame.foffset + fsize),
1323 current_frame.fpu_no, FP0_REG,
1324 current_frame.fpu_mask, false, false);
1327 if (frame_pointer_needed)
1328 emit_insn (gen_unlink (frame_pointer_rtx));
1329 else if (fsize_with_regs)
1330 emit_insn (gen_addsi3 (stack_pointer_rtx,
1331 stack_pointer_rtx,
1332 GEN_INT (fsize_with_regs)));
1334 if (crtl->calls_eh_return)
1335 emit_insn (gen_addsi3 (stack_pointer_rtx,
1336 stack_pointer_rtx,
1337 EH_RETURN_STACKADJ_RTX));
1339 if (!sibcall_p)
1340 emit_jump_insn (gen_rtx_RETURN (VOIDmode));
1343 /* Return true if X is a valid comparison operator for the dbcc
1344 instruction.
1346 Note it rejects floating point comparison operators.
1347 (In the future we could use Fdbcc).
1349 It also rejects some comparisons when CC_NO_OVERFLOW is set. */
1352 valid_dbcc_comparison_p_2 (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1354 switch (GET_CODE (x))
1356 case EQ: case NE: case GTU: case LTU:
1357 case GEU: case LEU:
1358 return 1;
1360 /* Reject some when CC_NO_OVERFLOW is set. This may be over
1361 conservative */
1362 case GT: case LT: case GE: case LE:
1363 return ! (cc_prev_status.flags & CC_NO_OVERFLOW);
1364 default:
1365 return 0;
1369 /* Return nonzero if flags are currently in the 68881 flag register. */
1371 flags_in_68881 (void)
1373 /* We could add support for these in the future */
1374 return cc_status.flags & CC_IN_68881;
1377 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL_P. */
1379 static bool
1380 m68k_ok_for_sibcall_p (tree decl, tree exp)
1382 enum m68k_function_kind kind;
1384 /* We cannot use sibcalls for nested functions because we use the
1385 static chain register for indirect calls. */
1386 if (CALL_EXPR_STATIC_CHAIN (exp))
1387 return false;
1389 kind = m68k_get_function_kind (current_function_decl);
1390 if (kind == m68k_fk_normal_function)
1391 /* We can always sibcall from a normal function, because it's
1392 undefined if it is calling an interrupt function. */
1393 return true;
1395 /* Otherwise we can only sibcall if the function kind is known to be
1396 the same. */
1397 if (decl && m68k_get_function_kind (decl) == kind)
1398 return true;
1400 return false;
1403 /* Convert X to a legitimate function call memory reference and return the
1404 result. */
1407 m68k_legitimize_call_address (rtx x)
1409 gcc_assert (MEM_P (x));
1410 if (call_operand (XEXP (x, 0), VOIDmode))
1411 return x;
1412 return replace_equiv_address (x, force_reg (Pmode, XEXP (x, 0)));
1415 /* Likewise for sibling calls. */
1418 m68k_legitimize_sibcall_address (rtx x)
1420 gcc_assert (MEM_P (x));
1421 if (sibcall_operand (XEXP (x, 0), VOIDmode))
1422 return x;
1424 emit_move_insn (gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM), XEXP (x, 0));
1425 return replace_equiv_address (x, gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM));
1428 /* Output a dbCC; jCC sequence. Note we do not handle the
1429 floating point version of this sequence (Fdbcc). We also
1430 do not handle alternative conditions when CC_NO_OVERFLOW is
1431 set. It is assumed that valid_dbcc_comparison_p and flags_in_68881 will
1432 kick those out before we get here. */
1434 void
1435 output_dbcc_and_branch (rtx *operands)
1437 switch (GET_CODE (operands[3]))
1439 case EQ:
1440 output_asm_insn ("dbeq %0,%l1\n\tjeq %l2", operands);
1441 break;
1443 case NE:
1444 output_asm_insn ("dbne %0,%l1\n\tjne %l2", operands);
1445 break;
1447 case GT:
1448 output_asm_insn ("dbgt %0,%l1\n\tjgt %l2", operands);
1449 break;
1451 case GTU:
1452 output_asm_insn ("dbhi %0,%l1\n\tjhi %l2", operands);
1453 break;
1455 case LT:
1456 output_asm_insn ("dblt %0,%l1\n\tjlt %l2", operands);
1457 break;
1459 case LTU:
1460 output_asm_insn ("dbcs %0,%l1\n\tjcs %l2", operands);
1461 break;
1463 case GE:
1464 output_asm_insn ("dbge %0,%l1\n\tjge %l2", operands);
1465 break;
1467 case GEU:
1468 output_asm_insn ("dbcc %0,%l1\n\tjcc %l2", operands);
1469 break;
1471 case LE:
1472 output_asm_insn ("dble %0,%l1\n\tjle %l2", operands);
1473 break;
1475 case LEU:
1476 output_asm_insn ("dbls %0,%l1\n\tjls %l2", operands);
1477 break;
1479 default:
1480 gcc_unreachable ();
1483 /* If the decrement is to be done in SImode, then we have
1484 to compensate for the fact that dbcc decrements in HImode. */
1485 switch (GET_MODE (operands[0]))
1487 case SImode:
1488 output_asm_insn ("clr%.w %0\n\tsubq%.l #1,%0\n\tjpl %l1", operands);
1489 break;
1491 case HImode:
1492 break;
1494 default:
1495 gcc_unreachable ();
1499 const char *
1500 output_scc_di (rtx op, rtx operand1, rtx operand2, rtx dest)
1502 rtx loperands[7];
1503 enum rtx_code op_code = GET_CODE (op);
1505 /* This does not produce a useful cc. */
1506 CC_STATUS_INIT;
1508 /* The m68k cmp.l instruction requires operand1 to be a reg as used
1509 below. Swap the operands and change the op if these requirements
1510 are not fulfilled. */
1511 if (GET_CODE (operand2) == REG && GET_CODE (operand1) != REG)
1513 rtx tmp = operand1;
1515 operand1 = operand2;
1516 operand2 = tmp;
1517 op_code = swap_condition (op_code);
1519 loperands[0] = operand1;
1520 if (GET_CODE (operand1) == REG)
1521 loperands[1] = gen_rtx_REG (SImode, REGNO (operand1) + 1);
1522 else
1523 loperands[1] = adjust_address (operand1, SImode, 4);
1524 if (operand2 != const0_rtx)
1526 loperands[2] = operand2;
1527 if (GET_CODE (operand2) == REG)
1528 loperands[3] = gen_rtx_REG (SImode, REGNO (operand2) + 1);
1529 else
1530 loperands[3] = adjust_address (operand2, SImode, 4);
1532 loperands[4] = gen_label_rtx ();
1533 if (operand2 != const0_rtx)
1534 output_asm_insn ("cmp%.l %2,%0\n\tjne %l4\n\tcmp%.l %3,%1", loperands);
1535 else
1537 if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[0]))
1538 output_asm_insn ("tst%.l %0", loperands);
1539 else
1540 output_asm_insn ("cmp%.w #0,%0", loperands);
1542 output_asm_insn ("jne %l4", loperands);
1544 if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[1]))
1545 output_asm_insn ("tst%.l %1", loperands);
1546 else
1547 output_asm_insn ("cmp%.w #0,%1", loperands);
1550 loperands[5] = dest;
1552 switch (op_code)
1554 case EQ:
1555 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1556 CODE_LABEL_NUMBER (loperands[4]));
1557 output_asm_insn ("seq %5", loperands);
1558 break;
1560 case NE:
1561 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1562 CODE_LABEL_NUMBER (loperands[4]));
1563 output_asm_insn ("sne %5", loperands);
1564 break;
1566 case GT:
1567 loperands[6] = gen_label_rtx ();
1568 output_asm_insn ("shi %5\n\tjra %l6", loperands);
1569 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1570 CODE_LABEL_NUMBER (loperands[4]));
1571 output_asm_insn ("sgt %5", loperands);
1572 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1573 CODE_LABEL_NUMBER (loperands[6]));
1574 break;
1576 case GTU:
1577 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1578 CODE_LABEL_NUMBER (loperands[4]));
1579 output_asm_insn ("shi %5", loperands);
1580 break;
1582 case LT:
1583 loperands[6] = gen_label_rtx ();
1584 output_asm_insn ("scs %5\n\tjra %l6", loperands);
1585 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1586 CODE_LABEL_NUMBER (loperands[4]));
1587 output_asm_insn ("slt %5", loperands);
1588 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1589 CODE_LABEL_NUMBER (loperands[6]));
1590 break;
1592 case LTU:
1593 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1594 CODE_LABEL_NUMBER (loperands[4]));
1595 output_asm_insn ("scs %5", loperands);
1596 break;
1598 case GE:
1599 loperands[6] = gen_label_rtx ();
1600 output_asm_insn ("scc %5\n\tjra %l6", loperands);
1601 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1602 CODE_LABEL_NUMBER (loperands[4]));
1603 output_asm_insn ("sge %5", loperands);
1604 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1605 CODE_LABEL_NUMBER (loperands[6]));
1606 break;
1608 case GEU:
1609 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1610 CODE_LABEL_NUMBER (loperands[4]));
1611 output_asm_insn ("scc %5", loperands);
1612 break;
1614 case LE:
1615 loperands[6] = gen_label_rtx ();
1616 output_asm_insn ("sls %5\n\tjra %l6", loperands);
1617 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1618 CODE_LABEL_NUMBER (loperands[4]));
1619 output_asm_insn ("sle %5", loperands);
1620 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1621 CODE_LABEL_NUMBER (loperands[6]));
1622 break;
1624 case LEU:
1625 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1626 CODE_LABEL_NUMBER (loperands[4]));
1627 output_asm_insn ("sls %5", loperands);
1628 break;
1630 default:
1631 gcc_unreachable ();
1633 return "";
1636 const char *
1637 output_btst (rtx *operands, rtx countop, rtx dataop, rtx insn, int signpos)
1639 operands[0] = countop;
1640 operands[1] = dataop;
1642 if (GET_CODE (countop) == CONST_INT)
1644 register int count = INTVAL (countop);
1645 /* If COUNT is bigger than size of storage unit in use,
1646 advance to the containing unit of same size. */
1647 if (count > signpos)
1649 int offset = (count & ~signpos) / 8;
1650 count = count & signpos;
1651 operands[1] = dataop = adjust_address (dataop, QImode, offset);
1653 if (count == signpos)
1654 cc_status.flags = CC_NOT_POSITIVE | CC_Z_IN_NOT_N;
1655 else
1656 cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N;
1658 /* These three statements used to use next_insns_test_no...
1659 but it appears that this should do the same job. */
1660 if (count == 31
1661 && next_insn_tests_no_inequality (insn))
1662 return "tst%.l %1";
1663 if (count == 15
1664 && next_insn_tests_no_inequality (insn))
1665 return "tst%.w %1";
1666 if (count == 7
1667 && next_insn_tests_no_inequality (insn))
1668 return "tst%.b %1";
1669 /* Try to use `movew to ccr' followed by the appropriate branch insn.
1670 On some m68k variants unfortunately that's slower than btst.
1671 On 68000 and higher, that should also work for all HImode operands. */
1672 if (TUNE_CPU32 || TARGET_COLDFIRE || optimize_size)
1674 if (count == 3 && DATA_REG_P (operands[1])
1675 && next_insn_tests_no_inequality (insn))
1677 cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N | CC_NO_OVERFLOW;
1678 return "move%.w %1,%%ccr";
1680 if (count == 2 && DATA_REG_P (operands[1])
1681 && next_insn_tests_no_inequality (insn))
1683 cc_status.flags = CC_NOT_NEGATIVE | CC_INVERTED | CC_NO_OVERFLOW;
1684 return "move%.w %1,%%ccr";
1686 /* count == 1 followed by bvc/bvs and
1687 count == 0 followed by bcc/bcs are also possible, but need
1688 m68k-specific CC_Z_IN_NOT_V and CC_Z_IN_NOT_C flags. */
1691 cc_status.flags = CC_NOT_NEGATIVE;
1693 return "btst %0,%1";
1696 /* Return true if X is a legitimate base register. STRICT_P says
1697 whether we need strict checking. */
1699 bool
1700 m68k_legitimate_base_reg_p (rtx x, bool strict_p)
1702 /* Allow SUBREG everywhere we allow REG. This results in better code. */
1703 if (!strict_p && GET_CODE (x) == SUBREG)
1704 x = SUBREG_REG (x);
1706 return (REG_P (x)
1707 && (strict_p
1708 ? REGNO_OK_FOR_BASE_P (REGNO (x))
1709 : REGNO_OK_FOR_BASE_NONSTRICT_P (REGNO (x))));
1712 /* Return true if X is a legitimate index register. STRICT_P says
1713 whether we need strict checking. */
1715 bool
1716 m68k_legitimate_index_reg_p (rtx x, bool strict_p)
1718 if (!strict_p && GET_CODE (x) == SUBREG)
1719 x = SUBREG_REG (x);
1721 return (REG_P (x)
1722 && (strict_p
1723 ? REGNO_OK_FOR_INDEX_P (REGNO (x))
1724 : REGNO_OK_FOR_INDEX_NONSTRICT_P (REGNO (x))));
1727 /* Return true if X is a legitimate index expression for a (d8,An,Xn) or
1728 (bd,An,Xn) addressing mode. Fill in the INDEX and SCALE fields of
1729 ADDRESS if so. STRICT_P says whether we need strict checking. */
1731 static bool
1732 m68k_decompose_index (rtx x, bool strict_p, struct m68k_address *address)
1734 int scale;
1736 /* Check for a scale factor. */
1737 scale = 1;
1738 if ((TARGET_68020 || TARGET_COLDFIRE)
1739 && GET_CODE (x) == MULT
1740 && GET_CODE (XEXP (x, 1)) == CONST_INT
1741 && (INTVAL (XEXP (x, 1)) == 2
1742 || INTVAL (XEXP (x, 1)) == 4
1743 || (INTVAL (XEXP (x, 1)) == 8
1744 && (TARGET_COLDFIRE_FPU || !TARGET_COLDFIRE))))
1746 scale = INTVAL (XEXP (x, 1));
1747 x = XEXP (x, 0);
1750 /* Check for a word extension. */
1751 if (!TARGET_COLDFIRE
1752 && GET_CODE (x) == SIGN_EXTEND
1753 && GET_MODE (XEXP (x, 0)) == HImode)
1754 x = XEXP (x, 0);
1756 if (m68k_legitimate_index_reg_p (x, strict_p))
1758 address->scale = scale;
1759 address->index = x;
1760 return true;
1763 return false;
1766 /* Return true if X is an illegitimate symbolic constant. */
1768 bool
1769 m68k_illegitimate_symbolic_constant_p (rtx x)
1771 rtx base, offset;
1773 if (M68K_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
1775 split_const (x, &base, &offset);
1776 if (GET_CODE (base) == SYMBOL_REF
1777 && !offset_within_block_p (base, INTVAL (offset)))
1778 return true;
1780 return false;
1783 /* Return true if X is a legitimate constant address that can reach
1784 bytes in the range [X, X + REACH). STRICT_P says whether we need
1785 strict checking. */
1787 static bool
1788 m68k_legitimate_constant_address_p (rtx x, unsigned int reach, bool strict_p)
1790 rtx base, offset;
1792 if (!CONSTANT_ADDRESS_P (x))
1793 return false;
1795 if (flag_pic
1796 && !(strict_p && TARGET_PCREL)
1797 && symbolic_operand (x, VOIDmode))
1798 return false;
1800 if (M68K_OFFSETS_MUST_BE_WITHIN_SECTIONS_P && reach > 1)
1802 split_const (x, &base, &offset);
1803 if (GET_CODE (base) == SYMBOL_REF
1804 && !offset_within_block_p (base, INTVAL (offset) + reach - 1))
1805 return false;
1808 return true;
1811 /* Return true if X is a LABEL_REF for a jump table. Assume that unplaced
1812 labels will become jump tables. */
1814 static bool
1815 m68k_jump_table_ref_p (rtx x)
1817 if (GET_CODE (x) != LABEL_REF)
1818 return false;
1820 x = XEXP (x, 0);
1821 if (!NEXT_INSN (x) && !PREV_INSN (x))
1822 return true;
1824 x = next_nonnote_insn (x);
1825 return x && JUMP_TABLE_DATA_P (x);
1828 /* Return true if X is a legitimate address for values of mode MODE.
1829 STRICT_P says whether strict checking is needed. If the address
1830 is valid, describe its components in *ADDRESS. */
1832 static bool
1833 m68k_decompose_address (enum machine_mode mode, rtx x,
1834 bool strict_p, struct m68k_address *address)
1836 unsigned int reach;
1838 memset (address, 0, sizeof (*address));
1840 if (mode == BLKmode)
1841 reach = 1;
1842 else
1843 reach = GET_MODE_SIZE (mode);
1845 /* Check for (An) (mode 2). */
1846 if (m68k_legitimate_base_reg_p (x, strict_p))
1848 address->base = x;
1849 return true;
1852 /* Check for -(An) and (An)+ (modes 3 and 4). */
1853 if ((GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_INC)
1854 && m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p))
1856 address->code = GET_CODE (x);
1857 address->base = XEXP (x, 0);
1858 return true;
1861 /* Check for (d16,An) (mode 5). */
1862 if (GET_CODE (x) == PLUS
1863 && GET_CODE (XEXP (x, 1)) == CONST_INT
1864 && IN_RANGE (INTVAL (XEXP (x, 1)), -0x8000, 0x8000 - reach)
1865 && m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p))
1867 address->base = XEXP (x, 0);
1868 address->offset = XEXP (x, 1);
1869 return true;
1872 /* Check for GOT loads. These are (bd,An,Xn) addresses if
1873 TARGET_68020 && flag_pic == 2, otherwise they are (d16,An)
1874 addresses. */
1875 if (flag_pic
1876 && GET_CODE (x) == PLUS
1877 && XEXP (x, 0) == pic_offset_table_rtx
1878 && (GET_CODE (XEXP (x, 1)) == SYMBOL_REF
1879 || GET_CODE (XEXP (x, 1)) == LABEL_REF))
1881 address->base = XEXP (x, 0);
1882 address->offset = XEXP (x, 1);
1883 return true;
1886 /* The ColdFire FPU only accepts addressing modes 2-5. */
1887 if (TARGET_COLDFIRE_FPU && GET_MODE_CLASS (mode) == MODE_FLOAT)
1888 return false;
1890 /* Check for (xxx).w and (xxx).l. Also, in the TARGET_PCREL case,
1891 check for (d16,PC) or (bd,PC,Xn) with a suppressed index register.
1892 All these modes are variations of mode 7. */
1893 if (m68k_legitimate_constant_address_p (x, reach, strict_p))
1895 address->offset = x;
1896 return true;
1899 /* Check for (d8,PC,Xn), a mode 7 form. This case is needed for
1900 tablejumps.
1902 ??? do_tablejump creates these addresses before placing the target
1903 label, so we have to assume that unplaced labels are jump table
1904 references. It seems unlikely that we would ever generate indexed
1905 accesses to unplaced labels in other cases. */
1906 if (GET_CODE (x) == PLUS
1907 && m68k_jump_table_ref_p (XEXP (x, 1))
1908 && m68k_decompose_index (XEXP (x, 0), strict_p, address))
1910 address->offset = XEXP (x, 1);
1911 return true;
1914 /* Everything hereafter deals with (d8,An,Xn.SIZE*SCALE) or
1915 (bd,An,Xn.SIZE*SCALE) addresses. */
1917 if (TARGET_68020)
1919 /* Check for a nonzero base displacement. */
1920 if (GET_CODE (x) == PLUS
1921 && m68k_legitimate_constant_address_p (XEXP (x, 1), reach, strict_p))
1923 address->offset = XEXP (x, 1);
1924 x = XEXP (x, 0);
1927 /* Check for a suppressed index register. */
1928 if (m68k_legitimate_base_reg_p (x, strict_p))
1930 address->base = x;
1931 return true;
1934 /* Check for a suppressed base register. Do not allow this case
1935 for non-symbolic offsets as it effectively gives gcc freedom
1936 to treat data registers as base registers, which can generate
1937 worse code. */
1938 if (address->offset
1939 && symbolic_operand (address->offset, VOIDmode)
1940 && m68k_decompose_index (x, strict_p, address))
1941 return true;
1943 else
1945 /* Check for a nonzero base displacement. */
1946 if (GET_CODE (x) == PLUS
1947 && GET_CODE (XEXP (x, 1)) == CONST_INT
1948 && IN_RANGE (INTVAL (XEXP (x, 1)), -0x80, 0x80 - reach))
1950 address->offset = XEXP (x, 1);
1951 x = XEXP (x, 0);
1955 /* We now expect the sum of a base and an index. */
1956 if (GET_CODE (x) == PLUS)
1958 if (m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p)
1959 && m68k_decompose_index (XEXP (x, 1), strict_p, address))
1961 address->base = XEXP (x, 0);
1962 return true;
1965 if (m68k_legitimate_base_reg_p (XEXP (x, 1), strict_p)
1966 && m68k_decompose_index (XEXP (x, 0), strict_p, address))
1968 address->base = XEXP (x, 1);
1969 return true;
1972 return false;
1975 /* Return true if X is a legitimate address for values of mode MODE.
1976 STRICT_P says whether strict checking is needed. */
1978 bool
1979 m68k_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
1981 struct m68k_address address;
1983 return m68k_decompose_address (mode, x, strict_p, &address);
1986 /* Return true if X is a memory, describing its address in ADDRESS if so.
1987 Apply strict checking if called during or after reload. */
1989 static bool
1990 m68k_legitimate_mem_p (rtx x, struct m68k_address *address)
1992 return (MEM_P (x)
1993 && m68k_decompose_address (GET_MODE (x), XEXP (x, 0),
1994 reload_in_progress || reload_completed,
1995 address));
1998 /* Return true if X matches the 'Q' constraint. It must be a memory
1999 with a base address and no constant offset or index. */
2001 bool
2002 m68k_matches_q_p (rtx x)
2004 struct m68k_address address;
2006 return (m68k_legitimate_mem_p (x, &address)
2007 && address.code == UNKNOWN
2008 && address.base
2009 && !address.offset
2010 && !address.index);
2013 /* Return true if X matches the 'U' constraint. It must be a base address
2014 with a constant offset and no index. */
2016 bool
2017 m68k_matches_u_p (rtx x)
2019 struct m68k_address address;
2021 return (m68k_legitimate_mem_p (x, &address)
2022 && address.code == UNKNOWN
2023 && address.base
2024 && address.offset
2025 && !address.index);
2028 /* Legitimize PIC addresses. If the address is already
2029 position-independent, we return ORIG. Newly generated
2030 position-independent addresses go to REG. If we need more
2031 than one register, we lose.
2033 An address is legitimized by making an indirect reference
2034 through the Global Offset Table with the name of the symbol
2035 used as an offset.
2037 The assembler and linker are responsible for placing the
2038 address of the symbol in the GOT. The function prologue
2039 is responsible for initializing a5 to the starting address
2040 of the GOT.
2042 The assembler is also responsible for translating a symbol name
2043 into a constant displacement from the start of the GOT.
2045 A quick example may make things a little clearer:
2047 When not generating PIC code to store the value 12345 into _foo
2048 we would generate the following code:
2050 movel #12345, _foo
2052 When generating PIC two transformations are made. First, the compiler
2053 loads the address of foo into a register. So the first transformation makes:
2055 lea _foo, a0
2056 movel #12345, a0@
2058 The code in movsi will intercept the lea instruction and call this
2059 routine which will transform the instructions into:
2061 movel a5@(_foo:w), a0
2062 movel #12345, a0@
2065 That (in a nutshell) is how *all* symbol and label references are
2066 handled. */
2069 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
2070 rtx reg)
2072 rtx pic_ref = orig;
2074 /* First handle a simple SYMBOL_REF or LABEL_REF */
2075 if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
2077 gcc_assert (reg);
2079 if (TARGET_COLDFIRE && TARGET_XGOT)
2080 /* When compiling with -mxgot switch the code for the above
2081 example will look like this:
2083 movel a5, a0
2084 addl _foo@GOT, a0
2085 movel a0@, a0
2086 movel #12345, a0@ */
2088 rtx pic_offset;
2090 /* Wrap ORIG in UNSPEC_GOTOFF to tip m68k_output_addr_const_extra
2091 to put @GOT after reference. */
2092 pic_offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
2093 UNSPEC_GOTOFF);
2094 pic_offset = gen_rtx_CONST (Pmode, pic_offset);
2095 emit_move_insn (reg, pic_offset);
2096 emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
2097 pic_ref = gen_rtx_MEM (Pmode, reg);
2099 else
2100 pic_ref = gen_rtx_MEM (Pmode,
2101 gen_rtx_PLUS (Pmode,
2102 pic_offset_table_rtx, orig));
2103 crtl->uses_pic_offset_table = 1;
2104 MEM_READONLY_P (pic_ref) = 1;
2105 emit_move_insn (reg, pic_ref);
2106 return reg;
2108 else if (GET_CODE (orig) == CONST)
2110 rtx base;
2112 /* Make sure this has not already been legitimized. */
2113 if (GET_CODE (XEXP (orig, 0)) == PLUS
2114 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2115 return orig;
2117 gcc_assert (reg);
2119 /* legitimize both operands of the PLUS */
2120 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
2122 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2123 orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2124 base == reg ? 0 : reg);
2126 if (GET_CODE (orig) == CONST_INT)
2127 return plus_constant (base, INTVAL (orig));
2128 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
2129 /* Likewise, should we set special REG_NOTEs here? */
2131 return pic_ref;
2136 #define USE_MOVQ(i) ((unsigned) ((i) + 128) <= 255)
2138 /* Return the type of move that should be used for integer I. */
2140 M68K_CONST_METHOD
2141 m68k_const_method (HOST_WIDE_INT i)
2143 unsigned u;
2145 if (USE_MOVQ (i))
2146 return MOVQ;
2148 /* The ColdFire doesn't have byte or word operations. */
2149 /* FIXME: This may not be useful for the m68060 either. */
2150 if (!TARGET_COLDFIRE)
2152 /* if -256 < N < 256 but N is not in range for a moveq
2153 N^ff will be, so use moveq #N^ff, dreg; not.b dreg. */
2154 if (USE_MOVQ (i ^ 0xff))
2155 return NOTB;
2156 /* Likewise, try with not.w */
2157 if (USE_MOVQ (i ^ 0xffff))
2158 return NOTW;
2159 /* This is the only value where neg.w is useful */
2160 if (i == -65408)
2161 return NEGW;
2164 /* Try also with swap. */
2165 u = i;
2166 if (USE_MOVQ ((u >> 16) | (u << 16)))
2167 return SWAP;
2169 if (TARGET_ISAB)
2171 /* Try using MVZ/MVS with an immediate value to load constants. */
2172 if (i >= 0 && i <= 65535)
2173 return MVZ;
2174 if (i >= -32768 && i <= 32767)
2175 return MVS;
2178 /* Otherwise, use move.l */
2179 return MOVL;
2182 /* Return the cost of moving constant I into a data register. */
2184 static int
2185 const_int_cost (HOST_WIDE_INT i)
2187 switch (m68k_const_method (i))
2189 case MOVQ:
2190 /* Constants between -128 and 127 are cheap due to moveq. */
2191 return 0;
2192 case MVZ:
2193 case MVS:
2194 case NOTB:
2195 case NOTW:
2196 case NEGW:
2197 case SWAP:
2198 /* Constants easily generated by moveq + not.b/not.w/neg.w/swap. */
2199 return 1;
2200 case MOVL:
2201 return 2;
2202 default:
2203 gcc_unreachable ();
2207 static bool
2208 m68k_rtx_costs (rtx x, int code, int outer_code, int *total)
2210 switch (code)
2212 case CONST_INT:
2213 /* Constant zero is super cheap due to clr instruction. */
2214 if (x == const0_rtx)
2215 *total = 0;
2216 else
2217 *total = const_int_cost (INTVAL (x));
2218 return true;
2220 case CONST:
2221 case LABEL_REF:
2222 case SYMBOL_REF:
2223 *total = 3;
2224 return true;
2226 case CONST_DOUBLE:
2227 /* Make 0.0 cheaper than other floating constants to
2228 encourage creating tstsf and tstdf insns. */
2229 if (outer_code == COMPARE
2230 && (x == CONST0_RTX (SFmode) || x == CONST0_RTX (DFmode)))
2231 *total = 4;
2232 else
2233 *total = 5;
2234 return true;
2236 /* These are vaguely right for a 68020. */
2237 /* The costs for long multiply have been adjusted to work properly
2238 in synth_mult on the 68020, relative to an average of the time
2239 for add and the time for shift, taking away a little more because
2240 sometimes move insns are needed. */
2241 /* div?.w is relatively cheaper on 68000 counted in COSTS_N_INSNS
2242 terms. */
2243 #define MULL_COST \
2244 (TUNE_68060 ? 2 \
2245 : TUNE_68040 ? 5 \
2246 : (TUNE_CFV2 && TUNE_EMAC) ? 3 \
2247 : (TUNE_CFV2 && TUNE_MAC) ? 4 \
2248 : TUNE_CFV2 ? 8 \
2249 : TARGET_COLDFIRE ? 3 : 13)
2251 #define MULW_COST \
2252 (TUNE_68060 ? 2 \
2253 : TUNE_68040 ? 3 \
2254 : TUNE_68000_10 ? 5 \
2255 : (TUNE_CFV2 && TUNE_EMAC) ? 3 \
2256 : (TUNE_CFV2 && TUNE_MAC) ? 2 \
2257 : TUNE_CFV2 ? 8 \
2258 : TARGET_COLDFIRE ? 2 : 8)
2260 #define DIVW_COST \
2261 (TARGET_CF_HWDIV ? 11 \
2262 : TUNE_68000_10 || TARGET_COLDFIRE ? 12 : 27)
2264 case PLUS:
2265 /* An lea costs about three times as much as a simple add. */
2266 if (GET_MODE (x) == SImode
2267 && GET_CODE (XEXP (x, 1)) == REG
2268 && GET_CODE (XEXP (x, 0)) == MULT
2269 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2270 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2271 && (INTVAL (XEXP (XEXP (x, 0), 1)) == 2
2272 || INTVAL (XEXP (XEXP (x, 0), 1)) == 4
2273 || INTVAL (XEXP (XEXP (x, 0), 1)) == 8))
2275 /* lea an@(dx:l:i),am */
2276 *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 2 : 3);
2277 return true;
2279 return false;
2281 case ASHIFT:
2282 case ASHIFTRT:
2283 case LSHIFTRT:
2284 if (TUNE_68060)
2286 *total = COSTS_N_INSNS(1);
2287 return true;
2289 if (TUNE_68000_10)
2291 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2293 if (INTVAL (XEXP (x, 1)) < 16)
2294 *total = COSTS_N_INSNS (2) + INTVAL (XEXP (x, 1)) / 2;
2295 else
2296 /* We're using clrw + swap for these cases. */
2297 *total = COSTS_N_INSNS (4) + (INTVAL (XEXP (x, 1)) - 16) / 2;
2299 else
2300 *total = COSTS_N_INSNS (10); /* Worst case. */
2301 return true;
2303 /* A shift by a big integer takes an extra instruction. */
2304 if (GET_CODE (XEXP (x, 1)) == CONST_INT
2305 && (INTVAL (XEXP (x, 1)) == 16))
2307 *total = COSTS_N_INSNS (2); /* clrw;swap */
2308 return true;
2310 if (GET_CODE (XEXP (x, 1)) == CONST_INT
2311 && !(INTVAL (XEXP (x, 1)) > 0
2312 && INTVAL (XEXP (x, 1)) <= 8))
2314 *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 1 : 3); /* lsr #i,dn */
2315 return true;
2317 return false;
2319 case MULT:
2320 if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2321 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
2322 && GET_MODE (x) == SImode)
2323 *total = COSTS_N_INSNS (MULW_COST);
2324 else if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
2325 *total = COSTS_N_INSNS (MULW_COST);
2326 else
2327 *total = COSTS_N_INSNS (MULL_COST);
2328 return true;
2330 case DIV:
2331 case UDIV:
2332 case MOD:
2333 case UMOD:
2334 if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
2335 *total = COSTS_N_INSNS (DIVW_COST); /* div.w */
2336 else if (TARGET_CF_HWDIV)
2337 *total = COSTS_N_INSNS (18);
2338 else
2339 *total = COSTS_N_INSNS (43); /* div.l */
2340 return true;
2342 default:
2343 return false;
2347 /* Return an instruction to move CONST_INT OPERANDS[1] into data register
2348 OPERANDS[0]. */
2350 static const char *
2351 output_move_const_into_data_reg (rtx *operands)
2353 HOST_WIDE_INT i;
2355 i = INTVAL (operands[1]);
2356 switch (m68k_const_method (i))
2358 case MVZ:
2359 return "mvzw %1,%0";
2360 case MVS:
2361 return "mvsw %1,%0";
2362 case MOVQ:
2363 return "moveq %1,%0";
2364 case NOTB:
2365 CC_STATUS_INIT;
2366 operands[1] = GEN_INT (i ^ 0xff);
2367 return "moveq %1,%0\n\tnot%.b %0";
2368 case NOTW:
2369 CC_STATUS_INIT;
2370 operands[1] = GEN_INT (i ^ 0xffff);
2371 return "moveq %1,%0\n\tnot%.w %0";
2372 case NEGW:
2373 CC_STATUS_INIT;
2374 return "moveq #-128,%0\n\tneg%.w %0";
2375 case SWAP:
2377 unsigned u = i;
2379 operands[1] = GEN_INT ((u << 16) | (u >> 16));
2380 return "moveq %1,%0\n\tswap %0";
2382 case MOVL:
2383 return "move%.l %1,%0";
2384 default:
2385 gcc_unreachable ();
2389 /* Return true if I can be handled by ISA B's mov3q instruction. */
2391 bool
2392 valid_mov3q_const (HOST_WIDE_INT i)
2394 return TARGET_ISAB && (i == -1 || IN_RANGE (i, 1, 7));
2397 /* Return an instruction to move CONST_INT OPERANDS[1] into OPERANDS[0].
2398 I is the value of OPERANDS[1]. */
2400 static const char *
2401 output_move_simode_const (rtx *operands)
2403 rtx dest;
2404 HOST_WIDE_INT src;
2406 dest = operands[0];
2407 src = INTVAL (operands[1]);
2408 if (src == 0
2409 && (DATA_REG_P (dest) || MEM_P (dest))
2410 /* clr insns on 68000 read before writing. */
2411 && ((TARGET_68010 || TARGET_COLDFIRE)
2412 || !(MEM_P (dest) && MEM_VOLATILE_P (dest))))
2413 return "clr%.l %0";
2414 else if (GET_MODE (dest) == SImode && valid_mov3q_const (src))
2415 return "mov3q%.l %1,%0";
2416 else if (src == 0 && ADDRESS_REG_P (dest))
2417 return "sub%.l %0,%0";
2418 else if (DATA_REG_P (dest))
2419 return output_move_const_into_data_reg (operands);
2420 else if (ADDRESS_REG_P (dest) && IN_RANGE (src, -0x8000, 0x7fff))
2422 if (valid_mov3q_const (src))
2423 return "mov3q%.l %1,%0";
2424 return "move%.w %1,%0";
2426 else if (MEM_P (dest)
2427 && GET_CODE (XEXP (dest, 0)) == PRE_DEC
2428 && REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2429 && IN_RANGE (src, -0x8000, 0x7fff))
2431 if (valid_mov3q_const (src))
2432 return "mov3q%.l %1,%-";
2433 return "pea %a1";
2435 return "move%.l %1,%0";
2438 const char *
2439 output_move_simode (rtx *operands)
2441 if (GET_CODE (operands[1]) == CONST_INT)
2442 return output_move_simode_const (operands);
2443 else if ((GET_CODE (operands[1]) == SYMBOL_REF
2444 || GET_CODE (operands[1]) == CONST)
2445 && push_operand (operands[0], SImode))
2446 return "pea %a1";
2447 else if ((GET_CODE (operands[1]) == SYMBOL_REF
2448 || GET_CODE (operands[1]) == CONST)
2449 && ADDRESS_REG_P (operands[0]))
2450 return "lea %a1,%0";
2451 return "move%.l %1,%0";
2454 const char *
2455 output_move_himode (rtx *operands)
2457 if (GET_CODE (operands[1]) == CONST_INT)
2459 if (operands[1] == const0_rtx
2460 && (DATA_REG_P (operands[0])
2461 || GET_CODE (operands[0]) == MEM)
2462 /* clr insns on 68000 read before writing. */
2463 && ((TARGET_68010 || TARGET_COLDFIRE)
2464 || !(GET_CODE (operands[0]) == MEM
2465 && MEM_VOLATILE_P (operands[0]))))
2466 return "clr%.w %0";
2467 else if (operands[1] == const0_rtx
2468 && ADDRESS_REG_P (operands[0]))
2469 return "sub%.l %0,%0";
2470 else if (DATA_REG_P (operands[0])
2471 && INTVAL (operands[1]) < 128
2472 && INTVAL (operands[1]) >= -128)
2473 return "moveq %1,%0";
2474 else if (INTVAL (operands[1]) < 0x8000
2475 && INTVAL (operands[1]) >= -0x8000)
2476 return "move%.w %1,%0";
2478 else if (CONSTANT_P (operands[1]))
2479 return "move%.l %1,%0";
2480 return "move%.w %1,%0";
2483 const char *
2484 output_move_qimode (rtx *operands)
2486 /* 68k family always modifies the stack pointer by at least 2, even for
2487 byte pushes. The 5200 (ColdFire) does not do this. */
2489 /* This case is generated by pushqi1 pattern now. */
2490 gcc_assert (!(GET_CODE (operands[0]) == MEM
2491 && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
2492 && XEXP (XEXP (operands[0], 0), 0) == stack_pointer_rtx
2493 && ! ADDRESS_REG_P (operands[1])
2494 && ! TARGET_COLDFIRE));
2496 /* clr and st insns on 68000 read before writing. */
2497 if (!ADDRESS_REG_P (operands[0])
2498 && ((TARGET_68010 || TARGET_COLDFIRE)
2499 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
2501 if (operands[1] == const0_rtx)
2502 return "clr%.b %0";
2503 if ((!TARGET_COLDFIRE || DATA_REG_P (operands[0]))
2504 && GET_CODE (operands[1]) == CONST_INT
2505 && (INTVAL (operands[1]) & 255) == 255)
2507 CC_STATUS_INIT;
2508 return "st %0";
2511 if (GET_CODE (operands[1]) == CONST_INT
2512 && DATA_REG_P (operands[0])
2513 && INTVAL (operands[1]) < 128
2514 && INTVAL (operands[1]) >= -128)
2515 return "moveq %1,%0";
2516 if (operands[1] == const0_rtx && ADDRESS_REG_P (operands[0]))
2517 return "sub%.l %0,%0";
2518 if (GET_CODE (operands[1]) != CONST_INT && CONSTANT_P (operands[1]))
2519 return "move%.l %1,%0";
2520 /* 68k family (including the 5200 ColdFire) does not support byte moves to
2521 from address registers. */
2522 if (ADDRESS_REG_P (operands[0]) || ADDRESS_REG_P (operands[1]))
2523 return "move%.w %1,%0";
2524 return "move%.b %1,%0";
2527 const char *
2528 output_move_stricthi (rtx *operands)
2530 if (operands[1] == const0_rtx
2531 /* clr insns on 68000 read before writing. */
2532 && ((TARGET_68010 || TARGET_COLDFIRE)
2533 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
2534 return "clr%.w %0";
2535 return "move%.w %1,%0";
2538 const char *
2539 output_move_strictqi (rtx *operands)
2541 if (operands[1] == const0_rtx
2542 /* clr insns on 68000 read before writing. */
2543 && ((TARGET_68010 || TARGET_COLDFIRE)
2544 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
2545 return "clr%.b %0";
2546 return "move%.b %1,%0";
2549 /* Return the best assembler insn template
2550 for moving operands[1] into operands[0] as a fullword. */
2552 static const char *
2553 singlemove_string (rtx *operands)
2555 if (GET_CODE (operands[1]) == CONST_INT)
2556 return output_move_simode_const (operands);
2557 return "move%.l %1,%0";
2561 /* Output assembler or rtl code to perform a doubleword move insn
2562 with operands OPERANDS.
2563 Pointers to 3 helper functions should be specified:
2564 HANDLE_REG_ADJUST to adjust a register by a small value,
2565 HANDLE_COMPADR to compute an address and
2566 HANDLE_MOVSI to move 4 bytes. */
2568 static void
2569 handle_move_double (rtx operands[2],
2570 void (*handle_reg_adjust) (rtx, int),
2571 void (*handle_compadr) (rtx [2]),
2572 void (*handle_movsi) (rtx [2]))
2574 enum
2576 REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP
2577 } optype0, optype1;
2578 rtx latehalf[2];
2579 rtx middlehalf[2];
2580 rtx xops[2];
2581 rtx addreg0 = 0, addreg1 = 0;
2582 int dest_overlapped_low = 0;
2583 int size = GET_MODE_SIZE (GET_MODE (operands[0]));
2585 middlehalf[0] = 0;
2586 middlehalf[1] = 0;
2588 /* First classify both operands. */
2590 if (REG_P (operands[0]))
2591 optype0 = REGOP;
2592 else if (offsettable_memref_p (operands[0]))
2593 optype0 = OFFSOP;
2594 else if (GET_CODE (XEXP (operands[0], 0)) == POST_INC)
2595 optype0 = POPOP;
2596 else if (GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
2597 optype0 = PUSHOP;
2598 else if (GET_CODE (operands[0]) == MEM)
2599 optype0 = MEMOP;
2600 else
2601 optype0 = RNDOP;
2603 if (REG_P (operands[1]))
2604 optype1 = REGOP;
2605 else if (CONSTANT_P (operands[1]))
2606 optype1 = CNSTOP;
2607 else if (offsettable_memref_p (operands[1]))
2608 optype1 = OFFSOP;
2609 else if (GET_CODE (XEXP (operands[1], 0)) == POST_INC)
2610 optype1 = POPOP;
2611 else if (GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
2612 optype1 = PUSHOP;
2613 else if (GET_CODE (operands[1]) == MEM)
2614 optype1 = MEMOP;
2615 else
2616 optype1 = RNDOP;
2618 /* Check for the cases that the operand constraints are not supposed
2619 to allow to happen. Generating code for these cases is
2620 painful. */
2621 gcc_assert (optype0 != RNDOP && optype1 != RNDOP);
2623 /* If one operand is decrementing and one is incrementing
2624 decrement the former register explicitly
2625 and change that operand into ordinary indexing. */
2627 if (optype0 == PUSHOP && optype1 == POPOP)
2629 operands[0] = XEXP (XEXP (operands[0], 0), 0);
2631 handle_reg_adjust (operands[0], -size);
2633 if (GET_MODE (operands[1]) == XFmode)
2634 operands[0] = gen_rtx_MEM (XFmode, operands[0]);
2635 else if (GET_MODE (operands[0]) == DFmode)
2636 operands[0] = gen_rtx_MEM (DFmode, operands[0]);
2637 else
2638 operands[0] = gen_rtx_MEM (DImode, operands[0]);
2639 optype0 = OFFSOP;
2641 if (optype0 == POPOP && optype1 == PUSHOP)
2643 operands[1] = XEXP (XEXP (operands[1], 0), 0);
2645 handle_reg_adjust (operands[1], -size);
2647 if (GET_MODE (operands[1]) == XFmode)
2648 operands[1] = gen_rtx_MEM (XFmode, operands[1]);
2649 else if (GET_MODE (operands[1]) == DFmode)
2650 operands[1] = gen_rtx_MEM (DFmode, operands[1]);
2651 else
2652 operands[1] = gen_rtx_MEM (DImode, operands[1]);
2653 optype1 = OFFSOP;
2656 /* If an operand is an unoffsettable memory ref, find a register
2657 we can increment temporarily to make it refer to the second word. */
2659 if (optype0 == MEMOP)
2660 addreg0 = find_addr_reg (XEXP (operands[0], 0));
2662 if (optype1 == MEMOP)
2663 addreg1 = find_addr_reg (XEXP (operands[1], 0));
2665 /* Ok, we can do one word at a time.
2666 Normally we do the low-numbered word first,
2667 but if either operand is autodecrementing then we
2668 do the high-numbered word first.
2670 In either case, set up in LATEHALF the operands to use
2671 for the high-numbered word and in some cases alter the
2672 operands in OPERANDS to be suitable for the low-numbered word. */
2674 if (size == 12)
2676 if (optype0 == REGOP)
2678 latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 2);
2679 middlehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2681 else if (optype0 == OFFSOP)
2683 middlehalf[0] = adjust_address (operands[0], SImode, 4);
2684 latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2686 else
2688 middlehalf[0] = adjust_address (operands[0], SImode, 0);
2689 latehalf[0] = adjust_address (operands[0], SImode, 0);
2692 if (optype1 == REGOP)
2694 latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 2);
2695 middlehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2697 else if (optype1 == OFFSOP)
2699 middlehalf[1] = adjust_address (operands[1], SImode, 4);
2700 latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2702 else if (optype1 == CNSTOP)
2704 if (GET_CODE (operands[1]) == CONST_DOUBLE)
2706 REAL_VALUE_TYPE r;
2707 long l[3];
2709 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
2710 REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
2711 operands[1] = GEN_INT (l[0]);
2712 middlehalf[1] = GEN_INT (l[1]);
2713 latehalf[1] = GEN_INT (l[2]);
2715 else
2717 /* No non-CONST_DOUBLE constant should ever appear
2718 here. */
2719 gcc_assert (!CONSTANT_P (operands[1]));
2722 else
2724 middlehalf[1] = adjust_address (operands[1], SImode, 0);
2725 latehalf[1] = adjust_address (operands[1], SImode, 0);
2728 else
2729 /* size is not 12: */
2731 if (optype0 == REGOP)
2732 latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2733 else if (optype0 == OFFSOP)
2734 latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2735 else
2736 latehalf[0] = adjust_address (operands[0], SImode, 0);
2738 if (optype1 == REGOP)
2739 latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2740 else if (optype1 == OFFSOP)
2741 latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2742 else if (optype1 == CNSTOP)
2743 split_double (operands[1], &operands[1], &latehalf[1]);
2744 else
2745 latehalf[1] = adjust_address (operands[1], SImode, 0);
2748 /* If insn is effectively movd N(sp),-(sp) then we will do the
2749 high word first. We should use the adjusted operand 1 (which is N+4(sp))
2750 for the low word as well, to compensate for the first decrement of sp. */
2751 if (optype0 == PUSHOP
2752 && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
2753 && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
2754 operands[1] = middlehalf[1] = latehalf[1];
2756 /* For (set (reg:DI N) (mem:DI ... (reg:SI N) ...)),
2757 if the upper part of reg N does not appear in the MEM, arrange to
2758 emit the move late-half first. Otherwise, compute the MEM address
2759 into the upper part of N and use that as a pointer to the memory
2760 operand. */
2761 if (optype0 == REGOP
2762 && (optype1 == OFFSOP || optype1 == MEMOP))
2764 rtx testlow = gen_rtx_REG (SImode, REGNO (operands[0]));
2766 if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2767 && reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2769 /* If both halves of dest are used in the src memory address,
2770 compute the address into latehalf of dest.
2771 Note that this can't happen if the dest is two data regs. */
2772 compadr:
2773 xops[0] = latehalf[0];
2774 xops[1] = XEXP (operands[1], 0);
2776 handle_compadr (xops);
2777 if (GET_MODE (operands[1]) == XFmode)
2779 operands[1] = gen_rtx_MEM (XFmode, latehalf[0]);
2780 middlehalf[1] = adjust_address (operands[1], DImode, size - 8);
2781 latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2783 else
2785 operands[1] = gen_rtx_MEM (DImode, latehalf[0]);
2786 latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2789 else if (size == 12
2790 && reg_overlap_mentioned_p (middlehalf[0],
2791 XEXP (operands[1], 0)))
2793 /* Check for two regs used by both source and dest.
2794 Note that this can't happen if the dest is all data regs.
2795 It can happen if the dest is d6, d7, a0.
2796 But in that case, latehalf is an addr reg, so
2797 the code at compadr does ok. */
2799 if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2800 || reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2801 goto compadr;
2803 /* JRV says this can't happen: */
2804 gcc_assert (!addreg0 && !addreg1);
2806 /* Only the middle reg conflicts; simply put it last. */
2807 handle_movsi (operands);
2808 handle_movsi (latehalf);
2809 handle_movsi (middlehalf);
2811 return;
2813 else if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0)))
2814 /* If the low half of dest is mentioned in the source memory
2815 address, the arrange to emit the move late half first. */
2816 dest_overlapped_low = 1;
2819 /* If one or both operands autodecrementing,
2820 do the two words, high-numbered first. */
2822 /* Likewise, the first move would clobber the source of the second one,
2823 do them in the other order. This happens only for registers;
2824 such overlap can't happen in memory unless the user explicitly
2825 sets it up, and that is an undefined circumstance. */
2827 if (optype0 == PUSHOP || optype1 == PUSHOP
2828 || (optype0 == REGOP && optype1 == REGOP
2829 && ((middlehalf[1] && REGNO (operands[0]) == REGNO (middlehalf[1]))
2830 || REGNO (operands[0]) == REGNO (latehalf[1])))
2831 || dest_overlapped_low)
2833 /* Make any unoffsettable addresses point at high-numbered word. */
2834 if (addreg0)
2835 handle_reg_adjust (addreg0, size - 4);
2836 if (addreg1)
2837 handle_reg_adjust (addreg1, size - 4);
2839 /* Do that word. */
2840 handle_movsi (latehalf);
2842 /* Undo the adds we just did. */
2843 if (addreg0)
2844 handle_reg_adjust (addreg0, -4);
2845 if (addreg1)
2846 handle_reg_adjust (addreg1, -4);
2848 if (size == 12)
2850 handle_movsi (middlehalf);
2852 if (addreg0)
2853 handle_reg_adjust (addreg0, -4);
2854 if (addreg1)
2855 handle_reg_adjust (addreg1, -4);
2858 /* Do low-numbered word. */
2860 handle_movsi (operands);
2861 return;
2864 /* Normal case: do the two words, low-numbered first. */
2866 handle_movsi (operands);
2868 /* Do the middle one of the three words for long double */
2869 if (size == 12)
2871 if (addreg0)
2872 handle_reg_adjust (addreg0, 4);
2873 if (addreg1)
2874 handle_reg_adjust (addreg1, 4);
2876 handle_movsi (middlehalf);
2879 /* Make any unoffsettable addresses point at high-numbered word. */
2880 if (addreg0)
2881 handle_reg_adjust (addreg0, 4);
2882 if (addreg1)
2883 handle_reg_adjust (addreg1, 4);
2885 /* Do that word. */
2886 handle_movsi (latehalf);
2888 /* Undo the adds we just did. */
2889 if (addreg0)
2890 handle_reg_adjust (addreg0, -(size - 4));
2891 if (addreg1)
2892 handle_reg_adjust (addreg1, -(size - 4));
2894 return;
2897 /* Output assembler code to adjust REG by N. */
2898 static void
2899 output_reg_adjust (rtx reg, int n)
2901 const char *s;
2903 gcc_assert (GET_MODE (reg) == SImode
2904 && -12 <= n && n != 0 && n <= 12);
2906 switch (n)
2908 case 12:
2909 s = "add%.l #12,%0";
2910 break;
2912 case 8:
2913 s = "addq%.l #8,%0";
2914 break;
2916 case 4:
2917 s = "addq%.l #4,%0";
2918 break;
2920 case -12:
2921 s = "sub%.l #12,%0";
2922 break;
2924 case -8:
2925 s = "subq%.l #8,%0";
2926 break;
2928 case -4:
2929 s = "subq%.l #4,%0";
2930 break;
2932 default:
2933 gcc_unreachable ();
2934 s = NULL;
2937 output_asm_insn (s, &reg);
2940 /* Emit rtl code to adjust REG by N. */
2941 static void
2942 emit_reg_adjust (rtx reg1, int n)
2944 rtx reg2;
2946 gcc_assert (GET_MODE (reg1) == SImode
2947 && -12 <= n && n != 0 && n <= 12);
2949 reg1 = copy_rtx (reg1);
2950 reg2 = copy_rtx (reg1);
2952 if (n < 0)
2953 emit_insn (gen_subsi3 (reg1, reg2, GEN_INT (-n)));
2954 else if (n > 0)
2955 emit_insn (gen_addsi3 (reg1, reg2, GEN_INT (n)));
2956 else
2957 gcc_unreachable ();
2960 /* Output assembler to load address OPERANDS[0] to register OPERANDS[1]. */
2961 static void
2962 output_compadr (rtx operands[2])
2964 output_asm_insn ("lea %a1,%0", operands);
2967 /* Output the best assembler insn for moving operands[1] into operands[0]
2968 as a fullword. */
2969 static void
2970 output_movsi (rtx operands[2])
2972 output_asm_insn (singlemove_string (operands), operands);
2975 /* Copy OP and change its mode to MODE. */
2976 static rtx
2977 copy_operand (rtx op, enum machine_mode mode)
2979 /* ??? This looks really ugly. There must be a better way
2980 to change a mode on the operand. */
2981 if (GET_MODE (op) != VOIDmode)
2983 if (REG_P (op))
2984 op = gen_rtx_REG (mode, REGNO (op));
2985 else
2987 op = copy_rtx (op);
2988 PUT_MODE (op, mode);
2992 return op;
2995 /* Emit rtl code for moving operands[1] into operands[0] as a fullword. */
2996 static void
2997 emit_movsi (rtx operands[2])
2999 operands[0] = copy_operand (operands[0], SImode);
3000 operands[1] = copy_operand (operands[1], SImode);
3002 emit_insn (gen_movsi (operands[0], operands[1]));
3005 /* Output assembler code to perform a doubleword move insn
3006 with operands OPERANDS. */
3007 const char *
3008 output_move_double (rtx *operands)
3010 handle_move_double (operands,
3011 output_reg_adjust, output_compadr, output_movsi);
3013 return "";
3016 /* Output rtl code to perform a doubleword move insn
3017 with operands OPERANDS. */
3018 void
3019 m68k_emit_move_double (rtx operands[2])
3021 handle_move_double (operands, emit_reg_adjust, emit_movsi, emit_movsi);
3024 /* Ensure mode of ORIG, a REG rtx, is MODE. Returns either ORIG or a
3025 new rtx with the correct mode. */
3027 static rtx
3028 force_mode (enum machine_mode mode, rtx orig)
3030 if (mode == GET_MODE (orig))
3031 return orig;
3033 if (REGNO (orig) >= FIRST_PSEUDO_REGISTER)
3034 abort ();
3036 return gen_rtx_REG (mode, REGNO (orig));
3039 static int
3040 fp_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3042 return reg_renumber && FP_REG_P (op);
3045 /* Emit insns to move operands[1] into operands[0].
3047 Return 1 if we have written out everything that needs to be done to
3048 do the move. Otherwise, return 0 and the caller will emit the move
3049 normally.
3051 Note SCRATCH_REG may not be in the proper mode depending on how it
3052 will be used. This routine is responsible for creating a new copy
3053 of SCRATCH_REG in the proper mode. */
3056 emit_move_sequence (rtx *operands, enum machine_mode mode, rtx scratch_reg)
3058 register rtx operand0 = operands[0];
3059 register rtx operand1 = operands[1];
3060 register rtx tem;
3062 if (scratch_reg
3063 && reload_in_progress && GET_CODE (operand0) == REG
3064 && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
3065 operand0 = reg_equiv_mem[REGNO (operand0)];
3066 else if (scratch_reg
3067 && reload_in_progress && GET_CODE (operand0) == SUBREG
3068 && GET_CODE (SUBREG_REG (operand0)) == REG
3069 && REGNO (SUBREG_REG (operand0)) >= FIRST_PSEUDO_REGISTER)
3071 /* We must not alter SUBREG_BYTE (operand0) since that would confuse
3072 the code which tracks sets/uses for delete_output_reload. */
3073 rtx temp = gen_rtx_SUBREG (GET_MODE (operand0),
3074 reg_equiv_mem [REGNO (SUBREG_REG (operand0))],
3075 SUBREG_BYTE (operand0));
3076 operand0 = alter_subreg (&temp);
3079 if (scratch_reg
3080 && reload_in_progress && GET_CODE (operand1) == REG
3081 && REGNO (operand1) >= FIRST_PSEUDO_REGISTER)
3082 operand1 = reg_equiv_mem[REGNO (operand1)];
3083 else if (scratch_reg
3084 && reload_in_progress && GET_CODE (operand1) == SUBREG
3085 && GET_CODE (SUBREG_REG (operand1)) == REG
3086 && REGNO (SUBREG_REG (operand1)) >= FIRST_PSEUDO_REGISTER)
3088 /* We must not alter SUBREG_BYTE (operand0) since that would confuse
3089 the code which tracks sets/uses for delete_output_reload. */
3090 rtx temp = gen_rtx_SUBREG (GET_MODE (operand1),
3091 reg_equiv_mem [REGNO (SUBREG_REG (operand1))],
3092 SUBREG_BYTE (operand1));
3093 operand1 = alter_subreg (&temp);
3096 if (scratch_reg && reload_in_progress && GET_CODE (operand0) == MEM
3097 && ((tem = find_replacement (&XEXP (operand0, 0)))
3098 != XEXP (operand0, 0)))
3099 operand0 = gen_rtx_MEM (GET_MODE (operand0), tem);
3100 if (scratch_reg && reload_in_progress && GET_CODE (operand1) == MEM
3101 && ((tem = find_replacement (&XEXP (operand1, 0)))
3102 != XEXP (operand1, 0)))
3103 operand1 = gen_rtx_MEM (GET_MODE (operand1), tem);
3105 /* Handle secondary reloads for loads/stores of FP registers where
3106 the address is symbolic by using the scratch register */
3107 if (fp_reg_operand (operand0, mode)
3108 && ((GET_CODE (operand1) == MEM
3109 && ! memory_address_p (DFmode, XEXP (operand1, 0)))
3110 || ((GET_CODE (operand1) == SUBREG
3111 && GET_CODE (XEXP (operand1, 0)) == MEM
3112 && !memory_address_p (DFmode, XEXP (XEXP (operand1, 0), 0)))))
3113 && scratch_reg)
3115 if (GET_CODE (operand1) == SUBREG)
3116 operand1 = XEXP (operand1, 0);
3118 /* SCRATCH_REG will hold an address. We want
3119 it in SImode regardless of what mode it was originally given
3120 to us. */
3121 scratch_reg = force_mode (SImode, scratch_reg);
3123 /* D might not fit in 14 bits either; for such cases load D into
3124 scratch reg. */
3125 if (!memory_address_p (Pmode, XEXP (operand1, 0)))
3127 emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
3128 emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1, 0)),
3129 Pmode,
3130 XEXP (XEXP (operand1, 0), 0),
3131 scratch_reg));
3133 else
3134 emit_move_insn (scratch_reg, XEXP (operand1, 0));
3135 emit_insn (gen_rtx_SET (VOIDmode, operand0,
3136 gen_rtx_MEM (mode, scratch_reg)));
3137 return 1;
3139 else if (fp_reg_operand (operand1, mode)
3140 && ((GET_CODE (operand0) == MEM
3141 && ! memory_address_p (DFmode, XEXP (operand0, 0)))
3142 || ((GET_CODE (operand0) == SUBREG)
3143 && GET_CODE (XEXP (operand0, 0)) == MEM
3144 && !memory_address_p (DFmode, XEXP (XEXP (operand0, 0), 0))))
3145 && scratch_reg)
3147 if (GET_CODE (operand0) == SUBREG)
3148 operand0 = XEXP (operand0, 0);
3150 /* SCRATCH_REG will hold an address and maybe the actual data. We want
3151 it in SIMODE regardless of what mode it was originally given
3152 to us. */
3153 scratch_reg = force_mode (SImode, scratch_reg);
3155 /* D might not fit in 14 bits either; for such cases load D into
3156 scratch reg. */
3157 if (!memory_address_p (Pmode, XEXP (operand0, 0)))
3159 emit_move_insn (scratch_reg, XEXP (XEXP (operand0, 0), 1));
3160 emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand0,
3161 0)),
3162 Pmode,
3163 XEXP (XEXP (operand0, 0),
3165 scratch_reg));
3167 else
3168 emit_move_insn (scratch_reg, XEXP (operand0, 0));
3169 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_MEM (mode, scratch_reg),
3170 operand1));
3171 return 1;
3173 /* Handle secondary reloads for loads of FP registers from constant
3174 expressions by forcing the constant into memory.
3176 use scratch_reg to hold the address of the memory location.
3178 The proper fix is to change PREFERRED_RELOAD_CLASS to return
3179 NO_REGS when presented with a const_int and an register class
3180 containing only FP registers. Doing so unfortunately creates
3181 more problems than it solves. Fix this for 2.5. */
3182 else if (fp_reg_operand (operand0, mode)
3183 && CONSTANT_P (operand1)
3184 && scratch_reg)
3186 rtx xoperands[2];
3188 /* SCRATCH_REG will hold an address and maybe the actual data. We want
3189 it in SIMODE regardless of what mode it was originally given
3190 to us. */
3191 scratch_reg = force_mode (SImode, scratch_reg);
3193 /* Force the constant into memory and put the address of the
3194 memory location into scratch_reg. */
3195 xoperands[0] = scratch_reg;
3196 xoperands[1] = XEXP (force_const_mem (mode, operand1), 0);
3197 emit_insn (gen_rtx_SET (mode, scratch_reg, xoperands[1]));
3199 /* Now load the destination register. */
3200 emit_insn (gen_rtx_SET (mode, operand0,
3201 gen_rtx_MEM (mode, scratch_reg)));
3202 return 1;
3205 /* Now have insn-emit do whatever it normally does. */
3206 return 0;
3209 /* Split one or more DImode RTL references into pairs of SImode
3210 references. The RTL can be REG, offsettable MEM, integer constant, or
3211 CONST_DOUBLE. "operands" is a pointer to an array of DImode RTL to
3212 split and "num" is its length. lo_half and hi_half are output arrays
3213 that parallel "operands". */
3215 void
3216 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
3218 while (num--)
3220 rtx op = operands[num];
3222 /* simplify_subreg refuses to split volatile memory addresses,
3223 but we still have to handle it. */
3224 if (GET_CODE (op) == MEM)
3226 lo_half[num] = adjust_address (op, SImode, 4);
3227 hi_half[num] = adjust_address (op, SImode, 0);
3229 else
3231 lo_half[num] = simplify_gen_subreg (SImode, op,
3232 GET_MODE (op) == VOIDmode
3233 ? DImode : GET_MODE (op), 4);
3234 hi_half[num] = simplify_gen_subreg (SImode, op,
3235 GET_MODE (op) == VOIDmode
3236 ? DImode : GET_MODE (op), 0);
3241 /* Split X into a base and a constant offset, storing them in *BASE
3242 and *OFFSET respectively. */
3244 static void
3245 m68k_split_offset (rtx x, rtx *base, HOST_WIDE_INT *offset)
3247 *offset = 0;
3248 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
3250 *offset += INTVAL (XEXP (x, 1));
3251 x = XEXP (x, 0);
3253 *base = x;
3256 /* Return true if PATTERN is a PARALLEL suitable for a movem or fmovem
3257 instruction. STORE_P says whether the move is a load or store.
3259 If the instruction uses post-increment or pre-decrement addressing,
3260 AUTOMOD_BASE is the base register and AUTOMOD_OFFSET is the total
3261 adjustment. This adjustment will be made by the first element of
3262 PARALLEL, with the loads or stores starting at element 1. If the
3263 instruction does not use post-increment or pre-decrement addressing,
3264 AUTOMOD_BASE is null, AUTOMOD_OFFSET is 0, and the loads or stores
3265 start at element 0. */
3267 bool
3268 m68k_movem_pattern_p (rtx pattern, rtx automod_base,
3269 HOST_WIDE_INT automod_offset, bool store_p)
3271 rtx base, mem_base, set, mem, reg, last_reg;
3272 HOST_WIDE_INT offset, mem_offset;
3273 int i, first, len;
3274 enum reg_class rclass;
3276 len = XVECLEN (pattern, 0);
3277 first = (automod_base != NULL);
3279 if (automod_base)
3281 /* Stores must be pre-decrement and loads must be post-increment. */
3282 if (store_p != (automod_offset < 0))
3283 return false;
3285 /* Work out the base and offset for lowest memory location. */
3286 base = automod_base;
3287 offset = (automod_offset < 0 ? automod_offset : 0);
3289 else
3291 /* Allow any valid base and offset in the first access. */
3292 base = NULL;
3293 offset = 0;
3296 last_reg = NULL;
3297 rclass = NO_REGS;
3298 for (i = first; i < len; i++)
3300 /* We need a plain SET. */
3301 set = XVECEXP (pattern, 0, i);
3302 if (GET_CODE (set) != SET)
3303 return false;
3305 /* Check that we have a memory location... */
3306 mem = XEXP (set, !store_p);
3307 if (!MEM_P (mem) || !memory_operand (mem, VOIDmode))
3308 return false;
3310 /* ...with the right address. */
3311 if (base == NULL)
3313 m68k_split_offset (XEXP (mem, 0), &base, &offset);
3314 /* The ColdFire instruction only allows (An) and (d16,An) modes.
3315 There are no mode restrictions for 680x0 besides the
3316 automodification rules enforced above. */
3317 if (TARGET_COLDFIRE
3318 && !m68k_legitimate_base_reg_p (base, reload_completed))
3319 return false;
3321 else
3323 m68k_split_offset (XEXP (mem, 0), &mem_base, &mem_offset);
3324 if (!rtx_equal_p (base, mem_base) || offset != mem_offset)
3325 return false;
3328 /* Check that we have a register of the required mode and class. */
3329 reg = XEXP (set, store_p);
3330 if (!REG_P (reg)
3331 || !HARD_REGISTER_P (reg)
3332 || GET_MODE (reg) != reg_raw_mode[REGNO (reg)])
3333 return false;
3335 if (last_reg)
3337 /* The register must belong to RCLASS and have a higher number
3338 than the register in the previous SET. */
3339 if (!TEST_HARD_REG_BIT (reg_class_contents[rclass], REGNO (reg))
3340 || REGNO (last_reg) >= REGNO (reg))
3341 return false;
3343 else
3345 /* Work out which register class we need. */
3346 if (INT_REGNO_P (REGNO (reg)))
3347 rclass = GENERAL_REGS;
3348 else if (FP_REGNO_P (REGNO (reg)))
3349 rclass = FP_REGS;
3350 else
3351 return false;
3354 last_reg = reg;
3355 offset += GET_MODE_SIZE (GET_MODE (reg));
3358 /* If we have an automodification, check whether the final offset is OK. */
3359 if (automod_base && offset != (automod_offset < 0 ? 0 : automod_offset))
3360 return false;
3362 /* Reject unprofitable cases. */
3363 if (len < first + (rclass == FP_REGS ? MIN_FMOVEM_REGS : MIN_MOVEM_REGS))
3364 return false;
3366 return true;
3369 /* Return the assembly code template for a movem or fmovem instruction
3370 whose pattern is given by PATTERN. Store the template's operands
3371 in OPERANDS.
3373 If the instruction uses post-increment or pre-decrement addressing,
3374 AUTOMOD_OFFSET is the total adjustment, otherwise it is 0. STORE_P
3375 is true if this is a store instruction. */
3377 const char *
3378 m68k_output_movem (rtx *operands, rtx pattern,
3379 HOST_WIDE_INT automod_offset, bool store_p)
3381 unsigned int mask;
3382 int i, first;
3384 gcc_assert (GET_CODE (pattern) == PARALLEL);
3385 mask = 0;
3386 first = (automod_offset != 0);
3387 for (i = first; i < XVECLEN (pattern, 0); i++)
3389 /* When using movem with pre-decrement addressing, register X + D0_REG
3390 is controlled by bit 15 - X. For all other addressing modes,
3391 register X + D0_REG is controlled by bit X. Confusingly, the
3392 register mask for fmovem is in the opposite order to that for
3393 movem. */
3394 unsigned int regno;
3396 gcc_assert (MEM_P (XEXP (XVECEXP (pattern, 0, i), !store_p)));
3397 gcc_assert (REG_P (XEXP (XVECEXP (pattern, 0, i), store_p)));
3398 regno = REGNO (XEXP (XVECEXP (pattern, 0, i), store_p));
3399 if (automod_offset < 0)
3401 if (FP_REGNO_P (regno))
3402 mask |= 1 << (regno - FP0_REG);
3403 else
3404 mask |= 1 << (15 - (regno - D0_REG));
3406 else
3408 if (FP_REGNO_P (regno))
3409 mask |= 1 << (7 - (regno - FP0_REG));
3410 else
3411 mask |= 1 << (regno - D0_REG);
3414 CC_STATUS_INIT;
3416 if (automod_offset == 0)
3417 operands[0] = XEXP (XEXP (XVECEXP (pattern, 0, first), !store_p), 0);
3418 else if (automod_offset < 0)
3419 operands[0] = gen_rtx_PRE_DEC (Pmode, SET_DEST (XVECEXP (pattern, 0, 0)));
3420 else
3421 operands[0] = gen_rtx_POST_INC (Pmode, SET_DEST (XVECEXP (pattern, 0, 0)));
3422 operands[1] = GEN_INT (mask);
3423 if (FP_REGNO_P (REGNO (XEXP (XVECEXP (pattern, 0, first), store_p))))
3425 if (store_p)
3426 return "fmovem %1,%a0";
3427 else
3428 return "fmovem %a0,%1";
3430 else
3432 if (store_p)
3433 return "movem%.l %1,%a0";
3434 else
3435 return "movem%.l %a0,%1";
3439 /* Return a REG that occurs in ADDR with coefficient 1.
3440 ADDR can be effectively incremented by incrementing REG. */
3442 static rtx
3443 find_addr_reg (rtx addr)
3445 while (GET_CODE (addr) == PLUS)
3447 if (GET_CODE (XEXP (addr, 0)) == REG)
3448 addr = XEXP (addr, 0);
3449 else if (GET_CODE (XEXP (addr, 1)) == REG)
3450 addr = XEXP (addr, 1);
3451 else if (CONSTANT_P (XEXP (addr, 0)))
3452 addr = XEXP (addr, 1);
3453 else if (CONSTANT_P (XEXP (addr, 1)))
3454 addr = XEXP (addr, 0);
3455 else
3456 gcc_unreachable ();
3458 gcc_assert (GET_CODE (addr) == REG);
3459 return addr;
3462 /* Output assembler code to perform a 32-bit 3-operand add. */
3464 const char *
3465 output_addsi3 (rtx *operands)
3467 if (! operands_match_p (operands[0], operands[1]))
3469 if (!ADDRESS_REG_P (operands[1]))
3471 rtx tmp = operands[1];
3473 operands[1] = operands[2];
3474 operands[2] = tmp;
3477 /* These insns can result from reloads to access
3478 stack slots over 64k from the frame pointer. */
3479 if (GET_CODE (operands[2]) == CONST_INT
3480 && (INTVAL (operands[2]) < -32768 || INTVAL (operands[2]) > 32767))
3481 return "move%.l %2,%0\n\tadd%.l %1,%0";
3482 if (GET_CODE (operands[2]) == REG)
3483 return MOTOROLA ? "lea (%1,%2.l),%0" : "lea %1@(0,%2:l),%0";
3484 return MOTOROLA ? "lea (%c2,%1),%0" : "lea %1@(%c2),%0";
3486 if (GET_CODE (operands[2]) == CONST_INT)
3488 if (INTVAL (operands[2]) > 0
3489 && INTVAL (operands[2]) <= 8)
3490 return "addq%.l %2,%0";
3491 if (INTVAL (operands[2]) < 0
3492 && INTVAL (operands[2]) >= -8)
3494 operands[2] = GEN_INT (- INTVAL (operands[2]));
3495 return "subq%.l %2,%0";
3497 /* On the CPU32 it is faster to use two addql instructions to
3498 add a small integer (8 < N <= 16) to a register.
3499 Likewise for subql. */
3500 if (TUNE_CPU32 && REG_P (operands[0]))
3502 if (INTVAL (operands[2]) > 8
3503 && INTVAL (operands[2]) <= 16)
3505 operands[2] = GEN_INT (INTVAL (operands[2]) - 8);
3506 return "addq%.l #8,%0\n\taddq%.l %2,%0";
3508 if (INTVAL (operands[2]) < -8
3509 && INTVAL (operands[2]) >= -16)
3511 operands[2] = GEN_INT (- INTVAL (operands[2]) - 8);
3512 return "subq%.l #8,%0\n\tsubq%.l %2,%0";
3515 if (ADDRESS_REG_P (operands[0])
3516 && INTVAL (operands[2]) >= -0x8000
3517 && INTVAL (operands[2]) < 0x8000)
3519 if (TUNE_68040)
3520 return "add%.w %2,%0";
3521 else
3522 return MOTOROLA ? "lea (%c2,%0),%0" : "lea %0@(%c2),%0";
3525 return "add%.l %2,%0";
3528 /* Store in cc_status the expressions that the condition codes will
3529 describe after execution of an instruction whose pattern is EXP.
3530 Do not alter them if the instruction would not alter the cc's. */
3532 /* On the 68000, all the insns to store in an address register fail to
3533 set the cc's. However, in some cases these instructions can make it
3534 possibly invalid to use the saved cc's. In those cases we clear out
3535 some or all of the saved cc's so they won't be used. */
3537 void
3538 notice_update_cc (rtx exp, rtx insn)
3540 if (GET_CODE (exp) == SET)
3542 if (GET_CODE (SET_SRC (exp)) == CALL)
3543 CC_STATUS_INIT;
3544 else if (ADDRESS_REG_P (SET_DEST (exp)))
3546 if (cc_status.value1 && modified_in_p (cc_status.value1, insn))
3547 cc_status.value1 = 0;
3548 if (cc_status.value2 && modified_in_p (cc_status.value2, insn))
3549 cc_status.value2 = 0;
3551 /* fmoves to memory or data registers do not set the condition
3552 codes. Normal moves _do_ set the condition codes, but not in
3553 a way that is appropriate for comparison with 0, because -0.0
3554 would be treated as a negative nonzero number. Note that it
3555 isn't appropriate to conditionalize this restriction on
3556 HONOR_SIGNED_ZEROS because that macro merely indicates whether
3557 we care about the difference between -0.0 and +0.0. */
3558 else if (!FP_REG_P (SET_DEST (exp))
3559 && SET_DEST (exp) != cc0_rtx
3560 && (FP_REG_P (SET_SRC (exp))
3561 || GET_CODE (SET_SRC (exp)) == FIX
3562 || FLOAT_MODE_P (GET_MODE (SET_DEST (exp)))))
3563 CC_STATUS_INIT;
3564 /* A pair of move insns doesn't produce a useful overall cc. */
3565 else if (!FP_REG_P (SET_DEST (exp))
3566 && !FP_REG_P (SET_SRC (exp))
3567 && GET_MODE_SIZE (GET_MODE (SET_SRC (exp))) > 4
3568 && (GET_CODE (SET_SRC (exp)) == REG
3569 || GET_CODE (SET_SRC (exp)) == MEM
3570 || GET_CODE (SET_SRC (exp)) == CONST_DOUBLE))
3571 CC_STATUS_INIT;
3572 else if (SET_DEST (exp) != pc_rtx)
3574 cc_status.flags = 0;
3575 cc_status.value1 = SET_DEST (exp);
3576 cc_status.value2 = SET_SRC (exp);
3579 else if (GET_CODE (exp) == PARALLEL
3580 && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
3582 rtx dest = SET_DEST (XVECEXP (exp, 0, 0));
3583 rtx src = SET_SRC (XVECEXP (exp, 0, 0));
3585 if (ADDRESS_REG_P (dest))
3586 CC_STATUS_INIT;
3587 else if (dest != pc_rtx)
3589 cc_status.flags = 0;
3590 cc_status.value1 = dest;
3591 cc_status.value2 = src;
3594 else
3595 CC_STATUS_INIT;
3596 if (cc_status.value2 != 0
3597 && ADDRESS_REG_P (cc_status.value2)
3598 && GET_MODE (cc_status.value2) == QImode)
3599 CC_STATUS_INIT;
3600 if (cc_status.value2 != 0)
3601 switch (GET_CODE (cc_status.value2))
3603 case ASHIFT: case ASHIFTRT: case LSHIFTRT:
3604 case ROTATE: case ROTATERT:
3605 /* These instructions always clear the overflow bit, and set
3606 the carry to the bit shifted out. */
3607 /* ??? We don't currently have a way to signal carry not valid,
3608 nor do we check for it in the branch insns. */
3609 CC_STATUS_INIT;
3610 break;
3612 case PLUS: case MINUS: case MULT:
3613 case DIV: case UDIV: case MOD: case UMOD: case NEG:
3614 if (GET_MODE (cc_status.value2) != VOIDmode)
3615 cc_status.flags |= CC_NO_OVERFLOW;
3616 break;
3617 case ZERO_EXTEND:
3618 /* (SET r1 (ZERO_EXTEND r2)) on this machine
3619 ends with a move insn moving r2 in r2's mode.
3620 Thus, the cc's are set for r2.
3621 This can set N bit spuriously. */
3622 cc_status.flags |= CC_NOT_NEGATIVE;
3624 default:
3625 break;
3627 if (cc_status.value1 && GET_CODE (cc_status.value1) == REG
3628 && cc_status.value2
3629 && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
3630 cc_status.value2 = 0;
3631 if (((cc_status.value1 && FP_REG_P (cc_status.value1))
3632 || (cc_status.value2 && FP_REG_P (cc_status.value2))))
3633 cc_status.flags = CC_IN_68881;
3634 if (cc_status.value2 && GET_CODE (cc_status.value2) == COMPARE
3635 && GET_MODE_CLASS (GET_MODE (XEXP (cc_status.value2, 0))) == MODE_FLOAT)
3637 cc_status.flags = CC_IN_68881;
3638 if (!FP_REG_P (XEXP (cc_status.value2, 0)))
3639 cc_status.flags |= CC_REVERSED;
3643 const char *
3644 output_move_const_double (rtx *operands)
3646 int code = standard_68881_constant_p (operands[1]);
3648 if (code != 0)
3650 static char buf[40];
3652 sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
3653 return buf;
3655 return "fmove%.d %1,%0";
3658 const char *
3659 output_move_const_single (rtx *operands)
3661 int code = standard_68881_constant_p (operands[1]);
3663 if (code != 0)
3665 static char buf[40];
3667 sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
3668 return buf;
3670 return "fmove%.s %f1,%0";
3673 /* Return nonzero if X, a CONST_DOUBLE, has a value that we can get
3674 from the "fmovecr" instruction.
3675 The value, anded with 0xff, gives the code to use in fmovecr
3676 to get the desired constant. */
3678 /* This code has been fixed for cross-compilation. */
3680 static int inited_68881_table = 0;
3682 static const char *const strings_68881[7] = {
3683 "0.0",
3684 "1.0",
3685 "10.0",
3686 "100.0",
3687 "10000.0",
3688 "1e8",
3689 "1e16"
3692 static const int codes_68881[7] = {
3693 0x0f,
3694 0x32,
3695 0x33,
3696 0x34,
3697 0x35,
3698 0x36,
3699 0x37
3702 REAL_VALUE_TYPE values_68881[7];
3704 /* Set up values_68881 array by converting the decimal values
3705 strings_68881 to binary. */
3707 void
3708 init_68881_table (void)
3710 int i;
3711 REAL_VALUE_TYPE r;
3712 enum machine_mode mode;
3714 mode = SFmode;
3715 for (i = 0; i < 7; i++)
3717 if (i == 6)
3718 mode = DFmode;
3719 r = REAL_VALUE_ATOF (strings_68881[i], mode);
3720 values_68881[i] = r;
3722 inited_68881_table = 1;
3726 standard_68881_constant_p (rtx x)
3728 REAL_VALUE_TYPE r;
3729 int i;
3731 /* fmovecr must be emulated on the 68040 and 68060, so it shouldn't be
3732 used at all on those chips. */
3733 if (TUNE_68040_60)
3734 return 0;
3736 if (! inited_68881_table)
3737 init_68881_table ();
3739 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3741 /* Use REAL_VALUES_IDENTICAL instead of REAL_VALUES_EQUAL so that -0.0
3742 is rejected. */
3743 for (i = 0; i < 6; i++)
3745 if (REAL_VALUES_IDENTICAL (r, values_68881[i]))
3746 return (codes_68881[i]);
3749 if (GET_MODE (x) == SFmode)
3750 return 0;
3752 if (REAL_VALUES_EQUAL (r, values_68881[6]))
3753 return (codes_68881[6]);
3755 /* larger powers of ten in the constants ram are not used
3756 because they are not equal to a `double' C constant. */
3757 return 0;
3760 /* If X is a floating-point constant, return the logarithm of X base 2,
3761 or 0 if X is not a power of 2. */
3764 floating_exact_log2 (rtx x)
3766 REAL_VALUE_TYPE r, r1;
3767 int exp;
3769 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3771 if (REAL_VALUES_LESS (r, dconst1))
3772 return 0;
3774 exp = real_exponent (&r);
3775 real_2expN (&r1, exp, DFmode);
3776 if (REAL_VALUES_EQUAL (r1, r))
3777 return exp;
3779 return 0;
3782 /* A C compound statement to output to stdio stream STREAM the
3783 assembler syntax for an instruction operand X. X is an RTL
3784 expression.
3786 CODE is a value that can be used to specify one of several ways
3787 of printing the operand. It is used when identical operands
3788 must be printed differently depending on the context. CODE
3789 comes from the `%' specification that was used to request
3790 printing of the operand. If the specification was just `%DIGIT'
3791 then CODE is 0; if the specification was `%LTR DIGIT' then CODE
3792 is the ASCII code for LTR.
3794 If X is a register, this macro should print the register's name.
3795 The names can be found in an array `reg_names' whose type is
3796 `char *[]'. `reg_names' is initialized from `REGISTER_NAMES'.
3798 When the machine description has a specification `%PUNCT' (a `%'
3799 followed by a punctuation character), this macro is called with
3800 a null pointer for X and the punctuation character for CODE.
3802 The m68k specific codes are:
3804 '.' for dot needed in Motorola-style opcode names.
3805 '-' for an operand pushing on the stack:
3806 sp@-, -(sp) or -(%sp) depending on the style of syntax.
3807 '+' for an operand pushing on the stack:
3808 sp@+, (sp)+ or (%sp)+ depending on the style of syntax.
3809 '@' for a reference to the top word on the stack:
3810 sp@, (sp) or (%sp) depending on the style of syntax.
3811 '#' for an immediate operand prefix (# in MIT and Motorola syntax
3812 but & in SGS syntax).
3813 '!' for the cc register (used in an `and to cc' insn).
3814 '$' for the letter `s' in an op code, but only on the 68040.
3815 '&' for the letter `d' in an op code, but only on the 68040.
3816 '/' for register prefix needed by longlong.h.
3817 '?' for m68k_library_id_string
3819 'b' for byte insn (no effect, on the Sun; this is for the ISI).
3820 'd' to force memory addressing to be absolute, not relative.
3821 'f' for float insn (print a CONST_DOUBLE as a float rather than in hex)
3822 'x' for float insn (print a CONST_DOUBLE as a float rather than in hex),
3823 or print pair of registers as rx:ry.
3824 'p' print an address with @PLTPC attached, but only if the operand
3825 is not locally-bound. */
3827 void
3828 print_operand (FILE *file, rtx op, int letter)
3830 if (letter == '.')
3832 if (MOTOROLA)
3833 fprintf (file, ".");
3835 else if (letter == '#')
3836 asm_fprintf (file, "%I");
3837 else if (letter == '-')
3838 asm_fprintf (file, MOTOROLA ? "-(%Rsp)" : "%Rsp@-");
3839 else if (letter == '+')
3840 asm_fprintf (file, MOTOROLA ? "(%Rsp)+" : "%Rsp@+");
3841 else if (letter == '@')
3842 asm_fprintf (file, MOTOROLA ? "(%Rsp)" : "%Rsp@");
3843 else if (letter == '!')
3844 asm_fprintf (file, "%Rfpcr");
3845 else if (letter == '$')
3847 if (TARGET_68040)
3848 fprintf (file, "s");
3850 else if (letter == '&')
3852 if (TARGET_68040)
3853 fprintf (file, "d");
3855 else if (letter == '/')
3856 asm_fprintf (file, "%R");
3857 else if (letter == '?')
3858 asm_fprintf (file, m68k_library_id_string);
3859 else if (letter == 'p')
3861 output_addr_const (file, op);
3862 if (!(GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op)))
3863 fprintf (file, "@PLTPC");
3865 else if (GET_CODE (op) == REG)
3867 if (letter == 'R')
3868 /* Print out the second register name of a register pair.
3869 I.e., R (6) => 7. */
3870 fputs (M68K_REGNAME(REGNO (op) + 1), file);
3871 else
3872 fputs (M68K_REGNAME(REGNO (op)), file);
3874 else if (GET_CODE (op) == MEM)
3876 output_address (XEXP (op, 0));
3877 if (letter == 'd' && ! TARGET_68020
3878 && CONSTANT_ADDRESS_P (XEXP (op, 0))
3879 && !(GET_CODE (XEXP (op, 0)) == CONST_INT
3880 && INTVAL (XEXP (op, 0)) < 0x8000
3881 && INTVAL (XEXP (op, 0)) >= -0x8000))
3882 fprintf (file, MOTOROLA ? ".l" : ":l");
3884 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == SFmode)
3886 REAL_VALUE_TYPE r;
3887 long l;
3888 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3889 REAL_VALUE_TO_TARGET_SINGLE (r, l);
3890 asm_fprintf (file, "%I0x%lx", l);
3892 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == XFmode)
3894 REAL_VALUE_TYPE r;
3895 long l[3];
3896 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3897 REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
3898 asm_fprintf (file, "%I0x%lx%08lx%08lx", l[0], l[1], l[2]);
3900 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == DFmode)
3902 REAL_VALUE_TYPE r;
3903 long l[2];
3904 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3905 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
3906 asm_fprintf (file, "%I0x%lx%08lx", l[0], l[1]);
3908 else
3910 /* Use `print_operand_address' instead of `output_addr_const'
3911 to ensure that we print relevant PIC stuff. */
3912 asm_fprintf (file, "%I");
3913 if (TARGET_PCREL
3914 && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST))
3915 print_operand_address (file, op);
3916 else
3917 output_addr_const (file, op);
3921 /* m68k implementation of OUTPUT_ADDR_CONST_EXTRA. */
3923 bool
3924 m68k_output_addr_const_extra (FILE *file, rtx x)
3926 if (GET_CODE (x) != UNSPEC || XINT (x, 1) != UNSPEC_GOTOFF)
3927 return false;
3929 output_addr_const (file, XVECEXP (x, 0, 0));
3930 /* ??? What is the non-MOTOROLA syntax? */
3931 fputs ("@GOT", file);
3932 return true;
3936 /* A C compound statement to output to stdio stream STREAM the
3937 assembler syntax for an instruction operand that is a memory
3938 reference whose address is ADDR. ADDR is an RTL expression.
3940 Note that this contains a kludge that knows that the only reason
3941 we have an address (plus (label_ref...) (reg...)) when not generating
3942 PIC code is in the insn before a tablejump, and we know that m68k.md
3943 generates a label LInnn: on such an insn.
3945 It is possible for PIC to generate a (plus (label_ref...) (reg...))
3946 and we handle that just like we would a (plus (symbol_ref...) (reg...)).
3948 This routine is responsible for distinguishing between -fpic and -fPIC
3949 style relocations in an address. When generating -fpic code the
3950 offset is output in word mode (e.g. movel a5@(_foo:w), a0). When generating
3951 -fPIC code the offset is output in long mode (e.g. movel a5@(_foo:l), a0) */
3953 void
3954 print_operand_address (FILE *file, rtx addr)
3956 struct m68k_address address;
3958 if (!m68k_decompose_address (QImode, addr, true, &address))
3959 gcc_unreachable ();
3961 if (address.code == PRE_DEC)
3962 fprintf (file, MOTOROLA ? "-(%s)" : "%s@-",
3963 M68K_REGNAME (REGNO (address.base)));
3964 else if (address.code == POST_INC)
3965 fprintf (file, MOTOROLA ? "(%s)+" : "%s@+",
3966 M68K_REGNAME (REGNO (address.base)));
3967 else if (!address.base && !address.index)
3969 /* A constant address. */
3970 gcc_assert (address.offset == addr);
3971 if (GET_CODE (addr) == CONST_INT)
3973 /* (xxx).w or (xxx).l. */
3974 if (IN_RANGE (INTVAL (addr), -0x8000, 0x7fff))
3975 fprintf (file, MOTOROLA ? "%d.w" : "%d:w", (int) INTVAL (addr));
3976 else
3977 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
3979 else if (TARGET_PCREL)
3981 /* (d16,PC) or (bd,PC,Xn) (with suppressed index register). */
3982 fputc ('(', file);
3983 output_addr_const (file, addr);
3984 asm_fprintf (file, flag_pic == 1 ? ":w,%Rpc)" : ":l,%Rpc)");
3986 else
3988 /* (xxx).l. We need a special case for SYMBOL_REF if the symbol
3989 name ends in `.<letter>', as the last 2 characters can be
3990 mistaken as a size suffix. Put the name in parentheses. */
3991 if (GET_CODE (addr) == SYMBOL_REF
3992 && strlen (XSTR (addr, 0)) > 2
3993 && XSTR (addr, 0)[strlen (XSTR (addr, 0)) - 2] == '.')
3995 putc ('(', file);
3996 output_addr_const (file, addr);
3997 putc (')', file);
3999 else
4000 output_addr_const (file, addr);
4003 else
4005 int labelno;
4007 /* If ADDR is a (d8,pc,Xn) address, this is the number of the
4008 label being accessed, otherwise it is -1. */
4009 labelno = (address.offset
4010 && !address.base
4011 && GET_CODE (address.offset) == LABEL_REF
4012 ? CODE_LABEL_NUMBER (XEXP (address.offset, 0))
4013 : -1);
4014 if (MOTOROLA)
4016 /* Print the "offset(base" component. */
4017 if (labelno >= 0)
4018 asm_fprintf (file, "%LL%d(%Rpc,", labelno);
4019 else
4021 if (address.offset)
4023 output_addr_const (file, address.offset);
4024 if (flag_pic && address.base == pic_offset_table_rtx)
4026 fprintf (file, "@GOT");
4027 if (flag_pic == 1 && TARGET_68020)
4028 fprintf (file, ".w");
4031 putc ('(', file);
4032 if (address.base)
4033 fputs (M68K_REGNAME (REGNO (address.base)), file);
4035 /* Print the ",index" component, if any. */
4036 if (address.index)
4038 if (address.base)
4039 putc (',', file);
4040 fprintf (file, "%s.%c",
4041 M68K_REGNAME (REGNO (address.index)),
4042 GET_MODE (address.index) == HImode ? 'w' : 'l');
4043 if (address.scale != 1)
4044 fprintf (file, "*%d", address.scale);
4046 putc (')', file);
4048 else /* !MOTOROLA */
4050 if (!address.offset && !address.index)
4051 fprintf (file, "%s@", M68K_REGNAME (REGNO (address.base)));
4052 else
4054 /* Print the "base@(offset" component. */
4055 if (labelno >= 0)
4056 asm_fprintf (file, "%Rpc@(%LL%d", labelno);
4057 else
4059 if (address.base)
4060 fputs (M68K_REGNAME (REGNO (address.base)), file);
4061 fprintf (file, "@(");
4062 if (address.offset)
4064 output_addr_const (file, address.offset);
4065 if (address.base == pic_offset_table_rtx && TARGET_68020)
4066 switch (flag_pic)
4068 case 1:
4069 fprintf (file, ":w"); break;
4070 case 2:
4071 fprintf (file, ":l"); break;
4072 default:
4073 break;
4077 /* Print the ",index" component, if any. */
4078 if (address.index)
4080 fprintf (file, ",%s:%c",
4081 M68K_REGNAME (REGNO (address.index)),
4082 GET_MODE (address.index) == HImode ? 'w' : 'l');
4083 if (address.scale != 1)
4084 fprintf (file, ":%d", address.scale);
4086 putc (')', file);
4092 /* Check for cases where a clr insns can be omitted from code using
4093 strict_low_part sets. For example, the second clrl here is not needed:
4094 clrl d0; movw a0@+,d0; use d0; clrl d0; movw a0@+; use d0; ...
4096 MODE is the mode of this STRICT_LOW_PART set. FIRST_INSN is the clear
4097 insn we are checking for redundancy. TARGET is the register set by the
4098 clear insn. */
4100 bool
4101 strict_low_part_peephole_ok (enum machine_mode mode, rtx first_insn,
4102 rtx target)
4104 rtx p = first_insn;
4106 while ((p = PREV_INSN (p)))
4108 if (NOTE_INSN_BASIC_BLOCK_P (p))
4109 return false;
4111 if (NOTE_P (p))
4112 continue;
4114 /* If it isn't an insn, then give up. */
4115 if (!INSN_P (p))
4116 return false;
4118 if (reg_set_p (target, p))
4120 rtx set = single_set (p);
4121 rtx dest;
4123 /* If it isn't an easy to recognize insn, then give up. */
4124 if (! set)
4125 return false;
4127 dest = SET_DEST (set);
4129 /* If this sets the entire target register to zero, then our
4130 first_insn is redundant. */
4131 if (rtx_equal_p (dest, target)
4132 && SET_SRC (set) == const0_rtx)
4133 return true;
4134 else if (GET_CODE (dest) == STRICT_LOW_PART
4135 && GET_CODE (XEXP (dest, 0)) == REG
4136 && REGNO (XEXP (dest, 0)) == REGNO (target)
4137 && (GET_MODE_SIZE (GET_MODE (XEXP (dest, 0)))
4138 <= GET_MODE_SIZE (mode)))
4139 /* This is a strict low part set which modifies less than
4140 we are using, so it is safe. */
4142 else
4143 return false;
4147 return false;
4150 /* Operand predicates for implementing asymmetric pc-relative addressing
4151 on m68k. The m68k supports pc-relative addressing (mode 7, register 2)
4152 when used as a source operand, but not as a destination operand.
4154 We model this by restricting the meaning of the basic predicates
4155 (general_operand, memory_operand, etc) to forbid the use of this
4156 addressing mode, and then define the following predicates that permit
4157 this addressing mode. These predicates can then be used for the
4158 source operands of the appropriate instructions.
4160 n.b. While it is theoretically possible to change all machine patterns
4161 to use this addressing more where permitted by the architecture,
4162 it has only been implemented for "common" cases: SImode, HImode, and
4163 QImode operands, and only for the principle operations that would
4164 require this addressing mode: data movement and simple integer operations.
4166 In parallel with these new predicates, two new constraint letters
4167 were defined: 'S' and 'T'. 'S' is the -mpcrel analog of 'm'.
4168 'T' replaces 's' in the non-pcrel case. It is a no-op in the pcrel case.
4169 In the pcrel case 's' is only valid in combination with 'a' registers.
4170 See addsi3, subsi3, cmpsi, and movsi patterns for a better understanding
4171 of how these constraints are used.
4173 The use of these predicates is strictly optional, though patterns that
4174 don't will cause an extra reload register to be allocated where one
4175 was not necessary:
4177 lea (abc:w,%pc),%a0 ; need to reload address
4178 moveq &1,%d1 ; since write to pc-relative space
4179 movel %d1,%a0@ ; is not allowed
4181 lea (abc:w,%pc),%a1 ; no need to reload address here
4182 movel %a1@,%d0 ; since "movel (abc:w,%pc),%d0" is ok
4184 For more info, consult tiemann@cygnus.com.
4187 All of the ugliness with predicates and constraints is due to the
4188 simple fact that the m68k does not allow a pc-relative addressing
4189 mode as a destination. gcc does not distinguish between source and
4190 destination addresses. Hence, if we claim that pc-relative address
4191 modes are valid, e.g. GO_IF_LEGITIMATE_ADDRESS accepts them, then we
4192 end up with invalid code. To get around this problem, we left
4193 pc-relative modes as invalid addresses, and then added special
4194 predicates and constraints to accept them.
4196 A cleaner way to handle this is to modify gcc to distinguish
4197 between source and destination addresses. We can then say that
4198 pc-relative is a valid source address but not a valid destination
4199 address, and hopefully avoid a lot of the predicate and constraint
4200 hackery. Unfortunately, this would be a pretty big change. It would
4201 be a useful change for a number of ports, but there aren't any current
4202 plans to undertake this.
4204 ***************************************************************************/
4207 const char *
4208 output_andsi3 (rtx *operands)
4210 int logval;
4211 if (GET_CODE (operands[2]) == CONST_INT
4212 && (INTVAL (operands[2]) | 0xffff) == -1
4213 && (DATA_REG_P (operands[0])
4214 || offsettable_memref_p (operands[0]))
4215 && !TARGET_COLDFIRE)
4217 if (GET_CODE (operands[0]) != REG)
4218 operands[0] = adjust_address (operands[0], HImode, 2);
4219 operands[2] = GEN_INT (INTVAL (operands[2]) & 0xffff);
4220 /* Do not delete a following tstl %0 insn; that would be incorrect. */
4221 CC_STATUS_INIT;
4222 if (operands[2] == const0_rtx)
4223 return "clr%.w %0";
4224 return "and%.w %2,%0";
4226 if (GET_CODE (operands[2]) == CONST_INT
4227 && (logval = exact_log2 (~ INTVAL (operands[2]))) >= 0
4228 && (DATA_REG_P (operands[0])
4229 || offsettable_memref_p (operands[0])))
4231 if (DATA_REG_P (operands[0]))
4232 operands[1] = GEN_INT (logval);
4233 else
4235 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
4236 operands[1] = GEN_INT (logval % 8);
4238 /* This does not set condition codes in a standard way. */
4239 CC_STATUS_INIT;
4240 return "bclr %1,%0";
4242 return "and%.l %2,%0";
4245 const char *
4246 output_iorsi3 (rtx *operands)
4248 register int logval;
4249 if (GET_CODE (operands[2]) == CONST_INT
4250 && INTVAL (operands[2]) >> 16 == 0
4251 && (DATA_REG_P (operands[0])
4252 || offsettable_memref_p (operands[0]))
4253 && !TARGET_COLDFIRE)
4255 if (GET_CODE (operands[0]) != REG)
4256 operands[0] = adjust_address (operands[0], HImode, 2);
4257 /* Do not delete a following tstl %0 insn; that would be incorrect. */
4258 CC_STATUS_INIT;
4259 if (INTVAL (operands[2]) == 0xffff)
4260 return "mov%.w %2,%0";
4261 return "or%.w %2,%0";
4263 if (GET_CODE (operands[2]) == CONST_INT
4264 && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
4265 && (DATA_REG_P (operands[0])
4266 || offsettable_memref_p (operands[0])))
4268 if (DATA_REG_P (operands[0]))
4269 operands[1] = GEN_INT (logval);
4270 else
4272 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
4273 operands[1] = GEN_INT (logval % 8);
4275 CC_STATUS_INIT;
4276 return "bset %1,%0";
4278 return "or%.l %2,%0";
4281 const char *
4282 output_xorsi3 (rtx *operands)
4284 register int logval;
4285 if (GET_CODE (operands[2]) == CONST_INT
4286 && INTVAL (operands[2]) >> 16 == 0
4287 && (offsettable_memref_p (operands[0]) || DATA_REG_P (operands[0]))
4288 && !TARGET_COLDFIRE)
4290 if (! DATA_REG_P (operands[0]))
4291 operands[0] = adjust_address (operands[0], HImode, 2);
4292 /* Do not delete a following tstl %0 insn; that would be incorrect. */
4293 CC_STATUS_INIT;
4294 if (INTVAL (operands[2]) == 0xffff)
4295 return "not%.w %0";
4296 return "eor%.w %2,%0";
4298 if (GET_CODE (operands[2]) == CONST_INT
4299 && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
4300 && (DATA_REG_P (operands[0])
4301 || offsettable_memref_p (operands[0])))
4303 if (DATA_REG_P (operands[0]))
4304 operands[1] = GEN_INT (logval);
4305 else
4307 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
4308 operands[1] = GEN_INT (logval % 8);
4310 CC_STATUS_INIT;
4311 return "bchg %1,%0";
4313 return "eor%.l %2,%0";
4316 /* Return the instruction that should be used for a call to address X,
4317 which is known to be in operand 0. */
4319 const char *
4320 output_call (rtx x)
4322 if (symbolic_operand (x, VOIDmode))
4323 return m68k_symbolic_call;
4324 else
4325 return "jsr %a0";
4328 /* Likewise sibling calls. */
4330 const char *
4331 output_sibcall (rtx x)
4333 if (symbolic_operand (x, VOIDmode))
4334 return m68k_symbolic_jump;
4335 else
4336 return "jmp %a0";
4339 #ifdef M68K_TARGET_COFF
4341 /* Output assembly to switch to section NAME with attribute FLAGS. */
4343 static void
4344 m68k_coff_asm_named_section (const char *name, unsigned int flags,
4345 tree decl ATTRIBUTE_UNUSED)
4347 char flagchar;
4349 if (flags & SECTION_WRITE)
4350 flagchar = 'd';
4351 else
4352 flagchar = 'x';
4354 fprintf (asm_out_file, "\t.section\t%s,\"%c\"\n", name, flagchar);
4357 #endif /* M68K_TARGET_COFF */
4359 static void
4360 m68k_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
4361 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
4362 tree function)
4364 rtx this_slot, offset, addr, mem, insn;
4366 /* Pretend to be a post-reload pass while generating rtl. */
4367 reload_completed = 1;
4369 /* The "this" pointer is stored at 4(%sp). */
4370 this_slot = gen_rtx_MEM (Pmode, plus_constant (stack_pointer_rtx, 4));
4372 /* Add DELTA to THIS. */
4373 if (delta != 0)
4375 /* Make the offset a legitimate operand for memory addition. */
4376 offset = GEN_INT (delta);
4377 if ((delta < -8 || delta > 8)
4378 && (TARGET_COLDFIRE || USE_MOVQ (delta)))
4380 emit_move_insn (gen_rtx_REG (Pmode, D0_REG), offset);
4381 offset = gen_rtx_REG (Pmode, D0_REG);
4383 emit_insn (gen_add3_insn (copy_rtx (this_slot),
4384 copy_rtx (this_slot), offset));
4387 /* If needed, add *(*THIS + VCALL_OFFSET) to THIS. */
4388 if (vcall_offset != 0)
4390 /* Set the static chain register to *THIS. */
4391 emit_move_insn (static_chain_rtx, this_slot);
4392 emit_move_insn (static_chain_rtx, gen_rtx_MEM (Pmode, static_chain_rtx));
4394 /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET. */
4395 addr = plus_constant (static_chain_rtx, vcall_offset);
4396 if (!m68k_legitimate_address_p (Pmode, addr, true))
4398 emit_insn (gen_rtx_SET (VOIDmode, static_chain_rtx, addr));
4399 addr = static_chain_rtx;
4402 /* Load the offset into %d0 and add it to THIS. */
4403 emit_move_insn (gen_rtx_REG (Pmode, D0_REG),
4404 gen_rtx_MEM (Pmode, addr));
4405 emit_insn (gen_add3_insn (copy_rtx (this_slot),
4406 copy_rtx (this_slot),
4407 gen_rtx_REG (Pmode, D0_REG)));
4410 /* Jump to the target function. Use a sibcall if direct jumps are
4411 allowed, otherwise load the address into a register first. */
4412 mem = DECL_RTL (function);
4413 if (!sibcall_operand (XEXP (mem, 0), VOIDmode))
4415 gcc_assert (flag_pic);
4417 if (!TARGET_SEP_DATA)
4419 /* Use the static chain register as a temporary (call-clobbered)
4420 GOT pointer for this function. We can use the static chain
4421 register because it isn't live on entry to the thunk. */
4422 SET_REGNO (pic_offset_table_rtx, STATIC_CHAIN_REGNUM);
4423 emit_insn (gen_load_got (pic_offset_table_rtx));
4425 legitimize_pic_address (XEXP (mem, 0), Pmode, static_chain_rtx);
4426 mem = replace_equiv_address (mem, static_chain_rtx);
4428 insn = emit_call_insn (gen_sibcall (mem, const0_rtx));
4429 SIBLING_CALL_P (insn) = 1;
4431 /* Run just enough of rest_of_compilation. */
4432 insn = get_insns ();
4433 split_all_insns_noflow ();
4434 final_start_function (insn, file, 1);
4435 final (insn, file, 1);
4436 final_end_function ();
4438 /* Clean up the vars set above. */
4439 reload_completed = 0;
4441 /* Restore the original PIC register. */
4442 if (flag_pic)
4443 SET_REGNO (pic_offset_table_rtx, PIC_REG);
4444 free_after_compilation (cfun);
4447 /* Worker function for TARGET_STRUCT_VALUE_RTX. */
4449 static rtx
4450 m68k_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
4451 int incoming ATTRIBUTE_UNUSED)
4453 return gen_rtx_REG (Pmode, M68K_STRUCT_VALUE_REGNUM);
4456 /* Return nonzero if register old_reg can be renamed to register new_reg. */
4458 m68k_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
4459 unsigned int new_reg)
4462 /* Interrupt functions can only use registers that have already been
4463 saved by the prologue, even if they would normally be
4464 call-clobbered. */
4466 if ((m68k_get_function_kind (current_function_decl)
4467 == m68k_fk_interrupt_handler)
4468 && !df_regs_ever_live_p (new_reg))
4469 return 0;
4471 return 1;
4474 /* Value is true if hard register REGNO can hold a value of machine-mode
4475 MODE. On the 68000, we let the cpu registers can hold any mode, but
4476 restrict the 68881 registers to floating-point modes. */
4478 bool
4479 m68k_regno_mode_ok (int regno, enum machine_mode mode)
4481 if (DATA_REGNO_P (regno))
4483 /* Data Registers, can hold aggregate if fits in. */
4484 if (regno + GET_MODE_SIZE (mode) / 4 <= 8)
4485 return true;
4487 else if (ADDRESS_REGNO_P (regno))
4489 if (regno + GET_MODE_SIZE (mode) / 4 <= 16)
4490 return true;
4492 else if (FP_REGNO_P (regno))
4494 /* FPU registers, hold float or complex float of long double or
4495 smaller. */
4496 if ((GET_MODE_CLASS (mode) == MODE_FLOAT
4497 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4498 && GET_MODE_UNIT_SIZE (mode) <= TARGET_FP_REG_SIZE)
4499 return true;
4501 return false;
4504 /* Implement SECONDARY_RELOAD_CLASS. */
4506 enum reg_class
4507 m68k_secondary_reload_class (enum reg_class rclass,
4508 enum machine_mode mode, rtx x)
4510 int regno;
4512 regno = true_regnum (x);
4514 /* If one operand of a movqi is an address register, the other
4515 operand must be a general register or constant. Other types
4516 of operand must be reloaded through a data register. */
4517 if (GET_MODE_SIZE (mode) == 1
4518 && reg_classes_intersect_p (rclass, ADDR_REGS)
4519 && !(INT_REGNO_P (regno) || CONSTANT_P (x)))
4520 return DATA_REGS;
4522 /* PC-relative addresses must be loaded into an address register first. */
4523 if (TARGET_PCREL
4524 && !reg_class_subset_p (rclass, ADDR_REGS)
4525 && symbolic_operand (x, VOIDmode))
4526 return ADDR_REGS;
4528 return NO_REGS;
4531 /* Implement PREFERRED_RELOAD_CLASS. */
4533 enum reg_class
4534 m68k_preferred_reload_class (rtx x, enum reg_class rclass)
4536 enum reg_class secondary_class;
4538 /* If RCLASS might need a secondary reload, try restricting it to
4539 a class that doesn't. */
4540 secondary_class = m68k_secondary_reload_class (rclass, GET_MODE (x), x);
4541 if (secondary_class != NO_REGS
4542 && reg_class_subset_p (secondary_class, rclass))
4543 return secondary_class;
4545 /* Prefer to use moveq for in-range constants. */
4546 if (GET_CODE (x) == CONST_INT
4547 && reg_class_subset_p (DATA_REGS, rclass)
4548 && IN_RANGE (INTVAL (x), -0x80, 0x7f))
4549 return DATA_REGS;
4551 /* ??? Do we really need this now? */
4552 if (GET_CODE (x) == CONST_DOUBLE
4553 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
4555 if (TARGET_HARD_FLOAT && reg_class_subset_p (FP_REGS, rclass))
4556 return FP_REGS;
4558 return NO_REGS;
4561 return rclass;
4564 /* Return floating point values in a 68881 register. This makes 68881 code
4565 a little bit faster. It also makes -msoft-float code incompatible with
4566 hard-float code, so people have to be careful not to mix the two.
4567 For ColdFire it was decided the ABI incompatibility is undesirable.
4568 If there is need for a hard-float ABI it is probably worth doing it
4569 properly and also passing function arguments in FP registers. */
4571 m68k_libcall_value (enum machine_mode mode)
4573 switch (mode) {
4574 case SFmode:
4575 case DFmode:
4576 case XFmode:
4577 if (TARGET_68881)
4578 return gen_rtx_REG (mode, FP0_REG);
4579 break;
4580 default:
4581 break;
4583 return gen_rtx_REG (mode, D0_REG);
4587 m68k_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED)
4589 enum machine_mode mode;
4591 mode = TYPE_MODE (valtype);
4592 switch (mode) {
4593 case SFmode:
4594 case DFmode:
4595 case XFmode:
4596 if (TARGET_68881)
4597 return gen_rtx_REG (mode, FP0_REG);
4598 break;
4599 default:
4600 break;
4603 /* If the function returns a pointer, push that into %a0. */
4604 if (func && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (func))))
4605 /* For compatibility with the large body of existing code which
4606 does not always properly declare external functions returning
4607 pointer types, the m68k/SVR4 convention is to copy the value
4608 returned for pointer functions from a0 to d0 in the function
4609 epilogue, so that callers that have neglected to properly
4610 declare the callee can still find the correct return value in
4611 d0. */
4612 return gen_rtx_PARALLEL
4613 (mode,
4614 gen_rtvec (2,
4615 gen_rtx_EXPR_LIST (VOIDmode,
4616 gen_rtx_REG (mode, A0_REG),
4617 const0_rtx),
4618 gen_rtx_EXPR_LIST (VOIDmode,
4619 gen_rtx_REG (mode, D0_REG),
4620 const0_rtx)));
4621 else if (POINTER_TYPE_P (valtype))
4622 return gen_rtx_REG (mode, A0_REG);
4623 else
4624 return gen_rtx_REG (mode, D0_REG);
4627 /* Worker function for TARGET_RETURN_IN_MEMORY. */
4628 #if M68K_HONOR_TARGET_STRICT_ALIGNMENT
4629 static bool
4630 m68k_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
4632 enum machine_mode mode = TYPE_MODE (type);
4634 if (mode == BLKmode)
4635 return true;
4637 /* If TYPE's known alignment is less than the alignment of MODE that
4638 would contain the structure, then return in memory. We need to
4639 do so to maintain the compatibility between code compiled with
4640 -mstrict-align and that compiled with -mno-strict-align. */
4641 if (AGGREGATE_TYPE_P (type)
4642 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (mode))
4643 return true;
4645 return false;
4647 #endif
4649 /* CPU to schedule the program for. */
4650 enum attr_cpu m68k_sched_cpu;
4652 /* MAC to schedule the program for. */
4653 enum attr_mac m68k_sched_mac;
4655 /* Operand type. */
4656 enum attr_op_type
4658 /* No operand. */
4659 OP_TYPE_NONE,
4661 /* Integer register. */
4662 OP_TYPE_RN,
4664 /* FP register. */
4665 OP_TYPE_FPN,
4667 /* Implicit mem reference (e.g. stack). */
4668 OP_TYPE_MEM1,
4670 /* Memory without offset or indexing. EA modes 2, 3 and 4. */
4671 OP_TYPE_MEM234,
4673 /* Memory with offset but without indexing. EA mode 5. */
4674 OP_TYPE_MEM5,
4676 /* Memory with indexing. EA mode 6. */
4677 OP_TYPE_MEM6,
4679 /* Memory referenced by absolute address. EA mode 7. */
4680 OP_TYPE_MEM7,
4682 /* Immediate operand that doesn't require extension word. */
4683 OP_TYPE_IMM_Q,
4685 /* Immediate 16 bit operand. */
4686 OP_TYPE_IMM_W,
4688 /* Immediate 32 bit operand. */
4689 OP_TYPE_IMM_L
4692 /* Return type of memory ADDR_RTX refers to. */
4693 static enum attr_op_type
4694 sched_address_type (enum machine_mode mode, rtx addr_rtx)
4696 struct m68k_address address;
4698 if (symbolic_operand (addr_rtx, VOIDmode))
4699 return OP_TYPE_MEM7;
4701 if (!m68k_decompose_address (mode, addr_rtx,
4702 reload_completed, &address))
4704 gcc_assert (!reload_completed);
4705 /* Reload will likely fix the address to be in the register. */
4706 return OP_TYPE_MEM234;
4709 if (address.scale != 0)
4710 return OP_TYPE_MEM6;
4712 if (address.base != NULL_RTX)
4714 if (address.offset == NULL_RTX)
4715 return OP_TYPE_MEM234;
4717 return OP_TYPE_MEM5;
4720 gcc_assert (address.offset != NULL_RTX);
4722 return OP_TYPE_MEM7;
4725 /* Return X or Y (depending on OPX_P) operand of INSN. */
4726 static rtx
4727 sched_get_operand (rtx insn, bool opx_p)
4729 int i;
4731 if (recog_memoized (insn) < 0)
4732 gcc_unreachable ();
4734 extract_constrain_insn_cached (insn);
4736 if (opx_p)
4737 i = get_attr_opx (insn);
4738 else
4739 i = get_attr_opy (insn);
4741 if (i >= recog_data.n_operands)
4742 return NULL;
4744 return recog_data.operand[i];
4747 /* Return type of INSN's operand X (if OPX_P) or operand Y (if !OPX_P).
4748 If ADDRESS_P is true, return type of memory location operand refers to. */
4749 static enum attr_op_type
4750 sched_attr_op_type (rtx insn, bool opx_p, bool address_p)
4752 rtx op;
4754 op = sched_get_operand (insn, opx_p);
4756 if (op == NULL)
4758 gcc_assert (!reload_completed);
4759 return OP_TYPE_RN;
4762 if (address_p)
4763 return sched_address_type (QImode, op);
4765 if (memory_operand (op, VOIDmode))
4766 return sched_address_type (GET_MODE (op), XEXP (op, 0));
4768 if (register_operand (op, VOIDmode))
4770 if ((!reload_completed && FLOAT_MODE_P (GET_MODE (op)))
4771 || (reload_completed && FP_REG_P (op)))
4772 return OP_TYPE_FPN;
4774 return OP_TYPE_RN;
4777 if (GET_CODE (op) == CONST_INT)
4779 int ival;
4781 ival = INTVAL (op);
4783 /* Check for quick constants. */
4784 switch (get_attr_type (insn))
4786 case TYPE_ALUQ_L:
4787 if (IN_RANGE (ival, 1, 8) || IN_RANGE (ival, -8, -1))
4788 return OP_TYPE_IMM_Q;
4790 gcc_assert (!reload_completed);
4791 break;
4793 case TYPE_MOVEQ_L:
4794 if (USE_MOVQ (ival))
4795 return OP_TYPE_IMM_Q;
4797 gcc_assert (!reload_completed);
4798 break;
4800 case TYPE_MOV3Q_L:
4801 if (valid_mov3q_const (ival))
4802 return OP_TYPE_IMM_Q;
4804 gcc_assert (!reload_completed);
4805 break;
4807 default:
4808 break;
4811 if (IN_RANGE (ival, -0x8000, 0x7fff))
4812 return OP_TYPE_IMM_W;
4814 return OP_TYPE_IMM_L;
4817 if (GET_CODE (op) == CONST_DOUBLE)
4819 switch (GET_MODE (op))
4821 case SFmode:
4822 return OP_TYPE_IMM_W;
4824 case VOIDmode:
4825 case DFmode:
4826 return OP_TYPE_IMM_L;
4828 default:
4829 gcc_unreachable ();
4833 if (symbolic_operand (op, VOIDmode)
4834 || LABEL_P (op))
4836 switch (GET_MODE (op))
4838 case QImode:
4839 return OP_TYPE_IMM_Q;
4841 case HImode:
4842 return OP_TYPE_IMM_W;
4844 case SImode:
4845 return OP_TYPE_IMM_L;
4847 default:
4848 if (GET_CODE (op) == SYMBOL_REF)
4849 /* ??? Just a guess. Probably we can guess better using length
4850 attribute of the instructions. */
4851 return OP_TYPE_IMM_W;
4853 return OP_TYPE_IMM_L;
4857 gcc_assert (!reload_completed);
4859 if (FLOAT_MODE_P (GET_MODE (op)))
4860 return OP_TYPE_FPN;
4862 return OP_TYPE_RN;
4865 /* Implement opx_type attribute.
4866 Return type of INSN's operand X.
4867 If ADDRESS_P is true, return type of memory location operand refers to. */
4868 enum attr_opx_type
4869 m68k_sched_attr_opx_type (rtx insn, int address_p)
4871 switch (sched_attr_op_type (insn, true, address_p != 0))
4873 case OP_TYPE_RN:
4874 return OPX_TYPE_RN;
4876 case OP_TYPE_FPN:
4877 return OPX_TYPE_FPN;
4879 case OP_TYPE_MEM1:
4880 return OPX_TYPE_MEM1;
4882 case OP_TYPE_MEM234:
4883 return OPX_TYPE_MEM234;
4885 case OP_TYPE_MEM5:
4886 return OPX_TYPE_MEM5;
4888 case OP_TYPE_MEM6:
4889 return OPX_TYPE_MEM6;
4891 case OP_TYPE_MEM7:
4892 return OPX_TYPE_MEM7;
4894 case OP_TYPE_IMM_Q:
4895 return OPX_TYPE_IMM_Q;
4897 case OP_TYPE_IMM_W:
4898 return OPX_TYPE_IMM_W;
4900 case OP_TYPE_IMM_L:
4901 return OPX_TYPE_IMM_L;
4903 default:
4904 gcc_unreachable ();
4905 return 0;
4909 /* Implement opy_type attribute.
4910 Return type of INSN's operand Y.
4911 If ADDRESS_P is true, return type of memory location operand refers to. */
4912 enum attr_opy_type
4913 m68k_sched_attr_opy_type (rtx insn, int address_p)
4915 switch (sched_attr_op_type (insn, false, address_p != 0))
4917 case OP_TYPE_RN:
4918 return OPY_TYPE_RN;
4920 case OP_TYPE_FPN:
4921 return OPY_TYPE_FPN;
4923 case OP_TYPE_MEM1:
4924 return OPY_TYPE_MEM1;
4926 case OP_TYPE_MEM234:
4927 return OPY_TYPE_MEM234;
4929 case OP_TYPE_MEM5:
4930 return OPY_TYPE_MEM5;
4932 case OP_TYPE_MEM6:
4933 return OPY_TYPE_MEM6;
4935 case OP_TYPE_MEM7:
4936 return OPY_TYPE_MEM7;
4938 case OP_TYPE_IMM_Q:
4939 return OPY_TYPE_IMM_Q;
4941 case OP_TYPE_IMM_W:
4942 return OPY_TYPE_IMM_W;
4944 case OP_TYPE_IMM_L:
4945 return OPY_TYPE_IMM_L;
4947 default:
4948 gcc_unreachable ();
4949 return 0;
4953 /* Return size of INSN as int. */
4954 static int
4955 sched_get_attr_size_int (rtx insn)
4957 int size;
4959 switch (get_attr_type (insn))
4961 case TYPE_IGNORE:
4962 /* There should be no references to m68k_sched_attr_size for 'ignore'
4963 instructions. */
4964 gcc_unreachable ();
4965 return 0;
4967 case TYPE_MUL_L:
4968 size = 2;
4969 break;
4971 default:
4972 size = 1;
4973 break;
4976 switch (get_attr_opx_type (insn))
4978 case OPX_TYPE_NONE:
4979 case OPX_TYPE_RN:
4980 case OPX_TYPE_FPN:
4981 case OPX_TYPE_MEM1:
4982 case OPX_TYPE_MEM234:
4983 case OPY_TYPE_IMM_Q:
4984 break;
4986 case OPX_TYPE_MEM5:
4987 case OPX_TYPE_MEM6:
4988 /* Here we assume that most absolute references are short. */
4989 case OPX_TYPE_MEM7:
4990 case OPY_TYPE_IMM_W:
4991 ++size;
4992 break;
4994 case OPY_TYPE_IMM_L:
4995 size += 2;
4996 break;
4998 default:
4999 gcc_unreachable ();
5002 switch (get_attr_opy_type (insn))
5004 case OPY_TYPE_NONE:
5005 case OPY_TYPE_RN:
5006 case OPY_TYPE_FPN:
5007 case OPY_TYPE_MEM1:
5008 case OPY_TYPE_MEM234:
5009 case OPY_TYPE_IMM_Q:
5010 break;
5012 case OPY_TYPE_MEM5:
5013 case OPY_TYPE_MEM6:
5014 /* Here we assume that most absolute references are short. */
5015 case OPY_TYPE_MEM7:
5016 case OPY_TYPE_IMM_W:
5017 ++size;
5018 break;
5020 case OPY_TYPE_IMM_L:
5021 size += 2;
5022 break;
5024 default:
5025 gcc_unreachable ();
5028 if (size > 3)
5030 gcc_assert (!reload_completed);
5032 size = 3;
5035 return size;
5038 /* Return size of INSN as attribute enum value. */
5039 enum attr_size
5040 m68k_sched_attr_size (rtx insn)
5042 switch (sched_get_attr_size_int (insn))
5044 case 1:
5045 return SIZE_1;
5047 case 2:
5048 return SIZE_2;
5050 case 3:
5051 return SIZE_3;
5053 default:
5054 gcc_unreachable ();
5055 return 0;
5059 /* Return operand X or Y (depending on OPX_P) of INSN,
5060 if it is a MEM, or NULL overwise. */
5061 static enum attr_op_type
5062 sched_get_opxy_mem_type (rtx insn, bool opx_p)
5064 if (opx_p)
5066 switch (get_attr_opx_type (insn))
5068 case OPX_TYPE_NONE:
5069 case OPX_TYPE_RN:
5070 case OPX_TYPE_FPN:
5071 case OPX_TYPE_IMM_Q:
5072 case OPX_TYPE_IMM_W:
5073 case OPX_TYPE_IMM_L:
5074 return OP_TYPE_RN;
5076 case OPX_TYPE_MEM1:
5077 case OPX_TYPE_MEM234:
5078 case OPX_TYPE_MEM5:
5079 case OPX_TYPE_MEM7:
5080 return OP_TYPE_MEM1;
5082 case OPX_TYPE_MEM6:
5083 return OP_TYPE_MEM6;
5085 default:
5086 gcc_unreachable ();
5087 return 0;
5090 else
5092 switch (get_attr_opy_type (insn))
5094 case OPY_TYPE_NONE:
5095 case OPY_TYPE_RN:
5096 case OPY_TYPE_FPN:
5097 case OPY_TYPE_IMM_Q:
5098 case OPY_TYPE_IMM_W:
5099 case OPY_TYPE_IMM_L:
5100 return OP_TYPE_RN;
5102 case OPY_TYPE_MEM1:
5103 case OPY_TYPE_MEM234:
5104 case OPY_TYPE_MEM5:
5105 case OPY_TYPE_MEM7:
5106 return OP_TYPE_MEM1;
5108 case OPY_TYPE_MEM6:
5109 return OP_TYPE_MEM6;
5111 default:
5112 gcc_unreachable ();
5113 return 0;
5118 /* Implement op_mem attribute. */
5119 enum attr_op_mem
5120 m68k_sched_attr_op_mem (rtx insn)
5122 enum attr_op_type opx;
5123 enum attr_op_type opy;
5125 opx = sched_get_opxy_mem_type (insn, true);
5126 opy = sched_get_opxy_mem_type (insn, false);
5128 if (opy == OP_TYPE_RN && opx == OP_TYPE_RN)
5129 return OP_MEM_00;
5131 if (opy == OP_TYPE_RN && opx == OP_TYPE_MEM1)
5133 switch (get_attr_opx_access (insn))
5135 case OPX_ACCESS_R:
5136 return OP_MEM_10;
5138 case OPX_ACCESS_W:
5139 return OP_MEM_01;
5141 case OPX_ACCESS_RW:
5142 return OP_MEM_11;
5144 default:
5145 gcc_unreachable ();
5146 return 0;
5150 if (opy == OP_TYPE_RN && opx == OP_TYPE_MEM6)
5152 switch (get_attr_opx_access (insn))
5154 case OPX_ACCESS_R:
5155 return OP_MEM_I0;
5157 case OPX_ACCESS_W:
5158 return OP_MEM_0I;
5160 case OPX_ACCESS_RW:
5161 return OP_MEM_I1;
5163 default:
5164 gcc_unreachable ();
5165 return 0;
5169 if (opy == OP_TYPE_MEM1 && opx == OP_TYPE_RN)
5170 return OP_MEM_10;
5172 if (opy == OP_TYPE_MEM1 && opx == OP_TYPE_MEM1)
5174 switch (get_attr_opx_access (insn))
5176 case OPX_ACCESS_W:
5177 return OP_MEM_11;
5179 default:
5180 gcc_assert (!reload_completed);
5181 return OP_MEM_11;
5185 if (opy == OP_TYPE_MEM1 && opx == OP_TYPE_MEM6)
5187 switch (get_attr_opx_access (insn))
5189 case OPX_ACCESS_W:
5190 return OP_MEM_1I;
5192 default:
5193 gcc_assert (!reload_completed);
5194 return OP_MEM_1I;
5198 if (opy == OP_TYPE_MEM6 && opx == OP_TYPE_RN)
5199 return OP_MEM_I0;
5201 if (opy == OP_TYPE_MEM6 && opx == OP_TYPE_MEM1)
5203 switch (get_attr_opx_access (insn))
5205 case OPX_ACCESS_W:
5206 return OP_MEM_I1;
5208 default:
5209 gcc_assert (!reload_completed);
5210 return OP_MEM_I1;
5214 gcc_assert (opy == OP_TYPE_MEM6 && opx == OP_TYPE_MEM6);
5215 gcc_assert (!reload_completed);
5216 return OP_MEM_I1;
5219 /* Jump instructions types. Indexed by INSN_UID.
5220 The same rtl insn can be expanded into different asm instructions
5221 depending on the cc0_status. To properly determine type of jump
5222 instructions we scan instruction stream and map jumps types to this
5223 array. */
5224 static enum attr_type *sched_branch_type;
5226 /* Return the type of the jump insn. */
5227 enum attr_type
5228 m68k_sched_branch_type (rtx insn)
5230 enum attr_type type;
5232 type = sched_branch_type[INSN_UID (insn)];
5234 gcc_assert (type != 0);
5236 return type;
5239 /* Data for ColdFire V4 index bypass.
5240 Producer modifies register that is used as index in consumer with
5241 specified scale. */
5242 static struct
5244 /* Producer instruction. */
5245 rtx pro;
5247 /* Consumer instruction. */
5248 rtx con;
5250 /* Scale of indexed memory access within consumer.
5251 Or zero if bypass should not be effective at the moment. */
5252 int scale;
5253 } sched_cfv4_bypass_data;
5255 /* An empty state that is used in m68k_sched_adjust_cost. */
5256 static state_t sched_adjust_cost_state;
5258 /* Implement adjust_cost scheduler hook.
5259 Return adjusted COST of dependency LINK between DEF_INSN and INSN. */
5260 static int
5261 m68k_sched_adjust_cost (rtx insn, rtx link ATTRIBUTE_UNUSED, rtx def_insn,
5262 int cost)
5264 int delay;
5266 if (recog_memoized (def_insn) < 0
5267 || recog_memoized (insn) < 0)
5268 return cost;
5270 if (sched_cfv4_bypass_data.scale == 1)
5271 /* Handle ColdFire V4 bypass for indexed address with 1x scale. */
5273 /* haifa-sched.c: insn_cost () calls bypass_p () just before
5274 targetm.sched.adjust_cost (). Hence, we can be relatively sure
5275 that the data in sched_cfv4_bypass_data is up to date. */
5276 gcc_assert (sched_cfv4_bypass_data.pro == def_insn
5277 && sched_cfv4_bypass_data.con == insn);
5279 if (cost < 3)
5280 cost = 3;
5282 sched_cfv4_bypass_data.pro = NULL;
5283 sched_cfv4_bypass_data.con = NULL;
5284 sched_cfv4_bypass_data.scale = 0;
5286 else
5287 gcc_assert (sched_cfv4_bypass_data.pro == NULL
5288 && sched_cfv4_bypass_data.con == NULL
5289 && sched_cfv4_bypass_data.scale == 0);
5291 /* Don't try to issue INSN earlier than DFA permits.
5292 This is especially useful for instructions that write to memory,
5293 as their true dependence (default) latency is better to be set to 0
5294 to workaround alias analysis limitations.
5295 This is, in fact, a machine independent tweak, so, probably,
5296 it should be moved to haifa-sched.c: insn_cost (). */
5297 delay = min_insn_conflict_delay (sched_adjust_cost_state, def_insn, insn);
5298 if (delay > cost)
5299 cost = delay;
5301 return cost;
5304 /* Return maximal number of insns that can be scheduled on a single cycle. */
5305 static int
5306 m68k_sched_issue_rate (void)
5308 switch (m68k_sched_cpu)
5310 case CPU_CFV1:
5311 case CPU_CFV2:
5312 case CPU_CFV3:
5313 return 1;
5315 case CPU_CFV4:
5316 return 2;
5318 default:
5319 gcc_unreachable ();
5320 return 0;
5324 /* Maximal length of instruction for current CPU.
5325 E.g. it is 3 for any ColdFire core. */
5326 static int max_insn_size;
5328 /* Data to model instruction buffer of CPU. */
5329 struct _sched_ib
5331 /* True if instruction buffer model is modeled for current CPU. */
5332 bool enabled_p;
5334 /* Size of the instruction buffer in words. */
5335 int size;
5337 /* Number of filled words in the instruction buffer. */
5338 int filled;
5340 /* Additional information about instruction buffer for CPUs that have
5341 a buffer of instruction records, rather then a plain buffer
5342 of instruction words. */
5343 struct _sched_ib_records
5345 /* Size of buffer in records. */
5346 int n_insns;
5348 /* Array to hold data on adjustements made to the size of the buffer. */
5349 int *adjust;
5351 /* Index of the above array. */
5352 int adjust_index;
5353 } records;
5355 /* An insn that reserves (marks empty) one word in the instruction buffer. */
5356 rtx insn;
5359 static struct _sched_ib sched_ib;
5361 /* ID of memory unit. */
5362 static int sched_mem_unit_code;
5364 /* Implementation of the targetm.sched.variable_issue () hook.
5365 It is called after INSN was issued. It returns the number of insns
5366 that can possibly get scheduled on the current cycle.
5367 It is used here to determine the effect of INSN on the instruction
5368 buffer. */
5369 static int
5370 m68k_sched_variable_issue (FILE *sched_dump ATTRIBUTE_UNUSED,
5371 int sched_verbose ATTRIBUTE_UNUSED,
5372 rtx insn, int can_issue_more)
5374 int insn_size;
5376 if (recog_memoized (insn) >= 0 && get_attr_type (insn) != TYPE_IGNORE)
5378 switch (m68k_sched_cpu)
5380 case CPU_CFV1:
5381 case CPU_CFV2:
5382 insn_size = sched_get_attr_size_int (insn);
5383 break;
5385 case CPU_CFV3:
5386 insn_size = sched_get_attr_size_int (insn);
5388 /* ColdFire V3 and V4 cores have instruction buffers that can
5389 accumulate up to 8 instructions regardless of instructions'
5390 sizes. So we should take care not to "prefetch" 24 one-word
5391 or 12 two-words instructions.
5392 To model this behavior we temporarily decrease size of the
5393 buffer by (max_insn_size - insn_size) for next 7 instructions. */
5395 int adjust;
5397 adjust = max_insn_size - insn_size;
5398 sched_ib.size -= adjust;
5400 if (sched_ib.filled > sched_ib.size)
5401 sched_ib.filled = sched_ib.size;
5403 sched_ib.records.adjust[sched_ib.records.adjust_index] = adjust;
5406 ++sched_ib.records.adjust_index;
5407 if (sched_ib.records.adjust_index == sched_ib.records.n_insns)
5408 sched_ib.records.adjust_index = 0;
5410 /* Undo adjustement we did 7 instructions ago. */
5411 sched_ib.size
5412 += sched_ib.records.adjust[sched_ib.records.adjust_index];
5414 break;
5416 case CPU_CFV4:
5417 gcc_assert (!sched_ib.enabled_p);
5418 insn_size = 0;
5419 break;
5421 default:
5422 gcc_unreachable ();
5425 gcc_assert (insn_size <= sched_ib.filled);
5426 --can_issue_more;
5428 else if (GET_CODE (PATTERN (insn)) == ASM_INPUT
5429 || asm_noperands (PATTERN (insn)) >= 0)
5430 insn_size = sched_ib.filled;
5431 else
5432 insn_size = 0;
5434 sched_ib.filled -= insn_size;
5436 return can_issue_more;
5439 /* Return how many instructions should scheduler lookahead to choose the
5440 best one. */
5441 static int
5442 m68k_sched_first_cycle_multipass_dfa_lookahead (void)
5444 return m68k_sched_issue_rate () - 1;
5447 /* Implementation of targetm.sched.md_init_global () hook.
5448 It is invoked once per scheduling pass and is used here
5449 to initialize scheduler constants. */
5450 static void
5451 m68k_sched_md_init_global (FILE *sched_dump ATTRIBUTE_UNUSED,
5452 int sched_verbose ATTRIBUTE_UNUSED,
5453 int n_insns ATTRIBUTE_UNUSED)
5455 /* Init branch types. */
5457 rtx insn;
5459 sched_branch_type = XCNEWVEC (enum attr_type, get_max_uid () + 1);
5461 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
5463 if (JUMP_P (insn))
5464 /* !!! FIXME: Implement real scan here. */
5465 sched_branch_type[INSN_UID (insn)] = TYPE_BCC;
5469 #ifdef ENABLE_CHECKING
5470 /* Check that all instructions have DFA reservations and
5471 that all instructions can be issued from a clean state. */
5473 rtx insn;
5474 state_t state;
5476 state = alloca (state_size ());
5478 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
5480 if (INSN_P (insn) && recog_memoized (insn) >= 0)
5482 gcc_assert (insn_has_dfa_reservation_p (insn));
5484 state_reset (state);
5485 if (state_transition (state, insn) >= 0)
5486 gcc_unreachable ();
5490 #endif
5492 /* Setup target cpu. */
5494 /* ColdFire V4 has a set of features to keep its instruction buffer full
5495 (e.g., a separate memory bus for instructions) and, hence, we do not model
5496 buffer for this CPU. */
5497 sched_ib.enabled_p = (m68k_sched_cpu != CPU_CFV4);
5499 switch (m68k_sched_cpu)
5501 case CPU_CFV4:
5502 sched_ib.filled = 0;
5504 /* FALLTHRU */
5506 case CPU_CFV1:
5507 case CPU_CFV2:
5508 max_insn_size = 3;
5509 sched_ib.records.n_insns = 0;
5510 sched_ib.records.adjust = NULL;
5511 break;
5513 case CPU_CFV3:
5514 max_insn_size = 3;
5515 sched_ib.records.n_insns = 8;
5516 sched_ib.records.adjust = XNEWVEC (int, sched_ib.records.n_insns);
5517 break;
5519 default:
5520 gcc_unreachable ();
5523 sched_mem_unit_code = get_cpu_unit_code ("cf_mem1");
5525 sched_adjust_cost_state = xmalloc (state_size ());
5526 state_reset (sched_adjust_cost_state);
5528 start_sequence ();
5529 emit_insn (gen_ib ());
5530 sched_ib.insn = get_insns ();
5531 end_sequence ();
5534 /* Scheduling pass is now finished. Free/reset static variables. */
5535 static void
5536 m68k_sched_md_finish_global (FILE *dump ATTRIBUTE_UNUSED,
5537 int verbose ATTRIBUTE_UNUSED)
5539 sched_ib.insn = NULL;
5541 free (sched_adjust_cost_state);
5542 sched_adjust_cost_state = NULL;
5544 sched_mem_unit_code = 0;
5546 free (sched_ib.records.adjust);
5547 sched_ib.records.adjust = NULL;
5548 sched_ib.records.n_insns = 0;
5549 max_insn_size = 0;
5551 free (sched_branch_type);
5552 sched_branch_type = NULL;
5555 /* Implementation of targetm.sched.md_init () hook.
5556 It is invoked each time scheduler starts on the new block (basic block or
5557 extended basic block). */
5558 static void
5559 m68k_sched_md_init (FILE *sched_dump ATTRIBUTE_UNUSED,
5560 int sched_verbose ATTRIBUTE_UNUSED,
5561 int n_insns ATTRIBUTE_UNUSED)
5563 switch (m68k_sched_cpu)
5565 case CPU_CFV1:
5566 case CPU_CFV2:
5567 sched_ib.size = 6;
5568 break;
5570 case CPU_CFV3:
5571 sched_ib.size = sched_ib.records.n_insns * max_insn_size;
5573 memset (sched_ib.records.adjust, 0,
5574 sched_ib.records.n_insns * sizeof (*sched_ib.records.adjust));
5575 sched_ib.records.adjust_index = 0;
5576 break;
5578 case CPU_CFV4:
5579 gcc_assert (!sched_ib.enabled_p);
5580 sched_ib.size = 0;
5581 break;
5583 default:
5584 gcc_unreachable ();
5587 if (sched_ib.enabled_p)
5588 /* haifa-sched.c: schedule_block () calls advance_cycle () just before
5589 the first cycle. Workaround that. */
5590 sched_ib.filled = -2;
5593 /* Implementation of targetm.sched.dfa_pre_advance_cycle () hook.
5594 It is invoked just before current cycle finishes and is used here
5595 to track if instruction buffer got its two words this cycle. */
5596 static void
5597 m68k_sched_dfa_pre_advance_cycle (void)
5599 if (!sched_ib.enabled_p)
5600 return;
5602 if (!cpu_unit_reservation_p (curr_state, sched_mem_unit_code))
5604 sched_ib.filled += 2;
5606 if (sched_ib.filled > sched_ib.size)
5607 sched_ib.filled = sched_ib.size;
5611 /* Implementation of targetm.sched.dfa_post_advance_cycle () hook.
5612 It is invoked just after new cycle begins and is used here
5613 to setup number of filled words in the instruction buffer so that
5614 instructions which won't have all their words prefetched would be
5615 stalled for a cycle. */
5616 static void
5617 m68k_sched_dfa_post_advance_cycle (void)
5619 int i;
5621 if (!sched_ib.enabled_p)
5622 return;
5624 /* Setup number of prefetched instruction words in the instruction
5625 buffer. */
5626 i = max_insn_size - sched_ib.filled;
5628 while (--i >= 0)
5630 if (state_transition (curr_state, sched_ib.insn) >= 0)
5631 gcc_unreachable ();
5635 /* Return X or Y (depending on OPX_P) operand of INSN,
5636 if it is an integer register, or NULL overwise. */
5637 static rtx
5638 sched_get_reg_operand (rtx insn, bool opx_p)
5640 rtx op = NULL;
5642 if (opx_p)
5644 if (get_attr_opx_type (insn) == OPX_TYPE_RN)
5646 op = sched_get_operand (insn, true);
5647 gcc_assert (op != NULL);
5649 if (!reload_completed && !REG_P (op))
5650 return NULL;
5653 else
5655 if (get_attr_opy_type (insn) == OPY_TYPE_RN)
5657 op = sched_get_operand (insn, false);
5658 gcc_assert (op != NULL);
5660 if (!reload_completed && !REG_P (op))
5661 return NULL;
5665 return op;
5668 /* Return true, if X or Y (depending on OPX_P) operand of INSN
5669 is a MEM. */
5670 static bool
5671 sched_mem_operand_p (rtx insn, bool opx_p)
5673 switch (sched_get_opxy_mem_type (insn, opx_p))
5675 case OP_TYPE_MEM1:
5676 case OP_TYPE_MEM6:
5677 return true;
5679 default:
5680 return false;
5684 /* Return X or Y (depending on OPX_P) operand of INSN,
5685 if it is a MEM, or NULL overwise. */
5686 static rtx
5687 sched_get_mem_operand (rtx insn, bool must_read_p, bool must_write_p)
5689 bool opx_p;
5690 bool opy_p;
5692 opx_p = false;
5693 opy_p = false;
5695 if (must_read_p)
5697 opx_p = true;
5698 opy_p = true;
5701 if (must_write_p)
5703 opx_p = true;
5704 opy_p = false;
5707 if (opy_p && sched_mem_operand_p (insn, false))
5708 return sched_get_operand (insn, false);
5710 if (opx_p && sched_mem_operand_p (insn, true))
5711 return sched_get_operand (insn, true);
5713 gcc_unreachable ();
5714 return NULL;
5717 /* Return non-zero if PRO modifies register used as part of
5718 address in CON. */
5720 m68k_sched_address_bypass_p (rtx pro, rtx con)
5722 rtx pro_x;
5723 rtx con_mem_read;
5725 pro_x = sched_get_reg_operand (pro, true);
5726 if (pro_x == NULL)
5727 return 0;
5729 con_mem_read = sched_get_mem_operand (con, true, false);
5730 gcc_assert (con_mem_read != NULL);
5732 if (reg_mentioned_p (pro_x, con_mem_read))
5733 return 1;
5735 return 0;
5738 /* Helper function for m68k_sched_indexed_address_bypass_p.
5739 if PRO modifies register used as index in CON,
5740 return scale of indexed memory access in CON. Return zero overwise. */
5741 static int
5742 sched_get_indexed_address_scale (rtx pro, rtx con)
5744 rtx reg;
5745 rtx mem;
5746 struct m68k_address address;
5748 reg = sched_get_reg_operand (pro, true);
5749 if (reg == NULL)
5750 return 0;
5752 mem = sched_get_mem_operand (con, true, false);
5753 gcc_assert (mem != NULL && MEM_P (mem));
5755 if (!m68k_decompose_address (GET_MODE (mem), XEXP (mem, 0), reload_completed,
5756 &address))
5757 gcc_unreachable ();
5759 if (REGNO (reg) == REGNO (address.index))
5761 gcc_assert (address.scale != 0);
5762 return address.scale;
5765 return 0;
5768 /* Return non-zero if PRO modifies register used
5769 as index with scale 2 or 4 in CON. */
5771 m68k_sched_indexed_address_bypass_p (rtx pro, rtx con)
5773 gcc_assert (sched_cfv4_bypass_data.pro == NULL
5774 && sched_cfv4_bypass_data.con == NULL
5775 && sched_cfv4_bypass_data.scale == 0);
5777 switch (sched_get_indexed_address_scale (pro, con))
5779 case 1:
5780 /* We can't have a variable latency bypass, so
5781 remember to adjust the insn cost in adjust_cost hook. */
5782 sched_cfv4_bypass_data.pro = pro;
5783 sched_cfv4_bypass_data.con = con;
5784 sched_cfv4_bypass_data.scale = 1;
5785 return 0;
5787 case 2:
5788 case 4:
5789 return 1;
5791 default:
5792 return 0;