alpha.cc revision 1.1.1.1 1 /* Subroutines used for code generation on the DEC Alpha.
2 Copyright (C) 1992-2022 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner (at) vlsi1.ultra.nyu.edu)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 #define IN_TARGET_CODE 1
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "memmodel.h"
34 #include "gimple.h"
35 #include "df.h"
36 #include "predict.h"
37 #include "tm_p.h"
38 #include "ssa.h"
39 #include "expmed.h"
40 #include "optabs.h"
41 #include "regs.h"
42 #include "emit-rtl.h"
43 #include "recog.h"
44 #include "diagnostic-core.h"
45 #include "alias.h"
46 #include "fold-const.h"
47 #include "stor-layout.h"
48 #include "calls.h"
49 #include "varasm.h"
50 #include "output.h"
51 #include "insn-attr.h"
52 #include "explow.h"
53 #include "expr.h"
54 #include "reload.h"
55 #include "except.h"
56 #include "common/common-target.h"
57 #include "debug.h"
58 #include "langhooks.h"
59 #include "cfgrtl.h"
60 #include "tree-pass.h"
61 #include "context.h"
62 #include "gimple-iterator.h"
63 #include "gimplify.h"
64 #include "tree-stdarg.h"
65 #include "tm-constrs.h"
66 #include "libfuncs.h"
67 #include "builtins.h"
68 #include "rtl-iter.h"
69 #include "flags.h"
70 #include "opts.h"
71
72 /* This file should be included last. */
73 #include "target-def.h"
74
75 /* Specify which cpu to schedule for. */
76 enum processor_type alpha_tune;
77
78 /* Which cpu we're generating code for. */
79 enum processor_type alpha_cpu;
80
81 static const char * const alpha_cpu_name[] =
82 {
83 "ev4", "ev5", "ev6"
84 };
85
86 /* Specify how accurate floating-point traps need to be. */
87
88 enum alpha_trap_precision alpha_tp;
89
90 /* Specify the floating-point rounding mode. */
91
92 enum alpha_fp_rounding_mode alpha_fprm;
93
94 /* Specify which things cause traps. */
95
96 enum alpha_fp_trap_mode alpha_fptm;
97
98 /* Nonzero if inside of a function, because the Alpha asm can't
99 handle .files inside of functions. */
100
101 static int inside_function = FALSE;
102
103 /* The number of cycles of latency we should assume on memory reads. */
104
105 static int alpha_memory_latency = 3;
106
107 /* Whether the function needs the GP. */
108
109 static int alpha_function_needs_gp;
110
111 /* The assembler name of the current function. */
112
113 static const char *alpha_fnname;
114
115 /* The next explicit relocation sequence number. */
116 extern GTY(()) int alpha_next_sequence_number;
117 int alpha_next_sequence_number = 1;
118
119 /* The literal and gpdisp sequence numbers for this insn, as printed
120 by %# and %* respectively. */
121 extern GTY(()) int alpha_this_literal_sequence_number;
122 extern GTY(()) int alpha_this_gpdisp_sequence_number;
123 int alpha_this_literal_sequence_number;
124 int alpha_this_gpdisp_sequence_number;
125
126 /* Costs of various operations on the different architectures. */
127
128 struct alpha_rtx_cost_data
129 {
130 unsigned char fp_add;
131 unsigned char fp_mult;
132 unsigned char fp_div_sf;
133 unsigned char fp_div_df;
134 unsigned char int_mult_si;
135 unsigned char int_mult_di;
136 unsigned char int_shift;
137 unsigned char int_cmov;
138 unsigned short int_div;
139 };
140
141 static struct alpha_rtx_cost_data const alpha_rtx_cost_data[PROCESSOR_MAX] =
142 {
143 { /* EV4 */
144 COSTS_N_INSNS (6), /* fp_add */
145 COSTS_N_INSNS (6), /* fp_mult */
146 COSTS_N_INSNS (34), /* fp_div_sf */
147 COSTS_N_INSNS (63), /* fp_div_df */
148 COSTS_N_INSNS (23), /* int_mult_si */
149 COSTS_N_INSNS (23), /* int_mult_di */
150 COSTS_N_INSNS (2), /* int_shift */
151 COSTS_N_INSNS (2), /* int_cmov */
152 COSTS_N_INSNS (97), /* int_div */
153 },
154 { /* EV5 */
155 COSTS_N_INSNS (4), /* fp_add */
156 COSTS_N_INSNS (4), /* fp_mult */
157 COSTS_N_INSNS (15), /* fp_div_sf */
158 COSTS_N_INSNS (22), /* fp_div_df */
159 COSTS_N_INSNS (8), /* int_mult_si */
160 COSTS_N_INSNS (12), /* int_mult_di */
161 COSTS_N_INSNS (1) + 1, /* int_shift */
162 COSTS_N_INSNS (1), /* int_cmov */
163 COSTS_N_INSNS (83), /* int_div */
164 },
165 { /* EV6 */
166 COSTS_N_INSNS (4), /* fp_add */
167 COSTS_N_INSNS (4), /* fp_mult */
168 COSTS_N_INSNS (12), /* fp_div_sf */
169 COSTS_N_INSNS (15), /* fp_div_df */
170 COSTS_N_INSNS (7), /* int_mult_si */
171 COSTS_N_INSNS (7), /* int_mult_di */
172 COSTS_N_INSNS (1), /* int_shift */
173 COSTS_N_INSNS (2), /* int_cmov */
174 COSTS_N_INSNS (86), /* int_div */
175 },
176 };
177
178 /* Similar but tuned for code size instead of execution latency. The
179 extra +N is fractional cost tuning based on latency. It's used to
180 encourage use of cheaper insns like shift, but only if there's just
181 one of them. */
182
183 static struct alpha_rtx_cost_data const alpha_rtx_cost_size =
184 {
185 COSTS_N_INSNS (1), /* fp_add */
186 COSTS_N_INSNS (1), /* fp_mult */
187 COSTS_N_INSNS (1), /* fp_div_sf */
188 COSTS_N_INSNS (1) + 1, /* fp_div_df */
189 COSTS_N_INSNS (1) + 1, /* int_mult_si */
190 COSTS_N_INSNS (1) + 2, /* int_mult_di */
191 COSTS_N_INSNS (1), /* int_shift */
192 COSTS_N_INSNS (1), /* int_cmov */
193 COSTS_N_INSNS (6), /* int_div */
194 };
195
196 /* Get the number of args of a function in one of two ways. */
197 #if TARGET_ABI_OPEN_VMS
198 #define NUM_ARGS crtl->args.info.num_args
199 #else
200 #define NUM_ARGS crtl->args.info
201 #endif
202
203 #define REG_PV 27
204 #define REG_RA 26
205
206 /* Declarations of static functions. */
207 static struct machine_function *alpha_init_machine_status (void);
208 static rtx alpha_emit_xfloating_compare (enum rtx_code *, rtx, rtx);
209 static void alpha_handle_trap_shadows (void);
210 static void alpha_align_insns (void);
211 static void alpha_override_options_after_change (void);
212
213 #if TARGET_ABI_OPEN_VMS
214 static void alpha_write_linkage (FILE *, const char *);
215 static bool vms_valid_pointer_mode (scalar_int_mode);
216 #else
217 #define vms_patch_builtins() gcc_unreachable()
218 #endif
219
220 static unsigned int
222 rest_of_handle_trap_shadows (void)
223 {
224 alpha_handle_trap_shadows ();
225 return 0;
226 }
227
228 namespace {
229
230 const pass_data pass_data_handle_trap_shadows =
231 {
232 RTL_PASS,
233 "trap_shadows", /* name */
234 OPTGROUP_NONE, /* optinfo_flags */
235 TV_NONE, /* tv_id */
236 0, /* properties_required */
237 0, /* properties_provided */
238 0, /* properties_destroyed */
239 0, /* todo_flags_start */
240 TODO_df_finish, /* todo_flags_finish */
241 };
242
243 class pass_handle_trap_shadows : public rtl_opt_pass
244 {
245 public:
246 pass_handle_trap_shadows(gcc::context *ctxt)
247 : rtl_opt_pass(pass_data_handle_trap_shadows, ctxt)
248 {}
249
250 /* opt_pass methods: */
251 virtual bool gate (function *)
252 {
253 return alpha_tp != ALPHA_TP_PROG || flag_exceptions;
254 }
255
256 virtual unsigned int execute (function *)
257 {
258 return rest_of_handle_trap_shadows ();
259 }
260
261 }; // class pass_handle_trap_shadows
262
263 } // anon namespace
264
265 rtl_opt_pass *
266 make_pass_handle_trap_shadows (gcc::context *ctxt)
267 {
268 return new pass_handle_trap_shadows (ctxt);
269 }
270
271 static unsigned int
272 rest_of_align_insns (void)
273 {
274 alpha_align_insns ();
275 return 0;
276 }
277
278 namespace {
279
280 const pass_data pass_data_align_insns =
281 {
282 RTL_PASS,
283 "align_insns", /* name */
284 OPTGROUP_NONE, /* optinfo_flags */
285 TV_NONE, /* tv_id */
286 0, /* properties_required */
287 0, /* properties_provided */
288 0, /* properties_destroyed */
289 0, /* todo_flags_start */
290 TODO_df_finish, /* todo_flags_finish */
291 };
292
293 class pass_align_insns : public rtl_opt_pass
294 {
295 public:
296 pass_align_insns(gcc::context *ctxt)
297 : rtl_opt_pass(pass_data_align_insns, ctxt)
298 {}
299
300 /* opt_pass methods: */
301 virtual bool gate (function *)
302 {
303 /* Due to the number of extra trapb insns, don't bother fixing up
304 alignment when trap precision is instruction. Moreover, we can
305 only do our job when sched2 is run. */
306 return ((alpha_tune == PROCESSOR_EV4
307 || alpha_tune == PROCESSOR_EV5)
308 && optimize && !optimize_size
309 && alpha_tp != ALPHA_TP_INSN
310 && flag_schedule_insns_after_reload);
311 }
312
313 virtual unsigned int execute (function *)
314 {
315 return rest_of_align_insns ();
316 }
317
318 }; // class pass_align_insns
319
320 } // anon namespace
321
322 rtl_opt_pass *
323 make_pass_align_insns (gcc::context *ctxt)
324 {
325 return new pass_align_insns (ctxt);
326 }
327
328 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
329 /* Implement TARGET_MANGLE_TYPE. */
330
331 static const char *
332 alpha_mangle_type (const_tree type)
333 {
334 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
335 && TARGET_LONG_DOUBLE_128)
336 return "g";
337
338 /* For all other types, use normal C++ mangling. */
339 return NULL;
340 }
341 #endif
342
343 /* Parse target option strings. */
344
345 static void
346 alpha_option_override (void)
347 {
348 static const struct cpu_table {
349 const char *const name;
350 const enum processor_type processor;
351 const int flags;
352 const unsigned short line_size; /* in bytes */
353 const unsigned short l1_size; /* in kb. */
354 const unsigned short l2_size; /* in kb. */
355 } cpu_table[] = {
356 /* EV4/LCA45 had 8k L1 caches; EV45 had 16k L1 caches.
357 EV4/EV45 had 128k to 16M 32-byte direct Bcache. LCA45
358 had 64k to 8M 8-byte direct Bcache. */
359 { "ev4", PROCESSOR_EV4, 0, 32, 8, 8*1024 },
360 { "21064", PROCESSOR_EV4, 0, 32, 8, 8*1024 },
361 { "ev45", PROCESSOR_EV4, 0, 32, 16, 16*1024 },
362
363 /* EV5 or EV56 had 8k 32 byte L1, 96k 32 or 64 byte L2,
364 and 1M to 16M 64 byte L3 (not modeled).
365 PCA56 had 16k 64-byte cache; PCA57 had 32k Icache.
366 PCA56 had 8k 64-byte cache; PCA57 had 16k Dcache. */
367 { "ev5", PROCESSOR_EV5, 0, 32, 8, 96 },
368 { "21164", PROCESSOR_EV5, 0, 32, 8, 96 },
369 { "ev56", PROCESSOR_EV5, MASK_BWX, 32, 8, 96 },
370 { "21164a", PROCESSOR_EV5, MASK_BWX, 32, 8, 96 },
371 { "pca56", PROCESSOR_EV5, MASK_BWX|MASK_MAX, 64, 16, 4*1024 },
372 { "21164PC",PROCESSOR_EV5, MASK_BWX|MASK_MAX, 64, 16, 4*1024 },
373 { "21164pc",PROCESSOR_EV5, MASK_BWX|MASK_MAX, 64, 16, 4*1024 },
374
375 /* EV6 had 64k 64 byte L1, 1M to 16M Bcache. */
376 { "ev6", PROCESSOR_EV6, MASK_BWX|MASK_MAX|MASK_FIX, 64, 64, 16*1024 },
377 { "21264", PROCESSOR_EV6, MASK_BWX|MASK_MAX|MASK_FIX, 64, 64, 16*1024 },
378 { "ev67", PROCESSOR_EV6, MASK_BWX|MASK_MAX|MASK_FIX|MASK_CIX,
379 64, 64, 16*1024 },
380 { "21264a", PROCESSOR_EV6, MASK_BWX|MASK_MAX|MASK_FIX|MASK_CIX,
381 64, 64, 16*1024 }
382 };
383
384 int const ct_size = ARRAY_SIZE (cpu_table);
385 int line_size = 0, l1_size = 0, l2_size = 0;
386 int i;
387
388 #ifdef SUBTARGET_OVERRIDE_OPTIONS
389 SUBTARGET_OVERRIDE_OPTIONS;
390 #endif
391
392 /* Default to full IEEE compliance mode for Go language. */
393 if (strcmp (lang_hooks.name, "GNU Go") == 0
394 && !(target_flags_explicit & MASK_IEEE))
395 target_flags |= MASK_IEEE;
396
397 alpha_fprm = ALPHA_FPRM_NORM;
398 alpha_tp = ALPHA_TP_PROG;
399 alpha_fptm = ALPHA_FPTM_N;
400
401 if (TARGET_IEEE)
402 {
403 alpha_tp = ALPHA_TP_INSN;
404 alpha_fptm = ALPHA_FPTM_SU;
405 }
406 if (TARGET_IEEE_WITH_INEXACT)
407 {
408 alpha_tp = ALPHA_TP_INSN;
409 alpha_fptm = ALPHA_FPTM_SUI;
410 }
411
412 if (alpha_tp_string)
413 {
414 if (! strcmp (alpha_tp_string, "p"))
415 alpha_tp = ALPHA_TP_PROG;
416 else if (! strcmp (alpha_tp_string, "f"))
417 alpha_tp = ALPHA_TP_FUNC;
418 else if (! strcmp (alpha_tp_string, "i"))
419 alpha_tp = ALPHA_TP_INSN;
420 else
421 error ("bad value %qs for %<-mtrap-precision%> switch",
422 alpha_tp_string);
423 }
424
425 if (alpha_fprm_string)
426 {
427 if (! strcmp (alpha_fprm_string, "n"))
428 alpha_fprm = ALPHA_FPRM_NORM;
429 else if (! strcmp (alpha_fprm_string, "m"))
430 alpha_fprm = ALPHA_FPRM_MINF;
431 else if (! strcmp (alpha_fprm_string, "c"))
432 alpha_fprm = ALPHA_FPRM_CHOP;
433 else if (! strcmp (alpha_fprm_string,"d"))
434 alpha_fprm = ALPHA_FPRM_DYN;
435 else
436 error ("bad value %qs for %<-mfp-rounding-mode%> switch",
437 alpha_fprm_string);
438 }
439
440 if (alpha_fptm_string)
441 {
442 if (strcmp (alpha_fptm_string, "n") == 0)
443 alpha_fptm = ALPHA_FPTM_N;
444 else if (strcmp (alpha_fptm_string, "u") == 0)
445 alpha_fptm = ALPHA_FPTM_U;
446 else if (strcmp (alpha_fptm_string, "su") == 0)
447 alpha_fptm = ALPHA_FPTM_SU;
448 else if (strcmp (alpha_fptm_string, "sui") == 0)
449 alpha_fptm = ALPHA_FPTM_SUI;
450 else
451 error ("bad value %qs for %<-mfp-trap-mode%> switch",
452 alpha_fptm_string);
453 }
454
455 if (alpha_cpu_string)
456 {
457 for (i = 0; i < ct_size; i++)
458 if (! strcmp (alpha_cpu_string, cpu_table [i].name))
459 {
460 alpha_tune = alpha_cpu = cpu_table[i].processor;
461 line_size = cpu_table[i].line_size;
462 l1_size = cpu_table[i].l1_size;
463 l2_size = cpu_table[i].l2_size;
464 target_flags &= ~ (MASK_BWX | MASK_MAX | MASK_FIX | MASK_CIX);
465 target_flags |= cpu_table[i].flags;
466 break;
467 }
468 if (i == ct_size)
469 error ("bad value %qs for %<-mcpu%> switch", alpha_cpu_string);
470 }
471
472 if (alpha_tune_string)
473 {
474 for (i = 0; i < ct_size; i++)
475 if (! strcmp (alpha_tune_string, cpu_table [i].name))
476 {
477 alpha_tune = cpu_table[i].processor;
478 line_size = cpu_table[i].line_size;
479 l1_size = cpu_table[i].l1_size;
480 l2_size = cpu_table[i].l2_size;
481 break;
482 }
483 if (i == ct_size)
484 error ("bad value %qs for %<-mtune%> switch", alpha_tune_string);
485 }
486
487 if (line_size)
488 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
489 param_l1_cache_line_size, line_size);
490 if (l1_size)
491 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
492 param_l1_cache_size, l1_size);
493 if (l2_size)
494 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
495 param_l2_cache_size, l2_size);
496
497 /* Do some sanity checks on the above options. */
498
499 if ((alpha_fptm == ALPHA_FPTM_SU || alpha_fptm == ALPHA_FPTM_SUI)
500 && alpha_tp != ALPHA_TP_INSN && alpha_cpu != PROCESSOR_EV6)
501 {
502 warning (0, "fp software completion requires %<-mtrap-precision=i%>");
503 alpha_tp = ALPHA_TP_INSN;
504 }
505
506 if (alpha_cpu == PROCESSOR_EV6)
507 {
508 /* Except for EV6 pass 1 (not released), we always have precise
509 arithmetic traps. Which means we can do software completion
510 without minding trap shadows. */
511 alpha_tp = ALPHA_TP_PROG;
512 }
513
514 if (TARGET_FLOAT_VAX)
515 {
516 if (alpha_fprm == ALPHA_FPRM_MINF || alpha_fprm == ALPHA_FPRM_DYN)
517 {
518 warning (0, "rounding mode not supported for VAX floats");
519 alpha_fprm = ALPHA_FPRM_NORM;
520 }
521 if (alpha_fptm == ALPHA_FPTM_SUI)
522 {
523 warning (0, "trap mode not supported for VAX floats");
524 alpha_fptm = ALPHA_FPTM_SU;
525 }
526 if (target_flags_explicit & MASK_LONG_DOUBLE_128)
527 warning (0, "128-bit %<long double%> not supported for VAX floats");
528 target_flags &= ~MASK_LONG_DOUBLE_128;
529 }
530
531 {
532 char *end;
533 int lat;
534
535 if (!alpha_mlat_string)
536 alpha_mlat_string = "L1";
537
538 if (ISDIGIT ((unsigned char)alpha_mlat_string[0])
539 && (lat = strtol (alpha_mlat_string, &end, 10), *end == '\0'))
540 ;
541 else if ((alpha_mlat_string[0] == 'L' || alpha_mlat_string[0] == 'l')
542 && ISDIGIT ((unsigned char)alpha_mlat_string[1])
543 && alpha_mlat_string[2] == '\0')
544 {
545 static int const cache_latency[][4] =
546 {
547 { 3, 30, -1 }, /* ev4 -- Bcache is a guess */
548 { 2, 12, 38 }, /* ev5 -- Bcache from PC164 LMbench numbers */
549 { 3, 12, 30 }, /* ev6 -- Bcache from DS20 LMbench. */
550 };
551
552 lat = alpha_mlat_string[1] - '0';
553 if (lat <= 0 || lat > 3 || cache_latency[alpha_tune][lat-1] == -1)
554 {
555 warning (0, "L%d cache latency unknown for %s",
556 lat, alpha_cpu_name[alpha_tune]);
557 lat = 3;
558 }
559 else
560 lat = cache_latency[alpha_tune][lat-1];
561 }
562 else if (! strcmp (alpha_mlat_string, "main"))
563 {
564 /* Most current memories have about 370ns latency. This is
565 a reasonable guess for a fast cpu. */
566 lat = 150;
567 }
568 else
569 {
570 warning (0, "bad value %qs for %<-mmemory-latency%>",
571 alpha_mlat_string);
572 lat = 3;
573 }
574
575 alpha_memory_latency = lat;
576 }
577
578 /* Default the definition of "small data" to 8 bytes. */
579 if (!OPTION_SET_P (g_switch_value))
580 g_switch_value = 8;
581
582 /* Infer TARGET_SMALL_DATA from -fpic/-fPIC. */
583 if (flag_pic == 1)
584 target_flags |= MASK_SMALL_DATA;
585 else if (flag_pic == 2)
586 target_flags &= ~MASK_SMALL_DATA;
587
588 alpha_override_options_after_change ();
589
590 /* Register variables and functions with the garbage collector. */
591
592 /* Set up function hooks. */
593 init_machine_status = alpha_init_machine_status;
594
595 /* Tell the compiler when we're using VAX floating point. */
596 if (TARGET_FLOAT_VAX)
597 {
598 REAL_MODE_FORMAT (SFmode) = &vax_f_format;
599 REAL_MODE_FORMAT (DFmode) = &vax_g_format;
600 REAL_MODE_FORMAT (TFmode) = NULL;
601 }
602
603 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
604 if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
605 target_flags |= MASK_LONG_DOUBLE_128;
606 #endif
607
608 }
609
610 /* Implement targetm.override_options_after_change. */
611
612 static void
613 alpha_override_options_after_change (void)
614 {
615 /* Align labels and loops for optimal branching. */
616 /* ??? Kludge these by not doing anything if we don't optimize. */
617 if (optimize > 0)
618 {
619 if (flag_align_loops && !str_align_loops)
620 str_align_loops = "16";
621 if (flag_align_jumps && !str_align_jumps)
622 str_align_jumps = "16";
623 }
624 if (flag_align_functions && !str_align_functions)
625 str_align_functions = "16";
626 }
627
628 /* Returns 1 if VALUE is a mask that contains full bytes of zero or ones. */
630
631 int
632 zap_mask (HOST_WIDE_INT value)
633 {
634 int i;
635
636 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
637 i++, value >>= 8)
638 if ((value & 0xff) != 0 && (value & 0xff) != 0xff)
639 return 0;
640
641 return 1;
642 }
643
644 /* Return true if OP is valid for a particular TLS relocation.
645 We are already guaranteed that OP is a CONST. */
646
647 int
648 tls_symbolic_operand_1 (rtx op, int size, int unspec)
649 {
650 op = XEXP (op, 0);
651
652 if (GET_CODE (op) != UNSPEC || XINT (op, 1) != unspec)
653 return 0;
654 op = XVECEXP (op, 0, 0);
655
656 if (GET_CODE (op) != SYMBOL_REF)
657 return 0;
658
659 switch (SYMBOL_REF_TLS_MODEL (op))
660 {
661 case TLS_MODEL_LOCAL_DYNAMIC:
662 return unspec == UNSPEC_DTPREL && size == alpha_tls_size;
663 case TLS_MODEL_INITIAL_EXEC:
664 return unspec == UNSPEC_TPREL && size == 64;
665 case TLS_MODEL_LOCAL_EXEC:
666 return unspec == UNSPEC_TPREL && size == alpha_tls_size;
667 default:
668 gcc_unreachable ();
669 }
670 }
671
672 /* Used by aligned_memory_operand and unaligned_memory_operand to
673 resolve what reload is going to do with OP if it's a register. */
674
675 rtx
676 resolve_reload_operand (rtx op)
677 {
678 if (reload_in_progress)
679 {
680 rtx tmp = op;
681 if (SUBREG_P (tmp))
682 tmp = SUBREG_REG (tmp);
683 if (REG_P (tmp)
684 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER)
685 {
686 op = reg_equiv_memory_loc (REGNO (tmp));
687 if (op == 0)
688 return 0;
689 }
690 }
691 return op;
692 }
693
694 /* The scalar modes supported differs from the default check-what-c-supports
695 version in that sometimes TFmode is available even when long double
696 indicates only DFmode. */
697
698 static bool
699 alpha_scalar_mode_supported_p (scalar_mode mode)
700 {
701 switch (mode)
702 {
703 case E_QImode:
704 case E_HImode:
705 case E_SImode:
706 case E_DImode:
707 case E_TImode: /* via optabs.cc */
708 return true;
709
710 case E_SFmode:
711 case E_DFmode:
712 return true;
713
714 case E_TFmode:
715 return TARGET_HAS_XFLOATING_LIBS;
716
717 default:
718 return false;
719 }
720 }
721
722 /* Alpha implements a couple of integer vector mode operations when
723 TARGET_MAX is enabled. We do not check TARGET_MAX here, however,
724 which allows the vectorizer to operate on e.g. move instructions,
725 or when expand_vector_operations can do something useful. */
726
727 static bool
728 alpha_vector_mode_supported_p (machine_mode mode)
729 {
730 return mode == V8QImode || mode == V4HImode || mode == V2SImode;
731 }
732
733 /* Return the TLS model to use for SYMBOL. */
734
735 static enum tls_model
736 tls_symbolic_operand_type (rtx symbol)
737 {
738 enum tls_model model;
739
740 if (GET_CODE (symbol) != SYMBOL_REF)
741 return TLS_MODEL_NONE;
742 model = SYMBOL_REF_TLS_MODEL (symbol);
743
744 /* Local-exec with a 64-bit size is the same code as initial-exec. */
745 if (model == TLS_MODEL_LOCAL_EXEC && alpha_tls_size == 64)
746 model = TLS_MODEL_INITIAL_EXEC;
747
748 return model;
749 }
750
751 /* Return true if the function DECL will share the same GP as any
753 function in the current unit of translation. */
754
755 static bool
756 decl_has_samegp (const_tree decl)
757 {
758 /* Functions that are not local can be overridden, and thus may
759 not share the same gp. */
760 if (!(*targetm.binds_local_p) (decl))
761 return false;
762
763 /* If -msmall-data is in effect, assume that there is only one GP
764 for the module, and so any local symbol has this property. We
765 need explicit relocations to be able to enforce this for symbols
766 not defined in this unit of translation, however. */
767 if (TARGET_EXPLICIT_RELOCS && TARGET_SMALL_DATA)
768 return true;
769
770 /* Functions that are not external are defined in this UoT. */
771 /* ??? Irritatingly, static functions not yet emitted are still
772 marked "external". Apply this to non-static functions only. */
773 return !TREE_PUBLIC (decl) || !DECL_EXTERNAL (decl);
774 }
775
776 /* Return true if EXP should be placed in the small data section. */
777
778 static bool
779 alpha_in_small_data_p (const_tree exp)
780 {
781 /* We want to merge strings, so we never consider them small data. */
782 if (TREE_CODE (exp) == STRING_CST)
783 return false;
784
785 /* Functions are never in the small data area. Duh. */
786 if (TREE_CODE (exp) == FUNCTION_DECL)
787 return false;
788
789 /* COMMON symbols are never small data. */
790 if (TREE_CODE (exp) == VAR_DECL && DECL_COMMON (exp))
791 return false;
792
793 if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
794 {
795 const char *section = DECL_SECTION_NAME (exp);
796 if (strcmp (section, ".sdata") == 0
797 || strcmp (section, ".sbss") == 0)
798 return true;
799 }
800 else
801 {
802 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
803
804 /* If this is an incomplete type with size 0, then we can't put it
805 in sdata because it might be too big when completed. */
806 if (size > 0 && size <= g_switch_value)
807 return true;
808 }
809
810 return false;
811 }
812
813 #if TARGET_ABI_OPEN_VMS
814 static bool
815 vms_valid_pointer_mode (scalar_int_mode mode)
816 {
817 return (mode == SImode || mode == DImode);
818 }
819
820 static bool
821 alpha_linkage_symbol_p (const char *symname)
822 {
823 int symlen = strlen (symname);
824
825 if (symlen > 4)
826 return strcmp (&symname [symlen - 4], "..lk") == 0;
827
828 return false;
829 }
830
831 #define LINKAGE_SYMBOL_REF_P(X) \
832 ((GET_CODE (X) == SYMBOL_REF \
833 && alpha_linkage_symbol_p (XSTR (X, 0))) \
834 || (GET_CODE (X) == CONST \
835 && GET_CODE (XEXP (X, 0)) == PLUS \
836 && GET_CODE (XEXP (XEXP (X, 0), 0)) == SYMBOL_REF \
837 && alpha_linkage_symbol_p (XSTR (XEXP (XEXP (X, 0), 0), 0))))
838 #endif
839
840 /* legitimate_address_p recognizes an RTL expression that is a valid
841 memory address for an instruction. The MODE argument is the
842 machine mode for the MEM expression that wants to use this address.
843
844 For Alpha, we have either a constant address or the sum of a
845 register and a constant address, or just a register. For DImode,
846 any of those forms can be surrounded with an AND that clear the
847 low-order three bits; this is an "unaligned" access. */
848
849 static bool
850 alpha_legitimate_address_p (machine_mode mode, rtx x, bool strict)
851 {
852 /* If this is an ldq_u type address, discard the outer AND. */
853 if (mode == DImode
854 && GET_CODE (x) == AND
855 && CONST_INT_P (XEXP (x, 1))
856 && INTVAL (XEXP (x, 1)) == -8)
857 x = XEXP (x, 0);
858
859 /* Discard non-paradoxical subregs. */
860 if (SUBREG_P (x)
861 && (GET_MODE_SIZE (GET_MODE (x))
862 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
863 x = SUBREG_REG (x);
864
865 /* Unadorned general registers are valid. */
866 if (REG_P (x)
867 && (strict
868 ? STRICT_REG_OK_FOR_BASE_P (x)
869 : NONSTRICT_REG_OK_FOR_BASE_P (x)))
870 return true;
871
872 /* Constant addresses (i.e. +/- 32k) are valid. */
873 if (CONSTANT_ADDRESS_P (x))
874 return true;
875
876 #if TARGET_ABI_OPEN_VMS
877 if (LINKAGE_SYMBOL_REF_P (x))
878 return true;
879 #endif
880
881 /* Register plus a small constant offset is valid. */
882 if (GET_CODE (x) == PLUS)
883 {
884 rtx ofs = XEXP (x, 1);
885 x = XEXP (x, 0);
886
887 /* Discard non-paradoxical subregs. */
888 if (SUBREG_P (x)
889 && (GET_MODE_SIZE (GET_MODE (x))
890 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
891 x = SUBREG_REG (x);
892
893 if (REG_P (x))
894 {
895 if (! strict
896 && NONSTRICT_REG_OK_FP_BASE_P (x)
897 && CONST_INT_P (ofs))
898 return true;
899 if ((strict
900 ? STRICT_REG_OK_FOR_BASE_P (x)
901 : NONSTRICT_REG_OK_FOR_BASE_P (x))
902 && CONSTANT_ADDRESS_P (ofs))
903 return true;
904 }
905 }
906
907 /* If we're managing explicit relocations, LO_SUM is valid, as are small
908 data symbols. Avoid explicit relocations of modes larger than word
909 mode since i.e. $LC0+8($1) can fold around +/- 32k offset. */
910 else if (TARGET_EXPLICIT_RELOCS
911 && GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
912 {
913 if (small_symbolic_operand (x, Pmode))
914 return true;
915
916 if (GET_CODE (x) == LO_SUM)
917 {
918 rtx ofs = XEXP (x, 1);
919 x = XEXP (x, 0);
920
921 /* Discard non-paradoxical subregs. */
922 if (SUBREG_P (x)
923 && (GET_MODE_SIZE (GET_MODE (x))
924 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
925 x = SUBREG_REG (x);
926
927 /* Must have a valid base register. */
928 if (! (REG_P (x)
929 && (strict
930 ? STRICT_REG_OK_FOR_BASE_P (x)
931 : NONSTRICT_REG_OK_FOR_BASE_P (x))))
932 return false;
933
934 /* The symbol must be local. */
935 if (local_symbolic_operand (ofs, Pmode)
936 || dtp32_symbolic_operand (ofs, Pmode)
937 || tp32_symbolic_operand (ofs, Pmode))
938 return true;
939 }
940 }
941
942 return false;
943 }
944
945 /* Build the SYMBOL_REF for __tls_get_addr. */
946
947 static GTY(()) rtx tls_get_addr_libfunc;
948
949 static rtx
950 get_tls_get_addr (void)
951 {
952 if (!tls_get_addr_libfunc)
953 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
954 return tls_get_addr_libfunc;
955 }
956
957 /* Try machine-dependent ways of modifying an illegitimate address
958 to be legitimate. If we find one, return the new, valid address. */
959
960 static rtx
961 alpha_legitimize_address_1 (rtx x, rtx scratch, machine_mode mode)
962 {
963 HOST_WIDE_INT addend;
964
965 /* If the address is (plus reg const_int) and the CONST_INT is not a
966 valid offset, compute the high part of the constant and add it to
967 the register. Then our address is (plus temp low-part-const). */
968 if (GET_CODE (x) == PLUS
969 && REG_P (XEXP (x, 0))
970 && CONST_INT_P (XEXP (x, 1))
971 && ! CONSTANT_ADDRESS_P (XEXP (x, 1)))
972 {
973 addend = INTVAL (XEXP (x, 1));
974 x = XEXP (x, 0);
975 goto split_addend;
976 }
977
978 /* If the address is (const (plus FOO const_int)), find the low-order
979 part of the CONST_INT. Then load FOO plus any high-order part of the
980 CONST_INT into a register. Our address is (plus reg low-part-const).
981 This is done to reduce the number of GOT entries. */
982 if (can_create_pseudo_p ()
983 && GET_CODE (x) == CONST
984 && GET_CODE (XEXP (x, 0)) == PLUS
985 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
986 {
987 addend = INTVAL (XEXP (XEXP (x, 0), 1));
988 x = force_reg (Pmode, XEXP (XEXP (x, 0), 0));
989 goto split_addend;
990 }
991
992 /* If we have a (plus reg const), emit the load as in (2), then add
993 the two registers, and finally generate (plus reg low-part-const) as
994 our address. */
995 if (can_create_pseudo_p ()
996 && GET_CODE (x) == PLUS
997 && REG_P (XEXP (x, 0))
998 && GET_CODE (XEXP (x, 1)) == CONST
999 && GET_CODE (XEXP (XEXP (x, 1), 0)) == PLUS
1000 && CONST_INT_P (XEXP (XEXP (XEXP (x, 1), 0), 1)))
1001 {
1002 addend = INTVAL (XEXP (XEXP (XEXP (x, 1), 0), 1));
1003 x = expand_simple_binop (Pmode, PLUS, XEXP (x, 0),
1004 XEXP (XEXP (XEXP (x, 1), 0), 0),
1005 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1006 goto split_addend;
1007 }
1008
1009 /* If this is a local symbol, split the address into HIGH/LO_SUM parts.
1010 Avoid modes larger than word mode since i.e. $LC0+8($1) can fold
1011 around +/- 32k offset. */
1012 if (TARGET_EXPLICIT_RELOCS
1013 && GET_MODE_SIZE (mode) <= UNITS_PER_WORD
1014 && symbolic_operand (x, Pmode))
1015 {
1016 rtx r0, r16, eqv, tga, tp, dest, seq;
1017 rtx_insn *insn;
1018
1019 switch (tls_symbolic_operand_type (x))
1020 {
1021 case TLS_MODEL_NONE:
1022 break;
1023
1024 case TLS_MODEL_GLOBAL_DYNAMIC:
1025 {
1026 start_sequence ();
1027
1028 r0 = gen_rtx_REG (Pmode, 0);
1029 r16 = gen_rtx_REG (Pmode, 16);
1030 tga = get_tls_get_addr ();
1031 dest = gen_reg_rtx (Pmode);
1032 seq = GEN_INT (alpha_next_sequence_number++);
1033
1034 emit_insn (gen_movdi_er_tlsgd (r16, pic_offset_table_rtx, x, seq));
1035 rtx val = gen_call_value_osf_tlsgd (r0, tga, seq);
1036 insn = emit_call_insn (val);
1037 RTL_CONST_CALL_P (insn) = 1;
1038 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r16);
1039
1040 insn = get_insns ();
1041 end_sequence ();
1042
1043 emit_libcall_block (insn, dest, r0, x);
1044 return dest;
1045 }
1046
1047 case TLS_MODEL_LOCAL_DYNAMIC:
1048 {
1049 start_sequence ();
1050
1051 r0 = gen_rtx_REG (Pmode, 0);
1052 r16 = gen_rtx_REG (Pmode, 16);
1053 tga = get_tls_get_addr ();
1054 scratch = gen_reg_rtx (Pmode);
1055 seq = GEN_INT (alpha_next_sequence_number++);
1056
1057 emit_insn (gen_movdi_er_tlsldm (r16, pic_offset_table_rtx, seq));
1058 rtx val = gen_call_value_osf_tlsldm (r0, tga, seq);
1059 insn = emit_call_insn (val);
1060 RTL_CONST_CALL_P (insn) = 1;
1061 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r16);
1062
1063 insn = get_insns ();
1064 end_sequence ();
1065
1066 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1067 UNSPEC_TLSLDM_CALL);
1068 emit_libcall_block (insn, scratch, r0, eqv);
1069
1070 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPREL);
1071 eqv = gen_rtx_CONST (Pmode, eqv);
1072
1073 if (alpha_tls_size == 64)
1074 {
1075 dest = gen_reg_rtx (Pmode);
1076 emit_insn (gen_rtx_SET (dest, eqv));
1077 emit_insn (gen_adddi3 (dest, dest, scratch));
1078 return dest;
1079 }
1080 if (alpha_tls_size == 32)
1081 {
1082 rtx temp = gen_rtx_HIGH (Pmode, eqv);
1083 temp = gen_rtx_PLUS (Pmode, scratch, temp);
1084 scratch = gen_reg_rtx (Pmode);
1085 emit_insn (gen_rtx_SET (scratch, temp));
1086 }
1087 return gen_rtx_LO_SUM (Pmode, scratch, eqv);
1088 }
1089
1090 case TLS_MODEL_INITIAL_EXEC:
1091 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_TPREL);
1092 eqv = gen_rtx_CONST (Pmode, eqv);
1093 tp = gen_reg_rtx (Pmode);
1094 scratch = gen_reg_rtx (Pmode);
1095 dest = gen_reg_rtx (Pmode);
1096
1097 emit_insn (gen_get_thread_pointerdi (tp));
1098 emit_insn (gen_rtx_SET (scratch, eqv));
1099 emit_insn (gen_adddi3 (dest, tp, scratch));
1100 return dest;
1101
1102 case TLS_MODEL_LOCAL_EXEC:
1103 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_TPREL);
1104 eqv = gen_rtx_CONST (Pmode, eqv);
1105 tp = gen_reg_rtx (Pmode);
1106
1107 emit_insn (gen_get_thread_pointerdi (tp));
1108 if (alpha_tls_size == 32)
1109 {
1110 rtx temp = gen_rtx_HIGH (Pmode, eqv);
1111 temp = gen_rtx_PLUS (Pmode, tp, temp);
1112 tp = gen_reg_rtx (Pmode);
1113 emit_insn (gen_rtx_SET (tp, temp));
1114 }
1115 return gen_rtx_LO_SUM (Pmode, tp, eqv);
1116
1117 default:
1118 gcc_unreachable ();
1119 }
1120
1121 if (local_symbolic_operand (x, Pmode))
1122 {
1123 if (small_symbolic_operand (x, Pmode))
1124 return x;
1125 else
1126 {
1127 if (can_create_pseudo_p ())
1128 scratch = gen_reg_rtx (Pmode);
1129 emit_insn (gen_rtx_SET (scratch, gen_rtx_HIGH (Pmode, x)));
1130 return gen_rtx_LO_SUM (Pmode, scratch, x);
1131 }
1132 }
1133 }
1134
1135 return NULL;
1136
1137 split_addend:
1138 {
1139 HOST_WIDE_INT low, high;
1140
1141 low = ((addend & 0xffff) ^ 0x8000) - 0x8000;
1142 addend -= low;
1143 high = ((addend & 0xffffffff) ^ 0x80000000) - 0x80000000;
1144 addend -= high;
1145
1146 if (addend)
1147 x = expand_simple_binop (Pmode, PLUS, x, GEN_INT (addend),
1148 (!can_create_pseudo_p () ? scratch : NULL_RTX),
1149 1, OPTAB_LIB_WIDEN);
1150 if (high)
1151 x = expand_simple_binop (Pmode, PLUS, x, GEN_INT (high),
1152 (!can_create_pseudo_p () ? scratch : NULL_RTX),
1153 1, OPTAB_LIB_WIDEN);
1154
1155 return plus_constant (Pmode, x, low);
1156 }
1157 }
1158
1159
1160 /* Try machine-dependent ways of modifying an illegitimate address
1161 to be legitimate. Return X or the new, valid address. */
1162
1163 static rtx
1164 alpha_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1165 machine_mode mode)
1166 {
1167 rtx new_x = alpha_legitimize_address_1 (x, NULL_RTX, mode);
1168 return new_x ? new_x : x;
1169 }
1170
1171 /* Return true if ADDR has an effect that depends on the machine mode it
1172 is used for. On the Alpha this is true only for the unaligned modes.
1173 We can simplify the test since we know that the address must be valid. */
1174
1175 static bool
1176 alpha_mode_dependent_address_p (const_rtx addr,
1177 addr_space_t as ATTRIBUTE_UNUSED)
1178 {
1179 return GET_CODE (addr) == AND;
1180 }
1181
1182 /* Primarily this is required for TLS symbols, but given that our move
1183 patterns *ought* to be able to handle any symbol at any time, we
1184 should never be spilling symbolic operands to the constant pool, ever. */
1185
1186 static bool
1187 alpha_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1188 {
1189 enum rtx_code code = GET_CODE (x);
1190 return code == SYMBOL_REF || code == LABEL_REF || code == CONST;
1191 }
1192
1193 /* We do not allow indirect calls to be optimized into sibling calls, nor
1194 can we allow a call to a function with a different GP to be optimized
1195 into a sibcall. */
1196
1197 static bool
1198 alpha_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
1199 {
1200 /* Can't do indirect tail calls, since we don't know if the target
1201 uses the same GP. */
1202 if (!decl)
1203 return false;
1204
1205 /* Otherwise, we can make a tail call if the target function shares
1206 the same GP. */
1207 return decl_has_samegp (decl);
1208 }
1209
1210 bool
1211 some_small_symbolic_operand_int (rtx x)
1212 {
1213 subrtx_var_iterator::array_type array;
1214 FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
1215 {
1216 rtx x = *iter;
1217 /* Don't re-split. */
1218 if (GET_CODE (x) == LO_SUM)
1219 iter.skip_subrtxes ();
1220 else if (small_symbolic_operand (x, Pmode))
1221 return true;
1222 }
1223 return false;
1224 }
1225
1226 rtx
1227 split_small_symbolic_operand (rtx x)
1228 {
1229 x = copy_insn (x);
1230 subrtx_ptr_iterator::array_type array;
1231 FOR_EACH_SUBRTX_PTR (iter, array, &x, ALL)
1232 {
1233 rtx *ptr = *iter;
1234 rtx x = *ptr;
1235 /* Don't re-split. */
1236 if (GET_CODE (x) == LO_SUM)
1237 iter.skip_subrtxes ();
1238 else if (small_symbolic_operand (x, Pmode))
1239 {
1240 *ptr = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, x);
1241 iter.skip_subrtxes ();
1242 }
1243 }
1244 return x;
1245 }
1246
1247 /* Indicate that INSN cannot be duplicated. This is true for any insn
1248 that we've marked with gpdisp relocs, since those have to stay in
1249 1-1 correspondence with one another.
1250
1251 Technically we could copy them if we could set up a mapping from one
1252 sequence number to another, across the set of insns to be duplicated.
1253 This seems overly complicated and error-prone since interblock motion
1254 from sched-ebb could move one of the pair of insns to a different block.
1255
1256 Also cannot allow jsr insns to be duplicated. If they throw exceptions,
1257 then they'll be in a different block from their ldgp. Which could lead
1258 the bb reorder code to think that it would be ok to copy just the block
1259 containing the call and branch to the block containing the ldgp. */
1260
1261 static bool
1262 alpha_cannot_copy_insn_p (rtx_insn *insn)
1263 {
1264 if (!reload_completed || !TARGET_EXPLICIT_RELOCS)
1265 return false;
1266 if (recog_memoized (insn) >= 0)
1267 return get_attr_cannot_copy (insn);
1268 else
1269 return false;
1270 }
1271
1272
1273 /* Try a machine-dependent way of reloading an illegitimate address
1274 operand. If we find one, push the reload and return the new rtx. */
1275
1276 rtx
1277 alpha_legitimize_reload_address (rtx x,
1278 machine_mode mode ATTRIBUTE_UNUSED,
1279 int opnum, int type,
1280 int ind_levels ATTRIBUTE_UNUSED)
1281 {
1282 /* We must recognize output that we have already generated ourselves. */
1283 if (GET_CODE (x) == PLUS
1284 && GET_CODE (XEXP (x, 0)) == PLUS
1285 && REG_P (XEXP (XEXP (x, 0), 0))
1286 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
1287 && CONST_INT_P (XEXP (x, 1)))
1288 {
1289 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1290 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
1291 opnum, (enum reload_type) type);
1292 return x;
1293 }
1294
1295 /* We wish to handle large displacements off a base register by
1296 splitting the addend across an ldah and the mem insn. This
1297 cuts number of extra insns needed from 3 to 1. */
1298 if (GET_CODE (x) == PLUS
1299 && REG_P (XEXP (x, 0))
1300 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
1301 && REGNO_OK_FOR_BASE_P (REGNO (XEXP (x, 0)))
1302 && CONST_INT_P (XEXP (x, 1)))
1303 {
1304 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
1305 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
1306 HOST_WIDE_INT high
1307 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
1308
1309 /* Check for 32-bit overflow. */
1310 if (high + low != val)
1311 return NULL_RTX;
1312
1313 /* Reload the high part into a base reg; leave the low part
1314 in the mem directly. */
1315 x = gen_rtx_PLUS (GET_MODE (x),
1316 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
1317 GEN_INT (high)),
1318 GEN_INT (low));
1319
1320 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1321 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
1322 opnum, (enum reload_type) type);
1323 return x;
1324 }
1325
1326 return NULL_RTX;
1327 }
1328
1329 /* Return the cost of moving between registers of various classes. Moving
1331 between FLOAT_REGS and anything else except float regs is expensive.
1332 In fact, we make it quite expensive because we really don't want to
1333 do these moves unless it is clearly worth it. Optimizations may
1334 reduce the impact of not being able to allocate a pseudo to a
1335 hard register. */
1336
1337 static int
1338 alpha_register_move_cost (machine_mode /*mode*/,
1339 reg_class_t from, reg_class_t to)
1340 {
1341 if ((from == FLOAT_REGS) == (to == FLOAT_REGS))
1342 return 2;
1343
1344 if (TARGET_FIX)
1345 return (from == FLOAT_REGS) ? 6 : 8;
1346
1347 return 4 + 2 * alpha_memory_latency;
1348 }
1349
1350 /* Return the cost of moving data of MODE from a register to
1351 or from memory. On the Alpha, bump this up a bit. */
1352
1353 static int
1354 alpha_memory_move_cost (machine_mode /*mode*/, reg_class_t /*regclass*/,
1355 bool /*in*/)
1356 {
1357 return 2 * alpha_memory_latency;
1358 }
1359
1360 /* Compute a (partial) cost for rtx X. Return true if the complete
1361 cost has been computed, and false if subexpressions should be
1362 scanned. In either case, *TOTAL contains the cost result. */
1363
1364 static bool
1365 alpha_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno, int *total,
1366 bool speed)
1367 {
1368 int code = GET_CODE (x);
1369 bool float_mode_p = FLOAT_MODE_P (mode);
1370 const struct alpha_rtx_cost_data *cost_data;
1371
1372 if (!speed)
1373 cost_data = &alpha_rtx_cost_size;
1374 else
1375 cost_data = &alpha_rtx_cost_data[alpha_tune];
1376
1377 switch (code)
1378 {
1379 case CONST_INT:
1380 /* If this is an 8-bit constant, return zero since it can be used
1381 nearly anywhere with no cost. If it is a valid operand for an
1382 ADD or AND, likewise return 0 if we know it will be used in that
1383 context. Otherwise, return 2 since it might be used there later.
1384 All other constants take at least two insns. */
1385 if (INTVAL (x) >= 0 && INTVAL (x) < 256)
1386 {
1387 *total = 0;
1388 return true;
1389 }
1390 /* FALLTHRU */
1391
1392 case CONST_DOUBLE:
1393 case CONST_WIDE_INT:
1394 if (x == CONST0_RTX (mode))
1395 *total = 0;
1396 else if ((outer_code == PLUS && add_operand (x, VOIDmode))
1397 || (outer_code == AND && and_operand (x, VOIDmode)))
1398 *total = 0;
1399 else if (add_operand (x, VOIDmode) || and_operand (x, VOIDmode))
1400 *total = 2;
1401 else
1402 *total = COSTS_N_INSNS (2);
1403 return true;
1404
1405 case CONST:
1406 case SYMBOL_REF:
1407 case LABEL_REF:
1408 if (TARGET_EXPLICIT_RELOCS && small_symbolic_operand (x, VOIDmode))
1409 *total = COSTS_N_INSNS (outer_code != MEM);
1410 else if (TARGET_EXPLICIT_RELOCS && local_symbolic_operand (x, VOIDmode))
1411 *total = COSTS_N_INSNS (1 + (outer_code != MEM));
1412 else if (tls_symbolic_operand_type (x))
1413 /* Estimate of cost for call_pal rduniq. */
1414 /* ??? How many insns do we emit here? More than one... */
1415 *total = COSTS_N_INSNS (15);
1416 else
1417 /* Otherwise we do a load from the GOT. */
1418 *total = COSTS_N_INSNS (!speed ? 1 : alpha_memory_latency);
1419 return true;
1420
1421 case HIGH:
1422 /* This is effectively an add_operand. */
1423 *total = 2;
1424 return true;
1425
1426 case PLUS:
1427 case MINUS:
1428 if (float_mode_p)
1429 *total = cost_data->fp_add;
1430 else if (GET_CODE (XEXP (x, 0)) == ASHIFT
1431 && const23_operand (XEXP (XEXP (x, 0), 1), VOIDmode))
1432 {
1433 *total = (rtx_cost (XEXP (XEXP (x, 0), 0), mode,
1434 (enum rtx_code) outer_code, opno, speed)
1435 + rtx_cost (XEXP (x, 1), mode,
1436 (enum rtx_code) outer_code, opno, speed)
1437 + COSTS_N_INSNS (1));
1438 return true;
1439 }
1440 return false;
1441
1442 case MULT:
1443 if (float_mode_p)
1444 *total = cost_data->fp_mult;
1445 else if (mode == DImode)
1446 *total = cost_data->int_mult_di;
1447 else
1448 *total = cost_data->int_mult_si;
1449 return false;
1450
1451 case ASHIFT:
1452 if (CONST_INT_P (XEXP (x, 1))
1453 && INTVAL (XEXP (x, 1)) <= 3)
1454 {
1455 *total = COSTS_N_INSNS (1);
1456 return false;
1457 }
1458 /* FALLTHRU */
1459
1460 case ASHIFTRT:
1461 case LSHIFTRT:
1462 *total = cost_data->int_shift;
1463 return false;
1464
1465 case IF_THEN_ELSE:
1466 if (float_mode_p)
1467 *total = cost_data->fp_add;
1468 else
1469 *total = cost_data->int_cmov;
1470 return false;
1471
1472 case DIV:
1473 case UDIV:
1474 case MOD:
1475 case UMOD:
1476 if (!float_mode_p)
1477 *total = cost_data->int_div;
1478 else if (mode == SFmode)
1479 *total = cost_data->fp_div_sf;
1480 else
1481 *total = cost_data->fp_div_df;
1482 return false;
1483
1484 case MEM:
1485 *total = COSTS_N_INSNS (!speed ? 1 : alpha_memory_latency);
1486 return true;
1487
1488 case NEG:
1489 if (! float_mode_p)
1490 {
1491 *total = COSTS_N_INSNS (1);
1492 return false;
1493 }
1494 /* FALLTHRU */
1495
1496 case ABS:
1497 if (! float_mode_p)
1498 {
1499 *total = COSTS_N_INSNS (1) + cost_data->int_cmov;
1500 return false;
1501 }
1502 /* FALLTHRU */
1503
1504 case FLOAT:
1505 case UNSIGNED_FLOAT:
1506 case FIX:
1507 case UNSIGNED_FIX:
1508 case FLOAT_TRUNCATE:
1509 *total = cost_data->fp_add;
1510 return false;
1511
1512 case FLOAT_EXTEND:
1513 if (MEM_P (XEXP (x, 0)))
1514 *total = 0;
1515 else
1516 *total = cost_data->fp_add;
1517 return false;
1518
1519 default:
1520 return false;
1521 }
1522 }
1523
1524 /* REF is an alignable memory location. Place an aligned SImode
1526 reference into *PALIGNED_MEM and the number of bits to shift into
1527 *PBITNUM. SCRATCH is a free register for use in reloading out
1528 of range stack slots. */
1529
1530 void
1531 get_aligned_mem (rtx ref, rtx *paligned_mem, rtx *pbitnum)
1532 {
1533 rtx base;
1534 HOST_WIDE_INT disp, offset;
1535
1536 gcc_assert (MEM_P (ref));
1537
1538 if (reload_in_progress)
1539 {
1540 base = find_replacement (&XEXP (ref, 0));
1541 gcc_assert (memory_address_p (GET_MODE (ref), base));
1542 }
1543 else
1544 base = XEXP (ref, 0);
1545
1546 if (GET_CODE (base) == PLUS)
1547 disp = INTVAL (XEXP (base, 1)), base = XEXP (base, 0);
1548 else
1549 disp = 0;
1550
1551 /* Find the byte offset within an aligned word. If the memory itself is
1552 claimed to be aligned, believe it. Otherwise, aligned_memory_operand
1553 will have examined the base register and determined it is aligned, and
1554 thus displacements from it are naturally alignable. */
1555 if (MEM_ALIGN (ref) >= 32)
1556 offset = 0;
1557 else
1558 offset = disp & 3;
1559
1560 /* The location should not cross aligned word boundary. */
1561 gcc_assert (offset + GET_MODE_SIZE (GET_MODE (ref))
1562 <= GET_MODE_SIZE (SImode));
1563
1564 /* Access the entire aligned word. */
1565 *paligned_mem = widen_memory_access (ref, SImode, -offset);
1566
1567 /* Convert the byte offset within the word to a bit offset. */
1568 offset *= BITS_PER_UNIT;
1569 *pbitnum = GEN_INT (offset);
1570 }
1571
1572 /* Similar, but just get the address. Handle the two reload cases.
1573 Add EXTRA_OFFSET to the address we return. */
1574
1575 rtx
1576 get_unaligned_address (rtx ref)
1577 {
1578 rtx base;
1579 HOST_WIDE_INT offset = 0;
1580
1581 gcc_assert (MEM_P (ref));
1582
1583 if (reload_in_progress)
1584 {
1585 base = find_replacement (&XEXP (ref, 0));
1586 gcc_assert (memory_address_p (GET_MODE (ref), base));
1587 }
1588 else
1589 base = XEXP (ref, 0);
1590
1591 if (GET_CODE (base) == PLUS)
1592 offset += INTVAL (XEXP (base, 1)), base = XEXP (base, 0);
1593
1594 return plus_constant (Pmode, base, offset);
1595 }
1596
1597 /* Compute a value X, such that X & 7 == (ADDR + OFS) & 7.
1598 X is always returned in a register. */
1599
1600 rtx
1601 get_unaligned_offset (rtx addr, HOST_WIDE_INT ofs)
1602 {
1603 if (GET_CODE (addr) == PLUS)
1604 {
1605 ofs += INTVAL (XEXP (addr, 1));
1606 addr = XEXP (addr, 0);
1607 }
1608
1609 return expand_simple_binop (Pmode, PLUS, addr, GEN_INT (ofs & 7),
1610 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1611 }
1612
1613 /* On the Alpha, all (non-symbolic) constants except zero go into
1614 a floating-point register via memory. Note that we cannot
1615 return anything that is not a subset of RCLASS, and that some
1616 symbolic constants cannot be dropped to memory. */
1617
1618 enum reg_class
1619 alpha_preferred_reload_class(rtx x, enum reg_class rclass)
1620 {
1621 /* Zero is present in any register class. */
1622 if (x == CONST0_RTX (GET_MODE (x)))
1623 return rclass;
1624
1625 /* These sorts of constants we can easily drop to memory. */
1626 if (CONST_SCALAR_INT_P (x)
1627 || CONST_DOUBLE_P (x)
1628 || GET_CODE (x) == CONST_VECTOR)
1629 {
1630 if (rclass == FLOAT_REGS)
1631 return NO_REGS;
1632 if (rclass == ALL_REGS)
1633 return GENERAL_REGS;
1634 return rclass;
1635 }
1636
1637 /* All other kinds of constants should not (and in the case of HIGH
1638 cannot) be dropped to memory -- instead we use a GENERAL_REGS
1639 secondary reload. */
1640 if (CONSTANT_P (x))
1641 return (rclass == ALL_REGS ? GENERAL_REGS : rclass);
1642
1643 return rclass;
1644 }
1645
1646 /* Inform reload about cases where moving X with a mode MODE to a register in
1647 RCLASS requires an extra scratch or immediate register. Return the class
1648 needed for the immediate register. */
1649
1650 static reg_class_t
1651 alpha_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
1652 machine_mode mode, secondary_reload_info *sri)
1653 {
1654 enum reg_class rclass = (enum reg_class) rclass_i;
1655
1656 /* Loading and storing HImode or QImode values to and from memory
1657 usually requires a scratch register. */
1658 if (!TARGET_BWX && (mode == QImode || mode == HImode || mode == CQImode))
1659 {
1660 if (any_memory_operand (x, mode))
1661 {
1662 if (in_p)
1663 {
1664 if (!aligned_memory_operand (x, mode))
1665 sri->icode = direct_optab_handler (reload_in_optab, mode);
1666 }
1667 else
1668 sri->icode = direct_optab_handler (reload_out_optab, mode);
1669 return NO_REGS;
1670 }
1671 }
1672
1673 /* We also cannot do integral arithmetic into FP regs, as might result
1674 from register elimination into a DImode fp register. */
1675 if (rclass == FLOAT_REGS)
1676 {
1677 if (MEM_P (x) && GET_CODE (XEXP (x, 0)) == AND)
1678 return GENERAL_REGS;
1679 if (in_p && INTEGRAL_MODE_P (mode)
1680 && !MEM_P (x) && !REG_P (x) && !CONST_INT_P (x))
1681 return GENERAL_REGS;
1682 }
1683
1684 return NO_REGS;
1685 }
1686
1687 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
1688
1689 If we are copying between general and FP registers, we need a memory
1690 location unless the FIX extension is available. */
1691
1692 static bool
1693 alpha_secondary_memory_needed (machine_mode, reg_class_t class1,
1694 reg_class_t class2)
1695 {
1696 return (!TARGET_FIX
1697 && ((class1 == FLOAT_REGS && class2 != FLOAT_REGS)
1698 || (class2 == FLOAT_REGS && class1 != FLOAT_REGS)));
1699 }
1700
1701 /* Implement TARGET_SECONDARY_MEMORY_NEEDED_MODE. If MODE is
1702 floating-point, use it. Otherwise, widen to a word like the default.
1703 This is needed because we always store integers in FP registers in
1704 quadword format. This whole area is very tricky! */
1705
1706 static machine_mode
1707 alpha_secondary_memory_needed_mode (machine_mode mode)
1708 {
1709 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1710 return mode;
1711 if (GET_MODE_SIZE (mode) >= 4)
1712 return mode;
1713 return mode_for_size (BITS_PER_WORD, GET_MODE_CLASS (mode), 0).require ();
1714 }
1715
1716 /* Given SEQ, which is an INSN list, look for any MEMs in either
1718 a SET_DEST or a SET_SRC and copy the in-struct, unchanging, and
1719 volatile flags from REF into each of the MEMs found. If REF is not
1720 a MEM, don't do anything. */
1721
1722 void
1723 alpha_set_memflags (rtx seq, rtx ref)
1724 {
1725 rtx_insn *insn;
1726
1727 if (!MEM_P (ref))
1728 return;
1729
1730 /* This is only called from alpha.md, after having had something
1731 generated from one of the insn patterns. So if everything is
1732 zero, the pattern is already up-to-date. */
1733 if (!MEM_VOLATILE_P (ref)
1734 && !MEM_NOTRAP_P (ref)
1735 && !MEM_READONLY_P (ref))
1736 return;
1737
1738 subrtx_var_iterator::array_type array;
1739 for (insn = as_a <rtx_insn *> (seq); insn; insn = NEXT_INSN (insn))
1740 if (INSN_P (insn))
1741 FOR_EACH_SUBRTX_VAR (iter, array, PATTERN (insn), NONCONST)
1742 {
1743 rtx x = *iter;
1744 if (MEM_P (x))
1745 {
1746 MEM_VOLATILE_P (x) = MEM_VOLATILE_P (ref);
1747 MEM_NOTRAP_P (x) = MEM_NOTRAP_P (ref);
1748 MEM_READONLY_P (x) = MEM_READONLY_P (ref);
1749 /* Sadly, we cannot use alias sets because the extra
1750 aliasing produced by the AND interferes. Given that
1751 two-byte quantities are the only thing we would be
1752 able to differentiate anyway, there does not seem to
1753 be any point in convoluting the early out of the
1754 alias check. */
1755 iter.skip_subrtxes ();
1756 }
1757 }
1758 else
1759 gcc_unreachable ();
1760 }
1761
1762 static rtx alpha_emit_set_const (rtx, machine_mode, HOST_WIDE_INT,
1764 int, bool);
1765
1766 /* Internal routine for alpha_emit_set_const to check for N or below insns.
1767 If NO_OUTPUT is true, then we only check to see if N insns are possible,
1768 and return pc_rtx if successful. */
1769
1770 static rtx
1771 alpha_emit_set_const_1 (rtx target, machine_mode mode,
1772 HOST_WIDE_INT c, int n, bool no_output)
1773 {
1774 HOST_WIDE_INT new_const;
1775 int i, bits;
1776 /* Use a pseudo if highly optimizing and still generating RTL. */
1777 rtx subtarget
1778 = (flag_expensive_optimizations && can_create_pseudo_p () ? 0 : target);
1779 rtx temp, insn;
1780
1781 /* If this is a sign-extended 32-bit constant, we can do this in at most
1782 three insns, so do it if we have enough insns left. */
1783
1784 if (c >> 31 == -1 || c >> 31 == 0)
1785 {
1786 HOST_WIDE_INT low = ((c & 0xffff) ^ 0x8000) - 0x8000;
1787 HOST_WIDE_INT tmp1 = c - low;
1788 HOST_WIDE_INT high = (((tmp1 >> 16) & 0xffff) ^ 0x8000) - 0x8000;
1789 HOST_WIDE_INT extra = 0;
1790
1791 /* If HIGH will be interpreted as negative but the constant is
1792 positive, we must adjust it to do two ldha insns. */
1793
1794 if ((high & 0x8000) != 0 && c >= 0)
1795 {
1796 extra = 0x4000;
1797 tmp1 -= 0x40000000;
1798 high = ((tmp1 >> 16) & 0xffff) - 2 * ((tmp1 >> 16) & 0x8000);
1799 }
1800
1801 if (c == low || (low == 0 && extra == 0))
1802 {
1803 /* We used to use copy_to_suggested_reg (GEN_INT (c), target, mode)
1804 but that meant that we can't handle INT_MIN on 32-bit machines
1805 (like NT/Alpha), because we recurse indefinitely through
1806 emit_move_insn to gen_movdi. So instead, since we know exactly
1807 what we want, create it explicitly. */
1808
1809 if (no_output)
1810 return pc_rtx;
1811 if (target == NULL)
1812 target = gen_reg_rtx (mode);
1813 emit_insn (gen_rtx_SET (target, GEN_INT (c)));
1814 return target;
1815 }
1816 else if (n >= 2 + (extra != 0))
1817 {
1818 if (no_output)
1819 return pc_rtx;
1820 if (!can_create_pseudo_p ())
1821 {
1822 emit_insn (gen_rtx_SET (target, GEN_INT (high << 16)));
1823 temp = target;
1824 }
1825 else
1826 temp = copy_to_suggested_reg (GEN_INT (high << 16),
1827 subtarget, mode);
1828
1829 /* As of 2002-02-23, addsi3 is only available when not optimizing.
1830 This means that if we go through expand_binop, we'll try to
1831 generate extensions, etc, which will require new pseudos, which
1832 will fail during some split phases. The SImode add patterns
1833 still exist, but are not named. So build the insns by hand. */
1834
1835 if (extra != 0)
1836 {
1837 if (! subtarget)
1838 subtarget = gen_reg_rtx (mode);
1839 insn = gen_rtx_PLUS (mode, temp, GEN_INT (extra << 16));
1840 insn = gen_rtx_SET (subtarget, insn);
1841 emit_insn (insn);
1842 temp = subtarget;
1843 }
1844
1845 if (target == NULL)
1846 target = gen_reg_rtx (mode);
1847 insn = gen_rtx_PLUS (mode, temp, GEN_INT (low));
1848 insn = gen_rtx_SET (target, insn);
1849 emit_insn (insn);
1850 return target;
1851 }
1852 }
1853
1854 /* If we couldn't do it that way, try some other methods. But if we have
1855 no instructions left, don't bother. Likewise, if this is SImode and
1856 we can't make pseudos, we can't do anything since the expand_binop
1857 and expand_unop calls will widen and try to make pseudos. */
1858
1859 if (n == 1 || (mode == SImode && !can_create_pseudo_p ()))
1860 return 0;
1861
1862 /* Next, see if we can load a related constant and then shift and possibly
1863 negate it to get the constant we want. Try this once each increasing
1864 numbers of insns. */
1865
1866 for (i = 1; i < n; i++)
1867 {
1868 /* First, see if minus some low bits, we've an easy load of
1869 high bits. */
1870
1871 new_const = ((c & 0xffff) ^ 0x8000) - 0x8000;
1872 if (new_const != 0)
1873 {
1874 temp = alpha_emit_set_const (subtarget, mode, c - new_const, i, no_output);
1875 if (temp)
1876 {
1877 if (no_output)
1878 return temp;
1879 return expand_binop (mode, add_optab, temp, GEN_INT (new_const),
1880 target, 0, OPTAB_WIDEN);
1881 }
1882 }
1883
1884 /* Next try complementing. */
1885 temp = alpha_emit_set_const (subtarget, mode, ~c, i, no_output);
1886 if (temp)
1887 {
1888 if (no_output)
1889 return temp;
1890 return expand_unop (mode, one_cmpl_optab, temp, target, 0);
1891 }
1892
1893 /* Next try to form a constant and do a left shift. We can do this
1894 if some low-order bits are zero; the exact_log2 call below tells
1895 us that information. The bits we are shifting out could be any
1896 value, but here we'll just try the 0- and sign-extended forms of
1897 the constant. To try to increase the chance of having the same
1898 constant in more than one insn, start at the highest number of
1899 bits to shift, but try all possibilities in case a ZAPNOT will
1900 be useful. */
1901
1902 bits = exact_log2 (c & -c);
1903 if (bits > 0)
1904 for (; bits > 0; bits--)
1905 {
1906 new_const = c >> bits;
1907 temp = alpha_emit_set_const (subtarget, mode, new_const, i, no_output);
1908 if (!temp && c < 0)
1909 {
1910 new_const = (unsigned HOST_WIDE_INT)c >> bits;
1911 temp = alpha_emit_set_const (subtarget, mode, new_const,
1912 i, no_output);
1913 }
1914 if (temp)
1915 {
1916 if (no_output)
1917 return temp;
1918 return expand_binop (mode, ashl_optab, temp, GEN_INT (bits),
1919 target, 0, OPTAB_WIDEN);
1920 }
1921 }
1922
1923 /* Now try high-order zero bits. Here we try the shifted-in bits as
1924 all zero and all ones. Be careful to avoid shifting outside the
1925 mode and to avoid shifting outside the host wide int size. */
1926
1927 bits = (MIN (HOST_BITS_PER_WIDE_INT, GET_MODE_SIZE (mode) * 8)
1928 - floor_log2 (c) - 1);
1929 if (bits > 0)
1930 for (; bits > 0; bits--)
1931 {
1932 new_const = c << bits;
1933 temp = alpha_emit_set_const (subtarget, mode, new_const, i, no_output);
1934 if (!temp)
1935 {
1936 new_const = (c << bits) | ((HOST_WIDE_INT_1U << bits) - 1);
1937 temp = alpha_emit_set_const (subtarget, mode, new_const,
1938 i, no_output);
1939 }
1940 if (temp)
1941 {
1942 if (no_output)
1943 return temp;
1944 return expand_binop (mode, lshr_optab, temp, GEN_INT (bits),
1945 target, 1, OPTAB_WIDEN);
1946 }
1947 }
1948
1949 /* Now try high-order 1 bits. We get that with a sign-extension.
1950 But one bit isn't enough here. Be careful to avoid shifting outside
1951 the mode and to avoid shifting outside the host wide int size. */
1952
1953 bits = (MIN (HOST_BITS_PER_WIDE_INT, GET_MODE_SIZE (mode) * 8)
1954 - floor_log2 (~ c) - 2);
1955 if (bits > 0)
1956 for (; bits > 0; bits--)
1957 {
1958 new_const = c << bits;
1959 temp = alpha_emit_set_const (subtarget, mode, new_const, i, no_output);
1960 if (!temp)
1961 {
1962 new_const = (c << bits) | ((HOST_WIDE_INT_1U << bits) - 1);
1963 temp = alpha_emit_set_const (subtarget, mode, new_const,
1964 i, no_output);
1965 }
1966 if (temp)
1967 {
1968 if (no_output)
1969 return temp;
1970 return expand_binop (mode, ashr_optab, temp, GEN_INT (bits),
1971 target, 0, OPTAB_WIDEN);
1972 }
1973 }
1974 }
1975
1976 /* Finally, see if can load a value into the target that is the same as the
1977 constant except that all bytes that are 0 are changed to be 0xff. If we
1978 can, then we can do a ZAPNOT to obtain the desired constant. */
1979
1980 new_const = c;
1981 for (i = 0; i < 64; i += 8)
1982 if ((new_const & ((HOST_WIDE_INT) 0xff << i)) == 0)
1983 new_const |= (HOST_WIDE_INT) 0xff << i;
1984
1985 /* We are only called for SImode and DImode. If this is SImode, ensure that
1986 we are sign extended to a full word. */
1987
1988 if (mode == SImode)
1989 new_const = ((new_const & 0xffffffff) ^ 0x80000000) - 0x80000000;
1990
1991 if (new_const != c)
1992 {
1993 temp = alpha_emit_set_const (subtarget, mode, new_const, n - 1, no_output);
1994 if (temp)
1995 {
1996 if (no_output)
1997 return temp;
1998 return expand_binop (mode, and_optab, temp, GEN_INT (c | ~ new_const),
1999 target, 0, OPTAB_WIDEN);
2000 }
2001 }
2002
2003 return 0;
2004 }
2005
2006 /* Try to output insns to set TARGET equal to the constant C if it can be
2007 done in less than N insns. Do all computations in MODE. Returns the place
2008 where the output has been placed if it can be done and the insns have been
2009 emitted. If it would take more than N insns, zero is returned and no
2010 insns and emitted. */
2011
2012 static rtx
2013 alpha_emit_set_const (rtx target, machine_mode mode,
2014 HOST_WIDE_INT c, int n, bool no_output)
2015 {
2016 machine_mode orig_mode = mode;
2017 rtx orig_target = target;
2018 rtx result = 0;
2019 int i;
2020
2021 /* If we can't make any pseudos, TARGET is an SImode hard register, we
2022 can't load this constant in one insn, do this in DImode. */
2023 if (!can_create_pseudo_p () && mode == SImode
2024 && REG_P (target) && REGNO (target) < FIRST_PSEUDO_REGISTER)
2025 {
2026 result = alpha_emit_set_const_1 (target, mode, c, 1, no_output);
2027 if (result)
2028 return result;
2029
2030 target = no_output ? NULL : gen_lowpart (DImode, target);
2031 mode = DImode;
2032 }
2033 else if (mode == V8QImode || mode == V4HImode || mode == V2SImode)
2034 {
2035 target = no_output ? NULL : gen_lowpart (DImode, target);
2036 mode = DImode;
2037 }
2038
2039 /* Try 1 insn, then 2, then up to N. */
2040 for (i = 1; i <= n; i++)
2041 {
2042 result = alpha_emit_set_const_1 (target, mode, c, i, no_output);
2043 if (result)
2044 {
2045 rtx_insn *insn;
2046 rtx set;
2047
2048 if (no_output)
2049 return result;
2050
2051 insn = get_last_insn ();
2052 set = single_set (insn);
2053 if (! CONSTANT_P (SET_SRC (set)))
2054 set_unique_reg_note (get_last_insn (), REG_EQUAL, GEN_INT (c));
2055 break;
2056 }
2057 }
2058
2059 /* Allow for the case where we changed the mode of TARGET. */
2060 if (result)
2061 {
2062 if (result == target)
2063 result = orig_target;
2064 else if (mode != orig_mode)
2065 result = gen_lowpart (orig_mode, result);
2066 }
2067
2068 return result;
2069 }
2070
2071 /* Having failed to find a 3 insn sequence in alpha_emit_set_const,
2072 fall back to a straight forward decomposition. We do this to avoid
2073 exponential run times encountered when looking for longer sequences
2074 with alpha_emit_set_const. */
2075
2076 static rtx
2077 alpha_emit_set_long_const (rtx target, HOST_WIDE_INT c1)
2078 {
2079 HOST_WIDE_INT d1, d2, d3, d4;
2080 machine_mode mode = GET_MODE (target);
2081 rtx orig_target = target;
2082
2083 /* Decompose the entire word */
2084
2085 d1 = ((c1 & 0xffff) ^ 0x8000) - 0x8000;
2086 c1 -= d1;
2087 d2 = ((c1 & 0xffffffff) ^ 0x80000000) - 0x80000000;
2088 c1 = (c1 - d2) >> 32;
2089 d3 = ((c1 & 0xffff) ^ 0x8000) - 0x8000;
2090 c1 -= d3;
2091 d4 = ((c1 & 0xffffffff) ^ 0x80000000) - 0x80000000;
2092 gcc_assert (c1 == d4);
2093
2094 if (mode != DImode)
2095 target = gen_lowpart (DImode, target);
2096
2097 /* Construct the high word */
2098 if (d4)
2099 {
2100 emit_move_insn (target, GEN_INT (d4));
2101 if (d3)
2102 emit_move_insn (target, gen_rtx_PLUS (DImode, target, GEN_INT (d3)));
2103 }
2104 else
2105 emit_move_insn (target, GEN_INT (d3));
2106
2107 /* Shift it into place */
2108 emit_move_insn (target, gen_rtx_ASHIFT (DImode, target, GEN_INT (32)));
2109
2110 /* Add in the low bits. */
2111 if (d2)
2112 emit_move_insn (target, gen_rtx_PLUS (DImode, target, GEN_INT (d2)));
2113 if (d1)
2114 emit_move_insn (target, gen_rtx_PLUS (DImode, target, GEN_INT (d1)));
2115
2116 return orig_target;
2117 }
2118
2119 /* Given an integral CONST_INT or CONST_VECTOR, return the low 64 bits. */
2120
2121 static HOST_WIDE_INT
2122 alpha_extract_integer (rtx x)
2123 {
2124 if (GET_CODE (x) == CONST_VECTOR)
2125 x = simplify_subreg (DImode, x, GET_MODE (x), 0);
2126
2127 gcc_assert (CONST_INT_P (x));
2128
2129 return INTVAL (x);
2130 }
2131
2132 /* Implement TARGET_LEGITIMATE_CONSTANT_P. This is all constants for which
2133 we are willing to load the value into a register via a move pattern.
2134 Normally this is all symbolic constants, integral constants that
2135 take three or fewer instructions, and floating-point zero. */
2136
2137 bool
2138 alpha_legitimate_constant_p (machine_mode mode, rtx x)
2139 {
2140 HOST_WIDE_INT i0;
2141
2142 switch (GET_CODE (x))
2143 {
2144 case LABEL_REF:
2145 case HIGH:
2146 return true;
2147
2148 case CONST:
2149 if (GET_CODE (XEXP (x, 0)) == PLUS
2150 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
2151 x = XEXP (XEXP (x, 0), 0);
2152 else
2153 return true;
2154
2155 if (GET_CODE (x) != SYMBOL_REF)
2156 return true;
2157 /* FALLTHRU */
2158
2159 case SYMBOL_REF:
2160 /* TLS symbols are never valid. */
2161 return SYMBOL_REF_TLS_MODEL (x) == 0;
2162
2163 case CONST_WIDE_INT:
2164 if (TARGET_BUILD_CONSTANTS)
2165 return true;
2166 if (x == CONST0_RTX (mode))
2167 return true;
2168 mode = DImode;
2169 gcc_assert (CONST_WIDE_INT_NUNITS (x) == 2);
2170 i0 = CONST_WIDE_INT_ELT (x, 1);
2171 if (alpha_emit_set_const_1 (NULL_RTX, mode, i0, 3, true) == NULL)
2172 return false;
2173 i0 = CONST_WIDE_INT_ELT (x, 0);
2174 goto do_integer;
2175
2176 case CONST_DOUBLE:
2177 if (x == CONST0_RTX (mode))
2178 return true;
2179 return false;
2180
2181 case CONST_VECTOR:
2182 if (x == CONST0_RTX (mode))
2183 return true;
2184 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
2185 return false;
2186 if (GET_MODE_SIZE (mode) != 8)
2187 return false;
2188 /* FALLTHRU */
2189
2190 case CONST_INT:
2191 if (TARGET_BUILD_CONSTANTS)
2192 return true;
2193 i0 = alpha_extract_integer (x);
2194 do_integer:
2195 return alpha_emit_set_const_1 (NULL_RTX, mode, i0, 3, true) != NULL;
2196
2197 default:
2198 return false;
2199 }
2200 }
2201
2202 /* Operand 1 is known to be a constant, and should require more than one
2203 instruction to load. Emit that multi-part load. */
2204
2205 bool
2206 alpha_split_const_mov (machine_mode mode, rtx *operands)
2207 {
2208 HOST_WIDE_INT i0;
2209 rtx temp = NULL_RTX;
2210
2211 i0 = alpha_extract_integer (operands[1]);
2212
2213 temp = alpha_emit_set_const (operands[0], mode, i0, 3, false);
2214
2215 if (!temp && TARGET_BUILD_CONSTANTS)
2216 temp = alpha_emit_set_long_const (operands[0], i0);
2217
2218 if (temp)
2219 {
2220 if (!rtx_equal_p (operands[0], temp))
2221 emit_move_insn (operands[0], temp);
2222 return true;
2223 }
2224
2225 return false;
2226 }
2227
2228 /* Expand a move instruction; return true if all work is done.
2229 We don't handle non-bwx subword loads here. */
2230
2231 bool
2232 alpha_expand_mov (machine_mode mode, rtx *operands)
2233 {
2234 rtx tmp;
2235
2236 /* If the output is not a register, the input must be. */
2237 if (MEM_P (operands[0])
2238 && ! reg_or_0_operand (operands[1], mode))
2239 operands[1] = force_reg (mode, operands[1]);
2240
2241 /* Allow legitimize_address to perform some simplifications. */
2242 if (mode == Pmode && symbolic_operand (operands[1], mode))
2243 {
2244 tmp = alpha_legitimize_address_1 (operands[1], operands[0], mode);
2245 if (tmp)
2246 {
2247 if (tmp == operands[0])
2248 return true;
2249 operands[1] = tmp;
2250 return false;
2251 }
2252 }
2253
2254 /* Early out for non-constants and valid constants. */
2255 if (! CONSTANT_P (operands[1]) || input_operand (operands[1], mode))
2256 return false;
2257
2258 /* Split large integers. */
2259 if (CONST_INT_P (operands[1])
2260 || GET_CODE (operands[1]) == CONST_VECTOR)
2261 {
2262 if (alpha_split_const_mov (mode, operands))
2263 return true;
2264 }
2265
2266 /* Otherwise we've nothing left but to drop the thing to memory. */
2267 tmp = force_const_mem (mode, operands[1]);
2268
2269 if (tmp == NULL_RTX)
2270 return false;
2271
2272 if (reload_in_progress)
2273 {
2274 emit_move_insn (operands[0], XEXP (tmp, 0));
2275 operands[1] = replace_equiv_address (tmp, operands[0]);
2276 }
2277 else
2278 operands[1] = validize_mem (tmp);
2279 return false;
2280 }
2281
2282 /* Expand a non-bwx QImode or HImode move instruction;
2283 return true if all work is done. */
2284
2285 bool
2286 alpha_expand_mov_nobwx (machine_mode mode, rtx *operands)
2287 {
2288 rtx seq;
2289
2290 /* If the output is not a register, the input must be. */
2291 if (MEM_P (operands[0]))
2292 operands[1] = force_reg (mode, operands[1]);
2293
2294 /* Handle four memory cases, unaligned and aligned for either the input
2295 or the output. The only case where we can be called during reload is
2296 for aligned loads; all other cases require temporaries. */
2297
2298 if (any_memory_operand (operands[1], mode))
2299 {
2300 if (aligned_memory_operand (operands[1], mode))
2301 {
2302 if (reload_in_progress)
2303 {
2304 seq = gen_reload_in_aligned (mode, operands[0], operands[1]);
2305 emit_insn (seq);
2306 }
2307 else
2308 {
2309 rtx aligned_mem, bitnum;
2310 rtx scratch = gen_reg_rtx (SImode);
2311 rtx subtarget;
2312 bool copyout;
2313
2314 get_aligned_mem (operands[1], &aligned_mem, &bitnum);
2315
2316 subtarget = operands[0];
2317 if (REG_P (subtarget))
2318 subtarget = gen_lowpart (DImode, subtarget), copyout = false;
2319 else
2320 subtarget = gen_reg_rtx (DImode), copyout = true;
2321
2322 if (mode == QImode)
2323 seq = gen_aligned_loadqi (subtarget, aligned_mem,
2324 bitnum, scratch);
2325 else
2326 seq = gen_aligned_loadhi (subtarget, aligned_mem,
2327 bitnum, scratch);
2328 emit_insn (seq);
2329
2330 if (copyout)
2331 emit_move_insn (operands[0], gen_lowpart (mode, subtarget));
2332 }
2333 }
2334 else
2335 {
2336 /* Don't pass these as parameters since that makes the generated
2337 code depend on parameter evaluation order which will cause
2338 bootstrap failures. */
2339
2340 rtx temp1, temp2, subtarget, ua;
2341 bool copyout;
2342
2343 temp1 = gen_reg_rtx (DImode);
2344 temp2 = gen_reg_rtx (DImode);
2345
2346 subtarget = operands[0];
2347 if (REG_P (subtarget))
2348 subtarget = gen_lowpart (DImode, subtarget), copyout = false;
2349 else
2350 subtarget = gen_reg_rtx (DImode), copyout = true;
2351
2352 ua = get_unaligned_address (operands[1]);
2353 if (mode == QImode)
2354 seq = gen_unaligned_loadqi (subtarget, ua, temp1, temp2);
2355 else
2356 seq = gen_unaligned_loadhi (subtarget, ua, temp1, temp2);
2357
2358 alpha_set_memflags (seq, operands[1]);
2359 emit_insn (seq);
2360
2361 if (copyout)
2362 emit_move_insn (operands[0], gen_lowpart (mode, subtarget));
2363 }
2364 return true;
2365 }
2366
2367 if (any_memory_operand (operands[0], mode))
2368 {
2369 if (aligned_memory_operand (operands[0], mode))
2370 {
2371 rtx aligned_mem, bitnum;
2372 rtx temp1 = gen_reg_rtx (SImode);
2373 rtx temp2 = gen_reg_rtx (SImode);
2374
2375 get_aligned_mem (operands[0], &aligned_mem, &bitnum);
2376
2377 emit_insn (gen_aligned_store (aligned_mem, operands[1], bitnum,
2378 temp1, temp2));
2379 }
2380 else
2381 {
2382 rtx temp1 = gen_reg_rtx (DImode);
2383 rtx temp2 = gen_reg_rtx (DImode);
2384 rtx temp3 = gen_reg_rtx (DImode);
2385 rtx ua = get_unaligned_address (operands[0]);
2386
2387 seq = gen_unaligned_store
2388 (mode, ua, operands[1], temp1, temp2, temp3);
2389
2390 alpha_set_memflags (seq, operands[0]);
2391 emit_insn (seq);
2392 }
2393 return true;
2394 }
2395
2396 return false;
2397 }
2398
2399 /* Implement the movmisalign patterns. One of the operands is a memory
2400 that is not naturally aligned. Emit instructions to load it. */
2401
2402 void
2403 alpha_expand_movmisalign (machine_mode mode, rtx *operands)
2404 {
2405 /* Honor misaligned loads, for those we promised to do so. */
2406 if (MEM_P (operands[1]))
2407 {
2408 rtx tmp;
2409
2410 if (register_operand (operands[0], mode))
2411 tmp = operands[0];
2412 else
2413 tmp = gen_reg_rtx (mode);
2414
2415 alpha_expand_unaligned_load (tmp, operands[1], 8, 0, 0);
2416 if (tmp != operands[0])
2417 emit_move_insn (operands[0], tmp);
2418 }
2419 else if (MEM_P (operands[0]))
2420 {
2421 if (!reg_or_0_operand (operands[1], mode))
2422 operands[1] = force_reg (mode, operands[1]);
2423 alpha_expand_unaligned_store (operands[0], operands[1], 8, 0);
2424 }
2425 else
2426 gcc_unreachable ();
2427 }
2428
2429 /* Generate an unsigned DImode to FP conversion. This is the same code
2430 optabs would emit if we didn't have TFmode patterns.
2431
2432 For SFmode, this is the only construction I've found that can pass
2433 gcc.c-torture/execute/ieee/rbug.c. No scenario that uses DFmode
2434 intermediates will work, because you'll get intermediate rounding
2435 that ruins the end result. Some of this could be fixed by turning
2436 on round-to-positive-infinity, but that requires diddling the fpsr,
2437 which kills performance. I tried turning this around and converting
2438 to a negative number, so that I could turn on /m, but either I did
2439 it wrong or there's something else cause I wound up with the exact
2440 same single-bit error. There is a branch-less form of this same code:
2441
2442 srl $16,1,$1
2443 and $16,1,$2
2444 cmplt $16,0,$3
2445 or $1,$2,$2
2446 cmovge $16,$16,$2
2447 itoft $3,$f10
2448 itoft $2,$f11
2449 cvtqs $f11,$f11
2450 adds $f11,$f11,$f0
2451 fcmoveq $f10,$f11,$f0
2452
2453 I'm not using it because it's the same number of instructions as
2454 this branch-full form, and it has more serialized long latency
2455 instructions on the critical path.
2456
2457 For DFmode, we can avoid rounding errors by breaking up the word
2458 into two pieces, converting them separately, and adding them back:
2459
2460 LC0: .long 0,0x5f800000
2461
2462 itoft $16,$f11
2463 lda $2,LC0
2464 cmplt $16,0,$1
2465 cpyse $f11,$f31,$f10
2466 cpyse $f31,$f11,$f11
2467 s4addq $1,$2,$1
2468 lds $f12,0($1)
2469 cvtqt $f10,$f10
2470 cvtqt $f11,$f11
2471 addt $f12,$f10,$f0
2472 addt $f0,$f11,$f0
2473
2474 This doesn't seem to be a clear-cut win over the optabs form.
2475 It probably all depends on the distribution of numbers being
2476 converted -- in the optabs form, all but high-bit-set has a
2477 much lower minimum execution time. */
2478
2479 void
2480 alpha_emit_floatuns (rtx operands[2])
2481 {
2482 rtx neglab, donelab, i0, i1, f0, in, out;
2483 machine_mode mode;
2484
2485 out = operands[0];
2486 in = force_reg (DImode, operands[1]);
2487 mode = GET_MODE (out);
2488 neglab = gen_label_rtx ();
2489 donelab = gen_label_rtx ();
2490 i0 = gen_reg_rtx (DImode);
2491 i1 = gen_reg_rtx (DImode);
2492 f0 = gen_reg_rtx (mode);
2493
2494 emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, DImode, 0, neglab);
2495
2496 emit_insn (gen_rtx_SET (out, gen_rtx_FLOAT (mode, in)));
2497 emit_jump_insn (gen_jump (donelab));
2498 emit_barrier ();
2499
2500 emit_label (neglab);
2501
2502 emit_insn (gen_lshrdi3 (i0, in, const1_rtx));
2503 emit_insn (gen_anddi3 (i1, in, const1_rtx));
2504 emit_insn (gen_iordi3 (i0, i0, i1));
2505 emit_insn (gen_rtx_SET (f0, gen_rtx_FLOAT (mode, i0)));
2506 emit_insn (gen_rtx_SET (out, gen_rtx_PLUS (mode, f0, f0)));
2507
2508 emit_label (donelab);
2509 }
2510
2511 /* Generate the comparison for a conditional branch. */
2512
2513 void
2514 alpha_emit_conditional_branch (rtx operands[], machine_mode cmp_mode)
2515 {
2516 enum rtx_code cmp_code, branch_code;
2517 machine_mode branch_mode = VOIDmode;
2518 enum rtx_code code = GET_CODE (operands[0]);
2519 rtx op0 = operands[1], op1 = operands[2];
2520 rtx tem;
2521
2522 if (cmp_mode == TFmode)
2523 {
2524 op0 = alpha_emit_xfloating_compare (&code, op0, op1);
2525 op1 = const0_rtx;
2526 cmp_mode = DImode;
2527 }
2528
2529 /* The general case: fold the comparison code to the types of compares
2530 that we have, choosing the branch as necessary. */
2531 switch (code)
2532 {
2533 case EQ: case LE: case LT: case LEU: case LTU:
2534 case UNORDERED:
2535 /* We have these compares. */
2536 cmp_code = code, branch_code = NE;
2537 break;
2538
2539 case NE:
2540 case ORDERED:
2541 /* These must be reversed. */
2542 cmp_code = reverse_condition (code), branch_code = EQ;
2543 break;
2544
2545 case GE: case GT: case GEU: case GTU:
2546 /* For FP, we swap them, for INT, we reverse them. */
2547 if (cmp_mode == DFmode)
2548 {
2549 cmp_code = swap_condition (code);
2550 branch_code = NE;
2551 std::swap (op0, op1);
2552 }
2553 else
2554 {
2555 cmp_code = reverse_condition (code);
2556 branch_code = EQ;
2557 }
2558 break;
2559
2560 default:
2561 gcc_unreachable ();
2562 }
2563
2564 if (cmp_mode == DFmode)
2565 {
2566 if (flag_unsafe_math_optimizations && cmp_code != UNORDERED)
2567 {
2568 /* When we are not as concerned about non-finite values, and we
2569 are comparing against zero, we can branch directly. */
2570 if (op1 == CONST0_RTX (DFmode))
2571 cmp_code = UNKNOWN, branch_code = code;
2572 else if (op0 == CONST0_RTX (DFmode))
2573 {
2574 /* Undo the swap we probably did just above. */
2575 std::swap (op0, op1);
2576 branch_code = swap_condition (cmp_code);
2577 cmp_code = UNKNOWN;
2578 }
2579 }
2580 else
2581 {
2582 /* ??? We mark the branch mode to be CCmode to prevent the
2583 compare and branch from being combined, since the compare
2584 insn follows IEEE rules that the branch does not. */
2585 branch_mode = CCmode;
2586 }
2587 }
2588 else
2589 {
2590 /* The following optimizations are only for signed compares. */
2591 if (code != LEU && code != LTU && code != GEU && code != GTU)
2592 {
2593 /* Whee. Compare and branch against 0 directly. */
2594 if (op1 == const0_rtx)
2595 cmp_code = UNKNOWN, branch_code = code;
2596
2597 /* If the constants doesn't fit into an immediate, but can
2598 be generated by lda/ldah, we adjust the argument and
2599 compare against zero, so we can use beq/bne directly. */
2600 /* ??? Don't do this when comparing against symbols, otherwise
2601 we'll reduce (&x == 0x1234) to (&x-0x1234 == 0), which will
2602 be declared false out of hand (at least for non-weak). */
2603 else if (CONST_INT_P (op1)
2604 && (code == EQ || code == NE)
2605 && !(symbolic_operand (op0, VOIDmode)
2606 || (REG_P (op0) && REG_POINTER (op0))))
2607 {
2608 rtx n_op1 = GEN_INT (-INTVAL (op1));
2609
2610 if (! satisfies_constraint_I (op1)
2611 && (satisfies_constraint_K (n_op1)
2612 || satisfies_constraint_L (n_op1)))
2613 cmp_code = PLUS, branch_code = code, op1 = n_op1;
2614 }
2615 }
2616
2617 if (!reg_or_0_operand (op0, DImode))
2618 op0 = force_reg (DImode, op0);
2619 if (cmp_code != PLUS && !reg_or_8bit_operand (op1, DImode))
2620 op1 = force_reg (DImode, op1);
2621 }
2622
2623 /* Emit an initial compare instruction, if necessary. */
2624 tem = op0;
2625 if (cmp_code != UNKNOWN)
2626 {
2627 tem = gen_reg_rtx (cmp_mode);
2628 emit_move_insn (tem, gen_rtx_fmt_ee (cmp_code, cmp_mode, op0, op1));
2629 }
2630
2631 /* Emit the branch instruction. */
2632 tem = gen_rtx_SET (pc_rtx,
2633 gen_rtx_IF_THEN_ELSE (VOIDmode,
2634 gen_rtx_fmt_ee (branch_code,
2635 branch_mode, tem,
2636 CONST0_RTX (cmp_mode)),
2637 gen_rtx_LABEL_REF (VOIDmode,
2638 operands[3]),
2639 pc_rtx));
2640 emit_jump_insn (tem);
2641 }
2642
2643 /* Certain simplifications can be done to make invalid setcc operations
2644 valid. Return the final comparison, or NULL if we can't work. */
2645
2646 bool
2647 alpha_emit_setcc (rtx operands[], machine_mode cmp_mode)
2648 {
2649 enum rtx_code cmp_code;
2650 enum rtx_code code = GET_CODE (operands[1]);
2651 rtx op0 = operands[2], op1 = operands[3];
2652 rtx tmp;
2653
2654 if (cmp_mode == TFmode)
2655 {
2656 op0 = alpha_emit_xfloating_compare (&code, op0, op1);
2657 op1 = const0_rtx;
2658 cmp_mode = DImode;
2659 }
2660
2661 if (cmp_mode == DFmode && !TARGET_FIX)
2662 return 0;
2663
2664 /* The general case: fold the comparison code to the types of compares
2665 that we have, choosing the branch as necessary. */
2666
2667 cmp_code = UNKNOWN;
2668 switch (code)
2669 {
2670 case EQ: case LE: case LT: case LEU: case LTU:
2671 case UNORDERED:
2672 /* We have these compares. */
2673 if (cmp_mode == DFmode)
2674 cmp_code = code, code = NE;
2675 break;
2676
2677 case NE:
2678 if (cmp_mode == DImode && op1 == const0_rtx)
2679 break;
2680 /* FALLTHRU */
2681
2682 case ORDERED:
2683 cmp_code = reverse_condition (code);
2684 code = EQ;
2685 break;
2686
2687 case GE: case GT: case GEU: case GTU:
2688 /* These normally need swapping, but for integer zero we have
2689 special patterns that recognize swapped operands. */
2690 if (cmp_mode == DImode && op1 == const0_rtx)
2691 break;
2692 code = swap_condition (code);
2693 if (cmp_mode == DFmode)
2694 cmp_code = code, code = NE;
2695 std::swap (op0, op1);
2696 break;
2697
2698 default:
2699 gcc_unreachable ();
2700 }
2701
2702 if (cmp_mode == DImode)
2703 {
2704 if (!register_operand (op0, DImode))
2705 op0 = force_reg (DImode, op0);
2706 if (!reg_or_8bit_operand (op1, DImode))
2707 op1 = force_reg (DImode, op1);
2708 }
2709
2710 /* Emit an initial compare instruction, if necessary. */
2711 if (cmp_code != UNKNOWN)
2712 {
2713 tmp = gen_reg_rtx (cmp_mode);
2714 emit_insn (gen_rtx_SET (tmp, gen_rtx_fmt_ee (cmp_code, cmp_mode,
2715 op0, op1)));
2716
2717 op0 = cmp_mode != DImode ? gen_lowpart (DImode, tmp) : tmp;
2718 op1 = const0_rtx;
2719 }
2720
2721 /* Emit the setcc instruction. */
2722 emit_insn (gen_rtx_SET (operands[0], gen_rtx_fmt_ee (code, DImode,
2723 op0, op1)));
2724 return true;
2725 }
2726
2727
2728 /* Rewrite a comparison against zero CMP of the form
2729 (CODE (cc0) (const_int 0)) so it can be written validly in
2730 a conditional move (if_then_else CMP ...).
2731 If both of the operands that set cc0 are nonzero we must emit
2732 an insn to perform the compare (it can't be done within
2733 the conditional move). */
2734
2735 rtx
2736 alpha_emit_conditional_move (rtx cmp, machine_mode mode)
2737 {
2738 enum rtx_code code = GET_CODE (cmp);
2739 enum rtx_code cmov_code = NE;
2740 rtx op0 = XEXP (cmp, 0);
2741 rtx op1 = XEXP (cmp, 1);
2742 machine_mode cmp_mode
2743 = (GET_MODE (op0) == VOIDmode ? DImode : GET_MODE (op0));
2744 machine_mode cmov_mode = VOIDmode;
2745 int local_fast_math = flag_unsafe_math_optimizations;
2746 rtx tem;
2747
2748 if (cmp_mode == TFmode)
2749 {
2750 op0 = alpha_emit_xfloating_compare (&code, op0, op1);
2751 op1 = const0_rtx;
2752 cmp_mode = DImode;
2753 }
2754
2755 gcc_assert (cmp_mode == DFmode || cmp_mode == DImode);
2756
2757 if (FLOAT_MODE_P (cmp_mode) != FLOAT_MODE_P (mode))
2758 {
2759 enum rtx_code cmp_code;
2760
2761 if (! TARGET_FIX)
2762 return 0;
2763
2764 /* If we have fp<->int register move instructions, do a cmov by
2765 performing the comparison in fp registers, and move the
2766 zero/nonzero value to integer registers, where we can then
2767 use a normal cmov, or vice-versa. */
2768
2769 switch (code)
2770 {
2771 case EQ: case LE: case LT: case LEU: case LTU:
2772 case UNORDERED:
2773 /* We have these compares. */
2774 cmp_code = code, code = NE;
2775 break;
2776
2777 case NE:
2778 case ORDERED:
2779 /* These must be reversed. */
2780 cmp_code = reverse_condition (code), code = EQ;
2781 break;
2782
2783 case GE: case GT: case GEU: case GTU:
2784 /* These normally need swapping, but for integer zero we have
2785 special patterns that recognize swapped operands. */
2786 if (cmp_mode == DImode && op1 == const0_rtx)
2787 cmp_code = code, code = NE;
2788 else
2789 {
2790 cmp_code = swap_condition (code);
2791 code = NE;
2792 std::swap (op0, op1);
2793 }
2794 break;
2795
2796 default:
2797 gcc_unreachable ();
2798 }
2799
2800 if (cmp_mode == DImode)
2801 {
2802 if (!reg_or_0_operand (op0, DImode))
2803 op0 = force_reg (DImode, op0);
2804 if (!reg_or_8bit_operand (op1, DImode))
2805 op1 = force_reg (DImode, op1);
2806 }
2807
2808 tem = gen_reg_rtx (cmp_mode);
2809 emit_insn (gen_rtx_SET (tem, gen_rtx_fmt_ee (cmp_code, cmp_mode,
2810 op0, op1)));
2811
2812 cmp_mode = cmp_mode == DImode ? E_DFmode : E_DImode;
2813 op0 = gen_lowpart (cmp_mode, tem);
2814 op1 = CONST0_RTX (cmp_mode);
2815 cmp = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
2816 local_fast_math = 1;
2817 }
2818
2819 if (cmp_mode == DImode)
2820 {
2821 if (!reg_or_0_operand (op0, DImode))
2822 op0 = force_reg (DImode, op0);
2823 if (!reg_or_8bit_operand (op1, DImode))
2824 op1 = force_reg (DImode, op1);
2825 }
2826
2827 /* We may be able to use a conditional move directly.
2828 This avoids emitting spurious compares. */
2829 if (signed_comparison_operator (cmp, VOIDmode)
2830 && (cmp_mode == DImode || local_fast_math)
2831 && (op0 == CONST0_RTX (cmp_mode) || op1 == CONST0_RTX (cmp_mode)))
2832 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
2833
2834 /* We can't put the comparison inside the conditional move;
2835 emit a compare instruction and put that inside the
2836 conditional move. Make sure we emit only comparisons we have;
2837 swap or reverse as necessary. */
2838
2839 if (!can_create_pseudo_p ())
2840 return NULL_RTX;
2841
2842 switch (code)
2843 {
2844 case EQ: case LE: case LT: case LEU: case LTU:
2845 case UNORDERED:
2846 /* We have these compares: */
2847 break;
2848
2849 case NE:
2850 case ORDERED:
2851 /* These must be reversed. */
2852 code = reverse_condition (code);
2853 cmov_code = EQ;
2854 break;
2855
2856 case GE: case GT: case GEU: case GTU:
2857 /* These normally need swapping, but for integer zero we have
2858 special patterns that recognize swapped operands. */
2859 if (cmp_mode == DImode && op1 == const0_rtx)
2860 break;
2861 code = swap_condition (code);
2862 std::swap (op0, op1);
2863 break;
2864
2865 default:
2866 gcc_unreachable ();
2867 }
2868
2869 if (cmp_mode == DImode)
2870 {
2871 if (!reg_or_0_operand (op0, DImode))
2872 op0 = force_reg (DImode, op0);
2873 if (!reg_or_8bit_operand (op1, DImode))
2874 op1 = force_reg (DImode, op1);
2875 }
2876
2877 /* ??? We mark the branch mode to be CCmode to prevent the compare
2878 and cmov from being combined, since the compare insn follows IEEE
2879 rules that the cmov does not. */
2880 if (cmp_mode == DFmode && !local_fast_math)
2881 cmov_mode = CCmode;
2882
2883 tem = gen_reg_rtx (cmp_mode);
2884 emit_move_insn (tem, gen_rtx_fmt_ee (code, cmp_mode, op0, op1));
2885 return gen_rtx_fmt_ee (cmov_code, cmov_mode, tem, CONST0_RTX (cmp_mode));
2886 }
2887
2888 /* Simplify a conditional move of two constants into a setcc with
2889 arithmetic. This is done with a splitter since combine would
2890 just undo the work if done during code generation. It also catches
2891 cases we wouldn't have before cse. */
2892
2893 int
2894 alpha_split_conditional_move (enum rtx_code code, rtx dest, rtx cond,
2895 rtx t_rtx, rtx f_rtx)
2896 {
2897 HOST_WIDE_INT t, f, diff;
2898 machine_mode mode;
2899 rtx target, subtarget, tmp;
2900
2901 mode = GET_MODE (dest);
2902 t = INTVAL (t_rtx);
2903 f = INTVAL (f_rtx);
2904 diff = t - f;
2905
2906 if (((code == NE || code == EQ) && diff < 0)
2907 || (code == GE || code == GT))
2908 {
2909 code = reverse_condition (code);
2910 std::swap (t, f);
2911 diff = -diff;
2912 }
2913
2914 subtarget = target = dest;
2915 if (mode != DImode)
2916 {
2917 target = gen_lowpart (DImode, dest);
2918 if (can_create_pseudo_p ())
2919 subtarget = gen_reg_rtx (DImode);
2920 else
2921 subtarget = target;
2922 }
2923 /* Below, we must be careful to use copy_rtx on target and subtarget
2924 in intermediate insns, as they may be a subreg rtx, which may not
2925 be shared. */
2926
2927 if (f == 0 && exact_log2 (diff) > 0
2928 /* On EV6, we've got enough shifters to make non-arithmetic shifts
2929 viable over a longer latency cmove. On EV5, the E0 slot is a
2930 scarce resource, and on EV4 shift has the same latency as a cmove. */
2931 && (diff <= 8 || alpha_tune == PROCESSOR_EV6))
2932 {
2933 tmp = gen_rtx_fmt_ee (code, DImode, cond, const0_rtx);
2934 emit_insn (gen_rtx_SET (copy_rtx (subtarget), tmp));
2935
2936 tmp = gen_rtx_ASHIFT (DImode, copy_rtx (subtarget),
2937 GEN_INT (exact_log2 (t)));
2938 emit_insn (gen_rtx_SET (target, tmp));
2939 }
2940 else if (f == 0 && t == -1)
2941 {
2942 tmp = gen_rtx_fmt_ee (code, DImode, cond, const0_rtx);
2943 emit_insn (gen_rtx_SET (copy_rtx (subtarget), tmp));
2944
2945 emit_insn (gen_negdi2 (target, copy_rtx (subtarget)));
2946 }
2947 else if (diff == 1 || diff == 4 || diff == 8)
2948 {
2949 rtx add_op;
2950
2951 tmp = gen_rtx_fmt_ee (code, DImode, cond, const0_rtx);
2952 emit_insn (gen_rtx_SET (copy_rtx (subtarget), tmp));
2953
2954 if (diff == 1)
2955 emit_insn (gen_adddi3 (target, copy_rtx (subtarget), GEN_INT (f)));
2956 else
2957 {
2958 add_op = GEN_INT (f);
2959 if (sext_add_operand (add_op, mode))
2960 {
2961 tmp = gen_rtx_ASHIFT (DImode, copy_rtx (subtarget),
2962 GEN_INT (exact_log2 (diff)));
2963 tmp = gen_rtx_PLUS (DImode, tmp, add_op);
2964 emit_insn (gen_rtx_SET (target, tmp));
2965 }
2966 else
2967 return 0;
2968 }
2969 }
2970 else
2971 return 0;
2972
2973 return 1;
2974 }
2975
2976 /* Look up the function X_floating library function name for the
2978 given operation. */
2979
2980 struct GTY(()) xfloating_op
2981 {
2982 const enum rtx_code code;
2983 const char *const GTY((skip)) osf_func;
2984 const char *const GTY((skip)) vms_func;
2985 rtx libcall;
2986 };
2987
2988 static GTY(()) struct xfloating_op xfloating_ops[] =
2989 {
2990 { PLUS, "_OtsAddX", "OTS$ADD_X", 0 },
2991 { MINUS, "_OtsSubX", "OTS$SUB_X", 0 },
2992 { MULT, "_OtsMulX", "OTS$MUL_X", 0 },
2993 { DIV, "_OtsDivX", "OTS$DIV_X", 0 },
2994 { EQ, "_OtsEqlX", "OTS$EQL_X", 0 },
2995 { NE, "_OtsNeqX", "OTS$NEQ_X", 0 },
2996 { LT, "_OtsLssX", "OTS$LSS_X", 0 },
2997 { LE, "_OtsLeqX", "OTS$LEQ_X", 0 },
2998 { GT, "_OtsGtrX", "OTS$GTR_X", 0 },
2999 { GE, "_OtsGeqX", "OTS$GEQ_X", 0 },
3000 { FIX, "_OtsCvtXQ", "OTS$CVTXQ", 0 },
3001 { FLOAT, "_OtsCvtQX", "OTS$CVTQX", 0 },
3002 { UNSIGNED_FLOAT, "_OtsCvtQUX", "OTS$CVTQUX", 0 },
3003 { FLOAT_EXTEND, "_OtsConvertFloatTX", "OTS$CVT_FLOAT_T_X", 0 },
3004 { FLOAT_TRUNCATE, "_OtsConvertFloatXT", "OTS$CVT_FLOAT_X_T", 0 }
3005 };
3006
3007 static GTY(()) struct xfloating_op vax_cvt_ops[] =
3008 {
3009 { FLOAT_EXTEND, "_OtsConvertFloatGX", "OTS$CVT_FLOAT_G_X", 0 },
3010 { FLOAT_TRUNCATE, "_OtsConvertFloatXG", "OTS$CVT_FLOAT_X_G", 0 }
3011 };
3012
3013 static rtx
3014 alpha_lookup_xfloating_lib_func (enum rtx_code code)
3015 {
3016 struct xfloating_op *ops = xfloating_ops;
3017 long n = ARRAY_SIZE (xfloating_ops);
3018 long i;
3019
3020 gcc_assert (TARGET_HAS_XFLOATING_LIBS);
3021
3022 /* How irritating. Nothing to key off for the main table. */
3023 if (TARGET_FLOAT_VAX && (code == FLOAT_EXTEND || code == FLOAT_TRUNCATE))
3024 {
3025 ops = vax_cvt_ops;
3026 n = ARRAY_SIZE (vax_cvt_ops);
3027 }
3028
3029 for (i = 0; i < n; ++i, ++ops)
3030 if (ops->code == code)
3031 {
3032 rtx func = ops->libcall;
3033 if (!func)
3034 {
3035 func = init_one_libfunc (TARGET_ABI_OPEN_VMS
3036 ? ops->vms_func : ops->osf_func);
3037 ops->libcall = func;
3038 }
3039 return func;
3040 }
3041
3042 gcc_unreachable ();
3043 }
3044
3045 /* Most X_floating operations take the rounding mode as an argument.
3046 Compute that here. */
3047
3048 static int
3049 alpha_compute_xfloating_mode_arg (enum rtx_code code,
3050 enum alpha_fp_rounding_mode round)
3051 {
3052 int mode;
3053
3054 switch (round)
3055 {
3056 case ALPHA_FPRM_NORM:
3057 mode = 2;
3058 break;
3059 case ALPHA_FPRM_MINF:
3060 mode = 1;
3061 break;
3062 case ALPHA_FPRM_CHOP:
3063 mode = 0;
3064 break;
3065 case ALPHA_FPRM_DYN:
3066 mode = 4;
3067 break;
3068 default:
3069 gcc_unreachable ();
3070
3071 /* XXX For reference, round to +inf is mode = 3. */
3072 }
3073
3074 if (code == FLOAT_TRUNCATE && alpha_fptm == ALPHA_FPTM_N)
3075 mode |= 0x10000;
3076
3077 return mode;
3078 }
3079
3080 /* Emit an X_floating library function call.
3081
3082 Note that these functions do not follow normal calling conventions:
3083 TFmode arguments are passed in two integer registers (as opposed to
3084 indirect); TFmode return values appear in R16+R17.
3085
3086 FUNC is the function to call.
3087 TARGET is where the output belongs.
3088 OPERANDS are the inputs.
3089 NOPERANDS is the count of inputs.
3090 EQUIV is the expression equivalent for the function.
3091 */
3092
3093 static void
3094 alpha_emit_xfloating_libcall (rtx func, rtx target, rtx operands[],
3095 int noperands, rtx equiv)
3096 {
3097 rtx usage = NULL_RTX, reg;
3098 int regno = 16, i;
3099
3100 start_sequence ();
3101
3102 for (i = 0; i < noperands; ++i)
3103 {
3104 switch (GET_MODE (operands[i]))
3105 {
3106 case E_TFmode:
3107 reg = gen_rtx_REG (TFmode, regno);
3108 regno += 2;
3109 break;
3110
3111 case E_DFmode:
3112 reg = gen_rtx_REG (DFmode, regno + 32);
3113 regno += 1;
3114 break;
3115
3116 case E_VOIDmode:
3117 gcc_assert (CONST_INT_P (operands[i]));
3118 /* FALLTHRU */
3119 case E_DImode:
3120 reg = gen_rtx_REG (DImode, regno);
3121 regno += 1;
3122 break;
3123
3124 default:
3125 gcc_unreachable ();
3126 }
3127
3128 emit_move_insn (reg, operands[i]);
3129 use_reg (&usage, reg);
3130 }
3131
3132 switch (GET_MODE (target))
3133 {
3134 case E_TFmode:
3135 reg = gen_rtx_REG (TFmode, 16);
3136 break;
3137 case E_DFmode:
3138 reg = gen_rtx_REG (DFmode, 32);
3139 break;
3140 case E_DImode:
3141 reg = gen_rtx_REG (DImode, 0);
3142 break;
3143 default:
3144 gcc_unreachable ();
3145 }
3146
3147 rtx mem = gen_rtx_MEM (QImode, func);
3148 rtx_insn *tmp = emit_call_insn (gen_call_value (reg, mem, const0_rtx,
3149 const0_rtx, const0_rtx));
3150 CALL_INSN_FUNCTION_USAGE (tmp) = usage;
3151 RTL_CONST_CALL_P (tmp) = 1;
3152
3153 tmp = get_insns ();
3154 end_sequence ();
3155
3156 emit_libcall_block (tmp, target, reg, equiv);
3157 }
3158
3159 /* Emit an X_floating library function call for arithmetic (+,-,*,/). */
3160
3161 void
3162 alpha_emit_xfloating_arith (enum rtx_code code, rtx operands[])
3163 {
3164 rtx func;
3165 int mode;
3166 rtx out_operands[3];
3167
3168 func = alpha_lookup_xfloating_lib_func (code);
3169 mode = alpha_compute_xfloating_mode_arg (code, alpha_fprm);
3170
3171 out_operands[0] = operands[1];
3172 out_operands[1] = operands[2];
3173 out_operands[2] = GEN_INT (mode);
3174 alpha_emit_xfloating_libcall (func, operands[0], out_operands, 3,
3175 gen_rtx_fmt_ee (code, TFmode, operands[1],
3176 operands[2]));
3177 }
3178
3179 /* Emit an X_floating library function call for a comparison. */
3180
3181 static rtx
3182 alpha_emit_xfloating_compare (enum rtx_code *pcode, rtx op0, rtx op1)
3183 {
3184 enum rtx_code cmp_code, res_code;
3185 rtx func, out, operands[2], note;
3186
3187 /* X_floating library comparison functions return
3188 -1 unordered
3189 0 false
3190 1 true
3191 Convert the compare against the raw return value. */
3192
3193 cmp_code = *pcode;
3194 switch (cmp_code)
3195 {
3196 case UNORDERED:
3197 cmp_code = EQ;
3198 res_code = LT;
3199 break;
3200 case ORDERED:
3201 cmp_code = EQ;
3202 res_code = GE;
3203 break;
3204 case NE:
3205 res_code = NE;
3206 break;
3207 case EQ:
3208 case LT:
3209 case GT:
3210 case LE:
3211 case GE:
3212 res_code = GT;
3213 break;
3214 default:
3215 gcc_unreachable ();
3216 }
3217 *pcode = res_code;
3218
3219 func = alpha_lookup_xfloating_lib_func (cmp_code);
3220
3221 operands[0] = op0;
3222 operands[1] = op1;
3223 out = gen_reg_rtx (DImode);
3224
3225 /* What's actually returned is -1,0,1, not a proper boolean value. */
3226 note = gen_rtx_fmt_ee (cmp_code, VOIDmode, op0, op1);
3227 note = gen_rtx_UNSPEC (DImode, gen_rtvec (1, note), UNSPEC_XFLT_COMPARE);
3228 alpha_emit_xfloating_libcall (func, out, operands, 2, note);
3229
3230 return out;
3231 }
3232
3233 /* Emit an X_floating library function call for a conversion. */
3234
3235 void
3236 alpha_emit_xfloating_cvt (enum rtx_code orig_code, rtx operands[])
3237 {
3238 int noperands = 1, mode;
3239 rtx out_operands[2];
3240 rtx func;
3241 enum rtx_code code = orig_code;
3242
3243 if (code == UNSIGNED_FIX)
3244 code = FIX;
3245
3246 func = alpha_lookup_xfloating_lib_func (code);
3247
3248 out_operands[0] = operands[1];
3249
3250 switch (code)
3251 {
3252 case FIX:
3253 mode = alpha_compute_xfloating_mode_arg (code, ALPHA_FPRM_CHOP);
3254 out_operands[1] = GEN_INT (mode);
3255 noperands = 2;
3256 break;
3257 case FLOAT_TRUNCATE:
3258 mode = alpha_compute_xfloating_mode_arg (code, alpha_fprm);
3259 out_operands[1] = GEN_INT (mode);
3260 noperands = 2;
3261 break;
3262 default:
3263 break;
3264 }
3265
3266 alpha_emit_xfloating_libcall (func, operands[0], out_operands, noperands,
3267 gen_rtx_fmt_e (orig_code,
3268 GET_MODE (operands[0]),
3269 operands[1]));
3270 }
3271
3272 /* Split a TImode or TFmode move from OP[1] to OP[0] into a pair of
3273 DImode moves from OP[2,3] to OP[0,1]. If FIXUP_OVERLAP is true,
3274 guarantee that the sequence
3275 set (OP[0] OP[2])
3276 set (OP[1] OP[3])
3277 is valid. Naturally, output operand ordering is little-endian.
3278 This is used by *movtf_internal and *movti_internal. */
3279
3280 void
3281 alpha_split_tmode_pair (rtx operands[4], machine_mode mode,
3282 bool fixup_overlap)
3283 {
3284 switch (GET_CODE (operands[1]))
3285 {
3286 case REG:
3287 operands[3] = gen_rtx_REG (DImode, REGNO (operands[1]) + 1);
3288 operands[2] = gen_rtx_REG (DImode, REGNO (operands[1]));
3289 break;
3290
3291 case MEM:
3292 operands[3] = adjust_address (operands[1], DImode, 8);
3293 operands[2] = adjust_address (operands[1], DImode, 0);
3294 break;
3295
3296 CASE_CONST_SCALAR_INT:
3297 case CONST_DOUBLE:
3298 gcc_assert (operands[1] == CONST0_RTX (mode));
3299 operands[2] = operands[3] = const0_rtx;
3300 break;
3301
3302 default:
3303 gcc_unreachable ();
3304 }
3305
3306 switch (GET_CODE (operands[0]))
3307 {
3308 case REG:
3309 operands[1] = gen_rtx_REG (DImode, REGNO (operands[0]) + 1);
3310 operands[0] = gen_rtx_REG (DImode, REGNO (operands[0]));
3311 break;
3312
3313 case MEM:
3314 operands[1] = adjust_address (operands[0], DImode, 8);
3315 operands[0] = adjust_address (operands[0], DImode, 0);
3316 break;
3317
3318 default:
3319 gcc_unreachable ();
3320 }
3321
3322 if (fixup_overlap && reg_overlap_mentioned_p (operands[0], operands[3]))
3323 {
3324 std::swap (operands[0], operands[1]);
3325 std::swap (operands[2], operands[3]);
3326 }
3327 }
3328
3329 /* Implement negtf2 or abstf2. Op0 is destination, op1 is source,
3330 op2 is a register containing the sign bit, operation is the
3331 logical operation to be performed. */
3332
3333 void
3334 alpha_split_tfmode_frobsign (rtx operands[3], rtx (*operation) (rtx, rtx, rtx))
3335 {
3336 rtx high_bit = operands[2];
3337 rtx scratch;
3338 int move;
3339
3340 alpha_split_tmode_pair (operands, TFmode, false);
3341
3342 /* Detect three flavors of operand overlap. */
3343 move = 1;
3344 if (rtx_equal_p (operands[0], operands[2]))
3345 move = 0;
3346 else if (rtx_equal_p (operands[1], operands[2]))
3347 {
3348 if (rtx_equal_p (operands[0], high_bit))
3349 move = 2;
3350 else
3351 move = -1;
3352 }
3353
3354 if (move < 0)
3355 emit_move_insn (operands[0], operands[2]);
3356
3357 /* ??? If the destination overlaps both source tf and high_bit, then
3358 assume source tf is dead in its entirety and use the other half
3359 for a scratch register. Otherwise "scratch" is just the proper
3360 destination register. */
3361 scratch = operands[move < 2 ? 1 : 3];
3362
3363 emit_insn ((*operation) (scratch, high_bit, operands[3]));
3364
3365 if (move > 0)
3366 {
3367 emit_move_insn (operands[0], operands[2]);
3368 if (move > 1)
3369 emit_move_insn (operands[1], scratch);
3370 }
3371 }
3372
3373 /* Use ext[wlq][lh] as the Architecture Handbook describes for extracting
3375 unaligned data:
3376
3377 unsigned: signed:
3378 word: ldq_u r1,X(r11) ldq_u r1,X(r11)
3379 ldq_u r2,X+1(r11) ldq_u r2,X+1(r11)
3380 lda r3,X(r11) lda r3,X+2(r11)
3381 extwl r1,r3,r1 extql r1,r3,r1
3382 extwh r2,r3,r2 extqh r2,r3,r2
3383 or r1.r2.r1 or r1,r2,r1
3384 sra r1,48,r1
3385
3386 long: ldq_u r1,X(r11) ldq_u r1,X(r11)
3387 ldq_u r2,X+3(r11) ldq_u r2,X+3(r11)
3388 lda r3,X(r11) lda r3,X(r11)
3389 extll r1,r3,r1 extll r1,r3,r1
3390 extlh r2,r3,r2 extlh r2,r3,r2
3391 or r1.r2.r1 addl r1,r2,r1
3392
3393 quad: ldq_u r1,X(r11)
3394 ldq_u r2,X+7(r11)
3395 lda r3,X(r11)
3396 extql r1,r3,r1
3397 extqh r2,r3,r2
3398 or r1.r2.r1
3399 */
3400
3401 void
3402 alpha_expand_unaligned_load (rtx tgt, rtx mem, HOST_WIDE_INT size,
3403 HOST_WIDE_INT ofs, int sign)
3404 {
3405 rtx meml, memh, addr, extl, exth, tmp, mema;
3406 machine_mode mode;
3407
3408 if (TARGET_BWX && size == 2)
3409 {
3410 meml = adjust_address (mem, QImode, ofs);
3411 memh = adjust_address (mem, QImode, ofs+1);
3412 extl = gen_reg_rtx (DImode);
3413 exth = gen_reg_rtx (DImode);
3414 emit_insn (gen_zero_extendqidi2 (extl, meml));
3415 emit_insn (gen_zero_extendqidi2 (exth, memh));
3416 exth = expand_simple_binop (DImode, ASHIFT, exth, GEN_INT (8),
3417 NULL, 1, OPTAB_LIB_WIDEN);
3418 addr = expand_simple_binop (DImode, IOR, extl, exth,
3419 NULL, 1, OPTAB_LIB_WIDEN);
3420
3421 if (sign && GET_MODE (tgt) != HImode)
3422 {
3423 addr = gen_lowpart (HImode, addr);
3424 emit_insn (gen_extend_insn (tgt, addr, GET_MODE (tgt), HImode, 0));
3425 }
3426 else
3427 {
3428 if (GET_MODE (tgt) != DImode)
3429 addr = gen_lowpart (GET_MODE (tgt), addr);
3430 emit_move_insn (tgt, addr);
3431 }
3432 return;
3433 }
3434
3435 meml = gen_reg_rtx (DImode);
3436 memh = gen_reg_rtx (DImode);
3437 addr = gen_reg_rtx (DImode);
3438 extl = gen_reg_rtx (DImode);
3439 exth = gen_reg_rtx (DImode);
3440
3441 mema = XEXP (mem, 0);
3442 if (GET_CODE (mema) == LO_SUM)
3443 mema = force_reg (Pmode, mema);
3444
3445 /* AND addresses cannot be in any alias set, since they may implicitly
3446 alias surrounding code. Ideally we'd have some alias set that
3447 covered all types except those with alignment 8 or higher. */
3448
3449 tmp = change_address (mem, DImode,
3450 gen_rtx_AND (DImode,
3451 plus_constant (DImode, mema, ofs),
3452 GEN_INT (-8)));
3453 set_mem_alias_set (tmp, 0);
3454 emit_move_insn (meml, tmp);
3455
3456 tmp = change_address (mem, DImode,
3457 gen_rtx_AND (DImode,
3458 plus_constant (DImode, mema,
3459 ofs + size - 1),
3460 GEN_INT (-8)));
3461 set_mem_alias_set (tmp, 0);
3462 emit_move_insn (memh, tmp);
3463
3464 if (sign && size == 2)
3465 {
3466 emit_move_insn (addr, plus_constant (Pmode, mema, ofs+2));
3467
3468 emit_insn (gen_extql (extl, meml, addr));
3469 emit_insn (gen_extqh (exth, memh, addr));
3470
3471 /* We must use tgt here for the target. Alpha-vms port fails if we use
3472 addr for the target, because addr is marked as a pointer and combine
3473 knows that pointers are always sign-extended 32-bit values. */
3474 addr = expand_binop (DImode, ior_optab, extl, exth, tgt, 1, OPTAB_WIDEN);
3475 addr = expand_binop (DImode, ashr_optab, addr, GEN_INT (48),
3476 addr, 1, OPTAB_WIDEN);
3477 }
3478 else
3479 {
3480 emit_move_insn (addr, plus_constant (Pmode, mema, ofs));
3481 emit_insn (gen_extxl (extl, meml, GEN_INT (size*8), addr));
3482 switch ((int) size)
3483 {
3484 case 2:
3485 emit_insn (gen_extwh (exth, memh, addr));
3486 mode = HImode;
3487 break;
3488 case 4:
3489 emit_insn (gen_extlh (exth, memh, addr));
3490 mode = SImode;
3491 break;
3492 case 8:
3493 emit_insn (gen_extqh (exth, memh, addr));
3494 mode = DImode;
3495 break;
3496 default:
3497 gcc_unreachable ();
3498 }
3499
3500 addr = expand_binop (mode, ior_optab, gen_lowpart (mode, extl),
3501 gen_lowpart (mode, exth), gen_lowpart (mode, tgt),
3502 sign, OPTAB_WIDEN);
3503 }
3504
3505 if (addr != tgt)
3506 emit_move_insn (tgt, gen_lowpart (GET_MODE (tgt), addr));
3507 }
3508
3509 /* Similarly, use ins and msk instructions to perform unaligned stores. */
3510
3511 void
3512 alpha_expand_unaligned_store (rtx dst, rtx src,
3513 HOST_WIDE_INT size, HOST_WIDE_INT ofs)
3514 {
3515 rtx dstl, dsth, addr, insl, insh, meml, memh, dsta;
3516
3517 if (TARGET_BWX && size == 2)
3518 {
3519 if (src != const0_rtx)
3520 {
3521 dstl = gen_lowpart (QImode, src);
3522 dsth = expand_simple_binop (DImode, LSHIFTRT, src, GEN_INT (8),
3523 NULL, 1, OPTAB_LIB_WIDEN);
3524 dsth = gen_lowpart (QImode, dsth);
3525 }
3526 else
3527 dstl = dsth = const0_rtx;
3528
3529 meml = adjust_address (dst, QImode, ofs);
3530 memh = adjust_address (dst, QImode, ofs+1);
3531
3532 emit_move_insn (meml, dstl);
3533 emit_move_insn (memh, dsth);
3534 return;
3535 }
3536
3537 dstl = gen_reg_rtx (DImode);
3538 dsth = gen_reg_rtx (DImode);
3539 insl = gen_reg_rtx (DImode);
3540 insh = gen_reg_rtx (DImode);
3541
3542 dsta = XEXP (dst, 0);
3543 if (GET_CODE (dsta) == LO_SUM)
3544 dsta = force_reg (Pmode, dsta);
3545
3546 /* AND addresses cannot be in any alias set, since they may implicitly
3547 alias surrounding code. Ideally we'd have some alias set that
3548 covered all types except those with alignment 8 or higher. */
3549
3550 meml = change_address (dst, DImode,
3551 gen_rtx_AND (DImode,
3552 plus_constant (DImode, dsta, ofs),
3553 GEN_INT (-8)));
3554 set_mem_alias_set (meml, 0);
3555
3556 memh = change_address (dst, DImode,
3557 gen_rtx_AND (DImode,
3558 plus_constant (DImode, dsta,
3559 ofs + size - 1),
3560 GEN_INT (-8)));
3561 set_mem_alias_set (memh, 0);
3562
3563 emit_move_insn (dsth, memh);
3564 emit_move_insn (dstl, meml);
3565
3566 addr = copy_addr_to_reg (plus_constant (Pmode, dsta, ofs));
3567
3568 if (src != CONST0_RTX (GET_MODE (src)))
3569 {
3570 emit_insn (gen_insxh (insh, gen_lowpart (DImode, src),
3571 GEN_INT (size*8), addr));
3572
3573 switch ((int) size)
3574 {
3575 case 2:
3576 emit_insn (gen_inswl (insl, gen_lowpart (HImode, src), addr));
3577 break;
3578 case 4:
3579 emit_insn (gen_insll (insl, gen_lowpart (SImode, src), addr));
3580 break;
3581 case 8:
3582 emit_insn (gen_insql (insl, gen_lowpart (DImode, src), addr));
3583 break;
3584 default:
3585 gcc_unreachable ();
3586 }
3587 }
3588
3589 emit_insn (gen_mskxh (dsth, dsth, GEN_INT (size*8), addr));
3590
3591 switch ((int) size)
3592 {
3593 case 2:
3594 emit_insn (gen_mskwl (dstl, dstl, addr));
3595 break;
3596 case 4:
3597 emit_insn (gen_mskll (dstl, dstl, addr));
3598 break;
3599 case 8:
3600 emit_insn (gen_mskql (dstl, dstl, addr));
3601 break;
3602 default:
3603 gcc_unreachable ();
3604 }
3605
3606 if (src != CONST0_RTX (GET_MODE (src)))
3607 {
3608 dsth = expand_binop (DImode, ior_optab, insh, dsth, dsth, 0, OPTAB_WIDEN);
3609 dstl = expand_binop (DImode, ior_optab, insl, dstl, dstl, 0, OPTAB_WIDEN);
3610 }
3611
3612 /* Must store high before low for degenerate case of aligned. */
3613 emit_move_insn (memh, dsth);
3614 emit_move_insn (meml, dstl);
3615 }
3616
3617 /* The block move code tries to maximize speed by separating loads and
3618 stores at the expense of register pressure: we load all of the data
3619 before we store it back out. There are two secondary effects worth
3620 mentioning, that this speeds copying to/from aligned and unaligned
3621 buffers, and that it makes the code significantly easier to write. */
3622
3623 #define MAX_MOVE_WORDS 8
3624
3625 /* Load an integral number of consecutive unaligned quadwords. */
3626
3627 static void
3628 alpha_expand_unaligned_load_words (rtx *out_regs, rtx smem,
3629 HOST_WIDE_INT words, HOST_WIDE_INT ofs)
3630 {
3631 rtx const im8 = GEN_INT (-8);
3632 rtx ext_tmps[MAX_MOVE_WORDS], data_regs[MAX_MOVE_WORDS+1];
3633 rtx sreg, areg, tmp, smema;
3634 HOST_WIDE_INT i;
3635
3636 smema = XEXP (smem, 0);
3637 if (GET_CODE (smema) == LO_SUM)
3638 smema = force_reg (Pmode, smema);
3639
3640 /* Generate all the tmp registers we need. */
3641 for (i = 0; i < words; ++i)
3642 {
3643 data_regs[i] = out_regs[i];
3644 ext_tmps[i] = gen_reg_rtx (DImode);
3645 }
3646 data_regs[words] = gen_reg_rtx (DImode);
3647
3648 if (ofs != 0)
3649 smem = adjust_address (smem, GET_MODE (smem), ofs);
3650
3651 /* Load up all of the source data. */
3652 for (i = 0; i < words; ++i)
3653 {
3654 tmp = change_address (smem, DImode,
3655 gen_rtx_AND (DImode,
3656 plus_constant (DImode, smema, 8*i),
3657 im8));
3658 set_mem_alias_set (tmp, 0);
3659 emit_move_insn (data_regs[i], tmp);
3660 }
3661
3662 tmp = change_address (smem, DImode,
3663 gen_rtx_AND (DImode,
3664 plus_constant (DImode, smema,
3665 8*words - 1),
3666 im8));
3667 set_mem_alias_set (tmp, 0);
3668 emit_move_insn (data_regs[words], tmp);
3669
3670 /* Extract the half-word fragments. Unfortunately DEC decided to make
3671 extxh with offset zero a noop instead of zeroing the register, so
3672 we must take care of that edge condition ourselves with cmov. */
3673
3674 sreg = copy_addr_to_reg (smema);
3675 areg = expand_binop (DImode, and_optab, sreg, GEN_INT (7), NULL,
3676 1, OPTAB_WIDEN);
3677 for (i = 0; i < words; ++i)
3678 {
3679 emit_insn (gen_extql (data_regs[i], data_regs[i], sreg));
3680 emit_insn (gen_extqh (ext_tmps[i], data_regs[i+1], sreg));
3681 emit_insn (gen_rtx_SET (ext_tmps[i],
3682 gen_rtx_IF_THEN_ELSE (DImode,
3683 gen_rtx_EQ (DImode, areg,
3684 const0_rtx),
3685 const0_rtx, ext_tmps[i])));
3686 }
3687
3688 /* Merge the half-words into whole words. */
3689 for (i = 0; i < words; ++i)
3690 {
3691 out_regs[i] = expand_binop (DImode, ior_optab, data_regs[i],
3692 ext_tmps[i], data_regs[i], 1, OPTAB_WIDEN);
3693 }
3694 }
3695
3696 /* Store an integral number of consecutive unaligned quadwords. DATA_REGS
3697 may be NULL to store zeros. */
3698
3699 static void
3700 alpha_expand_unaligned_store_words (rtx *data_regs, rtx dmem,
3701 HOST_WIDE_INT words, HOST_WIDE_INT ofs)
3702 {
3703 rtx const im8 = GEN_INT (-8);
3704 rtx ins_tmps[MAX_MOVE_WORDS];
3705 rtx st_tmp_1, st_tmp_2, dreg;
3706 rtx st_addr_1, st_addr_2, dmema;
3707 HOST_WIDE_INT i;
3708
3709 dmema = XEXP (dmem, 0);
3710 if (GET_CODE (dmema) == LO_SUM)
3711 dmema = force_reg (Pmode, dmema);
3712
3713 /* Generate all the tmp registers we need. */
3714 if (data_regs != NULL)
3715 for (i = 0; i < words; ++i)
3716 ins_tmps[i] = gen_reg_rtx(DImode);
3717 st_tmp_1 = gen_reg_rtx(DImode);
3718 st_tmp_2 = gen_reg_rtx(DImode);
3719
3720 if (ofs != 0)
3721 dmem = adjust_address (dmem, GET_MODE (dmem), ofs);
3722
3723 st_addr_2 = change_address (dmem, DImode,
3724 gen_rtx_AND (DImode,
3725 plus_constant (DImode, dmema,
3726 words*8 - 1),
3727 im8));
3728 set_mem_alias_set (st_addr_2, 0);
3729
3730 st_addr_1 = change_address (dmem, DImode,
3731 gen_rtx_AND (DImode, dmema, im8));
3732 set_mem_alias_set (st_addr_1, 0);
3733
3734 /* Load up the destination end bits. */
3735 emit_move_insn (st_tmp_2, st_addr_2);
3736 emit_move_insn (st_tmp_1, st_addr_1);
3737
3738 /* Shift the input data into place. */
3739 dreg = copy_addr_to_reg (dmema);
3740 if (data_regs != NULL)
3741 {
3742 for (i = words-1; i >= 0; --i)
3743 {
3744 emit_insn (gen_insqh (ins_tmps[i], data_regs[i], dreg));
3745 emit_insn (gen_insql (data_regs[i], data_regs[i], dreg));
3746 }
3747 for (i = words-1; i > 0; --i)
3748 {
3749 ins_tmps[i-1] = expand_binop (DImode, ior_optab, data_regs[i],
3750 ins_tmps[i-1], ins_tmps[i-1], 1,
3751 OPTAB_WIDEN);
3752 }
3753 }
3754
3755 /* Split and merge the ends with the destination data. */
3756 emit_insn (gen_mskqh (st_tmp_2, st_tmp_2, dreg));
3757 emit_insn (gen_mskql (st_tmp_1, st_tmp_1, dreg));
3758
3759 if (data_regs != NULL)
3760 {
3761 st_tmp_2 = expand_binop (DImode, ior_optab, st_tmp_2, ins_tmps[words-1],
3762 st_tmp_2, 1, OPTAB_WIDEN);
3763 st_tmp_1 = expand_binop (DImode, ior_optab, st_tmp_1, data_regs[0],
3764 st_tmp_1, 1, OPTAB_WIDEN);
3765 }
3766
3767 /* Store it all. */
3768 emit_move_insn (st_addr_2, st_tmp_2);
3769 for (i = words-1; i > 0; --i)
3770 {
3771 rtx tmp = change_address (dmem, DImode,
3772 gen_rtx_AND (DImode,
3773 plus_constant (DImode,
3774 dmema, i*8),
3775 im8));
3776 set_mem_alias_set (tmp, 0);
3777 emit_move_insn (tmp, data_regs ? ins_tmps[i-1] : const0_rtx);
3778 }
3779 emit_move_insn (st_addr_1, st_tmp_1);
3780 }
3781
3782
3783 /* Expand string/block move operations.
3784
3785 operands[0] is the pointer to the destination.
3786 operands[1] is the pointer to the source.
3787 operands[2] is the number of bytes to move.
3788 operands[3] is the alignment. */
3789
3790 int
3791 alpha_expand_block_move (rtx operands[])
3792 {
3793 rtx bytes_rtx = operands[2];
3794 rtx align_rtx = operands[3];
3795 HOST_WIDE_INT orig_bytes = INTVAL (bytes_rtx);
3796 HOST_WIDE_INT bytes = orig_bytes;
3797 HOST_WIDE_INT src_align = INTVAL (align_rtx) * BITS_PER_UNIT;
3798 HOST_WIDE_INT dst_align = src_align;
3799 rtx orig_src = operands[1];
3800 rtx orig_dst = operands[0];
3801 rtx data_regs[2 * MAX_MOVE_WORDS + 16];
3802 rtx tmp;
3803 unsigned int i, words, ofs, nregs = 0;
3804
3805 if (orig_bytes <= 0)
3806 return 1;
3807 else if (orig_bytes > MAX_MOVE_WORDS * UNITS_PER_WORD)
3808 return 0;
3809
3810 /* Look for additional alignment information from recorded register info. */
3811
3812 tmp = XEXP (orig_src, 0);
3813 if (REG_P (tmp))
3814 src_align = MAX (src_align, REGNO_POINTER_ALIGN (REGNO (tmp)));
3815 else if (GET_CODE (tmp) == PLUS
3816 && REG_P (XEXP (tmp, 0))
3817 && CONST_INT_P (XEXP (tmp, 1)))
3818 {
3819 unsigned HOST_WIDE_INT c = INTVAL (XEXP (tmp, 1));
3820 unsigned int a = REGNO_POINTER_ALIGN (REGNO (XEXP (tmp, 0)));
3821
3822 if (a > src_align)
3823 {
3824 if (a >= 64 && c % 8 == 0)
3825 src_align = 64;
3826 else if (a >= 32 && c % 4 == 0)
3827 src_align = 32;
3828 else if (a >= 16 && c % 2 == 0)
3829 src_align = 16;
3830 }
3831 }
3832
3833 tmp = XEXP (orig_dst, 0);
3834 if (REG_P (tmp))
3835 dst_align = MAX (dst_align, REGNO_POINTER_ALIGN (REGNO (tmp)));
3836 else if (GET_CODE (tmp) == PLUS
3837 && REG_P (XEXP (tmp, 0))
3838 && CONST_INT_P (XEXP (tmp, 1)))
3839 {
3840 unsigned HOST_WIDE_INT c = INTVAL (XEXP (tmp, 1));
3841 unsigned int a = REGNO_POINTER_ALIGN (REGNO (XEXP (tmp, 0)));
3842
3843 if (a > dst_align)
3844 {
3845 if (a >= 64 && c % 8 == 0)
3846 dst_align = 64;
3847 else if (a >= 32 && c % 4 == 0)
3848 dst_align = 32;
3849 else if (a >= 16 && c % 2 == 0)
3850 dst_align = 16;
3851 }
3852 }
3853
3854 ofs = 0;
3855 if (src_align >= 64 && bytes >= 8)
3856 {
3857 words = bytes / 8;
3858
3859 for (i = 0; i < words; ++i)
3860 data_regs[nregs + i] = gen_reg_rtx (DImode);
3861
3862 for (i = 0; i < words; ++i)
3863 emit_move_insn (data_regs[nregs + i],
3864 adjust_address (orig_src, DImode, ofs + i * 8));
3865
3866 nregs += words;
3867 bytes -= words * 8;
3868 ofs += words * 8;
3869 }
3870
3871 if (src_align >= 32 && bytes >= 4)
3872 {
3873 words = bytes / 4;
3874
3875 for (i = 0; i < words; ++i)
3876 data_regs[nregs + i] = gen_reg_rtx (SImode);
3877
3878 for (i = 0; i < words; ++i)
3879 emit_move_insn (data_regs[nregs + i],
3880 adjust_address (orig_src, SImode, ofs + i * 4));
3881
3882 nregs += words;
3883 bytes -= words * 4;
3884 ofs += words * 4;
3885 }
3886
3887 if (bytes >= 8)
3888 {
3889 words = bytes / 8;
3890
3891 for (i = 0; i < words+1; ++i)
3892 data_regs[nregs + i] = gen_reg_rtx (DImode);
3893
3894 alpha_expand_unaligned_load_words (data_regs + nregs, orig_src,
3895 words, ofs);
3896
3897 nregs += words;
3898 bytes -= words * 8;
3899 ofs += words * 8;
3900 }
3901
3902 if (! TARGET_BWX && bytes >= 4)
3903 {
3904 data_regs[nregs++] = tmp = gen_reg_rtx (SImode);
3905 alpha_expand_unaligned_load (tmp, orig_src, 4, ofs, 0);
3906 bytes -= 4;
3907 ofs += 4;
3908 }
3909
3910 if (bytes >= 2)
3911 {
3912 if (src_align >= 16)
3913 {
3914 do {
3915 data_regs[nregs++] = tmp = gen_reg_rtx (HImode);
3916 emit_move_insn (tmp, adjust_address (orig_src, HImode, ofs));
3917 bytes -= 2;
3918 ofs += 2;
3919 } while (bytes >= 2);
3920 }
3921 else if (! TARGET_BWX)
3922 {
3923 data_regs[nregs++] = tmp = gen_reg_rtx (HImode);
3924 alpha_expand_unaligned_load (tmp, orig_src, 2, ofs, 0);
3925 bytes -= 2;
3926 ofs += 2;
3927 }
3928 }
3929
3930 while (bytes > 0)
3931 {
3932 data_regs[nregs++] = tmp = gen_reg_rtx (QImode);
3933 emit_move_insn (tmp, adjust_address (orig_src, QImode, ofs));
3934 bytes -= 1;
3935 ofs += 1;
3936 }
3937
3938 gcc_assert (nregs <= ARRAY_SIZE (data_regs));
3939
3940 /* Now save it back out again. */
3941
3942 i = 0, ofs = 0;
3943
3944 /* Write out the data in whatever chunks reading the source allowed. */
3945 if (dst_align >= 64)
3946 {
3947 while (i < nregs && GET_MODE (data_regs[i]) == DImode)
3948 {
3949 emit_move_insn (adjust_address (orig_dst, DImode, ofs),
3950 data_regs[i]);
3951 ofs += 8;
3952 i++;
3953 }
3954 }
3955
3956 if (dst_align >= 32)
3957 {
3958 /* If the source has remaining DImode regs, write them out in
3959 two pieces. */
3960 while (i < nregs && GET_MODE (data_regs[i]) == DImode)
3961 {
3962 tmp = expand_binop (DImode, lshr_optab, data_regs[i], GEN_INT (32),
3963 NULL_RTX, 1, OPTAB_WIDEN);
3964
3965 emit_move_insn (adjust_address (orig_dst, SImode, ofs),
3966 gen_lowpart (SImode, data_regs[i]));
3967 emit_move_insn (adjust_address (orig_dst, SImode, ofs + 4),
3968 gen_lowpart (SImode, tmp));
3969 ofs += 8;
3970 i++;
3971 }
3972
3973 while (i < nregs && GET_MODE (data_regs[i]) == SImode)
3974 {
3975 emit_move_insn (adjust_address (orig_dst, SImode, ofs),
3976 data_regs[i]);
3977 ofs += 4;
3978 i++;
3979 }
3980 }
3981
3982 if (i < nregs && GET_MODE (data_regs[i]) == DImode)
3983 {
3984 /* Write out a remaining block of words using unaligned methods. */
3985
3986 for (words = 1; i + words < nregs; words++)
3987 if (GET_MODE (data_regs[i + words]) != DImode)
3988 break;
3989
3990 if (words == 1)
3991 alpha_expand_unaligned_store (orig_dst, data_regs[i], 8, ofs);
3992 else
3993 alpha_expand_unaligned_store_words (data_regs + i, orig_dst,
3994 words, ofs);
3995
3996 i += words;
3997 ofs += words * 8;
3998 }
3999
4000 /* Due to the above, this won't be aligned. */
4001 /* ??? If we have more than one of these, consider constructing full
4002 words in registers and using alpha_expand_unaligned_store_words. */
4003 while (i < nregs && GET_MODE (data_regs[i]) == SImode)
4004 {
4005 alpha_expand_unaligned_store (orig_dst, data_regs[i], 4, ofs);
4006 ofs += 4;
4007 i++;
4008 }
4009
4010 if (dst_align >= 16)
4011 while (i < nregs && GET_MODE (data_regs[i]) == HImode)
4012 {
4013 emit_move_insn (adjust_address (orig_dst, HImode, ofs), data_regs[i]);
4014 i++;
4015 ofs += 2;
4016 }
4017 else
4018 while (i < nregs && GET_MODE (data_regs[i]) == HImode)
4019 {
4020 alpha_expand_unaligned_store (orig_dst, data_regs[i], 2, ofs);
4021 i++;
4022 ofs += 2;
4023 }
4024
4025 /* The remainder must be byte copies. */
4026 while (i < nregs)
4027 {
4028 gcc_assert (GET_MODE (data_regs[i]) == QImode);
4029 emit_move_insn (adjust_address (orig_dst, QImode, ofs), data_regs[i]);
4030 i++;
4031 ofs += 1;
4032 }
4033
4034 return 1;
4035 }
4036
4037 int
4038 alpha_expand_block_clear (rtx operands[])
4039 {
4040 rtx bytes_rtx = operands[1];
4041 rtx align_rtx = operands[3];
4042 HOST_WIDE_INT orig_bytes = INTVAL (bytes_rtx);
4043 HOST_WIDE_INT bytes = orig_bytes;
4044 HOST_WIDE_INT align = INTVAL (align_rtx) * BITS_PER_UNIT;
4045 HOST_WIDE_INT alignofs = 0;
4046 rtx orig_dst = operands[0];
4047 rtx tmp;
4048 int i, words, ofs = 0;
4049
4050 if (orig_bytes <= 0)
4051 return 1;
4052 if (orig_bytes > MAX_MOVE_WORDS * UNITS_PER_WORD)
4053 return 0;
4054
4055 /* Look for stricter alignment. */
4056 tmp = XEXP (orig_dst, 0);
4057 if (REG_P (tmp))
4058 align = MAX (align, REGNO_POINTER_ALIGN (REGNO (tmp)));
4059 else if (GET_CODE (tmp) == PLUS
4060 && REG_P (XEXP (tmp, 0))
4061 && CONST_INT_P (XEXP (tmp, 1)))
4062 {
4063 HOST_WIDE_INT c = INTVAL (XEXP (tmp, 1));
4064 int a = REGNO_POINTER_ALIGN (REGNO (XEXP (tmp, 0)));
4065
4066 if (a > align)
4067 {
4068 if (a >= 64)
4069 align = a, alignofs = 8 - c % 8;
4070 else if (a >= 32)
4071 align = a, alignofs = 4 - c % 4;
4072 else if (a >= 16)
4073 align = a, alignofs = 2 - c % 2;
4074 }
4075 }
4076
4077 /* Handle an unaligned prefix first. */
4078
4079 if (alignofs > 0)
4080 {
4081 /* Given that alignofs is bounded by align, the only time BWX could
4082 generate three stores is for a 7 byte fill. Prefer two individual
4083 stores over a load/mask/store sequence. */
4084 if ((!TARGET_BWX || alignofs == 7)
4085 && align >= 32
4086 && !(alignofs == 4 && bytes >= 4))
4087 {
4088 machine_mode mode = (align >= 64 ? DImode : SImode);
4089 int inv_alignofs = (align >= 64 ? 8 : 4) - alignofs;
4090 rtx mem, tmp;
4091 HOST_WIDE_INT mask;
4092
4093 mem = adjust_address (orig_dst, mode, ofs - inv_alignofs);
4094 set_mem_alias_set (mem, 0);
4095
4096 mask = ~(HOST_WIDE_INT_M1U << (inv_alignofs * 8));
4097 if (bytes < alignofs)
4098 {
4099 mask |= HOST_WIDE_INT_M1U << ((inv_alignofs + bytes) * 8);
4100 ofs += bytes;
4101 bytes = 0;
4102 }
4103 else
4104 {
4105 bytes -= alignofs;
4106 ofs += alignofs;
4107 }
4108 alignofs = 0;
4109
4110 tmp = expand_binop (mode, and_optab, mem, GEN_INT (mask),
4111 NULL_RTX, 1, OPTAB_WIDEN);
4112
4113 emit_move_insn (mem, tmp);
4114 }
4115
4116 if (TARGET_BWX && (alignofs & 1) && bytes >= 1)
4117 {
4118 emit_move_insn (adjust_address (orig_dst, QImode, ofs), const0_rtx);
4119 bytes -= 1;
4120 ofs += 1;
4121 alignofs -= 1;
4122 }
4123 if (TARGET_BWX && align >= 16 && (alignofs & 3) == 2 && bytes >= 2)
4124 {
4125 emit_move_insn (adjust_address (orig_dst, HImode, ofs), const0_rtx);
4126 bytes -= 2;
4127 ofs += 2;
4128 alignofs -= 2;
4129 }
4130 if (alignofs == 4 && bytes >= 4)
4131 {
4132 emit_move_insn (adjust_address (orig_dst, SImode, ofs), const0_rtx);
4133 bytes -= 4;
4134 ofs += 4;
4135 alignofs = 0;
4136 }
4137
4138 /* If we've not used the extra lead alignment information by now,
4139 we won't be able to. Downgrade align to match what's left over. */
4140 if (alignofs > 0)
4141 {
4142 alignofs = alignofs & -alignofs;
4143 align = MIN (align, alignofs * BITS_PER_UNIT);
4144 }
4145 }
4146
4147 /* Handle a block of contiguous long-words. */
4148
4149 if (align >= 64 && bytes >= 8)
4150 {
4151 words = bytes / 8;
4152
4153 for (i = 0; i < words; ++i)
4154 emit_move_insn (adjust_address (orig_dst, DImode, ofs + i * 8),
4155 const0_rtx);
4156
4157 bytes -= words * 8;
4158 ofs += words * 8;
4159 }
4160
4161 /* If the block is large and appropriately aligned, emit a single
4162 store followed by a sequence of stq_u insns. */
4163
4164 if (align >= 32 && bytes > 16)
4165 {
4166 rtx orig_dsta;
4167
4168 emit_move_insn (adjust_address (orig_dst, SImode, ofs), const0_rtx);
4169 bytes -= 4;
4170 ofs += 4;
4171
4172 orig_dsta = XEXP (orig_dst, 0);
4173 if (GET_CODE (orig_dsta) == LO_SUM)
4174 orig_dsta = force_reg (Pmode, orig_dsta);
4175
4176 words = bytes / 8;
4177 for (i = 0; i < words; ++i)
4178 {
4179 rtx mem
4180 = change_address (orig_dst, DImode,
4181 gen_rtx_AND (DImode,
4182 plus_constant (DImode, orig_dsta,
4183 ofs + i*8),
4184 GEN_INT (-8)));
4185 set_mem_alias_set (mem, 0);
4186 emit_move_insn (mem, const0_rtx);
4187 }
4188
4189 /* Depending on the alignment, the first stq_u may have overlapped
4190 with the initial stl, which means that the last stq_u didn't
4191 write as much as it would appear. Leave those questionable bytes
4192 unaccounted for. */
4193 bytes -= words * 8 - 4;
4194 ofs += words * 8 - 4;
4195 }
4196
4197 /* Handle a smaller block of aligned words. */
4198
4199 if ((align >= 64 && bytes == 4)
4200 || (align == 32 && bytes >= 4))
4201 {
4202 words = bytes / 4;
4203
4204 for (i = 0; i < words; ++i)
4205 emit_move_insn (adjust_address (orig_dst, SImode, ofs + i * 4),
4206 const0_rtx);
4207
4208 bytes -= words * 4;
4209 ofs += words * 4;
4210 }
4211
4212 /* An unaligned block uses stq_u stores for as many as possible. */
4213
4214 if (bytes >= 8)
4215 {
4216 words = bytes / 8;
4217
4218 alpha_expand_unaligned_store_words (NULL, orig_dst, words, ofs);
4219
4220 bytes -= words * 8;
4221 ofs += words * 8;
4222 }
4223
4224 /* Next clean up any trailing pieces. */
4225
4226 /* Count the number of bits in BYTES for which aligned stores could
4227 be emitted. */
4228 words = 0;
4229 for (i = (TARGET_BWX ? 1 : 4); i * BITS_PER_UNIT <= align ; i <<= 1)
4230 if (bytes & i)
4231 words += 1;
4232
4233 /* If we have appropriate alignment (and it wouldn't take too many
4234 instructions otherwise), mask out the bytes we need. */
4235 if (TARGET_BWX ? words > 2 : bytes > 0)
4236 {
4237 if (align >= 64)
4238 {
4239 rtx mem, tmp;
4240 HOST_WIDE_INT mask;
4241
4242 mem = adjust_address (orig_dst, DImode, ofs);
4243 set_mem_alias_set (mem, 0);
4244
4245 mask = HOST_WIDE_INT_M1U << (bytes * 8);
4246
4247 tmp = expand_binop (DImode, and_optab, mem, GEN_INT (mask),
4248 NULL_RTX, 1, OPTAB_WIDEN);
4249
4250 emit_move_insn (mem, tmp);
4251 return 1;
4252 }
4253 else if (align >= 32 && bytes < 4)
4254 {
4255 rtx mem, tmp;
4256 HOST_WIDE_INT mask;
4257
4258 mem = adjust_address (orig_dst, SImode, ofs);
4259 set_mem_alias_set (mem, 0);
4260
4261 mask = HOST_WIDE_INT_M1U << (bytes * 8);
4262
4263 tmp = expand_binop (SImode, and_optab, mem, GEN_INT (mask),
4264 NULL_RTX, 1, OPTAB_WIDEN);
4265
4266 emit_move_insn (mem, tmp);
4267 return 1;
4268 }
4269 }
4270
4271 if (!TARGET_BWX && bytes >= 4)
4272 {
4273 alpha_expand_unaligned_store (orig_dst, const0_rtx, 4, ofs);
4274 bytes -= 4;
4275 ofs += 4;
4276 }
4277
4278 if (bytes >= 2)
4279 {
4280 if (align >= 16)
4281 {
4282 do {
4283 emit_move_insn (adjust_address (orig_dst, HImode, ofs),
4284 const0_rtx);
4285 bytes -= 2;
4286 ofs += 2;
4287 } while (bytes >= 2);
4288 }
4289 else if (! TARGET_BWX)
4290 {
4291 alpha_expand_unaligned_store (orig_dst, const0_rtx, 2, ofs);
4292 bytes -= 2;
4293 ofs += 2;
4294 }
4295 }
4296
4297 while (bytes > 0)
4298 {
4299 emit_move_insn (adjust_address (orig_dst, QImode, ofs), const0_rtx);
4300 bytes -= 1;
4301 ofs += 1;
4302 }
4303
4304 return 1;
4305 }
4306
4307 /* Returns a mask so that zap(x, value) == x & mask. */
4308
4309 rtx
4310 alpha_expand_zap_mask (HOST_WIDE_INT value)
4311 {
4312 rtx result;
4313 int i;
4314 HOST_WIDE_INT mask = 0;
4315
4316 for (i = 7; i >= 0; --i)
4317 {
4318 mask <<= 8;
4319 if (!((value >> i) & 1))
4320 mask |= 0xff;
4321 }
4322
4323 result = gen_int_mode (mask, DImode);
4324 return result;
4325 }
4326
4327 void
4328 alpha_expand_builtin_vector_binop (rtx (*gen) (rtx, rtx, rtx),
4329 machine_mode mode,
4330 rtx op0, rtx op1, rtx op2)
4331 {
4332 op0 = gen_lowpart (mode, op0);
4333
4334 if (op1 == const0_rtx)
4335 op1 = CONST0_RTX (mode);
4336 else
4337 op1 = gen_lowpart (mode, op1);
4338
4339 if (op2 == const0_rtx)
4340 op2 = CONST0_RTX (mode);
4341 else
4342 op2 = gen_lowpart (mode, op2);
4343
4344 emit_insn ((*gen) (op0, op1, op2));
4345 }
4346
4347 /* A subroutine of the atomic operation splitters. Jump to LABEL if
4348 COND is true. Mark the jump as unlikely to be taken. */
4349
4350 static void
4351 emit_unlikely_jump (rtx cond, rtx label)
4352 {
4353 rtx x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
4354 rtx_insn *insn = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
4355 add_reg_br_prob_note (insn, profile_probability::very_unlikely ());
4356 }
4357
4358 /* Subroutines of the atomic operation splitters. Emit barriers
4359 as needed for the memory MODEL. */
4360
4361 static void
4362 alpha_pre_atomic_barrier (enum memmodel model)
4363 {
4364 if (need_atomic_barrier_p (model, true))
4365 emit_insn (gen_memory_barrier ());
4366 }
4367
4368 static void
4369 alpha_post_atomic_barrier (enum memmodel model)
4370 {
4371 if (need_atomic_barrier_p (model, false))
4372 emit_insn (gen_memory_barrier ());
4373 }
4374
4375 /* A subroutine of the atomic operation splitters. Emit an insxl
4376 instruction in MODE. */
4377
4378 static rtx
4379 emit_insxl (machine_mode mode, rtx op1, rtx op2)
4380 {
4381 rtx ret = gen_reg_rtx (DImode);
4382 rtx (*fn) (rtx, rtx, rtx);
4383
4384 switch (mode)
4385 {
4386 case E_QImode:
4387 fn = gen_insbl;
4388 break;
4389 case E_HImode:
4390 fn = gen_inswl;
4391 break;
4392 case E_SImode:
4393 fn = gen_insll;
4394 break;
4395 case E_DImode:
4396 fn = gen_insql;
4397 break;
4398 default:
4399 gcc_unreachable ();
4400 }
4401
4402 op1 = force_reg (mode, op1);
4403 emit_insn (fn (ret, op1, op2));
4404
4405 return ret;
4406 }
4407
4408 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
4409 to perform. MEM is the memory on which to operate. VAL is the second
4410 operand of the binary operator. BEFORE and AFTER are optional locations to
4411 return the value of MEM either before of after the operation. SCRATCH is
4412 a scratch register. */
4413
4414 void
4415 alpha_split_atomic_op (enum rtx_code code, rtx mem, rtx val, rtx before,
4416 rtx after, rtx scratch, enum memmodel model)
4417 {
4418 machine_mode mode = GET_MODE (mem);
4419 rtx label, x, cond = gen_rtx_REG (DImode, REGNO (scratch));
4420
4421 alpha_pre_atomic_barrier (model);
4422
4423 label = gen_label_rtx ();
4424 emit_label (label);
4425 label = gen_rtx_LABEL_REF (DImode, label);
4426
4427 if (before == NULL)
4428 before = scratch;
4429 emit_insn (gen_load_locked (mode, before, mem));
4430
4431 if (code == NOT)
4432 {
4433 x = gen_rtx_AND (mode, before, val);
4434 emit_insn (gen_rtx_SET (val, x));
4435
4436 x = gen_rtx_NOT (mode, val);
4437 }
4438 else
4439 x = gen_rtx_fmt_ee (code, mode, before, val);
4440 if (after)
4441 emit_insn (gen_rtx_SET (after, copy_rtx (x)));
4442 emit_insn (gen_rtx_SET (scratch, x));
4443
4444 emit_insn (gen_store_conditional (mode, cond, mem, scratch));
4445
4446 x = gen_rtx_EQ (DImode, cond, const0_rtx);
4447 emit_unlikely_jump (x, label);
4448
4449 alpha_post_atomic_barrier (model);
4450 }
4451
4452 /* Expand a compare and swap operation. */
4453
4454 void
4455 alpha_split_compare_and_swap (rtx operands[])
4456 {
4457 rtx cond, retval, mem, oldval, newval;
4458 bool is_weak;
4459 enum memmodel mod_s, mod_f;
4460 machine_mode mode;
4461 rtx label1, label2, x;
4462
4463 cond = operands[0];
4464 retval = operands[1];
4465 mem = operands[2];
4466 oldval = operands[3];
4467 newval = operands[4];
4468 is_weak = (operands[5] != const0_rtx);
4469 mod_s = memmodel_from_int (INTVAL (operands[6]));
4470 mod_f = memmodel_from_int (INTVAL (operands[7]));
4471 mode = GET_MODE (mem);
4472
4473 alpha_pre_atomic_barrier (mod_s);
4474
4475 label1 = NULL_RTX;
4476 if (!is_weak)
4477 {
4478 label1 = gen_rtx_LABEL_REF (DImode, gen_label_rtx ());
4479 emit_label (XEXP (label1, 0));
4480 }
4481 label2 = gen_rtx_LABEL_REF (DImode, gen_label_rtx ());
4482
4483 emit_insn (gen_load_locked (mode, retval, mem));
4484
4485 x = gen_lowpart (DImode, retval);
4486 if (oldval == const0_rtx)
4487 {
4488 emit_move_insn (cond, const0_rtx);
4489 x = gen_rtx_NE (DImode, x, const0_rtx);
4490 }
4491 else
4492 {
4493 x = gen_rtx_EQ (DImode, x, oldval);
4494 emit_insn (gen_rtx_SET (cond, x));
4495 x = gen_rtx_EQ (DImode, cond, const0_rtx);
4496 }
4497 emit_unlikely_jump (x, label2);
4498
4499 emit_move_insn (cond, newval);
4500 emit_insn (gen_store_conditional
4501 (mode, cond, mem, gen_lowpart (mode, cond)));
4502
4503 if (!is_weak)
4504 {
4505 x = gen_rtx_EQ (DImode, cond, const0_rtx);
4506 emit_unlikely_jump (x, label1);
4507 }
4508
4509 if (!is_mm_relaxed (mod_f))
4510 emit_label (XEXP (label2, 0));
4511
4512 alpha_post_atomic_barrier (mod_s);
4513
4514 if (is_mm_relaxed (mod_f))
4515 emit_label (XEXP (label2, 0));
4516 }
4517
4518 void
4519 alpha_expand_compare_and_swap_12 (rtx operands[])
4520 {
4521 rtx cond, dst, mem, oldval, newval, is_weak, mod_s, mod_f;
4522 machine_mode mode;
4523 rtx addr, align, wdst;
4524
4525 cond = operands[0];
4526 dst = operands[1];
4527 mem = operands[2];
4528 oldval = operands[3];
4529 newval = operands[4];
4530 is_weak = operands[5];
4531 mod_s = operands[6];
4532 mod_f = operands[7];
4533 mode = GET_MODE (mem);
4534
4535 /* We forced the address into a register via mem_noofs_operand. */
4536 addr = XEXP (mem, 0);
4537 gcc_assert (register_operand (addr, DImode));
4538
4539 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-8),
4540 NULL_RTX, 1, OPTAB_DIRECT);
4541
4542 oldval = convert_modes (DImode, mode, oldval, 1);
4543
4544 if (newval != const0_rtx)
4545 newval = emit_insxl (mode, newval, addr);
4546
4547 wdst = gen_reg_rtx (DImode);
4548 emit_insn (gen_atomic_compare_and_swap_1
4549 (mode, cond, wdst, mem, oldval, newval, align,
4550 is_weak, mod_s, mod_f));
4551
4552 emit_move_insn (dst, gen_lowpart (mode, wdst));
4553 }
4554
4555 void
4556 alpha_split_compare_and_swap_12 (rtx operands[])
4557 {
4558 rtx cond, dest, orig_mem, oldval, newval, align, scratch;
4559 machine_mode mode;
4560 bool is_weak;
4561 enum memmodel mod_s, mod_f;
4562 rtx label1, label2, mem, addr, width, mask, x;
4563
4564 cond = operands[0];
4565 dest = operands[1];
4566 orig_mem = operands[2];
4567 oldval = operands[3];
4568 newval = operands[4];
4569 align = operands[5];
4570 is_weak = (operands[6] != const0_rtx);
4571 mod_s = memmodel_from_int (INTVAL (operands[7]));
4572 mod_f = memmodel_from_int (INTVAL (operands[8]));
4573 scratch = operands[9];
4574 mode = GET_MODE (orig_mem);
4575 addr = XEXP (orig_mem, 0);
4576
4577 mem = gen_rtx_MEM (DImode, align);
4578 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
4579 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
4580 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
4581
4582 alpha_pre_atomic_barrier (mod_s);
4583
4584 label1 = NULL_RTX;
4585 if (!is_weak)
4586 {
4587 label1 = gen_rtx_LABEL_REF (DImode, gen_label_rtx ());
4588 emit_label (XEXP (label1, 0));
4589 }
4590 label2 = gen_rtx_LABEL_REF (DImode, gen_label_rtx ());
4591
4592 emit_insn (gen_load_locked (DImode, scratch, mem));
4593
4594 width = GEN_INT (GET_MODE_BITSIZE (mode));
4595 mask = GEN_INT (mode == QImode ? 0xff : 0xffff);
4596 emit_insn (gen_extxl (dest, scratch, width, addr));
4597
4598 if (oldval == const0_rtx)
4599 {
4600 emit_move_insn (cond, const0_rtx);
4601 x = gen_rtx_NE (DImode, dest, const0_rtx);
4602 }
4603 else
4604 {
4605 x = gen_rtx_EQ (DImode, dest, oldval);
4606 emit_insn (gen_rtx_SET (cond, x));
4607 x = gen_rtx_EQ (DImode, cond, const0_rtx);
4608 }
4609 emit_unlikely_jump (x, label2);
4610
4611 emit_insn (gen_mskxl (cond, scratch, mask, addr));
4612
4613 if (newval != const0_rtx)
4614 emit_insn (gen_iordi3 (cond, cond, newval));
4615
4616 emit_insn (gen_store_conditional (DImode, cond, mem, cond));
4617
4618 if (!is_weak)
4619 {
4620 x = gen_rtx_EQ (DImode, cond, const0_rtx);
4621 emit_unlikely_jump (x, label1);
4622 }
4623
4624 if (!is_mm_relaxed (mod_f))
4625 emit_label (XEXP (label2, 0));
4626
4627 alpha_post_atomic_barrier (mod_s);
4628
4629 if (is_mm_relaxed (mod_f))
4630 emit_label (XEXP (label2, 0));
4631 }
4632
4633 /* Expand an atomic exchange operation. */
4634
4635 void
4636 alpha_split_atomic_exchange (rtx operands[])
4637 {
4638 rtx retval, mem, val, scratch;
4639 enum memmodel model;
4640 machine_mode mode;
4641 rtx label, x, cond;
4642
4643 retval = operands[0];
4644 mem = operands[1];
4645 val = operands[2];
4646 model = (enum memmodel) INTVAL (operands[3]);
4647 scratch = operands[4];
4648 mode = GET_MODE (mem);
4649 cond = gen_lowpart (DImode, scratch);
4650
4651 alpha_pre_atomic_barrier (model);
4652
4653 label = gen_rtx_LABEL_REF (DImode, gen_label_rtx ());
4654 emit_label (XEXP (label, 0));
4655
4656 emit_insn (gen_load_locked (mode, retval, mem));
4657 emit_move_insn (scratch, val);
4658 emit_insn (gen_store_conditional (mode, cond, mem, scratch));
4659
4660 x = gen_rtx_EQ (DImode, cond, const0_rtx);
4661 emit_unlikely_jump (x, label);
4662
4663 alpha_post_atomic_barrier (model);
4664 }
4665
4666 void
4667 alpha_expand_atomic_exchange_12 (rtx operands[])
4668 {
4669 rtx dst, mem, val, model;
4670 machine_mode mode;
4671 rtx addr, align, wdst;
4672
4673 dst = operands[0];
4674 mem = operands[1];
4675 val = operands[2];
4676 model = operands[3];
4677 mode = GET_MODE (mem);
4678
4679 /* We forced the address into a register via mem_noofs_operand. */
4680 addr = XEXP (mem, 0);
4681 gcc_assert (register_operand (addr, DImode));
4682
4683 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-8),
4684 NULL_RTX, 1, OPTAB_DIRECT);
4685
4686 /* Insert val into the correct byte location within the word. */
4687 if (val != const0_rtx)
4688 val = emit_insxl (mode, val, addr);
4689
4690 wdst = gen_reg_rtx (DImode);
4691 emit_insn (gen_atomic_exchange_1 (mode, wdst, mem, val, align, model));
4692
4693 emit_move_insn (dst, gen_lowpart (mode, wdst));
4694 }
4695
4696 void
4697 alpha_split_atomic_exchange_12 (rtx operands[])
4698 {
4699 rtx dest, orig_mem, addr, val, align, scratch;
4700 rtx label, mem, width, mask, x;
4701 machine_mode mode;
4702 enum memmodel model;
4703
4704 dest = operands[0];
4705 orig_mem = operands[1];
4706 val = operands[2];
4707 align = operands[3];
4708 model = (enum memmodel) INTVAL (operands[4]);
4709 scratch = operands[5];
4710 mode = GET_MODE (orig_mem);
4711 addr = XEXP (orig_mem, 0);
4712
4713 mem = gen_rtx_MEM (DImode, align);
4714 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
4715 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
4716 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
4717
4718 alpha_pre_atomic_barrier (model);
4719
4720 label = gen_rtx_LABEL_REF (DImode, gen_label_rtx ());
4721 emit_label (XEXP (label, 0));
4722
4723 emit_insn (gen_load_locked (DImode, scratch, mem));
4724
4725 width = GEN_INT (GET_MODE_BITSIZE (mode));
4726 mask = GEN_INT (mode == QImode ? 0xff : 0xffff);
4727 emit_insn (gen_extxl (dest, scratch, width, addr));
4728 emit_insn (gen_mskxl (scratch, scratch, mask, addr));
4729 if (val != const0_rtx)
4730 emit_insn (gen_iordi3 (scratch, scratch, val));
4731
4732 emit_insn (gen_store_conditional (DImode, scratch, mem, scratch));
4733
4734 x = gen_rtx_EQ (DImode, scratch, const0_rtx);
4735 emit_unlikely_jump (x, label);
4736
4737 alpha_post_atomic_barrier (model);
4738 }
4739
4740 /* Adjust the cost of a scheduling dependency. Return the new cost of
4742 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
4743
4744 static int
4745 alpha_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
4746 unsigned int)
4747 {
4748 enum attr_type dep_insn_type;
4749
4750 /* If the dependence is an anti-dependence, there is no cost. For an
4751 output dependence, there is sometimes a cost, but it doesn't seem
4752 worth handling those few cases. */
4753 if (dep_type != 0)
4754 return cost;
4755
4756 /* If we can't recognize the insns, we can't really do anything. */
4757 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
4758 return cost;
4759
4760 dep_insn_type = get_attr_type (dep_insn);
4761
4762 /* Bring in the user-defined memory latency. */
4763 if (dep_insn_type == TYPE_ILD
4764 || dep_insn_type == TYPE_FLD
4765 || dep_insn_type == TYPE_LDSYM)
4766 cost += alpha_memory_latency-1;
4767
4768 /* Everything else handled in DFA bypasses now. */
4769
4770 return cost;
4771 }
4772
4773 /* The number of instructions that can be issued per cycle. */
4774
4775 static int
4776 alpha_issue_rate (void)
4777 {
4778 return (alpha_tune == PROCESSOR_EV4 ? 2 : 4);
4779 }
4780
4781 /* How many alternative schedules to try. This should be as wide as the
4782 scheduling freedom in the DFA, but no wider. Making this value too
4783 large results extra work for the scheduler.
4784
4785 For EV4, loads can be issued to either IB0 or IB1, thus we have 2
4786 alternative schedules. For EV5, we can choose between E0/E1 and
4787 FA/FM. For EV6, an arithmetic insn can be issued to U0/U1/L0/L1. */
4788
4789 static int
4790 alpha_multipass_dfa_lookahead (void)
4791 {
4792 return (alpha_tune == PROCESSOR_EV6 ? 4 : 2);
4793 }
4794
4795 /* Machine-specific function data. */
4797
4798 struct GTY(()) alpha_links;
4799
4800 struct GTY(()) machine_function
4801 {
4802 unsigned HOST_WIDE_INT sa_mask;
4803 HOST_WIDE_INT sa_size;
4804 HOST_WIDE_INT frame_size;
4805
4806 /* For flag_reorder_blocks_and_partition. */
4807 rtx gp_save_rtx;
4808
4809 /* For VMS condition handlers. */
4810 bool uses_condition_handler;
4811
4812 /* Linkage entries. */
4813 hash_map<nofree_string_hash, alpha_links *> *links;
4814 };
4815
4816 /* How to allocate a 'struct machine_function'. */
4817
4818 static struct machine_function *
4819 alpha_init_machine_status (void)
4820 {
4821 return ggc_cleared_alloc<machine_function> ();
4822 }
4823
4824 /* Support for frame based VMS condition handlers. */
4825
4826 /* A VMS condition handler may be established for a function with a call to
4827 __builtin_establish_vms_condition_handler, and cancelled with a call to
4828 __builtin_revert_vms_condition_handler.
4829
4830 The VMS Condition Handling Facility knows about the existence of a handler
4831 from the procedure descriptor .handler field. As the VMS native compilers,
4832 we store the user specified handler's address at a fixed location in the
4833 stack frame and point the procedure descriptor at a common wrapper which
4834 fetches the real handler's address and issues an indirect call.
4835
4836 The indirection wrapper is "__gcc_shell_handler", provided by libgcc.
4837
4838 We force the procedure kind to PT_STACK, and the fixed frame location is
4839 fp+8, just before the register save area. We use the handler_data field in
4840 the procedure descriptor to state the fp offset at which the installed
4841 handler address can be found. */
4842
4843 #define VMS_COND_HANDLER_FP_OFFSET 8
4844
4845 /* Expand code to store the currently installed user VMS condition handler
4846 into TARGET and install HANDLER as the new condition handler. */
4847
4848 void
4849 alpha_expand_builtin_establish_vms_condition_handler (rtx target, rtx handler)
4850 {
4851 rtx handler_slot_address = plus_constant (Pmode, hard_frame_pointer_rtx,
4852 VMS_COND_HANDLER_FP_OFFSET);
4853
4854 rtx handler_slot
4855 = gen_rtx_MEM (DImode, handler_slot_address);
4856
4857 emit_move_insn (target, handler_slot);
4858 emit_move_insn (handler_slot, handler);
4859
4860 /* Notify the start/prologue/epilogue emitters that the condition handler
4861 slot is needed. In addition to reserving the slot space, this will force
4862 the procedure kind to PT_STACK so ensure that the hard_frame_pointer_rtx
4863 use above is correct. */
4864 cfun->machine->uses_condition_handler = true;
4865 }
4866
4867 /* Expand code to store the current VMS condition handler into TARGET and
4868 nullify it. */
4869
4870 void
4871 alpha_expand_builtin_revert_vms_condition_handler (rtx target)
4872 {
4873 /* We implement this by establishing a null condition handler, with the tiny
4874 side effect of setting uses_condition_handler. This is a little bit
4875 pessimistic if no actual builtin_establish call is ever issued, which is
4876 not a real problem and expected never to happen anyway. */
4877
4878 alpha_expand_builtin_establish_vms_condition_handler (target, const0_rtx);
4879 }
4880
4881 /* Functions to save and restore alpha_return_addr_rtx. */
4882
4883 /* Start the ball rolling with RETURN_ADDR_RTX. */
4884
4885 rtx
4886 alpha_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
4887 {
4888 if (count != 0)
4889 return const0_rtx;
4890
4891 return get_hard_reg_initial_val (Pmode, REG_RA);
4892 }
4893
4894 /* Return or create a memory slot containing the gp value for the current
4895 function. Needed only if TARGET_LD_BUGGY_LDGP. */
4896
4897 rtx
4898 alpha_gp_save_rtx (void)
4899 {
4900 rtx_insn *seq;
4901 rtx m = cfun->machine->gp_save_rtx;
4902
4903 if (m == NULL)
4904 {
4905 start_sequence ();
4906
4907 m = assign_stack_local (DImode, UNITS_PER_WORD, BITS_PER_WORD);
4908 m = validize_mem (m);
4909 emit_move_insn (m, pic_offset_table_rtx);
4910
4911 seq = get_insns ();
4912 end_sequence ();
4913
4914 /* We used to simply emit the sequence after entry_of_function.
4915 However this breaks the CFG if the first instruction in the
4916 first block is not the NOTE_INSN_BASIC_BLOCK, for example a
4917 label. Emit the sequence properly on the edge. We are only
4918 invoked from dw2_build_landing_pads and finish_eh_generation
4919 will call commit_edge_insertions thanks to a kludge. */
4920 insert_insn_on_edge (seq,
4921 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4922
4923 cfun->machine->gp_save_rtx = m;
4924 }
4925
4926 return m;
4927 }
4928
4929 static void
4930 alpha_instantiate_decls (void)
4931 {
4932 if (cfun->machine->gp_save_rtx != NULL_RTX)
4933 instantiate_decl_rtl (cfun->machine->gp_save_rtx);
4934 }
4935
4936 static int
4937 alpha_ra_ever_killed (void)
4938 {
4939 rtx_insn *top;
4940
4941 if (!has_hard_reg_initial_val (Pmode, REG_RA))
4942 return (int)df_regs_ever_live_p (REG_RA);
4943
4944 push_topmost_sequence ();
4945 top = get_insns ();
4946 pop_topmost_sequence ();
4947
4948 return reg_set_between_p (gen_rtx_REG (Pmode, REG_RA), top, NULL);
4949 }
4950
4951
4952 /* Return the trap mode suffix applicable to the current
4954 instruction, or NULL. */
4955
4956 static const char *
4957 get_trap_mode_suffix (void)
4958 {
4959 enum attr_trap_suffix s = get_attr_trap_suffix (current_output_insn);
4960
4961 switch (s)
4962 {
4963 case TRAP_SUFFIX_NONE:
4964 return NULL;
4965
4966 case TRAP_SUFFIX_SU:
4967 if (alpha_fptm >= ALPHA_FPTM_SU)
4968 return "su";
4969 return NULL;
4970
4971 case TRAP_SUFFIX_SUI:
4972 if (alpha_fptm >= ALPHA_FPTM_SUI)
4973 return "sui";
4974 return NULL;
4975
4976 case TRAP_SUFFIX_V_SV:
4977 switch (alpha_fptm)
4978 {
4979 case ALPHA_FPTM_N:
4980 return NULL;
4981 case ALPHA_FPTM_U:
4982 return "v";
4983 case ALPHA_FPTM_SU:
4984 case ALPHA_FPTM_SUI:
4985 return "sv";
4986 default:
4987 gcc_unreachable ();
4988 }
4989
4990 case TRAP_SUFFIX_V_SV_SVI:
4991 switch (alpha_fptm)
4992 {
4993 case ALPHA_FPTM_N:
4994 return NULL;
4995 case ALPHA_FPTM_U:
4996 return "v";
4997 case ALPHA_FPTM_SU:
4998 return "sv";
4999 case ALPHA_FPTM_SUI:
5000 return "svi";
5001 default:
5002 gcc_unreachable ();
5003 }
5004 break;
5005
5006 case TRAP_SUFFIX_U_SU_SUI:
5007 switch (alpha_fptm)
5008 {
5009 case ALPHA_FPTM_N:
5010 return NULL;
5011 case ALPHA_FPTM_U:
5012 return "u";
5013 case ALPHA_FPTM_SU:
5014 return "su";
5015 case ALPHA_FPTM_SUI:
5016 return "sui";
5017 default:
5018 gcc_unreachable ();
5019 }
5020 break;
5021
5022 default:
5023 gcc_unreachable ();
5024 }
5025 gcc_unreachable ();
5026 }
5027
5028 /* Return the rounding mode suffix applicable to the current
5029 instruction, or NULL. */
5030
5031 static const char *
5032 get_round_mode_suffix (void)
5033 {
5034 enum attr_round_suffix s = get_attr_round_suffix (current_output_insn);
5035
5036 switch (s)
5037 {
5038 case ROUND_SUFFIX_NONE:
5039 return NULL;
5040 case ROUND_SUFFIX_NORMAL:
5041 switch (alpha_fprm)
5042 {
5043 case ALPHA_FPRM_NORM:
5044 return NULL;
5045 case ALPHA_FPRM_MINF:
5046 return "m";
5047 case ALPHA_FPRM_CHOP:
5048 return "c";
5049 case ALPHA_FPRM_DYN:
5050 return "d";
5051 default:
5052 gcc_unreachable ();
5053 }
5054 break;
5055
5056 case ROUND_SUFFIX_C:
5057 return "c";
5058
5059 default:
5060 gcc_unreachable ();
5061 }
5062 gcc_unreachable ();
5063 }
5064
5065 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
5066
5067 static bool
5068 alpha_print_operand_punct_valid_p (unsigned char code)
5069 {
5070 return (code == '/' || code == ',' || code == '-' || code == '~'
5071 || code == '#' || code == '*' || code == '&');
5072 }
5073
5074 /* Implement TARGET_PRINT_OPERAND. The alpha-specific
5075 operand codes are documented below. */
5076
5077 static void
5078 alpha_print_operand (FILE *file, rtx x, int code)
5079 {
5080 int i;
5081
5082 switch (code)
5083 {
5084 case '~':
5085 /* Print the assembler name of the current function. */
5086 assemble_name (file, alpha_fnname);
5087 break;
5088
5089 case '&':
5090 if (const char *name = get_some_local_dynamic_name ())
5091 assemble_name (file, name);
5092 else
5093 output_operand_lossage ("'%%&' used without any "
5094 "local dynamic TLS references");
5095 break;
5096
5097 case '/':
5098 /* Generates the instruction suffix. The TRAP_SUFFIX and ROUND_SUFFIX
5099 attributes are examined to determine what is appropriate. */
5100 {
5101 const char *trap = get_trap_mode_suffix ();
5102 const char *round = get_round_mode_suffix ();
5103
5104 if (trap || round)
5105 fprintf (file, "/%s%s", (trap ? trap : ""), (round ? round : ""));
5106 break;
5107 }
5108
5109 case ',':
5110 /* Generates single precision suffix for floating point
5111 instructions (s for IEEE, f for VAX). */
5112 fputc ((TARGET_FLOAT_VAX ? 'f' : 's'), file);
5113 break;
5114
5115 case '-':
5116 /* Generates double precision suffix for floating point
5117 instructions (t for IEEE, g for VAX). */
5118 fputc ((TARGET_FLOAT_VAX ? 'g' : 't'), file);
5119 break;
5120
5121 case '#':
5122 if (alpha_this_literal_sequence_number == 0)
5123 alpha_this_literal_sequence_number = alpha_next_sequence_number++;
5124 fprintf (file, "%d", alpha_this_literal_sequence_number);
5125 break;
5126
5127 case '*':
5128 if (alpha_this_gpdisp_sequence_number == 0)
5129 alpha_this_gpdisp_sequence_number = alpha_next_sequence_number++;
5130 fprintf (file, "%d", alpha_this_gpdisp_sequence_number);
5131 break;
5132
5133 case 'J':
5134 {
5135 const char *lituse;
5136
5137 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD_CALL)
5138 {
5139 x = XVECEXP (x, 0, 0);
5140 lituse = "lituse_tlsgd";
5141 }
5142 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM_CALL)
5143 {
5144 x = XVECEXP (x, 0, 0);
5145 lituse = "lituse_tlsldm";
5146 }
5147 else if (CONST_INT_P (x))
5148 lituse = "lituse_jsr";
5149 else
5150 {
5151 output_operand_lossage ("invalid %%J value");
5152 break;
5153 }
5154
5155 if (x != const0_rtx)
5156 fprintf (file, "\t\t!%s!%d", lituse, (int) INTVAL (x));
5157 }
5158 break;
5159
5160 case 'j':
5161 {
5162 const char *lituse;
5163
5164 #ifdef HAVE_AS_JSRDIRECT_RELOCS
5165 lituse = "lituse_jsrdirect";
5166 #else
5167 lituse = "lituse_jsr";
5168 #endif
5169
5170 gcc_assert (INTVAL (x) != 0);
5171 fprintf (file, "\t\t!%s!%d", lituse, (int) INTVAL (x));
5172 }
5173 break;
5174 case 'r':
5175 /* If this operand is the constant zero, write it as "$31". */
5176 if (REG_P (x))
5177 fprintf (file, "%s", reg_names[REGNO (x)]);
5178 else if (x == CONST0_RTX (GET_MODE (x)))
5179 fprintf (file, "$31");
5180 else
5181 output_operand_lossage ("invalid %%r value");
5182 break;
5183
5184 case 'R':
5185 /* Similar, but for floating-point. */
5186 if (REG_P (x))
5187 fprintf (file, "%s", reg_names[REGNO (x)]);
5188 else if (x == CONST0_RTX (GET_MODE (x)))
5189 fprintf (file, "$f31");
5190 else
5191 output_operand_lossage ("invalid %%R value");
5192 break;
5193
5194 case 'N':
5195 /* Write the 1's complement of a constant. */
5196 if (!CONST_INT_P (x))
5197 output_operand_lossage ("invalid %%N value");
5198
5199 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
5200 break;
5201
5202 case 'P':
5203 /* Write 1 << C, for a constant C. */
5204 if (!CONST_INT_P (x))
5205 output_operand_lossage ("invalid %%P value");
5206
5207 fprintf (file, HOST_WIDE_INT_PRINT_DEC, HOST_WIDE_INT_1 << INTVAL (x));
5208 break;
5209
5210 case 'h':
5211 /* Write the high-order 16 bits of a constant, sign-extended. */
5212 if (!CONST_INT_P (x))
5213 output_operand_lossage ("invalid %%h value");
5214
5215 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) >> 16);
5216 break;
5217
5218 case 'L':
5219 /* Write the low-order 16 bits of a constant, sign-extended. */
5220 if (!CONST_INT_P (x))
5221 output_operand_lossage ("invalid %%L value");
5222
5223 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
5224 (INTVAL (x) & 0xffff) - 2 * (INTVAL (x) & 0x8000));
5225 break;
5226
5227 case 'm':
5228 /* Write mask for ZAP insn. */
5229 if (CONST_INT_P (x))
5230 {
5231 HOST_WIDE_INT mask = 0, value = INTVAL (x);
5232
5233 for (i = 0; i < 8; i++, value >>= 8)
5234 if (value & 0xff)
5235 mask |= (1 << i);
5236
5237 fprintf (file, HOST_WIDE_INT_PRINT_DEC, mask);
5238 }
5239 else
5240 output_operand_lossage ("invalid %%m value");
5241 break;
5242
5243 case 'M':
5244 /* 'b', 'w', 'l', or 'q' as the value of the constant. */
5245 if (!mode_width_operand (x, VOIDmode))
5246 output_operand_lossage ("invalid %%M value");
5247
5248 fprintf (file, "%s",
5249 (INTVAL (x) == 8 ? "b"
5250 : INTVAL (x) == 16 ? "w"
5251 : INTVAL (x) == 32 ? "l"
5252 : "q"));
5253 break;
5254
5255 case 'U':
5256 /* Similar, except do it from the mask. */
5257 if (CONST_INT_P (x))
5258 {
5259 HOST_WIDE_INT value = INTVAL (x);
5260
5261 if (value == 0xff)
5262 {
5263 fputc ('b', file);
5264 break;
5265 }
5266 if (value == 0xffff)
5267 {
5268 fputc ('w', file);
5269 break;
5270 }
5271 if (value == 0xffffffff)
5272 {
5273 fputc ('l', file);
5274 break;
5275 }
5276 if (value == -1)
5277 {
5278 fputc ('q', file);
5279 break;
5280 }
5281 }
5282
5283 output_operand_lossage ("invalid %%U value");
5284 break;
5285
5286 case 's':
5287 /* Write the constant value divided by 8. */
5288 if (!CONST_INT_P (x)
5289 || (unsigned HOST_WIDE_INT) INTVAL (x) >= 64
5290 || (INTVAL (x) & 7) != 0)
5291 output_operand_lossage ("invalid %%s value");
5292
5293 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) / 8);
5294 break;
5295
5296 case 'C': case 'D': case 'c': case 'd':
5297 /* Write out comparison name. */
5298 {
5299 enum rtx_code c = GET_CODE (x);
5300
5301 if (!COMPARISON_P (x))
5302 output_operand_lossage ("invalid %%C value");
5303
5304 else if (code == 'D')
5305 c = reverse_condition (c);
5306 else if (code == 'c')
5307 c = swap_condition (c);
5308 else if (code == 'd')
5309 c = swap_condition (reverse_condition (c));
5310
5311 if (c == LEU)
5312 fprintf (file, "ule");
5313 else if (c == LTU)
5314 fprintf (file, "ult");
5315 else if (c == UNORDERED)
5316 fprintf (file, "un");
5317 else
5318 fprintf (file, "%s", GET_RTX_NAME (c));
5319 }
5320 break;
5321
5322 case 'E':
5323 /* Write the divide or modulus operator. */
5324 switch (GET_CODE (x))
5325 {
5326 case DIV:
5327 fprintf (file, "div%s", GET_MODE (x) == SImode ? "l" : "q");
5328 break;
5329 case UDIV:
5330 fprintf (file, "div%su", GET_MODE (x) == SImode ? "l" : "q");
5331 break;
5332 case MOD:
5333 fprintf (file, "rem%s", GET_MODE (x) == SImode ? "l" : "q");
5334 break;
5335 case UMOD:
5336 fprintf (file, "rem%su", GET_MODE (x) == SImode ? "l" : "q");
5337 break;
5338 default:
5339 output_operand_lossage ("invalid %%E value");
5340 break;
5341 }
5342 break;
5343
5344 case 'A':
5345 /* Write "_u" for unaligned access. */
5346 if (MEM_P (x) && GET_CODE (XEXP (x, 0)) == AND)
5347 fprintf (file, "_u");
5348 break;
5349
5350 case 0:
5351 if (REG_P (x))
5352 fprintf (file, "%s", reg_names[REGNO (x)]);
5353 else if (MEM_P (x))
5354 output_address (GET_MODE (x), XEXP (x, 0));
5355 else if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == UNSPEC)
5356 {
5357 switch (XINT (XEXP (x, 0), 1))
5358 {
5359 case UNSPEC_DTPREL:
5360 case UNSPEC_TPREL:
5361 output_addr_const (file, XVECEXP (XEXP (x, 0), 0, 0));
5362 break;
5363 default:
5364 output_operand_lossage ("unknown relocation unspec");
5365 break;
5366 }
5367 }
5368 else
5369 output_addr_const (file, x);
5370 break;
5371
5372 default:
5373 output_operand_lossage ("invalid %%xn code");
5374 }
5375 }
5376
5377 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
5378
5379 static void
5380 alpha_print_operand_address (FILE *file, machine_mode /*mode*/, rtx addr)
5381 {
5382 int basereg = 31;
5383 HOST_WIDE_INT offset = 0;
5384
5385 if (GET_CODE (addr) == AND)
5386 addr = XEXP (addr, 0);
5387
5388 if (GET_CODE (addr) == PLUS
5389 && CONST_INT_P (XEXP (addr, 1)))
5390 {
5391 offset = INTVAL (XEXP (addr, 1));
5392 addr = XEXP (addr, 0);
5393 }
5394
5395 if (GET_CODE (addr) == LO_SUM)
5396 {
5397 const char *reloc16, *reloclo;
5398 rtx op1 = XEXP (addr, 1);
5399
5400 if (GET_CODE (op1) == CONST && GET_CODE (XEXP (op1, 0)) == UNSPEC)
5401 {
5402 op1 = XEXP (op1, 0);
5403 switch (XINT (op1, 1))
5404 {
5405 case UNSPEC_DTPREL:
5406 reloc16 = NULL;
5407 reloclo = (alpha_tls_size == 16 ? "dtprel" : "dtprello");
5408 break;
5409 case UNSPEC_TPREL:
5410 reloc16 = NULL;
5411 reloclo = (alpha_tls_size == 16 ? "tprel" : "tprello");
5412 break;
5413 default:
5414 output_operand_lossage ("unknown relocation unspec");
5415 return;
5416 }
5417
5418 output_addr_const (file, XVECEXP (op1, 0, 0));
5419 }
5420 else
5421 {
5422 reloc16 = "gprel";
5423 reloclo = "gprellow";
5424 output_addr_const (file, op1);
5425 }
5426
5427 if (offset)
5428 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
5429
5430 addr = XEXP (addr, 0);
5431 switch (GET_CODE (addr))
5432 {
5433 case REG:
5434 basereg = REGNO (addr);
5435 break;
5436
5437 case SUBREG:
5438 basereg = subreg_regno (addr);
5439 break;
5440
5441 default:
5442 gcc_unreachable ();
5443 }
5444
5445 fprintf (file, "($%d)\t\t!%s", basereg,
5446 (basereg == 29 ? reloc16 : reloclo));
5447 return;
5448 }
5449
5450 switch (GET_CODE (addr))
5451 {
5452 case REG:
5453 basereg = REGNO (addr);
5454 break;
5455
5456 case SUBREG:
5457 basereg = subreg_regno (addr);
5458 break;
5459
5460 case CONST_INT:
5461 offset = INTVAL (addr);
5462 break;
5463
5464 case SYMBOL_REF:
5465 gcc_assert(TARGET_ABI_OPEN_VMS || this_is_asm_operands);
5466 fprintf (file, "%s", XSTR (addr, 0));
5467 return;
5468
5469 case CONST:
5470 gcc_assert(TARGET_ABI_OPEN_VMS || this_is_asm_operands);
5471 gcc_assert (GET_CODE (XEXP (addr, 0)) == PLUS
5472 && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF);
5473 fprintf (file, "%s+" HOST_WIDE_INT_PRINT_DEC,
5474 XSTR (XEXP (XEXP (addr, 0), 0), 0),
5475 INTVAL (XEXP (XEXP (addr, 0), 1)));
5476 return;
5477
5478 default:
5479 output_operand_lossage ("invalid operand address");
5480 return;
5481 }
5482
5483 fprintf (file, HOST_WIDE_INT_PRINT_DEC "($%d)", offset, basereg);
5484 }
5485
5486 /* Emit RTL insns to initialize the variable parts of a trampoline at
5488 M_TRAMP. FNDECL is target function's decl. CHAIN_VALUE is an rtx
5489 for the static chain value for the function. */
5490
5491 static void
5492 alpha_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
5493 {
5494 rtx fnaddr, mem, word1, word2;
5495
5496 fnaddr = XEXP (DECL_RTL (fndecl), 0);
5497
5498 #ifdef POINTERS_EXTEND_UNSIGNED
5499 fnaddr = convert_memory_address (Pmode, fnaddr);
5500 chain_value = convert_memory_address (Pmode, chain_value);
5501 #endif
5502
5503 if (TARGET_ABI_OPEN_VMS)
5504 {
5505 const char *fnname;
5506 char *trname;
5507
5508 /* Construct the name of the trampoline entry point. */
5509 fnname = XSTR (fnaddr, 0);
5510 trname = (char *) alloca (strlen (fnname) + 5);
5511 strcpy (trname, fnname);
5512 strcat (trname, "..tr");
5513 fnname = ggc_alloc_string (trname, strlen (trname) + 1);
5514 word2 = gen_rtx_SYMBOL_REF (Pmode, fnname);
5515
5516 /* Trampoline (or "bounded") procedure descriptor is constructed from
5517 the function's procedure descriptor with certain fields zeroed IAW
5518 the VMS calling standard. This is stored in the first quadword. */
5519 word1 = force_reg (DImode, gen_const_mem (DImode, fnaddr));
5520 word1 = expand_and (DImode, word1,
5521 GEN_INT (HOST_WIDE_INT_C (0xffff0fff0000fff0)),
5522 NULL);
5523 }
5524 else
5525 {
5526 /* These 4 instructions are:
5527 ldq $1,24($27)
5528 ldq $27,16($27)
5529 jmp $31,($27),0
5530 nop
5531 We don't bother setting the HINT field of the jump; the nop
5532 is merely there for padding. */
5533 word1 = GEN_INT (HOST_WIDE_INT_C (0xa77b0010a43b0018));
5534 word2 = GEN_INT (HOST_WIDE_INT_C (0x47ff041f6bfb0000));
5535 }
5536
5537 /* Store the first two words, as computed above. */
5538 mem = adjust_address (m_tramp, DImode, 0);
5539 emit_move_insn (mem, word1);
5540 mem = adjust_address (m_tramp, DImode, 8);
5541 emit_move_insn (mem, word2);
5542
5543 /* Store function address and static chain value. */
5544 mem = adjust_address (m_tramp, Pmode, 16);
5545 emit_move_insn (mem, fnaddr);
5546 mem = adjust_address (m_tramp, Pmode, 24);
5547 emit_move_insn (mem, chain_value);
5548
5549 if (TARGET_ABI_OSF)
5550 {
5551 emit_insn (gen_imb ());
5552 #ifdef HAVE_ENABLE_EXECUTE_STACK
5553 emit_library_call (init_one_libfunc ("__enable_execute_stack"),
5554 LCT_NORMAL, VOIDmode, XEXP (m_tramp, 0), Pmode);
5555 #endif
5556 }
5557 }
5558
5559 /* Determine where to put an argument to a function.
5561 Value is zero to push the argument on the stack,
5562 or a hard register in which to store the argument.
5563
5564 CUM is a variable of type CUMULATIVE_ARGS which gives info about
5565 the preceding args and about the function being called.
5566 ARG is a description of the argument.
5567
5568 On Alpha the first 6 words of args are normally in registers
5569 and the rest are pushed. */
5570
5571 static rtx
5572 alpha_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
5573 {
5574 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5575 int basereg;
5576 int num_args;
5577
5578 /* Don't get confused and pass small structures in FP registers. */
5579 if (arg.aggregate_type_p ())
5580 basereg = 16;
5581 else
5582 {
5583 /* With alpha_split_complex_arg, we shouldn't see any raw complex
5584 values here. */
5585 gcc_checking_assert (!COMPLEX_MODE_P (arg.mode));
5586
5587 /* Set up defaults for FP operands passed in FP registers, and
5588 integral operands passed in integer registers. */
5589 if (TARGET_FPREGS && GET_MODE_CLASS (arg.mode) == MODE_FLOAT)
5590 basereg = 32 + 16;
5591 else
5592 basereg = 16;
5593 }
5594
5595 /* ??? Irritatingly, the definition of CUMULATIVE_ARGS is different for
5596 the two platforms, so we can't avoid conditional compilation. */
5597 #if TARGET_ABI_OPEN_VMS
5598 {
5599 if (arg.end_marker_p ())
5600 return alpha_arg_info_reg_val (*cum);
5601
5602 num_args = cum->num_args;
5603 if (num_args >= 6
5604 || targetm.calls.must_pass_in_stack (arg))
5605 return NULL_RTX;
5606 }
5607 #elif TARGET_ABI_OSF
5608 {
5609 if (*cum >= 6)
5610 return NULL_RTX;
5611 num_args = *cum;
5612
5613 if (arg.end_marker_p ())
5614 basereg = 16;
5615 else if (targetm.calls.must_pass_in_stack (arg))
5616 return NULL_RTX;
5617 }
5618 #else
5619 #error Unhandled ABI
5620 #endif
5621
5622 return gen_rtx_REG (arg.mode, num_args + basereg);
5623 }
5624
5625 /* Update the data in CUM to advance over argument ARG. */
5626
5627 static void
5628 alpha_function_arg_advance (cumulative_args_t cum_v,
5629 const function_arg_info &arg)
5630 {
5631 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5632 bool onstack = targetm.calls.must_pass_in_stack (arg);
5633 int increment = onstack ? 6 : ALPHA_ARG_SIZE (arg.mode, arg.type);
5634
5635 #if TARGET_ABI_OSF
5636 *cum += increment;
5637 #else
5638 if (!onstack && cum->num_args < 6)
5639 cum->atypes[cum->num_args] = alpha_arg_type (arg.mode);
5640 cum->num_args += increment;
5641 #endif
5642 }
5643
5644 static int
5645 alpha_arg_partial_bytes (cumulative_args_t cum_v, const function_arg_info &arg)
5646 {
5647 int words = 0;
5648 CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED = get_cumulative_args (cum_v);
5649
5650 #if TARGET_ABI_OPEN_VMS
5651 if (cum->num_args < 6
5652 && 6 < cum->num_args + ALPHA_ARG_SIZE (arg.mode, arg.type))
5653 words = 6 - cum->num_args;
5654 #elif TARGET_ABI_OSF
5655 if (*cum < 6 && 6 < *cum + ALPHA_ARG_SIZE (arg.mode, arg.type))
5656 words = 6 - *cum;
5657 #else
5658 #error Unhandled ABI
5659 #endif
5660
5661 return words * UNITS_PER_WORD;
5662 }
5663
5664
5665 /* Return true if TYPE must be returned in memory, instead of in registers. */
5666
5667 static bool
5668 alpha_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5669 {
5670 machine_mode mode = VOIDmode;
5671 int size;
5672
5673 if (type)
5674 {
5675 mode = TYPE_MODE (type);
5676
5677 /* All aggregates are returned in memory, except on OpenVMS where
5678 records that fit 64 bits should be returned by immediate value
5679 as required by section 3.8.7.1 of the OpenVMS Calling Standard. */
5680 if (TARGET_ABI_OPEN_VMS
5681 && TREE_CODE (type) != ARRAY_TYPE
5682 && (unsigned HOST_WIDE_INT) int_size_in_bytes(type) <= 8)
5683 return false;
5684
5685 if (AGGREGATE_TYPE_P (type))
5686 return true;
5687 }
5688
5689 size = GET_MODE_SIZE (mode);
5690 switch (GET_MODE_CLASS (mode))
5691 {
5692 case MODE_VECTOR_FLOAT:
5693 /* Pass all float vectors in memory, like an aggregate. */
5694 return true;
5695
5696 case MODE_COMPLEX_FLOAT:
5697 /* We judge complex floats on the size of their element,
5698 not the size of the whole type. */
5699 size = GET_MODE_UNIT_SIZE (mode);
5700 break;
5701
5702 case MODE_INT:
5703 case MODE_FLOAT:
5704 case MODE_COMPLEX_INT:
5705 case MODE_VECTOR_INT:
5706 break;
5707
5708 default:
5709 /* ??? We get called on all sorts of random stuff from
5710 aggregate_value_p. We must return something, but it's not
5711 clear what's safe to return. Pretend it's a struct I
5712 guess. */
5713 return true;
5714 }
5715
5716 /* Otherwise types must fit in one register. */
5717 return size > UNITS_PER_WORD;
5718 }
5719
5720 /* Return true if ARG should be passed by invisible reference. */
5721
5722 static bool
5723 alpha_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
5724 {
5725 /* Pass float and _Complex float variable arguments by reference.
5726 This avoids 64-bit store from a FP register to a pretend args save area
5727 and subsequent 32-bit load from the saved location to a FP register.
5728
5729 Note that 32-bit loads and stores to/from a FP register on alpha reorder
5730 bits to form a canonical 64-bit value in the FP register. This fact
5731 invalidates compiler assumption that 32-bit FP value lives in the lower
5732 32-bits of the passed 64-bit FP value, so loading the 32-bit value from
5733 the stored 64-bit location using 32-bit FP load is invalid on alpha.
5734
5735 This introduces sort of ABI incompatibility, but until _Float32 was
5736 introduced, C-family languages promoted 32-bit float variable arg to
5737 a 64-bit double, and it was not allowed to pass float as a varible
5738 argument. Passing _Complex float as a variable argument never
5739 worked on alpha. Thus, we have no backward compatibility issues
5740 to worry about, and passing unpromoted _Float32 and _Complex float
5741 as a variable argument will actually work in the future. */
5742
5743 if (arg.mode == SFmode || arg.mode == SCmode)
5744 return !arg.named;
5745
5746 return arg.mode == TFmode || arg.mode == TCmode;
5747 }
5748
5749 /* Define how to find the value returned by a function. VALTYPE is the
5750 data type of the value (as a tree). If the precise function being
5751 called is known, FUNC is its FUNCTION_DECL; otherwise, FUNC is 0.
5752 MODE is set instead of VALTYPE for libcalls.
5753
5754 On Alpha the value is found in $0 for integer functions and
5755 $f0 for floating-point functions. */
5756
5757 static rtx
5758 alpha_function_value_1 (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
5759 machine_mode mode)
5760 {
5761 unsigned int regnum, dummy ATTRIBUTE_UNUSED;
5762 enum mode_class mclass;
5763
5764 gcc_assert (!valtype || !alpha_return_in_memory (valtype, func));
5765
5766 if (valtype)
5767 mode = TYPE_MODE (valtype);
5768
5769 mclass = GET_MODE_CLASS (mode);
5770 switch (mclass)
5771 {
5772 case MODE_INT:
5773 /* Do the same thing as PROMOTE_MODE except for libcalls on VMS,
5774 where we have them returning both SImode and DImode. */
5775 if (!(TARGET_ABI_OPEN_VMS && valtype && AGGREGATE_TYPE_P (valtype)))
5776 PROMOTE_MODE (mode, dummy, valtype);
5777 /* FALLTHRU */
5778
5779 case MODE_COMPLEX_INT:
5780 case MODE_VECTOR_INT:
5781 regnum = 0;
5782 break;
5783
5784 case MODE_FLOAT:
5785 regnum = 32;
5786 break;
5787
5788 case MODE_COMPLEX_FLOAT:
5789 {
5790 machine_mode cmode = GET_MODE_INNER (mode);
5791
5792 return gen_rtx_PARALLEL
5793 (VOIDmode,
5794 gen_rtvec (2,
5795 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (cmode, 32),
5796 const0_rtx),
5797 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (cmode, 33),
5798 GEN_INT (GET_MODE_SIZE (cmode)))));
5799 }
5800
5801 case MODE_RANDOM:
5802 /* We should only reach here for BLKmode on VMS. */
5803 gcc_assert (TARGET_ABI_OPEN_VMS && mode == BLKmode);
5804 regnum = 0;
5805 break;
5806
5807 default:
5808 gcc_unreachable ();
5809 }
5810
5811 return gen_rtx_REG (mode, regnum);
5812 }
5813
5814 /* Implement TARGET_FUNCTION_VALUE. */
5815
5816 static rtx
5817 alpha_function_value (const_tree valtype, const_tree fn_decl_or_type,
5818 bool /*outgoing*/)
5819 {
5820 return alpha_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5821 }
5822
5823 /* Implement TARGET_LIBCALL_VALUE. */
5824
5825 static rtx
5826 alpha_libcall_value (machine_mode mode, const_rtx /*fun*/)
5827 {
5828 return alpha_function_value_1 (NULL_TREE, NULL_TREE, mode);
5829 }
5830
5831 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5832
5833 On the Alpha, $0 $1 and $f0 $f1 are the only register thus used. */
5834
5835 static bool
5836 alpha_function_value_regno_p (const unsigned int regno)
5837 {
5838 return (regno == 0 || regno == 1 || regno == 32 || regno == 33);
5839 }
5840
5841 /* TCmode complex values are passed by invisible reference. We
5842 should not split these values. */
5843
5844 static bool
5845 alpha_split_complex_arg (const_tree type)
5846 {
5847 return TYPE_MODE (type) != TCmode;
5848 }
5849
5850 static tree
5851 alpha_build_builtin_va_list (void)
5852 {
5853 tree base, ofs, space, record, type_decl;
5854
5855 if (TARGET_ABI_OPEN_VMS)
5856 return ptr_type_node;
5857
5858 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
5859 type_decl = build_decl (BUILTINS_LOCATION,
5860 TYPE_DECL, get_identifier ("__va_list_tag"), record);
5861 TYPE_STUB_DECL (record) = type_decl;
5862 TYPE_NAME (record) = type_decl;
5863
5864 /* C++? SET_IS_AGGR_TYPE (record, 1); */
5865
5866 /* Dummy field to prevent alignment warnings. */
5867 space = build_decl (BUILTINS_LOCATION,
5868 FIELD_DECL, NULL_TREE, integer_type_node);
5869 DECL_FIELD_CONTEXT (space) = record;
5870 DECL_ARTIFICIAL (space) = 1;
5871 DECL_IGNORED_P (space) = 1;
5872
5873 ofs = build_decl (BUILTINS_LOCATION,
5874 FIELD_DECL, get_identifier ("__offset"),
5875 integer_type_node);
5876 DECL_FIELD_CONTEXT (ofs) = record;
5877 DECL_CHAIN (ofs) = space;
5878
5879 base = build_decl (BUILTINS_LOCATION,
5880 FIELD_DECL, get_identifier ("__base"),
5881 ptr_type_node);
5882 DECL_FIELD_CONTEXT (base) = record;
5883 DECL_CHAIN (base) = ofs;
5884
5885 TYPE_FIELDS (record) = base;
5886 layout_type (record);
5887
5888 va_list_gpr_counter_field = ofs;
5889 return record;
5890 }
5891
5892 #if TARGET_ABI_OSF
5893 /* Helper function for alpha_stdarg_optimize_hook. Skip over casts
5894 and constant additions. */
5895
5896 static gimple *
5897 va_list_skip_additions (tree lhs)
5898 {
5899 gimple *stmt;
5900
5901 for (;;)
5902 {
5903 enum tree_code code;
5904
5905 stmt = SSA_NAME_DEF_STMT (lhs);
5906
5907 if (gimple_code (stmt) == GIMPLE_PHI)
5908 return stmt;
5909
5910 if (!is_gimple_assign (stmt)
5911 || gimple_assign_lhs (stmt) != lhs)
5912 return NULL;
5913
5914 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
5915 return stmt;
5916 code = gimple_assign_rhs_code (stmt);
5917 if (!CONVERT_EXPR_CODE_P (code)
5918 && ((code != PLUS_EXPR && code != POINTER_PLUS_EXPR)
5919 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST
5920 || !tree_fits_uhwi_p (gimple_assign_rhs2 (stmt))))
5921 return stmt;
5922
5923 lhs = gimple_assign_rhs1 (stmt);
5924 }
5925 }
5926
5927 /* Check if LHS = RHS statement is
5928 LHS = *(ap.__base + ap.__offset + cst)
5929 or
5930 LHS = *(ap.__base
5931 + ((ap.__offset + cst <= 47)
5932 ? ap.__offset + cst - 48 : ap.__offset + cst) + cst2).
5933 If the former, indicate that GPR registers are needed,
5934 if the latter, indicate that FPR registers are needed.
5935
5936 Also look for LHS = (*ptr).field, where ptr is one of the forms
5937 listed above.
5938
5939 On alpha, cfun->va_list_gpr_size is used as size of the needed
5940 regs and cfun->va_list_fpr_size is a bitmask, bit 0 set if GPR
5941 registers are needed and bit 1 set if FPR registers are needed.
5942 Return true if va_list references should not be scanned for the
5943 current statement. */
5944
5945 static bool
5946 alpha_stdarg_optimize_hook (struct stdarg_info *si, const gimple *stmt)
5947 {
5948 tree base, offset, rhs;
5949 int offset_arg = 1;
5950 gimple *base_stmt;
5951
5952 if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
5953 != GIMPLE_SINGLE_RHS)
5954 return false;
5955
5956 rhs = gimple_assign_rhs1 (stmt);
5957 while (handled_component_p (rhs))
5958 rhs = TREE_OPERAND (rhs, 0);
5959 if (TREE_CODE (rhs) != MEM_REF
5960 || TREE_CODE (TREE_OPERAND (rhs, 0)) != SSA_NAME)
5961 return false;
5962
5963 stmt = va_list_skip_additions (TREE_OPERAND (rhs, 0));
5964 if (stmt == NULL
5965 || !is_gimple_assign (stmt)
5966 || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR)
5967 return false;
5968
5969 base = gimple_assign_rhs1 (stmt);
5970 if (TREE_CODE (base) == SSA_NAME)
5971 {
5972 base_stmt = va_list_skip_additions (base);
5973 if (base_stmt
5974 && is_gimple_assign (base_stmt)
5975 && gimple_assign_rhs_code (base_stmt) == COMPONENT_REF)
5976 base = gimple_assign_rhs1 (base_stmt);
5977 }
5978
5979 if (TREE_CODE (base) != COMPONENT_REF
5980 || TREE_OPERAND (base, 1) != TYPE_FIELDS (va_list_type_node))
5981 {
5982 base = gimple_assign_rhs2 (stmt);
5983 if (TREE_CODE (base) == SSA_NAME)
5984 {
5985 base_stmt = va_list_skip_additions (base);
5986 if (base_stmt
5987 && is_gimple_assign (base_stmt)
5988 && gimple_assign_rhs_code (base_stmt) == COMPONENT_REF)
5989 base = gimple_assign_rhs1 (base_stmt);
5990 }
5991
5992 if (TREE_CODE (base) != COMPONENT_REF
5993 || TREE_OPERAND (base, 1) != TYPE_FIELDS (va_list_type_node))
5994 return false;
5995
5996 offset_arg = 0;
5997 }
5998
5999 base = get_base_address (base);
6000 if (TREE_CODE (base) != VAR_DECL
6001 || !bitmap_bit_p (si->va_list_vars, DECL_UID (base) + num_ssa_names))
6002 return false;
6003
6004 offset = gimple_op (stmt, 1 + offset_arg);
6005 if (TREE_CODE (offset) == SSA_NAME)
6006 {
6007 gimple *offset_stmt = va_list_skip_additions (offset);
6008
6009 if (offset_stmt
6010 && gimple_code (offset_stmt) == GIMPLE_PHI)
6011 {
6012 HOST_WIDE_INT sub;
6013 gimple *arg1_stmt, *arg2_stmt;
6014 tree arg1, arg2;
6015 enum tree_code code1, code2;
6016
6017 if (gimple_phi_num_args (offset_stmt) != 2)
6018 goto escapes;
6019
6020 arg1_stmt
6021 = va_list_skip_additions (gimple_phi_arg_def (offset_stmt, 0));
6022 arg2_stmt
6023 = va_list_skip_additions (gimple_phi_arg_def (offset_stmt, 1));
6024 if (arg1_stmt == NULL
6025 || !is_gimple_assign (arg1_stmt)
6026 || arg2_stmt == NULL
6027 || !is_gimple_assign (arg2_stmt))
6028 goto escapes;
6029
6030 code1 = gimple_assign_rhs_code (arg1_stmt);
6031 code2 = gimple_assign_rhs_code (arg2_stmt);
6032 if (code1 == COMPONENT_REF
6033 && (code2 == MINUS_EXPR || code2 == PLUS_EXPR))
6034 /* Do nothing. */;
6035 else if (code2 == COMPONENT_REF
6036 && (code1 == MINUS_EXPR || code1 == PLUS_EXPR))
6037 {
6038 std::swap (arg1_stmt, arg2_stmt);
6039 code2 = code1;
6040 }
6041 else
6042 goto escapes;
6043
6044 if (!tree_fits_shwi_p (gimple_assign_rhs2 (arg2_stmt)))
6045 goto escapes;
6046
6047 sub = tree_to_shwi (gimple_assign_rhs2 (arg2_stmt));
6048 if (code2 == MINUS_EXPR)
6049 sub = -sub;
6050 if (sub < -48 || sub > -32)
6051 goto escapes;
6052
6053 arg1 = gimple_assign_rhs1 (arg1_stmt);
6054 arg2 = gimple_assign_rhs1 (arg2_stmt);
6055 if (TREE_CODE (arg2) == SSA_NAME)
6056 {
6057 arg2_stmt = va_list_skip_additions (arg2);
6058 if (arg2_stmt == NULL
6059 || !is_gimple_assign (arg2_stmt)
6060 || gimple_assign_rhs_code (arg2_stmt) != COMPONENT_REF)
6061 goto escapes;
6062 arg2 = gimple_assign_rhs1 (arg2_stmt);
6063 }
6064 if (arg1 != arg2)
6065 goto escapes;
6066
6067 if (TREE_CODE (arg1) != COMPONENT_REF
6068 || TREE_OPERAND (arg1, 1) != va_list_gpr_counter_field
6069 || get_base_address (arg1) != base)
6070 goto escapes;
6071
6072 /* Need floating point regs. */
6073 cfun->va_list_fpr_size |= 2;
6074 return false;
6075 }
6076 if (offset_stmt
6077 && is_gimple_assign (offset_stmt)
6078 && gimple_assign_rhs_code (offset_stmt) == COMPONENT_REF)
6079 offset = gimple_assign_rhs1 (offset_stmt);
6080 }
6081 if (TREE_CODE (offset) != COMPONENT_REF
6082 || TREE_OPERAND (offset, 1) != va_list_gpr_counter_field
6083 || get_base_address (offset) != base)
6084 goto escapes;
6085 else
6086 /* Need general regs. */
6087 cfun->va_list_fpr_size |= 1;
6088 return false;
6089
6090 escapes:
6091 si->va_list_escapes = true;
6092 return false;
6093 }
6094 #endif
6095
6096 /* Perform any needed actions needed for a function that is receiving a
6097 variable number of arguments. */
6098
6099 static void
6100 alpha_setup_incoming_varargs (cumulative_args_t pcum,
6101 const function_arg_info &arg,
6102 int *pretend_size, int no_rtl)
6103 {
6104 CUMULATIVE_ARGS cum = *get_cumulative_args (pcum);
6105
6106 /* Skip the current argument. */
6107 targetm.calls.function_arg_advance (pack_cumulative_args (&cum), arg);
6108
6109 #if TARGET_ABI_OPEN_VMS
6110 /* For VMS, we allocate space for all 6 arg registers plus a count.
6111
6112 However, if NO registers need to be saved, don't allocate any space.
6113 This is not only because we won't need the space, but because AP
6114 includes the current_pretend_args_size and we don't want to mess up
6115 any ap-relative addresses already made. */
6116 if (cum.num_args < 6)
6117 {
6118 if (!no_rtl)
6119 {
6120 emit_move_insn (gen_rtx_REG (DImode, 1), virtual_incoming_args_rtx);
6121 emit_insn (gen_arg_home ());
6122 }
6123 *pretend_size = 7 * UNITS_PER_WORD;
6124 }
6125 #else
6126 /* On OSF/1 and friends, we allocate space for all 12 arg registers, but
6127 only push those that are remaining. However, if NO registers need to
6128 be saved, don't allocate any space. This is not only because we won't
6129 need the space, but because AP includes the current_pretend_args_size
6130 and we don't want to mess up any ap-relative addresses already made.
6131
6132 If we are not to use the floating-point registers, save the integer
6133 registers where we would put the floating-point registers. This is
6134 not the most efficient way to implement varargs with just one register
6135 class, but it isn't worth doing anything more efficient in this rare
6136 case. */
6137 if (cum >= 6)
6138 return;
6139
6140 if (!no_rtl)
6141 {
6142 int count;
6143 alias_set_type set = get_varargs_alias_set ();
6144 rtx tmp;
6145
6146 count = cfun->va_list_gpr_size / UNITS_PER_WORD;
6147 if (count > 6 - cum)
6148 count = 6 - cum;
6149
6150 /* Detect whether integer registers or floating-point registers
6151 are needed by the detected va_arg statements. See above for
6152 how these values are computed. Note that the "escape" value
6153 is VA_LIST_MAX_FPR_SIZE, which is 255, which has both of
6154 these bits set. */
6155 gcc_assert ((VA_LIST_MAX_FPR_SIZE & 3) == 3);
6156
6157 if (cfun->va_list_fpr_size & 1)
6158 {
6159 tmp = gen_rtx_MEM (BLKmode,
6160 plus_constant (Pmode, virtual_incoming_args_rtx,
6161 (cum + 6) * UNITS_PER_WORD));
6162 MEM_NOTRAP_P (tmp) = 1;
6163 set_mem_alias_set (tmp, set);
6164 move_block_from_reg (16 + cum, tmp, count);
6165 }
6166
6167 if (cfun->va_list_fpr_size & 2)
6168 {
6169 tmp = gen_rtx_MEM (BLKmode,
6170 plus_constant (Pmode, virtual_incoming_args_rtx,
6171 cum * UNITS_PER_WORD));
6172 MEM_NOTRAP_P (tmp) = 1;
6173 set_mem_alias_set (tmp, set);
6174 move_block_from_reg (16 + cum + TARGET_FPREGS*32, tmp, count);
6175 }
6176 }
6177 *pretend_size = 12 * UNITS_PER_WORD;
6178 #endif
6179 }
6180
6181 static void
6182 alpha_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
6183 {
6184 HOST_WIDE_INT offset;
6185 tree t, offset_field, base_field;
6186
6187 if (TREE_CODE (TREE_TYPE (valist)) == ERROR_MARK)
6188 return;
6189
6190 /* For Unix, TARGET_SETUP_INCOMING_VARARGS moves the starting address base
6191 up by 48, storing fp arg registers in the first 48 bytes, and the
6192 integer arg registers in the next 48 bytes. This is only done,
6193 however, if any integer registers need to be stored.
6194
6195 If no integer registers need be stored, then we must subtract 48
6196 in order to account for the integer arg registers which are counted
6197 in argsize above, but which are not actually stored on the stack.
6198 Must further be careful here about structures straddling the last
6199 integer argument register; that futzes with pretend_args_size,
6200 which changes the meaning of AP. */
6201
6202 if (NUM_ARGS < 6)
6203 offset = TARGET_ABI_OPEN_VMS ? UNITS_PER_WORD : 6 * UNITS_PER_WORD;
6204 else
6205 offset = -6 * UNITS_PER_WORD + crtl->args.pretend_args_size;
6206
6207 if (TARGET_ABI_OPEN_VMS)
6208 {
6209 t = make_tree (ptr_type_node, virtual_incoming_args_rtx);
6210 t = fold_build_pointer_plus_hwi (t, offset + NUM_ARGS * UNITS_PER_WORD);
6211 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
6212 TREE_SIDE_EFFECTS (t) = 1;
6213 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6214 }
6215 else
6216 {
6217 base_field = TYPE_FIELDS (TREE_TYPE (valist));
6218 offset_field = DECL_CHAIN (base_field);
6219
6220 base_field = build3 (COMPONENT_REF, TREE_TYPE (base_field),
6221 valist, base_field, NULL_TREE);
6222 offset_field = build3 (COMPONENT_REF, TREE_TYPE (offset_field),
6223 valist, offset_field, NULL_TREE);
6224
6225 t = make_tree (ptr_type_node, virtual_incoming_args_rtx);
6226 t = fold_build_pointer_plus_hwi (t, offset);
6227 t = build2 (MODIFY_EXPR, TREE_TYPE (base_field), base_field, t);
6228 TREE_SIDE_EFFECTS (t) = 1;
6229 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6230
6231 t = build_int_cst (NULL_TREE, NUM_ARGS * UNITS_PER_WORD);
6232 t = build2 (MODIFY_EXPR, TREE_TYPE (offset_field), offset_field, t);
6233 TREE_SIDE_EFFECTS (t) = 1;
6234 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6235 }
6236 }
6237
6238 static tree
6239 alpha_gimplify_va_arg_1 (tree type, tree base, tree offset,
6240 gimple_seq *pre_p)
6241 {
6242 tree type_size, ptr_type, addend, t, addr;
6243 gimple_seq internal_post;
6244
6245 /* If the type could not be passed in registers, skip the block
6246 reserved for the registers. */
6247 if (must_pass_va_arg_in_stack (type))
6248 {
6249 t = build_int_cst (TREE_TYPE (offset), 6*8);
6250 gimplify_assign (offset,
6251 build2 (MAX_EXPR, TREE_TYPE (offset), offset, t),
6252 pre_p);
6253 }
6254
6255 addend = offset;
6256 ptr_type = build_pointer_type_for_mode (type, ptr_mode, true);
6257
6258 if (TREE_CODE (type) == COMPLEX_TYPE)
6259 {
6260 tree real_part, imag_part, real_temp;
6261
6262 real_part = alpha_gimplify_va_arg_1 (TREE_TYPE (type), base,
6263 offset, pre_p);
6264
6265 /* Copy the value into a new temporary, lest the formal temporary
6266 be reused out from under us. */
6267 real_temp = get_initialized_tmp_var (real_part, pre_p, NULL);
6268
6269 imag_part = alpha_gimplify_va_arg_1 (TREE_TYPE (type), base,
6270 offset, pre_p);
6271
6272 return build2 (COMPLEX_EXPR, type, real_temp, imag_part);
6273 }
6274 else if (TREE_CODE (type) == REAL_TYPE)
6275 {
6276 tree fpaddend, cond, fourtyeight;
6277
6278 fourtyeight = build_int_cst (TREE_TYPE (addend), 6*8);
6279 fpaddend = fold_build2 (MINUS_EXPR, TREE_TYPE (addend),
6280 addend, fourtyeight);
6281 cond = fold_build2 (LT_EXPR, boolean_type_node, addend, fourtyeight);
6282 addend = fold_build3 (COND_EXPR, TREE_TYPE (addend), cond,
6283 fpaddend, addend);
6284 }
6285
6286 /* Build the final address and force that value into a temporary. */
6287 addr = fold_build_pointer_plus (fold_convert (ptr_type, base), addend);
6288 internal_post = NULL;
6289 gimplify_expr (&addr, pre_p, &internal_post, is_gimple_val, fb_rvalue);
6290 gimple_seq_add_seq (pre_p, internal_post);
6291
6292 /* Update the offset field. */
6293 type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type));
6294 if (type_size == NULL || TREE_OVERFLOW (type_size))
6295 t = size_zero_node;
6296 else
6297 {
6298 t = size_binop (PLUS_EXPR, type_size, size_int (7));
6299 t = size_binop (TRUNC_DIV_EXPR, t, size_int (8));
6300 t = size_binop (MULT_EXPR, t, size_int (8));
6301 }
6302 t = fold_convert (TREE_TYPE (offset), t);
6303 gimplify_assign (offset, build2 (PLUS_EXPR, TREE_TYPE (offset), offset, t),
6304 pre_p);
6305
6306 return build_va_arg_indirect_ref (addr);
6307 }
6308
6309 static tree
6310 alpha_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
6311 gimple_seq *post_p)
6312 {
6313 tree offset_field, base_field, offset, base, t, r;
6314 bool indirect;
6315
6316 if (TARGET_ABI_OPEN_VMS)
6317 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
6318
6319 base_field = TYPE_FIELDS (va_list_type_node);
6320 offset_field = DECL_CHAIN (base_field);
6321 base_field = build3 (COMPONENT_REF, TREE_TYPE (base_field),
6322 valist, base_field, NULL_TREE);
6323 offset_field = build3 (COMPONENT_REF, TREE_TYPE (offset_field),
6324 valist, offset_field, NULL_TREE);
6325
6326 /* Pull the fields of the structure out into temporaries. Since we never
6327 modify the base field, we can use a formal temporary. Sign-extend the
6328 offset field so that it's the proper width for pointer arithmetic. */
6329 base = get_formal_tmp_var (base_field, pre_p);
6330
6331 t = fold_convert (build_nonstandard_integer_type (64, 0), offset_field);
6332 offset = get_initialized_tmp_var (t, pre_p, NULL);
6333
6334 indirect = pass_va_arg_by_reference (type);
6335
6336 if (indirect)
6337 {
6338 if (TREE_CODE (type) == COMPLEX_TYPE
6339 && targetm.calls.split_complex_arg (type))
6340 {
6341 tree real_part, imag_part, real_temp;
6342
6343 tree ptr_type = build_pointer_type_for_mode (TREE_TYPE (type),
6344 ptr_mode, true);
6345
6346 real_part = alpha_gimplify_va_arg_1 (ptr_type, base,
6347 offset, pre_p);
6348 real_part = build_va_arg_indirect_ref (real_part);
6349
6350 /* Copy the value into a new temporary, lest the formal temporary
6351 be reused out from under us. */
6352 real_temp = get_initialized_tmp_var (real_part, pre_p, NULL);
6353
6354 imag_part = alpha_gimplify_va_arg_1 (ptr_type, base,
6355 offset, pre_p);
6356 imag_part = build_va_arg_indirect_ref (imag_part);
6357
6358 r = build2 (COMPLEX_EXPR, type, real_temp, imag_part);
6359
6360 /* Stuff the offset temporary back into its field. */
6361 gimplify_assign (unshare_expr (offset_field),
6362 fold_convert (TREE_TYPE (offset_field), offset),
6363 pre_p);
6364 return r;
6365 }
6366 else
6367 type = build_pointer_type_for_mode (type, ptr_mode, true);
6368 }
6369
6370 /* Find the value. Note that this will be a stable indirection, or
6371 a composite of stable indirections in the case of complex. */
6372 r = alpha_gimplify_va_arg_1 (type, base, offset, pre_p);
6373
6374 /* Stuff the offset temporary back into its field. */
6375 gimplify_assign (unshare_expr (offset_field),
6376 fold_convert (TREE_TYPE (offset_field), offset), pre_p);
6377
6378 if (indirect)
6379 r = build_va_arg_indirect_ref (r);
6380
6381 return r;
6382 }
6383
6384 /* Builtins. */
6386
6387 enum alpha_builtin
6388 {
6389 ALPHA_BUILTIN_CMPBGE,
6390 ALPHA_BUILTIN_EXTBL,
6391 ALPHA_BUILTIN_EXTWL,
6392 ALPHA_BUILTIN_EXTLL,
6393 ALPHA_BUILTIN_EXTQL,
6394 ALPHA_BUILTIN_EXTWH,
6395 ALPHA_BUILTIN_EXTLH,
6396 ALPHA_BUILTIN_EXTQH,
6397 ALPHA_BUILTIN_INSBL,
6398 ALPHA_BUILTIN_INSWL,
6399 ALPHA_BUILTIN_INSLL,
6400 ALPHA_BUILTIN_INSQL,
6401 ALPHA_BUILTIN_INSWH,
6402 ALPHA_BUILTIN_INSLH,
6403 ALPHA_BUILTIN_INSQH,
6404 ALPHA_BUILTIN_MSKBL,
6405 ALPHA_BUILTIN_MSKWL,
6406 ALPHA_BUILTIN_MSKLL,
6407 ALPHA_BUILTIN_MSKQL,
6408 ALPHA_BUILTIN_MSKWH,
6409 ALPHA_BUILTIN_MSKLH,
6410 ALPHA_BUILTIN_MSKQH,
6411 ALPHA_BUILTIN_UMULH,
6412 ALPHA_BUILTIN_ZAP,
6413 ALPHA_BUILTIN_ZAPNOT,
6414 ALPHA_BUILTIN_AMASK,
6415 ALPHA_BUILTIN_IMPLVER,
6416 ALPHA_BUILTIN_RPCC,
6417 ALPHA_BUILTIN_ESTABLISH_VMS_CONDITION_HANDLER,
6418 ALPHA_BUILTIN_REVERT_VMS_CONDITION_HANDLER,
6419
6420 /* TARGET_MAX */
6421 ALPHA_BUILTIN_MINUB8,
6422 ALPHA_BUILTIN_MINSB8,
6423 ALPHA_BUILTIN_MINUW4,
6424 ALPHA_BUILTIN_MINSW4,
6425 ALPHA_BUILTIN_MAXUB8,
6426 ALPHA_BUILTIN_MAXSB8,
6427 ALPHA_BUILTIN_MAXUW4,
6428 ALPHA_BUILTIN_MAXSW4,
6429 ALPHA_BUILTIN_PERR,
6430 ALPHA_BUILTIN_PKLB,
6431 ALPHA_BUILTIN_PKWB,
6432 ALPHA_BUILTIN_UNPKBL,
6433 ALPHA_BUILTIN_UNPKBW,
6434
6435 /* TARGET_CIX */
6436 ALPHA_BUILTIN_CTTZ,
6437 ALPHA_BUILTIN_CTLZ,
6438 ALPHA_BUILTIN_CTPOP,
6439
6440 ALPHA_BUILTIN_max
6441 };
6442
6443 static enum insn_code const code_for_builtin[ALPHA_BUILTIN_max] = {
6444 CODE_FOR_builtin_cmpbge,
6445 CODE_FOR_extbl,
6446 CODE_FOR_extwl,
6447 CODE_FOR_extll,
6448 CODE_FOR_extql,
6449 CODE_FOR_extwh,
6450 CODE_FOR_extlh,
6451 CODE_FOR_extqh,
6452 CODE_FOR_builtin_insbl,
6453 CODE_FOR_builtin_inswl,
6454 CODE_FOR_builtin_insll,
6455 CODE_FOR_insql,
6456 CODE_FOR_inswh,
6457 CODE_FOR_inslh,
6458 CODE_FOR_insqh,
6459 CODE_FOR_mskbl,
6460 CODE_FOR_mskwl,
6461 CODE_FOR_mskll,
6462 CODE_FOR_mskql,
6463 CODE_FOR_mskwh,
6464 CODE_FOR_msklh,
6465 CODE_FOR_mskqh,
6466 CODE_FOR_umuldi3_highpart,
6467 CODE_FOR_builtin_zap,
6468 CODE_FOR_builtin_zapnot,
6469 CODE_FOR_builtin_amask,
6470 CODE_FOR_builtin_implver,
6471 CODE_FOR_builtin_rpcc,
6472 CODE_FOR_builtin_establish_vms_condition_handler,
6473 CODE_FOR_builtin_revert_vms_condition_handler,
6474
6475 /* TARGET_MAX */
6476 CODE_FOR_builtin_minub8,
6477 CODE_FOR_builtin_minsb8,
6478 CODE_FOR_builtin_minuw4,
6479 CODE_FOR_builtin_minsw4,
6480 CODE_FOR_builtin_maxub8,
6481 CODE_FOR_builtin_maxsb8,
6482 CODE_FOR_builtin_maxuw4,
6483 CODE_FOR_builtin_maxsw4,
6484 CODE_FOR_builtin_perr,
6485 CODE_FOR_builtin_pklb,
6486 CODE_FOR_builtin_pkwb,
6487 CODE_FOR_builtin_unpkbl,
6488 CODE_FOR_builtin_unpkbw,
6489
6490 /* TARGET_CIX */
6491 CODE_FOR_ctzdi2,
6492 CODE_FOR_clzdi2,
6493 CODE_FOR_popcountdi2
6494 };
6495
6496 struct alpha_builtin_def
6497 {
6498 const char *name;
6499 enum alpha_builtin code;
6500 unsigned int target_mask;
6501 bool is_const;
6502 };
6503
6504 static struct alpha_builtin_def const zero_arg_builtins[] = {
6505 { "__builtin_alpha_implver", ALPHA_BUILTIN_IMPLVER, 0, true },
6506 { "__builtin_alpha_rpcc", ALPHA_BUILTIN_RPCC, 0, false }
6507 };
6508
6509 static struct alpha_builtin_def const one_arg_builtins[] = {
6510 { "__builtin_alpha_amask", ALPHA_BUILTIN_AMASK, 0, true },
6511 { "__builtin_alpha_pklb", ALPHA_BUILTIN_PKLB, MASK_MAX, true },
6512 { "__builtin_alpha_pkwb", ALPHA_BUILTIN_PKWB, MASK_MAX, true },
6513 { "__builtin_alpha_unpkbl", ALPHA_BUILTIN_UNPKBL, MASK_MAX, true },
6514 { "__builtin_alpha_unpkbw", ALPHA_BUILTIN_UNPKBW, MASK_MAX, true },
6515 { "__builtin_alpha_cttz", ALPHA_BUILTIN_CTTZ, MASK_CIX, true },
6516 { "__builtin_alpha_ctlz", ALPHA_BUILTIN_CTLZ, MASK_CIX, true },
6517 { "__builtin_alpha_ctpop", ALPHA_BUILTIN_CTPOP, MASK_CIX, true }
6518 };
6519
6520 static struct alpha_builtin_def const two_arg_builtins[] = {
6521 { "__builtin_alpha_cmpbge", ALPHA_BUILTIN_CMPBGE, 0, true },
6522 { "__builtin_alpha_extbl", ALPHA_BUILTIN_EXTBL, 0, true },
6523 { "__builtin_alpha_extwl", ALPHA_BUILTIN_EXTWL, 0, true },
6524 { "__builtin_alpha_extll", ALPHA_BUILTIN_EXTLL, 0, true },
6525 { "__builtin_alpha_extql", ALPHA_BUILTIN_EXTQL, 0, true },
6526 { "__builtin_alpha_extwh", ALPHA_BUILTIN_EXTWH, 0, true },
6527 { "__builtin_alpha_extlh", ALPHA_BUILTIN_EXTLH, 0, true },
6528 { "__builtin_alpha_extqh", ALPHA_BUILTIN_EXTQH, 0, true },
6529 { "__builtin_alpha_insbl", ALPHA_BUILTIN_INSBL, 0, true },
6530 { "__builtin_alpha_inswl", ALPHA_BUILTIN_INSWL, 0, true },
6531 { "__builtin_alpha_insll", ALPHA_BUILTIN_INSLL, 0, true },
6532 { "__builtin_alpha_insql", ALPHA_BUILTIN_INSQL, 0, true },
6533 { "__builtin_alpha_inswh", ALPHA_BUILTIN_INSWH, 0, true },
6534 { "__builtin_alpha_inslh", ALPHA_BUILTIN_INSLH, 0, true },
6535 { "__builtin_alpha_insqh", ALPHA_BUILTIN_INSQH, 0, true },
6536 { "__builtin_alpha_mskbl", ALPHA_BUILTIN_MSKBL, 0, true },
6537 { "__builtin_alpha_mskwl", ALPHA_BUILTIN_MSKWL, 0, true },
6538 { "__builtin_alpha_mskll", ALPHA_BUILTIN_MSKLL, 0, true },
6539 { "__builtin_alpha_mskql", ALPHA_BUILTIN_MSKQL, 0, true },
6540 { "__builtin_alpha_mskwh", ALPHA_BUILTIN_MSKWH, 0, true },
6541 { "__builtin_alpha_msklh", ALPHA_BUILTIN_MSKLH, 0, true },
6542 { "__builtin_alpha_mskqh", ALPHA_BUILTIN_MSKQH, 0, true },
6543 { "__builtin_alpha_umulh", ALPHA_BUILTIN_UMULH, 0, true },
6544 { "__builtin_alpha_zap", ALPHA_BUILTIN_ZAP, 0, true },
6545 { "__builtin_alpha_zapnot", ALPHA_BUILTIN_ZAPNOT, 0, true },
6546 { "__builtin_alpha_minub8", ALPHA_BUILTIN_MINUB8, MASK_MAX, true },
6547 { "__builtin_alpha_minsb8", ALPHA_BUILTIN_MINSB8, MASK_MAX, true },
6548 { "__builtin_alpha_minuw4", ALPHA_BUILTIN_MINUW4, MASK_MAX, true },
6549 { "__builtin_alpha_minsw4", ALPHA_BUILTIN_MINSW4, MASK_MAX, true },
6550 { "__builtin_alpha_maxub8", ALPHA_BUILTIN_MAXUB8, MASK_MAX, true },
6551 { "__builtin_alpha_maxsb8", ALPHA_BUILTIN_MAXSB8, MASK_MAX, true },
6552 { "__builtin_alpha_maxuw4", ALPHA_BUILTIN_MAXUW4, MASK_MAX, true },
6553 { "__builtin_alpha_maxsw4", ALPHA_BUILTIN_MAXSW4, MASK_MAX, true },
6554 { "__builtin_alpha_perr", ALPHA_BUILTIN_PERR, MASK_MAX, true }
6555 };
6556
6557 static GTY(()) tree alpha_dimode_u;
6558 static GTY(()) tree alpha_v8qi_u;
6559 static GTY(()) tree alpha_v8qi_s;
6560 static GTY(()) tree alpha_v4hi_u;
6561 static GTY(()) tree alpha_v4hi_s;
6562
6563 static GTY(()) tree alpha_builtins[(int) ALPHA_BUILTIN_max];
6564
6565 /* Return the alpha builtin for CODE. */
6566
6567 static tree
6568 alpha_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
6569 {
6570 if (code >= ALPHA_BUILTIN_max)
6571 return error_mark_node;
6572 return alpha_builtins[code];
6573 }
6574
6575 /* Helper function of alpha_init_builtins. Add the built-in specified
6576 by NAME, TYPE, CODE, and ECF. */
6577
6578 static void
6579 alpha_builtin_function (const char *name, tree ftype,
6580 enum alpha_builtin code, unsigned ecf)
6581 {
6582 tree decl = add_builtin_function (name, ftype, (int) code,
6583 BUILT_IN_MD, NULL, NULL_TREE);
6584
6585 if (ecf & ECF_CONST)
6586 TREE_READONLY (decl) = 1;
6587 if (ecf & ECF_NOTHROW)
6588 TREE_NOTHROW (decl) = 1;
6589
6590 alpha_builtins [(int) code] = decl;
6591 }
6592
6593 /* Helper function of alpha_init_builtins. Add the COUNT built-in
6594 functions pointed to by P, with function type FTYPE. */
6595
6596 static void
6597 alpha_add_builtins (const struct alpha_builtin_def *p, size_t count,
6598 tree ftype)
6599 {
6600 size_t i;
6601
6602 for (i = 0; i < count; ++i, ++p)
6603 if ((target_flags & p->target_mask) == p->target_mask)
6604 alpha_builtin_function (p->name, ftype, p->code,
6605 (p->is_const ? ECF_CONST : 0) | ECF_NOTHROW);
6606 }
6607
6608 static void
6609 alpha_init_builtins (void)
6610 {
6611 tree ftype;
6612
6613 alpha_dimode_u = lang_hooks.types.type_for_mode (DImode, 1);
6614 alpha_v8qi_u = build_vector_type (unsigned_intQI_type_node, 8);
6615 alpha_v8qi_s = build_vector_type (intQI_type_node, 8);
6616 alpha_v4hi_u = build_vector_type (unsigned_intHI_type_node, 4);
6617 alpha_v4hi_s = build_vector_type (intHI_type_node, 4);
6618
6619 ftype = build_function_type_list (alpha_dimode_u, NULL_TREE);
6620 alpha_add_builtins (zero_arg_builtins, ARRAY_SIZE (zero_arg_builtins), ftype);
6621
6622 ftype = build_function_type_list (alpha_dimode_u, alpha_dimode_u, NULL_TREE);
6623 alpha_add_builtins (one_arg_builtins, ARRAY_SIZE (one_arg_builtins), ftype);
6624
6625 ftype = build_function_type_list (alpha_dimode_u, alpha_dimode_u,
6626 alpha_dimode_u, NULL_TREE);
6627 alpha_add_builtins (two_arg_builtins, ARRAY_SIZE (two_arg_builtins), ftype);
6628
6629 if (TARGET_ABI_OPEN_VMS)
6630 {
6631 ftype = build_function_type_list (ptr_type_node, ptr_type_node,
6632 NULL_TREE);
6633 alpha_builtin_function ("__builtin_establish_vms_condition_handler",
6634 ftype,
6635 ALPHA_BUILTIN_ESTABLISH_VMS_CONDITION_HANDLER,
6636 0);
6637
6638 ftype = build_function_type_list (ptr_type_node, void_type_node,
6639 NULL_TREE);
6640 alpha_builtin_function ("__builtin_revert_vms_condition_handler", ftype,
6641 ALPHA_BUILTIN_REVERT_VMS_CONDITION_HANDLER, 0);
6642
6643 vms_patch_builtins ();
6644 }
6645 }
6646
6647 /* Expand an expression EXP that calls a built-in function,
6648 with result going to TARGET if that's convenient
6649 (and in mode MODE if that's convenient).
6650 SUBTARGET may be used as the target for computing one of EXP's operands.
6651 IGNORE is nonzero if the value is to be ignored. */
6652
6653 static rtx
6654 alpha_expand_builtin (tree exp, rtx target,
6655 rtx subtarget ATTRIBUTE_UNUSED,
6656 machine_mode mode ATTRIBUTE_UNUSED,
6657 int ignore ATTRIBUTE_UNUSED)
6658 {
6659 #define MAX_ARGS 2
6660
6661 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
6662 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
6663 tree arg;
6664 call_expr_arg_iterator iter;
6665 enum insn_code icode;
6666 rtx op[MAX_ARGS], pat;
6667 int arity;
6668 bool nonvoid;
6669
6670 if (fcode >= ALPHA_BUILTIN_max)
6671 internal_error ("bad builtin fcode");
6672 icode = code_for_builtin[fcode];
6673 if (icode == 0)
6674 internal_error ("bad builtin fcode");
6675
6676 nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
6677
6678 arity = 0;
6679 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6680 {
6681 const struct insn_operand_data *insn_op;
6682
6683 if (arg == error_mark_node)
6684 return NULL_RTX;
6685 if (arity > MAX_ARGS)
6686 return NULL_RTX;
6687
6688 insn_op = &insn_data[icode].operand[arity + nonvoid];
6689
6690 op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, EXPAND_NORMAL);
6691
6692 if (!(*insn_op->predicate) (op[arity], insn_op->mode))
6693 op[arity] = copy_to_mode_reg (insn_op->mode, op[arity]);
6694 arity++;
6695 }
6696
6697 if (nonvoid)
6698 {
6699 machine_mode tmode = insn_data[icode].operand[0].mode;
6700 if (!target
6701 || GET_MODE (target) != tmode
6702 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
6703 target = gen_reg_rtx (tmode);
6704 }
6705
6706 switch (arity)
6707 {
6708 case 0:
6709 pat = GEN_FCN (icode) (target);
6710 break;
6711 case 1:
6712 if (nonvoid)
6713 pat = GEN_FCN (icode) (target, op[0]);
6714 else
6715 pat = GEN_FCN (icode) (op[0]);
6716 break;
6717 case 2:
6718 pat = GEN_FCN (icode) (target, op[0], op[1]);
6719 break;
6720 default:
6721 gcc_unreachable ();
6722 }
6723 if (!pat)
6724 return NULL_RTX;
6725 emit_insn (pat);
6726
6727 if (nonvoid)
6728 return target;
6729 else
6730 return const0_rtx;
6731 }
6732
6733 /* Fold the builtin for the CMPBGE instruction. This is a vector comparison
6734 with an 8-bit output vector. OPINT contains the integer operands; bit N
6735 of OP_CONST is set if OPINT[N] is valid. */
6736
6737 static tree
6738 alpha_fold_builtin_cmpbge (unsigned HOST_WIDE_INT opint[], long op_const)
6739 {
6740 if (op_const == 3)
6741 {
6742 int i, val;
6743 for (i = 0, val = 0; i < 8; ++i)
6744 {
6745 unsigned HOST_WIDE_INT c0 = (opint[0] >> (i * 8)) & 0xff;
6746 unsigned HOST_WIDE_INT c1 = (opint[1] >> (i * 8)) & 0xff;
6747 if (c0 >= c1)
6748 val |= 1 << i;
6749 }
6750 return build_int_cst (alpha_dimode_u, val);
6751 }
6752 else if (op_const == 2 && opint[1] == 0)
6753 return build_int_cst (alpha_dimode_u, 0xff);
6754 return NULL;
6755 }
6756
6757 /* Fold the builtin for the ZAPNOT instruction. This is essentially a
6758 specialized form of an AND operation. Other byte manipulation instructions
6759 are defined in terms of this instruction, so this is also used as a
6760 subroutine for other builtins.
6761
6762 OP contains the tree operands; OPINT contains the extracted integer values.
6763 Bit N of OP_CONST it set if OPINT[N] is valid. OP may be null if only
6764 OPINT may be considered. */
6765
6766 static tree
6767 alpha_fold_builtin_zapnot (tree *op, unsigned HOST_WIDE_INT opint[],
6768 long op_const)
6769 {
6770 if (op_const & 2)
6771 {
6772 unsigned HOST_WIDE_INT mask = 0;
6773 int i;
6774
6775 for (i = 0; i < 8; ++i)
6776 if ((opint[1] >> i) & 1)
6777 mask |= (unsigned HOST_WIDE_INT)0xff << (i * 8);
6778
6779 if (op_const & 1)
6780 return build_int_cst (alpha_dimode_u, opint[0] & mask);
6781
6782 if (op)
6783 return fold_build2 (BIT_AND_EXPR, alpha_dimode_u, op[0],
6784 build_int_cst (alpha_dimode_u, mask));
6785 }
6786 else if ((op_const & 1) && opint[0] == 0)
6787 return build_int_cst (alpha_dimode_u, 0);
6788 return NULL;
6789 }
6790
6791 /* Fold the builtins for the EXT family of instructions. */
6792
6793 static tree
6794 alpha_fold_builtin_extxx (tree op[], unsigned HOST_WIDE_INT opint[],
6795 long op_const, unsigned HOST_WIDE_INT bytemask,
6796 bool is_high)
6797 {
6798 long zap_const = 2;
6799 tree *zap_op = NULL;
6800
6801 if (op_const & 2)
6802 {
6803 unsigned HOST_WIDE_INT loc;
6804
6805 loc = opint[1] & 7;
6806 loc *= BITS_PER_UNIT;
6807
6808 if (loc != 0)
6809 {
6810 if (op_const & 1)
6811 {
6812 unsigned HOST_WIDE_INT temp = opint[0];
6813 if (is_high)
6814 temp <<= loc;
6815 else
6816 temp >>= loc;
6817 opint[0] = temp;
6818 zap_const = 3;
6819 }
6820 }
6821 else
6822 zap_op = op;
6823 }
6824
6825 opint[1] = bytemask;
6826 return alpha_fold_builtin_zapnot (zap_op, opint, zap_const);
6827 }
6828
6829 /* Fold the builtins for the INS family of instructions. */
6830
6831 static tree
6832 alpha_fold_builtin_insxx (tree op[], unsigned HOST_WIDE_INT opint[],
6833 long op_const, unsigned HOST_WIDE_INT bytemask,
6834 bool is_high)
6835 {
6836 if ((op_const & 1) && opint[0] == 0)
6837 return build_int_cst (alpha_dimode_u, 0);
6838
6839 if (op_const & 2)
6840 {
6841 unsigned HOST_WIDE_INT temp, loc, byteloc;
6842 tree *zap_op = NULL;
6843
6844 loc = opint[1] & 7;
6845 bytemask <<= loc;
6846
6847 temp = opint[0];
6848 if (is_high)
6849 {
6850 byteloc = (64 - (loc * 8)) & 0x3f;
6851 if (byteloc == 0)
6852 zap_op = op;
6853 else
6854 temp >>= byteloc;
6855 bytemask >>= 8;
6856 }
6857 else
6858 {
6859 byteloc = loc * 8;
6860 if (byteloc == 0)
6861 zap_op = op;
6862 else
6863 temp <<= byteloc;
6864 }
6865
6866 opint[0] = temp;
6867 opint[1] = bytemask;
6868 return alpha_fold_builtin_zapnot (zap_op, opint, op_const);
6869 }
6870
6871 return NULL;
6872 }
6873
6874 static tree
6875 alpha_fold_builtin_mskxx (tree op[], unsigned HOST_WIDE_INT opint[],
6876 long op_const, unsigned HOST_WIDE_INT bytemask,
6877 bool is_high)
6878 {
6879 if (op_const & 2)
6880 {
6881 unsigned HOST_WIDE_INT loc;
6882
6883 loc = opint[1] & 7;
6884 bytemask <<= loc;
6885
6886 if (is_high)
6887 bytemask >>= 8;
6888
6889 opint[1] = bytemask ^ 0xff;
6890 }
6891
6892 return alpha_fold_builtin_zapnot (op, opint, op_const);
6893 }
6894
6895 static tree
6896 alpha_fold_vector_minmax (enum tree_code code, tree op[], tree vtype)
6897 {
6898 tree op0 = fold_convert (vtype, op[0]);
6899 tree op1 = fold_convert (vtype, op[1]);
6900 tree val = fold_build2 (code, vtype, op0, op1);
6901 return fold_build1 (VIEW_CONVERT_EXPR, alpha_dimode_u, val);
6902 }
6903
6904 static tree
6905 alpha_fold_builtin_perr (unsigned HOST_WIDE_INT opint[], long op_const)
6906 {
6907 unsigned HOST_WIDE_INT temp = 0;
6908 int i;
6909
6910 if (op_const != 3)
6911 return NULL;
6912
6913 for (i = 0; i < 8; ++i)
6914 {
6915 unsigned HOST_WIDE_INT a = (opint[0] >> (i * 8)) & 0xff;
6916 unsigned HOST_WIDE_INT b = (opint[1] >> (i * 8)) & 0xff;
6917 if (a >= b)
6918 temp += a - b;
6919 else
6920 temp += b - a;
6921 }
6922
6923 return build_int_cst (alpha_dimode_u, temp);
6924 }
6925
6926 static tree
6927 alpha_fold_builtin_pklb (unsigned HOST_WIDE_INT opint[], long op_const)
6928 {
6929 unsigned HOST_WIDE_INT temp;
6930
6931 if (op_const == 0)
6932 return NULL;
6933
6934 temp = opint[0] & 0xff;
6935 temp |= (opint[0] >> 24) & 0xff00;
6936
6937 return build_int_cst (alpha_dimode_u, temp);
6938 }
6939
6940 static tree
6941 alpha_fold_builtin_pkwb (unsigned HOST_WIDE_INT opint[], long op_const)
6942 {
6943 unsigned HOST_WIDE_INT temp;
6944
6945 if (op_const == 0)
6946 return NULL;
6947
6948 temp = opint[0] & 0xff;
6949 temp |= (opint[0] >> 8) & 0xff00;
6950 temp |= (opint[0] >> 16) & 0xff0000;
6951 temp |= (opint[0] >> 24) & 0xff000000;
6952
6953 return build_int_cst (alpha_dimode_u, temp);
6954 }
6955
6956 static tree
6957 alpha_fold_builtin_unpkbl (unsigned HOST_WIDE_INT opint[], long op_const)
6958 {
6959 unsigned HOST_WIDE_INT temp;
6960
6961 if (op_const == 0)
6962 return NULL;
6963
6964 temp = opint[0] & 0xff;
6965 temp |= (opint[0] & 0xff00) << 24;
6966
6967 return build_int_cst (alpha_dimode_u, temp);
6968 }
6969
6970 static tree
6971 alpha_fold_builtin_unpkbw (unsigned HOST_WIDE_INT opint[], long op_const)
6972 {
6973 unsigned HOST_WIDE_INT temp;
6974
6975 if (op_const == 0)
6976 return NULL;
6977
6978 temp = opint[0] & 0xff;
6979 temp |= (opint[0] & 0x0000ff00) << 8;
6980 temp |= (opint[0] & 0x00ff0000) << 16;
6981 temp |= (opint[0] & 0xff000000) << 24;
6982
6983 return build_int_cst (alpha_dimode_u, temp);
6984 }
6985
6986 static tree
6987 alpha_fold_builtin_cttz (unsigned HOST_WIDE_INT opint[], long op_const)
6988 {
6989 unsigned HOST_WIDE_INT temp;
6990
6991 if (op_const == 0)
6992 return NULL;
6993
6994 if (opint[0] == 0)
6995 temp = 64;
6996 else
6997 temp = exact_log2 (opint[0] & -opint[0]);
6998
6999 return build_int_cst (alpha_dimode_u, temp);
7000 }
7001
7002 static tree
7003 alpha_fold_builtin_ctlz (unsigned HOST_WIDE_INT opint[], long op_const)
7004 {
7005 unsigned HOST_WIDE_INT temp;
7006
7007 if (op_const == 0)
7008 return NULL;
7009
7010 if (opint[0] == 0)
7011 temp = 64;
7012 else
7013 temp = 64 - floor_log2 (opint[0]) - 1;
7014
7015 return build_int_cst (alpha_dimode_u, temp);
7016 }
7017
7018 static tree
7019 alpha_fold_builtin_ctpop (unsigned HOST_WIDE_INT opint[], long op_const)
7020 {
7021 unsigned HOST_WIDE_INT temp, op;
7022
7023 if (op_const == 0)
7024 return NULL;
7025
7026 op = opint[0];
7027 temp = 0;
7028 while (op)
7029 temp++, op &= op - 1;
7030
7031 return build_int_cst (alpha_dimode_u, temp);
7032 }
7033
7034 /* Fold one of our builtin functions. */
7035
7036 static tree
7037 alpha_fold_builtin (tree fndecl, int n_args, tree *op,
7038 bool ignore ATTRIBUTE_UNUSED)
7039 {
7040 unsigned HOST_WIDE_INT opint[MAX_ARGS];
7041 long op_const = 0;
7042 int i;
7043
7044 if (n_args > MAX_ARGS)
7045 return NULL;
7046
7047 for (i = 0; i < n_args; i++)
7048 {
7049 tree arg = op[i];
7050 if (arg == error_mark_node)
7051 return NULL;
7052
7053 opint[i] = 0;
7054 if (TREE_CODE (arg) == INTEGER_CST)
7055 {
7056 op_const |= 1L << i;
7057 opint[i] = int_cst_value (arg);
7058 }
7059 }
7060
7061 switch (DECL_MD_FUNCTION_CODE (fndecl))
7062 {
7063 case ALPHA_BUILTIN_CMPBGE:
7064 return alpha_fold_builtin_cmpbge (opint, op_const);
7065
7066 case ALPHA_BUILTIN_EXTBL:
7067 return alpha_fold_builtin_extxx (op, opint, op_const, 0x01, false);
7068 case ALPHA_BUILTIN_EXTWL:
7069 return alpha_fold_builtin_extxx (op, opint, op_const, 0x03, false);
7070 case ALPHA_BUILTIN_EXTLL:
7071 return alpha_fold_builtin_extxx (op, opint, op_const, 0x0f, false);
7072 case ALPHA_BUILTIN_EXTQL:
7073 return alpha_fold_builtin_extxx (op, opint, op_const, 0xff, false);
7074 case ALPHA_BUILTIN_EXTWH:
7075 return alpha_fold_builtin_extxx (op, opint, op_const, 0x03, true);
7076 case ALPHA_BUILTIN_EXTLH:
7077 return alpha_fold_builtin_extxx (op, opint, op_const, 0x0f, true);
7078 case ALPHA_BUILTIN_EXTQH:
7079 return alpha_fold_builtin_extxx (op, opint, op_const, 0xff, true);
7080
7081 case ALPHA_BUILTIN_INSBL:
7082 return alpha_fold_builtin_insxx (op, opint, op_const, 0x01, false);
7083 case ALPHA_BUILTIN_INSWL:
7084 return alpha_fold_builtin_insxx (op, opint, op_const, 0x03, false);
7085 case ALPHA_BUILTIN_INSLL:
7086 return alpha_fold_builtin_insxx (op, opint, op_const, 0x0f, false);
7087 case ALPHA_BUILTIN_INSQL:
7088 return alpha_fold_builtin_insxx (op, opint, op_const, 0xff, false);
7089 case ALPHA_BUILTIN_INSWH:
7090 return alpha_fold_builtin_insxx (op, opint, op_const, 0x03, true);
7091 case ALPHA_BUILTIN_INSLH:
7092 return alpha_fold_builtin_insxx (op, opint, op_const, 0x0f, true);
7093 case ALPHA_BUILTIN_INSQH:
7094 return alpha_fold_builtin_insxx (op, opint, op_const, 0xff, true);
7095
7096 case ALPHA_BUILTIN_MSKBL:
7097 return alpha_fold_builtin_mskxx (op, opint, op_const, 0x01, false);
7098 case ALPHA_BUILTIN_MSKWL:
7099 return alpha_fold_builtin_mskxx (op, opint, op_const, 0x03, false);
7100 case ALPHA_BUILTIN_MSKLL:
7101 return alpha_fold_builtin_mskxx (op, opint, op_const, 0x0f, false);
7102 case ALPHA_BUILTIN_MSKQL:
7103 return alpha_fold_builtin_mskxx (op, opint, op_const, 0xff, false);
7104 case ALPHA_BUILTIN_MSKWH:
7105 return alpha_fold_builtin_mskxx (op, opint, op_const, 0x03, true);
7106 case ALPHA_BUILTIN_MSKLH:
7107 return alpha_fold_builtin_mskxx (op, opint, op_const, 0x0f, true);
7108 case ALPHA_BUILTIN_MSKQH:
7109 return alpha_fold_builtin_mskxx (op, opint, op_const, 0xff, true);
7110
7111 case ALPHA_BUILTIN_ZAP:
7112 opint[1] ^= 0xff;
7113 /* FALLTHRU */
7114 case ALPHA_BUILTIN_ZAPNOT:
7115 return alpha_fold_builtin_zapnot (op, opint, op_const);
7116
7117 case ALPHA_BUILTIN_MINUB8:
7118 return alpha_fold_vector_minmax (MIN_EXPR, op, alpha_v8qi_u);
7119 case ALPHA_BUILTIN_MINSB8:
7120 return alpha_fold_vector_minmax (MIN_EXPR, op, alpha_v8qi_s);
7121 case ALPHA_BUILTIN_MINUW4:
7122 return alpha_fold_vector_minmax (MIN_EXPR, op, alpha_v4hi_u);
7123 case ALPHA_BUILTIN_MINSW4:
7124 return alpha_fold_vector_minmax (MIN_EXPR, op, alpha_v4hi_s);
7125 case ALPHA_BUILTIN_MAXUB8:
7126 return alpha_fold_vector_minmax (MAX_EXPR, op, alpha_v8qi_u);
7127 case ALPHA_BUILTIN_MAXSB8:
7128 return alpha_fold_vector_minmax (MAX_EXPR, op, alpha_v8qi_s);
7129 case ALPHA_BUILTIN_MAXUW4:
7130 return alpha_fold_vector_minmax (MAX_EXPR, op, alpha_v4hi_u);
7131 case ALPHA_BUILTIN_MAXSW4:
7132 return alpha_fold_vector_minmax (MAX_EXPR, op, alpha_v4hi_s);
7133
7134 case ALPHA_BUILTIN_PERR:
7135 return alpha_fold_builtin_perr (opint, op_const);
7136 case ALPHA_BUILTIN_PKLB:
7137 return alpha_fold_builtin_pklb (opint, op_const);
7138 case ALPHA_BUILTIN_PKWB:
7139 return alpha_fold_builtin_pkwb (opint, op_const);
7140 case ALPHA_BUILTIN_UNPKBL:
7141 return alpha_fold_builtin_unpkbl (opint, op_const);
7142 case ALPHA_BUILTIN_UNPKBW:
7143 return alpha_fold_builtin_unpkbw (opint, op_const);
7144
7145 case ALPHA_BUILTIN_CTTZ:
7146 return alpha_fold_builtin_cttz (opint, op_const);
7147 case ALPHA_BUILTIN_CTLZ:
7148 return alpha_fold_builtin_ctlz (opint, op_const);
7149 case ALPHA_BUILTIN_CTPOP:
7150 return alpha_fold_builtin_ctpop (opint, op_const);
7151
7152 case ALPHA_BUILTIN_AMASK:
7153 case ALPHA_BUILTIN_IMPLVER:
7154 case ALPHA_BUILTIN_RPCC:
7155 /* None of these are foldable at compile-time. */
7156 default:
7157 return NULL;
7158 }
7159 }
7160
7161 bool
7162 alpha_gimple_fold_builtin (gimple_stmt_iterator *gsi)
7163 {
7164 bool changed = false;
7165 gimple *stmt = gsi_stmt (*gsi);
7166 tree call = gimple_call_fn (stmt);
7167 gimple *new_stmt = NULL;
7168
7169 if (call)
7170 {
7171 tree fndecl = gimple_call_fndecl (stmt);
7172
7173 if (fndecl)
7174 {
7175 tree arg0, arg1;
7176
7177 switch (DECL_MD_FUNCTION_CODE (fndecl))
7178 {
7179 case ALPHA_BUILTIN_UMULH:
7180 arg0 = gimple_call_arg (stmt, 0);
7181 arg1 = gimple_call_arg (stmt, 1);
7182
7183 new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
7184 MULT_HIGHPART_EXPR, arg0, arg1);
7185 break;
7186 default:
7187 break;
7188 }
7189 }
7190 }
7191
7192 if (new_stmt)
7193 {
7194 gsi_replace (gsi, new_stmt, true);
7195 changed = true;
7196 }
7197
7198 return changed;
7199 }
7200
7201 /* This page contains routines that are used to determine what the function
7203 prologue and epilogue code will do and write them out. */
7204
7205 /* Compute the size of the save area in the stack. */
7206
7207 /* These variables are used for communication between the following functions.
7208 They indicate various things about the current function being compiled
7209 that are used to tell what kind of prologue, epilogue and procedure
7210 descriptor to generate. */
7211
7212 /* Nonzero if we need a stack procedure. */
7213 enum alpha_procedure_types {PT_NULL = 0, PT_REGISTER = 1, PT_STACK = 2};
7214 static enum alpha_procedure_types alpha_procedure_type;
7215
7216 /* Register number (either FP or SP) that is used to unwind the frame. */
7217 static int vms_unwind_regno;
7218
7219 /* Register number used to save FP. We need not have one for RA since
7220 we don't modify it for register procedures. This is only defined
7221 for register frame procedures. */
7222 static int vms_save_fp_regno;
7223
7224 /* Register number used to reference objects off our PV. */
7225 static int vms_base_regno;
7226
7227 /* Compute register masks for saved registers, register save area size,
7228 and total frame size. */
7229 static void
7230 alpha_compute_frame_layout (void)
7231 {
7232 unsigned HOST_WIDE_INT sa_mask = 0;
7233 HOST_WIDE_INT frame_size;
7234 int sa_size;
7235
7236 /* When outputting a thunk, we don't have valid register life info,
7237 but assemble_start_function wants to output .frame and .mask
7238 directives. */
7239 if (!cfun->is_thunk)
7240 {
7241 if (TARGET_ABI_OPEN_VMS && alpha_procedure_type == PT_STACK)
7242 sa_mask |= HOST_WIDE_INT_1U << HARD_FRAME_POINTER_REGNUM;
7243
7244 /* One for every register we have to save. */
7245 for (unsigned i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7246 if (! call_used_or_fixed_reg_p (i)
7247 && df_regs_ever_live_p (i) && i != REG_RA)
7248 sa_mask |= HOST_WIDE_INT_1U << i;
7249
7250 /* We need to restore these for the handler. */
7251 if (crtl->calls_eh_return)
7252 {
7253 for (unsigned i = 0; ; ++i)
7254 {
7255 unsigned regno = EH_RETURN_DATA_REGNO (i);
7256 if (regno == INVALID_REGNUM)
7257 break;
7258 sa_mask |= HOST_WIDE_INT_1U << regno;
7259 }
7260 }
7261
7262 /* If any register spilled, then spill the return address also. */
7263 /* ??? This is required by the Digital stack unwind specification
7264 and isn't needed if we're doing Dwarf2 unwinding. */
7265 if (sa_mask || alpha_ra_ever_killed ())
7266 sa_mask |= HOST_WIDE_INT_1U << REG_RA;
7267 }
7268
7269 sa_size = popcount_hwi(sa_mask);
7270 frame_size = get_frame_size ();
7271
7272 if (TARGET_ABI_OPEN_VMS)
7273 {
7274 /* Start with a stack procedure if we make any calls (REG_RA used), or
7275 need a frame pointer, with a register procedure if we otherwise need
7276 at least a slot, and with a null procedure in other cases. */
7277 if ((sa_mask >> REG_RA) & 1 || frame_pointer_needed)
7278 alpha_procedure_type = PT_STACK;
7279 else if (frame_size != 0)
7280 alpha_procedure_type = PT_REGISTER;
7281 else
7282 alpha_procedure_type = PT_NULL;
7283
7284 /* Don't reserve space for saving FP & RA yet. Do that later after we've
7285 made the final decision on stack procedure vs register procedure. */
7286 if (alpha_procedure_type == PT_STACK)
7287 sa_size -= 2;
7288
7289 /* Decide whether to refer to objects off our PV via FP or PV.
7290 If we need FP for something else or if we receive a nonlocal
7291 goto (which expects PV to contain the value), we must use PV.
7292 Otherwise, start by assuming we can use FP. */
7293
7294 vms_base_regno
7295 = (frame_pointer_needed
7296 || cfun->has_nonlocal_label
7297 || alpha_procedure_type == PT_STACK
7298 || crtl->outgoing_args_size)
7299 ? REG_PV : HARD_FRAME_POINTER_REGNUM;
7300
7301 /* If we want to copy PV into FP, we need to find some register
7302 in which to save FP. */
7303 vms_save_fp_regno = -1;
7304 if (vms_base_regno == HARD_FRAME_POINTER_REGNUM)
7305 for (unsigned i = 0; i < 32; i++)
7306 if (! fixed_regs[i] && call_used_or_fixed_reg_p (i)
7307 && ! df_regs_ever_live_p (i))
7308 {
7309 vms_save_fp_regno = i;
7310 break;
7311 }
7312
7313 /* A VMS condition handler requires a stack procedure in our
7314 implementation. (not required by the calling standard). */
7315 if ((vms_save_fp_regno == -1 && alpha_procedure_type == PT_REGISTER)
7316 || cfun->machine->uses_condition_handler)
7317 vms_base_regno = REG_PV, alpha_procedure_type = PT_STACK;
7318 else if (alpha_procedure_type == PT_NULL)
7319 vms_base_regno = REG_PV;
7320
7321 /* Stack unwinding should be done via FP unless we use it for PV. */
7322 vms_unwind_regno = (vms_base_regno == REG_PV
7323 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM);
7324
7325 /* If this is a stack procedure, allow space for saving FP, RA and
7326 a condition handler slot if needed. */
7327 if (alpha_procedure_type == PT_STACK)
7328 sa_size += 2 + cfun->machine->uses_condition_handler;
7329 }
7330 else
7331 {
7332 /* Our size must be even (multiple of 16 bytes). */
7333 if (sa_size & 1)
7334 sa_size++;
7335 }
7336 sa_size *= 8;
7337
7338 if (TARGET_ABI_OPEN_VMS)
7339 frame_size = ALPHA_ROUND (sa_size
7340 + (alpha_procedure_type == PT_STACK ? 8 : 0)
7341 + frame_size
7342 + crtl->args.pretend_args_size);
7343 else
7344 frame_size = (ALPHA_ROUND (crtl->outgoing_args_size)
7345 + sa_size
7346 + ALPHA_ROUND (frame_size + crtl->args.pretend_args_size));
7347
7348 cfun->machine->sa_mask = sa_mask;
7349 cfun->machine->sa_size = sa_size;
7350 cfun->machine->frame_size = frame_size;
7351 }
7352
7353 #undef TARGET_COMPUTE_FRAME_LAYOUT
7354 #define TARGET_COMPUTE_FRAME_LAYOUT alpha_compute_frame_layout
7355
7356 /* Return 1 if this function can directly return via $26. */
7357
7358 bool
7359 direct_return (void)
7360 {
7361 return (TARGET_ABI_OSF
7362 && reload_completed
7363 && cfun->machine->frame_size == 0);
7364 }
7365
7366 /* Define the offset between two registers, one to be eliminated,
7367 and the other its replacement, at the start of a routine. */
7368
7369 HOST_WIDE_INT
7370 alpha_initial_elimination_offset (unsigned int from,
7371 unsigned int to ATTRIBUTE_UNUSED)
7372 {
7373 HOST_WIDE_INT ret;
7374
7375 ret = cfun->machine->sa_size;
7376 ret += ALPHA_ROUND (crtl->outgoing_args_size);
7377
7378 switch (from)
7379 {
7380 case FRAME_POINTER_REGNUM:
7381 break;
7382
7383 case ARG_POINTER_REGNUM:
7384 ret += (ALPHA_ROUND (get_frame_size ()
7385 + crtl->args.pretend_args_size)
7386 - crtl->args.pretend_args_size);
7387 break;
7388
7389 default:
7390 gcc_unreachable ();
7391 }
7392
7393 return ret;
7394 }
7395
7396 #if TARGET_ABI_OPEN_VMS
7397
7398 /* Worker function for TARGET_CAN_ELIMINATE. */
7399
7400 static bool
7401 alpha_vms_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
7402 {
7403 switch (alpha_procedure_type)
7404 {
7405 case PT_NULL:
7406 /* NULL procedures have no frame of their own and we only
7407 know how to resolve from the current stack pointer. */
7408 return to == STACK_POINTER_REGNUM;
7409
7410 case PT_REGISTER:
7411 case PT_STACK:
7412 /* We always eliminate except to the stack pointer if there is no
7413 usable frame pointer at hand. */
7414 return (to != STACK_POINTER_REGNUM
7415 || vms_unwind_regno != HARD_FRAME_POINTER_REGNUM);
7416 }
7417
7418 gcc_unreachable ();
7419 }
7420
7421 /* FROM is to be eliminated for TO. Return the offset so that TO+offset
7422 designates the same location as FROM. */
7423
7424 HOST_WIDE_INT
7425 alpha_vms_initial_elimination_offset (unsigned int from, unsigned int to)
7426 {
7427 /* The only possible attempts we ever expect are ARG or FRAME_PTR to
7428 HARD_FRAME or STACK_PTR. We need the alpha_procedure_type to decide
7429 on the proper computations and will need the register save area size
7430 in most cases. */
7431
7432 HOST_WIDE_INT sa_size = cfun->machine->sa_size;
7433
7434 /* PT_NULL procedures have no frame of their own and we only allow
7435 elimination to the stack pointer. This is the argument pointer and we
7436 resolve the soft frame pointer to that as well. */
7437
7438 if (alpha_procedure_type == PT_NULL)
7439 return 0;
7440
7441 /* For a PT_STACK procedure the frame layout looks as follows
7442
7443 -----> decreasing addresses
7444
7445 < size rounded up to 16 | likewise >
7446 --------------#------------------------------+++--------------+++-------#
7447 incoming args # pretended args | "frame" | regs sa | PV | outgoing args #
7448 --------------#---------------------------------------------------------#
7449 ^ ^ ^ ^
7450 ARG_PTR FRAME_PTR HARD_FRAME_PTR STACK_PTR
7451
7452
7453 PT_REGISTER procedures are similar in that they may have a frame of their
7454 own. They have no regs-sa/pv/outgoing-args area.
7455
7456 We first compute offset to HARD_FRAME_PTR, then add what we need to get
7457 to STACK_PTR if need be. */
7458
7459 {
7460 HOST_WIDE_INT offset;
7461 HOST_WIDE_INT pv_save_size = alpha_procedure_type == PT_STACK ? 8 : 0;
7462
7463 switch (from)
7464 {
7465 case FRAME_POINTER_REGNUM:
7466 offset = ALPHA_ROUND (sa_size + pv_save_size);
7467 break;
7468 case ARG_POINTER_REGNUM:
7469 offset = (ALPHA_ROUND (sa_size + pv_save_size
7470 + get_frame_size ()
7471 + crtl->args.pretend_args_size)
7472 - crtl->args.pretend_args_size);
7473 break;
7474 default:
7475 gcc_unreachable ();
7476 }
7477
7478 if (to == STACK_POINTER_REGNUM)
7479 offset += ALPHA_ROUND (crtl->outgoing_args_size);
7480
7481 return offset;
7482 }
7483 }
7484
7485 #define COMMON_OBJECT "common_object"
7486
7487 static tree
7488 common_object_handler (tree *node, tree name ATTRIBUTE_UNUSED,
7489 tree args ATTRIBUTE_UNUSED, int flags ATTRIBUTE_UNUSED,
7490 bool *no_add_attrs ATTRIBUTE_UNUSED)
7491 {
7492 tree decl = *node;
7493 gcc_assert (DECL_P (decl));
7494
7495 DECL_COMMON (decl) = 1;
7496 return NULL_TREE;
7497 }
7498
7499 static const struct attribute_spec vms_attribute_table[] =
7500 {
7501 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
7502 affects_type_identity, handler, exclude } */
7503 { COMMON_OBJECT, 0, 1, true, false, false, false, common_object_handler,
7504 NULL },
7505 { NULL, 0, 0, false, false, false, false, NULL, NULL }
7506 };
7507
7508 void
7509 vms_output_aligned_decl_common(FILE *file, tree decl, const char *name,
7510 unsigned HOST_WIDE_INT size,
7511 unsigned int align)
7512 {
7513 tree attr = DECL_ATTRIBUTES (decl);
7514 fprintf (file, "%s", COMMON_ASM_OP);
7515 assemble_name (file, name);
7516 fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED, size);
7517 /* ??? Unlike on OSF/1, the alignment factor is not in log units. */
7518 fprintf (file, ",%u", align / BITS_PER_UNIT);
7519 if (attr)
7520 {
7521 attr = lookup_attribute (COMMON_OBJECT, attr);
7522 if (attr)
7523 fprintf (file, ",%s",
7524 IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (attr))));
7525 }
7526 fputc ('\n', file);
7527 }
7528
7529 #undef COMMON_OBJECT
7530
7531 #endif
7532
7533 bool
7534 alpha_find_lo_sum_using_gp (rtx insn)
7535 {
7536 subrtx_iterator::array_type array;
7537 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
7538 {
7539 const_rtx x = *iter;
7540 if (GET_CODE (x) == LO_SUM && XEXP (x, 0) == pic_offset_table_rtx)
7541 return true;
7542 }
7543 return false;
7544 }
7545
7546 static int
7547 alpha_does_function_need_gp (void)
7548 {
7549 rtx_insn *insn;
7550
7551 /* The GP being variable is an OSF abi thing. */
7552 if (! TARGET_ABI_OSF)
7553 return 0;
7554
7555 /* We need the gp to load the address of __mcount. */
7556 if (TARGET_PROFILING_NEEDS_GP && crtl->profile)
7557 return 1;
7558
7559 /* The code emitted by alpha_output_mi_thunk_osf uses the gp. */
7560 if (cfun->is_thunk)
7561 return 1;
7562
7563 /* The nonlocal receiver pattern assumes that the gp is valid for
7564 the nested function. Reasonable because it's almost always set
7565 correctly already. For the cases where that's wrong, make sure
7566 the nested function loads its gp on entry. */
7567 if (crtl->has_nonlocal_goto)
7568 return 1;
7569
7570 /* If we need a GP (we have a LDSYM insn or a CALL_INSN), load it first.
7571 Even if we are a static function, we still need to do this in case
7572 our address is taken and passed to something like qsort. */
7573
7574 push_topmost_sequence ();
7575 insn = get_insns ();
7576 pop_topmost_sequence ();
7577
7578 for (; insn; insn = NEXT_INSN (insn))
7579 if (NONDEBUG_INSN_P (insn)
7580 && GET_CODE (PATTERN (insn)) != USE
7581 && GET_CODE (PATTERN (insn)) != CLOBBER
7582 && get_attr_usegp (insn))
7583 return 1;
7584
7585 return 0;
7586 }
7587
7588 /* Helper function for alpha_store_data_bypass_p, handle just a single SET
7589 IN_SET. */
7590
7591 static bool
7592 alpha_store_data_bypass_p_1 (rtx_insn *out_insn, rtx in_set)
7593 {
7594 if (!MEM_P (SET_DEST (in_set)))
7595 return false;
7596
7597 rtx out_set = single_set (out_insn);
7598 if (out_set)
7599 return !reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_set));
7600
7601 rtx out_pat = PATTERN (out_insn);
7602 if (GET_CODE (out_pat) != PARALLEL)
7603 return false;
7604
7605 for (int i = 0; i < XVECLEN (out_pat, 0); i++)
7606 {
7607 rtx out_exp = XVECEXP (out_pat, 0, i);
7608
7609 if (GET_CODE (out_exp) == CLOBBER || GET_CODE (out_exp) == USE
7610 || GET_CODE (out_exp) == TRAP_IF)
7611 continue;
7612
7613 gcc_assert (GET_CODE (out_exp) == SET);
7614
7615 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_set)))
7616 return false;
7617 }
7618
7619 return true;
7620 }
7621
7622 /* True if the dependency between OUT_INSN and IN_INSN is on the store
7623 data not the address operand(s) of the store. IN_INSN and OUT_INSN
7624 must be either a single_set or a PARALLEL with SETs inside.
7625
7626 This alpha-specific version of store_data_bypass_p ignores TRAP_IF
7627 that would result in assertion failure (and internal compiler error)
7628 in the generic store_data_bypass_p function. */
7629
7630 int
7631 alpha_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
7632 {
7633 rtx in_set = single_set (in_insn);
7634 if (in_set)
7635 return alpha_store_data_bypass_p_1 (out_insn, in_set);
7636
7637 rtx in_pat = PATTERN (in_insn);
7638 if (GET_CODE (in_pat) != PARALLEL)
7639 return false;
7640
7641 for (int i = 0; i < XVECLEN (in_pat, 0); i++)
7642 {
7643 rtx in_exp = XVECEXP (in_pat, 0, i);
7644
7645 if (GET_CODE (in_exp) == CLOBBER || GET_CODE (in_exp) == USE
7646 || GET_CODE (in_exp) == TRAP_IF)
7647 continue;
7648
7649 gcc_assert (GET_CODE (in_exp) == SET);
7650
7651 if (!alpha_store_data_bypass_p_1 (out_insn, in_exp))
7652 return false;
7653 }
7654
7655 return true;
7656 }
7657
7658 /* Helper function to set RTX_FRAME_RELATED_P on instructions, including
7660 sequences. */
7661
7662 static rtx_insn *
7663 set_frame_related_p (void)
7664 {
7665 rtx_insn *seq = get_insns ();
7666 rtx_insn *insn;
7667
7668 end_sequence ();
7669
7670 if (!seq)
7671 return NULL;
7672
7673 if (INSN_P (seq))
7674 {
7675 insn = seq;
7676 while (insn != NULL_RTX)
7677 {
7678 RTX_FRAME_RELATED_P (insn) = 1;
7679 insn = NEXT_INSN (insn);
7680 }
7681 seq = emit_insn (seq);
7682 }
7683 else
7684 {
7685 seq = emit_insn (seq);
7686 RTX_FRAME_RELATED_P (seq) = 1;
7687 }
7688 return seq;
7689 }
7690
7691 #define FRP(exp) (start_sequence (), exp, set_frame_related_p ())
7692
7693 /* Generates a store with the proper unwind info attached. VALUE is
7694 stored at BASE_REG+BASE_OFS. If FRAME_BIAS is nonzero, then BASE_REG
7695 contains SP+FRAME_BIAS, and that is the unwind info that should be
7696 generated. If FRAME_REG != VALUE, then VALUE is being stored on
7697 behalf of FRAME_REG, and FRAME_REG should be present in the unwind. */
7698
7699 static void
7700 emit_frame_store_1 (rtx value, rtx base_reg, HOST_WIDE_INT frame_bias,
7701 HOST_WIDE_INT base_ofs, rtx frame_reg)
7702 {
7703 rtx addr, mem;
7704 rtx_insn *insn;
7705
7706 addr = plus_constant (Pmode, base_reg, base_ofs);
7707 mem = gen_frame_mem (DImode, addr);
7708
7709 insn = emit_move_insn (mem, value);
7710 RTX_FRAME_RELATED_P (insn) = 1;
7711
7712 if (frame_bias || value != frame_reg)
7713 {
7714 if (frame_bias)
7715 {
7716 addr = plus_constant (Pmode, stack_pointer_rtx,
7717 frame_bias + base_ofs);
7718 mem = gen_rtx_MEM (DImode, addr);
7719 }
7720
7721 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
7722 gen_rtx_SET (mem, frame_reg));
7723 }
7724 }
7725
7726 static void
7727 emit_frame_store (unsigned int regno, rtx base_reg,
7728 HOST_WIDE_INT frame_bias, HOST_WIDE_INT base_ofs)
7729 {
7730 rtx reg = gen_rtx_REG (DImode, regno);
7731 emit_frame_store_1 (reg, base_reg, frame_bias, base_ofs, reg);
7732 }
7733
7734 /* Write function prologue. */
7735
7736 /* On vms we have two kinds of functions:
7737
7738 - stack frame (PROC_STACK)
7739 these are 'normal' functions with local vars and which are
7740 calling other functions
7741 - register frame (PROC_REGISTER)
7742 keeps all data in registers, needs no stack
7743
7744 We must pass this to the assembler so it can generate the
7745 proper pdsc (procedure descriptor)
7746 This is done with the '.pdesc' command.
7747
7748 On not-vms, we don't really differentiate between the two, as we can
7749 simply allocate stack without saving registers. */
7750
7751 void
7752 alpha_expand_prologue (void)
7753 {
7754 /* Registers to save. */
7755 unsigned HOST_WIDE_INT sa_mask = cfun->machine->sa_mask;
7756 /* Stack space needed for pushing registers clobbered by us. */
7757 HOST_WIDE_INT sa_size = cfun->machine->sa_size;
7758 /* Complete stack size needed. */
7759 HOST_WIDE_INT frame_size = cfun->machine->frame_size;
7760 /* Probed stack size; it additionally includes the size of
7761 the "reserve region" if any. */
7762 HOST_WIDE_INT probed_size, sa_bias;
7763 /* Offset from base reg to register save area. */
7764 HOST_WIDE_INT reg_offset;
7765 rtx sa_reg;
7766
7767 if (flag_stack_usage_info)
7768 current_function_static_stack_size = frame_size;
7769
7770 if (TARGET_ABI_OPEN_VMS)
7771 reg_offset = 8 + 8 * cfun->machine->uses_condition_handler;
7772 else
7773 reg_offset = ALPHA_ROUND (crtl->outgoing_args_size);
7774
7775 /* Emit an insn to reload GP, if needed. */
7776 if (TARGET_ABI_OSF)
7777 {
7778 alpha_function_needs_gp = alpha_does_function_need_gp ();
7779 if (alpha_function_needs_gp)
7780 emit_insn (gen_prologue_ldgp ());
7781 }
7782
7783 /* TARGET_PROFILING_NEEDS_GP actually implies that we need to insert
7784 the call to mcount ourselves, rather than having the linker do it
7785 magically in response to -pg. Since _mcount has special linkage,
7786 don't represent the call as a call. */
7787 if (TARGET_PROFILING_NEEDS_GP && crtl->profile)
7788 emit_insn (gen_prologue_mcount ());
7789
7790 /* Adjust the stack by the frame size. If the frame size is > 4096
7791 bytes, we need to be sure we probe somewhere in the first and last
7792 4096 bytes (we can probably get away without the latter test) and
7793 every 8192 bytes in between. If the frame size is > 32768, we
7794 do this in a loop. Otherwise, we generate the explicit probe
7795 instructions.
7796
7797 Note that we are only allowed to adjust sp once in the prologue. */
7798
7799 probed_size = frame_size;
7800 if (flag_stack_check || flag_stack_clash_protection)
7801 probed_size += get_stack_check_protect ();
7802
7803 if (probed_size <= 32768)
7804 {
7805 if (probed_size > 4096)
7806 {
7807 int probed;
7808
7809 for (probed = 4096; probed < probed_size; probed += 8192)
7810 emit_insn (gen_stack_probe_internal (GEN_INT (-probed)));
7811
7812 /* We only have to do this probe if we aren't saving registers or
7813 if we are probing beyond the frame because of -fstack-check. */
7814 if ((sa_size == 0 && probed_size > probed - 4096)
7815 || flag_stack_check || flag_stack_clash_protection)
7816 emit_insn (gen_stack_probe_internal (GEN_INT (-probed_size)));
7817 }
7818
7819 if (frame_size != 0)
7820 FRP (emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
7821 GEN_INT (-frame_size))));
7822 }
7823 else
7824 {
7825 /* Here we generate code to set R22 to SP + 4096 and set R23 to the
7826 number of 8192 byte blocks to probe. We then probe each block
7827 in the loop and then set SP to the proper location. If the
7828 amount remaining is > 4096, we have to do one more probe if we
7829 are not saving any registers or if we are probing beyond the
7830 frame because of -fstack-check. */
7831
7832 HOST_WIDE_INT blocks = (probed_size + 4096) / 8192;
7833 HOST_WIDE_INT leftover = probed_size + 4096 - blocks * 8192;
7834 rtx ptr = gen_rtx_REG (DImode, 22);
7835 rtx count = gen_rtx_REG (DImode, 23);
7836 rtx seq;
7837
7838 emit_move_insn (count, GEN_INT (blocks));
7839 emit_insn (gen_adddi3 (ptr, stack_pointer_rtx, GEN_INT (4096)));
7840
7841 /* Because of the difficulty in emitting a new basic block this
7842 late in the compilation, generate the loop as a single insn. */
7843 emit_insn (gen_prologue_stack_probe_loop (count, ptr));
7844
7845 if ((leftover > 4096 && sa_size == 0)
7846 || flag_stack_check || flag_stack_clash_protection)
7847 {
7848 rtx last = gen_rtx_MEM (DImode,
7849 plus_constant (Pmode, ptr, -leftover));
7850 MEM_VOLATILE_P (last) = 1;
7851 emit_move_insn (last, const0_rtx);
7852 }
7853
7854 if (flag_stack_check || flag_stack_clash_protection)
7855 {
7856 /* If -fstack-check is specified we have to load the entire
7857 constant into a register and subtract from the sp in one go,
7858 because the probed stack size is not equal to the frame size. */
7859 HOST_WIDE_INT lo, hi;
7860 lo = ((frame_size & 0xffff) ^ 0x8000) - 0x8000;
7861 hi = frame_size - lo;
7862
7863 emit_move_insn (ptr, GEN_INT (hi));
7864 emit_insn (gen_adddi3 (ptr, ptr, GEN_INT (lo)));
7865 seq = emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx,
7866 ptr));
7867 }
7868 else
7869 {
7870 seq = emit_insn (gen_adddi3 (stack_pointer_rtx, ptr,
7871 GEN_INT (-leftover)));
7872 }
7873
7874 /* This alternative is special, because the DWARF code cannot
7875 possibly intuit through the loop above. So we invent this
7876 note it looks at instead. */
7877 RTX_FRAME_RELATED_P (seq) = 1;
7878 add_reg_note (seq, REG_FRAME_RELATED_EXPR,
7879 gen_rtx_SET (stack_pointer_rtx,
7880 plus_constant (Pmode, stack_pointer_rtx,
7881 -frame_size)));
7882 }
7883
7884 /* Cope with very large offsets to the register save area. */
7885 sa_bias = 0;
7886 sa_reg = stack_pointer_rtx;
7887 if (reg_offset + sa_size > 0x8000)
7888 {
7889 int low = ((reg_offset & 0xffff) ^ 0x8000) - 0x8000;
7890 rtx sa_bias_rtx;
7891
7892 if (low + sa_size <= 0x8000)
7893 sa_bias = reg_offset - low, reg_offset = low;
7894 else
7895 sa_bias = reg_offset, reg_offset = 0;
7896
7897 sa_reg = gen_rtx_REG (DImode, 24);
7898 sa_bias_rtx = GEN_INT (sa_bias);
7899
7900 if (add_operand (sa_bias_rtx, DImode))
7901 emit_insn (gen_adddi3 (sa_reg, stack_pointer_rtx, sa_bias_rtx));
7902 else
7903 {
7904 emit_move_insn (sa_reg, sa_bias_rtx);
7905 emit_insn (gen_adddi3 (sa_reg, stack_pointer_rtx, sa_reg));
7906 }
7907 }
7908
7909 /* Save regs in stack order. Beginning with VMS PV. */
7910 if (TARGET_ABI_OPEN_VMS && alpha_procedure_type == PT_STACK)
7911 emit_frame_store (REG_PV, stack_pointer_rtx, 0, 0);
7912
7913 /* Save register RA next, followed by any other registers
7914 that need to be saved. */
7915 for (unsigned i = REG_RA; sa_mask != 0; i = ctz_hwi(sa_mask))
7916 {
7917 emit_frame_store (i, sa_reg, sa_bias, reg_offset);
7918 reg_offset += 8;
7919 sa_mask &= ~(HOST_WIDE_INT_1U << i);
7920 }
7921
7922 if (TARGET_ABI_OPEN_VMS)
7923 {
7924 /* Register frame procedures save the fp. */
7925 if (alpha_procedure_type == PT_REGISTER)
7926 {
7927 rtx_insn *insn =
7928 emit_move_insn (gen_rtx_REG (DImode, vms_save_fp_regno),
7929 hard_frame_pointer_rtx);
7930 add_reg_note (insn, REG_CFA_REGISTER, NULL);
7931 RTX_FRAME_RELATED_P (insn) = 1;
7932 }
7933
7934 if (alpha_procedure_type != PT_NULL && vms_base_regno != REG_PV)
7935 emit_insn (gen_force_movdi (gen_rtx_REG (DImode, vms_base_regno),
7936 gen_rtx_REG (DImode, REG_PV)));
7937
7938 if (alpha_procedure_type != PT_NULL
7939 && vms_unwind_regno == HARD_FRAME_POINTER_REGNUM)
7940 FRP (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx));
7941
7942 /* If we have to allocate space for outgoing args, do it now. */
7943 if (crtl->outgoing_args_size != 0)
7944 {
7945 rtx_insn *seq
7946 = emit_move_insn (stack_pointer_rtx,
7947 plus_constant
7948 (Pmode, hard_frame_pointer_rtx,
7949 - (ALPHA_ROUND
7950 (crtl->outgoing_args_size))));
7951
7952 /* Only set FRAME_RELATED_P on the stack adjustment we just emitted
7953 if ! frame_pointer_needed. Setting the bit will change the CFA
7954 computation rule to use sp again, which would be wrong if we had
7955 frame_pointer_needed, as this means sp might move unpredictably
7956 later on.
7957
7958 Also, note that
7959 frame_pointer_needed
7960 => vms_unwind_regno == HARD_FRAME_POINTER_REGNUM
7961 and
7962 crtl->outgoing_args_size != 0
7963 => alpha_procedure_type != PT_NULL,
7964
7965 so when we are not setting the bit here, we are guaranteed to
7966 have emitted an FRP frame pointer update just before. */
7967 RTX_FRAME_RELATED_P (seq) = ! frame_pointer_needed;
7968 }
7969 }
7970 else
7971 {
7972 /* If we need a frame pointer, set it from the stack pointer. */
7973 if (frame_pointer_needed)
7974 {
7975 if (TARGET_CAN_FAULT_IN_PROLOGUE)
7976 FRP (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx));
7977 else
7978 /* This must always be the last instruction in the
7979 prologue, thus we emit a special move + clobber. */
7980 FRP (emit_insn (gen_init_fp (hard_frame_pointer_rtx,
7981 stack_pointer_rtx, sa_reg)));
7982 }
7983 }
7984
7985 /* The ABIs for VMS and OSF/1 say that while we can schedule insns into
7986 the prologue, for exception handling reasons, we cannot do this for
7987 any insn that might fault. We could prevent this for mems with a
7988 (clobber:BLK (scratch)), but this doesn't work for fp insns. So we
7989 have to prevent all such scheduling with a blockage.
7990
7991 Linux, on the other hand, never bothered to implement OSF/1's
7992 exception handling, and so doesn't care about such things. Anyone
7993 planning to use dwarf2 frame-unwind info can also omit the blockage. */
7994
7995 if (! TARGET_CAN_FAULT_IN_PROLOGUE)
7996 emit_insn (gen_blockage ());
7997 }
7998
7999 /* Count the number of .file directives, so that .loc is up to date. */
8000 int num_source_filenames = 0;
8001
8002 /* Output the textual info surrounding the prologue. */
8003
8004 void
8005 alpha_start_function (FILE *file, const char *fnname,
8006 tree decl ATTRIBUTE_UNUSED)
8007 {
8008 unsigned long imask, fmask;
8009 /* Complete stack size needed. */
8010 HOST_WIDE_INT frame_size = cfun->machine->frame_size;
8011 /* The maximum debuggable frame size. */
8012 const HOST_WIDE_INT max_frame_size = HOST_WIDE_INT_1 << 31;
8013 /* Offset from base reg to register save area. */
8014 HOST_WIDE_INT reg_offset;
8015 char *entry_label = (char *) alloca (strlen (fnname) + 6);
8016 char *tramp_label = (char *) alloca (strlen (fnname) + 6);
8017 int i;
8018
8019 #if TARGET_ABI_OPEN_VMS
8020 vms_start_function (fnname);
8021 #endif
8022
8023 alpha_fnname = fnname;
8024
8025 if (TARGET_ABI_OPEN_VMS)
8026 reg_offset = 8 + 8 * cfun->machine->uses_condition_handler;
8027 else
8028 reg_offset = ALPHA_ROUND (crtl->outgoing_args_size);
8029
8030 imask = cfun->machine->sa_mask & 0xffffffffu;
8031 fmask = cfun->machine->sa_mask >> 32;
8032
8033 /* Issue function start and label. */
8034 if (TARGET_ABI_OPEN_VMS || !flag_inhibit_size_directive)
8035 {
8036 fputs ("\t.ent ", file);
8037 assemble_name (file, fnname);
8038 putc ('\n', file);
8039
8040 /* If the function needs GP, we'll write the "..ng" label there.
8041 Otherwise, do it here. */
8042 if (TARGET_ABI_OSF
8043 && ! alpha_function_needs_gp
8044 && ! cfun->is_thunk)
8045 {
8046 putc ('$', file);
8047 assemble_name (file, fnname);
8048 fputs ("..ng:\n", file);
8049 }
8050 }
8051 /* Nested functions on VMS that are potentially called via trampoline
8052 get a special transfer entry point that loads the called functions
8053 procedure descriptor and static chain. */
8054 if (TARGET_ABI_OPEN_VMS
8055 && !TREE_PUBLIC (decl)
8056 && DECL_CONTEXT (decl)
8057 && !TYPE_P (DECL_CONTEXT (decl))
8058 && TREE_CODE (DECL_CONTEXT (decl)) != TRANSLATION_UNIT_DECL)
8059 {
8060 strcpy (tramp_label, fnname);
8061 strcat (tramp_label, "..tr");
8062 ASM_OUTPUT_LABEL (file, tramp_label);
8063 fprintf (file, "\tldq $1,24($27)\n");
8064 fprintf (file, "\tldq $27,16($27)\n");
8065 }
8066
8067 strcpy (entry_label, fnname);
8068 if (TARGET_ABI_OPEN_VMS)
8069 strcat (entry_label, "..en");
8070
8071 ASM_OUTPUT_LABEL (file, entry_label);
8072 inside_function = TRUE;
8073
8074 if (TARGET_ABI_OPEN_VMS)
8075 fprintf (file, "\t.base $%d\n", vms_base_regno);
8076
8077 if (TARGET_ABI_OSF
8078 && TARGET_IEEE_CONFORMANT
8079 && !flag_inhibit_size_directive)
8080 {
8081 /* Set flags in procedure descriptor to request IEEE-conformant
8082 math-library routines. The value we set it to is PDSC_EXC_IEEE
8083 (/usr/include/pdsc.h). */
8084 fputs ("\t.eflag 48\n", file);
8085 }
8086
8087 /* Set up offsets to alpha virtual arg/local debugging pointer. */
8088 alpha_auto_offset = -frame_size + crtl->args.pretend_args_size;
8089 alpha_arg_offset = -frame_size + 48;
8090
8091 /* Describe our frame. If the frame size is larger than an integer,
8092 print it as zero to avoid an assembler error. We won't be
8093 properly describing such a frame, but that's the best we can do. */
8094 if (TARGET_ABI_OPEN_VMS)
8095 fprintf (file, "\t.frame $%d," HOST_WIDE_INT_PRINT_DEC ",$26,"
8096 HOST_WIDE_INT_PRINT_DEC "\n",
8097 vms_unwind_regno,
8098 frame_size >= max_frame_size ? 0 : frame_size,
8099 reg_offset);
8100 else if (!flag_inhibit_size_directive)
8101 fprintf (file, "\t.frame $%d," HOST_WIDE_INT_PRINT_DEC ",$26,%d\n",
8102 (frame_pointer_needed
8103 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM),
8104 frame_size >= max_frame_size ? 0 : frame_size,
8105 crtl->args.pretend_args_size);
8106
8107 /* Describe which registers were spilled. */
8108 if (TARGET_ABI_OPEN_VMS)
8109 {
8110 if (imask)
8111 /* ??? Does VMS care if mask contains ra? The old code didn't
8112 set it, so I don't here. */
8113 fprintf (file, "\t.mask 0x%lx,0\n", imask & ~(1UL << REG_RA));
8114 if (fmask)
8115 fprintf (file, "\t.fmask 0x%lx,0\n", fmask);
8116 if (alpha_procedure_type == PT_REGISTER)
8117 fprintf (file, "\t.fp_save $%d\n", vms_save_fp_regno);
8118 }
8119 else if (!flag_inhibit_size_directive)
8120 {
8121 if (imask)
8122 {
8123 fprintf (file, "\t.mask 0x%lx," HOST_WIDE_INT_PRINT_DEC "\n", imask,
8124 frame_size >= max_frame_size ? 0 : reg_offset - frame_size);
8125
8126 for (i = 0; i < 32; ++i)
8127 if (imask & (1UL << i))
8128 reg_offset += 8;
8129 }
8130
8131 if (fmask)
8132 fprintf (file, "\t.fmask 0x%lx," HOST_WIDE_INT_PRINT_DEC "\n", fmask,
8133 frame_size >= max_frame_size ? 0 : reg_offset - frame_size);
8134 }
8135
8136 #if TARGET_ABI_OPEN_VMS
8137 /* If a user condition handler has been installed at some point, emit
8138 the procedure descriptor bits to point the Condition Handling Facility
8139 at the indirection wrapper, and state the fp offset at which the user
8140 handler may be found. */
8141 if (cfun->machine->uses_condition_handler)
8142 {
8143 fprintf (file, "\t.handler __gcc_shell_handler\n");
8144 fprintf (file, "\t.handler_data %d\n", VMS_COND_HANDLER_FP_OFFSET);
8145 }
8146
8147 #ifdef TARGET_VMS_CRASH_DEBUG
8148 /* Support of minimal traceback info. */
8149 switch_to_section (readonly_data_section);
8150 fprintf (file, "\t.align 3\n");
8151 assemble_name (file, fnname); fputs ("..na:\n", file);
8152 fputs ("\t.ascii \"", file);
8153 assemble_name (file, fnname);
8154 fputs ("\\0\"\n", file);
8155 switch_to_section (text_section);
8156 #endif
8157 #endif /* TARGET_ABI_OPEN_VMS */
8158 }
8159
8160 /* Emit the .prologue note at the scheduled end of the prologue. */
8161
8162 static void
8163 alpha_output_function_end_prologue (FILE *file)
8164 {
8165 if (TARGET_ABI_OPEN_VMS)
8166 fputs ("\t.prologue\n", file);
8167 else if (!flag_inhibit_size_directive)
8168 fprintf (file, "\t.prologue %d\n",
8169 alpha_function_needs_gp || cfun->is_thunk);
8170 }
8171
8172 /* Write function epilogue. */
8173
8174 void
8175 alpha_expand_epilogue (void)
8176 {
8177 /* Registers to save. */
8178 unsigned HOST_WIDE_INT sa_mask = cfun->machine->sa_mask;
8179 /* Stack space needed for pushing registers clobbered by us. */
8180 HOST_WIDE_INT sa_size = cfun->machine->sa_size;
8181 /* Complete stack size needed. */
8182 HOST_WIDE_INT frame_size = cfun->machine->frame_size;
8183 /* Offset from base reg to register save area. */
8184 HOST_WIDE_INT reg_offset;
8185 int fp_is_frame_pointer, fp_offset;
8186 rtx sa_reg, sa_reg_exp = NULL;
8187 rtx sp_adj1, sp_adj2, mem, reg, insn;
8188 rtx eh_ofs;
8189 rtx cfa_restores = NULL_RTX;
8190
8191 if (TARGET_ABI_OPEN_VMS)
8192 {
8193 if (alpha_procedure_type == PT_STACK)
8194 reg_offset = 8 + 8 * cfun->machine->uses_condition_handler;
8195 else
8196 reg_offset = 0;
8197 }
8198 else
8199 reg_offset = ALPHA_ROUND (crtl->outgoing_args_size);
8200
8201 fp_is_frame_pointer
8202 = (TARGET_ABI_OPEN_VMS
8203 ? alpha_procedure_type == PT_STACK
8204 : frame_pointer_needed);
8205 fp_offset = 0;
8206 sa_reg = stack_pointer_rtx;
8207
8208 if (crtl->calls_eh_return)
8209 eh_ofs = EH_RETURN_STACKADJ_RTX;
8210 else
8211 eh_ofs = NULL_RTX;
8212
8213 if (sa_size)
8214 {
8215 /* If we have a frame pointer, restore SP from it. */
8216 if (TARGET_ABI_OPEN_VMS
8217 ? vms_unwind_regno == HARD_FRAME_POINTER_REGNUM
8218 : frame_pointer_needed)
8219 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
8220
8221 /* Cope with very large offsets to the register save area. */
8222 if (reg_offset + sa_size > 0x8000)
8223 {
8224 int low = ((reg_offset & 0xffff) ^ 0x8000) - 0x8000;
8225 HOST_WIDE_INT bias;
8226
8227 if (low + sa_size <= 0x8000)
8228 bias = reg_offset - low, reg_offset = low;
8229 else
8230 bias = reg_offset, reg_offset = 0;
8231
8232 sa_reg = gen_rtx_REG (DImode, 22);
8233 sa_reg_exp = plus_constant (Pmode, stack_pointer_rtx, bias);
8234
8235 emit_move_insn (sa_reg, sa_reg_exp);
8236 }
8237
8238 /* Restore registers in order, excepting a true frame pointer. */
8239 for (unsigned i = REG_RA; sa_mask != 0; i = ctz_hwi(sa_mask))
8240 {
8241 if (i == HARD_FRAME_POINTER_REGNUM && fp_is_frame_pointer)
8242 fp_offset = reg_offset;
8243 else
8244 {
8245 mem = gen_frame_mem (DImode,
8246 plus_constant (Pmode, sa_reg,
8247 reg_offset));
8248 reg = gen_rtx_REG (DImode, i);
8249 emit_move_insn (reg, mem);
8250 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
8251 cfa_restores);
8252 }
8253 reg_offset += 8;
8254 sa_mask &= ~(HOST_WIDE_INT_1U << i);
8255 }
8256 }
8257
8258 if (frame_size || eh_ofs)
8259 {
8260 sp_adj1 = stack_pointer_rtx;
8261
8262 if (eh_ofs)
8263 {
8264 sp_adj1 = gen_rtx_REG (DImode, 23);
8265 emit_move_insn (sp_adj1,
8266 gen_rtx_PLUS (Pmode, stack_pointer_rtx, eh_ofs));
8267 }
8268
8269 /* If the stack size is large, begin computation into a temporary
8270 register so as not to interfere with a potential fp restore,
8271 which must be consecutive with an SP restore. */
8272 if (frame_size < 32768 && !cfun->calls_alloca)
8273 sp_adj2 = GEN_INT (frame_size);
8274 else if (frame_size < 0x40007fffL)
8275 {
8276 int low = ((frame_size & 0xffff) ^ 0x8000) - 0x8000;
8277
8278 sp_adj2 = plus_constant (Pmode, sp_adj1, frame_size - low);
8279 if (sa_reg_exp && rtx_equal_p (sa_reg_exp, sp_adj2))
8280 sp_adj1 = sa_reg;
8281 else
8282 {
8283 sp_adj1 = gen_rtx_REG (DImode, 23);
8284 emit_move_insn (sp_adj1, sp_adj2);
8285 }
8286 sp_adj2 = GEN_INT (low);
8287 }
8288 else
8289 {
8290 rtx tmp = gen_rtx_REG (DImode, 23);
8291 sp_adj2 = alpha_emit_set_const (tmp, DImode, frame_size, 3, false);
8292 if (!sp_adj2)
8293 {
8294 /* We can't drop new things to memory this late, afaik,
8295 so build it up by pieces. */
8296 sp_adj2 = alpha_emit_set_long_const (tmp, frame_size);
8297 gcc_assert (sp_adj2);
8298 }
8299 }
8300
8301 /* From now on, things must be in order. So emit blockages. */
8302
8303 /* Restore the frame pointer. */
8304 if (fp_is_frame_pointer)
8305 {
8306 emit_insn (gen_blockage ());
8307 mem = gen_frame_mem (DImode, plus_constant (Pmode, sa_reg,
8308 fp_offset));
8309 emit_move_insn (hard_frame_pointer_rtx, mem);
8310 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
8311 hard_frame_pointer_rtx, cfa_restores);
8312 }
8313 else if (TARGET_ABI_OPEN_VMS)
8314 {
8315 emit_insn (gen_blockage ());
8316 emit_move_insn (hard_frame_pointer_rtx,
8317 gen_rtx_REG (DImode, vms_save_fp_regno));
8318 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
8319 hard_frame_pointer_rtx, cfa_restores);
8320 }
8321
8322 /* Restore the stack pointer. */
8323 emit_insn (gen_blockage ());
8324 if (sp_adj2 == const0_rtx)
8325 insn = emit_move_insn (stack_pointer_rtx, sp_adj1);
8326 else
8327 insn = emit_move_insn (stack_pointer_rtx,
8328 gen_rtx_PLUS (DImode, sp_adj1, sp_adj2));
8329 REG_NOTES (insn) = cfa_restores;
8330 add_reg_note (insn, REG_CFA_DEF_CFA, stack_pointer_rtx);
8331 RTX_FRAME_RELATED_P (insn) = 1;
8332 }
8333 else
8334 {
8335 gcc_assert (cfa_restores == NULL);
8336
8337 if (TARGET_ABI_OPEN_VMS && alpha_procedure_type == PT_REGISTER)
8338 {
8339 emit_insn (gen_blockage ());
8340 insn = emit_move_insn (hard_frame_pointer_rtx,
8341 gen_rtx_REG (DImode, vms_save_fp_regno));
8342 add_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx);
8343 RTX_FRAME_RELATED_P (insn) = 1;
8344 }
8345 }
8346 }
8347
8348 /* Output the rest of the textual info surrounding the epilogue. */
8350
8351 void
8352 alpha_end_function (FILE *file, const char *fnname, tree decl ATTRIBUTE_UNUSED)
8353 {
8354 rtx_insn *insn;
8355
8356 /* We output a nop after noreturn calls at the very end of the function to
8357 ensure that the return address always remains in the caller's code range,
8358 as not doing so might confuse unwinding engines. */
8359 insn = get_last_insn ();
8360 if (!INSN_P (insn))
8361 insn = prev_active_insn (insn);
8362 if (insn && CALL_P (insn))
8363 output_asm_insn (get_insn_template (CODE_FOR_nop, NULL), NULL);
8364
8365 #if TARGET_ABI_OPEN_VMS
8366 /* Write the linkage entries. */
8367 alpha_write_linkage (file, fnname);
8368 #endif
8369
8370 /* End the function. */
8371 if (TARGET_ABI_OPEN_VMS
8372 || !flag_inhibit_size_directive)
8373 {
8374 fputs ("\t.end ", file);
8375 assemble_name (file, fnname);
8376 putc ('\n', file);
8377 }
8378 inside_function = FALSE;
8379 }
8380
8381 #if TARGET_ABI_OSF
8382 /* Emit a tail call to FUNCTION after adjusting THIS by DELTA.
8383
8384 In order to avoid the hordes of differences between generated code
8385 with and without TARGET_EXPLICIT_RELOCS, and to avoid duplicating
8386 lots of code loading up large constants, generate rtl and emit it
8387 instead of going straight to text.
8388
8389 Not sure why this idea hasn't been explored before... */
8390
8391 static void
8392 alpha_output_mi_thunk_osf (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
8393 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
8394 tree function)
8395 {
8396 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
8397 HOST_WIDE_INT hi, lo;
8398 rtx this_rtx, funexp;
8399 rtx_insn *insn;
8400
8401 /* We always require a valid GP. */
8402 emit_insn (gen_prologue_ldgp ());
8403 emit_note (NOTE_INSN_PROLOGUE_END);
8404
8405 /* Find the "this" pointer. If the function returns a structure,
8406 the structure return pointer is in $16. */
8407 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
8408 this_rtx = gen_rtx_REG (Pmode, 17);
8409 else
8410 this_rtx = gen_rtx_REG (Pmode, 16);
8411
8412 /* Add DELTA. When possible we use ldah+lda. Otherwise load the
8413 entire constant for the add. */
8414 lo = ((delta & 0xffff) ^ 0x8000) - 0x8000;
8415 hi = (((delta - lo) & 0xffffffff) ^ 0x80000000) - 0x80000000;
8416 if (hi + lo == delta)
8417 {
8418 if (hi)
8419 emit_insn (gen_adddi3 (this_rtx, this_rtx, GEN_INT (hi)));
8420 if (lo)
8421 emit_insn (gen_adddi3 (this_rtx, this_rtx, GEN_INT (lo)));
8422 }
8423 else
8424 {
8425 rtx tmp = alpha_emit_set_long_const (gen_rtx_REG (Pmode, 0), delta);
8426 emit_insn (gen_adddi3 (this_rtx, this_rtx, tmp));
8427 }
8428
8429 /* Add a delta stored in the vtable at VCALL_OFFSET. */
8430 if (vcall_offset)
8431 {
8432 rtx tmp, tmp2;
8433
8434 tmp = gen_rtx_REG (Pmode, 0);
8435 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
8436
8437 lo = ((vcall_offset & 0xffff) ^ 0x8000) - 0x8000;
8438 hi = (((vcall_offset - lo) & 0xffffffff) ^ 0x80000000) - 0x80000000;
8439 if (hi + lo == vcall_offset)
8440 {
8441 if (hi)
8442 emit_insn (gen_adddi3 (tmp, tmp, GEN_INT (hi)));
8443 }
8444 else
8445 {
8446 tmp2 = alpha_emit_set_long_const (gen_rtx_REG (Pmode, 1),
8447 vcall_offset);
8448 emit_insn (gen_adddi3 (tmp, tmp, tmp2));
8449 lo = 0;
8450 }
8451 if (lo)
8452 tmp2 = gen_rtx_PLUS (Pmode, tmp, GEN_INT (lo));
8453 else
8454 tmp2 = tmp;
8455 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp2));
8456
8457 emit_insn (gen_adddi3 (this_rtx, this_rtx, tmp));
8458 }
8459
8460 /* Generate a tail call to the target function. */
8461 if (! TREE_USED (function))
8462 {
8463 assemble_external (function);
8464 TREE_USED (function) = 1;
8465 }
8466 funexp = XEXP (DECL_RTL (function), 0);
8467 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
8468 insn = emit_call_insn (gen_sibcall (funexp, const0_rtx));
8469 SIBLING_CALL_P (insn) = 1;
8470
8471 /* Run just enough of rest_of_compilation to get the insns emitted.
8472 There's not really enough bulk here to make other passes such as
8473 instruction scheduling worth while. */
8474 insn = get_insns ();
8475 shorten_branches (insn);
8476 assemble_start_function (thunk_fndecl, fnname);
8477 final_start_function (insn, file, 1);
8478 final (insn, file, 1);
8479 final_end_function ();
8480 assemble_end_function (thunk_fndecl, fnname);
8481 }
8482 #endif /* TARGET_ABI_OSF */
8483
8484 /* Debugging support. */
8486
8487 #include "gstab.h"
8488
8489 /* Name of the file containing the current function. */
8490
8491 static const char *current_function_file = "";
8492
8493 /* Offsets to alpha virtual arg/local debugging pointers. */
8494
8495 long alpha_arg_offset;
8496 long alpha_auto_offset;
8497
8498 /* Emit a new filename to a stream. */
8500
8501 void
8502 alpha_output_filename (FILE *stream, const char *name)
8503 {
8504 static int first_time = TRUE;
8505
8506 if (first_time)
8507 {
8508 first_time = FALSE;
8509 ++num_source_filenames;
8510 current_function_file = name;
8511 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8512 output_quoted_string (stream, name);
8513 fprintf (stream, "\n");
8514 }
8515
8516 else if (name != current_function_file
8517 && strcmp (name, current_function_file) != 0)
8518 {
8519 ++num_source_filenames;
8520 current_function_file = name;
8521 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8522
8523 output_quoted_string (stream, name);
8524 fprintf (stream, "\n");
8525 }
8526 }
8527
8528 /* Structure to show the current status of registers and memory. */
8530
8531 struct shadow_summary
8532 {
8533 struct {
8534 unsigned int i : 31; /* Mask of int regs */
8535 unsigned int fp : 31; /* Mask of fp regs */
8536 unsigned int mem : 1; /* mem == imem | fpmem */
8537 } used, defd;
8538 };
8539
8540 /* Summary the effects of expression X on the machine. Update SUM, a pointer
8541 to the summary structure. SET is nonzero if the insn is setting the
8542 object, otherwise zero. */
8543
8544 static void
8545 summarize_insn (rtx x, struct shadow_summary *sum, int set)
8546 {
8547 const char *format_ptr;
8548 int i, j;
8549
8550 if (x == 0)
8551 return;
8552
8553 switch (GET_CODE (x))
8554 {
8555 /* ??? Note that this case would be incorrect if the Alpha had a
8556 ZERO_EXTRACT in SET_DEST. */
8557 case SET:
8558 summarize_insn (SET_SRC (x), sum, 0);
8559 summarize_insn (SET_DEST (x), sum, 1);
8560 break;
8561
8562 case CLOBBER:
8563 summarize_insn (XEXP (x, 0), sum, 1);
8564 break;
8565
8566 case USE:
8567 summarize_insn (XEXP (x, 0), sum, 0);
8568 break;
8569
8570 case ASM_OPERANDS:
8571 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
8572 summarize_insn (ASM_OPERANDS_INPUT (x, i), sum, 0);
8573 break;
8574
8575 case PARALLEL:
8576 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8577 summarize_insn (XVECEXP (x, 0, i), sum, 0);
8578 break;
8579
8580 case SUBREG:
8581 summarize_insn (SUBREG_REG (x), sum, 0);
8582 break;
8583
8584 case REG:
8585 {
8586 int regno = REGNO (x);
8587 unsigned long mask = ((unsigned long) 1) << (regno % 32);
8588
8589 if (regno == 31 || regno == 63)
8590 break;
8591
8592 if (set)
8593 {
8594 if (regno < 32)
8595 sum->defd.i |= mask;
8596 else
8597 sum->defd.fp |= mask;
8598 }
8599 else
8600 {
8601 if (regno < 32)
8602 sum->used.i |= mask;
8603 else
8604 sum->used.fp |= mask;
8605 }
8606 }
8607 break;
8608
8609 case MEM:
8610 if (set)
8611 sum->defd.mem = 1;
8612 else
8613 sum->used.mem = 1;
8614
8615 /* Find the regs used in memory address computation: */
8616 summarize_insn (XEXP (x, 0), sum, 0);
8617 break;
8618
8619 case CONST_INT: case CONST_WIDE_INT: case CONST_DOUBLE:
8620 case SYMBOL_REF: case LABEL_REF: case CONST:
8621 case SCRATCH: case ASM_INPUT:
8622 break;
8623
8624 /* Handle common unary and binary ops for efficiency. */
8625 case COMPARE: case PLUS: case MINUS: case MULT: case DIV:
8626 case MOD: case UDIV: case UMOD: case AND: case IOR:
8627 case XOR: case ASHIFT: case ROTATE: case ASHIFTRT: case LSHIFTRT:
8628 case ROTATERT: case SMIN: case SMAX: case UMIN: case UMAX:
8629 case NE: case EQ: case GE: case GT: case LE:
8630 case LT: case GEU: case GTU: case LEU: case LTU:
8631 summarize_insn (XEXP (x, 0), sum, 0);
8632 summarize_insn (XEXP (x, 1), sum, 0);
8633 break;
8634
8635 case NEG: case NOT: case SIGN_EXTEND: case ZERO_EXTEND:
8636 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE: case FLOAT:
8637 case FIX: case UNSIGNED_FLOAT: case UNSIGNED_FIX: case ABS:
8638 case SQRT: case FFS:
8639 summarize_insn (XEXP (x, 0), sum, 0);
8640 break;
8641
8642 default:
8643 format_ptr = GET_RTX_FORMAT (GET_CODE (x));
8644 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8645 switch (format_ptr[i])
8646 {
8647 case 'e':
8648 summarize_insn (XEXP (x, i), sum, 0);
8649 break;
8650
8651 case 'E':
8652 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8653 summarize_insn (XVECEXP (x, i, j), sum, 0);
8654 break;
8655
8656 case 'i':
8657 break;
8658
8659 default:
8660 gcc_unreachable ();
8661 }
8662 }
8663 }
8664
8665 /* Ensure a sufficient number of `trapb' insns are in the code when
8666 the user requests code with a trap precision of functions or
8667 instructions.
8668
8669 In naive mode, when the user requests a trap-precision of
8670 "instruction", a trapb is needed after every instruction that may
8671 generate a trap. This ensures that the code is resumption safe but
8672 it is also slow.
8673
8674 When optimizations are turned on, we delay issuing a trapb as long
8675 as possible. In this context, a trap shadow is the sequence of
8676 instructions that starts with a (potentially) trap generating
8677 instruction and extends to the next trapb or call_pal instruction
8678 (but GCC never generates call_pal by itself). We can delay (and
8679 therefore sometimes omit) a trapb subject to the following
8680 conditions:
8681
8682 (a) On entry to the trap shadow, if any Alpha register or memory
8683 location contains a value that is used as an operand value by some
8684 instruction in the trap shadow (live on entry), then no instruction
8685 in the trap shadow may modify the register or memory location.
8686
8687 (b) Within the trap shadow, the computation of the base register
8688 for a memory load or store instruction may not involve using the
8689 result of an instruction that might generate an UNPREDICTABLE
8690 result.
8691
8692 (c) Within the trap shadow, no register may be used more than once
8693 as a destination register. (This is to make life easier for the
8694 trap-handler.)
8695
8696 (d) The trap shadow may not include any branch instructions. */
8697
8698 static void
8699 alpha_handle_trap_shadows (void)
8700 {
8701 struct shadow_summary shadow;
8702 int trap_pending, exception_nesting;
8703 rtx_insn *i, *n;
8704
8705 trap_pending = 0;
8706 exception_nesting = 0;
8707 shadow.used.i = 0;
8708 shadow.used.fp = 0;
8709 shadow.used.mem = 0;
8710 shadow.defd = shadow.used;
8711
8712 for (i = get_insns (); i ; i = NEXT_INSN (i))
8713 {
8714 if (NOTE_P (i))
8715 {
8716 switch (NOTE_KIND (i))
8717 {
8718 case NOTE_INSN_EH_REGION_BEG:
8719 exception_nesting++;
8720 if (trap_pending)
8721 goto close_shadow;
8722 break;
8723
8724 case NOTE_INSN_EH_REGION_END:
8725 exception_nesting--;
8726 if (trap_pending)
8727 goto close_shadow;
8728 break;
8729
8730 case NOTE_INSN_EPILOGUE_BEG:
8731 if (trap_pending && alpha_tp >= ALPHA_TP_FUNC)
8732 goto close_shadow;
8733 break;
8734 }
8735 }
8736 else if (trap_pending)
8737 {
8738 if (alpha_tp == ALPHA_TP_FUNC)
8739 {
8740 if (JUMP_P (i)
8741 && GET_CODE (PATTERN (i)) == RETURN)
8742 goto close_shadow;
8743 }
8744 else if (alpha_tp == ALPHA_TP_INSN)
8745 {
8746 if (optimize > 0)
8747 {
8748 struct shadow_summary sum;
8749
8750 sum.used.i = 0;
8751 sum.used.fp = 0;
8752 sum.used.mem = 0;
8753 sum.defd = sum.used;
8754
8755 switch (GET_CODE (i))
8756 {
8757 case INSN:
8758 /* Annoyingly, get_attr_trap will die on these. */
8759 if (GET_CODE (PATTERN (i)) == USE
8760 || GET_CODE (PATTERN (i)) == CLOBBER)
8761 break;
8762
8763 summarize_insn (PATTERN (i), &sum, 0);
8764
8765 if ((sum.defd.i & shadow.defd.i)
8766 || (sum.defd.fp & shadow.defd.fp))
8767 {
8768 /* (c) would be violated */
8769 goto close_shadow;
8770 }
8771
8772 /* Combine shadow with summary of current insn: */
8773 shadow.used.i |= sum.used.i;
8774 shadow.used.fp |= sum.used.fp;
8775 shadow.used.mem |= sum.used.mem;
8776 shadow.defd.i |= sum.defd.i;
8777 shadow.defd.fp |= sum.defd.fp;
8778 shadow.defd.mem |= sum.defd.mem;
8779
8780 if ((sum.defd.i & shadow.used.i)
8781 || (sum.defd.fp & shadow.used.fp)
8782 || (sum.defd.mem & shadow.used.mem))
8783 {
8784 /* (a) would be violated (also takes care of (b)) */
8785 gcc_assert (get_attr_trap (i) != TRAP_YES
8786 || (!(sum.defd.i & sum.used.i)
8787 && !(sum.defd.fp & sum.used.fp)));
8788
8789 goto close_shadow;
8790 }
8791 break;
8792
8793 case BARRIER:
8794 /* __builtin_unreachable can expand to no code at all,
8795 leaving (barrier) RTXes in the instruction stream. */
8796 goto close_shadow_notrapb;
8797
8798 case JUMP_INSN:
8799 case CALL_INSN:
8800 case CODE_LABEL:
8801 goto close_shadow;
8802
8803 case DEBUG_INSN:
8804 break;
8805
8806 default:
8807 gcc_unreachable ();
8808 }
8809 }
8810 else
8811 {
8812 close_shadow:
8813 n = emit_insn_before (gen_trapb (), i);
8814 PUT_MODE (n, TImode);
8815 PUT_MODE (i, TImode);
8816 close_shadow_notrapb:
8817 trap_pending = 0;
8818 shadow.used.i = 0;
8819 shadow.used.fp = 0;
8820 shadow.used.mem = 0;
8821 shadow.defd = shadow.used;
8822 }
8823 }
8824 }
8825
8826 if ((exception_nesting > 0 || alpha_tp >= ALPHA_TP_FUNC)
8827 && NONJUMP_INSN_P (i)
8828 && GET_CODE (PATTERN (i)) != USE
8829 && GET_CODE (PATTERN (i)) != CLOBBER
8830 && get_attr_trap (i) == TRAP_YES)
8831 {
8832 if (optimize && !trap_pending)
8833 summarize_insn (PATTERN (i), &shadow, 0);
8834 trap_pending = 1;
8835 }
8836 }
8837 }
8838
8839 /* Alpha can only issue instruction groups simultaneously if they are
8841 suitably aligned. This is very processor-specific. */
8842 /* There are a number of entries in alphaev4_insn_pipe and alphaev5_insn_pipe
8843 that are marked "fake". These instructions do not exist on that target,
8844 but it is possible to see these insns with deranged combinations of
8845 command-line options, such as "-mtune=ev4 -mmax". Instead of aborting,
8846 choose a result at random. */
8847
8848 enum alphaev4_pipe {
8849 EV4_STOP = 0,
8850 EV4_IB0 = 1,
8851 EV4_IB1 = 2,
8852 EV4_IBX = 4
8853 };
8854
8855 enum alphaev5_pipe {
8856 EV5_STOP = 0,
8857 EV5_NONE = 1,
8858 EV5_E01 = 2,
8859 EV5_E0 = 4,
8860 EV5_E1 = 8,
8861 EV5_FAM = 16,
8862 EV5_FA = 32,
8863 EV5_FM = 64
8864 };
8865
8866 static enum alphaev4_pipe
8867 alphaev4_insn_pipe (rtx_insn *insn)
8868 {
8869 if (recog_memoized (insn) < 0)
8870 return EV4_STOP;
8871 if (get_attr_length (insn) != 4)
8872 return EV4_STOP;
8873
8874 switch (get_attr_type (insn))
8875 {
8876 case TYPE_ILD:
8877 case TYPE_LDSYM:
8878 case TYPE_FLD:
8879 case TYPE_LD_L:
8880 return EV4_IBX;
8881
8882 case TYPE_IADD:
8883 case TYPE_ILOG:
8884 case TYPE_ICMOV:
8885 case TYPE_ICMP:
8886 case TYPE_FST:
8887 case TYPE_SHIFT:
8888 case TYPE_IMUL:
8889 case TYPE_FBR:
8890 case TYPE_MVI: /* fake */
8891 return EV4_IB0;
8892
8893 case TYPE_IST:
8894 case TYPE_MISC:
8895 case TYPE_IBR:
8896 case TYPE_JSR:
8897 case TYPE_CALLPAL:
8898 case TYPE_FCPYS:
8899 case TYPE_FCMOV:
8900 case TYPE_FADD:
8901 case TYPE_FDIV:
8902 case TYPE_FMUL:
8903 case TYPE_ST_C:
8904 case TYPE_MB:
8905 case TYPE_FSQRT: /* fake */
8906 case TYPE_FTOI: /* fake */
8907 case TYPE_ITOF: /* fake */
8908 return EV4_IB1;
8909
8910 default:
8911 gcc_unreachable ();
8912 }
8913 }
8914
8915 static enum alphaev5_pipe
8916 alphaev5_insn_pipe (rtx_insn *insn)
8917 {
8918 if (recog_memoized (insn) < 0)
8919 return EV5_STOP;
8920 if (get_attr_length (insn) != 4)
8921 return EV5_STOP;
8922
8923 switch (get_attr_type (insn))
8924 {
8925 case TYPE_ILD:
8926 case TYPE_FLD:
8927 case TYPE_LDSYM:
8928 case TYPE_IADD:
8929 case TYPE_ILOG:
8930 case TYPE_ICMOV:
8931 case TYPE_ICMP:
8932 return EV5_E01;
8933
8934 case TYPE_IST:
8935 case TYPE_FST:
8936 case TYPE_SHIFT:
8937 case TYPE_IMUL:
8938 case TYPE_MISC:
8939 case TYPE_MVI:
8940 case TYPE_LD_L:
8941 case TYPE_ST_C:
8942 case TYPE_MB:
8943 case TYPE_FTOI: /* fake */
8944 case TYPE_ITOF: /* fake */
8945 return EV5_E0;
8946
8947 case TYPE_IBR:
8948 case TYPE_JSR:
8949 case TYPE_CALLPAL:
8950 return EV5_E1;
8951
8952 case TYPE_FCPYS:
8953 return EV5_FAM;
8954
8955 case TYPE_FBR:
8956 case TYPE_FCMOV:
8957 case TYPE_FADD:
8958 case TYPE_FDIV:
8959 case TYPE_FSQRT: /* fake */
8960 return EV5_FA;
8961
8962 case TYPE_FMUL:
8963 return EV5_FM;
8964
8965 default:
8966 gcc_unreachable ();
8967 }
8968 }
8969
8970 /* IN_USE is a mask of the slots currently filled within the insn group.
8971 The mask bits come from alphaev4_pipe above. If EV4_IBX is set, then
8972 the insn in EV4_IB0 can be swapped by the hardware into EV4_IB1.
8973
8974 LEN is, of course, the length of the group in bytes. */
8975
8976 static rtx_insn *
8977 alphaev4_next_group (rtx_insn *insn, int *pin_use, int *plen)
8978 {
8979 int len, in_use;
8980
8981 len = in_use = 0;
8982
8983 if (! INSN_P (insn)
8984 || GET_CODE (PATTERN (insn)) == CLOBBER
8985 || GET_CODE (PATTERN (insn)) == USE)
8986 goto next_and_done;
8987
8988 while (1)
8989 {
8990 enum alphaev4_pipe pipe;
8991
8992 pipe = alphaev4_insn_pipe (insn);
8993 switch (pipe)
8994 {
8995 case EV4_STOP:
8996 /* Force complex instructions to start new groups. */
8997 if (in_use)
8998 goto done;
8999
9000 /* If this is a completely unrecognized insn, it's an asm.
9001 We don't know how long it is, so record length as -1 to
9002 signal a needed realignment. */
9003 if (recog_memoized (insn) < 0)
9004 len = -1;
9005 else
9006 len = get_attr_length (insn);
9007 goto next_and_done;
9008
9009 case EV4_IBX:
9010 if (in_use & EV4_IB0)
9011 {
9012 if (in_use & EV4_IB1)
9013 goto done;
9014 in_use |= EV4_IB1;
9015 }
9016 else
9017 in_use |= EV4_IB0 | EV4_IBX;
9018 break;
9019
9020 case EV4_IB0:
9021 if (in_use & EV4_IB0)
9022 {
9023 if (!(in_use & EV4_IBX) || (in_use & EV4_IB1))
9024 goto done;
9025 in_use |= EV4_IB1;
9026 }
9027 in_use |= EV4_IB0;
9028 break;
9029
9030 case EV4_IB1:
9031 if (in_use & EV4_IB1)
9032 goto done;
9033 in_use |= EV4_IB1;
9034 break;
9035
9036 default:
9037 gcc_unreachable ();
9038 }
9039 len += 4;
9040
9041 /* Haifa doesn't do well scheduling branches. */
9042 if (JUMP_P (insn))
9043 goto next_and_done;
9044
9045 next:
9046 insn = next_nonnote_insn (insn);
9047
9048 if (!insn || ! INSN_P (insn))
9049 goto done;
9050
9051 /* Let Haifa tell us where it thinks insn group boundaries are. */
9052 if (GET_MODE (insn) == TImode)
9053 goto done;
9054
9055 if (GET_CODE (insn) == CLOBBER || GET_CODE (insn) == USE)
9056 goto next;
9057 }
9058
9059 next_and_done:
9060 insn = next_nonnote_insn (insn);
9061
9062 done:
9063 *plen = len;
9064 *pin_use = in_use;
9065 return insn;
9066 }
9067
9068 /* IN_USE is a mask of the slots currently filled within the insn group.
9069 The mask bits come from alphaev5_pipe above. If EV5_E01 is set, then
9070 the insn in EV5_E0 can be swapped by the hardware into EV5_E1.
9071
9072 LEN is, of course, the length of the group in bytes. */
9073
9074 static rtx_insn *
9075 alphaev5_next_group (rtx_insn *insn, int *pin_use, int *plen)
9076 {
9077 int len, in_use;
9078
9079 len = in_use = 0;
9080
9081 if (! INSN_P (insn)
9082 || GET_CODE (PATTERN (insn)) == CLOBBER
9083 || GET_CODE (PATTERN (insn)) == USE)
9084 goto next_and_done;
9085
9086 while (1)
9087 {
9088 enum alphaev5_pipe pipe;
9089
9090 pipe = alphaev5_insn_pipe (insn);
9091 switch (pipe)
9092 {
9093 case EV5_STOP:
9094 /* Force complex instructions to start new groups. */
9095 if (in_use)
9096 goto done;
9097
9098 /* If this is a completely unrecognized insn, it's an asm.
9099 We don't know how long it is, so record length as -1 to
9100 signal a needed realignment. */
9101 if (recog_memoized (insn) < 0)
9102 len = -1;
9103 else
9104 len = get_attr_length (insn);
9105 goto next_and_done;
9106
9107 /* ??? Most of the places below, we would like to assert never
9108 happen, as it would indicate an error either in Haifa, or
9109 in the scheduling description. Unfortunately, Haifa never
9110 schedules the last instruction of the BB, so we don't have
9111 an accurate TI bit to go off. */
9112 case EV5_E01:
9113 if (in_use & EV5_E0)
9114 {
9115 if (in_use & EV5_E1)
9116 goto done;
9117 in_use |= EV5_E1;
9118 }
9119 else
9120 in_use |= EV5_E0 | EV5_E01;
9121 break;
9122
9123 case EV5_E0:
9124 if (in_use & EV5_E0)
9125 {
9126 if (!(in_use & EV5_E01) || (in_use & EV5_E1))
9127 goto done;
9128 in_use |= EV5_E1;
9129 }
9130 in_use |= EV5_E0;
9131 break;
9132
9133 case EV5_E1:
9134 if (in_use & EV5_E1)
9135 goto done;
9136 in_use |= EV5_E1;
9137 break;
9138
9139 case EV5_FAM:
9140 if (in_use & EV5_FA)
9141 {
9142 if (in_use & EV5_FM)
9143 goto done;
9144 in_use |= EV5_FM;
9145 }
9146 else
9147 in_use |= EV5_FA | EV5_FAM;
9148 break;
9149
9150 case EV5_FA:
9151 if (in_use & EV5_FA)
9152 goto done;
9153 in_use |= EV5_FA;
9154 break;
9155
9156 case EV5_FM:
9157 if (in_use & EV5_FM)
9158 goto done;
9159 in_use |= EV5_FM;
9160 break;
9161
9162 case EV5_NONE:
9163 break;
9164
9165 default:
9166 gcc_unreachable ();
9167 }
9168 len += 4;
9169
9170 /* Haifa doesn't do well scheduling branches. */
9171 /* ??? If this is predicted not-taken, slotting continues, except
9172 that no more IBR, FBR, or JSR insns may be slotted. */
9173 if (JUMP_P (insn))
9174 goto next_and_done;
9175
9176 next:
9177 insn = next_nonnote_insn (insn);
9178
9179 if (!insn || ! INSN_P (insn))
9180 goto done;
9181
9182 /* Let Haifa tell us where it thinks insn group boundaries are. */
9183 if (GET_MODE (insn) == TImode)
9184 goto done;
9185
9186 if (GET_CODE (insn) == CLOBBER || GET_CODE (insn) == USE)
9187 goto next;
9188 }
9189
9190 next_and_done:
9191 insn = next_nonnote_insn (insn);
9192
9193 done:
9194 *plen = len;
9195 *pin_use = in_use;
9196 return insn;
9197 }
9198
9199 static rtx
9200 alphaev4_next_nop (int *pin_use)
9201 {
9202 int in_use = *pin_use;
9203 rtx nop;
9204
9205 if (!(in_use & EV4_IB0))
9206 {
9207 in_use |= EV4_IB0;
9208 nop = gen_nop ();
9209 }
9210 else if ((in_use & (EV4_IBX|EV4_IB1)) == EV4_IBX)
9211 {
9212 in_use |= EV4_IB1;
9213 nop = gen_nop ();
9214 }
9215 else if (TARGET_FP && !(in_use & EV4_IB1))
9216 {
9217 in_use |= EV4_IB1;
9218 nop = gen_fnop ();
9219 }
9220 else
9221 nop = gen_unop ();
9222
9223 *pin_use = in_use;
9224 return nop;
9225 }
9226
9227 static rtx
9228 alphaev5_next_nop (int *pin_use)
9229 {
9230 int in_use = *pin_use;
9231 rtx nop;
9232
9233 if (!(in_use & EV5_E1))
9234 {
9235 in_use |= EV5_E1;
9236 nop = gen_nop ();
9237 }
9238 else if (TARGET_FP && !(in_use & EV5_FA))
9239 {
9240 in_use |= EV5_FA;
9241 nop = gen_fnop ();
9242 }
9243 else if (TARGET_FP && !(in_use & EV5_FM))
9244 {
9245 in_use |= EV5_FM;
9246 nop = gen_fnop ();
9247 }
9248 else
9249 nop = gen_unop ();
9250
9251 *pin_use = in_use;
9252 return nop;
9253 }
9254
9255 /* The instruction group alignment main loop. */
9256
9257 static void
9258 alpha_align_insns_1 (unsigned int max_align,
9259 rtx_insn *(*next_group) (rtx_insn *, int *, int *),
9260 rtx (*next_nop) (int *))
9261 {
9262 /* ALIGN is the known alignment for the insn group. */
9263 unsigned int align;
9264 /* OFS is the offset of the current insn in the insn group. */
9265 int ofs;
9266 int prev_in_use, in_use, len, ldgp;
9267 rtx_insn *i, *next;
9268
9269 /* Let shorten branches care for assigning alignments to code labels. */
9270 shorten_branches (get_insns ());
9271
9272 unsigned int option_alignment = align_functions.levels[0].get_value ();
9273 if (option_alignment < 4)
9274 align = 4;
9275 else if ((unsigned int) option_alignment < max_align)
9276 align = option_alignment;
9277 else
9278 align = max_align;
9279
9280 ofs = prev_in_use = 0;
9281 i = get_insns ();
9282 if (NOTE_P (i))
9283 i = next_nonnote_insn (i);
9284
9285 ldgp = alpha_function_needs_gp ? 8 : 0;
9286
9287 while (i)
9288 {
9289 next = (*next_group) (i, &in_use, &len);
9290
9291 /* When we see a label, resync alignment etc. */
9292 if (LABEL_P (i))
9293 {
9294 unsigned int new_align
9295 = label_to_alignment (i).levels[0].get_value ();
9296
9297 if (new_align >= align)
9298 {
9299 align = new_align < max_align ? new_align : max_align;
9300 ofs = 0;
9301 }
9302
9303 else if (ofs & (new_align-1))
9304 ofs = (ofs | (new_align-1)) + 1;
9305 gcc_assert (!len);
9306 }
9307
9308 /* Handle complex instructions special. */
9309 else if (in_use == 0)
9310 {
9311 /* Asms will have length < 0. This is a signal that we have
9312 lost alignment knowledge. Assume, however, that the asm
9313 will not mis-align instructions. */
9314 if (len < 0)
9315 {
9316 ofs = 0;
9317 align = 4;
9318 len = 0;
9319 }
9320 }
9321
9322 /* If the known alignment is smaller than the recognized insn group,
9323 realign the output. */
9324 else if ((int) align < len)
9325 {
9326 unsigned int new_log_align = len > 8 ? 4 : 3;
9327 rtx_insn *prev, *where;
9328
9329 where = prev = prev_nonnote_insn (i);
9330 if (!where || !LABEL_P (where))
9331 where = i;
9332
9333 /* Can't realign between a call and its gp reload. */
9334 if (! (TARGET_EXPLICIT_RELOCS
9335 && prev && CALL_P (prev)))
9336 {
9337 emit_insn_before (gen_realign (GEN_INT (new_log_align)), where);
9338 align = 1 << new_log_align;
9339 ofs = 0;
9340 }
9341 }
9342
9343 /* We may not insert padding inside the initial ldgp sequence. */
9344 else if (ldgp > 0)
9345 ldgp -= len;
9346
9347 /* If the group won't fit in the same INT16 as the previous,
9348 we need to add padding to keep the group together. Rather
9349 than simply leaving the insn filling to the assembler, we
9350 can make use of the knowledge of what sorts of instructions
9351 were issued in the previous group to make sure that all of
9352 the added nops are really free. */
9353 else if (ofs + len > (int) align)
9354 {
9355 int nop_count = (align - ofs) / 4;
9356 rtx_insn *where;
9357
9358 /* Insert nops before labels, branches, and calls to truly merge
9359 the execution of the nops with the previous instruction group. */
9360 where = prev_nonnote_insn (i);
9361 if (where)
9362 {
9363 if (LABEL_P (where))
9364 {
9365 rtx_insn *where2 = prev_nonnote_insn (where);
9366 if (where2 && JUMP_P (where2))
9367 where = where2;
9368 }
9369 else if (NONJUMP_INSN_P (where))
9370 where = i;
9371 }
9372 else
9373 where = i;
9374
9375 do
9376 emit_insn_before ((*next_nop)(&prev_in_use), where);
9377 while (--nop_count);
9378 ofs = 0;
9379 }
9380
9381 ofs = (ofs + len) & (align - 1);
9382 prev_in_use = in_use;
9383 i = next;
9384 }
9385 }
9386
9387 static void
9388 alpha_align_insns (void)
9389 {
9390 if (alpha_tune == PROCESSOR_EV4)
9391 alpha_align_insns_1 (8, alphaev4_next_group, alphaev4_next_nop);
9392 else if (alpha_tune == PROCESSOR_EV5)
9393 alpha_align_insns_1 (16, alphaev5_next_group, alphaev5_next_nop);
9394 else
9395 gcc_unreachable ();
9396 }
9397
9398 /* Insert an unop between sibcall or noreturn function call and GP load. */
9399
9400 static void
9401 alpha_pad_function_end (void)
9402 {
9403 rtx_insn *insn, *next;
9404
9405 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9406 {
9407 if (!CALL_P (insn)
9408 || !(SIBLING_CALL_P (insn)
9409 || find_reg_note (insn, REG_NORETURN, NULL_RTX)))
9410 continue;
9411
9412 next = next_active_insn (insn);
9413 if (next)
9414 {
9415 rtx pat = PATTERN (next);
9416
9417 if (GET_CODE (pat) == SET
9418 && GET_CODE (SET_SRC (pat)) == UNSPEC_VOLATILE
9419 && XINT (SET_SRC (pat), 1) == UNSPECV_LDGP1)
9420 emit_insn_after (gen_unop (), insn);
9421 }
9422 }
9423 }
9424
9425 /* Machine dependent reorg pass. */
9427
9428 static void
9429 alpha_reorg (void)
9430 {
9431 /* Workaround for a linker error that triggers when an exception
9432 handler immediatelly follows a sibcall or a noreturn function.
9433
9434 In the sibcall case:
9435
9436 The instruction stream from an object file:
9437
9438 1d8: 00 00 fb 6b jmp (t12)
9439 1dc: 00 00 ba 27 ldah gp,0(ra)
9440 1e0: 00 00 bd 23 lda gp,0(gp)
9441 1e4: 00 00 7d a7 ldq t12,0(gp)
9442 1e8: 00 40 5b 6b jsr ra,(t12),1ec <__funcZ+0x1ec>
9443
9444 was converted in the final link pass to:
9445
9446 12003aa88: 67 fa ff c3 br 120039428 <...>
9447 12003aa8c: 00 00 fe 2f unop
9448 12003aa90: 00 00 fe 2f unop
9449 12003aa94: 48 83 7d a7 ldq t12,-31928(gp)
9450 12003aa98: 00 40 5b 6b jsr ra,(t12),12003aa9c <__func+0x1ec>
9451
9452 And in the noreturn case:
9453
9454 The instruction stream from an object file:
9455
9456 54: 00 40 5b 6b jsr ra,(t12),58 <__func+0x58>
9457 58: 00 00 ba 27 ldah gp,0(ra)
9458 5c: 00 00 bd 23 lda gp,0(gp)
9459 60: 00 00 7d a7 ldq t12,0(gp)
9460 64: 00 40 5b 6b jsr ra,(t12),68 <__func+0x68>
9461
9462 was converted in the final link pass to:
9463
9464 fdb24: a0 03 40 d3 bsr ra,fe9a8 <_called_func+0x8>
9465 fdb28: 00 00 fe 2f unop
9466 fdb2c: 00 00 fe 2f unop
9467 fdb30: 30 82 7d a7 ldq t12,-32208(gp)
9468 fdb34: 00 40 5b 6b jsr ra,(t12),fdb38 <__func+0x68>
9469
9470 GP load instructions were wrongly cleared by the linker relaxation
9471 pass. This workaround prevents removal of GP loads by inserting
9472 an unop instruction between a sibcall or noreturn function call and
9473 exception handler prologue. */
9474
9475 if (current_function_has_exception_handlers ())
9476 alpha_pad_function_end ();
9477
9478 /* CALL_PAL that implements trap insn, updates program counter to point
9479 after the insn. In case trap is the last insn in the function,
9480 emit NOP to guarantee that PC remains inside function boundaries.
9481 This workaround is needed to get reliable backtraces. */
9482
9483 rtx_insn *insn = prev_active_insn (get_last_insn ());
9484
9485 if (insn && NONJUMP_INSN_P (insn))
9486 {
9487 rtx pat = PATTERN (insn);
9488 if (GET_CODE (pat) == PARALLEL)
9489 {
9490 rtx vec = XVECEXP (pat, 0, 0);
9491 if (GET_CODE (vec) == TRAP_IF
9492 && XEXP (vec, 0) == const1_rtx)
9493 emit_insn_after (gen_unop (), insn);
9494 }
9495 }
9496 }
9497
9498 static void
9500 alpha_file_start (void)
9501 {
9502 default_file_start ();
9503
9504 fputs ("\t.set noreorder\n", asm_out_file);
9505 fputs ("\t.set volatile\n", asm_out_file);
9506 if (TARGET_ABI_OSF)
9507 fputs ("\t.set noat\n", asm_out_file);
9508 if (TARGET_EXPLICIT_RELOCS)
9509 fputs ("\t.set nomacro\n", asm_out_file);
9510 if (TARGET_SUPPORT_ARCH | TARGET_BWX | TARGET_MAX | TARGET_FIX | TARGET_CIX)
9511 {
9512 const char *arch;
9513
9514 if (alpha_cpu == PROCESSOR_EV6 || TARGET_FIX || TARGET_CIX)
9515 arch = "ev6";
9516 else if (TARGET_MAX)
9517 arch = "pca56";
9518 else if (TARGET_BWX)
9519 arch = "ev56";
9520 else if (alpha_cpu == PROCESSOR_EV5)
9521 arch = "ev5";
9522 else
9523 arch = "ev4";
9524
9525 fprintf (asm_out_file, "\t.arch %s\n", arch);
9526 }
9527 }
9528
9529 /* Since we don't have a .dynbss section, we should not allow global
9530 relocations in the .rodata section. */
9531
9532 static int
9533 alpha_elf_reloc_rw_mask (void)
9534 {
9535 return flag_pic ? 3 : 2;
9536 }
9537
9538 /* Return a section for X. The only special thing we do here is to
9539 honor small data. */
9540
9541 static section *
9542 alpha_elf_select_rtx_section (machine_mode mode, rtx x,
9543 unsigned HOST_WIDE_INT align)
9544 {
9545 if (TARGET_SMALL_DATA && GET_MODE_SIZE (mode) <= g_switch_value)
9546 /* ??? Consider using mergeable sdata sections. */
9547 return sdata_section;
9548 else
9549 return default_elf_select_rtx_section (mode, x, align);
9550 }
9551
9552 static unsigned int
9553 alpha_elf_section_type_flags (tree decl, const char *name, int reloc)
9554 {
9555 unsigned int flags = 0;
9556
9557 if (strcmp (name, ".sdata") == 0
9558 || startswith (name, ".sdata.")
9559 || startswith (name, ".gnu.linkonce.s.")
9560 || strcmp (name, ".sbss") == 0
9561 || startswith (name, ".sbss.")
9562 || startswith (name, ".gnu.linkonce.sb."))
9563 flags = SECTION_SMALL;
9564
9565 flags |= default_section_type_flags (decl, name, reloc);
9566 return flags;
9567 }
9568
9569 /* Structure to collect function names for final output in link section. */
9571 /* Note that items marked with GTY can't be ifdef'ed out. */
9572
9573 enum reloc_kind
9574 {
9575 KIND_LINKAGE,
9576 KIND_CODEADDR
9577 };
9578
9579 struct GTY(()) alpha_links
9580 {
9581 rtx func;
9582 rtx linkage;
9583 enum reloc_kind rkind;
9584 };
9585
9586 #if TARGET_ABI_OPEN_VMS
9587
9588 /* Return the VMS argument type corresponding to MODE. */
9589
9590 enum avms_arg_type
9591 alpha_arg_type (machine_mode mode)
9592 {
9593 switch (mode)
9594 {
9595 case E_SFmode:
9596 return TARGET_FLOAT_VAX ? FF : FS;
9597 case E_DFmode:
9598 return TARGET_FLOAT_VAX ? FD : FT;
9599 default:
9600 return I64;
9601 }
9602 }
9603
9604 /* Return an rtx for an integer representing the VMS Argument Information
9605 register value. */
9606
9607 rtx
9608 alpha_arg_info_reg_val (CUMULATIVE_ARGS cum)
9609 {
9610 unsigned HOST_WIDE_INT regval = cum.num_args;
9611 int i;
9612
9613 for (i = 0; i < 6; i++)
9614 regval |= ((int) cum.atypes[i]) << (i * 3 + 8);
9615
9616 return GEN_INT (regval);
9617 }
9618
9619
9621 /* Return a SYMBOL_REF representing the reference to the .linkage entry
9622 of function FUNC built for calls made from CFUNDECL. LFLAG is 1 if
9623 this is the reference to the linkage pointer value, 0 if this is the
9624 reference to the function entry value. RFLAG is 1 if this a reduced
9625 reference (code address only), 0 if this is a full reference. */
9626
9627 rtx
9628 alpha_use_linkage (rtx func, bool lflag, bool rflag)
9629 {
9630 struct alpha_links *al = NULL;
9631 const char *name = XSTR (func, 0);
9632
9633 if (cfun->machine->links)
9634 {
9635 /* Is this name already defined? */
9636 alpha_links **slot = cfun->machine->links->get (name);
9637 if (slot)
9638 al = *slot;
9639 }
9640 else
9641 cfun->machine->links
9642 = hash_map<nofree_string_hash, alpha_links *>::create_ggc (64);
9643
9644 if (al == NULL)
9645 {
9646 size_t buf_len;
9647 char *linksym;
9648 tree id;
9649
9650 if (name[0] == '*')
9651 name++;
9652
9653 /* Follow transparent alias, as this is used for CRTL translations. */
9654 id = maybe_get_identifier (name);
9655 if (id)
9656 {
9657 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
9658 id = TREE_CHAIN (id);
9659 name = IDENTIFIER_POINTER (id);
9660 }
9661
9662 buf_len = strlen (name) + 8 + 9;
9663 linksym = (char *) alloca (buf_len);
9664 snprintf (linksym, buf_len, "$%d..%s..lk", cfun->funcdef_no, name);
9665
9666 al = ggc_alloc<alpha_links> ();
9667 al->func = func;
9668 al->linkage = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (linksym));
9669
9670 cfun->machine->links->put (ggc_strdup (name), al);
9671 }
9672
9673 al->rkind = rflag ? KIND_CODEADDR : KIND_LINKAGE;
9674
9675 if (lflag)
9676 return gen_rtx_MEM (Pmode, plus_constant (Pmode, al->linkage, 8));
9677 else
9678 return al->linkage;
9679 }
9680
9681 static int
9682 alpha_write_one_linkage (const char *name, alpha_links *link, FILE *stream)
9683 {
9684 ASM_OUTPUT_INTERNAL_LABEL (stream, XSTR (link->linkage, 0));
9685 if (link->rkind == KIND_CODEADDR)
9686 {
9687 /* External and used, request code address. */
9688 fprintf (stream, "\t.code_address ");
9689 }
9690 else
9691 {
9692 if (!SYMBOL_REF_EXTERNAL_P (link->func)
9693 && SYMBOL_REF_LOCAL_P (link->func))
9694 {
9695 /* Locally defined, build linkage pair. */
9696 fprintf (stream, "\t.quad %s..en\n", name);
9697 fprintf (stream, "\t.quad ");
9698 }
9699 else
9700 {
9701 /* External, request linkage pair. */
9702 fprintf (stream, "\t.linkage ");
9703 }
9704 }
9705 assemble_name (stream, name);
9706 fputs ("\n", stream);
9707
9708 return 0;
9709 }
9710
9711 static void
9712 alpha_write_linkage (FILE *stream, const char *funname)
9713 {
9714 fprintf (stream, "\t.link\n");
9715 fprintf (stream, "\t.align 3\n");
9716 in_section = NULL;
9717
9718 #ifdef TARGET_VMS_CRASH_DEBUG
9719 fputs ("\t.name ", stream);
9720 assemble_name (stream, funname);
9721 fputs ("..na\n", stream);
9722 #endif
9723
9724 ASM_OUTPUT_LABEL (stream, funname);
9725 fprintf (stream, "\t.pdesc ");
9726 assemble_name (stream, funname);
9727 fprintf (stream, "..en,%s\n",
9728 alpha_procedure_type == PT_STACK ? "stack"
9729 : alpha_procedure_type == PT_REGISTER ? "reg" : "null");
9730
9731 if (cfun->machine->links)
9732 {
9733 hash_map<nofree_string_hash, alpha_links *>::iterator iter
9734 = cfun->machine->links->begin ();
9735 for (; iter != cfun->machine->links->end (); ++iter)
9736 alpha_write_one_linkage ((*iter).first, (*iter).second, stream);
9737 }
9738 }
9739
9740 /* Switch to an arbitrary section NAME with attributes as specified
9741 by FLAGS. ALIGN specifies any known alignment requirements for
9742 the section; 0 if the default should be used. */
9743
9744 static void
9745 vms_asm_named_section (const char *name, unsigned int flags,
9746 tree decl ATTRIBUTE_UNUSED)
9747 {
9748 fputc ('\n', asm_out_file);
9749 fprintf (asm_out_file, ".section\t%s", name);
9750
9751 if (flags & SECTION_DEBUG)
9752 fprintf (asm_out_file, ",NOWRT");
9753
9754 fputc ('\n', asm_out_file);
9755 }
9756
9757 /* Record an element in the table of global constructors. SYMBOL is
9758 a SYMBOL_REF of the function to be called; PRIORITY is a number
9759 between 0 and MAX_INIT_PRIORITY.
9760
9761 Differs from default_ctors_section_asm_out_constructor in that the
9762 width of the .ctors entry is always 64 bits, rather than the 32 bits
9763 used by a normal pointer. */
9764
9765 static void
9766 vms_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
9767 {
9768 switch_to_section (ctors_section);
9769 assemble_align (BITS_PER_WORD);
9770 assemble_integer (symbol, UNITS_PER_WORD, BITS_PER_WORD, 1);
9771 }
9772
9773 static void
9774 vms_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
9775 {
9776 switch_to_section (dtors_section);
9777 assemble_align (BITS_PER_WORD);
9778 assemble_integer (symbol, UNITS_PER_WORD, BITS_PER_WORD, 1);
9779 }
9780 #else
9781 rtx
9782 alpha_use_linkage (rtx func ATTRIBUTE_UNUSED,
9783 bool lflag ATTRIBUTE_UNUSED,
9784 bool rflag ATTRIBUTE_UNUSED)
9785 {
9786 return NULL_RTX;
9787 }
9788
9789 #endif /* TARGET_ABI_OPEN_VMS */
9790
9791 static void
9793 alpha_init_libfuncs (void)
9794 {
9795 if (TARGET_ABI_OPEN_VMS)
9796 {
9797 /* Use the VMS runtime library functions for division and
9798 remainder. */
9799 set_optab_libfunc (sdiv_optab, SImode, "OTS$DIV_I");
9800 set_optab_libfunc (sdiv_optab, DImode, "OTS$DIV_L");
9801 set_optab_libfunc (udiv_optab, SImode, "OTS$DIV_UI");
9802 set_optab_libfunc (udiv_optab, DImode, "OTS$DIV_UL");
9803 set_optab_libfunc (smod_optab, SImode, "OTS$REM_I");
9804 set_optab_libfunc (smod_optab, DImode, "OTS$REM_L");
9805 set_optab_libfunc (umod_optab, SImode, "OTS$REM_UI");
9806 set_optab_libfunc (umod_optab, DImode, "OTS$REM_UL");
9807 #ifdef MEM_LIBFUNCS_INIT
9808 MEM_LIBFUNCS_INIT;
9809 #endif
9810 }
9811 }
9812
9813 /* On the Alpha, we use this to disable the floating-point registers
9814 when they don't exist. */
9815
9816 static void
9817 alpha_conditional_register_usage (void)
9818 {
9819 int i;
9820 if (! TARGET_FPREGS)
9821 for (i = 32; i < 63; i++)
9822 fixed_regs[i] = call_used_regs[i] = 1;
9823 }
9824
9825 /* Canonicalize a comparison from one we don't have to one we do have. */
9826
9827 static void
9828 alpha_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
9829 bool op0_preserve_value)
9830 {
9831 if (!op0_preserve_value
9832 && (*code == GE || *code == GT || *code == GEU || *code == GTU)
9833 && (REG_P (*op1) || *op1 == const0_rtx))
9834 {
9835 std::swap (*op0, *op1);
9836 *code = (int)swap_condition ((enum rtx_code)*code);
9837 }
9838
9839 if ((*code == LT || *code == LTU)
9840 && CONST_INT_P (*op1) && INTVAL (*op1) == 256)
9841 {
9842 *code = *code == LT ? LE : LEU;
9843 *op1 = GEN_INT (255);
9844 }
9845 }
9846
9847 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
9848
9849 static void
9850 alpha_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
9851 {
9852 const unsigned HOST_WIDE_INT SWCR_STATUS_MASK = (0x3fUL << 17);
9853
9854 tree fenv_var, get_fpscr, set_fpscr, mask, ld_fenv, masked_fenv;
9855 tree new_fenv_var, reload_fenv, restore_fnenv;
9856 tree update_call, atomic_feraiseexcept, hold_fnclex;
9857
9858 /* Assume OSF/1 compatible interfaces. */
9859 if (!TARGET_ABI_OSF)
9860 return;
9861
9862 /* Generate the equivalent of :
9863 unsigned long fenv_var;
9864 fenv_var = __ieee_get_fp_control ();
9865
9866 unsigned long masked_fenv;
9867 masked_fenv = fenv_var & mask;
9868
9869 __ieee_set_fp_control (masked_fenv); */
9870
9871 fenv_var = create_tmp_var_raw (long_unsigned_type_node);
9872 get_fpscr
9873 = build_fn_decl ("__ieee_get_fp_control",
9874 build_function_type_list (long_unsigned_type_node, NULL));
9875 set_fpscr
9876 = build_fn_decl ("__ieee_set_fp_control",
9877 build_function_type_list (void_type_node, NULL));
9878 mask = build_int_cst (long_unsigned_type_node, ~SWCR_STATUS_MASK);
9879 ld_fenv = build4 (TARGET_EXPR, long_unsigned_type_node, fenv_var,
9880 build_call_expr (get_fpscr, 0), NULL_TREE, NULL_TREE);
9881 masked_fenv = build2 (BIT_AND_EXPR, long_unsigned_type_node, fenv_var, mask);
9882 hold_fnclex = build_call_expr (set_fpscr, 1, masked_fenv);
9883 *hold = build2 (COMPOUND_EXPR, void_type_node,
9884 build2 (COMPOUND_EXPR, void_type_node, masked_fenv, ld_fenv),
9885 hold_fnclex);
9886
9887 /* Store the value of masked_fenv to clear the exceptions:
9888 __ieee_set_fp_control (masked_fenv); */
9889
9890 *clear = build_call_expr (set_fpscr, 1, masked_fenv);
9891
9892 /* Generate the equivalent of :
9893 unsigned long new_fenv_var;
9894 new_fenv_var = __ieee_get_fp_control ();
9895
9896 __ieee_set_fp_control (fenv_var);
9897
9898 __atomic_feraiseexcept (new_fenv_var); */
9899
9900 new_fenv_var = create_tmp_var_raw (long_unsigned_type_node);
9901 reload_fenv = build4 (TARGET_EXPR, long_unsigned_type_node, new_fenv_var,
9902 build_call_expr (get_fpscr, 0), NULL_TREE, NULL_TREE);
9903 restore_fnenv = build_call_expr (set_fpscr, 1, fenv_var);
9904 atomic_feraiseexcept = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
9905 update_call
9906 = build_call_expr (atomic_feraiseexcept, 1,
9907 fold_convert (integer_type_node, new_fenv_var));
9908 *update = build2 (COMPOUND_EXPR, void_type_node,
9909 build2 (COMPOUND_EXPR, void_type_node,
9910 reload_fenv, restore_fnenv), update_call);
9911 }
9912
9913 /* Implement TARGET_HARD_REGNO_MODE_OK. On Alpha, the integer registers
9914 can hold any mode. The floating-point registers can hold 64-bit
9915 integers as well, but not smaller values. */
9916
9917 static bool
9918 alpha_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
9919 {
9920 if (IN_RANGE (regno, 32, 62))
9921 return (mode == SFmode
9922 || mode == DFmode
9923 || mode == DImode
9924 || mode == SCmode
9925 || mode == DCmode);
9926 return true;
9927 }
9928
9929 /* Implement TARGET_MODES_TIEABLE_P. This asymmetric test is true when
9930 MODE1 could be put in an FP register but MODE2 could not. */
9931
9932 static bool
9933 alpha_modes_tieable_p (machine_mode mode1, machine_mode mode2)
9934 {
9935 return (alpha_hard_regno_mode_ok (32, mode1)
9936 ? alpha_hard_regno_mode_ok (32, mode2)
9937 : true);
9938 }
9939
9940 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
9941
9942 static bool
9943 alpha_can_change_mode_class (machine_mode from, machine_mode to,
9944 reg_class_t rclass)
9945 {
9946 return (GET_MODE_SIZE (from) == GET_MODE_SIZE (to)
9947 || !reg_classes_intersect_p (FLOAT_REGS, rclass));
9948 }
9949
9950 /* Initialize the GCC target structure. */
9952 #if TARGET_ABI_OPEN_VMS
9953 # undef TARGET_ATTRIBUTE_TABLE
9954 # define TARGET_ATTRIBUTE_TABLE vms_attribute_table
9955 # undef TARGET_CAN_ELIMINATE
9956 # define TARGET_CAN_ELIMINATE alpha_vms_can_eliminate
9957 #endif
9958
9959 #undef TARGET_IN_SMALL_DATA_P
9960 #define TARGET_IN_SMALL_DATA_P alpha_in_small_data_p
9961
9962 #undef TARGET_ASM_ALIGNED_HI_OP
9963 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
9964 #undef TARGET_ASM_ALIGNED_DI_OP
9965 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
9966
9967 /* Default unaligned ops are provided for ELF systems. To get unaligned
9968 data for non-ELF systems, we have to turn off auto alignment. */
9969 #if TARGET_ABI_OPEN_VMS
9970 #undef TARGET_ASM_UNALIGNED_HI_OP
9971 #define TARGET_ASM_UNALIGNED_HI_OP "\t.align 0\n\t.word\t"
9972 #undef TARGET_ASM_UNALIGNED_SI_OP
9973 #define TARGET_ASM_UNALIGNED_SI_OP "\t.align 0\n\t.long\t"
9974 #undef TARGET_ASM_UNALIGNED_DI_OP
9975 #define TARGET_ASM_UNALIGNED_DI_OP "\t.align 0\n\t.quad\t"
9976 #endif
9977
9978 #undef TARGET_ASM_RELOC_RW_MASK
9979 #define TARGET_ASM_RELOC_RW_MASK alpha_elf_reloc_rw_mask
9980 #undef TARGET_ASM_SELECT_RTX_SECTION
9981 #define TARGET_ASM_SELECT_RTX_SECTION alpha_elf_select_rtx_section
9982 #undef TARGET_SECTION_TYPE_FLAGS
9983 #define TARGET_SECTION_TYPE_FLAGS alpha_elf_section_type_flags
9984
9985 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
9986 #define TARGET_ASM_FUNCTION_END_PROLOGUE alpha_output_function_end_prologue
9987
9988 #undef TARGET_INIT_LIBFUNCS
9989 #define TARGET_INIT_LIBFUNCS alpha_init_libfuncs
9990
9991 #undef TARGET_LEGITIMIZE_ADDRESS
9992 #define TARGET_LEGITIMIZE_ADDRESS alpha_legitimize_address
9993 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
9994 #define TARGET_MODE_DEPENDENT_ADDRESS_P alpha_mode_dependent_address_p
9995
9996 #undef TARGET_ASM_FILE_START
9997 #define TARGET_ASM_FILE_START alpha_file_start
9998
9999 #undef TARGET_SCHED_ADJUST_COST
10000 #define TARGET_SCHED_ADJUST_COST alpha_adjust_cost
10001 #undef TARGET_SCHED_ISSUE_RATE
10002 #define TARGET_SCHED_ISSUE_RATE alpha_issue_rate
10003 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
10004 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
10005 alpha_multipass_dfa_lookahead
10006
10007 #undef TARGET_HAVE_TLS
10008 #define TARGET_HAVE_TLS HAVE_AS_TLS
10009
10010 #undef TARGET_BUILTIN_DECL
10011 #define TARGET_BUILTIN_DECL alpha_builtin_decl
10012 #undef TARGET_INIT_BUILTINS
10013 #define TARGET_INIT_BUILTINS alpha_init_builtins
10014 #undef TARGET_EXPAND_BUILTIN
10015 #define TARGET_EXPAND_BUILTIN alpha_expand_builtin
10016 #undef TARGET_FOLD_BUILTIN
10017 #define TARGET_FOLD_BUILTIN alpha_fold_builtin
10018 #undef TARGET_GIMPLE_FOLD_BUILTIN
10019 #define TARGET_GIMPLE_FOLD_BUILTIN alpha_gimple_fold_builtin
10020
10021 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
10022 #define TARGET_FUNCTION_OK_FOR_SIBCALL alpha_function_ok_for_sibcall
10023 #undef TARGET_CANNOT_COPY_INSN_P
10024 #define TARGET_CANNOT_COPY_INSN_P alpha_cannot_copy_insn_p
10025 #undef TARGET_LEGITIMATE_CONSTANT_P
10026 #define TARGET_LEGITIMATE_CONSTANT_P alpha_legitimate_constant_p
10027 #undef TARGET_CANNOT_FORCE_CONST_MEM
10028 #define TARGET_CANNOT_FORCE_CONST_MEM alpha_cannot_force_const_mem
10029
10030 #if TARGET_ABI_OSF
10031 #undef TARGET_ASM_OUTPUT_MI_THUNK
10032 #define TARGET_ASM_OUTPUT_MI_THUNK alpha_output_mi_thunk_osf
10033 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
10034 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
10035 #undef TARGET_STDARG_OPTIMIZE_HOOK
10036 #define TARGET_STDARG_OPTIMIZE_HOOK alpha_stdarg_optimize_hook
10037 #endif
10038
10039 #undef TARGET_PRINT_OPERAND
10040 #define TARGET_PRINT_OPERAND alpha_print_operand
10041 #undef TARGET_PRINT_OPERAND_ADDRESS
10042 #define TARGET_PRINT_OPERAND_ADDRESS alpha_print_operand_address
10043 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
10044 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P alpha_print_operand_punct_valid_p
10045
10046 /* Use 16-bits anchor. */
10047 #undef TARGET_MIN_ANCHOR_OFFSET
10048 #define TARGET_MIN_ANCHOR_OFFSET -0x7fff - 1
10049 #undef TARGET_MAX_ANCHOR_OFFSET
10050 #define TARGET_MAX_ANCHOR_OFFSET 0x7fff
10051 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
10052 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
10053
10054 #undef TARGET_REGISTER_MOVE_COST
10055 #define TARGET_REGISTER_MOVE_COST alpha_register_move_cost
10056 #undef TARGET_MEMORY_MOVE_COST
10057 #define TARGET_MEMORY_MOVE_COST alpha_memory_move_cost
10058 #undef TARGET_RTX_COSTS
10059 #define TARGET_RTX_COSTS alpha_rtx_costs
10060 #undef TARGET_ADDRESS_COST
10061 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
10062
10063 #undef TARGET_MACHINE_DEPENDENT_REORG
10064 #define TARGET_MACHINE_DEPENDENT_REORG alpha_reorg
10065
10066 #undef TARGET_PROMOTE_FUNCTION_MODE
10067 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
10068 #undef TARGET_PROMOTE_PROTOTYPES
10069 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_false
10070
10071 #undef TARGET_FUNCTION_VALUE
10072 #define TARGET_FUNCTION_VALUE alpha_function_value
10073 #undef TARGET_LIBCALL_VALUE
10074 #define TARGET_LIBCALL_VALUE alpha_libcall_value
10075 #undef TARGET_FUNCTION_VALUE_REGNO_P
10076 #define TARGET_FUNCTION_VALUE_REGNO_P alpha_function_value_regno_p
10077 #undef TARGET_RETURN_IN_MEMORY
10078 #define TARGET_RETURN_IN_MEMORY alpha_return_in_memory
10079 #undef TARGET_PASS_BY_REFERENCE
10080 #define TARGET_PASS_BY_REFERENCE alpha_pass_by_reference
10081 #undef TARGET_SETUP_INCOMING_VARARGS
10082 #define TARGET_SETUP_INCOMING_VARARGS alpha_setup_incoming_varargs
10083 #undef TARGET_STRICT_ARGUMENT_NAMING
10084 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
10085 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
10086 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
10087 #undef TARGET_SPLIT_COMPLEX_ARG
10088 #define TARGET_SPLIT_COMPLEX_ARG alpha_split_complex_arg
10089 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
10090 #define TARGET_GIMPLIFY_VA_ARG_EXPR alpha_gimplify_va_arg
10091 #undef TARGET_ARG_PARTIAL_BYTES
10092 #define TARGET_ARG_PARTIAL_BYTES alpha_arg_partial_bytes
10093 #undef TARGET_FUNCTION_ARG
10094 #define TARGET_FUNCTION_ARG alpha_function_arg
10095 #undef TARGET_FUNCTION_ARG_ADVANCE
10096 #define TARGET_FUNCTION_ARG_ADVANCE alpha_function_arg_advance
10097 #undef TARGET_TRAMPOLINE_INIT
10098 #define TARGET_TRAMPOLINE_INIT alpha_trampoline_init
10099
10100 #undef TARGET_INSTANTIATE_DECLS
10101 #define TARGET_INSTANTIATE_DECLS alpha_instantiate_decls
10102
10103 #undef TARGET_SECONDARY_RELOAD
10104 #define TARGET_SECONDARY_RELOAD alpha_secondary_reload
10105 #undef TARGET_SECONDARY_MEMORY_NEEDED
10106 #define TARGET_SECONDARY_MEMORY_NEEDED alpha_secondary_memory_needed
10107 #undef TARGET_SECONDARY_MEMORY_NEEDED_MODE
10108 #define TARGET_SECONDARY_MEMORY_NEEDED_MODE alpha_secondary_memory_needed_mode
10109
10110 #undef TARGET_SCALAR_MODE_SUPPORTED_P
10111 #define TARGET_SCALAR_MODE_SUPPORTED_P alpha_scalar_mode_supported_p
10112 #undef TARGET_VECTOR_MODE_SUPPORTED_P
10113 #define TARGET_VECTOR_MODE_SUPPORTED_P alpha_vector_mode_supported_p
10114
10115 #undef TARGET_BUILD_BUILTIN_VA_LIST
10116 #define TARGET_BUILD_BUILTIN_VA_LIST alpha_build_builtin_va_list
10117
10118 #undef TARGET_EXPAND_BUILTIN_VA_START
10119 #define TARGET_EXPAND_BUILTIN_VA_START alpha_va_start
10120
10121 #undef TARGET_OPTION_OVERRIDE
10122 #define TARGET_OPTION_OVERRIDE alpha_option_override
10123
10124 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
10125 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE \
10126 alpha_override_options_after_change
10127
10128 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
10129 #undef TARGET_MANGLE_TYPE
10130 #define TARGET_MANGLE_TYPE alpha_mangle_type
10131 #endif
10132
10133 #undef TARGET_LRA_P
10134 #define TARGET_LRA_P hook_bool_void_false
10135
10136 #undef TARGET_LEGITIMATE_ADDRESS_P
10137 #define TARGET_LEGITIMATE_ADDRESS_P alpha_legitimate_address_p
10138
10139 #undef TARGET_CONDITIONAL_REGISTER_USAGE
10140 #define TARGET_CONDITIONAL_REGISTER_USAGE alpha_conditional_register_usage
10141
10142 #undef TARGET_CANONICALIZE_COMPARISON
10143 #define TARGET_CANONICALIZE_COMPARISON alpha_canonicalize_comparison
10144
10145 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
10146 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV alpha_atomic_assign_expand_fenv
10147
10148 #undef TARGET_HARD_REGNO_MODE_OK
10149 #define TARGET_HARD_REGNO_MODE_OK alpha_hard_regno_mode_ok
10150
10151 #undef TARGET_MODES_TIEABLE_P
10152 #define TARGET_MODES_TIEABLE_P alpha_modes_tieable_p
10153
10154 #undef TARGET_CAN_CHANGE_MODE_CLASS
10155 #define TARGET_CAN_CHANGE_MODE_CLASS alpha_can_change_mode_class
10156
10157 struct gcc_target targetm = TARGET_INITIALIZER;
10158
10159
10160 #include "gt-alpha.h"
10162