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