OSDN Git Service

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