]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/mn10300/mn10300.c
* config/i386/i386.md ("*movtf_internal): Penalize moves to and
[thirdparty/gcc.git] / gcc / config / mn10300 / mn10300.c
CommitLineData
29a404f9 1/* Subroutines for insn-output.c for Matsushita MN10300 series
978b9403 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6e31cc90 3 Free Software Foundation, Inc.
29a404f9 4 Contributed by Jeff Law (law@cygnus.com).
5
187b36cf 6This file is part of GCC.
29a404f9 7
187b36cf 8GCC is free software; you can redistribute it and/or modify
29a404f9 9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
187b36cf 13GCC is distributed in the hope that it will be useful,
29a404f9 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
187b36cf 19along with GCC; see the file COPYING. If not, write to
dbddc6c4 20the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
29a404f9 22
29a404f9 23#include "config.h"
7014838c 24#include "system.h"
805e22b2 25#include "coretypes.h"
26#include "tm.h"
29a404f9 27#include "rtl.h"
4faf81b8 28#include "tree.h"
29a404f9 29#include "regs.h"
30#include "hard-reg-set.h"
31#include "real.h"
32#include "insn-config.h"
33#include "conditions.h"
29a404f9 34#include "output.h"
35#include "insn-attr.h"
36#include "flags.h"
37#include "recog.h"
38#include "expr.h"
d8fc4d0b 39#include "optabs.h"
4faf81b8 40#include "function.h"
29a404f9 41#include "obstack.h"
59086782 42#include "toplev.h"
43#include "tm_p.h"
a767736d 44#include "target.h"
45#include "target-def.h"
29a404f9 46
b87a151a 47/* This is used by GOTaddr2picreg to uniquely identify
48 UNSPEC_INT_LABELs. */
49int mn10300_unspec_int_label_counter;
50
1acdfc69 51/* This is used in the am33_2.0-linux-gnu port, in which global symbol
52 names are not prefixed by underscores, to tell whether to prefix a
53 label with a plus sign or not, so that the assembler can tell
54 symbol names from register names. */
55int mn10300_protect_label;
56
8c2c40c5 57/* The selected processor. */
58enum processor_type mn10300_processor = PROCESSOR_DEFAULT;
59
8ecf154e 60/* The size of the callee register save area. Right now we save everything
61 on entry since it costs us nothing in code size. It does cost us from a
62 speed standpoint, so we want to optimize this sooner or later. */
3bcd7b00 63#define REG_SAVE_BYTES (4 * regs_ever_live[2] \
64 + 4 * regs_ever_live[3] \
911517ac 65 + 4 * regs_ever_live[6] \
66 + 4 * regs_ever_live[7] \
67 + 16 * (regs_ever_live[14] || regs_ever_live[15] \
68 || regs_ever_live[16] || regs_ever_live[17]))
fab7adbf 69
70
8c2c40c5 71static bool mn10300_handle_option (size_t, const char *, int);
0792f2cf 72static int mn10300_address_cost_1 (rtx, int *);
73static int mn10300_address_cost (rtx);
74static bool mn10300_rtx_costs (rtx, int, int, int *);
75static void mn10300_file_start (void);
f2d49d02 76static bool mn10300_return_in_memory (tree, tree);
77static rtx mn10300_builtin_saveregs (void);
b981d932 78static bool mn10300_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
79 tree, bool);
f054eb3c 80static int mn10300_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
81 tree, bool);
a767736d 82\f
83/* Initialize the GCC target structure. */
58356836 84#undef TARGET_ASM_ALIGNED_HI_OP
85#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
8ecf154e 86
fab7adbf 87#undef TARGET_RTX_COSTS
88#define TARGET_RTX_COSTS mn10300_rtx_costs
ec0457a8 89#undef TARGET_ADDRESS_COST
90#define TARGET_ADDRESS_COST mn10300_address_cost
fab7adbf 91
92c473b8 92#undef TARGET_ASM_FILE_START
93#define TARGET_ASM_FILE_START mn10300_file_start
94#undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
95#define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
96
8c2c40c5 97#undef TARGET_DEFAULT_TARGET_FLAGS
00b1da0e 98#define TARGET_DEFAULT_TARGET_FLAGS MASK_MULT_BUG | MASK_PTR_A0D0
8c2c40c5 99#undef TARGET_HANDLE_OPTION
100#define TARGET_HANDLE_OPTION mn10300_handle_option
101
b87a151a 102#undef TARGET_ENCODE_SECTION_INFO
103#define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
104
f2d49d02 105#undef TARGET_PROMOTE_PROTOTYPES
106#define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
f2d49d02 107#undef TARGET_RETURN_IN_MEMORY
108#define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
b981d932 109#undef TARGET_PASS_BY_REFERENCE
110#define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
13f08ee7 111#undef TARGET_CALLEE_COPIES
112#define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
f054eb3c 113#undef TARGET_ARG_PARTIAL_BYTES
114#define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
f2d49d02 115
116#undef TARGET_EXPAND_BUILTIN_SAVEREGS
117#define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
118
b87a151a 119static void mn10300_encode_section_info (tree, rtx, int);
57e4bbfb 120struct gcc_target targetm = TARGET_INITIALIZER;
a767736d 121\f
8c2c40c5 122/* Implement TARGET_HANDLE_OPTION. */
123
124static bool
125mn10300_handle_option (size_t code,
126 const char *arg ATTRIBUTE_UNUSED,
127 int value)
128{
129 switch (code)
130 {
131 case OPT_mam33:
132 mn10300_processor = value ? PROCESSOR_AM33 : PROCESSOR_MN10300;
133 return true;
134 case OPT_mam33_2:
135 mn10300_processor = (value
136 ? PROCESSOR_AM33_2
137 : MIN (PROCESSOR_AM33, PROCESSOR_DEFAULT));
138 return true;
139 default:
140 return true;
141 }
142}
143
144/* Implement OVERRIDE_OPTIONS. */
145
146void
147mn10300_override_options (void)
148{
149 if (TARGET_AM33)
150 target_flags &= ~MASK_MULT_BUG;
151}
152
92c473b8 153static void
3285410a 154mn10300_file_start (void)
29a404f9 155{
92c473b8 156 default_file_start ();
911517ac 157
b166356e 158 if (TARGET_AM33_2)
159 fprintf (asm_out_file, "\t.am33_2\n");
160 else if (TARGET_AM33)
92c473b8 161 fprintf (asm_out_file, "\t.am33\n");
29a404f9 162}
163\f
164
29a404f9 165/* Print operand X using operand code CODE to assembly language output file
166 FILE. */
167
168void
3285410a 169print_operand (FILE *file, rtx x, int code)
29a404f9 170{
171 switch (code)
172 {
173 case 'b':
174 case 'B':
b166356e 175 if (cc_status.mdep.fpCC)
176 {
177 switch (code == 'b' ? GET_CODE (x)
178 : reverse_condition_maybe_unordered (GET_CODE (x)))
179 {
180 case NE:
181 fprintf (file, "ne");
182 break;
183 case EQ:
184 fprintf (file, "eq");
185 break;
186 case GE:
187 fprintf (file, "ge");
188 break;
189 case GT:
190 fprintf (file, "gt");
191 break;
192 case LE:
193 fprintf (file, "le");
194 break;
195 case LT:
196 fprintf (file, "lt");
197 break;
198 case ORDERED:
199 fprintf (file, "lge");
200 break;
201 case UNORDERED:
202 fprintf (file, "uo");
203 break;
204 case LTGT:
205 fprintf (file, "lg");
206 break;
207 case UNEQ:
208 fprintf (file, "ue");
209 break;
210 case UNGE:
211 fprintf (file, "uge");
212 break;
213 case UNGT:
214 fprintf (file, "ug");
215 break;
216 case UNLE:
217 fprintf (file, "ule");
218 break;
219 case UNLT:
220 fprintf (file, "ul");
221 break;
222 default:
cf41bb03 223 gcc_unreachable ();
b166356e 224 }
225 break;
226 }
29a404f9 227 /* These are normal and reversed branches. */
228 switch (code == 'b' ? GET_CODE (x) : reverse_condition (GET_CODE (x)))
229 {
230 case NE:
231 fprintf (file, "ne");
232 break;
233 case EQ:
234 fprintf (file, "eq");
235 break;
236 case GE:
237 fprintf (file, "ge");
238 break;
239 case GT:
240 fprintf (file, "gt");
241 break;
242 case LE:
243 fprintf (file, "le");
244 break;
245 case LT:
246 fprintf (file, "lt");
247 break;
248 case GEU:
249 fprintf (file, "cc");
250 break;
251 case GTU:
252 fprintf (file, "hi");
253 break;
254 case LEU:
255 fprintf (file, "ls");
256 break;
257 case LTU:
258 fprintf (file, "cs");
259 break;
260 default:
cf41bb03 261 gcc_unreachable ();
29a404f9 262 }
263 break;
264 case 'C':
265 /* This is used for the operand to a call instruction;
266 if it's a REG, enclose it in parens, else output
267 the operand normally. */
268 if (GET_CODE (x) == REG)
269 {
270 fputc ('(', file);
271 print_operand (file, x, 0);
272 fputc (')', file);
273 }
274 else
275 print_operand (file, x, 0);
276 break;
fb16c776 277
b166356e 278 case 'D':
279 switch (GET_CODE (x))
280 {
281 case MEM:
282 fputc ('(', file);
283 output_address (XEXP (x, 0));
284 fputc (')', file);
285 break;
286
287 case REG:
288 fprintf (file, "fd%d", REGNO (x) - 18);
289 break;
290
291 default:
cf41bb03 292 gcc_unreachable ();
b166356e 293 }
294 break;
295
6ce19398 296 /* These are the least significant word in a 64bit value. */
297 case 'L':
298 switch (GET_CODE (x))
299 {
300 case MEM:
301 fputc ('(', file);
302 output_address (XEXP (x, 0));
303 fputc (')', file);
304 break;
305
306 case REG:
307 fprintf (file, "%s", reg_names[REGNO (x)]);
308 break;
309
310 case SUBREG:
701e46d0 311 fprintf (file, "%s", reg_names[subreg_regno (x)]);
6ce19398 312 break;
313
314 case CONST_DOUBLE:
315 {
316 long val[2];
317 REAL_VALUE_TYPE rv;
318
319 switch (GET_MODE (x))
320 {
321 case DFmode:
322 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
323 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
ad3c0f03 324 fprintf (file, "0x%lx", val[0]);
6ce19398 325 break;;
326 case SFmode:
327 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
328 REAL_VALUE_TO_TARGET_SINGLE (rv, val[0]);
ad3c0f03 329 fprintf (file, "0x%lx", val[0]);
6ce19398 330 break;;
331 case VOIDmode:
332 case DImode:
333 print_operand_address (file,
334 GEN_INT (CONST_DOUBLE_LOW (x)));
335 break;
59086782 336 default:
337 break;
6ce19398 338 }
339 break;
340 }
341
342 case CONST_INT:
964d057c 343 {
344 rtx low, high;
345 split_double (x, &low, &high);
346 fprintf (file, "%ld", (long)INTVAL (low));
347 break;
348 }
6ce19398 349
350 default:
cf41bb03 351 gcc_unreachable ();
6ce19398 352 }
353 break;
354
355 /* Similarly, but for the most significant word. */
356 case 'H':
357 switch (GET_CODE (x))
358 {
359 case MEM:
360 fputc ('(', file);
eafc6604 361 x = adjust_address (x, SImode, 4);
6ce19398 362 output_address (XEXP (x, 0));
363 fputc (')', file);
364 break;
365
366 case REG:
367 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
368 break;
369
370 case SUBREG:
701e46d0 371 fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
6ce19398 372 break;
373
374 case CONST_DOUBLE:
375 {
376 long val[2];
377 REAL_VALUE_TYPE rv;
378
379 switch (GET_MODE (x))
380 {
381 case DFmode:
382 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
383 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
ad3c0f03 384 fprintf (file, "0x%lx", val[1]);
6ce19398 385 break;;
386 case SFmode:
cf41bb03 387 gcc_unreachable ();
6ce19398 388 case VOIDmode:
389 case DImode:
fb16c776 390 print_operand_address (file,
6ce19398 391 GEN_INT (CONST_DOUBLE_HIGH (x)));
392 break;
59086782 393 default:
394 break;
6ce19398 395 }
396 break;
397 }
398
399 case CONST_INT:
964d057c 400 {
401 rtx low, high;
402 split_double (x, &low, &high);
403 fprintf (file, "%ld", (long)INTVAL (high));
404 break;
405 }
406
6ce19398 407 default:
cf41bb03 408 gcc_unreachable ();
6ce19398 409 }
410 break;
411
412 case 'A':
413 fputc ('(', file);
414 if (GET_CODE (XEXP (x, 0)) == REG)
bcd9bd66 415 output_address (gen_rtx_PLUS (SImode, XEXP (x, 0), const0_rtx));
6ce19398 416 else
417 output_address (XEXP (x, 0));
418 fputc (')', file);
419 break;
420
167fa942 421 case 'N':
cf41bb03 422 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
058f71f0 423 fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
424 break;
425
426 case 'U':
cf41bb03 427 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
058f71f0 428 fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
167fa942 429 break;
430
63e678f2 431 /* For shift counts. The hardware ignores the upper bits of
432 any immediate, but the assembler will flag an out of range
433 shift count as an error. So we mask off the high bits
434 of the immediate here. */
435 case 'S':
436 if (GET_CODE (x) == CONST_INT)
437 {
058f71f0 438 fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
63e678f2 439 break;
440 }
441 /* FALL THROUGH */
442
29a404f9 443 default:
444 switch (GET_CODE (x))
445 {
446 case MEM:
447 fputc ('(', file);
448 output_address (XEXP (x, 0));
449 fputc (')', file);
450 break;
451
6ce19398 452 case PLUS:
453 output_address (x);
454 break;
455
29a404f9 456 case REG:
457 fprintf (file, "%s", reg_names[REGNO (x)]);
458 break;
459
460 case SUBREG:
701e46d0 461 fprintf (file, "%s", reg_names[subreg_regno (x)]);
29a404f9 462 break;
463
6ce19398 464 /* This will only be single precision.... */
465 case CONST_DOUBLE:
466 {
467 unsigned long val;
468 REAL_VALUE_TYPE rv;
469
470 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
471 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
ad3c0f03 472 fprintf (file, "0x%lx", val);
6ce19398 473 break;
474 }
475
29a404f9 476 case CONST_INT:
477 case SYMBOL_REF:
478 case CONST:
479 case LABEL_REF:
480 case CODE_LABEL:
b87a151a 481 case UNSPEC:
29a404f9 482 print_operand_address (file, x);
483 break;
484 default:
cf41bb03 485 gcc_unreachable ();
29a404f9 486 }
487 break;
488 }
489}
490
491/* Output assembly language output for the address ADDR to FILE. */
492
493void
3285410a 494print_operand_address (FILE *file, rtx addr)
29a404f9 495{
496 switch (GET_CODE (addr))
497 {
911517ac 498 case POST_INC:
499 print_operand_address (file, XEXP (addr, 0));
500 fputc ('+', file);
501 break;
29a404f9 502 case REG:
49fd1b62 503 print_operand (file, addr, 0);
29a404f9 504 break;
505 case PLUS:
506 {
507 rtx base, index;
508 if (REG_P (XEXP (addr, 0))
509 && REG_OK_FOR_BASE_P (XEXP (addr, 0)))
510 base = XEXP (addr, 0), index = XEXP (addr, 1);
511 else if (REG_P (XEXP (addr, 1))
512 && REG_OK_FOR_BASE_P (XEXP (addr, 1)))
513 base = XEXP (addr, 1), index = XEXP (addr, 0);
514 else
cf41bb03 515 gcc_unreachable ();
29a404f9 516 print_operand (file, index, 0);
517 fputc (',', file);
518 print_operand (file, base, 0);;
519 break;
520 }
521 case SYMBOL_REF:
522 output_addr_const (file, addr);
523 break;
524 default:
525 output_addr_const (file, addr);
526 break;
527 }
528}
529
b166356e 530/* Count the number of FP registers that have to be saved. */
531static int
3285410a 532fp_regs_to_save (void)
b166356e 533{
534 int i, n = 0;
535
536 if (! TARGET_AM33_2)
537 return 0;
538
539 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
540 if (regs_ever_live[i] && ! call_used_regs[i])
541 ++n;
542
543 return n;
544}
545
4caa3669 546/* Print a set of registers in the format required by "movm" and "ret".
547 Register K is saved if bit K of MASK is set. The data and address
548 registers can be stored individually, but the extended registers cannot.
f2b32076 549 We assume that the mask already takes that into account. For instance,
09e5ce26 550 bits 14 to 17 must have the same value. */
4caa3669 551
552void
3285410a 553mn10300_print_reg_list (FILE *file, int mask)
4caa3669 554{
555 int need_comma;
556 int i;
557
558 need_comma = 0;
559 fputc ('[', file);
560
561 for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
562 if ((mask & (1 << i)) != 0)
563 {
564 if (need_comma)
565 fputc (',', file);
566 fputs (reg_names [i], file);
567 need_comma = 1;
568 }
569
570 if ((mask & 0x3c000) != 0)
571 {
cf41bb03 572 gcc_assert ((mask & 0x3c000) == 0x3c000);
4caa3669 573 if (need_comma)
574 fputc (',', file);
575 fputs ("exreg1", file);
576 need_comma = 1;
577 }
578
579 fputc (']', file);
580}
581
6ce19398 582int
3285410a 583can_use_return_insn (void)
6ce19398 584{
36ed4406 585 /* size includes the fixed stack space needed for function calls. */
586 int size = get_frame_size () + current_function_outgoing_args_size;
587
588 /* And space for the return pointer. */
589 size += current_function_outgoing_args_size ? 4 : 0;
6ce19398 590
591 return (reload_completed
592 && size == 0
593 && !regs_ever_live[2]
594 && !regs_ever_live[3]
595 && !regs_ever_live[6]
596 && !regs_ever_live[7]
911517ac 597 && !regs_ever_live[14]
598 && !regs_ever_live[15]
599 && !regs_ever_live[16]
600 && !regs_ever_live[17]
b166356e 601 && fp_regs_to_save () == 0
6ce19398 602 && !frame_pointer_needed);
603}
604
4caa3669 605/* Returns the set of live, callee-saved registers as a bitmask. The
606 callee-saved extended registers cannot be stored individually, so
09e5ce26 607 all of them will be included in the mask if any one of them is used. */
4caa3669 608
609int
3285410a 610mn10300_get_live_callee_saved_regs (void)
4caa3669 611{
612 int mask;
613 int i;
614
615 mask = 0;
b166356e 616 for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
4caa3669 617 if (regs_ever_live[i] && ! call_used_regs[i])
618 mask |= (1 << i);
619 if ((mask & 0x3c000) != 0)
620 mask |= 0x3c000;
621
622 return mask;
623}
624
625/* Generate an instruction that pushes several registers onto the stack.
626 Register K will be saved if bit K in MASK is set. The function does
627 nothing if MASK is zero.
628
629 To be compatible with the "movm" instruction, the lowest-numbered
630 register must be stored in the lowest slot. If MASK is the set
631 { R1,...,RN }, where R1...RN are ordered least first, the generated
632 instruction will have the form:
633
634 (parallel
635 (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
636 (set (mem:SI (plus:SI (reg:SI 9)
637 (const_int -1*4)))
638 (reg:SI RN))
639 ...
640 (set (mem:SI (plus:SI (reg:SI 9)
641 (const_int -N*4)))
642 (reg:SI R1))) */
643
644void
3285410a 645mn10300_gen_multiple_store (int mask)
4caa3669 646{
647 if (mask != 0)
648 {
649 int i;
650 int count;
651 rtx par;
652 int pari;
653
09e5ce26 654 /* Count how many registers need to be saved. */
4caa3669 655 count = 0;
b166356e 656 for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
4caa3669 657 if ((mask & (1 << i)) != 0)
658 count += 1;
659
660 /* We need one PARALLEL element to update the stack pointer and
09e5ce26 661 an additional element for each register that is stored. */
4caa3669 662 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count + 1));
663
09e5ce26 664 /* Create the instruction that updates the stack pointer. */
4caa3669 665 XVECEXP (par, 0, 0)
666 = gen_rtx_SET (SImode,
667 stack_pointer_rtx,
668 gen_rtx_PLUS (SImode,
669 stack_pointer_rtx,
670 GEN_INT (-count * 4)));
671
09e5ce26 672 /* Create each store. */
4caa3669 673 pari = 1;
b166356e 674 for (i = LAST_EXTENDED_REGNUM; i >= 0; i--)
4caa3669 675 if ((mask & (1 << i)) != 0)
676 {
677 rtx address = gen_rtx_PLUS (SImode,
678 stack_pointer_rtx,
679 GEN_INT (-pari * 4));
680 XVECEXP(par, 0, pari)
681 = gen_rtx_SET (VOIDmode,
682 gen_rtx_MEM (SImode, address),
683 gen_rtx_REG (SImode, i));
684 pari += 1;
685 }
686
687 par = emit_insn (par);
688 RTX_FRAME_RELATED_P (par) = 1;
689 }
690}
691
29a404f9 692void
3285410a 693expand_prologue (void)
29a404f9 694{
e3f95b40 695 HOST_WIDE_INT size;
f1899bff 696
f1899bff 697 /* SIZE includes the fixed stack space needed for function calls. */
36ed4406 698 size = get_frame_size () + current_function_outgoing_args_size;
699 size += (current_function_outgoing_args_size ? 4 : 0);
29a404f9 700
09e5ce26 701 /* If we use any of the callee-saved registers, save them now. */
4caa3669 702 mn10300_gen_multiple_store (mn10300_get_live_callee_saved_regs ());
48cb86e3 703
b166356e 704 if (TARGET_AM33_2 && fp_regs_to_save ())
705 {
706 int num_regs_to_save = fp_regs_to_save (), i;
707 HOST_WIDE_INT xsize;
708 enum { save_sp_merge,
709 save_sp_no_merge,
710 save_sp_partial_merge,
711 save_a0_merge,
712 save_a0_no_merge } strategy;
713 unsigned int strategy_size = (unsigned)-1, this_strategy_size;
714 rtx reg;
715 rtx insn;
716
717 /* We have several different strategies to save FP registers.
718 We can store them using SP offsets, which is beneficial if
719 there are just a few registers to save, or we can use `a0' in
720 post-increment mode (`a0' is the only call-clobbered address
721 register that is never used to pass information to a
722 function). Furthermore, if we don't need a frame pointer, we
723 can merge the two SP adds into a single one, but this isn't
724 always beneficial; sometimes we can just split the two adds
725 so that we don't exceed a 16-bit constant size. The code
726 below will select which strategy to use, so as to generate
727 smallest code. Ties are broken in favor or shorter sequences
728 (in terms of number of instructions). */
729
730#define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
731 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
732#define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
733 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
734#define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
735 (((S) >= (L)) ? (SIZE1) * (N) \
736 : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
737 + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
738 : (ELSE))
739#define SIZE_FMOV_SP_(S,N) \
740 (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
741 SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
742 (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
743#define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
744
745 /* Consider alternative save_sp_merge only if we don't need the
fa483857 746 frame pointer and size is nonzero. */
b166356e 747 if (! frame_pointer_needed && size)
748 {
749 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
750 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
751 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
752 this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
753
754 if (this_strategy_size < strategy_size)
755 {
756 strategy = save_sp_merge;
757 strategy_size = this_strategy_size;
758 }
759 }
760
761 /* Consider alternative save_sp_no_merge unconditionally. */
762 /* Insn: add -4 * num_regs_to_save, sp. */
763 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
764 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
765 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
766 if (size)
767 {
768 /* Insn: add -size, sp. */
769 this_strategy_size += SIZE_ADD_SP (-size);
770 }
771
772 if (this_strategy_size < strategy_size)
773 {
774 strategy = save_sp_no_merge;
775 strategy_size = this_strategy_size;
776 }
777
778 /* Consider alternative save_sp_partial_merge only if we don't
779 need a frame pointer and size is reasonably large. */
780 if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
781 {
782 /* Insn: add -128, sp. */
783 this_strategy_size = SIZE_ADD_SP (-128);
784 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
785 this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
786 num_regs_to_save);
787 if (size)
788 {
789 /* Insn: add 128-size, sp. */
790 this_strategy_size += SIZE_ADD_SP (128 - size);
791 }
792
793 if (this_strategy_size < strategy_size)
794 {
795 strategy = save_sp_partial_merge;
796 strategy_size = this_strategy_size;
797 }
798 }
799
800 /* Consider alternative save_a0_merge only if we don't need a
fa483857 801 frame pointer, size is nonzero and the user hasn't
b166356e 802 changed the calling conventions of a0. */
803 if (! frame_pointer_needed && size
804 && call_used_regs[FIRST_ADDRESS_REGNUM]
805 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
806 {
807 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
808 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
809 /* Insn: mov sp, a0. */
810 this_strategy_size++;
811 if (size)
812 {
813 /* Insn: add size, a0. */
814 this_strategy_size += SIZE_ADD_AX (size);
815 }
816 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
817 this_strategy_size += 3 * num_regs_to_save;
818
819 if (this_strategy_size < strategy_size)
820 {
821 strategy = save_a0_merge;
822 strategy_size = this_strategy_size;
823 }
824 }
825
826 /* Consider alternative save_a0_no_merge if the user hasn't
09e5ce26 827 changed the calling conventions of a0. */
b166356e 828 if (call_used_regs[FIRST_ADDRESS_REGNUM]
829 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
830 {
831 /* Insn: add -4 * num_regs_to_save, sp. */
832 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
833 /* Insn: mov sp, a0. */
834 this_strategy_size++;
835 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
836 this_strategy_size += 3 * num_regs_to_save;
837 if (size)
838 {
839 /* Insn: add -size, sp. */
840 this_strategy_size += SIZE_ADD_SP (-size);
841 }
842
843 if (this_strategy_size < strategy_size)
844 {
845 strategy = save_a0_no_merge;
846 strategy_size = this_strategy_size;
847 }
848 }
849
850 /* Emit the initial SP add, common to all strategies. */
851 switch (strategy)
852 {
853 case save_sp_no_merge:
854 case save_a0_no_merge:
855 emit_insn (gen_addsi3 (stack_pointer_rtx,
856 stack_pointer_rtx,
857 GEN_INT (-4 * num_regs_to_save)));
858 xsize = 0;
859 break;
860
861 case save_sp_partial_merge:
862 emit_insn (gen_addsi3 (stack_pointer_rtx,
863 stack_pointer_rtx,
864 GEN_INT (-128)));
865 xsize = 128 - 4 * num_regs_to_save;
866 size -= xsize;
867 break;
868
869 case save_sp_merge:
870 case save_a0_merge:
871 emit_insn (gen_addsi3 (stack_pointer_rtx,
872 stack_pointer_rtx,
873 GEN_INT (-(size + 4 * num_regs_to_save))));
874 /* We'll have to adjust FP register saves according to the
09e5ce26 875 frame size. */
b166356e 876 xsize = size;
877 /* Since we've already created the stack frame, don't do it
09e5ce26 878 again at the end of the function. */
b166356e 879 size = 0;
880 break;
881
882 default:
cf41bb03 883 gcc_unreachable ();
b166356e 884 }
fb16c776 885
b166356e 886 /* Now prepare register a0, if we have decided to use it. */
887 switch (strategy)
888 {
889 case save_sp_merge:
890 case save_sp_no_merge:
891 case save_sp_partial_merge:
892 reg = 0;
893 break;
894
895 case save_a0_merge:
896 case save_a0_no_merge:
897 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
898 emit_insn (gen_movsi (reg, stack_pointer_rtx));
899 if (xsize)
900 emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize)));
901 reg = gen_rtx_POST_INC (SImode, reg);
902 break;
fb16c776 903
b166356e 904 default:
cf41bb03 905 gcc_unreachable ();
b166356e 906 }
fb16c776 907
b166356e 908 /* Now actually save the FP registers. */
909 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
910 if (regs_ever_live[i] && ! call_used_regs[i])
911 {
912 rtx addr;
913
914 if (reg)
915 addr = reg;
916 else
917 {
918 /* If we aren't using `a0', use an SP offset. */
919 if (xsize)
920 {
921 addr = gen_rtx_PLUS (SImode,
922 stack_pointer_rtx,
923 GEN_INT (xsize));
924 }
925 else
926 addr = stack_pointer_rtx;
fb16c776 927
b166356e 928 xsize += 4;
929 }
930
931 insn = emit_insn (gen_movsi (gen_rtx_MEM (SImode, addr),
932 gen_rtx_REG (SImode, i)));
933
934 RTX_FRAME_RELATED_P (insn) = 1;
935 }
936 }
937
48cb86e3 938 /* Now put the frame pointer into the frame pointer register. */
29a404f9 939 if (frame_pointer_needed)
014546df 940 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
29a404f9 941
48cb86e3 942 /* Allocate stack for this frame. */
29a404f9 943 if (size)
944 emit_insn (gen_addsi3 (stack_pointer_rtx,
945 stack_pointer_rtx,
946 GEN_INT (-size)));
b87a151a 947 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
948 {
949 rtx insn = get_last_insn ();
950 rtx last = emit_insn (gen_GOTaddr2picreg ());
951
952 /* Mark these insns as possibly dead. Sometimes, flow2 may
953 delete all uses of the PIC register. In this case, let it
954 delete the initialization too. */
955 do
956 {
957 insn = NEXT_INSN (insn);
958
959 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
960 const0_rtx,
961 REG_NOTES (insn));
962 }
963 while (insn != last);
964 }
29a404f9 965}
966
967void
3285410a 968expand_epilogue (void)
29a404f9 969{
e3f95b40 970 HOST_WIDE_INT size;
f1899bff 971
f1899bff 972 /* SIZE includes the fixed stack space needed for function calls. */
36ed4406 973 size = get_frame_size () + current_function_outgoing_args_size;
974 size += (current_function_outgoing_args_size ? 4 : 0);
29a404f9 975
b166356e 976 if (TARGET_AM33_2 && fp_regs_to_save ())
977 {
978 int num_regs_to_save = fp_regs_to_save (), i;
979 rtx reg = 0;
980
981 /* We have several options to restore FP registers. We could
982 load them from SP offsets, but, if there are enough FP
983 registers to restore, we win if we use a post-increment
984 addressing mode. */
985
986 /* If we have a frame pointer, it's the best option, because we
987 already know it has the value we want. */
988 if (frame_pointer_needed)
989 reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
990 /* Otherwise, we may use `a1', since it's call-clobbered and
991 it's never used for return values. But only do so if it's
992 smaller than using SP offsets. */
993 else
994 {
995 enum { restore_sp_post_adjust,
996 restore_sp_pre_adjust,
997 restore_sp_partial_adjust,
998 restore_a1 } strategy;
999 unsigned int this_strategy_size, strategy_size = (unsigned)-1;
1000
1001 /* Consider using sp offsets before adjusting sp. */
1002 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1003 this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
1004 /* If size is too large, we'll have to adjust SP with an
1005 add. */
1006 if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
1007 {
1008 /* Insn: add size + 4 * num_regs_to_save, sp. */
1009 this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
1010 }
1011 /* If we don't have to restore any non-FP registers,
1012 we'll be able to save one byte by using rets. */
1013 if (! REG_SAVE_BYTES)
1014 this_strategy_size--;
1015
1016 if (this_strategy_size < strategy_size)
1017 {
1018 strategy = restore_sp_post_adjust;
1019 strategy_size = this_strategy_size;
1020 }
1021
1022 /* Consider using sp offsets after adjusting sp. */
1023 /* Insn: add size, sp. */
1024 this_strategy_size = SIZE_ADD_SP (size);
1025 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1026 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
1027 /* We're going to use ret to release the FP registers
09e5ce26 1028 save area, so, no savings. */
b166356e 1029
1030 if (this_strategy_size < strategy_size)
1031 {
1032 strategy = restore_sp_pre_adjust;
1033 strategy_size = this_strategy_size;
1034 }
1035
1036 /* Consider using sp offsets after partially adjusting sp.
1037 When size is close to 32Kb, we may be able to adjust SP
1038 with an imm16 add instruction while still using fmov
1039 (d8,sp). */
1040 if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
1041 {
1042 /* Insn: add size + 4 * num_regs_to_save
1043 + REG_SAVE_BYTES - 252,sp. */
1044 this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
1045 + REG_SAVE_BYTES - 252);
1046 /* Insn: fmov (##,sp),fs#, fo each fs# to be restored. */
1047 this_strategy_size += SIZE_FMOV_SP (252 - REG_SAVE_BYTES
1048 - 4 * num_regs_to_save,
1049 num_regs_to_save);
1050 /* We're going to use ret to release the FP registers
09e5ce26 1051 save area, so, no savings. */
b166356e 1052
1053 if (this_strategy_size < strategy_size)
1054 {
1055 strategy = restore_sp_partial_adjust;
1056 strategy_size = this_strategy_size;
1057 }
1058 }
1059
1060 /* Consider using a1 in post-increment mode, as long as the
1061 user hasn't changed the calling conventions of a1. */
1062 if (call_used_regs[FIRST_ADDRESS_REGNUM+1]
1063 && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1064 {
1065 /* Insn: mov sp,a1. */
1066 this_strategy_size = 1;
1067 if (size)
1068 {
1069 /* Insn: add size,a1. */
1070 this_strategy_size += SIZE_ADD_AX (size);
1071 }
1072 /* Insn: fmov (a1+),fs#, for each fs# to be restored. */
1073 this_strategy_size += 3 * num_regs_to_save;
1074 /* If size is large enough, we may be able to save a
1075 couple of bytes. */
1076 if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
1077 {
1078 /* Insn: mov a1,sp. */
1079 this_strategy_size += 2;
1080 }
1081 /* If we don't have to restore any non-FP registers,
1082 we'll be able to save one byte by using rets. */
1083 if (! REG_SAVE_BYTES)
1084 this_strategy_size--;
1085
1086 if (this_strategy_size < strategy_size)
1087 {
1088 strategy = restore_a1;
1089 strategy_size = this_strategy_size;
1090 }
1091 }
1092
1093 switch (strategy)
1094 {
1095 case restore_sp_post_adjust:
1096 break;
1097
1098 case restore_sp_pre_adjust:
1099 emit_insn (gen_addsi3 (stack_pointer_rtx,
1100 stack_pointer_rtx,
1101 GEN_INT (size)));
1102 size = 0;
1103 break;
1104
1105 case restore_sp_partial_adjust:
1106 emit_insn (gen_addsi3 (stack_pointer_rtx,
1107 stack_pointer_rtx,
1108 GEN_INT (size + 4 * num_regs_to_save
1109 + REG_SAVE_BYTES - 252)));
1110 size = 252 - REG_SAVE_BYTES - 4 * num_regs_to_save;
1111 break;
fb16c776 1112
b166356e 1113 case restore_a1:
1114 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1115 emit_insn (gen_movsi (reg, stack_pointer_rtx));
1116 if (size)
1117 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1118 break;
1119
1120 default:
cf41bb03 1121 gcc_unreachable ();
b166356e 1122 }
1123 }
1124
1125 /* Adjust the selected register, if any, for post-increment. */
1126 if (reg)
1127 reg = gen_rtx_POST_INC (SImode, reg);
1128
1129 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
1130 if (regs_ever_live[i] && ! call_used_regs[i])
1131 {
1132 rtx addr;
fb16c776 1133
b166356e 1134 if (reg)
1135 addr = reg;
1136 else if (size)
1137 {
1138 /* If we aren't using a post-increment register, use an
09e5ce26 1139 SP offset. */
b166356e 1140 addr = gen_rtx_PLUS (SImode,
1141 stack_pointer_rtx,
1142 GEN_INT (size));
1143 }
1144 else
1145 addr = stack_pointer_rtx;
1146
1147 size += 4;
1148
1149 emit_insn (gen_movsi (gen_rtx_REG (SImode, i),
1150 gen_rtx_MEM (SImode, addr)));
1151 }
1152
1153 /* If we were using the restore_a1 strategy and the number of
1154 bytes to be released won't fit in the `ret' byte, copy `a1'
1155 to `sp', to avoid having to use `add' to adjust it. */
1156 if (! frame_pointer_needed && reg && size + REG_SAVE_BYTES > 255)
1157 {
1158 emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1159 size = 0;
1160 }
1161 }
1162
461cabcc 1163 /* Maybe cut back the stack, except for the register save area.
1164
1165 If the frame pointer exists, then use the frame pointer to
1166 cut back the stack.
1167
1168 If the stack size + register save area is more than 255 bytes,
1169 then the stack must be cut back here since the size + register
fb16c776 1170 save size is too big for a ret/retf instruction.
461cabcc 1171
1172 Else leave it alone, it will be cut back as part of the
1173 ret/retf instruction, or there wasn't any stack to begin with.
1174
dfd1079d 1175 Under no circumstances should the register save area be
461cabcc 1176 deallocated here, that would leave a window where an interrupt
1177 could occur and trash the register save area. */
29a404f9 1178 if (frame_pointer_needed)
1179 {
29a404f9 1180 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
b21218d6 1181 size = 0;
1182 }
55daf463 1183 else if (size + REG_SAVE_BYTES > 255)
b21218d6 1184 {
1185 emit_insn (gen_addsi3 (stack_pointer_rtx,
1186 stack_pointer_rtx,
1187 GEN_INT (size)));
1188 size = 0;
29a404f9 1189 }
29a404f9 1190
55daf463 1191 /* Adjust the stack and restore callee-saved registers, if any. */
1192 if (size || regs_ever_live[2] || regs_ever_live[3]
48cb86e3 1193 || regs_ever_live[6] || regs_ever_live[7]
911517ac 1194 || regs_ever_live[14] || regs_ever_live[15]
1195 || regs_ever_live[16] || regs_ever_live[17]
48cb86e3 1196 || frame_pointer_needed)
55daf463 1197 emit_jump_insn (gen_return_internal_regs
1198 (GEN_INT (size + REG_SAVE_BYTES)));
48cb86e3 1199 else
55daf463 1200 emit_jump_insn (gen_return_internal ());
29a404f9 1201}
1202
1203/* Update the condition code from the insn. */
1204
1205void
3285410a 1206notice_update_cc (rtx body, rtx insn)
29a404f9 1207{
29a404f9 1208 switch (get_attr_cc (insn))
1209 {
1210 case CC_NONE:
1211 /* Insn does not affect CC at all. */
1212 break;
1213
1214 case CC_NONE_0HIT:
1215 /* Insn does not change CC, but the 0'th operand has been changed. */
1216 if (cc_status.value1 != 0
ed420a25 1217 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
29a404f9 1218 cc_status.value1 = 0;
1219 break;
1220
14058057 1221 case CC_SET_ZN:
ed420a25 1222 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
14058057 1223 V,C are unusable. */
29a404f9 1224 CC_STATUS_INIT;
14058057 1225 cc_status.flags |= CC_NO_CARRY | CC_OVERFLOW_UNUSABLE;
ed420a25 1226 cc_status.value1 = recog_data.operand[0];
29a404f9 1227 break;
1228
14058057 1229 case CC_SET_ZNV:
ed420a25 1230 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
14058057 1231 C is unusable. */
45e3d109 1232 CC_STATUS_INIT;
14058057 1233 cc_status.flags |= CC_NO_CARRY;
ed420a25 1234 cc_status.value1 = recog_data.operand[0];
45e3d109 1235 break;
1236
29a404f9 1237 case CC_COMPARE:
1238 /* The insn is a compare instruction. */
1239 CC_STATUS_INIT;
1240 cc_status.value1 = SET_SRC (body);
b166356e 1241 if (GET_CODE (cc_status.value1) == COMPARE
1242 && GET_MODE (XEXP (cc_status.value1, 0)) == SFmode)
1243 cc_status.mdep.fpCC = 1;
29a404f9 1244 break;
1245
1246 case CC_CLOBBER:
1247 /* Insn doesn't leave CC in a usable state. */
1248 CC_STATUS_INIT;
1249 break;
45e3d109 1250
1251 default:
cf41bb03 1252 gcc_unreachable ();
29a404f9 1253 }
29a404f9 1254}
1255
a2f10574 1256/* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
4caa3669 1257 This function is for MATCH_PARALLEL and so assumes OP is known to be
1258 parallel. If OP is a multiple store, return a mask indicating which
1259 registers it saves. Return 0 otherwise. */
1260
1261int
3285410a 1262store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4caa3669 1263{
1264 int count;
1265 int mask;
1266 int i;
1267 unsigned int last;
1268 rtx elt;
1269
1270 count = XVECLEN (op, 0);
1271 if (count < 2)
1272 return 0;
1273
1274 /* Check that first instruction has the form (set (sp) (plus A B)) */
1275 elt = XVECEXP (op, 0, 0);
1276 if (GET_CODE (elt) != SET
1277 || GET_CODE (SET_DEST (elt)) != REG
1278 || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1279 || GET_CODE (SET_SRC (elt)) != PLUS)
1280 return 0;
1281
1282 /* Check that A is the stack pointer and B is the expected stack size.
1283 For OP to match, each subsequent instruction should push a word onto
1284 the stack. We therefore expect the first instruction to create
09e5ce26 1285 COUNT-1 stack slots. */
4caa3669 1286 elt = SET_SRC (elt);
1287 if (GET_CODE (XEXP (elt, 0)) != REG
1288 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1289 || GET_CODE (XEXP (elt, 1)) != CONST_INT
1290 || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1291 return 0;
1292
1293 /* Now go through the rest of the vector elements. They must be
1294 ordered so that the first instruction stores the highest-numbered
1295 register to the highest stack slot and that subsequent instructions
1296 store a lower-numbered register to the slot below.
1297
1298 LAST keeps track of the smallest-numbered register stored so far.
09e5ce26 1299 MASK is the set of stored registers. */
b166356e 1300 last = LAST_EXTENDED_REGNUM + 1;
4caa3669 1301 mask = 0;
1302 for (i = 1; i < count; i++)
1303 {
09e5ce26 1304 /* Check that element i is a (set (mem M) R) and that R is valid. */
4caa3669 1305 elt = XVECEXP (op, 0, i);
1306 if (GET_CODE (elt) != SET
1307 || GET_CODE (SET_DEST (elt)) != MEM
1308 || GET_CODE (SET_SRC (elt)) != REG
1309 || REGNO (SET_SRC (elt)) >= last)
1310 return 0;
1311
1312 /* R was OK, so provisionally add it to MASK. We return 0 in any
09e5ce26 1313 case if the rest of the instruction has a flaw. */
4caa3669 1314 last = REGNO (SET_SRC (elt));
1315 mask |= (1 << last);
1316
1317 /* Check that M has the form (plus (sp) (const_int -I*4)) */
1318 elt = XEXP (SET_DEST (elt), 0);
1319 if (GET_CODE (elt) != PLUS
1320 || GET_CODE (XEXP (elt, 0)) != REG
1321 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1322 || GET_CODE (XEXP (elt, 1)) != CONST_INT
1323 || INTVAL (XEXP (elt, 1)) != -i * 4)
1324 return 0;
1325 }
1326
09e5ce26 1327 /* All or none of the callee-saved extended registers must be in the set. */
4caa3669 1328 if ((mask & 0x3c000) != 0
1329 && (mask & 0x3c000) != 0x3c000)
1330 return 0;
1331
1332 return mask;
1333}
1334
29a404f9 1335/* What (if any) secondary registers are needed to move IN with mode
fb16c776 1336 MODE into a register in register class CLASS.
29a404f9 1337
1338 We might be able to simplify this. */
1339enum reg_class
51149d85 1340mn10300_secondary_reload_class (enum reg_class class, enum machine_mode mode,
1341 rtx in)
29a404f9 1342{
29a404f9 1343 /* Memory loads less than a full word wide can't have an
1344 address or stack pointer destination. They must use
1345 a data register as an intermediate register. */
79ea832f 1346 if ((GET_CODE (in) == MEM
1347 || (GET_CODE (in) == REG
1348 && REGNO (in) >= FIRST_PSEUDO_REGISTER)
1349 || (GET_CODE (in) == SUBREG
1350 && GET_CODE (SUBREG_REG (in)) == REG
1351 && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER))
29a404f9 1352 && (mode == QImode || mode == HImode)
79ea832f 1353 && (class == ADDRESS_REGS || class == SP_REGS
b0ba03a6 1354 || class == SP_OR_ADDRESS_REGS))
8ecf154e 1355 {
911517ac 1356 if (TARGET_AM33)
1357 return DATA_OR_EXTENDED_REGS;
8ecf154e 1358 return DATA_REGS;
1359 }
29a404f9 1360
1361 /* We can't directly load sp + const_int into a data register;
1362 we must use an address register as an intermediate. */
48cb86e3 1363 if (class != SP_REGS
1364 && class != ADDRESS_REGS
1365 && class != SP_OR_ADDRESS_REGS
911517ac 1366 && class != SP_OR_EXTENDED_REGS
1367 && class != ADDRESS_OR_EXTENDED_REGS
1368 && class != SP_OR_ADDRESS_OR_EXTENDED_REGS
29a404f9 1369 && (in == stack_pointer_rtx
1370 || (GET_CODE (in) == PLUS
48cb86e3 1371 && (XEXP (in, 0) == stack_pointer_rtx
1372 || XEXP (in, 1) == stack_pointer_rtx))))
29a404f9 1373 return ADDRESS_REGS;
1374
19de5de1 1375 if (GET_CODE (in) == PLUS
1376 && (XEXP (in, 0) == stack_pointer_rtx
1377 || XEXP (in, 1) == stack_pointer_rtx))
8ecf154e 1378 {
911517ac 1379 if (TARGET_AM33)
1380 return DATA_OR_EXTENDED_REGS;
8ecf154e 1381 return DATA_REGS;
1382 }
fb16c776 1383
b166356e 1384 if (TARGET_AM33_2 && class == FP_REGS
fb16c776 1385 && GET_CODE (in) == MEM
1386 && ! (GET_CODE (in) == MEM && !CONSTANT_ADDRESS_P (XEXP (in, 0))))
b166356e 1387 {
1388 if (TARGET_AM33)
1389 return DATA_OR_EXTENDED_REGS;
1390 return DATA_REGS;
1391 }
1392
48cb86e3 1393 /* Otherwise assume no secondary reloads are needed. */
1394 return NO_REGS;
1395}
1396
1397int
3285410a 1398initial_offset (int from, int to)
48cb86e3 1399{
f1899bff 1400 /* The difference between the argument pointer and the frame pointer
1401 is the size of the callee register save area. */
48cb86e3 1402 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
29a404f9 1403 {
48cb86e3 1404 if (regs_ever_live[2] || regs_ever_live[3]
1405 || regs_ever_live[6] || regs_ever_live[7]
911517ac 1406 || regs_ever_live[14] || regs_ever_live[15]
1407 || regs_ever_live[16] || regs_ever_live[17]
b166356e 1408 || fp_regs_to_save ()
48cb86e3 1409 || frame_pointer_needed)
b166356e 1410 return REG_SAVE_BYTES
1411 + 4 * fp_regs_to_save ();
48cb86e3 1412 else
bb4959a8 1413 return 0;
29a404f9 1414 }
1415
f1899bff 1416 /* The difference between the argument pointer and the stack pointer is
1417 the sum of the size of this function's frame, the callee register save
1418 area, and the fixed stack space needed for function calls (if any). */
48cb86e3 1419 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1420 {
1421 if (regs_ever_live[2] || regs_ever_live[3]
1422 || regs_ever_live[6] || regs_ever_live[7]
911517ac 1423 || regs_ever_live[14] || regs_ever_live[15]
1424 || regs_ever_live[16] || regs_ever_live[17]
b166356e 1425 || fp_regs_to_save ()
48cb86e3 1426 || frame_pointer_needed)
8ecf154e 1427 return (get_frame_size () + REG_SAVE_BYTES
b166356e 1428 + 4 * fp_regs_to_save ()
36ed4406 1429 + (current_function_outgoing_args_size
fb16c776 1430 ? current_function_outgoing_args_size + 4 : 0));
48cb86e3 1431 else
36ed4406 1432 return (get_frame_size ()
1433 + (current_function_outgoing_args_size
fb16c776 1434 ? current_function_outgoing_args_size + 4 : 0));
48cb86e3 1435 }
29a404f9 1436
f1899bff 1437 /* The difference between the frame pointer and stack pointer is the sum
1438 of the size of this function's frame and the fixed stack space needed
1439 for function calls (if any). */
48cb86e3 1440 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
36ed4406 1441 return (get_frame_size ()
1442 + (current_function_outgoing_args_size
fb16c776 1443 ? current_function_outgoing_args_size + 4 : 0));
48cb86e3 1444
cf41bb03 1445 gcc_unreachable ();
29a404f9 1446}
bb4959a8 1447
6644435d 1448/* Worker function for TARGET_RETURN_IN_MEMORY. */
1449
f2d49d02 1450static bool
1451mn10300_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
1452{
1453 /* Return values > 8 bytes in length in memory. */
00b1da0e 1454 return (int_size_in_bytes (type) > 8
1455 || int_size_in_bytes (type) == 0
1456 || TYPE_MODE (type) == BLKmode);
f2d49d02 1457}
1458
bb4959a8 1459/* Flush the argument registers to the stack for a stdarg function;
1460 return the new argument pointer. */
f2d49d02 1461static rtx
3285410a 1462mn10300_builtin_saveregs (void)
bb4959a8 1463{
ed554036 1464 rtx offset, mem;
bb4959a8 1465 tree fntype = TREE_TYPE (current_function_decl);
1466 int argadj = ((!(TYPE_ARG_TYPES (fntype) != 0
1467 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
1468 != void_type_node)))
1469 ? UNITS_PER_WORD : 0);
ed554036 1470 int set = get_varargs_alias_set ();
bb4959a8 1471
1472 if (argadj)
1473 offset = plus_constant (current_function_arg_offset_rtx, argadj);
1474 else
1475 offset = current_function_arg_offset_rtx;
1476
ed554036 1477 mem = gen_rtx_MEM (SImode, current_function_internal_arg_pointer);
ab6ab77e 1478 set_mem_alias_set (mem, set);
ed554036 1479 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1480
1481 mem = gen_rtx_MEM (SImode,
1482 plus_constant (current_function_internal_arg_pointer, 4));
ab6ab77e 1483 set_mem_alias_set (mem, set);
ed554036 1484 emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1485
bb4959a8 1486 return copy_to_reg (expand_binop (Pmode, add_optab,
1487 current_function_internal_arg_pointer,
1488 offset, 0, 0, OPTAB_LIB_WIDEN));
1489}
1490
ed554036 1491void
3285410a 1492mn10300_va_start (tree valist, rtx nextarg)
ed554036 1493{
7ccc713a 1494 nextarg = expand_builtin_saveregs ();
7df226a2 1495 std_expand_builtin_va_start (valist, nextarg);
ed554036 1496}
1497
b981d932 1498/* Return true when a parameter should be passed by reference. */
1499
1500static bool
1501mn10300_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
1502 enum machine_mode mode, tree type,
1503 bool named ATTRIBUTE_UNUSED)
1504{
1505 unsigned HOST_WIDE_INT size;
1506
1507 if (type)
1508 size = int_size_in_bytes (type);
1509 else
1510 size = GET_MODE_SIZE (mode);
1511
00b1da0e 1512 return (size > 8 || size == 0);
b981d932 1513}
1514
bb4959a8 1515/* Return an RTX to represent where a value with mode MODE will be returned
1516 from a function. If the result is 0, the argument is pushed. */
1517
1518rtx
3285410a 1519function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1520 tree type, int named ATTRIBUTE_UNUSED)
bb4959a8 1521{
1522 rtx result = 0;
1523 int size, align;
1524
1525 /* We only support using 2 data registers as argument registers. */
1526 int nregs = 2;
1527
1528 /* Figure out the size of the object to be passed. */
1529 if (mode == BLKmode)
1530 size = int_size_in_bytes (type);
1531 else
1532 size = GET_MODE_SIZE (mode);
1533
1534 /* Figure out the alignment of the object to be passed. */
1535 align = size;
1536
1537 cum->nbytes = (cum->nbytes + 3) & ~3;
1538
1539 /* Don't pass this arg via a register if all the argument registers
1540 are used up. */
1541 if (cum->nbytes > nregs * UNITS_PER_WORD)
1542 return 0;
1543
1544 /* Don't pass this arg via a register if it would be split between
1545 registers and memory. */
1546 if (type == NULL_TREE
1547 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1548 return 0;
1549
1550 switch (cum->nbytes / UNITS_PER_WORD)
1551 {
1552 case 0:
7014838c 1553 result = gen_rtx_REG (mode, 0);
bb4959a8 1554 break;
1555 case 1:
7014838c 1556 result = gen_rtx_REG (mode, 1);
bb4959a8 1557 break;
1558 default:
1559 result = 0;
1560 }
1561
1562 return result;
1563}
1564
f054eb3c 1565/* Return the number of bytes of registers to use for an argument passed
1566 partially in registers and partially in memory. */
bb4959a8 1567
f054eb3c 1568static int
1569mn10300_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1570 tree type, bool named ATTRIBUTE_UNUSED)
bb4959a8 1571{
1572 int size, align;
1573
1574 /* We only support using 2 data registers as argument registers. */
1575 int nregs = 2;
1576
1577 /* Figure out the size of the object to be passed. */
1578 if (mode == BLKmode)
1579 size = int_size_in_bytes (type);
1580 else
1581 size = GET_MODE_SIZE (mode);
1582
1583 /* Figure out the alignment of the object to be passed. */
1584 align = size;
1585
1586 cum->nbytes = (cum->nbytes + 3) & ~3;
1587
1588 /* Don't pass this arg via a register if all the argument registers
1589 are used up. */
1590 if (cum->nbytes > nregs * UNITS_PER_WORD)
1591 return 0;
1592
1593 if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1594 return 0;
1595
1596 /* Don't pass this arg via a register if it would be split between
1597 registers and memory. */
1598 if (type == NULL_TREE
1599 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1600 return 0;
1601
f054eb3c 1602 return nregs * UNITS_PER_WORD - cum->nbytes;
bb4959a8 1603}
1604
00b1da0e 1605/* Return the location of the function's value. This will be either
1606 $d0 for integer functions, $a0 for pointers, or a PARALLEL of both
1607 $d0 and $a0 if the -mreturn-pointer-on-do flag is set. Note that
1608 we only return the PARALLEL for outgoing values; we do not want
1609 callers relying on this extra copy. */
1610
1611rtx
1612mn10300_function_value (tree valtype, tree func, int outgoing)
1613{
1614 rtx rv;
1615 enum machine_mode mode = TYPE_MODE (valtype);
1616
1617 if (! POINTER_TYPE_P (valtype))
1618 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1619 else if (! TARGET_PTR_A0D0 || ! outgoing
1620 || current_function_returns_struct)
1621 return gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM);
1622
1623 rv = gen_rtx_PARALLEL (mode, rtvec_alloc (2));
1624 XVECEXP (rv, 0, 0)
1625 = gen_rtx_EXPR_LIST (VOIDmode,
1626 gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM),
1627 GEN_INT (0));
fb16c776 1628
00b1da0e 1629 XVECEXP (rv, 0, 1)
1630 = gen_rtx_EXPR_LIST (VOIDmode,
1631 gen_rtx_REG (mode, FIRST_DATA_REGNUM),
1632 GEN_INT (0));
1633 return rv;
1634}
1635
bb4959a8 1636/* Output a tst insn. */
feb9af9f 1637const char *
3285410a 1638output_tst (rtx operand, rtx insn)
bb4959a8 1639{
bb4959a8 1640 rtx temp;
1641 int past_call = 0;
1642
1643 /* We can save a byte if we can find a register which has the value
1644 zero in it. */
1645 temp = PREV_INSN (insn);
6e90c6cd 1646 while (optimize && temp)
bb4959a8 1647 {
1648 rtx set;
1649
1650 /* We allow the search to go through call insns. We record
1651 the fact that we've past a CALL_INSN and reject matches which
1652 use call clobbered registers. */
1653 if (GET_CODE (temp) == CODE_LABEL
1654 || GET_CODE (temp) == JUMP_INSN
1655 || GET_CODE (temp) == BARRIER)
1656 break;
1657
1658 if (GET_CODE (temp) == CALL_INSN)
1659 past_call = 1;
1660
1661 if (GET_CODE (temp) == NOTE)
1662 {
1663 temp = PREV_INSN (temp);
1664 continue;
1665 }
1666
09e5ce26 1667 /* It must be an insn, see if it is a simple set. */
bb4959a8 1668 set = single_set (temp);
1669 if (!set)
1670 {
1671 temp = PREV_INSN (temp);
1672 continue;
1673 }
1674
1675 /* Are we setting a data register to zero (this does not win for
fb16c776 1676 address registers)?
bb4959a8 1677
1678 If it's a call clobbered register, have we past a call?
1679
1680 Make sure the register we find isn't the same as ourself;
8ecf154e 1681 the mn10300 can't encode that.
1682
1683 ??? reg_set_between_p return nonzero anytime we pass a CALL_INSN
1684 so the code to detect calls here isn't doing anything useful. */
bb4959a8 1685 if (REG_P (SET_DEST (set))
1686 && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1687 && !reg_set_between_p (SET_DEST (set), temp, insn)
6e90c6cd 1688 && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1689 == REGNO_REG_CLASS (REGNO (operand)))
911517ac 1690 && REGNO_REG_CLASS (REGNO (SET_DEST (set))) != EXTENDED_REGS
1691 && REGNO (SET_DEST (set)) != REGNO (operand)
fb16c776 1692 && (!past_call
911517ac 1693 || !call_used_regs[REGNO (SET_DEST (set))]))
1694 {
1695 rtx xoperands[2];
1696 xoperands[0] = operand;
1697 xoperands[1] = SET_DEST (set);
1698
1699 output_asm_insn ("cmp %1,%0", xoperands);
1700 return "";
1701 }
1702
1703 if (REGNO_REG_CLASS (REGNO (operand)) == EXTENDED_REGS
1704 && REG_P (SET_DEST (set))
1705 && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1706 && !reg_set_between_p (SET_DEST (set), temp, insn)
1707 && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1708 != REGNO_REG_CLASS (REGNO (operand)))
1709 && REGNO_REG_CLASS (REGNO (SET_DEST (set))) == EXTENDED_REGS
bb4959a8 1710 && REGNO (SET_DEST (set)) != REGNO (operand)
fb16c776 1711 && (!past_call
bb4959a8 1712 || !call_used_regs[REGNO (SET_DEST (set))]))
1713 {
1714 rtx xoperands[2];
1715 xoperands[0] = operand;
1716 xoperands[1] = SET_DEST (set);
1717
1718 output_asm_insn ("cmp %1,%0", xoperands);
1719 return "";
1720 }
1721 temp = PREV_INSN (temp);
1722 }
1723 return "cmp 0,%0";
1724}
36ed4406 1725
1726int
3285410a 1727impossible_plus_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
36ed4406 1728{
36ed4406 1729 if (GET_CODE (op) != PLUS)
1730 return 0;
1731
19de5de1 1732 if (XEXP (op, 0) == stack_pointer_rtx
1733 || XEXP (op, 1) == stack_pointer_rtx)
36ed4406 1734 return 1;
1735
36ed4406 1736 return 0;
1737}
c4cd8f6a 1738
9d3b5b5e 1739/* Similarly, but when using a zero_extract pattern for a btst where
1740 the source operand might end up in memory. */
1741int
3285410a 1742mask_ok_for_mem_btst (int len, int bit)
9d3b5b5e 1743{
805e22b2 1744 unsigned int mask = 0;
9d3b5b5e 1745
1746 while (len > 0)
1747 {
1748 mask |= (1 << bit);
1749 bit++;
1750 len--;
1751 }
1752
1753 /* MASK must bit into an 8bit value. */
1754 return (((mask & 0xff) == mask)
1755 || ((mask & 0xff00) == mask)
1756 || ((mask & 0xff0000) == mask)
1757 || ((mask & 0xff000000) == mask));
1758}
1759
c4cd8f6a 1760/* Return 1 if X contains a symbolic expression. We know these
1761 expressions will have one of a few well defined forms, so
1762 we need only check those forms. */
1763int
3285410a 1764symbolic_operand (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
c4cd8f6a 1765{
1766 switch (GET_CODE (op))
1767 {
1768 case SYMBOL_REF:
1769 case LABEL_REF:
1770 return 1;
1771 case CONST:
1772 op = XEXP (op, 0);
1773 return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1774 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1775 && GET_CODE (XEXP (op, 1)) == CONST_INT);
1776 default:
1777 return 0;
1778 }
1779}
1780
1781/* Try machine dependent ways of modifying an illegitimate address
1782 to be legitimate. If we find one, return the new valid address.
1783 This macro is used in only one place: `memory_address' in explow.c.
1784
1785 OLDX is the address as it was before break_out_memory_refs was called.
1786 In some cases it is useful to look at this to decide what needs to be done.
1787
1788 MODE and WIN are passed so that this macro can use
1789 GO_IF_LEGITIMATE_ADDRESS.
1790
1791 Normally it is always safe for this macro to do nothing. It exists to
1792 recognize opportunities to optimize the output.
1793
1794 But on a few ports with segmented architectures and indexed addressing
1795 (mn10300, hppa) it is used to rewrite certain problematical addresses. */
1796rtx
3285410a 1797legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1798 enum machine_mode mode ATTRIBUTE_UNUSED)
c4cd8f6a 1799{
b87a151a 1800 if (flag_pic && ! legitimate_pic_operand_p (x))
1801 x = legitimize_pic_address (oldx, NULL_RTX);
1802
c4cd8f6a 1803 /* Uh-oh. We might have an address for x[n-100000]. This needs
1804 special handling to avoid creating an indexed memory address
1805 with x-100000 as the base. */
1806 if (GET_CODE (x) == PLUS
1807 && symbolic_operand (XEXP (x, 1), VOIDmode))
1808 {
1809 /* Ugly. We modify things here so that the address offset specified
1810 by the index expression is computed first, then added to x to form
1811 the entire address. */
1812
59086782 1813 rtx regx1, regy1, regy2, y;
c4cd8f6a 1814
1815 /* Strip off any CONST. */
1816 y = XEXP (x, 1);
1817 if (GET_CODE (y) == CONST)
1818 y = XEXP (y, 0);
1819
c927a8ab 1820 if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1821 {
1822 regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1823 regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1824 regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1825 regx1 = force_reg (Pmode,
29bb088d 1826 gen_rtx_fmt_ee (GET_CODE (y), Pmode, regx1, regy2));
7014838c 1827 return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
c927a8ab 1828 }
c4cd8f6a 1829 }
11b4605c 1830 return x;
c4cd8f6a 1831}
e2aead91 1832
b87a151a 1833/* Convert a non-PIC address in `orig' to a PIC address using @GOT or
09e5ce26 1834 @GOTOFF in `reg'. */
b87a151a 1835rtx
3285410a 1836legitimize_pic_address (rtx orig, rtx reg)
b87a151a 1837{
1838 if (GET_CODE (orig) == LABEL_REF
1839 || (GET_CODE (orig) == SYMBOL_REF
1840 && (CONSTANT_POOL_ADDRESS_P (orig)
1841 || ! MN10300_GLOBAL_P (orig))))
1842 {
1843 if (reg == 0)
1844 reg = gen_reg_rtx (Pmode);
1845
1846 emit_insn (gen_symGOTOFF2reg (reg, orig));
1847 return reg;
1848 }
1849 else if (GET_CODE (orig) == SYMBOL_REF)
1850 {
1851 if (reg == 0)
1852 reg = gen_reg_rtx (Pmode);
1853
1854 emit_insn (gen_symGOT2reg (reg, orig));
1855 return reg;
1856 }
1857 return orig;
1858}
1859
1860/* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
fa483857 1861 isn't protected by a PIC unspec; nonzero otherwise. */
b87a151a 1862int
3285410a 1863legitimate_pic_operand_p (rtx x)
b87a151a 1864{
1865 register const char *fmt;
1866 register int i;
1867
1868 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1869 return 0;
1870
1871 if (GET_CODE (x) == UNSPEC
1872 && (XINT (x, 1) == UNSPEC_PIC
1873 || XINT (x, 1) == UNSPEC_GOT
1874 || XINT (x, 1) == UNSPEC_GOTOFF
1875 || XINT (x, 1) == UNSPEC_PLT))
1876 return 1;
1877
b87a151a 1878 fmt = GET_RTX_FORMAT (GET_CODE (x));
1879 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1880 {
1881 if (fmt[i] == 'E')
1882 {
1883 register int j;
1884
1885 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1886 if (! legitimate_pic_operand_p (XVECEXP (x, i, j)))
1887 return 0;
1888 }
1889 else if (fmt[i] == 'e' && ! legitimate_pic_operand_p (XEXP (x, i)))
1890 return 0;
1891 }
1892
1893 return 1;
1894}
1895
5411aa8c 1896/* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
1897 legitimate, and FALSE otherwise. */
1898bool
1899legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1900{
1901 if (CONSTANT_ADDRESS_P (x)
1902 && (! flag_pic || legitimate_pic_operand_p (x)))
1903 return TRUE;
1904
1905 if (RTX_OK_FOR_BASE_P (x, strict))
1906 return TRUE;
1907
1908 if (TARGET_AM33
1909 && GET_CODE (x) == POST_INC
1910 && RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1911 && (mode == SImode || mode == SFmode || mode == HImode))
1912 return TRUE;
1913
1914 if (GET_CODE (x) == PLUS)
1915 {
1916 rtx base = 0, index = 0;
1917
1918 if (REG_P (XEXP (x, 0))
1919 && REGNO_STRICT_OK_FOR_BASE_P (REGNO (XEXP (x, 0)), strict))
1920 {
1921 base = XEXP (x, 0);
1922 index = XEXP (x, 1);
1923 }
1924
1925 if (REG_P (XEXP (x, 1))
1926 && REGNO_STRICT_OK_FOR_BASE_P (REGNO (XEXP (x, 1)), strict))
1927 {
1928 base = XEXP (x, 1);
1929 index = XEXP (x, 0);
1930 }
1931
1932 if (base != 0 && index != 0)
1933 {
1934 if (GET_CODE (index) == CONST_INT)
1935 return TRUE;
1936 if (GET_CODE (index) == CONST
9247cc1c 1937 && GET_CODE (XEXP (index, 0)) != PLUS
5411aa8c 1938 && (! flag_pic
1939 || legitimate_pic_operand_p (index)))
1940 return TRUE;
1941 }
1942 }
1943
1944 return FALSE;
1945}
1946
ec0457a8 1947static int
3285410a 1948mn10300_address_cost_1 (rtx x, int *unsig)
e2aead91 1949{
e2aead91 1950 switch (GET_CODE (x))
1951 {
1952 case REG:
1953 switch (REGNO_REG_CLASS (REGNO (x)))
1954 {
1955 case SP_REGS:
1956 *unsig = 1;
1957 return 0;
1958
1959 case ADDRESS_REGS:
1960 return 1;
1961
1962 case DATA_REGS:
1963 case EXTENDED_REGS:
b166356e 1964 case FP_REGS:
e2aead91 1965 return 3;
1966
1967 case NO_REGS:
1968 return 5;
1969
1970 default:
cf41bb03 1971 gcc_unreachable ();
e2aead91 1972 }
1973
1974 case PLUS:
1975 case MINUS:
cbe23f2f 1976 case ASHIFT:
1977 case AND:
e2aead91 1978 case IOR:
ec0457a8 1979 return (mn10300_address_cost_1 (XEXP (x, 0), unsig)
1980 + mn10300_address_cost_1 (XEXP (x, 1), unsig));
e2aead91 1981
1982 case EXPR_LIST:
1983 case SUBREG:
1984 case MEM:
ec0457a8 1985 return mn10300_address_cost (XEXP (x, 0));
e2aead91 1986
1987 case ZERO_EXTEND:
1988 *unsig = 1;
ec0457a8 1989 return mn10300_address_cost_1 (XEXP (x, 0), unsig);
e2aead91 1990
1991 case CONST_INT:
1992 if (INTVAL (x) == 0)
1993 return 0;
1994 if (INTVAL (x) + (*unsig ? 0 : 0x80) < 0x100)
1995 return 1;
1996 if (INTVAL (x) + (*unsig ? 0 : 0x8000) < 0x10000)
1997 return 3;
1998 if (INTVAL (x) + (*unsig ? 0 : 0x800000) < 0x1000000)
1999 return 5;
2000 return 7;
2001
2002 case CONST:
2003 case SYMBOL_REF:
cbe23f2f 2004 case LABEL_REF:
e2aead91 2005 return 8;
2006
e2aead91 2007 default:
cf41bb03 2008 gcc_unreachable ();
e2aead91 2009
2010 }
2011}
fab7adbf 2012
ec0457a8 2013static int
3285410a 2014mn10300_address_cost (rtx x)
ec0457a8 2015{
2016 int s = 0;
2017 return mn10300_address_cost_1 (x, &s);
2018}
2019
fab7adbf 2020static bool
3285410a 2021mn10300_rtx_costs (rtx x, int code, int outer_code, int *total)
fab7adbf 2022{
2023 switch (code)
2024 {
2025 case CONST_INT:
2026 /* Zeros are extremely cheap. */
2027 if (INTVAL (x) == 0 && outer_code == SET)
2028 *total = 0;
2029 /* If it fits in 8 bits, then it's still relatively cheap. */
2030 else if (INT_8_BITS (INTVAL (x)))
2031 *total = 1;
2032 /* This is the "base" cost, includes constants where either the
2033 upper or lower 16bits are all zeros. */
2034 else if (INT_16_BITS (INTVAL (x))
2035 || (INTVAL (x) & 0xffff) == 0
2036 || (INTVAL (x) & 0xffff0000) == 0)
2037 *total = 2;
2038 else
2039 *total = 4;
2040 return true;
2041
2042 case CONST:
2043 case LABEL_REF:
2044 case SYMBOL_REF:
2045 /* These are more costly than a CONST_INT, but we can relax them,
2046 so they're less costly than a CONST_DOUBLE. */
2047 *total = 6;
2048 return true;
2049
2050 case CONST_DOUBLE:
2051 /* We don't optimize CONST_DOUBLEs well nor do we relax them well,
2052 so their cost is very high. */
2053 *total = 8;
2054 return true;
2055
2056 /* ??? This probably needs more work. */
2057 case MOD:
2058 case DIV:
2059 case MULT:
2060 *total = 8;
2061 return true;
2062
2063 default:
2064 return false;
2065 }
2066}
8935d57c 2067
2068/* Check whether a constant used to initialize a DImode or DFmode can
2069 use a clr instruction. The code here must be kept in sync with
2070 movdf and movdi. */
2071
2072bool
3285410a 2073mn10300_wide_const_load_uses_clr (rtx operands[2])
8935d57c 2074{
2075 long val[2];
2076
2077 if (GET_CODE (operands[0]) != REG
2078 || REGNO_REG_CLASS (REGNO (operands[0])) != DATA_REGS)
2079 return false;
2080
2081 switch (GET_CODE (operands[1]))
2082 {
2083 case CONST_INT:
2084 {
2085 rtx low, high;
2086 split_double (operands[1], &low, &high);
2087 val[0] = INTVAL (low);
2088 val[1] = INTVAL (high);
2089 }
2090 break;
fb16c776 2091
8935d57c 2092 case CONST_DOUBLE:
2093 if (GET_MODE (operands[1]) == DFmode)
2094 {
2095 REAL_VALUE_TYPE rv;
2096
2097 REAL_VALUE_FROM_CONST_DOUBLE (rv, operands[1]);
2098 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
2099 }
2100 else if (GET_MODE (operands[1]) == VOIDmode
2101 || GET_MODE (operands[1]) == DImode)
2102 {
2103 val[0] = CONST_DOUBLE_LOW (operands[1]);
2104 val[1] = CONST_DOUBLE_HIGH (operands[1]);
2105 }
2106 break;
fb16c776 2107
8935d57c 2108 default:
2109 return false;
2110 }
2111
2112 return val[0] == 0 || val[1] == 0;
2113}
b87a151a 2114/* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2115 may access it using GOTOFF instead of GOT. */
2116
2117static void
48ed5fc2 2118mn10300_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
b87a151a 2119{
2120 rtx symbol;
2121
2122 if (GET_CODE (rtl) != MEM)
2123 return;
2124 symbol = XEXP (rtl, 0);
2125 if (GET_CODE (symbol) != SYMBOL_REF)
2126 return;
2127
2128 if (flag_pic)
2129 SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2130}