]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/bfin/bfin.c
gcc/
[thirdparty/gcc.git] / gcc / config / bfin / bfin.c
1 /* The Blackfin code generation auxiliary output file.
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Analog Devices.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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 "insn-config.h"
29 #include "insn-codes.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "varasm.h"
39 #include "calls.h"
40 #include "flags.h"
41 #include "except.h"
42 #include "function.h"
43 #include "target.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "emit-rtl.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "diagnostic-core.h"
51 #include "recog.h"
52 #include "optabs.h"
53 #include "predict.h"
54 #include "dominance.h"
55 #include "cfg.h"
56 #include "cfgrtl.h"
57 #include "cfganal.h"
58 #include "lcm.h"
59 #include "cfgbuild.h"
60 #include "cfgcleanup.h"
61 #include "basic-block.h"
62 #include "plugin-api.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "langhooks.h"
66 #include "bfin-protos.h"
67 #include "tm_p.h"
68 #include "tm-preds.h"
69 #include "tm-constrs.h"
70 #include "gt-bfin.h"
71 #include "timevar.h"
72 #include "df.h"
73 #include "sel-sched.h"
74 #include "hw-doloop.h"
75 #include "opts.h"
76 #include "dumpfile.h"
77 #include "builtins.h"
78
79 #include "target-def.h"
80
81 /* A C structure for machine-specific, per-function data.
82 This is added to the cfun structure. */
83 struct GTY(()) machine_function
84 {
85 /* Set if we are notified by the doloop pass that a hardware loop
86 was created. */
87 int has_hardware_loops;
88
89 /* Set if we create a memcpy pattern that uses loop registers. */
90 int has_loopreg_clobber;
91 };
92
93 /* RTX for condition code flag register and RETS register */
94 extern GTY(()) rtx bfin_cc_rtx;
95 extern GTY(()) rtx bfin_rets_rtx;
96 rtx bfin_cc_rtx, bfin_rets_rtx;
97
98 int max_arg_registers = 0;
99
100 /* Arrays used when emitting register names. */
101 const char *short_reg_names[] = SHORT_REGISTER_NAMES;
102 const char *high_reg_names[] = HIGH_REGISTER_NAMES;
103 const char *dregs_pair_names[] = DREGS_PAIR_NAMES;
104 const char *byte_reg_names[] = BYTE_REGISTER_NAMES;
105
106 static int arg_regs[] = FUNCTION_ARG_REGISTERS;
107 static int ret_regs[] = FUNCTION_RETURN_REGISTERS;
108
109 int splitting_for_sched, splitting_loops;
110
111 static void
112 bfin_globalize_label (FILE *stream, const char *name)
113 {
114 fputs (".global ", stream);
115 assemble_name (stream, name);
116 fputc (';',stream);
117 fputc ('\n',stream);
118 }
119
120 static void
121 output_file_start (void)
122 {
123 FILE *file = asm_out_file;
124 int i;
125
126 fprintf (file, ".file \"%s\";\n", LOCATION_FILE (input_location));
127
128 for (i = 0; arg_regs[i] >= 0; i++)
129 ;
130 max_arg_registers = i; /* how many arg reg used */
131 }
132
133 /* Examine machine-dependent attributes of function type FUNTYPE and return its
134 type. See the definition of E_FUNKIND. */
135
136 static e_funkind
137 funkind (const_tree funtype)
138 {
139 tree attrs = TYPE_ATTRIBUTES (funtype);
140 if (lookup_attribute ("interrupt_handler", attrs))
141 return INTERRUPT_HANDLER;
142 else if (lookup_attribute ("exception_handler", attrs))
143 return EXCPT_HANDLER;
144 else if (lookup_attribute ("nmi_handler", attrs))
145 return NMI_HANDLER;
146 else
147 return SUBROUTINE;
148 }
149 \f
150 /* Legitimize PIC addresses. If the address is already position-independent,
151 we return ORIG. Newly generated position-independent addresses go into a
152 reg. This is REG if nonzero, otherwise we allocate register(s) as
153 necessary. PICREG is the register holding the pointer to the PIC offset
154 table. */
155
156 static rtx
157 legitimize_pic_address (rtx orig, rtx reg, rtx picreg)
158 {
159 rtx addr = orig;
160 rtx new_rtx = orig;
161
162 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
163 {
164 int unspec;
165 rtx tmp;
166
167 if (TARGET_ID_SHARED_LIBRARY)
168 unspec = UNSPEC_MOVE_PIC;
169 else if (GET_CODE (addr) == SYMBOL_REF
170 && SYMBOL_REF_FUNCTION_P (addr))
171 unspec = UNSPEC_FUNCDESC_GOT17M4;
172 else
173 unspec = UNSPEC_MOVE_FDPIC;
174
175 if (reg == 0)
176 {
177 gcc_assert (can_create_pseudo_p ());
178 reg = gen_reg_rtx (Pmode);
179 }
180
181 tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), unspec);
182 new_rtx = gen_const_mem (Pmode, gen_rtx_PLUS (Pmode, picreg, tmp));
183
184 emit_move_insn (reg, new_rtx);
185 if (picreg == pic_offset_table_rtx)
186 crtl->uses_pic_offset_table = 1;
187 return reg;
188 }
189
190 else if (GET_CODE (addr) == CONST || GET_CODE (addr) == PLUS)
191 {
192 rtx base;
193
194 if (GET_CODE (addr) == CONST)
195 {
196 addr = XEXP (addr, 0);
197 gcc_assert (GET_CODE (addr) == PLUS);
198 }
199
200 if (XEXP (addr, 0) == picreg)
201 return orig;
202
203 if (reg == 0)
204 {
205 gcc_assert (can_create_pseudo_p ());
206 reg = gen_reg_rtx (Pmode);
207 }
208
209 base = legitimize_pic_address (XEXP (addr, 0), reg, picreg);
210 addr = legitimize_pic_address (XEXP (addr, 1),
211 base == reg ? NULL_RTX : reg,
212 picreg);
213
214 if (GET_CODE (addr) == CONST_INT)
215 {
216 gcc_assert (! reload_in_progress && ! reload_completed);
217 addr = force_reg (Pmode, addr);
218 }
219
220 if (GET_CODE (addr) == PLUS && CONSTANT_P (XEXP (addr, 1)))
221 {
222 base = gen_rtx_PLUS (Pmode, base, XEXP (addr, 0));
223 addr = XEXP (addr, 1);
224 }
225
226 return gen_rtx_PLUS (Pmode, base, addr);
227 }
228
229 return new_rtx;
230 }
231 \f
232 /* Stack frame layout. */
233
234 /* For a given REGNO, determine whether it must be saved in the function
235 prologue. IS_INTHANDLER specifies whether we're generating a normal
236 prologue or an interrupt/exception one. */
237 static bool
238 must_save_p (bool is_inthandler, unsigned regno)
239 {
240 if (D_REGNO_P (regno))
241 {
242 bool is_eh_return_reg = false;
243 if (crtl->calls_eh_return)
244 {
245 unsigned j;
246 for (j = 0; ; j++)
247 {
248 unsigned test = EH_RETURN_DATA_REGNO (j);
249 if (test == INVALID_REGNUM)
250 break;
251 if (test == regno)
252 is_eh_return_reg = true;
253 }
254 }
255
256 return (is_eh_return_reg
257 || (df_regs_ever_live_p (regno)
258 && !fixed_regs[regno]
259 && (is_inthandler || !call_used_regs[regno])));
260 }
261 else if (P_REGNO_P (regno))
262 {
263 return ((df_regs_ever_live_p (regno)
264 && !fixed_regs[regno]
265 && (is_inthandler || !call_used_regs[regno]))
266 || (is_inthandler
267 && (ENABLE_WA_05000283 || ENABLE_WA_05000315)
268 && regno == REG_P5)
269 || (!TARGET_FDPIC
270 && regno == PIC_OFFSET_TABLE_REGNUM
271 && (crtl->uses_pic_offset_table
272 || (TARGET_ID_SHARED_LIBRARY && !crtl->is_leaf))));
273 }
274 else
275 return ((is_inthandler || !call_used_regs[regno])
276 && (df_regs_ever_live_p (regno)
277 || (!leaf_function_p () && call_used_regs[regno])));
278
279 }
280
281 /* Compute the number of DREGS to save with a push_multiple operation.
282 This could include registers that aren't modified in the function,
283 since push_multiple only takes a range of registers.
284 If IS_INTHANDLER, then everything that is live must be saved, even
285 if normally call-clobbered.
286 If CONSECUTIVE, return the number of registers we can save in one
287 instruction with a push/pop multiple instruction. */
288
289 static int
290 n_dregs_to_save (bool is_inthandler, bool consecutive)
291 {
292 int count = 0;
293 unsigned i;
294
295 for (i = REG_R7 + 1; i-- != REG_R0;)
296 {
297 if (must_save_p (is_inthandler, i))
298 count++;
299 else if (consecutive)
300 return count;
301 }
302 return count;
303 }
304
305 /* Like n_dregs_to_save, but compute number of PREGS to save. */
306
307 static int
308 n_pregs_to_save (bool is_inthandler, bool consecutive)
309 {
310 int count = 0;
311 unsigned i;
312
313 for (i = REG_P5 + 1; i-- != REG_P0;)
314 if (must_save_p (is_inthandler, i))
315 count++;
316 else if (consecutive)
317 return count;
318 return count;
319 }
320
321 /* Determine if we are going to save the frame pointer in the prologue. */
322
323 static bool
324 must_save_fp_p (void)
325 {
326 return df_regs_ever_live_p (REG_FP);
327 }
328
329 /* Determine if we are going to save the RETS register. */
330 static bool
331 must_save_rets_p (void)
332 {
333 return df_regs_ever_live_p (REG_RETS);
334 }
335
336 static bool
337 stack_frame_needed_p (void)
338 {
339 /* EH return puts a new return address into the frame using an
340 address relative to the frame pointer. */
341 if (crtl->calls_eh_return)
342 return true;
343 return frame_pointer_needed;
344 }
345
346 /* Emit code to save registers in the prologue. SAVEALL is nonzero if we
347 must save all registers; this is used for interrupt handlers.
348 SPREG contains (reg:SI REG_SP). IS_INTHANDLER is true if we're doing
349 this for an interrupt (or exception) handler. */
350
351 static void
352 expand_prologue_reg_save (rtx spreg, int saveall, bool is_inthandler)
353 {
354 rtx predec1 = gen_rtx_PRE_DEC (SImode, spreg);
355 rtx predec = gen_rtx_MEM (SImode, predec1);
356 int ndregs = saveall ? 8 : n_dregs_to_save (is_inthandler, false);
357 int npregs = saveall ? 6 : n_pregs_to_save (is_inthandler, false);
358 int ndregs_consec = saveall ? 8 : n_dregs_to_save (is_inthandler, true);
359 int npregs_consec = saveall ? 6 : n_pregs_to_save (is_inthandler, true);
360 int dregno, pregno;
361 int total_consec = ndregs_consec + npregs_consec;
362 int i, d_to_save;
363
364 if (saveall || is_inthandler)
365 {
366 rtx_insn *insn = emit_move_insn (predec, gen_rtx_REG (SImode, REG_ASTAT));
367
368 RTX_FRAME_RELATED_P (insn) = 1;
369 for (dregno = REG_LT0; dregno <= REG_LB1; dregno++)
370 if (! crtl->is_leaf
371 || cfun->machine->has_hardware_loops
372 || cfun->machine->has_loopreg_clobber
373 || (ENABLE_WA_05000257
374 && (dregno == REG_LC0 || dregno == REG_LC1)))
375 {
376 insn = emit_move_insn (predec, gen_rtx_REG (SImode, dregno));
377 RTX_FRAME_RELATED_P (insn) = 1;
378 }
379 }
380
381 if (total_consec != 0)
382 {
383 rtx_insn *insn;
384 rtx val = GEN_INT (-total_consec * 4);
385 rtx pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_consec + 2));
386
387 XVECEXP (pat, 0, 0) = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, val),
388 UNSPEC_PUSH_MULTIPLE);
389 XVECEXP (pat, 0, total_consec + 1) = gen_rtx_SET (spreg,
390 gen_rtx_PLUS (Pmode,
391 spreg,
392 val));
393 RTX_FRAME_RELATED_P (XVECEXP (pat, 0, total_consec + 1)) = 1;
394 d_to_save = ndregs_consec;
395 dregno = REG_R7 + 1 - ndregs_consec;
396 pregno = REG_P5 + 1 - npregs_consec;
397 for (i = 0; i < total_consec; i++)
398 {
399 rtx memref = gen_rtx_MEM (word_mode,
400 gen_rtx_PLUS (Pmode, spreg,
401 GEN_INT (- i * 4 - 4)));
402 rtx subpat;
403 if (d_to_save > 0)
404 {
405 subpat = gen_rtx_SET (memref, gen_rtx_REG (word_mode, dregno++));
406 d_to_save--;
407 }
408 else
409 {
410 subpat = gen_rtx_SET (memref, gen_rtx_REG (word_mode, pregno++));
411 }
412 XVECEXP (pat, 0, i + 1) = subpat;
413 RTX_FRAME_RELATED_P (subpat) = 1;
414 }
415 insn = emit_insn (pat);
416 RTX_FRAME_RELATED_P (insn) = 1;
417 }
418
419 for (dregno = REG_R0; ndregs != ndregs_consec; dregno++)
420 {
421 if (must_save_p (is_inthandler, dregno))
422 {
423 rtx_insn *insn =
424 emit_move_insn (predec, gen_rtx_REG (word_mode, dregno));
425 RTX_FRAME_RELATED_P (insn) = 1;
426 ndregs--;
427 }
428 }
429 for (pregno = REG_P0; npregs != npregs_consec; pregno++)
430 {
431 if (must_save_p (is_inthandler, pregno))
432 {
433 rtx_insn *insn =
434 emit_move_insn (predec, gen_rtx_REG (word_mode, pregno));
435 RTX_FRAME_RELATED_P (insn) = 1;
436 npregs--;
437 }
438 }
439 for (i = REG_P7 + 1; i < REG_CC; i++)
440 if (saveall
441 || (is_inthandler
442 && (df_regs_ever_live_p (i)
443 || (!leaf_function_p () && call_used_regs[i]))))
444 {
445 rtx_insn *insn;
446 if (i == REG_A0 || i == REG_A1)
447 insn = emit_move_insn (gen_rtx_MEM (PDImode, predec1),
448 gen_rtx_REG (PDImode, i));
449 else
450 insn = emit_move_insn (predec, gen_rtx_REG (SImode, i));
451 RTX_FRAME_RELATED_P (insn) = 1;
452 }
453 }
454
455 /* Emit code to restore registers in the epilogue. SAVEALL is nonzero if we
456 must save all registers; this is used for interrupt handlers.
457 SPREG contains (reg:SI REG_SP). IS_INTHANDLER is true if we're doing
458 this for an interrupt (or exception) handler. */
459
460 static void
461 expand_epilogue_reg_restore (rtx spreg, bool saveall, bool is_inthandler)
462 {
463 rtx postinc1 = gen_rtx_POST_INC (SImode, spreg);
464 rtx postinc = gen_rtx_MEM (SImode, postinc1);
465
466 int ndregs = saveall ? 8 : n_dregs_to_save (is_inthandler, false);
467 int npregs = saveall ? 6 : n_pregs_to_save (is_inthandler, false);
468 int ndregs_consec = saveall ? 8 : n_dregs_to_save (is_inthandler, true);
469 int npregs_consec = saveall ? 6 : n_pregs_to_save (is_inthandler, true);
470 int total_consec = ndregs_consec + npregs_consec;
471 int i, regno;
472 rtx_insn *insn;
473
474 /* A slightly crude technique to stop flow from trying to delete "dead"
475 insns. */
476 MEM_VOLATILE_P (postinc) = 1;
477
478 for (i = REG_CC - 1; i > REG_P7; i--)
479 if (saveall
480 || (is_inthandler
481 && (df_regs_ever_live_p (i)
482 || (!leaf_function_p () && call_used_regs[i]))))
483 {
484 if (i == REG_A0 || i == REG_A1)
485 {
486 rtx mem = gen_rtx_MEM (PDImode, postinc1);
487 MEM_VOLATILE_P (mem) = 1;
488 emit_move_insn (gen_rtx_REG (PDImode, i), mem);
489 }
490 else
491 emit_move_insn (gen_rtx_REG (SImode, i), postinc);
492 }
493
494 regno = REG_P5 - npregs_consec;
495 for (; npregs != npregs_consec; regno--)
496 {
497 if (must_save_p (is_inthandler, regno))
498 {
499 emit_move_insn (gen_rtx_REG (word_mode, regno), postinc);
500 npregs--;
501 }
502 }
503 regno = REG_R7 - ndregs_consec;
504 for (; ndregs != ndregs_consec; regno--)
505 {
506 if (must_save_p (is_inthandler, regno))
507 {
508 emit_move_insn (gen_rtx_REG (word_mode, regno), postinc);
509 ndregs--;
510 }
511 }
512
513 if (total_consec != 0)
514 {
515 rtx pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_consec + 1));
516 XVECEXP (pat, 0, 0)
517 = gen_rtx_SET (spreg, gen_rtx_PLUS (Pmode, spreg,
518 GEN_INT (total_consec * 4)));
519
520 if (npregs_consec > 0)
521 regno = REG_P5 + 1;
522 else
523 regno = REG_R7 + 1;
524
525 for (i = 0; i < total_consec; i++)
526 {
527 rtx addr = (i > 0
528 ? gen_rtx_PLUS (Pmode, spreg, GEN_INT (i * 4))
529 : spreg);
530 rtx memref = gen_rtx_MEM (word_mode, addr);
531
532 regno--;
533 XVECEXP (pat, 0, i + 1)
534 = gen_rtx_SET (gen_rtx_REG (word_mode, regno), memref);
535
536 if (npregs_consec > 0)
537 {
538 if (--npregs_consec == 0)
539 regno = REG_R7 + 1;
540 }
541 }
542
543 insn = emit_insn (pat);
544 RTX_FRAME_RELATED_P (insn) = 1;
545 }
546 if (saveall || is_inthandler)
547 {
548 for (regno = REG_LB1; regno >= REG_LT0; regno--)
549 if (! crtl->is_leaf
550 || cfun->machine->has_hardware_loops
551 || cfun->machine->has_loopreg_clobber
552 || (ENABLE_WA_05000257 && (regno == REG_LC0 || regno == REG_LC1)))
553 emit_move_insn (gen_rtx_REG (SImode, regno), postinc);
554
555 emit_move_insn (gen_rtx_REG (SImode, REG_ASTAT), postinc);
556 }
557 }
558
559 /* Perform any needed actions needed for a function that is receiving a
560 variable number of arguments.
561
562 CUM is as above.
563
564 MODE and TYPE are the mode and type of the current parameter.
565
566 PRETEND_SIZE is a variable that should be set to the amount of stack
567 that must be pushed by the prolog to pretend that our caller pushed
568 it.
569
570 Normally, this macro will push all remaining incoming registers on the
571 stack and set PRETEND_SIZE to the length of the registers pushed.
572
573 Blackfin specific :
574 - VDSP C compiler manual (our ABI) says that a variable args function
575 should save the R0, R1 and R2 registers in the stack.
576 - The caller will always leave space on the stack for the
577 arguments that are passed in registers, so we dont have
578 to leave any extra space.
579 - now, the vastart pointer can access all arguments from the stack. */
580
581 static void
582 setup_incoming_varargs (cumulative_args_t cum,
583 machine_mode mode ATTRIBUTE_UNUSED,
584 tree type ATTRIBUTE_UNUSED, int *pretend_size,
585 int no_rtl)
586 {
587 rtx mem;
588 int i;
589
590 if (no_rtl)
591 return;
592
593 /* The move for named arguments will be generated automatically by the
594 compiler. We need to generate the move rtx for the unnamed arguments
595 if they are in the first 3 words. We assume at least 1 named argument
596 exists, so we never generate [ARGP] = R0 here. */
597
598 for (i = get_cumulative_args (cum)->words + 1; i < max_arg_registers; i++)
599 {
600 mem = gen_rtx_MEM (Pmode,
601 plus_constant (Pmode, arg_pointer_rtx,
602 (i * UNITS_PER_WORD)));
603 emit_move_insn (mem, gen_rtx_REG (Pmode, i));
604 }
605
606 *pretend_size = 0;
607 }
608
609 /* Value should be nonzero if functions must have frame pointers.
610 Zero means the frame pointer need not be set up (and parms may
611 be accessed via the stack pointer) in functions that seem suitable. */
612
613 static bool
614 bfin_frame_pointer_required (void)
615 {
616 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
617
618 if (fkind != SUBROUTINE)
619 return true;
620
621 /* We turn on -fomit-frame-pointer if -momit-leaf-frame-pointer is used,
622 so we have to override it for non-leaf functions. */
623 if (TARGET_OMIT_LEAF_FRAME_POINTER && ! crtl->is_leaf)
624 return true;
625
626 return false;
627 }
628
629 /* Return the number of registers pushed during the prologue. */
630
631 static int
632 n_regs_saved_by_prologue (void)
633 {
634 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
635 bool is_inthandler = fkind != SUBROUTINE;
636 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
637 bool all = (lookup_attribute ("saveall", attrs) != NULL_TREE
638 || (is_inthandler && !crtl->is_leaf));
639 int ndregs = all ? 8 : n_dregs_to_save (is_inthandler, false);
640 int npregs = all ? 6 : n_pregs_to_save (is_inthandler, false);
641 int n = ndregs + npregs;
642 int i;
643
644 if (all || stack_frame_needed_p ())
645 n += 2;
646 else
647 {
648 if (must_save_fp_p ())
649 n++;
650 if (must_save_rets_p ())
651 n++;
652 }
653
654 if (fkind != SUBROUTINE || all)
655 {
656 /* Increment once for ASTAT. */
657 n++;
658 if (! crtl->is_leaf
659 || cfun->machine->has_hardware_loops
660 || cfun->machine->has_loopreg_clobber)
661 {
662 n += 6;
663 }
664 }
665
666 if (fkind != SUBROUTINE)
667 {
668 /* RETE/X/N. */
669 if (lookup_attribute ("nesting", attrs))
670 n++;
671 }
672
673 for (i = REG_P7 + 1; i < REG_CC; i++)
674 if (all
675 || (fkind != SUBROUTINE
676 && (df_regs_ever_live_p (i)
677 || (!leaf_function_p () && call_used_regs[i]))))
678 n += i == REG_A0 || i == REG_A1 ? 2 : 1;
679
680 return n;
681 }
682
683 /* Given FROM and TO register numbers, say whether this elimination is
684 allowed. Frame pointer elimination is automatically handled.
685
686 All other eliminations are valid. */
687
688 static bool
689 bfin_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
690 {
691 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
692 }
693
694 /* Return the offset between two registers, one to be eliminated, and the other
695 its replacement, at the start of a routine. */
696
697 HOST_WIDE_INT
698 bfin_initial_elimination_offset (int from, int to)
699 {
700 HOST_WIDE_INT offset = 0;
701
702 if (from == ARG_POINTER_REGNUM)
703 offset = n_regs_saved_by_prologue () * 4;
704
705 if (to == STACK_POINTER_REGNUM)
706 {
707 if (crtl->outgoing_args_size >= FIXED_STACK_AREA)
708 offset += crtl->outgoing_args_size;
709 else if (crtl->outgoing_args_size)
710 offset += FIXED_STACK_AREA;
711
712 offset += get_frame_size ();
713 }
714
715 return offset;
716 }
717
718 /* Emit code to load a constant CONSTANT into register REG; setting
719 RTX_FRAME_RELATED_P on all insns we generate if RELATED is true.
720 Make sure that the insns we generate need not be split. */
721
722 static void
723 frame_related_constant_load (rtx reg, HOST_WIDE_INT constant, bool related)
724 {
725 rtx_insn *insn;
726 rtx cst = GEN_INT (constant);
727
728 if (constant >= -32768 && constant < 65536)
729 insn = emit_move_insn (reg, cst);
730 else
731 {
732 /* We don't call split_load_immediate here, since dwarf2out.c can get
733 confused about some of the more clever sequences it can generate. */
734 insn = emit_insn (gen_movsi_high (reg, cst));
735 if (related)
736 RTX_FRAME_RELATED_P (insn) = 1;
737 insn = emit_insn (gen_movsi_low (reg, reg, cst));
738 }
739 if (related)
740 RTX_FRAME_RELATED_P (insn) = 1;
741 }
742
743 /* Generate efficient code to add a value to a P register.
744 Set RTX_FRAME_RELATED_P on the generated insns if FRAME is nonzero.
745 EPILOGUE_P is zero if this function is called for prologue,
746 otherwise it's nonzero. And it's less than zero if this is for
747 sibcall epilogue. */
748
749 static void
750 add_to_reg (rtx reg, HOST_WIDE_INT value, int frame, int epilogue_p)
751 {
752 if (value == 0)
753 return;
754
755 /* Choose whether to use a sequence using a temporary register, or
756 a sequence with multiple adds. We can add a signed 7-bit value
757 in one instruction. */
758 if (value > 120 || value < -120)
759 {
760 rtx tmpreg;
761 rtx tmpreg2;
762 rtx_insn *insn;
763
764 tmpreg2 = NULL_RTX;
765
766 /* For prologue or normal epilogue, P1 can be safely used
767 as the temporary register. For sibcall epilogue, we try to find
768 a call used P register, which will be restored in epilogue.
769 If we cannot find such a P register, we have to use one I register
770 to help us. */
771
772 if (epilogue_p >= 0)
773 tmpreg = gen_rtx_REG (SImode, REG_P1);
774 else
775 {
776 int i;
777 for (i = REG_P0; i <= REG_P5; i++)
778 if ((df_regs_ever_live_p (i) && ! call_used_regs[i])
779 || (!TARGET_FDPIC
780 && i == PIC_OFFSET_TABLE_REGNUM
781 && (crtl->uses_pic_offset_table
782 || (TARGET_ID_SHARED_LIBRARY
783 && ! crtl->is_leaf))))
784 break;
785 if (i <= REG_P5)
786 tmpreg = gen_rtx_REG (SImode, i);
787 else
788 {
789 tmpreg = gen_rtx_REG (SImode, REG_P1);
790 tmpreg2 = gen_rtx_REG (SImode, REG_I0);
791 emit_move_insn (tmpreg2, tmpreg);
792 }
793 }
794
795 if (frame)
796 frame_related_constant_load (tmpreg, value, TRUE);
797 else
798 insn = emit_move_insn (tmpreg, GEN_INT (value));
799
800 insn = emit_insn (gen_addsi3 (reg, reg, tmpreg));
801 if (frame)
802 RTX_FRAME_RELATED_P (insn) = 1;
803
804 if (tmpreg2 != NULL_RTX)
805 emit_move_insn (tmpreg, tmpreg2);
806 }
807 else
808 do
809 {
810 int size = value;
811 rtx_insn *insn;
812
813 if (size > 60)
814 size = 60;
815 else if (size < -60)
816 /* We could use -62, but that would leave the stack unaligned, so
817 it's no good. */
818 size = -60;
819
820 insn = emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
821 if (frame)
822 RTX_FRAME_RELATED_P (insn) = 1;
823 value -= size;
824 }
825 while (value != 0);
826 }
827
828 /* Generate a LINK insn for a frame sized FRAME_SIZE. If this constant
829 is too large, generate a sequence of insns that has the same effect.
830 SPREG contains (reg:SI REG_SP). */
831
832 static void
833 emit_link_insn (rtx spreg, HOST_WIDE_INT frame_size)
834 {
835 HOST_WIDE_INT link_size = frame_size;
836 rtx_insn *insn;
837 int i;
838
839 if (link_size > 262140)
840 link_size = 262140;
841
842 /* Use a LINK insn with as big a constant as possible, then subtract
843 any remaining size from the SP. */
844 insn = emit_insn (gen_link (GEN_INT (-8 - link_size)));
845 RTX_FRAME_RELATED_P (insn) = 1;
846
847 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
848 {
849 rtx set = XVECEXP (PATTERN (insn), 0, i);
850 gcc_assert (GET_CODE (set) == SET);
851 RTX_FRAME_RELATED_P (set) = 1;
852 }
853
854 frame_size -= link_size;
855
856 if (frame_size > 0)
857 {
858 /* Must use a call-clobbered PREG that isn't the static chain. */
859 rtx tmpreg = gen_rtx_REG (Pmode, REG_P1);
860
861 frame_related_constant_load (tmpreg, -frame_size, TRUE);
862 insn = emit_insn (gen_addsi3 (spreg, spreg, tmpreg));
863 RTX_FRAME_RELATED_P (insn) = 1;
864 }
865 }
866
867 /* Return the number of bytes we must reserve for outgoing arguments
868 in the current function's stack frame. */
869
870 static HOST_WIDE_INT
871 arg_area_size (void)
872 {
873 if (crtl->outgoing_args_size)
874 {
875 if (crtl->outgoing_args_size >= FIXED_STACK_AREA)
876 return crtl->outgoing_args_size;
877 else
878 return FIXED_STACK_AREA;
879 }
880 return 0;
881 }
882
883 /* Save RETS and FP, and allocate a stack frame. ALL is true if the
884 function must save all its registers (true only for certain interrupt
885 handlers). */
886
887 static void
888 do_link (rtx spreg, HOST_WIDE_INT frame_size, bool all)
889 {
890 frame_size += arg_area_size ();
891
892 if (all
893 || stack_frame_needed_p ()
894 || (must_save_rets_p () && must_save_fp_p ()))
895 emit_link_insn (spreg, frame_size);
896 else
897 {
898 if (must_save_rets_p ())
899 {
900 rtx pat = gen_movsi (gen_rtx_MEM (Pmode,
901 gen_rtx_PRE_DEC (Pmode, spreg)),
902 bfin_rets_rtx);
903 rtx_insn *insn = emit_insn (pat);
904 RTX_FRAME_RELATED_P (insn) = 1;
905 }
906 if (must_save_fp_p ())
907 {
908 rtx pat = gen_movsi (gen_rtx_MEM (Pmode,
909 gen_rtx_PRE_DEC (Pmode, spreg)),
910 gen_rtx_REG (Pmode, REG_FP));
911 rtx_insn *insn = emit_insn (pat);
912 RTX_FRAME_RELATED_P (insn) = 1;
913 }
914 add_to_reg (spreg, -frame_size, 1, 0);
915 }
916 }
917
918 /* Like do_link, but used for epilogues to deallocate the stack frame.
919 EPILOGUE_P is zero if this function is called for prologue,
920 otherwise it's nonzero. And it's less than zero if this is for
921 sibcall epilogue. */
922
923 static void
924 do_unlink (rtx spreg, HOST_WIDE_INT frame_size, bool all, int epilogue_p)
925 {
926 frame_size += arg_area_size ();
927
928 if (stack_frame_needed_p ())
929 emit_insn (gen_unlink ());
930 else
931 {
932 rtx postinc = gen_rtx_MEM (Pmode, gen_rtx_POST_INC (Pmode, spreg));
933
934 add_to_reg (spreg, frame_size, 0, epilogue_p);
935 if (all || must_save_fp_p ())
936 {
937 rtx fpreg = gen_rtx_REG (Pmode, REG_FP);
938 emit_move_insn (fpreg, postinc);
939 emit_use (fpreg);
940 }
941 if (all || must_save_rets_p ())
942 {
943 emit_move_insn (bfin_rets_rtx, postinc);
944 emit_use (bfin_rets_rtx);
945 }
946 }
947 }
948
949 /* Generate a prologue suitable for a function of kind FKIND. This is
950 called for interrupt and exception handler prologues.
951 SPREG contains (reg:SI REG_SP). */
952
953 static void
954 expand_interrupt_handler_prologue (rtx spreg, e_funkind fkind, bool all)
955 {
956 HOST_WIDE_INT frame_size = get_frame_size ();
957 rtx predec1 = gen_rtx_PRE_DEC (SImode, spreg);
958 rtx predec = gen_rtx_MEM (SImode, predec1);
959 rtx_insn *insn;
960 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
961 tree kspisusp = lookup_attribute ("kspisusp", attrs);
962
963 if (kspisusp)
964 {
965 insn = emit_move_insn (spreg, gen_rtx_REG (Pmode, REG_USP));
966 RTX_FRAME_RELATED_P (insn) = 1;
967 }
968
969 /* We need space on the stack in case we need to save the argument
970 registers. */
971 if (fkind == EXCPT_HANDLER)
972 {
973 insn = emit_insn (gen_addsi3 (spreg, spreg, GEN_INT (-12)));
974 RTX_FRAME_RELATED_P (insn) = 1;
975 }
976
977 /* If we're calling other functions, they won't save their call-clobbered
978 registers, so we must save everything here. */
979 if (!crtl->is_leaf)
980 all = true;
981 expand_prologue_reg_save (spreg, all, true);
982
983 if (ENABLE_WA_05000283 || ENABLE_WA_05000315)
984 {
985 rtx chipid = GEN_INT (trunc_int_for_mode (0xFFC00014, SImode));
986 rtx p5reg = gen_rtx_REG (Pmode, REG_P5);
987 emit_insn (gen_movbi (bfin_cc_rtx, const1_rtx));
988 emit_insn (gen_movsi_high (p5reg, chipid));
989 emit_insn (gen_movsi_low (p5reg, p5reg, chipid));
990 emit_insn (gen_dummy_load (p5reg, bfin_cc_rtx));
991 }
992
993 if (lookup_attribute ("nesting", attrs))
994 {
995 rtx srcreg = gen_rtx_REG (Pmode, ret_regs[fkind]);
996 insn = emit_move_insn (predec, srcreg);
997 RTX_FRAME_RELATED_P (insn) = 1;
998 }
999
1000 do_link (spreg, frame_size, all);
1001
1002 if (fkind == EXCPT_HANDLER)
1003 {
1004 rtx r0reg = gen_rtx_REG (SImode, REG_R0);
1005 rtx r1reg = gen_rtx_REG (SImode, REG_R1);
1006 rtx r2reg = gen_rtx_REG (SImode, REG_R2);
1007
1008 emit_move_insn (r0reg, gen_rtx_REG (SImode, REG_SEQSTAT));
1009 emit_insn (gen_ashrsi3 (r0reg, r0reg, GEN_INT (26)));
1010 emit_insn (gen_ashlsi3 (r0reg, r0reg, GEN_INT (26)));
1011 emit_move_insn (r1reg, spreg);
1012 emit_move_insn (r2reg, gen_rtx_REG (Pmode, REG_FP));
1013 emit_insn (gen_addsi3 (r2reg, r2reg, GEN_INT (8)));
1014 }
1015 }
1016
1017 /* Generate an epilogue suitable for a function of kind FKIND. This is
1018 called for interrupt and exception handler epilogues.
1019 SPREG contains (reg:SI REG_SP). */
1020
1021 static void
1022 expand_interrupt_handler_epilogue (rtx spreg, e_funkind fkind, bool all)
1023 {
1024 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1025 rtx postinc1 = gen_rtx_POST_INC (SImode, spreg);
1026 rtx postinc = gen_rtx_MEM (SImode, postinc1);
1027
1028 /* A slightly crude technique to stop flow from trying to delete "dead"
1029 insns. */
1030 MEM_VOLATILE_P (postinc) = 1;
1031
1032 do_unlink (spreg, get_frame_size (), all, 1);
1033
1034 if (lookup_attribute ("nesting", attrs))
1035 {
1036 rtx srcreg = gen_rtx_REG (Pmode, ret_regs[fkind]);
1037 emit_move_insn (srcreg, postinc);
1038 }
1039
1040 /* If we're calling other functions, they won't save their call-clobbered
1041 registers, so we must save (and restore) everything here. */
1042 if (!crtl->is_leaf)
1043 all = true;
1044
1045 expand_epilogue_reg_restore (spreg, all, true);
1046
1047 /* Deallocate any space we left on the stack in case we needed to save the
1048 argument registers. */
1049 if (fkind == EXCPT_HANDLER)
1050 emit_insn (gen_addsi3 (spreg, spreg, GEN_INT (12)));
1051
1052 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode, ret_regs[fkind])));
1053 }
1054
1055 /* Used while emitting the prologue to generate code to load the correct value
1056 into the PIC register, which is passed in DEST. */
1057
1058 static rtx
1059 bfin_load_pic_reg (rtx dest)
1060 {
1061 struct cgraph_local_info *i = NULL;
1062 rtx addr;
1063
1064 i = cgraph_node::local_info (current_function_decl);
1065
1066 /* Functions local to the translation unit don't need to reload the
1067 pic reg, since the caller always passes a usable one. */
1068 if (i && i->local)
1069 return pic_offset_table_rtx;
1070
1071 if (global_options_set.x_bfin_library_id)
1072 addr = plus_constant (Pmode, pic_offset_table_rtx,
1073 -4 - bfin_library_id * 4);
1074 else
1075 addr = gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
1076 gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1077 UNSPEC_LIBRARY_OFFSET));
1078 emit_insn (gen_movsi (dest, gen_rtx_MEM (Pmode, addr)));
1079 return dest;
1080 }
1081
1082 /* Generate RTL for the prologue of the current function. */
1083
1084 void
1085 bfin_expand_prologue (void)
1086 {
1087 HOST_WIDE_INT frame_size = get_frame_size ();
1088 rtx spreg = gen_rtx_REG (Pmode, REG_SP);
1089 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
1090 rtx pic_reg_loaded = NULL_RTX;
1091 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1092 bool all = lookup_attribute ("saveall", attrs) != NULL_TREE;
1093
1094 if (flag_stack_usage_info)
1095 current_function_static_stack_size = frame_size;
1096
1097 if (fkind != SUBROUTINE)
1098 {
1099 expand_interrupt_handler_prologue (spreg, fkind, all);
1100 return;
1101 }
1102
1103 if (crtl->limit_stack
1104 || (TARGET_STACK_CHECK_L1
1105 && !DECL_NO_LIMIT_STACK (current_function_decl)))
1106 {
1107 HOST_WIDE_INT offset
1108 = bfin_initial_elimination_offset (ARG_POINTER_REGNUM,
1109 STACK_POINTER_REGNUM);
1110 rtx lim = crtl->limit_stack ? stack_limit_rtx : NULL_RTX;
1111 rtx tmp = gen_rtx_REG (Pmode, REG_R3);
1112 rtx p2reg = gen_rtx_REG (Pmode, REG_P2);
1113
1114 emit_move_insn (tmp, p2reg);
1115 if (!lim)
1116 {
1117 emit_move_insn (p2reg, gen_int_mode (0xFFB00000, SImode));
1118 emit_move_insn (p2reg, gen_rtx_MEM (Pmode, p2reg));
1119 lim = p2reg;
1120 }
1121 if (GET_CODE (lim) == SYMBOL_REF)
1122 {
1123 if (TARGET_ID_SHARED_LIBRARY)
1124 {
1125 rtx p1reg = gen_rtx_REG (Pmode, REG_P1);
1126 rtx val;
1127 pic_reg_loaded = bfin_load_pic_reg (p2reg);
1128 val = legitimize_pic_address (stack_limit_rtx, p1reg,
1129 pic_reg_loaded);
1130 emit_move_insn (p1reg, val);
1131 frame_related_constant_load (p2reg, offset, FALSE);
1132 emit_insn (gen_addsi3 (p2reg, p2reg, p1reg));
1133 lim = p2reg;
1134 }
1135 else
1136 {
1137 rtx limit = plus_constant (Pmode, lim, offset);
1138 emit_move_insn (p2reg, limit);
1139 lim = p2reg;
1140 }
1141 }
1142 else
1143 {
1144 if (lim != p2reg)
1145 emit_move_insn (p2reg, lim);
1146 add_to_reg (p2reg, offset, 0, 0);
1147 lim = p2reg;
1148 }
1149 emit_insn (gen_compare_lt (bfin_cc_rtx, spreg, lim));
1150 emit_insn (gen_trapifcc ());
1151 emit_move_insn (p2reg, tmp);
1152 }
1153 expand_prologue_reg_save (spreg, all, false);
1154
1155 do_link (spreg, frame_size, all);
1156
1157 if (TARGET_ID_SHARED_LIBRARY
1158 && !TARGET_SEP_DATA
1159 && (crtl->uses_pic_offset_table
1160 || !crtl->is_leaf))
1161 bfin_load_pic_reg (pic_offset_table_rtx);
1162 }
1163
1164 /* Generate RTL for the epilogue of the current function. NEED_RETURN is zero
1165 if this is for a sibcall. EH_RETURN is nonzero if we're expanding an
1166 eh_return pattern. SIBCALL_P is true if this is a sibcall epilogue,
1167 false otherwise. */
1168
1169 void
1170 bfin_expand_epilogue (int need_return, int eh_return, bool sibcall_p)
1171 {
1172 rtx spreg = gen_rtx_REG (Pmode, REG_SP);
1173 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
1174 int e = sibcall_p ? -1 : 1;
1175 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1176 bool all = lookup_attribute ("saveall", attrs) != NULL_TREE;
1177
1178 if (fkind != SUBROUTINE)
1179 {
1180 expand_interrupt_handler_epilogue (spreg, fkind, all);
1181 return;
1182 }
1183
1184 do_unlink (spreg, get_frame_size (), all, e);
1185
1186 expand_epilogue_reg_restore (spreg, all, false);
1187
1188 /* Omit the return insn if this is for a sibcall. */
1189 if (! need_return)
1190 return;
1191
1192 if (eh_return)
1193 emit_insn (gen_addsi3 (spreg, spreg, gen_rtx_REG (Pmode, REG_P2)));
1194
1195 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode, REG_RETS)));
1196 }
1197 \f
1198 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
1199
1200 int
1201 bfin_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
1202 unsigned int new_reg)
1203 {
1204 /* Interrupt functions can only use registers that have already been
1205 saved by the prologue, even if they would normally be
1206 call-clobbered. */
1207
1208 if (funkind (TREE_TYPE (current_function_decl)) != SUBROUTINE
1209 && !df_regs_ever_live_p (new_reg))
1210 return 0;
1211
1212 return 1;
1213 }
1214
1215 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
1216 static void
1217 bfin_extra_live_on_entry (bitmap regs)
1218 {
1219 if (TARGET_FDPIC)
1220 bitmap_set_bit (regs, FDPIC_REGNO);
1221 }
1222
1223 /* Return the value of the return address for the frame COUNT steps up
1224 from the current frame, after the prologue.
1225 We punt for everything but the current frame by returning const0_rtx. */
1226
1227 rtx
1228 bfin_return_addr_rtx (int count)
1229 {
1230 if (count != 0)
1231 return const0_rtx;
1232
1233 return get_hard_reg_initial_val (Pmode, REG_RETS);
1234 }
1235
1236 static rtx
1237 bfin_delegitimize_address (rtx orig_x)
1238 {
1239 rtx x = orig_x;
1240
1241 if (GET_CODE (x) != MEM)
1242 return orig_x;
1243
1244 x = XEXP (x, 0);
1245 if (GET_CODE (x) == PLUS
1246 && GET_CODE (XEXP (x, 1)) == UNSPEC
1247 && XINT (XEXP (x, 1), 1) == UNSPEC_MOVE_PIC
1248 && GET_CODE (XEXP (x, 0)) == REG
1249 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
1250 return XVECEXP (XEXP (x, 1), 0, 0);
1251
1252 return orig_x;
1253 }
1254
1255 /* This predicate is used to compute the length of a load/store insn.
1256 OP is a MEM rtx, we return nonzero if its addressing mode requires a
1257 32-bit instruction. */
1258
1259 int
1260 effective_address_32bit_p (rtx op, machine_mode mode)
1261 {
1262 HOST_WIDE_INT offset;
1263
1264 mode = GET_MODE (op);
1265 op = XEXP (op, 0);
1266
1267 if (GET_CODE (op) != PLUS)
1268 {
1269 gcc_assert (REG_P (op) || GET_CODE (op) == POST_INC
1270 || GET_CODE (op) == PRE_DEC || GET_CODE (op) == POST_DEC);
1271 return 0;
1272 }
1273
1274 if (GET_CODE (XEXP (op, 1)) == UNSPEC)
1275 return 1;
1276
1277 offset = INTVAL (XEXP (op, 1));
1278
1279 /* All byte loads use a 16-bit offset. */
1280 if (GET_MODE_SIZE (mode) == 1)
1281 return 1;
1282
1283 if (GET_MODE_SIZE (mode) == 4)
1284 {
1285 /* Frame pointer relative loads can use a negative offset, all others
1286 are restricted to a small positive one. */
1287 if (XEXP (op, 0) == frame_pointer_rtx)
1288 return offset < -128 || offset > 60;
1289 return offset < 0 || offset > 60;
1290 }
1291
1292 /* Must be HImode now. */
1293 return offset < 0 || offset > 30;
1294 }
1295
1296 /* Returns true if X is a memory reference using an I register. */
1297 bool
1298 bfin_dsp_memref_p (rtx x)
1299 {
1300 if (! MEM_P (x))
1301 return false;
1302 x = XEXP (x, 0);
1303 if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_INC
1304 || GET_CODE (x) == POST_DEC || GET_CODE (x) == PRE_DEC)
1305 x = XEXP (x, 0);
1306 return IREG_P (x);
1307 }
1308
1309 /* Return cost of the memory address ADDR.
1310 All addressing modes are equally cheap on the Blackfin. */
1311
1312 static int
1313 bfin_address_cost (rtx addr ATTRIBUTE_UNUSED,
1314 machine_mode mode ATTRIBUTE_UNUSED,
1315 addr_space_t as ATTRIBUTE_UNUSED,
1316 bool speed ATTRIBUTE_UNUSED)
1317 {
1318 return 1;
1319 }
1320
1321 /* Subroutine of print_operand; used to print a memory reference X to FILE. */
1322
1323 void
1324 print_address_operand (FILE *file, rtx x)
1325 {
1326 switch (GET_CODE (x))
1327 {
1328 case PLUS:
1329 output_address (XEXP (x, 0));
1330 fprintf (file, "+");
1331 output_address (XEXP (x, 1));
1332 break;
1333
1334 case PRE_DEC:
1335 fprintf (file, "--");
1336 output_address (XEXP (x, 0));
1337 break;
1338 case POST_INC:
1339 output_address (XEXP (x, 0));
1340 fprintf (file, "++");
1341 break;
1342 case POST_DEC:
1343 output_address (XEXP (x, 0));
1344 fprintf (file, "--");
1345 break;
1346
1347 default:
1348 gcc_assert (GET_CODE (x) != MEM);
1349 print_operand (file, x, 0);
1350 break;
1351 }
1352 }
1353
1354 /* Adding intp DImode support by Tony
1355 * -- Q: (low word)
1356 * -- R: (high word)
1357 */
1358
1359 void
1360 print_operand (FILE *file, rtx x, char code)
1361 {
1362 machine_mode mode;
1363
1364 if (code == '!')
1365 {
1366 if (GET_MODE (current_output_insn) == SImode)
1367 fprintf (file, " ||");
1368 else
1369 fprintf (file, ";");
1370 return;
1371 }
1372
1373 mode = GET_MODE (x);
1374
1375 switch (code)
1376 {
1377 case 'j':
1378 switch (GET_CODE (x))
1379 {
1380 case EQ:
1381 fprintf (file, "e");
1382 break;
1383 case NE:
1384 fprintf (file, "ne");
1385 break;
1386 case GT:
1387 fprintf (file, "g");
1388 break;
1389 case LT:
1390 fprintf (file, "l");
1391 break;
1392 case GE:
1393 fprintf (file, "ge");
1394 break;
1395 case LE:
1396 fprintf (file, "le");
1397 break;
1398 case GTU:
1399 fprintf (file, "g");
1400 break;
1401 case LTU:
1402 fprintf (file, "l");
1403 break;
1404 case GEU:
1405 fprintf (file, "ge");
1406 break;
1407 case LEU:
1408 fprintf (file, "le");
1409 break;
1410 default:
1411 output_operand_lossage ("invalid %%j value");
1412 }
1413 break;
1414
1415 case 'J': /* reverse logic */
1416 switch (GET_CODE(x))
1417 {
1418 case EQ:
1419 fprintf (file, "ne");
1420 break;
1421 case NE:
1422 fprintf (file, "e");
1423 break;
1424 case GT:
1425 fprintf (file, "le");
1426 break;
1427 case LT:
1428 fprintf (file, "ge");
1429 break;
1430 case GE:
1431 fprintf (file, "l");
1432 break;
1433 case LE:
1434 fprintf (file, "g");
1435 break;
1436 case GTU:
1437 fprintf (file, "le");
1438 break;
1439 case LTU:
1440 fprintf (file, "ge");
1441 break;
1442 case GEU:
1443 fprintf (file, "l");
1444 break;
1445 case LEU:
1446 fprintf (file, "g");
1447 break;
1448 default:
1449 output_operand_lossage ("invalid %%J value");
1450 }
1451 break;
1452
1453 default:
1454 switch (GET_CODE (x))
1455 {
1456 case REG:
1457 if (code == 'h')
1458 {
1459 if (REGNO (x) < 32)
1460 fprintf (file, "%s", short_reg_names[REGNO (x)]);
1461 else
1462 output_operand_lossage ("invalid operand for code '%c'", code);
1463 }
1464 else if (code == 'd')
1465 {
1466 if (REGNO (x) < 32)
1467 fprintf (file, "%s", high_reg_names[REGNO (x)]);
1468 else
1469 output_operand_lossage ("invalid operand for code '%c'", code);
1470 }
1471 else if (code == 'w')
1472 {
1473 if (REGNO (x) == REG_A0 || REGNO (x) == REG_A1)
1474 fprintf (file, "%s.w", reg_names[REGNO (x)]);
1475 else
1476 output_operand_lossage ("invalid operand for code '%c'", code);
1477 }
1478 else if (code == 'x')
1479 {
1480 if (REGNO (x) == REG_A0 || REGNO (x) == REG_A1)
1481 fprintf (file, "%s.x", reg_names[REGNO (x)]);
1482 else
1483 output_operand_lossage ("invalid operand for code '%c'", code);
1484 }
1485 else if (code == 'v')
1486 {
1487 if (REGNO (x) == REG_A0)
1488 fprintf (file, "AV0");
1489 else if (REGNO (x) == REG_A1)
1490 fprintf (file, "AV1");
1491 else
1492 output_operand_lossage ("invalid operand for code '%c'", code);
1493 }
1494 else if (code == 'D')
1495 {
1496 if (D_REGNO_P (REGNO (x)))
1497 fprintf (file, "%s", dregs_pair_names[REGNO (x)]);
1498 else
1499 output_operand_lossage ("invalid operand for code '%c'", code);
1500 }
1501 else if (code == 'H')
1502 {
1503 if ((mode == DImode || mode == DFmode) && REG_P (x))
1504 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
1505 else
1506 output_operand_lossage ("invalid operand for code '%c'", code);
1507 }
1508 else if (code == 'T')
1509 {
1510 if (D_REGNO_P (REGNO (x)))
1511 fprintf (file, "%s", byte_reg_names[REGNO (x)]);
1512 else
1513 output_operand_lossage ("invalid operand for code '%c'", code);
1514 }
1515 else
1516 fprintf (file, "%s", reg_names[REGNO (x)]);
1517 break;
1518
1519 case MEM:
1520 fputc ('[', file);
1521 x = XEXP (x,0);
1522 print_address_operand (file, x);
1523 fputc (']', file);
1524 break;
1525
1526 case CONST_INT:
1527 if (code == 'M')
1528 {
1529 switch (INTVAL (x))
1530 {
1531 case MACFLAG_NONE:
1532 break;
1533 case MACFLAG_FU:
1534 fputs ("(FU)", file);
1535 break;
1536 case MACFLAG_T:
1537 fputs ("(T)", file);
1538 break;
1539 case MACFLAG_TFU:
1540 fputs ("(TFU)", file);
1541 break;
1542 case MACFLAG_W32:
1543 fputs ("(W32)", file);
1544 break;
1545 case MACFLAG_IS:
1546 fputs ("(IS)", file);
1547 break;
1548 case MACFLAG_IU:
1549 fputs ("(IU)", file);
1550 break;
1551 case MACFLAG_IH:
1552 fputs ("(IH)", file);
1553 break;
1554 case MACFLAG_M:
1555 fputs ("(M)", file);
1556 break;
1557 case MACFLAG_IS_M:
1558 fputs ("(IS,M)", file);
1559 break;
1560 case MACFLAG_ISS2:
1561 fputs ("(ISS2)", file);
1562 break;
1563 case MACFLAG_S2RND:
1564 fputs ("(S2RND)", file);
1565 break;
1566 default:
1567 gcc_unreachable ();
1568 }
1569 break;
1570 }
1571 else if (code == 'b')
1572 {
1573 if (INTVAL (x) == 0)
1574 fputs ("+=", file);
1575 else if (INTVAL (x) == 1)
1576 fputs ("-=", file);
1577 else
1578 gcc_unreachable ();
1579 break;
1580 }
1581 /* Moves to half registers with d or h modifiers always use unsigned
1582 constants. */
1583 else if (code == 'd')
1584 x = GEN_INT ((INTVAL (x) >> 16) & 0xffff);
1585 else if (code == 'h')
1586 x = GEN_INT (INTVAL (x) & 0xffff);
1587 else if (code == 'N')
1588 x = GEN_INT (-INTVAL (x));
1589 else if (code == 'X')
1590 x = GEN_INT (exact_log2 (0xffffffff & INTVAL (x)));
1591 else if (code == 'Y')
1592 x = GEN_INT (exact_log2 (0xffffffff & ~INTVAL (x)));
1593 else if (code == 'Z')
1594 /* Used for LINK insns. */
1595 x = GEN_INT (-8 - INTVAL (x));
1596
1597 /* fall through */
1598
1599 case SYMBOL_REF:
1600 output_addr_const (file, x);
1601 break;
1602
1603 case CONST_DOUBLE:
1604 output_operand_lossage ("invalid const_double operand");
1605 break;
1606
1607 case UNSPEC:
1608 switch (XINT (x, 1))
1609 {
1610 case UNSPEC_MOVE_PIC:
1611 output_addr_const (file, XVECEXP (x, 0, 0));
1612 fprintf (file, "@GOT");
1613 break;
1614
1615 case UNSPEC_MOVE_FDPIC:
1616 output_addr_const (file, XVECEXP (x, 0, 0));
1617 fprintf (file, "@GOT17M4");
1618 break;
1619
1620 case UNSPEC_FUNCDESC_GOT17M4:
1621 output_addr_const (file, XVECEXP (x, 0, 0));
1622 fprintf (file, "@FUNCDESC_GOT17M4");
1623 break;
1624
1625 case UNSPEC_LIBRARY_OFFSET:
1626 fprintf (file, "_current_shared_library_p5_offset_");
1627 break;
1628
1629 default:
1630 gcc_unreachable ();
1631 }
1632 break;
1633
1634 default:
1635 output_addr_const (file, x);
1636 }
1637 }
1638 }
1639 \f
1640 /* Argument support functions. */
1641
1642 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1643 for a call to a function whose data type is FNTYPE.
1644 For a library call, FNTYPE is 0.
1645 VDSP C Compiler manual, our ABI says that
1646 first 3 words of arguments will use R0, R1 and R2.
1647 */
1648
1649 void
1650 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
1651 rtx libname ATTRIBUTE_UNUSED)
1652 {
1653 static CUMULATIVE_ARGS zero_cum;
1654
1655 *cum = zero_cum;
1656
1657 /* Set up the number of registers to use for passing arguments. */
1658
1659 cum->nregs = max_arg_registers;
1660 cum->arg_regs = arg_regs;
1661
1662 cum->call_cookie = CALL_NORMAL;
1663 /* Check for a longcall attribute. */
1664 if (fntype && lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
1665 cum->call_cookie |= CALL_SHORT;
1666 else if (fntype && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype)))
1667 cum->call_cookie |= CALL_LONG;
1668
1669 return;
1670 }
1671
1672 /* Update the data in CUM to advance over an argument
1673 of mode MODE and data type TYPE.
1674 (TYPE is null for libcalls where that information may not be available.) */
1675
1676 static void
1677 bfin_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
1678 const_tree type, bool named ATTRIBUTE_UNUSED)
1679 {
1680 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1681 int count, bytes, words;
1682
1683 bytes = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
1684 words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1685
1686 cum->words += words;
1687 cum->nregs -= words;
1688
1689 if (cum->nregs <= 0)
1690 {
1691 cum->nregs = 0;
1692 cum->arg_regs = NULL;
1693 }
1694 else
1695 {
1696 for (count = 1; count <= words; count++)
1697 cum->arg_regs++;
1698 }
1699
1700 return;
1701 }
1702
1703 /* Define where to put the arguments to a function.
1704 Value is zero to push the argument on the stack,
1705 or a hard register in which to store the argument.
1706
1707 MODE is the argument's machine mode.
1708 TYPE is the data type of the argument (as a tree).
1709 This is null for libcalls where that information may
1710 not be available.
1711 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1712 the preceding args and about the function being called.
1713 NAMED is nonzero if this argument is a named parameter
1714 (otherwise it is an extra parameter matching an ellipsis). */
1715
1716 static rtx
1717 bfin_function_arg (cumulative_args_t cum_v, machine_mode mode,
1718 const_tree type, bool named ATTRIBUTE_UNUSED)
1719 {
1720 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1721 int bytes
1722 = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
1723
1724 if (mode == VOIDmode)
1725 /* Compute operand 2 of the call insn. */
1726 return GEN_INT (cum->call_cookie);
1727
1728 if (bytes == -1)
1729 return NULL_RTX;
1730
1731 if (cum->nregs)
1732 return gen_rtx_REG (mode, *(cum->arg_regs));
1733
1734 return NULL_RTX;
1735 }
1736
1737 /* For an arg passed partly in registers and partly in memory,
1738 this is the number of bytes passed in registers.
1739 For args passed entirely in registers or entirely in memory, zero.
1740
1741 Refer VDSP C Compiler manual, our ABI.
1742 First 3 words are in registers. So, if an argument is larger
1743 than the registers available, it will span the register and
1744 stack. */
1745
1746 static int
1747 bfin_arg_partial_bytes (cumulative_args_t cum, machine_mode mode,
1748 tree type ATTRIBUTE_UNUSED,
1749 bool named ATTRIBUTE_UNUSED)
1750 {
1751 int bytes
1752 = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
1753 int bytes_left = get_cumulative_args (cum)->nregs * UNITS_PER_WORD;
1754
1755 if (bytes == -1)
1756 return 0;
1757
1758 if (bytes_left == 0)
1759 return 0;
1760 if (bytes > bytes_left)
1761 return bytes_left;
1762 return 0;
1763 }
1764
1765 /* Variable sized types are passed by reference. */
1766
1767 static bool
1768 bfin_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
1769 machine_mode mode ATTRIBUTE_UNUSED,
1770 const_tree type, bool named ATTRIBUTE_UNUSED)
1771 {
1772 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
1773 }
1774
1775 /* Decide whether a type should be returned in memory (true)
1776 or in a register (false). This is called by the macro
1777 TARGET_RETURN_IN_MEMORY. */
1778
1779 static bool
1780 bfin_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1781 {
1782 int size = int_size_in_bytes (type);
1783 return size > 2 * UNITS_PER_WORD || size == -1;
1784 }
1785
1786 /* Register in which address to store a structure value
1787 is passed to a function. */
1788 static rtx
1789 bfin_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
1790 int incoming ATTRIBUTE_UNUSED)
1791 {
1792 return gen_rtx_REG (Pmode, REG_P0);
1793 }
1794
1795 /* Return true when register may be used to pass function parameters. */
1796
1797 bool
1798 function_arg_regno_p (int n)
1799 {
1800 int i;
1801 for (i = 0; arg_regs[i] != -1; i++)
1802 if (n == arg_regs[i])
1803 return true;
1804 return false;
1805 }
1806
1807 /* Returns 1 if OP contains a symbol reference */
1808
1809 int
1810 symbolic_reference_mentioned_p (rtx op)
1811 {
1812 register const char *fmt;
1813 register int i;
1814
1815 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
1816 return 1;
1817
1818 fmt = GET_RTX_FORMAT (GET_CODE (op));
1819 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
1820 {
1821 if (fmt[i] == 'E')
1822 {
1823 register int j;
1824
1825 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1826 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
1827 return 1;
1828 }
1829
1830 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
1831 return 1;
1832 }
1833
1834 return 0;
1835 }
1836
1837 /* Decide whether we can make a sibling call to a function. DECL is the
1838 declaration of the function being targeted by the call and EXP is the
1839 CALL_EXPR representing the call. */
1840
1841 static bool
1842 bfin_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
1843 tree exp ATTRIBUTE_UNUSED)
1844 {
1845 struct cgraph_local_info *this_func, *called_func;
1846 e_funkind fkind = funkind (TREE_TYPE (current_function_decl));
1847 if (fkind != SUBROUTINE)
1848 return false;
1849 if (!TARGET_ID_SHARED_LIBRARY || TARGET_SEP_DATA)
1850 return true;
1851
1852 /* When compiling for ID shared libraries, can't sibcall a local function
1853 from a non-local function, because the local function thinks it does
1854 not need to reload P5 in the prologue, but the sibcall wil pop P5 in the
1855 sibcall epilogue, and we end up with the wrong value in P5. */
1856
1857 if (!decl)
1858 /* Not enough information. */
1859 return false;
1860
1861 this_func = cgraph_node::local_info (current_function_decl);
1862 called_func = cgraph_node::local_info (decl);
1863 if (!called_func)
1864 return false;
1865 return !called_func->local || this_func->local;
1866 }
1867 \f
1868 /* Write a template for a trampoline to F. */
1869
1870 static void
1871 bfin_asm_trampoline_template (FILE *f)
1872 {
1873 if (TARGET_FDPIC)
1874 {
1875 fprintf (f, "\t.dd\t0x00000000\n"); /* 0 */
1876 fprintf (f, "\t.dd\t0x00000000\n"); /* 0 */
1877 fprintf (f, "\t.dd\t0x0000e109\n"); /* p1.l = fn low */
1878 fprintf (f, "\t.dd\t0x0000e149\n"); /* p1.h = fn high */
1879 fprintf (f, "\t.dd\t0x0000e10a\n"); /* p2.l = sc low */
1880 fprintf (f, "\t.dd\t0x0000e14a\n"); /* p2.h = sc high */
1881 fprintf (f, "\t.dw\t0xac4b\n"); /* p3 = [p1 + 4] */
1882 fprintf (f, "\t.dw\t0x9149\n"); /* p1 = [p1] */
1883 fprintf (f, "\t.dw\t0x0051\n"); /* jump (p1)*/
1884 }
1885 else
1886 {
1887 fprintf (f, "\t.dd\t0x0000e109\n"); /* p1.l = fn low */
1888 fprintf (f, "\t.dd\t0x0000e149\n"); /* p1.h = fn high */
1889 fprintf (f, "\t.dd\t0x0000e10a\n"); /* p2.l = sc low */
1890 fprintf (f, "\t.dd\t0x0000e14a\n"); /* p2.h = sc high */
1891 fprintf (f, "\t.dw\t0x0051\n"); /* jump (p1)*/
1892 }
1893 }
1894
1895 /* Emit RTL insns to initialize the variable parts of a trampoline at
1896 M_TRAMP. FNDECL is the target function. CHAIN_VALUE is an RTX for
1897 the static chain value for the function. */
1898
1899 static void
1900 bfin_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1901 {
1902 rtx t1 = copy_to_reg (XEXP (DECL_RTL (fndecl), 0));
1903 rtx t2 = copy_to_reg (chain_value);
1904 rtx mem;
1905 int i = 0;
1906
1907 emit_block_move (m_tramp, assemble_trampoline_template (),
1908 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
1909
1910 if (TARGET_FDPIC)
1911 {
1912 rtx a = force_reg (Pmode, plus_constant (Pmode, XEXP (m_tramp, 0), 8));
1913 mem = adjust_address (m_tramp, Pmode, 0);
1914 emit_move_insn (mem, a);
1915 i = 8;
1916 }
1917
1918 mem = adjust_address (m_tramp, HImode, i + 2);
1919 emit_move_insn (mem, gen_lowpart (HImode, t1));
1920 emit_insn (gen_ashrsi3 (t1, t1, GEN_INT (16)));
1921 mem = adjust_address (m_tramp, HImode, i + 6);
1922 emit_move_insn (mem, gen_lowpart (HImode, t1));
1923
1924 mem = adjust_address (m_tramp, HImode, i + 10);
1925 emit_move_insn (mem, gen_lowpart (HImode, t2));
1926 emit_insn (gen_ashrsi3 (t2, t2, GEN_INT (16)));
1927 mem = adjust_address (m_tramp, HImode, i + 14);
1928 emit_move_insn (mem, gen_lowpart (HImode, t2));
1929 }
1930
1931 /* Emit insns to move operands[1] into operands[0]. */
1932
1933 void
1934 emit_pic_move (rtx *operands, machine_mode mode ATTRIBUTE_UNUSED)
1935 {
1936 rtx temp = reload_in_progress ? operands[0] : gen_reg_rtx (Pmode);
1937
1938 gcc_assert (!TARGET_FDPIC || !(reload_in_progress || reload_completed));
1939 if (GET_CODE (operands[0]) == MEM && SYMBOLIC_CONST (operands[1]))
1940 operands[1] = force_reg (SImode, operands[1]);
1941 else
1942 operands[1] = legitimize_pic_address (operands[1], temp,
1943 TARGET_FDPIC ? OUR_FDPIC_REG
1944 : pic_offset_table_rtx);
1945 }
1946
1947 /* Expand a move operation in mode MODE. The operands are in OPERANDS.
1948 Returns true if no further code must be generated, false if the caller
1949 should generate an insn to move OPERANDS[1] to OPERANDS[0]. */
1950
1951 bool
1952 expand_move (rtx *operands, machine_mode mode)
1953 {
1954 rtx op = operands[1];
1955 if ((TARGET_ID_SHARED_LIBRARY || TARGET_FDPIC)
1956 && SYMBOLIC_CONST (op))
1957 emit_pic_move (operands, mode);
1958 else if (mode == SImode && GET_CODE (op) == CONST
1959 && GET_CODE (XEXP (op, 0)) == PLUS
1960 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
1961 && !targetm.legitimate_constant_p (mode, op))
1962 {
1963 rtx dest = operands[0];
1964 rtx op0, op1;
1965 gcc_assert (!reload_in_progress && !reload_completed);
1966 op = XEXP (op, 0);
1967 op0 = force_reg (mode, XEXP (op, 0));
1968 op1 = XEXP (op, 1);
1969 if (!insn_data[CODE_FOR_addsi3].operand[2].predicate (op1, mode))
1970 op1 = force_reg (mode, op1);
1971 if (GET_CODE (dest) == MEM)
1972 dest = gen_reg_rtx (mode);
1973 emit_insn (gen_addsi3 (dest, op0, op1));
1974 if (dest == operands[0])
1975 return true;
1976 operands[1] = dest;
1977 }
1978 /* Don't generate memory->memory or constant->memory moves, go through a
1979 register */
1980 else if ((reload_in_progress | reload_completed) == 0
1981 && GET_CODE (operands[0]) == MEM
1982 && GET_CODE (operands[1]) != REG)
1983 operands[1] = force_reg (mode, operands[1]);
1984 return false;
1985 }
1986 \f
1987 /* Split one or more DImode RTL references into pairs of SImode
1988 references. The RTL can be REG, offsettable MEM, integer constant, or
1989 CONST_DOUBLE. "operands" is a pointer to an array of DImode RTL to
1990 split and "num" is its length. lo_half and hi_half are output arrays
1991 that parallel "operands". */
1992
1993 void
1994 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
1995 {
1996 while (num--)
1997 {
1998 rtx op = operands[num];
1999
2000 /* simplify_subreg refuse to split volatile memory addresses,
2001 but we still have to handle it. */
2002 if (GET_CODE (op) == MEM)
2003 {
2004 lo_half[num] = adjust_address (op, SImode, 0);
2005 hi_half[num] = adjust_address (op, SImode, 4);
2006 }
2007 else
2008 {
2009 lo_half[num] = simplify_gen_subreg (SImode, op,
2010 GET_MODE (op) == VOIDmode
2011 ? DImode : GET_MODE (op), 0);
2012 hi_half[num] = simplify_gen_subreg (SImode, op,
2013 GET_MODE (op) == VOIDmode
2014 ? DImode : GET_MODE (op), 4);
2015 }
2016 }
2017 }
2018 \f
2019 bool
2020 bfin_longcall_p (rtx op, int call_cookie)
2021 {
2022 gcc_assert (GET_CODE (op) == SYMBOL_REF);
2023 if (SYMBOL_REF_WEAK (op))
2024 return 1;
2025 if (call_cookie & CALL_SHORT)
2026 return 0;
2027 if (call_cookie & CALL_LONG)
2028 return 1;
2029 if (TARGET_LONG_CALLS)
2030 return 1;
2031 return 0;
2032 }
2033
2034 /* Expand a call instruction. FNADDR is the call target, RETVAL the return value.
2035 COOKIE is a CONST_INT holding the call_cookie prepared init_cumulative_args.
2036 SIBCALL is nonzero if this is a sibling call. */
2037
2038 void
2039 bfin_expand_call (rtx retval, rtx fnaddr, rtx callarg1, rtx cookie, int sibcall)
2040 {
2041 rtx use = NULL, call;
2042 rtx callee = XEXP (fnaddr, 0);
2043 int nelts = 3;
2044 rtx pat;
2045 rtx picreg = get_hard_reg_initial_val (SImode, FDPIC_REGNO);
2046 rtx retsreg = gen_rtx_REG (Pmode, REG_RETS);
2047 int n;
2048
2049 /* In an untyped call, we can get NULL for operand 2. */
2050 if (cookie == NULL_RTX)
2051 cookie = const0_rtx;
2052
2053 /* Static functions and indirect calls don't need the pic register. */
2054 if (!TARGET_FDPIC && flag_pic
2055 && GET_CODE (callee) == SYMBOL_REF
2056 && !SYMBOL_REF_LOCAL_P (callee))
2057 use_reg (&use, pic_offset_table_rtx);
2058
2059 if (TARGET_FDPIC)
2060 {
2061 int caller_in_sram, callee_in_sram;
2062
2063 /* 0 is not in sram, 1 is in L1 sram, 2 is in L2 sram. */
2064 caller_in_sram = callee_in_sram = 0;
2065
2066 if (lookup_attribute ("l1_text",
2067 DECL_ATTRIBUTES (cfun->decl)) != NULL_TREE)
2068 caller_in_sram = 1;
2069 else if (lookup_attribute ("l2",
2070 DECL_ATTRIBUTES (cfun->decl)) != NULL_TREE)
2071 caller_in_sram = 2;
2072
2073 if (GET_CODE (callee) == SYMBOL_REF
2074 && SYMBOL_REF_DECL (callee) && DECL_P (SYMBOL_REF_DECL (callee)))
2075 {
2076 if (lookup_attribute
2077 ("l1_text",
2078 DECL_ATTRIBUTES (SYMBOL_REF_DECL (callee))) != NULL_TREE)
2079 callee_in_sram = 1;
2080 else if (lookup_attribute
2081 ("l2",
2082 DECL_ATTRIBUTES (SYMBOL_REF_DECL (callee))) != NULL_TREE)
2083 callee_in_sram = 2;
2084 }
2085
2086 if (GET_CODE (callee) != SYMBOL_REF
2087 || bfin_longcall_p (callee, INTVAL (cookie))
2088 || (GET_CODE (callee) == SYMBOL_REF
2089 && !SYMBOL_REF_LOCAL_P (callee)
2090 && TARGET_INLINE_PLT)
2091 || caller_in_sram != callee_in_sram
2092 || (caller_in_sram && callee_in_sram
2093 && (GET_CODE (callee) != SYMBOL_REF
2094 || !SYMBOL_REF_LOCAL_P (callee))))
2095 {
2096 rtx addr = callee;
2097 if (! address_operand (addr, Pmode))
2098 addr = force_reg (Pmode, addr);
2099
2100 fnaddr = gen_reg_rtx (SImode);
2101 emit_insn (gen_load_funcdescsi (fnaddr, addr));
2102 fnaddr = gen_rtx_MEM (Pmode, fnaddr);
2103
2104 picreg = gen_reg_rtx (SImode);
2105 emit_insn (gen_load_funcdescsi (picreg,
2106 plus_constant (Pmode, addr, 4)));
2107 }
2108
2109 nelts++;
2110 }
2111 else if ((!register_no_elim_operand (callee, Pmode)
2112 && GET_CODE (callee) != SYMBOL_REF)
2113 || (GET_CODE (callee) == SYMBOL_REF
2114 && ((TARGET_ID_SHARED_LIBRARY && !TARGET_LEAF_ID_SHARED_LIBRARY)
2115 || bfin_longcall_p (callee, INTVAL (cookie)))))
2116 {
2117 callee = copy_to_mode_reg (Pmode, callee);
2118 fnaddr = gen_rtx_MEM (Pmode, callee);
2119 }
2120 call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
2121
2122 if (retval)
2123 call = gen_rtx_SET (retval, call);
2124
2125 pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nelts));
2126 n = 0;
2127 XVECEXP (pat, 0, n++) = call;
2128 if (TARGET_FDPIC)
2129 XVECEXP (pat, 0, n++) = gen_rtx_USE (VOIDmode, picreg);
2130 XVECEXP (pat, 0, n++) = gen_rtx_USE (VOIDmode, cookie);
2131 if (sibcall)
2132 XVECEXP (pat, 0, n++) = ret_rtx;
2133 else
2134 XVECEXP (pat, 0, n++) = gen_rtx_CLOBBER (VOIDmode, retsreg);
2135 call = emit_call_insn (pat);
2136 if (use)
2137 CALL_INSN_FUNCTION_USAGE (call) = use;
2138 }
2139 \f
2140 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
2141
2142 int
2143 hard_regno_mode_ok (int regno, machine_mode mode)
2144 {
2145 /* Allow only dregs to store value of mode HI or QI */
2146 enum reg_class rclass = REGNO_REG_CLASS (regno);
2147
2148 if (mode == CCmode)
2149 return 0;
2150
2151 if (mode == V2HImode)
2152 return D_REGNO_P (regno);
2153 if (rclass == CCREGS)
2154 return mode == BImode;
2155 if (mode == PDImode || mode == V2PDImode)
2156 return regno == REG_A0 || regno == REG_A1;
2157
2158 /* Allow all normal 32-bit regs, except REG_M3, in case regclass ever comes
2159 up with a bad register class (such as ALL_REGS) for DImode. */
2160 if (mode == DImode)
2161 return regno < REG_M3;
2162
2163 if (mode == SImode
2164 && TEST_HARD_REG_BIT (reg_class_contents[PROLOGUE_REGS], regno))
2165 return 1;
2166
2167 return TEST_HARD_REG_BIT (reg_class_contents[MOST_REGS], regno);
2168 }
2169
2170 /* Implements target hook vector_mode_supported_p. */
2171
2172 static bool
2173 bfin_vector_mode_supported_p (machine_mode mode)
2174 {
2175 return mode == V2HImode;
2176 }
2177
2178 /* Worker function for TARGET_REGISTER_MOVE_COST. */
2179
2180 static int
2181 bfin_register_move_cost (machine_mode mode,
2182 reg_class_t class1, reg_class_t class2)
2183 {
2184 /* These need secondary reloads, so they're more expensive. */
2185 if ((class1 == CCREGS && !reg_class_subset_p (class2, DREGS))
2186 || (class2 == CCREGS && !reg_class_subset_p (class1, DREGS)))
2187 return 4;
2188
2189 /* If optimizing for size, always prefer reg-reg over reg-memory moves. */
2190 if (optimize_size)
2191 return 2;
2192
2193 if (GET_MODE_CLASS (mode) == MODE_INT)
2194 {
2195 /* Discourage trying to use the accumulators. */
2196 if (TEST_HARD_REG_BIT (reg_class_contents[class1], REG_A0)
2197 || TEST_HARD_REG_BIT (reg_class_contents[class1], REG_A1)
2198 || TEST_HARD_REG_BIT (reg_class_contents[class2], REG_A0)
2199 || TEST_HARD_REG_BIT (reg_class_contents[class2], REG_A1))
2200 return 20;
2201 }
2202 return 2;
2203 }
2204
2205 /* Worker function for TARGET_MEMORY_MOVE_COST.
2206
2207 ??? In theory L1 memory has single-cycle latency. We should add a switch
2208 that tells the compiler whether we expect to use only L1 memory for the
2209 program; it'll make the costs more accurate. */
2210
2211 static int
2212 bfin_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2213 reg_class_t rclass,
2214 bool in ATTRIBUTE_UNUSED)
2215 {
2216 /* Make memory accesses slightly more expensive than any register-register
2217 move. Also, penalize non-DP registers, since they need secondary
2218 reloads to load and store. */
2219 if (! reg_class_subset_p (rclass, DPREGS))
2220 return 10;
2221
2222 return 8;
2223 }
2224
2225 /* Inform reload about cases where moving X with a mode MODE to a register in
2226 RCLASS requires an extra scratch register. Return the class needed for the
2227 scratch register. */
2228
2229 static reg_class_t
2230 bfin_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
2231 machine_mode mode, secondary_reload_info *sri)
2232 {
2233 /* If we have HImode or QImode, we can only use DREGS as secondary registers;
2234 in most other cases we can also use PREGS. */
2235 enum reg_class default_class = GET_MODE_SIZE (mode) >= 4 ? DPREGS : DREGS;
2236 enum reg_class x_class = NO_REGS;
2237 enum rtx_code code = GET_CODE (x);
2238 enum reg_class rclass = (enum reg_class) rclass_i;
2239
2240 if (code == SUBREG)
2241 x = SUBREG_REG (x), code = GET_CODE (x);
2242 if (REG_P (x))
2243 {
2244 int regno = REGNO (x);
2245 if (regno >= FIRST_PSEUDO_REGISTER)
2246 regno = reg_renumber[regno];
2247
2248 if (regno == -1)
2249 code = MEM;
2250 else
2251 x_class = REGNO_REG_CLASS (regno);
2252 }
2253
2254 /* We can be asked to reload (plus (FP) (large_constant)) into a DREG.
2255 This happens as a side effect of register elimination, and we need
2256 a scratch register to do it. */
2257 if (fp_plus_const_operand (x, mode))
2258 {
2259 rtx op2 = XEXP (x, 1);
2260 int large_constant_p = ! satisfies_constraint_Ks7 (op2);
2261
2262 if (rclass == PREGS || rclass == PREGS_CLOBBERED)
2263 return NO_REGS;
2264 /* If destination is a DREG, we can do this without a scratch register
2265 if the constant is valid for an add instruction. */
2266 if ((rclass == DREGS || rclass == DPREGS)
2267 && ! large_constant_p)
2268 return NO_REGS;
2269 /* Reloading to anything other than a DREG? Use a PREG scratch
2270 register. */
2271 sri->icode = CODE_FOR_reload_insi;
2272 return NO_REGS;
2273 }
2274
2275 /* Data can usually be moved freely between registers of most classes.
2276 AREGS are an exception; they can only move to or from another register
2277 in AREGS or one in DREGS. They can also be assigned the constant 0. */
2278 if (x_class == AREGS || x_class == EVEN_AREGS || x_class == ODD_AREGS)
2279 return (rclass == DREGS || rclass == AREGS || rclass == EVEN_AREGS
2280 || rclass == ODD_AREGS
2281 ? NO_REGS : DREGS);
2282
2283 if (rclass == AREGS || rclass == EVEN_AREGS || rclass == ODD_AREGS)
2284 {
2285 if (code == MEM)
2286 {
2287 sri->icode = in_p ? CODE_FOR_reload_inpdi : CODE_FOR_reload_outpdi;
2288 return NO_REGS;
2289 }
2290
2291 if (x != const0_rtx && x_class != DREGS)
2292 {
2293 return DREGS;
2294 }
2295 else
2296 return NO_REGS;
2297 }
2298
2299 /* CCREGS can only be moved from/to DREGS. */
2300 if (rclass == CCREGS && x_class != DREGS)
2301 return DREGS;
2302 if (x_class == CCREGS && rclass != DREGS)
2303 return DREGS;
2304
2305 /* All registers other than AREGS can load arbitrary constants. The only
2306 case that remains is MEM. */
2307 if (code == MEM)
2308 if (! reg_class_subset_p (rclass, default_class))
2309 return default_class;
2310
2311 return NO_REGS;
2312 }
2313
2314 /* Implement TARGET_CLASS_LIKELY_SPILLED_P. */
2315
2316 static bool
2317 bfin_class_likely_spilled_p (reg_class_t rclass)
2318 {
2319 switch (rclass)
2320 {
2321 case PREGS_CLOBBERED:
2322 case PROLOGUE_REGS:
2323 case P0REGS:
2324 case D0REGS:
2325 case D1REGS:
2326 case D2REGS:
2327 case CCREGS:
2328 return true;
2329
2330 default:
2331 break;
2332 }
2333
2334 return false;
2335 }
2336 \f
2337 static struct machine_function *
2338 bfin_init_machine_status (void)
2339 {
2340 return ggc_cleared_alloc<machine_function> ();
2341 }
2342
2343 /* Implement the TARGET_OPTION_OVERRIDE hook. */
2344
2345 static void
2346 bfin_option_override (void)
2347 {
2348 /* If processor type is not specified, enable all workarounds. */
2349 if (bfin_cpu_type == BFIN_CPU_UNKNOWN)
2350 {
2351 int i;
2352
2353 for (i = 0; bfin_cpus[i].name != NULL; i++)
2354 bfin_workarounds |= bfin_cpus[i].workarounds;
2355
2356 bfin_si_revision = 0xffff;
2357 }
2358
2359 if (bfin_csync_anomaly == 1)
2360 bfin_workarounds |= WA_SPECULATIVE_SYNCS;
2361 else if (bfin_csync_anomaly == 0)
2362 bfin_workarounds &= ~WA_SPECULATIVE_SYNCS;
2363
2364 if (bfin_specld_anomaly == 1)
2365 bfin_workarounds |= WA_SPECULATIVE_LOADS;
2366 else if (bfin_specld_anomaly == 0)
2367 bfin_workarounds &= ~WA_SPECULATIVE_LOADS;
2368
2369 if (TARGET_OMIT_LEAF_FRAME_POINTER)
2370 flag_omit_frame_pointer = 1;
2371
2372 #ifdef SUBTARGET_FDPIC_NOT_SUPPORTED
2373 if (TARGET_FDPIC)
2374 error ("-mfdpic is not supported, please use a bfin-linux-uclibc target");
2375 #endif
2376
2377 /* Library identification */
2378 if (global_options_set.x_bfin_library_id && ! TARGET_ID_SHARED_LIBRARY)
2379 error ("-mshared-library-id= specified without -mid-shared-library");
2380
2381 if (stack_limit_rtx && TARGET_FDPIC)
2382 {
2383 warning (0, "-fstack-limit- options are ignored with -mfdpic; use -mstack-check-l1");
2384 stack_limit_rtx = NULL_RTX;
2385 }
2386
2387 if (stack_limit_rtx && TARGET_STACK_CHECK_L1)
2388 error ("can%'t use multiple stack checking methods together");
2389
2390 if (TARGET_ID_SHARED_LIBRARY && TARGET_FDPIC)
2391 error ("ID shared libraries and FD-PIC mode can%'t be used together");
2392
2393 /* Don't allow the user to specify -mid-shared-library and -msep-data
2394 together, as it makes little sense from a user's point of view... */
2395 if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
2396 error ("cannot specify both -msep-data and -mid-shared-library");
2397 /* ... internally, however, it's nearly the same. */
2398 if (TARGET_SEP_DATA)
2399 target_flags |= MASK_ID_SHARED_LIBRARY | MASK_LEAF_ID_SHARED_LIBRARY;
2400
2401 if (TARGET_ID_SHARED_LIBRARY && flag_pic == 0)
2402 flag_pic = 1;
2403
2404 /* There is no single unaligned SI op for PIC code. Sometimes we
2405 need to use ".4byte" and sometimes we need to use ".picptr".
2406 See bfin_assemble_integer for details. */
2407 if (TARGET_FDPIC)
2408 targetm.asm_out.unaligned_op.si = 0;
2409
2410 /* Silently turn off flag_pic if not doing FDPIC or ID shared libraries,
2411 since we don't support it and it'll just break. */
2412 if (flag_pic && !TARGET_FDPIC && !TARGET_ID_SHARED_LIBRARY)
2413 flag_pic = 0;
2414
2415 if (TARGET_MULTICORE && bfin_cpu_type != BFIN_CPU_BF561)
2416 error ("-mmulticore can only be used with BF561");
2417
2418 if (TARGET_COREA && !TARGET_MULTICORE)
2419 error ("-mcorea should be used with -mmulticore");
2420
2421 if (TARGET_COREB && !TARGET_MULTICORE)
2422 error ("-mcoreb should be used with -mmulticore");
2423
2424 if (TARGET_COREA && TARGET_COREB)
2425 error ("-mcorea and -mcoreb can%'t be used together");
2426
2427 flag_schedule_insns = 0;
2428
2429 init_machine_status = bfin_init_machine_status;
2430 }
2431
2432 /* Return the destination address of BRANCH.
2433 We need to use this instead of get_attr_length, because the
2434 cbranch_with_nops pattern conservatively sets its length to 6, and
2435 we still prefer to use shorter sequences. */
2436
2437 static int
2438 branch_dest (rtx_insn *branch)
2439 {
2440 rtx dest;
2441 int dest_uid;
2442 rtx pat = PATTERN (branch);
2443 if (GET_CODE (pat) == PARALLEL)
2444 pat = XVECEXP (pat, 0, 0);
2445 dest = SET_SRC (pat);
2446 if (GET_CODE (dest) == IF_THEN_ELSE)
2447 dest = XEXP (dest, 1);
2448 dest = XEXP (dest, 0);
2449 dest_uid = INSN_UID (dest);
2450 return INSN_ADDRESSES (dest_uid);
2451 }
2452
2453 /* Return nonzero if INSN is annotated with a REG_BR_PROB note that indicates
2454 it's a branch that's predicted taken. */
2455
2456 static int
2457 cbranch_predicted_taken_p (rtx insn)
2458 {
2459 rtx x = find_reg_note (insn, REG_BR_PROB, 0);
2460
2461 if (x)
2462 {
2463 int pred_val = XINT (x, 0);
2464
2465 return pred_val >= REG_BR_PROB_BASE / 2;
2466 }
2467
2468 return 0;
2469 }
2470
2471 /* Templates for use by asm_conditional_branch. */
2472
2473 static const char *ccbranch_templates[][3] = {
2474 { "if !cc jump %3;", "if cc jump 4 (bp); jump.s %3;", "if cc jump 6 (bp); jump.l %3;" },
2475 { "if cc jump %3;", "if !cc jump 4 (bp); jump.s %3;", "if !cc jump 6 (bp); jump.l %3;" },
2476 { "if !cc jump %3 (bp);", "if cc jump 4; jump.s %3;", "if cc jump 6; jump.l %3;" },
2477 { "if cc jump %3 (bp);", "if !cc jump 4; jump.s %3;", "if !cc jump 6; jump.l %3;" },
2478 };
2479
2480 /* Output INSN, which is a conditional branch instruction with operands
2481 OPERANDS.
2482
2483 We deal with the various forms of conditional branches that can be generated
2484 by bfin_reorg to prevent the hardware from doing speculative loads, by
2485 - emitting a sufficient number of nops, if N_NOPS is nonzero, or
2486 - always emitting the branch as predicted taken, if PREDICT_TAKEN is true.
2487 Either of these is only necessary if the branch is short, otherwise the
2488 template we use ends in an unconditional jump which flushes the pipeline
2489 anyway. */
2490
2491 void
2492 asm_conditional_branch (rtx_insn *insn, rtx *operands, int n_nops, int predict_taken)
2493 {
2494 int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
2495 /* Note : offset for instructions like if cc jmp; jump.[sl] offset
2496 is to be taken from start of if cc rather than jump.
2497 Range for jump.s is (-4094, 4096) instead of (-4096, 4094)
2498 */
2499 int len = (offset >= -1024 && offset <= 1022 ? 0
2500 : offset >= -4094 && offset <= 4096 ? 1
2501 : 2);
2502 int bp = predict_taken && len == 0 ? 1 : cbranch_predicted_taken_p (insn);
2503 int idx = (bp << 1) | (GET_CODE (operands[0]) == EQ ? BRF : BRT);
2504 output_asm_insn (ccbranch_templates[idx][len], operands);
2505 gcc_assert (n_nops == 0 || !bp);
2506 if (len == 0)
2507 while (n_nops-- > 0)
2508 output_asm_insn ("nop;", NULL);
2509 }
2510
2511 /* Emit rtl for a comparison operation CMP in mode MODE. Operands have been
2512 stored in bfin_compare_op0 and bfin_compare_op1 already. */
2513
2514 rtx
2515 bfin_gen_compare (rtx cmp, machine_mode mode ATTRIBUTE_UNUSED)
2516 {
2517 enum rtx_code code1, code2;
2518 rtx op0 = XEXP (cmp, 0), op1 = XEXP (cmp, 1);
2519 rtx tem = bfin_cc_rtx;
2520 enum rtx_code code = GET_CODE (cmp);
2521
2522 /* If we have a BImode input, then we already have a compare result, and
2523 do not need to emit another comparison. */
2524 if (GET_MODE (op0) == BImode)
2525 {
2526 gcc_assert ((code == NE || code == EQ) && op1 == const0_rtx);
2527 tem = op0, code2 = code;
2528 }
2529 else
2530 {
2531 switch (code) {
2532 /* bfin has these conditions */
2533 case EQ:
2534 case LT:
2535 case LE:
2536 case LEU:
2537 case LTU:
2538 code1 = code;
2539 code2 = NE;
2540 break;
2541 default:
2542 code1 = reverse_condition (code);
2543 code2 = EQ;
2544 break;
2545 }
2546 emit_insn (gen_rtx_SET (tem, gen_rtx_fmt_ee (code1, BImode, op0, op1)));
2547 }
2548
2549 return gen_rtx_fmt_ee (code2, BImode, tem, CONST0_RTX (BImode));
2550 }
2551 \f
2552 /* Return nonzero iff C has exactly one bit set if it is interpreted
2553 as a 32-bit constant. */
2554
2555 int
2556 log2constp (unsigned HOST_WIDE_INT c)
2557 {
2558 c &= 0xFFFFFFFF;
2559 return c != 0 && (c & (c-1)) == 0;
2560 }
2561
2562 /* Returns the number of consecutive least significant zeros in the binary
2563 representation of *V.
2564 We modify *V to contain the original value arithmetically shifted right by
2565 the number of zeroes. */
2566
2567 static int
2568 shiftr_zero (HOST_WIDE_INT *v)
2569 {
2570 unsigned HOST_WIDE_INT tmp = *v;
2571 unsigned HOST_WIDE_INT sgn;
2572 int n = 0;
2573
2574 if (tmp == 0)
2575 return 0;
2576
2577 sgn = tmp & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1));
2578 while ((tmp & 0x1) == 0 && n <= 32)
2579 {
2580 tmp = (tmp >> 1) | sgn;
2581 n++;
2582 }
2583 *v = tmp;
2584 return n;
2585 }
2586
2587 /* After reload, split the load of an immediate constant. OPERANDS are the
2588 operands of the movsi_insn pattern which we are splitting. We return
2589 nonzero if we emitted a sequence to load the constant, zero if we emitted
2590 nothing because we want to use the splitter's default sequence. */
2591
2592 int
2593 split_load_immediate (rtx operands[])
2594 {
2595 HOST_WIDE_INT val = INTVAL (operands[1]);
2596 HOST_WIDE_INT tmp;
2597 HOST_WIDE_INT shifted = val;
2598 HOST_WIDE_INT shifted_compl = ~val;
2599 int num_zero = shiftr_zero (&shifted);
2600 int num_compl_zero = shiftr_zero (&shifted_compl);
2601 unsigned int regno = REGNO (operands[0]);
2602
2603 /* This case takes care of single-bit set/clear constants, which we could
2604 also implement with BITSET/BITCLR. */
2605 if (num_zero
2606 && shifted >= -32768 && shifted < 65536
2607 && (D_REGNO_P (regno)
2608 || (regno >= REG_P0 && regno <= REG_P7 && num_zero <= 2)))
2609 {
2610 emit_insn (gen_movsi (operands[0], gen_int_mode (shifted, SImode)));
2611 emit_insn (gen_ashlsi3 (operands[0], operands[0], GEN_INT (num_zero)));
2612 return 1;
2613 }
2614
2615 tmp = val & 0xFFFF;
2616 tmp |= -(tmp & 0x8000);
2617
2618 /* If high word has one bit set or clear, try to use a bit operation. */
2619 if (D_REGNO_P (regno))
2620 {
2621 if (log2constp (val & 0xFFFF0000))
2622 {
2623 emit_insn (gen_movsi (operands[0], GEN_INT (val & 0xFFFF)));
2624 emit_insn (gen_iorsi3 (operands[0], operands[0],
2625 gen_int_mode (val & 0xFFFF0000, SImode)));
2626 return 1;
2627 }
2628 else if (log2constp (val | 0xFFFF) && (val & 0x8000) != 0)
2629 {
2630 emit_insn (gen_movsi (operands[0], GEN_INT (tmp)));
2631 emit_insn (gen_andsi3 (operands[0], operands[0],
2632 gen_int_mode (val | 0xFFFF, SImode)));
2633 }
2634 }
2635
2636 if (D_REGNO_P (regno))
2637 {
2638 if (tmp >= -64 && tmp <= 63)
2639 {
2640 emit_insn (gen_movsi (operands[0], GEN_INT (tmp)));
2641 emit_insn (gen_movstricthi_high (operands[0],
2642 gen_int_mode (val & -65536,
2643 SImode)));
2644 return 1;
2645 }
2646
2647 if ((val & 0xFFFF0000) == 0)
2648 {
2649 emit_insn (gen_movsi (operands[0], const0_rtx));
2650 emit_insn (gen_movsi_low (operands[0], operands[0], operands[1]));
2651 return 1;
2652 }
2653
2654 if ((val & 0xFFFF0000) == 0xFFFF0000)
2655 {
2656 emit_insn (gen_movsi (operands[0], constm1_rtx));
2657 emit_insn (gen_movsi_low (operands[0], operands[0], operands[1]));
2658 return 1;
2659 }
2660 }
2661
2662 /* Need DREGs for the remaining case. */
2663 if (regno > REG_R7)
2664 return 0;
2665
2666 if (optimize_size
2667 && num_compl_zero && shifted_compl >= -64 && shifted_compl <= 63)
2668 {
2669 /* If optimizing for size, generate a sequence that has more instructions
2670 but is shorter. */
2671 emit_insn (gen_movsi (operands[0], gen_int_mode (shifted_compl, SImode)));
2672 emit_insn (gen_ashlsi3 (operands[0], operands[0],
2673 GEN_INT (num_compl_zero)));
2674 emit_insn (gen_one_cmplsi2 (operands[0], operands[0]));
2675 return 1;
2676 }
2677 return 0;
2678 }
2679 \f
2680 /* Return true if the legitimate memory address for a memory operand of mode
2681 MODE. Return false if not. */
2682
2683 static bool
2684 bfin_valid_add (machine_mode mode, HOST_WIDE_INT value)
2685 {
2686 unsigned HOST_WIDE_INT v = value > 0 ? value : -value;
2687 int sz = GET_MODE_SIZE (mode);
2688 int shift = sz == 1 ? 0 : sz == 2 ? 1 : 2;
2689 /* The usual offsettable_memref machinery doesn't work so well for this
2690 port, so we deal with the problem here. */
2691 if (value > 0 && sz == 8)
2692 v += 4;
2693 return (v & ~(0x7fff << shift)) == 0;
2694 }
2695
2696 static bool
2697 bfin_valid_reg_p (unsigned int regno, int strict, machine_mode mode,
2698 enum rtx_code outer_code)
2699 {
2700 if (strict)
2701 return REGNO_OK_FOR_BASE_STRICT_P (regno, mode, outer_code, SCRATCH);
2702 else
2703 return REGNO_OK_FOR_BASE_NONSTRICT_P (regno, mode, outer_code, SCRATCH);
2704 }
2705
2706 /* Recognize an RTL expression that is a valid memory address for an
2707 instruction. The MODE argument is the machine mode for the MEM expression
2708 that wants to use this address.
2709
2710 Blackfin addressing modes are as follows:
2711
2712 [preg]
2713 [preg + imm16]
2714
2715 B [ Preg + uimm15 ]
2716 W [ Preg + uimm16m2 ]
2717 [ Preg + uimm17m4 ]
2718
2719 [preg++]
2720 [preg--]
2721 [--sp]
2722 */
2723
2724 static bool
2725 bfin_legitimate_address_p (machine_mode mode, rtx x, bool strict)
2726 {
2727 switch (GET_CODE (x)) {
2728 case REG:
2729 if (bfin_valid_reg_p (REGNO (x), strict, mode, MEM))
2730 return true;
2731 break;
2732 case PLUS:
2733 if (REG_P (XEXP (x, 0))
2734 && bfin_valid_reg_p (REGNO (XEXP (x, 0)), strict, mode, PLUS)
2735 && ((GET_CODE (XEXP (x, 1)) == UNSPEC && mode == SImode)
2736 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2737 && bfin_valid_add (mode, INTVAL (XEXP (x, 1))))))
2738 return true;
2739 break;
2740 case POST_INC:
2741 case POST_DEC:
2742 if (LEGITIMATE_MODE_FOR_AUTOINC_P (mode)
2743 && REG_P (XEXP (x, 0))
2744 && bfin_valid_reg_p (REGNO (XEXP (x, 0)), strict, mode, POST_INC))
2745 return true;
2746 case PRE_DEC:
2747 if (LEGITIMATE_MODE_FOR_AUTOINC_P (mode)
2748 && XEXP (x, 0) == stack_pointer_rtx
2749 && REG_P (XEXP (x, 0))
2750 && bfin_valid_reg_p (REGNO (XEXP (x, 0)), strict, mode, PRE_DEC))
2751 return true;
2752 break;
2753 default:
2754 break;
2755 }
2756 return false;
2757 }
2758
2759 /* Decide whether we can force certain constants to memory. If we
2760 decide we can't, the caller should be able to cope with it in
2761 another way. */
2762
2763 static bool
2764 bfin_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED,
2765 rtx x ATTRIBUTE_UNUSED)
2766 {
2767 /* We have only one class of non-legitimate constants, and our movsi
2768 expander knows how to handle them. Dropping these constants into the
2769 data section would only shift the problem - we'd still get relocs
2770 outside the object, in the data section rather than the text section. */
2771 return true;
2772 }
2773
2774 /* Ensure that for any constant of the form symbol + offset, the offset
2775 remains within the object. Any other constants are ok.
2776 This ensures that flat binaries never have to deal with relocations
2777 crossing section boundaries. */
2778
2779 static bool
2780 bfin_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2781 {
2782 rtx sym;
2783 HOST_WIDE_INT offset;
2784
2785 if (GET_CODE (x) != CONST)
2786 return true;
2787
2788 x = XEXP (x, 0);
2789 gcc_assert (GET_CODE (x) == PLUS);
2790
2791 sym = XEXP (x, 0);
2792 x = XEXP (x, 1);
2793 if (GET_CODE (sym) != SYMBOL_REF
2794 || GET_CODE (x) != CONST_INT)
2795 return true;
2796 offset = INTVAL (x);
2797
2798 if (SYMBOL_REF_DECL (sym) == 0)
2799 return true;
2800 if (offset < 0
2801 || offset >= int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (sym))))
2802 return false;
2803
2804 return true;
2805 }
2806
2807 static bool
2808 bfin_rtx_costs (rtx x, int code_i, int outer_code_i, int opno, int *total,
2809 bool speed)
2810 {
2811 enum rtx_code code = (enum rtx_code) code_i;
2812 enum rtx_code outer_code = (enum rtx_code) outer_code_i;
2813 int cost2 = COSTS_N_INSNS (1);
2814 rtx op0, op1;
2815
2816 switch (code)
2817 {
2818 case CONST_INT:
2819 if (outer_code == SET || outer_code == PLUS)
2820 *total = satisfies_constraint_Ks7 (x) ? 0 : cost2;
2821 else if (outer_code == AND)
2822 *total = log2constp (~INTVAL (x)) ? 0 : cost2;
2823 else if (outer_code == LE || outer_code == LT || outer_code == EQ)
2824 *total = (INTVAL (x) >= -4 && INTVAL (x) <= 3) ? 0 : cost2;
2825 else if (outer_code == LEU || outer_code == LTU)
2826 *total = (INTVAL (x) >= 0 && INTVAL (x) <= 7) ? 0 : cost2;
2827 else if (outer_code == MULT)
2828 *total = (INTVAL (x) == 2 || INTVAL (x) == 4) ? 0 : cost2;
2829 else if (outer_code == ASHIFT && (INTVAL (x) == 1 || INTVAL (x) == 2))
2830 *total = 0;
2831 else if (outer_code == ASHIFT || outer_code == ASHIFTRT
2832 || outer_code == LSHIFTRT)
2833 *total = (INTVAL (x) >= 0 && INTVAL (x) <= 31) ? 0 : cost2;
2834 else if (outer_code == IOR || outer_code == XOR)
2835 *total = (INTVAL (x) & (INTVAL (x) - 1)) == 0 ? 0 : cost2;
2836 else
2837 *total = cost2;
2838 return true;
2839
2840 case CONST:
2841 case LABEL_REF:
2842 case SYMBOL_REF:
2843 case CONST_DOUBLE:
2844 *total = COSTS_N_INSNS (2);
2845 return true;
2846
2847 case PLUS:
2848 op0 = XEXP (x, 0);
2849 op1 = XEXP (x, 1);
2850 if (GET_MODE (x) == SImode)
2851 {
2852 if (GET_CODE (op0) == MULT
2853 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
2854 {
2855 HOST_WIDE_INT val = INTVAL (XEXP (op0, 1));
2856 if (val == 2 || val == 4)
2857 {
2858 *total = cost2;
2859 *total += rtx_cost (XEXP (op0, 0), outer_code, opno, speed);
2860 *total += rtx_cost (op1, outer_code, opno, speed);
2861 return true;
2862 }
2863 }
2864 *total = cost2;
2865 if (GET_CODE (op0) != REG
2866 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2867 *total += set_src_cost (op0, speed);
2868 #if 0 /* We'd like to do this for accuracy, but it biases the loop optimizer
2869 towards creating too many induction variables. */
2870 if (!reg_or_7bit_operand (op1, SImode))
2871 *total += set_src_cost (op1, speed);
2872 #endif
2873 }
2874 else if (GET_MODE (x) == DImode)
2875 {
2876 *total = 6 * cost2;
2877 if (GET_CODE (op1) != CONST_INT
2878 || !satisfies_constraint_Ks7 (op1))
2879 *total += rtx_cost (op1, PLUS, 1, speed);
2880 if (GET_CODE (op0) != REG
2881 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2882 *total += rtx_cost (op0, PLUS, 0, speed);
2883 }
2884 return true;
2885
2886 case MINUS:
2887 if (GET_MODE (x) == DImode)
2888 *total = 6 * cost2;
2889 else
2890 *total = cost2;
2891 return true;
2892
2893 case ASHIFT:
2894 case ASHIFTRT:
2895 case LSHIFTRT:
2896 if (GET_MODE (x) == DImode)
2897 *total = 6 * cost2;
2898 else
2899 *total = cost2;
2900
2901 op0 = XEXP (x, 0);
2902 op1 = XEXP (x, 1);
2903 if (GET_CODE (op0) != REG
2904 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2905 *total += rtx_cost (op0, code, 0, speed);
2906
2907 return true;
2908
2909 case IOR:
2910 case AND:
2911 case XOR:
2912 op0 = XEXP (x, 0);
2913 op1 = XEXP (x, 1);
2914
2915 /* Handle special cases of IOR: rotates, ALIGN insns, movstricthi_high. */
2916 if (code == IOR)
2917 {
2918 if ((GET_CODE (op0) == LSHIFTRT && GET_CODE (op1) == ASHIFT)
2919 || (GET_CODE (op0) == ASHIFT && GET_CODE (op1) == ZERO_EXTEND)
2920 || (GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
2921 || (GET_CODE (op0) == AND && GET_CODE (op1) == CONST_INT))
2922 {
2923 *total = cost2;
2924 return true;
2925 }
2926 }
2927
2928 if (GET_CODE (op0) != REG
2929 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2930 *total += rtx_cost (op0, code, 0, speed);
2931
2932 if (GET_MODE (x) == DImode)
2933 {
2934 *total = 2 * cost2;
2935 return true;
2936 }
2937 *total = cost2;
2938 if (GET_MODE (x) != SImode)
2939 return true;
2940
2941 if (code == AND)
2942 {
2943 if (! rhs_andsi3_operand (XEXP (x, 1), SImode))
2944 *total += rtx_cost (XEXP (x, 1), code, 1, speed);
2945 }
2946 else
2947 {
2948 if (! regorlog2_operand (XEXP (x, 1), SImode))
2949 *total += rtx_cost (XEXP (x, 1), code, 1, speed);
2950 }
2951
2952 return true;
2953
2954 case ZERO_EXTRACT:
2955 case SIGN_EXTRACT:
2956 if (outer_code == SET
2957 && XEXP (x, 1) == const1_rtx
2958 && GET_CODE (XEXP (x, 2)) == CONST_INT)
2959 {
2960 *total = 2 * cost2;
2961 return true;
2962 }
2963 /* fall through */
2964
2965 case SIGN_EXTEND:
2966 case ZERO_EXTEND:
2967 *total = cost2;
2968 return true;
2969
2970 case MULT:
2971 {
2972 op0 = XEXP (x, 0);
2973 op1 = XEXP (x, 1);
2974 if (GET_CODE (op0) == GET_CODE (op1)
2975 && (GET_CODE (op0) == ZERO_EXTEND
2976 || GET_CODE (op0) == SIGN_EXTEND))
2977 {
2978 *total = COSTS_N_INSNS (1);
2979 op0 = XEXP (op0, 0);
2980 op1 = XEXP (op1, 0);
2981 }
2982 else if (!speed)
2983 *total = COSTS_N_INSNS (1);
2984 else
2985 *total = COSTS_N_INSNS (3);
2986
2987 if (GET_CODE (op0) != REG
2988 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
2989 *total += rtx_cost (op0, MULT, 0, speed);
2990 if (GET_CODE (op1) != REG
2991 && (GET_CODE (op1) != SUBREG || GET_CODE (SUBREG_REG (op1)) != REG))
2992 *total += rtx_cost (op1, MULT, 1, speed);
2993 }
2994 return true;
2995
2996 case UDIV:
2997 case UMOD:
2998 *total = COSTS_N_INSNS (32);
2999 return true;
3000
3001 case VEC_CONCAT:
3002 case VEC_SELECT:
3003 if (outer_code == SET)
3004 *total = cost2;
3005 return true;
3006
3007 default:
3008 return false;
3009 }
3010 }
3011 \f
3012 /* Used for communication between {push,pop}_multiple_operation (which
3013 we use not only as a predicate) and the corresponding output functions. */
3014 static int first_preg_to_save, first_dreg_to_save;
3015 static int n_regs_to_save;
3016
3017 int
3018 analyze_push_multiple_operation (rtx op)
3019 {
3020 int lastdreg = 8, lastpreg = 6;
3021 int i, group;
3022
3023 first_preg_to_save = lastpreg;
3024 first_dreg_to_save = lastdreg;
3025 for (i = 1, group = 0; i < XVECLEN (op, 0) - 1; i++)
3026 {
3027 rtx t = XVECEXP (op, 0, i);
3028 rtx src, dest;
3029 int regno;
3030
3031 if (GET_CODE (t) != SET)
3032 return 0;
3033
3034 src = SET_SRC (t);
3035 dest = SET_DEST (t);
3036 if (GET_CODE (dest) != MEM || ! REG_P (src))
3037 return 0;
3038 dest = XEXP (dest, 0);
3039 if (GET_CODE (dest) != PLUS
3040 || ! REG_P (XEXP (dest, 0))
3041 || REGNO (XEXP (dest, 0)) != REG_SP
3042 || GET_CODE (XEXP (dest, 1)) != CONST_INT
3043 || INTVAL (XEXP (dest, 1)) != -i * 4)
3044 return 0;
3045
3046 regno = REGNO (src);
3047 if (group == 0)
3048 {
3049 if (D_REGNO_P (regno))
3050 {
3051 group = 1;
3052 first_dreg_to_save = lastdreg = regno - REG_R0;
3053 }
3054 else if (regno >= REG_P0 && regno <= REG_P7)
3055 {
3056 group = 2;
3057 first_preg_to_save = lastpreg = regno - REG_P0;
3058 }
3059 else
3060 return 0;
3061
3062 continue;
3063 }
3064
3065 if (group == 1)
3066 {
3067 if (regno >= REG_P0 && regno <= REG_P7)
3068 {
3069 group = 2;
3070 first_preg_to_save = lastpreg = regno - REG_P0;
3071 }
3072 else if (regno != REG_R0 + lastdreg + 1)
3073 return 0;
3074 else
3075 lastdreg++;
3076 }
3077 else if (group == 2)
3078 {
3079 if (regno != REG_P0 + lastpreg + 1)
3080 return 0;
3081 lastpreg++;
3082 }
3083 }
3084 n_regs_to_save = 8 - first_dreg_to_save + 6 - first_preg_to_save;
3085 return 1;
3086 }
3087
3088 int
3089 analyze_pop_multiple_operation (rtx op)
3090 {
3091 int lastdreg = 8, lastpreg = 6;
3092 int i, group;
3093
3094 for (i = 1, group = 0; i < XVECLEN (op, 0); i++)
3095 {
3096 rtx t = XVECEXP (op, 0, i);
3097 rtx src, dest;
3098 int regno;
3099
3100 if (GET_CODE (t) != SET)
3101 return 0;
3102
3103 src = SET_SRC (t);
3104 dest = SET_DEST (t);
3105 if (GET_CODE (src) != MEM || ! REG_P (dest))
3106 return 0;
3107 src = XEXP (src, 0);
3108
3109 if (i == 1)
3110 {
3111 if (! REG_P (src) || REGNO (src) != REG_SP)
3112 return 0;
3113 }
3114 else if (GET_CODE (src) != PLUS
3115 || ! REG_P (XEXP (src, 0))
3116 || REGNO (XEXP (src, 0)) != REG_SP
3117 || GET_CODE (XEXP (src, 1)) != CONST_INT
3118 || INTVAL (XEXP (src, 1)) != (i - 1) * 4)
3119 return 0;
3120
3121 regno = REGNO (dest);
3122 if (group == 0)
3123 {
3124 if (regno == REG_R7)
3125 {
3126 group = 1;
3127 lastdreg = 7;
3128 }
3129 else if (regno != REG_P0 + lastpreg - 1)
3130 return 0;
3131 else
3132 lastpreg--;
3133 }
3134 else if (group == 1)
3135 {
3136 if (regno != REG_R0 + lastdreg - 1)
3137 return 0;
3138 else
3139 lastdreg--;
3140 }
3141 }
3142 first_dreg_to_save = lastdreg;
3143 first_preg_to_save = lastpreg;
3144 n_regs_to_save = 8 - first_dreg_to_save + 6 - first_preg_to_save;
3145 return 1;
3146 }
3147
3148 /* Emit assembly code for one multi-register push described by INSN, with
3149 operands in OPERANDS. */
3150
3151 void
3152 output_push_multiple (rtx insn, rtx *operands)
3153 {
3154 char buf[80];
3155 int ok;
3156
3157 /* Validate the insn again, and compute first_[dp]reg_to_save. */
3158 ok = analyze_push_multiple_operation (PATTERN (insn));
3159 gcc_assert (ok);
3160
3161 if (first_dreg_to_save == 8)
3162 sprintf (buf, "[--sp] = ( p5:%d );\n", first_preg_to_save);
3163 else if (first_preg_to_save == 6)
3164 sprintf (buf, "[--sp] = ( r7:%d );\n", first_dreg_to_save);
3165 else
3166 sprintf (buf, "[--sp] = ( r7:%d, p5:%d );\n",
3167 first_dreg_to_save, first_preg_to_save);
3168
3169 output_asm_insn (buf, operands);
3170 }
3171
3172 /* Emit assembly code for one multi-register pop described by INSN, with
3173 operands in OPERANDS. */
3174
3175 void
3176 output_pop_multiple (rtx insn, rtx *operands)
3177 {
3178 char buf[80];
3179 int ok;
3180
3181 /* Validate the insn again, and compute first_[dp]reg_to_save. */
3182 ok = analyze_pop_multiple_operation (PATTERN (insn));
3183 gcc_assert (ok);
3184
3185 if (first_dreg_to_save == 8)
3186 sprintf (buf, "( p5:%d ) = [sp++];\n", first_preg_to_save);
3187 else if (first_preg_to_save == 6)
3188 sprintf (buf, "( r7:%d ) = [sp++];\n", first_dreg_to_save);
3189 else
3190 sprintf (buf, "( r7:%d, p5:%d ) = [sp++];\n",
3191 first_dreg_to_save, first_preg_to_save);
3192
3193 output_asm_insn (buf, operands);
3194 }
3195
3196 /* Adjust DST and SRC by OFFSET bytes, and generate one move in mode MODE. */
3197
3198 static void
3199 single_move_for_movmem (rtx dst, rtx src, machine_mode mode, HOST_WIDE_INT offset)
3200 {
3201 rtx scratch = gen_reg_rtx (mode);
3202 rtx srcmem, dstmem;
3203
3204 srcmem = adjust_address_nv (src, mode, offset);
3205 dstmem = adjust_address_nv (dst, mode, offset);
3206 emit_move_insn (scratch, srcmem);
3207 emit_move_insn (dstmem, scratch);
3208 }
3209
3210 /* Expand a string move operation of COUNT_EXP bytes from SRC to DST, with
3211 alignment ALIGN_EXP. Return true if successful, false if we should fall
3212 back on a different method. */
3213
3214 bool
3215 bfin_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
3216 {
3217 rtx srcreg, destreg, countreg;
3218 HOST_WIDE_INT align = 0;
3219 unsigned HOST_WIDE_INT count = 0;
3220
3221 if (GET_CODE (align_exp) == CONST_INT)
3222 align = INTVAL (align_exp);
3223 if (GET_CODE (count_exp) == CONST_INT)
3224 {
3225 count = INTVAL (count_exp);
3226 #if 0
3227 if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
3228 return false;
3229 #endif
3230 }
3231
3232 /* If optimizing for size, only do single copies inline. */
3233 if (optimize_size)
3234 {
3235 if (count == 2 && align < 2)
3236 return false;
3237 if (count == 4 && align < 4)
3238 return false;
3239 if (count != 1 && count != 2 && count != 4)
3240 return false;
3241 }
3242 if (align < 2 && count != 1)
3243 return false;
3244
3245 destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
3246 if (destreg != XEXP (dst, 0))
3247 dst = replace_equiv_address_nv (dst, destreg);
3248 srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
3249 if (srcreg != XEXP (src, 0))
3250 src = replace_equiv_address_nv (src, srcreg);
3251
3252 if (count != 0 && align >= 2)
3253 {
3254 unsigned HOST_WIDE_INT offset = 0;
3255
3256 if (align >= 4)
3257 {
3258 if ((count & ~3) == 4)
3259 {
3260 single_move_for_movmem (dst, src, SImode, offset);
3261 offset = 4;
3262 }
3263 else if (count & ~3)
3264 {
3265 HOST_WIDE_INT new_count = ((count >> 2) & 0x3fffffff) - 1;
3266 countreg = copy_to_mode_reg (Pmode, GEN_INT (new_count));
3267
3268 emit_insn (gen_rep_movsi (destreg, srcreg, countreg, destreg, srcreg));
3269 cfun->machine->has_loopreg_clobber = true;
3270 }
3271 if (count & 2)
3272 {
3273 single_move_for_movmem (dst, src, HImode, offset);
3274 offset += 2;
3275 }
3276 }
3277 else
3278 {
3279 if ((count & ~1) == 2)
3280 {
3281 single_move_for_movmem (dst, src, HImode, offset);
3282 offset = 2;
3283 }
3284 else if (count & ~1)
3285 {
3286 HOST_WIDE_INT new_count = ((count >> 1) & 0x7fffffff) - 1;
3287 countreg = copy_to_mode_reg (Pmode, GEN_INT (new_count));
3288
3289 emit_insn (gen_rep_movhi (destreg, srcreg, countreg, destreg, srcreg));
3290 cfun->machine->has_loopreg_clobber = true;
3291 }
3292 }
3293 if (count & 1)
3294 {
3295 single_move_for_movmem (dst, src, QImode, offset);
3296 }
3297 return true;
3298 }
3299 return false;
3300 }
3301 \f
3302 /* Compute the alignment for a local variable.
3303 TYPE is the data type, and ALIGN is the alignment that
3304 the object would ordinarily have. The value of this macro is used
3305 instead of that alignment to align the object. */
3306
3307 unsigned
3308 bfin_local_alignment (tree type, unsigned align)
3309 {
3310 /* Increasing alignment for (relatively) big types allows the builtin
3311 memcpy can use 32 bit loads/stores. */
3312 if (TYPE_SIZE (type)
3313 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3314 && wi::gtu_p (TYPE_SIZE (type), 8)
3315 && align < 32)
3316 return 32;
3317 return align;
3318 }
3319 \f
3320 /* Implement TARGET_SCHED_ISSUE_RATE. */
3321
3322 static int
3323 bfin_issue_rate (void)
3324 {
3325 return 3;
3326 }
3327
3328 static int
3329 bfin_adjust_cost (rtx_insn *insn, rtx link, rtx_insn *dep_insn, int cost)
3330 {
3331 enum attr_type dep_insn_type;
3332 int dep_insn_code_number;
3333
3334 /* Anti and output dependencies have zero cost. */
3335 if (REG_NOTE_KIND (link) != 0)
3336 return 0;
3337
3338 dep_insn_code_number = recog_memoized (dep_insn);
3339
3340 /* If we can't recognize the insns, we can't really do anything. */
3341 if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
3342 return cost;
3343
3344 dep_insn_type = get_attr_type (dep_insn);
3345
3346 if (dep_insn_type == TYPE_MOVE || dep_insn_type == TYPE_MCLD)
3347 {
3348 rtx pat = PATTERN (dep_insn);
3349 rtx dest, src;
3350
3351 if (GET_CODE (pat) == PARALLEL)
3352 pat = XVECEXP (pat, 0, 0);
3353 dest = SET_DEST (pat);
3354 src = SET_SRC (pat);
3355 if (! ADDRESS_REGNO_P (REGNO (dest))
3356 || ! (MEM_P (src) || D_REGNO_P (REGNO (src))))
3357 return cost;
3358 return cost + (dep_insn_type == TYPE_MOVE ? 4 : 3);
3359 }
3360
3361 return cost;
3362 }
3363 \f
3364 /* This function acts like NEXT_INSN, but is aware of three-insn bundles and
3365 skips all subsequent parallel instructions if INSN is the start of such
3366 a group. */
3367 static rtx_insn *
3368 find_next_insn_start (rtx_insn *insn)
3369 {
3370 if (GET_MODE (insn) == SImode)
3371 {
3372 while (GET_MODE (insn) != QImode)
3373 insn = NEXT_INSN (insn);
3374 }
3375 return NEXT_INSN (insn);
3376 }
3377
3378 /* This function acts like PREV_INSN, but is aware of three-insn bundles and
3379 skips all subsequent parallel instructions if INSN is the start of such
3380 a group. */
3381 static rtx_insn *
3382 find_prev_insn_start (rtx_insn *insn)
3383 {
3384 insn = PREV_INSN (insn);
3385 gcc_assert (GET_MODE (insn) != SImode);
3386 if (GET_MODE (insn) == QImode)
3387 {
3388 while (GET_MODE (PREV_INSN (insn)) == SImode)
3389 insn = PREV_INSN (insn);
3390 }
3391 return insn;
3392 }
3393 \f
3394 /* Implement TARGET_CAN_USE_DOLOOP_P. */
3395
3396 static bool
3397 bfin_can_use_doloop_p (const widest_int &, const widest_int &iterations_max,
3398 unsigned int, bool)
3399 {
3400 /* Due to limitations in the hardware (an initial loop count of 0
3401 does not loop 2^32 times) we must avoid to generate a hardware
3402 loops when we cannot rule out this case. */
3403 if (!flag_unsafe_loop_optimizations
3404 && wi::geu_p (iterations_max, 0xFFFFFFFF))
3405 return false;
3406 return true;
3407 }
3408
3409 /* Increment the counter for the number of loop instructions in the
3410 current function. */
3411
3412 void
3413 bfin_hardware_loop (void)
3414 {
3415 cfun->machine->has_hardware_loops++;
3416 }
3417
3418 /* Maximum loop nesting depth. */
3419 #define MAX_LOOP_DEPTH 2
3420
3421 /* Maximum size of a loop. */
3422 #define MAX_LOOP_LENGTH 2042
3423
3424 /* Maximum distance of the LSETUP instruction from the loop start. */
3425 #define MAX_LSETUP_DISTANCE 30
3426
3427 /* Estimate the length of INSN conservatively. */
3428
3429 static int
3430 length_for_loop (rtx_insn *insn)
3431 {
3432 int length = 0;
3433 if (JUMP_P (insn) && any_condjump_p (insn) && !optimize_size)
3434 {
3435 if (ENABLE_WA_SPECULATIVE_SYNCS)
3436 length = 8;
3437 else if (ENABLE_WA_SPECULATIVE_LOADS)
3438 length = 6;
3439 }
3440 else if (LABEL_P (insn))
3441 {
3442 if (ENABLE_WA_SPECULATIVE_SYNCS)
3443 length = 4;
3444 }
3445
3446 if (NONDEBUG_INSN_P (insn))
3447 length += get_attr_length (insn);
3448
3449 return length;
3450 }
3451
3452 /* Optimize LOOP. */
3453
3454 static bool
3455 hwloop_optimize (hwloop_info loop)
3456 {
3457 basic_block bb;
3458 rtx_insn *insn, *last_insn;
3459 rtx loop_init, start_label, end_label;
3460 rtx iter_reg, scratchreg, scratch_init, scratch_init_insn;
3461 rtx lc_reg, lt_reg, lb_reg;
3462 rtx seq_end;
3463 rtx_insn *seq;
3464 int length;
3465 bool clobber0, clobber1;
3466
3467 if (loop->depth > MAX_LOOP_DEPTH)
3468 {
3469 if (dump_file)
3470 fprintf (dump_file, ";; loop %d too deep\n", loop->loop_no);
3471 return false;
3472 }
3473
3474 /* Get the loop iteration register. */
3475 iter_reg = loop->iter_reg;
3476
3477 gcc_assert (REG_P (iter_reg));
3478
3479 scratchreg = NULL_RTX;
3480 scratch_init = iter_reg;
3481 scratch_init_insn = NULL_RTX;
3482 if (!PREG_P (iter_reg) && loop->incoming_src)
3483 {
3484 basic_block bb_in = loop->incoming_src;
3485 int i;
3486 for (i = REG_P0; i <= REG_P5; i++)
3487 if ((df_regs_ever_live_p (i)
3488 || (funkind (TREE_TYPE (current_function_decl)) == SUBROUTINE
3489 && call_used_regs[i]))
3490 && !REGNO_REG_SET_P (df_get_live_out (bb_in), i))
3491 {
3492 scratchreg = gen_rtx_REG (SImode, i);
3493 break;
3494 }
3495 for (insn = BB_END (bb_in); insn != BB_HEAD (bb_in);
3496 insn = PREV_INSN (insn))
3497 {
3498 rtx set;
3499 if (NOTE_P (insn) || BARRIER_P (insn))
3500 continue;
3501 set = single_set (insn);
3502 if (set && rtx_equal_p (SET_DEST (set), iter_reg))
3503 {
3504 if (CONSTANT_P (SET_SRC (set)))
3505 {
3506 scratch_init = SET_SRC (set);
3507 scratch_init_insn = insn;
3508 }
3509 break;
3510 }
3511 else if (reg_mentioned_p (iter_reg, PATTERN (insn)))
3512 break;
3513 }
3514 }
3515
3516 if (loop->incoming_src)
3517 {
3518 /* Make sure the predecessor is before the loop start label, as required by
3519 the LSETUP instruction. */
3520 length = 0;
3521 insn = BB_END (loop->incoming_src);
3522 /* If we have to insert the LSETUP before a jump, count that jump in the
3523 length. */
3524 if (vec_safe_length (loop->incoming) > 1
3525 || !(loop->incoming->last ()->flags & EDGE_FALLTHRU))
3526 {
3527 gcc_assert (JUMP_P (insn));
3528 insn = PREV_INSN (insn);
3529 }
3530
3531 for (; insn && insn != loop->start_label; insn = NEXT_INSN (insn))
3532 length += length_for_loop (insn);
3533
3534 if (!insn)
3535 {
3536 if (dump_file)
3537 fprintf (dump_file, ";; loop %d lsetup not before loop_start\n",
3538 loop->loop_no);
3539 return false;
3540 }
3541
3542 /* Account for the pop of a scratch register where necessary. */
3543 if (!PREG_P (iter_reg) && scratchreg == NULL_RTX
3544 && ENABLE_WA_LOAD_LCREGS)
3545 length += 2;
3546
3547 if (length > MAX_LSETUP_DISTANCE)
3548 {
3549 if (dump_file)
3550 fprintf (dump_file, ";; loop %d lsetup too far away\n", loop->loop_no);
3551 return false;
3552 }
3553 }
3554
3555 /* Check if start_label appears before loop_end and calculate the
3556 offset between them. We calculate the length of instructions
3557 conservatively. */
3558 length = 0;
3559 for (insn = loop->start_label;
3560 insn && insn != loop->loop_end;
3561 insn = NEXT_INSN (insn))
3562 length += length_for_loop (insn);
3563
3564 if (!insn)
3565 {
3566 if (dump_file)
3567 fprintf (dump_file, ";; loop %d start_label not before loop_end\n",
3568 loop->loop_no);
3569 return false;
3570 }
3571
3572 loop->length = length;
3573 if (loop->length > MAX_LOOP_LENGTH)
3574 {
3575 if (dump_file)
3576 fprintf (dump_file, ";; loop %d too long\n", loop->loop_no);
3577 return false;
3578 }
3579
3580 /* Scan all the blocks to make sure they don't use iter_reg. */
3581 if (loop->iter_reg_used || loop->iter_reg_used_outside)
3582 {
3583 if (dump_file)
3584 fprintf (dump_file, ";; loop %d uses iterator\n", loop->loop_no);
3585 return false;
3586 }
3587
3588 clobber0 = (TEST_HARD_REG_BIT (loop->regs_set_in_loop, REG_LC0)
3589 || TEST_HARD_REG_BIT (loop->regs_set_in_loop, REG_LB0)
3590 || TEST_HARD_REG_BIT (loop->regs_set_in_loop, REG_LT0));
3591 clobber1 = (TEST_HARD_REG_BIT (loop->regs_set_in_loop, REG_LC1)
3592 || TEST_HARD_REG_BIT (loop->regs_set_in_loop, REG_LB1)
3593 || TEST_HARD_REG_BIT (loop->regs_set_in_loop, REG_LT1));
3594 if (clobber0 && clobber1)
3595 {
3596 if (dump_file)
3597 fprintf (dump_file, ";; loop %d no loop reg available\n",
3598 loop->loop_no);
3599 return false;
3600 }
3601
3602 /* There should be an instruction before the loop_end instruction
3603 in the same basic block. And the instruction must not be
3604 - JUMP
3605 - CONDITIONAL BRANCH
3606 - CALL
3607 - CSYNC
3608 - SSYNC
3609 - Returns (RTS, RTN, etc.) */
3610
3611 bb = loop->tail;
3612 last_insn = find_prev_insn_start (loop->loop_end);
3613
3614 while (1)
3615 {
3616 for (; last_insn != BB_HEAD (bb);
3617 last_insn = find_prev_insn_start (last_insn))
3618 if (NONDEBUG_INSN_P (last_insn))
3619 break;
3620
3621 if (last_insn != BB_HEAD (bb))
3622 break;
3623
3624 if (single_pred_p (bb)
3625 && single_pred_edge (bb)->flags & EDGE_FALLTHRU
3626 && single_pred (bb) != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3627 {
3628 bb = single_pred (bb);
3629 last_insn = BB_END (bb);
3630 continue;
3631 }
3632 else
3633 {
3634 last_insn = NULL;
3635 break;
3636 }
3637 }
3638
3639 if (!last_insn)
3640 {
3641 if (dump_file)
3642 fprintf (dump_file, ";; loop %d has no last instruction\n",
3643 loop->loop_no);
3644 return false;
3645 }
3646
3647 if (JUMP_P (last_insn) && !any_condjump_p (last_insn))
3648 {
3649 if (dump_file)
3650 fprintf (dump_file, ";; loop %d has bad last instruction\n",
3651 loop->loop_no);
3652 return false;
3653 }
3654 /* In all other cases, try to replace a bad last insn with a nop. */
3655 else if (JUMP_P (last_insn)
3656 || CALL_P (last_insn)
3657 || get_attr_type (last_insn) == TYPE_SYNC
3658 || get_attr_type (last_insn) == TYPE_CALL
3659 || get_attr_seq_insns (last_insn) == SEQ_INSNS_MULTI
3660 || recog_memoized (last_insn) == CODE_FOR_return_internal
3661 || GET_CODE (PATTERN (last_insn)) == ASM_INPUT
3662 || asm_noperands (PATTERN (last_insn)) >= 0)
3663 {
3664 if (loop->length + 2 > MAX_LOOP_LENGTH)
3665 {
3666 if (dump_file)
3667 fprintf (dump_file, ";; loop %d too long\n", loop->loop_no);
3668 return false;
3669 }
3670 if (dump_file)
3671 fprintf (dump_file, ";; loop %d has bad last insn; replace with nop\n",
3672 loop->loop_no);
3673
3674 last_insn = emit_insn_after (gen_forced_nop (), last_insn);
3675 }
3676
3677 loop->last_insn = last_insn;
3678
3679 /* The loop is good for replacement. */
3680 start_label = loop->start_label;
3681 end_label = gen_label_rtx ();
3682 iter_reg = loop->iter_reg;
3683
3684 if (loop->depth == 1 && !clobber1)
3685 {
3686 lc_reg = gen_rtx_REG (SImode, REG_LC1);
3687 lb_reg = gen_rtx_REG (SImode, REG_LB1);
3688 lt_reg = gen_rtx_REG (SImode, REG_LT1);
3689 SET_HARD_REG_BIT (loop->regs_set_in_loop, REG_LC1);
3690 }
3691 else
3692 {
3693 lc_reg = gen_rtx_REG (SImode, REG_LC0);
3694 lb_reg = gen_rtx_REG (SImode, REG_LB0);
3695 lt_reg = gen_rtx_REG (SImode, REG_LT0);
3696 SET_HARD_REG_BIT (loop->regs_set_in_loop, REG_LC0);
3697 }
3698
3699 loop->end_label = end_label;
3700
3701 /* Create a sequence containing the loop setup. */
3702 start_sequence ();
3703
3704 /* LSETUP only accepts P registers. If we have one, we can use it,
3705 otherwise there are several ways of working around the problem.
3706 If we're not affected by anomaly 312, we can load the LC register
3707 from any iteration register, and use LSETUP without initialization.
3708 If we've found a P scratch register that's not live here, we can
3709 instead copy the iter_reg into that and use an initializing LSETUP.
3710 If all else fails, push and pop P0 and use it as a scratch. */
3711 if (P_REGNO_P (REGNO (iter_reg)))
3712 {
3713 loop_init = gen_lsetup_with_autoinit (lt_reg, start_label,
3714 lb_reg, end_label,
3715 lc_reg, iter_reg);
3716 seq_end = emit_insn (loop_init);
3717 }
3718 else if (!ENABLE_WA_LOAD_LCREGS && DPREG_P (iter_reg))
3719 {
3720 emit_insn (gen_movsi (lc_reg, iter_reg));
3721 loop_init = gen_lsetup_without_autoinit (lt_reg, start_label,
3722 lb_reg, end_label,
3723 lc_reg);
3724 seq_end = emit_insn (loop_init);
3725 }
3726 else if (scratchreg != NULL_RTX)
3727 {
3728 emit_insn (gen_movsi (scratchreg, scratch_init));
3729 loop_init = gen_lsetup_with_autoinit (lt_reg, start_label,
3730 lb_reg, end_label,
3731 lc_reg, scratchreg);
3732 seq_end = emit_insn (loop_init);
3733 if (scratch_init_insn != NULL_RTX)
3734 delete_insn (scratch_init_insn);
3735 }
3736 else
3737 {
3738 rtx p0reg = gen_rtx_REG (SImode, REG_P0);
3739 rtx push = gen_frame_mem (SImode,
3740 gen_rtx_PRE_DEC (SImode, stack_pointer_rtx));
3741 rtx pop = gen_frame_mem (SImode,
3742 gen_rtx_POST_INC (SImode, stack_pointer_rtx));
3743 emit_insn (gen_movsi (push, p0reg));
3744 emit_insn (gen_movsi (p0reg, scratch_init));
3745 loop_init = gen_lsetup_with_autoinit (lt_reg, start_label,
3746 lb_reg, end_label,
3747 lc_reg, p0reg);
3748 emit_insn (loop_init);
3749 seq_end = emit_insn (gen_movsi (p0reg, pop));
3750 if (scratch_init_insn != NULL_RTX)
3751 delete_insn (scratch_init_insn);
3752 }
3753
3754 if (dump_file)
3755 {
3756 fprintf (dump_file, ";; replacing loop %d initializer with\n",
3757 loop->loop_no);
3758 print_rtl_single (dump_file, loop_init);
3759 fprintf (dump_file, ";; replacing loop %d terminator with\n",
3760 loop->loop_no);
3761 print_rtl_single (dump_file, loop->loop_end);
3762 }
3763
3764 /* If the loop isn't entered at the top, also create a jump to the entry
3765 point. */
3766 if (!loop->incoming_src && loop->head != loop->incoming_dest)
3767 {
3768 rtx label = BB_HEAD (loop->incoming_dest);
3769 /* If we're jumping to the final basic block in the loop, and there's
3770 only one cheap instruction before the end (typically an increment of
3771 an induction variable), we can just emit a copy here instead of a
3772 jump. */
3773 if (loop->incoming_dest == loop->tail
3774 && next_real_insn (label) == last_insn
3775 && asm_noperands (last_insn) < 0
3776 && GET_CODE (PATTERN (last_insn)) == SET)
3777 {
3778 seq_end = emit_insn (copy_rtx (PATTERN (last_insn)));
3779 }
3780 else
3781 {
3782 rtx_insn *ret = emit_jump_insn (gen_jump (label));
3783 JUMP_LABEL (ret) = label;
3784 LABEL_NUSES (label)++;
3785 seq_end = emit_barrier ();
3786 }
3787 }
3788
3789 seq = get_insns ();
3790 end_sequence ();
3791
3792 if (loop->incoming_src)
3793 {
3794 rtx_insn *prev = BB_END (loop->incoming_src);
3795 if (vec_safe_length (loop->incoming) > 1
3796 || !(loop->incoming->last ()->flags & EDGE_FALLTHRU))
3797 {
3798 gcc_assert (JUMP_P (prev));
3799 prev = PREV_INSN (prev);
3800 }
3801 emit_insn_after (seq, prev);
3802 }
3803 else
3804 {
3805 basic_block new_bb;
3806 edge e;
3807 edge_iterator ei;
3808
3809 #ifdef ENABLE_CHECKING
3810 if (loop->head != loop->incoming_dest)
3811 {
3812 /* We aren't entering the loop at the top. Since we've established
3813 that the loop is entered only at one point, this means there
3814 can't be fallthru edges into the head. Any such fallthru edges
3815 would become invalid when we insert the new block, so verify
3816 that this does not in fact happen. */
3817 FOR_EACH_EDGE (e, ei, loop->head->preds)
3818 gcc_assert (!(e->flags & EDGE_FALLTHRU));
3819 }
3820 #endif
3821
3822 emit_insn_before (seq, BB_HEAD (loop->head));
3823 seq = emit_label_before (gen_label_rtx (), seq);
3824
3825 new_bb = create_basic_block (seq, seq_end, loop->head->prev_bb);
3826 FOR_EACH_EDGE (e, ei, loop->incoming)
3827 {
3828 if (!(e->flags & EDGE_FALLTHRU)
3829 || e->dest != loop->head)
3830 redirect_edge_and_branch_force (e, new_bb);
3831 else
3832 redirect_edge_succ (e, new_bb);
3833 }
3834 e = make_edge (new_bb, loop->head, 0);
3835 }
3836
3837 delete_insn (loop->loop_end);
3838 /* Insert the loop end label before the last instruction of the loop. */
3839 emit_label_before (as_a <rtx_code_label *> (loop->end_label),
3840 loop->last_insn);
3841
3842 return true;
3843 }
3844
3845 /* A callback for the hw-doloop pass. Called when a loop we have discovered
3846 turns out not to be optimizable; we have to split the doloop_end pattern
3847 into a subtract and a test. */
3848 static void
3849 hwloop_fail (hwloop_info loop)
3850 {
3851 rtx insn = loop->loop_end;
3852
3853 if (DPREG_P (loop->iter_reg))
3854 {
3855 /* If loop->iter_reg is a DREG or PREG, we can split it here
3856 without scratch register. */
3857 rtx insn, test;
3858
3859 emit_insn_before (gen_addsi3 (loop->iter_reg,
3860 loop->iter_reg,
3861 constm1_rtx),
3862 loop->loop_end);
3863
3864 test = gen_rtx_NE (VOIDmode, loop->iter_reg, const0_rtx);
3865 insn = emit_jump_insn_before (gen_cbranchsi4 (test,
3866 loop->iter_reg, const0_rtx,
3867 loop->start_label),
3868 loop->loop_end);
3869
3870 JUMP_LABEL (insn) = loop->start_label;
3871 LABEL_NUSES (loop->start_label)++;
3872 delete_insn (loop->loop_end);
3873 }
3874 else
3875 {
3876 splitting_loops = 1;
3877 try_split (PATTERN (insn), safe_as_a <rtx_insn *> (insn), 1);
3878 splitting_loops = 0;
3879 }
3880 }
3881
3882 /* A callback for the hw-doloop pass. This function examines INSN; if
3883 it is a loop_end pattern we recognize, return the reg rtx for the
3884 loop counter. Otherwise, return NULL_RTX. */
3885
3886 static rtx
3887 hwloop_pattern_reg (rtx_insn *insn)
3888 {
3889 rtx reg;
3890
3891 if (!JUMP_P (insn) || recog_memoized (insn) != CODE_FOR_loop_end)
3892 return NULL_RTX;
3893
3894 reg = SET_DEST (XVECEXP (PATTERN (insn), 0, 1));
3895 if (!REG_P (reg))
3896 return NULL_RTX;
3897 return reg;
3898 }
3899
3900 static struct hw_doloop_hooks bfin_doloop_hooks =
3901 {
3902 hwloop_pattern_reg,
3903 hwloop_optimize,
3904 hwloop_fail
3905 };
3906
3907 /* Run from machine_dependent_reorg, this pass looks for doloop_end insns
3908 and tries to rewrite the RTL of these loops so that proper Blackfin
3909 hardware loops are generated. */
3910
3911 static void
3912 bfin_reorg_loops (void)
3913 {
3914 reorg_loops (true, &bfin_doloop_hooks);
3915 }
3916 \f
3917 /* Possibly generate a SEQUENCE out of three insns found in SLOT.
3918 Returns true if we modified the insn chain, false otherwise. */
3919 static bool
3920 gen_one_bundle (rtx_insn *slot[3])
3921 {
3922 gcc_assert (slot[1] != NULL_RTX);
3923
3924 /* Don't add extra NOPs if optimizing for size. */
3925 if (optimize_size
3926 && (slot[0] == NULL_RTX || slot[2] == NULL_RTX))
3927 return false;
3928
3929 /* Verify that we really can do the multi-issue. */
3930 if (slot[0])
3931 {
3932 rtx_insn *t = NEXT_INSN (slot[0]);
3933 while (t != slot[1])
3934 {
3935 if (! NOTE_P (t) || NOTE_KIND (t) != NOTE_INSN_DELETED)
3936 return false;
3937 t = NEXT_INSN (t);
3938 }
3939 }
3940 if (slot[2])
3941 {
3942 rtx_insn *t = NEXT_INSN (slot[1]);
3943 while (t != slot[2])
3944 {
3945 if (! NOTE_P (t) || NOTE_KIND (t) != NOTE_INSN_DELETED)
3946 return false;
3947 t = NEXT_INSN (t);
3948 }
3949 }
3950
3951 if (slot[0] == NULL_RTX)
3952 {
3953 slot[0] = emit_insn_before (gen_mnop (), slot[1]);
3954 df_insn_rescan (slot[0]);
3955 }
3956 if (slot[2] == NULL_RTX)
3957 {
3958 slot[2] = emit_insn_after (gen_forced_nop (), slot[1]);
3959 df_insn_rescan (slot[2]);
3960 }
3961
3962 /* Avoid line number information being printed inside one bundle. */
3963 if (INSN_LOCATION (slot[1])
3964 && INSN_LOCATION (slot[1]) != INSN_LOCATION (slot[0]))
3965 INSN_LOCATION (slot[1]) = INSN_LOCATION (slot[0]);
3966 if (INSN_LOCATION (slot[2])
3967 && INSN_LOCATION (slot[2]) != INSN_LOCATION (slot[0]))
3968 INSN_LOCATION (slot[2]) = INSN_LOCATION (slot[0]);
3969
3970 /* Terminate them with "|| " instead of ";" in the output. */
3971 PUT_MODE (slot[0], SImode);
3972 PUT_MODE (slot[1], SImode);
3973 /* Terminate the bundle, for the benefit of reorder_var_tracking_notes. */
3974 PUT_MODE (slot[2], QImode);
3975 return true;
3976 }
3977
3978 /* Go through all insns, and use the information generated during scheduling
3979 to generate SEQUENCEs to represent bundles of instructions issued
3980 simultaneously. */
3981
3982 static void
3983 bfin_gen_bundles (void)
3984 {
3985 basic_block bb;
3986 FOR_EACH_BB_FN (bb, cfun)
3987 {
3988 rtx_insn *insn, *next;
3989 rtx_insn *slot[3];
3990 int n_filled = 0;
3991
3992 slot[0] = slot[1] = slot[2] = NULL;
3993 for (insn = BB_HEAD (bb);; insn = next)
3994 {
3995 int at_end;
3996 rtx delete_this = NULL_RTX;
3997
3998 if (NONDEBUG_INSN_P (insn))
3999 {
4000 enum attr_type type = get_attr_type (insn);
4001
4002 if (type == TYPE_STALL)
4003 {
4004 gcc_assert (n_filled == 0);
4005 delete_this = insn;
4006 }
4007 else
4008 {
4009 if (type == TYPE_DSP32 || type == TYPE_DSP32SHIFTIMM)
4010 slot[0] = insn;
4011 else if (slot[1] == NULL_RTX)
4012 slot[1] = insn;
4013 else
4014 slot[2] = insn;
4015 n_filled++;
4016 }
4017 }
4018
4019 next = NEXT_INSN (insn);
4020 while (next && insn != BB_END (bb)
4021 && !(INSN_P (next)
4022 && GET_CODE (PATTERN (next)) != USE
4023 && GET_CODE (PATTERN (next)) != CLOBBER))
4024 {
4025 insn = next;
4026 next = NEXT_INSN (insn);
4027 }
4028
4029 /* BB_END can change due to emitting extra NOPs, so check here. */
4030 at_end = insn == BB_END (bb);
4031 if (delete_this == NULL_RTX && (at_end || GET_MODE (next) == TImode))
4032 {
4033 if ((n_filled < 2
4034 || !gen_one_bundle (slot))
4035 && slot[0] != NULL_RTX)
4036 {
4037 rtx pat = PATTERN (slot[0]);
4038 if (GET_CODE (pat) == SET
4039 && GET_CODE (SET_SRC (pat)) == UNSPEC
4040 && XINT (SET_SRC (pat), 1) == UNSPEC_32BIT)
4041 {
4042 SET_SRC (pat) = XVECEXP (SET_SRC (pat), 0, 0);
4043 INSN_CODE (slot[0]) = -1;
4044 df_insn_rescan (slot[0]);
4045 }
4046 }
4047 n_filled = 0;
4048 slot[0] = slot[1] = slot[2] = NULL;
4049 }
4050 if (delete_this != NULL_RTX)
4051 delete_insn (delete_this);
4052 if (at_end)
4053 break;
4054 }
4055 }
4056 }
4057
4058 /* Ensure that no var tracking notes are emitted in the middle of a
4059 three-instruction bundle. */
4060
4061 static void
4062 reorder_var_tracking_notes (void)
4063 {
4064 basic_block bb;
4065 FOR_EACH_BB_FN (bb, cfun)
4066 {
4067 rtx_insn *insn, *next;
4068 rtx_insn *queue = NULL;
4069 bool in_bundle = false;
4070
4071 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = next)
4072 {
4073 next = NEXT_INSN (insn);
4074
4075 if (INSN_P (insn))
4076 {
4077 /* Emit queued up notes at the last instruction of a bundle. */
4078 if (GET_MODE (insn) == QImode)
4079 {
4080 while (queue)
4081 {
4082 rtx_insn *next_queue = PREV_INSN (queue);
4083 SET_PREV_INSN (NEXT_INSN (insn)) = queue;
4084 SET_NEXT_INSN (queue) = NEXT_INSN (insn);
4085 SET_NEXT_INSN (insn) = queue;
4086 SET_PREV_INSN (queue) = insn;
4087 queue = next_queue;
4088 }
4089 in_bundle = false;
4090 }
4091 else if (GET_MODE (insn) == SImode)
4092 in_bundle = true;
4093 }
4094 else if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
4095 {
4096 if (in_bundle)
4097 {
4098 rtx_insn *prev = PREV_INSN (insn);
4099 SET_PREV_INSN (next) = prev;
4100 SET_NEXT_INSN (prev) = next;
4101
4102 SET_PREV_INSN (insn) = queue;
4103 queue = insn;
4104 }
4105 }
4106 }
4107 }
4108 }
4109 \f
4110 /* On some silicon revisions, functions shorter than a certain number of cycles
4111 can cause unpredictable behaviour. Work around this by adding NOPs as
4112 needed. */
4113 static void
4114 workaround_rts_anomaly (void)
4115 {
4116 rtx_insn *insn, *first_insn = NULL;
4117 int cycles = 4;
4118
4119 if (! ENABLE_WA_RETS)
4120 return;
4121
4122 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4123 {
4124 rtx pat;
4125
4126 if (BARRIER_P (insn))
4127 return;
4128
4129 if (NOTE_P (insn) || LABEL_P (insn))
4130 continue;
4131
4132 if (JUMP_TABLE_DATA_P (insn))
4133 continue;
4134
4135 if (first_insn == NULL_RTX)
4136 first_insn = insn;
4137 pat = PATTERN (insn);
4138 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
4139 || GET_CODE (pat) == ASM_INPUT
4140 || asm_noperands (pat) >= 0)
4141 continue;
4142
4143 if (CALL_P (insn))
4144 return;
4145
4146 if (JUMP_P (insn))
4147 {
4148 if (recog_memoized (insn) == CODE_FOR_return_internal)
4149 break;
4150
4151 /* Nothing to worry about for direct jumps. */
4152 if (!any_condjump_p (insn))
4153 return;
4154 if (cycles <= 1)
4155 return;
4156 cycles--;
4157 }
4158 else if (INSN_P (insn))
4159 {
4160 rtx pat = PATTERN (insn);
4161 int this_cycles = 1;
4162
4163 if (GET_CODE (pat) == PARALLEL)
4164 {
4165 if (analyze_push_multiple_operation (pat)
4166 || analyze_pop_multiple_operation (pat))
4167 this_cycles = n_regs_to_save;
4168 }
4169 else
4170 {
4171 int icode = recog_memoized (insn);
4172
4173 if (icode == CODE_FOR_link)
4174 this_cycles = 4;
4175 else if (icode == CODE_FOR_unlink)
4176 this_cycles = 3;
4177 else if (icode == CODE_FOR_mulsi3)
4178 this_cycles = 5;
4179 }
4180 if (this_cycles >= cycles)
4181 return;
4182
4183 cycles -= this_cycles;
4184 }
4185 }
4186 while (cycles > 0)
4187 {
4188 emit_insn_before (gen_nop (), first_insn);
4189 cycles--;
4190 }
4191 }
4192
4193 /* Return an insn type for INSN that can be used by the caller for anomaly
4194 workarounds. This differs from plain get_attr_type in that it handles
4195 SEQUENCEs. */
4196
4197 static enum attr_type
4198 type_for_anomaly (rtx_insn *insn)
4199 {
4200 rtx pat = PATTERN (insn);
4201 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (pat))
4202 {
4203 enum attr_type t;
4204 t = get_attr_type (seq->insn (1));
4205 if (t == TYPE_MCLD)
4206 return t;
4207 t = get_attr_type (seq->insn (2));
4208 if (t == TYPE_MCLD)
4209 return t;
4210 return TYPE_MCST;
4211 }
4212 else
4213 return get_attr_type (insn);
4214 }
4215
4216 /* Return true iff the address found in MEM is based on the register
4217 NP_REG and optionally has a positive offset. */
4218 static bool
4219 harmless_null_pointer_p (rtx mem, int np_reg)
4220 {
4221 mem = XEXP (mem, 0);
4222 if (GET_CODE (mem) == POST_INC || GET_CODE (mem) == POST_DEC)
4223 mem = XEXP (mem, 0);
4224 if (REG_P (mem) && (int) REGNO (mem) == np_reg)
4225 return true;
4226 if (GET_CODE (mem) == PLUS
4227 && REG_P (XEXP (mem, 0)) && (int) REGNO (XEXP (mem, 0)) == np_reg)
4228 {
4229 mem = XEXP (mem, 1);
4230 if (GET_CODE (mem) == CONST_INT && INTVAL (mem) > 0)
4231 return true;
4232 }
4233 return false;
4234 }
4235
4236 /* Return nonzero if INSN contains any loads that may trap. */
4237
4238 static bool
4239 trapping_loads_p (rtx_insn *insn, int np_reg, bool after_np_branch)
4240 {
4241 rtx mem = SET_SRC (single_set (insn));
4242
4243 if (!after_np_branch)
4244 np_reg = -1;
4245 return ((np_reg == -1 || !harmless_null_pointer_p (mem, np_reg))
4246 && may_trap_p (mem));
4247 }
4248
4249 /* Return INSN if it is of TYPE_MCLD. Alternatively, if INSN is the start of
4250 a three-insn bundle, see if one of them is a load and return that if so.
4251 Return NULL if the insn does not contain loads. */
4252 static rtx_insn *
4253 find_load (rtx_insn *insn)
4254 {
4255 if (!NONDEBUG_INSN_P (insn))
4256 return NULL;
4257 if (get_attr_type (insn) == TYPE_MCLD)
4258 return insn;
4259 if (GET_MODE (insn) != SImode)
4260 return NULL;
4261 do {
4262 insn = NEXT_INSN (insn);
4263 if ((GET_MODE (insn) == SImode || GET_MODE (insn) == QImode)
4264 && get_attr_type (insn) == TYPE_MCLD)
4265 return insn;
4266 } while (GET_MODE (insn) != QImode);
4267 return NULL;
4268 }
4269
4270 /* Determine whether PAT is an indirect call pattern. */
4271 static bool
4272 indirect_call_p (rtx pat)
4273 {
4274 if (GET_CODE (pat) == PARALLEL)
4275 pat = XVECEXP (pat, 0, 0);
4276 if (GET_CODE (pat) == SET)
4277 pat = SET_SRC (pat);
4278 gcc_assert (GET_CODE (pat) == CALL);
4279 pat = XEXP (pat, 0);
4280 gcc_assert (GET_CODE (pat) == MEM);
4281 pat = XEXP (pat, 0);
4282
4283 return REG_P (pat);
4284 }
4285
4286 /* During workaround_speculation, track whether we're in the shadow of a
4287 conditional branch that tests a P register for NULL. If so, we can omit
4288 emitting NOPs if we see a load from that P register, since a speculative
4289 access at address 0 isn't a problem, and the load is executed in all other
4290 cases anyway.
4291 Global for communication with note_np_check_stores through note_stores.
4292 */
4293 int np_check_regno = -1;
4294 bool np_after_branch = false;
4295
4296 /* Subroutine of workaround_speculation, called through note_stores. */
4297 static void
4298 note_np_check_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4299 void *data ATTRIBUTE_UNUSED)
4300 {
4301 if (REG_P (x) && (REGNO (x) == REG_CC || (int) REGNO (x) == np_check_regno))
4302 np_check_regno = -1;
4303 }
4304
4305 static void
4306 workaround_speculation (void)
4307 {
4308 rtx_insn *insn, *next;
4309 rtx_insn *last_condjump = NULL;
4310 int cycles_since_jump = INT_MAX;
4311 int delay_added = 0;
4312
4313 if (! ENABLE_WA_SPECULATIVE_LOADS && ! ENABLE_WA_SPECULATIVE_SYNCS
4314 && ! ENABLE_WA_INDIRECT_CALLS)
4315 return;
4316
4317 /* First pass: find predicted-false branches; if something after them
4318 needs nops, insert them or change the branch to predict true. */
4319 for (insn = get_insns (); insn; insn = next)
4320 {
4321 rtx pat;
4322 int delay_needed = 0;
4323
4324 next = find_next_insn_start (insn);
4325
4326 if (NOTE_P (insn) || BARRIER_P (insn))
4327 continue;
4328 if (JUMP_TABLE_DATA_P (insn))
4329 continue;
4330
4331 if (LABEL_P (insn))
4332 {
4333 np_check_regno = -1;
4334 continue;
4335 }
4336
4337 pat = PATTERN (insn);
4338 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
4339 continue;
4340
4341 if (GET_CODE (pat) == ASM_INPUT || asm_noperands (pat) >= 0)
4342 {
4343 np_check_regno = -1;
4344 continue;
4345 }
4346
4347 if (JUMP_P (insn))
4348 {
4349 /* Is this a condjump based on a null pointer comparison we saw
4350 earlier? */
4351 if (np_check_regno != -1
4352 && recog_memoized (insn) == CODE_FOR_cbranchbi4)
4353 {
4354 rtx op = XEXP (SET_SRC (PATTERN (insn)), 0);
4355 gcc_assert (GET_CODE (op) == EQ || GET_CODE (op) == NE);
4356 if (GET_CODE (op) == NE)
4357 np_after_branch = true;
4358 }
4359 if (any_condjump_p (insn)
4360 && ! cbranch_predicted_taken_p (insn))
4361 {
4362 last_condjump = insn;
4363 delay_added = 0;
4364 cycles_since_jump = 0;
4365 }
4366 else
4367 cycles_since_jump = INT_MAX;
4368 }
4369 else if (CALL_P (insn))
4370 {
4371 np_check_regno = -1;
4372 if (cycles_since_jump < INT_MAX)
4373 cycles_since_jump++;
4374 if (indirect_call_p (pat) && ENABLE_WA_INDIRECT_CALLS)
4375 {
4376 delay_needed = 3;
4377 }
4378 }
4379 else if (NONDEBUG_INSN_P (insn))
4380 {
4381 rtx_insn *load_insn = find_load (insn);
4382 enum attr_type type = type_for_anomaly (insn);
4383
4384 if (cycles_since_jump < INT_MAX)
4385 cycles_since_jump++;
4386
4387 /* Detect a comparison of a P register with zero. If we later
4388 see a condjump based on it, we have found a null pointer
4389 check. */
4390 if (recog_memoized (insn) == CODE_FOR_compare_eq)
4391 {
4392 rtx src = SET_SRC (PATTERN (insn));
4393 if (REG_P (XEXP (src, 0))
4394 && P_REGNO_P (REGNO (XEXP (src, 0)))
4395 && XEXP (src, 1) == const0_rtx)
4396 {
4397 np_check_regno = REGNO (XEXP (src, 0));
4398 np_after_branch = false;
4399 }
4400 else
4401 np_check_regno = -1;
4402 }
4403
4404 if (load_insn && ENABLE_WA_SPECULATIVE_LOADS)
4405 {
4406 if (trapping_loads_p (load_insn, np_check_regno,
4407 np_after_branch))
4408 delay_needed = 4;
4409 }
4410 else if (type == TYPE_SYNC && ENABLE_WA_SPECULATIVE_SYNCS)
4411 delay_needed = 3;
4412
4413 /* See if we need to forget about a null pointer comparison
4414 we found earlier. */
4415 if (recog_memoized (insn) != CODE_FOR_compare_eq)
4416 {
4417 note_stores (PATTERN (insn), note_np_check_stores, NULL);
4418 if (np_check_regno != -1)
4419 {
4420 if (find_regno_note (insn, REG_INC, np_check_regno))
4421 np_check_regno = -1;
4422 }
4423 }
4424
4425 }
4426
4427 if (delay_needed > cycles_since_jump
4428 && (delay_needed - cycles_since_jump) > delay_added)
4429 {
4430 rtx pat1;
4431 int num_clobbers;
4432 rtx *op = recog_data.operand;
4433
4434 delay_needed -= cycles_since_jump;
4435
4436 extract_insn (last_condjump);
4437 if (optimize_size)
4438 {
4439 pat1 = gen_cbranch_predicted_taken (op[0], op[1], op[2],
4440 op[3]);
4441 cycles_since_jump = INT_MAX;
4442 }
4443 else
4444 {
4445 /* Do not adjust cycles_since_jump in this case, so that
4446 we'll increase the number of NOPs for a subsequent insn
4447 if necessary. */
4448 pat1 = gen_cbranch_with_nops (op[0], op[1], op[2], op[3],
4449 GEN_INT (delay_needed));
4450 delay_added = delay_needed;
4451 }
4452 PATTERN (last_condjump) = pat1;
4453 INSN_CODE (last_condjump) = recog (pat1, insn, &num_clobbers);
4454 }
4455 if (CALL_P (insn))
4456 {
4457 cycles_since_jump = INT_MAX;
4458 delay_added = 0;
4459 }
4460 }
4461
4462 /* Second pass: for predicted-true branches, see if anything at the
4463 branch destination needs extra nops. */
4464 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4465 {
4466 int cycles_since_jump;
4467 if (JUMP_P (insn)
4468 && any_condjump_p (insn)
4469 && (INSN_CODE (insn) == CODE_FOR_cbranch_predicted_taken
4470 || cbranch_predicted_taken_p (insn)))
4471 {
4472 rtx_insn *target = JUMP_LABEL_AS_INSN (insn);
4473 rtx label = target;
4474 rtx_insn *next_tgt;
4475
4476 cycles_since_jump = 0;
4477 for (; target && cycles_since_jump < 3; target = next_tgt)
4478 {
4479 rtx pat;
4480
4481 next_tgt = find_next_insn_start (target);
4482
4483 if (NOTE_P (target) || BARRIER_P (target) || LABEL_P (target))
4484 continue;
4485
4486 if (JUMP_TABLE_DATA_P (target))
4487 continue;
4488
4489 pat = PATTERN (target);
4490 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
4491 || GET_CODE (pat) == ASM_INPUT
4492 || asm_noperands (pat) >= 0)
4493 continue;
4494
4495 if (NONDEBUG_INSN_P (target))
4496 {
4497 rtx_insn *load_insn = find_load (target);
4498 enum attr_type type = type_for_anomaly (target);
4499 int delay_needed = 0;
4500 if (cycles_since_jump < INT_MAX)
4501 cycles_since_jump++;
4502
4503 if (load_insn && ENABLE_WA_SPECULATIVE_LOADS)
4504 {
4505 if (trapping_loads_p (load_insn, -1, false))
4506 delay_needed = 2;
4507 }
4508 else if (type == TYPE_SYNC && ENABLE_WA_SPECULATIVE_SYNCS)
4509 delay_needed = 2;
4510
4511 if (delay_needed > cycles_since_jump)
4512 {
4513 rtx_insn *prev = prev_real_insn (label);
4514 delay_needed -= cycles_since_jump;
4515 if (dump_file)
4516 fprintf (dump_file, "Adding %d nops after %d\n",
4517 delay_needed, INSN_UID (label));
4518 if (JUMP_P (prev)
4519 && INSN_CODE (prev) == CODE_FOR_cbranch_with_nops)
4520 {
4521 rtx x;
4522 HOST_WIDE_INT v;
4523
4524 if (dump_file)
4525 fprintf (dump_file,
4526 "Reducing nops on insn %d.\n",
4527 INSN_UID (prev));
4528 x = PATTERN (prev);
4529 x = XVECEXP (x, 0, 1);
4530 v = INTVAL (XVECEXP (x, 0, 0)) - delay_needed;
4531 XVECEXP (x, 0, 0) = GEN_INT (v);
4532 }
4533 while (delay_needed-- > 0)
4534 emit_insn_after (gen_nop (), label);
4535 break;
4536 }
4537 }
4538 }
4539 }
4540 }
4541 }
4542
4543 /* Called just before the final scheduling pass. If we need to insert NOPs
4544 later on to work around speculative loads, insert special placeholder
4545 insns that cause loads to be delayed for as many cycles as necessary
4546 (and possible). This reduces the number of NOPs we need to add.
4547 The dummy insns we generate are later removed by bfin_gen_bundles. */
4548 static void
4549 add_sched_insns_for_speculation (void)
4550 {
4551 rtx_insn *insn;
4552
4553 if (! ENABLE_WA_SPECULATIVE_LOADS && ! ENABLE_WA_SPECULATIVE_SYNCS
4554 && ! ENABLE_WA_INDIRECT_CALLS)
4555 return;
4556
4557 /* First pass: find predicted-false branches; if something after them
4558 needs nops, insert them or change the branch to predict true. */
4559 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4560 {
4561 rtx pat;
4562
4563 if (NOTE_P (insn) || BARRIER_P (insn) || LABEL_P (insn))
4564 continue;
4565 if (JUMP_TABLE_DATA_P (insn))
4566 continue;
4567
4568 pat = PATTERN (insn);
4569 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
4570 || GET_CODE (pat) == ASM_INPUT
4571 || asm_noperands (pat) >= 0)
4572 continue;
4573
4574 if (JUMP_P (insn))
4575 {
4576 if (any_condjump_p (insn)
4577 && !cbranch_predicted_taken_p (insn))
4578 {
4579 rtx_insn *n = next_real_insn (insn);
4580 emit_insn_before (gen_stall (GEN_INT (3)), n);
4581 }
4582 }
4583 }
4584
4585 /* Second pass: for predicted-true branches, see if anything at the
4586 branch destination needs extra nops. */
4587 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4588 {
4589 if (JUMP_P (insn)
4590 && any_condjump_p (insn)
4591 && (cbranch_predicted_taken_p (insn)))
4592 {
4593 rtx target = JUMP_LABEL (insn);
4594 rtx_insn *next = next_real_insn (target);
4595
4596 if (GET_CODE (PATTERN (next)) == UNSPEC_VOLATILE
4597 && get_attr_type (next) == TYPE_STALL)
4598 continue;
4599 emit_insn_before (gen_stall (GEN_INT (1)), next);
4600 }
4601 }
4602 }
4603
4604 /* We use the machine specific reorg pass for emitting CSYNC instructions
4605 after conditional branches as needed.
4606
4607 The Blackfin is unusual in that a code sequence like
4608 if cc jump label
4609 r0 = (p0)
4610 may speculatively perform the load even if the condition isn't true. This
4611 happens for a branch that is predicted not taken, because the pipeline
4612 isn't flushed or stalled, so the early stages of the following instructions,
4613 which perform the memory reference, are allowed to execute before the
4614 jump condition is evaluated.
4615 Therefore, we must insert additional instructions in all places where this
4616 could lead to incorrect behavior. The manual recommends CSYNC, while
4617 VDSP seems to use NOPs (even though its corresponding compiler option is
4618 named CSYNC).
4619
4620 When optimizing for speed, we emit NOPs, which seems faster than a CSYNC.
4621 When optimizing for size, we turn the branch into a predicted taken one.
4622 This may be slower due to mispredicts, but saves code size. */
4623
4624 static void
4625 bfin_reorg (void)
4626 {
4627 /* We are freeing block_for_insn in the toplev to keep compatibility
4628 with old MDEP_REORGS that are not CFG based. Recompute it now. */
4629 compute_bb_for_insn ();
4630
4631 if (flag_schedule_insns_after_reload)
4632 {
4633 splitting_for_sched = 1;
4634 split_all_insns ();
4635 splitting_for_sched = 0;
4636
4637 add_sched_insns_for_speculation ();
4638
4639 timevar_push (TV_SCHED2);
4640 if (flag_selective_scheduling2
4641 && !maybe_skip_selective_scheduling ())
4642 run_selective_scheduling ();
4643 else
4644 schedule_insns ();
4645 timevar_pop (TV_SCHED2);
4646
4647 /* Examine the schedule and insert nops as necessary for 64-bit parallel
4648 instructions. */
4649 bfin_gen_bundles ();
4650 }
4651
4652 df_analyze ();
4653
4654 /* Doloop optimization */
4655 if (cfun->machine->has_hardware_loops)
4656 bfin_reorg_loops ();
4657
4658 workaround_speculation ();
4659
4660 if (flag_var_tracking)
4661 {
4662 timevar_push (TV_VAR_TRACKING);
4663 variable_tracking_main ();
4664 reorder_var_tracking_notes ();
4665 timevar_pop (TV_VAR_TRACKING);
4666 }
4667
4668 df_finish_pass (false);
4669
4670 workaround_rts_anomaly ();
4671 }
4672 \f
4673 /* Handle interrupt_handler, exception_handler and nmi_handler function
4674 attributes; arguments as in struct attribute_spec.handler. */
4675
4676 static tree
4677 handle_int_attribute (tree *node, tree name,
4678 tree args ATTRIBUTE_UNUSED,
4679 int flags ATTRIBUTE_UNUSED,
4680 bool *no_add_attrs)
4681 {
4682 tree x = *node;
4683 if (TREE_CODE (x) == FUNCTION_DECL)
4684 x = TREE_TYPE (x);
4685
4686 if (TREE_CODE (x) != FUNCTION_TYPE)
4687 {
4688 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4689 name);
4690 *no_add_attrs = true;
4691 }
4692 else if (funkind (x) != SUBROUTINE)
4693 error ("multiple function type attributes specified");
4694
4695 return NULL_TREE;
4696 }
4697
4698 /* Return 0 if the attributes for two types are incompatible, 1 if they
4699 are compatible, and 2 if they are nearly compatible (which causes a
4700 warning to be generated). */
4701
4702 static int
4703 bfin_comp_type_attributes (const_tree type1, const_tree type2)
4704 {
4705 e_funkind kind1, kind2;
4706
4707 if (TREE_CODE (type1) != FUNCTION_TYPE)
4708 return 1;
4709
4710 kind1 = funkind (type1);
4711 kind2 = funkind (type2);
4712
4713 if (kind1 != kind2)
4714 return 0;
4715
4716 /* Check for mismatched modifiers */
4717 if (!lookup_attribute ("nesting", TYPE_ATTRIBUTES (type1))
4718 != !lookup_attribute ("nesting", TYPE_ATTRIBUTES (type2)))
4719 return 0;
4720
4721 if (!lookup_attribute ("saveall", TYPE_ATTRIBUTES (type1))
4722 != !lookup_attribute ("saveall", TYPE_ATTRIBUTES (type2)))
4723 return 0;
4724
4725 if (!lookup_attribute ("kspisusp", TYPE_ATTRIBUTES (type1))
4726 != !lookup_attribute ("kspisusp", TYPE_ATTRIBUTES (type2)))
4727 return 0;
4728
4729 if (!lookup_attribute ("longcall", TYPE_ATTRIBUTES (type1))
4730 != !lookup_attribute ("longcall", TYPE_ATTRIBUTES (type2)))
4731 return 0;
4732
4733 return 1;
4734 }
4735
4736 /* Handle a "longcall" or "shortcall" attribute; arguments as in
4737 struct attribute_spec.handler. */
4738
4739 static tree
4740 bfin_handle_longcall_attribute (tree *node, tree name,
4741 tree args ATTRIBUTE_UNUSED,
4742 int flags ATTRIBUTE_UNUSED,
4743 bool *no_add_attrs)
4744 {
4745 if (TREE_CODE (*node) != FUNCTION_TYPE
4746 && TREE_CODE (*node) != FIELD_DECL
4747 && TREE_CODE (*node) != TYPE_DECL)
4748 {
4749 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4750 name);
4751 *no_add_attrs = true;
4752 }
4753
4754 if ((strcmp (IDENTIFIER_POINTER (name), "longcall") == 0
4755 && lookup_attribute ("shortcall", TYPE_ATTRIBUTES (*node)))
4756 || (strcmp (IDENTIFIER_POINTER (name), "shortcall") == 0
4757 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (*node))))
4758 {
4759 warning (OPT_Wattributes,
4760 "can%'t apply both longcall and shortcall attributes to the same function");
4761 *no_add_attrs = true;
4762 }
4763
4764 return NULL_TREE;
4765 }
4766
4767 /* Handle a "l1_text" attribute; arguments as in
4768 struct attribute_spec.handler. */
4769
4770 static tree
4771 bfin_handle_l1_text_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4772 int ARG_UNUSED (flags), bool *no_add_attrs)
4773 {
4774 tree decl = *node;
4775
4776 if (TREE_CODE (decl) != FUNCTION_DECL)
4777 {
4778 error ("%qE attribute only applies to functions",
4779 name);
4780 *no_add_attrs = true;
4781 }
4782
4783 /* The decl may have already been given a section attribute
4784 from a previous declaration. Ensure they match. */
4785 else if (DECL_SECTION_NAME (decl) != NULL
4786 && strcmp (DECL_SECTION_NAME (decl),
4787 ".l1.text") != 0)
4788 {
4789 error ("section of %q+D conflicts with previous declaration",
4790 decl);
4791 *no_add_attrs = true;
4792 }
4793 else
4794 set_decl_section_name (decl, ".l1.text");
4795
4796 return NULL_TREE;
4797 }
4798
4799 /* Handle a "l1_data", "l1_data_A" or "l1_data_B" attribute;
4800 arguments as in struct attribute_spec.handler. */
4801
4802 static tree
4803 bfin_handle_l1_data_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4804 int ARG_UNUSED (flags), bool *no_add_attrs)
4805 {
4806 tree decl = *node;
4807
4808 if (TREE_CODE (decl) != VAR_DECL)
4809 {
4810 error ("%qE attribute only applies to variables",
4811 name);
4812 *no_add_attrs = true;
4813 }
4814 else if (current_function_decl != NULL_TREE
4815 && !TREE_STATIC (decl))
4816 {
4817 error ("%qE attribute cannot be specified for local variables",
4818 name);
4819 *no_add_attrs = true;
4820 }
4821 else
4822 {
4823 const char *section_name;
4824
4825 if (strcmp (IDENTIFIER_POINTER (name), "l1_data") == 0)
4826 section_name = ".l1.data";
4827 else if (strcmp (IDENTIFIER_POINTER (name), "l1_data_A") == 0)
4828 section_name = ".l1.data.A";
4829 else if (strcmp (IDENTIFIER_POINTER (name), "l1_data_B") == 0)
4830 section_name = ".l1.data.B";
4831 else
4832 gcc_unreachable ();
4833
4834 /* The decl may have already been given a section attribute
4835 from a previous declaration. Ensure they match. */
4836 if (DECL_SECTION_NAME (decl) != NULL
4837 && strcmp (DECL_SECTION_NAME (decl),
4838 section_name) != 0)
4839 {
4840 error ("section of %q+D conflicts with previous declaration",
4841 decl);
4842 *no_add_attrs = true;
4843 }
4844 else
4845 set_decl_section_name (decl, section_name);
4846 }
4847
4848 return NULL_TREE;
4849 }
4850
4851 /* Handle a "l2" attribute; arguments as in struct attribute_spec.handler. */
4852
4853 static tree
4854 bfin_handle_l2_attribute (tree *node, tree ARG_UNUSED (name),
4855 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4856 bool *no_add_attrs)
4857 {
4858 tree decl = *node;
4859
4860 if (TREE_CODE (decl) == FUNCTION_DECL)
4861 {
4862 if (DECL_SECTION_NAME (decl) != NULL
4863 && strcmp (DECL_SECTION_NAME (decl),
4864 ".l2.text") != 0)
4865 {
4866 error ("section of %q+D conflicts with previous declaration",
4867 decl);
4868 *no_add_attrs = true;
4869 }
4870 else
4871 set_decl_section_name (decl, ".l2.text");
4872 }
4873 else if (TREE_CODE (decl) == VAR_DECL)
4874 {
4875 if (DECL_SECTION_NAME (decl) != NULL
4876 && strcmp (DECL_SECTION_NAME (decl),
4877 ".l2.data") != 0)
4878 {
4879 error ("section of %q+D conflicts with previous declaration",
4880 decl);
4881 *no_add_attrs = true;
4882 }
4883 else
4884 set_decl_section_name (decl, ".l2.data");
4885 }
4886
4887 return NULL_TREE;
4888 }
4889
4890 /* Table of valid machine attributes. */
4891 static const struct attribute_spec bfin_attribute_table[] =
4892 {
4893 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
4894 affects_type_identity } */
4895 { "interrupt_handler", 0, 0, false, true, true, handle_int_attribute,
4896 false },
4897 { "exception_handler", 0, 0, false, true, true, handle_int_attribute,
4898 false },
4899 { "nmi_handler", 0, 0, false, true, true, handle_int_attribute, false },
4900 { "nesting", 0, 0, false, true, true, NULL, false },
4901 { "kspisusp", 0, 0, false, true, true, NULL, false },
4902 { "saveall", 0, 0, false, true, true, NULL, false },
4903 { "longcall", 0, 0, false, true, true, bfin_handle_longcall_attribute,
4904 false },
4905 { "shortcall", 0, 0, false, true, true, bfin_handle_longcall_attribute,
4906 false },
4907 { "l1_text", 0, 0, true, false, false, bfin_handle_l1_text_attribute,
4908 false },
4909 { "l1_data", 0, 0, true, false, false, bfin_handle_l1_data_attribute,
4910 false },
4911 { "l1_data_A", 0, 0, true, false, false, bfin_handle_l1_data_attribute,
4912 false },
4913 { "l1_data_B", 0, 0, true, false, false, bfin_handle_l1_data_attribute,
4914 false },
4915 { "l2", 0, 0, true, false, false, bfin_handle_l2_attribute, false },
4916 { NULL, 0, 0, false, false, false, NULL, false }
4917 };
4918 \f
4919 /* Implementation of TARGET_ASM_INTEGER. When using FD-PIC, we need to
4920 tell the assembler to generate pointers to function descriptors in
4921 some cases. */
4922
4923 static bool
4924 bfin_assemble_integer (rtx value, unsigned int size, int aligned_p)
4925 {
4926 if (TARGET_FDPIC && size == UNITS_PER_WORD)
4927 {
4928 if (GET_CODE (value) == SYMBOL_REF
4929 && SYMBOL_REF_FUNCTION_P (value))
4930 {
4931 fputs ("\t.picptr\tfuncdesc(", asm_out_file);
4932 output_addr_const (asm_out_file, value);
4933 fputs (")\n", asm_out_file);
4934 return true;
4935 }
4936 if (!aligned_p)
4937 {
4938 /* We've set the unaligned SI op to NULL, so we always have to
4939 handle the unaligned case here. */
4940 assemble_integer_with_op ("\t.4byte\t", value);
4941 return true;
4942 }
4943 }
4944 return default_assemble_integer (value, size, aligned_p);
4945 }
4946 \f
4947 /* Output the assembler code for a thunk function. THUNK_DECL is the
4948 declaration for the thunk function itself, FUNCTION is the decl for
4949 the target function. DELTA is an immediate constant offset to be
4950 added to THIS. If VCALL_OFFSET is nonzero, the word at
4951 *(*this + vcall_offset) should be added to THIS. */
4952
4953 static void
4954 bfin_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
4955 tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
4956 HOST_WIDE_INT vcall_offset, tree function)
4957 {
4958 rtx xops[3];
4959 /* The this parameter is passed as the first argument. */
4960 rtx this_rtx = gen_rtx_REG (Pmode, REG_R0);
4961
4962 /* Adjust the this parameter by a fixed constant. */
4963 if (delta)
4964 {
4965 xops[1] = this_rtx;
4966 if (delta >= -64 && delta <= 63)
4967 {
4968 xops[0] = GEN_INT (delta);
4969 output_asm_insn ("%1 += %0;", xops);
4970 }
4971 else if (delta >= -128 && delta < -64)
4972 {
4973 xops[0] = GEN_INT (delta + 64);
4974 output_asm_insn ("%1 += -64; %1 += %0;", xops);
4975 }
4976 else if (delta > 63 && delta <= 126)
4977 {
4978 xops[0] = GEN_INT (delta - 63);
4979 output_asm_insn ("%1 += 63; %1 += %0;", xops);
4980 }
4981 else
4982 {
4983 xops[0] = GEN_INT (delta);
4984 output_asm_insn ("r3.l = %h0; r3.h = %d0; %1 = %1 + r3;", xops);
4985 }
4986 }
4987
4988 /* Adjust the this parameter by a value stored in the vtable. */
4989 if (vcall_offset)
4990 {
4991 rtx p2tmp = gen_rtx_REG (Pmode, REG_P2);
4992 rtx tmp = gen_rtx_REG (Pmode, REG_R3);
4993
4994 xops[1] = tmp;
4995 xops[2] = p2tmp;
4996 output_asm_insn ("%2 = r0; %2 = [%2];", xops);
4997
4998 /* Adjust the this parameter. */
4999 xops[0] = gen_rtx_MEM (Pmode, plus_constant (Pmode, p2tmp,
5000 vcall_offset));
5001 if (!memory_operand (xops[0], Pmode))
5002 {
5003 rtx tmp2 = gen_rtx_REG (Pmode, REG_P1);
5004 xops[0] = GEN_INT (vcall_offset);
5005 xops[1] = tmp2;
5006 output_asm_insn ("%h1 = %h0; %d1 = %d0; %2 = %2 + %1", xops);
5007 xops[0] = gen_rtx_MEM (Pmode, p2tmp);
5008 }
5009 xops[2] = this_rtx;
5010 output_asm_insn ("%1 = %0; %2 = %2 + %1;", xops);
5011 }
5012
5013 xops[0] = XEXP (DECL_RTL (function), 0);
5014 if (1 || !flag_pic || (*targetm.binds_local_p) (function))
5015 output_asm_insn ("jump.l\t%P0", xops);
5016 }
5017 \f
5018 /* Codes for all the Blackfin builtins. */
5019 enum bfin_builtins
5020 {
5021 BFIN_BUILTIN_CSYNC,
5022 BFIN_BUILTIN_SSYNC,
5023 BFIN_BUILTIN_ONES,
5024 BFIN_BUILTIN_COMPOSE_2X16,
5025 BFIN_BUILTIN_EXTRACTLO,
5026 BFIN_BUILTIN_EXTRACTHI,
5027
5028 BFIN_BUILTIN_SSADD_2X16,
5029 BFIN_BUILTIN_SSSUB_2X16,
5030 BFIN_BUILTIN_SSADDSUB_2X16,
5031 BFIN_BUILTIN_SSSUBADD_2X16,
5032 BFIN_BUILTIN_MULT_2X16,
5033 BFIN_BUILTIN_MULTR_2X16,
5034 BFIN_BUILTIN_NEG_2X16,
5035 BFIN_BUILTIN_ABS_2X16,
5036 BFIN_BUILTIN_MIN_2X16,
5037 BFIN_BUILTIN_MAX_2X16,
5038
5039 BFIN_BUILTIN_SSADD_1X16,
5040 BFIN_BUILTIN_SSSUB_1X16,
5041 BFIN_BUILTIN_MULT_1X16,
5042 BFIN_BUILTIN_MULTR_1X16,
5043 BFIN_BUILTIN_NORM_1X16,
5044 BFIN_BUILTIN_NEG_1X16,
5045 BFIN_BUILTIN_ABS_1X16,
5046 BFIN_BUILTIN_MIN_1X16,
5047 BFIN_BUILTIN_MAX_1X16,
5048
5049 BFIN_BUILTIN_SUM_2X16,
5050 BFIN_BUILTIN_DIFFHL_2X16,
5051 BFIN_BUILTIN_DIFFLH_2X16,
5052
5053 BFIN_BUILTIN_SSADD_1X32,
5054 BFIN_BUILTIN_SSSUB_1X32,
5055 BFIN_BUILTIN_NORM_1X32,
5056 BFIN_BUILTIN_ROUND_1X32,
5057 BFIN_BUILTIN_NEG_1X32,
5058 BFIN_BUILTIN_ABS_1X32,
5059 BFIN_BUILTIN_MIN_1X32,
5060 BFIN_BUILTIN_MAX_1X32,
5061 BFIN_BUILTIN_MULT_1X32,
5062 BFIN_BUILTIN_MULT_1X32X32,
5063 BFIN_BUILTIN_MULT_1X32X32NS,
5064
5065 BFIN_BUILTIN_MULHISILL,
5066 BFIN_BUILTIN_MULHISILH,
5067 BFIN_BUILTIN_MULHISIHL,
5068 BFIN_BUILTIN_MULHISIHH,
5069
5070 BFIN_BUILTIN_LSHIFT_1X16,
5071 BFIN_BUILTIN_LSHIFT_2X16,
5072 BFIN_BUILTIN_SSASHIFT_1X16,
5073 BFIN_BUILTIN_SSASHIFT_2X16,
5074 BFIN_BUILTIN_SSASHIFT_1X32,
5075
5076 BFIN_BUILTIN_CPLX_MUL_16,
5077 BFIN_BUILTIN_CPLX_MAC_16,
5078 BFIN_BUILTIN_CPLX_MSU_16,
5079
5080 BFIN_BUILTIN_CPLX_MUL_16_S40,
5081 BFIN_BUILTIN_CPLX_MAC_16_S40,
5082 BFIN_BUILTIN_CPLX_MSU_16_S40,
5083
5084 BFIN_BUILTIN_CPLX_SQU,
5085
5086 BFIN_BUILTIN_LOADBYTES,
5087
5088 BFIN_BUILTIN_MAX
5089 };
5090
5091 #define def_builtin(NAME, TYPE, CODE) \
5092 do { \
5093 add_builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, \
5094 NULL, NULL_TREE); \
5095 } while (0)
5096
5097 /* Set up all builtin functions for this target. */
5098 static void
5099 bfin_init_builtins (void)
5100 {
5101 tree V2HI_type_node = build_vector_type_for_mode (intHI_type_node, V2HImode);
5102 tree void_ftype_void
5103 = build_function_type_list (void_type_node, NULL_TREE);
5104 tree short_ftype_short
5105 = build_function_type_list (short_integer_type_node, short_integer_type_node,
5106 NULL_TREE);
5107 tree short_ftype_int_int
5108 = build_function_type_list (short_integer_type_node, integer_type_node,
5109 integer_type_node, NULL_TREE);
5110 tree int_ftype_int_int
5111 = build_function_type_list (integer_type_node, integer_type_node,
5112 integer_type_node, NULL_TREE);
5113 tree int_ftype_int
5114 = build_function_type_list (integer_type_node, integer_type_node,
5115 NULL_TREE);
5116 tree short_ftype_int
5117 = build_function_type_list (short_integer_type_node, integer_type_node,
5118 NULL_TREE);
5119 tree int_ftype_v2hi_v2hi
5120 = build_function_type_list (integer_type_node, V2HI_type_node,
5121 V2HI_type_node, NULL_TREE);
5122 tree v2hi_ftype_v2hi_v2hi
5123 = build_function_type_list (V2HI_type_node, V2HI_type_node,
5124 V2HI_type_node, NULL_TREE);
5125 tree v2hi_ftype_v2hi_v2hi_v2hi
5126 = build_function_type_list (V2HI_type_node, V2HI_type_node,
5127 V2HI_type_node, V2HI_type_node, NULL_TREE);
5128 tree v2hi_ftype_int_int
5129 = build_function_type_list (V2HI_type_node, integer_type_node,
5130 integer_type_node, NULL_TREE);
5131 tree v2hi_ftype_v2hi_int
5132 = build_function_type_list (V2HI_type_node, V2HI_type_node,
5133 integer_type_node, NULL_TREE);
5134 tree int_ftype_short_short
5135 = build_function_type_list (integer_type_node, short_integer_type_node,
5136 short_integer_type_node, NULL_TREE);
5137 tree v2hi_ftype_v2hi
5138 = build_function_type_list (V2HI_type_node, V2HI_type_node, NULL_TREE);
5139 tree short_ftype_v2hi
5140 = build_function_type_list (short_integer_type_node, V2HI_type_node,
5141 NULL_TREE);
5142 tree int_ftype_pint
5143 = build_function_type_list (integer_type_node,
5144 build_pointer_type (integer_type_node),
5145 NULL_TREE);
5146
5147 /* Add the remaining MMX insns with somewhat more complicated types. */
5148 def_builtin ("__builtin_bfin_csync", void_ftype_void, BFIN_BUILTIN_CSYNC);
5149 def_builtin ("__builtin_bfin_ssync", void_ftype_void, BFIN_BUILTIN_SSYNC);
5150
5151 def_builtin ("__builtin_bfin_ones", short_ftype_int, BFIN_BUILTIN_ONES);
5152
5153 def_builtin ("__builtin_bfin_compose_2x16", v2hi_ftype_int_int,
5154 BFIN_BUILTIN_COMPOSE_2X16);
5155 def_builtin ("__builtin_bfin_extract_hi", short_ftype_v2hi,
5156 BFIN_BUILTIN_EXTRACTHI);
5157 def_builtin ("__builtin_bfin_extract_lo", short_ftype_v2hi,
5158 BFIN_BUILTIN_EXTRACTLO);
5159
5160 def_builtin ("__builtin_bfin_min_fr2x16", v2hi_ftype_v2hi_v2hi,
5161 BFIN_BUILTIN_MIN_2X16);
5162 def_builtin ("__builtin_bfin_max_fr2x16", v2hi_ftype_v2hi_v2hi,
5163 BFIN_BUILTIN_MAX_2X16);
5164
5165 def_builtin ("__builtin_bfin_add_fr2x16", v2hi_ftype_v2hi_v2hi,
5166 BFIN_BUILTIN_SSADD_2X16);
5167 def_builtin ("__builtin_bfin_sub_fr2x16", v2hi_ftype_v2hi_v2hi,
5168 BFIN_BUILTIN_SSSUB_2X16);
5169 def_builtin ("__builtin_bfin_dspaddsubsat", v2hi_ftype_v2hi_v2hi,
5170 BFIN_BUILTIN_SSADDSUB_2X16);
5171 def_builtin ("__builtin_bfin_dspsubaddsat", v2hi_ftype_v2hi_v2hi,
5172 BFIN_BUILTIN_SSSUBADD_2X16);
5173 def_builtin ("__builtin_bfin_mult_fr2x16", v2hi_ftype_v2hi_v2hi,
5174 BFIN_BUILTIN_MULT_2X16);
5175 def_builtin ("__builtin_bfin_multr_fr2x16", v2hi_ftype_v2hi_v2hi,
5176 BFIN_BUILTIN_MULTR_2X16);
5177 def_builtin ("__builtin_bfin_negate_fr2x16", v2hi_ftype_v2hi,
5178 BFIN_BUILTIN_NEG_2X16);
5179 def_builtin ("__builtin_bfin_abs_fr2x16", v2hi_ftype_v2hi,
5180 BFIN_BUILTIN_ABS_2X16);
5181
5182 def_builtin ("__builtin_bfin_min_fr1x16", short_ftype_int_int,
5183 BFIN_BUILTIN_MIN_1X16);
5184 def_builtin ("__builtin_bfin_max_fr1x16", short_ftype_int_int,
5185 BFIN_BUILTIN_MAX_1X16);
5186
5187 def_builtin ("__builtin_bfin_add_fr1x16", short_ftype_int_int,
5188 BFIN_BUILTIN_SSADD_1X16);
5189 def_builtin ("__builtin_bfin_sub_fr1x16", short_ftype_int_int,
5190 BFIN_BUILTIN_SSSUB_1X16);
5191 def_builtin ("__builtin_bfin_mult_fr1x16", short_ftype_int_int,
5192 BFIN_BUILTIN_MULT_1X16);
5193 def_builtin ("__builtin_bfin_multr_fr1x16", short_ftype_int_int,
5194 BFIN_BUILTIN_MULTR_1X16);
5195 def_builtin ("__builtin_bfin_negate_fr1x16", short_ftype_short,
5196 BFIN_BUILTIN_NEG_1X16);
5197 def_builtin ("__builtin_bfin_abs_fr1x16", short_ftype_short,
5198 BFIN_BUILTIN_ABS_1X16);
5199 def_builtin ("__builtin_bfin_norm_fr1x16", short_ftype_int,
5200 BFIN_BUILTIN_NORM_1X16);
5201
5202 def_builtin ("__builtin_bfin_sum_fr2x16", short_ftype_v2hi,
5203 BFIN_BUILTIN_SUM_2X16);
5204 def_builtin ("__builtin_bfin_diff_hl_fr2x16", short_ftype_v2hi,
5205 BFIN_BUILTIN_DIFFHL_2X16);
5206 def_builtin ("__builtin_bfin_diff_lh_fr2x16", short_ftype_v2hi,
5207 BFIN_BUILTIN_DIFFLH_2X16);
5208
5209 def_builtin ("__builtin_bfin_mulhisill", int_ftype_v2hi_v2hi,
5210 BFIN_BUILTIN_MULHISILL);
5211 def_builtin ("__builtin_bfin_mulhisihl", int_ftype_v2hi_v2hi,
5212 BFIN_BUILTIN_MULHISIHL);
5213 def_builtin ("__builtin_bfin_mulhisilh", int_ftype_v2hi_v2hi,
5214 BFIN_BUILTIN_MULHISILH);
5215 def_builtin ("__builtin_bfin_mulhisihh", int_ftype_v2hi_v2hi,
5216 BFIN_BUILTIN_MULHISIHH);
5217
5218 def_builtin ("__builtin_bfin_min_fr1x32", int_ftype_int_int,
5219 BFIN_BUILTIN_MIN_1X32);
5220 def_builtin ("__builtin_bfin_max_fr1x32", int_ftype_int_int,
5221 BFIN_BUILTIN_MAX_1X32);
5222
5223 def_builtin ("__builtin_bfin_add_fr1x32", int_ftype_int_int,
5224 BFIN_BUILTIN_SSADD_1X32);
5225 def_builtin ("__builtin_bfin_sub_fr1x32", int_ftype_int_int,
5226 BFIN_BUILTIN_SSSUB_1X32);
5227 def_builtin ("__builtin_bfin_negate_fr1x32", int_ftype_int,
5228 BFIN_BUILTIN_NEG_1X32);
5229 def_builtin ("__builtin_bfin_abs_fr1x32", int_ftype_int,
5230 BFIN_BUILTIN_ABS_1X32);
5231 def_builtin ("__builtin_bfin_norm_fr1x32", short_ftype_int,
5232 BFIN_BUILTIN_NORM_1X32);
5233 def_builtin ("__builtin_bfin_round_fr1x32", short_ftype_int,
5234 BFIN_BUILTIN_ROUND_1X32);
5235 def_builtin ("__builtin_bfin_mult_fr1x32", int_ftype_short_short,
5236 BFIN_BUILTIN_MULT_1X32);
5237 def_builtin ("__builtin_bfin_mult_fr1x32x32", int_ftype_int_int,
5238 BFIN_BUILTIN_MULT_1X32X32);
5239 def_builtin ("__builtin_bfin_mult_fr1x32x32NS", int_ftype_int_int,
5240 BFIN_BUILTIN_MULT_1X32X32NS);
5241
5242 /* Shifts. */
5243 def_builtin ("__builtin_bfin_shl_fr1x16", short_ftype_int_int,
5244 BFIN_BUILTIN_SSASHIFT_1X16);
5245 def_builtin ("__builtin_bfin_shl_fr2x16", v2hi_ftype_v2hi_int,
5246 BFIN_BUILTIN_SSASHIFT_2X16);
5247 def_builtin ("__builtin_bfin_lshl_fr1x16", short_ftype_int_int,
5248 BFIN_BUILTIN_LSHIFT_1X16);
5249 def_builtin ("__builtin_bfin_lshl_fr2x16", v2hi_ftype_v2hi_int,
5250 BFIN_BUILTIN_LSHIFT_2X16);
5251 def_builtin ("__builtin_bfin_shl_fr1x32", int_ftype_int_int,
5252 BFIN_BUILTIN_SSASHIFT_1X32);
5253
5254 /* Complex numbers. */
5255 def_builtin ("__builtin_bfin_cmplx_add", v2hi_ftype_v2hi_v2hi,
5256 BFIN_BUILTIN_SSADD_2X16);
5257 def_builtin ("__builtin_bfin_cmplx_sub", v2hi_ftype_v2hi_v2hi,
5258 BFIN_BUILTIN_SSSUB_2X16);
5259 def_builtin ("__builtin_bfin_cmplx_mul", v2hi_ftype_v2hi_v2hi,
5260 BFIN_BUILTIN_CPLX_MUL_16);
5261 def_builtin ("__builtin_bfin_cmplx_mac", v2hi_ftype_v2hi_v2hi_v2hi,
5262 BFIN_BUILTIN_CPLX_MAC_16);
5263 def_builtin ("__builtin_bfin_cmplx_msu", v2hi_ftype_v2hi_v2hi_v2hi,
5264 BFIN_BUILTIN_CPLX_MSU_16);
5265 def_builtin ("__builtin_bfin_cmplx_mul_s40", v2hi_ftype_v2hi_v2hi,
5266 BFIN_BUILTIN_CPLX_MUL_16_S40);
5267 def_builtin ("__builtin_bfin_cmplx_mac_s40", v2hi_ftype_v2hi_v2hi_v2hi,
5268 BFIN_BUILTIN_CPLX_MAC_16_S40);
5269 def_builtin ("__builtin_bfin_cmplx_msu_s40", v2hi_ftype_v2hi_v2hi_v2hi,
5270 BFIN_BUILTIN_CPLX_MSU_16_S40);
5271 def_builtin ("__builtin_bfin_csqu_fr16", v2hi_ftype_v2hi,
5272 BFIN_BUILTIN_CPLX_SQU);
5273
5274 /* "Unaligned" load. */
5275 def_builtin ("__builtin_bfin_loadbytes", int_ftype_pint,
5276 BFIN_BUILTIN_LOADBYTES);
5277
5278 }
5279
5280
5281 struct builtin_description
5282 {
5283 const enum insn_code icode;
5284 const char *const name;
5285 const enum bfin_builtins code;
5286 int macflag;
5287 };
5288
5289 static const struct builtin_description bdesc_2arg[] =
5290 {
5291 { CODE_FOR_composev2hi, "__builtin_bfin_compose_2x16", BFIN_BUILTIN_COMPOSE_2X16, -1 },
5292
5293 { CODE_FOR_ssashiftv2hi3, "__builtin_bfin_shl_fr2x16", BFIN_BUILTIN_SSASHIFT_2X16, -1 },
5294 { CODE_FOR_ssashifthi3, "__builtin_bfin_shl_fr1x16", BFIN_BUILTIN_SSASHIFT_1X16, -1 },
5295 { CODE_FOR_lshiftv2hi3, "__builtin_bfin_lshl_fr2x16", BFIN_BUILTIN_LSHIFT_2X16, -1 },
5296 { CODE_FOR_lshifthi3, "__builtin_bfin_lshl_fr1x16", BFIN_BUILTIN_LSHIFT_1X16, -1 },
5297 { CODE_FOR_ssashiftsi3, "__builtin_bfin_shl_fr1x32", BFIN_BUILTIN_SSASHIFT_1X32, -1 },
5298
5299 { CODE_FOR_sminhi3, "__builtin_bfin_min_fr1x16", BFIN_BUILTIN_MIN_1X16, -1 },
5300 { CODE_FOR_smaxhi3, "__builtin_bfin_max_fr1x16", BFIN_BUILTIN_MAX_1X16, -1 },
5301 { CODE_FOR_ssaddhi3, "__builtin_bfin_add_fr1x16", BFIN_BUILTIN_SSADD_1X16, -1 },
5302 { CODE_FOR_sssubhi3, "__builtin_bfin_sub_fr1x16", BFIN_BUILTIN_SSSUB_1X16, -1 },
5303
5304 { CODE_FOR_sminsi3, "__builtin_bfin_min_fr1x32", BFIN_BUILTIN_MIN_1X32, -1 },
5305 { CODE_FOR_smaxsi3, "__builtin_bfin_max_fr1x32", BFIN_BUILTIN_MAX_1X32, -1 },
5306 { CODE_FOR_ssaddsi3, "__builtin_bfin_add_fr1x32", BFIN_BUILTIN_SSADD_1X32, -1 },
5307 { CODE_FOR_sssubsi3, "__builtin_bfin_sub_fr1x32", BFIN_BUILTIN_SSSUB_1X32, -1 },
5308
5309 { CODE_FOR_sminv2hi3, "__builtin_bfin_min_fr2x16", BFIN_BUILTIN_MIN_2X16, -1 },
5310 { CODE_FOR_smaxv2hi3, "__builtin_bfin_max_fr2x16", BFIN_BUILTIN_MAX_2X16, -1 },
5311 { CODE_FOR_ssaddv2hi3, "__builtin_bfin_add_fr2x16", BFIN_BUILTIN_SSADD_2X16, -1 },
5312 { CODE_FOR_sssubv2hi3, "__builtin_bfin_sub_fr2x16", BFIN_BUILTIN_SSSUB_2X16, -1 },
5313 { CODE_FOR_ssaddsubv2hi3, "__builtin_bfin_dspaddsubsat", BFIN_BUILTIN_SSADDSUB_2X16, -1 },
5314 { CODE_FOR_sssubaddv2hi3, "__builtin_bfin_dspsubaddsat", BFIN_BUILTIN_SSSUBADD_2X16, -1 },
5315
5316 { CODE_FOR_flag_mulhisi, "__builtin_bfin_mult_fr1x32", BFIN_BUILTIN_MULT_1X32, MACFLAG_NONE },
5317 { CODE_FOR_flag_mulhi, "__builtin_bfin_mult_fr1x16", BFIN_BUILTIN_MULT_1X16, MACFLAG_T },
5318 { CODE_FOR_flag_mulhi, "__builtin_bfin_multr_fr1x16", BFIN_BUILTIN_MULTR_1X16, MACFLAG_NONE },
5319 { CODE_FOR_flag_mulv2hi, "__builtin_bfin_mult_fr2x16", BFIN_BUILTIN_MULT_2X16, MACFLAG_T },
5320 { CODE_FOR_flag_mulv2hi, "__builtin_bfin_multr_fr2x16", BFIN_BUILTIN_MULTR_2X16, MACFLAG_NONE },
5321
5322 { CODE_FOR_mulhisi_ll, "__builtin_bfin_mulhisill", BFIN_BUILTIN_MULHISILL, -1 },
5323 { CODE_FOR_mulhisi_lh, "__builtin_bfin_mulhisilh", BFIN_BUILTIN_MULHISILH, -1 },
5324 { CODE_FOR_mulhisi_hl, "__builtin_bfin_mulhisihl", BFIN_BUILTIN_MULHISIHL, -1 },
5325 { CODE_FOR_mulhisi_hh, "__builtin_bfin_mulhisihh", BFIN_BUILTIN_MULHISIHH, -1 }
5326
5327 };
5328
5329 static const struct builtin_description bdesc_1arg[] =
5330 {
5331 { CODE_FOR_loadbytes, "__builtin_bfin_loadbytes", BFIN_BUILTIN_LOADBYTES, 0 },
5332
5333 { CODE_FOR_ones, "__builtin_bfin_ones", BFIN_BUILTIN_ONES, 0 },
5334
5335 { CODE_FOR_clrsbhi2, "__builtin_bfin_norm_fr1x16", BFIN_BUILTIN_NORM_1X16, 0 },
5336 { CODE_FOR_ssneghi2, "__builtin_bfin_negate_fr1x16", BFIN_BUILTIN_NEG_1X16, 0 },
5337 { CODE_FOR_abshi2, "__builtin_bfin_abs_fr1x16", BFIN_BUILTIN_ABS_1X16, 0 },
5338
5339 { CODE_FOR_clrsbsi2, "__builtin_bfin_norm_fr1x32", BFIN_BUILTIN_NORM_1X32, 0 },
5340 { CODE_FOR_ssroundsi2, "__builtin_bfin_round_fr1x32", BFIN_BUILTIN_ROUND_1X32, 0 },
5341 { CODE_FOR_ssnegsi2, "__builtin_bfin_negate_fr1x32", BFIN_BUILTIN_NEG_1X32, 0 },
5342 { CODE_FOR_ssabssi2, "__builtin_bfin_abs_fr1x32", BFIN_BUILTIN_ABS_1X32, 0 },
5343
5344 { CODE_FOR_movv2hi_hi_low, "__builtin_bfin_extract_lo", BFIN_BUILTIN_EXTRACTLO, 0 },
5345 { CODE_FOR_movv2hi_hi_high, "__builtin_bfin_extract_hi", BFIN_BUILTIN_EXTRACTHI, 0 },
5346 { CODE_FOR_ssnegv2hi2, "__builtin_bfin_negate_fr2x16", BFIN_BUILTIN_NEG_2X16, 0 },
5347 { CODE_FOR_ssabsv2hi2, "__builtin_bfin_abs_fr2x16", BFIN_BUILTIN_ABS_2X16, 0 }
5348 };
5349
5350 /* Errors in the source file can cause expand_expr to return const0_rtx
5351 where we expect a vector. To avoid crashing, use one of the vector
5352 clear instructions. */
5353 static rtx
5354 safe_vector_operand (rtx x, machine_mode mode)
5355 {
5356 if (x != const0_rtx)
5357 return x;
5358 x = gen_reg_rtx (SImode);
5359
5360 emit_insn (gen_movsi (x, CONST0_RTX (SImode)));
5361 return gen_lowpart (mode, x);
5362 }
5363
5364 /* Subroutine of bfin_expand_builtin to take care of binop insns. MACFLAG is -1
5365 if this is a normal binary op, or one of the MACFLAG_xxx constants. */
5366
5367 static rtx
5368 bfin_expand_binop_builtin (enum insn_code icode, tree exp, rtx target,
5369 int macflag)
5370 {
5371 rtx pat;
5372 tree arg0 = CALL_EXPR_ARG (exp, 0);
5373 tree arg1 = CALL_EXPR_ARG (exp, 1);
5374 rtx op0 = expand_normal (arg0);
5375 rtx op1 = expand_normal (arg1);
5376 machine_mode op0mode = GET_MODE (op0);
5377 machine_mode op1mode = GET_MODE (op1);
5378 machine_mode tmode = insn_data[icode].operand[0].mode;
5379 machine_mode mode0 = insn_data[icode].operand[1].mode;
5380 machine_mode mode1 = insn_data[icode].operand[2].mode;
5381
5382 if (VECTOR_MODE_P (mode0))
5383 op0 = safe_vector_operand (op0, mode0);
5384 if (VECTOR_MODE_P (mode1))
5385 op1 = safe_vector_operand (op1, mode1);
5386
5387 if (! target
5388 || GET_MODE (target) != tmode
5389 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5390 target = gen_reg_rtx (tmode);
5391
5392 if ((op0mode == SImode || op0mode == VOIDmode) && mode0 == HImode)
5393 {
5394 op0mode = HImode;
5395 op0 = gen_lowpart (HImode, op0);
5396 }
5397 if ((op1mode == SImode || op1mode == VOIDmode) && mode1 == HImode)
5398 {
5399 op1mode = HImode;
5400 op1 = gen_lowpart (HImode, op1);
5401 }
5402 /* In case the insn wants input operands in modes different from
5403 the result, abort. */
5404 gcc_assert ((op0mode == mode0 || op0mode == VOIDmode)
5405 && (op1mode == mode1 || op1mode == VOIDmode));
5406
5407 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5408 op0 = copy_to_mode_reg (mode0, op0);
5409 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5410 op1 = copy_to_mode_reg (mode1, op1);
5411
5412 if (macflag == -1)
5413 pat = GEN_FCN (icode) (target, op0, op1);
5414 else
5415 pat = GEN_FCN (icode) (target, op0, op1, GEN_INT (macflag));
5416 if (! pat)
5417 return 0;
5418
5419 emit_insn (pat);
5420 return target;
5421 }
5422
5423 /* Subroutine of bfin_expand_builtin to take care of unop insns. */
5424
5425 static rtx
5426 bfin_expand_unop_builtin (enum insn_code icode, tree exp,
5427 rtx target)
5428 {
5429 rtx pat;
5430 tree arg0 = CALL_EXPR_ARG (exp, 0);
5431 rtx op0 = expand_normal (arg0);
5432 machine_mode op0mode = GET_MODE (op0);
5433 machine_mode tmode = insn_data[icode].operand[0].mode;
5434 machine_mode mode0 = insn_data[icode].operand[1].mode;
5435
5436 if (! target
5437 || GET_MODE (target) != tmode
5438 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5439 target = gen_reg_rtx (tmode);
5440
5441 if (VECTOR_MODE_P (mode0))
5442 op0 = safe_vector_operand (op0, mode0);
5443
5444 if (op0mode == SImode && mode0 == HImode)
5445 {
5446 op0mode = HImode;
5447 op0 = gen_lowpart (HImode, op0);
5448 }
5449 gcc_assert (op0mode == mode0 || op0mode == VOIDmode);
5450
5451 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5452 op0 = copy_to_mode_reg (mode0, op0);
5453
5454 pat = GEN_FCN (icode) (target, op0);
5455 if (! pat)
5456 return 0;
5457 emit_insn (pat);
5458 return target;
5459 }
5460
5461 /* Expand an expression EXP that calls a built-in function,
5462 with result going to TARGET if that's convenient
5463 (and in mode MODE if that's convenient).
5464 SUBTARGET may be used as the target for computing one of EXP's operands.
5465 IGNORE is nonzero if the value is to be ignored. */
5466
5467 static rtx
5468 bfin_expand_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
5469 rtx subtarget ATTRIBUTE_UNUSED,
5470 machine_mode mode ATTRIBUTE_UNUSED,
5471 int ignore ATTRIBUTE_UNUSED)
5472 {
5473 size_t i;
5474 enum insn_code icode;
5475 const struct builtin_description *d;
5476 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
5477 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5478 tree arg0, arg1, arg2;
5479 rtx op0, op1, op2, accvec, pat, tmp1, tmp2, a0reg, a1reg;
5480 machine_mode tmode, mode0;
5481
5482 switch (fcode)
5483 {
5484 case BFIN_BUILTIN_CSYNC:
5485 emit_insn (gen_csync ());
5486 return 0;
5487 case BFIN_BUILTIN_SSYNC:
5488 emit_insn (gen_ssync ());
5489 return 0;
5490
5491 case BFIN_BUILTIN_DIFFHL_2X16:
5492 case BFIN_BUILTIN_DIFFLH_2X16:
5493 case BFIN_BUILTIN_SUM_2X16:
5494 arg0 = CALL_EXPR_ARG (exp, 0);
5495 op0 = expand_normal (arg0);
5496 icode = (fcode == BFIN_BUILTIN_DIFFHL_2X16 ? CODE_FOR_subhilov2hi3
5497 : fcode == BFIN_BUILTIN_DIFFLH_2X16 ? CODE_FOR_sublohiv2hi3
5498 : CODE_FOR_ssaddhilov2hi3);
5499 tmode = insn_data[icode].operand[0].mode;
5500 mode0 = insn_data[icode].operand[1].mode;
5501
5502 if (! target
5503 || GET_MODE (target) != tmode
5504 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5505 target = gen_reg_rtx (tmode);
5506
5507 if (VECTOR_MODE_P (mode0))
5508 op0 = safe_vector_operand (op0, mode0);
5509
5510 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5511 op0 = copy_to_mode_reg (mode0, op0);
5512
5513 pat = GEN_FCN (icode) (target, op0, op0);
5514 if (! pat)
5515 return 0;
5516 emit_insn (pat);
5517 return target;
5518
5519 case BFIN_BUILTIN_MULT_1X32X32:
5520 case BFIN_BUILTIN_MULT_1X32X32NS:
5521 arg0 = CALL_EXPR_ARG (exp, 0);
5522 arg1 = CALL_EXPR_ARG (exp, 1);
5523 op0 = expand_normal (arg0);
5524 op1 = expand_normal (arg1);
5525 if (! target
5526 || !register_operand (target, SImode))
5527 target = gen_reg_rtx (SImode);
5528 if (! register_operand (op0, SImode))
5529 op0 = copy_to_mode_reg (SImode, op0);
5530 if (! register_operand (op1, SImode))
5531 op1 = copy_to_mode_reg (SImode, op1);
5532
5533 a1reg = gen_rtx_REG (PDImode, REG_A1);
5534 a0reg = gen_rtx_REG (PDImode, REG_A0);
5535 tmp1 = gen_lowpart (V2HImode, op0);
5536 tmp2 = gen_lowpart (V2HImode, op1);
5537 emit_insn (gen_flag_macinit1hi (a1reg,
5538 gen_lowpart (HImode, op0),
5539 gen_lowpart (HImode, op1),
5540 GEN_INT (MACFLAG_FU)));
5541 emit_insn (gen_lshrpdi3 (a1reg, a1reg, GEN_INT (16)));
5542
5543 if (fcode == BFIN_BUILTIN_MULT_1X32X32)
5544 emit_insn (gen_flag_mul_macv2hi_parts_acconly (a0reg, a1reg, tmp1, tmp2,
5545 const1_rtx, const1_rtx,
5546 const1_rtx, const0_rtx, a1reg,
5547 const0_rtx, GEN_INT (MACFLAG_NONE),
5548 GEN_INT (MACFLAG_M)));
5549 else
5550 {
5551 /* For saturating multiplication, there's exactly one special case
5552 to be handled: multiplying the smallest negative value with
5553 itself. Due to shift correction in fractional multiplies, this
5554 can overflow. Iff this happens, OP2 will contain 1, which, when
5555 added in 32 bits to the smallest negative, wraps to the largest
5556 positive, which is the result we want. */
5557 op2 = gen_reg_rtx (V2HImode);
5558 emit_insn (gen_packv2hi (op2, tmp1, tmp2, const0_rtx, const0_rtx));
5559 emit_insn (gen_movsibi (gen_rtx_REG (BImode, REG_CC),
5560 gen_lowpart (SImode, op2)));
5561 emit_insn (gen_flag_mul_macv2hi_parts_acconly_andcc0 (a0reg, a1reg, tmp1, tmp2,
5562 const1_rtx, const1_rtx,
5563 const1_rtx, const0_rtx, a1reg,
5564 const0_rtx, GEN_INT (MACFLAG_NONE),
5565 GEN_INT (MACFLAG_M)));
5566 op2 = gen_reg_rtx (SImode);
5567 emit_insn (gen_movbisi (op2, gen_rtx_REG (BImode, REG_CC)));
5568 }
5569 emit_insn (gen_flag_machi_parts_acconly (a1reg, tmp2, tmp1,
5570 const1_rtx, const0_rtx,
5571 a1reg, const0_rtx, GEN_INT (MACFLAG_M)));
5572 emit_insn (gen_ashrpdi3 (a1reg, a1reg, GEN_INT (15)));
5573 emit_insn (gen_sum_of_accumulators (target, a0reg, a0reg, a1reg));
5574 if (fcode == BFIN_BUILTIN_MULT_1X32X32NS)
5575 emit_insn (gen_addsi3 (target, target, op2));
5576 return target;
5577
5578 case BFIN_BUILTIN_CPLX_MUL_16:
5579 case BFIN_BUILTIN_CPLX_MUL_16_S40:
5580 arg0 = CALL_EXPR_ARG (exp, 0);
5581 arg1 = CALL_EXPR_ARG (exp, 1);
5582 op0 = expand_normal (arg0);
5583 op1 = expand_normal (arg1);
5584 accvec = gen_reg_rtx (V2PDImode);
5585 icode = CODE_FOR_flag_macv2hi_parts;
5586 tmode = insn_data[icode].operand[0].mode;
5587
5588 if (! target
5589 || GET_MODE (target) != V2HImode
5590 || ! (*insn_data[icode].operand[0].predicate) (target, V2HImode))
5591 target = gen_reg_rtx (tmode);
5592 if (! register_operand (op0, GET_MODE (op0)))
5593 op0 = copy_to_mode_reg (GET_MODE (op0), op0);
5594 if (! register_operand (op1, GET_MODE (op1)))
5595 op1 = copy_to_mode_reg (GET_MODE (op1), op1);
5596
5597 if (fcode == BFIN_BUILTIN_CPLX_MUL_16)
5598 emit_insn (gen_flag_macinit1v2hi_parts (accvec, op0, op1, const0_rtx,
5599 const0_rtx, const0_rtx,
5600 const1_rtx, GEN_INT (MACFLAG_W32)));
5601 else
5602 emit_insn (gen_flag_macinit1v2hi_parts (accvec, op0, op1, const0_rtx,
5603 const0_rtx, const0_rtx,
5604 const1_rtx, GEN_INT (MACFLAG_NONE)));
5605 emit_insn (gen_flag_macv2hi_parts (target, op0, op1, const1_rtx,
5606 const1_rtx, const1_rtx,
5607 const0_rtx, accvec, const1_rtx, const0_rtx,
5608 GEN_INT (MACFLAG_NONE), accvec));
5609
5610 return target;
5611
5612 case BFIN_BUILTIN_CPLX_MAC_16:
5613 case BFIN_BUILTIN_CPLX_MSU_16:
5614 case BFIN_BUILTIN_CPLX_MAC_16_S40:
5615 case BFIN_BUILTIN_CPLX_MSU_16_S40:
5616 arg0 = CALL_EXPR_ARG (exp, 0);
5617 arg1 = CALL_EXPR_ARG (exp, 1);
5618 arg2 = CALL_EXPR_ARG (exp, 2);
5619 op0 = expand_normal (arg0);
5620 op1 = expand_normal (arg1);
5621 op2 = expand_normal (arg2);
5622 accvec = gen_reg_rtx (V2PDImode);
5623 icode = CODE_FOR_flag_macv2hi_parts;
5624 tmode = insn_data[icode].operand[0].mode;
5625
5626 if (! target
5627 || GET_MODE (target) != V2HImode
5628 || ! (*insn_data[icode].operand[0].predicate) (target, V2HImode))
5629 target = gen_reg_rtx (tmode);
5630 if (! register_operand (op1, GET_MODE (op1)))
5631 op1 = copy_to_mode_reg (GET_MODE (op1), op1);
5632 if (! register_operand (op2, GET_MODE (op2)))
5633 op2 = copy_to_mode_reg (GET_MODE (op2), op2);
5634
5635 tmp1 = gen_reg_rtx (SImode);
5636 tmp2 = gen_reg_rtx (SImode);
5637 emit_insn (gen_ashlsi3 (tmp1, gen_lowpart (SImode, op0), GEN_INT (16)));
5638 emit_move_insn (tmp2, gen_lowpart (SImode, op0));
5639 emit_insn (gen_movstricthi_1 (gen_lowpart (HImode, tmp2), const0_rtx));
5640 emit_insn (gen_load_accumulator_pair (accvec, tmp1, tmp2));
5641 if (fcode == BFIN_BUILTIN_CPLX_MAC_16
5642 || fcode == BFIN_BUILTIN_CPLX_MSU_16)
5643 emit_insn (gen_flag_macv2hi_parts_acconly (accvec, op1, op2, const0_rtx,
5644 const0_rtx, const0_rtx,
5645 const1_rtx, accvec, const0_rtx,
5646 const0_rtx,
5647 GEN_INT (MACFLAG_W32)));
5648 else
5649 emit_insn (gen_flag_macv2hi_parts_acconly (accvec, op1, op2, const0_rtx,
5650 const0_rtx, const0_rtx,
5651 const1_rtx, accvec, const0_rtx,
5652 const0_rtx,
5653 GEN_INT (MACFLAG_NONE)));
5654 if (fcode == BFIN_BUILTIN_CPLX_MAC_16
5655 || fcode == BFIN_BUILTIN_CPLX_MAC_16_S40)
5656 {
5657 tmp1 = const1_rtx;
5658 tmp2 = const0_rtx;
5659 }
5660 else
5661 {
5662 tmp1 = const0_rtx;
5663 tmp2 = const1_rtx;
5664 }
5665 emit_insn (gen_flag_macv2hi_parts (target, op1, op2, const1_rtx,
5666 const1_rtx, const1_rtx,
5667 const0_rtx, accvec, tmp1, tmp2,
5668 GEN_INT (MACFLAG_NONE), accvec));
5669
5670 return target;
5671
5672 case BFIN_BUILTIN_CPLX_SQU:
5673 arg0 = CALL_EXPR_ARG (exp, 0);
5674 op0 = expand_normal (arg0);
5675 accvec = gen_reg_rtx (V2PDImode);
5676 icode = CODE_FOR_flag_mulv2hi;
5677 tmp1 = gen_reg_rtx (V2HImode);
5678 tmp2 = gen_reg_rtx (V2HImode);
5679
5680 if (! target
5681 || GET_MODE (target) != V2HImode
5682 || ! (*insn_data[icode].operand[0].predicate) (target, V2HImode))
5683 target = gen_reg_rtx (V2HImode);
5684 if (! register_operand (op0, GET_MODE (op0)))
5685 op0 = copy_to_mode_reg (GET_MODE (op0), op0);
5686
5687 emit_insn (gen_flag_mulv2hi (tmp1, op0, op0, GEN_INT (MACFLAG_NONE)));
5688
5689 emit_insn (gen_flag_mulhi_parts (gen_lowpart (HImode, tmp2), op0, op0,
5690 const0_rtx, const1_rtx,
5691 GEN_INT (MACFLAG_NONE)));
5692
5693 emit_insn (gen_ssaddhi3_high_parts (target, tmp2, tmp2, tmp2, const0_rtx,
5694 const0_rtx));
5695 emit_insn (gen_sssubhi3_low_parts (target, target, tmp1, tmp1,
5696 const0_rtx, const1_rtx));
5697
5698 return target;
5699
5700 default:
5701 break;
5702 }
5703
5704 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5705 if (d->code == fcode)
5706 return bfin_expand_binop_builtin (d->icode, exp, target,
5707 d->macflag);
5708
5709 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5710 if (d->code == fcode)
5711 return bfin_expand_unop_builtin (d->icode, exp, target);
5712
5713 gcc_unreachable ();
5714 }
5715
5716 static void
5717 bfin_conditional_register_usage (void)
5718 {
5719 /* initialize condition code flag register rtx */
5720 bfin_cc_rtx = gen_rtx_REG (BImode, REG_CC);
5721 bfin_rets_rtx = gen_rtx_REG (Pmode, REG_RETS);
5722 if (TARGET_FDPIC)
5723 call_used_regs[FDPIC_REGNO] = 1;
5724 if (!TARGET_FDPIC && flag_pic)
5725 {
5726 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
5727 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
5728 }
5729 }
5730 \f
5731 #undef TARGET_INIT_BUILTINS
5732 #define TARGET_INIT_BUILTINS bfin_init_builtins
5733
5734 #undef TARGET_EXPAND_BUILTIN
5735 #define TARGET_EXPAND_BUILTIN bfin_expand_builtin
5736
5737 #undef TARGET_ASM_GLOBALIZE_LABEL
5738 #define TARGET_ASM_GLOBALIZE_LABEL bfin_globalize_label
5739
5740 #undef TARGET_ASM_FILE_START
5741 #define TARGET_ASM_FILE_START output_file_start
5742
5743 #undef TARGET_ATTRIBUTE_TABLE
5744 #define TARGET_ATTRIBUTE_TABLE bfin_attribute_table
5745
5746 #undef TARGET_COMP_TYPE_ATTRIBUTES
5747 #define TARGET_COMP_TYPE_ATTRIBUTES bfin_comp_type_attributes
5748
5749 #undef TARGET_RTX_COSTS
5750 #define TARGET_RTX_COSTS bfin_rtx_costs
5751
5752 #undef TARGET_ADDRESS_COST
5753 #define TARGET_ADDRESS_COST bfin_address_cost
5754
5755 #undef TARGET_REGISTER_MOVE_COST
5756 #define TARGET_REGISTER_MOVE_COST bfin_register_move_cost
5757
5758 #undef TARGET_MEMORY_MOVE_COST
5759 #define TARGET_MEMORY_MOVE_COST bfin_memory_move_cost
5760
5761 #undef TARGET_ASM_INTEGER
5762 #define TARGET_ASM_INTEGER bfin_assemble_integer
5763
5764 #undef TARGET_MACHINE_DEPENDENT_REORG
5765 #define TARGET_MACHINE_DEPENDENT_REORG bfin_reorg
5766
5767 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
5768 #define TARGET_FUNCTION_OK_FOR_SIBCALL bfin_function_ok_for_sibcall
5769
5770 #undef TARGET_ASM_OUTPUT_MI_THUNK
5771 #define TARGET_ASM_OUTPUT_MI_THUNK bfin_output_mi_thunk
5772 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
5773 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
5774
5775 #undef TARGET_SCHED_ADJUST_COST
5776 #define TARGET_SCHED_ADJUST_COST bfin_adjust_cost
5777
5778 #undef TARGET_SCHED_ISSUE_RATE
5779 #define TARGET_SCHED_ISSUE_RATE bfin_issue_rate
5780
5781 #undef TARGET_PROMOTE_FUNCTION_MODE
5782 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
5783
5784 #undef TARGET_ARG_PARTIAL_BYTES
5785 #define TARGET_ARG_PARTIAL_BYTES bfin_arg_partial_bytes
5786
5787 #undef TARGET_FUNCTION_ARG
5788 #define TARGET_FUNCTION_ARG bfin_function_arg
5789
5790 #undef TARGET_FUNCTION_ARG_ADVANCE
5791 #define TARGET_FUNCTION_ARG_ADVANCE bfin_function_arg_advance
5792
5793 #undef TARGET_PASS_BY_REFERENCE
5794 #define TARGET_PASS_BY_REFERENCE bfin_pass_by_reference
5795
5796 #undef TARGET_SETUP_INCOMING_VARARGS
5797 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
5798
5799 #undef TARGET_STRUCT_VALUE_RTX
5800 #define TARGET_STRUCT_VALUE_RTX bfin_struct_value_rtx
5801
5802 #undef TARGET_VECTOR_MODE_SUPPORTED_P
5803 #define TARGET_VECTOR_MODE_SUPPORTED_P bfin_vector_mode_supported_p
5804
5805 #undef TARGET_OPTION_OVERRIDE
5806 #define TARGET_OPTION_OVERRIDE bfin_option_override
5807
5808 #undef TARGET_SECONDARY_RELOAD
5809 #define TARGET_SECONDARY_RELOAD bfin_secondary_reload
5810
5811 #undef TARGET_CLASS_LIKELY_SPILLED_P
5812 #define TARGET_CLASS_LIKELY_SPILLED_P bfin_class_likely_spilled_p
5813
5814 #undef TARGET_DELEGITIMIZE_ADDRESS
5815 #define TARGET_DELEGITIMIZE_ADDRESS bfin_delegitimize_address
5816
5817 #undef TARGET_LEGITIMATE_CONSTANT_P
5818 #define TARGET_LEGITIMATE_CONSTANT_P bfin_legitimate_constant_p
5819
5820 #undef TARGET_CANNOT_FORCE_CONST_MEM
5821 #define TARGET_CANNOT_FORCE_CONST_MEM bfin_cannot_force_const_mem
5822
5823 #undef TARGET_RETURN_IN_MEMORY
5824 #define TARGET_RETURN_IN_MEMORY bfin_return_in_memory
5825
5826 #undef TARGET_LEGITIMATE_ADDRESS_P
5827 #define TARGET_LEGITIMATE_ADDRESS_P bfin_legitimate_address_p
5828
5829 #undef TARGET_FRAME_POINTER_REQUIRED
5830 #define TARGET_FRAME_POINTER_REQUIRED bfin_frame_pointer_required
5831
5832 #undef TARGET_CAN_ELIMINATE
5833 #define TARGET_CAN_ELIMINATE bfin_can_eliminate
5834
5835 #undef TARGET_CONDITIONAL_REGISTER_USAGE
5836 #define TARGET_CONDITIONAL_REGISTER_USAGE bfin_conditional_register_usage
5837
5838 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
5839 #define TARGET_ASM_TRAMPOLINE_TEMPLATE bfin_asm_trampoline_template
5840 #undef TARGET_TRAMPOLINE_INIT
5841 #define TARGET_TRAMPOLINE_INIT bfin_trampoline_init
5842
5843 #undef TARGET_EXTRA_LIVE_ON_ENTRY
5844 #define TARGET_EXTRA_LIVE_ON_ENTRY bfin_extra_live_on_entry
5845
5846 /* Passes after sched2 can break the helpful TImode annotations that
5847 haifa-sched puts on every insn. Just do scheduling in reorg. */
5848 #undef TARGET_DELAY_SCHED2
5849 #define TARGET_DELAY_SCHED2 true
5850
5851 /* Variable tracking should be run after all optimizations which
5852 change order of insns. It also needs a valid CFG. */
5853 #undef TARGET_DELAY_VARTRACK
5854 #define TARGET_DELAY_VARTRACK true
5855
5856 #undef TARGET_CAN_USE_DOLOOP_P
5857 #define TARGET_CAN_USE_DOLOOP_P bfin_can_use_doloop_p
5858
5859 struct gcc_target targetm = TARGET_INITIALIZER;