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