OSDN Git Service

Fix last commit.
[pf3gnuchains/gcc-fork.git] / gcc / config / c4x / c4x.c
1 /* Subroutines for assembler code output on the TMS320C[34]x
2    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003
3    Free Software Foundation, Inc.
4
5    Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz)
6               and Herman Ten Brugge (Haj.Ten.Brugge@net.HCC.nl).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25 /* Some output-actions in c4x.md need these.  */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "real.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
38 #include "conditions.h"
39 #include "output.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "libfuncs.h"
44 #include "flags.h"
45 #include "loop.h"
46 #include "recog.h"
47 #include "ggc.h"
48 #include "cpplib.h"
49 #include "toplev.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53
54 rtx smulhi3_libfunc;
55 rtx umulhi3_libfunc;
56 rtx fix_truncqfhi2_libfunc;
57 rtx fixuns_truncqfhi2_libfunc;
58 rtx fix_trunchfhi2_libfunc;
59 rtx fixuns_trunchfhi2_libfunc;
60 rtx floathiqf2_libfunc;
61 rtx floatunshiqf2_libfunc;
62 rtx floathihf2_libfunc;
63 rtx floatunshihf2_libfunc;
64
65 static int c4x_leaf_function;
66
67 static const char *const float_reg_names[] = FLOAT_REGISTER_NAMES;
68
69 /* Array of the smallest class containing reg number REGNO, indexed by
70    REGNO.  Used by REGNO_REG_CLASS in c4x.h.  We assume that all these
71    registers are available and set the class to NO_REGS for registers 
72    that the target switches say are unavailable.  */
73
74 enum reg_class c4x_regclass_map[FIRST_PSEUDO_REGISTER] =
75 {
76                                 /* Reg          Modes           Saved.  */
77   R0R1_REGS,                    /* R0           QI, QF, HF      No.  */
78   R0R1_REGS,                    /* R1           QI, QF, HF      No.  */
79   R2R3_REGS,                    /* R2           QI, QF, HF      No.  */
80   R2R3_REGS,                    /* R3           QI, QF, HF      No.  */
81   EXT_LOW_REGS,                 /* R4           QI, QF, HF      QI.  */
82   EXT_LOW_REGS,                 /* R5           QI, QF, HF      QI.  */
83   EXT_LOW_REGS,                 /* R6           QI, QF, HF      QF.  */
84   EXT_LOW_REGS,                 /* R7           QI, QF, HF      QF.  */
85   ADDR_REGS,                    /* AR0          QI              No.  */
86   ADDR_REGS,                    /* AR1          QI              No.  */
87   ADDR_REGS,                    /* AR2          QI              No.  */
88   ADDR_REGS,                    /* AR3          QI              QI.  */
89   ADDR_REGS,                    /* AR4          QI              QI.  */
90   ADDR_REGS,                    /* AR5          QI              QI.  */
91   ADDR_REGS,                    /* AR6          QI              QI.  */
92   ADDR_REGS,                    /* AR7          QI              QI.  */
93   DP_REG,                       /* DP           QI              No.  */
94   INDEX_REGS,                   /* IR0          QI              No.  */
95   INDEX_REGS,                   /* IR1          QI              No.  */
96   BK_REG,                       /* BK           QI              QI.  */
97   SP_REG,                       /* SP           QI              No.  */
98   ST_REG,                       /* ST           CC              No.  */
99   NO_REGS,                      /* DIE/IE                       No.  */
100   NO_REGS,                      /* IIE/IF                       No.  */
101   NO_REGS,                      /* IIF/IOF                      No.  */
102   INT_REGS,                     /* RS           QI              No.  */
103   INT_REGS,                     /* RE           QI              No.  */
104   RC_REG,                       /* RC           QI              No.  */
105   EXT_REGS,                     /* R8           QI, QF, HF      QI.  */
106   EXT_REGS,                     /* R9           QI, QF, HF      No.  */
107   EXT_REGS,                     /* R10          QI, QF, HF      No.  */
108   EXT_REGS,                     /* R11          QI, QF, HF      No.  */
109 };
110
111 enum machine_mode c4x_caller_save_map[FIRST_PSEUDO_REGISTER] =
112 {
113                                 /* Reg          Modes           Saved.  */
114   HFmode,                       /* R0           QI, QF, HF      No.  */
115   HFmode,                       /* R1           QI, QF, HF      No.  */
116   HFmode,                       /* R2           QI, QF, HF      No.  */
117   HFmode,                       /* R3           QI, QF, HF      No.  */
118   QFmode,                       /* R4           QI, QF, HF      QI.  */
119   QFmode,                       /* R5           QI, QF, HF      QI.  */
120   QImode,                       /* R6           QI, QF, HF      QF.  */
121   QImode,                       /* R7           QI, QF, HF      QF.  */
122   QImode,                       /* AR0          QI              No.  */
123   QImode,                       /* AR1          QI              No.  */
124   QImode,                       /* AR2          QI              No.  */
125   QImode,                       /* AR3          QI              QI.  */
126   QImode,                       /* AR4          QI              QI.  */
127   QImode,                       /* AR5          QI              QI.  */
128   QImode,                       /* AR6          QI              QI.  */
129   QImode,                       /* AR7          QI              QI.  */
130   VOIDmode,                     /* DP           QI              No.  */
131   QImode,                       /* IR0          QI              No.  */
132   QImode,                       /* IR1          QI              No.  */
133   QImode,                       /* BK           QI              QI.  */
134   VOIDmode,                     /* SP           QI              No.  */
135   VOIDmode,                     /* ST           CC              No.  */
136   VOIDmode,                     /* DIE/IE                       No.  */
137   VOIDmode,                     /* IIE/IF                       No.  */
138   VOIDmode,                     /* IIF/IOF                      No.  */
139   QImode,                       /* RS           QI              No.  */
140   QImode,                       /* RE           QI              No.  */
141   VOIDmode,                     /* RC           QI              No.  */
142   QFmode,                       /* R8           QI, QF, HF      QI.  */
143   HFmode,                       /* R9           QI, QF, HF      No.  */
144   HFmode,                       /* R10          QI, QF, HF      No.  */
145   HFmode,                       /* R11          QI, QF, HF      No.  */
146 };
147
148
149 /* Test and compare insns in c4x.md store the information needed to
150    generate branch and scc insns here.  */
151
152 rtx c4x_compare_op0;
153 rtx c4x_compare_op1;
154
155 const char *c4x_rpts_cycles_string;
156 int c4x_rpts_cycles = 0;        /* Max. cycles for RPTS.  */
157 const char *c4x_cpu_version_string;
158 int c4x_cpu_version = 40;       /* CPU version C30/31/32/33/40/44.  */
159
160 /* Pragma definitions.  */
161
162 tree code_tree = NULL_TREE;
163 tree data_tree = NULL_TREE;
164 tree pure_tree = NULL_TREE;
165 tree noreturn_tree = NULL_TREE;
166 tree interrupt_tree = NULL_TREE;
167 tree naked_tree = NULL_TREE;
168
169 /* Forward declarations */
170 static int c4x_isr_reg_used_p (unsigned int);
171 static int c4x_leaf_function_p (void);
172 static int c4x_naked_function_p (void);
173 static int c4x_immed_float_p (rtx);
174 static int c4x_a_register (rtx);
175 static int c4x_x_register (rtx);
176 static int c4x_immed_int_constant (rtx);
177 static int c4x_immed_float_constant (rtx);
178 static int c4x_K_constant (rtx);
179 static int c4x_N_constant (rtx);
180 static int c4x_O_constant (rtx);
181 static int c4x_R_indirect (rtx);
182 static int c4x_S_indirect (rtx);
183 static void c4x_S_address_parse (rtx , int *, int *, int *, int *);
184 static int c4x_valid_operands (enum rtx_code, rtx *, enum machine_mode, int);
185 static int c4x_arn_reg_operand (rtx, enum machine_mode, unsigned int);
186 static int c4x_arn_mem_operand (rtx, enum machine_mode, unsigned int);
187 static void c4x_file_start (void);
188 static void c4x_file_end (void);
189 static void c4x_check_attribute (const char *, tree, tree, tree *);
190 static int c4x_r11_set_p (rtx);
191 static int c4x_rptb_valid_p (rtx, rtx);
192 static void c4x_reorg (void);
193 static int c4x_label_ref_used_p (rtx, rtx);
194 static tree c4x_handle_fntype_attribute (tree *, tree, tree, int, bool *);
195 const struct attribute_spec c4x_attribute_table[];
196 static void c4x_insert_attributes (tree, tree *);
197 static void c4x_asm_named_section (const char *, unsigned int);
198 static int c4x_adjust_cost (rtx, rtx, rtx, int);
199 static void c4x_globalize_label (FILE *, const char *);
200 static bool c4x_rtx_costs (rtx, int, int, int *);
201 static int c4x_address_cost (rtx);
202 static void c4x_init_libfuncs (void);
203 \f
204 /* Initialize the GCC target structure.  */
205 #undef TARGET_ASM_BYTE_OP
206 #define TARGET_ASM_BYTE_OP "\t.word\t"
207 #undef TARGET_ASM_ALIGNED_HI_OP
208 #define TARGET_ASM_ALIGNED_HI_OP NULL
209 #undef TARGET_ASM_ALIGNED_SI_OP
210 #define TARGET_ASM_ALIGNED_SI_OP NULL
211 #undef TARGET_ASM_FILE_START
212 #define TARGET_ASM_FILE_START c4x_file_start
213 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
214 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
215 #undef TARGET_ASM_FILE_END
216 #define TARGET_ASM_FILE_END c4x_file_end
217
218 #undef TARGET_ATTRIBUTE_TABLE
219 #define TARGET_ATTRIBUTE_TABLE c4x_attribute_table
220
221 #undef TARGET_INSERT_ATTRIBUTES
222 #define TARGET_INSERT_ATTRIBUTES c4x_insert_attributes
223
224 #undef TARGET_INIT_BUILTINS
225 #define TARGET_INIT_BUILTINS c4x_init_builtins
226
227 #undef TARGET_EXPAND_BUILTIN
228 #define TARGET_EXPAND_BUILTIN c4x_expand_builtin
229
230 #undef TARGET_SCHED_ADJUST_COST
231 #define TARGET_SCHED_ADJUST_COST c4x_adjust_cost
232
233 #undef TARGET_ASM_GLOBALIZE_LABEL
234 #define TARGET_ASM_GLOBALIZE_LABEL c4x_globalize_label
235
236 #undef TARGET_RTX_COSTS
237 #define TARGET_RTX_COSTS c4x_rtx_costs
238 #undef TARGET_ADDRESS_COST
239 #define TARGET_ADDRESS_COST c4x_address_cost
240
241 #undef TARGET_MACHINE_DEPENDENT_REORG
242 #define TARGET_MACHINE_DEPENDENT_REORG c4x_reorg
243
244 #undef TARGET_INIT_LIBFUNCS
245 #define TARGET_INIT_LIBFUNCS c4x_init_libfuncs
246
247 struct gcc_target targetm = TARGET_INITIALIZER;
248 \f
249 /* Override command line options.
250    Called once after all options have been parsed.
251    Mostly we process the processor
252    type and sometimes adjust other TARGET_ options.  */
253
254 void
255 c4x_override_options (void)
256 {
257   if (c4x_rpts_cycles_string)
258     c4x_rpts_cycles = atoi (c4x_rpts_cycles_string);
259   else
260     c4x_rpts_cycles = 0;
261
262   if (TARGET_C30)
263     c4x_cpu_version = 30;
264   else if (TARGET_C31)
265     c4x_cpu_version = 31;
266   else if (TARGET_C32)
267     c4x_cpu_version = 32;
268   else if (TARGET_C33)
269     c4x_cpu_version = 33;
270   else if (TARGET_C40)
271     c4x_cpu_version = 40;
272   else if (TARGET_C44)
273     c4x_cpu_version = 44;
274   else
275     c4x_cpu_version = 40;              
276
277   /* -mcpu=xx overrides -m40 etc.  */
278   if (c4x_cpu_version_string)
279     {
280       const char *p = c4x_cpu_version_string;
281       
282       /* Also allow -mcpu=c30 etc.  */
283       if (*p == 'c' || *p == 'C')
284         p++;
285       c4x_cpu_version = atoi (p);
286     }
287
288   target_flags &= ~(C30_FLAG | C31_FLAG | C32_FLAG | C33_FLAG |
289                     C40_FLAG | C44_FLAG);
290
291   switch (c4x_cpu_version)
292     {
293     case 30: target_flags |= C30_FLAG; break;
294     case 31: target_flags |= C31_FLAG; break;
295     case 32: target_flags |= C32_FLAG; break;
296     case 33: target_flags |= C33_FLAG; break;
297     case 40: target_flags |= C40_FLAG; break;
298     case 44: target_flags |= C44_FLAG; break;
299     default:
300       warning ("unknown CPU version %d, using 40.\n", c4x_cpu_version);
301       c4x_cpu_version = 40;
302       target_flags |= C40_FLAG;
303     }
304
305   if (TARGET_C30 || TARGET_C31 || TARGET_C32 || TARGET_C33)
306     target_flags |= C3X_FLAG;
307   else
308     target_flags &= ~C3X_FLAG;
309
310   /* Convert foo / 8.0 into foo * 0.125, etc.  */
311   set_fast_math_flags (1);
312
313   /* We should phase out the following at some stage.
314      This provides compatibility with the old -mno-aliases option.  */
315   if (! TARGET_ALIASES && ! flag_argument_noalias)
316     flag_argument_noalias = 1;
317 }
318
319
320 /* This is called before c4x_override_options.  */
321
322 void
323 c4x_optimization_options (int level ATTRIBUTE_UNUSED,
324                           int size ATTRIBUTE_UNUSED)
325 {
326   /* Scheduling before register allocation can screw up global
327      register allocation, especially for functions that use MPY||ADD
328      instructions.  The benefit we gain we get by scheduling before
329      register allocation is probably marginal anyhow.  */
330   flag_schedule_insns = 0;
331 }
332
333
334 /* Write an ASCII string.  */
335
336 #define C4X_ASCII_LIMIT 40
337
338 void
339 c4x_output_ascii (FILE *stream, const char *ptr, int len)
340 {
341   char sbuf[C4X_ASCII_LIMIT + 1];
342   int s, l, special, first = 1, onlys;
343
344   if (len)
345       fprintf (stream, "\t.byte\t");
346
347   for (s = l = 0; len > 0; --len, ++ptr)
348     {
349       onlys = 0;
350
351       /* Escape " and \ with a \".  */
352       special = *ptr == '\"' || *ptr == '\\';
353
354       /* If printable - add to buff.  */
355       if ((! TARGET_TI || ! special) && *ptr >= 0x20 && *ptr < 0x7f)
356         {
357           if (special)
358             sbuf[s++] = '\\';
359           sbuf[s++] = *ptr;
360           if (s < C4X_ASCII_LIMIT - 1)
361             continue;
362           onlys = 1;
363         }
364       if (s)
365         {
366           if (first)
367             first = 0;
368           else
369             {
370               fputc (',', stream);
371               l++;
372             }
373
374           sbuf[s] = 0;
375           fprintf (stream, "\"%s\"", sbuf);
376           l += s + 2;
377           if (TARGET_TI && l >= 80 && len > 1)
378             {
379               fprintf (stream, "\n\t.byte\t");
380               first = 1;
381               l = 0;
382             }
383         
384           s = 0;
385         }
386       if (onlys)
387         continue;
388
389       if (first)
390         first = 0;
391       else
392         {
393           fputc (',', stream);
394           l++;
395         }
396
397       fprintf (stream, "%d", *ptr);
398       l += 3;
399       if (TARGET_TI && l >= 80 && len > 1)
400         {
401           fprintf (stream, "\n\t.byte\t");
402           first = 1;
403           l = 0;
404         }
405     }
406   if (s)
407     {
408       if (! first)
409         fputc (',', stream);
410
411       sbuf[s] = 0;
412       fprintf (stream, "\"%s\"", sbuf);
413       s = 0;
414     }
415   fputc ('\n', stream);
416 }
417
418
419 int
420 c4x_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
421 {
422   switch (mode)
423     {
424 #if Pmode != QImode
425     case Pmode:                 /* Pointer (24/32 bits).  */
426 #endif
427     case QImode:                /* Integer (32 bits).  */
428       return IS_INT_REGNO (regno);
429
430     case QFmode:                /* Float, Double (32 bits).  */
431     case HFmode:                /* Long Double (40 bits).  */
432       return IS_EXT_REGNO (regno);
433
434     case CCmode:                /* Condition Codes.  */
435     case CC_NOOVmode:           /* Condition Codes.  */
436       return IS_ST_REGNO (regno);
437
438     case HImode:                /* Long Long (64 bits).  */
439       /* We need two registers to store long longs.  Note that 
440          it is much easier to constrain the first register
441          to start on an even boundary.  */
442       return IS_INT_REGNO (regno)
443         && IS_INT_REGNO (regno + 1)
444         && (regno & 1) == 0;
445
446     default:
447       return 0;                 /* We don't support these modes.  */
448     }
449
450   return 0;
451 }
452
453 /* Return nonzero if REGNO1 can be renamed to REGNO2.  */
454 int
455 c4x_hard_regno_rename_ok (unsigned int regno1, unsigned int regno2)
456 {
457   /* We can not copy call saved registers from mode QI into QF or from
458      mode QF into QI.  */
459   if (IS_FLOAT_CALL_SAVED_REGNO (regno1) && IS_INT_CALL_SAVED_REGNO (regno2))
460     return 0;
461   if (IS_INT_CALL_SAVED_REGNO (regno1) && IS_FLOAT_CALL_SAVED_REGNO (regno2))
462     return 0;
463   /* We cannot copy from an extended (40 bit) register to a standard
464      (32 bit) register because we only set the condition codes for
465      extended registers.  */
466   if (IS_EXT_REGNO (regno1) && ! IS_EXT_REGNO (regno2))
467     return 0;
468   if (IS_EXT_REGNO (regno2) && ! IS_EXT_REGNO (regno1))
469     return 0;
470   return 1;
471 }
472
473 /* The TI C3x C compiler register argument runtime model uses 6 registers,
474    AR2, R2, R3, RC, RS, RE.
475
476    The first two floating point arguments (float, double, long double)
477    that are found scanning from left to right are assigned to R2 and R3.
478
479    The remaining integer (char, short, int, long) or pointer arguments
480    are assigned to the remaining registers in the order AR2, R2, R3,
481    RC, RS, RE when scanning left to right, except for the last named
482    argument prior to an ellipsis denoting variable number of
483    arguments.  We don't have to worry about the latter condition since
484    function.c treats the last named argument as anonymous (unnamed).
485
486    All arguments that cannot be passed in registers are pushed onto
487    the stack in reverse order (right to left).  GCC handles that for us.
488
489    c4x_init_cumulative_args() is called at the start, so we can parse
490    the args to see how many floating point arguments and how many
491    integer (or pointer) arguments there are.  c4x_function_arg() is
492    then called (sometimes repeatedly) for each argument (parsed left
493    to right) to obtain the register to pass the argument in, or zero
494    if the argument is to be passed on the stack.  Once the compiler is
495    happy, c4x_function_arg_advance() is called.
496
497    Don't use R0 to pass arguments in, we use 0 to indicate a stack
498    argument.  */
499
500 static const int c4x_int_reglist[3][6] =
501 {
502   {AR2_REGNO, R2_REGNO, R3_REGNO, RC_REGNO, RS_REGNO, RE_REGNO},
503   {AR2_REGNO, R3_REGNO, RC_REGNO, RS_REGNO, RE_REGNO, 0},
504   {AR2_REGNO, RC_REGNO, RS_REGNO, RE_REGNO, 0, 0}
505 };
506
507 static const int c4x_fp_reglist[2] = {R2_REGNO, R3_REGNO};
508
509
510 /* Initialize a variable CUM of type CUMULATIVE_ARGS for a call to a
511    function whose data type is FNTYPE.
512    For a library call, FNTYPE is  0.  */
513
514 void
515 c4x_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, rtx libname)
516 {
517   tree param, next_param;
518
519   cum->floats = cum->ints = 0;
520   cum->init = 0;
521   cum->var = 0;
522   cum->args = 0;
523
524   if (TARGET_DEBUG)
525     {
526       fprintf (stderr, "\nc4x_init_cumulative_args (");
527       if (fntype)
528         {
529           tree ret_type = TREE_TYPE (fntype);
530
531           fprintf (stderr, "fntype code = %s, ret code = %s",
532                    tree_code_name[(int) TREE_CODE (fntype)],
533                    tree_code_name[(int) TREE_CODE (ret_type)]);
534         }
535       else
536         fprintf (stderr, "no fntype");
537
538       if (libname)
539         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
540     }
541
542   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
543
544   for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
545        param; param = next_param)
546     {
547       tree type;
548
549       next_param = TREE_CHAIN (param);
550
551       type = TREE_VALUE (param);
552       if (type && type != void_type_node)
553         {
554           enum machine_mode mode;
555
556           /* If the last arg doesn't have void type then we have
557              variable arguments.  */
558           if (! next_param)
559             cum->var = 1;
560
561           if ((mode = TYPE_MODE (type)))
562             {
563               if (! MUST_PASS_IN_STACK (mode, type))
564                 {
565                   /* Look for float, double, or long double argument.  */
566                   if (mode == QFmode || mode == HFmode)
567                     cum->floats++;
568                   /* Look for integer, enumeral, boolean, char, or pointer
569                      argument.  */
570                   else if (mode == QImode || mode == Pmode)
571                     cum->ints++;
572                 }
573             }
574           cum->args++;
575         }
576     }
577
578   if (TARGET_DEBUG)
579     fprintf (stderr, "%s%s, args = %d)\n",
580              cum->prototype ? ", prototype" : "",
581              cum->var ? ", variable args" : "",
582              cum->args);
583 }
584
585
586 /* Update the data in CUM to advance over an argument
587    of mode MODE and data type TYPE.
588    (TYPE is null for libcalls where that information may not be available.)  */
589
590 void
591 c4x_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
592                           tree type, int named)
593 {
594   if (TARGET_DEBUG)
595     fprintf (stderr, "c4x_function_adv(mode=%s, named=%d)\n\n",
596              GET_MODE_NAME (mode), named);
597   if (! TARGET_MEMPARM 
598       && named
599       && type
600       && ! MUST_PASS_IN_STACK (mode, type))
601     {
602       /* Look for float, double, or long double argument.  */
603       if (mode == QFmode || mode == HFmode)
604         cum->floats++;
605       /* Look for integer, enumeral, boolean, char, or pointer argument.  */
606       else if (mode == QImode || mode == Pmode)
607         cum->ints++;
608     }
609   else if (! TARGET_MEMPARM && ! type)
610     {
611       /* Handle libcall arguments.  */
612       if (mode == QFmode || mode == HFmode)
613         cum->floats++;
614       else if (mode == QImode || mode == Pmode)
615         cum->ints++;
616     }
617   return;
618 }
619
620
621 /* Define where to put the arguments to a function.  Value is zero to
622    push the argument on the stack, or a hard register in which to
623    store the argument.
624
625    MODE is the argument's machine mode.
626    TYPE is the data type of the argument (as a tree).
627    This is null for libcalls where that information may
628    not be available.
629    CUM is a variable of type CUMULATIVE_ARGS which gives info about
630    the preceding args and about the function being called.
631    NAMED is nonzero if this argument is a named parameter
632    (otherwise it is an extra parameter matching an ellipsis).  */
633
634 struct rtx_def *
635 c4x_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
636                   tree type, int named)
637 {
638   int reg = 0;                  /* Default to passing argument on stack.  */
639
640   if (! cum->init)
641     {
642       /* We can handle at most 2 floats in R2, R3.  */
643       cum->maxfloats = (cum->floats > 2) ? 2 : cum->floats;
644
645       /* We can handle at most 6 integers minus number of floats passed 
646          in registers.  */
647       cum->maxints = (cum->ints > 6 - cum->maxfloats) ? 
648         6 - cum->maxfloats : cum->ints;
649
650       /* If there is no prototype, assume all the arguments are integers.  */
651       if (! cum->prototype)
652         cum->maxints = 6;
653
654       cum->ints = cum->floats = 0;
655       cum->init = 1;
656     }
657
658   /* This marks the last argument.  We don't need to pass this through
659      to the call insn.  */
660   if (type == void_type_node)
661     return 0;
662
663   if (! TARGET_MEMPARM 
664       && named 
665       && type
666       && ! MUST_PASS_IN_STACK (mode, type))
667     {
668       /* Look for float, double, or long double argument.  */
669       if (mode == QFmode || mode == HFmode)
670         {
671           if (cum->floats < cum->maxfloats)
672             reg = c4x_fp_reglist[cum->floats];
673         }
674       /* Look for integer, enumeral, boolean, char, or pointer argument.  */
675       else if (mode == QImode || mode == Pmode)
676         {
677           if (cum->ints < cum->maxints)
678             reg = c4x_int_reglist[cum->maxfloats][cum->ints];
679         }
680     }
681   else if (! TARGET_MEMPARM && ! type)
682     {
683       /* We could use a different argument calling model for libcalls,
684          since we're only calling functions in libgcc.  Thus we could
685          pass arguments for long longs in registers rather than on the
686          stack.  In the meantime, use the odd TI format.  We make the
687          assumption that we won't have more than two floating point
688          args, six integer args, and that all the arguments are of the
689          same mode.  */
690       if (mode == QFmode || mode == HFmode)
691         reg = c4x_fp_reglist[cum->floats];
692       else if (mode == QImode || mode == Pmode)
693         reg = c4x_int_reglist[0][cum->ints];
694     }
695
696   if (TARGET_DEBUG)
697     {
698       fprintf (stderr, "c4x_function_arg(mode=%s, named=%d",
699                GET_MODE_NAME (mode), named);
700       if (reg)
701         fprintf (stderr, ", reg=%s", reg_names[reg]);
702       else
703         fprintf (stderr, ", stack");
704       fprintf (stderr, ")\n");
705     }
706   if (reg)
707     return gen_rtx_REG (mode, reg);
708   else
709     return NULL_RTX;
710 }
711
712 /* C[34]x arguments grow in weird ways (downwards) that the standard
713    varargs stuff can't handle..  */
714 rtx
715 c4x_va_arg (tree valist, tree type)
716 {
717   tree t;
718
719   t = build (PREDECREMENT_EXPR, TREE_TYPE (valist), valist,
720              build_int_2 (int_size_in_bytes (type), 0));
721   TREE_SIDE_EFFECTS (t) = 1;
722
723   return expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
724 }
725
726
727 static int
728 c4x_isr_reg_used_p (unsigned int regno)
729 {
730   /* Don't save/restore FP or ST, we handle them separately.  */
731   if (regno == FRAME_POINTER_REGNUM
732       || IS_ST_REGNO (regno))
733     return 0;
734
735   /* We could be a little smarter abut saving/restoring DP.
736      We'll only save if for the big memory model or if
737      we're paranoid. ;-)  */
738   if (IS_DP_REGNO (regno))
739     return ! TARGET_SMALL || TARGET_PARANOID;
740
741   /* Only save/restore regs in leaf function that are used.  */
742   if (c4x_leaf_function)
743     return regs_ever_live[regno] && fixed_regs[regno] == 0;
744
745   /* Only save/restore regs that are used by the ISR and regs
746      that are likely to be used by functions the ISR calls
747      if they are not fixed.  */
748   return IS_EXT_REGNO (regno)
749     || ((regs_ever_live[regno] || call_used_regs[regno]) 
750         && fixed_regs[regno] == 0);
751 }
752
753
754 static int
755 c4x_leaf_function_p (void)
756 {
757   /* A leaf function makes no calls, so we only need
758      to save/restore the registers we actually use.
759      For the global variable leaf_function to be set, we need
760      to define LEAF_REGISTERS and all that it entails.
761      Let's check ourselves...   */
762
763   if (lookup_attribute ("leaf_pretend",
764                         TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
765     return 1;
766
767   /* Use the leaf_pretend attribute at your own risk.  This is a hack
768      to speed up ISRs that call a function infrequently where the
769      overhead of saving and restoring the additional registers is not
770      warranted.  You must save and restore the additional registers
771      required by the called function.  Caveat emptor.  Here's enough
772      rope...  */
773
774   if (leaf_function_p ())
775     return 1;
776
777   return 0;
778 }
779
780
781 static int
782 c4x_naked_function_p (void)
783 {
784   tree type;
785
786   type = TREE_TYPE (current_function_decl);
787   return lookup_attribute ("naked", TYPE_ATTRIBUTES (type)) != NULL;
788 }
789
790
791 int
792 c4x_interrupt_function_p (void)
793 {
794   const char *cfun_name;
795   if (lookup_attribute ("interrupt",
796                         TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
797     return 1;
798
799   /* Look for TI style c_intnn.  */
800   cfun_name = current_function_name ();
801   return cfun_name[0] == 'c'
802     && cfun_name[1] == '_'
803     && cfun_name[2] == 'i'
804     && cfun_name[3] == 'n' 
805     && cfun_name[4] == 't'
806     && ISDIGIT (cfun_name[5])
807     && ISDIGIT (cfun_name[6]);
808 }
809
810 void
811 c4x_expand_prologue (void)
812 {
813   unsigned int regno;
814   int size = get_frame_size ();
815   rtx insn;
816
817   /* In functions where ar3 is not used but frame pointers are still
818      specified, frame pointers are not adjusted (if >= -O2) and this
819      is used so it won't needlessly push the frame pointer.  */
820   int dont_push_ar3;
821
822   /* For __naked__ function don't build a prologue.  */
823   if (c4x_naked_function_p ())
824     {
825       return;
826     }
827   
828   /* For __interrupt__ function build specific prologue.  */
829   if (c4x_interrupt_function_p ())
830     {
831       c4x_leaf_function = c4x_leaf_function_p ();
832       
833       insn = emit_insn (gen_push_st ());
834       RTX_FRAME_RELATED_P (insn) = 1;
835       if (size)
836         {
837           insn = emit_insn (gen_pushqi ( gen_rtx_REG (QImode, AR3_REGNO)));
838           RTX_FRAME_RELATED_P (insn) = 1;
839           insn = emit_insn (gen_movqi (gen_rtx_REG (QImode, AR3_REGNO),
840                                        gen_rtx_REG (QImode, SP_REGNO)));
841           RTX_FRAME_RELATED_P (insn) = 1;
842           /* We require that an ISR uses fewer than 32768 words of
843              local variables, otherwise we have to go to lots of
844              effort to save a register, load it with the desired size,
845              adjust the stack pointer, and then restore the modified
846              register.  Frankly, I think it is a poor ISR that
847              requires more than 32767 words of local temporary
848              storage!  */
849           if (size > 32767)
850             error ("ISR %s requires %d words of local vars, max is 32767",
851                    current_function_name (), size);
852
853           insn = emit_insn (gen_addqi3 (gen_rtx_REG (QImode, SP_REGNO),
854                                         gen_rtx_REG (QImode, SP_REGNO),
855                                         GEN_INT (size)));
856           RTX_FRAME_RELATED_P (insn) = 1;
857         }
858       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
859         {
860           if (c4x_isr_reg_used_p (regno))
861             {
862               if (regno == DP_REGNO)
863                 {
864                   insn = emit_insn (gen_push_dp ());
865                   RTX_FRAME_RELATED_P (insn) = 1;
866                 }
867               else
868                 {
869                   insn = emit_insn (gen_pushqi (gen_rtx_REG (QImode, regno)));
870                   RTX_FRAME_RELATED_P (insn) = 1;
871                   if (IS_EXT_REGNO (regno))
872                     {
873                       insn = emit_insn (gen_pushqf
874                                         (gen_rtx_REG (QFmode, regno)));
875                       RTX_FRAME_RELATED_P (insn) = 1;
876                     }
877                 }
878             }
879         }
880       /* We need to clear the repeat mode flag if the ISR is
881          going to use a RPTB instruction or uses the RC, RS, or RE
882          registers.  */
883       if (regs_ever_live[RC_REGNO] 
884           || regs_ever_live[RS_REGNO] 
885           || regs_ever_live[RE_REGNO])
886         {
887           insn = emit_insn (gen_andn_st (GEN_INT(~0x100)));
888           RTX_FRAME_RELATED_P (insn) = 1;
889         }
890
891       /* Reload DP reg if we are paranoid about some turkey
892          violating small memory model rules.  */
893       if (TARGET_SMALL && TARGET_PARANOID)
894         {
895           insn = emit_insn (gen_set_ldp_prologue
896                             (gen_rtx_REG (QImode, DP_REGNO),
897                              gen_rtx_SYMBOL_REF (QImode, "data_sec")));
898           RTX_FRAME_RELATED_P (insn) = 1;
899         }
900     }
901   else
902     {
903       if (frame_pointer_needed)
904         {
905           if ((size != 0)
906               || (current_function_args_size != 0)
907               || (optimize < 2))
908             {
909               insn = emit_insn (gen_pushqi ( gen_rtx_REG (QImode, AR3_REGNO)));
910               RTX_FRAME_RELATED_P (insn) = 1;
911               insn = emit_insn (gen_movqi (gen_rtx_REG (QImode, AR3_REGNO),
912                                            gen_rtx_REG (QImode, SP_REGNO)));
913               RTX_FRAME_RELATED_P (insn) = 1;
914               dont_push_ar3 = 1;
915             }
916           else
917             {
918               /* Since ar3 is not used, we don't need to push it.  */
919               dont_push_ar3 = 1;
920             }
921         }
922       else
923         {
924           /* If we use ar3, we need to push it.   */
925           dont_push_ar3 = 0;
926           if ((size != 0) || (current_function_args_size != 0))
927             {
928               /* If we are omitting the frame pointer, we still have
929                  to make space for it so the offsets are correct
930                  unless we don't use anything on the stack at all.  */
931               size += 1;
932             }
933         }
934       
935       if (size > 32767)
936         {
937           /* Local vars are too big, it will take multiple operations
938              to increment SP.  */
939           if (TARGET_C3X)
940             {
941               insn = emit_insn (gen_movqi (gen_rtx_REG (QImode, R1_REGNO),
942                                            GEN_INT(size >> 16)));
943               RTX_FRAME_RELATED_P (insn) = 1;
944               insn = emit_insn (gen_lshrqi3 (gen_rtx_REG (QImode, R1_REGNO),
945                                              gen_rtx_REG (QImode, R1_REGNO),
946                                              GEN_INT(-16)));
947               RTX_FRAME_RELATED_P (insn) = 1;
948             }
949           else
950             {
951               insn = emit_insn (gen_movqi (gen_rtx_REG (QImode, R1_REGNO),
952                                            GEN_INT(size & ~0xffff)));
953               RTX_FRAME_RELATED_P (insn) = 1;
954             }
955           insn = emit_insn (gen_iorqi3 (gen_rtx_REG (QImode, R1_REGNO),
956                                         gen_rtx_REG (QImode, R1_REGNO),
957                                         GEN_INT(size & 0xffff)));
958           RTX_FRAME_RELATED_P (insn) = 1;
959           insn = emit_insn (gen_addqi3 (gen_rtx_REG (QImode, SP_REGNO),
960                                         gen_rtx_REG (QImode, SP_REGNO),
961                                         gen_rtx_REG (QImode, R1_REGNO)));
962           RTX_FRAME_RELATED_P (insn) = 1;
963         }
964       else if (size != 0)
965         {
966           /* Local vars take up less than 32767 words, so we can directly
967              add the number.  */
968           insn = emit_insn (gen_addqi3 (gen_rtx_REG (QImode, SP_REGNO),
969                                         gen_rtx_REG (QImode, SP_REGNO),
970                                         GEN_INT (size)));
971           RTX_FRAME_RELATED_P (insn) = 1;
972         }
973       
974       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
975         {
976           if (regs_ever_live[regno] && ! call_used_regs[regno])
977             {
978               if (IS_FLOAT_CALL_SAVED_REGNO (regno))
979                 {
980                   if (TARGET_PRESERVE_FLOAT)
981                     {
982                       insn = emit_insn (gen_pushqi
983                                         (gen_rtx_REG (QImode, regno)));
984                       RTX_FRAME_RELATED_P (insn) = 1;
985                     }
986                   insn = emit_insn (gen_pushqf (gen_rtx_REG (QFmode, regno)));
987                   RTX_FRAME_RELATED_P (insn) = 1;
988                 }
989               else if ((! dont_push_ar3) || (regno != AR3_REGNO))
990                 {
991                   insn = emit_insn (gen_pushqi ( gen_rtx_REG (QImode, regno)));
992                   RTX_FRAME_RELATED_P (insn) = 1;
993                 }
994             }
995         }
996     }
997 }
998
999
1000 void
1001 c4x_expand_epilogue(void)
1002 {
1003   int regno;
1004   int jump = 0;
1005   int dont_pop_ar3;
1006   rtx insn;
1007   int size = get_frame_size ();
1008   
1009   /* For __naked__ function build no epilogue.  */
1010   if (c4x_naked_function_p ())
1011     {
1012       insn = emit_jump_insn (gen_return_from_epilogue ());
1013       RTX_FRAME_RELATED_P (insn) = 1;
1014       return;
1015     }
1016
1017   /* For __interrupt__ function build specific epilogue.  */
1018   if (c4x_interrupt_function_p ())
1019     {
1020       for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; --regno)
1021         {
1022           if (! c4x_isr_reg_used_p (regno))
1023             continue;
1024           if (regno == DP_REGNO)
1025             {
1026               insn = emit_insn (gen_pop_dp ());
1027               RTX_FRAME_RELATED_P (insn) = 1;
1028             }
1029           else
1030             {
1031               /* We have to use unspec because the compiler will delete insns
1032                  that are not call-saved.  */
1033               if (IS_EXT_REGNO (regno))
1034                 {
1035                   insn = emit_insn (gen_popqf_unspec
1036                                     (gen_rtx_REG (QFmode, regno)));
1037                   RTX_FRAME_RELATED_P (insn) = 1;
1038                 }
1039               insn = emit_insn (gen_popqi_unspec (gen_rtx_REG (QImode, regno)));
1040               RTX_FRAME_RELATED_P (insn) = 1;
1041             }
1042         }
1043       if (size)
1044         {
1045           insn = emit_insn (gen_subqi3 (gen_rtx_REG (QImode, SP_REGNO),
1046                                         gen_rtx_REG (QImode, SP_REGNO),
1047                                         GEN_INT(size)));
1048           RTX_FRAME_RELATED_P (insn) = 1;
1049           insn = emit_insn (gen_popqi
1050                             (gen_rtx_REG (QImode, AR3_REGNO)));
1051           RTX_FRAME_RELATED_P (insn) = 1;
1052         }
1053       insn = emit_insn (gen_pop_st ());
1054       RTX_FRAME_RELATED_P (insn) = 1;
1055       insn = emit_jump_insn (gen_return_from_interrupt_epilogue ());
1056       RTX_FRAME_RELATED_P (insn) = 1;
1057     }
1058   else
1059     {
1060       if (frame_pointer_needed)
1061         {
1062           if ((size != 0) 
1063               || (current_function_args_size != 0) 
1064               || (optimize < 2))
1065             {
1066               insn = emit_insn
1067                 (gen_movqi (gen_rtx_REG (QImode, R2_REGNO),
1068                             gen_rtx_MEM (QImode,
1069                                          gen_rtx_PLUS 
1070                                          (QImode, gen_rtx_REG (QImode,
1071                                                                AR3_REGNO),
1072                                           GEN_INT(-1)))));
1073               RTX_FRAME_RELATED_P (insn) = 1;
1074               
1075               /* We already have the return value and the fp,
1076                  so we need to add those to the stack.  */
1077               size += 2;
1078               jump = 1;
1079               dont_pop_ar3 = 1;
1080             }
1081           else
1082             {
1083               /* Since ar3 is not used for anything, we don't need to
1084                  pop it.  */
1085               dont_pop_ar3 = 1;
1086             }
1087         }
1088       else
1089         {
1090           dont_pop_ar3 = 0;     /* If we use ar3, we need to pop it.  */
1091           if (size || current_function_args_size)
1092             {
1093               /* If we are omitting the frame pointer, we still have
1094                  to make space for it so the offsets are correct
1095                  unless we don't use anything on the stack at all.  */
1096               size += 1;
1097             }
1098         }
1099       
1100       /* Now restore the saved registers, putting in the delayed branch
1101          where required.  */
1102       for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
1103         {
1104           if (regs_ever_live[regno] && ! call_used_regs[regno])
1105             {
1106               if (regno == AR3_REGNO && dont_pop_ar3)
1107                 continue;
1108               
1109               if (IS_FLOAT_CALL_SAVED_REGNO (regno))
1110                 {
1111                   insn = emit_insn (gen_popqf_unspec
1112                                     (gen_rtx_REG (QFmode, regno)));
1113                   RTX_FRAME_RELATED_P (insn) = 1;
1114                   if (TARGET_PRESERVE_FLOAT)
1115                     {
1116                       insn = emit_insn (gen_popqi_unspec
1117                                         (gen_rtx_REG (QImode, regno)));
1118                       RTX_FRAME_RELATED_P (insn) = 1;
1119                     }
1120                 }
1121               else
1122                 {
1123                   insn = emit_insn (gen_popqi (gen_rtx_REG (QImode, regno)));
1124                   RTX_FRAME_RELATED_P (insn) = 1;
1125                 }
1126             }
1127         }
1128       
1129       if (frame_pointer_needed)
1130         {
1131           if ((size != 0)
1132               || (current_function_args_size != 0)
1133               || (optimize < 2))
1134             {
1135               /* Restore the old FP.  */
1136               insn = emit_insn 
1137                 (gen_movqi 
1138                  (gen_rtx_REG (QImode, AR3_REGNO),
1139                   gen_rtx_MEM (QImode, gen_rtx_REG (QImode, AR3_REGNO))));
1140               
1141               RTX_FRAME_RELATED_P (insn) = 1;
1142             }
1143         }
1144       
1145       if (size > 32767)
1146         {
1147           /* Local vars are too big, it will take multiple operations
1148              to decrement SP.  */
1149           if (TARGET_C3X)
1150             {
1151               insn = emit_insn (gen_movqi (gen_rtx_REG (QImode, R3_REGNO),
1152                                            GEN_INT(size >> 16)));
1153               RTX_FRAME_RELATED_P (insn) = 1;
1154               insn = emit_insn (gen_lshrqi3 (gen_rtx_REG (QImode, R3_REGNO),
1155                                              gen_rtx_REG (QImode, R3_REGNO),
1156                                              GEN_INT(-16)));
1157               RTX_FRAME_RELATED_P (insn) = 1;
1158             }
1159           else
1160             {
1161               insn = emit_insn (gen_movqi (gen_rtx_REG (QImode, R3_REGNO),
1162                                            GEN_INT(size & ~0xffff)));
1163               RTX_FRAME_RELATED_P (insn) = 1;
1164             }
1165           insn = emit_insn (gen_iorqi3 (gen_rtx_REG (QImode, R3_REGNO),
1166                                         gen_rtx_REG (QImode, R3_REGNO),
1167                                         GEN_INT(size & 0xffff)));
1168           RTX_FRAME_RELATED_P (insn) = 1;
1169           insn = emit_insn (gen_subqi3 (gen_rtx_REG (QImode, SP_REGNO),
1170                                         gen_rtx_REG (QImode, SP_REGNO),
1171                                         gen_rtx_REG (QImode, R3_REGNO)));
1172           RTX_FRAME_RELATED_P (insn) = 1;
1173         }
1174       else if (size != 0)
1175         {
1176           /* Local vars take up less than 32768 words, so we can directly
1177              subtract the number.  */
1178           insn = emit_insn (gen_subqi3 (gen_rtx_REG (QImode, SP_REGNO),
1179                                         gen_rtx_REG (QImode, SP_REGNO),
1180                                         GEN_INT(size)));
1181           RTX_FRAME_RELATED_P (insn) = 1;
1182         }
1183       
1184       if (jump)
1185         {
1186           insn = emit_jump_insn (gen_return_indirect_internal
1187                                  (gen_rtx_REG (QImode, R2_REGNO)));
1188           RTX_FRAME_RELATED_P (insn) = 1;
1189         }
1190       else
1191         {
1192           insn = emit_jump_insn (gen_return_from_epilogue ());
1193           RTX_FRAME_RELATED_P (insn) = 1;
1194         }
1195     }
1196 }
1197
1198
1199 int
1200 c4x_null_epilogue_p (void)
1201 {
1202   int regno;
1203
1204   if (reload_completed
1205       && ! c4x_naked_function_p ()
1206       && ! c4x_interrupt_function_p ()
1207       && ! current_function_calls_alloca
1208       && ! current_function_args_size
1209       && ! (optimize < 2)
1210       && ! get_frame_size ())
1211     {
1212       for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
1213         if (regs_ever_live[regno] && ! call_used_regs[regno]
1214             && (regno != AR3_REGNO))
1215           return 1;
1216       return 0;
1217     }
1218   return 1;
1219 }
1220
1221
1222 int
1223 c4x_emit_move_sequence (rtx *operands, enum machine_mode mode)
1224 {
1225   rtx op0 = operands[0];
1226   rtx op1 = operands[1];
1227
1228   if (! reload_in_progress
1229       && ! REG_P (op0) 
1230       && ! REG_P (op1)
1231       && ! (stik_const_operand (op1, mode) && ! push_operand (op0, mode)))
1232     op1 = force_reg (mode, op1);
1233
1234   if (GET_CODE (op1) == LO_SUM
1235       && GET_MODE (op1) == Pmode
1236       && dp_reg_operand (XEXP (op1, 0), mode))
1237     {
1238       /* expand_increment will sometimes create a LO_SUM immediate
1239          address.  */
1240       op1 = XEXP (op1, 1);
1241     }
1242   else if (symbolic_address_operand (op1, mode))
1243     {
1244       if (TARGET_LOAD_ADDRESS)
1245         {
1246           /* Alias analysis seems to do a better job if we force
1247              constant addresses to memory after reload.  */
1248           emit_insn (gen_load_immed_address (op0, op1));
1249           return 1;
1250         }
1251       else
1252         {
1253           /* Stick symbol or label address into the constant pool.  */
1254           op1 = force_const_mem (Pmode, op1);
1255         }
1256     }
1257   else if (mode == HFmode && CONSTANT_P (op1) && ! LEGITIMATE_CONSTANT_P (op1))
1258     {
1259       /* We could be a lot smarter about loading some of these
1260          constants...  */
1261       op1 = force_const_mem (mode, op1);
1262     }
1263
1264   /* Convert (MEM (SYMREF)) to a (MEM (LO_SUM (REG) (SYMREF)))
1265      and emit associated (HIGH (SYMREF)) if large memory model.  
1266      c4x_legitimize_address could be used to do this,
1267      perhaps by calling validize_address.  */
1268   if (TARGET_EXPOSE_LDP
1269       && ! (reload_in_progress || reload_completed)
1270       && GET_CODE (op1) == MEM
1271       && symbolic_address_operand (XEXP (op1, 0), Pmode))
1272     {
1273       rtx dp_reg = gen_rtx_REG (Pmode, DP_REGNO);
1274       if (! TARGET_SMALL)
1275         emit_insn (gen_set_ldp (dp_reg, XEXP (op1, 0)));
1276       op1 = change_address (op1, mode,
1277                             gen_rtx_LO_SUM (Pmode, dp_reg, XEXP (op1, 0)));
1278     }
1279
1280   if (TARGET_EXPOSE_LDP
1281       && ! (reload_in_progress || reload_completed)
1282       && GET_CODE (op0) == MEM 
1283       && symbolic_address_operand (XEXP (op0, 0), Pmode))
1284     {
1285       rtx dp_reg = gen_rtx_REG (Pmode, DP_REGNO);
1286       if (! TARGET_SMALL)
1287         emit_insn (gen_set_ldp (dp_reg, XEXP (op0, 0)));
1288       op0 = change_address (op0, mode,
1289                             gen_rtx_LO_SUM (Pmode, dp_reg, XEXP (op0, 0)));
1290     }
1291
1292   if (GET_CODE (op0) == SUBREG
1293       && mixed_subreg_operand (op0, mode))
1294     {
1295       /* We should only generate these mixed mode patterns
1296          during RTL generation.  If we need do it later on
1297          then we'll have to emit patterns that won't clobber CC.  */
1298       if (reload_in_progress || reload_completed)
1299         abort ();
1300       if (GET_MODE (SUBREG_REG (op0)) == QImode)
1301         op0 = SUBREG_REG (op0);
1302       else if (GET_MODE (SUBREG_REG (op0)) == HImode)
1303         {
1304           op0 = copy_rtx (op0);
1305           PUT_MODE (op0, QImode);
1306         }
1307       else
1308         abort ();
1309
1310       if (mode == QFmode)
1311         emit_insn (gen_storeqf_int_clobber (op0, op1));
1312       else
1313         abort ();
1314       return 1;
1315     }
1316
1317   if (GET_CODE (op1) == SUBREG
1318       && mixed_subreg_operand (op1, mode))
1319     {
1320       /* We should only generate these mixed mode patterns
1321          during RTL generation.  If we need do it later on
1322          then we'll have to emit patterns that won't clobber CC.  */
1323       if (reload_in_progress || reload_completed)
1324         abort ();
1325       if (GET_MODE (SUBREG_REG (op1)) == QImode)
1326         op1 = SUBREG_REG (op1);
1327       else if (GET_MODE (SUBREG_REG (op1)) == HImode)
1328         {
1329           op1 = copy_rtx (op1);
1330           PUT_MODE (op1, QImode);
1331         }
1332       else
1333         abort ();
1334
1335       if (mode == QFmode)
1336         emit_insn (gen_loadqf_int_clobber (op0, op1));
1337       else
1338         abort ();
1339       return 1;
1340     }
1341
1342   if (mode == QImode
1343       && reg_operand (op0, mode)
1344       && const_int_operand (op1, mode)
1345       && ! IS_INT16_CONST (INTVAL (op1))
1346       && ! IS_HIGH_CONST (INTVAL (op1)))
1347     {
1348       emit_insn (gen_loadqi_big_constant (op0, op1));
1349       return 1;
1350     }
1351
1352   if (mode == HImode
1353       && reg_operand (op0, mode)
1354       && const_int_operand (op1, mode))
1355     {
1356       emit_insn (gen_loadhi_big_constant (op0, op1));
1357       return 1;
1358     }
1359
1360   /* Adjust operands in case we have modified them.  */
1361   operands[0] = op0;
1362   operands[1] = op1;
1363
1364   /* Emit normal pattern.  */
1365   return 0;
1366 }
1367
1368
1369 void
1370 c4x_emit_libcall (rtx libcall, enum rtx_code code,
1371                   enum machine_mode dmode, enum machine_mode smode,
1372                   int noperands, rtx *operands)
1373 {
1374   rtx ret;
1375   rtx insns;
1376   rtx equiv;
1377
1378   start_sequence ();
1379   switch (noperands)
1380     {
1381     case 2:
1382       ret = emit_library_call_value (libcall, NULL_RTX, 1, dmode, 1,
1383                                      operands[1], smode);
1384       equiv = gen_rtx (code, dmode, operands[1]);
1385       break;
1386
1387     case 3:
1388       ret = emit_library_call_value (libcall, NULL_RTX, 1, dmode, 2,
1389                                      operands[1], smode, operands[2], smode);
1390       equiv = gen_rtx (code, dmode, operands[1], operands[2]);
1391       break;
1392
1393     default:
1394       abort ();
1395     }
1396
1397   insns = get_insns ();
1398   end_sequence ();
1399   emit_libcall_block (insns, operands[0], ret, equiv);
1400 }
1401
1402
1403 void
1404 c4x_emit_libcall3 (rtx libcall, enum rtx_code code,
1405                    enum machine_mode mode, rtx *operands)
1406 {
1407   c4x_emit_libcall (libcall, code, mode, mode, 3, operands);
1408 }
1409
1410
1411 void
1412 c4x_emit_libcall_mulhi (rtx libcall, enum rtx_code code,
1413                         enum machine_mode mode, rtx *operands)
1414 {
1415   rtx ret;
1416   rtx insns;
1417   rtx equiv;
1418
1419   start_sequence ();
1420   ret = emit_library_call_value (libcall, NULL_RTX, 1, mode, 2,
1421                                  operands[1], mode, operands[2], mode);
1422   equiv = gen_rtx_TRUNCATE (mode,
1423                    gen_rtx_LSHIFTRT (HImode,
1424                             gen_rtx_MULT (HImode,
1425                                      gen_rtx (code, HImode, operands[1]),
1426                                      gen_rtx (code, HImode, operands[2])),
1427                                      GEN_INT (32)));
1428   insns = get_insns ();
1429   end_sequence ();
1430   emit_libcall_block (insns, operands[0], ret, equiv);
1431 }
1432
1433
1434 int
1435 c4x_legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
1436 {
1437   rtx base = NULL_RTX;          /* Base register (AR0-AR7).  */
1438   rtx indx = NULL_RTX;          /* Index register (IR0,IR1).  */
1439   rtx disp = NULL_RTX;          /* Displacement.  */
1440   enum rtx_code code;
1441
1442   code = GET_CODE (addr);
1443   switch (code)
1444     {
1445       /* Register indirect with auto increment/decrement.  We don't
1446          allow SP here---push_operand should recognize an operand
1447          being pushed on the stack.  */
1448
1449     case PRE_DEC:
1450     case PRE_INC:
1451     case POST_DEC:
1452       if (mode != QImode && mode != QFmode)
1453         return 0;
1454
1455     case POST_INC:
1456       base = XEXP (addr, 0);
1457       if (! REG_P (base))
1458         return 0;
1459       break;
1460
1461     case PRE_MODIFY:
1462     case POST_MODIFY:
1463       {
1464         rtx op0 = XEXP (addr, 0);
1465         rtx op1 = XEXP (addr, 1);
1466
1467         if (mode != QImode && mode != QFmode)
1468           return 0;
1469
1470         if (! REG_P (op0) 
1471             || (GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS))
1472           return 0;
1473         base = XEXP (op1, 0);
1474         if (! REG_P (base))
1475             return 0;
1476         if (REGNO (base) != REGNO (op0))
1477           return 0;
1478         if (REG_P (XEXP (op1, 1)))
1479           indx = XEXP (op1, 1);
1480         else
1481           disp = XEXP (op1, 1);
1482       }
1483       break;
1484         
1485       /* Register indirect.  */
1486     case REG:
1487       base = addr;
1488       break;
1489
1490       /* Register indirect with displacement or index.  */
1491     case PLUS:
1492       {
1493         rtx op0 = XEXP (addr, 0);
1494         rtx op1 = XEXP (addr, 1);
1495         enum rtx_code code0 = GET_CODE (op0);
1496
1497         switch (code0)
1498           {
1499           case REG:
1500             if (REG_P (op1))
1501               {
1502                 base = op0;     /* Base + index.  */
1503                 indx = op1;
1504                 if (IS_INDEX_REG (base) || IS_ADDR_REG (indx))
1505                   {
1506                     base = op1;
1507                     indx = op0;
1508                   }
1509               }
1510             else
1511               {
1512                 base = op0;     /* Base + displacement.  */
1513                 disp = op1;
1514               }
1515             break;
1516
1517           default:
1518             return 0;
1519           }
1520       }
1521       break;
1522
1523       /* Direct addressing with DP register.  */
1524     case LO_SUM:
1525       {
1526         rtx op0 = XEXP (addr, 0);
1527         rtx op1 = XEXP (addr, 1);
1528
1529         /* HImode and HFmode direct memory references aren't truly
1530            offsettable (consider case at end of data page).  We
1531            probably get better code by loading a pointer and using an
1532            indirect memory reference.  */
1533         if (mode == HImode || mode == HFmode)
1534           return 0;
1535
1536         if (!REG_P (op0) || REGNO (op0) != DP_REGNO)
1537           return 0;
1538
1539         if ((GET_CODE (op1) == SYMBOL_REF || GET_CODE (op1) == LABEL_REF))
1540           return 1;
1541
1542         if (GET_CODE (op1) == CONST)
1543           return 1;
1544         return 0;
1545       }
1546       break;
1547
1548       /* Direct addressing with some work for the assembler...  */
1549     case CONST:
1550       /* Direct addressing.  */
1551     case LABEL_REF:
1552     case SYMBOL_REF:
1553       if (! TARGET_EXPOSE_LDP && ! strict && mode != HFmode && mode != HImode)
1554         return 1;
1555       /* These need to be converted to a LO_SUM (...). 
1556          LEGITIMIZE_RELOAD_ADDRESS will do this during reload.  */
1557       return 0;
1558
1559       /* Do not allow direct memory access to absolute addresses.
1560          This is more pain than it's worth, especially for the
1561          small memory model where we can't guarantee that
1562          this address is within the data page---we don't want
1563          to modify the DP register in the small memory model,
1564          even temporarily, since an interrupt can sneak in....  */
1565     case CONST_INT:
1566       return 0;
1567
1568       /* Indirect indirect addressing.  */
1569     case MEM:
1570       return 0;
1571
1572     case CONST_DOUBLE:
1573       fatal_insn ("using CONST_DOUBLE for address", addr);
1574
1575     default:
1576       return 0;
1577     }
1578
1579   /* Validate the base register.  */
1580   if (base)
1581     {
1582       /* Check that the address is offsettable for HImode and HFmode.  */
1583       if (indx && (mode == HImode || mode == HFmode))
1584         return 0;
1585
1586       /* Handle DP based stuff.  */
1587       if (REGNO (base) == DP_REGNO)
1588         return 1;
1589       if (strict && ! REGNO_OK_FOR_BASE_P (REGNO (base)))
1590         return 0;
1591       else if (! strict && ! IS_ADDR_OR_PSEUDO_REG (base))
1592         return 0;
1593     }
1594
1595   /* Now validate the index register.  */
1596   if (indx)
1597     {
1598       if (GET_CODE (indx) != REG)
1599         return 0;
1600       if (strict && ! REGNO_OK_FOR_INDEX_P (REGNO (indx)))
1601         return 0;
1602       else if (! strict && ! IS_INDEX_OR_PSEUDO_REG (indx))
1603         return 0;
1604     }
1605
1606   /* Validate displacement.  */
1607   if (disp)
1608     {
1609       if (GET_CODE (disp) != CONST_INT)
1610         return 0;
1611       if (mode == HImode || mode == HFmode)
1612         {
1613           /* The offset displacement must be legitimate.  */
1614           if (! IS_DISP8_OFF_CONST (INTVAL (disp)))
1615             return 0;
1616         }
1617       else
1618         {
1619           if (! IS_DISP8_CONST (INTVAL (disp)))
1620             return 0;
1621         }
1622       /* Can't add an index with a disp.  */
1623       if (indx)
1624         return 0;               
1625     }
1626   return 1;
1627 }
1628
1629
1630 rtx
1631 c4x_legitimize_address (rtx orig ATTRIBUTE_UNUSED,
1632                         enum machine_mode mode ATTRIBUTE_UNUSED)
1633 {
1634   if (GET_CODE (orig) == SYMBOL_REF
1635       || GET_CODE (orig) == LABEL_REF)
1636     {
1637       if (mode == HImode || mode == HFmode)
1638         {
1639           /* We need to force the address into
1640              a register so that it is offsettable.  */
1641           rtx addr_reg = gen_reg_rtx (Pmode);
1642           emit_move_insn (addr_reg, orig);
1643           return addr_reg;
1644         }
1645       else
1646         {
1647           rtx dp_reg = gen_rtx_REG (Pmode, DP_REGNO);
1648           
1649           if (! TARGET_SMALL)
1650             emit_insn (gen_set_ldp (dp_reg, orig));
1651           
1652           return gen_rtx_LO_SUM (Pmode, dp_reg, orig);
1653         }
1654     }
1655
1656   return NULL_RTX;
1657 }
1658
1659
1660 /* Provide the costs of an addressing mode that contains ADDR.
1661    If ADDR is not a valid address, its cost is irrelevant.  
1662    This is used in cse and loop optimization to determine
1663    if it is worthwhile storing a common address into a register. 
1664    Unfortunately, the C4x address cost depends on other operands.  */
1665
1666 static int 
1667 c4x_address_cost (rtx addr)
1668 {
1669   switch (GET_CODE (addr))
1670     {
1671     case REG:
1672       return 1;
1673
1674     case POST_INC:
1675     case POST_DEC:
1676     case PRE_INC:
1677     case PRE_DEC:
1678       return 1;
1679       
1680       /* These shouldn't be directly generated.  */
1681     case SYMBOL_REF:
1682     case LABEL_REF:
1683     case CONST:
1684       return 10;
1685
1686     case LO_SUM:
1687       {
1688         rtx op1 = XEXP (addr, 1);
1689
1690         if (GET_CODE (op1) == LABEL_REF || GET_CODE (op1) == SYMBOL_REF)
1691           return TARGET_SMALL ? 3 : 4;
1692         
1693         if (GET_CODE (op1) == CONST)
1694           {
1695             rtx offset = const0_rtx;
1696             
1697             op1 = eliminate_constant_term (op1, &offset);
1698             
1699             /* ??? These costs need rethinking...  */
1700             if (GET_CODE (op1) == LABEL_REF)
1701               return 3;
1702             
1703             if (GET_CODE (op1) != SYMBOL_REF)
1704               return 4;
1705             
1706             if (INTVAL (offset) == 0)
1707               return 3;
1708
1709             return 4;
1710           }
1711         fatal_insn ("c4x_address_cost: Invalid addressing mode", addr);
1712       }
1713       break;
1714       
1715     case PLUS:
1716       {
1717         register rtx op0 = XEXP (addr, 0);
1718         register rtx op1 = XEXP (addr, 1);
1719         
1720         if (GET_CODE (op0) != REG)
1721           break;
1722         
1723         switch (GET_CODE (op1))
1724           {
1725           default:
1726             break;
1727
1728           case REG:
1729             /* This cost for REG+REG must be greater than the cost
1730                for REG if we want autoincrement addressing modes.  */
1731             return 2;
1732
1733           case CONST_INT:
1734             /* The following tries to improve GIV combination
1735                in strength reduce but appears not to help.  */
1736             if (TARGET_DEVEL && IS_UINT5_CONST (INTVAL (op1)))
1737               return 1;
1738
1739             if (IS_DISP1_CONST (INTVAL (op1)))
1740               return 1;
1741
1742             if (! TARGET_C3X && IS_UINT5_CONST (INTVAL (op1)))
1743               return 2;
1744
1745             return 3;
1746           }
1747       }
1748     default:
1749       break;
1750     }
1751   
1752   return 4;
1753 }
1754
1755
1756 rtx
1757 c4x_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
1758 {
1759   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
1760   rtx cc_reg;
1761
1762   if (mode == CC_NOOVmode
1763       && (code == LE || code == GE || code == LT || code == GT))
1764     return NULL_RTX;
1765
1766   cc_reg = gen_rtx_REG (mode, ST_REGNO);
1767   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
1768                           gen_rtx_COMPARE (mode, x, y)));
1769   return cc_reg;
1770 }
1771
1772 char *
1773 c4x_output_cbranch (const char *form, rtx seq)
1774 {
1775   int delayed = 0;
1776   int annultrue = 0;
1777   int annulfalse = 0;
1778   rtx delay;
1779   char *cp;
1780   static char str[100];
1781   
1782   if (final_sequence)
1783     {
1784       delay = XVECEXP (final_sequence, 0, 1);
1785       delayed = ! INSN_ANNULLED_BRANCH_P (seq);
1786       annultrue = INSN_ANNULLED_BRANCH_P (seq) && ! INSN_FROM_TARGET_P (delay);
1787       annulfalse = INSN_ANNULLED_BRANCH_P (seq) && INSN_FROM_TARGET_P (delay);
1788     }
1789   strcpy (str, form);
1790   cp = &str [strlen (str)];
1791   if (delayed)
1792     {
1793       *cp++ = '%';
1794       *cp++ = '#';
1795     }
1796   if (annultrue)
1797     {
1798       *cp++ = 'a';
1799       *cp++ = 't';
1800     }
1801   if (annulfalse)
1802     {
1803       *cp++ = 'a'; 
1804       *cp++ = 'f';
1805     }
1806   *cp++ = '\t';
1807   *cp++ = '%'; 
1808   *cp++ = 'l';
1809   *cp++ = '1';
1810   *cp = 0;
1811   return str;
1812 }
1813
1814 void
1815 c4x_print_operand (FILE *file, rtx op, int letter)
1816 {
1817   rtx op1;
1818   enum rtx_code code;
1819
1820   switch (letter)
1821     {
1822     case '#':                   /* Delayed.  */
1823       if (final_sequence)
1824         fprintf (file, "d");
1825       return;
1826     }
1827
1828   code = GET_CODE (op);
1829   switch (letter)
1830     {
1831     case 'A':                   /* Direct address.  */
1832       if (code == CONST_INT || code == SYMBOL_REF || code == CONST)
1833         fprintf (file, "@");
1834       break;
1835
1836     case 'H':                   /* Sethi.  */
1837       output_addr_const (file, op);
1838       return;
1839
1840     case 'I':                   /* Reversed condition.  */
1841       code = reverse_condition (code);
1842       break;
1843
1844     case 'L':                   /* Log 2 of constant.  */
1845       if (code != CONST_INT)
1846         fatal_insn ("c4x_print_operand: %%L inconsistency", op);
1847       fprintf (file, "%d", exact_log2 (INTVAL (op)));
1848       return;
1849
1850     case 'N':                   /* Ones complement of small constant.  */
1851       if (code != CONST_INT)
1852         fatal_insn ("c4x_print_operand: %%N inconsistency", op);
1853       fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~INTVAL (op));
1854       return;
1855
1856     case 'K':                   /* Generate ldp(k) if direct address.  */
1857       if (! TARGET_SMALL
1858           && code == MEM
1859           && GET_CODE (XEXP (op, 0)) == LO_SUM
1860           && GET_CODE (XEXP (XEXP (op, 0), 0)) == REG
1861           && REGNO (XEXP (XEXP (op, 0), 0)) == DP_REGNO)
1862         {
1863           op1 = XEXP (XEXP (op, 0), 1);
1864           if (GET_CODE(op1) == CONST_INT || GET_CODE(op1) == SYMBOL_REF)
1865             {
1866               fprintf (file, "\t%s\t@", TARGET_C3X ? "ldp" : "ldpk");
1867               output_address (XEXP (adjust_address (op, VOIDmode, 1), 0));
1868               fprintf (file, "\n");
1869             }
1870         }
1871       return;
1872
1873     case 'M':                   /* Generate ldp(k) if direct address.  */
1874       if (! TARGET_SMALL        /* Only used in asm statements.  */
1875           && code == MEM
1876           && (GET_CODE (XEXP (op, 0)) == CONST
1877               || GET_CODE (XEXP (op, 0)) == SYMBOL_REF))
1878         {
1879           fprintf (file, "%s\t@", TARGET_C3X ? "ldp" : "ldpk");
1880           output_address (XEXP (op, 0));
1881           fprintf (file, "\n\t");
1882         }
1883       return;
1884
1885     case 'O':                   /* Offset address.  */
1886       if (code == MEM && c4x_autoinc_operand (op, Pmode))
1887         break;
1888       else if (code == MEM)
1889         output_address (XEXP (adjust_address (op, VOIDmode, 1), 0));
1890       else if (code == REG)
1891         fprintf (file, "%s", reg_names[REGNO (op) + 1]);
1892       else
1893         fatal_insn ("c4x_print_operand: %%O inconsistency", op);
1894       return;
1895
1896     case 'C':                   /* Call.  */
1897       break;
1898
1899     case 'U':                   /* Call/callu.  */
1900       if (code != SYMBOL_REF)
1901         fprintf (file, "u");
1902       return;
1903
1904     default:
1905       break;
1906     }
1907   
1908   switch (code)
1909     {
1910     case REG:
1911       if (GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
1912           && ! TARGET_TI)
1913         fprintf (file, "%s", float_reg_names[REGNO (op)]);
1914       else
1915         fprintf (file, "%s", reg_names[REGNO (op)]);
1916       break;
1917       
1918     case MEM:
1919       output_address (XEXP (op, 0));
1920       break;
1921       
1922     case CONST_DOUBLE:
1923       {
1924         char str[64];
1925         
1926         real_to_decimal (str, CONST_DOUBLE_REAL_VALUE (op),
1927                          sizeof (str), 0, 1);
1928         fprintf (file, "%s", str);
1929       }
1930       break;
1931       
1932     case CONST_INT:
1933       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
1934       break;
1935       
1936     case NE:
1937       fprintf (file, "ne");
1938       break;
1939       
1940     case EQ:
1941       fprintf (file, "eq");
1942       break;
1943       
1944     case GE:
1945       fprintf (file, "ge");
1946       break;
1947
1948     case GT:
1949       fprintf (file, "gt");
1950       break;
1951
1952     case LE:
1953       fprintf (file, "le");
1954       break;
1955
1956     case LT:
1957       fprintf (file, "lt");
1958       break;
1959
1960     case GEU:
1961       fprintf (file, "hs");
1962       break;
1963
1964     case GTU:
1965       fprintf (file, "hi");
1966       break;
1967
1968     case LEU:
1969       fprintf (file, "ls");
1970       break;
1971
1972     case LTU:
1973       fprintf (file, "lo");
1974       break;
1975
1976     case SYMBOL_REF:
1977       output_addr_const (file, op);
1978       break;
1979
1980     case CONST:
1981       output_addr_const (file, XEXP (op, 0));
1982       break;
1983
1984     case CODE_LABEL:
1985       break;
1986
1987     default:
1988       fatal_insn ("c4x_print_operand: Bad operand case", op);
1989       break;
1990     }
1991 }
1992
1993
1994 void
1995 c4x_print_operand_address (FILE *file, rtx addr)
1996 {
1997   switch (GET_CODE (addr))
1998     {
1999     case REG:
2000       fprintf (file, "*%s", reg_names[REGNO (addr)]);
2001       break;
2002
2003     case PRE_DEC:
2004       fprintf (file, "*--%s", reg_names[REGNO (XEXP (addr, 0))]);
2005       break;
2006
2007     case POST_INC:
2008       fprintf (file, "*%s++", reg_names[REGNO (XEXP (addr, 0))]);
2009       break;
2010
2011     case POST_MODIFY:
2012       {
2013         rtx op0 = XEXP (XEXP (addr, 1), 0);
2014         rtx op1 = XEXP (XEXP (addr, 1), 1);
2015         
2016         if (GET_CODE (XEXP (addr, 1)) == PLUS && REG_P (op1))
2017           fprintf (file, "*%s++(%s)", reg_names[REGNO (op0)],
2018                    reg_names[REGNO (op1)]);
2019         else if (GET_CODE (XEXP (addr, 1)) == PLUS && INTVAL (op1) > 0)
2020           fprintf (file, "*%s++(" HOST_WIDE_INT_PRINT_DEC ")",
2021                    reg_names[REGNO (op0)], INTVAL (op1));
2022         else if (GET_CODE (XEXP (addr, 1)) == PLUS && INTVAL (op1) < 0)
2023           fprintf (file, "*%s--(" HOST_WIDE_INT_PRINT_DEC ")",
2024                    reg_names[REGNO (op0)], -INTVAL (op1));
2025         else if (GET_CODE (XEXP (addr, 1)) == MINUS && REG_P (op1))
2026           fprintf (file, "*%s--(%s)", reg_names[REGNO (op0)],
2027                    reg_names[REGNO (op1)]);
2028         else
2029           fatal_insn ("c4x_print_operand_address: Bad post_modify", addr);
2030       }
2031       break;
2032       
2033     case PRE_MODIFY:
2034       {
2035         rtx op0 = XEXP (XEXP (addr, 1), 0);
2036         rtx op1 = XEXP (XEXP (addr, 1), 1);
2037         
2038         if (GET_CODE (XEXP (addr, 1)) == PLUS && REG_P (op1))
2039           fprintf (file, "*++%s(%s)", reg_names[REGNO (op0)],
2040                    reg_names[REGNO (op1)]);
2041         else if (GET_CODE (XEXP (addr, 1)) == PLUS && INTVAL (op1) > 0)
2042           fprintf (file, "*++%s(" HOST_WIDE_INT_PRINT_DEC ")",
2043                    reg_names[REGNO (op0)], INTVAL (op1));
2044         else if (GET_CODE (XEXP (addr, 1)) == PLUS && INTVAL (op1) < 0)
2045           fprintf (file, "*--%s(" HOST_WIDE_INT_PRINT_DEC ")",
2046                    reg_names[REGNO (op0)], -INTVAL (op1));
2047         else if (GET_CODE (XEXP (addr, 1)) == MINUS && REG_P (op1))
2048           fprintf (file, "*--%s(%s)", reg_names[REGNO (op0)],
2049                    reg_names[REGNO (op1)]);
2050         else
2051           fatal_insn ("c4x_print_operand_address: Bad pre_modify", addr);
2052       }
2053       break;
2054       
2055     case PRE_INC:
2056       fprintf (file, "*++%s", reg_names[REGNO (XEXP (addr, 0))]);
2057       break;
2058
2059     case POST_DEC:
2060       fprintf (file, "*%s--", reg_names[REGNO (XEXP (addr, 0))]);
2061       break;
2062
2063     case PLUS:                  /* Indirect with displacement.  */
2064       {
2065         rtx op0 = XEXP (addr, 0);
2066         rtx op1 = XEXP (addr, 1);
2067
2068         if (REG_P (op0))
2069           {
2070             if (REG_P (op1))
2071               {
2072                 if (IS_INDEX_REG (op0))
2073                   {
2074                     fprintf (file, "*+%s(%s)",
2075                              reg_names[REGNO (op1)],
2076                              reg_names[REGNO (op0)]);   /* Index + base.  */
2077                   }
2078                 else
2079                   {
2080                     fprintf (file, "*+%s(%s)",
2081                              reg_names[REGNO (op0)],
2082                              reg_names[REGNO (op1)]);   /* Base + index.  */
2083                   }
2084               }
2085             else if (INTVAL (op1) < 0)
2086               {
2087                 fprintf (file, "*-%s(" HOST_WIDE_INT_PRINT_DEC ")",
2088                          reg_names[REGNO (op0)],
2089                          -INTVAL (op1));        /* Base - displacement.  */
2090               }
2091             else
2092               {
2093                 fprintf (file, "*+%s(" HOST_WIDE_INT_PRINT_DEC ")",
2094                          reg_names[REGNO (op0)],
2095                          INTVAL (op1)); /* Base + displacement.  */
2096               }
2097           }
2098         else
2099           fatal_insn ("c4x_print_operand_address: Bad operand case", addr);
2100       }
2101       break;
2102
2103     case LO_SUM:
2104       {
2105         rtx op0 = XEXP (addr, 0);
2106         rtx op1 = XEXP (addr, 1);
2107           
2108         if (REG_P (op0) && REGNO (op0) == DP_REGNO)
2109           c4x_print_operand_address (file, op1);
2110         else
2111           fatal_insn ("c4x_print_operand_address: Bad operand case", addr);
2112       }
2113       break;
2114
2115     case CONST:
2116     case SYMBOL_REF:
2117     case LABEL_REF:
2118       fprintf (file, "@");
2119       output_addr_const (file, addr);
2120       break;
2121
2122       /* We shouldn't access CONST_INT addresses.  */
2123     case CONST_INT:
2124
2125     default:
2126       fatal_insn ("c4x_print_operand_address: Bad operand case", addr);
2127       break;
2128     }
2129 }
2130
2131
2132 /* Return nonzero if the floating point operand will fit
2133    in the immediate field.  */
2134
2135 static int
2136 c4x_immed_float_p (rtx op)
2137 {
2138   long convval[2];
2139   int exponent;
2140   REAL_VALUE_TYPE r;
2141
2142   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2143   if (GET_MODE (op) == HFmode)
2144     REAL_VALUE_TO_TARGET_DOUBLE (r, convval);
2145   else
2146     {
2147       REAL_VALUE_TO_TARGET_SINGLE (r, convval[0]);
2148       convval[1] = 0;
2149     }
2150
2151   /* Sign extend exponent.  */
2152   exponent = (((convval[0] >> 24) & 0xff) ^ 0x80) - 0x80;
2153   if (exponent == -128)
2154     return 1;                   /* 0.0  */
2155   if ((convval[0] & 0x00000fff) != 0 || convval[1] != 0)
2156     return 0;                   /* Precision doesn't fit.  */
2157   return (exponent <= 7)        /* Positive exp.  */
2158     && (exponent >= -7);        /* Negative exp.  */
2159 }
2160
2161
2162 /* The last instruction in a repeat block cannot be a Bcond, DBcound,
2163    CALL, CALLCond, TRAPcond, RETIcond, RETScond, IDLE, RPTB or RPTS.
2164
2165    None of the last four instructions from the bottom of the block can
2166    be a BcondD, BRD, DBcondD, RPTBD, LAJ, LAJcond, LATcond, BcondAF,
2167    BcondAT or RETIcondD.
2168
2169    This routine scans the four previous insns for a jump insn, and if
2170    one is found, returns 1 so that we bung in a nop instruction.
2171    This simple minded strategy will add a nop, when it may not
2172    be required.  Say when there is a JUMP_INSN near the end of the
2173    block that doesn't get converted into a delayed branch.
2174
2175    Note that we cannot have a call insn, since we don't generate
2176    repeat loops with calls in them (although I suppose we could, but
2177    there's no benefit.)  
2178
2179    !!! FIXME.  The rptb_top insn may be sucked into a SEQUENCE.  */
2180
2181 int
2182 c4x_rptb_nop_p (rtx insn)
2183 {
2184   rtx start_label;
2185   int i;
2186
2187   /* Extract the start label from the jump pattern (rptb_end).  */
2188   start_label = XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn), 0, 0)), 1), 0);
2189
2190   /* If there is a label at the end of the loop we must insert
2191      a NOP.  */
2192   do {
2193     insn = previous_insn (insn);
2194   } while (GET_CODE (insn) == NOTE
2195            || GET_CODE (insn) == USE
2196            || GET_CODE (insn) == CLOBBER);
2197   if (GET_CODE (insn) == CODE_LABEL)
2198     return 1;
2199
2200   for (i = 0; i < 4; i++)
2201     {
2202       /* Search back for prev non-note and non-label insn.  */
2203       while (GET_CODE (insn) == NOTE || GET_CODE (insn) == CODE_LABEL
2204              || GET_CODE (insn) == USE || GET_CODE (insn) == CLOBBER)
2205         {
2206           if (insn == start_label)
2207             return i == 0;
2208
2209           insn = previous_insn (insn);
2210         };
2211
2212       /* If we have a jump instruction we should insert a NOP. If we
2213          hit repeat block top we should only insert a NOP if the loop
2214          is empty.  */
2215       if (GET_CODE (insn) == JUMP_INSN)
2216         return 1;
2217       insn = previous_insn (insn);
2218     }
2219   return 0;
2220 }
2221
2222
2223 /* The C4x looping instruction needs to be emitted at the top of the
2224   loop.  Emitting the true RTL for a looping instruction at the top of
2225   the loop can cause problems with flow analysis.  So instead, a dummy
2226   doloop insn is emitted at the end of the loop.  This routine checks
2227   for the presence of this doloop insn and then searches back to the
2228   top of the loop, where it inserts the true looping insn (provided
2229   there are no instructions in the loop which would cause problems).
2230   Any additional labels can be emitted at this point.  In addition, if
2231   the desired loop count register was not allocated, this routine does
2232   nothing. 
2233
2234   Before we can create a repeat block looping instruction we have to
2235   verify that there are no jumps outside the loop and no jumps outside
2236   the loop go into this loop. This can happen in the basic blocks reorder
2237   pass. The C4x cpu can not handle this.  */
2238
2239 static int
2240 c4x_label_ref_used_p (rtx x, rtx code_label)
2241 {
2242   enum rtx_code code;
2243   int i, j;
2244   const char *fmt;
2245
2246   if (x == 0)
2247     return 0;
2248
2249   code = GET_CODE (x);
2250   if (code == LABEL_REF)
2251     return INSN_UID (XEXP (x,0)) == INSN_UID (code_label);
2252
2253   fmt = GET_RTX_FORMAT (code);
2254   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2255     {
2256       if (fmt[i] == 'e')
2257         {
2258           if (c4x_label_ref_used_p (XEXP (x, i), code_label))
2259             return 1;
2260         }
2261       else if (fmt[i] == 'E')
2262         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2263           if (c4x_label_ref_used_p (XVECEXP (x, i, j), code_label))
2264             return 1;
2265     }
2266   return 0;
2267 }
2268
2269
2270 static int
2271 c4x_rptb_valid_p (rtx insn, rtx start_label)
2272 {
2273   rtx end = insn;
2274   rtx start;
2275   rtx tmp;
2276
2277   /* Find the start label.  */
2278   for (; insn; insn = PREV_INSN (insn))
2279     if (insn == start_label)
2280       break;
2281
2282   /* Note found then we can not use a rptb or rpts.  The label was
2283      probably moved by the basic block reorder pass.  */
2284   if (! insn)
2285     return 0;
2286
2287   start = insn;
2288   /* If any jump jumps inside this block then we must fail.  */
2289   for (insn = PREV_INSN (start); insn; insn = PREV_INSN (insn))
2290     {
2291       if (GET_CODE (insn) == CODE_LABEL)
2292         {
2293           for (tmp = NEXT_INSN (start); tmp != end; tmp = NEXT_INSN(tmp))
2294             if (GET_CODE (tmp) == JUMP_INSN
2295                 && c4x_label_ref_used_p (tmp, insn))
2296               return 0;
2297         }
2298     }
2299   for (insn = NEXT_INSN (end); insn; insn = NEXT_INSN (insn))
2300     {
2301       if (GET_CODE (insn) == CODE_LABEL)
2302         {
2303           for (tmp = NEXT_INSN (start); tmp != end; tmp = NEXT_INSN(tmp))
2304             if (GET_CODE (tmp) == JUMP_INSN
2305                 && c4x_label_ref_used_p (tmp, insn))
2306               return 0;
2307         }
2308     }
2309   /* If any jump jumps outside this block then we must fail.  */
2310   for (insn = NEXT_INSN (start); insn != end; insn = NEXT_INSN (insn))
2311     {
2312       if (GET_CODE (insn) == CODE_LABEL)
2313         {
2314           for (tmp = NEXT_INSN (end); tmp; tmp = NEXT_INSN(tmp))
2315             if (GET_CODE (tmp) == JUMP_INSN
2316                 && c4x_label_ref_used_p (tmp, insn))
2317               return 0;
2318           for (tmp = PREV_INSN (start); tmp; tmp = PREV_INSN(tmp))
2319             if (GET_CODE (tmp) == JUMP_INSN
2320                 && c4x_label_ref_used_p (tmp, insn))
2321               return 0;
2322         }
2323     }
2324
2325   /* All checks OK.  */
2326   return 1;
2327 }
2328
2329
2330 void
2331 c4x_rptb_insert (rtx insn)
2332 {
2333   rtx end_label;
2334   rtx start_label;
2335   rtx new_start_label;
2336   rtx count_reg;
2337
2338   /* If the count register has not been allocated to RC, say if
2339      there is a movstr pattern in the loop, then do not insert a
2340      RPTB instruction.  Instead we emit a decrement and branch
2341      at the end of the loop.  */
2342   count_reg = XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn), 0, 0)), 0), 0);
2343   if (REGNO (count_reg) != RC_REGNO)
2344     return;
2345
2346   /* Extract the start label from the jump pattern (rptb_end).  */
2347   start_label = XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn), 0, 0)), 1), 0);
2348   
2349   if (! c4x_rptb_valid_p (insn, start_label))
2350     {
2351       /* We can not use the rptb insn.  Replace it so reorg can use
2352          the delay slots of the jump insn.  */
2353       emit_insn_before (gen_addqi3 (count_reg, count_reg, GEN_INT (-1)), insn);
2354       emit_insn_before (gen_cmpqi (count_reg, GEN_INT (0)), insn);
2355       emit_insn_before (gen_bge (start_label), insn);
2356       LABEL_NUSES (start_label)++;
2357       delete_insn (insn);
2358       return;
2359     }
2360
2361   end_label = gen_label_rtx ();
2362   LABEL_NUSES (end_label)++;
2363   emit_label_after (end_label, insn);
2364
2365   new_start_label = gen_label_rtx ();
2366   LABEL_NUSES (new_start_label)++;
2367
2368   for (; insn; insn = PREV_INSN (insn))
2369     {
2370       if (insn == start_label)
2371          break;
2372       if (GET_CODE (insn) == JUMP_INSN &&
2373           JUMP_LABEL (insn) == start_label)
2374         redirect_jump (insn, new_start_label, 0);
2375     }
2376   if (! insn)
2377     fatal_insn ("c4x_rptb_insert: Cannot find start label", start_label);
2378
2379   emit_label_after (new_start_label, insn);
2380
2381   if (TARGET_RPTS && c4x_rptb_rpts_p (PREV_INSN (insn), 0))
2382     emit_insn_after (gen_rpts_top (new_start_label, end_label), insn);
2383   else
2384     emit_insn_after (gen_rptb_top (new_start_label, end_label), insn);
2385   if (LABEL_NUSES (start_label) == 0)
2386     delete_insn (start_label);
2387 }
2388
2389
2390 /* We need to use direct addressing for large constants and addresses
2391    that cannot fit within an instruction.  We must check for these
2392    after after the final jump optimization pass, since this may
2393    introduce a local_move insn for a SYMBOL_REF.  This pass
2394    must come before delayed branch slot filling since it can generate
2395    additional instructions.
2396
2397    This function also fixes up RTPB style loops that didn't get RC
2398    allocated as the loop counter.  */
2399
2400 static void
2401 c4x_reorg (void)
2402 {
2403   rtx insn;
2404
2405   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2406     {
2407       /* Look for insn.  */
2408       if (INSN_P (insn))
2409         {
2410           int insn_code_number;
2411           rtx old;
2412
2413           insn_code_number = recog_memoized (insn);
2414
2415           if (insn_code_number < 0)
2416             continue;
2417
2418           /* Insert the RTX for RPTB at the top of the loop
2419              and a label at the end of the loop.  */
2420           if (insn_code_number == CODE_FOR_rptb_end)
2421             c4x_rptb_insert(insn);
2422
2423           /* We need to split the insn here. Otherwise the calls to
2424              force_const_mem will not work for load_immed_address.  */
2425           old = insn;
2426
2427           /* Don't split the insn if it has been deleted.  */
2428           if (! INSN_DELETED_P (old))
2429             insn = try_split (PATTERN(old), old, 1);
2430
2431           /* When not optimizing, the old insn will be still left around
2432              with only the 'deleted' bit set.  Transform it into a note
2433              to avoid confusion of subsequent processing.  */
2434           if (INSN_DELETED_P (old))
2435             {
2436               PUT_CODE (old, NOTE);
2437               NOTE_LINE_NUMBER (old) = NOTE_INSN_DELETED;
2438               NOTE_SOURCE_FILE (old) = 0;
2439             }
2440         }
2441     }
2442 }
2443
2444
2445 static int
2446 c4x_a_register (rtx op)
2447 {
2448   return REG_P (op) && IS_ADDR_OR_PSEUDO_REG (op);
2449 }
2450
2451
2452 static int
2453 c4x_x_register (rtx op)
2454 {
2455   return REG_P (op) && IS_INDEX_OR_PSEUDO_REG (op);
2456 }
2457
2458
2459 static int
2460 c4x_immed_int_constant (rtx op)
2461 {
2462   if (GET_CODE (op) != CONST_INT)
2463     return 0;
2464
2465   return GET_MODE (op) == VOIDmode
2466     || GET_MODE_CLASS (GET_MODE (op)) == MODE_INT
2467     || GET_MODE_CLASS (GET_MODE (op)) == MODE_PARTIAL_INT;
2468 }
2469
2470
2471 static int
2472 c4x_immed_float_constant (rtx op)
2473 {
2474   if (GET_CODE (op) != CONST_DOUBLE)
2475     return 0;
2476
2477   /* Do not check if the CONST_DOUBLE is in memory. If there is a MEM
2478      present this only means that a MEM rtx has been generated. It does
2479      not mean the rtx is really in memory.  */
2480
2481   return GET_MODE (op) == QFmode || GET_MODE (op) == HFmode;
2482 }
2483
2484
2485 int
2486 c4x_shiftable_constant (rtx op)
2487 {
2488   int i;
2489   int mask;
2490   int val = INTVAL (op);
2491
2492   for (i = 0; i < 16; i++)
2493     {
2494       if (val & (1 << i))
2495         break;
2496     }
2497   mask = ((0xffff >> i) << 16) | 0xffff;
2498   if (IS_INT16_CONST (val & (1 << 31) ? (val >> i) | ~mask
2499                                       : (val >> i) & mask))
2500     return i;
2501   return -1;
2502
2503
2504
2505 int
2506 c4x_H_constant (rtx op)
2507 {
2508   return c4x_immed_float_constant (op) && c4x_immed_float_p (op);
2509 }
2510
2511
2512 int
2513 c4x_I_constant (rtx op)
2514 {
2515   return c4x_immed_int_constant (op) && IS_INT16_CONST (INTVAL (op));
2516 }
2517
2518
2519 int
2520 c4x_J_constant (rtx op)
2521 {
2522   if (TARGET_C3X)
2523     return 0;
2524   return c4x_immed_int_constant (op) && IS_INT8_CONST (INTVAL (op));
2525 }
2526
2527
2528 static int
2529 c4x_K_constant (rtx op)
2530 {
2531   if (TARGET_C3X || ! c4x_immed_int_constant (op))
2532     return 0;
2533   return IS_INT5_CONST (INTVAL (op));
2534 }
2535
2536
2537 int
2538 c4x_L_constant (rtx op)
2539 {
2540   return c4x_immed_int_constant (op) && IS_UINT16_CONST (INTVAL (op));
2541 }
2542
2543
2544 static int
2545 c4x_N_constant (rtx op)
2546 {
2547   return c4x_immed_int_constant (op) && IS_NOT_UINT16_CONST (INTVAL (op));
2548 }
2549
2550
2551 static int
2552 c4x_O_constant (rtx op)
2553 {
2554   return c4x_immed_int_constant (op) && IS_HIGH_CONST (INTVAL (op));
2555 }
2556
2557
2558 /* The constraints do not have to check the register class,
2559    except when needed to discriminate between the constraints.
2560    The operand has been checked by the predicates to be valid.  */
2561
2562 /* ARx + 9-bit signed const or IRn
2563    *ARx, *+ARx(n), *-ARx(n), *+ARx(IRn), *-Arx(IRn) for -256 < n < 256
2564    We don't include the pre/post inc/dec forms here since
2565    they are handled by the <> constraints.  */
2566
2567 int
2568 c4x_Q_constraint (rtx op)
2569 {
2570   enum machine_mode mode = GET_MODE (op);
2571
2572   if (GET_CODE (op) != MEM)
2573     return 0;
2574   op = XEXP (op, 0);
2575   switch (GET_CODE (op))
2576     {
2577     case REG:
2578       return 1;
2579
2580     case PLUS:
2581       {
2582         rtx op0 = XEXP (op, 0);
2583         rtx op1 = XEXP (op, 1);
2584
2585         if (! REG_P (op0))
2586           return 0;
2587
2588         if (REG_P (op1))
2589           return 1;
2590
2591         if (GET_CODE (op1) != CONST_INT)
2592           return 0;
2593
2594         /* HImode and HFmode must be offsettable.  */
2595         if (mode == HImode || mode == HFmode)
2596           return IS_DISP8_OFF_CONST (INTVAL (op1));
2597         
2598         return IS_DISP8_CONST (INTVAL (op1));
2599       }
2600       break;
2601
2602     default:
2603       break;
2604     }
2605   return 0;
2606 }
2607
2608
2609 /* ARx + 5-bit unsigned const
2610    *ARx, *+ARx(n) for n < 32.  */
2611
2612 int
2613 c4x_R_constraint (rtx op)
2614 {
2615   enum machine_mode mode = GET_MODE (op);
2616
2617   if (TARGET_C3X)
2618     return 0;
2619   if (GET_CODE (op) != MEM)
2620     return 0;
2621   op = XEXP (op, 0);
2622   switch (GET_CODE (op))
2623     {
2624     case REG:
2625       return 1;
2626
2627     case PLUS:
2628       {
2629         rtx op0 = XEXP (op, 0);
2630         rtx op1 = XEXP (op, 1);
2631
2632         if (! REG_P (op0))
2633           return 0;
2634
2635         if (GET_CODE (op1) != CONST_INT)
2636           return 0;
2637
2638         /* HImode and HFmode must be offsettable.  */
2639         if (mode == HImode || mode == HFmode)
2640           return IS_UINT5_CONST (INTVAL (op1) + 1);
2641         
2642         return IS_UINT5_CONST (INTVAL (op1));
2643       }
2644       break;
2645
2646     default:
2647       break;
2648     }
2649   return 0;
2650 }
2651
2652
2653 static int
2654 c4x_R_indirect (rtx op)
2655 {
2656   enum machine_mode mode = GET_MODE (op);
2657
2658   if (TARGET_C3X || GET_CODE (op) != MEM)
2659     return 0;
2660
2661   op = XEXP (op, 0);
2662   switch (GET_CODE (op))
2663     {
2664     case REG:
2665       return IS_ADDR_OR_PSEUDO_REG (op);
2666
2667     case PLUS:
2668       {
2669         rtx op0 = XEXP (op, 0);
2670         rtx op1 = XEXP (op, 1);
2671
2672         /* HImode and HFmode must be offsettable.  */
2673         if (mode == HImode || mode == HFmode)
2674           return IS_ADDR_OR_PSEUDO_REG (op0)
2675             && GET_CODE (op1) == CONST_INT 
2676             && IS_UINT5_CONST (INTVAL (op1) + 1);
2677
2678         return REG_P (op0)
2679           && IS_ADDR_OR_PSEUDO_REG (op0)
2680           && GET_CODE (op1) == CONST_INT
2681           && IS_UINT5_CONST (INTVAL (op1));
2682       }
2683       break;
2684
2685     default:
2686       break;
2687     }
2688   return 0;
2689 }
2690
2691
2692 /* ARx + 1-bit unsigned const or IRn
2693    *ARx, *+ARx(1), *-ARx(1), *+ARx(IRn), *-Arx(IRn)
2694    We don't include the pre/post inc/dec forms here since
2695    they are handled by the <> constraints.  */
2696
2697 int
2698 c4x_S_constraint (rtx op)
2699 {
2700   enum machine_mode mode = GET_MODE (op);
2701   if (GET_CODE (op) != MEM)
2702     return 0;
2703   op = XEXP (op, 0);
2704   switch (GET_CODE (op))
2705     {
2706     case REG:
2707       return 1;
2708
2709     case PRE_MODIFY:
2710     case POST_MODIFY:
2711       {
2712         rtx op0 = XEXP (op, 0);
2713         rtx op1 = XEXP (op, 1);
2714         
2715         if ((GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS)
2716             || (op0 != XEXP (op1, 0)))
2717           return 0;
2718         
2719         op0 = XEXP (op1, 0);
2720         op1 = XEXP (op1, 1);
2721         return REG_P (op0) && REG_P (op1);
2722         /* Pre or post_modify with a displacement of 0 or 1 
2723            should not be generated.  */
2724       }
2725       break;
2726
2727     case PLUS:
2728       {
2729         rtx op0 = XEXP (op, 0);
2730         rtx op1 = XEXP (op, 1);
2731
2732         if (!REG_P (op0))
2733           return 0;
2734
2735         if (REG_P (op1))
2736           return 1;
2737
2738         if (GET_CODE (op1) != CONST_INT)
2739           return 0;
2740         
2741         /* HImode and HFmode must be offsettable.  */
2742         if (mode == HImode || mode == HFmode)
2743           return IS_DISP1_OFF_CONST (INTVAL (op1));
2744         
2745         return IS_DISP1_CONST (INTVAL (op1));
2746       }
2747       break;
2748
2749     default:
2750       break;
2751     }
2752   return 0;
2753 }
2754
2755
2756 static int
2757 c4x_S_indirect (rtx op)
2758 {
2759   enum machine_mode mode = GET_MODE (op);
2760   if (GET_CODE (op) != MEM)
2761     return 0;
2762
2763   op = XEXP (op, 0);
2764   switch (GET_CODE (op))
2765     {
2766     case PRE_DEC:
2767     case POST_DEC:
2768       if (mode != QImode && mode != QFmode)
2769         return 0;
2770     case PRE_INC:
2771     case POST_INC:
2772       op = XEXP (op, 0);
2773
2774     case REG:
2775       return IS_ADDR_OR_PSEUDO_REG (op);
2776
2777     case PRE_MODIFY:
2778     case POST_MODIFY:
2779       {
2780         rtx op0 = XEXP (op, 0);
2781         rtx op1 = XEXP (op, 1);
2782         
2783         if (mode != QImode && mode != QFmode)
2784           return 0;
2785
2786         if ((GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS)
2787             || (op0 != XEXP (op1, 0)))
2788           return 0;
2789         
2790         op0 = XEXP (op1, 0);
2791         op1 = XEXP (op1, 1);
2792         return REG_P (op0) && IS_ADDR_OR_PSEUDO_REG (op0)
2793           && REG_P (op1) && IS_INDEX_OR_PSEUDO_REG (op1);
2794         /* Pre or post_modify with a displacement of 0 or 1 
2795            should not be generated.  */
2796       }
2797
2798     case PLUS:
2799       {
2800         rtx op0 = XEXP (op, 0);
2801         rtx op1 = XEXP (op, 1);
2802
2803         if (REG_P (op0))
2804           {
2805             /* HImode and HFmode must be offsettable.  */
2806             if (mode == HImode || mode == HFmode)
2807               return IS_ADDR_OR_PSEUDO_REG (op0)
2808                 && GET_CODE (op1) == CONST_INT 
2809                 && IS_DISP1_OFF_CONST (INTVAL (op1));
2810
2811             if (REG_P (op1))
2812               return (IS_INDEX_OR_PSEUDO_REG (op1)
2813                       && IS_ADDR_OR_PSEUDO_REG (op0))
2814                 || (IS_ADDR_OR_PSEUDO_REG (op1)
2815                     && IS_INDEX_OR_PSEUDO_REG (op0));
2816             
2817             return IS_ADDR_OR_PSEUDO_REG (op0)
2818               && GET_CODE (op1) == CONST_INT 
2819               && IS_DISP1_CONST (INTVAL (op1));
2820           }
2821       }
2822       break;
2823
2824     default:
2825       break;
2826     }
2827   return 0;
2828 }
2829
2830
2831 /* Direct memory operand.  */
2832
2833 int
2834 c4x_T_constraint (rtx op)
2835 {
2836   if (GET_CODE (op) != MEM)
2837     return 0;
2838   op = XEXP (op, 0);
2839
2840   if (GET_CODE (op) != LO_SUM)
2841     {
2842       /* Allow call operands.  */
2843       return GET_CODE (op) == SYMBOL_REF
2844         && GET_MODE (op) == Pmode
2845         && SYMBOL_REF_FUNCTION_P (op);
2846     }
2847
2848   /* HImode and HFmode are not offsettable.  */
2849   if (GET_MODE (op) == HImode || GET_CODE (op) == HFmode)
2850     return 0;
2851
2852   if ((GET_CODE (XEXP (op, 0)) == REG)
2853       && (REGNO (XEXP (op, 0)) == DP_REGNO))
2854     return c4x_U_constraint (XEXP (op, 1));
2855   
2856   return 0;
2857 }
2858
2859
2860 /* Symbolic operand.  */
2861
2862 int
2863 c4x_U_constraint (rtx op)
2864 {
2865   /* Don't allow direct addressing to an arbitrary constant.  */
2866   return GET_CODE (op) == CONST
2867          || GET_CODE (op) == SYMBOL_REF
2868          || GET_CODE (op) == LABEL_REF;
2869 }
2870
2871
2872 int
2873 c4x_autoinc_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2874 {
2875   if (GET_CODE (op) == MEM)
2876     {
2877       enum rtx_code code = GET_CODE (XEXP (op, 0));
2878       
2879       if (code == PRE_INC
2880           || code == PRE_DEC
2881           || code == POST_INC
2882           || code == POST_DEC
2883           || code == PRE_MODIFY
2884           || code == POST_MODIFY
2885           )
2886         return 1;
2887     }
2888   return 0;
2889 }
2890
2891
2892 /* Match any operand.  */
2893
2894 int
2895 any_operand (register rtx op ATTRIBUTE_UNUSED,
2896              enum machine_mode mode ATTRIBUTE_UNUSED)
2897 {
2898   return 1;
2899 }
2900
2901
2902 /* Nonzero if OP is a floating point value with value 0.0.  */
2903
2904 int
2905 fp_zero_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2906 {
2907   REAL_VALUE_TYPE r;
2908
2909   if (GET_CODE (op) != CONST_DOUBLE)
2910     return 0;
2911   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2912   return REAL_VALUES_EQUAL (r, dconst0);
2913 }
2914
2915
2916 int
2917 const_operand (register rtx op, register enum machine_mode mode)
2918 {
2919   switch (mode)
2920     {
2921     case QFmode:
2922     case HFmode:
2923       if (GET_CODE (op) != CONST_DOUBLE
2924           || GET_MODE (op) != mode
2925           || GET_MODE_CLASS (mode) != MODE_FLOAT)
2926         return 0;
2927
2928       return c4x_immed_float_p (op);
2929
2930 #if Pmode != QImode
2931     case Pmode:
2932 #endif
2933     case QImode:
2934       if (GET_CODE (op) == CONSTANT_P_RTX)
2935         return 1;
2936
2937       if (GET_CODE (op) != CONST_INT
2938           || (GET_MODE (op) != VOIDmode && GET_MODE (op) != mode)
2939           || GET_MODE_CLASS (mode) != MODE_INT)
2940         return 0;
2941
2942       return IS_HIGH_CONST (INTVAL (op)) || IS_INT16_CONST (INTVAL (op));
2943
2944     case HImode:
2945       return 0;
2946
2947     default:
2948       return 0;
2949     }
2950 }
2951
2952
2953 int
2954 stik_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2955 {
2956   return c4x_K_constant (op);
2957 }
2958
2959
2960 int
2961 not_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2962 {
2963   return c4x_N_constant (op);
2964 }
2965
2966
2967 int
2968 reg_operand (rtx op, enum machine_mode mode)
2969 {
2970   if (GET_CODE (op) == SUBREG
2971       && GET_MODE (op) == QFmode)
2972     return 0;
2973   return register_operand (op, mode);
2974 }
2975
2976
2977 int
2978 mixed_subreg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2979 {
2980   /* Allow (subreg:HF (reg:HI)) that be generated for a union of an
2981      int and a long double.  */
2982   if (GET_CODE (op) == SUBREG
2983       && (GET_MODE (op) == QFmode)
2984       && (GET_MODE (SUBREG_REG (op)) == QImode
2985           || GET_MODE (SUBREG_REG (op)) == HImode))
2986     return 1;
2987   return 0;
2988 }
2989
2990
2991 int
2992 reg_imm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2993 {
2994   if (REG_P (op) || CONSTANT_P (op))
2995     return 1;
2996   return 0;
2997 }
2998
2999
3000 int
3001 not_modify_reg (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3002 {
3003   if (REG_P (op) || CONSTANT_P (op))
3004     return 1;
3005   if (GET_CODE (op) != MEM)
3006     return 0;
3007   op = XEXP (op, 0);
3008   switch (GET_CODE (op))
3009     {
3010     case REG:
3011       return 1;
3012
3013     case PLUS:
3014       {
3015         rtx op0 = XEXP (op, 0);
3016         rtx op1 = XEXP (op, 1);
3017
3018         if (! REG_P (op0))
3019           return 0;
3020         
3021         if (REG_P (op1) || GET_CODE (op1) == CONST_INT)
3022           return 1;
3023       }
3024
3025     case LO_SUM:
3026       {
3027         rtx op0 = XEXP (op, 0);
3028           
3029         if (REG_P (op0) && REGNO (op0) == DP_REGNO)
3030           return 1;
3031       }
3032       break;
3033      
3034     case CONST:
3035     case SYMBOL_REF:
3036     case LABEL_REF:
3037       return 1;
3038
3039     default:
3040       break;
3041     }
3042   return 0;
3043 }
3044
3045
3046 int
3047 not_rc_reg (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3048 {
3049   if (REG_P (op) && REGNO (op) == RC_REGNO)
3050     return 0;
3051   return 1;
3052 }
3053
3054
3055 /* Extended precision register R0-R1.  */
3056
3057 int
3058 r0r1_reg_operand (rtx op, enum machine_mode mode)
3059 {
3060   if (! reg_operand (op, mode))
3061     return 0;
3062   if (GET_CODE (op) == SUBREG)
3063     op = SUBREG_REG (op);
3064   return REG_P (op) && IS_R0R1_OR_PSEUDO_REG (op);
3065 }
3066
3067
3068 /* Extended precision register R2-R3.  */
3069
3070 int
3071 r2r3_reg_operand (rtx op, enum machine_mode mode)
3072 {
3073   if (! reg_operand (op, mode))
3074     return 0;
3075   if (GET_CODE (op) == SUBREG)
3076     op = SUBREG_REG (op);
3077   return REG_P (op) && IS_R2R3_OR_PSEUDO_REG (op);
3078 }
3079
3080
3081 /* Low extended precision register R0-R7.  */
3082
3083 int
3084 ext_low_reg_operand (rtx op, enum machine_mode mode)
3085 {
3086   if (! reg_operand (op, mode))
3087     return 0;
3088   if (GET_CODE (op) == SUBREG)
3089     op = SUBREG_REG (op);
3090   return REG_P (op) && IS_EXT_LOW_OR_PSEUDO_REG (op);
3091 }
3092
3093
3094 /* Extended precision register.  */
3095
3096 int
3097 ext_reg_operand (rtx op, enum machine_mode mode)
3098 {
3099   if (! reg_operand (op, mode))
3100     return 0;
3101   if (GET_CODE (op) == SUBREG)
3102     op = SUBREG_REG (op);
3103   if (! REG_P (op))
3104     return 0;
3105   return IS_EXT_OR_PSEUDO_REG (op);
3106 }
3107
3108
3109 /* Standard precision register.  */
3110
3111 int
3112 std_reg_operand (rtx op, enum machine_mode mode)
3113 {
3114   if (! reg_operand (op, mode))
3115     return 0;
3116   if (GET_CODE (op) == SUBREG)
3117     op = SUBREG_REG (op);
3118   return REG_P (op) && IS_STD_OR_PSEUDO_REG (op);
3119 }
3120
3121 /* Standard precision or normal register.  */
3122
3123 int
3124 std_or_reg_operand (rtx op, enum machine_mode mode)
3125 {
3126   if (reload_in_progress)
3127     return std_reg_operand (op, mode);
3128   return reg_operand (op, mode);
3129 }
3130
3131 /* Address register.  */
3132
3133 int
3134 addr_reg_operand (rtx op, enum machine_mode mode)
3135 {
3136   if (! reg_operand (op, mode))
3137     return 0;
3138   return c4x_a_register (op);
3139 }
3140
3141
3142 /* Index register.  */
3143
3144 int
3145 index_reg_operand (rtx op, enum machine_mode mode)
3146 {
3147   if (! reg_operand (op, mode))
3148     return 0;
3149   if (GET_CODE (op) == SUBREG)
3150     op = SUBREG_REG (op);
3151   return c4x_x_register (op);
3152 }
3153
3154
3155 /* DP register.  */
3156
3157 int
3158 dp_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3159 {
3160   return REG_P (op) && IS_DP_OR_PSEUDO_REG (op);
3161 }
3162
3163
3164 /* SP register.  */
3165
3166 int
3167 sp_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3168 {
3169   return REG_P (op) && IS_SP_OR_PSEUDO_REG (op);
3170 }
3171
3172
3173 /* ST register.  */
3174
3175 int
3176 st_reg_operand (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3177 {
3178   return REG_P (op) && IS_ST_OR_PSEUDO_REG (op);
3179 }
3180
3181
3182 /* RC register.  */
3183
3184 int
3185 rc_reg_operand (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3186 {
3187   return REG_P (op) && IS_RC_OR_PSEUDO_REG (op);
3188 }
3189
3190
3191 int
3192 call_address_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3193 {
3194   return (REG_P (op) || symbolic_address_operand (op, mode));
3195 }
3196
3197
3198 /* Symbolic address operand.  */
3199
3200 int
3201 symbolic_address_operand (register rtx op,
3202                           enum machine_mode mode ATTRIBUTE_UNUSED)
3203 {
3204   switch (GET_CODE (op))
3205     {
3206     case CONST:
3207     case SYMBOL_REF:
3208     case LABEL_REF:
3209       return 1;
3210     default:
3211       return 0;
3212     }
3213 }
3214
3215
3216 /* Check dst operand of a move instruction.  */
3217
3218 int
3219 dst_operand (rtx op, enum machine_mode mode)
3220 {
3221   if (GET_CODE (op) == SUBREG
3222       && mixed_subreg_operand (op, mode))
3223     return 0;
3224
3225   if (REG_P (op))
3226     return reg_operand (op, mode);
3227
3228   return nonimmediate_operand (op, mode);
3229 }
3230
3231
3232 /* Check src operand of two operand arithmetic instructions.  */
3233
3234 int
3235 src_operand (rtx op, enum machine_mode mode)
3236 {
3237   if (GET_CODE (op) == SUBREG
3238       && mixed_subreg_operand (op, mode))
3239     return 0;
3240
3241   if (REG_P (op))
3242     return reg_operand (op, mode);
3243
3244   if (mode == VOIDmode)
3245     mode = GET_MODE (op);
3246
3247   if (GET_CODE (op) == CONST_INT)
3248     return (mode == QImode || mode == Pmode || mode == HImode)
3249       && c4x_I_constant (op);
3250
3251   /* We don't like CONST_DOUBLE integers.  */
3252   if (GET_CODE (op) == CONST_DOUBLE)
3253     return c4x_H_constant (op);
3254
3255   /* Disallow symbolic addresses.  Only the predicate
3256      symbolic_address_operand will match these.  */
3257   if (GET_CODE (op) == SYMBOL_REF
3258       || GET_CODE (op) == LABEL_REF
3259       || GET_CODE (op) == CONST)
3260     return 0;
3261
3262   /* If TARGET_LOAD_DIRECT_MEMS is nonzero, disallow direct memory
3263      access to symbolic addresses.  These operands will get forced
3264      into a register and the movqi expander will generate a
3265      HIGH/LO_SUM pair if TARGET_EXPOSE_LDP is nonzero.  */
3266   if (GET_CODE (op) == MEM
3267       && ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
3268            || GET_CODE (XEXP (op, 0)) == LABEL_REF
3269            || GET_CODE (XEXP (op, 0)) == CONST)))
3270     return ! TARGET_LOAD_DIRECT_MEMS && GET_MODE (op) == mode;
3271
3272   return general_operand (op, mode);
3273 }
3274
3275
3276 int
3277 src_hi_operand (rtx op, enum machine_mode mode)
3278 {
3279   if (c4x_O_constant (op))
3280     return 1;
3281   return src_operand (op, mode);
3282 }
3283
3284
3285 /* Check src operand of two operand logical instructions.  */
3286
3287 int
3288 lsrc_operand (rtx op, enum machine_mode mode)
3289 {
3290   if (mode == VOIDmode)
3291     mode = GET_MODE (op);
3292
3293   if (mode != QImode && mode != Pmode)
3294     fatal_insn ("mode not QImode", op);
3295
3296   if (GET_CODE (op) == CONST_INT)
3297     return c4x_L_constant (op) || c4x_J_constant (op);
3298
3299   return src_operand (op, mode);
3300 }
3301
3302
3303 /* Check src operand of two operand tricky instructions.  */
3304
3305 int
3306 tsrc_operand (rtx op, enum machine_mode mode)
3307 {
3308   if (mode == VOIDmode)
3309     mode = GET_MODE (op);
3310
3311   if (mode != QImode && mode != Pmode)
3312     fatal_insn ("mode not QImode", op);
3313
3314   if (GET_CODE (op) == CONST_INT)
3315     return c4x_L_constant (op) || c4x_N_constant (op) || c4x_J_constant (op);
3316
3317   return src_operand (op, mode);
3318 }
3319
3320
3321 /* Check src operand of two operand non immedidate instructions.  */
3322
3323 int
3324 nonimmediate_src_operand (rtx op, enum machine_mode mode)
3325 {
3326   if (GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
3327     return 0;
3328
3329   return src_operand (op, mode);
3330 }
3331
3332
3333 /* Check logical src operand of two operand non immedidate instructions.  */
3334
3335 int
3336 nonimmediate_lsrc_operand (rtx op, enum machine_mode mode)
3337 {
3338   if (GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
3339     return 0;
3340
3341   return lsrc_operand (op, mode);
3342 }
3343
3344
3345 int
3346 reg_or_const_operand (rtx op, enum machine_mode mode)
3347 {
3348   return reg_operand (op, mode) || const_operand (op, mode);
3349 }
3350
3351
3352 /* Check for indirect operands allowable in parallel instruction.  */
3353
3354 int
3355 par_ind_operand (rtx op, enum machine_mode mode)
3356 {
3357   if (mode != VOIDmode && mode != GET_MODE (op))
3358     return 0;
3359
3360   return c4x_S_indirect (op);
3361 }
3362
3363
3364 /* Check for operands allowable in parallel instruction.  */
3365
3366 int
3367 parallel_operand (rtx op, enum machine_mode mode)
3368 {
3369   return ext_low_reg_operand (op, mode) || par_ind_operand (op, mode);
3370 }
3371
3372
3373 static void 
3374 c4x_S_address_parse (rtx op, int *base, int *incdec, int *index, int *disp)
3375 {
3376   *base = 0;
3377   *incdec = 0;
3378   *index = 0;
3379   *disp = 0;
3380        
3381   if (GET_CODE (op) != MEM)
3382     fatal_insn ("invalid indirect memory address", op);
3383   
3384   op = XEXP (op, 0);
3385   switch (GET_CODE (op))
3386     {
3387     case PRE_DEC:
3388       *base = REGNO (XEXP (op, 0));
3389       *incdec = 1;
3390       *disp = -1;
3391       return;
3392
3393     case POST_DEC:
3394       *base = REGNO (XEXP (op, 0));
3395       *incdec = 1;
3396       *disp = 0;
3397       return;
3398
3399     case PRE_INC:
3400       *base = REGNO (XEXP (op, 0));
3401       *incdec = 1;
3402       *disp = 1;
3403       return;
3404
3405     case POST_INC:
3406       *base = REGNO (XEXP (op, 0));
3407       *incdec = 1;
3408       *disp = 0;
3409       return;
3410
3411     case POST_MODIFY:
3412       *base = REGNO (XEXP (op, 0));
3413       if (REG_P (XEXP (XEXP (op, 1), 1)))
3414         {
3415           *index = REGNO (XEXP (XEXP (op, 1), 1));
3416           *disp = 0;            /* ??? */
3417         }
3418       else
3419           *disp = INTVAL (XEXP (XEXP (op, 1), 1));
3420       *incdec = 1;
3421       return;
3422
3423     case PRE_MODIFY:
3424       *base = REGNO (XEXP (op, 0));
3425       if (REG_P (XEXP (XEXP (op, 1), 1)))
3426         {
3427           *index = REGNO (XEXP (XEXP (op, 1), 1));
3428           *disp = 1;            /* ??? */
3429         }
3430       else
3431           *disp = INTVAL (XEXP (XEXP (op, 1), 1));
3432       *incdec = 1;
3433
3434       return;
3435
3436     case REG:
3437       *base = REGNO (op);
3438       return;
3439
3440     case PLUS:
3441       {
3442         rtx op0 = XEXP (op, 0);
3443         rtx op1 = XEXP (op, 1);
3444
3445         if (c4x_a_register (op0))
3446           {
3447             if (c4x_x_register (op1))
3448               {
3449                 *base = REGNO (op0);
3450                 *index = REGNO (op1);
3451                 return;
3452               }
3453             else if ((GET_CODE (op1) == CONST_INT 
3454                       && IS_DISP1_CONST (INTVAL (op1))))
3455               {
3456                 *base = REGNO (op0);
3457                 *disp = INTVAL (op1);
3458                 return;
3459               }
3460           }
3461         else if (c4x_x_register (op0) && c4x_a_register (op1))
3462           {
3463             *base = REGNO (op1);
3464             *index = REGNO (op0);
3465             return;
3466           }
3467       }
3468       /* Fallthrough.  */
3469
3470     default:
3471       fatal_insn ("invalid indirect (S) memory address", op);
3472     }
3473 }
3474
3475
3476 int
3477 c4x_address_conflict (rtx op0, rtx op1, int store0, int store1)
3478 {
3479   int base0;
3480   int base1;
3481   int incdec0;
3482   int incdec1;
3483   int index0;
3484   int index1;
3485   int disp0;
3486   int disp1;
3487   
3488   if (MEM_VOLATILE_P (op0) && MEM_VOLATILE_P (op1))
3489     return 1;
3490
3491   c4x_S_address_parse (op0, &base0, &incdec0, &index0, &disp0);
3492   c4x_S_address_parse (op1, &base1, &incdec1, &index1, &disp1);
3493
3494   if (store0 && store1)
3495     {
3496       /* If we have two stores in parallel to the same address, then
3497          the C4x only executes one of the stores.  This is unlikely to
3498          cause problems except when writing to a hardware device such
3499          as a FIFO since the second write will be lost.  The user
3500          should flag the hardware location as being volatile so that
3501          we don't do this optimization.  While it is unlikely that we
3502          have an aliased address if both locations are not marked
3503          volatile, it is probably safer to flag a potential conflict
3504          if either location is volatile.  */
3505       if (! flag_argument_noalias)
3506         {
3507           if (MEM_VOLATILE_P (op0) || MEM_VOLATILE_P (op1))
3508             return 1;
3509         }
3510     }
3511
3512   /* If have a parallel load and a store to the same address, the load
3513      is performed first, so there is no conflict.  Similarly, there is
3514      no conflict if have parallel loads from the same address.  */
3515
3516   /* Cannot use auto increment or auto decrement twice for same
3517      base register.  */
3518   if (base0 == base1 && incdec0 && incdec0)
3519     return 1;
3520
3521   /* It might be too confusing for GCC if we have use a base register
3522      with a side effect and a memory reference using the same register
3523      in parallel.  */
3524   if (! TARGET_DEVEL && base0 == base1 && (incdec0 || incdec1))
3525     return 1;
3526
3527   /* We can not optimize the case where op1 and op2 refer to the same
3528      address.  */
3529   if (base0 == base1 && disp0 == disp1 && index0 == index1)
3530     return 1;
3531
3532   /* No conflict.  */
3533   return 0;
3534 }
3535
3536
3537 /* Check for while loop inside a decrement and branch loop.  */
3538
3539 int
3540 c4x_label_conflict (rtx insn, rtx jump, rtx db)
3541 {
3542   while (insn)
3543     {
3544       if (GET_CODE (insn) == CODE_LABEL)
3545         {
3546           if (CODE_LABEL_NUMBER (jump) == CODE_LABEL_NUMBER (insn))
3547             return 1;
3548           if (CODE_LABEL_NUMBER (db) == CODE_LABEL_NUMBER (insn))
3549             return 0;
3550         }
3551       insn = PREV_INSN (insn);
3552     }
3553   return 1;
3554 }
3555
3556
3557 /* Validate combination of operands for parallel load/store instructions.  */
3558
3559 int
3560 valid_parallel_load_store (rtx *operands,
3561                            enum machine_mode mode ATTRIBUTE_UNUSED)
3562 {
3563   rtx op0 = operands[0];
3564   rtx op1 = operands[1];
3565   rtx op2 = operands[2];
3566   rtx op3 = operands[3];
3567
3568   if (GET_CODE (op0) == SUBREG)
3569     op0 = SUBREG_REG (op0);
3570   if (GET_CODE (op1) == SUBREG)
3571     op1 = SUBREG_REG (op1);
3572   if (GET_CODE (op2) == SUBREG)
3573     op2 = SUBREG_REG (op2);
3574   if (GET_CODE (op3) == SUBREG)
3575     op3 = SUBREG_REG (op3);
3576
3577   /* The patterns should only allow ext_low_reg_operand() or
3578      par_ind_operand() operands.  Thus of the 4 operands, only 2
3579      should be REGs and the other 2 should be MEMs.  */
3580
3581   /* This test prevents the multipack pass from using this pattern if
3582      op0 is used as an index or base register in op2 or op3, since
3583      this combination will require reloading.  */
3584   if (GET_CODE (op0) == REG
3585       && ((GET_CODE (op2) == MEM && reg_mentioned_p (op0, XEXP (op2, 0)))
3586           || (GET_CODE (op3) == MEM && reg_mentioned_p (op0, XEXP (op3, 0)))))
3587     return 0;
3588
3589   /* LDI||LDI.  */
3590   if (GET_CODE (op0) == REG && GET_CODE (op2) == REG)
3591     return (REGNO (op0) != REGNO (op2))
3592       && GET_CODE (op1) == MEM && GET_CODE (op3) == MEM
3593       && ! c4x_address_conflict (op1, op3, 0, 0);
3594
3595   /* STI||STI.  */
3596   if (GET_CODE (op1) == REG && GET_CODE (op3) == REG)
3597     return GET_CODE (op0) == MEM && GET_CODE (op2) == MEM
3598       && ! c4x_address_conflict (op0, op2, 1, 1);
3599
3600   /* LDI||STI.  */
3601   if (GET_CODE (op0) == REG && GET_CODE (op3) == REG)
3602     return GET_CODE (op1) == MEM && GET_CODE (op2) == MEM
3603       && ! c4x_address_conflict (op1, op2, 0, 1);
3604
3605   /* STI||LDI.  */
3606   if (GET_CODE (op1) == REG && GET_CODE (op2) == REG)
3607     return GET_CODE (op0) == MEM && GET_CODE (op3) == MEM
3608       && ! c4x_address_conflict (op0, op3, 1, 0);
3609
3610   return 0;
3611 }
3612
3613
3614 int
3615 valid_parallel_operands_4 (rtx *operands,
3616                            enum machine_mode mode ATTRIBUTE_UNUSED)
3617 {
3618   rtx op0 = operands[0];
3619   rtx op2 = operands[2];
3620
3621   if (GET_CODE (op0) == SUBREG)
3622     op0 = SUBREG_REG (op0);
3623   if (GET_CODE (op2) == SUBREG)
3624     op2 = SUBREG_REG (op2);
3625
3626   /* This test prevents the multipack pass from using this pattern if
3627      op0 is used as an index or base register in op2, since this combination
3628      will require reloading.  */
3629   if (GET_CODE (op0) == REG
3630       && GET_CODE (op2) == MEM
3631       && reg_mentioned_p (op0, XEXP (op2, 0)))
3632     return 0;
3633
3634   return 1;
3635 }
3636
3637
3638 int
3639 valid_parallel_operands_5 (rtx *operands,
3640                            enum machine_mode mode ATTRIBUTE_UNUSED)
3641 {
3642   int regs = 0;
3643   rtx op0 = operands[0];
3644   rtx op1 = operands[1];
3645   rtx op2 = operands[2];
3646   rtx op3 = operands[3];
3647
3648   if (GET_CODE (op0) == SUBREG)
3649     op0 = SUBREG_REG (op0);
3650   if (GET_CODE (op1) == SUBREG)
3651     op1 = SUBREG_REG (op1);
3652   if (GET_CODE (op2) == SUBREG)
3653     op2 = SUBREG_REG (op2);
3654
3655   /* The patterns should only allow ext_low_reg_operand() or
3656      par_ind_operand() operands.  Operands 1 and 2 may be commutative
3657      but only one of them can be a register.  */
3658   if (GET_CODE (op1) == REG)
3659     regs++;
3660   if (GET_CODE (op2) == REG)
3661     regs++;
3662
3663   if (regs != 1)
3664     return 0;
3665
3666   /* This test prevents the multipack pass from using this pattern if
3667      op0 is used as an index or base register in op3, since this combination
3668      will require reloading.  */
3669   if (GET_CODE (op0) == REG
3670       && GET_CODE (op3) == MEM
3671       && reg_mentioned_p (op0, XEXP (op3, 0)))
3672     return 0;
3673
3674   return 1;
3675 }
3676
3677
3678 int
3679 valid_parallel_operands_6 (rtx *operands,
3680                            enum machine_mode mode ATTRIBUTE_UNUSED)
3681 {
3682   int regs = 0;
3683   rtx op0 = operands[0];
3684   rtx op1 = operands[1];
3685   rtx op2 = operands[2];
3686   rtx op4 = operands[4];
3687   rtx op5 = operands[5];
3688
3689   if (GET_CODE (op1) == SUBREG)
3690     op1 = SUBREG_REG (op1);
3691   if (GET_CODE (op2) == SUBREG)
3692     op2 = SUBREG_REG (op2);
3693   if (GET_CODE (op4) == SUBREG)
3694     op4 = SUBREG_REG (op4);
3695   if (GET_CODE (op5) == SUBREG)
3696     op5 = SUBREG_REG (op5);
3697
3698   /* The patterns should only allow ext_low_reg_operand() or
3699      par_ind_operand() operands.  Thus of the 4 input operands, only 2
3700      should be REGs and the other 2 should be MEMs.  */
3701
3702   if (GET_CODE (op1) == REG)
3703     regs++;
3704   if (GET_CODE (op2) == REG)
3705     regs++;
3706   if (GET_CODE (op4) == REG)
3707     regs++;
3708   if (GET_CODE (op5) == REG)
3709     regs++;
3710
3711   /* The new C30/C40 silicon dies allow 3 regs of the 4 input operands. 
3712      Perhaps we should count the MEMs as well?  */
3713   if (regs != 2)
3714     return 0;
3715
3716   /* This test prevents the multipack pass from using this pattern if
3717      op0 is used as an index or base register in op4 or op5, since
3718      this combination will require reloading.  */
3719   if (GET_CODE (op0) == REG
3720       && ((GET_CODE (op4) == MEM && reg_mentioned_p (op0, XEXP (op4, 0)))
3721           || (GET_CODE (op5) == MEM && reg_mentioned_p (op0, XEXP (op5, 0)))))
3722     return 0;
3723
3724   return 1;
3725 }
3726
3727
3728 /* Validate combination of src operands.  Note that the operands have
3729    been screened by the src_operand predicate.  We just have to check
3730    that the combination of operands is valid.  If FORCE is set, ensure
3731    that the destination regno is valid if we have a 2 operand insn.  */
3732
3733 static int
3734 c4x_valid_operands (enum rtx_code code, rtx *operands,
3735                     enum machine_mode mode ATTRIBUTE_UNUSED,
3736                     int force)
3737 {
3738   rtx op0;
3739   rtx op1;
3740   rtx op2;
3741   enum rtx_code code1;
3742   enum rtx_code code2;
3743
3744
3745   /* FIXME, why can't we tighten the operands for IF_THEN_ELSE?  */
3746   if (code == IF_THEN_ELSE)
3747       return 1 || (operands[0] == operands[2] || operands[0] == operands[3]);
3748
3749   if (code == COMPARE)
3750     {
3751       op1 = operands[0];
3752       op2 = operands[1];
3753     }
3754   else
3755     {
3756       op1 = operands[1];
3757       op2 = operands[2];
3758     }
3759
3760   op0 = operands[0];
3761
3762   if (GET_CODE (op0) == SUBREG)
3763     op0 = SUBREG_REG (op0);
3764   if (GET_CODE (op1) == SUBREG)
3765     op1 = SUBREG_REG (op1);
3766   if (GET_CODE (op2) == SUBREG)
3767     op2 = SUBREG_REG (op2);
3768
3769   code1 = GET_CODE (op1);
3770   code2 = GET_CODE (op2);
3771
3772   
3773   if (code1 == REG && code2 == REG)
3774     return 1;
3775
3776   if (code1 == MEM && code2 == MEM)
3777     {
3778       if (c4x_S_indirect (op1) && c4x_S_indirect (op2))
3779         return 1;
3780       return c4x_R_indirect (op1) && c4x_R_indirect (op2);
3781     }
3782
3783   /* We cannot handle two MEMs or two CONSTS, etc.  */
3784   if (code1 == code2)
3785     return 0;
3786
3787   if (code1 == REG)
3788     {
3789       switch (code2)
3790         {
3791         case CONST_INT:
3792           if (c4x_J_constant (op2) && c4x_R_indirect (op1))
3793             return 1;
3794           break;
3795           
3796         case CONST_DOUBLE:
3797           if (! c4x_H_constant (op2))
3798             return 0;
3799           break;
3800
3801           /* Any valid memory operand screened by src_operand is OK.  */
3802         case MEM:
3803           break;
3804           
3805           /* After CSE, any remaining (ADDRESSOF:P reg) gets converted
3806              into a stack slot memory address comprising a PLUS and a
3807              constant.  */
3808         case ADDRESSOF:
3809           break;
3810           
3811         default:
3812           fatal_insn ("c4x_valid_operands: Internal error", op2);
3813           break;
3814         }
3815       
3816       if (GET_CODE (op0) == SCRATCH)
3817           return 1;
3818
3819       if (!REG_P (op0))
3820           return 0;
3821
3822       /* Check that we have a valid destination register for a two operand
3823          instruction.  */
3824       return ! force || code == COMPARE || REGNO (op1) == REGNO (op0);
3825     }
3826
3827
3828   /* Check non-commutative operators.  */
3829   if (code == ASHIFTRT || code == LSHIFTRT
3830       || code == ASHIFT || code == COMPARE)
3831     return code2 == REG
3832       && (c4x_S_indirect (op1) || c4x_R_indirect (op1));
3833
3834
3835   /* Assume MINUS is commutative since the subtract patterns
3836      also support the reverse subtract instructions.  Since op1
3837      is not a register, and op2 is a register, op1 can only
3838      be a restricted memory operand for a shift instruction.  */
3839   if (code2 == REG)
3840     {
3841       switch (code1)
3842         {
3843         case CONST_INT:
3844           break;
3845       
3846         case CONST_DOUBLE:
3847           if (! c4x_H_constant (op1))
3848             return 0;
3849           break;
3850
3851           /* Any valid memory operand screened by src_operand is OK.  */      
3852         case MEM:
3853           break;
3854           
3855           /* After CSE, any remaining (ADDRESSOF:P reg) gets converted
3856              into a stack slot memory address comprising a PLUS and a
3857              constant.  */
3858         case ADDRESSOF:
3859           break;
3860           
3861         default:
3862           abort ();
3863           break;
3864         }
3865
3866       if (GET_CODE (op0) == SCRATCH)
3867           return 1;
3868
3869       if (!REG_P (op0))
3870           return 0;
3871
3872       /* Check that we have a valid destination register for a two operand
3873          instruction.  */
3874       return ! force || REGNO (op1) == REGNO (op0);
3875     }
3876       
3877   if (c4x_J_constant (op1) && c4x_R_indirect (op2))
3878     return 1;
3879
3880   return 0;
3881 }
3882
3883
3884 int valid_operands (enum rtx_code code, rtx *operands, enum machine_mode mode)
3885 {
3886
3887   /* If we are not optimizing then we have to let anything go and let
3888      reload fix things up.  instantiate_decl in function.c can produce
3889      invalid insns by changing the offset of a memory operand from a
3890      valid one into an invalid one, when the second operand is also a
3891      memory operand.  The alternative is not to allow two memory
3892      operands for an insn when not optimizing.  The problem only rarely
3893      occurs, for example with the C-torture program DFcmp.c.  */
3894
3895   return ! optimize || c4x_valid_operands (code, operands, mode, 0);
3896 }
3897
3898
3899 int
3900 legitimize_operands (enum rtx_code code, rtx *operands, enum machine_mode mode)
3901 {
3902   /* Compare only has 2 operands.  */
3903   if (code == COMPARE)
3904     {
3905       /* During RTL generation, force constants into pseudos so that
3906          they can get hoisted out of loops.  This will tie up an extra
3907          register but can save an extra cycle.  Only do this if loop
3908          optimization enabled.  (We cannot pull this trick for add and
3909          sub instructions since the flow pass won't find
3910          autoincrements etc.)  This allows us to generate compare
3911          instructions like CMPI R0, *AR0++ where R0 = 42, say, instead
3912          of LDI *AR0++, R0; CMPI 42, R0. 
3913
3914          Note that expand_binops will try to load an expensive constant
3915          into a register if it is used within a loop.  Unfortunately,
3916          the cost mechanism doesn't allow us to look at the other
3917          operand to decide whether the constant is expensive.  */
3918       
3919       if (! reload_in_progress
3920           && TARGET_HOIST
3921           && optimize > 0
3922           && GET_CODE (operands[1]) == CONST_INT 
3923           && preserve_subexpressions_p ()
3924           && rtx_cost (operands[1], code) > 1)
3925         operands[1] = force_reg (mode, operands[1]);
3926       
3927       if (! reload_in_progress
3928           && ! c4x_valid_operands (code, operands, mode, 0))
3929         operands[0] = force_reg (mode, operands[0]);
3930       return 1;
3931     }
3932   
3933   /* We cannot do this for ADDI/SUBI insns since we will
3934      defeat the flow pass from finding autoincrement addressing
3935      opportunities.  */
3936   if (! reload_in_progress
3937       && ! ((code == PLUS || code == MINUS) && mode == Pmode)
3938       && TARGET_HOIST
3939       && optimize > 1
3940       && GET_CODE (operands[2]) == CONST_INT
3941       && preserve_subexpressions_p ()
3942       && rtx_cost (operands[2], code) > 1)
3943     operands[2] = force_reg (mode, operands[2]);
3944
3945   /* We can get better code on a C30 if we force constant shift counts
3946      into a register.  This way they can get hoisted out of loops,
3947      tying up a register, but saving an instruction.  The downside is
3948      that they may get allocated to an address or index register, and
3949      thus we will get a pipeline conflict if there is a nearby
3950      indirect address using an address register. 
3951
3952      Note that expand_binops will not try to load an expensive constant
3953      into a register if it is used within a loop for a shift insn.  */
3954   
3955   if (! reload_in_progress
3956       && ! c4x_valid_operands (code, operands, mode, TARGET_FORCE))
3957     {
3958       /* If the operand combination is invalid, we force operand1 into a
3959          register, preventing reload from having doing to do this at a
3960          later stage.  */
3961       operands[1] = force_reg (mode, operands[1]);
3962       if (TARGET_FORCE)
3963         {
3964           emit_move_insn (operands[0], operands[1]);
3965           operands[1] = copy_rtx (operands[0]);
3966         }
3967       else
3968         {
3969           /* Just in case...  */
3970           if (! c4x_valid_operands (code, operands, mode, 0))
3971             operands[2] = force_reg (mode, operands[2]);
3972         }
3973     }
3974
3975   /* Right shifts require a negative shift count, but GCC expects
3976      a positive count, so we emit a NEG.  */
3977   if ((code == ASHIFTRT || code == LSHIFTRT)
3978       && (GET_CODE (operands[2]) != CONST_INT))
3979     operands[2] = gen_rtx_NEG (mode, negate_rtx (mode, operands[2]));
3980   
3981   return 1;
3982 }
3983
3984
3985 /* The following predicates are used for instruction scheduling.  */
3986
3987 int
3988 group1_reg_operand (rtx op, enum machine_mode mode)
3989 {
3990   if (mode != VOIDmode && mode != GET_MODE (op))
3991     return 0;
3992   if (GET_CODE (op) == SUBREG)
3993     op = SUBREG_REG (op);
3994   return REG_P (op) && (! reload_completed || IS_GROUP1_REG (op));
3995 }
3996
3997
3998 int
3999 group1_mem_operand (rtx op, enum machine_mode mode)
4000 {
4001   if (mode != VOIDmode && mode != GET_MODE (op))
4002     return 0;
4003
4004   if (GET_CODE (op) == MEM)
4005     {
4006       op = XEXP (op, 0);
4007       if (GET_CODE (op) == PLUS)
4008         {
4009           rtx op0 = XEXP (op, 0);
4010           rtx op1 = XEXP (op, 1);
4011
4012           if ((REG_P (op0) && (! reload_completed || IS_GROUP1_REG (op0)))
4013               || (REG_P (op1) && (! reload_completed || IS_GROUP1_REG (op1))))
4014             return 1;
4015         }
4016       else if ((REG_P (op)) && (! reload_completed || IS_GROUP1_REG (op)))
4017         return 1;
4018     }
4019
4020   return 0;
4021 }
4022
4023
4024 /* Return true if any one of the address registers.  */
4025
4026 int
4027 arx_reg_operand (rtx op, enum machine_mode mode)
4028 {
4029   if (mode != VOIDmode && mode != GET_MODE (op))
4030     return 0;
4031   if (GET_CODE (op) == SUBREG)
4032     op = SUBREG_REG (op);
4033   return REG_P (op) && (! reload_completed || IS_ADDR_REG (op));
4034 }
4035
4036
4037 static int
4038 c4x_arn_reg_operand (rtx op, enum machine_mode mode, unsigned int regno)
4039 {
4040   if (mode != VOIDmode && mode != GET_MODE (op))
4041     return 0;
4042   if (GET_CODE (op) == SUBREG)
4043     op = SUBREG_REG (op);
4044   return REG_P (op) && (! reload_completed || (REGNO (op) == regno));
4045 }
4046
4047
4048 static int
4049 c4x_arn_mem_operand (rtx op, enum machine_mode mode, unsigned int regno)
4050 {
4051   if (mode != VOIDmode && mode != GET_MODE (op))
4052     return 0;
4053
4054   if (GET_CODE (op) == MEM)
4055     {
4056       op = XEXP (op, 0);
4057       switch (GET_CODE (op))
4058         {
4059         case PRE_DEC:
4060         case POST_DEC:
4061         case PRE_INC:
4062         case POST_INC:
4063           op = XEXP (op, 0);
4064
4065         case REG:
4066           return REG_P (op) && (! reload_completed || (REGNO (op) == regno));
4067
4068         case PRE_MODIFY:
4069         case POST_MODIFY:
4070           if (REG_P (XEXP (op, 0)) && (! reload_completed 
4071                                        || (REGNO (XEXP (op, 0)) == regno)))
4072             return 1;
4073           if (REG_P (XEXP (XEXP (op, 1), 1))
4074               && (! reload_completed
4075                   || (REGNO (XEXP (XEXP (op, 1), 1)) == regno)))
4076             return 1;
4077           break;
4078
4079         case PLUS:
4080           {
4081             rtx op0 = XEXP (op, 0);
4082             rtx op1 = XEXP (op, 1);
4083
4084             if ((REG_P (op0) && (! reload_completed
4085                                  || (REGNO (op0) == regno)))
4086                 || (REG_P (op1) && (! reload_completed
4087                                     || (REGNO (op1) == regno))))
4088               return 1;
4089           }
4090           break;
4091
4092         default:
4093           break;
4094         }
4095     }
4096   return 0;
4097 }
4098
4099
4100 int
4101 ar0_reg_operand (rtx op, enum machine_mode mode)
4102 {
4103   return c4x_arn_reg_operand (op, mode, AR0_REGNO);
4104 }
4105
4106
4107 int
4108 ar0_mem_operand (rtx op, enum machine_mode mode)
4109 {
4110   return c4x_arn_mem_operand (op, mode, AR0_REGNO);
4111 }
4112
4113
4114 int
4115 ar1_reg_operand (rtx op, enum machine_mode mode)
4116 {
4117   return c4x_arn_reg_operand (op, mode, AR1_REGNO);
4118 }
4119
4120
4121 int
4122 ar1_mem_operand (rtx op, enum machine_mode mode)
4123 {
4124   return c4x_arn_mem_operand (op, mode, AR1_REGNO);
4125 }
4126
4127
4128 int
4129 ar2_reg_operand (rtx op, enum machine_mode mode)
4130 {
4131   return c4x_arn_reg_operand (op, mode, AR2_REGNO);
4132 }
4133
4134
4135 int
4136 ar2_mem_operand (rtx op, enum machine_mode mode)
4137 {
4138   return c4x_arn_mem_operand (op, mode, AR2_REGNO);
4139 }
4140
4141
4142 int
4143 ar3_reg_operand (rtx op, enum machine_mode mode)
4144 {
4145   return c4x_arn_reg_operand (op, mode, AR3_REGNO);
4146 }
4147
4148
4149 int
4150 ar3_mem_operand (rtx op, enum machine_mode mode)
4151 {
4152   return c4x_arn_mem_operand (op, mode, AR3_REGNO);
4153 }
4154
4155
4156 int
4157 ar4_reg_operand (rtx op, enum machine_mode mode)
4158 {
4159   return c4x_arn_reg_operand (op, mode, AR4_REGNO);
4160 }
4161
4162
4163 int
4164 ar4_mem_operand (rtx op, enum machine_mode mode)
4165 {
4166   return c4x_arn_mem_operand (op, mode, AR4_REGNO);
4167 }
4168
4169
4170 int
4171 ar5_reg_operand (rtx op, enum machine_mode mode)
4172 {
4173   return c4x_arn_reg_operand (op, mode, AR5_REGNO);
4174 }
4175
4176
4177 int
4178 ar5_mem_operand (rtx op, enum machine_mode mode)
4179 {
4180   return c4x_arn_mem_operand (op, mode, AR5_REGNO);
4181 }
4182
4183
4184 int
4185 ar6_reg_operand (rtx op, enum machine_mode mode)
4186 {
4187   return c4x_arn_reg_operand (op, mode, AR6_REGNO);
4188 }
4189
4190
4191 int
4192 ar6_mem_operand (rtx op, enum machine_mode mode)
4193 {
4194   return c4x_arn_mem_operand (op, mode, AR6_REGNO);
4195 }
4196
4197
4198 int
4199 ar7_reg_operand (rtx op, enum machine_mode mode)
4200 {
4201   return c4x_arn_reg_operand (op, mode, AR7_REGNO);
4202 }
4203
4204
4205 int
4206 ar7_mem_operand (rtx op, enum machine_mode mode)
4207 {
4208   return c4x_arn_mem_operand (op, mode, AR7_REGNO);
4209 }
4210
4211
4212 int
4213 ir0_reg_operand (rtx op, enum machine_mode mode)
4214 {
4215   return c4x_arn_reg_operand (op, mode, IR0_REGNO);
4216 }
4217
4218
4219 int
4220 ir0_mem_operand (rtx op, enum machine_mode mode)
4221 {
4222   return c4x_arn_mem_operand (op, mode, IR0_REGNO);
4223 }
4224
4225
4226 int
4227 ir1_reg_operand (rtx op, enum machine_mode mode)
4228 {
4229   return c4x_arn_reg_operand (op, mode, IR1_REGNO);
4230 }
4231
4232
4233 int
4234 ir1_mem_operand (rtx op, enum machine_mode mode)
4235 {
4236   return c4x_arn_mem_operand (op, mode, IR1_REGNO);
4237 }
4238
4239
4240 /* This is similar to operand_subword but allows autoincrement
4241    addressing.  */
4242
4243 rtx
4244 c4x_operand_subword (rtx op, int i, int validate_address,
4245                      enum machine_mode  mode)
4246 {
4247   if (mode != HImode && mode != HFmode)
4248     fatal_insn ("c4x_operand_subword: invalid mode", op);
4249
4250   if (mode == HFmode && REG_P (op))
4251     fatal_insn ("c4x_operand_subword: invalid operand", op);
4252
4253   if (GET_CODE (op) == MEM)
4254     {
4255       enum rtx_code code = GET_CODE (XEXP (op, 0));
4256       enum machine_mode mode = GET_MODE (XEXP (op, 0));
4257       enum machine_mode submode;
4258
4259       submode = mode;
4260       if (mode == HImode)
4261         submode = QImode;
4262       else if (mode == HFmode)
4263         submode = QFmode;
4264
4265       switch (code)
4266         {
4267         case POST_INC:
4268         case PRE_INC:
4269           return gen_rtx_MEM (submode, XEXP (op, 0));
4270           
4271         case POST_DEC:
4272         case PRE_DEC:
4273         case PRE_MODIFY:
4274         case POST_MODIFY:
4275           /* We could handle these with some difficulty.
4276              e.g., *p-- => *(p-=2); *(p+1).  */
4277           fatal_insn ("c4x_operand_subword: invalid autoincrement", op);
4278
4279         case SYMBOL_REF:
4280         case LABEL_REF:
4281         case CONST:
4282         case CONST_INT:
4283           fatal_insn ("c4x_operand_subword: invalid address", op);
4284
4285           /* Even though offsettable_address_p considers (MEM
4286              (LO_SUM)) to be offsettable, it is not safe if the
4287              address is at the end of the data page since we also have
4288              to fix up the associated high PART.  In this case where
4289              we are trying to split a HImode or HFmode memory
4290              reference, we would have to emit another insn to reload a
4291              new HIGH value.  It's easier to disable LO_SUM memory references
4292              in HImode or HFmode and we probably get better code.  */
4293         case LO_SUM:
4294           fatal_insn ("c4x_operand_subword: address not offsettable", op);
4295   
4296         default:
4297           break;
4298         }
4299     }
4300   
4301   return operand_subword (op, i, validate_address, mode);
4302 }
4303
4304 struct name_list
4305 {
4306   struct name_list *next;
4307   const char *name;
4308 };
4309
4310 static struct name_list *global_head;
4311 static struct name_list *extern_head;
4312
4313
4314 /* Add NAME to list of global symbols and remove from external list if
4315    present on external list.  */
4316
4317 void
4318 c4x_global_label (const char *name)
4319 {
4320   struct name_list *p, *last;
4321
4322   /* Do not insert duplicate names, so linearly search through list of
4323      existing names.  */
4324   p = global_head;
4325   while (p)
4326     {
4327       if (strcmp (p->name, name) == 0)
4328         return;
4329       p = p->next;
4330     }
4331   p = (struct name_list *) xmalloc (sizeof *p);
4332   p->next = global_head;
4333   p->name = name;
4334   global_head = p;
4335
4336   /* Remove this name from ref list if present.  */
4337   last = NULL;
4338   p = extern_head;
4339   while (p)
4340     {
4341       if (strcmp (p->name, name) == 0)
4342         {
4343           if (last)
4344             last->next = p->next;
4345           else
4346             extern_head = p->next;
4347           break;
4348         }
4349       last = p;
4350       p = p->next;
4351     }
4352 }
4353
4354
4355 /* Add NAME to list of external symbols.  */
4356
4357 void
4358 c4x_external_ref (const char *name)
4359 {
4360   struct name_list *p;
4361
4362   /* Do not insert duplicate names.  */
4363   p = extern_head;
4364   while (p)
4365     {
4366       if (strcmp (p->name, name) == 0)
4367         return;
4368       p = p->next;
4369     }
4370   
4371   /* Do not insert ref if global found.  */
4372   p = global_head;
4373   while (p)
4374     {
4375       if (strcmp (p->name, name) == 0)
4376         return;
4377       p = p->next;
4378     }
4379   p = (struct name_list *) xmalloc (sizeof *p);
4380   p->next = extern_head;
4381   p->name = name;
4382   extern_head = p;
4383 }
4384
4385 /* We need to have a data section we can identify so that we can set
4386    the DP register back to a data pointer in the small memory model.
4387    This is only required for ISRs if we are paranoid that someone
4388    may have quietly changed this register on the sly.  */
4389 static void
4390 c4x_file_start (void)
4391 {
4392   int dspversion = 0;
4393   if (TARGET_C30) dspversion = 30;
4394   if (TARGET_C31) dspversion = 31;
4395   if (TARGET_C32) dspversion = 32;
4396   if (TARGET_C33) dspversion = 33;
4397   if (TARGET_C40) dspversion = 40;
4398   if (TARGET_C44) dspversion = 44;
4399
4400   default_file_start ();
4401   fprintf (asm_out_file, "\t.version\t%d\n", dspversion);
4402   fputs ("\n\t.data\ndata_sec:\n", asm_out_file);
4403 }
4404
4405
4406 static void
4407 c4x_file_end (void)
4408 {
4409   struct name_list *p;
4410   
4411   /* Output all external names that are not global.  */
4412   p = extern_head;
4413   while (p)
4414     {
4415       fprintf (asm_out_file, "\t.ref\t");
4416       assemble_name (asm_out_file, p->name);
4417       fprintf (asm_out_file, "\n");
4418       p = p->next;
4419     }
4420   fprintf (asm_out_file, "\t.end\n");
4421 }
4422
4423
4424 static void
4425 c4x_check_attribute (const char *attrib, tree list, tree decl, tree *attributes)
4426 {
4427   while (list != NULL_TREE
4428          && IDENTIFIER_POINTER (TREE_PURPOSE (list))
4429          != IDENTIFIER_POINTER (DECL_NAME (decl)))
4430     list = TREE_CHAIN (list);
4431   if (list)
4432     *attributes = tree_cons (get_identifier (attrib), TREE_VALUE (list),
4433                              *attributes);
4434 }
4435
4436
4437 static void
4438 c4x_insert_attributes (tree decl, tree *attributes)
4439 {
4440   switch (TREE_CODE (decl))
4441     {
4442     case FUNCTION_DECL:
4443       c4x_check_attribute ("section", code_tree, decl, attributes);
4444       c4x_check_attribute ("const", pure_tree, decl, attributes);
4445       c4x_check_attribute ("noreturn", noreturn_tree, decl, attributes);
4446       c4x_check_attribute ("interrupt", interrupt_tree, decl, attributes);
4447       c4x_check_attribute ("naked", naked_tree, decl, attributes);
4448       break;
4449
4450     case VAR_DECL:
4451       c4x_check_attribute ("section", data_tree, decl, attributes);
4452       break;
4453
4454     default:
4455       break;
4456     }
4457 }
4458
4459 /* Table of valid machine attributes.  */
4460 const struct attribute_spec c4x_attribute_table[] =
4461 {
4462   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
4463   { "interrupt",    0, 0, false, true,  true,  c4x_handle_fntype_attribute },
4464   { "naked",    0, 0, false, true,  true,  c4x_handle_fntype_attribute },
4465   { "leaf_pretend", 0, 0, false, true,  true,  c4x_handle_fntype_attribute },
4466   { NULL,           0, 0, false, false, false, NULL }
4467 };
4468
4469 /* Handle an attribute requiring a FUNCTION_TYPE;
4470    arguments as in struct attribute_spec.handler.  */
4471 static tree
4472 c4x_handle_fntype_attribute (tree *node, tree name,
4473                              tree args ATTRIBUTE_UNUSED,
4474                              int flags ATTRIBUTE_UNUSED,
4475                              bool *no_add_attrs)
4476 {
4477   if (TREE_CODE (*node) != FUNCTION_TYPE)
4478     {
4479       warning ("`%s' attribute only applies to functions",
4480                IDENTIFIER_POINTER (name));
4481       *no_add_attrs = true;
4482     }
4483
4484   return NULL_TREE;
4485 }
4486
4487
4488 /* !!! FIXME to emit RPTS correctly.  */
4489
4490 int
4491 c4x_rptb_rpts_p (rtx insn, rtx op)
4492 {
4493   /* The next insn should be our label marking where the
4494      repeat block starts.  */
4495   insn = NEXT_INSN (insn);
4496   if (GET_CODE (insn) != CODE_LABEL)
4497     {
4498       /* Some insns may have been shifted between the RPTB insn
4499          and the top label... They were probably destined to
4500          be moved out of the loop.  For now, let's leave them
4501          where they are and print a warning.  We should
4502          probably move these insns before the repeat block insn.  */
4503       if (TARGET_DEBUG)
4504         fatal_insn("c4x_rptb_rpts_p: Repeat block top label moved\n",
4505                    insn);
4506       return 0;
4507     }
4508
4509   /* Skip any notes.  */
4510   insn = next_nonnote_insn (insn);
4511
4512   /* This should be our first insn in the loop.  */
4513   if (! INSN_P (insn))
4514     return 0;
4515
4516   /* Skip any notes.  */
4517   insn = next_nonnote_insn (insn);
4518
4519   if (! INSN_P (insn))
4520     return 0;
4521
4522   if (recog_memoized (insn) != CODE_FOR_rptb_end)
4523     return 0;
4524
4525   if (TARGET_RPTS)
4526     return 1;
4527
4528   return (GET_CODE (op) == CONST_INT) && TARGET_RPTS_CYCLES (INTVAL (op));
4529 }
4530
4531
4532 /* Check if register r11 is used as the destination of an insn.  */
4533
4534 static int
4535 c4x_r11_set_p(rtx x)
4536 {
4537   rtx set;
4538   int i, j;
4539   const char *fmt;
4540
4541   if (x == 0)
4542     return 0;
4543
4544   if (INSN_P (x) && GET_CODE (PATTERN (x)) == SEQUENCE)
4545     x = XVECEXP (PATTERN (x), 0, XVECLEN (PATTERN (x), 0) - 1);
4546
4547   if (INSN_P (x) && (set = single_set (x)))
4548     x = SET_DEST (set);
4549
4550   if (GET_CODE (x) == REG && REGNO (x) == R11_REGNO)
4551     return 1;
4552
4553   fmt = GET_RTX_FORMAT (GET_CODE (x));
4554   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4555     {
4556       if (fmt[i] == 'e')
4557         {
4558           if (c4x_r11_set_p (XEXP (x, i)))
4559             return 1;
4560         }
4561       else if (fmt[i] == 'E')
4562         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4563           if (c4x_r11_set_p (XVECEXP (x, i, j)))
4564             return 1;
4565     }
4566   return 0;
4567 }
4568
4569
4570 /* The c4x sometimes has a problem when the insn before the laj insn
4571    sets the r11 register.  Check for this situation.  */
4572
4573 int
4574 c4x_check_laj_p (rtx insn)
4575 {
4576   insn = prev_nonnote_insn (insn);
4577
4578   /* If this is the start of the function no nop is needed.  */
4579   if (insn == 0)
4580     return 0;
4581
4582   /* If the previous insn is a code label we have to insert a nop. This
4583      could be a jump or table jump. We can find the normal jumps by
4584      scanning the function but this will not find table jumps.  */
4585   if (GET_CODE (insn) == CODE_LABEL)
4586     return 1;
4587
4588   /* If the previous insn sets register r11 we have to insert a nop.  */
4589   if (c4x_r11_set_p (insn))
4590     return 1;
4591
4592   /* No nop needed.  */
4593   return 0;
4594 }
4595
4596
4597 /* Adjust the cost of a scheduling dependency.  Return the new cost of
4598    a dependency LINK or INSN on DEP_INSN.  COST is the current cost. 
4599    A set of an address register followed by a use occurs a 2 cycle
4600    stall (reduced to a single cycle on the c40 using LDA), while
4601    a read of an address register followed by a use occurs a single cycle.  */
4602
4603 #define SET_USE_COST    3
4604 #define SETLDA_USE_COST 2
4605 #define READ_USE_COST   2
4606
4607 static int
4608 c4x_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
4609 {
4610   /* Don't worry about this until we know what registers have been
4611      assigned.  */
4612   if (flag_schedule_insns == 0 && ! reload_completed)
4613     return 0;
4614
4615   /* How do we handle dependencies where a read followed by another
4616      read causes a pipeline stall?  For example, a read of ar0 followed
4617      by the use of ar0 for a memory reference.  It looks like we
4618      need to extend the scheduler to handle this case.  */
4619
4620   /* Reload sometimes generates a CLOBBER of a stack slot, e.g.,
4621      (clobber (mem:QI (plus:QI (reg:QI 11 ar3) (const_int 261)))),
4622      so only deal with insns we know about.  */
4623   if (recog_memoized (dep_insn) < 0)
4624     return 0;
4625
4626   if (REG_NOTE_KIND (link) == 0)
4627     {
4628       int max = 0;
4629
4630       /* Data dependency; DEP_INSN writes a register that INSN reads some
4631          cycles later.  */
4632       if (TARGET_C3X)
4633         {
4634           if (get_attr_setgroup1 (dep_insn) && get_attr_usegroup1 (insn))
4635             max = SET_USE_COST > max ? SET_USE_COST : max;
4636           if (get_attr_readarx (dep_insn) && get_attr_usegroup1 (insn))
4637             max = READ_USE_COST > max ? READ_USE_COST : max;
4638         }
4639       else
4640         {
4641           /* This could be significantly optimized. We should look
4642              to see if dep_insn sets ar0-ar7 or ir0-ir1 and if
4643              insn uses ar0-ar7.  We then test if the same register
4644              is used.  The tricky bit is that some operands will
4645              use several registers...  */
4646           if (get_attr_setar0 (dep_insn) && get_attr_usear0 (insn))
4647             max = SET_USE_COST > max ? SET_USE_COST : max;
4648           if (get_attr_setlda_ar0 (dep_insn) && get_attr_usear0 (insn))
4649             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4650           if (get_attr_readar0 (dep_insn) && get_attr_usear0 (insn))
4651             max = READ_USE_COST > max ? READ_USE_COST : max;
4652
4653           if (get_attr_setar1 (dep_insn) && get_attr_usear1 (insn))
4654             max = SET_USE_COST > max ? SET_USE_COST : max;
4655           if (get_attr_setlda_ar1 (dep_insn) && get_attr_usear1 (insn))
4656             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4657           if (get_attr_readar1 (dep_insn) && get_attr_usear1 (insn))
4658             max = READ_USE_COST > max ? READ_USE_COST : max;
4659
4660           if (get_attr_setar2 (dep_insn) && get_attr_usear2 (insn))
4661             max = SET_USE_COST > max ? SET_USE_COST : max;
4662           if (get_attr_setlda_ar2 (dep_insn) && get_attr_usear2 (insn))
4663             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4664           if (get_attr_readar2 (dep_insn) && get_attr_usear2 (insn))
4665             max = READ_USE_COST > max ? READ_USE_COST : max;
4666
4667           if (get_attr_setar3 (dep_insn) && get_attr_usear3 (insn))
4668             max = SET_USE_COST > max ? SET_USE_COST : max;
4669           if (get_attr_setlda_ar3 (dep_insn) && get_attr_usear3 (insn))
4670             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4671           if (get_attr_readar3 (dep_insn) && get_attr_usear3 (insn))
4672             max = READ_USE_COST > max ? READ_USE_COST : max;
4673
4674           if (get_attr_setar4 (dep_insn) && get_attr_usear4 (insn))
4675             max = SET_USE_COST > max ? SET_USE_COST : max;
4676           if (get_attr_setlda_ar4 (dep_insn) && get_attr_usear4 (insn))
4677             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4678           if (get_attr_readar4 (dep_insn) && get_attr_usear4 (insn))
4679             max = READ_USE_COST > max ? READ_USE_COST : max;
4680
4681           if (get_attr_setar5 (dep_insn) && get_attr_usear5 (insn))
4682             max = SET_USE_COST > max ? SET_USE_COST : max;
4683           if (get_attr_setlda_ar5 (dep_insn) && get_attr_usear5 (insn))
4684             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4685           if (get_attr_readar5 (dep_insn) && get_attr_usear5 (insn))
4686             max = READ_USE_COST > max ? READ_USE_COST : max;
4687
4688           if (get_attr_setar6 (dep_insn) && get_attr_usear6 (insn))
4689             max = SET_USE_COST > max ? SET_USE_COST : max;
4690           if (get_attr_setlda_ar6 (dep_insn) && get_attr_usear6 (insn))
4691             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4692           if (get_attr_readar6 (dep_insn) && get_attr_usear6 (insn))
4693             max = READ_USE_COST > max ? READ_USE_COST : max;
4694
4695           if (get_attr_setar7 (dep_insn) && get_attr_usear7 (insn))
4696             max = SET_USE_COST > max ? SET_USE_COST : max;
4697           if (get_attr_setlda_ar7 (dep_insn) && get_attr_usear7 (insn))
4698             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4699           if (get_attr_readar7 (dep_insn) && get_attr_usear7 (insn))
4700             max = READ_USE_COST > max ? READ_USE_COST : max;
4701
4702           if (get_attr_setir0 (dep_insn) && get_attr_useir0 (insn))
4703             max = SET_USE_COST > max ? SET_USE_COST : max;
4704           if (get_attr_setlda_ir0 (dep_insn) && get_attr_useir0 (insn))
4705             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4706
4707           if (get_attr_setir1 (dep_insn) && get_attr_useir1 (insn))
4708             max = SET_USE_COST > max ? SET_USE_COST : max;
4709           if (get_attr_setlda_ir1 (dep_insn) && get_attr_useir1 (insn))
4710             max = SETLDA_USE_COST > max ? SETLDA_USE_COST : max;
4711         }
4712
4713       if (max)
4714         cost = max;
4715
4716       /* For other data dependencies, the default cost specified in the
4717          md is correct.  */
4718       return cost;
4719     }
4720   else if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
4721     {
4722       /* Anti dependency; DEP_INSN reads a register that INSN writes some
4723          cycles later.  */
4724
4725       /* For c4x anti dependencies, the cost is 0.  */
4726       return 0;
4727     }
4728   else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4729     {
4730       /* Output dependency; DEP_INSN writes a register that INSN writes some
4731          cycles later.  */
4732
4733       /* For c4x output dependencies, the cost is 0.  */
4734       return 0;
4735     }
4736   else
4737     abort ();
4738 }
4739
4740 void
4741 c4x_init_builtins (void)
4742 {
4743   tree endlink = void_list_node;
4744
4745   builtin_function ("fast_ftoi",
4746                     build_function_type 
4747                     (integer_type_node,
4748                      tree_cons (NULL_TREE, double_type_node, endlink)),
4749                     C4X_BUILTIN_FIX, BUILT_IN_MD, NULL, NULL_TREE);
4750   builtin_function ("ansi_ftoi",
4751                     build_function_type 
4752                     (integer_type_node, 
4753                      tree_cons (NULL_TREE, double_type_node, endlink)),
4754                     C4X_BUILTIN_FIX_ANSI, BUILT_IN_MD, NULL, NULL_TREE);
4755   if (TARGET_C3X)
4756     builtin_function ("fast_imult",
4757                       build_function_type
4758                       (integer_type_node, 
4759                        tree_cons (NULL_TREE, integer_type_node,
4760                                   tree_cons (NULL_TREE,
4761                                              integer_type_node, endlink))),
4762                       C4X_BUILTIN_MPYI, BUILT_IN_MD, NULL, NULL_TREE);
4763   else
4764     {
4765       builtin_function ("toieee",
4766                         build_function_type 
4767                         (double_type_node,
4768                          tree_cons (NULL_TREE, double_type_node, endlink)),
4769                         C4X_BUILTIN_TOIEEE, BUILT_IN_MD, NULL, NULL_TREE);
4770       builtin_function ("frieee",
4771                         build_function_type
4772                         (double_type_node, 
4773                          tree_cons (NULL_TREE, double_type_node, endlink)),
4774                         C4X_BUILTIN_FRIEEE, BUILT_IN_MD, NULL, NULL_TREE);
4775       builtin_function ("fast_invf",
4776                         build_function_type 
4777                         (double_type_node, 
4778                          tree_cons (NULL_TREE, double_type_node, endlink)),
4779                         C4X_BUILTIN_RCPF, BUILT_IN_MD, NULL, NULL_TREE);
4780     }
4781 }
4782
4783
4784 rtx
4785 c4x_expand_builtin (tree exp, rtx target,
4786                     rtx subtarget ATTRIBUTE_UNUSED,
4787                     enum machine_mode mode ATTRIBUTE_UNUSED,
4788                     int ignore ATTRIBUTE_UNUSED)
4789 {
4790   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4791   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4792   tree arglist = TREE_OPERAND (exp, 1);
4793   tree arg0, arg1;
4794   rtx r0, r1;
4795
4796   switch (fcode)
4797     {
4798     case C4X_BUILTIN_FIX:
4799       arg0 = TREE_VALUE (arglist);
4800       r0 = expand_expr (arg0, NULL_RTX, QFmode, 0);
4801       r0 = protect_from_queue (r0, 0);
4802       if (! target || ! register_operand (target, QImode))
4803         target = gen_reg_rtx (QImode);
4804       emit_insn (gen_fixqfqi_clobber (target, r0));
4805       return target;
4806
4807     case C4X_BUILTIN_FIX_ANSI:
4808       arg0 = TREE_VALUE (arglist);
4809       r0 = expand_expr (arg0, NULL_RTX, QFmode, 0);
4810       r0 = protect_from_queue (r0, 0);
4811       if (! target || ! register_operand (target, QImode))
4812         target = gen_reg_rtx (QImode);
4813       emit_insn (gen_fix_truncqfqi2 (target, r0));
4814       return target;
4815
4816     case C4X_BUILTIN_MPYI:
4817       if (! TARGET_C3X)
4818         break;
4819       arg0 = TREE_VALUE (arglist);
4820       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4821       r0 = expand_expr (arg0, NULL_RTX, QImode, 0);
4822       r1 = expand_expr (arg1, NULL_RTX, QImode, 0);
4823       r0 = protect_from_queue (r0, 0);
4824       r1 = protect_from_queue (r1, 0);
4825       if (! target || ! register_operand (target, QImode))
4826         target = gen_reg_rtx (QImode);
4827       emit_insn (gen_mulqi3_24_clobber (target, r0, r1));
4828       return target;
4829
4830     case C4X_BUILTIN_TOIEEE:
4831       if (TARGET_C3X)
4832         break;
4833       arg0 = TREE_VALUE (arglist);
4834       r0 = expand_expr (arg0, NULL_RTX, QFmode, 0);
4835       r0 = protect_from_queue (r0, 0);
4836       if (! target || ! register_operand (target, QFmode))
4837         target = gen_reg_rtx (QFmode);
4838       emit_insn (gen_toieee (target, r0));
4839       return target;
4840
4841     case C4X_BUILTIN_FRIEEE:
4842       if (TARGET_C3X)
4843         break;
4844       arg0 = TREE_VALUE (arglist);
4845       if (TREE_CODE (arg0) == VAR_DECL || TREE_CODE (arg0) == PARM_DECL)
4846         put_var_into_stack (arg0, /*rescan=*/true);
4847       r0 = expand_expr (arg0, NULL_RTX, QFmode, 0);
4848       r0 = protect_from_queue (r0, 0);
4849       if (register_operand (r0, QFmode))
4850         {
4851           r1 = assign_stack_local (QFmode, GET_MODE_SIZE (QFmode), 0);
4852           emit_move_insn (r1, r0);
4853           r0 = r1;
4854         }
4855       if (! target || ! register_operand (target, QFmode))
4856         target = gen_reg_rtx (QFmode);
4857       emit_insn (gen_frieee (target, r0));
4858       return target;
4859
4860     case C4X_BUILTIN_RCPF:
4861       if (TARGET_C3X)
4862         break;
4863       arg0 = TREE_VALUE (arglist);
4864       r0 = expand_expr (arg0, NULL_RTX, QFmode, 0);
4865       r0 = protect_from_queue (r0, 0);
4866       if (! target || ! register_operand (target, QFmode))
4867         target = gen_reg_rtx (QFmode);
4868       emit_insn (gen_rcpfqf_clobber (target, r0));
4869       return target;
4870     }
4871   return NULL_RTX;
4872 }
4873
4874 static void
4875 c4x_init_libfuncs (void)
4876 {
4877   set_optab_libfunc (smul_optab, QImode, "__mulqi3");
4878   set_optab_libfunc (sdiv_optab, QImode, "__divqi3");
4879   set_optab_libfunc (udiv_optab, QImode, "__udivqi3");
4880   set_optab_libfunc (smod_optab, QImode, "__modqi3");
4881   set_optab_libfunc (umod_optab, QImode, "__umodqi3");
4882   set_optab_libfunc (sdiv_optab, QFmode, "__divqf3");
4883   set_optab_libfunc (smul_optab, HFmode, "__mulhf3");
4884   set_optab_libfunc (sdiv_optab, HFmode, "__divhf3");
4885   set_optab_libfunc (smul_optab, HImode, "__mulhi3");
4886   set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
4887   set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
4888   set_optab_libfunc (smod_optab, HImode, "__modhi3");
4889   set_optab_libfunc (umod_optab, HImode, "__umodhi3");
4890   set_optab_libfunc (ffs_optab,  QImode, "__ffs");
4891   smulhi3_libfunc           = init_one_libfunc ("__smulhi3_high");
4892   umulhi3_libfunc           = init_one_libfunc ("__umulhi3_high");
4893   fix_truncqfhi2_libfunc    = init_one_libfunc ("__fix_truncqfhi2");
4894   fixuns_truncqfhi2_libfunc = init_one_libfunc ("__ufix_truncqfhi2");
4895   fix_trunchfhi2_libfunc    = init_one_libfunc ("__fix_trunchfhi2");
4896   fixuns_trunchfhi2_libfunc = init_one_libfunc ("__ufix_trunchfhi2");
4897   floathiqf2_libfunc        = init_one_libfunc ("__floathiqf2");
4898   floatunshiqf2_libfunc     = init_one_libfunc ("__ufloathiqf2");
4899   floathihf2_libfunc        = init_one_libfunc ("__floathihf2");
4900   floatunshihf2_libfunc     = init_one_libfunc ("__ufloathihf2");
4901 }
4902
4903 static void
4904 c4x_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED)
4905 {
4906   fprintf (asm_out_file, "\t.sect\t\"%s\"\n", name);
4907 }
4908
4909 static void
4910 c4x_globalize_label (FILE *stream, const char *name)
4911 {
4912   default_globalize_label (stream, name);
4913   c4x_global_label (name);
4914 }
4915 \f
4916 #define SHIFT_CODE_P(C) \
4917   ((C) == ASHIFT || (C) == ASHIFTRT || (C) == LSHIFTRT)
4918 #define LOGICAL_CODE_P(C) \
4919   ((C) == NOT || (C) == AND || (C) == IOR || (C) == XOR)
4920
4921 /* Compute a (partial) cost for rtx X.  Return true if the complete
4922    cost has been computed, and false if subexpressions should be
4923    scanned.  In either case, *TOTAL contains the cost result.  */
4924
4925 static bool
4926 c4x_rtx_costs (rtx x, int code, int outer_code, int *total)
4927 {
4928   HOST_WIDE_INT val;
4929
4930   switch (code)
4931     {
4932       /* Some small integers are effectively free for the C40.  We should
4933          also consider if we are using the small memory model.  With
4934          the big memory model we require an extra insn for a constant
4935          loaded from memory.  */
4936
4937     case CONST_INT:
4938       val = INTVAL (x);
4939       if (c4x_J_constant (x))
4940         *total = 0;
4941       else if (! TARGET_C3X
4942                && outer_code == AND
4943                && (val == 255 || val == 65535))
4944         *total = 0;
4945       else if (! TARGET_C3X
4946                && (outer_code == ASHIFTRT || outer_code == LSHIFTRT)
4947                && (val == 16 || val == 24))
4948         *total = 0;
4949       else if (TARGET_C3X && SHIFT_CODE_P (outer_code))
4950         *total = 3;
4951       else if (LOGICAL_CODE_P (outer_code)
4952                ? c4x_L_constant (x) : c4x_I_constant (x))
4953         *total = 2;
4954       else
4955         *total = 4;
4956       return true;
4957
4958     case CONST:
4959     case LABEL_REF:
4960     case SYMBOL_REF:
4961       *total = 4;
4962       return true;
4963
4964     case CONST_DOUBLE:
4965       if (c4x_H_constant (x))
4966         *total = 2;
4967       else if (GET_MODE (x) == QFmode)
4968         *total = 4;
4969       else
4970         *total = 8;
4971       return true;
4972
4973     /* ??? Note that we return true, rather than false so that rtx_cost
4974        doesn't include the constant costs.  Otherwise expand_mult will
4975        think that it is cheaper to synthesize a multiply rather than to
4976        use a multiply instruction.  I think this is because the algorithm
4977        synth_mult doesn't take into account the loading of the operands,
4978        whereas the calculation of mult_cost does.  */
4979     case PLUS:
4980     case MINUS:
4981     case AND:
4982     case IOR:
4983     case XOR:
4984     case ASHIFT:
4985     case ASHIFTRT:
4986     case LSHIFTRT:
4987       *total = COSTS_N_INSNS (1);
4988       return true;
4989
4990     case MULT:
4991       *total = COSTS_N_INSNS (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
4992                               || TARGET_MPYI ? 1 : 14);
4993       return true;
4994
4995     case DIV:
4996     case UDIV:
4997     case MOD:
4998     case UMOD:
4999       *total = COSTS_N_INSNS (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
5000                               ? 15 : 50);
5001       return true;
5002
5003     default:
5004       return false;
5005     }
5006 }