]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/mcore/mcore.c
* gcc.c-torture/compile/20000609-1.c: New test.
[thirdparty/gcc.git] / gcc / config / mcore / mcore.c
CommitLineData
8f90be4c
NC
1/* Output routines for Motorola MCore processor
2 Copyright (C) 1993, 1999, 2000 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
4bd048ef 20#include "system.h"
8f90be4c
NC
21#include "assert.h"
22#include "gansidecl.h"
23
24#include "config.h"
25#include "rtl.h"
26#include "mcore.h"
27
28#include "regs.h"
29#include "hard-reg-set.h"
30#include "real.h"
31#include "insn-config.h"
32#include "conditions.h"
33#include "insn-flags.h"
34#include "tree.h"
35#include "output.h"
36#include "insn-attr.h"
37#include "flags.h"
38#include "obstack.h"
39#include "expr.h"
40#include "reload.h"
41#include "recog.h"
42#include "function.h"
43#include "ggc.h"
44#include "toplev.h"
45#include "mcore-protos.h"
46
47static int const_ok_for_mcore PARAMS ((int));
48static int try_constant_tricks PARAMS ((long, int *, int *));
49
50/* Maximum size we are allowed to grow the stack in a single operation.
51 If we want more, we must do it in increments of at most this size.
52 If this value is 0, we don't check at all. */
53const char * mcore_stack_increment_string = 0;
54int mcore_stack_increment = STACK_UNITS_MAXSTEP;
55
56/* For dumping information about frame sizes. */
57char * mcore_current_function_name = 0;
58long mcore_current_compilation_timestamp = 0;
59
60/* Global variables for machine-dependent things. */
61
62/* Saved operands from the last compare to use when we generate an scc
63 or bcc insn. */
64rtx arch_compare_op0;
65rtx arch_compare_op1;
66
67/* Provides the class number of the smallest class containing
68 reg number. */
69int regno_reg_class[FIRST_PSEUDO_REGISTER] =
70{
71 GENERAL_REGS, ONLYR1_REGS, LRW_REGS, LRW_REGS,
72 LRW_REGS, LRW_REGS, LRW_REGS, LRW_REGS,
73 LRW_REGS, LRW_REGS, LRW_REGS, LRW_REGS,
74 LRW_REGS, LRW_REGS, LRW_REGS, GENERAL_REGS,
75 GENERAL_REGS, C_REGS, NO_REGS, NO_REGS,
76};
77
78/* Provide reg_class from a letter such as appears in the machine
79 description. */
80enum reg_class reg_class_from_letter[] =
81{
82 /* a */ LRW_REGS, /* b */ ONLYR1_REGS, /* c */ C_REGS, /* d */ NO_REGS,
83 /* e */ NO_REGS, /* f */ NO_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
84 /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ NO_REGS,
85 /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
86 /* q */ NO_REGS, /* r */ GENERAL_REGS, /* s */ NO_REGS, /* t */ NO_REGS,
87 /* u */ NO_REGS, /* v */ NO_REGS, /* w */ NO_REGS, /* x */ ALL_REGS,
88 /* y */ NO_REGS, /* z */ NO_REGS
89};
90
91/* Adjust the stack and return the number of bytes taken to do it. */
92static void
93output_stack_adjust (direction, size)
94 int direction;
95 int size;
96{
97 /* If extending stack a lot, we do it incrementally. */
98 if (direction < 0 && size > mcore_stack_increment && mcore_stack_increment > 0)
99 {
100 rtx tmp = gen_rtx (REG, SImode, 1);
101 rtx memref;
102 emit_insn (gen_movsi (tmp, GEN_INT (mcore_stack_increment)));
103 do
104 {
105 emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
106 memref = gen_rtx (MEM, SImode, stack_pointer_rtx);
107 MEM_VOLATILE_P (memref) = 1;
108 emit_insn (gen_movsi (memref, stack_pointer_rtx));
109 size -= mcore_stack_increment;
110 }
111 while (size > mcore_stack_increment);
112
113 /* 'size' is now the residual for the last adjustment, which doesn't
114 * require a probe. */
115 }
116
117 if (size)
118 {
119 rtx insn;
120 rtx val = GEN_INT (size);
121
122 if (size > 32)
123 {
124 rtx nval = gen_rtx (REG, SImode, 1);
125 emit_insn (gen_movsi (nval, val));
126 val = nval;
127 }
128
129 if (direction > 0)
130 insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
131 else
132 insn = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
133
134 emit_insn (insn);
135 }
136}
137
138/* Work out the registers which need to be saved, both as a mask and a
139 count. */
140static int
141calc_live_regs (count)
142 int * count;
143{
144 int reg;
145 int live_regs_mask = 0;
146
147 * count = 0;
148
149 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
150 {
151 if (regs_ever_live[reg] && !call_used_regs[reg])
152 {
153 (*count)++;
154 live_regs_mask |= (1 << reg);
155 }
156 }
157
158 return live_regs_mask;
159}
160
161/* Print the operand address in x to the stream. */
162void
163mcore_print_operand_address (stream, x)
164 FILE * stream;
165 rtx x;
166{
167 switch (GET_CODE (x))
168 {
169 case REG:
170 fprintf (stream, "(%s)", reg_names[REGNO (x)]);
171 break;
172
173 case PLUS:
174 {
175 rtx base = XEXP (x, 0);
176 rtx index = XEXP (x, 1);
177
178 if (GET_CODE (base) != REG)
179 {
180 /* Ensure that BASE is a register (one of them must be). */
181 rtx temp = base;
182 base = index;
183 index = temp;
184 }
185
186 switch (GET_CODE (index))
187 {
188 case CONST_INT:
189 fprintf (stream, "(%s,%d)", reg_names[REGNO(base)],
190 INTVAL (index));
191 break;
192
193 default:
194 debug_rtx (x);
195
196 abort ();
197 }
198 }
199
200 break;
201
202 default:
203 output_addr_const (stream, x);
204 break;
205 }
206}
207
208/* Print operand x (an rtx) in assembler syntax to file stream
209 according to modifier code.
210
211 'R' print the next register or memory location along, ie the lsw in
212 a double word value
213 'O' print a constant without the #
214 'M' print a constant as its negative
215 'P' print log2 of a power of two
216 'Q' print log2 of an inverse of a power of two
217 'U' print register for ldm/stm instruction
218 'X' print byte number for xtrbN instruction */
219void
220mcore_print_operand (stream, x, code)
221 FILE * stream;
222 rtx x;
223 int code;
224{
225 switch (code)
226 {
227 case 'N':
228 if (INTVAL(x) == -1)
229 fprintf (asm_out_file, "32");
230 else
231 fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x) + 1));
232 break;
233 case 'P':
234 fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x)));
235 break;
236 case 'Q':
237 fprintf (asm_out_file, "%d", exact_log2 (~INTVAL (x)));
238 break;
239 case 'O':
240 fprintf (asm_out_file, "%d", INTVAL (x));
241 break;
242 case 'M':
243 fprintf (asm_out_file, "%d", - INTVAL (x));
244 break;
245 case 'R':
246 /* Next location along in memory or register. */
247 switch (GET_CODE (x))
248 {
249 case REG:
250 fputs (reg_names[REGNO (x) + 1], (stream));
251 break;
252 case MEM:
253 mcore_print_operand_address (stream,
254 XEXP (adj_offsettable_operand (x, 4), 0));
255 break;
256 default:
257 abort ();
258 }
259 break;
260 case 'U':
261 fprintf (asm_out_file, "%s-%s", reg_names[REGNO (x)],
262 reg_names[REGNO (x) + 3]);
263 break;
264 case 'x':
265 fprintf (asm_out_file, "0x%x", INTVAL (x));
266 break;
267 case 'X':
268 fprintf (asm_out_file, "%d", 3 - INTVAL (x) / 8);
269 break;
270
271 default:
272 switch (GET_CODE (x))
273 {
274 case REG:
275 fputs (reg_names[REGNO (x)], (stream));
276 break;
277 case MEM:
278 output_address (XEXP (x, 0));
279 break;
280 default:
281 output_addr_const (stream, x);
282 break;
283 }
284 break;
285 }
286}
287
288/* What does a constant cost ? */
289int
290mcore_const_costs (exp, code)
291 rtx exp;
292 enum rtx_code code;
293{
294
295 int val = INTVAL (exp);
296
297 /* Easy constants. */
298 if ( CONST_OK_FOR_I (val)
299 || CONST_OK_FOR_M (val)
300 || CONST_OK_FOR_N (val)
301 || (code == PLUS && CONST_OK_FOR_L (val)))
302 return 1;
303 else if (code == AND
304 && ( CONST_OK_FOR_M (~val)
305 || CONST_OK_FOR_N (~val)))
306 return 2;
307 else if (code == PLUS
308 && ( CONST_OK_FOR_I (-val)
309 || CONST_OK_FOR_M (-val)
310 || CONST_OK_FOR_N (-val)))
311 return 2;
312
313 return 5;
314}
315
316/* What does an and instruction cost - we do this b/c immediates may
317 have been relaxed. We want to ensure that cse will cse relaxed immeds
318 out. Otherwise we'll get bad code (multiple reloads of the same const) */
319int
320mcore_and_cost (x)
321 rtx x;
322{
323 int val;
324
325 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
326 return 2;
327
328 val = INTVAL (XEXP (x, 1));
329
330 /* Do it directly. */
331 if (CONST_OK_FOR_K (val) || CONST_OK_FOR_M (~val))
332 return 2;
333 /* Takes one instruction to load. */
334 else if (const_ok_for_mcore (val))
335 return 3;
336 /* Takes two instructions to load. */
337 else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
338 return 4;
339
340 /* takes a lrw to load */
341 return 5;
342}
343
344/* What does an or cost - see and_cost(). */
345int
346mcore_ior_cost (x)
347 rtx x;
348{
349 int val;
350
351 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
352 return 2;
353
354 val = INTVAL (XEXP (x, 1));
355
356 /* Do it directly with bclri. */
357 if (CONST_OK_FOR_M (val))
358 return 2;
359 /* Takes one instruction to load. */
360 else if (const_ok_for_mcore (val))
361 return 3;
362 /* Takes two instructions to load. */
363 else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
364 return 4;
365
366 /* Takes a lrw to load. */
367 return 5;
368}
369
370/* Check to see if a comparison against a constant can be made more efficient
371 by incrementing/decrementing the constant to get one that is more efficient
372 to load. */
373int
374mcore_modify_comparison (code)
375 enum rtx_code code;
376{
377 rtx op1 = arch_compare_op1;
378
379 if (GET_CODE (op1) == CONST_INT)
380 {
381 int val = INTVAL (op1);
382
383 switch (code)
384 {
385 case LE:
386 if (CONST_OK_FOR_J (val + 1))
387 {
388 arch_compare_op1 = GEN_INT (val + 1);
389 return 1;
390 }
391 break;
392
393 default:
394 break;
395 }
396 }
397
398 return 0;
399}
400
401/* Prepare the operands for a comparison. */
402rtx
403mcore_gen_compare_reg (code)
404 enum rtx_code code;
405{
406 rtx op0 = arch_compare_op0;
407 rtx op1 = arch_compare_op1;
408 rtx cc_reg = gen_rtx (REG, CCmode, CC_REG);
409
410 if (CONSTANT_P (op1) && GET_CODE (op1) != CONST_INT)
411 op1 = force_reg (SImode, op1);
412
413 /* cmpnei: 0-31 (K immediate)
414 cmplti: 1-32 (J immediate, 0 using btsti x,31) */
415 switch (code)
416 {
417 case EQ: /* use inverted condition, cmpne */
418 code = NE;
419 /* drop through */
420 case NE: /* use normal condition, cmpne */
421 if (GET_CODE (op1) == CONST_INT && ! CONST_OK_FOR_K (INTVAL (op1)))
422 op1 = force_reg (SImode, op1);
423 break;
424
425 case LE: /* use inverted condition, reversed cmplt */
426 code = GT;
427 /* drop through */
428 case GT: /* use normal condition, reversed cmplt */
429 if (GET_CODE (op1) == CONST_INT)
430 op1 = force_reg (SImode, op1);
431 break;
432
433 case GE: /* use inverted condition, cmplt */
434 code = LT;
435 /* drop through */
436 case LT: /* use normal condition, cmplt */
437 if (GET_CODE (op1) == CONST_INT &&
438 /* covered by btsti x,31 */
439 INTVAL (op1) != 0 &&
440 ! CONST_OK_FOR_J (INTVAL (op1)))
441 op1 = force_reg (SImode, op1);
442 break;
443
444 case GTU: /* use inverted condition, cmple */
445 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) == 0)
446 {
447 /* Unsigned > 0 is the same as != 0, but we need
448 to invert the condition, so we want to set
449 code = EQ. This cannot be done however, as the
450 mcore does not support such a test. Instead we
451 cope with this case in the "bgtu" pattern itself
452 so we should never reach this point. */
453 /* code = EQ; */
454 abort ();
455 break;
456 }
457 code = LEU;
458 /* drop through */
459 case LEU: /* use normal condition, reversed cmphs */
460 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
461 op1 = force_reg (SImode, op1);
462 break;
463
464 case LTU: /* use inverted condition, cmphs */
465 code = GEU;
466 /* drop through */
467 case GEU: /* use normal condition, cmphs */
468 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
469 op1 = force_reg (SImode, op1);
470 break;
471
472 default:
473 break;
474 }
475
476 emit_insn (gen_rtx (SET, VOIDmode, cc_reg, gen_rtx (code, CCmode, op0, op1)));
477
478 return cc_reg;
479}
480
481
482int
483mcore_symbolic_address_p (x)
484 rtx x;
485{
486 switch (GET_CODE (x))
487 {
488 case SYMBOL_REF:
489 case LABEL_REF:
490 return 1;
491 case CONST:
492 x = XEXP (x, 0);
493 return ( (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
494 || GET_CODE (XEXP (x, 0)) == LABEL_REF)
495 && GET_CODE (XEXP (x, 1)) == CONST_INT);
496 default:
497 return 0;
498 }
499}
500
501int
502mcore_call_address_operand (x, mode)
503 rtx x;
504 enum machine_mode mode;
505{
506 return register_operand (x, mode) || CONSTANT_P (x);
507}
508
509/* Functions to output assembly code for a function call. */
510char *
511mcore_output_call (operands, index)
512 rtx operands[];
513 int index;
514{
515 static char buffer[20];
516 rtx addr = operands [index];
517
518 if (REG_P (addr))
519 {
520 if (TARGET_CG_DATA)
521 {
522 if (mcore_current_function_name == 0)
523 abort ();
524
525 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
526 "unknown", 1);
527 }
528
529 sprintf (buffer, "jsr\t%%%d", index);
530 }
531 else
532 {
533 if (TARGET_CG_DATA)
534 {
535 if (mcore_current_function_name == 0)
536 abort ();
537
538 if (GET_CODE (addr) != SYMBOL_REF)
539 abort ();
540
541 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, XSTR (addr, 0), 0);
542 }
543
544 sprintf (buffer, "jbsr\t%%%d", index);
545 }
546
547 return buffer;
548}
549
550/* Can we load a constant with a single instruction ? */
551static int
552const_ok_for_mcore (value)
553 int value;
554{
555 if (value >= 0 && value <= 127)
556 return 1;
557
558 /* Try exact power of two. */
559 if ((value & (value - 1)) == 0)
560 return 1;
561
562 /* Try exact power of two - 1. */
563 if ((value & (value + 1)) == 0)
564 return 1;
565
566 return 0;
567}
568
569/* Can we load a constant inline with up to 2 instructions ? */
570int
571mcore_const_ok_for_inline (value)
572 long value;
573{
574 int x, y;
575
576 return try_constant_tricks (value, & x, & y) > 0;
577}
578
579/* Are we loading the constant using a not ? */
580int
581mcore_const_trick_uses_not (value)
582 long value;
583{
584 int x, y;
585
586 return try_constant_tricks (value, & x, & y) == 2;
587}
588
589/* Try tricks to load a constant inline and return the trick number if
590 success (0 is non-inlinable).
591 *
592 * 0: not inlinable
593 * 1: single instruction (do the usual thing)
594 * 2: single insn followed by a 'not'
595 * 3: single insn followed by a subi
596 * 4: single insn followed by an addi
597 * 5: single insn followed by rsubi
598 * 6: single insn followed by bseti
599 * 7: single insn followed by bclri
600 * 8: single insn followed by rotli
601 * 9: single insn followed by lsli
602 * 10: single insn followed by ixh
603 * 11: single insn followed by ixw
604 */
605
606static int
607try_constant_tricks (value, x, y)
608 long value;
609 int * x;
610 int * y;
611{
612 int i;
613 unsigned bit, shf, rot;
614
615 if (const_ok_for_mcore (value))
616 return 1; /* do the usual thing */
617
618 if (TARGET_HARDLIT)
619 {
620 if (const_ok_for_mcore (~value))
621 {
622 *x = ~value;
623 return 2;
624 }
625
626 for (i = 1; i <= 32; i++)
627 {
628 if (const_ok_for_mcore (value - i))
629 {
630 *x = value - i;
631 *y = i;
632
633 return 3;
634 }
635
636 if (const_ok_for_mcore (value + i))
637 {
638 *x = value + i;
639 *y = i;
640
641 return 4;
642 }
643 }
644
645 bit = 0x80000000UL;
646
647 for (i = 0; i <= 31; i++)
648 {
649 if (const_ok_for_mcore (i - value))
650 {
651 *x = i - value;
652 *y = i;
653
654 return 5;
655 }
656
657 if (const_ok_for_mcore (value & ~bit))
658 {
659 *y = bit;
660 *x = value & ~bit;
661
662 return 6;
663 }
664
665 if (const_ok_for_mcore (value | bit))
666 {
667 *y = ~bit;
668 *x = value | bit;
669
670 return 7;
671 }
672
673 bit >>= 1;
674 }
675
676 shf = value;
677 rot = value;
678
679 for (i = 1; i < 31; i++)
680 {
681 int c;
682
683 /* MCore has rotate left. */
684 c = rot << 31;
685 rot >>= 1;
686 rot &= 0x7FFFFFFF;
687 rot |= c; /* Simulate rotate. */
688
689 if (const_ok_for_mcore (rot))
690 {
691 *y = i;
692 *x = rot;
693
694 return 8;
695 }
696
697 if (shf & 1)
698 shf = 0; /* Can't use logical shift, low order bit is one. */
699
700 shf >>= 1;
701
702 if (shf != 0 && const_ok_for_mcore (shf))
703 {
704 *y = i;
705 *x = shf;
706
707 return 9;
708 }
709 }
710
711 if ((value % 3) == 0 && const_ok_for_mcore (value / 3))
712 {
713 *x = value / 3;
714
715 return 10;
716 }
717
718 if ((value % 5) == 0 && const_ok_for_mcore (value / 5))
719 {
720 *x = value / 5;
721
722 return 11;
723 }
724 }
725
726 return 0;
727}
728
729
730/* Check whether reg is dead at first. This is done by searching ahead
731 for either the next use (i.e., reg is live), a death note, or a set of
732 reg. Don't just use dead_or_set_p() since reload does not always mark
733 deaths (especially if PRESERVE_DEATH_NOTES_REGNO_P is not defined). We
734 can ignore subregs by extracting the actual register. BRC */
735int
736mcore_is_dead (first, reg)
737 rtx first;
738 rtx reg;
739{
740 rtx insn;
741
742 /* For mcore, subregs can't live independently of their parent regs. */
743 if (GET_CODE (reg) == SUBREG)
744 reg = SUBREG_REG (reg);
745
746 /* Dies immediately. */
747 if (dead_or_set_p (first, reg))
748 return 1;
749
750 /* Look for conclusive evidence of live/death, otherwise we have
751 to assume that it is live. */
752 for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
753 {
754 if (GET_CODE (insn) == JUMP_INSN)
755 return 0; /* We lose track, assume it is alive. */
756
757 else if (GET_CODE(insn) == CALL_INSN)
758 {
759 /* Call's might use it for target or register parms. */
760 if (reg_referenced_p (reg, PATTERN (insn))
761 || find_reg_fusage (insn, USE, reg))
762 return 0;
763 else if (dead_or_set_p (insn, reg))
764 return 1;
765 }
766 else if (GET_CODE (insn) == INSN)
767 {
768 if (reg_referenced_p (reg, PATTERN (insn)))
769 return 0;
770 else if (dead_or_set_p (insn, reg))
771 return 1;
772 }
773 }
774
775 /* No conclusive evidence either way, we can not take the chance
776 that control flow hid the use from us -- "I'm not dead yet". */
777 return 0;
778}
779
780
781/* Count the number of ones in mask. */
782int
783mcore_num_ones (mask)
784 int mask;
785{
786 /* A trick to count set bits recently posted on comp.compilers */
787 mask = (mask >> 1 & 0x55555555) + (mask & 0x55555555);
788 mask = ((mask >> 2) & 0x33333333) + (mask & 0x33333333);
789 mask = ((mask >> 4) + mask) & 0x0f0f0f0f;
790 mask = ((mask >> 8) + mask);
791
792 return (mask + (mask >> 16)) & 0xff;
793}
794
795/* Count the number of zeros in mask. */
796int
797mcore_num_zeros (mask)
798 int mask;
799{
800 return 32 - mcore_num_ones (mask);
801}
802
803/* Determine byte being masked. */
804int
805mcore_byte_offset (mask)
806 unsigned int mask;
807{
808 if (mask == 0x00ffffffUL)
809 return 0;
810 else if (mask == 0xff00ffffUL)
811 return 1;
812 else if (mask == 0xffff00ffUL)
813 return 2;
814 else if (mask == 0xffffff00UL)
815 return 3;
816
817 return -1;
818}
819
820/* Determine halfword being masked. */
821int
822mcore_halfword_offset (mask)
823 unsigned int mask;
824{
825 if (mask == 0x0000ffffL)
826 return 0;
827 else if (mask == 0xffff0000UL)
828 return 1;
829
830 return -1;
831}
832
833/* Output a series of bseti's corresponding to mask. */
834char *
835mcore_output_bseti (dst, mask)
836 rtx dst;
837 int mask;
838{
839 rtx out_operands[2];
840 int bit;
841
842 out_operands[0] = dst;
843
844 for (bit = 0; bit < 32; bit++)
845 {
846 if ((mask & 0x1) == 0x1)
847 {
848 out_operands[1] = GEN_INT (bit);
849
850 output_asm_insn ("bseti\t%0,%1", out_operands);
851 }
852 mask >>= 1;
853 }
854
855 return "";
856}
857
858/* Output a series of bclri's corresponding to mask. */
859char *
860mcore_output_bclri (dst, mask)
861 rtx dst;
862 int mask;
863{
864 rtx out_operands[2];
865 int bit;
866
867 out_operands[0] = dst;
868
869 for (bit = 0; bit < 32; bit++)
870 {
871 if ((mask & 0x1) == 0x0)
872 {
873 out_operands[1] = GEN_INT (bit);
874
875 output_asm_insn ("bclri\t%0,%1", out_operands);
876 }
877
878 mask >>= 1;
879 }
880
881 return "";
882}
883
884/* Output a conditional move of two constants that are +/- 1 within each
885 other. See the "movtK" patterns in mcore.md. I'm not sure this is
886 really worth the effort. */
887char *
888mcore_output_cmov (operands, cmp_t, test)
889 rtx operands[];
890 int cmp_t;
891 char * test;
892{
893 int load_value;
894 int adjust_value;
895 rtx out_operands[4];
896
897 out_operands[0] = operands[0];
898
899 /* check to see which constant is loadable */
900
901 if (const_ok_for_mcore (INTVAL (operands[1])))
902 {
903 out_operands[1] = operands[1];
904 out_operands[2] = operands[2];
905 }
906 else if (const_ok_for_mcore (INTVAL (operands[2])))
907 {
908 out_operands[1] = operands[2];
909 out_operands[2] = operands[1];
910
911 /* complement test since constants are swapped */
912 cmp_t = (cmp_t == 0);
913 }
914 load_value = INTVAL (out_operands[1]);
915 adjust_value = INTVAL (out_operands[2]);
916
917 /* first output the test if folded into the pattern */
918
919 if (test)
920 output_asm_insn (test, operands);
921
922 /* load the constant - for now, only support constants that can be
923 generated with a single instruction. maybe add general inlinable
924 constants later (this will increase the # of patterns since the
925 instruction sequence has a different length attribute). */
926
927 if (load_value >= 0 && load_value <= 127)
928 output_asm_insn ("movi\t%0,%1", out_operands);
929 else if ((load_value & (load_value - 1)) == 0)
930 output_asm_insn ("bgeni\t%0,%P1", out_operands);
931 else if ((load_value & (load_value + 1)) == 0)
932 output_asm_insn ("bmaski\t%0,%N1", out_operands);
933
934 /* output the constant adjustment */
935
936 if (load_value > adjust_value)
937 {
938 if (cmp_t)
939 output_asm_insn ("decf\t%0", out_operands);
940 else
941 output_asm_insn ("dect\t%0", out_operands);
942 }
943 else
944 {
945 if (cmp_t)
946 output_asm_insn ("incf\t%0", out_operands);
947 else
948 output_asm_insn ("inct\t%0", out_operands);
949 }
950
951 return "";
952}
953
954/* Outputs the peephole for moving a constant that gets not'ed followed
955 by an and (i.e. combine the not and the and into andn) BRC */
956char *
957mcore_output_andn (insn, operands)
958 rtx insn ATTRIBUTE_UNUSED;
959 rtx operands[];
960{
961 int x, y;
962 rtx out_operands[3];
963 char * load_op;
964 char buf[256];
965
966 if (try_constant_tricks (INTVAL (operands[1]), &x, &y) != 2)
967 abort ();
968
969 out_operands[0] = operands[0];
970 out_operands[1] = GEN_INT(x);
971 out_operands[2] = operands[2];
972
973 if (x >= 0 && x <= 127)
974 load_op = "movi\t%0,%1";
975 /* try exact power of two */
976 else if ((x & (x - 1)) == 0)
977 load_op = "bgeni\t%0,%P1";
978 /* try exact power of two - 1 */
979 else if ((x & (x + 1)) == 0)
980 load_op = "bmaski\t%0,%N1";
981 else
982 load_op = "BADMOVI\t%0,%1";
983
984 sprintf (buf, "%s\n\tandn\t%%2,%%0", load_op);
985 output_asm_insn (buf, out_operands);
986
987 return "";
988}
989
990/* Output an inline constant. */
991static char *
992output_inline_const (mode, operands)
993 enum machine_mode mode;
994 rtx operands[];
995{
996 int x = 0, y = 0;
997 int trick_no;
998 rtx out_operands[3];
999 char buf[256];
1000 char load_op[256];
1001 char *dst_fmt;
1002 int value;
1003
1004 value = INTVAL (operands[1]);
1005
1006 if ((trick_no = try_constant_tricks (value, &x, &y)) == 0)
1007 {
1008 /* lrw's are handled separately: Large inlinable constants
1009 never get turned into lrw's. Our caller uses try_constant_tricks
1010 to back off to an lrw rather than calling this routine. */
1011 abort ();
1012 }
1013
1014 if (trick_no == 1)
1015 x = value;
1016
1017 /* operands: 0 = dst, 1 = load immed., 2 = immed. adjustment */
1018
1019 out_operands[0] = operands[0];
1020 out_operands[1] = GEN_INT (x);
1021
1022 if (trick_no > 2)
1023 out_operands[2] = GEN_INT (y);
1024
1025 /* Select dst format based on mode */
1026
1027 if (mode == DImode && (! TARGET_LITTLE_END))
1028 dst_fmt = "%R0";
1029 else
1030 dst_fmt = "%0";
1031
1032 if (x >= 0 && x <= 127)
1033 sprintf (load_op, "movi\t%s,%%1", dst_fmt);
1034 /* Try exact power of two. */
1035 else if ((x & (x - 1)) == 0)
1036 sprintf (load_op, "bgeni\t%s,%%P1", dst_fmt);
1037 /* try exact power of two - 1. */
1038 else if ((x & (x + 1)) == 0)
1039 sprintf (load_op, "bmaski\t%s,%%N1", dst_fmt);
1040 else
1041 sprintf (load_op, "BADMOVI\t%s,%%1", dst_fmt);
1042
1043 switch (trick_no)
1044 {
1045 case 1:
1046 strcpy (buf, load_op);
1047 break;
1048 case 2: /* not */
1049 sprintf (buf, "%s\n\tnot\t%s\t// %d 0x%x", load_op, dst_fmt, value, value);
1050 break;
1051 case 3: /* add */
1052 sprintf (buf, "%s\n\taddi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1053 break;
1054 case 4: /* sub */
1055 sprintf (buf, "%s\n\tsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1056 break;
1057 case 5: /* rsub */
1058 /* never happens unless -mrsubi, see try_constant_tricks() */
1059 sprintf (buf, "%s\n\trsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1060 break;
1061 case 6: /* bset */
1062 sprintf (buf, "%s\n\tbseti\t%s,%%P2\t// %d 0x%x", load_op, dst_fmt, value, value);
1063 break;
1064 case 7: /* bclr */
1065 sprintf (buf, "%s\n\tbclri\t%s,%%Q2\t// %d 0x%x", load_op, dst_fmt, value, value);
1066 break;
1067 case 8: /* rotl */
1068 sprintf (buf, "%s\n\trotli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1069 break;
1070 case 9: /* lsl */
1071 sprintf (buf, "%s\n\tlsli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1072 break;
1073 case 10: /* ixh */
1074 sprintf (buf, "%s\n\tixh\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1075 break;
1076 case 11: /* ixw */
1077 sprintf (buf, "%s\n\tixw\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1078 break;
1079 default:
1080 return "";
1081 }
1082
1083 output_asm_insn (buf, out_operands);
1084
1085 return "";
1086}
1087
1088/* Output a move of a word or less value. */
1089char *
1090mcore_output_move (insn, operands, mode)
1091 rtx insn ATTRIBUTE_UNUSED;
1092 rtx operands[];
1093 enum machine_mode mode ATTRIBUTE_UNUSED;
1094{
1095 rtx dst = operands[0];
1096 rtx src = operands[1];
1097
1098 if (GET_CODE (dst) == REG)
1099 {
1100 if (GET_CODE (src) == REG)
1101 {
1102 if (REGNO (src) == CC_REG) /* r-c */
1103 return "mvc\t%0";
1104 else
1105 return "mov\t%0,%1"; /* r-r*/
1106 }
1107 else if (GET_CODE (src) == MEM)
1108 {
1109 if (GET_CODE (XEXP (src, 0)) == LABEL_REF)
1110 return "lrw\t%0,[%1]"; /* a-R */
1111 else
1112 return "ldw\t%0,%1"; /* r-m */
1113 }
1114 else if (GET_CODE (src) == CONST_INT)
1115 {
1116 int x, y;
1117
1118 if (CONST_OK_FOR_I (INTVAL (src))) /* r-I */
1119 return "movi\t%0,%1";
1120 else if (CONST_OK_FOR_M (INTVAL (src))) /* r-M */
1121 return "bgeni\t%0,%P1\t// %1 %x1";
1122 else if (CONST_OK_FOR_N (INTVAL (src))) /* r-N */
1123 return "bmaski\t%0,%N1\t// %1 %x1";
1124 else if (try_constant_tricks (INTVAL (src), &x, &y)) /* R-P */
1125 return output_inline_const (SImode, operands); /* 1-2 insns */
1126 else
1127 return "lrw\t%0,%x1\t// %1"; /* get it from literal pool */
1128 }
1129 else
1130 return "lrw\t%0, %1"; /* into the literal pool */
1131 }
1132 else if (GET_CODE (dst) == MEM) /* m-r */
1133 return "stw\t%1,%0";
1134
1135 abort ();
1136}
1137
1138/* Outputs a constant inline -- regardless of the cost.
1139 Useful for things where we've gotten into trouble and think we'd
1140 be doing an lrw into r15 (forbidden). This lets us get out of
1141 that pickle even after register allocation. */
1142char *
1143mcore_output_inline_const_forced (insn, operands, mode)
1144 rtx insn ATTRIBUTE_UNUSED;
1145 rtx operands[];
1146 enum machine_mode mode ATTRIBUTE_UNUSED;
1147{
1148 unsigned long value = INTVAL (operands[1]);
1149 unsigned long ovalue = value;
1150 struct piece
1151 {
1152 int low;
1153 int shift;
1154 }
1155 part[6];
1156 int i;
1157
1158 if (mcore_const_ok_for_inline (value))
1159 return output_inline_const (SImode, operands);
1160
1161 for (i = 0; (unsigned) i < sizeof (part) / sizeof (part[0]); i++)
1162 {
1163 part[i].shift = 0;
1164 part[i].low = (value & 0x1F);
1165 value -= part[i].low;
1166
1167 if (mcore_const_ok_for_inline (value))
1168 break;
1169 else
1170 {
1171 value >>= 5;
1172 part[i].shift = 5;
1173
1174 while ((value & 1) == 0)
1175 {
1176 part[i].shift++;
1177 value >>= 1;
1178 }
1179
1180 if (mcore_const_ok_for_inline (value))
1181 break;
1182 }
1183 }
1184
1185 /* 5 bits per iteration, a maximum of 5 times == 25 bits and leaves
1186 7 bits left in the constant -- which we know we can cover with
1187 a movi. The final value can't be zero otherwise we'd have stopped
1188 in the previous iteration. */
1189 if (value == 0 || ! mcore_const_ok_for_inline (value))
1190 abort ();
1191
1192 /* Now, work our way backwards emitting the constant. */
1193
1194 /* Emit the value that remains -- it will be non-zero. */
1195 operands[1] = GEN_INT (value);
1196 output_asm_insn (output_inline_const (SImode, operands), operands);
1197
1198 while (i >= 0)
1199 {
1200 /* Shift anything we've already loaded. */
1201 if (part[i].shift)
1202 {
1203 operands[2] = GEN_INT (part[i].shift);
1204 output_asm_insn ("lsli %0,%2", operands);
1205 value <<= part[i].shift;
1206 }
1207
1208 /* Add anything we need into the low 5 bits. */
1209 if (part[i].low != 0)
1210 {
1211 operands[2] = GEN_INT (part[i].low);
1212 output_asm_insn ("addi %0,%2", operands);
1213 value += part[i].low;
1214 }
1215
1216 i--;
1217 }
1218
1219 if (value != ovalue) /* sanity */
1220 abort ();
1221
1222 /* We've output all the instructions. */
1223 return "";
1224}
1225
1226/* Return a sequence of instructions to perform DI or DF move.
1227 Since the MCORE cannot move a DI or DF in one instruction, we have
1228 to take care when we see overlapping source and dest registers. */
1229char *
1230mcore_output_movedouble (operands, mode)
1231 rtx operands[];
1232 enum machine_mode mode ATTRIBUTE_UNUSED;
1233{
1234 rtx dst = operands[0];
1235 rtx src = operands[1];
1236
1237 if (GET_CODE (dst) == REG)
1238 {
1239 if (GET_CODE (src) == REG)
1240 {
1241 int dstreg = REGNO (dst);
1242 int srcreg = REGNO (src);
1243 /* Ensure the second source not overwritten. */
1244 if (srcreg + 1 == dstreg)
1245 return "mov %R0,%R1\n\tmov %0,%1";
1246 else
1247 return "mov %0,%1\n\tmov %R0,%R1";
1248 }
1249 else if (GET_CODE (src) == MEM)
1250 {
1251 rtx memexp = memexp = XEXP (src, 0);
1252 int dstreg = REGNO (dst);
1253 int basereg = -1;
1254
1255 if (GET_CODE (memexp) == LABEL_REF)
1256 return "lrw\t%0,[%1]\n\tlrw\t%R0,[%R1]";
1257 else if (GET_CODE (memexp) == REG)
1258 basereg = REGNO (memexp);
1259 else if (GET_CODE (memexp) == PLUS)
1260 {
1261 if (GET_CODE (XEXP (memexp, 0)) == REG)
1262 basereg = REGNO (XEXP (memexp, 0));
1263 else if (GET_CODE (XEXP (memexp, 1)) == REG)
1264 basereg = REGNO (XEXP (memexp, 1));
1265 else
1266 abort ();
1267 }
1268 else
1269 abort ();
1270
1271 /* ??? length attribute is wrong here */
1272 if (dstreg == basereg)
1273 {
1274 /* just load them in reverse order */
1275 return "ldw\t%R0,%R1\n\tldw\t%0,%1";
1276 /* XXX: alternative: move basereg to basereg+1
1277 * and then fall through */
1278 }
1279 else
1280 return "ldw\t%0,%1\n\tldw\t%R0,%R1";
1281 }
1282 else if (GET_CODE (src) == CONST_INT)
1283 {
1284 if (TARGET_LITTLE_END)
1285 {
1286 if (CONST_OK_FOR_I (INTVAL (src)))
1287 output_asm_insn ("movi %0,%1", operands);
1288 else if (CONST_OK_FOR_M (INTVAL (src)))
1289 output_asm_insn ("bgeni %0,%P1", operands);
1290 else if (INTVAL (src) == -1)
1291 output_asm_insn ("bmaski %0,32", operands);
1292 else if (CONST_OK_FOR_N (INTVAL (src)))
1293 output_asm_insn ("bmaski %0,%N1", operands);
1294 else
1295 abort ();
1296
1297 if (INTVAL (src) < 0)
1298 return "bmaski %R0,32";
1299 else
1300 return "movi %R0,0";
1301 }
1302 else
1303 {
1304 if (CONST_OK_FOR_I (INTVAL (src)))
1305 output_asm_insn ("movi %R0,%1", operands);
1306 else if (CONST_OK_FOR_M (INTVAL (src)))
1307 output_asm_insn ("bgeni %R0,%P1", operands);
1308 else if (INTVAL (src) == -1)
1309 output_asm_insn ("bmaski %R0,32", operands);
1310 else if (CONST_OK_FOR_N (INTVAL (src)))
1311 output_asm_insn ("bmaski %R0,%N1", operands);
1312 else
1313 abort ();
1314
1315 if (INTVAL (src) < 0)
1316 return "bmaski %0,32";
1317 else
1318 return "movi %0,0";
1319 }
1320 }
1321 else
1322 abort ();
1323 }
1324 else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
1325 return "stw\t%1,%0\n\tstw\t%R1,%R0";
1326 else
1327 abort ();
1328}
1329
1330/* Predicates used by the templates. */
1331
1332/* Non zero if OP can be source of a simple move operation. */
1333int
1334mcore_general_movsrc_operand (op, mode)
1335 rtx op;
1336 enum machine_mode mode;
1337{
1338 /* Any (MEM LABEL_REF) is OK. That is a pc-relative load. */
1339 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == LABEL_REF)
1340 return 1;
1341
1342 return general_operand (op, mode);
1343}
1344
1345/* Non zero if OP can be destination of a simple move operation. */
1346int
1347mcore_general_movdst_operand (op, mode)
1348 rtx op;
1349 enum machine_mode mode;
1350{
1351 if (GET_CODE (op) == REG && REGNO (op) == CC_REG)
1352 return 0;
1353
1354 return general_operand (op, mode);
1355}
1356
1357/* Nonzero if OP is a normal arithmetic register. */
1358int
1359mcore_arith_reg_operand (op, mode)
1360 rtx op;
1361 enum machine_mode mode;
1362{
1363 if (! register_operand (op, mode))
1364 return 0;
1365
1366 if (GET_CODE (op) == SUBREG)
1367 op = SUBREG_REG (op);
1368
1369 if (GET_CODE (op) == REG)
1370 return REGNO (op) != CC_REG;
1371
1372 return 1;
1373}
1374
1375/* Non zero if OP should be recognized during reload for an ixh/ixw
1376 operand. See the ixh/ixw patterns. */
1377int
1378mcore_reload_operand (op, mode)
1379 rtx op;
1380 enum machine_mode mode;
1381{
1382 if (mcore_arith_reg_operand (op, mode))
1383 return 1;
1384
1385 if (! reload_in_progress)
1386 return 0;
1387
1388 return GET_CODE (op) == MEM;
1389}
1390
1391/* Nonzero if OP is a valid source operand for an arithmetic insn. */
1392int
1393mcore_arith_J_operand (op, mode)
1394 rtx op;
1395 enum machine_mode mode;
1396{
1397 if (register_operand (op, mode))
1398 return 1;
1399
1400 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_J (INTVAL (op)))
1401 return 1;
1402
1403 return 0;
1404}
1405
1406/* Nonzero if OP is a valid source operand for an arithmetic insn. */
1407int
1408mcore_arith_K_operand (op, mode)
1409 rtx op;
1410 enum machine_mode mode;
1411{
1412 if (register_operand (op, mode))
1413 return 1;
1414
1415 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
1416 return 1;
1417
1418 return 0;
1419}
1420
1421/* Nonzero if OP is a valid source operand for a shift or rotate insn. */
1422int
1423mcore_arith_K_operand_not_0 (op, mode)
1424 rtx op;
1425 enum machine_mode mode;
1426{
1427 if (register_operand (op, mode))
1428 return 1;
1429
1430 if ( GET_CODE (op) == CONST_INT
1431 && CONST_OK_FOR_K (INTVAL (op))
1432 && INTVAL (op) != 0)
1433 return 1;
1434
1435 return 0;
1436}
1437
1438int
1439mcore_arith_K_S_operand (op, mode)
1440 rtx op;
1441 enum machine_mode mode;
1442{
1443 if (register_operand (op, mode))
1444 return 1;
1445
1446 if (GET_CODE (op) == CONST_INT)
1447 {
1448 if (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_M (~INTVAL (op)))
1449 return 1;
1450 }
1451
1452 return 0;
1453}
1454
1455int
1456mcore_arith_S_operand (op)
1457 rtx op;
1458{
1459 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (~INTVAL (op)))
1460 return 1;
1461
1462 return 0;
1463}
1464
1465int
1466mcore_arith_M_operand (op, mode)
1467 rtx op;
1468 enum machine_mode mode;
1469{
1470 if (register_operand (op, mode))
1471 return 1;
1472
1473 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
1474 return 1;
1475
1476 return 0;
1477}
1478
1479/* Nonzero if OP is a valid source operand for loading */
1480int
1481mcore_arith_imm_operand (op, mode)
1482 rtx op;
1483 enum machine_mode mode;
1484{
1485 if (register_operand (op, mode))
1486 return 1;
1487
1488 if (GET_CODE (op) == CONST_INT && const_ok_for_mcore (INTVAL (op)))
1489 return 1;
1490
1491 return 0;
1492}
1493
1494int
1495mcore_arith_any_imm_operand (op, mode)
1496 rtx op;
1497 enum machine_mode mode;
1498{
1499 if (register_operand (op, mode))
1500 return 1;
1501
1502 if (GET_CODE (op) == CONST_INT)
1503 return 1;
1504
1505 return 0;
1506}
1507
1508/* Nonzero if OP is a valid source operand for a cmov with two consts +/- 1 */
1509int
1510mcore_arith_O_operand (op, mode)
1511 rtx op;
1512 enum machine_mode mode;
1513{
1514 if (register_operand (op, mode))
1515 return 1;
1516
1517 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_O (INTVAL (op)))
1518 return 1;
1519
1520 return 0;
1521}
1522
1523/* Nonzero if OP is a valid source operand for a btsti. */
1524int
1525mcore_literal_K_operand (op, mode)
1526 rtx op;
1527 enum machine_mode mode ATTRIBUTE_UNUSED;
1528{
1529 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
1530 return 1;
1531
1532 return 0;
1533}
1534
1535/* Nonzero if OP is a valid source operand for an add/sub insn. */
1536int
1537mcore_addsub_operand (op, mode)
1538 rtx op;
1539 enum machine_mode mode;
1540{
1541 if (register_operand (op, mode))
1542 return 1;
1543
1544 if (GET_CODE (op) == CONST_INT)
1545 {
1546 return 1;
1547
1548 /* The following is removed because it precludes large constants from being
1549 returned as valid source operands for and add/sub insn. While large
1550 constants may not directly be used in an add/sub, they may if first loaded
1551 into a register. Thus, this predicate should indicate that they are valid,
1552 and the constraint in mcore.md should control whether an additional load to
1553 register is needed. (see mcore.md, addsi) -- DAC 4/2/1998 */
1554 /*
1555 if (CONST_OK_FOR_J(INTVAL(op)) || CONST_OK_FOR_L(INTVAL(op)))
1556 return 1;
1557 */
1558 }
1559
1560 return 0;
1561}
1562
1563/* Nonzero if OP is a valid source operand for a compare operation. */
1564int
1565mcore_compare_operand (op, mode)
1566 rtx op;
1567 enum machine_mode mode;
1568{
1569 if (register_operand (op, mode))
1570 return 1;
1571
1572 if (GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
1573 return 1;
1574
1575 return 0;
1576}
1577
1578/* Expand insert bit field. BRC */
1579int
1580mcore_expand_insv (operands)
1581 rtx operands[];
1582{
1583 int width = INTVAL (operands[1]);
1584 int posn = INTVAL (operands[2]);
1585 int mask;
1586 rtx mreg, sreg, ereg;
1587
1588 /* To get width 1 insv, the test in store_bit_field() (expmed.c, line 191)
1589 for width==1 must be removed. Look around line 368. This is something
1590 we really want the md part to do. */
1591
1592 if (width == 1 && GET_CODE (operands[3]) == CONST_INT)
1593 {
1594 /* Do directly with bseti or bclri */
1595 /* RBE: 2/97 consider only low bit of constant */
1596 if ((INTVAL(operands[3])&1) == 0)
1597 {
1598 mask = ~(1 << posn);
1599 emit_insn (gen_rtx (SET, SImode, operands[0],
1600 gen_rtx (AND, SImode, operands[0], GEN_INT (mask))));
1601 }
1602 else
1603 {
1604 mask = 1 << posn;
1605 emit_insn (gen_rtx (SET, SImode, operands[0],
1606 gen_rtx (IOR, SImode, operands[0], GEN_INT (mask))));
1607 }
1608
1609 return 1;
1610 }
1611
1612 /* Look at some bitfield placements that we aren't interested
1613 * in handling ourselves, unless specifically directed to do so */
1614 if (! TARGET_W_FIELD)
1615 return 0; /* Generally, give up about now. */
1616
1617 if (width == 8 && posn % 8 == 0)
1618 /* Byte sized and aligned; let caller break it up. */
1619 return 0;
1620
1621 if (width == 16 && posn % 16 == 0)
1622 /* Short sized and aligned; let caller break it up. */
1623 return 0;
1624
1625 /* The general case - we can do this a little bit better than what the
1626 machine independent part tries. This will get rid of all the subregs
1627 that mess up constant folding in combine when working with relaxed
1628 immediates. */
1629
1630 /* If setting the entire field, do it directly. */
1631 if (GET_CODE (operands[3]) == CONST_INT &&
1632 INTVAL (operands[3]) == ((1 << width) - 1))
1633 {
1634 mreg = force_reg (SImode, GEN_INT (INTVAL (operands[3]) << posn));
1635 emit_insn (gen_rtx (SET, SImode, operands[0],
1636 gen_rtx (IOR, SImode, operands[0], mreg)));
1637 return 1;
1638 }
1639
1640 /* Generate the clear mask. */
1641 mreg = force_reg (SImode, GEN_INT (~(((1 << width) - 1) << posn)));
1642
1643 /* Clear the field, to overlay it later with the source. */
1644 emit_insn (gen_rtx (SET, SImode, operands[0],
1645 gen_rtx (AND, SImode, operands[0], mreg)));
1646
1647 /* If the source is constant 0, we've nothing to add back. */
1648 if (GET_CODE (operands[3]) == CONST_INT && INTVAL (operands[3]) == 0)
1649 return 1;
1650
1651 /* XXX: Should we worry about more games with constant values?
1652 We've covered the high profile: set/clear single-bit and many-bit
1653 fields. How often do we see "arbitrary bit pattern" constants? */
1654 sreg = copy_to_mode_reg (SImode, operands[3]);
1655
1656 /* Extract src as same width as dst (needed for signed values). We
1657 always have to do this since we widen everything to SImode.
1658 We don't have to mask if we're shifting this up against the
1659 MSB of the register (e.g., the shift will push out any hi-order
1660 bits. */
1661 if (width + posn != GET_MODE_SIZE (SImode))
1662 {
1663 ereg = force_reg (SImode, GEN_INT ((1 << width) - 1));
1664 emit_insn (gen_rtx (SET, SImode, sreg,
1665 gen_rtx (AND, SImode, sreg, ereg)));
1666 }
1667
1668 /* Insert source value in dest. */
1669 if (posn != 0)
1670 emit_insn (gen_rtx (SET, SImode, sreg,
1671 gen_rtx (ASHIFT, SImode, sreg, GEN_INT (posn))));
1672
1673 emit_insn (gen_rtx (SET, SImode, operands[0],
1674 gen_rtx (IOR, SImode, operands[0], sreg)));
1675
1676 return 1;
1677}
1678
1679/* Return 1 if OP is a load multiple operation. It is known to be a
1680 PARALLEL and the first section will be tested. */
1681int
1682mcore_load_multiple_operation (op, mode)
1683 rtx op;
1684 enum machine_mode mode ATTRIBUTE_UNUSED;
1685{
1686 int count = XVECLEN (op, 0);
1687 int dest_regno;
1688 rtx src_addr;
1689 int i;
1690
1691 /* Perform a quick check so we don't blow up below. */
1692 if (count <= 1
1693 || GET_CODE (XVECEXP (op, 0, 0)) != SET
1694 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
1695 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
1696 return 0;
1697
1698 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
1699 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
1700
1701 for (i = 1; i < count; i++)
1702 {
1703 rtx elt = XVECEXP (op, 0, i);
1704
1705 if (GET_CODE (elt) != SET
1706 || GET_CODE (SET_DEST (elt)) != REG
1707 || GET_MODE (SET_DEST (elt)) != SImode
1708 || REGNO (SET_DEST (elt)) != dest_regno + i
1709 || GET_CODE (SET_SRC (elt)) != MEM
1710 || GET_MODE (SET_SRC (elt)) != SImode
1711 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
1712 || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
1713 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
1714 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
1715 return 0;
1716 }
1717
1718 return 1;
1719}
1720
1721/* Similar, but tests for store multiple. */
1722int
1723mcore_store_multiple_operation (op, mode)
1724 rtx op;
1725 enum machine_mode mode ATTRIBUTE_UNUSED;
1726{
1727 int count = XVECLEN (op, 0);
1728 int src_regno;
1729 rtx dest_addr;
1730 int i;
1731
1732 /* Perform a quick check so we don't blow up below. */
1733 if (count <= 1
1734 || GET_CODE (XVECEXP (op, 0, 0)) != SET
1735 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
1736 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
1737 return 0;
1738
1739 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
1740 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
1741
1742 for (i = 1; i < count; i++)
1743 {
1744 rtx elt = XVECEXP (op, 0, i);
1745
1746 if (GET_CODE (elt) != SET
1747 || GET_CODE (SET_SRC (elt)) != REG
1748 || GET_MODE (SET_SRC (elt)) != SImode
1749 || REGNO (SET_SRC (elt)) != src_regno + i
1750 || GET_CODE (SET_DEST (elt)) != MEM
1751 || GET_MODE (SET_DEST (elt)) != SImode
1752 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
1753 || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
1754 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
1755 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
1756 return 0;
1757 }
1758
1759 return 1;
1760}
1761\f
1762/* ??? Block move stuff stolen from m88k. This code has not been
1763 verified for correctness. */
1764
1765/* Emit code to perform a block move. Choose the best method.
1766
1767 OPERANDS[0] is the destination.
1768 OPERANDS[1] is the source.
1769 OPERANDS[2] is the size.
1770 OPERANDS[3] is the alignment safe to use. */
1771
1772/* Emit code to perform a block move with an offset sequence of ldw/st
1773 instructions (..., ldw 0, stw 1, ldw 1, stw 0, ...). SIZE and ALIGN are
1774 known constants. DEST and SRC are registers. OFFSET is the known
1775 starting point for the output pattern. */
1776
1777static enum machine_mode mode_from_align[] =
1778{
1779 VOIDmode, QImode, HImode, VOIDmode, SImode,
1780 VOIDmode, VOIDmode, VOIDmode, DImode
1781};
1782
1783static void
1784block_move_sequence (dest, dst_mem, src, src_mem, size, align, offset)
1785 rtx dest, dst_mem;
1786 rtx src, src_mem;
1787 int size;
1788 int align;
1789 int offset;
1790{
1791 rtx temp[2];
1792 enum machine_mode mode[2];
1793 int amount[2];
1794 int active[2];
1795 int phase = 0;
1796 int next;
1797 int offset_ld = offset;
1798 int offset_st = offset;
1799
1800 active[0] = active[1] = FALSE;
1801
1802 /* Establish parameters for the first load and for the second load if
1803 it is known to be the same mode as the first. */
1804 amount[0] = amount[1] = align;
1805
1806 mode[0] = mode_from_align[align];
1807
1808 temp[0] = gen_reg_rtx (mode[0]);
1809
1810 if (size >= 2 * align)
1811 {
1812 mode[1] = mode[0];
1813 temp[1] = gen_reg_rtx (mode[1]);
1814 }
1815
1816 do
1817 {
1818 rtx srcp, dstp;
1819
1820 next = phase;
1821 phase = !phase;
1822
1823 if (size > 0)
1824 {
1825 /* Change modes as the sequence tails off. */
1826 if (size < amount[next])
1827 {
1828 amount[next] = (size >= 4 ? 4 : (size >= 2 ? 2 : 1));
1829 mode[next] = mode_from_align[amount[next]];
1830 temp[next] = gen_reg_rtx (mode[next]);
1831 }
1832
1833 size -= amount[next];
1834 srcp = gen_rtx (MEM,
1835#if 0
1836 MEM_IN_STRUCT_P (src_mem) ? mode[next] : BLKmode,
1837#else
1838 mode[next],
1839#endif
1840 gen_rtx (PLUS, Pmode, src,
1841 gen_rtx (CONST_INT, SImode, offset_ld)));
1842
1843 RTX_UNCHANGING_P (srcp) = RTX_UNCHANGING_P (src_mem);
1844 MEM_VOLATILE_P (srcp) = MEM_VOLATILE_P (src_mem);
1845 MEM_IN_STRUCT_P (srcp) = 1;
1846 emit_insn (gen_rtx (SET, VOIDmode, temp[next], srcp));
1847 offset_ld += amount[next];
1848 active[next] = TRUE;
1849 }
1850
1851 if (active[phase])
1852 {
1853 active[phase] = FALSE;
1854
1855 dstp = gen_rtx (MEM,
1856#if 0
1857 MEM_IN_STRUCT_P (dst_mem) ? mode[phase] : BLKmode,
1858#else
1859 mode[phase],
1860#endif
1861 gen_rtx (PLUS, Pmode, dest,
1862 gen_rtx (CONST_INT, SImode, offset_st)));
1863
1864 RTX_UNCHANGING_P (dstp) = RTX_UNCHANGING_P (dst_mem);
1865 MEM_VOLATILE_P (dstp) = MEM_VOLATILE_P (dst_mem);
1866 MEM_IN_STRUCT_P (dstp) = 1;
1867 emit_insn (gen_rtx (SET, VOIDmode, dstp, temp[phase]));
1868 offset_st += amount[phase];
1869 }
1870 }
1871 while (active[next]);
1872}
1873
1874void
1875mcore_expand_block_move (dst_mem, src_mem, operands)
1876 rtx dst_mem;
1877 rtx src_mem;
1878 rtx * operands;
1879{
1880 int align = INTVAL (operands[3]);
1881 int bytes;
1882
1883 if (GET_CODE (operands[2]) == CONST_INT)
1884 {
1885 bytes = INTVAL (operands[2]);
1886
1887 if (bytes <= 0)
1888 return;
1889 if (align > 4)
1890 align = 4;
1891
1892 /* RBE: bumped 1 and 2 byte align from 1 and 2 to 4 and 8 bytes before
1893 we give up and go to memcpy.. */
1894 if ((align == 4 && (bytes <= 4*4
1895 || ((bytes & 01) == 0 && bytes <= 8*4)
1896 || ((bytes & 03) == 0 && bytes <= 16*4)))
1897 || (align == 2 && bytes <= 4*2)
1898 || (align == 1 && bytes <= 4*1))
1899 {
1900 block_move_sequence (operands[0], dst_mem, operands[1], src_mem,
1901 bytes, align, 0);
1902 return;
1903 }
1904 }
1905
1906 /* If we get here, just use the library routine. */
1907 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "memcpy"), 0, VOIDmode, 3,
1908 operands[0], Pmode, operands[1], Pmode, operands[2],
1909 SImode);
1910}
1911\f
1912
1913/* Code to generate prologue and epilogue sequences. */
1914static int number_of_regs_before_varargs;
1915/* Set by SETUP_INCOMING_VARARGS to indicate to prolog that this is
1916 for a varargs function. */
1917static int current_function_anonymous_args;
1918
1919
1920#define STACK_BYTES (STACK_BOUNDARY/BITS_PER_UNIT)
1921#define STORE_REACH (64) /* Maximum displace of word store + 4. */
1922#define ADDI_REACH (32) /* Maximum addi operand. */
1923
1924struct mcore_frame
1925{
1926 int arg_size; /* stdarg spills (bytes) */
1927 int reg_size; /* non-volatile reg saves (bytes) */
1928 int reg_mask; /* non-volatile reg saves */
1929 int local_size; /* locals */
1930 int outbound_size; /* arg overflow on calls out */
1931 int pad_outbound;
1932 int pad_local;
1933 int pad_reg;
1934
1935 /* describe the steps we'll use to grow it */
1936#define MAX_STACK_GROWS 4 /* gives us some spare space */
1937 int growth[MAX_STACK_GROWS];
1938 int arg_offset;
1939 int reg_offset;
1940 int reg_growth;
1941 int local_growth;
1942};
1943
1944static void
1945layout_mcore_frame (infp)
1946 struct mcore_frame * infp;
1947{
1948 int n;
1949 unsigned int i;
1950 int nbytes;
1951 int regarg;
1952 int localregarg;
1953 int localreg;
1954 int outbounds;
1955 unsigned int growths;
1956 int step;
1957
1958 /* Might have to spill bytes to re-assemble a big argument that
1959 was passed partially in registers and partially on the stack. */
1960 nbytes = current_function_pretend_args_size;
1961
1962 /* Determine how much space for spilled anonymous args (e.g., stdarg). */
1963 if (current_function_anonymous_args)
1964 nbytes += (NPARM_REGS - number_of_regs_before_varargs) * UNITS_PER_WORD;
1965
1966 infp->arg_size = nbytes;
1967
1968 /* How much space to save non-volatile registers we stomp. */
1969 infp->reg_mask = calc_live_regs (& n);
1970 infp->reg_size = n * 4;
1971
1972 /* And the rest of it... locals and space for overflowed outbounds. */
1973 infp->local_size = get_frame_size ();
1974 infp->outbound_size = current_function_outgoing_args_size;
1975
1976 /* Make sure we have a whole number of words for the locals. */
1977 if (infp->local_size % STACK_BYTES)
1978 infp->local_size = (infp->local_size + STACK_BYTES - 1) & ~ (STACK_BYTES -1);
1979
1980 /* Only thing we know we have to pad is the outbound space, since
1981 we've aligned our locals assuming that base of locals is aligned. */
1982 infp->pad_local = 0;
1983 infp->pad_reg = 0;
1984 infp->pad_outbound = 0;
1985 if (infp->outbound_size % STACK_BYTES)
1986 infp->pad_outbound = STACK_BYTES - (infp->outbound_size % STACK_BYTES);
1987
1988 /* Now we see how we want to stage the prologue so that it does
1989 the most appropriate stack growth and register saves to either:
1990 (1) run fast,
1991 (2) reduce instruction space, or
1992 (3) reduce stack space. */
1993 for (i = 0; i < sizeof (infp->growth) / sizeof (infp->growth[0]); i++)
1994 infp->growth[i] = 0;
1995
1996 regarg = infp->reg_size + infp->arg_size;
1997 localregarg = infp->local_size + regarg;
1998 localreg = infp->local_size + infp->reg_size;
1999 outbounds = infp->outbound_size + infp->pad_outbound;
2000 growths = 0;
2001
2002 /* XXX: Consider one where we consider localregarg + outbound too! */
2003
2004 /* Frame of <= 32 bytes and using stm would get <= 2 registers.
2005 use stw's with offsets and buy the frame in one shot. */
2006 if (localregarg <= ADDI_REACH
2007 && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
2008 {
2009 /* Make sure we'll be aligned. */
2010 if (localregarg % STACK_BYTES)
2011 infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
2012
2013 step = localregarg + infp->pad_reg;
2014 infp->reg_offset = infp->local_size;
2015
2016 if (outbounds + step <= ADDI_REACH && !frame_pointer_needed)
2017 {
2018 step += outbounds;
2019 infp->reg_offset += outbounds;
2020 outbounds = 0;
2021 }
2022
2023 infp->arg_offset = step - 4;
2024 infp->growth[growths++] = step;
2025 infp->reg_growth = growths;
2026 infp->local_growth = growths;
2027
2028 /* If we haven't already folded it in... */
2029 if (outbounds)
2030 infp->growth[growths++] = outbounds;
2031
2032 goto finish;
2033 }
2034
2035 /* Frame can't be done with a single subi, but can be done with 2
2036 insns. If the 'stm' is getting <= 2 registers, we use stw's and
2037 shift some of the stack purchase into the first subi, so both are
2038 single instructions. */
2039 if (localregarg <= STORE_REACH
2040 && (infp->local_size > ADDI_REACH)
2041 && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
2042 {
2043 int all;
2044
2045 /* Make sure we'll be aligned; use either pad_reg or pad_local. */
2046 if (localregarg % STACK_BYTES)
2047 infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
2048
2049 all = localregarg + infp->pad_reg + infp->pad_local;
2050 step = ADDI_REACH; /* As much up front as we can. */
2051 if (step > all)
2052 step = all;
2053
2054 /* XXX: Consider whether step will still be aligned; we believe so. */
2055 infp->arg_offset = step - 4;
2056 infp->growth[growths++] = step;
2057 infp->reg_growth = growths;
2058 infp->reg_offset = step - infp->pad_reg - infp->reg_size;
2059 all -= step;
2060
2061 /* Can we fold in any space required for outbounds? */
2062 if (outbounds + all <= ADDI_REACH && !frame_pointer_needed)
2063 {
2064 all += outbounds;
2065 outbounds = 0;
2066 }
2067
2068 /* Get the rest of the locals in place. */
2069 step = all;
2070 infp->growth[growths++] = step;
2071 infp->local_growth = growths;
2072 all -= step;
2073
2074 assert (all == 0);
2075
2076 /* Finish off if we need to do so... */
2077 if (outbounds)
2078 infp->growth[growths++] = outbounds;
2079
2080 goto finish;
2081 }
2082
2083 /* Registers + args is nicely aligned, so we'll buy that in one shot.
2084 Then we buy the rest of the frame in 1 or 2 steps depending on
2085 whether we need a frame pointer. */
2086 if ((regarg % STACK_BYTES) == 0)
2087 {
2088 infp->growth[growths++] = regarg;
2089 infp->reg_growth = growths;
2090 infp->arg_offset = regarg - 4;
2091 infp->reg_offset = 0;
2092
2093 if (infp->local_size % STACK_BYTES)
2094 infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
2095
2096 step = infp->local_size + infp->pad_local;
2097
2098 if (!frame_pointer_needed)
2099 {
2100 step += outbounds;
2101 outbounds = 0;
2102 }
2103
2104 infp->growth[growths++] = step;
2105 infp->local_growth = growths;
2106
2107 /* If there's any left to be done... */
2108 if (outbounds)
2109 infp->growth[growths++] = outbounds;
2110
2111 goto finish;
2112 }
2113
2114 /* XXX: optimizations that we'll want to play with....
2115 * -- regarg is not aligned, but it's a small number of registers;
2116 * use some of localsize so that regarg is aligned and then
2117 * save the registers.
2118 *
2119 */
2120
2121 /* Simple encoding; plods down the stack buying the pieces as it goes.
2122 * -- does not optimize space consumption.
2123 * -- does not attempt to optimize instruction counts.
2124 * -- but it is safe for all alignments.
2125 */
2126 if (regarg % STACK_BYTES != 0)
2127 infp->pad_reg = STACK_BYTES - (regarg % STACK_BYTES);
2128
2129 infp->growth[growths++] = infp->arg_size + infp->reg_size + infp->pad_reg;
2130 infp->reg_growth = growths;
2131 infp->arg_offset = infp->growth[0] - 4;
2132 infp->reg_offset = 0;
2133
2134 if (frame_pointer_needed)
2135 {
2136 if (infp->local_size % STACK_BYTES != 0)
2137 infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
2138
2139 infp->growth[growths++] = infp->local_size + infp->pad_local;
2140 infp->local_growth = growths;
2141
2142 infp->growth[growths++] = outbounds;
2143 }
2144 else
2145 {
2146 if ((infp->local_size + outbounds) % STACK_BYTES != 0)
2147 infp->pad_local = STACK_BYTES - ((infp->local_size + outbounds) % STACK_BYTES);
2148
2149 infp->growth[growths++] = infp->local_size + infp->pad_local + outbounds;
2150 infp->local_growth = growths;
2151 }
2152
2153 /* Anything else that we've forgotten?, plus a few consistency checks. */
2154 finish:
2155 assert (infp->reg_offset >= 0);
2156 assert (growths <= MAX_STACK_GROWS);
2157
2158 for (i = 0; i < growths; i++)
2159 {
2160 if (infp->growth[i] % STACK_BYTES)
2161 {
2162 fprintf (stderr,"stack growth of %d is not %d aligned\n",
2163 infp->growth[i], STACK_BYTES);
2164 abort ();
2165 }
2166 }
2167}
2168
2169/* Define the offset between two registers, one to be eliminated, and
2170 the other its replacement, at the start of a routine. */
2171int
2172mcore_initial_elimination_offset (from, to)
2173 int from;
2174 int to;
2175{
2176 int above_frame;
2177 int below_frame;
2178 struct mcore_frame fi;
2179
2180 layout_mcore_frame (& fi);
2181
2182 /* fp to ap */
2183 above_frame = fi.local_size + fi.pad_local + fi.reg_size + fi.pad_reg;
2184 /* sp to fp */
2185 below_frame = fi.outbound_size + fi.pad_outbound;
2186
2187 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
2188 return above_frame;
2189
2190 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2191 return above_frame + below_frame;
2192
2193 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2194 return below_frame;
2195
2196 abort ();
2197
2198 return 0;
2199}
2200
2201/* Keep track of some information about varargs for the prolog. */
2202void
2203mcore_setup_incoming_varargs (args_so_far, mode, type, ptr_pretend_size)
2204 CUMULATIVE_ARGS args_so_far;
2205 enum machine_mode mode;
2206 tree type;
2207 int * ptr_pretend_size ATTRIBUTE_UNUSED;
2208{
2209 current_function_anonymous_args = 1;
2210
2211 /* We need to know how many argument registers are used before
2212 the varargs start, so that we can push the remaining argument
2213 registers during the prologue. */
2214 number_of_regs_before_varargs = args_so_far + mcore_num_arg_regs (mode, type);
2215
2216 /* There is a bug somwehere in the arg handling code.
2217 Until I can find it this workaround always pushes the
2218 last named argument onto the stack. */
2219 number_of_regs_before_varargs = args_so_far;
2220
2221 /* The last named argument may be split between argument registers
2222 and the stack. Allow for this here. */
2223 if (number_of_regs_before_varargs > NPARM_REGS)
2224 number_of_regs_before_varargs = NPARM_REGS;
2225}
2226
2227void
2228mcore_expand_prolog ()
2229{
2230 struct mcore_frame fi;
2231 int space_allocated = 0;
2232 int growth = 0;
2233
2234 /* Find out what we're doing. */
2235 layout_mcore_frame (&fi);
2236
2237 space_allocated = fi.arg_size + fi.reg_size + fi.local_size +
2238 fi.outbound_size + fi.pad_outbound + fi.pad_local + fi.pad_reg;
2239
2240 if (TARGET_CG_DATA)
2241 {
2242 /* Emit a symbol for this routine's frame size. */
2243 rtx x;
2244 int len;
2245
2246 x = DECL_RTL (current_function_decl);
2247
2248 if (GET_CODE (x) != MEM)
2249 abort ();
2250
2251 x = XEXP (x, 0);
2252
2253 if (GET_CODE (x) != SYMBOL_REF)
2254 abort ();
2255
2256 if (mcore_current_function_name)
2257 free (mcore_current_function_name);
2258
2259 len = strlen (XSTR (x, 0)) + 1;
2260 mcore_current_function_name = (char *) malloc (len);
2261
2262 memcpy (mcore_current_function_name, XSTR (x, 0), len);
2263
2264 ASM_OUTPUT_CG_NODE (asm_out_file, mcore_current_function_name, space_allocated);
2265
2266 if (current_function_calls_alloca)
2267 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, "alloca", 1);
2268
2269 /* 970425: RBE:
2270 We're looking at how the 8byte alignment affects stack layout
2271 and where we had to pad things. This emits information we can
2272 extract which tells us about frame sizes and the like. */
2273 fprintf (asm_out_file,
2274 "\t.equ\t__$frame$info$_%s_$_%d_%d_x%x_%d_%d_%d,0\n",
2275 mcore_current_function_name,
2276 fi.arg_size, fi.reg_size, fi.reg_mask,
2277 fi.local_size, fi.outbound_size,
2278 frame_pointer_needed);
2279 }
2280
2281 if (mcore_naked_function_p ())
2282 return;
2283
2284 /* Handle stdarg+regsaves in one shot: can't be more than 64 bytes. */
2285 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2286
2287 /* If we have a parameter passed partially in regs and partially in memory,
2288 the registers will have been stored to memory already in function.c. So
2289 we only need to do something here for varargs functions. */
2290 if (fi.arg_size != 0 && current_function_pretend_args_size == 0)
2291 {
2292 int offset;
2293 int rn = FIRST_PARM_REG + NPARM_REGS - 1;
2294 int remaining = fi.arg_size;
2295
2296 for (offset = fi.arg_offset; remaining >= 4; offset -= 4, rn--, remaining -= 4)
2297 {
2298 emit_insn (gen_movsi
2299 (gen_rtx (MEM, SImode,
2300 plus_constant (stack_pointer_rtx, offset)),
2301 gen_rtx (REG, SImode, rn)));
2302 }
2303 }
2304
2305 /* Do we need another stack adjustment before we do the register saves? */
2306 if (growth < fi.reg_growth)
2307 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2308
2309 if (fi.reg_size != 0)
2310 {
2311 int i;
2312 int offs = fi.reg_offset;
2313
2314 for (i = 15; i >= 0; i--)
2315 {
2316 if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2317 {
2318 int first_reg = 15;
2319
2320 while (fi.reg_mask & (1 << first_reg))
2321 first_reg--;
2322 first_reg++;
2323
2324 emit_insn (gen_store_multiple (gen_rtx (MEM, SImode, stack_pointer_rtx),
2325 gen_rtx (REG, SImode, first_reg),
2326 GEN_INT (16 - first_reg)));
2327
2328 i -= (15 - first_reg);
2329 offs += (16 - first_reg) * 4;
2330 }
2331 else if (fi.reg_mask & (1 << i))
2332 {
2333 emit_insn (gen_movsi
2334 (gen_rtx (MEM, SImode,
2335 plus_constant (stack_pointer_rtx, offs)),
2336 gen_rtx (REG, SImode, i)));
2337 offs += 4;
2338 }
2339 }
2340 }
2341
2342 /* Figure the locals + outbounds. */
2343 if (frame_pointer_needed)
2344 {
2345 /* If we haven't already purchased to 'fp'. */
2346 if (growth < fi.local_growth)
2347 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2348
2349 emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
2350
2351 /* ... and then go any remaining distance for outbounds, etc. */
2352 if (fi.growth[growth])
2353 output_stack_adjust (-1, fi.growth[growth++]);
2354 }
2355 else
2356 {
2357 if (growth < fi.local_growth)
2358 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2359 if (fi.growth[growth])
2360 output_stack_adjust (-1, fi.growth[growth++]);
2361 }
2362}
2363
2364void
2365mcore_expand_epilog ()
2366{
2367 struct mcore_frame fi;
2368 int i;
2369 int offs;
2370 int growth = MAX_STACK_GROWS - 1 ;
2371
2372 /* Find out what we're doing. */
2373 layout_mcore_frame(&fi);
2374
2375 if (mcore_naked_function_p ())
2376 return;
2377
2378 /* If we had a frame pointer, restore the sp from that. */
2379 if (frame_pointer_needed)
2380 {
2381 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
2382 growth = fi.local_growth - 1;
2383 }
2384 else
2385 {
2386 /* XXX: while loop should accumulate and do a single sell. */
2387 while (growth >= fi.local_growth)
2388 {
2389 if (fi.growth[growth] != 0)
2390 output_stack_adjust (1, fi.growth[growth]);
2391 growth--;
2392 }
2393 }
2394
2395 /* Make sure we've shrunk stack back to the point where the registers
2396 were laid down. This is typically 0/1 iterations. Then pull the
2397 register save information back off the stack. */
2398
2399 while (growth >= fi.reg_growth)
2400 output_stack_adjust ( 1, fi.growth[growth--]);
2401
2402 offs = fi.reg_offset;
2403
2404 for (i = 15; i >= 0; i--)
2405 {
2406 if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2407 {
2408 int first_reg;
2409
2410 /* Find the starting register. */
2411 first_reg = 15;
2412
2413 while (fi.reg_mask & (1 << first_reg))
2414 first_reg--;
2415
2416 first_reg++;
2417
2418 emit_insn (gen_load_multiple (gen_rtx (REG, SImode, first_reg),
2419 gen_rtx (MEM, SImode, stack_pointer_rtx),
2420 GEN_INT (16 - first_reg)));
2421
2422 i -= (15 - first_reg);
2423 offs += (16 - first_reg) * 4;
2424 }
2425 else if (fi.reg_mask & (1 << i))
2426 {
2427 emit_insn (gen_movsi
2428 (gen_rtx (REG, SImode, i),
2429 gen_rtx (MEM, SImode,
2430 plus_constant (stack_pointer_rtx, offs))));
2431 offs += 4;
2432 }
2433 }
2434
2435 /* Give back anything else. */
2436 /* XXX: Should accumuate total and then give it back... */
2437 while (growth >= 0)
2438 output_stack_adjust ( 1, fi.growth[growth--]);
2439}
2440\f
2441/* This code is borrowed from the SH port. */
2442
2443/* The MCORE cannot load a large constant into a register, constants have to
2444 come from a pc relative load. The reference of a pc relative load
2445 instruction must be less than 1k infront of the instruction. This
2446 means that we often have to dump a constant inside a function, and
2447 generate code to branch around it.
2448
2449 It is important to minimize this, since the branches will slow things
2450 down and make things bigger.
2451
2452 Worst case code looks like:
2453
2454 lrw L1,r0
2455 br L2
2456 align
2457 L1: .long value
2458 L2:
2459 ..
2460
2461 lrw L3,r0
2462 br L4
2463 align
2464 L3: .long value
2465 L4:
2466 ..
2467
2468 We fix this by performing a scan before scheduling, which notices which
2469 instructions need to have their operands fetched from the constant table
2470 and builds the table.
2471
2472 The algorithm is:
2473
2474 scan, find an instruction which needs a pcrel move. Look forward, find the
2475 last barrier which is within MAX_COUNT bytes of the requirement.
2476 If there isn't one, make one. Process all the instructions between
2477 the find and the barrier.
2478
2479 In the above example, we can tell that L3 is within 1k of L1, so
2480 the first move can be shrunk from the 2 insn+constant sequence into
2481 just 1 insn, and the constant moved to L3 to make:
2482
2483 lrw L1,r0
2484 ..
2485 lrw L3,r0
2486 bra L4
2487 align
2488 L3:.long value
2489 L4:.long value
2490
2491 Then the second move becomes the target for the shortening process. */
2492
2493typedef struct
2494{
2495 rtx value; /* Value in table. */
2496 rtx label; /* Label of value. */
2497} pool_node;
2498
2499/* The maximum number of constants that can fit into one pool, since
2500 the pc relative range is 0...1020 bytes and constants are at least 4
2501 bytes long. We subtact 4 from the range to allow for the case where
2502 we need to add a branch/align before the constant pool. */
2503
2504#define MAX_COUNT 1016
2505#define MAX_POOL_SIZE (MAX_COUNT/4)
2506static pool_node pool_vector[MAX_POOL_SIZE];
2507static int pool_size;
2508
2509/* Dump out any constants accumulated in the final pass. These
2510 will only be labels. */
2511char *
2512mcore_output_jump_label_table ()
2513{
2514 int i;
2515
2516 if (pool_size)
2517 {
2518 fprintf (asm_out_file, "\t.align 2\n");
2519
2520 for (i = 0; i < pool_size; i++)
2521 {
2522 pool_node * p = pool_vector + i;
2523
2524 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (p->label));
2525
2526 output_asm_insn (".long %0", &p->value);
2527 }
2528
2529 pool_size = 0;
2530 }
2531
2532 return "";
2533}
2534
2535#if 0 /* XXX temporarily suppressed until I have time to look at what this code does. */
2536
2537/* We need these below. They use information stored in tables to figure out
2538 what values are in what registers, etc. This is okay, since these tables
2539 are valid at the time mcore_dependent_simplify_rtx() is invoked. Don't
2540 use them anywhere else. BRC */
2541
2542extern unsigned HOST_WIDE_INT nonzero_bits PARAMS ((rtx, enum machine_mode));
2543extern int num_sign_bit_copies PARAMS ((Rtx, enum machine_mode));
2544
2545/* Do machine dependent simplifications: see simplify_rtx() in combine.c.
2546 GENERAL_SIMPLIFY controls whether general machine independent
2547 simplifications should be tried after machine dependent ones. Thus,
2548 we can filter out certain simplifications and keep the simplify_rtx()
2549 from changing things that we just simplified in a machine dependent
2550 fashion. This is experimental. BRC */
2551rtx
2552mcore_dependent_simplify_rtx (x, int_op0_mode, last, in_dest, general_simplify)
2553 rtx x;
2554 int int_op0_mode;
2555 int last;
2556 int in_dest;
2557 int * general_simplify;
2558{
2559 enum machine_mode mode = GET_MODE (x);
2560 enum rtx_code code = GET_CODE (x);
2561
2562 /* always simplify unless explicitly asked not to */
2563
2564 * general_simplify = 1;
2565
2566 if (code == IF_THEN_ELSE)
2567 {
2568 int i;
2569 rtx cond = XEXP(x, 0);
2570 rtx true = XEXP(x, 1);
2571 rtx false = XEXP(x, 2);
2572 enum rtx_code true_code = GET_CODE (cond);
2573
2574 /* On the mcore, when doing -mcmov-one, we don't want to simplify:
2575
2576 (if_then_else (ne A 0) C1 0)
2577
2578 if it would be turned into a shift by simplify_if_then_else().
2579 instead, leave it alone so that it will collapse into a conditional
2580 move. besides, at least for the mcore, doing this simplification does
2581 not typically help. see combine.c, line 4217. BRC */
2582
2583 if (true_code == NE && XEXP (cond, 1) == const0_rtx
2584 && false == const0_rtx && GET_CODE (true) == CONST_INT
2585 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
2586 && (i = exact_log2 (INTVAL (true))) >= 0)
2587 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
2588 == GET_MODE_BITSIZE (mode))
2589 && (i = exact_log2 (- INTVAL (true))) >= 0)))
2590 {
2591 *general_simplify = 0;
2592 return x;
2593 }
2594 }
2595
2596 return x;
2597}
2598#endif
2599
2600typedef enum
2601{
2602 COND_NO,
2603 COND_MOV_INSN,
2604 COND_CLR_INSN,
2605 COND_INC_INSN,
2606 COND_DEC_INSN,
2607 COND_BRANCH_INSN
2608}
2609cond_type;
2610
2611/* Check whether insn is a candidate for a conditional. */
2612static cond_type
2613is_cond_candidate (insn)
2614 rtx insn;
2615{
2616 /* The only things we conditionalize are those that can be directly
2617 changed into a conditional. Only bother with SImode items. If
2618 we wanted to be a little more aggressive, we could also do other
2619 modes such as DImode with reg-reg move or load 0. */
2620 if (GET_CODE (insn) == INSN)
2621 {
2622 rtx pat = PATTERN (insn);
2623 rtx src, dst;
2624
2625 if (GET_CODE (pat) != SET)
2626 return COND_NO;
2627
2628 dst = XEXP (pat, 0);
2629
2630 if ((GET_CODE (dst) != REG &&
2631 GET_CODE (dst) != SUBREG) ||
2632 GET_MODE (dst) != SImode)
2633 return COND_NO;
2634
2635 src = XEXP (pat, 1);
2636
2637 if ((GET_CODE (src) == REG ||
2638 (GET_CODE (src) == SUBREG &&
2639 GET_CODE (SUBREG_REG (src)) == REG)) &&
2640 GET_MODE (src) == SImode)
2641 return COND_MOV_INSN;
2642 else if (GET_CODE (src) == CONST_INT &&
2643 INTVAL (src) == 0)
2644 return COND_CLR_INSN;
2645 else if (GET_CODE (src) == PLUS &&
2646 (GET_CODE (XEXP (src, 0)) == REG ||
2647 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2648 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2649 GET_MODE (XEXP (src, 0)) == SImode &&
2650 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2651 INTVAL (XEXP (src, 1)) == 1)
2652 return COND_INC_INSN;
2653 else if (((GET_CODE (src) == MINUS &&
2654 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2655 INTVAL( XEXP (src, 1)) == 1) ||
2656 (GET_CODE (src) == PLUS &&
2657 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2658 INTVAL (XEXP (src, 1)) == -1)) &&
2659 (GET_CODE (XEXP (src, 0)) == REG ||
2660 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2661 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2662 GET_MODE (XEXP (src, 0)) == SImode)
2663 return COND_DEC_INSN;
2664
2665 /* some insns that we don't bother with:
2666 (set (rx:DI) (ry:DI))
2667 (set (rx:DI) (const_int 0))
2668 */
2669
2670 }
2671 else if (GET_CODE (insn) == JUMP_INSN &&
2672 GET_CODE (PATTERN (insn)) == SET &&
2673 GET_CODE (XEXP (PATTERN (insn), 1)) == LABEL_REF)
2674 return COND_BRANCH_INSN;
2675
2676 return COND_NO;
2677}
2678
2679/* Emit a conditional version of insn and replace the old insn with the
2680 new one. Return the new insn if emitted. */
2681static rtx
2682emit_new_cond_insn (insn, cond)
2683 rtx insn;
2684 int cond;
2685{
2686 rtx c_insn = 0;
2687 rtx pat, dst, src;
2688 cond_type num;
2689
2690 if ((num = is_cond_candidate (insn)) == COND_NO)
2691 return NULL;
2692
2693 pat = PATTERN (insn);
2694
2695 if (GET_CODE (insn) == INSN)
2696 {
2697 dst = SET_DEST (pat);
2698 src = SET_SRC (pat);
2699 }
2700 else
2701 dst = JUMP_LABEL (insn);
2702
2703 switch (num)
2704 {
2705 case COND_MOV_INSN:
2706 case COND_CLR_INSN:
2707 if (cond)
2708 c_insn = gen_movt0 (dst, src, dst);
2709 else
2710 c_insn = gen_movt0 (dst, dst, src);
2711 break;
2712
2713 case COND_INC_INSN:
2714 if (cond)
2715 c_insn = gen_incscc (dst, dst);
2716 else
2717 c_insn = gen_incscc_false (dst, dst);
2718 break;
2719
2720 case COND_DEC_INSN:
2721 if (cond)
2722 c_insn = gen_decscc (dst, dst);
2723 else
2724 c_insn = gen_decscc_false (dst, dst);
2725 break;
2726
2727 case COND_BRANCH_INSN:
2728 if (cond)
2729 c_insn = gen_branch_true (dst);
2730 else
2731 c_insn = gen_branch_false (dst);
2732 break;
2733
2734 default:
2735 return NULL;
2736 }
2737
2738 /* Only copy the notes if they exist. */
2739 if (rtx_length [GET_CODE (c_insn)] >= 7 && rtx_length [GET_CODE (insn)] >= 7)
2740 {
2741 /* We really don't need to bother with the notes and links at this
2742 point, but go ahead and save the notes. This will help is_dead()
2743 when applying peepholes (links don't matter since they are not
2744 used any more beyond this point for the mcore). */
2745 REG_NOTES (c_insn) = REG_NOTES (insn);
2746 }
2747
2748 if (num == COND_BRANCH_INSN)
2749 {
2750 /* For jumps, we need to be a little bit careful and emit the new jump
2751 before the old one and to update the use count for the target label.
2752 This way, the barrier following the old (uncond) jump will get
2753 deleted, but the label won't. */
2754 c_insn = emit_jump_insn_before (c_insn, insn);
2755
2756 ++ LABEL_NUSES (dst);
2757
2758 JUMP_LABEL (c_insn) = dst;
2759 }
2760 else
2761 c_insn = emit_insn_after (c_insn, insn);
2762
2763 delete_insn (insn);
2764
2765 return c_insn;
2766}
2767
2768/* Attempt to change a basic block into a series of conditional insns. This
2769 works by taking the branch at the end of the 1st block and scanning for the
2770 end of the 2nd block. If all instructions in the 2nd block have cond.
2771 versions and the label at the start of block 3 is the same as the target
2772 from the branch at block 1, then conditionalize all insn in block 2 using
2773 the inverse condition of the branch at block 1. (Note I'm bending the
2774 definition of basic block here.)
2775
2776 e.g., change:
2777
2778 bt L2 <-- end of block 1 (delete)
2779 mov r7,r8
2780 addu r7,1
2781 br L3 <-- end of block 2
2782
2783 L2: ... <-- start of block 3 (NUSES==1)
2784 L3: ...
2785
2786 to:
2787
2788 movf r7,r8
2789 incf r7
2790 bf L3
2791
2792 L3: ...
2793
2794 we can delete the L2 label if NUSES==1 and re-apply the optimization
2795 starting at the last instruction of block 2. This may allow an entire
2796 if-then-else statement to be conditionalized. BRC */
2797static rtx
2798conditionalize_block (first)
2799 rtx first;
2800{
2801 rtx insn;
2802 rtx br_pat;
2803 rtx end_blk_1_br = 0;
2804 rtx end_blk_2_insn = 0;
2805 rtx start_blk_3_lab = 0;
2806 int cond;
2807 int br_lab_num;
2808 int blk_size = 0;
2809
2810
2811 /* Check that the first insn is a candidate conditional jump. This is
2812 the one that we'll eliminate. If not, advance to the next insn to
2813 try. */
2814 if (GET_CODE (first) != JUMP_INSN ||
2815 GET_CODE (PATTERN (first)) != SET ||
2816 GET_CODE (XEXP (PATTERN (first), 1)) != IF_THEN_ELSE)
2817 return NEXT_INSN (first);
2818
2819 /* Extract some information we need. */
2820 end_blk_1_br = first;
2821 br_pat = PATTERN (end_blk_1_br);
2822
2823 /* Complement the condition since we use the reverse cond. for the insns. */
2824 cond = (GET_CODE (XEXP (XEXP (br_pat, 1), 0)) == EQ);
2825
2826 /* Determine what kind of branch we have. */
2827 if (GET_CODE (XEXP (XEXP (br_pat, 1), 1)) == LABEL_REF)
2828 {
2829 /* A normal branch, so extract label out of first arm. */
2830 br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 1), 0));
2831 }
2832 else
2833 {
2834 /* An inverse branch, so extract the label out of the 2nd arm
2835 and complement the condition. */
2836 cond = (cond == 0);
2837 br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 2), 0));
2838 }
2839
2840 /* Scan forward for the start of block 2: it must start with a
2841 label and that label must be the same as the branch target
2842 label from block 1. We don't care about whether block 2 actually
2843 ends with a branch or a label (an uncond. branch is
2844 conditionalizable). */
2845 for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
2846 {
2847 enum rtx_code code;
2848
2849 code = GET_CODE (insn);
2850
2851 /* Look for the label at the start of block 3. */
2852 if (code == CODE_LABEL && CODE_LABEL_NUMBER (insn) == br_lab_num)
2853 break;
2854
2855 /* Skip barriers, notes, and conditionalizable insns. If the
2856 insn is not conditionalizable or makes this optimization fail,
2857 just return the next insn so we can start over from that point. */
2858 if (code != BARRIER && code != NOTE && !is_cond_candidate (insn))
2859 return NEXT_INSN (insn);
2860
2861 /* Remember the last real insn before the label (ie end of block 2). */
2862 if (code == JUMP_INSN || code == INSN)
2863 {
2864 blk_size ++;
2865 end_blk_2_insn = insn;
2866 }
2867 }
2868
2869 if (!insn)
2870 return insn;
2871
2872 /* It is possible for this optimization to slow performance if the blocks
2873 are long. This really depends upon whether the branch is likely taken
2874 or not. If the branch is taken, we slow performance in many cases. But,
2875 if the branch is not taken, we always help performance (for a single
2876 block, but for a double block (i.e. when the optimization is re-applied)
2877 this is not true since the 'right thing' depends on the overall length of
2878 the collapsed block). As a compromise, don't apply this optimization on
2879 blocks larger than size 2 (unlikely for the mcore) when speed is important.
2880 the best threshold depends on the latencies of the instructions (i.e.,
2881 the branch penalty). */
2882 if (optimize > 1 && blk_size > 2)
2883 return insn;
2884
2885 /* At this point, we've found the start of block 3 and we know that
2886 it is the destination of the branch from block 1. Also, all
2887 instructions in the block 2 are conditionalizable. So, apply the
2888 conditionalization and delete the branch. */
2889 start_blk_3_lab = insn;
2890
2891 for (insn = NEXT_INSN (end_blk_1_br); insn != start_blk_3_lab;
2892 insn = NEXT_INSN (insn))
2893 {
2894 rtx newinsn;
2895
2896 if (INSN_DELETED_P (insn))
2897 continue;
2898
2899 /* Try to form a conditional variant of the instruction and emit it. */
2900 if ((newinsn = emit_new_cond_insn (insn, cond)))
2901 {
2902 if (end_blk_2_insn == insn)
2903 end_blk_2_insn = newinsn;
2904
2905 insn = newinsn;
2906 }
2907 }
2908
2909 /* Note whether we will delete the label starting blk 3 when the jump
2910 gets deleted. If so, we want to re-apply this optimization at the
2911 last real instruction right before the label. */
2912 if (LABEL_NUSES (start_blk_3_lab) == 1)
2913 {
2914 start_blk_3_lab = 0;
2915 }
2916
2917 /* ??? we probably should redistribute the death notes for this insn, esp.
2918 the death of cc, but it doesn't really matter this late in the game.
2919 The peepholes all use is_dead() which will find the correct death
2920 regardless of whether there is a note. */
2921 delete_insn (end_blk_1_br);
2922
2923 if (! start_blk_3_lab)
2924 return end_blk_2_insn;
2925
2926 /* Return the insn right after the label at the start of block 3. */
2927 return NEXT_INSN (start_blk_3_lab);
2928}
2929
2930/* Apply the conditionalization of blocks optimization. This is the
2931 outer loop that traverses through the insns scanning for a branch
2932 that signifies an opportunity to apply the optimization. Note that
2933 this optimization is applied late. If we could apply it earlier,
2934 say before cse 2, it may expose more optimization opportunities.
2935 but, the pay back probably isn't really worth the effort (we'd have
2936 to update all reg/flow/notes/links/etc to make it work - and stick it
2937 in before cse 2). */
2938static void
2939conditionalize_optimization (first)
2940 rtx first;
2941{
2942 rtx insn;
2943
2944 for (insn = first; insn; insn = conditionalize_block (insn))
2945 continue;
2946}
2947
2948static int saved_warn_return_type = -1;
2949static int saved_warn_return_type_count = 0;
2950
2951/* This function is called from toplev.c before reorg. */
2952void
2953mcore_dependent_reorg (first)
2954 rtx first;
2955{
2956 /* Reset this variable. */
2957 current_function_anonymous_args = 0;
2958
2959 /* Restore the warn_return_type if it has been altered */
2960 if (saved_warn_return_type != -1)
2961 {
2962 /* Only restore the value if we have reached another function.
2963 The test of warn_return_type occurs in final_function () in
2964 c-decl.c a long time after the code for the function is generated,
2965 so we need a counter to tell us when we have finished parsing that
2966 function and can restore the flag. */
2967 if (--saved_warn_return_type_count == 0)
2968 {
2969 warn_return_type = saved_warn_return_type;
2970 saved_warn_return_type = -1;
2971 }
2972 }
2973
2974 if (optimize == 0)
2975 return;
2976
2977 /* Conditionalize blocks where we can. */
2978 conditionalize_optimization (first);
2979
2980 /* Literal pool generation is now pushed off until the assembler. */
2981}
2982
2983\f
2984/* Return the reg_class to use when reloading the rtx X into the class
2985 CLASS. */
2986
2987/* If the input is (PLUS REG CONSTANT) representing a stack slot address,
2988 then we want to restrict the class to LRW_REGS since that ensures that
2989 will be able to safely load the constant.
2990
2991 If the input is a constant that should be loaded with mvir1, then use
2992 ONLYR1_REGS.
2993
2994 ??? We don't handle the case where we have (PLUS REG CONSTANT) and
2995 the constant should be loaded with mvir1, because that can lead to cases
2996 where an instruction needs two ONLYR1_REGS reloads. */
2997enum reg_class
2998mcore_reload_class (x, class)
2999 rtx x;
3000 enum reg_class class;
3001{
3002 enum reg_class new_class;
3003
3004 if (class == GENERAL_REGS && CONSTANT_P (x)
3005 && (GET_CODE (x) != CONST_INT
3006 || ( ! CONST_OK_FOR_I (INTVAL (x))
3007 && ! CONST_OK_FOR_M (INTVAL (x))
3008 && ! CONST_OK_FOR_N (INTVAL (x)))))
3009 new_class = LRW_REGS;
3010 else
3011 new_class = class;
3012
3013 return new_class;
3014}
3015
3016/* Tell me if a pair of reg/subreg rtx's actually refer to the same
3017 register. Note that the current version doesn't worry about whether
3018 they are the same mode or note (e.g., a QImode in r2 matches an HImode
3019 in r2 matches an SImode in r2. Might think in the future about whether
3020 we want to be able to say something about modes. */
3021int
3022mcore_is_same_reg (x, y)
3023 rtx x;
3024 rtx y;
3025{
3026 /* Strip any and all of the subreg wrappers. */
3027 while (GET_CODE (x) == SUBREG)
3028 x = SUBREG_REG (x);
3029
3030 while (GET_CODE (y) == SUBREG)
3031 y = SUBREG_REG (y);
3032
3033 if (GET_CODE(x) == REG && GET_CODE(y) == REG && REGNO(x) == REGNO(y))
3034 return 1;
3035
3036 return 0;
3037}
3038
3039/* Called to register all of our global variables with the garbage
3040 collector. */
3041static void
3042mcore_add_gc_roots ()
3043{
3044 ggc_add_rtx_root (&arch_compare_op0, 1);
3045 ggc_add_rtx_root (&arch_compare_op1, 1);
3046}
3047
3048void
3049mcore_override_options ()
3050{
3051 if (mcore_stack_increment_string)
3052 {
3053 mcore_stack_increment = atoi (mcore_stack_increment_string);
3054
3055 if (mcore_stack_increment < 0
3056 || (mcore_stack_increment == 0
3057 && (mcore_stack_increment_string[0] != '0'
3058 || mcore_stack_increment_string[1] != 0)))
3059 error ("Invalid option `-mstack-increment=%s'",
3060 mcore_stack_increment_string);
3061 }
3062
3063 /* Only the m340 supports little endian code. */
3064 if (TARGET_LITTLE_END && ! TARGET_M340)
3065 target_flags |= M340_BIT;
3066
3067 mcore_add_gc_roots ();
3068}
3069\f
3070int
3071mcore_must_pass_on_stack (mode, type)
3072 enum machine_mode mode ATTRIBUTE_UNUSED;
3073 tree type;
3074{
3075 if (type == NULL)
3076 return 0;
3077
3078 /* If the argugment can have its address taken, it must
3079 be placed on the stack. */
3080 if (TREE_ADDRESSABLE (type))
3081 return 1;
3082
3083 return 0;
3084}
3085
3086/* Compute the number of word sized registers needed to
3087 hold a function argument of mode MODE and type TYPE. */
3088int
3089mcore_num_arg_regs (mode, type)
3090 enum machine_mode mode;
3091 tree type;
3092{
3093 int size;
3094
3095 if (MUST_PASS_IN_STACK (mode, type))
3096 return 0;
3097
3098 if (type && mode == BLKmode)
3099 size = int_size_in_bytes (type);
3100 else
3101 size = GET_MODE_SIZE (mode);
3102
3103 return ROUND_ADVANCE (size);
3104}
3105
3106static rtx
3107handle_structs_in_regs (mode, type, reg)
3108 enum machine_mode mode;
3109 tree type;
3110 int reg;
3111{
3112 int size;
3113
3114 /* The MCore ABI defines that a structure whoes size is not a whole multiple
3115 of bytes is passed packed into registers (or spilled onto the stack if
3116 not enough registers are available) with the last few bytes of the
3117 structure being packed, left-justified, into the last register/stack slot.
3118 GCC handles this correctly if the last word is in a stack slot, but we
3119 have to generate a special, PARALLEL RTX if the last word is in an
3120 argument register. */
3121 if (type
3122 && TYPE_MODE (type) == BLKmode
3123 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3124 && (size = int_size_in_bytes (type)) > UNITS_PER_WORD
3125 && (size % UNITS_PER_WORD != 0)
3126 && (reg + mcore_num_arg_regs (mode, type) <= (FIRST_PARM_REG + NPARM_REGS)))
3127 {
3128 rtx arg_regs [NPARM_REGS];
3129 int nregs;
3130 rtx result;
3131 rtvec rtvec;
3132
3133 for (nregs = 0; size > 0; size -= UNITS_PER_WORD)
3134 {
3135 arg_regs [nregs] =
3136 gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, reg ++),
3137 GEN_INT (nregs * UNITS_PER_WORD));
3138 nregs ++;
3139 }
3140
3141 /* We assume here that NPARM_REGS == 6. The assert checks this. */
3142 assert (sizeof (arg_regs) / sizeof (arg_regs[0]) == 6);
3143 rtvec = gen_rtvec (nregs, arg_regs[0], arg_regs[1], arg_regs[2],
3144 arg_regs[3], arg_regs[4], arg_regs[5]);
3145
3146 result = gen_rtx_PARALLEL (mode, rtvec);
3147 return result;
3148 }
3149
3150 return gen_rtx_REG (mode, reg);
3151}
3152
3153rtx
3154mcore_function_value (valtype, func)
3155 tree valtype;
3156 tree func ATTRIBUTE_UNUSED;
3157{
3158 enum machine_mode mode;
3159 int unsigned_p;
3160
3161 mode = TYPE_MODE (valtype);
3162
3163 PROMOTE_MODE (mode, unsigned_p, NULL);
3164
3165 return handle_structs_in_regs (mode, valtype, FIRST_RET_REG);
3166}
3167
3168/* Define where to put the arguments to a function.
3169 Value is zero to push the argument on the stack,
3170 or a hard register in which to store the argument.
3171
3172 MODE is the argument's machine mode.
3173 TYPE is the data type of the argument (as a tree).
3174 This is null for libcalls where that information may
3175 not be available.
3176 CUM is a variable of type CUMULATIVE_ARGS which gives info about
3177 the preceding args and about the function being called.
3178 NAMED is nonzero if this argument is a named parameter
3179 (otherwise it is an extra parameter matching an ellipsis).
3180
3181 On MCore the first args are normally in registers
3182 and the rest are pushed. Any arg that starts within the first
3183 NPARM_REGS words is at least partially passed in a register unless
3184 its data type forbids. */
3185rtx
3186mcore_function_arg (cum, mode, type, named)
3187 CUMULATIVE_ARGS cum;
3188 enum machine_mode mode;
3189 tree type;
3190 int named;
3191{
3192 int arg_reg;
3193
3194 if (! named)
3195 return 0;
3196
3197 if (MUST_PASS_IN_STACK (mode, type))
3198 return 0;
3199
3200 arg_reg = ROUND_REG (cum, mode);
3201
3202 if (arg_reg < NPARM_REGS)
3203 return handle_structs_in_regs (mode, type, FIRST_PARM_REG + arg_reg);
3204
3205 return 0;
3206}
3207
3208/* Implements the FUNCTION_ARG_PARTIAL_NREGS macro.
3209 Returns the number of argument registers required to hold *part* of
3210 a parameter of machine mode MODE and type TYPE (which may be NULL if
3211 the type is not known). If the argument fits entirly in the argument
3212 registers, or entirely on the stack, then 0 is returned. CUM is the
3213 number of argument registers already used by earlier parameters to
3214 the function. */
3215int
3216mcore_function_arg_partial_nregs (cum, mode, type, named)
3217 CUMULATIVE_ARGS cum;
3218 enum machine_mode mode;
3219 tree type;
3220 int named;
3221{
3222 int reg = ROUND_REG (cum, mode);
3223
3224 if (named == 0)
3225 return 0;
3226
3227 if (MUST_PASS_IN_STACK (mode, type))
3228 return 0;
3229
3230 /* REG is not the *hardware* register number of the register that holds
3231 the argument, it is the *argument* register number. So for example,
3232 the first argument to a function goes in argument register 0, which
3233 translates (for the MCore) into hardware register 2. The second
3234 argument goes into argument register 1, which translates into hardware
3235 register 3, and so on. NPARM_REGS is the number of argument registers
3236 supported by the target, not the maximum hardware register number of
3237 the target. */
3238 if (reg >= NPARM_REGS)
3239 return 0;
3240
3241 /* If the argument fits entirely in registers, return 0. */
3242 if (reg + mcore_num_arg_regs (mode, type) <= NPARM_REGS)
3243 return 0;
3244
3245 /* The argument overflows the number of available argument registers.
3246 Compute how many argument registers have not yet been assigned to
3247 hold an argument. */
3248 reg = NPARM_REGS - reg;
3249
3250 /* Return partially in registers and partially on the stack. */
3251 return reg;
3252}
3253\f
3254/* Return non-zero if SYMBOL is marked as being dllexport'd. */
3255int
3256mcore_dllexport_name_p (symbol)
3257 char * symbol;
3258{
3259 return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
3260}
3261
3262/* Return non-zero if SYMBOL is marked as being dllimport'd. */
3263int
3264mcore_dllimport_name_p (symbol)
3265 char * symbol;
3266{
3267 return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
3268}
3269
3270/* Mark a DECL as being dllexport'd. */
3271static void
3272mcore_mark_dllexport (decl)
3273 tree decl;
3274{
3275 char * oldname;
3276 char * newname;
3277 rtx rtlname;
3278 tree idp;
3279
3280 rtlname = XEXP (DECL_RTL (decl), 0);
3281
3282 if (GET_CODE (rtlname) == SYMBOL_REF)
3283 oldname = XSTR (rtlname, 0);
3284 else if ( GET_CODE (rtlname) == MEM
3285 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
3286 oldname = XSTR (XEXP (rtlname, 0), 0);
3287 else
3288 abort ();
3289
3290 if (mcore_dllexport_name_p (oldname))
3291 return; /* Already done. */
3292
3293 newname = alloca (strlen (oldname) + 4);
3294 sprintf (newname, "@e.%s", oldname);
3295
3296 /* We pass newname through get_identifier to ensure it has a unique
3297 address. RTL processing can sometimes peek inside the symbol ref
3298 and compare the string's addresses to see if two symbols are
3299 identical. */
3300 /* ??? At least I think that's why we do this. */
3301 idp = get_identifier (newname);
3302
3303 XEXP (DECL_RTL (decl), 0) =
3304 gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
3305}
3306
3307/* Mark a DECL as being dllimport'd. */
3308static void
3309mcore_mark_dllimport (decl)
3310 tree decl;
3311{
3312 char * oldname;
3313 char * newname;
3314 tree idp;
3315 rtx rtlname;
3316 rtx newrtl;
3317
3318 rtlname = XEXP (DECL_RTL (decl), 0);
3319
3320 if (GET_CODE (rtlname) == SYMBOL_REF)
3321 oldname = XSTR (rtlname, 0);
3322 else if ( GET_CODE (rtlname) == MEM
3323 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
3324 oldname = XSTR (XEXP (rtlname, 0), 0);
3325 else
3326 abort ();
3327
3328 if (mcore_dllexport_name_p (oldname))
3329 abort (); /* This shouldn't happen. */
3330 else if (mcore_dllimport_name_p (oldname))
3331 return; /* Already done. */
3332
3333 /* ??? One can well ask why we're making these checks here,
3334 and that would be a good question. */
3335
3336 /* Imported variables can't be initialized. */
3337 if (TREE_CODE (decl) == VAR_DECL
3338 && !DECL_VIRTUAL_P (decl)
3339 && DECL_INITIAL (decl))
3340 {
3341 error_with_decl (decl, "initialized variable `%s' is marked dllimport");
3342 return;
3343 }
3344
3345 /* `extern' needn't be specified with dllimport.
3346 Specify `extern' now and hope for the best. Sigh. */
3347 if (TREE_CODE (decl) == VAR_DECL
3348 /* ??? Is this test for vtables needed? */
3349 && !DECL_VIRTUAL_P (decl))
3350 {
3351 DECL_EXTERNAL (decl) = 1;
3352 TREE_PUBLIC (decl) = 1;
3353 }
3354
3355 newname = alloca (strlen (oldname) + 11);
3356 sprintf (newname, "@i.__imp_%s", oldname);
3357
3358 /* We pass newname through get_identifier to ensure it has a unique
3359 address. RTL processing can sometimes peek inside the symbol ref
3360 and compare the string's addresses to see if two symbols are
3361 identical. */
3362 /* ??? At least I think that's why we do this. */
3363 idp = get_identifier (newname);
3364
3365 newrtl = gen_rtx (MEM, Pmode,
3366 gen_rtx (SYMBOL_REF, Pmode,
3367 IDENTIFIER_POINTER (idp)));
3368 XEXP (DECL_RTL (decl), 0) = newrtl;
3369}
3370
3371static int
3372mcore_dllexport_p (decl)
3373 tree decl;
3374{
3375 if ( TREE_CODE (decl) != VAR_DECL
3376 && TREE_CODE (decl) != FUNCTION_DECL)
3377 return 0;
3378
3379 return lookup_attribute ("dllexport", DECL_MACHINE_ATTRIBUTES (decl)) != 0;
3380}
3381
3382static int
3383mcore_dllimport_p (decl)
3384 tree decl;
3385{
3386 if ( TREE_CODE (decl) != VAR_DECL
3387 && TREE_CODE (decl) != FUNCTION_DECL)
3388 return 0;
3389
3390 return lookup_attribute ("dllimport", DECL_MACHINE_ATTRIBUTES (decl)) != 0;
3391}
3392
3393/* Cover function to implement ENCODE_SECTION_INFO. */
3394void
3395mcore_encode_section_info (decl)
3396 tree decl;
3397{
3398 /* This bit is copied from arm.h. */
3399 if (optimize > 0
3400 && TREE_CONSTANT (decl)
3401 && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
3402 {
3403 rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
3404 ? TREE_CST_RTL (decl) : DECL_RTL (decl));
3405 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
3406 }
3407
3408 /* Mark the decl so we can tell from the rtl whether the object is
3409 dllexport'd or dllimport'd. */
3410 if (mcore_dllexport_p (decl))
3411 mcore_mark_dllexport (decl);
3412 else if (mcore_dllimport_p (decl))
3413 mcore_mark_dllimport (decl);
3414
3415 /* It might be that DECL has already been marked as dllimport, but
3416 a subsequent definition nullified that. The attribute is gone
3417 but DECL_RTL still has @i.__imp_foo. We need to remove that. */
3418 else if ((TREE_CODE (decl) == FUNCTION_DECL
3419 || TREE_CODE (decl) == VAR_DECL)
3420 && DECL_RTL (decl) != NULL_RTX
3421 && GET_CODE (DECL_RTL (decl)) == MEM
3422 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
3423 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
3424 && mcore_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
3425 {
3cce094d 3426 const char * oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
8f90be4c
NC
3427 tree idp = get_identifier (oldname + 9);
3428 rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
3429
3430 XEXP (DECL_RTL (decl), 0) = newrtl;
3431
3432 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
3433 ??? We leave these alone for now. */
3434 }
3435}
3436
3437/* MCore specific attribute support.
3438 dllexport - for exporting a function/variable that will live in a dll
3439 dllimport - for importing a function/variable from a dll
3440 naked - do not create a function prologue/epilogue. */
3441int
3442mcore_valid_machine_decl_attribute (decl, attributes, attr, args)
3443 tree decl;
3444 tree attributes ATTRIBUTE_UNUSED;
3445 tree attr;
3446 tree args;
3447{
3448 if (args != NULL_TREE)
3449 return 0;
3450
3451 if (is_attribute_p ("dllexport", attr))
3452 return 1;
3453
3454 if (is_attribute_p ("dllimport", attr))
3455 return 1;
3456
3457 if (is_attribute_p ("naked", attr) &&
3458 TREE_CODE (decl) == FUNCTION_DECL)
3459 {
3460 /* PR14310 - don't complain about lack of return statement
3461 in naked functions. The solution here is a gross hack
3462 but this is the only way to solve the problem without
3463 adding a new feature to GCC. I did try submitting a patch
3464 that would add such a new feature, but it was (rightfully)
3465 rejected on the grounds that it was creeping featurism,
3466 so hence this code. */
3467 if (warn_return_type)
3468 {
3469 saved_warn_return_type = warn_return_type;
3470 warn_return_type = 0;
3471 saved_warn_return_type_count = 2;
3472 }
3473 else if (saved_warn_return_type_count)
3474 saved_warn_return_type_count = 2;
3475
3476 return 1;
3477 }
3478
3479 return 0;
3480}
3481
3482/* Merge attributes in decls OLD and NEW.
3483 This handles the following situation:
3484
3485 __declspec (dllimport) int foo;
3486 int foo;
3487
3488 The second instance of `foo' nullifies the dllimport. */
3489tree
3490mcore_merge_machine_decl_attributes (old, new)
3491 tree old;
3492 tree new;
3493{
3494 tree a;
3495 int delete_dllimport_p;
3496
3497 old = DECL_MACHINE_ATTRIBUTES (old);
3498 new = DECL_MACHINE_ATTRIBUTES (new);
3499
3500 /* What we need to do here is remove from `old' dllimport if it doesn't
3501 appear in `new'. dllimport behaves like extern: if a declaration is
3502 marked dllimport and a definition appears later, then the object
3503 is not dllimport'd. */
3504 if ( lookup_attribute ("dllimport", old) != NULL_TREE
3505 && lookup_attribute ("dllimport", new) == NULL_TREE)
3506 delete_dllimport_p = 1;
3507 else
3508 delete_dllimport_p = 0;
3509
3510 a = merge_attributes (old, new);
3511
3512 if (delete_dllimport_p)
3513 {
3514 tree prev,t;
3515
3516 /* Scan the list for dllimport and delete it. */
3517 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
3518 {
3519 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
3520 {
3521 if (prev == NULL_TREE)
3522 a = TREE_CHAIN (a);
3523 else
3524 TREE_CHAIN (prev) = TREE_CHAIN (t);
3525 break;
3526 }
3527 }
3528 }
3529
3530 return a;
3531}
3532
3533/* Cover function for UNIQUE_SECTION. */
3534
3535void
3536mcore_unique_section (decl, reloc)
3537 tree decl;
3538 int reloc ATTRIBUTE_UNUSED;
3539{
3540 int len;
3541 char * name;
3542 char * string;
3543 char * prefix;
3544
3545 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3546
3547 /* Strip off any encoding in name. */
3548 STRIP_NAME_ENCODING (name, name);
3549
3550 /* The object is put in, for example, section .text$foo.
3551 The linker will then ultimately place them in .text
3552 (everything from the $ on is stripped). */
3553 if (TREE_CODE (decl) == FUNCTION_DECL)
3554 prefix = ".text$";
3555 /* For compatability with EPOC, we ignore the fact that the
3556 section might have relocs against it. */
3557 else if (DECL_READONLY_SECTION (decl, 0))
3558 prefix = ".rdata$";
3559 else
3560 prefix = ".data$";
3561
3562 len = strlen (name) + strlen (prefix);
3563 string = alloca (len + 1);
3564
3565 sprintf (string, "%s%s", prefix, name);
3566
3567 DECL_SECTION_NAME (decl) = build_string (len, string);
3568}
3569
3570int
3571mcore_naked_function_p ()
3572{
3573 return lookup_attribute ("naked", DECL_MACHINE_ATTRIBUTES (current_function_decl)) != NULL_TREE;
3574}