* config/alpha/alpha.c (alpha_build_builtin_va_list): Pass location to
[official-gcc.git] / gcc / config / xtensa / xtensa.c
blob773223b2f35a571cfe01694cb4101d7582702016
1 /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "insn-attr.h"
35 #include "insn-codes.h"
36 #include "recog.h"
37 #include "output.h"
38 #include "tree.h"
39 #include "expr.h"
40 #include "flags.h"
41 #include "reload.h"
42 #include "tm_p.h"
43 #include "function.h"
44 #include "toplev.h"
45 #include "optabs.h"
46 #include "libfuncs.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "langhooks.h"
51 #include "gimple.h"
52 #include "df.h"
55 /* Enumeration for all of the relational tests, so that we can build
56 arrays indexed by the test type, and not worry about the order
57 of EQ, NE, etc. */
59 enum internal_test
61 ITEST_EQ,
62 ITEST_NE,
63 ITEST_GT,
64 ITEST_GE,
65 ITEST_LT,
66 ITEST_LE,
67 ITEST_GTU,
68 ITEST_GEU,
69 ITEST_LTU,
70 ITEST_LEU,
71 ITEST_MAX
74 /* Array giving truth value on whether or not a given hard register
75 can support a given mode. */
76 char xtensa_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
78 /* Current frame size calculated by compute_frame_size. */
79 unsigned xtensa_current_frame_size;
81 /* Largest block move to handle in-line. */
82 #define LARGEST_MOVE_RATIO 15
84 /* Define the structure for the machine field in struct function. */
85 struct GTY(()) machine_function
87 int accesses_prev_frame;
88 bool need_a7_copy;
89 bool vararg_a7;
90 rtx vararg_a7_copy;
91 rtx set_frame_ptr_insn;
94 /* Vector, indexed by hard register number, which contains 1 for a
95 register that is allowable in a candidate for leaf function
96 treatment. */
98 const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
101 1, 1, 1,
102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
106 /* Map hard register number to register class */
107 const enum reg_class xtensa_regno_to_class[FIRST_PSEUDO_REGISTER] =
109 RL_REGS, SP_REG, RL_REGS, RL_REGS,
110 RL_REGS, RL_REGS, RL_REGS, GR_REGS,
111 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
112 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
113 AR_REGS, AR_REGS, BR_REGS,
114 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
115 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
116 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
117 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
118 ACC_REG,
121 static enum internal_test map_test_to_internal_test (enum rtx_code);
122 static rtx gen_int_relational (enum rtx_code, rtx, rtx, int *);
123 static rtx gen_float_relational (enum rtx_code, rtx, rtx);
124 static rtx gen_conditional_move (enum rtx_code, enum machine_mode, rtx, rtx);
125 static rtx fixup_subreg_mem (rtx);
126 static struct machine_function * xtensa_init_machine_status (void);
127 static rtx xtensa_legitimize_tls_address (rtx);
128 static rtx xtensa_legitimize_address (rtx, rtx, enum machine_mode);
129 static bool xtensa_return_in_msb (const_tree);
130 static void printx (FILE *, signed int);
131 static void xtensa_function_epilogue (FILE *, HOST_WIDE_INT);
132 static rtx xtensa_builtin_saveregs (void);
133 static bool xtensa_legitimate_address_p (enum machine_mode, rtx, bool);
134 static unsigned int xtensa_multibss_section_type_flags (tree, const char *,
135 int) ATTRIBUTE_UNUSED;
136 static section *xtensa_select_rtx_section (enum machine_mode, rtx,
137 unsigned HOST_WIDE_INT);
138 static bool xtensa_rtx_costs (rtx, int, int, int *, bool);
139 static tree xtensa_build_builtin_va_list (void);
140 static bool xtensa_return_in_memory (const_tree, const_tree);
141 static tree xtensa_gimplify_va_arg_expr (tree, tree, gimple_seq *,
142 gimple_seq *);
143 static rtx xtensa_function_value (const_tree, const_tree, bool);
144 static void xtensa_init_builtins (void);
145 static tree xtensa_fold_builtin (tree, tree, bool);
146 static rtx xtensa_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
147 static void xtensa_va_start (tree, rtx);
149 static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
150 REG_ALLOC_ORDER;
153 /* This macro generates the assembly code for function exit,
154 on machines that need it. If FUNCTION_EPILOGUE is not defined
155 then individual return instructions are generated for each
156 return statement. Args are same as for FUNCTION_PROLOGUE. */
158 #undef TARGET_ASM_FUNCTION_EPILOGUE
159 #define TARGET_ASM_FUNCTION_EPILOGUE xtensa_function_epilogue
161 /* These hooks specify assembly directives for creating certain kinds
162 of integer object. */
164 #undef TARGET_ASM_ALIGNED_SI_OP
165 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
167 #undef TARGET_ASM_SELECT_RTX_SECTION
168 #define TARGET_ASM_SELECT_RTX_SECTION xtensa_select_rtx_section
170 #undef TARGET_DEFAULT_TARGET_FLAGS
171 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_FUSED_MADD)
173 #undef TARGET_LEGITIMIZE_ADDRESS
174 #define TARGET_LEGITIMIZE_ADDRESS xtensa_legitimize_address
176 #undef TARGET_RTX_COSTS
177 #define TARGET_RTX_COSTS xtensa_rtx_costs
178 #undef TARGET_ADDRESS_COST
179 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
181 #undef TARGET_BUILD_BUILTIN_VA_LIST
182 #define TARGET_BUILD_BUILTIN_VA_LIST xtensa_build_builtin_va_list
184 #undef TARGET_EXPAND_BUILTIN_VA_START
185 #define TARGET_EXPAND_BUILTIN_VA_START xtensa_va_start
187 #undef TARGET_PROMOTE_FUNCTION_ARGS
188 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_const_tree_true
189 #undef TARGET_PROMOTE_FUNCTION_RETURN
190 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_const_tree_true
191 #undef TARGET_PROMOTE_PROTOTYPES
192 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
194 #undef TARGET_RETURN_IN_MEMORY
195 #define TARGET_RETURN_IN_MEMORY xtensa_return_in_memory
196 #undef TARGET_FUNCTION_VALUE
197 #define TARGET_FUNCTION_VALUE xtensa_function_value
198 #undef TARGET_SPLIT_COMPLEX_ARG
199 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
200 #undef TARGET_MUST_PASS_IN_STACK
201 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
203 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
204 #define TARGET_EXPAND_BUILTIN_SAVEREGS xtensa_builtin_saveregs
205 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
206 #define TARGET_GIMPLIFY_VA_ARG_EXPR xtensa_gimplify_va_arg_expr
208 #undef TARGET_RETURN_IN_MSB
209 #define TARGET_RETURN_IN_MSB xtensa_return_in_msb
211 #undef TARGET_INIT_BUILTINS
212 #define TARGET_INIT_BUILTINS xtensa_init_builtins
213 #undef TARGET_FOLD_BUILTIN
214 #define TARGET_FOLD_BUILTIN xtensa_fold_builtin
215 #undef TARGET_EXPAND_BUILTIN
216 #define TARGET_EXPAND_BUILTIN xtensa_expand_builtin
218 #undef TARGET_SECONDARY_RELOAD
219 #define TARGET_SECONDARY_RELOAD xtensa_secondary_reload
221 #undef TARGET_HAVE_TLS
222 #define TARGET_HAVE_TLS (TARGET_THREADPTR && HAVE_AS_TLS)
224 #undef TARGET_CANNOT_FORCE_CONST_MEM
225 #define TARGET_CANNOT_FORCE_CONST_MEM xtensa_tls_referenced_p
227 #undef TARGET_LEGITIMATE_ADDRESS_P
228 #define TARGET_LEGITIMATE_ADDRESS_P xtensa_legitimate_address_p
230 struct gcc_target targetm = TARGET_INITIALIZER;
233 /* Functions to test Xtensa immediate operand validity. */
235 bool
236 xtensa_simm8 (HOST_WIDE_INT v)
238 return v >= -128 && v <= 127;
242 bool
243 xtensa_simm8x256 (HOST_WIDE_INT v)
245 return (v & 255) == 0 && (v >= -32768 && v <= 32512);
249 bool
250 xtensa_simm12b (HOST_WIDE_INT v)
252 return v >= -2048 && v <= 2047;
256 static bool
257 xtensa_uimm8 (HOST_WIDE_INT v)
259 return v >= 0 && v <= 255;
263 static bool
264 xtensa_uimm8x2 (HOST_WIDE_INT v)
266 return (v & 1) == 0 && (v >= 0 && v <= 510);
270 static bool
271 xtensa_uimm8x4 (HOST_WIDE_INT v)
273 return (v & 3) == 0 && (v >= 0 && v <= 1020);
277 static bool
278 xtensa_b4const (HOST_WIDE_INT v)
280 switch (v)
282 case -1:
283 case 1:
284 case 2:
285 case 3:
286 case 4:
287 case 5:
288 case 6:
289 case 7:
290 case 8:
291 case 10:
292 case 12:
293 case 16:
294 case 32:
295 case 64:
296 case 128:
297 case 256:
298 return true;
300 return false;
304 bool
305 xtensa_b4const_or_zero (HOST_WIDE_INT v)
307 if (v == 0)
308 return true;
309 return xtensa_b4const (v);
313 bool
314 xtensa_b4constu (HOST_WIDE_INT v)
316 switch (v)
318 case 32768:
319 case 65536:
320 case 2:
321 case 3:
322 case 4:
323 case 5:
324 case 6:
325 case 7:
326 case 8:
327 case 10:
328 case 12:
329 case 16:
330 case 32:
331 case 64:
332 case 128:
333 case 256:
334 return true;
336 return false;
340 bool
341 xtensa_mask_immediate (HOST_WIDE_INT v)
343 #define MAX_MASK_SIZE 16
344 int mask_size;
346 for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
348 if ((v & 1) == 0)
349 return false;
350 v = v >> 1;
351 if (v == 0)
352 return true;
355 return false;
359 /* This is just like the standard true_regnum() function except that it
360 works even when reg_renumber is not initialized. */
363 xt_true_regnum (rtx x)
365 if (GET_CODE (x) == REG)
367 if (reg_renumber
368 && REGNO (x) >= FIRST_PSEUDO_REGISTER
369 && reg_renumber[REGNO (x)] >= 0)
370 return reg_renumber[REGNO (x)];
371 return REGNO (x);
373 if (GET_CODE (x) == SUBREG)
375 int base = xt_true_regnum (SUBREG_REG (x));
376 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
377 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
378 GET_MODE (SUBREG_REG (x)),
379 SUBREG_BYTE (x), GET_MODE (x));
381 return -1;
386 xtensa_valid_move (enum machine_mode mode, rtx *operands)
388 /* Either the destination or source must be a register, and the
389 MAC16 accumulator doesn't count. */
391 if (register_operand (operands[0], mode))
393 int dst_regnum = xt_true_regnum (operands[0]);
395 /* The stack pointer can only be assigned with a MOVSP opcode. */
396 if (dst_regnum == STACK_POINTER_REGNUM)
397 return (mode == SImode
398 && register_operand (operands[1], mode)
399 && !ACC_REG_P (xt_true_regnum (operands[1])));
401 if (!ACC_REG_P (dst_regnum))
402 return true;
404 if (register_operand (operands[1], mode))
406 int src_regnum = xt_true_regnum (operands[1]);
407 if (!ACC_REG_P (src_regnum))
408 return true;
410 return FALSE;
415 smalloffset_mem_p (rtx op)
417 if (GET_CODE (op) == MEM)
419 rtx addr = XEXP (op, 0);
420 if (GET_CODE (addr) == REG)
421 return BASE_REG_P (addr, 0);
422 if (GET_CODE (addr) == PLUS)
424 rtx offset = XEXP (addr, 0);
425 HOST_WIDE_INT val;
426 if (GET_CODE (offset) != CONST_INT)
427 offset = XEXP (addr, 1);
428 if (GET_CODE (offset) != CONST_INT)
429 return FALSE;
431 val = INTVAL (offset);
432 return (val & 3) == 0 && (val >= 0 && val <= 60);
435 return FALSE;
440 constantpool_address_p (rtx addr)
442 rtx sym = addr;
444 if (GET_CODE (addr) == CONST)
446 rtx offset;
448 /* Only handle (PLUS (SYM, OFFSET)) form. */
449 addr = XEXP (addr, 0);
450 if (GET_CODE (addr) != PLUS)
451 return FALSE;
453 /* Make sure the address is word aligned. */
454 offset = XEXP (addr, 1);
455 if ((GET_CODE (offset) != CONST_INT)
456 || ((INTVAL (offset) & 3) != 0))
457 return FALSE;
459 sym = XEXP (addr, 0);
462 if ((GET_CODE (sym) == SYMBOL_REF)
463 && CONSTANT_POOL_ADDRESS_P (sym))
464 return TRUE;
465 return FALSE;
470 constantpool_mem_p (rtx op)
472 if (GET_CODE (op) == SUBREG)
473 op = SUBREG_REG (op);
474 if (GET_CODE (op) == MEM)
475 return constantpool_address_p (XEXP (op, 0));
476 return FALSE;
480 /* Return TRUE if X is a thread-local symbol. */
482 static bool
483 xtensa_tls_symbol_p (rtx x)
485 if (! TARGET_HAVE_TLS)
486 return false;
488 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
492 void
493 xtensa_extend_reg (rtx dst, rtx src)
495 rtx temp = gen_reg_rtx (SImode);
496 rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
498 /* Generate paradoxical subregs as needed so that the modes match. */
499 src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
500 dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
502 emit_insn (gen_ashlsi3 (temp, src, shift));
503 emit_insn (gen_ashrsi3 (dst, temp, shift));
507 bool
508 xtensa_mem_offset (unsigned v, enum machine_mode mode)
510 switch (mode)
512 case BLKmode:
513 /* Handle the worst case for block moves. See xtensa_expand_block_move
514 where we emit an optimized block move operation if the block can be
515 moved in < "move_ratio" pieces. The worst case is when the block is
516 aligned but has a size of (3 mod 4) (does this happen?) so that the
517 last piece requires a byte load/store. */
518 return (xtensa_uimm8 (v)
519 && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
521 case QImode:
522 return xtensa_uimm8 (v);
524 case HImode:
525 return xtensa_uimm8x2 (v);
527 case DFmode:
528 return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
530 default:
531 break;
534 return xtensa_uimm8x4 (v);
538 /* Make normal rtx_code into something we can index from an array. */
540 static enum internal_test
541 map_test_to_internal_test (enum rtx_code test_code)
543 enum internal_test test = ITEST_MAX;
545 switch (test_code)
547 default: break;
548 case EQ: test = ITEST_EQ; break;
549 case NE: test = ITEST_NE; break;
550 case GT: test = ITEST_GT; break;
551 case GE: test = ITEST_GE; break;
552 case LT: test = ITEST_LT; break;
553 case LE: test = ITEST_LE; break;
554 case GTU: test = ITEST_GTU; break;
555 case GEU: test = ITEST_GEU; break;
556 case LTU: test = ITEST_LTU; break;
557 case LEU: test = ITEST_LEU; break;
560 return test;
564 /* Generate the code to compare two integer values. The return value is
565 the comparison expression. */
567 static rtx
568 gen_int_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
569 rtx cmp0, /* first operand to compare */
570 rtx cmp1, /* second operand to compare */
571 int *p_invert /* whether branch needs to reverse test */)
573 struct cmp_info
575 enum rtx_code test_code; /* test code to use in insn */
576 bool (*const_range_p) (HOST_WIDE_INT); /* range check function */
577 int const_add; /* constant to add (convert LE -> LT) */
578 int reverse_regs; /* reverse registers in test */
579 int invert_const; /* != 0 if invert value if cmp1 is constant */
580 int invert_reg; /* != 0 if invert value if cmp1 is register */
581 int unsignedp; /* != 0 for unsigned comparisons. */
584 static struct cmp_info info[ (int)ITEST_MAX ] = {
586 { EQ, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* EQ */
587 { NE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* NE */
589 { LT, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* GT */
590 { GE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* GE */
591 { LT, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* LT */
592 { GE, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* LE */
594 { LTU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* GTU */
595 { GEU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* GEU */
596 { LTU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* LTU */
597 { GEU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* LEU */
600 enum internal_test test;
601 enum machine_mode mode;
602 struct cmp_info *p_info;
604 test = map_test_to_internal_test (test_code);
605 gcc_assert (test != ITEST_MAX);
607 p_info = &info[ (int)test ];
609 mode = GET_MODE (cmp0);
610 if (mode == VOIDmode)
611 mode = GET_MODE (cmp1);
613 /* Make sure we can handle any constants given to us. */
614 if (GET_CODE (cmp1) == CONST_INT)
616 HOST_WIDE_INT value = INTVAL (cmp1);
617 unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
619 /* if the immediate overflows or does not fit in the immediate field,
620 spill it to a register */
622 if ((p_info->unsignedp ?
623 (uvalue + p_info->const_add > uvalue) :
624 (value + p_info->const_add > value)) != (p_info->const_add > 0))
626 cmp1 = force_reg (mode, cmp1);
628 else if (!(p_info->const_range_p) (value + p_info->const_add))
630 cmp1 = force_reg (mode, cmp1);
633 else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
635 cmp1 = force_reg (mode, cmp1);
638 /* See if we need to invert the result. */
639 *p_invert = ((GET_CODE (cmp1) == CONST_INT)
640 ? p_info->invert_const
641 : p_info->invert_reg);
643 /* Comparison to constants, may involve adding 1 to change a LT into LE.
644 Comparison between two registers, may involve switching operands. */
645 if (GET_CODE (cmp1) == CONST_INT)
647 if (p_info->const_add != 0)
648 cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
651 else if (p_info->reverse_regs)
653 rtx temp = cmp0;
654 cmp0 = cmp1;
655 cmp1 = temp;
658 return gen_rtx_fmt_ee (p_info->test_code, VOIDmode, cmp0, cmp1);
662 /* Generate the code to compare two float values. The return value is
663 the comparison expression. */
665 static rtx
666 gen_float_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
667 rtx cmp0, /* first operand to compare */
668 rtx cmp1 /* second operand to compare */)
670 rtx (*gen_fn) (rtx, rtx, rtx);
671 rtx brtmp;
672 int reverse_regs, invert;
674 switch (test_code)
676 case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
677 case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
678 case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
679 case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
680 case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
681 case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
682 case UNEQ: reverse_regs = 0; invert = 0; gen_fn = gen_suneq_sf; break;
683 case LTGT: reverse_regs = 0; invert = 1; gen_fn = gen_suneq_sf; break;
684 case UNLE: reverse_regs = 0; invert = 0; gen_fn = gen_sunle_sf; break;
685 case UNGT: reverse_regs = 1; invert = 0; gen_fn = gen_sunlt_sf; break;
686 case UNLT: reverse_regs = 0; invert = 0; gen_fn = gen_sunlt_sf; break;
687 case UNGE: reverse_regs = 1; invert = 0; gen_fn = gen_sunle_sf; break;
688 case UNORDERED:
689 reverse_regs = 0; invert = 0; gen_fn = gen_sunordered_sf; break;
690 case ORDERED:
691 reverse_regs = 0; invert = 1; gen_fn = gen_sunordered_sf; break;
692 default:
693 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
694 reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
697 if (reverse_regs)
699 rtx temp = cmp0;
700 cmp0 = cmp1;
701 cmp1 = temp;
704 brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
705 emit_insn (gen_fn (brtmp, cmp0, cmp1));
707 return gen_rtx_fmt_ee (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
711 void
712 xtensa_expand_conditional_branch (rtx *operands, enum machine_mode mode)
714 enum rtx_code test_code = GET_CODE (operands[0]);
715 rtx cmp0 = operands[1];
716 rtx cmp1 = operands[2];
717 rtx cmp;
718 int invert;
719 rtx label1, label2;
721 switch (mode)
723 case DFmode:
724 default:
725 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
727 case SImode:
728 invert = FALSE;
729 cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
730 break;
732 case SFmode:
733 if (!TARGET_HARD_FLOAT)
734 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode,
735 cmp0, cmp1));
736 invert = FALSE;
737 cmp = gen_float_relational (test_code, cmp0, cmp1);
738 break;
741 /* Generate the branch. */
743 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
744 label2 = pc_rtx;
746 if (invert)
748 label2 = label1;
749 label1 = pc_rtx;
752 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
753 gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
754 label1,
755 label2)));
759 static rtx
760 gen_conditional_move (enum rtx_code code, enum machine_mode mode,
761 rtx op0, rtx op1)
763 if (mode == SImode)
765 rtx cmp;
767 /* Jump optimization calls get_condition() which canonicalizes
768 comparisons like (GE x <const>) to (GT x <const-1>).
769 Transform those comparisons back to GE, since that is the
770 comparison supported in Xtensa. We shouldn't have to
771 transform <LE x const> comparisons, because neither
772 xtensa_expand_conditional_branch() nor get_condition() will
773 produce them. */
775 if ((code == GT) && (op1 == constm1_rtx))
777 code = GE;
778 op1 = const0_rtx;
780 cmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
782 if (boolean_operator (cmp, VOIDmode))
784 /* Swap the operands to make const0 second. */
785 if (op0 == const0_rtx)
787 op0 = op1;
788 op1 = const0_rtx;
791 /* If not comparing against zero, emit a comparison (subtract). */
792 if (op1 != const0_rtx)
794 op0 = expand_binop (SImode, sub_optab, op0, op1,
795 0, 0, OPTAB_LIB_WIDEN);
796 op1 = const0_rtx;
799 else if (branch_operator (cmp, VOIDmode))
801 /* Swap the operands to make const0 second. */
802 if (op0 == const0_rtx)
804 op0 = op1;
805 op1 = const0_rtx;
807 switch (code)
809 case LT: code = GE; break;
810 case GE: code = LT; break;
811 default: gcc_unreachable ();
815 if (op1 != const0_rtx)
816 return 0;
818 else
819 return 0;
821 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
824 if (TARGET_HARD_FLOAT && mode == SFmode)
825 return gen_float_relational (code, op0, op1);
827 return 0;
832 xtensa_expand_conditional_move (rtx *operands, int isflt)
834 rtx dest = operands[0];
835 rtx cmp = operands[1];
836 enum machine_mode cmp_mode = GET_MODE (XEXP (cmp, 0));
837 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
839 if (!(cmp = gen_conditional_move (GET_CODE (cmp), cmp_mode,
840 XEXP (cmp, 0), XEXP (cmp, 1))))
841 return 0;
843 if (isflt)
844 gen_fn = (cmp_mode == SImode
845 ? gen_movsfcc_internal0
846 : gen_movsfcc_internal1);
847 else
848 gen_fn = (cmp_mode == SImode
849 ? gen_movsicc_internal0
850 : gen_movsicc_internal1);
852 emit_insn (gen_fn (dest, XEXP (cmp, 0), operands[2], operands[3], cmp));
853 return 1;
858 xtensa_expand_scc (rtx operands[4], enum machine_mode cmp_mode)
860 rtx dest = operands[0];
861 rtx cmp;
862 rtx one_tmp, zero_tmp;
863 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
865 if (!(cmp = gen_conditional_move (GET_CODE (operands[1]), cmp_mode,
866 operands[2], operands[3])))
867 return 0;
869 one_tmp = gen_reg_rtx (SImode);
870 zero_tmp = gen_reg_rtx (SImode);
871 emit_insn (gen_movsi (one_tmp, const_true_rtx));
872 emit_insn (gen_movsi (zero_tmp, const0_rtx));
874 gen_fn = (cmp_mode == SImode
875 ? gen_movsicc_internal0
876 : gen_movsicc_internal1);
877 emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
878 return 1;
882 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1]. MODE is
883 for the output, i.e., the input operands are twice as big as MODE. */
885 void
886 xtensa_split_operand_pair (rtx operands[4], enum machine_mode mode)
888 switch (GET_CODE (operands[1]))
890 case REG:
891 operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
892 operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
893 break;
895 case MEM:
896 operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
897 operands[2] = adjust_address (operands[1], mode, 0);
898 break;
900 case CONST_INT:
901 case CONST_DOUBLE:
902 split_double (operands[1], &operands[2], &operands[3]);
903 break;
905 default:
906 gcc_unreachable ();
909 switch (GET_CODE (operands[0]))
911 case REG:
912 operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
913 operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
914 break;
916 case MEM:
917 operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
918 operands[0] = adjust_address (operands[0], mode, 0);
919 break;
921 default:
922 gcc_unreachable ();
927 /* Emit insns to move operands[1] into operands[0].
928 Return 1 if we have written out everything that needs to be done to
929 do the move. Otherwise, return 0 and the caller will emit the move
930 normally. */
933 xtensa_emit_move_sequence (rtx *operands, enum machine_mode mode)
935 rtx src = operands[1];
937 if (CONSTANT_P (src)
938 && (GET_CODE (src) != CONST_INT || ! xtensa_simm12b (INTVAL (src))))
940 rtx dst = operands[0];
942 if (xtensa_tls_referenced_p (src))
944 rtx addend = NULL;
946 if (GET_CODE (src) == CONST && GET_CODE (XEXP (src, 0)) == PLUS)
948 addend = XEXP (XEXP (src, 0), 1);
949 src = XEXP (XEXP (src, 0), 0);
952 src = xtensa_legitimize_tls_address (src);
953 if (addend)
955 src = gen_rtx_PLUS (mode, src, addend);
956 src = force_operand (src, dst);
958 emit_move_insn (dst, src);
959 return 1;
962 if (! TARGET_CONST16)
964 src = force_const_mem (SImode, src);
965 operands[1] = src;
968 /* PC-relative loads are always SImode, and CONST16 is only
969 supported in the movsi pattern, so add a SUBREG for any other
970 (smaller) mode. */
972 if (mode != SImode)
974 if (register_operand (dst, mode))
976 emit_move_insn (simplify_gen_subreg (SImode, dst, mode, 0), src);
977 return 1;
979 else
981 src = force_reg (SImode, src);
982 src = gen_lowpart_SUBREG (mode, src);
983 operands[1] = src;
988 if (!(reload_in_progress | reload_completed)
989 && !xtensa_valid_move (mode, operands))
990 operands[1] = force_reg (mode, operands[1]);
992 operands[1] = xtensa_copy_incoming_a7 (operands[1]);
994 /* During reload we don't want to emit (subreg:X (mem:Y)) since that
995 instruction won't be recognized after reload, so we remove the
996 subreg and adjust mem accordingly. */
997 if (reload_in_progress)
999 operands[0] = fixup_subreg_mem (operands[0]);
1000 operands[1] = fixup_subreg_mem (operands[1]);
1002 return 0;
1006 static rtx
1007 fixup_subreg_mem (rtx x)
1009 if (GET_CODE (x) == SUBREG
1010 && GET_CODE (SUBREG_REG (x)) == REG
1011 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1013 rtx temp =
1014 gen_rtx_SUBREG (GET_MODE (x),
1015 reg_equiv_mem [REGNO (SUBREG_REG (x))],
1016 SUBREG_BYTE (x));
1017 x = alter_subreg (&temp);
1019 return x;
1023 /* Check if an incoming argument in a7 is expected to be used soon and
1024 if OPND is a register or register pair that includes a7. If so,
1025 create a new pseudo and copy a7 into that pseudo at the very
1026 beginning of the function, followed by the special "set_frame_ptr"
1027 unspec_volatile insn. The return value is either the original
1028 operand, if it is not a7, or the new pseudo containing a copy of
1029 the incoming argument. This is necessary because the register
1030 allocator will ignore conflicts with a7 and may either assign some
1031 other pseudo to a7 or use a7 as the hard_frame_pointer, clobbering
1032 the incoming argument in a7. By copying the argument out of a7 as
1033 the very first thing, and then immediately following that with an
1034 unspec_volatile to keep the scheduler away, we should avoid any
1035 problems. Putting the set_frame_ptr insn at the beginning, with
1036 only the a7 copy before it, also makes it easier for the prologue
1037 expander to initialize the frame pointer after the a7 copy and to
1038 fix up the a7 copy to use the stack pointer instead of the frame
1039 pointer. */
1042 xtensa_copy_incoming_a7 (rtx opnd)
1044 rtx entry_insns = 0;
1045 rtx reg, tmp;
1046 enum machine_mode mode;
1048 if (!cfun->machine->need_a7_copy)
1049 return opnd;
1051 /* This function should never be called again once a7 has been copied. */
1052 gcc_assert (!cfun->machine->set_frame_ptr_insn);
1054 mode = GET_MODE (opnd);
1056 /* The operand using a7 may come in a later instruction, so just return
1057 the original operand if it doesn't use a7. */
1058 reg = opnd;
1059 if (GET_CODE (reg) == SUBREG)
1061 gcc_assert (SUBREG_BYTE (reg) == 0);
1062 reg = SUBREG_REG (reg);
1064 if (GET_CODE (reg) != REG
1065 || REGNO (reg) > A7_REG
1066 || REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) <= A7_REG)
1067 return opnd;
1069 /* 1-word args will always be in a7; 2-word args in a6/a7. */
1070 gcc_assert (REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) - 1 == A7_REG);
1072 cfun->machine->need_a7_copy = false;
1074 /* Copy a7 to a new pseudo at the function entry. Use gen_raw_REG to
1075 create the REG for a7 so that hard_frame_pointer_rtx is not used. */
1077 start_sequence ();
1078 tmp = gen_reg_rtx (mode);
1080 switch (mode)
1082 case DFmode:
1083 case DImode:
1084 /* Copy the value out of A7 here but keep the first word in A6 until
1085 after the set_frame_ptr insn. Otherwise, the register allocator
1086 may decide to put "subreg (tmp, 0)" in A7 and clobber the incoming
1087 value. */
1088 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 4),
1089 gen_raw_REG (SImode, A7_REG)));
1090 break;
1091 case SFmode:
1092 emit_insn (gen_movsf_internal (tmp, gen_raw_REG (mode, A7_REG)));
1093 break;
1094 case SImode:
1095 emit_insn (gen_movsi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1096 break;
1097 case HImode:
1098 emit_insn (gen_movhi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1099 break;
1100 case QImode:
1101 emit_insn (gen_movqi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1102 break;
1103 default:
1104 gcc_unreachable ();
1107 cfun->machine->set_frame_ptr_insn = emit_insn (gen_set_frame_ptr ());
1109 /* For DF and DI mode arguments, copy the incoming value in A6 now. */
1110 if (mode == DFmode || mode == DImode)
1111 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 0),
1112 gen_rtx_REG (SImode, A7_REG - 1)));
1113 entry_insns = get_insns ();
1114 end_sequence ();
1116 if (cfun->machine->vararg_a7)
1118 /* This is called from within builtin_saveregs, which will insert the
1119 saveregs code at the function entry, ahead of anything placed at
1120 the function entry now. Instead, save the sequence to be inserted
1121 at the beginning of the saveregs code. */
1122 cfun->machine->vararg_a7_copy = entry_insns;
1124 else
1126 /* Put entry_insns after the NOTE that starts the function. If
1127 this is inside a start_sequence, make the outer-level insn
1128 chain current, so the code is placed at the start of the
1129 function. */
1130 push_topmost_sequence ();
1131 /* Do not use entry_of_function() here. This is called from within
1132 expand_function_start, when the CFG still holds GIMPLE. */
1133 emit_insn_after (entry_insns, get_insns ());
1134 pop_topmost_sequence ();
1137 return tmp;
1141 /* Try to expand a block move operation to a sequence of RTL move
1142 instructions. If not optimizing, or if the block size is not a
1143 constant, or if the block is too large, the expansion fails and GCC
1144 falls back to calling memcpy().
1146 operands[0] is the destination
1147 operands[1] is the source
1148 operands[2] is the length
1149 operands[3] is the alignment */
1152 xtensa_expand_block_move (rtx *operands)
1154 static const enum machine_mode mode_from_align[] =
1156 VOIDmode, QImode, HImode, VOIDmode, SImode,
1159 rtx dst_mem = operands[0];
1160 rtx src_mem = operands[1];
1161 HOST_WIDE_INT bytes, align;
1162 int num_pieces, move_ratio;
1163 rtx temp[2];
1164 enum machine_mode mode[2];
1165 int amount[2];
1166 bool active[2];
1167 int phase = 0;
1168 int next;
1169 int offset_ld = 0;
1170 int offset_st = 0;
1171 rtx x;
1173 /* If this is not a fixed size move, just call memcpy. */
1174 if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
1175 return 0;
1177 bytes = INTVAL (operands[2]);
1178 align = INTVAL (operands[3]);
1180 /* Anything to move? */
1181 if (bytes <= 0)
1182 return 0;
1184 if (align > MOVE_MAX)
1185 align = MOVE_MAX;
1187 /* Decide whether to expand inline based on the optimization level. */
1188 move_ratio = 4;
1189 if (optimize > 2)
1190 move_ratio = LARGEST_MOVE_RATIO;
1191 num_pieces = (bytes / align) + (bytes % align); /* Close enough anyway. */
1192 if (num_pieces > move_ratio)
1193 return 0;
1195 x = XEXP (dst_mem, 0);
1196 if (!REG_P (x))
1198 x = force_reg (Pmode, x);
1199 dst_mem = replace_equiv_address (dst_mem, x);
1202 x = XEXP (src_mem, 0);
1203 if (!REG_P (x))
1205 x = force_reg (Pmode, x);
1206 src_mem = replace_equiv_address (src_mem, x);
1209 active[0] = active[1] = false;
1213 next = phase;
1214 phase ^= 1;
1216 if (bytes > 0)
1218 int next_amount;
1220 next_amount = (bytes >= 4 ? 4 : (bytes >= 2 ? 2 : 1));
1221 next_amount = MIN (next_amount, align);
1223 amount[next] = next_amount;
1224 mode[next] = mode_from_align[next_amount];
1225 temp[next] = gen_reg_rtx (mode[next]);
1227 x = adjust_address (src_mem, mode[next], offset_ld);
1228 emit_insn (gen_rtx_SET (VOIDmode, temp[next], x));
1230 offset_ld += next_amount;
1231 bytes -= next_amount;
1232 active[next] = true;
1235 if (active[phase])
1237 active[phase] = false;
1239 x = adjust_address (dst_mem, mode[phase], offset_st);
1240 emit_insn (gen_rtx_SET (VOIDmode, x, temp[phase]));
1242 offset_st += amount[phase];
1245 while (active[next]);
1247 return 1;
1251 void
1252 xtensa_expand_nonlocal_goto (rtx *operands)
1254 rtx goto_handler = operands[1];
1255 rtx containing_fp = operands[3];
1257 /* Generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1258 is too big to generate in-line. */
1260 if (GET_CODE (containing_fp) != REG)
1261 containing_fp = force_reg (Pmode, containing_fp);
1263 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1264 0, VOIDmode, 2,
1265 containing_fp, Pmode,
1266 goto_handler, Pmode);
1270 static struct machine_function *
1271 xtensa_init_machine_status (void)
1273 return GGC_CNEW (struct machine_function);
1277 /* Shift VAL of mode MODE left by COUNT bits. */
1279 static inline rtx
1280 xtensa_expand_mask_and_shift (rtx val, enum machine_mode mode, rtx count)
1282 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
1283 NULL_RTX, 1, OPTAB_DIRECT);
1284 return expand_simple_binop (SImode, ASHIFT, val, count,
1285 NULL_RTX, 1, OPTAB_DIRECT);
1289 /* Structure to hold the initial parameters for a compare_and_swap operation
1290 in HImode and QImode. */
1292 struct alignment_context
1294 rtx memsi; /* SI aligned memory location. */
1295 rtx shift; /* Bit offset with regard to lsb. */
1296 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
1297 rtx modemaski; /* ~modemask */
1301 /* Initialize structure AC for word access to HI and QI mode memory. */
1303 static void
1304 init_alignment_context (struct alignment_context *ac, rtx mem)
1306 enum machine_mode mode = GET_MODE (mem);
1307 rtx byteoffset = NULL_RTX;
1308 bool aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
1310 if (aligned)
1311 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
1312 else
1314 /* Alignment is unknown. */
1315 rtx addr, align;
1317 /* Force the address into a register. */
1318 addr = force_reg (Pmode, XEXP (mem, 0));
1320 /* Align it to SImode. */
1321 align = expand_simple_binop (Pmode, AND, addr,
1322 GEN_INT (-GET_MODE_SIZE (SImode)),
1323 NULL_RTX, 1, OPTAB_DIRECT);
1324 /* Generate MEM. */
1325 ac->memsi = gen_rtx_MEM (SImode, align);
1326 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
1327 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
1328 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
1330 byteoffset = expand_simple_binop (Pmode, AND, addr,
1331 GEN_INT (GET_MODE_SIZE (SImode) - 1),
1332 NULL_RTX, 1, OPTAB_DIRECT);
1335 /* Calculate shiftcount. */
1336 if (TARGET_BIG_ENDIAN)
1338 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
1339 if (!aligned)
1340 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
1341 NULL_RTX, 1, OPTAB_DIRECT);
1343 else
1345 if (aligned)
1346 ac->shift = NULL_RTX;
1347 else
1348 ac->shift = byteoffset;
1351 if (ac->shift != NULL_RTX)
1353 /* Shift is the byte count, but we need the bitcount. */
1354 ac->shift = expand_simple_binop (SImode, MULT, ac->shift,
1355 GEN_INT (BITS_PER_UNIT),
1356 NULL_RTX, 1, OPTAB_DIRECT);
1357 ac->modemask = expand_simple_binop (SImode, ASHIFT,
1358 GEN_INT (GET_MODE_MASK (mode)),
1359 ac->shift,
1360 NULL_RTX, 1, OPTAB_DIRECT);
1362 else
1363 ac->modemask = GEN_INT (GET_MODE_MASK (mode));
1365 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask, NULL_RTX, 1);
1369 /* Expand an atomic compare and swap operation for HImode and QImode.
1370 MEM is the memory location, CMP the old value to compare MEM with
1371 and NEW_RTX the value to set if CMP == MEM. */
1373 void
1374 xtensa_expand_compare_and_swap (rtx target, rtx mem, rtx cmp, rtx new_rtx)
1376 enum machine_mode mode = GET_MODE (mem);
1377 struct alignment_context ac;
1378 rtx tmp, cmpv, newv, val;
1379 rtx oldval = gen_reg_rtx (SImode);
1380 rtx res = gen_reg_rtx (SImode);
1381 rtx csloop = gen_label_rtx ();
1382 rtx csend = gen_label_rtx ();
1384 init_alignment_context (&ac, mem);
1386 if (ac.shift != NULL_RTX)
1388 cmp = xtensa_expand_mask_and_shift (cmp, mode, ac.shift);
1389 new_rtx = xtensa_expand_mask_and_shift (new_rtx, mode, ac.shift);
1392 /* Load the surrounding word into VAL with the MEM value masked out. */
1393 val = force_reg (SImode, expand_simple_binop (SImode, AND, ac.memsi,
1394 ac.modemaski, NULL_RTX, 1,
1395 OPTAB_DIRECT));
1396 emit_label (csloop);
1398 /* Patch CMP and NEW_RTX into VAL at correct position. */
1399 cmpv = force_reg (SImode, expand_simple_binop (SImode, IOR, cmp, val,
1400 NULL_RTX, 1, OPTAB_DIRECT));
1401 newv = force_reg (SImode, expand_simple_binop (SImode, IOR, new_rtx, val,
1402 NULL_RTX, 1, OPTAB_DIRECT));
1404 /* Jump to end if we're done. */
1405 emit_insn (gen_sync_compare_and_swapsi (res, ac.memsi, cmpv, newv));
1406 emit_cmp_and_jump_insns (res, cmpv, EQ, const0_rtx, SImode, true, csend);
1408 /* Check for changes outside mode. */
1409 emit_move_insn (oldval, val);
1410 tmp = expand_simple_binop (SImode, AND, res, ac.modemaski,
1411 val, 1, OPTAB_DIRECT);
1412 if (tmp != val)
1413 emit_move_insn (val, tmp);
1415 /* Loop internal if so. */
1416 emit_cmp_and_jump_insns (oldval, val, NE, const0_rtx, SImode, true, csloop);
1418 emit_label (csend);
1420 /* Return the correct part of the bitfield. */
1421 convert_move (target,
1422 (ac.shift == NULL_RTX ? res
1423 : expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
1424 NULL_RTX, 1, OPTAB_DIRECT)),
1429 /* Expand an atomic operation CODE of mode MODE (either HImode or QImode --
1430 the default expansion works fine for SImode). MEM is the memory location
1431 and VAL the value to play with. If AFTER is true then store the value
1432 MEM holds after the operation, if AFTER is false then store the value MEM
1433 holds before the operation. If TARGET is zero then discard that value, else
1434 store it to TARGET. */
1436 void
1437 xtensa_expand_atomic (enum rtx_code code, rtx target, rtx mem, rtx val,
1438 bool after)
1440 enum machine_mode mode = GET_MODE (mem);
1441 struct alignment_context ac;
1442 rtx csloop = gen_label_rtx ();
1443 rtx cmp, tmp;
1444 rtx old = gen_reg_rtx (SImode);
1445 rtx new_rtx = gen_reg_rtx (SImode);
1446 rtx orig = NULL_RTX;
1448 init_alignment_context (&ac, mem);
1450 /* Prepare values before the compare-and-swap loop. */
1451 if (ac.shift != NULL_RTX)
1452 val = xtensa_expand_mask_and_shift (val, mode, ac.shift);
1453 switch (code)
1455 case PLUS:
1456 case MINUS:
1457 orig = gen_reg_rtx (SImode);
1458 convert_move (orig, val, 1);
1459 break;
1461 case SET:
1462 case IOR:
1463 case XOR:
1464 break;
1466 case MULT: /* NAND */
1467 case AND:
1468 /* val = "11..1<val>11..1" */
1469 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
1470 NULL_RTX, 1, OPTAB_DIRECT);
1471 break;
1473 default:
1474 gcc_unreachable ();
1477 /* Load full word. Subsequent loads are performed by S32C1I. */
1478 cmp = force_reg (SImode, ac.memsi);
1480 emit_label (csloop);
1481 emit_move_insn (old, cmp);
1483 switch (code)
1485 case PLUS:
1486 case MINUS:
1487 val = expand_simple_binop (SImode, code, old, orig,
1488 NULL_RTX, 1, OPTAB_DIRECT);
1489 val = expand_simple_binop (SImode, AND, val, ac.modemask,
1490 NULL_RTX, 1, OPTAB_DIRECT);
1491 /* FALLTHRU */
1492 case SET:
1493 tmp = expand_simple_binop (SImode, AND, old, ac.modemaski,
1494 NULL_RTX, 1, OPTAB_DIRECT);
1495 tmp = expand_simple_binop (SImode, IOR, tmp, val,
1496 new_rtx, 1, OPTAB_DIRECT);
1497 break;
1499 case AND:
1500 case IOR:
1501 case XOR:
1502 tmp = expand_simple_binop (SImode, code, old, val,
1503 new_rtx, 1, OPTAB_DIRECT);
1504 break;
1506 case MULT: /* NAND */
1507 tmp = expand_simple_binop (SImode, XOR, old, ac.modemask,
1508 NULL_RTX, 1, OPTAB_DIRECT);
1509 tmp = expand_simple_binop (SImode, AND, tmp, val,
1510 new_rtx, 1, OPTAB_DIRECT);
1511 break;
1513 default:
1514 gcc_unreachable ();
1517 if (tmp != new_rtx)
1518 emit_move_insn (new_rtx, tmp);
1519 emit_insn (gen_sync_compare_and_swapsi (cmp, ac.memsi, old, new_rtx));
1520 emit_cmp_and_jump_insns (cmp, old, NE, const0_rtx, SImode, true, csloop);
1522 if (target)
1524 tmp = (after ? new_rtx : cmp);
1525 convert_move (target,
1526 (ac.shift == NULL_RTX ? tmp
1527 : expand_simple_binop (SImode, LSHIFTRT, tmp, ac.shift,
1528 NULL_RTX, 1, OPTAB_DIRECT)),
1534 void
1535 xtensa_setup_frame_addresses (void)
1537 /* Set flag to cause FRAME_POINTER_REQUIRED to be set. */
1538 cfun->machine->accesses_prev_frame = 1;
1540 emit_library_call
1541 (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
1542 0, VOIDmode, 0);
1546 /* Emit the assembly for the end of a zero-cost loop. Normally we just emit
1547 a comment showing where the end of the loop is. However, if there is a
1548 label or a branch at the end of the loop then we need to place a nop
1549 there. If the loop ends with a label we need the nop so that branches
1550 targeting that label will target the nop (and thus remain in the loop),
1551 instead of targeting the instruction after the loop (and thus exiting
1552 the loop). If the loop ends with a branch, we need the nop in case the
1553 branch is targeting a location inside the loop. When the branch
1554 executes it will cause the loop count to be decremented even if it is
1555 taken (because it is the last instruction in the loop), so we need to
1556 nop after the branch to prevent the loop count from being decremented
1557 when the branch is taken. */
1559 void
1560 xtensa_emit_loop_end (rtx insn, rtx *operands)
1562 char done = 0;
1564 for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
1566 switch (GET_CODE (insn))
1568 case NOTE:
1569 case BARRIER:
1570 break;
1572 case CODE_LABEL:
1573 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1574 done = 1;
1575 break;
1577 default:
1579 rtx body = PATTERN (insn);
1581 if (GET_CODE (body) == JUMP_INSN)
1583 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1584 done = 1;
1586 else if ((GET_CODE (body) != USE)
1587 && (GET_CODE (body) != CLOBBER))
1588 done = 1;
1590 break;
1594 output_asm_insn ("# loop end for %0", operands);
1598 char *
1599 xtensa_emit_branch (bool inverted, bool immed, rtx *operands)
1601 static char result[64];
1602 enum rtx_code code;
1603 const char *op;
1605 code = GET_CODE (operands[3]);
1606 switch (code)
1608 case EQ: op = inverted ? "ne" : "eq"; break;
1609 case NE: op = inverted ? "eq" : "ne"; break;
1610 case LT: op = inverted ? "ge" : "lt"; break;
1611 case GE: op = inverted ? "lt" : "ge"; break;
1612 case LTU: op = inverted ? "geu" : "ltu"; break;
1613 case GEU: op = inverted ? "ltu" : "geu"; break;
1614 default: gcc_unreachable ();
1617 if (immed)
1619 if (INTVAL (operands[1]) == 0)
1620 sprintf (result, "b%sz%s\t%%0, %%2", op,
1621 (TARGET_DENSITY && (code == EQ || code == NE)) ? ".n" : "");
1622 else
1623 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1625 else
1626 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1628 return result;
1632 char *
1633 xtensa_emit_bit_branch (bool inverted, bool immed, rtx *operands)
1635 static char result[64];
1636 const char *op;
1638 switch (GET_CODE (operands[3]))
1640 case EQ: op = inverted ? "bs" : "bc"; break;
1641 case NE: op = inverted ? "bc" : "bs"; break;
1642 default: gcc_unreachable ();
1645 if (immed)
1647 unsigned bitnum = INTVAL (operands[1]) & 0x1f;
1648 operands[1] = GEN_INT (bitnum);
1649 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
1651 else
1652 sprintf (result, "b%s\t%%0, %%1, %%2", op);
1654 return result;
1658 char *
1659 xtensa_emit_movcc (bool inverted, bool isfp, bool isbool, rtx *operands)
1661 static char result[64];
1662 enum rtx_code code;
1663 const char *op;
1665 code = GET_CODE (operands[4]);
1666 if (isbool)
1668 switch (code)
1670 case EQ: op = inverted ? "t" : "f"; break;
1671 case NE: op = inverted ? "f" : "t"; break;
1672 default: gcc_unreachable ();
1675 else
1677 switch (code)
1679 case EQ: op = inverted ? "nez" : "eqz"; break;
1680 case NE: op = inverted ? "eqz" : "nez"; break;
1681 case LT: op = inverted ? "gez" : "ltz"; break;
1682 case GE: op = inverted ? "ltz" : "gez"; break;
1683 default: gcc_unreachable ();
1687 sprintf (result, "mov%s%s\t%%0, %%%d, %%1",
1688 op, isfp ? ".s" : "", inverted ? 3 : 2);
1689 return result;
1693 char *
1694 xtensa_emit_call (int callop, rtx *operands)
1696 static char result[64];
1697 rtx tgt = operands[callop];
1699 if (GET_CODE (tgt) == CONST_INT)
1700 sprintf (result, "call8\t0x%lx", INTVAL (tgt));
1701 else if (register_operand (tgt, VOIDmode))
1702 sprintf (result, "callx8\t%%%d", callop);
1703 else
1704 sprintf (result, "call8\t%%%d", callop);
1706 return result;
1710 bool
1711 xtensa_legitimate_address_p (enum machine_mode mode, rtx addr, bool strict)
1713 /* Allow constant pool addresses. */
1714 if (mode != BLKmode && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
1715 && ! TARGET_CONST16 && constantpool_address_p (addr)
1716 && ! xtensa_tls_referenced_p (addr))
1717 return true;
1719 while (GET_CODE (addr) == SUBREG)
1720 addr = SUBREG_REG (addr);
1722 /* Allow base registers. */
1723 if (GET_CODE (addr) == REG && BASE_REG_P (addr, strict))
1724 return true;
1726 /* Check for "register + offset" addressing. */
1727 if (GET_CODE (addr) == PLUS)
1729 rtx xplus0 = XEXP (addr, 0);
1730 rtx xplus1 = XEXP (addr, 1);
1731 enum rtx_code code0;
1732 enum rtx_code code1;
1734 while (GET_CODE (xplus0) == SUBREG)
1735 xplus0 = SUBREG_REG (xplus0);
1736 code0 = GET_CODE (xplus0);
1738 while (GET_CODE (xplus1) == SUBREG)
1739 xplus1 = SUBREG_REG (xplus1);
1740 code1 = GET_CODE (xplus1);
1742 /* Swap operands if necessary so the register is first. */
1743 if (code0 != REG && code1 == REG)
1745 xplus0 = XEXP (addr, 1);
1746 xplus1 = XEXP (addr, 0);
1747 code0 = GET_CODE (xplus0);
1748 code1 = GET_CODE (xplus1);
1751 if (code0 == REG && BASE_REG_P (xplus0, strict)
1752 && code1 == CONST_INT
1753 && xtensa_mem_offset (INTVAL (xplus1), mode))
1754 return true;
1757 return false;
1761 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol. */
1763 static GTY(()) rtx xtensa_tls_module_base_symbol;
1765 static rtx
1766 xtensa_tls_module_base (void)
1768 if (! xtensa_tls_module_base_symbol)
1770 xtensa_tls_module_base_symbol =
1771 gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
1772 SYMBOL_REF_FLAGS (xtensa_tls_module_base_symbol)
1773 |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
1776 return xtensa_tls_module_base_symbol;
1780 static rtx
1781 xtensa_call_tls_desc (rtx sym, rtx *retp)
1783 rtx fn, arg, a10, call_insn, insns;
1785 start_sequence ();
1786 fn = gen_reg_rtx (Pmode);
1787 arg = gen_reg_rtx (Pmode);
1788 a10 = gen_rtx_REG (Pmode, 10);
1790 emit_insn (gen_tls_func (fn, sym));
1791 emit_insn (gen_tls_arg (arg, sym));
1792 emit_move_insn (a10, arg);
1793 call_insn = emit_call_insn (gen_tls_call (a10, fn, sym, const1_rtx));
1794 CALL_INSN_FUNCTION_USAGE (call_insn)
1795 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, a10),
1796 CALL_INSN_FUNCTION_USAGE (call_insn));
1797 insns = get_insns ();
1798 end_sequence ();
1800 *retp = a10;
1801 return insns;
1805 static rtx
1806 xtensa_legitimize_tls_address (rtx x)
1808 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
1809 rtx dest, tp, ret, modbase, base, addend, insns;
1811 dest = gen_reg_rtx (Pmode);
1812 switch (model)
1814 case TLS_MODEL_GLOBAL_DYNAMIC:
1815 insns = xtensa_call_tls_desc (x, &ret);
1816 emit_libcall_block (insns, dest, ret, x);
1817 break;
1819 case TLS_MODEL_LOCAL_DYNAMIC:
1820 base = gen_reg_rtx (Pmode);
1821 modbase = xtensa_tls_module_base ();
1822 insns = xtensa_call_tls_desc (modbase, &ret);
1823 emit_libcall_block (insns, base, ret, modbase);
1824 addend = force_reg (SImode, gen_sym_DTPOFF (x));
1825 emit_insn (gen_addsi3 (dest, base, addend));
1826 break;
1828 case TLS_MODEL_INITIAL_EXEC:
1829 case TLS_MODEL_LOCAL_EXEC:
1830 tp = gen_reg_rtx (SImode);
1831 emit_insn (gen_load_tp (tp));
1832 addend = force_reg (SImode, gen_sym_TPOFF (x));
1833 emit_insn (gen_addsi3 (dest, tp, addend));
1834 break;
1836 default:
1837 gcc_unreachable ();
1840 return dest;
1845 xtensa_legitimize_address (rtx x,
1846 rtx oldx ATTRIBUTE_UNUSED,
1847 enum machine_mode mode)
1849 if (xtensa_tls_symbol_p (x))
1850 return xtensa_legitimize_tls_address (x);
1852 if (GET_CODE (x) == PLUS)
1854 rtx plus0 = XEXP (x, 0);
1855 rtx plus1 = XEXP (x, 1);
1857 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
1859 plus0 = XEXP (x, 1);
1860 plus1 = XEXP (x, 0);
1863 /* Try to split up the offset to use an ADDMI instruction. */
1864 if (GET_CODE (plus0) == REG
1865 && GET_CODE (plus1) == CONST_INT
1866 && !xtensa_mem_offset (INTVAL (plus1), mode)
1867 && !xtensa_simm8 (INTVAL (plus1))
1868 && xtensa_mem_offset (INTVAL (plus1) & 0xff, mode)
1869 && xtensa_simm8x256 (INTVAL (plus1) & ~0xff))
1871 rtx temp = gen_reg_rtx (Pmode);
1872 rtx addmi_offset = GEN_INT (INTVAL (plus1) & ~0xff);
1873 emit_insn (gen_rtx_SET (Pmode, temp,
1874 gen_rtx_PLUS (Pmode, plus0, addmi_offset)));
1875 return gen_rtx_PLUS (Pmode, temp, GEN_INT (INTVAL (plus1) & 0xff));
1879 return x;
1883 /* Helper for xtensa_tls_referenced_p. */
1885 static int
1886 xtensa_tls_referenced_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1888 if (GET_CODE (*x) == SYMBOL_REF)
1889 return SYMBOL_REF_TLS_MODEL (*x) != 0;
1891 /* Ignore TLS references that have already been legitimized. */
1892 if (GET_CODE (*x) == UNSPEC)
1894 switch (XINT (*x, 1))
1896 case UNSPEC_TPOFF:
1897 case UNSPEC_DTPOFF:
1898 case UNSPEC_TLS_FUNC:
1899 case UNSPEC_TLS_ARG:
1900 case UNSPEC_TLS_CALL:
1901 return -1;
1902 default:
1903 break;
1907 return 0;
1911 /* Return TRUE if X contains any TLS symbol references. */
1913 bool
1914 xtensa_tls_referenced_p (rtx x)
1916 if (! TARGET_HAVE_TLS)
1917 return false;
1919 return for_each_rtx (&x, xtensa_tls_referenced_p_1, NULL);
1923 /* Return the debugger register number to use for 'regno'. */
1926 xtensa_dbx_register_number (int regno)
1928 int first = -1;
1930 if (GP_REG_P (regno))
1932 regno -= GP_REG_FIRST;
1933 first = 0;
1935 else if (BR_REG_P (regno))
1937 regno -= BR_REG_FIRST;
1938 first = 16;
1940 else if (FP_REG_P (regno))
1942 regno -= FP_REG_FIRST;
1943 first = 48;
1945 else if (ACC_REG_P (regno))
1947 first = 0x200; /* Start of Xtensa special registers. */
1948 regno = 16; /* ACCLO is special register 16. */
1951 /* When optimizing, we sometimes get asked about pseudo-registers
1952 that don't represent hard registers. Return 0 for these. */
1953 if (first == -1)
1954 return 0;
1956 return first + regno;
1960 /* Argument support functions. */
1962 /* Initialize CUMULATIVE_ARGS for a function. */
1964 void
1965 init_cumulative_args (CUMULATIVE_ARGS *cum, int incoming)
1967 cum->arg_words = 0;
1968 cum->incoming = incoming;
1972 /* Advance the argument to the next argument position. */
1974 void
1975 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type)
1977 int words, max;
1978 int *arg_words;
1980 arg_words = &cum->arg_words;
1981 max = MAX_ARGS_IN_REGISTERS;
1983 words = (((mode != BLKmode)
1984 ? (int) GET_MODE_SIZE (mode)
1985 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1987 if (*arg_words < max
1988 && (targetm.calls.must_pass_in_stack (mode, type)
1989 || *arg_words + words > max))
1990 *arg_words = max;
1992 *arg_words += words;
1996 /* Return an RTL expression containing the register for the given mode,
1997 or 0 if the argument is to be passed on the stack. INCOMING_P is nonzero
1998 if this is an incoming argument to the current function. */
2001 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
2002 int incoming_p)
2004 int regbase, words, max;
2005 int *arg_words;
2006 int regno;
2008 arg_words = &cum->arg_words;
2009 regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
2010 max = MAX_ARGS_IN_REGISTERS;
2012 words = (((mode != BLKmode)
2013 ? (int) GET_MODE_SIZE (mode)
2014 : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2016 if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
2018 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_WORD;
2019 *arg_words = (*arg_words + align - 1) & -align;
2022 if (*arg_words + words > max)
2023 return (rtx)0;
2025 regno = regbase + *arg_words;
2027 if (cum->incoming && regno <= A7_REG && regno + words > A7_REG)
2028 cfun->machine->need_a7_copy = true;
2030 return gen_rtx_REG (mode, regno);
2035 function_arg_boundary (enum machine_mode mode, tree type)
2037 unsigned int alignment;
2039 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2040 if (alignment < PARM_BOUNDARY)
2041 alignment = PARM_BOUNDARY;
2042 if (alignment > STACK_BOUNDARY)
2043 alignment = STACK_BOUNDARY;
2044 return alignment;
2048 static bool
2049 xtensa_return_in_msb (const_tree valtype)
2051 return (TARGET_BIG_ENDIAN
2052 && AGGREGATE_TYPE_P (valtype)
2053 && int_size_in_bytes (valtype) >= UNITS_PER_WORD);
2057 void
2058 override_options (void)
2060 int regno;
2061 enum machine_mode mode;
2063 if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
2064 error ("boolean registers required for the floating-point option");
2066 /* Set up array giving whether a given register can hold a given mode. */
2067 for (mode = VOIDmode;
2068 mode != MAX_MACHINE_MODE;
2069 mode = (enum machine_mode) ((int) mode + 1))
2071 int size = GET_MODE_SIZE (mode);
2072 enum mode_class mclass = GET_MODE_CLASS (mode);
2074 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2076 int temp;
2078 if (ACC_REG_P (regno))
2079 temp = (TARGET_MAC16
2080 && (mclass == MODE_INT) && (size <= UNITS_PER_WORD));
2081 else if (GP_REG_P (regno))
2082 temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
2083 else if (FP_REG_P (regno))
2084 temp = (TARGET_HARD_FLOAT && (mode == SFmode));
2085 else if (BR_REG_P (regno))
2086 temp = (TARGET_BOOLEANS && (mode == CCmode));
2087 else
2088 temp = FALSE;
2090 xtensa_hard_regno_mode_ok[(int) mode][regno] = temp;
2094 init_machine_status = xtensa_init_machine_status;
2096 /* Check PIC settings. PIC is only supported when using L32R
2097 instructions, and some targets need to always use PIC. */
2098 if (flag_pic && TARGET_CONST16)
2099 error ("-f%s is not supported with CONST16 instructions",
2100 (flag_pic > 1 ? "PIC" : "pic"));
2101 else if (XTENSA_ALWAYS_PIC)
2103 if (TARGET_CONST16)
2104 error ("PIC is required but not supported with CONST16 instructions");
2105 flag_pic = 1;
2107 /* There's no need for -fPIC (as opposed to -fpic) on Xtensa. */
2108 if (flag_pic > 1)
2109 flag_pic = 1;
2110 if (flag_pic && !flag_pie)
2111 flag_shlib = 1;
2113 /* Hot/cold partitioning does not work on this architecture, because of
2114 constant pools (the load instruction cannot necessarily reach that far).
2115 Therefore disable it on this architecture. */
2116 if (flag_reorder_blocks_and_partition)
2118 flag_reorder_blocks_and_partition = 0;
2119 flag_reorder_blocks = 1;
2124 /* A C compound statement to output to stdio stream STREAM the
2125 assembler syntax for an instruction operand X. X is an RTL
2126 expression.
2128 CODE is a value that can be used to specify one of several ways
2129 of printing the operand. It is used when identical operands
2130 must be printed differently depending on the context. CODE
2131 comes from the '%' specification that was used to request
2132 printing of the operand. If the specification was just '%DIGIT'
2133 then CODE is 0; if the specification was '%LTR DIGIT' then CODE
2134 is the ASCII code for LTR.
2136 If X is a register, this macro should print the register's name.
2137 The names can be found in an array 'reg_names' whose type is
2138 'char *[]'. 'reg_names' is initialized from 'REGISTER_NAMES'.
2140 When the machine description has a specification '%PUNCT' (a '%'
2141 followed by a punctuation character), this macro is called with
2142 a null pointer for X and the punctuation character for CODE.
2144 'a', 'c', 'l', and 'n' are reserved.
2146 The Xtensa specific codes are:
2148 'd' CONST_INT, print as signed decimal
2149 'x' CONST_INT, print as signed hexadecimal
2150 'K' CONST_INT, print number of bits in mask for EXTUI
2151 'R' CONST_INT, print (X & 0x1f)
2152 'L' CONST_INT, print ((32 - X) & 0x1f)
2153 'D' REG, print second register of double-word register operand
2154 'N' MEM, print address of next word following a memory operand
2155 'v' MEM, if memory reference is volatile, output a MEMW before it
2156 't' any constant, add "@h" suffix for top 16 bits
2157 'b' any constant, add "@l" suffix for bottom 16 bits
2160 static void
2161 printx (FILE *file, signed int val)
2163 /* Print a hexadecimal value in a nice way. */
2164 if ((val > -0xa) && (val < 0xa))
2165 fprintf (file, "%d", val);
2166 else if (val < 0)
2167 fprintf (file, "-0x%x", -val);
2168 else
2169 fprintf (file, "0x%x", val);
2173 void
2174 print_operand (FILE *file, rtx x, int letter)
2176 if (!x)
2177 error ("PRINT_OPERAND null pointer");
2179 switch (letter)
2181 case 'D':
2182 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2183 fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
2184 else
2185 output_operand_lossage ("invalid %%D value");
2186 break;
2188 case 'v':
2189 if (GET_CODE (x) == MEM)
2191 /* For a volatile memory reference, emit a MEMW before the
2192 load or store. */
2193 if (MEM_VOLATILE_P (x) && TARGET_SERIALIZE_VOLATILE)
2194 fprintf (file, "memw\n\t");
2196 else
2197 output_operand_lossage ("invalid %%v value");
2198 break;
2200 case 'N':
2201 if (GET_CODE (x) == MEM
2202 && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
2204 x = adjust_address (x, GET_MODE (x) == DFmode ? SFmode : SImode, 4);
2205 output_address (XEXP (x, 0));
2207 else
2208 output_operand_lossage ("invalid %%N value");
2209 break;
2211 case 'K':
2212 if (GET_CODE (x) == CONST_INT)
2214 int num_bits = 0;
2215 unsigned val = INTVAL (x);
2216 while (val & 1)
2218 num_bits += 1;
2219 val = val >> 1;
2221 if ((val != 0) || (num_bits == 0) || (num_bits > 16))
2222 fatal_insn ("invalid mask", x);
2224 fprintf (file, "%d", num_bits);
2226 else
2227 output_operand_lossage ("invalid %%K value");
2228 break;
2230 case 'L':
2231 if (GET_CODE (x) == CONST_INT)
2232 fprintf (file, "%ld", (32 - INTVAL (x)) & 0x1f);
2233 else
2234 output_operand_lossage ("invalid %%L value");
2235 break;
2237 case 'R':
2238 if (GET_CODE (x) == CONST_INT)
2239 fprintf (file, "%ld", INTVAL (x) & 0x1f);
2240 else
2241 output_operand_lossage ("invalid %%R value");
2242 break;
2244 case 'x':
2245 if (GET_CODE (x) == CONST_INT)
2246 printx (file, INTVAL (x));
2247 else
2248 output_operand_lossage ("invalid %%x value");
2249 break;
2251 case 'd':
2252 if (GET_CODE (x) == CONST_INT)
2253 fprintf (file, "%ld", INTVAL (x));
2254 else
2255 output_operand_lossage ("invalid %%d value");
2256 break;
2258 case 't':
2259 case 'b':
2260 if (GET_CODE (x) == CONST_INT)
2262 printx (file, INTVAL (x));
2263 fputs (letter == 't' ? "@h" : "@l", file);
2265 else if (GET_CODE (x) == CONST_DOUBLE)
2267 REAL_VALUE_TYPE r;
2268 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2269 if (GET_MODE (x) == SFmode)
2271 long l;
2272 REAL_VALUE_TO_TARGET_SINGLE (r, l);
2273 fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
2275 else
2276 output_operand_lossage ("invalid %%t/%%b value");
2278 else if (GET_CODE (x) == CONST)
2280 /* X must be a symbolic constant on ELF. Write an expression
2281 suitable for 'const16' that sets the high or low 16 bits. */
2282 if (GET_CODE (XEXP (x, 0)) != PLUS
2283 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
2284 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2285 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
2286 output_operand_lossage ("invalid %%t/%%b value");
2287 print_operand (file, XEXP (XEXP (x, 0), 0), 0);
2288 fputs (letter == 't' ? "@h" : "@l", file);
2289 /* There must be a non-alphanumeric character between 'h' or 'l'
2290 and the number. The '-' is added by print_operand() already. */
2291 if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
2292 fputs ("+", file);
2293 print_operand (file, XEXP (XEXP (x, 0), 1), 0);
2295 else
2297 output_addr_const (file, x);
2298 fputs (letter == 't' ? "@h" : "@l", file);
2300 break;
2302 default:
2303 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2304 fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
2305 else if (GET_CODE (x) == MEM)
2306 output_address (XEXP (x, 0));
2307 else if (GET_CODE (x) == CONST_INT)
2308 fprintf (file, "%ld", INTVAL (x));
2309 else
2310 output_addr_const (file, x);
2315 /* A C compound statement to output to stdio stream STREAM the
2316 assembler syntax for an instruction operand that is a memory
2317 reference whose address is ADDR. ADDR is an RTL expression. */
2319 void
2320 print_operand_address (FILE *file, rtx addr)
2322 if (!addr)
2323 error ("PRINT_OPERAND_ADDRESS, null pointer");
2325 switch (GET_CODE (addr))
2327 default:
2328 fatal_insn ("invalid address", addr);
2329 break;
2331 case REG:
2332 fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
2333 break;
2335 case PLUS:
2337 rtx reg = (rtx)0;
2338 rtx offset = (rtx)0;
2339 rtx arg0 = XEXP (addr, 0);
2340 rtx arg1 = XEXP (addr, 1);
2342 if (GET_CODE (arg0) == REG)
2344 reg = arg0;
2345 offset = arg1;
2347 else if (GET_CODE (arg1) == REG)
2349 reg = arg1;
2350 offset = arg0;
2352 else
2353 fatal_insn ("no register in address", addr);
2355 if (CONSTANT_P (offset))
2357 fprintf (file, "%s, ", reg_names [REGNO (reg)]);
2358 output_addr_const (file, offset);
2360 else
2361 fatal_insn ("address offset not a constant", addr);
2363 break;
2365 case LABEL_REF:
2366 case SYMBOL_REF:
2367 case CONST_INT:
2368 case CONST:
2369 output_addr_const (file, addr);
2370 break;
2375 bool
2376 xtensa_output_addr_const_extra (FILE *fp, rtx x)
2378 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
2380 switch (XINT (x, 1))
2382 case UNSPEC_TPOFF:
2383 output_addr_const (fp, XVECEXP (x, 0, 0));
2384 fputs ("@TPOFF", fp);
2385 return true;
2386 case UNSPEC_DTPOFF:
2387 output_addr_const (fp, XVECEXP (x, 0, 0));
2388 fputs ("@DTPOFF", fp);
2389 return true;
2390 case UNSPEC_PLT:
2391 if (flag_pic)
2393 output_addr_const (fp, XVECEXP (x, 0, 0));
2394 fputs ("@PLT", fp);
2395 return true;
2397 break;
2398 default:
2399 break;
2402 return false;
2406 void
2407 xtensa_output_literal (FILE *file, rtx x, enum machine_mode mode, int labelno)
2409 long value_long[2];
2410 REAL_VALUE_TYPE r;
2411 int size;
2412 rtx first, second;
2414 fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
2416 switch (GET_MODE_CLASS (mode))
2418 case MODE_FLOAT:
2419 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
2421 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2422 switch (mode)
2424 case SFmode:
2425 REAL_VALUE_TO_TARGET_SINGLE (r, value_long[0]);
2426 if (HOST_BITS_PER_LONG > 32)
2427 value_long[0] &= 0xffffffff;
2428 fprintf (file, "0x%08lx\n", value_long[0]);
2429 break;
2431 case DFmode:
2432 REAL_VALUE_TO_TARGET_DOUBLE (r, value_long);
2433 if (HOST_BITS_PER_LONG > 32)
2435 value_long[0] &= 0xffffffff;
2436 value_long[1] &= 0xffffffff;
2438 fprintf (file, "0x%08lx, 0x%08lx\n",
2439 value_long[0], value_long[1]);
2440 break;
2442 default:
2443 gcc_unreachable ();
2446 break;
2448 case MODE_INT:
2449 case MODE_PARTIAL_INT:
2450 size = GET_MODE_SIZE (mode);
2451 switch (size)
2453 case 4:
2454 output_addr_const (file, x);
2455 fputs ("\n", file);
2456 break;
2458 case 8:
2459 split_double (x, &first, &second);
2460 output_addr_const (file, first);
2461 fputs (", ", file);
2462 output_addr_const (file, second);
2463 fputs ("\n", file);
2464 break;
2466 default:
2467 gcc_unreachable ();
2469 break;
2471 default:
2472 gcc_unreachable ();
2477 /* Return the bytes needed to compute the frame pointer from the current
2478 stack pointer. */
2480 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
2481 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
2483 long
2484 compute_frame_size (int size)
2486 /* Add space for the incoming static chain value. */
2487 if (cfun->static_chain_decl != NULL)
2488 size += (1 * UNITS_PER_WORD);
2490 xtensa_current_frame_size =
2491 XTENSA_STACK_ALIGN (size
2492 + crtl->outgoing_args_size
2493 + (WINDOW_SIZE * UNITS_PER_WORD));
2494 return xtensa_current_frame_size;
2499 xtensa_frame_pointer_required (void)
2501 /* The code to expand builtin_frame_addr and builtin_return_addr
2502 currently uses the hard_frame_pointer instead of frame_pointer.
2503 This seems wrong but maybe it's necessary for other architectures.
2504 This function is derived from the i386 code. */
2506 if (cfun->machine->accesses_prev_frame)
2507 return 1;
2509 return 0;
2513 /* minimum frame = reg save area (4 words) plus static chain (1 word)
2514 and the total number of words must be a multiple of 128 bits. */
2515 #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD)
2517 void
2518 xtensa_expand_prologue (void)
2520 HOST_WIDE_INT total_size;
2521 rtx size_rtx;
2522 rtx insn, note_rtx;
2524 total_size = compute_frame_size (get_frame_size ());
2525 size_rtx = GEN_INT (total_size);
2527 if (total_size < (1 << (12+3)))
2528 insn = emit_insn (gen_entry (size_rtx));
2529 else
2531 /* Use a8 as a temporary since a0-a7 may be live. */
2532 rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
2533 emit_insn (gen_entry (GEN_INT (MIN_FRAME_SIZE)));
2534 emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
2535 emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
2536 insn = emit_insn (gen_movsi (stack_pointer_rtx, tmp_reg));
2539 if (frame_pointer_needed)
2541 if (cfun->machine->set_frame_ptr_insn)
2543 rtx first;
2545 push_topmost_sequence ();
2546 first = get_insns ();
2547 pop_topmost_sequence ();
2549 /* For all instructions prior to set_frame_ptr_insn, replace
2550 hard_frame_pointer references with stack_pointer. */
2551 for (insn = first;
2552 insn != cfun->machine->set_frame_ptr_insn;
2553 insn = NEXT_INSN (insn))
2555 if (INSN_P (insn))
2557 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
2558 hard_frame_pointer_rtx,
2559 stack_pointer_rtx);
2560 df_insn_rescan (insn);
2564 else
2565 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
2566 stack_pointer_rtx));
2569 /* Create a note to describe the CFA. Because this is only used to set
2570 DW_AT_frame_base for debug info, don't bother tracking changes through
2571 each instruction in the prologue. It just takes up space. */
2572 note_rtx = gen_rtx_SET (VOIDmode, (frame_pointer_needed
2573 ? hard_frame_pointer_rtx
2574 : stack_pointer_rtx),
2575 plus_constant (stack_pointer_rtx, -total_size));
2576 RTX_FRAME_RELATED_P (insn) = 1;
2577 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2578 note_rtx, REG_NOTES (insn));
2582 /* Clear variables at function end. */
2584 void
2585 xtensa_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
2586 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
2588 xtensa_current_frame_size = 0;
2593 xtensa_return_addr (int count, rtx frame)
2595 rtx result, retaddr, curaddr, label;
2597 if (count == -1)
2598 retaddr = gen_rtx_REG (Pmode, A0_REG);
2599 else
2601 rtx addr = plus_constant (frame, -4 * UNITS_PER_WORD);
2602 addr = memory_address (Pmode, addr);
2603 retaddr = gen_reg_rtx (Pmode);
2604 emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
2607 /* The 2 most-significant bits of the return address on Xtensa hold
2608 the register window size. To get the real return address, these
2609 bits must be replaced with the high bits from some address in the
2610 code. */
2612 /* Get the 2 high bits of a local label in the code. */
2613 curaddr = gen_reg_rtx (Pmode);
2614 label = gen_label_rtx ();
2615 emit_label (label);
2616 LABEL_PRESERVE_P (label) = 1;
2617 emit_move_insn (curaddr, gen_rtx_LABEL_REF (Pmode, label));
2618 emit_insn (gen_lshrsi3 (curaddr, curaddr, GEN_INT (30)));
2619 emit_insn (gen_ashlsi3 (curaddr, curaddr, GEN_INT (30)));
2621 /* Clear the 2 high bits of the return address. */
2622 result = gen_reg_rtx (Pmode);
2623 emit_insn (gen_ashlsi3 (result, retaddr, GEN_INT (2)));
2624 emit_insn (gen_lshrsi3 (result, result, GEN_INT (2)));
2626 /* Combine them to get the result. */
2627 emit_insn (gen_iorsi3 (result, result, curaddr));
2628 return result;
2632 /* Create the va_list data type.
2634 This structure is set up by __builtin_saveregs. The __va_reg field
2635 points to a stack-allocated region holding the contents of the
2636 incoming argument registers. The __va_ndx field is an index
2637 initialized to the position of the first unnamed (variable)
2638 argument. This same index is also used to address the arguments
2639 passed in memory. Thus, the __va_stk field is initialized to point
2640 to the position of the first argument in memory offset to account
2641 for the arguments passed in registers and to account for the size
2642 of the argument registers not being 16-byte aligned. E.G., there
2643 are 6 argument registers of 4 bytes each, but we want the __va_ndx
2644 for the first stack argument to have the maximal alignment of 16
2645 bytes, so we offset the __va_stk address by 32 bytes so that
2646 __va_stk[32] references the first argument on the stack. */
2648 static tree
2649 xtensa_build_builtin_va_list (void)
2651 tree f_stk, f_reg, f_ndx, record, type_decl;
2653 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2654 type_decl = build_decl (BUILTINS_LOCATION,
2655 TYPE_DECL, get_identifier ("__va_list_tag"), record);
2657 f_stk = build_decl (BUILTINS_LOCATION,
2658 FIELD_DECL, get_identifier ("__va_stk"),
2659 ptr_type_node);
2660 f_reg = build_decl (BUILTINS_LOCATION,
2661 FIELD_DECL, get_identifier ("__va_reg"),
2662 ptr_type_node);
2663 f_ndx = build_decl (BUILTINS_LOCATION,
2664 FIELD_DECL, get_identifier ("__va_ndx"),
2665 integer_type_node);
2667 DECL_FIELD_CONTEXT (f_stk) = record;
2668 DECL_FIELD_CONTEXT (f_reg) = record;
2669 DECL_FIELD_CONTEXT (f_ndx) = record;
2671 TREE_CHAIN (record) = type_decl;
2672 TYPE_NAME (record) = type_decl;
2673 TYPE_FIELDS (record) = f_stk;
2674 TREE_CHAIN (f_stk) = f_reg;
2675 TREE_CHAIN (f_reg) = f_ndx;
2677 layout_type (record);
2678 return record;
2682 /* Save the incoming argument registers on the stack. Returns the
2683 address of the saved registers. */
2685 static rtx
2686 xtensa_builtin_saveregs (void)
2688 rtx gp_regs;
2689 int arg_words = crtl->args.info.arg_words;
2690 int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
2692 if (gp_left <= 0)
2693 return const0_rtx;
2695 /* Allocate the general-purpose register space. */
2696 gp_regs = assign_stack_local
2697 (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
2698 set_mem_alias_set (gp_regs, get_varargs_alias_set ());
2700 /* Now store the incoming registers. */
2701 cfun->machine->need_a7_copy = true;
2702 cfun->machine->vararg_a7 = true;
2703 move_block_from_reg (GP_ARG_FIRST + arg_words,
2704 adjust_address (gp_regs, BLKmode,
2705 arg_words * UNITS_PER_WORD),
2706 gp_left);
2707 gcc_assert (cfun->machine->vararg_a7_copy != 0);
2708 emit_insn_before (cfun->machine->vararg_a7_copy, get_insns ());
2710 return XEXP (gp_regs, 0);
2714 /* Implement `va_start' for varargs and stdarg. We look at the
2715 current function to fill in an initial va_list. */
2717 static void
2718 xtensa_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
2720 tree f_stk, stk;
2721 tree f_reg, reg;
2722 tree f_ndx, ndx;
2723 tree t, u;
2724 int arg_words;
2726 arg_words = crtl->args.info.arg_words;
2728 f_stk = TYPE_FIELDS (va_list_type_node);
2729 f_reg = TREE_CHAIN (f_stk);
2730 f_ndx = TREE_CHAIN (f_reg);
2732 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk, NULL_TREE);
2733 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
2734 f_reg, NULL_TREE);
2735 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
2736 f_ndx, NULL_TREE);
2738 /* Call __builtin_saveregs; save the result in __va_reg */
2739 u = make_tree (sizetype, expand_builtin_saveregs ());
2740 u = fold_convert (ptr_type_node, u);
2741 t = build2 (MODIFY_EXPR, ptr_type_node, reg, u);
2742 TREE_SIDE_EFFECTS (t) = 1;
2743 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2745 /* Set the __va_stk member to ($arg_ptr - 32). */
2746 u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
2747 u = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node, u, size_int (-32));
2748 t = build2 (MODIFY_EXPR, ptr_type_node, stk, u);
2749 TREE_SIDE_EFFECTS (t) = 1;
2750 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2752 /* Set the __va_ndx member. If the first variable argument is on
2753 the stack, adjust __va_ndx by 2 words to account for the extra
2754 alignment offset for __va_stk. */
2755 if (arg_words >= MAX_ARGS_IN_REGISTERS)
2756 arg_words += 2;
2757 t = build2 (MODIFY_EXPR, integer_type_node, ndx,
2758 build_int_cst (integer_type_node, arg_words * UNITS_PER_WORD));
2759 TREE_SIDE_EFFECTS (t) = 1;
2760 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2764 /* Implement `va_arg'. */
2766 static tree
2767 xtensa_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
2768 gimple_seq *post_p ATTRIBUTE_UNUSED)
2770 tree f_stk, stk;
2771 tree f_reg, reg;
2772 tree f_ndx, ndx;
2773 tree type_size, array, orig_ndx, addr, size, va_size, t;
2774 tree lab_false, lab_over, lab_false2;
2775 bool indirect;
2777 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
2778 if (indirect)
2779 type = build_pointer_type (type);
2781 /* Handle complex values as separate real and imaginary parts. */
2782 if (TREE_CODE (type) == COMPLEX_TYPE)
2784 tree real_part, imag_part;
2786 real_part = xtensa_gimplify_va_arg_expr (valist, TREE_TYPE (type),
2787 pre_p, NULL);
2788 real_part = get_initialized_tmp_var (real_part, pre_p, NULL);
2790 imag_part = xtensa_gimplify_va_arg_expr (unshare_expr (valist),
2791 TREE_TYPE (type),
2792 pre_p, NULL);
2793 imag_part = get_initialized_tmp_var (imag_part, pre_p, NULL);
2795 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
2798 f_stk = TYPE_FIELDS (va_list_type_node);
2799 f_reg = TREE_CHAIN (f_stk);
2800 f_ndx = TREE_CHAIN (f_reg);
2802 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist,
2803 f_stk, NULL_TREE);
2804 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
2805 f_reg, NULL_TREE);
2806 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
2807 f_ndx, NULL_TREE);
2809 type_size = size_in_bytes (type);
2810 va_size = round_up (type_size, UNITS_PER_WORD);
2811 gimplify_expr (&va_size, pre_p, NULL, is_gimple_val, fb_rvalue);
2814 /* First align __va_ndx if necessary for this arg:
2816 orig_ndx = (AP).__va_ndx;
2817 if (__alignof__ (TYPE) > 4 )
2818 orig_ndx = ((orig_ndx + __alignof__ (TYPE) - 1)
2819 & -__alignof__ (TYPE)); */
2821 orig_ndx = get_initialized_tmp_var (ndx, pre_p, NULL);
2823 if (TYPE_ALIGN (type) > BITS_PER_WORD)
2825 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_UNIT;
2827 t = build2 (PLUS_EXPR, integer_type_node, unshare_expr (orig_ndx),
2828 build_int_cst (integer_type_node, align - 1));
2829 t = build2 (BIT_AND_EXPR, integer_type_node, t,
2830 build_int_cst (integer_type_node, -align));
2831 gimplify_assign (unshare_expr (orig_ndx), t, pre_p);
2835 /* Increment __va_ndx to point past the argument:
2837 (AP).__va_ndx = orig_ndx + __va_size (TYPE); */
2839 t = fold_convert (integer_type_node, va_size);
2840 t = build2 (PLUS_EXPR, integer_type_node, orig_ndx, t);
2841 gimplify_assign (unshare_expr (ndx), t, pre_p);
2844 /* Check if the argument is in registers:
2846 if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
2847 && !must_pass_in_stack (type))
2848 __array = (AP).__va_reg; */
2850 array = create_tmp_var (ptr_type_node, NULL);
2852 lab_over = NULL;
2853 if (!targetm.calls.must_pass_in_stack (TYPE_MODE (type), type))
2855 lab_false = create_artificial_label (UNKNOWN_LOCATION);
2856 lab_over = create_artificial_label (UNKNOWN_LOCATION);
2858 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (ndx),
2859 build_int_cst (integer_type_node,
2860 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
2861 t = build3 (COND_EXPR, void_type_node, t,
2862 build1 (GOTO_EXPR, void_type_node, lab_false),
2863 NULL_TREE);
2864 gimplify_and_add (t, pre_p);
2866 gimplify_assign (unshare_expr (array), reg, pre_p);
2868 t = build1 (GOTO_EXPR, void_type_node, lab_over);
2869 gimplify_and_add (t, pre_p);
2871 t = build1 (LABEL_EXPR, void_type_node, lab_false);
2872 gimplify_and_add (t, pre_p);
2876 /* ...otherwise, the argument is on the stack (never split between
2877 registers and the stack -- change __va_ndx if necessary):
2879 else
2881 if (orig_ndx <= __MAX_ARGS_IN_REGISTERS * 4)
2882 (AP).__va_ndx = 32 + __va_size (TYPE);
2883 __array = (AP).__va_stk;
2884 } */
2886 lab_false2 = create_artificial_label (UNKNOWN_LOCATION);
2888 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (orig_ndx),
2889 build_int_cst (integer_type_node,
2890 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
2891 t = build3 (COND_EXPR, void_type_node, t,
2892 build1 (GOTO_EXPR, void_type_node, lab_false2),
2893 NULL_TREE);
2894 gimplify_and_add (t, pre_p);
2896 t = size_binop (PLUS_EXPR, unshare_expr (va_size), size_int (32));
2897 t = fold_convert (integer_type_node, t);
2898 gimplify_assign (unshare_expr (ndx), t, pre_p);
2900 t = build1 (LABEL_EXPR, void_type_node, lab_false2);
2901 gimplify_and_add (t, pre_p);
2903 gimplify_assign (array, stk, pre_p);
2905 if (lab_over)
2907 t = build1 (LABEL_EXPR, void_type_node, lab_over);
2908 gimplify_and_add (t, pre_p);
2912 /* Given the base array pointer (__array) and index to the subsequent
2913 argument (__va_ndx), find the address:
2915 __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
2916 ? sizeof (TYPE)
2917 : __va_size (TYPE))
2919 The results are endian-dependent because values smaller than one word
2920 are aligned differently. */
2923 if (BYTES_BIG_ENDIAN && TREE_CODE (type_size) == INTEGER_CST)
2925 t = fold_build2 (GE_EXPR, boolean_type_node, unshare_expr (type_size),
2926 size_int (PARM_BOUNDARY / BITS_PER_UNIT));
2927 t = fold_build3 (COND_EXPR, sizetype, t, unshare_expr (va_size),
2928 unshare_expr (type_size));
2929 size = t;
2931 else
2932 size = unshare_expr (va_size);
2934 t = fold_convert (sizetype, unshare_expr (ndx));
2935 t = build2 (MINUS_EXPR, sizetype, t, size);
2936 addr = build2 (POINTER_PLUS_EXPR, ptr_type_node, unshare_expr (array), t);
2938 addr = fold_convert (build_pointer_type (type), addr);
2939 if (indirect)
2940 addr = build_va_arg_indirect_ref (addr);
2941 return build_va_arg_indirect_ref (addr);
2945 /* Builtins. */
2947 enum xtensa_builtin
2949 XTENSA_BUILTIN_UMULSIDI3,
2950 XTENSA_BUILTIN_THREAD_POINTER,
2951 XTENSA_BUILTIN_SET_THREAD_POINTER,
2952 XTENSA_BUILTIN_max
2956 static void
2957 xtensa_init_builtins (void)
2959 tree ftype, decl;
2961 ftype = build_function_type_list (unsigned_intDI_type_node,
2962 unsigned_intSI_type_node,
2963 unsigned_intSI_type_node, NULL_TREE);
2965 decl = add_builtin_function ("__builtin_umulsidi3", ftype,
2966 XTENSA_BUILTIN_UMULSIDI3, BUILT_IN_MD,
2967 "__umulsidi3", NULL_TREE);
2968 TREE_NOTHROW (decl) = 1;
2969 TREE_READONLY (decl) = 1;
2971 if (TARGET_THREADPTR)
2973 ftype = build_function_type (ptr_type_node, void_list_node);
2974 decl = add_builtin_function ("__builtin_thread_pointer", ftype,
2975 XTENSA_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
2976 NULL, NULL_TREE);
2977 TREE_READONLY (decl) = 1;
2978 TREE_NOTHROW (decl) = 1;
2980 ftype = build_function_type_list (void_type_node, ptr_type_node,
2981 NULL_TREE);
2982 decl = add_builtin_function ("__builtin_set_thread_pointer", ftype,
2983 XTENSA_BUILTIN_SET_THREAD_POINTER,
2984 BUILT_IN_MD, NULL, NULL_TREE);
2985 TREE_NOTHROW (decl) = 1;
2990 static tree
2991 xtensa_fold_builtin (tree fndecl, tree arglist, bool ignore ATTRIBUTE_UNUSED)
2993 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
2994 tree arg0, arg1;
2996 switch (fcode)
2998 case XTENSA_BUILTIN_UMULSIDI3:
2999 arg0 = TREE_VALUE (arglist);
3000 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3001 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3002 || TARGET_MUL32_HIGH)
3003 return fold_build2 (MULT_EXPR, unsigned_intDI_type_node,
3004 fold_convert (unsigned_intDI_type_node, arg0),
3005 fold_convert (unsigned_intDI_type_node, arg1));
3006 break;
3008 case XTENSA_BUILTIN_THREAD_POINTER:
3009 case XTENSA_BUILTIN_SET_THREAD_POINTER:
3010 break;
3012 default:
3013 internal_error ("bad builtin code");
3014 break;
3017 return NULL;
3021 static rtx
3022 xtensa_expand_builtin (tree exp, rtx target,
3023 rtx subtarget ATTRIBUTE_UNUSED,
3024 enum machine_mode mode ATTRIBUTE_UNUSED,
3025 int ignore)
3027 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
3028 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3029 rtx arg;
3031 switch (fcode)
3033 case XTENSA_BUILTIN_UMULSIDI3:
3034 /* The umulsidi3 builtin is just a mechanism to avoid calling the real
3035 __umulsidi3 function when the Xtensa configuration can directly
3036 implement it. If not, just call the function. */
3037 return expand_call (exp, target, ignore);
3039 case XTENSA_BUILTIN_THREAD_POINTER:
3040 if (!target || !register_operand (target, Pmode))
3041 target = gen_reg_rtx (Pmode);
3042 emit_insn (gen_load_tp (target));
3043 return target;
3045 case XTENSA_BUILTIN_SET_THREAD_POINTER:
3046 arg = expand_normal (CALL_EXPR_ARG (exp, 0));
3047 if (!register_operand (arg, Pmode))
3048 arg = copy_to_mode_reg (Pmode, arg);
3049 emit_insn (gen_set_tp (arg));
3050 return const0_rtx;
3052 default:
3053 internal_error ("bad builtin code");
3055 return NULL_RTX;
3059 enum reg_class
3060 xtensa_preferred_reload_class (rtx x, enum reg_class rclass, int isoutput)
3062 if (!isoutput && CONSTANT_P (x) && GET_CODE (x) == CONST_DOUBLE)
3063 return NO_REGS;
3065 /* Don't use the stack pointer or hard frame pointer for reloads!
3066 The hard frame pointer would normally be OK except that it may
3067 briefly hold an incoming argument in the prologue, and reload
3068 won't know that it is live because the hard frame pointer is
3069 treated specially. */
3071 if (rclass == AR_REGS || rclass == GR_REGS)
3072 return RL_REGS;
3074 return rclass;
3078 enum reg_class
3079 xtensa_secondary_reload (bool in_p, rtx x, enum reg_class rclass,
3080 enum machine_mode mode, secondary_reload_info *sri)
3082 int regno;
3084 if (in_p && constantpool_mem_p (x))
3086 if (rclass == FP_REGS)
3087 return RL_REGS;
3089 if (mode == QImode)
3090 sri->icode = CODE_FOR_reloadqi_literal;
3091 else if (mode == HImode)
3092 sri->icode = CODE_FOR_reloadhi_literal;
3095 regno = xt_true_regnum (x);
3096 if (ACC_REG_P (regno))
3097 return ((rclass == GR_REGS || rclass == RL_REGS) ? NO_REGS : RL_REGS);
3098 if (rclass == ACC_REG)
3099 return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
3101 return NO_REGS;
3105 void
3106 order_regs_for_local_alloc (void)
3108 if (!leaf_function_p ())
3110 memcpy (reg_alloc_order, reg_nonleaf_alloc_order,
3111 FIRST_PSEUDO_REGISTER * sizeof (int));
3113 else
3115 int i, num_arg_regs;
3116 int nxt = 0;
3118 /* Use the AR registers in increasing order (skipping a0 and a1)
3119 but save the incoming argument registers for a last resort. */
3120 num_arg_regs = crtl->args.info.arg_words;
3121 if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
3122 num_arg_regs = MAX_ARGS_IN_REGISTERS;
3123 for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
3124 reg_alloc_order[nxt++] = i + num_arg_regs;
3125 for (i = 0; i < num_arg_regs; i++)
3126 reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
3128 /* List the coprocessor registers in order. */
3129 for (i = 0; i < BR_REG_NUM; i++)
3130 reg_alloc_order[nxt++] = BR_REG_FIRST + i;
3132 /* List the FP registers in order for now. */
3133 for (i = 0; i < 16; i++)
3134 reg_alloc_order[nxt++] = FP_REG_FIRST + i;
3136 /* GCC requires that we list *all* the registers.... */
3137 reg_alloc_order[nxt++] = 0; /* a0 = return address */
3138 reg_alloc_order[nxt++] = 1; /* a1 = stack pointer */
3139 reg_alloc_order[nxt++] = 16; /* pseudo frame pointer */
3140 reg_alloc_order[nxt++] = 17; /* pseudo arg pointer */
3142 reg_alloc_order[nxt++] = ACC_REG_FIRST; /* MAC16 accumulator */
3147 /* Some Xtensa targets support multiple bss sections. If the section
3148 name ends with ".bss", add SECTION_BSS to the flags. */
3150 static unsigned int
3151 xtensa_multibss_section_type_flags (tree decl, const char *name, int reloc)
3153 unsigned int flags = default_section_type_flags (decl, name, reloc);
3154 const char *suffix;
3156 suffix = strrchr (name, '.');
3157 if (suffix && strcmp (suffix, ".bss") == 0)
3159 if (!decl || (TREE_CODE (decl) == VAR_DECL
3160 && DECL_INITIAL (decl) == NULL_TREE))
3161 flags |= SECTION_BSS; /* @nobits */
3162 else
3163 warning (0, "only uninitialized variables can be placed in a "
3164 ".bss section");
3167 return flags;
3171 /* The literal pool stays with the function. */
3173 static section *
3174 xtensa_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
3175 rtx x ATTRIBUTE_UNUSED,
3176 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
3178 return function_section (current_function_decl);
3182 /* Compute a (partial) cost for rtx X. Return true if the complete
3183 cost has been computed, and false if subexpressions should be
3184 scanned. In either case, *TOTAL contains the cost result. */
3186 static bool
3187 xtensa_rtx_costs (rtx x, int code, int outer_code, int *total,
3188 bool speed ATTRIBUTE_UNUSED)
3190 switch (code)
3192 case CONST_INT:
3193 switch (outer_code)
3195 case SET:
3196 if (xtensa_simm12b (INTVAL (x)))
3198 *total = 4;
3199 return true;
3201 break;
3202 case PLUS:
3203 if (xtensa_simm8 (INTVAL (x))
3204 || xtensa_simm8x256 (INTVAL (x)))
3206 *total = 0;
3207 return true;
3209 break;
3210 case AND:
3211 if (xtensa_mask_immediate (INTVAL (x)))
3213 *total = 0;
3214 return true;
3216 break;
3217 case COMPARE:
3218 if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
3220 *total = 0;
3221 return true;
3223 break;
3224 case ASHIFT:
3225 case ASHIFTRT:
3226 case LSHIFTRT:
3227 case ROTATE:
3228 case ROTATERT:
3229 /* No way to tell if X is the 2nd operand so be conservative. */
3230 default: break;
3232 if (xtensa_simm12b (INTVAL (x)))
3233 *total = 5;
3234 else if (TARGET_CONST16)
3235 *total = COSTS_N_INSNS (2);
3236 else
3237 *total = 6;
3238 return true;
3240 case CONST:
3241 case LABEL_REF:
3242 case SYMBOL_REF:
3243 if (TARGET_CONST16)
3244 *total = COSTS_N_INSNS (2);
3245 else
3246 *total = 5;
3247 return true;
3249 case CONST_DOUBLE:
3250 if (TARGET_CONST16)
3251 *total = COSTS_N_INSNS (4);
3252 else
3253 *total = 7;
3254 return true;
3256 case MEM:
3258 int num_words =
3259 (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) ? 2 : 1;
3261 if (memory_address_p (GET_MODE (x), XEXP ((x), 0)))
3262 *total = COSTS_N_INSNS (num_words);
3263 else
3264 *total = COSTS_N_INSNS (2*num_words);
3265 return true;
3268 case FFS:
3269 case CTZ:
3270 *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
3271 return true;
3273 case CLZ:
3274 *total = COSTS_N_INSNS (TARGET_NSA ? 1 : 50);
3275 return true;
3277 case NOT:
3278 *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 3 : 2);
3279 return true;
3281 case AND:
3282 case IOR:
3283 case XOR:
3284 if (GET_MODE (x) == DImode)
3285 *total = COSTS_N_INSNS (2);
3286 else
3287 *total = COSTS_N_INSNS (1);
3288 return true;
3290 case ASHIFT:
3291 case ASHIFTRT:
3292 case LSHIFTRT:
3293 if (GET_MODE (x) == DImode)
3294 *total = COSTS_N_INSNS (50);
3295 else
3296 *total = COSTS_N_INSNS (1);
3297 return true;
3299 case ABS:
3301 enum machine_mode xmode = GET_MODE (x);
3302 if (xmode == SFmode)
3303 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3304 else if (xmode == DFmode)
3305 *total = COSTS_N_INSNS (50);
3306 else
3307 *total = COSTS_N_INSNS (4);
3308 return true;
3311 case PLUS:
3312 case MINUS:
3314 enum machine_mode xmode = GET_MODE (x);
3315 if (xmode == SFmode)
3316 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3317 else if (xmode == DFmode || xmode == DImode)
3318 *total = COSTS_N_INSNS (50);
3319 else
3320 *total = COSTS_N_INSNS (1);
3321 return true;
3324 case NEG:
3325 *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 4 : 2);
3326 return true;
3328 case MULT:
3330 enum machine_mode xmode = GET_MODE (x);
3331 if (xmode == SFmode)
3332 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
3333 else if (xmode == DFmode)
3334 *total = COSTS_N_INSNS (50);
3335 else if (xmode == DImode)
3336 *total = COSTS_N_INSNS (TARGET_MUL32_HIGH ? 10 : 50);
3337 else if (TARGET_MUL32)
3338 *total = COSTS_N_INSNS (4);
3339 else if (TARGET_MAC16)
3340 *total = COSTS_N_INSNS (16);
3341 else if (TARGET_MUL16)
3342 *total = COSTS_N_INSNS (12);
3343 else
3344 *total = COSTS_N_INSNS (50);
3345 return true;
3348 case DIV:
3349 case MOD:
3351 enum machine_mode xmode = GET_MODE (x);
3352 if (xmode == SFmode)
3354 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
3355 return true;
3357 else if (xmode == DFmode)
3359 *total = COSTS_N_INSNS (50);
3360 return true;
3363 /* Fall through. */
3365 case UDIV:
3366 case UMOD:
3368 enum machine_mode xmode = GET_MODE (x);
3369 if (xmode == DImode)
3370 *total = COSTS_N_INSNS (50);
3371 else if (TARGET_DIV32)
3372 *total = COSTS_N_INSNS (32);
3373 else
3374 *total = COSTS_N_INSNS (50);
3375 return true;
3378 case SQRT:
3379 if (GET_MODE (x) == SFmode)
3380 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
3381 else
3382 *total = COSTS_N_INSNS (50);
3383 return true;
3385 case SMIN:
3386 case UMIN:
3387 case SMAX:
3388 case UMAX:
3389 *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
3390 return true;
3392 case SIGN_EXTRACT:
3393 case SIGN_EXTEND:
3394 *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
3395 return true;
3397 case ZERO_EXTRACT:
3398 case ZERO_EXTEND:
3399 *total = COSTS_N_INSNS (1);
3400 return true;
3402 default:
3403 return false;
3407 /* Worker function for TARGET_RETURN_IN_MEMORY. */
3409 static bool
3410 xtensa_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3412 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
3413 > 4 * UNITS_PER_WORD);
3416 /* Worker function for TARGET_FUNCTION_VALUE. */
3419 xtensa_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
3420 bool outgoing)
3422 return gen_rtx_REG ((INTEGRAL_TYPE_P (valtype)
3423 && TYPE_PRECISION (valtype) < BITS_PER_WORD)
3424 ? SImode : TYPE_MODE (valtype),
3425 outgoing ? GP_OUTGOING_RETURN : GP_RETURN);
3428 /* TRAMPOLINE_TEMPLATE: For Xtensa, the trampoline must perform an ENTRY
3429 instruction with a minimal stack frame in order to get some free
3430 registers. Once the actual call target is known, the proper stack frame
3431 size is extracted from the ENTRY instruction at the target and the
3432 current frame is adjusted to match. The trampoline then transfers
3433 control to the instruction following the ENTRY at the target. Note:
3434 this assumes that the target begins with an ENTRY instruction. */
3436 void
3437 xtensa_trampoline_template (FILE *stream)
3439 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3441 fprintf (stream, "\t.begin no-transform\n");
3442 fprintf (stream, "\tentry\tsp, %d\n", MIN_FRAME_SIZE);
3444 if (use_call0)
3446 /* Save the return address. */
3447 fprintf (stream, "\tmov\ta10, a0\n");
3449 /* Use a CALL0 instruction to skip past the constants and in the
3450 process get the PC into A0. This allows PC-relative access to
3451 the constants without relying on L32R. */
3452 fprintf (stream, "\tcall0\t.Lskipconsts\n");
3454 else
3455 fprintf (stream, "\tj\t.Lskipconsts\n");
3457 fprintf (stream, "\t.align\t4\n");
3458 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
3459 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
3460 fprintf (stream, ".Lskipconsts:\n");
3462 /* Load the static chain and function address from the trampoline. */
3463 if (use_call0)
3465 fprintf (stream, "\taddi\ta0, a0, 3\n");
3466 fprintf (stream, "\tl32i\ta9, a0, 0\n");
3467 fprintf (stream, "\tl32i\ta8, a0, 4\n");
3469 else
3471 fprintf (stream, "\tl32r\ta9, .Lchainval\n");
3472 fprintf (stream, "\tl32r\ta8, .Lfnaddr\n");
3475 /* Store the static chain. */
3476 fprintf (stream, "\ts32i\ta9, sp, %d\n", MIN_FRAME_SIZE - 20);
3478 /* Set the proper stack pointer value. */
3479 fprintf (stream, "\tl32i\ta9, a8, 0\n");
3480 fprintf (stream, "\textui\ta9, a9, %d, 12\n",
3481 TARGET_BIG_ENDIAN ? 8 : 12);
3482 fprintf (stream, "\tslli\ta9, a9, 3\n");
3483 fprintf (stream, "\taddi\ta9, a9, %d\n", -MIN_FRAME_SIZE);
3484 fprintf (stream, "\tsub\ta9, sp, a9\n");
3485 fprintf (stream, "\tmovsp\tsp, a9\n");
3487 if (use_call0)
3488 /* Restore the return address. */
3489 fprintf (stream, "\tmov\ta0, a10\n");
3491 /* Jump to the instruction following the ENTRY. */
3492 fprintf (stream, "\taddi\ta8, a8, 3\n");
3493 fprintf (stream, "\tjx\ta8\n");
3495 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
3496 if (use_call0)
3497 fprintf (stream, "\t.byte\t0\n");
3498 else
3499 fprintf (stream, "\tnop\n");
3501 fprintf (stream, "\t.end no-transform\n");
3505 void
3506 xtensa_initialize_trampoline (rtx addr, rtx func, rtx chain)
3508 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
3509 int chain_off = use_call0 ? 12 : 8;
3510 int func_off = use_call0 ? 16 : 12;
3511 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (addr, chain_off)), chain);
3512 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (addr, func_off)), func);
3513 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_sync_caches"),
3514 0, VOIDmode, 1, addr, Pmode);
3518 #include "gt-xtensa.h"