* arm.c (arm_split_constant): Don't try to force a constant to
[official-gcc.git] / gcc / config / arm / arm.c
blob4a393c94e119ab2000c868c109a3ea92b479f7b0
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
4 and Martin Simmons (@harleqn.co.uk).
5 More major hacks by Richard Earnshaw (rearnsha@arm.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "real.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-flags.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "reload.h"
37 #include "tree.h"
38 #include "expr.h"
39 #include "toplev.h"
41 /* The maximum number of insns skipped which will be conditionalised if
42 possible. */
43 static int max_insns_skipped = 5;
45 extern FILE * asm_out_file;
46 /* Some function declarations. */
48 static HOST_WIDE_INT int_log2 PROTO ((HOST_WIDE_INT));
49 static char *output_multi_immediate PROTO ((rtx *, char *, char *, int,
50 HOST_WIDE_INT));
51 static int arm_gen_constant PROTO ((enum rtx_code, enum machine_mode,
52 HOST_WIDE_INT, rtx, rtx, int, int));
53 static int arm_naked_function_p PROTO ((tree));
54 static void init_fpa_table PROTO ((void));
55 static enum machine_mode select_dominance_cc_mode PROTO ((enum rtx_code, rtx,
56 rtx, HOST_WIDE_INT));
57 static HOST_WIDE_INT add_constant PROTO ((rtx, enum machine_mode, int *));
58 static void dump_table PROTO ((rtx));
59 static int fixit PROTO ((rtx, enum machine_mode, int));
60 static rtx find_barrier PROTO ((rtx, int));
61 static int broken_move PROTO ((rtx));
62 static char *fp_const_from_val PROTO ((REAL_VALUE_TYPE *));
63 static int eliminate_lr2ip PROTO ((rtx *));
64 static char *shift_op PROTO ((rtx, HOST_WIDE_INT *));
65 static int pattern_really_clobbers_lr PROTO ((rtx));
66 static int function_really_clobbers_lr PROTO ((rtx));
67 static void emit_multi_reg_push PROTO ((int));
68 static void emit_sfm PROTO ((int, int));
69 static enum arm_cond_code get_arm_condition_code PROTO ((rtx));
71 /* Define the information needed to generate branch insns. This is
72 stored from the compare operation. */
74 rtx arm_compare_op0, arm_compare_op1;
75 int arm_compare_fp;
77 /* What type of floating point are we tuning for? */
78 enum floating_point_type arm_fpu;
80 /* What type of floating point instructions are available? */
81 enum floating_point_type arm_fpu_arch;
83 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode */
84 enum prog_mode_type arm_prgmode;
86 /* Set by the -mfp=... option */
87 char * target_fp_name = NULL;
89 /* Used to parse -mstructure_size_boundary command line option. */
90 char * structure_size_string = NULL;
91 int arm_structure_size_boundary = 32; /* Used to be 8 */
93 /* Nonzero if this is an "M" variant of the processor. */
94 int arm_fast_multiply = 0;
96 /* Nonzero if this chip supports the ARM Architecture 4 extensions */
97 int arm_arch4 = 0;
99 /* Nonzero if this chip can benefit from laod scheduling. */
100 int arm_ld_sched = 0;
102 /* Nonzero if this chip is a StrongARM. */
103 int arm_is_strong = 0;
105 /* Nonzero if this chip is a an ARM6 or an ARM7. */
106 int arm_is_6_or_7 = 0;
108 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
109 must report the mode of the memory reference from PRINT_OPERAND to
110 PRINT_OPERAND_ADDRESS. */
111 enum machine_mode output_memory_reference_mode;
113 /* Nonzero if the prologue must setup `fp'. */
114 int current_function_anonymous_args;
116 /* The register number to be used for the PIC offset register. */
117 int arm_pic_register = 9;
119 /* Location counter of .text segment. */
120 int arm_text_location = 0;
122 /* Set to one if we think that lr is only saved because of subroutine calls,
123 but all of these can be `put after' return insns */
124 int lr_save_eliminated;
126 /* Set to 1 when a return insn is output, this means that the epilogue
127 is not needed. */
129 static int return_used_this_function;
131 static int after_arm_reorg = 0;
133 static int arm_constant_limit = 3;
135 /* For an explanation of these variables, see final_prescan_insn below. */
136 int arm_ccfsm_state;
137 enum arm_cond_code arm_current_cc;
138 rtx arm_target_insn;
139 int arm_target_label;
141 /* The condition codes of the ARM, and the inverse function. */
142 char * arm_condition_codes[] =
144 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
145 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
148 static enum arm_cond_code get_arm_condition_code ();
150 #define streq(string1, string2) (strcmp (string1, string2) == 0)
152 /* Initialization code */
154 #define FL_CO_PROC 0x01 /* Has external co-processor bus */
155 #define FL_FAST_MULT 0x02 /* Fast multiply */
156 #define FL_MODE26 0x04 /* 26-bit mode support */
157 #define FL_MODE32 0x08 /* 32-bit mode support */
158 #define FL_ARCH4 0x10 /* Architecture rel 4 */
159 #define FL_THUMB 0x20 /* Thumb aware */
160 #define FL_LDSCHED 0x40 /* Load scheduling necessary */
161 #define FL_STRONG 0x80 /* StrongARM */
163 struct processors
165 char * name;
166 unsigned int flags;
169 /* Not all of these give usefully different compilation alternatives,
170 but there is no simple way of generalizing them. */
171 static struct processors all_cores[] =
173 /* ARM Cores */
175 {"arm2", FL_CO_PROC | FL_MODE26 },
176 {"arm250", FL_CO_PROC | FL_MODE26 },
177 {"arm3", FL_CO_PROC | FL_MODE26 },
178 {"arm6", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
179 {"arm60", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
180 {"arm600", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
181 {"arm610", FL_MODE26 | FL_MODE32 },
182 {"arm620", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
183 {"arm7", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
184 {"arm7m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT }, /* arm7m doesn't exist on its own, */
185 {"arm7d", FL_CO_PROC | FL_MODE26 | FL_MODE32 }, /* but only with D, (and I), */
186 {"arm7dm", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT }, /* but those don't alter the code, */
187 {"arm7di", FL_CO_PROC | FL_MODE26 | FL_MODE32 }, /* so arm7m is sometimes used. */
188 {"arm7dmi", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
189 {"arm70", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
190 {"arm700", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
191 {"arm700i", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
192 {"arm710", FL_MODE26 | FL_MODE32 },
193 {"arm710c", FL_MODE26 | FL_MODE32 },
194 {"arm7100", FL_MODE26 | FL_MODE32 },
195 {"arm7500", FL_MODE26 | FL_MODE32 },
196 {"arm7500fe", FL_CO_PROC | FL_MODE26 | FL_MODE32 }, /* Doesn't really have an external co-proc, but does have embedded fpu. */
197 {"arm7tdmi", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
198 {"arm8", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
199 {"arm810", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
200 {"arm9", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
201 {"arm9tdmi", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
202 {"strongarm", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
203 {"strongarm110", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
204 {"strongarm1100", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
206 {NULL, 0}
209 static struct processors all_architectures[] =
211 /* ARM Architectures */
213 {"armv2", FL_CO_PROC | FL_MODE26 },
214 {"armv2a", FL_CO_PROC | FL_MODE26 },
215 {"armv3", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
216 {"armv3m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
217 {"armv4", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 },
218 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
219 implementations that support it, so we will leave it out for now. */
220 {"armv4t", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
221 {NULL, 0}
224 /* This is a magic stucture. The 'string' field is magically filled in
225 with a pointer to the value specified by the user on the command line
226 assuming that the user has specified such a value. */
228 struct arm_cpu_select arm_select[] =
230 /* string name processors */
231 { NULL, "-mcpu=", all_cores },
232 { NULL, "-march=", all_architectures },
233 { NULL, "-mtune=", all_cores }
236 /* Fix up any incompatible options that the user has specified.
237 This has now turned into a maze. */
238 void
239 arm_override_options ()
241 unsigned int flags = 0;
242 unsigned i;
243 struct arm_cpu_select * ptr;
245 /* Set up the flags based on the cpu/architecture selected by the user. */
246 for (i = sizeof (arm_select) / sizeof (arm_select[0]); i--;)
248 struct arm_cpu_select * ptr = arm_select + i;
250 if (ptr->string != NULL && ptr->string[0] != '\0')
252 struct processors * sel;
254 for (sel = ptr->processors; sel->name != NULL; sel ++)
255 if (streq (ptr->string, sel->name))
257 if (flags != 0)
259 /* We scan the arm_select array in the order:
260 tune -> arch -> cpu
261 So if we have been asked to tune for, say, an ARM8,
262 but we are told that the cpu is only an ARM6, then
263 we have problems. We detect this by seeing if the
264 flags bits accumulated so far can be supported by the
265 cpu/architecture type now being parsed. If they can,
266 then OR in any new bits. If they cannot then report
267 an error. */
268 if ((flags & sel->flags) != flags)
269 error ("switch %s%s overridden by another switch",
270 ptr->string, sel->name );
273 flags = sel->flags;
275 break;
278 if (sel->name == NULL)
279 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
283 /* If the user did not specify a processor, choose one for them. */
284 if (flags == 0)
286 struct processors * sel;
287 int sought = 0;
289 if (TARGET_THUMB_INTERWORK)
291 sought |= FL_THUMB;
293 /* Force apcs-32 to be used for Thumb targets. */
294 target_flags |= ARM_FLAG_APCS_32;
297 if (! TARGET_APCS_32)
298 sought |= FL_MODE26;
300 if (sought != 0)
302 for (sel = all_cores; sel->name != NULL; sel++)
303 if ((sel->flags & sought) == sought)
305 flags = sel->flags;
306 break;
309 if (sel->name == NULL)
310 fatal ("Unable to select a cpu that matches command line specification");
312 else
314 /* The user did not specify any command line switches that require
315 a certain kind of CPU. Use TARGET_CPU_DEFAULT instead. */
317 static struct cpu_default
319 int cpu;
320 char * name;
322 cpu_defaults[] =
324 { TARGET_CPU_arm2, "arm2" },
325 { TARGET_CPU_arm6, "arm6" },
326 { TARGET_CPU_arm610, "arm610" },
327 { TARGET_CPU_arm7m, "arm7m" },
328 { TARGET_CPU_arm7500fe, "arm7500fe" },
329 { TARGET_CPU_arm7tdmi, "arm7tdmi" },
330 { TARGET_CPU_arm8, "arm8" },
331 { TARGET_CPU_arm810, "arm810" },
332 { TARGET_CPU_arm9, "arm9" },
333 { TARGET_CPU_strongarm, "strongarm" },
334 { TARGET_CPU_generic, "arm" },
335 { 0, 0 }
337 struct cpu_default * def;
339 /* Find the default. */
340 for (def = cpu_defaults; def->name; def ++)
341 if (def->cpu == TARGET_CPU_DEFAULT)
342 break;
344 if (def->name == NULL)
345 abort ();
347 /* Find the default CPU's flags. */
348 for (sel = all_cores; sel->name != NULL; sel ++)
349 if (streq (def->name, sel->name))
350 break;
352 if (sel->name == NULL)
353 abort ();
355 flags = sel->flags;
359 /* Make sure that the processor choice does not conflict with any of the
360 other command line choices. */
361 if (TARGET_APCS_32 && !(flags & FL_MODE32))
363 warning ("target CPU does not support APCS-32" );
364 target_flags &= ~ ARM_FLAG_APCS_32;
366 else if (! TARGET_APCS_32 && !(flags & FL_MODE26))
368 warning ("target CPU does not support APCS-26" );
369 target_flags |= ARM_FLAG_APCS_32;
372 if (TARGET_THUMB_INTERWORK && !(flags & FL_THUMB))
374 warning ("target CPU does not support interworking" );
375 target_flags &= ~ARM_FLAG_THUMB;
378 /* If interworking is enabled then APCS-32 must be selected as well. */
379 if (TARGET_THUMB_INTERWORK)
381 if (! TARGET_APCS_32)
382 warning ("interworking forces APCS-32 to be used" );
383 target_flags |= ARM_FLAG_APCS_32;
386 if (TARGET_APCS_STACK && ! TARGET_APCS)
388 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
389 target_flags |= ARM_FLAG_APCS_FRAME;
392 if (write_symbols != NO_DEBUG && flag_omit_frame_pointer)
393 warning ("-g with -fomit-frame-pointer may not give sensible debugging");
395 if (TARGET_POKE_FUNCTION_NAME)
396 target_flags |= ARM_FLAG_APCS_FRAME;
398 if (TARGET_APCS_REENT && flag_pic)
399 fatal ("-fpic and -mapcs-reent are incompatible");
401 if (TARGET_APCS_REENT)
402 warning ("APCS reentrant code not supported. Ignored");
404 /* If stack checking is disabled, we can use r10 as the PIC register,
405 which keeps r9 available. */
406 if (flag_pic && ! TARGET_APCS_STACK)
407 arm_pic_register = 10;
409 /* Well, I'm about to have a go, but pic is NOT going to be compatible
410 with APCS reentrancy, since that requires too much support in the
411 assembler and linker, and the ARMASM assembler seems to lack some
412 required directives. */
413 if (flag_pic)
414 warning ("Position independent code not supported");
416 if (TARGET_APCS_FLOAT)
417 warning ("Passing floating point arguments in fp regs not yet supported");
419 /* Initialise booleans used elsewhere in this file, and in arm.md */
420 arm_fast_multiply = (flags & FL_FAST_MULT) != 0;
421 arm_arch4 = (flags & FL_ARCH4) != 0;
422 arm_ld_sched = (flags & FL_LDSCHED) != 0;
423 arm_is_strong = (flags & FL_STRONG);
425 /* The arm.md file needs to know if theprocessor is an ARM6 or an ARM7 */
426 arm_is_6_or_7 = ((flags & (FL_MODE26 | FL_MODE32)) && !(flags & FL_ARCH4));
428 /* Default value for floating point code... if no co-processor
429 bus, then schedule for emulated floating point. Otherwise,
430 assume the user has an FPA.
431 Note: this does not prevent use of floating point instructions,
432 -msoft-float does that. */
433 if ((flags & FL_CO_PROC) == 0)
434 arm_fpu = FP_SOFT3;
435 else
436 arm_fpu = FP_HARD;
438 if (target_fp_name)
440 if (streq (target_fp_name, "2"))
441 arm_fpu_arch = FP_SOFT2;
442 else if (streq (target_fp_name, "3"))
443 arm_fpu_arch = FP_SOFT3;
444 else
445 fatal ("Invalid floating point emulation option: -mfpe-%s",
446 target_fp_name);
448 else
449 arm_fpu_arch = FP_DEFAULT;
451 if (TARGET_FPE && arm_fpu != FP_HARD)
452 arm_fpu = FP_SOFT2;
454 /* For arm2/3 there is no need to do any scheduling if there is only
455 a floating point emulator, or we are doing software floating-point. */
456 if ((TARGET_SOFT_FLOAT || arm_fpu != FP_HARD) && (flags & FL_MODE32) == 0)
457 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
459 arm_prog_mode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
461 if (structure_size_string != NULL)
463 int size = strtol (structure_size_string, NULL, 0);
465 if (size == 8 || size == 32)
466 arm_structure_size_boundary = size;
467 else
468 warning ("Structure size boundary can only be set to 8 or 32");
471 /* If optimizing for space, don't synthesize constants.
472 For processors with load scheduling, it never costs more than 2 cycles
473 to load a constant, and the load scheduler may well reduce that to 1. */
474 if (optimize_size || (flags & FL_LDSCHED))
475 arm_constant_limit = 1;
477 /* If optimizing for size, bump the number of instructions that we
478 are prepared to conditionally execute (even on a StrongARM).
479 Otherwise for the StrongARM, which has early execution of branches,
480 a sequence that is worth skipping is shorter. */
481 if (optimize_size)
482 max_insns_skipped = 6;
483 else if (arm_is_strong)
484 max_insns_skipped = 3;
487 /* Return 1 if it is possible to return using a single instruction */
490 use_return_insn (iscond)
491 int iscond;
493 int regno;
495 if (!reload_completed
496 || current_function_pretend_args_size
497 || current_function_anonymous_args
498 || ((get_frame_size () + current_function_outgoing_args_size != 0)
499 && !(TARGET_APCS && frame_pointer_needed)))
500 return 0;
502 /* Can't be done if interworking with Thumb, and any registers have been
503 stacked. Similarly, on StrongARM, conditional returns are expensive
504 if they aren't taken and registers have been stacked. */
505 if (iscond && arm_is_strong && frame_pointer_needed)
506 return 0;
507 if ((iscond && arm_is_strong)
508 || TARGET_THUMB_INTERWORK)
509 for (regno = 0; regno < 16; regno++)
510 if (regs_ever_live[regno] && ! call_used_regs[regno])
511 return 0;
513 /* Can't be done if any of the FPU regs are pushed, since this also
514 requires an insn */
515 for (regno = 16; regno < 24; regno++)
516 if (regs_ever_live[regno] && ! call_used_regs[regno])
517 return 0;
519 /* If a function is naked, don't use the "return" insn. */
520 if (arm_naked_function_p (current_function_decl))
521 return 0;
523 return 1;
526 /* Return TRUE if int I is a valid immediate ARM constant. */
529 const_ok_for_arm (i)
530 HOST_WIDE_INT i;
532 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
534 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
535 be all zero, or all one. */
536 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
537 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
538 != ((~(unsigned HOST_WIDE_INT) 0)
539 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
540 return FALSE;
542 /* Fast return for 0 and powers of 2 */
543 if ((i & (i - 1)) == 0)
544 return TRUE;
548 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
549 return TRUE;
550 mask =
551 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
552 >> (32 - 2)) | ~((unsigned HOST_WIDE_INT) 0xffffffff);
553 } while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
555 return FALSE;
558 /* Return true if I is a valid constant for the operation CODE. */
560 const_ok_for_op (i, code, mode)
561 HOST_WIDE_INT i;
562 enum rtx_code code;
563 enum machine_mode mode;
565 if (const_ok_for_arm (i))
566 return 1;
568 switch (code)
570 case PLUS:
571 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
573 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
574 case XOR:
575 case IOR:
576 return 0;
578 case AND:
579 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
581 default:
582 abort ();
586 /* Emit a sequence of insns to handle a large constant.
587 CODE is the code of the operation required, it can be any of SET, PLUS,
588 IOR, AND, XOR, MINUS;
589 MODE is the mode in which the operation is being performed;
590 VAL is the integer to operate on;
591 SOURCE is the other operand (a register, or a null-pointer for SET);
592 SUBTARGETS means it is safe to create scratch registers if that will
593 either produce a simpler sequence, or we will want to cse the values.
594 Return value is the number of insns emitted. */
597 arm_split_constant (code, mode, val, target, source, subtargets)
598 enum rtx_code code;
599 enum machine_mode mode;
600 HOST_WIDE_INT val;
601 rtx target;
602 rtx source;
603 int subtargets;
605 if (subtargets || code == SET
606 || (GET_CODE (target) == REG && GET_CODE (source) == REG
607 && REGNO (target) != REGNO (source)))
609 /* After arm_reorg has been called, we can't fix up expensive
610 constants by pushing them into memory so we must synthesise
611 them in-line, regardless of the cost. This is only likely to
612 be more costly on chips that have load delay slots and we are
613 compiling without running the scheduler (so no splitting
614 occurred before the final instruction emission. */
615 if (! after_arm_reorg
616 && (arm_gen_constant (code, mode, val, target, source, 1, 0)
617 > arm_constant_limit + (code != SET)))
619 if (code == SET)
621 /* Currently SET is the only monadic value for CODE, all
622 the rest are diadic. */
623 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
624 return 1;
626 else
628 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
630 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
631 /* For MINUS, the value is subtracted from, since we never
632 have subtraction of a constant. */
633 if (code == MINUS)
634 emit_insn (gen_rtx_SET (VOIDmode, target,
635 gen_rtx (code, mode, temp, source)));
636 else
637 emit_insn (gen_rtx_SET (VOIDmode, target,
638 gen_rtx (code, mode, source, temp)));
639 return 2;
644 return arm_gen_constant (code, mode, val, target, source, subtargets, 1);
647 /* As above, but extra parameter GENERATE which, if clear, suppresses
648 RTL generation. */
650 arm_gen_constant (code, mode, val, target, source, subtargets, generate)
651 enum rtx_code code;
652 enum machine_mode mode;
653 HOST_WIDE_INT val;
654 rtx target;
655 rtx source;
656 int subtargets;
657 int generate;
659 int can_invert = 0;
660 int can_negate = 0;
661 int can_negate_initial = 0;
662 int can_shift = 0;
663 int i;
664 int num_bits_set = 0;
665 int set_sign_bit_copies = 0;
666 int clear_sign_bit_copies = 0;
667 int clear_zero_bit_copies = 0;
668 int set_zero_bit_copies = 0;
669 int insns = 0;
670 unsigned HOST_WIDE_INT temp1, temp2;
671 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
673 /* find out which operations are safe for a given CODE. Also do a quick
674 check for degenerate cases; these can occur when DImode operations
675 are split. */
676 switch (code)
678 case SET:
679 can_invert = 1;
680 can_shift = 1;
681 can_negate = 1;
682 break;
684 case PLUS:
685 can_negate = 1;
686 can_negate_initial = 1;
687 break;
689 case IOR:
690 if (remainder == 0xffffffff)
692 if (generate)
693 emit_insn (gen_rtx_SET (VOIDmode, target,
694 GEN_INT (ARM_SIGN_EXTEND (val))));
695 return 1;
697 if (remainder == 0)
699 if (reload_completed && rtx_equal_p (target, source))
700 return 0;
701 if (generate)
702 emit_insn (gen_rtx_SET (VOIDmode, target, source));
703 return 1;
705 break;
707 case AND:
708 if (remainder == 0)
710 if (generate)
711 emit_insn (gen_rtx_SET (VOIDmode, target, const0_rtx));
712 return 1;
714 if (remainder == 0xffffffff)
716 if (reload_completed && rtx_equal_p (target, source))
717 return 0;
718 if (generate)
719 emit_insn (gen_rtx_SET (VOIDmode, target, source));
720 return 1;
722 can_invert = 1;
723 break;
725 case XOR:
726 if (remainder == 0)
728 if (reload_completed && rtx_equal_p (target, source))
729 return 0;
730 if (generate)
731 emit_insn (gen_rtx_SET (VOIDmode, target, source));
732 return 1;
734 if (remainder == 0xffffffff)
736 if (generate)
737 emit_insn (gen_rtx_SET (VOIDmode, target,
738 gen_rtx_NOT (mode, source)));
739 return 1;
742 /* We don't know how to handle this yet below. */
743 abort ();
745 case MINUS:
746 /* We treat MINUS as (val - source), since (source - val) is always
747 passed as (source + (-val)). */
748 if (remainder == 0)
750 if (generate)
751 emit_insn (gen_rtx_SET (VOIDmode, target,
752 gen_rtx_NEG (mode, source)));
753 return 1;
755 if (const_ok_for_arm (val))
757 if (generate)
758 emit_insn (gen_rtx_SET (VOIDmode, target,
759 gen_rtx_MINUS (mode, GEN_INT (val),
760 source)));
761 return 1;
763 can_negate = 1;
765 break;
767 default:
768 abort ();
771 /* If we can do it in one insn get out quickly */
772 if (const_ok_for_arm (val)
773 || (can_negate_initial && const_ok_for_arm (-val))
774 || (can_invert && const_ok_for_arm (~val)))
776 if (generate)
777 emit_insn (gen_rtx_SET (VOIDmode, target,
778 (source ? gen_rtx (code, mode, source,
779 GEN_INT (val))
780 : GEN_INT (val))));
781 return 1;
785 /* Calculate a few attributes that may be useful for specific
786 optimizations. */
788 for (i = 31; i >= 0; i--)
790 if ((remainder & (1 << i)) == 0)
791 clear_sign_bit_copies++;
792 else
793 break;
796 for (i = 31; i >= 0; i--)
798 if ((remainder & (1 << i)) != 0)
799 set_sign_bit_copies++;
800 else
801 break;
804 for (i = 0; i <= 31; i++)
806 if ((remainder & (1 << i)) == 0)
807 clear_zero_bit_copies++;
808 else
809 break;
812 for (i = 0; i <= 31; i++)
814 if ((remainder & (1 << i)) != 0)
815 set_zero_bit_copies++;
816 else
817 break;
820 switch (code)
822 case SET:
823 /* See if we can do this by sign_extending a constant that is known
824 to be negative. This is a good, way of doing it, since the shift
825 may well merge into a subsequent insn. */
826 if (set_sign_bit_copies > 1)
828 if (const_ok_for_arm
829 (temp1 = ARM_SIGN_EXTEND (remainder
830 << (set_sign_bit_copies - 1))))
832 if (generate)
834 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
835 emit_insn (gen_rtx_SET (VOIDmode, new_src,
836 GEN_INT (temp1)));
837 emit_insn (gen_ashrsi3 (target, new_src,
838 GEN_INT (set_sign_bit_copies - 1)));
840 return 2;
842 /* For an inverted constant, we will need to set the low bits,
843 these will be shifted out of harm's way. */
844 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
845 if (const_ok_for_arm (~temp1))
847 if (generate)
849 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
850 emit_insn (gen_rtx_SET (VOIDmode, new_src,
851 GEN_INT (temp1)));
852 emit_insn (gen_ashrsi3 (target, new_src,
853 GEN_INT (set_sign_bit_copies - 1)));
855 return 2;
859 /* See if we can generate this by setting the bottom (or the top)
860 16 bits, and then shifting these into the other half of the
861 word. We only look for the simplest cases, to do more would cost
862 too much. Be careful, however, not to generate this when the
863 alternative would take fewer insns. */
864 if (val & 0xffff0000)
866 temp1 = remainder & 0xffff0000;
867 temp2 = remainder & 0x0000ffff;
869 /* Overlaps outside this range are best done using other methods. */
870 for (i = 9; i < 24; i++)
872 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
873 && ! const_ok_for_arm (temp2))
875 rtx new_src = (subtargets
876 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
877 : target);
878 insns = arm_gen_constant (code, mode, temp2, new_src,
879 source, subtargets, generate);
880 source = new_src;
881 if (generate)
882 emit_insn (gen_rtx_SET
883 (VOIDmode, target,
884 gen_rtx_IOR (mode,
885 gen_rtx_ASHIFT (mode, source,
886 GEN_INT (i)),
887 source)));
888 return insns + 1;
892 /* Don't duplicate cases already considered. */
893 for (i = 17; i < 24; i++)
895 if (((temp1 | (temp1 >> i)) == remainder)
896 && ! const_ok_for_arm (temp1))
898 rtx new_src = (subtargets
899 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
900 : target);
901 insns = arm_gen_constant (code, mode, temp1, new_src,
902 source, subtargets, generate);
903 source = new_src;
904 if (generate)
905 emit_insn
906 (gen_rtx_SET (VOIDmode, target,
907 gen_rtx_IOR
908 (mode,
909 gen_rtx_LSHIFTRT (mode, source,
910 GEN_INT (i)),
911 source)));
912 return insns + 1;
916 break;
918 case IOR:
919 case XOR:
920 /* If we have IOR or XOR, and the constant can be loaded in a
921 single instruction, and we can find a temporary to put it in,
922 then this can be done in two instructions instead of 3-4. */
923 if (subtargets
924 /* TARGET can't be NULL if SUBTARGETS is 0 */
925 || (reload_completed && ! reg_mentioned_p (target, source)))
927 if (const_ok_for_arm (ARM_SIGN_EXTEND (~ val)))
929 if (generate)
931 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
933 emit_insn (gen_rtx_SET (VOIDmode, sub, GEN_INT (val)));
934 emit_insn (gen_rtx_SET (VOIDmode, target,
935 gen_rtx (code, mode, source, sub)));
937 return 2;
941 if (code == XOR)
942 break;
944 if (set_sign_bit_copies > 8
945 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
947 if (generate)
949 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
950 rtx shift = GEN_INT (set_sign_bit_copies);
952 emit_insn (gen_rtx_SET (VOIDmode, sub,
953 gen_rtx_NOT (mode,
954 gen_rtx_ASHIFT (mode,
955 source,
956 shift))));
957 emit_insn (gen_rtx_SET (VOIDmode, target,
958 gen_rtx_NOT (mode,
959 gen_rtx_LSHIFTRT (mode, sub,
960 shift))));
962 return 2;
965 if (set_zero_bit_copies > 8
966 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
968 if (generate)
970 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
971 rtx shift = GEN_INT (set_zero_bit_copies);
973 emit_insn (gen_rtx_SET (VOIDmode, sub,
974 gen_rtx_NOT (mode,
975 gen_rtx_LSHIFTRT (mode,
976 source,
977 shift))));
978 emit_insn (gen_rtx_SET (VOIDmode, target,
979 gen_rtx_NOT (mode,
980 gen_rtx_ASHIFT (mode, sub,
981 shift))));
983 return 2;
986 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~ val)))
988 if (generate)
990 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
991 emit_insn (gen_rtx_SET (VOIDmode, sub,
992 gen_rtx_NOT (mode, source)));
993 source = sub;
994 if (subtargets)
995 sub = gen_reg_rtx (mode);
996 emit_insn (gen_rtx_SET (VOIDmode, sub,
997 gen_rtx_AND (mode, source,
998 GEN_INT (temp1))));
999 emit_insn (gen_rtx_SET (VOIDmode, target,
1000 gen_rtx_NOT (mode, sub)));
1002 return 3;
1004 break;
1006 case AND:
1007 /* See if two shifts will do 2 or more insn's worth of work. */
1008 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1010 HOST_WIDE_INT shift_mask = ((0xffffffff
1011 << (32 - clear_sign_bit_copies))
1012 & 0xffffffff);
1014 if ((remainder | shift_mask) != 0xffffffff)
1016 if (generate)
1018 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1019 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1020 new_src, source, subtargets, 1);
1021 source = new_src;
1023 else
1025 rtx targ = subtargets ? NULL_RTX : target;
1026 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1027 targ, source, subtargets, 0);
1031 if (generate)
1033 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1034 rtx shift = GEN_INT (clear_sign_bit_copies);
1036 emit_insn (gen_ashlsi3 (new_src, source, shift));
1037 emit_insn (gen_lshrsi3 (target, new_src, shift));
1040 return insns + 2;
1043 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1045 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1047 if ((remainder | shift_mask) != 0xffffffff)
1049 if (generate)
1051 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1053 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1054 new_src, source, subtargets, 1);
1055 source = new_src;
1057 else
1059 rtx targ = subtargets ? NULL_RTX : target;
1061 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1062 targ, source, subtargets, 0);
1066 if (generate)
1068 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1069 rtx shift = GEN_INT (clear_zero_bit_copies);
1071 emit_insn (gen_lshrsi3 (new_src, source, shift));
1072 emit_insn (gen_ashlsi3 (target, new_src, shift));
1075 return insns + 2;
1078 break;
1080 default:
1081 break;
1084 for (i = 0; i < 32; i++)
1085 if (remainder & (1 << i))
1086 num_bits_set++;
1088 if (code == AND || (can_invert && num_bits_set > 16))
1089 remainder = (~remainder) & 0xffffffff;
1090 else if (code == PLUS && num_bits_set > 16)
1091 remainder = (-remainder) & 0xffffffff;
1092 else
1094 can_invert = 0;
1095 can_negate = 0;
1098 /* Now try and find a way of doing the job in either two or three
1099 instructions.
1100 We start by looking for the largest block of zeros that are aligned on
1101 a 2-bit boundary, we then fill up the temps, wrapping around to the
1102 top of the word when we drop off the bottom.
1103 In the worst case this code should produce no more than four insns. */
1105 int best_start = 0;
1106 int best_consecutive_zeros = 0;
1108 for (i = 0; i < 32; i += 2)
1110 int consecutive_zeros = 0;
1112 if (! (remainder & (3 << i)))
1114 while ((i < 32) && ! (remainder & (3 << i)))
1116 consecutive_zeros += 2;
1117 i += 2;
1119 if (consecutive_zeros > best_consecutive_zeros)
1121 best_consecutive_zeros = consecutive_zeros;
1122 best_start = i - consecutive_zeros;
1124 i -= 2;
1128 /* Now start emitting the insns, starting with the one with the highest
1129 bit set: we do this so that the smallest number will be emitted last;
1130 this is more likely to be combinable with addressing insns. */
1131 i = best_start;
1134 int end;
1136 if (i <= 0)
1137 i += 32;
1138 if (remainder & (3 << (i - 2)))
1140 end = i - 8;
1141 if (end < 0)
1142 end += 32;
1143 temp1 = remainder & ((0x0ff << end)
1144 | ((i < end) ? (0xff >> (32 - end)) : 0));
1145 remainder &= ~temp1;
1147 if (generate)
1149 rtx new_src;
1151 if (code == SET)
1152 emit_insn (gen_rtx_SET (VOIDmode,
1153 new_src = (subtargets
1154 ? gen_reg_rtx (mode)
1155 : target),
1156 GEN_INT (can_invert
1157 ? ~temp1 : temp1)));
1158 else if (code == MINUS)
1159 emit_insn (gen_rtx_SET (VOIDmode,
1160 new_src = (subtargets
1161 ? gen_reg_rtx (mode)
1162 : target),
1163 gen_rtx (code, mode, GEN_INT (temp1),
1164 source)));
1165 else
1166 emit_insn (gen_rtx_SET (VOIDmode,
1167 new_src = (remainder
1168 ? (subtargets
1169 ? gen_reg_rtx (mode)
1170 : target)
1171 : target),
1172 gen_rtx (code, mode, source,
1173 GEN_INT (can_invert ? ~temp1
1174 : (can_negate
1175 ? -temp1
1176 : temp1)))));
1177 source = new_src;
1180 if (code == SET)
1182 can_invert = 0;
1183 code = PLUS;
1185 else if (code == MINUS)
1186 code = PLUS;
1188 insns++;
1189 i -= 6;
1191 i -= 2;
1192 } while (remainder);
1194 return insns;
1197 /* Canonicalize a comparison so that we are more likely to recognize it.
1198 This can be done for a few constant compares, where we can make the
1199 immediate value easier to load. */
1200 enum rtx_code
1201 arm_canonicalize_comparison (code, op1)
1202 enum rtx_code code;
1203 rtx *op1;
1205 unsigned HOST_WIDE_INT i = INTVAL (*op1);
1207 switch (code)
1209 case EQ:
1210 case NE:
1211 return code;
1213 case GT:
1214 case LE:
1215 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1216 - 1)
1217 && (const_ok_for_arm (i+1) || const_ok_for_arm (- (i+1))))
1219 *op1 = GEN_INT (i+1);
1220 return code == GT ? GE : LT;
1222 break;
1224 case GE:
1225 case LT:
1226 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1227 && (const_ok_for_arm (i-1) || const_ok_for_arm (- (i-1))))
1229 *op1 = GEN_INT (i-1);
1230 return code == GE ? GT : LE;
1232 break;
1234 case GTU:
1235 case LEU:
1236 if (i != ~((unsigned HOST_WIDE_INT) 0)
1237 && (const_ok_for_arm (i+1) || const_ok_for_arm (- (i+1))))
1239 *op1 = GEN_INT (i + 1);
1240 return code == GTU ? GEU : LTU;
1242 break;
1244 case GEU:
1245 case LTU:
1246 if (i != 0
1247 && (const_ok_for_arm (i - 1) || const_ok_for_arm (- (i - 1))))
1249 *op1 = GEN_INT (i - 1);
1250 return code == GEU ? GTU : LEU;
1252 break;
1254 default:
1255 abort ();
1258 return code;
1261 /* Decide whether a type should be returned in memory (true)
1262 or in a register (false). This is called by the macro
1263 RETURN_IN_MEMORY. */
1265 arm_return_in_memory (type)
1266 tree type;
1268 if (! AGGREGATE_TYPE_P (type))
1270 /* All simple types are returned in registers. */
1271 return 0;
1273 else if (int_size_in_bytes (type) > 4)
1275 /* All structures/unions bigger than one word are returned in memory. */
1276 return 1;
1278 else if (TREE_CODE (type) == RECORD_TYPE)
1280 tree field;
1282 /* For a struct the APCS says that we must return in a register if
1283 every addressable element has an offset of zero. For practical
1284 purposes this means that the structure can have at most one non
1285 bit-field element and that this element must be the first one in
1286 the structure. */
1288 /* Find the first field, ignoring non FIELD_DECL things which will
1289 have been created by C++. */
1290 for (field = TYPE_FIELDS (type);
1291 field && TREE_CODE (field) != FIELD_DECL;
1292 field = TREE_CHAIN (field))
1293 continue;
1295 if (field == NULL)
1296 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
1298 /* Now check the remaining fields, if any. */
1299 for (field = TREE_CHAIN (field);
1300 field;
1301 field = TREE_CHAIN (field))
1303 if (TREE_CODE (field) != FIELD_DECL)
1304 continue;
1306 if (! DECL_BIT_FIELD_TYPE (field))
1307 return 1;
1310 return 0;
1312 else if (TREE_CODE (type) == UNION_TYPE)
1314 tree field;
1316 /* Unions can be returned in registers if every element is
1317 integral, or can be returned in an integer register. */
1318 for (field = TYPE_FIELDS (type);
1319 field;
1320 field = TREE_CHAIN (field))
1322 if (TREE_CODE (field) != FIELD_DECL)
1323 continue;
1325 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1326 return 1;
1328 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1329 return 1;
1332 return 0;
1335 /* XXX Not sure what should be done for other aggregates, so put them in
1336 memory. */
1337 return 1;
1341 legitimate_pic_operand_p (x)
1342 rtx x;
1344 if (CONSTANT_P (x) && flag_pic
1345 && (GET_CODE (x) == SYMBOL_REF
1346 || (GET_CODE (x) == CONST
1347 && GET_CODE (XEXP (x, 0)) == PLUS
1348 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
1349 return 0;
1351 return 1;
1355 legitimize_pic_address (orig, mode, reg)
1356 rtx orig;
1357 enum machine_mode mode;
1358 rtx reg;
1360 if (GET_CODE (orig) == SYMBOL_REF)
1362 rtx pic_ref, address;
1363 rtx insn;
1364 int subregs = 0;
1366 if (reg == 0)
1368 if (reload_in_progress || reload_completed)
1369 abort ();
1370 else
1371 reg = gen_reg_rtx (Pmode);
1373 subregs = 1;
1376 #ifdef AOF_ASSEMBLER
1377 /* The AOF assembler can generate relocations for these directly, and
1378 understands that the PIC register has to be added into the offset.
1380 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
1381 #else
1382 if (subregs)
1383 address = gen_reg_rtx (Pmode);
1384 else
1385 address = reg;
1387 emit_insn (gen_pic_load_addr (address, orig));
1389 pic_ref = gen_rtx_MEM (Pmode,
1390 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
1391 address));
1392 RTX_UNCHANGING_P (pic_ref) = 1;
1393 insn = emit_move_insn (reg, pic_ref);
1394 #endif
1395 current_function_uses_pic_offset_table = 1;
1396 /* Put a REG_EQUAL note on this insn, so that it can be optimized
1397 by loop. */
1398 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
1399 REG_NOTES (insn));
1400 return reg;
1402 else if (GET_CODE (orig) == CONST)
1404 rtx base, offset;
1406 if (GET_CODE (XEXP (orig, 0)) == PLUS
1407 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1408 return orig;
1410 if (reg == 0)
1412 if (reload_in_progress || reload_completed)
1413 abort ();
1414 else
1415 reg = gen_reg_rtx (Pmode);
1418 if (GET_CODE (XEXP (orig, 0)) == PLUS)
1420 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1421 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1422 base == reg ? 0 : reg);
1424 else
1425 abort ();
1427 if (GET_CODE (offset) == CONST_INT)
1429 /* The base register doesn't really matter, we only want to
1430 test the index for the appropriate mode. */
1431 GO_IF_LEGITIMATE_INDEX (mode, 0, offset, win);
1433 if (! reload_in_progress && ! reload_completed)
1434 offset = force_reg (Pmode, offset);
1435 else
1436 abort ();
1438 win:
1439 if (GET_CODE (offset) == CONST_INT)
1440 return plus_constant_for_output (base, INTVAL (offset));
1443 if (GET_MODE_SIZE (mode) > 4
1444 && (GET_MODE_CLASS (mode) == MODE_INT
1445 || TARGET_SOFT_FLOAT))
1447 emit_insn (gen_addsi3 (reg, base, offset));
1448 return reg;
1451 return gen_rtx_PLUS (Pmode, base, offset);
1453 else if (GET_CODE (orig) == LABEL_REF)
1454 current_function_uses_pic_offset_table = 1;
1456 return orig;
1459 static rtx pic_rtx;
1462 is_pic(x)
1463 rtx x;
1465 if (x == pic_rtx)
1466 return 1;
1467 return 0;
1470 void
1471 arm_finalize_pic ()
1473 #ifndef AOF_ASSEMBLER
1474 rtx l1, pic_tmp, pic_tmp2, seq;
1475 rtx global_offset_table;
1477 if (current_function_uses_pic_offset_table == 0)
1478 return;
1480 if (! flag_pic)
1481 abort ();
1483 start_sequence ();
1484 l1 = gen_label_rtx ();
1486 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1487 /* On the ARM the PC register contains 'dot + 8' at the time of the
1488 addition. */
1489 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), 8);
1490 pic_tmp2 = gen_rtx_CONST (VOIDmode,
1491 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
1493 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
1495 emit_insn (gen_pic_load_addr (pic_offset_table_rtx, pic_rtx));
1496 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
1498 seq = gen_sequence ();
1499 end_sequence ();
1500 emit_insn_after (seq, get_insns ());
1502 /* Need to emit this whether or not we obey regdecls,
1503 since setjmp/longjmp can cause life info to screw up. */
1504 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
1505 #endif /* AOF_ASSEMBLER */
1508 #define REG_OR_SUBREG_REG(X) \
1509 (GET_CODE (X) == REG \
1510 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
1512 #define REG_OR_SUBREG_RTX(X) \
1513 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
1515 #define ARM_FRAME_RTX(X) \
1516 ((X) == frame_pointer_rtx || (X) == stack_pointer_rtx \
1517 || (X) == arg_pointer_rtx)
1520 arm_rtx_costs (x, code, outer_code)
1521 rtx x;
1522 enum rtx_code code, outer_code;
1524 enum machine_mode mode = GET_MODE (x);
1525 enum rtx_code subcode;
1526 int extra_cost;
1528 switch (code)
1530 case MEM:
1531 /* Memory costs quite a lot for the first word, but subsequent words
1532 load at the equivalent of a single insn each. */
1533 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
1534 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
1536 case DIV:
1537 case MOD:
1538 return 100;
1540 case ROTATE:
1541 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
1542 return 4;
1543 /* Fall through */
1544 case ROTATERT:
1545 if (mode != SImode)
1546 return 8;
1547 /* Fall through */
1548 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
1549 if (mode == DImode)
1550 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
1551 + ((GET_CODE (XEXP (x, 0)) == REG
1552 || (GET_CODE (XEXP (x, 0)) == SUBREG
1553 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
1554 ? 0 : 8));
1555 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
1556 || (GET_CODE (XEXP (x, 0)) == SUBREG
1557 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
1558 ? 0 : 4)
1559 + ((GET_CODE (XEXP (x, 1)) == REG
1560 || (GET_CODE (XEXP (x, 1)) == SUBREG
1561 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
1562 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
1563 ? 0 : 4));
1565 case MINUS:
1566 if (mode == DImode)
1567 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
1568 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
1569 || (GET_CODE (XEXP (x, 0)) == CONST_INT
1570 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
1571 ? 0 : 8));
1573 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1574 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1575 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
1576 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
1577 ? 0 : 8)
1578 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
1579 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
1580 && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
1581 ? 0 : 8));
1583 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
1584 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
1585 && REG_OR_SUBREG_REG (XEXP (x, 1))))
1586 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
1587 || subcode == ASHIFTRT || subcode == LSHIFTRT
1588 || subcode == ROTATE || subcode == ROTATERT
1589 || (subcode == MULT
1590 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
1591 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
1592 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
1593 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
1594 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
1595 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
1596 && REG_OR_SUBREG_REG (XEXP (x, 0))))
1597 return 1;
1598 /* Fall through */
1600 case PLUS:
1601 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1602 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
1603 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1604 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
1605 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
1606 ? 0 : 8));
1608 /* Fall through */
1609 case AND: case XOR: case IOR:
1610 extra_cost = 0;
1612 /* Normally the frame registers will be spilt into reg+const during
1613 reload, so it is a bad idea to combine them with other instructions,
1614 since then they might not be moved outside of loops. As a compromise
1615 we allow integration with ops that have a constant as their second
1616 operand. */
1617 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
1618 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
1619 && GET_CODE (XEXP (x, 1)) != CONST_INT)
1620 || (REG_OR_SUBREG_REG (XEXP (x, 0))
1621 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
1622 extra_cost = 4;
1624 if (mode == DImode)
1625 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
1626 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1627 || (GET_CODE (XEXP (x, 1)) == CONST_INT
1628 && const_ok_for_op (INTVAL (XEXP (x, 1)), code, mode)))
1629 ? 0 : 8));
1631 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
1632 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
1633 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1634 || (GET_CODE (XEXP (x, 1)) == CONST_INT
1635 && const_ok_for_op (INTVAL (XEXP (x, 1)), code, mode)))
1636 ? 0 : 4));
1638 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
1639 return (1 + extra_cost
1640 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
1641 || subcode == LSHIFTRT || subcode == ASHIFTRT
1642 || subcode == ROTATE || subcode == ROTATERT
1643 || (subcode == MULT
1644 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1645 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
1646 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
1647 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
1648 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
1649 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
1650 ? 0 : 4));
1652 return 8;
1654 case MULT:
1655 /* There is no point basing this on the tuning, since it is always the
1656 fast variant if it exists at all */
1657 if (arm_fast_multiply && mode == DImode
1658 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
1659 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
1660 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
1661 return 8;
1663 if (GET_MODE_CLASS (mode) == MODE_FLOAT
1664 || mode == DImode)
1665 return 30;
1667 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
1669 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
1670 & (unsigned HOST_WIDE_INT) 0xffffffff);
1671 int add_cost = const_ok_for_arm (i) ? 4 : 8;
1672 int j;
1673 /* Tune as appropriate */
1674 int booth_unit_size = (arm_fast_multiply ? 8 : 2);
1676 for (j = 0; i && j < 32; j += booth_unit_size)
1678 i >>= booth_unit_size;
1679 add_cost += 2;
1682 return add_cost;
1685 return ((arm_fast_multiply ? 8 : 30)
1686 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
1687 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
1689 case TRUNCATE:
1690 if (arm_fast_multiply && mode == SImode
1691 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
1692 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
1693 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
1694 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
1695 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
1696 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
1697 return 8;
1698 return 99;
1700 case NEG:
1701 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1702 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
1703 /* Fall through */
1704 case NOT:
1705 if (mode == DImode)
1706 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
1708 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
1710 case IF_THEN_ELSE:
1711 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
1712 return 14;
1713 return 2;
1715 case COMPARE:
1716 return 1;
1718 case ABS:
1719 return 4 + (mode == DImode ? 4 : 0);
1721 case SIGN_EXTEND:
1722 if (GET_MODE (XEXP (x, 0)) == QImode)
1723 return (4 + (mode == DImode ? 4 : 0)
1724 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
1725 /* Fall through */
1726 case ZERO_EXTEND:
1727 switch (GET_MODE (XEXP (x, 0)))
1729 case QImode:
1730 return (1 + (mode == DImode ? 4 : 0)
1731 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
1733 case HImode:
1734 return (4 + (mode == DImode ? 4 : 0)
1735 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
1737 case SImode:
1738 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
1740 default:
1741 break;
1743 abort ();
1745 default:
1746 return 99;
1751 arm_adjust_cost (insn, link, dep, cost)
1752 rtx insn;
1753 rtx link;
1754 rtx dep;
1755 int cost;
1757 rtx i_pat, d_pat;
1759 /* XXX This is not strictly true for the FPA. */
1760 if (REG_NOTE_KIND(link) == REG_DEP_ANTI
1761 || REG_NOTE_KIND(link) == REG_DEP_OUTPUT)
1762 return 0;
1764 if ((i_pat = single_set (insn)) != NULL
1765 && GET_CODE (SET_SRC (i_pat)) == MEM
1766 && (d_pat = single_set (dep)) != NULL
1767 && GET_CODE (SET_DEST (d_pat)) == MEM)
1769 /* This is a load after a store, there is no conflict if the load reads
1770 from a cached area. Assume that loads from the stack, and from the
1771 constant pool are cached, and that others will miss. This is a
1772 hack. */
1774 /* debug_rtx (insn);
1775 debug_rtx (dep);
1776 debug_rtx (link);
1777 fprintf (stderr, "costs %d\n", cost); */
1779 if (CONSTANT_POOL_ADDRESS_P (XEXP (SET_SRC (i_pat), 0))
1780 || reg_mentioned_p (stack_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
1781 || reg_mentioned_p (frame_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
1782 || reg_mentioned_p (hard_frame_pointer_rtx,
1783 XEXP (SET_SRC (i_pat), 0)))
1785 /* fprintf (stderr, "***** Now 1\n"); */
1786 return 1;
1790 return cost;
1793 /* This code has been fixed for cross compilation. */
1795 static int fpa_consts_inited = 0;
1797 char *strings_fpa[8] = {
1798 "0", "1", "2", "3",
1799 "4", "5", "0.5", "10"
1802 static REAL_VALUE_TYPE values_fpa[8];
1804 static void
1805 init_fpa_table ()
1807 int i;
1808 REAL_VALUE_TYPE r;
1810 for (i = 0; i < 8; i++)
1812 r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
1813 values_fpa[i] = r;
1816 fpa_consts_inited = 1;
1819 /* Return TRUE if rtx X is a valid immediate FPU constant. */
1822 const_double_rtx_ok_for_fpu (x)
1823 rtx x;
1825 REAL_VALUE_TYPE r;
1826 int i;
1828 if (!fpa_consts_inited)
1829 init_fpa_table ();
1831 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1832 if (REAL_VALUE_MINUS_ZERO (r))
1833 return 0;
1835 for (i = 0; i < 8; i++)
1836 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
1837 return 1;
1839 return 0;
1842 /* Return TRUE if rtx X is a valid immediate FPU constant. */
1845 neg_const_double_rtx_ok_for_fpu (x)
1846 rtx x;
1848 REAL_VALUE_TYPE r;
1849 int i;
1851 if (!fpa_consts_inited)
1852 init_fpa_table ();
1854 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1855 r = REAL_VALUE_NEGATE (r);
1856 if (REAL_VALUE_MINUS_ZERO (r))
1857 return 0;
1859 for (i = 0; i < 8; i++)
1860 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
1861 return 1;
1863 return 0;
1866 /* Predicates for `match_operand' and `match_operator'. */
1868 /* s_register_operand is the same as register_operand, but it doesn't accept
1869 (SUBREG (MEM)...).
1871 This function exists because at the time it was put in it led to better
1872 code. SUBREG(MEM) always needs a reload in the places where
1873 s_register_operand is used, and this seemed to lead to excessive
1874 reloading. */
1877 s_register_operand (op, mode)
1878 register rtx op;
1879 enum machine_mode mode;
1881 if (GET_MODE (op) != mode && mode != VOIDmode)
1882 return 0;
1884 if (GET_CODE (op) == SUBREG)
1885 op = SUBREG_REG (op);
1887 /* We don't consider registers whose class is NO_REGS
1888 to be a register operand. */
1889 return (GET_CODE (op) == REG
1890 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
1891 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
1894 /* Only accept reg, subreg(reg), const_int. */
1897 reg_or_int_operand (op, mode)
1898 register rtx op;
1899 enum machine_mode mode;
1901 if (GET_CODE (op) == CONST_INT)
1902 return 1;
1904 if (GET_MODE (op) != mode && mode != VOIDmode)
1905 return 0;
1907 if (GET_CODE (op) == SUBREG)
1908 op = SUBREG_REG (op);
1910 /* We don't consider registers whose class is NO_REGS
1911 to be a register operand. */
1912 return (GET_CODE (op) == REG
1913 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
1914 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
1917 /* Return 1 if OP is an item in memory, given that we are in reload. */
1920 reload_memory_operand (op, mode)
1921 rtx op;
1922 enum machine_mode mode;
1924 int regno = true_regnum (op);
1926 return (! CONSTANT_P (op)
1927 && (regno == -1
1928 || (GET_CODE (op) == REG
1929 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
1932 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
1933 memory access (architecture V4) */
1935 bad_signed_byte_operand (op, mode)
1936 rtx op;
1937 enum machine_mode mode;
1939 if (! memory_operand (op, mode) || GET_CODE (op) != MEM)
1940 return 0;
1942 op = XEXP (op, 0);
1944 /* A sum of anything more complex than reg + reg or reg + const is bad */
1945 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
1946 && (! s_register_operand (XEXP (op, 0), VOIDmode)
1947 || (! s_register_operand (XEXP (op, 1), VOIDmode)
1948 && GET_CODE (XEXP (op, 1)) != CONST_INT)))
1949 return 1;
1951 /* Big constants are also bad */
1952 if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
1953 && (INTVAL (XEXP (op, 1)) > 0xff
1954 || -INTVAL (XEXP (op, 1)) > 0xff))
1955 return 1;
1957 /* Everything else is good, or can will automatically be made so. */
1958 return 0;
1961 /* Return TRUE for valid operands for the rhs of an ARM instruction. */
1964 arm_rhs_operand (op, mode)
1965 rtx op;
1966 enum machine_mode mode;
1968 return (s_register_operand (op, mode)
1969 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
1972 /* Return TRUE for valid operands for the rhs of an ARM instruction, or a load.
1976 arm_rhsm_operand (op, mode)
1977 rtx op;
1978 enum machine_mode mode;
1980 return (s_register_operand (op, mode)
1981 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
1982 || memory_operand (op, mode));
1985 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
1986 constant that is valid when negated. */
1989 arm_add_operand (op, mode)
1990 rtx op;
1991 enum machine_mode mode;
1993 return (s_register_operand (op, mode)
1994 || (GET_CODE (op) == CONST_INT
1995 && (const_ok_for_arm (INTVAL (op))
1996 || const_ok_for_arm (-INTVAL (op)))));
2000 arm_not_operand (op, mode)
2001 rtx op;
2002 enum machine_mode mode;
2004 return (s_register_operand (op, mode)
2005 || (GET_CODE (op) == CONST_INT
2006 && (const_ok_for_arm (INTVAL (op))
2007 || const_ok_for_arm (~INTVAL (op)))));
2010 /* Return TRUE if the operand is a memory reference which contains an
2011 offsettable address. */
2013 offsettable_memory_operand (op, mode)
2014 register rtx op;
2015 enum machine_mode mode;
2017 if (mode == VOIDmode)
2018 mode = GET_MODE (op);
2020 return (mode == GET_MODE (op)
2021 && GET_CODE (op) == MEM
2022 && offsettable_address_p (reload_completed | reload_in_progress,
2023 mode, XEXP (op, 0)));
2026 /* Return TRUE if the operand is a memory reference which is, or can be
2027 made word aligned by adjusting the offset. */
2029 alignable_memory_operand (op, mode)
2030 register rtx op;
2031 enum machine_mode mode;
2033 rtx reg;
2035 if (mode == VOIDmode)
2036 mode = GET_MODE (op);
2038 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
2039 return 0;
2041 op = XEXP (op, 0);
2043 return ((GET_CODE (reg = op) == REG
2044 || (GET_CODE (op) == SUBREG
2045 && GET_CODE (reg = SUBREG_REG (op)) == REG)
2046 || (GET_CODE (op) == PLUS
2047 && GET_CODE (XEXP (op, 1)) == CONST_INT
2048 && (GET_CODE (reg = XEXP (op, 0)) == REG
2049 || (GET_CODE (XEXP (op, 0)) == SUBREG
2050 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
2051 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 4);
2054 /* Similar to s_register_operand, but does not allow hard integer
2055 registers. */
2057 f_register_operand (op, mode)
2058 register rtx op;
2059 enum machine_mode mode;
2061 if (GET_MODE (op) != mode && mode != VOIDmode)
2062 return 0;
2064 if (GET_CODE (op) == SUBREG)
2065 op = SUBREG_REG (op);
2067 /* We don't consider registers whose class is NO_REGS
2068 to be a register operand. */
2069 return (GET_CODE (op) == REG
2070 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2071 || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
2074 /* Return TRUE for valid operands for the rhs of an FPU instruction. */
2077 fpu_rhs_operand (op, mode)
2078 rtx op;
2079 enum machine_mode mode;
2081 if (s_register_operand (op, mode))
2082 return TRUE;
2083 else if (GET_CODE (op) == CONST_DOUBLE)
2084 return (const_double_rtx_ok_for_fpu (op));
2086 return FALSE;
2090 fpu_add_operand (op, mode)
2091 rtx op;
2092 enum machine_mode mode;
2094 if (s_register_operand (op, mode))
2095 return TRUE;
2096 else if (GET_CODE (op) == CONST_DOUBLE)
2097 return (const_double_rtx_ok_for_fpu (op)
2098 || neg_const_double_rtx_ok_for_fpu (op));
2100 return FALSE;
2103 /* Return nonzero if OP is a constant power of two. */
2106 power_of_two_operand (op, mode)
2107 rtx op;
2108 enum machine_mode mode;
2110 if (GET_CODE (op) == CONST_INT)
2112 HOST_WIDE_INT value = INTVAL(op);
2113 return value != 0 && (value & (value - 1)) == 0;
2115 return FALSE;
2118 /* Return TRUE for a valid operand of a DImode operation.
2119 Either: REG, CONST_DOUBLE or MEM(DImode_address).
2120 Note that this disallows MEM(REG+REG), but allows
2121 MEM(PRE/POST_INC/DEC(REG)). */
2124 di_operand (op, mode)
2125 rtx op;
2126 enum machine_mode mode;
2128 if (s_register_operand (op, mode))
2129 return TRUE;
2131 switch (GET_CODE (op))
2133 case CONST_DOUBLE:
2134 case CONST_INT:
2135 return TRUE;
2137 case MEM:
2138 return memory_address_p (DImode, XEXP (op, 0));
2140 default:
2141 return FALSE;
2145 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
2146 Either: REG, CONST_DOUBLE or MEM(DImode_address).
2147 Note that this disallows MEM(REG+REG), but allows
2148 MEM(PRE/POST_INC/DEC(REG)). */
2151 soft_df_operand (op, mode)
2152 rtx op;
2153 enum machine_mode mode;
2155 if (s_register_operand (op, mode))
2156 return TRUE;
2158 switch (GET_CODE (op))
2160 case CONST_DOUBLE:
2161 return TRUE;
2163 case MEM:
2164 return memory_address_p (DFmode, XEXP (op, 0));
2166 default:
2167 return FALSE;
2171 /* Return TRUE for valid index operands. */
2174 index_operand (op, mode)
2175 rtx op;
2176 enum machine_mode mode;
2178 return (s_register_operand(op, mode)
2179 || (immediate_operand (op, mode)
2180 && INTVAL (op) < 4096 && INTVAL (op) > -4096));
2183 /* Return TRUE for valid shifts by a constant. This also accepts any
2184 power of two on the (somewhat overly relaxed) assumption that the
2185 shift operator in this case was a mult. */
2188 const_shift_operand (op, mode)
2189 rtx op;
2190 enum machine_mode mode;
2192 return (power_of_two_operand (op, mode)
2193 || (immediate_operand (op, mode)
2194 && (INTVAL (op) < 32 && INTVAL (op) > 0)));
2197 /* Return TRUE for arithmetic operators which can be combined with a multiply
2198 (shift). */
2201 shiftable_operator (x, mode)
2202 rtx x;
2203 enum machine_mode mode;
2205 if (GET_MODE (x) != mode)
2206 return FALSE;
2207 else
2209 enum rtx_code code = GET_CODE (x);
2211 return (code == PLUS || code == MINUS
2212 || code == IOR || code == XOR || code == AND);
2216 /* Return TRUE for shift operators. */
2219 shift_operator (x, mode)
2220 rtx x;
2221 enum machine_mode mode;
2223 if (GET_MODE (x) != mode)
2224 return FALSE;
2225 else
2227 enum rtx_code code = GET_CODE (x);
2229 if (code == MULT)
2230 return power_of_two_operand (XEXP (x, 1));
2232 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
2233 || code == ROTATERT);
2237 int equality_operator (x, mode)
2238 rtx x;
2239 enum machine_mode mode;
2241 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
2244 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
2247 minmax_operator (x, mode)
2248 rtx x;
2249 enum machine_mode mode;
2251 enum rtx_code code = GET_CODE (x);
2253 if (GET_MODE (x) != mode)
2254 return FALSE;
2256 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
2259 /* return TRUE if x is EQ or NE */
2261 /* Return TRUE if this is the condition code register, if we aren't given
2262 a mode, accept any class CCmode register */
2265 cc_register (x, mode)
2266 rtx x;
2267 enum machine_mode mode;
2269 if (mode == VOIDmode)
2271 mode = GET_MODE (x);
2272 if (GET_MODE_CLASS (mode) != MODE_CC)
2273 return FALSE;
2276 if (mode == GET_MODE (x) && GET_CODE (x) == REG && REGNO (x) == 24)
2277 return TRUE;
2279 return FALSE;
2282 /* Return TRUE if this is the condition code register, if we aren't given
2283 a mode, accept any class CCmode register which indicates a dominance
2284 expression. */
2287 dominant_cc_register (x, mode)
2288 rtx x;
2289 enum machine_mode mode;
2291 if (mode == VOIDmode)
2293 mode = GET_MODE (x);
2294 if (GET_MODE_CLASS (mode) != MODE_CC)
2295 return FALSE;
2298 if (mode != CC_DNEmode && mode != CC_DEQmode
2299 && mode != CC_DLEmode && mode != CC_DLTmode
2300 && mode != CC_DGEmode && mode != CC_DGTmode
2301 && mode != CC_DLEUmode && mode != CC_DLTUmode
2302 && mode != CC_DGEUmode && mode != CC_DGTUmode)
2303 return FALSE;
2305 if (mode == GET_MODE (x) && GET_CODE (x) == REG && REGNO (x) == 24)
2306 return TRUE;
2308 return FALSE;
2311 /* Return TRUE if X references a SYMBOL_REF. */
2313 symbol_mentioned_p (x)
2314 rtx x;
2316 register char *fmt;
2317 register int i;
2319 if (GET_CODE (x) == SYMBOL_REF)
2320 return 1;
2322 fmt = GET_RTX_FORMAT (GET_CODE (x));
2323 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2325 if (fmt[i] == 'E')
2327 register int j;
2329 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2330 if (symbol_mentioned_p (XVECEXP (x, i, j)))
2331 return 1;
2333 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
2334 return 1;
2337 return 0;
2340 /* Return TRUE if X references a LABEL_REF. */
2342 label_mentioned_p (x)
2343 rtx x;
2345 register char *fmt;
2346 register int i;
2348 if (GET_CODE (x) == LABEL_REF)
2349 return 1;
2351 fmt = GET_RTX_FORMAT (GET_CODE (x));
2352 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2354 if (fmt[i] == 'E')
2356 register int j;
2358 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2359 if (label_mentioned_p (XVECEXP (x, i, j)))
2360 return 1;
2362 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
2363 return 1;
2366 return 0;
2369 enum rtx_code
2370 minmax_code (x)
2371 rtx x;
2373 enum rtx_code code = GET_CODE (x);
2375 if (code == SMAX)
2376 return GE;
2377 else if (code == SMIN)
2378 return LE;
2379 else if (code == UMIN)
2380 return LEU;
2381 else if (code == UMAX)
2382 return GEU;
2384 abort ();
2387 /* Return 1 if memory locations are adjacent */
2390 adjacent_mem_locations (a, b)
2391 rtx a, b;
2393 int val0 = 0, val1 = 0;
2394 int reg0, reg1;
2396 if ((GET_CODE (XEXP (a, 0)) == REG
2397 || (GET_CODE (XEXP (a, 0)) == PLUS
2398 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
2399 && (GET_CODE (XEXP (b, 0)) == REG
2400 || (GET_CODE (XEXP (b, 0)) == PLUS
2401 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
2403 if (GET_CODE (XEXP (a, 0)) == PLUS)
2405 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
2406 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
2408 else
2409 reg0 = REGNO (XEXP (a, 0));
2410 if (GET_CODE (XEXP (b, 0)) == PLUS)
2412 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
2413 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
2415 else
2416 reg1 = REGNO (XEXP (b, 0));
2417 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
2419 return 0;
2422 /* Return 1 if OP is a load multiple operation. It is known to be
2423 parallel and the first section will be tested. */
2426 load_multiple_operation (op, mode)
2427 rtx op;
2428 enum machine_mode mode;
2430 HOST_WIDE_INT count = XVECLEN (op, 0);
2431 int dest_regno;
2432 rtx src_addr;
2433 HOST_WIDE_INT i = 1, base = 0;
2434 rtx elt;
2436 if (count <= 1
2437 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
2438 return 0;
2440 /* Check to see if this might be a write-back */
2441 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
2443 i++;
2444 base = 1;
2446 /* Now check it more carefully */
2447 if (GET_CODE (SET_DEST (elt)) != REG
2448 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
2449 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
2450 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
2451 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
2452 || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
2453 || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
2454 || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
2455 != REGNO (SET_DEST (elt)))
2456 return 0;
2458 count--;
2461 /* Perform a quick check so we don't blow up below. */
2462 if (count <= i
2463 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
2464 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
2465 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
2466 return 0;
2468 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
2469 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
2471 for (; i < count; i++)
2473 elt = XVECEXP (op, 0, i);
2475 if (GET_CODE (elt) != SET
2476 || GET_CODE (SET_DEST (elt)) != REG
2477 || GET_MODE (SET_DEST (elt)) != SImode
2478 || REGNO (SET_DEST (elt)) != dest_regno + i - base
2479 || GET_CODE (SET_SRC (elt)) != MEM
2480 || GET_MODE (SET_SRC (elt)) != SImode
2481 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
2482 || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
2483 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
2484 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
2485 return 0;
2488 return 1;
2491 /* Return 1 if OP is a store multiple operation. It is known to be
2492 parallel and the first section will be tested. */
2495 store_multiple_operation (op, mode)
2496 rtx op;
2497 enum machine_mode mode;
2499 HOST_WIDE_INT count = XVECLEN (op, 0);
2500 int src_regno;
2501 rtx dest_addr;
2502 HOST_WIDE_INT i = 1, base = 0;
2503 rtx elt;
2505 if (count <= 1
2506 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
2507 return 0;
2509 /* Check to see if this might be a write-back */
2510 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
2512 i++;
2513 base = 1;
2515 /* Now check it more carefully */
2516 if (GET_CODE (SET_DEST (elt)) != REG
2517 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
2518 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
2519 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
2520 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
2521 || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
2522 || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
2523 || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
2524 != REGNO (SET_DEST (elt)))
2525 return 0;
2527 count--;
2530 /* Perform a quick check so we don't blow up below. */
2531 if (count <= i
2532 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
2533 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
2534 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
2535 return 0;
2537 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
2538 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
2540 for (; i < count; i++)
2542 elt = XVECEXP (op, 0, i);
2544 if (GET_CODE (elt) != SET
2545 || GET_CODE (SET_SRC (elt)) != REG
2546 || GET_MODE (SET_SRC (elt)) != SImode
2547 || REGNO (SET_SRC (elt)) != src_regno + i - base
2548 || GET_CODE (SET_DEST (elt)) != MEM
2549 || GET_MODE (SET_DEST (elt)) != SImode
2550 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
2551 || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
2552 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
2553 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
2554 return 0;
2557 return 1;
2561 load_multiple_sequence (operands, nops, regs, base, load_offset)
2562 rtx *operands;
2563 int nops;
2564 int *regs;
2565 int *base;
2566 HOST_WIDE_INT *load_offset;
2568 int unsorted_regs[4];
2569 HOST_WIDE_INT unsorted_offsets[4];
2570 int order[4];
2571 int base_reg = -1;
2572 int i;
2574 /* Can only handle 2, 3, or 4 insns at present, though could be easily
2575 extended if required. */
2576 if (nops < 2 || nops > 4)
2577 abort ();
2579 /* Loop over the operands and check that the memory references are
2580 suitable (ie immediate offsets from the same base register). At
2581 the same time, extract the target register, and the memory
2582 offsets. */
2583 for (i = 0; i < nops; i++)
2585 rtx reg;
2586 rtx offset;
2588 /* Convert a subreg of a mem into the mem itself. */
2589 if (GET_CODE (operands[nops + i]) == SUBREG)
2590 operands[nops + i] = alter_subreg(operands[nops + i]);
2592 if (GET_CODE (operands[nops + i]) != MEM)
2593 abort ();
2595 /* Don't reorder volatile memory references; it doesn't seem worth
2596 looking for the case where the order is ok anyway. */
2597 if (MEM_VOLATILE_P (operands[nops + i]))
2598 return 0;
2600 offset = const0_rtx;
2602 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
2603 || (GET_CODE (reg) == SUBREG
2604 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
2605 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
2606 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
2607 == REG)
2608 || (GET_CODE (reg) == SUBREG
2609 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
2610 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
2611 == CONST_INT)))
2613 if (i == 0)
2615 base_reg = REGNO(reg);
2616 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
2617 ? REGNO (operands[i])
2618 : REGNO (SUBREG_REG (operands[i])));
2619 order[0] = 0;
2621 else
2623 if (base_reg != REGNO (reg))
2624 /* Not addressed from the same base register. */
2625 return 0;
2627 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
2628 ? REGNO (operands[i])
2629 : REGNO (SUBREG_REG (operands[i])));
2630 if (unsorted_regs[i] < unsorted_regs[order[0]])
2631 order[0] = i;
2634 /* If it isn't an integer register, or if it overwrites the
2635 base register but isn't the last insn in the list, then
2636 we can't do this. */
2637 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
2638 || (i != nops - 1 && unsorted_regs[i] == base_reg))
2639 return 0;
2641 unsorted_offsets[i] = INTVAL (offset);
2643 else
2644 /* Not a suitable memory address. */
2645 return 0;
2648 /* All the useful information has now been extracted from the
2649 operands into unsorted_regs and unsorted_offsets; additionally,
2650 order[0] has been set to the lowest numbered register in the
2651 list. Sort the registers into order, and check that the memory
2652 offsets are ascending and adjacent. */
2654 for (i = 1; i < nops; i++)
2656 int j;
2658 order[i] = order[i - 1];
2659 for (j = 0; j < nops; j++)
2660 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
2661 && (order[i] == order[i - 1]
2662 || unsorted_regs[j] < unsorted_regs[order[i]]))
2663 order[i] = j;
2665 /* Have we found a suitable register? if not, one must be used more
2666 than once. */
2667 if (order[i] == order[i - 1])
2668 return 0;
2670 /* Is the memory address adjacent and ascending? */
2671 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
2672 return 0;
2675 if (base)
2677 *base = base_reg;
2679 for (i = 0; i < nops; i++)
2680 regs[i] = unsorted_regs[order[i]];
2682 *load_offset = unsorted_offsets[order[0]];
2685 if (unsorted_offsets[order[0]] == 0)
2686 return 1; /* ldmia */
2688 if (unsorted_offsets[order[0]] == 4)
2689 return 2; /* ldmib */
2691 if (unsorted_offsets[order[nops - 1]] == 0)
2692 return 3; /* ldmda */
2694 if (unsorted_offsets[order[nops - 1]] == -4)
2695 return 4; /* ldmdb */
2697 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm if
2698 the offset isn't small enough. The reason 2 ldrs are faster is because
2699 these ARMs are able to do more than one cache access in a single cycle.
2700 The ARM9 and StrongARM have Harvard caches, whilst the ARM8 has a double
2701 bandwidth cache. This means that these cores can do both an instruction
2702 fetch and a data fetch in a single cycle, so the trick of calculating the
2703 address into a scratch register (one of the result regs) and then doing a
2704 load multiple actually becomes slower (and no smaller in code size). That
2705 is the transformation
2707 ldr rd1, [rbase + offset]
2708 ldr rd2, [rbase + offset + 4]
2712 add rd1, rbase, offset
2713 ldmia rd1, {rd1, rd2}
2715 produces worse code -- '3 cycles + any stalls on rd2' instead of '2 cycles
2716 + any stalls on rd2'. On ARMs with only one cache access per cycle, the
2717 first sequence could never complete in less than 6 cycles, whereas the ldm
2718 sequence would only take 5 and would make better use of sequential accesses
2719 if not hitting the cache.
2721 We cheat here and test 'arm_ld_sched' which we currently know to only be
2722 true for the ARM8, ARM9 and StrongARM. If this ever changes, then the test
2723 below needs to be reworked. */
2724 if (nops == 2 && arm_ld_sched)
2725 return 0;
2727 /* Can't do it without setting up the offset, only do this if it takes
2728 no more than one insn. */
2729 return (const_ok_for_arm (unsorted_offsets[order[0]])
2730 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
2733 char *
2734 emit_ldm_seq (operands, nops)
2735 rtx *operands;
2736 int nops;
2738 int regs[4];
2739 int base_reg;
2740 HOST_WIDE_INT offset;
2741 char buf[100];
2742 int i;
2744 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
2746 case 1:
2747 strcpy (buf, "ldm%?ia\t");
2748 break;
2750 case 2:
2751 strcpy (buf, "ldm%?ib\t");
2752 break;
2754 case 3:
2755 strcpy (buf, "ldm%?da\t");
2756 break;
2758 case 4:
2759 strcpy (buf, "ldm%?db\t");
2760 break;
2762 case 5:
2763 if (offset >= 0)
2764 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
2765 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
2766 (long) offset);
2767 else
2768 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
2769 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
2770 (long) -offset);
2771 output_asm_insn (buf, operands);
2772 base_reg = regs[0];
2773 strcpy (buf, "ldm%?ia\t");
2774 break;
2776 default:
2777 abort ();
2780 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
2781 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
2783 for (i = 1; i < nops; i++)
2784 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
2785 reg_names[regs[i]]);
2787 strcat (buf, "}\t%@ phole ldm");
2789 output_asm_insn (buf, operands);
2790 return "";
2794 store_multiple_sequence (operands, nops, regs, base, load_offset)
2795 rtx *operands;
2796 int nops;
2797 int *regs;
2798 int *base;
2799 HOST_WIDE_INT *load_offset;
2801 int unsorted_regs[4];
2802 HOST_WIDE_INT unsorted_offsets[4];
2803 int order[4];
2804 int base_reg = -1;
2805 int i;
2807 /* Can only handle 2, 3, or 4 insns at present, though could be easily
2808 extended if required. */
2809 if (nops < 2 || nops > 4)
2810 abort ();
2812 /* Loop over the operands and check that the memory references are
2813 suitable (ie immediate offsets from the same base register). At
2814 the same time, extract the target register, and the memory
2815 offsets. */
2816 for (i = 0; i < nops; i++)
2818 rtx reg;
2819 rtx offset;
2821 /* Convert a subreg of a mem into the mem itself. */
2822 if (GET_CODE (operands[nops + i]) == SUBREG)
2823 operands[nops + i] = alter_subreg(operands[nops + i]);
2825 if (GET_CODE (operands[nops + i]) != MEM)
2826 abort ();
2828 /* Don't reorder volatile memory references; it doesn't seem worth
2829 looking for the case where the order is ok anyway. */
2830 if (MEM_VOLATILE_P (operands[nops + i]))
2831 return 0;
2833 offset = const0_rtx;
2835 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
2836 || (GET_CODE (reg) == SUBREG
2837 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
2838 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
2839 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
2840 == REG)
2841 || (GET_CODE (reg) == SUBREG
2842 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
2843 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
2844 == CONST_INT)))
2846 if (i == 0)
2848 base_reg = REGNO(reg);
2849 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
2850 ? REGNO (operands[i])
2851 : REGNO (SUBREG_REG (operands[i])));
2852 order[0] = 0;
2854 else
2856 if (base_reg != REGNO (reg))
2857 /* Not addressed from the same base register. */
2858 return 0;
2860 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
2861 ? REGNO (operands[i])
2862 : REGNO (SUBREG_REG (operands[i])));
2863 if (unsorted_regs[i] < unsorted_regs[order[0]])
2864 order[0] = i;
2867 /* If it isn't an integer register, then we can't do this. */
2868 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
2869 return 0;
2871 unsorted_offsets[i] = INTVAL (offset);
2873 else
2874 /* Not a suitable memory address. */
2875 return 0;
2878 /* All the useful information has now been extracted from the
2879 operands into unsorted_regs and unsorted_offsets; additionally,
2880 order[0] has been set to the lowest numbered register in the
2881 list. Sort the registers into order, and check that the memory
2882 offsets are ascending and adjacent. */
2884 for (i = 1; i < nops; i++)
2886 int j;
2888 order[i] = order[i - 1];
2889 for (j = 0; j < nops; j++)
2890 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
2891 && (order[i] == order[i - 1]
2892 || unsorted_regs[j] < unsorted_regs[order[i]]))
2893 order[i] = j;
2895 /* Have we found a suitable register? if not, one must be used more
2896 than once. */
2897 if (order[i] == order[i - 1])
2898 return 0;
2900 /* Is the memory address adjacent and ascending? */
2901 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
2902 return 0;
2905 if (base)
2907 *base = base_reg;
2909 for (i = 0; i < nops; i++)
2910 regs[i] = unsorted_regs[order[i]];
2912 *load_offset = unsorted_offsets[order[0]];
2915 if (unsorted_offsets[order[0]] == 0)
2916 return 1; /* stmia */
2918 if (unsorted_offsets[order[0]] == 4)
2919 return 2; /* stmib */
2921 if (unsorted_offsets[order[nops - 1]] == 0)
2922 return 3; /* stmda */
2924 if (unsorted_offsets[order[nops - 1]] == -4)
2925 return 4; /* stmdb */
2927 return 0;
2930 char *
2931 emit_stm_seq (operands, nops)
2932 rtx *operands;
2933 int nops;
2935 int regs[4];
2936 int base_reg;
2937 HOST_WIDE_INT offset;
2938 char buf[100];
2939 int i;
2941 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
2943 case 1:
2944 strcpy (buf, "stm%?ia\t");
2945 break;
2947 case 2:
2948 strcpy (buf, "stm%?ib\t");
2949 break;
2951 case 3:
2952 strcpy (buf, "stm%?da\t");
2953 break;
2955 case 4:
2956 strcpy (buf, "stm%?db\t");
2957 break;
2959 default:
2960 abort ();
2963 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
2964 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
2966 for (i = 1; i < nops; i++)
2967 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
2968 reg_names[regs[i]]);
2970 strcat (buf, "}\t%@ phole stm");
2972 output_asm_insn (buf, operands);
2973 return "";
2977 multi_register_push (op, mode)
2978 rtx op;
2979 enum machine_mode mode;
2981 if (GET_CODE (op) != PARALLEL
2982 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2983 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
2984 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != 2))
2985 return 0;
2987 return 1;
2991 /* Routines for use with attributes */
2993 /* Return nonzero if ATTR is a valid attribute for DECL.
2994 ATTRIBUTES are any existing attributes and ARGS are the arguments
2995 supplied with ATTR.
2997 Supported attributes:
2999 naked: don't output any prologue or epilogue code, the user is assumed
3000 to do the right thing. */
3003 arm_valid_machine_decl_attribute (decl, attributes, attr, args)
3004 tree decl;
3005 tree attributes;
3006 tree attr;
3007 tree args;
3009 if (args != NULL_TREE)
3010 return 0;
3012 if (is_attribute_p ("naked", attr))
3013 return TREE_CODE (decl) == FUNCTION_DECL;
3014 return 0;
3017 /* Return non-zero if FUNC is a naked function. */
3019 static int
3020 arm_naked_function_p (func)
3021 tree func;
3023 tree a;
3025 if (TREE_CODE (func) != FUNCTION_DECL)
3026 abort ();
3028 a = lookup_attribute ("naked", DECL_MACHINE_ATTRIBUTES (func));
3029 return a != NULL_TREE;
3032 /* Routines for use in generating RTL */
3035 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
3036 in_struct_p, scalar_p)
3037 int base_regno;
3038 int count;
3039 rtx from;
3040 int up;
3041 int write_back;
3042 int unchanging_p;
3043 int in_struct_p;
3044 int scalar_p;
3046 int i = 0, j;
3047 rtx result;
3048 int sign = up ? 1 : -1;
3049 rtx mem;
3051 result = gen_rtx_PARALLEL (VOIDmode,
3052 rtvec_alloc (count + (write_back ? 2 : 0)));
3053 if (write_back)
3055 XVECEXP (result, 0, 0)
3056 = gen_rtx_SET (GET_MODE (from), from,
3057 plus_constant (from, count * 4 * sign));
3058 i = 1;
3059 count++;
3062 for (j = 0; i < count; i++, j++)
3064 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
3065 RTX_UNCHANGING_P (mem) = unchanging_p;
3066 MEM_IN_STRUCT_P (mem) = in_struct_p;
3067 MEM_SCALAR_P (mem) = scalar_p;
3068 XVECEXP (result, 0, i)
3069 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
3072 if (write_back)
3073 XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, from);
3075 return result;
3079 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
3080 in_struct_p, scalar_p)
3081 int base_regno;
3082 int count;
3083 rtx to;
3084 int up;
3085 int write_back;
3086 int unchanging_p;
3087 int in_struct_p;
3088 int scalar_p;
3090 int i = 0, j;
3091 rtx result;
3092 int sign = up ? 1 : -1;
3093 rtx mem;
3095 result = gen_rtx_PARALLEL (VOIDmode,
3096 rtvec_alloc (count + (write_back ? 2 : 0)));
3097 if (write_back)
3099 XVECEXP (result, 0, 0)
3100 = gen_rtx_SET (GET_MODE (to), to,
3101 plus_constant (to, count * 4 * sign));
3102 i = 1;
3103 count++;
3106 for (j = 0; i < count; i++, j++)
3108 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
3109 RTX_UNCHANGING_P (mem) = unchanging_p;
3110 MEM_IN_STRUCT_P (mem) = in_struct_p;
3111 MEM_SCALAR_P (mem) = scalar_p;
3113 XVECEXP (result, 0, i)
3114 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
3117 if (write_back)
3118 XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, to);
3120 return result;
3124 arm_gen_movstrqi (operands)
3125 rtx *operands;
3127 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
3128 int i;
3129 rtx src, dst;
3130 rtx st_src, st_dst, fin_src, fin_dst;
3131 rtx part_bytes_reg = NULL;
3132 rtx mem;
3133 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
3134 int dst_scalar_p, src_scalar_p;
3136 if (GET_CODE (operands[2]) != CONST_INT
3137 || GET_CODE (operands[3]) != CONST_INT
3138 || INTVAL (operands[2]) > 64
3139 || INTVAL (operands[3]) & 3)
3140 return 0;
3142 st_dst = XEXP (operands[0], 0);
3143 st_src = XEXP (operands[1], 0);
3145 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
3146 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
3147 dst_scalar_p = MEM_SCALAR_P (operands[0]);
3148 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
3149 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
3150 src_scalar_p = MEM_SCALAR_P (operands[1]);
3152 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
3153 fin_src = src = copy_to_mode_reg (SImode, st_src);
3155 in_words_to_go = (INTVAL (operands[2]) + 3) / 4;
3156 out_words_to_go = INTVAL (operands[2]) / 4;
3157 last_bytes = INTVAL (operands[2]) & 3;
3159 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
3160 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
3162 for (i = 0; in_words_to_go >= 2; i+=4)
3164 if (in_words_to_go > 4)
3165 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
3166 src_unchanging_p,
3167 src_in_struct_p,
3168 src_scalar_p));
3169 else
3170 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
3171 FALSE, src_unchanging_p,
3172 src_in_struct_p, src_scalar_p));
3174 if (out_words_to_go)
3176 if (out_words_to_go > 4)
3177 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
3178 dst_unchanging_p,
3179 dst_in_struct_p,
3180 dst_scalar_p));
3181 else if (out_words_to_go != 1)
3182 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
3183 dst, TRUE,
3184 (last_bytes == 0
3185 ? FALSE : TRUE),
3186 dst_unchanging_p,
3187 dst_in_struct_p,
3188 dst_scalar_p));
3189 else
3191 mem = gen_rtx_MEM (SImode, dst);
3192 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3193 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3194 MEM_SCALAR_P (mem) = dst_scalar_p;
3195 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
3196 if (last_bytes != 0)
3197 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
3201 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
3202 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
3205 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
3206 if (out_words_to_go)
3208 rtx sreg;
3210 mem = gen_rtx_MEM (SImode, src);
3211 RTX_UNCHANGING_P (mem) = src_unchanging_p;
3212 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3213 MEM_SCALAR_P (mem) = src_scalar_p;
3214 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
3215 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
3217 mem = gen_rtx_MEM (SImode, dst);
3218 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3219 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3220 MEM_SCALAR_P (mem) = dst_scalar_p;
3221 emit_move_insn (mem, sreg);
3222 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
3223 in_words_to_go--;
3225 if (in_words_to_go) /* Sanity check */
3226 abort ();
3229 if (in_words_to_go)
3231 if (in_words_to_go < 0)
3232 abort ();
3234 mem = gen_rtx_MEM (SImode, src);
3235 RTX_UNCHANGING_P (mem) = src_unchanging_p;
3236 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3237 MEM_SCALAR_P (mem) = src_scalar_p;
3238 part_bytes_reg = copy_to_mode_reg (SImode, mem);
3241 if (BYTES_BIG_ENDIAN && last_bytes)
3243 rtx tmp = gen_reg_rtx (SImode);
3245 if (part_bytes_reg == NULL)
3246 abort ();
3248 /* The bytes we want are in the top end of the word */
3249 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
3250 GEN_INT (8 * (4 - last_bytes))));
3251 part_bytes_reg = tmp;
3253 while (last_bytes)
3255 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
3256 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3257 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3258 MEM_SCALAR_P (mem) = dst_scalar_p;
3259 emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
3260 if (--last_bytes)
3262 tmp = gen_reg_rtx (SImode);
3263 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
3264 part_bytes_reg = tmp;
3269 else
3271 while (last_bytes)
3273 if (part_bytes_reg == NULL)
3274 abort ();
3276 mem = gen_rtx_MEM (QImode, dst);
3277 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3278 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3279 MEM_SCALAR_P (mem) = dst_scalar_p;
3280 emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
3281 if (--last_bytes)
3283 rtx tmp = gen_reg_rtx (SImode);
3285 emit_insn (gen_addsi3 (dst, dst, const1_rtx));
3286 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
3287 part_bytes_reg = tmp;
3292 return 1;
3295 /* Generate a memory reference for a half word, such that it will be loaded
3296 into the top 16 bits of the word. We can assume that the address is
3297 known to be alignable and of the form reg, or plus (reg, const). */
3299 gen_rotated_half_load (memref)
3300 rtx memref;
3302 HOST_WIDE_INT offset = 0;
3303 rtx base = XEXP (memref, 0);
3305 if (GET_CODE (base) == PLUS)
3307 offset = INTVAL (XEXP (base, 1));
3308 base = XEXP (base, 0);
3311 /* If we aren't allowed to generate unaligned addresses, then fail. */
3312 if (TARGET_SHORT_BY_BYTES
3313 && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
3314 return NULL;
3316 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
3318 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
3319 return base;
3321 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
3324 static enum machine_mode
3325 select_dominance_cc_mode (op, x, y, cond_or)
3326 enum rtx_code op;
3327 rtx x;
3328 rtx y;
3329 HOST_WIDE_INT cond_or;
3331 enum rtx_code cond1, cond2;
3332 int swapped = 0;
3334 /* Currently we will probably get the wrong result if the individual
3335 comparisons are not simple. This also ensures that it is safe to
3336 reverse a comparison if necessary. */
3337 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
3338 != CCmode)
3339 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
3340 != CCmode))
3341 return CCmode;
3343 if (cond_or)
3344 cond1 = reverse_condition (cond1);
3346 /* If the comparisons are not equal, and one doesn't dominate the other,
3347 then we can't do this. */
3348 if (cond1 != cond2
3349 && ! comparison_dominates_p (cond1, cond2)
3350 && (swapped = 1, ! comparison_dominates_p (cond2, cond1)))
3351 return CCmode;
3353 if (swapped)
3355 enum rtx_code temp = cond1;
3356 cond1 = cond2;
3357 cond2 = temp;
3360 switch (cond1)
3362 case EQ:
3363 if (cond2 == EQ || ! cond_or)
3364 return CC_DEQmode;
3366 switch (cond2)
3368 case LE: return CC_DLEmode;
3369 case LEU: return CC_DLEUmode;
3370 case GE: return CC_DGEmode;
3371 case GEU: return CC_DGEUmode;
3372 default: break;
3375 break;
3377 case LT:
3378 if (cond2 == LT || ! cond_or)
3379 return CC_DLTmode;
3380 if (cond2 == LE)
3381 return CC_DLEmode;
3382 if (cond2 == NE)
3383 return CC_DNEmode;
3384 break;
3386 case GT:
3387 if (cond2 == GT || ! cond_or)
3388 return CC_DGTmode;
3389 if (cond2 == GE)
3390 return CC_DGEmode;
3391 if (cond2 == NE)
3392 return CC_DNEmode;
3393 break;
3395 case LTU:
3396 if (cond2 == LTU || ! cond_or)
3397 return CC_DLTUmode;
3398 if (cond2 == LEU)
3399 return CC_DLEUmode;
3400 if (cond2 == NE)
3401 return CC_DNEmode;
3402 break;
3404 case GTU:
3405 if (cond2 == GTU || ! cond_or)
3406 return CC_DGTUmode;
3407 if (cond2 == GEU)
3408 return CC_DGEUmode;
3409 if (cond2 == NE)
3410 return CC_DNEmode;
3411 break;
3413 /* The remaining cases only occur when both comparisons are the
3414 same. */
3415 case NE:
3416 return CC_DNEmode;
3418 case LE:
3419 return CC_DLEmode;
3421 case GE:
3422 return CC_DGEmode;
3424 case LEU:
3425 return CC_DLEUmode;
3427 case GEU:
3428 return CC_DGEUmode;
3430 default:
3431 break;
3434 abort ();
3437 enum machine_mode
3438 arm_select_cc_mode (op, x, y)
3439 enum rtx_code op;
3440 rtx x;
3441 rtx y;
3443 /* All floating point compares return CCFP if it is an equality
3444 comparison, and CCFPE otherwise. */
3445 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3446 return (op == EQ || op == NE) ? CCFPmode : CCFPEmode;
3448 /* A compare with a shifted operand. Because of canonicalization, the
3449 comparison will have to be swapped when we emit the assembler. */
3450 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
3451 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
3452 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
3453 || GET_CODE (x) == ROTATERT))
3454 return CC_SWPmode;
3456 /* This is a special case that is used by combine to allow a
3457 comparison of a shifted byte load to be split into a zero-extend
3458 followed by a comparison of the shifted integer (only valid for
3459 equalities and unsigned inequalities). */
3460 if (GET_MODE (x) == SImode
3461 && GET_CODE (x) == ASHIFT
3462 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
3463 && GET_CODE (XEXP (x, 0)) == SUBREG
3464 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
3465 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
3466 && (op == EQ || op == NE
3467 || op == GEU || op == GTU || op == LTU || op == LEU)
3468 && GET_CODE (y) == CONST_INT)
3469 return CC_Zmode;
3471 /* An operation that sets the condition codes as a side-effect, the
3472 V flag is not set correctly, so we can only use comparisons where
3473 this doesn't matter. (For LT and GE we can use "mi" and "pl"
3474 instead. */
3475 if (GET_MODE (x) == SImode
3476 && y == const0_rtx
3477 && (op == EQ || op == NE || op == LT || op == GE)
3478 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
3479 || GET_CODE (x) == AND || GET_CODE (x) == IOR
3480 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
3481 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
3482 || GET_CODE (x) == LSHIFTRT
3483 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
3484 || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
3485 return CC_NOOVmode;
3487 /* A construct for a conditional compare, if the false arm contains
3488 0, then both conditions must be true, otherwise either condition
3489 must be true. Not all conditions are possible, so CCmode is
3490 returned if it can't be done. */
3491 if (GET_CODE (x) == IF_THEN_ELSE
3492 && (XEXP (x, 2) == const0_rtx
3493 || XEXP (x, 2) == const1_rtx)
3494 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3495 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
3496 return select_dominance_cc_mode (op, XEXP (x, 0), XEXP (x, 1),
3497 INTVAL (XEXP (x, 2)));
3499 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
3500 return CC_Zmode;
3502 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
3503 && GET_CODE (x) == PLUS
3504 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
3505 return CC_Cmode;
3507 return CCmode;
3510 /* X and Y are two things to compare using CODE. Emit the compare insn and
3511 return the rtx for register 0 in the proper mode. FP means this is a
3512 floating point compare: I don't think that it is needed on the arm. */
3515 gen_compare_reg (code, x, y, fp)
3516 enum rtx_code code;
3517 rtx x, y;
3518 int fp;
3520 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
3521 rtx cc_reg = gen_rtx_REG (mode, 24);
3523 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
3524 gen_rtx_COMPARE (mode, x, y)));
3526 return cc_reg;
3529 void
3530 arm_reload_in_hi (operands)
3531 rtx *operands;
3533 rtx base = find_replacement (&XEXP (operands[1], 0));
3535 emit_insn (gen_zero_extendqisi2 (operands[2], gen_rtx_MEM (QImode, base)));
3536 /* Handle the case where the address is too complex to be offset by 1. */
3537 if (GET_CODE (base) == MINUS
3538 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
3540 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[0]));
3542 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
3543 base = base_plus;
3546 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
3547 gen_rtx_MEM (QImode,
3548 plus_constant (base, 1))));
3549 if (BYTES_BIG_ENDIAN)
3550 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
3551 gen_rtx_IOR (SImode,
3552 gen_rtx_ASHIFT
3553 (SImode,
3554 gen_rtx_SUBREG (SImode, operands[0], 0),
3555 GEN_INT (8)),
3556 operands[2])));
3557 else
3558 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
3559 gen_rtx_IOR (SImode,
3560 gen_rtx_ASHIFT (SImode, operands[2],
3561 GEN_INT (8)),
3562 gen_rtx_SUBREG (SImode, operands[0],
3563 0))));
3566 void
3567 arm_reload_out_hi (operands)
3568 rtx *operands;
3570 rtx base = find_replacement (&XEXP (operands[0], 0));
3572 if (BYTES_BIG_ENDIAN)
3574 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, 1)),
3575 gen_rtx_SUBREG (QImode, operands[1], 0)));
3576 emit_insn (gen_lshrsi3 (operands[2],
3577 gen_rtx_SUBREG (SImode, operands[1], 0),
3578 GEN_INT (8)));
3579 emit_insn (gen_movqi (gen_rtx_MEM (QImode, base),
3580 gen_rtx_SUBREG (QImode, operands[2], 0)));
3582 else
3584 emit_insn (gen_movqi (gen_rtx_MEM (QImode, base),
3585 gen_rtx_SUBREG (QImode, operands[1], 0)));
3586 emit_insn (gen_lshrsi3 (operands[2],
3587 gen_rtx_SUBREG (SImode, operands[1], 0),
3588 GEN_INT (8)));
3589 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, 1)),
3590 gen_rtx_SUBREG (QImode, operands[2], 0)));
3594 /* Routines for manipulation of the constant pool. */
3595 /* This is unashamedly hacked from the version in sh.c, since the problem is
3596 extremely similar. */
3598 /* Arm instructions cannot load a large constant into a register,
3599 constants have to come from a pc relative load. The reference of a pc
3600 relative load instruction must be less than 1k infront of the instruction.
3601 This means that we often have to dump a constant inside a function, and
3602 generate code to branch around it.
3604 It is important to minimize this, since the branches will slow things
3605 down and make things bigger.
3607 Worst case code looks like:
3609 ldr rn, L1
3610 b L2
3611 align
3612 L1: .long value
3616 ldr rn, L3
3617 b L4
3618 align
3619 L3: .long value
3623 We fix this by performing a scan before scheduling, which notices which
3624 instructions need to have their operands fetched from the constant table
3625 and builds the table.
3628 The algorithm is:
3630 scan, find an instruction which needs a pcrel move. Look forward, find th
3631 last barrier which is within MAX_COUNT bytes of the requirement.
3632 If there isn't one, make one. Process all the instructions between
3633 the find and the barrier.
3635 In the above example, we can tell that L3 is within 1k of L1, so
3636 the first move can be shrunk from the 2 insn+constant sequence into
3637 just 1 insn, and the constant moved to L3 to make:
3639 ldr rn, L1
3641 ldr rn, L3
3642 b L4
3643 align
3644 L1: .long value
3645 L3: .long value
3648 Then the second move becomes the target for the shortening process.
3652 typedef struct
3654 rtx value; /* Value in table */
3655 HOST_WIDE_INT next_offset;
3656 enum machine_mode mode; /* Mode of value */
3657 } pool_node;
3659 /* The maximum number of constants that can fit into one pool, since
3660 the pc relative range is 0...1020 bytes and constants are at least 4
3661 bytes long */
3663 #define MAX_POOL_SIZE (1020/4)
3664 static pool_node pool_vector[MAX_POOL_SIZE];
3665 static int pool_size;
3666 static rtx pool_vector_label;
3668 /* Add a constant to the pool and return its offset within the current
3669 pool.
3671 X is the rtx we want to replace. MODE is its mode. On return,
3672 ADDRESS_ONLY will be non-zero if we really want the address of such
3673 a constant, not the constant itself. */
3674 static HOST_WIDE_INT
3675 add_constant (x, mode, address_only)
3676 rtx x;
3677 enum machine_mode mode;
3678 int * address_only;
3680 int i;
3681 HOST_WIDE_INT offset;
3683 * address_only = 0;
3685 if (mode == SImode && GET_CODE (x) == MEM && CONSTANT_P (XEXP (x, 0))
3686 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3687 x = get_pool_constant (XEXP (x, 0));
3688 else if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P(x))
3690 *address_only = 1;
3691 mode = get_pool_mode (x);
3692 x = get_pool_constant (x);
3694 #ifndef AOF_ASSEMBLER
3695 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == 3)
3696 x = XVECEXP (x, 0, 0);
3697 #endif
3699 #ifdef AOF_ASSEMBLER
3700 /* PIC Symbol references need to be converted into offsets into the
3701 based area. */
3702 if (flag_pic && GET_CODE (x) == SYMBOL_REF)
3703 x = aof_pic_entry (x);
3704 #endif /* AOF_ASSEMBLER */
3706 /* First see if we've already got it */
3707 for (i = 0; i < pool_size; i++)
3709 if (GET_CODE (x) == pool_vector[i].value->code
3710 && mode == pool_vector[i].mode)
3712 if (GET_CODE (x) == CODE_LABEL)
3714 if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
3715 continue;
3717 if (rtx_equal_p (x, pool_vector[i].value))
3718 return pool_vector[i].next_offset - GET_MODE_SIZE (mode);
3722 /* Need a new one */
3723 pool_vector[pool_size].next_offset = GET_MODE_SIZE (mode);
3724 offset = 0;
3725 if (pool_size == 0)
3726 pool_vector_label = gen_label_rtx ();
3727 else
3728 pool_vector[pool_size].next_offset
3729 += (offset = pool_vector[pool_size - 1].next_offset);
3731 pool_vector[pool_size].value = x;
3732 pool_vector[pool_size].mode = mode;
3733 pool_size++;
3734 return offset;
3737 /* Output the literal table */
3738 static void
3739 dump_table (scan)
3740 rtx scan;
3742 int i;
3744 scan = emit_label_after (gen_label_rtx (), scan);
3745 scan = emit_insn_after (gen_align_4 (), scan);
3746 scan = emit_label_after (pool_vector_label, scan);
3748 for (i = 0; i < pool_size; i++)
3750 pool_node *p = pool_vector + i;
3752 switch (GET_MODE_SIZE (p->mode))
3754 case 4:
3755 scan = emit_insn_after (gen_consttable_4 (p->value), scan);
3756 break;
3758 case 8:
3759 scan = emit_insn_after (gen_consttable_8 (p->value), scan);
3760 break;
3762 default:
3763 abort ();
3764 break;
3768 scan = emit_insn_after (gen_consttable_end (), scan);
3769 scan = emit_barrier_after (scan);
3770 pool_size = 0;
3773 /* Non zero if the src operand needs to be fixed up */
3774 static int
3775 fixit (src, mode, destreg)
3776 rtx src;
3777 enum machine_mode mode;
3778 int destreg;
3780 if (CONSTANT_P (src))
3782 if (GET_CODE (src) == CONST_INT)
3783 return (! const_ok_for_arm (INTVAL (src))
3784 && ! const_ok_for_arm (~INTVAL (src)));
3785 if (GET_CODE (src) == CONST_DOUBLE)
3786 return (GET_MODE (src) == VOIDmode
3787 || destreg < 16
3788 || (! const_double_rtx_ok_for_fpu (src)
3789 && ! neg_const_double_rtx_ok_for_fpu (src)));
3790 return symbol_mentioned_p (src);
3792 #ifndef AOF_ASSEMBLER
3793 else if (GET_CODE (src) == UNSPEC && XINT (src, 1) == 3)
3794 return 1;
3795 #endif
3796 else
3797 return (mode == SImode && GET_CODE (src) == MEM
3798 && GET_CODE (XEXP (src, 0)) == SYMBOL_REF
3799 && CONSTANT_POOL_ADDRESS_P (XEXP (src, 0)));
3802 /* Find the last barrier less than MAX_COUNT bytes from FROM, or create one. */
3803 static rtx
3804 find_barrier (from, max_count)
3805 rtx from;
3806 int max_count;
3808 int count = 0;
3809 rtx found_barrier = 0;
3810 rtx last = from;
3812 while (from && count < max_count)
3814 rtx tmp;
3816 if (GET_CODE (from) == BARRIER)
3817 found_barrier = from;
3819 /* Count the length of this insn */
3820 if (GET_CODE (from) == INSN
3821 && GET_CODE (PATTERN (from)) == SET
3822 && CONSTANT_P (SET_SRC (PATTERN (from)))
3823 && CONSTANT_POOL_ADDRESS_P (SET_SRC (PATTERN (from))))
3824 count += 8;
3825 /* Handle table jumps as a single entity. */
3826 else if (GET_CODE (from) == JUMP_INSN
3827 && JUMP_LABEL (from) != 0
3828 && ((tmp = next_real_insn (JUMP_LABEL (from)))
3829 == next_real_insn (from))
3830 && tmp != NULL
3831 && GET_CODE (tmp) == JUMP_INSN
3832 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
3833 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
3835 int elt = GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC ? 1 : 0;
3836 count += (get_attr_length (from)
3837 + GET_MODE_SIZE (SImode) * XVECLEN (PATTERN (tmp), elt));
3838 /* Continue after the dispatch table. */
3839 last = from;
3840 from = NEXT_INSN (tmp);
3841 continue;
3843 else
3844 count += get_attr_length (from);
3846 last = from;
3847 from = NEXT_INSN (from);
3850 if (! found_barrier)
3852 /* We didn't find a barrier in time to
3853 dump our stuff, so we'll make one. */
3854 rtx label = gen_label_rtx ();
3856 if (from)
3857 from = PREV_INSN (last);
3858 else
3859 from = get_last_insn ();
3861 /* Walk back to be just before any jump. */
3862 while (GET_CODE (from) == JUMP_INSN
3863 || GET_CODE (from) == NOTE
3864 || GET_CODE (from) == CODE_LABEL)
3865 from = PREV_INSN (from);
3867 from = emit_jump_insn_after (gen_jump (label), from);
3868 JUMP_LABEL (from) = label;
3869 found_barrier = emit_barrier_after (from);
3870 emit_label_after (label, found_barrier);
3873 return found_barrier;
3876 /* Non zero if the insn is a move instruction which needs to be fixed. */
3877 static int
3878 broken_move (insn)
3879 rtx insn;
3881 if (!INSN_DELETED_P (insn)
3882 && GET_CODE (insn) == INSN
3883 && GET_CODE (PATTERN (insn)) == SET)
3885 rtx pat = PATTERN (insn);
3886 rtx src = SET_SRC (pat);
3887 rtx dst = SET_DEST (pat);
3888 int destreg;
3889 enum machine_mode mode = GET_MODE (dst);
3891 if (dst == pc_rtx)
3892 return 0;
3894 if (GET_CODE (dst) == REG)
3895 destreg = REGNO (dst);
3896 else if (GET_CODE (dst) == SUBREG && GET_CODE (SUBREG_REG (dst)) == REG)
3897 destreg = REGNO (SUBREG_REG (dst));
3898 else
3899 return 0;
3901 return fixit (src, mode, destreg);
3903 return 0;
3906 void
3907 arm_reorg (first)
3908 rtx first;
3910 rtx insn;
3911 int count_size;
3913 #if 0
3914 /* The ldr instruction can work with up to a 4k offset, and most constants
3915 will be loaded with one of these instructions; however, the adr
3916 instruction and the ldf instructions only work with a 1k offset. This
3917 code needs to be rewritten to use the 4k offset when possible, and to
3918 adjust when a 1k offset is needed. For now we just use a 1k offset
3919 from the start. */
3920 count_size = 4000;
3922 /* Floating point operands can't work further than 1024 bytes from the
3923 PC, so to make things simple we restrict all loads for such functions.
3925 if (TARGET_HARD_FLOAT)
3927 int regno;
3929 for (regno = 16; regno < 24; regno++)
3930 if (regs_ever_live[regno])
3932 count_size = 1000;
3933 break;
3936 #else
3937 count_size = 1000;
3938 #endif /* 0 */
3940 for (insn = first; insn; insn = NEXT_INSN (insn))
3942 if (broken_move (insn))
3944 /* This is a broken move instruction, scan ahead looking for
3945 a barrier to stick the constant table behind */
3946 rtx scan;
3947 rtx barrier = find_barrier (insn, count_size);
3949 /* Now find all the moves between the points and modify them */
3950 for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
3952 if (broken_move (scan))
3954 /* This is a broken move instruction, add it to the pool */
3955 rtx pat = PATTERN (scan);
3956 rtx src = SET_SRC (pat);
3957 rtx dst = SET_DEST (pat);
3958 enum machine_mode mode = GET_MODE (dst);
3959 HOST_WIDE_INT offset;
3960 rtx newinsn = scan;
3961 rtx newsrc;
3962 rtx addr;
3963 int scratch;
3964 int address_only;
3966 /* If this is an HImode constant load, convert it into
3967 an SImode constant load. Since the register is always
3968 32 bits this is safe. We have to do this, since the
3969 load pc-relative instruction only does a 32-bit load. */
3970 if (mode == HImode)
3972 mode = SImode;
3973 if (GET_CODE (dst) != REG)
3974 abort ();
3975 PUT_MODE (dst, SImode);
3978 offset = add_constant (src, mode, &address_only);
3979 addr = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
3980 pool_vector_label),
3981 offset);
3983 /* If we only want the address of the pool entry, or
3984 for wide moves to integer regs we need to split
3985 the address calculation off into a separate insn.
3986 If necessary, the load can then be done with a
3987 load-multiple. This is safe, since we have
3988 already noted the length of such insns to be 8,
3989 and we are immediately over-writing the scratch
3990 we have grabbed with the final result. */
3991 if ((address_only || GET_MODE_SIZE (mode) > 4)
3992 && (scratch = REGNO (dst)) < 16)
3994 rtx reg;
3996 if (mode == SImode)
3997 reg = dst;
3998 else
3999 reg = gen_rtx_REG (SImode, scratch);
4001 newinsn = emit_insn_after (gen_movaddr (reg, addr),
4002 newinsn);
4003 addr = reg;
4006 if (! address_only)
4008 newsrc = gen_rtx_MEM (mode, addr);
4010 /* XXX Fixme -- I think the following is bogus. */
4011 /* Build a jump insn wrapper around the move instead
4012 of an ordinary insn, because we want to have room for
4013 the target label rtx in fld[7], which an ordinary
4014 insn doesn't have. */
4015 newinsn
4016 = emit_jump_insn_after (gen_rtx_SET (VOIDmode, dst,
4017 newsrc),
4018 newinsn);
4019 JUMP_LABEL (newinsn) = pool_vector_label;
4021 /* But it's still an ordinary insn */
4022 PUT_CODE (newinsn, INSN);
4025 /* Kill old insn */
4026 delete_insn (scan);
4027 scan = newinsn;
4030 dump_table (barrier);
4031 insn = scan;
4035 after_arm_reorg = 1;
4039 /* Routines to output assembly language. */
4041 /* If the rtx is the correct value then return the string of the number.
4042 In this way we can ensure that valid double constants are generated even
4043 when cross compiling. */
4044 char *
4045 fp_immediate_constant (x)
4046 rtx x;
4048 REAL_VALUE_TYPE r;
4049 int i;
4051 if (!fpa_consts_inited)
4052 init_fpa_table ();
4054 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4055 for (i = 0; i < 8; i++)
4056 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
4057 return strings_fpa[i];
4059 abort ();
4062 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
4063 static char *
4064 fp_const_from_val (r)
4065 REAL_VALUE_TYPE *r;
4067 int i;
4069 if (! fpa_consts_inited)
4070 init_fpa_table ();
4072 for (i = 0; i < 8; i++)
4073 if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
4074 return strings_fpa[i];
4076 abort ();
4079 /* Output the operands of a LDM/STM instruction to STREAM.
4080 MASK is the ARM register set mask of which only bits 0-15 are important.
4081 INSTR is the possibly suffixed base register. HAT unequals zero if a hat
4082 must follow the register list. */
4084 void
4085 print_multi_reg (stream, instr, mask, hat)
4086 FILE *stream;
4087 char *instr;
4088 int mask, hat;
4090 int i;
4091 int not_first = FALSE;
4093 fputc ('\t', stream);
4094 fprintf (stream, instr, REGISTER_PREFIX);
4095 fputs (", {", stream);
4096 for (i = 0; i < 16; i++)
4097 if (mask & (1 << i))
4099 if (not_first)
4100 fprintf (stream, ", ");
4101 fprintf (stream, "%s%s", REGISTER_PREFIX, reg_names[i]);
4102 not_first = TRUE;
4105 fprintf (stream, "}%s\n", hat ? "^" : "");
4108 /* Output a 'call' insn. */
4110 char *
4111 output_call (operands)
4112 rtx *operands;
4114 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
4116 if (REGNO (operands[0]) == 14)
4118 operands[0] = gen_rtx_REG (SImode, 12);
4119 output_asm_insn ("mov%?\t%0, %|lr", operands);
4121 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4123 if (TARGET_THUMB_INTERWORK)
4124 output_asm_insn ("bx%?\t%0", operands);
4125 else
4126 output_asm_insn ("mov%?\t%|pc, %0", operands);
4128 return "";
4131 static int
4132 eliminate_lr2ip (x)
4133 rtx *x;
4135 int something_changed = 0;
4136 rtx x0 = *x;
4137 int code = GET_CODE (x0);
4138 register int i, j;
4139 register char *fmt;
4141 switch (code)
4143 case REG:
4144 if (REGNO (x0) == 14)
4146 *x = gen_rtx_REG (SImode, 12);
4147 return 1;
4149 return 0;
4150 default:
4151 /* Scan through the sub-elements and change any references there */
4152 fmt = GET_RTX_FORMAT (code);
4153 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4154 if (fmt[i] == 'e')
4155 something_changed |= eliminate_lr2ip (&XEXP (x0, i));
4156 else if (fmt[i] == 'E')
4157 for (j = 0; j < XVECLEN (x0, i); j++)
4158 something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
4159 return something_changed;
4163 /* Output a 'call' insn that is a reference in memory. */
4165 char *
4166 output_call_mem (operands)
4167 rtx *operands;
4169 operands[0] = copy_rtx (operands[0]); /* Be ultra careful */
4170 /* Handle calls using lr by using ip (which may be clobbered in subr anyway).
4172 if (eliminate_lr2ip (&operands[0]))
4173 output_asm_insn ("mov%?\t%|ip, %|lr", operands);
4175 if (TARGET_THUMB_INTERWORK)
4177 output_asm_insn ("ldr%?\t%|ip, %0", operands);
4178 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4179 output_asm_insn ("bx%?\t%|ip", operands);
4181 else
4183 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4184 output_asm_insn ("ldr%?\t%|pc, %0", operands);
4187 return "";
4191 /* Output a move from arm registers to an fpu registers.
4192 OPERANDS[0] is an fpu register.
4193 OPERANDS[1] is the first registers of an arm register pair. */
4195 char *
4196 output_mov_long_double_fpu_from_arm (operands)
4197 rtx *operands;
4199 int arm_reg0 = REGNO (operands[1]);
4200 rtx ops[3];
4202 if (arm_reg0 == 12)
4203 abort();
4205 ops[0] = gen_rtx_REG (SImode, arm_reg0);
4206 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4207 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
4209 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
4210 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
4211 return "";
4214 /* Output a move from an fpu register to arm registers.
4215 OPERANDS[0] is the first registers of an arm register pair.
4216 OPERANDS[1] is an fpu register. */
4218 char *
4219 output_mov_long_double_arm_from_fpu (operands)
4220 rtx *operands;
4222 int arm_reg0 = REGNO (operands[0]);
4223 rtx ops[3];
4225 if (arm_reg0 == 12)
4226 abort();
4228 ops[0] = gen_rtx_REG (SImode, arm_reg0);
4229 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4230 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
4232 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
4233 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
4234 return "";
4237 /* Output a move from arm registers to arm registers of a long double
4238 OPERANDS[0] is the destination.
4239 OPERANDS[1] is the source. */
4240 char *
4241 output_mov_long_double_arm_from_arm (operands)
4242 rtx *operands;
4244 /* We have to be careful here because the two might overlap */
4245 int dest_start = REGNO (operands[0]);
4246 int src_start = REGNO (operands[1]);
4247 rtx ops[2];
4248 int i;
4250 if (dest_start < src_start)
4252 for (i = 0; i < 3; i++)
4254 ops[0] = gen_rtx_REG (SImode, dest_start + i);
4255 ops[1] = gen_rtx_REG (SImode, src_start + i);
4256 output_asm_insn ("mov%?\t%0, %1", ops);
4259 else
4261 for (i = 2; i >= 0; i--)
4263 ops[0] = gen_rtx_REG (SImode, dest_start + i);
4264 ops[1] = gen_rtx_REG (SImode, src_start + i);
4265 output_asm_insn ("mov%?\t%0, %1", ops);
4269 return "";
4273 /* Output a move from arm registers to an fpu registers.
4274 OPERANDS[0] is an fpu register.
4275 OPERANDS[1] is the first registers of an arm register pair. */
4277 char *
4278 output_mov_double_fpu_from_arm (operands)
4279 rtx *operands;
4281 int arm_reg0 = REGNO (operands[1]);
4282 rtx ops[2];
4284 if (arm_reg0 == 12)
4285 abort();
4286 ops[0] = gen_rtx_REG (SImode, arm_reg0);
4287 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4288 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
4289 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
4290 return "";
4293 /* Output a move from an fpu register to arm registers.
4294 OPERANDS[0] is the first registers of an arm register pair.
4295 OPERANDS[1] is an fpu register. */
4297 char *
4298 output_mov_double_arm_from_fpu (operands)
4299 rtx *operands;
4301 int arm_reg0 = REGNO (operands[0]);
4302 rtx ops[2];
4304 if (arm_reg0 == 12)
4305 abort();
4307 ops[0] = gen_rtx_REG (SImode, arm_reg0);
4308 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4309 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
4310 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
4311 return "";
4314 /* Output a move between double words.
4315 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
4316 or MEM<-REG and all MEMs must be offsettable addresses. */
4318 char *
4319 output_move_double (operands)
4320 rtx *operands;
4322 enum rtx_code code0 = GET_CODE (operands[0]);
4323 enum rtx_code code1 = GET_CODE (operands[1]);
4324 rtx otherops[3];
4326 if (code0 == REG)
4328 int reg0 = REGNO (operands[0]);
4330 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
4331 if (code1 == REG)
4333 int reg1 = REGNO (operands[1]);
4334 if (reg1 == 12)
4335 abort();
4337 /* Ensure the second source is not overwritten */
4338 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
4339 output_asm_insn("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
4340 else
4341 output_asm_insn("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
4343 else if (code1 == CONST_DOUBLE)
4345 if (GET_MODE (operands[1]) == DFmode)
4347 long l[2];
4348 union real_extract u;
4350 bcopy ((char *) &CONST_DOUBLE_LOW (operands[1]), (char *) &u,
4351 sizeof (u));
4352 REAL_VALUE_TO_TARGET_DOUBLE (u.d, l);
4353 otherops[1] = GEN_INT(l[1]);
4354 operands[1] = GEN_INT(l[0]);
4356 else if (GET_MODE (operands[1]) != VOIDmode)
4357 abort ();
4358 else if (WORDS_BIG_ENDIAN)
4361 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
4362 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
4364 else
4367 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
4368 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
4370 output_mov_immediate (operands);
4371 output_mov_immediate (otherops);
4373 else if (code1 == CONST_INT)
4375 #if HOST_BITS_PER_WIDE_INT > 32
4376 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
4377 what the upper word is. */
4378 if (WORDS_BIG_ENDIAN)
4380 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
4381 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
4383 else
4385 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
4386 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
4388 #else
4389 /* Sign extend the intval into the high-order word */
4390 if (WORDS_BIG_ENDIAN)
4392 otherops[1] = operands[1];
4393 operands[1] = (INTVAL (operands[1]) < 0
4394 ? constm1_rtx : const0_rtx);
4396 else
4397 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
4398 #endif
4399 output_mov_immediate (otherops);
4400 output_mov_immediate (operands);
4402 else if (code1 == MEM)
4404 switch (GET_CODE (XEXP (operands[1], 0)))
4406 case REG:
4407 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
4408 break;
4410 case PRE_INC:
4411 abort (); /* Should never happen now */
4412 break;
4414 case PRE_DEC:
4415 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
4416 break;
4418 case POST_INC:
4419 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
4420 break;
4422 case POST_DEC:
4423 abort (); /* Should never happen now */
4424 break;
4426 case LABEL_REF:
4427 case CONST:
4428 output_asm_insn ("adr%?\t%0, %1", operands);
4429 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
4430 break;
4432 default:
4433 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1)))
4435 otherops[0] = operands[0];
4436 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
4437 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
4438 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
4440 if (GET_CODE (otherops[2]) == CONST_INT)
4442 switch (INTVAL (otherops[2]))
4444 case -8:
4445 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
4446 return "";
4447 case -4:
4448 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
4449 return "";
4450 case 4:
4451 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
4452 return "";
4454 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
4455 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
4456 else
4457 output_asm_insn ("add%?\t%0, %1, %2", otherops);
4459 else
4460 output_asm_insn ("add%?\t%0, %1, %2", otherops);
4462 else
4463 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
4464 return "ldm%?ia\t%0, %M0";
4466 else
4468 otherops[1] = adj_offsettable_operand (operands[1], 4);
4469 /* Take care of overlapping base/data reg. */
4470 if (reg_mentioned_p (operands[0], operands[1]))
4472 output_asm_insn ("ldr%?\t%0, %1", otherops);
4473 output_asm_insn ("ldr%?\t%0, %1", operands);
4475 else
4477 output_asm_insn ("ldr%?\t%0, %1", operands);
4478 output_asm_insn ("ldr%?\t%0, %1", otherops);
4483 else
4484 abort(); /* Constraints should prevent this */
4486 else if (code0 == MEM && code1 == REG)
4488 if (REGNO (operands[1]) == 12)
4489 abort();
4491 switch (GET_CODE (XEXP (operands[0], 0)))
4493 case REG:
4494 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
4495 break;
4497 case PRE_INC:
4498 abort (); /* Should never happen now */
4499 break;
4501 case PRE_DEC:
4502 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
4503 break;
4505 case POST_INC:
4506 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
4507 break;
4509 case POST_DEC:
4510 abort (); /* Should never happen now */
4511 break;
4513 case PLUS:
4514 if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
4516 switch (INTVAL (XEXP (XEXP (operands[0], 0), 1)))
4518 case -8:
4519 output_asm_insn ("stm%?db\t%m0, %M1", operands);
4520 return "";
4522 case -4:
4523 output_asm_insn ("stm%?da\t%m0, %M1", operands);
4524 return "";
4526 case 4:
4527 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
4528 return "";
4531 /* Fall through */
4533 default:
4534 otherops[0] = adj_offsettable_operand (operands[0], 4);
4535 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
4536 output_asm_insn ("str%?\t%1, %0", operands);
4537 output_asm_insn ("str%?\t%1, %0", otherops);
4540 else
4541 abort(); /* Constraints should prevent this */
4543 return "";
4547 /* Output an arbitrary MOV reg, #n.
4548 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
4550 char *
4551 output_mov_immediate (operands)
4552 rtx *operands;
4554 HOST_WIDE_INT n = INTVAL (operands[1]);
4555 int n_ones = 0;
4556 int i;
4558 /* Try to use one MOV */
4559 if (const_ok_for_arm (n))
4561 output_asm_insn ("mov%?\t%0, %1", operands);
4562 return "";
4565 /* Try to use one MVN */
4566 if (const_ok_for_arm (~n))
4568 operands[1] = GEN_INT (~n);
4569 output_asm_insn ("mvn%?\t%0, %1", operands);
4570 return "";
4573 /* If all else fails, make it out of ORRs or BICs as appropriate. */
4575 for (i=0; i < 32; i++)
4576 if (n & 1 << i)
4577 n_ones++;
4579 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
4580 output_multi_immediate(operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1,
4581 ~n);
4582 else
4583 output_multi_immediate(operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1,
4586 return "";
4590 /* Output an ADD r, s, #n where n may be too big for one instruction. If
4591 adding zero to one register, output nothing. */
4593 char *
4594 output_add_immediate (operands)
4595 rtx *operands;
4597 HOST_WIDE_INT n = INTVAL (operands[2]);
4599 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
4601 if (n < 0)
4602 output_multi_immediate (operands,
4603 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
4604 -n);
4605 else
4606 output_multi_immediate (operands,
4607 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
4611 return "";
4614 /* Output a multiple immediate operation.
4615 OPERANDS is the vector of operands referred to in the output patterns.
4616 INSTR1 is the output pattern to use for the first constant.
4617 INSTR2 is the output pattern to use for subsequent constants.
4618 IMMED_OP is the index of the constant slot in OPERANDS.
4619 N is the constant value. */
4621 static char *
4622 output_multi_immediate (operands, instr1, instr2, immed_op, n)
4623 rtx *operands;
4624 char *instr1, *instr2;
4625 int immed_op;
4626 HOST_WIDE_INT n;
4628 #if HOST_BITS_PER_WIDE_INT > 32
4629 n &= 0xffffffff;
4630 #endif
4632 if (n == 0)
4634 operands[immed_op] = const0_rtx;
4635 output_asm_insn (instr1, operands); /* Quick and easy output */
4637 else
4639 int i;
4640 char *instr = instr1;
4642 /* Note that n is never zero here (which would give no output) */
4643 for (i = 0; i < 32; i += 2)
4645 if (n & (3 << i))
4647 operands[immed_op] = GEN_INT (n & (255 << i));
4648 output_asm_insn (instr, operands);
4649 instr = instr2;
4650 i += 6;
4654 return "";
4658 /* Return the appropriate ARM instruction for the operation code.
4659 The returned result should not be overwritten. OP is the rtx of the
4660 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
4661 was shifted. */
4663 char *
4664 arithmetic_instr (op, shift_first_arg)
4665 rtx op;
4666 int shift_first_arg;
4668 switch (GET_CODE (op))
4670 case PLUS:
4671 return "add";
4673 case MINUS:
4674 return shift_first_arg ? "rsb" : "sub";
4676 case IOR:
4677 return "orr";
4679 case XOR:
4680 return "eor";
4682 case AND:
4683 return "and";
4685 default:
4686 abort ();
4691 /* Ensure valid constant shifts and return the appropriate shift mnemonic
4692 for the operation code. The returned result should not be overwritten.
4693 OP is the rtx code of the shift.
4694 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
4695 shift. */
4697 static char *
4698 shift_op (op, amountp)
4699 rtx op;
4700 HOST_WIDE_INT *amountp;
4702 char *mnem;
4703 enum rtx_code code = GET_CODE (op);
4705 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
4706 *amountp = -1;
4707 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
4708 *amountp = INTVAL (XEXP (op, 1));
4709 else
4710 abort ();
4712 switch (code)
4714 case ASHIFT:
4715 mnem = "asl";
4716 break;
4718 case ASHIFTRT:
4719 mnem = "asr";
4720 break;
4722 case LSHIFTRT:
4723 mnem = "lsr";
4724 break;
4726 case ROTATERT:
4727 mnem = "ror";
4728 break;
4730 case MULT:
4731 /* We never have to worry about the amount being other than a
4732 power of 2, since this case can never be reloaded from a reg. */
4733 if (*amountp != -1)
4734 *amountp = int_log2 (*amountp);
4735 else
4736 abort ();
4737 return "asl";
4739 default:
4740 abort ();
4743 if (*amountp != -1)
4745 /* This is not 100% correct, but follows from the desire to merge
4746 multiplication by a power of 2 with the recognizer for a
4747 shift. >=32 is not a valid shift for "asl", so we must try and
4748 output a shift that produces the correct arithmetical result.
4749 Using lsr #32 is identical except for the fact that the carry bit
4750 is not set correctly if we set the flags; but we never use the
4751 carry bit from such an operation, so we can ignore that. */
4752 if (code == ROTATERT)
4753 *amountp &= 31; /* Rotate is just modulo 32 */
4754 else if (*amountp != (*amountp & 31))
4756 if (code == ASHIFT)
4757 mnem = "lsr";
4758 *amountp = 32;
4761 /* Shifts of 0 are no-ops. */
4762 if (*amountp == 0)
4763 return NULL;
4766 return mnem;
4770 /* Obtain the shift from the POWER of two. */
4772 static HOST_WIDE_INT
4773 int_log2 (power)
4774 HOST_WIDE_INT power;
4776 HOST_WIDE_INT shift = 0;
4778 while (((((HOST_WIDE_INT) 1) << shift) & power) == 0)
4780 if (shift > 31)
4781 abort ();
4782 shift++;
4785 return shift;
4788 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
4789 /bin/as is horribly restrictive. */
4791 void
4792 output_ascii_pseudo_op (stream, p, len)
4793 FILE *stream;
4794 unsigned char *p;
4795 int len;
4797 int i;
4798 int len_so_far = 1000;
4799 int chars_so_far = 0;
4801 for (i = 0; i < len; i++)
4803 register int c = p[i];
4805 if (len_so_far > 50)
4807 if (chars_so_far)
4808 fputs ("\"\n", stream);
4809 fputs ("\t.ascii\t\"", stream);
4810 len_so_far = 0;
4811 chars_so_far = 0;
4814 if (c == '\"' || c == '\\')
4816 putc('\\', stream);
4817 len_so_far++;
4820 if (c >= ' ' && c < 0177)
4822 putc (c, stream);
4823 len_so_far++;
4825 else
4827 fprintf (stream, "\\%03o", c);
4828 len_so_far +=4;
4831 chars_so_far++;
4834 fputs ("\"\n", stream);
4838 /* Try to determine whether a pattern really clobbers the link register.
4839 This information is useful when peepholing, so that lr need not be pushed
4840 if we combine a call followed by a return.
4841 NOTE: This code does not check for side-effect expressions in a SET_SRC:
4842 such a check should not be needed because these only update an existing
4843 value within a register; the register must still be set elsewhere within
4844 the function. */
4846 static int
4847 pattern_really_clobbers_lr (x)
4848 rtx x;
4850 int i;
4852 switch (GET_CODE (x))
4854 case SET:
4855 switch (GET_CODE (SET_DEST (x)))
4857 case REG:
4858 return REGNO (SET_DEST (x)) == 14;
4860 case SUBREG:
4861 if (GET_CODE (XEXP (SET_DEST (x), 0)) == REG)
4862 return REGNO (XEXP (SET_DEST (x), 0)) == 14;
4864 if (GET_CODE (XEXP (SET_DEST (x), 0)) == MEM)
4865 return 0;
4866 abort ();
4868 default:
4869 return 0;
4872 case PARALLEL:
4873 for (i = 0; i < XVECLEN (x, 0); i++)
4874 if (pattern_really_clobbers_lr (XVECEXP (x, 0, i)))
4875 return 1;
4876 return 0;
4878 case CLOBBER:
4879 switch (GET_CODE (XEXP (x, 0)))
4881 case REG:
4882 return REGNO (XEXP (x, 0)) == 14;
4884 case SUBREG:
4885 if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG)
4886 return REGNO (XEXP (XEXP (x, 0), 0)) == 14;
4887 abort ();
4889 default:
4890 return 0;
4893 case UNSPEC:
4894 return 1;
4896 default:
4897 return 0;
4901 static int
4902 function_really_clobbers_lr (first)
4903 rtx first;
4905 rtx insn, next;
4907 for (insn = first; insn; insn = next_nonnote_insn (insn))
4909 switch (GET_CODE (insn))
4911 case BARRIER:
4912 case NOTE:
4913 case CODE_LABEL:
4914 case JUMP_INSN: /* Jump insns only change the PC (and conds) */
4915 case INLINE_HEADER:
4916 break;
4918 case INSN:
4919 if (pattern_really_clobbers_lr (PATTERN (insn)))
4920 return 1;
4921 break;
4923 case CALL_INSN:
4924 /* Don't yet know how to handle those calls that are not to a
4925 SYMBOL_REF */
4926 if (GET_CODE (PATTERN (insn)) != PARALLEL)
4927 abort ();
4929 switch (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)))
4931 case CALL:
4932 if (GET_CODE (XEXP (XEXP (XVECEXP (PATTERN (insn), 0, 0), 0), 0))
4933 != SYMBOL_REF)
4934 return 1;
4935 break;
4937 case SET:
4938 if (GET_CODE (XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn),
4939 0, 0)), 0), 0))
4940 != SYMBOL_REF)
4941 return 1;
4942 break;
4944 default: /* Don't recognize it, be safe */
4945 return 1;
4948 /* A call can be made (by peepholing) not to clobber lr iff it is
4949 followed by a return. There may, however, be a use insn iff
4950 we are returning the result of the call.
4951 If we run off the end of the insn chain, then that means the
4952 call was at the end of the function. Unfortunately we don't
4953 have a return insn for the peephole to recognize, so we
4954 must reject this. (Can this be fixed by adding our own insn?) */
4955 if ((next = next_nonnote_insn (insn)) == NULL)
4956 return 1;
4958 /* No need to worry about lr if the call never returns */
4959 if (GET_CODE (next) == BARRIER)
4960 break;
4962 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == USE
4963 && (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
4964 && (REGNO (SET_DEST (XVECEXP (PATTERN (insn), 0, 0)))
4965 == REGNO (XEXP (PATTERN (next), 0))))
4966 if ((next = next_nonnote_insn (next)) == NULL)
4967 return 1;
4969 if (GET_CODE (next) == JUMP_INSN
4970 && GET_CODE (PATTERN (next)) == RETURN)
4971 break;
4972 return 1;
4974 default:
4975 abort ();
4979 /* We have reached the end of the chain so lr was _not_ clobbered */
4980 return 0;
4983 char *
4984 output_return_instruction (operand, really_return, reverse)
4985 rtx operand;
4986 int really_return;
4987 int reverse;
4989 char instr[100];
4990 int reg, live_regs = 0;
4991 int volatile_func = (optimize > 0
4992 && TREE_THIS_VOLATILE (current_function_decl));
4994 return_used_this_function = 1;
4996 if (volatile_func)
4998 rtx ops[2];
4999 /* If this function was declared non-returning, and we have found a tail
5000 call, then we have to trust that the called function won't return. */
5001 if (! really_return)
5002 return "";
5004 /* Otherwise, trap an attempted return by aborting. */
5005 ops[0] = operand;
5006 ops[1] = gen_rtx_SYMBOL_REF (Pmode, "abort");
5007 assemble_external_libcall (ops[1]);
5008 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
5009 return "";
5012 if (current_function_calls_alloca && ! really_return)
5013 abort();
5015 for (reg = 0; reg <= 10; reg++)
5016 if (regs_ever_live[reg] && ! call_used_regs[reg])
5017 live_regs++;
5019 if (live_regs || (regs_ever_live[14] && ! lr_save_eliminated))
5020 live_regs++;
5022 if (frame_pointer_needed)
5023 live_regs += 4;
5025 if (live_regs)
5027 if (lr_save_eliminated || ! regs_ever_live[14])
5028 live_regs++;
5030 if (frame_pointer_needed)
5031 strcpy (instr,
5032 reverse ? "ldm%?%D0ea\t%|fp, {" : "ldm%?%d0ea\t%|fp, {");
5033 else
5034 strcpy (instr,
5035 reverse ? "ldm%?%D0fd\t%|sp!, {" : "ldm%?%d0fd\t%|sp!, {");
5037 for (reg = 0; reg <= 10; reg++)
5038 if (regs_ever_live[reg] && ! call_used_regs[reg])
5040 strcat (instr, "%|");
5041 strcat (instr, reg_names[reg]);
5042 if (--live_regs)
5043 strcat (instr, ", ");
5046 if (frame_pointer_needed)
5048 strcat (instr, "%|");
5049 strcat (instr, reg_names[11]);
5050 strcat (instr, ", ");
5051 strcat (instr, "%|");
5052 strcat (instr, reg_names[13]);
5053 strcat (instr, ", ");
5054 strcat (instr, "%|");
5055 strcat (instr, TARGET_THUMB_INTERWORK || (! really_return)
5056 ? reg_names[14] : reg_names[15] );
5058 else
5060 strcat (instr, "%|");
5061 if (TARGET_THUMB_INTERWORK && really_return)
5062 strcat (instr, reg_names[12]);
5063 else
5064 strcat (instr, really_return ? reg_names[15] : reg_names[14]);
5066 strcat (instr, (TARGET_APCS_32 || !really_return) ? "}" : "}^");
5067 output_asm_insn (instr, &operand);
5069 if (TARGET_THUMB_INTERWORK && really_return)
5071 strcpy (instr, "bx%?");
5072 strcat (instr, reverse ? "%D0" : "%d0");
5073 strcat (instr, "\t%|");
5074 strcat (instr, frame_pointer_needed ? "lr" : "ip");
5076 output_asm_insn (instr, & operand);
5079 else if (really_return)
5081 if (TARGET_THUMB_INTERWORK)
5082 sprintf (instr, "bx%%?%%%s0\t%%|lr", reverse ? "D" : "d");
5083 else
5084 sprintf (instr, "mov%%?%%%s0%s\t%%|pc, %%|lr",
5085 reverse ? "D" : "d", TARGET_APCS_32 ? "" : "s");
5087 output_asm_insn (instr, & operand);
5090 return "";
5093 /* Return nonzero if optimizing and the current function is volatile.
5094 Such functions never return, and many memory cycles can be saved
5095 by not storing register values that will never be needed again.
5096 This optimization was added to speed up context switching in a
5097 kernel application. */
5100 arm_volatile_func ()
5102 return (optimize > 0 && TREE_THIS_VOLATILE (current_function_decl));
5105 /* The amount of stack adjustment that happens here, in output_return and in
5106 output_epilogue must be exactly the same as was calculated during reload,
5107 or things will point to the wrong place. The only time we can safely
5108 ignore this constraint is when a function has no arguments on the stack,
5109 no stack frame requirement and no live registers execpt for `lr'. If we
5110 can guarantee that by making all function calls into tail calls and that
5111 lr is not clobbered in any other way, then there is no need to push lr
5112 onto the stack. */
5114 void
5115 output_func_prologue (f, frame_size)
5116 FILE *f;
5117 int frame_size;
5119 int reg, live_regs_mask = 0;
5120 int volatile_func = (optimize > 0
5121 && TREE_THIS_VOLATILE (current_function_decl));
5123 /* Nonzero if we must stuff some register arguments onto the stack as if
5124 they were passed there. */
5125 int store_arg_regs = 0;
5127 if (arm_ccfsm_state || arm_target_insn)
5128 abort (); /* Sanity check */
5130 if (arm_naked_function_p (current_function_decl))
5131 return;
5133 return_used_this_function = 0;
5134 lr_save_eliminated = 0;
5136 fprintf (f, "\t%s args = %d, pretend = %d, frame = %d\n",
5137 ASM_COMMENT_START, current_function_args_size,
5138 current_function_pretend_args_size, frame_size);
5139 fprintf (f, "\t%s frame_needed = %d, current_function_anonymous_args = %d\n",
5140 ASM_COMMENT_START, frame_pointer_needed,
5141 current_function_anonymous_args);
5143 if (volatile_func)
5144 fprintf (f, "\t%s Volatile function.\n", ASM_COMMENT_START);
5146 if (current_function_anonymous_args && current_function_pretend_args_size)
5147 store_arg_regs = 1;
5149 for (reg = 0; reg <= 10; reg++)
5150 if (regs_ever_live[reg] && ! call_used_regs[reg])
5151 live_regs_mask |= (1 << reg);
5153 if (frame_pointer_needed)
5154 live_regs_mask |= 0xD800;
5155 else if (regs_ever_live[14])
5157 if (! current_function_args_size
5158 && ! function_really_clobbers_lr (get_insns ()))
5159 lr_save_eliminated = 1;
5160 else
5161 live_regs_mask |= 0x4000;
5164 if (live_regs_mask)
5166 /* if a di mode load/store multiple is used, and the base register
5167 is r3, then r4 can become an ever live register without lr
5168 doing so, in this case we need to push lr as well, or we
5169 will fail to get a proper return. */
5171 live_regs_mask |= 0x4000;
5172 lr_save_eliminated = 0;
5176 if (lr_save_eliminated)
5177 fprintf (f,"\t%s I don't think this function clobbers lr\n",
5178 ASM_COMMENT_START);
5180 #ifdef AOF_ASSEMBLER
5181 if (flag_pic)
5182 fprintf (f, "\tmov\t%sip, %s%s\n", REGISTER_PREFIX, REGISTER_PREFIX,
5183 reg_names[PIC_OFFSET_TABLE_REGNUM]);
5184 #endif
5188 void
5189 output_func_epilogue (f, frame_size)
5190 FILE *f;
5191 int frame_size;
5193 int reg, live_regs_mask = 0;
5194 /* If we need this then it will always be at least this much */
5195 int floats_offset = 12;
5196 rtx operands[3];
5197 int volatile_func = (optimize > 0
5198 && TREE_THIS_VOLATILE (current_function_decl));
5200 if (use_return_insn (FALSE) && return_used_this_function)
5202 if ((frame_size + current_function_outgoing_args_size) != 0
5203 && !(frame_pointer_needed && TARGET_APCS))
5204 abort ();
5205 goto epilogue_done;
5208 /* Naked functions don't have epilogues. */
5209 if (arm_naked_function_p (current_function_decl))
5210 goto epilogue_done;
5212 /* A volatile function should never return. Call abort. */
5213 if (TARGET_ABORT_NORETURN && volatile_func)
5215 rtx op = gen_rtx_SYMBOL_REF (Pmode, "abort");
5216 assemble_external_libcall (op);
5217 output_asm_insn ("bl\t%a0", &op);
5218 goto epilogue_done;
5221 for (reg = 0; reg <= 10; reg++)
5222 if (regs_ever_live[reg] && ! call_used_regs[reg])
5224 live_regs_mask |= (1 << reg);
5225 floats_offset += 4;
5228 if (frame_pointer_needed)
5230 if (arm_fpu_arch == FP_SOFT2)
5232 for (reg = 23; reg > 15; reg--)
5233 if (regs_ever_live[reg] && ! call_used_regs[reg])
5235 floats_offset += 12;
5236 fprintf (f, "\tldfe\t%s%s, [%sfp, #-%d]\n", REGISTER_PREFIX,
5237 reg_names[reg], REGISTER_PREFIX, floats_offset);
5240 else
5242 int start_reg = 23;
5244 for (reg = 23; reg > 15; reg--)
5246 if (regs_ever_live[reg] && ! call_used_regs[reg])
5248 floats_offset += 12;
5249 /* We can't unstack more than four registers at once */
5250 if (start_reg - reg == 3)
5252 fprintf (f, "\tlfm\t%s%s, 4, [%sfp, #-%d]\n",
5253 REGISTER_PREFIX, reg_names[reg],
5254 REGISTER_PREFIX, floats_offset);
5255 start_reg = reg - 1;
5258 else
5260 if (reg != start_reg)
5261 fprintf (f, "\tlfm\t%s%s, %d, [%sfp, #-%d]\n",
5262 REGISTER_PREFIX, reg_names[reg + 1],
5263 start_reg - reg, REGISTER_PREFIX, floats_offset);
5265 start_reg = reg - 1;
5269 /* Just in case the last register checked also needs unstacking. */
5270 if (reg != start_reg)
5271 fprintf (f, "\tlfm\t%s%s, %d, [%sfp, #-%d]\n",
5272 REGISTER_PREFIX, reg_names[reg + 1],
5273 start_reg - reg, REGISTER_PREFIX, floats_offset);
5276 if (TARGET_THUMB_INTERWORK)
5278 live_regs_mask |= 0x6800;
5279 print_multi_reg (f, "ldmea\t%sfp", live_regs_mask, FALSE);
5280 fprintf (f, "\tbx\t%slr\n", REGISTER_PREFIX);
5282 else
5284 live_regs_mask |= 0xA800;
5285 print_multi_reg (f, "ldmea\t%sfp", live_regs_mask,
5286 TARGET_APCS_32 ? FALSE : TRUE);
5289 else
5291 /* Restore stack pointer if necessary. */
5292 if (frame_size + current_function_outgoing_args_size != 0)
5294 operands[0] = operands[1] = stack_pointer_rtx;
5295 operands[2] = GEN_INT (frame_size
5296 + current_function_outgoing_args_size);
5297 output_add_immediate (operands);
5300 if (arm_fpu_arch == FP_SOFT2)
5302 for (reg = 16; reg < 24; reg++)
5303 if (regs_ever_live[reg] && ! call_used_regs[reg])
5304 fprintf (f, "\tldfe\t%s%s, [%ssp], #12\n", REGISTER_PREFIX,
5305 reg_names[reg], REGISTER_PREFIX);
5307 else
5309 int start_reg = 16;
5311 for (reg = 16; reg < 24; reg++)
5313 if (regs_ever_live[reg] && ! call_used_regs[reg])
5315 if (reg - start_reg == 3)
5317 fprintf (f, "\tlfmfd\t%s%s, 4, [%ssp]!\n",
5318 REGISTER_PREFIX, reg_names[start_reg],
5319 REGISTER_PREFIX);
5320 start_reg = reg + 1;
5323 else
5325 if (reg != start_reg)
5326 fprintf (f, "\tlfmfd\t%s%s, %d, [%ssp]!\n",
5327 REGISTER_PREFIX, reg_names[start_reg],
5328 reg - start_reg, REGISTER_PREFIX);
5330 start_reg = reg + 1;
5334 /* Just in case the last register checked also needs unstacking. */
5335 if (reg != start_reg)
5336 fprintf (f, "\tlfmfd\t%s%s, %d, [%ssp]!\n",
5337 REGISTER_PREFIX, reg_names[start_reg],
5338 reg - start_reg, REGISTER_PREFIX);
5341 if (current_function_pretend_args_size == 0 && regs_ever_live[14])
5343 if (TARGET_THUMB_INTERWORK)
5345 if (! lr_save_eliminated)
5346 live_regs_mask |= 0x4000;
5348 if (live_regs_mask != 0)
5349 print_multi_reg (f, "ldmfd\t%ssp!", live_regs_mask, FALSE);
5351 fprintf (f, "\tbx\t%slr\n", REGISTER_PREFIX);
5353 else if (lr_save_eliminated)
5354 fprintf (f, (TARGET_APCS_32 ? "\tmov\t%spc, %slr\n"
5355 : "\tmovs\t%spc, %slr\n"),
5356 REGISTER_PREFIX, REGISTER_PREFIX, f);
5357 else
5358 print_multi_reg (f, "ldmfd\t%ssp!", live_regs_mask | 0x8000,
5359 TARGET_APCS_32 ? FALSE : TRUE);
5361 else
5363 if (live_regs_mask || regs_ever_live[14])
5365 /* Restore the integer regs, and the return address into lr */
5366 if (! lr_save_eliminated)
5367 live_regs_mask |= 0x4000;
5369 if (live_regs_mask != 0)
5370 print_multi_reg (f, "ldmfd\t%ssp!", live_regs_mask, FALSE);
5373 if (current_function_pretend_args_size)
5375 /* Unwind the pre-pushed regs */
5376 operands[0] = operands[1] = stack_pointer_rtx;
5377 operands[2] = GEN_INT (current_function_pretend_args_size);
5378 output_add_immediate (operands);
5380 /* And finally, go home */
5381 if (TARGET_THUMB_INTERWORK)
5382 fprintf (f, "\tbx\t%slr\n", REGISTER_PREFIX);
5383 else if (TARGET_APCS_32)
5384 fprintf (f, "\tmov\t%spc, %slr\n", REGISTER_PREFIX, REGISTER_PREFIX );
5385 else
5386 fprintf (f, "\tmovs\t%spc, %slr\n", REGISTER_PREFIX, REGISTER_PREFIX );
5390 epilogue_done:
5392 /* Reset the ARM-specific per-function variables. */
5393 current_function_anonymous_args = 0;
5394 after_arm_reorg = 0;
5397 static void
5398 emit_multi_reg_push (mask)
5399 int mask;
5401 int num_regs = 0;
5402 int i, j;
5403 rtx par;
5405 for (i = 0; i < 16; i++)
5406 if (mask & (1 << i))
5407 num_regs++;
5409 if (num_regs == 0 || num_regs > 16)
5410 abort ();
5412 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
5414 for (i = 0; i < 16; i++)
5416 if (mask & (1 << i))
5418 XVECEXP (par, 0, 0)
5419 = gen_rtx_SET (VOIDmode,
5420 gen_rtx_MEM (BLKmode,
5421 gen_rtx_PRE_DEC (BLKmode,
5422 stack_pointer_rtx)),
5423 gen_rtx_UNSPEC (BLKmode,
5424 gen_rtvec (1,
5425 gen_rtx_REG (SImode, i)),
5426 2));
5427 break;
5431 for (j = 1, i++; j < num_regs; i++)
5433 if (mask & (1 << i))
5435 XVECEXP (par, 0, j)
5436 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, i));
5437 j++;
5441 emit_insn (par);
5444 static void
5445 emit_sfm (base_reg, count)
5446 int base_reg;
5447 int count;
5449 rtx par;
5450 int i;
5452 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
5454 XVECEXP (par, 0, 0)
5455 = gen_rtx_SET (VOIDmode,
5456 gen_rtx_MEM (BLKmode,
5457 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
5458 gen_rtx_UNSPEC (BLKmode,
5459 gen_rtvec (1, gen_rtx_REG (XFmode,
5460 base_reg++)),
5461 2));
5462 for (i = 1; i < count; i++)
5463 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode,
5464 gen_rtx_REG (XFmode, base_reg++));
5466 emit_insn (par);
5469 void
5470 arm_expand_prologue ()
5472 int reg;
5473 rtx amount = GEN_INT (-(get_frame_size ()
5474 + current_function_outgoing_args_size));
5475 int live_regs_mask = 0;
5476 int store_arg_regs = 0;
5477 int volatile_func = (optimize > 0
5478 && TREE_THIS_VOLATILE (current_function_decl));
5480 /* Naked functions don't have prologues. */
5481 if (arm_naked_function_p (current_function_decl))
5482 return;
5484 if (current_function_anonymous_args && current_function_pretend_args_size)
5485 store_arg_regs = 1;
5487 if (! volatile_func)
5488 for (reg = 0; reg <= 10; reg++)
5489 if (regs_ever_live[reg] && ! call_used_regs[reg])
5490 live_regs_mask |= 1 << reg;
5492 if (! volatile_func && regs_ever_live[14])
5493 live_regs_mask |= 0x4000;
5495 if (frame_pointer_needed)
5497 live_regs_mask |= 0xD800;
5498 emit_insn (gen_movsi (gen_rtx_REG (SImode, 12),
5499 stack_pointer_rtx));
5502 if (current_function_pretend_args_size)
5504 if (store_arg_regs)
5505 emit_multi_reg_push ((0xf0 >> (current_function_pretend_args_size / 4))
5506 & 0xf);
5507 else
5508 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
5509 GEN_INT (-current_function_pretend_args_size)));
5512 if (live_regs_mask)
5514 /* If we have to push any regs, then we must push lr as well, or
5515 we won't get a proper return. */
5516 live_regs_mask |= 0x4000;
5517 emit_multi_reg_push (live_regs_mask);
5520 /* For now the integer regs are still pushed in output_func_epilogue (). */
5522 if (! volatile_func)
5524 if (arm_fpu_arch == FP_SOFT2)
5526 for (reg = 23; reg > 15; reg--)
5527 if (regs_ever_live[reg] && ! call_used_regs[reg])
5528 emit_insn (gen_rtx_SET
5529 (VOIDmode,
5530 gen_rtx_MEM (XFmode,
5531 gen_rtx_PRE_DEC (XFmode,
5532 stack_pointer_rtx)),
5533 gen_rtx_REG (XFmode, reg)));
5535 else
5537 int start_reg = 23;
5539 for (reg = 23; reg > 15; reg--)
5541 if (regs_ever_live[reg] && ! call_used_regs[reg])
5543 if (start_reg - reg == 3)
5545 emit_sfm (reg, 4);
5546 start_reg = reg - 1;
5549 else
5551 if (start_reg != reg)
5552 emit_sfm (reg + 1, start_reg - reg);
5553 start_reg = reg - 1;
5557 if (start_reg != reg)
5558 emit_sfm (reg + 1, start_reg - reg);
5562 if (frame_pointer_needed)
5563 emit_insn (gen_addsi3 (hard_frame_pointer_rtx, gen_rtx_REG (SImode, 12),
5564 (GEN_INT
5565 (-(4 + current_function_pretend_args_size)))));
5567 if (amount != const0_rtx)
5569 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, amount));
5570 emit_insn (gen_rtx_CLOBBER (VOIDmode,
5571 gen_rtx_MEM (BLKmode, stack_pointer_rtx)));
5574 /* If we are profiling, make sure no instructions are scheduled before
5575 the call to mcount. Similarly if the user has requested no
5576 scheduling in the prolog. */
5577 if (profile_flag || profile_block_flag || TARGET_NO_SCHED_PRO)
5578 emit_insn (gen_blockage ());
5582 /* If CODE is 'd', then the X is a condition operand and the instruction
5583 should only be executed if the condition is true.
5584 if CODE is 'D', then the X is a condition operand and the instruction
5585 should only be executed if the condition is false: however, if the mode
5586 of the comparison is CCFPEmode, then always execute the instruction -- we
5587 do this because in these circumstances !GE does not necessarily imply LT;
5588 in these cases the instruction pattern will take care to make sure that
5589 an instruction containing %d will follow, thereby undoing the effects of
5590 doing this instruction unconditionally.
5591 If CODE is 'N' then X is a floating point operand that must be negated
5592 before output.
5593 If CODE is 'B' then output a bitwise inverted value of X (a const int).
5594 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
5596 void
5597 arm_print_operand (stream, x, code)
5598 FILE *stream;
5599 rtx x;
5600 int code;
5602 switch (code)
5604 case '@':
5605 fputs (ASM_COMMENT_START, stream);
5606 return;
5608 case '|':
5609 fputs (REGISTER_PREFIX, stream);
5610 return;
5612 case '?':
5613 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
5614 fputs (arm_condition_codes[arm_current_cc], stream);
5615 return;
5617 case 'N':
5619 REAL_VALUE_TYPE r;
5620 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5621 r = REAL_VALUE_NEGATE (r);
5622 fprintf (stream, "%s", fp_const_from_val (&r));
5624 return;
5626 case 'B':
5627 if (GET_CODE (x) == CONST_INT)
5628 fprintf (stream,
5629 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
5630 "%d",
5631 #else
5632 "%ld",
5633 #endif
5634 ARM_SIGN_EXTEND (~ INTVAL (x)));
5635 else
5637 putc ('~', stream);
5638 output_addr_const (stream, x);
5640 return;
5642 case 'i':
5643 fprintf (stream, "%s", arithmetic_instr (x, 1));
5644 return;
5646 case 'I':
5647 fprintf (stream, "%s", arithmetic_instr (x, 0));
5648 return;
5650 case 'S':
5652 HOST_WIDE_INT val;
5653 char *shift = shift_op (x, &val);
5655 if (shift)
5657 fprintf (stream, ", %s ", shift_op (x, &val));
5658 if (val == -1)
5659 arm_print_operand (stream, XEXP (x, 1), 0);
5660 else
5661 fprintf (stream,
5662 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
5663 "#%d",
5664 #else
5665 "#%ld",
5666 #endif
5667 val);
5670 return;
5672 case 'Q':
5673 if (REGNO (x) > 15)
5674 abort ();
5675 fputs (REGISTER_PREFIX, stream);
5676 fputs (reg_names[REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0)], stream);
5677 return;
5679 case 'R':
5680 if (REGNO (x) > 15)
5681 abort ();
5682 fputs (REGISTER_PREFIX, stream);
5683 fputs (reg_names[REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1)], stream);
5684 return;
5686 case 'm':
5687 fputs (REGISTER_PREFIX, stream);
5688 if (GET_CODE (XEXP (x, 0)) == REG)
5689 fputs (reg_names[REGNO (XEXP (x, 0))], stream);
5690 else
5691 fputs (reg_names[REGNO (XEXP (XEXP (x, 0), 0))], stream);
5692 return;
5694 case 'M':
5695 fprintf (stream, "{%s%s-%s%s}", REGISTER_PREFIX, reg_names[REGNO (x)],
5696 REGISTER_PREFIX, reg_names[REGNO (x) - 1
5697 + ((GET_MODE_SIZE (GET_MODE (x))
5698 + GET_MODE_SIZE (SImode) - 1)
5699 / GET_MODE_SIZE (SImode))]);
5700 return;
5702 case 'd':
5703 if (x)
5704 fputs (arm_condition_codes[get_arm_condition_code (x)],
5705 stream);
5706 return;
5708 case 'D':
5709 if (x)
5710 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
5711 (get_arm_condition_code (x))],
5712 stream);
5713 return;
5715 default:
5716 if (x == 0)
5717 abort ();
5719 if (GET_CODE (x) == REG)
5721 fputs (REGISTER_PREFIX, stream);
5722 fputs (reg_names[REGNO (x)], stream);
5724 else if (GET_CODE (x) == MEM)
5726 output_memory_reference_mode = GET_MODE (x);
5727 output_address (XEXP (x, 0));
5729 else if (GET_CODE (x) == CONST_DOUBLE)
5730 fprintf (stream, "#%s", fp_immediate_constant (x));
5731 else if (GET_CODE (x) == NEG)
5732 abort (); /* This should never happen now. */
5733 else
5735 fputc ('#', stream);
5736 output_addr_const (stream, x);
5742 /* A finite state machine takes care of noticing whether or not instructions
5743 can be conditionally executed, and thus decrease execution time and code
5744 size by deleting branch instructions. The fsm is controlled by
5745 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
5747 /* The state of the fsm controlling condition codes are:
5748 0: normal, do nothing special
5749 1: make ASM_OUTPUT_OPCODE not output this instruction
5750 2: make ASM_OUTPUT_OPCODE not output this instruction
5751 3: make instructions conditional
5752 4: make instructions conditional
5754 State transitions (state->state by whom under condition):
5755 0 -> 1 final_prescan_insn if the `target' is a label
5756 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
5757 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
5758 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
5759 3 -> 0 ASM_OUTPUT_INTERNAL_LABEL if the `target' label is reached
5760 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
5761 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
5762 (the target insn is arm_target_insn).
5764 If the jump clobbers the conditions then we use states 2 and 4.
5766 A similar thing can be done with conditional return insns.
5768 XXX In case the `target' is an unconditional branch, this conditionalising
5769 of the instructions always reduces code size, but not always execution
5770 time. But then, I want to reduce the code size to somewhere near what
5771 /bin/cc produces. */
5773 /* Returns the index of the ARM condition code string in
5774 `arm_condition_codes'. COMPARISON should be an rtx like
5775 `(eq (...) (...))'. */
5777 static enum arm_cond_code
5778 get_arm_condition_code (comparison)
5779 rtx comparison;
5781 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
5782 register int code;
5783 register enum rtx_code comp_code = GET_CODE (comparison);
5785 if (GET_MODE_CLASS (mode) != MODE_CC)
5786 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
5787 XEXP (comparison, 1));
5789 switch (mode)
5791 case CC_DNEmode: code = ARM_NE; goto dominance;
5792 case CC_DEQmode: code = ARM_EQ; goto dominance;
5793 case CC_DGEmode: code = ARM_GE; goto dominance;
5794 case CC_DGTmode: code = ARM_GT; goto dominance;
5795 case CC_DLEmode: code = ARM_LE; goto dominance;
5796 case CC_DLTmode: code = ARM_LT; goto dominance;
5797 case CC_DGEUmode: code = ARM_CS; goto dominance;
5798 case CC_DGTUmode: code = ARM_HI; goto dominance;
5799 case CC_DLEUmode: code = ARM_LS; goto dominance;
5800 case CC_DLTUmode: code = ARM_CC;
5802 dominance:
5803 if (comp_code != EQ && comp_code != NE)
5804 abort ();
5806 if (comp_code == EQ)
5807 return ARM_INVERSE_CONDITION_CODE (code);
5808 return code;
5810 case CC_NOOVmode:
5811 switch (comp_code)
5813 case NE: return ARM_NE;
5814 case EQ: return ARM_EQ;
5815 case GE: return ARM_PL;
5816 case LT: return ARM_MI;
5817 default: abort ();
5820 case CC_Zmode:
5821 case CCFPmode:
5822 switch (comp_code)
5824 case NE: return ARM_NE;
5825 case EQ: return ARM_EQ;
5826 default: abort ();
5829 case CCFPEmode:
5830 switch (comp_code)
5832 case GE: return ARM_GE;
5833 case GT: return ARM_GT;
5834 case LE: return ARM_LS;
5835 case LT: return ARM_MI;
5836 default: abort ();
5839 case CC_SWPmode:
5840 switch (comp_code)
5842 case NE: return ARM_NE;
5843 case EQ: return ARM_EQ;
5844 case GE: return ARM_LE;
5845 case GT: return ARM_LT;
5846 case LE: return ARM_GE;
5847 case LT: return ARM_GT;
5848 case GEU: return ARM_LS;
5849 case GTU: return ARM_CC;
5850 case LEU: return ARM_CS;
5851 case LTU: return ARM_HI;
5852 default: abort ();
5855 case CC_Cmode:
5856 switch (comp_code)
5858 case LTU: return ARM_CS;
5859 case GEU: return ARM_CC;
5860 default: abort ();
5863 case CCmode:
5864 switch (comp_code)
5866 case NE: return ARM_NE;
5867 case EQ: return ARM_EQ;
5868 case GE: return ARM_GE;
5869 case GT: return ARM_GT;
5870 case LE: return ARM_LE;
5871 case LT: return ARM_LT;
5872 case GEU: return ARM_CS;
5873 case GTU: return ARM_HI;
5874 case LEU: return ARM_LS;
5875 case LTU: return ARM_CC;
5876 default: abort ();
5879 default: abort ();
5882 abort ();
5886 void
5887 final_prescan_insn (insn, opvec, noperands)
5888 rtx insn;
5889 rtx *opvec;
5890 int noperands;
5892 /* BODY will hold the body of INSN. */
5893 register rtx body = PATTERN (insn);
5895 /* This will be 1 if trying to repeat the trick, and things need to be
5896 reversed if it appears to fail. */
5897 int reverse = 0;
5899 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
5900 taken are clobbered, even if the rtl suggests otherwise. It also
5901 means that we have to grub around within the jump expression to find
5902 out what the conditions are when the jump isn't taken. */
5903 int jump_clobbers = 0;
5905 /* If we start with a return insn, we only succeed if we find another one. */
5906 int seeking_return = 0;
5908 /* START_INSN will hold the insn from where we start looking. This is the
5909 first insn after the following code_label if REVERSE is true. */
5910 rtx start_insn = insn;
5912 /* If in state 4, check if the target branch is reached, in order to
5913 change back to state 0. */
5914 if (arm_ccfsm_state == 4)
5916 if (insn == arm_target_insn)
5918 arm_target_insn = NULL;
5919 arm_ccfsm_state = 0;
5921 return;
5924 /* If in state 3, it is possible to repeat the trick, if this insn is an
5925 unconditional branch to a label, and immediately following this branch
5926 is the previous target label which is only used once, and the label this
5927 branch jumps to is not too far off. */
5928 if (arm_ccfsm_state == 3)
5930 if (simplejump_p (insn))
5932 start_insn = next_nonnote_insn (start_insn);
5933 if (GET_CODE (start_insn) == BARRIER)
5935 /* XXX Isn't this always a barrier? */
5936 start_insn = next_nonnote_insn (start_insn);
5938 if (GET_CODE (start_insn) == CODE_LABEL
5939 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
5940 && LABEL_NUSES (start_insn) == 1)
5941 reverse = TRUE;
5942 else
5943 return;
5945 else if (GET_CODE (body) == RETURN)
5947 start_insn = next_nonnote_insn (start_insn);
5948 if (GET_CODE (start_insn) == BARRIER)
5949 start_insn = next_nonnote_insn (start_insn);
5950 if (GET_CODE (start_insn) == CODE_LABEL
5951 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
5952 && LABEL_NUSES (start_insn) == 1)
5954 reverse = TRUE;
5955 seeking_return = 1;
5957 else
5958 return;
5960 else
5961 return;
5964 if (arm_ccfsm_state != 0 && !reverse)
5965 abort ();
5966 if (GET_CODE (insn) != JUMP_INSN)
5967 return;
5969 /* This jump might be paralleled with a clobber of the condition codes
5970 the jump should always come first */
5971 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5972 body = XVECEXP (body, 0, 0);
5974 #if 0
5975 /* If this is a conditional return then we don't want to know */
5976 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
5977 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
5978 && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
5979 || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
5980 return;
5981 #endif
5983 if (reverse
5984 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
5985 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
5987 int insns_skipped;
5988 int fail = FALSE, succeed = FALSE;
5989 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
5990 int then_not_else = TRUE;
5991 rtx this_insn = start_insn, label = 0;
5993 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
5995 /* The code below is wrong for these, and I haven't time to
5996 fix it now. So we just do the safe thing and return. This
5997 whole function needs re-writing anyway. */
5998 jump_clobbers = 1;
5999 return;
6002 /* Register the insn jumped to. */
6003 if (reverse)
6005 if (!seeking_return)
6006 label = XEXP (SET_SRC (body), 0);
6008 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
6009 label = XEXP (XEXP (SET_SRC (body), 1), 0);
6010 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
6012 label = XEXP (XEXP (SET_SRC (body), 2), 0);
6013 then_not_else = FALSE;
6015 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
6016 seeking_return = 1;
6017 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
6019 seeking_return = 1;
6020 then_not_else = FALSE;
6022 else
6023 abort ();
6025 /* See how many insns this branch skips, and what kind of insns. If all
6026 insns are okay, and the label or unconditional branch to the same
6027 label is not too far away, succeed. */
6028 for (insns_skipped = 0;
6029 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
6031 rtx scanbody;
6033 this_insn = next_nonnote_insn (this_insn);
6034 if (!this_insn)
6035 break;
6037 switch (GET_CODE (this_insn))
6039 case CODE_LABEL:
6040 /* Succeed if it is the target label, otherwise fail since
6041 control falls in from somewhere else. */
6042 if (this_insn == label)
6044 if (jump_clobbers)
6046 arm_ccfsm_state = 2;
6047 this_insn = next_nonnote_insn (this_insn);
6049 else
6050 arm_ccfsm_state = 1;
6051 succeed = TRUE;
6053 else
6054 fail = TRUE;
6055 break;
6057 case BARRIER:
6058 /* Succeed if the following insn is the target label.
6059 Otherwise fail.
6060 If return insns are used then the last insn in a function
6061 will be a barrier. */
6062 this_insn = next_nonnote_insn (this_insn);
6063 if (this_insn && this_insn == label)
6065 if (jump_clobbers)
6067 arm_ccfsm_state = 2;
6068 this_insn = next_nonnote_insn (this_insn);
6070 else
6071 arm_ccfsm_state = 1;
6072 succeed = TRUE;
6074 else
6075 fail = TRUE;
6076 break;
6078 case CALL_INSN:
6079 /* If using 32-bit addresses the cc is not preserved over
6080 calls */
6081 if (TARGET_APCS_32)
6083 /* Succeed if the following insn is the target label,
6084 or if the following two insns are a barrier and
6085 the target label. */
6086 this_insn = next_nonnote_insn (this_insn);
6087 if (this_insn && GET_CODE (this_insn) == BARRIER)
6088 this_insn = next_nonnote_insn (this_insn);
6090 if (this_insn && this_insn == label
6091 && insns_skipped < max_insns_skipped)
6093 if (jump_clobbers)
6095 arm_ccfsm_state = 2;
6096 this_insn = next_nonnote_insn (this_insn);
6098 else
6099 arm_ccfsm_state = 1;
6100 succeed = TRUE;
6102 else
6103 fail = TRUE;
6105 break;
6107 case JUMP_INSN:
6108 /* If this is an unconditional branch to the same label, succeed.
6109 If it is to another label, do nothing. If it is conditional,
6110 fail. */
6111 /* XXX Probably, the tests for SET and the PC are unnecessary. */
6113 scanbody = PATTERN (this_insn);
6114 if (GET_CODE (scanbody) == SET
6115 && GET_CODE (SET_DEST (scanbody)) == PC)
6117 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
6118 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
6120 arm_ccfsm_state = 2;
6121 succeed = TRUE;
6123 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
6124 fail = TRUE;
6126 /* Fail if a conditional return is undesirable (eg on a
6127 StrongARM), but still allow this if optimizing for size. */
6128 else if (GET_CODE (scanbody) == RETURN
6129 && ! use_return_insn (TRUE)
6130 && ! optimize_size)
6131 fail = TRUE;
6132 else if (GET_CODE (scanbody) == RETURN
6133 && seeking_return)
6135 arm_ccfsm_state = 2;
6136 succeed = TRUE;
6138 else if (GET_CODE (scanbody) == PARALLEL)
6140 switch (get_attr_conds (this_insn))
6142 case CONDS_NOCOND:
6143 break;
6144 default:
6145 fail = TRUE;
6146 break;
6149 break;
6151 case INSN:
6152 /* Instructions using or affecting the condition codes make it
6153 fail. */
6154 scanbody = PATTERN (this_insn);
6155 if (! (GET_CODE (scanbody) == SET
6156 || GET_CODE (scanbody) == PARALLEL)
6157 || get_attr_conds (this_insn) != CONDS_NOCOND)
6158 fail = TRUE;
6159 break;
6161 default:
6162 break;
6165 if (succeed)
6167 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
6168 arm_target_label = CODE_LABEL_NUMBER (label);
6169 else if (seeking_return || arm_ccfsm_state == 2)
6171 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
6173 this_insn = next_nonnote_insn (this_insn);
6174 if (this_insn && (GET_CODE (this_insn) == BARRIER
6175 || GET_CODE (this_insn) == CODE_LABEL))
6176 abort ();
6178 if (!this_insn)
6180 /* Oh, dear! we ran off the end.. give up */
6181 recog (PATTERN (insn), insn, NULL_PTR);
6182 arm_ccfsm_state = 0;
6183 arm_target_insn = NULL;
6184 return;
6186 arm_target_insn = this_insn;
6188 else
6189 abort ();
6190 if (jump_clobbers)
6192 if (reverse)
6193 abort ();
6194 arm_current_cc =
6195 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
6196 0), 0), 1));
6197 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
6198 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
6199 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
6200 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
6202 else
6204 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
6205 what it was. */
6206 if (!reverse)
6207 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
6208 0));
6211 if (reverse || then_not_else)
6212 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
6214 /* restore recog_operand (getting the attributes of other insns can
6215 destroy this array, but final.c assumes that it remains intact
6216 across this call; since the insn has been recognized already we
6217 call recog direct). */
6218 recog (PATTERN (insn), insn, NULL_PTR);
6222 #ifdef AOF_ASSEMBLER
6223 /* Special functions only needed when producing AOF syntax assembler. */
6225 rtx aof_pic_label = NULL_RTX;
6226 struct pic_chain
6228 struct pic_chain *next;
6229 char *symname;
6232 static struct pic_chain *aof_pic_chain = NULL;
6235 aof_pic_entry (x)
6236 rtx x;
6238 struct pic_chain **chainp;
6239 int offset;
6241 if (aof_pic_label == NULL_RTX)
6243 /* This needs to persist throughout the compilation. */
6244 end_temporary_allocation ();
6245 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
6246 resume_temporary_allocation ();
6249 for (offset = 0, chainp = &aof_pic_chain; *chainp;
6250 offset += 4, chainp = &(*chainp)->next)
6251 if ((*chainp)->symname == XSTR (x, 0))
6252 return plus_constant (aof_pic_label, offset);
6254 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
6255 (*chainp)->next = NULL;
6256 (*chainp)->symname = XSTR (x, 0);
6257 return plus_constant (aof_pic_label, offset);
6260 void
6261 aof_dump_pic_table (f)
6262 FILE *f;
6264 struct pic_chain *chain;
6266 if (aof_pic_chain == NULL)
6267 return;
6269 fprintf (f, "\tAREA |%s$$adcons|, BASED %s%s\n",
6270 reg_names[PIC_OFFSET_TABLE_REGNUM], REGISTER_PREFIX,
6271 reg_names[PIC_OFFSET_TABLE_REGNUM]);
6272 fputs ("|x$adcons|\n", f);
6274 for (chain = aof_pic_chain; chain; chain = chain->next)
6276 fputs ("\tDCD\t", f);
6277 assemble_name (f, chain->symname);
6278 fputs ("\n", f);
6282 int arm_text_section_count = 1;
6284 char *
6285 aof_text_section ()
6287 static char buf[100];
6288 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
6289 arm_text_section_count++);
6290 if (flag_pic)
6291 strcat (buf, ", PIC, REENTRANT");
6292 return buf;
6295 static int arm_data_section_count = 1;
6297 char *
6298 aof_data_section ()
6300 static char buf[100];
6301 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
6302 return buf;
6305 /* The AOF assembler is religiously strict about declarations of
6306 imported and exported symbols, so that it is impossible to declare
6307 a function as imported near the beginning of the file, and then to
6308 export it later on. It is, however, possible to delay the decision
6309 until all the functions in the file have been compiled. To get
6310 around this, we maintain a list of the imports and exports, and
6311 delete from it any that are subsequently defined. At the end of
6312 compilation we spit the remainder of the list out before the END
6313 directive. */
6315 struct import
6317 struct import *next;
6318 char *name;
6321 static struct import *imports_list = NULL;
6323 void
6324 aof_add_import (name)
6325 char *name;
6327 struct import *new;
6329 for (new = imports_list; new; new = new->next)
6330 if (new->name == name)
6331 return;
6333 new = (struct import *) xmalloc (sizeof (struct import));
6334 new->next = imports_list;
6335 imports_list = new;
6336 new->name = name;
6339 void
6340 aof_delete_import (name)
6341 char *name;
6343 struct import **old;
6345 for (old = &imports_list; *old; old = & (*old)->next)
6347 if ((*old)->name == name)
6349 *old = (*old)->next;
6350 return;
6355 int arm_main_function = 0;
6357 void
6358 aof_dump_imports (f)
6359 FILE *f;
6361 /* The AOF assembler needs this to cause the startup code to be extracted
6362 from the library. Brining in __main causes the whole thing to work
6363 automagically. */
6364 if (arm_main_function)
6366 text_section ();
6367 fputs ("\tIMPORT __main\n", f);
6368 fputs ("\tDCD __main\n", f);
6371 /* Now dump the remaining imports. */
6372 while (imports_list)
6374 fprintf (f, "\tIMPORT\t");
6375 assemble_name (f, imports_list->name);
6376 fputc ('\n', f);
6377 imports_list = imports_list->next;
6380 #endif /* AOF_ASSEMBLER */