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