]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/mn10300/mn10300.c
re PR sanitizer/68260 (false positive with tsan)
[thirdparty/gcc.git] / gcc / config / mn10300 / mn10300.c
CommitLineData
11bb1f11 1/* Subroutines for insn-output.c for Matsushita MN10300 series
818ab71a 2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
11bb1f11
JL
3 Contributed by Jeff Law (law@cygnus.com).
4
e7ab5593 5 This file is part of GCC.
11bb1f11 6
e7ab5593
NC
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11bb1f11 11
e7ab5593
NC
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
11bb1f11 16
e7ab5593
NC
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
11bb1f11 20
11bb1f11 21#include "config.h"
c5c76735 22#include "system.h"
4977bab6 23#include "coretypes.h"
c7131fb2 24#include "backend.h"
e11c4407 25#include "target.h"
11bb1f11 26#include "rtl.h"
e11c4407
AM
27#include "tree.h"
28#include "cfghooks.h"
29#include "cfgloop.h"
c7131fb2 30#include "df.h"
e11c4407
AM
31#include "tm_p.h"
32#include "optabs.h"
33#include "regs.h"
34#include "emit-rtl.h"
35#include "recog.h"
36#include "diagnostic-core.h"
40e23961 37#include "alias.h"
d8a2d370
DN
38#include "stor-layout.h"
39#include "varasm.h"
40#include "calls.h"
11bb1f11
JL
41#include "output.h"
42#include "insn-attr.h"
6528281d 43#include "reload.h"
36566b39 44#include "explow.h"
11bb1f11 45#include "expr.h"
a45d420a 46#include "tm-constrs.h"
60393bbc 47#include "cfgrtl.h"
7ee2468b 48#include "dumpfile.h"
9b2b7279 49#include "builtins.h"
11bb1f11 50
994c5d85 51/* This file should be included last. */
d58627a0
RS
52#include "target-def.h"
53
2beef00e
AO
54/* This is used in the am33_2.0-linux-gnu port, in which global symbol
55 names are not prefixed by underscores, to tell whether to prefix a
56 label with a plus sign or not, so that the assembler can tell
57 symbol names from register names. */
58int mn10300_protect_label;
59
f3f63737
NC
60/* Selected processor type for tuning. */
61enum processor_type mn10300_tune_cpu = PROCESSOR_DEFAULT;
62
bad41521
RH
63#define CC_FLAG_Z 1
64#define CC_FLAG_N 2
65#define CC_FLAG_C 4
66#define CC_FLAG_V 8
67
ef4bddc2 68static int cc_flags_for_mode(machine_mode);
bad41521 69static int cc_flags_for_code(enum rtx_code);
672a6f42 70\f
c5387660 71/* Implement TARGET_OPTION_OVERRIDE. */
c5387660
JM
72static void
73mn10300_option_override (void)
13dd556c
RS
74{
75 if (TARGET_AM33)
76 target_flags &= ~MASK_MULT_BUG;
f3f63737
NC
77 else
78 {
79 /* Disable scheduling for the MN10300 as we do
80 not have timing information available for it. */
81 flag_schedule_insns = 0;
82 flag_schedule_insns_after_reload = 0;
ec815d65
RH
83
84 /* Force enable splitting of wide types, as otherwise it is trivial
85 to run out of registers. Indeed, this works so well that register
86 allocation problems are now more common *without* optimization,
87 when this flag is not enabled by default. */
88 flag_split_wide_types = 1;
f3f63737 89 }
bad41521 90
f3f63737
NC
91 if (mn10300_tune_string)
92 {
93 if (strcasecmp (mn10300_tune_string, "mn10300") == 0)
94 mn10300_tune_cpu = PROCESSOR_MN10300;
95 else if (strcasecmp (mn10300_tune_string, "am33") == 0)
96 mn10300_tune_cpu = PROCESSOR_AM33;
97 else if (strcasecmp (mn10300_tune_string, "am33-2") == 0)
98 mn10300_tune_cpu = PROCESSOR_AM33_2;
99 else if (strcasecmp (mn10300_tune_string, "am34") == 0)
100 mn10300_tune_cpu = PROCESSOR_AM34;
101 else
102 error ("-mtune= expects mn10300, am33, am33-2, or am34");
103 }
13dd556c
RS
104}
105
1bc7c5b6 106static void
f1777882 107mn10300_file_start (void)
11bb1f11 108{
1bc7c5b6 109 default_file_start ();
705ac34f 110
18e9d2f9
AO
111 if (TARGET_AM33_2)
112 fprintf (asm_out_file, "\t.am33_2\n");
113 else if (TARGET_AM33)
1bc7c5b6 114 fprintf (asm_out_file, "\t.am33\n");
11bb1f11
JL
115}
116\f
298362c8
NC
117/* Note: This list must match the liw_op attribute in mn10300.md. */
118
119static const char *liw_op_names[] =
120{
121 "add", "cmp", "sub", "mov",
122 "and", "or", "xor",
123 "asr", "lsr", "asl",
124 "none", "max"
125};
126
11bb1f11
JL
127/* Print operand X using operand code CODE to assembly language output file
128 FILE. */
129
130void
e7ab5593 131mn10300_print_operand (FILE *file, rtx x, int code)
11bb1f11
JL
132{
133 switch (code)
134 {
298362c8
NC
135 case 'W':
136 {
137 unsigned int liw_op = UINTVAL (x);
bad41521 138
298362c8
NC
139 gcc_assert (TARGET_ALLOW_LIW);
140 gcc_assert (liw_op < LIW_OP_MAX);
141 fputs (liw_op_names[liw_op], file);
11bb1f11 142 break;
298362c8 143 }
bad41521 144
298362c8
NC
145 case 'b':
146 case 'B':
147 {
148 enum rtx_code cmp = GET_CODE (x);
ef4bddc2 149 machine_mode mode = GET_MODE (XEXP (x, 0));
298362c8
NC
150 const char *str;
151 int have_flags;
152
153 if (code == 'B')
154 cmp = reverse_condition (cmp);
155 have_flags = cc_flags_for_mode (mode);
5abc5de9 156
298362c8 157 switch (cmp)
18e9d2f9 158 {
298362c8
NC
159 case NE:
160 str = "ne";
18e9d2f9 161 break;
298362c8
NC
162 case EQ:
163 str = "eq";
164 break;
165 case GE:
166 /* bge is smaller than bnc. */
167 str = (have_flags & CC_FLAG_V ? "ge" : "nc");
168 break;
169 case LT:
170 str = (have_flags & CC_FLAG_V ? "lt" : "ns");
171 break;
172 case GT:
173 str = "gt";
174 break;
175 case LE:
176 str = "le";
177 break;
178 case GEU:
179 str = "cc";
180 break;
181 case GTU:
182 str = "hi";
183 break;
184 case LEU:
185 str = "ls";
186 break;
187 case LTU:
188 str = "cs";
189 break;
190 case ORDERED:
191 str = "lge";
192 break;
193 case UNORDERED:
194 str = "uo";
195 break;
196 case LTGT:
197 str = "lg";
198 break;
199 case UNEQ:
200 str = "ue";
201 break;
202 case UNGE:
203 str = "uge";
204 break;
205 case UNGT:
206 str = "ug";
207 break;
208 case UNLE:
209 str = "ule";
210 break;
211 case UNLT:
212 str = "ul";
18e9d2f9 213 break;
18e9d2f9 214 default:
dc759020 215 gcc_unreachable ();
18e9d2f9 216 }
298362c8
NC
217
218 gcc_checking_assert ((cc_flags_for_code (cmp) & ~have_flags) == 0);
219 fputs (str, file);
220 }
221 break;
222
223 case 'C':
224 /* This is used for the operand to a call instruction;
225 if it's a REG, enclose it in parens, else output
226 the operand normally. */
227 if (REG_P (x))
228 {
229 fputc ('(', file);
230 mn10300_print_operand (file, x, 0);
231 fputc (')', file);
232 }
233 else
234 mn10300_print_operand (file, x, 0);
235 break;
236
237 case 'D':
238 switch (GET_CODE (x))
239 {
240 case MEM:
241 fputc ('(', file);
cc8ca59e 242 output_address (GET_MODE (x), XEXP (x, 0));
298362c8
NC
243 fputc (')', file);
244 break;
245
246 case REG:
247 fprintf (file, "fd%d", REGNO (x) - 18);
248 break;
249
250 default:
251 gcc_unreachable ();
252 }
253 break;
18e9d2f9 254
38c37a0e 255 /* These are the least significant word in a 64bit value. */
298362c8
NC
256 case 'L':
257 switch (GET_CODE (x))
258 {
259 case MEM:
260 fputc ('(', file);
cc8ca59e 261 output_address (GET_MODE (x), XEXP (x, 0));
298362c8
NC
262 fputc (')', file);
263 break;
38c37a0e 264
298362c8
NC
265 case REG:
266 fprintf (file, "%s", reg_names[REGNO (x)]);
267 break;
38c37a0e 268
298362c8
NC
269 case SUBREG:
270 fprintf (file, "%s", reg_names[subreg_regno (x)]);
271 break;
38c37a0e 272
298362c8
NC
273 case CONST_DOUBLE:
274 {
275 long val[2];
38c37a0e 276
298362c8
NC
277 switch (GET_MODE (x))
278 {
279 case DFmode:
34a72c33
RS
280 REAL_VALUE_TO_TARGET_DOUBLE
281 (*CONST_DOUBLE_REAL_VALUE (x), val);
298362c8
NC
282 fprintf (file, "0x%lx", val[0]);
283 break;;
284 case SFmode:
34a72c33
RS
285 REAL_VALUE_TO_TARGET_SINGLE
286 (*CONST_DOUBLE_REAL_VALUE (x), val[0]);
298362c8
NC
287 fprintf (file, "0x%lx", val[0]);
288 break;;
289 case VOIDmode:
290 case DImode:
291 mn10300_print_operand_address (file,
292 GEN_INT (CONST_DOUBLE_LOW (x)));
293 break;
294 default:
38c37a0e
JL
295 break;
296 }
298362c8
NC
297 break;
298 }
38c37a0e 299
298362c8
NC
300 case CONST_INT:
301 {
302 rtx low, high;
303 split_double (x, &low, &high);
304 fprintf (file, "%ld", (long)INTVAL (low));
305 break;
212bc5fa 306 }
38c37a0e 307
298362c8
NC
308 default:
309 gcc_unreachable ();
310 }
311 break;
38c37a0e
JL
312
313 /* Similarly, but for the most significant word. */
298362c8
NC
314 case 'H':
315 switch (GET_CODE (x))
316 {
317 case MEM:
318 fputc ('(', file);
319 x = adjust_address (x, SImode, 4);
cc8ca59e 320 output_address (GET_MODE (x), XEXP (x, 0));
298362c8
NC
321 fputc (')', file);
322 break;
38c37a0e 323
298362c8
NC
324 case REG:
325 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
326 break;
38c37a0e 327
298362c8
NC
328 case SUBREG:
329 fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
330 break;
38c37a0e 331
298362c8
NC
332 case CONST_DOUBLE:
333 {
334 long val[2];
38c37a0e 335
298362c8
NC
336 switch (GET_MODE (x))
337 {
338 case DFmode:
34a72c33
RS
339 REAL_VALUE_TO_TARGET_DOUBLE
340 (*CONST_DOUBLE_REAL_VALUE (x), val);
298362c8
NC
341 fprintf (file, "0x%lx", val[1]);
342 break;;
343 case SFmode:
344 gcc_unreachable ();
345 case VOIDmode:
346 case DImode:
347 mn10300_print_operand_address (file,
348 GEN_INT (CONST_DOUBLE_HIGH (x)));
349 break;
350 default:
38c37a0e
JL
351 break;
352 }
298362c8
NC
353 break;
354 }
38c37a0e 355
298362c8
NC
356 case CONST_INT:
357 {
358 rtx low, high;
359 split_double (x, &low, &high);
360 fprintf (file, "%ld", (long)INTVAL (high));
361 break;
38c37a0e 362 }
38c37a0e 363
298362c8
NC
364 default:
365 gcc_unreachable ();
366 }
367 break;
38c37a0e 368
298362c8
NC
369 case 'A':
370 fputc ('(', file);
371 if (REG_P (XEXP (x, 0)))
cc8ca59e
JB
372 output_address (VOIDmode, gen_rtx_PLUS (SImode,
373 XEXP (x, 0), const0_rtx));
298362c8 374 else
cc8ca59e 375 output_address (VOIDmode, XEXP (x, 0));
298362c8
NC
376 fputc (')', file);
377 break;
a58be199 378
298362c8
NC
379 case 'N':
380 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
381 fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
382 break;
383
384 case 'U':
385 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
386 fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
387 break;
6fafc523 388
576e5acc
JL
389 /* For shift counts. The hardware ignores the upper bits of
390 any immediate, but the assembler will flag an out of range
391 shift count as an error. So we mask off the high bits
392 of the immediate here. */
298362c8
NC
393 case 'S':
394 if (CONST_INT_P (x))
395 {
396 fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
397 break;
398 }
399 /* FALL THROUGH */
576e5acc 400
298362c8
NC
401 default:
402 switch (GET_CODE (x))
403 {
404 case MEM:
405 fputc ('(', file);
cc8ca59e 406 output_address (GET_MODE (x), XEXP (x, 0));
298362c8
NC
407 fputc (')', file);
408 break;
11bb1f11 409
298362c8 410 case PLUS:
cc8ca59e 411 output_address (VOIDmode, x);
298362c8 412 break;
38c37a0e 413
298362c8
NC
414 case REG:
415 fprintf (file, "%s", reg_names[REGNO (x)]);
416 break;
11bb1f11 417
298362c8
NC
418 case SUBREG:
419 fprintf (file, "%s", reg_names[subreg_regno (x)]);
420 break;
11bb1f11 421
38c37a0e 422 /* This will only be single precision.... */
298362c8
NC
423 case CONST_DOUBLE:
424 {
425 unsigned long val;
38c37a0e 426
34a72c33 427 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
298362c8 428 fprintf (file, "0x%lx", val);
11bb1f11 429 break;
11bb1f11 430 }
298362c8
NC
431
432 case CONST_INT:
433 case SYMBOL_REF:
434 case CONST:
435 case LABEL_REF:
436 case CODE_LABEL:
437 case UNSPEC:
438 mn10300_print_operand_address (file, x);
439 break;
440 default:
441 gcc_unreachable ();
442 }
443 break;
444 }
11bb1f11
JL
445}
446
447/* Output assembly language output for the address ADDR to FILE. */
448
449void
e7ab5593 450mn10300_print_operand_address (FILE *file, rtx addr)
11bb1f11
JL
451{
452 switch (GET_CODE (addr))
453 {
705ac34f 454 case POST_INC:
36846b26 455 mn10300_print_operand (file, XEXP (addr, 0), 0);
705ac34f
JL
456 fputc ('+', file);
457 break;
36846b26
RH
458
459 case POST_MODIFY:
460 mn10300_print_operand (file, XEXP (addr, 0), 0);
461 fputc ('+', file);
462 fputc (',', file);
463 mn10300_print_operand (file, XEXP (addr, 1), 0);
464 break;
465
11bb1f11 466 case REG:
e7ab5593 467 mn10300_print_operand (file, addr, 0);
11bb1f11
JL
468 break;
469 case PLUS:
470 {
36846b26
RH
471 rtx base = XEXP (addr, 0);
472 rtx index = XEXP (addr, 1);
473
474 if (REG_P (index) && !REG_OK_FOR_INDEX_P (index))
475 {
476 rtx x = base;
477 base = index;
478 index = x;
479
480 gcc_assert (REG_P (index) && REG_OK_FOR_INDEX_P (index));
481 }
482 gcc_assert (REG_OK_FOR_BASE_P (base));
483
e7ab5593 484 mn10300_print_operand (file, index, 0);
11bb1f11 485 fputc (',', file);
36846b26 486 mn10300_print_operand (file, base, 0);
11bb1f11
JL
487 break;
488 }
489 case SYMBOL_REF:
490 output_addr_const (file, addr);
491 break;
492 default:
493 output_addr_const (file, addr);
494 break;
495 }
496}
497
535bd17c
AS
498/* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.
499
500 Used for PIC-specific UNSPECs. */
501
502static bool
503mn10300_asm_output_addr_const_extra (FILE *file, rtx x)
504{
505 if (GET_CODE (x) == UNSPEC)
506 {
507 switch (XINT (x, 1))
508 {
535bd17c
AS
509 case UNSPEC_PIC:
510 /* GLOBAL_OFFSET_TABLE or local symbols, no suffix. */
511 output_addr_const (file, XVECEXP (x, 0, 0));
512 break;
513 case UNSPEC_GOT:
514 output_addr_const (file, XVECEXP (x, 0, 0));
515 fputs ("@GOT", file);
516 break;
517 case UNSPEC_GOTOFF:
518 output_addr_const (file, XVECEXP (x, 0, 0));
519 fputs ("@GOTOFF", file);
520 break;
521 case UNSPEC_PLT:
522 output_addr_const (file, XVECEXP (x, 0, 0));
523 fputs ("@PLT", file);
524 break;
525 case UNSPEC_GOTSYM_OFF:
526 assemble_name (file, GOT_SYMBOL_NAME);
527 fputs ("-(", file);
528 output_addr_const (file, XVECEXP (x, 0, 0));
529 fputs ("-.)", file);
530 break;
531 default:
532 return false;
533 }
534 return true;
535 }
536 else
537 return false;
538}
539
18e9d2f9
AO
540/* Count the number of FP registers that have to be saved. */
541static int
f1777882 542fp_regs_to_save (void)
18e9d2f9
AO
543{
544 int i, n = 0;
545
546 if (! TARGET_AM33_2)
547 return 0;
548
549 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
9d54866d 550 if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
18e9d2f9
AO
551 ++n;
552
553 return n;
554}
555
f6cd7c62
RS
556/* Print a set of registers in the format required by "movm" and "ret".
557 Register K is saved if bit K of MASK is set. The data and address
558 registers can be stored individually, but the extended registers cannot.
9f5ed61a 559 We assume that the mask already takes that into account. For instance,
8596d0a1 560 bits 14 to 17 must have the same value. */
f6cd7c62
RS
561
562void
f1777882 563mn10300_print_reg_list (FILE *file, int mask)
f6cd7c62
RS
564{
565 int need_comma;
566 int i;
567
568 need_comma = 0;
569 fputc ('[', file);
570
571 for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
572 if ((mask & (1 << i)) != 0)
573 {
574 if (need_comma)
575 fputc (',', file);
576 fputs (reg_names [i], file);
577 need_comma = 1;
578 }
579
580 if ((mask & 0x3c000) != 0)
581 {
dc759020 582 gcc_assert ((mask & 0x3c000) == 0x3c000);
f6cd7c62
RS
583 if (need_comma)
584 fputc (',', file);
585 fputs ("exreg1", file);
586 need_comma = 1;
587 }
588
589 fputc (']', file);
590}
591
37a185d7
RH
592/* If the MDR register is never clobbered, we can use the RETF instruction
593 which takes the address from the MDR register. This is 3 cycles faster
594 than having to load the address from the stack. */
595
596bool
597mn10300_can_use_retf_insn (void)
598{
599 /* Don't bother if we're not optimizing. In this case we won't
600 have proper access to df_regs_ever_live_p. */
601 if (!optimize)
602 return false;
603
604 /* EH returns alter the saved return address; MDR is not current. */
605 if (crtl->calls_eh_return)
606 return false;
607
608 /* Obviously not if MDR is ever clobbered. */
609 if (df_regs_ever_live_p (MDR_REG))
610 return false;
611
612 /* ??? Careful not to use this during expand_epilogue etc. */
613 gcc_assert (!in_sequence_p ());
614 return leaf_function_p ();
615}
616
617bool
618mn10300_can_use_rets_insn (void)
38c37a0e 619{
040c5757 620 return !mn10300_initial_offset (ARG_POINTER_REGNUM, STACK_POINTER_REGNUM);
38c37a0e
JL
621}
622
f6cd7c62
RS
623/* Returns the set of live, callee-saved registers as a bitmask. The
624 callee-saved extended registers cannot be stored individually, so
20b2e6a0 625 all of them will be included in the mask if any one of them is used.
e902c266
NC
626 Also returns the number of bytes in the registers in the mask if
627 BYTES_SAVED is not NULL. */
f6cd7c62 628
e902c266
NC
629unsigned int
630mn10300_get_live_callee_saved_regs (unsigned int * bytes_saved)
f6cd7c62
RS
631{
632 int mask;
633 int i;
e902c266 634 unsigned int count;
f6cd7c62 635
e902c266 636 count = mask = 0;
18e9d2f9 637 for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
9d54866d 638 if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
e902c266
NC
639 {
640 mask |= (1 << i);
641 ++ count;
642 }
643
f6cd7c62 644 if ((mask & 0x3c000) != 0)
e902c266
NC
645 {
646 for (i = 0x04000; i < 0x40000; i <<= 1)
647 if ((mask & i) == 0)
648 ++ count;
649
650 mask |= 0x3c000;
651 }
652
653 if (bytes_saved)
654 * bytes_saved = count * UNITS_PER_WORD;
f6cd7c62
RS
655
656 return mask;
657}
658
2720cc47
NC
659static rtx
660F (rtx r)
661{
662 RTX_FRAME_RELATED_P (r) = 1;
663 return r;
664}
665
f6cd7c62
RS
666/* Generate an instruction that pushes several registers onto the stack.
667 Register K will be saved if bit K in MASK is set. The function does
668 nothing if MASK is zero.
669
670 To be compatible with the "movm" instruction, the lowest-numbered
671 register must be stored in the lowest slot. If MASK is the set
672 { R1,...,RN }, where R1...RN are ordered least first, the generated
673 instruction will have the form:
674
675 (parallel
676 (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
677 (set (mem:SI (plus:SI (reg:SI 9)
678 (const_int -1*4)))
679 (reg:SI RN))
680 ...
681 (set (mem:SI (plus:SI (reg:SI 9)
682 (const_int -N*4)))
683 (reg:SI R1))) */
684
cc909bba
RH
685static void
686mn10300_gen_multiple_store (unsigned int mask)
f6cd7c62 687{
cc909bba
RH
688 /* The order in which registers are stored, from SP-4 through SP-N*4. */
689 static const unsigned int store_order[8] = {
690 /* e2, e3: never saved */
691 FIRST_EXTENDED_REGNUM + 4,
692 FIRST_EXTENDED_REGNUM + 5,
693 FIRST_EXTENDED_REGNUM + 6,
694 FIRST_EXTENDED_REGNUM + 7,
695 /* e0, e1, mdrq, mcrh, mcrl, mcvf: never saved. */
696 FIRST_DATA_REGNUM + 2,
697 FIRST_DATA_REGNUM + 3,
698 FIRST_ADDRESS_REGNUM + 2,
699 FIRST_ADDRESS_REGNUM + 3,
700 /* d0, d1, a0, a1, mdr, lir, lar: never saved. */
701 };
702
703 rtx x, elts[9];
704 unsigned int i;
705 int count;
706
707 if (mask == 0)
708 return;
709
710 for (i = count = 0; i < ARRAY_SIZE(store_order); ++i)
f6cd7c62 711 {
cc909bba
RH
712 unsigned regno = store_order[i];
713
714 if (((mask >> regno) & 1) == 0)
715 continue;
f6cd7c62 716
cc909bba 717 ++count;
0a81f074 718 x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
cc909bba 719 x = gen_frame_mem (SImode, x);
f7df4a84 720 x = gen_rtx_SET (x, gen_rtx_REG (SImode, regno));
cc909bba
RH
721 elts[count] = F(x);
722
723 /* Remove the register from the mask so that... */
724 mask &= ~(1u << regno);
f6cd7c62 725 }
cc909bba
RH
726
727 /* ... we can make sure that we didn't try to use a register
728 not listed in the store order. */
729 gcc_assert (mask == 0);
730
731 /* Create the instruction that updates the stack pointer. */
0a81f074 732 x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
f7df4a84 733 x = gen_rtx_SET (stack_pointer_rtx, x);
cc909bba
RH
734 elts[0] = F(x);
735
736 /* We need one PARALLEL element to update the stack pointer and
737 an additional element for each register that is stored. */
738 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (count + 1, elts));
739 F (emit_insn (x));
f6cd7c62
RS
740}
741
3a22ad89
NC
742static inline unsigned int
743popcount (unsigned int mask)
744{
745 unsigned int count = 0;
746
747 while (mask)
748 {
749 ++ count;
750 mask &= ~ (mask & - mask);
751 }
752 return count;
753}
754
11bb1f11 755void
e7ab5593 756mn10300_expand_prologue (void)
11bb1f11 757{
040c5757 758 HOST_WIDE_INT size = mn10300_frame_size ();
3a22ad89 759 unsigned int mask;
11bb1f11 760
3a22ad89 761 mask = mn10300_get_live_callee_saved_regs (NULL);
8596d0a1 762 /* If we use any of the callee-saved registers, save them now. */
3a22ad89
NC
763 mn10300_gen_multiple_store (mask);
764
765 if (flag_stack_usage_info)
766 current_function_static_stack_size = size + popcount (mask) * 4;
777fbf09 767
18e9d2f9
AO
768 if (TARGET_AM33_2 && fp_regs_to_save ())
769 {
770 int num_regs_to_save = fp_regs_to_save (), i;
771 HOST_WIDE_INT xsize;
e7ab5593
NC
772 enum
773 {
774 save_sp_merge,
775 save_sp_no_merge,
776 save_sp_partial_merge,
777 save_a0_merge,
778 save_a0_no_merge
779 } strategy;
18e9d2f9
AO
780 unsigned int strategy_size = (unsigned)-1, this_strategy_size;
781 rtx reg;
18e9d2f9 782
3a22ad89
NC
783 if (flag_stack_usage_info)
784 current_function_static_stack_size += num_regs_to_save * 4;
785
18e9d2f9
AO
786 /* We have several different strategies to save FP registers.
787 We can store them using SP offsets, which is beneficial if
788 there are just a few registers to save, or we can use `a0' in
789 post-increment mode (`a0' is the only call-clobbered address
790 register that is never used to pass information to a
791 function). Furthermore, if we don't need a frame pointer, we
792 can merge the two SP adds into a single one, but this isn't
793 always beneficial; sometimes we can just split the two adds
794 so that we don't exceed a 16-bit constant size. The code
795 below will select which strategy to use, so as to generate
796 smallest code. Ties are broken in favor or shorter sequences
797 (in terms of number of instructions). */
798
799#define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
800 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
801#define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
802 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
f4a88680
JR
803
804/* We add 0 * (S) in two places to promote to the type of S,
805 so that all arms of the conditional have the same type. */
18e9d2f9 806#define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
f4a88680 807 (((S) >= (L)) ? 0 * (S) + (SIZE1) * (N) \
18e9d2f9
AO
808 : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
809 + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
f4a88680 810 : 0 * (S) + (ELSE))
18e9d2f9
AO
811#define SIZE_FMOV_SP_(S,N) \
812 (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
813 SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
814 (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
815#define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
816
817 /* Consider alternative save_sp_merge only if we don't need the
4375e090 818 frame pointer and size is nonzero. */
18e9d2f9
AO
819 if (! frame_pointer_needed && size)
820 {
821 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
822 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
823 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
824 this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
825
826 if (this_strategy_size < strategy_size)
827 {
828 strategy = save_sp_merge;
829 strategy_size = this_strategy_size;
830 }
831 }
832
833 /* Consider alternative save_sp_no_merge unconditionally. */
834 /* Insn: add -4 * num_regs_to_save, sp. */
835 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
836 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
837 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
838 if (size)
839 {
840 /* Insn: add -size, sp. */
841 this_strategy_size += SIZE_ADD_SP (-size);
842 }
843
844 if (this_strategy_size < strategy_size)
845 {
846 strategy = save_sp_no_merge;
847 strategy_size = this_strategy_size;
848 }
849
850 /* Consider alternative save_sp_partial_merge only if we don't
851 need a frame pointer and size is reasonably large. */
852 if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
853 {
854 /* Insn: add -128, sp. */
855 this_strategy_size = SIZE_ADD_SP (-128);
856 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
857 this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
858 num_regs_to_save);
859 if (size)
860 {
861 /* Insn: add 128-size, sp. */
862 this_strategy_size += SIZE_ADD_SP (128 - size);
863 }
864
865 if (this_strategy_size < strategy_size)
866 {
867 strategy = save_sp_partial_merge;
868 strategy_size = this_strategy_size;
869 }
870 }
871
872 /* Consider alternative save_a0_merge only if we don't need a
4375e090 873 frame pointer, size is nonzero and the user hasn't
18e9d2f9
AO
874 changed the calling conventions of a0. */
875 if (! frame_pointer_needed && size
9d54866d 876 && call_really_used_regs [FIRST_ADDRESS_REGNUM]
18e9d2f9
AO
877 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
878 {
879 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
880 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
881 /* Insn: mov sp, a0. */
882 this_strategy_size++;
883 if (size)
884 {
885 /* Insn: add size, a0. */
886 this_strategy_size += SIZE_ADD_AX (size);
887 }
888 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
889 this_strategy_size += 3 * num_regs_to_save;
890
891 if (this_strategy_size < strategy_size)
892 {
893 strategy = save_a0_merge;
894 strategy_size = this_strategy_size;
895 }
896 }
897
898 /* Consider alternative save_a0_no_merge if the user hasn't
8596d0a1 899 changed the calling conventions of a0. */
9d54866d 900 if (call_really_used_regs [FIRST_ADDRESS_REGNUM]
18e9d2f9
AO
901 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
902 {
903 /* Insn: add -4 * num_regs_to_save, sp. */
904 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
905 /* Insn: mov sp, a0. */
906 this_strategy_size++;
907 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
908 this_strategy_size += 3 * num_regs_to_save;
909 if (size)
910 {
911 /* Insn: add -size, sp. */
912 this_strategy_size += SIZE_ADD_SP (-size);
913 }
914
915 if (this_strategy_size < strategy_size)
916 {
917 strategy = save_a0_no_merge;
918 strategy_size = this_strategy_size;
919 }
920 }
921
922 /* Emit the initial SP add, common to all strategies. */
923 switch (strategy)
924 {
925 case save_sp_no_merge:
926 case save_a0_no_merge:
2720cc47
NC
927 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
928 stack_pointer_rtx,
929 GEN_INT (-4 * num_regs_to_save))));
18e9d2f9
AO
930 xsize = 0;
931 break;
932
933 case save_sp_partial_merge:
2720cc47
NC
934 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
935 stack_pointer_rtx,
936 GEN_INT (-128))));
18e9d2f9
AO
937 xsize = 128 - 4 * num_regs_to_save;
938 size -= xsize;
939 break;
940
941 case save_sp_merge:
942 case save_a0_merge:
2720cc47
NC
943 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
944 stack_pointer_rtx,
945 GEN_INT (-(size + 4 * num_regs_to_save)))));
18e9d2f9 946 /* We'll have to adjust FP register saves according to the
8596d0a1 947 frame size. */
18e9d2f9
AO
948 xsize = size;
949 /* Since we've already created the stack frame, don't do it
8596d0a1 950 again at the end of the function. */
18e9d2f9
AO
951 size = 0;
952 break;
953
954 default:
dc759020 955 gcc_unreachable ();
18e9d2f9 956 }
5abc5de9 957
18e9d2f9
AO
958 /* Now prepare register a0, if we have decided to use it. */
959 switch (strategy)
960 {
961 case save_sp_merge:
962 case save_sp_no_merge:
963 case save_sp_partial_merge:
964 reg = 0;
965 break;
966
967 case save_a0_merge:
968 case save_a0_no_merge:
969 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
2720cc47 970 F (emit_insn (gen_movsi (reg, stack_pointer_rtx)));
18e9d2f9 971 if (xsize)
2720cc47 972 F (emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize))));
18e9d2f9
AO
973 reg = gen_rtx_POST_INC (SImode, reg);
974 break;
5abc5de9 975
18e9d2f9 976 default:
dc759020 977 gcc_unreachable ();
18e9d2f9 978 }
5abc5de9 979
18e9d2f9
AO
980 /* Now actually save the FP registers. */
981 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
9d54866d 982 if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
18e9d2f9
AO
983 {
984 rtx addr;
985
986 if (reg)
987 addr = reg;
988 else
989 {
990 /* If we aren't using `a0', use an SP offset. */
991 if (xsize)
992 {
993 addr = gen_rtx_PLUS (SImode,
994 stack_pointer_rtx,
995 GEN_INT (xsize));
996 }
997 else
998 addr = stack_pointer_rtx;
5abc5de9 999
18e9d2f9
AO
1000 xsize += 4;
1001 }
1002
2720cc47
NC
1003 F (emit_insn (gen_movsf (gen_rtx_MEM (SFmode, addr),
1004 gen_rtx_REG (SFmode, i))));
18e9d2f9
AO
1005 }
1006 }
1007
777fbf09 1008 /* Now put the frame pointer into the frame pointer register. */
11bb1f11 1009 if (frame_pointer_needed)
2720cc47 1010 F (emit_move_insn (frame_pointer_rtx, stack_pointer_rtx));
11bb1f11 1011
777fbf09 1012 /* Allocate stack for this frame. */
11bb1f11 1013 if (size)
2720cc47
NC
1014 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
1015 stack_pointer_rtx,
1016 GEN_INT (-size))));
1017
6fb5fa3c 1018 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
040c5757 1019 emit_insn (gen_load_pic ());
11bb1f11
JL
1020}
1021
1022void
e7ab5593 1023mn10300_expand_epilogue (void)
11bb1f11 1024{
040c5757 1025 HOST_WIDE_INT size = mn10300_frame_size ();
e902c266
NC
1026 unsigned int reg_save_bytes;
1027
1028 mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1029
18e9d2f9
AO
1030 if (TARGET_AM33_2 && fp_regs_to_save ())
1031 {
1032 int num_regs_to_save = fp_regs_to_save (), i;
1033 rtx reg = 0;
1034
1035 /* We have several options to restore FP registers. We could
1036 load them from SP offsets, but, if there are enough FP
1037 registers to restore, we win if we use a post-increment
1038 addressing mode. */
1039
1040 /* If we have a frame pointer, it's the best option, because we
1041 already know it has the value we want. */
1042 if (frame_pointer_needed)
1043 reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
1044 /* Otherwise, we may use `a1', since it's call-clobbered and
1045 it's never used for return values. But only do so if it's
1046 smaller than using SP offsets. */
1047 else
1048 {
1049 enum { restore_sp_post_adjust,
1050 restore_sp_pre_adjust,
1051 restore_sp_partial_adjust,
1052 restore_a1 } strategy;
1053 unsigned int this_strategy_size, strategy_size = (unsigned)-1;
1054
1055 /* Consider using sp offsets before adjusting sp. */
1056 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1057 this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
1058 /* If size is too large, we'll have to adjust SP with an
1059 add. */
37a185d7 1060 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
18e9d2f9
AO
1061 {
1062 /* Insn: add size + 4 * num_regs_to_save, sp. */
1063 this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
1064 }
1065 /* If we don't have to restore any non-FP registers,
1066 we'll be able to save one byte by using rets. */
37a185d7 1067 if (! reg_save_bytes)
18e9d2f9
AO
1068 this_strategy_size--;
1069
1070 if (this_strategy_size < strategy_size)
1071 {
1072 strategy = restore_sp_post_adjust;
1073 strategy_size = this_strategy_size;
1074 }
1075
1076 /* Consider using sp offsets after adjusting sp. */
1077 /* Insn: add size, sp. */
1078 this_strategy_size = SIZE_ADD_SP (size);
1079 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1080 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
1081 /* We're going to use ret to release the FP registers
8596d0a1 1082 save area, so, no savings. */
18e9d2f9
AO
1083
1084 if (this_strategy_size < strategy_size)
1085 {
1086 strategy = restore_sp_pre_adjust;
1087 strategy_size = this_strategy_size;
1088 }
1089
1090 /* Consider using sp offsets after partially adjusting sp.
1091 When size is close to 32Kb, we may be able to adjust SP
1092 with an imm16 add instruction while still using fmov
1093 (d8,sp). */
37a185d7 1094 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
18e9d2f9
AO
1095 {
1096 /* Insn: add size + 4 * num_regs_to_save
37a185d7 1097 + reg_save_bytes - 252,sp. */
18e9d2f9 1098 this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
c81369fa 1099 + (int) reg_save_bytes - 252);
18e9d2f9 1100 /* Insn: fmov (##,sp),fs#, fo each fs# to be restored. */
37a185d7 1101 this_strategy_size += SIZE_FMOV_SP (252 - reg_save_bytes
18e9d2f9
AO
1102 - 4 * num_regs_to_save,
1103 num_regs_to_save);
1104 /* We're going to use ret to release the FP registers
8596d0a1 1105 save area, so, no savings. */
18e9d2f9
AO
1106
1107 if (this_strategy_size < strategy_size)
1108 {
1109 strategy = restore_sp_partial_adjust;
1110 strategy_size = this_strategy_size;
1111 }
1112 }
1113
1114 /* Consider using a1 in post-increment mode, as long as the
1115 user hasn't changed the calling conventions of a1. */
9d54866d 1116 if (call_really_used_regs [FIRST_ADDRESS_REGNUM + 1]
18e9d2f9
AO
1117 && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1118 {
1119 /* Insn: mov sp,a1. */
1120 this_strategy_size = 1;
1121 if (size)
1122 {
1123 /* Insn: add size,a1. */
1124 this_strategy_size += SIZE_ADD_AX (size);
1125 }
1126 /* Insn: fmov (a1+),fs#, for each fs# to be restored. */
1127 this_strategy_size += 3 * num_regs_to_save;
1128 /* If size is large enough, we may be able to save a
1129 couple of bytes. */
37a185d7 1130 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
18e9d2f9
AO
1131 {
1132 /* Insn: mov a1,sp. */
1133 this_strategy_size += 2;
1134 }
1135 /* If we don't have to restore any non-FP registers,
1136 we'll be able to save one byte by using rets. */
37a185d7 1137 if (! reg_save_bytes)
18e9d2f9
AO
1138 this_strategy_size--;
1139
1140 if (this_strategy_size < strategy_size)
1141 {
1142 strategy = restore_a1;
1143 strategy_size = this_strategy_size;
1144 }
1145 }
1146
1147 switch (strategy)
1148 {
1149 case restore_sp_post_adjust:
1150 break;
1151
1152 case restore_sp_pre_adjust:
1153 emit_insn (gen_addsi3 (stack_pointer_rtx,
1154 stack_pointer_rtx,
1155 GEN_INT (size)));
1156 size = 0;
1157 break;
1158
1159 case restore_sp_partial_adjust:
1160 emit_insn (gen_addsi3 (stack_pointer_rtx,
1161 stack_pointer_rtx,
1162 GEN_INT (size + 4 * num_regs_to_save
37a185d7
RH
1163 + reg_save_bytes - 252)));
1164 size = 252 - reg_save_bytes - 4 * num_regs_to_save;
18e9d2f9 1165 break;
5abc5de9 1166
18e9d2f9
AO
1167 case restore_a1:
1168 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1169 emit_insn (gen_movsi (reg, stack_pointer_rtx));
1170 if (size)
1171 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1172 break;
1173
1174 default:
dc759020 1175 gcc_unreachable ();
18e9d2f9
AO
1176 }
1177 }
1178
1179 /* Adjust the selected register, if any, for post-increment. */
1180 if (reg)
1181 reg = gen_rtx_POST_INC (SImode, reg);
1182
1183 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
9d54866d 1184 if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
18e9d2f9
AO
1185 {
1186 rtx addr;
5abc5de9 1187
18e9d2f9
AO
1188 if (reg)
1189 addr = reg;
1190 else if (size)
1191 {
1192 /* If we aren't using a post-increment register, use an
8596d0a1 1193 SP offset. */
18e9d2f9
AO
1194 addr = gen_rtx_PLUS (SImode,
1195 stack_pointer_rtx,
1196 GEN_INT (size));
1197 }
1198 else
1199 addr = stack_pointer_rtx;
1200
1201 size += 4;
1202
2720cc47
NC
1203 emit_insn (gen_movsf (gen_rtx_REG (SFmode, i),
1204 gen_rtx_MEM (SFmode, addr)));
18e9d2f9
AO
1205 }
1206
1207 /* If we were using the restore_a1 strategy and the number of
1208 bytes to be released won't fit in the `ret' byte, copy `a1'
1209 to `sp', to avoid having to use `add' to adjust it. */
37a185d7 1210 if (! frame_pointer_needed && reg && size + reg_save_bytes > 255)
18e9d2f9
AO
1211 {
1212 emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1213 size = 0;
1214 }
1215 }
1216
5d29a95f
JL
1217 /* Maybe cut back the stack, except for the register save area.
1218
1219 If the frame pointer exists, then use the frame pointer to
1220 cut back the stack.
1221
1222 If the stack size + register save area is more than 255 bytes,
1223 then the stack must be cut back here since the size + register
5abc5de9 1224 save size is too big for a ret/retf instruction.
5d29a95f
JL
1225
1226 Else leave it alone, it will be cut back as part of the
1227 ret/retf instruction, or there wasn't any stack to begin with.
1228
dab66575 1229 Under no circumstances should the register save area be
5d29a95f
JL
1230 deallocated here, that would leave a window where an interrupt
1231 could occur and trash the register save area. */
11bb1f11
JL
1232 if (frame_pointer_needed)
1233 {
11bb1f11 1234 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
4246e0c5
JL
1235 size = 0;
1236 }
37a185d7 1237 else if (size + reg_save_bytes > 255)
4246e0c5
JL
1238 {
1239 emit_insn (gen_addsi3 (stack_pointer_rtx,
1240 stack_pointer_rtx,
1241 GEN_INT (size)));
1242 size = 0;
11bb1f11 1243 }
11bb1f11 1244
ed6089d6 1245 /* Adjust the stack and restore callee-saved registers, if any. */
37a185d7 1246 if (mn10300_can_use_rets_insn ())
3810076b 1247 emit_jump_insn (ret_rtx);
777fbf09 1248 else
e902c266 1249 emit_jump_insn (gen_return_ret (GEN_INT (size + reg_save_bytes)));
11bb1f11
JL
1250}
1251
05713b80 1252/* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
f6cd7c62
RS
1253 This function is for MATCH_PARALLEL and so assumes OP is known to be
1254 parallel. If OP is a multiple store, return a mask indicating which
1255 registers it saves. Return 0 otherwise. */
1256
c345a0b1
NC
1257unsigned int
1258mn10300_store_multiple_regs (rtx op)
f6cd7c62
RS
1259{
1260 int count;
1261 int mask;
1262 int i;
1263 unsigned int last;
1264 rtx elt;
1265
1266 count = XVECLEN (op, 0);
1267 if (count < 2)
1268 return 0;
1269
1270 /* Check that first instruction has the form (set (sp) (plus A B)) */
1271 elt = XVECEXP (op, 0, 0);
1272 if (GET_CODE (elt) != SET
e7ab5593 1273 || (! REG_P (SET_DEST (elt)))
f6cd7c62
RS
1274 || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1275 || GET_CODE (SET_SRC (elt)) != PLUS)
1276 return 0;
1277
1278 /* Check that A is the stack pointer and B is the expected stack size.
1279 For OP to match, each subsequent instruction should push a word onto
1280 the stack. We therefore expect the first instruction to create
8596d0a1 1281 COUNT-1 stack slots. */
f6cd7c62 1282 elt = SET_SRC (elt);
e7ab5593 1283 if ((! REG_P (XEXP (elt, 0)))
f6cd7c62 1284 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
e7ab5593 1285 || (! CONST_INT_P (XEXP (elt, 1)))
f6cd7c62
RS
1286 || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1287 return 0;
1288
f6cd7c62
RS
1289 mask = 0;
1290 for (i = 1; i < count; i++)
1291 {
cc909bba
RH
1292 /* Check that element i is a (set (mem M) R). */
1293 /* ??? Validate the register order a-la mn10300_gen_multiple_store.
1294 Remember: the ordering is *not* monotonic. */
f6cd7c62
RS
1295 elt = XVECEXP (op, 0, i);
1296 if (GET_CODE (elt) != SET
e7ab5593 1297 || (! MEM_P (SET_DEST (elt)))
cc909bba 1298 || (! REG_P (SET_SRC (elt))))
f6cd7c62
RS
1299 return 0;
1300
cc909bba 1301 /* Remember which registers are to be saved. */
f6cd7c62
RS
1302 last = REGNO (SET_SRC (elt));
1303 mask |= (1 << last);
1304
1305 /* Check that M has the form (plus (sp) (const_int -I*4)) */
1306 elt = XEXP (SET_DEST (elt), 0);
1307 if (GET_CODE (elt) != PLUS
e7ab5593 1308 || (! REG_P (XEXP (elt, 0)))
f6cd7c62 1309 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
e7ab5593 1310 || (! CONST_INT_P (XEXP (elt, 1)))
f6cd7c62
RS
1311 || INTVAL (XEXP (elt, 1)) != -i * 4)
1312 return 0;
1313 }
1314
8596d0a1 1315 /* All or none of the callee-saved extended registers must be in the set. */
f6cd7c62
RS
1316 if ((mask & 0x3c000) != 0
1317 && (mask & 0x3c000) != 0x3c000)
1318 return 0;
1319
1320 return mask;
1321}
1322
f2831cc9
AS
1323/* Implement TARGET_PREFERRED_RELOAD_CLASS. */
1324
1325static reg_class_t
1326mn10300_preferred_reload_class (rtx x, reg_class_t rclass)
1327{
1328 if (x == stack_pointer_rtx && rclass != SP_REGS)
8b119bb6 1329 return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
f2831cc9
AS
1330 else if (MEM_P (x)
1331 || (REG_P (x)
1332 && !HARD_REGISTER_P (x))
1333 || (GET_CODE (x) == SUBREG
1334 && REG_P (SUBREG_REG (x))
1335 && !HARD_REGISTER_P (SUBREG_REG (x))))
1336 return LIMIT_RELOAD_CLASS (GET_MODE (x), rclass);
1337 else
1338 return rclass;
1339}
1340
1341/* Implement TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
1342
1343static reg_class_t
1344mn10300_preferred_output_reload_class (rtx x, reg_class_t rclass)
1345{
1346 if (x == stack_pointer_rtx && rclass != SP_REGS)
8b119bb6 1347 return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
f2831cc9
AS
1348 return rclass;
1349}
1350
8b119bb6 1351/* Implement TARGET_SECONDARY_RELOAD. */
e7ab5593 1352
8b119bb6
RH
1353static reg_class_t
1354mn10300_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
ef4bddc2 1355 machine_mode mode, secondary_reload_info *sri)
11bb1f11 1356{
8b119bb6
RH
1357 enum reg_class rclass = (enum reg_class) rclass_i;
1358 enum reg_class xclass = NO_REGS;
1359 unsigned int xregno = INVALID_REGNUM;
1360
1361 if (REG_P (x))
4d1a91c2 1362 {
8b119bb6
RH
1363 xregno = REGNO (x);
1364 if (xregno >= FIRST_PSEUDO_REGISTER)
1365 xregno = true_regnum (x);
1366 if (xregno != INVALID_REGNUM)
1367 xclass = REGNO_REG_CLASS (xregno);
1368 }
1369
1370 if (!TARGET_AM33)
1371 {
1372 /* Memory load/stores less than a full word wide can't have an
1373 address or stack pointer destination. They must use a data
1374 register as an intermediate register. */
1375 if (rclass != DATA_REGS
1376 && (mode == QImode || mode == HImode)
1377 && xclass == NO_REGS)
1378 return DATA_REGS;
1379
1380 /* We can only move SP to/from an address register. */
1381 if (in_p
1382 && rclass == SP_REGS
1383 && xclass != ADDRESS_REGS)
1384 return ADDRESS_REGS;
1385 if (!in_p
1386 && xclass == SP_REGS
1387 && rclass != ADDRESS_REGS
1388 && rclass != SP_OR_ADDRESS_REGS)
1389 return ADDRESS_REGS;
4d1a91c2 1390 }
11bb1f11 1391
8b119bb6
RH
1392 /* We can't directly load sp + const_int into a register;
1393 we must use an address register as an scratch. */
1394 if (in_p
1395 && rclass != SP_REGS
0a2aaacc 1396 && rclass != SP_OR_ADDRESS_REGS
36846b26 1397 && rclass != SP_OR_GENERAL_REGS
8b119bb6
RH
1398 && GET_CODE (x) == PLUS
1399 && (XEXP (x, 0) == stack_pointer_rtx
1400 || XEXP (x, 1) == stack_pointer_rtx))
1401 {
1402 sri->icode = CODE_FOR_reload_plus_sp_const;
1403 return NO_REGS;
1404 }
11bb1f11 1405
c25a21f5
RH
1406 /* We can only move MDR to/from a data register. */
1407 if (rclass == MDR_REGS && xclass != DATA_REGS)
1408 return DATA_REGS;
1409 if (xclass == MDR_REGS && rclass != DATA_REGS)
1410 return DATA_REGS;
1411
8b119bb6 1412 /* We can't load/store an FP register from a constant address. */
6528281d 1413 if (TARGET_AM33_2
8b119bb6
RH
1414 && (rclass == FP_REGS || xclass == FP_REGS)
1415 && (xclass == NO_REGS || rclass == NO_REGS))
18e9d2f9 1416 {
8b119bb6
RH
1417 rtx addr = NULL;
1418
1419 if (xregno >= FIRST_PSEUDO_REGISTER && xregno != INVALID_REGNUM)
1420 {
f2034d06 1421 addr = reg_equiv_mem (xregno);
8b119bb6
RH
1422 if (addr)
1423 addr = XEXP (addr, 0);
1424 }
1425 else if (MEM_P (x))
1426 addr = XEXP (x, 0);
6528281d 1427
8b119bb6 1428 if (addr && CONSTANT_ADDRESS_P (addr))
36846b26 1429 return GENERAL_REGS;
18e9d2f9 1430 }
777fbf09
JL
1431 /* Otherwise assume no secondary reloads are needed. */
1432 return NO_REGS;
1433}
1434
040c5757
RH
1435int
1436mn10300_frame_size (void)
1437{
1438 /* size includes the fixed stack space needed for function calls. */
1439 int size = get_frame_size () + crtl->outgoing_args_size;
1440
1441 /* And space for the return pointer. */
1442 size += crtl->outgoing_args_size ? 4 : 0;
1443
1444 return size;
1445}
1446
777fbf09 1447int
e7ab5593 1448mn10300_initial_offset (int from, int to)
777fbf09 1449{
040c5757
RH
1450 int diff = 0;
1451
1452 gcc_assert (from == ARG_POINTER_REGNUM || from == FRAME_POINTER_REGNUM);
1453 gcc_assert (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
1454
1455 if (to == STACK_POINTER_REGNUM)
1456 diff = mn10300_frame_size ();
1457
3dbc43d1
JL
1458 /* The difference between the argument pointer and the frame pointer
1459 is the size of the callee register save area. */
040c5757 1460 if (from == ARG_POINTER_REGNUM)
11bb1f11 1461 {
e902c266
NC
1462 unsigned int reg_save_bytes;
1463
1464 mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1465 diff += reg_save_bytes;
040c5757 1466 diff += 4 * fp_regs_to_save ();
11bb1f11
JL
1467 }
1468
040c5757 1469 return diff;
11bb1f11 1470}
22ef4e9b 1471
bd5bd7ac
KH
1472/* Worker function for TARGET_RETURN_IN_MEMORY. */
1473
9024ea92 1474static bool
586de218 1475mn10300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
9024ea92
KH
1476{
1477 /* Return values > 8 bytes in length in memory. */
b1eb8119
DD
1478 return (int_size_in_bytes (type) > 8
1479 || int_size_in_bytes (type) == 0
1480 || TYPE_MODE (type) == BLKmode);
9024ea92
KH
1481}
1482
22ef4e9b
JL
1483/* Flush the argument registers to the stack for a stdarg function;
1484 return the new argument pointer. */
9024ea92 1485static rtx
f1777882 1486mn10300_builtin_saveregs (void)
22ef4e9b 1487{
fc2acc87 1488 rtx offset, mem;
22ef4e9b 1489 tree fntype = TREE_TYPE (current_function_decl);
f38958e8 1490 int argadj = ((!stdarg_p (fntype))
22ef4e9b 1491 ? UNITS_PER_WORD : 0);
4862826d 1492 alias_set_type set = get_varargs_alias_set ();
22ef4e9b
JL
1493
1494 if (argadj)
0a81f074 1495 offset = plus_constant (Pmode, crtl->args.arg_offset_rtx, argadj);
22ef4e9b 1496 else
38173d38 1497 offset = crtl->args.arg_offset_rtx;
22ef4e9b 1498
38173d38 1499 mem = gen_rtx_MEM (SImode, crtl->args.internal_arg_pointer);
ba4828e0 1500 set_mem_alias_set (mem, set);
fc2acc87
RH
1501 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1502
1503 mem = gen_rtx_MEM (SImode,
0a81f074
RS
1504 plus_constant (Pmode,
1505 crtl->args.internal_arg_pointer, 4));
ba4828e0 1506 set_mem_alias_set (mem, set);
fc2acc87
RH
1507 emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1508
22ef4e9b 1509 return copy_to_reg (expand_binop (Pmode, add_optab,
38173d38 1510 crtl->args.internal_arg_pointer,
22ef4e9b
JL
1511 offset, 0, 0, OPTAB_LIB_WIDEN));
1512}
1513
d7bd8aeb 1514static void
f1777882 1515mn10300_va_start (tree valist, rtx nextarg)
fc2acc87 1516{
6c535c69 1517 nextarg = expand_builtin_saveregs ();
e5faf155 1518 std_expand_builtin_va_start (valist, nextarg);
fc2acc87
RH
1519}
1520
8cd5a4e0
RH
1521/* Return true when a parameter should be passed by reference. */
1522
1523static bool
d5cc9181 1524mn10300_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
ef4bddc2 1525 machine_mode mode, const_tree type,
8cd5a4e0
RH
1526 bool named ATTRIBUTE_UNUSED)
1527{
1528 unsigned HOST_WIDE_INT size;
1529
1530 if (type)
1531 size = int_size_in_bytes (type);
1532 else
1533 size = GET_MODE_SIZE (mode);
1534
b1eb8119 1535 return (size > 8 || size == 0);
8cd5a4e0
RH
1536}
1537
22ef4e9b 1538/* Return an RTX to represent where a value with mode MODE will be returned
990dc016 1539 from a function. If the result is NULL_RTX, the argument is pushed. */
22ef4e9b 1540
ce236858 1541static rtx
ef4bddc2 1542mn10300_function_arg (cumulative_args_t cum_v, machine_mode mode,
ce236858 1543 const_tree type, bool named ATTRIBUTE_UNUSED)
22ef4e9b 1544{
d5cc9181 1545 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
990dc016 1546 rtx result = NULL_RTX;
f4a88680 1547 int size;
22ef4e9b
JL
1548
1549 /* We only support using 2 data registers as argument registers. */
1550 int nregs = 2;
1551
1552 /* Figure out the size of the object to be passed. */
1553 if (mode == BLKmode)
1554 size = int_size_in_bytes (type);
1555 else
1556 size = GET_MODE_SIZE (mode);
1557
22ef4e9b
JL
1558 cum->nbytes = (cum->nbytes + 3) & ~3;
1559
1560 /* Don't pass this arg via a register if all the argument registers
1561 are used up. */
1562 if (cum->nbytes > nregs * UNITS_PER_WORD)
990dc016 1563 return result;
22ef4e9b
JL
1564
1565 /* Don't pass this arg via a register if it would be split between
1566 registers and memory. */
1567 if (type == NULL_TREE
1568 && cum->nbytes + size > nregs * UNITS_PER_WORD)
990dc016 1569 return result;
22ef4e9b
JL
1570
1571 switch (cum->nbytes / UNITS_PER_WORD)
1572 {
1573 case 0:
990dc016 1574 result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM);
22ef4e9b
JL
1575 break;
1576 case 1:
990dc016 1577 result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM + 1);
22ef4e9b
JL
1578 break;
1579 default:
990dc016 1580 break;
22ef4e9b
JL
1581 }
1582
1583 return result;
1584}
1585
ce236858
NF
1586/* Update the data in CUM to advance over an argument
1587 of mode MODE and data type TYPE.
1588 (TYPE is null for libcalls where that information may not be available.) */
1589
1590static void
ef4bddc2 1591mn10300_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
ce236858
NF
1592 const_tree type, bool named ATTRIBUTE_UNUSED)
1593{
d5cc9181
JR
1594 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1595
ce236858
NF
1596 cum->nbytes += (mode != BLKmode
1597 ? (GET_MODE_SIZE (mode) + 3) & ~3
1598 : (int_size_in_bytes (type) + 3) & ~3);
1599}
1600
78a52f11
RH
1601/* Return the number of bytes of registers to use for an argument passed
1602 partially in registers and partially in memory. */
22ef4e9b 1603
78a52f11 1604static int
ef4bddc2 1605mn10300_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
78a52f11 1606 tree type, bool named ATTRIBUTE_UNUSED)
22ef4e9b 1607{
d5cc9181 1608 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
f4a88680 1609 int size;
22ef4e9b
JL
1610
1611 /* We only support using 2 data registers as argument registers. */
1612 int nregs = 2;
1613
1614 /* Figure out the size of the object to be passed. */
1615 if (mode == BLKmode)
1616 size = int_size_in_bytes (type);
1617 else
1618 size = GET_MODE_SIZE (mode);
1619
22ef4e9b
JL
1620 cum->nbytes = (cum->nbytes + 3) & ~3;
1621
1622 /* Don't pass this arg via a register if all the argument registers
1623 are used up. */
1624 if (cum->nbytes > nregs * UNITS_PER_WORD)
1625 return 0;
1626
1627 if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1628 return 0;
1629
1630 /* Don't pass this arg via a register if it would be split between
1631 registers and memory. */
1632 if (type == NULL_TREE
1633 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1634 return 0;
1635
78a52f11 1636 return nregs * UNITS_PER_WORD - cum->nbytes;
22ef4e9b
JL
1637}
1638
b1eb8119
DD
1639/* Return the location of the function's value. This will be either
1640 $d0 for integer functions, $a0 for pointers, or a PARALLEL of both
1641 $d0 and $a0 if the -mreturn-pointer-on-do flag is set. Note that
1642 we only return the PARALLEL for outgoing values; we do not want
1643 callers relying on this extra copy. */
1644
34732b0a
AS
1645static rtx
1646mn10300_function_value (const_tree valtype,
1647 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1648 bool outgoing)
b1eb8119
DD
1649{
1650 rtx rv;
ef4bddc2 1651 machine_mode mode = TYPE_MODE (valtype);
b1eb8119
DD
1652
1653 if (! POINTER_TYPE_P (valtype))
1654 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1655 else if (! TARGET_PTR_A0D0 || ! outgoing
e3b5732b 1656 || cfun->returns_struct)
b1eb8119
DD
1657 return gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM);
1658
1659 rv = gen_rtx_PARALLEL (mode, rtvec_alloc (2));
1660 XVECEXP (rv, 0, 0)
1661 = gen_rtx_EXPR_LIST (VOIDmode,
1662 gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM),
1663 GEN_INT (0));
5abc5de9 1664
b1eb8119
DD
1665 XVECEXP (rv, 0, 1)
1666 = gen_rtx_EXPR_LIST (VOIDmode,
1667 gen_rtx_REG (mode, FIRST_DATA_REGNUM),
1668 GEN_INT (0));
1669 return rv;
1670}
1671
34732b0a
AS
1672/* Implements TARGET_LIBCALL_VALUE. */
1673
1674static rtx
ef4bddc2 1675mn10300_libcall_value (machine_mode mode,
34732b0a
AS
1676 const_rtx fun ATTRIBUTE_UNUSED)
1677{
1678 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1679}
1680
1681/* Implements FUNCTION_VALUE_REGNO_P. */
1682
1683bool
1684mn10300_function_value_regno_p (const unsigned int regno)
1685{
1686 return (regno == FIRST_DATA_REGNUM || regno == FIRST_ADDRESS_REGNUM);
1687}
1688
bad41521 1689/* Output an addition operation. */
4af476d7 1690
1943c2c1 1691const char *
bad41521 1692mn10300_output_add (rtx operands[3], bool need_flags)
22ef4e9b 1693{
bad41521
RH
1694 rtx dest, src1, src2;
1695 unsigned int dest_regnum, src1_regnum, src2_regnum;
1696 enum reg_class src1_class, src2_class, dest_class;
22ef4e9b 1697
bad41521
RH
1698 dest = operands[0];
1699 src1 = operands[1];
1700 src2 = operands[2];
22ef4e9b 1701
bad41521
RH
1702 dest_regnum = true_regnum (dest);
1703 src1_regnum = true_regnum (src1);
22ef4e9b 1704
bad41521
RH
1705 dest_class = REGNO_REG_CLASS (dest_regnum);
1706 src1_class = REGNO_REG_CLASS (src1_regnum);
22ef4e9b 1707
298362c8 1708 if (CONST_INT_P (src2))
bad41521
RH
1709 {
1710 gcc_assert (dest_regnum == src1_regnum);
22ef4e9b 1711
bad41521
RH
1712 if (src2 == const1_rtx && !need_flags)
1713 return "inc %0";
1714 if (INTVAL (src2) == 4 && !need_flags && dest_class != DATA_REGS)
1715 return "inc4 %0";
705ac34f 1716
bad41521
RH
1717 gcc_assert (!need_flags || dest_class != SP_REGS);
1718 return "add %2,%0";
1719 }
1720 else if (CONSTANT_P (src2))
1721 return "add %2,%0";
1722
1723 src2_regnum = true_regnum (src2);
1724 src2_class = REGNO_REG_CLASS (src2_regnum);
1725
1726 if (dest_regnum == src1_regnum)
1727 return "add %2,%0";
1728 if (dest_regnum == src2_regnum)
1729 return "add %1,%0";
1730
1731 /* The rest of the cases are reg = reg+reg. For AM33, we can implement
1732 this directly, as below, but when optimizing for space we can sometimes
1733 do better by using a mov+add. For MN103, we claimed that we could
1734 implement a three-operand add because the various move and add insns
1735 change sizes across register classes, and we can often do better than
1736 reload in choosing which operand to move. */
1737 if (TARGET_AM33 && optimize_insn_for_speed_p ())
1738 return "add %2,%1,%0";
1739
1740 /* Catch cases where no extended register was used. */
1741 if (src1_class != EXTENDED_REGS
1742 && src2_class != EXTENDED_REGS
1743 && dest_class != EXTENDED_REGS)
1744 {
1745 /* We have to copy one of the sources into the destination, then
1746 add the other source to the destination.
1747
1748 Carefully select which source to copy to the destination; a
1749 naive implementation will waste a byte when the source classes
1750 are different and the destination is an address register.
1751 Selecting the lowest cost register copy will optimize this
1752 sequence. */
1753 if (src1_class == dest_class)
1754 return "mov %1,%0\n\tadd %2,%0";
1755 else
1756 return "mov %2,%0\n\tadd %1,%0";
1757 }
705ac34f 1758
bad41521 1759 /* At least one register is an extended register. */
22ef4e9b 1760
bad41521
RH
1761 /* The three operand add instruction on the am33 is a win iff the
1762 output register is an extended register, or if both source
1763 registers are extended registers. */
1764 if (dest_class == EXTENDED_REGS || src1_class == src2_class)
1765 return "add %2,%1,%0";
1766
1767 /* It is better to copy one of the sources to the destination, then
1768 perform a 2 address add. The destination in this case must be
1769 an address or data register and one of the sources must be an
1770 extended register and the remaining source must not be an extended
1771 register.
1772
1773 The best code for this case is to copy the extended reg to the
1774 destination, then emit a two address add. */
1775 if (src1_class == EXTENDED_REGS)
1776 return "mov %1,%0\n\tadd %2,%0";
1777 else
1778 return "mov %2,%0\n\tadd %1,%0";
22ef4e9b 1779}
460f4b9d 1780
e9ad4573
JL
1781/* Return 1 if X contains a symbolic expression. We know these
1782 expressions will have one of a few well defined forms, so
1783 we need only check those forms. */
e7ab5593 1784
e9ad4573 1785int
e7ab5593 1786mn10300_symbolic_operand (rtx op,
ef4bddc2 1787 machine_mode mode ATTRIBUTE_UNUSED)
e9ad4573
JL
1788{
1789 switch (GET_CODE (op))
1790 {
1791 case SYMBOL_REF:
1792 case LABEL_REF:
1793 return 1;
1794 case CONST:
1795 op = XEXP (op, 0);
1796 return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1797 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
f3f63737 1798 && CONST_INT_P (XEXP (op, 1)));
e9ad4573
JL
1799 default:
1800 return 0;
1801 }
1802}
1803
1804/* Try machine dependent ways of modifying an illegitimate address
1805 to be legitimate. If we find one, return the new valid address.
1806 This macro is used in only one place: `memory_address' in explow.c.
1807
1808 OLDX is the address as it was before break_out_memory_refs was called.
1809 In some cases it is useful to look at this to decide what needs to be done.
1810
e9ad4573
JL
1811 Normally it is always safe for this macro to do nothing. It exists to
1812 recognize opportunities to optimize the output.
1813
1814 But on a few ports with segmented architectures and indexed addressing
1815 (mn10300, hppa) it is used to rewrite certain problematical addresses. */
e7ab5593 1816
4af476d7 1817static rtx
506d7b68 1818mn10300_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
ef4bddc2 1819 machine_mode mode ATTRIBUTE_UNUSED)
e9ad4573 1820{
e7ab5593
NC
1821 if (flag_pic && ! mn10300_legitimate_pic_operand_p (x))
1822 x = mn10300_legitimize_pic_address (oldx, NULL_RTX);
d1776069 1823
e9ad4573
JL
1824 /* Uh-oh. We might have an address for x[n-100000]. This needs
1825 special handling to avoid creating an indexed memory address
1826 with x-100000 as the base. */
1827 if (GET_CODE (x) == PLUS
e7ab5593 1828 && mn10300_symbolic_operand (XEXP (x, 1), VOIDmode))
e9ad4573
JL
1829 {
1830 /* Ugly. We modify things here so that the address offset specified
1831 by the index expression is computed first, then added to x to form
1832 the entire address. */
1833
69bc71fa 1834 rtx regx1, regy1, regy2, y;
e9ad4573
JL
1835
1836 /* Strip off any CONST. */
1837 y = XEXP (x, 1);
1838 if (GET_CODE (y) == CONST)
1839 y = XEXP (y, 0);
1840
bf4219f0
JL
1841 if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1842 {
1843 regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1844 regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1845 regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1846 regx1 = force_reg (Pmode,
e7ab5593
NC
1847 gen_rtx_fmt_ee (GET_CODE (y), Pmode, regx1,
1848 regy2));
c5c76735 1849 return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
bf4219f0 1850 }
e9ad4573 1851 }
371036e0 1852 return x;
e9ad4573 1853}
460ad325 1854
d1776069 1855/* Convert a non-PIC address in `orig' to a PIC address using @GOT or
8596d0a1 1856 @GOTOFF in `reg'. */
e7ab5593 1857
d1776069 1858rtx
e7ab5593 1859mn10300_legitimize_pic_address (rtx orig, rtx reg)
d1776069 1860{
53855940
RH
1861 rtx x;
1862
d1776069
AO
1863 if (GET_CODE (orig) == LABEL_REF
1864 || (GET_CODE (orig) == SYMBOL_REF
1865 && (CONSTANT_POOL_ADDRESS_P (orig)
1866 || ! MN10300_GLOBAL_P (orig))))
1867 {
53855940 1868 if (reg == NULL)
d1776069
AO
1869 reg = gen_reg_rtx (Pmode);
1870
53855940
RH
1871 x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOTOFF);
1872 x = gen_rtx_CONST (SImode, x);
1873 emit_move_insn (reg, x);
1874
1875 x = emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
d1776069
AO
1876 }
1877 else if (GET_CODE (orig) == SYMBOL_REF)
1878 {
53855940 1879 if (reg == NULL)
d1776069
AO
1880 reg = gen_reg_rtx (Pmode);
1881
53855940
RH
1882 x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOT);
1883 x = gen_rtx_CONST (SImode, x);
1884 x = gen_rtx_PLUS (SImode, pic_offset_table_rtx, x);
1885 x = gen_const_mem (SImode, x);
1886
1887 x = emit_move_insn (reg, x);
d1776069 1888 }
53855940
RH
1889 else
1890 return orig;
1891
1892 set_unique_reg_note (x, REG_EQUAL, orig);
1893 return reg;
d1776069
AO
1894}
1895
1896/* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
4375e090 1897 isn't protected by a PIC unspec; nonzero otherwise. */
e7ab5593 1898
d1776069 1899int
e7ab5593 1900mn10300_legitimate_pic_operand_p (rtx x)
d1776069 1901{
e7ab5593
NC
1902 const char *fmt;
1903 int i;
d1776069
AO
1904
1905 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1906 return 0;
1907
1908 if (GET_CODE (x) == UNSPEC
1909 && (XINT (x, 1) == UNSPEC_PIC
1910 || XINT (x, 1) == UNSPEC_GOT
1911 || XINT (x, 1) == UNSPEC_GOTOFF
d4e2d7d2
RS
1912 || XINT (x, 1) == UNSPEC_PLT
1913 || XINT (x, 1) == UNSPEC_GOTSYM_OFF))
d1776069
AO
1914 return 1;
1915
d1776069
AO
1916 fmt = GET_RTX_FORMAT (GET_CODE (x));
1917 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1918 {
1919 if (fmt[i] == 'E')
1920 {
4af476d7 1921 int j;
d1776069
AO
1922
1923 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
e7ab5593 1924 if (! mn10300_legitimate_pic_operand_p (XVECEXP (x, i, j)))
d1776069
AO
1925 return 0;
1926 }
e7ab5593
NC
1927 else if (fmt[i] == 'e'
1928 && ! mn10300_legitimate_pic_operand_p (XEXP (x, i)))
d1776069
AO
1929 return 0;
1930 }
1931
1932 return 1;
1933}
1934
e733134f 1935/* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
c6c3dba9
PB
1936 legitimate, and FALSE otherwise.
1937
1938 On the mn10300, the value in the address register must be
1939 in the same memory space/segment as the effective address.
1940
1941 This is problematical for reload since it does not understand
1942 that base+index != index+base in a memory reference.
1943
1944 Note it is still possible to use reg+reg addressing modes,
1945 it's just much more difficult. For a discussion of a possible
1946 workaround and solution, see the comments in pa.c before the
1947 function record_unscaled_index_insn_codes. */
1948
4af476d7 1949static bool
ef4bddc2 1950mn10300_legitimate_address_p (machine_mode mode, rtx x, bool strict)
e733134f 1951{
36846b26
RH
1952 rtx base, index;
1953
1954 if (CONSTANT_ADDRESS_P (x))
1955 return !flag_pic || mn10300_legitimate_pic_operand_p (x);
e733134f
AO
1956
1957 if (RTX_OK_FOR_BASE_P (x, strict))
36846b26
RH
1958 return true;
1959
1960 if (TARGET_AM33 && (mode == SImode || mode == SFmode || mode == HImode))
1961 {
1962 if (GET_CODE (x) == POST_INC)
1963 return RTX_OK_FOR_BASE_P (XEXP (x, 0), strict);
1964 if (GET_CODE (x) == POST_MODIFY)
1965 return (RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1966 && CONSTANT_ADDRESS_P (XEXP (x, 1)));
1967 }
1968
1969 if (GET_CODE (x) != PLUS)
1970 return false;
e733134f 1971
36846b26
RH
1972 base = XEXP (x, 0);
1973 index = XEXP (x, 1);
e733134f 1974
36846b26
RH
1975 if (!REG_P (base))
1976 return false;
1977 if (REG_P (index))
e733134f 1978 {
36846b26
RH
1979 /* ??? Without AM33 generalized (Ri,Rn) addressing, reg+reg
1980 addressing is hard to satisfy. */
1981 if (!TARGET_AM33)
1982 return false;
e733134f 1983
36846b26
RH
1984 return (REGNO_GENERAL_P (REGNO (base), strict)
1985 && REGNO_GENERAL_P (REGNO (index), strict));
1986 }
e733134f 1987
36846b26
RH
1988 if (!REGNO_STRICT_OK_FOR_BASE_P (REGNO (base), strict))
1989 return false;
e733134f 1990
36846b26
RH
1991 if (CONST_INT_P (index))
1992 return IN_RANGE (INTVAL (index), -1 - 0x7fffffff, 0x7fffffff);
1993
1994 if (CONSTANT_ADDRESS_P (index))
1995 return !flag_pic || mn10300_legitimate_pic_operand_p (index);
1996
1997 return false;
1998}
1999
2000bool
2001mn10300_regno_in_class_p (unsigned regno, int rclass, bool strict)
2002{
2003 if (regno >= FIRST_PSEUDO_REGISTER)
2004 {
2005 if (!strict)
2006 return true;
2007 if (!reg_renumber)
2008 return false;
2009 regno = reg_renumber[regno];
ba4ec0e0
NC
2010 if (regno == INVALID_REGNUM)
2011 return false;
36846b26
RH
2012 }
2013 return TEST_HARD_REG_BIT (reg_class_contents[rclass], regno);
2014}
2015
2016rtx
2017mn10300_legitimize_reload_address (rtx x,
ef4bddc2 2018 machine_mode mode ATTRIBUTE_UNUSED,
36846b26
RH
2019 int opnum, int type,
2020 int ind_levels ATTRIBUTE_UNUSED)
2021{
2022 bool any_change = false;
2023
2024 /* See above re disabling reg+reg addressing for MN103. */
2025 if (!TARGET_AM33)
2026 return NULL_RTX;
2027
2028 if (GET_CODE (x) != PLUS)
2029 return NULL_RTX;
2030
2031 if (XEXP (x, 0) == stack_pointer_rtx)
2032 {
2033 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2034 GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2035 opnum, (enum reload_type) type);
2036 any_change = true;
2037 }
2038 if (XEXP (x, 1) == stack_pointer_rtx)
2039 {
2040 push_reload (XEXP (x, 1), NULL_RTX, &XEXP (x, 1), NULL,
2041 GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2042 opnum, (enum reload_type) type);
2043 any_change = true;
e733134f
AO
2044 }
2045
36846b26 2046 return any_change ? x : NULL_RTX;
e733134f
AO
2047}
2048
1a627b35 2049/* Implement TARGET_LEGITIMATE_CONSTANT_P. Returns TRUE if X is a valid
4af476d7
NC
2050 constant. Note that some "constants" aren't valid, such as TLS
2051 symbols and unconverted GOT-based references, so we eliminate
2052 those here. */
2053
1a627b35 2054static bool
ef4bddc2 2055mn10300_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
4af476d7
NC
2056{
2057 switch (GET_CODE (x))
2058 {
2059 case CONST:
2060 x = XEXP (x, 0);
2061
2062 if (GET_CODE (x) == PLUS)
2063 {
e7ab5593 2064 if (! CONST_INT_P (XEXP (x, 1)))
4af476d7
NC
2065 return false;
2066 x = XEXP (x, 0);
2067 }
2068
2069 /* Only some unspecs are valid as "constants". */
2070 if (GET_CODE (x) == UNSPEC)
2071 {
4af476d7
NC
2072 switch (XINT (x, 1))
2073 {
4af476d7
NC
2074 case UNSPEC_PIC:
2075 case UNSPEC_GOT:
2076 case UNSPEC_GOTOFF:
2077 case UNSPEC_PLT:
2078 return true;
2079 default:
2080 return false;
2081 }
2082 }
2083
2084 /* We must have drilled down to a symbol. */
e7ab5593 2085 if (! mn10300_symbolic_operand (x, Pmode))
4af476d7
NC
2086 return false;
2087 break;
2088
2089 default:
2090 break;
2091 }
2092
2093 return true;
2094}
2095
126b1483
RH
2096/* Undo pic address legitimization for the benefit of debug info. */
2097
2098static rtx
2099mn10300_delegitimize_address (rtx orig_x)
2100{
2101 rtx x = orig_x, ret, addend = NULL;
2102 bool need_mem;
2103
2104 if (MEM_P (x))
2105 x = XEXP (x, 0);
2106 if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
2107 return orig_x;
2108
2109 if (XEXP (x, 0) == pic_offset_table_rtx)
2110 ;
2111 /* With the REG+REG addressing of AM33, var-tracking can re-assemble
2112 some odd-looking "addresses" that were never valid in the first place.
2113 We need to look harder to avoid warnings being emitted. */
2114 else if (GET_CODE (XEXP (x, 0)) == PLUS)
2115 {
2116 rtx x0 = XEXP (x, 0);
2117 rtx x00 = XEXP (x0, 0);
2118 rtx x01 = XEXP (x0, 1);
2119
2120 if (x00 == pic_offset_table_rtx)
2121 addend = x01;
2122 else if (x01 == pic_offset_table_rtx)
2123 addend = x00;
2124 else
2125 return orig_x;
2126
2127 }
2128 else
2129 return orig_x;
2130 x = XEXP (x, 1);
2131
2132 if (GET_CODE (x) != CONST)
2133 return orig_x;
2134 x = XEXP (x, 0);
2135 if (GET_CODE (x) != UNSPEC)
2136 return orig_x;
2137
2138 ret = XVECEXP (x, 0, 0);
2139 if (XINT (x, 1) == UNSPEC_GOTOFF)
2140 need_mem = false;
2141 else if (XINT (x, 1) == UNSPEC_GOT)
2142 need_mem = true;
2143 else
2144 return orig_x;
2145
2146 gcc_assert (GET_CODE (ret) == SYMBOL_REF);
2147 if (need_mem != MEM_P (orig_x))
2148 return orig_x;
2149 if (need_mem && addend)
2150 return orig_x;
2151 if (addend)
2152 ret = gen_rtx_PLUS (Pmode, addend, ret);
2153 return ret;
2154}
2155
72d6e3c5
RH
2156/* For addresses, costs are relative to "MOV (Rm),Rn". For AM33 this is
2157 the 3-byte fully general instruction; for MN103 this is the 2-byte form
2158 with an address register. */
2159
dcefdf67 2160static int
ef4bddc2 2161mn10300_address_cost (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
b413068c 2162 addr_space_t as ATTRIBUTE_UNUSED, bool speed)
460ad325 2163{
72d6e3c5
RH
2164 HOST_WIDE_INT i;
2165 rtx base, index;
2166
460ad325
AO
2167 switch (GET_CODE (x))
2168 {
72d6e3c5
RH
2169 case CONST:
2170 case SYMBOL_REF:
2171 case LABEL_REF:
2172 /* We assume all of these require a 32-bit constant, even though
2173 some symbol and label references can be relaxed. */
2174 return speed ? 1 : 4;
2175
460ad325 2176 case REG:
72d6e3c5
RH
2177 case SUBREG:
2178 case POST_INC:
2179 return 0;
2180
2181 case POST_MODIFY:
2182 /* Assume any symbolic offset is a 32-bit constant. */
2183 i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2184 if (IN_RANGE (i, -128, 127))
2185 return speed ? 0 : 1;
2186 if (speed)
2187 return 1;
2188 if (IN_RANGE (i, -0x800000, 0x7fffff))
2189 return 3;
2190 return 4;
2191
2192 case PLUS:
2193 base = XEXP (x, 0);
2194 index = XEXP (x, 1);
2195 if (register_operand (index, SImode))
460ad325 2196 {
72d6e3c5
RH
2197 /* Attempt to minimize the number of registers in the address.
2198 This is similar to what other ports do. */
2199 if (register_operand (base, SImode))
2200 return 1;
460ad325 2201
72d6e3c5
RH
2202 base = XEXP (x, 1);
2203 index = XEXP (x, 0);
2204 }
460ad325 2205
72d6e3c5
RH
2206 /* Assume any symbolic offset is a 32-bit constant. */
2207 i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2208 if (IN_RANGE (i, -128, 127))
2209 return speed ? 0 : 1;
2210 if (IN_RANGE (i, -32768, 32767))
2211 return speed ? 0 : 2;
2212 return speed ? 2 : 6;
460ad325 2213
72d6e3c5 2214 default:
e548c9df 2215 return rtx_cost (x, Pmode, MEM, 0, speed);
72d6e3c5
RH
2216 }
2217}
460ad325 2218
72d6e3c5 2219/* Implement the TARGET_REGISTER_MOVE_COST hook.
460ad325 2220
72d6e3c5
RH
2221 Recall that the base value of 2 is required by assumptions elsewhere
2222 in the body of the compiler, and that cost 2 is special-cased as an
2223 early exit from reload meaning no work is required. */
460ad325 2224
72d6e3c5 2225static int
ef4bddc2 2226mn10300_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
72d6e3c5
RH
2227 reg_class_t ifrom, reg_class_t ito)
2228{
2229 enum reg_class from = (enum reg_class) ifrom;
2230 enum reg_class to = (enum reg_class) ito;
2231 enum reg_class scratch, test;
2232
2233 /* Simplify the following code by unifying the fp register classes. */
2234 if (to == FP_ACC_REGS)
2235 to = FP_REGS;
2236 if (from == FP_ACC_REGS)
2237 from = FP_REGS;
2238
2239 /* Diagnose invalid moves by costing them as two moves. */
2240
2241 scratch = NO_REGS;
2242 test = from;
2243 if (to == SP_REGS)
2244 scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
c25a21f5
RH
2245 else if (to == MDR_REGS)
2246 scratch = DATA_REGS;
72d6e3c5
RH
2247 else if (to == FP_REGS && to != from)
2248 scratch = GENERAL_REGS;
2249 else
2250 {
2251 test = to;
2252 if (from == SP_REGS)
2253 scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
c25a21f5
RH
2254 else if (from == MDR_REGS)
2255 scratch = DATA_REGS;
72d6e3c5
RH
2256 else if (from == FP_REGS && to != from)
2257 scratch = GENERAL_REGS;
2258 }
2259 if (scratch != NO_REGS && !reg_class_subset_p (test, scratch))
2260 return (mn10300_register_move_cost (VOIDmode, from, scratch)
2261 + mn10300_register_move_cost (VOIDmode, scratch, to));
460ad325 2262
72d6e3c5 2263 /* From here on, all we need consider are legal combinations. */
460ad325 2264
72d6e3c5
RH
2265 if (optimize_size)
2266 {
2267 /* The scale here is bytes * 2. */
460ad325 2268
72d6e3c5
RH
2269 if (from == to && (to == ADDRESS_REGS || to == DATA_REGS))
2270 return 2;
460ad325 2271
72d6e3c5
RH
2272 if (from == SP_REGS)
2273 return (to == ADDRESS_REGS ? 2 : 6);
2274
2275 /* For MN103, all remaining legal moves are two bytes. */
2276 if (TARGET_AM33)
2277 return 4;
2278
2279 if (to == SP_REGS)
2280 return (from == ADDRESS_REGS ? 4 : 6);
2281
2282 if ((from == ADDRESS_REGS || from == DATA_REGS)
2283 && (to == ADDRESS_REGS || to == DATA_REGS))
2284 return 4;
2285
2286 if (to == EXTENDED_REGS)
2287 return (to == from ? 6 : 4);
460ad325 2288
72d6e3c5
RH
2289 /* What's left are SP_REGS, FP_REGS, or combinations of the above. */
2290 return 6;
2291 }
2292 else
2293 {
2294 /* The scale here is cycles * 2. */
2295
2296 if (to == FP_REGS)
2297 return 8;
2298 if (from == FP_REGS)
2299 return 4;
2300
2301 /* All legal moves between integral registers are single cycle. */
2302 return 2;
460ad325
AO
2303 }
2304}
3c50106f 2305
72d6e3c5
RH
2306/* Implement the TARGET_MEMORY_MOVE_COST hook.
2307
2308 Given lack of the form of the address, this must be speed-relative,
2309 though we should never be less expensive than a size-relative register
2310 move cost above. This is not a problem. */
2311
dcefdf67 2312static int
ef4bddc2 2313mn10300_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
72d6e3c5 2314 reg_class_t iclass, bool in ATTRIBUTE_UNUSED)
dcefdf67 2315{
72d6e3c5
RH
2316 enum reg_class rclass = (enum reg_class) iclass;
2317
2318 if (rclass == FP_REGS)
2319 return 8;
2320 return 6;
dcefdf67
RH
2321}
2322
72d6e3c5
RH
2323/* Implement the TARGET_RTX_COSTS hook.
2324
2325 Speed-relative costs are relative to COSTS_N_INSNS, which is intended
2326 to represent cycles. Size-relative costs are in bytes. */
2327
3c50106f 2328static bool
e548c9df
AM
2329mn10300_rtx_costs (rtx x, machine_mode mode, int outer_code,
2330 int opno ATTRIBUTE_UNUSED, int *ptotal, bool speed)
3c50106f 2331{
72d6e3c5
RH
2332 /* This value is used for SYMBOL_REF etc where we want to pretend
2333 we have a full 32-bit constant. */
2334 HOST_WIDE_INT i = 0x12345678;
2335 int total;
e548c9df 2336 int code = GET_CODE (x);
72d6e3c5 2337
3c50106f
RH
2338 switch (code)
2339 {
2340 case CONST_INT:
72d6e3c5
RH
2341 i = INTVAL (x);
2342 do_int_costs:
2343 if (speed)
2344 {
2345 if (outer_code == SET)
2346 {
2347 /* 16-bit integer loads have latency 1, 32-bit loads 2. */
2348 if (IN_RANGE (i, -32768, 32767))
2349 total = COSTS_N_INSNS (1);
2350 else
2351 total = COSTS_N_INSNS (2);
2352 }
2353 else
2354 {
2355 /* 16-bit integer operands don't affect latency;
2356 24-bit and 32-bit operands add a cycle. */
2357 if (IN_RANGE (i, -32768, 32767))
2358 total = 0;
2359 else
2360 total = COSTS_N_INSNS (1);
2361 }
2362 }
3c50106f 2363 else
72d6e3c5
RH
2364 {
2365 if (outer_code == SET)
2366 {
2367 if (i == 0)
2368 total = 1;
2369 else if (IN_RANGE (i, -128, 127))
2370 total = 2;
2371 else if (IN_RANGE (i, -32768, 32767))
2372 total = 3;
2373 else
2374 total = 6;
2375 }
2376 else
2377 {
2378 /* Reference here is ADD An,Dn, vs ADD imm,Dn. */
2379 if (IN_RANGE (i, -128, 127))
2380 total = 0;
2381 else if (IN_RANGE (i, -32768, 32767))
2382 total = 2;
2383 else if (TARGET_AM33 && IN_RANGE (i, -0x01000000, 0x00ffffff))
2384 total = 3;
2385 else
2386 total = 4;
2387 }
2388 }
2389 goto alldone;
3c50106f
RH
2390
2391 case CONST:
2392 case LABEL_REF:
2393 case SYMBOL_REF:
3c50106f 2394 case CONST_DOUBLE:
72d6e3c5
RH
2395 /* We assume all of these require a 32-bit constant, even though
2396 some symbol and label references can be relaxed. */
2397 goto do_int_costs;
f90b7a5a 2398
72d6e3c5
RH
2399 case UNSPEC:
2400 switch (XINT (x, 1))
2401 {
2402 case UNSPEC_PIC:
2403 case UNSPEC_GOT:
2404 case UNSPEC_GOTOFF:
2405 case UNSPEC_PLT:
2406 case UNSPEC_GOTSYM_OFF:
2407 /* The PIC unspecs also resolve to a 32-bit constant. */
2408 goto do_int_costs;
3c50106f 2409
72d6e3c5
RH
2410 default:
2411 /* Assume any non-listed unspec is some sort of arithmetic. */
2412 goto do_arith_costs;
2413 }
fe7496dd 2414
72d6e3c5
RH
2415 case PLUS:
2416 /* Notice the size difference of INC and INC4. */
2417 if (!speed && outer_code == SET && CONST_INT_P (XEXP (x, 1)))
2418 {
2419 i = INTVAL (XEXP (x, 1));
2420 if (i == 1 || i == 4)
2421 {
e548c9df 2422 total = 1 + rtx_cost (XEXP (x, 0), mode, PLUS, 0, speed);
72d6e3c5
RH
2423 goto alldone;
2424 }
2425 }
2426 goto do_arith_costs;
2427
2428 case MINUS:
2429 case AND:
2430 case IOR:
2431 case XOR:
2432 case NOT:
2433 case NEG:
2434 case ZERO_EXTEND:
2435 case SIGN_EXTEND:
2436 case COMPARE:
2437 case BSWAP:
2438 case CLZ:
2439 do_arith_costs:
2440 total = (speed ? COSTS_N_INSNS (1) : 2);
2441 break;
fe7496dd 2442
72d6e3c5
RH
2443 case ASHIFT:
2444 /* Notice the size difference of ASL2 and variants. */
2445 if (!speed && CONST_INT_P (XEXP (x, 1)))
2446 switch (INTVAL (XEXP (x, 1)))
2447 {
2448 case 1:
2449 case 2:
2450 total = 1;
2451 goto alldone;
2452 case 3:
2453 case 4:
2454 total = 2;
2455 goto alldone;
2456 }
2457 /* FALLTHRU */
fe7496dd 2458
72d6e3c5
RH
2459 case ASHIFTRT:
2460 case LSHIFTRT:
2461 total = (speed ? COSTS_N_INSNS (1) : 3);
2462 goto alldone;
fe7496dd 2463
72d6e3c5
RH
2464 case MULT:
2465 total = (speed ? COSTS_N_INSNS (3) : 2);
fe7496dd 2466 break;
5abc5de9 2467
72d6e3c5
RH
2468 case DIV:
2469 case UDIV:
2470 case MOD:
2471 case UMOD:
2472 total = (speed ? COSTS_N_INSNS (39)
2473 /* Include space to load+retrieve MDR. */
2474 : code == MOD || code == UMOD ? 6 : 4);
fe7496dd 2475 break;
5abc5de9 2476
72d6e3c5 2477 case MEM:
e548c9df 2478 total = mn10300_address_cost (XEXP (x, 0), mode,
b413068c 2479 MEM_ADDR_SPACE (x), speed);
72d6e3c5
RH
2480 if (speed)
2481 total = COSTS_N_INSNS (2 + total);
2482 goto alldone;
2483
fe7496dd 2484 default:
72d6e3c5
RH
2485 /* Probably not implemented. Assume external call. */
2486 total = (speed ? COSTS_N_INSNS (10) : 7);
2487 break;
fe7496dd
AO
2488 }
2489
72d6e3c5
RH
2490 *ptotal = total;
2491 return false;
2492
2493 alldone:
2494 *ptotal = total;
2495 return true;
fe7496dd 2496}
72d6e3c5 2497
d1776069
AO
2498/* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2499 may access it using GOTOFF instead of GOT. */
2500
2501static void
2ba3d2a9 2502mn10300_encode_section_info (tree decl, rtx rtl, int first)
d1776069
AO
2503{
2504 rtx symbol;
2505
2ba3d2a9
NC
2506 default_encode_section_info (decl, rtl, first);
2507
e7ab5593 2508 if (! MEM_P (rtl))
d1776069 2509 return;
2ba3d2a9 2510
d1776069
AO
2511 symbol = XEXP (rtl, 0);
2512 if (GET_CODE (symbol) != SYMBOL_REF)
2513 return;
2514
2515 if (flag_pic)
2516 SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2517}
e6ff3083
AS
2518
2519/* Dispatch tables on the mn10300 are extremely expensive in terms of code
2520 and readonly data size. So we crank up the case threshold value to
2521 encourage a series of if/else comparisons to implement many small switch
2522 statements. In theory, this value could be increased much more if we
2523 were solely optimizing for space, but we keep it "reasonable" to avoid
2524 serious code efficiency lossage. */
2525
4af476d7
NC
2526static unsigned int
2527mn10300_case_values_threshold (void)
e6ff3083
AS
2528{
2529 return 6;
2530}
bdeb5f0c 2531
bdeb5f0c
RH
2532/* Worker function for TARGET_TRAMPOLINE_INIT. */
2533
2534static void
2535mn10300_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2536{
d6a3e264
RH
2537 rtx mem, disp, fnaddr = XEXP (DECL_RTL (fndecl), 0);
2538
2539 /* This is a strict alignment target, which means that we play
2540 some games to make sure that the locations at which we need
2541 to store <chain> and <disp> wind up at aligned addresses.
2542
2543 0x28 0x00 add 0,d0
2544 0xfc 0xdd mov chain,a1
2545 <chain>
2546 0xf8 0xed 0x00 btst 0,d1
2547 0xdc jmp fnaddr
2548 <disp>
2549
2550 Note that the two extra insns are effectively nops; they
2551 clobber the flags but do not affect the contents of D0 or D1. */
bdeb5f0c 2552
d6a3e264 2553 disp = expand_binop (SImode, sub_optab, fnaddr,
0a81f074 2554 plus_constant (Pmode, XEXP (m_tramp, 0), 11),
d6a3e264 2555 NULL_RTX, 1, OPTAB_DIRECT);
bdeb5f0c 2556
d6a3e264
RH
2557 mem = adjust_address (m_tramp, SImode, 0);
2558 emit_move_insn (mem, gen_int_mode (0xddfc0028, SImode));
2559 mem = adjust_address (m_tramp, SImode, 4);
bdeb5f0c 2560 emit_move_insn (mem, chain_value);
d6a3e264
RH
2561 mem = adjust_address (m_tramp, SImode, 8);
2562 emit_move_insn (mem, gen_int_mode (0xdc00edf8, SImode));
2563 mem = adjust_address (m_tramp, SImode, 12);
2564 emit_move_insn (mem, disp);
bdeb5f0c 2565}
990dc016
NC
2566
2567/* Output the assembler code for a C++ thunk function.
2568 THUNK_DECL is the declaration for the thunk function itself, FUNCTION
2569 is the decl for the target function. DELTA is an immediate constant
2570 offset to be added to the THIS parameter. If VCALL_OFFSET is nonzero
2571 the word at the adjusted address *(*THIS' + VCALL_OFFSET) should be
2572 additionally added to THIS. Finally jump to the entry point of
2573 FUNCTION. */
2574
2575static void
2576mn10300_asm_output_mi_thunk (FILE * file,
2577 tree thunk_fndecl ATTRIBUTE_UNUSED,
2578 HOST_WIDE_INT delta,
2579 HOST_WIDE_INT vcall_offset,
2580 tree function)
2581{
2582 const char * _this;
2583
2584 /* Get the register holding the THIS parameter. Handle the case
2585 where there is a hidden first argument for a returned structure. */
2586 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2587 _this = reg_names [FIRST_ARGUMENT_REGNUM + 1];
2588 else
2589 _this = reg_names [FIRST_ARGUMENT_REGNUM];
2590
2591 fprintf (file, "\t%s Thunk Entry Point:\n", ASM_COMMENT_START);
2592
2593 if (delta)
2594 fprintf (file, "\tadd %d, %s\n", (int) delta, _this);
2595
2596 if (vcall_offset)
2597 {
2598 const char * scratch = reg_names [FIRST_ADDRESS_REGNUM + 1];
2599
2600 fprintf (file, "\tmov %s, %s\n", _this, scratch);
2601 fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2602 fprintf (file, "\tadd %d, %s\n", (int) vcall_offset, scratch);
2603 fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2604 fprintf (file, "\tadd %s, %s\n", scratch, _this);
2605 }
2606
2607 fputs ("\tjmp ", file);
2608 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2609 putc ('\n', file);
2610}
2611
2612/* Return true if mn10300_output_mi_thunk would be able to output the
2613 assembler code for the thunk function specified by the arguments
2614 it is passed, and false otherwise. */
2615
2616static bool
2617mn10300_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
2618 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
2619 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2620 const_tree function ATTRIBUTE_UNUSED)
2621{
2622 return true;
2623}
4af476d7
NC
2624
2625bool
ef4bddc2 2626mn10300_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
4af476d7
NC
2627{
2628 if (REGNO_REG_CLASS (regno) == FP_REGS
2629 || REGNO_REG_CLASS (regno) == FP_ACC_REGS)
2630 /* Do not store integer values in FP registers. */
2631 return GET_MODE_CLASS (mode) == MODE_FLOAT && ((regno & 1) == 0);
c70da878
NC
2632
2633 if (! TARGET_AM33 && REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2634 return false;
2635
4af476d7
NC
2636 if (((regno) & 1) == 0 || GET_MODE_SIZE (mode) == 4)
2637 return true;
2638
2639 if (REGNO_REG_CLASS (regno) == DATA_REGS
2640 || (TARGET_AM33 && REGNO_REG_CLASS (regno) == ADDRESS_REGS)
2641 || REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2642 return GET_MODE_SIZE (mode) <= 4;
2643
2644 return false;
2645}
2646
2647bool
ef4bddc2 2648mn10300_modes_tieable (machine_mode mode1, machine_mode mode2)
4af476d7
NC
2649{
2650 if (GET_MODE_CLASS (mode1) == MODE_FLOAT
2651 && GET_MODE_CLASS (mode2) != MODE_FLOAT)
2652 return false;
2653
2654 if (GET_MODE_CLASS (mode2) == MODE_FLOAT
2655 && GET_MODE_CLASS (mode1) != MODE_FLOAT)
2656 return false;
2657
2658 if (TARGET_AM33
2659 || mode1 == mode2
2660 || (GET_MODE_SIZE (mode1) <= 4 && GET_MODE_SIZE (mode2) <= 4))
2661 return true;
2662
2663 return false;
2664}
2665
bad41521 2666static int
ef4bddc2 2667cc_flags_for_mode (machine_mode mode)
bad41521
RH
2668{
2669 switch (mode)
2670 {
2671 case CCmode:
2672 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C | CC_FLAG_V;
2673 case CCZNCmode:
2674 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C;
2675 case CCZNmode:
2676 return CC_FLAG_Z | CC_FLAG_N;
2677 case CC_FLOATmode:
2678 return -1;
2679 default:
2680 gcc_unreachable ();
2681 }
2682}
2683
2684static int
2685cc_flags_for_code (enum rtx_code code)
2686{
2687 switch (code)
2688 {
2689 case EQ: /* Z */
2690 case NE: /* ~Z */
2691 return CC_FLAG_Z;
2692
2693 case LT: /* N */
2694 case GE: /* ~N */
2695 return CC_FLAG_N;
2696 break;
2697
2698 case GT: /* ~(Z|(N^V)) */
2699 case LE: /* Z|(N^V) */
2700 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_V;
2701
2702 case GEU: /* ~C */
2703 case LTU: /* C */
2704 return CC_FLAG_C;
2705
2706 case GTU: /* ~(C | Z) */
2707 case LEU: /* C | Z */
2708 return CC_FLAG_Z | CC_FLAG_C;
2709
2710 case ORDERED:
2711 case UNORDERED:
2712 case LTGT:
2713 case UNEQ:
2714 case UNGE:
2715 case UNGT:
2716 case UNLE:
2717 case UNLT:
2718 return -1;
2719
2720 default:
2721 gcc_unreachable ();
2722 }
2723}
2724
ef4bddc2 2725machine_mode
bad41521 2726mn10300_select_cc_mode (enum rtx_code code, rtx x, rtx y ATTRIBUTE_UNUSED)
4af476d7 2727{
bad41521
RH
2728 int req;
2729
2730 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2731 return CC_FLOATmode;
2732
2733 req = cc_flags_for_code (code);
2734
2735 if (req & CC_FLAG_V)
2736 return CCmode;
2737 if (req & CC_FLAG_C)
2738 return CCZNCmode;
2739 return CCZNmode;
4af476d7 2740}
f3f63737
NC
2741
2742static inline bool
af480750 2743set_is_load_p (rtx set)
f3f63737 2744{
af480750 2745 return MEM_P (SET_SRC (set));
f3f63737
NC
2746}
2747
2748static inline bool
af480750 2749set_is_store_p (rtx set)
f3f63737 2750{
af480750 2751 return MEM_P (SET_DEST (set));
f3f63737
NC
2752}
2753
2754/* Update scheduling costs for situations that cannot be
2755 described using the attributes and DFA machinery.
2756 DEP is the insn being scheduled.
2757 INSN is the previous insn.
2758 COST is the current cycle cost for DEP. */
2759
2760static int
b505225b
TS
2761mn10300_adjust_sched_cost (rtx_insn *insn, int dep_type, rtx_insn *dep,
2762 int cost, unsigned int)
f3f63737 2763{
af480750
DM
2764 rtx insn_set;
2765 rtx dep_set;
2766 int timings;
f3f63737
NC
2767
2768 if (!TARGET_AM33)
2769 return 1;
2770
af480750
DM
2771 /* We are only interested in pairs of SET. */
2772 insn_set = single_set (insn);
2773 if (!insn_set)
2774 return cost;
f3f63737 2775
af480750
DM
2776 dep_set = single_set (dep);
2777 if (!dep_set)
2778 return cost;
f3f63737
NC
2779
2780 /* For the AM34 a load instruction that follows a
2781 store instruction incurs an extra cycle of delay. */
2782 if (mn10300_tune_cpu == PROCESSOR_AM34
af480750
DM
2783 && set_is_load_p (dep_set)
2784 && set_is_store_p (insn_set))
f3f63737
NC
2785 cost += 1;
2786
2787 /* For the AM34 a non-store, non-branch FPU insn that follows
2788 another FPU insn incurs a one cycle throughput increase. */
2789 else if (mn10300_tune_cpu == PROCESSOR_AM34
af480750 2790 && ! set_is_store_p (insn_set)
f3f63737 2791 && ! JUMP_P (insn)
af480750
DM
2792 && GET_MODE_CLASS (GET_MODE (SET_SRC (dep_set))) == MODE_FLOAT
2793 && GET_MODE_CLASS (GET_MODE (SET_SRC (insn_set))) == MODE_FLOAT)
f3f63737
NC
2794 cost += 1;
2795
2796 /* Resolve the conflict described in section 1-7-4 of
2797 Chapter 3 of the MN103E Series Instruction Manual
2798 where it says:
2799
073a8998 2800 "When the preceding instruction is a CPU load or
f3f63737
NC
2801 store instruction, a following FPU instruction
2802 cannot be executed until the CPU completes the
2803 latency period even though there are no register
2804 or flag dependencies between them." */
2805
2806 /* Only the AM33-2 (and later) CPUs have FPU instructions. */
2807 if (! TARGET_AM33_2)
2808 return cost;
2809
2810 /* If a data dependence already exists then the cost is correct. */
b505225b 2811 if (dep_type == 0)
f3f63737
NC
2812 return cost;
2813
2814 /* Check that the instruction about to scheduled is an FPU instruction. */
af480750 2815 if (GET_MODE_CLASS (GET_MODE (SET_SRC (dep_set))) != MODE_FLOAT)
f3f63737
NC
2816 return cost;
2817
2818 /* Now check to see if the previous instruction is a load or store. */
af480750 2819 if (! set_is_load_p (insn_set) && ! set_is_store_p (insn_set))
f3f63737
NC
2820 return cost;
2821
2822 /* XXX: Verify: The text of 1-7-4 implies that the restriction
073a8998 2823 only applies when an INTEGER load/store precedes an FPU
f3f63737 2824 instruction, but is this true ? For now we assume that it is. */
af480750 2825 if (GET_MODE_CLASS (GET_MODE (SET_SRC (insn_set))) != MODE_INT)
f3f63737
NC
2826 return cost;
2827
2828 /* Extract the latency value from the timings attribute. */
af480750 2829 timings = get_attr_timings (insn);
f3f63737
NC
2830 return timings < 100 ? (timings % 10) : (timings % 100);
2831}
5efd84c5
NF
2832
2833static void
2834mn10300_conditional_register_usage (void)
2835{
2836 unsigned int i;
2837
2838 if (!TARGET_AM33)
2839 {
2840 for (i = FIRST_EXTENDED_REGNUM;
2841 i <= LAST_EXTENDED_REGNUM; i++)
2842 fixed_regs[i] = call_used_regs[i] = 1;
2843 }
2844 if (!TARGET_AM33_2)
2845 {
2846 for (i = FIRST_FP_REGNUM;
2847 i <= LAST_FP_REGNUM; i++)
2848 fixed_regs[i] = call_used_regs[i] = 1;
2849 }
2850 if (flag_pic)
2851 fixed_regs[PIC_OFFSET_TABLE_REGNUM] =
2852 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2853}
a49b692a 2854
7ca35180 2855/* Worker function for TARGET_MD_ASM_ADJUST.
a49b692a
RH
2856 We do this in the mn10300 backend to maintain source compatibility
2857 with the old cc0-based compiler. */
2858
7ca35180
RH
2859static rtx_insn *
2860mn10300_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
2861 vec<const char *> &/*constraints*/,
2862 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
a49b692a 2863{
7ca35180
RH
2864 clobbers.safe_push (gen_rtx_REG (CCmode, CC_REG));
2865 SET_HARD_REG_BIT (clobbered_regs, CC_REG);
2866 return NULL;
a49b692a 2867}
4af476d7 2868\f
bad41521
RH
2869/* A helper function for splitting cbranch patterns after reload. */
2870
2871void
ef4bddc2 2872mn10300_split_cbranch (machine_mode cmp_mode, rtx cmp_op, rtx label_ref)
bad41521
RH
2873{
2874 rtx flags, x;
2875
2876 flags = gen_rtx_REG (cmp_mode, CC_REG);
2877 x = gen_rtx_COMPARE (cmp_mode, XEXP (cmp_op, 0), XEXP (cmp_op, 1));
f7df4a84 2878 x = gen_rtx_SET (flags, x);
bad41521
RH
2879 emit_insn (x);
2880
2881 x = gen_rtx_fmt_ee (GET_CODE (cmp_op), VOIDmode, flags, const0_rtx);
2882 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x, label_ref, pc_rtx);
f7df4a84 2883 x = gen_rtx_SET (pc_rtx, x);
bad41521
RH
2884 emit_jump_insn (x);
2885}
2886
2887/* A helper function for matching parallels that set the flags. */
2888
2889bool
ef4bddc2 2890mn10300_match_ccmode (rtx insn, machine_mode cc_mode)
bad41521
RH
2891{
2892 rtx op1, flags;
ef4bddc2 2893 machine_mode flags_mode;
bad41521
RH
2894
2895 gcc_checking_assert (XVECLEN (PATTERN (insn), 0) == 2);
2896
2897 op1 = XVECEXP (PATTERN (insn), 0, 1);
2898 gcc_checking_assert (GET_CODE (SET_SRC (op1)) == COMPARE);
2899
2900 flags = SET_DEST (op1);
2901 flags_mode = GET_MODE (flags);
2902
2903 if (GET_MODE (SET_SRC (op1)) != flags_mode)
2904 return false;
2905 if (GET_MODE_CLASS (flags_mode) != MODE_CC)
2906 return false;
2907
2908 /* Ensure that the mode of FLAGS is compatible with CC_MODE. */
2909 if (cc_flags_for_mode (flags_mode) & ~cc_flags_for_mode (cc_mode))
2910 return false;
2911
2912 return true;
2913}
2914
cf13d9cf
NC
2915/* This function is used to help split:
2916
2917 (set (reg) (and (reg) (int)))
2918
2919 into:
2920
2921 (set (reg) (shift (reg) (int))
2922 (set (reg) (shift (reg) (int))
2923
2924 where the shitfs will be shorter than the "and" insn.
2925
2926 It returns the number of bits that should be shifted. A positive
2927 values means that the low bits are to be cleared (and hence the
2928 shifts should be right followed by left) whereas a negative value
2929 means that the high bits are to be cleared (left followed by right).
2930 Zero is returned when it would not be economical to split the AND. */
2931
bad41521
RH
2932int
2933mn10300_split_and_operand_count (rtx op)
2934{
2935 HOST_WIDE_INT val = INTVAL (op);
2936 int count;
2937
2938 if (val < 0)
2939 {
2940 /* High bit is set, look for bits clear at the bottom. */
2941 count = exact_log2 (-val);
2942 if (count < 0)
2943 return 0;
2944 /* This is only size win if we can use the asl2 insn. Otherwise we
2945 would be replacing 1 6-byte insn with 2 3-byte insns. */
2946 if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2947 return 0;
cf13d9cf 2948 return count;
bad41521
RH
2949 }
2950 else
2951 {
2952 /* High bit is clear, look for bits set at the bottom. */
2953 count = exact_log2 (val + 1);
2954 count = 32 - count;
2955 /* Again, this is only a size win with asl2. */
2956 if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2957 return 0;
2958 return -count;
2959 }
2960}
2961\f
a45d420a
NC
2962struct liw_data
2963{
2964 enum attr_liw slot;
2965 enum attr_liw_op op;
2966 rtx dest;
2967 rtx src;
2968};
2969
2970/* Decide if the given insn is a candidate for LIW bundling. If it is then
2971 extract the operands and LIW attributes from the insn and use them to fill
2972 in the liw_data structure. Return true upon success or false if the insn
2973 cannot be bundled. */
298362c8
NC
2974
2975static bool
e8a54173 2976extract_bundle (rtx_insn *insn, struct liw_data * pdata)
298362c8 2977{
a45d420a 2978 bool allow_consts = true;
2cf320a8 2979 rtx p;
298362c8 2980
a45d420a
NC
2981 gcc_assert (pdata != NULL);
2982
e8a54173 2983 if (insn == NULL)
a45d420a
NC
2984 return false;
2985 /* Make sure that we are dealing with a simple SET insn. */
298362c8 2986 p = single_set (insn);
a45d420a
NC
2987 if (p == NULL_RTX)
2988 return false;
2989
2990 /* Make sure that it could go into one of the LIW pipelines. */
2991 pdata->slot = get_attr_liw (insn);
2992 if (pdata->slot == LIW_BOTH)
2993 return false;
2994
2995 pdata->op = get_attr_liw_op (insn);
2996
a45d420a 2997 switch (pdata->op)
298362c8
NC
2998 {
2999 case LIW_OP_MOV:
a45d420a
NC
3000 pdata->dest = SET_DEST (p);
3001 pdata->src = SET_SRC (p);
298362c8
NC
3002 break;
3003 case LIW_OP_CMP:
a45d420a
NC
3004 pdata->dest = XEXP (SET_SRC (p), 0);
3005 pdata->src = XEXP (SET_SRC (p), 1);
298362c8
NC
3006 break;
3007 case LIW_OP_NONE:
3008 return false;
a45d420a
NC
3009 case LIW_OP_AND:
3010 case LIW_OP_OR:
3011 case LIW_OP_XOR:
3012 /* The AND, OR and XOR long instruction words only accept register arguments. */
3013 allow_consts = false;
3014 /* Fall through. */
298362c8 3015 default:
a45d420a
NC
3016 pdata->dest = SET_DEST (p);
3017 pdata->src = XEXP (SET_SRC (p), 1);
298362c8
NC
3018 break;
3019 }
3020
a45d420a
NC
3021 if (! REG_P (pdata->dest))
3022 return false;
3023
3024 if (REG_P (pdata->src))
3025 return true;
3026
3027 return allow_consts && satisfies_constraint_O (pdata->src);
298362c8
NC
3028}
3029
a45d420a
NC
3030/* Make sure that it is OK to execute LIW1 and LIW2 in parallel. GCC generated
3031 the instructions with the assumption that LIW1 would be executed before LIW2
3032 so we must check for overlaps between their sources and destinations. */
298362c8
NC
3033
3034static bool
a45d420a
NC
3035check_liw_constraints (struct liw_data * pliw1, struct liw_data * pliw2)
3036{
3037 /* Check for slot conflicts. */
3038 if (pliw2->slot == pliw1->slot && pliw1->slot != LIW_EITHER)
298362c8
NC
3039 return false;
3040
a45d420a
NC
3041 /* If either operation is a compare, then "dest" is really an input; the real
3042 destination is CC_REG. So these instructions need different checks. */
3043
3044 /* Changing "CMP ; OP" into "CMP | OP" is OK because the comparison will
3045 check its values prior to any changes made by OP. */
3046 if (pliw1->op == LIW_OP_CMP)
3047 {
3048 /* Two sequential comparisons means dead code, which ought to
3049 have been eliminated given that bundling only happens with
3050 optimization. We cannot bundle them in any case. */
3051 gcc_assert (pliw1->op != pliw2->op);
3052 return true;
3053 }
298362c8 3054
a45d420a
NC
3055 /* Changing "OP ; CMP" into "OP | CMP" does not work if the value being compared
3056 is the destination of OP, as the CMP will look at the old value, not the new
3057 one. */
3058 if (pliw2->op == LIW_OP_CMP)
298362c8 3059 {
a45d420a
NC
3060 if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3061 return false;
3062
3063 if (REG_P (pliw2->src))
3064 return REGNO (pliw2->src) != REGNO (pliw1->dest);
3065
3066 return true;
3067 }
3068
3069 /* Changing "OP1 ; OP2" into "OP1 | OP2" does not work if they both write to the
3070 same destination register. */
3071 if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3072 return false;
3073
3074 /* Changing "OP1 ; OP2" into "OP1 | OP2" generally does not work if the destination
3075 of OP1 is the source of OP2. The exception is when OP1 is a MOVE instruction when
3076 we can replace the source in OP2 with the source of OP1. */
3077 if (REG_P (pliw2->src) && REGNO (pliw2->src) == REGNO (pliw1->dest))
3078 {
3079 if (pliw1->op == LIW_OP_MOV && REG_P (pliw1->src))
298362c8 3080 {
a45d420a
NC
3081 if (! REG_P (pliw1->src)
3082 && (pliw2->op == LIW_OP_AND
3083 || pliw2->op == LIW_OP_OR
3084 || pliw2->op == LIW_OP_XOR))
3085 return false;
3086
3087 pliw2->src = pliw1->src;
298362c8
NC
3088 return true;
3089 }
3090 return false;
3091 }
3092
a45d420a 3093 /* Everything else is OK. */
298362c8
NC
3094 return true;
3095}
3096
298362c8
NC
3097/* Combine pairs of insns into LIW bundles. */
3098
3099static void
3100mn10300_bundle_liw (void)
3101{
e8a54173 3102 rtx_insn *r;
298362c8 3103
e8a54173 3104 for (r = get_insns (); r != NULL; r = next_nonnote_nondebug_insn (r))
298362c8 3105 {
e8a54173 3106 rtx_insn *insn1, *insn2;
a45d420a 3107 struct liw_data liw1, liw2;
298362c8
NC
3108
3109 insn1 = r;
a45d420a 3110 if (! extract_bundle (insn1, & liw1))
298362c8
NC
3111 continue;
3112
3113 insn2 = next_nonnote_nondebug_insn (insn1);
a45d420a 3114 if (! extract_bundle (insn2, & liw2))
298362c8
NC
3115 continue;
3116
a45d420a
NC
3117 /* Check for source/destination overlap. */
3118 if (! check_liw_constraints (& liw1, & liw2))
298362c8
NC
3119 continue;
3120
a45d420a 3121 if (liw1.slot == LIW_OP2 || liw2.slot == LIW_OP1)
298362c8 3122 {
a45d420a
NC
3123 struct liw_data temp;
3124
3125 temp = liw1;
298362c8 3126 liw1 = liw2;
a45d420a 3127 liw2 = temp;
298362c8
NC
3128 }
3129
298362c8
NC
3130 delete_insn (insn2);
3131
e8a54173 3132 rtx insn2_pat;
a45d420a 3133 if (liw1.op == LIW_OP_CMP)
e8a54173
DM
3134 insn2_pat = gen_cmp_liw (liw2.dest, liw2.src, liw1.dest, liw1.src,
3135 GEN_INT (liw2.op));
a45d420a 3136 else if (liw2.op == LIW_OP_CMP)
e8a54173
DM
3137 insn2_pat = gen_liw_cmp (liw1.dest, liw1.src, liw2.dest, liw2.src,
3138 GEN_INT (liw1.op));
298362c8 3139 else
e8a54173
DM
3140 insn2_pat = gen_liw (liw1.dest, liw2.dest, liw1.src, liw2.src,
3141 GEN_INT (liw1.op), GEN_INT (liw2.op));
298362c8 3142
e8a54173 3143 insn2 = emit_insn_after (insn2_pat, insn1);
298362c8
NC
3144 delete_insn (insn1);
3145 r = insn2;
3146 }
3147}
3148
662c03f4
NC
3149#define DUMP(reason, insn) \
3150 do \
3151 { \
3152 if (dump_file) \
3153 { \
3154 fprintf (dump_file, reason "\n"); \
3155 if (insn != NULL_RTX) \
3156 print_rtl_single (dump_file, insn); \
3157 fprintf(dump_file, "\n"); \
3158 } \
3159 } \
3160 while (0)
3161
3162/* Replace the BRANCH insn with a Lcc insn that goes to LABEL.
3163 Insert a SETLB insn just before LABEL. */
3164
3165static void
3166mn10300_insert_setlb_lcc (rtx label, rtx branch)
3167{
3168 rtx lcc, comparison, cmp_reg;
3169
3170 if (LABEL_NUSES (label) > 1)
3171 {
e60365d3 3172 rtx_insn *insn;
662c03f4
NC
3173
3174 /* This label is used both as an entry point to the loop
3175 and as a loop-back point for the loop. We need to separate
3176 these two functions so that the SETLB happens upon entry,
3177 but the loop-back does not go to the SETLB instruction. */
3178 DUMP ("Inserting SETLB insn after:", label);
3179 insn = emit_insn_after (gen_setlb (), label);
3180 label = gen_label_rtx ();
3181 emit_label_after (label, insn);
3182 DUMP ("Created new loop-back label:", label);
3183 }
3184 else
3185 {
3186 DUMP ("Inserting SETLB insn before:", label);
3187 emit_insn_before (gen_setlb (), label);
3188 }
3189
3190 comparison = XEXP (SET_SRC (PATTERN (branch)), 0);
3191 cmp_reg = XEXP (comparison, 0);
3192 gcc_assert (REG_P (cmp_reg));
3193
3194 /* If the comparison has not already been split out of the branch
3195 then do so now. */
3196 gcc_assert (REGNO (cmp_reg) == CC_REG);
3197
3198 if (GET_MODE (cmp_reg) == CC_FLOATmode)
3199 lcc = gen_FLcc (comparison, label);
3200 else
3201 lcc = gen_Lcc (comparison, label);
3202
e73de8f3 3203 rtx_insn *jump = emit_jump_insn_before (lcc, branch);
543e1b5f 3204 mark_jump_label (XVECEXP (lcc, 0, 0), jump, 0);
e73de8f3 3205 JUMP_LABEL (jump) = label;
662c03f4 3206 DUMP ("Replacing branch insn...", branch);
e73de8f3 3207 DUMP ("... with Lcc insn:", jump);
662c03f4
NC
3208 delete_insn (branch);
3209}
3210
3211static bool
b8244d74 3212mn10300_block_contains_call (basic_block block)
662c03f4 3213{
b32d5189 3214 rtx_insn *insn;
662c03f4
NC
3215
3216 FOR_BB_INSNS (block, insn)
3217 if (CALL_P (insn))
3218 return true;
3219
3220 return false;
3221}
3222
3223static bool
3224mn10300_loop_contains_call_insn (loop_p loop)
3225{
3226 basic_block * bbs;
3227 bool result = false;
3228 unsigned int i;
3229
3230 bbs = get_loop_body (loop);
3231
3232 for (i = 0; i < loop->num_nodes; i++)
3233 if (mn10300_block_contains_call (bbs[i]))
3234 {
3235 result = true;
3236 break;
3237 }
3238
3239 free (bbs);
3240 return result;
3241}
3242
3243static void
3244mn10300_scan_for_setlb_lcc (void)
3245{
662c03f4
NC
3246 loop_p loop;
3247
3248 DUMP ("Looking for loops that can use the SETLB insn", NULL_RTX);
3249
3250 df_analyze ();
3251 compute_bb_for_insn ();
3252
3253 /* Find the loops. */
4861a1f7 3254 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
662c03f4
NC
3255
3256 /* FIXME: For now we only investigate innermost loops. In practice however
3257 if an inner loop is not suitable for use with the SETLB/Lcc insns, it may
3258 be the case that its parent loop is suitable. Thus we should check all
3259 loops, but work from the innermost outwards. */
f0bd40b1 3260 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
662c03f4
NC
3261 {
3262 const char * reason = NULL;
3263
3264 /* Check to see if we can modify this loop. If we cannot
3265 then set 'reason' to describe why it could not be done. */
3266 if (loop->latch == NULL)
3267 reason = "it contains multiple latches";
3268 else if (loop->header != loop->latch)
3269 /* FIXME: We could handle loops that span multiple blocks,
3270 but this requires a lot more work tracking down the branches
3271 that need altering, so for now keep things simple. */
3272 reason = "the loop spans multiple blocks";
3273 else if (mn10300_loop_contains_call_insn (loop))
3274 reason = "it contains CALL insns";
3275 else
3276 {
68a1a6c0 3277 rtx_insn *branch = BB_END (loop->latch);
662c03f4
NC
3278
3279 gcc_assert (JUMP_P (branch));
3280 if (single_set (branch) == NULL_RTX || ! any_condjump_p (branch))
3281 /* We cannot optimize tablejumps and the like. */
3282 /* FIXME: We could handle unconditional jumps. */
3283 reason = "it is not a simple loop";
3284 else
3285 {
68a1a6c0 3286 rtx_insn *label;
662c03f4
NC
3287
3288 if (dump_file)
3289 flow_loop_dump (loop, dump_file, NULL, 0);
3290
3291 label = BB_HEAD (loop->header);
3292 gcc_assert (LABEL_P (label));
3293
3294 mn10300_insert_setlb_lcc (label, branch);
3295 }
3296 }
3297
3298 if (dump_file && reason != NULL)
3299 fprintf (dump_file, "Loop starting with insn %d is not suitable because %s\n",
3300 INSN_UID (BB_HEAD (loop->header)),
3301 reason);
3302 }
3303
4861a1f7 3304 loop_optimizer_finalize ();
662c03f4
NC
3305
3306 df_finish_pass (false);
3307
3308 DUMP ("SETLB scan complete", NULL_RTX);
3309}
3310
298362c8
NC
3311static void
3312mn10300_reorg (void)
3313{
662c03f4
NC
3314 /* These are optimizations, so only run them if optimizing. */
3315 if (TARGET_AM33 && (optimize > 0 || optimize_size))
298362c8 3316 {
662c03f4
NC
3317 if (TARGET_ALLOW_SETLB)
3318 mn10300_scan_for_setlb_lcc ();
3319
298362c8
NC
3320 if (TARGET_ALLOW_LIW)
3321 mn10300_bundle_liw ();
3322 }
3323}
3324\f
e7ab5593
NC
3325/* Initialize the GCC target structure. */
3326
298362c8
NC
3327#undef TARGET_MACHINE_DEPENDENT_REORG
3328#define TARGET_MACHINE_DEPENDENT_REORG mn10300_reorg
3329
e7ab5593
NC
3330#undef TARGET_ASM_ALIGNED_HI_OP
3331#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
3332
3333#undef TARGET_LEGITIMIZE_ADDRESS
3334#define TARGET_LEGITIMIZE_ADDRESS mn10300_legitimize_address
3335
72d6e3c5
RH
3336#undef TARGET_ADDRESS_COST
3337#define TARGET_ADDRESS_COST mn10300_address_cost
3338#undef TARGET_REGISTER_MOVE_COST
3339#define TARGET_REGISTER_MOVE_COST mn10300_register_move_cost
3340#undef TARGET_MEMORY_MOVE_COST
3341#define TARGET_MEMORY_MOVE_COST mn10300_memory_move_cost
e7ab5593
NC
3342#undef TARGET_RTX_COSTS
3343#define TARGET_RTX_COSTS mn10300_rtx_costs
e7ab5593
NC
3344
3345#undef TARGET_ASM_FILE_START
3346#define TARGET_ASM_FILE_START mn10300_file_start
3347#undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
3348#define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
3349
535bd17c
AS
3350#undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
3351#define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA mn10300_asm_output_addr_const_extra
3352
e7ab5593
NC
3353#undef TARGET_OPTION_OVERRIDE
3354#define TARGET_OPTION_OVERRIDE mn10300_option_override
3355
3356#undef TARGET_ENCODE_SECTION_INFO
3357#define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
3358
3359#undef TARGET_PROMOTE_PROTOTYPES
3360#define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
3361#undef TARGET_RETURN_IN_MEMORY
3362#define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
3363#undef TARGET_PASS_BY_REFERENCE
3364#define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
3365#undef TARGET_CALLEE_COPIES
3366#define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
3367#undef TARGET_ARG_PARTIAL_BYTES
3368#define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
ce236858
NF
3369#undef TARGET_FUNCTION_ARG
3370#define TARGET_FUNCTION_ARG mn10300_function_arg
3371#undef TARGET_FUNCTION_ARG_ADVANCE
3372#define TARGET_FUNCTION_ARG_ADVANCE mn10300_function_arg_advance
e7ab5593
NC
3373
3374#undef TARGET_EXPAND_BUILTIN_SAVEREGS
3375#define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
3376#undef TARGET_EXPAND_BUILTIN_VA_START
3377#define TARGET_EXPAND_BUILTIN_VA_START mn10300_va_start
3378
3379#undef TARGET_CASE_VALUES_THRESHOLD
3380#define TARGET_CASE_VALUES_THRESHOLD mn10300_case_values_threshold
3381
3382#undef TARGET_LEGITIMATE_ADDRESS_P
3383#define TARGET_LEGITIMATE_ADDRESS_P mn10300_legitimate_address_p
126b1483
RH
3384#undef TARGET_DELEGITIMIZE_ADDRESS
3385#define TARGET_DELEGITIMIZE_ADDRESS mn10300_delegitimize_address
1a627b35
RS
3386#undef TARGET_LEGITIMATE_CONSTANT_P
3387#define TARGET_LEGITIMATE_CONSTANT_P mn10300_legitimate_constant_p
e7ab5593 3388
f2831cc9
AS
3389#undef TARGET_PREFERRED_RELOAD_CLASS
3390#define TARGET_PREFERRED_RELOAD_CLASS mn10300_preferred_reload_class
3391#undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
8b119bb6
RH
3392#define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS \
3393 mn10300_preferred_output_reload_class
3394#undef TARGET_SECONDARY_RELOAD
3395#define TARGET_SECONDARY_RELOAD mn10300_secondary_reload
f2831cc9 3396
e7ab5593
NC
3397#undef TARGET_TRAMPOLINE_INIT
3398#define TARGET_TRAMPOLINE_INIT mn10300_trampoline_init
3399
3400#undef TARGET_FUNCTION_VALUE
3401#define TARGET_FUNCTION_VALUE mn10300_function_value
3402#undef TARGET_LIBCALL_VALUE
3403#define TARGET_LIBCALL_VALUE mn10300_libcall_value
3404
3405#undef TARGET_ASM_OUTPUT_MI_THUNK
3406#define TARGET_ASM_OUTPUT_MI_THUNK mn10300_asm_output_mi_thunk
3407#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3408#define TARGET_ASM_CAN_OUTPUT_MI_THUNK mn10300_can_output_mi_thunk
3409
f3f63737
NC
3410#undef TARGET_SCHED_ADJUST_COST
3411#define TARGET_SCHED_ADJUST_COST mn10300_adjust_sched_cost
3412
5efd84c5
NF
3413#undef TARGET_CONDITIONAL_REGISTER_USAGE
3414#define TARGET_CONDITIONAL_REGISTER_USAGE mn10300_conditional_register_usage
3415
7ca35180
RH
3416#undef TARGET_MD_ASM_ADJUST
3417#define TARGET_MD_ASM_ADJUST mn10300_md_asm_adjust
a49b692a 3418
3843787f
RH
3419#undef TARGET_FLAGS_REGNUM
3420#define TARGET_FLAGS_REGNUM CC_REG
3421
e7ab5593 3422struct gcc_target targetm = TARGET_INITIALIZER;