Turn HARD_REGNO_NREGS into a target hook
[official-gcc.git] / gcc / config / xtensa / xtensa.c
blob9c11ae043b528f5f92aabc017a5c7440ef64da81
1 /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
2 Copyright (C) 2001-2017 Free Software Foundation, Inc.
3 Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
5 This file is part of GCC.
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.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "df.h"
31 #include "memmodel.h"
32 #include "tm_p.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "optabs.h"
36 #include "regs.h"
37 #include "emit-rtl.h"
38 #include "recog.h"
39 #include "diagnostic-core.h"
40 #include "cfgrtl.h"
41 #include "output.h"
42 #include "fold-const.h"
43 #include "stor-layout.h"
44 #include "calls.h"
45 #include "varasm.h"
46 #include "alias.h"
47 #include "explow.h"
48 #include "expr.h"
49 #include "reload.h"
50 #include "langhooks.h"
51 #include "gimplify.h"
52 #include "builtins.h"
53 #include "dumpfile.h"
54 #include "hw-doloop.h"
55 #include "rtl-iter.h"
57 /* This file should be included last. */
58 #include "target-def.h"
60 /* Enumeration for all of the relational tests, so that we can build
61 arrays indexed by the test type, and not worry about the order
62 of EQ, NE, etc. */
64 enum internal_test
66 ITEST_EQ,
67 ITEST_NE,
68 ITEST_GT,
69 ITEST_GE,
70 ITEST_LT,
71 ITEST_LE,
72 ITEST_GTU,
73 ITEST_GEU,
74 ITEST_LTU,
75 ITEST_LEU,
76 ITEST_MAX
79 /* Array giving truth value on whether or not a given hard register
80 can support a given mode. */
81 static char xtensa_hard_regno_mode_ok_p
82 [(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
84 /* Largest block move to handle in-line. */
85 #define LARGEST_MOVE_RATIO 15
87 /* Define the structure for the machine field in struct function. */
88 struct GTY(()) machine_function
90 int accesses_prev_frame;
91 bool need_a7_copy;
92 bool vararg_a7;
93 rtx vararg_a7_copy;
94 rtx_insn *set_frame_ptr_insn;
95 /* Current frame size calculated by compute_frame_size. */
96 unsigned current_frame_size;
97 /* Callee-save area size in the current frame calculated by
98 compute_frame_size. */
99 int callee_save_size;
100 bool frame_laid_out;
101 bool epilogue_done;
104 /* Vector, indexed by hard register number, which contains 1 for a
105 register that is allowable in a candidate for leaf function
106 treatment. */
108 const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
111 1, 1, 1,
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
116 static void xtensa_option_override (void);
117 static enum internal_test map_test_to_internal_test (enum rtx_code);
118 static rtx gen_int_relational (enum rtx_code, rtx, rtx, int *);
119 static rtx gen_float_relational (enum rtx_code, rtx, rtx);
120 static rtx gen_conditional_move (enum rtx_code, machine_mode, rtx, rtx);
121 static rtx fixup_subreg_mem (rtx);
122 static struct machine_function * xtensa_init_machine_status (void);
123 static rtx xtensa_legitimize_tls_address (rtx);
124 static rtx xtensa_legitimize_address (rtx, rtx, machine_mode);
125 static bool xtensa_mode_dependent_address_p (const_rtx, addr_space_t);
126 static bool xtensa_return_in_msb (const_tree);
127 static void printx (FILE *, signed int);
128 static rtx xtensa_builtin_saveregs (void);
129 static bool xtensa_legitimate_address_p (machine_mode, rtx, bool);
130 static unsigned int xtensa_multibss_section_type_flags (tree, const char *,
131 int) ATTRIBUTE_UNUSED;
132 static section *xtensa_select_rtx_section (machine_mode, rtx,
133 unsigned HOST_WIDE_INT);
134 static bool xtensa_rtx_costs (rtx, machine_mode, int, int, int *, bool);
135 static int xtensa_register_move_cost (machine_mode, reg_class_t,
136 reg_class_t);
137 static int xtensa_memory_move_cost (machine_mode, reg_class_t, bool);
138 static tree xtensa_build_builtin_va_list (void);
139 static bool xtensa_return_in_memory (const_tree, const_tree);
140 static tree xtensa_gimplify_va_arg_expr (tree, tree, gimple_seq *,
141 gimple_seq *);
142 static void xtensa_function_arg_advance (cumulative_args_t, machine_mode,
143 const_tree, bool);
144 static rtx xtensa_function_arg (cumulative_args_t, machine_mode,
145 const_tree, bool);
146 static rtx xtensa_function_incoming_arg (cumulative_args_t,
147 machine_mode, const_tree, bool);
148 static rtx xtensa_function_value (const_tree, const_tree, bool);
149 static rtx xtensa_libcall_value (machine_mode, const_rtx);
150 static bool xtensa_function_value_regno_p (const unsigned int);
151 static unsigned int xtensa_function_arg_boundary (machine_mode,
152 const_tree);
153 static void xtensa_init_builtins (void);
154 static tree xtensa_fold_builtin (tree, int, tree *, bool);
155 static rtx xtensa_expand_builtin (tree, rtx, rtx, machine_mode, int);
156 static void xtensa_va_start (tree, rtx);
157 static bool xtensa_frame_pointer_required (void);
158 static rtx xtensa_static_chain (const_tree, bool);
159 static void xtensa_asm_trampoline_template (FILE *);
160 static void xtensa_trampoline_init (rtx, tree, rtx);
161 static bool xtensa_output_addr_const_extra (FILE *, rtx);
162 static bool xtensa_cannot_force_const_mem (machine_mode, rtx);
164 static reg_class_t xtensa_preferred_reload_class (rtx, reg_class_t);
165 static reg_class_t xtensa_preferred_output_reload_class (rtx, reg_class_t);
166 static reg_class_t xtensa_secondary_reload (bool, rtx, reg_class_t,
167 machine_mode,
168 struct secondary_reload_info *);
170 static bool constantpool_address_p (const_rtx addr);
171 static bool xtensa_legitimate_constant_p (machine_mode, rtx);
172 static void xtensa_reorg (void);
173 static bool xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
174 unsigned int, bool);
175 static const char *xtensa_invalid_within_doloop (const rtx_insn *);
177 static bool xtensa_member_type_forces_blk (const_tree,
178 machine_mode mode);
180 static void xtensa_conditional_register_usage (void);
181 static unsigned int xtensa_hard_regno_nregs (unsigned int, machine_mode);
182 static bool xtensa_hard_regno_mode_ok (unsigned int, machine_mode);
183 static bool xtensa_modes_tieable_p (machine_mode, machine_mode);
187 /* These hooks specify assembly directives for creating certain kinds
188 of integer object. */
190 #undef TARGET_ASM_ALIGNED_SI_OP
191 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
193 #undef TARGET_ASM_SELECT_RTX_SECTION
194 #define TARGET_ASM_SELECT_RTX_SECTION xtensa_select_rtx_section
196 #undef TARGET_LEGITIMIZE_ADDRESS
197 #define TARGET_LEGITIMIZE_ADDRESS xtensa_legitimize_address
198 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
199 #define TARGET_MODE_DEPENDENT_ADDRESS_P xtensa_mode_dependent_address_p
201 #undef TARGET_REGISTER_MOVE_COST
202 #define TARGET_REGISTER_MOVE_COST xtensa_register_move_cost
203 #undef TARGET_MEMORY_MOVE_COST
204 #define TARGET_MEMORY_MOVE_COST xtensa_memory_move_cost
205 #undef TARGET_RTX_COSTS
206 #define TARGET_RTX_COSTS xtensa_rtx_costs
207 #undef TARGET_ADDRESS_COST
208 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
210 #undef TARGET_MEMBER_TYPE_FORCES_BLK
211 #define TARGET_MEMBER_TYPE_FORCES_BLK xtensa_member_type_forces_blk
213 #undef TARGET_BUILD_BUILTIN_VA_LIST
214 #define TARGET_BUILD_BUILTIN_VA_LIST xtensa_build_builtin_va_list
216 #undef TARGET_EXPAND_BUILTIN_VA_START
217 #define TARGET_EXPAND_BUILTIN_VA_START xtensa_va_start
219 #undef TARGET_PROMOTE_FUNCTION_MODE
220 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
221 #undef TARGET_PROMOTE_PROTOTYPES
222 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
224 #undef TARGET_RETURN_IN_MEMORY
225 #define TARGET_RETURN_IN_MEMORY xtensa_return_in_memory
226 #undef TARGET_FUNCTION_VALUE
227 #define TARGET_FUNCTION_VALUE xtensa_function_value
228 #undef TARGET_LIBCALL_VALUE
229 #define TARGET_LIBCALL_VALUE xtensa_libcall_value
230 #undef TARGET_FUNCTION_VALUE_REGNO_P
231 #define TARGET_FUNCTION_VALUE_REGNO_P xtensa_function_value_regno_p
233 #undef TARGET_SPLIT_COMPLEX_ARG
234 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
235 #undef TARGET_MUST_PASS_IN_STACK
236 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
237 #undef TARGET_FUNCTION_ARG_ADVANCE
238 #define TARGET_FUNCTION_ARG_ADVANCE xtensa_function_arg_advance
239 #undef TARGET_FUNCTION_ARG
240 #define TARGET_FUNCTION_ARG xtensa_function_arg
241 #undef TARGET_FUNCTION_INCOMING_ARG
242 #define TARGET_FUNCTION_INCOMING_ARG xtensa_function_incoming_arg
243 #undef TARGET_FUNCTION_ARG_BOUNDARY
244 #define TARGET_FUNCTION_ARG_BOUNDARY xtensa_function_arg_boundary
246 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
247 #define TARGET_EXPAND_BUILTIN_SAVEREGS xtensa_builtin_saveregs
248 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
249 #define TARGET_GIMPLIFY_VA_ARG_EXPR xtensa_gimplify_va_arg_expr
251 #undef TARGET_RETURN_IN_MSB
252 #define TARGET_RETURN_IN_MSB xtensa_return_in_msb
254 #undef TARGET_INIT_BUILTINS
255 #define TARGET_INIT_BUILTINS xtensa_init_builtins
256 #undef TARGET_FOLD_BUILTIN
257 #define TARGET_FOLD_BUILTIN xtensa_fold_builtin
258 #undef TARGET_EXPAND_BUILTIN
259 #define TARGET_EXPAND_BUILTIN xtensa_expand_builtin
261 #undef TARGET_PREFERRED_RELOAD_CLASS
262 #define TARGET_PREFERRED_RELOAD_CLASS xtensa_preferred_reload_class
263 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
264 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS xtensa_preferred_output_reload_class
266 #undef TARGET_SECONDARY_RELOAD
267 #define TARGET_SECONDARY_RELOAD xtensa_secondary_reload
269 #undef TARGET_HAVE_TLS
270 #define TARGET_HAVE_TLS (TARGET_THREADPTR && HAVE_AS_TLS)
272 #undef TARGET_CANNOT_FORCE_CONST_MEM
273 #define TARGET_CANNOT_FORCE_CONST_MEM xtensa_cannot_force_const_mem
275 #undef TARGET_LRA_P
276 #define TARGET_LRA_P hook_bool_void_false
278 #undef TARGET_LEGITIMATE_ADDRESS_P
279 #define TARGET_LEGITIMATE_ADDRESS_P xtensa_legitimate_address_p
281 #undef TARGET_FRAME_POINTER_REQUIRED
282 #define TARGET_FRAME_POINTER_REQUIRED xtensa_frame_pointer_required
284 #undef TARGET_STATIC_CHAIN
285 #define TARGET_STATIC_CHAIN xtensa_static_chain
286 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
287 #define TARGET_ASM_TRAMPOLINE_TEMPLATE xtensa_asm_trampoline_template
288 #undef TARGET_TRAMPOLINE_INIT
289 #define TARGET_TRAMPOLINE_INIT xtensa_trampoline_init
291 #undef TARGET_OPTION_OVERRIDE
292 #define TARGET_OPTION_OVERRIDE xtensa_option_override
294 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
295 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA xtensa_output_addr_const_extra
297 #undef TARGET_LEGITIMATE_CONSTANT_P
298 #define TARGET_LEGITIMATE_CONSTANT_P xtensa_legitimate_constant_p
300 #undef TARGET_MACHINE_DEPENDENT_REORG
301 #define TARGET_MACHINE_DEPENDENT_REORG xtensa_reorg
303 #undef TARGET_CAN_USE_DOLOOP_P
304 #define TARGET_CAN_USE_DOLOOP_P xtensa_can_use_doloop_p
306 #undef TARGET_INVALID_WITHIN_DOLOOP
307 #define TARGET_INVALID_WITHIN_DOLOOP xtensa_invalid_within_doloop
309 #undef TARGET_CONDITIONAL_REGISTER_USAGE
310 #define TARGET_CONDITIONAL_REGISTER_USAGE xtensa_conditional_register_usage
312 #undef TARGET_HARD_REGNO_NREGS
313 #define TARGET_HARD_REGNO_NREGS xtensa_hard_regno_nregs
314 #undef TARGET_HARD_REGNO_MODE_OK
315 #define TARGET_HARD_REGNO_MODE_OK xtensa_hard_regno_mode_ok
317 #undef TARGET_MODES_TIEABLE_P
318 #define TARGET_MODES_TIEABLE_P xtensa_modes_tieable_p
320 struct gcc_target targetm = TARGET_INITIALIZER;
323 /* Functions to test Xtensa immediate operand validity. */
325 bool
326 xtensa_simm8 (HOST_WIDE_INT v)
328 return v >= -128 && v <= 127;
332 bool
333 xtensa_simm8x256 (HOST_WIDE_INT v)
335 return (v & 255) == 0 && (v >= -32768 && v <= 32512);
339 bool
340 xtensa_simm12b (HOST_WIDE_INT v)
342 return v >= -2048 && v <= 2047;
346 static bool
347 xtensa_uimm8 (HOST_WIDE_INT v)
349 return v >= 0 && v <= 255;
353 static bool
354 xtensa_uimm8x2 (HOST_WIDE_INT v)
356 return (v & 1) == 0 && (v >= 0 && v <= 510);
360 static bool
361 xtensa_uimm8x4 (HOST_WIDE_INT v)
363 return (v & 3) == 0 && (v >= 0 && v <= 1020);
367 static bool
368 xtensa_b4const (HOST_WIDE_INT v)
370 switch (v)
372 case -1:
373 case 1:
374 case 2:
375 case 3:
376 case 4:
377 case 5:
378 case 6:
379 case 7:
380 case 8:
381 case 10:
382 case 12:
383 case 16:
384 case 32:
385 case 64:
386 case 128:
387 case 256:
388 return true;
390 return false;
394 bool
395 xtensa_b4const_or_zero (HOST_WIDE_INT v)
397 if (v == 0)
398 return true;
399 return xtensa_b4const (v);
403 bool
404 xtensa_b4constu (HOST_WIDE_INT v)
406 switch (v)
408 case 32768:
409 case 65536:
410 case 2:
411 case 3:
412 case 4:
413 case 5:
414 case 6:
415 case 7:
416 case 8:
417 case 10:
418 case 12:
419 case 16:
420 case 32:
421 case 64:
422 case 128:
423 case 256:
424 return true;
426 return false;
430 bool
431 xtensa_mask_immediate (HOST_WIDE_INT v)
433 #define MAX_MASK_SIZE 16
434 int mask_size;
436 for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
438 if ((v & 1) == 0)
439 return false;
440 v = v >> 1;
441 if (v == 0)
442 return true;
445 return false;
449 /* This is just like the standard true_regnum() function except that it
450 works even when reg_renumber is not initialized. */
453 xt_true_regnum (rtx x)
455 if (GET_CODE (x) == REG)
457 if (reg_renumber
458 && REGNO (x) >= FIRST_PSEUDO_REGISTER
459 && reg_renumber[REGNO (x)] >= 0)
460 return reg_renumber[REGNO (x)];
461 return REGNO (x);
463 if (GET_CODE (x) == SUBREG)
465 int base = xt_true_regnum (SUBREG_REG (x));
466 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
467 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
468 GET_MODE (SUBREG_REG (x)),
469 SUBREG_BYTE (x), GET_MODE (x));
471 return -1;
476 xtensa_valid_move (machine_mode mode, rtx *operands)
478 /* Either the destination or source must be a register, and the
479 MAC16 accumulator doesn't count. */
481 if (register_operand (operands[0], mode))
483 int dst_regnum = xt_true_regnum (operands[0]);
485 if (xtensa_tls_referenced_p (operands[1]))
486 return FALSE;
488 /* The stack pointer can only be assigned with a MOVSP opcode. */
489 if (dst_regnum == STACK_POINTER_REGNUM)
490 return !TARGET_WINDOWED_ABI
491 || (mode == SImode
492 && register_operand (operands[1], mode)
493 && !ACC_REG_P (xt_true_regnum (operands[1])));
495 if (!ACC_REG_P (dst_regnum))
496 return true;
498 if (register_operand (operands[1], mode))
500 int src_regnum = xt_true_regnum (operands[1]);
501 if (!ACC_REG_P (src_regnum))
502 return true;
504 return FALSE;
509 smalloffset_mem_p (rtx op)
511 if (GET_CODE (op) == MEM)
513 rtx addr = XEXP (op, 0);
514 if (GET_CODE (addr) == REG)
515 return BASE_REG_P (addr, 0);
516 if (GET_CODE (addr) == PLUS)
518 rtx offset = XEXP (addr, 0);
519 HOST_WIDE_INT val;
520 if (GET_CODE (offset) != CONST_INT)
521 offset = XEXP (addr, 1);
522 if (GET_CODE (offset) != CONST_INT)
523 return FALSE;
525 val = INTVAL (offset);
526 return (val & 3) == 0 && (val >= 0 && val <= 60);
529 return FALSE;
533 static bool
534 constantpool_address_p (const_rtx addr)
536 const_rtx sym = addr;
538 if (GET_CODE (addr) == CONST)
540 rtx offset;
542 /* Only handle (PLUS (SYM, OFFSET)) form. */
543 addr = XEXP (addr, 0);
544 if (GET_CODE (addr) != PLUS)
545 return false;
547 /* Make sure the address is word aligned. */
548 offset = XEXP (addr, 1);
549 if ((!CONST_INT_P (offset))
550 || ((INTVAL (offset) & 3) != 0))
551 return false;
553 sym = XEXP (addr, 0);
556 if ((GET_CODE (sym) == SYMBOL_REF)
557 && CONSTANT_POOL_ADDRESS_P (sym))
558 return true;
559 return false;
564 constantpool_mem_p (rtx op)
566 if (GET_CODE (op) == SUBREG)
567 op = SUBREG_REG (op);
568 if (GET_CODE (op) == MEM)
569 return constantpool_address_p (XEXP (op, 0));
570 return FALSE;
574 /* Return TRUE if X is a thread-local symbol. */
576 static bool
577 xtensa_tls_symbol_p (rtx x)
579 if (! TARGET_HAVE_TLS)
580 return false;
582 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
586 void
587 xtensa_extend_reg (rtx dst, rtx src)
589 rtx temp = gen_reg_rtx (SImode);
590 rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
592 /* Generate paradoxical subregs as needed so that the modes match. */
593 src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
594 dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
596 emit_insn (gen_ashlsi3 (temp, src, shift));
597 emit_insn (gen_ashrsi3 (dst, temp, shift));
601 bool
602 xtensa_mem_offset (unsigned v, machine_mode mode)
604 switch (mode)
606 case E_BLKmode:
607 /* Handle the worst case for block moves. See xtensa_expand_block_move
608 where we emit an optimized block move operation if the block can be
609 moved in < "move_ratio" pieces. The worst case is when the block is
610 aligned but has a size of (3 mod 4) (does this happen?) so that the
611 last piece requires a byte load/store. */
612 return (xtensa_uimm8 (v)
613 && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
615 case E_QImode:
616 return xtensa_uimm8 (v);
618 case E_HImode:
619 return xtensa_uimm8x2 (v);
621 case E_DImode:
622 case E_DFmode:
623 return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
625 default:
626 break;
629 return xtensa_uimm8x4 (v);
633 /* Make normal rtx_code into something we can index from an array. */
635 static enum internal_test
636 map_test_to_internal_test (enum rtx_code test_code)
638 enum internal_test test = ITEST_MAX;
640 switch (test_code)
642 default: break;
643 case EQ: test = ITEST_EQ; break;
644 case NE: test = ITEST_NE; break;
645 case GT: test = ITEST_GT; break;
646 case GE: test = ITEST_GE; break;
647 case LT: test = ITEST_LT; break;
648 case LE: test = ITEST_LE; break;
649 case GTU: test = ITEST_GTU; break;
650 case GEU: test = ITEST_GEU; break;
651 case LTU: test = ITEST_LTU; break;
652 case LEU: test = ITEST_LEU; break;
655 return test;
659 /* Generate the code to compare two integer values. The return value is
660 the comparison expression. */
662 static rtx
663 gen_int_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
664 rtx cmp0, /* first operand to compare */
665 rtx cmp1, /* second operand to compare */
666 int *p_invert /* whether branch needs to reverse test */)
668 struct cmp_info
670 enum rtx_code test_code; /* test code to use in insn */
671 bool (*const_range_p) (HOST_WIDE_INT); /* range check function */
672 int const_add; /* constant to add (convert LE -> LT) */
673 int reverse_regs; /* reverse registers in test */
674 int invert_const; /* != 0 if invert value if cmp1 is constant */
675 int invert_reg; /* != 0 if invert value if cmp1 is register */
676 int unsignedp; /* != 0 for unsigned comparisons. */
679 static struct cmp_info info[ (int)ITEST_MAX ] = {
681 { EQ, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* EQ */
682 { NE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* NE */
684 { LT, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* GT */
685 { GE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* GE */
686 { LT, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* LT */
687 { GE, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* LE */
689 { LTU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* GTU */
690 { GEU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* GEU */
691 { LTU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* LTU */
692 { GEU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* LEU */
695 enum internal_test test;
696 machine_mode mode;
697 struct cmp_info *p_info;
699 test = map_test_to_internal_test (test_code);
700 gcc_assert (test != ITEST_MAX);
702 p_info = &info[ (int)test ];
704 mode = GET_MODE (cmp0);
705 if (mode == VOIDmode)
706 mode = GET_MODE (cmp1);
708 /* Make sure we can handle any constants given to us. */
709 if (GET_CODE (cmp1) == CONST_INT)
711 HOST_WIDE_INT value = INTVAL (cmp1);
712 unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
714 /* if the immediate overflows or does not fit in the immediate field,
715 spill it to a register */
717 if ((p_info->unsignedp ?
718 (uvalue + p_info->const_add > uvalue) :
719 (value + p_info->const_add > value)) != (p_info->const_add > 0))
721 cmp1 = force_reg (mode, cmp1);
723 else if (!(p_info->const_range_p) (value + p_info->const_add))
725 cmp1 = force_reg (mode, cmp1);
728 else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
730 cmp1 = force_reg (mode, cmp1);
733 /* See if we need to invert the result. */
734 *p_invert = ((GET_CODE (cmp1) == CONST_INT)
735 ? p_info->invert_const
736 : p_info->invert_reg);
738 /* Comparison to constants, may involve adding 1 to change a LT into LE.
739 Comparison between two registers, may involve switching operands. */
740 if (GET_CODE (cmp1) == CONST_INT)
742 if (p_info->const_add != 0)
743 cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
746 else if (p_info->reverse_regs)
748 rtx temp = cmp0;
749 cmp0 = cmp1;
750 cmp1 = temp;
753 return gen_rtx_fmt_ee (p_info->test_code, VOIDmode, cmp0, cmp1);
757 /* Generate the code to compare two float values. The return value is
758 the comparison expression. */
760 static rtx
761 gen_float_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
762 rtx cmp0, /* first operand to compare */
763 rtx cmp1 /* second operand to compare */)
765 rtx (*gen_fn) (rtx, rtx, rtx);
766 rtx brtmp;
767 int reverse_regs, invert;
769 switch (test_code)
771 case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
772 case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
773 case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
774 case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
775 case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
776 case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
777 case UNEQ: reverse_regs = 0; invert = 0; gen_fn = gen_suneq_sf; break;
778 case LTGT: reverse_regs = 0; invert = 1; gen_fn = gen_suneq_sf; break;
779 case UNLE: reverse_regs = 0; invert = 0; gen_fn = gen_sunle_sf; break;
780 case UNGT: reverse_regs = 1; invert = 0; gen_fn = gen_sunlt_sf; break;
781 case UNLT: reverse_regs = 0; invert = 0; gen_fn = gen_sunlt_sf; break;
782 case UNGE: reverse_regs = 1; invert = 0; gen_fn = gen_sunle_sf; break;
783 case UNORDERED:
784 reverse_regs = 0; invert = 0; gen_fn = gen_sunordered_sf; break;
785 case ORDERED:
786 reverse_regs = 0; invert = 1; gen_fn = gen_sunordered_sf; break;
787 default:
788 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
789 reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
792 if (reverse_regs)
794 rtx temp = cmp0;
795 cmp0 = cmp1;
796 cmp1 = temp;
799 brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
800 emit_insn (gen_fn (brtmp, cmp0, cmp1));
802 return gen_rtx_fmt_ee (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
806 void
807 xtensa_expand_conditional_branch (rtx *operands, machine_mode mode)
809 enum rtx_code test_code = GET_CODE (operands[0]);
810 rtx cmp0 = operands[1];
811 rtx cmp1 = operands[2];
812 rtx cmp;
813 int invert;
814 rtx label1, label2;
816 switch (mode)
818 case E_DFmode:
819 default:
820 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
822 case E_SImode:
823 invert = FALSE;
824 cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
825 break;
827 case E_SFmode:
828 if (!TARGET_HARD_FLOAT)
829 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode,
830 cmp0, cmp1));
831 invert = FALSE;
832 cmp = gen_float_relational (test_code, cmp0, cmp1);
833 break;
836 /* Generate the branch. */
838 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
839 label2 = pc_rtx;
841 if (invert)
843 label2 = label1;
844 label1 = pc_rtx;
847 emit_jump_insn (gen_rtx_SET (pc_rtx,
848 gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
849 label1,
850 label2)));
854 static rtx
855 gen_conditional_move (enum rtx_code code, machine_mode mode,
856 rtx op0, rtx op1)
858 if (mode == SImode)
860 rtx cmp;
862 /* Jump optimization calls get_condition() which canonicalizes
863 comparisons like (GE x <const>) to (GT x <const-1>).
864 Transform those comparisons back to GE, since that is the
865 comparison supported in Xtensa. We shouldn't have to
866 transform <LE x const> comparisons, because neither
867 xtensa_expand_conditional_branch() nor get_condition() will
868 produce them. */
870 if ((code == GT) && (op1 == constm1_rtx))
872 code = GE;
873 op1 = const0_rtx;
875 cmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
877 if (boolean_operator (cmp, VOIDmode))
879 /* Swap the operands to make const0 second. */
880 if (op0 == const0_rtx)
882 op0 = op1;
883 op1 = const0_rtx;
886 /* If not comparing against zero, emit a comparison (subtract). */
887 if (op1 != const0_rtx)
889 op0 = expand_binop (SImode, sub_optab, op0, op1,
890 0, 0, OPTAB_LIB_WIDEN);
891 op1 = const0_rtx;
894 else if (branch_operator (cmp, VOIDmode))
896 /* Swap the operands to make const0 second. */
897 if (op0 == const0_rtx)
899 op0 = op1;
900 op1 = const0_rtx;
902 switch (code)
904 case LT: code = GE; break;
905 case GE: code = LT; break;
906 default: gcc_unreachable ();
910 if (op1 != const0_rtx)
911 return 0;
913 else
914 return 0;
916 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
919 if (TARGET_HARD_FLOAT && mode == SFmode)
920 return gen_float_relational (code, op0, op1);
922 return 0;
927 xtensa_expand_conditional_move (rtx *operands, int isflt)
929 rtx dest = operands[0];
930 rtx cmp = operands[1];
931 machine_mode cmp_mode = GET_MODE (XEXP (cmp, 0));
932 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
934 if (!(cmp = gen_conditional_move (GET_CODE (cmp), cmp_mode,
935 XEXP (cmp, 0), XEXP (cmp, 1))))
936 return 0;
938 if (isflt)
939 gen_fn = (cmp_mode == SImode
940 ? gen_movsfcc_internal0
941 : gen_movsfcc_internal1);
942 else
943 gen_fn = (cmp_mode == SImode
944 ? gen_movsicc_internal0
945 : gen_movsicc_internal1);
947 emit_insn (gen_fn (dest, XEXP (cmp, 0), operands[2], operands[3], cmp));
948 return 1;
953 xtensa_expand_scc (rtx operands[4], machine_mode cmp_mode)
955 rtx dest = operands[0];
956 rtx cmp;
957 rtx one_tmp, zero_tmp;
958 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
960 if (!(cmp = gen_conditional_move (GET_CODE (operands[1]), cmp_mode,
961 operands[2], operands[3])))
962 return 0;
964 one_tmp = gen_reg_rtx (SImode);
965 zero_tmp = gen_reg_rtx (SImode);
966 emit_insn (gen_movsi (one_tmp, const_true_rtx));
967 emit_insn (gen_movsi (zero_tmp, const0_rtx));
969 gen_fn = (cmp_mode == SImode
970 ? gen_movsicc_internal0
971 : gen_movsicc_internal1);
972 emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
973 return 1;
977 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1]. MODE is
978 for the output, i.e., the input operands are twice as big as MODE. */
980 void
981 xtensa_split_operand_pair (rtx operands[4], machine_mode mode)
983 switch (GET_CODE (operands[1]))
985 case REG:
986 operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
987 operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
988 break;
990 case MEM:
991 operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
992 operands[2] = adjust_address (operands[1], mode, 0);
993 break;
995 case CONST_INT:
996 case CONST_DOUBLE:
997 split_double (operands[1], &operands[2], &operands[3]);
998 break;
1000 default:
1001 gcc_unreachable ();
1004 switch (GET_CODE (operands[0]))
1006 case REG:
1007 operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
1008 operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
1009 break;
1011 case MEM:
1012 operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
1013 operands[0] = adjust_address (operands[0], mode, 0);
1014 break;
1016 default:
1017 gcc_unreachable ();
1022 /* Emit insns to move operands[1] into operands[0].
1023 Return 1 if we have written out everything that needs to be done to
1024 do the move. Otherwise, return 0 and the caller will emit the move
1025 normally. */
1028 xtensa_emit_move_sequence (rtx *operands, machine_mode mode)
1030 rtx src = operands[1];
1032 if (CONSTANT_P (src)
1033 && (GET_CODE (src) != CONST_INT || ! xtensa_simm12b (INTVAL (src))))
1035 rtx dst = operands[0];
1037 if (xtensa_tls_referenced_p (src))
1039 rtx addend = NULL;
1041 if (GET_CODE (src) == CONST && GET_CODE (XEXP (src, 0)) == PLUS)
1043 addend = XEXP (XEXP (src, 0), 1);
1044 src = XEXP (XEXP (src, 0), 0);
1047 src = xtensa_legitimize_tls_address (src);
1048 if (addend)
1050 src = gen_rtx_PLUS (mode, src, addend);
1051 src = force_operand (src, dst);
1053 emit_move_insn (dst, src);
1054 return 1;
1057 if (! TARGET_AUTO_LITPOOLS && ! TARGET_CONST16)
1059 src = force_const_mem (SImode, src);
1060 operands[1] = src;
1063 /* PC-relative loads are always SImode, and CONST16 is only
1064 supported in the movsi pattern, so add a SUBREG for any other
1065 (smaller) mode. */
1067 if (mode != SImode)
1069 if (register_operand (dst, mode))
1071 emit_move_insn (simplify_gen_subreg (SImode, dst, mode, 0), src);
1072 return 1;
1074 else
1076 src = force_reg (SImode, src);
1077 src = gen_lowpart_SUBREG (mode, src);
1078 operands[1] = src;
1083 if (!(reload_in_progress | reload_completed)
1084 && !xtensa_valid_move (mode, operands))
1085 operands[1] = force_reg (mode, operands[1]);
1087 operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1089 /* During reload we don't want to emit (subreg:X (mem:Y)) since that
1090 instruction won't be recognized after reload, so we remove the
1091 subreg and adjust mem accordingly. */
1092 if (reload_in_progress)
1094 operands[0] = fixup_subreg_mem (operands[0]);
1095 operands[1] = fixup_subreg_mem (operands[1]);
1097 return 0;
1101 static rtx
1102 fixup_subreg_mem (rtx x)
1104 if (GET_CODE (x) == SUBREG
1105 && GET_CODE (SUBREG_REG (x)) == REG
1106 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1108 rtx temp =
1109 gen_rtx_SUBREG (GET_MODE (x),
1110 reg_equiv_mem (REGNO (SUBREG_REG (x))),
1111 SUBREG_BYTE (x));
1112 x = alter_subreg (&temp, true);
1114 return x;
1118 /* Check if an incoming argument in a7 is expected to be used soon and
1119 if OPND is a register or register pair that includes a7. If so,
1120 create a new pseudo and copy a7 into that pseudo at the very
1121 beginning of the function, followed by the special "set_frame_ptr"
1122 unspec_volatile insn. The return value is either the original
1123 operand, if it is not a7, or the new pseudo containing a copy of
1124 the incoming argument. This is necessary because the register
1125 allocator will ignore conflicts with a7 and may either assign some
1126 other pseudo to a7 or use a7 as the hard_frame_pointer, clobbering
1127 the incoming argument in a7. By copying the argument out of a7 as
1128 the very first thing, and then immediately following that with an
1129 unspec_volatile to keep the scheduler away, we should avoid any
1130 problems. Putting the set_frame_ptr insn at the beginning, with
1131 only the a7 copy before it, also makes it easier for the prologue
1132 expander to initialize the frame pointer after the a7 copy and to
1133 fix up the a7 copy to use the stack pointer instead of the frame
1134 pointer. */
1137 xtensa_copy_incoming_a7 (rtx opnd)
1139 rtx entry_insns = 0;
1140 rtx reg, tmp;
1141 machine_mode mode;
1143 if (!cfun->machine->need_a7_copy)
1144 return opnd;
1146 /* This function should never be called again once a7 has been copied. */
1147 gcc_assert (!cfun->machine->set_frame_ptr_insn);
1149 mode = GET_MODE (opnd);
1151 /* The operand using a7 may come in a later instruction, so just return
1152 the original operand if it doesn't use a7. */
1153 reg = opnd;
1154 if (GET_CODE (reg) == SUBREG)
1156 gcc_assert (SUBREG_BYTE (reg) == 0);
1157 reg = SUBREG_REG (reg);
1159 if (GET_CODE (reg) != REG
1160 || REGNO (reg) > A7_REG
1161 || REGNO (reg) + hard_regno_nregs (A7_REG, mode) <= A7_REG)
1162 return opnd;
1164 /* 1-word args will always be in a7; 2-word args in a6/a7. */
1165 gcc_assert (REGNO (reg) + hard_regno_nregs (A7_REG, mode) - 1 == A7_REG);
1167 cfun->machine->need_a7_copy = false;
1169 /* Copy a7 to a new pseudo at the function entry. Use gen_raw_REG to
1170 create the REG for a7 so that hard_frame_pointer_rtx is not used. */
1172 start_sequence ();
1173 tmp = gen_reg_rtx (mode);
1175 switch (mode)
1177 case E_DFmode:
1178 case E_DImode:
1179 /* Copy the value out of A7 here but keep the first word in A6 until
1180 after the set_frame_ptr insn. Otherwise, the register allocator
1181 may decide to put "subreg (tmp, 0)" in A7 and clobber the incoming
1182 value. */
1183 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 4),
1184 gen_raw_REG (SImode, A7_REG)));
1185 break;
1186 case E_SFmode:
1187 emit_insn (gen_movsf_internal (tmp, gen_raw_REG (mode, A7_REG)));
1188 break;
1189 case E_SImode:
1190 emit_insn (gen_movsi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1191 break;
1192 case E_HImode:
1193 emit_insn (gen_movhi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1194 break;
1195 case E_QImode:
1196 emit_insn (gen_movqi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1197 break;
1198 default:
1199 gcc_unreachable ();
1202 cfun->machine->set_frame_ptr_insn = emit_insn (gen_set_frame_ptr ());
1204 /* For DF and DI mode arguments, copy the incoming value in A6 now. */
1205 if (mode == DFmode || mode == DImode)
1206 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 0),
1207 gen_rtx_REG (SImode, A7_REG - 1)));
1208 entry_insns = get_insns ();
1209 end_sequence ();
1211 if (cfun->machine->vararg_a7)
1213 /* This is called from within builtin_saveregs, which will insert the
1214 saveregs code at the function entry, ahead of anything placed at
1215 the function entry now. Instead, save the sequence to be inserted
1216 at the beginning of the saveregs code. */
1217 cfun->machine->vararg_a7_copy = entry_insns;
1219 else
1221 /* Put entry_insns after the NOTE that starts the function. If
1222 this is inside a start_sequence, make the outer-level insn
1223 chain current, so the code is placed at the start of the
1224 function. */
1225 push_topmost_sequence ();
1226 /* Do not use entry_of_function() here. This is called from within
1227 expand_function_start, when the CFG still holds GIMPLE. */
1228 emit_insn_after (entry_insns, get_insns ());
1229 pop_topmost_sequence ();
1232 return tmp;
1236 /* Try to expand a block move operation to a sequence of RTL move
1237 instructions. If not optimizing, or if the block size is not a
1238 constant, or if the block is too large, the expansion fails and GCC
1239 falls back to calling memcpy().
1241 operands[0] is the destination
1242 operands[1] is the source
1243 operands[2] is the length
1244 operands[3] is the alignment */
1247 xtensa_expand_block_move (rtx *operands)
1249 static const machine_mode mode_from_align[] =
1251 VOIDmode, QImode, HImode, VOIDmode, SImode,
1254 rtx dst_mem = operands[0];
1255 rtx src_mem = operands[1];
1256 HOST_WIDE_INT bytes, align;
1257 int num_pieces, move_ratio;
1258 rtx temp[2];
1259 machine_mode mode[2];
1260 int amount[2];
1261 bool active[2];
1262 int phase = 0;
1263 int next;
1264 int offset_ld = 0;
1265 int offset_st = 0;
1266 rtx x;
1268 /* If this is not a fixed size move, just call memcpy. */
1269 if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
1270 return 0;
1272 bytes = INTVAL (operands[2]);
1273 align = INTVAL (operands[3]);
1275 /* Anything to move? */
1276 if (bytes <= 0)
1277 return 0;
1279 if (align > MOVE_MAX)
1280 align = MOVE_MAX;
1282 /* Decide whether to expand inline based on the optimization level. */
1283 move_ratio = 4;
1284 if (optimize > 2)
1285 move_ratio = LARGEST_MOVE_RATIO;
1286 num_pieces = (bytes / align) + (bytes % align); /* Close enough anyway. */
1287 if (num_pieces > move_ratio)
1288 return 0;
1290 x = XEXP (dst_mem, 0);
1291 if (!REG_P (x))
1293 x = force_reg (Pmode, x);
1294 dst_mem = replace_equiv_address (dst_mem, x);
1297 x = XEXP (src_mem, 0);
1298 if (!REG_P (x))
1300 x = force_reg (Pmode, x);
1301 src_mem = replace_equiv_address (src_mem, x);
1304 active[0] = active[1] = false;
1308 next = phase;
1309 phase ^= 1;
1311 if (bytes > 0)
1313 int next_amount;
1315 next_amount = (bytes >= 4 ? 4 : (bytes >= 2 ? 2 : 1));
1316 next_amount = MIN (next_amount, align);
1318 amount[next] = next_amount;
1319 mode[next] = mode_from_align[next_amount];
1320 temp[next] = gen_reg_rtx (mode[next]);
1322 x = adjust_address (src_mem, mode[next], offset_ld);
1323 emit_insn (gen_rtx_SET (temp[next], x));
1325 offset_ld += next_amount;
1326 bytes -= next_amount;
1327 active[next] = true;
1330 if (active[phase])
1332 active[phase] = false;
1334 x = adjust_address (dst_mem, mode[phase], offset_st);
1335 emit_insn (gen_rtx_SET (x, temp[phase]));
1337 offset_st += amount[phase];
1340 while (active[next]);
1342 return 1;
1346 void
1347 xtensa_expand_nonlocal_goto (rtx *operands)
1349 rtx goto_handler = operands[1];
1350 rtx containing_fp = operands[3];
1352 /* Generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1353 is too big to generate in-line. */
1355 if (GET_CODE (containing_fp) != REG)
1356 containing_fp = force_reg (Pmode, containing_fp);
1358 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1359 LCT_NORMAL, VOIDmode,
1360 containing_fp, Pmode,
1361 goto_handler, Pmode);
1365 static struct machine_function *
1366 xtensa_init_machine_status (void)
1368 return ggc_cleared_alloc<machine_function> ();
1372 /* Shift VAL of mode MODE left by COUNT bits. */
1374 static inline rtx
1375 xtensa_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
1377 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
1378 NULL_RTX, 1, OPTAB_DIRECT);
1379 return expand_simple_binop (SImode, ASHIFT, val, count,
1380 NULL_RTX, 1, OPTAB_DIRECT);
1384 /* Structure to hold the initial parameters for a compare_and_swap operation
1385 in HImode and QImode. */
1387 struct alignment_context
1389 rtx memsi; /* SI aligned memory location. */
1390 rtx shift; /* Bit offset with regard to lsb. */
1391 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
1392 rtx modemaski; /* ~modemask */
1396 /* Initialize structure AC for word access to HI and QI mode memory. */
1398 static void
1399 init_alignment_context (struct alignment_context *ac, rtx mem)
1401 machine_mode mode = GET_MODE (mem);
1402 rtx byteoffset = NULL_RTX;
1403 bool aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
1405 if (aligned)
1406 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
1407 else
1409 /* Alignment is unknown. */
1410 rtx addr, align;
1412 /* Force the address into a register. */
1413 addr = force_reg (Pmode, XEXP (mem, 0));
1415 /* Align it to SImode. */
1416 align = expand_simple_binop (Pmode, AND, addr,
1417 GEN_INT (-GET_MODE_SIZE (SImode)),
1418 NULL_RTX, 1, OPTAB_DIRECT);
1419 /* Generate MEM. */
1420 ac->memsi = gen_rtx_MEM (SImode, align);
1421 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
1422 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
1423 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
1425 byteoffset = expand_simple_binop (Pmode, AND, addr,
1426 GEN_INT (GET_MODE_SIZE (SImode) - 1),
1427 NULL_RTX, 1, OPTAB_DIRECT);
1430 /* Calculate shiftcount. */
1431 if (TARGET_BIG_ENDIAN)
1433 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
1434 if (!aligned)
1435 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
1436 NULL_RTX, 1, OPTAB_DIRECT);
1438 else
1440 if (aligned)
1441 ac->shift = NULL_RTX;
1442 else
1443 ac->shift = byteoffset;
1446 if (ac->shift != NULL_RTX)
1448 /* Shift is the byte count, but we need the bitcount. */
1449 gcc_assert (exact_log2 (BITS_PER_UNIT) >= 0);
1450 ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift,
1451 GEN_INT (exact_log2 (BITS_PER_UNIT)),
1452 NULL_RTX, 1, OPTAB_DIRECT);
1453 ac->modemask = expand_simple_binop (SImode, ASHIFT,
1454 GEN_INT (GET_MODE_MASK (mode)),
1455 ac->shift,
1456 NULL_RTX, 1, OPTAB_DIRECT);
1458 else
1459 ac->modemask = GEN_INT (GET_MODE_MASK (mode));
1461 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask, NULL_RTX, 1);
1465 /* Expand an atomic compare and swap operation for HImode and QImode.
1466 MEM is the memory location, CMP the old value to compare MEM with
1467 and NEW_RTX the value to set if CMP == MEM. */
1469 void
1470 xtensa_expand_compare_and_swap (rtx target, rtx mem, rtx cmp, rtx new_rtx)
1472 machine_mode mode = GET_MODE (mem);
1473 struct alignment_context ac;
1474 rtx tmp, cmpv, newv, val;
1475 rtx oldval = gen_reg_rtx (SImode);
1476 rtx res = gen_reg_rtx (SImode);
1477 rtx_code_label *csloop = gen_label_rtx ();
1478 rtx_code_label *csend = gen_label_rtx ();
1480 init_alignment_context (&ac, mem);
1482 if (ac.shift != NULL_RTX)
1484 cmp = xtensa_expand_mask_and_shift (cmp, mode, ac.shift);
1485 new_rtx = xtensa_expand_mask_and_shift (new_rtx, mode, ac.shift);
1488 /* Load the surrounding word into VAL with the MEM value masked out. */
1489 val = force_reg (SImode, expand_simple_binop (SImode, AND, ac.memsi,
1490 ac.modemaski, NULL_RTX, 1,
1491 OPTAB_DIRECT));
1492 emit_label (csloop);
1494 /* Patch CMP and NEW_RTX into VAL at correct position. */
1495 cmpv = force_reg (SImode, expand_simple_binop (SImode, IOR, cmp, val,
1496 NULL_RTX, 1, OPTAB_DIRECT));
1497 newv = force_reg (SImode, expand_simple_binop (SImode, IOR, new_rtx, val,
1498 NULL_RTX, 1, OPTAB_DIRECT));
1500 /* Jump to end if we're done. */
1501 emit_insn (gen_sync_compare_and_swapsi (res, ac.memsi, cmpv, newv));
1502 emit_cmp_and_jump_insns (res, cmpv, EQ, const0_rtx, SImode, true, csend);
1504 /* Check for changes outside mode. */
1505 emit_move_insn (oldval, val);
1506 tmp = expand_simple_binop (SImode, AND, res, ac.modemaski,
1507 val, 1, OPTAB_DIRECT);
1508 if (tmp != val)
1509 emit_move_insn (val, tmp);
1511 /* Loop internal if so. */
1512 emit_cmp_and_jump_insns (oldval, val, NE, const0_rtx, SImode, true, csloop);
1514 emit_label (csend);
1516 /* Return the correct part of the bitfield. */
1517 convert_move (target,
1518 (ac.shift == NULL_RTX ? res
1519 : expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
1520 NULL_RTX, 1, OPTAB_DIRECT)),
1525 /* Expand an atomic operation CODE of mode MODE (either HImode or QImode --
1526 the default expansion works fine for SImode). MEM is the memory location
1527 and VAL the value to play with. If AFTER is true then store the value
1528 MEM holds after the operation, if AFTER is false then store the value MEM
1529 holds before the operation. If TARGET is zero then discard that value, else
1530 store it to TARGET. */
1532 void
1533 xtensa_expand_atomic (enum rtx_code code, rtx target, rtx mem, rtx val,
1534 bool after)
1536 machine_mode mode = GET_MODE (mem);
1537 struct alignment_context ac;
1538 rtx_code_label *csloop = gen_label_rtx ();
1539 rtx cmp, tmp;
1540 rtx old = gen_reg_rtx (SImode);
1541 rtx new_rtx = gen_reg_rtx (SImode);
1542 rtx orig = NULL_RTX;
1544 init_alignment_context (&ac, mem);
1546 /* Prepare values before the compare-and-swap loop. */
1547 if (ac.shift != NULL_RTX)
1548 val = xtensa_expand_mask_and_shift (val, mode, ac.shift);
1549 switch (code)
1551 case PLUS:
1552 case MINUS:
1553 orig = gen_reg_rtx (SImode);
1554 convert_move (orig, val, 1);
1555 break;
1557 case SET:
1558 case IOR:
1559 case XOR:
1560 break;
1562 case MULT: /* NAND */
1563 case AND:
1564 /* val = "11..1<val>11..1" */
1565 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
1566 NULL_RTX, 1, OPTAB_DIRECT);
1567 break;
1569 default:
1570 gcc_unreachable ();
1573 /* Load full word. Subsequent loads are performed by S32C1I. */
1574 cmp = force_reg (SImode, ac.memsi);
1576 emit_label (csloop);
1577 emit_move_insn (old, cmp);
1579 switch (code)
1581 case PLUS:
1582 case MINUS:
1583 val = expand_simple_binop (SImode, code, old, orig,
1584 NULL_RTX, 1, OPTAB_DIRECT);
1585 val = expand_simple_binop (SImode, AND, val, ac.modemask,
1586 NULL_RTX, 1, OPTAB_DIRECT);
1587 /* FALLTHRU */
1588 case SET:
1589 tmp = expand_simple_binop (SImode, AND, old, ac.modemaski,
1590 NULL_RTX, 1, OPTAB_DIRECT);
1591 tmp = expand_simple_binop (SImode, IOR, tmp, val,
1592 new_rtx, 1, OPTAB_DIRECT);
1593 break;
1595 case AND:
1596 case IOR:
1597 case XOR:
1598 tmp = expand_simple_binop (SImode, code, old, val,
1599 new_rtx, 1, OPTAB_DIRECT);
1600 break;
1602 case MULT: /* NAND */
1603 tmp = expand_simple_binop (SImode, XOR, old, ac.modemask,
1604 NULL_RTX, 1, OPTAB_DIRECT);
1605 tmp = expand_simple_binop (SImode, AND, tmp, val,
1606 new_rtx, 1, OPTAB_DIRECT);
1607 break;
1609 default:
1610 gcc_unreachable ();
1613 if (tmp != new_rtx)
1614 emit_move_insn (new_rtx, tmp);
1615 emit_insn (gen_sync_compare_and_swapsi (cmp, ac.memsi, old, new_rtx));
1616 emit_cmp_and_jump_insns (cmp, old, NE, const0_rtx, SImode, true, csloop);
1618 if (target)
1620 tmp = (after ? new_rtx : cmp);
1621 convert_move (target,
1622 (ac.shift == NULL_RTX ? tmp
1623 : expand_simple_binop (SImode, LSHIFTRT, tmp, ac.shift,
1624 NULL_RTX, 1, OPTAB_DIRECT)),
1630 void
1631 xtensa_setup_frame_addresses (void)
1633 /* Set flag to cause TARGET_FRAME_POINTER_REQUIRED to return true. */
1634 cfun->machine->accesses_prev_frame = 1;
1636 if (TARGET_WINDOWED_ABI)
1637 emit_library_call
1638 (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
1639 LCT_NORMAL, VOIDmode);
1643 /* Emit the assembly for the end of a zero-cost loop. Normally we just emit
1644 a comment showing where the end of the loop is. However, if there is a
1645 label or a branch at the end of the loop then we need to place a nop
1646 there. If the loop ends with a label we need the nop so that branches
1647 targeting that label will target the nop (and thus remain in the loop),
1648 instead of targeting the instruction after the loop (and thus exiting
1649 the loop). If the loop ends with a branch, we need the nop in case the
1650 branch is targeting a location inside the loop. When the branch
1651 executes it will cause the loop count to be decremented even if it is
1652 taken (because it is the last instruction in the loop), so we need to
1653 nop after the branch to prevent the loop count from being decremented
1654 when the branch is taken. */
1656 void
1657 xtensa_emit_loop_end (rtx_insn *insn, rtx *operands)
1659 char done = 0;
1661 for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
1663 switch (GET_CODE (insn))
1665 case NOTE:
1666 case BARRIER:
1667 break;
1669 case CODE_LABEL:
1670 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1671 done = 1;
1672 break;
1674 default:
1676 rtx body = PATTERN (insn);
1678 if (JUMP_P (body))
1680 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1681 done = 1;
1683 else if ((GET_CODE (body) != USE)
1684 && (GET_CODE (body) != CLOBBER))
1685 done = 1;
1687 break;
1691 output_asm_insn ("%1_LEND:", operands);
1695 char *
1696 xtensa_emit_branch (bool inverted, bool immed, rtx *operands)
1698 static char result[64];
1699 enum rtx_code code;
1700 const char *op;
1702 code = GET_CODE (operands[3]);
1703 switch (code)
1705 case EQ: op = inverted ? "ne" : "eq"; break;
1706 case NE: op = inverted ? "eq" : "ne"; break;
1707 case LT: op = inverted ? "ge" : "lt"; break;
1708 case GE: op = inverted ? "lt" : "ge"; break;
1709 case LTU: op = inverted ? "geu" : "ltu"; break;
1710 case GEU: op = inverted ? "ltu" : "geu"; break;
1711 default: gcc_unreachable ();
1714 if (immed)
1716 if (INTVAL (operands[1]) == 0)
1717 sprintf (result, "b%sz%s\t%%0, %%2", op,
1718 (TARGET_DENSITY && (code == EQ || code == NE)) ? ".n" : "");
1719 else
1720 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1722 else
1723 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1725 return result;
1729 char *
1730 xtensa_emit_bit_branch (bool inverted, bool immed, rtx *operands)
1732 static char result[64];
1733 const char *op;
1735 switch (GET_CODE (operands[3]))
1737 case EQ: op = inverted ? "bs" : "bc"; break;
1738 case NE: op = inverted ? "bc" : "bs"; break;
1739 default: gcc_unreachable ();
1742 if (immed)
1744 unsigned bitnum = INTVAL (operands[1]) & 0x1f;
1745 operands[1] = GEN_INT (bitnum);
1746 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1748 else
1749 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1751 return result;
1755 char *
1756 xtensa_emit_movcc (bool inverted, bool isfp, bool isbool, rtx *operands)
1758 static char result[64];
1759 enum rtx_code code;
1760 const char *op;
1762 code = GET_CODE (operands[4]);
1763 if (isbool)
1765 switch (code)
1767 case EQ: op = inverted ? "t" : "f"; break;
1768 case NE: op = inverted ? "f" : "t"; break;
1769 default: gcc_unreachable ();
1772 else
1774 switch (code)
1776 case EQ: op = inverted ? "nez" : "eqz"; break;
1777 case NE: op = inverted ? "eqz" : "nez"; break;
1778 case LT: op = inverted ? "gez" : "ltz"; break;
1779 case GE: op = inverted ? "ltz" : "gez"; break;
1780 default: gcc_unreachable ();
1784 sprintf (result, "mov%s%s\t%%0, %%%d, %%1",
1785 op, isfp ? ".s" : "", inverted ? 3 : 2);
1786 return result;
1790 char *
1791 xtensa_emit_call (int callop, rtx *operands)
1793 static char result[64];
1794 rtx tgt = operands[callop];
1796 if (GET_CODE (tgt) == CONST_INT)
1797 sprintf (result, "call%d\t" HOST_WIDE_INT_PRINT_HEX,
1798 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);
1804 return result;
1808 bool
1809 xtensa_legitimate_address_p (machine_mode mode, rtx addr, bool strict)
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;
1817 while (GET_CODE (addr) == SUBREG)
1818 addr = SUBREG_REG (addr);
1820 /* Allow base registers. */
1821 if (GET_CODE (addr) == REG && BASE_REG_P (addr, strict))
1822 return true;
1824 /* Check for "register + offset" addressing. */
1825 if (GET_CODE (addr) == PLUS)
1827 rtx xplus0 = XEXP (addr, 0);
1828 rtx xplus1 = XEXP (addr, 1);
1829 enum rtx_code code0;
1830 enum rtx_code code1;
1832 while (GET_CODE (xplus0) == SUBREG)
1833 xplus0 = SUBREG_REG (xplus0);
1834 code0 = GET_CODE (xplus0);
1836 while (GET_CODE (xplus1) == SUBREG)
1837 xplus1 = SUBREG_REG (xplus1);
1838 code1 = GET_CODE (xplus1);
1840 /* Swap operands if necessary so the register is first. */
1841 if (code0 != REG && code1 == REG)
1843 xplus0 = XEXP (addr, 1);
1844 xplus1 = XEXP (addr, 0);
1845 code0 = GET_CODE (xplus0);
1846 code1 = GET_CODE (xplus1);
1849 if (code0 == REG && BASE_REG_P (xplus0, strict)
1850 && code1 == CONST_INT
1851 && xtensa_mem_offset (INTVAL (xplus1), mode))
1852 return true;
1855 return false;
1859 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol. */
1861 static GTY(()) rtx xtensa_tls_module_base_symbol;
1863 static rtx
1864 xtensa_tls_module_base (void)
1866 if (! xtensa_tls_module_base_symbol)
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;
1874 return xtensa_tls_module_base_symbol;
1878 static rtx_insn *
1879 xtensa_call_tls_desc (rtx sym, rtx *retp)
1881 rtx fn, arg, a_io;
1882 rtx_insn *call_insn, *insns;
1884 start_sequence ();
1885 fn = gen_reg_rtx (Pmode);
1886 arg = gen_reg_rtx (Pmode);
1887 a_io = gen_rtx_REG (Pmode, WINDOW_SIZE + 2);
1889 emit_insn (gen_tls_func (fn, sym));
1890 emit_insn (gen_tls_arg (arg, sym));
1891 emit_move_insn (a_io, arg);
1892 call_insn = emit_call_insn (gen_tls_call (a_io, fn, sym, const1_rtx));
1893 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn), a_io);
1894 insns = get_insns ();
1895 end_sequence ();
1897 *retp = a_io;
1898 return insns;
1902 static rtx
1903 xtensa_legitimize_tls_address (rtx x)
1905 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
1906 rtx dest, tp, ret, modbase, base, addend;
1907 rtx_insn *insns;
1909 dest = gen_reg_rtx (Pmode);
1910 switch (model)
1912 case TLS_MODEL_GLOBAL_DYNAMIC:
1913 insns = xtensa_call_tls_desc (x, &ret);
1914 emit_libcall_block (insns, dest, ret, x);
1915 break;
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;
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;
1934 default:
1935 gcc_unreachable ();
1938 return dest;
1943 xtensa_legitimize_address (rtx x,
1944 rtx oldx ATTRIBUTE_UNUSED,
1945 machine_mode mode)
1947 if (xtensa_tls_symbol_p (x))
1948 return xtensa_legitimize_tls_address (x);
1950 if (GET_CODE (x) == PLUS)
1952 rtx plus0 = XEXP (x, 0);
1953 rtx plus1 = XEXP (x, 1);
1955 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
1957 plus0 = XEXP (x, 1);
1958 plus1 = XEXP (x, 0);
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))
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));
1977 return x;
1980 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
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. */
1990 static bool
1991 xtensa_mode_dependent_address_p (const_rtx addr,
1992 addr_space_t as ATTRIBUTE_UNUSED)
1994 return constantpool_address_p (addr);
1997 /* Return TRUE if X contains any TLS symbol references. */
1999 bool
2000 xtensa_tls_referenced_p (rtx x)
2002 if (! TARGET_HAVE_TLS)
2003 return false;
2005 subrtx_iterator::array_type array;
2006 FOR_EACH_SUBRTX (iter, array, x, ALL)
2008 const_rtx x = *iter;
2009 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0)
2010 return true;
2012 /* Ignore TLS references that have already been legitimized. */
2013 if (GET_CODE (x) == UNSPEC)
2014 switch (XINT (x, 1))
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;
2027 return false;
2031 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2033 static bool
2034 xtensa_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2036 return xtensa_tls_referenced_p (x);
2040 /* Return the debugger register number to use for 'regno'. */
2043 xtensa_dbx_register_number (int regno)
2045 int first = -1;
2047 if (GP_REG_P (regno))
2049 regno -= GP_REG_FIRST;
2050 first = 0;
2052 else if (BR_REG_P (regno))
2054 regno -= BR_REG_FIRST;
2055 first = 16;
2057 else if (FP_REG_P (regno))
2059 regno -= FP_REG_FIRST;
2060 first = 48;
2062 else if (ACC_REG_P (regno))
2064 first = 0x200; /* Start of Xtensa special registers. */
2065 regno = 16; /* ACCLO is special register 16. */
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;
2073 return first + regno;
2077 /* Argument support functions. */
2079 /* Initialize CUMULATIVE_ARGS for a function. */
2081 void
2082 init_cumulative_args (CUMULATIVE_ARGS *cum, int incoming)
2084 cum->arg_words = 0;
2085 cum->incoming = incoming;
2089 /* Advance the argument to the next argument position. */
2091 static void
2092 xtensa_function_arg_advance (cumulative_args_t cum, machine_mode mode,
2093 const_tree type, bool named ATTRIBUTE_UNUSED)
2095 int words, max;
2096 int *arg_words;
2098 arg_words = &get_cumulative_args (cum)->arg_words;
2099 max = MAX_ARGS_IN_REGISTERS;
2101 words = (((mode != BLKmode)
2102 ? (int) GET_MODE_SIZE (mode)
2103 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2105 if (*arg_words < max
2106 && (targetm.calls.must_pass_in_stack (mode, type)
2107 || *arg_words + words > max))
2108 *arg_words = max;
2110 *arg_words += words;
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. */
2118 static rtx
2119 xtensa_function_arg_1 (cumulative_args_t cum_v, machine_mode mode,
2120 const_tree type, bool incoming_p)
2122 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2123 int regbase, words, max;
2124 int *arg_words;
2125 int regno;
2127 arg_words = &cum->arg_words;
2128 regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
2129 max = MAX_ARGS_IN_REGISTERS;
2131 words = (((mode != BLKmode)
2132 ? (int) GET_MODE_SIZE (mode)
2133 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2135 if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
2137 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_WORD;
2138 *arg_words = (*arg_words + align - 1) & -align;
2141 if (*arg_words + words > max)
2142 return (rtx)0;
2144 regno = regbase + *arg_words;
2146 if (cum->incoming && regno <= A7_REG && regno + words > A7_REG)
2147 cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
2149 return gen_rtx_REG (mode, regno);
2152 /* Implement TARGET_FUNCTION_ARG. */
2154 static rtx
2155 xtensa_function_arg (cumulative_args_t cum, machine_mode mode,
2156 const_tree type, bool named ATTRIBUTE_UNUSED)
2158 return xtensa_function_arg_1 (cum, mode, type, false);
2161 /* Implement TARGET_FUNCTION_INCOMING_ARG. */
2163 static rtx
2164 xtensa_function_incoming_arg (cumulative_args_t cum, machine_mode mode,
2165 const_tree type, bool named ATTRIBUTE_UNUSED)
2167 return xtensa_function_arg_1 (cum, mode, type, true);
2170 static unsigned int
2171 xtensa_function_arg_boundary (machine_mode mode, const_tree type)
2173 unsigned int alignment;
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;
2184 static bool
2185 xtensa_return_in_msb (const_tree valtype)
2187 return (TARGET_BIG_ENDIAN
2188 && AGGREGATE_TYPE_P (valtype)
2189 && int_size_in_bytes (valtype) >= UNITS_PER_WORD);
2193 static void
2194 xtensa_option_override (void)
2196 int regno;
2197 machine_mode mode;
2199 /* Use CONST16 in the absence of L32R.
2200 Set it in the TARGET_OPTION_OVERRIDE to avoid dependency on xtensa
2201 configuration in the xtensa-common.c */
2203 if (!TARGET_L32R)
2204 target_flags |= MASK_CONST16;
2206 if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
2207 error ("boolean registers required for the floating-point option");
2209 /* Set up array giving whether a given register can hold a given mode. */
2210 for (mode = VOIDmode;
2211 mode != MAX_MACHINE_MODE;
2212 mode = (machine_mode) ((int) mode + 1))
2214 int size = GET_MODE_SIZE (mode);
2215 enum mode_class mclass = GET_MODE_CLASS (mode);
2217 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2219 int temp;
2221 if (ACC_REG_P (regno))
2222 temp = (TARGET_MAC16
2223 && (mclass == MODE_INT) && (size <= UNITS_PER_WORD));
2224 else if (GP_REG_P (regno))
2225 temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
2226 else if (FP_REG_P (regno))
2227 temp = (TARGET_HARD_FLOAT && (mode == SFmode));
2228 else if (BR_REG_P (regno))
2229 temp = (TARGET_BOOLEANS && (mode == CCmode));
2230 else
2231 temp = FALSE;
2233 xtensa_hard_regno_mode_ok_p[(int) mode][regno] = temp;
2237 init_machine_status = xtensa_init_machine_status;
2239 /* Check PIC settings. PIC is only supported when using L32R
2240 instructions, and some targets need to always use PIC. */
2241 if (flag_pic && TARGET_CONST16)
2242 error ("-f%s is not supported with CONST16 instructions",
2243 (flag_pic > 1 ? "PIC" : "pic"));
2244 else if (TARGET_FORCE_NO_PIC)
2245 flag_pic = 0;
2246 else if (XTENSA_ALWAYS_PIC)
2248 if (TARGET_CONST16)
2249 error ("PIC is required but not supported with CONST16 instructions");
2250 flag_pic = 1;
2252 /* There's no need for -fPIC (as opposed to -fpic) on Xtensa. */
2253 if (flag_pic > 1)
2254 flag_pic = 1;
2255 if (flag_pic && !flag_pie)
2256 flag_shlib = 1;
2258 /* Hot/cold partitioning does not work on this architecture, because of
2259 constant pools (the load instruction cannot necessarily reach that far).
2260 Therefore disable it on this architecture. */
2261 if (flag_reorder_blocks_and_partition)
2263 flag_reorder_blocks_and_partition = 0;
2264 flag_reorder_blocks = 1;
2268 /* Implement TARGET_HARD_REGNO_NREGS. */
2270 static unsigned int
2271 xtensa_hard_regno_nregs (unsigned int regno, machine_mode mode)
2273 if (FP_REG_P (regno))
2274 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_FPREG);
2275 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
2278 /* Implement TARGET_HARD_REGNO_MODE_OK. */
2280 static bool
2281 xtensa_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2283 return xtensa_hard_regno_mode_ok_p[mode][regno];
2286 /* Implement TARGET_MODES_TIEABLE_P. */
2288 static bool
2289 xtensa_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2291 return ((GET_MODE_CLASS (mode1) == MODE_FLOAT
2292 || GET_MODE_CLASS (mode1) == MODE_COMPLEX_FLOAT)
2293 == (GET_MODE_CLASS (mode2) == MODE_FLOAT
2294 || GET_MODE_CLASS (mode2) == MODE_COMPLEX_FLOAT));
2297 /* A C compound statement to output to stdio stream STREAM the
2298 assembler syntax for an instruction operand X. X is an RTL
2299 expression.
2301 CODE is a value that can be used to specify one of several ways
2302 of printing the operand. It is used when identical operands
2303 must be printed differently depending on the context. CODE
2304 comes from the '%' specification that was used to request
2305 printing of the operand. If the specification was just '%DIGIT'
2306 then CODE is 0; if the specification was '%LTR DIGIT' then CODE
2307 is the ASCII code for LTR.
2309 If X is a register, this macro should print the register's name.
2310 The names can be found in an array 'reg_names' whose type is
2311 'char *[]'. 'reg_names' is initialized from 'REGISTER_NAMES'.
2313 When the machine description has a specification '%PUNCT' (a '%'
2314 followed by a punctuation character), this macro is called with
2315 a null pointer for X and the punctuation character for CODE.
2317 'a', 'c', 'l', and 'n' are reserved.
2319 The Xtensa specific codes are:
2321 'd' CONST_INT, print as signed decimal
2322 'x' CONST_INT, print as signed hexadecimal
2323 'K' CONST_INT, print number of bits in mask for EXTUI
2324 'R' CONST_INT, print (X & 0x1f)
2325 'L' CONST_INT, print ((32 - X) & 0x1f)
2326 'D' REG, print second register of double-word register operand
2327 'N' MEM, print address of next word following a memory operand
2328 'v' MEM, if memory reference is volatile, output a MEMW before it
2329 't' any constant, add "@h" suffix for top 16 bits
2330 'b' any constant, add "@l" suffix for bottom 16 bits
2333 static void
2334 printx (FILE *file, signed int val)
2336 /* Print a hexadecimal value in a nice way. */
2337 if ((val > -0xa) && (val < 0xa))
2338 fprintf (file, "%d", val);
2339 else if (val < 0)
2340 fprintf (file, "-0x%x", -val);
2341 else
2342 fprintf (file, "0x%x", val);
2346 void
2347 print_operand (FILE *file, rtx x, int letter)
2349 if (!x)
2350 error ("PRINT_OPERAND null pointer");
2352 switch (letter)
2354 case 'D':
2355 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2356 fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
2357 else
2358 output_operand_lossage ("invalid %%D value");
2359 break;
2361 case 'v':
2362 if (GET_CODE (x) == MEM)
2364 /* For a volatile memory reference, emit a MEMW before the
2365 load or store. */
2366 if (MEM_VOLATILE_P (x) && TARGET_SERIALIZE_VOLATILE)
2367 fprintf (file, "memw\n\t");
2369 else
2370 output_operand_lossage ("invalid %%v value");
2371 break;
2373 case 'N':
2374 if (GET_CODE (x) == MEM
2375 && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
2377 x = adjust_address (x, GET_MODE (x) == DFmode ? E_SFmode : E_SImode,
2379 output_address (GET_MODE (x), XEXP (x, 0));
2381 else
2382 output_operand_lossage ("invalid %%N value");
2383 break;
2385 case 'K':
2386 if (GET_CODE (x) == CONST_INT)
2388 int num_bits = 0;
2389 unsigned val = INTVAL (x);
2390 while (val & 1)
2392 num_bits += 1;
2393 val = val >> 1;
2395 if ((val != 0) || (num_bits == 0) || (num_bits > 16))
2396 fatal_insn ("invalid mask", x);
2398 fprintf (file, "%d", num_bits);
2400 else
2401 output_operand_lossage ("invalid %%K value");
2402 break;
2404 case 'L':
2405 if (GET_CODE (x) == CONST_INT)
2406 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 0x1f);
2407 else
2408 output_operand_lossage ("invalid %%L value");
2409 break;
2411 case 'R':
2412 if (GET_CODE (x) == CONST_INT)
2413 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x1f);
2414 else
2415 output_operand_lossage ("invalid %%R value");
2416 break;
2418 case 'x':
2419 if (GET_CODE (x) == CONST_INT)
2420 printx (file, INTVAL (x));
2421 else
2422 output_operand_lossage ("invalid %%x value");
2423 break;
2425 case 'd':
2426 if (GET_CODE (x) == CONST_INT)
2427 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
2428 else
2429 output_operand_lossage ("invalid %%d value");
2430 break;
2432 case 't':
2433 case 'b':
2434 if (GET_CODE (x) == CONST_INT)
2436 printx (file, INTVAL (x));
2437 fputs (letter == 't' ? "@h" : "@l", file);
2439 else if (GET_CODE (x) == CONST_DOUBLE)
2441 if (GET_MODE (x) == SFmode)
2443 long l;
2444 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
2445 fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
2447 else
2448 output_operand_lossage ("invalid %%t/%%b value");
2450 else if (GET_CODE (x) == CONST)
2452 /* X must be a symbolic constant on ELF. Write an expression
2453 suitable for 'const16' that sets the high or low 16 bits. */
2454 if (GET_CODE (XEXP (x, 0)) != PLUS
2455 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
2456 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2457 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
2458 output_operand_lossage ("invalid %%t/%%b value");
2459 print_operand (file, XEXP (XEXP (x, 0), 0), 0);
2460 fputs (letter == 't' ? "@h" : "@l", file);
2461 /* There must be a non-alphanumeric character between 'h' or 'l'
2462 and the number. The '-' is added by print_operand() already. */
2463 if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
2464 fputs ("+", file);
2465 print_operand (file, XEXP (XEXP (x, 0), 1), 0);
2467 else
2469 output_addr_const (file, x);
2470 fputs (letter == 't' ? "@h" : "@l", file);
2472 break;
2474 case 'y':
2475 if (GET_CODE (x) == CONST_DOUBLE &&
2476 GET_MODE (x) == SFmode)
2478 long l;
2479 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
2480 fprintf (file, "0x%08lx", l);
2481 break;
2484 /* fall through */
2486 default:
2487 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2488 fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
2489 else if (GET_CODE (x) == MEM)
2490 output_address (GET_MODE (x), XEXP (x, 0));
2491 else if (GET_CODE (x) == CONST_INT)
2492 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
2493 else
2494 output_addr_const (file, x);
2499 /* A C compound statement to output to stdio stream STREAM the
2500 assembler syntax for an instruction operand that is a memory
2501 reference whose address is ADDR. ADDR is an RTL expression. */
2503 void
2504 print_operand_address (FILE *file, rtx addr)
2506 if (!addr)
2507 error ("PRINT_OPERAND_ADDRESS, null pointer");
2509 switch (GET_CODE (addr))
2511 default:
2512 fatal_insn ("invalid address", addr);
2513 break;
2515 case REG:
2516 fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
2517 break;
2519 case PLUS:
2521 rtx reg = (rtx)0;
2522 rtx offset = (rtx)0;
2523 rtx arg0 = XEXP (addr, 0);
2524 rtx arg1 = XEXP (addr, 1);
2526 if (GET_CODE (arg0) == REG)
2528 reg = arg0;
2529 offset = arg1;
2531 else if (GET_CODE (arg1) == REG)
2533 reg = arg1;
2534 offset = arg0;
2536 else
2537 fatal_insn ("no register in address", addr);
2539 if (CONSTANT_P (offset))
2541 fprintf (file, "%s, ", reg_names [REGNO (reg)]);
2542 output_addr_const (file, offset);
2544 else
2545 fatal_insn ("address offset not a constant", addr);
2547 break;
2549 case LABEL_REF:
2550 case SYMBOL_REF:
2551 case CONST_INT:
2552 case CONST:
2553 output_addr_const (file, addr);
2554 break;
2558 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
2560 static bool
2561 xtensa_output_addr_const_extra (FILE *fp, rtx x)
2563 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
2565 switch (XINT (x, 1))
2567 case UNSPEC_TPOFF:
2568 output_addr_const (fp, XVECEXP (x, 0, 0));
2569 fputs ("@TPOFF", fp);
2570 return true;
2571 case UNSPEC_DTPOFF:
2572 output_addr_const (fp, XVECEXP (x, 0, 0));
2573 fputs ("@DTPOFF", fp);
2574 return true;
2575 case UNSPEC_PLT:
2576 if (flag_pic)
2578 output_addr_const (fp, XVECEXP (x, 0, 0));
2579 fputs ("@PLT", fp);
2580 return true;
2582 break;
2583 default:
2584 break;
2587 return false;
2590 static void
2591 xtensa_output_integer_literal_parts (FILE *file, rtx x, int size)
2593 if (size > 4 && !(size & (size - 1)))
2595 rtx first, second;
2597 split_double (x, &first, &second);
2598 xtensa_output_integer_literal_parts (file, first, size / 2);
2599 fputs (", ", file);
2600 xtensa_output_integer_literal_parts (file, second, size / 2);
2602 else if (size == 4)
2604 output_addr_const (file, x);
2606 else
2608 gcc_unreachable();
2612 void
2613 xtensa_output_literal (FILE *file, rtx x, machine_mode mode, int labelno)
2615 long value_long[2];
2617 fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
2619 switch (GET_MODE_CLASS (mode))
2621 case MODE_FLOAT:
2622 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
2624 switch (mode)
2626 case E_SFmode:
2627 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x),
2628 value_long[0]);
2629 if (HOST_BITS_PER_LONG > 32)
2630 value_long[0] &= 0xffffffff;
2631 fprintf (file, "0x%08lx\n", value_long[0]);
2632 break;
2634 case E_DFmode:
2635 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x),
2636 value_long);
2637 if (HOST_BITS_PER_LONG > 32)
2639 value_long[0] &= 0xffffffff;
2640 value_long[1] &= 0xffffffff;
2642 fprintf (file, "0x%08lx, 0x%08lx\n",
2643 value_long[0], value_long[1]);
2644 break;
2646 default:
2647 gcc_unreachable ();
2650 break;
2652 case MODE_INT:
2653 case MODE_PARTIAL_INT:
2654 xtensa_output_integer_literal_parts (file, x, GET_MODE_SIZE (mode));
2655 fputs ("\n", file);
2656 break;
2658 default:
2659 gcc_unreachable ();
2663 static bool
2664 xtensa_call_save_reg(int regno)
2666 if (TARGET_WINDOWED_ABI)
2667 return false;
2669 if (regno == A0_REG)
2670 return crtl->profile || !crtl->is_leaf || crtl->calls_eh_return ||
2671 df_regs_ever_live_p (regno);
2673 if (crtl->calls_eh_return && regno >= 2 && regno < 4)
2674 return true;
2676 return !fixed_regs[regno] && !call_used_regs[regno] &&
2677 df_regs_ever_live_p (regno);
2680 /* Return the bytes needed to compute the frame pointer from the current
2681 stack pointer. */
2683 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
2684 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
2686 long
2687 compute_frame_size (int size)
2689 int regno;
2691 if (reload_completed && cfun->machine->frame_laid_out)
2692 return cfun->machine->current_frame_size;
2694 /* Add space for the incoming static chain value. */
2695 if (cfun->static_chain_decl != NULL)
2696 size += (1 * UNITS_PER_WORD);
2698 cfun->machine->callee_save_size = 0;
2699 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2701 if (xtensa_call_save_reg(regno))
2702 cfun->machine->callee_save_size += UNITS_PER_WORD;
2705 cfun->machine->current_frame_size =
2706 XTENSA_STACK_ALIGN (size
2707 + cfun->machine->callee_save_size
2708 + crtl->outgoing_args_size
2709 + (WINDOW_SIZE * UNITS_PER_WORD));
2710 cfun->machine->callee_save_size =
2711 XTENSA_STACK_ALIGN (cfun->machine->callee_save_size);
2712 cfun->machine->frame_laid_out = true;
2713 return cfun->machine->current_frame_size;
2717 bool
2718 xtensa_frame_pointer_required (void)
2720 /* The code to expand builtin_frame_addr and builtin_return_addr
2721 currently uses the hard_frame_pointer instead of frame_pointer.
2722 This seems wrong but maybe it's necessary for other architectures.
2723 This function is derived from the i386 code. */
2725 if (cfun->machine->accesses_prev_frame)
2726 return true;
2728 return false;
2731 HOST_WIDE_INT
2732 xtensa_initial_elimination_offset (int from, int to ATTRIBUTE_UNUSED)
2734 long frame_size = compute_frame_size (get_frame_size ());
2735 HOST_WIDE_INT offset;
2737 switch (from)
2739 case FRAME_POINTER_REGNUM:
2740 if (FRAME_GROWS_DOWNWARD)
2741 offset = frame_size - (WINDOW_SIZE * UNITS_PER_WORD)
2742 - cfun->machine->callee_save_size;
2743 else
2744 offset = 0;
2745 break;
2746 case ARG_POINTER_REGNUM:
2747 offset = frame_size;
2748 break;
2749 default:
2750 gcc_unreachable ();
2753 return offset;
2756 /* minimum frame = reg save area (4 words) plus static chain (1 word)
2757 and the total number of words must be a multiple of 128 bits. */
2758 #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD)
2760 void
2761 xtensa_expand_prologue (void)
2763 HOST_WIDE_INT total_size;
2764 rtx_insn *insn = NULL;
2765 rtx note_rtx;
2768 total_size = compute_frame_size (get_frame_size ());
2770 if (flag_stack_usage_info)
2771 current_function_static_stack_size = total_size;
2773 if (TARGET_WINDOWED_ABI)
2775 if (total_size < (1 << (12+3)))
2776 insn = emit_insn (gen_entry (GEN_INT (total_size)));
2777 else
2779 /* Use a8 as a temporary since a0-a7 may be live. */
2780 rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
2781 emit_insn (gen_entry (GEN_INT (MIN_FRAME_SIZE)));
2782 emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
2783 emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
2784 insn = emit_insn (gen_movsi (stack_pointer_rtx, tmp_reg));
2787 else
2789 int regno;
2790 HOST_WIDE_INT offset = 0;
2791 int callee_save_size = cfun->machine->callee_save_size;
2793 /* -128 is a limit of single addi instruction. */
2794 if (total_size > 0 && total_size <= 128)
2796 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2797 GEN_INT (-total_size)));
2798 RTX_FRAME_RELATED_P (insn) = 1;
2799 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2800 plus_constant (Pmode, stack_pointer_rtx,
2801 -total_size));
2802 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2803 offset = total_size - UNITS_PER_WORD;
2805 else if (callee_save_size)
2807 /* 1020 is maximal s32i offset, if the frame is bigger than that
2808 * we move sp to the end of callee-saved save area, save and then
2809 * move it to its final location. */
2810 if (total_size > 1024)
2812 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2813 GEN_INT (-callee_save_size)));
2814 RTX_FRAME_RELATED_P (insn) = 1;
2815 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2816 plus_constant (Pmode, stack_pointer_rtx,
2817 -callee_save_size));
2818 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2819 offset = callee_save_size - UNITS_PER_WORD;
2821 else
2823 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2824 emit_move_insn (tmp_reg, GEN_INT (total_size));
2825 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
2826 stack_pointer_rtx, tmp_reg));
2827 RTX_FRAME_RELATED_P (insn) = 1;
2828 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2829 plus_constant (Pmode, stack_pointer_rtx,
2830 -total_size));
2831 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2832 offset = total_size - UNITS_PER_WORD;
2836 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2838 if (xtensa_call_save_reg(regno))
2840 rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
2841 rtx mem = gen_frame_mem (SImode, x);
2842 rtx reg = gen_rtx_REG (SImode, regno);
2844 offset -= UNITS_PER_WORD;
2845 insn = emit_move_insn (mem, reg);
2846 RTX_FRAME_RELATED_P (insn) = 1;
2847 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
2848 gen_rtx_SET (mem, reg));
2851 if (total_size > 1024)
2853 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2854 emit_move_insn (tmp_reg, GEN_INT (total_size -
2855 callee_save_size));
2856 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
2857 stack_pointer_rtx, tmp_reg));
2858 RTX_FRAME_RELATED_P (insn) = 1;
2859 note_rtx = gen_rtx_SET (stack_pointer_rtx,
2860 plus_constant (Pmode, stack_pointer_rtx,
2861 callee_save_size -
2862 total_size));
2863 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2867 if (frame_pointer_needed)
2869 if (cfun->machine->set_frame_ptr_insn)
2871 rtx_insn *first;
2873 push_topmost_sequence ();
2874 first = get_insns ();
2875 pop_topmost_sequence ();
2877 /* For all instructions prior to set_frame_ptr_insn, replace
2878 hard_frame_pointer references with stack_pointer. */
2879 for (insn = first;
2880 insn != cfun->machine->set_frame_ptr_insn;
2881 insn = NEXT_INSN (insn))
2883 if (INSN_P (insn))
2885 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
2886 hard_frame_pointer_rtx,
2887 stack_pointer_rtx);
2888 df_insn_rescan (insn);
2892 else
2894 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
2895 stack_pointer_rtx));
2896 if (!TARGET_WINDOWED_ABI)
2898 note_rtx = gen_rtx_SET (hard_frame_pointer_rtx,
2899 stack_pointer_rtx);
2900 RTX_FRAME_RELATED_P (insn) = 1;
2901 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2906 if (TARGET_WINDOWED_ABI)
2908 /* Create a note to describe the CFA. Because this is only used to set
2909 DW_AT_frame_base for debug info, don't bother tracking changes through
2910 each instruction in the prologue. It just takes up space. */
2911 note_rtx = gen_rtx_SET ((frame_pointer_needed
2912 ? hard_frame_pointer_rtx
2913 : stack_pointer_rtx),
2914 plus_constant (Pmode, stack_pointer_rtx,
2915 -total_size));
2916 RTX_FRAME_RELATED_P (insn) = 1;
2917 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2921 void
2922 xtensa_expand_epilogue (void)
2924 if (!TARGET_WINDOWED_ABI)
2926 int regno;
2927 HOST_WIDE_INT offset;
2929 if (cfun->machine->current_frame_size > (frame_pointer_needed ? 127 : 1024))
2931 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2932 emit_move_insn (tmp_reg, GEN_INT (cfun->machine->current_frame_size -
2933 cfun->machine->callee_save_size));
2934 emit_insn (gen_addsi3 (stack_pointer_rtx, frame_pointer_needed ?
2935 hard_frame_pointer_rtx : stack_pointer_rtx,
2936 tmp_reg));
2937 offset = cfun->machine->callee_save_size - UNITS_PER_WORD;
2939 else
2941 if (frame_pointer_needed)
2942 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
2943 offset = cfun->machine->current_frame_size - UNITS_PER_WORD;
2946 /* Prevent reordering of saved a0 update and loading it back from
2947 the save area. */
2948 if (crtl->calls_eh_return)
2949 emit_insn (gen_blockage ());
2951 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2953 if (xtensa_call_save_reg(regno))
2955 rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
2957 offset -= UNITS_PER_WORD;
2958 emit_move_insn (gen_rtx_REG (SImode, regno),
2959 gen_frame_mem (SImode, x));
2963 if (cfun->machine->current_frame_size > 0)
2965 if (frame_pointer_needed || /* always reachable with addi */
2966 cfun->machine->current_frame_size > 1024 ||
2967 cfun->machine->current_frame_size <= 127)
2969 if (cfun->machine->current_frame_size <= 127)
2970 offset = cfun->machine->current_frame_size;
2971 else
2972 offset = cfun->machine->callee_save_size;
2974 emit_insn (gen_addsi3 (stack_pointer_rtx,
2975 stack_pointer_rtx,
2976 GEN_INT (offset)));
2978 else
2980 rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
2981 emit_move_insn (tmp_reg,
2982 GEN_INT (cfun->machine->current_frame_size));
2983 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2984 tmp_reg));
2988 if (crtl->calls_eh_return)
2989 emit_insn (gen_add3_insn (stack_pointer_rtx,
2990 stack_pointer_rtx,
2991 EH_RETURN_STACKADJ_RTX));
2993 cfun->machine->epilogue_done = true;
2994 emit_jump_insn (gen_return ());
2997 bool
2998 xtensa_use_return_instruction_p (void)
3000 if (!reload_completed)
3001 return false;
3002 if (TARGET_WINDOWED_ABI)
3003 return true;
3004 if (compute_frame_size (get_frame_size ()) == 0)
3005 return true;
3006 return cfun->machine->epilogue_done;
3009 void
3010 xtensa_set_return_address (rtx address, rtx scratch)
3012 HOST_WIDE_INT total_size = compute_frame_size (get_frame_size ());
3013 rtx frame = frame_pointer_needed ?
3014 hard_frame_pointer_rtx : stack_pointer_rtx;
3015 rtx a0_addr = plus_constant (Pmode, frame,
3016 total_size - UNITS_PER_WORD);
3017 rtx note = gen_rtx_SET (gen_frame_mem (SImode, a0_addr),
3018 gen_rtx_REG (SImode, A0_REG));
3019 rtx insn;
3021 if (total_size > 1024) {
3022 emit_move_insn (scratch, GEN_INT (total_size - UNITS_PER_WORD));
3023 emit_insn (gen_addsi3 (scratch, frame, scratch));
3024 a0_addr = scratch;
3027 insn = emit_move_insn (gen_frame_mem (SImode, a0_addr), address);
3028 RTX_FRAME_RELATED_P (insn) = 1;
3029 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
3033 xtensa_return_addr (int count, rtx frame)
3035 rtx result, retaddr, curaddr, label;
3037 if (!TARGET_WINDOWED_ABI)
3039 if (count != 0)
3040 return const0_rtx;
3042 return get_hard_reg_initial_val (Pmode, A0_REG);
3045 if (count == -1)
3046 retaddr = gen_rtx_REG (Pmode, A0_REG);
3047 else
3049 rtx addr = plus_constant (Pmode, frame, -4 * UNITS_PER_WORD);
3050 addr = memory_address (Pmode, addr);
3051 retaddr = gen_reg_rtx (Pmode);
3052 emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
3055 /* The 2 most-significant bits of the return address on Xtensa hold
3056 the register window size. To get the real return address, these
3057 bits must be replaced with the high bits from some address in the
3058 code. */
3060 /* Get the 2 high bits of a local label in the code. */
3061 curaddr = gen_reg_rtx (Pmode);
3062 label = gen_label_rtx ();
3063 emit_label (label);
3064 LABEL_PRESERVE_P (label) = 1;
3065 emit_move_insn (curaddr, gen_rtx_LABEL_REF (Pmode, label));
3066 emit_insn (gen_lshrsi3 (curaddr, curaddr, GEN_INT (30)));
3067 emit_insn (gen_ashlsi3 (curaddr, curaddr, GEN_INT (30)));
3069 /* Clear the 2 high bits of the return address. */
3070 result = gen_reg_rtx (Pmode);
3071 emit_insn (gen_ashlsi3 (result, retaddr, GEN_INT (2)));
3072 emit_insn (gen_lshrsi3 (result, result, GEN_INT (2)));
3074 /* Combine them to get the result. */
3075 emit_insn (gen_iorsi3 (result, result, curaddr));
3076 return result;
3079 /* Disable the use of word-sized or smaller complex modes for structures,
3080 and for function arguments in particular, where they cause problems with
3081 register a7. The xtensa_copy_incoming_a7 function assumes that there is
3082 a single reference to an argument in a7, but with small complex modes the
3083 real and imaginary components may be extracted separately, leading to two
3084 uses of the register, only one of which would be replaced. */
3086 static bool
3087 xtensa_member_type_forces_blk (const_tree, machine_mode mode)
3089 return mode == CQImode || mode == CHImode;
3092 /* Create the va_list data type.
3094 This structure is set up by __builtin_saveregs. The __va_reg field
3095 points to a stack-allocated region holding the contents of the
3096 incoming argument registers. The __va_ndx field is an index
3097 initialized to the position of the first unnamed (variable)
3098 argument. This same index is also used to address the arguments
3099 passed in memory. Thus, the __va_stk field is initialized to point
3100 to the position of the first argument in memory offset to account
3101 for the arguments passed in registers and to account for the size
3102 of the argument registers not being 16-byte aligned. E.G., there
3103 are 6 argument registers of 4 bytes each, but we want the __va_ndx
3104 for the first stack argument to have the maximal alignment of 16
3105 bytes, so we offset the __va_stk address by 32 bytes so that
3106 __va_stk[32] references the first argument on the stack. */
3108 static tree
3109 xtensa_build_builtin_va_list (void)
3111 tree f_stk, f_reg, f_ndx, record, type_decl;
3113 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3114 type_decl = build_decl (BUILTINS_LOCATION,
3115 TYPE_DECL, get_identifier ("__va_list_tag"), record);
3117 f_stk = build_decl (BUILTINS_LOCATION,
3118 FIELD_DECL, get_identifier ("__va_stk"),
3119 ptr_type_node);
3120 f_reg = build_decl (BUILTINS_LOCATION,
3121 FIELD_DECL, get_identifier ("__va_reg"),
3122 ptr_type_node);
3123 f_ndx = build_decl (BUILTINS_LOCATION,
3124 FIELD_DECL, get_identifier ("__va_ndx"),
3125 integer_type_node);
3127 DECL_FIELD_CONTEXT (f_stk) = record;
3128 DECL_FIELD_CONTEXT (f_reg) = record;
3129 DECL_FIELD_CONTEXT (f_ndx) = record;
3131 TYPE_STUB_DECL (record) = type_decl;
3132 TYPE_NAME (record) = type_decl;
3133 TYPE_FIELDS (record) = f_stk;
3134 DECL_CHAIN (f_stk) = f_reg;
3135 DECL_CHAIN (f_reg) = f_ndx;
3137 layout_type (record);
3138 return record;
3142 /* Save the incoming argument registers on the stack. Returns the
3143 address of the saved registers. */
3145 static rtx
3146 xtensa_builtin_saveregs (void)
3148 rtx gp_regs;
3149 int arg_words = crtl->args.info.arg_words;
3150 int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
3152 if (gp_left <= 0)
3153 return const0_rtx;
3155 /* Allocate the general-purpose register space. */
3156 gp_regs = assign_stack_local
3157 (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
3158 set_mem_alias_set (gp_regs, get_varargs_alias_set ());
3160 /* Now store the incoming registers. */
3161 cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
3162 cfun->machine->vararg_a7 = true;
3163 move_block_from_reg (GP_ARG_FIRST + arg_words,
3164 adjust_address (gp_regs, BLKmode,
3165 arg_words * UNITS_PER_WORD),
3166 gp_left);
3167 if (cfun->machine->vararg_a7_copy != 0)
3168 emit_insn_before (cfun->machine->vararg_a7_copy, get_insns ());
3170 return XEXP (gp_regs, 0);
3174 /* Implement `va_start' for varargs and stdarg. We look at the
3175 current function to fill in an initial va_list. */
3177 static void
3178 xtensa_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
3180 tree f_stk, stk;
3181 tree f_reg, reg;
3182 tree f_ndx, ndx;
3183 tree t, u;
3184 int arg_words;
3186 arg_words = crtl->args.info.arg_words;
3188 f_stk = TYPE_FIELDS (va_list_type_node);
3189 f_reg = DECL_CHAIN (f_stk);
3190 f_ndx = DECL_CHAIN (f_reg);
3192 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk, NULL_TREE);
3193 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
3194 f_reg, NULL_TREE);
3195 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
3196 f_ndx, NULL_TREE);
3198 /* Call __builtin_saveregs; save the result in __va_reg */
3199 u = make_tree (sizetype, expand_builtin_saveregs ());
3200 u = fold_convert (ptr_type_node, u);
3201 t = build2 (MODIFY_EXPR, ptr_type_node, reg, u);
3202 TREE_SIDE_EFFECTS (t) = 1;
3203 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3205 /* Set the __va_stk member to ($arg_ptr - 32). */
3206 u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
3207 u = fold_build_pointer_plus_hwi (u, -32);
3208 t = build2 (MODIFY_EXPR, ptr_type_node, stk, u);
3209 TREE_SIDE_EFFECTS (t) = 1;
3210 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3212 /* Set the __va_ndx member. If the first variable argument is on
3213 the stack, adjust __va_ndx by 2 words to account for the extra
3214 alignment offset for __va_stk. */
3215 if (arg_words >= MAX_ARGS_IN_REGISTERS)
3216 arg_words += 2;
3217 t = build2 (MODIFY_EXPR, integer_type_node, ndx,
3218 build_int_cst (integer_type_node, arg_words * UNITS_PER_WORD));
3219 TREE_SIDE_EFFECTS (t) = 1;
3220 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3224 /* Implement `va_arg'. */
3226 static tree
3227 xtensa_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
3228 gimple_seq *post_p ATTRIBUTE_UNUSED)
3230 tree f_stk, stk;
3231 tree f_reg, reg;
3232 tree f_ndx, ndx;
3233 tree type_size, array, orig_ndx, addr, size, va_size, t;
3234 tree lab_false, lab_over, lab_false2;
3235 bool indirect;
3237 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3238 if (indirect)
3239 type = build_pointer_type (type);
3241 /* Handle complex values as separate real and imaginary parts. */
3242 if (TREE_CODE (type) == COMPLEX_TYPE)
3244 tree real_part, imag_part;
3246 real_part = xtensa_gimplify_va_arg_expr (valist, TREE_TYPE (type),
3247 pre_p, NULL);
3248 real_part = get_initialized_tmp_var (real_part, pre_p, NULL);
3250 imag_part = xtensa_gimplify_va_arg_expr (unshare_expr (valist),
3251 TREE_TYPE (type),
3252 pre_p, NULL);
3253 imag_part = get_initialized_tmp_var (imag_part, pre_p, NULL);
3255 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
3258 f_stk = TYPE_FIELDS (va_list_type_node);
3259 f_reg = DECL_CHAIN (f_stk);
3260 f_ndx = DECL_CHAIN (f_reg);
3262 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist,
3263 f_stk, NULL_TREE);
3264 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
3265 f_reg, NULL_TREE);
3266 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
3267 f_ndx, NULL_TREE);
3269 type_size = size_in_bytes (type);
3270 va_size = round_up (type_size, UNITS_PER_WORD);
3271 gimplify_expr (&va_size, pre_p, NULL, is_gimple_val, fb_rvalue);
3274 /* First align __va_ndx if necessary for this arg:
3276 orig_ndx = (AP).__va_ndx;
3277 if (__alignof__ (TYPE) > 4 )
3278 orig_ndx = ((orig_ndx + __alignof__ (TYPE) - 1)
3279 & -__alignof__ (TYPE)); */
3281 orig_ndx = get_initialized_tmp_var (ndx, pre_p, NULL);
3283 if (TYPE_ALIGN (type) > BITS_PER_WORD)
3285 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_UNIT;
3287 t = build2 (PLUS_EXPR, integer_type_node, unshare_expr (orig_ndx),
3288 build_int_cst (integer_type_node, align - 1));
3289 t = build2 (BIT_AND_EXPR, integer_type_node, t,
3290 build_int_cst (integer_type_node, -align));
3291 gimplify_assign (unshare_expr (orig_ndx), t, pre_p);
3295 /* Increment __va_ndx to point past the argument:
3297 (AP).__va_ndx = orig_ndx + __va_size (TYPE); */
3299 t = fold_convert (integer_type_node, va_size);
3300 t = build2 (PLUS_EXPR, integer_type_node, orig_ndx, t);
3301 gimplify_assign (unshare_expr (ndx), t, pre_p);
3304 /* Check if the argument is in registers:
3306 if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
3307 && !must_pass_in_stack (type))
3308 __array = (AP).__va_reg; */
3310 array = create_tmp_var (ptr_type_node);
3312 lab_over = NULL;
3313 if (!targetm.calls.must_pass_in_stack (TYPE_MODE (type), type))
3315 lab_false = create_artificial_label (UNKNOWN_LOCATION);
3316 lab_over = create_artificial_label (UNKNOWN_LOCATION);
3318 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (ndx),
3319 build_int_cst (integer_type_node,
3320 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
3321 t = build3 (COND_EXPR, void_type_node, t,
3322 build1 (GOTO_EXPR, void_type_node, lab_false),
3323 NULL_TREE);
3324 gimplify_and_add (t, pre_p);
3326 gimplify_assign (unshare_expr (array), reg, pre_p);
3328 t = build1 (GOTO_EXPR, void_type_node, lab_over);
3329 gimplify_and_add (t, pre_p);
3331 t = build1 (LABEL_EXPR, void_type_node, lab_false);
3332 gimplify_and_add (t, pre_p);
3336 /* ...otherwise, the argument is on the stack (never split between
3337 registers and the stack -- change __va_ndx if necessary):
3339 else
3341 if (orig_ndx <= __MAX_ARGS_IN_REGISTERS * 4)
3342 (AP).__va_ndx = 32 + __va_size (TYPE);
3343 __array = (AP).__va_stk;
3344 } */
3346 lab_false2 = create_artificial_label (UNKNOWN_LOCATION);
3348 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (orig_ndx),
3349 build_int_cst (integer_type_node,
3350 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
3351 t = build3 (COND_EXPR, void_type_node, t,
3352 build1 (GOTO_EXPR, void_type_node, lab_false2),
3353 NULL_TREE);
3354 gimplify_and_add (t, pre_p);
3356 t = size_binop (PLUS_EXPR, unshare_expr (va_size), size_int (32));
3357 t = fold_convert (integer_type_node, t);
3358 gimplify_assign (unshare_expr (ndx), t, pre_p);
3360 t = build1 (LABEL_EXPR, void_type_node, lab_false2);
3361 gimplify_and_add (t, pre_p);
3363 gimplify_assign (array, stk, pre_p);
3365 if (lab_over)
3367 t = build1 (LABEL_EXPR, void_type_node, lab_over);
3368 gimplify_and_add (t, pre_p);
3372 /* Given the base array pointer (__array) and index to the subsequent
3373 argument (__va_ndx), find the address:
3375 __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
3376 ? sizeof (TYPE)
3377 : __va_size (TYPE))
3379 The results are endian-dependent because values smaller than one word
3380 are aligned differently. */
3383 if (BYTES_BIG_ENDIAN && TREE_CODE (type_size) == INTEGER_CST)
3385 t = fold_build2 (GE_EXPR, boolean_type_node, unshare_expr (type_size),
3386 size_int (PARM_BOUNDARY / BITS_PER_UNIT));
3387 t = fold_build3 (COND_EXPR, sizetype, t, unshare_expr (va_size),
3388 unshare_expr (type_size));
3389 size = t;
3391 else
3392 size = unshare_expr (va_size);
3394 t = fold_convert (sizetype, unshare_expr (ndx));
3395 t = build2 (MINUS_EXPR, sizetype, t, size);
3396 addr = fold_build_pointer_plus (unshare_expr (array), t);
3398 addr = fold_convert (build_pointer_type (type), addr);
3399 if (indirect)
3400 addr = build_va_arg_indirect_ref (addr);
3401 return build_va_arg_indirect_ref (addr);
3405 /* Builtins. */
3407 enum xtensa_builtin
3409 XTENSA_BUILTIN_UMULSIDI3,
3410 XTENSA_BUILTIN_max
3414 static void
3415 xtensa_init_builtins (void)
3417 tree ftype, decl;
3419 ftype = build_function_type_list (unsigned_intDI_type_node,
3420 unsigned_intSI_type_node,
3421 unsigned_intSI_type_node, NULL_TREE);
3423 decl = add_builtin_function ("__builtin_umulsidi3", ftype,
3424 XTENSA_BUILTIN_UMULSIDI3, BUILT_IN_MD,
3425 "__umulsidi3", NULL_TREE);
3426 TREE_NOTHROW (decl) = 1;
3427 TREE_READONLY (decl) = 1;
3431 static tree
3432 xtensa_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED, tree *args,
3433 bool ignore ATTRIBUTE_UNUSED)
3435 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3436 tree arg0, arg1;
3438 switch (fcode)
3440 case XTENSA_BUILTIN_UMULSIDI3:
3441 arg0 = args[0];
3442 arg1 = args[1];
3443 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3444 || TARGET_MUL32_HIGH)
3445 return fold_build2 (MULT_EXPR, unsigned_intDI_type_node,
3446 fold_convert (unsigned_intDI_type_node, arg0),
3447 fold_convert (unsigned_intDI_type_node, arg1));
3448 break;
3450 default:
3451 internal_error ("bad builtin code");
3452 break;
3455 return NULL;
3459 static rtx
3460 xtensa_expand_builtin (tree exp, rtx target,
3461 rtx subtarget ATTRIBUTE_UNUSED,
3462 machine_mode mode ATTRIBUTE_UNUSED,
3463 int ignore)
3465 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
3466 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3468 switch (fcode)
3470 case XTENSA_BUILTIN_UMULSIDI3:
3471 /* The umulsidi3 builtin is just a mechanism to avoid calling the real
3472 __umulsidi3 function when the Xtensa configuration can directly
3473 implement it. If not, just call the function. */
3474 return expand_call (exp, target, ignore);
3476 default:
3477 internal_error ("bad builtin code");
3479 return NULL_RTX;
3482 /* Worker function for TARGET_PREFERRED_RELOAD_CLASS. */
3484 static reg_class_t
3485 xtensa_preferred_reload_class (rtx x, reg_class_t rclass)
3487 if (CONSTANT_P (x) && CONST_DOUBLE_P (x))
3488 return NO_REGS;
3490 /* Don't use the stack pointer or hard frame pointer for reloads!
3491 The hard frame pointer would normally be OK except that it may
3492 briefly hold an incoming argument in the prologue, and reload
3493 won't know that it is live because the hard frame pointer is
3494 treated specially. */
3496 if (rclass == AR_REGS || rclass == GR_REGS)
3497 return RL_REGS;
3499 return rclass;
3502 /* Worker function for TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
3504 static reg_class_t
3505 xtensa_preferred_output_reload_class (rtx x ATTRIBUTE_UNUSED,
3506 reg_class_t rclass)
3508 /* Don't use the stack pointer or hard frame pointer for reloads!
3509 The hard frame pointer would normally be OK except that it may
3510 briefly hold an incoming argument in the prologue, and reload
3511 won't know that it is live because the hard frame pointer is
3512 treated specially. */
3514 if (rclass == AR_REGS || rclass == GR_REGS)
3515 return RL_REGS;
3517 return rclass;
3520 /* Worker function for TARGET_SECONDARY_RELOAD. */
3522 static reg_class_t
3523 xtensa_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
3524 machine_mode mode, secondary_reload_info *sri)
3526 int regno;
3528 if (in_p && constantpool_mem_p (x))
3530 if (rclass == FP_REGS)
3531 return RL_REGS;
3533 if (mode == QImode)
3534 sri->icode = CODE_FOR_reloadqi_literal;
3535 else if (mode == HImode)
3536 sri->icode = CODE_FOR_reloadhi_literal;
3539 regno = xt_true_regnum (x);
3540 if (ACC_REG_P (regno))
3541 return ((rclass == GR_REGS || rclass == RL_REGS) ? NO_REGS : RL_REGS);
3542 if (rclass == ACC_REG)
3543 return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
3545 return NO_REGS;
3549 void
3550 order_regs_for_local_alloc (void)
3552 if (!leaf_function_p ())
3554 static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
3555 REG_ALLOC_ORDER;
3556 static const int reg_nonleaf_alloc_order_call0[FIRST_PSEUDO_REGISTER] =
3558 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 12, 13, 14, 15,
3560 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
3561 0, 1, 16, 17,
3565 memcpy (reg_alloc_order, TARGET_WINDOWED_ABI ?
3566 reg_nonleaf_alloc_order : reg_nonleaf_alloc_order_call0,
3567 FIRST_PSEUDO_REGISTER * sizeof (int));
3569 else
3571 int i, num_arg_regs;
3572 int nxt = 0;
3574 /* Use the AR registers in increasing order (skipping a0 and a1)
3575 but save the incoming argument registers for a last resort. */
3576 num_arg_regs = crtl->args.info.arg_words;
3577 if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
3578 num_arg_regs = MAX_ARGS_IN_REGISTERS;
3579 for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
3580 reg_alloc_order[nxt++] = i + num_arg_regs;
3581 for (i = 0; i < num_arg_regs; i++)
3582 reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
3584 /* List the coprocessor registers in order. */
3585 for (i = 0; i < BR_REG_NUM; i++)
3586 reg_alloc_order[nxt++] = BR_REG_FIRST + i;
3588 /* List the FP registers in order for now. */
3589 for (i = 0; i < 16; i++)
3590 reg_alloc_order[nxt++] = FP_REG_FIRST + i;
3592 /* GCC requires that we list *all* the registers.... */
3593 reg_alloc_order[nxt++] = 0; /* a0 = return address */
3594 reg_alloc_order[nxt++] = 1; /* a1 = stack pointer */
3595 reg_alloc_order[nxt++] = 16; /* pseudo frame pointer */
3596 reg_alloc_order[nxt++] = 17; /* pseudo arg pointer */
3598 reg_alloc_order[nxt++] = ACC_REG_FIRST; /* MAC16 accumulator */
3603 /* Some Xtensa targets support multiple bss sections. If the section
3604 name ends with ".bss", add SECTION_BSS to the flags. */
3606 static unsigned int
3607 xtensa_multibss_section_type_flags (tree decl, const char *name, int reloc)
3609 unsigned int flags = default_section_type_flags (decl, name, reloc);
3610 const char *suffix;
3612 suffix = strrchr (name, '.');
3613 if (suffix && strcmp (suffix, ".bss") == 0)
3615 if (!decl || (TREE_CODE (decl) == VAR_DECL
3616 && DECL_INITIAL (decl) == NULL_TREE))
3617 flags |= SECTION_BSS; /* @nobits */
3618 else
3619 warning (0, "only uninitialized variables can be placed in a "
3620 ".bss section");
3623 return flags;
3627 /* The literal pool stays with the function. */
3629 static section *
3630 xtensa_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
3631 rtx x ATTRIBUTE_UNUSED,
3632 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
3634 return function_section (current_function_decl);
3637 /* Worker function for TARGET_REGISTER_MOVE_COST. */
3639 static int
3640 xtensa_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3641 reg_class_t from, reg_class_t to)
3643 if (from == to && from != BR_REGS && to != BR_REGS)
3644 return 2;
3645 else if (reg_class_subset_p (from, AR_REGS)
3646 && reg_class_subset_p (to, AR_REGS))
3647 return 2;
3648 else if (reg_class_subset_p (from, AR_REGS) && to == ACC_REG)
3649 return 3;
3650 else if (from == ACC_REG && reg_class_subset_p (to, AR_REGS))
3651 return 3;
3652 else
3653 return 10;
3656 /* Worker function for TARGET_MEMORY_MOVE_COST. */
3658 static int
3659 xtensa_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3660 reg_class_t rclass ATTRIBUTE_UNUSED,
3661 bool in ATTRIBUTE_UNUSED)
3663 return 4;
3666 /* Compute a (partial) cost for rtx X. Return true if the complete
3667 cost has been computed, and false if subexpressions should be
3668 scanned. In either case, *TOTAL contains the cost result. */
3670 static bool
3671 xtensa_rtx_costs (rtx x, machine_mode mode, int outer_code,
3672 int opno ATTRIBUTE_UNUSED,
3673 int *total, bool speed ATTRIBUTE_UNUSED)
3675 int code = GET_CODE (x);
3677 switch (code)
3679 case CONST_INT:
3680 switch (outer_code)
3682 case SET:
3683 if (xtensa_simm12b (INTVAL (x)))
3685 *total = 4;
3686 return true;
3688 break;
3689 case PLUS:
3690 if (xtensa_simm8 (INTVAL (x))
3691 || xtensa_simm8x256 (INTVAL (x)))
3693 *total = 0;
3694 return true;
3696 break;
3697 case AND:
3698 if (xtensa_mask_immediate (INTVAL (x)))
3700 *total = 0;
3701 return true;
3703 break;
3704 case COMPARE:
3705 if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
3707 *total = 0;
3708 return true;
3710 break;
3711 case ASHIFT:
3712 case ASHIFTRT:
3713 case LSHIFTRT:
3714 case ROTATE:
3715 case ROTATERT:
3716 /* No way to tell if X is the 2nd operand so be conservative. */
3717 default: break;
3719 if (xtensa_simm12b (INTVAL (x)))
3720 *total = 5;
3721 else if (TARGET_CONST16)
3722 *total = COSTS_N_INSNS (2);
3723 else
3724 *total = 6;
3725 return true;
3727 case CONST:
3728 case LABEL_REF:
3729 case SYMBOL_REF:
3730 if (TARGET_CONST16)
3731 *total = COSTS_N_INSNS (2);
3732 else
3733 *total = 5;
3734 return true;
3736 case CONST_DOUBLE:
3737 if (TARGET_CONST16)
3738 *total = COSTS_N_INSNS (4);
3739 else
3740 *total = 7;
3741 return true;
3743 case MEM:
3745 int num_words =
3746 (GET_MODE_SIZE (mode) > UNITS_PER_WORD) ? 2 : 1;
3748 if (memory_address_p (mode, XEXP ((x), 0)))
3749 *total = COSTS_N_INSNS (num_words);
3750 else
3751 *total = COSTS_N_INSNS (2*num_words);
3752 return true;
3755 case FFS:
3756 case CTZ:
3757 *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
3758 return true;
3760 case CLZ:
3761 *total = COSTS_N_INSNS (TARGET_NSA ? 1 : 50);
3762 return true;
3764 case NOT:
3765 *total = COSTS_N_INSNS (mode == DImode ? 3 : 2);
3766 return true;
3768 case AND:
3769 case IOR:
3770 case XOR:
3771 if (mode == DImode)
3772 *total = COSTS_N_INSNS (2);
3773 else
3774 *total = COSTS_N_INSNS (1);
3775 return true;
3777 case ASHIFT:
3778 case ASHIFTRT:
3779 case LSHIFTRT:
3780 if (mode == DImode)
3781 *total = COSTS_N_INSNS (50);
3782 else
3783 *total = COSTS_N_INSNS (1);
3784 return true;
3786 case ABS:
3788 if (mode == SFmode)
3789 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3790 else if (mode == DFmode)
3791 *total = COSTS_N_INSNS (50);
3792 else
3793 *total = COSTS_N_INSNS (4);
3794 return true;
3797 case PLUS:
3798 case MINUS:
3800 if (mode == SFmode)
3801 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3802 else if (mode == DFmode || mode == DImode)
3803 *total = COSTS_N_INSNS (50);
3804 else
3805 *total = COSTS_N_INSNS (1);
3806 return true;
3809 case NEG:
3810 *total = COSTS_N_INSNS (mode == DImode ? 4 : 2);
3811 return true;
3813 case MULT:
3815 if (mode == SFmode)
3816 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
3817 else if (mode == DFmode)
3818 *total = COSTS_N_INSNS (50);
3819 else if (mode == DImode)
3820 *total = COSTS_N_INSNS (TARGET_MUL32_HIGH ? 10 : 50);
3821 else if (TARGET_MUL32)
3822 *total = COSTS_N_INSNS (4);
3823 else if (TARGET_MAC16)
3824 *total = COSTS_N_INSNS (16);
3825 else if (TARGET_MUL16)
3826 *total = COSTS_N_INSNS (12);
3827 else
3828 *total = COSTS_N_INSNS (50);
3829 return true;
3832 case DIV:
3833 case MOD:
3835 if (mode == SFmode)
3837 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
3838 return true;
3840 else if (mode == DFmode)
3842 *total = COSTS_N_INSNS (50);
3843 return true;
3846 /* Fall through. */
3848 case UDIV:
3849 case UMOD:
3851 if (mode == DImode)
3852 *total = COSTS_N_INSNS (50);
3853 else if (TARGET_DIV32)
3854 *total = COSTS_N_INSNS (32);
3855 else
3856 *total = COSTS_N_INSNS (50);
3857 return true;
3860 case SQRT:
3861 if (mode == SFmode)
3862 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
3863 else
3864 *total = COSTS_N_INSNS (50);
3865 return true;
3867 case SMIN:
3868 case UMIN:
3869 case SMAX:
3870 case UMAX:
3871 *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
3872 return true;
3874 case SIGN_EXTRACT:
3875 case SIGN_EXTEND:
3876 *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
3877 return true;
3879 case ZERO_EXTRACT:
3880 case ZERO_EXTEND:
3881 *total = COSTS_N_INSNS (1);
3882 return true;
3884 default:
3885 return false;
3889 /* Worker function for TARGET_RETURN_IN_MEMORY. */
3891 static bool
3892 xtensa_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3894 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
3895 > 4 * UNITS_PER_WORD);
3898 /* Worker function for TARGET_FUNCTION_VALUE. */
3901 xtensa_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
3902 bool outgoing)
3904 return gen_rtx_REG ((INTEGRAL_TYPE_P (valtype)
3905 && TYPE_PRECISION (valtype) < BITS_PER_WORD)
3906 ? SImode : TYPE_MODE (valtype),
3907 outgoing ? GP_OUTGOING_RETURN : GP_RETURN);
3910 /* Worker function for TARGET_LIBCALL_VALUE. */
3912 static rtx
3913 xtensa_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
3915 return gen_rtx_REG ((GET_MODE_CLASS (mode) == MODE_INT
3916 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3917 ? SImode : mode, GP_RETURN);
3920 /* Worker function TARGET_FUNCTION_VALUE_REGNO_P. */
3922 static bool
3923 xtensa_function_value_regno_p (const unsigned int regno)
3925 return (regno == GP_RETURN);
3928 /* The static chain is passed in memory. Provide rtx giving 'mem'
3929 expressions that denote where they are stored. */
3931 static rtx
3932 xtensa_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
3934 if (TARGET_WINDOWED_ABI)
3936 rtx base = incoming_p ? arg_pointer_rtx : stack_pointer_rtx;
3937 return gen_frame_mem (Pmode, plus_constant (Pmode, base,
3938 -5 * UNITS_PER_WORD));
3940 else
3941 return gen_rtx_REG (Pmode, A8_REG);
3945 /* TRAMPOLINE_TEMPLATE: For Xtensa, the trampoline must perform an ENTRY
3946 instruction with a minimal stack frame in order to get some free
3947 registers. Once the actual call target is known, the proper stack frame
3948 size is extracted from the ENTRY instruction at the target and the
3949 current frame is adjusted to match. The trampoline then transfers
3950 control to the instruction following the ENTRY at the target. Note:
3951 this assumes that the target begins with an ENTRY instruction. */
3953 static void
3954 xtensa_asm_trampoline_template (FILE *stream)
3956 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3958 fprintf (stream, "\t.begin no-transform\n");
3960 if (TARGET_WINDOWED_ABI)
3962 fprintf (stream, "\tentry\tsp, %d\n", MIN_FRAME_SIZE);
3964 if (use_call0)
3966 /* Save the return address. */
3967 fprintf (stream, "\tmov\ta10, a0\n");
3969 /* Use a CALL0 instruction to skip past the constants and in the
3970 process get the PC into A0. This allows PC-relative access to
3971 the constants without relying on L32R. */
3972 fprintf (stream, "\tcall0\t.Lskipconsts\n");
3974 else
3975 fprintf (stream, "\tj\t.Lskipconsts\n");
3977 fprintf (stream, "\t.align\t4\n");
3978 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
3979 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
3980 fprintf (stream, ".Lskipconsts:\n");
3982 /* Load the static chain and function address from the trampoline. */
3983 if (use_call0)
3985 fprintf (stream, "\taddi\ta0, a0, 3\n");
3986 fprintf (stream, "\tl32i\ta9, a0, 0\n");
3987 fprintf (stream, "\tl32i\ta8, a0, 4\n");
3989 else
3991 fprintf (stream, "\tl32r\ta9, .Lchainval\n");
3992 fprintf (stream, "\tl32r\ta8, .Lfnaddr\n");
3995 /* Store the static chain. */
3996 fprintf (stream, "\ts32i\ta9, sp, %d\n", MIN_FRAME_SIZE - 20);
3998 /* Set the proper stack pointer value. */
3999 fprintf (stream, "\tl32i\ta9, a8, 0\n");
4000 fprintf (stream, "\textui\ta9, a9, %d, 12\n",
4001 TARGET_BIG_ENDIAN ? 8 : 12);
4002 fprintf (stream, "\tslli\ta9, a9, 3\n");
4003 fprintf (stream, "\taddi\ta9, a9, %d\n", -MIN_FRAME_SIZE);
4004 fprintf (stream, "\tsub\ta9, sp, a9\n");
4005 fprintf (stream, "\tmovsp\tsp, a9\n");
4007 if (use_call0)
4008 /* Restore the return address. */
4009 fprintf (stream, "\tmov\ta0, a10\n");
4011 /* Jump to the instruction following the ENTRY. */
4012 fprintf (stream, "\taddi\ta8, a8, 3\n");
4013 fprintf (stream, "\tjx\ta8\n");
4015 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
4016 if (use_call0)
4017 fprintf (stream, "\t.byte\t0\n");
4018 else
4019 fprintf (stream, "\tnop\n");
4021 else
4023 if (use_call0)
4025 /* Save the return address. */
4026 fprintf (stream, "\tmov\ta10, a0\n");
4028 /* Use a CALL0 instruction to skip past the constants and in the
4029 process get the PC into A0. This allows PC-relative access to
4030 the constants without relying on L32R. */
4031 fprintf (stream, "\tcall0\t.Lskipconsts\n");
4033 else
4034 fprintf (stream, "\tj\t.Lskipconsts\n");
4036 fprintf (stream, "\t.align\t4\n");
4037 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
4038 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
4039 fprintf (stream, ".Lskipconsts:\n");
4041 /* Load the static chain and function address from the trampoline. */
4042 if (use_call0)
4044 fprintf (stream, "\taddi\ta0, a0, 3\n");
4045 fprintf (stream, "\tl32i\ta8, a0, 0\n");
4046 fprintf (stream, "\tl32i\ta9, a0, 4\n");
4047 fprintf (stream, "\tmov\ta0, a10\n");
4049 else
4051 fprintf (stream, "\tl32r\ta8, .Lchainval\n");
4052 fprintf (stream, "\tl32r\ta9, .Lfnaddr\n");
4054 fprintf (stream, "\tjx\ta9\n");
4056 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
4057 if (use_call0)
4058 fprintf (stream, "\t.byte\t0\n");
4059 else
4060 fprintf (stream, "\tnop\n");
4062 fprintf (stream, "\t.end no-transform\n");
4065 static void
4066 xtensa_trampoline_init (rtx m_tramp, tree fndecl, rtx chain)
4068 rtx func = XEXP (DECL_RTL (fndecl), 0);
4069 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
4070 int chain_off;
4071 int func_off;
4073 if (TARGET_WINDOWED_ABI)
4075 chain_off = use_call0 ? 12 : 8;
4076 func_off = use_call0 ? 16 : 12;
4078 else
4080 chain_off = use_call0 ? 8 : 4;
4081 func_off = use_call0 ? 12 : 8;
4084 emit_block_move (m_tramp, assemble_trampoline_template (),
4085 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4087 emit_move_insn (adjust_address (m_tramp, SImode, chain_off), chain);
4088 emit_move_insn (adjust_address (m_tramp, SImode, func_off), func);
4089 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_sync_caches"),
4090 LCT_NORMAL, VOIDmode, XEXP (m_tramp, 0), Pmode);
4093 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
4095 static bool
4096 xtensa_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
4098 return !xtensa_tls_referenced_p (x);
4101 /* Implement TARGET_CAN_USE_DOLOOP_P. */
4103 static bool
4104 xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
4105 unsigned int loop_depth, bool entered_at_top)
4107 /* Considering limitations in the hardware, only use doloop
4108 for innermost loops which must be entered from the top. */
4109 if (loop_depth > 1 || !entered_at_top)
4110 return false;
4112 return true;
4115 /* NULL if INSN insn is valid within a low-overhead loop.
4116 Otherwise return why doloop cannot be applied. */
4118 static const char *
4119 xtensa_invalid_within_doloop (const rtx_insn *insn)
4121 if (CALL_P (insn))
4122 return "Function call in the loop.";
4124 if (JUMP_P (insn) && INSN_CODE (insn) == CODE_FOR_return)
4125 return "Return from a call instruction in the loop.";
4127 return NULL;
4130 /* Optimize LOOP. */
4132 static bool
4133 hwloop_optimize (hwloop_info loop)
4135 int i;
4136 edge entry_edge;
4137 basic_block entry_bb;
4138 rtx iter_reg;
4139 rtx_insn *insn, *seq, *entry_after;
4141 if (loop->depth > 1)
4143 if (dump_file)
4144 fprintf (dump_file, ";; loop %d is not innermost\n",
4145 loop->loop_no);
4146 return false;
4149 if (!loop->incoming_dest)
4151 if (dump_file)
4152 fprintf (dump_file, ";; loop %d has more than one entry\n",
4153 loop->loop_no);
4154 return false;
4157 if (loop->incoming_dest != loop->head)
4159 if (dump_file)
4160 fprintf (dump_file, ";; loop %d is not entered from head\n",
4161 loop->loop_no);
4162 return false;
4165 if (loop->has_call || loop->has_asm)
4167 if (dump_file)
4168 fprintf (dump_file, ";; loop %d has invalid insn\n",
4169 loop->loop_no);
4170 return false;
4173 /* Scan all the blocks to make sure they don't use iter_reg. */
4174 if (loop->iter_reg_used || loop->iter_reg_used_outside)
4176 if (dump_file)
4177 fprintf (dump_file, ";; loop %d uses iterator\n",
4178 loop->loop_no);
4179 return false;
4182 /* Check if start_label appears before doloop_end. */
4183 insn = loop->start_label;
4184 while (insn && insn != loop->loop_end)
4185 insn = NEXT_INSN (insn);
4187 if (!insn)
4189 if (dump_file)
4190 fprintf (dump_file, ";; loop %d start_label not before loop_end\n",
4191 loop->loop_no);
4192 return false;
4195 /* Get the loop iteration register. */
4196 iter_reg = loop->iter_reg;
4198 gcc_assert (REG_P (iter_reg));
4200 entry_edge = NULL;
4202 FOR_EACH_VEC_SAFE_ELT (loop->incoming, i, entry_edge)
4203 if (entry_edge->flags & EDGE_FALLTHRU)
4204 break;
4206 if (entry_edge == NULL)
4207 return false;
4209 /* Place the zero_cost_loop_start instruction before the loop. */
4210 entry_bb = entry_edge->src;
4212 start_sequence ();
4214 insn = emit_insn (gen_zero_cost_loop_start (loop->iter_reg,
4215 loop->start_label,
4216 loop->iter_reg));
4218 seq = get_insns ();
4220 if (!single_succ_p (entry_bb) || vec_safe_length (loop->incoming) > 1)
4222 basic_block new_bb;
4223 edge e;
4224 edge_iterator ei;
4226 emit_insn_before (seq, BB_HEAD (loop->head));
4227 seq = emit_label_before (gen_label_rtx (), seq);
4228 new_bb = create_basic_block (seq, insn, entry_bb);
4229 FOR_EACH_EDGE (e, ei, loop->incoming)
4231 if (!(e->flags & EDGE_FALLTHRU))
4232 redirect_edge_and_branch_force (e, new_bb);
4233 else
4234 redirect_edge_succ (e, new_bb);
4237 make_edge (new_bb, loop->head, 0);
4239 else
4241 entry_after = BB_END (entry_bb);
4242 while (DEBUG_INSN_P (entry_after)
4243 || (NOTE_P (entry_after)
4244 && NOTE_KIND (entry_after) != NOTE_INSN_BASIC_BLOCK
4245 /* Make sure we don't split a call and its corresponding
4246 CALL_ARG_LOCATION note. */
4247 && NOTE_KIND (entry_after) != NOTE_INSN_CALL_ARG_LOCATION))
4248 entry_after = PREV_INSN (entry_after);
4250 emit_insn_after (seq, entry_after);
4253 end_sequence ();
4255 return true;
4258 /* A callback for the hw-doloop pass. Called when a loop we have discovered
4259 turns out not to be optimizable; we have to split the loop_end pattern into
4260 a subtract and a test. */
4262 static void
4263 hwloop_fail (hwloop_info loop)
4265 rtx test;
4266 rtx_insn *insn = loop->loop_end;
4268 emit_insn_before (gen_addsi3 (loop->iter_reg,
4269 loop->iter_reg,
4270 constm1_rtx),
4271 loop->loop_end);
4273 test = gen_rtx_NE (VOIDmode, loop->iter_reg, const0_rtx);
4274 insn = emit_jump_insn_before (gen_cbranchsi4 (test,
4275 loop->iter_reg, const0_rtx,
4276 loop->start_label),
4277 loop->loop_end);
4279 JUMP_LABEL (insn) = loop->start_label;
4280 LABEL_NUSES (loop->start_label)++;
4281 delete_insn (loop->loop_end);
4284 /* A callback for the hw-doloop pass. This function examines INSN; if
4285 it is a doloop_end pattern we recognize, return the reg rtx for the
4286 loop counter. Otherwise, return NULL_RTX. */
4288 static rtx
4289 hwloop_pattern_reg (rtx_insn *insn)
4291 rtx reg;
4293 if (!JUMP_P (insn) || recog_memoized (insn) != CODE_FOR_loop_end)
4294 return NULL_RTX;
4296 reg = SET_DEST (XVECEXP (PATTERN (insn), 0, 1));
4297 if (!REG_P (reg))
4298 return NULL_RTX;
4300 return reg;
4304 static struct hw_doloop_hooks xtensa_doloop_hooks =
4306 hwloop_pattern_reg,
4307 hwloop_optimize,
4308 hwloop_fail
4311 /* Run from machine_dependent_reorg, this pass looks for doloop_end insns
4312 and tries to rewrite the RTL of these loops so that proper Xtensa
4313 hardware loops are generated. */
4315 static void
4316 xtensa_reorg_loops (void)
4318 if (TARGET_LOOPS)
4319 reorg_loops (false, &xtensa_doloop_hooks);
4322 /* Implement the TARGET_MACHINE_DEPENDENT_REORG pass. */
4324 static void
4325 xtensa_reorg (void)
4327 /* We are freeing block_for_insn in the toplev to keep compatibility
4328 with old MDEP_REORGS that are not CFG based. Recompute it now. */
4329 compute_bb_for_insn ();
4331 df_analyze ();
4333 /* Doloop optimization. */
4334 xtensa_reorg_loops ();
4337 /* Update register usage after having seen the compiler flags. */
4339 static void
4340 xtensa_conditional_register_usage (void)
4342 unsigned i, c_mask;
4344 c_mask = TARGET_WINDOWED_ABI ? (1 << 1) : (1 << 2);
4346 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4348 /* Set/reset conditionally defined registers from
4349 CALL_USED_REGISTERS initializer. */
4350 if (call_used_regs[i] > 1)
4351 call_used_regs[i] = !!(call_used_regs[i] & c_mask);
4354 /* Remove hard FP register from the preferred reload registers set. */
4355 CLEAR_HARD_REG_BIT (reg_class_contents[(int)RL_REGS],
4356 HARD_FRAME_POINTER_REGNUM);
4359 /* Map hard register number to register class */
4361 enum reg_class xtensa_regno_to_class (int regno)
4363 static const enum reg_class regno_to_class[FIRST_PSEUDO_REGISTER] =
4365 RL_REGS, SP_REG, RL_REGS, RL_REGS,
4366 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
4367 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
4368 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
4369 AR_REGS, AR_REGS, BR_REGS,
4370 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4371 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4372 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4373 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
4374 ACC_REG,
4377 if (regno == HARD_FRAME_POINTER_REGNUM)
4378 return GR_REGS;
4379 else
4380 return regno_to_class[regno];
4383 #include "gt-xtensa.h"