gcc/
[official-gcc.git] / gcc / config / xtensa / xtensa.c
blob06e1eb78ada7012fa7ffa6c9fd33e1e5241657be
1 /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
2 Copyright (C) 2001-2014 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 "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "insn-attr.h"
33 #include "insn-codes.h"
34 #include "recog.h"
35 #include "output.h"
36 #include "tree.h"
37 #include "stringpool.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "varasm.h"
41 #include "expr.h"
42 #include "flags.h"
43 #include "reload.h"
44 #include "tm_p.h"
45 #include "function.h"
46 #include "diagnostic-core.h"
47 #include "optabs.h"
48 #include "libfuncs.h"
49 #include "ggc.h"
50 #include "target.h"
51 #include "target-def.h"
52 #include "langhooks.h"
53 #include "pointer-set.h"
54 #include "hash-table.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-fold.h"
58 #include "tree-eh.h"
59 #include "gimple-expr.h"
60 #include "is-a.h"
61 #include "gimple.h"
62 #include "gimplify.h"
63 #include "df.h"
66 /* Enumeration for all of the relational tests, so that we can build
67 arrays indexed by the test type, and not worry about the order
68 of EQ, NE, etc. */
70 enum internal_test
72 ITEST_EQ,
73 ITEST_NE,
74 ITEST_GT,
75 ITEST_GE,
76 ITEST_LT,
77 ITEST_LE,
78 ITEST_GTU,
79 ITEST_GEU,
80 ITEST_LTU,
81 ITEST_LEU,
82 ITEST_MAX
85 /* Array giving truth value on whether or not a given hard register
86 can support a given mode. */
87 char xtensa_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
89 /* Current frame size calculated by compute_frame_size. */
90 unsigned xtensa_current_frame_size;
92 /* Largest block move to handle in-line. */
93 #define LARGEST_MOVE_RATIO 15
95 /* Define the structure for the machine field in struct function. */
96 struct GTY(()) machine_function
98 int accesses_prev_frame;
99 bool need_a7_copy;
100 bool vararg_a7;
101 rtx vararg_a7_copy;
102 rtx set_frame_ptr_insn;
105 /* Vector, indexed by hard register number, which contains 1 for a
106 register that is allowable in a candidate for leaf function
107 treatment. */
109 const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
112 1, 1, 1,
113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
117 /* Map hard register number to register class */
118 const enum reg_class xtensa_regno_to_class[FIRST_PSEUDO_REGISTER] =
120 RL_REGS, SP_REG, RL_REGS, RL_REGS,
121 RL_REGS, RL_REGS, RL_REGS, GR_REGS,
122 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
123 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
124 AR_REGS, AR_REGS, BR_REGS,
125 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
126 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
127 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
128 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
129 ACC_REG,
132 static void xtensa_option_override (void);
133 static enum internal_test map_test_to_internal_test (enum rtx_code);
134 static rtx gen_int_relational (enum rtx_code, rtx, rtx, int *);
135 static rtx gen_float_relational (enum rtx_code, rtx, rtx);
136 static rtx gen_conditional_move (enum rtx_code, enum machine_mode, rtx, rtx);
137 static rtx fixup_subreg_mem (rtx);
138 static struct machine_function * xtensa_init_machine_status (void);
139 static rtx xtensa_legitimize_tls_address (rtx);
140 static rtx xtensa_legitimize_address (rtx, rtx, enum machine_mode);
141 static bool xtensa_mode_dependent_address_p (const_rtx, addr_space_t);
142 static bool xtensa_return_in_msb (const_tree);
143 static void printx (FILE *, signed int);
144 static void xtensa_function_epilogue (FILE *, HOST_WIDE_INT);
145 static rtx xtensa_builtin_saveregs (void);
146 static bool xtensa_legitimate_address_p (enum machine_mode, rtx, bool);
147 static unsigned int xtensa_multibss_section_type_flags (tree, const char *,
148 int) ATTRIBUTE_UNUSED;
149 static section *xtensa_select_rtx_section (enum machine_mode, rtx,
150 unsigned HOST_WIDE_INT);
151 static bool xtensa_rtx_costs (rtx, int, int, int, int *, bool);
152 static int xtensa_register_move_cost (enum machine_mode, reg_class_t,
153 reg_class_t);
154 static int xtensa_memory_move_cost (enum machine_mode, reg_class_t, bool);
155 static tree xtensa_build_builtin_va_list (void);
156 static bool xtensa_return_in_memory (const_tree, const_tree);
157 static tree xtensa_gimplify_va_arg_expr (tree, tree, gimple_seq *,
158 gimple_seq *);
159 static void xtensa_function_arg_advance (cumulative_args_t, enum machine_mode,
160 const_tree, bool);
161 static rtx xtensa_function_arg (cumulative_args_t, enum machine_mode,
162 const_tree, bool);
163 static rtx xtensa_function_incoming_arg (cumulative_args_t,
164 enum machine_mode, const_tree, bool);
165 static rtx xtensa_function_value (const_tree, const_tree, bool);
166 static rtx xtensa_libcall_value (enum machine_mode, const_rtx);
167 static bool xtensa_function_value_regno_p (const unsigned int);
168 static unsigned int xtensa_function_arg_boundary (enum machine_mode,
169 const_tree);
170 static void xtensa_init_builtins (void);
171 static tree xtensa_fold_builtin (tree, int, tree *, bool);
172 static rtx xtensa_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
173 static void xtensa_va_start (tree, rtx);
174 static bool xtensa_frame_pointer_required (void);
175 static rtx xtensa_static_chain (const_tree, bool);
176 static void xtensa_asm_trampoline_template (FILE *);
177 static void xtensa_trampoline_init (rtx, tree, rtx);
178 static bool xtensa_output_addr_const_extra (FILE *, rtx);
179 static bool xtensa_cannot_force_const_mem (enum machine_mode, rtx);
181 static reg_class_t xtensa_preferred_reload_class (rtx, reg_class_t);
182 static reg_class_t xtensa_preferred_output_reload_class (rtx, reg_class_t);
183 static reg_class_t xtensa_secondary_reload (bool, rtx, reg_class_t,
184 enum machine_mode,
185 struct secondary_reload_info *);
187 static bool constantpool_address_p (const_rtx addr);
188 static bool xtensa_legitimate_constant_p (enum machine_mode, rtx);
190 static bool xtensa_member_type_forces_blk (const_tree,
191 enum machine_mode mode);
193 static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
194 REG_ALLOC_ORDER;
197 /* This macro generates the assembly code for function exit,
198 on machines that need it. If FUNCTION_EPILOGUE is not defined
199 then individual return instructions are generated for each
200 return statement. Args are same as for FUNCTION_PROLOGUE. */
202 #undef TARGET_ASM_FUNCTION_EPILOGUE
203 #define TARGET_ASM_FUNCTION_EPILOGUE xtensa_function_epilogue
205 /* These hooks specify assembly directives for creating certain kinds
206 of integer object. */
208 #undef TARGET_ASM_ALIGNED_SI_OP
209 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
211 #undef TARGET_ASM_SELECT_RTX_SECTION
212 #define TARGET_ASM_SELECT_RTX_SECTION xtensa_select_rtx_section
214 #undef TARGET_LEGITIMIZE_ADDRESS
215 #define TARGET_LEGITIMIZE_ADDRESS xtensa_legitimize_address
216 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
217 #define TARGET_MODE_DEPENDENT_ADDRESS_P xtensa_mode_dependent_address_p
219 #undef TARGET_REGISTER_MOVE_COST
220 #define TARGET_REGISTER_MOVE_COST xtensa_register_move_cost
221 #undef TARGET_MEMORY_MOVE_COST
222 #define TARGET_MEMORY_MOVE_COST xtensa_memory_move_cost
223 #undef TARGET_RTX_COSTS
224 #define TARGET_RTX_COSTS xtensa_rtx_costs
225 #undef TARGET_ADDRESS_COST
226 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
228 #undef TARGET_MEMBER_TYPE_FORCES_BLK
229 #define TARGET_MEMBER_TYPE_FORCES_BLK xtensa_member_type_forces_blk
231 #undef TARGET_BUILD_BUILTIN_VA_LIST
232 #define TARGET_BUILD_BUILTIN_VA_LIST xtensa_build_builtin_va_list
234 #undef TARGET_EXPAND_BUILTIN_VA_START
235 #define TARGET_EXPAND_BUILTIN_VA_START xtensa_va_start
237 #undef TARGET_PROMOTE_FUNCTION_MODE
238 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
239 #undef TARGET_PROMOTE_PROTOTYPES
240 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
242 #undef TARGET_RETURN_IN_MEMORY
243 #define TARGET_RETURN_IN_MEMORY xtensa_return_in_memory
244 #undef TARGET_FUNCTION_VALUE
245 #define TARGET_FUNCTION_VALUE xtensa_function_value
246 #undef TARGET_LIBCALL_VALUE
247 #define TARGET_LIBCALL_VALUE xtensa_libcall_value
248 #undef TARGET_FUNCTION_VALUE_REGNO_P
249 #define TARGET_FUNCTION_VALUE_REGNO_P xtensa_function_value_regno_p
251 #undef TARGET_SPLIT_COMPLEX_ARG
252 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
253 #undef TARGET_MUST_PASS_IN_STACK
254 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
255 #undef TARGET_FUNCTION_ARG_ADVANCE
256 #define TARGET_FUNCTION_ARG_ADVANCE xtensa_function_arg_advance
257 #undef TARGET_FUNCTION_ARG
258 #define TARGET_FUNCTION_ARG xtensa_function_arg
259 #undef TARGET_FUNCTION_INCOMING_ARG
260 #define TARGET_FUNCTION_INCOMING_ARG xtensa_function_incoming_arg
261 #undef TARGET_FUNCTION_ARG_BOUNDARY
262 #define TARGET_FUNCTION_ARG_BOUNDARY xtensa_function_arg_boundary
264 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
265 #define TARGET_EXPAND_BUILTIN_SAVEREGS xtensa_builtin_saveregs
266 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
267 #define TARGET_GIMPLIFY_VA_ARG_EXPR xtensa_gimplify_va_arg_expr
269 #undef TARGET_RETURN_IN_MSB
270 #define TARGET_RETURN_IN_MSB xtensa_return_in_msb
272 #undef TARGET_INIT_BUILTINS
273 #define TARGET_INIT_BUILTINS xtensa_init_builtins
274 #undef TARGET_FOLD_BUILTIN
275 #define TARGET_FOLD_BUILTIN xtensa_fold_builtin
276 #undef TARGET_EXPAND_BUILTIN
277 #define TARGET_EXPAND_BUILTIN xtensa_expand_builtin
279 #undef TARGET_PREFERRED_RELOAD_CLASS
280 #define TARGET_PREFERRED_RELOAD_CLASS xtensa_preferred_reload_class
281 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
282 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS xtensa_preferred_output_reload_class
284 #undef TARGET_SECONDARY_RELOAD
285 #define TARGET_SECONDARY_RELOAD xtensa_secondary_reload
287 #undef TARGET_HAVE_TLS
288 #define TARGET_HAVE_TLS (TARGET_THREADPTR && HAVE_AS_TLS)
290 #undef TARGET_CANNOT_FORCE_CONST_MEM
291 #define TARGET_CANNOT_FORCE_CONST_MEM xtensa_cannot_force_const_mem
293 #undef TARGET_LEGITIMATE_ADDRESS_P
294 #define TARGET_LEGITIMATE_ADDRESS_P xtensa_legitimate_address_p
296 #undef TARGET_FRAME_POINTER_REQUIRED
297 #define TARGET_FRAME_POINTER_REQUIRED xtensa_frame_pointer_required
299 #undef TARGET_STATIC_CHAIN
300 #define TARGET_STATIC_CHAIN xtensa_static_chain
301 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
302 #define TARGET_ASM_TRAMPOLINE_TEMPLATE xtensa_asm_trampoline_template
303 #undef TARGET_TRAMPOLINE_INIT
304 #define TARGET_TRAMPOLINE_INIT xtensa_trampoline_init
306 #undef TARGET_OPTION_OVERRIDE
307 #define TARGET_OPTION_OVERRIDE xtensa_option_override
309 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
310 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA xtensa_output_addr_const_extra
312 #undef TARGET_LEGITIMATE_CONSTANT_P
313 #define TARGET_LEGITIMATE_CONSTANT_P xtensa_legitimate_constant_p
315 struct gcc_target targetm = TARGET_INITIALIZER;
318 /* Functions to test Xtensa immediate operand validity. */
320 bool
321 xtensa_simm8 (HOST_WIDE_INT v)
323 return v >= -128 && v <= 127;
327 bool
328 xtensa_simm8x256 (HOST_WIDE_INT v)
330 return (v & 255) == 0 && (v >= -32768 && v <= 32512);
334 bool
335 xtensa_simm12b (HOST_WIDE_INT v)
337 return v >= -2048 && v <= 2047;
341 static bool
342 xtensa_uimm8 (HOST_WIDE_INT v)
344 return v >= 0 && v <= 255;
348 static bool
349 xtensa_uimm8x2 (HOST_WIDE_INT v)
351 return (v & 1) == 0 && (v >= 0 && v <= 510);
355 static bool
356 xtensa_uimm8x4 (HOST_WIDE_INT v)
358 return (v & 3) == 0 && (v >= 0 && v <= 1020);
362 static bool
363 xtensa_b4const (HOST_WIDE_INT v)
365 switch (v)
367 case -1:
368 case 1:
369 case 2:
370 case 3:
371 case 4:
372 case 5:
373 case 6:
374 case 7:
375 case 8:
376 case 10:
377 case 12:
378 case 16:
379 case 32:
380 case 64:
381 case 128:
382 case 256:
383 return true;
385 return false;
389 bool
390 xtensa_b4const_or_zero (HOST_WIDE_INT v)
392 if (v == 0)
393 return true;
394 return xtensa_b4const (v);
398 bool
399 xtensa_b4constu (HOST_WIDE_INT v)
401 switch (v)
403 case 32768:
404 case 65536:
405 case 2:
406 case 3:
407 case 4:
408 case 5:
409 case 6:
410 case 7:
411 case 8:
412 case 10:
413 case 12:
414 case 16:
415 case 32:
416 case 64:
417 case 128:
418 case 256:
419 return true;
421 return false;
425 bool
426 xtensa_mask_immediate (HOST_WIDE_INT v)
428 #define MAX_MASK_SIZE 16
429 int mask_size;
431 for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
433 if ((v & 1) == 0)
434 return false;
435 v = v >> 1;
436 if (v == 0)
437 return true;
440 return false;
444 /* This is just like the standard true_regnum() function except that it
445 works even when reg_renumber is not initialized. */
448 xt_true_regnum (rtx x)
450 if (GET_CODE (x) == REG)
452 if (reg_renumber
453 && REGNO (x) >= FIRST_PSEUDO_REGISTER
454 && reg_renumber[REGNO (x)] >= 0)
455 return reg_renumber[REGNO (x)];
456 return REGNO (x);
458 if (GET_CODE (x) == SUBREG)
460 int base = xt_true_regnum (SUBREG_REG (x));
461 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
462 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
463 GET_MODE (SUBREG_REG (x)),
464 SUBREG_BYTE (x), GET_MODE (x));
466 return -1;
471 xtensa_valid_move (enum machine_mode mode, rtx *operands)
473 /* Either the destination or source must be a register, and the
474 MAC16 accumulator doesn't count. */
476 if (register_operand (operands[0], mode))
478 int dst_regnum = xt_true_regnum (operands[0]);
480 /* The stack pointer can only be assigned with a MOVSP opcode. */
481 if (dst_regnum == STACK_POINTER_REGNUM)
482 return (mode == SImode
483 && register_operand (operands[1], mode)
484 && !ACC_REG_P (xt_true_regnum (operands[1])));
486 if (!ACC_REG_P (dst_regnum))
487 return true;
489 if (register_operand (operands[1], mode))
491 int src_regnum = xt_true_regnum (operands[1]);
492 if (!ACC_REG_P (src_regnum))
493 return true;
495 return FALSE;
500 smalloffset_mem_p (rtx op)
502 if (GET_CODE (op) == MEM)
504 rtx addr = XEXP (op, 0);
505 if (GET_CODE (addr) == REG)
506 return BASE_REG_P (addr, 0);
507 if (GET_CODE (addr) == PLUS)
509 rtx offset = XEXP (addr, 0);
510 HOST_WIDE_INT val;
511 if (GET_CODE (offset) != CONST_INT)
512 offset = XEXP (addr, 1);
513 if (GET_CODE (offset) != CONST_INT)
514 return FALSE;
516 val = INTVAL (offset);
517 return (val & 3) == 0 && (val >= 0 && val <= 60);
520 return FALSE;
524 static bool
525 constantpool_address_p (const_rtx addr)
527 const_rtx sym = addr;
529 if (GET_CODE (addr) == CONST)
531 rtx offset;
533 /* Only handle (PLUS (SYM, OFFSET)) form. */
534 addr = XEXP (addr, 0);
535 if (GET_CODE (addr) != PLUS)
536 return false;
538 /* Make sure the address is word aligned. */
539 offset = XEXP (addr, 1);
540 if ((!CONST_INT_P (offset))
541 || ((INTVAL (offset) & 3) != 0))
542 return false;
544 sym = XEXP (addr, 0);
547 if ((GET_CODE (sym) == SYMBOL_REF)
548 && CONSTANT_POOL_ADDRESS_P (sym))
549 return true;
550 return false;
555 constantpool_mem_p (rtx op)
557 if (GET_CODE (op) == SUBREG)
558 op = SUBREG_REG (op);
559 if (GET_CODE (op) == MEM)
560 return constantpool_address_p (XEXP (op, 0));
561 return FALSE;
565 /* Return TRUE if X is a thread-local symbol. */
567 static bool
568 xtensa_tls_symbol_p (rtx x)
570 if (! TARGET_HAVE_TLS)
571 return false;
573 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
577 void
578 xtensa_extend_reg (rtx dst, rtx src)
580 rtx temp = gen_reg_rtx (SImode);
581 rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
583 /* Generate paradoxical subregs as needed so that the modes match. */
584 src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
585 dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
587 emit_insn (gen_ashlsi3 (temp, src, shift));
588 emit_insn (gen_ashrsi3 (dst, temp, shift));
592 bool
593 xtensa_mem_offset (unsigned v, enum machine_mode mode)
595 switch (mode)
597 case BLKmode:
598 /* Handle the worst case for block moves. See xtensa_expand_block_move
599 where we emit an optimized block move operation if the block can be
600 moved in < "move_ratio" pieces. The worst case is when the block is
601 aligned but has a size of (3 mod 4) (does this happen?) so that the
602 last piece requires a byte load/store. */
603 return (xtensa_uimm8 (v)
604 && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
606 case QImode:
607 return xtensa_uimm8 (v);
609 case HImode:
610 return xtensa_uimm8x2 (v);
612 case DFmode:
613 return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
615 default:
616 break;
619 return xtensa_uimm8x4 (v);
623 /* Make normal rtx_code into something we can index from an array. */
625 static enum internal_test
626 map_test_to_internal_test (enum rtx_code test_code)
628 enum internal_test test = ITEST_MAX;
630 switch (test_code)
632 default: break;
633 case EQ: test = ITEST_EQ; break;
634 case NE: test = ITEST_NE; break;
635 case GT: test = ITEST_GT; break;
636 case GE: test = ITEST_GE; break;
637 case LT: test = ITEST_LT; break;
638 case LE: test = ITEST_LE; break;
639 case GTU: test = ITEST_GTU; break;
640 case GEU: test = ITEST_GEU; break;
641 case LTU: test = ITEST_LTU; break;
642 case LEU: test = ITEST_LEU; break;
645 return test;
649 /* Generate the code to compare two integer values. The return value is
650 the comparison expression. */
652 static rtx
653 gen_int_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
654 rtx cmp0, /* first operand to compare */
655 rtx cmp1, /* second operand to compare */
656 int *p_invert /* whether branch needs to reverse test */)
658 struct cmp_info
660 enum rtx_code test_code; /* test code to use in insn */
661 bool (*const_range_p) (HOST_WIDE_INT); /* range check function */
662 int const_add; /* constant to add (convert LE -> LT) */
663 int reverse_regs; /* reverse registers in test */
664 int invert_const; /* != 0 if invert value if cmp1 is constant */
665 int invert_reg; /* != 0 if invert value if cmp1 is register */
666 int unsignedp; /* != 0 for unsigned comparisons. */
669 static struct cmp_info info[ (int)ITEST_MAX ] = {
671 { EQ, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* EQ */
672 { NE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* NE */
674 { LT, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* GT */
675 { GE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* GE */
676 { LT, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* LT */
677 { GE, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* LE */
679 { LTU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* GTU */
680 { GEU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* GEU */
681 { LTU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* LTU */
682 { GEU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* LEU */
685 enum internal_test test;
686 enum machine_mode mode;
687 struct cmp_info *p_info;
689 test = map_test_to_internal_test (test_code);
690 gcc_assert (test != ITEST_MAX);
692 p_info = &info[ (int)test ];
694 mode = GET_MODE (cmp0);
695 if (mode == VOIDmode)
696 mode = GET_MODE (cmp1);
698 /* Make sure we can handle any constants given to us. */
699 if (GET_CODE (cmp1) == CONST_INT)
701 HOST_WIDE_INT value = INTVAL (cmp1);
702 unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
704 /* if the immediate overflows or does not fit in the immediate field,
705 spill it to a register */
707 if ((p_info->unsignedp ?
708 (uvalue + p_info->const_add > uvalue) :
709 (value + p_info->const_add > value)) != (p_info->const_add > 0))
711 cmp1 = force_reg (mode, cmp1);
713 else if (!(p_info->const_range_p) (value + p_info->const_add))
715 cmp1 = force_reg (mode, cmp1);
718 else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
720 cmp1 = force_reg (mode, cmp1);
723 /* See if we need to invert the result. */
724 *p_invert = ((GET_CODE (cmp1) == CONST_INT)
725 ? p_info->invert_const
726 : p_info->invert_reg);
728 /* Comparison to constants, may involve adding 1 to change a LT into LE.
729 Comparison between two registers, may involve switching operands. */
730 if (GET_CODE (cmp1) == CONST_INT)
732 if (p_info->const_add != 0)
733 cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
736 else if (p_info->reverse_regs)
738 rtx temp = cmp0;
739 cmp0 = cmp1;
740 cmp1 = temp;
743 return gen_rtx_fmt_ee (p_info->test_code, VOIDmode, cmp0, cmp1);
747 /* Generate the code to compare two float values. The return value is
748 the comparison expression. */
750 static rtx
751 gen_float_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
752 rtx cmp0, /* first operand to compare */
753 rtx cmp1 /* second operand to compare */)
755 rtx (*gen_fn) (rtx, rtx, rtx);
756 rtx brtmp;
757 int reverse_regs, invert;
759 switch (test_code)
761 case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
762 case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
763 case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
764 case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
765 case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
766 case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
767 case UNEQ: reverse_regs = 0; invert = 0; gen_fn = gen_suneq_sf; break;
768 case LTGT: reverse_regs = 0; invert = 1; gen_fn = gen_suneq_sf; break;
769 case UNLE: reverse_regs = 0; invert = 0; gen_fn = gen_sunle_sf; break;
770 case UNGT: reverse_regs = 1; invert = 0; gen_fn = gen_sunlt_sf; break;
771 case UNLT: reverse_regs = 0; invert = 0; gen_fn = gen_sunlt_sf; break;
772 case UNGE: reverse_regs = 1; invert = 0; gen_fn = gen_sunle_sf; break;
773 case UNORDERED:
774 reverse_regs = 0; invert = 0; gen_fn = gen_sunordered_sf; break;
775 case ORDERED:
776 reverse_regs = 0; invert = 1; gen_fn = gen_sunordered_sf; break;
777 default:
778 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
779 reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
782 if (reverse_regs)
784 rtx temp = cmp0;
785 cmp0 = cmp1;
786 cmp1 = temp;
789 brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
790 emit_insn (gen_fn (brtmp, cmp0, cmp1));
792 return gen_rtx_fmt_ee (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
796 void
797 xtensa_expand_conditional_branch (rtx *operands, enum machine_mode mode)
799 enum rtx_code test_code = GET_CODE (operands[0]);
800 rtx cmp0 = operands[1];
801 rtx cmp1 = operands[2];
802 rtx cmp;
803 int invert;
804 rtx label1, label2;
806 switch (mode)
808 case DFmode:
809 default:
810 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
812 case SImode:
813 invert = FALSE;
814 cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
815 break;
817 case SFmode:
818 if (!TARGET_HARD_FLOAT)
819 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode,
820 cmp0, cmp1));
821 invert = FALSE;
822 cmp = gen_float_relational (test_code, cmp0, cmp1);
823 break;
826 /* Generate the branch. */
828 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
829 label2 = pc_rtx;
831 if (invert)
833 label2 = label1;
834 label1 = pc_rtx;
837 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
838 gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
839 label1,
840 label2)));
844 static rtx
845 gen_conditional_move (enum rtx_code code, enum machine_mode mode,
846 rtx op0, rtx op1)
848 if (mode == SImode)
850 rtx cmp;
852 /* Jump optimization calls get_condition() which canonicalizes
853 comparisons like (GE x <const>) to (GT x <const-1>).
854 Transform those comparisons back to GE, since that is the
855 comparison supported in Xtensa. We shouldn't have to
856 transform <LE x const> comparisons, because neither
857 xtensa_expand_conditional_branch() nor get_condition() will
858 produce them. */
860 if ((code == GT) && (op1 == constm1_rtx))
862 code = GE;
863 op1 = const0_rtx;
865 cmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
867 if (boolean_operator (cmp, VOIDmode))
869 /* Swap the operands to make const0 second. */
870 if (op0 == const0_rtx)
872 op0 = op1;
873 op1 = const0_rtx;
876 /* If not comparing against zero, emit a comparison (subtract). */
877 if (op1 != const0_rtx)
879 op0 = expand_binop (SImode, sub_optab, op0, op1,
880 0, 0, OPTAB_LIB_WIDEN);
881 op1 = const0_rtx;
884 else if (branch_operator (cmp, VOIDmode))
886 /* Swap the operands to make const0 second. */
887 if (op0 == const0_rtx)
889 op0 = op1;
890 op1 = const0_rtx;
892 switch (code)
894 case LT: code = GE; break;
895 case GE: code = LT; break;
896 default: gcc_unreachable ();
900 if (op1 != const0_rtx)
901 return 0;
903 else
904 return 0;
906 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
909 if (TARGET_HARD_FLOAT && mode == SFmode)
910 return gen_float_relational (code, op0, op1);
912 return 0;
917 xtensa_expand_conditional_move (rtx *operands, int isflt)
919 rtx dest = operands[0];
920 rtx cmp = operands[1];
921 enum machine_mode cmp_mode = GET_MODE (XEXP (cmp, 0));
922 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
924 if (!(cmp = gen_conditional_move (GET_CODE (cmp), cmp_mode,
925 XEXP (cmp, 0), XEXP (cmp, 1))))
926 return 0;
928 if (isflt)
929 gen_fn = (cmp_mode == SImode
930 ? gen_movsfcc_internal0
931 : gen_movsfcc_internal1);
932 else
933 gen_fn = (cmp_mode == SImode
934 ? gen_movsicc_internal0
935 : gen_movsicc_internal1);
937 emit_insn (gen_fn (dest, XEXP (cmp, 0), operands[2], operands[3], cmp));
938 return 1;
943 xtensa_expand_scc (rtx operands[4], enum machine_mode cmp_mode)
945 rtx dest = operands[0];
946 rtx cmp;
947 rtx one_tmp, zero_tmp;
948 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
950 if (!(cmp = gen_conditional_move (GET_CODE (operands[1]), cmp_mode,
951 operands[2], operands[3])))
952 return 0;
954 one_tmp = gen_reg_rtx (SImode);
955 zero_tmp = gen_reg_rtx (SImode);
956 emit_insn (gen_movsi (one_tmp, const_true_rtx));
957 emit_insn (gen_movsi (zero_tmp, const0_rtx));
959 gen_fn = (cmp_mode == SImode
960 ? gen_movsicc_internal0
961 : gen_movsicc_internal1);
962 emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
963 return 1;
967 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1]. MODE is
968 for the output, i.e., the input operands are twice as big as MODE. */
970 void
971 xtensa_split_operand_pair (rtx operands[4], enum machine_mode mode)
973 switch (GET_CODE (operands[1]))
975 case REG:
976 operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
977 operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
978 break;
980 case MEM:
981 operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
982 operands[2] = adjust_address (operands[1], mode, 0);
983 break;
985 case CONST_INT:
986 case CONST_DOUBLE:
987 split_double (operands[1], &operands[2], &operands[3]);
988 break;
990 default:
991 gcc_unreachable ();
994 switch (GET_CODE (operands[0]))
996 case REG:
997 operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
998 operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
999 break;
1001 case MEM:
1002 operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
1003 operands[0] = adjust_address (operands[0], mode, 0);
1004 break;
1006 default:
1007 gcc_unreachable ();
1012 /* Emit insns to move operands[1] into operands[0].
1013 Return 1 if we have written out everything that needs to be done to
1014 do the move. Otherwise, return 0 and the caller will emit the move
1015 normally. */
1018 xtensa_emit_move_sequence (rtx *operands, enum machine_mode mode)
1020 rtx src = operands[1];
1022 if (CONSTANT_P (src)
1023 && (GET_CODE (src) != CONST_INT || ! xtensa_simm12b (INTVAL (src))))
1025 rtx dst = operands[0];
1027 if (xtensa_tls_referenced_p (src))
1029 rtx addend = NULL;
1031 if (GET_CODE (src) == CONST && GET_CODE (XEXP (src, 0)) == PLUS)
1033 addend = XEXP (XEXP (src, 0), 1);
1034 src = XEXP (XEXP (src, 0), 0);
1037 src = xtensa_legitimize_tls_address (src);
1038 if (addend)
1040 src = gen_rtx_PLUS (mode, src, addend);
1041 src = force_operand (src, dst);
1043 emit_move_insn (dst, src);
1044 return 1;
1047 if (! TARGET_CONST16)
1049 src = force_const_mem (SImode, src);
1050 operands[1] = src;
1053 /* PC-relative loads are always SImode, and CONST16 is only
1054 supported in the movsi pattern, so add a SUBREG for any other
1055 (smaller) mode. */
1057 if (mode != SImode)
1059 if (register_operand (dst, mode))
1061 emit_move_insn (simplify_gen_subreg (SImode, dst, mode, 0), src);
1062 return 1;
1064 else
1066 src = force_reg (SImode, src);
1067 src = gen_lowpart_SUBREG (mode, src);
1068 operands[1] = src;
1073 if (!(reload_in_progress | reload_completed)
1074 && !xtensa_valid_move (mode, operands))
1075 operands[1] = force_reg (mode, operands[1]);
1077 operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1079 /* During reload we don't want to emit (subreg:X (mem:Y)) since that
1080 instruction won't be recognized after reload, so we remove the
1081 subreg and adjust mem accordingly. */
1082 if (reload_in_progress)
1084 operands[0] = fixup_subreg_mem (operands[0]);
1085 operands[1] = fixup_subreg_mem (operands[1]);
1087 return 0;
1091 static rtx
1092 fixup_subreg_mem (rtx x)
1094 if (GET_CODE (x) == SUBREG
1095 && GET_CODE (SUBREG_REG (x)) == REG
1096 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1098 rtx temp =
1099 gen_rtx_SUBREG (GET_MODE (x),
1100 reg_equiv_mem (REGNO (SUBREG_REG (x))),
1101 SUBREG_BYTE (x));
1102 x = alter_subreg (&temp, true);
1104 return x;
1108 /* Check if an incoming argument in a7 is expected to be used soon and
1109 if OPND is a register or register pair that includes a7. If so,
1110 create a new pseudo and copy a7 into that pseudo at the very
1111 beginning of the function, followed by the special "set_frame_ptr"
1112 unspec_volatile insn. The return value is either the original
1113 operand, if it is not a7, or the new pseudo containing a copy of
1114 the incoming argument. This is necessary because the register
1115 allocator will ignore conflicts with a7 and may either assign some
1116 other pseudo to a7 or use a7 as the hard_frame_pointer, clobbering
1117 the incoming argument in a7. By copying the argument out of a7 as
1118 the very first thing, and then immediately following that with an
1119 unspec_volatile to keep the scheduler away, we should avoid any
1120 problems. Putting the set_frame_ptr insn at the beginning, with
1121 only the a7 copy before it, also makes it easier for the prologue
1122 expander to initialize the frame pointer after the a7 copy and to
1123 fix up the a7 copy to use the stack pointer instead of the frame
1124 pointer. */
1127 xtensa_copy_incoming_a7 (rtx opnd)
1129 rtx entry_insns = 0;
1130 rtx reg, tmp;
1131 enum machine_mode mode;
1133 if (!cfun->machine->need_a7_copy)
1134 return opnd;
1136 /* This function should never be called again once a7 has been copied. */
1137 gcc_assert (!cfun->machine->set_frame_ptr_insn);
1139 mode = GET_MODE (opnd);
1141 /* The operand using a7 may come in a later instruction, so just return
1142 the original operand if it doesn't use a7. */
1143 reg = opnd;
1144 if (GET_CODE (reg) == SUBREG)
1146 gcc_assert (SUBREG_BYTE (reg) == 0);
1147 reg = SUBREG_REG (reg);
1149 if (GET_CODE (reg) != REG
1150 || REGNO (reg) > A7_REG
1151 || REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) <= A7_REG)
1152 return opnd;
1154 /* 1-word args will always be in a7; 2-word args in a6/a7. */
1155 gcc_assert (REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) - 1 == A7_REG);
1157 cfun->machine->need_a7_copy = false;
1159 /* Copy a7 to a new pseudo at the function entry. Use gen_raw_REG to
1160 create the REG for a7 so that hard_frame_pointer_rtx is not used. */
1162 start_sequence ();
1163 tmp = gen_reg_rtx (mode);
1165 switch (mode)
1167 case DFmode:
1168 case DImode:
1169 /* Copy the value out of A7 here but keep the first word in A6 until
1170 after the set_frame_ptr insn. Otherwise, the register allocator
1171 may decide to put "subreg (tmp, 0)" in A7 and clobber the incoming
1172 value. */
1173 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 4),
1174 gen_raw_REG (SImode, A7_REG)));
1175 break;
1176 case SFmode:
1177 emit_insn (gen_movsf_internal (tmp, gen_raw_REG (mode, A7_REG)));
1178 break;
1179 case SImode:
1180 emit_insn (gen_movsi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1181 break;
1182 case HImode:
1183 emit_insn (gen_movhi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1184 break;
1185 case QImode:
1186 emit_insn (gen_movqi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1187 break;
1188 default:
1189 gcc_unreachable ();
1192 cfun->machine->set_frame_ptr_insn = emit_insn (gen_set_frame_ptr ());
1194 /* For DF and DI mode arguments, copy the incoming value in A6 now. */
1195 if (mode == DFmode || mode == DImode)
1196 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 0),
1197 gen_rtx_REG (SImode, A7_REG - 1)));
1198 entry_insns = get_insns ();
1199 end_sequence ();
1201 if (cfun->machine->vararg_a7)
1203 /* This is called from within builtin_saveregs, which will insert the
1204 saveregs code at the function entry, ahead of anything placed at
1205 the function entry now. Instead, save the sequence to be inserted
1206 at the beginning of the saveregs code. */
1207 cfun->machine->vararg_a7_copy = entry_insns;
1209 else
1211 /* Put entry_insns after the NOTE that starts the function. If
1212 this is inside a start_sequence, make the outer-level insn
1213 chain current, so the code is placed at the start of the
1214 function. */
1215 push_topmost_sequence ();
1216 /* Do not use entry_of_function() here. This is called from within
1217 expand_function_start, when the CFG still holds GIMPLE. */
1218 emit_insn_after (entry_insns, get_insns ());
1219 pop_topmost_sequence ();
1222 return tmp;
1226 /* Try to expand a block move operation to a sequence of RTL move
1227 instructions. If not optimizing, or if the block size is not a
1228 constant, or if the block is too large, the expansion fails and GCC
1229 falls back to calling memcpy().
1231 operands[0] is the destination
1232 operands[1] is the source
1233 operands[2] is the length
1234 operands[3] is the alignment */
1237 xtensa_expand_block_move (rtx *operands)
1239 static const enum machine_mode mode_from_align[] =
1241 VOIDmode, QImode, HImode, VOIDmode, SImode,
1244 rtx dst_mem = operands[0];
1245 rtx src_mem = operands[1];
1246 HOST_WIDE_INT bytes, align;
1247 int num_pieces, move_ratio;
1248 rtx temp[2];
1249 enum machine_mode mode[2];
1250 int amount[2];
1251 bool active[2];
1252 int phase = 0;
1253 int next;
1254 int offset_ld = 0;
1255 int offset_st = 0;
1256 rtx x;
1258 /* If this is not a fixed size move, just call memcpy. */
1259 if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
1260 return 0;
1262 bytes = INTVAL (operands[2]);
1263 align = INTVAL (operands[3]);
1265 /* Anything to move? */
1266 if (bytes <= 0)
1267 return 0;
1269 if (align > MOVE_MAX)
1270 align = MOVE_MAX;
1272 /* Decide whether to expand inline based on the optimization level. */
1273 move_ratio = 4;
1274 if (optimize > 2)
1275 move_ratio = LARGEST_MOVE_RATIO;
1276 num_pieces = (bytes / align) + (bytes % align); /* Close enough anyway. */
1277 if (num_pieces > move_ratio)
1278 return 0;
1280 x = XEXP (dst_mem, 0);
1281 if (!REG_P (x))
1283 x = force_reg (Pmode, x);
1284 dst_mem = replace_equiv_address (dst_mem, x);
1287 x = XEXP (src_mem, 0);
1288 if (!REG_P (x))
1290 x = force_reg (Pmode, x);
1291 src_mem = replace_equiv_address (src_mem, x);
1294 active[0] = active[1] = false;
1298 next = phase;
1299 phase ^= 1;
1301 if (bytes > 0)
1303 int next_amount;
1305 next_amount = (bytes >= 4 ? 4 : (bytes >= 2 ? 2 : 1));
1306 next_amount = MIN (next_amount, align);
1308 amount[next] = next_amount;
1309 mode[next] = mode_from_align[next_amount];
1310 temp[next] = gen_reg_rtx (mode[next]);
1312 x = adjust_address (src_mem, mode[next], offset_ld);
1313 emit_insn (gen_rtx_SET (VOIDmode, temp[next], x));
1315 offset_ld += next_amount;
1316 bytes -= next_amount;
1317 active[next] = true;
1320 if (active[phase])
1322 active[phase] = false;
1324 x = adjust_address (dst_mem, mode[phase], offset_st);
1325 emit_insn (gen_rtx_SET (VOIDmode, x, temp[phase]));
1327 offset_st += amount[phase];
1330 while (active[next]);
1332 return 1;
1336 void
1337 xtensa_expand_nonlocal_goto (rtx *operands)
1339 rtx goto_handler = operands[1];
1340 rtx containing_fp = operands[3];
1342 /* Generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1343 is too big to generate in-line. */
1345 if (GET_CODE (containing_fp) != REG)
1346 containing_fp = force_reg (Pmode, containing_fp);
1348 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1349 LCT_NORMAL, VOIDmode, 2,
1350 containing_fp, Pmode,
1351 goto_handler, Pmode);
1355 static struct machine_function *
1356 xtensa_init_machine_status (void)
1358 return ggc_alloc_cleared_machine_function ();
1362 /* Shift VAL of mode MODE left by COUNT bits. */
1364 static inline rtx
1365 xtensa_expand_mask_and_shift (rtx val, enum machine_mode mode, rtx count)
1367 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
1368 NULL_RTX, 1, OPTAB_DIRECT);
1369 return expand_simple_binop (SImode, ASHIFT, val, count,
1370 NULL_RTX, 1, OPTAB_DIRECT);
1374 /* Structure to hold the initial parameters for a compare_and_swap operation
1375 in HImode and QImode. */
1377 struct alignment_context
1379 rtx memsi; /* SI aligned memory location. */
1380 rtx shift; /* Bit offset with regard to lsb. */
1381 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
1382 rtx modemaski; /* ~modemask */
1386 /* Initialize structure AC for word access to HI and QI mode memory. */
1388 static void
1389 init_alignment_context (struct alignment_context *ac, rtx mem)
1391 enum machine_mode mode = GET_MODE (mem);
1392 rtx byteoffset = NULL_RTX;
1393 bool aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
1395 if (aligned)
1396 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
1397 else
1399 /* Alignment is unknown. */
1400 rtx addr, align;
1402 /* Force the address into a register. */
1403 addr = force_reg (Pmode, XEXP (mem, 0));
1405 /* Align it to SImode. */
1406 align = expand_simple_binop (Pmode, AND, addr,
1407 GEN_INT (-GET_MODE_SIZE (SImode)),
1408 NULL_RTX, 1, OPTAB_DIRECT);
1409 /* Generate MEM. */
1410 ac->memsi = gen_rtx_MEM (SImode, align);
1411 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
1412 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
1413 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
1415 byteoffset = expand_simple_binop (Pmode, AND, addr,
1416 GEN_INT (GET_MODE_SIZE (SImode) - 1),
1417 NULL_RTX, 1, OPTAB_DIRECT);
1420 /* Calculate shiftcount. */
1421 if (TARGET_BIG_ENDIAN)
1423 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
1424 if (!aligned)
1425 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
1426 NULL_RTX, 1, OPTAB_DIRECT);
1428 else
1430 if (aligned)
1431 ac->shift = NULL_RTX;
1432 else
1433 ac->shift = byteoffset;
1436 if (ac->shift != NULL_RTX)
1438 /* Shift is the byte count, but we need the bitcount. */
1439 ac->shift = expand_simple_binop (SImode, MULT, ac->shift,
1440 GEN_INT (BITS_PER_UNIT),
1441 NULL_RTX, 1, OPTAB_DIRECT);
1442 ac->modemask = expand_simple_binop (SImode, ASHIFT,
1443 GEN_INT (GET_MODE_MASK (mode)),
1444 ac->shift,
1445 NULL_RTX, 1, OPTAB_DIRECT);
1447 else
1448 ac->modemask = GEN_INT (GET_MODE_MASK (mode));
1450 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask, NULL_RTX, 1);
1454 /* Expand an atomic compare and swap operation for HImode and QImode.
1455 MEM is the memory location, CMP the old value to compare MEM with
1456 and NEW_RTX the value to set if CMP == MEM. */
1458 void
1459 xtensa_expand_compare_and_swap (rtx target, rtx mem, rtx cmp, rtx new_rtx)
1461 enum machine_mode mode = GET_MODE (mem);
1462 struct alignment_context ac;
1463 rtx tmp, cmpv, newv, val;
1464 rtx oldval = gen_reg_rtx (SImode);
1465 rtx res = gen_reg_rtx (SImode);
1466 rtx csloop = gen_label_rtx ();
1467 rtx csend = gen_label_rtx ();
1469 init_alignment_context (&ac, mem);
1471 if (ac.shift != NULL_RTX)
1473 cmp = xtensa_expand_mask_and_shift (cmp, mode, ac.shift);
1474 new_rtx = xtensa_expand_mask_and_shift (new_rtx, mode, ac.shift);
1477 /* Load the surrounding word into VAL with the MEM value masked out. */
1478 val = force_reg (SImode, expand_simple_binop (SImode, AND, ac.memsi,
1479 ac.modemaski, NULL_RTX, 1,
1480 OPTAB_DIRECT));
1481 emit_label (csloop);
1483 /* Patch CMP and NEW_RTX into VAL at correct position. */
1484 cmpv = force_reg (SImode, expand_simple_binop (SImode, IOR, cmp, val,
1485 NULL_RTX, 1, OPTAB_DIRECT));
1486 newv = force_reg (SImode, expand_simple_binop (SImode, IOR, new_rtx, val,
1487 NULL_RTX, 1, OPTAB_DIRECT));
1489 /* Jump to end if we're done. */
1490 emit_insn (gen_sync_compare_and_swapsi (res, ac.memsi, cmpv, newv));
1491 emit_cmp_and_jump_insns (res, cmpv, EQ, const0_rtx, SImode, true, csend);
1493 /* Check for changes outside mode. */
1494 emit_move_insn (oldval, val);
1495 tmp = expand_simple_binop (SImode, AND, res, ac.modemaski,
1496 val, 1, OPTAB_DIRECT);
1497 if (tmp != val)
1498 emit_move_insn (val, tmp);
1500 /* Loop internal if so. */
1501 emit_cmp_and_jump_insns (oldval, val, NE, const0_rtx, SImode, true, csloop);
1503 emit_label (csend);
1505 /* Return the correct part of the bitfield. */
1506 convert_move (target,
1507 (ac.shift == NULL_RTX ? res
1508 : expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
1509 NULL_RTX, 1, OPTAB_DIRECT)),
1514 /* Expand an atomic operation CODE of mode MODE (either HImode or QImode --
1515 the default expansion works fine for SImode). MEM is the memory location
1516 and VAL the value to play with. If AFTER is true then store the value
1517 MEM holds after the operation, if AFTER is false then store the value MEM
1518 holds before the operation. If TARGET is zero then discard that value, else
1519 store it to TARGET. */
1521 void
1522 xtensa_expand_atomic (enum rtx_code code, rtx target, rtx mem, rtx val,
1523 bool after)
1525 enum machine_mode mode = GET_MODE (mem);
1526 struct alignment_context ac;
1527 rtx csloop = gen_label_rtx ();
1528 rtx cmp, tmp;
1529 rtx old = gen_reg_rtx (SImode);
1530 rtx new_rtx = gen_reg_rtx (SImode);
1531 rtx orig = NULL_RTX;
1533 init_alignment_context (&ac, mem);
1535 /* Prepare values before the compare-and-swap loop. */
1536 if (ac.shift != NULL_RTX)
1537 val = xtensa_expand_mask_and_shift (val, mode, ac.shift);
1538 switch (code)
1540 case PLUS:
1541 case MINUS:
1542 orig = gen_reg_rtx (SImode);
1543 convert_move (orig, val, 1);
1544 break;
1546 case SET:
1547 case IOR:
1548 case XOR:
1549 break;
1551 case MULT: /* NAND */
1552 case AND:
1553 /* val = "11..1<val>11..1" */
1554 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
1555 NULL_RTX, 1, OPTAB_DIRECT);
1556 break;
1558 default:
1559 gcc_unreachable ();
1562 /* Load full word. Subsequent loads are performed by S32C1I. */
1563 cmp = force_reg (SImode, ac.memsi);
1565 emit_label (csloop);
1566 emit_move_insn (old, cmp);
1568 switch (code)
1570 case PLUS:
1571 case MINUS:
1572 val = expand_simple_binop (SImode, code, old, orig,
1573 NULL_RTX, 1, OPTAB_DIRECT);
1574 val = expand_simple_binop (SImode, AND, val, ac.modemask,
1575 NULL_RTX, 1, OPTAB_DIRECT);
1576 /* FALLTHRU */
1577 case SET:
1578 tmp = expand_simple_binop (SImode, AND, old, ac.modemaski,
1579 NULL_RTX, 1, OPTAB_DIRECT);
1580 tmp = expand_simple_binop (SImode, IOR, tmp, val,
1581 new_rtx, 1, OPTAB_DIRECT);
1582 break;
1584 case AND:
1585 case IOR:
1586 case XOR:
1587 tmp = expand_simple_binop (SImode, code, old, val,
1588 new_rtx, 1, OPTAB_DIRECT);
1589 break;
1591 case MULT: /* NAND */
1592 tmp = expand_simple_binop (SImode, XOR, old, ac.modemask,
1593 NULL_RTX, 1, OPTAB_DIRECT);
1594 tmp = expand_simple_binop (SImode, AND, tmp, val,
1595 new_rtx, 1, OPTAB_DIRECT);
1596 break;
1598 default:
1599 gcc_unreachable ();
1602 if (tmp != new_rtx)
1603 emit_move_insn (new_rtx, tmp);
1604 emit_insn (gen_sync_compare_and_swapsi (cmp, ac.memsi, old, new_rtx));
1605 emit_cmp_and_jump_insns (cmp, old, NE, const0_rtx, SImode, true, csloop);
1607 if (target)
1609 tmp = (after ? new_rtx : cmp);
1610 convert_move (target,
1611 (ac.shift == NULL_RTX ? tmp
1612 : expand_simple_binop (SImode, LSHIFTRT, tmp, ac.shift,
1613 NULL_RTX, 1, OPTAB_DIRECT)),
1619 void
1620 xtensa_setup_frame_addresses (void)
1622 /* Set flag to cause TARGET_FRAME_POINTER_REQUIRED to return true. */
1623 cfun->machine->accesses_prev_frame = 1;
1625 emit_library_call
1626 (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
1627 LCT_NORMAL, VOIDmode, 0);
1631 /* Emit the assembly for the end of a zero-cost loop. Normally we just emit
1632 a comment showing where the end of the loop is. However, if there is a
1633 label or a branch at the end of the loop then we need to place a nop
1634 there. If the loop ends with a label we need the nop so that branches
1635 targeting that label will target the nop (and thus remain in the loop),
1636 instead of targeting the instruction after the loop (and thus exiting
1637 the loop). If the loop ends with a branch, we need the nop in case the
1638 branch is targeting a location inside the loop. When the branch
1639 executes it will cause the loop count to be decremented even if it is
1640 taken (because it is the last instruction in the loop), so we need to
1641 nop after the branch to prevent the loop count from being decremented
1642 when the branch is taken. */
1644 void
1645 xtensa_emit_loop_end (rtx insn, rtx *operands)
1647 char done = 0;
1649 for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
1651 switch (GET_CODE (insn))
1653 case NOTE:
1654 case BARRIER:
1655 break;
1657 case CODE_LABEL:
1658 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1659 done = 1;
1660 break;
1662 default:
1664 rtx body = PATTERN (insn);
1666 if (JUMP_P (body))
1668 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1669 done = 1;
1671 else if ((GET_CODE (body) != USE)
1672 && (GET_CODE (body) != CLOBBER))
1673 done = 1;
1675 break;
1679 output_asm_insn ("# loop end for %0", operands);
1683 char *
1684 xtensa_emit_branch (bool inverted, bool immed, rtx *operands)
1686 static char result[64];
1687 enum rtx_code code;
1688 const char *op;
1690 code = GET_CODE (operands[3]);
1691 switch (code)
1693 case EQ: op = inverted ? "ne" : "eq"; break;
1694 case NE: op = inverted ? "eq" : "ne"; break;
1695 case LT: op = inverted ? "ge" : "lt"; break;
1696 case GE: op = inverted ? "lt" : "ge"; break;
1697 case LTU: op = inverted ? "geu" : "ltu"; break;
1698 case GEU: op = inverted ? "ltu" : "geu"; break;
1699 default: gcc_unreachable ();
1702 if (immed)
1704 if (INTVAL (operands[1]) == 0)
1705 sprintf (result, "b%sz%s\t%%0, %%2", op,
1706 (TARGET_DENSITY && (code == EQ || code == NE)) ? ".n" : "");
1707 else
1708 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1710 else
1711 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1713 return result;
1717 char *
1718 xtensa_emit_bit_branch (bool inverted, bool immed, rtx *operands)
1720 static char result[64];
1721 const char *op;
1723 switch (GET_CODE (operands[3]))
1725 case EQ: op = inverted ? "bs" : "bc"; break;
1726 case NE: op = inverted ? "bc" : "bs"; break;
1727 default: gcc_unreachable ();
1730 if (immed)
1732 unsigned bitnum = INTVAL (operands[1]) & 0x1f;
1733 operands[1] = GEN_INT (bitnum);
1734 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1736 else
1737 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1739 return result;
1743 char *
1744 xtensa_emit_movcc (bool inverted, bool isfp, bool isbool, rtx *operands)
1746 static char result[64];
1747 enum rtx_code code;
1748 const char *op;
1750 code = GET_CODE (operands[4]);
1751 if (isbool)
1753 switch (code)
1755 case EQ: op = inverted ? "t" : "f"; break;
1756 case NE: op = inverted ? "f" : "t"; break;
1757 default: gcc_unreachable ();
1760 else
1762 switch (code)
1764 case EQ: op = inverted ? "nez" : "eqz"; break;
1765 case NE: op = inverted ? "eqz" : "nez"; break;
1766 case LT: op = inverted ? "gez" : "ltz"; break;
1767 case GE: op = inverted ? "ltz" : "gez"; break;
1768 default: gcc_unreachable ();
1772 sprintf (result, "mov%s%s\t%%0, %%%d, %%1",
1773 op, isfp ? ".s" : "", inverted ? 3 : 2);
1774 return result;
1778 char *
1779 xtensa_emit_call (int callop, rtx *operands)
1781 static char result[64];
1782 rtx tgt = operands[callop];
1784 if (GET_CODE (tgt) == CONST_INT)
1785 sprintf (result, "call8\t0x%lx", INTVAL (tgt));
1786 else if (register_operand (tgt, VOIDmode))
1787 sprintf (result, "callx8\t%%%d", callop);
1788 else
1789 sprintf (result, "call8\t%%%d", callop);
1791 return result;
1795 bool
1796 xtensa_legitimate_address_p (enum machine_mode mode, rtx addr, bool strict)
1798 /* Allow constant pool addresses. */
1799 if (mode != BLKmode && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
1800 && ! TARGET_CONST16 && constantpool_address_p (addr)
1801 && ! xtensa_tls_referenced_p (addr))
1802 return true;
1804 while (GET_CODE (addr) == SUBREG)
1805 addr = SUBREG_REG (addr);
1807 /* Allow base registers. */
1808 if (GET_CODE (addr) == REG && BASE_REG_P (addr, strict))
1809 return true;
1811 /* Check for "register + offset" addressing. */
1812 if (GET_CODE (addr) == PLUS)
1814 rtx xplus0 = XEXP (addr, 0);
1815 rtx xplus1 = XEXP (addr, 1);
1816 enum rtx_code code0;
1817 enum rtx_code code1;
1819 while (GET_CODE (xplus0) == SUBREG)
1820 xplus0 = SUBREG_REG (xplus0);
1821 code0 = GET_CODE (xplus0);
1823 while (GET_CODE (xplus1) == SUBREG)
1824 xplus1 = SUBREG_REG (xplus1);
1825 code1 = GET_CODE (xplus1);
1827 /* Swap operands if necessary so the register is first. */
1828 if (code0 != REG && code1 == REG)
1830 xplus0 = XEXP (addr, 1);
1831 xplus1 = XEXP (addr, 0);
1832 code0 = GET_CODE (xplus0);
1833 code1 = GET_CODE (xplus1);
1836 if (code0 == REG && BASE_REG_P (xplus0, strict)
1837 && code1 == CONST_INT
1838 && xtensa_mem_offset (INTVAL (xplus1), mode))
1839 return true;
1842 return false;
1846 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol. */
1848 static GTY(()) rtx xtensa_tls_module_base_symbol;
1850 static rtx
1851 xtensa_tls_module_base (void)
1853 if (! xtensa_tls_module_base_symbol)
1855 xtensa_tls_module_base_symbol =
1856 gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
1857 SYMBOL_REF_FLAGS (xtensa_tls_module_base_symbol)
1858 |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
1861 return xtensa_tls_module_base_symbol;
1865 static rtx
1866 xtensa_call_tls_desc (rtx sym, rtx *retp)
1868 rtx fn, arg, a10, call_insn, insns;
1870 start_sequence ();
1871 fn = gen_reg_rtx (Pmode);
1872 arg = gen_reg_rtx (Pmode);
1873 a10 = gen_rtx_REG (Pmode, 10);
1875 emit_insn (gen_tls_func (fn, sym));
1876 emit_insn (gen_tls_arg (arg, sym));
1877 emit_move_insn (a10, arg);
1878 call_insn = emit_call_insn (gen_tls_call (a10, fn, sym, const1_rtx));
1879 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn), a10);
1880 insns = get_insns ();
1881 end_sequence ();
1883 *retp = a10;
1884 return insns;
1888 static rtx
1889 xtensa_legitimize_tls_address (rtx x)
1891 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
1892 rtx dest, tp, ret, modbase, base, addend, insns;
1894 dest = gen_reg_rtx (Pmode);
1895 switch (model)
1897 case TLS_MODEL_GLOBAL_DYNAMIC:
1898 insns = xtensa_call_tls_desc (x, &ret);
1899 emit_libcall_block (insns, dest, ret, x);
1900 break;
1902 case TLS_MODEL_LOCAL_DYNAMIC:
1903 base = gen_reg_rtx (Pmode);
1904 modbase = xtensa_tls_module_base ();
1905 insns = xtensa_call_tls_desc (modbase, &ret);
1906 emit_libcall_block (insns, base, ret, modbase);
1907 addend = force_reg (SImode, gen_sym_DTPOFF (x));
1908 emit_insn (gen_addsi3 (dest, base, addend));
1909 break;
1911 case TLS_MODEL_INITIAL_EXEC:
1912 case TLS_MODEL_LOCAL_EXEC:
1913 tp = gen_reg_rtx (SImode);
1914 emit_insn (gen_get_thread_pointersi (tp));
1915 addend = force_reg (SImode, gen_sym_TPOFF (x));
1916 emit_insn (gen_addsi3 (dest, tp, addend));
1917 break;
1919 default:
1920 gcc_unreachable ();
1923 return dest;
1928 xtensa_legitimize_address (rtx x,
1929 rtx oldx ATTRIBUTE_UNUSED,
1930 enum machine_mode mode)
1932 if (xtensa_tls_symbol_p (x))
1933 return xtensa_legitimize_tls_address (x);
1935 if (GET_CODE (x) == PLUS)
1937 rtx plus0 = XEXP (x, 0);
1938 rtx plus1 = XEXP (x, 1);
1940 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
1942 plus0 = XEXP (x, 1);
1943 plus1 = XEXP (x, 0);
1946 /* Try to split up the offset to use an ADDMI instruction. */
1947 if (GET_CODE (plus0) == REG
1948 && GET_CODE (plus1) == CONST_INT
1949 && !xtensa_mem_offset (INTVAL (plus1), mode)
1950 && !xtensa_simm8 (INTVAL (plus1))
1951 && xtensa_mem_offset (INTVAL (plus1) & 0xff, mode)
1952 && xtensa_simm8x256 (INTVAL (plus1) & ~0xff))
1954 rtx temp = gen_reg_rtx (Pmode);
1955 rtx addmi_offset = GEN_INT (INTVAL (plus1) & ~0xff);
1956 emit_insn (gen_rtx_SET (Pmode, temp,
1957 gen_rtx_PLUS (Pmode, plus0, addmi_offset)));
1958 return gen_rtx_PLUS (Pmode, temp, GEN_INT (INTVAL (plus1) & 0xff));
1962 return x;
1965 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
1967 Treat constant-pool references as "mode dependent" since they can
1968 only be accessed with SImode loads. This works around a bug in the
1969 combiner where a constant pool reference is temporarily converted
1970 to an HImode load, which is then assumed to zero-extend based on
1971 our definition of LOAD_EXTEND_OP. This is wrong because the high
1972 bits of a 16-bit value in the constant pool are now sign-extended
1973 by default. */
1975 static bool
1976 xtensa_mode_dependent_address_p (const_rtx addr,
1977 addr_space_t as ATTRIBUTE_UNUSED)
1979 return constantpool_address_p (addr);
1982 /* Helper for xtensa_tls_referenced_p. */
1984 static int
1985 xtensa_tls_referenced_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1987 if (GET_CODE (*x) == SYMBOL_REF)
1988 return SYMBOL_REF_TLS_MODEL (*x) != 0;
1990 /* Ignore TLS references that have already been legitimized. */
1991 if (GET_CODE (*x) == UNSPEC)
1993 switch (XINT (*x, 1))
1995 case UNSPEC_TPOFF:
1996 case UNSPEC_DTPOFF:
1997 case UNSPEC_TLS_FUNC:
1998 case UNSPEC_TLS_ARG:
1999 case UNSPEC_TLS_CALL:
2000 return -1;
2001 default:
2002 break;
2006 return 0;
2010 /* Return TRUE if X contains any TLS symbol references. */
2012 bool
2013 xtensa_tls_referenced_p (rtx x)
2015 if (! TARGET_HAVE_TLS)
2016 return false;
2018 return for_each_rtx (&x, xtensa_tls_referenced_p_1, NULL);
2022 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2024 static bool
2025 xtensa_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2027 return xtensa_tls_referenced_p (x);
2031 /* Return the debugger register number to use for 'regno'. */
2034 xtensa_dbx_register_number (int regno)
2036 int first = -1;
2038 if (GP_REG_P (regno))
2040 regno -= GP_REG_FIRST;
2041 first = 0;
2043 else if (BR_REG_P (regno))
2045 regno -= BR_REG_FIRST;
2046 first = 16;
2048 else if (FP_REG_P (regno))
2050 regno -= FP_REG_FIRST;
2051 first = 48;
2053 else if (ACC_REG_P (regno))
2055 first = 0x200; /* Start of Xtensa special registers. */
2056 regno = 16; /* ACCLO is special register 16. */
2059 /* When optimizing, we sometimes get asked about pseudo-registers
2060 that don't represent hard registers. Return 0 for these. */
2061 if (first == -1)
2062 return 0;
2064 return first + regno;
2068 /* Argument support functions. */
2070 /* Initialize CUMULATIVE_ARGS for a function. */
2072 void
2073 init_cumulative_args (CUMULATIVE_ARGS *cum, int incoming)
2075 cum->arg_words = 0;
2076 cum->incoming = incoming;
2080 /* Advance the argument to the next argument position. */
2082 static void
2083 xtensa_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
2084 const_tree type, bool named ATTRIBUTE_UNUSED)
2086 int words, max;
2087 int *arg_words;
2089 arg_words = &get_cumulative_args (cum)->arg_words;
2090 max = MAX_ARGS_IN_REGISTERS;
2092 words = (((mode != BLKmode)
2093 ? (int) GET_MODE_SIZE (mode)
2094 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2096 if (*arg_words < max
2097 && (targetm.calls.must_pass_in_stack (mode, type)
2098 || *arg_words + words > max))
2099 *arg_words = max;
2101 *arg_words += words;
2105 /* Return an RTL expression containing the register for the given mode,
2106 or 0 if the argument is to be passed on the stack. INCOMING_P is nonzero
2107 if this is an incoming argument to the current function. */
2109 static rtx
2110 xtensa_function_arg_1 (cumulative_args_t cum_v, enum machine_mode mode,
2111 const_tree type, bool incoming_p)
2113 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2114 int regbase, words, max;
2115 int *arg_words;
2116 int regno;
2118 arg_words = &cum->arg_words;
2119 regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
2120 max = MAX_ARGS_IN_REGISTERS;
2122 words = (((mode != BLKmode)
2123 ? (int) GET_MODE_SIZE (mode)
2124 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2126 if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
2128 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_WORD;
2129 *arg_words = (*arg_words + align - 1) & -align;
2132 if (*arg_words + words > max)
2133 return (rtx)0;
2135 regno = regbase + *arg_words;
2137 if (cum->incoming && regno <= A7_REG && regno + words > A7_REG)
2138 cfun->machine->need_a7_copy = true;
2140 return gen_rtx_REG (mode, regno);
2143 /* Implement TARGET_FUNCTION_ARG. */
2145 static rtx
2146 xtensa_function_arg (cumulative_args_t cum, enum machine_mode mode,
2147 const_tree type, bool named ATTRIBUTE_UNUSED)
2149 return xtensa_function_arg_1 (cum, mode, type, false);
2152 /* Implement TARGET_FUNCTION_INCOMING_ARG. */
2154 static rtx
2155 xtensa_function_incoming_arg (cumulative_args_t cum, enum machine_mode mode,
2156 const_tree type, bool named ATTRIBUTE_UNUSED)
2158 return xtensa_function_arg_1 (cum, mode, type, true);
2161 static unsigned int
2162 xtensa_function_arg_boundary (enum machine_mode mode, const_tree type)
2164 unsigned int alignment;
2166 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2167 if (alignment < PARM_BOUNDARY)
2168 alignment = PARM_BOUNDARY;
2169 if (alignment > STACK_BOUNDARY)
2170 alignment = STACK_BOUNDARY;
2171 return alignment;
2175 static bool
2176 xtensa_return_in_msb (const_tree valtype)
2178 return (TARGET_BIG_ENDIAN
2179 && AGGREGATE_TYPE_P (valtype)
2180 && int_size_in_bytes (valtype) >= UNITS_PER_WORD);
2184 static void
2185 xtensa_option_override (void)
2187 int regno;
2188 enum machine_mode mode;
2190 if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
2191 error ("boolean registers required for the floating-point option");
2193 /* Set up array giving whether a given register can hold a given mode. */
2194 for (mode = VOIDmode;
2195 mode != MAX_MACHINE_MODE;
2196 mode = (enum machine_mode) ((int) mode + 1))
2198 int size = GET_MODE_SIZE (mode);
2199 enum mode_class mclass = GET_MODE_CLASS (mode);
2201 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2203 int temp;
2205 if (ACC_REG_P (regno))
2206 temp = (TARGET_MAC16
2207 && (mclass == MODE_INT) && (size <= UNITS_PER_WORD));
2208 else if (GP_REG_P (regno))
2209 temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
2210 else if (FP_REG_P (regno))
2211 temp = (TARGET_HARD_FLOAT && (mode == SFmode));
2212 else if (BR_REG_P (regno))
2213 temp = (TARGET_BOOLEANS && (mode == CCmode));
2214 else
2215 temp = FALSE;
2217 xtensa_hard_regno_mode_ok[(int) mode][regno] = temp;
2221 init_machine_status = xtensa_init_machine_status;
2223 /* Check PIC settings. PIC is only supported when using L32R
2224 instructions, and some targets need to always use PIC. */
2225 if (flag_pic && TARGET_CONST16)
2226 error ("-f%s is not supported with CONST16 instructions",
2227 (flag_pic > 1 ? "PIC" : "pic"));
2228 else if (TARGET_FORCE_NO_PIC)
2229 flag_pic = 0;
2230 else if (XTENSA_ALWAYS_PIC)
2232 if (TARGET_CONST16)
2233 error ("PIC is required but not supported with CONST16 instructions");
2234 flag_pic = 1;
2236 /* There's no need for -fPIC (as opposed to -fpic) on Xtensa. */
2237 if (flag_pic > 1)
2238 flag_pic = 1;
2239 if (flag_pic && !flag_pie)
2240 flag_shlib = 1;
2242 /* Hot/cold partitioning does not work on this architecture, because of
2243 constant pools (the load instruction cannot necessarily reach that far).
2244 Therefore disable it on this architecture. */
2245 if (flag_reorder_blocks_and_partition)
2247 flag_reorder_blocks_and_partition = 0;
2248 flag_reorder_blocks = 1;
2252 /* A C compound statement to output to stdio stream STREAM the
2253 assembler syntax for an instruction operand X. X is an RTL
2254 expression.
2256 CODE is a value that can be used to specify one of several ways
2257 of printing the operand. It is used when identical operands
2258 must be printed differently depending on the context. CODE
2259 comes from the '%' specification that was used to request
2260 printing of the operand. If the specification was just '%DIGIT'
2261 then CODE is 0; if the specification was '%LTR DIGIT' then CODE
2262 is the ASCII code for LTR.
2264 If X is a register, this macro should print the register's name.
2265 The names can be found in an array 'reg_names' whose type is
2266 'char *[]'. 'reg_names' is initialized from 'REGISTER_NAMES'.
2268 When the machine description has a specification '%PUNCT' (a '%'
2269 followed by a punctuation character), this macro is called with
2270 a null pointer for X and the punctuation character for CODE.
2272 'a', 'c', 'l', and 'n' are reserved.
2274 The Xtensa specific codes are:
2276 'd' CONST_INT, print as signed decimal
2277 'x' CONST_INT, print as signed hexadecimal
2278 'K' CONST_INT, print number of bits in mask for EXTUI
2279 'R' CONST_INT, print (X & 0x1f)
2280 'L' CONST_INT, print ((32 - X) & 0x1f)
2281 'D' REG, print second register of double-word register operand
2282 'N' MEM, print address of next word following a memory operand
2283 'v' MEM, if memory reference is volatile, output a MEMW before it
2284 't' any constant, add "@h" suffix for top 16 bits
2285 'b' any constant, add "@l" suffix for bottom 16 bits
2288 static void
2289 printx (FILE *file, signed int val)
2291 /* Print a hexadecimal value in a nice way. */
2292 if ((val > -0xa) && (val < 0xa))
2293 fprintf (file, "%d", val);
2294 else if (val < 0)
2295 fprintf (file, "-0x%x", -val);
2296 else
2297 fprintf (file, "0x%x", val);
2301 void
2302 print_operand (FILE *file, rtx x, int letter)
2304 if (!x)
2305 error ("PRINT_OPERAND null pointer");
2307 switch (letter)
2309 case 'D':
2310 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2311 fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
2312 else
2313 output_operand_lossage ("invalid %%D value");
2314 break;
2316 case 'v':
2317 if (GET_CODE (x) == MEM)
2319 /* For a volatile memory reference, emit a MEMW before the
2320 load or store. */
2321 if (MEM_VOLATILE_P (x) && TARGET_SERIALIZE_VOLATILE)
2322 fprintf (file, "memw\n\t");
2324 else
2325 output_operand_lossage ("invalid %%v value");
2326 break;
2328 case 'N':
2329 if (GET_CODE (x) == MEM
2330 && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
2332 x = adjust_address (x, GET_MODE (x) == DFmode ? SFmode : SImode, 4);
2333 output_address (XEXP (x, 0));
2335 else
2336 output_operand_lossage ("invalid %%N value");
2337 break;
2339 case 'K':
2340 if (GET_CODE (x) == CONST_INT)
2342 int num_bits = 0;
2343 unsigned val = INTVAL (x);
2344 while (val & 1)
2346 num_bits += 1;
2347 val = val >> 1;
2349 if ((val != 0) || (num_bits == 0) || (num_bits > 16))
2350 fatal_insn ("invalid mask", x);
2352 fprintf (file, "%d", num_bits);
2354 else
2355 output_operand_lossage ("invalid %%K value");
2356 break;
2358 case 'L':
2359 if (GET_CODE (x) == CONST_INT)
2360 fprintf (file, "%ld", (32 - INTVAL (x)) & 0x1f);
2361 else
2362 output_operand_lossage ("invalid %%L value");
2363 break;
2365 case 'R':
2366 if (GET_CODE (x) == CONST_INT)
2367 fprintf (file, "%ld", INTVAL (x) & 0x1f);
2368 else
2369 output_operand_lossage ("invalid %%R value");
2370 break;
2372 case 'x':
2373 if (GET_CODE (x) == CONST_INT)
2374 printx (file, INTVAL (x));
2375 else
2376 output_operand_lossage ("invalid %%x value");
2377 break;
2379 case 'd':
2380 if (GET_CODE (x) == CONST_INT)
2381 fprintf (file, "%ld", INTVAL (x));
2382 else
2383 output_operand_lossage ("invalid %%d value");
2384 break;
2386 case 't':
2387 case 'b':
2388 if (GET_CODE (x) == CONST_INT)
2390 printx (file, INTVAL (x));
2391 fputs (letter == 't' ? "@h" : "@l", file);
2393 else if (GET_CODE (x) == CONST_DOUBLE)
2395 REAL_VALUE_TYPE r;
2396 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2397 if (GET_MODE (x) == SFmode)
2399 long l;
2400 REAL_VALUE_TO_TARGET_SINGLE (r, l);
2401 fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
2403 else
2404 output_operand_lossage ("invalid %%t/%%b value");
2406 else if (GET_CODE (x) == CONST)
2408 /* X must be a symbolic constant on ELF. Write an expression
2409 suitable for 'const16' that sets the high or low 16 bits. */
2410 if (GET_CODE (XEXP (x, 0)) != PLUS
2411 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
2412 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2413 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
2414 output_operand_lossage ("invalid %%t/%%b value");
2415 print_operand (file, XEXP (XEXP (x, 0), 0), 0);
2416 fputs (letter == 't' ? "@h" : "@l", file);
2417 /* There must be a non-alphanumeric character between 'h' or 'l'
2418 and the number. The '-' is added by print_operand() already. */
2419 if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
2420 fputs ("+", file);
2421 print_operand (file, XEXP (XEXP (x, 0), 1), 0);
2423 else
2425 output_addr_const (file, x);
2426 fputs (letter == 't' ? "@h" : "@l", file);
2428 break;
2430 default:
2431 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2432 fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
2433 else if (GET_CODE (x) == MEM)
2434 output_address (XEXP (x, 0));
2435 else if (GET_CODE (x) == CONST_INT)
2436 fprintf (file, "%ld", INTVAL (x));
2437 else
2438 output_addr_const (file, x);
2443 /* A C compound statement to output to stdio stream STREAM the
2444 assembler syntax for an instruction operand that is a memory
2445 reference whose address is ADDR. ADDR is an RTL expression. */
2447 void
2448 print_operand_address (FILE *file, rtx addr)
2450 if (!addr)
2451 error ("PRINT_OPERAND_ADDRESS, null pointer");
2453 switch (GET_CODE (addr))
2455 default:
2456 fatal_insn ("invalid address", addr);
2457 break;
2459 case REG:
2460 fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
2461 break;
2463 case PLUS:
2465 rtx reg = (rtx)0;
2466 rtx offset = (rtx)0;
2467 rtx arg0 = XEXP (addr, 0);
2468 rtx arg1 = XEXP (addr, 1);
2470 if (GET_CODE (arg0) == REG)
2472 reg = arg0;
2473 offset = arg1;
2475 else if (GET_CODE (arg1) == REG)
2477 reg = arg1;
2478 offset = arg0;
2480 else
2481 fatal_insn ("no register in address", addr);
2483 if (CONSTANT_P (offset))
2485 fprintf (file, "%s, ", reg_names [REGNO (reg)]);
2486 output_addr_const (file, offset);
2488 else
2489 fatal_insn ("address offset not a constant", addr);
2491 break;
2493 case LABEL_REF:
2494 case SYMBOL_REF:
2495 case CONST_INT:
2496 case CONST:
2497 output_addr_const (file, addr);
2498 break;
2502 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
2504 static bool
2505 xtensa_output_addr_const_extra (FILE *fp, rtx x)
2507 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
2509 switch (XINT (x, 1))
2511 case UNSPEC_TPOFF:
2512 output_addr_const (fp, XVECEXP (x, 0, 0));
2513 fputs ("@TPOFF", fp);
2514 return true;
2515 case UNSPEC_DTPOFF:
2516 output_addr_const (fp, XVECEXP (x, 0, 0));
2517 fputs ("@DTPOFF", fp);
2518 return true;
2519 case UNSPEC_PLT:
2520 if (flag_pic)
2522 output_addr_const (fp, XVECEXP (x, 0, 0));
2523 fputs ("@PLT", fp);
2524 return true;
2526 break;
2527 default:
2528 break;
2531 return false;
2535 void
2536 xtensa_output_literal (FILE *file, rtx x, enum machine_mode mode, int labelno)
2538 long value_long[2];
2539 REAL_VALUE_TYPE r;
2540 int size;
2541 rtx first, second;
2543 fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
2545 switch (GET_MODE_CLASS (mode))
2547 case MODE_FLOAT:
2548 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
2550 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2551 switch (mode)
2553 case SFmode:
2554 REAL_VALUE_TO_TARGET_SINGLE (r, value_long[0]);
2555 if (HOST_BITS_PER_LONG > 32)
2556 value_long[0] &= 0xffffffff;
2557 fprintf (file, "0x%08lx\n", value_long[0]);
2558 break;
2560 case DFmode:
2561 REAL_VALUE_TO_TARGET_DOUBLE (r, value_long);
2562 if (HOST_BITS_PER_LONG > 32)
2564 value_long[0] &= 0xffffffff;
2565 value_long[1] &= 0xffffffff;
2567 fprintf (file, "0x%08lx, 0x%08lx\n",
2568 value_long[0], value_long[1]);
2569 break;
2571 default:
2572 gcc_unreachable ();
2575 break;
2577 case MODE_INT:
2578 case MODE_PARTIAL_INT:
2579 size = GET_MODE_SIZE (mode);
2580 switch (size)
2582 case 4:
2583 output_addr_const (file, x);
2584 fputs ("\n", file);
2585 break;
2587 case 8:
2588 split_double (x, &first, &second);
2589 output_addr_const (file, first);
2590 fputs (", ", file);
2591 output_addr_const (file, second);
2592 fputs ("\n", file);
2593 break;
2595 default:
2596 gcc_unreachable ();
2598 break;
2600 default:
2601 gcc_unreachable ();
2606 /* Return the bytes needed to compute the frame pointer from the current
2607 stack pointer. */
2609 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
2610 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
2612 long
2613 compute_frame_size (int size)
2615 /* Add space for the incoming static chain value. */
2616 if (cfun->static_chain_decl != NULL)
2617 size += (1 * UNITS_PER_WORD);
2619 xtensa_current_frame_size =
2620 XTENSA_STACK_ALIGN (size
2621 + crtl->outgoing_args_size
2622 + (WINDOW_SIZE * UNITS_PER_WORD));
2623 return xtensa_current_frame_size;
2627 bool
2628 xtensa_frame_pointer_required (void)
2630 /* The code to expand builtin_frame_addr and builtin_return_addr
2631 currently uses the hard_frame_pointer instead of frame_pointer.
2632 This seems wrong but maybe it's necessary for other architectures.
2633 This function is derived from the i386 code. */
2635 if (cfun->machine->accesses_prev_frame)
2636 return true;
2638 return false;
2642 /* minimum frame = reg save area (4 words) plus static chain (1 word)
2643 and the total number of words must be a multiple of 128 bits. */
2644 #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD)
2646 void
2647 xtensa_expand_prologue (void)
2649 HOST_WIDE_INT total_size;
2650 rtx size_rtx;
2651 rtx insn, note_rtx;
2653 total_size = compute_frame_size (get_frame_size ());
2654 size_rtx = GEN_INT (total_size);
2656 if (total_size < (1 << (12+3)))
2657 insn = emit_insn (gen_entry (size_rtx));
2658 else
2660 /* Use a8 as a temporary since a0-a7 may be live. */
2661 rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
2662 emit_insn (gen_entry (GEN_INT (MIN_FRAME_SIZE)));
2663 emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
2664 emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
2665 insn = emit_insn (gen_movsi (stack_pointer_rtx, tmp_reg));
2668 if (frame_pointer_needed)
2670 if (cfun->machine->set_frame_ptr_insn)
2672 rtx first;
2674 push_topmost_sequence ();
2675 first = get_insns ();
2676 pop_topmost_sequence ();
2678 /* For all instructions prior to set_frame_ptr_insn, replace
2679 hard_frame_pointer references with stack_pointer. */
2680 for (insn = first;
2681 insn != cfun->machine->set_frame_ptr_insn;
2682 insn = NEXT_INSN (insn))
2684 if (INSN_P (insn))
2686 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
2687 hard_frame_pointer_rtx,
2688 stack_pointer_rtx);
2689 df_insn_rescan (insn);
2693 else
2694 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
2695 stack_pointer_rtx));
2698 /* Create a note to describe the CFA. Because this is only used to set
2699 DW_AT_frame_base for debug info, don't bother tracking changes through
2700 each instruction in the prologue. It just takes up space. */
2701 note_rtx = gen_rtx_SET (VOIDmode, (frame_pointer_needed
2702 ? hard_frame_pointer_rtx
2703 : stack_pointer_rtx),
2704 plus_constant (Pmode, stack_pointer_rtx,
2705 -total_size));
2706 RTX_FRAME_RELATED_P (insn) = 1;
2707 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2711 /* Clear variables at function end. */
2713 void
2714 xtensa_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
2715 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
2717 xtensa_current_frame_size = 0;
2722 xtensa_return_addr (int count, rtx frame)
2724 rtx result, retaddr, curaddr, label;
2726 if (count == -1)
2727 retaddr = gen_rtx_REG (Pmode, A0_REG);
2728 else
2730 rtx addr = plus_constant (Pmode, frame, -4 * UNITS_PER_WORD);
2731 addr = memory_address (Pmode, addr);
2732 retaddr = gen_reg_rtx (Pmode);
2733 emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
2736 /* The 2 most-significant bits of the return address on Xtensa hold
2737 the register window size. To get the real return address, these
2738 bits must be replaced with the high bits from some address in the
2739 code. */
2741 /* Get the 2 high bits of a local label in the code. */
2742 curaddr = gen_reg_rtx (Pmode);
2743 label = gen_label_rtx ();
2744 emit_label (label);
2745 LABEL_PRESERVE_P (label) = 1;
2746 emit_move_insn (curaddr, gen_rtx_LABEL_REF (Pmode, label));
2747 emit_insn (gen_lshrsi3 (curaddr, curaddr, GEN_INT (30)));
2748 emit_insn (gen_ashlsi3 (curaddr, curaddr, GEN_INT (30)));
2750 /* Clear the 2 high bits of the return address. */
2751 result = gen_reg_rtx (Pmode);
2752 emit_insn (gen_ashlsi3 (result, retaddr, GEN_INT (2)));
2753 emit_insn (gen_lshrsi3 (result, result, GEN_INT (2)));
2755 /* Combine them to get the result. */
2756 emit_insn (gen_iorsi3 (result, result, curaddr));
2757 return result;
2760 /* Disable the use of word-sized or smaller complex modes for structures,
2761 and for function arguments in particular, where they cause problems with
2762 register a7. The xtensa_copy_incoming_a7 function assumes that there is
2763 a single reference to an argument in a7, but with small complex modes the
2764 real and imaginary components may be extracted separately, leading to two
2765 uses of the register, only one of which would be replaced. */
2767 static bool
2768 xtensa_member_type_forces_blk (const_tree, enum machine_mode mode)
2770 return mode == CQImode || mode == CHImode;
2773 /* Create the va_list data type.
2775 This structure is set up by __builtin_saveregs. The __va_reg field
2776 points to a stack-allocated region holding the contents of the
2777 incoming argument registers. The __va_ndx field is an index
2778 initialized to the position of the first unnamed (variable)
2779 argument. This same index is also used to address the arguments
2780 passed in memory. Thus, the __va_stk field is initialized to point
2781 to the position of the first argument in memory offset to account
2782 for the arguments passed in registers and to account for the size
2783 of the argument registers not being 16-byte aligned. E.G., there
2784 are 6 argument registers of 4 bytes each, but we want the __va_ndx
2785 for the first stack argument to have the maximal alignment of 16
2786 bytes, so we offset the __va_stk address by 32 bytes so that
2787 __va_stk[32] references the first argument on the stack. */
2789 static tree
2790 xtensa_build_builtin_va_list (void)
2792 tree f_stk, f_reg, f_ndx, record, type_decl;
2794 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2795 type_decl = build_decl (BUILTINS_LOCATION,
2796 TYPE_DECL, get_identifier ("__va_list_tag"), record);
2798 f_stk = build_decl (BUILTINS_LOCATION,
2799 FIELD_DECL, get_identifier ("__va_stk"),
2800 ptr_type_node);
2801 f_reg = build_decl (BUILTINS_LOCATION,
2802 FIELD_DECL, get_identifier ("__va_reg"),
2803 ptr_type_node);
2804 f_ndx = build_decl (BUILTINS_LOCATION,
2805 FIELD_DECL, get_identifier ("__va_ndx"),
2806 integer_type_node);
2808 DECL_FIELD_CONTEXT (f_stk) = record;
2809 DECL_FIELD_CONTEXT (f_reg) = record;
2810 DECL_FIELD_CONTEXT (f_ndx) = record;
2812 TYPE_STUB_DECL (record) = type_decl;
2813 TYPE_NAME (record) = type_decl;
2814 TYPE_FIELDS (record) = f_stk;
2815 DECL_CHAIN (f_stk) = f_reg;
2816 DECL_CHAIN (f_reg) = f_ndx;
2818 layout_type (record);
2819 return record;
2823 /* Save the incoming argument registers on the stack. Returns the
2824 address of the saved registers. */
2826 static rtx
2827 xtensa_builtin_saveregs (void)
2829 rtx gp_regs;
2830 int arg_words = crtl->args.info.arg_words;
2831 int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
2833 if (gp_left <= 0)
2834 return const0_rtx;
2836 /* Allocate the general-purpose register space. */
2837 gp_regs = assign_stack_local
2838 (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
2839 set_mem_alias_set (gp_regs, get_varargs_alias_set ());
2841 /* Now store the incoming registers. */
2842 cfun->machine->need_a7_copy = true;
2843 cfun->machine->vararg_a7 = true;
2844 move_block_from_reg (GP_ARG_FIRST + arg_words,
2845 adjust_address (gp_regs, BLKmode,
2846 arg_words * UNITS_PER_WORD),
2847 gp_left);
2848 gcc_assert (cfun->machine->vararg_a7_copy != 0);
2849 emit_insn_before (cfun->machine->vararg_a7_copy, get_insns ());
2851 return XEXP (gp_regs, 0);
2855 /* Implement `va_start' for varargs and stdarg. We look at the
2856 current function to fill in an initial va_list. */
2858 static void
2859 xtensa_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
2861 tree f_stk, stk;
2862 tree f_reg, reg;
2863 tree f_ndx, ndx;
2864 tree t, u;
2865 int arg_words;
2867 arg_words = crtl->args.info.arg_words;
2869 f_stk = TYPE_FIELDS (va_list_type_node);
2870 f_reg = DECL_CHAIN (f_stk);
2871 f_ndx = DECL_CHAIN (f_reg);
2873 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk, NULL_TREE);
2874 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
2875 f_reg, NULL_TREE);
2876 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
2877 f_ndx, NULL_TREE);
2879 /* Call __builtin_saveregs; save the result in __va_reg */
2880 u = make_tree (sizetype, expand_builtin_saveregs ());
2881 u = fold_convert (ptr_type_node, u);
2882 t = build2 (MODIFY_EXPR, ptr_type_node, reg, u);
2883 TREE_SIDE_EFFECTS (t) = 1;
2884 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2886 /* Set the __va_stk member to ($arg_ptr - 32). */
2887 u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
2888 u = fold_build_pointer_plus_hwi (u, -32);
2889 t = build2 (MODIFY_EXPR, ptr_type_node, stk, u);
2890 TREE_SIDE_EFFECTS (t) = 1;
2891 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2893 /* Set the __va_ndx member. If the first variable argument is on
2894 the stack, adjust __va_ndx by 2 words to account for the extra
2895 alignment offset for __va_stk. */
2896 if (arg_words >= MAX_ARGS_IN_REGISTERS)
2897 arg_words += 2;
2898 t = build2 (MODIFY_EXPR, integer_type_node, ndx,
2899 build_int_cst (integer_type_node, arg_words * UNITS_PER_WORD));
2900 TREE_SIDE_EFFECTS (t) = 1;
2901 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2905 /* Implement `va_arg'. */
2907 static tree
2908 xtensa_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
2909 gimple_seq *post_p ATTRIBUTE_UNUSED)
2911 tree f_stk, stk;
2912 tree f_reg, reg;
2913 tree f_ndx, ndx;
2914 tree type_size, array, orig_ndx, addr, size, va_size, t;
2915 tree lab_false, lab_over, lab_false2;
2916 bool indirect;
2918 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
2919 if (indirect)
2920 type = build_pointer_type (type);
2922 /* Handle complex values as separate real and imaginary parts. */
2923 if (TREE_CODE (type) == COMPLEX_TYPE)
2925 tree real_part, imag_part;
2927 real_part = xtensa_gimplify_va_arg_expr (valist, TREE_TYPE (type),
2928 pre_p, NULL);
2929 real_part = get_initialized_tmp_var (real_part, pre_p, NULL);
2931 imag_part = xtensa_gimplify_va_arg_expr (unshare_expr (valist),
2932 TREE_TYPE (type),
2933 pre_p, NULL);
2934 imag_part = get_initialized_tmp_var (imag_part, pre_p, NULL);
2936 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
2939 f_stk = TYPE_FIELDS (va_list_type_node);
2940 f_reg = DECL_CHAIN (f_stk);
2941 f_ndx = DECL_CHAIN (f_reg);
2943 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist,
2944 f_stk, NULL_TREE);
2945 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
2946 f_reg, NULL_TREE);
2947 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
2948 f_ndx, NULL_TREE);
2950 type_size = size_in_bytes (type);
2951 va_size = round_up (type_size, UNITS_PER_WORD);
2952 gimplify_expr (&va_size, pre_p, NULL, is_gimple_val, fb_rvalue);
2955 /* First align __va_ndx if necessary for this arg:
2957 orig_ndx = (AP).__va_ndx;
2958 if (__alignof__ (TYPE) > 4 )
2959 orig_ndx = ((orig_ndx + __alignof__ (TYPE) - 1)
2960 & -__alignof__ (TYPE)); */
2962 orig_ndx = get_initialized_tmp_var (ndx, pre_p, NULL);
2964 if (TYPE_ALIGN (type) > BITS_PER_WORD)
2966 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_UNIT;
2968 t = build2 (PLUS_EXPR, integer_type_node, unshare_expr (orig_ndx),
2969 build_int_cst (integer_type_node, align - 1));
2970 t = build2 (BIT_AND_EXPR, integer_type_node, t,
2971 build_int_cst (integer_type_node, -align));
2972 gimplify_assign (unshare_expr (orig_ndx), t, pre_p);
2976 /* Increment __va_ndx to point past the argument:
2978 (AP).__va_ndx = orig_ndx + __va_size (TYPE); */
2980 t = fold_convert (integer_type_node, va_size);
2981 t = build2 (PLUS_EXPR, integer_type_node, orig_ndx, t);
2982 gimplify_assign (unshare_expr (ndx), t, pre_p);
2985 /* Check if the argument is in registers:
2987 if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
2988 && !must_pass_in_stack (type))
2989 __array = (AP).__va_reg; */
2991 array = create_tmp_var (ptr_type_node, NULL);
2993 lab_over = NULL;
2994 if (!targetm.calls.must_pass_in_stack (TYPE_MODE (type), type))
2996 lab_false = create_artificial_label (UNKNOWN_LOCATION);
2997 lab_over = create_artificial_label (UNKNOWN_LOCATION);
2999 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (ndx),
3000 build_int_cst (integer_type_node,
3001 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
3002 t = build3 (COND_EXPR, void_type_node, t,
3003 build1 (GOTO_EXPR, void_type_node, lab_false),
3004 NULL_TREE);
3005 gimplify_and_add (t, pre_p);
3007 gimplify_assign (unshare_expr (array), reg, pre_p);
3009 t = build1 (GOTO_EXPR, void_type_node, lab_over);
3010 gimplify_and_add (t, pre_p);
3012 t = build1 (LABEL_EXPR, void_type_node, lab_false);
3013 gimplify_and_add (t, pre_p);
3017 /* ...otherwise, the argument is on the stack (never split between
3018 registers and the stack -- change __va_ndx if necessary):
3020 else
3022 if (orig_ndx <= __MAX_ARGS_IN_REGISTERS * 4)
3023 (AP).__va_ndx = 32 + __va_size (TYPE);
3024 __array = (AP).__va_stk;
3025 } */
3027 lab_false2 = create_artificial_label (UNKNOWN_LOCATION);
3029 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (orig_ndx),
3030 build_int_cst (integer_type_node,
3031 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
3032 t = build3 (COND_EXPR, void_type_node, t,
3033 build1 (GOTO_EXPR, void_type_node, lab_false2),
3034 NULL_TREE);
3035 gimplify_and_add (t, pre_p);
3037 t = size_binop (PLUS_EXPR, unshare_expr (va_size), size_int (32));
3038 t = fold_convert (integer_type_node, t);
3039 gimplify_assign (unshare_expr (ndx), t, pre_p);
3041 t = build1 (LABEL_EXPR, void_type_node, lab_false2);
3042 gimplify_and_add (t, pre_p);
3044 gimplify_assign (array, stk, pre_p);
3046 if (lab_over)
3048 t = build1 (LABEL_EXPR, void_type_node, lab_over);
3049 gimplify_and_add (t, pre_p);
3053 /* Given the base array pointer (__array) and index to the subsequent
3054 argument (__va_ndx), find the address:
3056 __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
3057 ? sizeof (TYPE)
3058 : __va_size (TYPE))
3060 The results are endian-dependent because values smaller than one word
3061 are aligned differently. */
3064 if (BYTES_BIG_ENDIAN && TREE_CODE (type_size) == INTEGER_CST)
3066 t = fold_build2 (GE_EXPR, boolean_type_node, unshare_expr (type_size),
3067 size_int (PARM_BOUNDARY / BITS_PER_UNIT));
3068 t = fold_build3 (COND_EXPR, sizetype, t, unshare_expr (va_size),
3069 unshare_expr (type_size));
3070 size = t;
3072 else
3073 size = unshare_expr (va_size);
3075 t = fold_convert (sizetype, unshare_expr (ndx));
3076 t = build2 (MINUS_EXPR, sizetype, t, size);
3077 addr = fold_build_pointer_plus (unshare_expr (array), t);
3079 addr = fold_convert (build_pointer_type (type), addr);
3080 if (indirect)
3081 addr = build_va_arg_indirect_ref (addr);
3082 return build_va_arg_indirect_ref (addr);
3086 /* Builtins. */
3088 enum xtensa_builtin
3090 XTENSA_BUILTIN_UMULSIDI3,
3091 XTENSA_BUILTIN_max
3095 static void
3096 xtensa_init_builtins (void)
3098 tree ftype, decl;
3100 ftype = build_function_type_list (unsigned_intDI_type_node,
3101 unsigned_intSI_type_node,
3102 unsigned_intSI_type_node, NULL_TREE);
3104 decl = add_builtin_function ("__builtin_umulsidi3", ftype,
3105 XTENSA_BUILTIN_UMULSIDI3, BUILT_IN_MD,
3106 "__umulsidi3", NULL_TREE);
3107 TREE_NOTHROW (decl) = 1;
3108 TREE_READONLY (decl) = 1;
3112 static tree
3113 xtensa_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED, tree *args,
3114 bool ignore ATTRIBUTE_UNUSED)
3116 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3117 tree arg0, arg1;
3119 switch (fcode)
3121 case XTENSA_BUILTIN_UMULSIDI3:
3122 arg0 = args[0];
3123 arg1 = args[1];
3124 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3125 || TARGET_MUL32_HIGH)
3126 return fold_build2 (MULT_EXPR, unsigned_intDI_type_node,
3127 fold_convert (unsigned_intDI_type_node, arg0),
3128 fold_convert (unsigned_intDI_type_node, arg1));
3129 break;
3131 default:
3132 internal_error ("bad builtin code");
3133 break;
3136 return NULL;
3140 static rtx
3141 xtensa_expand_builtin (tree exp, rtx target,
3142 rtx subtarget ATTRIBUTE_UNUSED,
3143 enum machine_mode mode ATTRIBUTE_UNUSED,
3144 int ignore)
3146 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
3147 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3149 switch (fcode)
3151 case XTENSA_BUILTIN_UMULSIDI3:
3152 /* The umulsidi3 builtin is just a mechanism to avoid calling the real
3153 __umulsidi3 function when the Xtensa configuration can directly
3154 implement it. If not, just call the function. */
3155 return expand_call (exp, target, ignore);
3157 default:
3158 internal_error ("bad builtin code");
3160 return NULL_RTX;
3163 /* Worker function for TARGET_PREFERRED_RELOAD_CLASS. */
3165 static reg_class_t
3166 xtensa_preferred_reload_class (rtx x, reg_class_t rclass)
3168 if (CONSTANT_P (x) && CONST_DOUBLE_P (x))
3169 return NO_REGS;
3171 /* Don't use the stack pointer or hard frame pointer for reloads!
3172 The hard frame pointer would normally be OK except that it may
3173 briefly hold an incoming argument in the prologue, and reload
3174 won't know that it is live because the hard frame pointer is
3175 treated specially. */
3177 if (rclass == AR_REGS || rclass == GR_REGS)
3178 return RL_REGS;
3180 return rclass;
3183 /* Worker function for TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
3185 static reg_class_t
3186 xtensa_preferred_output_reload_class (rtx x ATTRIBUTE_UNUSED,
3187 reg_class_t rclass)
3189 /* Don't use the stack pointer or hard frame pointer for reloads!
3190 The hard frame pointer would normally be OK except that it may
3191 briefly hold an incoming argument in the prologue, and reload
3192 won't know that it is live because the hard frame pointer is
3193 treated specially. */
3195 if (rclass == AR_REGS || rclass == GR_REGS)
3196 return RL_REGS;
3198 return rclass;
3201 /* Worker function for TARGET_SECONDARY_RELOAD. */
3203 static reg_class_t
3204 xtensa_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
3205 enum machine_mode mode, secondary_reload_info *sri)
3207 int regno;
3209 if (in_p && constantpool_mem_p (x))
3211 if (rclass == FP_REGS)
3212 return RL_REGS;
3214 if (mode == QImode)
3215 sri->icode = CODE_FOR_reloadqi_literal;
3216 else if (mode == HImode)
3217 sri->icode = CODE_FOR_reloadhi_literal;
3220 regno = xt_true_regnum (x);
3221 if (ACC_REG_P (regno))
3222 return ((rclass == GR_REGS || rclass == RL_REGS) ? NO_REGS : RL_REGS);
3223 if (rclass == ACC_REG)
3224 return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
3226 return NO_REGS;
3230 void
3231 order_regs_for_local_alloc (void)
3233 if (!leaf_function_p ())
3235 memcpy (reg_alloc_order, reg_nonleaf_alloc_order,
3236 FIRST_PSEUDO_REGISTER * sizeof (int));
3238 else
3240 int i, num_arg_regs;
3241 int nxt = 0;
3243 /* Use the AR registers in increasing order (skipping a0 and a1)
3244 but save the incoming argument registers for a last resort. */
3245 num_arg_regs = crtl->args.info.arg_words;
3246 if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
3247 num_arg_regs = MAX_ARGS_IN_REGISTERS;
3248 for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
3249 reg_alloc_order[nxt++] = i + num_arg_regs;
3250 for (i = 0; i < num_arg_regs; i++)
3251 reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
3253 /* List the coprocessor registers in order. */
3254 for (i = 0; i < BR_REG_NUM; i++)
3255 reg_alloc_order[nxt++] = BR_REG_FIRST + i;
3257 /* List the FP registers in order for now. */
3258 for (i = 0; i < 16; i++)
3259 reg_alloc_order[nxt++] = FP_REG_FIRST + i;
3261 /* GCC requires that we list *all* the registers.... */
3262 reg_alloc_order[nxt++] = 0; /* a0 = return address */
3263 reg_alloc_order[nxt++] = 1; /* a1 = stack pointer */
3264 reg_alloc_order[nxt++] = 16; /* pseudo frame pointer */
3265 reg_alloc_order[nxt++] = 17; /* pseudo arg pointer */
3267 reg_alloc_order[nxt++] = ACC_REG_FIRST; /* MAC16 accumulator */
3272 /* Some Xtensa targets support multiple bss sections. If the section
3273 name ends with ".bss", add SECTION_BSS to the flags. */
3275 static unsigned int
3276 xtensa_multibss_section_type_flags (tree decl, const char *name, int reloc)
3278 unsigned int flags = default_section_type_flags (decl, name, reloc);
3279 const char *suffix;
3281 suffix = strrchr (name, '.');
3282 if (suffix && strcmp (suffix, ".bss") == 0)
3284 if (!decl || (TREE_CODE (decl) == VAR_DECL
3285 && DECL_INITIAL (decl) == NULL_TREE))
3286 flags |= SECTION_BSS; /* @nobits */
3287 else
3288 warning (0, "only uninitialized variables can be placed in a "
3289 ".bss section");
3292 return flags;
3296 /* The literal pool stays with the function. */
3298 static section *
3299 xtensa_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
3300 rtx x ATTRIBUTE_UNUSED,
3301 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
3303 return function_section (current_function_decl);
3306 /* Worker function for TARGET_REGISTER_MOVE_COST. */
3308 static int
3309 xtensa_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
3310 reg_class_t from, reg_class_t to)
3312 if (from == to && from != BR_REGS && to != BR_REGS)
3313 return 2;
3314 else if (reg_class_subset_p (from, AR_REGS)
3315 && reg_class_subset_p (to, AR_REGS))
3316 return 2;
3317 else if (reg_class_subset_p (from, AR_REGS) && to == ACC_REG)
3318 return 3;
3319 else if (from == ACC_REG && reg_class_subset_p (to, AR_REGS))
3320 return 3;
3321 else
3322 return 10;
3325 /* Worker function for TARGET_MEMORY_MOVE_COST. */
3327 static int
3328 xtensa_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
3329 reg_class_t rclass ATTRIBUTE_UNUSED,
3330 bool in ATTRIBUTE_UNUSED)
3332 return 4;
3335 /* Compute a (partial) cost for rtx X. Return true if the complete
3336 cost has been computed, and false if subexpressions should be
3337 scanned. In either case, *TOTAL contains the cost result. */
3339 static bool
3340 xtensa_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3341 int *total, bool speed ATTRIBUTE_UNUSED)
3343 switch (code)
3345 case CONST_INT:
3346 switch (outer_code)
3348 case SET:
3349 if (xtensa_simm12b (INTVAL (x)))
3351 *total = 4;
3352 return true;
3354 break;
3355 case PLUS:
3356 if (xtensa_simm8 (INTVAL (x))
3357 || xtensa_simm8x256 (INTVAL (x)))
3359 *total = 0;
3360 return true;
3362 break;
3363 case AND:
3364 if (xtensa_mask_immediate (INTVAL (x)))
3366 *total = 0;
3367 return true;
3369 break;
3370 case COMPARE:
3371 if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
3373 *total = 0;
3374 return true;
3376 break;
3377 case ASHIFT:
3378 case ASHIFTRT:
3379 case LSHIFTRT:
3380 case ROTATE:
3381 case ROTATERT:
3382 /* No way to tell if X is the 2nd operand so be conservative. */
3383 default: break;
3385 if (xtensa_simm12b (INTVAL (x)))
3386 *total = 5;
3387 else if (TARGET_CONST16)
3388 *total = COSTS_N_INSNS (2);
3389 else
3390 *total = 6;
3391 return true;
3393 case CONST:
3394 case LABEL_REF:
3395 case SYMBOL_REF:
3396 if (TARGET_CONST16)
3397 *total = COSTS_N_INSNS (2);
3398 else
3399 *total = 5;
3400 return true;
3402 case CONST_DOUBLE:
3403 if (TARGET_CONST16)
3404 *total = COSTS_N_INSNS (4);
3405 else
3406 *total = 7;
3407 return true;
3409 case MEM:
3411 int num_words =
3412 (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) ? 2 : 1;
3414 if (memory_address_p (GET_MODE (x), XEXP ((x), 0)))
3415 *total = COSTS_N_INSNS (num_words);
3416 else
3417 *total = COSTS_N_INSNS (2*num_words);
3418 return true;
3421 case FFS:
3422 case CTZ:
3423 *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
3424 return true;
3426 case CLZ:
3427 *total = COSTS_N_INSNS (TARGET_NSA ? 1 : 50);
3428 return true;
3430 case NOT:
3431 *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 3 : 2);
3432 return true;
3434 case AND:
3435 case IOR:
3436 case XOR:
3437 if (GET_MODE (x) == DImode)
3438 *total = COSTS_N_INSNS (2);
3439 else
3440 *total = COSTS_N_INSNS (1);
3441 return true;
3443 case ASHIFT:
3444 case ASHIFTRT:
3445 case LSHIFTRT:
3446 if (GET_MODE (x) == DImode)
3447 *total = COSTS_N_INSNS (50);
3448 else
3449 *total = COSTS_N_INSNS (1);
3450 return true;
3452 case ABS:
3454 enum machine_mode xmode = GET_MODE (x);
3455 if (xmode == SFmode)
3456 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3457 else if (xmode == DFmode)
3458 *total = COSTS_N_INSNS (50);
3459 else
3460 *total = COSTS_N_INSNS (4);
3461 return true;
3464 case PLUS:
3465 case MINUS:
3467 enum machine_mode xmode = GET_MODE (x);
3468 if (xmode == SFmode)
3469 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3470 else if (xmode == DFmode || xmode == DImode)
3471 *total = COSTS_N_INSNS (50);
3472 else
3473 *total = COSTS_N_INSNS (1);
3474 return true;
3477 case NEG:
3478 *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 4 : 2);
3479 return true;
3481 case MULT:
3483 enum machine_mode xmode = GET_MODE (x);
3484 if (xmode == SFmode)
3485 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
3486 else if (xmode == DFmode)
3487 *total = COSTS_N_INSNS (50);
3488 else if (xmode == DImode)
3489 *total = COSTS_N_INSNS (TARGET_MUL32_HIGH ? 10 : 50);
3490 else if (TARGET_MUL32)
3491 *total = COSTS_N_INSNS (4);
3492 else if (TARGET_MAC16)
3493 *total = COSTS_N_INSNS (16);
3494 else if (TARGET_MUL16)
3495 *total = COSTS_N_INSNS (12);
3496 else
3497 *total = COSTS_N_INSNS (50);
3498 return true;
3501 case DIV:
3502 case MOD:
3504 enum machine_mode xmode = GET_MODE (x);
3505 if (xmode == SFmode)
3507 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
3508 return true;
3510 else if (xmode == DFmode)
3512 *total = COSTS_N_INSNS (50);
3513 return true;
3516 /* Fall through. */
3518 case UDIV:
3519 case UMOD:
3521 enum machine_mode xmode = GET_MODE (x);
3522 if (xmode == DImode)
3523 *total = COSTS_N_INSNS (50);
3524 else if (TARGET_DIV32)
3525 *total = COSTS_N_INSNS (32);
3526 else
3527 *total = COSTS_N_INSNS (50);
3528 return true;
3531 case SQRT:
3532 if (GET_MODE (x) == SFmode)
3533 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
3534 else
3535 *total = COSTS_N_INSNS (50);
3536 return true;
3538 case SMIN:
3539 case UMIN:
3540 case SMAX:
3541 case UMAX:
3542 *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
3543 return true;
3545 case SIGN_EXTRACT:
3546 case SIGN_EXTEND:
3547 *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
3548 return true;
3550 case ZERO_EXTRACT:
3551 case ZERO_EXTEND:
3552 *total = COSTS_N_INSNS (1);
3553 return true;
3555 default:
3556 return false;
3560 /* Worker function for TARGET_RETURN_IN_MEMORY. */
3562 static bool
3563 xtensa_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3565 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
3566 > 4 * UNITS_PER_WORD);
3569 /* Worker function for TARGET_FUNCTION_VALUE. */
3572 xtensa_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
3573 bool outgoing)
3575 return gen_rtx_REG ((INTEGRAL_TYPE_P (valtype)
3576 && TYPE_PRECISION (valtype) < BITS_PER_WORD)
3577 ? SImode : TYPE_MODE (valtype),
3578 outgoing ? GP_OUTGOING_RETURN : GP_RETURN);
3581 /* Worker function for TARGET_LIBCALL_VALUE. */
3583 static rtx
3584 xtensa_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
3586 return gen_rtx_REG ((GET_MODE_CLASS (mode) == MODE_INT
3587 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3588 ? SImode : mode, GP_RETURN);
3591 /* Worker function TARGET_FUNCTION_VALUE_REGNO_P. */
3593 static bool
3594 xtensa_function_value_regno_p (const unsigned int regno)
3596 return (regno == GP_RETURN);
3599 /* The static chain is passed in memory. Provide rtx giving 'mem'
3600 expressions that denote where they are stored. */
3602 static rtx
3603 xtensa_static_chain (const_tree ARG_UNUSED (fndecl), bool incoming_p)
3605 rtx base = incoming_p ? arg_pointer_rtx : stack_pointer_rtx;
3606 return gen_frame_mem (Pmode, plus_constant (Pmode, base,
3607 -5 * UNITS_PER_WORD));
3611 /* TRAMPOLINE_TEMPLATE: For Xtensa, the trampoline must perform an ENTRY
3612 instruction with a minimal stack frame in order to get some free
3613 registers. Once the actual call target is known, the proper stack frame
3614 size is extracted from the ENTRY instruction at the target and the
3615 current frame is adjusted to match. The trampoline then transfers
3616 control to the instruction following the ENTRY at the target. Note:
3617 this assumes that the target begins with an ENTRY instruction. */
3619 static void
3620 xtensa_asm_trampoline_template (FILE *stream)
3622 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3624 fprintf (stream, "\t.begin no-transform\n");
3625 fprintf (stream, "\tentry\tsp, %d\n", MIN_FRAME_SIZE);
3627 if (use_call0)
3629 /* Save the return address. */
3630 fprintf (stream, "\tmov\ta10, a0\n");
3632 /* Use a CALL0 instruction to skip past the constants and in the
3633 process get the PC into A0. This allows PC-relative access to
3634 the constants without relying on L32R. */
3635 fprintf (stream, "\tcall0\t.Lskipconsts\n");
3637 else
3638 fprintf (stream, "\tj\t.Lskipconsts\n");
3640 fprintf (stream, "\t.align\t4\n");
3641 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
3642 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
3643 fprintf (stream, ".Lskipconsts:\n");
3645 /* Load the static chain and function address from the trampoline. */
3646 if (use_call0)
3648 fprintf (stream, "\taddi\ta0, a0, 3\n");
3649 fprintf (stream, "\tl32i\ta9, a0, 0\n");
3650 fprintf (stream, "\tl32i\ta8, a0, 4\n");
3652 else
3654 fprintf (stream, "\tl32r\ta9, .Lchainval\n");
3655 fprintf (stream, "\tl32r\ta8, .Lfnaddr\n");
3658 /* Store the static chain. */
3659 fprintf (stream, "\ts32i\ta9, sp, %d\n", MIN_FRAME_SIZE - 20);
3661 /* Set the proper stack pointer value. */
3662 fprintf (stream, "\tl32i\ta9, a8, 0\n");
3663 fprintf (stream, "\textui\ta9, a9, %d, 12\n",
3664 TARGET_BIG_ENDIAN ? 8 : 12);
3665 fprintf (stream, "\tslli\ta9, a9, 3\n");
3666 fprintf (stream, "\taddi\ta9, a9, %d\n", -MIN_FRAME_SIZE);
3667 fprintf (stream, "\tsub\ta9, sp, a9\n");
3668 fprintf (stream, "\tmovsp\tsp, a9\n");
3670 if (use_call0)
3671 /* Restore the return address. */
3672 fprintf (stream, "\tmov\ta0, a10\n");
3674 /* Jump to the instruction following the ENTRY. */
3675 fprintf (stream, "\taddi\ta8, a8, 3\n");
3676 fprintf (stream, "\tjx\ta8\n");
3678 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
3679 if (use_call0)
3680 fprintf (stream, "\t.byte\t0\n");
3681 else
3682 fprintf (stream, "\tnop\n");
3684 fprintf (stream, "\t.end no-transform\n");
3687 static void
3688 xtensa_trampoline_init (rtx m_tramp, tree fndecl, rtx chain)
3690 rtx func = XEXP (DECL_RTL (fndecl), 0);
3691 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3692 int chain_off = use_call0 ? 12 : 8;
3693 int func_off = use_call0 ? 16 : 12;
3695 emit_block_move (m_tramp, assemble_trampoline_template (),
3696 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
3698 emit_move_insn (adjust_address (m_tramp, SImode, chain_off), chain);
3699 emit_move_insn (adjust_address (m_tramp, SImode, func_off), func);
3700 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_sync_caches"),
3701 LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
3704 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
3706 static bool
3707 xtensa_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
3709 return !xtensa_tls_referenced_p (x);
3712 #include "gt-xtensa.h"