* config/bfin/bfin.md (reload_outpdi, reload_inpdi): New patterns.
[official-gcc/alias-decl.git] / gcc / config / bfin / bfin.c
blobed456a880bb42b4ec4930d7cb93747719add563e
1 /* The Blackfin code generation auxiliary output file.
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Analog Devices.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "insn-codes.h"
31 #include "conditions.h"
32 #include "insn-flags.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "except.h"
38 #include "function.h"
39 #include "input.h"
40 #include "target.h"
41 #include "target-def.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "recog.h"
45 #include "optabs.h"
46 #include "ggc.h"
47 #include "integrate.h"
48 #include "cgraph.h"
49 #include "langhooks.h"
50 #include "bfin-protos.h"
51 #include "tm-preds.h"
52 #include "gt-bfin.h"
53 #include "basic-block.h"
54 #include "cfglayout.h"
55 #include "timevar.h"
56 #include "df.h"
58 /* A C structure for machine-specific, per-function data.
59 This is added to the cfun structure. */
60 struct machine_function GTY(())
62 int has_hardware_loops;
65 /* Test and compare insns in bfin.md store the information needed to
66 generate branch and scc insns here. */
67 rtx bfin_compare_op0, bfin_compare_op1;
69 /* RTX for condition code flag register and RETS register */
70 extern GTY(()) rtx bfin_cc_rtx;
71 extern GTY(()) rtx bfin_rets_rtx;
72 rtx bfin_cc_rtx, bfin_rets_rtx;
74 int max_arg_registers = 0;
76 /* Arrays used when emitting register names. */
77 const char *short_reg_names[] = SHORT_REGISTER_NAMES;
78 const char *high_reg_names[] = HIGH_REGISTER_NAMES;
79 const char *dregs_pair_names[] = DREGS_PAIR_NAMES;
80 const char *byte_reg_names[] = BYTE_REGISTER_NAMES;
82 static int arg_regs[] = FUNCTION_ARG_REGISTERS;
84 /* Nonzero if -mshared-library-id was given. */
85 static int bfin_lib_id_given;
87 /* Nonzero if -fschedule-insns2 was given. We override it and
88 call the scheduler ourselves during reorg. */
89 static int bfin_flag_schedule_insns2;
91 /* Determines whether we run variable tracking in machine dependent
92 reorganization. */
93 static int bfin_flag_var_tracking;
95 /* -mcpu support */
96 bfin_cpu_t bfin_cpu_type = DEFAULT_CPU_TYPE;
98 int splitting_for_sched;
100 static void
101 bfin_globalize_label (FILE *stream, const char *name)
103 fputs (".global ", stream);
104 assemble_name (stream, name);
105 fputc (';',stream);
106 fputc ('\n',stream);
109 static void
110 output_file_start (void)
112 FILE *file = asm_out_file;
113 int i;
115 /* Variable tracking should be run after all optimizations which change order
116 of insns. It also needs a valid CFG. This can't be done in
117 override_options, because flag_var_tracking is finalized after
118 that. */
119 bfin_flag_var_tracking = flag_var_tracking;
120 flag_var_tracking = 0;
122 fprintf (file, ".file \"%s\";\n", input_filename);
124 for (i = 0; arg_regs[i] >= 0; i++)
126 max_arg_registers = i; /* how many arg reg used */
129 /* Called early in the compilation to conditionally modify
130 fixed_regs/call_used_regs. */
132 void
133 conditional_register_usage (void)
135 /* initialize condition code flag register rtx */
136 bfin_cc_rtx = gen_rtx_REG (BImode, REG_CC);
137 bfin_rets_rtx = gen_rtx_REG (Pmode, REG_RETS);
140 /* Examine machine-dependent attributes of function type FUNTYPE and return its
141 type. See the definition of E_FUNKIND. */
143 static e_funkind
144 funkind (const_tree funtype)
146 tree attrs = TYPE_ATTRIBUTES (funtype);
147 if (lookup_attribute ("interrupt_handler", attrs))
148 return INTERRUPT_HANDLER;
149 else if (lookup_attribute ("exception_handler", attrs))
150 return EXCPT_HANDLER;
151 else if (lookup_attribute ("nmi_handler", attrs))
152 return NMI_HANDLER;
153 else
154 return SUBROUTINE;
157 /* Legitimize PIC addresses. If the address is already position-independent,
158 we return ORIG. Newly generated position-independent addresses go into a
159 reg. This is REG if nonzero, otherwise we allocate register(s) as
160 necessary. PICREG is the register holding the pointer to the PIC offset
161 table. */
163 static rtx
164 legitimize_pic_address (rtx orig, rtx reg, rtx picreg)
166 rtx addr = orig;
167 rtx new = orig;
169 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
171 int unspec;
172 rtx tmp;
174 if (TARGET_ID_SHARED_LIBRARY)
175 unspec = UNSPEC_MOVE_PIC;
176 else if (GET_CODE (addr) == SYMBOL_REF
177 && SYMBOL_REF_FUNCTION_P (addr))
178 unspec = UNSPEC_FUNCDESC_GOT17M4;
179 else
180 unspec = UNSPEC_MOVE_FDPIC;
182 if (reg == 0)
184 gcc_assert (can_create_pseudo_p ());
185 reg = gen_reg_rtx (Pmode);
188 tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), unspec);
189 new = gen_const_mem (Pmode, gen_rtx_PLUS (Pmode, picreg, tmp));
191 emit_move_insn (reg, new);
192 if (picreg == pic_offset_table_rtx)
193 current_function_uses_pic_offset_table = 1;
194 return reg;
197 else if (GET_CODE (addr) == CONST || GET_CODE (addr) == PLUS)
199 rtx base;
201 if (GET_CODE (addr) == CONST)
203 addr = XEXP (addr, 0);
204 gcc_assert (GET_CODE (addr) == PLUS);
207 if (XEXP (addr, 0) == picreg)
208 return orig;
210 if (reg == 0)
212 gcc_assert (can_create_pseudo_p ());
213 reg = gen_reg_rtx (Pmode);
216 base = legitimize_pic_address (XEXP (addr, 0), reg, picreg);
217 addr = legitimize_pic_address (XEXP (addr, 1),
218 base == reg ? NULL_RTX : reg,
219 picreg);
221 if (GET_CODE (addr) == CONST_INT)
223 gcc_assert (! reload_in_progress && ! reload_completed);
224 addr = force_reg (Pmode, addr);
227 if (GET_CODE (addr) == PLUS && CONSTANT_P (XEXP (addr, 1)))
229 base = gen_rtx_PLUS (Pmode, base, XEXP (addr, 0));
230 addr = XEXP (addr, 1);
233 return gen_rtx_PLUS (Pmode, base, addr);
236 return new;
239 /* Stack frame layout. */
241 /* Compute the number of DREGS to save with a push_multiple operation.
242 This could include registers that aren't modified in the function,
243 since push_multiple only takes a range of registers.
244 If IS_INTHANDLER, then everything that is live must be saved, even
245 if normally call-clobbered. */
247 static int
248 n_dregs_to_save (bool is_inthandler)
250 unsigned i;
252 for (i = REG_R0; i <= REG_R7; i++)
254 if (df_regs_ever_live_p (i) && (is_inthandler || ! call_used_regs[i]))
255 return REG_R7 - i + 1;
257 if (current_function_calls_eh_return)
259 unsigned j;
260 for (j = 0; ; j++)
262 unsigned test = EH_RETURN_DATA_REGNO (j);
263 if (test == INVALID_REGNUM)
264 break;
265 if (test == i)
266 return REG_R7 - i + 1;
271 return 0;
274 /* Like n_dregs_to_save, but compute number of PREGS to save. */
276 static int
277 n_pregs_to_save (bool is_inthandler)
279 unsigned i;
281 for (i = REG_P0; i <= REG_P5; i++)
282 if ((df_regs_ever_live_p (i) && (is_inthandler || ! call_used_regs[i]))
283 || (!TARGET_FDPIC
284 && i == PIC_OFFSET_TABLE_REGNUM
285 && (current_function_uses_pic_offset_table
286 || (TARGET_ID_SHARED_LIBRARY && ! current_function_is_leaf))))
287 return REG_P5 - i + 1;
288 return 0;
291 /* Determine if we are going to save the frame pointer in the prologue. */
293 static bool
294 must_save_fp_p (void)
296 return frame_pointer_needed || df_regs_ever_live_p (REG_FP);
299 static bool
300 stack_frame_needed_p (void)
302 /* EH return puts a new return address into the frame using an
303 address relative to the frame pointer. */
304 if (current_function_calls_eh_return)
305 return true;
306 return frame_pointer_needed;
309 /* Emit code to save registers in the prologue. SAVEALL is nonzero if we
310 must save all registers; this is used for interrupt handlers.
311 SPREG contains (reg:SI REG_SP). IS_INTHANDLER is true if we're doing
312 this for an interrupt (or exception) handler. */
314 static void
315 expand_prologue_reg_save (rtx spreg, int saveall, bool is_inthandler)
317 int ndregs = saveall ? 8 : n_dregs_to_save (is_inthandler);
318 int npregs = saveall ? 6 : n_pregs_to_save (is_inthandler);
319 int dregno = REG_R7 + 1 - ndregs;
320 int pregno = REG_P5 + 1 - npregs;
321 int total = ndregs + npregs;
322 int i;
323 rtx pat, insn, val;
325 if (total == 0)
326 return;
328 val = GEN_INT (-total * 4);
329 pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total + 2));
330 XVECEXP (pat, 0, 0) = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, val),
331 UNSPEC_PUSH_MULTIPLE);
332 XVECEXP (pat, 0, total + 1) = gen_rtx_SET (VOIDmode, spreg,
333 gen_rtx_PLUS (Pmode, spreg,
334 val));
335 RTX_FRAME_RELATED_P (XVECEXP (pat, 0, total + 1)) = 1;
336 for (i = 0; i < total; i++)
338 rtx memref = gen_rtx_MEM (word_mode,
339 gen_rtx_PLUS (Pmode, spreg,
340 GEN_INT (- i * 4 - 4)));
341 rtx subpat;
342 if (ndregs > 0)
344 subpat = gen_rtx_SET (VOIDmode, memref, gen_rtx_REG (word_mode,
345 dregno++));
346 ndregs--;
348 else
350 subpat = gen_rtx_SET (VOIDmode, memref, gen_rtx_REG (word_mode,
351 pregno++));
352 npregs++;
354 XVECEXP (pat, 0, i + 1) = subpat;
355 RTX_FRAME_RELATED_P (subpat) = 1;
357 insn = emit_insn (pat);
358 RTX_FRAME_RELATED_P (insn) = 1;
361 /* Emit code to restore registers in the epilogue. SAVEALL is nonzero if we
362 must save all registers; this is used for interrupt handlers.
363 SPREG contains (reg:SI REG_SP). IS_INTHANDLER is true if we're doing
364 this for an interrupt (or exception) handler. */
366 static void
367 expand_epilogue_reg_restore (rtx spreg, bool saveall, bool is_inthandler)
369 int ndregs = saveall ? 8 : n_dregs_to_save (is_inthandler);
370 int npregs = saveall ? 6 : n_pregs_to_save (is_inthandler);
371 int total = ndregs + npregs;
372 int i, regno;
373 rtx pat, insn;
375 if (total == 0)
376 return;
378 pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total + 1));
379 XVECEXP (pat, 0, 0) = gen_rtx_SET (VOIDmode, spreg,
380 gen_rtx_PLUS (Pmode, spreg,
381 GEN_INT (total * 4)));
383 if (npregs > 0)
384 regno = REG_P5 + 1;
385 else
386 regno = REG_R7 + 1;
388 for (i = 0; i < total; i++)
390 rtx addr = (i > 0
391 ? gen_rtx_PLUS (Pmode, spreg, GEN_INT (i * 4))
392 : spreg);
393 rtx memref = gen_rtx_MEM (word_mode, addr);
395 regno--;
396 XVECEXP (pat, 0, i + 1)
397 = gen_rtx_SET (VOIDmode, gen_rtx_REG (word_mode, regno), memref);
399 if (npregs > 0)
401 if (--npregs == 0)
402 regno = REG_R7 + 1;
406 insn = emit_insn (pat);
407 RTX_FRAME_RELATED_P (insn) = 1;
410 /* Perform any needed actions needed for a function that is receiving a
411 variable number of arguments.
413 CUM is as above.
415 MODE and TYPE are the mode and type of the current parameter.
417 PRETEND_SIZE is a variable that should be set to the amount of stack
418 that must be pushed by the prolog to pretend that our caller pushed
421 Normally, this macro will push all remaining incoming registers on the
422 stack and set PRETEND_SIZE to the length of the registers pushed.
424 Blackfin specific :
425 - VDSP C compiler manual (our ABI) says that a variable args function
426 should save the R0, R1 and R2 registers in the stack.
427 - The caller will always leave space on the stack for the
428 arguments that are passed in registers, so we dont have
429 to leave any extra space.
430 - now, the vastart pointer can access all arguments from the stack. */
432 static void
433 setup_incoming_varargs (CUMULATIVE_ARGS *cum,
434 enum machine_mode mode ATTRIBUTE_UNUSED,
435 tree type ATTRIBUTE_UNUSED, int *pretend_size,
436 int no_rtl)
438 rtx mem;
439 int i;
441 if (no_rtl)
442 return;
444 /* The move for named arguments will be generated automatically by the
445 compiler. We need to generate the move rtx for the unnamed arguments
446 if they are in the first 3 words. We assume at least 1 named argument
447 exists, so we never generate [ARGP] = R0 here. */
449 for (i = cum->words + 1; i < max_arg_registers; i++)
451 mem = gen_rtx_MEM (Pmode,
452 plus_constant (arg_pointer_rtx, (i * UNITS_PER_WORD)));
453 emit_move_insn (mem, gen_rtx_REG (Pmode, i));
456 *pretend_size = 0;
459 /* Value should be nonzero if functions must have frame pointers.
460 Zero means the frame pointer need not be set up (and parms may
461 be accessed via the stack pointer) in functions that seem suitable. */
464 bfin_frame_pointer_required (void)
466 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
468 if (fkind != SUBROUTINE)
469 return 1;
471 /* We turn on -fomit-frame-pointer if -momit-leaf-frame-pointer is used,
472 so we have to override it for non-leaf functions. */
473 if (TARGET_OMIT_LEAF_FRAME_POINTER && ! current_function_is_leaf)
474 return 1;
476 return 0;
479 /* Return the number of registers pushed during the prologue. */
481 static int
482 n_regs_saved_by_prologue (void)
484 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
485 bool is_inthandler = fkind != SUBROUTINE;
486 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
487 bool all = (lookup_attribute ("saveall", attrs) != NULL_TREE
488 || (is_inthandler && !current_function_is_leaf));
489 int ndregs = all ? 8 : n_dregs_to_save (is_inthandler);
490 int npregs = all ? 6 : n_pregs_to_save (is_inthandler);
491 int n = ndregs + npregs;
493 if (all || stack_frame_needed_p ())
494 /* We use a LINK instruction in this case. */
495 n += 2;
496 else
498 if (must_save_fp_p ())
499 n++;
500 if (! current_function_is_leaf)
501 n++;
504 if (fkind != SUBROUTINE)
506 int i;
508 /* Increment once for ASTAT. */
509 n++;
511 /* RETE/X/N. */
512 if (lookup_attribute ("nesting", attrs))
513 n++;
515 for (i = REG_P7 + 1; i < REG_CC; i++)
516 if (all
517 || df_regs_ever_live_p (i)
518 || (!leaf_function_p () && call_used_regs[i]))
519 n += i == REG_A0 || i == REG_A1 ? 2 : 1;
521 return n;
524 /* Return the offset between two registers, one to be eliminated, and the other
525 its replacement, at the start of a routine. */
527 HOST_WIDE_INT
528 bfin_initial_elimination_offset (int from, int to)
530 HOST_WIDE_INT offset = 0;
532 if (from == ARG_POINTER_REGNUM)
533 offset = n_regs_saved_by_prologue () * 4;
535 if (to == STACK_POINTER_REGNUM)
537 if (current_function_outgoing_args_size >= FIXED_STACK_AREA)
538 offset += current_function_outgoing_args_size;
539 else if (current_function_outgoing_args_size)
540 offset += FIXED_STACK_AREA;
542 offset += get_frame_size ();
545 return offset;
548 /* Emit code to load a constant CONSTANT into register REG; setting
549 RTX_FRAME_RELATED_P on all insns we generate if RELATED is true.
550 Make sure that the insns we generate need not be split. */
552 static void
553 frame_related_constant_load (rtx reg, HOST_WIDE_INT constant, bool related)
555 rtx insn;
556 rtx cst = GEN_INT (constant);
558 if (constant >= -32768 && constant < 65536)
559 insn = emit_move_insn (reg, cst);
560 else
562 /* We don't call split_load_immediate here, since dwarf2out.c can get
563 confused about some of the more clever sequences it can generate. */
564 insn = emit_insn (gen_movsi_high (reg, cst));
565 if (related)
566 RTX_FRAME_RELATED_P (insn) = 1;
567 insn = emit_insn (gen_movsi_low (reg, reg, cst));
569 if (related)
570 RTX_FRAME_RELATED_P (insn) = 1;
573 /* Generate efficient code to add a value to a P register.
574 Set RTX_FRAME_RELATED_P on the generated insns if FRAME is nonzero.
575 EPILOGUE_P is zero if this function is called for prologue,
576 otherwise it's nonzero. And it's less than zero if this is for
577 sibcall epilogue. */
579 static void
580 add_to_reg (rtx reg, HOST_WIDE_INT value, int frame, int epilogue_p)
582 if (value == 0)
583 return;
585 /* Choose whether to use a sequence using a temporary register, or
586 a sequence with multiple adds. We can add a signed 7-bit value
587 in one instruction. */
588 if (value > 120 || value < -120)
590 rtx tmpreg;
591 rtx tmpreg2;
592 rtx insn;
594 tmpreg2 = NULL_RTX;
596 /* For prologue or normal epilogue, P1 can be safely used
597 as the temporary register. For sibcall epilogue, we try to find
598 a call used P register, which will be restored in epilogue.
599 If we cannot find such a P register, we have to use one I register
600 to help us. */
602 if (epilogue_p >= 0)
603 tmpreg = gen_rtx_REG (SImode, REG_P1);
604 else
606 int i;
607 for (i = REG_P0; i <= REG_P5; i++)
608 if ((df_regs_ever_live_p (i) && ! call_used_regs[i])
609 || (!TARGET_FDPIC
610 && i == PIC_OFFSET_TABLE_REGNUM
611 && (current_function_uses_pic_offset_table
612 || (TARGET_ID_SHARED_LIBRARY
613 && ! current_function_is_leaf))))
614 break;
615 if (i <= REG_P5)
616 tmpreg = gen_rtx_REG (SImode, i);
617 else
619 tmpreg = gen_rtx_REG (SImode, REG_P1);
620 tmpreg2 = gen_rtx_REG (SImode, REG_I0);
621 emit_move_insn (tmpreg2, tmpreg);
625 if (frame)
626 frame_related_constant_load (tmpreg, value, TRUE);
627 else
628 insn = emit_move_insn (tmpreg, GEN_INT (value));
630 insn = emit_insn (gen_addsi3 (reg, reg, tmpreg));
631 if (frame)
632 RTX_FRAME_RELATED_P (insn) = 1;
634 if (tmpreg2 != NULL_RTX)
635 emit_move_insn (tmpreg, tmpreg2);
637 else
640 int size = value;
641 rtx insn;
643 if (size > 60)
644 size = 60;
645 else if (size < -60)
646 /* We could use -62, but that would leave the stack unaligned, so
647 it's no good. */
648 size = -60;
650 insn = emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
651 if (frame)
652 RTX_FRAME_RELATED_P (insn) = 1;
653 value -= size;
655 while (value != 0);
658 /* Generate a LINK insn for a frame sized FRAME_SIZE. If this constant
659 is too large, generate a sequence of insns that has the same effect.
660 SPREG contains (reg:SI REG_SP). */
662 static void
663 emit_link_insn (rtx spreg, HOST_WIDE_INT frame_size)
665 HOST_WIDE_INT link_size = frame_size;
666 rtx insn;
667 int i;
669 if (link_size > 262140)
670 link_size = 262140;
672 /* Use a LINK insn with as big a constant as possible, then subtract
673 any remaining size from the SP. */
674 insn = emit_insn (gen_link (GEN_INT (-8 - link_size)));
675 RTX_FRAME_RELATED_P (insn) = 1;
677 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
679 rtx set = XVECEXP (PATTERN (insn), 0, i);
680 gcc_assert (GET_CODE (set) == SET);
681 RTX_FRAME_RELATED_P (set) = 1;
684 frame_size -= link_size;
686 if (frame_size > 0)
688 /* Must use a call-clobbered PREG that isn't the static chain. */
689 rtx tmpreg = gen_rtx_REG (Pmode, REG_P1);
691 frame_related_constant_load (tmpreg, -frame_size, TRUE);
692 insn = emit_insn (gen_addsi3 (spreg, spreg, tmpreg));
693 RTX_FRAME_RELATED_P (insn) = 1;
697 /* Return the number of bytes we must reserve for outgoing arguments
698 in the current function's stack frame. */
700 static HOST_WIDE_INT
701 arg_area_size (void)
703 if (current_function_outgoing_args_size)
705 if (current_function_outgoing_args_size >= FIXED_STACK_AREA)
706 return current_function_outgoing_args_size;
707 else
708 return FIXED_STACK_AREA;
710 return 0;
713 /* Save RETS and FP, and allocate a stack frame. ALL is true if the
714 function must save all its registers (true only for certain interrupt
715 handlers). */
717 static void
718 do_link (rtx spreg, HOST_WIDE_INT frame_size, bool all)
720 frame_size += arg_area_size ();
722 if (all || stack_frame_needed_p ()
723 || (must_save_fp_p () && ! current_function_is_leaf))
724 emit_link_insn (spreg, frame_size);
725 else
727 if (! current_function_is_leaf)
729 rtx pat = gen_movsi (gen_rtx_MEM (Pmode,
730 gen_rtx_PRE_DEC (Pmode, spreg)),
731 bfin_rets_rtx);
732 rtx insn = emit_insn (pat);
733 RTX_FRAME_RELATED_P (insn) = 1;
735 if (must_save_fp_p ())
737 rtx pat = gen_movsi (gen_rtx_MEM (Pmode,
738 gen_rtx_PRE_DEC (Pmode, spreg)),
739 gen_rtx_REG (Pmode, REG_FP));
740 rtx insn = emit_insn (pat);
741 RTX_FRAME_RELATED_P (insn) = 1;
743 add_to_reg (spreg, -frame_size, 1, 0);
747 /* Like do_link, but used for epilogues to deallocate the stack frame.
748 EPILOGUE_P is zero if this function is called for prologue,
749 otherwise it's nonzero. And it's less than zero if this is for
750 sibcall epilogue. */
752 static void
753 do_unlink (rtx spreg, HOST_WIDE_INT frame_size, bool all, int epilogue_p)
755 frame_size += arg_area_size ();
757 if (all || stack_frame_needed_p ())
758 emit_insn (gen_unlink ());
759 else
761 rtx postinc = gen_rtx_MEM (Pmode, gen_rtx_POST_INC (Pmode, spreg));
763 add_to_reg (spreg, frame_size, 0, epilogue_p);
764 if (must_save_fp_p ())
766 rtx fpreg = gen_rtx_REG (Pmode, REG_FP);
767 emit_move_insn (fpreg, postinc);
768 emit_insn (gen_rtx_USE (VOIDmode, fpreg));
770 if (! current_function_is_leaf)
772 emit_move_insn (bfin_rets_rtx, postinc);
773 emit_insn (gen_rtx_USE (VOIDmode, bfin_rets_rtx));
778 /* Generate a prologue suitable for a function of kind FKIND. This is
779 called for interrupt and exception handler prologues.
780 SPREG contains (reg:SI REG_SP). */
782 static void
783 expand_interrupt_handler_prologue (rtx spreg, e_funkind fkind)
785 int i;
786 HOST_WIDE_INT frame_size = get_frame_size ();
787 rtx predec1 = gen_rtx_PRE_DEC (SImode, spreg);
788 rtx predec = gen_rtx_MEM (SImode, predec1);
789 rtx insn;
790 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
791 bool all = lookup_attribute ("saveall", attrs) != NULL_TREE;
792 tree kspisusp = lookup_attribute ("kspisusp", attrs);
794 if (kspisusp)
796 insn = emit_move_insn (spreg, gen_rtx_REG (Pmode, REG_USP));
797 RTX_FRAME_RELATED_P (insn) = 1;
800 /* We need space on the stack in case we need to save the argument
801 registers. */
802 if (fkind == EXCPT_HANDLER)
804 insn = emit_insn (gen_addsi3 (spreg, spreg, GEN_INT (-12)));
805 RTX_FRAME_RELATED_P (insn) = 1;
808 insn = emit_move_insn (predec, gen_rtx_REG (SImode, REG_ASTAT));
809 RTX_FRAME_RELATED_P (insn) = 1;
811 /* If we're calling other functions, they won't save their call-clobbered
812 registers, so we must save everything here. */
813 if (!current_function_is_leaf)
814 all = true;
815 expand_prologue_reg_save (spreg, all, true);
817 for (i = REG_P7 + 1; i < REG_CC; i++)
818 if (all
819 || df_regs_ever_live_p (i)
820 || (!leaf_function_p () && call_used_regs[i]))
822 if (i == REG_A0 || i == REG_A1)
823 insn = emit_move_insn (gen_rtx_MEM (PDImode, predec1),
824 gen_rtx_REG (PDImode, i));
825 else
826 insn = emit_move_insn (predec, gen_rtx_REG (SImode, i));
827 RTX_FRAME_RELATED_P (insn) = 1;
830 if (lookup_attribute ("nesting", attrs))
832 rtx srcreg = gen_rtx_REG (Pmode, (fkind == EXCPT_HANDLER ? REG_RETX
833 : fkind == NMI_HANDLER ? REG_RETN
834 : REG_RETI));
835 insn = emit_move_insn (predec, srcreg);
836 RTX_FRAME_RELATED_P (insn) = 1;
839 do_link (spreg, frame_size, all);
841 if (fkind == EXCPT_HANDLER)
843 rtx r0reg = gen_rtx_REG (SImode, REG_R0);
844 rtx r1reg = gen_rtx_REG (SImode, REG_R1);
845 rtx r2reg = gen_rtx_REG (SImode, REG_R2);
846 rtx insn;
848 insn = emit_move_insn (r0reg, gen_rtx_REG (SImode, REG_SEQSTAT));
849 insn = emit_insn (gen_ashrsi3 (r0reg, r0reg, GEN_INT (26)));
850 insn = emit_insn (gen_ashlsi3 (r0reg, r0reg, GEN_INT (26)));
851 insn = emit_move_insn (r1reg, spreg);
852 insn = emit_move_insn (r2reg, gen_rtx_REG (Pmode, REG_FP));
853 insn = emit_insn (gen_addsi3 (r2reg, r2reg, GEN_INT (8)));
857 /* Generate an epilogue suitable for a function of kind FKIND. This is
858 called for interrupt and exception handler epilogues.
859 SPREG contains (reg:SI REG_SP). */
861 static void
862 expand_interrupt_handler_epilogue (rtx spreg, e_funkind fkind)
864 int i;
865 rtx postinc1 = gen_rtx_POST_INC (SImode, spreg);
866 rtx postinc = gen_rtx_MEM (SImode, postinc1);
867 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
868 bool all = lookup_attribute ("saveall", attrs) != NULL_TREE;
870 /* A slightly crude technique to stop flow from trying to delete "dead"
871 insns. */
872 MEM_VOLATILE_P (postinc) = 1;
874 do_unlink (spreg, get_frame_size (), all, 1);
876 if (lookup_attribute ("nesting", attrs))
878 rtx srcreg = gen_rtx_REG (Pmode, (fkind == EXCPT_HANDLER ? REG_RETX
879 : fkind == NMI_HANDLER ? REG_RETN
880 : REG_RETI));
881 emit_move_insn (srcreg, postinc);
884 /* If we're calling other functions, they won't save their call-clobbered
885 registers, so we must save (and restore) everything here. */
886 if (!current_function_is_leaf)
887 all = true;
889 for (i = REG_CC - 1; i > REG_P7; i--)
890 if (all
891 || df_regs_ever_live_p (i)
892 || (!leaf_function_p () && call_used_regs[i]))
894 if (i == REG_A0 || i == REG_A1)
896 rtx mem = gen_rtx_MEM (PDImode, postinc1);
897 MEM_VOLATILE_P (mem) = 1;
898 emit_move_insn (gen_rtx_REG (PDImode, i), mem);
900 else
901 emit_move_insn (gen_rtx_REG (SImode, i), postinc);
904 expand_epilogue_reg_restore (spreg, all, true);
906 emit_move_insn (gen_rtx_REG (SImode, REG_ASTAT), postinc);
908 /* Deallocate any space we left on the stack in case we needed to save the
909 argument registers. */
910 if (fkind == EXCPT_HANDLER)
911 emit_insn (gen_addsi3 (spreg, spreg, GEN_INT (12)));
913 emit_jump_insn (gen_return_internal (GEN_INT (fkind)));
916 /* Used while emitting the prologue to generate code to load the correct value
917 into the PIC register, which is passed in DEST. */
919 static rtx
920 bfin_load_pic_reg (rtx dest)
922 struct cgraph_local_info *i = NULL;
923 rtx addr, insn;
925 if (flag_unit_at_a_time)
926 i = cgraph_local_info (current_function_decl);
928 /* Functions local to the translation unit don't need to reload the
929 pic reg, since the caller always passes a usable one. */
930 if (i && i->local)
931 return pic_offset_table_rtx;
933 if (bfin_lib_id_given)
934 addr = plus_constant (pic_offset_table_rtx, -4 - bfin_library_id * 4);
935 else
936 addr = gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
937 gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
938 UNSPEC_LIBRARY_OFFSET));
939 insn = emit_insn (gen_movsi (dest, gen_rtx_MEM (Pmode, addr)));
940 return dest;
943 /* Generate RTL for the prologue of the current function. */
945 void
946 bfin_expand_prologue (void)
948 HOST_WIDE_INT frame_size = get_frame_size ();
949 rtx spreg = gen_rtx_REG (Pmode, REG_SP);
950 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
951 rtx pic_reg_loaded = NULL_RTX;
953 if (fkind != SUBROUTINE)
955 expand_interrupt_handler_prologue (spreg, fkind);
956 return;
959 if (current_function_limit_stack
960 || TARGET_STACK_CHECK_L1)
962 HOST_WIDE_INT offset
963 = bfin_initial_elimination_offset (ARG_POINTER_REGNUM,
964 STACK_POINTER_REGNUM);
965 rtx lim = current_function_limit_stack ? stack_limit_rtx : NULL_RTX;
966 rtx p2reg = gen_rtx_REG (Pmode, REG_P2);
968 if (!lim)
970 emit_move_insn (p2reg, gen_int_mode (0xFFB00000, SImode));
971 emit_move_insn (p2reg, gen_rtx_MEM (Pmode, p2reg));
972 lim = p2reg;
974 if (GET_CODE (lim) == SYMBOL_REF)
976 if (TARGET_ID_SHARED_LIBRARY)
978 rtx p1reg = gen_rtx_REG (Pmode, REG_P1);
979 rtx val;
980 pic_reg_loaded = bfin_load_pic_reg (p2reg);
981 val = legitimize_pic_address (stack_limit_rtx, p1reg,
982 pic_reg_loaded);
983 emit_move_insn (p1reg, val);
984 frame_related_constant_load (p2reg, offset, FALSE);
985 emit_insn (gen_addsi3 (p2reg, p2reg, p1reg));
986 lim = p2reg;
988 else
990 rtx limit = plus_constant (lim, offset);
991 emit_move_insn (p2reg, limit);
992 lim = p2reg;
995 else
997 if (lim != p2reg)
998 emit_move_insn (p2reg, lim);
999 add_to_reg (p2reg, offset, 0, 0);
1000 lim = p2reg;
1002 emit_insn (gen_compare_lt (bfin_cc_rtx, spreg, lim));
1003 emit_insn (gen_trapifcc ());
1005 expand_prologue_reg_save (spreg, 0, false);
1007 do_link (spreg, frame_size, false);
1009 if (TARGET_ID_SHARED_LIBRARY
1010 && !TARGET_SEP_DATA
1011 && (current_function_uses_pic_offset_table
1012 || !current_function_is_leaf))
1013 bfin_load_pic_reg (pic_offset_table_rtx);
1016 /* Generate RTL for the epilogue of the current function. NEED_RETURN is zero
1017 if this is for a sibcall. EH_RETURN is nonzero if we're expanding an
1018 eh_return pattern. SIBCALL_P is true if this is a sibcall epilogue,
1019 false otherwise. */
1021 void
1022 bfin_expand_epilogue (int need_return, int eh_return, bool sibcall_p)
1024 rtx spreg = gen_rtx_REG (Pmode, REG_SP);
1025 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
1026 int e = sibcall_p ? -1 : 1;
1028 if (fkind != SUBROUTINE)
1030 expand_interrupt_handler_epilogue (spreg, fkind);
1031 return;
1034 do_unlink (spreg, get_frame_size (), false, e);
1036 expand_epilogue_reg_restore (spreg, false, false);
1038 /* Omit the return insn if this is for a sibcall. */
1039 if (! need_return)
1040 return;
1042 if (eh_return)
1043 emit_insn (gen_addsi3 (spreg, spreg, gen_rtx_REG (Pmode, REG_P2)));
1045 emit_jump_insn (gen_return_internal (GEN_INT (SUBROUTINE)));
1048 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
1051 bfin_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
1052 unsigned int new_reg)
1054 /* Interrupt functions can only use registers that have already been
1055 saved by the prologue, even if they would normally be
1056 call-clobbered. */
1058 if (funkind (TREE_TYPE (current_function_decl)) != SUBROUTINE
1059 && !df_regs_ever_live_p (new_reg))
1060 return 0;
1062 return 1;
1065 /* Return the value of the return address for the frame COUNT steps up
1066 from the current frame, after the prologue.
1067 We punt for everything but the current frame by returning const0_rtx. */
1070 bfin_return_addr_rtx (int count)
1072 if (count != 0)
1073 return const0_rtx;
1075 return get_hard_reg_initial_val (Pmode, REG_RETS);
1078 /* Try machine-dependent ways of modifying an illegitimate address X
1079 to be legitimate. If we find one, return the new, valid address,
1080 otherwise return NULL_RTX.
1082 OLDX is the address as it was before break_out_memory_refs was called.
1083 In some cases it is useful to look at this to decide what needs to be done.
1085 MODE is the mode of the memory reference. */
1088 legitimize_address (rtx x ATTRIBUTE_UNUSED, rtx oldx ATTRIBUTE_UNUSED,
1089 enum machine_mode mode ATTRIBUTE_UNUSED)
1091 return NULL_RTX;
1094 static rtx
1095 bfin_delegitimize_address (rtx orig_x)
1097 rtx x = orig_x;
1099 if (GET_CODE (x) != MEM)
1100 return orig_x;
1102 x = XEXP (x, 0);
1103 if (GET_CODE (x) == PLUS
1104 && GET_CODE (XEXP (x, 1)) == UNSPEC
1105 && XINT (XEXP (x, 1), 1) == UNSPEC_MOVE_PIC
1106 && GET_CODE (XEXP (x, 0)) == REG
1107 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
1108 return XVECEXP (XEXP (x, 1), 0, 0);
1110 return orig_x;
1113 /* This predicate is used to compute the length of a load/store insn.
1114 OP is a MEM rtx, we return nonzero if its addressing mode requires a
1115 32-bit instruction. */
1118 effective_address_32bit_p (rtx op, enum machine_mode mode)
1120 HOST_WIDE_INT offset;
1122 mode = GET_MODE (op);
1123 op = XEXP (op, 0);
1125 if (GET_CODE (op) != PLUS)
1127 gcc_assert (REG_P (op) || GET_CODE (op) == POST_INC
1128 || GET_CODE (op) == PRE_DEC || GET_CODE (op) == POST_DEC);
1129 return 0;
1132 if (GET_CODE (XEXP (op, 1)) == UNSPEC)
1133 return 1;
1135 offset = INTVAL (XEXP (op, 1));
1137 /* All byte loads use a 16-bit offset. */
1138 if (GET_MODE_SIZE (mode) == 1)
1139 return 1;
1141 if (GET_MODE_SIZE (mode) == 4)
1143 /* Frame pointer relative loads can use a negative offset, all others
1144 are restricted to a small positive one. */
1145 if (XEXP (op, 0) == frame_pointer_rtx)
1146 return offset < -128 || offset > 60;
1147 return offset < 0 || offset > 60;
1150 /* Must be HImode now. */
1151 return offset < 0 || offset > 30;
1154 /* Returns true if X is a memory reference using an I register. */
1155 bool
1156 bfin_dsp_memref_p (rtx x)
1158 if (! MEM_P (x))
1159 return false;
1160 x = XEXP (x, 0);
1161 if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_INC
1162 || GET_CODE (x) == POST_DEC || GET_CODE (x) == PRE_DEC)
1163 x = XEXP (x, 0);
1164 return IREG_P (x);
1167 /* Return cost of the memory address ADDR.
1168 All addressing modes are equally cheap on the Blackfin. */
1170 static int
1171 bfin_address_cost (rtx addr ATTRIBUTE_UNUSED)
1173 return 1;
1176 /* Subroutine of print_operand; used to print a memory reference X to FILE. */
1178 void
1179 print_address_operand (FILE *file, rtx x)
1181 switch (GET_CODE (x))
1183 case PLUS:
1184 output_address (XEXP (x, 0));
1185 fprintf (file, "+");
1186 output_address (XEXP (x, 1));
1187 break;
1189 case PRE_DEC:
1190 fprintf (file, "--");
1191 output_address (XEXP (x, 0));
1192 break;
1193 case POST_INC:
1194 output_address (XEXP (x, 0));
1195 fprintf (file, "++");
1196 break;
1197 case POST_DEC:
1198 output_address (XEXP (x, 0));
1199 fprintf (file, "--");
1200 break;
1202 default:
1203 gcc_assert (GET_CODE (x) != MEM);
1204 print_operand (file, x, 0);
1205 break;
1209 /* Adding intp DImode support by Tony
1210 * -- Q: (low word)
1211 * -- R: (high word)
1214 void
1215 print_operand (FILE *file, rtx x, char code)
1217 enum machine_mode mode;
1219 if (code == '!')
1221 if (GET_MODE (current_output_insn) == SImode)
1222 fprintf (file, " ||");
1223 else
1224 fprintf (file, ";");
1225 return;
1228 mode = GET_MODE (x);
1230 switch (code)
1232 case 'j':
1233 switch (GET_CODE (x))
1235 case EQ:
1236 fprintf (file, "e");
1237 break;
1238 case NE:
1239 fprintf (file, "ne");
1240 break;
1241 case GT:
1242 fprintf (file, "g");
1243 break;
1244 case LT:
1245 fprintf (file, "l");
1246 break;
1247 case GE:
1248 fprintf (file, "ge");
1249 break;
1250 case LE:
1251 fprintf (file, "le");
1252 break;
1253 case GTU:
1254 fprintf (file, "g");
1255 break;
1256 case LTU:
1257 fprintf (file, "l");
1258 break;
1259 case GEU:
1260 fprintf (file, "ge");
1261 break;
1262 case LEU:
1263 fprintf (file, "le");
1264 break;
1265 default:
1266 output_operand_lossage ("invalid %%j value");
1268 break;
1270 case 'J': /* reverse logic */
1271 switch (GET_CODE(x))
1273 case EQ:
1274 fprintf (file, "ne");
1275 break;
1276 case NE:
1277 fprintf (file, "e");
1278 break;
1279 case GT:
1280 fprintf (file, "le");
1281 break;
1282 case LT:
1283 fprintf (file, "ge");
1284 break;
1285 case GE:
1286 fprintf (file, "l");
1287 break;
1288 case LE:
1289 fprintf (file, "g");
1290 break;
1291 case GTU:
1292 fprintf (file, "le");
1293 break;
1294 case LTU:
1295 fprintf (file, "ge");
1296 break;
1297 case GEU:
1298 fprintf (file, "l");
1299 break;
1300 case LEU:
1301 fprintf (file, "g");
1302 break;
1303 default:
1304 output_operand_lossage ("invalid %%J value");
1306 break;
1308 default:
1309 switch (GET_CODE (x))
1311 case REG:
1312 if (code == 'h')
1314 if (REGNO (x) < 32)
1315 fprintf (file, "%s", short_reg_names[REGNO (x)]);
1316 else
1317 output_operand_lossage ("invalid operand for code '%c'", code);
1319 else if (code == 'd')
1321 if (REGNO (x) < 32)
1322 fprintf (file, "%s", high_reg_names[REGNO (x)]);
1323 else
1324 output_operand_lossage ("invalid operand for code '%c'", code);
1326 else if (code == 'w')
1328 if (REGNO (x) == REG_A0 || REGNO (x) == REG_A1)
1329 fprintf (file, "%s.w", reg_names[REGNO (x)]);
1330 else
1331 output_operand_lossage ("invalid operand for code '%c'", code);
1333 else if (code == 'x')
1335 if (REGNO (x) == REG_A0 || REGNO (x) == REG_A1)
1336 fprintf (file, "%s.x", reg_names[REGNO (x)]);
1337 else
1338 output_operand_lossage ("invalid operand for code '%c'", code);
1340 else if (code == 'v')
1342 if (REGNO (x) == REG_A0)
1343 fprintf (file, "AV0");
1344 else if (REGNO (x) == REG_A1)
1345 fprintf (file, "AV1");
1346 else
1347 output_operand_lossage ("invalid operand for code '%c'", code);
1349 else if (code == 'D')
1351 if (D_REGNO_P (REGNO (x)))
1352 fprintf (file, "%s", dregs_pair_names[REGNO (x)]);
1353 else
1354 output_operand_lossage ("invalid operand for code '%c'", code);
1356 else if (code == 'H')
1358 if ((mode == DImode || mode == DFmode) && REG_P (x))
1359 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
1360 else
1361 output_operand_lossage ("invalid operand for code '%c'", code);
1363 else if (code == 'T')
1365 if (D_REGNO_P (REGNO (x)))
1366 fprintf (file, "%s", byte_reg_names[REGNO (x)]);
1367 else
1368 output_operand_lossage ("invalid operand for code '%c'", code);
1370 else
1371 fprintf (file, "%s", reg_names[REGNO (x)]);
1372 break;
1374 case MEM:
1375 fputc ('[', file);
1376 x = XEXP (x,0);
1377 print_address_operand (file, x);
1378 fputc (']', file);
1379 break;
1381 case CONST_INT:
1382 if (code == 'M')
1384 switch (INTVAL (x))
1386 case MACFLAG_NONE:
1387 break;
1388 case MACFLAG_FU:
1389 fputs ("(FU)", file);
1390 break;
1391 case MACFLAG_T:
1392 fputs ("(T)", file);
1393 break;
1394 case MACFLAG_TFU:
1395 fputs ("(TFU)", file);
1396 break;
1397 case MACFLAG_W32:
1398 fputs ("(W32)", file);
1399 break;
1400 case MACFLAG_IS:
1401 fputs ("(IS)", file);
1402 break;
1403 case MACFLAG_IU:
1404 fputs ("(IU)", file);
1405 break;
1406 case MACFLAG_IH:
1407 fputs ("(IH)", file);
1408 break;
1409 case MACFLAG_M:
1410 fputs ("(M)", file);
1411 break;
1412 case MACFLAG_IS_M:
1413 fputs ("(IS,M)", file);
1414 break;
1415 case MACFLAG_ISS2:
1416 fputs ("(ISS2)", file);
1417 break;
1418 case MACFLAG_S2RND:
1419 fputs ("(S2RND)", file);
1420 break;
1421 default:
1422 gcc_unreachable ();
1424 break;
1426 else if (code == 'b')
1428 if (INTVAL (x) == 0)
1429 fputs ("+=", file);
1430 else if (INTVAL (x) == 1)
1431 fputs ("-=", file);
1432 else
1433 gcc_unreachable ();
1434 break;
1436 /* Moves to half registers with d or h modifiers always use unsigned
1437 constants. */
1438 else if (code == 'd')
1439 x = GEN_INT ((INTVAL (x) >> 16) & 0xffff);
1440 else if (code == 'h')
1441 x = GEN_INT (INTVAL (x) & 0xffff);
1442 else if (code == 'N')
1443 x = GEN_INT (-INTVAL (x));
1444 else if (code == 'X')
1445 x = GEN_INT (exact_log2 (0xffffffff & INTVAL (x)));
1446 else if (code == 'Y')
1447 x = GEN_INT (exact_log2 (0xffffffff & ~INTVAL (x)));
1448 else if (code == 'Z')
1449 /* Used for LINK insns. */
1450 x = GEN_INT (-8 - INTVAL (x));
1452 /* fall through */
1454 case SYMBOL_REF:
1455 output_addr_const (file, x);
1456 break;
1458 case CONST_DOUBLE:
1459 output_operand_lossage ("invalid const_double operand");
1460 break;
1462 case UNSPEC:
1463 switch (XINT (x, 1))
1465 case UNSPEC_MOVE_PIC:
1466 output_addr_const (file, XVECEXP (x, 0, 0));
1467 fprintf (file, "@GOT");
1468 break;
1470 case UNSPEC_MOVE_FDPIC:
1471 output_addr_const (file, XVECEXP (x, 0, 0));
1472 fprintf (file, "@GOT17M4");
1473 break;
1475 case UNSPEC_FUNCDESC_GOT17M4:
1476 output_addr_const (file, XVECEXP (x, 0, 0));
1477 fprintf (file, "@FUNCDESC_GOT17M4");
1478 break;
1480 case UNSPEC_LIBRARY_OFFSET:
1481 fprintf (file, "_current_shared_library_p5_offset_");
1482 break;
1484 default:
1485 gcc_unreachable ();
1487 break;
1489 default:
1490 output_addr_const (file, x);
1495 /* Argument support functions. */
1497 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1498 for a call to a function whose data type is FNTYPE.
1499 For a library call, FNTYPE is 0.
1500 VDSP C Compiler manual, our ABI says that
1501 first 3 words of arguments will use R0, R1 and R2.
1504 void
1505 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
1506 rtx libname ATTRIBUTE_UNUSED)
1508 static CUMULATIVE_ARGS zero_cum;
1510 *cum = zero_cum;
1512 /* Set up the number of registers to use for passing arguments. */
1514 cum->nregs = max_arg_registers;
1515 cum->arg_regs = arg_regs;
1517 cum->call_cookie = CALL_NORMAL;
1518 /* Check for a longcall attribute. */
1519 if (fntype && lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
1520 cum->call_cookie |= CALL_SHORT;
1521 else if (fntype && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype)))
1522 cum->call_cookie |= CALL_LONG;
1524 return;
1527 /* Update the data in CUM to advance over an argument
1528 of mode MODE and data type TYPE.
1529 (TYPE is null for libcalls where that information may not be available.) */
1531 void
1532 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
1533 int named ATTRIBUTE_UNUSED)
1535 int count, bytes, words;
1537 bytes = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
1538 words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1540 cum->words += words;
1541 cum->nregs -= words;
1543 if (cum->nregs <= 0)
1545 cum->nregs = 0;
1546 cum->arg_regs = NULL;
1548 else
1550 for (count = 1; count <= words; count++)
1551 cum->arg_regs++;
1554 return;
1557 /* Define where to put the arguments to a function.
1558 Value is zero to push the argument on the stack,
1559 or a hard register in which to store the argument.
1561 MODE is the argument's machine mode.
1562 TYPE is the data type of the argument (as a tree).
1563 This is null for libcalls where that information may
1564 not be available.
1565 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1566 the preceding args and about the function being called.
1567 NAMED is nonzero if this argument is a named parameter
1568 (otherwise it is an extra parameter matching an ellipsis). */
1570 struct rtx_def *
1571 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
1572 int named ATTRIBUTE_UNUSED)
1574 int bytes
1575 = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
1577 if (mode == VOIDmode)
1578 /* Compute operand 2 of the call insn. */
1579 return GEN_INT (cum->call_cookie);
1581 if (bytes == -1)
1582 return NULL_RTX;
1584 if (cum->nregs)
1585 return gen_rtx_REG (mode, *(cum->arg_regs));
1587 return NULL_RTX;
1590 /* For an arg passed partly in registers and partly in memory,
1591 this is the number of bytes passed in registers.
1592 For args passed entirely in registers or entirely in memory, zero.
1594 Refer VDSP C Compiler manual, our ABI.
1595 First 3 words are in registers. So, if an argument is larger
1596 than the registers available, it will span the register and
1597 stack. */
1599 static int
1600 bfin_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1601 tree type ATTRIBUTE_UNUSED,
1602 bool named ATTRIBUTE_UNUSED)
1604 int bytes
1605 = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
1606 int bytes_left = cum->nregs * UNITS_PER_WORD;
1608 if (bytes == -1)
1609 return 0;
1611 if (bytes_left == 0)
1612 return 0;
1613 if (bytes > bytes_left)
1614 return bytes_left;
1615 return 0;
1618 /* Variable sized types are passed by reference. */
1620 static bool
1621 bfin_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
1622 enum machine_mode mode ATTRIBUTE_UNUSED,
1623 const_tree type, bool named ATTRIBUTE_UNUSED)
1625 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
1628 /* Decide whether a type should be returned in memory (true)
1629 or in a register (false). This is called by the macro
1630 RETURN_IN_MEMORY. */
1633 bfin_return_in_memory (const_tree type)
1635 int size = int_size_in_bytes (type);
1636 return size > 2 * UNITS_PER_WORD || size == -1;
1639 /* Register in which address to store a structure value
1640 is passed to a function. */
1641 static rtx
1642 bfin_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
1643 int incoming ATTRIBUTE_UNUSED)
1645 return gen_rtx_REG (Pmode, REG_P0);
1648 /* Return true when register may be used to pass function parameters. */
1650 bool
1651 function_arg_regno_p (int n)
1653 int i;
1654 for (i = 0; arg_regs[i] != -1; i++)
1655 if (n == arg_regs[i])
1656 return true;
1657 return false;
1660 /* Returns 1 if OP contains a symbol reference */
1663 symbolic_reference_mentioned_p (rtx op)
1665 register const char *fmt;
1666 register int i;
1668 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
1669 return 1;
1671 fmt = GET_RTX_FORMAT (GET_CODE (op));
1672 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
1674 if (fmt[i] == 'E')
1676 register int j;
1678 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1679 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
1680 return 1;
1683 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
1684 return 1;
1687 return 0;
1690 /* Decide whether we can make a sibling call to a function. DECL is the
1691 declaration of the function being targeted by the call and EXP is the
1692 CALL_EXPR representing the call. */
1694 static bool
1695 bfin_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
1696 tree exp ATTRIBUTE_UNUSED)
1698 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
1699 if (fkind != SUBROUTINE)
1700 return false;
1701 if (!TARGET_ID_SHARED_LIBRARY || TARGET_SEP_DATA)
1702 return true;
1704 /* When compiling for ID shared libraries, can't sibcall a local function
1705 from a non-local function, because the local function thinks it does
1706 not need to reload P5 in the prologue, but the sibcall wil pop P5 in the
1707 sibcall epilogue, and we end up with the wrong value in P5. */
1709 if (!flag_unit_at_a_time || decl == NULL)
1710 /* Not enough information. */
1711 return false;
1714 struct cgraph_local_info *this_func, *called_func;
1716 this_func = cgraph_local_info (current_function_decl);
1717 called_func = cgraph_local_info (decl);
1718 return !called_func->local || this_func->local;
1722 /* Emit RTL insns to initialize the variable parts of a trampoline at
1723 TRAMP. FNADDR is an RTX for the address of the function's pure
1724 code. CXT is an RTX for the static chain value for the function. */
1726 void
1727 initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
1729 rtx t1 = copy_to_reg (fnaddr);
1730 rtx t2 = copy_to_reg (cxt);
1731 rtx addr;
1732 int i = 0;
1734 if (TARGET_FDPIC)
1736 rtx a = memory_address (Pmode, plus_constant (tramp, 8));
1737 addr = memory_address (Pmode, tramp);
1738 emit_move_insn (gen_rtx_MEM (SImode, addr), a);
1739 i = 8;
1742 addr = memory_address (Pmode, plus_constant (tramp, i + 2));
1743 emit_move_insn (gen_rtx_MEM (HImode, addr), gen_lowpart (HImode, t1));
1744 emit_insn (gen_ashrsi3 (t1, t1, GEN_INT (16)));
1745 addr = memory_address (Pmode, plus_constant (tramp, i + 6));
1746 emit_move_insn (gen_rtx_MEM (HImode, addr), gen_lowpart (HImode, t1));
1748 addr = memory_address (Pmode, plus_constant (tramp, i + 10));
1749 emit_move_insn (gen_rtx_MEM (HImode, addr), gen_lowpart (HImode, t2));
1750 emit_insn (gen_ashrsi3 (t2, t2, GEN_INT (16)));
1751 addr = memory_address (Pmode, plus_constant (tramp, i + 14));
1752 emit_move_insn (gen_rtx_MEM (HImode, addr), gen_lowpart (HImode, t2));
1755 /* Emit insns to move operands[1] into operands[0]. */
1757 void
1758 emit_pic_move (rtx *operands, enum machine_mode mode ATTRIBUTE_UNUSED)
1760 rtx temp = reload_in_progress ? operands[0] : gen_reg_rtx (Pmode);
1762 gcc_assert (!TARGET_FDPIC || !(reload_in_progress || reload_completed));
1763 if (GET_CODE (operands[0]) == MEM && SYMBOLIC_CONST (operands[1]))
1764 operands[1] = force_reg (SImode, operands[1]);
1765 else
1766 operands[1] = legitimize_pic_address (operands[1], temp,
1767 TARGET_FDPIC ? OUR_FDPIC_REG
1768 : pic_offset_table_rtx);
1771 /* Expand a move operation in mode MODE. The operands are in OPERANDS.
1772 Returns true if no further code must be generated, false if the caller
1773 should generate an insn to move OPERANDS[1] to OPERANDS[0]. */
1775 bool
1776 expand_move (rtx *operands, enum machine_mode mode)
1778 rtx op = operands[1];
1779 if ((TARGET_ID_SHARED_LIBRARY || TARGET_FDPIC)
1780 && SYMBOLIC_CONST (op))
1781 emit_pic_move (operands, mode);
1782 else if (mode == SImode && GET_CODE (op) == CONST
1783 && GET_CODE (XEXP (op, 0)) == PLUS
1784 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
1785 && !bfin_legitimate_constant_p (op))
1787 rtx dest = operands[0];
1788 rtx op0, op1;
1789 gcc_assert (!reload_in_progress && !reload_completed);
1790 op = XEXP (op, 0);
1791 op0 = force_reg (mode, XEXP (op, 0));
1792 op1 = XEXP (op, 1);
1793 if (!insn_data[CODE_FOR_addsi3].operand[2].predicate (op1, mode))
1794 op1 = force_reg (mode, op1);
1795 if (GET_CODE (dest) == MEM)
1796 dest = gen_reg_rtx (mode);
1797 emit_insn (gen_addsi3 (dest, op0, op1));
1798 if (dest == operands[0])
1799 return true;
1800 operands[1] = dest;
1802 /* Don't generate memory->memory or constant->memory moves, go through a
1803 register */
1804 else if ((reload_in_progress | reload_completed) == 0
1805 && GET_CODE (operands[0]) == MEM
1806 && GET_CODE (operands[1]) != REG)
1807 operands[1] = force_reg (mode, operands[1]);
1808 return false;
1811 /* Split one or more DImode RTL references into pairs of SImode
1812 references. The RTL can be REG, offsettable MEM, integer constant, or
1813 CONST_DOUBLE. "operands" is a pointer to an array of DImode RTL to
1814 split and "num" is its length. lo_half and hi_half are output arrays
1815 that parallel "operands". */
1817 void
1818 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
1820 while (num--)
1822 rtx op = operands[num];
1824 /* simplify_subreg refuse to split volatile memory addresses,
1825 but we still have to handle it. */
1826 if (GET_CODE (op) == MEM)
1828 lo_half[num] = adjust_address (op, SImode, 0);
1829 hi_half[num] = adjust_address (op, SImode, 4);
1831 else
1833 lo_half[num] = simplify_gen_subreg (SImode, op,
1834 GET_MODE (op) == VOIDmode
1835 ? DImode : GET_MODE (op), 0);
1836 hi_half[num] = simplify_gen_subreg (SImode, op,
1837 GET_MODE (op) == VOIDmode
1838 ? DImode : GET_MODE (op), 4);
1843 bool
1844 bfin_longcall_p (rtx op, int call_cookie)
1846 gcc_assert (GET_CODE (op) == SYMBOL_REF);
1847 if (call_cookie & CALL_SHORT)
1848 return 0;
1849 if (call_cookie & CALL_LONG)
1850 return 1;
1851 if (TARGET_LONG_CALLS)
1852 return 1;
1853 return 0;
1856 /* Expand a call instruction. FNADDR is the call target, RETVAL the return value.
1857 COOKIE is a CONST_INT holding the call_cookie prepared init_cumulative_args.
1858 SIBCALL is nonzero if this is a sibling call. */
1860 void
1861 bfin_expand_call (rtx retval, rtx fnaddr, rtx callarg1, rtx cookie, int sibcall)
1863 rtx use = NULL, call;
1864 rtx callee = XEXP (fnaddr, 0);
1865 int nelts = 2 + !!sibcall;
1866 rtx pat;
1867 rtx picreg = get_hard_reg_initial_val (SImode, FDPIC_REGNO);
1868 int n;
1870 /* In an untyped call, we can get NULL for operand 2. */
1871 if (cookie == NULL_RTX)
1872 cookie = const0_rtx;
1874 /* Static functions and indirect calls don't need the pic register. */
1875 if (!TARGET_FDPIC && flag_pic
1876 && GET_CODE (callee) == SYMBOL_REF
1877 && !SYMBOL_REF_LOCAL_P (callee))
1878 use_reg (&use, pic_offset_table_rtx);
1880 if (TARGET_FDPIC)
1882 int caller_has_l1_text, callee_has_l1_text;
1884 caller_has_l1_text = callee_has_l1_text = 0;
1886 if (lookup_attribute ("l1_text",
1887 DECL_ATTRIBUTES (cfun->decl)) != NULL_TREE)
1888 caller_has_l1_text = 1;
1890 if (GET_CODE (callee) == SYMBOL_REF
1891 && SYMBOL_REF_DECL (callee) && DECL_P (SYMBOL_REF_DECL (callee))
1892 && lookup_attribute
1893 ("l1_text",
1894 DECL_ATTRIBUTES (SYMBOL_REF_DECL (callee))) != NULL_TREE)
1895 callee_has_l1_text = 1;
1897 if (GET_CODE (callee) != SYMBOL_REF
1898 || bfin_longcall_p (callee, INTVAL (cookie))
1899 || (GET_CODE (callee) == SYMBOL_REF
1900 && !SYMBOL_REF_LOCAL_P (callee)
1901 && TARGET_INLINE_PLT)
1902 || caller_has_l1_text != callee_has_l1_text
1903 || (caller_has_l1_text && callee_has_l1_text
1904 && (GET_CODE (callee) != SYMBOL_REF
1905 || !SYMBOL_REF_LOCAL_P (callee))))
1907 rtx addr = callee;
1908 if (! address_operand (addr, Pmode))
1909 addr = force_reg (Pmode, addr);
1911 fnaddr = gen_reg_rtx (SImode);
1912 emit_insn (gen_load_funcdescsi (fnaddr, addr));
1913 fnaddr = gen_rtx_MEM (Pmode, fnaddr);
1915 picreg = gen_reg_rtx (SImode);
1916 emit_insn (gen_load_funcdescsi (picreg,
1917 plus_constant (addr, 4)));
1920 nelts++;
1922 else if ((!register_no_elim_operand (callee, Pmode)
1923 && GET_CODE (callee) != SYMBOL_REF)
1924 || (GET_CODE (callee) == SYMBOL_REF
1925 && ((TARGET_ID_SHARED_LIBRARY && !TARGET_LEAF_ID_SHARED_LIBRARY)
1926 || bfin_longcall_p (callee, INTVAL (cookie)))))
1928 callee = copy_to_mode_reg (Pmode, callee);
1929 fnaddr = gen_rtx_MEM (Pmode, callee);
1931 call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
1933 if (retval)
1934 call = gen_rtx_SET (VOIDmode, retval, call);
1936 pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nelts));
1937 n = 0;
1938 XVECEXP (pat, 0, n++) = call;
1939 if (TARGET_FDPIC)
1940 XVECEXP (pat, 0, n++) = gen_rtx_USE (VOIDmode, picreg);
1941 XVECEXP (pat, 0, n++) = gen_rtx_USE (VOIDmode, cookie);
1942 if (sibcall)
1943 XVECEXP (pat, 0, n++) = gen_rtx_RETURN (VOIDmode);
1944 call = emit_call_insn (pat);
1945 if (use)
1946 CALL_INSN_FUNCTION_USAGE (call) = use;
1949 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
1952 hard_regno_mode_ok (int regno, enum machine_mode mode)
1954 /* Allow only dregs to store value of mode HI or QI */
1955 enum reg_class class = REGNO_REG_CLASS (regno);
1957 if (mode == CCmode)
1958 return 0;
1960 if (mode == V2HImode)
1961 return D_REGNO_P (regno);
1962 if (class == CCREGS)
1963 return mode == BImode;
1964 if (mode == PDImode || mode == V2PDImode)
1965 return regno == REG_A0 || regno == REG_A1;
1967 /* Allow all normal 32-bit regs, except REG_M3, in case regclass ever comes
1968 up with a bad register class (such as ALL_REGS) for DImode. */
1969 if (mode == DImode)
1970 return regno < REG_M3;
1972 if (mode == SImode
1973 && TEST_HARD_REG_BIT (reg_class_contents[PROLOGUE_REGS], regno))
1974 return 1;
1976 return TEST_HARD_REG_BIT (reg_class_contents[MOST_REGS], regno);
1979 /* Implements target hook vector_mode_supported_p. */
1981 static bool
1982 bfin_vector_mode_supported_p (enum machine_mode mode)
1984 return mode == V2HImode;
1987 /* Return the cost of moving data from a register in class CLASS1 to
1988 one in class CLASS2. A cost of 2 is the default. */
1991 bfin_register_move_cost (enum machine_mode mode,
1992 enum reg_class class1, enum reg_class class2)
1994 /* These need secondary reloads, so they're more expensive. */
1995 if ((class1 == CCREGS && class2 != DREGS)
1996 || (class1 != DREGS && class2 == CCREGS))
1997 return 4;
1999 /* If optimizing for size, always prefer reg-reg over reg-memory moves. */
2000 if (optimize_size)
2001 return 2;
2003 /* There are some stalls involved when moving from a DREG to a different
2004 class reg, and using the value in one of the following instructions.
2005 Attempt to model this by slightly discouraging such moves. */
2006 if (class1 == DREGS && class2 != DREGS)
2007 return 2 * 2;
2009 if (GET_MODE_CLASS (mode) == MODE_INT)
2011 /* Discourage trying to use the accumulators. */
2012 if (TEST_HARD_REG_BIT (reg_class_contents[class1], REG_A0)
2013 || TEST_HARD_REG_BIT (reg_class_contents[class1], REG_A1)
2014 || TEST_HARD_REG_BIT (reg_class_contents[class2], REG_A0)
2015 || TEST_HARD_REG_BIT (reg_class_contents[class2], REG_A1))
2016 return 20;
2018 return 2;
2021 /* Return the cost of moving data of mode M between a
2022 register and memory. A value of 2 is the default; this cost is
2023 relative to those in `REGISTER_MOVE_COST'.
2025 ??? In theory L1 memory has single-cycle latency. We should add a switch
2026 that tells the compiler whether we expect to use only L1 memory for the
2027 program; it'll make the costs more accurate. */
2030 bfin_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
2031 enum reg_class class,
2032 int in ATTRIBUTE_UNUSED)
2034 /* Make memory accesses slightly more expensive than any register-register
2035 move. Also, penalize non-DP registers, since they need secondary
2036 reloads to load and store. */
2037 if (! reg_class_subset_p (class, DPREGS))
2038 return 10;
2040 return 8;
2043 /* Inform reload about cases where moving X with a mode MODE to a register in
2044 CLASS requires an extra scratch register. Return the class needed for the
2045 scratch register. */
2047 static enum reg_class
2048 bfin_secondary_reload (bool in_p, rtx x, enum reg_class class,
2049 enum machine_mode mode, secondary_reload_info *sri)
2051 /* If we have HImode or QImode, we can only use DREGS as secondary registers;
2052 in most other cases we can also use PREGS. */
2053 enum reg_class default_class = GET_MODE_SIZE (mode) >= 4 ? DPREGS : DREGS;
2054 enum reg_class x_class = NO_REGS;
2055 enum rtx_code code = GET_CODE (x);
2057 if (code == SUBREG)
2058 x = SUBREG_REG (x), code = GET_CODE (x);
2059 if (REG_P (x))
2061 int regno = REGNO (x);
2062 if (regno >= FIRST_PSEUDO_REGISTER)
2063 regno = reg_renumber[regno];
2065 if (regno == -1)
2066 code = MEM;
2067 else
2068 x_class = REGNO_REG_CLASS (regno);
2071 /* We can be asked to reload (plus (FP) (large_constant)) into a DREG.
2072 This happens as a side effect of register elimination, and we need
2073 a scratch register to do it. */
2074 if (fp_plus_const_operand (x, mode))
2076 rtx op2 = XEXP (x, 1);
2077 int large_constant_p = ! CONST_7BIT_IMM_P (INTVAL (op2));
2079 if (class == PREGS || class == PREGS_CLOBBERED)
2080 return NO_REGS;
2081 /* If destination is a DREG, we can do this without a scratch register
2082 if the constant is valid for an add instruction. */
2083 if ((class == DREGS || class == DPREGS)
2084 && ! large_constant_p)
2085 return NO_REGS;
2086 /* Reloading to anything other than a DREG? Use a PREG scratch
2087 register. */
2088 sri->icode = CODE_FOR_reload_insi;
2089 return NO_REGS;
2092 /* Data can usually be moved freely between registers of most classes.
2093 AREGS are an exception; they can only move to or from another register
2094 in AREGS or one in DREGS. They can also be assigned the constant 0. */
2095 if (x_class == AREGS || x_class == EVEN_AREGS || x_class == ODD_AREGS)
2096 return (class == DREGS || class == AREGS || class == EVEN_AREGS
2097 || class == ODD_AREGS
2098 ? NO_REGS : DREGS);
2100 if (class == AREGS || class == EVEN_AREGS || class == ODD_AREGS)
2102 if (code == MEM)
2104 sri->icode = in_p ? CODE_FOR_reload_inpdi : CODE_FOR_reload_outpdi;
2105 return NO_REGS;
2108 if (x != const0_rtx && x_class != DREGS)
2110 return DREGS;
2112 else
2113 return NO_REGS;
2116 /* CCREGS can only be moved from/to DREGS. */
2117 if (class == CCREGS && x_class != DREGS)
2118 return DREGS;
2119 if (x_class == CCREGS && class != DREGS)
2120 return DREGS;
2122 /* All registers other than AREGS can load arbitrary constants. The only
2123 case that remains is MEM. */
2124 if (code == MEM)
2125 if (! reg_class_subset_p (class, default_class))
2126 return default_class;
2128 return NO_REGS;
2131 /* Implement TARGET_HANDLE_OPTION. */
2133 static bool
2134 bfin_handle_option (size_t code, const char *arg, int value)
2136 switch (code)
2138 case OPT_mshared_library_id_:
2139 if (value > MAX_LIBRARY_ID)
2140 error ("-mshared-library-id=%s is not between 0 and %d",
2141 arg, MAX_LIBRARY_ID);
2142 bfin_lib_id_given = 1;
2143 return true;
2145 case OPT_mcpu_:
2146 if (strcmp (arg, "bf531") == 0)
2147 bfin_cpu_type = BFIN_CPU_BF531;
2148 else if (strcmp (arg, "bf532") == 0)
2149 bfin_cpu_type = BFIN_CPU_BF532;
2150 else if (strcmp (arg, "bf533") == 0)
2151 bfin_cpu_type = BFIN_CPU_BF533;
2152 else if (strcmp (arg, "bf534") == 0)
2153 bfin_cpu_type = BFIN_CPU_BF534;
2154 else if (strcmp (arg, "bf536") == 0)
2155 bfin_cpu_type = BFIN_CPU_BF536;
2156 else if (strcmp (arg, "bf537") == 0)
2157 bfin_cpu_type = BFIN_CPU_BF537;
2158 else if (strcmp (arg, "bf561") == 0)
2160 warning (0, "bf561 support is incomplete yet.");
2161 bfin_cpu_type = BFIN_CPU_BF561;
2163 else
2164 return false;
2165 return true;
2167 default:
2168 return true;
2172 static struct machine_function *
2173 bfin_init_machine_status (void)
2175 struct machine_function *f;
2177 f = ggc_alloc_cleared (sizeof (struct machine_function));
2179 return f;
2182 /* Implement the macro OVERRIDE_OPTIONS. */
2184 void
2185 override_options (void)
2187 if (TARGET_OMIT_LEAF_FRAME_POINTER)
2188 flag_omit_frame_pointer = 1;
2190 /* Library identification */
2191 if (bfin_lib_id_given && ! TARGET_ID_SHARED_LIBRARY)
2192 error ("-mshared-library-id= specified without -mid-shared-library");
2194 if (TARGET_ID_SHARED_LIBRARY && flag_pic == 0)
2195 flag_pic = 1;
2197 if (stack_limit_rtx && TARGET_STACK_CHECK_L1)
2198 error ("Can't use multiple stack checking methods together.");
2200 if (TARGET_ID_SHARED_LIBRARY && TARGET_FDPIC)
2201 error ("ID shared libraries and FD-PIC mode can't be used together.");
2203 /* Don't allow the user to specify -mid-shared-library and -msep-data
2204 together, as it makes little sense from a user's point of view... */
2205 if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
2206 error ("cannot specify both -msep-data and -mid-shared-library");
2207 /* ... internally, however, it's nearly the same. */
2208 if (TARGET_SEP_DATA)
2209 target_flags |= MASK_ID_SHARED_LIBRARY | MASK_LEAF_ID_SHARED_LIBRARY;
2211 /* There is no single unaligned SI op for PIC code. Sometimes we
2212 need to use ".4byte" and sometimes we need to use ".picptr".
2213 See bfin_assemble_integer for details. */
2214 if (TARGET_FDPIC)
2215 targetm.asm_out.unaligned_op.si = 0;
2217 /* Silently turn off flag_pic if not doing FDPIC or ID shared libraries,
2218 since we don't support it and it'll just break. */
2219 if (flag_pic && !TARGET_FDPIC && !TARGET_ID_SHARED_LIBRARY)
2220 flag_pic = 0;
2222 flag_schedule_insns = 0;
2224 /* Passes after sched2 can break the helpful TImode annotations that
2225 haifa-sched puts on every insn. Just do scheduling in reorg. */
2226 bfin_flag_schedule_insns2 = flag_schedule_insns_after_reload;
2227 flag_schedule_insns_after_reload = 0;
2229 init_machine_status = bfin_init_machine_status;
2232 /* Return the destination address of BRANCH.
2233 We need to use this instead of get_attr_length, because the
2234 cbranch_with_nops pattern conservatively sets its length to 6, and
2235 we still prefer to use shorter sequences. */
2237 static int
2238 branch_dest (rtx branch)
2240 rtx dest;
2241 int dest_uid;
2242 rtx pat = PATTERN (branch);
2243 if (GET_CODE (pat) == PARALLEL)
2244 pat = XVECEXP (pat, 0, 0);
2245 dest = SET_SRC (pat);
2246 if (GET_CODE (dest) == IF_THEN_ELSE)
2247 dest = XEXP (dest, 1);
2248 dest = XEXP (dest, 0);
2249 dest_uid = INSN_UID (dest);
2250 return INSN_ADDRESSES (dest_uid);
2253 /* Return nonzero if INSN is annotated with a REG_BR_PROB note that indicates
2254 it's a branch that's predicted taken. */
2256 static int
2257 cbranch_predicted_taken_p (rtx insn)
2259 rtx x = find_reg_note (insn, REG_BR_PROB, 0);
2261 if (x)
2263 int pred_val = INTVAL (XEXP (x, 0));
2265 return pred_val >= REG_BR_PROB_BASE / 2;
2268 return 0;
2271 /* Templates for use by asm_conditional_branch. */
2273 static const char *ccbranch_templates[][3] = {
2274 { "if !cc jump %3;", "if cc jump 4 (bp); jump.s %3;", "if cc jump 6 (bp); jump.l %3;" },
2275 { "if cc jump %3;", "if !cc jump 4 (bp); jump.s %3;", "if !cc jump 6 (bp); jump.l %3;" },
2276 { "if !cc jump %3 (bp);", "if cc jump 4; jump.s %3;", "if cc jump 6; jump.l %3;" },
2277 { "if cc jump %3 (bp);", "if !cc jump 4; jump.s %3;", "if !cc jump 6; jump.l %3;" },
2280 /* Output INSN, which is a conditional branch instruction with operands
2281 OPERANDS.
2283 We deal with the various forms of conditional branches that can be generated
2284 by bfin_reorg to prevent the hardware from doing speculative loads, by
2285 - emitting a sufficient number of nops, if N_NOPS is nonzero, or
2286 - always emitting the branch as predicted taken, if PREDICT_TAKEN is true.
2287 Either of these is only necessary if the branch is short, otherwise the
2288 template we use ends in an unconditional jump which flushes the pipeline
2289 anyway. */
2291 void
2292 asm_conditional_branch (rtx insn, rtx *operands, int n_nops, int predict_taken)
2294 int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
2295 /* Note : offset for instructions like if cc jmp; jump.[sl] offset
2296 is to be taken from start of if cc rather than jump.
2297 Range for jump.s is (-4094, 4096) instead of (-4096, 4094)
2299 int len = (offset >= -1024 && offset <= 1022 ? 0
2300 : offset >= -4094 && offset <= 4096 ? 1
2301 : 2);
2302 int bp = predict_taken && len == 0 ? 1 : cbranch_predicted_taken_p (insn);
2303 int idx = (bp << 1) | (GET_CODE (operands[0]) == EQ ? BRF : BRT);
2304 output_asm_insn (ccbranch_templates[idx][len], operands);
2305 gcc_assert (n_nops == 0 || !bp);
2306 if (len == 0)
2307 while (n_nops-- > 0)
2308 output_asm_insn ("nop;", NULL);
2311 /* Emit rtl for a comparison operation CMP in mode MODE. Operands have been
2312 stored in bfin_compare_op0 and bfin_compare_op1 already. */
2315 bfin_gen_compare (rtx cmp, enum machine_mode mode ATTRIBUTE_UNUSED)
2317 enum rtx_code code1, code2;
2318 rtx op0 = bfin_compare_op0, op1 = bfin_compare_op1;
2319 rtx tem = bfin_cc_rtx;
2320 enum rtx_code code = GET_CODE (cmp);
2322 /* If we have a BImode input, then we already have a compare result, and
2323 do not need to emit another comparison. */
2324 if (GET_MODE (op0) == BImode)
2326 gcc_assert ((code == NE || code == EQ) && op1 == const0_rtx);
2327 tem = op0, code2 = code;
2329 else
2331 switch (code) {
2332 /* bfin has these conditions */
2333 case EQ:
2334 case LT:
2335 case LE:
2336 case LEU:
2337 case LTU:
2338 code1 = code;
2339 code2 = NE;
2340 break;
2341 default:
2342 code1 = reverse_condition (code);
2343 code2 = EQ;
2344 break;
2346 emit_insn (gen_rtx_SET (BImode, tem,
2347 gen_rtx_fmt_ee (code1, BImode, op0, op1)));
2350 return gen_rtx_fmt_ee (code2, BImode, tem, CONST0_RTX (BImode));
2353 /* Return nonzero iff C has exactly one bit set if it is interpreted
2354 as a 32-bit constant. */
2357 log2constp (unsigned HOST_WIDE_INT c)
2359 c &= 0xFFFFFFFF;
2360 return c != 0 && (c & (c-1)) == 0;
2363 /* Returns the number of consecutive least significant zeros in the binary
2364 representation of *V.
2365 We modify *V to contain the original value arithmetically shifted right by
2366 the number of zeroes. */
2368 static int
2369 shiftr_zero (HOST_WIDE_INT *v)
2371 unsigned HOST_WIDE_INT tmp = *v;
2372 unsigned HOST_WIDE_INT sgn;
2373 int n = 0;
2375 if (tmp == 0)
2376 return 0;
2378 sgn = tmp & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1));
2379 while ((tmp & 0x1) == 0 && n <= 32)
2381 tmp = (tmp >> 1) | sgn;
2382 n++;
2384 *v = tmp;
2385 return n;
2388 /* After reload, split the load of an immediate constant. OPERANDS are the
2389 operands of the movsi_insn pattern which we are splitting. We return
2390 nonzero if we emitted a sequence to load the constant, zero if we emitted
2391 nothing because we want to use the splitter's default sequence. */
2394 split_load_immediate (rtx operands[])
2396 HOST_WIDE_INT val = INTVAL (operands[1]);
2397 HOST_WIDE_INT tmp;
2398 HOST_WIDE_INT shifted = val;
2399 HOST_WIDE_INT shifted_compl = ~val;
2400 int num_zero = shiftr_zero (&shifted);
2401 int num_compl_zero = shiftr_zero (&shifted_compl);
2402 unsigned int regno = REGNO (operands[0]);
2404 /* This case takes care of single-bit set/clear constants, which we could
2405 also implement with BITSET/BITCLR. */
2406 if (num_zero
2407 && shifted >= -32768 && shifted < 65536
2408 && (D_REGNO_P (regno)
2409 || (regno >= REG_P0 && regno <= REG_P7 && num_zero <= 2)))
2411 emit_insn (gen_movsi (operands[0], GEN_INT (shifted)));
2412 emit_insn (gen_ashlsi3 (operands[0], operands[0], GEN_INT (num_zero)));
2413 return 1;
2416 tmp = val & 0xFFFF;
2417 tmp |= -(tmp & 0x8000);
2419 /* If high word has one bit set or clear, try to use a bit operation. */
2420 if (D_REGNO_P (regno))
2422 if (log2constp (val & 0xFFFF0000))
2424 emit_insn (gen_movsi (operands[0], GEN_INT (val & 0xFFFF)));
2425 emit_insn (gen_iorsi3 (operands[0], operands[0], GEN_INT (val & 0xFFFF0000)));
2426 return 1;
2428 else if (log2constp (val | 0xFFFF) && (val & 0x8000) != 0)
2430 emit_insn (gen_movsi (operands[0], GEN_INT (tmp)));
2431 emit_insn (gen_andsi3 (operands[0], operands[0], GEN_INT (val | 0xFFFF)));
2435 if (D_REGNO_P (regno))
2437 if (CONST_7BIT_IMM_P (tmp))
2439 emit_insn (gen_movsi (operands[0], GEN_INT (tmp)));
2440 emit_insn (gen_movstricthi_high (operands[0], GEN_INT (val & -65536)));
2441 return 1;
2444 if ((val & 0xFFFF0000) == 0)
2446 emit_insn (gen_movsi (operands[0], const0_rtx));
2447 emit_insn (gen_movsi_low (operands[0], operands[0], operands[1]));
2448 return 1;
2451 if ((val & 0xFFFF0000) == 0xFFFF0000)
2453 emit_insn (gen_movsi (operands[0], constm1_rtx));
2454 emit_insn (gen_movsi_low (operands[0], operands[0], operands[1]));
2455 return 1;
2459 /* Need DREGs for the remaining case. */
2460 if (regno > REG_R7)
2461 return 0;
2463 if (optimize_size
2464 && num_compl_zero && CONST_7BIT_IMM_P (shifted_compl))
2466 /* If optimizing for size, generate a sequence that has more instructions
2467 but is shorter. */
2468 emit_insn (gen_movsi (operands[0], GEN_INT (shifted_compl)));
2469 emit_insn (gen_ashlsi3 (operands[0], operands[0],
2470 GEN_INT (num_compl_zero)));
2471 emit_insn (gen_one_cmplsi2 (operands[0], operands[0]));
2472 return 1;
2474 return 0;
2477 /* Return true if the legitimate memory address for a memory operand of mode
2478 MODE. Return false if not. */
2480 static bool
2481 bfin_valid_add (enum machine_mode mode, HOST_WIDE_INT value)
2483 unsigned HOST_WIDE_INT v = value > 0 ? value : -value;
2484 int sz = GET_MODE_SIZE (mode);
2485 int shift = sz == 1 ? 0 : sz == 2 ? 1 : 2;
2486 /* The usual offsettable_memref machinery doesn't work so well for this
2487 port, so we deal with the problem here. */
2488 if (value > 0 && sz == 8)
2489 v += 4;
2490 return (v & ~(0x7fff << shift)) == 0;
2493 static bool
2494 bfin_valid_reg_p (unsigned int regno, int strict, enum machine_mode mode,
2495 enum rtx_code outer_code)
2497 if (strict)
2498 return REGNO_OK_FOR_BASE_STRICT_P (regno, mode, outer_code, SCRATCH);
2499 else
2500 return REGNO_OK_FOR_BASE_NONSTRICT_P (regno, mode, outer_code, SCRATCH);
2503 bool
2504 bfin_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
2506 switch (GET_CODE (x)) {
2507 case REG:
2508 if (bfin_valid_reg_p (REGNO (x), strict, mode, MEM))
2509 return true;
2510 break;
2511 case PLUS:
2512 if (REG_P (XEXP (x, 0))
2513 && bfin_valid_reg_p (REGNO (XEXP (x, 0)), strict, mode, PLUS)
2514 && ((GET_CODE (XEXP (x, 1)) == UNSPEC && mode == SImode)
2515 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2516 && bfin_valid_add (mode, INTVAL (XEXP (x, 1))))))
2517 return true;
2518 break;
2519 case POST_INC:
2520 case POST_DEC:
2521 if (LEGITIMATE_MODE_FOR_AUTOINC_P (mode)
2522 && REG_P (XEXP (x, 0))
2523 && bfin_valid_reg_p (REGNO (XEXP (x, 0)), strict, mode, POST_INC))
2524 return true;
2525 case PRE_DEC:
2526 if (LEGITIMATE_MODE_FOR_AUTOINC_P (mode)
2527 && XEXP (x, 0) == stack_pointer_rtx
2528 && REG_P (XEXP (x, 0))
2529 && bfin_valid_reg_p (REGNO (XEXP (x, 0)), strict, mode, PRE_DEC))
2530 return true;
2531 break;
2532 default:
2533 break;
2535 return false;
2538 /* Decide whether we can force certain constants to memory. If we
2539 decide we can't, the caller should be able to cope with it in
2540 another way. */
2542 static bool
2543 bfin_cannot_force_const_mem (rtx x ATTRIBUTE_UNUSED)
2545 /* We have only one class of non-legitimate constants, and our movsi
2546 expander knows how to handle them. Dropping these constants into the
2547 data section would only shift the problem - we'd still get relocs
2548 outside the object, in the data section rather than the text section. */
2549 return true;
2552 /* Ensure that for any constant of the form symbol + offset, the offset
2553 remains within the object. Any other constants are ok.
2554 This ensures that flat binaries never have to deal with relocations
2555 crossing section boundaries. */
2557 bool
2558 bfin_legitimate_constant_p (rtx x)
2560 rtx sym;
2561 HOST_WIDE_INT offset;
2563 if (GET_CODE (x) != CONST)
2564 return true;
2566 x = XEXP (x, 0);
2567 gcc_assert (GET_CODE (x) == PLUS);
2569 sym = XEXP (x, 0);
2570 x = XEXP (x, 1);
2571 if (GET_CODE (sym) != SYMBOL_REF
2572 || GET_CODE (x) != CONST_INT)
2573 return true;
2574 offset = INTVAL (x);
2576 if (SYMBOL_REF_DECL (sym) == 0)
2577 return true;
2578 if (offset < 0
2579 || offset >= int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (sym))))
2580 return false;
2582 return true;
2585 static bool
2586 bfin_rtx_costs (rtx x, int code, int outer_code, int *total)
2588 int cost2 = COSTS_N_INSNS (1);
2589 rtx op0, op1;
2591 switch (code)
2593 case CONST_INT:
2594 if (outer_code == SET || outer_code == PLUS)
2595 *total = CONST_7BIT_IMM_P (INTVAL (x)) ? 0 : cost2;
2596 else if (outer_code == AND)
2597 *total = log2constp (~INTVAL (x)) ? 0 : cost2;
2598 else if (outer_code == LE || outer_code == LT || outer_code == EQ)
2599 *total = (INTVAL (x) >= -4 && INTVAL (x) <= 3) ? 0 : cost2;
2600 else if (outer_code == LEU || outer_code == LTU)
2601 *total = (INTVAL (x) >= 0 && INTVAL (x) <= 7) ? 0 : cost2;
2602 else if (outer_code == MULT)
2603 *total = (INTVAL (x) == 2 || INTVAL (x) == 4) ? 0 : cost2;
2604 else if (outer_code == ASHIFT && (INTVAL (x) == 1 || INTVAL (x) == 2))
2605 *total = 0;
2606 else if (outer_code == ASHIFT || outer_code == ASHIFTRT
2607 || outer_code == LSHIFTRT)
2608 *total = (INTVAL (x) >= 0 && INTVAL (x) <= 31) ? 0 : cost2;
2609 else if (outer_code == IOR || outer_code == XOR)
2610 *total = (INTVAL (x) & (INTVAL (x) - 1)) == 0 ? 0 : cost2;
2611 else
2612 *total = cost2;
2613 return true;
2615 case CONST:
2616 case LABEL_REF:
2617 case SYMBOL_REF:
2618 case CONST_DOUBLE:
2619 *total = COSTS_N_INSNS (2);
2620 return true;
2622 case PLUS:
2623 op0 = XEXP (x, 0);
2624 op1 = XEXP (x, 1);
2625 if (GET_MODE (x) == SImode)
2627 if (GET_CODE (op0) == MULT
2628 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
2630 HOST_WIDE_INT val = INTVAL (XEXP (op0, 1));
2631 if (val == 2 || val == 4)
2633 *total = cost2;
2634 *total += rtx_cost (XEXP (op0, 0), outer_code);
2635 *total += rtx_cost (op1, outer_code);
2636 return true;
2639 *total = cost2;
2640 if (GET_CODE (op0) != REG
2641 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2642 *total += rtx_cost (op0, SET);
2643 #if 0 /* We'd like to do this for accuracy, but it biases the loop optimizer
2644 towards creating too many induction variables. */
2645 if (!reg_or_7bit_operand (op1, SImode))
2646 *total += rtx_cost (op1, SET);
2647 #endif
2649 else if (GET_MODE (x) == DImode)
2651 *total = 6 * cost2;
2652 if (GET_CODE (op1) != CONST_INT
2653 || !CONST_7BIT_IMM_P (INTVAL (op1)))
2654 *total += rtx_cost (op1, PLUS);
2655 if (GET_CODE (op0) != REG
2656 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2657 *total += rtx_cost (op0, PLUS);
2659 return true;
2661 case MINUS:
2662 if (GET_MODE (x) == DImode)
2663 *total = 6 * cost2;
2664 else
2665 *total = cost2;
2666 return true;
2668 case ASHIFT:
2669 case ASHIFTRT:
2670 case LSHIFTRT:
2671 if (GET_MODE (x) == DImode)
2672 *total = 6 * cost2;
2673 else
2674 *total = cost2;
2676 op0 = XEXP (x, 0);
2677 op1 = XEXP (x, 1);
2678 if (GET_CODE (op0) != REG
2679 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2680 *total += rtx_cost (op0, code);
2682 return true;
2684 case IOR:
2685 case AND:
2686 case XOR:
2687 op0 = XEXP (x, 0);
2688 op1 = XEXP (x, 1);
2690 /* Handle special cases of IOR: rotates, ALIGN insns, movstricthi_high. */
2691 if (code == IOR)
2693 if ((GET_CODE (op0) == LSHIFTRT && GET_CODE (op1) == ASHIFT)
2694 || (GET_CODE (op0) == ASHIFT && GET_CODE (op1) == ZERO_EXTEND)
2695 || (GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
2696 || (GET_CODE (op0) == AND && GET_CODE (op1) == CONST_INT))
2698 *total = cost2;
2699 return true;
2703 if (GET_CODE (op0) != REG
2704 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2705 *total += rtx_cost (op0, code);
2707 if (GET_MODE (x) == DImode)
2709 *total = 2 * cost2;
2710 return true;
2712 *total = cost2;
2713 if (GET_MODE (x) != SImode)
2714 return true;
2716 if (code == AND)
2718 if (! rhs_andsi3_operand (XEXP (x, 1), SImode))
2719 *total += rtx_cost (XEXP (x, 1), code);
2721 else
2723 if (! regorlog2_operand (XEXP (x, 1), SImode))
2724 *total += rtx_cost (XEXP (x, 1), code);
2727 return true;
2729 case ZERO_EXTRACT:
2730 case SIGN_EXTRACT:
2731 if (outer_code == SET
2732 && XEXP (x, 1) == const1_rtx
2733 && GET_CODE (XEXP (x, 2)) == CONST_INT)
2735 *total = 2 * cost2;
2736 return true;
2738 /* fall through */
2740 case SIGN_EXTEND:
2741 case ZERO_EXTEND:
2742 *total = cost2;
2743 return true;
2745 case MULT:
2747 op0 = XEXP (x, 0);
2748 op1 = XEXP (x, 1);
2749 if (GET_CODE (op0) == GET_CODE (op1)
2750 && (GET_CODE (op0) == ZERO_EXTEND
2751 || GET_CODE (op0) == SIGN_EXTEND))
2753 *total = COSTS_N_INSNS (1);
2754 op0 = XEXP (op0, 0);
2755 op1 = XEXP (op1, 0);
2757 else if (optimize_size)
2758 *total = COSTS_N_INSNS (1);
2759 else
2760 *total = COSTS_N_INSNS (3);
2762 if (GET_CODE (op0) != REG
2763 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2764 *total += rtx_cost (op0, MULT);
2765 if (GET_CODE (op1) != REG
2766 && (GET_CODE (op1) != SUBREG || GET_CODE (SUBREG_REG (op1)) != REG))
2767 *total += rtx_cost (op1, MULT);
2769 return true;
2771 case UDIV:
2772 case UMOD:
2773 *total = COSTS_N_INSNS (32);
2774 return true;
2776 case VEC_CONCAT:
2777 case VEC_SELECT:
2778 if (outer_code == SET)
2779 *total = cost2;
2780 return true;
2782 default:
2783 return false;
2787 /* Used for communication between {push,pop}_multiple_operation (which
2788 we use not only as a predicate) and the corresponding output functions. */
2789 static int first_preg_to_save, first_dreg_to_save;
2792 push_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2794 int lastdreg = 8, lastpreg = 6;
2795 int i, group;
2797 first_preg_to_save = lastpreg;
2798 first_dreg_to_save = lastdreg;
2799 for (i = 1, group = 0; i < XVECLEN (op, 0) - 1; i++)
2801 rtx t = XVECEXP (op, 0, i);
2802 rtx src, dest;
2803 int regno;
2805 if (GET_CODE (t) != SET)
2806 return 0;
2808 src = SET_SRC (t);
2809 dest = SET_DEST (t);
2810 if (GET_CODE (dest) != MEM || ! REG_P (src))
2811 return 0;
2812 dest = XEXP (dest, 0);
2813 if (GET_CODE (dest) != PLUS
2814 || ! REG_P (XEXP (dest, 0))
2815 || REGNO (XEXP (dest, 0)) != REG_SP
2816 || GET_CODE (XEXP (dest, 1)) != CONST_INT
2817 || INTVAL (XEXP (dest, 1)) != -i * 4)
2818 return 0;
2820 regno = REGNO (src);
2821 if (group == 0)
2823 if (D_REGNO_P (regno))
2825 group = 1;
2826 first_dreg_to_save = lastdreg = regno - REG_R0;
2828 else if (regno >= REG_P0 && regno <= REG_P7)
2830 group = 2;
2831 first_preg_to_save = lastpreg = regno - REG_P0;
2833 else
2834 return 0;
2836 continue;
2839 if (group == 1)
2841 if (regno >= REG_P0 && regno <= REG_P7)
2843 group = 2;
2844 first_preg_to_save = lastpreg = regno - REG_P0;
2846 else if (regno != REG_R0 + lastdreg + 1)
2847 return 0;
2848 else
2849 lastdreg++;
2851 else if (group == 2)
2853 if (regno != REG_P0 + lastpreg + 1)
2854 return 0;
2855 lastpreg++;
2858 return 1;
2862 pop_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2864 int lastdreg = 8, lastpreg = 6;
2865 int i, group;
2867 for (i = 1, group = 0; i < XVECLEN (op, 0); i++)
2869 rtx t = XVECEXP (op, 0, i);
2870 rtx src, dest;
2871 int regno;
2873 if (GET_CODE (t) != SET)
2874 return 0;
2876 src = SET_SRC (t);
2877 dest = SET_DEST (t);
2878 if (GET_CODE (src) != MEM || ! REG_P (dest))
2879 return 0;
2880 src = XEXP (src, 0);
2882 if (i == 1)
2884 if (! REG_P (src) || REGNO (src) != REG_SP)
2885 return 0;
2887 else if (GET_CODE (src) != PLUS
2888 || ! REG_P (XEXP (src, 0))
2889 || REGNO (XEXP (src, 0)) != REG_SP
2890 || GET_CODE (XEXP (src, 1)) != CONST_INT
2891 || INTVAL (XEXP (src, 1)) != (i - 1) * 4)
2892 return 0;
2894 regno = REGNO (dest);
2895 if (group == 0)
2897 if (regno == REG_R7)
2899 group = 1;
2900 lastdreg = 7;
2902 else if (regno != REG_P0 + lastpreg - 1)
2903 return 0;
2904 else
2905 lastpreg--;
2907 else if (group == 1)
2909 if (regno != REG_R0 + lastdreg - 1)
2910 return 0;
2911 else
2912 lastdreg--;
2915 first_dreg_to_save = lastdreg;
2916 first_preg_to_save = lastpreg;
2917 return 1;
2920 /* Emit assembly code for one multi-register push described by INSN, with
2921 operands in OPERANDS. */
2923 void
2924 output_push_multiple (rtx insn, rtx *operands)
2926 char buf[80];
2927 int ok;
2929 /* Validate the insn again, and compute first_[dp]reg_to_save. */
2930 ok = push_multiple_operation (PATTERN (insn), VOIDmode);
2931 gcc_assert (ok);
2933 if (first_dreg_to_save == 8)
2934 sprintf (buf, "[--sp] = ( p5:%d );\n", first_preg_to_save);
2935 else if (first_preg_to_save == 6)
2936 sprintf (buf, "[--sp] = ( r7:%d );\n", first_dreg_to_save);
2937 else
2938 sprintf (buf, "[--sp] = ( r7:%d, p5:%d );\n",
2939 first_dreg_to_save, first_preg_to_save);
2941 output_asm_insn (buf, operands);
2944 /* Emit assembly code for one multi-register pop described by INSN, with
2945 operands in OPERANDS. */
2947 void
2948 output_pop_multiple (rtx insn, rtx *operands)
2950 char buf[80];
2951 int ok;
2953 /* Validate the insn again, and compute first_[dp]reg_to_save. */
2954 ok = pop_multiple_operation (PATTERN (insn), VOIDmode);
2955 gcc_assert (ok);
2957 if (first_dreg_to_save == 8)
2958 sprintf (buf, "( p5:%d ) = [sp++];\n", first_preg_to_save);
2959 else if (first_preg_to_save == 6)
2960 sprintf (buf, "( r7:%d ) = [sp++];\n", first_dreg_to_save);
2961 else
2962 sprintf (buf, "( r7:%d, p5:%d ) = [sp++];\n",
2963 first_dreg_to_save, first_preg_to_save);
2965 output_asm_insn (buf, operands);
2968 /* Adjust DST and SRC by OFFSET bytes, and generate one move in mode MODE. */
2970 static void
2971 single_move_for_movmem (rtx dst, rtx src, enum machine_mode mode, HOST_WIDE_INT offset)
2973 rtx scratch = gen_reg_rtx (mode);
2974 rtx srcmem, dstmem;
2976 srcmem = adjust_address_nv (src, mode, offset);
2977 dstmem = adjust_address_nv (dst, mode, offset);
2978 emit_move_insn (scratch, srcmem);
2979 emit_move_insn (dstmem, scratch);
2982 /* Expand a string move operation of COUNT_EXP bytes from SRC to DST, with
2983 alignment ALIGN_EXP. Return true if successful, false if we should fall
2984 back on a different method. */
2986 bool
2987 bfin_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
2989 rtx srcreg, destreg, countreg;
2990 HOST_WIDE_INT align = 0;
2991 unsigned HOST_WIDE_INT count = 0;
2993 if (GET_CODE (align_exp) == CONST_INT)
2994 align = INTVAL (align_exp);
2995 if (GET_CODE (count_exp) == CONST_INT)
2997 count = INTVAL (count_exp);
2998 #if 0
2999 if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
3000 return false;
3001 #endif
3004 /* If optimizing for size, only do single copies inline. */
3005 if (optimize_size)
3007 if (count == 2 && align < 2)
3008 return false;
3009 if (count == 4 && align < 4)
3010 return false;
3011 if (count != 1 && count != 2 && count != 4)
3012 return false;
3014 if (align < 2 && count != 1)
3015 return false;
3017 destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
3018 if (destreg != XEXP (dst, 0))
3019 dst = replace_equiv_address_nv (dst, destreg);
3020 srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
3021 if (srcreg != XEXP (src, 0))
3022 src = replace_equiv_address_nv (src, srcreg);
3024 if (count != 0 && align >= 2)
3026 unsigned HOST_WIDE_INT offset = 0;
3028 if (align >= 4)
3030 if ((count & ~3) == 4)
3032 single_move_for_movmem (dst, src, SImode, offset);
3033 offset = 4;
3035 else if (count & ~3)
3037 HOST_WIDE_INT new_count = ((count >> 2) & 0x3fffffff) - 1;
3038 countreg = copy_to_mode_reg (Pmode, GEN_INT (new_count));
3040 emit_insn (gen_rep_movsi (destreg, srcreg, countreg, destreg, srcreg));
3042 if (count & 2)
3044 single_move_for_movmem (dst, src, HImode, offset);
3045 offset += 2;
3048 else
3050 if ((count & ~1) == 2)
3052 single_move_for_movmem (dst, src, HImode, offset);
3053 offset = 2;
3055 else if (count & ~1)
3057 HOST_WIDE_INT new_count = ((count >> 1) & 0x7fffffff) - 1;
3058 countreg = copy_to_mode_reg (Pmode, GEN_INT (new_count));
3060 emit_insn (gen_rep_movhi (destreg, srcreg, countreg, destreg, srcreg));
3063 if (count & 1)
3065 single_move_for_movmem (dst, src, QImode, offset);
3067 return true;
3069 return false;
3072 /* Compute the alignment for a local variable.
3073 TYPE is the data type, and ALIGN is the alignment that
3074 the object would ordinarily have. The value of this macro is used
3075 instead of that alignment to align the object. */
3078 bfin_local_alignment (tree type, int align)
3080 /* Increasing alignment for (relatively) big types allows the builtin
3081 memcpy can use 32 bit loads/stores. */
3082 if (TYPE_SIZE (type)
3083 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3084 && (TREE_INT_CST_LOW (TYPE_SIZE (type)) > 8
3085 || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 32)
3086 return 32;
3087 return align;
3090 /* Implement TARGET_SCHED_ISSUE_RATE. */
3092 static int
3093 bfin_issue_rate (void)
3095 return 3;
3098 static int
3099 bfin_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
3101 enum attr_type insn_type, dep_insn_type;
3102 int dep_insn_code_number;
3104 /* Anti and output dependencies have zero cost. */
3105 if (REG_NOTE_KIND (link) != 0)
3106 return 0;
3108 dep_insn_code_number = recog_memoized (dep_insn);
3110 /* If we can't recognize the insns, we can't really do anything. */
3111 if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
3112 return cost;
3114 insn_type = get_attr_type (insn);
3115 dep_insn_type = get_attr_type (dep_insn);
3117 if (dep_insn_type == TYPE_MOVE || dep_insn_type == TYPE_MCLD)
3119 rtx pat = PATTERN (dep_insn);
3120 rtx dest = SET_DEST (pat);
3121 rtx src = SET_SRC (pat);
3122 if (! ADDRESS_REGNO_P (REGNO (dest))
3123 || ! (MEM_P (src) || D_REGNO_P (REGNO (src))))
3124 return cost;
3125 return cost + (dep_insn_type == TYPE_MOVE ? 4 : 3);
3128 return cost;
3132 /* Increment the counter for the number of loop instructions in the
3133 current function. */
3135 void
3136 bfin_hardware_loop (void)
3138 cfun->machine->has_hardware_loops++;
3141 /* Maximum loop nesting depth. */
3142 #define MAX_LOOP_DEPTH 2
3144 /* Maximum size of a loop. */
3145 #define MAX_LOOP_LENGTH 2042
3147 /* Maximum distance of the LSETUP instruction from the loop start. */
3148 #define MAX_LSETUP_DISTANCE 30
3150 /* We need to keep a vector of loops */
3151 typedef struct loop_info *loop_info;
3152 DEF_VEC_P (loop_info);
3153 DEF_VEC_ALLOC_P (loop_info,heap);
3155 /* Information about a loop we have found (or are in the process of
3156 finding). */
3157 struct loop_info GTY (())
3159 /* loop number, for dumps */
3160 int loop_no;
3162 /* All edges that jump into and out of the loop. */
3163 VEC(edge,gc) *incoming;
3165 /* We can handle two cases: all incoming edges have the same destination
3166 block, or all incoming edges have the same source block. These two
3167 members are set to the common source or destination we found, or NULL
3168 if different blocks were found. If both are NULL the loop can't be
3169 optimized. */
3170 basic_block incoming_src;
3171 basic_block incoming_dest;
3173 /* First block in the loop. This is the one branched to by the loop_end
3174 insn. */
3175 basic_block head;
3177 /* Last block in the loop (the one with the loop_end insn). */
3178 basic_block tail;
3180 /* The successor block of the loop. This is the one the loop_end insn
3181 falls into. */
3182 basic_block successor;
3184 /* The last instruction in the tail. */
3185 rtx last_insn;
3187 /* The loop_end insn. */
3188 rtx loop_end;
3190 /* The iteration register. */
3191 rtx iter_reg;
3193 /* The new initialization insn. */
3194 rtx init;
3196 /* The new initialization instruction. */
3197 rtx loop_init;
3199 /* The new label placed at the beginning of the loop. */
3200 rtx start_label;
3202 /* The new label placed at the end of the loop. */
3203 rtx end_label;
3205 /* The length of the loop. */
3206 int length;
3208 /* The nesting depth of the loop. */
3209 int depth;
3211 /* Nonzero if we can't optimize this loop. */
3212 int bad;
3214 /* True if we have visited this loop. */
3215 int visited;
3217 /* True if this loop body clobbers any of LC0, LT0, or LB0. */
3218 int clobber_loop0;
3220 /* True if this loop body clobbers any of LC1, LT1, or LB1. */
3221 int clobber_loop1;
3223 /* Next loop in the graph. */
3224 struct loop_info *next;
3226 /* Immediate outer loop of this loop. */
3227 struct loop_info *outer;
3229 /* Vector of blocks only within the loop, including those within
3230 inner loops. */
3231 VEC (basic_block,heap) *blocks;
3233 /* Same information in a bitmap. */
3234 bitmap block_bitmap;
3236 /* Vector of inner loops within this loop */
3237 VEC (loop_info,heap) *loops;
3240 static void
3241 bfin_dump_loops (loop_info loops)
3243 loop_info loop;
3245 for (loop = loops; loop; loop = loop->next)
3247 loop_info i;
3248 basic_block b;
3249 unsigned ix;
3251 fprintf (dump_file, ";; loop %d: ", loop->loop_no);
3252 if (loop->bad)
3253 fprintf (dump_file, "(bad) ");
3254 fprintf (dump_file, "{head:%d, depth:%d}", loop->head->index, loop->depth);
3256 fprintf (dump_file, " blocks: [ ");
3257 for (ix = 0; VEC_iterate (basic_block, loop->blocks, ix, b); ix++)
3258 fprintf (dump_file, "%d ", b->index);
3259 fprintf (dump_file, "] ");
3261 fprintf (dump_file, " inner loops: [ ");
3262 for (ix = 0; VEC_iterate (loop_info, loop->loops, ix, i); ix++)
3263 fprintf (dump_file, "%d ", i->loop_no);
3264 fprintf (dump_file, "]\n");
3266 fprintf (dump_file, "\n");
3269 /* Scan the blocks of LOOP (and its inferiors) looking for basic block
3270 BB. Return true, if we find it. */
3272 static bool
3273 bfin_bb_in_loop (loop_info loop, basic_block bb)
3275 return bitmap_bit_p (loop->block_bitmap, bb->index);
3278 /* Scan the blocks of LOOP (and its inferiors) looking for uses of
3279 REG. Return true, if we find any. Don't count the loop's loop_end
3280 insn if it matches LOOP_END. */
3282 static bool
3283 bfin_scan_loop (loop_info loop, rtx reg, rtx loop_end)
3285 unsigned ix;
3286 basic_block bb;
3288 for (ix = 0; VEC_iterate (basic_block, loop->blocks, ix, bb); ix++)
3290 rtx insn;
3292 for (insn = BB_HEAD (bb);
3293 insn != NEXT_INSN (BB_END (bb));
3294 insn = NEXT_INSN (insn))
3296 if (!INSN_P (insn))
3297 continue;
3298 if (insn == loop_end)
3299 continue;
3300 if (reg_mentioned_p (reg, PATTERN (insn)))
3301 return true;
3304 return false;
3307 /* Estimate the length of INSN conservatively. */
3309 static int
3310 length_for_loop (rtx insn)
3312 int length = 0;
3313 if (JUMP_P (insn) && any_condjump_p (insn) && !optimize_size)
3315 if (TARGET_CSYNC_ANOMALY)
3316 length = 8;
3317 else if (TARGET_SPECLD_ANOMALY)
3318 length = 6;
3320 else if (LABEL_P (insn))
3322 if (TARGET_CSYNC_ANOMALY)
3323 length = 4;
3326 if (INSN_P (insn))
3327 length += get_attr_length (insn);
3329 return length;
3332 /* Optimize LOOP. */
3334 static void
3335 bfin_optimize_loop (loop_info loop)
3337 basic_block bb;
3338 loop_info inner;
3339 rtx insn, init_insn, last_insn, nop_insn;
3340 rtx loop_init, start_label, end_label;
3341 rtx reg_lc0, reg_lc1, reg_lt0, reg_lt1, reg_lb0, reg_lb1;
3342 rtx iter_reg;
3343 rtx lc_reg, lt_reg, lb_reg;
3344 rtx seq, seq_end;
3345 int length;
3346 unsigned ix;
3347 int inner_depth = 0;
3349 if (loop->visited)
3350 return;
3352 loop->visited = 1;
3354 if (loop->bad)
3356 if (dump_file)
3357 fprintf (dump_file, ";; loop %d bad when found\n", loop->loop_no);
3358 goto bad_loop;
3361 /* Every loop contains in its list of inner loops every loop nested inside
3362 it, even if there are intermediate loops. This works because we're doing
3363 a depth-first search here and never visit a loop more than once. */
3364 for (ix = 0; VEC_iterate (loop_info, loop->loops, ix, inner); ix++)
3366 bfin_optimize_loop (inner);
3368 if (!inner->bad && inner_depth < inner->depth)
3370 inner_depth = inner->depth;
3372 loop->clobber_loop0 |= inner->clobber_loop0;
3373 loop->clobber_loop1 |= inner->clobber_loop1;
3377 loop->depth = inner_depth + 1;
3378 if (loop->depth > MAX_LOOP_DEPTH)
3380 if (dump_file)
3381 fprintf (dump_file, ";; loop %d too deep\n", loop->loop_no);
3382 goto bad_loop;
3385 /* Get the loop iteration register. */
3386 iter_reg = loop->iter_reg;
3388 if (!DPREG_P (iter_reg))
3390 if (dump_file)
3391 fprintf (dump_file, ";; loop %d iteration count NOT in PREG or DREG\n",
3392 loop->loop_no);
3393 goto bad_loop;
3396 if (loop->incoming_src)
3398 /* Make sure the predecessor is before the loop start label, as required by
3399 the LSETUP instruction. */
3400 length = 0;
3401 for (insn = BB_END (loop->incoming_src);
3402 insn && insn != loop->start_label;
3403 insn = NEXT_INSN (insn))
3404 length += length_for_loop (insn);
3406 if (!insn)
3408 if (dump_file)
3409 fprintf (dump_file, ";; loop %d lsetup not before loop_start\n",
3410 loop->loop_no);
3411 goto bad_loop;
3414 if (length > MAX_LSETUP_DISTANCE)
3416 if (dump_file)
3417 fprintf (dump_file, ";; loop %d lsetup too far away\n", loop->loop_no);
3418 goto bad_loop;
3422 /* Check if start_label appears before loop_end and calculate the
3423 offset between them. We calculate the length of instructions
3424 conservatively. */
3425 length = 0;
3426 for (insn = loop->start_label;
3427 insn && insn != loop->loop_end;
3428 insn = NEXT_INSN (insn))
3429 length += length_for_loop (insn);
3431 if (!insn)
3433 if (dump_file)
3434 fprintf (dump_file, ";; loop %d start_label not before loop_end\n",
3435 loop->loop_no);
3436 goto bad_loop;
3439 loop->length = length;
3440 if (loop->length > MAX_LOOP_LENGTH)
3442 if (dump_file)
3443 fprintf (dump_file, ";; loop %d too long\n", loop->loop_no);
3444 goto bad_loop;
3447 /* Scan all the blocks to make sure they don't use iter_reg. */
3448 if (bfin_scan_loop (loop, iter_reg, loop->loop_end))
3450 if (dump_file)
3451 fprintf (dump_file, ";; loop %d uses iterator\n", loop->loop_no);
3452 goto bad_loop;
3455 /* Scan all the insns to see if the loop body clobber
3456 any hardware loop registers. */
3458 reg_lc0 = gen_rtx_REG (SImode, REG_LC0);
3459 reg_lc1 = gen_rtx_REG (SImode, REG_LC1);
3460 reg_lt0 = gen_rtx_REG (SImode, REG_LT0);
3461 reg_lt1 = gen_rtx_REG (SImode, REG_LT1);
3462 reg_lb0 = gen_rtx_REG (SImode, REG_LB0);
3463 reg_lb1 = gen_rtx_REG (SImode, REG_LB1);
3465 for (ix = 0; VEC_iterate (basic_block, loop->blocks, ix, bb); ix++)
3467 rtx insn;
3469 for (insn = BB_HEAD (bb);
3470 insn != NEXT_INSN (BB_END (bb));
3471 insn = NEXT_INSN (insn))
3473 if (!INSN_P (insn))
3474 continue;
3476 if (reg_set_p (reg_lc0, insn)
3477 || reg_set_p (reg_lt0, insn)
3478 || reg_set_p (reg_lb0, insn))
3479 loop->clobber_loop0 = 1;
3481 if (reg_set_p (reg_lc1, insn)
3482 || reg_set_p (reg_lt1, insn)
3483 || reg_set_p (reg_lb1, insn))
3484 loop->clobber_loop1 |= 1;
3488 if ((loop->clobber_loop0 && loop->clobber_loop1)
3489 || (loop->depth == MAX_LOOP_DEPTH && loop->clobber_loop0))
3491 loop->depth = MAX_LOOP_DEPTH + 1;
3492 if (dump_file)
3493 fprintf (dump_file, ";; loop %d no loop reg available\n",
3494 loop->loop_no);
3495 goto bad_loop;
3498 /* There should be an instruction before the loop_end instruction
3499 in the same basic block. And the instruction must not be
3500 - JUMP
3501 - CONDITIONAL BRANCH
3502 - CALL
3503 - CSYNC
3504 - SSYNC
3505 - Returns (RTS, RTN, etc.) */
3507 bb = loop->tail;
3508 last_insn = PREV_INSN (loop->loop_end);
3510 while (1)
3512 for (; last_insn != PREV_INSN (BB_HEAD (bb));
3513 last_insn = PREV_INSN (last_insn))
3514 if (INSN_P (last_insn))
3515 break;
3517 if (last_insn != PREV_INSN (BB_HEAD (bb)))
3518 break;
3520 if (single_pred_p (bb)
3521 && single_pred (bb) != ENTRY_BLOCK_PTR)
3523 bb = single_pred (bb);
3524 last_insn = BB_END (bb);
3525 continue;
3527 else
3529 last_insn = NULL_RTX;
3530 break;
3534 if (!last_insn)
3536 if (dump_file)
3537 fprintf (dump_file, ";; loop %d has no last instruction\n",
3538 loop->loop_no);
3539 goto bad_loop;
3542 if (JUMP_P (last_insn))
3544 loop_info inner = bb->aux;
3545 if (inner
3546 && inner->outer == loop
3547 && inner->loop_end == last_insn
3548 && inner->depth == 1)
3549 /* This jump_insn is the exact loop_end of an inner loop
3550 and to be optimized away. So use the inner's last_insn. */
3551 last_insn = inner->last_insn;
3552 else
3554 if (dump_file)
3555 fprintf (dump_file, ";; loop %d has bad last instruction\n",
3556 loop->loop_no);
3557 goto bad_loop;
3560 else if (CALL_P (last_insn)
3561 || (GET_CODE (PATTERN (last_insn)) != SEQUENCE
3562 && get_attr_type (last_insn) == TYPE_SYNC)
3563 || recog_memoized (last_insn) == CODE_FOR_return_internal)
3565 if (dump_file)
3566 fprintf (dump_file, ";; loop %d has bad last instruction\n",
3567 loop->loop_no);
3568 goto bad_loop;
3571 if (GET_CODE (PATTERN (last_insn)) == ASM_INPUT
3572 || asm_noperands (PATTERN (last_insn)) >= 0
3573 || (GET_CODE (PATTERN (last_insn)) != SEQUENCE
3574 && get_attr_seq_insns (last_insn) == SEQ_INSNS_MULTI))
3576 nop_insn = emit_insn_after (gen_nop (), last_insn);
3577 last_insn = nop_insn;
3580 loop->last_insn = last_insn;
3582 /* The loop is good for replacement. */
3583 start_label = loop->start_label;
3584 end_label = gen_label_rtx ();
3585 iter_reg = loop->iter_reg;
3587 if (loop->depth == 1 && !loop->clobber_loop1)
3589 lc_reg = reg_lc1;
3590 lt_reg = reg_lt1;
3591 lb_reg = reg_lb1;
3592 loop->clobber_loop1 = 1;
3594 else
3596 lc_reg = reg_lc0;
3597 lt_reg = reg_lt0;
3598 lb_reg = reg_lb0;
3599 loop->clobber_loop0 = 1;
3602 /* If iter_reg is a DREG, we need generate an instruction to load
3603 the loop count into LC register. */
3604 if (D_REGNO_P (REGNO (iter_reg)))
3606 init_insn = gen_movsi (lc_reg, iter_reg);
3607 loop_init = gen_lsetup_without_autoinit (lt_reg, start_label,
3608 lb_reg, end_label,
3609 lc_reg);
3611 else if (P_REGNO_P (REGNO (iter_reg)))
3613 init_insn = NULL_RTX;
3614 loop_init = gen_lsetup_with_autoinit (lt_reg, start_label,
3615 lb_reg, end_label,
3616 lc_reg, iter_reg);
3618 else
3619 gcc_unreachable ();
3621 loop->init = init_insn;
3622 loop->end_label = end_label;
3623 loop->loop_init = loop_init;
3625 if (dump_file)
3627 fprintf (dump_file, ";; replacing loop %d initializer with\n",
3628 loop->loop_no);
3629 print_rtl_single (dump_file, loop->loop_init);
3630 fprintf (dump_file, ";; replacing loop %d terminator with\n",
3631 loop->loop_no);
3632 print_rtl_single (dump_file, loop->loop_end);
3635 start_sequence ();
3637 if (loop->init != NULL_RTX)
3638 emit_insn (loop->init);
3639 seq_end = emit_insn (loop->loop_init);
3641 seq = get_insns ();
3642 end_sequence ();
3644 if (loop->incoming_src)
3646 rtx prev = BB_END (loop->incoming_src);
3647 if (VEC_length (edge, loop->incoming) > 1
3648 || !(VEC_last (edge, loop->incoming)->flags & EDGE_FALLTHRU))
3650 gcc_assert (JUMP_P (prev));
3651 prev = PREV_INSN (prev);
3653 emit_insn_after (seq, prev);
3655 else
3657 basic_block new_bb;
3658 edge e;
3659 edge_iterator ei;
3661 if (loop->head != loop->incoming_dest)
3663 FOR_EACH_EDGE (e, ei, loop->head->preds)
3665 if (e->flags & EDGE_FALLTHRU)
3667 rtx newjump = gen_jump (loop->start_label);
3668 emit_insn_before (newjump, BB_HEAD (loop->head));
3669 new_bb = create_basic_block (newjump, newjump, loop->head->prev_bb);
3670 gcc_assert (new_bb = loop->head->prev_bb);
3671 break;
3676 emit_insn_before (seq, BB_HEAD (loop->head));
3677 seq = emit_label_before (gen_label_rtx (), seq);
3679 new_bb = create_basic_block (seq, seq_end, loop->head->prev_bb);
3680 FOR_EACH_EDGE (e, ei, loop->incoming)
3682 if (!(e->flags & EDGE_FALLTHRU)
3683 || e->dest != loop->head)
3684 redirect_edge_and_branch_force (e, new_bb);
3685 else
3686 redirect_edge_succ (e, new_bb);
3690 delete_insn (loop->loop_end);
3691 /* Insert the loop end label before the last instruction of the loop. */
3692 emit_label_before (loop->end_label, loop->last_insn);
3694 return;
3696 bad_loop:
3698 if (dump_file)
3699 fprintf (dump_file, ";; loop %d is bad\n", loop->loop_no);
3701 loop->bad = 1;
3703 if (DPREG_P (loop->iter_reg))
3705 /* If loop->iter_reg is a DREG or PREG, we can split it here
3706 without scratch register. */
3707 rtx insn;
3709 emit_insn_before (gen_addsi3 (loop->iter_reg,
3710 loop->iter_reg,
3711 constm1_rtx),
3712 loop->loop_end);
3714 emit_insn_before (gen_cmpsi (loop->iter_reg, const0_rtx),
3715 loop->loop_end);
3717 insn = emit_jump_insn_before (gen_bne (loop->start_label),
3718 loop->loop_end);
3720 JUMP_LABEL (insn) = loop->start_label;
3721 LABEL_NUSES (loop->start_label)++;
3722 delete_insn (loop->loop_end);
3726 /* Called from bfin_reorg_loops when a potential loop end is found. LOOP is
3727 a newly set up structure describing the loop, it is this function's
3728 responsibility to fill most of it. TAIL_BB and TAIL_INSN point to the
3729 loop_end insn and its enclosing basic block. */
3731 static void
3732 bfin_discover_loop (loop_info loop, basic_block tail_bb, rtx tail_insn)
3734 unsigned dwork = 0;
3735 basic_block bb;
3736 VEC (basic_block,heap) *works = VEC_alloc (basic_block,heap,20);
3738 loop->tail = tail_bb;
3739 loop->head = BRANCH_EDGE (tail_bb)->dest;
3740 loop->successor = FALLTHRU_EDGE (tail_bb)->dest;
3741 loop->loop_end = tail_insn;
3742 loop->last_insn = NULL_RTX;
3743 loop->iter_reg = SET_DEST (XVECEXP (PATTERN (tail_insn), 0, 1));
3744 loop->depth = loop->length = 0;
3745 loop->visited = 0;
3746 loop->clobber_loop0 = loop->clobber_loop1 = 0;
3747 loop->outer = NULL;
3748 loop->loops = NULL;
3749 loop->incoming = VEC_alloc (edge, gc, 2);
3750 loop->init = loop->loop_init = NULL_RTX;
3751 loop->start_label = XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (tail_insn), 0, 0)), 1), 0);
3752 loop->end_label = NULL_RTX;
3753 loop->bad = 0;
3755 VEC_safe_push (basic_block, heap, works, loop->head);
3757 while (VEC_iterate (basic_block, works, dwork++, bb))
3759 edge e;
3760 edge_iterator ei;
3761 if (bb == EXIT_BLOCK_PTR)
3763 /* We've reached the exit block. The loop must be bad. */
3764 if (dump_file)
3765 fprintf (dump_file,
3766 ";; Loop is bad - reached exit block while scanning\n");
3767 loop->bad = 1;
3768 break;
3771 if (bitmap_bit_p (loop->block_bitmap, bb->index))
3772 continue;
3774 /* We've not seen this block before. Add it to the loop's
3775 list and then add each successor to the work list. */
3777 VEC_safe_push (basic_block, heap, loop->blocks, bb);
3778 bitmap_set_bit (loop->block_bitmap, bb->index);
3780 if (bb != tail_bb)
3782 FOR_EACH_EDGE (e, ei, bb->succs)
3784 basic_block succ = EDGE_SUCC (bb, ei.index)->dest;
3785 if (!REGNO_REG_SET_P (df_get_live_in (succ),
3786 REGNO (loop->iter_reg)))
3787 continue;
3788 if (!VEC_space (basic_block, works, 1))
3790 if (dwork)
3792 VEC_block_remove (basic_block, works, 0, dwork);
3793 dwork = 0;
3795 else
3796 VEC_reserve (basic_block, heap, works, 1);
3798 VEC_quick_push (basic_block, works, succ);
3803 /* Find the predecessor, and make sure nothing else jumps into this loop. */
3804 if (!loop->bad)
3806 int pass, retry;
3807 for (dwork = 0; VEC_iterate (basic_block, loop->blocks, dwork, bb); dwork++)
3809 edge e;
3810 edge_iterator ei;
3811 FOR_EACH_EDGE (e, ei, bb->preds)
3813 basic_block pred = e->src;
3815 if (!bfin_bb_in_loop (loop, pred))
3817 if (dump_file)
3818 fprintf (dump_file, ";; Loop %d: incoming edge %d -> %d\n",
3819 loop->loop_no, pred->index,
3820 e->dest->index);
3821 VEC_safe_push (edge, gc, loop->incoming, e);
3826 for (pass = 0, retry = 1; retry && pass < 2; pass++)
3828 edge e;
3829 edge_iterator ei;
3830 bool first = true;
3831 retry = 0;
3833 FOR_EACH_EDGE (e, ei, loop->incoming)
3835 if (first)
3837 loop->incoming_src = e->src;
3838 loop->incoming_dest = e->dest;
3839 first = false;
3841 else
3843 if (e->dest != loop->incoming_dest)
3844 loop->incoming_dest = NULL;
3845 if (e->src != loop->incoming_src)
3846 loop->incoming_src = NULL;
3848 if (loop->incoming_src == NULL && loop->incoming_dest == NULL)
3850 if (pass == 0)
3852 if (dump_file)
3853 fprintf (dump_file,
3854 ";; retrying loop %d with forwarder blocks\n",
3855 loop->loop_no);
3856 retry = 1;
3857 break;
3859 loop->bad = 1;
3860 if (dump_file)
3861 fprintf (dump_file,
3862 ";; can't find suitable entry for loop %d\n",
3863 loop->loop_no);
3864 goto out;
3867 if (retry)
3869 retry = 0;
3870 FOR_EACH_EDGE (e, ei, loop->incoming)
3872 if (forwarder_block_p (e->src))
3874 edge e2;
3875 edge_iterator ei2;
3877 if (dump_file)
3878 fprintf (dump_file,
3879 ";; Adding forwarder block %d to loop %d and retrying\n",
3880 e->src->index, loop->loop_no);
3881 VEC_safe_push (basic_block, heap, loop->blocks, e->src);
3882 bitmap_set_bit (loop->block_bitmap, e->src->index);
3883 FOR_EACH_EDGE (e2, ei2, e->src->preds)
3884 VEC_safe_push (edge, gc, loop->incoming, e2);
3885 VEC_unordered_remove (edge, loop->incoming, ei.index);
3886 retry = 1;
3887 break;
3894 out:
3895 VEC_free (basic_block, heap, works);
3898 /* Analyze the structure of the loops in the current function. Use STACK
3899 for bitmap allocations. Returns all the valid candidates for hardware
3900 loops found in this function. */
3901 static loop_info
3902 bfin_discover_loops (bitmap_obstack *stack, FILE *dump_file)
3904 loop_info loops = NULL;
3905 loop_info loop;
3906 basic_block bb;
3907 bitmap tmp_bitmap;
3908 int nloops = 0;
3910 /* Find all the possible loop tails. This means searching for every
3911 loop_end instruction. For each one found, create a loop_info
3912 structure and add the head block to the work list. */
3913 FOR_EACH_BB (bb)
3915 rtx tail = BB_END (bb);
3917 while (GET_CODE (tail) == NOTE)
3918 tail = PREV_INSN (tail);
3920 bb->aux = NULL;
3922 if (INSN_P (tail) && recog_memoized (tail) == CODE_FOR_loop_end)
3924 /* A possible loop end */
3926 loop = XNEW (struct loop_info);
3927 loop->next = loops;
3928 loops = loop;
3929 loop->loop_no = nloops++;
3930 loop->blocks = VEC_alloc (basic_block, heap, 20);
3931 loop->block_bitmap = BITMAP_ALLOC (stack);
3932 bb->aux = loop;
3934 if (dump_file)
3936 fprintf (dump_file, ";; potential loop %d ending at\n",
3937 loop->loop_no);
3938 print_rtl_single (dump_file, tail);
3941 bfin_discover_loop (loop, bb, tail);
3945 tmp_bitmap = BITMAP_ALLOC (stack);
3946 /* Compute loop nestings. */
3947 for (loop = loops; loop; loop = loop->next)
3949 loop_info other;
3950 if (loop->bad)
3951 continue;
3953 for (other = loop->next; other; other = other->next)
3955 if (other->bad)
3956 continue;
3958 bitmap_and (tmp_bitmap, other->block_bitmap, loop->block_bitmap);
3959 if (bitmap_empty_p (tmp_bitmap))
3960 continue;
3961 if (bitmap_equal_p (tmp_bitmap, other->block_bitmap))
3963 other->outer = loop;
3964 VEC_safe_push (loop_info, heap, loop->loops, other);
3966 else if (bitmap_equal_p (tmp_bitmap, loop->block_bitmap))
3968 loop->outer = other;
3969 VEC_safe_push (loop_info, heap, other->loops, loop);
3971 else
3973 if (dump_file)
3974 fprintf (dump_file,
3975 ";; can't find suitable nesting for loops %d and %d\n",
3976 loop->loop_no, other->loop_no);
3977 loop->bad = other->bad = 1;
3981 BITMAP_FREE (tmp_bitmap);
3983 return loops;
3986 /* Free up the loop structures in LOOPS. */
3987 static void
3988 free_loops (loop_info loops)
3990 while (loops)
3992 loop_info loop = loops;
3993 loops = loop->next;
3994 VEC_free (loop_info, heap, loop->loops);
3995 VEC_free (basic_block, heap, loop->blocks);
3996 BITMAP_FREE (loop->block_bitmap);
3997 XDELETE (loop);
4001 #define BB_AUX_INDEX(BB) ((unsigned)(BB)->aux)
4003 /* The taken-branch edge from the loop end can actually go forward. Since the
4004 Blackfin's LSETUP instruction requires that the loop end be after the loop
4005 start, try to reorder a loop's basic blocks when we find such a case. */
4006 static void
4007 bfin_reorder_loops (loop_info loops, FILE *dump_file)
4009 basic_block bb;
4010 loop_info loop;
4012 FOR_EACH_BB (bb)
4013 bb->aux = NULL;
4014 cfg_layout_initialize (0);
4016 for (loop = loops; loop; loop = loop->next)
4018 unsigned index;
4019 basic_block bb;
4020 edge e;
4021 edge_iterator ei;
4023 if (loop->bad)
4024 continue;
4026 /* Recreate an index for basic blocks that represents their order. */
4027 for (bb = ENTRY_BLOCK_PTR->next_bb, index = 0;
4028 bb != EXIT_BLOCK_PTR;
4029 bb = bb->next_bb, index++)
4030 bb->aux = (PTR) index;
4032 if (BB_AUX_INDEX (loop->head) < BB_AUX_INDEX (loop->tail))
4033 continue;
4035 FOR_EACH_EDGE (e, ei, loop->head->succs)
4037 if (bitmap_bit_p (loop->block_bitmap, e->dest->index)
4038 && BB_AUX_INDEX (e->dest) < BB_AUX_INDEX (loop->tail))
4040 basic_block start_bb = e->dest;
4041 basic_block start_prev_bb = start_bb->prev_bb;
4043 if (dump_file)
4044 fprintf (dump_file, ";; Moving block %d before block %d\n",
4045 loop->head->index, start_bb->index);
4046 loop->head->prev_bb->next_bb = loop->head->next_bb;
4047 loop->head->next_bb->prev_bb = loop->head->prev_bb;
4049 loop->head->prev_bb = start_prev_bb;
4050 loop->head->next_bb = start_bb;
4051 start_prev_bb->next_bb = start_bb->prev_bb = loop->head;
4052 break;
4055 loops = loops->next;
4058 FOR_EACH_BB (bb)
4060 if (bb->next_bb != EXIT_BLOCK_PTR)
4061 bb->aux = bb->next_bb;
4062 else
4063 bb->aux = NULL;
4065 cfg_layout_finalize ();
4066 df_analyze ();
4069 /* Run from machine_dependent_reorg, this pass looks for doloop_end insns
4070 and tries to rewrite the RTL of these loops so that proper Blackfin
4071 hardware loops are generated. */
4073 static void
4074 bfin_reorg_loops (FILE *dump_file)
4076 loop_info loops = NULL;
4077 loop_info loop;
4078 basic_block bb;
4079 bitmap_obstack stack;
4081 bitmap_obstack_initialize (&stack);
4083 if (dump_file)
4084 fprintf (dump_file, ";; Find loops, first pass\n\n");
4086 loops = bfin_discover_loops (&stack, dump_file);
4088 if (dump_file)
4089 bfin_dump_loops (loops);
4091 bfin_reorder_loops (loops, dump_file);
4092 free_loops (loops);
4094 if (dump_file)
4095 fprintf (dump_file, ";; Find loops, second pass\n\n");
4097 loops = bfin_discover_loops (&stack, dump_file);
4098 if (dump_file)
4100 fprintf (dump_file, ";; All loops found:\n\n");
4101 bfin_dump_loops (loops);
4104 /* Now apply the optimizations. */
4105 for (loop = loops; loop; loop = loop->next)
4106 bfin_optimize_loop (loop);
4108 if (dump_file)
4110 fprintf (dump_file, ";; After hardware loops optimization:\n\n");
4111 bfin_dump_loops (loops);
4114 free_loops (loops);
4116 if (dump_file)
4117 print_rtl (dump_file, get_insns ());
4119 FOR_EACH_BB (bb)
4120 bb->aux = NULL;
4123 /* Possibly generate a SEQUENCE out of three insns found in SLOT.
4124 Returns true if we modified the insn chain, false otherwise. */
4125 static bool
4126 gen_one_bundle (rtx slot[3])
4128 gcc_assert (slot[1] != NULL_RTX);
4130 /* Verify that we really can do the multi-issue. */
4131 if (slot[0])
4133 rtx t = NEXT_INSN (slot[0]);
4134 while (t != slot[1])
4136 if (GET_CODE (t) != NOTE
4137 || NOTE_KIND (t) != NOTE_INSN_DELETED)
4138 return false;
4139 t = NEXT_INSN (t);
4142 if (slot[2])
4144 rtx t = NEXT_INSN (slot[1]);
4145 while (t != slot[2])
4147 if (GET_CODE (t) != NOTE
4148 || NOTE_KIND (t) != NOTE_INSN_DELETED)
4149 return false;
4150 t = NEXT_INSN (t);
4154 if (slot[0] == NULL_RTX)
4156 slot[0] = emit_insn_before (gen_mnop (), slot[1]);
4157 df_insn_rescan (slot[0]);
4159 if (slot[2] == NULL_RTX)
4161 slot[2] = emit_insn_after (gen_forced_nop (), slot[1]);
4162 df_insn_rescan (slot[2]);
4165 /* Avoid line number information being printed inside one bundle. */
4166 if (INSN_LOCATOR (slot[1])
4167 && INSN_LOCATOR (slot[1]) != INSN_LOCATOR (slot[0]))
4168 INSN_LOCATOR (slot[1]) = INSN_LOCATOR (slot[0]);
4169 if (INSN_LOCATOR (slot[2])
4170 && INSN_LOCATOR (slot[2]) != INSN_LOCATOR (slot[0]))
4171 INSN_LOCATOR (slot[2]) = INSN_LOCATOR (slot[0]);
4173 /* Terminate them with "|| " instead of ";" in the output. */
4174 PUT_MODE (slot[0], SImode);
4175 PUT_MODE (slot[1], SImode);
4176 /* Terminate the bundle, for the benefit of reorder_var_tracking_notes. */
4177 PUT_MODE (slot[2], QImode);
4178 return true;
4181 /* Go through all insns, and use the information generated during scheduling
4182 to generate SEQUENCEs to represent bundles of instructions issued
4183 simultaneously. */
4185 static void
4186 bfin_gen_bundles (void)
4188 basic_block bb;
4189 FOR_EACH_BB (bb)
4191 rtx insn, next;
4192 rtx slot[3];
4193 int n_filled = 0;
4195 slot[0] = slot[1] = slot[2] = NULL_RTX;
4196 for (insn = BB_HEAD (bb);; insn = next)
4198 int at_end;
4199 if (INSN_P (insn))
4201 if (get_attr_type (insn) == TYPE_DSP32)
4202 slot[0] = insn;
4203 else if (slot[1] == NULL_RTX)
4204 slot[1] = insn;
4205 else
4206 slot[2] = insn;
4207 n_filled++;
4210 next = NEXT_INSN (insn);
4211 while (next && insn != BB_END (bb)
4212 && !(INSN_P (next)
4213 && GET_CODE (PATTERN (next)) != USE
4214 && GET_CODE (PATTERN (next)) != CLOBBER))
4216 insn = next;
4217 next = NEXT_INSN (insn);
4220 /* BB_END can change due to emitting extra NOPs, so check here. */
4221 at_end = insn == BB_END (bb);
4222 if (at_end || GET_MODE (next) == TImode)
4224 if ((n_filled < 2
4225 || !gen_one_bundle (slot))
4226 && slot[0] != NULL_RTX)
4228 rtx pat = PATTERN (slot[0]);
4229 if (GET_CODE (pat) == SET
4230 && GET_CODE (SET_SRC (pat)) == UNSPEC
4231 && XINT (SET_SRC (pat), 1) == UNSPEC_32BIT)
4233 SET_SRC (pat) = XVECEXP (SET_SRC (pat), 0, 0);
4234 INSN_CODE (slot[0]) = -1;
4235 df_insn_rescan (slot[0]);
4238 n_filled = 0;
4239 slot[0] = slot[1] = slot[2] = NULL_RTX;
4241 if (at_end)
4242 break;
4247 /* Ensure that no var tracking notes are emitted in the middle of a
4248 three-instruction bundle. */
4250 static void
4251 reorder_var_tracking_notes (void)
4253 basic_block bb;
4254 FOR_EACH_BB (bb)
4256 rtx insn, next;
4257 rtx queue = NULL_RTX;
4258 bool in_bundle = false;
4260 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = next)
4262 next = NEXT_INSN (insn);
4264 if (INSN_P (insn))
4266 /* Emit queued up notes at the last instruction of a bundle. */
4267 if (GET_MODE (insn) == QImode)
4269 while (queue)
4271 rtx next_queue = PREV_INSN (queue);
4272 PREV_INSN (NEXT_INSN (insn)) = queue;
4273 NEXT_INSN (queue) = NEXT_INSN (insn);
4274 NEXT_INSN (insn) = queue;
4275 PREV_INSN (queue) = insn;
4276 queue = next_queue;
4278 in_bundle = false;
4280 else if (GET_MODE (insn) == SImode)
4281 in_bundle = true;
4283 else if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
4285 if (in_bundle)
4287 rtx prev = PREV_INSN (insn);
4288 PREV_INSN (next) = prev;
4289 NEXT_INSN (prev) = next;
4291 PREV_INSN (insn) = queue;
4292 queue = insn;
4299 /* Return an insn type for INSN that can be used by the caller for anomaly
4300 workarounds. This differs from plain get_attr_type in that it handles
4301 SEQUENCEs. */
4303 static enum attr_type
4304 type_for_anomaly (rtx insn)
4306 rtx pat = PATTERN (insn);
4307 if (GET_CODE (pat) == SEQUENCE)
4309 enum attr_type t;
4310 t = get_attr_type (XVECEXP (pat, 0, 1));
4311 if (t == TYPE_MCLD)
4312 return t;
4313 t = get_attr_type (XVECEXP (pat, 0, 2));
4314 if (t == TYPE_MCLD)
4315 return t;
4316 return TYPE_MCST;
4318 else
4319 return get_attr_type (insn);
4322 /* Return nonzero if INSN contains any loads that may trap. It handles
4323 SEQUENCEs correctly. */
4325 static bool
4326 trapping_loads_p (rtx insn)
4328 rtx pat = PATTERN (insn);
4329 if (GET_CODE (pat) == SEQUENCE)
4331 enum attr_type t;
4332 t = get_attr_type (XVECEXP (pat, 0, 1));
4333 if (t == TYPE_MCLD
4334 && may_trap_p (SET_SRC (PATTERN (XVECEXP (pat, 0, 1)))))
4335 return true;
4336 t = get_attr_type (XVECEXP (pat, 0, 2));
4337 if (t == TYPE_MCLD
4338 && may_trap_p (SET_SRC (PATTERN (XVECEXP (pat, 0, 2)))))
4339 return true;
4340 return false;
4342 else
4343 return may_trap_p (SET_SRC (single_set (insn)));
4346 /* This function acts like NEXT_INSN, but is aware of three-insn bundles and
4347 skips all subsequent parallel instructions if INSN is the start of such
4348 a group. */
4349 static rtx
4350 find_next_insn_start (rtx insn)
4352 if (GET_MODE (insn) == SImode)
4354 while (GET_MODE (insn) != QImode)
4355 insn = NEXT_INSN (insn);
4357 return NEXT_INSN (insn);
4360 /* Return INSN if it is of TYPE_MCLD. Alternatively, if INSN is the start of
4361 a three-insn bundle, see if one of them is a load and return that if so.
4362 Return NULL_RTX if the insn does not contain loads. */
4363 static rtx
4364 find_load (rtx insn)
4366 if (get_attr_type (insn) == TYPE_MCLD)
4367 return insn;
4368 if (GET_MODE (insn) != SImode)
4369 return NULL_RTX;
4370 do {
4371 insn = NEXT_INSN (insn);
4372 if ((GET_MODE (insn) == SImode || GET_MODE (insn) == QImode)
4373 && get_attr_type (insn) == TYPE_MCLD)
4374 return insn;
4375 } while (GET_MODE (insn) != QImode);
4376 return NULL_RTX;
4379 /* We use the machine specific reorg pass for emitting CSYNC instructions
4380 after conditional branches as needed.
4382 The Blackfin is unusual in that a code sequence like
4383 if cc jump label
4384 r0 = (p0)
4385 may speculatively perform the load even if the condition isn't true. This
4386 happens for a branch that is predicted not taken, because the pipeline
4387 isn't flushed or stalled, so the early stages of the following instructions,
4388 which perform the memory reference, are allowed to execute before the
4389 jump condition is evaluated.
4390 Therefore, we must insert additional instructions in all places where this
4391 could lead to incorrect behavior. The manual recommends CSYNC, while
4392 VDSP seems to use NOPs (even though its corresponding compiler option is
4393 named CSYNC).
4395 When optimizing for speed, we emit NOPs, which seems faster than a CSYNC.
4396 When optimizing for size, we turn the branch into a predicted taken one.
4397 This may be slower due to mispredicts, but saves code size. */
4399 static void
4400 bfin_reorg (void)
4402 rtx insn, next;
4403 rtx last_condjump = NULL_RTX;
4404 int cycles_since_jump = INT_MAX;
4406 /* We are freeing block_for_insn in the toplev to keep compatibility
4407 with old MDEP_REORGS that are not CFG based. Recompute it now. */
4408 compute_bb_for_insn ();
4410 if (bfin_flag_schedule_insns2)
4412 splitting_for_sched = 1;
4413 split_all_insns ();
4414 splitting_for_sched = 0;
4416 timevar_push (TV_SCHED2);
4417 schedule_insns ();
4418 timevar_pop (TV_SCHED2);
4420 /* Examine the schedule and insert nops as necessary for 64-bit parallel
4421 instructions. */
4422 bfin_gen_bundles ();
4425 df_analyze ();
4427 /* Doloop optimization */
4428 if (cfun->machine->has_hardware_loops)
4429 bfin_reorg_loops (dump_file);
4431 if (! TARGET_SPECLD_ANOMALY && ! TARGET_CSYNC_ANOMALY)
4432 return;
4434 /* First pass: find predicted-false branches; if something after them
4435 needs nops, insert them or change the branch to predict true. */
4436 for (insn = get_insns (); insn; insn = next)
4438 rtx pat;
4440 next = find_next_insn_start (insn);
4442 if (NOTE_P (insn) || BARRIER_P (insn) || LABEL_P (insn))
4443 continue;
4445 pat = PATTERN (insn);
4446 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
4447 || GET_CODE (pat) == ASM_INPUT || GET_CODE (pat) == ADDR_VEC
4448 || GET_CODE (pat) == ADDR_DIFF_VEC || asm_noperands (pat) >= 0)
4449 continue;
4451 if (JUMP_P (insn))
4453 if (any_condjump_p (insn)
4454 && ! cbranch_predicted_taken_p (insn))
4456 last_condjump = insn;
4457 cycles_since_jump = 0;
4459 else
4460 cycles_since_jump = INT_MAX;
4462 else if (INSN_P (insn))
4464 rtx load_insn = find_load (insn);
4465 enum attr_type type = type_for_anomaly (insn);
4466 int delay_needed = 0;
4467 if (cycles_since_jump < INT_MAX)
4468 cycles_since_jump++;
4470 if (load_insn && TARGET_SPECLD_ANOMALY)
4472 if (trapping_loads_p (load_insn))
4473 delay_needed = 3;
4475 else if (type == TYPE_SYNC && TARGET_CSYNC_ANOMALY)
4476 delay_needed = 4;
4478 if (delay_needed > cycles_since_jump)
4480 rtx pat;
4481 int num_clobbers;
4482 rtx *op = recog_data.operand;
4484 delay_needed -= cycles_since_jump;
4486 extract_insn (last_condjump);
4487 if (optimize_size)
4489 pat = gen_cbranch_predicted_taken (op[0], op[1], op[2],
4490 op[3]);
4491 cycles_since_jump = INT_MAX;
4493 else
4494 /* Do not adjust cycles_since_jump in this case, so that
4495 we'll increase the number of NOPs for a subsequent insn
4496 if necessary. */
4497 pat = gen_cbranch_with_nops (op[0], op[1], op[2], op[3],
4498 GEN_INT (delay_needed));
4499 PATTERN (last_condjump) = pat;
4500 INSN_CODE (last_condjump) = recog (pat, insn, &num_clobbers);
4504 /* Second pass: for predicted-true branches, see if anything at the
4505 branch destination needs extra nops. */
4506 if (! TARGET_CSYNC_ANOMALY)
4507 return;
4509 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4511 if (JUMP_P (insn)
4512 && any_condjump_p (insn)
4513 && (INSN_CODE (insn) == CODE_FOR_cbranch_predicted_taken
4514 || cbranch_predicted_taken_p (insn)))
4516 rtx target = JUMP_LABEL (insn);
4517 rtx label = target;
4518 cycles_since_jump = 0;
4519 for (; target && cycles_since_jump < 3; target = NEXT_INSN (target))
4521 rtx pat;
4523 if (NOTE_P (target) || BARRIER_P (target) || LABEL_P (target))
4524 continue;
4526 pat = PATTERN (target);
4527 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
4528 || GET_CODE (pat) == ASM_INPUT || GET_CODE (pat) == ADDR_VEC
4529 || GET_CODE (pat) == ADDR_DIFF_VEC || asm_noperands (pat) >= 0)
4530 continue;
4532 if (INSN_P (target))
4534 enum attr_type type = type_for_anomaly (target);
4535 int delay_needed = 0;
4536 if (cycles_since_jump < INT_MAX)
4537 cycles_since_jump++;
4539 if (type == TYPE_SYNC && TARGET_CSYNC_ANOMALY)
4540 delay_needed = 2;
4542 if (delay_needed > cycles_since_jump)
4544 rtx prev = prev_real_insn (label);
4545 delay_needed -= cycles_since_jump;
4546 if (dump_file)
4547 fprintf (dump_file, "Adding %d nops after %d\n",
4548 delay_needed, INSN_UID (label));
4549 if (JUMP_P (prev)
4550 && INSN_CODE (prev) == CODE_FOR_cbranch_with_nops)
4552 rtx x;
4553 HOST_WIDE_INT v;
4555 if (dump_file)
4556 fprintf (dump_file,
4557 "Reducing nops on insn %d.\n",
4558 INSN_UID (prev));
4559 x = PATTERN (prev);
4560 x = XVECEXP (x, 0, 1);
4561 v = INTVAL (XVECEXP (x, 0, 0)) - delay_needed;
4562 XVECEXP (x, 0, 0) = GEN_INT (v);
4564 while (delay_needed-- > 0)
4565 emit_insn_after (gen_nop (), label);
4566 break;
4573 if (bfin_flag_var_tracking)
4575 timevar_push (TV_VAR_TRACKING);
4576 variable_tracking_main ();
4577 reorder_var_tracking_notes ();
4578 timevar_pop (TV_VAR_TRACKING);
4580 df_finish_pass (false);
4583 /* Handle interrupt_handler, exception_handler and nmi_handler function
4584 attributes; arguments as in struct attribute_spec.handler. */
4586 static tree
4587 handle_int_attribute (tree *node, tree name,
4588 tree args ATTRIBUTE_UNUSED,
4589 int flags ATTRIBUTE_UNUSED,
4590 bool *no_add_attrs)
4592 tree x = *node;
4593 if (TREE_CODE (x) == FUNCTION_DECL)
4594 x = TREE_TYPE (x);
4596 if (TREE_CODE (x) != FUNCTION_TYPE)
4598 warning (OPT_Wattributes, "%qs attribute only applies to functions",
4599 IDENTIFIER_POINTER (name));
4600 *no_add_attrs = true;
4602 else if (funkind (x) != SUBROUTINE)
4603 error ("multiple function type attributes specified");
4605 return NULL_TREE;
4608 /* Return 0 if the attributes for two types are incompatible, 1 if they
4609 are compatible, and 2 if they are nearly compatible (which causes a
4610 warning to be generated). */
4612 static int
4613 bfin_comp_type_attributes (const_tree type1, const_tree type2)
4615 e_funkind kind1, kind2;
4617 if (TREE_CODE (type1) != FUNCTION_TYPE)
4618 return 1;
4620 kind1 = funkind (type1);
4621 kind2 = funkind (type2);
4623 if (kind1 != kind2)
4624 return 0;
4626 /* Check for mismatched modifiers */
4627 if (!lookup_attribute ("nesting", TYPE_ATTRIBUTES (type1))
4628 != !lookup_attribute ("nesting", TYPE_ATTRIBUTES (type2)))
4629 return 0;
4631 if (!lookup_attribute ("saveall", TYPE_ATTRIBUTES (type1))
4632 != !lookup_attribute ("saveall", TYPE_ATTRIBUTES (type2)))
4633 return 0;
4635 if (!lookup_attribute ("kspisusp", TYPE_ATTRIBUTES (type1))
4636 != !lookup_attribute ("kspisusp", TYPE_ATTRIBUTES (type2)))
4637 return 0;
4639 if (!lookup_attribute ("longcall", TYPE_ATTRIBUTES (type1))
4640 != !lookup_attribute ("longcall", TYPE_ATTRIBUTES (type2)))
4641 return 0;
4643 return 1;
4646 /* Handle a "longcall" or "shortcall" attribute; arguments as in
4647 struct attribute_spec.handler. */
4649 static tree
4650 bfin_handle_longcall_attribute (tree *node, tree name,
4651 tree args ATTRIBUTE_UNUSED,
4652 int flags ATTRIBUTE_UNUSED,
4653 bool *no_add_attrs)
4655 if (TREE_CODE (*node) != FUNCTION_TYPE
4656 && TREE_CODE (*node) != FIELD_DECL
4657 && TREE_CODE (*node) != TYPE_DECL)
4659 warning (OPT_Wattributes, "`%s' attribute only applies to functions",
4660 IDENTIFIER_POINTER (name));
4661 *no_add_attrs = true;
4664 if ((strcmp (IDENTIFIER_POINTER (name), "longcall") == 0
4665 && lookup_attribute ("shortcall", TYPE_ATTRIBUTES (*node)))
4666 || (strcmp (IDENTIFIER_POINTER (name), "shortcall") == 0
4667 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (*node))))
4669 warning (OPT_Wattributes,
4670 "can't apply both longcall and shortcall attributes to the same function");
4671 *no_add_attrs = true;
4674 return NULL_TREE;
4677 /* Handle a "l1_text" attribute; arguments as in
4678 struct attribute_spec.handler. */
4680 static tree
4681 bfin_handle_l1_text_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4682 int ARG_UNUSED (flags), bool *no_add_attrs)
4684 tree decl = *node;
4686 if (TREE_CODE (decl) != FUNCTION_DECL)
4688 error ("`%s' attribute only applies to functions",
4689 IDENTIFIER_POINTER (name));
4690 *no_add_attrs = true;
4693 /* The decl may have already been given a section attribute
4694 from a previous declaration. Ensure they match. */
4695 else if (DECL_SECTION_NAME (decl) != NULL_TREE
4696 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
4697 ".l1.text") != 0)
4699 error ("section of %q+D conflicts with previous declaration",
4700 decl);
4701 *no_add_attrs = true;
4703 else
4704 DECL_SECTION_NAME (decl) = build_string (9, ".l1.text");
4706 return NULL_TREE;
4709 /* Handle a "l1_data", "l1_data_A" or "l1_data_B" attribute;
4710 arguments as in struct attribute_spec.handler. */
4712 static tree
4713 bfin_handle_l1_data_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4714 int ARG_UNUSED (flags), bool *no_add_attrs)
4716 tree decl = *node;
4718 if (TREE_CODE (decl) != VAR_DECL)
4720 error ("`%s' attribute only applies to variables",
4721 IDENTIFIER_POINTER (name));
4722 *no_add_attrs = true;
4724 else if (current_function_decl != NULL_TREE
4725 && !TREE_STATIC (decl))
4727 error ("`%s' attribute cannot be specified for local variables",
4728 IDENTIFIER_POINTER (name));
4729 *no_add_attrs = true;
4731 else
4733 const char *section_name;
4735 if (strcmp (IDENTIFIER_POINTER (name), "l1_data") == 0)
4736 section_name = ".l1.data";
4737 else if (strcmp (IDENTIFIER_POINTER (name), "l1_data_A") == 0)
4738 section_name = ".l1.data.A";
4739 else if (strcmp (IDENTIFIER_POINTER (name), "l1_data_B") == 0)
4740 section_name = ".l1.data.B";
4741 else
4742 gcc_unreachable ();
4744 /* The decl may have already been given a section attribute
4745 from a previous declaration. Ensure they match. */
4746 if (DECL_SECTION_NAME (decl) != NULL_TREE
4747 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
4748 section_name) != 0)
4750 error ("section of %q+D conflicts with previous declaration",
4751 decl);
4752 *no_add_attrs = true;
4754 else
4755 DECL_SECTION_NAME (decl)
4756 = build_string (strlen (section_name) + 1, section_name);
4759 return NULL_TREE;
4762 /* Table of valid machine attributes. */
4763 const struct attribute_spec bfin_attribute_table[] =
4765 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
4766 { "interrupt_handler", 0, 0, false, true, true, handle_int_attribute },
4767 { "exception_handler", 0, 0, false, true, true, handle_int_attribute },
4768 { "nmi_handler", 0, 0, false, true, true, handle_int_attribute },
4769 { "nesting", 0, 0, false, true, true, NULL },
4770 { "kspisusp", 0, 0, false, true, true, NULL },
4771 { "saveall", 0, 0, false, true, true, NULL },
4772 { "longcall", 0, 0, false, true, true, bfin_handle_longcall_attribute },
4773 { "shortcall", 0, 0, false, true, true, bfin_handle_longcall_attribute },
4774 { "l1_text", 0, 0, true, false, false, bfin_handle_l1_text_attribute },
4775 { "l1_data", 0, 0, true, false, false, bfin_handle_l1_data_attribute },
4776 { "l1_data_A", 0, 0, true, false, false, bfin_handle_l1_data_attribute },
4777 { "l1_data_B", 0, 0, true, false, false, bfin_handle_l1_data_attribute },
4778 { NULL, 0, 0, false, false, false, NULL }
4781 /* Implementation of TARGET_ASM_INTEGER. When using FD-PIC, we need to
4782 tell the assembler to generate pointers to function descriptors in
4783 some cases. */
4785 static bool
4786 bfin_assemble_integer (rtx value, unsigned int size, int aligned_p)
4788 if (TARGET_FDPIC && size == UNITS_PER_WORD)
4790 if (GET_CODE (value) == SYMBOL_REF
4791 && SYMBOL_REF_FUNCTION_P (value))
4793 fputs ("\t.picptr\tfuncdesc(", asm_out_file);
4794 output_addr_const (asm_out_file, value);
4795 fputs (")\n", asm_out_file);
4796 return true;
4798 if (!aligned_p)
4800 /* We've set the unaligned SI op to NULL, so we always have to
4801 handle the unaligned case here. */
4802 assemble_integer_with_op ("\t.4byte\t", value);
4803 return true;
4806 return default_assemble_integer (value, size, aligned_p);
4809 /* Output the assembler code for a thunk function. THUNK_DECL is the
4810 declaration for the thunk function itself, FUNCTION is the decl for
4811 the target function. DELTA is an immediate constant offset to be
4812 added to THIS. If VCALL_OFFSET is nonzero, the word at
4813 *(*this + vcall_offset) should be added to THIS. */
4815 static void
4816 bfin_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
4817 tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
4818 HOST_WIDE_INT vcall_offset, tree function)
4820 rtx xops[3];
4821 /* The this parameter is passed as the first argument. */
4822 rtx this = gen_rtx_REG (Pmode, REG_R0);
4824 /* Adjust the this parameter by a fixed constant. */
4825 if (delta)
4827 xops[1] = this;
4828 if (delta >= -64 && delta <= 63)
4830 xops[0] = GEN_INT (delta);
4831 output_asm_insn ("%1 += %0;", xops);
4833 else if (delta >= -128 && delta < -64)
4835 xops[0] = GEN_INT (delta + 64);
4836 output_asm_insn ("%1 += -64; %1 += %0;", xops);
4838 else if (delta > 63 && delta <= 126)
4840 xops[0] = GEN_INT (delta - 63);
4841 output_asm_insn ("%1 += 63; %1 += %0;", xops);
4843 else
4845 xops[0] = GEN_INT (delta);
4846 output_asm_insn ("r3.l = %h0; r3.h = %d0; %1 = %1 + r3;", xops);
4850 /* Adjust the this parameter by a value stored in the vtable. */
4851 if (vcall_offset)
4853 rtx p2tmp = gen_rtx_REG (Pmode, REG_P2);
4854 rtx tmp = gen_rtx_REG (Pmode, REG_R3);
4856 xops[1] = tmp;
4857 xops[2] = p2tmp;
4858 output_asm_insn ("%2 = r0; %2 = [%2];", xops);
4860 /* Adjust the this parameter. */
4861 xops[0] = gen_rtx_MEM (Pmode, plus_constant (p2tmp, vcall_offset));
4862 if (!memory_operand (xops[0], Pmode))
4864 rtx tmp2 = gen_rtx_REG (Pmode, REG_P1);
4865 xops[0] = GEN_INT (vcall_offset);
4866 xops[1] = tmp2;
4867 output_asm_insn ("%h1 = %h0; %d1 = %d0; %2 = %2 + %1", xops);
4868 xops[0] = gen_rtx_MEM (Pmode, p2tmp);
4870 xops[2] = this;
4871 output_asm_insn ("%1 = %0; %2 = %2 + %1;", xops);
4874 xops[0] = XEXP (DECL_RTL (function), 0);
4875 if (1 || !flag_pic || (*targetm.binds_local_p) (function))
4876 output_asm_insn ("jump.l\t%P0", xops);
4879 /* Codes for all the Blackfin builtins. */
4880 enum bfin_builtins
4882 BFIN_BUILTIN_CSYNC,
4883 BFIN_BUILTIN_SSYNC,
4884 BFIN_BUILTIN_COMPOSE_2X16,
4885 BFIN_BUILTIN_EXTRACTLO,
4886 BFIN_BUILTIN_EXTRACTHI,
4888 BFIN_BUILTIN_SSADD_2X16,
4889 BFIN_BUILTIN_SSSUB_2X16,
4890 BFIN_BUILTIN_SSADDSUB_2X16,
4891 BFIN_BUILTIN_SSSUBADD_2X16,
4892 BFIN_BUILTIN_MULT_2X16,
4893 BFIN_BUILTIN_MULTR_2X16,
4894 BFIN_BUILTIN_NEG_2X16,
4895 BFIN_BUILTIN_ABS_2X16,
4896 BFIN_BUILTIN_MIN_2X16,
4897 BFIN_BUILTIN_MAX_2X16,
4899 BFIN_BUILTIN_SSADD_1X16,
4900 BFIN_BUILTIN_SSSUB_1X16,
4901 BFIN_BUILTIN_MULT_1X16,
4902 BFIN_BUILTIN_MULTR_1X16,
4903 BFIN_BUILTIN_NORM_1X16,
4904 BFIN_BUILTIN_NEG_1X16,
4905 BFIN_BUILTIN_ABS_1X16,
4906 BFIN_BUILTIN_MIN_1X16,
4907 BFIN_BUILTIN_MAX_1X16,
4909 BFIN_BUILTIN_SUM_2X16,
4910 BFIN_BUILTIN_DIFFHL_2X16,
4911 BFIN_BUILTIN_DIFFLH_2X16,
4913 BFIN_BUILTIN_SSADD_1X32,
4914 BFIN_BUILTIN_SSSUB_1X32,
4915 BFIN_BUILTIN_NORM_1X32,
4916 BFIN_BUILTIN_ROUND_1X32,
4917 BFIN_BUILTIN_NEG_1X32,
4918 BFIN_BUILTIN_ABS_1X32,
4919 BFIN_BUILTIN_MIN_1X32,
4920 BFIN_BUILTIN_MAX_1X32,
4921 BFIN_BUILTIN_MULT_1X32,
4922 BFIN_BUILTIN_MULT_1X32X32,
4923 BFIN_BUILTIN_MULT_1X32X32NS,
4925 BFIN_BUILTIN_MULHISILL,
4926 BFIN_BUILTIN_MULHISILH,
4927 BFIN_BUILTIN_MULHISIHL,
4928 BFIN_BUILTIN_MULHISIHH,
4930 BFIN_BUILTIN_LSHIFT_1X16,
4931 BFIN_BUILTIN_LSHIFT_2X16,
4932 BFIN_BUILTIN_SSASHIFT_1X16,
4933 BFIN_BUILTIN_SSASHIFT_2X16,
4934 BFIN_BUILTIN_SSASHIFT_1X32,
4936 BFIN_BUILTIN_CPLX_MUL_16,
4937 BFIN_BUILTIN_CPLX_MAC_16,
4938 BFIN_BUILTIN_CPLX_MSU_16,
4940 BFIN_BUILTIN_MAX
4943 #define def_builtin(NAME, TYPE, CODE) \
4944 do { \
4945 add_builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, \
4946 NULL, NULL_TREE); \
4947 } while (0)
4949 /* Set up all builtin functions for this target. */
4950 static void
4951 bfin_init_builtins (void)
4953 tree V2HI_type_node = build_vector_type_for_mode (intHI_type_node, V2HImode);
4954 tree void_ftype_void
4955 = build_function_type (void_type_node, void_list_node);
4956 tree short_ftype_short
4957 = build_function_type_list (short_integer_type_node, short_integer_type_node,
4958 NULL_TREE);
4959 tree short_ftype_int_int
4960 = build_function_type_list (short_integer_type_node, integer_type_node,
4961 integer_type_node, NULL_TREE);
4962 tree int_ftype_int_int
4963 = build_function_type_list (integer_type_node, integer_type_node,
4964 integer_type_node, NULL_TREE);
4965 tree int_ftype_int
4966 = build_function_type_list (integer_type_node, integer_type_node,
4967 NULL_TREE);
4968 tree short_ftype_int
4969 = build_function_type_list (short_integer_type_node, integer_type_node,
4970 NULL_TREE);
4971 tree int_ftype_v2hi_v2hi
4972 = build_function_type_list (integer_type_node, V2HI_type_node,
4973 V2HI_type_node, NULL_TREE);
4974 tree v2hi_ftype_v2hi_v2hi
4975 = build_function_type_list (V2HI_type_node, V2HI_type_node,
4976 V2HI_type_node, NULL_TREE);
4977 tree v2hi_ftype_v2hi_v2hi_v2hi
4978 = build_function_type_list (V2HI_type_node, V2HI_type_node,
4979 V2HI_type_node, V2HI_type_node, NULL_TREE);
4980 tree v2hi_ftype_int_int
4981 = build_function_type_list (V2HI_type_node, integer_type_node,
4982 integer_type_node, NULL_TREE);
4983 tree v2hi_ftype_v2hi_int
4984 = build_function_type_list (V2HI_type_node, V2HI_type_node,
4985 integer_type_node, NULL_TREE);
4986 tree int_ftype_short_short
4987 = build_function_type_list (integer_type_node, short_integer_type_node,
4988 short_integer_type_node, NULL_TREE);
4989 tree v2hi_ftype_v2hi
4990 = build_function_type_list (V2HI_type_node, V2HI_type_node, NULL_TREE);
4991 tree short_ftype_v2hi
4992 = build_function_type_list (short_integer_type_node, V2HI_type_node,
4993 NULL_TREE);
4995 /* Add the remaining MMX insns with somewhat more complicated types. */
4996 def_builtin ("__builtin_bfin_csync", void_ftype_void, BFIN_BUILTIN_CSYNC);
4997 def_builtin ("__builtin_bfin_ssync", void_ftype_void, BFIN_BUILTIN_SSYNC);
4999 def_builtin ("__builtin_bfin_compose_2x16", v2hi_ftype_int_int,
5000 BFIN_BUILTIN_COMPOSE_2X16);
5001 def_builtin ("__builtin_bfin_extract_hi", short_ftype_v2hi,
5002 BFIN_BUILTIN_EXTRACTHI);
5003 def_builtin ("__builtin_bfin_extract_lo", short_ftype_v2hi,
5004 BFIN_BUILTIN_EXTRACTLO);
5006 def_builtin ("__builtin_bfin_min_fr2x16", v2hi_ftype_v2hi_v2hi,
5007 BFIN_BUILTIN_MIN_2X16);
5008 def_builtin ("__builtin_bfin_max_fr2x16", v2hi_ftype_v2hi_v2hi,
5009 BFIN_BUILTIN_MAX_2X16);
5011 def_builtin ("__builtin_bfin_add_fr2x16", v2hi_ftype_v2hi_v2hi,
5012 BFIN_BUILTIN_SSADD_2X16);
5013 def_builtin ("__builtin_bfin_sub_fr2x16", v2hi_ftype_v2hi_v2hi,
5014 BFIN_BUILTIN_SSSUB_2X16);
5015 def_builtin ("__builtin_bfin_dspaddsubsat", v2hi_ftype_v2hi_v2hi,
5016 BFIN_BUILTIN_SSADDSUB_2X16);
5017 def_builtin ("__builtin_bfin_dspsubaddsat", v2hi_ftype_v2hi_v2hi,
5018 BFIN_BUILTIN_SSSUBADD_2X16);
5019 def_builtin ("__builtin_bfin_mult_fr2x16", v2hi_ftype_v2hi_v2hi,
5020 BFIN_BUILTIN_MULT_2X16);
5021 def_builtin ("__builtin_bfin_multr_fr2x16", v2hi_ftype_v2hi_v2hi,
5022 BFIN_BUILTIN_MULTR_2X16);
5023 def_builtin ("__builtin_bfin_negate_fr2x16", v2hi_ftype_v2hi,
5024 BFIN_BUILTIN_NEG_2X16);
5025 def_builtin ("__builtin_bfin_abs_fr2x16", v2hi_ftype_v2hi,
5026 BFIN_BUILTIN_ABS_2X16);
5028 def_builtin ("__builtin_bfin_add_fr1x16", short_ftype_int_int,
5029 BFIN_BUILTIN_SSADD_1X16);
5030 def_builtin ("__builtin_bfin_sub_fr1x16", short_ftype_int_int,
5031 BFIN_BUILTIN_SSSUB_1X16);
5032 def_builtin ("__builtin_bfin_mult_fr1x16", short_ftype_int_int,
5033 BFIN_BUILTIN_MULT_1X16);
5034 def_builtin ("__builtin_bfin_multr_fr1x16", short_ftype_int_int,
5035 BFIN_BUILTIN_MULTR_1X16);
5036 def_builtin ("__builtin_bfin_negate_fr1x16", short_ftype_short,
5037 BFIN_BUILTIN_NEG_1X16);
5038 def_builtin ("__builtin_bfin_abs_fr1x16", short_ftype_short,
5039 BFIN_BUILTIN_ABS_1X16);
5040 def_builtin ("__builtin_bfin_norm_fr1x16", short_ftype_int,
5041 BFIN_BUILTIN_NORM_1X16);
5043 def_builtin ("__builtin_bfin_sum_fr2x16", short_ftype_v2hi,
5044 BFIN_BUILTIN_SUM_2X16);
5045 def_builtin ("__builtin_bfin_diff_hl_fr2x16", short_ftype_v2hi,
5046 BFIN_BUILTIN_DIFFHL_2X16);
5047 def_builtin ("__builtin_bfin_diff_lh_fr2x16", short_ftype_v2hi,
5048 BFIN_BUILTIN_DIFFLH_2X16);
5050 def_builtin ("__builtin_bfin_mulhisill", int_ftype_v2hi_v2hi,
5051 BFIN_BUILTIN_MULHISILL);
5052 def_builtin ("__builtin_bfin_mulhisihl", int_ftype_v2hi_v2hi,
5053 BFIN_BUILTIN_MULHISIHL);
5054 def_builtin ("__builtin_bfin_mulhisilh", int_ftype_v2hi_v2hi,
5055 BFIN_BUILTIN_MULHISILH);
5056 def_builtin ("__builtin_bfin_mulhisihh", int_ftype_v2hi_v2hi,
5057 BFIN_BUILTIN_MULHISIHH);
5059 def_builtin ("__builtin_bfin_add_fr1x32", int_ftype_int_int,
5060 BFIN_BUILTIN_SSADD_1X32);
5061 def_builtin ("__builtin_bfin_sub_fr1x32", int_ftype_int_int,
5062 BFIN_BUILTIN_SSSUB_1X32);
5063 def_builtin ("__builtin_bfin_negate_fr1x32", int_ftype_int,
5064 BFIN_BUILTIN_NEG_1X32);
5065 def_builtin ("__builtin_bfin_abs_fr1x32", int_ftype_int,
5066 BFIN_BUILTIN_ABS_1X32);
5067 def_builtin ("__builtin_bfin_norm_fr1x32", short_ftype_int,
5068 BFIN_BUILTIN_NORM_1X32);
5069 def_builtin ("__builtin_bfin_round_fr1x32", short_ftype_int,
5070 BFIN_BUILTIN_ROUND_1X32);
5071 def_builtin ("__builtin_bfin_mult_fr1x32", int_ftype_short_short,
5072 BFIN_BUILTIN_MULT_1X32);
5073 def_builtin ("__builtin_bfin_mult_fr1x32x32", int_ftype_int_int,
5074 BFIN_BUILTIN_MULT_1X32X32);
5075 def_builtin ("__builtin_bfin_mult_fr1x32x32NS", int_ftype_int_int,
5076 BFIN_BUILTIN_MULT_1X32X32NS);
5078 /* Shifts. */
5079 def_builtin ("__builtin_bfin_shl_fr1x16", short_ftype_int_int,
5080 BFIN_BUILTIN_SSASHIFT_1X16);
5081 def_builtin ("__builtin_bfin_shl_fr2x16", v2hi_ftype_v2hi_int,
5082 BFIN_BUILTIN_SSASHIFT_2X16);
5083 def_builtin ("__builtin_bfin_lshl_fr1x16", short_ftype_int_int,
5084 BFIN_BUILTIN_LSHIFT_1X16);
5085 def_builtin ("__builtin_bfin_lshl_fr2x16", v2hi_ftype_v2hi_int,
5086 BFIN_BUILTIN_LSHIFT_2X16);
5087 def_builtin ("__builtin_bfin_shl_fr1x32", int_ftype_int_int,
5088 BFIN_BUILTIN_SSASHIFT_1X32);
5090 /* Complex numbers. */
5091 def_builtin ("__builtin_bfin_cmplx_mul", v2hi_ftype_v2hi_v2hi,
5092 BFIN_BUILTIN_CPLX_MUL_16);
5093 def_builtin ("__builtin_bfin_cmplx_mac", v2hi_ftype_v2hi_v2hi_v2hi,
5094 BFIN_BUILTIN_CPLX_MAC_16);
5095 def_builtin ("__builtin_bfin_cmplx_msu", v2hi_ftype_v2hi_v2hi_v2hi,
5096 BFIN_BUILTIN_CPLX_MSU_16);
5100 struct builtin_description
5102 const enum insn_code icode;
5103 const char *const name;
5104 const enum bfin_builtins code;
5105 int macflag;
5108 static const struct builtin_description bdesc_2arg[] =
5110 { CODE_FOR_composev2hi, "__builtin_bfin_compose_2x16", BFIN_BUILTIN_COMPOSE_2X16, -1 },
5112 { CODE_FOR_ssashiftv2hi3, "__builtin_bfin_shl_fr2x16", BFIN_BUILTIN_SSASHIFT_2X16, -1 },
5113 { CODE_FOR_ssashifthi3, "__builtin_bfin_shl_fr1x16", BFIN_BUILTIN_SSASHIFT_1X16, -1 },
5114 { CODE_FOR_lshiftv2hi3, "__builtin_bfin_lshl_fr2x16", BFIN_BUILTIN_LSHIFT_2X16, -1 },
5115 { CODE_FOR_lshifthi3, "__builtin_bfin_lshl_fr1x16", BFIN_BUILTIN_LSHIFT_1X16, -1 },
5116 { CODE_FOR_ssashiftsi3, "__builtin_bfin_shl_fr1x32", BFIN_BUILTIN_SSASHIFT_1X32, -1 },
5118 { CODE_FOR_sminhi3, "__builtin_bfin_min_fr1x16", BFIN_BUILTIN_MIN_1X16, -1 },
5119 { CODE_FOR_smaxhi3, "__builtin_bfin_max_fr1x16", BFIN_BUILTIN_MAX_1X16, -1 },
5120 { CODE_FOR_ssaddhi3, "__builtin_bfin_add_fr1x16", BFIN_BUILTIN_SSADD_1X16, -1 },
5121 { CODE_FOR_sssubhi3, "__builtin_bfin_sub_fr1x16", BFIN_BUILTIN_SSSUB_1X16, -1 },
5123 { CODE_FOR_sminsi3, "__builtin_bfin_min_fr1x32", BFIN_BUILTIN_MIN_1X32, -1 },
5124 { CODE_FOR_smaxsi3, "__builtin_bfin_max_fr1x32", BFIN_BUILTIN_MAX_1X32, -1 },
5125 { CODE_FOR_ssaddsi3, "__builtin_bfin_add_fr1x32", BFIN_BUILTIN_SSADD_1X32, -1 },
5126 { CODE_FOR_sssubsi3, "__builtin_bfin_sub_fr1x32", BFIN_BUILTIN_SSSUB_1X32, -1 },
5128 { CODE_FOR_sminv2hi3, "__builtin_bfin_min_fr2x16", BFIN_BUILTIN_MIN_2X16, -1 },
5129 { CODE_FOR_smaxv2hi3, "__builtin_bfin_max_fr2x16", BFIN_BUILTIN_MAX_2X16, -1 },
5130 { CODE_FOR_ssaddv2hi3, "__builtin_bfin_add_fr2x16", BFIN_BUILTIN_SSADD_2X16, -1 },
5131 { CODE_FOR_sssubv2hi3, "__builtin_bfin_sub_fr2x16", BFIN_BUILTIN_SSSUB_2X16, -1 },
5132 { CODE_FOR_ssaddsubv2hi3, "__builtin_bfin_dspaddsubsat", BFIN_BUILTIN_SSADDSUB_2X16, -1 },
5133 { CODE_FOR_sssubaddv2hi3, "__builtin_bfin_dspsubaddsat", BFIN_BUILTIN_SSSUBADD_2X16, -1 },
5135 { CODE_FOR_flag_mulhisi, "__builtin_bfin_mult_fr1x32", BFIN_BUILTIN_MULT_1X32, MACFLAG_NONE },
5136 { CODE_FOR_flag_mulhi, "__builtin_bfin_mult_fr1x16", BFIN_BUILTIN_MULT_1X16, MACFLAG_T },
5137 { CODE_FOR_flag_mulhi, "__builtin_bfin_multr_fr1x16", BFIN_BUILTIN_MULTR_1X16, MACFLAG_NONE },
5138 { CODE_FOR_flag_mulv2hi, "__builtin_bfin_mult_fr2x16", BFIN_BUILTIN_MULT_2X16, MACFLAG_T },
5139 { CODE_FOR_flag_mulv2hi, "__builtin_bfin_multr_fr2x16", BFIN_BUILTIN_MULTR_2X16, MACFLAG_NONE }
5142 static const struct builtin_description bdesc_1arg[] =
5144 { CODE_FOR_signbitshi2, "__builtin_bfin_norm_fr1x16", BFIN_BUILTIN_NORM_1X16, 0 },
5145 { CODE_FOR_ssneghi2, "__builtin_bfin_negate_fr1x16", BFIN_BUILTIN_NEG_1X16, 0 },
5146 { CODE_FOR_abshi2, "__builtin_bfin_abs_fr1x16", BFIN_BUILTIN_ABS_1X16, 0 },
5148 { CODE_FOR_signbitssi2, "__builtin_bfin_norm_fr1x32", BFIN_BUILTIN_NORM_1X32, 0 },
5149 { CODE_FOR_ssroundsi2, "__builtin_bfin_round_fr1x32", BFIN_BUILTIN_ROUND_1X32, 0 },
5150 { CODE_FOR_ssnegsi2, "__builtin_bfin_negate_fr1x32", BFIN_BUILTIN_NEG_1X32, 0 },
5151 { CODE_FOR_ssabssi2, "__builtin_bfin_abs_fr1x32", BFIN_BUILTIN_ABS_1X32, 0 },
5153 { CODE_FOR_movv2hi_hi_low, "__builtin_bfin_extract_lo", BFIN_BUILTIN_EXTRACTLO, 0 },
5154 { CODE_FOR_movv2hi_hi_high, "__builtin_bfin_extract_hi", BFIN_BUILTIN_EXTRACTHI, 0 },
5155 { CODE_FOR_ssnegv2hi2, "__builtin_bfin_negate_fr2x16", BFIN_BUILTIN_NEG_2X16, 0 },
5156 { CODE_FOR_ssabsv2hi2, "__builtin_bfin_abs_fr2x16", BFIN_BUILTIN_ABS_2X16, 0 }
5159 /* Errors in the source file can cause expand_expr to return const0_rtx
5160 where we expect a vector. To avoid crashing, use one of the vector
5161 clear instructions. */
5162 static rtx
5163 safe_vector_operand (rtx x, enum machine_mode mode)
5165 if (x != const0_rtx)
5166 return x;
5167 x = gen_reg_rtx (SImode);
5169 emit_insn (gen_movsi (x, CONST0_RTX (SImode)));
5170 return gen_lowpart (mode, x);
5173 /* Subroutine of bfin_expand_builtin to take care of binop insns. MACFLAG is -1
5174 if this is a normal binary op, or one of the MACFLAG_xxx constants. */
5176 static rtx
5177 bfin_expand_binop_builtin (enum insn_code icode, tree exp, rtx target,
5178 int macflag)
5180 rtx pat;
5181 tree arg0 = CALL_EXPR_ARG (exp, 0);
5182 tree arg1 = CALL_EXPR_ARG (exp, 1);
5183 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5184 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5185 enum machine_mode op0mode = GET_MODE (op0);
5186 enum machine_mode op1mode = GET_MODE (op1);
5187 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5188 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5189 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5191 if (VECTOR_MODE_P (mode0))
5192 op0 = safe_vector_operand (op0, mode0);
5193 if (VECTOR_MODE_P (mode1))
5194 op1 = safe_vector_operand (op1, mode1);
5196 if (! target
5197 || GET_MODE (target) != tmode
5198 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5199 target = gen_reg_rtx (tmode);
5201 if ((op0mode == SImode || op0mode == VOIDmode) && mode0 == HImode)
5203 op0mode = HImode;
5204 op0 = gen_lowpart (HImode, op0);
5206 if ((op1mode == SImode || op1mode == VOIDmode) && mode1 == HImode)
5208 op1mode = HImode;
5209 op1 = gen_lowpart (HImode, op1);
5211 /* In case the insn wants input operands in modes different from
5212 the result, abort. */
5213 gcc_assert ((op0mode == mode0 || op0mode == VOIDmode)
5214 && (op1mode == mode1 || op1mode == VOIDmode));
5216 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5217 op0 = copy_to_mode_reg (mode0, op0);
5218 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5219 op1 = copy_to_mode_reg (mode1, op1);
5221 if (macflag == -1)
5222 pat = GEN_FCN (icode) (target, op0, op1);
5223 else
5224 pat = GEN_FCN (icode) (target, op0, op1, GEN_INT (macflag));
5225 if (! pat)
5226 return 0;
5228 emit_insn (pat);
5229 return target;
5232 /* Subroutine of bfin_expand_builtin to take care of unop insns. */
5234 static rtx
5235 bfin_expand_unop_builtin (enum insn_code icode, tree exp,
5236 rtx target)
5238 rtx pat;
5239 tree arg0 = CALL_EXPR_ARG (exp, 0);
5240 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5241 enum machine_mode op0mode = GET_MODE (op0);
5242 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5243 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5245 if (! target
5246 || GET_MODE (target) != tmode
5247 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5248 target = gen_reg_rtx (tmode);
5250 if (VECTOR_MODE_P (mode0))
5251 op0 = safe_vector_operand (op0, mode0);
5253 if (op0mode == SImode && mode0 == HImode)
5255 op0mode = HImode;
5256 op0 = gen_lowpart (HImode, op0);
5258 gcc_assert (op0mode == mode0 || op0mode == VOIDmode);
5260 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5261 op0 = copy_to_mode_reg (mode0, op0);
5263 pat = GEN_FCN (icode) (target, op0);
5264 if (! pat)
5265 return 0;
5266 emit_insn (pat);
5267 return target;
5270 /* Expand an expression EXP that calls a built-in function,
5271 with result going to TARGET if that's convenient
5272 (and in mode MODE if that's convenient).
5273 SUBTARGET may be used as the target for computing one of EXP's operands.
5274 IGNORE is nonzero if the value is to be ignored. */
5276 static rtx
5277 bfin_expand_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
5278 rtx subtarget ATTRIBUTE_UNUSED,
5279 enum machine_mode mode ATTRIBUTE_UNUSED,
5280 int ignore ATTRIBUTE_UNUSED)
5282 size_t i;
5283 enum insn_code icode;
5284 const struct builtin_description *d;
5285 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
5286 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5287 tree arg0, arg1, arg2;
5288 rtx op0, op1, op2, accvec, pat, tmp1, tmp2, a0reg, a1reg;
5289 enum machine_mode tmode, mode0;
5291 switch (fcode)
5293 case BFIN_BUILTIN_CSYNC:
5294 emit_insn (gen_csync ());
5295 return 0;
5296 case BFIN_BUILTIN_SSYNC:
5297 emit_insn (gen_ssync ());
5298 return 0;
5300 case BFIN_BUILTIN_DIFFHL_2X16:
5301 case BFIN_BUILTIN_DIFFLH_2X16:
5302 case BFIN_BUILTIN_SUM_2X16:
5303 arg0 = CALL_EXPR_ARG (exp, 0);
5304 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5305 icode = (fcode == BFIN_BUILTIN_DIFFHL_2X16 ? CODE_FOR_subhilov2hi3
5306 : fcode == BFIN_BUILTIN_DIFFLH_2X16 ? CODE_FOR_sublohiv2hi3
5307 : CODE_FOR_ssaddhilov2hi3);
5308 tmode = insn_data[icode].operand[0].mode;
5309 mode0 = insn_data[icode].operand[1].mode;
5311 if (! target
5312 || GET_MODE (target) != tmode
5313 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5314 target = gen_reg_rtx (tmode);
5316 if (VECTOR_MODE_P (mode0))
5317 op0 = safe_vector_operand (op0, mode0);
5319 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5320 op0 = copy_to_mode_reg (mode0, op0);
5322 pat = GEN_FCN (icode) (target, op0, op0);
5323 if (! pat)
5324 return 0;
5325 emit_insn (pat);
5326 return target;
5328 case BFIN_BUILTIN_MULT_1X32X32:
5329 case BFIN_BUILTIN_MULT_1X32X32NS:
5330 arg0 = CALL_EXPR_ARG (exp, 0);
5331 arg1 = CALL_EXPR_ARG (exp, 1);
5332 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5333 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5334 if (! target
5335 || !register_operand (target, SImode))
5336 target = gen_reg_rtx (SImode);
5338 a1reg = gen_rtx_REG (PDImode, REG_A1);
5339 a0reg = gen_rtx_REG (PDImode, REG_A0);
5340 tmp1 = gen_lowpart (V2HImode, op0);
5341 tmp2 = gen_lowpart (V2HImode, op1);
5342 emit_insn (gen_flag_macinit1hi (a1reg,
5343 gen_lowpart (HImode, op0),
5344 gen_lowpart (HImode, op1),
5345 GEN_INT (MACFLAG_FU)));
5346 emit_insn (gen_lshrpdi3 (a1reg, a1reg, GEN_INT (16)));
5348 if (fcode == BFIN_BUILTIN_MULT_1X32X32)
5349 emit_insn (gen_flag_mul_macv2hi_parts_acconly (a0reg, a1reg, tmp1, tmp2,
5350 const1_rtx, const1_rtx,
5351 const1_rtx, const0_rtx, a1reg,
5352 const0_rtx, GEN_INT (MACFLAG_NONE),
5353 GEN_INT (MACFLAG_M)));
5354 else
5356 /* For saturating multiplication, there's exactly one special case
5357 to be handled: multiplying the smallest negative value with
5358 itself. Due to shift correction in fractional multiplies, this
5359 can overflow. Iff this happens, OP2 will contain 1, which, when
5360 added in 32 bits to the smallest negative, wraps to the largest
5361 positive, which is the result we want. */
5362 op2 = gen_reg_rtx (V2HImode);
5363 emit_insn (gen_packv2hi (op2, tmp1, tmp2, const0_rtx, const0_rtx));
5364 emit_insn (gen_movsibi (gen_rtx_REG (BImode, REG_CC),
5365 gen_lowpart (SImode, op2)));
5366 emit_insn (gen_flag_mul_macv2hi_parts_acconly_andcc0 (a0reg, a1reg, tmp1, tmp2,
5367 const1_rtx, const1_rtx,
5368 const1_rtx, const0_rtx, a1reg,
5369 const0_rtx, GEN_INT (MACFLAG_NONE),
5370 GEN_INT (MACFLAG_M)));
5371 op2 = gen_reg_rtx (SImode);
5372 emit_insn (gen_movbisi (op2, gen_rtx_REG (BImode, REG_CC)));
5374 emit_insn (gen_flag_machi_parts_acconly (a1reg, tmp2, tmp1,
5375 const1_rtx, const0_rtx,
5376 a1reg, const0_rtx, GEN_INT (MACFLAG_M)));
5377 emit_insn (gen_ashrpdi3 (a1reg, a1reg, GEN_INT (15)));
5378 emit_insn (gen_sum_of_accumulators (target, a0reg, a0reg, a1reg));
5379 if (fcode == BFIN_BUILTIN_MULT_1X32X32NS)
5380 emit_insn (gen_addsi3 (target, target, op2));
5381 return target;
5383 case BFIN_BUILTIN_CPLX_MUL_16:
5384 arg0 = CALL_EXPR_ARG (exp, 0);
5385 arg1 = CALL_EXPR_ARG (exp, 1);
5386 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5387 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5388 accvec = gen_reg_rtx (V2PDImode);
5390 if (! target
5391 || GET_MODE (target) != V2HImode
5392 || ! (*insn_data[icode].operand[0].predicate) (target, V2HImode))
5393 target = gen_reg_rtx (tmode);
5394 if (! register_operand (op0, GET_MODE (op0)))
5395 op0 = copy_to_mode_reg (GET_MODE (op0), op0);
5396 if (! register_operand (op1, GET_MODE (op1)))
5397 op1 = copy_to_mode_reg (GET_MODE (op1), op1);
5399 emit_insn (gen_flag_macinit1v2hi_parts (accvec, op0, op1, const0_rtx,
5400 const0_rtx, const0_rtx,
5401 const1_rtx, GEN_INT (MACFLAG_NONE)));
5402 emit_insn (gen_flag_macv2hi_parts (target, op0, op1, const1_rtx,
5403 const1_rtx, const1_rtx,
5404 const0_rtx, accvec, const1_rtx, const0_rtx,
5405 GEN_INT (MACFLAG_NONE), accvec));
5407 return target;
5409 case BFIN_BUILTIN_CPLX_MAC_16:
5410 case BFIN_BUILTIN_CPLX_MSU_16:
5411 arg0 = CALL_EXPR_ARG (exp, 0);
5412 arg1 = CALL_EXPR_ARG (exp, 1);
5413 arg2 = CALL_EXPR_ARG (exp, 2);
5414 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5415 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5416 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5417 accvec = gen_reg_rtx (V2PDImode);
5419 if (! target
5420 || GET_MODE (target) != V2HImode
5421 || ! (*insn_data[icode].operand[0].predicate) (target, V2HImode))
5422 target = gen_reg_rtx (tmode);
5423 if (! register_operand (op1, GET_MODE (op1)))
5424 op1 = copy_to_mode_reg (GET_MODE (op1), op1);
5425 if (! register_operand (op2, GET_MODE (op2)))
5426 op2 = copy_to_mode_reg (GET_MODE (op2), op2);
5428 tmp1 = gen_reg_rtx (SImode);
5429 tmp2 = gen_reg_rtx (SImode);
5430 emit_insn (gen_ashlsi3 (tmp1, gen_lowpart (SImode, op0), GEN_INT (16)));
5431 emit_move_insn (tmp2, gen_lowpart (SImode, op0));
5432 emit_insn (gen_movstricthi_1 (gen_lowpart (HImode, tmp2), const0_rtx));
5433 emit_insn (gen_load_accumulator_pair (accvec, tmp1, tmp2));
5434 emit_insn (gen_flag_macv2hi_parts_acconly (accvec, op1, op2, const0_rtx,
5435 const0_rtx, const0_rtx,
5436 const1_rtx, accvec, const0_rtx,
5437 const0_rtx,
5438 GEN_INT (MACFLAG_W32)));
5439 tmp1 = (fcode == BFIN_BUILTIN_CPLX_MAC_16 ? const1_rtx : const0_rtx);
5440 tmp2 = (fcode == BFIN_BUILTIN_CPLX_MAC_16 ? const0_rtx : const1_rtx);
5441 emit_insn (gen_flag_macv2hi_parts (target, op1, op2, const1_rtx,
5442 const1_rtx, const1_rtx,
5443 const0_rtx, accvec, tmp1, tmp2,
5444 GEN_INT (MACFLAG_NONE), accvec));
5446 return target;
5448 default:
5449 break;
5452 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5453 if (d->code == fcode)
5454 return bfin_expand_binop_builtin (d->icode, exp, target,
5455 d->macflag);
5457 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5458 if (d->code == fcode)
5459 return bfin_expand_unop_builtin (d->icode, exp, target);
5461 gcc_unreachable ();
5464 #undef TARGET_INIT_BUILTINS
5465 #define TARGET_INIT_BUILTINS bfin_init_builtins
5467 #undef TARGET_EXPAND_BUILTIN
5468 #define TARGET_EXPAND_BUILTIN bfin_expand_builtin
5470 #undef TARGET_ASM_GLOBALIZE_LABEL
5471 #define TARGET_ASM_GLOBALIZE_LABEL bfin_globalize_label
5473 #undef TARGET_ASM_FILE_START
5474 #define TARGET_ASM_FILE_START output_file_start
5476 #undef TARGET_ATTRIBUTE_TABLE
5477 #define TARGET_ATTRIBUTE_TABLE bfin_attribute_table
5479 #undef TARGET_COMP_TYPE_ATTRIBUTES
5480 #define TARGET_COMP_TYPE_ATTRIBUTES bfin_comp_type_attributes
5482 #undef TARGET_RTX_COSTS
5483 #define TARGET_RTX_COSTS bfin_rtx_costs
5485 #undef TARGET_ADDRESS_COST
5486 #define TARGET_ADDRESS_COST bfin_address_cost
5488 #undef TARGET_ASM_INTEGER
5489 #define TARGET_ASM_INTEGER bfin_assemble_integer
5491 #undef TARGET_MACHINE_DEPENDENT_REORG
5492 #define TARGET_MACHINE_DEPENDENT_REORG bfin_reorg
5494 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
5495 #define TARGET_FUNCTION_OK_FOR_SIBCALL bfin_function_ok_for_sibcall
5497 #undef TARGET_ASM_OUTPUT_MI_THUNK
5498 #define TARGET_ASM_OUTPUT_MI_THUNK bfin_output_mi_thunk
5499 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
5500 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
5502 #undef TARGET_SCHED_ADJUST_COST
5503 #define TARGET_SCHED_ADJUST_COST bfin_adjust_cost
5505 #undef TARGET_SCHED_ISSUE_RATE
5506 #define TARGET_SCHED_ISSUE_RATE bfin_issue_rate
5508 #undef TARGET_PROMOTE_PROTOTYPES
5509 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
5510 #undef TARGET_PROMOTE_FUNCTION_ARGS
5511 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_const_tree_true
5512 #undef TARGET_PROMOTE_FUNCTION_RETURN
5513 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_const_tree_true
5515 #undef TARGET_ARG_PARTIAL_BYTES
5516 #define TARGET_ARG_PARTIAL_BYTES bfin_arg_partial_bytes
5518 #undef TARGET_PASS_BY_REFERENCE
5519 #define TARGET_PASS_BY_REFERENCE bfin_pass_by_reference
5521 #undef TARGET_SETUP_INCOMING_VARARGS
5522 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
5524 #undef TARGET_STRUCT_VALUE_RTX
5525 #define TARGET_STRUCT_VALUE_RTX bfin_struct_value_rtx
5527 #undef TARGET_VECTOR_MODE_SUPPORTED_P
5528 #define TARGET_VECTOR_MODE_SUPPORTED_P bfin_vector_mode_supported_p
5530 #undef TARGET_HANDLE_OPTION
5531 #define TARGET_HANDLE_OPTION bfin_handle_option
5533 #undef TARGET_DEFAULT_TARGET_FLAGS
5534 #define TARGET_DEFAULT_TARGET_FLAGS TARGET_DEFAULT
5536 #undef TARGET_SECONDARY_RELOAD
5537 #define TARGET_SECONDARY_RELOAD bfin_secondary_reload
5539 #undef TARGET_DELEGITIMIZE_ADDRESS
5540 #define TARGET_DELEGITIMIZE_ADDRESS bfin_delegitimize_address
5542 #undef TARGET_CANNOT_FORCE_CONST_MEM
5543 #define TARGET_CANNOT_FORCE_CONST_MEM bfin_cannot_force_const_mem
5545 struct gcc_target targetm = TARGET_INITIALIZER;