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