OSDN Git Service

f0f6a936407c0a2e2e9378d4b2ea790a53de1c84
[pf3gnuchains/gcc-fork.git] / gcc / config / vax / vax.c
1 /* Subroutines for insn-output.c for VAX.
2    Copyright (C) 1987, 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002,
3    2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "df.h"
28 #include "tree.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "function.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "flags.h"
41 #include "debug.h"
42 #include "toplev.h"
43 #include "tm-preds.h"
44 #include "tm-constrs.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "target-def.h"
48
49 static bool vax_legitimate_address_p (enum machine_mode, rtx, bool);
50 static void vax_output_function_prologue (FILE *, HOST_WIDE_INT);
51 static void vax_file_start (void);
52 static void vax_init_libfuncs (void);
53 static void vax_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
54                                  HOST_WIDE_INT, tree);
55 static int vax_address_cost_1 (rtx);
56 static int vax_address_cost (rtx, bool);
57 static bool vax_rtx_costs (rtx, int, int, int *, bool);
58 static rtx vax_struct_value_rtx (tree, int);
59 static rtx vax_builtin_setjmp_frame_value (void);
60 \f
61 /* Initialize the GCC target structure.  */
62 #undef TARGET_ASM_ALIGNED_HI_OP
63 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
64
65 #undef TARGET_ASM_FUNCTION_PROLOGUE
66 #define TARGET_ASM_FUNCTION_PROLOGUE vax_output_function_prologue
67
68 #undef TARGET_ASM_FILE_START
69 #define TARGET_ASM_FILE_START vax_file_start
70 #undef TARGET_ASM_FILE_START_APP_OFF
71 #define TARGET_ASM_FILE_START_APP_OFF true
72
73 #undef TARGET_INIT_LIBFUNCS
74 #define TARGET_INIT_LIBFUNCS vax_init_libfuncs
75
76 #undef TARGET_ASM_OUTPUT_MI_THUNK
77 #define TARGET_ASM_OUTPUT_MI_THUNK vax_output_mi_thunk
78 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
79 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
80
81 #undef TARGET_DEFAULT_TARGET_FLAGS
82 #define TARGET_DEFAULT_TARGET_FLAGS TARGET_DEFAULT
83
84 #undef TARGET_RTX_COSTS
85 #define TARGET_RTX_COSTS vax_rtx_costs
86 #undef TARGET_ADDRESS_COST
87 #define TARGET_ADDRESS_COST vax_address_cost
88
89 #undef TARGET_PROMOTE_PROTOTYPES
90 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
91
92 #undef TARGET_STRUCT_VALUE_RTX
93 #define TARGET_STRUCT_VALUE_RTX vax_struct_value_rtx
94
95 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
96 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE vax_builtin_setjmp_frame_value
97
98 #undef TARGET_LEGITIMATE_ADDRESS_P
99 #define TARGET_LEGITIMATE_ADDRESS_P vax_legitimate_address_p
100
101 struct gcc_target targetm = TARGET_INITIALIZER;
102 \f
103 /* Set global variables as needed for the options enabled.  */
104
105 void
106 override_options (void)
107 {
108   /* We're VAX floating point, not IEEE floating point.  */
109   if (TARGET_G_FLOAT)
110     REAL_MODE_FORMAT (DFmode) = &vax_g_format;
111 }
112
113 /* Generate the assembly code for function entry.  FILE is a stdio
114    stream to output the code to.  SIZE is an int: how many units of
115    temporary storage to allocate.
116
117    Refer to the array `regs_ever_live' to determine which registers to
118    save; `regs_ever_live[I]' is nonzero if register number I is ever
119    used in the function.  This function is responsible for knowing
120    which registers should not be saved even if used.  */
121
122 static void
123 vax_output_function_prologue (FILE * file, HOST_WIDE_INT size)
124 {
125   int regno;
126   int mask = 0;
127
128   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
129     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
130       mask |= 1 << regno;
131
132   fprintf (file, "\t.word 0x%x\n", mask);
133
134   if (dwarf2out_do_frame ())
135     {
136       const char *label = dwarf2out_cfi_label ();
137       int offset = 0;
138
139       for (regno = FIRST_PSEUDO_REGISTER-1; regno >= 0; --regno)
140         if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
141           dwarf2out_reg_save (label, regno, offset -= 4);
142
143       dwarf2out_reg_save (label, PC_REGNUM, offset -= 4);
144       dwarf2out_reg_save (label, FRAME_POINTER_REGNUM, offset -= 4);
145       dwarf2out_reg_save (label, ARG_POINTER_REGNUM, offset -= 4);
146       dwarf2out_def_cfa (label, FRAME_POINTER_REGNUM, -(offset - 4));
147     }
148
149   size -= STARTING_FRAME_OFFSET;
150   if (size >= 64)
151     asm_fprintf (file, "\tmovab %wd(%Rsp),%Rsp\n", -size);
152   else if (size)
153     asm_fprintf (file, "\tsubl2 $%wd,%Rsp\n", size);
154 }
155
156 /* When debugging with stabs, we want to output an extra dummy label
157    so that gas can distinguish between D_float and G_float prior to
158    processing the .stabs directive identifying type double.  */
159 static void
160 vax_file_start (void)
161 {
162   default_file_start ();
163
164   if (write_symbols == DBX_DEBUG)
165     fprintf (asm_out_file, "___vax_%c_doubles:\n", ASM_DOUBLE_CHAR);
166 }
167
168 /* We can use the BSD C library routines for the libgcc calls that are
169    still generated, since that's what they boil down to anyways.  When
170    ELF, avoid the user's namespace.  */
171
172 static void
173 vax_init_libfuncs (void)
174 {
175   set_optab_libfunc (udiv_optab, SImode, TARGET_ELF ? "*__udiv" : "*udiv");
176   set_optab_libfunc (umod_optab, SImode, TARGET_ELF ? "*__urem" : "*urem");
177 }
178
179 /* This is like nonimmediate_operand with a restriction on the type of MEM.  */
180
181 static void
182 split_quadword_operands (rtx insn, enum rtx_code code, rtx * operands,
183                          rtx * low, int n)
184 {
185   int i;
186
187   for (i = 0; i < n; i++)
188     low[i] = 0;
189
190   for (i = 0; i < n; i++)
191     {
192       if (MEM_P (operands[i])
193           && (GET_CODE (XEXP (operands[i], 0)) == PRE_DEC
194               || GET_CODE (XEXP (operands[i], 0)) == POST_INC))
195         {
196           rtx addr = XEXP (operands[i], 0);
197           operands[i] = low[i] = gen_rtx_MEM (SImode, addr);
198         }
199       else if (optimize_size && MEM_P (operands[i])
200                && REG_P (XEXP (operands[i], 0))
201                && (code != MINUS || operands[1] != const0_rtx)
202                && find_regno_note (insn, REG_DEAD,
203                                    REGNO (XEXP (operands[i], 0))))
204         {
205           low[i] = gen_rtx_MEM (SImode,
206                                 gen_rtx_POST_INC (Pmode,
207                                                   XEXP (operands[i], 0)));
208           operands[i] = gen_rtx_MEM (SImode, XEXP (operands[i], 0));
209         }
210       else
211         {
212           low[i] = operand_subword (operands[i], 0, 0, DImode);
213           operands[i] = operand_subword (operands[i], 1, 0, DImode);
214         }
215     }
216 }
217 \f
218 void
219 print_operand_address (FILE * file, rtx addr)
220 {
221   rtx orig = addr;
222   rtx reg1, breg, ireg;
223   rtx offset;
224
225  retry:
226   switch (GET_CODE (addr))
227     {
228     case MEM:
229       fprintf (file, "*");
230       addr = XEXP (addr, 0);
231       goto retry;
232
233     case REG:
234       fprintf (file, "(%s)", reg_names[REGNO (addr)]);
235       break;
236
237     case PRE_DEC:
238       fprintf (file, "-(%s)", reg_names[REGNO (XEXP (addr, 0))]);
239       break;
240
241     case POST_INC:
242       fprintf (file, "(%s)+", reg_names[REGNO (XEXP (addr, 0))]);
243       break;
244
245     case PLUS:
246       /* There can be either two or three things added here.  One must be a
247          REG.  One can be either a REG or a MULT of a REG and an appropriate
248          constant, and the third can only be a constant or a MEM.
249
250          We get these two or three things and put the constant or MEM in
251          OFFSET, the MULT or REG in IREG, and the REG in BREG.  If we have
252          a register and can't tell yet if it is a base or index register,
253          put it into REG1.  */
254
255       reg1 = 0; ireg = 0; breg = 0; offset = 0;
256
257       if (CONSTANT_ADDRESS_P (XEXP (addr, 0))
258           || MEM_P (XEXP (addr, 0)))
259         {
260           offset = XEXP (addr, 0);
261           addr = XEXP (addr, 1);
262         }
263       else if (CONSTANT_ADDRESS_P (XEXP (addr, 1))
264                || MEM_P (XEXP (addr, 1)))
265         {
266           offset = XEXP (addr, 1);
267           addr = XEXP (addr, 0);
268         }
269       else if (GET_CODE (XEXP (addr, 1)) == MULT)
270         {
271           ireg = XEXP (addr, 1);
272           addr = XEXP (addr, 0);
273         }
274       else if (GET_CODE (XEXP (addr, 0)) == MULT)
275         {
276           ireg = XEXP (addr, 0);
277           addr = XEXP (addr, 1);
278         }
279       else if (REG_P (XEXP (addr, 1)))
280         {
281           reg1 = XEXP (addr, 1);
282           addr = XEXP (addr, 0);
283         }
284       else if (REG_P (XEXP (addr, 0)))
285         {
286           reg1 = XEXP (addr, 0);
287           addr = XEXP (addr, 1);
288         }
289       else
290         gcc_unreachable ();
291
292       if (REG_P (addr))
293         {
294           if (reg1)
295             ireg = addr;
296           else
297             reg1 = addr;
298         }
299       else if (GET_CODE (addr) == MULT)
300         ireg = addr;
301       else
302         {
303           gcc_assert (GET_CODE (addr) == PLUS);
304           if (CONSTANT_ADDRESS_P (XEXP (addr, 0))
305               || MEM_P (XEXP (addr, 0)))
306             {
307               if (offset)
308                 {
309                   if (CONST_INT_P (offset))
310                     offset = plus_constant (XEXP (addr, 0), INTVAL (offset));
311                   else
312                     {
313                       gcc_assert (CONST_INT_P (XEXP (addr, 0)));
314                       offset = plus_constant (offset, INTVAL (XEXP (addr, 0)));
315                     }
316                 }
317               offset = XEXP (addr, 0);
318             }
319           else if (REG_P (XEXP (addr, 0)))
320             {
321               if (reg1)
322                 ireg = reg1, breg = XEXP (addr, 0), reg1 = 0;
323               else
324                 reg1 = XEXP (addr, 0);
325             }
326           else
327             {
328               gcc_assert (GET_CODE (XEXP (addr, 0)) == MULT);
329               gcc_assert (!ireg);
330               ireg = XEXP (addr, 0);
331             }
332
333           if (CONSTANT_ADDRESS_P (XEXP (addr, 1))
334               || MEM_P (XEXP (addr, 1)))
335             {
336               if (offset)
337                 {
338                   if (CONST_INT_P (offset))
339                     offset = plus_constant (XEXP (addr, 1), INTVAL (offset));
340                   else
341                     {
342                       gcc_assert (CONST_INT_P (XEXP (addr, 1)));
343                       offset = plus_constant (offset, INTVAL (XEXP (addr, 1)));
344                     }
345                 }
346               offset = XEXP (addr, 1);
347             }
348           else if (REG_P (XEXP (addr, 1)))
349             {
350               if (reg1)
351                 ireg = reg1, breg = XEXP (addr, 1), reg1 = 0;
352               else
353                 reg1 = XEXP (addr, 1);
354             }
355           else
356             {
357               gcc_assert (GET_CODE (XEXP (addr, 1)) == MULT);
358               gcc_assert (!ireg);
359               ireg = XEXP (addr, 1);
360             }
361         }
362
363       /* If REG1 is nonzero, figure out if it is a base or index register.  */
364       if (reg1)
365         {
366           if (breg
367               || (flag_pic && GET_CODE (addr) == SYMBOL_REF)
368               || (offset
369                   && (MEM_P (offset)
370                       || (flag_pic && symbolic_operand (offset, SImode)))))
371             {
372               gcc_assert (!ireg);
373               ireg = reg1;
374             }
375           else
376             breg = reg1;
377         }
378
379       if (offset != 0)
380         {
381           if (flag_pic && symbolic_operand (offset, SImode))
382             {
383               if (breg && ireg)
384                 {
385                   debug_rtx (orig);
386                   output_operand_lossage ("symbol used with both base and indexed registers");
387                 }
388
389 #ifdef NO_EXTERNAL_INDIRECT_ADDRESS
390               if (flag_pic > 1 && GET_CODE (offset) == CONST
391                   && GET_CODE (XEXP (XEXP (offset, 0), 0)) == SYMBOL_REF
392                   && !SYMBOL_REF_LOCAL_P (XEXP (XEXP (offset, 0), 0)))
393                 {
394                   debug_rtx (orig);
395                   output_operand_lossage ("symbol with offset used in PIC mode");
396                 }
397 #endif
398
399               /* symbol(reg) isn't PIC, but symbol[reg] is.  */
400               if (breg)
401                 {
402                   ireg = breg;
403                   breg = 0;
404                 }
405
406             }
407
408           output_address (offset);
409         }
410
411       if (breg != 0)
412         fprintf (file, "(%s)", reg_names[REGNO (breg)]);
413
414       if (ireg != 0)
415         {
416           if (GET_CODE (ireg) == MULT)
417             ireg = XEXP (ireg, 0);
418           gcc_assert (REG_P (ireg));
419           fprintf (file, "[%s]", reg_names[REGNO (ireg)]);
420         }
421       break;
422
423     default:
424       output_addr_const (file, addr);
425     }
426 }
427
428 void
429 print_operand (FILE *file, rtx x, int code)
430 {
431   if (code == '#')
432     fputc (ASM_DOUBLE_CHAR, file);
433   else if (code == '|')
434     fputs (REGISTER_PREFIX, file);
435   else if (code == 'c')
436     fputs (cond_name (x), file);
437   else if (code == 'C')
438     fputs (rev_cond_name (x), file);
439   else if (code == 'D' && CONST_INT_P (x) && INTVAL (x) < 0)
440     fprintf (file, "$" NEG_HWI_PRINT_HEX16, INTVAL (x));
441   else if (code == 'P' && CONST_INT_P (x))
442     fprintf (file, "$" HOST_WIDE_INT_PRINT_DEC, INTVAL (x) + 1);
443   else if (code == 'N' && CONST_INT_P (x))
444     fprintf (file, "$" HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
445   /* rotl instruction cannot deal with negative arguments.  */
446   else if (code == 'R' && CONST_INT_P (x))
447     fprintf (file, "$" HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
448   else if (code == 'H' && CONST_INT_P (x))
449     fprintf (file, "$%d", (int) (0xffff & ~ INTVAL (x)));
450   else if (code == 'h' && CONST_INT_P (x))
451     fprintf (file, "$%d", (short) - INTVAL (x));
452   else if (code == 'B' && CONST_INT_P (x))
453     fprintf (file, "$%d", (int) (0xff & ~ INTVAL (x)));
454   else if (code == 'b' && CONST_INT_P (x))
455     fprintf (file, "$%d", (int) (0xff & - INTVAL (x)));
456   else if (code == 'M' && CONST_INT_P (x))
457     fprintf (file, "$%d", ~((1 << INTVAL (x)) - 1));
458   else if (REG_P (x))
459     fprintf (file, "%s", reg_names[REGNO (x)]);
460   else if (MEM_P (x))
461     output_address (XEXP (x, 0));
462   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
463     {
464       char dstr[30];
465       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x),
466                        sizeof (dstr), 0, 1);
467       fprintf (file, "$0f%s", dstr);
468     }
469   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
470     {
471       char dstr[30];
472       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x),
473                        sizeof (dstr), 0, 1);
474       fprintf (file, "$0%c%s", ASM_DOUBLE_CHAR, dstr);
475     }
476   else
477     {
478       if (flag_pic > 1 && symbolic_operand (x, SImode))
479         {
480           debug_rtx (x);
481           output_operand_lossage ("symbol used as immediate operand");
482         }
483       putc ('$', file);
484       output_addr_const (file, x);
485     }
486 }
487 \f
488 const char *
489 cond_name (rtx op)
490 {
491   switch (GET_CODE (op))
492     {
493     case NE:
494       return "neq";
495     case EQ:
496       return "eql";
497     case GE:
498       return "geq";
499     case GT:
500       return "gtr";
501     case LE:
502       return "leq";
503     case LT:
504       return "lss";
505     case GEU:
506       return "gequ";
507     case GTU:
508       return "gtru";
509     case LEU:
510       return "lequ";
511     case LTU:
512       return "lssu";
513
514     default:
515       gcc_unreachable ();
516     }
517 }
518
519 const char *
520 rev_cond_name (rtx op)
521 {
522   switch (GET_CODE (op))
523     {
524     case EQ:
525       return "neq";
526     case NE:
527       return "eql";
528     case LT:
529       return "geq";
530     case LE:
531       return "gtr";
532     case GT:
533       return "leq";
534     case GE:
535       return "lss";
536     case LTU:
537       return "gequ";
538     case LEU:
539       return "gtru";
540     case GTU:
541       return "lequ";
542     case GEU:
543       return "lssu";
544
545     default:
546       gcc_unreachable ();
547     }
548 }
549
550 static bool
551 vax_float_literal (rtx c)
552 {
553   enum machine_mode mode;
554   REAL_VALUE_TYPE r, s;
555   int i;
556
557   if (GET_CODE (c) != CONST_DOUBLE)
558     return false;
559
560   mode = GET_MODE (c);
561
562   if (c == const_tiny_rtx[(int) mode][0]
563       || c == const_tiny_rtx[(int) mode][1]
564       || c == const_tiny_rtx[(int) mode][2])
565     return true;
566
567   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
568
569   for (i = 0; i < 7; i++)
570     {
571       int x = 1 << i;
572       bool ok;
573       REAL_VALUE_FROM_INT (s, x, 0, mode);
574
575       if (REAL_VALUES_EQUAL (r, s))
576         return true;
577       ok = exact_real_inverse (mode, &s);
578       gcc_assert (ok);
579       if (REAL_VALUES_EQUAL (r, s))
580         return true;
581     }
582   return false;
583 }
584
585
586 /* Return the cost in cycles of a memory address, relative to register
587    indirect.
588
589    Each of the following adds the indicated number of cycles:
590
591    1 - symbolic address
592    1 - pre-decrement
593    1 - indexing and/or offset(register)
594    2 - indirect */
595
596
597 static int
598 vax_address_cost_1 (rtx addr)
599 {
600   int reg = 0, indexed = 0, indir = 0, offset = 0, predec = 0;
601   rtx plus_op0 = 0, plus_op1 = 0;
602  restart:
603   switch (GET_CODE (addr))
604     {
605     case PRE_DEC:
606       predec = 1;
607     case REG:
608     case SUBREG:
609     case POST_INC:
610       reg = 1;
611       break;
612     case MULT:
613       indexed = 1;      /* 2 on VAX 2 */
614       break;
615     case CONST_INT:
616       /* byte offsets cost nothing (on a VAX 2, they cost 1 cycle) */
617       if (offset == 0)
618         offset = (unsigned HOST_WIDE_INT)(INTVAL(addr)+128) > 256;
619       break;
620     case CONST:
621     case SYMBOL_REF:
622       offset = 1;       /* 2 on VAX 2 */
623       break;
624     case LABEL_REF:     /* this is probably a byte offset from the pc */
625       if (offset == 0)
626         offset = 1;
627       break;
628     case PLUS:
629       if (plus_op0)
630         plus_op1 = XEXP (addr, 0);
631       else
632         plus_op0 = XEXP (addr, 0);
633       addr = XEXP (addr, 1);
634       goto restart;
635     case MEM:
636       indir = 2;        /* 3 on VAX 2 */
637       addr = XEXP (addr, 0);
638       goto restart;
639     default:
640       break;
641     }
642
643   /* Up to 3 things can be added in an address.  They are stored in
644      plus_op0, plus_op1, and addr.  */
645
646   if (plus_op0)
647     {
648       addr = plus_op0;
649       plus_op0 = 0;
650       goto restart;
651     }
652   if (plus_op1)
653     {
654       addr = plus_op1;
655       plus_op1 = 0;
656       goto restart;
657     }
658   /* Indexing and register+offset can both be used (except on a VAX 2)
659      without increasing execution time over either one alone.  */
660   if (reg && indexed && offset)
661     return reg + indir + offset + predec;
662   return reg + indexed + indir + offset + predec;
663 }
664
665 static int
666 vax_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
667 {
668   return (1 + (REG_P (x) ? 0 : vax_address_cost_1 (x)));
669 }
670
671 /* Cost of an expression on a VAX.  This version has costs tuned for the
672    CVAX chip (found in the VAX 3 series) with comments for variations on
673    other models.
674
675    FIXME: The costs need review, particularly for TRUNCATE, FLOAT_EXTEND
676    and FLOAT_TRUNCATE.  We need a -mcpu option to allow provision of
677    costs on a per cpu basis.  */
678
679 static bool
680 vax_rtx_costs (rtx x, int code, int outer_code, int *total,
681                bool speed ATTRIBUTE_UNUSED)
682 {
683   enum machine_mode mode = GET_MODE (x);
684   int i = 0;                               /* may be modified in switch */
685   const char *fmt = GET_RTX_FORMAT (code); /* may be modified in switch */
686
687   switch (code)
688     {
689       /* On a VAX, constants from 0..63 are cheap because they can use the
690          1 byte literal constant format.  Compare to -1 should be made cheap
691          so that decrement-and-branch insns can be formed more easily (if
692          the value -1 is copied to a register some decrement-and-branch
693          patterns will not match).  */
694     case CONST_INT:
695       if (INTVAL (x) == 0)
696         {
697           *total = 0;
698           return true;
699         }
700       if (outer_code == AND)
701         {
702           *total = ((unsigned HOST_WIDE_INT) ~INTVAL (x) <= 077) ? 1 : 2;
703           return true;
704         }
705       if ((unsigned HOST_WIDE_INT) INTVAL (x) <= 077
706           || (outer_code == COMPARE
707               && INTVAL (x) == -1)
708           || ((outer_code == PLUS || outer_code == MINUS)
709               && (unsigned HOST_WIDE_INT) -INTVAL (x) <= 077))
710         {
711           *total = 1;
712           return true;
713         }
714       /* FALLTHRU */
715
716     case CONST:
717     case LABEL_REF:
718     case SYMBOL_REF:
719       *total = 3;
720       return true;
721
722     case CONST_DOUBLE:
723       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
724         *total = vax_float_literal (x) ? 5 : 8;
725       else
726         *total = ((CONST_DOUBLE_HIGH (x) == 0
727                    && (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (x) < 64)
728                   || (outer_code == PLUS
729                       && CONST_DOUBLE_HIGH (x) == -1
730                       && (unsigned HOST_WIDE_INT)-CONST_DOUBLE_LOW (x) < 64))
731                  ? 2 : 5;
732       return true;
733
734     case POST_INC:
735       *total = 2;
736       return true;              /* Implies register operand.  */
737
738     case PRE_DEC:
739       *total = 3;
740       return true;              /* Implies register operand.  */
741
742     case MULT:
743       switch (mode)
744         {
745         case DFmode:
746           *total = 16;          /* 4 on VAX 9000 */
747           break;
748         case SFmode:
749           *total = 9;           /* 4 on VAX 9000, 12 on VAX 2 */
750           break;
751         case DImode:
752           *total = 16;          /* 6 on VAX 9000, 28 on VAX 2 */
753           break;
754         case SImode:
755         case HImode:
756         case QImode:
757           *total = 10;          /* 3-4 on VAX 9000, 20-28 on VAX 2 */
758           break;
759         default:
760           *total = MAX_COST;    /* Mode is not supported.  */
761           return true;
762         }
763       break;
764
765     case UDIV:
766       if (mode != SImode)
767         {
768           *total = MAX_COST;    /* Mode is not supported.  */
769           return true;
770         }
771       *total = 17;
772       break;
773
774     case DIV:
775       if (mode == DImode)
776         *total = 30;            /* Highly variable.  */
777       else if (mode == DFmode)
778         /* divide takes 28 cycles if the result is not zero, 13 otherwise */
779         *total = 24;
780       else
781         *total = 11;            /* 25 on VAX 2 */
782       break;
783
784     case MOD:
785       *total = 23;
786       break;
787
788     case UMOD:
789       if (mode != SImode)
790         {
791           *total = MAX_COST;    /* Mode is not supported.  */
792           return true;
793         }
794       *total = 29;
795       break;
796
797     case FLOAT:
798       *total = (6               /* 4 on VAX 9000 */
799                 + (mode == DFmode) + (GET_MODE (XEXP (x, 0)) != SImode));
800       break;
801
802     case FIX:
803       *total = 7;               /* 17 on VAX 2 */
804       break;
805
806     case ASHIFT:
807     case LSHIFTRT:
808     case ASHIFTRT:
809       if (mode == DImode)
810         *total = 12;
811       else
812         *total = 10;            /* 6 on VAX 9000 */
813       break;
814
815     case ROTATE:
816     case ROTATERT:
817       *total = 6;               /* 5 on VAX 2, 4 on VAX 9000 */
818       if (CONST_INT_P (XEXP (x, 1)))
819         fmt = "e";              /* all constant rotate counts are short */
820       break;
821
822     case PLUS:
823     case MINUS:
824       *total = (mode == DFmode) ? 13 : 8; /* 6/8 on VAX 9000, 16/15 on VAX 2 */
825       /* Small integer operands can use subl2 and addl2.  */
826       if ((CONST_INT_P (XEXP (x, 1)))
827           && (unsigned HOST_WIDE_INT)(INTVAL (XEXP (x, 1)) + 63) < 127)
828         fmt = "e";
829       break;
830
831     case IOR:
832     case XOR:
833       *total = 3;
834       break;
835
836     case AND:
837       /* AND is special because the first operand is complemented.  */
838       *total = 3;
839       if (CONST_INT_P (XEXP (x, 0)))
840         {
841           if ((unsigned HOST_WIDE_INT)~INTVAL (XEXP (x, 0)) > 63)
842             *total = 4;
843           fmt = "e";
844           i = 1;
845         }
846       break;
847
848     case NEG:
849       if (mode == DFmode)
850         *total = 9;
851       else if (mode == SFmode)
852         *total = 6;
853       else if (mode == DImode)
854         *total = 4;
855       else
856         *total = 2;
857       break;
858
859     case NOT:
860       *total = 2;
861       break;
862
863     case ZERO_EXTRACT:
864     case SIGN_EXTRACT:
865       *total = 15;
866       break;
867
868     case MEM:
869       if (mode == DImode || mode == DFmode)
870         *total = 5;             /* 7 on VAX 2 */
871       else
872         *total = 3;             /* 4 on VAX 2 */
873       x = XEXP (x, 0);
874       if (!REG_P (x) && GET_CODE (x) != POST_INC)
875         *total += vax_address_cost_1 (x);
876       return true;
877
878     case FLOAT_EXTEND:
879     case FLOAT_TRUNCATE:
880     case TRUNCATE:
881       *total = 3;               /* FIXME: Costs need to be checked  */
882       break;
883
884     default:
885       return false;
886     }
887
888   /* Now look inside the expression.  Operands which are not registers or
889      short constants add to the cost.
890
891      FMT and I may have been adjusted in the switch above for instructions
892      which require special handling.  */
893
894   while (*fmt++ == 'e')
895     {
896       rtx op = XEXP (x, i);
897
898       i += 1;
899       code = GET_CODE (op);
900
901       /* A NOT is likely to be found as the first operand of an AND
902          (in which case the relevant cost is of the operand inside
903          the not) and not likely to be found anywhere else.  */
904       if (code == NOT)
905         op = XEXP (op, 0), code = GET_CODE (op);
906
907       switch (code)
908         {
909         case CONST_INT:
910           if ((unsigned HOST_WIDE_INT)INTVAL (op) > 63
911               && GET_MODE (x) != QImode)
912             *total += 1;        /* 2 on VAX 2 */
913           break;
914         case CONST:
915         case LABEL_REF:
916         case SYMBOL_REF:
917           *total += 1;          /* 2 on VAX 2 */
918           break;
919         case CONST_DOUBLE:
920           if (GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
921             {
922               /* Registers are faster than floating point constants -- even
923                  those constants which can be encoded in a single byte.  */
924               if (vax_float_literal (op))
925                 *total += 1;
926               else
927                 *total += (GET_MODE (x) == DFmode) ? 3 : 2;
928             }
929           else
930             {
931               if (CONST_DOUBLE_HIGH (op) != 0
932                   || (unsigned HOST_WIDE_INT)CONST_DOUBLE_LOW (op) > 63)
933                 *total += 2;
934             }
935           break;
936         case MEM:
937           *total += 1;          /* 2 on VAX 2 */
938           if (!REG_P (XEXP (op, 0)))
939             *total += vax_address_cost_1 (XEXP (op, 0));
940           break;
941         case REG:
942         case SUBREG:
943           break;
944         default:
945           *total += 1;
946           break;
947         }
948     }
949   return true;
950 }
951 \f
952 /* Output code to add DELTA to the first argument, and then jump to FUNCTION.
953    Used for C++ multiple inheritance.
954         .mask   ^m<r2,r3,r4,r5,r6,r7,r8,r9,r10,r11>  #conservative entry mask
955         addl2   $DELTA, 4(ap)   #adjust first argument
956         jmp     FUNCTION+2      #jump beyond FUNCTION's entry mask
957 */
958
959 static void
960 vax_output_mi_thunk (FILE * file,
961                      tree thunk ATTRIBUTE_UNUSED,
962                      HOST_WIDE_INT delta,
963                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
964                      tree function)
965 {
966   fprintf (file, "\t.word 0x0ffc\n\taddl2 $" HOST_WIDE_INT_PRINT_DEC, delta);
967   asm_fprintf (file, ",4(%Rap)\n");
968   fprintf (file, "\tjmp ");
969   assemble_name (file,  XSTR (XEXP (DECL_RTL (function), 0), 0));
970   fprintf (file, "+2\n");
971 }
972 \f
973 static rtx
974 vax_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
975                       int incoming ATTRIBUTE_UNUSED)
976 {
977   return gen_rtx_REG (Pmode, VAX_STRUCT_VALUE_REGNUM);
978 }
979
980 static rtx
981 vax_builtin_setjmp_frame_value (void)
982 {
983   return hard_frame_pointer_rtx;
984 }
985
986 /* Worker function for NOTICE_UPDATE_CC.  */
987
988 void
989 vax_notice_update_cc (rtx exp, rtx insn ATTRIBUTE_UNUSED)
990 {
991   if (GET_CODE (exp) == SET)
992     {
993       if (GET_CODE (SET_SRC (exp)) == CALL)
994         CC_STATUS_INIT;
995       else if (GET_CODE (SET_DEST (exp)) != ZERO_EXTRACT
996                && GET_CODE (SET_DEST (exp)) != PC)
997         {
998           cc_status.flags = 0;
999           /* The integer operations below don't set carry or
1000              set it in an incompatible way.  That's ok though
1001              as the Z bit is all we need when doing unsigned
1002              comparisons on the result of these insns (since
1003              they're always with 0).  Set CC_NO_OVERFLOW to
1004              generate the correct unsigned branches.  */
1005           switch (GET_CODE (SET_SRC (exp)))
1006             {
1007             case NEG:
1008               if (GET_MODE_CLASS (GET_MODE (exp)) == MODE_FLOAT)
1009                 break;
1010             case AND:
1011             case IOR:
1012             case XOR:
1013             case NOT:
1014             case MEM:
1015             case REG:
1016               cc_status.flags = CC_NO_OVERFLOW;
1017               break;
1018             default:
1019               break;
1020             }
1021           cc_status.value1 = SET_DEST (exp);
1022           cc_status.value2 = SET_SRC (exp);
1023         }
1024     }
1025   else if (GET_CODE (exp) == PARALLEL
1026            && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
1027     {
1028       if (GET_CODE (SET_SRC (XVECEXP (exp, 0, 0))) == CALL)
1029         CC_STATUS_INIT;
1030       else if (GET_CODE (SET_DEST (XVECEXP (exp, 0, 0))) != PC)
1031         {
1032           cc_status.flags = 0;
1033           cc_status.value1 = SET_DEST (XVECEXP (exp, 0, 0));
1034           cc_status.value2 = SET_SRC (XVECEXP (exp, 0, 0));
1035         }
1036       else
1037         /* PARALLELs whose first element sets the PC are aob,
1038            sob insns.  They do change the cc's.  */
1039         CC_STATUS_INIT;
1040     }
1041   else
1042     CC_STATUS_INIT;
1043   if (cc_status.value1 && REG_P (cc_status.value1)
1044       && cc_status.value2
1045       && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
1046     cc_status.value2 = 0;
1047   if (cc_status.value1 && MEM_P (cc_status.value1)
1048       && cc_status.value2
1049       && MEM_P (cc_status.value2))
1050     cc_status.value2 = 0;
1051   /* Actual condition, one line up, should be that value2's address
1052      depends on value1, but that is too much of a pain.  */
1053 }
1054
1055 /* Output integer move instructions.  */
1056
1057 const char *
1058 vax_output_int_move (rtx insn ATTRIBUTE_UNUSED, rtx *operands,
1059                      enum machine_mode mode)
1060 {
1061   rtx hi[3], lo[3];
1062   const char *pattern_hi, *pattern_lo;
1063
1064   switch (mode)
1065     {
1066     case DImode:
1067       if (operands[1] == const0_rtx)
1068         return "clrq %0";
1069       if (TARGET_QMATH && optimize_size
1070           && (CONST_INT_P (operands[1])
1071               || GET_CODE (operands[1]) == CONST_DOUBLE))
1072         {
1073           unsigned HOST_WIDE_INT hval, lval;
1074           int n;
1075
1076           if (GET_CODE (operands[1]) == CONST_DOUBLE)
1077             {
1078               gcc_assert (HOST_BITS_PER_WIDE_INT != 64);
1079
1080               /* Make sure only the low 32 bits are valid.  */
1081               lval = CONST_DOUBLE_LOW (operands[1]) & 0xffffffff;
1082               hval = CONST_DOUBLE_HIGH (operands[1]) & 0xffffffff;
1083             }
1084           else
1085             {
1086               lval = INTVAL (operands[1]);
1087               hval = 0;
1088             }
1089
1090           /* Here we see if we are trying to see if the 64bit value is really
1091              a 6bit shifted some arbitrary amount.  If so, we can use ashq to
1092              shift it to the correct value saving 7 bytes (1 addr-mode-byte +
1093              8 bytes - 1 shift byte - 1 short literal byte.  */
1094           if (lval != 0
1095               && (n = exact_log2 (lval & (- lval))) != -1
1096               && (lval >> n) < 64)
1097             {
1098               lval >>= n;
1099
1100 #if HOST_BITS_PER_WIDE_INT == 32
1101               /* On 32bit platforms, if the 6bits didn't overflow into the
1102                  upper 32bit value that value better be 0.  If we have
1103                  overflowed, make sure it wasn't too much.  */
1104               if (hval != 0)
1105                 {
1106                   if (n <= 26 || hval >= ((unsigned)1 << (n - 26)))
1107                     n = 0;      /* failure */
1108                   else
1109                     lval |= hval << (32 - n);
1110                 }
1111 #endif
1112               /*  If n is 0, then ashq is not the best way to emit this.  */
1113               if (n > 0)
1114                 {
1115                   operands[1] = GEN_INT (lval);
1116                   operands[2] = GEN_INT (n);
1117                   return "ashq %2,%1,%0";
1118                 }
1119 #if HOST_BITS_PER_WIDE_INT == 32
1120             }
1121           /* On 32bit platforms, if the low 32bit value is 0, checkout the
1122              upper 32bit value.  */
1123           else if (hval != 0
1124                    && (n = exact_log2 (hval & (- hval)) - 1) != -1
1125                    && (hval >> n) < 64)
1126             {
1127               operands[1] = GEN_INT (hval >> n);
1128               operands[2] = GEN_INT (n + 32);
1129               return "ashq %2,%1,%0";
1130 #endif
1131             }
1132         }
1133
1134       if (TARGET_QMATH
1135           && (!MEM_P (operands[0])
1136               || GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
1137               || GET_CODE (XEXP (operands[0], 0)) == POST_INC
1138               || !illegal_addsub_di_memory_operand (operands[0], DImode))
1139           && ((CONST_INT_P (operands[1])
1140                && (unsigned HOST_WIDE_INT) INTVAL (operands[1]) >= 64)
1141               || GET_CODE (operands[1]) == CONST_DOUBLE))
1142         {
1143           hi[0] = operands[0];
1144           hi[1] = operands[1];
1145
1146           split_quadword_operands (insn, SET, hi, lo, 2);
1147
1148           pattern_lo = vax_output_int_move (NULL, lo, SImode);
1149           pattern_hi = vax_output_int_move (NULL, hi, SImode);
1150
1151           /* The patterns are just movl/movl or pushl/pushl then a movq will
1152              be shorter (1 opcode byte + 1 addrmode byte + 8 immediate value
1153              bytes .vs. 2 opcode bytes + 2 addrmode bytes + 8 immediate value
1154              value bytes.  */
1155           if ((!strncmp (pattern_lo, "movl", 4)
1156               && !strncmp (pattern_hi, "movl", 4))
1157               || (!strncmp (pattern_lo, "pushl", 5)
1158                   && !strncmp (pattern_hi, "pushl", 5)))
1159             return "movq %1,%0";
1160
1161           if (MEM_P (operands[0])
1162               && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
1163             {
1164               output_asm_insn (pattern_hi, hi);
1165               operands[0] = lo[0];
1166               operands[1] = lo[1];
1167               operands[2] = lo[2];
1168               return pattern_lo;
1169             }
1170           else
1171             {
1172               output_asm_insn (pattern_lo, lo);
1173               operands[0] = hi[0];
1174               operands[1] = hi[1];
1175               operands[2] = hi[2];
1176               return pattern_hi;
1177             }
1178         }
1179       return "movq %1,%0";
1180
1181     case SImode:
1182       if (symbolic_operand (operands[1], SImode))
1183         {
1184           if (push_operand (operands[0], SImode))
1185             return "pushab %a1";
1186           return "movab %a1,%0";
1187         }
1188
1189       if (operands[1] == const0_rtx)
1190         {
1191           if (push_operand (operands[1], SImode))
1192             return "pushl %1";
1193           return "clrl %0";
1194         }
1195
1196       if (CONST_INT_P (operands[1])
1197           && (unsigned HOST_WIDE_INT) INTVAL (operands[1]) >= 64)
1198         {
1199           HOST_WIDE_INT i = INTVAL (operands[1]);
1200           int n;
1201           if ((unsigned HOST_WIDE_INT)(~i) < 64)
1202             return "mcoml %N1,%0";
1203           if ((unsigned HOST_WIDE_INT)i < 0x100)
1204             return "movzbl %1,%0";
1205           if (i >= -0x80 && i < 0)
1206             return "cvtbl %1,%0";
1207           if (optimize_size
1208               && (n = exact_log2 (i & (-i))) != -1
1209               && ((unsigned HOST_WIDE_INT)i >> n) < 64)
1210             {
1211               operands[1] = GEN_INT ((unsigned HOST_WIDE_INT)i >> n);
1212               operands[2] = GEN_INT (n);
1213               return "ashl %2,%1,%0";
1214             }
1215           if ((unsigned HOST_WIDE_INT)i < 0x10000)
1216             return "movzwl %1,%0";
1217           if (i >= -0x8000 && i < 0)
1218             return "cvtwl %1,%0";
1219         }
1220       if (push_operand (operands[0], SImode))
1221         return "pushl %1";
1222       return "movl %1,%0";
1223
1224     case HImode:
1225       if (CONST_INT_P (operands[1]))
1226         {
1227           HOST_WIDE_INT i = INTVAL (operands[1]);
1228           if (i == 0)
1229             return "clrw %0";
1230           else if ((unsigned HOST_WIDE_INT)i < 64)
1231             return "movw %1,%0";
1232           else if ((unsigned HOST_WIDE_INT)~i < 64)
1233             return "mcomw %H1,%0";
1234           else if ((unsigned HOST_WIDE_INT)i < 256)
1235             return "movzbw %1,%0";
1236           else if (i >= -0x80 && i < 0)
1237             return "cvtbw %1,%0";
1238         }
1239       return "movw %1,%0";
1240
1241     case QImode:
1242       if (CONST_INT_P (operands[1]))
1243         {
1244           HOST_WIDE_INT i = INTVAL (operands[1]);
1245           if (i == 0)
1246             return "clrb %0";
1247           else if ((unsigned HOST_WIDE_INT)~i < 64)
1248             return "mcomb %B1,%0";
1249         }
1250       return "movb %1,%0";
1251
1252     default:
1253       gcc_unreachable ();
1254     }
1255 }
1256
1257 /* Output integer add instructions.
1258
1259    The space-time-opcode tradeoffs for addition vary by model of VAX.
1260
1261    On a VAX 3 "movab (r1)[r2],r3" is faster than "addl3 r1,r2,r3",
1262    but it not faster on other models.
1263
1264    "movab #(r1),r2" is usually shorter than "addl3 #,r1,r2", and is
1265    faster on a VAX 3, but some VAXen (e.g. VAX 9000) will stall if
1266    a register is used in an address too soon after it is set.
1267    Compromise by using movab only when it is shorter than the add
1268    or the base register in the address is one of sp, ap, and fp,
1269    which are not modified very often.  */
1270
1271 const char *
1272 vax_output_int_add (rtx insn, rtx *operands, enum machine_mode mode)
1273 {
1274   switch (mode)
1275     {
1276     case DImode:
1277       {
1278         rtx low[3];
1279         const char *pattern;
1280         int carry = 1;
1281         bool sub;
1282
1283         if (TARGET_QMATH && 0)
1284           debug_rtx (insn);
1285
1286         split_quadword_operands (insn, PLUS, operands, low, 3);
1287
1288         if (TARGET_QMATH)
1289           {
1290             gcc_assert (rtx_equal_p (operands[0], operands[1]));
1291 #ifdef NO_EXTERNAL_INDIRECT_ADDRESSS
1292             gcc_assert (!flag_pic || !external_memory_operand (low[2], SImode));
1293             gcc_assert (!flag_pic || !external_memory_operand (low[0], SImode));
1294 #endif
1295
1296             /* No reason to add a 0 to the low part and thus no carry, so just
1297                emit the appropriate add/sub instruction.  */
1298             if (low[2] == const0_rtx)
1299               return vax_output_int_add (NULL, operands, SImode);
1300
1301             /* Are we doing addition or subtraction?  */
1302             sub = CONST_INT_P (operands[2]) && INTVAL (operands[2]) < 0;
1303
1304             /* We can't use vax_output_int_add since some the patterns don't
1305                modify the carry bit.  */
1306             if (sub)
1307               {
1308                 if (low[2] == constm1_rtx)
1309                   pattern = "decl %0";
1310                 else
1311                   pattern = "subl2 $%n2,%0";
1312               }
1313             else
1314               {
1315                 if (low[2] == const1_rtx)
1316                   pattern = "incl %0";
1317                 else
1318                   pattern = "addl2 %2,%0";
1319               }
1320             output_asm_insn (pattern, low);
1321
1322             /* In 2's complement, -n = ~n + 1.  Since we are dealing with
1323                two 32bit parts, we complement each and then add one to
1324                low part.  We know that the low part can't overflow since
1325                it's value can never be 0.  */
1326             if (sub)
1327                 return "sbwc %N2,%0";
1328             return "adwc %2,%0";
1329           }
1330
1331         /* Add low parts.  */
1332         if (rtx_equal_p (operands[0], operands[1]))
1333           {
1334             if (low[2] == const0_rtx)
1335         /* Should examine operand, punt if not POST_INC.  */
1336               pattern = "tstl %0", carry = 0;
1337             else if (low[2] == const1_rtx)
1338               pattern = "incl %0";
1339             else
1340               pattern = "addl2 %2,%0";
1341           }
1342         else
1343           {
1344             if (low[2] == const0_rtx)
1345               pattern = "movl %1,%0", carry = 0;
1346             else
1347               pattern = "addl3 %2,%1,%0";
1348           }
1349         if (pattern)
1350           output_asm_insn (pattern, low);
1351         if (!carry)
1352           /* If CARRY is 0, we don't have any carry value to worry about.  */
1353           return get_insn_template (CODE_FOR_addsi3, insn);
1354         /* %0 = C + %1 + %2 */
1355         if (!rtx_equal_p (operands[0], operands[1]))
1356           output_asm_insn ((operands[1] == const0_rtx
1357                             ? "clrl %0"
1358                             : "movl %1,%0"), operands);
1359         return "adwc %2,%0";
1360       }
1361
1362     case SImode:
1363       if (rtx_equal_p (operands[0], operands[1]))
1364         {
1365           if (operands[2] == const1_rtx)
1366             return "incl %0";
1367           if (operands[2] == constm1_rtx)
1368             return "decl %0";
1369           if (CONST_INT_P (operands[2])
1370               && (unsigned HOST_WIDE_INT) (- INTVAL (operands[2])) < 64)
1371             return "subl2 $%n2,%0";
1372           if (CONST_INT_P (operands[2])
1373               && (unsigned HOST_WIDE_INT) INTVAL (operands[2]) >= 64
1374               && REG_P (operands[1])
1375               && ((INTVAL (operands[2]) < 32767 && INTVAL (operands[2]) > -32768)
1376                    || REGNO (operands[1]) > 11))
1377             return "movab %c2(%1),%0";
1378           if (REG_P (operands[0]) && symbolic_operand (operands[2], SImode))
1379             return "movab %a2[%0],%0";
1380           return "addl2 %2,%0";
1381         }
1382
1383       if (rtx_equal_p (operands[0], operands[2]))
1384         {
1385           if (REG_P (operands[0]) && symbolic_operand (operands[1], SImode))
1386             return "movab %a1[%0],%0";
1387           return "addl2 %1,%0";
1388         }
1389
1390       if (CONST_INT_P (operands[2])
1391           && INTVAL (operands[2]) < 32767
1392           && INTVAL (operands[2]) > -32768
1393           && REG_P (operands[1])
1394           && push_operand (operands[0], SImode))
1395         return "pushab %c2(%1)";
1396
1397       if (CONST_INT_P (operands[2])
1398           && (unsigned HOST_WIDE_INT) (- INTVAL (operands[2])) < 64)
1399         return "subl3 $%n2,%1,%0";
1400
1401       if (CONST_INT_P (operands[2])
1402           && (unsigned HOST_WIDE_INT) INTVAL (operands[2]) >= 64
1403           && REG_P (operands[1])
1404           && ((INTVAL (operands[2]) < 32767 && INTVAL (operands[2]) > -32768)
1405                || REGNO (operands[1]) > 11))
1406         return "movab %c2(%1),%0";
1407
1408       /* Add this if using gcc on a VAX 3xxx:
1409       if (REG_P (operands[1]) && REG_P (operands[2]))
1410         return "movab (%1)[%2],%0";
1411       */
1412
1413       if (REG_P (operands[1]) && symbolic_operand (operands[2], SImode))
1414         {
1415           if (push_operand (operands[0], SImode))
1416             return "pushab %a2[%1]";
1417           return "movab %a2[%1],%0";
1418         }
1419
1420       if (REG_P (operands[2]) && symbolic_operand (operands[1], SImode))
1421         {
1422           if (push_operand (operands[0], SImode))
1423             return "pushab %a1[%2]";
1424           return "movab %a1[%2],%0";
1425         }
1426
1427       if (flag_pic && REG_P (operands[0])
1428           && symbolic_operand (operands[2], SImode))
1429         return "movab %a2,%0;addl2 %1,%0";
1430
1431       if (flag_pic
1432           && (symbolic_operand (operands[1], SImode)
1433               || symbolic_operand (operands[1], SImode)))
1434         debug_rtx (insn);
1435
1436       return "addl3 %1,%2,%0";
1437
1438     case HImode:
1439       if (rtx_equal_p (operands[0], operands[1]))
1440         {
1441           if (operands[2] == const1_rtx)
1442             return "incw %0";
1443           if (operands[2] == constm1_rtx)
1444             return "decw %0";
1445           if (CONST_INT_P (operands[2])
1446               && (unsigned HOST_WIDE_INT) (- INTVAL (operands[2])) < 64)
1447             return "subw2 $%n2,%0";
1448           return "addw2 %2,%0";
1449         }
1450       if (rtx_equal_p (operands[0], operands[2]))
1451         return "addw2 %1,%0";
1452       if (CONST_INT_P (operands[2])
1453           && (unsigned HOST_WIDE_INT) (- INTVAL (operands[2])) < 64)
1454         return "subw3 $%n2,%1,%0";
1455       return "addw3 %1,%2,%0";
1456
1457     case QImode:
1458       if (rtx_equal_p (operands[0], operands[1]))
1459         {
1460           if (operands[2] == const1_rtx)
1461             return "incb %0";
1462           if (operands[2] == constm1_rtx)
1463             return "decb %0";
1464           if (CONST_INT_P (operands[2])
1465               && (unsigned HOST_WIDE_INT) (- INTVAL (operands[2])) < 64)
1466             return "subb2 $%n2,%0";
1467           return "addb2 %2,%0";
1468         }
1469       if (rtx_equal_p (operands[0], operands[2]))
1470         return "addb2 %1,%0";
1471       if (CONST_INT_P (operands[2])
1472           && (unsigned HOST_WIDE_INT) (- INTVAL (operands[2])) < 64)
1473         return "subb3 $%n2,%1,%0";
1474       return "addb3 %1,%2,%0";
1475
1476     default:
1477       gcc_unreachable ();
1478     }
1479 }
1480
1481 const char *
1482 vax_output_int_subtract (rtx insn, rtx *operands, enum machine_mode mode)
1483 {
1484   switch (mode)
1485     {
1486     case DImode:
1487       {
1488         rtx low[3];
1489         const char *pattern;
1490         int carry = 1;
1491
1492         if (TARGET_QMATH && 0)
1493           debug_rtx (insn);
1494
1495         split_quadword_operands (insn, MINUS, operands, low, 3);
1496
1497         if (TARGET_QMATH)
1498           {
1499             if (operands[1] == const0_rtx && low[1] == const0_rtx)
1500               {
1501                 /* Negation is tricky.  It's basically complement and increment.
1502                    Negate hi, then lo, and subtract the carry back.  */
1503                 if ((MEM_P (low[0]) && GET_CODE (XEXP (low[0], 0)) == POST_INC)
1504                     || (MEM_P (operands[0])
1505                         && GET_CODE (XEXP (operands[0], 0)) == POST_INC))
1506                   fatal_insn ("illegal operand detected", insn);
1507                 output_asm_insn ("mnegl %2,%0", operands);
1508                 output_asm_insn ("mnegl %2,%0", low);
1509                 return "sbwc $0,%0";
1510               }
1511             gcc_assert (rtx_equal_p (operands[0], operands[1]));
1512             gcc_assert (rtx_equal_p (low[0], low[1]));
1513             if (low[2] == const1_rtx)
1514               output_asm_insn ("decl %0", low);
1515             else
1516               output_asm_insn ("subl2 %2,%0", low);
1517             return "sbwc %2,%0";
1518           }
1519
1520         /* Subtract low parts.  */
1521         if (rtx_equal_p (operands[0], operands[1]))
1522           {
1523             if (low[2] == const0_rtx)
1524               pattern = 0, carry = 0;
1525             else if (low[2] == constm1_rtx)
1526               pattern = "decl %0";
1527             else
1528               pattern = "subl2 %2,%0";
1529           }
1530         else
1531           {
1532             if (low[2] == constm1_rtx)
1533               pattern = "decl %0";
1534             else if (low[2] == const0_rtx)
1535               pattern = get_insn_template (CODE_FOR_movsi, insn), carry = 0;
1536             else
1537               pattern = "subl3 %2,%1,%0";
1538           }
1539         if (pattern)
1540           output_asm_insn (pattern, low);
1541         if (carry)
1542           {
1543             if (!rtx_equal_p (operands[0], operands[1]))
1544               return "movl %1,%0;sbwc %2,%0";
1545             return "sbwc %2,%0";
1546             /* %0 = %2 - %1 - C */
1547           }
1548         return get_insn_template (CODE_FOR_subsi3, insn);
1549       }
1550
1551     default:
1552       gcc_unreachable ();
1553   }
1554 }
1555
1556 /* True if X is an rtx for a constant that is a valid address.  */
1557
1558 bool
1559 legitimate_constant_address_p (rtx x)
1560 {
1561   if (GET_CODE (x) == LABEL_REF || GET_CODE (x) == SYMBOL_REF
1562           || CONST_INT_P (x) || GET_CODE (x) == HIGH)
1563     return true;
1564   if (GET_CODE (x) != CONST)
1565     return false;
1566 #ifdef NO_EXTERNAL_INDIRECT_ADDRESS
1567   if (flag_pic
1568       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1569       && !SYMBOL_REF_LOCAL_P (XEXP (XEXP (x, 0), 0)))
1570     return false;
1571 #endif
1572    return true;
1573 }
1574
1575 /* True if the constant value X is a legitimate general operand.
1576    It is given that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
1577
1578 bool
1579 legitimate_constant_p (rtx x ATTRIBUTE_UNUSED)
1580 {
1581   return true;
1582 }
1583
1584 /* The other macros defined here are used only in legitimate_address_p ().  */
1585
1586 /* Nonzero if X is a hard reg that can be used as an index
1587    or, if not strict, if it is a pseudo reg.  */
1588 #define INDEX_REGISTER_P(X, STRICT) \
1589 (REG_P (X) && (!(STRICT) || REGNO_OK_FOR_INDEX_P (REGNO (X))))
1590
1591 /* Nonzero if X is a hard reg that can be used as a base reg
1592    or, if not strict, if it is a pseudo reg.  */
1593 #define BASE_REGISTER_P(X, STRICT) \
1594 (REG_P (X) && (!(STRICT) || REGNO_OK_FOR_BASE_P (REGNO (X))))
1595
1596 #ifdef NO_EXTERNAL_INDIRECT_ADDRESS
1597
1598 /* Re-definition of CONSTANT_ADDRESS_P, which is true only when there
1599    are no SYMBOL_REFs for external symbols present.  */
1600
1601 static bool
1602 indirectable_constant_address_p (rtx x, bool indirect)
1603 {
1604   if (GET_CODE (x) == SYMBOL_REF)
1605     return !flag_pic || SYMBOL_REF_LOCAL_P (x) || !indirect;
1606
1607   if (GET_CODE (x) == CONST)
1608     return !flag_pic
1609            || GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
1610            || SYMBOL_REF_LOCAL_P (XEXP (XEXP (x, 0), 0));
1611
1612   return CONSTANT_ADDRESS_P (x);
1613 }
1614
1615 #else /* not NO_EXTERNAL_INDIRECT_ADDRESS */
1616
1617 static bool
1618 indirectable_constant_address_p (rtx x, bool indirect ATTRIBUTE_UNUSED)
1619 {
1620   return CONSTANT_ADDRESS_P (x);
1621 }
1622
1623 #endif /* not NO_EXTERNAL_INDIRECT_ADDRESS */
1624
1625 /* True if X is an address which can be indirected.  External symbols
1626    could be in a sharable image library, so we disallow those.  */
1627
1628 static bool
1629 indirectable_address_p (rtx x, bool strict, bool indirect)
1630 {
1631   if (indirectable_constant_address_p (x, indirect)
1632       || BASE_REGISTER_P (x, strict))
1633     return true;
1634   if (GET_CODE (x) != PLUS
1635       || !BASE_REGISTER_P (XEXP (x, 0), strict)
1636       || (flag_pic && !CONST_INT_P (XEXP (x, 1))))
1637     return false;
1638   return indirectable_constant_address_p (XEXP (x, 1), indirect);
1639 }
1640
1641 /* Return true if x is a valid address not using indexing.
1642    (This much is the easy part.)  */
1643 static bool
1644 nonindexed_address_p (rtx x, bool strict)
1645 {
1646   rtx xfoo0;
1647   if (REG_P (x))
1648     {
1649       extern rtx *reg_equiv_mem;
1650       if (! reload_in_progress
1651           || reg_equiv_mem[REGNO (x)] == 0
1652           || indirectable_address_p (reg_equiv_mem[REGNO (x)], strict, false))
1653         return true;
1654     }
1655   if (indirectable_constant_address_p (x, false))
1656     return true;
1657   if (indirectable_address_p (x, strict, false))
1658     return true;
1659   xfoo0 = XEXP (x, 0);
1660   if (MEM_P (x) && indirectable_address_p (xfoo0, strict, true))
1661     return true;
1662   if ((GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_INC)
1663       && BASE_REGISTER_P (xfoo0, strict))
1664     return true;
1665   return false;
1666 }
1667
1668 /* True if PROD is either a reg times size of mode MODE and MODE is less
1669    than or equal 8 bytes, or just a reg if MODE is one byte.  */
1670
1671 static bool
1672 index_term_p (rtx prod, enum machine_mode mode, bool strict)
1673 {
1674   rtx xfoo0, xfoo1;
1675
1676   if (GET_MODE_SIZE (mode) == 1)
1677     return BASE_REGISTER_P (prod, strict);
1678
1679   if (GET_CODE (prod) != MULT || GET_MODE_SIZE (mode) > 8)
1680     return false;
1681
1682   xfoo0 = XEXP (prod, 0);
1683   xfoo1 = XEXP (prod, 1);
1684
1685   if (CONST_INT_P (xfoo0)
1686       && INTVAL (xfoo0) == (int)GET_MODE_SIZE (mode)
1687       && INDEX_REGISTER_P (xfoo1, strict))
1688     return true;
1689
1690   if (CONST_INT_P (xfoo1)
1691       && INTVAL (xfoo1) == (int)GET_MODE_SIZE (mode)
1692       && INDEX_REGISTER_P (xfoo0, strict))
1693     return true;
1694
1695   return false;
1696 }
1697
1698 /* Return true if X is the sum of a register
1699    and a valid index term for mode MODE.  */
1700 static bool
1701 reg_plus_index_p (rtx x, enum machine_mode mode, bool strict)
1702 {
1703   rtx xfoo0, xfoo1;
1704
1705   if (GET_CODE (x) != PLUS)
1706     return false;
1707
1708   xfoo0 = XEXP (x, 0);
1709   xfoo1 = XEXP (x, 1);
1710
1711   if (BASE_REGISTER_P (xfoo0, strict) && index_term_p (xfoo1, mode, strict))
1712     return true;
1713
1714   if (BASE_REGISTER_P (xfoo1, strict) && index_term_p (xfoo0, mode, strict))
1715     return true;
1716
1717   return false;
1718 }
1719
1720 /* Return true if xfoo0 and xfoo1 constitute a valid indexed address.  */
1721 static bool
1722 indexable_address_p (rtx xfoo0, rtx xfoo1, enum machine_mode mode, bool strict)
1723 {
1724   if (!CONSTANT_ADDRESS_P (xfoo0))
1725     return false;
1726   if (BASE_REGISTER_P (xfoo1, strict))
1727     return !flag_pic || mode == QImode;
1728   if (flag_pic && symbolic_operand (xfoo0, SImode))
1729     return false;
1730   return reg_plus_index_p (xfoo1, mode, strict);
1731 }
1732
1733 /* legitimate_address_p returns true if it recognizes an RTL expression "x"
1734    that is a valid memory address for an instruction.
1735    The MODE argument is the machine mode for the MEM expression
1736    that wants to use this address.  */
1737 bool
1738 vax_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1739 {
1740   rtx xfoo0, xfoo1;
1741
1742   if (nonindexed_address_p (x, strict))
1743     return true;
1744
1745   if (GET_CODE (x) != PLUS)
1746     return false;
1747
1748   /* Handle <address>[index] represented with index-sum outermost */
1749
1750   xfoo0 = XEXP (x, 0);
1751   xfoo1 = XEXP (x, 1);
1752
1753   if (index_term_p (xfoo0, mode, strict)
1754       && nonindexed_address_p (xfoo1, strict))
1755     return true;
1756
1757   if (index_term_p (xfoo1, mode, strict)
1758       && nonindexed_address_p (xfoo0, strict))
1759     return true;
1760
1761   /* Handle offset(reg)[index] with offset added outermost */
1762
1763   if (indexable_address_p (xfoo0, xfoo1, mode, strict)
1764       || indexable_address_p (xfoo1, xfoo0, mode, strict))
1765     return true;
1766
1767   return false;
1768 }
1769
1770 /* Return true if x (a legitimate address expression) has an effect that
1771    depends on the machine mode it is used for.  On the VAX, the predecrement
1772    and postincrement address depend thus (the amount of decrement or
1773    increment being the length of the operand) and all indexed address depend
1774    thus (because the index scale factor is the length of the operand).  */
1775
1776 bool
1777 vax_mode_dependent_address_p (rtx x)
1778 {
1779   rtx xfoo0, xfoo1;
1780
1781   /* Auto-increment cases are now dealt with generically in recog.c.  */
1782   if (GET_CODE (x) != PLUS)
1783     return false;
1784
1785   xfoo0 = XEXP (x, 0);
1786   xfoo1 = XEXP (x, 1);
1787
1788   if (CONST_INT_P (xfoo0) && REG_P (xfoo1))
1789     return false;
1790   if (CONST_INT_P (xfoo1) && REG_P (xfoo0))
1791     return false;
1792   if (!flag_pic && CONSTANT_ADDRESS_P (xfoo0) && REG_P (xfoo1))
1793     return false;
1794   if (!flag_pic && CONSTANT_ADDRESS_P (xfoo1) && REG_P (xfoo0))
1795     return false;
1796
1797   return true;
1798 }
1799
1800 static rtx
1801 fixup_mathdi_operand (rtx x, enum machine_mode mode)
1802 {
1803   if (illegal_addsub_di_memory_operand (x, mode))
1804     {
1805       rtx addr = XEXP (x, 0);
1806       rtx temp = gen_reg_rtx (Pmode);
1807       rtx offset = 0;
1808 #ifdef NO_EXTERNAL_INDIRECT_ADDRESS
1809       if (GET_CODE (addr) == CONST && flag_pic)
1810         {
1811           offset = XEXP (XEXP (addr, 0), 1);
1812           addr = XEXP (XEXP (addr, 0), 0);
1813         }
1814 #endif
1815       emit_move_insn (temp, addr);
1816       if (offset)
1817         temp = gen_rtx_PLUS (Pmode, temp, offset);
1818       x = gen_rtx_MEM (DImode, temp);
1819     }
1820   return x;
1821 }
1822
1823 void
1824 vax_expand_addsub_di_operands (rtx * operands, enum rtx_code code)
1825 {
1826   int hi_only = operand_subword (operands[2], 0, 0, DImode) == const0_rtx;
1827   rtx temp;
1828
1829   rtx (*gen_old_insn)(rtx, rtx, rtx);
1830   rtx (*gen_si_insn)(rtx, rtx, rtx);
1831   rtx (*gen_insn)(rtx, rtx, rtx);
1832
1833   if (code == PLUS)
1834     {
1835       gen_old_insn = gen_adddi3_old;
1836       gen_si_insn = gen_addsi3;
1837       gen_insn = gen_adcdi3;
1838     }
1839   else if (code == MINUS)
1840     {
1841       gen_old_insn = gen_subdi3_old;
1842       gen_si_insn = gen_subsi3;
1843       gen_insn = gen_sbcdi3;
1844     }
1845   else
1846     gcc_unreachable ();
1847
1848   /* If this is addition (thus operands are commutative) and if there is one
1849      addend that duplicates the desination, we want that addend to be the
1850      first addend.  */
1851   if (code == PLUS
1852       && rtx_equal_p (operands[0], operands[2])
1853       && !rtx_equal_p (operands[1], operands[2]))
1854     {
1855       temp = operands[2];
1856       operands[2] = operands[1];
1857       operands[1] = temp;
1858     }
1859
1860   if (!TARGET_QMATH)
1861     {
1862       emit_insn ((*gen_old_insn) (operands[0], operands[1], operands[2]));
1863     }
1864   else if (hi_only)
1865     {
1866       if (!rtx_equal_p (operands[0], operands[1])
1867           && (REG_P (operands[0]) && MEM_P (operands[1])))
1868         {
1869           emit_move_insn (operands[0], operands[1]);
1870           operands[1] = operands[0];
1871         }
1872
1873       operands[0] = fixup_mathdi_operand (operands[0], DImode);
1874       operands[1] = fixup_mathdi_operand (operands[1], DImode);
1875       operands[2] = fixup_mathdi_operand (operands[2], DImode);
1876
1877       if (!rtx_equal_p (operands[0], operands[1]))
1878         emit_move_insn (operand_subword (operands[0], 0, 0, DImode),
1879                           operand_subword (operands[1], 0, 0, DImode));
1880
1881       emit_insn ((*gen_si_insn) (operand_subword (operands[0], 1, 0, DImode),
1882                                  operand_subword (operands[1], 1, 0, DImode),
1883                                  operand_subword (operands[2], 1, 0, DImode)));
1884     }
1885   else
1886     {
1887       /* If are adding the same value together, that's really a multiply by 2,
1888          and that's just a left shift of 1.  */
1889       if (rtx_equal_p (operands[1], operands[2]))
1890         {
1891           gcc_assert (code != MINUS);
1892           emit_insn (gen_ashldi3 (operands[0], operands[1], const1_rtx));
1893           return;
1894         }
1895
1896       operands[0] = fixup_mathdi_operand (operands[0], DImode);
1897
1898       /* If an operand is the same as operand[0], use the operand[0] rtx
1899          because fixup will an equivalent rtx but not an equal one. */
1900
1901       if (rtx_equal_p (operands[0], operands[1]))
1902         operands[1] = operands[0];
1903       else
1904         operands[1] = fixup_mathdi_operand (operands[1], DImode);
1905
1906       if (rtx_equal_p (operands[0], operands[2]))
1907         operands[2] = operands[0];
1908       else
1909         operands[2] = fixup_mathdi_operand (operands[2], DImode);
1910
1911       /* If we are subtracting not from ourselves [d = a - b], and because the
1912          carry ops are two operand only, we would need to do a move prior to
1913          the subtract.  And if d == b, we would need a temp otherwise
1914          [d = a, d -= d] and we end up with 0.  Instead we rewrite d = a - b
1915          into d = -b, d += a.  Since -b can never overflow, even if b == d,
1916          no temp is needed.
1917
1918          If we are doing addition, since the carry ops are two operand, if
1919          we aren't adding to ourselves, move the first addend to the
1920          destination first.  */
1921
1922       gcc_assert (operands[1] != const0_rtx || code == MINUS);
1923       if (!rtx_equal_p (operands[0], operands[1]) && operands[1] != const0_rtx)
1924         {
1925           if (code == MINUS && CONSTANT_P (operands[1]))
1926             {
1927               temp = gen_reg_rtx (DImode);
1928               emit_insn (gen_sbcdi3 (operands[0], const0_rtx, operands[2]));
1929               code = PLUS;
1930               gen_insn = gen_adcdi3;
1931               operands[2] = operands[1];
1932               operands[1] = operands[0];
1933             }
1934           else
1935             emit_move_insn (operands[0], operands[1]);
1936         }
1937
1938       /* Subtracting a constant will have been rewritten to an addition of the
1939          negative of that constant before we get here.  */
1940       gcc_assert (!CONSTANT_P (operands[2]) || code == PLUS);
1941       emit_insn ((*gen_insn) (operands[0], operands[1], operands[2]));
1942     }
1943 }
1944
1945 bool
1946 adjacent_operands_p (rtx lo, rtx hi, enum machine_mode mode)
1947 {
1948   HOST_WIDE_INT lo_offset;
1949   HOST_WIDE_INT hi_offset;
1950
1951   if (GET_CODE (lo) != GET_CODE (hi))
1952     return false;
1953
1954   if (REG_P (lo))
1955     return mode == SImode && REGNO (lo) + 1 == REGNO (hi);
1956   if (CONST_INT_P (lo))
1957     return INTVAL (hi) == 0 && 0 <= INTVAL (lo) && INTVAL (lo) < 64;
1958   if (CONST_INT_P (lo))
1959     return mode != SImode;
1960
1961   if (!MEM_P (lo))
1962     return false;
1963
1964   if (MEM_VOLATILE_P (lo) || MEM_VOLATILE_P (hi))
1965     return false;
1966
1967   lo = XEXP (lo, 0);
1968   hi = XEXP (hi, 0);
1969
1970   if (GET_CODE (lo) == POST_INC /* || GET_CODE (lo) == PRE_DEC */)
1971     return rtx_equal_p (lo, hi);
1972
1973   switch (GET_CODE (lo))
1974     {
1975     case REG:
1976     case SYMBOL_REF:
1977       lo_offset = 0;
1978       break;
1979     case CONST:
1980       lo = XEXP (lo, 0);
1981       /* FALLTHROUGH */
1982     case PLUS:
1983       if (!CONST_INT_P (XEXP (lo, 1)))
1984         return false;
1985       lo_offset = INTVAL (XEXP (lo, 1));
1986       lo = XEXP (lo, 0);
1987       break;
1988     default:
1989       return false;
1990     }
1991
1992   switch (GET_CODE (hi))
1993     {
1994     case REG:
1995     case SYMBOL_REF:
1996       hi_offset = 0;
1997       break;
1998     case CONST:
1999       hi = XEXP (hi, 0);
2000       /* FALLTHROUGH */
2001     case PLUS:
2002       if (!CONST_INT_P (XEXP (hi, 1)))
2003         return false;
2004       hi_offset = INTVAL (XEXP (hi, 1));
2005       hi = XEXP (hi, 0);
2006       break;
2007     default:
2008       return false;
2009     }
2010
2011   if (GET_CODE (lo) == MULT || GET_CODE (lo) == PLUS)
2012     return false;
2013
2014   return rtx_equal_p (lo, hi)
2015          && hi_offset - lo_offset == GET_MODE_SIZE (mode);
2016 }