OSDN Git Service

4948174af93ec0a4dcaaf61dba04c2a33d16bedf
[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 Free Software Foundation, Inc.
4    Contributed by Tim Moore (moore@cs.utah.edu), based on sparc.c
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "output.h"
37 #include "except.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "reload.h"
41 #include "integrate.h"
42 #include "function.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 copy_reg_pointer (rtx, rtx);
88 static void fix_range (const char *);
89 static bool pa_handle_option (size_t, const char *, int);
90 static int hppa_address_cost (rtx);
91 static bool hppa_rtx_costs (rtx, int, int, int *);
92 static inline rtx force_mode (enum machine_mode, rtx);
93 static void pa_reorg (void);
94 static void pa_combine_instructions (void);
95 static int pa_can_combine_p (rtx, rtx, rtx, int, rtx, rtx, rtx);
96 static int forward_branch_p (rtx);
97 static void compute_zdepwi_operands (unsigned HOST_WIDE_INT, unsigned *);
98 static int compute_movmem_length (rtx);
99 static int compute_clrmem_length (rtx);
100 static bool pa_assemble_integer (rtx, unsigned int, int);
101 static void remove_useless_addtr_insns (int);
102 static void store_reg (int, HOST_WIDE_INT, int);
103 static void store_reg_modify (int, int, HOST_WIDE_INT);
104 static void load_reg (int, HOST_WIDE_INT, int);
105 static void set_reg_plus_d (int, int, HOST_WIDE_INT, int);
106 static void pa_output_function_prologue (FILE *, HOST_WIDE_INT);
107 static void update_total_code_bytes (int);
108 static void pa_output_function_epilogue (FILE *, HOST_WIDE_INT);
109 static int pa_adjust_cost (rtx, rtx, rtx, int);
110 static int pa_adjust_priority (rtx, int);
111 static int pa_issue_rate (void);
112 static void pa_som_asm_init_sections (void) ATTRIBUTE_UNUSED;
113 static section *pa_select_section (tree, int, unsigned HOST_WIDE_INT)
114      ATTRIBUTE_UNUSED;
115 static void pa_encode_section_info (tree, rtx, int);
116 static const char *pa_strip_name_encoding (const char *);
117 static bool pa_function_ok_for_sibcall (tree, tree);
118 static void pa_globalize_label (FILE *, const char *)
119      ATTRIBUTE_UNUSED;
120 static void pa_asm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
121                                     HOST_WIDE_INT, tree);
122 #if !defined(USE_COLLECT2)
123 static void pa_asm_out_constructor (rtx, int);
124 static void pa_asm_out_destructor (rtx, int);
125 #endif
126 static void pa_init_builtins (void);
127 static rtx hppa_builtin_saveregs (void);
128 static tree hppa_gimplify_va_arg_expr (tree, tree, tree *, tree *);
129 static bool pa_scalar_mode_supported_p (enum machine_mode);
130 static bool pa_commutative_p (rtx x, int outer_code);
131 static void copy_fp_args (rtx) ATTRIBUTE_UNUSED;
132 static int length_fp_args (rtx) ATTRIBUTE_UNUSED;
133 static inline void pa_file_start_level (void) ATTRIBUTE_UNUSED;
134 static inline void pa_file_start_space (int) ATTRIBUTE_UNUSED;
135 static inline void pa_file_start_file (int) ATTRIBUTE_UNUSED;
136 static inline void pa_file_start_mcount (const char*) ATTRIBUTE_UNUSED;
137 static void pa_elf_file_start (void) ATTRIBUTE_UNUSED;
138 static void pa_som_file_start (void) ATTRIBUTE_UNUSED;
139 static void pa_linux_file_start (void) ATTRIBUTE_UNUSED;
140 static void pa_hpux64_gas_file_start (void) ATTRIBUTE_UNUSED;
141 static void pa_hpux64_hpas_file_start (void) ATTRIBUTE_UNUSED;
142 static void output_deferred_plabels (void);
143 static void output_deferred_profile_counters (void) ATTRIBUTE_UNUSED;
144 #ifdef ASM_OUTPUT_EXTERNAL_REAL
145 static void pa_hpux_file_end (void);
146 #endif
147 #ifdef HPUX_LONG_DOUBLE_LIBRARY
148 static void pa_hpux_init_libfuncs (void);
149 #endif
150 static rtx pa_struct_value_rtx (tree, int);
151 static bool pa_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
152                                   tree, bool);
153 static int pa_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
154                                  tree, bool);
155 static struct machine_function * pa_init_machine_status (void);
156 static enum reg_class pa_secondary_reload (bool, rtx, enum reg_class,
157                                            enum machine_mode,
158                                            secondary_reload_info *);
159
160
161 /* The following extra sections are only used for SOM.  */
162 static GTY(()) section *som_readonly_data_section;
163 static GTY(()) section *som_one_only_readonly_data_section;
164 static GTY(()) section *som_one_only_data_section;
165
166 /* Save the operands last given to a compare for use when we
167    generate a scc or bcc insn.  */
168 rtx hppa_compare_op0, hppa_compare_op1;
169 enum cmp_type hppa_branch_type;
170
171 /* Which cpu we are scheduling for.  */
172 enum processor_type pa_cpu = TARGET_SCHED_DEFAULT;
173
174 /* The UNIX standard to use for predefines and linking.  */
175 int flag_pa_unix = TARGET_HPUX_11_11 ? 1998 : TARGET_HPUX_10_10 ? 1995 : 1993;
176
177 /* Counts for the number of callee-saved general and floating point
178    registers which were saved by the current function's prologue.  */
179 static int gr_saved, fr_saved;
180
181 static rtx find_addr_reg (rtx);
182
183 /* Keep track of the number of bytes we have output in the CODE subspace
184    during this compilation so we'll know when to emit inline long-calls.  */
185 unsigned long total_code_bytes;
186
187 /* The last address of the previous function plus the number of bytes in
188    associated thunks that have been output.  This is used to determine if
189    a thunk can use an IA-relative branch to reach its target function.  */
190 static int last_address;
191
192 /* Variables to handle plabels that we discover are necessary at assembly
193    output time.  They are output after the current function.  */
194 struct deferred_plabel GTY(())
195 {
196   rtx internal_label;
197   rtx symbol;
198 };
199 static GTY((length ("n_deferred_plabels"))) struct deferred_plabel *
200   deferred_plabels;
201 static size_t n_deferred_plabels = 0;
202
203 \f
204 /* Initialize the GCC target structure.  */
205
206 #undef TARGET_ASM_ALIGNED_HI_OP
207 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
208 #undef TARGET_ASM_ALIGNED_SI_OP
209 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
210 #undef TARGET_ASM_ALIGNED_DI_OP
211 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
212 #undef TARGET_ASM_UNALIGNED_HI_OP
213 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
214 #undef TARGET_ASM_UNALIGNED_SI_OP
215 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
216 #undef TARGET_ASM_UNALIGNED_DI_OP
217 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
218 #undef TARGET_ASM_INTEGER
219 #define TARGET_ASM_INTEGER pa_assemble_integer
220
221 #undef TARGET_ASM_FUNCTION_PROLOGUE
222 #define TARGET_ASM_FUNCTION_PROLOGUE pa_output_function_prologue
223 #undef TARGET_ASM_FUNCTION_EPILOGUE
224 #define TARGET_ASM_FUNCTION_EPILOGUE pa_output_function_epilogue
225
226 #undef TARGET_SCHED_ADJUST_COST
227 #define TARGET_SCHED_ADJUST_COST pa_adjust_cost
228 #undef TARGET_SCHED_ADJUST_PRIORITY
229 #define TARGET_SCHED_ADJUST_PRIORITY pa_adjust_priority
230 #undef TARGET_SCHED_ISSUE_RATE
231 #define TARGET_SCHED_ISSUE_RATE pa_issue_rate
232
233 #undef TARGET_ENCODE_SECTION_INFO
234 #define TARGET_ENCODE_SECTION_INFO pa_encode_section_info
235 #undef TARGET_STRIP_NAME_ENCODING
236 #define TARGET_STRIP_NAME_ENCODING pa_strip_name_encoding
237
238 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
239 #define TARGET_FUNCTION_OK_FOR_SIBCALL pa_function_ok_for_sibcall
240
241 #undef TARGET_COMMUTATIVE_P
242 #define TARGET_COMMUTATIVE_P pa_commutative_p
243
244 #undef TARGET_ASM_OUTPUT_MI_THUNK
245 #define TARGET_ASM_OUTPUT_MI_THUNK pa_asm_output_mi_thunk
246 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
247 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
248
249 #undef TARGET_ASM_FILE_END
250 #ifdef ASM_OUTPUT_EXTERNAL_REAL
251 #define TARGET_ASM_FILE_END pa_hpux_file_end
252 #else
253 #define TARGET_ASM_FILE_END output_deferred_plabels
254 #endif
255
256 #if !defined(USE_COLLECT2)
257 #undef TARGET_ASM_CONSTRUCTOR
258 #define TARGET_ASM_CONSTRUCTOR pa_asm_out_constructor
259 #undef TARGET_ASM_DESTRUCTOR
260 #define TARGET_ASM_DESTRUCTOR pa_asm_out_destructor
261 #endif
262
263 #undef TARGET_DEFAULT_TARGET_FLAGS
264 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | TARGET_CPU_DEFAULT)
265 #undef TARGET_HANDLE_OPTION
266 #define TARGET_HANDLE_OPTION pa_handle_option
267
268 #undef TARGET_INIT_BUILTINS
269 #define TARGET_INIT_BUILTINS pa_init_builtins
270
271 #undef TARGET_RTX_COSTS
272 #define TARGET_RTX_COSTS hppa_rtx_costs
273 #undef TARGET_ADDRESS_COST
274 #define TARGET_ADDRESS_COST hppa_address_cost
275
276 #undef TARGET_MACHINE_DEPENDENT_REORG
277 #define TARGET_MACHINE_DEPENDENT_REORG pa_reorg
278
279 #ifdef HPUX_LONG_DOUBLE_LIBRARY
280 #undef TARGET_INIT_LIBFUNCS
281 #define TARGET_INIT_LIBFUNCS pa_hpux_init_libfuncs
282 #endif
283
284 #undef TARGET_PROMOTE_FUNCTION_RETURN
285 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
286 #undef TARGET_PROMOTE_PROTOTYPES
287 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
288
289 #undef TARGET_STRUCT_VALUE_RTX
290 #define TARGET_STRUCT_VALUE_RTX pa_struct_value_rtx
291 #undef TARGET_RETURN_IN_MEMORY
292 #define TARGET_RETURN_IN_MEMORY pa_return_in_memory
293 #undef TARGET_MUST_PASS_IN_STACK
294 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
295 #undef TARGET_PASS_BY_REFERENCE
296 #define TARGET_PASS_BY_REFERENCE pa_pass_by_reference
297 #undef TARGET_CALLEE_COPIES
298 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
299 #undef TARGET_ARG_PARTIAL_BYTES
300 #define TARGET_ARG_PARTIAL_BYTES pa_arg_partial_bytes
301
302 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
303 #define TARGET_EXPAND_BUILTIN_SAVEREGS hppa_builtin_saveregs
304 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
305 #define TARGET_GIMPLIFY_VA_ARG_EXPR hppa_gimplify_va_arg_expr
306
307 #undef TARGET_SCALAR_MODE_SUPPORTED_P
308 #define TARGET_SCALAR_MODE_SUPPORTED_P pa_scalar_mode_supported_p
309
310 #undef TARGET_CANNOT_FORCE_CONST_MEM
311 #define TARGET_CANNOT_FORCE_CONST_MEM pa_tls_referenced_p
312
313 #undef TARGET_SECONDARY_RELOAD
314 #define TARGET_SECONDARY_RELOAD pa_secondary_reload
315
316 struct gcc_target targetm = TARGET_INITIALIZER;
317 \f
318 /* Parse the -mfixed-range= option string.  */
319
320 static void
321 fix_range (const char *const_str)
322 {
323   int i, first, last;
324   char *str, *dash, *comma;
325
326   /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
327      REG2 are either register names or register numbers.  The effect
328      of this option is to mark the registers in the range from REG1 to
329      REG2 as ``fixed'' so they won't be used by the compiler.  This is
330      used, e.g., to ensure that kernel mode code doesn't use fr4-fr31.  */
331
332   i = strlen (const_str);
333   str = (char *) alloca (i + 1);
334   memcpy (str, const_str, i + 1);
335
336   while (1)
337     {
338       dash = strchr (str, '-');
339       if (!dash)
340         {
341           warning (0, "value of -mfixed-range must have form REG1-REG2");
342           return;
343         }
344       *dash = '\0';
345
346       comma = strchr (dash + 1, ',');
347       if (comma)
348         *comma = '\0';
349
350       first = decode_reg_name (str);
351       if (first < 0)
352         {
353           warning (0, "unknown register name: %s", str);
354           return;
355         }
356
357       last = decode_reg_name (dash + 1);
358       if (last < 0)
359         {
360           warning (0, "unknown register name: %s", dash + 1);
361           return;
362         }
363
364       *dash = '-';
365
366       if (first > last)
367         {
368           warning (0, "%s-%s is an empty range", str, dash + 1);
369           return;
370         }
371
372       for (i = first; i <= last; ++i)
373         fixed_regs[i] = call_used_regs[i] = 1;
374
375       if (!comma)
376         break;
377
378       *comma = ',';
379       str = comma + 1;
380     }
381
382   /* Check if all floating point registers have been fixed.  */
383   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
384     if (!fixed_regs[i])
385       break;
386
387   if (i > FP_REG_LAST)
388     target_flags |= MASK_DISABLE_FPREGS;
389 }
390
391 /* Implement TARGET_HANDLE_OPTION.  */
392
393 static bool
394 pa_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
395 {
396   switch (code)
397     {
398     case OPT_mnosnake:
399     case OPT_mpa_risc_1_0:
400     case OPT_march_1_0:
401       target_flags &= ~(MASK_PA_11 | MASK_PA_20);
402       return true;
403
404     case OPT_msnake:
405     case OPT_mpa_risc_1_1:
406     case OPT_march_1_1:
407       target_flags &= ~MASK_PA_20;
408       target_flags |= MASK_PA_11;
409       return true;
410
411     case OPT_mpa_risc_2_0:
412     case OPT_march_2_0:
413       target_flags |= MASK_PA_11 | MASK_PA_20;
414       return true;
415
416     case OPT_mschedule_:
417       if (strcmp (arg, "8000") == 0)
418         pa_cpu = PROCESSOR_8000;
419       else if (strcmp (arg, "7100") == 0)
420         pa_cpu = PROCESSOR_7100;
421       else if (strcmp (arg, "700") == 0)
422         pa_cpu = PROCESSOR_700;
423       else if (strcmp (arg, "7100LC") == 0)
424         pa_cpu = PROCESSOR_7100LC;
425       else if (strcmp (arg, "7200") == 0)
426         pa_cpu = PROCESSOR_7200;
427       else if (strcmp (arg, "7300") == 0)
428         pa_cpu = PROCESSOR_7300;
429       else
430         return false;
431       return true;
432
433     case OPT_mfixed_range_:
434       fix_range (arg);
435       return true;
436
437 #if TARGET_HPUX
438     case OPT_munix_93:
439       flag_pa_unix = 1993;
440       return true;
441 #endif
442
443 #if TARGET_HPUX_10_10
444     case OPT_munix_95:
445       flag_pa_unix = 1995;
446       return true;
447 #endif
448
449 #if TARGET_HPUX_11_11
450     case OPT_munix_98:
451       flag_pa_unix = 1998;
452       return true;
453 #endif
454
455     default:
456       return true;
457     }
458 }
459
460 void
461 override_options (void)
462 {
463   /* Unconditional branches in the delay slot are not compatible with dwarf2
464      call frame information.  There is no benefit in using this optimization
465      on PA8000 and later processors.  */
466   if (pa_cpu >= PROCESSOR_8000
467       || (! USING_SJLJ_EXCEPTIONS && flag_exceptions)
468       || flag_unwind_tables)
469     target_flags &= ~MASK_JUMP_IN_DELAY;
470
471   if (flag_pic && TARGET_PORTABLE_RUNTIME)
472     {
473       warning (0, "PIC code generation is not supported in the portable runtime model");
474     }
475
476   if (flag_pic && TARGET_FAST_INDIRECT_CALLS)
477    {
478       warning (0, "PIC code generation is not compatible with fast indirect calls");
479    }
480
481   if (! TARGET_GAS && write_symbols != NO_DEBUG)
482     {
483       warning (0, "-g is only supported when using GAS on this processor,");
484       warning (0, "-g option disabled");
485       write_symbols = NO_DEBUG;
486     }
487
488   /* We only support the "big PIC" model now.  And we always generate PIC
489      code when in 64bit mode.  */
490   if (flag_pic == 1 || TARGET_64BIT)
491     flag_pic = 2;
492
493   /* We can't guarantee that .dword is available for 32-bit targets.  */
494   if (UNITS_PER_WORD == 4)
495     targetm.asm_out.aligned_op.di = NULL;
496
497   /* The unaligned ops are only available when using GAS.  */
498   if (!TARGET_GAS)
499     {
500       targetm.asm_out.unaligned_op.hi = NULL;
501       targetm.asm_out.unaligned_op.si = NULL;
502       targetm.asm_out.unaligned_op.di = NULL;
503     }
504
505   init_machine_status = pa_init_machine_status;
506 }
507
508 static void
509 pa_init_builtins (void)
510 {
511 #ifdef DONT_HAVE_FPUTC_UNLOCKED
512   built_in_decls[(int) BUILT_IN_FPUTC_UNLOCKED] =
513     built_in_decls[(int) BUILT_IN_PUTC_UNLOCKED];
514   implicit_built_in_decls[(int) BUILT_IN_FPUTC_UNLOCKED]
515     = implicit_built_in_decls[(int) BUILT_IN_PUTC_UNLOCKED];
516 #endif
517 #if TARGET_HPUX_11
518   if (built_in_decls [BUILT_IN_FINITE])
519     set_user_assembler_name (built_in_decls [BUILT_IN_FINITE], "_Isfinite");
520   if (built_in_decls [BUILT_IN_FINITEF])
521     set_user_assembler_name (built_in_decls [BUILT_IN_FINITEF], "_Isfinitef");
522 #endif
523 }
524
525 /* Function to init struct machine_function.
526    This will be called, via a pointer variable,
527    from push_function_context.  */
528
529 static struct machine_function *
530 pa_init_machine_status (void)
531 {
532   return ggc_alloc_cleared (sizeof (machine_function));
533 }
534
535 /* If FROM is a probable pointer register, mark TO as a probable
536    pointer register with the same pointer alignment as FROM.  */
537
538 static void
539 copy_reg_pointer (rtx to, rtx from)
540 {
541   if (REG_POINTER (from))
542     mark_reg_pointer (to, REGNO_POINTER_ALIGN (REGNO (from)));
543 }
544
545 /* Return 1 if X contains a symbolic expression.  We know these
546    expressions will have one of a few well defined forms, so
547    we need only check those forms.  */
548 int
549 symbolic_expression_p (rtx x)
550 {
551
552   /* Strip off any HIGH.  */
553   if (GET_CODE (x) == HIGH)
554     x = XEXP (x, 0);
555
556   return (symbolic_operand (x, VOIDmode));
557 }
558
559 /* Accept any constant that can be moved in one instruction into a
560    general register.  */
561 int
562 cint_ok_for_move (HOST_WIDE_INT ival)
563 {
564   /* OK if ldo, ldil, or zdepi, can be used.  */
565   return (VAL_14_BITS_P (ival)
566           || ldil_cint_p (ival)
567           || zdepi_cint_p (ival));
568 }
569 \f
570 /* Return truth value of whether OP can be used as an operand in a
571    adddi3 insn.  */
572 int
573 adddi3_operand (rtx op, enum machine_mode mode)
574 {
575   return (register_operand (op, mode)
576           || (GET_CODE (op) == CONST_INT
577               && (TARGET_64BIT ? INT_14_BITS (op) : INT_11_BITS (op))));
578 }
579
580 /* True iff the operand OP can be used as the destination operand of
581    an integer store.  This also implies the operand could be used as
582    the source operand of an integer load.  Symbolic, lo_sum and indexed
583    memory operands are not allowed.  We accept reloading pseudos and
584    other memory operands.  */
585 int
586 integer_store_memory_operand (rtx op, enum machine_mode mode)
587 {
588   return ((reload_in_progress
589            && REG_P (op)
590            && REGNO (op) >= FIRST_PSEUDO_REGISTER
591            && reg_renumber [REGNO (op)] < 0)
592           || (GET_CODE (op) == MEM
593               && (reload_in_progress || memory_address_p (mode, XEXP (op, 0)))
594               && !symbolic_memory_operand (op, VOIDmode)
595               && !IS_LO_SUM_DLT_ADDR_P (XEXP (op, 0))
596               && !IS_INDEX_ADDR_P (XEXP (op, 0))));
597 }
598
599 /* True iff ldil can be used to load this CONST_INT.  The least
600    significant 11 bits of the value must be zero and the value must
601    not change sign when extended from 32 to 64 bits.  */
602 int
603 ldil_cint_p (HOST_WIDE_INT ival)
604 {
605   HOST_WIDE_INT x = ival & (((HOST_WIDE_INT) -1 << 31) | 0x7ff);
606
607   return x == 0 || x == ((HOST_WIDE_INT) -1 << 31);
608 }
609
610 /* True iff zdepi can be used to generate this CONST_INT.
611    zdepi first sign extends a 5-bit signed number to a given field
612    length, then places this field anywhere in a zero.  */
613 int
614 zdepi_cint_p (unsigned HOST_WIDE_INT x)
615 {
616   unsigned HOST_WIDE_INT lsb_mask, t;
617
618   /* This might not be obvious, but it's at least fast.
619      This function is critical; we don't have the time loops would take.  */
620   lsb_mask = x & -x;
621   t = ((x >> 4) + lsb_mask) & ~(lsb_mask - 1);
622   /* Return true iff t is a power of two.  */
623   return ((t & (t - 1)) == 0);
624 }
625
626 /* True iff depi or extru can be used to compute (reg & mask).
627    Accept bit pattern like these:
628    0....01....1
629    1....10....0
630    1..10..01..1  */
631 int
632 and_mask_p (unsigned HOST_WIDE_INT mask)
633 {
634   mask = ~mask;
635   mask += mask & -mask;
636   return (mask & (mask - 1)) == 0;
637 }
638
639 /* True iff depi can be used to compute (reg | MASK).  */
640 int
641 ior_mask_p (unsigned HOST_WIDE_INT mask)
642 {
643   mask += mask & -mask;
644   return (mask & (mask - 1)) == 0;
645 }
646 \f
647 /* Legitimize PIC addresses.  If the address is already
648    position-independent, we return ORIG.  Newly generated
649    position-independent addresses go to REG.  If we need more
650    than one register, we lose.  */
651
652 rtx
653 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
654 {
655   rtx pic_ref = orig;
656
657   gcc_assert (!PA_SYMBOL_REF_TLS_P (orig));
658
659   /* Labels need special handling.  */
660   if (pic_label_operand (orig, mode))
661     {
662       /* We do not want to go through the movXX expanders here since that
663          would create recursion.
664
665          Nor do we really want to call a generator for a named pattern
666          since that requires multiple patterns if we want to support
667          multiple word sizes.
668
669          So instead we just emit the raw set, which avoids the movXX
670          expanders completely.  */
671       mark_reg_pointer (reg, BITS_PER_UNIT);
672       emit_insn (gen_rtx_SET (VOIDmode, reg, orig));
673       current_function_uses_pic_offset_table = 1;
674       return reg;
675     }
676   if (GET_CODE (orig) == SYMBOL_REF)
677     {
678       rtx insn, tmp_reg;
679
680       gcc_assert (reg);
681
682       /* Before reload, allocate a temporary register for the intermediate
683          result.  This allows the sequence to be deleted when the final
684          result is unused and the insns are trivially dead.  */
685       tmp_reg = ((reload_in_progress || reload_completed)
686                  ? reg : gen_reg_rtx (Pmode));
687
688       emit_move_insn (tmp_reg,
689                       gen_rtx_PLUS (word_mode, pic_offset_table_rtx,
690                                     gen_rtx_HIGH (word_mode, orig)));
691       pic_ref
692         = gen_const_mem (Pmode,
693                          gen_rtx_LO_SUM (Pmode, tmp_reg,
694                                          gen_rtx_UNSPEC (Pmode,
695                                                          gen_rtvec (1, orig),
696                                                          UNSPEC_DLTIND14R)));
697
698       current_function_uses_pic_offset_table = 1;
699       mark_reg_pointer (reg, BITS_PER_UNIT);
700       insn = emit_move_insn (reg, pic_ref);
701
702       /* Put a REG_EQUAL note on this insn, so that it can be optimized.  */
703       set_unique_reg_note (insn, REG_EQUAL, orig);
704
705       return reg;
706     }
707   else if (GET_CODE (orig) == CONST)
708     {
709       rtx base;
710
711       if (GET_CODE (XEXP (orig, 0)) == PLUS
712           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
713         return orig;
714
715       gcc_assert (reg);
716       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
717       
718       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
719       orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
720                                      base == reg ? 0 : reg);
721
722       if (GET_CODE (orig) == CONST_INT)
723         {
724           if (INT_14_BITS (orig))
725             return plus_constant (base, INTVAL (orig));
726           orig = force_reg (Pmode, orig);
727         }
728       pic_ref = gen_rtx_PLUS (Pmode, base, orig);
729       /* Likewise, should we set special REG_NOTEs here?  */
730     }
731
732   return pic_ref;
733 }
734
735 static GTY(()) rtx gen_tls_tga;
736
737 static rtx
738 gen_tls_get_addr (void)
739 {
740   if (!gen_tls_tga)
741     gen_tls_tga = init_one_libfunc ("__tls_get_addr");
742   return gen_tls_tga;
743 }
744
745 static rtx
746 hppa_tls_call (rtx arg)
747 {
748   rtx ret;
749
750   ret = gen_reg_rtx (Pmode);
751   emit_library_call_value (gen_tls_get_addr (), ret,
752                            LCT_CONST, Pmode, 1, arg, Pmode);
753
754   return ret;
755 }
756
757 static rtx
758 legitimize_tls_address (rtx addr)
759 {
760   rtx ret, insn, tmp, t1, t2, tp;
761   enum tls_model model = SYMBOL_REF_TLS_MODEL (addr);
762
763   switch (model) 
764     {
765       case TLS_MODEL_GLOBAL_DYNAMIC:
766         tmp = gen_reg_rtx (Pmode);
767         if (flag_pic)
768           emit_insn (gen_tgd_load_pic (tmp, addr));
769         else
770           emit_insn (gen_tgd_load (tmp, addr));
771         ret = hppa_tls_call (tmp);
772         break;
773
774       case TLS_MODEL_LOCAL_DYNAMIC:
775         ret = gen_reg_rtx (Pmode);
776         tmp = gen_reg_rtx (Pmode);
777         start_sequence ();
778         if (flag_pic)
779           emit_insn (gen_tld_load_pic (tmp, addr));
780         else
781           emit_insn (gen_tld_load (tmp, addr));
782         t1 = hppa_tls_call (tmp);
783         insn = get_insns ();
784         end_sequence ();
785         t2 = gen_reg_rtx (Pmode);
786         emit_libcall_block (insn, t2, t1, 
787                             gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
788                                             UNSPEC_TLSLDBASE));
789         emit_insn (gen_tld_offset_load (ret, addr, t2));
790         break;
791
792       case TLS_MODEL_INITIAL_EXEC:
793         tp = gen_reg_rtx (Pmode);
794         tmp = gen_reg_rtx (Pmode);
795         ret = gen_reg_rtx (Pmode);
796         emit_insn (gen_tp_load (tp));
797         if (flag_pic)
798           emit_insn (gen_tie_load_pic (tmp, addr));
799         else
800           emit_insn (gen_tie_load (tmp, addr));
801         emit_move_insn (ret, gen_rtx_PLUS (Pmode, tp, tmp));
802         break;
803
804       case TLS_MODEL_LOCAL_EXEC:
805         tp = gen_reg_rtx (Pmode);
806         ret = gen_reg_rtx (Pmode);
807         emit_insn (gen_tp_load (tp));
808         emit_insn (gen_tle_load (ret, addr, tp));
809         break;
810
811       default:
812         gcc_unreachable ();
813     }
814
815   return ret;
816 }
817
818 /* Try machine-dependent ways of modifying an illegitimate address
819    to be legitimate.  If we find one, return the new, valid address.
820    This macro is used in only one place: `memory_address' in explow.c.
821
822    OLDX is the address as it was before break_out_memory_refs was called.
823    In some cases it is useful to look at this to decide what needs to be done.
824
825    MODE and WIN are passed so that this macro can use
826    GO_IF_LEGITIMATE_ADDRESS.
827
828    It is always safe for this macro to do nothing.  It exists to recognize
829    opportunities to optimize the output.
830
831    For the PA, transform:
832
833         memory(X + <large int>)
834
835    into:
836
837         if (<large int> & mask) >= 16
838           Y = (<large int> & ~mask) + mask + 1  Round up.
839         else
840           Y = (<large int> & ~mask)             Round down.
841         Z = X + Y
842         memory (Z + (<large int> - Y));
843
844    This is for CSE to find several similar references, and only use one Z.
845
846    X can either be a SYMBOL_REF or REG, but because combine cannot
847    perform a 4->2 combination we do nothing for SYMBOL_REF + D where
848    D will not fit in 14 bits.
849
850    MODE_FLOAT references allow displacements which fit in 5 bits, so use
851    0x1f as the mask.
852
853    MODE_INT references allow displacements which fit in 14 bits, so use
854    0x3fff as the mask.
855
856    This relies on the fact that most mode MODE_FLOAT references will use FP
857    registers and most mode MODE_INT references will use integer registers.
858    (In the rare case of an FP register used in an integer MODE, we depend
859    on secondary reloads to clean things up.)
860
861
862    It is also beneficial to handle (plus (mult (X) (Y)) (Z)) in a special
863    manner if Y is 2, 4, or 8.  (allows more shadd insns and shifted indexed
864    addressing modes to be used).
865
866    Put X and Z into registers.  Then put the entire expression into
867    a register.  */
868
869 rtx
870 hppa_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
871                          enum machine_mode mode)
872 {
873   rtx orig = x;
874
875   /* We need to canonicalize the order of operands in unscaled indexed
876      addresses since the code that checks if an address is valid doesn't
877      always try both orders.  */
878   if (!TARGET_NO_SPACE_REGS
879       && GET_CODE (x) == PLUS
880       && GET_MODE (x) == Pmode
881       && REG_P (XEXP (x, 0))
882       && REG_P (XEXP (x, 1))
883       && REG_POINTER (XEXP (x, 0))
884       && !REG_POINTER (XEXP (x, 1)))
885     return gen_rtx_PLUS (Pmode, XEXP (x, 1), XEXP (x, 0));
886
887   if (PA_SYMBOL_REF_TLS_P (x))
888     return legitimize_tls_address (x);
889   else if (flag_pic)
890     return legitimize_pic_address (x, mode, gen_reg_rtx (Pmode));
891
892   /* Strip off CONST.  */
893   if (GET_CODE (x) == CONST)
894     x = XEXP (x, 0);
895
896   /* Special case.  Get the SYMBOL_REF into a register and use indexing.
897      That should always be safe.  */
898   if (GET_CODE (x) == PLUS
899       && GET_CODE (XEXP (x, 0)) == REG
900       && GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
901     {
902       rtx reg = force_reg (Pmode, XEXP (x, 1));
903       return force_reg (Pmode, gen_rtx_PLUS (Pmode, reg, XEXP (x, 0)));
904     }
905
906   /* Note we must reject symbols which represent function addresses
907      since the assembler/linker can't handle arithmetic on plabels.  */
908   if (GET_CODE (x) == PLUS
909       && GET_CODE (XEXP (x, 1)) == CONST_INT
910       && ((GET_CODE (XEXP (x, 0)) == SYMBOL_REF
911            && !FUNCTION_NAME_P (XSTR (XEXP (x, 0), 0)))
912           || GET_CODE (XEXP (x, 0)) == REG))
913     {
914       rtx int_part, ptr_reg;
915       int newoffset;
916       int offset = INTVAL (XEXP (x, 1));
917       int mask;
918
919       mask = (GET_MODE_CLASS (mode) == MODE_FLOAT
920               ? (TARGET_PA_20 ? 0x3fff : 0x1f) : 0x3fff);
921
922       /* Choose which way to round the offset.  Round up if we
923          are >= halfway to the next boundary.  */
924       if ((offset & mask) >= ((mask + 1) / 2))
925         newoffset = (offset & ~ mask) + mask + 1;
926       else
927         newoffset = (offset & ~ mask);
928
929       /* If the newoffset will not fit in 14 bits (ldo), then
930          handling this would take 4 or 5 instructions (2 to load
931          the SYMBOL_REF + 1 or 2 to load the newoffset + 1 to
932          add the new offset and the SYMBOL_REF.)  Combine can
933          not handle 4->2 or 5->2 combinations, so do not create
934          them.  */
935       if (! VAL_14_BITS_P (newoffset)
936           && GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
937         {
938           rtx const_part = plus_constant (XEXP (x, 0), newoffset);
939           rtx tmp_reg
940             = force_reg (Pmode,
941                          gen_rtx_HIGH (Pmode, const_part));
942           ptr_reg
943             = force_reg (Pmode,
944                          gen_rtx_LO_SUM (Pmode,
945                                          tmp_reg, const_part));
946         }
947       else
948         {
949           if (! VAL_14_BITS_P (newoffset))
950             int_part = force_reg (Pmode, GEN_INT (newoffset));
951           else
952             int_part = GEN_INT (newoffset);
953
954           ptr_reg = force_reg (Pmode,
955                                gen_rtx_PLUS (Pmode,
956                                              force_reg (Pmode, XEXP (x, 0)),
957                                              int_part));
958         }
959       return plus_constant (ptr_reg, offset - newoffset);
960     }
961
962   /* Handle (plus (mult (a) (shadd_constant)) (b)).  */
963
964   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT
965       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
966       && shadd_constant_p (INTVAL (XEXP (XEXP (x, 0), 1)))
967       && (OBJECT_P (XEXP (x, 1))
968           || GET_CODE (XEXP (x, 1)) == SUBREG)
969       && GET_CODE (XEXP (x, 1)) != CONST)
970     {
971       int val = INTVAL (XEXP (XEXP (x, 0), 1));
972       rtx reg1, reg2;
973
974       reg1 = XEXP (x, 1);
975       if (GET_CODE (reg1) != REG)
976         reg1 = force_reg (Pmode, force_operand (reg1, 0));
977
978       reg2 = XEXP (XEXP (x, 0), 0);
979       if (GET_CODE (reg2) != REG)
980         reg2 = force_reg (Pmode, force_operand (reg2, 0));
981
982       return force_reg (Pmode, gen_rtx_PLUS (Pmode,
983                                              gen_rtx_MULT (Pmode,
984                                                            reg2,
985                                                            GEN_INT (val)),
986                                              reg1));
987     }
988
989   /* Similarly for (plus (plus (mult (a) (shadd_constant)) (b)) (c)).
990
991      Only do so for floating point modes since this is more speculative
992      and we lose if it's an integer store.  */
993   if (GET_CODE (x) == PLUS
994       && GET_CODE (XEXP (x, 0)) == PLUS
995       && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
996       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
997       && shadd_constant_p (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1)))
998       && (mode == SFmode || mode == DFmode))
999     {
1000
1001       /* First, try and figure out what to use as a base register.  */
1002       rtx reg1, reg2, base, idx, orig_base;
1003
1004       reg1 = XEXP (XEXP (x, 0), 1);
1005       reg2 = XEXP (x, 1);
1006       base = NULL_RTX;
1007       idx = NULL_RTX;
1008
1009       /* Make sure they're both regs.  If one was a SYMBOL_REF [+ const],
1010          then emit_move_sequence will turn on REG_POINTER so we'll know
1011          it's a base register below.  */
1012       if (GET_CODE (reg1) != REG)
1013         reg1 = force_reg (Pmode, force_operand (reg1, 0));
1014
1015       if (GET_CODE (reg2) != REG)
1016         reg2 = force_reg (Pmode, force_operand (reg2, 0));
1017
1018       /* Figure out what the base and index are.  */
1019
1020       if (GET_CODE (reg1) == REG
1021           && REG_POINTER (reg1))
1022         {
1023           base = reg1;
1024           orig_base = XEXP (XEXP (x, 0), 1);
1025           idx = gen_rtx_PLUS (Pmode,
1026                               gen_rtx_MULT (Pmode,
1027                                             XEXP (XEXP (XEXP (x, 0), 0), 0),
1028                                             XEXP (XEXP (XEXP (x, 0), 0), 1)),
1029                               XEXP (x, 1));
1030         }
1031       else if (GET_CODE (reg2) == REG
1032                && REG_POINTER (reg2))
1033         {
1034           base = reg2;
1035           orig_base = XEXP (x, 1);
1036           idx = XEXP (x, 0);
1037         }
1038
1039       if (base == 0)
1040         return orig;
1041
1042       /* If the index adds a large constant, try to scale the
1043          constant so that it can be loaded with only one insn.  */
1044       if (GET_CODE (XEXP (idx, 1)) == CONST_INT
1045           && VAL_14_BITS_P (INTVAL (XEXP (idx, 1))
1046                             / INTVAL (XEXP (XEXP (idx, 0), 1)))
1047           && INTVAL (XEXP (idx, 1)) % INTVAL (XEXP (XEXP (idx, 0), 1)) == 0)
1048         {
1049           /* Divide the CONST_INT by the scale factor, then add it to A.  */
1050           int val = INTVAL (XEXP (idx, 1));
1051
1052           val /= INTVAL (XEXP (XEXP (idx, 0), 1));
1053           reg1 = XEXP (XEXP (idx, 0), 0);
1054           if (GET_CODE (reg1) != REG)
1055             reg1 = force_reg (Pmode, force_operand (reg1, 0));
1056
1057           reg1 = force_reg (Pmode, gen_rtx_PLUS (Pmode, reg1, GEN_INT (val)));
1058
1059           /* We can now generate a simple scaled indexed address.  */
1060           return
1061             force_reg
1062               (Pmode, gen_rtx_PLUS (Pmode,
1063                                     gen_rtx_MULT (Pmode, reg1,
1064                                                   XEXP (XEXP (idx, 0), 1)),
1065                                     base));
1066         }
1067
1068       /* If B + C is still a valid base register, then add them.  */
1069       if (GET_CODE (XEXP (idx, 1)) == CONST_INT
1070           && INTVAL (XEXP (idx, 1)) <= 4096
1071           && INTVAL (XEXP (idx, 1)) >= -4096)
1072         {
1073           int val = INTVAL (XEXP (XEXP (idx, 0), 1));
1074           rtx reg1, reg2;
1075
1076           reg1 = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, XEXP (idx, 1)));
1077
1078           reg2 = XEXP (XEXP (idx, 0), 0);
1079           if (GET_CODE (reg2) != CONST_INT)
1080             reg2 = force_reg (Pmode, force_operand (reg2, 0));
1081
1082           return force_reg (Pmode, gen_rtx_PLUS (Pmode,
1083                                                  gen_rtx_MULT (Pmode,
1084                                                                reg2,
1085                                                                GEN_INT (val)),
1086                                                  reg1));
1087         }
1088
1089       /* Get the index into a register, then add the base + index and
1090          return a register holding the result.  */
1091
1092       /* First get A into a register.  */
1093       reg1 = XEXP (XEXP (idx, 0), 0);
1094       if (GET_CODE (reg1) != REG)
1095         reg1 = force_reg (Pmode, force_operand (reg1, 0));
1096
1097       /* And get B into a register.  */
1098       reg2 = XEXP (idx, 1);
1099       if (GET_CODE (reg2) != REG)
1100         reg2 = force_reg (Pmode, force_operand (reg2, 0));
1101
1102       reg1 = force_reg (Pmode,
1103                         gen_rtx_PLUS (Pmode,
1104                                       gen_rtx_MULT (Pmode, reg1,
1105                                                     XEXP (XEXP (idx, 0), 1)),
1106                                       reg2));
1107
1108       /* Add the result to our base register and return.  */
1109       return force_reg (Pmode, gen_rtx_PLUS (Pmode, base, reg1));
1110
1111     }
1112
1113   /* Uh-oh.  We might have an address for x[n-100000].  This needs
1114      special handling to avoid creating an indexed memory address
1115      with x-100000 as the base.
1116
1117      If the constant part is small enough, then it's still safe because
1118      there is a guard page at the beginning and end of the data segment.
1119
1120      Scaled references are common enough that we want to try and rearrange the
1121      terms so that we can use indexing for these addresses too.  Only
1122      do the optimization for floatint point modes.  */
1123
1124   if (GET_CODE (x) == PLUS
1125       && symbolic_expression_p (XEXP (x, 1)))
1126     {
1127       /* Ugly.  We modify things here so that the address offset specified
1128          by the index expression is computed first, then added to x to form
1129          the entire address.  */
1130
1131       rtx regx1, regx2, regy1, regy2, y;
1132
1133       /* Strip off any CONST.  */
1134       y = XEXP (x, 1);
1135       if (GET_CODE (y) == CONST)
1136         y = XEXP (y, 0);
1137
1138       if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1139         {
1140           /* See if this looks like
1141                 (plus (mult (reg) (shadd_const))
1142                       (const (plus (symbol_ref) (const_int))))
1143
1144              Where const_int is small.  In that case the const
1145              expression is a valid pointer for indexing.
1146
1147              If const_int is big, but can be divided evenly by shadd_const
1148              and added to (reg).  This allows more scaled indexed addresses.  */
1149           if (GET_CODE (XEXP (y, 0)) == SYMBOL_REF
1150               && GET_CODE (XEXP (x, 0)) == MULT
1151               && GET_CODE (XEXP (y, 1)) == CONST_INT
1152               && INTVAL (XEXP (y, 1)) >= -4096
1153               && INTVAL (XEXP (y, 1)) <= 4095
1154               && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1155               && shadd_constant_p (INTVAL (XEXP (XEXP (x, 0), 1))))
1156             {
1157               int val = INTVAL (XEXP (XEXP (x, 0), 1));
1158               rtx reg1, reg2;
1159
1160               reg1 = XEXP (x, 1);
1161               if (GET_CODE (reg1) != REG)
1162                 reg1 = force_reg (Pmode, force_operand (reg1, 0));
1163
1164               reg2 = XEXP (XEXP (x, 0), 0);
1165               if (GET_CODE (reg2) != REG)
1166                 reg2 = force_reg (Pmode, force_operand (reg2, 0));
1167
1168               return force_reg (Pmode,
1169                                 gen_rtx_PLUS (Pmode,
1170                                               gen_rtx_MULT (Pmode,
1171                                                             reg2,
1172                                                             GEN_INT (val)),
1173                                               reg1));
1174             }
1175           else if ((mode == DFmode || mode == SFmode)
1176                    && GET_CODE (XEXP (y, 0)) == SYMBOL_REF
1177                    && GET_CODE (XEXP (x, 0)) == MULT
1178                    && GET_CODE (XEXP (y, 1)) == CONST_INT
1179                    && INTVAL (XEXP (y, 1)) % INTVAL (XEXP (XEXP (x, 0), 1)) == 0
1180                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1181                    && shadd_constant_p (INTVAL (XEXP (XEXP (x, 0), 1))))
1182             {
1183               regx1
1184                 = force_reg (Pmode, GEN_INT (INTVAL (XEXP (y, 1))
1185                                              / INTVAL (XEXP (XEXP (x, 0), 1))));
1186               regx2 = XEXP (XEXP (x, 0), 0);
1187               if (GET_CODE (regx2) != REG)
1188                 regx2 = force_reg (Pmode, force_operand (regx2, 0));
1189               regx2 = force_reg (Pmode, gen_rtx_fmt_ee (GET_CODE (y), Pmode,
1190                                                         regx2, regx1));
1191               return
1192                 force_reg (Pmode,
1193                            gen_rtx_PLUS (Pmode,
1194                                          gen_rtx_MULT (Pmode, regx2,
1195                                                        XEXP (XEXP (x, 0), 1)),
1196                                          force_reg (Pmode, XEXP (y, 0))));
1197             }
1198           else if (GET_CODE (XEXP (y, 1)) == CONST_INT
1199                    && INTVAL (XEXP (y, 1)) >= -4096
1200                    && INTVAL (XEXP (y, 1)) <= 4095)
1201             {
1202               /* This is safe because of the guard page at the
1203                  beginning and end of the data space.  Just
1204                  return the original address.  */
1205               return orig;
1206             }
1207           else
1208             {
1209               /* Doesn't look like one we can optimize.  */
1210               regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1211               regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1212               regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1213               regx1 = force_reg (Pmode,
1214                                  gen_rtx_fmt_ee (GET_CODE (y), Pmode,
1215                                                  regx1, regy2));
1216               return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1217             }
1218         }
1219     }
1220
1221   return orig;
1222 }
1223
1224 /* For the HPPA, REG and REG+CONST is cost 0
1225    and addresses involving symbolic constants are cost 2.
1226
1227    PIC addresses are very expensive.
1228
1229    It is no coincidence that this has the same structure
1230    as GO_IF_LEGITIMATE_ADDRESS.  */
1231
1232 static int
1233 hppa_address_cost (rtx X)
1234 {
1235   switch (GET_CODE (X))
1236     {
1237     case REG:
1238     case PLUS:
1239     case LO_SUM:
1240       return 1;
1241     case HIGH:
1242       return 2;
1243     default:
1244       return 4;
1245     }
1246 }
1247
1248 /* Compute a (partial) cost for rtx X.  Return true if the complete
1249    cost has been computed, and false if subexpressions should be
1250    scanned.  In either case, *TOTAL contains the cost result.  */
1251
1252 static bool
1253 hppa_rtx_costs (rtx x, int code, int outer_code, int *total)
1254 {
1255   switch (code)
1256     {
1257     case CONST_INT:
1258       if (INTVAL (x) == 0)
1259         *total = 0;
1260       else if (INT_14_BITS (x))
1261         *total = 1;
1262       else
1263         *total = 2;
1264       return true;
1265
1266     case HIGH:
1267       *total = 2;
1268       return true;
1269
1270     case CONST:
1271     case LABEL_REF:
1272     case SYMBOL_REF:
1273       *total = 4;
1274       return true;
1275
1276     case CONST_DOUBLE:
1277       if ((x == CONST0_RTX (DFmode) || x == CONST0_RTX (SFmode))
1278           && outer_code != SET)
1279         *total = 0;
1280       else
1281         *total = 8;
1282       return true;
1283
1284     case MULT:
1285       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1286         *total = COSTS_N_INSNS (3);
1287       else if (TARGET_PA_11 && !TARGET_DISABLE_FPREGS && !TARGET_SOFT_FLOAT)
1288         *total = COSTS_N_INSNS (8);
1289       else
1290         *total = COSTS_N_INSNS (20);
1291       return true;
1292
1293     case DIV:
1294       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1295         {
1296           *total = COSTS_N_INSNS (14);
1297           return true;
1298         }
1299       /* FALLTHRU */
1300
1301     case UDIV:
1302     case MOD:
1303     case UMOD:
1304       *total = COSTS_N_INSNS (60);
1305       return true;
1306
1307     case PLUS: /* this includes shNadd insns */
1308     case MINUS:
1309       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1310         *total = COSTS_N_INSNS (3);
1311       else
1312         *total = COSTS_N_INSNS (1);
1313       return true;
1314
1315     case ASHIFT:
1316     case ASHIFTRT:
1317     case LSHIFTRT:
1318       *total = COSTS_N_INSNS (1);
1319       return true;
1320
1321     default:
1322       return false;
1323     }
1324 }
1325
1326 /* Ensure mode of ORIG, a REG rtx, is MODE.  Returns either ORIG or a
1327    new rtx with the correct mode.  */
1328 static inline rtx
1329 force_mode (enum machine_mode mode, rtx orig)
1330 {
1331   if (mode == GET_MODE (orig))
1332     return orig;
1333
1334   gcc_assert (REGNO (orig) < FIRST_PSEUDO_REGISTER);
1335
1336   return gen_rtx_REG (mode, REGNO (orig));
1337 }
1338
1339 /* Return 1 if *X is a thread-local symbol.  */
1340
1341 static int
1342 pa_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1343 {
1344   return PA_SYMBOL_REF_TLS_P (*x);
1345 }
1346
1347 /* Return 1 if X contains a thread-local symbol.  */
1348
1349 bool
1350 pa_tls_referenced_p (rtx x)
1351 {
1352   if (!TARGET_HAVE_TLS)
1353     return false;
1354
1355   return for_each_rtx (&x, &pa_tls_symbol_ref_1, 0);
1356 }
1357
1358 /* Emit insns to move operands[1] into operands[0].
1359
1360    Return 1 if we have written out everything that needs to be done to
1361    do the move.  Otherwise, return 0 and the caller will emit the move
1362    normally.
1363
1364    Note SCRATCH_REG may not be in the proper mode depending on how it
1365    will be used.  This routine is responsible for creating a new copy
1366    of SCRATCH_REG in the proper mode.  */
1367
1368 int
1369 emit_move_sequence (rtx *operands, enum machine_mode mode, rtx scratch_reg)
1370 {
1371   register rtx operand0 = operands[0];
1372   register rtx operand1 = operands[1];
1373   register rtx tem;
1374
1375   /* We can only handle indexed addresses in the destination operand
1376      of floating point stores.  Thus, we need to break out indexed
1377      addresses from the destination operand.  */
1378   if (GET_CODE (operand0) == MEM && IS_INDEX_ADDR_P (XEXP (operand0, 0)))
1379     {
1380       gcc_assert (can_create_pseudo_p ());
1381
1382       tem = copy_to_mode_reg (Pmode, XEXP (operand0, 0));
1383       operand0 = replace_equiv_address (operand0, tem);
1384     }
1385
1386   /* On targets with non-equivalent space registers, break out unscaled
1387      indexed addresses from the source operand before the final CSE.
1388      We have to do this because the REG_POINTER flag is not correctly
1389      carried through various optimization passes and CSE may substitute
1390      a pseudo without the pointer set for one with the pointer set.  As
1391      a result, we loose various opportunities to create insns with
1392      unscaled indexed addresses.  */
1393   if (!TARGET_NO_SPACE_REGS
1394       && !cse_not_expected
1395       && GET_CODE (operand1) == MEM
1396       && GET_CODE (XEXP (operand1, 0)) == PLUS
1397       && REG_P (XEXP (XEXP (operand1, 0), 0))
1398       && REG_P (XEXP (XEXP (operand1, 0), 1)))
1399     operand1
1400       = replace_equiv_address (operand1,
1401                                copy_to_mode_reg (Pmode, XEXP (operand1, 0)));
1402
1403   if (scratch_reg
1404       && reload_in_progress && GET_CODE (operand0) == REG
1405       && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
1406     operand0 = reg_equiv_mem[REGNO (operand0)];
1407   else if (scratch_reg
1408            && reload_in_progress && GET_CODE (operand0) == SUBREG
1409            && GET_CODE (SUBREG_REG (operand0)) == REG
1410            && REGNO (SUBREG_REG (operand0)) >= FIRST_PSEUDO_REGISTER)
1411     {
1412      /* We must not alter SUBREG_BYTE (operand0) since that would confuse
1413         the code which tracks sets/uses for delete_output_reload.  */
1414       rtx temp = gen_rtx_SUBREG (GET_MODE (operand0),
1415                                  reg_equiv_mem [REGNO (SUBREG_REG (operand0))],
1416                                  SUBREG_BYTE (operand0));
1417       operand0 = alter_subreg (&temp);
1418     }
1419
1420   if (scratch_reg
1421       && reload_in_progress && GET_CODE (operand1) == REG
1422       && REGNO (operand1) >= FIRST_PSEUDO_REGISTER)
1423     operand1 = reg_equiv_mem[REGNO (operand1)];
1424   else if (scratch_reg
1425            && reload_in_progress && GET_CODE (operand1) == SUBREG
1426            && GET_CODE (SUBREG_REG (operand1)) == REG
1427            && REGNO (SUBREG_REG (operand1)) >= FIRST_PSEUDO_REGISTER)
1428     {
1429      /* We must not alter SUBREG_BYTE (operand0) since that would confuse
1430         the code which tracks sets/uses for delete_output_reload.  */
1431       rtx temp = gen_rtx_SUBREG (GET_MODE (operand1),
1432                                  reg_equiv_mem [REGNO (SUBREG_REG (operand1))],
1433                                  SUBREG_BYTE (operand1));
1434       operand1 = alter_subreg (&temp);
1435     }
1436
1437   if (scratch_reg && reload_in_progress && GET_CODE (operand0) == MEM
1438       && ((tem = find_replacement (&XEXP (operand0, 0)))
1439           != XEXP (operand0, 0)))
1440     operand0 = replace_equiv_address (operand0, tem);
1441
1442   if (scratch_reg && reload_in_progress && GET_CODE (operand1) == MEM
1443       && ((tem = find_replacement (&XEXP (operand1, 0)))
1444           != XEXP (operand1, 0)))
1445     operand1 = replace_equiv_address (operand1, tem);
1446
1447   /* Handle secondary reloads for loads/stores of FP registers from
1448      REG+D addresses where D does not fit in 5 or 14 bits, including
1449      (subreg (mem (addr))) cases.  */
1450   if (scratch_reg
1451       && fp_reg_operand (operand0, mode)
1452       && ((GET_CODE (operand1) == MEM
1453            && !memory_address_p ((GET_MODE_SIZE (mode) == 4 ? SFmode : DFmode),
1454                                  XEXP (operand1, 0)))
1455           || ((GET_CODE (operand1) == SUBREG
1456                && GET_CODE (XEXP (operand1, 0)) == MEM
1457                && !memory_address_p ((GET_MODE_SIZE (mode) == 4
1458                                       ? SFmode : DFmode),
1459                                      XEXP (XEXP (operand1, 0), 0))))))
1460     {
1461       if (GET_CODE (operand1) == SUBREG)
1462         operand1 = XEXP (operand1, 0);
1463
1464       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
1465          it in WORD_MODE regardless of what mode it was originally given
1466          to us.  */
1467       scratch_reg = force_mode (word_mode, scratch_reg);
1468
1469       /* D might not fit in 14 bits either; for such cases load D into
1470          scratch reg.  */
1471       if (!memory_address_p (Pmode, XEXP (operand1, 0)))
1472         {
1473           emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
1474           emit_move_insn (scratch_reg,
1475                           gen_rtx_fmt_ee (GET_CODE (XEXP (operand1, 0)),
1476                                           Pmode,
1477                                           XEXP (XEXP (operand1, 0), 0),
1478                                           scratch_reg));
1479         }
1480       else
1481         emit_move_insn (scratch_reg, XEXP (operand1, 0));
1482       emit_insn (gen_rtx_SET (VOIDmode, operand0,
1483                               replace_equiv_address (operand1, scratch_reg)));
1484       return 1;
1485     }
1486   else if (scratch_reg
1487            && fp_reg_operand (operand1, mode)
1488            && ((GET_CODE (operand0) == MEM
1489                 && !memory_address_p ((GET_MODE_SIZE (mode) == 4
1490                                         ? SFmode : DFmode),
1491                                        XEXP (operand0, 0)))
1492                || ((GET_CODE (operand0) == SUBREG)
1493                    && GET_CODE (XEXP (operand0, 0)) == MEM
1494                    && !memory_address_p ((GET_MODE_SIZE (mode) == 4
1495                                           ? SFmode : DFmode),
1496                                          XEXP (XEXP (operand0, 0), 0)))))
1497     {
1498       if (GET_CODE (operand0) == SUBREG)
1499         operand0 = XEXP (operand0, 0);
1500
1501       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
1502          it in WORD_MODE regardless of what mode it was originally given
1503          to us.  */
1504       scratch_reg = force_mode (word_mode, scratch_reg);
1505
1506       /* D might not fit in 14 bits either; for such cases load D into
1507          scratch reg.  */
1508       if (!memory_address_p (Pmode, XEXP (operand0, 0)))
1509         {
1510           emit_move_insn (scratch_reg, XEXP (XEXP (operand0, 0), 1));
1511           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand0,
1512                                                                         0)),
1513                                                        Pmode,
1514                                                        XEXP (XEXP (operand0, 0),
1515                                                                    0),
1516                                                        scratch_reg));
1517         }
1518       else
1519         emit_move_insn (scratch_reg, XEXP (operand0, 0));
1520       emit_insn (gen_rtx_SET (VOIDmode,
1521                               replace_equiv_address (operand0, scratch_reg),
1522                               operand1));
1523       return 1;
1524     }
1525   /* Handle secondary reloads for loads of FP registers from constant
1526      expressions by forcing the constant into memory.
1527
1528      Use scratch_reg to hold the address of the memory location.
1529
1530      The proper fix is to change PREFERRED_RELOAD_CLASS to return
1531      NO_REGS when presented with a const_int and a register class
1532      containing only FP registers.  Doing so unfortunately creates
1533      more problems than it solves.   Fix this for 2.5.  */
1534   else if (scratch_reg
1535            && CONSTANT_P (operand1)
1536            && fp_reg_operand (operand0, mode))
1537     {
1538       rtx const_mem, xoperands[2];
1539
1540       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
1541          it in WORD_MODE regardless of what mode it was originally given
1542          to us.  */
1543       scratch_reg = force_mode (word_mode, scratch_reg);
1544
1545       /* Force the constant into memory and put the address of the
1546          memory location into scratch_reg.  */
1547       const_mem = force_const_mem (mode, operand1);
1548       xoperands[0] = scratch_reg;
1549       xoperands[1] = XEXP (const_mem, 0);
1550       emit_move_sequence (xoperands, Pmode, 0);
1551
1552       /* Now load the destination register.  */
1553       emit_insn (gen_rtx_SET (mode, operand0,
1554                               replace_equiv_address (const_mem, scratch_reg)));
1555       return 1;
1556     }
1557   /* Handle secondary reloads for SAR.  These occur when trying to load
1558      the SAR from memory, FP register, or with a constant.  */
1559   else if (scratch_reg
1560            && GET_CODE (operand0) == REG
1561            && REGNO (operand0) < FIRST_PSEUDO_REGISTER
1562            && REGNO_REG_CLASS (REGNO (operand0)) == SHIFT_REGS
1563            && (GET_CODE (operand1) == MEM
1564                || GET_CODE (operand1) == CONST_INT
1565                || (GET_CODE (operand1) == REG
1566                    && FP_REG_CLASS_P (REGNO_REG_CLASS (REGNO (operand1))))))
1567     {
1568       /* D might not fit in 14 bits either; for such cases load D into
1569          scratch reg.  */
1570       if (GET_CODE (operand1) == MEM
1571           && !memory_address_p (Pmode, XEXP (operand1, 0)))
1572         {
1573           /* We are reloading the address into the scratch register, so we
1574              want to make sure the scratch register is a full register.  */
1575           scratch_reg = force_mode (word_mode, scratch_reg);
1576
1577           emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
1578           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1,
1579                                                                         0)),
1580                                                        Pmode,
1581                                                        XEXP (XEXP (operand1, 0),
1582                                                        0),
1583                                                        scratch_reg));
1584
1585           /* Now we are going to load the scratch register from memory,
1586              we want to load it in the same width as the original MEM,
1587              which must be the same as the width of the ultimate destination,
1588              OPERAND0.  */
1589           scratch_reg = force_mode (GET_MODE (operand0), scratch_reg);
1590
1591           emit_move_insn (scratch_reg,
1592                           replace_equiv_address (operand1, scratch_reg));
1593         }
1594       else
1595         {
1596           /* We want to load the scratch register using the same mode as
1597              the ultimate destination.  */
1598           scratch_reg = force_mode (GET_MODE (operand0), scratch_reg);
1599
1600           emit_move_insn (scratch_reg, operand1);
1601         }
1602
1603       /* And emit the insn to set the ultimate destination.  We know that
1604          the scratch register has the same mode as the destination at this
1605          point.  */
1606       emit_move_insn (operand0, scratch_reg);
1607       return 1;
1608     }
1609   /* Handle the most common case: storing into a register.  */
1610   else if (register_operand (operand0, mode))
1611     {
1612       if (register_operand (operand1, mode)
1613           || (GET_CODE (operand1) == CONST_INT
1614               && cint_ok_for_move (INTVAL (operand1)))
1615           || (operand1 == CONST0_RTX (mode))
1616           || (GET_CODE (operand1) == HIGH
1617               && !symbolic_operand (XEXP (operand1, 0), VOIDmode))
1618           /* Only `general_operands' can come here, so MEM is ok.  */
1619           || GET_CODE (operand1) == MEM)
1620         {
1621           /* Various sets are created during RTL generation which don't
1622              have the REG_POINTER flag correctly set.  After the CSE pass,
1623              instruction recognition can fail if we don't consistently
1624              set this flag when performing register copies.  This should
1625              also improve the opportunities for creating insns that use
1626              unscaled indexing.  */
1627           if (REG_P (operand0) && REG_P (operand1))
1628             {
1629               if (REG_POINTER (operand1)
1630                   && !REG_POINTER (operand0)
1631                   && !HARD_REGISTER_P (operand0))
1632                 copy_reg_pointer (operand0, operand1);
1633               else if (REG_POINTER (operand0)
1634                        && !REG_POINTER (operand1)
1635                        && !HARD_REGISTER_P (operand1))
1636                 copy_reg_pointer (operand1, operand0);
1637             }
1638           
1639           /* When MEMs are broken out, the REG_POINTER flag doesn't
1640              get set.  In some cases, we can set the REG_POINTER flag
1641              from the declaration for the MEM.  */
1642           if (REG_P (operand0)
1643               && GET_CODE (operand1) == MEM
1644               && !REG_POINTER (operand0))
1645             {
1646               tree decl = MEM_EXPR (operand1);
1647
1648               /* Set the register pointer flag and register alignment
1649                  if the declaration for this memory reference is a
1650                  pointer type.  Fortran indirect argument references
1651                  are ignored.  */
1652               if (decl
1653                   && !(flag_argument_noalias > 1
1654                        && TREE_CODE (decl) == INDIRECT_REF
1655                        && TREE_CODE (TREE_OPERAND (decl, 0)) == PARM_DECL))
1656                 {
1657                   tree type;
1658
1659                   /* If this is a COMPONENT_REF, use the FIELD_DECL from
1660                      tree operand 1.  */
1661                   if (TREE_CODE (decl) == COMPONENT_REF)
1662                     decl = TREE_OPERAND (decl, 1);
1663
1664                   type = TREE_TYPE (decl);
1665                   if (TREE_CODE (type) == ARRAY_TYPE)
1666                     type = get_inner_array_type (type);
1667
1668                   if (POINTER_TYPE_P (type))
1669                     {
1670                       int align;
1671
1672                       type = TREE_TYPE (type);
1673                       /* Using TYPE_ALIGN_OK is rather conservative as
1674                          only the ada frontend actually sets it.  */
1675                       align = (TYPE_ALIGN_OK (type) ? TYPE_ALIGN (type)
1676                                : BITS_PER_UNIT);
1677                       mark_reg_pointer (operand0, align);
1678                     }
1679                 }
1680             }
1681
1682           emit_insn (gen_rtx_SET (VOIDmode, operand0, operand1));
1683           return 1;
1684         }
1685     }
1686   else if (GET_CODE (operand0) == MEM)
1687     {
1688       if (mode == DFmode && operand1 == CONST0_RTX (mode)
1689           && !(reload_in_progress || reload_completed))
1690         {
1691           rtx temp = gen_reg_rtx (DFmode);
1692
1693           emit_insn (gen_rtx_SET (VOIDmode, temp, operand1));
1694           emit_insn (gen_rtx_SET (VOIDmode, operand0, temp));
1695           return 1;
1696         }
1697       if (register_operand (operand1, mode) || operand1 == CONST0_RTX (mode))
1698         {
1699           /* Run this case quickly.  */
1700           emit_insn (gen_rtx_SET (VOIDmode, operand0, operand1));
1701           return 1;
1702         }
1703       if (! (reload_in_progress || reload_completed))
1704         {
1705           operands[0] = validize_mem (operand0);
1706           operands[1] = operand1 = force_reg (mode, operand1);
1707         }
1708     }
1709
1710   /* Simplify the source if we need to.
1711      Note we do have to handle function labels here, even though we do
1712      not consider them legitimate constants.  Loop optimizations can
1713      call the emit_move_xxx with one as a source.  */
1714   if ((GET_CODE (operand1) != HIGH && immediate_operand (operand1, mode))
1715       || function_label_operand (operand1, mode)
1716       || (GET_CODE (operand1) == HIGH
1717           && symbolic_operand (XEXP (operand1, 0), mode)))
1718     {
1719       int ishighonly = 0;
1720
1721       if (GET_CODE (operand1) == HIGH)
1722         {
1723           ishighonly = 1;
1724           operand1 = XEXP (operand1, 0);
1725         }
1726       if (symbolic_operand (operand1, mode))
1727         {
1728           /* Argh.  The assembler and linker can't handle arithmetic
1729              involving plabels.
1730
1731              So we force the plabel into memory, load operand0 from
1732              the memory location, then add in the constant part.  */
1733           if ((GET_CODE (operand1) == CONST
1734                && GET_CODE (XEXP (operand1, 0)) == PLUS
1735                && function_label_operand (XEXP (XEXP (operand1, 0), 0), Pmode))
1736               || function_label_operand (operand1, mode))
1737             {
1738               rtx temp, const_part;
1739
1740               /* Figure out what (if any) scratch register to use.  */
1741               if (reload_in_progress || reload_completed)
1742                 {
1743                   scratch_reg = scratch_reg ? scratch_reg : operand0;
1744                   /* SCRATCH_REG will hold an address and maybe the actual
1745                      data.  We want it in WORD_MODE regardless of what mode it
1746                      was originally given to us.  */
1747                   scratch_reg = force_mode (word_mode, scratch_reg);
1748                 }
1749               else if (flag_pic)
1750                 scratch_reg = gen_reg_rtx (Pmode);
1751
1752               if (GET_CODE (operand1) == CONST)
1753                 {
1754                   /* Save away the constant part of the expression.  */
1755                   const_part = XEXP (XEXP (operand1, 0), 1);
1756                   gcc_assert (GET_CODE (const_part) == CONST_INT);
1757
1758                   /* Force the function label into memory.  */
1759                   temp = force_const_mem (mode, XEXP (XEXP (operand1, 0), 0));
1760                 }
1761               else
1762                 {
1763                   /* No constant part.  */
1764                   const_part = NULL_RTX;
1765
1766                   /* Force the function label into memory.  */
1767                   temp = force_const_mem (mode, operand1);
1768                 }
1769
1770
1771               /* Get the address of the memory location.  PIC-ify it if
1772                  necessary.  */
1773               temp = XEXP (temp, 0);
1774               if (flag_pic)
1775                 temp = legitimize_pic_address (temp, mode, scratch_reg);
1776
1777               /* Put the address of the memory location into our destination
1778                  register.  */
1779               operands[1] = temp;
1780               emit_move_sequence (operands, mode, scratch_reg);
1781
1782               /* Now load from the memory location into our destination
1783                  register.  */
1784               operands[1] = gen_rtx_MEM (Pmode, operands[0]);
1785               emit_move_sequence (operands, mode, scratch_reg);
1786
1787               /* And add back in the constant part.  */
1788               if (const_part != NULL_RTX)
1789                 expand_inc (operand0, const_part);
1790
1791               return 1;
1792             }
1793
1794           if (flag_pic)
1795             {
1796               rtx temp;
1797
1798               if (reload_in_progress || reload_completed)
1799                 {
1800                   temp = scratch_reg ? scratch_reg : operand0;
1801                   /* TEMP will hold an address and maybe the actual
1802                      data.  We want it in WORD_MODE regardless of what mode it
1803                      was originally given to us.  */
1804                   temp = force_mode (word_mode, temp);
1805                 }
1806               else
1807                 temp = gen_reg_rtx (Pmode);
1808
1809               /* (const (plus (symbol) (const_int))) must be forced to
1810                  memory during/after reload if the const_int will not fit
1811                  in 14 bits.  */
1812               if (GET_CODE (operand1) == CONST
1813                        && GET_CODE (XEXP (operand1, 0)) == PLUS
1814                        && GET_CODE (XEXP (XEXP (operand1, 0), 1)) == CONST_INT
1815                        && !INT_14_BITS (XEXP (XEXP (operand1, 0), 1))
1816                        && (reload_completed || reload_in_progress)
1817                        && flag_pic)
1818                 {
1819                   rtx const_mem = force_const_mem (mode, operand1);
1820                   operands[1] = legitimize_pic_address (XEXP (const_mem, 0),
1821                                                         mode, temp);
1822                   operands[1] = replace_equiv_address (const_mem, operands[1]);
1823                   emit_move_sequence (operands, mode, temp);
1824                 }
1825               else
1826                 {
1827                   operands[1] = legitimize_pic_address (operand1, mode, temp);
1828                   if (REG_P (operand0) && REG_P (operands[1]))
1829                     copy_reg_pointer (operand0, operands[1]);
1830                   emit_insn (gen_rtx_SET (VOIDmode, operand0, operands[1]));
1831                 }
1832             }
1833           /* On the HPPA, references to data space are supposed to use dp,
1834              register 27, but showing it in the RTL inhibits various cse
1835              and loop optimizations.  */
1836           else
1837             {
1838               rtx temp, set;
1839
1840               if (reload_in_progress || reload_completed)
1841                 {
1842                   temp = scratch_reg ? scratch_reg : operand0;
1843                   /* TEMP will hold an address and maybe the actual
1844                      data.  We want it in WORD_MODE regardless of what mode it
1845                      was originally given to us.  */
1846                   temp = force_mode (word_mode, temp);
1847                 }
1848               else
1849                 temp = gen_reg_rtx (mode);
1850
1851               /* Loading a SYMBOL_REF into a register makes that register
1852                  safe to be used as the base in an indexed address.
1853
1854                  Don't mark hard registers though.  That loses.  */
1855               if (GET_CODE (operand0) == REG
1856                   && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
1857                 mark_reg_pointer (operand0, BITS_PER_UNIT);
1858               if (REGNO (temp) >= FIRST_PSEUDO_REGISTER)
1859                 mark_reg_pointer (temp, BITS_PER_UNIT);
1860
1861               if (ishighonly)
1862                 set = gen_rtx_SET (mode, operand0, temp);
1863               else
1864                 set = gen_rtx_SET (VOIDmode,
1865                                    operand0,
1866                                    gen_rtx_LO_SUM (mode, temp, operand1));
1867
1868               emit_insn (gen_rtx_SET (VOIDmode,
1869                                       temp,
1870                                       gen_rtx_HIGH (mode, operand1)));
1871               emit_insn (set);
1872
1873             }
1874           return 1;
1875         }
1876       else if (pa_tls_referenced_p (operand1))
1877         {
1878           rtx tmp = operand1;
1879           rtx addend = NULL;
1880
1881           if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
1882             {
1883               addend = XEXP (XEXP (tmp, 0), 1);
1884               tmp = XEXP (XEXP (tmp, 0), 0);
1885             }
1886
1887           gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
1888           tmp = legitimize_tls_address (tmp);
1889           if (addend)
1890             {
1891               tmp = gen_rtx_PLUS (mode, tmp, addend);
1892               tmp = force_operand (tmp, operands[0]);
1893             }
1894           operands[1] = tmp;
1895         }
1896       else if (GET_CODE (operand1) != CONST_INT
1897                || !cint_ok_for_move (INTVAL (operand1)))
1898         {
1899           rtx insn, temp;
1900           rtx op1 = operand1;
1901           HOST_WIDE_INT value = 0;
1902           HOST_WIDE_INT insv = 0;
1903           int insert = 0;
1904
1905           if (GET_CODE (operand1) == CONST_INT)
1906             value = INTVAL (operand1);
1907
1908           if (TARGET_64BIT
1909               && GET_CODE (operand1) == CONST_INT
1910               && HOST_BITS_PER_WIDE_INT > 32
1911               && GET_MODE_BITSIZE (GET_MODE (operand0)) > 32)
1912             {
1913               HOST_WIDE_INT nval;
1914
1915               /* Extract the low order 32 bits of the value and sign extend.
1916                  If the new value is the same as the original value, we can
1917                  can use the original value as-is.  If the new value is
1918                  different, we use it and insert the most-significant 32-bits
1919                  of the original value into the final result.  */
1920               nval = ((value & (((HOST_WIDE_INT) 2 << 31) - 1))
1921                       ^ ((HOST_WIDE_INT) 1 << 31)) - ((HOST_WIDE_INT) 1 << 31);
1922               if (value != nval)
1923                 {
1924 #if HOST_BITS_PER_WIDE_INT > 32
1925                   insv = value >= 0 ? value >> 32 : ~(~value >> 32);
1926 #endif
1927                   insert = 1;
1928                   value = nval;
1929                   operand1 = GEN_INT (nval);
1930                 }
1931             }
1932
1933           if (reload_in_progress || reload_completed)
1934             temp = scratch_reg ? scratch_reg : operand0;
1935           else
1936             temp = gen_reg_rtx (mode);
1937
1938           /* We don't directly split DImode constants on 32-bit targets
1939              because PLUS uses an 11-bit immediate and the insn sequence
1940              generated is not as efficient as the one using HIGH/LO_SUM.  */
1941           if (GET_CODE (operand1) == CONST_INT
1942               && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD
1943               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1944               && !insert)
1945             {
1946               /* Directly break constant into high and low parts.  This
1947                  provides better optimization opportunities because various
1948                  passes recognize constants split with PLUS but not LO_SUM.
1949                  We use a 14-bit signed low part except when the addition
1950                  of 0x4000 to the high part might change the sign of the
1951                  high part.  */
1952               HOST_WIDE_INT low = value & 0x3fff;
1953               HOST_WIDE_INT high = value & ~ 0x3fff;
1954
1955               if (low >= 0x2000)
1956                 {
1957                   if (high == 0x7fffc000 || (mode == HImode && high == 0x4000))
1958                     high += 0x2000;
1959                   else
1960                     high += 0x4000;
1961                 }
1962
1963               low = value - high;
1964
1965               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (high)));
1966               operands[1] = gen_rtx_PLUS (mode, temp, GEN_INT (low));
1967             }
1968           else
1969             {
1970               emit_insn (gen_rtx_SET (VOIDmode, temp,
1971                                       gen_rtx_HIGH (mode, operand1)));
1972               operands[1] = gen_rtx_LO_SUM (mode, temp, operand1);
1973             }
1974
1975           insn = emit_move_insn (operands[0], operands[1]);
1976
1977           /* Now insert the most significant 32 bits of the value
1978              into the register.  When we don't have a second register
1979              available, it could take up to nine instructions to load
1980              a 64-bit integer constant.  Prior to reload, we force
1981              constants that would take more than three instructions
1982              to load to the constant pool.  During and after reload,
1983              we have to handle all possible values.  */
1984           if (insert)
1985             {
1986               /* Use a HIGH/LO_SUM/INSV sequence if we have a second
1987                  register and the value to be inserted is outside the
1988                  range that can be loaded with three depdi instructions.  */
1989               if (temp != operand0 && (insv >= 16384 || insv < -16384))
1990                 {
1991                   operand1 = GEN_INT (insv);
1992
1993                   emit_insn (gen_rtx_SET (VOIDmode, temp,
1994                                           gen_rtx_HIGH (mode, operand1)));
1995                   emit_move_insn (temp, gen_rtx_LO_SUM (mode, temp, operand1));
1996                   emit_insn (gen_insv (operand0, GEN_INT (32),
1997                                        const0_rtx, temp));
1998                 }
1999               else
2000                 {
2001                   int len = 5, pos = 27;
2002
2003                   /* Insert the bits using the depdi instruction.  */
2004                   while (pos >= 0)
2005                     {
2006                       HOST_WIDE_INT v5 = ((insv & 31) ^ 16) - 16;
2007                       HOST_WIDE_INT sign = v5 < 0;
2008
2009                       /* Left extend the insertion.  */
2010                       insv = (insv >= 0 ? insv >> len : ~(~insv >> len));
2011                       while (pos > 0 && (insv & 1) == sign)
2012                         {
2013                           insv = (insv >= 0 ? insv >> 1 : ~(~insv >> 1));
2014                           len += 1;
2015                           pos -= 1;
2016                         }
2017
2018                       emit_insn (gen_insv (operand0, GEN_INT (len),
2019                                            GEN_INT (pos), GEN_INT (v5)));
2020
2021                       len = pos > 0 && pos < 5 ? pos : 5;
2022                       pos -= len;
2023                     }
2024                 }
2025             }
2026
2027           set_unique_reg_note (insn, REG_EQUAL, op1);
2028
2029           return 1;
2030         }
2031     }
2032   /* Now have insn-emit do whatever it normally does.  */
2033   return 0;
2034 }
2035
2036 /* Examine EXP and return nonzero if it contains an ADDR_EXPR (meaning
2037    it will need a link/runtime reloc).  */
2038
2039 int
2040 reloc_needed (tree exp)
2041 {
2042   int reloc = 0;
2043
2044   switch (TREE_CODE (exp))
2045     {
2046     case ADDR_EXPR:
2047       return 1;
2048
2049     case POINTER_PLUS_EXPR:
2050     case PLUS_EXPR:
2051     case MINUS_EXPR:
2052       reloc = reloc_needed (TREE_OPERAND (exp, 0));
2053       reloc |= reloc_needed (TREE_OPERAND (exp, 1));
2054       break;
2055
2056     case NOP_EXPR:
2057     case CONVERT_EXPR:
2058     case NON_LVALUE_EXPR:
2059       reloc = reloc_needed (TREE_OPERAND (exp, 0));
2060       break;
2061
2062     case CONSTRUCTOR:
2063       {
2064         tree value;
2065         unsigned HOST_WIDE_INT ix;
2066
2067         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), ix, value)
2068           if (value)
2069             reloc |= reloc_needed (value);
2070       }
2071       break;
2072
2073     case ERROR_MARK:
2074       break;
2075
2076     default:
2077       break;
2078     }
2079   return reloc;
2080 }
2081
2082 /* Does operand (which is a symbolic_operand) live in text space?
2083    If so, SYMBOL_REF_FLAG, which is set by pa_encode_section_info,
2084    will be true.  */
2085
2086 int
2087 read_only_operand (rtx operand, enum machine_mode mode ATTRIBUTE_UNUSED)
2088 {
2089   if (GET_CODE (operand) == CONST)
2090     operand = XEXP (XEXP (operand, 0), 0);
2091   if (flag_pic)
2092     {
2093       if (GET_CODE (operand) == SYMBOL_REF)
2094         return SYMBOL_REF_FLAG (operand) && !CONSTANT_POOL_ADDRESS_P (operand);
2095     }
2096   else
2097     {
2098       if (GET_CODE (operand) == SYMBOL_REF)
2099         return SYMBOL_REF_FLAG (operand) || CONSTANT_POOL_ADDRESS_P (operand);
2100     }
2101   return 1;
2102 }
2103
2104 \f
2105 /* Return the best assembler insn template
2106    for moving operands[1] into operands[0] as a fullword.  */
2107 const char *
2108 singlemove_string (rtx *operands)
2109 {
2110   HOST_WIDE_INT intval;
2111
2112   if (GET_CODE (operands[0]) == MEM)
2113     return "stw %r1,%0";
2114   if (GET_CODE (operands[1]) == MEM)
2115     return "ldw %1,%0";
2116   if (GET_CODE (operands[1]) == CONST_DOUBLE)
2117     {
2118       long i;
2119       REAL_VALUE_TYPE d;
2120
2121       gcc_assert (GET_MODE (operands[1]) == SFmode);
2122
2123       /* Translate the CONST_DOUBLE to a CONST_INT with the same target
2124          bit pattern.  */
2125       REAL_VALUE_FROM_CONST_DOUBLE (d, operands[1]);
2126       REAL_VALUE_TO_TARGET_SINGLE (d, i);
2127
2128       operands[1] = GEN_INT (i);
2129       /* Fall through to CONST_INT case.  */
2130     }
2131   if (GET_CODE (operands[1]) == CONST_INT)
2132     {
2133       intval = INTVAL (operands[1]);
2134
2135       if (VAL_14_BITS_P (intval))
2136         return "ldi %1,%0";
2137       else if ((intval & 0x7ff) == 0)
2138         return "ldil L'%1,%0";
2139       else if (zdepi_cint_p (intval))
2140         return "{zdepi %Z1,%0|depwi,z %Z1,%0}";
2141       else
2142         return "ldil L'%1,%0\n\tldo R'%1(%0),%0";
2143     }
2144   return "copy %1,%0";
2145 }
2146 \f
2147
2148 /* Compute position (in OP[1]) and width (in OP[2])
2149    useful for copying IMM to a register using the zdepi
2150    instructions.  Store the immediate value to insert in OP[0].  */
2151 static void
2152 compute_zdepwi_operands (unsigned HOST_WIDE_INT imm, unsigned *op)
2153 {
2154   int lsb, len;
2155
2156   /* Find the least significant set bit in IMM.  */
2157   for (lsb = 0; lsb < 32; lsb++)
2158     {
2159       if ((imm & 1) != 0)
2160         break;
2161       imm >>= 1;
2162     }
2163
2164   /* Choose variants based on *sign* of the 5-bit field.  */
2165   if ((imm & 0x10) == 0)
2166     len = (lsb <= 28) ? 4 : 32 - lsb;
2167   else
2168     {
2169       /* Find the width of the bitstring in IMM.  */
2170       for (len = 5; len < 32; len++)
2171         {
2172           if ((imm & (1 << len)) == 0)
2173             break;
2174         }
2175
2176       /* Sign extend IMM as a 5-bit value.  */
2177       imm = (imm & 0xf) - 0x10;
2178     }
2179
2180   op[0] = imm;
2181   op[1] = 31 - lsb;
2182   op[2] = len;
2183 }
2184
2185 /* Compute position (in OP[1]) and width (in OP[2])
2186    useful for copying IMM to a register using the depdi,z
2187    instructions.  Store the immediate value to insert in OP[0].  */
2188 void
2189 compute_zdepdi_operands (unsigned HOST_WIDE_INT imm, unsigned *op)
2190 {
2191   HOST_WIDE_INT lsb, len;
2192
2193   /* Find the least significant set bit in IMM.  */
2194   for (lsb = 0; lsb < HOST_BITS_PER_WIDE_INT; lsb++)
2195     {
2196       if ((imm & 1) != 0)
2197         break;
2198       imm >>= 1;
2199     }
2200
2201   /* Choose variants based on *sign* of the 5-bit field.  */
2202   if ((imm & 0x10) == 0)
2203     len = ((lsb <= HOST_BITS_PER_WIDE_INT - 4)
2204            ? 4 : HOST_BITS_PER_WIDE_INT - lsb);
2205   else
2206     {
2207       /* Find the width of the bitstring in IMM.  */
2208       for (len = 5; len < HOST_BITS_PER_WIDE_INT; len++)
2209         {
2210           if ((imm & ((unsigned HOST_WIDE_INT) 1 << len)) == 0)
2211             break;
2212         }
2213
2214       /* Sign extend IMM as a 5-bit value.  */
2215       imm = (imm & 0xf) - 0x10;
2216     }
2217
2218   op[0] = imm;
2219   op[1] = 63 - lsb;
2220   op[2] = len;
2221 }
2222
2223 /* Output assembler code to perform a doubleword move insn
2224    with operands OPERANDS.  */
2225
2226 const char *
2227 output_move_double (rtx *operands)
2228 {
2229   enum { REGOP, OFFSOP, MEMOP, CNSTOP, RNDOP } optype0, optype1;
2230   rtx latehalf[2];
2231   rtx addreg0 = 0, addreg1 = 0;
2232
2233   /* First classify both operands.  */
2234
2235   if (REG_P (operands[0]))
2236     optype0 = REGOP;
2237   else if (offsettable_memref_p (operands[0]))
2238     optype0 = OFFSOP;
2239   else if (GET_CODE (operands[0]) == MEM)
2240     optype0 = MEMOP;
2241   else
2242     optype0 = RNDOP;
2243
2244   if (REG_P (operands[1]))
2245     optype1 = REGOP;
2246   else if (CONSTANT_P (operands[1]))
2247     optype1 = CNSTOP;
2248   else if (offsettable_memref_p (operands[1]))
2249     optype1 = OFFSOP;
2250   else if (GET_CODE (operands[1]) == MEM)
2251     optype1 = MEMOP;
2252   else
2253     optype1 = RNDOP;
2254
2255   /* Check for the cases that the operand constraints are not
2256      supposed to allow to happen.  */
2257   gcc_assert (optype0 == REGOP || optype1 == REGOP);
2258
2259   /* Handle copies between general and floating registers.  */
2260
2261   if (optype0 == REGOP && optype1 == REGOP
2262       && FP_REG_P (operands[0]) ^ FP_REG_P (operands[1]))
2263     {
2264       if (FP_REG_P (operands[0]))
2265         {
2266           output_asm_insn ("{stws|stw} %1,-16(%%sp)", operands);
2267           output_asm_insn ("{stws|stw} %R1,-12(%%sp)", operands);
2268           return "{fldds|fldd} -16(%%sp),%0";
2269         }
2270       else
2271         {
2272           output_asm_insn ("{fstds|fstd} %1,-16(%%sp)", operands);
2273           output_asm_insn ("{ldws|ldw} -16(%%sp),%0", operands);
2274           return "{ldws|ldw} -12(%%sp),%R0";
2275         }
2276     }
2277
2278    /* Handle auto decrementing and incrementing loads and stores
2279      specifically, since the structure of the function doesn't work
2280      for them without major modification.  Do it better when we learn
2281      this port about the general inc/dec addressing of PA.
2282      (This was written by tege.  Chide him if it doesn't work.)  */
2283
2284   if (optype0 == MEMOP)
2285     {
2286       /* We have to output the address syntax ourselves, since print_operand
2287          doesn't deal with the addresses we want to use.  Fix this later.  */
2288
2289       rtx addr = XEXP (operands[0], 0);
2290       if (GET_CODE (addr) == POST_INC || GET_CODE (addr) == POST_DEC)
2291         {
2292           rtx high_reg = gen_rtx_SUBREG (SImode, operands[1], 0);
2293
2294           operands[0] = XEXP (addr, 0);
2295           gcc_assert (GET_CODE (operands[1]) == REG
2296                       && GET_CODE (operands[0]) == REG);
2297
2298           gcc_assert (!reg_overlap_mentioned_p (high_reg, addr));
2299           
2300           /* No overlap between high target register and address
2301              register.  (We do this in a non-obvious way to
2302              save a register file writeback)  */
2303           if (GET_CODE (addr) == POST_INC)
2304             return "{stws|stw},ma %1,8(%0)\n\tstw %R1,-4(%0)";
2305           return "{stws|stw},ma %1,-8(%0)\n\tstw %R1,12(%0)";
2306         }
2307       else if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
2308         {
2309           rtx high_reg = gen_rtx_SUBREG (SImode, operands[1], 0);
2310
2311           operands[0] = XEXP (addr, 0);
2312           gcc_assert (GET_CODE (operands[1]) == REG
2313                       && GET_CODE (operands[0]) == REG);
2314           
2315           gcc_assert (!reg_overlap_mentioned_p (high_reg, addr));
2316           /* No overlap between high target register and address
2317              register.  (We do this in a non-obvious way to save a
2318              register file writeback)  */
2319           if (GET_CODE (addr) == PRE_INC)
2320             return "{stws|stw},mb %1,8(%0)\n\tstw %R1,4(%0)";
2321           return "{stws|stw},mb %1,-8(%0)\n\tstw %R1,4(%0)";
2322         }
2323     }
2324   if (optype1 == MEMOP)
2325     {
2326       /* We have to output the address syntax ourselves, since print_operand
2327          doesn't deal with the addresses we want to use.  Fix this later.  */
2328
2329       rtx addr = XEXP (operands[1], 0);
2330       if (GET_CODE (addr) == POST_INC || GET_CODE (addr) == POST_DEC)
2331         {
2332           rtx high_reg = gen_rtx_SUBREG (SImode, operands[0], 0);
2333
2334           operands[1] = XEXP (addr, 0);
2335           gcc_assert (GET_CODE (operands[0]) == REG
2336                       && GET_CODE (operands[1]) == REG);
2337
2338           if (!reg_overlap_mentioned_p (high_reg, addr))
2339             {
2340               /* No overlap between high target register and address
2341                  register.  (We do this in a non-obvious way to
2342                  save a register file writeback)  */
2343               if (GET_CODE (addr) == POST_INC)
2344                 return "{ldws|ldw},ma 8(%1),%0\n\tldw -4(%1),%R0";
2345               return "{ldws|ldw},ma -8(%1),%0\n\tldw 12(%1),%R0";
2346             }
2347           else
2348             {
2349               /* This is an undefined situation.  We should load into the
2350                  address register *and* update that register.  Probably
2351                  we don't need to handle this at all.  */
2352               if (GET_CODE (addr) == POST_INC)
2353                 return "ldw 4(%1),%R0\n\t{ldws|ldw},ma 8(%1),%0";
2354               return "ldw 4(%1),%R0\n\t{ldws|ldw},ma -8(%1),%0";
2355             }
2356         }
2357       else if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
2358         {
2359           rtx high_reg = gen_rtx_SUBREG (SImode, operands[0], 0);
2360
2361           operands[1] = XEXP (addr, 0);
2362           gcc_assert (GET_CODE (operands[0]) == REG
2363                       && GET_CODE (operands[1]) == REG);
2364
2365           if (!reg_overlap_mentioned_p (high_reg, addr))
2366             {
2367               /* No overlap between high target register and address
2368                  register.  (We do this in a non-obvious way to
2369                  save a register file writeback)  */
2370               if (GET_CODE (addr) == PRE_INC)
2371                 return "{ldws|ldw},mb 8(%1),%0\n\tldw 4(%1),%R0";
2372               return "{ldws|ldw},mb -8(%1),%0\n\tldw 4(%1),%R0";
2373             }
2374           else
2375             {
2376               /* This is an undefined situation.  We should load into the
2377                  address register *and* update that register.  Probably
2378                  we don't need to handle this at all.  */
2379               if (GET_CODE (addr) == PRE_INC)
2380                 return "ldw 12(%1),%R0\n\t{ldws|ldw},mb 8(%1),%0";
2381               return "ldw -4(%1),%R0\n\t{ldws|ldw},mb -8(%1),%0";
2382             }
2383         }
2384       else if (GET_CODE (addr) == PLUS
2385                && GET_CODE (XEXP (addr, 0)) == MULT)
2386         {
2387           rtx xoperands[4];
2388           rtx high_reg = gen_rtx_SUBREG (SImode, operands[0], 0);
2389
2390           if (!reg_overlap_mentioned_p (high_reg, addr))
2391             {
2392               xoperands[0] = high_reg;
2393               xoperands[1] = XEXP (addr, 1);
2394               xoperands[2] = XEXP (XEXP (addr, 0), 0);
2395               xoperands[3] = XEXP (XEXP (addr, 0), 1);
2396               output_asm_insn ("{sh%O3addl %2,%1,%0|shladd,l %2,%O3,%1,%0}",
2397                                xoperands);
2398               return "ldw 4(%0),%R0\n\tldw 0(%0),%0";
2399             }
2400           else
2401             {
2402               xoperands[0] = high_reg;
2403               xoperands[1] = XEXP (addr, 1);
2404               xoperands[2] = XEXP (XEXP (addr, 0), 0);
2405               xoperands[3] = XEXP (XEXP (addr, 0), 1);
2406               output_asm_insn ("{sh%O3addl %2,%1,%R0|shladd,l %2,%O3,%1,%R0}",
2407                                xoperands);
2408               return "ldw 0(%R0),%0\n\tldw 4(%R0),%R0";
2409             }
2410         }
2411     }
2412
2413   /* If an operand is an unoffsettable memory ref, find a register
2414      we can increment temporarily to make it refer to the second word.  */
2415
2416   if (optype0 == MEMOP)
2417     addreg0 = find_addr_reg (XEXP (operands[0], 0));
2418
2419   if (optype1 == MEMOP)
2420     addreg1 = find_addr_reg (XEXP (operands[1], 0));
2421
2422   /* Ok, we can do one word at a time.
2423      Normally we do the low-numbered word first.
2424
2425      In either case, set up in LATEHALF the operands to use
2426      for the high-numbered word and in some cases alter the
2427      operands in OPERANDS to be suitable for the low-numbered word.  */
2428
2429   if (optype0 == REGOP)
2430     latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2431   else if (optype0 == OFFSOP)
2432     latehalf[0] = adjust_address (operands[0], SImode, 4);
2433   else
2434     latehalf[0] = operands[0];
2435
2436   if (optype1 == REGOP)
2437     latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2438   else if (optype1 == OFFSOP)
2439     latehalf[1] = adjust_address (operands[1], SImode, 4);
2440   else if (optype1 == CNSTOP)
2441     split_double (operands[1], &operands[1], &latehalf[1]);
2442   else
2443     latehalf[1] = operands[1];
2444
2445   /* If the first move would clobber the source of the second one,
2446      do them in the other order.
2447
2448      This can happen in two cases:
2449
2450         mem -> register where the first half of the destination register
2451         is the same register used in the memory's address.  Reload
2452         can create such insns.
2453
2454         mem in this case will be either register indirect or register
2455         indirect plus a valid offset.
2456
2457         register -> register move where REGNO(dst) == REGNO(src + 1)
2458         someone (Tim/Tege?) claimed this can happen for parameter loads.
2459
2460      Handle mem -> register case first.  */
2461   if (optype0 == REGOP
2462       && (optype1 == MEMOP || optype1 == OFFSOP)
2463       && refers_to_regno_p (REGNO (operands[0]), REGNO (operands[0]) + 1,
2464                             operands[1], 0))
2465     {
2466       /* Do the late half first.  */
2467       if (addreg1)
2468         output_asm_insn ("ldo 4(%0),%0", &addreg1);
2469       output_asm_insn (singlemove_string (latehalf), latehalf);
2470
2471       /* Then clobber.  */
2472       if (addreg1)
2473         output_asm_insn ("ldo -4(%0),%0", &addreg1);
2474       return singlemove_string (operands);
2475     }
2476
2477   /* Now handle register -> register case.  */
2478   if (optype0 == REGOP && optype1 == REGOP
2479       && REGNO (operands[0]) == REGNO (operands[1]) + 1)
2480     {
2481       output_asm_insn (singlemove_string (latehalf), latehalf);
2482       return singlemove_string (operands);
2483     }
2484
2485   /* Normal case: do the two words, low-numbered first.  */
2486
2487   output_asm_insn (singlemove_string (operands), operands);
2488
2489   /* Make any unoffsettable addresses point at high-numbered word.  */
2490   if (addreg0)
2491     output_asm_insn ("ldo 4(%0),%0", &addreg0);
2492   if (addreg1)
2493     output_asm_insn ("ldo 4(%0),%0", &addreg1);
2494
2495   /* Do that word.  */
2496   output_asm_insn (singlemove_string (latehalf), latehalf);
2497
2498   /* Undo the adds we just did.  */
2499   if (addreg0)
2500     output_asm_insn ("ldo -4(%0),%0", &addreg0);
2501   if (addreg1)
2502     output_asm_insn ("ldo -4(%0),%0", &addreg1);
2503
2504   return "";
2505 }
2506 \f
2507 const char *
2508 output_fp_move_double (rtx *operands)
2509 {
2510   if (FP_REG_P (operands[0]))
2511     {
2512       if (FP_REG_P (operands[1])
2513           || operands[1] == CONST0_RTX (GET_MODE (operands[0])))
2514         output_asm_insn ("fcpy,dbl %f1,%0", operands);
2515       else
2516         output_asm_insn ("fldd%F1 %1,%0", operands);
2517     }
2518   else if (FP_REG_P (operands[1]))
2519     {
2520       output_asm_insn ("fstd%F0 %1,%0", operands);
2521     }
2522   else
2523     {
2524       rtx xoperands[2];
2525       
2526       gcc_assert (operands[1] == CONST0_RTX (GET_MODE (operands[0])));
2527       
2528       /* This is a pain.  You have to be prepared to deal with an
2529          arbitrary address here including pre/post increment/decrement.
2530
2531          so avoid this in the MD.  */
2532       gcc_assert (GET_CODE (operands[0]) == REG);
2533       
2534       xoperands[1] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2535       xoperands[0] = operands[0];
2536       output_asm_insn ("copy %%r0,%0\n\tcopy %%r0,%1", xoperands);
2537     }
2538   return "";
2539 }
2540 \f
2541 /* Return a REG that occurs in ADDR with coefficient 1.
2542    ADDR can be effectively incremented by incrementing REG.  */
2543
2544 static rtx
2545 find_addr_reg (rtx addr)
2546 {
2547   while (GET_CODE (addr) == PLUS)
2548     {
2549       if (GET_CODE (XEXP (addr, 0)) == REG)
2550         addr = XEXP (addr, 0);
2551       else if (GET_CODE (XEXP (addr, 1)) == REG)
2552         addr = XEXP (addr, 1);
2553       else if (CONSTANT_P (XEXP (addr, 0)))
2554         addr = XEXP (addr, 1);
2555       else if (CONSTANT_P (XEXP (addr, 1)))
2556         addr = XEXP (addr, 0);
2557       else
2558         gcc_unreachable ();
2559     }
2560   gcc_assert (GET_CODE (addr) == REG);
2561   return addr;
2562 }
2563
2564 /* Emit code to perform a block move.
2565
2566    OPERANDS[0] is the destination pointer as a REG, clobbered.
2567    OPERANDS[1] is the source pointer as a REG, clobbered.
2568    OPERANDS[2] is a register for temporary storage.
2569    OPERANDS[3] is a register for temporary storage.
2570    OPERANDS[4] is the size as a CONST_INT
2571    OPERANDS[5] is the alignment safe to use, as a CONST_INT.
2572    OPERANDS[6] is another temporary register.  */
2573
2574 const char *
2575 output_block_move (rtx *operands, int size_is_constant ATTRIBUTE_UNUSED)
2576 {
2577   int align = INTVAL (operands[5]);
2578   unsigned long n_bytes = INTVAL (operands[4]);
2579
2580   /* We can't move more than a word at a time because the PA
2581      has no longer integer move insns.  (Could use fp mem ops?)  */
2582   if (align > (TARGET_64BIT ? 8 : 4))
2583     align = (TARGET_64BIT ? 8 : 4);
2584
2585   /* Note that we know each loop below will execute at least twice
2586      (else we would have open-coded the copy).  */
2587   switch (align)
2588     {
2589       case 8:
2590         /* Pre-adjust the loop counter.  */
2591         operands[4] = GEN_INT (n_bytes - 16);
2592         output_asm_insn ("ldi %4,%2", operands);
2593
2594         /* Copying loop.  */
2595         output_asm_insn ("ldd,ma 8(%1),%3", operands);
2596         output_asm_insn ("ldd,ma 8(%1),%6", operands);
2597         output_asm_insn ("std,ma %3,8(%0)", operands);
2598         output_asm_insn ("addib,>= -16,%2,.-12", operands);
2599         output_asm_insn ("std,ma %6,8(%0)", operands);
2600
2601         /* Handle the residual.  There could be up to 7 bytes of
2602            residual to copy!  */
2603         if (n_bytes % 16 != 0)
2604           {
2605             operands[4] = GEN_INT (n_bytes % 8);
2606             if (n_bytes % 16 >= 8)
2607               output_asm_insn ("ldd,ma 8(%1),%3", operands);
2608             if (n_bytes % 8 != 0)
2609               output_asm_insn ("ldd 0(%1),%6", operands);
2610             if (n_bytes % 16 >= 8)
2611               output_asm_insn ("std,ma %3,8(%0)", operands);
2612             if (n_bytes % 8 != 0)
2613               output_asm_insn ("stdby,e %6,%4(%0)", operands);
2614           }
2615         return "";
2616
2617       case 4:
2618         /* Pre-adjust the loop counter.  */
2619         operands[4] = GEN_INT (n_bytes - 8);
2620         output_asm_insn ("ldi %4,%2", operands);
2621
2622         /* Copying loop.  */
2623         output_asm_insn ("{ldws|ldw},ma 4(%1),%3", operands);
2624         output_asm_insn ("{ldws|ldw},ma 4(%1),%6", operands);
2625         output_asm_insn ("{stws|stw},ma %3,4(%0)", operands);
2626         output_asm_insn ("addib,>= -8,%2,.-12", operands);
2627         output_asm_insn ("{stws|stw},ma %6,4(%0)", operands);
2628
2629         /* Handle the residual.  There could be up to 7 bytes of
2630            residual to copy!  */
2631         if (n_bytes % 8 != 0)
2632           {
2633             operands[4] = GEN_INT (n_bytes % 4);
2634             if (n_bytes % 8 >= 4)
2635               output_asm_insn ("{ldws|ldw},ma 4(%1),%3", operands);
2636             if (n_bytes % 4 != 0)
2637               output_asm_insn ("ldw 0(%1),%6", operands);
2638             if (n_bytes % 8 >= 4)
2639               output_asm_insn ("{stws|stw},ma %3,4(%0)", operands);
2640             if (n_bytes % 4 != 0)
2641               output_asm_insn ("{stbys|stby},e %6,%4(%0)", operands);
2642           }
2643         return "";
2644
2645       case 2:
2646         /* Pre-adjust the loop counter.  */
2647         operands[4] = GEN_INT (n_bytes - 4);
2648         output_asm_insn ("ldi %4,%2", operands);
2649
2650         /* Copying loop.  */
2651         output_asm_insn ("{ldhs|ldh},ma 2(%1),%3", operands);
2652         output_asm_insn ("{ldhs|ldh},ma 2(%1),%6", operands);
2653         output_asm_insn ("{sths|sth},ma %3,2(%0)", operands);
2654         output_asm_insn ("addib,>= -4,%2,.-12", operands);
2655         output_asm_insn ("{sths|sth},ma %6,2(%0)", operands);
2656
2657         /* Handle the residual.  */
2658         if (n_bytes % 4 != 0)
2659           {
2660             if (n_bytes % 4 >= 2)
2661               output_asm_insn ("{ldhs|ldh},ma 2(%1),%3", operands);
2662             if (n_bytes % 2 != 0)
2663               output_asm_insn ("ldb 0(%1),%6", operands);
2664             if (n_bytes % 4 >= 2)
2665               output_asm_insn ("{sths|sth},ma %3,2(%0)", operands);
2666             if (n_bytes % 2 != 0)
2667               output_asm_insn ("stb %6,0(%0)", operands);
2668           }
2669         return "";
2670
2671       case 1:
2672         /* Pre-adjust the loop counter.  */
2673         operands[4] = GEN_INT (n_bytes - 2);
2674         output_asm_insn ("ldi %4,%2", operands);
2675
2676         /* Copying loop.  */
2677         output_asm_insn ("{ldbs|ldb},ma 1(%1),%3", operands);
2678         output_asm_insn ("{ldbs|ldb},ma 1(%1),%6", operands);
2679         output_asm_insn ("{stbs|stb},ma %3,1(%0)", operands);
2680         output_asm_insn ("addib,>= -2,%2,.-12", operands);
2681         output_asm_insn ("{stbs|stb},ma %6,1(%0)", operands);
2682
2683         /* Handle the residual.  */
2684         if (n_bytes % 2 != 0)
2685           {
2686             output_asm_insn ("ldb 0(%1),%3", operands);
2687             output_asm_insn ("stb %3,0(%0)", operands);
2688           }
2689         return "";
2690
2691       default:
2692         gcc_unreachable ();
2693     }
2694 }
2695
2696 /* Count the number of insns necessary to handle this block move.
2697
2698    Basic structure is the same as emit_block_move, except that we
2699    count insns rather than emit them.  */
2700
2701 static int
2702 compute_movmem_length (rtx insn)
2703 {
2704   rtx pat = PATTERN (insn);
2705   unsigned int align = INTVAL (XEXP (XVECEXP (pat, 0, 7), 0));
2706   unsigned long n_bytes = INTVAL (XEXP (XVECEXP (pat, 0, 6), 0));
2707   unsigned int n_insns = 0;
2708
2709   /* We can't move more than four bytes at a time because the PA
2710      has no longer integer move insns.  (Could use fp mem ops?)  */
2711   if (align > (TARGET_64BIT ? 8 : 4))
2712     align = (TARGET_64BIT ? 8 : 4);
2713
2714   /* The basic copying loop.  */
2715   n_insns = 6;
2716
2717   /* Residuals.  */
2718   if (n_bytes % (2 * align) != 0)
2719     {
2720       if ((n_bytes % (2 * align)) >= align)
2721         n_insns += 2;
2722
2723       if ((n_bytes % align) != 0)
2724         n_insns += 2;
2725     }
2726
2727   /* Lengths are expressed in bytes now; each insn is 4 bytes.  */
2728   return n_insns * 4;
2729 }
2730
2731 /* Emit code to perform a block clear.
2732
2733    OPERANDS[0] is the destination pointer as a REG, clobbered.
2734    OPERANDS[1] is a register for temporary storage.
2735    OPERANDS[2] is the size as a CONST_INT
2736    OPERANDS[3] is the alignment safe to use, as a CONST_INT.  */
2737
2738 const char *
2739 output_block_clear (rtx *operands, int size_is_constant ATTRIBUTE_UNUSED)
2740 {
2741   int align = INTVAL (operands[3]);
2742   unsigned long n_bytes = INTVAL (operands[2]);
2743
2744   /* We can't clear more than a word at a time because the PA
2745      has no longer integer move insns.  */
2746   if (align > (TARGET_64BIT ? 8 : 4))
2747     align = (TARGET_64BIT ? 8 : 4);
2748
2749   /* Note that we know each loop below will execute at least twice
2750      (else we would have open-coded the copy).  */
2751   switch (align)
2752     {
2753       case 8:
2754         /* Pre-adjust the loop counter.  */
2755         operands[2] = GEN_INT (n_bytes - 16);
2756         output_asm_insn ("ldi %2,%1", operands);
2757
2758         /* Loop.  */
2759         output_asm_insn ("std,ma %%r0,8(%0)", operands);
2760         output_asm_insn ("addib,>= -16,%1,.-4", operands);
2761         output_asm_insn ("std,ma %%r0,8(%0)", operands);
2762
2763         /* Handle the residual.  There could be up to 7 bytes of
2764            residual to copy!  */
2765         if (n_bytes % 16 != 0)
2766           {
2767             operands[2] = GEN_INT (n_bytes % 8);
2768             if (n_bytes % 16 >= 8)
2769               output_asm_insn ("std,ma %%r0,8(%0)", operands);
2770             if (n_bytes % 8 != 0)
2771               output_asm_insn ("stdby,e %%r0,%2(%0)", operands);
2772           }
2773         return "";
2774
2775       case 4:
2776         /* Pre-adjust the loop counter.  */
2777         operands[2] = GEN_INT (n_bytes - 8);
2778         output_asm_insn ("ldi %2,%1", operands);
2779
2780         /* Loop.  */
2781         output_asm_insn ("{stws|stw},ma %%r0,4(%0)", operands);
2782         output_asm_insn ("addib,>= -8,%1,.-4", operands);
2783         output_asm_insn ("{stws|stw},ma %%r0,4(%0)", operands);
2784
2785         /* Handle the residual.  There could be up to 7 bytes of
2786            residual to copy!  */
2787         if (n_bytes % 8 != 0)
2788           {
2789             operands[2] = GEN_INT (n_bytes % 4);
2790             if (n_bytes % 8 >= 4)
2791               output_asm_insn ("{stws|stw},ma %%r0,4(%0)", operands);
2792             if (n_bytes % 4 != 0)
2793               output_asm_insn ("{stbys|stby},e %%r0,%2(%0)", operands);
2794           }
2795         return "";
2796
2797       case 2:
2798         /* Pre-adjust the loop counter.  */
2799         operands[2] = GEN_INT (n_bytes - 4);
2800         output_asm_insn ("ldi %2,%1", operands);
2801
2802         /* Loop.  */
2803         output_asm_insn ("{sths|sth},ma %%r0,2(%0)", operands);
2804         output_asm_insn ("addib,>= -4,%1,.-4", operands);
2805         output_asm_insn ("{sths|sth},ma %%r0,2(%0)", operands);
2806
2807         /* Handle the residual.  */
2808         if (n_bytes % 4 != 0)
2809           {
2810             if (n_bytes % 4 >= 2)
2811               output_asm_insn ("{sths|sth},ma %%r0,2(%0)", operands);
2812             if (n_bytes % 2 != 0)
2813               output_asm_insn ("stb %%r0,0(%0)", operands);
2814           }
2815         return "";
2816
2817       case 1:
2818         /* Pre-adjust the loop counter.  */
2819         operands[2] = GEN_INT (n_bytes - 2);
2820         output_asm_insn ("ldi %2,%1", operands);
2821
2822         /* Loop.  */
2823         output_asm_insn ("{stbs|stb},ma %%r0,1(%0)", operands);
2824         output_asm_insn ("addib,>= -2,%1,.-4", operands);
2825         output_asm_insn ("{stbs|stb},ma %%r0,1(%0)", operands);
2826
2827         /* Handle the residual.  */
2828         if (n_bytes % 2 != 0)
2829           output_asm_insn ("stb %%r0,0(%0)", operands);
2830
2831         return "";
2832
2833       default:
2834         gcc_unreachable ();
2835     }
2836 }
2837
2838 /* Count the number of insns necessary to handle this block move.
2839
2840    Basic structure is the same as emit_block_move, except that we
2841    count insns rather than emit them.  */
2842
2843 static int
2844 compute_clrmem_length (rtx insn)
2845 {
2846   rtx pat = PATTERN (insn);
2847   unsigned int align = INTVAL (XEXP (XVECEXP (pat, 0, 4), 0));
2848   unsigned long n_bytes = INTVAL (XEXP (XVECEXP (pat, 0, 3), 0));
2849   unsigned int n_insns = 0;
2850
2851   /* We can't clear more than a word at a time because the PA
2852      has no longer integer move insns.  */
2853   if (align > (TARGET_64BIT ? 8 : 4))
2854     align = (TARGET_64BIT ? 8 : 4);
2855
2856   /* The basic loop.  */
2857   n_insns = 4;
2858
2859   /* Residuals.  */
2860   if (n_bytes % (2 * align) != 0)
2861     {
2862       if ((n_bytes % (2 * align)) >= align)
2863         n_insns++;
2864
2865       if ((n_bytes % align) != 0)
2866         n_insns++;
2867     }
2868
2869   /* Lengths are expressed in bytes now; each insn is 4 bytes.  */
2870   return n_insns * 4;
2871 }
2872 \f
2873
2874 const char *
2875 output_and (rtx *operands)
2876 {
2877   if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) != 0)
2878     {
2879       unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2880       int ls0, ls1, ms0, p, len;
2881
2882       for (ls0 = 0; ls0 < 32; ls0++)
2883         if ((mask & (1 << ls0)) == 0)
2884           break;
2885
2886       for (ls1 = ls0; ls1 < 32; ls1++)
2887         if ((mask & (1 << ls1)) != 0)
2888           break;
2889
2890       for (ms0 = ls1; ms0 < 32; ms0++)
2891         if ((mask & (1 << ms0)) == 0)
2892           break;
2893
2894       gcc_assert (ms0 == 32);
2895
2896       if (ls1 == 32)
2897         {
2898           len = ls0;
2899
2900           gcc_assert (len);
2901
2902           operands[2] = GEN_INT (len);
2903           return "{extru|extrw,u} %1,31,%2,%0";
2904         }
2905       else
2906         {
2907           /* We could use this `depi' for the case above as well, but `depi'
2908              requires one more register file access than an `extru'.  */
2909
2910           p = 31 - ls0;
2911           len = ls1 - ls0;
2912
2913           operands[2] = GEN_INT (p);
2914           operands[3] = GEN_INT (len);
2915           return "{depi|depwi} 0,%2,%3,%0";
2916         }
2917     }
2918   else
2919     return "and %1,%2,%0";
2920 }
2921
2922 /* Return a string to perform a bitwise-and of operands[1] with operands[2]
2923    storing the result in operands[0].  */
2924 const char *
2925 output_64bit_and (rtx *operands)
2926 {
2927   if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) != 0)
2928     {
2929       unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2930       int ls0, ls1, ms0, p, len;
2931
2932       for (ls0 = 0; ls0 < HOST_BITS_PER_WIDE_INT; ls0++)
2933         if ((mask & ((unsigned HOST_WIDE_INT) 1 << ls0)) == 0)
2934           break;
2935
2936       for (ls1 = ls0; ls1 < HOST_BITS_PER_WIDE_INT; ls1++)
2937         if ((mask & ((unsigned HOST_WIDE_INT) 1 << ls1)) != 0)
2938           break;
2939
2940       for (ms0 = ls1; ms0 < HOST_BITS_PER_WIDE_INT; ms0++)
2941         if ((mask & ((unsigned HOST_WIDE_INT) 1 << ms0)) == 0)
2942           break;
2943
2944       gcc_assert (ms0 == HOST_BITS_PER_WIDE_INT);
2945
2946       if (ls1 == HOST_BITS_PER_WIDE_INT)
2947         {
2948           len = ls0;
2949
2950           gcc_assert (len);
2951
2952           operands[2] = GEN_INT (len);
2953           return "extrd,u %1,63,%2,%0";
2954         }
2955       else
2956         {
2957           /* We could use this `depi' for the case above as well, but `depi'
2958              requires one more register file access than an `extru'.  */
2959
2960           p = 63 - ls0;
2961           len = ls1 - ls0;
2962
2963           operands[2] = GEN_INT (p);
2964           operands[3] = GEN_INT (len);
2965           return "depdi 0,%2,%3,%0";
2966         }
2967     }
2968   else
2969     return "and %1,%2,%0";
2970 }
2971
2972 const char *
2973 output_ior (rtx *operands)
2974 {
2975   unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
2976   int bs0, bs1, p, len;
2977
2978   if (INTVAL (operands[2]) == 0)
2979     return "copy %1,%0";
2980
2981   for (bs0 = 0; bs0 < 32; bs0++)
2982     if ((mask & (1 << bs0)) != 0)
2983       break;
2984
2985   for (bs1 = bs0; bs1 < 32; bs1++)
2986     if ((mask & (1 << bs1)) == 0)
2987       break;
2988
2989   gcc_assert (bs1 == 32 || ((unsigned HOST_WIDE_INT) 1 << bs1) > mask);
2990
2991   p = 31 - bs0;
2992   len = bs1 - bs0;
2993
2994   operands[2] = GEN_INT (p);
2995   operands[3] = GEN_INT (len);
2996   return "{depi|depwi} -1,%2,%3,%0";
2997 }
2998
2999 /* Return a string to perform a bitwise-and of operands[1] with operands[2]
3000    storing the result in operands[0].  */
3001 const char *
3002 output_64bit_ior (rtx *operands)
3003 {
3004   unsigned HOST_WIDE_INT mask = INTVAL (operands[2]);
3005   int bs0, bs1, p, len;
3006
3007   if (INTVAL (operands[2]) == 0)
3008     return "copy %1,%0";
3009
3010   for (bs0 = 0; bs0 < HOST_BITS_PER_WIDE_INT; bs0++)
3011     if ((mask & ((unsigned HOST_WIDE_INT) 1 << bs0)) != 0)
3012       break;
3013
3014   for (bs1 = bs0; bs1 < HOST_BITS_PER_WIDE_INT; bs1++)
3015     if ((mask & ((unsigned HOST_WIDE_INT) 1 << bs1)) == 0)
3016       break;
3017
3018   gcc_assert (bs1 == HOST_BITS_PER_WIDE_INT
3019               || ((unsigned HOST_WIDE_INT) 1 << bs1) > mask);
3020
3021   p = 63 - bs0;
3022   len = bs1 - bs0;
3023
3024   operands[2] = GEN_INT (p);
3025   operands[3] = GEN_INT (len);
3026   return "depdi -1,%2,%3,%0";
3027 }
3028 \f
3029 /* Target hook for assembling integer objects.  This code handles
3030    aligned SI and DI integers specially since function references
3031    must be preceded by P%.  */
3032
3033 static bool
3034 pa_assemble_integer (rtx x, unsigned int size, int aligned_p)
3035 {
3036   if (size == UNITS_PER_WORD
3037       && aligned_p
3038       && function_label_operand (x, VOIDmode))
3039     {
3040       fputs (size == 8? "\t.dword\tP%" : "\t.word\tP%", asm_out_file);
3041       output_addr_const (asm_out_file, x);
3042       fputc ('\n', asm_out_file);
3043       return true;
3044     }
3045   return default_assemble_integer (x, size, aligned_p);
3046 }
3047 \f
3048 /* Output an ascii string.  */
3049 void
3050 output_ascii (FILE *file, const char *p, int size)
3051 {
3052   int i;
3053   int chars_output;
3054   unsigned char partial_output[16];     /* Max space 4 chars can occupy.  */
3055
3056   /* The HP assembler can only take strings of 256 characters at one
3057      time.  This is a limitation on input line length, *not* the
3058      length of the string.  Sigh.  Even worse, it seems that the
3059      restriction is in number of input characters (see \xnn &
3060      \whatever).  So we have to do this very carefully.  */
3061
3062   fputs ("\t.STRING \"", file);
3063
3064   chars_output = 0;
3065   for (i = 0; i < size; i += 4)
3066     {
3067       int co = 0;
3068       int io = 0;
3069       for (io = 0, co = 0; io < MIN (4, size - i); io++)
3070         {
3071           register unsigned int c = (unsigned char) p[i + io];
3072
3073           if (c == '\"' || c == '\\')
3074             partial_output[co++] = '\\';
3075           if (c >= ' ' && c < 0177)
3076             partial_output[co++] = c;
3077           else
3078             {
3079               unsigned int hexd;
3080               partial_output[co++] = '\\';
3081               partial_output[co++] = 'x';
3082               hexd =  c  / 16 - 0 + '0';
3083               if (hexd > '9')
3084                 hexd -= '9' - 'a' + 1;
3085               partial_output[co++] = hexd;
3086               hexd =  c % 16 - 0 + '0';
3087               if (hexd > '9')
3088                 hexd -= '9' - 'a' + 1;
3089               partial_output[co++] = hexd;
3090             }
3091         }
3092       if (chars_output + co > 243)
3093         {
3094           fputs ("\"\n\t.STRING \"", file);
3095           chars_output = 0;
3096         }
3097       fwrite (partial_output, 1, (size_t) co, file);
3098       chars_output += co;
3099       co = 0;
3100     }
3101   fputs ("\"\n", file);
3102 }
3103
3104 /* Try to rewrite floating point comparisons & branches to avoid
3105    useless add,tr insns.
3106
3107    CHECK_NOTES is nonzero if we should examine REG_DEAD notes
3108    to see if FPCC is dead.  CHECK_NOTES is nonzero for the
3109    first attempt to remove useless add,tr insns.  It is zero
3110    for the second pass as reorg sometimes leaves bogus REG_DEAD
3111    notes lying around.
3112
3113    When CHECK_NOTES is zero we can only eliminate add,tr insns
3114    when there's a 1:1 correspondence between fcmp and ftest/fbranch
3115    instructions.  */
3116 static void
3117 remove_useless_addtr_insns (int check_notes)
3118 {
3119   rtx insn;
3120   static int pass = 0;
3121
3122   /* This is fairly cheap, so always run it when optimizing.  */
3123   if (optimize > 0)
3124     {
3125       int fcmp_count = 0;
3126       int fbranch_count = 0;
3127
3128       /* Walk all the insns in this function looking for fcmp & fbranch
3129          instructions.  Keep track of how many of each we find.  */
3130       for (insn = get_insns (); insn; insn = next_insn (insn))
3131         {
3132           rtx tmp;
3133
3134           /* Ignore anything that isn't an INSN or a JUMP_INSN.  */
3135           if (GET_CODE (insn) != INSN && GET_CODE (insn) != JUMP_INSN)
3136             continue;
3137
3138           tmp = PATTERN (insn);
3139
3140           /* It must be a set.  */
3141           if (GET_CODE (tmp) != SET)
3142             continue;
3143
3144           /* If the destination is CCFP, then we've found an fcmp insn.  */
3145           tmp = SET_DEST (tmp);
3146           if (GET_CODE (tmp) == REG && REGNO (tmp) == 0)
3147             {
3148               fcmp_count++;
3149               continue;
3150             }
3151
3152           tmp = PATTERN (insn);
3153           /* If this is an fbranch instruction, bump the fbranch counter.  */
3154           if (GET_CODE (tmp) == SET
3155               && SET_DEST (tmp) == pc_rtx
3156               && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
3157               && GET_CODE (XEXP (SET_SRC (tmp), 0)) == NE
3158               && GET_CODE (XEXP (XEXP (SET_SRC (tmp), 0), 0)) == REG
3159               && REGNO (XEXP (XEXP (SET_SRC (tmp), 0), 0)) == 0)
3160             {
3161               fbranch_count++;
3162               continue;
3163             }
3164         }
3165
3166
3167       /* Find all floating point compare + branch insns.  If possible,
3168          reverse the comparison & the branch to avoid add,tr insns.  */
3169       for (insn = get_insns (); insn; insn = next_insn (insn))
3170         {
3171           rtx tmp, next;
3172
3173           /* Ignore anything that isn't an INSN.  */
3174           if (GET_CODE (insn) != INSN)
3175             continue;
3176
3177           tmp = PATTERN (insn);
3178
3179           /* It must be a set.  */
3180           if (GET_CODE (tmp) != SET)
3181             continue;
3182
3183           /* The destination must be CCFP, which is register zero.  */
3184           tmp = SET_DEST (tmp);
3185           if (GET_CODE (tmp) != REG || REGNO (tmp) != 0)
3186             continue;
3187
3188           /* INSN should be a set of CCFP.
3189
3190              See if the result of this insn is used in a reversed FP
3191              conditional branch.  If so, reverse our condition and
3192              the branch.  Doing so avoids useless add,tr insns.  */
3193           next = next_insn (insn);
3194           while (next)
3195             {
3196               /* Jumps, calls and labels stop our search.  */
3197               if (GET_CODE (next) == JUMP_INSN
3198                   || GET_CODE (next) == CALL_INSN
3199                   || GET_CODE (next) == CODE_LABEL)
3200                 break;
3201
3202               /* As does another fcmp insn.  */
3203               if (GET_CODE (next) == INSN
3204                   && GET_CODE (PATTERN (next)) == SET
3205                   && GET_CODE (SET_DEST (PATTERN (next))) == REG
3206                   && REGNO (SET_DEST (PATTERN (next))) == 0)
3207                 break;
3208
3209               next = next_insn (next);
3210             }
3211
3212           /* Is NEXT_INSN a branch?  */
3213           if (next
3214               && GET_CODE (next) == JUMP_INSN)
3215             {
3216               rtx pattern = PATTERN (next);
3217
3218               /* If it a reversed fp conditional branch (e.g. uses add,tr)
3219                  and CCFP dies, then reverse our conditional and the branch
3220                  to avoid the add,tr.  */
3221               if (GET_CODE (pattern) == SET
3222                   && SET_DEST (pattern) == pc_rtx
3223                   && GET_CODE (SET_SRC (pattern)) == IF_THEN_ELSE
3224                   && GET_CODE (XEXP (SET_SRC (pattern), 0)) == NE
3225                   && GET_CODE (XEXP (XEXP (SET_SRC (pattern), 0), 0)) == REG
3226                   && REGNO (XEXP (XEXP (SET_SRC (pattern), 0), 0)) == 0
3227                   && GET_CODE (XEXP (SET_SRC (pattern), 1)) == PC
3228                   && (fcmp_count == fbranch_count
3229                       || (check_notes
3230                           && find_regno_note (next, REG_DEAD, 0))))
3231                 {
3232                   /* Reverse the branch.  */
3233                   tmp = XEXP (SET_SRC (pattern), 1);
3234                   XEXP (SET_SRC (pattern), 1) = XEXP (SET_SRC (pattern), 2);
3235                   XEXP (SET_SRC (pattern), 2) = tmp;
3236                   INSN_CODE (next) = -1;
3237
3238                   /* Reverse our condition.  */
3239                   tmp = PATTERN (insn);
3240                   PUT_CODE (XEXP (tmp, 1),
3241                             (reverse_condition_maybe_unordered
3242                              (GET_CODE (XEXP (tmp, 1)))));
3243                 }
3244             }
3245         }
3246     }
3247
3248   pass = !pass;
3249
3250 }
3251 \f
3252 /* You may have trouble believing this, but this is the 32 bit HP-PA
3253    stack layout.  Wow.
3254
3255    Offset               Contents
3256
3257    Variable arguments   (optional; any number may be allocated)
3258
3259    SP-(4*(N+9))         arg word N
3260         :                   :
3261       SP-56             arg word 5
3262       SP-52             arg word 4
3263
3264    Fixed arguments      (must be allocated; may remain unused)
3265
3266       SP-48             arg word 3
3267       SP-44             arg word 2
3268       SP-40             arg word 1
3269       SP-36             arg word 0
3270
3271    Frame Marker
3272
3273       SP-32             External Data Pointer (DP)
3274       SP-28             External sr4
3275       SP-24             External/stub RP (RP')
3276       SP-20             Current RP
3277       SP-16             Static Link
3278       SP-12             Clean up
3279       SP-8              Calling Stub RP (RP'')
3280       SP-4              Previous SP
3281
3282    Top of Frame
3283
3284       SP-0              Stack Pointer (points to next available address)
3285
3286 */
3287
3288 /* This function saves registers as follows.  Registers marked with ' are
3289    this function's registers (as opposed to the previous function's).
3290    If a frame_pointer isn't needed, r4 is saved as a general register;
3291    the space for the frame pointer is still allocated, though, to keep
3292    things simple.
3293
3294
3295    Top of Frame
3296
3297        SP (FP')         Previous FP
3298        SP + 4           Alignment filler (sigh)
3299        SP + 8           Space for locals reserved here.
3300        .
3301        .
3302        .
3303        SP + n           All call saved register used.
3304        .
3305        .
3306        .
3307        SP + o           All call saved fp registers used.
3308        .
3309        .
3310        .
3311        SP + p (SP')     points to next available address.
3312
3313 */
3314
3315 /* Global variables set by output_function_prologue().  */
3316 /* Size of frame.  Need to know this to emit return insns from
3317    leaf procedures.  */
3318 static HOST_WIDE_INT actual_fsize, local_fsize;
3319 static int save_fregs;
3320
3321 /* Emit RTL to store REG at the memory location specified by BASE+DISP.
3322    Handle case where DISP > 8k by using the add_high_const patterns.
3323
3324    Note in DISP > 8k case, we will leave the high part of the address
3325    in %r1.  There is code in expand_hppa_{prologue,epilogue} that knows this.*/
3326
3327 static void
3328 store_reg (int reg, HOST_WIDE_INT disp, int base)
3329 {
3330   rtx insn, dest, src, basereg;
3331
3332   src = gen_rtx_REG (word_mode, reg);
3333   basereg = gen_rtx_REG (Pmode, base);
3334   if (VAL_14_BITS_P (disp))
3335     {
3336       dest = gen_rtx_MEM (word_mode, plus_constant (basereg, disp));
3337       insn = emit_move_insn (dest, src);
3338     }
3339   else if (TARGET_64BIT && !VAL_32_BITS_P (disp))
3340     {
3341       rtx delta = GEN_INT (disp);
3342       rtx tmpreg = gen_rtx_REG (Pmode, 1);
3343
3344       emit_move_insn (tmpreg, delta);
3345       insn = emit_move_insn (tmpreg, gen_rtx_PLUS (Pmode, tmpreg, basereg));
3346       if (DO_FRAME_NOTES)
3347         {
3348           REG_NOTES (insn)
3349             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3350                 gen_rtx_SET (VOIDmode, tmpreg,
3351                              gen_rtx_PLUS (Pmode, basereg, delta)),
3352                 REG_NOTES (insn));
3353           RTX_FRAME_RELATED_P (insn) = 1;
3354         }
3355       dest = gen_rtx_MEM (word_mode, tmpreg);
3356       insn = emit_move_insn (dest, src);
3357     }
3358   else
3359     {
3360       rtx delta = GEN_INT (disp);
3361       rtx high = gen_rtx_PLUS (Pmode, basereg, gen_rtx_HIGH (Pmode, delta));
3362       rtx tmpreg = gen_rtx_REG (Pmode, 1);
3363
3364       emit_move_insn (tmpreg, high);
3365       dest = gen_rtx_MEM (word_mode, gen_rtx_LO_SUM (Pmode, tmpreg, delta));
3366       insn = emit_move_insn (dest, src);
3367       if (DO_FRAME_NOTES)
3368         {
3369           REG_NOTES (insn)
3370             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3371                 gen_rtx_SET (VOIDmode,
3372                              gen_rtx_MEM (word_mode,
3373                                           gen_rtx_PLUS (word_mode, basereg,
3374                                                         delta)),
3375                              src),
3376                 REG_NOTES (insn));
3377         }
3378     }
3379
3380   if (DO_FRAME_NOTES)
3381     RTX_FRAME_RELATED_P (insn) = 1;
3382 }
3383
3384 /* Emit RTL to store REG at the memory location specified by BASE and then
3385    add MOD to BASE.  MOD must be <= 8k.  */
3386
3387 static void
3388 store_reg_modify (int base, int reg, HOST_WIDE_INT mod)
3389 {
3390   rtx insn, basereg, srcreg, delta;
3391
3392   gcc_assert (VAL_14_BITS_P (mod));
3393
3394   basereg = gen_rtx_REG (Pmode, base);
3395   srcreg = gen_rtx_REG (word_mode, reg);
3396   delta = GEN_INT (mod);
3397
3398   insn = emit_insn (gen_post_store (basereg, srcreg, delta));
3399   if (DO_FRAME_NOTES)
3400     {
3401       RTX_FRAME_RELATED_P (insn) = 1;
3402
3403       /* RTX_FRAME_RELATED_P must be set on each frame related set
3404          in a parallel with more than one element.  */
3405       RTX_FRAME_RELATED_P (XVECEXP (PATTERN (insn), 0, 0)) = 1;
3406       RTX_FRAME_RELATED_P (XVECEXP (PATTERN (insn), 0, 1)) = 1;
3407     }
3408 }
3409
3410 /* Emit RTL to set REG to the value specified by BASE+DISP.  Handle case
3411    where DISP > 8k by using the add_high_const patterns.  NOTE indicates
3412    whether to add a frame note or not.
3413
3414    In the DISP > 8k case, we leave the high part of the address in %r1.
3415    There is code in expand_hppa_{prologue,epilogue} that knows about this.  */
3416
3417 static void
3418 set_reg_plus_d (int reg, int base, HOST_WIDE_INT disp, int note)
3419 {
3420   rtx insn;
3421
3422   if (VAL_14_BITS_P (disp))
3423     {
3424       insn = emit_move_insn (gen_rtx_REG (Pmode, reg),
3425                              plus_constant (gen_rtx_REG (Pmode, base), disp));
3426     }
3427   else if (TARGET_64BIT && !VAL_32_BITS_P (disp))
3428     {
3429       rtx basereg = gen_rtx_REG (Pmode, base);
3430       rtx delta = GEN_INT (disp);
3431       rtx tmpreg = gen_rtx_REG (Pmode, 1);
3432
3433       emit_move_insn (tmpreg, delta);
3434       insn = emit_move_insn (gen_rtx_REG (Pmode, reg),
3435                              gen_rtx_PLUS (Pmode, tmpreg, basereg));
3436       if (DO_FRAME_NOTES)
3437         REG_NOTES (insn)
3438           = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3439               gen_rtx_SET (VOIDmode, tmpreg,
3440                            gen_rtx_PLUS (Pmode, basereg, delta)),
3441               REG_NOTES (insn));
3442     }
3443   else
3444     {
3445       rtx basereg = gen_rtx_REG (Pmode, base);
3446       rtx delta = GEN_INT (disp);
3447       rtx tmpreg = gen_rtx_REG (Pmode, 1);
3448
3449       emit_move_insn (tmpreg,
3450                       gen_rtx_PLUS (Pmode, basereg,
3451                                     gen_rtx_HIGH (Pmode, delta)));
3452       insn = emit_move_insn (gen_rtx_REG (Pmode, reg),
3453                              gen_rtx_LO_SUM (Pmode, tmpreg, delta));
3454     }
3455
3456   if (DO_FRAME_NOTES && note)
3457     RTX_FRAME_RELATED_P (insn) = 1;
3458 }
3459
3460 HOST_WIDE_INT
3461 compute_frame_size (HOST_WIDE_INT size, int *fregs_live)
3462 {
3463   int freg_saved = 0;
3464   int i, j;
3465
3466   /* The code in hppa_expand_prologue and hppa_expand_epilogue must
3467      be consistent with the rounding and size calculation done here.
3468      Change them at the same time.  */
3469
3470   /* We do our own stack alignment.  First, round the size of the
3471      stack locals up to a word boundary.  */
3472   size = (size + UNITS_PER_WORD - 1) & ~(UNITS_PER_WORD - 1);
3473
3474   /* Space for previous frame pointer + filler.  If any frame is
3475      allocated, we need to add in the STARTING_FRAME_OFFSET.  We
3476      waste some space here for the sake of HP compatibility.  The
3477      first slot is only used when the frame pointer is needed.  */
3478   if (size || frame_pointer_needed)
3479     size += STARTING_FRAME_OFFSET;
3480   
3481   /* If the current function calls __builtin_eh_return, then we need
3482      to allocate stack space for registers that will hold data for
3483      the exception handler.  */
3484   if (DO_FRAME_NOTES && current_function_calls_eh_return)
3485     {
3486       unsigned int i;
3487
3488       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
3489         continue;
3490       size += i * UNITS_PER_WORD;
3491     }
3492
3493   /* Account for space used by the callee general register saves.  */
3494   for (i = 18, j = frame_pointer_needed ? 4 : 3; i >= j; i--)
3495     if (df_regs_ever_live_p (i))
3496       size += UNITS_PER_WORD;
3497
3498   /* Account for space used by the callee floating point register saves.  */
3499   for (i = FP_SAVED_REG_LAST; i >= FP_SAVED_REG_FIRST; i -= FP_REG_STEP)
3500     if (df_regs_ever_live_p (i)
3501         || (!TARGET_64BIT && df_regs_ever_live_p (i + 1)))
3502       {
3503         freg_saved = 1;
3504
3505         /* We always save both halves of the FP register, so always
3506            increment the frame size by 8 bytes.  */
3507         size += 8;
3508       }
3509
3510   /* If any of the floating registers are saved, account for the
3511      alignment needed for the floating point register save block.  */
3512   if (freg_saved)
3513     {
3514       size = (size + 7) & ~7;
3515       if (fregs_live)
3516         *fregs_live = 1;
3517     }
3518
3519   /* The various ABIs include space for the outgoing parameters in the
3520      size of the current function's stack frame.  We don't need to align
3521      for the outgoing arguments as their alignment is set by the final
3522      rounding for the frame as a whole.  */
3523   size += current_function_outgoing_args_size;
3524
3525   /* Allocate space for the fixed frame marker.  This space must be
3526      allocated for any function that makes calls or allocates
3527      stack space.  */
3528   if (!current_function_is_leaf || size)
3529     size += TARGET_64BIT ? 48 : 32;
3530
3531   /* Finally, round to the preferred stack boundary.  */
3532   return ((size + PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)
3533           & ~(PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1));
3534 }
3535
3536 /* Generate the assembly code for function entry.  FILE is a stdio
3537    stream to output the code to.  SIZE is an int: how many units of
3538    temporary storage to allocate.
3539
3540    Refer to the array `regs_ever_live' to determine which registers to
3541    save; `regs_ever_live[I]' is nonzero if register number I is ever
3542    used in the function.  This function is responsible for knowing
3543    which registers should not be saved even if used.  */
3544
3545 /* On HP-PA, move-double insns between fpu and cpu need an 8-byte block
3546    of memory.  If any fpu reg is used in the function, we allocate
3547    such a block here, at the bottom of the frame, just in case it's needed.
3548
3549    If this function is a leaf procedure, then we may choose not
3550    to do a "save" insn.  The decision about whether or not
3551    to do this is made in regclass.c.  */
3552
3553 static void
3554 pa_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
3555 {
3556   /* The function's label and associated .PROC must never be
3557      separated and must be output *after* any profiling declarations
3558      to avoid changing spaces/subspaces within a procedure.  */
3559   ASM_OUTPUT_LABEL (file, XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0));
3560   fputs ("\t.PROC\n", file);
3561
3562   /* hppa_expand_prologue does the dirty work now.  We just need
3563      to output the assembler directives which denote the start
3564      of a function.  */
3565   fprintf (file, "\t.CALLINFO FRAME=" HOST_WIDE_INT_PRINT_DEC, actual_fsize);
3566   if (df_regs_ever_live_p (2))
3567     fputs (",CALLS,SAVE_RP", file);
3568   else
3569     fputs (",NO_CALLS", file);
3570
3571   /* The SAVE_SP flag is used to indicate that register %r3 is stored
3572      at the beginning of the frame and that it is used as the frame
3573      pointer for the frame.  We do this because our current frame
3574      layout doesn't conform to that specified in the HP runtime
3575      documentation and we need a way to indicate to programs such as
3576      GDB where %r3 is saved.  The SAVE_SP flag was chosen because it
3577      isn't used by HP compilers but is supported by the assembler.
3578      However, SAVE_SP is supposed to indicate that the previous stack
3579      pointer has been saved in the frame marker.  */
3580   if (frame_pointer_needed)
3581     fputs (",SAVE_SP", file);
3582
3583   /* Pass on information about the number of callee register saves
3584      performed in the prologue.
3585
3586      The compiler is supposed to pass the highest register number
3587      saved, the assembler then has to adjust that number before
3588      entering it into the unwind descriptor (to account for any
3589      caller saved registers with lower register numbers than the
3590      first callee saved register).  */
3591   if (gr_saved)
3592     fprintf (file, ",ENTRY_GR=%d", gr_saved + 2);
3593
3594   if (fr_saved)
3595     fprintf (file, ",ENTRY_FR=%d", fr_saved + 11);
3596
3597   fputs ("\n\t.ENTRY\n", file);
3598
3599   remove_useless_addtr_insns (0);
3600 }
3601
3602 void
3603 hppa_expand_prologue (void)
3604 {
3605   int merge_sp_adjust_with_store = 0;
3606   HOST_WIDE_INT size = get_frame_size ();
3607   HOST_WIDE_INT offset;
3608   int i;
3609   rtx insn, tmpreg;
3610
3611   gr_saved = 0;
3612   fr_saved = 0;
3613   save_fregs = 0;
3614
3615   /* Compute total size for frame pointer, filler, locals and rounding to
3616      the next word boundary.  Similar code appears in compute_frame_size
3617      and must be changed in tandem with this code.  */
3618   local_fsize = (size + UNITS_PER_WORD - 1) & ~(UNITS_PER_WORD - 1);
3619   if (local_fsize || frame_pointer_needed)
3620     local_fsize += STARTING_FRAME_OFFSET;
3621
3622   actual_fsize = compute_frame_size (size, &save_fregs);
3623
3624   /* Compute a few things we will use often.  */
3625   tmpreg = gen_rtx_REG (word_mode, 1);
3626
3627   /* Save RP first.  The calling conventions manual states RP will
3628      always be stored into the caller's frame at sp - 20 or sp - 16
3629      depending on which ABI is in use.  */
3630   if (df_regs_ever_live_p (2) || current_function_calls_eh_return)
3631     store_reg (2, TARGET_64BIT ? -16 : -20, STACK_POINTER_REGNUM);
3632
3633   /* Allocate the local frame and set up the frame pointer if needed.  */
3634   if (actual_fsize != 0)
3635     {
3636       if (frame_pointer_needed)
3637         {
3638           /* Copy the old frame pointer temporarily into %r1.  Set up the
3639              new stack pointer, then store away the saved old frame pointer
3640              into the stack at sp and at the same time update the stack
3641              pointer by actual_fsize bytes.  Two versions, first
3642              handles small (<8k) frames.  The second handles large (>=8k)
3643              frames.  */
3644           insn = emit_move_insn (tmpreg, frame_pointer_rtx);
3645           if (DO_FRAME_NOTES)
3646             RTX_FRAME_RELATED_P (insn) = 1;
3647
3648           insn = emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
3649           if (DO_FRAME_NOTES)
3650             RTX_FRAME_RELATED_P (insn) = 1;
3651
3652           if (VAL_14_BITS_P (actual_fsize))
3653             store_reg_modify (STACK_POINTER_REGNUM, 1, actual_fsize);
3654           else
3655             {
3656               /* It is incorrect to store the saved frame pointer at *sp,
3657                  then increment sp (writes beyond the current stack boundary).
3658
3659                  So instead use stwm to store at *sp and post-increment the
3660                  stack pointer as an atomic operation.  Then increment sp to
3661                  finish allocating the new frame.  */
3662               HOST_WIDE_INT adjust1 = 8192 - 64;
3663               HOST_WIDE_INT adjust2 = actual_fsize - adjust1;
3664
3665               store_reg_modify (STACK_POINTER_REGNUM, 1, adjust1);
3666               set_reg_plus_d (STACK_POINTER_REGNUM, STACK_POINTER_REGNUM,
3667                               adjust2, 1);
3668             }
3669
3670           /* We set SAVE_SP in frames that need a frame pointer.  Thus,
3671              we need to store the previous stack pointer (frame pointer)
3672              into the frame marker on targets that use the HP unwind
3673              library.  This allows the HP unwind library to be used to
3674              unwind GCC frames.  However, we are not fully compatible
3675              with the HP library because our frame layout differs from
3676              that specified in the HP runtime specification.
3677
3678              We don't want a frame note on this instruction as the frame
3679              marker moves during dynamic stack allocation.
3680
3681              This instruction also serves as a blockage to prevent
3682              register spills from being scheduled before the stack
3683              pointer is raised.  This is necessary as we store
3684              registers using the frame pointer as a base register,
3685              and the frame pointer is set before sp is raised.  */
3686           if (TARGET_HPUX_UNWIND_LIBRARY)
3687             {
3688               rtx addr = gen_rtx_PLUS (word_mode, stack_pointer_rtx,
3689                                        GEN_INT (TARGET_64BIT ? -8 : -4));
3690
3691               emit_move_insn (gen_rtx_MEM (word_mode, addr),
3692                               frame_pointer_rtx);
3693             }
3694           else
3695             emit_insn (gen_blockage ());
3696         }
3697       /* no frame pointer needed.  */
3698       else
3699         {
3700           /* In some cases we can perform the first callee register save
3701              and allocating the stack frame at the same time.   If so, just
3702              make a note of it and defer allocating the frame until saving
3703              the callee registers.  */
3704           if (VAL_14_BITS_P (actual_fsize) && local_fsize == 0)
3705             merge_sp_adjust_with_store = 1;
3706           /* Can not optimize.  Adjust the stack frame by actual_fsize
3707              bytes.  */
3708           else
3709             set_reg_plus_d (STACK_POINTER_REGNUM, STACK_POINTER_REGNUM,
3710                             actual_fsize, 1);
3711         }
3712     }
3713
3714   /* Normal register save.
3715
3716      Do not save the frame pointer in the frame_pointer_needed case.  It
3717      was done earlier.  */
3718   if (frame_pointer_needed)
3719     {
3720       offset = local_fsize;
3721
3722       /* Saving the EH return data registers in the frame is the simplest
3723          way to get the frame unwind information emitted.  We put them
3724          just before the general registers.  */
3725       if (DO_FRAME_NOTES && current_function_calls_eh_return)
3726         {
3727           unsigned int i, regno;
3728
3729           for (i = 0; ; ++i)
3730             {
3731               regno = EH_RETURN_DATA_REGNO (i);
3732               if (regno == INVALID_REGNUM)
3733                 break;
3734
3735               store_reg (regno, offset, FRAME_POINTER_REGNUM);
3736               offset += UNITS_PER_WORD;
3737             }
3738         }
3739
3740       for (i = 18; i >= 4; i--)
3741         if (df_regs_ever_live_p (i) && ! call_used_regs[i])
3742           {
3743             store_reg (i, offset, FRAME_POINTER_REGNUM);
3744             offset += UNITS_PER_WORD;
3745             gr_saved++;
3746           }
3747       /* Account for %r3 which is saved in a special place.  */
3748       gr_saved++;
3749     }
3750   /* No frame pointer needed.  */
3751   else
3752     {
3753       offset = local_fsize - actual_fsize;
3754
3755       /* Saving the EH return data registers in the frame is the simplest
3756          way to get the frame unwind information emitted.  */
3757       if (DO_FRAME_NOTES && current_function_calls_eh_return)
3758         {
3759           unsigned int i, regno;
3760
3761           for (i = 0; ; ++i)
3762             {
3763               regno = EH_RETURN_DATA_REGNO (i);
3764               if (regno == INVALID_REGNUM)
3765                 break;
3766
3767               /* If merge_sp_adjust_with_store is nonzero, then we can
3768                  optimize the first save.  */
3769               if (merge_sp_adjust_with_store)
3770                 {
3771                   store_reg_modify (STACK_POINTER_REGNUM, regno, -offset);
3772                   merge_sp_adjust_with_store = 0;
3773                 }
3774               else
3775                 store_reg (regno, offset, STACK_POINTER_REGNUM);
3776               offset += UNITS_PER_WORD;
3777             }
3778         }
3779
3780       for (i = 18; i >= 3; i--)
3781         if (df_regs_ever_live_p (i) && ! call_used_regs[i])
3782           {
3783             /* If merge_sp_adjust_with_store is nonzero, then we can
3784                optimize the first GR save.  */
3785             if (merge_sp_adjust_with_store)
3786               {
3787                 store_reg_modify (STACK_POINTER_REGNUM, i, -offset);
3788                 merge_sp_adjust_with_store = 0;
3789               }
3790             else
3791               store_reg (i, offset, STACK_POINTER_REGNUM);
3792             offset += UNITS_PER_WORD;
3793             gr_saved++;
3794           }
3795
3796       /* If we wanted to merge the SP adjustment with a GR save, but we never
3797          did any GR saves, then just emit the adjustment here.  */
3798       if (merge_sp_adjust_with_store)
3799         set_reg_plus_d (STACK_POINTER_REGNUM, STACK_POINTER_REGNUM,
3800                         actual_fsize, 1);
3801     }
3802
3803   /* The hppa calling conventions say that %r19, the pic offset
3804      register, is saved at sp - 32 (in this function's frame)
3805      when generating PIC code.  FIXME:  What is the correct thing
3806      to do for functions which make no calls and allocate no
3807      frame?  Do we need to allocate a frame, or can we just omit
3808      the save?   For now we'll just omit the save.
3809      
3810      We don't want a note on this insn as the frame marker can
3811      move if there is a dynamic stack allocation.  */
3812   if (flag_pic && actual_fsize != 0 && !TARGET_64BIT)
3813     {
3814       rtx addr = gen_rtx_PLUS (word_mode, stack_pointer_rtx, GEN_INT (-32));
3815
3816       emit_move_insn (gen_rtx_MEM (word_mode, addr), pic_offset_table_rtx);
3817
3818     }
3819
3820   /* Align pointer properly (doubleword boundary).  */
3821   offset = (offset + 7) & ~7;
3822
3823   /* Floating point register store.  */
3824   if (save_fregs)
3825     {
3826       rtx base;
3827
3828       /* First get the frame or stack pointer to the start of the FP register
3829          save area.  */
3830       if (frame_pointer_needed)
3831         {
3832           set_reg_plus_d (1, FRAME_POINTER_REGNUM, offset, 0);
3833           base = frame_pointer_rtx;
3834         }
3835       else
3836         {
3837           set_reg_plus_d (1, STACK_POINTER_REGNUM, offset, 0);
3838           base = stack_pointer_rtx;
3839         }
3840
3841       /* Now actually save the FP registers.  */
3842       for (i = FP_SAVED_REG_LAST; i >= FP_SAVED_REG_FIRST; i -= FP_REG_STEP)
3843         {
3844           if (df_regs_ever_live_p (i)
3845               || (! TARGET_64BIT && df_regs_ever_live_p (i + 1)))
3846             {
3847               rtx addr, insn, reg;
3848               addr = gen_rtx_MEM (DFmode, gen_rtx_POST_INC (DFmode, tmpreg));
3849               reg = gen_rtx_REG (DFmode, i);
3850               insn = emit_move_insn (addr, reg);
3851               if (DO_FRAME_NOTES)
3852                 {
3853                   RTX_FRAME_RELATED_P (insn) = 1;
3854                   if (TARGET_64BIT)
3855                     {
3856                       rtx mem = gen_rtx_MEM (DFmode,
3857                                              plus_constant (base, offset));
3858                       REG_NOTES (insn)
3859                         = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3860                                              gen_rtx_SET (VOIDmode, mem, reg),
3861                                              REG_NOTES (insn));
3862                     }
3863                   else
3864                     {
3865                       rtx meml = gen_rtx_MEM (SFmode,
3866                                               plus_constant (base, offset));
3867                       rtx memr = gen_rtx_MEM (SFmode,
3868                                               plus_constant (base, offset + 4));
3869                       rtx regl = gen_rtx_REG (SFmode, i);
3870                       rtx regr = gen_rtx_REG (SFmode, i + 1);
3871                       rtx setl = gen_rtx_SET (VOIDmode, meml, regl);
3872                       rtx setr = gen_rtx_SET (VOIDmode, memr, regr);
3873                       rtvec vec;
3874
3875                       RTX_FRAME_RELATED_P (setl) = 1;
3876                       RTX_FRAME_RELATED_P (setr) = 1;
3877                       vec = gen_rtvec (2, setl, setr);
3878                       REG_NOTES (insn)
3879                         = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3880                                              gen_rtx_SEQUENCE (VOIDmode, vec),
3881                                              REG_NOTES (insn));
3882                     }
3883                 }
3884               offset += GET_MODE_SIZE (DFmode);
3885               fr_saved++;
3886             }
3887         }
3888     }
3889 }
3890
3891 /* Emit RTL to load REG from the memory location specified by BASE+DISP.
3892    Handle case where DISP > 8k by using the add_high_const patterns.  */
3893
3894 static void
3895 load_reg (int reg, HOST_WIDE_INT disp, int base)
3896 {
3897   rtx dest = gen_rtx_REG (word_mode, reg);
3898   rtx basereg = gen_rtx_REG (Pmode, base);
3899   rtx src;
3900
3901   if (VAL_14_BITS_P (disp))
3902     src = gen_rtx_MEM (word_mode, plus_constant (basereg, disp));
3903   else if (TARGET_64BIT && !VAL_32_BITS_P (disp))
3904     {
3905       rtx delta = GEN_INT (disp);
3906       rtx tmpreg = gen_rtx_REG (Pmode, 1);
3907
3908       emit_move_insn (tmpreg, delta);
3909       if (TARGET_DISABLE_INDEXING)
3910         {
3911           emit_move_insn (tmpreg, gen_rtx_PLUS (Pmode, tmpreg, basereg));
3912           src = gen_rtx_MEM (word_mode, tmpreg);
3913         }
3914       else
3915         src = gen_rtx_MEM (word_mode, gen_rtx_PLUS (Pmode, tmpreg, basereg));
3916     }
3917   else
3918     {
3919       rtx delta = GEN_INT (disp);
3920       rtx high = gen_rtx_PLUS (Pmode, basereg, gen_rtx_HIGH (Pmode, delta));
3921       rtx tmpreg = gen_rtx_REG (Pmode, 1);
3922
3923       emit_move_insn (tmpreg, high);
3924       src = gen_rtx_MEM (word_mode, gen_rtx_LO_SUM (Pmode, tmpreg, delta));
3925     }
3926
3927   emit_move_insn (dest, src);
3928 }
3929
3930 /* Update the total code bytes output to the text section.  */
3931
3932 static void
3933 update_total_code_bytes (int nbytes)
3934 {
3935   if ((TARGET_PORTABLE_RUNTIME || !TARGET_GAS || !TARGET_SOM)
3936       && !IN_NAMED_SECTION_P (cfun->decl))
3937     {
3938       if (INSN_ADDRESSES_SET_P ())
3939         {
3940           unsigned long old_total = total_code_bytes;
3941