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