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