OSDN Git Service

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