OSDN Git Service

* (hppa_init_pic_save): Emit the pic offset table
[pf3gnuchains/gcc-fork.git] / gcc / config / pa / pa.c
1 /* Subroutines for insn-output.c for HPPA.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Contributed by Tim Moore (moore@cs.utah.edu), based on sparc.c
5
6 This file is part of GNU CC.
7
8 GNU CC 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 2, or (at your option)
11 any later version.
12
13 GNU CC 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 GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "reload.h"
37 #include "expr.h"
38 #include "c-tree.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "toplev.h"
42 #include "ggc.h"
43 #include "recog.h"
44 #include "tm_p.h"
45
46 static void pa_init_machine_status PARAMS ((struct function *));
47 static void pa_mark_machine_status PARAMS ((struct function *));
48 static void pa_free_machine_status PARAMS ((struct function *));
49 static void pa_combine_instructions                     PARAMS ((rtx));
50 static int pa_can_combine_p     PARAMS ((rtx, rtx, rtx, int, rtx, rtx, rtx));
51 static int forward_branch_p                             PARAMS ((rtx));
52 static int shadd_constant_p                             PARAMS ((int));
53 static void pa_add_gc_roots                             PARAMS ((void));
54 static void mark_deferred_plabels                       PARAMS ((void *));
55 static void compute_zdepwi_operands                     PARAMS ((unsigned HOST_WIDE_INT, unsigned *));
56 static int compute_movstrsi_length                      PARAMS ((rtx));
57 static void remove_useless_addtr_insns                  PARAMS ((rtx, int));
58 static void store_reg                                   PARAMS ((int, int, int));
59 static void load_reg                                    PARAMS ((int, int, int));
60 static void set_reg_plus_d                              PARAMS ((int, int, int));
61
62 /* Save the operands last given to a compare for use when we
63    generate a scc or bcc insn.  */
64
65 rtx hppa_compare_op0, hppa_compare_op1;
66 enum cmp_type hppa_branch_type;
67
68 /* Which cpu we are scheduling for.  */
69 enum processor_type pa_cpu;
70
71 /* String to hold which cpu we are scheduling for.  */
72 const char *pa_cpu_string;
73
74 /* Which architecture we are generating code for.  */
75 enum architecture_type pa_arch;
76
77 /* String to hold which architecture we are generating code for.  */
78 const char *pa_arch_string;
79
80 /* Set by the FUNCTION_PROFILER macro. */
81 int hp_profile_labelno;
82
83 /* Counts for the number of callee-saved general and floating point
84    registers which were saved by the current function's prologue.  */
85 static int gr_saved, fr_saved;
86
87 static rtx find_addr_reg PARAMS ((rtx));
88
89 /* Keep track of the number of bytes we have output in the CODE subspaces
90    during this compilation so we'll know when to emit inline long-calls.  */
91
92 unsigned int total_code_bytes;
93
94 /* Variables to handle plabels that we discover are necessary at assembly
95    output time.  They are output after the current function.  */
96
97 struct deferred_plabel
98 {
99   rtx internal_label;
100   char *name;
101 } *deferred_plabels = 0;
102 int n_deferred_plabels = 0;
103
104 void
105 override_options ()
106 {
107   /* Default to 7100LC scheduling.  */
108   if (pa_cpu_string && ! strcmp (pa_cpu_string, "7100"))
109     {
110       pa_cpu_string = "7100";
111       pa_cpu = PROCESSOR_7100;
112     }
113   else if (pa_cpu_string && ! strcmp (pa_cpu_string, "700"))
114     {
115       pa_cpu_string = "700";
116       pa_cpu = PROCESSOR_700;
117     }
118   else if (pa_cpu_string == NULL
119          || ! strcmp (pa_cpu_string, "7100LC"))
120     {
121       pa_cpu_string = "7100LC";
122       pa_cpu = PROCESSOR_7100LC;
123     }
124   else if (pa_cpu_string && ! strcmp (pa_cpu_string, "7200"))
125     {
126       pa_cpu_string = "7200";
127       pa_cpu = PROCESSOR_7200;
128     }
129   else if (pa_cpu_string && ! strcmp (pa_cpu_string, "8000"))
130     {
131       pa_cpu_string = "8000";
132       pa_cpu = PROCESSOR_8000;
133     }
134   else
135     {
136       warning ("Unknown -mschedule= option (%s).\nValid options are 700, 7100, 7100LC, 7200, and 8000\n", pa_cpu_string);
137     }
138
139   /* Set the instruction set architecture.  */
140   if (pa_arch_string && ! strcmp (pa_arch_string, "1.0"))
141     {
142       pa_arch_string = "1.0";
143       pa_arch = ARCHITECTURE_10;
144       target_flags &= ~(MASK_PA_11 | MASK_PA_20);
145     }
146   else if (pa_arch_string && ! strcmp (pa_arch_string, "1.1"))
147     {
148       pa_arch_string = "1.1";
149       pa_arch = ARCHITECTURE_11;
150       target_flags &= ~MASK_PA_20;
151       target_flags |= MASK_PA_11;
152     }
153   else if (pa_arch_string && ! strcmp (pa_arch_string, "2.0"))
154     {
155       pa_arch_string = "2.0";
156       pa_arch = ARCHITECTURE_20;
157       target_flags |= MASK_PA_11 | MASK_PA_20;
158     }
159   else if (pa_arch_string)
160     {
161       warning ("Unknown -march= option (%s).\nValid options are 1.0, 1.1, and 2.0\n", pa_arch_string);
162     }
163
164   if (flag_pic && TARGET_PORTABLE_RUNTIME)
165     {
166       warning ("PIC code generation is not supported in the portable runtime model\n");
167     }
168
169   if (flag_pic && (TARGET_NO_SPACE_REGS || TARGET_FAST_INDIRECT_CALLS))
170    {
171       warning ("PIC code generation is not compatible with fast indirect calls\n");
172    }
173
174   if (flag_pic && profile_flag)
175     {
176       warning ("PIC code generation is not compatible with profiling\n");
177     }
178
179   if (! TARGET_GAS && write_symbols != NO_DEBUG)
180     {
181       warning ("-g is only supported when using GAS on this processor,");
182       warning ("-g option disabled.");
183       write_symbols = NO_DEBUG;
184     }
185
186   /* We always generate PIC code when in 64bit mode.  */
187   if (TARGET_64BIT)
188     flag_pic = 2;
189
190   /* Register global variables with the garbage collector.  */
191   pa_add_gc_roots ();
192
193   /* Arrange to save and restore machine status around nested functions.  */
194   init_machine_status = pa_init_machine_status;
195   mark_machine_status = pa_mark_machine_status;
196   free_machine_status = pa_free_machine_status;
197 }
198
199 /* Functions to initialize pic_offset_table_save_rtx.
200    These will be called, via pointer variables,
201    from push_function_context and pop_function_context.  */
202
203 static void
204 pa_init_machine_status (p)
205      struct function *p;
206 {
207   p->machine = (machine_function *) xmalloc (sizeof (machine_function));
208
209   p->machine->pic_offset_table_save_rtx = NULL_RTX;
210 }
211
212 static void
213 pa_mark_machine_status (p)
214      struct function *p;
215 {
216   if (p->machine)
217     ggc_mark_rtx (p->machine->pic_offset_table_save_rtx);
218 }
219
220 static void
221 pa_free_machine_status (p)
222      struct function *p;
223 {
224   if (p->machine == NULL)
225     return;
226
227   free (p->machine);
228   p->machine = NULL;
229 }
230
231
232 /* Return non-zero only if OP is a register of mode MODE,
233    or CONST0_RTX.  */
234 int
235 reg_or_0_operand (op, mode)
236      rtx op;
237      enum machine_mode mode;
238 {
239   return (op == CONST0_RTX (mode) || register_operand (op, mode));
240 }
241
242 /* Return non-zero if OP is suitable for use in a call to a named
243    function.
244
245    For 2.5 try to eliminate either call_operand_address or
246    function_label_operand, they perform very similar functions.  */
247 int
248 call_operand_address (op, mode)
249      rtx op;
250      enum machine_mode mode ATTRIBUTE_UNUSED;
251 {
252   return (GET_MODE (op) == word_mode
253           && CONSTANT_P (op) && ! TARGET_PORTABLE_RUNTIME);
254 }
255
256 /* Return 1 if X contains a symbolic expression.  We know these
257    expressions will have one of a few well defined forms, so
258    we need only check those forms.  */
259 int
260 symbolic_expression_p (x)
261      register rtx x;
262 {
263
264   /* Strip off any HIGH. */
265   if (GET_CODE (x) == HIGH)
266     x = XEXP (x, 0);
267
268   return (symbolic_operand (x, VOIDmode));
269 }
270
271 int
272 symbolic_operand (op, mode)
273      register rtx op;
274      enum machine_mode mode ATTRIBUTE_UNUSED;
275 {
276   switch (GET_CODE (op))
277     {
278     case SYMBOL_REF:
279     case LABEL_REF:
280       return 1;
281     case CONST:
282       op = XEXP (op, 0);
283       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
284                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
285               && GET_CODE (XEXP (op, 1)) == CONST_INT);
286     default:
287       return 0;
288     }
289 }
290
291 /* Return truth value of statement that OP is a symbolic memory
292    operand of mode MODE.  */
293
294 int
295 symbolic_memory_operand (op, mode)
296      rtx op;
297      enum machine_mode mode ATTRIBUTE_UNUSED;
298 {
299   if (GET_CODE (op) == SUBREG)
300     op = SUBREG_REG (op);
301   if (GET_CODE (op) != MEM)
302     return 0;
303   op = XEXP (op, 0);
304   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST
305           || GET_CODE (op) == HIGH || GET_CODE (op) == LABEL_REF);
306 }
307
308 /* Return 1 if the operand is either a register or a memory operand that is
309    not symbolic.  */
310
311 int
312 reg_or_nonsymb_mem_operand (op, mode)
313     register rtx op;
314     enum machine_mode mode;
315 {
316   if (register_operand (op, mode))
317     return 1;
318
319   if (memory_operand (op, mode) && ! symbolic_memory_operand (op, mode))
320     return 1;
321
322   return 0;
323 }
324
325 /* Return 1 if the operand is either a register, zero, or a memory operand
326    that is not symbolic.  */
327
328 int
329 reg_or_0_or_nonsymb_mem_operand (op, mode)
330     register rtx op;
331     enum machine_mode mode;
332 {
333   if (register_operand (op, mode))
334     return 1;
335
336   if (op == CONST0_RTX (mode))
337     return 1;
338
339   if (memory_operand (op, mode) && ! symbolic_memory_operand (op, mode))
340     return 1;
341
342   return 0;
343 }
344
345 /* Accept any constant that can be moved in one instructions into a
346    general register.  */
347 int
348 cint_ok_for_move (intval)
349      HOST_WIDE_INT intval;
350 {
351   /* OK if ldo, ldil, or zdepi, can be used.  */
352   return (CONST_OK_FOR_LETTER_P (intval, 'J')
353           || CONST_OK_FOR_LETTER_P (intval, 'N')
354           || CONST_OK_FOR_LETTER_P (intval, 'K'));
355 }
356
357 /* Accept anything that can be moved in one instruction into a general
358    register.  */
359 int
360 move_operand (op, mode)
361      rtx op;
362      enum machine_mode mode;
363 {
364   if (register_operand (op, mode))
365     return 1;
366
367   if (GET_CODE (op) == CONSTANT_P_RTX)
368     return 1;
369
370   if (GET_CODE (op) == CONST_INT)
371     return cint_ok_for_move (INTVAL (op));
372
373   if (GET_CODE (op) == SUBREG)
374     op = SUBREG_REG (op);
375   if (GET_CODE (op) != MEM)
376     return 0;
377
378   op = XEXP (op, 0);
379
380   /* The LO_SUM part of a DLT reference is not considered a move_operand;
381      we must reject it here since it must be accepted by memory_address_p.  */
382   if (GET_CODE (op) == LO_SUM
383       && GET_CODE (XEXP (op, 0)) == REG
384       && REG_OK_FOR_BASE_P (XEXP (op, 0))
385       && GET_CODE (XEXP (op, 1)) == UNSPEC)
386     return 0;
387
388   /* Since move_operand is only used for source operands, we can always
389      allow scaled indexing!  */
390   if (! TARGET_DISABLE_INDEXING
391       && GET_CODE (op) == PLUS
392       && ((GET_CODE (XEXP (op, 0)) == MULT
393            && GET_CODE (XEXP (XEXP (op, 0), 0)) == REG
394            && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
395            && INTVAL (XEXP (XEXP (op, 0), 1)) == GET_MODE_SIZE (mode)
396            && GET_CODE (XEXP (op, 1)) == REG)
397           || (GET_CODE (XEXP (op, 1)) == MULT
398               &&GET_CODE (XEXP (XEXP (op, 1), 0)) == REG
399               && GET_CODE (XEXP (XEXP (op, 1), 1)) == CONST_INT
400               && INTVAL (XEXP (XEXP (op, 1), 1)) == GET_MODE_SIZE (mode)
401               && GET_CODE (XEXP (op, 0)) == REG)))
402     return 1;
403
404   return memory_address_p (mode, op);
405 }
406
407 /* Accept REG and any CONST_INT that can be moved in one instruction into a
408    general register.  */
409 int
410 reg_or_cint_move_operand (op, mode)
411      rtx op;
412      enum machine_mode mode;
413 {
414   if (register_operand (op, mode))
415     return 1;
416
417   if (GET_CODE (op) == CONST_INT)
418     return cint_ok_for_move (INTVAL (op));
419
420   return 0;
421 }
422
423 int
424 pic_label_operand (op, mode)
425      rtx op;
426      enum machine_mode mode ATTRIBUTE_UNUSED;
427 {
428   if (!flag_pic)
429     return 0;
430
431   switch (GET_CODE (op))
432     {
433     case LABEL_REF:
434       return 1;
435     case CONST:
436       op = XEXP (op, 0);
437       return (GET_CODE (XEXP (op, 0)) == LABEL_REF
438               && GET_CODE (XEXP (op, 1)) == CONST_INT);
439     default:
440       return 0;
441     }
442 }
443
444 int
445 fp_reg_operand (op, mode)
446      rtx op;
447      enum machine_mode mode ATTRIBUTE_UNUSED;
448 {
449   return reg_renumber && FP_REG_P (op);
450 }
451
452 \f
453
454 /* Return truth value of whether OP can be used as an operand in a
455    three operand arithmetic insn that accepts registers of mode MODE
456    or 14-bit signed integers.  */
457 int
458 arith_operand (op, mode)
459      rtx op;
460      enum machine_mode mode;
461 {
462   return (register_operand (op, mode)
463           || (GET_CODE (op) == CONST_INT && INT_14_BITS (op)));
464 }
465
466 /* Return truth value of whether OP can be used as an operand in a
467    three operand arithmetic insn that accepts registers of mode MODE
468    or 11-bit signed integers.  */
469 int
470 arith11_operand (op, mode)
471      rtx op;
472      enum machine_mode mode;
473 {
474   return (register_operand (op, mode)
475           || (GET_CODE (op) == CONST_INT && INT_11_BITS (op)));
476 }
477
478 /* A constant integer suitable for use in a PRE_MODIFY memory
479    reference.  */
480 int
481 pre_cint_operand (op, mode)
482      rtx op;
483      enum machine_mode mode ATTRIBUTE_UNUSED;
484 {
485   return (GET_CODE (op) == CONST_INT
486           && INTVAL (op) >= -0x2000 && INTVAL (op) < 0x10);
487 }
488
489 /* A constant integer suitable for use in a POST_MODIFY memory
490    reference.  */
491 int
492 post_cint_operand (op, mode)
493      rtx op;
494      enum machine_mode mode ATTRIBUTE_UNUSED;
495 {
496   return (GET_CODE (op) == CONST_INT
497           && INTVAL (op) < 0x2000 && INTVAL (op) >= -0x10);
498 }
499
500 int
501 arith_double_operand (op, mode)
502      rtx op;
503      enum machine_mode mode;
504 {
505   return (register_operand (op, mode)
506           || (GET_CODE (op) == CONST_DOUBLE
507               && GET_MODE (op) == mode
508               && VAL_14_BITS_P (CONST_DOUBLE_LOW (op))
509               && ((CONST_DOUBLE_HIGH (op) >= 0)
510                   == ((CONST_DOUBLE_LOW (op) & 0x1000) == 0))));
511 }
512
513 /* Return truth value of whether OP is a integer which fits the
514    range constraining immediate operands in three-address insns, or
515    is an integer register.  */
516
517 int
518 ireg_or_int5_operand (op, mode)
519      rtx op;
520      enum machine_mode mode ATTRIBUTE_UNUSED;
521 {
522   return ((GET_CODE (op) == CONST_INT && INT_5_BITS (op))
523           || (GET_CODE (op) == REG && REGNO (op) > 0 && REGNO (op) < 32));
524 }
525
526 /* Return nonzero if OP is an integer register, else return zero.  */
527 int
528 ireg_operand (op, mode)
529      rtx op;
530      enum machine_mode mode ATTRIBUTE_UNUSED;
531 {
532   return (GET_CODE (op) == REG && REGNO (op) > 0 && REGNO (op) < 32);
533 }
534
535 /* Return truth value of whether OP is a integer which fits the
536    range constraining immediate operands in three-address insns.  */
537
538 int
539 int5_operand (op, mode)
540      rtx op;
541      enum machine_mode mode ATTRIBUTE_UNUSED;
542 {
543   return (GET_CODE (op) == CONST_INT && INT_5_BITS (op));
544 }
545
546 int
547 uint5_operand (op, mode)
548      rtx op;
549      enum machine_mode mode ATTRIBUTE_UNUSED;
550 {
551   return (GET_CODE (op) == CONST_INT && INT_U5_BITS (op));
552 }
553
554 int
555 int11_operand (op, mode)
556      rtx op;
557      enum machine_mode mode ATTRIBUTE_UNUSED;
558 {
559   return (GET_CODE (op) == CONST_INT && INT_11_BITS (op));
560 }
561
562 int
563 uint32_operand (op, mode)
564      rtx op;
565      enum machine_mode mode ATTRIBUTE_UNUSED;
566 {
567 #if HOST_BITS_PER_WIDE_INT > 32
568   /* All allowed constants will fit a CONST_INT.  */
569   return (GET_CODE (op) == CONST_INT
570           && (INTVAL (op) >= 0 && INTVAL (op) < 0x100000000L));
571 #else
572   return (GET_CODE (op) == CONST_INT
573           || (GET_CODE (op) == CONST_DOUBLE
574               && CONST_DOUBLE_HIGH (op) == 0));
575 #endif
576 }
577
578 int
579 arith5_operand (op, mode)
580      rtx op;
581      enum machine_mode mode;
582 {
583   return register_operand (op, mode) || int5_operand (op, mode);
584 }
585
586 /* True iff zdepi can be used to generate this CONST_INT.  */
587 int
588 zdepi_cint_p (x)
589      unsigned HOST_WIDE_INT x;
590 {
591   unsigned HOST_WIDE_INT lsb_mask, t;
592
593   /* This might not be obvious, but it's at least fast.
594      This function is critical; we don't have the time loops would take.  */
595   lsb_mask = x & -x;
596   t = ((x >> 4) + lsb_mask) & ~(lsb_mask - 1);
597   /* Return true iff t is a power of two.  */
598   return ((t & (t - 1)) == 0);
599 }
600
601 /* True iff depi or extru can be used to compute (reg & mask).
602    Accept bit pattern like these:
603    0....01....1
604    1....10....0
605    1..10..01..1  */
606 int
607 and_mask_p (mask)
608      unsigned HOST_WIDE_INT mask;
609 {
610   mask = ~mask;
611   mask += mask & -mask;
612   return (mask & (mask - 1)) == 0;
613 }
614
615 /* True iff depi or extru can be used to compute (reg & OP).  */
616 int
617 and_operand (op, mode)
618      rtx op;
619      enum machine_mode mode;
620 {
621   return (register_operand (op, mode)
622           || (GET_CODE (op) == CONST_INT && and_mask_p (INTVAL (op))));
623 }
624
625 /* True iff depi can be used to compute (reg | MASK).  */
626 int
627 ior_mask_p (mask)
628      unsigned HOST_WIDE_INT mask;
629 {
630   mask += mask & -mask;
631   return (mask & (mask - 1)) == 0;
632 }
633
634 /* True iff depi can be used to compute (reg | OP).  */
635 int
636 ior_operand (op, mode)
637      rtx op;
638      enum machine_mode mode ATTRIBUTE_UNUSED;
639 {
640   return (GET_CODE (op) == CONST_INT && ior_mask_p (INTVAL (op)));
641 }
642
643 int
644 lhs_lshift_operand (op, mode)
645      rtx op;
646      enum machine_mode mode;
647 {
648   return register_operand (op, mode) || lhs_lshift_cint_operand (op, mode);
649 }
650
651 /* True iff OP is a CONST_INT of the forms 0...0xxxx or 0...01...1xxxx.
652    Such values can be the left hand side x in (x << r), using the zvdepi
653    instruction.  */
654 int
655 lhs_lshift_cint_operand (op, mode)
656      rtx op;
657      enum machine_mode mode ATTRIBUTE_UNUSED;
658 {
659   unsigned HOST_WIDE_INT x;
660   if (GET_CODE (op) != CONST_INT)
661     return 0;
662   x = INTVAL (op) >> 4;
663   return (x & (x + 1)) == 0;
664 }
665
666 int
667 arith32_operand (op, mode)
668      rtx op;
669      enum machine_mode mode;
670 {
671   return register_operand (op, mode) || GET_CODE (op) == CONST_INT;
672 }
673
674 int
675 pc_or_label_operand (op, mode)
676      rtx op;
677      enum machine_mode mode ATTRIBUTE_UNUSED;
678 {
679   return (GET_CODE (op) == PC || GET_CODE (op) == LABEL_REF);
680 }
681 \f
682 /* Legitimize PIC addresses.  If the address is already
683    position-independent, we return ORIG.  Newly generated
684    position-independent addresses go to REG.  If we need more
685    than one register, we lose.  */
686
687 rtx
688 legitimize_pic_address (orig, mode, reg)
689      rtx orig, reg;
690      enum machine_mode mode;
691 {
692   rtx pic_ref = orig;
693
694   /* Labels need special handling.  */
695   if (pic_label_operand (orig, mode))
696     {
697       /* We do not want to go through the movXX expanders here since that
698          would create recursion.
699
700          Nor do we really want to call a generator for a named pattern
701          since that requires multiple patterns if we want to support
702          multiple word sizes.
703
704          So instead we just emit the raw set, which avoids the movXX
705          expanders completely.  */
706       emit_insn (gen_rtx_SET (VOIDmode, reg, orig));
707       current_function_uses_pic_offset_table = 1;
708       return reg;
709     }
710   if (GET_CODE (orig) == SYMBOL_REF)
711     {
712       if (reg == 0)
713         abort ();
714
715       if (flag_pic == 2)
716         {
717           emit_move_insn (reg,
718                           gen_rtx_PLUS (word_mode, pic_offset_table_rtx,
719                                         gen_rtx_HIGH (word_mode, orig)));
720           pic_ref
721             = gen_rtx_MEM (Pmode,
722                            gen_rtx_LO_SUM (Pmode, reg,
723                                            gen_rtx_UNSPEC (Pmode,
724                                                            gen_rtvec (1, orig),
725                                                            0)));
726         }
727       else
728         pic_ref = gen_rtx_MEM (Pmode,
729                                gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
730                                              orig));
731
732       current_function_uses_pic_offset_table = 1;
733       RTX_UNCHANGING_P (pic_ref) = 1;
734       emit_move_insn (reg, pic_ref);
735       return reg;
736     }
737   else if (GET_CODE (orig) == CONST)
738     {
739       rtx base;
740
741       if (GET_CODE (XEXP (orig, 0)) == PLUS
742           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
743         return orig;
744
745       if (reg == 0)
746         abort ();
747
748       if (GET_CODE (XEXP (orig, 0)) == PLUS)
749         {
750           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
751           orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
752                                          base == reg ? 0 : reg);
753         }
754       else abort ();
755       if (GET_CODE (orig) == CONST_INT)
756         {
757           if (INT_14_BITS (orig))
758             return plus_constant_for_output (base, INTVAL (orig));
759           orig = force_reg (Pmode, orig);
760         }
761       pic_ref = gen_rtx_PLUS (Pmode, base, orig);
762       /* Likewise, should we set special REG_NOTEs here?  */
763     }
764   return pic_ref;
765 }
766
767 /* Try machine-dependent ways of modifying an illegitimate address
768    to be legitimate.  If we find one, return the new, valid address.
769    This macro is used in only one place: `memory_address' in explow.c.
770
771    OLDX is the address as it was before break_out_memory_refs was called.
772    In some cases it is useful to look at this to decide what needs to be done.
773
774    MODE and WIN are passed so that this macro can use
775    GO_IF_LEGITIMATE_ADDRESS.
776
777    It is always safe for this macro to do nothing.  It exists to recognize
778    opportunities to optimize the output.
779
780    For the PA, transform:
781
782         memory(X + <large int>)
783
784    into:
785
786         if (<large int> & mask) >= 16
787           Y = (<large int> & ~mask) + mask + 1  Round up.
788         else
789           Y = (<large int> & ~mask)             Round down.
790         Z = X + Y
791         memory (Z + (<large int> - Y));
792
793    This is for CSE to find several similar references, and only use one Z.
794
795    X can either be a SYMBOL_REF or REG, but because combine can not
796    perform a 4->2 combination we do nothing for SYMBOL_REF + D where
797    D will not fit in 14 bits.
798
799    MODE_FLOAT references allow displacements which fit in 5 bits, so use
800    0x1f as the mask.
801
802    MODE_INT references allow displacements which fit in 14 bits, so use
803    0x3fff as the mask.
804
805    This relies on the fact that most mode MODE_FLOAT references will use FP
806    registers and most mode MODE_INT references will use integer registers.
807    (In the rare case of an FP register used in an integer MODE, we depend
808    on secondary reloads to clean things up.)
809
810
811    It is also beneficial to handle (plus (mult (X) (Y)) (Z)) in a special
812    manner if Y is 2, 4, or 8.  (allows more shadd insns and shifted indexed
813    addressing modes to be used).
814
815    Put X and Z into registers.  Then put the entire expression into
816    a register.  */
817
818 rtx
819 hppa_legitimize_address (x, oldx, mode)
820      rtx x, oldx ATTRIBUTE_UNUSED;
821      enum machine_mode mode;
822 {
823   rtx orig = x;
824
825   if (flag_pic)
826     return legitimize_pic_address (x, mode, gen_reg_rtx (Pmode));
827
828   /* Strip off CONST. */
829   if (GET_CODE (x) == CONST)
830     x = XEXP (x, 0);
831
832   /* Special case.  Get the SYMBOL_REF into a register and use indexing.
833      That should always be safe.  */
834   if (GET_CODE (x) == PLUS
835       && GET_CODE (XEXP (x, 0)) == REG
836       && GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
837     {
838       rtx reg = force_reg (Pmode, XEXP (x, 1));
839       return force_reg (Pmode, gen_rtx_PLUS (Pmode, reg, XEXP (x, 0)));
840     }
841
842   /* Note we must reject symbols which represent function addresses
843      since the assembler/linker can't handle arithmetic on plabels.  */
844   if (GET_CODE (x) == PLUS
845       && GET_CODE (XEXP (x, 1)) == CONST_INT
846       && ((GET_CODE (XEXP (x, 0)) == SYMBOL_REF
847            && !FUNCTION_NAME_P (XSTR (XEXP (x, 0), 0)))
848           || GET_CODE (XEXP (x, 0)) == REG))
849     {
850       rtx int_part, ptr_reg;
851       int newoffset;
852       int offset = INTVAL (XEXP (x, 1));
853       int mask;
854
855       mask = (GET_MODE_CLASS (mode) == MODE_FLOAT
856               ? (TARGET_PA_20 ? 0x3fff : 0x1f) : 0x3fff);
857
858       /* Choose which way to round the offset.  Round up if we
859          are >= halfway to the next boundary.  */
860       if ((offset & mask) >= ((mask + 1) / 2))
861         newoffset = (offset & ~ mask) + mask + 1;
862       else
863         newoffset = (offset & ~ mask);
864
865       /* If the newoffset will not fit in 14 bits (ldo), then
866          handling this would take 4 or 5 instructions (2 to load
867          the SYMBOL_REF + 1 or 2 to load the newoffset + 1 to
868          add the new offset and the SYMBOL_REF.)  Combine can
869          not handle 4->2 or 5->2 combinations, so do not create
870          them.  */
871       if (! VAL_14_BITS_P (newoffset)
872           && GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
873         {
874           rtx const_part = plus_constant (XEXP (x, 0), newoffset);
875           rtx tmp_reg
876             = force_reg (Pmode,
877                          gen_rtx_HIGH (Pmode, const_part));
878           ptr_reg
879             = force_reg (Pmode,
880                          gen_rtx_LO_SUM (Pmode,
881                                          tmp_reg, const_part));
882         }
883       else
884         {
885           if (! VAL_14_BITS_P (newoffset))
886             int_part = force_reg (Pmode, GEN_INT (newoffset));
887           else
888             int_part = GEN_INT (newoffset);
889
890           ptr_reg = force_reg (Pmode,
891                                gen_rtx_PLUS (Pmode,
892                                              force_reg (Pmode, XEXP (x, 0)),
893                                              int_part));
894         }
895       return plus_constant (ptr_reg, offset - newoffset);
896     }
897
898   /* Handle (plus (mult (a) (shadd_constant)) (b)).  */
899
900   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT
901       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
902       && shadd_constant_p (INTVAL (XEXP (XEXP (x, 0), 1)))
903       && (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == 'o'
904           || GET_CODE (XEXP (x, 1)) == SUBREG)
905       && GET_CODE (XEXP (x, 1)) != CONST)
906     {
907       int val = INTVAL (XEXP (XEXP (x, 0), 1));
908       rtx reg1, reg2;
909
910       reg1 = XEXP (x, 1);
911       if (GET_CODE (reg1) != REG)
912         reg1 = force_reg (Pmode, force_operand (reg1, 0));
913
914       reg2 = XEXP (XEXP (x, 0), 0);
915       if (GET_CODE (reg2) != REG)
916         reg2 = force_reg (Pmode, force_operand (reg2, 0));
917
918       return force_reg (Pmode, gen_rtx_PLUS (Pmode,
919                                              gen_rtx_MULT (Pmode,
920                                                            reg2,
921                                                            GEN_INT (val)),
922                                              reg1));
923     }
924
925   /* Similarly for (plus (plus (mult (a) (shadd_constant)) (b)) (c)).
926
927      Only do so for floating point modes since this is more speculative
928      and we lose if it's an integer store.  */
929   if (GET_CODE (x) == PLUS
930       && GET_CODE (XEXP (x, 0)) == PLUS
931       && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
932       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
933       && shadd_constant_p (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1)))
934       && (mode == SFmode || mode == DFmode))
935     {
936
937       /* First, try and figure out what to use as a base register.  */
938       rtx reg1, reg2, base, idx, orig_base;
939
940       reg1 = XEXP (XEXP (x, 0), 1);
941       reg2 = XEXP (x, 1);
942       base = NULL_RTX;
943       idx = NULL_RTX;
944
945       /* Make sure they're both regs.  If one was a SYMBOL_REF [+ const],
946          then emit_move_sequence will turn on REG_POINTER so we'll know
947          it's a base register below.  */
948       if (GET_CODE (reg1) != REG)
949         reg1 = force_reg (Pmode, force_operand (reg1, 0));
950
951       if (GET_CODE (reg2) != REG)
952         reg2 = force_reg (Pmode, force_operand (reg2, 0));
953
954       /* Figure out what the base and index are.  */
955          
956       if (GET_CODE (reg1) == REG
957           && REG_POINTER (reg1))
958         {
959           base = reg1;
960           orig_base = XEXP (XEXP (x, 0), 1);
961           idx = gen_rtx_PLUS (Pmode,
962                               gen_rtx_MULT (Pmode,
963                                             XEXP (XEXP (XEXP (x, 0), 0), 0),
964                                             XEXP (XEXP (XEXP (x, 0), 0), 1)),
965                               XEXP (x, 1));
966         }
967       else if (GET_CODE (reg2) == REG
968                && REG_POINTER (reg2))
969         {
970           base = reg2;
971           orig_base = XEXP (x, 1);
972           idx = XEXP (x, 0);
973         }
974
975       if (base == 0)
976         return orig;
977
978       /* If the index adds a large constant, try to scale the
979          constant so that it can be loaded with only one insn.  */
980       if (GET_CODE (XEXP (idx, 1)) == CONST_INT
981           && VAL_14_BITS_P (INTVAL (XEXP (idx, 1))
982                             / INTVAL (XEXP (XEXP (idx, 0), 1)))
983           && INTVAL (XEXP (idx, 1)) % INTVAL (XEXP (XEXP (idx, 0), 1)) == 0)
984         {
985           /* Divide the CONST_INT by the scale factor, then add it to A.  */
986           int val = INTVAL (XEXP (idx, 1));
987
988           val /= INTVAL (XEXP (XEXP (idx, 0), 1));
989           reg1 = XEXP (XEXP (idx, 0), 0);
990           if (GET_CODE (reg1) != REG)
991             reg1 = force_reg (Pmode, force_operand (reg1, 0));
992
993           reg1 = force_reg (Pmode, gen_rtx_PLUS (Pmode, reg1, GEN_INT (val)));
994
995           /* We can now generate a simple scaled indexed address.  */
996           return
997             force_reg
998               (Pmode, gen_rtx_PLUS (Pmode,
999                                     gen_rtx_MULT (Pmode, reg1,
1000                                                   XEXP (XEXP (idx, 0), 1)),
1001                                     base));
1002         }
1003
1004       /* If B + C is still a valid base register, then add them.  */
1005       if (GET_CODE (XEXP (idx, 1)) == CONST_INT
1006           && INTVAL (XEXP (idx, 1)) <= 4096
1007           && INTVAL (XEXP (idx, 1)) >= -4096)
1008         {
1009           int val = INTVAL (XEXP (XEXP (idx, 0), 1));
1010           rtx reg1, reg2;
1011
1012           reg1 = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, XEXP (idx, 1)));
1013
1014           reg2 = XEXP (XEXP (idx, 0), 0);
1015           if (GET_CODE (reg2) != CONST_INT)
1016             reg2 = force_reg (Pmode, force_operand (reg2, 0));
1017
1018           return force_reg (Pmode, gen_rtx_PLUS (Pmode,
1019                                                  gen_rtx_MULT (Pmode,
1020                                                                reg2,
1021                                                                GEN_INT (val)),
1022                                                  reg1));
1023         }
1024
1025       /* Get the index into a register, then add the base + index and
1026          return a register holding the result.  */
1027
1028       /* First get A into a register.  */
1029       reg1 = XEXP (XEXP (idx, 0), 0);
1030       if (GET_CODE (reg1) != REG)
1031         reg1 = force_reg (Pmode, force_operand (reg1, 0));
1032
1033       /* And get B into a register.  */
1034       reg2 = XEXP (idx, 1);
1035       if (GET_CODE (reg2) != REG)
1036         reg2 = force_reg (Pmode, force_operand (reg2, 0));
1037
1038       reg1 = force_reg (Pmode,
1039                         gen_rtx_PLUS (Pmode,
1040                                       gen_rtx_MULT (Pmode, reg1,
1041                                                     XEXP (XEXP (idx, 0), 1)),
1042                                       reg2));
1043
1044       /* Add the result to our base register and return.  */
1045       return force_reg (Pmode, gen_rtx_PLUS (Pmode, base, reg1));
1046       
1047     }
1048
1049   /* Uh-oh.  We might have an address for x[n-100000].  This needs
1050      special handling to avoid creating an indexed memory address
1051      with x-100000 as the base.
1052     
1053      If the constant part is small enough, then it's still safe because
1054      there is a guard page at the beginning and end of the data segment.
1055
1056      Scaled references are common enough that we want to try and rearrange the
1057      terms so that we can use indexing for these addresses too.  Only
1058      do the optimization for floatint point modes.  */
1059
1060   if (GET_CODE (x) == PLUS
1061       && symbolic_expression_p (XEXP (x, 1)))
1062     {
1063       /* Ugly.  We modify things here so that the address offset specified
1064          by the index expression is computed first, then added to x to form
1065          the entire address.  */
1066
1067       rtx regx1, regx2, regy1, regy2, y;
1068
1069       /* Strip off any CONST.  */
1070       y = XEXP (x, 1);
1071       if (GET_CODE (y) == CONST)
1072         y = XEXP (y, 0);
1073
1074       if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1075         {
1076           /* See if this looks like
1077                 (plus (mult (reg) (shadd_const))
1078                       (const (plus (symbol_ref) (const_int))))
1079
1080              Where const_int is small.  In that case the const
1081              expression is a valid pointer for indexing. 
1082
1083              If const_int is big, but can be divided evenly by shadd_const
1084              and added to (reg).  This allows more scaled indexed addresses.  */
1085           if (GET_CODE (XEXP (y, 0)) == SYMBOL_REF
1086               && GET_CODE (XEXP (x, 0)) == MULT
1087               && GET_CODE (XEXP (y, 1)) == CONST_INT
1088               && INTVAL (XEXP (y, 1)) >= -4096
1089               && INTVAL (XEXP (y, 1)) <= 4095
1090               && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1091               && shadd_constant_p (INTVAL (XEXP (XEXP (x, 0), 1))))
1092             {
1093               int val = INTVAL (XEXP (XEXP (x, 0), 1));
1094               rtx reg1, reg2;
1095
1096               reg1 = XEXP (x, 1);
1097               if (GET_CODE (reg1) != REG)
1098                 reg1 = force_reg (Pmode, force_operand (reg1, 0));
1099
1100               reg2 = XEXP (XEXP (x, 0), 0);
1101               if (GET_CODE (reg2) != REG)
1102                 reg2 = force_reg (Pmode, force_operand (reg2, 0));
1103
1104               return force_reg (Pmode,
1105                                 gen_rtx_PLUS (Pmode,
1106                                               gen_rtx_MULT (Pmode,
1107                                                             reg2,
1108                                                             GEN_INT (val)),
1109                                               reg1));
1110             }
1111           else if ((mode == DFmode || mode == SFmode)
1112                    && GET_CODE (XEXP (y, 0)) == SYMBOL_REF
1113                    && GET_CODE (XEXP (x, 0)) == MULT
1114                    && GET_CODE (XEXP (y, 1)) == CONST_INT
1115                    && INTVAL (XEXP (y, 1)) % INTVAL (XEXP (XEXP (x, 0), 1)) == 0
1116                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1117                    && shadd_constant_p (INTVAL (XEXP (XEXP (x, 0), 1))))
1118             {
1119               regx1
1120                 = force_reg (Pmode, GEN_INT (INTVAL (XEXP (y, 1))
1121                                              / INTVAL (XEXP (XEXP (x, 0), 1))));
1122               regx2 = XEXP (XEXP (x, 0), 0);
1123               if (GET_CODE (regx2) != REG)
1124                 regx2 = force_reg (Pmode, force_operand (regx2, 0));
1125               regx2 = force_reg (Pmode, gen_rtx_fmt_ee (GET_CODE (y), Pmode,
1126                                                         regx2, regx1));
1127               return
1128                 force_reg (Pmode,
1129                            gen_rtx_PLUS (Pmode,
1130                                          gen_rtx_MULT (Pmode, regx2,
1131                                                        XEXP (XEXP (x, 0), 1)),
1132                                          force_reg (Pmode, XEXP (y, 0))));
1133             }
1134           else if (GET_CODE (XEXP (y, 1)) == CONST_INT
1135                    && INTVAL (XEXP (y, 1)) >= -4096
1136                    && INTVAL (XEXP (y, 1)) <= 4095)
1137             {
1138               /* This is safe because of the guard page at the
1139                  beginning and end of the data space.  Just
1140                  return the original address.  */
1141               return orig;
1142             }
1143           else
1144             {
1145               /* Doesn't look like one we can optimize.  */
1146               regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1147               regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1148               regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1149               regx1 = force_reg (Pmode,
1150                                  gen_rtx_fmt_ee (GET_CODE (y), Pmode,
1151                                                  regx1, regy2));
1152               return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1153             }
1154         }
1155     }
1156
1157   return orig;
1158 }
1159
1160 /* For the HPPA, REG and REG+CONST is cost 0
1161    and addresses involving symbolic constants are cost 2.
1162
1163    PIC addresses are very expensive.
1164
1165    It is no coincidence that this has the same structure
1166    as GO_IF_LEGITIMATE_ADDRESS.  */
1167 int
1168 hppa_address_cost (X)
1169      rtx X;
1170 {
1171   if (GET_CODE (X) == PLUS)
1172       return 1;
1173   else if (GET_CODE (X) == LO_SUM)
1174     return 1;
1175   else if (GET_CODE (X) == HIGH)
1176     return 2;
1177   return 4;
1178 }
1179
1180 /* Emit insns to move operands[1] into operands[0].
1181
1182    Return 1 if we have written out everything that needs to be done to
1183    do the move.  Otherwise, return 0 and the caller will emit the move
1184    normally. 
1185
1186    Note SCRATCH_REG may not be in the proper mode depending on how it
1187    will be used.  This routine is resposible for creating a new copy
1188    of SCRATCH_REG in the proper mode.  */
1189
1190 int
1191 emit_move_sequence (operands, mode, scratch_reg)
1192      rtx *operands;
1193      enum machine_mode mode;
1194      rtx scratch_reg;
1195 {
1196   register rtx operand0 = operands[0];
1197   register rtx operand1 = operands[1];
1198   register rtx tem;
1199
1200   if (scratch_reg
1201       && reload_in_progress && GET_CODE (operand0) == REG
1202       && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
1203     operand0 = reg_equiv_mem[REGNO (operand0)];
1204   else if (scratch_reg
1205            && reload_in_progress && GET_CODE (operand0) == SUBREG
1206            && GET_CODE (SUBREG_REG (operand0)) == REG
1207            && REGNO (SUBREG_REG (operand0)) >= FIRST_PSEUDO_REGISTER)
1208     {
1209      /* We must not alter SUBREG_WORD (operand0) since that would confuse
1210         the code which tracks sets/uses for delete_output_reload.  */
1211       rtx temp = gen_rtx_SUBREG (GET_MODE (operand0),
1212                                  reg_equiv_mem [REGNO (SUBREG_REG (operand0))],
1213                                  SUBREG_WORD (operand0));
1214       operand0 = alter_subreg (temp);
1215     }
1216
1217   if (scratch_reg
1218       && reload_in_progress && GET_CODE (operand1) == REG
1219       && REGNO (operand1) >= FIRST_PSEUDO_REGISTER)
1220     operand1 = reg_equiv_mem[REGNO (operand1)];
1221   else if (scratch_reg
1222            && reload_in_progress && GET_CODE (operand1) == SUBREG
1223            && GET_CODE (SUBREG_REG (operand1)) == REG
1224            && REGNO (SUBREG_REG (operand1)) >= FIRST_PSEUDO_REGISTER)
1225     {
1226      /* We must not alter SUBREG_WORD (operand0) since that would confuse
1227         the code which tracks sets/uses for delete_output_reload.  */
1228       rtx temp = gen_rtx_SUBREG (GET_MODE (operand1),
1229                                  reg_equiv_mem [REGNO (SUBREG_REG (operand1))],
1230                                  SUBREG_WORD (operand1));
1231       operand1 = alter_subreg (temp);
1232     }
1233
1234   if (scratch_reg && reload_in_progress && GET_CODE (operand0) == MEM
1235       && ((tem = find_replacement (&XEXP (operand0, 0)))
1236           != XEXP (operand0, 0)))
1237     operand0 = gen_rtx_MEM (GET_MODE (operand0), tem);
1238   if (scratch_reg && reload_in_progress && GET_CODE (operand1) == MEM
1239       && ((tem = find_replacement (&XEXP (operand1, 0)))
1240           != XEXP (operand1, 0)))
1241     operand1 = gen_rtx_MEM (GET_MODE (operand1), tem);
1242
1243   /* Handle secondary reloads for loads/stores of FP registers from
1244      REG+D addresses where D does not fit in 5 bits, including 
1245      (subreg (mem (addr))) cases.  */
1246   if (fp_reg_operand (operand0, mode)
1247       && ((GET_CODE (operand1) == MEM
1248            && ! memory_address_p (DFmode, XEXP (operand1, 0)))
1249           || ((GET_CODE (operand1) == SUBREG
1250                && GET_CODE (XEXP (operand1, 0)) == MEM
1251                && !memory_address_p (DFmode, XEXP (XEXP (operand1, 0), 0)))))
1252       && scratch_reg)
1253     {
1254       if (GET_CODE (operand1) == SUBREG)
1255         operand1 = XEXP (operand1, 0);
1256
1257       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
1258          it in WORD_MODE regardless of what mode it was originally given
1259          to us.  */
1260       scratch_reg = gen_rtx_REG (word_mode, REGNO (scratch_reg));
1261
1262       /* D might not fit in 14 bits either; for such cases load D into
1263          scratch reg.  */
1264       if (!memory_address_p (Pmode, XEXP (operand1, 0)))
1265         {
1266           emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
1267           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1, 0)),
1268                                                        Pmode,
1269                                                        XEXP (XEXP (operand1, 0), 0),
1270                                                        scratch_reg));
1271         }
1272       else
1273         emit_move_insn (scratch_reg, XEXP (operand1, 0));
1274       emit_insn (gen_rtx_SET (VOIDmode, operand0,
1275                               gen_rtx_MEM (mode, scratch_reg)));
1276       return 1;
1277     }
1278   else if (fp_reg_operand (operand1, mode)
1279            && ((GET_CODE (operand0) == MEM
1280                 && ! memory_address_p (DFmode, XEXP (operand0, 0)))
1281                || ((GET_CODE (operand0) == SUBREG)
1282                    && GET_CODE (XEXP (operand0, 0)) == MEM
1283                    && !memory_address_p (DFmode, XEXP (XEXP (operand0, 0), 0))))
1284            && scratch_reg)
1285     {
1286       if (GET_CODE (operand0) == SUBREG)
1287         operand0 = XEXP (operand0, 0);
1288
1289       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
1290          it in WORD_MODE regardless of what mode it was originally given
1291          to us.  */
1292       scratch_reg = gen_rtx_REG (word_mode, REGNO (scratch_reg));
1293
1294       /* D might not fit in 14 bits either; for such cases load D into
1295          scratch reg.  */
1296       if (!memory_address_p (Pmode, XEXP (operand0, 0)))
1297         {
1298           emit_move_insn (scratch_reg, XEXP (XEXP (operand0, 0), 1));
1299           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand0,
1300                                                                         0)),
1301                                                        Pmode,
1302                                                        XEXP (XEXP (operand0, 0),
1303                                                                    0),
1304                                                        scratch_reg));
1305         }
1306       else
1307         emit_move_insn (scratch_reg, XEXP (operand0, 0));
1308       emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_MEM (mode, scratch_reg),
1309                               operand1));
1310       return 1;
1311     }
1312   /* Handle secondary reloads for loads of FP registers from constant
1313      expressions by forcing the constant into memory.
1314
1315      use scratch_reg to hold the address of the memory location.
1316
1317      The proper fix is to change PREFERRED_RELOAD_CLASS to return
1318      NO_REGS when presented with a const_int and an register class
1319      containing only FP registers.  Doing so unfortunately creates
1320      more problems than it solves.   Fix this for 2.5.  */
1321   else if (fp_reg_operand (operand0, mode)
1322            && CONSTANT_P (operand1)
1323            && scratch_reg)
1324     {
1325       rtx xoperands[2];
1326
1327       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
1328          it in WORD_MODE regardless of what mode it was originally given
1329          to us.  */
1330       scratch_reg = gen_rtx_REG (word_mode, REGNO (scratch_reg));
1331
1332       /* Force the constant into memory and put the address of the
1333          memory location into scratch_reg.  */
1334       xoperands[0] = scratch_reg;
1335       xoperands[1] = XEXP (force_const_mem (mode, operand1), 0);
1336       emit_move_sequence (xoperands, Pmode, 0);
1337
1338       /* Now load the destination register.  */
1339       emit_insn (gen_rtx_SET (mode, operand0,
1340                               gen_rtx_MEM (mode, scratch_reg)));
1341       return 1;
1342     }
1343   /* Handle secondary reloads for SAR.  These occur when trying to load
1344      the SAR from memory a FP register, or with a constant.  */
1345   else if (GET_CODE (operand0) == REG
1346            && REGNO_REG_CLASS (REGNO (operand0)) == SHIFT_REGS
1347            && (GET_CODE (operand1) == MEM
1348                || GET_CODE (operand1) == CONST_INT
1349                || (GET_CODE (operand1) == REG
1350                    && FP_REG_CLASS_P (REGNO_REG_CLASS (REGNO (operand1)))))
1351            && scratch_reg)
1352     {
1353       /* D might not fit in 14 bits either; for such cases load D into
1354          scratch reg.  */
1355       if (GET_CODE (operand1) == MEM
1356           && !memory_address_p (Pmode, XEXP (operand1, 0)))
1357         {
1358           /* We are reloading the address into the scratch register, so we
1359              want to make sure the scratch register is a full register.  */
1360           scratch_reg = gen_rtx_REG (word_mode, REGNO (scratch_reg));
1361
1362           emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));   
1363           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1,
1364                                                                         0)),
1365                                                        Pmode,
1366                                                        XEXP (XEXP (operand1, 0),
1367                                                        0),
1368                                                        scratch_reg));
1369
1370           /* Now we are going to load the scratch register from memory,
1371              we want to load it in the same width as the original MEM,
1372              which must be the same as the width of the ultimate destination,
1373              OPERAND0.  */
1374           scratch_reg = gen_rtx_REG (GET_MODE (operand0), REGNO (scratch_reg));
1375           
1376           emit_move_insn (scratch_reg, gen_rtx_MEM (GET_MODE (operand0),
1377                                                     scratch_reg));
1378         }
1379       else
1380         {
1381           /* We want to load the scratch register using the same mode as
1382              the ultimate destination.  */
1383           scratch_reg = gen_rtx_REG (GET_MODE (operand0), REGNO (scratch_reg));
1384           emit_move_insn (scratch_reg, operand1);
1385         }
1386
1387       /* And emit the insn to set the ultimate destination.  We know that
1388          the scratch register has the same mode as the destination at this
1389          point.  */
1390       emit_move_insn (operand0, scratch_reg);
1391       return 1;
1392     }
1393   /* Handle most common case: storing into a register.  */
1394   else if (register_operand (operand0, mode))
1395     {
1396       if (register_operand (operand1, mode)
1397           || (GET_CODE (operand1) == CONST_INT && INT_14_BITS (operand1))
1398           || (operand1 == CONST0_RTX (mode))
1399           || (GET_CODE (operand1) == HIGH
1400               && !symbolic_operand (XEXP (operand1, 0), VOIDmode))
1401           /* Only `general_operands' can come here, so MEM is ok.  */
1402           || GET_CODE (operand1) == MEM)
1403         {
1404           /* Run this case quickly.  */
1405           emit_insn (gen_rtx_SET (VOIDmode, operand0, operand1));
1406           return 1;
1407         }
1408     }
1409   else if (GET_CODE (operand0) == MEM)
1410     {
1411       if (mode == DFmode && operand1 == CONST0_RTX (mode)
1412           && !(reload_in_progress || reload_completed))
1413         {
1414           rtx temp = gen_reg_rtx (DFmode);
1415
1416           emit_insn (gen_rtx_SET (VOIDmode, temp, operand1));
1417           emit_insn (gen_rtx_SET (VOIDmode, operand0, temp));
1418           return 1;
1419         }
1420       if (register_operand (operand1, mode) || operand1 == CONST0_RTX (mode))
1421         {
1422           /* Run this case quickly.  */
1423           emit_insn (gen_rtx_SET (VOIDmode, operand0, operand1));
1424           return 1;
1425         }
1426       if (! (reload_in_progress || reload_completed))
1427         {
1428           operands[0] = validize_mem (operand0);
1429           operands[1] = operand1 = force_reg (mode, operand1);
1430         }
1431     }
1432
1433   /* Simplify the source if we need to.
1434      Note we do have to handle function labels here, even though we do
1435      not consider them legitimate constants.  Loop optimizations can
1436      call the emit_move_xxx with one as a source.  */
1437   if ((GET_CODE (operand1) != HIGH && immediate_operand (operand1, mode))
1438       || function_label_operand (operand1, mode)
1439       || (GET_CODE (operand1) == HIGH
1440           && symbolic_operand (XEXP (operand1, 0), mode)))
1441     {
1442       int ishighonly = 0;
1443
1444       if (GET_CODE (operand1) == HIGH)
1445         {
1446           ishighonly = 1;
1447           operand1 = XEXP (operand1, 0);
1448         }
1449       if (symbolic_operand (operand1, mode))
1450         {
1451           /* Argh.  The assembler and linker can't handle arithmetic
1452              involving plabels.
1453
1454              So we force the plabel into memory, load operand0 from
1455              the memory location, then add in the constant part.  */
1456           if ((GET_CODE (operand1) == CONST
1457                && GET_CODE (XEXP (operand1, 0)) == PLUS
1458                && function_label_operand (XEXP (XEXP (operand1, 0), 0), Pmode))
1459               || function_label_operand (operand1, mode))
1460             {
1461               rtx temp, const_part;
1462
1463               /* Figure out what (if any) scratch register to use.  */
1464               if (reload_in_progress || reload_completed)
1465                 {
1466                   scratch_reg = scratch_reg ? scratch_reg : operand0;
1467                   /* SCRATCH_REG will hold an address and maybe the actual
1468                      data.  We want it in WORD_MODE regardless of what mode it
1469                      was originally given to us.  */
1470                   scratch_reg = gen_rtx_REG (word_mode, REGNO (scratch_reg));
1471                 }
1472               else if (flag_pic)
1473                 scratch_reg = gen_reg_rtx (Pmode);
1474
1475               if (GET_CODE (operand1) == CONST)
1476                 {
1477                   /* Save away the constant part of the expression.  */
1478                   const_part = XEXP (XEXP (operand1, 0), 1);
1479                   if (GET_CODE (const_part) != CONST_INT)
1480                     abort ();
1481
1482                   /* Force the function label into memory.  */
1483                   temp = force_const_mem (mode, XEXP (XEXP (operand1, 0), 0));
1484                 }
1485               else
1486                 {
1487                   /* No constant part.  */
1488                   const_part = NULL_RTX;
1489
1490                   /* Force the function label into memory.  */
1491                   temp = force_const_mem (mode, operand1);
1492                 }
1493                 
1494
1495               /* Get the address of the memory location.  PIC-ify it if
1496                  necessary.  */
1497               temp = XEXP (temp, 0);
1498               if (flag_pic)
1499                 temp = legitimize_pic_address (temp, mode, scratch_reg);
1500
1501               /* Put the address of the memory location into our destination
1502                  register.  */
1503               operands[1] = temp;
1504               emit_move_sequence (operands, mode, scratch_reg);
1505
1506               /* Now load from the memory location into our destination
1507                  register.  */
1508               operands[1] = gen_rtx_MEM (Pmode, operands[0]);
1509               emit_move_sequence (operands, mode, scratch_reg);
1510
1511               /* And add back in the constant part.  */
1512               if (const_part != NULL_RTX)
1513                 expand_inc (operand0, const_part);
1514
1515               return 1;
1516             }
1517
1518           if (flag_pic)
1519             {
1520               rtx temp;
1521
1522               if (reload_in_progress || reload_completed)
1523                 {
1524                   temp = scratch_reg ? scratch_reg : operand0;
1525                   /* TEMP will hold an address and maybe the actual
1526                      data.  We want it in WORD_MODE regardless of what mode it
1527                      was originally given to us.  */
1528                   temp = gen_rtx_REG (word_mode, REGNO (temp));
1529                 }
1530               else
1531                 temp = gen_reg_rtx (Pmode);
1532
1533               /* (const (plus (symbol) (const_int))) must be forced to
1534                  memory during/after reload if the const_int will not fit
1535                  in 14 bits.  */
1536               if (GET_CODE (operand1) == CONST
1537                        && GET_CODE (XEXP (operand1, 0)) == PLUS
1538                        && GET_CODE (XEXP (XEXP (operand1, 0), 1)) == CONST_INT
1539                        && !INT_14_BITS (XEXP (XEXP (operand1, 0), 1))
1540                        && (reload_completed || reload_in_progress)
1541                        && flag_pic)
1542                 {
1543                   operands[1] = force_const_mem (mode, operand1);
1544                   operands[1] = legitimize_pic_address (XEXP (operands[1], 0),
1545                                                         mode, temp);
1546                   emit_move_sequence (operands, mode, temp);
1547                 }
1548               else
1549                 {
1550                   operands[1] = legitimize_pic_address (operand1, mode, temp);
1551                   emit_insn (gen_rtx_SET (VOIDmode, operand0, operands[1]));
1552                 }
1553             }
1554           /* On the HPPA, references to data space are supposed to use dp,
1555              register 27, but showing it in the RTL inhibits various cse
1556              and loop optimizations.  */
1557           else
1558             {
1559               rtx temp, set;
1560
1561               if (reload_in_progress || reload_completed)
1562                 {
1563                   temp = scratch_reg ? scratch_reg : operand0;
1564                   /* TEMP will hold an address and maybe the actual
1565                      data.  We want it in WORD_MODE regardless of what mode it
1566                      was originally given to us.  */
1567                   temp = gen_rtx_REG (word_mode, REGNO (temp));
1568                 }
1569               else
1570                 temp = gen_reg_rtx (mode);
1571
1572               /* Loading a SYMBOL_REF into a register makes that register
1573                  safe to be used as the base in an indexed address. 
1574
1575                  Don't mark hard registers though.  That loses.  */
1576               if (GET_CODE (operand0) == REG
1577                   && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
1578                 REG_POINTER (operand0) = 1;
1579               if (REGNO (temp) >= FIRST_PSEUDO_REGISTER)
1580                 REG_POINTER (temp) = 1;
1581               if (ishighonly)
1582                 set = gen_rtx_SET (mode, operand0, temp);
1583               else
1584                 set = gen_rtx_SET (VOIDmode,
1585                                    operand0,
1586                                    gen_rtx_LO_SUM (mode, temp, operand1));
1587
1588               emit_insn (gen_rtx_SET (VOIDmode,
1589                                       temp,
1590                                       gen_rtx_HIGH (mode, operand1)));
1591               emit_insn (set);
1592
1593             }
1594           return 1;
1595         }
1596       else if (GET_CODE (operand1) != CONST_INT
1597                || ! cint_ok_for_move (INTVAL (operand1)))
1598         {
1599           rtx temp;
1600           int need_zero_extend = 0;
1601
1602           if (TARGET_64BIT && GET_CODE (operand1) == CONST_INT
1603               && GET_MODE_BITSIZE (GET_MODE (operand0)) > 32)
1604             {
1605               HOST_WIDE_INT val = INTVAL (operand1);
1606               HOST_WIDE_INT nval = INTVAL (operand1);
1607
1608               /* If the value is the same after a 32->64bit sign
1609                  extension, then we can use it as-is.  Else we will
1610                  need to sign extend the constant from 32->64bits
1611                  then zero extend the result from 32->64bits.  */
1612               nval = ((val & 0xffffffff) ^ (~0x7fffffff)) + 0x80000000;
1613               if (val != nval)
1614                 {
1615                   need_zero_extend = 1;
1616                   operand1 = GEN_INT (nval);
1617                 }
1618             }
1619
1620           if (reload_in_progress || reload_completed)
1621             temp = operand0;
1622           else
1623             temp = gen_reg_rtx (mode);
1624
1625           emit_insn (gen_rtx_SET (VOIDmode, temp,
1626                                   gen_rtx_HIGH (mode, operand1)));
1627           operands[1] = gen_rtx_LO_SUM (mode, temp, operand1);
1628           emit_move_insn (operands[0], operands[1]);
1629
1630           if (need_zero_extend)
1631             {
1632               emit_insn (gen_zero_extendsidi2 (operands[0],
1633                                                gen_rtx_SUBREG (SImode,
1634                                                                operands[0],
1635                                                                0)));
1636             }
1637         
1638           return 1;
1639         }
1640     }
1641   /* Now have insn-emit do whatever it normally does.  */
1642   return 0;
1643 }
1644
1645 /* Examine EXP and return nonzero if it contains an ADDR_EXPR (meaning
1646    it will need a link/runtime reloc).  */
1647
1648 int
1649 reloc_needed (exp)
1650      tree exp;
1651 {
1652   int reloc = 0;
1653
1654   switch (TREE_CODE (exp))
1655     {
1656     case ADDR_EXPR:
1657       return 1;
1658
1659     case PLUS_EXPR:
1660     case MINUS_EXPR:
1661       reloc = reloc_needed (TREE_OPERAND (exp, 0));
1662       reloc |= reloc_needed (TREE_OPERAND (exp, 1));
1663       break;
1664
1665     case NOP_EXPR:
1666     case CONVERT_EXPR:
1667     case NON_LVALUE_EXPR:
1668       reloc = reloc_needed (TREE_OPERAND (exp, 0));
1669       break;
1670
1671     case CONSTRUCTOR:
1672       {
1673         register tree link;
1674         for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
1675           if (TREE_VALUE (link) != 0)
1676             reloc |= reloc_needed (TREE_VALUE (link));
1677       }
1678       break;
1679
1680     case ERROR_MARK:
1681       break;
1682
1683     default:
1684       break;
1685     }
1686   return reloc;
1687 }
1688
1689 /* Does operand (which is a symbolic_operand) live in text space? If
1690    so SYMBOL_REF_FLAG, which is set by ENCODE_SECTION_INFO, will be true.  */
1691
1692 int
1693 read_only_operand (operand, mode)
1694      rtx operand;
1695      enum machine_mode mode ATTRIBUTE_UNUSED;
1696 {
1697   if (GET_CODE (operand) == CONST)
1698     operand = XEXP (XEXP (operand, 0), 0);
1699   if (flag_pic)
1700     {
1701       if (GET_CODE (operand) == SYMBOL_REF)
1702         return SYMBOL_REF_FLAG (operand) && !CONSTANT_POOL_ADDRESS_P (operand);
1703     }
1704   else
1705     {
1706       if (GET_CODE (operand) == SYMBOL_REF)
1707         return SYMBOL_REF_FLAG (operand) || CONSTANT_POOL_ADDRESS_P (operand);
1708     }
1709   return 1;
1710 }
1711
1712 \f
1713 /* Return the best assembler insn template
1714    for moving operands[1] into operands[0] as a fullword.   */
1715 const char *
1716 singlemove_string (operands)
1717      rtx *operands;
1718 {
1719   HOST_WIDE_INT intval;
1720
1721   if (GET_CODE (operands[0]) == MEM)
1722     return "stw %r1,%0";
1723   if (GET_CODE (operands[1]) == MEM)
1724     return "ldw %1,%0";
1725   if (GET_CODE (operands[1]) == CONST_DOUBLE)
1726     {
1727       long i;
1728       REAL_VALUE_TYPE d;
1729
1730       if (GET_MODE (operands[1]) != SFmode)
1731         abort ();
1732
1733       /* Translate the CONST_DOUBLE to a CONST_INT with the same target
1734          bit pattern.  */
1735       REAL_VALUE_FROM_CONST_DOUBLE (d, operands[1]);
1736       REAL_VALUE_TO_TARGET_SINGLE (d, i);
1737
1738       operands[1] = GEN_INT (i);
1739       /* Fall through to CONST_INT case.  */
1740     }
1741   if (GET_CODE (operands[1]) == CONST_INT)
1742     {
1743       intval = INTVAL (operands[1]);
1744
1745       if (VAL_14_BITS_P (intval))
1746         return "ldi %1,%0";
1747       else if ((intval & 0x7ff) == 0)
1748         return "ldil L'%1,%0";
1749       else if (zdepi_cint_p (intval))
1750         return "{zdepi %Z1,%0|depwi,z %Z1,%0}";
1751       else
1752         return "ldil L'%1,%0\n\tldo R'%1(%0),%0";
1753     }
1754   return "copy %1,%0";
1755 }
1756 \f
1757
1758 /* Compute position (in OP[1]) and width (in OP[2])
1759    useful for copying IMM to a register using the zdepi
1760    instructions.  Store the immediate value to insert in OP[0].  */
1761 static void
1762 compute_zdepwi_operands (imm, op)
1763      unsigned HOST_WIDE_INT imm;
1764      unsigned *op;
1765 {
1766   int lsb, len;
1767
1768   /* Find the least significant set bit in IMM.  */
1769   for (lsb = 0; lsb < 32; lsb++)
1770     {
1771       if ((imm & 1) != 0)
1772         break;
1773       imm >>= 1;
1774     }
1775
1776   /* Choose variants based on *sign* of the 5-bit field.  */
1777   if ((imm & 0x10) == 0)
1778     len = (lsb <= 28) ? 4 : 32 - lsb;
1779   else
1780     {
1781       /* Find the width of the bitstring in IMM.  */
1782       for (len = 5; len < 32; len++)
1783         {
1784           if ((imm & (1 << len)) == 0)
1785             break;
1786         }
1787
1788       /* Sign extend IMM as a 5-bit value.  */
1789       imm = (imm & 0xf) - 0x10;
1790     }
1791
1792   op[0] = imm;
1793   op[1] = 31 - lsb;
1794   op[2] = len;
1795 }
1796
1797 /* Compute position (in OP[1]) and width (in OP[2])
1798    useful for copying IMM to a register using the depdi,z
1799    instructions.  Store the immediate value to insert in OP[0].  */
1800 void
1801 compute_zdepdi_operands (imm, op)
1802      unsigned HOST_WIDE_INT imm;
1803      unsigned *op;
1804 {
1805   HOST_WIDE_INT lsb, len;
1806
1807   /* Find the least significant set bit in IMM.  */
1808   for (lsb = 0; lsb < HOST_BITS_PER_WIDE_INT; lsb++)
1809     {
1810       if ((imm & 1) != 0)
1811         break;
1812       imm >>= 1;
1813     }
1814
1815   /* Choose variants based on *sign* of the 5-bit field.  */
1816   if ((imm & 0x10) == 0)
1817     len = ((lsb <= HOST_BITS_PER_WIDE_INT - 4)
1818            ? 4 : HOST_BITS_PER_WIDE_INT - lsb);
1819   else
1820     {
1821       /* Find the width of the bitstring in IMM.  */
1822       for (len = 5; len < HOST_BITS_PER_WIDE_INT; len++)
1823         {
1824           if ((imm & ((unsigned HOST_WIDE_INT)1 << len)) == 0)
1825             break;
1826         }
1827
1828       /* Sign extend IMM as a 5-bit value.  */
1829       imm = (imm & 0xf) - 0x10;
1830     }
1831
1832   op[0] = imm;
1833   op[1] = 63 - lsb;
1834   op[2] = len;
1835 }
1836
1837 /* Output assembler code to perform a doubleword move insn
1838    with operands OPERANDS.  */
1839
1840 const char *
1841 output_move_double (operands)
1842      rtx *operands;
1843 {
1844   enum { REGOP, OFFSOP, MEMOP, CNSTOP, RNDOP } optype0, optype1;
1845   rtx latehalf[2];
1846   rtx addreg0 = 0, addreg1 = 0;
1847
1848   /* First classify both operands.  */
1849
1850   if (REG_P (operands[0]))
1851     optype0 = REGOP;
1852   else if (offsettable_memref_p (operands[0]))
1853     optype0 = OFFSOP;
1854   else if (GET_CODE (operands[0]) == MEM)
1855     optype0 = MEMOP;
1856   else
1857     optype0 = RNDOP;
1858
1859   if (REG_P (operands[1]))
1860     optype1 = REGOP;
1861   else if (CONSTANT_P (operands[1]))
1862     optype1 = CNSTOP;
1863   else if (offsettable_memref_p (operands[1]))
1864     optype1 = OFFSOP;
1865   else if (GET_CODE (operands[1]) == MEM)
1866     optype1 = MEMOP;
1867   else
1868     optype1 = RNDOP;
1869
1870   /* Check for the cases that the operand constraints are not
1871      supposed to allow to happen.  Abort if we get one,
1872      because generating code for these cases is painful.  */
1873
1874   if (optype0 != REGOP && optype1 != REGOP)
1875     abort ();
1876
1877    /* Handle auto decrementing and incrementing loads and stores
1878      specifically, since the structure of the function doesn't work
1879      for them without major modification.  Do it better when we learn
1880      this port about the general inc/dec addressing of PA.
1881      (This was written by tege.  Chide him if it doesn't work.)  */
1882
1883   if (optype0 == MEMOP)
1884     {
1885       /* We have to output the address syntax ourselves, since print_operand
1886          doesn't deal with the addresses we want to use.  Fix this later.  */
1887
1888       rtx addr = XEXP (operands[0], 0);
1889       if (GET_CODE (addr) == POST_INC || GET_CODE (addr) == POST_DEC)
1890         {
1891           rtx high_reg = gen_rtx_SUBREG (SImode, operands[1], 0);
1892
1893           operands[0] = XEXP (addr, 0);
1894           if (GET_CODE (operands[1]) != REG || GET_CODE (operands[0]) != REG)
1895             abort ();
1896
1897           if (!reg_overlap_mentioned_p (high_reg, addr))
1898             {
1899               /* No overlap between high target register and address
1900                  register.  (We do this in a non-obvious way to
1901                  save a register file writeback)  */
1902               if (GET_CODE (addr) == POST_INC)
1903                 return "{stws|stw},ma %1,8(%0)\n\tstw %R1,-4(%0)";
1904               return "{stws|stw},ma %1,-8(%0)\n\tstw %R1,12(%0)";
1905             }
1906           else
1907             abort();
1908         }
1909       else if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
1910         {
1911           rtx high_reg = gen_rtx_SUBREG (SImode, operands[1], 0);
1912
1913           operands[0] = XEXP (addr, 0);
1914           if (GET_CODE (operands[1]) != REG || GET_CODE (operands[0]) != REG)
1915             abort ();
1916
1917           if (!reg_overlap_mentioned_p (high_reg, addr))
1918             {
1919               /* No overlap between high target register and address
1920                  register.  (We do this in a non-obvious way to
1921                  save a register file writeback)  */
1922               if (GET_CODE (addr) == PRE_INC)
1923                 return "{stws|stw},mb %1,8(%0)\n\tstw %R1,4(%0)";
1924               return "{stws|stw},mb %1,-8(%0)\n\tstw %R1,4(%0)";
1925             }
1926           else
1927             abort();
1928         }
1929     }
1930   if (optype1 == MEMOP)
1931     {
1932       /* We have to output the address syntax ourselves, since print_operand
1933          doesn't deal with the addresses we want to use.  Fix this later.  */
1934
1935       rtx addr = XEXP (operands[1], 0);
1936       if (GET_CODE (addr) == POST_INC || GET_CODE (addr) == POST_DEC)
1937         {
1938           rtx high_reg = gen_rtx_SUBREG (SImode, operands[0], 0);
1939
1940           operands[1] = XEXP (addr, 0);
1941           if (GET_CODE (operands[0]) != REG || GET_CODE (operands[1]) != REG)
1942             abort ();
1943
1944           if (!reg_overlap_mentioned_p (high_reg, addr))
1945             {
1946               /* No overlap between high target register and address
1947                  register.  (We do this in a non-obvious way to
1948                  save a register file writeback)  */
1949               if (GET_CODE (addr) == POST_INC)
1950                 return "{ldws|ldw},ma 8(%1),%0\n\tldw -4(%1),%R0";
1951               return "{ldws|ldw},ma -8(%1),%0\n\tldw 12(%1),%R0}";
1952             }
1953           else
1954             {
1955               /* This is an undefined situation.  We should load into the
1956                  address register *and* update that register.  Probably
1957                  we don't need to handle this at all.  */
1958               if (GET_CODE (addr) == POST_INC)
1959                 return "ldw 4(%1),%R0\n\t{ldws|ldw},ma 8(%1),%0";
1960               return "ldw 4(%1),%R0\n\t{ldws|ldw},ma -8(%1),%0";
1961             }
1962         }
1963       else if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
1964         {
1965           rtx high_reg = gen_rtx_SUBREG (SImode, operands[0], 0);
1966
1967           operands[1] = XEXP (addr, 0);
1968           if (GET_CODE (operands[0]) != REG || GET_CODE (operands[1]) != REG)
1969             abort ();
1970
1971           if (!reg_overlap_mentioned_p (high_reg, addr))
1972             {
1973               /* No overlap between high target register and address
1974                  register.  (We do this in a non-obvious way to
1975                  save a register file writeback)  */
1976               if (GET_CODE (addr) == PRE_INC)
1977                 return "{ldws|ldw},mb 8(%1),%0\n\tldw 4(%1),%R0";
1978               return "{ldws|ldw},mb -8(%1),%0\n\tldw 4(%1),%R0";
1979             }
1980           else
1981             {
1982               /* This is an undefined situation.  We should load into the
1983                  address register *and* update that register.  Probably
1984                  we don't need to handle this at all.  */
1985               if (GET_CODE (addr) == PRE_INC)
1986                 return "ldw 12(%1),%R0\n\t{ldws|ldw},mb 8(%1),%0";
1987               return "ldw -4(%1),%R0\n\t{ldws|ldw},mb -8(%1),%0";
1988             }
1989         }
1990       else if (GET_CODE (addr) == PLUS
1991                && GET_CODE (XEXP (addr, 0)) == MULT)
1992         {
1993           rtx high_reg = gen_rtx_SUBREG (SImode, operands[0], 0);
1994
1995           if (!reg_overlap_mentioned_p (high_reg, addr))
1996             {
1997               rtx xoperands[3];
1998
1999               xoperands[0] = high_reg;
2000               xoperands[1] = XEXP (addr, 1);
2001               xoperands[2] = XEXP (XEXP (addr, 0), 0);
2002               xoperands[3] = XEXP (XEXP (addr, 0), 1);
2003               output_asm_insn ("{sh%O3addl %2,%1,%0|shladd,l %2,%O3,%1,%0}",
2004                                xoperands);
2005               return "ldw 4(%0),%R0\n\tldw 0(%0),%0";
2006             }
2007           else
2008             {
2009               rtx xoperands[3];
2010
2011               xoperands[0] = high_reg;
2012               xoperands[1] = XEXP (addr, 1);
2013               xoperands[2] = XEXP (XEXP (addr, 0), 0);
2014               xoperands[3] = XEXP (XEXP (addr, 0), 1);
2015               output_asm_insn ("{sh%O3addl %2,%1,%R0|shladd,l %2,%O3,%1,%R0}",
2016                                xoperands);
2017               return "ldw 0(%R0),%0\n\tldw 4(%R0),%R0";
2018             }
2019            
2020         }
2021     }
2022
2023   /* If an operand is an unoffsettable memory ref, find a register
2024      we can increment temporarily to make it refer to the second word.  */
2025
2026   if (optype0 == MEMOP)
2027     addreg0 = find_addr_reg (XEXP (operands[0], 0));
2028
2029   if (optype1 == MEMOP)
2030     addreg1 = find_addr_reg (XEXP (operands[1], 0));
2031
2032   /* Ok, we can do one word at a time.
2033      Normally we do the low-numbered word first.
2034
2035      In either case, set up in LATEHALF the operands to use
2036      for the high-numbered word and in some cases alter the
2037      operands in OPERANDS to be suitable for the low-numbered word.  */
2038
2039   if (optype0 == REGOP)
2040     latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2041   else if (optype0 == OFFSOP)
2042     latehalf[0] = adj_offsettable_operand (operands[0], 4);
2043   else
2044     latehalf[0] = operands[0];
2045
2046   if (optype1 == REGOP)
2047     latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2048   else if (optype1 == OFFSOP)
2049     latehalf[1] = adj_offsettable_operand (operands[1], 4);
2050   else if (optype1 == CNSTOP)
2051     split_double (operands[1], &operands[1], &latehalf[1]);
2052   else
2053     latehalf[1] = operands[1];
2054
2055   /* If the first move would clobber the source of the second one,
2056      do them in the other order.
2057
2058      This can happen in two cases:
2059
2060         mem -> register where the first half of the destination register
2061         is the same register used in the memory's address.  Reload
2062         can create such insns.
2063
2064         mem in this case will be either register indirect or register
2065         indirect plus a valid offset. 
2066
2067         register -> register move where REGNO(dst) == REGNO(src + 1)
2068         someone (Tim/Tege?) claimed this can happen for parameter loads. 
2069
2070      Handle mem -> register case first.  */
2071   if (optype0 == REGOP
2072       && (optype1 == MEMOP || optype1 == OFFSOP)
2073       && refers_to_regno_p (REGNO (operands[0]), REGNO (operands[0]) + 1,
2074                             operands[1], 0))
2075     {
2076       /* Do the late half first.  */
2077       if (addreg1)
2078         output_asm_insn ("ldo 4(%0),%0", &addreg1);
2079       output_asm_insn (singlemove_string (latehalf), latehalf);
2080
2081       /* Then clobber.  */
2082       if (addreg1)
2083         output_asm_insn ("ldo -4(%0),%0", &addreg1);
2084       return singlemove_string (operands);
2085     }
2086
2087   /* Now handle register -> register case.  */
2088   if (optype0 == REGOP && optype1 == REGOP
2089       && REGNO (operands[0]) == REGNO (operands[1]) + 1)
2090     {
2091       output_asm_insn (singlemove_string (latehalf), latehalf);
2092       return singlemove_string (operands);
2093     }
2094
2095   /* Normal case: do the two words, low-numbered first.  */
2096
2097   output_asm_insn (singlemove_string (operands), operands);
2098
2099   /* Make any unoffsettable addresses point at high-numbered word.  */
2100   if (addreg0)
2101     output_asm_insn ("ldo 4(%0),%0", &addreg0);
2102   if (addreg1)
2103     output_asm_insn ("ldo 4(%0),%0", &addreg1);
2104
2105   /* Do that word.  */
2106   output_asm_insn (singlemove_string (latehalf), latehalf);
2107
2108   /* Undo the adds we just did.  */
2109   if (addreg0)
2110     output_asm_insn ("ldo -4(%0),%0", &addreg0);
2111   if (addreg1)
2112     output_asm_insn ("ldo -4(%0),%0", &addreg1);
2113
2114   return "";
2115 }
2116 \f
2117 const char *
2118 output_fp_move_double (operands)
2119      rtx *operands;
2120 {
2121   if (FP_REG_P (operands[0]))
2122     {
2123       if (FP_REG_P (operands[1])
2124           || operands[1] == CONST0_RTX (GET_MODE (operands[0])))
2125         output_asm_insn ("fcpy,dbl %f1,%0", operands);
2126       else
2127         output_asm_insn ("fldd%F1 %1,%0", operands);
2128     }
2129   else if (FP_REG_P (operands[1]))
2130     {
2131       output_asm_insn ("fstd%F0 %1,%0", operands);
2132     }
2133   else if (operands[1] == CONST0_RTX (GET_MODE (operands[0])))
2134     {
2135       if (GET_CODE (operands[0]) == REG)
2136         {
2137           rtx xoperands[2];
2138           xoperands[1] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2139           xoperands[0] = operands[0];
2140           output_asm_insn ("copy %%r0,%0\n\tcopy %%r0,%1", xoperands);
2141         }
2142       /* This is a pain.  You have to be prepared to deal with an
2143          arbitrary address here including pre/post increment/decrement.
2144
2145          so avoid this in the MD.  */
2146       else
2147         abort ();
2148     }
2149   else abort ();
2150   return "";
2151 }
2152 \f
2153 /* Return a REG that occurs in ADDR with coefficient 1.
2154    ADDR can be effectively incremented by incrementing REG.  */
2155
2156 static rtx
2157 find_addr_reg (addr)
2158      rtx addr;
2159 {
2160   while (GET_CODE (addr) == PLUS)
2161     {
2162       if (GET_CODE (XEXP (addr, 0)) == REG)
2163         addr = XEXP (addr, 0);
2164       else if (GET_CODE (XEXP (addr, 1)) == REG)
2165         addr = XEXP (addr, 1);
2166       else if (CONSTANT_P (XEXP (addr, 0)))
2167         addr = XEXP (addr, 1);
2168       else if (CONSTANT_P (XEXP (addr, 1)))
2169         addr = XEXP (addr, 0);
2170       else
2171         abort ();
2172     }
2173   if (GET_CODE (addr) == REG)
2174     return addr;
2175   abort ();
2176 }
2177
2178 /* Emit code to perform a block move.
2179
2180    OPERANDS[0] is the destination pointer as a REG, clobbered.
2181    OPERANDS[1] is the source pointer as a REG, clobbered.
2182    OPERANDS[2] is a register for temporary storage.
2183    OPERANDS[4] is the size as a CONST_INT
2184    OPERANDS[3] is a register for temporary storage.
2185    OPERANDS[5] is the alignment safe to use, as a CONST_INT. 
2186    OPERANDS[6] is another temporary register.   */
2187
2188 const char *
2189 output_block_move (operands, size_is_constant)
2190      rtx *operands;
2191      int size_is_constant ATTRIBUTE_UNUSED;
2192 {
2193   int align = INTVAL (operands[5]);
2194   unsigned long n_bytes = INTVAL (operands[4]);
2195
2196   /* We can't move more than four bytes at a time because the PA
2197      has no longer integer move insns.  (Could use fp mem ops?)  */
2198   if (align > 4)
2199     align = 4;
2200
2201   /* Note that we know each loop below will execute at least twice
2202      (else we would have open-coded the copy).  */
2203   switch (align)
2204     {
2205       case 4:
2206         /* Pre-adjust the loop counter.  */
2207         operands[4] = GEN_INT (n_bytes - 8);
2208         output_asm_insn ("ldi %4,%2", operands);
2209
2210         /* Copying loop.  */
2211         output_asm_insn ("{ldws|ldw},ma 4(%1),%3", operands);
2212         output_asm_insn ("{ldws|ldw},ma 4(%1),%6", operands);
2213         output_asm_insn ("{stws|stw},ma %3,4(%0)", operands);
2214         output_asm_insn ("addib,>= -8,%2,.-12", operands);
2215         output_asm_insn ("{stws|stw},ma %6,4(%0)", operands);
2216
2217         /* Handle the residual.  There could be up to 7 bytes of
2218            residual to copy!  */
2219         if (n_bytes % 8 != 0)
2220           {
2221             operands[4] = GEN_INT (n_bytes % 4);
2222             if (n_bytes % 8 >= 4)
2223               output_asm_insn ("{ldws|ldw},ma 4(%1),%3", operands);
2224             if (n_bytes % 4 != 0)
2225               output_asm_insn ("ldw 0(%1),%6", operands);
2226             if (n_bytes % 8 >= 4)
2227               output_asm_insn ("{stws|stw},ma %3,4(%0)", operands);
2228             if (n_bytes % 4 != 0)
2229               output_asm_insn ("{stbys|stby},e %6,%4(%0)", operands);
2230           }
2231         return "";
2232
2233       case 2:
2234         /* Pre-adjust the loop counter.  */
2235         operands[4] = GEN_INT (n_bytes - 4);
2236         output_asm_insn ("ldi %4,%2", operands);
2237
2238         /* Copying loop.  */
2239         output_asm_insn ("{ldhs|ldh},ma 2(%1),%3", operands);
2240         output_asm_insn ("{ldhs|ldh},ma 2(%1),%6", operands);
2241         output_asm_insn ("{sths|sth},ma %3,2(%0)", operands);
2242         output_asm_insn ("addib,>= -4,%2,.-12", operands);
2243         output_asm_insn ("{sths|sth},ma %6,2(%0)", operands);
2244
2245         /* Handle the residual.  */
2246         if (n_bytes % 4 != 0)
2247           {
2248             if (n_bytes % 4 >= 2)
2249               output_asm_insn ("{ldhs|ldh},ma 2(%1),%3", operands);
2250             if (n_bytes % 2 != 0)
2251               output_asm_insn ("ldb 0(%1),%6", operands);
2252             if (n_bytes % 4 >= 2)
2253               output_asm_insn ("{sths|sth},ma %3,2(%0)", operands);
2254             if (n_bytes % 2 != 0)
2255               output_asm_insn ("stb %6,0(%0)", operands);
2256           }
2257         return "";
2258
2259       case 1:
2260         /* Pre-adjust the loop counter.  */
2261         operands[4] = GEN_INT (n_bytes - 2);
2262         output_asm_insn ("ldi %4,%2", operands);
2263
2264         /* Copying loop.  */
2265         output_asm_insn ("{ldbs|ldb},ma 1(%1),%3", operands);
2266         output_asm_insn ("{ldbs|ldb},ma 1(%1),%6", operands);
2267         output_asm_insn ("{stbs|stb},ma %3,1(%0)", operands);
2268         output_asm_insn ("addib,>= -2,%2,.-12", operands);
2269         output_asm_insn ("{stbs|stb},ma %6,1(%0)", operands);
2270
2271         /* Handle the residual.  */
2272         if (n_bytes % 2 != 0)
2273           {
2274             output_asm_insn ("ldb 0(%1),%3", operands);
2275             output_asm_insn ("stb %3,0(%0)", operands);
2276           }
2277         return "";
2278
2279       default:
2280         abort ();
2281     }
2282 }
2283
2284 /* Count the number of insns necessary to handle this block move.
2285
2286    Basic structure is the same as emit_block_move, except that we
2287    count insns rather than emit them.  */
2288
2289 static int
2290 compute_movstrsi_length (insn)
2291      rtx insn;
2292 {
2293   rtx pat = PATTERN (insn);
2294   int align = INTVAL (XEXP (XVECEXP (pat, 0, 6), 0));
2295   unsigned long n_bytes = INTVAL (XEXP (XVECEXP (pat, 0, 5), 0));
2296   unsigned int n_insns = 0;
2297
2298   /* We can't move more than four bytes at a time because the PA
2299      has no longer integer move insns.  (Could use fp mem ops?)  */
2300   if (align > 4)
2301     align = 4;
2302
2303   /* The basic copying loop.  */
2304   n_insns = 6;
2305
2306   /* Residuals.  */
2307   if (n_bytes % (2 * align) != 0)
2308     {
2309       if ((n_bytes % (2 * align)) >= align)
2310         n_insns += 2;
2311
2312       if ((n_bytes % align) != 0)
2313         n_insns += 2;
2314     }
2315
2316   /* Lengths are expressed in bytes now; each insn is 4 bytes.  */
2317   return n_insns * 4;
2318 }
2319 \f
2320
2321 const char *
2322 output_and (operands)
2323      rtx *operands;
2324 {
2325   if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) != 0)
2326     {
2327       unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2328       int ls0, ls1, ms0, p, len;
2329
2330       for (ls0 = 0; ls0 < 32; ls0++)
2331         if ((mask & (1 << ls0)) == 0)
2332           break;
2333
2334       for (ls1 = ls0; ls1 < 32; ls1++)
2335         if ((mask & (1 << ls1)) != 0)
2336           break;
2337
2338       for (ms0 = ls1; ms0 < 32; ms0++)
2339         if ((mask & (1 << ms0)) == 0)
2340           break;
2341
2342       if (ms0 != 32)
2343         abort();
2344
2345       if (ls1 == 32)
2346         {
2347           len = ls0;
2348
2349           if (len == 0)
2350             abort ();
2351
2352           operands[2] = GEN_INT (len);
2353           return "{extru|extrw,u} %1,31,%2,%0";
2354         }
2355       else
2356         {
2357           /* We could use this `depi' for the case above as well, but `depi'
2358              requires one more register file access than an `extru'.  */
2359
2360           p = 31 - ls0;
2361           len = ls1 - ls0;
2362
2363           operands[2] = GEN_INT (p);
2364           operands[3] = GEN_INT (len);
2365           return "{depi|depwi} 0,%2,%3,%0";
2366         }
2367     }
2368   else
2369     return "and %1,%2,%0";
2370 }
2371
2372 /* Return a string to perform a bitwise-and of operands[1] with operands[2]
2373    storing the result in operands[0].  */
2374 const char *
2375 output_64bit_and (operands)
2376      rtx *operands;
2377 {
2378   if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) != 0)
2379     {
2380       unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2381       unsigned HOST_WIDE_INT ls0, ls1, ms0, p, len;
2382
2383       for (ls0 = 0; ls0 < HOST_BITS_PER_WIDE_INT; ls0++)
2384         if ((mask & ((unsigned HOST_WIDE_INT)1 << ls0)) == 0)
2385           break;
2386
2387       for (ls1 = ls0; ls1 < HOST_BITS_PER_WIDE_INT; ls1++)
2388         if ((mask & ((unsigned HOST_WIDE_INT)1 << ls1)) != 0)
2389           break;
2390
2391       for (ms0 = ls1; ms0 < HOST_BITS_PER_WIDE_INT; ms0++)
2392         if ((mask & ((unsigned HOST_WIDE_INT)1 << ms0)) == 0)
2393           break;
2394
2395       if (ms0 != HOST_BITS_PER_WIDE_INT)
2396         abort();
2397
2398       if (ls1 == HOST_BITS_PER_WIDE_INT)
2399         {
2400           len = ls0;
2401
2402           if (len == 0)
2403             abort ();
2404
2405           operands[2] = GEN_INT (len);
2406           return "extrd,u %1,63,%2,%0";
2407         }
2408       else
2409         {
2410           /* We could use this `depi' for the case above as well, but `depi'
2411              requires one more register file access than an `extru'.  */
2412
2413           p = 63 - ls0;
2414           len = ls1 - ls0;
2415
2416           operands[2] = GEN_INT (p);
2417           operands[3] = GEN_INT (len);
2418           return "depdi 0,%2,%3,%0";
2419         }
2420     }
2421   else
2422     return "and %1,%2,%0";
2423 }
2424
2425 const char *
2426 output_ior (operands)
2427      rtx *operands;
2428 {
2429   unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2430   int bs0, bs1, p, len;
2431
2432   if (INTVAL (operands[2]) == 0)
2433     return "copy %1,%0";
2434
2435   for (bs0 = 0; bs0 < 32; bs0++)
2436     if ((mask & (1 << bs0)) != 0)
2437       break;
2438
2439   for (bs1 = bs0; bs1 < 32; bs1++)
2440     if ((mask & (1 << bs1)) == 0)
2441       break;
2442
2443   if (bs1 != 32 && ((unsigned HOST_WIDE_INT) 1 << bs1) <= mask)
2444     abort();
2445
2446   p = 31 - bs0;
2447   len = bs1 - bs0;
2448
2449   operands[2] = GEN_INT (p);
2450   operands[3] = GEN_INT (len);
2451   return "{depi|depwi} -1,%2,%3,%0";
2452 }
2453
2454 /* Return a string to perform a bitwise-and of operands[1] with operands[2]
2455    storing the result in operands[0].  */
2456 const char *
2457 output_64bit_ior (operands)
2458      rtx *operands;
2459 {
2460   unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2461   unsigned HOST_WIDE_INT bs0, bs1, p, len;
2462
2463   if (INTVAL (operands[2]) == 0)
2464     return "copy %1,%0";
2465
2466   for (bs0 = 0; bs0 < HOST_BITS_PER_WIDE_INT; bs0++)
2467     if ((mask & ((unsigned HOST_WIDE_INT)1 << bs0)) != 0)
2468       break;
2469
2470   for (bs1 = bs0; bs1 < HOST_BITS_PER_WIDE_INT; bs1++)
2471     if ((mask & ((unsigned HOST_WIDE_INT)1 << bs1)) == 0)
2472       break;
2473
2474   if (bs1 != HOST_BITS_PER_WIDE_INT
2475       && ((unsigned HOST_WIDE_INT) 1 << bs1) <= mask)
2476     abort();
2477
2478   p = 63 - bs0;
2479   len = bs1 - bs0;
2480
2481   operands[2] = GEN_INT (p);
2482   operands[3] = GEN_INT (len);
2483   return "depdi -1,%2,%3,%0";
2484 }
2485 \f
2486 /* Output an ascii string.  */
2487 void
2488 output_ascii (file, p, size)
2489      FILE *file;
2490      const unsigned char *p;
2491      int size;
2492 {
2493   int i;
2494   int chars_output;
2495   unsigned char partial_output[16];     /* Max space 4 chars can occupy.   */
2496
2497   /* The HP assembler can only take strings of 256 characters at one
2498      time.  This is a limitation on input line length, *not* the
2499      length of the string.  Sigh.  Even worse, it seems that the
2500      restriction is in number of input characters (see \xnn &
2501      \whatever).  So we have to do this very carefully.  */
2502
2503   fputs ("\t.STRING \"", file);
2504
2505   chars_output = 0;
2506   for (i = 0; i < size; i += 4)
2507     {
2508       int co = 0;
2509       int io = 0;
2510       for (io = 0, co = 0; io < MIN (4, size - i); io++)
2511         {
2512           register unsigned int c = p[i + io];
2513
2514           if (c == '\"' || c == '\\')
2515             partial_output[co++] = '\\';
2516           if (c >= ' ' && c < 0177)
2517             partial_output[co++] = c;
2518           else
2519             {
2520               unsigned int hexd;
2521               partial_output[co++] = '\\';
2522               partial_output[co++] = 'x';
2523               hexd =  c  / 16 - 0 + '0';
2524               if (hexd > '9')
2525                 hexd -= '9' - 'a' + 1;
2526               partial_output[co++] = hexd;
2527               hexd =  c % 16 - 0 + '0';
2528               if (hexd > '9')
2529                 hexd -= '9' - 'a' + 1;
2530               partial_output[co++] = hexd;
2531             }
2532         }
2533       if (chars_output + co > 243)
2534         {
2535           fputs ("\"\n\t.STRING \"", file);
2536           chars_output = 0;
2537         }
2538       fwrite (partial_output, 1, co, file);
2539       chars_output += co;
2540       co = 0;
2541     }
2542   fputs ("\"\n", file);
2543 }
2544
2545 /* Try to rewrite floating point comparisons & branches to avoid
2546    useless add,tr insns.
2547
2548    CHECK_NOTES is nonzero if we should examine REG_DEAD notes
2549    to see if FPCC is dead.  CHECK_NOTES is nonzero for the
2550    first attempt to remove useless add,tr insns.  It is zero
2551    for the second pass as reorg sometimes leaves bogus REG_DEAD
2552    notes lying around.
2553
2554    When CHECK_NOTES is zero we can only eliminate add,tr insns
2555    when there's a 1:1 correspondence between fcmp and ftest/fbranch
2556    instructions.  */
2557 static void
2558 remove_useless_addtr_insns (insns, check_notes)
2559      rtx insns;
2560      int check_notes;
2561 {
2562   rtx insn;
2563   static int pass = 0;
2564
2565   /* This is fairly cheap, so always run it when optimizing.  */
2566   if (optimize > 0)
2567     {
2568       int fcmp_count = 0;
2569       int fbranch_count = 0;
2570
2571       /* Walk all the insns in this function looking for fcmp & fbranch
2572          instructions.  Keep track of how many of each we find.  */
2573       insns = get_insns ();
2574       for (insn = insns; insn; insn = next_insn (insn))
2575         {
2576           rtx tmp;
2577
2578           /* Ignore anything that isn't an INSN or a JUMP_INSN.  */
2579           if (GET_CODE (insn) != INSN && GET_CODE (insn) != JUMP_INSN)
2580             continue;
2581
2582           tmp = PATTERN (insn);
2583
2584           /* It must be a set.  */
2585           if (GET_CODE (tmp) != SET)
2586             continue;
2587
2588           /* If the destination is CCFP, then we've found an fcmp insn.  */
2589           tmp = SET_DEST (tmp);
2590           if (GET_CODE (tmp) == REG && REGNO (tmp) == 0)
2591             {
2592               fcmp_count++;
2593               continue;
2594             }
2595             
2596           tmp = PATTERN (insn);
2597           /* If this is an fbranch instruction, bump the fbranch counter.  */
2598           if (GET_CODE (tmp) == SET
2599               && SET_DEST (tmp) == pc_rtx
2600               && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
2601               && GET_CODE (XEXP (SET_SRC (tmp), 0)) == NE
2602               && GET_CODE (XEXP (XEXP (SET_SRC (tmp), 0), 0)) == REG
2603               && REGNO (XEXP (XEXP (SET_SRC (tmp), 0), 0)) == 0)
2604             {
2605               fbranch_count++;
2606               continue;
2607             }
2608         }
2609
2610
2611       /* Find all floating point compare + branch insns.  If possible,
2612          reverse the comparison & the branch to avoid add,tr insns.  */
2613       for (insn = insns; insn; insn = next_insn (insn))
2614         {
2615           rtx tmp, next;
2616
2617           /* Ignore anything that isn't an INSN.  */
2618           if (GET_CODE (insn) != INSN)
2619             continue;
2620
2621           tmp = PATTERN (insn);
2622
2623           /* It must be a set.  */
2624           if (GET_CODE (tmp) != SET)
2625             continue;
2626
2627           /* The destination must be CCFP, which is register zero.  */
2628           tmp = SET_DEST (tmp);
2629           if (GET_CODE (tmp) != REG || REGNO (tmp) != 0)
2630             continue;
2631
2632           /* INSN should be a set of CCFP.
2633
2634              See if the result of this insn is used in a reversed FP
2635              conditional branch.  If so, reverse our condition and
2636              the branch.  Doing so avoids useless add,tr insns.  */
2637           next = next_insn (insn);
2638           while (next)
2639             {
2640               /* Jumps, calls and labels stop our search.  */
2641               if (GET_CODE (next) == JUMP_INSN
2642                   || GET_CODE (next) == CALL_INSN
2643                   || GET_CODE (next) == CODE_LABEL)
2644                 break;
2645
2646               /* As does another fcmp insn.  */
2647               if (GET_CODE (next) == INSN
2648                   && GET_CODE (PATTERN (next)) == SET
2649                   && GET_CODE (SET_DEST (PATTERN (next))) == REG
2650                   && REGNO (SET_DEST (PATTERN (next))) == 0)
2651                 break;
2652
2653               next = next_insn (next);
2654             }
2655
2656           /* Is NEXT_INSN a branch?  */
2657           if (next
2658               && GET_CODE (next) == JUMP_INSN)
2659             {
2660               rtx pattern = PATTERN (next);
2661
2662               /* If it a reversed fp conditional branch (eg uses add,tr)
2663                  and CCFP dies, then reverse our conditional and the branch
2664                  to avoid the add,tr.  */
2665               if (GET_CODE (pattern) == SET
2666                   && SET_DEST (pattern) == pc_rtx
2667                   && GET_CODE (SET_SRC (pattern)) == IF_THEN_ELSE
2668                   && GET_CODE (XEXP (SET_SRC (pattern), 0)) == NE
2669                   && GET_CODE (XEXP (XEXP (SET_SRC (pattern), 0), 0)) == REG
2670                   && REGNO (XEXP (XEXP (SET_SRC (pattern), 0), 0)) == 0
2671                   && GET_CODE (XEXP (SET_SRC (pattern), 1)) == PC
2672                   && (fcmp_count == fbranch_count
2673                       || (check_notes
2674                           && find_regno_note (next, REG_DEAD, 0))))
2675                 {
2676                   /* Reverse the branch.  */
2677                   tmp = XEXP (SET_SRC (pattern), 1);
2678                   XEXP (SET_SRC (pattern), 1) = XEXP (SET_SRC (pattern), 2);
2679                   XEXP (SET_SRC (pattern), 2) = tmp;
2680                   INSN_CODE (next) = -1;
2681
2682                   /* Reverse our condition.  */
2683                   tmp = PATTERN (insn);
2684                   PUT_CODE (XEXP (tmp, 1),
2685                     reverse_condition_maybe_unordered (GET_CODE (XEXP (tmp,
2686                       1))));
2687                 }
2688             }
2689         }
2690     }
2691
2692   pass = !pass;
2693
2694 }
2695 \f
2696 /* You may have trouble believing this, but this is the 32 bit HP-PA stack
2697    layout.  Wow.
2698
2699    Offset               Contents
2700
2701    Variable arguments   (optional; any number may be allocated)
2702
2703    SP-(4*(N+9))         arg word N
2704         :                   :
2705       SP-56             arg word 5
2706       SP-52             arg word 4
2707
2708    Fixed arguments      (must be allocated; may remain unused)
2709
2710       SP-48             arg word 3
2711       SP-44             arg word 2
2712       SP-40             arg word 1
2713       SP-36             arg word 0
2714
2715    Frame Marker
2716
2717       SP-32             External Data Pointer (DP)
2718       SP-28             External sr4
2719       SP-24             External/stub RP (RP')
2720       SP-20             Current RP
2721       SP-16             Static Link
2722       SP-12             Clean up
2723       SP-8              Calling Stub RP (RP'')
2724       SP-4              Previous SP
2725
2726    Top of Frame
2727
2728       SP-0              Stack Pointer (points to next available address)
2729
2730 */
2731
2732 /* This function saves registers as follows.  Registers marked with ' are
2733    this function's registers (as opposed to the previous function's).
2734    If a frame_pointer isn't needed, r4 is saved as a general register;
2735    the space for the frame pointer is still allocated, though, to keep
2736    things simple.
2737
2738
2739    Top of Frame
2740
2741        SP (FP')         Previous FP
2742        SP + 4           Alignment filler (sigh)
2743        SP + 8           Space for locals reserved here.
2744        .
2745        .
2746        .
2747        SP + n           All call saved register used.
2748        .
2749        .
2750        .
2751        SP + o           All call saved fp registers used.
2752        .
2753        .
2754        .
2755        SP + p (SP')     points to next available address.
2756
2757 */
2758
2759 /* Emit RTL to store REG at the memory location specified by BASE+DISP.
2760    Handle case where DISP > 8k by using the add_high_const patterns.
2761
2762    Note in DISP > 8k case, we will leave the high part of the address
2763    in %r1.  There is code in expand_hppa_{prologue,epilogue} that knows this.*/
2764
2765 static void
2766 store_reg (reg, disp, base)
2767      int reg, disp, base;
2768 {
2769   if (VAL_14_BITS_P (disp))
2770     emit_move_insn (gen_rtx_MEM (word_mode,
2771                                  plus_constant (gen_rtx_REG (Pmode, base),
2772                                                 disp)),
2773                     gen_rtx_REG (word_mode, reg));
2774   else
2775     {
2776       emit_move_insn (gen_rtx_REG (Pmode, 1),
2777                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, base),
2778                                     gen_rtx_HIGH (Pmode, GEN_INT (disp))));
2779       emit_move_insn (gen_rtx_MEM (word_mode,
2780                                    gen_rtx_LO_SUM (Pmode,
2781                                                    gen_rtx_REG (Pmode, 1),
2782                                                    GEN_INT (disp))),
2783                       gen_rtx_REG (word_mode, reg));
2784     }
2785 }
2786
2787 /* Emit RTL to load REG from the memory location specified by BASE+DISP.
2788    Handle case where DISP > 8k by using the add_high_const patterns.
2789
2790    Note in DISP > 8k case, we will leave the high part of the address
2791    in %r1.  There is code in expand_hppa_{prologue,epilogue} that knows this.*/
2792
2793 static void
2794 load_reg (reg, disp, base)
2795      int reg, disp, base;
2796 {
2797   if (VAL_14_BITS_P (disp))
2798     emit_move_insn (gen_rtx_REG (word_mode, reg),
2799                     gen_rtx_MEM (word_mode,
2800                                  plus_constant (gen_rtx_REG (Pmode, base),
2801                                                 disp)));
2802   else
2803     {
2804       emit_move_insn (gen_rtx_REG (Pmode, 1),
2805                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, base),
2806                                     gen_rtx_HIGH (Pmode, GEN_INT (disp))));
2807       emit_move_insn (gen_rtx_REG (word_mode, reg),
2808                       gen_rtx_MEM (word_mode,
2809                                    gen_rtx_LO_SUM (Pmode,
2810                                                    gen_rtx_REG (Pmode, 1),
2811                                                    GEN_INT (disp))));
2812     }
2813 }
2814
2815 /* Emit RTL to set REG to the value specified by BASE+DISP.
2816    Handle case where DISP > 8k by using the add_high_const patterns.
2817
2818    Note in DISP > 8k case, we will leave the high part of the address
2819    in %r1.  There is code in expand_hppa_{prologue,epilogue} that knows this.*/
2820
2821 static void
2822 set_reg_plus_d (reg, base, disp)
2823      int reg, base, disp;
2824 {
2825   if (VAL_14_BITS_P (disp))
2826     emit_move_insn (gen_rtx_REG (Pmode, reg),
2827                     plus_constant (gen_rtx_REG (Pmode, base), disp));
2828   else
2829     {
2830       emit_move_insn (gen_rtx_REG (Pmode, 1),
2831                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, base),
2832                                     gen_rtx_HIGH (Pmode, GEN_INT (disp))));
2833       emit_move_insn (gen_rtx_REG (Pmode, reg),
2834                       gen_rtx_LO_SUM (Pmode,
2835                                       gen_rtx_REG (Pmode, 1),
2836                                        GEN_INT (disp)));
2837     }
2838 }
2839
2840 /* Global variables set by FUNCTION_PROLOGUE.  */
2841 /* Size of frame.  Need to know this to emit return insns from
2842    leaf procedures.  */
2843 static int actual_fsize;
2844 static int local_fsize, save_fregs;
2845
2846 int
2847 compute_frame_size (size, fregs_live)
2848      int size;
2849      int *fregs_live;
2850 {
2851   int i, fsize;
2852
2853   /* Space for frame pointer + filler. If any frame is allocated
2854      we need to add this in because of STARTING_FRAME_OFFSET.
2855
2856      Similar code also appears in hppa_expand_prologue.  Change both
2857      of them at the same time.  */
2858   fsize = size + (size || frame_pointer_needed ? STARTING_FRAME_OFFSET : 0);
2859
2860   /* Account for space used by the callee general register saves.  */
2861   for (i = 18; i >= 3; i--)
2862     if (regs_ever_live[i])
2863       fsize += UNITS_PER_WORD;
2864
2865   /* Round the stack.  */
2866   fsize = (fsize + 7) & ~7;
2867
2868   /* Account for space used by the callee floating point register saves.  */
2869   for (i = FP_SAVED_REG_LAST; i >= FP_SAVED_REG_FIRST; i -= FP_REG_STEP)
2870     if (regs_ever_live[i]
2871         || (! TARGET_64BIT && regs_ever_live[i + 1]))
2872       {
2873         if (fregs_live)
2874           *fregs_live = 1;
2875
2876         /* We always save both halves of the FP register, so always
2877            increment the frame size by 8 bytes.  */
2878         fsize += 8;
2879       }
2880
2881   /* The various ABIs include space for the outgoing parameters in the
2882      size of the current function's stack frame.  */
2883   fsize += current_function_outgoing_args_size;
2884
2885   /* Allocate space for the fixed frame marker.  This space must be
2886      allocated for any function that makes calls or otherwise allocates
2887      stack space.  */
2888   if (!current_function_is_leaf || fsize)
2889     fsize += 32;
2890
2891   return (fsize + STACK_BOUNDARY - 1) & ~(STACK_BOUNDARY - 1);
2892 }
2893
2894 rtx hp_profile_label_rtx;
2895 static char hp_profile_label_name[8];
2896 void
2897 output_function_prologue (file, size)
2898      FILE *file;
2899      int size ATTRIBUTE_UNUSED;
2900 {
2901   /* The function's label and associated .PROC must never be
2902      separated and must be output *after* any profiling declarations
2903      to avoid changing spaces/subspaces within a procedure.  */
2904   ASM_OUTPUT_LABEL (file, XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0));
2905   fputs ("\t.PROC\n", file);
2906
2907   /* hppa_expand_prologue does the dirty work now.  We just need
2908      to output the assembler directives which denote the start
2909      of a function.  */
2910   fprintf (file, "\t.CALLINFO FRAME=%d", actual_fsize);
2911   if (regs_ever_live[2] || profile_flag)
2912     fputs (",CALLS,SAVE_RP", file);
2913   else
2914     fputs (",NO_CALLS", file);
2915
2916   if (frame_pointer_needed)
2917     fputs (",SAVE_SP", file);
2918
2919   /* Pass on information about the number of callee register saves
2920      performed in the prologue.
2921
2922      The compiler is supposed to pass the highest register number
2923      saved, the assembler then has to adjust that number before
2924      entering it into the unwind descriptor (to account for any
2925      caller saved registers with lower register numbers than the
2926      first callee saved register).  */
2927   if (gr_saved)
2928     fprintf (file, ",ENTRY_GR=%d", gr_saved + 2);
2929
2930   if (fr_saved)
2931     fprintf (file, ",ENTRY_FR=%d", fr_saved + 11);
2932
2933   fputs ("\n\t.ENTRY\n", file);
2934
2935   /* Horrid hack.  emit_function_prologue will modify this RTL in
2936      place to get the expected results.  */
2937   if (profile_flag)
2938     ASM_GENERATE_INTERNAL_LABEL (hp_profile_label_name, "LP",
2939                                  hp_profile_labelno);
2940
2941   /* If we're using GAS and not using the portable runtime model, then
2942      we don't need to accumulate the total number of code bytes.  */
2943   if (TARGET_GAS && ! TARGET_PORTABLE_RUNTIME)
2944     total_code_bytes = 0;
2945   else if (INSN_ADDRESSES_SET_P ())
2946     {
2947       unsigned int old_total = total_code_bytes;
2948
2949       total_code_bytes += INSN_ADDRESSES (INSN_UID (get_last_insn()));
2950       total_code_bytes += FUNCTION_BOUNDARY / BITS_PER_UNIT;
2951
2952       /* Be prepared to handle overflows.  */
2953       total_code_bytes = old_total > total_code_bytes ? -1 : total_code_bytes;
2954     }
2955   else
2956     total_code_bytes = -1;
2957
2958   remove_useless_addtr_insns (get_insns (), 0);
2959 }
2960
2961 void
2962 hppa_expand_prologue()
2963 {
2964   extern char call_used_regs[];
2965   int size = get_frame_size ();
2966   int merge_sp_adjust_with_store = 0;
2967   int i, offset;
2968   rtx tmpreg, size_rtx;
2969
2970   gr_saved = 0;
2971   fr_saved = 0;
2972   save_fregs = 0;
2973
2974   /* Allocate space for frame pointer + filler. If any frame is allocated
2975      we need to add this in because of STARTING_FRAME_OFFSET.
2976
2977      Similar code also appears in compute_frame_size.  Change both
2978      of them at the same time.  */
2979   local_fsize = size + (size || frame_pointer_needed
2980                         ? STARTING_FRAME_OFFSET : 0);
2981
2982   actual_fsize = compute_frame_size (size, &save_fregs);
2983
2984   /* Compute a few things we will use often.  */
2985   tmpreg = gen_rtx_REG (word_mode, 1);
2986   size_rtx = GEN_INT (actual_fsize);
2987
2988   /* Save RP first.  The calling conventions manual states RP will
2989      always be stored into the caller's frame at sp-20 or sp - 16
2990      depending on which ABI is in use.  */
2991   if ((regs_ever_live[2] || profile_flag) && TARGET_64BIT)
2992     store_reg (2, -16, STACK_POINTER_REGNUM);
2993
2994   if ((regs_ever_live[2] || profile_flag) && ! TARGET_64BIT)
2995     store_reg (2, -20, STACK_POINTER_REGNUM);
2996
2997   /* Allocate the local frame and set up the frame pointer if needed.  */
2998   if (actual_fsize)
2999   {
3000     if (frame_pointer_needed)
3001       {
3002         /* Copy the old frame pointer temporarily into %r1.  Set up the
3003            new stack pointer, then store away the saved old frame pointer
3004            into the stack at sp+actual_fsize and at the same time update
3005            the stack pointer by actual_fsize bytes.  Two versions, first
3006            handles small (<8k) frames.  The second handles large (>8k)
3007            frames.  */
3008         emit_move_insn (tmpreg, frame_pointer_rtx);
3009         emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
3010         if (VAL_14_BITS_P (actual_fsize))
3011           emit_insn (gen_post_store (stack_pointer_rtx, tmpreg, size_rtx));
3012         else
3013           {
3014             /* It is incorrect to store the saved frame pointer at *sp,
3015                then increment sp (writes beyond the current stack boundary).
3016
3017                So instead use stwm to store at *sp and post-increment the
3018                stack pointer as an atomic operation.  Then increment sp to
3019                finish allocating the new frame.  */
3020             emit_insn (gen_post_store (stack_pointer_rtx, tmpreg,
3021                        GEN_INT (64)));
3022             set_reg_plus_d (STACK_POINTER_REGNUM,
3023                             STACK_POINTER_REGNUM,
3024                             actual_fsize - 64);
3025           }
3026       }
3027     /* no frame pointer needed.  */
3028     else
3029       {
3030         /* In some cases we can perform the first callee register save
3031            and allocating the stack frame at the same time.   If so, just
3032            make a note of it and defer allocating the frame until saving
3033            the callee registers.  */
3034         if (VAL_14_BITS_P (-actual_fsize)
3035             && local_fsize == 0
3036             && ! profile_flag
3037             && ! flag_pic)
3038           merge_sp_adjust_with_store = 1;
3039         /* Can not optimize.  Adjust the stack frame by actual_fsize bytes.  */
3040         else if (actual_fsize != 0)
3041           set_reg_plus_d (STACK_POINTER_REGNUM,
3042                           STACK_POINTER_REGNUM,
3043                           actual_fsize);
3044       }
3045   }
3046
3047   /* The hppa calling conventions say that %r19, the pic offset
3048      register, is saved at sp - 32 (in this function's frame)  when
3049      generating PIC code.  FIXME:  What is the correct thing to do
3050      for functions which make no calls and allocate no frame?  Do
3051      we need to allocate a frame, or can we just omit the save?   For
3052      now we'll just omit the save.  */
3053   if (actual_fsize != 0 && flag_pic && !TARGET_64BIT)
3054     store_reg (PIC_OFFSET_TABLE_REGNUM, -32, STACK_POINTER_REGNUM);
3055
3056   /* Profiling code.
3057
3058      Instead of taking one argument, the counter label, as most normal
3059      mcounts do, _mcount appears to behave differently on the HPPA.  It
3060      takes the return address of the caller, the address of this routine,
3061      and the address of the label.  Also, it isn't magic, so
3062      argument registers have to be preserved.  */
3063   if (profile_flag)
3064     {
3065       int pc_offset, i, arg_offset, basereg, offsetadj;
3066
3067       pc_offset = 4 + (frame_pointer_needed
3068                        ? (VAL_14_BITS_P (actual_fsize) ? 12 : 20)
3069                        : (VAL_14_BITS_P (actual_fsize) ? 4 : 8));
3070
3071       /* When the function has a frame pointer, use it as the base
3072          register for saving/restore registers.  Else use the stack
3073          pointer.  Adjust the offset according to the frame size if
3074          this function does not have a frame pointer.  */
3075
3076       basereg = frame_pointer_needed ? FRAME_POINTER_REGNUM
3077                                      : STACK_POINTER_REGNUM;
3078       offsetadj = frame_pointer_needed ? 0 : actual_fsize;
3079
3080       /* Horrid hack.  emit_function_prologue will modify this RTL in
3081          place to get the expected results.   sprintf here is just to
3082          put something in the name.  */
3083       sprintf(hp_profile_label_name, "LP$%04d", -1);
3084       hp_profile_label_rtx = gen_rtx_SYMBOL_REF (Pmode,
3085                                                  hp_profile_label_name);
3086       if (current_function_returns_struct)
3087         store_reg (STRUCT_VALUE_REGNUM, - 12 - offsetadj, basereg);
3088       if (current_function_needs_context)
3089         store_reg (STATIC_CHAIN_REGNUM, - 16 - offsetadj, basereg);
3090
3091       for (i = 26, arg_offset = -36 - offsetadj; i >= 23; i--, arg_offset -= 4)
3092         if (regs_ever_live [i])
3093           {
3094             store_reg (i, arg_offset, basereg);
3095             /* Deal with arg_offset not fitting in 14 bits.  */
3096             pc_offset += VAL_14_BITS_P (arg_offset) ? 4 : 8;
3097           }
3098
3099       emit_move_insn (gen_rtx_REG (word_mode, 26), gen_rtx_REG (word_mode, 2));
3100       emit_move_insn (tmpreg, gen_rtx_HIGH (Pmode, hp_profile_label_rtx));
3101       emit_move_insn (gen_rtx_REG (Pmode, 24),
3102                       gen_rtx_LO_SUM (Pmode, tmpreg, hp_profile_label_rtx));
3103       /* %r25 is set from within the output pattern.  */
3104       emit_insn (gen_call_profiler (GEN_INT (- pc_offset - 20)));
3105
3106       /* Restore argument registers.  */
3107       for (i = 26, arg_offset = -36 - offsetadj; i >= 23; i--, arg_offset -= 4)
3108         if (regs_ever_live [i])
3109           load_reg (i, arg_offset, basereg);
3110
3111       if (current_function_returns_struct)
3112         load_reg (STRUCT_VALUE_REGNUM, -12 - offsetadj, basereg);
3113
3114       if (current_function_needs_context)
3115         load_reg (STATIC_CHAIN_REGNUM, -16 - offsetadj, basereg);
3116     }
3117
3118   /* Normal register save.
3119
3120      Do not save the frame pointer in the frame_pointer_needed case.  It
3121      was done earlier.  */
3122   if (frame_pointer_needed)
3123     {
3124       for (i = 18, offset = local_fsize; i >= 4; i--)
3125         if (regs_ever_live[i] && ! call_used_regs[i])
3126           {
3127             store_reg (i, offset, FRAME_POINTER_REGNUM);
3128             offset += UNITS_PER_WORD;
3129             gr_saved++;
3130           }
3131       /* Account for %r3 which is saved in a special place.  */
3132       gr_saved++;
3133     }
3134   /* No frame pointer needed.  */
3135   else
3136     {
3137       for (i = 18, offset = local_fsize - actual_fsize; i >= 3; i--)
3138         if (regs_ever_live[i] && ! call_used_regs[i])
3139           {
3140             /* If merge_sp_adjust_with_store is nonzero, then we can
3141                optimize the first GR save.  */
3142             if (merge_sp_adjust_with_store)
3143               {
3144                 merge_sp_adjust_with_store = 0;
3145                 emit_insn (gen_post_store (stack_pointer_rtx,
3146                                            gen_rtx_REG (word_mode, i),
3147                                            GEN_INT (-offset)));
3148               }
3149             else
3150               store_reg (i, offset, STACK_POINTER_REGNUM);
3151             offset += UNITS_PER_WORD;
3152             gr_saved++;
3153           }
3154
3155       /* If we wanted to merge the SP adjustment with a GR save, but we never
3156          did any GR saves, then just emit the adjustment here.  */
3157       if (merge_sp_adjust_with_store)
3158         set_reg_plus_d (STACK_POINTER_REGNUM,
3159                         STACK_POINTER_REGNUM,
3160                         actual_fsize);
3161     }
3162
3163   /* Align pointer properly (doubleword boundary).  */
3164   offset = (offset + 7) & ~7;
3165
3166   /* Floating point register store.  */
3167   if (save_fregs)
3168     {
3169       /* First get the frame or stack pointer to the start of the FP register
3170          save area.  */
3171       if (frame_pointer_needed)
3172         set_reg_plus_d (1, FRAME_POINTER_REGNUM, offset);
3173       else
3174         set_reg_plus_d (1, STACK_POINTER_REGNUM, offset);
3175
3176       /* Now actually save the FP registers.  */
3177       for (i = FP_SAVED_REG_LAST; i >= FP_SAVED_REG_FIRST; i -= FP_REG_STEP)
3178         {
3179           if (regs_ever_live[i]
3180               || (! TARGET_64BIT && regs_ever_live[i + 1]))
3181             {
3182               emit_move_insn (gen_rtx_MEM (DFmode,
3183                                            gen_rtx_POST_INC (DFmode, tmpreg)),
3184                               gen_rtx_REG (DFmode, i));
3185               fr_saved++;
3186             }
3187         }
3188     }
3189 }
3190
3191
3192 void
3193 output_function_epilogue (file, size)
3194      FILE *file;
3195      int size ATTRIBUTE_UNUSED;
3196 {
3197   rtx insn = get_last_insn ();
3198
3199   /* hppa_expand_epilogue does the dirty work now.  We just need
3200      to output the assembler directives which denote the end
3201      of a function.
3202
3203      To make debuggers happy, emit a nop if the epilogue was completely
3204      eliminated due to a volatile call as the last insn in the
3205      current function.  That way the return address (in %r2) will
3206      always point to a valid instruction in the current function.  */
3207
3208   /* Get the last real insn.  */
3209   if (GET_CODE (insn) == NOTE)
3210     insn = prev_real_insn (insn);
3211
3212   /* If it is a sequence, then look inside.  */
3213   if (insn && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
3214     insn = XVECEXP (PATTERN (insn), 0, 0);
3215
3216   /* If insn is a CALL_INSN, then it must be a call to a volatile
3217      function (otherwise there would be epilogue insns).  */
3218   if (insn && GET_CODE (insn) == CALL_INSN)
3219     fputs ("\tnop\n", file);
3220
3221   fputs ("\t.EXIT\n\t.PROCEND\n", file);
3222 }
3223
3224 void
3225 hppa_expand_epilogue ()
3226 {
3227   rtx tmpreg;
3228   int offset,i;
3229   int merge_sp_adjust_with_load  = 0;
3230
3231   /* We will use this often.  */
3232   tmpreg = gen_rtx_REG (word_mode, 1);
3233
3234   /* Try to restore RP early to avoid load/use interlocks when
3235      RP gets used in the return (bv) instruction.  This appears to still
3236      be necessary even when we schedule the prologue and epilogue. */
3237   if (frame_pointer_needed
3238       && !TARGET_64BIT
3239       && (regs_ever_live [2] || profile_flag))
3240     load_reg (2, -20, FRAME_POINTER_REGNUM);
3241   else if (TARGET_64BIT && frame_pointer_needed
3242            && (regs_ever_live[2] || profile_flag))
3243     load_reg (2, -16, FRAME_POINTER_REGNUM);
3244   else if (TARGET_64BIT
3245            && ! frame_pointer_needed
3246            && (regs_ever_live[2] || profile_flag)
3247            && VAL_14_BITS_P (actual_fsize + 20))
3248     load_reg (2, - (actual_fsize + 16), STACK_POINTER_REGNUM);
3249   /* No frame pointer, and stack is smaller than 8k.  */
3250   else if (! frame_pointer_needed
3251            && VAL_14_BITS_P (actual_fsize + 20)
3252            && (regs_ever_live[2] || profile_flag))
3253     load_reg (2, - (actual_fsize + 20), STACK_POINTER_REGNUM);
3254
3255   /* General register restores.  */
3256   if (frame_pointer_needed)
3257     {
3258       for (i = 18, offset = local_fsize; i >= 4; i--)
3259         if (regs_ever_live[i] && ! call_used_regs[i])
3260           {
3261             load_reg (i, offset, FRAME_POINTER_REGNUM);
3262             offset += UNITS_PER_WORD;
3263           }
3264     }
3265   else
3266     {
3267       for (i = 18, offset = local_fsize - actual_fsize; i >= 3; i--)
3268         {
3269           if (regs_ever_live[i] && ! call_used_regs[i])
3270             {
3271               /* Only for the first load.
3272                  merge_sp_adjust_with_load holds the register load
3273                  with which we will merge the sp adjustment.  */
3274               if (VAL_14_BITS_P (actual_fsize + 20)
3275                   && local_fsize == 0
3276                   && ! merge_sp_adjust_with_load)
3277                 merge_sp_adjust_with_load = i;
3278               else
3279                 load_reg (i, offset, STACK_POINTER_REGNUM);
3280               offset += UNITS_PER_WORD;
3281             }
3282         }
3283     }
3284
3285   /* Align pointer properly (doubleword boundary).  */
3286   offset = (offset + 7) & ~7;
3287
3288   /* FP register restores.  */
3289   if (save_fregs)
3290     {
3291       /* Adjust the register to index off of.  */
3292       if (frame_pointer_needed)
3293         set_reg_plus_d (1, FRAME_POINTER_REGNUM, offset);
3294       else
3295         set_reg_plus_d (1, STACK_POINTER_REGNUM, offset);
3296
3297       /* Actually do the restores now.  */
3298       for (i = FP_SAVED_REG_LAST; i >= FP_SAVED_REG_FIRST; i -= FP_REG_STEP)
3299         {
3300           if (regs_ever_live[i]
3301               || (! TARGET_64BIT && regs_ever_live[i + 1]))
3302             {
3303               emit_move_insn (gen_rtx_REG (DFmode, i),
3304                               gen_rtx_MEM (DFmode,
3305                                            gen_rtx_POST_INC (DFmode, tmpreg)));
3306             }
3307         }
3308     }
3309
3310   /* Emit a blockage insn here to keep these insns from being moved to
3311      an earlier spot in the epilogue, or into the main instruction stream.
3312
3313      This is necessary as we must not cut the stack back before all the
3314      restores are finished.  */
3315   emit_insn (gen_blockage ());
3316   /* No frame pointer, but we have a stack greater than 8k.  We restore
3317      %r2 very late in this case.  (All other cases are restored as early
3318      as possible.)  */
3319   if (! frame_pointer_needed
3320       && ! VAL_14_BITS_P (actual_fsize + 20)
3321       && ! TARGET_64BIT
3322       && (regs_ever_live[2] || profile_flag))
3323     {
3324       set_reg_plus_d (STACK_POINTER_REGNUM,
3325                       STACK_POINTER_REGNUM,
3326                       - actual_fsize);
3327
3328       /* This used to try and be clever by not depending on the value in
3329          %r30 and instead use the value held in %r1 (so that the 2nd insn
3330          which sets %r30 could be put in the delay slot of the return insn).
3331         
3332          That won't work since if the stack is exactly 8k set_reg_plus_d
3333          doesn't set %r1, just %r30.  */
3334       load_reg (2, - 20, STACK_POINTER_REGNUM);
3335     }
3336   else if (! frame_pointer_needed
3337            && ! VAL_14_BITS_P (actual_fsize + 20)
3338            && TARGET_64BIT
3339            && (regs_ever_live[2] || profile_flag))
3340     {
3341       set_reg_plus_d (STACK_POINTER_REGNUM,
3342                       STACK_POINTER_REGNUM,
3343                       - actual_fsize);
3344
3345       /* This used to try and be clever by not depending on the value in
3346          %r30 and instead use the value held in %r1 (so that the 2nd insn
3347          which sets %r30 could be put in the delay slot of the return insn).
3348         
3349          That won't work since if the stack is exactly 8k set_reg_plus_d
3350          doesn't set %r1, just %r30.  */
3351       load_reg (2, - 16, STACK_POINTER_REGNUM);
3352     }
3353
3354   /* Reset stack pointer (and possibly frame pointer).  The stack 
3355      pointer is initially set to fp + 64 to avoid a race condition.  */
3356   else if (frame_pointer_needed)
3357     {
3358       set_reg_plus_d (STACK_POINTER_REGNUM, FRAME_POINTER_REGNUM, 64);
3359       emit_insn (gen_pre_load (frame_pointer_rtx, 
3360                                stack_pointer_rtx,
3361                                GEN_INT (-64)));
3362     }
3363   /* If we were deferring a callee register restore, do it now.  */
3364   else if (! frame_pointer_needed  && merge_sp_adjust_with_load)
3365     emit_insn (gen_pre_load (gen_rtx_REG (word_mode, merge_sp_adjust_with_load),
3366                              stack_pointer_rtx,
3367                              GEN_INT (- actual_fsize)));
3368   else if (actual_fsize != 0)
3369     set_reg_plus_d (STACK_POINTER_REGNUM,
3370                     STACK_POINTER_REGNUM,
3371                     - actual_fsize);
3372 }
3373
3374 /* Set up a callee saved register for the pic offset table register.  */
3375 void hppa_init_pic_save ()
3376 {
3377   rtx insn, picreg;
3378
3379   picreg = gen_rtx_REG (word_mode, PIC_OFFSET_TABLE_REGNUM);
3380   PIC_OFFSET_TABLE_SAVE_RTX = gen_reg_rtx (Pmode);
3381   insn = gen_rtx_SET (VOIDmode, PIC_OFFSET_TABLE_SAVE_RTX, picreg);
3382
3383   /* Emit the insn at the beginning of the function after the prologue.  */
3384   push_topmost_sequence ();
3385   emit_insn_after (insn, last_parm_insn ? last_parm_insn : get_insns ());
3386   pop_topmost_sequence ();
3387 }
3388
3389 /* Fetch the return address for the frame COUNT steps up from
3390    the current frame, after the prologue.  FRAMEADDR is the
3391    frame pointer of the COUNT frame.
3392
3393    We want to ignore any export stub remnants here.
3394
3395    The value returned is used in two different ways:
3396
3397         1. To find a function's caller.
3398
3399         2. To change the return address for a function.
3400
3401    This function handles most instances of case 1; however, it will
3402    fail if there are two levels of stubs to execute on the return
3403    path.  The only way I believe that can happen is if the return value
3404    needs a parameter relocation, which never happens for C code.
3405
3406    This function handles most instances of case 2; however, it will
3407    fail if we did not originally have stub code on the return path
3408    but will need code on the new return path.  This can happen if
3409    the caller & callee are both in the main program, but the new
3410    return location is in a shared library.
3411
3412    To handle this correctly we need to set the return pointer at
3413    frame-20 to point to a return stub frame-24 to point to the
3414    location we wish to return to.  */
3415
3416 rtx
3417 return_addr_rtx (count, frameaddr)
3418      int count ATTRIBUTE_UNUSED;
3419      rtx frameaddr;
3420 {
3421   rtx label;
3422   rtx saved_rp;
3423   rtx ins;
3424
3425   saved_rp = gen_reg_rtx (Pmode);
3426
3427   /* First, we start off with the normal return address pointer from
3428      -20[frameaddr].  */
3429
3430   if (TARGET_64BIT)
3431     return gen_rtx_MEM (Pmode, plus_constant (frameaddr, -16));
3432   else
3433     emit_move_insn (saved_rp, plus_constant (frameaddr, -5 * UNITS_PER_WORD));
3434
3435   /* Get pointer to the instruction stream.  We have to mask out the
3436      privilege level from the two low order bits of the return address
3437      pointer here so that ins will point to the start of the first
3438      instruction that would have been executed if we returned.  */
3439   ins = copy_to_reg (gen_rtx_AND (Pmode,
3440                                   copy_to_reg (gen_rtx_MEM (Pmode, saved_rp)),
3441                                   MASK_RETURN_ADDR));
3442   label = gen_label_rtx ();
3443
3444   /* Check the instruction stream at the normal return address for the
3445      export stub:
3446
3447         0x4bc23fd1 | stub+8:   ldw -18(sr0,sp),rp
3448         0x004010a1 | stub+12:  ldsid (sr0,rp),r1
3449         0x00011820 | stub+16:  mtsp r1,sr0
3450         0xe0400002 | stub+20:  be,n 0(sr0,rp)
3451
3452      If it is an export stub, than our return address is really in
3453      -24[frameaddr].  */
3454
3455   emit_cmp_insn (gen_rtx_MEM (SImode, ins),
3456                  GEN_INT (0x4bc23fd1),
3457                  NE, NULL_RTX, SImode, 1, 0);
3458   emit_jump_insn (gen_bne (label));
3459
3460   emit_cmp_insn (gen_rtx_MEM (SImode, plus_constant (ins, 4)),
3461                  GEN_INT (0x004010a1),
3462                  NE, NULL_RTX, SImode, 1, 0);
3463   emit_jump_insn (gen_bne (label));
3464
3465   emit_cmp_insn (gen_rtx_MEM (SImode, plus_constant (ins, 8)),
3466                  GEN_INT (0x00011820),
3467                  NE, NULL_RTX, SImode, 1, 0);
3468   emit_jump_insn (gen_bne (label));
3469
3470   emit_cmp_insn (gen_rtx_MEM (SImode, plus_constant (ins, 12)),
3471                  GEN_INT (0xe0400002),
3472                  NE, NULL_RTX, SImode, 1, 0);
3473
3474   /* If there is no export stub then just use our initial guess of
3475      -20[frameaddr].  */
3476
3477   emit_jump_insn (gen_bne (label));
3478
3479   /* Here we know that our return address pointer points to an export
3480      stub.  We don't want to return the address of the export stub,
3481      but rather the return address that leads back into user code.
3482      That return address is stored at -24[frameaddr].  */
3483
3484   emit_move_insn (saved_rp, plus_constant (frameaddr, -6 * UNITS_PER_WORD));
3485
3486   emit_label (label);
3487   return gen_rtx_MEM (Pmode, memory_address (Pmode, saved_rp));
3488 }
3489
3490 /* This is only valid once reload has completed because it depends on
3491    knowing exactly how much (if any) frame there is and...
3492
3493    It's only valid if there is no frame marker to de-allocate and...
3494
3495    It's only valid if %r2 hasn't been saved into the caller's frame
3496    (we're not profiling and %r2 isn't live anywhere).  */
3497 int
3498 hppa_can_use_return_insn_p ()
3499 {
3500   return (reload_completed
3501           && (compute_frame_size (get_frame_size (), 0) ? 0 : 1)
3502           && ! profile_flag
3503           && ! regs_ever_live[2]
3504           && ! frame_pointer_needed);
3505 }
3506
3507 void
3508 emit_bcond_fp (code, operand0)
3509      enum rtx_code code;
3510      rtx operand0;
3511 {
3512   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
3513                                gen_rtx_IF_THEN_ELSE (VOIDmode,
3514                                                      gen_rtx_fmt_ee (code,
3515                                                               VOIDmode,
3516                                                               gen_rtx_REG (CCFPmode, 0),
3517                                                               const0_rtx),
3518                                                      gen_rtx_LABEL_REF (VOIDmode, operand0),
3519                                                      pc_rtx)));
3520
3521 }
3522
3523 rtx
3524 gen_cmp_fp (code, operand0, operand1)
3525      enum rtx_code code;
3526      rtx operand0, operand1;
3527 {
3528   return gen_rtx_SET (VOIDmode, gen_rtx_REG (CCFPmode, 0),
3529                       gen_rtx_fmt_ee (code, CCFPmode, operand0, operand1));
3530 }
3531
3532 /* Adjust the cost of a scheduling dependency.  Return the new cost of
3533    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
3534
3535 int
3536 pa_adjust_cost (insn, link, dep_insn, cost)
3537      rtx insn;
3538      rtx link;
3539      rtx dep_insn;
3540      int cost;
3541 {
3542   enum attr_type attr_type;
3543
3544   /* Don't adjust costs for a pa8000 chip.  */
3545   if (pa_cpu >= PROCESSOR_8000)
3546     return cost;
3547
3548   if (! recog_memoized (insn))
3549     return 0;
3550
3551   attr_type = get_attr_type (insn);
3552
3553   if (REG_NOTE_KIND (link) == 0)
3554     {
3555       /* Data dependency; DEP_INSN writes a register that INSN reads some
3556          cycles later.  */
3557
3558       if (attr_type == TYPE_FPSTORE)
3559         {
3560           rtx pat = PATTERN (insn);
3561           rtx dep_pat = PATTERN (dep_insn);
3562           if (GET_CODE (pat) == PARALLEL)
3563             {
3564               /* This happens for the fstXs,mb patterns.  */
3565               pat = XVECEXP (pat, 0, 0);
3566             }
3567           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
3568             /* If this happens, we have to extend this to schedule
3569                optimally.  Return 0 for now.  */
3570           return 0;
3571
3572           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
3573             {
3574               if (! recog_memoized (dep_insn))
3575                 return 0;
3576               /* DEP_INSN is writing its result to the register
3577                  being stored in the fpstore INSN.  */
3578               switch (get_attr_type (dep_insn))
3579                 {
3580                 case TYPE_FPLOAD:
3581                   /* This cost 3 cycles, not 2 as the md says for the
3582                      700 and 7100.  */
3583                   return cost + 1;
3584
3585                 case TYPE_FPALU:
3586                 case TYPE_FPMULSGL:
3587                 case TYPE_FPMULDBL:
3588                 case TYPE_FPDIVSGL:
3589                 case TYPE_FPDIVDBL:
3590                 case TYPE_FPSQRTSGL:
3591                 case TYPE_FPSQRTDBL:
3592                   /* In these important cases, we save one cycle compared to
3593                      when flop instruction feed each other.  */
3594                   return cost - 1;
3595
3596                 default:
3597                   return cost;
3598                 }
3599             }
3600         }
3601
3602       /* For other data dependencies, the default cost specified in the
3603          md is correct.  */
3604       return cost;
3605     }
3606   else if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
3607     {
3608       /* Anti dependency; DEP_INSN reads a register that INSN writes some
3609          cycles later.  */
3610
3611       if (attr_type == TYPE_FPLOAD)
3612         {
3613           rtx pat = PATTERN (insn);
3614           rtx dep_pat = PATTERN (dep_insn);
3615           if (GET_CODE (pat) == PARALLEL)
3616             {
3617               /* This happens for the fldXs,mb patterns.  */
3618               pat = XVECEXP (pat, 0, 0);
3619             }
3620           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
3621             /* If this happens, we have to extend this to schedule
3622                optimally.  Return 0 for now.  */
3623           return 0;
3624
3625           if (reg_mentioned_p (SET_DEST (pat), SET_SRC (dep_pat)))
3626             {
3627               if (! recog_memoized (dep_insn))
3628                 return 0;
3629               switch (get_attr_type (dep_insn))
3630                 {
3631                 case TYPE_FPALU:
3632                 case TYPE_FPMULSGL:
3633                 case TYPE_FPMULDBL:
3634                 case TYPE_FPDIVSGL:
3635                 case TYPE_FPDIVDBL:
3636                 case TYPE_FPSQRTSGL:
3637                 case TYPE_FPSQRTDBL:
3638                   /* A fpload can't be issued until one cycle before a
3639                      preceding arithmetic operation has finished if
3640                      the target of the fpload is any of the sources
3641                      (or destination) of the arithmetic operation.  */
3642                   return cost - 1;
3643
3644                 default:
3645                   return 0;
3646                 }
3647             }
3648         }
3649       else if (attr_type == TYPE_FPALU)
3650         {
3651           rtx pat = PATTERN (insn);
3652           rtx dep_pat = PATTERN (dep_insn);
3653           if (GET_CODE (pat) == PARALLEL)
3654             {
3655               /* This happens for the fldXs,mb patterns.  */
3656               pat = XVECEXP (pat, 0, 0);
3657             }
3658           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
3659             /* If this happens, we have to extend this to schedule
3660                optimally.  Return 0 for now.  */
3661           return 0;
3662
3663           if (reg_mentioned_p (SET_DEST (pat), SET_SRC (dep_pat)))
3664             {
3665               if (! recog_memoized (dep_insn))
3666                 return 0;
3667               switch (get_attr_type (dep_insn))
3668                 {
3669                 case TYPE_FPDIVSGL:
3670                 case TYPE_FPDIVDBL:
3671                 case TYPE_FPSQRTSGL:
3672                 case TYPE_FPSQRTDBL:
3673                   /* An ALU flop can't be issued until two cycles before a
3674                      preceding divide or sqrt operation has finished if
3675                      the target of the ALU flop is any of the sources
3676                      (or destination) of the divide or sqrt operation.  */
3677                   return cost - 2;
3678
3679                 default:
3680                   return 0;
3681                 }
3682             }
3683         }
3684
3685       /* For other anti dependencies, the cost is 0.  */
3686       return 0;
3687     }
3688   else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
3689     {
3690       /* Output dependency; DEP_INSN writes a register that INSN writes some
3691          cycles later.  */
3692       if (attr_type == TYPE_FPLOAD)
3693         {
3694           rtx pat = PATTERN (insn);
3695           rtx dep_pat = PATTERN (dep_insn);
3696           if (GET_CODE (pat) == PARALLEL)
3697             {
3698               /* This happens for the fldXs,mb patterns.  */
3699               pat = XVECEXP (pat, 0, 0);
3700             }
3701           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
3702             /* If this happens, we have to extend this to schedule
3703                optimally.  Return 0 for now.  */
3704           return 0;
3705
3706           if (reg_mentioned_p (SET_DEST (pat), SET_DEST (dep_pat)))
3707             {
3708               if (! recog_memoized (dep_insn))
3709                 return 0;
3710               switch (get_attr_type (dep_insn))
3711                 {
3712                 case TYPE_FPALU:
3713                 case TYPE_FPMULSGL:
3714                 case TYPE_FPMULDBL:
3715                 case TYPE_FPDIVSGL:
3716                 case TYPE_FPDIVDBL:
3717                 case TYPE_FPSQRTSGL:
3718                 case TYPE_FPSQRTDBL:
3719                   /* A fpload can't be issued until one cycle before a
3720                      preceding arithmetic operation has finished if
3721                      the target of the fpload is the destination of the
3722                      arithmetic operation.  */
3723                   return cost - 1;
3724
3725                 default:
3726                   return 0;
3727                 }
3728             }
3729         }
3730       else if (attr_type == TYPE_FPALU)
3731         {
3732           rtx pat = PATTERN (insn);
3733           rtx dep_pat = PATTERN (dep_insn);
3734           if (GET_CODE (pat) == PARALLEL)
3735             {
3736               /* This happens for the fldXs,mb patterns.  */
3737               pat = XVECEXP (pat, 0, 0);
3738             }
3739           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
3740             /* If this happens, we have to extend this to schedule
3741                optimally.  Return 0 for now.  */
3742           return 0;
3743
3744           if (reg_mentioned_p (SET_DEST (pat), SET_DEST (dep_pat)))
3745             {
3746               if (! recog_memoized (dep_insn))
3747                 return 0;
3748               switch (get_attr_type (dep_insn))
3749                 {
3750                 case TYPE_FPDIVSGL:
3751                 case TYPE_FPDIVDBL:
3752                 case TYPE_FPSQRTSGL:
3753                 case TYPE_FPSQRTDBL:
3754                   /* An ALU flop can't be issued until two cycles before a
3755                      preceding divide or sqrt operation has finished if
3756                      the target of the ALU flop is also the target of
3757                      the divide or sqrt operation.  */
3758                   return cost - 2;
3759
3760                 default:
3761                   return 0;
3762                 }
3763             }
3764         }
3765
3766       /* For other output dependencies, the cost is 0.  */
3767       return 0;
3768     }
3769   else
3770     abort ();
3771 }
3772
3773 /* Return any length adjustment needed by INSN which already has its length
3774    computed as LENGTH.   Return zero if no adjustment is necessary.
3775
3776    For the PA: function calls, millicode calls, and backwards short
3777    conditional branches with unfilled delay slots need an adjustment by +1
3778    (to account for the NOP which will be inserted into the instruction stream).
3779
3780    Also compute the length of an inline block move here as it is too
3781    complicated to express as a length attribute in pa.md.  */
3782 int
3783 pa_adjust_insn_length (insn, length)
3784     rtx insn;
3785     int length;
3786 {
3787   rtx pat = PATTERN (insn);
3788
3789   /* Call insns which are *not* indirect and have unfilled delay slots.  */
3790   if (GET_CODE (insn) == CALL_INSN)
3791     {
3792
3793       if (GET_CODE (XVECEXP (pat, 0, 0)) == CALL
3794           && GET_CODE (XEXP (XEXP (XVECEXP (pat, 0, 0), 0), 0)) == SYMBOL_REF)
3795         return 4;
3796       else if (GET_CODE (XVECEXP (pat, 0, 0)) == SET
3797                && GET_CODE (XEXP (XEXP (XEXP (XVECEXP (pat, 0, 0), 1), 0), 0))
3798                   == SYMBOL_REF)
3799         return 4;
3800       else
3801         return 0;
3802     }
3803   /* Jumps inside switch tables which have unfilled delay slots 
3804      also need adjustment.  */
3805   else if (GET_CODE (insn) == JUMP_INSN
3806            && simplejump_p (insn)
3807            && GET_MODE (insn) == SImode)
3808     return 4;
3809   /* Millicode insn with an unfilled delay slot.  */
3810   else if (GET_CODE (insn) == INSN
3811            && GET_CODE (pat) != SEQUENCE
3812            && GET_CODE (pat) != USE
3813            && GET_CODE (pat) != CLOBBER
3814            && get_attr_type (insn) == TYPE_MILLI)
3815     return 4;
3816   /* Block move pattern.  */
3817   else if (GET_CODE (insn) == INSN
3818            && GET_CODE (pat) == PARALLEL
3819            && GET_CODE (XEXP (XVECEXP (pat, 0, 0), 0)) == MEM
3820            && GET_CODE (XEXP (XVECEXP (pat, 0, 0), 1)) == MEM
3821            && GET_MODE (XEXP (XVECEXP (pat, 0, 0), 0)) == BLKmode
3822            && GET_MODE (XEXP (XVECEXP (pat, 0, 0), 1)) == BLKmode)
3823     return compute_movstrsi_length (insn) - 4;
3824   /* Conditional branch with an unfilled delay slot.  */
3825   else if (GET_CODE (insn) == JUMP_INSN && ! simplejump_p (insn))
3826     {
3827       /* Adjust a short backwards conditional with an unfilled delay slot.  */
3828       if (GET_CODE (pat) == SET
3829           && length == 4
3830           && ! forward_branch_p (insn))
3831         return 4;
3832       else if (GET_CODE (pat) == PARALLEL
3833                && get_attr_type (insn) == TYPE_PARALLEL_BRANCH
3834                && length == 4)
3835         return 4;
3836       /* Adjust dbra insn with short backwards conditional branch with
3837          unfilled delay slot -- only for case where counter is in a
3838          general register register. */
3839       else if (GET_CODE (pat) == PARALLEL
3840                && GET_CODE (XVECEXP (pat, 0, 1)) == SET
3841                && GET_CODE (XEXP (XVECEXP (pat, 0, 1), 0)) == REG
3842                && ! FP_REG_P (XEXP (XVECEXP (pat, 0, 1), 0))
3843                && length == 4
3844                && ! forward_branch_p (insn))
3845         return 4;
3846       else
3847         return 0;
3848     }
3849   return 0;
3850 }
3851
3852 /* Print operand X (an rtx) in assembler syntax to file FILE.
3853    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
3854    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
3855
3856 void
3857 print_operand (file, x, code)
3858      FILE *file;
3859      rtx x;
3860      int code;
3861 {
3862   switch (code)
3863     {
3864     case '#':
3865       /* Output a 'nop' if there's nothing for the delay slot.  */
3866       if (dbr_sequence_length () == 0)
3867         fputs ("\n\tnop", file);
3868       return;
3869     case '*':
3870       /* Output an nullification completer if there's nothing for the */
3871       /* delay slot or nullification is requested.  */
3872       if (dbr_sequence_length () == 0 ||
3873           (final_sequence &&
3874            INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))))
3875         fputs (",n", file);
3876       return;
3877     case 'R':
3878       /* Print out the second register name of a register pair.
3879          I.e., R (6) => 7.  */
3880       fputs (reg_names[REGNO (x)+1], file);
3881       return;
3882     case 'r':
3883       /* A register or zero. */
3884       if (x == const0_rtx
3885           || (x == CONST0_RTX (DFmode))
3886           || (x == CONST0_RTX (SFmode)))
3887         {
3888           fputs ("%r0", file);
3889           return;
3890         }
3891       else
3892         break;
3893     case 'f':
3894       /* A register or zero (floating point). */
3895       if (x == const0_rtx
3896           || (x == CONST0_RTX (DFmode))
3897           || (x == CONST0_RTX (SFmode)))
3898         {
3899           fputs ("%fr0", file);
3900           return;
3901         }
3902       else
3903         break;
3904     case 'C':                   /* Plain (C)ondition */
3905     case 'X':
3906       switch (GET_CODE (x))
3907         {
3908         case EQ:
3909           fputs ("=", file);  break;
3910         case NE:
3911           fputs ("<>", file);  break;
3912         case GT:
3913           fputs (">", file);  break;
3914         case GE:
3915           fputs (">=", file);  break;
3916         case GEU:
3917           fputs (">>=", file);  break;
3918         case GTU:
3919           fputs (">>", file);  break;
3920         case LT:
3921           fputs ("<", file);  break;
3922         case LE:
3923           fputs ("<=", file);  break;
3924         case LEU:
3925           fputs ("<<=", file);  break;
3926         case LTU:
3927           fputs ("<<", file);  break;
3928         default:
3929           abort ();
3930         }
3931       return;
3932     case 'N':                   /* Condition, (N)egated */
3933       switch (GET_CODE (x))
3934         {
3935         case EQ:
3936           fputs ("<>", file);  break;
3937         case NE:
3938           fputs ("=", file);  break;
3939         case GT:
3940           fputs ("<=", file);  break;
3941         case GE:
3942           fputs ("<", file);  break;
3943         case GEU:
3944           fputs ("<<", file);  break;
3945         case GTU:
3946           fputs ("<<=", file);  break;
3947         case LT:
3948           fputs (">=", file);  break;
3949         case LE:
3950           fputs (">", file);  break;
3951         case LEU:
3952           fputs (">>", file);  break;
3953         case LTU:
3954           fputs (">>=", file);  break;
3955         default:
3956           abort ();
3957         }
3958       return;
3959     /* For floating point comparisons. Note that the output predicates are the
3960        complement of the desired mode. */
3961     case 'Y':
3962       switch (GET_CODE (x))
3963         {
3964         case EQ:
3965           fputs ("!=", file);  break;
3966         case NE:
3967           fputs ("=", file);  break;
3968         case GT:
3969           fputs ("!>", file);  break;
3970         case GE:
3971           fputs ("!>=", file);  break;
3972         case LT:
3973           fputs ("!<", file);  break;
3974         case LE:
3975           fputs ("!<=", file);  break;
3976         case LTGT:
3977           fputs ("!<>", file);  break;
3978         case UNLE:
3979           fputs (">", file);  break;
3980         case UNLT:
3981           fputs (">=", file);  break;
3982         case UNGE:
3983           fputs ("<", file);  break;
3984         case UNGT:
3985           fputs ("<=", file);  break;
3986         case UNEQ:
3987           fputs ("<>", file);  break;
3988         case UNORDERED:
3989           fputs ("<=>", file);  break;
3990         case ORDERED:
3991           fputs ("!<=>", file);  break;
3992         default:
3993           abort ();
3994         }
3995       return;
3996     case 'S':                   /* Condition, operands are (S)wapped.  */
3997       switch (GET_CODE (x))
3998         {
3999         case EQ:
4000           fputs ("=", file);  break;
4001         case NE:
4002           fputs ("<>", file);  break;
4003         case GT:
4004           fputs ("<", file);  break;
4005         case GE:
4006           fputs ("<=", file);  break;
4007         case GEU:
4008           fputs ("<<=", file);  break;
4009         case GTU:
4010           fputs ("<<", file);  break;
4011         case LT:
4012           fputs (">", file);  break;
4013         case LE:
4014           fputs (">=", file);  break;
4015         case LEU:
4016           fputs (">>=", file);  break;
4017         case LTU:
4018           fputs (">>", file);  break;
4019         default:
4020           abort ();
4021         }
4022       return;
4023     case 'B':                   /* Condition, (B)oth swapped and negate.  */
4024       switch (GET_CODE (x))
4025         {
4026         case EQ:
4027           fputs ("<>", file);  break;
4028         case NE:
4029           fputs ("=", file);  break;
4030         case GT:
4031           fputs (">=", file);  break;
4032         case GE:
4033           fputs (">", file);  break;
4034         case GEU:
4035           fputs (">>", file);  break;
4036         case GTU:
4037           fputs (">>=", file);  break;
4038         case LT:
4039           fputs ("<=", file);  break;
4040         case LE:
4041           fputs ("<", file);  break;
4042         case LEU:
4043           fputs ("<<", file);  break;
4044         case LTU:
4045           fputs ("<<=", file);  break;
4046         default:
4047           abort ();
4048         }
4049       return;
4050     case 'k':
4051       if (GET_CODE (x) == CONST_INT)
4052         {
4053           fprintf (file, "%d", ~INTVAL (x));
4054           return;
4055         }
4056       abort();
4057     case 'Q':
4058       if (GET_CODE (x) == CONST_INT)
4059         {
4060           fprintf (file, "%d", 64 - (INTVAL (x) & 63));
4061           return;
4062         }
4063       abort();
4064     case 'L':
4065       if (GET_CODE (x) == CONST_INT)
4066         {
4067           fprintf (file, "%d", 32 - (INTVAL (x) & 31));
4068           return;
4069         }
4070       abort();
4071     case 'O':
4072       if (GET_CODE (x) == CONST_INT && exact_log2 (INTVAL (x)) >= 0)
4073         {
4074           fprintf (file, "%d", exact_log2 (INTVAL (x)));
4075           return;
4076         }
4077       abort();
4078     case 'p':
4079       if (GET_CODE (x) == CONST_INT)
4080         {
4081           fprintf (file, "%d", 63 - (INTVAL (x) & 63));
4082           return;
4083         }
4084       abort();
4085     case 'P':
4086       if (GET_CODE (x) == CONST_INT)
4087         {
4088           fprintf (file, "%d", 31 - (INTVAL (x) & 31));
4089           return;
4090         }
4091       abort();
4092     case 'I':
4093       if (GET_CODE (x) == CONST_INT)
4094         fputs ("i", file);
4095       return;
4096     case 'M':
4097     case 'F':
4098       switch (GET_CODE (XEXP (x, 0)))
4099         {
4100         case PRE_DEC:
4101         case PRE_INC:
4102           if (ASSEMBLER_DIALECT == 0)
4103             fputs ("s,mb", file);
4104           else
4105             fputs (",mb", file);
4106           break;
4107         case POST_DEC:
4108         case POST_INC:
4109           if (ASSEMBLER_DIALECT == 0)
4110             fputs ("s,ma", file);
4111           else
4112             fputs (",ma", file);
4113           break;
4114         case PLUS:
4115           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4116               || GET_CODE (XEXP (XEXP (x, 0), 1)) == MULT)
4117             {
4118               if (ASSEMBLER_DIALECT == 0)
4119                 fputs ("x,s", file);
4120               else
4121                 fputs (",s", file);
4122             }
4123           else if (code == 'F' && ASSEMBLER_DIALECT == 0)
4124             fputs ("s", file);
4125           break;
4126         default:
4127           if (code == 'F' && ASSEMBLER_DIALECT == 0)
4128             fputs ("s", file);
4129           break;
4130         }
4131       return;
4132     case 'G':
4133       output_global_address (file, x, 0);
4134       return;
4135     case 'H':
4136       output_global_address (file, x, 1);
4137       return;
4138     case 0:                     /* Don't do anything special */
4139       break;
4140     case 'Z':
4141       {
4142         unsigned op[3];
4143         compute_zdepwi_operands (INTVAL (x), op);
4144         fprintf (file, "%d,%d,%d", op[0], op[1], op[2]);
4145         return;
4146       }
4147     case 'z':
4148       {
4149         unsigned op[3];
4150         compute_zdepdi_operands (INTVAL (x), op);
4151         fprintf (file, "%d,%d,%d", op[0], op[1], op[2]);
4152         return;
4153       }
4154     case 'c':
4155       /* We can get here from a .vtable_inherit due to our
4156          CONSTANT_ADDRESS_P rejecting perfectly good constant
4157          addresses.  */
4158       break;
4159     default:
4160       abort ();
4161     }
4162   if (GET_CODE (x) == REG)
4163     {
4164       fputs (reg_names [REGNO (x)], file);
4165       if (TARGET_64BIT && FP_REG_P (x) && GET_MODE_SIZE (GET_MODE (x)) <= 4)
4166         {
4167           fputs ("R", file);
4168           return;
4169         }
4170       if (FP_REG_P (x)
4171           && GET_MODE_SIZE (GET_MODE (x)) <= 4
4172           && (REGNO (x) & 1) == 0)
4173         fputs ("L", file);
4174     }
4175   else if (GET_CODE (x) == MEM)
4176     {
4177       int size = GET_MODE_SIZE (GET_MODE (x));
4178       rtx base = NULL_RTX;
4179       switch (GET_CODE (XEXP (x, 0)))
4180         {
4181         case PRE_DEC:
4182         case POST_DEC:
4183           base = XEXP (XEXP (x, 0), 0);
4184           fprintf (file, "-%d(%s)", size, reg_names [REGNO (base)]);
4185           break;
4186         case PRE_INC:
4187         case POST_INC:
4188           base = XEXP (XEXP (x, 0), 0);
4189           fprintf (file, "%d(%s)", size, reg_names [REGNO (base)]);
4190           break;
4191         default:
4192           if (GET_CODE (XEXP (x, 0)) == PLUS
4193               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT)
4194             fprintf (file, "%s(%s)",
4195                      reg_names [REGNO (XEXP (XEXP (XEXP (x, 0), 0), 0))],
4196                      reg_names [REGNO (XEXP (XEXP (x, 0), 1))]);
4197           else if (GET_CODE (XEXP (x, 0)) == PLUS
4198                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == MULT)
4199             fprintf (file, "%s(%s)",
4200                      reg_names [REGNO (XEXP (XEXP (XEXP (x, 0), 1), 0))],
4201                      reg_names [REGNO (XEXP (XEXP (x, 0), 0))]);
4202           else
4203             output_address (XEXP (x, 0));
4204           break;
4205         }
4206     }
4207   else
4208     output_addr_const (file, x);
4209 }
4210
4211 /* output a SYMBOL_REF or a CONST expression involving a SYMBOL_REF. */
4212
4213 void
4214 output_global_address (file, x, round_constant)
4215      FILE *file;
4216      rtx x;
4217      int round_constant;
4218 {
4219
4220   /* Imagine  (high (const (plus ...))).  */
4221   if (GET_CODE (x) == HIGH)
4222     x = XEXP (x, 0);
4223
4224   if (GET_CODE (x) == SYMBOL_REF && read_only_operand (x, VOIDmode))
4225     assemble_name (file, XSTR (x, 0));
4226   else if (GET_CODE (x) == SYMBOL_REF && !flag_pic)
4227     {
4228       assemble_name (file, XSTR (x, 0));
4229       fputs ("-$global$", file);
4230     }
4231   else if (GET_CODE (x) == CONST)
4232     {
4233       const char *sep = "";
4234       int offset = 0;           /* assembler wants -$global$ at end */
4235       rtx base = NULL_RTX;
4236
4237       if (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)
4238         {
4239           base = XEXP (XEXP (x, 0), 0);
4240           output_addr_const (file, base);
4241         }
4242       else if (GET_CODE (XEXP (XEXP (x, 0), 0)) == CONST_INT)
4243         offset = INTVAL (XEXP (XEXP (x, 0), 0));
4244       else abort ();
4245
4246       if (GET_CODE (XEXP (XEXP (x, 0), 1)) == SYMBOL_REF)
4247         {
4248           base = XEXP (XEXP (x, 0), 1);
4249           output_addr_const (file, base);
4250         }
4251       else if (GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
4252         offset = INTVAL (XEXP (XEXP (x, 0),1));
4253       else abort ();
4254
4255       /* How bogus.  The compiler is apparently responsible for
4256          rounding the constant if it uses an LR field selector.
4257
4258          The linker and/or assembler seem a better place since
4259          they have to do this kind of thing already.
4260
4261          If we fail to do this, HP's optimizing linker may eliminate
4262          an addil, but not update the ldw/stw/ldo instruction that
4263          uses the result of the addil.  */
4264       if (round_constant)
4265         offset = ((offset + 0x1000) & ~0x1fff);
4266
4267       if (GET_CODE (XEXP (x, 0)) == PLUS)
4268         {
4269           if (offset < 0)
4270             {
4271               offset = -offset;
4272               sep = "-";
4273             }
4274           else
4275             sep = "+";
4276         }
4277       else if (GET_CODE (XEXP (x, 0)) == MINUS
4278                && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
4279         sep = "-";
4280       else abort ();
4281
4282       if (!read_only_operand (base, VOIDmode) && !flag_pic)
4283         fputs ("-$global$", file);
4284       if (offset)
4285         fprintf (file,"%s%d", sep, offset);
4286     }
4287   else
4288     output_addr_const (file, x);
4289 }
4290
4291 void
4292 output_deferred_plabels (file)
4293      FILE *file;
4294 {
4295   int i;
4296   /* If we have deferred plabels, then we need to switch into the data
4297      section and align it to a 4 byte boundary before we output the
4298      deferred plabels.  */
4299   if (n_deferred_plabels)
4300     {
4301       data_section ();
4302       ASM_OUTPUT_ALIGN (file, 2);
4303     }
4304
4305   /* Now output the deferred plabels.  */
4306   for (i = 0; i < n_deferred_plabels; i++)
4307     {
4308       ASM_OUTPUT_INTERNAL_LABEL (file, "L", CODE_LABEL_NUMBER (deferred_plabels[i].internal_label));
4309       assemble_integer (gen_rtx_SYMBOL_REF (VOIDmode,
4310                                             deferred_plabels[i].name), 4, 1);
4311     }
4312 }
4313
4314 /* HP's millicode routines mean something special to the assembler.
4315    Keep track of which ones we have used.  */
4316
4317 enum millicodes { remI, remU, divI, divU, mulI, mulU, end1000 };
4318 static void import_milli                        PARAMS ((enum millicodes));
4319 static char imported[(int)end1000];
4320 static const char * const milli_names[] = {"remI", "remU", "divI", "divU", "mulI", "mulU"};
4321 static char import_string[] = ".IMPORT $$....,MILLICODE";
4322 #define MILLI_START 10
4323
4324 static void
4325 import_milli (code)
4326      enum millicodes code;
4327 {
4328   char str[sizeof (import_string)];
4329
4330   if (!imported[(int)code])
4331     {
4332       imported[(int)code] = 1;
4333       strcpy (str, import_string);
4334       strncpy (str + MILLI_START, milli_names[(int)code], 4);
4335       output_asm_insn (str, 0);
4336     }
4337 }
4338
4339 /* The register constraints have put the operands and return value in
4340    the proper registers. */
4341
4342 const char *
4343 output_mul_insn (unsignedp, insn)
4344      int unsignedp ATTRIBUTE_UNUSED;
4345      rtx insn;
4346 {
4347   import_milli (mulI);
4348   return output_millicode_call (insn, gen_rtx_SYMBOL_REF (Pmode, "$$mulI"));
4349 }
4350
4351 /* Emit the rtl for doing a division by a constant. */
4352
4353 /* Do magic division millicodes exist for this value? */
4354 static int magic_milli[]= {0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0,
4355                              1, 1};
4356
4357 /* We'll use an array to keep track of the magic millicodes and
4358    whether or not we've used them already. [n][0] is signed, [n][1] is
4359    unsigned. */
4360
4361 static int div_milli[16][2];
4362
4363 int
4364 div_operand (op, mode)
4365      rtx op;
4366      enum machine_mode mode;
4367 {
4368   return (mode == SImode
4369           && ((GET_CODE (op) == REG && REGNO (op) == 25)
4370               || (GET_CODE (op) == CONST_INT && INTVAL (op) > 0
4371                   && INTVAL (op) < 16 && magic_milli[INTVAL (op)])));
4372 }
4373
4374 int
4375 emit_hpdiv_const (operands, unsignedp)
4376      rtx *operands;
4377      int unsignedp;
4378 {
4379   if (GET_CODE (operands[2]) == CONST_INT
4380       && INTVAL (operands[2]) > 0
4381       && INTVAL (operands[2]) < 16
4382       && magic_milli[INTVAL (operands[2])])
4383     {
4384       emit_move_insn (gen_rtx_REG (SImode, 26), operands[1]);
4385       emit
4386         (gen_rtx
4387          (PARALLEL, VOIDmode,
4388           gen_rtvec (6, gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, 29),
4389                                      gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4390                                                      SImode,
4391                                                      gen_rtx_REG (SImode, 26),
4392                                                      operands[2])),
4393                      gen_rtx_CLOBBER (VOIDmode, operands[4]),
4394                      gen_rtx_CLOBBER (VOIDmode, operands[3]),
4395                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 26)),
4396                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 25)),
4397                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 31)))));
4398       emit_move_insn (operands[0], gen_rtx_REG (SImode, 29));
4399       return 1;
4400     }
4401   return 0;
4402 }
4403
4404 const char *
4405 output_div_insn (operands, unsignedp, insn)
4406      rtx *operands;
4407      int unsignedp;
4408      rtx insn;
4409 {
4410   int divisor;
4411
4412   /* If the divisor is a constant, try to use one of the special
4413      opcodes .*/
4414   if (GET_CODE (operands[0]) == CONST_INT)
4415     {
4416       static char buf[100];
4417       divisor = INTVAL (operands[0]);
4418       if (!div_milli[divisor][unsignedp])
4419         {
4420           div_milli[divisor][unsignedp] = 1;
4421           if (unsignedp)
4422             output_asm_insn (".IMPORT $$divU_%0,MILLICODE", operands);
4423           else
4424             output_asm_insn (".IMPORT $$divI_%0,MILLICODE", operands);
4425         }
4426       if (unsignedp)
4427         {
4428           sprintf (buf, "$$divU_%d", INTVAL (operands[0]));
4429           return output_millicode_call (insn,
4430                                         gen_rtx_SYMBOL_REF (SImode, buf));
4431         }
4432       else
4433         {
4434           sprintf (buf, "$$divI_%d", INTVAL (operands[0]));
4435           return output_millicode_call (insn,
4436                                         gen_rtx_SYMBOL_REF (SImode, buf));
4437         }
4438     }
4439   /* Divisor isn't a special constant. */
4440   else
4441     {
4442       if (unsignedp)
4443         {
4444           import_milli (divU);
4445           return output_millicode_call (insn,
4446                                         gen_rtx_SYMBOL_REF (SImode, "$$divU"));
4447         }
4448       else
4449         {
4450           import_milli (divI);
4451           return output_millicode_call (insn,
4452                                         gen_rtx_SYMBOL_REF (SImode, "$$divI"));
4453         }
4454     }
4455 }
4456
4457 /* Output a $$rem millicode to do mod. */
4458
4459 const char *
4460 output_mod_insn (unsignedp, insn)
4461      int unsignedp;
4462      rtx insn;
4463 {
4464   if (unsignedp)
4465     {
4466       import_milli (remU);
4467       return output_millicode_call (insn,
4468                                     gen_rtx_SYMBOL_REF (SImode, "$$remU"));
4469     }
4470   else
4471     {
4472       import_milli (remI);
4473       return output_millicode_call (insn,
4474                                     gen_rtx_SYMBOL_REF (SImode, "$$remI"));
4475     }
4476 }
4477
4478 void
4479 output_arg_descriptor (call_insn)
4480      rtx call_insn;
4481 {
4482   const char *arg_regs[4];
4483   enum machine_mode arg_mode;
4484   rtx link;
4485   int i, output_flag = 0;
4486   int regno;
4487
4488   /* We neither need nor want argument location descriptors for the
4489      64bit runtime environment.  */
4490   if (TARGET_64BIT)
4491     return;
4492
4493   for (i = 0; i < 4; i++)
4494     arg_regs[i] = 0;
4495
4496   /* Specify explicitly that no argument relocations should take place
4497      if using the portable runtime calling conventions.  */
4498   if (TARGET_PORTABLE_RUNTIME)
4499     {
4500       fputs ("\t.CALL ARGW0=NO,ARGW1=NO,ARGW2=NO,ARGW3=NO,RETVAL=NO\n",
4501              asm_out_file);
4502       return;
4503     }
4504
4505   if (GET_CODE (call_insn) != CALL_INSN)
4506     abort ();
4507   for (link = CALL_INSN_FUNCTION_USAGE (call_insn); link; link = XEXP (link, 1))
4508     {
4509       rtx use = XEXP (link, 0);
4510
4511       if (! (GET_CODE (use) == USE
4512              && GET_CODE (XEXP (use, 0)) == REG
4513              && FUNCTION_ARG_REGNO_P (REGNO (XEXP (use, 0)))))
4514         continue;
4515
4516       arg_mode = GET_MODE (XEXP (use, 0));
4517       regno = REGNO (XEXP (use, 0));
4518       if (regno >= 23 && regno <= 26)
4519         {
4520           arg_regs[26 - regno] = "GR";
4521           if (arg_mode == DImode)
4522             arg_regs[25 - regno] = "GR";
4523         }
4524       else if (regno >= 32 && regno <= 39)
4525         {
4526           if (arg_mode == SFmode)
4527             arg_regs[(regno - 32) / 2] = "FR";
4528           else
4529             {
4530 #ifndef HP_FP_ARG_DESCRIPTOR_REVERSED
4531               arg_regs[(regno - 34) / 2] = "FR";
4532               arg_regs[(regno - 34) / 2 + 1] = "FU";
4533 #else
4534               arg_regs[(regno - 34) / 2] = "FU";
4535               arg_regs[(regno - 34) / 2 + 1] = "FR";
4536 #endif
4537             }
4538         }
4539     }
4540   fputs ("\t.CALL ", asm_out_file);
4541   for (i = 0; i < 4; i++)
4542     {
4543       if (arg_regs[i])
4544         {
4545           if (output_flag++)
4546             fputc (',', asm_out_file);
4547           fprintf (asm_out_file, "ARGW%d=%s", i, arg_regs[i]);
4548         }
4549     }
4550   fputc ('\n', asm_out_file);
4551 }
4552 \f
4553 /* Return the class of any secondary reload register that is needed to
4554    move IN into a register in class CLASS using mode MODE.
4555
4556    Profiling has showed this routine and its descendants account for
4557    a significant amount of compile time (~7%).  So it has been
4558    optimized to reduce redundant computations and eliminate useless
4559    function calls.
4560
4561    It might be worthwhile to try and make this a leaf function too.  */
4562
4563 enum reg_class
4564 secondary_reload_class (class, mode, in)
4565      enum reg_class class;
4566      enum machine_mode mode;
4567      rtx in;
4568 {
4569   int regno, is_symbolic;
4570
4571   /* Trying to load a constant into a FP register during PIC code
4572      generation will require %r1 as a scratch register.  */
4573   if (flag_pic == 2
4574       && GET_MODE_CLASS (mode) == MODE_INT
4575       && FP_REG_CLASS_P (class)
4576       && (GET_CODE (in) == CONST_INT || GET_CODE (in) == CONST_DOUBLE))
4577     return R1_REGS;
4578
4579   /* Profiling showed the PA port spends about 1.3% of its compilation
4580      time in true_regnum from calls inside secondary_reload_class.  */
4581
4582   if (GET_CODE (in) == REG)
4583     {
4584       regno = REGNO (in);
4585       if (regno >= FIRST_PSEUDO_REGISTER)
4586         regno = true_regnum (in);
4587     }
4588   else if (GET_CODE (in) == SUBREG)
4589     regno = true_regnum (in);
4590   else
4591     regno = -1;
4592
4593   /* If we have something like (mem (mem (...)), we can safely assume the
4594      inner MEM will end up in a general register after reloading, so there's
4595      no need for a secondary reload.  */
4596   if (GET_CODE (in) == MEM
4597       && GET_CODE (XEXP (in, 0)) == MEM)
4598     return NO_REGS;
4599
4600   /* Handle out of range displacement for integer mode loads/stores of
4601      FP registers.  */
4602   if (((regno >= FIRST_PSEUDO_REGISTER || regno == -1)
4603        && GET_MODE_CLASS (mode) == MODE_INT
4604        && FP_REG_CLASS_P (class))
4605       || (class == SHIFT_REGS && (regno <= 0 || regno >= 32)))
4606     return GENERAL_REGS;
4607
4608   if (GET_CODE (in) == HIGH)
4609     in = XEXP (in, 0);
4610
4611   /* Profiling has showed GCC spends about 2.6% of its compilation
4612      time in symbolic_operand from calls inside secondary_reload_class.
4613
4614      We use an inline copy and only compute its return value once to avoid
4615      useless work.  */
4616   switch (GET_CODE (in))
4617     {
4618       rtx tmp;
4619
4620       case SYMBOL_REF:
4621       case LABEL_REF:
4622         is_symbolic = 1;
4623         break;
4624       case CONST:
4625         tmp = XEXP (in, 0);
4626         is_symbolic = ((GET_CODE (XEXP (tmp, 0)) == SYMBOL_REF
4627                         || GET_CODE (XEXP (tmp, 0)) == LABEL_REF)
4628                        && GET_CODE (XEXP (tmp, 1)) == CONST_INT);
4629         break;
4630
4631       default:
4632         is_symbolic = 0;
4633         break;
4634     }
4635   
4636   if (!flag_pic
4637       && is_symbolic
4638       && read_only_operand (in, VOIDmode))
4639     return NO_REGS;
4640
4641   if (class != R1_REGS && is_symbolic)
4642     return R1_REGS;
4643
4644   return NO_REGS;
4645 }
4646
4647 enum direction
4648 function_arg_padding (mode, type)
4649      enum machine_mode mode;
4650      tree type;
4651 {
4652   int size;
4653
4654   if (mode == BLKmode)
4655     {
4656       if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4657         size = int_size_in_bytes (type) * BITS_PER_UNIT;
4658       else
4659         return upward;          /* Don't know if this is right, but */
4660                                 /* same as old definition. */
4661     }
4662   else
4663     size = GET_MODE_BITSIZE (mode);
4664   if (size < PARM_BOUNDARY)
4665     return downward;
4666   else if (size % PARM_BOUNDARY)
4667     return upward;
4668   else
4669     return none;
4670 }
4671
4672 \f
4673 /* Do what is necessary for `va_start'.  We look at the current function
4674    to determine if stdargs or varargs is used and fill in an initial
4675    va_list.  A pointer to this constructor is returned.  */
4676
4677 struct rtx_def *
4678 hppa_builtin_saveregs ()
4679 {
4680   rtx offset, dest;
4681   tree fntype = TREE_TYPE (current_function_decl);
4682   int argadj = ((!(TYPE_ARG_TYPES (fntype) != 0
4683                    && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4684                        != void_type_node)))
4685                 ? UNITS_PER_WORD : 0);
4686
4687   if (argadj)
4688     offset = plus_constant (current_function_arg_offset_rtx, argadj);
4689   else
4690     offset = current_function_arg_offset_rtx;
4691
4692   if (TARGET_64BIT)
4693     {
4694       int i, off;
4695       
4696       /* Adjust for varargs/stdarg differences.  */
4697       if (argadj)
4698         offset = plus_constant (current_function_arg_offset_rtx, -argadj);
4699       else
4700         offset = current_function_arg_offset_rtx;
4701
4702       /* We need to save %r26 .. %r19 inclusive starting at offset -64
4703          from the incoming arg pointer and growing to larger addresses.  */
4704       for (i = 26, off = -64; i >= 19; i--, off += 8)
4705         emit_move_insn (gen_rtx_MEM (word_mode,
4706                                      plus_constant (arg_pointer_rtx, off)),
4707                         gen_rtx_REG (word_mode, i));
4708
4709       /* The incoming args pointer points just beyond the flushback area;
4710          normally this is not a serious concern.  Howver, when we are doing
4711          varargs/stdargs we want to make the arg pointer point to the start
4712          of the incoming argument area.  */
4713       emit_move_insn (virtual_incoming_args_rtx,
4714                       plus_constant (arg_pointer_rtx, -64));
4715
4716       /* Now return a pointer to the first anonymous argument.  */
4717       return copy_to_reg (expand_binop (Pmode, add_optab,
4718                                         virtual_incoming_args_rtx,
4719                                         offset, 0, 0, OPTAB_LIB_WIDEN));
4720     }
4721
4722   /* Store general registers on the stack. */
4723   dest = gen_rtx_MEM (BLKmode,
4724                       plus_constant (current_function_internal_arg_pointer,
4725                                      -16));
4726   MEM_ALIAS_SET (dest) = get_varargs_alias_set ();
4727   move_block_from_reg (23, dest, 4, 4 * UNITS_PER_WORD);
4728
4729   /* move_block_from_reg will emit code to store the argument registers
4730      individually as scalar stores.
4731
4732      However, other insns may later load from the same addresses for
4733      a structure load (passing a struct to a varargs routine).
4734
4735      The alias code assumes that such aliasing can never happen, so we
4736      have to keep memory referencing insns from moving up beyond the
4737      last argument register store.  So we emit a blockage insn here.  */
4738   emit_insn (gen_blockage ());
4739
4740   if (current_function_check_memory_usage)
4741     emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
4742                        dest, ptr_mode,
4743                        GEN_INT (4 * UNITS_PER_WORD), TYPE_MODE (sizetype),
4744                        GEN_INT (MEMORY_USE_RW),
4745                        TYPE_MODE (integer_type_node));
4746
4747   return copy_to_reg (expand_binop (Pmode, add_optab,
4748                                     current_function_internal_arg_pointer,
4749                                     offset, 0, 0, OPTAB_LIB_WIDEN));
4750 }
4751
4752 void
4753 hppa_va_start (stdarg_p, valist, nextarg)
4754      int stdarg_p ATTRIBUTE_UNUSED;
4755      tree valist;
4756      rtx nextarg;
4757 {
4758   nextarg = expand_builtin_saveregs ();
4759   std_expand_builtin_va_start (1, valist, nextarg);
4760 }
4761
4762 rtx
4763 hppa_va_arg (valist, type)
4764      tree valist, type;
4765 {
4766   HOST_WIDE_INT align, size, ofs;
4767   tree t, ptr, pptr;
4768
4769   if (TARGET_64BIT)
4770     {
4771       /* Every argument in PA64 is passed by value (including large structs).
4772          Arguments with size greater than 8 must be aligned 0 MOD 16. */
4773
4774       size = int_size_in_bytes (type);
4775       if (size > UNITS_PER_WORD)
4776         {
4777           t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
4778                      build_int_2 (2 * UNITS_PER_WORD - 1, 0));
4779           t = build (BIT_AND_EXPR, TREE_TYPE (t), t, 
4780                      build_int_2 (-2 * UNITS_PER_WORD, -1));
4781           t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4782           TREE_SIDE_EFFECTS (t) = 1;
4783           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4784         }
4785       return std_expand_builtin_va_arg (valist, type);
4786     }
4787
4788   /* Compute the rounded size of the type.  */
4789   align = PARM_BOUNDARY / BITS_PER_UNIT;
4790   size = int_size_in_bytes (type);
4791
4792   ptr = build_pointer_type (type);
4793
4794   /* "Large" types are passed by reference.  */
4795   if (size > 8)
4796     {
4797       t = build (PREDECREMENT_EXPR, TREE_TYPE (valist), valist, 
4798                  build_int_2 (POINTER_SIZE / BITS_PER_UNIT, 0));
4799       TREE_SIDE_EFFECTS (t) = 1;
4800
4801       pptr = build_pointer_type (ptr);
4802       t = build1 (NOP_EXPR, pptr, t);
4803       TREE_SIDE_EFFECTS (t) = 1;
4804
4805       t = build1 (INDIRECT_REF, ptr, t);
4806       TREE_SIDE_EFFECTS (t) = 1;
4807     }
4808   else
4809     {
4810       t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
4811                  build_int_2 (-size, -1));
4812
4813       /* Copied from va-pa.h, but we probably don't need to align
4814          to word size, since we generate and preserve that invariant.  */
4815       t = build (BIT_AND_EXPR, TREE_TYPE (valist), t,
4816                  build_int_2 ((size > 4 ? -8 : -4), -1));
4817
4818       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4819       TREE_SIDE_EFFECTS (t) = 1;
4820       
4821       ofs = (8 - size) % 4;
4822       if (ofs)
4823         {
4824           t = build (PLUS_EXPR, TREE_TYPE (valist), t, build_int_2 (ofs, 0));
4825           TREE_SIDE_EFFECTS (t) = 1;
4826         }
4827
4828       t = build1 (NOP_EXPR, ptr, t);
4829       TREE_SIDE_EFFECTS (t) = 1;
4830     }
4831
4832   /* Calculate!  */
4833   return expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
4834 }
4835
4836
4837
4838 /* This routine handles all the normal conditional branch sequences we
4839    might need to generate.  It handles compare immediate vs compare
4840    register, nullification of delay slots, varying length branches,
4841    negated branches, and all combinations of the above.  It returns the
4842    output appropriate to emit the branch corresponding to all given
4843    parameters.  */
4844
4845 const char *
4846 output_cbranch (operands, nullify, length, negated, insn)
4847   rtx *operands;
4848   int nullify, length, negated;
4849   rtx insn;
4850 {
4851   static char buf[100];
4852   int useskip = 0;
4853
4854   /* A conditional branch to the following instruction (eg the delay slot) is
4855      asking for a disaster.  This can happen when not optimizing.
4856
4857      In such cases it is safe to emit nothing.  */
4858
4859   if (next_active_insn (JUMP_LABEL (insn)) == next_active_insn (insn))
4860     return "";
4861
4862   /* If this is a long branch with its delay slot unfilled, set `nullify'
4863      as it can nullify the delay slot and save a nop.  */
4864   if (length == 8 && dbr_sequence_length () == 0)
4865     nullify = 1;
4866
4867   /* If this is a short forward conditional branch which did not get
4868      its delay slot filled, the delay slot can still be nullified.  */
4869   if (! nullify && length == 4 && dbr_sequence_length () == 0)
4870     nullify = forward_branch_p (insn);
4871
4872   /* A forward branch over a single nullified insn can be done with a
4873      comclr instruction.  This avoids a single cycle penalty due to
4874      mis-predicted branch if we fall through (branch not taken).  */
4875   if (length == 4
4876       && next_real_insn (insn) != 0
4877       && get_attr_length (next_real_insn (insn)) == 4
4878       && JUMP_LABEL (insn) == next_nonnote_insn (next_real_insn (insn))
4879       && nullify)
4880     useskip = 1;
4881
4882   switch (length)
4883     {
4884       /* All short conditional branches except backwards with an unfilled
4885          delay slot.  */
4886       case 4:
4887         if (useskip)
4888           strcpy (buf, "{com%I2clr,|cmp%I2clr,}");
4889         else
4890           strcpy (buf, "{com%I2b,|cmp%I2b,}");
4891         if (GET_MODE (operands[1]) == DImode)
4892           strcat (buf, "*");
4893         if (negated)
4894           strcat (buf, "%B3");
4895         else
4896           strcat (buf, "%S3");
4897         if (useskip)
4898           strcat (buf, " %2,%r1,%%r0");
4899         else if (nullify)
4900           strcat (buf, ",n %2,%r1,%0");
4901         else
4902           strcat (buf, " %2,%r1,%0");
4903         break;
4904
4905      /* All long conditionals.  Note an short backward branch with an
4906         unfilled delay slot is treated just like a long backward branch
4907         with an unfilled delay slot.  */
4908       case 8:
4909         /* Handle weird backwards branch with a filled delay slot
4910            with is nullified.  */
4911         if (dbr_sequence_length () != 0
4912             && ! forward_branch_p (insn)
4913             && nullify)
4914           {
4915             strcpy (buf, "{com%I2b,|cmp%I2b,}");
4916             if (GET_MODE (operands[1]) == DImode)
4917               strcat (buf, "*");
4918             if (negated)
4919               strcat (buf, "%S3");
4920             else
4921               strcat (buf, "%B3");
4922             strcat (buf, ",n %2,%r1,.+12\n\tb %0");
4923           }
4924         /* Handle short backwards branch with an unfilled delay slot.
4925            Using a comb;nop rather than comiclr;bl saves 1 cycle for both
4926            taken and untaken branches.  */
4927         else if (dbr_sequence_length () == 0
4928                  && ! forward_branch_p (insn)
4929                  && INSN_ADDRESSES_SET_P ()
4930                  && VAL_14_BITS_P (INSN_ADDRESSES (INSN_UID (JUMP_LABEL (insn)))
4931                                     - INSN_ADDRESSES (INSN_UID (insn)) - 8))
4932           {
4933             strcpy (buf, "{com%I2b,|cmp%I2b,}");
4934             if (GET_MODE (operands[1]) == DImode)
4935               strcat (buf, "*");
4936             if (negated)
4937               strcat (buf, "%B3 %2,%r1,%0%#");
4938             else
4939               strcat (buf, "%S3 %2,%r1,%0%#");
4940           }
4941         else
4942           {
4943             strcpy (buf, "{com%I2clr,|cmp%I2clr,}");
4944             if (GET_MODE (operands[1]) == DImode)
4945               strcat (buf, "*");
4946             if (negated)
4947               strcat (buf, "%S3");
4948             else
4949               strcat (buf, "%B3");
4950             if (nullify)
4951               strcat (buf, " %2,%r1,%%r0\n\tb,n %0");
4952             else
4953               strcat (buf, " %2,%r1,%%r0\n\tb %0");
4954           }
4955         break;
4956
4957       case 20:
4958         /* Very long branch.  Right now we only handle these when not
4959            optimizing.  See "jump" pattern in pa.md for details.  */
4960         if (optimize)
4961           abort ();
4962
4963         /* Create a reversed conditional branch which branches around
4964            the following insns.  */
4965         if (negated)
4966           strcpy (buf, "{com%I2b,%S3,n %2,%r1,.+20|cmp%I2b,%S3,n %2,%r1,.+20}");
4967         else
4968           strcpy (buf, "{com%I2b,%B3,n %2,%r1,.+20|cmp%I2b,%B3,n %2,%r1,.+20}");
4969         if (GET_MODE (operands[1]) == DImode)
4970           {
4971             if (negated)
4972               strcpy (buf,
4973                       "{com%I2b,*%S3,n %2,%r1,.+20|cmp%I2b,*%S3,n %2,%r1,.+20}");
4974             else
4975               strcpy (buf,
4976                       "{com%I2b,*%B3,n %2,%r1,.+20|cmp%I2b,*%B3,n %2,%r1,.+20}");
4977           }
4978         output_asm_insn (buf, operands);
4979
4980         /* Output an insn to save %r1.  */
4981         output_asm_insn ("stw %%r1,-16(%%r30)", operands);
4982
4983         /* Now output a very long branch to the original target.  */
4984         output_asm_insn ("ldil L'%l0,%%r1\n\tbe R'%l0(%%sr4,%%r1)", operands);
4985
4986         /* Now restore the value of %r1 in the delay slot.  We're not
4987            optimizing so we know nothing else can be in the delay slot.  */
4988         return "ldw -16(%%r30),%%r1";
4989
4990       case 28:
4991         /* Very long branch when generating PIC code.  Right now we only
4992            handle these when not optimizing.  See "jump" pattern in pa.md
4993            for details.  */
4994         if (optimize)
4995           abort ();
4996
4997         /* Create a reversed conditional branch which branches around
4998            the following insns.  */
4999         if (negated)
5000           strcpy (buf, "{com%I2b,%S3,n %2,%r1,.+28|cmp%I2b,%S3,n %2,%r1,.+28}");
5001         else
5002           strcpy (buf, "{com%I2b,%B3,n %2,%r1,.+28|cmp%I2b,%B3,n %2,%r1,.+28}");
5003         if (GET_MODE (operands[1]) == DImode)
5004           {
5005             if (negated)
5006               strcpy (buf, "{com%I2b,*%S3,n %2,%r1,.+28|cmp%I2b,*%S3,n %2,%r1,.+28}");
5007             else
5008               strcpy (buf, "{com%I2b,*%B3,n %2,%r1,.+28|cmp%I2b,*%B3,n %2,%r1,.+28}");
5009           }
5010         output_asm_insn (buf, operands);
5011
5012         /* Output an insn to save %r1.  */
5013         output_asm_insn ("stw %%r1,-16(%%r30)", operands);
5014
5015         /* Now output a very long PIC branch to the original target.  */
5016         {
5017           rtx xoperands[5];
5018
5019           xoperands[0] = operands[0];
5020           xoperands[1] = operands[1];
5021           xoperands[2] = operands[2];
5022           xoperands[3] = operands[3];
5023           xoperands[4] = gen_label_rtx ();
5024
5025           output_asm_insn ("{bl|b,l} .+8,%%r1\n\taddil L'%l0-%l4,%%r1",
5026                            xoperands);
5027           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
5028                                      CODE_LABEL_NUMBER (xoperands[4]));
5029           output_asm_insn ("ldo R'%l0-%l4(%%r1),%%r1\n\tbv %%r0(%%r1)",
5030                            xoperands);
5031         }
5032
5033         /* Now restore the value of %r1 in the delay slot.  We're not
5034            optimizing so we know nothing else can be in the delay slot.  */
5035         return "ldw -16(%%r30),%%r1";
5036         
5037       default:
5038         abort();
5039     }
5040   return buf;
5041 }
5042
5043 /* This routine handles all the branch-on-bit conditional branch sequences we
5044    might need to generate.  It handles nullification of delay slots,
5045    varying length branches, negated branches and all combinations of the
5046    above.  it returns the appropriate output template to emit the branch.  */
5047
5048 const char *
5049 output_bb (operands, nullify, length, negated, insn, which)
5050   rtx *operands ATTRIBUTE_UNUSED;
5051   int nullify, length, negated;
5052   rtx insn;
5053   int which;
5054 {
5055   static char buf[100];
5056   int useskip = 0;
5057
5058   /* A conditional branch to the following instruction (eg the delay slot) is
5059      asking for a disaster.  I do not think this can happen as this pattern
5060      is only used when optimizing; jump optimization should eliminate the
5061      jump.  But be prepared just in case.  */
5062
5063   if (next_active_insn (JUMP_LABEL (insn)) == next_active_insn (insn))
5064     return "";
5065
5066   /* If this is a long branch with its delay slot unfilled, set `nullify'
5067      as it can nullify the delay slot and save a nop.  */
5068   if (length == 8 && dbr_sequence_length () == 0)
5069     nullify = 1;
5070
5071   /* If this is a short forward conditional branch which did not get
5072      its delay slot filled, the delay slot can still be nullified.  */
5073   if (! nullify && length == 4 && dbr_sequence_length () == 0)
5074     nullify = forward_branch_p (insn);
5075
5076   /* A forward branch over a single nullified insn can be done with a
5077      extrs instruction.  This avoids a single cycle penalty due to
5078      mis-predicted branch if we fall through (branch not taken).  */
5079
5080   if (length == 4
5081       && next_real_insn (insn) != 0
5082       && get_attr_length (next_real_insn (insn)) == 4
5083       && JUMP_LABEL (insn) == next_nonnote_insn (next_real_insn (insn))
5084       && nullify)
5085     useskip = 1;
5086
5087   switch (length)
5088     {
5089
5090       /* All short conditional branches except backwards with an unfilled
5091          delay slot.  */
5092       case 4:
5093         if (useskip)
5094           strcpy (buf, "{extrs,|extrw,s,}");
5095         else
5096           strcpy (buf, "bb,");
5097         if (useskip && GET_MODE (operands[0]) == DImode)
5098           strcpy (buf, "extrd,s,*");
5099         else if (GET_MODE (operands[0]) == DImode)
5100           strcpy (buf, "bb,*");
5101         if ((which == 0 && negated)
5102              || (which == 1 && ! negated))
5103           strcat (buf, ">=");
5104         else
5105           strcat (buf, "<");
5106         if (useskip)
5107           strcat (buf, " %0,%1,1,%%r0");
5108         else if (nullify && negated)
5109           strcat (buf, ",n %0,%1,%3");
5110         else if (nullify && ! negated)
5111           strcat (buf, ",n %0,%1,%2");
5112         else if (! nullify && negated)
5113           strcat (buf, "%0,%1,%3");
5114         else if (! nullify && ! negated)
5115           strcat (buf, " %0,%1,%2");
5116         break;
5117
5118      /* All long conditionals.  Note an short backward branch with an
5119         unfilled delay slot is treated just like a long backward branch
5120         with an unfilled delay slot.  */
5121       case 8:
5122         /* Handle weird backwards branch with a filled delay slot
5123            with is nullified.  */
5124         if (dbr_sequence_length () != 0
5125             && ! forward_branch_p (insn)
5126             && nullify)
5127           {
5128             strcpy (buf, "bb,");
5129             if (GET_MODE (operands[0]) == DImode)
5130               strcat (buf, "*");
5131             if ((which == 0 && negated)
5132                 || (which == 1 && ! negated))
5133               strcat (buf, "<");
5134             else
5135               strcat (buf, ">=");
5136             if (negated)
5137               strcat (buf, ",n %0,%1,.+12\n\tb %3");
5138             else
5139               strcat (buf, ",n %0,%1,.+12\n\tb %2");
5140           }
5141         /* Handle short backwards branch with an unfilled delay slot.
5142            Using a bb;nop rather than extrs;bl saves 1 cycle for both
5143            taken and untaken branches.  */
5144         else if (dbr_sequence_length () == 0
5145                  && ! forward_branch_p (insn)
5146                  && INSN_ADDRESSES_SET_P ()
5147                  && VAL_14_BITS_P (INSN_ADDRESSES (INSN_UID (JUMP_LABEL (insn)))
5148                                     - INSN_ADDRESSES (INSN_UID (insn)) - 8))
5149           {
5150             strcpy (buf, "bb,");
5151             if (GET_MODE (operands[0]) == DImode)
5152               strcat (buf, "*");
5153             if ((which == 0 && negated)
5154                 || (which == 1 && ! negated))
5155               strcat (buf, ">=");
5156             else
5157               strcat (buf, "<");
5158             if (negated)
5159               strcat (buf, " %0,%1,%3%#");
5160             else
5161               strcat (buf, " %0,%1,%2%#");
5162           }
5163         else
5164           {
5165             strcpy (buf, "{extrs,|extrw,s,}");
5166             if (GET_MODE (operands[0]) == DImode)
5167               strcpy (buf, "extrd,s,*");
5168             if ((which == 0 && negated)
5169                 || (which == 1 && ! negated))
5170               strcat (buf, "<");
5171             else
5172               strcat (buf, ">=");
5173             if (nullify && negated)
5174               strcat (buf, " %0,%1,1,%%r0\n\tb,n %3");
5175             else if (nullify && ! negated)
5176               strcat (buf, " %0,%1,1,%%r0\n\tb,n %2");
5177             else if (negated)
5178               strcat (buf, " %0,%1,1,%%r0\n\tb %3");
5179             else
5180               strcat (buf, " %0,%1,1,%%r0\n\tb %2");
5181           }
5182         break;
5183
5184       default:
5185         abort();
5186     }
5187   return buf;
5188 }
5189
5190 /* This routine handles all the branch-on-variable-bit conditional branch
5191    sequences we might need to generate.  It handles nullification of delay
5192    slots, varying length branches, negated branches and all combinations
5193    of the above.  it returns the appropriate output template to emit the
5194    branch.  */
5195
5196 const char *
5197 output_bvb (operands, nullify, length, negated, insn, which)
5198   rtx *operands ATTRIBUTE_UNUSED;
5199   int nullify, length, negated;
5200   rtx insn;
5201   int which;
5202 {
5203   static char buf[100];
5204   int useskip = 0;
5205
5206   /* A conditional branch to the following instruction (eg the delay slot) is
5207      asking for a disaster.  I do not think this can happen as this pattern
5208      is only used when optimizing; jump optimization should eliminate the
5209      jump.  But be prepared just in case.  */
5210
5211   if (next_active_insn (JUMP_LABEL (insn)) == next_active_insn (insn))
5212     return "";
5213
5214   /* If this is a long branch with its delay slot unfilled, set `nullify'
5215      as it can nullify the delay slot and save a nop.  */
5216   if (length == 8 && dbr_sequence_length () == 0)
5217     nullify = 1;
5218
5219   /* If this is a short forward conditional branch which did not get
5220      its delay slot filled, the delay slot can still be nullified.  */
5221   if (! nullify && length == 4 && dbr_sequence_length () == 0)
5222     nullify = forward_branch_p (insn);
5223
5224   /* A forward branch over a single nullified insn can be done with a
5225      extrs instruction.  This avoids a single cycle penalty due to
5226      mis-predicted branch if we fall through (branch not taken).  */
5227
5228   if (length == 4
5229       && next_real_insn (insn) != 0
5230       && get_attr_length (next_real_insn (insn)) == 4
5231       && JUMP_LABEL (insn) == next_nonnote_insn (next_real_insn (insn))
5232       && nullify)
5233     useskip = 1;
5234
5235   switch (length)
5236     {
5237
5238       /* All short conditional branches except backwards with an unfilled
5239          delay slot.  */
5240       case 4:
5241         if (useskip)
5242           strcpy (buf, "{vextrs,|extrw,s,}");
5243         else
5244           strcpy (buf, "{bvb,|bb,}");
5245         if (useskip && GET_MODE (operands[0]) == DImode)
5246           strcpy (buf, "extrd,s,*}");
5247         else if (GET_MODE (operands[0]) == DImode)
5248           strcpy (buf, "bb,*");
5249         if ((which == 0 && negated)
5250              || (which == 1 && ! negated))
5251           strcat (buf, ">=");
5252         else
5253           strcat (buf, "<");
5254         if (useskip)
5255           strcat (buf, "{ %0,1,%%r0| %0,%%sar,1,%%r0}");
5256         else if (nullify && negated)
5257           strcat (buf, "{,n %0,%3|,n %0,%%sar,%3}");
5258         else if (nullify && ! negated)
5259           strcat (buf, "{,n %0,%2|,n %0,%%sar,%2}");
5260         else if (! nullify && negated)
5261           strcat (buf, "{%0,%3|%0,%%sar,%3}");
5262         else if (! nullify && ! negated)
5263           strcat (buf, "{ %0,%2| %0,%%sar,%2}");
5264         break;
5265
5266      /* All long conditionals.  Note an short backward branch with an
5267         unfilled delay slot is treated just like a long backward branch
5268         with an unfilled delay slot.  */
5269       case 8:
5270         /* Handle weird backwards branch with a filled delay slot
5271            with is nullified.  */
5272         if (dbr_sequence_length () != 0
5273             && ! forward_branch_p (insn)
5274             && nullify)
5275           {
5276             strcpy (buf, "{bvb,|bb,}");
5277             if (GET_MODE (operands[0]) == DImode)
5278               strcat (buf, "*");
5279             if ((which == 0 && negated)
5280                 || (which == 1 && ! negated))
5281               strcat (buf, "<");
5282             else
5283               strcat (buf, ">=");
5284             if (negated)
5285               strcat (buf, "{,n %0,.+12\n\tb %3|,n %0,%%sar,.+12\n\tb %3}");
5286             else
5287               strcat (buf, "{,n %0,.+12\n\tb %2|,n %0,%%sar,.+12\n\tb %2}");
5288           }
5289         /* Handle short backwards branch with an unfilled delay slot.
5290            Using a bb;nop rather than extrs;bl saves 1 cycle for both
5291            taken and untaken branches.  */
5292         else if (dbr_sequence_length () == 0
5293                  && ! forward_branch_p (insn)
5294                  && INSN_ADDRESSES_SET_P ()
5295                  && VAL_14_BITS_P (INSN_ADDRESSES (INSN_UID (JUMP_LABEL (insn)))
5296                                     - INSN_ADDRESSES (INSN_UID (insn)) - 8))
5297           {
5298             strcpy (buf, "{bvb,|bb,}");
5299             if (GET_MODE (operands[0]) == DImode)
5300               strcat (buf, "*");
5301             if ((which == 0 && negated)
5302                 || (which == 1 && ! negated))
5303               strcat (buf, ">=");
5304             else
5305               strcat (buf, "<");
5306             if (negated)
5307               strcat (buf, "{ %0,%3%#| %0,%%sar,%3%#}");
5308             else
5309               strcat (buf, "{ %0,%2%#| %0,%%sar,%2%#}");
5310           }
5311         else
5312           {
5313             strcpy (buf, "{vextrs,|extrw,s,}");
5314             if (GET_MODE (operands[0]) == DImode)
5315               strcpy (buf, "extrd,s,*");
5316             if ((which == 0 && negated)
5317                 || (which == 1 && ! negated))
5318               strcat (buf, "<");
5319             else
5320               strcat (buf, ">=");
5321             if (nullify && negated)
5322               strcat (buf, "{ %0,1,%%r0\n\tb,n %3| %0,%%sar,1,%%r0\n\tb,n %3}");
5323             else if (nullify && ! negated)
5324               strcat (buf, "{ %0,1,%%r0\n\tb,n %2| %0,%%sar,1,%%r0\n\tb,n %2}");
5325             else if (negated)
5326               strcat (buf, "{ %0,1,%%r0\n\tb %3| %0,%%sar,1,%%r0\n\tb %3}");
5327             else
5328               strcat (buf, "{ %0,1,%%r0\n\tb %2| %0,%%sar,1,%%r0\n\tb %2}");
5329           }
5330         break;
5331
5332       default:
5333         abort();
5334     }
5335   return buf;
5336 }
5337
5338 /* Return the output template for emitting a dbra type insn.
5339
5340    Note it may perform some output operations on its own before
5341    returning the final output string.  */
5342 const char *
5343 output_dbra (operands, insn, which_alternative)
5344      rtx *operands;
5345      rtx insn;
5346      int which_alternative;
5347 {
5348
5349   /* A conditional branch to the following instruction (eg the delay slot) is
5350      asking for a disaster.  Be prepared!  */
5351
5352   if (next_active_insn (JUMP_LABEL (insn)) == next_active_insn (insn))
5353     {
5354       if (which_alternative == 0)
5355         return "ldo %1(%0),%0";
5356       else if (which_alternative == 1)
5357         {
5358           output_asm_insn ("{fstws|fstw} %0,-16(%%r30)",operands);
5359           output_asm_insn ("ldw -16(%%r30),%4",operands);
5360           output_asm_insn ("ldo %1(%4),%4\n\tstw %4,-16(%%r30)", operands);
5361           return "{fldws|fldw} -16(%%r30),%0";
5362         }
5363       else
5364         {
5365           output_asm_insn ("ldw %0,%4", operands);
5366           return "ldo %1(%4),%4\n\tstw %4,%0";
5367         }
5368     }
5369
5370   if (which_alternative == 0)
5371     {
5372       int nullify = INSN_ANNULLED_BRANCH_P (insn);
5373       int length = get_attr_length (insn);
5374
5375       /* If this is a long branch with its delay slot unfilled, set `nullify'
5376          as it can nullify the delay slot and save a nop.  */
5377       if (length == 8 && dbr_sequence_length () == 0)
5378         nullify = 1;
5379
5380       /* If this is a short forward conditional branch which did not get
5381          its delay slot filled, the delay slot can still be nullified.  */
5382       if (! nullify && length == 4 && dbr_sequence_length () == 0)
5383         nullify = forward_branch_p (insn);
5384
5385       /* Handle short versions first.  */
5386       if (length == 4 && nullify)
5387         return "addib,%C2,n %1,%0,%3";
5388       else if (length == 4 && ! nullify)
5389         return "addib,%C2 %1,%0,%3";
5390       else if (length == 8)
5391         {
5392           /* Handle weird backwards branch with a fulled delay slot
5393              which is nullified.  */
5394           if (dbr_sequence_length () != 0
5395               && ! forward_branch_p (insn)
5396               && nullify)
5397             return "addib,%N2,n %1,%0,.+12\n\tb %3";
5398           /* Handle short backwards branch with an unfilled delay slot.
5399              Using a addb;nop rather than addi;bl saves 1 cycle for both
5400              taken and untaken branches.  */
5401           else if (dbr_sequence_length () == 0
5402                    && ! forward_branch_p (insn)
5403                    && INSN_ADDRESSES_SET_P ()
5404                    && VAL_14_BITS_P (INSN_ADDRESSES (INSN_UID (JUMP_LABEL (insn)))
5405                                       - INSN_ADDRESSES (INSN_UID (insn)) - 8))
5406               return "addib,%C2 %1,%0,%3%#";
5407
5408           /* Handle normal cases.  */
5409           if (nullify)
5410             return "addi,%N2 %1,%0,%0\n\tb,n %3";
5411           else
5412             return "addi,%N2 %1,%0,%0\n\tb %3";
5413         }
5414       else
5415         abort();
5416     }
5417   /* Deal with gross reload from FP register case.  */
5418   else if (which_alternative == 1)
5419     {
5420       /* Move loop counter from FP register to MEM then into a GR,
5421          increment the GR, store the GR into MEM, and finally reload
5422          the FP register from MEM from within the branch's delay slot.  */
5423       output_asm_insn ("{fstws|fstw} %0,-16(%%r30)\n\tldw -16(%%r30),%4",operands);
5424       output_asm_insn ("ldo %1(%4),%4\n\tstw %4,-16(%%r30)", operands);
5425       if (get_attr_length (insn) == 24)
5426         return "{comb|cmpb},%S2 %%r0,%4,%3\n\t{fldws|fldw} -16(%%r30),%0";
5427       else
5428         return "{comclr|cmpclr},%B2 %%r0,%4,%%r0\n\tb %3\n\t{fldws|fldw} -16(%%r30),%0";
5429     }
5430   /* Deal with gross reload from memory case.  */
5431   else
5432     {
5433       /* Reload loop counter from memory, the store back to memory
5434          happens in the branch's delay slot.   */
5435       output_asm_insn ("ldw %0,%4", operands);
5436       if (get_attr_length (insn) == 12)
5437         return "addib,%C2 %1,%4,%3\n\tstw %4,%0";
5438       else
5439         return "addi,%N2 %1,%4,%4\n\tb %3\n\tstw %4,%0";
5440     }
5441 }
5442
5443 /* Return the output template for emitting a dbra type insn.
5444
5445    Note it may perform some output operations on its own before
5446    returning the final output string.  */
5447 const char *
5448 output_movb (operands, insn, which_alternative, reverse_comparison)
5449      rtx *operands;
5450      rtx insn;
5451      int which_alternative;
5452      int reverse_comparison;
5453 {
5454
5455   /* A conditional branch to the following instruction (eg the delay slot) is
5456      asking for a disaster.  Be prepared!  */
5457
5458   if (next_active_insn (JUMP_LABEL (insn)) == next_active_insn (insn))
5459     {
5460       if (which_alternative == 0)
5461         return "copy %1,%0";
5462       else if (which_alternative == 1)
5463         {
5464           output_asm_insn ("stw %1,-16(%%r30)",operands);
5465           return "{fldws|fldw} -16(%%r30),%0";
5466         }
5467       else if (which_alternative == 2)
5468         return "stw %1,%0";
5469       else
5470         return "mtsar %r1";
5471     }
5472
5473   /* Support the second variant.  */
5474   if (reverse_comparison)
5475     PUT_CODE (operands[2], reverse_condition (GET_CODE (operands[2])));
5476
5477   if (which_alternative == 0)
5478     {
5479       int nullify = INSN_ANNULLED_BRANCH_P (insn);
5480       int length = get_attr_length (insn);
5481
5482       /* If this is a long branch with its delay slot unfilled, set `nullify'
5483          as it can nullify the delay slot and save a nop.  */
5484       if (length == 8 && dbr_sequence_length () == 0)
5485         nullify = 1;
5486
5487       /* If this is a short forward conditional branch which did not get
5488          its delay slot filled, the delay slot can still be nullified.  */
5489       if (! nullify && length == 4 && dbr_sequence_length () == 0)
5490         nullify = forward_branch_p (insn);
5491
5492       /* Handle short versions first.  */
5493       if (length == 4 && nullify)
5494         return "movb,%C2,n %1,%0,%3";
5495       else if (length == 4 && ! nullify)
5496         return "movb,%C2 %1,%0,%3";
5497       else if (length == 8)
5498         {
5499           /* Handle weird backwards branch with a filled delay slot
5500              which is nullified.  */
5501           if (dbr_sequence_length () != 0
5502               && ! forward_branch_p (insn)
5503               && nullify)
5504             return "movb,%N2,n %1,%0,.+12\n\tb %3";
5505
5506           /* Handle short backwards branch with an unfilled delay slot.
5507              Using a movb;nop rather than or;bl saves 1 cycle for both
5508              taken and untaken branches.  */
5509           else if (dbr_sequence_length () == 0
5510                    && ! forward_branch_p (insn)
5511                    && INSN_ADDRESSES_SET_P ()
5512                    && VAL_14_BITS_P (INSN_ADDRESSES (INSN_UID (JUMP_LABEL (insn)))
5513                                       - INSN_ADDRESSES (INSN_UID (insn)) - 8))
5514             return "movb,%C2 %1,%0,%3%#";
5515           /* Handle normal cases.  */
5516           if (nullify)
5517             return "or,%N2 %1,%%r0,%0\n\tb,n %3";
5518           else
5519             return "or,%N2 %1,%%r0,%0\n\tb %3";
5520         }
5521       else
5522         abort();
5523     }
5524   /* Deal with gross reload from FP register case.  */
5525   else if (which_alternative == 1)
5526     {
5527       /* Move loop counter from FP register to MEM then into a GR,
5528          increment the GR, store the GR into MEM, and finally reload
5529          the FP register from MEM from within the branch's delay slot.  */
5530       output_asm_insn ("stw %1,-16(%%r30)",operands);
5531       if (get_attr_length (insn) == 12)
5532         return "{comb|cmpb},%S2 %%r0,%1,%3\n\t{fldws|fldw} -16(%%r30),%0";
5533       else
5534         return "{comclr|cmpclr},%B2 %%r0,%1,%%r0\n\tb %3\n\t{fldws|fldw} -16(%%r30),%0";
5535     }
5536   /* Deal with gross reload from memory case.  */
5537   else if (which_alternative == 2)
5538     {
5539       /* Reload loop counter from memory, the store back to memory
5540          happens in the branch's delay slot.   */
5541       if (get_attr_length (insn) == 8)
5542         return "{comb|cmpb},%S2 %%r0,%1,%3\n\tstw %1,%0";
5543       else
5544         return "{comclr|cmpclr},%B2 %%r0,%1,%%r0\n\tb %3\n\tstw %1,%0";
5545     }
5546   /* Handle SAR as a destination.  */
5547   else
5548     {
5549       if (get_attr_length (insn) == 8)
5550         return "{comb|cmpb},%S2 %%r0,%1,%3\n\tmtsar %r1";
5551       else
5552         return "{comclr|cmpclr},%B2 %%r0,%1,%%r0\n\tbl %3\n\tmtsar %r1";
5553     }
5554 }
5555
5556
5557 /* INSN is a millicode call.  It may have an unconditional jump in its delay
5558    slot.
5559
5560    CALL_DEST is the routine we are calling.  */
5561
5562 const char *
5563 output_millicode_call (insn, call_dest)
5564   rtx insn;
5565   rtx call_dest;
5566 {
5567   int distance;
5568   rtx xoperands[4];
5569   rtx seq_insn;
5570
5571   xoperands[3] = gen_rtx_REG (SImode, 31);
5572   if (TARGET_64BIT)
5573     xoperands[3] = gen_rtx_REG (SImode, 2);
5574
5575   /* Handle common case -- empty delay slot or no jump in the delay slot,
5576      and we're sure that the branch will reach the beginning of the $CODE$
5577      subspace.  */
5578   if ((dbr_sequence_length () == 0
5579        && (get_attr_length (insn) == 8 || get_attr_length (insn) == 28))
5580       || (dbr_sequence_length () != 0
5581           && GET_CODE (NEXT_INSN (insn)) != JUMP_INSN
5582           && get_attr_length (insn) == 4))
5583     {
5584       xoperands[0] = call_dest;
5585       output_asm_insn ("{bl|b,l} %0,%3%#", xoperands);
5586       return "";
5587     }
5588
5589   /* This call may not reach the beginning of the $CODE$ subspace.  */
5590   if (get_attr_length (insn) > 4)
5591     {
5592       int delay_insn_deleted = 0;
5593
5594       /* We need to emit an inline long-call branch.  */
5595       if (dbr_sequence_length () != 0
5596           && GET_CODE (NEXT_INSN (insn)) != JUMP_INSN)
5597         {
5598           /* A non-jump insn in the delay slot.  By definition we can
5599              emit this insn before the call.  */
5600           final_scan_insn (NEXT_INSN (insn), asm_out_file, optimize, 0, 0);
5601
5602           /* Now delete the delay insn.  */
5603           PUT_CODE (NEXT_INSN (insn), NOTE);
5604           NOTE_LINE_NUMBER (NEXT_INSN (insn)) = NOTE_INSN_DELETED;
5605           NOTE_SOURCE_FILE (NEXT_INSN (insn)) = 0;
5606           delay_insn_deleted = 1;
5607         }
5608
5609       /* If we're allowed to use be/ble instructions, then this is the
5610          best sequence to use for a long millicode call.  */
5611       if (TARGET_NO_SPACE_REGS || TARGET_FAST_INDIRECT_CALLS
5612           || ! (flag_pic  || TARGET_PORTABLE_RUNTIME))
5613         {
5614           xoperands[0] = call_dest;
5615           output_asm_insn ("ldil L%%%0,%3", xoperands);
5616           output_asm_insn ("{ble|be,l} R%%%0(%%sr4,%3)", xoperands);
5617           output_asm_insn ("nop", xoperands);
5618         }
5619       /* Pure portable runtime doesn't allow be/ble; we also don't have
5620          PIC support int he assembler/linker, so this sequence is needed.  */
5621       else if (TARGET_PORTABLE_RUNTIME)
5622         {
5623           xoperands[0] = call_dest;
5624           /* Get the address of our target into %r29. */
5625           output_asm_insn ("ldil L%%%0,%%r29", xoperands);
5626           output_asm_insn ("ldo R%%%0(%%r29),%%r29", xoperands);
5627
5628           /* Get our return address into %r31.  */
5629           output_asm_insn ("blr %%r0,%3", xoperands);
5630
5631           /* Jump to our target address in %r29.  */
5632           output_asm_insn ("bv,n %%r0(%%r29)", xoperands);
5633
5634           /* Empty delay slot.  Note this insn gets fetched twice and
5635              executed once.  To be safe we use a nop.  */
5636           output_asm_insn ("nop", xoperands);
5637           return "";
5638         }
5639       /* PIC long millicode call sequence.  */
5640       else
5641         {
5642           xoperands[0] = call_dest;
5643           xoperands[1] = gen_label_rtx ();
5644           /* Get our address + 8 into %r1.  */
5645           output_asm_insn ("{bl|b,l} .+8,%%r1", xoperands);
5646
5647           /* Add %r1 to the offset of our target from the next insn.  */
5648           output_asm_insn ("addil L%%%0-%1,%%r1", xoperands);
5649           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
5650                                      CODE_LABEL_NUMBER (xoperands[1]));
5651           output_asm_insn ("ldo R%%%0-%1(%%r1),%%r1", xoperands);
5652
5653           /* Get the return address into %r31.  */
5654           output_asm_insn ("blr 0,%3", xoperands);
5655
5656           /* Branch to our target which is in %r1.  */
5657           output_asm_insn ("bv,n %%r0(%%r1)", xoperands);
5658
5659           /* Empty delay slot.  Note this insn gets fetched twice and
5660              executed once.  To be safe we use a nop.  */
5661           output_asm_insn ("nop", xoperands);
5662         }
5663
5664       /* If we had a jump in the call's delay slot, output it now.  */
5665       if (dbr_sequence_length () != 0
5666           && !delay_insn_deleted)
5667         {
5668           xoperands[0] = XEXP (PATTERN (NEXT_INSN (insn)), 1);
5669           output_asm_insn ("b,n %0", xoperands);
5670
5671           /* Now delete the delay insn.  */
5672           PUT_CODE (NEXT_INSN (insn), NOTE);
5673           NOTE_LINE_NUMBER (NEXT_INSN (insn)) = NOTE_INSN_DELETED;
5674           NOTE_SOURCE_FILE (NEXT_INSN (insn)) = 0;
5675         }
5676       return "";
5677     }
5678
5679   /* This call has an unconditional jump in its delay slot and the
5680      call is known to reach its target or the beginning of the current
5681      subspace.  */
5682
5683   /* Use the containing sequence insn's address.  */
5684   seq_insn = NEXT_INSN (PREV_INSN (XVECEXP (final_sequence, 0, 0)));
5685
5686   distance = INSN_ADDRESSES (INSN_UID (JUMP_LABEL (NEXT_INSN (insn))))
5687                - INSN_ADDRESSES (INSN_UID (seq_insn)) - 8;
5688
5689   /* If the branch was too far away, emit a normal call followed
5690      by a nop, followed by the unconditional branch.
5691
5692      If the branch is close, then adjust %r2 from within the
5693      call's delay slot.  */
5694
5695   xoperands[0] = call_dest;
5696   xoperands[1] = XEXP (PATTERN (NEXT_INSN (insn)), 1);
5697   if (! VAL_14_BITS_P (distance))
5698     output_asm_insn ("{bl|b,l} %0,%3\n\tnop\n\tb,n %1", xoperands);
5699   else
5700     {
5701       xoperands[2] = gen_label_rtx ();
5702       output_asm_insn ("\n\t{bl|b,l} %0,%3\n\tldo %1-%2(%3),%3",
5703                        xoperands);
5704       ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
5705                                  CODE_LABEL_NUMBER (xoperands[2]));
5706     }
5707
5708   /* Delete the jump.  */
5709   PUT_CODE (NEXT_INSN (insn), NOTE);
5710   NOTE_LINE_NUMBER (NEXT_INSN (insn)) = NOTE_INSN_DELETED;
5711   NOTE_SOURCE_FILE (NEXT_INSN (insn)) = 0;
5712   return "";
5713 }
5714
5715 extern struct obstack permanent_obstack;
5716
5717 /* INSN is either a function call.  It may have an unconditional jump
5718    in its delay slot.
5719
5720    CALL_DEST is the routine we are calling.  */
5721
5722 const char *
5723 output_call (insn, call_dest, sibcall)
5724   rtx insn;
5725   rtx call_dest;
5726   int sibcall;
5727 {
5728   int distance;
5729   rtx xoperands[4];
5730   rtx seq_insn;
5731
5732   /* Handle common case -- empty delay slot or no jump in the delay slot,
5733      and we're sure that the branch will reach the beginning of the $CODE$
5734      subspace.  */
5735   if ((dbr_sequence_length () == 0
5736        && get_attr_length (insn) == 8)
5737       || (dbr_sequence_length () != 0
5738           && GET_CODE (NEXT_INSN (insn)) != JUMP_INSN
5739           && get_attr_length (insn) == 4))
5740     {
5741       xoperands[0] = call_dest;
5742       xoperands[1] = gen_rtx_REG (word_mode, sibcall ? 0 : 2);
5743       output_asm_insn ("{bl|b,l} %0,%1%#", xoperands);
5744       return "";
5745     }
5746
5747   /* This call may not reach the beginning of the $CODE$ subspace.  */
5748   if (get_attr_length (insn) > 8)
5749     {
5750       int delay_insn_deleted = 0;
5751       rtx xoperands[2];
5752       rtx link;
5753
5754       /* We need to emit an inline long-call branch.  Furthermore,
5755          because we're changing a named function call into an indirect
5756          function call well after the parameters have been set up, we
5757          need to make sure any FP args appear in both the integer
5758          and FP registers.  Also, we need move any delay slot insn
5759          out of the delay slot.  And finally, we can't rely on the linker
5760          being able to fix the call to $$dyncall!  -- Yuk!.  */
5761       if (dbr_sequence_length () != 0
5762           && GET_CODE (NEXT_INSN (insn)) != JUMP_INSN)
5763         {
5764           /* A non-jump insn in the delay slot.  By definition we can
5765              emit this insn before the call (and in fact before argument
5766              relocating.  */
5767           final_scan_insn (NEXT_INSN (insn), asm_out_file, optimize, 0, 0);
5768
5769           /* Now delete the delay insn.  */
5770           PUT_CODE (NEXT_INSN (insn), NOTE);
5771           NOTE_LINE_NUMBER (NEXT_INSN (insn)) = NOTE_INSN_DELETED;
5772           NOTE_SOURCE_FILE (NEXT_INSN (insn)) = 0;
5773           delay_insn_deleted = 1;
5774         }
5775
5776       /* Now copy any FP arguments into integer registers.  */
5777       for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
5778         {
5779           int arg_mode, regno;
5780           rtx use = XEXP (link, 0);
5781           if (! (GET_CODE (use) == USE
5782                  && GET_CODE (XEXP (use, 0)) == REG
5783                  && FUNCTION_ARG_REGNO_P (REGNO (XEXP (use, 0)))))
5784             continue;
5785
5786           arg_mode = GET_MODE (XEXP (use, 0));
5787           regno = REGNO (XEXP (use, 0));
5788           /* Is it a floating point register?  */
5789           if (regno >= 32 && regno <= 39)
5790             {
5791               /* Copy from the FP register into an integer register
5792                  (via memory).  */
5793               if (arg_mode == SFmode)
5794                 {
5795                   xoperands[0] = XEXP (use, 0);
5796                   xoperands[1] = gen_rtx_REG (SImode, 26 - (regno - 32) / 2);
5797                   output_asm_insn ("{fstws|fstw} %0,-16(%%sr0,%%r30)",
5798                                     xoperands);
5799                   output_asm_insn ("ldw -16(%%sr0,%%r30),%1", xoperands);
5800                 }
5801               else
5802                 {
5803                   xoperands[0] = XEXP (use, 0);
5804                   xoperands[1] = gen_rtx_REG (DImode, 25 - (regno - 34) / 2);
5805                   output_asm_insn ("{fstds|fstd} %0,-16(%%sr0,%%r30)",
5806                                     xoperands);
5807                   output_asm_insn ("ldw -12(%%sr0,%%r30),%R1", xoperands);
5808                   output_asm_insn ("ldw -16(%%sr0,%%r30),%1", xoperands);
5809                 }
5810             }
5811         }
5812
5813       /* Don't have to worry about TARGET_PORTABLE_RUNTIME here since
5814          we don't have any direct calls in that case.  */
5815         {
5816           int i;
5817           const char *name = XSTR (call_dest, 0);
5818
5819           /* See if we have already put this function on the list
5820              of deferred plabels.  This list is generally small,
5821              so a liner search is not too ugly.  If it proves too
5822              slow replace it with something faster.  */
5823           for (i = 0; i < n_deferred_plabels; i++)
5824             if (strcmp (name, deferred_plabels[i].name) == 0)
5825               break;
5826
5827           /* If the deferred plabel list is empty, or this entry was
5828              not found on the list, create a new entry on the list.  */
5829           if (deferred_plabels == NULL || i == n_deferred_plabels)
5830             {
5831               const char *real_name;
5832
5833               if (deferred_plabels == 0)
5834                 deferred_plabels = (struct deferred_plabel *)
5835                   xmalloc (1 * sizeof (struct deferred_plabel));
5836               else
5837                 deferred_plabels = (struct deferred_plabel *)
5838                   xrealloc (deferred_plabels,
5839                             ((n_deferred_plabels + 1)
5840                              * sizeof (struct deferred_plabel)));
5841
5842               i = n_deferred_plabels++;
5843               deferred_plabels[i].internal_label = gen_label_rtx ();
5844               deferred_plabels[i].name = obstack_alloc (&permanent_obstack,
5845                                                         strlen (name) + 1);
5846               strcpy (deferred_plabels[i].name, name);
5847
5848               /* Gross.  We have just implicitly taken the address of this
5849                  function, mark it as such.  */
5850               STRIP_NAME_ENCODING (real_name, name);
5851               TREE_SYMBOL_REFERENCED (get_identifier (real_name)) = 1;
5852             }
5853
5854           /* We have to load the address of the function using a procedure
5855              label (plabel).  Inline plabels can lose for PIC and other
5856              cases, so avoid them by creating a 32bit plabel in the data
5857              segment.  */
5858           if (flag_pic)
5859             {
5860               xoperands[0] = deferred_plabels[i].internal_label;
5861               xoperands[1] = gen_label_rtx ();
5862
5863               output_asm_insn ("addil LT%%%0,%%r19", xoperands);
5864               output_asm_insn ("ldw RT%%%0(%%r1),%%r22", xoperands);
5865               output_asm_insn ("ldw 0(%%r22),%%r22", xoperands);
5866
5867               /* Get our address + 8 into %r1.  */
5868               output_asm_insn ("{bl|b,l} .+8,%%r1", xoperands);
5869
5870               /* Add %r1 to the offset of dyncall from the next insn.  */
5871               output_asm_insn ("addil L%%$$dyncall-%1,%%r1", xoperands);
5872               ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
5873                                          CODE_LABEL_NUMBER (xoperands[1]));
5874               output_asm_insn ("ldo R%%$$dyncall-%1(%%r1),%%r1", xoperands);
5875
5876               /* Get the return address into %r31.  */
5877               output_asm_insn ("blr %%r0,%%r31", xoperands);
5878
5879               /* Branch to our target which is in %r1.  */
5880               output_asm_insn ("bv %%r0(%%r1)", xoperands);
5881
5882               if (sibcall)
5883                 {
5884                   /* This call never returns, so we do not need to fix the
5885                      return pointer.  */
5886                   output_asm_insn ("nop", xoperands);
5887                 }
5888               else
5889                 {
5890                   /* Copy the return address into %r2 also.  */
5891                   output_asm_insn ("copy %%r31,%%r2", xoperands);
5892                 }
5893             }
5894           else
5895             {
5896               xoperands[0] = deferred_plabels[i].internal_label;
5897
5898               /* Get the address of our target into %r22.  */
5899               output_asm_insn ("addil LR%%%0-$global$,%%r27", xoperands);
5900               output_asm_insn ("ldw RR%%%0-$global$(%%r1),%%r22", xoperands);
5901
5902               /* Get the high part of the  address of $dyncall into %r2, then
5903                  add in the low part in the branch instruction.  */
5904               output_asm_insn ("ldil L%%$$dyncall,%%r2", xoperands);
5905               output_asm_insn ("{ble|be,l}  R%%$$dyncall(%%sr4,%%r2)",
5906                                xoperands);
5907
5908               if (sibcall)
5909                 {
5910                   /* This call never returns, so we do not need to fix the
5911                      return pointer.  */
5912                   output_asm_insn ("nop", xoperands);
5913                 }
5914               else
5915                 {
5916                   /* Copy the return address into %r2 also.  */
5917                   output_asm_insn ("copy %%r31,%%r2", xoperands);
5918                 }
5919             }
5920         }
5921
5922       /* If we had a jump in the call's delay slot, output it now.  */
5923       if (dbr_sequence_length () != 0
5924           && !delay_insn_deleted)
5925         {
5926           xoperands[0] = XEXP (PATTERN (NEXT_INSN (insn)), 1);
5927           output_asm_insn ("b,n %0", xoperands);
5928
5929           /* Now delete the delay insn.  */
5930           PUT_CODE (NEXT_INSN (insn), NOTE);
5931           NOTE_LINE_NUMBER (NEXT_INSN (insn)) = NOTE_INSN_DELETED;
5932           NOTE_SOURCE_FILE (NEXT_INSN (insn)) = 0;
5933         }
5934       return "";
5935     }
5936
5937   /* This call has an unconditional jump in its delay slot and the
5938      call is known to reach its target or the beginning of the current
5939      subspace.  */
5940
5941   /* Use the containing sequence insn's address.  */
5942   seq_insn = NEXT_INSN (PREV_INSN (XVECEXP (final_sequence, 0, 0)));
5943
5944   distance = INSN_ADDRESSES (INSN_UID (JUMP_LABEL (NEXT_INSN (insn))))
5945                - INSN_ADDRESSES (INSN_UID (seq_insn)) - 8;
5946
5947   /* If the branch was too far away, emit a normal call followed
5948      by a nop, followed by the unconditional branch.
5949
5950      If the branch is close, then adjust %r2 from within the
5951      call's delay slot.  */
5952
5953   xoperands[0] = call_dest;
5954   xoperands[1] = XEXP (PATTERN (NEXT_INSN (insn)), 1);
5955   if (! VAL_14_BITS_P (distance))
5956     output_asm_insn ("{bl|b,l} %0,%%r2\n\tnop\n\tb,n %1", xoperands);
5957   else
5958     {
5959       xoperands[3] = gen_label_rtx ();
5960       output_asm_insn ("\n\t{bl|b,l} %0,%%r2\n\tldo %1-%3(%%r2),%%r2",
5961                        xoperands);
5962       ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
5963                                  CODE_LABEL_NUMBER (xoperands[3]));
5964     }
5965
5966   /* Delete the jump.  */
5967   PUT_CODE (NEXT_INSN (insn), NOTE);
5968   NOTE_LINE_NUMBER (NEXT_INSN (insn)) = NOTE_INSN_DELETED;
5969   NOTE_SOURCE_FILE (NEXT_INSN (insn)) = 0;
5970   return "";
5971 }
5972
5973 /* In HPUX 8.0's shared library scheme, special relocations are needed
5974    for function labels if they might be passed to a function
5975    in a shared library (because shared libraries don't live in code
5976    space), and special magic is needed to construct their address.  */
5977
5978 void
5979 hppa_encode_label (sym)
5980      rtx sym;
5981 {
5982   const char *str = XSTR (sym, 0);
5983   int len = strlen (str) + 1;
5984   char *newstr, *p;
5985
5986   p = newstr = alloca (len + 1);
5987   if (str[0] == '*')
5988     {
5989       str++;
5990       len--;
5991     }
5992   *p++ = '@';
5993   strcpy (p, str);
5994
5995   XSTR (sym,0) = ggc_alloc_string (newstr, len);
5996 }
5997
5998 int
5999 function_label_operand (op, mode)
6000      rtx op;
6001      enum machine_mode mode ATTRIBUTE_UNUSED;
6002 {
6003   return GET_CODE (op) == SYMBOL_REF && FUNCTION_NAME_P (XSTR (op, 0));
6004 }
6005
6006 /* Returns 1 if OP is a function label involved in a simple addition
6007    with a constant.  Used to keep certain patterns from matching
6008    during instruction combination.  */
6009 int
6010 is_function_label_plus_const (op)
6011      rtx op;
6012 {
6013   /* Strip off any CONST.  */
6014   if (GET_CODE (op) == CONST)
6015     op = XEXP (op, 0);
6016
6017   return (GET_CODE (op) == PLUS
6018           && function_label_operand (XEXP (op, 0), Pmode)
6019           && GET_CODE (XEXP (op, 1)) == CONST_INT);
6020 }
6021
6022 /* Returns 1 if the 6 operands specified in OPERANDS are suitable for
6023    use in fmpyadd instructions.  */
6024 int
6025 fmpyaddoperands (operands)
6026      rtx *operands;
6027 {
6028   enum machine_mode mode = GET_MODE (operands[0]);
6029
6030   /* Must be a floating point mode.  */
6031   if (mode != SFmode && mode != DFmode)
6032     return 0;
6033
6034   /* All modes must be the same.  */
6035   if (! (mode == GET_MODE (operands[1])
6036          && mode == GET_MODE (operands[2])
6037          && mode == GET_MODE (operands[3])
6038          && mode == GET_MODE (operands[4])
6039          && mode == GET_MODE (operands[5])))
6040     return 0;
6041
6042   /* All operands must be registers.  */
6043   if (! (GET_CODE (operands[1]) == REG
6044          && GET_CODE (operands[2]) == REG
6045          && GET_CODE (operands[3]) == REG
6046          && GET_CODE (operands[4]) == REG
6047          && GET_CODE (operands[5]) == REG))
6048     return 0;
6049
6050   /* Only 2 real operands to the addition.  One of the input operands must
6051      be the same as the output operand.  */
6052   if (! rtx_equal_p (operands[3], operands[4])
6053       && ! rtx_equal_p (operands[3], operands[5]))
6054     return 0;
6055
6056   /* Inout operand of add can not conflict with any operands from multiply.  */
6057   if (rtx_equal_p (operands[3], operands[0])
6058      || rtx_equal_p (operands[3], operands[1])
6059      || rtx_equal_p (operands[3], operands[2]))
6060     return 0;
6061
6062   /* multiply can not feed into addition operands.  */
6063   if (rtx_equal_p (operands[4], operands[0])
6064       || rtx_equal_p (operands[5], operands[0]))
6065     return 0;
6066
6067   /* SFmode limits the registers to the upper 32 of the 32bit FP regs.  */
6068   if (mode == SFmode
6069       && (REGNO_REG_CLASS (REGNO (operands[0])) != FPUPPER_REGS
6070           || REGNO_REG_CLASS (REGNO (operands[1])) != FPUPPER_REGS
6071           || REGNO_REG_CLASS (REGNO (operands[2])) != FPUPPER_REGS
6072           || REGNO_REG_CLASS (REGNO (operands[3])) != FPUPPER_REGS
6073           || REGNO_REG_CLASS (REGNO (operands[4])) != FPUPPER_REGS
6074           || REGNO_REG_CLASS (REGNO (operands[5])) != FPUPPER_REGS))
6075     return 0;
6076
6077   /* Passed.  Operands are suitable for fmpyadd.  */
6078   return 1;
6079 }
6080
6081 /* Returns 1 if the 6 operands specified in OPERANDS are suitable for
6082    use in fmpysub instructions.  */
6083 int
6084 fmpysuboperands (operands)
6085      rtx *operands;
6086 {
6087   enum machine_mode mode = GET_MODE (operands[0]);
6088
6089   /* Must be a floating point mode.  */
6090   if (mode != SFmode && mode != DFmode)
6091     return 0;
6092
6093   /* All modes must be the same.  */
6094   if (! (mode == GET_MODE (operands[1])
6095          && mode == GET_MODE (operands[2])
6096          && mode == GET_MODE (operands[3])
6097          && mode == GET_MODE (operands[4])
6098          && mode == GET_MODE (operands[5])))
6099     return 0;
6100
6101   /* All operands must be registers.  */
6102   if (! (GET_CODE (operands[1]) == REG
6103          && GET_CODE (operands[2]) == REG
6104          && GET_CODE (operands[3]) == REG
6105          && GET_CODE (operands[4]) == REG
6106          && GET_CODE (operands[5]) == REG))
6107     return 0;
6108
6109   /* Only 2 real operands to the subtraction.  Subtraction is not a commutative
6110      operation, so operands[4] must be the same as operand[3].  */
6111   if (! rtx_equal_p (operands[3], operands[4]))
6112     return 0;
6113
6114   /* multiply can not feed into subtraction.  */
6115   if (rtx_equal_p (operands[5], operands[0]))
6116     return 0;
6117
6118   /* Inout operand of sub can not conflict with any operands from multiply.  */
6119   if (rtx_equal_p (operands[3], operands[0])
6120      || rtx_equal_p (operands[3], operands[1])
6121      || rtx_equal_p (operands[3], operands[2]))
6122     return 0;
6123
6124   /* SFmode limits the registers to the upper 32 of the 32bit FP regs.  */
6125   if (mode == SFmode
6126       && (REGNO_REG_CLASS (REGNO (operands[0])) != FPUPPER_REGS
6127           || REGNO_REG_CLASS (REGNO (operands[1])) != FPUPPER_REGS
6128           || REGNO_REG_CLASS (REGNO (operands[2])) != FPUPPER_REGS
6129           || REGNO_REG_CLASS (REGNO (operands[3])) != FPUPPER_REGS
6130           || REGNO_REG_CLASS (REGNO (operands[4])) != FPUPPER_REGS
6131           || REGNO_REG_CLASS (REGNO (operands[5])) != FPUPPER_REGS))
6132     return 0;
6133
6134   /* Passed.  Operands are suitable for fmpysub.  */
6135   return 1;
6136 }
6137
6138 int
6139 plus_xor_ior_operator (op, mode)
6140      rtx op;
6141      enum machine_mode mode ATTRIBUTE_UNUSED;
6142 {
6143   return (GET_CODE (op) == PLUS || GET_CODE (op) == XOR
6144           || GET_CODE (op) == IOR);
6145 }
6146
6147 /* Return 1 if the given constant is 2, 4, or 8.  These are the valid
6148    constants for shadd instructions.  */
6149 static int
6150 shadd_constant_p (val)
6151      int val;
6152 {
6153   if (val == 2 || val == 4 || val == 8)
6154     return 1;
6155   else
6156     return 0;
6157 }
6158
6159 /* Return 1 if OP is a CONST_INT with the value 2, 4, or 8.  These are
6160    the valid constant for shadd instructions.  */
6161 int
6162 shadd_operand (op, mode)
6163      rtx op;
6164      enum machine_mode mode ATTRIBUTE_UNUSED;
6165 {
6166   return (GET_CODE (op) == CONST_INT && shadd_constant_p (INTVAL (op)));
6167 }
6168
6169 /* Return 1 if OP is valid as a base register in a reg + reg address.  */
6170
6171 int
6172 basereg_operand (op, mode)
6173      rtx op;
6174      enum machine_mode mode;
6175 {
6176   /* cse will create some unscaled indexed addresses, however; it
6177      generally isn't a win on the PA, so avoid creating unscaled
6178      indexed addresses until after cse is finished.  */
6179   if (!cse_not_expected)
6180     return 0;
6181
6182   /* Allow any register when TARGET_NO_SPACE_REGS is in effect since
6183      we don't have to worry about the braindamaged implicit space
6184      register selection from the basereg.  */
6185   if (TARGET_NO_SPACE_REGS)
6186     return (GET_CODE (op) == REG);
6187
6188   /* While it's always safe to index off the frame pointer, it's not
6189      always profitable, particularly when the frame pointer is being
6190      eliminated.  */
6191   if (! flag_omit_frame_pointer && op == frame_pointer_rtx)
6192     return 1;
6193
6194   return (GET_CODE (op) == REG
6195           && REG_POINTER (op)
6196           && register_operand (op, mode));
6197 }
6198
6199 /* Return 1 if this operand is anything other than a hard register.  */
6200
6201 int
6202 non_hard_reg_operand (op, mode)
6203      rtx op;
6204      enum machine_mode mode ATTRIBUTE_UNUSED;
6205 {
6206   return ! (GET_CODE (op) == REG && REGNO (op) < FIRST_PSEUDO_REGISTER);
6207 }
6208
6209 /* Return 1 if INSN branches forward.  Should be using insn_addresses
6210    to avoid walking through all the insns... */
6211 static int
6212 forward_branch_p (insn)
6213      rtx insn;
6214 {
6215   rtx label = JUMP_LABEL (insn);
6216
6217   while (insn)
6218     {
6219       if (insn == label)
6220         break;
6221       else
6222         insn = NEXT_INSN (insn);
6223     }
6224
6225   return (insn == label);
6226 }
6227
6228 /* Return 1 if OP is an equality comparison, else return 0.  */
6229 int
6230 eq_neq_comparison_operator (op, mode)
6231      rtx op;
6232      enum machine_mode mode ATTRIBUTE_UNUSED;
6233 {
6234   return (GET_CODE (op) == EQ || GET_CODE (op) == NE);
6235 }
6236
6237 /* Return 1 if OP is an operator suitable for use in a movb instruction.  */
6238 int
6239 movb_comparison_operator (op, mode)
6240      rtx op;
6241      enum machine_mode mode ATTRIBUTE_UNUSED;
6242 {
6243   return (GET_CODE (op) == EQ || GET_CODE (op) == NE
6244           || GET_CODE (op) == LT || GET_CODE (op) == GE);
6245 }
6246
6247 /* Return 1 if INSN is in the delay slot of a call instruction.  */
6248 int
6249 jump_in_call_delay (insn)
6250      rtx insn;
6251 {
6252
6253   if (GET_CODE (insn) != JUMP_INSN)
6254     return 0;
6255
6256   if (PREV_INSN (insn)
6257       && PREV_INSN (PREV_INSN (insn))
6258       && GET_CODE (next_active_insn (PREV_INSN (PREV_INSN (insn)))) == INSN)
6259     {
6260       rtx test_insn = next_active_insn (PREV_INSN (PREV_INSN (insn)));
6261
6262       return (GET_CODE (PATTERN (test_insn)) == SEQUENCE
6263               && XVECEXP (PATTERN (test_insn), 0, 1) == insn);
6264
6265     }
6266   else
6267     return 0;
6268 }
6269
6270 /* Output an unconditional move and branch insn.  */
6271
6272 const char *
6273 output_parallel_movb (operands, length)
6274      rtx *operands;
6275      int length;
6276 {
6277   /* These are the cases in which we win.  */
6278   if (length == 4)
6279     return "mov%I1b,tr %1,%0,%2";
6280
6281   /* None of these cases wins, but they don't lose either.  */
6282   if (dbr_sequence_length () == 0)
6283     {
6284       /* Nothing in the delay slot, fake it by putting the combined
6285          insn (the copy or add) in the delay slot of a bl.  */
6286       if (GET_CODE (operands[1]) == CONST_INT)
6287         return "b %2\n\tldi %1,%0";
6288       else
6289         return "b %2\n\tcopy %1,%0";
6290     }
6291   else
6292     {
6293       /* Something in the delay slot, but we've got a long branch.  */
6294       if (GET_CODE (operands[1]) == CONST_INT)
6295         return "ldi %1,%0\n\tb %2";
6296       else
6297         return "copy %1,%0\n\tb %2";
6298     }
6299 }
6300
6301 /* Output an unconditional add and branch insn.  */
6302
6303 const char *
6304 output_parallel_addb (operands, length)
6305      rtx *operands;
6306      int length;
6307 {
6308   /* To make life easy we want operand0 to be the shared input/output
6309      operand and operand1 to be the readonly operand.  */
6310   if (operands[0] == operands[1])
6311     operands[1] = operands[2];
6312
6313   /* These are the cases in which we win.  */
6314   if (length == 4)
6315     return "add%I1b,tr %1,%0,%3";
6316
6317   /* None of these cases win, but they don't lose either.  */
6318   if (dbr_sequence_length () == 0)
6319     {
6320       /* Nothing in the delay slot, fake it by putting the combined
6321          insn (the copy or add) in the delay slot of a bl.  */
6322       return "b %3\n\tadd%I1 %1,%0,%0";
6323     }
6324   else
6325     {
6326       /* Something in the delay slot, but we've got a long branch.  */
6327       return "add%I1 %1,%0,%0\n\tb %3";
6328     }
6329 }
6330
6331 /* Return nonzero if INSN (a jump insn) immediately follows a call to
6332    a named function.  This is used to discourage creating parallel movb/addb
6333    insns since a jump which immediately follows a call can execute in the
6334    delay slot of the call.
6335
6336    It is also used to avoid filling the delay slot of a jump which
6337    immediately follows a call since the jump can usually be eliminated
6338    completely by modifying RP in the delay slot of the call.  */
6339    
6340 int
6341 following_call (insn)
6342      rtx insn;
6343 {
6344   /* We do not parallel movb,addb or place jumps into call delay slots when
6345      optimizing for the PA8000.  */
6346   if (pa_cpu != PROCESSOR_8000)
6347     return 0;
6348
6349   /* Find the previous real insn, skipping NOTEs.  */
6350   insn = PREV_INSN (insn);
6351   while (insn && GET_CODE (insn) == NOTE)
6352     insn = PREV_INSN (insn);
6353
6354   /* Check for CALL_INSNs and millicode calls.  */
6355   if (insn
6356       && ((GET_CODE (insn) == CALL_INSN
6357            && get_attr_type (insn) != TYPE_DYNCALL)
6358           || (GET_CODE (insn) == INSN
6359               && GET_CODE (PATTERN (insn)) != SEQUENCE
6360               && GET_CODE (PATTERN (insn)) != USE
6361               && GET_CODE (PATTERN (insn)) != CLOBBER
6362               && get_attr_type (insn) == TYPE_MILLI)))
6363     return 1;
6364
6365   return 0;
6366 }
6367
6368 /* We use this hook to perform a PA specific optimization which is difficult
6369    to do in earlier passes.
6370
6371    We want the delay slots of branches within jump tables to be filled.
6372    None of the compiler passes at the moment even has the notion that a
6373    PA jump table doesn't contain addresses, but instead contains actual
6374    instructions!
6375
6376    Because we actually jump into the table, the addresses of each entry
6377    must stay constant in relation to the beginning of the table (which
6378    itself must stay constant relative to the instruction to jump into
6379    it).  I don't believe we can guarantee earlier passes of the compiler
6380    will adhere to those rules.
6381
6382    So, late in the compilation process we find all the jump tables, and
6383    expand them into real code -- eg each entry in the jump table vector
6384    will get an appropriate label followed by a jump to the final target.
6385
6386    Reorg and the final jump pass can then optimize these branches and
6387    fill their delay slots.  We end up with smaller, more efficient code.
6388
6389    The jump instructions within the table are special; we must be able 
6390    to identify them during assembly output (if the jumps don't get filled
6391    we need to emit a nop rather than nullifying the delay slot)).  We
6392    identify jumps in switch tables by marking the SET with DImode. 
6393
6394    We also surround the jump table itself with BEGIN_BRTAB and END_BRTAB
6395    insns.  This serves two purposes, first it prevents jump.c from
6396    noticing that the last N entries in the table jump to the instruction
6397    immediately after the table and deleting the jumps.  Second, those
6398    insns mark where we should emit .begin_brtab and .end_brtab directives
6399    when using GAS (allows for better link time optimizations).  */
6400
6401 void
6402 pa_reorg (insns)
6403      rtx insns;
6404 {
6405   rtx insn;
6406
6407   remove_useless_addtr_insns (insns, 1);
6408
6409   if (pa_cpu < PROCESSOR_8000)
6410     pa_combine_instructions (get_insns ());
6411
6412
6413   /* This is fairly cheap, so always run it if optimizing.  */
6414   if (optimize > 0 && !TARGET_BIG_SWITCH)
6415     {
6416       /* Find and explode all ADDR_VEC or ADDR_DIFF_VEC insns.  */
6417       insns = get_insns ();
6418       for (insn = insns; insn; insn = NEXT_INSN (insn))
6419         {
6420           rtx pattern, tmp, location;
6421           unsigned int length, i;
6422
6423           /* Find an ADDR_VEC or ADDR_DIFF_VEC insn to explode.  */
6424           if (GET_CODE (insn) != JUMP_INSN
6425               || (GET_CODE (PATTERN (insn)) != ADDR_VEC
6426                   && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC))
6427             continue;
6428
6429           /* Emit marker for the beginning of the branch table.  */
6430           emit_insn_before (gen_begin_brtab (), insn);
6431
6432           pattern = PATTERN (insn);
6433           location = PREV_INSN (insn);
6434           length = XVECLEN (pattern, GET_CODE (pattern) == ADDR_DIFF_VEC);
6435
6436           for (i = 0; i < length; i++)
6437             {
6438               /* Emit a label before each jump to keep jump.c from
6439                  removing this code.  */
6440               tmp = gen_label_rtx ();
6441               LABEL_NUSES (tmp) = 1;
6442               emit_label_after (tmp, location);
6443               location = NEXT_INSN (location);
6444
6445               if (GET_CODE (pattern) == ADDR_VEC)
6446                 {
6447                   /* Emit the jump itself.  */
6448                   tmp = gen_jump (XEXP (XVECEXP (pattern, 0, i), 0));
6449                   tmp = emit_jump_insn_after (tmp, location);
6450                   JUMP_LABEL (tmp) = XEXP (XVECEXP (pattern, 0, i), 0);
6451                   /* It is easy to rely on the branch table markers
6452                      during assembly output to trigger the correct code
6453                      for a switch table jump with an unfilled delay slot,
6454
6455                      However, that requires state and assumes that we look
6456                      at insns in order.
6457
6458                      We can't make such assumptions when computing the length
6459                      of instructions.  Ugh.  We could walk the insn chain to
6460                      determine if this instruction is in a branch table, but
6461                      that can get rather expensive, particularly during the
6462                      branch shortening phase of the compiler.
6463
6464                      So instead we mark this jump as being special.  This is
6465                      far from ideal and knows that no code after this will
6466                      muck around with the mode of the JUMP_INSN itself.  */
6467                   PUT_MODE (tmp, SImode);
6468                   LABEL_NUSES (JUMP_LABEL (tmp))++;
6469                   location = NEXT_INSN (location);
6470                 }
6471               else
6472                 {
6473                   /* Emit the jump itself.  */
6474                   tmp = gen_jump (XEXP (XVECEXP (pattern, 1, i), 0));
6475                   tmp = emit_jump_insn_after (tmp, location);
6476                   JUMP_LABEL (tmp) = XEXP (XVECEXP (pattern, 1, i), 0);
6477                   /* It is easy to rely on the branch table markers
6478                      during assembly output to trigger the correct code
6479                      for a switch table jump with an unfilled delay slot,
6480
6481                      However, that requires state and assumes that we look
6482                      at insns in order.
6483
6484                      We can't make such assumptions when computing the length
6485                      of instructions.  Ugh.  We could walk the insn chain to
6486                      determine if this instruction is in a branch table, but
6487                      that can get rather expensive, particularly during the
6488                      branch shortening phase of the compiler.
6489
6490                      So instead we mark this jump as being special.  This is
6491                      far from ideal and knows that no code after this will
6492                      muck around with the mode of the JUMP_INSN itself.  */
6493                   PUT_MODE (tmp, SImode);
6494                   LABEL_NUSES (JUMP_LABEL (tmp))++;
6495                   location = NEXT_INSN (location);
6496                 }
6497
6498               /* Emit a BARRIER after the jump.  */
6499               emit_barrier_after (location);
6500               location = NEXT_INSN (location);
6501             }
6502
6503           /* Emit marker for the end of the branch table.  */
6504           emit_insn_before (gen_end_brtab (), location);
6505           location = NEXT_INSN (location);
6506           emit_barrier_after (location);
6507
6508           /* Delete the ADDR_VEC or ADDR_DIFF_VEC.  */
6509           delete_insn (insn);
6510         }
6511     }
6512   else
6513     {
6514       /* Sill need an end_brtab insn.  */
6515       insns = get_insns ();
6516       for (insn = insns; insn; insn = NEXT_INSN (insn))
6517         {
6518           /* Find an ADDR_VEC insn.  */
6519           if (GET_CODE (insn) != JUMP_INSN
6520               || (GET_CODE (PATTERN (insn)) != ADDR_VEC
6521                   && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC))
6522             continue;
6523
6524           /* Now generate markers for the beginning and end of the
6525              branch table.  */
6526           emit_insn_before (gen_begin_brtab (), insn);
6527           emit_insn_after (gen_end_brtab (), insn);
6528         }
6529     }
6530 }
6531
6532 /* The PA has a number of odd instructions which can perform multiple
6533    tasks at once.  On first generation PA machines (PA1.0 and PA1.1)
6534    it may be profitable to combine two instructions into one instruction
6535    with two outputs.  It's not profitable PA2.0 machines because the
6536    two outputs would take two slots in the reorder buffers.
6537
6538    This routine finds instructions which can be combined and combines
6539    them.  We only support some of the potential combinations, and we
6540    only try common ways to find suitable instructions.
6541
6542       * addb can add two registers or a register and a small integer
6543       and jump to a nearby (+-8k) location.  Normally the jump to the
6544       nearby location is conditional on the result of the add, but by
6545       using the "true" condition we can make the jump unconditional.
6546       Thus addb can perform two independent operations in one insn.
6547
6548       * movb is similar to addb in that it can perform a reg->reg
6549       or small immediate->reg copy and jump to a nearby (+-8k location).
6550
6551       * fmpyadd and fmpysub can perform a FP multiply and either an
6552       FP add or FP sub if the operands of the multiply and add/sub are
6553       independent (there are other minor restrictions).  Note both
6554       the fmpy and fadd/fsub can in theory move to better spots according
6555       to data dependencies, but for now we require the fmpy stay at a
6556       fixed location.
6557
6558       * Many of the memory operations can perform pre & post updates
6559       of index registers.  GCC's pre/post increment/decrement addressing
6560       is far too simple to take advantage of all the possibilities.  This
6561       pass may not be suitable since those insns may not be independent.
6562
6563       * comclr can compare two ints or an int and a register, nullify
6564       the following instruction and zero some other register.  This
6565       is more difficult to use as it's harder to find an insn which
6566       will generate a comclr than finding something like an unconditional
6567       branch.  (conditional moves & long branches create comclr insns).
6568
6569       * Most arithmetic operations can conditionally skip the next
6570       instruction.  They can be viewed as "perform this operation
6571       and conditionally jump to this nearby location" (where nearby
6572       is an insns away).  These are difficult to use due to the
6573       branch length restrictions.  */
6574
6575 static void
6576 pa_combine_instructions (insns)
6577      rtx insns ATTRIBUTE_UNUSED;
6578 {
6579   rtx anchor, new;
6580
6581   /* This can get expensive since the basic algorithm is on the
6582      order of O(n^2) (or worse).  Only do it for -O2 or higher
6583      levels of optimization.  */
6584   if (optimize < 2)
6585     return;
6586
6587   /* Walk down the list of insns looking for "anchor" insns which
6588      may be combined with "floating" insns.  As the name implies,
6589      "anchor" instructions don't move, while "floating" insns may
6590      move around.  */
6591   new = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, NULL_RTX, NULL_RTX));
6592   new = make_insn_raw (new);
6593
6594   for (anchor = get_insns (); anchor; anchor = NEXT_INSN (anchor))
6595     {
6596       enum attr_pa_combine_type anchor_attr;
6597       enum attr_pa_combine_type floater_attr;
6598
6599       /* We only care about INSNs, JUMP_INSNs, and CALL_INSNs.
6600          Also ignore any special USE insns.  */
6601       if ((GET_CODE (anchor) != INSN
6602           && GET_CODE (anchor) != JUMP_INSN
6603           && GET_CODE (anchor) != CALL_INSN)
6604           || GET_CODE (PATTERN (anchor)) == USE
6605           || GET_CODE (PATTERN (anchor)) == CLOBBER
6606           || GET_CODE (PATTERN (anchor)) == ADDR_VEC
6607           || GET_CODE (PATTERN (anchor)) == ADDR_DIFF_VEC)
6608         continue;
6609
6610       anchor_attr = get_attr_pa_combine_type (anchor);
6611       /* See if anchor is an insn suitable for combination.  */
6612       if (anchor_attr == PA_COMBINE_TYPE_FMPY
6613           || anchor_attr == PA_COMBINE_TYPE_FADDSUB
6614           || (anchor_attr == PA_COMBINE_TYPE_UNCOND_BRANCH
6615               && ! forward_branch_p (anchor)))
6616         {
6617           rtx floater;
6618
6619           for (floater = PREV_INSN (anchor);
6620                floater;
6621                floater = PREV_INSN (floater))
6622             {
6623               if (GET_CODE (floater) == NOTE
6624                   || (GET_CODE (floater) == INSN
6625                       && (GET_CODE (PATTERN (floater)) == USE
6626                           || GET_CODE (PATTERN (floater)) == CLOBBER)))
6627                 continue;
6628
6629               /* Anything except a regular INSN will stop our search.  */
6630               if (GET_CODE (floater) != INSN
6631                   || GET_CODE (PATTERN (floater)) == ADDR_VEC
6632                   || GET_CODE (PATTERN (floater)) == ADDR_DIFF_VEC)
6633                 {
6634                   floater = NULL_RTX;
6635                   break;
6636                 }
6637
6638               /* See if FLOATER is suitable for combination with the
6639                  anchor.  */
6640               floater_attr = get_attr_pa_combine_type (floater);
6641               if ((anchor_attr == PA_COMBINE_TYPE_FMPY
6642                    && floater_attr == PA_COMBINE_TYPE_FADDSUB)
6643                   || (anchor_attr == PA_COMBINE_TYPE_FADDSUB
6644                       && floater_attr == PA_COMBINE_TYPE_FMPY))
6645                 {
6646                   /* If ANCHOR and FLOATER can be combined, then we're
6647                      done with this pass.  */
6648                   if (pa_can_combine_p (new, anchor, floater, 0,
6649                                         SET_DEST (PATTERN (floater)),
6650                                         XEXP (SET_SRC (PATTERN (floater)), 0),
6651                                         XEXP (SET_SRC (PATTERN (floater)), 1)))
6652                     break;
6653                 }
6654
6655               else if (anchor_attr == PA_COMBINE_TYPE_UNCOND_BRANCH
6656                        && floater_attr == PA_COMBINE_TYPE_ADDMOVE)
6657                 {
6658                   if (GET_CODE (SET_SRC (PATTERN (floater))) == PLUS)
6659                     {
6660                       if (pa_can_combine_p (new, anchor, floater, 0,
6661                                             SET_DEST (PATTERN (floater)),
6662                                         XEXP (SET_SRC (PATTERN (floater)), 0),
6663                                         XEXP (SET_SRC (PATTERN (floater)), 1)))
6664                         break;
6665                     }
6666                   else
6667                     {
6668                       if (pa_can_combine_p (new, anchor, floater, 0,
6669                                             SET_DEST (PATTERN (floater)),
6670                                             SET_SRC (PATTERN (floater)),
6671                                             SET_SRC (PATTERN (floater))))
6672                         break;
6673                     }
6674                 }
6675             }
6676
6677           /* If we didn't find anything on the backwards scan try forwards.  */
6678           if (!floater
6679               && (anchor_attr == PA_COMBINE_TYPE_FMPY
6680                   || anchor_attr == PA_COMBINE_TYPE_FADDSUB))
6681             {
6682               for (floater = anchor; floater; floater = NEXT_INSN (floater))
6683                 {
6684                   if (GET_CODE (floater) == NOTE
6685                       || (GET_CODE (floater) == INSN
6686                           && (GET_CODE (PATTERN (floater)) == USE
6687                               || GET_CODE (PATTERN (floater)) == CLOBBER)))
6688                         
6689                     continue;
6690
6691                   /* Anything except a regular INSN will stop our search.  */
6692                   if (GET_CODE (floater) != INSN
6693                       || GET_CODE (PATTERN (floater)) == ADDR_VEC
6694                       || GET_CODE (PATTERN (floater)) == ADDR_DIFF_VEC)
6695                     {
6696                       floater = NULL_RTX;
6697                       break;
6698                     }
6699
6700                   /* See if FLOATER is suitable for combination with the
6701                      anchor.  */
6702                   floater_attr = get_attr_pa_combine_type (floater);
6703                   if ((anchor_attr == PA_COMBINE_TYPE_FMPY
6704                        && floater_attr == PA_COMBINE_TYPE_FADDSUB)
6705                       || (anchor_attr == PA_COMBINE_TYPE_FADDSUB
6706                           && floater_attr == PA_COMBINE_TYPE_FMPY))
6707                     {
6708                       /* If ANCHOR and FLOATER can be combined, then we're
6709                          done with this pass.  */
6710                       if (pa_can_combine_p (new, anchor, floater, 1,
6711                                             SET_DEST (PATTERN (floater)),
6712                                             XEXP (SET_SRC (PATTERN(floater)),0),
6713                                             XEXP(SET_SRC(PATTERN(floater)),1)))
6714                         break;
6715                     }
6716                 }
6717             }
6718
6719           /* FLOATER will be nonzero if we found a suitable floating
6720              insn for combination with ANCHOR.  */
6721           if (floater
6722               && (anchor_attr == PA_COMBINE_TYPE_FADDSUB
6723                   || anchor_attr == PA_COMBINE_TYPE_FMPY))
6724             {
6725               /* Emit the new instruction and delete the old anchor.  */
6726               emit_insn_before (gen_rtx_PARALLEL
6727                                 (VOIDmode,
6728                                  gen_rtvec (2, PATTERN (anchor),
6729                                             PATTERN (floater))),
6730                                 anchor);
6731
6732               PUT_CODE (anchor, NOTE);
6733               NOTE_LINE_NUMBER (anchor) = NOTE_INSN_DELETED;
6734               NOTE_SOURCE_FILE (anchor) = 0;
6735
6736               /* Emit a special USE insn for FLOATER, then delete
6737                  the floating insn.  */
6738               emit_insn_before (gen_rtx_USE (VOIDmode, floater), floater);
6739               delete_insn (floater);
6740
6741               continue;
6742             }
6743           else if (floater
6744                    && anchor_attr == PA_COMBINE_TYPE_UNCOND_BRANCH)
6745             {
6746               rtx temp;
6747               /* Emit the new_jump instruction and delete the old anchor.  */
6748               temp
6749                 = emit_jump_insn_before (gen_rtx_PARALLEL
6750                                          (VOIDmode,
6751                                           gen_rtvec (2, PATTERN (anchor),
6752                                                      PATTERN (floater))),
6753                                          anchor);
6754
6755               JUMP_LABEL (temp) = JUMP_LABEL (anchor);
6756               PUT_CODE (anchor, NOTE);
6757               NOTE_LINE_NUMBER (anchor) = NOTE_INSN_DELETED;
6758               NOTE_SOURCE_FILE (anchor) = 0;
6759
6760               /* Emit a special USE insn for FLOATER, then delete
6761                  the floating insn.  */
6762               emit_insn_before (gen_rtx_USE (VOIDmode, floater), floater);
6763               delete_insn (floater);
6764               continue;
6765             }
6766         }
6767     }
6768 }
6769
6770 static int
6771 pa_can_combine_p (new, anchor, floater, reversed, dest, src1, src2)
6772      rtx new, anchor, floater;
6773      int reversed;
6774      rtx dest, src1, src2;
6775 {
6776   int insn_code_number;
6777   rtx start, end;
6778
6779   /* Create a PARALLEL with the patterns of ANCHOR and
6780      FLOATER, try to recognize it, then test constraints
6781      for the resulting pattern.
6782
6783      If the pattern doesn't match or the constraints
6784      aren't met keep searching for a suitable floater
6785      insn.  */
6786   XVECEXP (PATTERN (new), 0, 0) = PATTERN (anchor);
6787   XVECEXP (PATTERN (new), 0, 1) = PATTERN (floater);
6788   INSN_CODE (new) = -1;
6789   insn_code_number = recog_memoized (new);
6790   if (insn_code_number < 0
6791       || !constrain_operands (1))
6792     return 0;
6793
6794   if (reversed)
6795     {
6796       start = anchor;
6797       end = floater;
6798     }
6799   else
6800     {
6801       start = floater;
6802       end = anchor;
6803     }
6804
6805   /* There's up to three operands to consider.  One
6806      output and two inputs.
6807
6808      The output must not be used between FLOATER & ANCHOR
6809      exclusive.  The inputs must not be set between
6810      FLOATER and ANCHOR exclusive.  */
6811
6812   if (reg_used_between_p (dest, start, end))
6813     return 0;
6814
6815   if (reg_set_between_p (src1, start, end))
6816     return 0;
6817
6818   if (reg_set_between_p (src2, start, end))
6819     return 0;
6820
6821   /* If we get here, then everything is good.  */
6822   return 1;
6823 }
6824
6825 /* Return nonzero if references for INSN are delayed.
6826
6827    Millicode insns are actually function calls with some special
6828    constraints on arguments and register usage.
6829
6830    Millicode calls always expect their arguments in the integer argument
6831    registers, and always return their result in %r29 (ret1).  They
6832    are expected to clobber their arguments, %r1, %r29, and %r31 and
6833    nothing else.
6834
6835    By considering this effects delayed reorg reorg can put insns
6836    which set the argument registers into the delay slot of the millicode
6837    call -- thus they act more like traditional CALL_INSNs.
6838
6839    get_attr_type will try to recognize the given insn, so make sure to
6840    filter out things it will not accept -- SEQUENCE, USE and CLOBBER insns
6841    in particular.  */
6842 int
6843 insn_refs_are_delayed (insn)
6844      rtx insn;
6845 {
6846   return ((GET_CODE (insn) == INSN 
6847            && GET_CODE (PATTERN (insn)) != SEQUENCE
6848            && GET_CODE (PATTERN (insn)) != USE
6849            && GET_CODE (PATTERN (insn)) != CLOBBER
6850            && get_attr_type (insn) == TYPE_MILLI));
6851 }
6852
6853 /* Return the location of a parameter that is passed in a register or NULL
6854    if the parameter has any component that is passed in memory.
6855
6856    This is new code and will be pushed to into the net sources after
6857    further testing. 
6858
6859    ??? We might want to restructure this so that it looks more like other
6860    ports.  */
6861 rtx
6862 function_arg (cum, mode, type, named, incoming)
6863      CUMULATIVE_ARGS *cum;
6864      enum machine_mode mode;
6865      tree type;
6866      int named ATTRIBUTE_UNUSED;
6867      int incoming;
6868 {
6869   int max_arg_words = (TARGET_64BIT ? 8 : 4);
6870   int fpr_reg_base;
6871   int gpr_reg_base;
6872   rtx retval;
6873
6874   if (! TARGET_64BIT)
6875     {
6876       /* If this arg would be passed partially or totally on the stack, then
6877          this routine should return zero.  FUNCTION_ARG_PARTIAL_NREGS will
6878          handle arguments which are split between regs and stack slots if
6879          the ABI mandates split arguments.  */
6880       if (cum->words + FUNCTION_ARG_SIZE (mode, type) > max_arg_words
6881           || mode == VOIDmode)
6882         return NULL_RTX;
6883     }
6884   else
6885     {
6886       int offset = 0;
6887       if (FUNCTION_ARG_SIZE (mode, type) > 1 && (cum->words & 1))
6888         offset = 1;
6889       if (cum->words + offset >= max_arg_words
6890           || mode == VOIDmode)
6891         return NULL_RTX;
6892     }
6893
6894   /* The 32bit ABIs and the 64bit ABIs are rather different,
6895      particularly in their handling of FP registers.  We might
6896      be able to cleverly share code between them, but I'm not
6897      going to bother in the hope that splitting them up results
6898      in code that is more easily understood. 
6899
6900      The 64bit code probably is very wrong for structure passing.  */
6901   if (TARGET_64BIT)
6902     {
6903       /* Advance the base registers to their current locations.
6904
6905          Remember, gprs grow towards smaller register numbers while
6906          fprs grow to higher register numbers.  Also remember FP regs
6907          are always 4 bytes wide, while the size of an integer register
6908          varies based on the size of the target word.  */
6909       gpr_reg_base = 26 - cum->words;
6910       fpr_reg_base = 32 + cum->words;
6911           
6912       /* If the argument is more than a word long, then we need to align
6913          the base registers.  Same caveats as above.  */
6914       if (FUNCTION_ARG_SIZE (mode, type) > 1)
6915         {
6916           if (mode != BLKmode)
6917             {
6918               /* First deal with alignment of the doubleword.  */
6919               gpr_reg_base -= (cum->words & 1);
6920
6921               /* This seems backwards, but it is what HP specifies.  We need
6922                  gpr_reg_base to point to the smaller numbered register of
6923                  the integer register pair.  So if we have an even register
6924                   number, then decrement the gpr base.  */
6925               gpr_reg_base -= ((gpr_reg_base % 2) == 0);
6926
6927               /* FP values behave sanely, except that each FP reg is only
6928                  half of word.  */
6929               fpr_reg_base += ((fpr_reg_base % 2) == 0);
6930             }
6931           else
6932             {
6933               rtx loc[8];
6934               int i, offset = 0, ub;
6935               ub = FUNCTION_ARG_SIZE (mode, type); 
6936               ub = MIN(ub,
6937                        MAX(0, max_arg_words - cum->words - (cum->words & 1)));
6938               gpr_reg_base -= (cum->words & 1);
6939               for (i = 0; i < ub; i++)
6940                 {
6941                   loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
6942                                               gen_rtx_REG (DImode,
6943                                                            gpr_reg_base),
6944                                               GEN_INT(offset));
6945                   gpr_reg_base -= 1;
6946                   offset += 8;
6947                 }
6948               if (ub == 0)
6949                 return NULL_RTX;
6950               else if (ub == 1)
6951                 return XEXP (loc[0], 0);
6952               else
6953                 return gen_rtx_PARALLEL(mode, gen_rtvec_v(ub, loc));
6954             }
6955         }
6956     }
6957   else
6958     {
6959       /* If the argument is larger than a word, then we know precisely
6960          which registers we must use.  */
6961       if (FUNCTION_ARG_SIZE (mode, type) > 1)
6962         {
6963           if (cum->words)
6964             {
6965               gpr_reg_base = 23;
6966               fpr_reg_base = 38;
6967             }
6968           else
6969             {
6970               gpr_reg_base = 25;
6971               fpr_reg_base = 34;
6972             }
6973         }
6974       else
6975         {
6976            /* We have a single word (32 bits).  A simple computation
6977               will get us the register #s we need.  */
6978            gpr_reg_base = 26 - cum->words;
6979            fpr_reg_base = 32 + 2 * cum->words;
6980         }
6981     }
6982
6983   if (TARGET_64BIT && mode == TFmode)
6984     {
6985       return
6986         gen_rtx_PARALLEL
6987           (mode,
6988            gen_rtvec (2,
6989                       gen_rtx_EXPR_LIST (VOIDmode,
6990                                          gen_rtx_REG (DImode, gpr_reg_base + 1),
6991                                          const0_rtx),
6992                       gen_rtx_EXPR_LIST (VOIDmode,
6993                                          gen_rtx_REG (DImode, gpr_reg_base),
6994                                          GEN_INT (8))));
6995     }
6996   /* Determine if the register needs to be passed in both general and
6997      floating point registers.  */
6998   if ((TARGET_PORTABLE_RUNTIME || TARGET_64BIT)
6999       /* If we are doing soft-float with portable runtime, then there
7000          is no need to worry about FP regs.  */
7001       && ! TARGET_SOFT_FLOAT
7002       /* The parameter must be some kind of float, else we can just
7003          pass it in integer registers.  */
7004       && FLOAT_MODE_P (mode)
7005       /* The target function must not have a prototype.  */
7006       && cum->nargs_prototype <= 0
7007       /* libcalls do not need to pass items in both FP and general
7008          registers.  */
7009       && type != NULL_TREE
7010       /* All this hair applies to outgoing args only.  */
7011       && !incoming)
7012     {
7013       retval
7014         = gen_rtx_PARALLEL
7015             (mode,
7016              gen_rtvec (2,
7017                         gen_rtx_EXPR_LIST (VOIDmode,
7018                                            gen_rtx_REG (mode, fpr_reg_base),
7019                                            const0_rtx),
7020                         gen_rtx_EXPR_LIST (VOIDmode,
7021                                            gen_rtx_REG (mode, gpr_reg_base),
7022                                            const0_rtx)));
7023     }
7024   else
7025     {
7026       /* See if we should pass this parameter in a general register.  */
7027       if (TARGET_SOFT_FLOAT
7028           /* Indirect calls in the normal 32bit ABI require all arguments
7029              to be passed in general registers.  */
7030           || (!TARGET_PORTABLE_RUNTIME
7031               && !TARGET_64BIT
7032               && cum->indirect)
7033           /* If the parameter is not a floating point parameter, then
7034              it belongs in GPRs.  */
7035           || !FLOAT_MODE_P (mode))
7036         retval = gen_rtx_REG (mode, gpr_reg_base);
7037       else
7038         retval = gen_rtx_REG (mode, fpr_reg_base);
7039     }
7040   return retval;
7041 }
7042
7043
7044 /* If this arg would be passed totally in registers or totally on the stack,
7045    then this routine should return zero. It is currently called only for
7046    the 64-bit target. */
7047 int
7048 function_arg_partial_nregs (cum, mode, type, named)
7049      CUMULATIVE_ARGS *cum;
7050      enum machine_mode mode;
7051      tree type;
7052      int named ATTRIBUTE_UNUSED;
7053 {
7054   int max_arg_words = 8;
7055   int offset = 0;
7056
7057   if (FUNCTION_ARG_SIZE(mode, type) > 1 && (cum->words & 1))
7058     offset = 1;
7059
7060   if (cum->words + offset + FUNCTION_ARG_SIZE(mode, type) <= max_arg_words)
7061     /* Arg fits fully into registers. */
7062     return 0;
7063   else if (cum->words + offset >= max_arg_words) 
7064     /* Arg fully on the stack. */
7065     return 0;
7066   else
7067     /* Arg is split. */
7068     return max_arg_words - cum->words - offset;
7069
7070 }
7071
7072
7073 /* Return 1 if this is a comparison operator.  This allows the use of
7074    MATCH_OPERATOR to recognize all the branch insns.  */
7075
7076 int
7077 cmpib_comparison_operator (op, mode)
7078     register rtx op;
7079     enum machine_mode mode;
7080 {
7081   return ((mode == VOIDmode || GET_MODE (op) == mode)
7082           && (GET_CODE (op) == EQ
7083               || GET_CODE (op) == NE
7084               || GET_CODE (op) == GT
7085               || GET_CODE (op) == GTU
7086               || GET_CODE (op) == GE
7087               || GET_CODE (op) == LT
7088               || GET_CODE (op) == LE
7089               || GET_CODE (op) == LEU));
7090 }
7091
7092 /* Mark ARG (which is really a struct deferred_plabel **) for GC.  */
7093
7094 static void
7095 mark_deferred_plabels (arg)
7096      void *arg;
7097 {
7098   struct deferred_plabel *dp = *(struct deferred_plabel **) arg;
7099   int i;
7100
7101   for (i = 0; i < n_deferred_plabels; ++i)
7102     ggc_mark_rtx (dp[i].internal_label);
7103 }
7104
7105 /* Called to register all of our global variables with the garbage
7106    collector.  */
7107
7108 static void 
7109 pa_add_gc_roots ()
7110 {
7111   ggc_add_rtx_root (&hppa_compare_op0, 1);
7112   ggc_add_rtx_root (&hppa_compare_op1, 1);
7113   ggc_add_rtx_root (&hp_profile_label_rtx, 1);
7114   ggc_add_root (&deferred_plabels, 1, sizeof (&deferred_plabels),
7115                 &mark_deferred_plabels);
7116 }