]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/xtensa/xtensa.c
2015-06-17 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / config / xtensa / xtensa.c
1 /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 "predict.h"
29 #include "function.h"
30 #include "dominance.h"
31 #include "cfg.h"
32 #include "cfgrtl.h"
33 #include "cfganal.h"
34 #include "lcm.h"
35 #include "cfgbuild.h"
36 #include "cfgcleanup.h"
37 #include "basic-block.h"
38 #include "insn-config.h"
39 #include "conditions.h"
40 #include "insn-flags.h"
41 #include "insn-attr.h"
42 #include "insn-codes.h"
43 #include "recog.h"
44 #include "output.h"
45 #include "symtab.h"
46 #include "tree.h"
47 #include "fold-const.h"
48 #include "stringpool.h"
49 #include "stor-layout.h"
50 #include "calls.h"
51 #include "varasm.h"
52 #include "flags.h"
53 #include "alias.h"
54 #include "expmed.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "emit-rtl.h"
58 #include "stmt.h"
59 #include "expr.h"
60 #include "reload.h"
61 #include "tm_p.h"
62 #include "diagnostic-core.h"
63 #include "optabs.h"
64 #include "libfuncs.h"
65 #include "target.h"
66 #include "target-def.h"
67 #include "langhooks.h"
68 #include "tree-ssa-alias.h"
69 #include "internal-fn.h"
70 #include "gimple-fold.h"
71 #include "tree-eh.h"
72 #include "gimple-expr.h"
73 #include "gimple.h"
74 #include "gimplify.h"
75 #include "df.h"
76 #include "builtins.h"
77 #include "dumpfile.h"
78 #include "hw-doloop.h"
79 #include "rtl-iter.h"
80
81
82 /* Enumeration for all of the relational tests, so that we can build
83 arrays indexed by the test type, and not worry about the order
84 of EQ, NE, etc. */
85
86 enum internal_test
87 {
88 ITEST_EQ,
89 ITEST_NE,
90 ITEST_GT,
91 ITEST_GE,
92 ITEST_LT,
93 ITEST_LE,
94 ITEST_GTU,
95 ITEST_GEU,
96 ITEST_LTU,
97 ITEST_LEU,
98 ITEST_MAX
99 };
100
101 /* Array giving truth value on whether or not a given hard register
102 can support a given mode. */
103 char xtensa_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
104
105 /* Current frame size calculated by compute_frame_size. */
106 unsigned xtensa_current_frame_size;
107 /* Callee-save area size in the current frame calculated by compute_frame_size. */
108 int xtensa_callee_save_size;
109
110 /* Largest block move to handle in-line. */
111 #define LARGEST_MOVE_RATIO 15
112
113 /* Define the structure for the machine field in struct function. */
114 struct GTY(()) machine_function
115 {
116 int accesses_prev_frame;
117 bool need_a7_copy;
118 bool vararg_a7;
119 rtx vararg_a7_copy;
120 rtx_insn *set_frame_ptr_insn;
121 };
122
123 /* Vector, indexed by hard register number, which contains 1 for a
124 register that is allowable in a candidate for leaf function
125 treatment. */
126
127 const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
128 {
129 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
130 1, 1, 1,
131 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
132 1
133 };
134
135 static void xtensa_option_override (void);
136 static enum internal_test map_test_to_internal_test (enum rtx_code);
137 static rtx gen_int_relational (enum rtx_code, rtx, rtx, int *);
138 static rtx gen_float_relational (enum rtx_code, rtx, rtx);
139 static rtx gen_conditional_move (enum rtx_code, machine_mode, rtx, rtx);
140 static rtx fixup_subreg_mem (rtx);
141 static struct machine_function * xtensa_init_machine_status (void);
142 static rtx xtensa_legitimize_tls_address (rtx);
143 static rtx xtensa_legitimize_address (rtx, rtx, machine_mode);
144 static bool xtensa_mode_dependent_address_p (const_rtx, addr_space_t);
145 static bool xtensa_return_in_msb (const_tree);
146 static void printx (FILE *, signed int);
147 static rtx xtensa_builtin_saveregs (void);
148 static bool xtensa_legitimate_address_p (machine_mode, rtx, bool);
149 static unsigned int xtensa_multibss_section_type_flags (tree, const char *,
150 int) ATTRIBUTE_UNUSED;
151 static section *xtensa_select_rtx_section (machine_mode, rtx,
152 unsigned HOST_WIDE_INT);
153 static bool xtensa_rtx_costs (rtx, int, int, int, int *, bool);
154 static int xtensa_register_move_cost (machine_mode, reg_class_t,
155 reg_class_t);
156 static int xtensa_memory_move_cost (machine_mode, reg_class_t, bool);
157 static tree xtensa_build_builtin_va_list (void);
158 static bool xtensa_return_in_memory (const_tree, const_tree);
159 static tree xtensa_gimplify_va_arg_expr (tree, tree, gimple_seq *,
160 gimple_seq *);
161 static void xtensa_function_arg_advance (cumulative_args_t, machine_mode,
162 const_tree, bool);
163 static rtx xtensa_function_arg (cumulative_args_t, machine_mode,
164 const_tree, bool);
165 static rtx xtensa_function_incoming_arg (cumulative_args_t,
166 machine_mode, const_tree, bool);
167 static rtx xtensa_function_value (const_tree, const_tree, bool);
168 static rtx xtensa_libcall_value (machine_mode, const_rtx);
169 static bool xtensa_function_value_regno_p (const unsigned int);
170 static unsigned int xtensa_function_arg_boundary (machine_mode,
171 const_tree);
172 static void xtensa_init_builtins (void);
173 static tree xtensa_fold_builtin (tree, int, tree *, bool);
174 static rtx xtensa_expand_builtin (tree, rtx, rtx, machine_mode, int);
175 static void xtensa_va_start (tree, rtx);
176 static bool xtensa_frame_pointer_required (void);
177 static rtx xtensa_static_chain (const_tree, bool);
178 static void xtensa_asm_trampoline_template (FILE *);
179 static void xtensa_trampoline_init (rtx, tree, rtx);
180 static bool xtensa_output_addr_const_extra (FILE *, rtx);
181 static bool xtensa_cannot_force_const_mem (machine_mode, rtx);
182
183 static reg_class_t xtensa_preferred_reload_class (rtx, reg_class_t);
184 static reg_class_t xtensa_preferred_output_reload_class (rtx, reg_class_t);
185 static reg_class_t xtensa_secondary_reload (bool, rtx, reg_class_t,
186 machine_mode,
187 struct secondary_reload_info *);
188
189 static bool constantpool_address_p (const_rtx addr);
190 static bool xtensa_legitimate_constant_p (machine_mode, rtx);
191 static void xtensa_reorg (void);
192 static bool xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
193 unsigned int, bool);
194 static const char *xtensa_invalid_within_doloop (const rtx_insn *);
195
196 static bool xtensa_member_type_forces_blk (const_tree,
197 machine_mode mode);
198
199 static void xtensa_conditional_register_usage (void);
200
201 \f
202
203 /* These hooks specify assembly directives for creating certain kinds
204 of integer object. */
205
206 #undef TARGET_ASM_ALIGNED_SI_OP
207 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
208
209 #undef TARGET_ASM_SELECT_RTX_SECTION
210 #define TARGET_ASM_SELECT_RTX_SECTION xtensa_select_rtx_section
211
212 #undef TARGET_LEGITIMIZE_ADDRESS
213 #define TARGET_LEGITIMIZE_ADDRESS xtensa_legitimize_address
214 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
215 #define TARGET_MODE_DEPENDENT_ADDRESS_P xtensa_mode_dependent_address_p
216
217 #undef TARGET_REGISTER_MOVE_COST
218 #define TARGET_REGISTER_MOVE_COST xtensa_register_move_cost
219 #undef TARGET_MEMORY_MOVE_COST
220 #define TARGET_MEMORY_MOVE_COST xtensa_memory_move_cost
221 #undef TARGET_RTX_COSTS
222 #define TARGET_RTX_COSTS xtensa_rtx_costs
223 #undef TARGET_ADDRESS_COST
224 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
225
226 #undef TARGET_MEMBER_TYPE_FORCES_BLK
227 #define TARGET_MEMBER_TYPE_FORCES_BLK xtensa_member_type_forces_blk
228
229 #undef TARGET_BUILD_BUILTIN_VA_LIST
230 #define TARGET_BUILD_BUILTIN_VA_LIST xtensa_build_builtin_va_list
231
232 #undef TARGET_EXPAND_BUILTIN_VA_START
233 #define TARGET_EXPAND_BUILTIN_VA_START xtensa_va_start
234
235 #undef TARGET_PROMOTE_FUNCTION_MODE
236 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
237 #undef TARGET_PROMOTE_PROTOTYPES
238 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
239
240 #undef TARGET_RETURN_IN_MEMORY
241 #define TARGET_RETURN_IN_MEMORY xtensa_return_in_memory
242 #undef TARGET_FUNCTION_VALUE
243 #define TARGET_FUNCTION_VALUE xtensa_function_value
244 #undef TARGET_LIBCALL_VALUE
245 #define TARGET_LIBCALL_VALUE xtensa_libcall_value
246 #undef TARGET_FUNCTION_VALUE_REGNO_P
247 #define TARGET_FUNCTION_VALUE_REGNO_P xtensa_function_value_regno_p
248
249 #undef TARGET_SPLIT_COMPLEX_ARG
250 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
251 #undef TARGET_MUST_PASS_IN_STACK
252 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
253 #undef TARGET_FUNCTION_ARG_ADVANCE
254 #define TARGET_FUNCTION_ARG_ADVANCE xtensa_function_arg_advance
255 #undef TARGET_FUNCTION_ARG
256 #define TARGET_FUNCTION_ARG xtensa_function_arg
257 #undef TARGET_FUNCTION_INCOMING_ARG
258 #define TARGET_FUNCTION_INCOMING_ARG xtensa_function_incoming_arg
259 #undef TARGET_FUNCTION_ARG_BOUNDARY
260 #define TARGET_FUNCTION_ARG_BOUNDARY xtensa_function_arg_boundary
261
262 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
263 #define TARGET_EXPAND_BUILTIN_SAVEREGS xtensa_builtin_saveregs
264 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
265 #define TARGET_GIMPLIFY_VA_ARG_EXPR xtensa_gimplify_va_arg_expr
266
267 #undef TARGET_RETURN_IN_MSB
268 #define TARGET_RETURN_IN_MSB xtensa_return_in_msb
269
270 #undef TARGET_INIT_BUILTINS
271 #define TARGET_INIT_BUILTINS xtensa_init_builtins
272 #undef TARGET_FOLD_BUILTIN
273 #define TARGET_FOLD_BUILTIN xtensa_fold_builtin
274 #undef TARGET_EXPAND_BUILTIN
275 #define TARGET_EXPAND_BUILTIN xtensa_expand_builtin
276
277 #undef TARGET_PREFERRED_RELOAD_CLASS
278 #define TARGET_PREFERRED_RELOAD_CLASS xtensa_preferred_reload_class
279 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
280 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS xtensa_preferred_output_reload_class
281
282 #undef TARGET_SECONDARY_RELOAD
283 #define TARGET_SECONDARY_RELOAD xtensa_secondary_reload
284
285 #undef TARGET_HAVE_TLS
286 #define TARGET_HAVE_TLS (TARGET_THREADPTR && HAVE_AS_TLS)
287
288 #undef TARGET_CANNOT_FORCE_CONST_MEM
289 #define TARGET_CANNOT_FORCE_CONST_MEM xtensa_cannot_force_const_mem
290
291 #undef TARGET_LEGITIMATE_ADDRESS_P
292 #define TARGET_LEGITIMATE_ADDRESS_P xtensa_legitimate_address_p
293
294 #undef TARGET_FRAME_POINTER_REQUIRED
295 #define TARGET_FRAME_POINTER_REQUIRED xtensa_frame_pointer_required
296
297 #undef TARGET_STATIC_CHAIN
298 #define TARGET_STATIC_CHAIN xtensa_static_chain
299 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
300 #define TARGET_ASM_TRAMPOLINE_TEMPLATE xtensa_asm_trampoline_template
301 #undef TARGET_TRAMPOLINE_INIT
302 #define TARGET_TRAMPOLINE_INIT xtensa_trampoline_init
303
304 #undef TARGET_OPTION_OVERRIDE
305 #define TARGET_OPTION_OVERRIDE xtensa_option_override
306
307 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
308 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA xtensa_output_addr_const_extra
309
310 #undef TARGET_LEGITIMATE_CONSTANT_P
311 #define TARGET_LEGITIMATE_CONSTANT_P xtensa_legitimate_constant_p
312
313 #undef TARGET_MACHINE_DEPENDENT_REORG
314 #define TARGET_MACHINE_DEPENDENT_REORG xtensa_reorg
315
316 #undef TARGET_CAN_USE_DOLOOP_P
317 #define TARGET_CAN_USE_DOLOOP_P xtensa_can_use_doloop_p
318
319 #undef TARGET_INVALID_WITHIN_DOLOOP
320 #define TARGET_INVALID_WITHIN_DOLOOP xtensa_invalid_within_doloop
321
322 #undef TARGET_CONDITIONAL_REGISTER_USAGE
323 #define TARGET_CONDITIONAL_REGISTER_USAGE xtensa_conditional_register_usage
324
325 struct gcc_target targetm = TARGET_INITIALIZER;
326
327 \f
328 /* Functions to test Xtensa immediate operand validity. */
329
330 bool
331 xtensa_simm8 (HOST_WIDE_INT v)
332 {
333 return v >= -128 && v <= 127;
334 }
335
336
337 bool
338 xtensa_simm8x256 (HOST_WIDE_INT v)
339 {
340 return (v & 255) == 0 && (v >= -32768 && v <= 32512);
341 }
342
343
344 bool
345 xtensa_simm12b (HOST_WIDE_INT v)
346 {
347 return v >= -2048 && v <= 2047;
348 }
349
350
351 static bool
352 xtensa_uimm8 (HOST_WIDE_INT v)
353 {
354 return v >= 0 && v <= 255;
355 }
356
357
358 static bool
359 xtensa_uimm8x2 (HOST_WIDE_INT v)
360 {
361 return (v & 1) == 0 && (v >= 0 && v <= 510);
362 }
363
364
365 static bool
366 xtensa_uimm8x4 (HOST_WIDE_INT v)
367 {
368 return (v & 3) == 0 && (v >= 0 && v <= 1020);
369 }
370
371
372 static bool
373 xtensa_b4const (HOST_WIDE_INT v)
374 {
375 switch (v)
376 {
377 case -1:
378 case 1:
379 case 2:
380 case 3:
381 case 4:
382 case 5:
383 case 6:
384 case 7:
385 case 8:
386 case 10:
387 case 12:
388 case 16:
389 case 32:
390 case 64:
391 case 128:
392 case 256:
393 return true;
394 }
395 return false;
396 }
397
398
399 bool
400 xtensa_b4const_or_zero (HOST_WIDE_INT v)
401 {
402 if (v == 0)
403 return true;
404 return xtensa_b4const (v);
405 }
406
407
408 bool
409 xtensa_b4constu (HOST_WIDE_INT v)
410 {
411 switch (v)
412 {
413 case 32768:
414 case 65536:
415 case 2:
416 case 3:
417 case 4:
418 case 5:
419 case 6:
420 case 7:
421 case 8:
422 case 10:
423 case 12:
424 case 16:
425 case 32:
426 case 64:
427 case 128:
428 case 256:
429 return true;
430 }
431 return false;
432 }
433
434
435 bool
436 xtensa_mask_immediate (HOST_WIDE_INT v)
437 {
438 #define MAX_MASK_SIZE 16
439 int mask_size;
440
441 for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
442 {
443 if ((v & 1) == 0)
444 return false;
445 v = v >> 1;
446 if (v == 0)
447 return true;
448 }
449
450 return false;
451 }
452
453
454 /* This is just like the standard true_regnum() function except that it
455 works even when reg_renumber is not initialized. */
456
457 int
458 xt_true_regnum (rtx x)
459 {
460 if (GET_CODE (x) == REG)
461 {
462 if (reg_renumber
463 && REGNO (x) >= FIRST_PSEUDO_REGISTER
464 && reg_renumber[REGNO (x)] >= 0)
465 return reg_renumber[REGNO (x)];
466 return REGNO (x);
467 }
468 if (GET_CODE (x) == SUBREG)
469 {
470 int base = xt_true_regnum (SUBREG_REG (x));
471 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
472 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
473 GET_MODE (SUBREG_REG (x)),
474 SUBREG_BYTE (x), GET_MODE (x));
475 }
476 return -1;
477 }
478
479
480 int
481 xtensa_valid_move (machine_mode mode, rtx *operands)
482 {
483 /* Either the destination or source must be a register, and the
484 MAC16 accumulator doesn't count. */
485
486 if (register_operand (operands[0], mode))
487 {
488 int dst_regnum = xt_true_regnum (operands[0]);
489
490 /* The stack pointer can only be assigned with a MOVSP opcode. */
491 if (dst_regnum == STACK_POINTER_REGNUM)
492 return !TARGET_WINDOWED_ABI
493 || (mode == SImode
494 && register_operand (operands[1], mode)
495 && !ACC_REG_P (xt_true_regnum (operands[1])));
496
497 if (!ACC_REG_P (dst_regnum))
498 return true;
499 }
500 if (register_operand (operands[1], mode))
501 {
502 int src_regnum = xt_true_regnum (operands[1]);
503 if (!ACC_REG_P (src_regnum))
504 return true;
505 }
506 return FALSE;
507 }
508
509
510 int
511 smalloffset_mem_p (rtx op)
512 {
513 if (GET_CODE (op) == MEM)
514 {
515 rtx addr = XEXP (op, 0);
516 if (GET_CODE (addr) == REG)
517 return BASE_REG_P (addr, 0);
518 if (GET_CODE (addr) == PLUS)
519 {
520 rtx offset = XEXP (addr, 0);
521 HOST_WIDE_INT val;
522 if (GET_CODE (offset) != CONST_INT)
523 offset = XEXP (addr, 1);
524 if (GET_CODE (offset) != CONST_INT)
525 return FALSE;
526
527 val = INTVAL (offset);
528 return (val & 3) == 0 && (val >= 0 && val <= 60);
529 }
530 }
531 return FALSE;
532 }
533
534
535 static bool
536 constantpool_address_p (const_rtx addr)
537 {
538 const_rtx sym = addr;
539
540 if (GET_CODE (addr) == CONST)
541 {
542 rtx offset;
543
544 /* Only handle (PLUS (SYM, OFFSET)) form. */
545 addr = XEXP (addr, 0);
546 if (GET_CODE (addr) != PLUS)
547 return false;
548
549 /* Make sure the address is word aligned. */
550 offset = XEXP (addr, 1);
551 if ((!CONST_INT_P (offset))
552 || ((INTVAL (offset) & 3) != 0))
553 return false;
554
555 sym = XEXP (addr, 0);
556 }
557
558 if ((GET_CODE (sym) == SYMBOL_REF)
559 && CONSTANT_POOL_ADDRESS_P (sym))
560 return true;
561 return false;
562 }
563
564
565 int
566 constantpool_mem_p (rtx op)
567 {
568 if (GET_CODE (op) == SUBREG)
569 op = SUBREG_REG (op);
570 if (GET_CODE (op) == MEM)
571 return constantpool_address_p (XEXP (op, 0));
572 return FALSE;
573 }
574
575
576 /* Return TRUE if X is a thread-local symbol. */
577
578 static bool
579 xtensa_tls_symbol_p (rtx x)
580 {
581 if (! TARGET_HAVE_TLS)
582 return false;
583
584 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
585 }
586
587
588 void
589 xtensa_extend_reg (rtx dst, rtx src)
590 {
591 rtx temp = gen_reg_rtx (SImode);
592 rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
593
594 /* Generate paradoxical subregs as needed so that the modes match. */
595 src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
596 dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
597
598 emit_insn (gen_ashlsi3 (temp, src, shift));
599 emit_insn (gen_ashrsi3 (dst, temp, shift));
600 }
601
602
603 bool
604 xtensa_mem_offset (unsigned v, machine_mode mode)
605 {
606 switch (mode)
607 {
608 case BLKmode:
609 /* Handle the worst case for block moves. See xtensa_expand_block_move
610 where we emit an optimized block move operation if the block can be
611 moved in < "move_ratio" pieces. The worst case is when the block is
612 aligned but has a size of (3 mod 4) (does this happen?) so that the
613 last piece requires a byte load/store. */
614 return (xtensa_uimm8 (v)
615 && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
616
617 case QImode:
618 return xtensa_uimm8 (v);
619
620 case HImode:
621 return xtensa_uimm8x2 (v);
622
623 case DFmode:
624 return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
625
626 default:
627 break;
628 }
629
630 return xtensa_uimm8x4 (v);
631 }
632
633
634 /* Make normal rtx_code into something we can index from an array. */
635
636 static enum internal_test
637 map_test_to_internal_test (enum rtx_code test_code)
638 {
639 enum internal_test test = ITEST_MAX;
640
641 switch (test_code)
642 {
643 default: break;
644 case EQ: test = ITEST_EQ; break;
645 case NE: test = ITEST_NE; break;
646 case GT: test = ITEST_GT; break;
647 case GE: test = ITEST_GE; break;
648 case LT: test = ITEST_LT; break;
649 case LE: test = ITEST_LE; break;
650 case GTU: test = ITEST_GTU; break;
651 case GEU: test = ITEST_GEU; break;
652 case LTU: test = ITEST_LTU; break;
653 case LEU: test = ITEST_LEU; break;
654 }
655
656 return test;
657 }
658
659
660 /* Generate the code to compare two integer values. The return value is
661 the comparison expression. */
662
663 static rtx
664 gen_int_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
665 rtx cmp0, /* first operand to compare */
666 rtx cmp1, /* second operand to compare */
667 int *p_invert /* whether branch needs to reverse test */)
668 {
669 struct cmp_info
670 {
671 enum rtx_code test_code; /* test code to use in insn */
672 bool (*const_range_p) (HOST_WIDE_INT); /* range check function */
673 int const_add; /* constant to add (convert LE -> LT) */
674 int reverse_regs; /* reverse registers in test */
675 int invert_const; /* != 0 if invert value if cmp1 is constant */
676 int invert_reg; /* != 0 if invert value if cmp1 is register */
677 int unsignedp; /* != 0 for unsigned comparisons. */
678 };
679
680 static struct cmp_info info[ (int)ITEST_MAX ] = {
681
682 { EQ, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* EQ */
683 { NE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* NE */
684
685 { LT, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* GT */
686 { GE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* GE */
687 { LT, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* LT */
688 { GE, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* LE */
689
690 { LTU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* GTU */
691 { GEU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* GEU */
692 { LTU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* LTU */
693 { GEU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* LEU */
694 };
695
696 enum internal_test test;
697 machine_mode mode;
698 struct cmp_info *p_info;
699
700 test = map_test_to_internal_test (test_code);
701 gcc_assert (test != ITEST_MAX);
702
703 p_info = &info[ (int)test ];
704
705 mode = GET_MODE (cmp0);
706 if (mode == VOIDmode)
707 mode = GET_MODE (cmp1);
708
709 /* Make sure we can handle any constants given to us. */
710 if (GET_CODE (cmp1) == CONST_INT)
711 {
712 HOST_WIDE_INT value = INTVAL (cmp1);
713 unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
714
715 /* if the immediate overflows or does not fit in the immediate field,
716 spill it to a register */
717
718 if ((p_info->unsignedp ?
719 (uvalue + p_info->const_add > uvalue) :
720 (value + p_info->const_add > value)) != (p_info->const_add > 0))
721 {
722 cmp1 = force_reg (mode, cmp1);
723 }
724 else if (!(p_info->const_range_p) (value + p_info->const_add))
725 {
726 cmp1 = force_reg (mode, cmp1);
727 }
728 }
729 else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
730 {
731 cmp1 = force_reg (mode, cmp1);
732 }
733
734 /* See if we need to invert the result. */
735 *p_invert = ((GET_CODE (cmp1) == CONST_INT)
736 ? p_info->invert_const
737 : p_info->invert_reg);
738
739 /* Comparison to constants, may involve adding 1 to change a LT into LE.
740 Comparison between two registers, may involve switching operands. */
741 if (GET_CODE (cmp1) == CONST_INT)
742 {
743 if (p_info->const_add != 0)
744 cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
745
746 }
747 else if (p_info->reverse_regs)
748 {
749 rtx temp = cmp0;
750 cmp0 = cmp1;
751 cmp1 = temp;
752 }
753
754 return gen_rtx_fmt_ee (p_info->test_code, VOIDmode, cmp0, cmp1);
755 }
756
757
758 /* Generate the code to compare two float values. The return value is
759 the comparison expression. */
760
761 static rtx
762 gen_float_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
763 rtx cmp0, /* first operand to compare */
764 rtx cmp1 /* second operand to compare */)
765 {
766 rtx (*gen_fn) (rtx, rtx, rtx);
767 rtx brtmp;
768 int reverse_regs, invert;
769
770 switch (test_code)
771 {
772 case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
773 case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
774 case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
775 case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
776 case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
777 case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
778 case UNEQ: reverse_regs = 0; invert = 0; gen_fn = gen_suneq_sf; break;
779 case LTGT: reverse_regs = 0; invert = 1; gen_fn = gen_suneq_sf; break;
780 case UNLE: reverse_regs = 0; invert = 0; gen_fn = gen_sunle_sf; break;
781 case UNGT: reverse_regs = 1; invert = 0; gen_fn = gen_sunlt_sf; break;
782 case UNLT: reverse_regs = 0; invert = 0; gen_fn = gen_sunlt_sf; break;
783 case UNGE: reverse_regs = 1; invert = 0; gen_fn = gen_sunle_sf; break;
784 case UNORDERED:
785 reverse_regs = 0; invert = 0; gen_fn = gen_sunordered_sf; break;
786 case ORDERED:
787 reverse_regs = 0; invert = 1; gen_fn = gen_sunordered_sf; break;
788 default:
789 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
790 reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
791 }
792
793 if (reverse_regs)
794 {
795 rtx temp = cmp0;
796 cmp0 = cmp1;
797 cmp1 = temp;
798 }
799
800 brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
801 emit_insn (gen_fn (brtmp, cmp0, cmp1));
802
803 return gen_rtx_fmt_ee (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
804 }
805
806
807 void
808 xtensa_expand_conditional_branch (rtx *operands, machine_mode mode)
809 {
810 enum rtx_code test_code = GET_CODE (operands[0]);
811 rtx cmp0 = operands[1];
812 rtx cmp1 = operands[2];
813 rtx cmp;
814 int invert;
815 rtx label1, label2;
816
817 switch (mode)
818 {
819 case DFmode:
820 default:
821 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
822
823 case SImode:
824 invert = FALSE;
825 cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
826 break;
827
828 case SFmode:
829 if (!TARGET_HARD_FLOAT)
830 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode,
831 cmp0, cmp1));
832 invert = FALSE;
833 cmp = gen_float_relational (test_code, cmp0, cmp1);
834 break;
835 }
836
837 /* Generate the branch. */
838
839 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
840 label2 = pc_rtx;
841
842 if (invert)
843 {
844 label2 = label1;
845 label1 = pc_rtx;
846 }
847
848 emit_jump_insn (gen_rtx_SET (pc_rtx,
849 gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
850 label1,
851 label2)));
852 }
853
854
855 static rtx
856 gen_conditional_move (enum rtx_code code, machine_mode mode,
857 rtx op0, rtx op1)
858 {
859 if (mode == SImode)
860 {
861 rtx cmp;
862
863 /* Jump optimization calls get_condition() which canonicalizes
864 comparisons like (GE x <const>) to (GT x <const-1>).
865 Transform those comparisons back to GE, since that is the
866 comparison supported in Xtensa. We shouldn't have to
867 transform <LE x const> comparisons, because neither
868 xtensa_expand_conditional_branch() nor get_condition() will
869 produce them. */
870
871 if ((code == GT) && (op1 == constm1_rtx))
872 {
873 code = GE;
874 op1 = const0_rtx;
875 }
876 cmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
877
878 if (boolean_operator (cmp, VOIDmode))
879 {
880 /* Swap the operands to make const0 second. */
881 if (op0 == const0_rtx)
882 {
883 op0 = op1;
884 op1 = const0_rtx;
885 }
886
887 /* If not comparing against zero, emit a comparison (subtract). */
888 if (op1 != const0_rtx)
889 {
890 op0 = expand_binop (SImode, sub_optab, op0, op1,
891 0, 0, OPTAB_LIB_WIDEN);
892 op1 = const0_rtx;
893 }
894 }
895 else if (branch_operator (cmp, VOIDmode))
896 {
897 /* Swap the operands to make const0 second. */
898 if (op0 == const0_rtx)
899 {
900 op0 = op1;
901 op1 = const0_rtx;
902
903 switch (code)
904 {
905 case LT: code = GE; break;
906 case GE: code = LT; break;
907 default: gcc_unreachable ();
908 }
909 }
910
911 if (op1 != const0_rtx)
912 return 0;
913 }
914 else
915 return 0;
916
917 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
918 }
919
920 if (TARGET_HARD_FLOAT && mode == SFmode)
921 return gen_float_relational (code, op0, op1);
922
923 return 0;
924 }
925
926
927 int
928 xtensa_expand_conditional_move (rtx *operands, int isflt)
929 {
930 rtx dest = operands[0];
931 rtx cmp = operands[1];
932 machine_mode cmp_mode = GET_MODE (XEXP (cmp, 0));
933 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
934
935 if (!(cmp = gen_conditional_move (GET_CODE (cmp), cmp_mode,
936 XEXP (cmp, 0), XEXP (cmp, 1))))
937 return 0;
938
939 if (isflt)
940 gen_fn = (cmp_mode == SImode
941 ? gen_movsfcc_internal0
942 : gen_movsfcc_internal1);
943 else
944 gen_fn = (cmp_mode == SImode
945 ? gen_movsicc_internal0
946 : gen_movsicc_internal1);
947
948 emit_insn (gen_fn (dest, XEXP (cmp, 0), operands[2], operands[3], cmp));
949 return 1;
950 }
951
952
953 int
954 xtensa_expand_scc (rtx operands[4], machine_mode cmp_mode)
955 {
956 rtx dest = operands[0];
957 rtx cmp;
958 rtx one_tmp, zero_tmp;
959 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
960
961 if (!(cmp = gen_conditional_move (GET_CODE (operands[1]), cmp_mode,
962 operands[2], operands[3])))
963 return 0;
964
965 one_tmp = gen_reg_rtx (SImode);
966 zero_tmp = gen_reg_rtx (SImode);
967 emit_insn (gen_movsi (one_tmp, const_true_rtx));
968 emit_insn (gen_movsi (zero_tmp, const0_rtx));
969
970 gen_fn = (cmp_mode == SImode
971 ? gen_movsicc_internal0
972 : gen_movsicc_internal1);
973 emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
974 return 1;
975 }
976
977
978 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1]. MODE is
979 for the output, i.e., the input operands are twice as big as MODE. */
980
981 void
982 xtensa_split_operand_pair (rtx operands[4], machine_mode mode)
983 {
984 switch (GET_CODE (operands[1]))
985 {
986 case REG:
987 operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
988 operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
989 break;
990
991 case MEM:
992 operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
993 operands[2] = adjust_address (operands[1], mode, 0);
994 break;
995
996 case CONST_INT:
997 case CONST_DOUBLE:
998 split_double (operands[1], &operands[2], &operands[3]);
999 break;
1000
1001 default:
1002 gcc_unreachable ();
1003 }
1004
1005 switch (GET_CODE (operands[0]))
1006 {
1007 case REG:
1008 operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
1009 operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
1010 break;
1011
1012 case MEM:
1013 operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
1014 operands[0] = adjust_address (operands[0], mode, 0);
1015 break;
1016
1017 default:
1018 gcc_unreachable ();
1019 }
1020 }
1021
1022
1023 /* Emit insns to move operands[1] into operands[0].
1024 Return 1 if we have written out everything that needs to be done to
1025 do the move. Otherwise, return 0 and the caller will emit the move
1026 normally. */
1027
1028 int
1029 xtensa_emit_move_sequence (rtx *operands, machine_mode mode)
1030 {
1031 rtx src = operands[1];
1032
1033 if (CONSTANT_P (src)
1034 && (GET_CODE (src) != CONST_INT || ! xtensa_simm12b (INTVAL (src))))
1035 {
1036 rtx dst = operands[0];
1037
1038 if (xtensa_tls_referenced_p (src))
1039 {
1040 rtx addend = NULL;
1041
1042 if (GET_CODE (src) == CONST && GET_CODE (XEXP (src, 0)) == PLUS)
1043 {
1044 addend = XEXP (XEXP (src, 0), 1);
1045 src = XEXP (XEXP (src, 0), 0);
1046 }
1047
1048 src = xtensa_legitimize_tls_address (src);
1049 if (addend)
1050 {
1051 src = gen_rtx_PLUS (mode, src, addend);
1052 src = force_operand (src, dst);
1053 }
1054 emit_move_insn (dst, src);
1055 return 1;
1056 }
1057
1058 if (! TARGET_CONST16)
1059 {
1060 src = force_const_mem (SImode, src);
1061 operands[1] = src;
1062 }
1063
1064 /* PC-relative loads are always SImode, and CONST16 is only
1065 supported in the movsi pattern, so add a SUBREG for any other
1066 (smaller) mode. */
1067
1068 if (mode != SImode)
1069 {
1070 if (register_operand (dst, mode))
1071 {
1072 emit_move_insn (simplify_gen_subreg (SImode, dst, mode, 0), src);
1073 return 1;
1074 }
1075 else
1076 {
1077 src = force_reg (SImode, src);
1078 src = gen_lowpart_SUBREG (mode, src);
1079 operands[1] = src;
1080 }
1081 }
1082 }
1083
1084 if (!(reload_in_progress | reload_completed)
1085 && !xtensa_valid_move (mode, operands))
1086 operands[1] = force_reg (mode, operands[1]);
1087
1088 operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1089
1090 /* During reload we don't want to emit (subreg:X (mem:Y)) since that
1091 instruction won't be recognized after reload, so we remove the
1092 subreg and adjust mem accordingly. */
1093 if (reload_in_progress)
1094 {
1095 operands[0] = fixup_subreg_mem (operands[0]);
1096 operands[1] = fixup_subreg_mem (operands[1]);
1097 }
1098 return 0;
1099 }
1100
1101
1102 static rtx
1103 fixup_subreg_mem (rtx x)
1104 {
1105 if (GET_CODE (x) == SUBREG
1106 && GET_CODE (SUBREG_REG (x)) == REG
1107 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1108 {
1109 rtx temp =
1110 gen_rtx_SUBREG (GET_MODE (x),
1111 reg_equiv_mem (REGNO (SUBREG_REG (x))),
1112 SUBREG_BYTE (x));
1113 x = alter_subreg (&temp, true);
1114 }
1115 return x;
1116 }
1117
1118
1119 /* Check if an incoming argument in a7 is expected to be used soon and
1120 if OPND is a register or register pair that includes a7. If so,
1121 create a new pseudo and copy a7 into that pseudo at the very
1122 beginning of the function, followed by the special "set_frame_ptr"
1123 unspec_volatile insn. The return value is either the original
1124 operand, if it is not a7, or the new pseudo containing a copy of
1125 the incoming argument. This is necessary because the register
1126 allocator will ignore conflicts with a7 and may either assign some
1127 other pseudo to a7 or use a7 as the hard_frame_pointer, clobbering
1128 the incoming argument in a7. By copying the argument out of a7 as
1129 the very first thing, and then immediately following that with an
1130 unspec_volatile to keep the scheduler away, we should avoid any
1131 problems. Putting the set_frame_ptr insn at the beginning, with
1132 only the a7 copy before it, also makes it easier for the prologue
1133 expander to initialize the frame pointer after the a7 copy and to
1134 fix up the a7 copy to use the stack pointer instead of the frame
1135 pointer. */
1136
1137 rtx
1138 xtensa_copy_incoming_a7 (rtx opnd)
1139 {
1140 rtx entry_insns = 0;
1141 rtx reg, tmp;
1142 machine_mode mode;
1143
1144 if (!cfun->machine->need_a7_copy)
1145 return opnd;
1146
1147 /* This function should never be called again once a7 has been copied. */
1148 gcc_assert (!cfun->machine->set_frame_ptr_insn);
1149
1150 mode = GET_MODE (opnd);
1151
1152 /* The operand using a7 may come in a later instruction, so just return
1153 the original operand if it doesn't use a7. */
1154 reg = opnd;
1155 if (GET_CODE (reg) == SUBREG)
1156 {
1157 gcc_assert (SUBREG_BYTE (reg) == 0);
1158 reg = SUBREG_REG (reg);
1159 }
1160 if (GET_CODE (reg) != REG
1161 || REGNO (reg) > A7_REG
1162 || REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) <= A7_REG)
1163 return opnd;
1164
1165 /* 1-word args will always be in a7; 2-word args in a6/a7. */
1166 gcc_assert (REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) - 1 == A7_REG);
1167
1168 cfun->machine->need_a7_copy = false;
1169
1170 /* Copy a7 to a new pseudo at the function entry. Use gen_raw_REG to
1171 create the REG for a7 so that hard_frame_pointer_rtx is not used. */
1172
1173 start_sequence ();
1174 tmp = gen_reg_rtx (mode);
1175
1176 switch (mode)
1177 {
1178 case DFmode:
1179 case DImode:
1180 /* Copy the value out of A7 here but keep the first word in A6 until
1181 after the set_frame_ptr insn. Otherwise, the register allocator
1182 may decide to put "subreg (tmp, 0)" in A7 and clobber the incoming
1183 value. */
1184 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 4),
1185 gen_raw_REG (SImode, A7_REG)));
1186 break;
1187 case SFmode:
1188 emit_insn (gen_movsf_internal (tmp, gen_raw_REG (mode, A7_REG)));
1189 break;
1190 case SImode:
1191 emit_insn (gen_movsi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1192 break;
1193 case HImode:
1194 emit_insn (gen_movhi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1195 break;
1196 case QImode:
1197 emit_insn (gen_movqi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1198 break;
1199 default:
1200 gcc_unreachable ();
1201 }
1202
1203 cfun->machine->set_frame_ptr_insn = emit_insn (gen_set_frame_ptr ());
1204
1205 /* For DF and DI mode arguments, copy the incoming value in A6 now. */
1206 if (mode == DFmode || mode == DImode)
1207 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 0),
1208 gen_rtx_REG (SImode, A7_REG - 1)));
1209 entry_insns = get_insns ();
1210 end_sequence ();
1211
1212 if (cfun->machine->vararg_a7)
1213 {
1214 /* This is called from within builtin_saveregs, which will insert the
1215 saveregs code at the function entry, ahead of anything placed at
1216 the function entry now. Instead, save the sequence to be inserted
1217 at the beginning of the saveregs code. */
1218 cfun->machine->vararg_a7_copy = entry_insns;
1219 }
1220 else
1221 {
1222 /* Put entry_insns after the NOTE that starts the function. If
1223 this is inside a start_sequence, make the outer-level insn
1224 chain current, so the code is placed at the start of the
1225 function. */
1226 push_topmost_sequence ();
1227 /* Do not use entry_of_function() here. This is called from within
1228 expand_function_start, when the CFG still holds GIMPLE. */
1229 emit_insn_after (entry_insns, get_insns ());
1230 pop_topmost_sequence ();
1231 }
1232
1233 return tmp;
1234 }
1235
1236
1237 /* Try to expand a block move operation to a sequence of RTL move
1238 instructions. If not optimizing, or if the block size is not a
1239 constant, or if the block is too large, the expansion fails and GCC
1240 falls back to calling memcpy().
1241
1242 operands[0] is the destination
1243 operands[1] is the source
1244 operands[2] is the length
1245 operands[3] is the alignment */
1246
1247 int
1248 xtensa_expand_block_move (rtx *operands)
1249 {
1250 static const machine_mode mode_from_align[] =
1251 {
1252 VOIDmode, QImode, HImode, VOIDmode, SImode,
1253 };
1254
1255 rtx dst_mem = operands[0];
1256 rtx src_mem = operands[1];
1257 HOST_WIDE_INT bytes, align;
1258 int num_pieces, move_ratio;
1259 rtx temp[2];
1260 machine_mode mode[2];
1261 int amount[2];
1262 bool active[2];
1263 int phase = 0;
1264 int next;
1265 int offset_ld = 0;
1266 int offset_st = 0;
1267 rtx x;
1268
1269 /* If this is not a fixed size move, just call memcpy. */
1270 if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
1271 return 0;
1272
1273 bytes = INTVAL (operands[2]);
1274 align = INTVAL (operands[3]);
1275
1276 /* Anything to move? */
1277 if (bytes <= 0)
1278 return 0;
1279
1280 if (align > MOVE_MAX)
1281 align = MOVE_MAX;
1282
1283 /* Decide whether to expand inline based on the optimization level. */
1284 move_ratio = 4;
1285 if (optimize > 2)
1286 move_ratio = LARGEST_MOVE_RATIO;
1287 num_pieces = (bytes / align) + (bytes % align); /* Close enough anyway. */
1288 if (num_pieces > move_ratio)
1289 return 0;
1290
1291 x = XEXP (dst_mem, 0);
1292 if (!REG_P (x))
1293 {
1294 x = force_reg (Pmode, x);
1295 dst_mem = replace_equiv_address (dst_mem, x);
1296 }
1297
1298 x = XEXP (src_mem, 0);
1299 if (!REG_P (x))
1300 {
1301 x = force_reg (Pmode, x);
1302 src_mem = replace_equiv_address (src_mem, x);
1303 }
1304
1305 active[0] = active[1] = false;
1306
1307 do
1308 {
1309 next = phase;
1310 phase ^= 1;
1311
1312 if (bytes > 0)
1313 {
1314 int next_amount;
1315
1316 next_amount = (bytes >= 4 ? 4 : (bytes >= 2 ? 2 : 1));
1317 next_amount = MIN (next_amount, align);
1318
1319 amount[next] = next_amount;
1320 mode[next] = mode_from_align[next_amount];
1321 temp[next] = gen_reg_rtx (mode[next]);
1322
1323 x = adjust_address (src_mem, mode[next], offset_ld);
1324 emit_insn (gen_rtx_SET (temp[next], x));
1325
1326 offset_ld += next_amount;
1327 bytes -= next_amount;
1328 active[next] = true;
1329 }
1330
1331 if (active[phase])
1332 {
1333 active[phase] = false;
1334
1335 x = adjust_address (dst_mem, mode[phase], offset_st);
1336 emit_insn (gen_rtx_SET (x, temp[phase]));
1337
1338 offset_st += amount[phase];
1339 }
1340 }
1341 while (active[next]);
1342
1343 return 1;
1344 }
1345
1346
1347 void
1348 xtensa_expand_nonlocal_goto (rtx *operands)
1349 {
1350 rtx goto_handler = operands[1];
1351 rtx containing_fp = operands[3];
1352
1353 /* Generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1354 is too big to generate in-line. */
1355
1356 if (GET_CODE (containing_fp) != REG)
1357 containing_fp = force_reg (Pmode, containing_fp);
1358
1359 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1360 LCT_NORMAL, VOIDmode, 2,
1361 containing_fp, Pmode,
1362 goto_handler, Pmode);
1363 }
1364
1365
1366 static struct machine_function *
1367 xtensa_init_machine_status (void)
1368 {
1369 return ggc_cleared_alloc<machine_function> ();
1370 }
1371
1372
1373 /* Shift VAL of mode MODE left by COUNT bits. */
1374
1375 static inline rtx
1376 xtensa_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
1377 {
1378 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
1379 NULL_RTX, 1, OPTAB_DIRECT);
1380 return expand_simple_binop (SImode, ASHIFT, val, count,
1381 NULL_RTX, 1, OPTAB_DIRECT);
1382 }
1383
1384
1385 /* Structure to hold the initial parameters for a compare_and_swap operation
1386 in HImode and QImode. */
1387
1388 struct alignment_context
1389 {
1390 rtx memsi; /* SI aligned memory location. */
1391 rtx shift; /* Bit offset with regard to lsb. */
1392 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
1393 rtx modemaski; /* ~modemask */
1394 };
1395
1396
1397 /* Initialize structure AC for word access to HI and QI mode memory. */
1398
1399 static void
1400 init_alignment_context (struct alignment_context *ac, rtx mem)
1401 {
1402 machine_mode mode = GET_MODE (mem);
1403 rtx byteoffset = NULL_RTX;
1404 bool aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
1405
1406 if (aligned)
1407 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
1408 else
1409 {
1410 /* Alignment is unknown. */
1411 rtx addr, align;
1412
1413 /* Force the address into a register. */
1414 addr = force_reg (Pmode, XEXP (mem, 0));
1415
1416 /* Align it to SImode. */
1417 align = expand_simple_binop (Pmode, AND, addr,
1418 GEN_INT (-GET_MODE_SIZE (SImode)),
1419 NULL_RTX, 1, OPTAB_DIRECT);
1420 /* Generate MEM. */
1421 ac->memsi = gen_rtx_MEM (SImode, align);
1422 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
1423 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
1424 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
1425
1426 byteoffset = expand_simple_binop (Pmode, AND, addr,
1427 GEN_INT (GET_MODE_SIZE (SImode) - 1),
1428 NULL_RTX, 1, OPTAB_DIRECT);
1429 }
1430
1431 /* Calculate shiftcount. */
1432 if (TARGET_BIG_ENDIAN)
1433 {
1434 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
1435 if (!aligned)
1436 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
1437 NULL_RTX, 1, OPTAB_DIRECT);
1438 }
1439 else
1440 {
1441 if (aligned)
1442 ac->shift = NULL_RTX;
1443 else
1444 ac->shift = byteoffset;
1445 }
1446
1447 if (ac->shift != NULL_RTX)
1448 {
1449 /* Shift is the byte count, but we need the bitcount. */
1450 gcc_assert (exact_log2 (BITS_PER_UNIT) >= 0);
1451 ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift,
1452 GEN_INT (exact_log2 (BITS_PER_UNIT)),
1453 NULL_RTX, 1, OPTAB_DIRECT);
1454 ac->modemask = expand_simple_binop (SImode, ASHIFT,
1455 GEN_INT (GET_MODE_MASK (mode)),
1456 ac->shift,
1457 NULL_RTX, 1, OPTAB_DIRECT);
1458 }
1459 else
1460 ac->modemask = GEN_INT (GET_MODE_MASK (mode));
1461
1462 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask, NULL_RTX, 1);
1463 }
1464
1465
1466 /* Expand an atomic compare and swap operation for HImode and QImode.
1467 MEM is the memory location, CMP the old value to compare MEM with
1468 and NEW_RTX the value to set if CMP == MEM. */
1469
1470 void
1471 xtensa_expand_compare_and_swap (rtx target, rtx mem, rtx cmp, rtx new_rtx)
1472 {
1473 machine_mode mode = GET_MODE (mem);
1474 struct alignment_context ac;
1475 rtx tmp, cmpv, newv, val;
1476 rtx oldval = gen_reg_rtx (SImode);
1477 rtx res = gen_reg_rtx (SImode);
1478 rtx_code_label *csloop = gen_label_rtx ();
1479 rtx_code_label *csend = gen_label_rtx ();
1480
1481 init_alignment_context (&ac, mem);
1482
1483 if (ac.shift != NULL_RTX)
1484 {
1485 cmp = xtensa_expand_mask_and_shift (cmp, mode, ac.shift);
1486 new_rtx = xtensa_expand_mask_and_shift (new_rtx, mode, ac.shift);
1487 }
1488
1489 /* Load the surrounding word into VAL with the MEM value masked out. */
1490 val = force_reg (SImode, expand_simple_binop (SImode, AND, ac.memsi,
1491 ac.modemaski, NULL_RTX, 1,
1492 OPTAB_DIRECT));
1493 emit_label (csloop);
1494
1495 /* Patch CMP and NEW_RTX into VAL at correct position. */
1496 cmpv = force_reg (SImode, expand_simple_binop (SImode, IOR, cmp, val,
1497 NULL_RTX, 1, OPTAB_DIRECT));
1498 newv = force_reg (SImode, expand_simple_binop (SImode, IOR, new_rtx, val,
1499 NULL_RTX, 1, OPTAB_DIRECT));
1500
1501 /* Jump to end if we're done. */
1502 emit_insn (gen_sync_compare_and_swapsi (res, ac.memsi, cmpv, newv));
1503 emit_cmp_and_jump_insns (res, cmpv, EQ, const0_rtx, SImode, true, csend);
1504
1505 /* Check for changes outside mode. */
1506 emit_move_insn (oldval, val);
1507 tmp = expand_simple_binop (SImode, AND, res, ac.modemaski,
1508 val, 1, OPTAB_DIRECT);
1509 if (tmp != val)
1510 emit_move_insn (val, tmp);
1511
1512 /* Loop internal if so. */
1513 emit_cmp_and_jump_insns (oldval, val, NE, const0_rtx, SImode, true, csloop);
1514
1515 emit_label (csend);
1516
1517 /* Return the correct part of the bitfield. */
1518 convert_move (target,
1519 (ac.shift == NULL_RTX ? res
1520 : expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
1521 NULL_RTX, 1, OPTAB_DIRECT)),
1522 1);
1523 }
1524
1525
1526 /* Expand an atomic operation CODE of mode MODE (either HImode or QImode --
1527 the default expansion works fine for SImode). MEM is the memory location
1528 and VAL the value to play with. If AFTER is true then store the value
1529 MEM holds after the operation, if AFTER is false then store the value MEM
1530 holds before the operation. If TARGET is zero then discard that value, else
1531 store it to TARGET. */
1532
1533 void
1534 xtensa_expand_atomic (enum rtx_code code, rtx target, rtx mem, rtx val,
1535 bool after)
1536 {
1537 machine_mode mode = GET_MODE (mem);
1538 struct alignment_context ac;
1539 rtx_code_label *csloop = gen_label_rtx ();
1540 rtx cmp, tmp;
1541 rtx old = gen_reg_rtx (SImode);
1542 rtx new_rtx = gen_reg_rtx (SImode);
1543 rtx orig = NULL_RTX;
1544
1545 init_alignment_context (&ac, mem);
1546
1547 /* Prepare values before the compare-and-swap loop. */
1548 if (ac.shift != NULL_RTX)
1549 val = xtensa_expand_mask_and_shift (val, mode, ac.shift);
1550 switch (code)
1551 {
1552 case PLUS:
1553 case MINUS:
1554 orig = gen_reg_rtx (SImode);
1555 convert_move (orig, val, 1);
1556 break;
1557
1558 case SET:
1559 case IOR:
1560 case XOR:
1561 break;
1562
1563 case MULT: /* NAND */
1564 case AND:
1565 /* val = "11..1<val>11..1" */
1566 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
1567 NULL_RTX, 1, OPTAB_DIRECT);
1568 break;
1569
1570 default:
1571 gcc_unreachable ();
1572 }
1573
1574 /* Load full word. Subsequent loads are performed by S32C1I. */
1575 cmp = force_reg (SImode, ac.memsi);
1576
1577 emit_label (csloop);
1578 emit_move_insn (old, cmp);
1579
1580 switch (code)
1581 {
1582 case PLUS:
1583 case MINUS:
1584 val = expand_simple_binop (SImode, code, old, orig,
1585 NULL_RTX, 1, OPTAB_DIRECT);
1586 val = expand_simple_binop (SImode, AND, val, ac.modemask,
1587 NULL_RTX, 1, OPTAB_DIRECT);
1588 /* FALLTHRU */
1589 case SET:
1590 tmp = expand_simple_binop (SImode, AND, old, ac.modemaski,
1591 NULL_RTX, 1, OPTAB_DIRECT);
1592 tmp = expand_simple_binop (SImode, IOR, tmp, val,
1593 new_rtx, 1, OPTAB_DIRECT);
1594 break;
1595
1596 case AND:
1597 case IOR:
1598 case XOR:
1599 tmp = expand_simple_binop (SImode, code, old, val,
1600 new_rtx, 1, OPTAB_DIRECT);
1601 break;
1602
1603 case MULT: /* NAND */
1604 tmp = expand_simple_binop (SImode, XOR, old, ac.modemask,
1605 NULL_RTX, 1, OPTAB_DIRECT);
1606 tmp = expand_simple_binop (SImode, AND, tmp, val,
1607 new_rtx, 1, OPTAB_DIRECT);
1608 break;
1609
1610 default:
1611 gcc_unreachable ();
1612 }
1613
1614 if (tmp != new_rtx)
1615 emit_move_insn (new_rtx, tmp);
1616 emit_insn (gen_sync_compare_and_swapsi (cmp, ac.memsi, old, new_rtx));
1617 emit_cmp_and_jump_insns (cmp, old, NE, const0_rtx, SImode, true, csloop);
1618
1619 if (target)
1620 {
1621 tmp = (after ? new_rtx : cmp);
1622 convert_move (target,
1623 (ac.shift == NULL_RTX ? tmp
1624 : expand_simple_binop (SImode, LSHIFTRT, tmp, ac.shift,
1625 NULL_RTX, 1, OPTAB_DIRECT)),
1626 1);
1627 }
1628 }
1629
1630
1631 void
1632 xtensa_setup_frame_addresses (void)
1633 {
1634 /* Set flag to cause TARGET_FRAME_POINTER_REQUIRED to return true. */
1635 cfun->machine->accesses_prev_frame = 1;
1636
1637 if (TARGET_WINDOWED_ABI)
1638 emit_library_call
1639 (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
1640 LCT_NORMAL, VOIDmode, 0);
1641 }
1642
1643
1644 /* Emit the assembly for the end of a zero-cost loop. Normally we just emit
1645 a comment showing where the end of the loop is. However, if there is a
1646 label or a branch at the end of the loop then we need to place a nop
1647 there. If the loop ends with a label we need the nop so that branches
1648 targeting that label will target the nop (and thus remain in the loop),
1649 instead of targeting the instruction after the loop (and thus exiting
1650 the loop). If the loop ends with a branch, we need the nop in case the
1651 branch is targeting a location inside the loop. When the branch
1652 executes it will cause the loop count to be decremented even if it is
1653 taken (because it is the last instruction in the loop), so we need to
1654 nop after the branch to prevent the loop count from being decremented
1655 when the branch is taken. */
1656
1657 void
1658 xtensa_emit_loop_end (rtx_insn *insn, rtx *operands)
1659 {
1660 char done = 0;
1661
1662 for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
1663 {
1664 switch (GET_CODE (insn))
1665 {
1666 case NOTE:
1667 case BARRIER:
1668 break;
1669
1670 case CODE_LABEL:
1671 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1672 done = 1;
1673 break;
1674
1675 default:
1676 {
1677 rtx body = PATTERN (insn);
1678
1679 if (JUMP_P (body))
1680 {
1681 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1682 done = 1;
1683 }
1684 else if ((GET_CODE (body) != USE)
1685 && (GET_CODE (body) != CLOBBER))
1686 done = 1;
1687 }
1688 break;
1689 }
1690 }
1691
1692 output_asm_insn ("%1_LEND:", operands);
1693 }
1694
1695
1696 char *
1697 xtensa_emit_branch (bool inverted, bool immed, rtx *operands)
1698 {
1699 static char result[64];
1700 enum rtx_code code;
1701 const char *op;
1702
1703 code = GET_CODE (operands[3]);
1704 switch (code)
1705 {
1706 case EQ: op = inverted ? "ne" : "eq"; break;
1707 case NE: op = inverted ? "eq" : "ne"; break;
1708 case LT: op = inverted ? "ge" : "lt"; break;
1709 case GE: op = inverted ? "lt" : "ge"; break;
1710 case LTU: op = inverted ? "geu" : "ltu"; break;
1711 case GEU: op = inverted ? "ltu" : "geu"; break;
1712 default: gcc_unreachable ();
1713 }
1714
1715 if (immed)
1716 {
1717 if (INTVAL (operands[1]) == 0)
1718 sprintf (result, "b%sz%s\t%%0, %%2", op,
1719 (TARGET_DENSITY && (code == EQ || code == NE)) ? ".n" : "");
1720 else
1721 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1722 }
1723 else
1724 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1725
1726 return result;
1727 }
1728
1729
1730 char *
1731 xtensa_emit_bit_branch (bool inverted, bool immed, rtx *operands)
1732 {
1733 static char result[64];
1734 const char *op;
1735
1736 switch (GET_CODE (operands[3]))
1737 {
1738 case EQ: op = inverted ? "bs" : "bc"; break;
1739 case NE: op = inverted ? "bc" : "bs"; break;
1740 default: gcc_unreachable ();
1741 }
1742
1743 if (immed)
1744 {
1745 unsigned bitnum = INTVAL (operands[1]) & 0x1f;
1746 operands[1] = GEN_INT (bitnum);
1747 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1748 }
1749 else
1750 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1751
1752 return result;
1753 }
1754
1755
1756 char *
1757 xtensa_emit_movcc (bool inverted, bool isfp, bool isbool, rtx *operands)
1758 {
1759 static char result[64];
1760 enum rtx_code code;
1761 const char *op;
1762
1763 code = GET_CODE (operands[4]);
1764 if (isbool)
1765 {
1766 switch (code)
1767 {
1768 case EQ: op = inverted ? "t" : "f"; break;
1769 case NE: op = inverted ? "f" : "t"; break;
1770 default: gcc_unreachable ();
1771 }
1772 }
1773 else
1774 {
1775 switch (code)
1776 {
1777 case EQ: op = inverted ? "nez" : "eqz"; break;
1778 case NE: op = inverted ? "eqz" : "nez"; break;
1779 case LT: op = inverted ? "gez" : "ltz"; break;
1780 case GE: op = inverted ? "ltz" : "gez"; break;
1781 default: gcc_unreachable ();
1782 }
1783 }
1784
1785 sprintf (result, "mov%s%s\t%%0, %%%d, %%1",
1786 op, isfp ? ".s" : "", inverted ? 3 : 2);
1787 return result;
1788 }
1789
1790
1791 char *
1792 xtensa_emit_call (int callop, rtx *operands)
1793 {
1794 static char result[64];
1795 rtx tgt = operands[callop];
1796
1797 if (GET_CODE (tgt) == CONST_INT)
1798 sprintf (result, "call%d\t0x%lx", WINDOW_SIZE, INTVAL (tgt));
1799 else if (register_operand (tgt, VOIDmode))
1800 sprintf (result, "callx%d\t%%%d", WINDOW_SIZE, callop);
1801 else
1802 sprintf (result, "call%d\t%%%d", WINDOW_SIZE, callop);
1803
1804 return result;
1805 }
1806
1807
1808 bool
1809 xtensa_legitimate_address_p (machine_mode mode, rtx addr, bool strict)
1810 {
1811 /* Allow constant pool addresses. */
1812 if (mode != BLKmode && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
1813 && ! TARGET_CONST16 && constantpool_address_p (addr)
1814 && ! xtensa_tls_referenced_p (addr))
1815 return true;
1816
1817 while (GET_CODE (addr) == SUBREG)
1818 addr = SUBREG_REG (addr);
1819
1820 /* Allow base registers. */
1821 if (GET_CODE (addr) == REG && BASE_REG_P (addr, strict))
1822 return true;
1823
1824 /* Check for "register + offset" addressing. */
1825 if (GET_CODE (addr) == PLUS)
1826 {
1827 rtx xplus0 = XEXP (addr, 0);
1828 rtx xplus1 = XEXP (addr, 1);
1829 enum rtx_code code0;
1830 enum rtx_code code1;
1831
1832 while (GET_CODE (xplus0) == SUBREG)
1833 xplus0 = SUBREG_REG (xplus0);
1834 code0 = GET_CODE (xplus0);
1835
1836 while (GET_CODE (xplus1) == SUBREG)
1837 xplus1 = SUBREG_REG (xplus1);
1838 code1 = GET_CODE (xplus1);
1839
1840 /* Swap operands if necessary so the register is first. */
1841 if (code0 != REG && code1 == REG)
1842 {
1843 xplus0 = XEXP (addr, 1);
1844 xplus1 = XEXP (addr, 0);
1845 code0 = GET_CODE (xplus0);
1846 code1 = GET_CODE (xplus1);
1847 }
1848
1849 if (code0 == REG && BASE_REG_P (xplus0, strict)
1850 && code1 == CONST_INT
1851 && xtensa_mem_offset (INTVAL (xplus1), mode))
1852 return true;
1853 }
1854
1855 return false;
1856 }
1857
1858
1859 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol. */
1860
1861 static GTY(()) rtx xtensa_tls_module_base_symbol;
1862
1863 static rtx
1864 xtensa_tls_module_base (void)
1865 {
1866 if (! xtensa_tls_module_base_symbol)
1867 {
1868 xtensa_tls_module_base_symbol =
1869 gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
1870 SYMBOL_REF_FLAGS (xtensa_tls_module_base_symbol)
1871 |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
1872 }
1873
1874 return xtensa_tls_module_base_symbol;
1875 }
1876
1877
1878 static rtx_insn *
1879 xtensa_call_tls_desc (rtx sym, rtx *retp)
1880 {
1881 rtx fn, arg, a10;
1882 rtx_insn *call_insn, *insns;
1883
1884 start_sequence ();
1885 fn = gen_reg_rtx (Pmode);
1886 arg = gen_reg_rtx (Pmode);
1887 a10 = gen_rtx_REG (Pmode, 10);
1888
1889 emit_insn (gen_tls_func (fn, sym));
1890 emit_insn (gen_tls_arg (arg, sym));
1891 emit_move_insn (a10, arg);
1892 call_insn = emit_call_insn (gen_tls_call (a10, fn, sym, const1_rtx));
1893 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn), a10);
1894 insns = get_insns ();
1895 end_sequence ();
1896
1897 *retp = a10;
1898 return insns;
1899 }
1900
1901
1902 static rtx
1903 xtensa_legitimize_tls_address (rtx x)
1904 {
1905 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
1906 rtx dest, tp, ret, modbase, base, addend;
1907 rtx_insn *insns;
1908
1909 dest = gen_reg_rtx (Pmode);
1910 switch (model)
1911 {
1912 case TLS_MODEL_GLOBAL_DYNAMIC:
1913 insns = xtensa_call_tls_desc (x, &ret);
1914 emit_libcall_block (insns, dest, ret, x);
1915 break;
1916
1917 case TLS_MODEL_LOCAL_DYNAMIC:
1918 base = gen_reg_rtx (Pmode);
1919 modbase = xtensa_tls_module_base ();
1920 insns = xtensa_call_tls_desc (modbase, &ret);
1921 emit_libcall_block (insns, base, ret, modbase);
1922 addend = force_reg (SImode, gen_sym_DTPOFF (x));
1923 emit_insn (gen_addsi3 (dest, base, addend));
1924 break;
1925
1926 case TLS_MODEL_INITIAL_EXEC:
1927 case TLS_MODEL_LOCAL_EXEC:
1928 tp = gen_reg_rtx (SImode);
1929 emit_insn (gen_get_thread_pointersi (tp));
1930 addend = force_reg (SImode, gen_sym_TPOFF (x));
1931 emit_insn (gen_addsi3 (dest, tp, addend));
1932 break;
1933
1934 default:
1935 gcc_unreachable ();
1936 }
1937
1938 return dest;
1939 }
1940
1941
1942 rtx
1943 xtensa_legitimize_address (rtx x,
1944 rtx oldx ATTRIBUTE_UNUSED,
1945 machine_mode mode)
1946 {
1947 if (xtensa_tls_symbol_p (x))
1948 return xtensa_legitimize_tls_address (x);
1949
1950 if (GET_CODE (x) == PLUS)
1951 {
1952 rtx plus0 = XEXP (x, 0);
1953 rtx plus1 = XEXP (x, 1);
1954
1955 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
1956 {
1957 plus0 = XEXP (x, 1);
1958 plus1 = XEXP (x, 0);
1959 }
1960
1961 /* Try to split up the offset to use an ADDMI instruction. */
1962 if (GET_CODE (plus0) == REG
1963 && GET_CODE (plus1) == CONST_INT
1964 && !xtensa_mem_offset (INTVAL (plus1), mode)
1965 && !xtensa_simm8 (INTVAL (plus1))
1966 && xtensa_mem_offset (INTVAL (plus1) & 0xff, mode)
1967 && xtensa_simm8x256 (INTVAL (plus1) & ~0xff))
1968 {
1969 rtx temp = gen_reg_rtx (Pmode);
1970 rtx addmi_offset = GEN_INT (INTVAL (plus1) & ~0xff);
1971 emit_insn (gen_rtx_SET (temp, gen_rtx_PLUS (Pmode, plus0,
1972 addmi_offset)));
1973 return gen_rtx_PLUS (Pmode, temp, GEN_INT (INTVAL (plus1) & 0xff));
1974 }
1975 }
1976
1977 return x;
1978 }
1979
1980 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
1981
1982 Treat constant-pool references as "mode dependent" since they can
1983 only be accessed with SImode loads. This works around a bug in the
1984 combiner where a constant pool reference is temporarily converted
1985 to an HImode load, which is then assumed to zero-extend based on
1986 our definition of LOAD_EXTEND_OP. This is wrong because the high
1987 bits of a 16-bit value in the constant pool are now sign-extended
1988 by default. */
1989
1990 static bool
1991 xtensa_mode_dependent_address_p (const_rtx addr,
1992 addr_space_t as ATTRIBUTE_UNUSED)
1993 {
1994 return constantpool_address_p (addr);
1995 }
1996
1997 /* Return TRUE if X contains any TLS symbol references. */
1998
1999 bool
2000 xtensa_tls_referenced_p (rtx x)
2001 {
2002 if (! TARGET_HAVE_TLS)
2003 return false;
2004
2005 subrtx_iterator::array_type array;
2006 FOR_EACH_SUBRTX (iter, array, x, ALL)
2007 {
2008 const_rtx x = *iter;
2009 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0)
2010 return true;
2011
2012 /* Ignore TLS references that have already been legitimized. */
2013 if (GET_CODE (x) == UNSPEC)
2014 switch (XINT (x, 1))
2015 {
2016 case UNSPEC_TPOFF:
2017 case UNSPEC_DTPOFF:
2018 case UNSPEC_TLS_FUNC:
2019 case UNSPEC_TLS_ARG:
2020 case UNSPEC_TLS_CALL:
2021 iter.skip_subrtxes ();
2022 break;
2023 default:
2024 break;
2025 }
2026 }
2027 return false;
2028 }
2029
2030
2031 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2032
2033 static bool
2034 xtensa_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2035 {
2036 return xtensa_tls_referenced_p (x);
2037 }
2038
2039
2040 /* Return the debugger register number to use for 'regno'. */
2041
2042 int
2043 xtensa_dbx_register_number (int regno)
2044 {
2045 int first = -1;
2046
2047 if (GP_REG_P (regno))
2048 {
2049 regno -= GP_REG_FIRST;
2050 first = 0;
2051 }
2052 else if (BR_REG_P (regno))
2053 {
2054 regno -= BR_REG_FIRST;
2055 first = 16;
2056 }
2057 else if (FP_REG_P (regno))
2058 {
2059 regno -= FP_REG_FIRST;
2060 first = 48;
2061 }
2062 else if (ACC_REG_P (regno))
2063 {
2064 first = 0x200; /* Start of Xtensa special registers. */
2065 regno = 16; /* ACCLO is special register 16. */
2066 }
2067
2068 /* When optimizing, we sometimes get asked about pseudo-registers
2069 that don't represent hard registers. Return 0 for these. */
2070 if (first == -1)
2071 return 0;
2072
2073 return first + regno;
2074 }
2075
2076
2077 /* Argument support functions. */
2078
2079 /* Initialize CUMULATIVE_ARGS for a function. */
2080
2081 void
2082 init_cumulative_args (CUMULATIVE_ARGS *cum, int incoming)
2083 {
2084 cum->arg_words = 0;
2085 cum->incoming = incoming;
2086 }
2087
2088
2089 /* Advance the argument to the next argument position. */
2090
2091 static void
2092 xtensa_function_arg_advance (cumulative_args_t cum, machine_mode mode,
2093 const_tree type, bool named ATTRIBUTE_UNUSED)
2094 {
2095 int words, max;
2096 int *arg_words;
2097
2098 arg_words = &get_cumulative_args (cum)->arg_words;
2099 max = MAX_ARGS_IN_REGISTERS;
2100
2101 words = (((mode != BLKmode)
2102 ? (int) GET_MODE_SIZE (mode)
2103 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2104
2105 if (*arg_words < max
2106 && (targetm.calls.must_pass_in_stack (mode, type)
2107 || *arg_words + words > max))
2108 *arg_words = max;
2109
2110 *arg_words += words;
2111 }
2112
2113
2114 /* Return an RTL expression containing the register for the given mode,
2115 or 0 if the argument is to be passed on the stack. INCOMING_P is nonzero
2116 if this is an incoming argument to the current function. */
2117
2118 static rtx
2119 xtensa_function_arg_1 (cumulative_args_t cum_v, machine_mode mode,
2120 const_tree type, bool incoming_p)
2121 {
2122 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2123 int regbase, words, max;
2124 int *arg_words;
2125 int regno;
2126
2127 arg_words = &cum->arg_words;
2128 regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
2129 max = MAX_ARGS_IN_REGISTERS;
2130
2131 words = (((mode != BLKmode)
2132 ? (int) GET_MODE_SIZE (mode)
2133 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2134
2135 if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
2136 {
2137 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_WORD;
2138 *arg_words = (*arg_words + align - 1) & -align;
2139 }
2140
2141 if (*arg_words + words > max)
2142 return (rtx)0;
2143
2144 regno = regbase + *arg_words;
2145
2146 if (cum->incoming && regno <= A7_REG && regno + words > A7_REG)
2147 cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
2148
2149 return gen_rtx_REG (mode, regno);
2150 }
2151
2152 /* Implement TARGET_FUNCTION_ARG. */
2153
2154 static rtx
2155 xtensa_function_arg (cumulative_args_t cum, machine_mode mode,
2156 const_tree type, bool named ATTRIBUTE_UNUSED)
2157 {
2158 return xtensa_function_arg_1 (cum, mode, type, false);
2159 }
2160
2161 /* Implement TARGET_FUNCTION_INCOMING_ARG. */
2162
2163 static rtx
2164 xtensa_function_incoming_arg (cumulative_args_t cum, machine_mode mode,
2165 const_tree type, bool named ATTRIBUTE_UNUSED)
2166 {
2167 return xtensa_function_arg_1 (cum, mode, type, true);
2168 }
2169
2170 static unsigned int
2171 xtensa_function_arg_boundary (machine_mode mode, const_tree type)
2172 {
2173 unsigned int alignment;
2174
2175 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2176 if (alignment < PARM_BOUNDARY)
2177 alignment = PARM_BOUNDARY;
2178 if (alignment > STACK_BOUNDARY)
2179 alignment = STACK_BOUNDARY;
2180 return alignment;
2181 }
2182
2183
2184 static bool
2185 xtensa_return_in_msb (const_tree valtype)
2186 {
2187 return (TARGET_BIG_ENDIAN
2188 && AGGREGATE_TYPE_P (valtype)
2189 && int_size_in_bytes (valtype) >= UNITS_PER_WORD);
2190 }
2191
2192
2193 static void
2194 xtensa_option_override (void)
2195 {
2196 int regno;
2197 machine_mode mode;
2198
2199 if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
2200 error ("boolean registers required for the floating-point option");
2201
2202 /* Set up array giving whether a given register can hold a given mode. */
2203 for (mode = VOIDmode;
2204 mode != MAX_MACHINE_MODE;
2205 mode = (machine_mode) ((int) mode + 1))
2206 {
2207 int size = GET_MODE_SIZE (mode);
2208 enum mode_class mclass = GET_MODE_CLASS (mode);
2209
2210 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2211 {
2212 int temp;
2213
2214 if (ACC_REG_P (regno))
2215 temp = (TARGET_MAC16
2216 && (mclass == MODE_INT) && (size <= UNITS_PER_WORD));
2217 else if (GP_REG_P (regno))
2218 temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
2219 else if (FP_REG_P (regno))
2220 temp = (TARGET_HARD_FLOAT && (mode == SFmode));
2221 else if (BR_REG_P (regno))
2222 temp = (TARGET_BOOLEANS && (mode == CCmode));
2223 else
2224 temp = FALSE;
2225
2226 xtensa_hard_regno_mode_ok[(int) mode][regno] = temp;
2227 }
2228 }
2229
2230 init_machine_status = xtensa_init_machine_status;
2231
2232 /* Check PIC settings. PIC is only supported when using L32R
2233 instructions, and some targets need to always use PIC. */
2234 if (flag_pic && TARGET_CONST16)
2235 error ("-f%s is not supported with CONST16 instructions",
2236 (flag_pic > 1 ? "PIC" : "pic"));
2237 else if (TARGET_FORCE_NO_PIC)
2238 flag_pic = 0;
2239 else if (XTENSA_ALWAYS_PIC)
2240 {
2241 if (TARGET_CONST16)
2242 error ("PIC is required but not supported with CONST16 instructions");
2243 flag_pic = 1;
2244 }
2245 /* There's no need for -fPIC (as opposed to -fpic) on Xtensa. */
2246 if (flag_pic > 1)
2247 flag_pic = 1;
2248 if (flag_pic && !flag_pie)
2249 flag_shlib = 1;
2250
2251 /* Hot/cold partitioning does not work on this architecture, because of
2252 constant pools (the load instruction cannot necessarily reach that far).
2253 Therefore disable it on this architecture. */
2254 if (flag_reorder_blocks_and_partition)
2255 {
2256 flag_reorder_blocks_and_partition = 0;
2257 flag_reorder_blocks = 1;
2258 }
2259 }
2260
2261 /* A C compound statement to output to stdio stream STREAM the
2262 assembler syntax for an instruction operand X. X is an RTL
2263 expression.
2264
2265 CODE is a value that can be used to specify one of several ways
2266 of printing the operand. It is used when identical operands
2267 must be printed differently depending on the context. CODE
2268 comes from the '%' specification that was used to request
2269 printing of the operand. If the specification was just '%DIGIT'
2270 then CODE is 0; if the specification was '%LTR DIGIT' then CODE
2271 is the ASCII code for LTR.
2272
2273 If X is a register, this macro should print the register's name.
2274 The names can be found in an array 'reg_names' whose type is
2275 'char *[]'. 'reg_names' is initialized from 'REGISTER_NAMES'.
2276
2277 When the machine description has a specification '%PUNCT' (a '%'
2278 followed by a punctuation character), this macro is called with
2279 a null pointer for X and the punctuation character for CODE.
2280
2281 'a', 'c', 'l', and 'n' are reserved.
2282
2283 The Xtensa specific codes are:
2284
2285 'd' CONST_INT, print as signed decimal
2286 'x' CONST_INT, print as signed hexadecimal
2287 'K' CONST_INT, print number of bits in mask for EXTUI
2288 'R' CONST_INT, print (X & 0x1f)
2289 'L' CONST_INT, print ((32 - X) & 0x1f)
2290 'D' REG, print second register of double-word register operand
2291 'N' MEM, print address of next word following a memory operand
2292 'v' MEM, if memory reference is volatile, output a MEMW before it
2293 't' any constant, add "@h" suffix for top 16 bits
2294 'b' any constant, add "@l" suffix for bottom 16 bits
2295 */
2296
2297 static void
2298 printx (FILE *file, signed int val)
2299 {
2300 /* Print a hexadecimal value in a nice way. */
2301 if ((val > -0xa) && (val < 0xa))
2302 fprintf (file, "%d", val);
2303 else if (val < 0)
2304 fprintf (file, "-0x%x", -val);
2305 else
2306 fprintf (file, "0x%x", val);
2307 }
2308
2309
2310 void
2311 print_operand (FILE *file, rtx x, int letter)
2312 {
2313 if (!x)
2314 error ("PRINT_OPERAND null pointer");
2315
2316 switch (letter)
2317 {
2318 case 'D':
2319 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2320 fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
2321 else
2322 output_operand_lossage ("invalid %%D value");
2323 break;
2324
2325 case 'v':
2326 if (GET_CODE (x) == MEM)
2327 {
2328 /* For a volatile memory reference, emit a MEMW before the
2329 load or store. */
2330 if (MEM_VOLATILE_P (x) && TARGET_SERIALIZE_VOLATILE)
2331 fprintf (file, "memw\n\t");
2332 }
2333 else
2334 output_operand_lossage ("invalid %%v value");
2335 break;
2336
2337 case 'N':
2338 if (GET_CODE (x) == MEM
2339 && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
2340 {
2341 x = adjust_address (x, GET_MODE (x) == DFmode ? SFmode : SImode, 4);
2342 output_address (XEXP (x, 0));
2343 }
2344 else
2345 output_operand_lossage ("invalid %%N value");
2346 break;
2347
2348 case 'K':
2349 if (GET_CODE (x) == CONST_INT)
2350 {
2351 int num_bits = 0;
2352 unsigned val = INTVAL (x);
2353 while (val & 1)
2354 {
2355 num_bits += 1;
2356 val = val >> 1;
2357 }
2358 if ((val != 0) || (num_bits == 0) || (num_bits > 16))
2359 fatal_insn ("invalid mask", x);
2360
2361 fprintf (file, "%d", num_bits);
2362 }
2363 else
2364 output_operand_lossage ("invalid %%K value");
2365 break;
2366
2367 case 'L':
2368 if (GET_CODE (x) == CONST_INT)
2369 fprintf (file, "%ld", (32 - INTVAL (x)) & 0x1f);
2370 else
2371 output_operand_lossage ("invalid %%L value");
2372 break;
2373
2374 case 'R':
2375 if (GET_CODE (x) == CONST_INT)
2376 fprintf (file, "%ld", INTVAL (x) & 0x1f);
2377 else
2378 output_operand_lossage ("invalid %%R value");
2379 break;
2380
2381 case 'x':
2382 if (GET_CODE (x) == CONST_INT)
2383 printx (file, INTVAL (x));
2384 else
2385 output_operand_lossage ("invalid %%x value");
2386 break;
2387
2388 case 'd':
2389 if (GET_CODE (x) == CONST_INT)
2390 fprintf (file, "%ld", INTVAL (x));
2391 else
2392 output_operand_lossage ("invalid %%d value");
2393 break;
2394
2395 case 't':
2396 case 'b':
2397 if (GET_CODE (x) == CONST_INT)
2398 {
2399 printx (file, INTVAL (x));
2400 fputs (letter == 't' ? "@h" : "@l", file);
2401 }
2402 else if (GET_CODE (x) == CONST_DOUBLE)
2403 {
2404 REAL_VALUE_TYPE r;
2405 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2406 if (GET_MODE (x) == SFmode)
2407 {
2408 long l;
2409 REAL_VALUE_TO_TARGET_SINGLE (r, l);
2410 fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
2411 }
2412 else
2413 output_operand_lossage ("invalid %%t/%%b value");
2414 }
2415 else if (GET_CODE (x) == CONST)
2416 {
2417 /* X must be a symbolic constant on ELF. Write an expression
2418 suitable for 'const16' that sets the high or low 16 bits. */
2419 if (GET_CODE (XEXP (x, 0)) != PLUS
2420 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
2421 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2422 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
2423 output_operand_lossage ("invalid %%t/%%b value");
2424 print_operand (file, XEXP (XEXP (x, 0), 0), 0);
2425 fputs (letter == 't' ? "@h" : "@l", file);
2426 /* There must be a non-alphanumeric character between 'h' or 'l'
2427 and the number. The '-' is added by print_operand() already. */
2428 if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
2429 fputs ("+", file);
2430 print_operand (file, XEXP (XEXP (x, 0), 1), 0);
2431 }
2432 else
2433 {
2434 output_addr_const (file, x);
2435 fputs (letter == 't' ? "@h" : "@l", file);
2436 }
2437 break;
2438
2439 default:
2440 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2441 fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
2442 else if (GET_CODE (x) == MEM)
2443 output_address (XEXP (x, 0));
2444 else if (GET_CODE (x) == CONST_INT)
2445 fprintf (file, "%ld", INTVAL (x));
2446 else
2447 output_addr_const (file, x);
2448 }
2449 }
2450
2451
2452 /* A C compound statement to output to stdio stream STREAM the
2453 assembler syntax for an instruction operand that is a memory
2454 reference whose address is ADDR. ADDR is an RTL expression. */
2455
2456 void
2457 print_operand_address (FILE *file, rtx addr)
2458 {
2459 if (!addr)
2460 error ("PRINT_OPERAND_ADDRESS, null pointer");
2461
2462 switch (GET_CODE (addr))
2463 {
2464 default:
2465 fatal_insn ("invalid address", addr);
2466 break;
2467
2468 case REG:
2469 fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
2470 break;
2471
2472 case PLUS:
2473 {
2474 rtx reg = (rtx)0;
2475 rtx offset = (rtx)0;
2476 rtx arg0 = XEXP (addr, 0);
2477 rtx arg1 = XEXP (addr, 1);
2478
2479 if (GET_CODE (arg0) == REG)
2480 {
2481 reg = arg0;
2482 offset = arg1;
2483 }
2484 else if (GET_CODE (arg1) == REG)
2485 {
2486 reg = arg1;
2487 offset = arg0;
2488 }
2489 else
2490 fatal_insn ("no register in address", addr);
2491
2492 if (CONSTANT_P (offset))
2493 {
2494 fprintf (file, "%s, ", reg_names [REGNO (reg)]);
2495 output_addr_const (file, offset);
2496 }
2497 else
2498 fatal_insn ("address offset not a constant", addr);
2499 }
2500 break;
2501
2502 case LABEL_REF:
2503 case SYMBOL_REF:
2504 case CONST_INT:
2505 case CONST:
2506 output_addr_const (file, addr);
2507 break;
2508 }
2509 }
2510
2511 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
2512
2513 static bool
2514 xtensa_output_addr_const_extra (FILE *fp, rtx x)
2515 {
2516 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
2517 {
2518 switch (XINT (x, 1))
2519 {
2520 case UNSPEC_TPOFF:
2521 output_addr_const (fp, XVECEXP (x, 0, 0));
2522 fputs ("@TPOFF", fp);
2523 return true;
2524 case UNSPEC_DTPOFF:
2525 output_addr_const (fp, XVECEXP (x, 0, 0));
2526 fputs ("@DTPOFF", fp);
2527 return true;
2528 case UNSPEC_PLT:
2529 if (flag_pic)
2530 {
2531 output_addr_const (fp, XVECEXP (x, 0, 0));
2532 fputs ("@PLT", fp);
2533 return true;
2534 }
2535 break;
2536 default:
2537 break;
2538 }
2539 }
2540 return false;
2541 }
2542
2543
2544 void
2545 xtensa_output_literal (FILE *file, rtx x, machine_mode mode, int labelno)
2546 {
2547 long value_long[2];
2548 REAL_VALUE_TYPE r;
2549 int size;
2550 rtx first, second;
2551
2552 fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
2553
2554 switch (GET_MODE_CLASS (mode))
2555 {
2556 case MODE_FLOAT:
2557 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
2558
2559 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2560 switch (mode)
2561 {
2562 case SFmode:
2563 REAL_VALUE_TO_TARGET_SINGLE (r, value_long[0]);
2564 if (HOST_BITS_PER_LONG > 32)
2565 value_long[0] &= 0xffffffff;
2566 fprintf (file, "0x%08lx\n", value_long[0]);
2567 break;
2568
2569 case DFmode:
2570 REAL_VALUE_TO_TARGET_DOUBLE (r, value_long);
2571 if (HOST_BITS_PER_LONG > 32)
2572 {
2573 value_long[0] &= 0xffffffff;
2574 value_long[1] &= 0xffffffff;
2575 }
2576 fprintf (file, "0x%08lx, 0x%08lx\n",
2577 value_long[0], value_long[1]);
2578 break;
2579
2580 default:
2581 gcc_unreachable ();
2582 }
2583
2584 break;
2585
2586 case MODE_INT:
2587 case MODE_PARTIAL_INT:
2588 size = GET_MODE_SIZE (mode);
2589 switch (size)
2590 {
2591 case 4:
2592 output_addr_const (file, x);
2593 fputs ("\n", file);
2594 break;
2595
2596 case 8:
2597 split_double (x, &first, &second);
2598 output_addr_const (file, first);
2599 fputs (", ", file);
2600 output_addr_const (file, second);
2601 fputs ("\n", file);
2602 break;
2603
2604 default:
2605 gcc_unreachable ();
2606 }
2607 break;
2608
2609 default:
2610 gcc_unreachable ();
2611 }
2612 }
2613
2614 static bool
2615 xtensa_call_save_reg(int regno)
2616 {
2617 if (TARGET_WINDOWED_ABI)
2618 return false;
2619
2620 if (regno == A0_REG)
2621 return crtl->profile || !crtl->is_leaf || crtl->calls_eh_return ||
2622 df_regs_ever_live_p (regno);
2623
2624 if (crtl->calls_eh_return && regno >= 2 && regno < 4)
2625 return true;
2626
2627 return !fixed_regs[regno] && !call_used_regs[regno] &&
2628 df_regs_ever_live_p (regno);
2629 }
2630
2631 /* Return the bytes needed to compute the frame pointer from the current
2632 stack pointer. */
2633
2634 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
2635 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
2636
2637 long
2638 compute_frame_size (int size)
2639 {
2640 int regno;
2641
2642 /* Add space for the incoming static chain value. */
2643 if (cfun->static_chain_decl != NULL)
2644 size += (1 * UNITS_PER_WORD);
2645
2646 xtensa_callee_save_size = 0;
2647 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2648 {
2649 if (xtensa_call_save_reg(regno))
2650 xtensa_callee_save_size += UNITS_PER_WORD;
2651 }
2652
2653 xtensa_current_frame_size =
2654 XTENSA_STACK_ALIGN (size
2655 + xtensa_callee_save_size
2656 + crtl->outgoing_args_size
2657 + (WINDOW_SIZE * UNITS_PER_WORD));
2658 xtensa_callee_save_size = XTENSA_STACK_ALIGN (xtensa_callee_save_size);
2659 return xtensa_current_frame_size;
2660 }
2661
2662
2663 bool
2664 xtensa_frame_pointer_required (void)
2665 {
2666 /* The code to expand builtin_frame_addr and builtin_return_addr
2667 currently uses the hard_frame_pointer instead of frame_pointer.
2668 This seems wrong but maybe it's necessary for other architectures.
2669 This function is derived from the i386 code. */
2670
2671 if (cfun->machine->accesses_prev_frame)
2672 return true;
2673
2674 return false;
2675 }
2676
2677
2678 /* minimum frame = reg save area (4 words) plus static chain (1 word)
2679 and the total number of words must be a multiple of 128 bits. */
2680 #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD)
2681
2682 void
2683 xtensa_expand_prologue (void)
2684 {
2685 HOST_WIDE_INT total_size;
2686 rtx_insn *insn = NULL;
2687 rtx note_rtx;
2688
2689
2690 total_size = compute_frame_size (get_frame_size ());
2691
2692 if (TARGET_WINDOWED_ABI)
2693 {
2694 if (total_size < (1 << (12+3)))
2695 insn = emit_insn (gen_entry (GEN_INT (total_size)));
2696 else
2697 {
2698 /* Use a8 as a temporary since a0-a7 may be live. */
2699 rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
2700 emit_insn (gen_entry (GEN_INT (MIN_FRAME_SIZE)));
2701 emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
2702 emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
2703 insn = emit_insn (gen_movsi (stack_pointer_rtx, tmp_reg));
2704 }
2705 }
2706 else
2707 {
2708 int regno;
2709 HOST_WIDE_INT offset = 0;
2710
2711 /* -128 is a limit of single addi instruction. */
2712 if (total_size > 0 && total_size <= 128)
2713 {
2714 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2715 GEN_INT (-total_size)));
2716 RTX_FRAME_RELATED_P (insn) = 1;
2717 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2718 plus_constant (Pmode, stack_pointer_rtx,
2719 -total_size));
2720 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2721 offset = total_size - UNITS_PER_WORD;
2722 }
2723 else if (xtensa_callee_save_size)
2724 {
2725 /* 1020 is maximal s32i offset, if the frame is bigger than that
2726 * we move sp to the end of callee-saved save area, save and then
2727 * move it to its final location. */
2728 if (total_size > 1024)
2729 {
2730 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2731 GEN_INT (-xtensa_callee_save_size)));
2732 RTX_FRAME_RELATED_P (insn) = 1;
2733 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2734 plus_constant (Pmode, stack_pointer_rtx,
2735 -xtensa_callee_save_size));
2736 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2737 offset = xtensa_callee_save_size - UNITS_PER_WORD;
2738 }
2739 else
2740 {
2741 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2742 emit_move_insn (tmp_reg, GEN_INT (total_size));
2743 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
2744 stack_pointer_rtx, tmp_reg));
2745 RTX_FRAME_RELATED_P (insn) = 1;
2746 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2747 plus_constant (Pmode, stack_pointer_rtx,
2748 -total_size));
2749 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2750 offset = total_size - UNITS_PER_WORD;
2751 }
2752 }
2753
2754 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2755 {
2756 if (xtensa_call_save_reg(regno))
2757 {
2758 rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
2759 rtx mem = gen_frame_mem (SImode, x);
2760 rtx reg = gen_rtx_REG (SImode, regno);
2761
2762 offset -= UNITS_PER_WORD;
2763 insn = emit_move_insn (mem, reg);
2764 RTX_FRAME_RELATED_P (insn) = 1;
2765 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
2766 gen_rtx_SET (mem, reg));
2767 }
2768 }
2769 if (total_size > 1024)
2770 {
2771 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2772 emit_move_insn (tmp_reg, GEN_INT (total_size -
2773 xtensa_callee_save_size));
2774 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
2775 stack_pointer_rtx, tmp_reg));
2776 RTX_FRAME_RELATED_P (insn) = 1;
2777 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2778 plus_constant (Pmode, stack_pointer_rtx,
2779 xtensa_callee_save_size -
2780 total_size));
2781 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2782 }
2783 }
2784
2785 if (frame_pointer_needed)
2786 {
2787 if (cfun->machine->set_frame_ptr_insn)
2788 {
2789 rtx_insn *first;
2790
2791 push_topmost_sequence ();
2792 first = get_insns ();
2793 pop_topmost_sequence ();
2794
2795 /* For all instructions prior to set_frame_ptr_insn, replace
2796 hard_frame_pointer references with stack_pointer. */
2797 for (insn = first;
2798 insn != cfun->machine->set_frame_ptr_insn;
2799 insn = NEXT_INSN (insn))
2800 {
2801 if (INSN_P (insn))
2802 {
2803 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
2804 hard_frame_pointer_rtx,
2805 stack_pointer_rtx);
2806 df_insn_rescan (insn);
2807 }
2808 }
2809 }
2810 else
2811 {
2812 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
2813 stack_pointer_rtx));
2814 if (!TARGET_WINDOWED_ABI)
2815 {
2816 note_rtx = gen_rtx_SET (hard_frame_pointer_rtx,
2817 stack_pointer_rtx);
2818 RTX_FRAME_RELATED_P (insn) = 1;
2819 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2820 }
2821 }
2822 }
2823
2824 if (TARGET_WINDOWED_ABI)
2825 {
2826 /* Create a note to describe the CFA. Because this is only used to set
2827 DW_AT_frame_base for debug info, don't bother tracking changes through
2828 each instruction in the prologue. It just takes up space. */
2829 note_rtx = gen_rtx_SET ((frame_pointer_needed
2830 ? hard_frame_pointer_rtx
2831 : stack_pointer_rtx),
2832 plus_constant (Pmode, stack_pointer_rtx,
2833 -total_size));
2834 RTX_FRAME_RELATED_P (insn) = 1;
2835 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2836 }
2837 }
2838
2839 void
2840 xtensa_expand_epilogue (void)
2841 {
2842 if (!TARGET_WINDOWED_ABI)
2843 {
2844 int regno;
2845 HOST_WIDE_INT offset;
2846
2847 if (xtensa_current_frame_size > (frame_pointer_needed ? 127 : 1024))
2848 {
2849 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2850 emit_move_insn (tmp_reg, GEN_INT (xtensa_current_frame_size -
2851 xtensa_callee_save_size));
2852 emit_insn (gen_addsi3 (stack_pointer_rtx, frame_pointer_needed ?
2853 hard_frame_pointer_rtx : stack_pointer_rtx,
2854 tmp_reg));
2855 offset = xtensa_callee_save_size - UNITS_PER_WORD;
2856 }
2857 else
2858 {
2859 if (frame_pointer_needed)
2860 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
2861 offset = xtensa_current_frame_size - UNITS_PER_WORD;
2862 }
2863
2864 /* Prevent reordering of saved a0 update and loading it back from
2865 the save area. */
2866 if (crtl->calls_eh_return)
2867 emit_insn (gen_blockage ());
2868
2869 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2870 {
2871 if (xtensa_call_save_reg(regno))
2872 {
2873 rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
2874
2875 offset -= UNITS_PER_WORD;
2876 emit_move_insn (gen_rtx_REG (SImode, regno),
2877 gen_frame_mem (SImode, x));
2878 }
2879 }
2880
2881 if (xtensa_current_frame_size > 0)
2882 {
2883 if (frame_pointer_needed || /* always reachable with addi */
2884 xtensa_current_frame_size > 1024 ||
2885 xtensa_current_frame_size <= 127)
2886 {
2887 if (xtensa_current_frame_size <= 127)
2888 offset = xtensa_current_frame_size;
2889 else
2890 offset = xtensa_callee_save_size;
2891
2892 emit_insn (gen_addsi3 (stack_pointer_rtx,
2893 stack_pointer_rtx,
2894 GEN_INT (offset)));
2895 }
2896 else
2897 {
2898 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2899 emit_move_insn (tmp_reg, GEN_INT (xtensa_current_frame_size));
2900 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2901 tmp_reg));
2902 }
2903 }
2904
2905 if (crtl->calls_eh_return)
2906 emit_insn (gen_add3_insn (stack_pointer_rtx,
2907 stack_pointer_rtx,
2908 EH_RETURN_STACKADJ_RTX));
2909 }
2910 xtensa_current_frame_size = 0;
2911 xtensa_callee_save_size = 0;
2912 emit_jump_insn (gen_return ());
2913 }
2914
2915 void
2916 xtensa_set_return_address (rtx address, rtx scratch)
2917 {
2918 HOST_WIDE_INT total_size = compute_frame_size (get_frame_size ());
2919 rtx frame = frame_pointer_needed ?
2920 hard_frame_pointer_rtx : stack_pointer_rtx;
2921 rtx a0_addr = plus_constant (Pmode, frame,
2922 total_size - UNITS_PER_WORD);
2923 rtx note = gen_rtx_SET (gen_frame_mem (SImode, a0_addr),
2924 gen_rtx_REG (SImode, A0_REG));
2925 rtx insn;
2926
2927 if (total_size > 1024) {
2928 emit_move_insn (scratch, GEN_INT (total_size - UNITS_PER_WORD));
2929 emit_insn (gen_addsi3 (scratch, frame, scratch));
2930 a0_addr = scratch;
2931 }
2932
2933 insn = emit_move_insn (gen_frame_mem (SImode, a0_addr), address);
2934 RTX_FRAME_RELATED_P (insn) = 1;
2935 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
2936 }
2937
2938 rtx
2939 xtensa_return_addr (int count, rtx frame)
2940 {
2941 rtx result, retaddr, curaddr, label;
2942
2943 if (!TARGET_WINDOWED_ABI)
2944 {
2945 if (count != 0)
2946 return const0_rtx;
2947
2948 return get_hard_reg_initial_val (Pmode, A0_REG);
2949 }
2950
2951 if (count == -1)
2952 retaddr = gen_rtx_REG (Pmode, A0_REG);
2953 else
2954 {
2955 rtx addr = plus_constant (Pmode, frame, -4 * UNITS_PER_WORD);
2956 addr = memory_address (Pmode, addr);
2957 retaddr = gen_reg_rtx (Pmode);
2958 emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
2959 }
2960
2961 /* The 2 most-significant bits of the return address on Xtensa hold
2962 the register window size. To get the real return address, these
2963 bits must be replaced with the high bits from some address in the
2964 code. */
2965
2966 /* Get the 2 high bits of a local label in the code. */
2967 curaddr = gen_reg_rtx (Pmode);
2968 label = gen_label_rtx ();
2969 emit_label (label);
2970 LABEL_PRESERVE_P (label) = 1;
2971 emit_move_insn (curaddr, gen_rtx_LABEL_REF (Pmode, label));
2972 emit_insn (gen_lshrsi3 (curaddr, curaddr, GEN_INT (30)));
2973 emit_insn (gen_ashlsi3 (curaddr, curaddr, GEN_INT (30)));
2974
2975 /* Clear the 2 high bits of the return address. */
2976 result = gen_reg_rtx (Pmode);
2977 emit_insn (gen_ashlsi3 (result, retaddr, GEN_INT (2)));
2978 emit_insn (gen_lshrsi3 (result, result, GEN_INT (2)));
2979
2980 /* Combine them to get the result. */
2981 emit_insn (gen_iorsi3 (result, result, curaddr));
2982 return result;
2983 }
2984
2985 /* Disable the use of word-sized or smaller complex modes for structures,
2986 and for function arguments in particular, where they cause problems with
2987 register a7. The xtensa_copy_incoming_a7 function assumes that there is
2988 a single reference to an argument in a7, but with small complex modes the
2989 real and imaginary components may be extracted separately, leading to two
2990 uses of the register, only one of which would be replaced. */
2991
2992 static bool
2993 xtensa_member_type_forces_blk (const_tree, machine_mode mode)
2994 {
2995 return mode == CQImode || mode == CHImode;
2996 }
2997
2998 /* Create the va_list data type.
2999
3000 This structure is set up by __builtin_saveregs. The __va_reg field
3001 points to a stack-allocated region holding the contents of the
3002 incoming argument registers. The __va_ndx field is an index
3003 initialized to the position of the first unnamed (variable)
3004 argument. This same index is also used to address the arguments
3005 passed in memory. Thus, the __va_stk field is initialized to point
3006 to the position of the first argument in memory offset to account
3007 for the arguments passed in registers and to account for the size
3008 of the argument registers not being 16-byte aligned. E.G., there
3009 are 6 argument registers of 4 bytes each, but we want the __va_ndx
3010 for the first stack argument to have the maximal alignment of 16
3011 bytes, so we offset the __va_stk address by 32 bytes so that
3012 __va_stk[32] references the first argument on the stack. */
3013
3014 static tree
3015 xtensa_build_builtin_va_list (void)
3016 {
3017 tree f_stk, f_reg, f_ndx, record, type_decl;
3018
3019 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3020 type_decl = build_decl (BUILTINS_LOCATION,
3021 TYPE_DECL, get_identifier ("__va_list_tag"), record);
3022
3023 f_stk = build_decl (BUILTINS_LOCATION,
3024 FIELD_DECL, get_identifier ("__va_stk"),
3025 ptr_type_node);
3026 f_reg = build_decl (BUILTINS_LOCATION,
3027 FIELD_DECL, get_identifier ("__va_reg"),
3028 ptr_type_node);
3029 f_ndx = build_decl (BUILTINS_LOCATION,
3030 FIELD_DECL, get_identifier ("__va_ndx"),
3031 integer_type_node);
3032
3033 DECL_FIELD_CONTEXT (f_stk) = record;
3034 DECL_FIELD_CONTEXT (f_reg) = record;
3035 DECL_FIELD_CONTEXT (f_ndx) = record;
3036
3037 TYPE_STUB_DECL (record) = type_decl;
3038 TYPE_NAME (record) = type_decl;
3039 TYPE_FIELDS (record) = f_stk;
3040 DECL_CHAIN (f_stk) = f_reg;
3041 DECL_CHAIN (f_reg) = f_ndx;
3042
3043 layout_type (record);
3044 return record;
3045 }
3046
3047
3048 /* Save the incoming argument registers on the stack. Returns the
3049 address of the saved registers. */
3050
3051 static rtx
3052 xtensa_builtin_saveregs (void)
3053 {
3054 rtx gp_regs;
3055 int arg_words = crtl->args.info.arg_words;
3056 int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
3057
3058 if (gp_left <= 0)
3059 return const0_rtx;
3060
3061 /* Allocate the general-purpose register space. */
3062 gp_regs = assign_stack_local
3063 (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
3064 set_mem_alias_set (gp_regs, get_varargs_alias_set ());
3065
3066 /* Now store the incoming registers. */
3067 cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
3068 cfun->machine->vararg_a7 = true;
3069 move_block_from_reg (GP_ARG_FIRST + arg_words,
3070 adjust_address (gp_regs, BLKmode,
3071 arg_words * UNITS_PER_WORD),
3072 gp_left);
3073 if (cfun->machine->vararg_a7_copy != 0)
3074 emit_insn_before (cfun->machine->vararg_a7_copy, get_insns ());
3075
3076 return XEXP (gp_regs, 0);
3077 }
3078
3079
3080 /* Implement `va_start' for varargs and stdarg. We look at the
3081 current function to fill in an initial va_list. */
3082
3083 static void
3084 xtensa_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
3085 {
3086 tree f_stk, stk;
3087 tree f_reg, reg;
3088 tree f_ndx, ndx;
3089 tree t, u;
3090 int arg_words;
3091
3092 arg_words = crtl->args.info.arg_words;
3093
3094 f_stk = TYPE_FIELDS (va_list_type_node);
3095 f_reg = DECL_CHAIN (f_stk);
3096 f_ndx = DECL_CHAIN (f_reg);
3097
3098 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk, NULL_TREE);
3099 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
3100 f_reg, NULL_TREE);
3101 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
3102 f_ndx, NULL_TREE);
3103
3104 /* Call __builtin_saveregs; save the result in __va_reg */
3105 u = make_tree (sizetype, expand_builtin_saveregs ());
3106 u = fold_convert (ptr_type_node, u);
3107 t = build2 (MODIFY_EXPR, ptr_type_node, reg, u);
3108 TREE_SIDE_EFFECTS (t) = 1;
3109 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3110
3111 /* Set the __va_stk member to ($arg_ptr - 32). */
3112 u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
3113 u = fold_build_pointer_plus_hwi (u, -32);
3114 t = build2 (MODIFY_EXPR, ptr_type_node, stk, u);
3115 TREE_SIDE_EFFECTS (t) = 1;
3116 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3117
3118 /* Set the __va_ndx member. If the first variable argument is on
3119 the stack, adjust __va_ndx by 2 words to account for the extra
3120 alignment offset for __va_stk. */
3121 if (arg_words >= MAX_ARGS_IN_REGISTERS)
3122 arg_words += 2;
3123 t = build2 (MODIFY_EXPR, integer_type_node, ndx,
3124 build_int_cst (integer_type_node, arg_words * UNITS_PER_WORD));
3125 TREE_SIDE_EFFECTS (t) = 1;
3126 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3127 }
3128
3129
3130 /* Implement `va_arg'. */
3131
3132 static tree
3133 xtensa_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
3134 gimple_seq *post_p ATTRIBUTE_UNUSED)
3135 {
3136 tree f_stk, stk;
3137 tree f_reg, reg;
3138 tree f_ndx, ndx;
3139 tree type_size, array, orig_ndx, addr, size, va_size, t;
3140 tree lab_false, lab_over, lab_false2;
3141 bool indirect;
3142
3143 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3144 if (indirect)
3145 type = build_pointer_type (type);
3146
3147 /* Handle complex values as separate real and imaginary parts. */
3148 if (TREE_CODE (type) == COMPLEX_TYPE)
3149 {
3150 tree real_part, imag_part;
3151
3152 real_part = xtensa_gimplify_va_arg_expr (valist, TREE_TYPE (type),
3153 pre_p, NULL);
3154 real_part = get_initialized_tmp_var (real_part, pre_p, NULL);
3155
3156 imag_part = xtensa_gimplify_va_arg_expr (unshare_expr (valist),
3157 TREE_TYPE (type),
3158 pre_p, NULL);
3159 imag_part = get_initialized_tmp_var (imag_part, pre_p, NULL);
3160
3161 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
3162 }
3163
3164 f_stk = TYPE_FIELDS (va_list_type_node);
3165 f_reg = DECL_CHAIN (f_stk);
3166 f_ndx = DECL_CHAIN (f_reg);
3167
3168 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist,
3169 f_stk, NULL_TREE);
3170 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
3171 f_reg, NULL_TREE);
3172 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
3173 f_ndx, NULL_TREE);
3174
3175 type_size = size_in_bytes (type);
3176 va_size = round_up (type_size, UNITS_PER_WORD);
3177 gimplify_expr (&va_size, pre_p, NULL, is_gimple_val, fb_rvalue);
3178
3179
3180 /* First align __va_ndx if necessary for this arg:
3181
3182 orig_ndx = (AP).__va_ndx;
3183 if (__alignof__ (TYPE) > 4 )
3184 orig_ndx = ((orig_ndx + __alignof__ (TYPE) - 1)
3185 & -__alignof__ (TYPE)); */
3186
3187 orig_ndx = get_initialized_tmp_var (ndx, pre_p, NULL);
3188
3189 if (TYPE_ALIGN (type) > BITS_PER_WORD)
3190 {
3191 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_UNIT;
3192
3193 t = build2 (PLUS_EXPR, integer_type_node, unshare_expr (orig_ndx),
3194 build_int_cst (integer_type_node, align - 1));
3195 t = build2 (BIT_AND_EXPR, integer_type_node, t,
3196 build_int_cst (integer_type_node, -align));
3197 gimplify_assign (unshare_expr (orig_ndx), t, pre_p);
3198 }
3199
3200
3201 /* Increment __va_ndx to point past the argument:
3202
3203 (AP).__va_ndx = orig_ndx + __va_size (TYPE); */
3204
3205 t = fold_convert (integer_type_node, va_size);
3206 t = build2 (PLUS_EXPR, integer_type_node, orig_ndx, t);
3207 gimplify_assign (unshare_expr (ndx), t, pre_p);
3208
3209
3210 /* Check if the argument is in registers:
3211
3212 if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
3213 && !must_pass_in_stack (type))
3214 __array = (AP).__va_reg; */
3215
3216 array = create_tmp_var (ptr_type_node);
3217
3218 lab_over = NULL;
3219 if (!targetm.calls.must_pass_in_stack (TYPE_MODE (type), type))
3220 {
3221 lab_false = create_artificial_label (UNKNOWN_LOCATION);
3222 lab_over = create_artificial_label (UNKNOWN_LOCATION);
3223
3224 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (ndx),
3225 build_int_cst (integer_type_node,
3226 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
3227 t = build3 (COND_EXPR, void_type_node, t,
3228 build1 (GOTO_EXPR, void_type_node, lab_false),
3229 NULL_TREE);
3230 gimplify_and_add (t, pre_p);
3231
3232 gimplify_assign (unshare_expr (array), reg, pre_p);
3233
3234 t = build1 (GOTO_EXPR, void_type_node, lab_over);
3235 gimplify_and_add (t, pre_p);
3236
3237 t = build1 (LABEL_EXPR, void_type_node, lab_false);
3238 gimplify_and_add (t, pre_p);
3239 }
3240
3241
3242 /* ...otherwise, the argument is on the stack (never split between
3243 registers and the stack -- change __va_ndx if necessary):
3244
3245 else
3246 {
3247 if (orig_ndx <= __MAX_ARGS_IN_REGISTERS * 4)
3248 (AP).__va_ndx = 32 + __va_size (TYPE);
3249 __array = (AP).__va_stk;
3250 } */
3251
3252 lab_false2 = create_artificial_label (UNKNOWN_LOCATION);
3253
3254 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (orig_ndx),
3255 build_int_cst (integer_type_node,
3256 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
3257 t = build3 (COND_EXPR, void_type_node, t,
3258 build1 (GOTO_EXPR, void_type_node, lab_false2),
3259 NULL_TREE);
3260 gimplify_and_add (t, pre_p);
3261
3262 t = size_binop (PLUS_EXPR, unshare_expr (va_size), size_int (32));
3263 t = fold_convert (integer_type_node, t);
3264 gimplify_assign (unshare_expr (ndx), t, pre_p);
3265
3266 t = build1 (LABEL_EXPR, void_type_node, lab_false2);
3267 gimplify_and_add (t, pre_p);
3268
3269 gimplify_assign (array, stk, pre_p);
3270
3271 if (lab_over)
3272 {
3273 t = build1 (LABEL_EXPR, void_type_node, lab_over);
3274 gimplify_and_add (t, pre_p);
3275 }
3276
3277
3278 /* Given the base array pointer (__array) and index to the subsequent
3279 argument (__va_ndx), find the address:
3280
3281 __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
3282 ? sizeof (TYPE)
3283 : __va_size (TYPE))
3284
3285 The results are endian-dependent because values smaller than one word
3286 are aligned differently. */
3287
3288
3289 if (BYTES_BIG_ENDIAN && TREE_CODE (type_size) == INTEGER_CST)
3290 {
3291 t = fold_build2 (GE_EXPR, boolean_type_node, unshare_expr (type_size),
3292 size_int (PARM_BOUNDARY / BITS_PER_UNIT));
3293 t = fold_build3 (COND_EXPR, sizetype, t, unshare_expr (va_size),
3294 unshare_expr (type_size));
3295 size = t;
3296 }
3297 else
3298 size = unshare_expr (va_size);
3299
3300 t = fold_convert (sizetype, unshare_expr (ndx));
3301 t = build2 (MINUS_EXPR, sizetype, t, size);
3302 addr = fold_build_pointer_plus (unshare_expr (array), t);
3303
3304 addr = fold_convert (build_pointer_type (type), addr);
3305 if (indirect)
3306 addr = build_va_arg_indirect_ref (addr);
3307 return build_va_arg_indirect_ref (addr);
3308 }
3309
3310
3311 /* Builtins. */
3312
3313 enum xtensa_builtin
3314 {
3315 XTENSA_BUILTIN_UMULSIDI3,
3316 XTENSA_BUILTIN_max
3317 };
3318
3319
3320 static void
3321 xtensa_init_builtins (void)
3322 {
3323 tree ftype, decl;
3324
3325 ftype = build_function_type_list (unsigned_intDI_type_node,
3326 unsigned_intSI_type_node,
3327 unsigned_intSI_type_node, NULL_TREE);
3328
3329 decl = add_builtin_function ("__builtin_umulsidi3", ftype,
3330 XTENSA_BUILTIN_UMULSIDI3, BUILT_IN_MD,
3331 "__umulsidi3", NULL_TREE);
3332 TREE_NOTHROW (decl) = 1;
3333 TREE_READONLY (decl) = 1;
3334 }
3335
3336
3337 static tree
3338 xtensa_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED, tree *args,
3339 bool ignore ATTRIBUTE_UNUSED)
3340 {
3341 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3342 tree arg0, arg1;
3343
3344 switch (fcode)
3345 {
3346 case XTENSA_BUILTIN_UMULSIDI3:
3347 arg0 = args[0];
3348 arg1 = args[1];
3349 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3350 || TARGET_MUL32_HIGH)
3351 return fold_build2 (MULT_EXPR, unsigned_intDI_type_node,
3352 fold_convert (unsigned_intDI_type_node, arg0),
3353 fold_convert (unsigned_intDI_type_node, arg1));
3354 break;
3355
3356 default:
3357 internal_error ("bad builtin code");
3358 break;
3359 }
3360
3361 return NULL;
3362 }
3363
3364
3365 static rtx
3366 xtensa_expand_builtin (tree exp, rtx target,
3367 rtx subtarget ATTRIBUTE_UNUSED,
3368 machine_mode mode ATTRIBUTE_UNUSED,
3369 int ignore)
3370 {
3371 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
3372 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3373
3374 switch (fcode)
3375 {
3376 case XTENSA_BUILTIN_UMULSIDI3:
3377 /* The umulsidi3 builtin is just a mechanism to avoid calling the real
3378 __umulsidi3 function when the Xtensa configuration can directly
3379 implement it. If not, just call the function. */
3380 return expand_call (exp, target, ignore);
3381
3382 default:
3383 internal_error ("bad builtin code");
3384 }
3385 return NULL_RTX;
3386 }
3387
3388 /* Worker function for TARGET_PREFERRED_RELOAD_CLASS. */
3389
3390 static reg_class_t
3391 xtensa_preferred_reload_class (rtx x, reg_class_t rclass)
3392 {
3393 if (CONSTANT_P (x) && CONST_DOUBLE_P (x))
3394 return NO_REGS;
3395
3396 /* Don't use the stack pointer or hard frame pointer for reloads!
3397 The hard frame pointer would normally be OK except that it may
3398 briefly hold an incoming argument in the prologue, and reload
3399 won't know that it is live because the hard frame pointer is
3400 treated specially. */
3401
3402 if (rclass == AR_REGS || rclass == GR_REGS)
3403 return RL_REGS;
3404
3405 return rclass;
3406 }
3407
3408 /* Worker function for TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
3409
3410 static reg_class_t
3411 xtensa_preferred_output_reload_class (rtx x ATTRIBUTE_UNUSED,
3412 reg_class_t rclass)
3413 {
3414 /* Don't use the stack pointer or hard frame pointer for reloads!
3415 The hard frame pointer would normally be OK except that it may
3416 briefly hold an incoming argument in the prologue, and reload
3417 won't know that it is live because the hard frame pointer is
3418 treated specially. */
3419
3420 if (rclass == AR_REGS || rclass == GR_REGS)
3421 return RL_REGS;
3422
3423 return rclass;
3424 }
3425
3426 /* Worker function for TARGET_SECONDARY_RELOAD. */
3427
3428 static reg_class_t
3429 xtensa_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
3430 machine_mode mode, secondary_reload_info *sri)
3431 {
3432 int regno;
3433
3434 if (in_p && constantpool_mem_p (x))
3435 {
3436 if (rclass == FP_REGS)
3437 return RL_REGS;
3438
3439 if (mode == QImode)
3440 sri->icode = CODE_FOR_reloadqi_literal;
3441 else if (mode == HImode)
3442 sri->icode = CODE_FOR_reloadhi_literal;
3443 }
3444
3445 regno = xt_true_regnum (x);
3446 if (ACC_REG_P (regno))
3447 return ((rclass == GR_REGS || rclass == RL_REGS) ? NO_REGS : RL_REGS);
3448 if (rclass == ACC_REG)
3449 return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
3450
3451 return NO_REGS;
3452 }
3453
3454
3455 void
3456 order_regs_for_local_alloc (void)
3457 {
3458 if (!leaf_function_p ())
3459 {
3460 static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
3461 REG_ALLOC_ORDER;
3462 static const int reg_nonleaf_alloc_order_call0[FIRST_PSEUDO_REGISTER] =
3463 {
3464 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 12, 13, 14, 15,
3465 18,
3466 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
3467 0, 1, 16, 17,
3468 35,
3469 };
3470
3471 memcpy (reg_alloc_order, TARGET_WINDOWED_ABI ?
3472 reg_nonleaf_alloc_order : reg_nonleaf_alloc_order_call0,
3473 FIRST_PSEUDO_REGISTER * sizeof (int));
3474 }
3475 else
3476 {
3477 int i, num_arg_regs;
3478 int nxt = 0;
3479
3480 /* Use the AR registers in increasing order (skipping a0 and a1)
3481 but save the incoming argument registers for a last resort. */
3482 num_arg_regs = crtl->args.info.arg_words;
3483 if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
3484 num_arg_regs = MAX_ARGS_IN_REGISTERS;
3485 for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
3486 reg_alloc_order[nxt++] = i + num_arg_regs;
3487 for (i = 0; i < num_arg_regs; i++)
3488 reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
3489
3490 /* List the coprocessor registers in order. */
3491 for (i = 0; i < BR_REG_NUM; i++)
3492 reg_alloc_order[nxt++] = BR_REG_FIRST + i;
3493
3494 /* List the FP registers in order for now. */
3495 for (i = 0; i < 16; i++)
3496 reg_alloc_order[nxt++] = FP_REG_FIRST + i;
3497
3498 /* GCC requires that we list *all* the registers.... */
3499 reg_alloc_order[nxt++] = 0; /* a0 = return address */
3500 reg_alloc_order[nxt++] = 1; /* a1 = stack pointer */
3501 reg_alloc_order[nxt++] = 16; /* pseudo frame pointer */
3502 reg_alloc_order[nxt++] = 17; /* pseudo arg pointer */
3503
3504 reg_alloc_order[nxt++] = ACC_REG_FIRST; /* MAC16 accumulator */
3505 }
3506 }
3507
3508
3509 /* Some Xtensa targets support multiple bss sections. If the section
3510 name ends with ".bss", add SECTION_BSS to the flags. */
3511
3512 static unsigned int
3513 xtensa_multibss_section_type_flags (tree decl, const char *name, int reloc)
3514 {
3515 unsigned int flags = default_section_type_flags (decl, name, reloc);
3516 const char *suffix;
3517
3518 suffix = strrchr (name, '.');
3519 if (suffix && strcmp (suffix, ".bss") == 0)
3520 {
3521 if (!decl || (TREE_CODE (decl) == VAR_DECL
3522 && DECL_INITIAL (decl) == NULL_TREE))
3523 flags |= SECTION_BSS; /* @nobits */
3524 else
3525 warning (0, "only uninitialized variables can be placed in a "
3526 ".bss section");
3527 }
3528
3529 return flags;
3530 }
3531
3532
3533 /* The literal pool stays with the function. */
3534
3535 static section *
3536 xtensa_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
3537 rtx x ATTRIBUTE_UNUSED,
3538 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
3539 {
3540 return function_section (current_function_decl);
3541 }
3542
3543 /* Worker function for TARGET_REGISTER_MOVE_COST. */
3544
3545 static int
3546 xtensa_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3547 reg_class_t from, reg_class_t to)
3548 {
3549 if (from == to && from != BR_REGS && to != BR_REGS)
3550 return 2;
3551 else if (reg_class_subset_p (from, AR_REGS)
3552 && reg_class_subset_p (to, AR_REGS))
3553 return 2;
3554 else if (reg_class_subset_p (from, AR_REGS) && to == ACC_REG)
3555 return 3;
3556 else if (from == ACC_REG && reg_class_subset_p (to, AR_REGS))
3557 return 3;
3558 else
3559 return 10;
3560 }
3561
3562 /* Worker function for TARGET_MEMORY_MOVE_COST. */
3563
3564 static int
3565 xtensa_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3566 reg_class_t rclass ATTRIBUTE_UNUSED,
3567 bool in ATTRIBUTE_UNUSED)
3568 {
3569 return 4;
3570 }
3571
3572 /* Compute a (partial) cost for rtx X. Return true if the complete
3573 cost has been computed, and false if subexpressions should be
3574 scanned. In either case, *TOTAL contains the cost result. */
3575
3576 static bool
3577 xtensa_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3578 int *total, bool speed ATTRIBUTE_UNUSED)
3579 {
3580 switch (code)
3581 {
3582 case CONST_INT:
3583 switch (outer_code)
3584 {
3585 case SET:
3586 if (xtensa_simm12b (INTVAL (x)))
3587 {
3588 *total = 4;
3589 return true;
3590 }
3591 break;
3592 case PLUS:
3593 if (xtensa_simm8 (INTVAL (x))
3594 || xtensa_simm8x256 (INTVAL (x)))
3595 {
3596 *total = 0;
3597 return true;
3598 }
3599 break;
3600 case AND:
3601 if (xtensa_mask_immediate (INTVAL (x)))
3602 {
3603 *total = 0;
3604 return true;
3605 }
3606 break;
3607 case COMPARE:
3608 if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
3609 {
3610 *total = 0;
3611 return true;
3612 }
3613 break;
3614 case ASHIFT:
3615 case ASHIFTRT:
3616 case LSHIFTRT:
3617 case ROTATE:
3618 case ROTATERT:
3619 /* No way to tell if X is the 2nd operand so be conservative. */
3620 default: break;
3621 }
3622 if (xtensa_simm12b (INTVAL (x)))
3623 *total = 5;
3624 else if (TARGET_CONST16)
3625 *total = COSTS_N_INSNS (2);
3626 else
3627 *total = 6;
3628 return true;
3629
3630 case CONST:
3631 case LABEL_REF:
3632 case SYMBOL_REF:
3633 if (TARGET_CONST16)
3634 *total = COSTS_N_INSNS (2);
3635 else
3636 *total = 5;
3637 return true;
3638
3639 case CONST_DOUBLE:
3640 if (TARGET_CONST16)
3641 *total = COSTS_N_INSNS (4);
3642 else
3643 *total = 7;
3644 return true;
3645
3646 case MEM:
3647 {
3648 int num_words =
3649 (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) ? 2 : 1;
3650
3651 if (memory_address_p (GET_MODE (x), XEXP ((x), 0)))
3652 *total = COSTS_N_INSNS (num_words);
3653 else
3654 *total = COSTS_N_INSNS (2*num_words);
3655 return true;
3656 }
3657
3658 case FFS:
3659 case CTZ:
3660 *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
3661 return true;
3662
3663 case CLZ:
3664 *total = COSTS_N_INSNS (TARGET_NSA ? 1 : 50);
3665 return true;
3666
3667 case NOT:
3668 *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 3 : 2);
3669 return true;
3670
3671 case AND:
3672 case IOR:
3673 case XOR:
3674 if (GET_MODE (x) == DImode)
3675 *total = COSTS_N_INSNS (2);
3676 else
3677 *total = COSTS_N_INSNS (1);
3678 return true;
3679
3680 case ASHIFT:
3681 case ASHIFTRT:
3682 case LSHIFTRT:
3683 if (GET_MODE (x) == DImode)
3684 *total = COSTS_N_INSNS (50);
3685 else
3686 *total = COSTS_N_INSNS (1);
3687 return true;
3688
3689 case ABS:
3690 {
3691 machine_mode xmode = GET_MODE (x);
3692 if (xmode == SFmode)
3693 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3694 else if (xmode == DFmode)
3695 *total = COSTS_N_INSNS (50);
3696 else
3697 *total = COSTS_N_INSNS (4);
3698 return true;
3699 }
3700
3701 case PLUS:
3702 case MINUS:
3703 {
3704 machine_mode xmode = GET_MODE (x);
3705 if (xmode == SFmode)
3706 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3707 else if (xmode == DFmode || xmode == DImode)
3708 *total = COSTS_N_INSNS (50);
3709 else
3710 *total = COSTS_N_INSNS (1);
3711 return true;
3712 }
3713
3714 case NEG:
3715 *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 4 : 2);
3716 return true;
3717
3718 case MULT:
3719 {
3720 machine_mode xmode = GET_MODE (x);
3721 if (xmode == SFmode)
3722 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
3723 else if (xmode == DFmode)
3724 *total = COSTS_N_INSNS (50);
3725 else if (xmode == DImode)
3726 *total = COSTS_N_INSNS (TARGET_MUL32_HIGH ? 10 : 50);
3727 else if (TARGET_MUL32)
3728 *total = COSTS_N_INSNS (4);
3729 else if (TARGET_MAC16)
3730 *total = COSTS_N_INSNS (16);
3731 else if (TARGET_MUL16)
3732 *total = COSTS_N_INSNS (12);
3733 else
3734 *total = COSTS_N_INSNS (50);
3735 return true;
3736 }
3737
3738 case DIV:
3739 case MOD:
3740 {
3741 machine_mode xmode = GET_MODE (x);
3742 if (xmode == SFmode)
3743 {
3744 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
3745 return true;
3746 }
3747 else if (xmode == DFmode)
3748 {
3749 *total = COSTS_N_INSNS (50);
3750 return true;
3751 }
3752 }
3753 /* Fall through. */
3754
3755 case UDIV:
3756 case UMOD:
3757 {
3758 machine_mode xmode = GET_MODE (x);
3759 if (xmode == DImode)
3760 *total = COSTS_N_INSNS (50);
3761 else if (TARGET_DIV32)
3762 *total = COSTS_N_INSNS (32);
3763 else
3764 *total = COSTS_N_INSNS (50);
3765 return true;
3766 }
3767
3768 case SQRT:
3769 if (GET_MODE (x) == SFmode)
3770 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
3771 else
3772 *total = COSTS_N_INSNS (50);
3773 return true;
3774
3775 case SMIN:
3776 case UMIN:
3777 case SMAX:
3778 case UMAX:
3779 *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
3780 return true;
3781
3782 case SIGN_EXTRACT:
3783 case SIGN_EXTEND:
3784 *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
3785 return true;
3786
3787 case ZERO_EXTRACT:
3788 case ZERO_EXTEND:
3789 *total = COSTS_N_INSNS (1);
3790 return true;
3791
3792 default:
3793 return false;
3794 }
3795 }
3796
3797 /* Worker function for TARGET_RETURN_IN_MEMORY. */
3798
3799 static bool
3800 xtensa_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3801 {
3802 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
3803 > 4 * UNITS_PER_WORD);
3804 }
3805
3806 /* Worker function for TARGET_FUNCTION_VALUE. */
3807
3808 rtx
3809 xtensa_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
3810 bool outgoing)
3811 {
3812 return gen_rtx_REG ((INTEGRAL_TYPE_P (valtype)
3813 && TYPE_PRECISION (valtype) < BITS_PER_WORD)
3814 ? SImode : TYPE_MODE (valtype),
3815 outgoing ? GP_OUTGOING_RETURN : GP_RETURN);
3816 }
3817
3818 /* Worker function for TARGET_LIBCALL_VALUE. */
3819
3820 static rtx
3821 xtensa_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
3822 {
3823 return gen_rtx_REG ((GET_MODE_CLASS (mode) == MODE_INT
3824 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3825 ? SImode : mode, GP_RETURN);
3826 }
3827
3828 /* Worker function TARGET_FUNCTION_VALUE_REGNO_P. */
3829
3830 static bool
3831 xtensa_function_value_regno_p (const unsigned int regno)
3832 {
3833 return (regno == GP_RETURN);
3834 }
3835
3836 /* The static chain is passed in memory. Provide rtx giving 'mem'
3837 expressions that denote where they are stored. */
3838
3839 static rtx
3840 xtensa_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
3841 {
3842 if (TARGET_WINDOWED_ABI)
3843 {
3844 rtx base = incoming_p ? arg_pointer_rtx : stack_pointer_rtx;
3845 return gen_frame_mem (Pmode, plus_constant (Pmode, base,
3846 -5 * UNITS_PER_WORD));
3847 }
3848 else
3849 return gen_rtx_REG (Pmode, A8_REG);
3850 }
3851
3852
3853 /* TRAMPOLINE_TEMPLATE: For Xtensa, the trampoline must perform an ENTRY
3854 instruction with a minimal stack frame in order to get some free
3855 registers. Once the actual call target is known, the proper stack frame
3856 size is extracted from the ENTRY instruction at the target and the
3857 current frame is adjusted to match. The trampoline then transfers
3858 control to the instruction following the ENTRY at the target. Note:
3859 this assumes that the target begins with an ENTRY instruction. */
3860
3861 static void
3862 xtensa_asm_trampoline_template (FILE *stream)
3863 {
3864 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3865
3866 fprintf (stream, "\t.begin no-transform\n");
3867
3868 if (TARGET_WINDOWED_ABI)
3869 {
3870 fprintf (stream, "\tentry\tsp, %d\n", MIN_FRAME_SIZE);
3871
3872 if (use_call0)
3873 {
3874 /* Save the return address. */
3875 fprintf (stream, "\tmov\ta10, a0\n");
3876
3877 /* Use a CALL0 instruction to skip past the constants and in the
3878 process get the PC into A0. This allows PC-relative access to
3879 the constants without relying on L32R. */
3880 fprintf (stream, "\tcall0\t.Lskipconsts\n");
3881 }
3882 else
3883 fprintf (stream, "\tj\t.Lskipconsts\n");
3884
3885 fprintf (stream, "\t.align\t4\n");
3886 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
3887 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
3888 fprintf (stream, ".Lskipconsts:\n");
3889
3890 /* Load the static chain and function address from the trampoline. */
3891 if (use_call0)
3892 {
3893 fprintf (stream, "\taddi\ta0, a0, 3\n");
3894 fprintf (stream, "\tl32i\ta9, a0, 0\n");
3895 fprintf (stream, "\tl32i\ta8, a0, 4\n");
3896 }
3897 else
3898 {
3899 fprintf (stream, "\tl32r\ta9, .Lchainval\n");
3900 fprintf (stream, "\tl32r\ta8, .Lfnaddr\n");
3901 }
3902
3903 /* Store the static chain. */
3904 fprintf (stream, "\ts32i\ta9, sp, %d\n", MIN_FRAME_SIZE - 20);
3905
3906 /* Set the proper stack pointer value. */
3907 fprintf (stream, "\tl32i\ta9, a8, 0\n");
3908 fprintf (stream, "\textui\ta9, a9, %d, 12\n",
3909 TARGET_BIG_ENDIAN ? 8 : 12);
3910 fprintf (stream, "\tslli\ta9, a9, 3\n");
3911 fprintf (stream, "\taddi\ta9, a9, %d\n", -MIN_FRAME_SIZE);
3912 fprintf (stream, "\tsub\ta9, sp, a9\n");
3913 fprintf (stream, "\tmovsp\tsp, a9\n");
3914
3915 if (use_call0)
3916 /* Restore the return address. */
3917 fprintf (stream, "\tmov\ta0, a10\n");
3918
3919 /* Jump to the instruction following the ENTRY. */
3920 fprintf (stream, "\taddi\ta8, a8, 3\n");
3921 fprintf (stream, "\tjx\ta8\n");
3922
3923 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
3924 if (use_call0)
3925 fprintf (stream, "\t.byte\t0\n");
3926 else
3927 fprintf (stream, "\tnop\n");
3928 }
3929 else
3930 {
3931 if (use_call0)
3932 {
3933 /* Save the return address. */
3934 fprintf (stream, "\tmov\ta10, a0\n");
3935
3936 /* Use a CALL0 instruction to skip past the constants and in the
3937 process get the PC into A0. This allows PC-relative access to
3938 the constants without relying on L32R. */
3939 fprintf (stream, "\tcall0\t.Lskipconsts\n");
3940 }
3941 else
3942 fprintf (stream, "\tj\t.Lskipconsts\n");
3943
3944 fprintf (stream, "\t.align\t4\n");
3945 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
3946 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
3947 fprintf (stream, ".Lskipconsts:\n");
3948
3949 /* Load the static chain and function address from the trampoline. */
3950 if (use_call0)
3951 {
3952 fprintf (stream, "\taddi\ta0, a0, 3\n");
3953 fprintf (stream, "\tl32i\ta8, a0, 0\n");
3954 fprintf (stream, "\tl32i\ta9, a0, 4\n");
3955 fprintf (stream, "\tmov\ta0, a10\n");
3956 }
3957 else
3958 {
3959 fprintf (stream, "\tl32r\ta8, .Lchainval\n");
3960 fprintf (stream, "\tl32r\ta9, .Lfnaddr\n");
3961 }
3962 fprintf (stream, "\tjx\ta9\n");
3963
3964 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
3965 if (use_call0)
3966 fprintf (stream, "\t.byte\t0\n");
3967 else
3968 fprintf (stream, "\tnop\n");
3969 }
3970 fprintf (stream, "\t.end no-transform\n");
3971 }
3972
3973 static void
3974 xtensa_trampoline_init (rtx m_tramp, tree fndecl, rtx chain)
3975 {
3976 rtx func = XEXP (DECL_RTL (fndecl), 0);
3977 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3978 int chain_off;
3979 int func_off;
3980
3981 if (TARGET_WINDOWED_ABI)
3982 {
3983 chain_off = use_call0 ? 12 : 8;
3984 func_off = use_call0 ? 16 : 12;
3985 }
3986 else
3987 {
3988 chain_off = use_call0 ? 8 : 4;
3989 func_off = use_call0 ? 12 : 8;
3990 }
3991
3992 emit_block_move (m_tramp, assemble_trampoline_template (),
3993 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
3994
3995 emit_move_insn (adjust_address (m_tramp, SImode, chain_off), chain);
3996 emit_move_insn (adjust_address (m_tramp, SImode, func_off), func);
3997 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_sync_caches"),
3998 LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
3999 }
4000
4001 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
4002
4003 static bool
4004 xtensa_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
4005 {
4006 return !xtensa_tls_referenced_p (x);
4007 }
4008
4009 /* Implement TARGET_CAN_USE_DOLOOP_P. */
4010
4011 static bool
4012 xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
4013 unsigned int loop_depth, bool entered_at_top)
4014 {
4015 /* Considering limitations in the hardware, only use doloop
4016 for innermost loops which must be entered from the top. */
4017 if (loop_depth > 1 || !entered_at_top)
4018 return false;
4019
4020 return true;
4021 }
4022
4023 /* NULL if INSN insn is valid within a low-overhead loop.
4024 Otherwise return why doloop cannot be applied. */
4025
4026 static const char *
4027 xtensa_invalid_within_doloop (const rtx_insn *insn)
4028 {
4029 if (CALL_P (insn))
4030 return "Function call in the loop.";
4031
4032 if (JUMP_P (insn) && INSN_CODE (insn) == CODE_FOR_return)
4033 return "Return from a call instruction in the loop.";
4034
4035 return NULL;
4036 }
4037
4038 /* Optimize LOOP. */
4039
4040 #if TARGET_LOOPS
4041
4042 static bool
4043 hwloop_optimize (hwloop_info loop)
4044 {
4045 int i;
4046 edge entry_edge;
4047 basic_block entry_bb;
4048 rtx iter_reg;
4049 rtx_insn *insn, *seq, *entry_after;
4050
4051 if (loop->depth > 1)
4052 {
4053 if (dump_file)
4054 fprintf (dump_file, ";; loop %d is not innermost\n",
4055 loop->loop_no);
4056 return false;
4057 }
4058
4059 if (!loop->incoming_dest)
4060 {
4061 if (dump_file)
4062 fprintf (dump_file, ";; loop %d has more than one entry\n",
4063 loop->loop_no);
4064 return false;
4065 }
4066
4067 if (loop->incoming_dest != loop->head)
4068 {
4069 if (dump_file)
4070 fprintf (dump_file, ";; loop %d is not entered from head\n",
4071 loop->loop_no);
4072 return false;
4073 }
4074
4075 if (loop->has_call || loop->has_asm)
4076 {
4077 if (dump_file)
4078 fprintf (dump_file, ";; loop %d has invalid insn\n",
4079 loop->loop_no);
4080 return false;
4081 }
4082
4083 /* Scan all the blocks to make sure they don't use iter_reg. */
4084 if (loop->iter_reg_used || loop->iter_reg_used_outside)
4085 {
4086 if (dump_file)
4087 fprintf (dump_file, ";; loop %d uses iterator\n",
4088 loop->loop_no);
4089 return false;
4090 }
4091
4092 /* Check if start_label appears before doloop_end. */
4093 insn = loop->start_label;
4094 while (insn && insn != loop->loop_end)
4095 insn = NEXT_INSN (insn);
4096
4097 if (!insn)
4098 {
4099 if (dump_file)
4100 fprintf (dump_file, ";; loop %d start_label not before loop_end\n",
4101 loop->loop_no);
4102 return false;
4103 }
4104
4105 /* Get the loop iteration register. */
4106 iter_reg = loop->iter_reg;
4107
4108 gcc_assert (REG_P (iter_reg));
4109
4110 entry_edge = NULL;
4111
4112 FOR_EACH_VEC_SAFE_ELT (loop->incoming, i, entry_edge)
4113 if (entry_edge->flags & EDGE_FALLTHRU)
4114 break;
4115
4116 if (entry_edge == NULL)
4117 return false;
4118
4119 /* Place the zero_cost_loop_start instruction before the loop. */
4120 entry_bb = entry_edge->src;
4121
4122 start_sequence ();
4123
4124 insn = emit_insn (gen_zero_cost_loop_start (loop->iter_reg,
4125 loop->start_label,
4126 loop->iter_reg));
4127
4128 seq = get_insns ();
4129
4130 if (!single_succ_p (entry_bb) || vec_safe_length (loop->incoming) > 1)
4131 {
4132 basic_block new_bb;
4133 edge e;
4134 edge_iterator ei;
4135
4136 emit_insn_before (seq, BB_HEAD (loop->head));
4137 seq = emit_label_before (gen_label_rtx (), seq);
4138 new_bb = create_basic_block (seq, insn, entry_bb);
4139 FOR_EACH_EDGE (e, ei, loop->incoming)
4140 {
4141 if (!(e->flags & EDGE_FALLTHRU))
4142 redirect_edge_and_branch_force (e, new_bb);
4143 else
4144 redirect_edge_succ (e, new_bb);
4145 }
4146
4147 make_edge (new_bb, loop->head, 0);
4148 }
4149 else
4150 {
4151 entry_after = BB_END (entry_bb);
4152 while (DEBUG_INSN_P (entry_after)
4153 || (NOTE_P (entry_after)
4154 && NOTE_KIND (entry_after) != NOTE_INSN_BASIC_BLOCK))
4155 entry_after = PREV_INSN (entry_after);
4156
4157 emit_insn_after (seq, entry_after);
4158 }
4159
4160 end_sequence ();
4161
4162 return true;
4163 }
4164
4165 /* A callback for the hw-doloop pass. Called when a loop we have discovered
4166 turns out not to be optimizable; we have to split the loop_end pattern into
4167 a subtract and a test. */
4168
4169 static void
4170 hwloop_fail (hwloop_info loop)
4171 {
4172 rtx test;
4173 rtx_insn *insn = loop->loop_end;
4174
4175 emit_insn_before (gen_addsi3 (loop->iter_reg,
4176 loop->iter_reg,
4177 constm1_rtx),
4178 loop->loop_end);
4179
4180 test = gen_rtx_NE (VOIDmode, loop->iter_reg, const0_rtx);
4181 insn = emit_jump_insn_before (gen_cbranchsi4 (test,
4182 loop->iter_reg, const0_rtx,
4183 loop->start_label),
4184 loop->loop_end);
4185
4186 JUMP_LABEL (insn) = loop->start_label;
4187 LABEL_NUSES (loop->start_label)++;
4188 delete_insn (loop->loop_end);
4189 }
4190
4191 /* A callback for the hw-doloop pass. This function examines INSN; if
4192 it is a doloop_end pattern we recognize, return the reg rtx for the
4193 loop counter. Otherwise, return NULL_RTX. */
4194
4195 static rtx
4196 hwloop_pattern_reg (rtx_insn *insn)
4197 {
4198 rtx reg;
4199
4200 if (!JUMP_P (insn) || recog_memoized (insn) != CODE_FOR_loop_end)
4201 return NULL_RTX;
4202
4203 reg = SET_DEST (XVECEXP (PATTERN (insn), 0, 1));
4204 if (!REG_P (reg))
4205 return NULL_RTX;
4206
4207 return reg;
4208 }
4209
4210
4211 static struct hw_doloop_hooks xtensa_doloop_hooks =
4212 {
4213 hwloop_pattern_reg,
4214 hwloop_optimize,
4215 hwloop_fail
4216 };
4217
4218 /* Run from machine_dependent_reorg, this pass looks for doloop_end insns
4219 and tries to rewrite the RTL of these loops so that proper Xtensa
4220 hardware loops are generated. */
4221
4222 static void
4223 xtensa_reorg_loops (void)
4224 {
4225 reorg_loops (false, &xtensa_doloop_hooks);
4226 }
4227 #else
4228 static inline void
4229 xtensa_reorg_loops (void)
4230 {
4231 }
4232 #endif
4233
4234 /* Implement the TARGET_MACHINE_DEPENDENT_REORG pass. */
4235
4236 static void
4237 xtensa_reorg (void)
4238 {
4239 /* We are freeing block_for_insn in the toplev to keep compatibility
4240 with old MDEP_REORGS that are not CFG based. Recompute it now. */
4241 compute_bb_for_insn ();
4242
4243 df_analyze ();
4244
4245 /* Doloop optimization. */
4246 xtensa_reorg_loops ();
4247 }
4248
4249 /* Update register usage after having seen the compiler flags. */
4250
4251 static void
4252 xtensa_conditional_register_usage (void)
4253 {
4254 unsigned i, c_mask;
4255
4256 c_mask = TARGET_WINDOWED_ABI ? (1 << 1) : (1 << 2);
4257
4258 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4259 {
4260 /* Set/reset conditionally defined registers from
4261 CALL_USED_REGISTERS initializer. */
4262 if (call_used_regs[i] > 1)
4263 call_used_regs[i] = !!(call_used_regs[i] & c_mask);
4264 }
4265
4266 /* Remove hard FP register from the preferred reload registers set. */
4267 CLEAR_HARD_REG_BIT (reg_class_contents[(int)RL_REGS],
4268 HARD_FRAME_POINTER_REGNUM);
4269 }
4270
4271 /* Map hard register number to register class */
4272
4273 enum reg_class xtensa_regno_to_class (int regno)
4274 {
4275 static const enum reg_class regno_to_class[FIRST_PSEUDO_REGISTER] =
4276 {
4277 RL_REGS, SP_REG, RL_REGS, RL_REGS,
4278 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
4279 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
4280 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
4281 AR_REGS, AR_REGS, BR_REGS,
4282 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4283 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4284 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4285 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4286 ACC_REG,
4287 };
4288
4289 if (regno == HARD_FRAME_POINTER_REGNUM)
4290 return GR_REGS;
4291 else
4292 return regno_to_class[regno];
4293 }
4294
4295 #include "gt-xtensa.h"