Debian Codegraph @

gcc/config/xtensa/xtensa.cc

raw
1 /* Subroutines for insn-output.cc for Tensilica's Xtensa architecture.
2 Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 Contributed by Bob Wilson ([email protected]) 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 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "gimple.h"
31 #include "cfghooks.h"
32 #include "df.h"
33 #include "memmodel.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "attribs.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "emit-rtl.h"
40 #include "recog.h"
41 #include "diagnostic-core.h"
42 #include "cfgrtl.h"
43 #include "output.h"
44 #include "fold-const.h"
45 #include "stor-layout.h"
46 #include "calls.h"
47 #include "varasm.h"
48 #include "alias.h"
49 #include "explow.h"
50 #include "expr.h"
51 #include "reload.h"
52 #include "langhooks.h"
53 #include "gimplify.h"
54 #include "builtins.h"
55 #include "dumpfile.h"
56 #include "hw-doloop.h"
57 #include "rtl-iter.h"
58 #include "insn-attr.h"
59 #include "tree-pass.h"
60 #include "print-rtl.h"
61 #include "context.h"
62 #include "pass_manager.h"
63 #include <math.h>
64 #include "opts.h"
65
66 /* This file should be included last. */
67 #include "target-def.h"
68
69 /* Enumeration for all of the relational tests, so that we can build
70 arrays indexed by the test type, and not worry about the order
71 of EQ, NE, etc. */
72
73 enum internal_test
74 {
75 ITEST_EQ,
76 ITEST_NE,
77 ITEST_GT,
78 ITEST_GE,
79 ITEST_LT,
80 ITEST_LE,
81 ITEST_GTU,
82 ITEST_GEU,
83 ITEST_LTU,
84 ITEST_LEU,
85 ITEST_MAX
86 };
87
88 /* Array giving truth value on whether or not a given hard register
89 can support a given mode. */
90 static char xtensa_hard_regno_mode_ok_p
91 [(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
92
93 /* Largest block move to handle in-line. */
94 #define LARGEST_MOVE_RATIO 15
95
96 /* Define the structure for the machine field in struct function. */
97 struct GTY(()) machine_function
98 {
99 int accesses_prev_frame;
100 bool need_a7_copy;
101 bool vararg_a7;
102 rtx vararg_a7_copy;
103 rtx_insn *set_frame_ptr_insn;
104 /* Current frame size calculated by compute_frame_size. */
105 unsigned current_frame_size;
106 /* Callee-save area size in the current frame calculated by
107 compute_frame_size. */
108 int callee_save_size;
109 bool frame_laid_out;
110 bool inhibit_logues_a1_adjusts;
111 rtx last_logues_a9_content;
112 HARD_REG_SET eliminated_callee_saved;
113 hash_map<rtx, int> *litpool_usage;
114 };
115
116 static void xtensa_option_override (void);
117 static void xtensa_option_override_after_change (void);
118 static enum internal_test map_test_to_internal_test (enum rtx_code);
119 static rtx gen_int_relational (enum rtx_code, rtx, rtx);
120 static rtx gen_float_relational (enum rtx_code, rtx, rtx);
121 static rtx gen_conditional_move (enum rtx_code, machine_mode, rtx, rtx);
122 static struct machine_function * xtensa_init_machine_status (void);
123 static rtx xtensa_legitimize_tls_address (rtx);
124 static rtx xtensa_legitimize_address (rtx, rtx, machine_mode);
125 static bool xtensa_mode_dependent_address_p (const_rtx, addr_space_t);
126 static bool xtensa_return_in_msb (const_tree);
127 static void printx (FILE *, signed int);
128 static rtx xtensa_builtin_saveregs (void);
129 static bool xtensa_legitimate_address_p (machine_mode, rtx, bool,
130 code_helper = ERROR_MARK);
131 static unsigned int xtensa_multibss_section_type_flags (tree, const char *,
132 int) ATTRIBUTE_UNUSED;
133 static section *xtensa_select_rtx_section (machine_mode, rtx,
134 unsigned HOST_WIDE_INT);
135 static bool xtensa_rtx_costs (rtx, machine_mode, int, int, int *, bool);
136 static int xtensa_insn_cost (rtx_insn *, bool);
137 static int xtensa_register_move_cost (machine_mode, reg_class_t,
138 reg_class_t);
139 static tree xtensa_build_builtin_va_list (void);
140 static bool xtensa_return_in_memory (const_tree, const_tree);
141 static tree xtensa_gimplify_va_arg_expr (tree, tree, gimple_seq *,
142 gimple_seq *);
143 static void xtensa_function_arg_advance (cumulative_args_t,
144 const function_arg_info &);
145 static rtx xtensa_function_arg (cumulative_args_t, const function_arg_info &);
146 static rtx xtensa_function_incoming_arg (cumulative_args_t,
147 const function_arg_info &);
148 static rtx xtensa_function_value (const_tree, const_tree, bool);
149 static rtx xtensa_libcall_value (machine_mode, const_rtx);
150 static bool xtensa_function_value_regno_p (const unsigned int);
151 static unsigned int xtensa_function_arg_boundary (machine_mode,
152 const_tree);
153 static void xtensa_init_builtins (void);
154 static tree xtensa_fold_builtin (tree, int, tree *, bool);
155 static rtx xtensa_expand_builtin (tree, rtx, rtx, machine_mode, int);
156 static void xtensa_va_start (tree, rtx);
157 static bool xtensa_frame_pointer_required (void);
158 static rtx xtensa_static_chain (const_tree, bool);
159 static void xtensa_asm_trampoline_template (FILE *);
160 static void xtensa_trampoline_init (rtx, tree, rtx);
161 static bool xtensa_output_addr_const_extra (FILE *, rtx);
162 static bool xtensa_cannot_force_const_mem (machine_mode, rtx);
163
164 static reg_class_t xtensa_preferred_reload_class (rtx, reg_class_t);
165 static reg_class_t xtensa_preferred_output_reload_class (rtx, reg_class_t);
166 static reg_class_t xtensa_secondary_reload (bool, rtx, reg_class_t,
167 machine_mode,
168 struct secondary_reload_info *);
169
170 static bool constantpool_address_p (const_rtx addr);
171 static bool xtensa_legitimate_constant_p (machine_mode, rtx);
172 static void xtensa_reorg (void);
173 static bool xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
174 unsigned int, bool);
175 static const char *xtensa_invalid_within_doloop (const rtx_insn *);
176
177 static bool xtensa_member_type_forces_blk (const_tree,
178 machine_mode mode);
179
180 static void xtensa_conditional_register_usage (void);
181 static unsigned int xtensa_hard_regno_nregs (unsigned int, machine_mode);
182 static bool xtensa_hard_regno_mode_ok (unsigned int, machine_mode);
183 static bool xtensa_modes_tieable_p (machine_mode, machine_mode);
184 static HOST_WIDE_INT xtensa_constant_alignment (const_tree, HOST_WIDE_INT);
185 static bool xtensa_can_eliminate (const int from ATTRIBUTE_UNUSED,
186 const int to);
187 static HOST_WIDE_INT xtensa_starting_frame_offset (void);
188 static unsigned HOST_WIDE_INT xtensa_asan_shadow_offset (void);
189 static bool xtensa_function_ok_for_sibcall (tree, tree);
190 static bool xtensa_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
191 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
192 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
193 const_tree function ATTRIBUTE_UNUSED);
194 static void xtensa_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
195 HOST_WIDE_INT delta,
196 HOST_WIDE_INT vcall_offset,
197 tree function);
198
199 static rtx xtensa_delegitimize_address (rtx);
200
201
202
203 /* These hooks specify assembly directives for creating certain kinds
204 of integer object. */
205
206 #undef TARGET_ASM_ALIGNED_SI_OP
207 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
208
209 #undef TARGET_ASM_SELECT_RTX_SECTION
210 #define TARGET_ASM_SELECT_RTX_SECTION xtensa_select_rtx_section
211
212 #undef TARGET_LEGITIMIZE_ADDRESS
213 #define TARGET_LEGITIMIZE_ADDRESS xtensa_legitimize_address
214 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
215 #define TARGET_MODE_DEPENDENT_ADDRESS_P xtensa_mode_dependent_address_p
216
217 #undef TARGET_REGISTER_MOVE_COST
218 #define TARGET_REGISTER_MOVE_COST xtensa_register_move_cost
219 #undef TARGET_RTX_COSTS
220 #define TARGET_RTX_COSTS xtensa_rtx_costs
221 #undef TARGET_INSN_COST
222 #define TARGET_INSN_COST xtensa_insn_cost
223 #undef TARGET_ADDRESS_COST
224 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
225
226 #undef TARGET_MEMBER_TYPE_FORCES_BLK
227 #define TARGET_MEMBER_TYPE_FORCES_BLK xtensa_member_type_forces_blk
228
229 #undef TARGET_BUILD_BUILTIN_VA_LIST
230 #define TARGET_BUILD_BUILTIN_VA_LIST xtensa_build_builtin_va_list
231
232 #undef TARGET_EXPAND_BUILTIN_VA_START
233 #define TARGET_EXPAND_BUILTIN_VA_START xtensa_va_start
234
235 #undef TARGET_PROMOTE_FUNCTION_MODE
236 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
237 #undef TARGET_PROMOTE_PROTOTYPES
238 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
239
240 #undef TARGET_RETURN_IN_MEMORY
241 #define TARGET_RETURN_IN_MEMORY xtensa_return_in_memory
242 #undef TARGET_FUNCTION_VALUE
243 #define TARGET_FUNCTION_VALUE xtensa_function_value
244 #undef TARGET_LIBCALL_VALUE
245 #define TARGET_LIBCALL_VALUE xtensa_libcall_value
246 #undef TARGET_FUNCTION_VALUE_REGNO_P
247 #define TARGET_FUNCTION_VALUE_REGNO_P xtensa_function_value_regno_p
248
249 #undef TARGET_SPLIT_COMPLEX_ARG
250 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
251 #undef TARGET_MUST_PASS_IN_STACK
252 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
253 #undef TARGET_FUNCTION_ARG_ADVANCE
254 #define TARGET_FUNCTION_ARG_ADVANCE xtensa_function_arg_advance
255 #undef TARGET_FUNCTION_ARG
256 #define TARGET_FUNCTION_ARG xtensa_function_arg
257 #undef TARGET_FUNCTION_INCOMING_ARG
258 #define TARGET_FUNCTION_INCOMING_ARG xtensa_function_incoming_arg
259 #undef TARGET_FUNCTION_ARG_BOUNDARY
260 #define TARGET_FUNCTION_ARG_BOUNDARY xtensa_function_arg_boundary
261
262 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
263 #define TARGET_EXPAND_BUILTIN_SAVEREGS xtensa_builtin_saveregs
264 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
265 #define TARGET_GIMPLIFY_VA_ARG_EXPR xtensa_gimplify_va_arg_expr
266
267 #undef TARGET_RETURN_IN_MSB
268 #define TARGET_RETURN_IN_MSB xtensa_return_in_msb
269
270 #undef TARGET_INIT_BUILTINS
271 #define TARGET_INIT_BUILTINS xtensa_init_builtins
272 #undef TARGET_FOLD_BUILTIN
273 #define TARGET_FOLD_BUILTIN xtensa_fold_builtin
274 #undef TARGET_EXPAND_BUILTIN
275 #define TARGET_EXPAND_BUILTIN xtensa_expand_builtin
276
277 #undef TARGET_PREFERRED_RELOAD_CLASS
278 #define TARGET_PREFERRED_RELOAD_CLASS xtensa_preferred_reload_class
279 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
280 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS xtensa_preferred_output_reload_class
281
282 #undef TARGET_SECONDARY_RELOAD
283 #define TARGET_SECONDARY_RELOAD xtensa_secondary_reload
284
285 #undef TARGET_HAVE_TLS
286 #define TARGET_HAVE_TLS HAVE_AS_TLS
287
288 #undef TARGET_CANNOT_FORCE_CONST_MEM
289 #define TARGET_CANNOT_FORCE_CONST_MEM xtensa_cannot_force_const_mem
290
291 #undef TARGET_LEGITIMATE_ADDRESS_P
292 #define TARGET_LEGITIMATE_ADDRESS_P xtensa_legitimate_address_p
293
294 #undef TARGET_FRAME_POINTER_REQUIRED
295 #define TARGET_FRAME_POINTER_REQUIRED xtensa_frame_pointer_required
296
297 #undef TARGET_STATIC_CHAIN
298 #define TARGET_STATIC_CHAIN xtensa_static_chain
299 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
300 #define TARGET_ASM_TRAMPOLINE_TEMPLATE xtensa_asm_trampoline_template
301 #undef TARGET_TRAMPOLINE_INIT
302 #define TARGET_TRAMPOLINE_INIT xtensa_trampoline_init
303
304 #undef TARGET_OPTION_OVERRIDE
305 #define TARGET_OPTION_OVERRIDE xtensa_option_override
306
307 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
308 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE xtensa_option_override_after_change
309
310 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
311 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA xtensa_output_addr_const_extra
312
313 #undef TARGET_LEGITIMATE_CONSTANT_P
314 #define TARGET_LEGITIMATE_CONSTANT_P xtensa_legitimate_constant_p
315
316 #undef TARGET_MACHINE_DEPENDENT_REORG
317 #define TARGET_MACHINE_DEPENDENT_REORG xtensa_reorg
318
319 #undef TARGET_CAN_USE_DOLOOP_P
320 #define TARGET_CAN_USE_DOLOOP_P xtensa_can_use_doloop_p
321
322 #undef TARGET_INVALID_WITHIN_DOLOOP
323 #define TARGET_INVALID_WITHIN_DOLOOP xtensa_invalid_within_doloop
324
325 #undef TARGET_CONDITIONAL_REGISTER_USAGE
326 #define TARGET_CONDITIONAL_REGISTER_USAGE xtensa_conditional_register_usage
327
328 #undef TARGET_HARD_REGNO_NREGS
329 #define TARGET_HARD_REGNO_NREGS xtensa_hard_regno_nregs
330 #undef TARGET_HARD_REGNO_MODE_OK
331 #define TARGET_HARD_REGNO_MODE_OK xtensa_hard_regno_mode_ok
332
333 #undef TARGET_MODES_TIEABLE_P
334 #define TARGET_MODES_TIEABLE_P xtensa_modes_tieable_p
335
336 #undef TARGET_CONSTANT_ALIGNMENT
337 #define TARGET_CONSTANT_ALIGNMENT xtensa_constant_alignment
338
339 #undef TARGET_CAN_ELIMINATE
340 #define TARGET_CAN_ELIMINATE xtensa_can_eliminate
341
342 #undef TARGET_STARTING_FRAME_OFFSET
343 #define TARGET_STARTING_FRAME_OFFSET xtensa_starting_frame_offset
344
345 #undef TARGET_ASAN_SHADOW_OFFSET
346 #define TARGET_ASAN_SHADOW_OFFSET xtensa_asan_shadow_offset
347
348 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
349 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
350
351 #undef TARGET_DELEGITIMIZE_ADDRESS
352 #define TARGET_DELEGITIMIZE_ADDRESS xtensa_delegitimize_address
353
354 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
355 #define TARGET_FUNCTION_OK_FOR_SIBCALL xtensa_function_ok_for_sibcall
356
357 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
358 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK xtensa_can_output_mi_thunk
359
360 #undef TARGET_ASM_OUTPUT_MI_THUNK
361 #define TARGET_ASM_OUTPUT_MI_THUNK xtensa_output_mi_thunk
362
363 #undef TARGET_MAX_ANCHOR_OFFSET
364 #define TARGET_MAX_ANCHOR_OFFSET 1020
365
366 #undef TARGET_DIFFERENT_ADDR_DISPLACEMENT_P
367 #define TARGET_DIFFERENT_ADDR_DISPLACEMENT_P hook_bool_void_true
368
369 struct gcc_target targetm = TARGET_INITIALIZER;
370
371
372 /* Functions to test Xtensa immediate operand validity. */
373
374 bool
375 xtensa_simm8 (HOST_WIDE_INT v)
376 {
377 return IN_RANGE (v, -128, 127);
378 }
379
380
381 bool
382 xtensa_simm8x256 (HOST_WIDE_INT v)
383 {
384 return (v & 255) == 0 && IN_RANGE (v, -32768, 32512);
385 }
386
387
388 bool
389 xtensa_simm12b (HOST_WIDE_INT v)
390 {
391 return IN_RANGE (v, -2048, 2047);
392 }
393
394
395 static bool
396 xtensa_uimm8 (HOST_WIDE_INT v)
397 {
398 return IN_RANGE (v, 0, 255);
399 }
400
401
402 static bool
403 xtensa_uimm8x2 (HOST_WIDE_INT v)
404 {
405 return (v & 1) == 0 && IN_RANGE (v, 0, 510);
406 }
407
408
409 static bool
410 xtensa_uimm8x4 (HOST_WIDE_INT v)
411 {
412 return (v & 3) == 0 && IN_RANGE (v, 0, 1020);
413 }
414
415
416 static bool
417 xtensa_b4const (HOST_WIDE_INT v)
418 {
419 switch (v)
420 {
421 case -1:
422 case 1:
423 case 2:
424 case 3:
425 case 4:
426 case 5:
427 case 6:
428 case 7:
429 case 8:
430 case 10:
431 case 12:
432 case 16:
433 case 32:
434 case 64:
435 case 128:
436 case 256:
437 return true;
438 }
439 return false;
440 }
441
442
443 bool
444 xtensa_b4const_or_zero (HOST_WIDE_INT v)
445 {
446 if (v == 0)
447 return true;
448 return xtensa_b4const (v);
449 }
450
451
452 bool
453 xtensa_b4constu (HOST_WIDE_INT v)
454 {
455 switch (v)
456 {
457 case 32768:
458 case 65536:
459 case 2:
460 case 3:
461 case 4:
462 case 5:
463 case 6:
464 case 7:
465 case 8:
466 case 10:
467 case 12:
468 case 16:
469 case 32:
470 case 64:
471 case 128:
472 case 256:
473 return true;
474 }
475 return false;
476 }
477
478
479 bool
480 xtensa_m1_or_1_thru_15 (HOST_WIDE_INT v)
481 {
482 return v == -1 || IN_RANGE (v, 1, 15);
483 }
484
485
486 bool
487 xtensa_mask_immediate (HOST_WIDE_INT v)
488 {
489 return IN_RANGE (exact_log2 (v + 1), 1, 16);
490 }
491
492
493 /* This is just like the standard true_regnum() function except that it
494 works even when reg_renumber is not initialized. */
495
496 int
497 xt_true_regnum (rtx x)
498 {
499 if (REG_P (x))
500 {
501 if (! HARD_REGISTER_P (x)
502 && reg_renumber
503 && (lra_in_progress || reg_renumber[REGNO (x)] >= 0))
504 return reg_renumber[REGNO (x)];
505 return REGNO (x);
506 }
507 if (SUBREG_P (x))
508 {
509 int base = xt_true_regnum (SUBREG_REG (x));
510
511 if (base >= 0
512 && HARD_REGISTER_NUM_P (base))
513 {
514 struct subreg_info info;
515
516 subreg_get_info (lra_in_progress
517 ? (unsigned) base : REGNO (SUBREG_REG (x)),
518 GET_MODE (SUBREG_REG (x)),
519 SUBREG_BYTE (x), GET_MODE (x), &info);
520 if (info.representable_p)
521 return base + info.offset;
522 }
523 }
524 return -1;
525 }
526
527
528 int
529 xtensa_valid_move (machine_mode mode, rtx *operands)
530 {
531 /* Either the destination or source must be a register, and the
532 MAC16 accumulator doesn't count. */
533
534 if (register_operand (operands[0], mode))
535 {
536 int dst_regnum = xt_true_regnum (operands[0]);
537
538 if (xtensa_tls_referenced_p (operands[1]))
539 return FALSE;
540
541 /* The stack pointer can only be assigned with a MOVSP opcode. */
542 if (dst_regnum == STACK_POINTER_REGNUM)
543 return !TARGET_WINDOWED_ABI
544 || (mode == SImode
545 && register_operand (operands[1], mode)
546 && !ACC_REG_P (xt_true_regnum (operands[1])));
547
548 if (!ACC_REG_P (dst_regnum))
549 return true;
550 }
551 if (register_operand (operands[1], mode))
552 {
553 int src_regnum = xt_true_regnum (operands[1]);
554 if (!ACC_REG_P (src_regnum))
555 return true;
556 }
557 return FALSE;
558 }
559
560
561 int
562 smalloffset_mem_p (rtx op)
563 {
564 if (MEM_P (op))
565 {
566 rtx addr = XEXP (op, 0);
567 if (REG_P (addr))
568 return BASE_REG_P (addr, 0);
569 if (GET_CODE (addr) == PLUS)
570 {
571 rtx offset = XEXP (addr, 0);
572 HOST_WIDE_INT val;
573 if (! CONST_INT_P (offset))
574 offset = XEXP (addr, 1);
575 if (! CONST_INT_P (offset))
576 return FALSE;
577
578 val = INTVAL (offset);
579 return (val & 3) == 0 && IN_RANGE (val, 0, 60);
580 }
581 }
582 return FALSE;
583 }
584
585
586 static bool
587 constantpool_address_p (const_rtx addr)
588 {
589 const_rtx sym = addr;
590
591 if (GET_CODE (addr) == CONST)
592 {
593 rtx offset;
594
595 /* Only handle (PLUS (SYM, OFFSET)) form. */
596 addr = XEXP (addr, 0);
597 if (GET_CODE (addr) != PLUS)
598 return false;
599
600 /* Make sure the address is word aligned. */
601 offset = XEXP (addr, 1);
602 if ((!CONST_INT_P (offset))
603 || ((INTVAL (offset) & 3) != 0))
604 return false;
605
606 sym = XEXP (addr, 0);
607 }
608
609 if (SYMBOL_REF_P (sym)
610 && CONSTANT_POOL_ADDRESS_P (sym))
611 return true;
612 return false;
613 }
614
615
616 int
617 constantpool_mem_p (rtx op)
618 {
619 if (SUBREG_P (op))
620 op = SUBREG_REG (op);
621 if (MEM_P (op))
622 return constantpool_address_p (XEXP (op, 0));
623 return FALSE;
624 }
625
626
627 /* Return TRUE if X is a thread-local symbol. */
628
629 static bool
630 xtensa_tls_symbol_p (rtx x)
631 {
632 if (! targetm.have_tls)
633 return false;
634
635 return SYMBOL_REF_P (x) && SYMBOL_REF_TLS_MODEL (x) != 0;
636 }
637
638
639 void
640 xtensa_extend_reg (rtx dst, rtx src)
641 {
642 rtx temp = gen_reg_rtx (SImode);
643 rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
644
645 /* Generate paradoxical subregs as needed so that the modes match. */
646 src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
647 dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
648
649 emit_insn (gen_ashlsi3 (temp, src, shift));
650 emit_insn (gen_ashrsi3 (dst, temp, shift));
651 }
652
653
654 bool
655 xtensa_mem_offset (unsigned v, machine_mode mode)
656 {
657 switch (mode)
658 {
659 case E_BLKmode:
660 /* Handle the worst case for block moves. See xtensa_expand_block_move
661 where we emit an optimized block move operation if the block can be
662 moved in < "move_ratio" pieces. The worst case is when the block is
663 aligned but has a size of (3 mod 4) (does this happen?) so that the
664 last piece requires a byte load/store. */
665 return (xtensa_uimm8 (v)
666 && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
667
668 case E_QImode:
669 return xtensa_uimm8 (v);
670
671 case E_HImode:
672 return xtensa_uimm8x2 (v);
673
674 case E_DImode:
675 case E_DFmode:
676 return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
677
678 default:
679 break;
680 }
681
682 return xtensa_uimm8x4 (v);
683 }
684
685
686 /* Make normal rtx_code into something we can index from an array. */
687
688 static enum internal_test
689 map_test_to_internal_test (enum rtx_code test_code)
690 {
691 enum internal_test test = ITEST_MAX;
692
693 switch (test_code)
694 {
695 default: break;
696 case EQ: test = ITEST_EQ; break;
697 case NE: test = ITEST_NE; break;
698 case GT: test = ITEST_GT; break;
699 case GE: test = ITEST_GE; break;
700 case LT: test = ITEST_LT; break;
701 case LE: test = ITEST_LE; break;
702 case GTU: test = ITEST_GTU; break;
703 case GEU: test = ITEST_GEU; break;
704 case LTU: test = ITEST_LTU; break;
705 case LEU: test = ITEST_LEU; break;
706 }
707
708 return test;
709 }
710
711
712 /* Generate the code to compare two integer values. The return value is
713 the comparison expression. */
714
715 static rtx
716 gen_int_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
717 rtx cmp0, /* first operand to compare */
718 rtx cmp1 /* second operand to compare */)
719 {
720 struct cmp_info
721 {
722 enum rtx_code test_code; /* test code to use in insn */
723 bool (*const_range_p) (HOST_WIDE_INT); /* range check function */
724 int const_add; /* constant to add (convert LE -> LT) */
725 int reverse_regs; /* reverse registers in test */
726 int invert_const; /* != 0 if invert value if cmp1 is constant */
727 int invert_reg; /* != 0 if invert value if cmp1 is register */
728 int unsignedp; /* != 0 for unsigned comparisons. */
729 };
730
731 static struct cmp_info info[ (int)ITEST_MAX ] = {
732
733 { EQ, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* EQ */
734 { NE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* NE */
735
736 { LT, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* GT */
737 { GE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* GE */
738 { LT, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* LT */
739 { GE, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* LE */
740
741 { LTU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* GTU */
742 { GEU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* GEU */
743 { LTU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* LTU */
744 { GEU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* LEU */
745 };
746
747 enum internal_test test;
748 machine_mode mode;
749 struct cmp_info *p_info;
750 int invert;
751
752 test = map_test_to_internal_test (test_code);
753 gcc_assert (test != ITEST_MAX);
754
755 p_info = &info[ (int)test ];
756
757 mode = GET_MODE (cmp0);
758 if (mode == VOIDmode)
759 mode = GET_MODE (cmp1);
760
761 /* Make sure we can handle any constants given to us. */
762 if (CONST_INT_P (cmp1))
763 {
764 HOST_WIDE_INT value = INTVAL (cmp1);
765 unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
766
767 /* if the immediate overflows or does not fit in the immediate field,
768 spill it to a register */
769
770 if ((p_info->unsignedp ?
771 (uvalue + p_info->const_add > uvalue) :
772 (value + p_info->const_add > value)) != (p_info->const_add > 0))
773 {
774 cmp1 = force_reg (mode, cmp1);
775 }
776 else if (!(p_info->const_range_p) (value + p_info->const_add))
777 {
778 cmp1 = force_reg (mode, cmp1);
779 }
780 }
781 else if (! REG_P (cmp1) && ! SUBREG_P (cmp1))
782 {
783 cmp1 = force_reg (mode, cmp1);
784 }
785
786 /* See if we need to invert the result. */
787 invert = (CONST_INT_P (cmp1)
788 ? p_info->invert_const
789 : p_info->invert_reg);
790
791 /* Comparison to constants, may involve adding 1 to change a LT into LE.
792 Comparison between two registers, may involve switching operands. */
793 if (CONST_INT_P (cmp1))
794 {
795 if (p_info->const_add != 0)
796 cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
797
798 }
799 else if (p_info->reverse_regs)
800 std::swap (cmp0, cmp1);
801
802 return gen_rtx_fmt_ee (invert ? reverse_condition (p_info->test_code)
803 : p_info->test_code,
804 VOIDmode, cmp0, cmp1);
805 }
806
807
808 /* Generate the code to compare two float values. The return value is
809 the comparison expression. */
810
811 static rtx
812 gen_float_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
813 rtx cmp0, /* first operand to compare */
814 rtx cmp1 /* second operand to compare */)
815 {
816 rtx (*gen_fn) (rtx, rtx, rtx);
817 rtx brtmp;
818 int reverse_regs, invert;
819
820 switch (test_code)
821 {
822 case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
823 case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
824 case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
825 case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
826 case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
827 case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
828 case UNEQ: reverse_regs = 0; invert = 0; gen_fn = gen_suneq_sf; break;
829 case LTGT: reverse_regs = 0; invert = 1; gen_fn = gen_suneq_sf; break;
830 case UNLE: reverse_regs = 0; invert = 0; gen_fn = gen_sunle_sf; break;
831 case UNGT: reverse_regs = 1; invert = 0; gen_fn = gen_sunlt_sf; break;
832 case UNLT: reverse_regs = 0; invert = 0; gen_fn = gen_sunlt_sf; break;
833 case UNGE: reverse_regs = 1; invert = 0; gen_fn = gen_sunle_sf; break;
834 case UNORDERED:
835 reverse_regs = 0; invert = 0; gen_fn = gen_sunordered_sf; break;
836 case ORDERED:
837 reverse_regs = 0; invert = 1; gen_fn = gen_sunordered_sf; break;
838 default:
839 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
840 reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
841 }
842
843 if (reverse_regs)
844 std::swap (cmp0, cmp1);
845
846 brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
847 emit_insn (gen_fn (brtmp, cmp0, cmp1));
848
849 return gen_rtx_fmt_ee (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
850 }
851
852
853 void
854 xtensa_expand_conditional_branch (rtx *operands, machine_mode mode)
855 {
856 enum rtx_code test_code = GET_CODE (operands[0]);
857 rtx cmp0 = operands[1];
858 rtx cmp1 = operands[2];
859 rtx cmp, label;
860
861 switch (mode)
862 {
863 case E_SFmode:
864 if (TARGET_HARD_FLOAT)
865 {
866 cmp = gen_float_relational (test_code, cmp0, cmp1);
867 break;
868 }
869 /* FALLTHRU */
870
871 case E_DFmode:
872 default:
873 fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
874
875 case E_SImode:
876 cmp = gen_int_relational (test_code, cmp0, cmp1);
877 break;
878 }
879
880 /* Generate the branch. */
881 label = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
882 emit_jump_insn (gen_rtx_SET (pc_rtx,
883 gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
884 label,
885 pc_rtx)));
886 }
887
888
889 static rtx
890 gen_conditional_move (enum rtx_code code, machine_mode mode,
891 rtx op0, rtx op1)
892 {
893 if (mode == SImode)
894 {
895 rtx cmp;
896
897 /* Jump optimization calls get_condition() which canonicalizes
898 comparisons like (GE x <const>) to (GT x <const-1>).
899 Transform those comparisons back to GE, since that is the
900 comparison supported in Xtensa. We shouldn't have to
901 transform <LE x const> comparisons, because neither
902 xtensa_expand_conditional_branch() nor get_condition() will
903 produce them. */
904
905 if ((code == GT) && (op1 == constm1_rtx))
906 {
907 code = GE;
908 op1 = const0_rtx;
909 }
910 cmp = gen_rtx_fmt_ee (code, VOIDmode, pc_rtx, const0_rtx);
911
912 if (boolean_operator (cmp, VOIDmode))
913 {
914 /* Swap the operands to make const0 second. */
915 if (op0 == const0_rtx)
916 {
917 op0 = op1;
918 op1 = const0_rtx;
919 }
920
921 /* If not comparing against zero, emit a comparison (subtract). */
922 if (op1 != const0_rtx)
923 {
924 op0 = expand_binop (SImode, sub_optab, op0, op1,
925 0, 0, OPTAB_LIB_WIDEN);
926 op1 = const0_rtx;
927 }
928 }
929 else if (branch_operator (cmp, VOIDmode))
930 {
931 /* Swap the operands to make const0 second. */
932 if (op0 == const0_rtx)
933 {
934 op0 = op1;
935 op1 = const0_rtx;
936
937 switch (code)
938 {
939 case LT: code = GE; break;
940 case GE: code = LT; break;
941 default: gcc_unreachable ();
942 }
943 }
944
945 if (op1 != const0_rtx)
946 return 0;
947 }
948 else
949 return 0;
950
951 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
952 }
953
954 if (TARGET_HARD_FLOAT && mode == SFmode)
955 return gen_float_relational (code, op0, op1);
956
957 return 0;
958 }
959
960
961 int
962 xtensa_expand_conditional_move (rtx *operands, int isflt)
963 {
964 rtx dest = operands[0];
965 rtx cmp = operands[1];
966 machine_mode cmp_mode = GET_MODE (XEXP (cmp, 0));
967 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
968
969 if (!(cmp = gen_conditional_move (GET_CODE (cmp), cmp_mode,
970 XEXP (cmp, 0), XEXP (cmp, 1))))
971 return 0;
972
973 if (isflt)
974 gen_fn = (cmp_mode == SImode
975 ? gen_movsfcc_internal0
976 : gen_movsfcc_internal1);
977 else
978 gen_fn = (cmp_mode == SImode
979 ? gen_movsicc_internal0
980 : gen_movsicc_internal1);
981
982 emit_insn (gen_fn (dest, XEXP (cmp, 0), operands[2], operands[3], cmp));
983 return 1;
984 }
985
986
987 int
988 xtensa_expand_scc (rtx operands[4], machine_mode cmp_mode)
989 {
990 rtx dest = operands[0];
991 rtx cmp;
992 rtx one_tmp, zero_tmp;
993 rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
994
995 if (cmp_mode == SImode && TARGET_SALT)
996 {
997 rtx a = operands[2], b = force_reg (SImode, operands[3]);
998 enum rtx_code code = GET_CODE (operands[1]);
999 bool invert_res = false;
1000
1001 switch (code)
1002 {
1003 case GE:
1004 case GEU:
1005 invert_res = true;
1006 break;
1007 case GT:
1008 case GTU:
1009 std::swap (a, b);
1010 break;
1011 case LE:
1012 case LEU:
1013 invert_res = true;
1014 std::swap (a, b);
1015 break;
1016 default:
1017 break;
1018 }
1019
1020 switch (code)
1021 {
1022 case GE:
1023 case GT:
1024 case LE:
1025 case LT:
1026 emit_insn (gen_salt (dest, a, b));
1027 if (!invert_res)
1028 return 1;
1029 break;
1030 case GEU:
1031 case GTU:
1032 case LEU:
1033 case LTU:
1034 emit_insn (gen_saltu (dest, a, b));
1035 if (!invert_res)
1036 return 1;
1037 break;
1038 default:
1039 break;
1040 }
1041
1042 if (invert_res)
1043 {
1044 emit_insn (gen_negsi2 (dest, dest));
1045 emit_insn (gen_addsi3 (dest, dest, const1_rtx));
1046 return 1;
1047 }
1048 }
1049
1050 if (! (cmp = gen_conditional_move (GET_CODE (operands[1]), cmp_mode,
1051 operands[2], operands[3])))
1052 return 0;
1053
1054 one_tmp = force_reg (SImode, const1_rtx);
1055 zero_tmp = force_reg (SImode, const0_rtx);
1056
1057 gen_fn = (cmp_mode == SImode
1058 ? gen_movsicc_internal0
1059 : gen_movsicc_internal1);
1060 emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
1061 return 1;
1062 }
1063
1064
1065 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1]. MODE is
1066 for the output, i.e., the input operands are twice as big as MODE. */
1067
1068 void
1069 xtensa_split_operand_pair (rtx operands[4], machine_mode mode)
1070 {
1071 switch (GET_CODE (operands[1]))
1072 {
1073 case REG:
1074 operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
1075 operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
1076 break;
1077
1078 case MEM:
1079 operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
1080 operands[2] = adjust_address (operands[1], mode, 0);
1081 break;
1082
1083 case CONST_INT:
1084 case CONST_DOUBLE:
1085 split_double (operands[1], &operands[2], &operands[3]);
1086 break;
1087
1088 default:
1089 gcc_unreachable ();
1090 }
1091
1092 switch (GET_CODE (operands[0]))
1093 {
1094 case REG:
1095 operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
1096 operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
1097 break;
1098
1099 case MEM:
1100 operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
1101 operands[0] = adjust_address (operands[0], mode, 0);
1102 break;
1103
1104 default:
1105 gcc_unreachable ();
1106 }
1107 }
1108
1109
1110 /* Try to emit insns to load src (either naked or pooled SI/SF constant)
1111 into dst with synthesizing a such constant value from a sequence of
1112 load-immediate / arithmetic ones, instead of a L32R instruction
1113 (plus a constant in litpool). */
1114
1115 static int
1116 xtensa_constantsynth_2insn (rtx dst, HOST_WIDE_INT srcval,
1117 rtx (*gen_op)(rtx, HOST_WIDE_INT),
1118 HOST_WIDE_INT op_imm)
1119 {
1120 HOST_WIDE_INT imm = INT_MAX;
1121 rtx x = NULL_RTX;
1122 int shift, sqr;
1123
1124 gcc_assert (REG_P (dst));
1125
1126 shift = exact_log2 (srcval + 1);
1127 if (IN_RANGE (shift, 1, 31))
1128 {
1129 imm = -1;
1130 x = gen_lshrsi3 (dst, dst, GEN_INT (32 - shift));
1131 }
1132
1133 shift = ctz_hwi (srcval);
1134 if ((!x || (TARGET_DENSITY && ! IN_RANGE (imm, -32, 95)))
1135 && xtensa_simm12b (srcval >> shift))
1136 {
1137 imm = srcval >> shift;
1138 x = gen_ashlsi3 (dst, dst, GEN_INT (shift));
1139 }
1140
1141 if ((!x || (TARGET_DENSITY && ! IN_RANGE (imm, -32, 95)))
1142 && IN_RANGE (srcval, (-2048 - 32768), (2047 + 32512)))
1143 {
1144 HOST_WIDE_INT imm0, imm1;
1145
1146 if (srcval < -32768)
1147 imm1 = -32768;
1148 else if (srcval > 32512)
1149 imm1 = 32512;
1150 else
1151 imm1 = srcval & ~255;
1152 imm0 = srcval - imm1;
1153 if (TARGET_DENSITY && imm1 < 32512 && IN_RANGE (imm0, 224, 255))
1154 imm0 -= 256, imm1 += 256;
1155 imm = imm0;
1156 x = gen_addsi3 (dst, dst, GEN_INT (imm1));
1157 }
1158
1159 sqr = (int) floorf (sqrtf (srcval));
1160 if (TARGET_MUL32 && optimize_size
1161 && !x && IN_RANGE (srcval, 0, (2047 * 2047)) && sqr * sqr == srcval)
1162 {
1163 imm = sqr;
1164 x = gen_mulsi3 (dst, dst, dst);
1165 }
1166
1167 if (!x)
1168 return 0;
1169
1170 emit_move_insn (dst, GEN_INT (imm));
1171 emit_insn (x);
1172 if (gen_op)
1173 emit_move_insn (dst, gen_op (dst, op_imm));
1174
1175 return 1;
1176 }
1177
1178 static rtx
1179 xtensa_constantsynth_rtx_SLLI (rtx reg, HOST_WIDE_INT imm)
1180 {
1181 return gen_rtx_ASHIFT (SImode, reg, GEN_INT (imm));
1182 }
1183
1184 static rtx
1185 xtensa_constantsynth_rtx_ADDSUBX (rtx reg, HOST_WIDE_INT imm)
1186 {
1187 return imm == 7
1188 ? gen_rtx_MINUS (SImode, gen_rtx_ASHIFT (SImode, reg, GEN_INT (3)),
1189 reg)
1190 : gen_rtx_PLUS (SImode, gen_rtx_ASHIFT (SImode, reg,
1191 GEN_INT (floor_log2 (imm - 1))),
1192 reg);
1193 }
1194
1195 int
1196 xtensa_constantsynth (rtx dst, rtx src)
1197 {
1198 HOST_WIDE_INT srcval;
1199 static opt_pass *pass_rtl_split2;
1200 int *pv;
1201
1202 /* Derefer if src is litpool entry, and get integer constant value. */
1203 src = avoid_constant_pool_reference (src);
1204 if (CONST_INT_P (src))
1205 srcval = INTVAL (src);
1206 else if (CONST_DOUBLE_P (src) && GET_MODE (src) == SFmode)
1207 {
1208 long l;
1209
1210 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (src), l);
1211 srcval = (int32_t)l, src = GEN_INT (srcval);
1212 }
1213 else
1214 return 0;
1215
1216 /* Force dst as SImode. */
1217 gcc_assert (REG_P (dst));
1218 if (GET_MODE (dst) != SImode)
1219 dst = gen_rtx_REG (SImode, REGNO (dst));
1220
1221 if (optimize_size)
1222 {
1223 /* During the first split pass after register allocation (rtl-split2),
1224 record the occurrence of integer src value and do nothing. */
1225 if (!pass_rtl_split2)
1226 pass_rtl_split2 = g->get_passes ()->get_pass_by_name ("rtl-split2");
1227 if (current_pass == pass_rtl_split2)
1228 {
1229 if (!cfun->machine->litpool_usage)
1230 cfun->machine->litpool_usage = hash_map<rtx, int>::create_ggc ();
1231 if ((pv = cfun->machine->litpool_usage->get (src)))
1232 ++*pv;
1233 else
1234 cfun->machine->litpool_usage->put (src, 1);
1235 return 0;
1236 }
1237
1238 /* If two or more identical integer constants appear in the function,
1239 the code size can be reduced by re-emitting a "move" (load from an
1240 either litpool entry or relaxed immediate) instruction in SImode
1241 to increase the chances that the litpool entry will be shared. */
1242 if (cfun->machine->litpool_usage
1243 && (pv = cfun->machine->litpool_usage->get (src))
1244 && *pv > 1)
1245 {
1246 emit_move_insn (dst, src);
1247 return 1;
1248 }
1249 }
1250
1251 /* No need for synthesizing for what fits into MOVI instruction. */
1252 if (xtensa_simm12b (srcval))
1253 {
1254 emit_move_insn (dst, src);
1255 return 1;
1256 }
1257
1258 /* 2-insns substitution. */
1259 if ((optimize_size || (optimize && xtensa_extra_l32r_costs >= 1))
1260 && xtensa_constantsynth_2insn (dst, srcval, NULL, 0))
1261 return 1;
1262
1263 /* 3-insns substitution. */
1264 if (optimize > 1 && !optimize_size && xtensa_extra_l32r_costs >= 2)
1265 {
1266 int shift, divisor;
1267
1268 /* 2-insns substitution followed by SLLI. */
1269 shift = ctz_hwi (srcval);
1270 if (IN_RANGE (shift, 1, 31) &&
1271 xtensa_constantsynth_2insn (dst, srcval >> shift,
1272 xtensa_constantsynth_rtx_SLLI,
1273 shift))
1274 return 1;
1275
1276 /* 2-insns substitution followed by ADDX[248] or SUBX8. */
1277 if (TARGET_ADDX)
1278 for (divisor = 3; divisor <= 9; divisor += 2)
1279 if (srcval % divisor == 0 &&
1280 xtensa_constantsynth_2insn (dst, srcval / divisor,
1281 xtensa_constantsynth_rtx_ADDSUBX,
1282 divisor))
1283 return 1;
1284
1285 /* loading simm12 followed by left/right bitwise rotation:
1286 MOVI + SSAI + SRC. */
1287 if ((srcval & 0x001FF800) == 0
1288 || (srcval & 0x001FF800) == 0x001FF800)
1289 {
1290 int32_t v;
1291
1292 for (shift = 1; shift < 12; ++shift)
1293 {
1294 v = (int32_t)(((uint32_t)srcval >> shift)
1295 | ((uint32_t)srcval << (32 - shift)));
1296 if (xtensa_simm12b(v))
1297 {
1298 emit_move_insn (dst, GEN_INT (v));
1299 emit_insn (gen_rotlsi3 (dst, dst, GEN_INT (shift)));
1300 return 1;
1301 }
1302 v = (int32_t)(((uint32_t)srcval << shift)
1303 | ((uint32_t)srcval >> (32 - shift)));
1304 if (xtensa_simm12b(v))
1305 {
1306 emit_move_insn (dst, GEN_INT (v));
1307 emit_insn (gen_rotrsi3 (dst, dst, GEN_INT (shift)));
1308 return 1;
1309 }
1310 }
1311 }
1312 }
1313
1314 /* If cannot synthesize the value and also cannot fit into MOVI instruc-
1315 tion, re-emit a "move" (load from an either litpool entry or relaxed
1316 immediate) instruction in SImode in order to increase the chances that
1317 the litpool entry will be shared. */
1318 emit_move_insn (dst, src);
1319 return 1;
1320 }
1321
1322
1323 /* Emit insns to move operands[1] into operands[0].
1324 Return 1 if we have written out everything that needs to be done to
1325 do the move. Otherwise, return 0 and the caller will emit the move
1326 normally. */
1327
1328 int
1329 xtensa_emit_move_sequence (rtx *operands, machine_mode mode)
1330 {
1331 rtx src = operands[1];
1332
1333 if (CONSTANT_P (src)
1334 && (! CONST_INT_P (src) || ! xtensa_simm12b (INTVAL (src))))
1335 {
1336 rtx dst = operands[0];
1337
1338 if (xtensa_tls_referenced_p (src))
1339 {
1340 rtx addend = NULL;
1341
1342 if (GET_CODE (src) == CONST && GET_CODE (XEXP (src, 0)) == PLUS)
1343 {
1344 addend = XEXP (XEXP (src, 0), 1);
1345 src = XEXP (XEXP (src, 0), 0);
1346 }
1347
1348 src = xtensa_legitimize_tls_address (src);
1349 if (addend)
1350 {
1351 src = gen_rtx_PLUS (mode, src, addend);
1352 src = force_operand (src, dst);
1353 }
1354 emit_move_insn (dst, src);
1355 return 1;
1356 }
1357
1358 if (! TARGET_AUTO_LITPOOLS && ! TARGET_CONST16
1359 && ! (CONST_INT_P (src) && can_create_pseudo_p ()))
1360 {
1361 src = force_const_mem (SImode, src);
1362 operands[1] = src;
1363 }
1364
1365 /* PC-relative loads are always SImode, and CONST16 is only
1366 supported in the movsi pattern, so add a SUBREG for any other
1367 (smaller) mode. */
1368
1369 if (mode != SImode)
1370 {
1371 if (register_operand (dst, mode))
1372 {
1373 emit_move_insn (simplify_gen_subreg (SImode, dst, mode, 0), src);
1374 return 1;
1375 }
1376 else
1377 {
1378 src = force_reg (SImode, src);
1379 src = gen_lowpart_SUBREG (mode, src);
1380 operands[1] = src;
1381 }
1382 }
1383 }
1384
1385 if (can_create_pseudo_p ()
1386 && !xtensa_valid_move (mode, operands))
1387 operands[1] = force_reg (mode, operands[1]);
1388
1389 operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1390
1391 return 0;
1392 }
1393
1394
1395 /* Check if an incoming argument in a7 is expected to be used soon and
1396 if OPND is a register or register pair that includes a7. If so,
1397 create a new pseudo and copy a7 into that pseudo at the very
1398 beginning of the function, followed by the special "set_frame_ptr"
1399 unspec_volatile insn. The return value is either the original
1400 operand, if it is not a7, or the new pseudo containing a copy of
1401 the incoming argument. This is necessary because the register
1402 allocator will ignore conflicts with a7 and may either assign some
1403 other pseudo to a7 or use a7 as the hard_frame_pointer, clobbering
1404 the incoming argument in a7. By copying the argument out of a7 as
1405 the very first thing, and then immediately following that with an
1406 unspec_volatile to keep the scheduler away, we should avoid any
1407 problems. Putting the set_frame_ptr insn at the beginning, with
1408 only the a7 copy before it, also makes it easier for the prologue
1409 expander to initialize the frame pointer after the a7 copy and to
1410 fix up the a7 copy to use the stack pointer instead of the frame
1411 pointer. */
1412
1413 rtx
1414 xtensa_copy_incoming_a7 (rtx opnd)
1415 {
1416 rtx entry_insns = 0;
1417 rtx reg, tmp;
1418 machine_mode mode;
1419
1420 if (!cfun->machine->need_a7_copy)
1421 return opnd;
1422
1423 /* This function should never be called again once a7 has been copied. */
1424 gcc_assert (!cfun->machine->set_frame_ptr_insn);
1425
1426 mode = GET_MODE (opnd);
1427
1428 /* The operand using a7 may come in a later instruction, so just return
1429 the original operand if it doesn't use a7. */
1430 reg = opnd;
1431 if (SUBREG_P (reg))
1432 {
1433 gcc_assert (SUBREG_BYTE (reg) == 0);
1434 reg = SUBREG_REG (reg);
1435 }
1436 if (! REG_P (reg)
1437 || REGNO (reg) > A7_REG
1438 || REGNO (reg) + hard_regno_nregs (A7_REG, mode) <= A7_REG)
1439 return opnd;
1440
1441 /* 1-word args will always be in a7; 2-word args in a6/a7. */
1442 gcc_assert (REGNO (reg) + hard_regno_nregs (A7_REG, mode) - 1 == A7_REG);
1443
1444 cfun->machine->need_a7_copy = false;
1445
1446 /* Copy a7 to a new pseudo at the function entry. Use gen_raw_REG to
1447 create the REG for a7 so that hard_frame_pointer_rtx is not used. */
1448
1449 start_sequence ();
1450 tmp = gen_reg_rtx (mode);
1451
1452 switch (mode)
1453 {
1454 case E_DFmode:
1455 case E_DImode:
1456 /* Copy the value out of A7 here but keep the first word in A6 until
1457 after the set_frame_ptr insn. Otherwise, the register allocator
1458 may decide to put "subreg (tmp, 0)" in A7 and clobber the incoming
1459 value. */
1460 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 4),
1461 gen_raw_REG (SImode, A7_REG)));
1462 break;
1463 case E_SFmode:
1464 emit_insn (gen_movsf_internal (tmp, gen_raw_REG (mode, A7_REG)));
1465 break;
1466 case E_SImode:
1467 emit_insn (gen_movsi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1468 break;
1469 case E_HImode:
1470 emit_insn (gen_movhi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1471 break;
1472 case E_QImode:
1473 emit_insn (gen_movqi_internal (tmp, gen_raw_REG (mode, A7_REG)));
1474 break;
1475 default:
1476 gcc_unreachable ();
1477 }
1478
1479 cfun->machine->set_frame_ptr_insn = emit_insn (gen_set_frame_ptr ());
1480
1481 /* For DF and DI mode arguments, copy the incoming value in A6 now. */
1482 if (mode == DFmode || mode == DImode)
1483 emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 0),
1484 gen_rtx_REG (SImode, A7_REG - 1)));
1485 entry_insns = get_insns ();
1486 end_sequence ();
1487
1488 if (cfun->machine->vararg_a7)
1489 {
1490 /* This is called from within builtin_saveregs, which will insert the
1491 saveregs code at the function entry, ahead of anything placed at
1492 the function entry now. Instead, save the sequence to be inserted
1493 at the beginning of the saveregs code. */
1494 cfun->machine->vararg_a7_copy = entry_insns;
1495 }
1496 else
1497 {
1498 /* Put entry_insns after the NOTE that starts the function. If
1499 this is inside a start_sequence, make the outer-level insn
1500 chain current, so the code is placed at the start of the
1501 function. */
1502 push_topmost_sequence ();
1503 /* Do not use entry_of_function() here. This is called from within
1504 expand_function_start, when the CFG still holds GIMPLE. */
1505 emit_insn_after (entry_insns, get_insns ());
1506 pop_topmost_sequence ();
1507 }
1508
1509 return tmp;
1510 }
1511
1512
1513 /* Try to expand a block move operation to a sequence of RTL move
1514 instructions. If not optimizing, or if the block size is not a
1515 constant, or if the block is too large, the expansion fails and GCC
1516 falls back to calling memcpy().
1517
1518 operands[0] is the destination
1519 operands[1] is the source
1520 operands[2] is the length
1521 operands[3] is the alignment */
1522
1523 int
1524 xtensa_expand_block_move (rtx *operands)
1525 {
1526 static const machine_mode mode_from_align[] =
1527 {
1528 VOIDmode, QImode, HImode, VOIDmode, SImode,
1529 };
1530
1531 rtx dst_mem = operands[0];
1532 rtx src_mem = operands[1];
1533 HOST_WIDE_INT bytes, align;
1534 int num_pieces, move_ratio;
1535 rtx temp[2];
1536 machine_mode mode[2];
1537 int amount[2];
1538 bool active[2];
1539 int phase = 0;
1540 int next;
1541 int offset_ld = 0;
1542 int offset_st = 0;
1543 rtx x;
1544
1545 /* If this is not a fixed size move, just call memcpy. */
1546 if (!optimize || ! CONST_INT_P (operands[2]))
1547 return 0;
1548
1549 bytes = INTVAL (operands[2]);
1550 align = INTVAL (operands[3]);
1551
1552 /* Anything to move? */
1553 if (bytes <= 0)
1554 return 0;
1555
1556 if (align > MOVE_MAX)
1557 align = MOVE_MAX;
1558
1559 /* Decide whether to expand inline based on the optimization level. */
1560 move_ratio = 4;
1561 if (optimize > 2)
1562 move_ratio = LARGEST_MOVE_RATIO;
1563 num_pieces = (bytes / align) + ((bytes % align + 1) / 2);
1564 if (num_pieces > move_ratio)
1565 return 0;
1566
1567 x = XEXP (dst_mem, 0);
1568 if (!REG_P (x))
1569 {
1570 x = force_reg (Pmode, x);
1571 dst_mem = replace_equiv_address (dst_mem, x);
1572 }
1573
1574 x = XEXP (src_mem, 0);
1575 if (!REG_P (x))
1576 {
1577 x = force_reg (Pmode, x);
1578 src_mem = replace_equiv_address (src_mem, x);
1579 }
1580
1581 active[0] = active[1] = false;
1582
1583 do
1584 {
1585 next = phase;
1586 phase ^= 1;
1587
1588 if (bytes > 0)
1589 {
1590 int next_amount;
1591
1592 next_amount = (bytes >= 4 ? 4 : (bytes >= 2 ? 2 : 1));
1593 next_amount = MIN (next_amount, align);
1594
1595 amount[next] = next_amount;
1596 mode[next] = mode_from_align[next_amount];
1597 temp[next] = gen_reg_rtx (mode[next]);
1598
1599 x = adjust_address (src_mem, mode[next], offset_ld);
1600 emit_move_insn (temp[next], x);
1601
1602 offset_ld += next_amount;
1603 bytes -= next_amount;
1604 active[next] = true;
1605 }
1606
1607 if (active[phase])
1608 {
1609 active[phase] = false;
1610
1611 x = adjust_address (dst_mem, mode[phase], offset_st);
1612 emit_move_insn (x, temp[phase]);
1613
1614 offset_st += amount[phase];
1615 }
1616 }
1617 while (active[next]);
1618
1619 return 1;
1620 }
1621
1622
1623 /* Worker function for xtensa_expand_block_set().
1624
1625 Expand into an insn sequence that calls the "memset" function. */
1626
1627 static rtx_insn *
1628 xtensa_expand_block_set_libcall (rtx dst_mem,
1629 HOST_WIDE_INT value,
1630 HOST_WIDE_INT bytes)
1631 {
1632 rtx reg;
1633 rtx_insn *seq;
1634
1635 start_sequence ();
1636
1637 reg = XEXP (dst_mem, 0);
1638 if (! REG_P (reg))
1639 reg = XEXP (replace_equiv_address (dst_mem,
1640 force_reg (Pmode, reg)), 0);
1641 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "memset"),
1642 LCT_NORMAL, VOIDmode,
1643 reg, SImode,
1644 GEN_INT (value), SImode,
1645 GEN_INT (bytes), SImode);
1646
1647 seq = get_insns ();
1648 end_sequence ();
1649
1650 return seq;
1651 }
1652
1653 /* Worker function for xtensa_expand_block_set().
1654
1655 Expand into an insn sequence of one constant load followed by multiple
1656 memory stores. Returns NULL if the conditions for expansion are not
1657 met. */
1658
1659 static rtx_insn *
1660 xtensa_expand_block_set_unrolled_loop (rtx dst_mem,
1661 HOST_WIDE_INT value,
1662 HOST_WIDE_INT bytes,
1663 HOST_WIDE_INT align)
1664 {
1665 rtx reg;
1666 int offset;
1667 rtx_insn *seq;
1668
1669 if (bytes > 64)
1670 return NULL;
1671
1672 start_sequence ();
1673
1674 reg = XEXP (dst_mem, 0);
1675 if (! REG_P (reg))
1676 dst_mem = replace_equiv_address (dst_mem,
1677 force_reg (Pmode, reg));
1678 switch (align)
1679 {
1680 case 1:
1681 break;
1682 case 2:
1683 value = (int16_t)((uint8_t)value * 0x0101U);
1684 break;
1685 case 4:
1686 value = (int32_t)((uint8_t)value * 0x01010101U);
1687 break;
1688 default:
1689 gcc_unreachable ();
1690 }
1691 reg = force_reg (SImode, GEN_INT (value));
1692
1693 offset = 0;
1694 do
1695 {
1696 int unit_size = MIN (bytes, align);
1697 machine_mode unit_mode = (unit_size >= 4 ? SImode :
1698 (unit_size >= 2 ? HImode : QImode));
1699
1700 unit_size = GET_MODE_SIZE (unit_mode);
1701 emit_move_insn (adjust_address (dst_mem, unit_mode, offset),
1702 (unit_mode == SImode) ? reg
1703 : convert_to_mode (unit_mode, reg, true));
1704 offset += unit_size;
1705 bytes -= unit_size;
1706 }
1707 while (bytes > 0);
1708
1709 seq = get_insns ();
1710 end_sequence ();
1711
1712 return seq;
1713 }
1714
1715 /* Worker function for xtensa_expand_block_set(),
1716
1717 Expand into an insn sequence of a small loop that fill the memory
1718 range. Returns NULL if the conditions for expansion are not met. */
1719
1720 static rtx_insn *
1721 xtensa_expand_block_set_small_loop (rtx dst_mem,
1722 HOST_WIDE_INT value,
1723 HOST_WIDE_INT bytes,
1724 HOST_WIDE_INT align)
1725 {
1726 HOST_WIDE_INT count;
1727 rtx reg, dst, end;
1728 machine_mode unit_mode;
1729 rtx_code_label *label;
1730 rtx_insn *seq;
1731
1732 /* Totally-aligned block only. */
1733 if (bytes % align != 0)
1734 return NULL;
1735 count = bytes / align;
1736
1737 /* If the Loop Option (zero-overhead looping) is configured and active,
1738 almost no restrictions about the length of the block. */
1739 if (! (TARGET_LOOPS && optimize))
1740 {
1741 /* If 4-byte aligned, small loop substitution is almost optimal,
1742 thus limited to only offset to the end address for ADDI/ADDMI
1743 instruction. */
1744 if (align == 4
1745 && ! (bytes <= 127 || xtensa_simm8x256 (bytes)))
1746 return NULL;
1747
1748 /* If no 4-byte aligned, loop count should be treated as the
1749 constraint. */
1750 if (align != 4
1751 && count > ((optimize > 1 && !optimize_size) ? 8 : 15))
1752 return NULL;
1753 }
1754
1755 start_sequence ();
1756
1757 reg = XEXP (dst_mem, 0);
1758 if (REG_P (reg))
1759 emit_move_insn (dst = gen_reg_rtx (SImode), reg);
1760 else
1761 dst = XEXP (replace_equiv_address (dst_mem,
1762 force_reg (Pmode, reg)), 0);
1763 emit_insn (gen_addsi3 (end = gen_reg_rtx (SImode),
1764 dst,
1765 (TARGET_LOOPS && optimize)
1766 ? force_reg (SImode, GEN_INT (bytes))
1767 : GEN_INT (bytes)));
1768 switch (align)
1769 {
1770 case 1:
1771 unit_mode = QImode;
1772 break;
1773 case 2:
1774 value = (int16_t)((uint8_t)value * 0x0101U);
1775 unit_mode = HImode;
1776 break;
1777 case 4:
1778 value = (int32_t)((uint8_t)value * 0x01010101U);
1779 unit_mode = SImode;
1780 break;
1781 default:
1782 gcc_unreachable ();
1783 }
1784 reg = force_reg (unit_mode, GEN_INT (value));
1785
1786 emit_label (label = gen_label_rtx ());
1787 emit_move_insn (gen_rtx_MEM (unit_mode, dst), reg);
1788 emit_insn (gen_addsi3 (dst, dst, GEN_INT (align)));
1789 emit_cmp_and_jump_insns (dst, end, NE, const0_rtx, SImode, true, label);
1790
1791 seq = get_insns ();
1792 end_sequence ();
1793
1794 return seq;
1795 }
1796
1797
1798 /* Try to expand a block set operation to a sequence of RTL move
1799 instructions. If not optimizing, or if the block size is not a
1800 constant, or if the block is too large, or if the value to
1801 initialize the block with is not a constant, the expansion
1802 fails and GCC falls back to calling memset().
1803
1804 operands[0] is the destination
1805 operands[1] is the length
1806 operands[2] is the initialization value
1807 operands[3] is the alignment */
1808
1809 int
1810 xtensa_expand_block_set (rtx *operands)
1811 {
1812 rtx dst_mem = operands[0];
1813 HOST_WIDE_INT bytes, value, align;
1814 rtx_insn *seq[3];
1815 int min_cost, min_index, i, n, cost;
1816 rtx_insn *insn;
1817
1818 if (! CONST_INT_P (operands[1])
1819 || ! CONST_INT_P (operands[2])
1820 || (bytes = INTVAL (operands[1])) <= 0)
1821 return 0;
1822
1823 value = (int8_t)INTVAL (operands[2]);
1824 align = INTVAL (operands[3]);
1825 if (align > MOVE_MAX)
1826 align = MOVE_MAX;
1827
1828 /* Try to generate three equivalent insn sequences but method and
1829 size. */
1830 seq[0] = xtensa_expand_block_set_libcall (dst_mem, value, bytes);
1831 seq[1] = xtensa_expand_block_set_unrolled_loop (dst_mem, value,
1832 bytes, align);
1833 seq[2] = xtensa_expand_block_set_small_loop (dst_mem, value,
1834 bytes, align);
1835
1836 /* Find the sequence that has minimum size-basis insn costs. */
1837 if (dump_file)
1838 fprintf (dump_file, "xtensa_expand_block_set:\n");
1839 min_cost = INT_MAX, min_index = 0;
1840 for (i = 0; i < 3; ++i)
1841 if ((insn = seq[i]))
1842 {
1843 if (dump_file)
1844 fprintf (dump_file, " method %d...\n", i);
1845
1846 for (n = 0, cost = 0; insn; insn = NEXT_INSN (insn))
1847 {
1848 if (active_insn_p (insn))
1849 ++n, cost += xtensa_insn_cost (insn, false);
1850 if (dump_file)
1851 dump_insn_slim (dump_file, insn);
1852 }
1853
1854 /* Apply expansion bonus if -O2 or -O3 by discounting the cost
1855 other than libcall. */
1856 if (i > 0)
1857 {
1858 if (optimize == 2 && !optimize_size)
1859 cost = (cost + 1) / 2;
1860 else if (optimize >= 3)
1861 cost = (cost + 2) / 4;
1862 }
1863
1864 if (dump_file)
1865 fprintf (dump_file, "\t%d active insns, %d cost.\n", n, cost);
1866
1867 if (cost < min_cost)
1868 min_cost = cost, min_index = i;
1869 }
1870 if (dump_file)
1871 fprintf (dump_file, " choose method %d.\n", min_index);
1872
1873 /* Fall back if libcall is minimum. */
1874 if (min_index == 0)
1875 return 0;
1876
1877 emit_insn (seq[min_index]);
1878
1879 return 1;
1880 }
1881
1882
1883 void
1884 xtensa_expand_nonlocal_goto (rtx *operands)
1885 {
1886 rtx goto_handler = operands[1];
1887 rtx containing_fp = operands[3];
1888
1889 /* Generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1890 is too big to generate in-line. */
1891
1892 if (! REG_P (containing_fp))
1893 containing_fp = force_reg (Pmode, containing_fp);
1894
1895 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1896 LCT_NORMAL, VOIDmode,
1897 containing_fp, Pmode,
1898 goto_handler, Pmode);
1899 }
1900
1901
1902 static struct machine_function *
1903 xtensa_init_machine_status (void)
1904 {
1905 return ggc_cleared_alloc<machine_function> ();
1906 }
1907
1908
1909 /* Shift VAL of mode MODE left by COUNT bits. */
1910
1911 static inline rtx
1912 xtensa_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
1913 {
1914 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
1915 NULL_RTX, 1, OPTAB_DIRECT);
1916 return expand_simple_binop (SImode, ASHIFT, val, count,
1917 NULL_RTX, 1, OPTAB_DIRECT);
1918 }
1919
1920
1921 /* Structure to hold the initial parameters for a compare_and_swap operation
1922 in HImode and QImode. */
1923
1924 struct alignment_context
1925 {
1926 rtx memsi; /* SI aligned memory location. */
1927 rtx shift; /* Bit offset with regard to lsb. */
1928 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
1929 rtx modemaski; /* ~modemask */
1930 };
1931
1932
1933 /* Initialize structure AC for word access to HI and QI mode memory. */
1934
1935 static void
1936 init_alignment_context (struct alignment_context *ac, rtx mem)
1937 {
1938 machine_mode mode = GET_MODE (mem);
1939 rtx byteoffset = NULL_RTX;
1940 bool aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
1941
1942 if (aligned)
1943 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
1944 else
1945 {
1946 /* Alignment is unknown. */
1947 rtx addr, align;
1948
1949 /* Force the address into a register. */
1950 addr = force_reg (Pmode, XEXP (mem, 0));
1951
1952 /* Align it to SImode. */
1953 align = expand_simple_binop (Pmode, AND, addr,
1954 GEN_INT (-GET_MODE_SIZE (SImode)),
1955 NULL_RTX, 1, OPTAB_DIRECT);
1956 /* Generate MEM. */
1957 ac->memsi = gen_rtx_MEM (SImode, align);
1958 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
1959 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
1960 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
1961
1962 byteoffset = expand_simple_binop (Pmode, AND, addr,
1963 GEN_INT (GET_MODE_SIZE (SImode) - 1),
1964 NULL_RTX, 1, OPTAB_DIRECT);
1965 }
1966
1967 /* Calculate shiftcount. */
1968 if (TARGET_BIG_ENDIAN)
1969 {
1970 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
1971 if (!aligned)
1972 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
1973 NULL_RTX, 1, OPTAB_DIRECT);
1974 }
1975 else
1976 {
1977 if (aligned)
1978 ac->shift = NULL_RTX;
1979 else
1980 ac->shift = byteoffset;
1981 }
1982
1983 if (ac->shift != NULL_RTX)
1984 {
1985 /* Shift is the byte count, but we need the bitcount. */
1986 gcc_assert (exact_log2 (BITS_PER_UNIT) >= 0);
1987 ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift,
1988 GEN_INT (exact_log2 (BITS_PER_UNIT)),
1989 NULL_RTX, 1, OPTAB_DIRECT);
1990 ac->modemask = expand_simple_binop (SImode, ASHIFT,
1991 GEN_INT (GET_MODE_MASK (mode)),
1992 ac->shift,
1993 NULL_RTX, 1, OPTAB_DIRECT);
1994 }
1995 else
1996 ac->modemask = GEN_INT (GET_MODE_MASK (mode));
1997
1998 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask, NULL_RTX, 1);
1999 }
2000
2001
2002 /* Expand an atomic compare and swap operation for HImode and QImode.
2003 MEM is the memory location, CMP the old value to compare MEM with
2004 and NEW_RTX the value to set if CMP == MEM. */
2005
2006 void
2007 xtensa_expand_compare_and_swap (rtx target, rtx mem, rtx cmp, rtx new_rtx)
2008 {
2009 machine_mode mode = GET_MODE (mem);
2010 struct alignment_context ac;
2011 rtx tmp, cmpv, newv, val;
2012 rtx oldval = gen_reg_rtx (SImode);
2013 rtx res = gen_reg_rtx (SImode);
2014 rtx_code_label *csloop = gen_label_rtx ();
2015 rtx_code_label *csend = gen_label_rtx ();
2016
2017 init_alignment_context (&ac, mem);
2018
2019 if (ac.shift != NULL_RTX)
2020 {
2021 cmp = xtensa_expand_mask_and_shift (cmp, mode, ac.shift);
2022 new_rtx = xtensa_expand_mask_and_shift (new_rtx, mode, ac.shift);
2023 }
2024
2025 /* Load the surrounding word into VAL with the MEM value masked out. */
2026 val = force_reg (SImode, expand_simple_binop (SImode, AND, ac.memsi,
2027 ac.modemaski, NULL_RTX, 1,
2028 OPTAB_DIRECT));
2029 emit_label (csloop);
2030
2031 /* Patch CMP and NEW_RTX into VAL at correct position. */
2032 cmpv = force_reg (SImode, expand_simple_binop (SImode, IOR, cmp, val,
2033 NULL_RTX, 1, OPTAB_DIRECT));
2034 newv = force_reg (SImode, expand_simple_binop (SImode, IOR, new_rtx, val,
2035 NULL_RTX, 1, OPTAB_DIRECT));
2036
2037 /* Jump to end if we're done. */
2038 emit_insn (gen_sync_compare_and_swapsi (res, ac.memsi, cmpv, newv));
2039 emit_cmp_and_jump_insns (res, cmpv, EQ, const0_rtx, SImode, true, csend);
2040
2041 /* Check for changes outside mode. */
2042 emit_move_insn (oldval, val);
2043 tmp = expand_simple_binop (SImode, AND, res, ac.modemaski,
2044 val, 1, OPTAB_DIRECT);
2045 if (tmp != val)
2046 emit_move_insn (val, tmp);
2047
2048 /* Loop internal if so. */
2049 emit_cmp_and_jump_insns (oldval, val, NE, const0_rtx, SImode, true, csloop);
2050
2051 emit_label (csend);
2052
2053 /* Return the correct part of the bitfield. */
2054 convert_move (target,
2055 (ac.shift == NULL_RTX ? res
2056 : expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
2057 NULL_RTX, 1, OPTAB_DIRECT)),
2058 1);
2059 }
2060
2061
2062 /* Expand an atomic operation CODE of mode MODE (either HImode or QImode --
2063 the default expansion works fine for SImode). MEM is the memory location
2064 and VAL the value to play with. If AFTER is true then store the value
2065 MEM holds after the operation, if AFTER is false then store the value MEM
2066 holds before the operation. If TARGET is zero then discard that value, else
2067 store it to TARGET. */
2068
2069 void
2070 xtensa_expand_atomic (enum rtx_code code, rtx target, rtx mem, rtx val,
2071 bool after)
2072 {
2073 machine_mode mode = GET_MODE (mem);
2074 struct alignment_context ac;
2075 rtx_code_label *csloop = gen_label_rtx ();
2076 rtx cmp, tmp;
2077 rtx old = gen_reg_rtx (SImode);
2078 rtx new_rtx = gen_reg_rtx (SImode);
2079 rtx orig = NULL_RTX;
2080
2081 init_alignment_context (&ac, mem);
2082
2083 /* Prepare values before the compare-and-swap loop. */
2084 if (ac.shift != NULL_RTX)
2085 val = xtensa_expand_mask_and_shift (val, mode, ac.shift);
2086 switch (code)
2087 {
2088 case PLUS:
2089 case MINUS:
2090 orig = gen_reg_rtx (SImode);
2091 convert_move (orig, val, 1);
2092 break;
2093
2094 case SET:
2095 case IOR:
2096 case XOR:
2097 break;
2098
2099 case MULT: /* NAND */
2100 case AND:
2101 /* val = "11..1<val>11..1" */
2102 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
2103 NULL_RTX, 1, OPTAB_DIRECT);
2104 break;
2105
2106 default:
2107 gcc_unreachable ();
2108 }
2109
2110 /* Load full word. Subsequent loads are performed by S32C1I. */
2111 cmp = force_reg (SImode, ac.memsi);
2112
2113 emit_label (csloop);
2114 emit_move_insn (old, cmp);
2115
2116 switch (code)
2117 {
2118 case PLUS:
2119 case MINUS:
2120 val = expand_simple_binop (SImode, code, old, orig,
2121 NULL_RTX, 1, OPTAB_DIRECT);
2122 val = expand_simple_binop (SImode, AND, val, ac.modemask,
2123 NULL_RTX, 1, OPTAB_DIRECT);
2124 /* FALLTHRU */
2125 case SET:
2126 tmp = expand_simple_binop (SImode, AND, old, ac.modemaski,
2127 NULL_RTX, 1, OPTAB_DIRECT);
2128 tmp = expand_simple_binop (SImode, IOR, tmp, val,
2129 new_rtx, 1, OPTAB_DIRECT);
2130 break;
2131
2132 case AND:
2133 case IOR:
2134 case XOR:
2135 tmp = expand_simple_binop (SImode, code, old, val,
2136 new_rtx, 1, OPTAB_DIRECT);
2137 break;
2138
2139 case MULT: /* NAND */
2140 tmp = expand_simple_binop (SImode, AND, old, val,
2141 NULL_RTX, 1, OPTAB_DIRECT);
2142 tmp = expand_simple_binop (SImode, XOR, tmp, ac.modemask,
2143 new_rtx, 1, OPTAB_DIRECT);
2144 break;
2145
2146 default:
2147 gcc_unreachable ();
2148 }
2149
2150 if (tmp != new_rtx)
2151 emit_move_insn (new_rtx, tmp);
2152 emit_insn (gen_sync_compare_and_swapsi (cmp, ac.memsi, old, new_rtx));
2153 emit_cmp_and_jump_insns (cmp, old, NE, const0_rtx, SImode, true, csloop);
2154
2155 if (target)
2156 {
2157 tmp = (after ? new_rtx : cmp);
2158 convert_move (target,
2159 (ac.shift == NULL_RTX ? tmp
2160 : expand_simple_binop (SImode, LSHIFTRT, tmp, ac.shift,
2161 NULL_RTX, 1, OPTAB_DIRECT)),
2162 1);
2163 }
2164 }
2165
2166
2167 void
2168 xtensa_setup_frame_addresses (void)
2169 {
2170 /* Set flag to cause TARGET_FRAME_POINTER_REQUIRED to return true. */
2171 cfun->machine->accesses_prev_frame = 1;
2172
2173 if (TARGET_WINDOWED_ABI)
2174 emit_library_call
2175 (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
2176 LCT_NORMAL, VOIDmode);
2177 }
2178
2179
2180 /* Emit the assembly for the end of a zero-cost loop. Normally we just emit
2181 a comment showing where the end of the loop is. However, if there is a
2182 label or a branch at the end of the loop then we need to place a nop
2183 there. If the loop ends with a label we need the nop so that branches
2184 targeting that label will target the nop (and thus remain in the loop),
2185 instead of targeting the instruction after the loop (and thus exiting
2186 the loop). If the loop ends with a branch, we need the nop in case the
2187 branch is targeting a location inside the loop. When the branch
2188 executes it will cause the loop count to be decremented even if it is
2189 taken (because it is the last instruction in the loop), so we need to
2190 nop after the branch to prevent the loop count from being decremented
2191 when the branch is taken. */
2192
2193 void
2194 xtensa_emit_loop_end (rtx_insn *insn, rtx *operands)
2195 {
2196 char done = 0;
2197
2198 for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
2199 {
2200 switch (GET_CODE (insn))
2201 {
2202 case NOTE:
2203 case BARRIER:
2204 break;
2205
2206 case CODE_LABEL:
2207 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
2208 done = 1;
2209 break;
2210
2211 default:
2212 {
2213 rtx body = PATTERN (insn);
2214
2215 if (JUMP_P (body))
2216 {
2217 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
2218 done = 1;
2219 }
2220 else if ((GET_CODE (body) != USE)
2221 && (GET_CODE (body) != CLOBBER))
2222 done = 1;
2223 }
2224 break;
2225 }
2226 }
2227
2228 output_asm_insn ("%1_LEND:", operands);
2229 }
2230
2231
2232 char *
2233 xtensa_emit_branch (bool immed, rtx *operands)
2234 {
2235 static char result[64];
2236 enum rtx_code code = GET_CODE (operands[3]);
2237 const char *op;
2238
2239 switch (code)
2240 {
2241 case EQ: op = "eq"; break;
2242 case NE: op = "ne"; break;
2243 case LT: op = "lt"; break;
2244 case GE: op = "ge"; break;
2245 case LTU: op = "ltu"; break;
2246 case GEU: op = "geu"; break;
2247 default: gcc_unreachable ();
2248 }
2249
2250 if (immed)
2251 {
2252 if (INTVAL (operands[1]) == 0)
2253 sprintf (result, "b%sz%s\t%%0, %%2", op,
2254 (TARGET_DENSITY && (code == EQ || code == NE)) ? ".n" : "");
2255 else
2256 sprintf (result, "b%si\t%%0, %%d1, %%2", op);
2257 }
2258 else
2259 sprintf (result, "b%s\t%%0, %%1, %%2", op);
2260
2261 return result;
2262 }
2263
2264
2265 char *
2266 xtensa_emit_movcc (bool inverted, bool isfp, bool isbool, rtx *operands)
2267 {
2268 static char result[64];
2269 enum rtx_code code;
2270 const char *op;
2271
2272 code = GET_CODE (operands[4]);
2273 if (inverted)
2274 code = reverse_condition (code);
2275 if (isbool)
2276 {
2277 switch (code)
2278 {
2279 case EQ: op = "f"; break;
2280 case NE: op = "t"; break;
2281 default: gcc_unreachable ();
2282 }
2283 }
2284 else
2285 {
2286 switch (code)
2287 {
2288 case EQ: op = "eqz"; break;
2289 case NE: op = "nez"; break;
2290 case LT: op = "ltz"; break;
2291 case GE: op = "gez"; break;
2292 default: gcc_unreachable ();
2293 }
2294 }
2295
2296 sprintf (result, "mov%s%s\t%%0, %%%d, %%1",
2297 op, isfp ? ".s" : "", inverted ? 3 : 2);
2298 return result;
2299 }
2300
2301
2302 void
2303 xtensa_expand_call (int callop, rtx *operands)
2304 {
2305 rtx call;
2306 rtx_insn *call_insn;
2307 rtx addr = XEXP (operands[callop], 0);
2308
2309 if (flag_pic && SYMBOL_REF_P (addr)
2310 && (!SYMBOL_REF_LOCAL_P (addr) || SYMBOL_REF_EXTERNAL_P (addr)))
2311 addr = gen_sym_PLT (addr);
2312
2313 if (!call_insn_operand (addr, VOIDmode))
2314 {
2315 /* This may be called while generating MI thunk when we pretend
2316 that reload is over. Use a8 as a temporary register in that case. */
2317 rtx reg = can_create_pseudo_p ()
2318 ? copy_to_mode_reg (Pmode, addr)
2319 : copy_to_suggested_reg (addr,
2320 gen_rtx_REG (Pmode, A8_REG),
2321 Pmode);
2322 XEXP (operands[callop], 0) = reg;
2323 }
2324
2325 call = gen_rtx_CALL (VOIDmode, operands[callop], operands[callop + 1]);
2326
2327 if (callop)
2328 call = gen_rtx_SET (operands[0], call);
2329
2330 call_insn = emit_call_insn (call);
2331
2332 if (TARGET_WINDOWED_ABI)
2333 {
2334 /*
2335 * Windowed xtensa ABI specifies that static chain pointer is passed
2336 * in memory below the caller's stack pointer, which means that the
2337 * callee may clobber it if it's a non-leaf function.
2338 * Add the clobber expression for the static chain to the function call
2339 * expression list so that it is not assumed to be live across the call.
2340 */
2341 rtx clob = gen_rtx_CLOBBER (Pmode, xtensa_static_chain (NULL, false));
2342 CALL_INSN_FUNCTION_USAGE (call_insn) =
2343 gen_rtx_EXPR_LIST (Pmode, clob, CALL_INSN_FUNCTION_USAGE (call_insn));
2344 }
2345 }
2346
2347
2348 char *
2349 xtensa_emit_call (int callop, rtx *operands)
2350 {
2351 static char result[64];
2352 rtx tgt = operands[callop];
2353
2354 if (CONST_INT_P (tgt))
2355 sprintf (result, "call%d\t" HOST_WIDE_INT_PRINT_HEX,
2356 WINDOW_SIZE, INTVAL (tgt));
2357 else if (register_operand (tgt, VOIDmode))
2358 sprintf (result, "callx%d\t%%%d", WINDOW_SIZE, callop);
2359 else
2360 sprintf (result, "call%d\t%%%d", WINDOW_SIZE, callop);
2361
2362 return result;
2363 }
2364
2365
2366 char *
2367 xtensa_emit_sibcall (int callop, rtx *operands)
2368 {
2369 static char result[64];
2370 rtx tgt = operands[callop];
2371
2372 if (CONST_INT_P (tgt))
2373 sprintf (result, "j.l\t" HOST_WIDE_INT_PRINT_HEX ", a9",
2374 INTVAL (tgt));
2375 else if (register_operand (tgt, VOIDmode))
2376 sprintf (result, "jx\t%%%d", callop);
2377 else
2378 sprintf (result, "j.l\t%%%d, a9", callop);
2379
2380 return result;
2381 }
2382
2383 bool
2384 xtensa_legitimate_address_p (machine_mode mode, rtx addr, bool strict,
2385 code_helper)
2386 {
2387 /* Allow constant pool addresses. */
2388 if (mode != BLKmode
2389 && ! TARGET_CONST16 && constantpool_address_p (addr)
2390 && ! xtensa_tls_referenced_p (addr))
2391 return true;
2392
2393 while (SUBREG_P (addr))
2394 addr = SUBREG_REG (addr);
2395
2396 /* Allow base registers. */
2397 if (REG_P (addr) && BASE_REG_P (addr, strict))
2398 return true;
2399
2400 /* Check for "register + offset" addressing. */
2401 if (GET_CODE (addr) == PLUS)
2402 {
2403 rtx xplus0 = XEXP (addr, 0);
2404 rtx xplus1 = XEXP (addr, 1);
2405 enum rtx_code code0;
2406 enum rtx_code code1;
2407
2408 while (SUBREG_P (xplus0))
2409 xplus0 = SUBREG_REG (xplus0);
2410 code0 = GET_CODE (xplus0);
2411
2412 while (SUBREG_P (xplus1))
2413 xplus1 = SUBREG_REG (xplus1);
2414 code1 = GET_CODE (xplus1);
2415
2416 /* Swap operands if necessary so the register is first. */
2417 if (code0 != REG && code1 == REG)
2418 {
2419 xplus0 = XEXP (addr, 1);
2420 xplus1 = XEXP (addr, 0);
2421 code0 = GET_CODE (xplus0);
2422 code1 = GET_CODE (xplus1);
2423 }
2424
2425 if (code0 == REG && BASE_REG_P (xplus0, strict)
2426 && code1 == CONST_INT
2427 && xtensa_mem_offset (INTVAL (xplus1), mode))
2428 return true;
2429 }
2430
2431 return false;
2432 }
2433
2434
2435 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol. */
2436
2437 static GTY(()) rtx xtensa_tls_module_base_symbol;
2438
2439 static rtx
2440 xtensa_tls_module_base (void)
2441 {
2442 if (! xtensa_tls_module_base_symbol)
2443 {
2444 xtensa_tls_module_base_symbol =
2445 gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
2446 SYMBOL_REF_FLAGS (xtensa_tls_module_base_symbol)
2447 |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
2448 }
2449
2450 return xtensa_tls_module_base_symbol;
2451 }
2452
2453
2454 static rtx_insn *
2455 xtensa_call_tls_desc (rtx sym, rtx *retp)
2456 {
2457 rtx fn, arg, a_io;
2458 rtx_insn *call_insn, *insns;
2459
2460 start_sequence ();
2461 fn = gen_reg_rtx (Pmode);
2462 arg = gen_reg_rtx (Pmode);
2463 a_io = gen_rtx_REG (Pmode, WINDOW_SIZE + 2);
2464
2465 emit_insn (gen_tls_func (fn, sym));
2466 emit_insn (gen_tls_arg (arg, sym));
2467 emit_move_insn (a_io, arg);
2468 call_insn = emit_call_insn (gen_tls_call (a_io, fn, sym, const1_rtx));
2469 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn), a_io);
2470 insns = get_insns ();
2471 end_sequence ();
2472
2473 *retp = a_io;
2474 return insns;
2475 }
2476
2477
2478 static rtx
2479 xtensa_legitimize_tls_address (rtx x)
2480 {
2481 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
2482 rtx dest, tp, ret, modbase, base, addend;
2483 rtx_insn *insns;
2484
2485 dest = gen_reg_rtx (Pmode);
2486 switch (model)
2487 {
2488 case TLS_MODEL_GLOBAL_DYNAMIC:
2489 insns = xtensa_call_tls_desc (x, &ret);
2490 emit_libcall_block (insns, dest, ret, x);
2491 break;
2492
2493 case TLS_MODEL_LOCAL_DYNAMIC:
2494 base = gen_reg_rtx (Pmode);
2495 modbase = xtensa_tls_module_base ();
2496 insns = xtensa_call_tls_desc (modbase, &ret);
2497 emit_libcall_block (insns, base, ret, modbase);
2498 addend = force_reg (SImode, gen_sym_DTPOFF (x));
2499 emit_insn (gen_addsi3 (dest, base, addend));
2500 break;
2501
2502 case TLS_MODEL_INITIAL_EXEC:
2503 case TLS_MODEL_LOCAL_EXEC:
2504 tp = gen_reg_rtx (SImode);
2505 emit_insn (gen_get_thread_pointersi (tp));
2506 addend = force_reg (SImode, gen_sym_TPOFF (x));
2507 emit_insn (gen_addsi3 (dest, tp, addend));
2508 break;
2509
2510 default:
2511 gcc_unreachable ();
2512 }
2513
2514 return dest;
2515 }
2516
2517
2518 rtx
2519 xtensa_legitimize_address (rtx x,
2520 rtx oldx ATTRIBUTE_UNUSED,
2521 machine_mode mode)
2522 {
2523 if (xtensa_tls_symbol_p (x))
2524 return xtensa_legitimize_tls_address (x);
2525
2526 if (GET_CODE (x) == PLUS)
2527 {
2528 rtx plus0 = XEXP (x, 0);
2529 rtx plus1 = XEXP (x, 1);
2530
2531 if (! REG_P (plus0) && REG_P (plus1))
2532 {
2533 plus0 = XEXP (x, 1);
2534 plus1 = XEXP (x, 0);
2535 }
2536
2537 /* Try to split up the offset to use an ADDMI instruction. */
2538 if (REG_P (plus0) && CONST_INT_P (plus1)
2539 && !xtensa_mem_offset (INTVAL (plus1), mode)
2540 && !xtensa_simm8 (INTVAL (plus1))
2541 && xtensa_mem_offset (INTVAL (plus1) & 0xff, mode)
2542 && xtensa_simm8x256 (INTVAL (plus1) & ~0xff))
2543 {
2544 rtx temp = gen_reg_rtx (Pmode);
2545 rtx addmi_offset = GEN_INT (INTVAL (plus1) & ~0xff);
2546 emit_insn (gen_rtx_SET (temp, gen_rtx_PLUS (Pmode, plus0,
2547 addmi_offset)));
2548 return gen_rtx_PLUS (Pmode, temp, GEN_INT (INTVAL (plus1) & 0xff));
2549 }
2550 }
2551
2552 return x;
2553 }
2554
2555 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
2556
2557 Treat constant-pool references as "mode dependent" since they can
2558 only be accessed with SImode loads. This works around a bug in the
2559 combiner where a constant pool reference is temporarily converted
2560 to an HImode load, which is then assumed to zero-extend based on
2561 our definition of LOAD_EXTEND_OP. This is wrong because the high
2562 bits of a 16-bit value in the constant pool are now sign-extended
2563 by default. */
2564
2565 static bool
2566 xtensa_mode_dependent_address_p (const_rtx addr,
2567 addr_space_t as ATTRIBUTE_UNUSED)
2568 {
2569 return constantpool_address_p (addr);
2570 }
2571
2572 /* Return TRUE if X contains any TLS symbol references. */
2573
2574 bool
2575 xtensa_tls_referenced_p (rtx x)
2576 {
2577 if (! targetm.have_tls)
2578 return false;
2579
2580 subrtx_iterator::array_type array;
2581 FOR_EACH_SUBRTX (iter, array, x, ALL)
2582 {
2583 const_rtx x = *iter;
2584 if (SYMBOL_REF_P (x) && SYMBOL_REF_TLS_MODEL (x) != 0)
2585 return true;
2586
2587 /* Ignore TLS references that have already been legitimized. */
2588 if (GET_CODE (x) == UNSPEC)
2589 switch (XINT (x, 1))
2590 {
2591 case UNSPEC_TPOFF:
2592 case UNSPEC_DTPOFF:
2593 case UNSPEC_TLS_FUNC:
2594 case UNSPEC_TLS_ARG:
2595 case UNSPEC_TLS_CALL:
2596 iter.skip_subrtxes ();
2597 break;
2598 default:
2599 break;
2600 }
2601 }
2602 return false;
2603 }
2604
2605
2606 /* Helper function for "*shlrd_..." patterns. */
2607
2608 enum rtx_code
2609 xtensa_shlrd_which_direction (rtx op0, rtx op1)
2610 {
2611 if (GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
2612 return ASHIFT; /* shld */
2613 if (GET_CODE (op0) == LSHIFTRT && GET_CODE (op1) == ASHIFT)
2614 return LSHIFTRT; /* shrd */
2615
2616 return UNKNOWN;
2617 }
2618
2619
2620 /* Return true after "split1" pass has been finished. */
2621
2622 bool
2623 xtensa_split1_finished_p (void)
2624 {
2625 return cfun && (cfun->curr_properties & PROP_rtl_split_insns);
2626 }
2627
2628
2629 /* Split a DImode pair of reg (operand[0]) and const_int (operand[1]) into
2630 two SImode pairs, the low-part (operands[0] and [1]) and the high-part
2631 (operands[2] and [3]). */
2632
2633 void
2634 xtensa_split_DI_reg_imm (rtx *operands)
2635 {
2636 rtx lowpart, highpart;
2637
2638 if (WORDS_BIG_ENDIAN)
2639 split_double (operands[1], &highpart, &lowpart);
2640 else
2641 split_double (operands[1], &lowpart, &highpart);
2642
2643 operands[3] = highpart;
2644 operands[2] = gen_highpart (SImode, operands[0]);
2645 operands[1] = lowpart;
2646 operands[0] = gen_lowpart (SImode, operands[0]);
2647 }
2648
2649
2650 /* Try to split an integer value into what are suitable for two consecutive
2651 immediate addition instructions, ADDI or ADDMI. */
2652
2653 static bool
2654 xtensa_split_imm_two_addends (HOST_WIDE_INT imm, HOST_WIDE_INT v[2])
2655 {
2656 HOST_WIDE_INT v0, v1;
2657
2658 if (imm < -32768)
2659 v0 = -32768, v1 = imm + 32768;
2660 else if (imm > 32512)
2661 v0 = 32512, v1 = imm - 32512;
2662 else if (TARGET_DENSITY && xtensa_simm12b (imm))
2663 /* A pair of MOVI(.N) and ADD.N is one or two bytes less than two
2664 immediate additions if TARGET_DENSITY. */
2665 return false;
2666 else
2667 v0 = (imm + 128) & ~255L, v1 = imm - v0;
2668
2669 if (xtensa_simm8 (v1) || xtensa_simm8x256 (v1))
2670 {
2671 v[0] = v0, v[1] = v1;
2672 return true;
2673 }
2674
2675 return false;
2676 }
2677
2678
2679 /* Helper function for integer immediate addition with scratch register
2680 as needed, that splits and emits either up to two ADDI/ADDMI machine
2681 instructions or an addition by register following an integer immediate
2682 load (which may later be transformed by constantsynth).
2683
2684 If 'scratch' is NULL_RTX but still needed, a new pseudo-register will
2685 be allocated. Thus, after the reload/LRA pass, the specified scratch
2686 register must be a hard one. */
2687
2688 static bool
2689 xtensa_emit_add_imm (rtx dst, rtx src, HOST_WIDE_INT imm, rtx scratch,
2690 bool need_note)
2691 {
2692 bool retval = false;
2693 HOST_WIDE_INT v[2];
2694 rtx_insn *insn;
2695
2696 if (imm == 0)
2697 return false;
2698
2699 if (xtensa_simm8 (imm) || xtensa_simm8x256 (imm))
2700 insn = emit_insn (gen_addsi3 (dst, src, GEN_INT (imm)));
2701 else if (xtensa_split_imm_two_addends (imm, v))
2702 {
2703 if (!scratch)
2704 scratch = gen_reg_rtx (SImode);
2705 emit_insn (gen_addsi3 (scratch, src, GEN_INT (v[0])));
2706 insn = emit_insn (gen_addsi3 (dst, scratch, GEN_INT (v[1])));
2707 }
2708 else
2709 {
2710 if (scratch)
2711 emit_move_insn (scratch, GEN_INT (imm));
2712 else
2713 scratch = force_reg (SImode, GEN_INT (imm));
2714 retval = true;
2715 insn = emit_insn (gen_addsi3 (dst, src, scratch));
2716 }
2717
2718 if (need_note)
2719 {
2720 rtx note_rtx = gen_rtx_SET (dst, plus_constant (Pmode, src, imm));
2721
2722 RTX_FRAME_RELATED_P (insn) = 1;
2723 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
2724 }
2725
2726 return retval;
2727 }
2728
2729
2730 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2731
2732 static bool
2733 xtensa_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2734 {
2735 return xtensa_tls_referenced_p (x);
2736 }
2737
2738
2739 /* Return the debugger register number to use for 'regno'. */
2740
2741 int
2742 xtensa_debugger_regno (int regno)
2743 {
2744 int first = -1;
2745
2746 if (GP_REG_P (regno))
2747 {
2748 regno -= GP_REG_FIRST;
2749 first = 0;
2750 }
2751 else if (BR_REG_P (regno))
2752 {
2753 regno -= BR_REG_FIRST;
2754 first = 16;
2755 }
2756 else if (FP_REG_P (regno))
2757 {
2758 regno -= FP_REG_FIRST;
2759 first = 48;
2760 }
2761 else if (ACC_REG_P (regno))
2762 {
2763 first = 0x200; /* Start of Xtensa special registers. */
2764 regno = 16; /* ACCLO is special register 16. */
2765 }
2766
2767 /* When optimizing, we sometimes get asked about pseudo-registers
2768 that don't represent hard registers. Return 0 for these. */
2769 if (first == -1)
2770 return 0;
2771
2772 return first + regno;
2773 }
2774
2775
2776 /* Argument support functions. */
2777
2778 /* Initialize CUMULATIVE_ARGS for a function. */
2779
2780 void
2781 init_cumulative_args (CUMULATIVE_ARGS *cum, int incoming)
2782 {
2783 cum->arg_words = 0;
2784 cum->incoming = incoming;
2785 }
2786
2787
2788 /* Advance the argument to the next argument position. */
2789
2790 static void
2791 xtensa_function_arg_advance (cumulative_args_t cum,
2792 const function_arg_info &arg)
2793 {
2794 int words, max;
2795 int *arg_words;
2796
2797 arg_words = &get_cumulative_args (cum)->arg_words;
2798 max = MAX_ARGS_IN_REGISTERS;
2799
2800 words = ((arg.promoted_size_in_bytes () + UNITS_PER_WORD - 1)
2801 / UNITS_PER_WORD);
2802
2803 if (*arg_words < max
2804 && (targetm.calls.must_pass_in_stack (arg)
2805 || *arg_words + words > max))
2806 *arg_words = max;
2807
2808 *arg_words += words;
2809 }
2810
2811
2812 /* Return an RTL expression containing the register for the given argument,
2813 or 0 if the argument is to be passed on the stack. INCOMING_P is nonzero
2814 if this is an incoming argument to the current function. */
2815
2816 static rtx
2817 xtensa_function_arg_1 (cumulative_args_t cum_v, const function_arg_info &arg,
2818 bool incoming_p)
2819 {
2820 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2821 int regbase, words, max;
2822 int *arg_words;
2823 int regno;
2824
2825 arg_words = &cum->arg_words;
2826 regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
2827 max = MAX_ARGS_IN_REGISTERS;
2828
2829 words = ((arg.promoted_size_in_bytes () + UNITS_PER_WORD - 1)
2830 / UNITS_PER_WORD);
2831
2832 if (arg.type && (TYPE_ALIGN (arg.type) > BITS_PER_WORD))
2833 {
2834 int align = MIN (TYPE_ALIGN (arg.type), STACK_BOUNDARY) / BITS_PER_WORD;
2835 *arg_words = (*arg_words + align - 1) & -align;
2836 }
2837
2838 if (*arg_words + words > max)
2839 return (rtx)0;
2840
2841 regno = regbase + *arg_words;
2842
2843 if (cum->incoming && regno <= A7_REG && regno + words > A7_REG)
2844 cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
2845
2846 return gen_rtx_REG (arg.mode, regno);
2847 }
2848
2849 /* Implement TARGET_FUNCTION_ARG. */
2850
2851 static rtx
2852 xtensa_function_arg (cumulative_args_t cum, const function_arg_info &arg)
2853 {
2854 return xtensa_function_arg_1 (cum, arg, false);
2855 }
2856
2857 /* Implement TARGET_FUNCTION_INCOMING_ARG. */
2858
2859 static rtx
2860 xtensa_function_incoming_arg (cumulative_args_t cum,
2861 const function_arg_info &arg)
2862 {
2863 return xtensa_function_arg_1 (cum, arg, true);
2864 }
2865
2866 static unsigned int
2867 xtensa_function_arg_boundary (machine_mode mode, const_tree type)
2868 {
2869 unsigned int alignment;
2870
2871 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2872 if (alignment < PARM_BOUNDARY)
2873 alignment = PARM_BOUNDARY;
2874 if (alignment > STACK_BOUNDARY)
2875 alignment = STACK_BOUNDARY;
2876 return alignment;
2877 }
2878
2879
2880 static bool
2881 xtensa_return_in_msb (const_tree valtype)
2882 {
2883 return (TARGET_BIG_ENDIAN
2884 && AGGREGATE_TYPE_P (valtype)
2885 && int_size_in_bytes (valtype) >= UNITS_PER_WORD);
2886 }
2887
2888
2889 static void
2890 xtensa_option_override (void)
2891 {
2892 int regno;
2893 machine_mode mode;
2894
2895 if (xtensa_windowed_abi == -1)
2896 xtensa_windowed_abi = TARGET_WINDOWED_ABI_DEFAULT;
2897
2898 if (xtensa_strict_alignment == XTENSA_STRICT_ALIGNMENT_UNDEFINED)
2899 xtensa_strict_alignment = !XCHAL_UNALIGNED_LOAD_HW
2900 || !XCHAL_UNALIGNED_STORE_HW;
2901
2902 if (! TARGET_THREADPTR)
2903 targetm.have_tls = false;
2904
2905 /* Use CONST16 in the absence of L32R.
2906 Set it in the TARGET_OPTION_OVERRIDE to avoid dependency on xtensa
2907 configuration in the xtensa-common.cc */
2908
2909 if (!TARGET_L32R)
2910 target_flags |= MASK_CONST16;
2911
2912 if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
2913 error ("boolean registers required for the floating-point option");
2914
2915 /* Set up array giving whether a given register can hold a given mode. */
2916 for (mode = VOIDmode;
2917 mode != MAX_MACHINE_MODE;
2918 mode = (machine_mode) ((int) mode + 1))
2919 {
2920 int size = GET_MODE_SIZE (mode);
2921 enum mode_class mclass = GET_MODE_CLASS (mode);
2922
2923 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2924 {
2925 int temp;
2926
2927 if (ACC_REG_P (regno))
2928 temp = (TARGET_MAC16
2929 && (mclass == MODE_INT) && (size <= UNITS_PER_WORD));
2930 else if (GP_REG_P (regno))
2931 temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
2932 else if (FP_REG_P (regno))
2933 temp = (TARGET_HARD_FLOAT && (mode == SFmode));
2934 else if (BR_REG_P (regno))
2935 temp = (TARGET_BOOLEANS && (mode == CCmode));
2936 else
2937 temp = FALSE;
2938
2939 xtensa_hard_regno_mode_ok_p[(int) mode][regno] = temp;
2940 }
2941 }
2942
2943 init_machine_status = xtensa_init_machine_status;
2944
2945 /* Check PIC settings. PIC is only supported when using L32R
2946 instructions, and some targets need to always use PIC. */
2947 if (flag_pic && TARGET_CONST16)
2948 error ("%<-f%s%> is not supported with CONST16 instructions",
2949 (flag_pic > 1 ? "PIC" : "pic"));
2950 else if (TARGET_FORCE_NO_PIC)
2951 flag_pic = 0;
2952 else if (XTENSA_ALWAYS_PIC)
2953 {
2954 if (TARGET_CONST16)
2955 error ("PIC is required but not supported with CONST16 instructions");
2956 flag_pic = 1;
2957 }
2958 /* There's no need for -fPIC (as opposed to -fpic) on Xtensa. */
2959 if (flag_pic > 1)
2960 flag_pic = 1;
2961 if (flag_pic && !flag_pie)
2962 flag_shlib = 1;
2963
2964 /* Hot/cold partitioning does not work on this architecture, because of
2965 constant pools (the load instruction cannot necessarily reach that far).
2966 Therefore disable it on this architecture. */
2967 if (flag_reorder_blocks_and_partition)
2968 {
2969 flag_reorder_blocks_and_partition = 0;
2970 flag_reorder_blocks = 1;
2971 }
2972
2973 /* One of the late-combine passes runs after register allocation
2974 and can match define_insn_and_splits that were previously used
2975 only before register allocation. Some of those define_insn_and_splits
2976 require the split to take place, but have a split condition of
2977 can_create_pseudo_p, and so matching after RA will give an
2978 unsplittable instruction. Disable late-combine by default until
2979 the define_insn_and_splits are fixed. */
2980 if (!OPTION_SET_P (flag_late_combine_instructions))
2981 flag_late_combine_instructions = 0;
2982
2983 xtensa_option_override_after_change ();
2984 }
2985
2986 static void
2987 xtensa_option_override_after_change (void)
2988 {
2989 if (!OPTION_SET_P (flag_late_combine_instructions))
2990 flag_late_combine_instructions = 0;
2991 }
2992
2993 /* Implement TARGET_HARD_REGNO_NREGS. */
2994
2995 static unsigned int
2996 xtensa_hard_regno_nregs (unsigned int regno, machine_mode mode)
2997 {
2998 if (FP_REG_P (regno))
2999 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_FPREG);
3000 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
3001 }
3002
3003 /* Implement TARGET_HARD_REGNO_MODE_OK. */
3004
3005 static bool
3006 xtensa_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
3007 {
3008 return xtensa_hard_regno_mode_ok_p[mode][regno];
3009 }
3010
3011 /* Implement TARGET_MODES_TIEABLE_P. */
3012
3013 static bool
3014 xtensa_modes_tieable_p (machine_mode mode1, machine_mode mode2)
3015 {
3016 return ((GET_MODE_CLASS (mode1) == MODE_FLOAT
3017 || GET_MODE_CLASS (mode1) == MODE_COMPLEX_FLOAT)
3018 == (GET_MODE_CLASS (mode2) == MODE_FLOAT
3019 || GET_MODE_CLASS (mode2) == MODE_COMPLEX_FLOAT));
3020 }
3021
3022 /* A C compound statement to output to stdio stream STREAM the
3023 assembler syntax for an instruction operand X. X is an RTL
3024 expression.
3025
3026 CODE is a value that can be used to specify one of several ways
3027 of printing the operand. It is used when identical operands
3028 must be printed differently depending on the context. CODE
3029 comes from the '%' specification that was used to request
3030 printing of the operand. If the specification was just '%DIGIT'
3031 then CODE is 0; if the specification was '%LTR DIGIT' then CODE
3032 is the ASCII code for LTR.
3033
3034 If X is a register, this macro should print the register's name.
3035 The names can be found in an array 'reg_names' whose type is
3036 'char *[]'. 'reg_names' is initialized from 'REGISTER_NAMES'.
3037
3038 When the machine description has a specification '%PUNCT' (a '%'
3039 followed by a punctuation character), this macro is called with
3040 a null pointer for X and the punctuation character for CODE.
3041
3042 'a', 'c', 'l', and 'n' are reserved.
3043
3044 The Xtensa specific codes are:
3045
3046 'd' CONST_INT, print as signed decimal
3047 'x' CONST_INT, print as signed hexadecimal
3048 'K' CONST_INT, print number of bits in mask for EXTUI
3049 'R' CONST_INT, print (X & 0x1f)
3050 'L' CONST_INT, print ((32 - X) & 0x1f)
3051 'D' REG, print second register of double-word register operand
3052 'N' MEM, print address of next word following a memory operand
3053 'v' MEM, if memory reference is volatile, output a MEMW before it
3054 't' any constant, add "@h" suffix for top 16 bits
3055 'b' any constant, add "@l" suffix for bottom 16 bits
3056 */
3057
3058 static void
3059 printx (FILE *file, signed int val)
3060 {
3061 /* Print a hexadecimal value in a nice way. */
3062 if (IN_RANGE (val, -9, 9))
3063 fprintf (file, "%d", val);
3064 else if (val < 0)
3065 fprintf (file, "-0x%x", -val);
3066 else
3067 fprintf (file, "0x%x", val);
3068 }
3069
3070
3071 void
3072 print_operand (FILE *file, rtx x, int letter)
3073 {
3074 if (!x)
3075 error ("%<PRINT_OPERAND%> null pointer");
3076
3077 switch (letter)
3078 {
3079 case 'D':
3080 if (REG_P (x) || SUBREG_P (x))
3081 fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
3082 else
3083 output_operand_lossage ("invalid %%D value");
3084 break;
3085
3086 case 'v':
3087 if (MEM_P (x))
3088 {
3089 /* For a volatile memory reference, emit a MEMW before the
3090 load or store. */
3091 if (MEM_VOLATILE_P (x) && TARGET_SERIALIZE_VOLATILE)
3092 {
3093 rtx_insn *prev_insn
3094 = prev_nonnote_nondebug_insn (current_output_insn);
3095 rtx pat, src;
3096
3097 if (! (prev_insn && NONJUMP_INSN_P (prev_insn)
3098 && GET_CODE (pat = PATTERN (prev_insn)) == SET
3099 && GET_CODE (src = SET_SRC (pat)) == UNSPEC
3100 && XINT (src, 1) == UNSPEC_MEMW))
3101 fprintf (file, "memw\n\t");
3102 }
3103 }
3104 else
3105 output_operand_lossage ("invalid %%v value");
3106 break;
3107
3108 case 'N':
3109 if (MEM_P (x)
3110 && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
3111 {
3112 x = adjust_address (x, GET_MODE (x) == DFmode ? E_SFmode : E_SImode,
3113 4);
3114 output_address (GET_MODE (x), XEXP (x, 0));
3115 }
3116 else
3117 output_operand_lossage ("invalid %%N value");
3118 break;
3119
3120 case 'K':
3121 if (CONST_INT_P (x))
3122 {
3123 unsigned val = INTVAL (x);
3124 if (!xtensa_mask_immediate (val))
3125 fatal_insn ("invalid mask", x);
3126
3127 fprintf (file, "%d", floor_log2 (val + 1));
3128 }
3129 else
3130 output_operand_lossage ("invalid %%K value");
3131 break;
3132
3133 case 'L':
3134 if (CONST_INT_P (x))
3135 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 0x1f);
3136 else
3137 output_operand_lossage ("invalid %%L value");
3138 break;
3139
3140 case 'R':
3141 if (CONST_INT_P (x))
3142 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x1f);
3143 else
3144 output_operand_lossage ("invalid %%R value");
3145 break;
3146
3147 case 'x':
3148 if (CONST_INT_P (x))
3149 printx (file, INTVAL (x));
3150 else
3151 output_operand_lossage ("invalid %%x value");
3152 break;
3153
3154 case 'd':
3155 if (CONST_INT_P (x))
3156 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
3157 else
3158 output_operand_lossage ("invalid %%d value");
3159 break;
3160
3161 case 't':
3162 case 'b':
3163 if (CONST_INT_P (x))
3164 {
3165 printx (file, INTVAL (x));
3166 fputs (letter == 't' ? "@h" : "@l", file);
3167 }
3168 else if (CONST_DOUBLE_P (x))
3169 {
3170 if (GET_MODE (x) == SFmode)
3171 {
3172 long l;
3173 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
3174 fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
3175 }
3176 else
3177 output_operand_lossage ("invalid %%t/%%b value");
3178 }
3179 else if (GET_CODE (x) == CONST)
3180 {
3181 /* X must be a symbolic constant on ELF. Write an expression
3182 suitable for 'const16' that sets the high or low 16 bits. */
3183 if (GET_CODE (XEXP (x, 0)) != PLUS
3184 || (! SYMBOL_REF_P (XEXP (XEXP (x, 0), 0))
3185 && ! LABEL_REF_P (XEXP (XEXP (x, 0), 0)))
3186 || ! CONST_INT_P (XEXP (XEXP (x, 0), 1)))
3187 output_operand_lossage ("invalid %%t/%%b value");
3188 print_operand (file, XEXP (XEXP (x, 0), 0), 0);
3189 fputs (letter == 't' ? "@h" : "@l", file);
3190 /* There must be a non-alphanumeric character between 'h' or 'l'
3191 and the number. The '-' is added by print_operand() already. */
3192 if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
3193 fputs ("+", file);
3194 print_operand (file, XEXP (XEXP (x, 0), 1), 0);
3195 }
3196 else
3197 {
3198 output_addr_const (file, x);
3199 fputs (letter == 't' ? "@h" : "@l", file);
3200 }
3201 break;
3202
3203 case 'y':
3204 if (CONST_DOUBLE_P (x) && GET_MODE (x) == SFmode)
3205 {
3206 long l;
3207 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
3208 fprintf (file, "0x%08lx", l);
3209 break;
3210 }
3211
3212 /* fall through */
3213
3214 default:
3215 if (REG_P (x) || SUBREG_P (x))
3216 fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
3217 else if (MEM_P (x))
3218 output_address (GET_MODE (x), XEXP (x, 0));
3219 else if (CONST_INT_P (x))
3220 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
3221 else
3222 output_addr_const (file, x);
3223 }
3224 }
3225
3226
3227 /* A C compound statement to output to stdio stream STREAM the
3228 assembler syntax for an instruction operand that is a memory
3229 reference whose address is ADDR. ADDR is an RTL expression. */
3230
3231 void
3232 print_operand_address (FILE *file, rtx addr)
3233 {
3234 if (!addr)
3235 error ("%<PRINT_OPERAND_ADDRESS%>, null pointer");
3236
3237 switch (GET_CODE (addr))
3238 {
3239 default:
3240 fatal_insn ("invalid address", addr);
3241 break;
3242
3243 case REG:
3244 fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
3245 break;
3246
3247 case PLUS:
3248 {
3249 rtx reg = (rtx)0;
3250 rtx offset = (rtx)0;
3251 rtx arg0 = XEXP (addr, 0);
3252 rtx arg1 = XEXP (addr, 1);
3253
3254 if (REG_P (arg0))
3255 {
3256 reg = arg0;
3257 offset = arg1;
3258 }
3259 else if (REG_P (arg1))
3260 {
3261 reg = arg1;
3262 offset = arg0;
3263 }
3264 else
3265 fatal_insn ("no register in address", addr);
3266
3267 if (CONSTANT_P (offset))
3268 {
3269 fprintf (file, "%s, ", reg_names [REGNO (reg)]);
3270 output_addr_const (file, offset);
3271 }
3272 else
3273 fatal_insn ("address offset not a constant", addr);
3274 }
3275 break;
3276
3277 case LABEL_REF:
3278 case SYMBOL_REF:
3279 case CONST_INT:
3280 case CONST:
3281 output_addr_const (file, addr);
3282 break;
3283 }
3284 }
3285
3286 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
3287
3288 static bool
3289 xtensa_output_addr_const_extra (FILE *fp, rtx x)
3290 {
3291 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
3292 {
3293 switch (XINT (x, 1))
3294 {
3295 case UNSPEC_TPOFF:
3296 output_addr_const (fp, XVECEXP (x, 0, 0));
3297 fputs ("@TPOFF", fp);
3298 return true;
3299 case UNSPEC_DTPOFF:
3300 output_addr_const (fp, XVECEXP (x, 0, 0));
3301 fputs ("@DTPOFF", fp);
3302 return true;
3303 case UNSPEC_PLT:
3304 if (flag_pic)
3305 {
3306 output_addr_const (fp, XVECEXP (x, 0, 0));
3307 fputs ("@PLT", fp);
3308 return true;
3309 }
3310 break;
3311 default:
3312 break;
3313 }
3314 }
3315 return false;
3316 }
3317
3318 static void
3319 xtensa_output_integer_literal_parts (FILE *file, rtx x, int size)
3320 {
3321 if (size > 4 && !(size & (size - 1)))
3322 {
3323 rtx first, second;
3324
3325 split_double (x, &first, &second);
3326 xtensa_output_integer_literal_parts (file, first, size / 2);
3327 fputs (", ", file);
3328 xtensa_output_integer_literal_parts (file, second, size / 2);
3329 }
3330 else if (size == 4 || size == 2)
3331 {
3332 output_addr_const (file, x);
3333 }
3334 else
3335 {
3336 gcc_unreachable();
3337 }
3338 }
3339
3340 void
3341 xtensa_output_literal (FILE *file, rtx x, machine_mode mode, int labelno)
3342 {
3343 long value_long[2];
3344
3345 fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
3346
3347 switch (GET_MODE_CLASS (mode))
3348 {
3349 case MODE_FLOAT:
3350 gcc_assert (CONST_DOUBLE_P (x));
3351
3352 switch (mode)
3353 {
3354 case E_SFmode:
3355 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x),
3356 value_long[0]);
3357 if (HOST_BITS_PER_LONG > 32)
3358 value_long[0] &= 0xffffffff;
3359 fprintf (file, "0x%08lx\n", value_long[0]);
3360 break;
3361
3362 case E_DFmode:
3363 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x),
3364 value_long);
3365 if (HOST_BITS_PER_LONG > 32)
3366 {
3367 value_long[0] &= 0xffffffff;
3368 value_long[1] &= 0xffffffff;
3369 }
3370 fprintf (file, "0x%08lx, 0x%08lx\n",
3371 value_long[0], value_long[1]);
3372 break;
3373
3374 default:
3375 gcc_unreachable ();
3376 }
3377
3378 break;
3379
3380 case MODE_INT:
3381 case MODE_PARTIAL_INT:
3382 xtensa_output_integer_literal_parts (file, x, GET_MODE_SIZE (mode));
3383 fputs ("\n", file);
3384 break;
3385
3386 default:
3387 gcc_unreachable ();
3388 }
3389 }
3390
3391 static bool
3392 xtensa_call_save_reg (int regno)
3393 {
3394 if (TARGET_WINDOWED_ABI)
3395 return false;
3396
3397 if (regno == A0_REG)
3398 /* Ensure the return address to be saved to the stack slot in order
3399 to assist stack dump analysis when -Og is specified. */
3400 return optimize_debug
3401 || crtl->profile || !crtl->is_leaf || crtl->calls_eh_return
3402 || df_regs_ever_live_p (regno);
3403
3404 if (crtl->calls_eh_return && IN_RANGE (regno, 2, 3))
3405 return true;
3406
3407 return !call_used_or_fixed_reg_p (regno) && df_regs_ever_live_p (regno);
3408 }
3409
3410 /* Return the bytes needed to compute the frame pointer from the current
3411 stack pointer. */
3412
3413 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
3414 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
3415
3416 long
3417 compute_frame_size (poly_int64 size)
3418 {
3419 int regno;
3420
3421 if (reload_completed && cfun->machine->frame_laid_out)
3422 return cfun->machine->current_frame_size;
3423
3424 /* Add space for the incoming static chain value. */
3425 if (cfun->static_chain_decl != NULL)
3426 size += (1 * UNITS_PER_WORD);
3427
3428 cfun->machine->callee_save_size = 0;
3429 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
3430 {
3431 if (xtensa_call_save_reg (regno))
3432 cfun->machine->callee_save_size += UNITS_PER_WORD;
3433 }
3434
3435 cfun->machine->current_frame_size =
3436 XTENSA_STACK_ALIGN (size
3437 + cfun->machine->callee_save_size
3438 + crtl->outgoing_args_size
3439 + (WINDOW_SIZE * UNITS_PER_WORD));
3440 cfun->machine->callee_save_size =
3441 XTENSA_STACK_ALIGN (cfun->machine->callee_save_size);
3442 cfun->machine->frame_laid_out = true;
3443 return cfun->machine->current_frame_size;
3444 }
3445
3446
3447 bool
3448 xtensa_frame_pointer_required (void)
3449 {
3450 /* The code to expand builtin_frame_addr and builtin_return_addr
3451 currently uses the hard_frame_pointer instead of frame_pointer.
3452 This seems wrong but maybe it's necessary for other architectures.
3453 This function is derived from the i386 code. */
3454
3455 if (cfun->machine->accesses_prev_frame || cfun->has_nonlocal_label)
3456 return true;
3457
3458 return false;
3459 }
3460
3461 HOST_WIDE_INT
3462 xtensa_initial_elimination_offset (int from, int to ATTRIBUTE_UNUSED)
3463 {
3464 long frame_size = compute_frame_size (get_frame_size ());
3465 HOST_WIDE_INT offset;
3466
3467 switch (from)
3468 {
3469 case FRAME_POINTER_REGNUM:
3470 if (FRAME_GROWS_DOWNWARD)
3471 offset = frame_size - (WINDOW_SIZE * UNITS_PER_WORD)
3472 - cfun->machine->callee_save_size;
3473 else
3474 offset = 0;
3475 break;
3476 case ARG_POINTER_REGNUM:
3477 offset = frame_size;
3478 break;
3479 default:
3480 gcc_unreachable ();
3481 }
3482
3483 return offset;
3484 }
3485
3486 #define ADJUST_SP_NONE 0x0
3487 #define ADJUST_SP_NEED_NOTE 0x1
3488 #define ADJUST_SP_FRAME_PTR 0x2
3489 static void
3490 xtensa_emit_adjust_stack_ptr (HOST_WIDE_INT offset, int flags)
3491 {
3492 rtx src, scratch;
3493 rtx_insn *insn;
3494
3495 if (cfun->machine->inhibit_logues_a1_adjusts)
3496 return;
3497
3498 src = (flags & ADJUST_SP_FRAME_PTR)
3499 ? hard_frame_pointer_rtx : stack_pointer_rtx;
3500 scratch = gen_rtx_REG (Pmode, A9_REG);
3501
3502 if (df && DF_REG_DEF_COUNT (A9_REG) == 0
3503 && cfun->machine->last_logues_a9_content
3504 && -INTVAL (cfun->machine->last_logues_a9_content) == offset)
3505 {
3506 insn = emit_insn (gen_subsi3 (stack_pointer_rtx, src, scratch));
3507 if (flags & ADJUST_SP_NEED_NOTE)
3508 {
3509 rtx note_rtx = gen_rtx_SET (stack_pointer_rtx,
3510 plus_constant (Pmode, src, offset));
3511
3512 RTX_FRAME_RELATED_P (insn) = 1;
3513 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
3514 }
3515 }
3516 else if (xtensa_emit_add_imm (stack_pointer_rtx, src, offset, scratch,
3517 (flags & ADJUST_SP_NEED_NOTE)))
3518 cfun->machine->last_logues_a9_content = GEN_INT (offset);
3519 }
3520
3521 static bool
3522 xtensa_can_eliminate_callee_saved_reg_p (unsigned int regno,
3523 rtx_insn **p_insnS,
3524 rtx_insn **p_insnR)
3525 {
3526 df_ref ref;
3527 rtx_insn *insn, *insnS = NULL, *insnR = NULL;
3528 rtx pattern;
3529
3530 if (!optimize || !df || call_used_or_fixed_reg_p (regno))
3531 return false;
3532
3533 for (ref = DF_REG_DEF_CHAIN (regno);
3534 ref; ref = DF_REF_NEXT_REG (ref))
3535 if (DF_REF_CLASS (ref) != DF_REF_REGULAR
3536 || DEBUG_INSN_P (insn = DF_REF_INSN (ref)))
3537 continue;
3538 else if (GET_CODE (pattern = PATTERN (insn)) == SET
3539 && REG_P (SET_DEST (pattern))
3540 && REGNO (SET_DEST (pattern)) == regno
3541 && REG_NREGS (SET_DEST (pattern)) == 1
3542 && REG_P (SET_SRC (pattern))
3543 && REGNO (SET_SRC (pattern)) != A1_REG)
3544 {
3545 if (insnS)
3546 return false;
3547 insnS = insn;
3548 continue;
3549 }
3550 else
3551 return false;
3552
3553 for (ref = DF_REG_USE_CHAIN (regno);
3554 ref; ref = DF_REF_NEXT_REG (ref))
3555 if (DF_REF_CLASS (ref) != DF_REF_REGULAR
3556 || DEBUG_INSN_P (insn = DF_REF_INSN (ref)))
3557 continue;
3558 else if (GET_CODE (pattern = PATTERN (insn)) == SET
3559 && REG_P (SET_SRC (pattern))
3560 && REGNO (SET_SRC (pattern)) == regno
3561 && REG_NREGS (SET_SRC (pattern)) == 1
3562 && REG_P (SET_DEST (pattern))
3563 && REGNO (SET_DEST (pattern)) != A1_REG)
3564 {
3565 if (insnR)
3566 return false;
3567 insnR = insn;
3568 continue;
3569 }
3570 else
3571 return false;
3572
3573 if (!insnS || !insnR)
3574 return false;
3575
3576 *p_insnS = insnS, *p_insnR = insnR;
3577
3578 return true;
3579 }
3580
3581 /* minimum frame = reg save area (4 words) plus static chain (1 word)
3582 and the total number of words must be a multiple of 128 bits. */
3583 #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD)
3584
3585 void
3586 xtensa_expand_prologue (void)
3587 {
3588 HOST_WIDE_INT total_size;
3589 rtx_insn *insn = NULL;
3590 rtx note_rtx;
3591
3592 total_size = compute_frame_size (get_frame_size ());
3593
3594 if (flag_stack_usage_info)
3595 current_function_static_stack_size = total_size;
3596
3597 if (TARGET_WINDOWED_ABI)
3598 {
3599 if (total_size < (1 << (12+3)))
3600 insn = emit_insn (gen_entry (GEN_INT (total_size)));
3601 else
3602 {
3603 /* Use a8 as a temporary since a0-a7 may be live. */
3604 rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
3605 emit_insn (gen_entry (GEN_INT (MIN_FRAME_SIZE)));
3606 xtensa_emit_add_imm (tmp_reg, stack_pointer_rtx,
3607 MIN_FRAME_SIZE - total_size,
3608 tmp_reg, false);
3609 insn = emit_insn (gen_movsi (stack_pointer_rtx, tmp_reg));
3610 }
3611 }
3612 else
3613 {
3614 int regno;
3615 HOST_WIDE_INT offset = 0;
3616 int callee_save_size = cfun->machine->callee_save_size;
3617 df_ref ref;
3618 bool stack_pointer_needed = frame_pointer_needed
3619 || crtl->calls_eh_return;
3620 bool large_stack_needed;
3621
3622 /* Check if the function body really needs the stack pointer. */
3623 if (!stack_pointer_needed && df)
3624 for (ref = DF_REG_USE_CHAIN (A1_REG);
3625 ref; ref = DF_REF_NEXT_REG (ref))
3626 if (DF_REF_CLASS (ref) == DF_REF_REGULAR
3627 && NONJUMP_INSN_P (DF_REF_INSN (ref)))
3628 {
3629 stack_pointer_needed = true;
3630 break;
3631 }
3632 /* Check if callee-saved registers really need saving to the stack. */
3633 if (!stack_pointer_needed)
3634 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
3635 if (xtensa_call_save_reg (regno))
3636 {
3637 stack_pointer_needed = true;
3638 break;
3639 }
3640
3641 cfun->machine->inhibit_logues_a1_adjusts = !stack_pointer_needed;
3642
3643 /* -128 is a limit of single addi instruction. */
3644 if (IN_RANGE (total_size, 1, 128))
3645 {
3646 xtensa_emit_adjust_stack_ptr (-total_size,
3647 ADJUST_SP_NEED_NOTE);
3648 offset = total_size - UNITS_PER_WORD;
3649 }
3650 else if (callee_save_size)
3651 {
3652 /* 1020 is maximal s32i offset, if the frame is bigger than that
3653 * we move sp to the end of callee-saved save area, save and then
3654 * move it to its final location. */
3655 if (total_size > 1024)
3656 {
3657 xtensa_emit_adjust_stack_ptr (-callee_save_size,
3658 ADJUST_SP_NEED_NOTE);
3659 offset = callee_save_size - UNITS_PER_WORD;
3660 }
3661 else
3662 {
3663 xtensa_emit_adjust_stack_ptr (-total_size,
3664 ADJUST_SP_NEED_NOTE);
3665 offset = total_size - UNITS_PER_WORD;
3666 }
3667 }
3668
3669 large_stack_needed = total_size > 1024
3670 || (!callee_save_size && total_size > 128);
3671 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
3672 if (xtensa_call_save_reg(regno))
3673 {
3674 rtx x = gen_rtx_PLUS (Pmode,
3675 stack_pointer_rtx, GEN_INT (offset));
3676 rtx mem = gen_frame_mem (SImode, x);
3677 rtx_insn *insnS, *insnR;
3678
3679 if (!large_stack_needed
3680 && xtensa_can_eliminate_callee_saved_reg_p (regno,
3681 &insnS, &insnR))
3682 {
3683 if (frame_pointer_needed)
3684 mem = replace_rtx (mem, stack_pointer_rtx,
3685 hard_frame_pointer_rtx);
3686 SET_DEST (PATTERN (insnS)) = mem;
3687 df_insn_rescan (insnS);
3688 SET_SRC (PATTERN (insnR)) = copy_rtx (mem);
3689 df_insn_rescan (insnR);
3690 SET_HARD_REG_BIT (cfun->machine->eliminated_callee_saved,
3691 regno);
3692 }
3693 else
3694 {
3695 rtx reg = gen_rtx_REG (SImode, regno);
3696
3697 insn = emit_move_insn (mem, reg);
3698 RTX_FRAME_RELATED_P (insn) = 1;
3699 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3700 gen_rtx_SET (mem, reg));
3701 }
3702 offset -= UNITS_PER_WORD;
3703 }
3704 if (large_stack_needed)
3705 xtensa_emit_adjust_stack_ptr (callee_save_size - total_size,
3706 ADJUST_SP_NEED_NOTE);
3707 }
3708
3709 if (frame_pointer_needed)
3710 {
3711 if (cfun->machine->set_frame_ptr_insn)
3712 {
3713 rtx_insn *first;
3714
3715 push_topmost_sequence ();
3716 first = get_insns ();
3717 pop_topmost_sequence ();
3718
3719 /* For all instructions prior to set_frame_ptr_insn, replace
3720 hard_frame_pointer references with stack_pointer. */
3721 for (insn = first;
3722 insn != cfun->machine->set_frame_ptr_insn;
3723 insn = NEXT_INSN (insn))
3724 {
3725 if (INSN_P (insn))
3726 {
3727 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
3728 hard_frame_pointer_rtx,
3729 stack_pointer_rtx);
3730 df_insn_rescan (insn);
3731 }
3732 }
3733 }
3734 else
3735 {
3736 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
3737 stack_pointer_rtx));
3738 if (!TARGET_WINDOWED_ABI)
3739 {
3740 note_rtx = gen_rtx_SET (hard_frame_pointer_rtx,
3741 stack_pointer_rtx);
3742 RTX_FRAME_RELATED_P (insn) = 1;
3743 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
3744 }
3745 }
3746 }
3747
3748 if (TARGET_WINDOWED_ABI)
3749 {
3750 /* Create a note to describe the CFA. Because this is only used to set
3751 DW_AT_frame_base for debug info, don't bother tracking changes through
3752 each instruction in the prologue. It just takes up space. */
3753 note_rtx = gen_rtx_SET ((frame_pointer_needed
3754 ? hard_frame_pointer_rtx
3755 : stack_pointer_rtx),
3756 plus_constant (Pmode, stack_pointer_rtx,
3757 -total_size));
3758 RTX_FRAME_RELATED_P (insn) = 1;
3759 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
3760 }
3761 }
3762
3763 void
3764 xtensa_expand_epilogue (void)
3765 {
3766 if (!TARGET_WINDOWED_ABI)
3767 {
3768 int regno;
3769 HOST_WIDE_INT offset;
3770
3771 if (cfun->machine->current_frame_size > (frame_pointer_needed ? 127 : 1024))
3772 {
3773 xtensa_emit_adjust_stack_ptr (cfun->machine->current_frame_size -
3774 cfun->machine->callee_save_size,
3775 frame_pointer_needed
3776 ? ADJUST_SP_FRAME_PTR
3777 : ADJUST_SP_NONE);
3778 offset = cfun->machine->callee_save_size - UNITS_PER_WORD;
3779 }
3780 else
3781 {
3782 if (frame_pointer_needed)
3783 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
3784 offset = cfun->machine->current_frame_size - UNITS_PER_WORD;
3785 }
3786
3787 /* Prevent reordering of saved a0 update and loading it back from
3788 the save area. */
3789 if (crtl->calls_eh_return)
3790 emit_insn (gen_blockage ());
3791
3792 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
3793 if (xtensa_call_save_reg(regno))
3794 {
3795 if (! TEST_HARD_REG_BIT (cfun->machine->eliminated_callee_saved,
3796 regno))
3797 {
3798 rtx x = gen_rtx_PLUS (Pmode,
3799 stack_pointer_rtx, GEN_INT (offset));
3800 emit_move_insn (gen_rtx_REG (SImode, regno),
3801 gen_frame_mem (SImode, x));
3802 }
3803 offset -= UNITS_PER_WORD;
3804 }
3805
3806 if (cfun->machine->current_frame_size > 0)
3807 {
3808 if (frame_pointer_needed || /* always reachable with addi */
3809 cfun->machine->current_frame_size > 1024 ||
3810 cfun->machine->current_frame_size <= 127)
3811 {
3812 if (cfun->machine->current_frame_size <= 127)
3813 offset = cfun->machine->current_frame_size;
3814 else
3815 offset = cfun->machine->callee_save_size;
3816 if (offset)
3817 xtensa_emit_adjust_stack_ptr (offset, ADJUST_SP_NONE);
3818 }
3819 else
3820 xtensa_emit_adjust_stack_ptr (cfun->machine->current_frame_size,
3821 ADJUST_SP_NONE);
3822 }
3823
3824 if (crtl->calls_eh_return)
3825 emit_insn (gen_add3_insn (stack_pointer_rtx,
3826 stack_pointer_rtx,
3827 EH_RETURN_STACKADJ_RTX));
3828 }
3829 }
3830
3831 void
3832 xtensa_set_return_address (rtx address, rtx scratch)
3833 {
3834 HOST_WIDE_INT total_size = compute_frame_size (get_frame_size ());
3835 rtx frame = frame_pointer_needed ?
3836 hard_frame_pointer_rtx : stack_pointer_rtx;
3837 rtx a0_addr = plus_constant (Pmode, frame,
3838 total_size - UNITS_PER_WORD);
3839 rtx note = gen_rtx_SET (gen_frame_mem (SImode, a0_addr),
3840 gen_rtx_REG (SImode, A0_REG));
3841 rtx insn;
3842
3843 if (total_size > 1024)
3844 {
3845 xtensa_emit_add_imm (scratch, frame, total_size - UNITS_PER_WORD,
3846 scratch, false);
3847 a0_addr = scratch;
3848 }
3849
3850 insn = emit_move_insn (gen_frame_mem (SImode, a0_addr), address);
3851 RTX_FRAME_RELATED_P (insn) = 1;
3852 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
3853 }
3854
3855 rtx
3856 xtensa_return_addr (int count, rtx frame)
3857 {
3858 rtx result, retaddr, curaddr, label;
3859
3860 if (!TARGET_WINDOWED_ABI)
3861 {
3862 if (count != 0)
3863 return const0_rtx;
3864
3865 return get_hard_reg_initial_val (Pmode, A0_REG);
3866 }
3867
3868 if (count == -1)
3869 retaddr = gen_rtx_REG (Pmode, A0_REG);
3870 else
3871 {
3872 rtx addr = plus_constant (Pmode, frame, -4 * UNITS_PER_WORD);
3873 addr = memory_address (Pmode, addr);
3874 retaddr = gen_reg_rtx (Pmode);
3875 emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
3876 }
3877
3878 /* The 2 most-significant bits of the return address on Xtensa hold
3879 the register window size. To get the real return address, these
3880 bits must be replaced with the high bits from some address in the
3881 code. */
3882
3883 /* Get the 2 high bits of a local label in the code. */
3884 curaddr = gen_reg_rtx (Pmode);
3885 label = gen_label_rtx ();
3886 emit_label (label);
3887 LABEL_PRESERVE_P (label) = 1;
3888 emit_move_insn (curaddr, gen_rtx_LABEL_REF (Pmode, label));
3889 emit_insn (gen_lshrsi3 (curaddr, curaddr, GEN_INT (30)));
3890 emit_insn (gen_ashlsi3 (curaddr, curaddr, GEN_INT (30)));
3891
3892 /* Clear the 2 high bits of the return address. */
3893 result = gen_reg_rtx (Pmode);
3894 emit_insn (gen_ashlsi3 (result, retaddr, GEN_INT (2)));
3895 emit_insn (gen_lshrsi3 (result, result, GEN_INT (2)));
3896
3897 /* Combine them to get the result. */
3898 emit_insn (gen_iorsi3 (result, result, curaddr));
3899 return result;
3900 }
3901
3902 /* Disable the use of word-sized or smaller complex modes for structures,
3903 and for function arguments in particular, where they cause problems with
3904 register a7. The xtensa_copy_incoming_a7 function assumes that there is
3905 a single reference to an argument in a7, but with small complex modes the
3906 real and imaginary components may be extracted separately, leading to two
3907 uses of the register, only one of which would be replaced. */
3908
3909 static bool
3910 xtensa_member_type_forces_blk (const_tree, machine_mode mode)
3911 {
3912 return mode == CQImode || mode == CHImode;
3913 }
3914
3915 /* Create the va_list data type.
3916
3917 This structure is set up by __builtin_saveregs. The __va_reg field
3918 points to a stack-allocated region holding the contents of the
3919 incoming argument registers. The __va_ndx field is an index
3920 initialized to the position of the first unnamed (variable)
3921 argument. This same index is also used to address the arguments
3922 passed in memory. Thus, the __va_stk field is initialized to point
3923 to the position of the first argument in memory offset to account
3924 for the arguments passed in registers and to account for the size
3925 of the argument registers not being 16-byte aligned. E.G., there
3926 are 6 argument registers of 4 bytes each, but we want the __va_ndx
3927 for the first stack argument to have the maximal alignment of 16
3928 bytes, so we offset the __va_stk address by 32 bytes so that
3929 __va_stk[32] references the first argument on the stack. */
3930
3931 static tree
3932 xtensa_build_builtin_va_list (void)
3933 {
3934 tree f_stk, f_reg, f_ndx, record, type_decl;
3935
3936 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3937 type_decl = build_decl (BUILTINS_LOCATION,
3938 TYPE_DECL, get_identifier ("__va_list_tag"), record);
3939
3940 f_stk = build_decl (BUILTINS_LOCATION,
3941 FIELD_DECL, get_identifier ("__va_stk"),
3942 ptr_type_node);
3943 f_reg = build_decl (BUILTINS_LOCATION,
3944 FIELD_DECL, get_identifier ("__va_reg"),
3945 ptr_type_node);
3946 f_ndx = build_decl (BUILTINS_LOCATION,
3947 FIELD_DECL, get_identifier ("__va_ndx"),
3948 integer_type_node);
3949
3950 DECL_FIELD_CONTEXT (f_stk) = record;
3951 DECL_FIELD_CONTEXT (f_reg) = record;
3952 DECL_FIELD_CONTEXT (f_ndx) = record;
3953
3954 TYPE_STUB_DECL (record) = type_decl;
3955 TYPE_NAME (record) = type_decl;
3956 TYPE_FIELDS (record) = f_stk;
3957 DECL_CHAIN (f_stk) = f_reg;
3958 DECL_CHAIN (f_reg) = f_ndx;
3959
3960 layout_type (record);
3961 return record;
3962 }
3963
3964
3965 /* Save the incoming argument registers on the stack. Returns the
3966 address of the saved registers. */
3967
3968 static rtx
3969 xtensa_builtin_saveregs (void)
3970 {
3971 rtx gp_regs;
3972 int arg_words = crtl->args.info.arg_words;
3973 int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
3974
3975 if (gp_left <= 0)
3976 return const0_rtx;
3977
3978 /* Allocate the general-purpose register space. */
3979 gp_regs = assign_stack_local
3980 (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
3981 set_mem_alias_set (gp_regs, get_varargs_alias_set ());
3982
3983 /* Now store the incoming registers. */
3984 cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
3985 cfun->machine->vararg_a7 = true;
3986 move_block_from_reg (GP_ARG_FIRST + arg_words,
3987 adjust_address (gp_regs, BLKmode,
3988 arg_words * UNITS_PER_WORD),
3989 gp_left);
3990 if (cfun->machine->vararg_a7_copy != 0)
3991 emit_insn_before (cfun->machine->vararg_a7_copy, get_insns ());
3992
3993 return XEXP (gp_regs, 0);
3994 }
3995
3996
3997 /* Implement `va_start' for varargs and stdarg. We look at the
3998 current function to fill in an initial va_list. */
3999
4000 static void
4001 xtensa_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
4002 {
4003 tree f_stk, stk;
4004 tree f_reg, reg;
4005 tree f_ndx, ndx;
4006 tree t, u;
4007 int arg_words;
4008
4009 arg_words = crtl->args.info.arg_words;
4010
4011 f_stk = TYPE_FIELDS (va_list_type_node);
4012 f_reg = DECL_CHAIN (f_stk);
4013 f_ndx = DECL_CHAIN (f_reg);
4014
4015 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk, NULL_TREE);
4016 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
4017 f_reg, NULL_TREE);
4018 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
4019 f_ndx, NULL_TREE);
4020
4021 /* Call __builtin_saveregs; save the result in __va_reg */
4022 u = make_tree (sizetype, expand_builtin_saveregs ());
4023 u = fold_convert (ptr_type_node, u);
4024 t = build2 (MODIFY_EXPR, ptr_type_node, reg, u);
4025 TREE_SIDE_EFFECTS (t) = 1;
4026 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4027
4028 /* Set the __va_stk member to ($arg_ptr - 32). */
4029 u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
4030 u = fold_build_pointer_plus_hwi (u, -32);
4031 t = build2 (MODIFY_EXPR, ptr_type_node, stk, u);
4032 TREE_SIDE_EFFECTS (t) = 1;
4033 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4034
4035 /* Set the __va_ndx member. If the first variable argument is on
4036 the stack, adjust __va_ndx by 2 words to account for the extra
4037 alignment offset for __va_stk. */
4038 if (arg_words >= MAX_ARGS_IN_REGISTERS)
4039 arg_words += 2;
4040 t = build2 (MODIFY_EXPR, integer_type_node, ndx,
4041 build_int_cst (integer_type_node, arg_words * UNITS_PER_WORD));
4042 TREE_SIDE_EFFECTS (t) = 1;
4043 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4044 }
4045
4046
4047 /* Implement `va_arg'. */
4048
4049 static tree
4050 xtensa_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4051 gimple_seq *post_p ATTRIBUTE_UNUSED)
4052 {
4053 tree f_stk, stk;
4054 tree f_reg, reg;
4055 tree f_ndx, ndx;
4056 tree type_size, array, orig_ndx, addr, size, va_size, t;
4057 tree lab_false, lab_over, lab_false2;
4058 bool indirect;
4059
4060 indirect = pass_va_arg_by_reference (type);
4061 if (indirect)
4062 type = build_pointer_type (type);
4063
4064 /* Handle complex values as separate real and imaginary parts. */
4065 if (TREE_CODE (type) == COMPLEX_TYPE)
4066 {
4067 tree real_part, imag_part;
4068
4069 real_part = xtensa_gimplify_va_arg_expr (valist, TREE_TYPE (type),
4070 pre_p, NULL);
4071 real_part = get_initialized_tmp_var (real_part, pre_p, NULL);
4072
4073 imag_part = xtensa_gimplify_va_arg_expr (unshare_expr (valist),
4074 TREE_TYPE (type),
4075 pre_p, NULL);
4076 imag_part = get_initialized_tmp_var (imag_part, pre_p, NULL);
4077
4078 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
4079 }
4080
4081 f_stk = TYPE_FIELDS (va_list_type_node);
4082 f_reg = DECL_CHAIN (f_stk);
4083 f_ndx = DECL_CHAIN (f_reg);
4084
4085 stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist,
4086 f_stk, NULL_TREE);
4087 reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
4088 f_reg, NULL_TREE);
4089 ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
4090 f_ndx, NULL_TREE);
4091
4092 type_size = size_in_bytes (type);
4093 va_size = round_up (type_size, UNITS_PER_WORD);
4094 gimplify_expr (&va_size, pre_p, NULL, is_gimple_val, fb_rvalue);
4095
4096
4097 /* First align __va_ndx if necessary for this arg:
4098
4099 orig_ndx = (AP).__va_ndx;
4100 if (__alignof__ (TYPE) > 4 )
4101 orig_ndx = ((orig_ndx + __alignof__ (TYPE) - 1)
4102 & -__alignof__ (TYPE)); */
4103
4104 orig_ndx = get_initialized_tmp_var (ndx, pre_p, NULL);
4105
4106 if (TYPE_ALIGN (type) > BITS_PER_WORD)
4107 {
4108 int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_UNIT;
4109
4110 t = build2 (PLUS_EXPR, integer_type_node, unshare_expr (orig_ndx),
4111 build_int_cst (integer_type_node, align - 1));
4112 t = build2 (BIT_AND_EXPR, integer_type_node, t,
4113 build_int_cst (integer_type_node, -align));
4114 gimplify_assign (unshare_expr (orig_ndx), t, pre_p);
4115 }
4116
4117
4118 /* Increment __va_ndx to point past the argument:
4119
4120 (AP).__va_ndx = orig_ndx + __va_size (TYPE); */
4121
4122 t = fold_convert (integer_type_node, va_size);
4123 t = build2 (PLUS_EXPR, integer_type_node, orig_ndx, t);
4124 gimplify_assign (unshare_expr (ndx), t, pre_p);
4125
4126
4127 /* Check if the argument is in registers:
4128
4129 if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
4130 && !must_pass_in_stack (type))
4131 __array = (AP).__va_reg; */
4132
4133 array = create_tmp_var (ptr_type_node);
4134
4135 lab_over = NULL;
4136 if (!must_pass_va_arg_in_stack (type))
4137 {
4138 lab_false = create_artificial_label (UNKNOWN_LOCATION);
4139 lab_over = create_artificial_label (UNKNOWN_LOCATION);
4140
4141 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (ndx),
4142 build_int_cst (integer_type_node,
4143 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
4144 t = build3 (COND_EXPR, void_type_node, t,
4145 build1 (GOTO_EXPR, void_type_node, lab_false),
4146 NULL_TREE);
4147 gimplify_and_add (t, pre_p);
4148
4149 gimplify_assign (unshare_expr (array), reg, pre_p);
4150
4151 t = build1 (GOTO_EXPR, void_type_node, lab_over);
4152 gimplify_and_add (t, pre_p);
4153
4154 t = build1 (LABEL_EXPR, void_type_node, lab_false);
4155 gimplify_and_add (t, pre_p);
4156 }
4157
4158
4159 /* ...otherwise, the argument is on the stack (never split between
4160 registers and the stack -- change __va_ndx if necessary):
4161
4162 else
4163 {
4164 if (orig_ndx <= __MAX_ARGS_IN_REGISTERS * 4)
4165 (AP).__va_ndx = 32 + __va_size (TYPE);
4166 __array = (AP).__va_stk;
4167 } */
4168
4169 lab_false2 = create_artificial_label (UNKNOWN_LOCATION);
4170
4171 t = build2 (GT_EXPR, boolean_type_node, unshare_expr (orig_ndx),
4172 build_int_cst (integer_type_node,
4173 MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
4174 t = build3 (COND_EXPR, void_type_node, t,
4175 build1 (GOTO_EXPR, void_type_node, lab_false2),
4176 NULL_TREE);
4177 gimplify_and_add (t, pre_p);
4178
4179 t = size_binop (PLUS_EXPR, unshare_expr (va_size), size_int (32));
4180 t = fold_convert (integer_type_node, t);
4181 gimplify_assign (unshare_expr (ndx), t, pre_p);
4182
4183 t = build1 (LABEL_EXPR, void_type_node, lab_false2);
4184 gimplify_and_add (t, pre_p);
4185
4186 gimplify_assign (array, stk, pre_p);
4187
4188 if (lab_over)
4189 {
4190 t = build1 (LABEL_EXPR, void_type_node, lab_over);
4191 gimplify_and_add (t, pre_p);
4192 }
4193
4194
4195 /* Given the base array pointer (__array) and index to the subsequent
4196 argument (__va_ndx), find the address:
4197
4198 __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
4199 ? sizeof (TYPE)
4200 : __va_size (TYPE))
4201
4202 The results are endian-dependent because values smaller than one word
4203 are aligned differently. */
4204
4205
4206 if (BYTES_BIG_ENDIAN && TREE_CODE (type_size) == INTEGER_CST)
4207 {
4208 t = fold_build2 (GE_EXPR, boolean_type_node, unshare_expr (type_size),
4209 size_int (PARM_BOUNDARY / BITS_PER_UNIT));
4210 t = fold_build3 (COND_EXPR, sizetype, t, unshare_expr (va_size),
4211 unshare_expr (type_size));
4212 size = t;
4213 }
4214 else
4215 size = unshare_expr (va_size);
4216
4217 t = fold_convert (sizetype, unshare_expr (ndx));
4218 t = build2 (MINUS_EXPR, sizetype, t, size);
4219 addr = fold_build_pointer_plus (unshare_expr (array), t);
4220
4221 addr = fold_convert (build_pointer_type (type), addr);
4222 if (indirect)
4223 addr = build_va_arg_indirect_ref (addr);
4224 return build_va_arg_indirect_ref (addr);
4225 }
4226
4227
4228 /* Builtins. */
4229
4230 enum xtensa_builtin
4231 {
4232 XTENSA_BUILTIN_UMULSIDI3,
4233 XTENSA_BUILTIN_max
4234 };
4235
4236
4237 static void
4238 xtensa_init_builtins (void)
4239 {
4240 tree ftype, decl;
4241
4242 ftype = build_function_type_list (unsigned_intDI_type_node,
4243 unsigned_intSI_type_node,
4244 unsigned_intSI_type_node, NULL_TREE);
4245
4246 decl = add_builtin_function ("__builtin_umulsidi3", ftype,
4247 XTENSA_BUILTIN_UMULSIDI3, BUILT_IN_MD,
4248 "__umulsidi3", NULL_TREE);
4249 TREE_NOTHROW (decl) = 1;
4250 TREE_READONLY (decl) = 1;
4251 }
4252
4253
4254 static tree
4255 xtensa_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED, tree *args,
4256 bool ignore ATTRIBUTE_UNUSED)
4257 {
4258 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
4259 tree arg0, arg1;
4260
4261 switch (fcode)
4262 {
4263 case XTENSA_BUILTIN_UMULSIDI3:
4264 arg0 = args[0];
4265 arg1 = args[1];
4266 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
4267 || TARGET_MUL32_HIGH)
4268 return fold_build2 (MULT_EXPR, unsigned_intDI_type_node,
4269 fold_convert (unsigned_intDI_type_node, arg0),
4270 fold_convert (unsigned_intDI_type_node, arg1));
4271 break;
4272
4273 default:
4274 internal_error ("bad builtin code");
4275 break;
4276 }
4277
4278 return NULL;
4279 }
4280
4281
4282 static rtx
4283 xtensa_expand_builtin (tree exp, rtx target,
4284 rtx subtarget ATTRIBUTE_UNUSED,
4285 machine_mode mode ATTRIBUTE_UNUSED,
4286 int ignore)
4287 {
4288 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4289 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
4290
4291 switch (fcode)
4292 {
4293 case XTENSA_BUILTIN_UMULSIDI3:
4294 /* The umulsidi3 builtin is just a mechanism to avoid calling the real
4295 __umulsidi3 function when the Xtensa configuration can directly
4296 implement it. If not, just call the function. */
4297 return expand_call (exp, target, ignore);
4298
4299 default:
4300 internal_error ("bad builtin code");
4301 }
4302 return NULL_RTX;
4303 }
4304
4305 /* Worker function for TARGET_PREFERRED_RELOAD_CLASS. */
4306
4307 static reg_class_t
4308 xtensa_preferred_reload_class (rtx x, reg_class_t rclass)
4309 {
4310 if (CONSTANT_P (x) && CONST_DOUBLE_P (x))
4311 return NO_REGS;
4312
4313 /* Don't use the stack pointer or hard frame pointer for reloads!
4314 The hard frame pointer would normally be OK except that it may
4315 briefly hold an incoming argument in the prologue, and reload
4316 won't know that it is live because the hard frame pointer is
4317 treated specially. */
4318
4319 if (rclass == AR_REGS || rclass == GR_REGS)
4320 return RL_REGS;
4321
4322 return rclass;
4323 }
4324
4325 /* Worker function for TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
4326
4327 static reg_class_t
4328 xtensa_preferred_output_reload_class (rtx x ATTRIBUTE_UNUSED,
4329 reg_class_t rclass)
4330 {
4331 /* Don't use the stack pointer or hard frame pointer for reloads!
4332 The hard frame pointer would normally be OK except that it may
4333 briefly hold an incoming argument in the prologue, and reload
4334 won't know that it is live because the hard frame pointer is
4335 treated specially. */
4336
4337 if (rclass == AR_REGS || rclass == GR_REGS)
4338 return RL_REGS;
4339
4340 return rclass;
4341 }
4342
4343 /* Worker function for TARGET_SECONDARY_RELOAD. */
4344
4345 static reg_class_t
4346 xtensa_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
4347 machine_mode mode, secondary_reload_info *sri)
4348 {
4349 int regno;
4350
4351 if (in_p && constantpool_mem_p (x))
4352 {
4353 if (rclass == FP_REGS)
4354 return RL_REGS;
4355
4356 if (mode == QImode)
4357 sri->icode = CODE_FOR_reloadqi_literal;
4358 else if (mode == HImode)
4359 sri->icode = CODE_FOR_reloadhi_literal;
4360 }
4361
4362 regno = xt_true_regnum (x);
4363 if (ACC_REG_P (regno))
4364 return ((rclass == GR_REGS || rclass == RL_REGS) ? NO_REGS : RL_REGS);
4365 if (rclass == ACC_REG)
4366 return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
4367
4368 return NO_REGS;
4369 }
4370
4371 /* Called once at the start of IRA, by ADJUST_REG_ALLOC_ORDER. */
4372
4373 void
4374 xtensa_adjust_reg_alloc_order (void)
4375 {
4376 static const int reg_windowed_alloc_order[FIRST_PSEUDO_REGISTER] =
4377 REG_ALLOC_ORDER;
4378 static const int reg_call0_alloc_order[FIRST_PSEUDO_REGISTER] =
4379 {
4380 9, 10, 11, 7, 6, 5, 4, 3, 2, 8, 0, 12, 13, 14, 15,
4381 18,
4382 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
4383 1, 16, 17,
4384 35,
4385 };
4386
4387 memcpy (reg_alloc_order, TARGET_WINDOWED_ABI ?
4388 reg_windowed_alloc_order : reg_call0_alloc_order,
4389 FIRST_PSEUDO_REGISTER * sizeof (int));
4390 }
4391
4392
4393 /* Some Xtensa targets support multiple bss sections. If the section
4394 name ends with ".bss", add SECTION_BSS to the flags. */
4395
4396 static unsigned int
4397 xtensa_multibss_section_type_flags (tree decl, const char *name, int reloc)
4398 {
4399 unsigned int flags = default_section_type_flags (decl, name, reloc);
4400 const char *suffix;
4401
4402 suffix = strrchr (name, '.');
4403 if (suffix && strcmp (suffix, ".bss") == 0)
4404 {
4405 if (!decl || (VAR_P (decl)
4406 && DECL_INITIAL (decl) == NULL_TREE))
4407 flags |= SECTION_BSS; /* @nobits */
4408 else
4409 warning (0, "only uninitialized variables can be placed in a "
4410 "%<.bss%> section");
4411 }
4412
4413 return flags;
4414 }
4415
4416
4417 /* The literal pool stays with the function. */
4418
4419 static section *
4420 xtensa_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
4421 rtx x ATTRIBUTE_UNUSED,
4422 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4423 {
4424 return function_section (current_function_decl);
4425 }
4426
4427 /* Worker function for TARGET_REGISTER_MOVE_COST. */
4428
4429 static int
4430 xtensa_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
4431 reg_class_t from, reg_class_t to)
4432 {
4433 if (from == to && from != BR_REGS && to != BR_REGS)
4434 return 2;
4435 else if (reg_class_subset_p (from, AR_REGS)
4436 && reg_class_subset_p (to, AR_REGS))
4437 return 2;
4438 else if (reg_class_subset_p (from, AR_REGS) && to == ACC_REG)
4439 return 3;
4440 else if (from == ACC_REG && reg_class_subset_p (to, AR_REGS))
4441 return 3;
4442 else
4443 return 10;
4444 }
4445
4446 /* Compute a (partial) cost for rtx X. Return true if the complete
4447 cost has been computed, and false if subexpressions should be
4448 scanned. In either case, *TOTAL contains the cost result. */
4449
4450 static bool
4451 xtensa_rtx_costs (rtx x, machine_mode mode, int outer_code,
4452 int opno ATTRIBUTE_UNUSED,
4453 int *total, bool speed)
4454 {
4455 int code = GET_CODE (x);
4456
4457 switch (code)
4458 {
4459 case CONST_INT:
4460 switch (outer_code)
4461 {
4462 case SET:
4463 if (xtensa_simm12b (INTVAL (x))
4464 || (current_pass && current_pass->tv_id == TV_IFCVT))
4465 {
4466 *total = speed ? COSTS_N_INSNS (1) : 0;
4467 return true;
4468 }
4469 break;
4470 case PLUS:
4471 if (xtensa_simm8 (INTVAL (x))
4472 || xtensa_simm8x256 (INTVAL (x)))
4473 {
4474 *total = 0;
4475 return true;
4476 }
4477 break;
4478 case AND:
4479 if (xtensa_mask_immediate (INTVAL (x)))
4480 {
4481 *total = 0;
4482 return true;
4483 }
4484 break;
4485 case COMPARE:
4486 if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
4487 {
4488 *total = 0;
4489 return true;
4490 }
4491 break;
4492 case ASHIFT:
4493 case ASHIFTRT:
4494 case LSHIFTRT:
4495 case ROTATE:
4496 case ROTATERT:
4497 /* No way to tell if X is the 2nd operand so be conservative. */
4498 default: break;
4499 }
4500 if (xtensa_simm12b (INTVAL (x)))
4501 *total = 5;
4502 else if (TARGET_CONST16)
4503 *total = COSTS_N_INSNS (2);
4504 else
4505 *total = 6;
4506 return true;
4507
4508 case CONST:
4509 case LABEL_REF:
4510 case SYMBOL_REF:
4511 if (TARGET_CONST16)
4512 *total = COSTS_N_INSNS (2);
4513 else
4514 *total = 5;
4515 return true;
4516
4517 case CONST_DOUBLE:
4518 if (TARGET_CONST16)
4519 *total = COSTS_N_INSNS (4);
4520 else
4521 *total = 7;
4522 return true;
4523
4524 case MEM:
4525 {
4526 int num_words =
4527 (GET_MODE_SIZE (mode) > UNITS_PER_WORD) ? 2 : 1;
4528
4529 if (memory_address_p (mode, XEXP ((x), 0)))
4530 *total = COSTS_N_INSNS (num_words);
4531 else
4532 *total = COSTS_N_INSNS (2*num_words);
4533 return true;
4534 }
4535
4536 case FFS:
4537 case CTZ:
4538 *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
4539 return true;
4540
4541 case CLZ:
4542 case CLRSB:
4543 *total = COSTS_N_INSNS (TARGET_NSA ? 1 : 50);
4544 return true;
4545
4546 case BSWAP:
4547 *total = COSTS_N_INSNS (mode == HImode ? 3 : 5);
4548 return true;
4549
4550 case NOT:
4551 *total = COSTS_N_INSNS (mode == DImode ? 3 : 2);
4552 return true;
4553
4554 case AND:
4555 case IOR:
4556 case XOR:
4557 if (mode == DImode)
4558 *total = COSTS_N_INSNS (2);
4559 else
4560 *total = COSTS_N_INSNS (1);
4561 return true;
4562
4563 case ASHIFT:
4564 case ASHIFTRT:
4565 case LSHIFTRT:
4566 if (mode == DImode)
4567 *total = COSTS_N_INSNS (50);
4568 else
4569 *total = COSTS_N_INSNS (1);
4570 return true;
4571
4572 case ABS:
4573 case NEG:
4574 {
4575 if (mode == SFmode)
4576 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
4577 else if (mode == DFmode)
4578 *total = COSTS_N_INSNS (50);
4579 else if (mode == DImode)
4580 *total = COSTS_N_INSNS (4);
4581 else
4582 *total = COSTS_N_INSNS (1);
4583 return true;
4584 }
4585
4586 case PLUS:
4587 case MINUS:
4588 {
4589 if (mode == SFmode)
4590 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
4591 else if (mode == DFmode || mode == DImode)
4592 *total = COSTS_N_INSNS (50);
4593 else
4594 *total = COSTS_N_INSNS (1);
4595 return true;
4596 }
4597
4598 case MULT:
4599 {
4600 if (mode == SFmode)
4601 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
4602 else if (mode == DFmode)
4603 *total = COSTS_N_INSNS (50);
4604 else if (mode == DImode)
4605 *total = COSTS_N_INSNS (TARGET_MUL32_HIGH ? 10 : 50);
4606 else if (TARGET_MUL32)
4607 *total = COSTS_N_INSNS (4);
4608 else if (TARGET_MAC16)
4609 *total = COSTS_N_INSNS (16);
4610 else if (TARGET_MUL16)
4611 *total = COSTS_N_INSNS (12);
4612 else
4613 *total = COSTS_N_INSNS (50);
4614 return true;
4615 }
4616
4617 case DIV:
4618 case MOD:
4619 {
4620 if (mode == SFmode)
4621 {
4622 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
4623 return true;
4624 }
4625 else if (mode == DFmode)
4626 {
4627 *total = COSTS_N_INSNS (50);
4628 return true;
4629 }
4630 }
4631 /* Fall through. */
4632
4633 case UDIV:
4634 case UMOD:
4635 {
4636 if (mode == DImode)
4637 *total = COSTS_N_INSNS (speed ? 100 : 50);
4638 else if (TARGET_DIV32)
4639 *total = COSTS_N_INSNS (32);
4640 else
4641 *total = COSTS_N_INSNS (speed ? 100 : 50);
4642 return true;
4643 }
4644
4645 case SQRT:
4646 if (mode == SFmode)
4647 *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
4648 else
4649 *total = COSTS_N_INSNS (50);
4650 return true;
4651
4652 case SMIN:
4653 case UMIN:
4654 case SMAX:
4655 case UMAX:
4656 *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
4657 return true;
4658
4659 case SIGN_EXTRACT:
4660 case SIGN_EXTEND:
4661 *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
4662 return true;
4663
4664 case ZERO_EXTRACT:
4665 case ZERO_EXTEND:
4666 case IF_THEN_ELSE:
4667 *total = COSTS_N_INSNS (1);
4668 return true;
4669
4670 default:
4671 return false;
4672 }
4673 }
4674
4675 static bool
4676 xtensa_is_insn_L32R_p (const rtx_insn *insn)
4677 {
4678 rtx x = PATTERN (insn);
4679
4680 if (GET_CODE (x) != SET)
4681 return false;
4682
4683 x = XEXP (x, 1);
4684 if (MEM_P (x))
4685 {
4686 x = XEXP (x, 0);
4687 return (SYMBOL_REF_P (x) || CONST_INT_P (x))
4688 && CONSTANT_POOL_ADDRESS_P (x);
4689 }
4690
4691 /* relaxed MOVI instructions, that will be converted to L32R by the
4692 assembler. */
4693 if (CONST_INT_P (x)
4694 && ! xtensa_simm12b (INTVAL (x)))
4695 return true;
4696
4697 return false;
4698 }
4699
4700 /* Compute a relative costs of RTL insns. This is necessary in order to
4701 achieve better RTL insn splitting/combination result. */
4702
4703 static int
4704 xtensa_insn_cost (rtx_insn *insn, bool speed)
4705 {
4706 if (!(recog_memoized (insn) < 0))
4707 {
4708 int len = get_attr_length (insn);
4709
4710 if (len == 0)
4711 return COSTS_N_INSNS (0);
4712
4713 if (speed) /* For speed cost. */
4714 {
4715 int n = (len + 2) / 3;
4716
4717 /* "L32R" may be particular slow (implementation-dependent). */
4718 if (xtensa_is_insn_L32R_p (insn))
4719 return COSTS_N_INSNS (1 + xtensa_extra_l32r_costs);
4720
4721 /* Cost based on the pipeline model. */
4722 switch (get_attr_type (insn))
4723 {
4724 case TYPE_STORE:
4725 case TYPE_MOVE:
4726 case TYPE_ARITH:
4727 case TYPE_MULTI:
4728 case TYPE_NOP:
4729 case TYPE_FARITH:
4730 case TYPE_FSTORE:
4731 return COSTS_N_INSNS (n);
4732
4733 case TYPE_LOAD:
4734 return COSTS_N_INSNS (n - 1 + 2);
4735
4736 case TYPE_JUMP:
4737 case TYPE_CALL:
4738 return COSTS_N_INSNS (n - 1 + 3);
4739
4740 case TYPE_FCONV:
4741 case TYPE_FLOAD:
4742 case TYPE_MUL16:
4743 case TYPE_MUL32:
4744 case TYPE_RSR:
4745 return COSTS_N_INSNS (n * 2);
4746
4747 case TYPE_FMADD:
4748 return COSTS_N_INSNS (n * 4);
4749
4750 case TYPE_DIV32:
4751 return COSTS_N_INSNS (n * 16);
4752
4753 default:
4754 break;
4755 }
4756 }
4757 else /* For size cost. */
4758 {
4759 /* Cost based on the instruction length. */
4760 if (get_attr_type (insn) != TYPE_UNKNOWN)
4761 {
4762 /* "L32R" itself plus constant in litpool. */
4763 if (xtensa_is_insn_L32R_p (insn))
4764 len = 3 + 4;
4765
4766 /* Consider fractional instruction length (for example, ".n"
4767 short instructions or "L32R" litpool constants. */
4768 return (COSTS_N_INSNS (len) + 1) / 3;
4769 }
4770 }
4771 }
4772
4773 /* Fall back. */
4774 return pattern_cost (PATTERN (insn), speed);
4775 }
4776
4777 /* Worker function for TARGET_RETURN_IN_MEMORY. */
4778
4779 static bool
4780 xtensa_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
4781 {
4782 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
4783 > (unsigned) (GP_RETURN_LAST - GP_RETURN_FIRST + 1) * UNITS_PER_WORD);
4784 }
4785
4786 /* Worker function for TARGET_FUNCTION_VALUE. */
4787
4788 rtx
4789 xtensa_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
4790 bool outgoing)
4791 {
4792 return gen_rtx_REG ((INTEGRAL_TYPE_P (valtype)
4793 && TYPE_PRECISION (valtype) < BITS_PER_WORD)
4794 ? SImode : TYPE_MODE (valtype),
4795 outgoing ? GP_OUTGOING_RETURN : GP_RETURN_FIRST);
4796 }
4797
4798 /* Worker function for TARGET_LIBCALL_VALUE. */
4799
4800 static rtx
4801 xtensa_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
4802 {
4803 return gen_rtx_REG ((GET_MODE_CLASS (mode) == MODE_INT
4804 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4805 ? SImode : mode, GP_RETURN_FIRST);
4806 }
4807
4808 /* Worker function TARGET_FUNCTION_VALUE_REGNO_P. */
4809
4810 static bool
4811 xtensa_function_value_regno_p (const unsigned int regno)
4812 {
4813 return IN_RANGE (regno, GP_RETURN_FIRST, GP_RETURN_LAST);
4814 }
4815
4816 /* The static chain is passed in memory. Provide rtx giving 'mem'
4817 expressions that denote where they are stored. */
4818
4819 static rtx
4820 xtensa_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
4821 {
4822 if (TARGET_WINDOWED_ABI)
4823 {
4824 rtx base = incoming_p ? arg_pointer_rtx : stack_pointer_rtx;
4825 return gen_frame_mem (Pmode, plus_constant (Pmode, base,
4826 -5 * UNITS_PER_WORD));
4827 }
4828 else
4829 return gen_rtx_REG (Pmode, A8_REG);
4830 }
4831
4832
4833 /* TRAMPOLINE_TEMPLATE: For Xtensa, the trampoline must perform an ENTRY
4834 instruction with a minimal stack frame in order to get some free
4835 registers. Once the actual call target is known, the proper stack frame
4836 size is extracted from the ENTRY instruction at the target and the
4837 current frame is adjusted to match. The trampoline then transfers
4838 control to the instruction following the ENTRY at the target. Note:
4839 this assumes that the target begins with an ENTRY instruction. */
4840
4841 static void
4842 xtensa_asm_trampoline_template (FILE *stream)
4843 {
4844 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
4845
4846 fprintf (stream, "\t.begin no-transform\n");
4847
4848 if (TARGET_WINDOWED_ABI)
4849 {
4850 fprintf (stream, "\tentry\tsp, %d\n", MIN_FRAME_SIZE);
4851
4852 if (use_call0)
4853 {
4854 /* Save the return address. */
4855 fprintf (stream, "\tmov\ta10, a0\n");
4856
4857 /* Use a CALL0 instruction to skip past the constants and in the
4858 process get the PC into A0. This allows PC-relative access to
4859 the constants without relying on L32R. */
4860 fprintf (stream, "\tcall0\t.Lskipconsts\n");
4861 }
4862 else
4863 fprintf (stream, "\tj\t.Lskipconsts\n");
4864
4865 fprintf (stream, "\t.align\t4\n");
4866 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
4867 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
4868 fprintf (stream, ".Lskipconsts:\n");
4869
4870 /* Load the static chain and function address from the trampoline. */
4871 if (use_call0)
4872 {
4873 fprintf (stream, "\taddi\ta0, a0, 3\n");
4874 fprintf (stream, "\tl32i\ta9, a0, 0\n");
4875 fprintf (stream, "\tl32i\ta8, a0, 4\n");
4876 }
4877 else
4878 {
4879 fprintf (stream, "\tl32r\ta9, .Lchainval\n");
4880 fprintf (stream, "\tl32r\ta8, .Lfnaddr\n");
4881 }
4882
4883 /* Store the static chain. */
4884 fprintf (stream, "\ts32i\ta9, sp, %d\n", MIN_FRAME_SIZE - 20);
4885
4886 /* Set the proper stack pointer value. */
4887 fprintf (stream, "\tl32i\ta9, a8, 0\n");
4888 fprintf (stream, "\textui\ta9, a9, %d, 12\n",
4889 TARGET_BIG_ENDIAN ? 8 : 12);
4890 fprintf (stream, "\tslli\ta9, a9, 3\n");
4891 fprintf (stream, "\taddi\ta9, a9, %d\n", -MIN_FRAME_SIZE);
4892 fprintf (stream, "\tsub\ta9, sp, a9\n");
4893 fprintf (stream, "\tmovsp\tsp, a9\n");
4894
4895 if (use_call0)
4896 /* Restore the return address. */
4897 fprintf (stream, "\tmov\ta0, a10\n");
4898
4899 /* Jump to the instruction following the ENTRY. */
4900 fprintf (stream, "\taddi\ta8, a8, 3\n");
4901 fprintf (stream, "\tjx\ta8\n");
4902
4903 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
4904 if (use_call0)
4905 fprintf (stream, "\t.byte\t0\n");
4906 else
4907 fprintf (stream, "\tnop\n");
4908 }
4909 else
4910 {
4911 if (use_call0)
4912 {
4913 /* Save the return address. */
4914 fprintf (stream, "\tmov\ta10, a0\n");
4915
4916 /* Use a CALL0 instruction to skip past the constants and in the
4917 process get the PC into A0. This allows PC-relative access to
4918 the constants without relying on L32R. */
4919 fprintf (stream, "\tcall0\t.Lskipconsts\n");
4920 }
4921 else
4922 fprintf (stream, "\tj\t.Lskipconsts\n");
4923
4924 fprintf (stream, "\t.align\t4\n");
4925 fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
4926 fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
4927 fprintf (stream, ".Lskipconsts:\n");
4928
4929 /* Load the static chain and function address from the trampoline. */
4930 if (use_call0)
4931 {
4932 fprintf (stream, "\taddi\ta0, a0, 3\n");
4933 fprintf (stream, "\tl32i\ta8, a0, 0\n");
4934 fprintf (stream, "\tl32i\ta9, a0, 4\n");
4935 fprintf (stream, "\tmov\ta0, a10\n");
4936 }
4937 else
4938 {
4939 fprintf (stream, "\tl32r\ta8, .Lchainval\n");
4940 fprintf (stream, "\tl32r\ta9, .Lfnaddr\n");
4941 }
4942 fprintf (stream, "\tjx\ta9\n");
4943
4944 /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
4945 if (use_call0)
4946 fprintf (stream, "\t.byte\t0\n");
4947 else
4948 fprintf (stream, "\tnop\n");
4949 }
4950 fprintf (stream, "\t.end no-transform\n");
4951 }
4952
4953 static void
4954 xtensa_trampoline_init (rtx m_tramp, tree fndecl, rtx chain)
4955 {
4956 rtx func = XEXP (DECL_RTL (fndecl), 0);
4957 bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
4958 int chain_off;
4959 int func_off;
4960
4961 if (TARGET_WINDOWED_ABI)
4962 {
4963 chain_off = use_call0 ? 12 : 8;
4964 func_off = use_call0 ? 16 : 12;
4965 }
4966 else
4967 {
4968 chain_off = use_call0 ? 8 : 4;
4969 func_off = use_call0 ? 12 : 8;
4970 }
4971
4972 emit_block_move (m_tramp, assemble_trampoline_template (),
4973 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4974
4975 emit_move_insn (adjust_address (m_tramp, SImode, chain_off), chain);
4976 emit_move_insn (adjust_address (m_tramp, SImode, func_off), func);
4977 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_sync_caches"),
4978 LCT_NORMAL, VOIDmode, XEXP (m_tramp, 0), Pmode);
4979 }
4980
4981 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
4982
4983 static bool
4984 xtensa_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
4985 {
4986 if (CONST_INT_P (x))
4987 return TARGET_AUTO_LITPOOLS || TARGET_CONST16
4988 || xtensa_simm12b (INTVAL (x));
4989
4990 return !xtensa_tls_referenced_p (x);
4991 }
4992
4993 /* Implement TARGET_CAN_USE_DOLOOP_P. */
4994
4995 static bool
4996 xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
4997 unsigned int loop_depth, bool entered_at_top)
4998 {
4999 /* Considering limitations in the hardware, only use doloop
5000 for innermost loops which must be entered from the top. */
5001 if (loop_depth > 1 || !entered_at_top)
5002 return false;
5003
5004 return true;
5005 }
5006
5007 /* NULL if INSN insn is valid within a low-overhead loop.
5008 Otherwise return why doloop cannot be applied. */
5009
5010 static const char *
5011 xtensa_invalid_within_doloop (const rtx_insn *insn)
5012 {
5013 if (CALL_P (insn))
5014 return "Function call in the loop.";
5015
5016 if (JUMP_P (insn) && INSN_CODE (insn) == CODE_FOR_return)
5017 return "Return from a call instruction in the loop.";
5018
5019 return NULL;
5020 }
5021
5022 /* Optimize LOOP. */
5023
5024 static bool
5025 hwloop_optimize (hwloop_info loop)
5026 {
5027 int i;
5028 edge entry_edge;
5029 basic_block entry_bb;
5030 rtx iter_reg;
5031 rtx_insn *insn, *seq, *entry_after;
5032
5033 if (loop->depth > 1)
5034 {
5035 if (dump_file)
5036 fprintf (dump_file, ";; loop %d is not innermost\n",
5037 loop->loop_no);
5038 return false;
5039 }
5040
5041 if (!loop->incoming_dest)
5042 {
5043 if (dump_file)
5044 fprintf (dump_file, ";; loop %d has more than one entry\n",
5045 loop->loop_no);
5046 return false;
5047 }
5048
5049 if (loop->incoming_dest != loop->head)
5050 {
5051 if (dump_file)
5052 fprintf (dump_file, ";; loop %d is not entered from head\n",
5053 loop->loop_no);
5054 return false;
5055 }
5056
5057 if (loop->has_call || loop->has_asm)
5058 {
5059 if (dump_file)
5060 fprintf (dump_file, ";; loop %d has invalid insn\n",
5061 loop->loop_no);
5062 return false;
5063 }
5064
5065 /* Scan all the blocks to make sure they don't use iter_reg. */
5066 if (loop->iter_reg_used || loop->iter_reg_used_outside)
5067 {
5068 if (dump_file)
5069 fprintf (dump_file, ";; loop %d uses iterator\n",
5070 loop->loop_no);
5071 return false;
5072 }
5073
5074 /* Check if start_label appears before doloop_end. */
5075 insn = loop->start_label;
5076 while (insn && insn != loop->loop_end)
5077 insn = NEXT_INSN (insn);
5078
5079 if (!insn)
5080 {
5081 if (dump_file)
5082 fprintf (dump_file, ";; loop %d start_label not before loop_end\n",
5083 loop->loop_no);
5084 return false;
5085 }
5086
5087 /* Get the loop iteration register. */
5088 iter_reg = loop->iter_reg;
5089
5090 gcc_assert (REG_P (iter_reg));
5091
5092 entry_edge = NULL;
5093
5094 FOR_EACH_VEC_SAFE_ELT (loop->incoming, i, entry_edge)
5095 if (entry_edge->flags & EDGE_FALLTHRU)
5096 break;
5097
5098 if (entry_edge == NULL)
5099 return false;
5100
5101 /* Place the zero_cost_loop_start instruction before the loop. */
5102 entry_bb = entry_edge->src;
5103
5104 start_sequence ();
5105
5106 insn = emit_insn (gen_zero_cost_loop_start (loop->iter_reg,
5107 loop->start_label,
5108 loop->iter_reg));
5109
5110 seq = get_insns ();
5111
5112 entry_after = BB_END (entry_bb);
5113 if (!single_succ_p (entry_bb) || vec_safe_length (loop->incoming) > 1
5114 || !entry_after)
5115 {
5116 basic_block new_bb;
5117 edge e;
5118 edge_iterator ei;
5119
5120 emit_insn_before (seq, BB_HEAD (loop->head));
5121 seq = emit_label_before (gen_label_rtx (), seq);
5122 new_bb = create_basic_block (seq, insn, entry_bb);
5123 FOR_EACH_EDGE (e, ei, loop->incoming)
5124 {
5125 if (!(e->flags & EDGE_FALLTHRU))
5126 redirect_edge_and_branch_force (e, new_bb);
5127 else
5128 redirect_edge_succ (e, new_bb);
5129 }
5130
5131 make_edge (new_bb, loop->head, 0);
5132 }
5133 else
5134 {
5135 while (DEBUG_INSN_P (entry_after)
5136 || (NOTE_P (entry_after)
5137 && NOTE_KIND (entry_after) != NOTE_INSN_BASIC_BLOCK))
5138 entry_after = PREV_INSN (entry_after);
5139
5140 emit_insn_after (seq, entry_after);
5141 }
5142
5143 end_sequence ();
5144
5145 return true;
5146 }
5147
5148 /* A callback for the hw-doloop pass. Called when a loop we have discovered
5149 turns out not to be optimizable; we have to split the loop_end pattern into
5150 a subtract and a test. */
5151
5152 static void
5153 hwloop_fail (hwloop_info loop)
5154 {
5155 rtx test;
5156 rtx_insn *insn = loop->loop_end;
5157
5158 emit_insn_before (gen_addsi3 (loop->iter_reg,
5159 loop->iter_reg,
5160 constm1_rtx),
5161 loop->loop_end);
5162
5163 test = gen_rtx_NE (VOIDmode, loop->iter_reg, const0_rtx);
5164 insn = emit_jump_insn_before (gen_cbranchsi4 (test,
5165 loop->iter_reg, const0_rtx,
5166 loop->start_label),
5167 loop->loop_end);
5168
5169 JUMP_LABEL (insn) = loop->start_label;
5170 LABEL_NUSES (loop->start_label)++;
5171 delete_insn (loop->loop_end);
5172 }
5173
5174 /* A callback for the hw-doloop pass. This function examines INSN; if
5175 it is a doloop_end pattern we recognize, return the reg rtx for the
5176 loop counter. Otherwise, return NULL_RTX. */
5177
5178 static rtx
5179 hwloop_pattern_reg (rtx_insn *insn)
5180 {
5181 rtx reg;
5182
5183 if (!JUMP_P (insn) || recog_memoized (insn) != CODE_FOR_loop_end)
5184 return NULL_RTX;
5185
5186 reg = SET_DEST (XVECEXP (PATTERN (insn), 0, 1));
5187 if (!REG_P (reg))
5188 return NULL_RTX;
5189
5190 return reg;
5191 }
5192
5193
5194 static struct hw_doloop_hooks xtensa_doloop_hooks =
5195 {
5196 hwloop_pattern_reg,
5197 hwloop_optimize,
5198 hwloop_fail
5199 };
5200
5201 /* Run from machine_dependent_reorg, this pass looks for doloop_end insns
5202 and tries to rewrite the RTL of these loops so that proper Xtensa
5203 hardware loops are generated. */
5204
5205 static void
5206 xtensa_reorg_loops (void)
5207 {
5208 if (TARGET_LOOPS)
5209 reorg_loops (false, &xtensa_doloop_hooks);
5210 }
5211
5212 /* Implement the TARGET_MACHINE_DEPENDENT_REORG pass. */
5213
5214 static void
5215 xtensa_reorg (void)
5216 {
5217 /* We are freeing block_for_insn in the toplev to keep compatibility
5218 with old MDEP_REORGS that are not CFG based. Recompute it now. */
5219 compute_bb_for_insn ();
5220
5221 df_analyze ();
5222
5223 /* Doloop optimization. */
5224 xtensa_reorg_loops ();
5225 }
5226
5227 /* Update register usage after having seen the compiler flags. */
5228
5229 static void
5230 xtensa_conditional_register_usage (void)
5231 {
5232 unsigned i, c_mask;
5233
5234 c_mask = TARGET_WINDOWED_ABI ? (1 << 1) : (1 << 2);
5235
5236 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5237 {
5238 /* Set/reset conditionally defined registers from
5239 CALL_USED_REGISTERS initializer. */
5240 if (call_used_regs[i] > 1)
5241 call_used_regs[i] = !!(call_used_regs[i] & c_mask);
5242 }
5243
5244 /* Remove hard FP register from the preferred reload registers set. */
5245 CLEAR_HARD_REG_BIT (reg_class_contents[(int)RL_REGS],
5246 HARD_FRAME_POINTER_REGNUM);
5247
5248 /* Register A0 holds the return address upon entry to a function
5249 for the CALL0 ABI, but unlike the windowed register ABI, it is
5250 not reserved for this purpose and may hold other values after
5251 the return address has been saved. */
5252 if (!TARGET_WINDOWED_ABI)
5253 fixed_regs[A0_REG] = 0;
5254 }
5255
5256 /* Map hard register number to register class */
5257
5258 enum reg_class xtensa_regno_to_class (int regno)
5259 {
5260 static const enum reg_class regno_to_class[FIRST_PSEUDO_REGISTER] =
5261 {
5262 RL_REGS, SP_REG, RL_REGS, RL_REGS,
5263 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
5264 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
5265 RL_REGS, RL_REGS, RL_REGS, RL_REGS,
5266 AR_REGS, AR_REGS, BR_REGS,
5267 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
5268 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
5269 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
5270 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
5271 ACC_REG,
5272 };
5273
5274 if (regno == HARD_FRAME_POINTER_REGNUM)
5275 return GR_REGS;
5276 else
5277 return regno_to_class[regno];
5278 }
5279
5280 /* Implement TARGET_CONSTANT_ALIGNMENT. Align string constants and
5281 constructors to at least a word boundary. The typical use of this
5282 macro is to increase alignment for string constants to be word
5283 aligned so that 'strcpy' calls that copy constants can be done
5284 inline. */
5285
5286 static HOST_WIDE_INT
5287 xtensa_constant_alignment (const_tree exp, HOST_WIDE_INT align)
5288 {
5289 if ((TREE_CODE (exp) == STRING_CST || TREE_CODE (exp) == CONSTRUCTOR)
5290 && !optimize_size)
5291 return MAX (align, BITS_PER_WORD);
5292 return align;
5293 }
5294
5295 static bool
5296 xtensa_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
5297 {
5298 gcc_assert (from == ARG_POINTER_REGNUM || from == FRAME_POINTER_REGNUM);
5299
5300 /* If we need a frame pointer, ARG_POINTER_REGNUM and FRAME_POINTER_REGNUM
5301 can only eliminate to HARD_FRAME_POINTER_REGNUM. */
5302 return to == HARD_FRAME_POINTER_REGNUM
5303 || (!frame_pointer_needed && to == STACK_POINTER_REGNUM);
5304 }
5305
5306 /* Implement TARGET_STARTING_FRAME_OFFSET. */
5307
5308 static HOST_WIDE_INT
5309 xtensa_starting_frame_offset (void)
5310 {
5311 if (FRAME_GROWS_DOWNWARD)
5312 return 0;
5313 return crtl->outgoing_args_size;
5314 }
5315
5316 /* Implement TARGET_ASAN_SHADOW_OFFSET. */
5317
5318 static unsigned HOST_WIDE_INT
5319 xtensa_asan_shadow_offset (void)
5320 {
5321 return HOST_WIDE_INT_UC (0x10000000);
5322 }
5323
5324 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
5325 static bool
5326 xtensa_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED, tree exp ATTRIBUTE_UNUSED)
5327 {
5328 /* Do not allow sibcalls when windowed registers ABI is in effect. */
5329 if (TARGET_WINDOWED_ABI)
5330 return false;
5331
5332 return true;
5333 }
5334
5335 static bool
5336 xtensa_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
5337 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
5338 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
5339 const_tree function ATTRIBUTE_UNUSED)
5340 {
5341 if (TARGET_WINDOWED_ABI)
5342 return false;
5343
5344 return true;
5345 }
5346
5347 /* Output code to add DELTA to the first argument, and then jump
5348 to FUNCTION. Used for C++ multiple inheritance. */
5349 static void
5350 xtensa_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
5351 HOST_WIDE_INT delta,
5352 HOST_WIDE_INT vcall_offset,
5353 tree function)
5354 {
5355 rtx this_rtx;
5356 rtx funexp;
5357 rtx_insn *insn;
5358 int this_reg_no;
5359 rtx temp0 = gen_rtx_REG (Pmode, A9_REG);
5360 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk));
5361
5362 reload_completed = 1;
5363
5364 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
5365 this_reg_no = 3;
5366 else
5367 this_reg_no = 2;
5368
5369 this_rtx = gen_rtx_REG (Pmode, A0_REG + this_reg_no);
5370
5371 if (delta)
5372 xtensa_emit_add_imm (this_rtx, this_rtx, delta, temp0, false);
5373
5374 if (vcall_offset)
5375 {
5376 rtx temp1 = gen_rtx_REG (Pmode, A0_REG + 10);
5377 rtx addr = temp1;
5378
5379 emit_move_insn (temp0, gen_rtx_MEM (Pmode, this_rtx));
5380 if (xtensa_uimm8x4 (vcall_offset))
5381 addr = plus_constant (Pmode, temp0, vcall_offset);
5382 else
5383 xtensa_emit_add_imm (temp1, temp0, vcall_offset, temp1, false);
5384 emit_move_insn (temp1, gen_rtx_MEM (Pmode, addr));
5385 emit_insn (gen_add2_insn (this_rtx, temp1));
5386 }
5387
5388 /* Generate a tail call to the target function. */
5389 if (!TREE_USED (function))
5390 {
5391 assemble_external (function);
5392 TREE_USED (function) = 1;
5393 }
5394
5395 funexp = XEXP (DECL_RTL (function), 0);
5396 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
5397 insn = emit_call_insn (gen_sibcall (funexp, const0_rtx));
5398 SIBLING_CALL_P (insn) = 1;
5399
5400 insn = get_insns ();
5401 shorten_branches (insn);
5402 assemble_start_function (thunk, fnname);
5403 final_start_function (insn, file, 1);
5404 final (insn, file, 1);
5405 final_end_function ();
5406 assemble_end_function (thunk, fnname);
5407
5408 /* Stop pretending to be a post-reload pass. */
5409 reload_completed = 0;
5410 }
5411
5412 static rtx
5413 xtensa_delegitimize_address (rtx op)
5414 {
5415 switch (GET_CODE (op))
5416 {
5417 case CONST:
5418 return xtensa_delegitimize_address (XEXP (op, 0));
5419
5420 case UNSPEC:
5421 if (XINT (op, 1) == UNSPEC_PLT)
5422 return XVECEXP(op, 0, 0);
5423 break;
5424
5425 default:
5426 break;
5427 }
5428 return op;
5429 }
5430
5431 #include "gt-xtensa.h"