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