OSDN Git Service

* config/xtensa/xtensa.c (xtensa_multibss_section_type_flags): Add
[pf3gnuchains/gcc-fork.git] / gcc / config / xtensa / xtensa.c
1 /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
2    Copyright 2001,2002,2003 Free Software Foundation, Inc.
3    Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
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 "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "insn-attr.h"
35 #include "insn-codes.h"
36 #include "recog.h"
37 #include "output.h"
38 #include "tree.h"
39 #include "expr.h"
40 #include "flags.h"
41 #include "reload.h"
42 #include "tm_p.h"
43 #include "function.h"
44 #include "toplev.h"
45 #include "optabs.h"
46 #include "libfuncs.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "langhooks.h"
51
52 /* Enumeration for all of the relational tests, so that we can build
53    arrays indexed by the test type, and not worry about the order
54    of EQ, NE, etc.  */
55
56 enum internal_test {
57     ITEST_EQ,
58     ITEST_NE,
59     ITEST_GT,
60     ITEST_GE,
61     ITEST_LT,
62     ITEST_LE,
63     ITEST_GTU,
64     ITEST_GEU,
65     ITEST_LTU,
66     ITEST_LEU,
67     ITEST_MAX
68   };
69
70 /* Cached operands, and operator to compare for use in set/branch on
71    condition codes.  */
72 rtx branch_cmp[2];
73
74 /* what type of branch to use */
75 enum cmp_type branch_type;
76
77 /* Array giving truth value on whether or not a given hard register
78    can support a given mode.  */
79 char xtensa_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
80
81 /* Current frame size calculated by compute_frame_size.  */
82 unsigned xtensa_current_frame_size;
83
84 /* Tables of ld/st opcode names for block moves */
85 const char *xtensa_ld_opcodes[(int) MAX_MACHINE_MODE];
86 const char *xtensa_st_opcodes[(int) MAX_MACHINE_MODE];
87 #define LARGEST_MOVE_RATIO 15
88
89 /* Define the structure for the machine field in struct function.  */
90 struct machine_function GTY(())
91 {
92   int accesses_prev_frame;
93   bool incoming_a7_copied;
94 };
95
96 /* Vector, indexed by hard register number, which contains 1 for a
97    register that is allowable in a candidate for leaf function
98    treatment.  */
99
100 const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
101 {
102   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103   1, 1, 1,
104   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
105   1
106 };
107
108 /* Map hard register number to register class */
109 const enum reg_class xtensa_regno_to_class[FIRST_PSEUDO_REGISTER] =
110 {
111   RL_REGS,      SP_REG,         RL_REGS,        RL_REGS,
112   RL_REGS,      RL_REGS,        RL_REGS,        GR_REGS,
113   RL_REGS,      RL_REGS,        RL_REGS,        RL_REGS,
114   RL_REGS,      RL_REGS,        RL_REGS,        RL_REGS,
115   AR_REGS,      AR_REGS,        BR_REGS,
116   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
117   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
118   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
119   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
120   ACC_REG,
121 };
122
123 /* Map register constraint character to register class.  */
124 enum reg_class xtensa_char_to_class[256] =
125 {
126   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
127   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
128   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
129   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
130   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
131   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
132   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
133   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
134   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
135   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
136   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
137   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
138   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
139   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
140   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
141   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
142   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
143   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
144   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
145   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
146   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
147   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
148   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
149   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
150   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
151   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
152   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
153   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
154   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
155   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
156   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
157   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
158   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
159   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
160   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
161   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
162   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
163   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
164   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
165   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
166   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
167   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
168   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
169   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
170   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
171   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
172   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
173   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
174   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
175   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
176   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
177   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
178   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
179   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
180   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
181   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
182   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
183   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
184   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
185   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
186   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
187   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
188   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
189   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
190 };
191
192 static int b4const_or_zero PARAMS ((int));
193 static enum internal_test map_test_to_internal_test PARAMS ((enum rtx_code));
194 static rtx gen_int_relational PARAMS ((enum rtx_code, rtx, rtx, int *));
195 static rtx gen_float_relational PARAMS ((enum rtx_code, rtx, rtx));
196 static rtx gen_conditional_move PARAMS ((rtx));
197 static rtx fixup_subreg_mem PARAMS ((rtx x));
198 static enum machine_mode xtensa_find_mode_for_size PARAMS ((unsigned));
199 static struct machine_function * xtensa_init_machine_status PARAMS ((void));
200 static void printx PARAMS ((FILE *, signed int));
201 static void xtensa_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
202 static unsigned int xtensa_multibss_section_type_flags
203   PARAMS ((tree, const char *, int)) ATTRIBUTE_UNUSED;
204 static void xtensa_select_rtx_section
205   PARAMS ((enum machine_mode, rtx, unsigned HOST_WIDE_INT));
206 static bool xtensa_rtx_costs PARAMS ((rtx, int, int, int *));
207
208 static int current_function_arg_words;
209 static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
210   REG_ALLOC_ORDER;
211 \f
212
213 /* This macro generates the assembly code for function exit,
214    on machines that need it.  If FUNCTION_EPILOGUE is not defined
215    then individual return instructions are generated for each
216    return statement.  Args are same as for FUNCTION_PROLOGUE.  */
217
218 #undef TARGET_ASM_FUNCTION_EPILOGUE
219 #define TARGET_ASM_FUNCTION_EPILOGUE xtensa_function_epilogue
220
221 /* These hooks specify assembly directives for creating certain kinds
222    of integer object.  */
223
224 #undef TARGET_ASM_ALIGNED_SI_OP
225 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
226
227 #undef TARGET_ASM_SELECT_RTX_SECTION
228 #define TARGET_ASM_SELECT_RTX_SECTION  xtensa_select_rtx_section
229
230 #undef TARGET_RTX_COSTS
231 #define TARGET_RTX_COSTS xtensa_rtx_costs
232 #undef TARGET_ADDRESS_COST
233 #define TARGET_ADDRESS_COST hook_int_rtx_0
234
235 struct gcc_target targetm = TARGET_INITIALIZER;
236 \f
237
238 /*
239  * Functions to test Xtensa immediate operand validity.
240  */
241
242 int
243 xtensa_b4constu (v)
244      int v;
245 {
246   switch (v)
247     {
248     case 32768:
249     case 65536:
250     case 2:
251     case 3:
252     case 4:
253     case 5:
254     case 6:
255     case 7:
256     case 8:
257     case 10:
258     case 12:
259     case 16:
260     case 32:
261     case 64:
262     case 128:
263     case 256:
264       return 1;
265     }
266   return 0;
267 }
268
269 int
270 xtensa_simm8x256 (v)
271      int v;
272 {
273   return (v & 255) == 0 && (v >= -32768 && v <= 32512);
274 }
275
276 int
277 xtensa_ai4const (v)
278      int v;
279 {
280   return (v == -1 || (v >= 1 && v <= 15));
281 }
282
283 int
284 xtensa_simm7 (v)
285      int v;
286 {
287   return v >= -32 && v <= 95;
288 }
289
290 int
291 xtensa_b4const (v)
292      int v;
293 {
294   switch (v)
295     {
296     case -1:
297     case 1:
298     case 2:
299     case 3:
300     case 4:
301     case 5:
302     case 6:
303     case 7:
304     case 8:
305     case 10:
306     case 12:
307     case 16:
308     case 32:
309     case 64:
310     case 128:
311     case 256:
312       return 1;
313     }
314   return 0;
315 }
316
317 int
318 xtensa_simm8 (v)
319      int v;
320 {
321   return v >= -128 && v <= 127;
322 }
323
324 int
325 xtensa_tp7 (v)
326      int v;
327 {
328   return (v >= 7 && v <= 22);
329 }
330
331 int
332 xtensa_lsi4x4 (v)
333      int v;
334 {
335   return (v & 3) == 0 && (v >= 0 && v <= 60);
336 }
337
338 int
339 xtensa_simm12b (v)
340      int v;
341 {
342   return v >= -2048 && v <= 2047;
343 }
344
345 int
346 xtensa_uimm8 (v)
347      int v;
348 {
349   return v >= 0 && v <= 255;
350 }
351
352 int
353 xtensa_uimm8x2 (v)
354      int v;
355 {
356   return (v & 1) == 0 && (v >= 0 && v <= 510);
357 }
358
359 int
360 xtensa_uimm8x4 (v)
361      int v;
362 {
363   return (v & 3) == 0 && (v >= 0 && v <= 1020);
364 }
365
366
367 /* This is just like the standard true_regnum() function except that it
368    works even when reg_renumber is not initialized.  */
369
370 int
371 xt_true_regnum (x)
372      rtx x;
373 {
374   if (GET_CODE (x) == REG)
375     {
376       if (reg_renumber
377           && REGNO (x) >= FIRST_PSEUDO_REGISTER
378           && reg_renumber[REGNO (x)] >= 0)
379         return reg_renumber[REGNO (x)];
380       return REGNO (x);
381     }
382   if (GET_CODE (x) == SUBREG)
383     {
384       int base = xt_true_regnum (SUBREG_REG (x));
385       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
386         return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
387                                            GET_MODE (SUBREG_REG (x)),
388                                            SUBREG_BYTE (x), GET_MODE (x));
389     }
390   return -1;
391 }
392
393
394 int
395 add_operand (op, mode)
396      rtx op;
397      enum machine_mode mode;
398 {
399   if (GET_CODE (op) == CONST_INT)
400     return (xtensa_simm8 (INTVAL (op)) || xtensa_simm8x256 (INTVAL (op)));
401
402   return register_operand (op, mode);
403 }
404
405
406 int
407 arith_operand (op, mode)
408      rtx op;
409      enum machine_mode mode;
410 {
411   if (GET_CODE (op) == CONST_INT)
412     return xtensa_simm8 (INTVAL (op));
413
414   return register_operand (op, mode);
415 }
416
417
418 int
419 nonimmed_operand (op, mode)
420      rtx op;
421      enum machine_mode mode;
422 {
423   /* We cannot use the standard nonimmediate_operand() predicate because
424      it includes constant pool memory operands.  */
425
426   if (memory_operand (op, mode))
427     return !constantpool_address_p (XEXP (op, 0));
428
429   return register_operand (op, mode);
430 }
431
432
433 int
434 mem_operand (op, mode)
435      rtx op;
436      enum machine_mode mode;
437 {
438   /* We cannot use the standard memory_operand() predicate because
439      it includes constant pool memory operands.  */
440
441   if (memory_operand (op, mode))
442     return !constantpool_address_p (XEXP (op, 0));
443
444   return FALSE;
445 }
446
447
448 int
449 xtensa_valid_move (mode, operands)
450      enum machine_mode mode;
451      rtx *operands;
452 {
453   /* Either the destination or source must be a register, and the
454      MAC16 accumulator doesn't count.  */
455
456   if (register_operand (operands[0], mode))
457     {
458       int dst_regnum = xt_true_regnum (operands[0]);
459
460       /* The stack pointer can only be assigned with a MOVSP opcode.  */
461       if (dst_regnum == STACK_POINTER_REGNUM)
462         return (mode == SImode
463                 && register_operand (operands[1], mode)
464                 && !ACC_REG_P (xt_true_regnum (operands[1])));
465
466       if (!ACC_REG_P (dst_regnum))
467         return true;
468     }
469   if (register_operand (operands[1], mode))
470     {
471       int src_regnum = xt_true_regnum (operands[1]);
472       if (!ACC_REG_P (src_regnum))
473         return true;
474     }
475   return FALSE;
476 }
477
478
479 int
480 mask_operand (op, mode)
481      rtx op;
482      enum machine_mode mode;
483 {
484   if (GET_CODE (op) == CONST_INT)
485     return xtensa_mask_immediate (INTVAL (op));
486
487   return register_operand (op, mode);
488 }
489
490
491 int
492 extui_fldsz_operand (op, mode)
493      rtx op;
494      enum machine_mode mode ATTRIBUTE_UNUSED;
495 {
496   return ((GET_CODE (op) == CONST_INT)
497           && xtensa_mask_immediate ((1 << INTVAL (op)) - 1));
498 }
499
500
501 int
502 sext_operand (op, mode)
503      rtx op;
504      enum machine_mode mode;
505 {
506   if (TARGET_SEXT)
507     return nonimmed_operand (op, mode);
508   return mem_operand (op, mode);
509 }
510
511
512 int
513 sext_fldsz_operand (op, mode)
514      rtx op;
515      enum machine_mode mode ATTRIBUTE_UNUSED;
516 {
517   return ((GET_CODE (op) == CONST_INT) && xtensa_tp7 (INTVAL (op) - 1));
518 }
519
520
521 int
522 lsbitnum_operand (op, mode)
523      rtx op;
524      enum machine_mode mode ATTRIBUTE_UNUSED;
525 {
526   if (GET_CODE (op) == CONST_INT)
527     {
528       return (BITS_BIG_ENDIAN
529               ? (INTVAL (op) == BITS_PER_WORD-1)
530               : (INTVAL (op) == 0));
531     }
532   return FALSE;
533 }
534
535
536 static int
537 b4const_or_zero (v)
538      int v;
539 {
540   if (v == 0)
541     return TRUE;
542   return xtensa_b4const (v);
543 }
544
545
546 int
547 branch_operand (op, mode)
548      rtx op;
549      enum machine_mode mode;
550 {
551   if (GET_CODE (op) == CONST_INT)
552     return b4const_or_zero (INTVAL (op));
553
554   return register_operand (op, mode);
555 }
556
557
558 int
559 ubranch_operand (op, mode)
560      rtx op;
561      enum machine_mode mode;
562 {
563   if (GET_CODE (op) == CONST_INT)
564     return xtensa_b4constu (INTVAL (op));
565
566   return register_operand (op, mode);
567 }
568
569
570 int
571 call_insn_operand (op, mode)
572      rtx op;
573      enum machine_mode mode ATTRIBUTE_UNUSED;
574 {
575   if ((GET_CODE (op) == REG)
576       && (op != arg_pointer_rtx)
577       && ((REGNO (op) < FRAME_POINTER_REGNUM)
578           || (REGNO (op) > LAST_VIRTUAL_REGISTER)))
579     return TRUE;
580
581   if (CONSTANT_ADDRESS_P (op))
582     {
583       /* Direct calls only allowed to static functions with PIC.  */
584       if (flag_pic)
585         {
586           tree callee, callee_sec, caller_sec;
587
588           if (GET_CODE (op) != SYMBOL_REF || !SYMBOL_REF_LOCAL_P (op))
589             return FALSE;
590
591           /* Don't attempt a direct call if the callee is known to be in
592              a different section, since there's a good chance it will be
593              out of range.  */
594
595           if (flag_function_sections
596               || DECL_ONE_ONLY (current_function_decl))
597             return FALSE;
598           caller_sec = DECL_SECTION_NAME (current_function_decl);
599           callee = SYMBOL_REF_DECL (op);
600           if (callee)
601             {
602               if (DECL_ONE_ONLY (callee))
603                 return FALSE;
604               callee_sec = DECL_SECTION_NAME (callee);
605               if (((caller_sec == NULL_TREE) ^ (callee_sec == NULL_TREE))
606                   || (caller_sec != NULL_TREE
607                       && strcmp (TREE_STRING_POINTER (caller_sec),
608                                  TREE_STRING_POINTER (callee_sec)) != 0))
609                 return FALSE;
610             }
611           else if (caller_sec != NULL_TREE)
612             return FALSE;
613         }
614       return TRUE;
615     }
616
617   return FALSE;
618 }
619
620
621 int
622 move_operand (op, mode)
623      rtx op;
624      enum machine_mode mode;
625 {
626   if (register_operand (op, mode)
627       || memory_operand (op, mode))
628     return TRUE;
629
630   switch (mode)
631     {
632     case DFmode:
633     case SFmode:
634       return TARGET_CONST16 && CONSTANT_P (op);
635
636     case DImode:
637     case SImode:
638       if (TARGET_CONST16)
639         return CONSTANT_P (op);
640       /* fall through */
641
642     case HImode:
643     case QImode:
644       /* Accept CONSTANT_P_RTX, since it will be gone by CSE1 and
645          result in 0/1.  */
646       if (GET_CODE (op) == CONSTANT_P_RTX)
647         return TRUE;
648
649       if (GET_CODE (op) == CONST_INT && xtensa_simm12b (INTVAL (op)))
650         return TRUE;
651       break;
652
653     default:
654       break;
655     }
656
657   return FALSE;
658 }
659
660
661 int
662 smalloffset_mem_p (op)
663      rtx op;
664 {
665   if (GET_CODE (op) == MEM)
666     {
667       rtx addr = XEXP (op, 0);
668       if (GET_CODE (addr) == REG)
669         return REG_OK_FOR_BASE_P (addr);
670       if (GET_CODE (addr) == PLUS)
671         {
672           rtx offset = XEXP (addr, 0);
673           if (GET_CODE (offset) != CONST_INT)
674             offset = XEXP (addr, 1);
675           if (GET_CODE (offset) != CONST_INT)
676             return FALSE;
677           return xtensa_lsi4x4 (INTVAL (offset));
678         }
679     }
680   return FALSE;
681 }
682
683
684 int
685 constantpool_address_p (addr)
686      rtx addr;
687 {
688   rtx sym = addr;
689
690   if (GET_CODE (addr) == CONST)
691     {
692       rtx offset;
693
694       /* only handle (PLUS (SYM, OFFSET)) form */
695       addr = XEXP (addr, 0);
696       if (GET_CODE (addr) != PLUS)
697         return FALSE;
698
699       /* make sure the address is word aligned */
700       offset = XEXP (addr, 1);
701       if ((GET_CODE (offset) != CONST_INT)
702           || ((INTVAL (offset) & 3) != 0))
703         return FALSE;
704
705       sym = XEXP (addr, 0);
706     }
707
708   if ((GET_CODE (sym) == SYMBOL_REF)
709       && CONSTANT_POOL_ADDRESS_P (sym))
710     return TRUE;
711   return FALSE;
712 }
713
714
715 int
716 constantpool_mem_p (op)
717      rtx op;
718 {
719   if (GET_CODE (op) == MEM)
720     return constantpool_address_p (XEXP (op, 0));
721   return FALSE;
722 }
723
724
725 /* Accept the floating point constant 1 in the appropriate mode.  */
726
727 int
728 const_float_1_operand (op, mode)
729      rtx op;
730      enum machine_mode mode;
731 {
732   REAL_VALUE_TYPE d;
733   static REAL_VALUE_TYPE onedf;
734   static REAL_VALUE_TYPE onesf;
735   static int one_initialized;
736
737   if ((GET_CODE (op) != CONST_DOUBLE)
738       || (mode != GET_MODE (op))
739       || (mode != DFmode && mode != SFmode))
740     return FALSE;
741
742   REAL_VALUE_FROM_CONST_DOUBLE (d, op);
743
744   if (! one_initialized)
745     {
746       onedf = REAL_VALUE_ATOF ("1.0", DFmode);
747       onesf = REAL_VALUE_ATOF ("1.0", SFmode);
748       one_initialized = TRUE;
749     }
750
751   if (mode == DFmode)
752     return REAL_VALUES_EQUAL (d, onedf);
753   else
754     return REAL_VALUES_EQUAL (d, onesf);
755 }
756
757
758 int
759 fpmem_offset_operand (op, mode)
760      rtx op;
761      enum machine_mode mode ATTRIBUTE_UNUSED;
762 {
763   if (GET_CODE (op) == CONST_INT)
764     return xtensa_mem_offset (INTVAL (op), SFmode);
765   return 0;
766 }
767
768
769 void
770 xtensa_extend_reg (dst, src)
771      rtx dst;
772      rtx src;
773 {
774   rtx temp = gen_reg_rtx (SImode);
775   rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
776
777   /* generate paradoxical subregs as needed so that the modes match */
778   src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
779   dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
780
781   emit_insn (gen_ashlsi3 (temp, src, shift));
782   emit_insn (gen_ashrsi3 (dst, temp, shift));
783 }
784
785
786 int
787 branch_operator (x, mode)
788      rtx x;
789      enum machine_mode mode;
790 {
791   if (GET_MODE (x) != mode)
792     return FALSE;
793
794   switch (GET_CODE (x))
795     {
796     case EQ:
797     case NE:
798     case LT:
799     case GE:
800       return TRUE;
801     default:
802       break;
803     }
804   return FALSE;
805 }
806
807
808 int
809 ubranch_operator (x, mode)
810      rtx x;
811      enum machine_mode mode;
812 {
813   if (GET_MODE (x) != mode)
814     return FALSE;
815
816   switch (GET_CODE (x))
817     {
818     case LTU:
819     case GEU:
820       return TRUE;
821     default:
822       break;
823     }
824   return FALSE;
825 }
826
827
828 int
829 boolean_operator (x, mode)
830      rtx x;
831      enum machine_mode mode;
832 {
833   if (GET_MODE (x) != mode)
834     return FALSE;
835
836   switch (GET_CODE (x))
837     {
838     case EQ:
839     case NE:
840       return TRUE;
841     default:
842       break;
843     }
844   return FALSE;
845 }
846
847
848 int
849 xtensa_mask_immediate (v)
850      int v;
851 {
852 #define MAX_MASK_SIZE 16
853   int mask_size;
854
855   for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
856     {
857       if ((v & 1) == 0)
858         return FALSE;
859       v = v >> 1;
860       if (v == 0)
861         return TRUE;
862     }
863
864   return FALSE;
865 }
866
867
868 int
869 xtensa_mem_offset (v, mode)
870      unsigned v;
871      enum machine_mode mode;
872 {
873   switch (mode)
874     {
875     case BLKmode:
876       /* Handle the worst case for block moves.  See xtensa_expand_block_move
877          where we emit an optimized block move operation if the block can be
878          moved in < "move_ratio" pieces.  The worst case is when the block is
879          aligned but has a size of (3 mod 4) (does this happen?) so that the
880          last piece requires a byte load/store.  */
881       return (xtensa_uimm8 (v)
882               && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
883
884     case QImode:
885       return xtensa_uimm8 (v);
886
887     case HImode:
888       return xtensa_uimm8x2 (v);
889
890     case DFmode:
891       return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
892
893     default:
894       break;
895     }
896
897   return xtensa_uimm8x4 (v);
898 }
899
900
901 /* Make normal rtx_code into something we can index from an array */
902
903 static enum internal_test
904 map_test_to_internal_test (test_code)
905      enum rtx_code test_code;
906 {
907   enum internal_test test = ITEST_MAX;
908
909   switch (test_code)
910     {
911     default:                    break;
912     case EQ:  test = ITEST_EQ;  break;
913     case NE:  test = ITEST_NE;  break;
914     case GT:  test = ITEST_GT;  break;
915     case GE:  test = ITEST_GE;  break;
916     case LT:  test = ITEST_LT;  break;
917     case LE:  test = ITEST_LE;  break;
918     case GTU: test = ITEST_GTU; break;
919     case GEU: test = ITEST_GEU; break;
920     case LTU: test = ITEST_LTU; break;
921     case LEU: test = ITEST_LEU; break;
922     }
923
924   return test;
925 }
926
927
928 /* Generate the code to compare two integer values.  The return value is
929    the comparison expression.  */
930
931 static rtx
932 gen_int_relational (test_code, cmp0, cmp1, p_invert)
933      enum rtx_code test_code;   /* relational test (EQ, etc) */
934      rtx cmp0;                  /* first operand to compare */
935      rtx cmp1;                  /* second operand to compare */
936      int *p_invert;             /* whether branch needs to reverse its test */
937 {
938   struct cmp_info {
939     enum rtx_code test_code;    /* test code to use in insn */
940     int (*const_range_p) PARAMS ((int)); /* predicate function to check range */
941     int const_add;              /* constant to add (convert LE -> LT) */
942     int reverse_regs;           /* reverse registers in test */
943     int invert_const;           /* != 0 if invert value if cmp1 is constant */
944     int invert_reg;             /* != 0 if invert value if cmp1 is register */
945     int unsignedp;              /* != 0 for unsigned comparisons.  */
946   };
947
948   static struct cmp_info info[ (int)ITEST_MAX ] = {
949
950     { EQ,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* EQ  */
951     { NE,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* NE  */
952
953     { LT,       b4const_or_zero,        1, 1, 1, 0, 0 },        /* GT  */
954     { GE,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* GE  */
955     { LT,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* LT  */
956     { GE,       b4const_or_zero,        1, 1, 1, 0, 0 },        /* LE  */
957
958     { LTU,      xtensa_b4constu,        1, 1, 1, 0, 1 },        /* GTU */
959     { GEU,      xtensa_b4constu,        0, 0, 0, 0, 1 },        /* GEU */
960     { LTU,      xtensa_b4constu,        0, 0, 0, 0, 1 },        /* LTU */
961     { GEU,      xtensa_b4constu,        1, 1, 1, 0, 1 },        /* LEU */
962   };
963
964   enum internal_test test;
965   enum machine_mode mode;
966   struct cmp_info *p_info;
967
968   test = map_test_to_internal_test (test_code);
969   if (test == ITEST_MAX)
970     abort ();
971
972   p_info = &info[ (int)test ];
973
974   mode = GET_MODE (cmp0);
975   if (mode == VOIDmode)
976     mode = GET_MODE (cmp1);
977
978   /* Make sure we can handle any constants given to us.  */
979   if (GET_CODE (cmp1) == CONST_INT)
980     {
981       HOST_WIDE_INT value = INTVAL (cmp1);
982       unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
983
984       /* if the immediate overflows or does not fit in the immediate field,
985          spill it to a register */
986
987       if ((p_info->unsignedp ?
988            (uvalue + p_info->const_add > uvalue) :
989            (value + p_info->const_add > value)) != (p_info->const_add > 0))
990         {
991           cmp1 = force_reg (mode, cmp1);
992         }
993       else if (!(p_info->const_range_p) (value + p_info->const_add))
994         {
995           cmp1 = force_reg (mode, cmp1);
996         }
997     }
998   else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
999     {
1000       cmp1 = force_reg (mode, cmp1);
1001     }
1002
1003   /* See if we need to invert the result.  */
1004   *p_invert = ((GET_CODE (cmp1) == CONST_INT)
1005                ? p_info->invert_const
1006                : p_info->invert_reg);
1007
1008   /* Comparison to constants, may involve adding 1 to change a LT into LE.
1009      Comparison between two registers, may involve switching operands.  */
1010   if (GET_CODE (cmp1) == CONST_INT)
1011     {
1012       if (p_info->const_add != 0)
1013         cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
1014
1015     }
1016   else if (p_info->reverse_regs)
1017     {
1018       rtx temp = cmp0;
1019       cmp0 = cmp1;
1020       cmp1 = temp;
1021     }
1022
1023   return gen_rtx (p_info->test_code, VOIDmode, cmp0, cmp1);
1024 }
1025
1026
1027 /* Generate the code to compare two float values.  The return value is
1028    the comparison expression.  */
1029
1030 static rtx
1031 gen_float_relational (test_code, cmp0, cmp1)
1032      enum rtx_code test_code;   /* relational test (EQ, etc) */
1033      rtx cmp0;                  /* first operand to compare */
1034      rtx cmp1;                  /* second operand to compare */
1035 {
1036   rtx (*gen_fn) PARAMS ((rtx, rtx, rtx));
1037   rtx brtmp;
1038   int reverse_regs, invert;
1039
1040   switch (test_code)
1041     {
1042     case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
1043     case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
1044     case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
1045     case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
1046     case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
1047     case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
1048     default:
1049       fatal_insn ("bad test", gen_rtx (test_code, VOIDmode, cmp0, cmp1));
1050       reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
1051     }
1052
1053   if (reverse_regs)
1054     {
1055       rtx temp = cmp0;
1056       cmp0 = cmp1;
1057       cmp1 = temp;
1058     }
1059
1060   brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
1061   emit_insn (gen_fn (brtmp, cmp0, cmp1));
1062
1063   return gen_rtx (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
1064 }
1065
1066
1067 void
1068 xtensa_expand_conditional_branch (operands, test_code)
1069      rtx *operands;
1070      enum rtx_code test_code;
1071 {
1072   enum cmp_type type = branch_type;
1073   rtx cmp0 = branch_cmp[0];
1074   rtx cmp1 = branch_cmp[1];
1075   rtx cmp;
1076   int invert;
1077   rtx label1, label2;
1078
1079   switch (type)
1080     {
1081     case CMP_DF:
1082     default:
1083       fatal_insn ("bad test", gen_rtx (test_code, VOIDmode, cmp0, cmp1));
1084
1085     case CMP_SI:
1086       invert = FALSE;
1087       cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
1088       break;
1089
1090     case CMP_SF:
1091       if (!TARGET_HARD_FLOAT)
1092         fatal_insn ("bad test", gen_rtx (test_code, VOIDmode, cmp0, cmp1));
1093       invert = FALSE;
1094       cmp = gen_float_relational (test_code, cmp0, cmp1);
1095       break;
1096     }
1097
1098   /* Generate the branch.  */
1099
1100   label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
1101   label2 = pc_rtx;
1102
1103   if (invert)
1104     {
1105       label2 = label1;
1106       label1 = pc_rtx;
1107     }
1108
1109   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
1110                                gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
1111                                                      label1,
1112                                                      label2)));
1113 }
1114
1115
1116 static rtx
1117 gen_conditional_move (cmp)
1118      rtx cmp;
1119 {
1120   enum rtx_code code = GET_CODE (cmp);
1121   rtx op0 = branch_cmp[0];
1122   rtx op1 = branch_cmp[1];
1123
1124   if (branch_type == CMP_SI)
1125     {
1126       /* Jump optimization calls get_condition() which canonicalizes
1127          comparisons like (GE x <const>) to (GT x <const-1>).
1128          Transform those comparisons back to GE, since that is the
1129          comparison supported in Xtensa.  We shouldn't have to
1130          transform <LE x const> comparisons, because neither
1131          xtensa_expand_conditional_branch() nor get_condition() will
1132          produce them.  */
1133
1134       if ((code == GT) && (op1 == constm1_rtx))
1135         {
1136           code = GE;
1137           op1 = const0_rtx;
1138         }
1139       cmp = gen_rtx (code, VOIDmode, cc0_rtx, const0_rtx);
1140
1141       if (boolean_operator (cmp, VOIDmode))
1142         {
1143           /* swap the operands to make const0 second */
1144           if (op0 == const0_rtx)
1145             {
1146               op0 = op1;
1147               op1 = const0_rtx;
1148             }
1149
1150           /* if not comparing against zero, emit a comparison (subtract) */
1151           if (op1 != const0_rtx)
1152             {
1153               op0 = expand_binop (SImode, sub_optab, op0, op1,
1154                                   0, 0, OPTAB_LIB_WIDEN);
1155               op1 = const0_rtx;
1156             }
1157         }
1158       else if (branch_operator (cmp, VOIDmode))
1159         {
1160           /* swap the operands to make const0 second */
1161           if (op0 == const0_rtx)
1162             {
1163               op0 = op1;
1164               op1 = const0_rtx;
1165
1166               switch (code)
1167                 {
1168                 case LT: code = GE; break;
1169                 case GE: code = LT; break;
1170                 default: abort ();
1171                 }
1172             }
1173
1174           if (op1 != const0_rtx)
1175             return 0;
1176         }
1177       else
1178         return 0;
1179
1180       return gen_rtx (code, VOIDmode, op0, op1);
1181     }
1182
1183   if (TARGET_HARD_FLOAT && (branch_type == CMP_SF))
1184     return gen_float_relational (code, op0, op1);
1185
1186   return 0;
1187 }
1188
1189
1190 int
1191 xtensa_expand_conditional_move (operands, isflt)
1192     rtx *operands;
1193     int isflt;
1194 {
1195   rtx cmp;
1196   rtx (*gen_fn) PARAMS ((rtx, rtx, rtx, rtx, rtx));
1197
1198   if (!(cmp = gen_conditional_move (operands[1])))
1199     return 0;
1200
1201   if (isflt)
1202     gen_fn = (branch_type == CMP_SI
1203               ? gen_movsfcc_internal0
1204               : gen_movsfcc_internal1);
1205   else
1206     gen_fn = (branch_type == CMP_SI
1207               ? gen_movsicc_internal0
1208               : gen_movsicc_internal1);
1209
1210   emit_insn (gen_fn (operands[0], XEXP (cmp, 0),
1211                      operands[2], operands[3], cmp));
1212   return 1;
1213 }
1214
1215
1216 int
1217 xtensa_expand_scc (operands)
1218      rtx *operands;
1219 {
1220   rtx dest = operands[0];
1221   rtx cmp = operands[1];
1222   rtx one_tmp, zero_tmp;
1223   rtx (*gen_fn) PARAMS ((rtx, rtx, rtx, rtx, rtx));
1224
1225   if (!(cmp = gen_conditional_move (cmp)))
1226     return 0;
1227
1228   one_tmp = gen_reg_rtx (SImode);
1229   zero_tmp = gen_reg_rtx (SImode);
1230   emit_insn (gen_movsi (one_tmp, const_true_rtx));
1231   emit_insn (gen_movsi (zero_tmp, const0_rtx));
1232
1233   gen_fn = (branch_type == CMP_SI
1234             ? gen_movsicc_internal0
1235             : gen_movsicc_internal1);
1236   emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
1237   return 1;
1238 }
1239
1240
1241 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1].  MODE is
1242    for the output, i.e., the input operands are twice as big as MODE.  */
1243
1244 void
1245 xtensa_split_operand_pair (operands, mode)
1246      rtx operands[4];
1247      enum machine_mode mode;
1248 {
1249   switch (GET_CODE (operands[1]))
1250     {
1251     case REG:
1252       operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
1253       operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
1254       break;
1255
1256     case MEM:
1257       operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
1258       operands[2] = adjust_address (operands[1], mode, 0);
1259       break;
1260
1261     case CONST_INT:
1262     case CONST_DOUBLE:
1263       split_double (operands[1], &operands[2], &operands[3]);
1264       break;
1265
1266     default:
1267       abort ();
1268     }
1269
1270   switch (GET_CODE (operands[0]))
1271     {
1272     case REG:
1273       operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
1274       operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
1275       break;
1276
1277     case MEM:
1278       operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
1279       operands[0] = adjust_address (operands[0], mode, 0);
1280       break;
1281
1282     default:
1283       abort ();
1284     }
1285 }
1286
1287
1288 /* Emit insns to move operands[1] into operands[0].
1289    Return 1 if we have written out everything that needs to be done to
1290    do the move.  Otherwise, return 0 and the caller will emit the move
1291    normally.  */
1292
1293 int
1294 xtensa_emit_move_sequence (operands, mode)
1295      rtx *operands;
1296      enum machine_mode mode;
1297 {
1298   if (CONSTANT_P (operands[1])
1299       && GET_CODE (operands[1]) != CONSTANT_P_RTX
1300       && (GET_CODE (operands[1]) != CONST_INT
1301           || !xtensa_simm12b (INTVAL (operands[1]))))
1302     {
1303       if (!TARGET_CONST16)
1304         operands[1] = force_const_mem (SImode, operands[1]);
1305
1306       /* PC-relative loads are always SImode, and CONST16 is only
1307          supported in the movsi pattern, so add a SUBREG for any other
1308          (smaller) mode.  */
1309
1310       if (mode != SImode)
1311         {
1312           if (register_operand (operands[0], mode))
1313             {
1314               operands[0] = simplify_gen_subreg (SImode, operands[0], mode, 0);
1315               emit_move_insn (operands[0], operands[1]);
1316               return 1;
1317             }
1318           else
1319             {
1320               operands[1] = force_reg (SImode, operands[1]);
1321               operands[1] = gen_lowpart_SUBREG (mode, operands[1]);
1322             }
1323         }
1324     }
1325
1326   if (!(reload_in_progress | reload_completed))
1327     {
1328       if (!xtensa_valid_move (mode, operands))
1329         operands[1] = force_reg (mode, operands[1]);
1330
1331       if (xtensa_copy_incoming_a7 (operands, mode))
1332         return 1;
1333     }
1334
1335   /* During reload we don't want to emit (subreg:X (mem:Y)) since that
1336      instruction won't be recognized after reload, so we remove the
1337      subreg and adjust mem accordingly.  */
1338   if (reload_in_progress)
1339     {
1340       operands[0] = fixup_subreg_mem (operands[0]);
1341       operands[1] = fixup_subreg_mem (operands[1]);
1342     }
1343   return 0;
1344 }
1345
1346
1347 static rtx
1348 fixup_subreg_mem (x)
1349      rtx x;
1350 {
1351   if (GET_CODE (x) == SUBREG
1352       && GET_CODE (SUBREG_REG (x)) == REG
1353       && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1354     {
1355       rtx temp =
1356         gen_rtx_SUBREG (GET_MODE (x),
1357                         reg_equiv_mem [REGNO (SUBREG_REG (x))],
1358                         SUBREG_BYTE (x));
1359       x = alter_subreg (&temp);
1360     }
1361   return x;
1362 }
1363
1364
1365 /* Check if this move is copying an incoming argument in a7.  If so,
1366    emit the move, followed by the special "set_frame_ptr"
1367    unspec_volatile insn, at the very beginning of the function.  This
1368    is necessary because the register allocator will ignore conflicts
1369    with a7 and may assign some other pseudo to a7.  If that pseudo was
1370    assigned prior to this move, it would clobber the incoming argument
1371    in a7.  By copying the argument out of a7 as the very first thing,
1372    and then immediately following that with an unspec_volatile to keep
1373    the scheduler away, we should avoid any problems.  */
1374
1375 bool
1376 xtensa_copy_incoming_a7 (operands, mode)
1377      rtx *operands;
1378      enum machine_mode mode;
1379 {
1380   if (a7_overlap_mentioned_p (operands[1])
1381       && !cfun->machine->incoming_a7_copied)
1382     {
1383       rtx mov;
1384       switch (mode)
1385         {
1386         case DFmode:
1387           mov = gen_movdf_internal (operands[0], operands[1]);
1388           break;
1389         case SFmode:
1390           mov = gen_movsf_internal (operands[0], operands[1]);
1391           break;
1392         case DImode:
1393           mov = gen_movdi_internal (operands[0], operands[1]);
1394           break;
1395         case SImode:
1396           mov = gen_movsi_internal (operands[0], operands[1]);
1397           break;
1398         case HImode:
1399           mov = gen_movhi_internal (operands[0], operands[1]);
1400           break;
1401         case QImode:
1402           mov = gen_movqi_internal (operands[0], operands[1]);
1403           break;
1404         default:
1405           abort ();
1406         }
1407
1408       /* Insert the instructions before any other argument copies.
1409          (The set_frame_ptr insn comes _after_ the move, so push it
1410          out first.)  */
1411       push_topmost_sequence ();
1412       emit_insn_after (gen_set_frame_ptr (), get_insns ());
1413       emit_insn_after (mov, get_insns ());
1414       pop_topmost_sequence ();
1415
1416       /* Ideally the incoming argument in a7 would only be copied
1417          once, since propagating a7 into the body of a function
1418          will almost certainly lead to errors.  However, there is
1419          at least one harmless case (in GCSE) where the original
1420          copy from a7 is changed to copy into a new pseudo.  Thus,
1421          we use a flag to only do this special treatment for the
1422          first copy of a7.  */
1423
1424       cfun->machine->incoming_a7_copied = true;
1425
1426       return 1;
1427     }
1428
1429   return 0;
1430 }
1431
1432
1433 /* Try to expand a block move operation to an RTL block move instruction.
1434    If not optimizing or if the block size is not a constant or if the
1435    block is small, the expansion fails and GCC falls back to calling
1436    memcpy().
1437
1438    operands[0] is the destination
1439    operands[1] is the source
1440    operands[2] is the length
1441    operands[3] is the alignment */
1442
1443 int
1444 xtensa_expand_block_move (operands)
1445      rtx *operands;
1446 {
1447   rtx dest = operands[0];
1448   rtx src = operands[1];
1449   int bytes = INTVAL (operands[2]);
1450   int align = XINT (operands[3], 0);
1451   int num_pieces, move_ratio;
1452
1453   /* If this is not a fixed size move, just call memcpy */
1454   if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
1455     return 0;
1456
1457   /* Anything to move? */
1458   if (bytes <= 0)
1459     return 1;
1460
1461   if (align > MOVE_MAX)
1462     align = MOVE_MAX;
1463
1464   /* decide whether to expand inline based on the optimization level */
1465   move_ratio = 4;
1466   if (optimize > 2)
1467     move_ratio = LARGEST_MOVE_RATIO;
1468   num_pieces = (bytes / align) + (bytes % align); /* close enough anyway */
1469   if (num_pieces >= move_ratio)
1470     return 0;
1471
1472   /* make sure the memory addresses are valid */
1473   operands[0] = validize_mem (dest);
1474   operands[1] = validize_mem (src);
1475
1476   emit_insn (gen_movstrsi_internal (operands[0], operands[1],
1477                                     operands[2], operands[3]));
1478   return 1;
1479 }
1480
1481
1482 /*  Emit a sequence of instructions to implement a block move, trying
1483     to hide load delay slots as much as possible.  Load N values into
1484     temporary registers, store those N values, and repeat until the
1485     complete block has been moved.  N=delay_slots+1 */
1486
1487 struct meminsnbuf {
1488   char template[30];
1489   rtx operands[2];
1490 };
1491
1492 void
1493 xtensa_emit_block_move (operands, tmpregs, delay_slots)
1494      rtx *operands;
1495      rtx *tmpregs;
1496      int delay_slots;
1497 {
1498   rtx dest = operands[0];
1499   rtx src = operands[1];
1500   int bytes = INTVAL (operands[2]);
1501   int align = XINT (operands[3], 0);
1502   rtx from_addr = XEXP (src, 0);
1503   rtx to_addr = XEXP (dest, 0);
1504   int from_struct = MEM_IN_STRUCT_P (src);
1505   int to_struct = MEM_IN_STRUCT_P (dest);
1506   int offset = 0;
1507   int chunk_size, item_size;
1508   struct meminsnbuf *ldinsns, *stinsns;
1509   const char *ldname, *stname;
1510   enum machine_mode mode;
1511
1512   if (align > MOVE_MAX)
1513     align = MOVE_MAX;
1514   item_size = align;
1515   chunk_size = delay_slots + 1;
1516
1517   ldinsns = (struct meminsnbuf *)
1518     alloca (chunk_size * sizeof (struct meminsnbuf));
1519   stinsns = (struct meminsnbuf *)
1520     alloca (chunk_size * sizeof (struct meminsnbuf));
1521
1522   mode = xtensa_find_mode_for_size (item_size);
1523   item_size = GET_MODE_SIZE (mode);
1524   ldname = xtensa_ld_opcodes[(int) mode];
1525   stname = xtensa_st_opcodes[(int) mode];
1526
1527   while (bytes > 0)
1528     {
1529       int n;
1530
1531       for (n = 0; n < chunk_size; n++)
1532         {
1533           rtx addr, mem;
1534
1535           if (bytes == 0)
1536             {
1537               chunk_size = n;
1538               break;
1539             }
1540
1541           if (bytes < item_size)
1542             {
1543               /* find a smaller item_size which we can load & store */
1544               item_size = bytes;
1545               mode = xtensa_find_mode_for_size (item_size);
1546               item_size = GET_MODE_SIZE (mode);
1547               ldname = xtensa_ld_opcodes[(int) mode];
1548               stname = xtensa_st_opcodes[(int) mode];
1549             }
1550
1551           /* record the load instruction opcode and operands */
1552           addr = plus_constant (from_addr, offset);
1553           mem = gen_rtx_MEM (mode, addr);
1554           if (! memory_address_p (mode, addr))
1555             abort ();
1556           MEM_IN_STRUCT_P (mem) = from_struct;
1557           ldinsns[n].operands[0] = tmpregs[n];
1558           ldinsns[n].operands[1] = mem;
1559           sprintf (ldinsns[n].template, "%s\t%%0, %%1", ldname);
1560
1561           /* record the store instruction opcode and operands */
1562           addr = plus_constant (to_addr, offset);
1563           mem = gen_rtx_MEM (mode, addr);
1564           if (! memory_address_p (mode, addr))
1565             abort ();
1566           MEM_IN_STRUCT_P (mem) = to_struct;
1567           stinsns[n].operands[0] = tmpregs[n];
1568           stinsns[n].operands[1] = mem;
1569           sprintf (stinsns[n].template, "%s\t%%0, %%1", stname);
1570
1571           offset += item_size;
1572           bytes -= item_size;
1573         }
1574
1575       /* now output the loads followed by the stores */
1576       for (n = 0; n < chunk_size; n++)
1577         output_asm_insn (ldinsns[n].template, ldinsns[n].operands);
1578       for (n = 0; n < chunk_size; n++)
1579         output_asm_insn (stinsns[n].template, stinsns[n].operands);
1580     }
1581 }
1582
1583
1584 static enum machine_mode
1585 xtensa_find_mode_for_size (item_size)
1586      unsigned item_size;
1587 {
1588   enum machine_mode mode, tmode;
1589
1590   while (1)
1591     {
1592       mode = VOIDmode;
1593
1594       /* find mode closest to but not bigger than item_size */
1595       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1596            tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
1597         if (GET_MODE_SIZE (tmode) <= item_size)
1598           mode = tmode;
1599       if (mode == VOIDmode)
1600         abort ();
1601
1602       item_size = GET_MODE_SIZE (mode);
1603
1604       if (xtensa_ld_opcodes[(int) mode]
1605           && xtensa_st_opcodes[(int) mode])
1606         break;
1607
1608       /* cannot load & store this mode; try something smaller */
1609       item_size -= 1;
1610     }
1611
1612   return mode;
1613 }
1614
1615
1616 void
1617 xtensa_expand_nonlocal_goto (operands)
1618      rtx *operands;
1619 {
1620   rtx goto_handler = operands[1];
1621   rtx containing_fp = operands[3];
1622
1623   /* generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1624      is too big to generate in-line */
1625
1626   if (GET_CODE (containing_fp) != REG)
1627     containing_fp = force_reg (Pmode, containing_fp);
1628
1629   goto_handler = replace_rtx (copy_rtx (goto_handler),
1630                               virtual_stack_vars_rtx,
1631                               containing_fp);
1632
1633   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1634                      0, VOIDmode, 2,
1635                      containing_fp, Pmode,
1636                      goto_handler, Pmode);
1637 }
1638
1639
1640 static struct machine_function *
1641 xtensa_init_machine_status ()
1642 {
1643   return ggc_alloc_cleared (sizeof (struct machine_function));
1644 }
1645
1646
1647 void
1648 xtensa_setup_frame_addresses ()
1649 {
1650   /* Set flag to cause FRAME_POINTER_REQUIRED to be set.  */
1651   cfun->machine->accesses_prev_frame = 1;
1652
1653   emit_library_call
1654     (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
1655      0, VOIDmode, 0);
1656 }
1657
1658
1659 /* Emit the assembly for the end of a zero-cost loop.  Normally we just emit
1660    a comment showing where the end of the loop is.  However, if there is a
1661    label or a branch at the end of the loop then we need to place a nop
1662    there.  If the loop ends with a label we need the nop so that branches
1663    targetting that label will target the nop (and thus remain in the loop),
1664    instead of targetting the instruction after the loop (and thus exiting
1665    the loop).  If the loop ends with a branch, we need the nop in case the
1666    branch is targetting a location inside the loop.  When the branch
1667    executes it will cause the loop count to be decremented even if it is
1668    taken (because it is the last instruction in the loop), so we need to
1669    nop after the branch to prevent the loop count from being decremented
1670    when the branch is taken.  */
1671
1672 void
1673 xtensa_emit_loop_end (insn, operands)
1674      rtx insn;
1675      rtx *operands;
1676 {
1677   char done = 0;
1678
1679   for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
1680     {
1681       switch (GET_CODE (insn))
1682         {
1683         case NOTE:
1684         case BARRIER:
1685           break;
1686
1687         case CODE_LABEL:
1688           output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1689           done = 1;
1690           break;
1691
1692         default:
1693           {
1694             rtx body = PATTERN (insn);
1695
1696             if (GET_CODE (body) == JUMP_INSN)
1697               {
1698                 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1699                 done = 1;
1700               }
1701             else if ((GET_CODE (body) != USE)
1702                      && (GET_CODE (body) != CLOBBER))
1703               done = 1;
1704           }
1705           break;
1706         }
1707     }
1708
1709   output_asm_insn ("# loop end for %0", operands);
1710 }
1711
1712
1713 char *
1714 xtensa_emit_call (callop, operands)
1715      int callop;
1716      rtx *operands;
1717 {
1718   static char result[64];
1719   rtx tgt = operands[callop];
1720
1721   if (GET_CODE (tgt) == CONST_INT)
1722     sprintf (result, "call8\t0x%lx", INTVAL (tgt));
1723   else if (register_operand (tgt, VOIDmode))
1724     sprintf (result, "callx8\t%%%d", callop);
1725   else
1726     sprintf (result, "call8\t%%%d", callop);
1727
1728   return result;
1729 }
1730
1731
1732 /* Return the stabs register number to use for 'regno'.  */
1733
1734 int
1735 xtensa_dbx_register_number (regno)
1736      int regno;
1737 {
1738   int first = -1;
1739
1740   if (GP_REG_P (regno))
1741     {
1742       regno -= GP_REG_FIRST;
1743       first = 0;
1744     }
1745   else if (BR_REG_P (regno))
1746     {
1747       regno -= BR_REG_FIRST;
1748       first = 16;
1749     }
1750   else if (FP_REG_P (regno))
1751     {
1752       regno -= FP_REG_FIRST;
1753       /* The current numbering convention is that TIE registers are
1754          numbered in libcc order beginning with 256.  We can't guarantee
1755          that the FP registers will come first, so the following is just
1756          a guess.  It seems like we should make a special case for FP
1757          registers and give them fixed numbers < 256.  */
1758       first = 256;
1759     }
1760   else if (ACC_REG_P (regno))
1761     {
1762       first = 0;
1763       regno = -1;
1764     }
1765
1766   /* When optimizing, we sometimes get asked about pseudo-registers
1767      that don't represent hard registers.  Return 0 for these.  */
1768   if (first == -1)
1769     return 0;
1770
1771   return first + regno;
1772 }
1773
1774
1775 /* Argument support functions.  */
1776
1777 /* Initialize CUMULATIVE_ARGS for a function.  */
1778
1779 void
1780 init_cumulative_args (cum, fntype, libname)
1781      CUMULATIVE_ARGS *cum;      /* argument info to initialize */
1782      tree fntype ATTRIBUTE_UNUSED;      /* tree ptr for function decl */
1783      rtx libname ATTRIBUTE_UNUSED;      /* SYMBOL_REF of library name or 0 */
1784 {
1785   cum->arg_words = 0;
1786 }
1787
1788 /* Advance the argument to the next argument position.  */
1789
1790 void
1791 function_arg_advance (cum, mode, type)
1792      CUMULATIVE_ARGS *cum;      /* current arg information */
1793      enum machine_mode mode;    /* current arg mode */
1794      tree type;                 /* type of the argument or 0 if lib support */
1795 {
1796   int words, max;
1797   int *arg_words;
1798
1799   arg_words = &cum->arg_words;
1800   max = MAX_ARGS_IN_REGISTERS;
1801
1802   words = (((mode != BLKmode)
1803             ? (int) GET_MODE_SIZE (mode)
1804             : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1805
1806   if ((*arg_words + words > max) && (*arg_words < max))
1807     *arg_words = max;
1808
1809   *arg_words += words;
1810 }
1811
1812
1813 /* Return an RTL expression containing the register for the given mode,
1814    or 0 if the argument is to be passed on the stack.  */
1815
1816 rtx
1817 function_arg (cum, mode, type, incoming_p)
1818      CUMULATIVE_ARGS *cum;      /* current arg information */
1819      enum machine_mode mode;    /* current arg mode */
1820      tree type;                 /* type of the argument or 0 if lib support */
1821      int incoming_p;            /* computing the incoming registers? */
1822 {
1823   int regbase, words, max;
1824   int *arg_words;
1825   int regno;
1826   enum machine_mode result_mode;
1827
1828   arg_words = &cum->arg_words;
1829   regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
1830   max = MAX_ARGS_IN_REGISTERS;
1831
1832   words = (((mode != BLKmode)
1833             ? (int) GET_MODE_SIZE (mode)
1834             : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1835
1836   if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
1837     *arg_words += (*arg_words & 1);
1838
1839   if (*arg_words + words > max)
1840     return (rtx)0;
1841
1842   regno = regbase + *arg_words;
1843   result_mode = (mode == BLKmode ? TYPE_MODE (type) : mode);
1844
1845   /* We need to make sure that references to a7 are represented with
1846      rtx that is not equal to hard_frame_pointer_rtx.  For BLKmode and
1847      modes bigger than 2 words (because we only have patterns for
1848      modes of 2 words or smaller), we can't control the expansion
1849      unless we explicitly list the individual registers in a PARALLEL.  */
1850
1851   if ((mode == BLKmode || words > 2)
1852       && regno < A7_REG
1853       && regno + words > A7_REG)
1854     {
1855       rtx result;
1856       int n;
1857
1858       result = gen_rtx_PARALLEL (result_mode, rtvec_alloc (words));
1859       for (n = 0; n < words; n++)
1860         {
1861           XVECEXP (result, 0, n) =
1862             gen_rtx_EXPR_LIST (VOIDmode,
1863                                gen_raw_REG (SImode, regno + n),
1864                                GEN_INT (n * UNITS_PER_WORD));
1865         }
1866       return result;
1867     }
1868
1869   return gen_raw_REG (result_mode, regno);
1870 }
1871
1872
1873 void
1874 override_options ()
1875 {
1876   int regno;
1877   enum machine_mode mode;
1878
1879   if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
1880     error ("boolean registers required for the floating-point option");
1881
1882   /* set up the tables of ld/st opcode names for block moves */
1883   xtensa_ld_opcodes[(int) SImode] = "l32i";
1884   xtensa_ld_opcodes[(int) HImode] = "l16ui";
1885   xtensa_ld_opcodes[(int) QImode] = "l8ui";
1886   xtensa_st_opcodes[(int) SImode] = "s32i";
1887   xtensa_st_opcodes[(int) HImode] = "s16i";
1888   xtensa_st_opcodes[(int) QImode] = "s8i";
1889
1890   xtensa_char_to_class['q'] = SP_REG;
1891   xtensa_char_to_class['a'] = GR_REGS;
1892   xtensa_char_to_class['b'] = ((TARGET_BOOLEANS) ? BR_REGS : NO_REGS);
1893   xtensa_char_to_class['f'] = ((TARGET_HARD_FLOAT) ? FP_REGS : NO_REGS);
1894   xtensa_char_to_class['A'] = ((TARGET_MAC16) ? ACC_REG : NO_REGS);
1895   xtensa_char_to_class['B'] = ((TARGET_SEXT) ? GR_REGS : NO_REGS);
1896   xtensa_char_to_class['C'] = ((TARGET_MUL16) ? GR_REGS: NO_REGS);
1897   xtensa_char_to_class['D'] = ((TARGET_DENSITY) ? GR_REGS: NO_REGS);
1898   xtensa_char_to_class['d'] = ((TARGET_DENSITY) ? AR_REGS: NO_REGS);
1899   xtensa_char_to_class['W'] = ((TARGET_CONST16) ? GR_REGS: NO_REGS);
1900
1901   /* Set up array giving whether a given register can hold a given mode.  */
1902   for (mode = VOIDmode;
1903        mode != MAX_MACHINE_MODE;
1904        mode = (enum machine_mode) ((int) mode + 1))
1905     {
1906       int size = GET_MODE_SIZE (mode);
1907       enum mode_class class = GET_MODE_CLASS (mode);
1908
1909       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1910         {
1911           int temp;
1912
1913           if (ACC_REG_P (regno))
1914             temp = (TARGET_MAC16
1915                     && (class == MODE_INT) && (size <= UNITS_PER_WORD));
1916           else if (GP_REG_P (regno))
1917             temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
1918           else if (FP_REG_P (regno))
1919             temp = (TARGET_HARD_FLOAT && (mode == SFmode));
1920           else if (BR_REG_P (regno))
1921             temp = (TARGET_BOOLEANS && (mode == CCmode));
1922           else
1923             temp = FALSE;
1924
1925           xtensa_hard_regno_mode_ok[(int) mode][regno] = temp;
1926         }
1927     }
1928
1929   init_machine_status = xtensa_init_machine_status;
1930
1931   /* Check PIC settings.  PIC is only supported when using L32R
1932      instructions, and some targets need to always use PIC.  */
1933   if (flag_pic && TARGET_CONST16)
1934     error ("-f%s is not supported with CONST16 instructions",
1935            (flag_pic > 1 ? "PIC" : "pic"));
1936   else if (XTENSA_ALWAYS_PIC)
1937     {
1938       if (TARGET_CONST16)
1939         error ("PIC is required but not supported with CONST16 instructions");
1940       flag_pic = 1;
1941     }
1942   /* There's no need for -fPIC (as opposed to -fpic) on Xtensa.  */
1943   if (flag_pic > 1)
1944     flag_pic = 1;
1945 }
1946
1947
1948 /* A C compound statement to output to stdio stream STREAM the
1949    assembler syntax for an instruction operand X.  X is an RTL
1950    expression.
1951
1952    CODE is a value that can be used to specify one of several ways
1953    of printing the operand.  It is used when identical operands
1954    must be printed differently depending on the context.  CODE
1955    comes from the '%' specification that was used to request
1956    printing of the operand.  If the specification was just '%DIGIT'
1957    then CODE is 0; if the specification was '%LTR DIGIT' then CODE
1958    is the ASCII code for LTR.
1959
1960    If X is a register, this macro should print the register's name.
1961    The names can be found in an array 'reg_names' whose type is
1962    'char *[]'.  'reg_names' is initialized from 'REGISTER_NAMES'.
1963
1964    When the machine description has a specification '%PUNCT' (a '%'
1965    followed by a punctuation character), this macro is called with
1966    a null pointer for X and the punctuation character for CODE.
1967
1968    'a', 'c', 'l', and 'n' are reserved.
1969
1970    The Xtensa specific codes are:
1971
1972    'd'  CONST_INT, print as signed decimal
1973    'x'  CONST_INT, print as signed hexadecimal
1974    'K'  CONST_INT, print number of bits in mask for EXTUI
1975    'R'  CONST_INT, print (X & 0x1f)
1976    'L'  CONST_INT, print ((32 - X) & 0x1f)
1977    'D'  REG, print second register of double-word register operand
1978    'N'  MEM, print address of next word following a memory operand
1979    'v'  MEM, if memory reference is volatile, output a MEMW before it
1980    't'  any constant, add "@h" suffix for top 16 bits
1981    'b'  any constant, add "@l" suffix for bottom 16 bits
1982 */
1983
1984 static void
1985 printx (file, val)
1986      FILE *file;
1987      signed int val;
1988 {
1989   /* print a hexadecimal value in a nice way */
1990   if ((val > -0xa) && (val < 0xa))
1991     fprintf (file, "%d", val);
1992   else if (val < 0)
1993     fprintf (file, "-0x%x", -val);
1994   else
1995     fprintf (file, "0x%x", val);
1996 }
1997
1998
1999 void
2000 print_operand (file, x, letter)
2001      FILE *file;                /* file to write to */
2002      rtx x;                     /* operand to print */
2003      int letter;                /* %<letter> or 0 */
2004 {
2005   if (!x)
2006     error ("PRINT_OPERAND null pointer");
2007
2008   switch (letter)
2009     {
2010     case 'D':
2011       if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2012         fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
2013       else
2014         output_operand_lossage ("invalid %%D value");
2015       break;
2016
2017     case 'v':
2018       if (GET_CODE (x) == MEM)
2019         {
2020           /* For a volatile memory reference, emit a MEMW before the
2021              load or store.  */
2022           if (MEM_VOLATILE_P (x))
2023             fprintf (file, "memw\n\t");
2024         }
2025       else
2026         output_operand_lossage ("invalid %%v value");
2027       break;
2028
2029     case 'N':
2030       if (GET_CODE (x) == MEM
2031           && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
2032         {
2033           x = adjust_address (x, GET_MODE (x) == DFmode ? SFmode : SImode, 4);
2034           output_address (XEXP (x, 0));
2035         }
2036       else
2037         output_operand_lossage ("invalid %%N value");
2038       break;
2039
2040     case 'K':
2041       if (GET_CODE (x) == CONST_INT)
2042         {
2043           int num_bits = 0;
2044           unsigned val = INTVAL (x);
2045           while (val & 1)
2046             {
2047               num_bits += 1;
2048               val = val >> 1;
2049             }
2050           if ((val != 0) || (num_bits == 0) || (num_bits > 16))
2051             fatal_insn ("invalid mask", x);
2052
2053           fprintf (file, "%d", num_bits);
2054         }
2055       else
2056         output_operand_lossage ("invalid %%K value");
2057       break;
2058
2059     case 'L':
2060       if (GET_CODE (x) == CONST_INT)
2061         fprintf (file, "%ld", (32 - INTVAL (x)) & 0x1f);
2062       else
2063         output_operand_lossage ("invalid %%L value");
2064       break;
2065
2066     case 'R':
2067       if (GET_CODE (x) == CONST_INT)
2068         fprintf (file, "%ld", INTVAL (x) & 0x1f);
2069       else
2070         output_operand_lossage ("invalid %%R value");
2071       break;
2072
2073     case 'x':
2074       if (GET_CODE (x) == CONST_INT)
2075         printx (file, INTVAL (x));
2076       else
2077         output_operand_lossage ("invalid %%x value");
2078       break;
2079
2080     case 'd':
2081       if (GET_CODE (x) == CONST_INT)
2082         fprintf (file, "%ld", INTVAL (x));
2083       else
2084         output_operand_lossage ("invalid %%d value");
2085       break;
2086
2087     case 't':
2088     case 'b':
2089       if (GET_CODE (x) == CONST_INT)
2090         {
2091           printx (file, INTVAL (x));
2092           fputs (letter == 't' ? "@h" : "@l", file);
2093         }
2094       else if (GET_CODE (x) == CONST_DOUBLE)
2095         {
2096           REAL_VALUE_TYPE r;
2097           REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2098           if (GET_MODE (x) == SFmode)
2099             {
2100               long l;
2101               REAL_VALUE_TO_TARGET_SINGLE (r, l);
2102               fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
2103             }
2104           else
2105             output_operand_lossage ("invalid %%t/%%b value");
2106         }
2107       else if (GET_CODE (x) == CONST)
2108         {
2109           /* X must be a symbolic constant on ELF.  Write an expression
2110              suitable for 'const16' that sets the high or low 16 bits.  */
2111           if (GET_CODE (XEXP (x, 0)) != PLUS
2112               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
2113                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2114               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
2115             output_operand_lossage ("invalid %%t/%%b value");
2116           print_operand (file, XEXP (XEXP (x, 0), 0), 0);
2117           fputs (letter == 't' ? "@h" : "@l", file);
2118           /* There must be a non-alphanumeric character between 'h' or 'l'
2119              and the number.  The '-' is added by print_operand() already.  */
2120           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
2121             fputs ("+", file);
2122           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
2123         }
2124       else
2125         {
2126           output_addr_const (file, x);
2127           fputs (letter == 't' ? "@h" : "@l", file);
2128         }
2129       break;
2130
2131     default:
2132       if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2133         fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
2134       else if (GET_CODE (x) == MEM)
2135         output_address (XEXP (x, 0));
2136       else if (GET_CODE (x) == CONST_INT)
2137         fprintf (file, "%ld", INTVAL (x));
2138       else
2139         output_addr_const (file, x);
2140     }
2141 }
2142
2143
2144 /* A C compound statement to output to stdio stream STREAM the
2145    assembler syntax for an instruction operand that is a memory
2146    reference whose address is ADDR.  ADDR is an RTL expression.  */
2147
2148 void
2149 print_operand_address (file, addr)
2150      FILE *file;
2151      rtx addr;
2152 {
2153   if (!addr)
2154     error ("PRINT_OPERAND_ADDRESS, null pointer");
2155
2156   switch (GET_CODE (addr))
2157     {
2158     default:
2159       fatal_insn ("invalid address", addr);
2160       break;
2161
2162     case REG:
2163       fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
2164       break;
2165
2166     case PLUS:
2167       {
2168         rtx reg = (rtx)0;
2169         rtx offset = (rtx)0;
2170         rtx arg0 = XEXP (addr, 0);
2171         rtx arg1 = XEXP (addr, 1);
2172
2173         if (GET_CODE (arg0) == REG)
2174           {
2175             reg = arg0;
2176             offset = arg1;
2177           }
2178         else if (GET_CODE (arg1) == REG)
2179           {
2180             reg = arg1;
2181             offset = arg0;
2182           }
2183         else
2184           fatal_insn ("no register in address", addr);
2185
2186         if (CONSTANT_P (offset))
2187           {
2188             fprintf (file, "%s, ", reg_names [REGNO (reg)]);
2189             output_addr_const (file, offset);
2190           }
2191         else
2192           fatal_insn ("address offset not a constant", addr);
2193       }
2194       break;
2195
2196     case LABEL_REF:
2197     case SYMBOL_REF:
2198     case CONST_INT:
2199     case CONST:
2200       output_addr_const (file, addr);
2201       break;
2202     }
2203 }
2204
2205
2206 void
2207 xtensa_output_literal (file, x, mode, labelno)
2208      FILE *file;
2209      rtx x;
2210      enum machine_mode mode;
2211      int labelno;
2212 {
2213   long value_long[2];
2214   REAL_VALUE_TYPE r;
2215   int size;
2216
2217   fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
2218
2219   switch (GET_MODE_CLASS (mode))
2220     {
2221     case MODE_FLOAT:
2222       if (GET_CODE (x) != CONST_DOUBLE)
2223         abort ();
2224
2225       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2226       switch (mode)
2227         {
2228         case SFmode:
2229           REAL_VALUE_TO_TARGET_SINGLE (r, value_long[0]);
2230           fprintf (file, "0x%08lx\n", value_long[0]);
2231           break;
2232
2233         case DFmode:
2234           REAL_VALUE_TO_TARGET_DOUBLE (r, value_long);
2235           fprintf (file, "0x%08lx, 0x%08lx\n",
2236                    value_long[0], value_long[1]);
2237           break;
2238
2239         default:
2240           abort ();
2241         }
2242
2243       break;
2244
2245     case MODE_INT:
2246     case MODE_PARTIAL_INT:
2247       size = GET_MODE_SIZE (mode);
2248       if (size == 4)
2249         {
2250           output_addr_const (file, x);
2251           fputs ("\n", file);
2252         }
2253       else if (size == 8)
2254         {
2255           output_addr_const (file, operand_subword (x, 0, 0, DImode));
2256           fputs (", ", file);
2257           output_addr_const (file, operand_subword (x, 1, 0, DImode));
2258           fputs ("\n", file);
2259         }
2260       else
2261         abort ();
2262       break;
2263
2264     default:
2265       abort ();
2266     }
2267 }
2268
2269
2270 /* Return the bytes needed to compute the frame pointer from the current
2271    stack pointer.  */
2272
2273 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
2274 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
2275
2276 long
2277 compute_frame_size (size)
2278      int size;                  /* # of var. bytes allocated */
2279 {
2280   /* add space for the incoming static chain value */
2281   if (current_function_needs_context)
2282     size += (1 * UNITS_PER_WORD);
2283
2284   xtensa_current_frame_size =
2285     XTENSA_STACK_ALIGN (size
2286                         + current_function_outgoing_args_size
2287                         + (WINDOW_SIZE * UNITS_PER_WORD));
2288   return xtensa_current_frame_size;
2289 }
2290
2291
2292 int
2293 xtensa_frame_pointer_required ()
2294 {
2295   /* The code to expand builtin_frame_addr and builtin_return_addr
2296      currently uses the hard_frame_pointer instead of frame_pointer.
2297      This seems wrong but maybe it's necessary for other architectures.
2298      This function is derived from the i386 code.  */
2299
2300   if (cfun->machine->accesses_prev_frame)
2301     return 1;
2302
2303   return 0;
2304 }
2305
2306
2307 void
2308 xtensa_expand_prologue ()
2309 {
2310   HOST_WIDE_INT total_size;
2311   rtx size_rtx;
2312
2313   total_size = compute_frame_size (get_frame_size ());
2314   size_rtx = GEN_INT (total_size);
2315
2316   if (total_size < (1 << (12+3)))
2317     emit_insn (gen_entry (size_rtx, size_rtx));
2318   else
2319     {
2320       /* Use a8 as a temporary since a0-a7 may be live.  */
2321       rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
2322       emit_insn (gen_entry (size_rtx, GEN_INT (MIN_FRAME_SIZE)));
2323       emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
2324       emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
2325       emit_move_insn (stack_pointer_rtx, tmp_reg);
2326     }
2327
2328   if (frame_pointer_needed)
2329     {
2330       rtx first, insn, set_frame_ptr_insn = 0;
2331
2332       push_topmost_sequence ();
2333       first = get_insns ();
2334       pop_topmost_sequence ();
2335
2336       /* Search all instructions, looking for the insn that sets up the
2337          frame pointer.  This search will fail if the function does not
2338          have an incoming argument in $a7, but in that case, we can just
2339          set up the frame pointer at the very beginning of the
2340          function.  */
2341
2342       for (insn = first; insn; insn = NEXT_INSN (insn))
2343         {
2344           rtx pat;
2345
2346           if (!INSN_P (insn))
2347             continue;
2348
2349           pat = PATTERN (insn);
2350           if (GET_CODE (pat) == SET
2351               && GET_CODE (SET_SRC (pat)) == UNSPEC_VOLATILE
2352               && (XINT (SET_SRC (pat), 1) == UNSPECV_SET_FP))
2353             {
2354               set_frame_ptr_insn = insn;
2355               break;
2356             }
2357         }
2358
2359       if (set_frame_ptr_insn)
2360         {
2361           /* For all instructions prior to set_frame_ptr_insn, replace
2362              hard_frame_pointer references with stack_pointer.  */
2363           for (insn = first;
2364                insn != set_frame_ptr_insn;
2365                insn = NEXT_INSN (insn))
2366             {
2367               if (INSN_P (insn))
2368                 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
2369                                               hard_frame_pointer_rtx,
2370                                               stack_pointer_rtx);
2371             }
2372         }
2373       else
2374         emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
2375     }
2376 }
2377
2378
2379 /* Clear variables at function end.  */
2380
2381 void
2382 xtensa_function_epilogue (file, size)
2383      FILE *file ATTRIBUTE_UNUSED;
2384      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
2385 {
2386   xtensa_current_frame_size = 0;
2387 }
2388
2389
2390 rtx
2391 xtensa_return_addr (count, frame)
2392      int count;
2393      rtx frame;
2394 {
2395   rtx result, retaddr;
2396
2397   if (count == -1)
2398     retaddr = gen_rtx_REG (Pmode, A0_REG);
2399   else
2400     {
2401       rtx addr = plus_constant (frame, -4 * UNITS_PER_WORD);
2402       addr = memory_address (Pmode, addr);
2403       retaddr = gen_reg_rtx (Pmode);
2404       emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
2405     }
2406
2407   /* The 2 most-significant bits of the return address on Xtensa hold
2408      the register window size.  To get the real return address, these
2409      bits must be replaced with the high bits from the current PC.  */
2410
2411   result = gen_reg_rtx (Pmode);
2412   emit_insn (gen_fix_return_addr (result, retaddr));
2413   return result;
2414 }
2415
2416
2417 /* Create the va_list data type.
2418    This structure is set up by __builtin_saveregs.  The __va_reg
2419    field points to a stack-allocated region holding the contents of the
2420    incoming argument registers.  The __va_ndx field is an index initialized
2421    to the position of the first unnamed (variable) argument.  This same index
2422    is also used to address the arguments passed in memory.  Thus, the
2423    __va_stk field is initialized to point to the position of the first
2424    argument in memory offset to account for the arguments passed in
2425    registers.  E.G., if there are 6 argument registers, and each register is
2426    4 bytes, then __va_stk is set to $sp - (6 * 4); then __va_reg[N*4]
2427    references argument word N for 0 <= N < 6, and __va_stk[N*4] references
2428    argument word N for N >= 6.  */
2429
2430 tree
2431 xtensa_build_va_list ()
2432 {
2433   tree f_stk, f_reg, f_ndx, record, type_decl;
2434
2435   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2436   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2437
2438   f_stk = build_decl (FIELD_DECL, get_identifier ("__va_stk"),
2439                       ptr_type_node);
2440   f_reg = build_decl (FIELD_DECL, get_identifier ("__va_reg"),
2441                       ptr_type_node);
2442   f_ndx = build_decl (FIELD_DECL, get_identifier ("__va_ndx"),
2443                       integer_type_node);
2444
2445   DECL_FIELD_CONTEXT (f_stk) = record;
2446   DECL_FIELD_CONTEXT (f_reg) = record;
2447   DECL_FIELD_CONTEXT (f_ndx) = record;
2448
2449   TREE_CHAIN (record) = type_decl;
2450   TYPE_NAME (record) = type_decl;
2451   TYPE_FIELDS (record) = f_stk;
2452   TREE_CHAIN (f_stk) = f_reg;
2453   TREE_CHAIN (f_reg) = f_ndx;
2454
2455   layout_type (record);
2456   return record;
2457 }
2458
2459
2460 /* Save the incoming argument registers on the stack.  Returns the
2461    address of the saved registers.  */
2462
2463 rtx
2464 xtensa_builtin_saveregs ()
2465 {
2466   rtx gp_regs, dest;
2467   int arg_words = current_function_arg_words;
2468   int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
2469   int i;
2470
2471   if (gp_left == 0)
2472     return const0_rtx;
2473
2474   /* allocate the general-purpose register space */
2475   gp_regs = assign_stack_local
2476     (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
2477   set_mem_alias_set (gp_regs, get_varargs_alias_set ());
2478
2479   /* Now store the incoming registers.  */
2480   dest = change_address (gp_regs, SImode,
2481                          plus_constant (XEXP (gp_regs, 0),
2482                                         arg_words * UNITS_PER_WORD));
2483
2484   /* Note: Don't use move_block_from_reg() here because the incoming
2485      argument in a7 cannot be represented by hard_frame_pointer_rtx.
2486      Instead, call gen_raw_REG() directly so that we get a distinct
2487      instance of (REG:SI 7).  */
2488   for (i = 0; i < gp_left; i++)
2489     {
2490       emit_move_insn (operand_subword (dest, i, 1, BLKmode),
2491                       gen_raw_REG (SImode, GP_ARG_FIRST + arg_words + i));
2492     }
2493
2494   return XEXP (gp_regs, 0);
2495 }
2496
2497
2498 /* Implement `va_start' for varargs and stdarg.  We look at the
2499    current function to fill in an initial va_list.  */
2500
2501 void
2502 xtensa_va_start (valist, nextarg)
2503      tree valist;
2504      rtx nextarg ATTRIBUTE_UNUSED;
2505 {
2506   tree f_stk, stk;
2507   tree f_reg, reg;
2508   tree f_ndx, ndx;
2509   tree t, u;
2510   int arg_words;
2511
2512   arg_words = current_function_args_info.arg_words;
2513
2514   f_stk = TYPE_FIELDS (va_list_type_node);
2515   f_reg = TREE_CHAIN (f_stk);
2516   f_ndx = TREE_CHAIN (f_reg);
2517
2518   stk = build (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk);
2519   reg = build (COMPONENT_REF, TREE_TYPE (f_reg), valist, f_reg);
2520   ndx = build (COMPONENT_REF, TREE_TYPE (f_ndx), valist, f_ndx);
2521
2522   /* Call __builtin_saveregs; save the result in __va_reg */
2523   current_function_arg_words = arg_words;
2524   u = make_tree (ptr_type_node, expand_builtin_saveregs ());
2525   t = build (MODIFY_EXPR, ptr_type_node, reg, u);
2526   TREE_SIDE_EFFECTS (t) = 1;
2527   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2528
2529   /* Set the __va_stk member to $arg_ptr - (size of __va_reg area) */
2530   u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
2531   u = fold (build (PLUS_EXPR, ptr_type_node, u,
2532                    build_int_2 (-MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1)));
2533   t = build (MODIFY_EXPR, ptr_type_node, stk, u);
2534   TREE_SIDE_EFFECTS (t) = 1;
2535   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2536
2537   /* Set the __va_ndx member.  */
2538   u = build_int_2 (arg_words * UNITS_PER_WORD, 0);
2539   t = build (MODIFY_EXPR, integer_type_node, ndx, u);
2540   TREE_SIDE_EFFECTS (t) = 1;
2541   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2542 }
2543
2544
2545 /* Implement `va_arg'.  */
2546
2547 rtx
2548 xtensa_va_arg (valist, type)
2549      tree valist, type;
2550 {
2551   tree f_stk, stk;
2552   tree f_reg, reg;
2553   tree f_ndx, ndx;
2554   tree tmp, addr_tree, type_size;
2555   rtx array, orig_ndx, r, addr, size, va_size;
2556   rtx lab_false, lab_over, lab_false2;
2557
2558   f_stk = TYPE_FIELDS (va_list_type_node);
2559   f_reg = TREE_CHAIN (f_stk);
2560   f_ndx = TREE_CHAIN (f_reg);
2561
2562   stk = build (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk);
2563   reg = build (COMPONENT_REF, TREE_TYPE (f_reg), valist, f_reg);
2564   ndx = build (COMPONENT_REF, TREE_TYPE (f_ndx), valist, f_ndx);
2565
2566   type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type));
2567
2568   va_size = gen_reg_rtx (SImode);
2569   tmp = fold (build (MULT_EXPR, sizetype,
2570                      fold (build (TRUNC_DIV_EXPR, sizetype,
2571                                   fold (build (PLUS_EXPR, sizetype,
2572                                                type_size,
2573                                                size_int (UNITS_PER_WORD - 1))),
2574                                   size_int (UNITS_PER_WORD))),
2575                      size_int (UNITS_PER_WORD)));
2576   r = expand_expr (tmp, va_size, SImode, EXPAND_NORMAL);
2577   if (r != va_size)
2578     emit_move_insn (va_size, r);
2579
2580
2581   /* First align __va_ndx to a double word boundary if necessary for this arg:
2582
2583      if (__alignof__ (TYPE) > 4)
2584        (AP).__va_ndx = (((AP).__va_ndx + 7) & -8)
2585   */
2586
2587   if (TYPE_ALIGN (type) > BITS_PER_WORD)
2588     {
2589       tmp = build (PLUS_EXPR, integer_type_node, ndx,
2590                    build_int_2 ((2 * UNITS_PER_WORD) - 1, 0));
2591       tmp = build (BIT_AND_EXPR, integer_type_node, tmp,
2592                    build_int_2 (-2 * UNITS_PER_WORD, -1));
2593       tmp = build (MODIFY_EXPR, integer_type_node, ndx, tmp);
2594       TREE_SIDE_EFFECTS (tmp) = 1;
2595       expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2596     }
2597
2598
2599   /* Increment __va_ndx to point past the argument:
2600
2601      orig_ndx = (AP).__va_ndx;
2602      (AP).__va_ndx += __va_size (TYPE);
2603   */
2604
2605   orig_ndx = gen_reg_rtx (SImode);
2606   r = expand_expr (ndx, orig_ndx, SImode, EXPAND_NORMAL);
2607   if (r != orig_ndx)
2608     emit_move_insn (orig_ndx, r);
2609
2610   tmp = build (PLUS_EXPR, integer_type_node, ndx,
2611                make_tree (intSI_type_node, va_size));
2612   tmp = build (MODIFY_EXPR, integer_type_node, ndx, tmp);
2613   TREE_SIDE_EFFECTS (tmp) = 1;
2614   expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2615
2616
2617   /* Check if the argument is in registers:
2618
2619      if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
2620          && !MUST_PASS_IN_STACK (type))
2621         __array = (AP).__va_reg;
2622   */
2623
2624   array = gen_reg_rtx (Pmode);
2625
2626   lab_over = NULL_RTX;
2627   if (!MUST_PASS_IN_STACK (VOIDmode, type))
2628     {
2629       lab_false = gen_label_rtx ();
2630       lab_over = gen_label_rtx ();
2631
2632       emit_cmp_and_jump_insns (expand_expr (ndx, NULL_RTX, SImode,
2633                                             EXPAND_NORMAL),
2634                                GEN_INT (MAX_ARGS_IN_REGISTERS
2635                                         * UNITS_PER_WORD),
2636                                GT, const1_rtx, SImode, 0, lab_false);
2637
2638       r = expand_expr (reg, array, Pmode, EXPAND_NORMAL);
2639       if (r != array)
2640         emit_move_insn (array, r);
2641
2642       emit_jump_insn (gen_jump (lab_over));
2643       emit_barrier ();
2644       emit_label (lab_false);
2645     }
2646
2647   /* ...otherwise, the argument is on the stack (never split between
2648      registers and the stack -- change __va_ndx if necessary):
2649
2650      else
2651        {
2652          if (orig_ndx < __MAX_ARGS_IN_REGISTERS * 4)
2653              (AP).__va_ndx = __MAX_ARGS_IN_REGISTERS * 4 + __va_size (TYPE);
2654          __array = (AP).__va_stk;
2655        }
2656   */
2657
2658   lab_false2 = gen_label_rtx ();
2659   emit_cmp_and_jump_insns (orig_ndx,
2660                            GEN_INT (MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD),
2661                            GE, const1_rtx, SImode, 0, lab_false2);
2662
2663   tmp = build (PLUS_EXPR, sizetype, make_tree (intSI_type_node, va_size),
2664                build_int_2 (MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, 0));
2665   tmp = build (MODIFY_EXPR, integer_type_node, ndx, tmp);
2666   TREE_SIDE_EFFECTS (tmp) = 1;
2667   expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2668
2669   emit_label (lab_false2);
2670
2671   r = expand_expr (stk, array, Pmode, EXPAND_NORMAL);
2672   if (r != array)
2673     emit_move_insn (array, r);
2674
2675   if (lab_over != NULL_RTX)
2676     emit_label (lab_over);
2677
2678
2679   /* Given the base array pointer (__array) and index to the subsequent
2680      argument (__va_ndx), find the address:
2681
2682      __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
2683                                 ? sizeof (TYPE)
2684                                 : __va_size (TYPE))
2685
2686      The results are endian-dependent because values smaller than one word
2687      are aligned differently.
2688   */
2689
2690   size = gen_reg_rtx (SImode);
2691   emit_move_insn (size, va_size);
2692
2693   if (BYTES_BIG_ENDIAN)
2694     {
2695       rtx lab_use_va_size = gen_label_rtx ();
2696
2697       emit_cmp_and_jump_insns (expand_expr (type_size, NULL_RTX, SImode,
2698                                             EXPAND_NORMAL),
2699                                GEN_INT (PARM_BOUNDARY / BITS_PER_UNIT),
2700                                GE, const1_rtx, SImode, 0, lab_use_va_size);
2701
2702       r = expand_expr (type_size, size, SImode, EXPAND_NORMAL);
2703       if (r != size)
2704         emit_move_insn (size, r);
2705
2706       emit_label (lab_use_va_size);
2707     }
2708
2709   addr_tree = build (PLUS_EXPR, ptr_type_node,
2710                      make_tree (ptr_type_node, array),
2711                      ndx);
2712   addr_tree = build (MINUS_EXPR, ptr_type_node, addr_tree,
2713                      make_tree (intSI_type_node, size));
2714   addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2715   addr = copy_to_reg (addr);
2716   return addr;
2717 }
2718
2719
2720 enum reg_class
2721 xtensa_preferred_reload_class (x, class, isoutput)
2722      rtx x;
2723      enum reg_class class;
2724      int isoutput;
2725 {
2726   if (!isoutput && CONSTANT_P (x) && GET_CODE (x) == CONST_DOUBLE)
2727     return NO_REGS;
2728
2729   /* Don't use the stack pointer or hard frame pointer for reloads!
2730      The hard frame pointer would normally be OK except that it may
2731      briefly hold an incoming argument in the prologue, and reload
2732      won't know that it is live because the hard frame pointer is
2733      treated specially.  */
2734
2735   if (class == AR_REGS || class == GR_REGS)
2736     return RL_REGS;
2737
2738   return class;
2739 }
2740
2741
2742 enum reg_class
2743 xtensa_secondary_reload_class (class, mode, x, isoutput)
2744      enum reg_class class;
2745      enum machine_mode mode ATTRIBUTE_UNUSED;
2746      rtx x;
2747      int isoutput;
2748 {
2749   int regno;
2750
2751   if (GET_CODE (x) == SIGN_EXTEND)
2752     x = XEXP (x, 0);
2753   regno = xt_true_regnum (x);
2754
2755   if (!isoutput)
2756     {
2757       if (class == FP_REGS && constantpool_mem_p (x))
2758         return RL_REGS;
2759     }
2760
2761   if (ACC_REG_P (regno))
2762     return ((class == GR_REGS || class == RL_REGS) ? NO_REGS : RL_REGS);
2763   if (class == ACC_REG)
2764     return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
2765
2766   return NO_REGS;
2767 }
2768
2769
2770 void
2771 order_regs_for_local_alloc ()
2772 {
2773   if (!leaf_function_p ())
2774     {
2775       memcpy (reg_alloc_order, reg_nonleaf_alloc_order,
2776               FIRST_PSEUDO_REGISTER * sizeof (int));
2777     }
2778   else
2779     {
2780       int i, num_arg_regs;
2781       int nxt = 0;
2782
2783       /* use the AR registers in increasing order (skipping a0 and a1)
2784          but save the incoming argument registers for a last resort */
2785       num_arg_regs = current_function_args_info.arg_words;
2786       if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
2787         num_arg_regs = MAX_ARGS_IN_REGISTERS;
2788       for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
2789         reg_alloc_order[nxt++] = i + num_arg_regs;
2790       for (i = 0; i < num_arg_regs; i++)
2791         reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
2792
2793       /* list the coprocessor registers in order */
2794       for (i = 0; i < BR_REG_NUM; i++)
2795         reg_alloc_order[nxt++] = BR_REG_FIRST + i;
2796
2797       /* list the FP registers in order for now */
2798       for (i = 0; i < 16; i++)
2799         reg_alloc_order[nxt++] = FP_REG_FIRST + i;
2800
2801       /* GCC requires that we list *all* the registers....  */
2802       reg_alloc_order[nxt++] = 0;       /* a0 = return address */
2803       reg_alloc_order[nxt++] = 1;       /* a1 = stack pointer */
2804       reg_alloc_order[nxt++] = 16;      /* pseudo frame pointer */
2805       reg_alloc_order[nxt++] = 17;      /* pseudo arg pointer */
2806
2807       reg_alloc_order[nxt++] = ACC_REG_FIRST;   /* MAC16 accumulator */
2808     }
2809 }
2810
2811
2812 /* A customized version of reg_overlap_mentioned_p that only looks for
2813    references to a7 (as opposed to hard_frame_pointer_rtx).  */
2814
2815 int
2816 a7_overlap_mentioned_p (x)
2817      rtx x;
2818 {
2819   int i, j;
2820   unsigned int x_regno;
2821   const char *fmt;
2822
2823   if (GET_CODE (x) == REG)
2824     {
2825       x_regno = REGNO (x);
2826       return (x != hard_frame_pointer_rtx
2827               && x_regno < A7_REG + 1
2828               && x_regno + HARD_REGNO_NREGS (A7_REG, GET_MODE (x)) > A7_REG);
2829     }
2830
2831   if (GET_CODE (x) == SUBREG
2832       && GET_CODE (SUBREG_REG (x)) == REG
2833       && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
2834     {
2835       x_regno = subreg_regno (x);
2836       return (SUBREG_REG (x) != hard_frame_pointer_rtx
2837               && x_regno < A7_REG + 1
2838               && x_regno + HARD_REGNO_NREGS (A7_REG, GET_MODE (x)) > A7_REG);
2839     }
2840
2841   /* X does not match, so try its subexpressions.  */
2842   fmt = GET_RTX_FORMAT (GET_CODE (x));
2843   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2844     {
2845       if (fmt[i] == 'e')
2846         {
2847           if (a7_overlap_mentioned_p (XEXP (x, i)))
2848             return 1;
2849         }
2850       else if (fmt[i] == 'E')
2851         {
2852           for (j = XVECLEN (x, i) - 1; j >=0; j--)
2853             if (a7_overlap_mentioned_p (XVECEXP (x, i, j)))
2854               return 1;
2855         }
2856     }
2857
2858   return 0;
2859 }
2860
2861
2862 /* Some Xtensa targets support multiple bss sections.  If the section
2863    name ends with ".bss", add SECTION_BSS to the flags.  */
2864
2865 static unsigned int
2866 xtensa_multibss_section_type_flags (decl, name, reloc)
2867      tree decl;
2868      const char *name;
2869      int reloc;
2870 {
2871   unsigned int flags = default_section_type_flags (decl, name, reloc);
2872   const char *suffix;
2873
2874   suffix = strrchr (name, '.');
2875   if (suffix && strcmp (suffix, ".bss") == 0)
2876     {
2877       if (!decl || (TREE_CODE (decl) == VAR_DECL
2878                     && DECL_INITIAL (decl) == NULL_TREE))
2879         flags |= SECTION_BSS;  /* @nobits */
2880       else
2881         warning ("only uninitialized variables can be placed in a "
2882                  ".bss section");
2883     }
2884
2885   return flags;
2886 }
2887
2888
2889 /* The literal pool stays with the function.  */
2890
2891 static void
2892 xtensa_select_rtx_section (mode, x, align)
2893      enum machine_mode mode ATTRIBUTE_UNUSED;
2894      rtx x ATTRIBUTE_UNUSED;
2895      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
2896 {
2897   function_section (current_function_decl);
2898 }
2899
2900 /* Compute a (partial) cost for rtx X.  Return true if the complete
2901    cost has been computed, and false if subexpressions should be
2902    scanned.  In either case, *TOTAL contains the cost result.  */
2903
2904 static bool
2905 xtensa_rtx_costs (x, code, outer_code, total)
2906      rtx x;
2907      int code, outer_code;
2908      int *total;
2909 {
2910   switch (code)
2911     {
2912     case CONST_INT:
2913       switch (outer_code)
2914         {
2915         case SET:
2916           if (xtensa_simm12b (INTVAL (x)))
2917             {
2918               *total = 4;
2919               return true;
2920             }
2921           break;
2922         case PLUS:
2923           if (xtensa_simm8 (INTVAL (x))
2924               || xtensa_simm8x256 (INTVAL (x)))
2925             {
2926               *total = 0;
2927               return true;
2928             }
2929           break;
2930         case AND:
2931           if (xtensa_mask_immediate (INTVAL (x)))
2932             {
2933               *total = 0;
2934               return true;
2935             }
2936           break;
2937         case COMPARE:
2938           if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
2939             {
2940               *total = 0;
2941               return true;
2942             }
2943           break;
2944         case ASHIFT:
2945         case ASHIFTRT:
2946         case LSHIFTRT:
2947         case ROTATE:
2948         case ROTATERT:
2949           /* no way to tell if X is the 2nd operand so be conservative */
2950         default: break;
2951         }
2952       if (xtensa_simm12b (INTVAL (x)))
2953         *total = 5;
2954       else if (TARGET_CONST16)
2955         *total = COSTS_N_INSNS (2);
2956       else
2957         *total = 6;
2958       return true;
2959
2960     case CONST:
2961     case LABEL_REF:
2962     case SYMBOL_REF:
2963       if (TARGET_CONST16)
2964         *total = COSTS_N_INSNS (2);
2965       else
2966         *total = 5;
2967       return true;
2968
2969     case CONST_DOUBLE:
2970       if (TARGET_CONST16)
2971         *total = COSTS_N_INSNS (4);
2972       else
2973         *total = 7;
2974       return true;
2975
2976     case MEM:
2977       {
2978         int num_words =
2979           (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) ?  2 : 1;
2980
2981         if (memory_address_p (GET_MODE (x), XEXP ((x), 0)))
2982           *total = COSTS_N_INSNS (num_words);
2983         else
2984           *total = COSTS_N_INSNS (2*num_words);
2985         return true;
2986       }
2987
2988     case FFS:
2989       *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
2990       return true;
2991
2992     case NOT:
2993       *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 3 : 2);
2994       return true;
2995
2996     case AND:
2997     case IOR:
2998     case XOR:
2999       if (GET_MODE (x) == DImode)
3000         *total = COSTS_N_INSNS (2);
3001       else
3002         *total = COSTS_N_INSNS (1);
3003       return true;
3004
3005     case ASHIFT:
3006     case ASHIFTRT:
3007     case LSHIFTRT:
3008       if (GET_MODE (x) == DImode)
3009         *total = COSTS_N_INSNS (50);
3010       else
3011         *total = COSTS_N_INSNS (1);
3012       return true;
3013
3014     case ABS:
3015       {
3016         enum machine_mode xmode = GET_MODE (x);
3017         if (xmode == SFmode)
3018           *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3019         else if (xmode == DFmode)
3020           *total = COSTS_N_INSNS (50);
3021         else
3022           *total = COSTS_N_INSNS (4);
3023         return true;
3024       }
3025
3026     case PLUS:
3027     case MINUS:
3028       {
3029         enum machine_mode xmode = GET_MODE (x);
3030         if (xmode == SFmode)
3031           *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3032         else if (xmode == DFmode || xmode == DImode)
3033           *total = COSTS_N_INSNS (50);
3034         else
3035           *total = COSTS_N_INSNS (1);
3036         return true;
3037       }
3038
3039     case NEG:
3040       *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 4 : 2);
3041       return true;
3042
3043     case MULT:
3044       {
3045         enum machine_mode xmode = GET_MODE (x);
3046         if (xmode == SFmode)
3047           *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
3048         else if (xmode == DFmode || xmode == DImode)
3049           *total = COSTS_N_INSNS (50);
3050         else if (TARGET_MUL32)
3051           *total = COSTS_N_INSNS (4);
3052         else if (TARGET_MAC16)
3053           *total = COSTS_N_INSNS (16);
3054         else if (TARGET_MUL16)
3055           *total = COSTS_N_INSNS (12);
3056         else
3057           *total = COSTS_N_INSNS (50);
3058         return true;
3059       }
3060
3061     case DIV:
3062     case MOD:
3063       {
3064         enum machine_mode xmode = GET_MODE (x);
3065         if (xmode == SFmode)
3066           {
3067             *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
3068             return true;
3069           }
3070         else if (xmode == DFmode)
3071           {
3072             *total = COSTS_N_INSNS (50);
3073             return true;
3074           }
3075       }
3076       /* fall through */
3077
3078     case UDIV:
3079     case UMOD:
3080       {
3081         enum machine_mode xmode = GET_MODE (x);
3082         if (xmode == DImode)
3083           *total = COSTS_N_INSNS (50);
3084         else if (TARGET_DIV32)
3085           *total = COSTS_N_INSNS (32);
3086         else
3087           *total = COSTS_N_INSNS (50);
3088         return true;
3089       }
3090
3091     case SQRT:
3092       if (GET_MODE (x) == SFmode)
3093         *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
3094       else
3095         *total = COSTS_N_INSNS (50);
3096       return true;
3097
3098     case SMIN:
3099     case UMIN:
3100     case SMAX:
3101     case UMAX:
3102       *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
3103       return true;
3104
3105     case SIGN_EXTRACT:
3106     case SIGN_EXTEND:
3107       *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
3108       return true;
3109
3110     case ZERO_EXTRACT:
3111     case ZERO_EXTEND:
3112       *total = COSTS_N_INSNS (1);
3113       return true;
3114
3115     default:
3116       return false;
3117     }
3118 }
3119
3120 #include "gt-xtensa.h"